c++: fix throwing cleanup with label
[official-gcc.git] / gcc / cp / semantics.cc
bloba13c16f34a3fc60c6abd735b84e8fd785c4dc441
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.
6 Copyright (C) 1998-2023 Free Software Foundation, Inc.
7 Written by Mark Mitchell (mmitchell@usa.net) based on code found
8 formerly in parse.y and pt.cc.
10 This file is part of GCC.
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.
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.
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/>. */
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"
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. */
54 static tree maybe_convert_cond (tree);
55 static tree finalize_nrv_r (tree *, int *, void *);
56 static tree capture_decltype (tree);
58 /* Used for OpenMP non-static data member privatization. */
60 static hash_map<tree, tree> *omp_private_member_map;
61 static vec<tree> omp_private_member_vec;
62 static bool omp_private_member_ignore_next;
65 /* Deferred Access Checking Overview
66 ---------------------------------
68 Most C++ expressions and declarations require access checking
69 to be performed during parsing. However, in several cases,
70 this has to be treated differently.
72 For member declarations, access checking has to be deferred
73 until more information about the declaration is known. For
74 example:
76 class A {
77 typedef int X;
78 public:
79 X f();
82 A::X A::f();
83 A::X g();
85 When we are parsing the function return type `A::X', we don't
86 really know if this is allowed until we parse the function name.
88 Furthermore, some contexts require that access checking is
89 never performed at all. These include class heads, and template
90 instantiations.
92 Typical use of access checking functions is described here:
94 1. When we enter a context that requires certain access checking
95 mode, the function `push_deferring_access_checks' is called with
96 DEFERRING argument specifying the desired mode. Access checking
97 may be performed immediately (dk_no_deferred), deferred
98 (dk_deferred), or not performed (dk_no_check).
100 2. When a declaration such as a type, or a variable, is encountered,
101 the function `perform_or_defer_access_check' is called. It
102 maintains a vector of all deferred checks.
104 3. The global `current_class_type' or `current_function_decl' is then
105 setup by the parser. `enforce_access' relies on these information
106 to check access.
108 4. Upon exiting the context mentioned in step 1,
109 `perform_deferred_access_checks' is called to check all declaration
110 stored in the vector. `pop_deferring_access_checks' is then
111 called to restore the previous access checking mode.
113 In case of parsing error, we simply call `pop_deferring_access_checks'
114 without `perform_deferred_access_checks'. */
116 struct GTY(()) deferred_access {
117 /* A vector representing name-lookups for which we have deferred
118 checking access controls. We cannot check the accessibility of
119 names used in a decl-specifier-seq until we know what is being
120 declared because code like:
122 class A {
123 class B {};
124 B* f();
127 A::B* A::f() { return 0; }
129 is valid, even though `A::B' is not generally accessible. */
130 vec<deferred_access_check, va_gc> *deferred_access_checks;
132 /* The current mode of access checks. */
133 enum deferring_kind deferring_access_checks_kind;
136 /* Data for deferred access checking. */
137 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
138 static GTY(()) unsigned deferred_access_no_check;
140 /* Save the current deferred access states and start deferred
141 access checking iff DEFER_P is true. */
143 void
144 push_deferring_access_checks (deferring_kind deferring)
146 /* For context like template instantiation, access checking
147 disabling applies to all nested context. */
148 if (deferred_access_no_check || deferring == dk_no_check)
149 deferred_access_no_check++;
150 else
152 deferred_access e = {NULL, deferring};
153 vec_safe_push (deferred_access_stack, e);
157 /* Save the current deferred access states and start deferred access
158 checking, continuing the set of deferred checks in CHECKS. */
160 void
161 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
163 push_deferring_access_checks (dk_deferred);
164 if (!deferred_access_no_check)
165 deferred_access_stack->last().deferred_access_checks = checks;
168 /* Resume deferring access checks again after we stopped doing
169 this previously. */
171 void
172 resume_deferring_access_checks (void)
174 if (!deferred_access_no_check)
175 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
178 /* Stop deferring access checks. */
180 void
181 stop_deferring_access_checks (void)
183 if (!deferred_access_no_check)
184 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
187 /* Discard the current deferred access checks and restore the
188 previous states. */
190 void
191 pop_deferring_access_checks (void)
193 if (deferred_access_no_check)
194 deferred_access_no_check--;
195 else
196 deferred_access_stack->pop ();
199 /* Returns a TREE_LIST representing the deferred checks.
200 The TREE_PURPOSE of each node is the type through which the
201 access occurred; the TREE_VALUE is the declaration named.
204 vec<deferred_access_check, va_gc> *
205 get_deferred_access_checks (void)
207 if (deferred_access_no_check)
208 return NULL;
209 else
210 return (deferred_access_stack->last().deferred_access_checks);
213 /* Take current deferred checks and combine with the
214 previous states if we also defer checks previously.
215 Otherwise perform checks now. */
217 void
218 pop_to_parent_deferring_access_checks (void)
220 if (deferred_access_no_check)
221 deferred_access_no_check--;
222 else
224 vec<deferred_access_check, va_gc> *checks;
225 deferred_access *ptr;
227 checks = (deferred_access_stack->last ().deferred_access_checks);
229 deferred_access_stack->pop ();
230 ptr = &deferred_access_stack->last ();
231 if (ptr->deferring_access_checks_kind == dk_no_deferred)
233 /* Check access. */
234 perform_access_checks (checks, tf_warning_or_error);
236 else
238 /* Merge with parent. */
239 int i, j;
240 deferred_access_check *chk, *probe;
242 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
244 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
246 if (probe->binfo == chk->binfo &&
247 probe->decl == chk->decl &&
248 probe->diag_decl == chk->diag_decl)
249 goto found;
251 /* Insert into parent's checks. */
252 vec_safe_push (ptr->deferred_access_checks, *chk);
253 found:;
259 /* Called from enforce_access. A class has attempted (but failed) to access
260 DECL. It is already established that a baseclass of that class,
261 PARENT_BINFO, has private access to DECL. Examine certain special cases
262 to find a decl that accurately describes the source of the problem. If
263 none of the special cases apply, simply return DECL as the source of the
264 problem. */
266 static tree
267 get_class_access_diagnostic_decl (tree parent_binfo, tree decl)
269 /* When a class is denied access to a decl in a baseclass, most of the
270 time it is because the decl itself was declared as private at the point
271 of declaration.
273 However, in C++, there are (at least) two situations in which a decl
274 can be private even though it was not originally defined as such.
275 These two situations only apply if a baseclass had private access to
276 DECL (this function is only called if that is the case). */
278 /* We should first check whether the reason the parent had private access
279 to DECL was simply because DECL was created and declared as private in
280 the parent. If it was, then DECL is definitively the source of the
281 problem. */
282 if (SAME_BINFO_TYPE_P (context_for_name_lookup (decl),
283 BINFO_TYPE (parent_binfo)))
284 return decl;
286 /* 1. If the "using" keyword is used to inherit DECL within the parent,
287 this may cause DECL to be private, so we should return the using
288 statement as the source of the problem.
290 Scan the fields of PARENT_BINFO and see if there are any using decls. If
291 there are, see if they inherit DECL. If they do, that's where DECL must
292 have been declared private. */
294 for (tree parent_field = TYPE_FIELDS (BINFO_TYPE (parent_binfo));
295 parent_field;
296 parent_field = DECL_CHAIN (parent_field))
297 /* Not necessary, but also check TREE_PRIVATE for the sake of
298 eliminating obviously non-relevant using decls. */
299 if (TREE_CODE (parent_field) == USING_DECL
300 && TREE_PRIVATE (parent_field))
302 tree decl_stripped = strip_using_decl (parent_field);
304 /* The using statement might be overloaded. If so, we need to
305 check all of the overloads. */
306 for (ovl_iterator iter (decl_stripped); iter; ++iter)
307 /* If equal, the using statement inherits DECL, and so is the
308 source of the access failure, so return it. */
309 if (*iter == decl)
310 return parent_field;
313 /* 2. If DECL was privately inherited by the parent class, then DECL will
314 be inaccessible, even though it may originally have been accessible to
315 deriving classes. In that case, the fault lies with the parent, since it
316 used a private inheritance, so we return the parent as the source of the
317 problem.
319 Since this is the last check, we just assume it's true. At worst, it
320 will simply point to the class that failed to give access, which is
321 technically true. */
322 return TYPE_NAME (BINFO_TYPE (parent_binfo));
325 /* If the current scope isn't allowed to access DECL along
326 BASETYPE_PATH, give an error, or if we're parsing a function or class
327 template, defer the access check to be performed at instantiation time.
328 The most derived class in BASETYPE_PATH is the one used to qualify DECL.
329 DIAG_DECL is the declaration to use in the error diagnostic. */
331 static bool
332 enforce_access (tree basetype_path, tree decl, tree diag_decl,
333 tsubst_flags_t complain, access_failure_info *afi = NULL)
335 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
337 if (flag_new_inheriting_ctors
338 && DECL_INHERITED_CTOR (decl))
340 /* 7.3.3/18: The additional constructors are accessible if they would be
341 accessible when used to construct an object of the corresponding base
342 class. */
343 decl = strip_inheriting_ctors (decl);
344 basetype_path = lookup_base (basetype_path, DECL_CONTEXT (decl),
345 ba_any, NULL, complain);
348 tree cs = current_scope ();
349 if (in_template_context
350 && (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL))
351 if (tree template_info = get_template_info (cs))
353 /* When parsing a function or class template, we in general need to
354 defer access checks until template instantiation time, since a friend
355 declaration may grant access only to a particular specialization of
356 the template. */
358 if (accessible_p (basetype_path, decl, /*consider_local_p=*/true))
359 /* But if the member is deemed accessible at parse time, then we can
360 assume it'll be accessible at instantiation time. */
361 return true;
363 /* Access of a dependent decl should be rechecked after tsubst'ing
364 into the user of the decl, rather than explicitly deferring the
365 check here. */
366 gcc_assert (!uses_template_parms (decl));
367 if (TREE_CODE (decl) == FIELD_DECL)
368 gcc_assert (!uses_template_parms (DECL_CONTEXT (decl)));
370 /* Defer this access check until instantiation time. */
371 deferred_access_check access_check;
372 access_check.binfo = basetype_path;
373 access_check.decl = decl;
374 access_check.diag_decl = diag_decl;
375 access_check.loc = input_location;
376 vec_safe_push (TI_DEFERRED_ACCESS_CHECKS (template_info), access_check);
377 return true;
380 if (!accessible_p (basetype_path, decl, /*consider_local_p=*/true))
382 if (flag_new_inheriting_ctors)
383 diag_decl = strip_inheriting_ctors (diag_decl);
384 if (complain & tf_error)
386 access_kind access_failure_reason = ak_none;
388 /* By default, using the decl as the source of the problem will
389 usually give correct results. */
390 tree diag_location = diag_decl;
392 /* However, if a parent of BASETYPE_PATH had private access to decl,
393 then it actually might be the case that the source of the problem
394 is not DECL. */
395 tree parent_binfo = get_parent_with_private_access (decl,
396 basetype_path);
398 /* So if a parent did have private access, then we need to do
399 special checks to obtain the best diagnostic location decl. */
400 if (parent_binfo != NULL_TREE)
402 diag_location = get_class_access_diagnostic_decl (parent_binfo,
403 diag_decl);
405 /* We also at this point know that the reason access failed was
406 because decl was private. */
407 access_failure_reason = ak_private;
410 /* Finally, generate an error message. */
411 complain_about_access (decl, diag_decl, diag_location, true,
412 access_failure_reason);
414 if (afi)
415 afi->record_access_failure (basetype_path, decl, diag_decl);
416 return false;
419 return true;
422 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
423 is the BINFO indicating the qualifying scope used to access the
424 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
425 or we aren't in SFINAE context or all the checks succeed return TRUE,
426 otherwise FALSE. */
428 bool
429 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
430 tsubst_flags_t complain)
432 int i;
433 deferred_access_check *chk;
434 location_t loc = input_location;
435 bool ok = true;
437 if (!checks)
438 return true;
440 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
442 input_location = chk->loc;
443 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
446 input_location = loc;
447 return (complain & tf_error) ? true : ok;
450 /* Perform the deferred access checks.
452 After performing the checks, we still have to keep the list
453 `deferred_access_stack->deferred_access_checks' since we may want
454 to check access for them again later in a different context.
455 For example:
457 class A {
458 typedef int X;
459 static X a;
461 A::X A::a, x; // No error for `A::a', error for `x'
463 We have to perform deferred access of `A::X', first with `A::a',
464 next with `x'. Return value like perform_access_checks above. */
466 bool
467 perform_deferred_access_checks (tsubst_flags_t complain)
469 return perform_access_checks (get_deferred_access_checks (), complain);
472 /* Defer checking the accessibility of DECL, when looked up in
473 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
474 Return value like perform_access_checks above.
475 If non-NULL, report failures to AFI. */
477 bool
478 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
479 tsubst_flags_t complain,
480 access_failure_info *afi)
482 int i;
483 deferred_access *ptr;
484 deferred_access_check *chk;
486 /* Exit if we are in a context that no access checking is performed. */
487 if (deferred_access_no_check)
488 return true;
490 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
492 ptr = &deferred_access_stack->last ();
494 /* If we are not supposed to defer access checks, just check now. */
495 if (ptr->deferring_access_checks_kind == dk_no_deferred)
497 bool ok = enforce_access (binfo, decl, diag_decl, complain, afi);
498 return (complain & tf_error) ? true : ok;
501 /* See if we are already going to perform this check. */
502 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
504 if (chk->decl == decl && chk->binfo == binfo &&
505 chk->diag_decl == diag_decl)
507 return true;
510 /* If not, record the check. */
511 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
512 vec_safe_push (ptr->deferred_access_checks, new_access);
514 return true;
517 /* Returns nonzero if the current statement is a full expression,
518 i.e. temporaries created during that statement should be destroyed
519 at the end of the statement. */
522 stmts_are_full_exprs_p (void)
524 return current_stmt_tree ()->stmts_are_full_exprs_p;
527 /* T is a statement. Add it to the statement-tree. This is the C++
528 version. The C/ObjC frontends have a slightly different version of
529 this function. */
531 tree
532 add_stmt (tree t)
534 enum tree_code code = TREE_CODE (t);
536 if (EXPR_P (t) && code != LABEL_EXPR)
538 if (!EXPR_HAS_LOCATION (t))
539 SET_EXPR_LOCATION (t, input_location);
541 /* When we expand a statement-tree, we must know whether or not the
542 statements are full-expressions. We record that fact here. */
543 if (STATEMENT_CODE_P (TREE_CODE (t)))
544 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
547 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
548 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
550 /* Add T to the statement-tree. Non-side-effect statements need to be
551 recorded during statement expressions. */
552 gcc_checking_assert (!stmt_list_stack->is_empty ());
553 append_to_statement_list_force (t, &cur_stmt_list);
555 return t;
558 /* Returns the stmt_tree to which statements are currently being added. */
560 stmt_tree
561 current_stmt_tree (void)
563 return (cfun
564 ? &cfun->language->base.x_stmt_tree
565 : &scope_chain->x_stmt_tree);
568 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
570 static tree
571 maybe_cleanup_point_expr (tree expr)
573 if (!processing_template_decl && stmts_are_full_exprs_p ())
574 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
575 return expr;
578 /* Like maybe_cleanup_point_expr except have the type of the new expression be
579 void so we don't need to create a temporary variable to hold the inner
580 expression. The reason why we do this is because the original type might be
581 an aggregate and we cannot create a temporary variable for that type. */
583 tree
584 maybe_cleanup_point_expr_void (tree expr)
586 if (!processing_template_decl && stmts_are_full_exprs_p ())
587 expr = fold_build_cleanup_point_expr (void_type_node, expr);
588 return expr;
593 /* Create a declaration statement for the declaration given by the DECL. */
595 void
596 add_decl_expr (tree decl)
598 tree r = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
599 if (DECL_INITIAL (decl)
600 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
601 r = maybe_cleanup_point_expr_void (r);
602 add_stmt (r);
605 /* Set EXPR_LOCATION of the cleanups of any CLEANUP_STMT in STMTS to LOC. */
607 static void
608 set_cleanup_locs (tree stmts, location_t loc)
610 if (TREE_CODE (stmts) == CLEANUP_STMT)
612 tree t = CLEANUP_EXPR (stmts);
613 if (t && TREE_CODE (t) != POSTCONDITION_STMT)
614 protected_set_expr_location (t, loc);
615 /* Avoid locus differences for C++ cdtor calls depending on whether
616 cdtor_returns_this: a conversion to void is added to discard the return
617 value, and this conversion ends up carrying the location, and when it
618 gets discarded, the location is lost. So hold it in the call as
619 well. */
620 if (TREE_CODE (t) == NOP_EXPR
621 && TREE_TYPE (t) == void_type_node
622 && TREE_CODE (TREE_OPERAND (t, 0)) == CALL_EXPR)
623 protected_set_expr_location (TREE_OPERAND (t, 0), loc);
624 set_cleanup_locs (CLEANUP_BODY (stmts), loc);
626 else if (TREE_CODE (stmts) == STATEMENT_LIST)
627 for (tree stmt : tsi_range (stmts))
628 set_cleanup_locs (stmt, loc);
631 /* True iff the innermost block scope is a try block. */
633 static bool
634 at_try_scope ()
636 cp_binding_level *b = current_binding_level;
637 while (b && b->kind == sk_cleanup)
638 b = b->level_chain;
639 return b && b->kind == sk_try;
642 /* Finish a scope. */
644 tree
645 do_poplevel (tree stmt_list)
647 tree block = NULL;
649 bool was_try = at_try_scope ();
651 if (stmts_are_full_exprs_p ())
652 block = poplevel (kept_level_p (), 1, 0);
654 /* This needs to come after poplevel merges sk_cleanup statement_lists. */
655 maybe_splice_retval_cleanup (stmt_list, was_try);
657 stmt_list = pop_stmt_list (stmt_list);
659 /* input_location is the last token of the scope, usually a }. */
660 set_cleanup_locs (stmt_list, input_location);
662 if (!processing_template_decl)
664 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
665 /* ??? See c_end_compound_stmt re statement expressions. */
668 return stmt_list;
671 /* Begin a new scope. */
673 static tree
674 do_pushlevel (scope_kind sk)
676 tree ret = push_stmt_list ();
677 if (stmts_are_full_exprs_p ())
678 begin_scope (sk, NULL);
679 return ret;
682 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
683 when the current scope is exited. EH_ONLY is true when this is not
684 meant to apply to normal control flow transfer. DECL is the VAR_DECL
685 being cleaned up, if any, or null for temporaries or subobjects. */
687 void
688 push_cleanup (tree decl, tree cleanup, bool eh_only)
690 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
691 CLEANUP_EH_ONLY (stmt) = eh_only;
692 add_stmt (stmt);
693 CLEANUP_BODY (stmt) = push_stmt_list ();
696 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
697 the current loops, represented by 'NULL_TREE' if we've seen a possible
698 exit, and 'error_mark_node' if not. This is currently used only to
699 suppress the warning about a function with no return statements, and
700 therefore we don't bother noting returns as possible exits. We also
701 don't bother with gotos. */
703 static void
704 begin_maybe_infinite_loop (tree cond)
706 /* Only track this while parsing a function, not during instantiation. */
707 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
708 && !processing_template_decl))
709 return;
710 bool maybe_infinite = true;
711 if (cond)
713 cond = fold_non_dependent_expr (cond);
714 maybe_infinite = integer_nonzerop (cond);
716 vec_safe_push (cp_function_chain->infinite_loops,
717 maybe_infinite ? error_mark_node : NULL_TREE);
721 /* A break is a possible exit for the current loop. */
723 void
724 break_maybe_infinite_loop (void)
726 if (!cfun)
727 return;
728 cp_function_chain->infinite_loops->last() = NULL_TREE;
731 /* If we reach the end of the loop without seeing a possible exit, we have
732 an infinite loop. */
734 static void
735 end_maybe_infinite_loop (tree cond)
737 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
738 && !processing_template_decl))
739 return;
740 tree current = cp_function_chain->infinite_loops->pop();
741 if (current != NULL_TREE)
743 cond = fold_non_dependent_expr (cond);
744 if (integer_nonzerop (cond))
745 current_function_infinite_loop = 1;
749 /* Begin a conditional that might contain a declaration. When generating
750 normal code, we want the declaration to appear before the statement
751 containing the conditional. When generating template code, we want the
752 conditional to be rendered as the raw DECL_EXPR. */
754 static void
755 begin_cond (tree *cond_p)
757 if (processing_template_decl)
758 *cond_p = push_stmt_list ();
761 /* Finish such a conditional. */
763 static void
764 finish_cond (tree *cond_p, tree expr)
766 if (processing_template_decl)
768 tree cond = pop_stmt_list (*cond_p);
770 if (expr == NULL_TREE)
771 /* Empty condition in 'for'. */
772 gcc_assert (empty_expr_stmt_p (cond));
773 else if (check_for_bare_parameter_packs (expr))
774 expr = error_mark_node;
775 else if (!empty_expr_stmt_p (cond))
776 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
778 *cond_p = expr;
781 /* If *COND_P specifies a conditional with a declaration, transform the
782 loop such that
783 while (A x = 42) { }
784 for (; A x = 42;) { }
785 becomes
786 while (true) { A x = 42; if (!x) break; }
787 for (;;) { A x = 42; if (!x) break; }
788 The statement list for BODY will be empty if the conditional did
789 not declare anything. */
791 static void
792 simplify_loop_decl_cond (tree *cond_p, tree body)
794 tree cond, if_stmt;
796 if (!TREE_SIDE_EFFECTS (body))
797 return;
799 cond = *cond_p;
800 *cond_p = boolean_true_node;
802 if_stmt = begin_if_stmt ();
803 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, false, tf_warning_or_error);
804 finish_if_stmt_cond (cond, if_stmt);
805 finish_break_stmt ();
806 finish_then_clause (if_stmt);
807 finish_if_stmt (if_stmt);
810 /* Finish a goto-statement. */
812 tree
813 finish_goto_stmt (tree destination)
815 if (identifier_p (destination))
816 destination = lookup_label (destination);
818 /* We warn about unused labels with -Wunused. That means we have to
819 mark the used labels as used. */
820 if (TREE_CODE (destination) == LABEL_DECL)
821 TREE_USED (destination) = 1;
822 else
824 destination = mark_rvalue_use (destination);
825 if (!processing_template_decl)
827 destination = cp_convert (ptr_type_node, destination,
828 tf_warning_or_error);
829 if (error_operand_p (destination))
830 return NULL_TREE;
831 destination
832 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
833 destination);
837 check_goto (destination);
839 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
840 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
843 /* Returns true if CALL is a (possibly wrapped) CALL_EXPR or AGGR_INIT_EXPR
844 to operator= () that is written as an operator expression. */
845 static bool
846 is_assignment_op_expr_p (tree call)
848 if (call == NULL_TREE)
849 return false;
851 call = extract_call_expr (call);
852 if (call == NULL_TREE
853 || call == error_mark_node
854 || !CALL_EXPR_OPERATOR_SYNTAX (call))
855 return false;
857 tree fndecl = cp_get_callee_fndecl_nofold (call);
858 return fndecl != NULL_TREE
859 && DECL_ASSIGNMENT_OPERATOR_P (fndecl)
860 && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR);
863 /* COND is the condition-expression for an if, while, etc.,
864 statement. Convert it to a boolean value, if appropriate.
865 In addition, verify sequence points if -Wsequence-point is enabled. */
867 static tree
868 maybe_convert_cond (tree cond)
870 /* Empty conditions remain empty. */
871 if (!cond)
872 return NULL_TREE;
874 /* Wait until we instantiate templates before doing conversion. */
875 if (type_dependent_expression_p (cond))
876 return cond;
878 if (warn_sequence_point && !processing_template_decl)
879 verify_sequence_points (cond);
881 /* Do the conversion. */
882 cond = convert_from_reference (cond);
884 if ((TREE_CODE (cond) == MODIFY_EXPR || is_assignment_op_expr_p (cond))
885 && warn_parentheses
886 && !warning_suppressed_p (cond, OPT_Wparentheses)
887 && warning_at (cp_expr_loc_or_input_loc (cond),
888 OPT_Wparentheses, "suggest parentheses around "
889 "assignment used as truth value"))
890 suppress_warning (cond, OPT_Wparentheses);
892 return condition_conversion (cond);
895 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
897 tree
898 finish_expr_stmt (tree expr)
900 tree r = NULL_TREE;
901 location_t loc = EXPR_LOCATION (expr);
903 if (expr != NULL_TREE)
905 /* If we ran into a problem, make sure we complained. */
906 gcc_assert (expr != error_mark_node || seen_error ());
908 if (!processing_template_decl)
910 if (warn_sequence_point)
911 verify_sequence_points (expr);
912 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
914 else if (!type_dependent_expression_p (expr))
915 convert_to_void (build_non_dependent_expr (expr), ICV_STATEMENT,
916 tf_warning_or_error);
918 if (check_for_bare_parameter_packs (expr))
919 expr = error_mark_node;
921 /* Simplification of inner statement expressions, compound exprs,
922 etc can result in us already having an EXPR_STMT. */
923 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
925 if (TREE_CODE (expr) != EXPR_STMT)
926 expr = build_stmt (loc, EXPR_STMT, expr);
927 expr = maybe_cleanup_point_expr_void (expr);
930 r = add_stmt (expr);
933 return r;
937 /* Begin an if-statement. Returns a newly created IF_STMT if
938 appropriate. */
940 tree
941 begin_if_stmt (void)
943 tree r, scope;
944 scope = do_pushlevel (sk_cond);
945 r = build_stmt (input_location, IF_STMT, NULL_TREE,
946 NULL_TREE, NULL_TREE, scope);
947 current_binding_level->this_entity = r;
948 begin_cond (&IF_COND (r));
949 return r;
952 /* Returns true if FN, a CALL_EXPR, is a call to
953 std::is_constant_evaluated or __builtin_is_constant_evaluated. */
955 static bool
956 is_std_constant_evaluated_p (tree fn)
958 /* std::is_constant_evaluated takes no arguments. */
959 if (call_expr_nargs (fn) != 0)
960 return false;
962 tree fndecl = cp_get_callee_fndecl_nofold (fn);
963 if (fndecl == NULL_TREE)
964 return false;
966 if (fndecl_built_in_p (fndecl, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
967 BUILT_IN_FRONTEND))
968 return true;
970 if (!decl_in_std_namespace_p (fndecl))
971 return false;
973 tree name = DECL_NAME (fndecl);
974 return name && id_equal (name, "is_constant_evaluated");
977 /* Callback function for maybe_warn_for_constant_evaluated that looks
978 for calls to std::is_constant_evaluated in TP. */
980 static tree
981 find_std_constant_evaluated_r (tree *tp, int *walk_subtrees, void *)
983 tree t = *tp;
985 if (TYPE_P (t) || TREE_CONSTANT (t))
987 *walk_subtrees = false;
988 return NULL_TREE;
991 switch (TREE_CODE (t))
993 case CALL_EXPR:
994 if (is_std_constant_evaluated_p (t))
995 return t;
996 break;
997 case EXPR_STMT:
998 /* Don't warn in statement expressions. */
999 *walk_subtrees = false;
1000 return NULL_TREE;
1001 default:
1002 break;
1005 return NULL_TREE;
1008 /* In certain contexts, std::is_constant_evaluated() is always true (for
1009 instance, in a consteval function or in a constexpr if), or always false
1010 (e.g., in a non-constexpr non-consteval function) so give the user a clue. */
1012 static void
1013 maybe_warn_for_constant_evaluated (tree cond, bool constexpr_if)
1015 if (!warn_tautological_compare)
1016 return;
1018 /* Suppress warning for std::is_constant_evaluated if the conditional
1019 comes from a macro. */
1020 if (from_macro_expansion_at (EXPR_LOCATION (cond)))
1021 return;
1023 cond = cp_walk_tree_without_duplicates (&cond, find_std_constant_evaluated_r,
1024 NULL);
1025 if (cond)
1027 if (constexpr_if)
1028 warning_at (EXPR_LOCATION (cond), OPT_Wtautological_compare,
1029 "%<std::is_constant_evaluated%> always evaluates to "
1030 "true in %<if constexpr%>");
1031 else if (!maybe_constexpr_fn (current_function_decl))
1032 warning_at (EXPR_LOCATION (cond), OPT_Wtautological_compare,
1033 "%<std::is_constant_evaluated%> always evaluates to "
1034 "false in a non-%<constexpr%> function");
1035 else if (DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
1036 warning_at (EXPR_LOCATION (cond), OPT_Wtautological_compare,
1037 "%<std::is_constant_evaluated%> always evaluates to "
1038 "true in a %<consteval%> function");
1042 /* Process the COND of an if-statement, which may be given by
1043 IF_STMT. */
1045 tree
1046 finish_if_stmt_cond (tree orig_cond, tree if_stmt)
1048 tree cond = maybe_convert_cond (orig_cond);
1049 if (IF_STMT_CONSTEXPR_P (if_stmt)
1050 && !type_dependent_expression_p (cond)
1051 && require_constant_expression (cond)
1052 && !instantiation_dependent_expression_p (cond)
1053 /* Wait until instantiation time, since only then COND has been
1054 converted to bool. */
1055 && TYPE_MAIN_VARIANT (TREE_TYPE (cond)) == boolean_type_node)
1057 maybe_warn_for_constant_evaluated (cond, /*constexpr_if=*/true);
1058 cond = instantiate_non_dependent_expr (cond);
1059 cond = cxx_constant_value (cond);
1061 else
1063 maybe_warn_for_constant_evaluated (cond, /*constexpr_if=*/false);
1064 if (processing_template_decl)
1065 cond = orig_cond;
1067 finish_cond (&IF_COND (if_stmt), cond);
1068 add_stmt (if_stmt);
1069 THEN_CLAUSE (if_stmt) = push_stmt_list ();
1070 return cond;
1073 /* Finish the then-clause of an if-statement, which may be given by
1074 IF_STMT. */
1076 tree
1077 finish_then_clause (tree if_stmt)
1079 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
1080 return if_stmt;
1083 /* Begin the else-clause of an if-statement. */
1085 void
1086 begin_else_clause (tree if_stmt)
1088 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
1091 /* Finish the else-clause of an if-statement, which may be given by
1092 IF_STMT. */
1094 void
1095 finish_else_clause (tree if_stmt)
1097 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
1100 /* Callback for cp_walk_tree to mark all {VAR,PARM}_DECLs in a tree as
1101 read. */
1103 static tree
1104 maybe_mark_exp_read_r (tree *tp, int *, void *)
1106 tree t = *tp;
1107 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1108 mark_exp_read (t);
1109 return NULL_TREE;
1112 /* Finish an if-statement. */
1114 void
1115 finish_if_stmt (tree if_stmt)
1117 tree scope = IF_SCOPE (if_stmt);
1118 IF_SCOPE (if_stmt) = NULL;
1119 if (IF_STMT_CONSTEXPR_P (if_stmt))
1121 /* Prevent various -Wunused warnings. We might not instantiate
1122 either of these branches, so we would not mark the variables
1123 used in that branch as read. */
1124 cp_walk_tree_without_duplicates (&THEN_CLAUSE (if_stmt),
1125 maybe_mark_exp_read_r, NULL);
1126 cp_walk_tree_without_duplicates (&ELSE_CLAUSE (if_stmt),
1127 maybe_mark_exp_read_r, NULL);
1129 add_stmt (do_poplevel (scope));
1132 /* Begin a while-statement. Returns a newly created WHILE_STMT if
1133 appropriate. */
1135 tree
1136 begin_while_stmt (void)
1138 tree r;
1139 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
1140 add_stmt (r);
1141 WHILE_BODY (r) = do_pushlevel (sk_block);
1142 begin_cond (&WHILE_COND (r));
1143 return r;
1146 /* Process the COND of a while-statement, which may be given by
1147 WHILE_STMT. */
1149 void
1150 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep,
1151 unsigned short unroll)
1153 cond = maybe_convert_cond (cond);
1154 finish_cond (&WHILE_COND (while_stmt), cond);
1155 begin_maybe_infinite_loop (cond);
1156 if (ivdep && cond != error_mark_node)
1157 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
1158 TREE_TYPE (WHILE_COND (while_stmt)),
1159 WHILE_COND (while_stmt),
1160 build_int_cst (integer_type_node,
1161 annot_expr_ivdep_kind),
1162 integer_zero_node);
1163 if (unroll && cond != error_mark_node)
1164 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
1165 TREE_TYPE (WHILE_COND (while_stmt)),
1166 WHILE_COND (while_stmt),
1167 build_int_cst (integer_type_node,
1168 annot_expr_unroll_kind),
1169 build_int_cst (integer_type_node,
1170 unroll));
1171 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
1174 /* Finish a while-statement, which may be given by WHILE_STMT. */
1176 void
1177 finish_while_stmt (tree while_stmt)
1179 end_maybe_infinite_loop (boolean_true_node);
1180 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
1183 /* Begin a do-statement. Returns a newly created DO_STMT if
1184 appropriate. */
1186 tree
1187 begin_do_stmt (void)
1189 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
1190 begin_maybe_infinite_loop (boolean_true_node);
1191 add_stmt (r);
1192 DO_BODY (r) = push_stmt_list ();
1193 return r;
1196 /* Finish the body of a do-statement, which may be given by DO_STMT. */
1198 void
1199 finish_do_body (tree do_stmt)
1201 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
1203 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
1204 body = STATEMENT_LIST_TAIL (body)->stmt;
1206 if (IS_EMPTY_STMT (body))
1207 warning (OPT_Wempty_body,
1208 "suggest explicit braces around empty body in %<do%> statement");
1211 /* Finish a do-statement, which may be given by DO_STMT, and whose
1212 COND is as indicated. */
1214 void
1215 finish_do_stmt (tree cond, tree do_stmt, bool ivdep, unsigned short unroll)
1217 cond = maybe_convert_cond (cond);
1218 end_maybe_infinite_loop (cond);
1219 /* Unlike other iteration statements, the condition may not contain
1220 a declaration, so we don't call finish_cond which checks for
1221 unexpanded parameter packs. */
1222 if (check_for_bare_parameter_packs (cond))
1223 cond = error_mark_node;
1224 if (ivdep && cond != error_mark_node)
1225 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
1226 build_int_cst (integer_type_node, annot_expr_ivdep_kind),
1227 integer_zero_node);
1228 if (unroll && cond != error_mark_node)
1229 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
1230 build_int_cst (integer_type_node, annot_expr_unroll_kind),
1231 build_int_cst (integer_type_node, unroll));
1232 DO_COND (do_stmt) = cond;
1235 /* Finish a return-statement. The EXPRESSION returned, if any, is as
1236 indicated. */
1238 tree
1239 finish_return_stmt (tree expr)
1241 tree r;
1242 bool no_warning;
1244 expr = check_return_expr (expr, &no_warning);
1246 if (error_operand_p (expr)
1247 || (flag_openmp && !check_omp_return ()))
1249 /* Suppress -Wreturn-type for this function. */
1250 if (warn_return_type)
1251 suppress_warning (current_function_decl, OPT_Wreturn_type);
1252 return error_mark_node;
1255 if (!processing_template_decl)
1257 if (warn_sequence_point)
1258 verify_sequence_points (expr);
1261 r = build_stmt (input_location, RETURN_EXPR, expr);
1262 if (no_warning)
1263 suppress_warning (r, OPT_Wreturn_type);
1264 r = maybe_cleanup_point_expr_void (r);
1265 r = add_stmt (r);
1267 return r;
1270 /* Begin the scope of a for-statement or a range-for-statement.
1271 Both the returned trees are to be used in a call to
1272 begin_for_stmt or begin_range_for_stmt. */
1274 tree
1275 begin_for_scope (tree *init)
1277 tree scope = do_pushlevel (sk_for);
1279 if (processing_template_decl)
1280 *init = push_stmt_list ();
1281 else
1282 *init = NULL_TREE;
1284 return scope;
1287 /* Begin a for-statement. Returns a new FOR_STMT.
1288 SCOPE and INIT should be the return of begin_for_scope,
1289 or both NULL_TREE */
1291 tree
1292 begin_for_stmt (tree scope, tree init)
1294 tree r;
1296 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
1297 NULL_TREE, NULL_TREE, NULL_TREE);
1299 if (scope == NULL_TREE)
1301 gcc_assert (!init);
1302 scope = begin_for_scope (&init);
1305 FOR_INIT_STMT (r) = init;
1306 FOR_SCOPE (r) = scope;
1308 return r;
1311 /* Finish the init-statement of a for-statement, which may be
1312 given by FOR_STMT. */
1314 void
1315 finish_init_stmt (tree for_stmt)
1317 if (processing_template_decl)
1318 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
1319 add_stmt (for_stmt);
1320 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
1321 begin_cond (&FOR_COND (for_stmt));
1324 /* Finish the COND of a for-statement, which may be given by
1325 FOR_STMT. */
1327 void
1328 finish_for_cond (tree cond, tree for_stmt, bool ivdep, unsigned short unroll)
1330 cond = maybe_convert_cond (cond);
1331 finish_cond (&FOR_COND (for_stmt), cond);
1332 begin_maybe_infinite_loop (cond);
1333 if (ivdep && cond != error_mark_node)
1334 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1335 TREE_TYPE (FOR_COND (for_stmt)),
1336 FOR_COND (for_stmt),
1337 build_int_cst (integer_type_node,
1338 annot_expr_ivdep_kind),
1339 integer_zero_node);
1340 if (unroll && cond != error_mark_node)
1341 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1342 TREE_TYPE (FOR_COND (for_stmt)),
1343 FOR_COND (for_stmt),
1344 build_int_cst (integer_type_node,
1345 annot_expr_unroll_kind),
1346 build_int_cst (integer_type_node,
1347 unroll));
1348 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
1351 /* Finish the increment-EXPRESSION in a for-statement, which may be
1352 given by FOR_STMT. */
1354 void
1355 finish_for_expr (tree expr, tree for_stmt)
1357 if (!expr)
1358 return;
1359 /* If EXPR is an overloaded function, issue an error; there is no
1360 context available to use to perform overload resolution. */
1361 if (type_unknown_p (expr))
1363 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1364 expr = error_mark_node;
1366 if (!processing_template_decl)
1368 if (warn_sequence_point)
1369 verify_sequence_points (expr);
1370 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1371 tf_warning_or_error);
1373 else if (!type_dependent_expression_p (expr))
1374 convert_to_void (build_non_dependent_expr (expr), ICV_THIRD_IN_FOR,
1375 tf_warning_or_error);
1376 expr = maybe_cleanup_point_expr_void (expr);
1377 if (check_for_bare_parameter_packs (expr))
1378 expr = error_mark_node;
1379 FOR_EXPR (for_stmt) = expr;
1382 /* Finish the body of a for-statement, which may be given by
1383 FOR_STMT. The increment-EXPR for the loop must be
1384 provided.
1385 It can also finish RANGE_FOR_STMT. */
1387 void
1388 finish_for_stmt (tree for_stmt)
1390 end_maybe_infinite_loop (boolean_true_node);
1392 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1393 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1394 else
1395 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1397 /* Pop the scope for the body of the loop. */
1398 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1399 ? &RANGE_FOR_SCOPE (for_stmt)
1400 : &FOR_SCOPE (for_stmt));
1401 tree scope = *scope_ptr;
1402 *scope_ptr = NULL;
1404 /* During parsing of the body, range for uses "__for_{range,begin,end} "
1405 decl names to make those unaccessible by code in the body.
1406 Change it to ones with underscore instead of space, so that it can
1407 be inspected in the debugger. */
1408 tree range_for_decl[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
1409 gcc_assert (CPTI_FOR_BEGIN__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 1
1410 && CPTI_FOR_END__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 2
1411 && CPTI_FOR_RANGE_IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 3
1412 && CPTI_FOR_BEGIN_IDENTIFIER == CPTI_FOR_BEGIN__IDENTIFIER + 3
1413 && CPTI_FOR_END_IDENTIFIER == CPTI_FOR_END__IDENTIFIER + 3);
1414 for (int i = 0; i < 3; i++)
1416 tree id = cp_global_trees[CPTI_FOR_RANGE__IDENTIFIER + i];
1417 if (IDENTIFIER_BINDING (id)
1418 && IDENTIFIER_BINDING (id)->scope == current_binding_level)
1420 range_for_decl[i] = IDENTIFIER_BINDING (id)->value;
1421 gcc_assert (VAR_P (range_for_decl[i])
1422 && DECL_ARTIFICIAL (range_for_decl[i]));
1426 add_stmt (do_poplevel (scope));
1428 /* If we're being called from build_vec_init, don't mess with the names of
1429 the variables for an enclosing range-for. */
1430 if (!stmts_are_full_exprs_p ())
1431 return;
1433 for (int i = 0; i < 3; i++)
1434 if (range_for_decl[i])
1435 DECL_NAME (range_for_decl[i])
1436 = cp_global_trees[CPTI_FOR_RANGE_IDENTIFIER + i];
1439 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1440 SCOPE and INIT should be the return of begin_for_scope,
1441 or both NULL_TREE .
1442 To finish it call finish_for_stmt(). */
1444 tree
1445 begin_range_for_stmt (tree scope, tree init)
1447 begin_maybe_infinite_loop (boolean_false_node);
1449 tree r = build_stmt (input_location, RANGE_FOR_STMT, NULL_TREE, NULL_TREE,
1450 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1452 if (scope == NULL_TREE)
1454 gcc_assert (!init);
1455 scope = begin_for_scope (&init);
1458 /* Since C++20, RANGE_FOR_STMTs can use the init tree, so save it. */
1459 RANGE_FOR_INIT_STMT (r) = init;
1460 RANGE_FOR_SCOPE (r) = scope;
1462 return r;
1465 /* Finish the head of a range-based for statement, which may
1466 be given by RANGE_FOR_STMT. DECL must be the declaration
1467 and EXPR must be the loop expression. */
1469 void
1470 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1472 if (processing_template_decl)
1473 RANGE_FOR_INIT_STMT (range_for_stmt)
1474 = pop_stmt_list (RANGE_FOR_INIT_STMT (range_for_stmt));
1475 RANGE_FOR_DECL (range_for_stmt) = decl;
1476 RANGE_FOR_EXPR (range_for_stmt) = expr;
1477 add_stmt (range_for_stmt);
1478 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1481 /* Finish a break-statement. */
1483 tree
1484 finish_break_stmt (void)
1486 /* In switch statements break is sometimes stylistically used after
1487 a return statement. This can lead to spurious warnings about
1488 control reaching the end of a non-void function when it is
1489 inlined. Note that we are calling block_may_fallthru with
1490 language specific tree nodes; this works because
1491 block_may_fallthru returns true when given something it does not
1492 understand. */
1493 if (!block_may_fallthru (cur_stmt_list))
1494 return void_node;
1495 note_break_stmt ();
1496 return add_stmt (build_stmt (input_location, BREAK_STMT));
1499 /* Finish a continue-statement. */
1501 tree
1502 finish_continue_stmt (void)
1504 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1507 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1508 appropriate. */
1510 tree
1511 begin_switch_stmt (void)
1513 tree r, scope;
1515 scope = do_pushlevel (sk_cond);
1516 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1518 begin_cond (&SWITCH_STMT_COND (r));
1520 return r;
1523 /* Finish the cond of a switch-statement. */
1525 void
1526 finish_switch_cond (tree cond, tree switch_stmt)
1528 tree orig_type = NULL;
1530 if (!processing_template_decl)
1532 /* Convert the condition to an integer or enumeration type. */
1533 tree orig_cond = cond;
1534 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1535 if (cond == NULL_TREE)
1537 error_at (cp_expr_loc_or_input_loc (orig_cond),
1538 "switch quantity not an integer");
1539 cond = error_mark_node;
1541 /* We want unlowered type here to handle enum bit-fields. */
1542 orig_type = unlowered_expr_type (cond);
1543 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1544 orig_type = TREE_TYPE (cond);
1545 if (cond != error_mark_node)
1547 /* [stmt.switch]
1549 Integral promotions are performed. */
1550 cond = perform_integral_promotions (cond);
1551 cond = maybe_cleanup_point_expr (cond);
1554 if (check_for_bare_parameter_packs (cond))
1555 cond = error_mark_node;
1556 else if (!processing_template_decl && warn_sequence_point)
1557 verify_sequence_points (cond);
1559 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1560 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1561 add_stmt (switch_stmt);
1562 push_switch (switch_stmt);
1563 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1566 /* Finish the body of a switch-statement, which may be given by
1567 SWITCH_STMT. The COND to switch on is indicated. */
1569 void
1570 finish_switch_stmt (tree switch_stmt)
1572 tree scope;
1574 SWITCH_STMT_BODY (switch_stmt) =
1575 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1576 pop_switch ();
1578 scope = SWITCH_STMT_SCOPE (switch_stmt);
1579 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1580 add_stmt (do_poplevel (scope));
1583 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1584 appropriate. */
1586 tree
1587 begin_try_block (void)
1589 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1590 add_stmt (r);
1591 TRY_STMTS (r) = push_stmt_list ();
1592 return r;
1595 /* Likewise, for a function-try-block. The block returned in
1596 *COMPOUND_STMT is an artificial outer scope, containing the
1597 function-try-block. */
1599 tree
1600 begin_function_try_block (tree *compound_stmt)
1602 tree r;
1603 /* This outer scope does not exist in the C++ standard, but we need
1604 a place to put __FUNCTION__ and similar variables. */
1605 *compound_stmt = begin_compound_stmt (0);
1606 r = begin_try_block ();
1607 FN_TRY_BLOCK_P (r) = 1;
1608 return r;
1611 /* Finish a try-block, which may be given by TRY_BLOCK. */
1613 void
1614 finish_try_block (tree try_block)
1616 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1617 TRY_HANDLERS (try_block) = push_stmt_list ();
1620 /* Finish the body of a cleanup try-block, which may be given by
1621 TRY_BLOCK. */
1623 void
1624 finish_cleanup_try_block (tree try_block)
1626 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1629 /* Finish an implicitly generated try-block, with a cleanup is given
1630 by CLEANUP. */
1632 void
1633 finish_cleanup (tree cleanup, tree try_block)
1635 TRY_HANDLERS (try_block) = cleanup;
1636 CLEANUP_P (try_block) = 1;
1639 /* Likewise, for a function-try-block. */
1641 void
1642 finish_function_try_block (tree try_block)
1644 finish_try_block (try_block);
1645 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1646 the try block, but moving it inside. */
1647 in_function_try_handler = 1;
1650 /* Finish a handler-sequence for a try-block, which may be given by
1651 TRY_BLOCK. */
1653 void
1654 finish_handler_sequence (tree try_block)
1656 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1657 check_handlers (TRY_HANDLERS (try_block));
1660 /* Finish the handler-seq for a function-try-block, given by
1661 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1662 begin_function_try_block. */
1664 void
1665 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1667 in_function_try_handler = 0;
1668 finish_handler_sequence (try_block);
1669 finish_compound_stmt (compound_stmt);
1672 /* Begin a handler. Returns a HANDLER if appropriate. */
1674 tree
1675 begin_handler (void)
1677 tree r;
1679 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1680 add_stmt (r);
1682 /* Create a binding level for the eh_info and the exception object
1683 cleanup. */
1684 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1686 return r;
1689 /* Finish the handler-parameters for a handler, which may be given by
1690 HANDLER. DECL is the declaration for the catch parameter, or NULL
1691 if this is a `catch (...)' clause. */
1693 void
1694 finish_handler_parms (tree decl, tree handler)
1696 tree type = NULL_TREE;
1697 if (processing_template_decl)
1699 if (decl)
1701 decl = pushdecl (decl);
1702 decl = push_template_decl (decl);
1703 HANDLER_PARMS (handler) = decl;
1704 type = TREE_TYPE (decl);
1707 else
1709 type = expand_start_catch_block (decl);
1710 if (warn_catch_value
1711 && type != NULL_TREE
1712 && type != error_mark_node
1713 && !TYPE_REF_P (TREE_TYPE (decl)))
1715 tree orig_type = TREE_TYPE (decl);
1716 if (CLASS_TYPE_P (orig_type))
1718 if (TYPE_POLYMORPHIC_P (orig_type))
1719 warning_at (DECL_SOURCE_LOCATION (decl),
1720 OPT_Wcatch_value_,
1721 "catching polymorphic type %q#T by value",
1722 orig_type);
1723 else if (warn_catch_value > 1)
1724 warning_at (DECL_SOURCE_LOCATION (decl),
1725 OPT_Wcatch_value_,
1726 "catching type %q#T by value", orig_type);
1728 else if (warn_catch_value > 2)
1729 warning_at (DECL_SOURCE_LOCATION (decl),
1730 OPT_Wcatch_value_,
1731 "catching non-reference type %q#T", orig_type);
1734 HANDLER_TYPE (handler) = type;
1737 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1738 the return value from the matching call to finish_handler_parms. */
1740 void
1741 finish_handler (tree handler)
1743 if (!processing_template_decl)
1744 expand_end_catch_block ();
1745 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1748 /* Begin a compound statement. FLAGS contains some bits that control the
1749 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1750 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1751 block of a function. If BCS_TRY_BLOCK is set, this is the block
1752 created on behalf of a TRY statement. Returns a token to be passed to
1753 finish_compound_stmt. */
1755 tree
1756 begin_compound_stmt (unsigned int flags)
1758 tree r;
1760 if (flags & BCS_NO_SCOPE)
1762 r = push_stmt_list ();
1763 STATEMENT_LIST_NO_SCOPE (r) = 1;
1765 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1766 But, if it's a statement-expression with a scopeless block, there's
1767 nothing to keep, and we don't want to accidentally keep a block
1768 *inside* the scopeless block. */
1769 keep_next_level (false);
1771 else
1773 scope_kind sk = sk_block;
1774 if (flags & BCS_TRY_BLOCK)
1775 sk = sk_try;
1776 else if (flags & BCS_TRANSACTION)
1777 sk = sk_transaction;
1778 else if (flags & BCS_STMT_EXPR)
1779 sk = sk_stmt_expr;
1780 r = do_pushlevel (sk);
1783 /* When processing a template, we need to remember where the braces were,
1784 so that we can set up identical scopes when instantiating the template
1785 later. BIND_EXPR is a handy candidate for this.
1786 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1787 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1788 processing templates. */
1789 if (processing_template_decl)
1791 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1792 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1793 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1794 TREE_SIDE_EFFECTS (r) = 1;
1797 return r;
1800 /* Finish a compound-statement, which is given by STMT. */
1802 void
1803 finish_compound_stmt (tree stmt)
1805 if (TREE_CODE (stmt) == BIND_EXPR)
1807 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1808 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1809 discard the BIND_EXPR so it can be merged with the containing
1810 STATEMENT_LIST. */
1811 if (TREE_CODE (body) == STATEMENT_LIST
1812 && STATEMENT_LIST_HEAD (body) == NULL
1813 && !BIND_EXPR_BODY_BLOCK (stmt)
1814 && !BIND_EXPR_TRY_BLOCK (stmt))
1815 stmt = body;
1816 else
1817 BIND_EXPR_BODY (stmt) = body;
1819 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1820 stmt = pop_stmt_list (stmt);
1821 else
1823 /* Destroy any ObjC "super" receivers that may have been
1824 created. */
1825 objc_clear_super_receiver ();
1827 stmt = do_poplevel (stmt);
1830 /* ??? See c_end_compound_stmt wrt statement expressions. */
1831 add_stmt (stmt);
1834 /* Finish an asm-statement, whose components are a STRING, some
1835 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1836 LABELS. Also note whether the asm-statement should be
1837 considered volatile, and whether it is asm inline. */
1839 tree
1840 finish_asm_stmt (location_t loc, int volatile_p, tree string,
1841 tree output_operands, tree input_operands, tree clobbers,
1842 tree labels, bool inline_p)
1844 tree r;
1845 tree t;
1846 int ninputs = list_length (input_operands);
1847 int noutputs = list_length (output_operands);
1849 if (!processing_template_decl)
1851 const char *constraint;
1852 const char **oconstraints;
1853 bool allows_mem, allows_reg, is_inout;
1854 tree operand;
1855 int i;
1857 oconstraints = XALLOCAVEC (const char *, noutputs);
1859 string = resolve_asm_operand_names (string, output_operands,
1860 input_operands, labels);
1862 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1864 operand = TREE_VALUE (t);
1866 /* ??? Really, this should not be here. Users should be using a
1867 proper lvalue, dammit. But there's a long history of using
1868 casts in the output operands. In cases like longlong.h, this
1869 becomes a primitive form of typechecking -- if the cast can be
1870 removed, then the output operand had a type of the proper width;
1871 otherwise we'll get an error. Gross, but ... */
1872 STRIP_NOPS (operand);
1874 operand = mark_lvalue_use (operand);
1876 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1877 operand = error_mark_node;
1879 if (operand != error_mark_node
1880 && (TREE_READONLY (operand)
1881 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1882 /* Functions are not modifiable, even though they are
1883 lvalues. */
1884 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (operand))
1885 /* If it's an aggregate and any field is const, then it is
1886 effectively const. */
1887 || (CLASS_TYPE_P (TREE_TYPE (operand))
1888 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1889 cxx_readonly_error (loc, operand, lv_asm);
1891 tree *op = &operand;
1892 while (TREE_CODE (*op) == COMPOUND_EXPR)
1893 op = &TREE_OPERAND (*op, 1);
1894 switch (TREE_CODE (*op))
1896 case PREINCREMENT_EXPR:
1897 case PREDECREMENT_EXPR:
1898 case MODIFY_EXPR:
1899 *op = genericize_compound_lvalue (*op);
1900 op = &TREE_OPERAND (*op, 1);
1901 break;
1902 default:
1903 break;
1906 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1907 oconstraints[i] = constraint;
1909 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
1910 &allows_mem, &allows_reg, &is_inout))
1912 /* If the operand is going to end up in memory,
1913 mark it addressable. */
1914 if (!allows_reg && !cxx_mark_addressable (*op))
1915 operand = error_mark_node;
1917 else
1918 operand = error_mark_node;
1920 TREE_VALUE (t) = operand;
1923 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
1925 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
1926 bool constraint_parsed
1927 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
1928 oconstraints, &allows_mem, &allows_reg);
1929 /* If the operand is going to end up in memory, don't call
1930 decay_conversion. */
1931 if (constraint_parsed && !allows_reg && allows_mem)
1932 operand = mark_lvalue_use (TREE_VALUE (t));
1933 else
1934 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
1936 /* If the type of the operand hasn't been determined (e.g.,
1937 because it involves an overloaded function), then issue
1938 an error message. There's no context available to
1939 resolve the overloading. */
1940 if (TREE_TYPE (operand) == unknown_type_node)
1942 error_at (loc,
1943 "type of %<asm%> operand %qE could not be determined",
1944 TREE_VALUE (t));
1945 operand = error_mark_node;
1948 if (constraint_parsed)
1950 /* If the operand is going to end up in memory,
1951 mark it addressable. */
1952 if (!allows_reg && allows_mem)
1954 /* Strip the nops as we allow this case. FIXME, this really
1955 should be rejected or made deprecated. */
1956 STRIP_NOPS (operand);
1958 tree *op = &operand;
1959 while (TREE_CODE (*op) == COMPOUND_EXPR)
1960 op = &TREE_OPERAND (*op, 1);
1961 switch (TREE_CODE (*op))
1963 case PREINCREMENT_EXPR:
1964 case PREDECREMENT_EXPR:
1965 case MODIFY_EXPR:
1966 *op = genericize_compound_lvalue (*op);
1967 op = &TREE_OPERAND (*op, 1);
1968 break;
1969 default:
1970 break;
1973 if (!cxx_mark_addressable (*op))
1974 operand = error_mark_node;
1976 else if (!allows_reg && !allows_mem)
1978 /* If constraint allows neither register nor memory,
1979 try harder to get a constant. */
1980 tree constop = maybe_constant_value (operand);
1981 if (TREE_CONSTANT (constop))
1982 operand = constop;
1985 else
1986 operand = error_mark_node;
1988 TREE_VALUE (t) = operand;
1992 r = build_stmt (loc, ASM_EXPR, string,
1993 output_operands, input_operands,
1994 clobbers, labels);
1995 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
1996 ASM_INLINE_P (r) = inline_p;
1997 r = maybe_cleanup_point_expr_void (r);
1998 return add_stmt (r);
2001 /* Finish a label with the indicated NAME. Returns the new label. */
2003 tree
2004 finish_label_stmt (tree name)
2006 tree decl = define_label (input_location, name);
2008 if (decl == error_mark_node)
2009 return error_mark_node;
2011 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
2013 return decl;
2016 /* Finish a series of declarations for local labels. G++ allows users
2017 to declare "local" labels, i.e., labels with scope. This extension
2018 is useful when writing code involving statement-expressions. */
2020 void
2021 finish_label_decl (tree name)
2023 if (!at_function_scope_p ())
2025 error ("%<__label__%> declarations are only allowed in function scopes");
2026 return;
2029 add_decl_expr (declare_local_label (name));
2032 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
2034 void
2035 finish_decl_cleanup (tree decl, tree cleanup)
2037 push_cleanup (decl, cleanup, false);
2040 /* If the current scope exits with an exception, run CLEANUP. */
2042 void
2043 finish_eh_cleanup (tree cleanup)
2045 push_cleanup (NULL, cleanup, true);
2048 /* The MEM_INITS is a list of mem-initializers, in reverse of the
2049 order they were written by the user. Each node is as for
2050 emit_mem_initializers. */
2052 void
2053 finish_mem_initializers (tree mem_inits)
2055 /* Reorder the MEM_INITS so that they are in the order they appeared
2056 in the source program. */
2057 mem_inits = nreverse (mem_inits);
2059 if (processing_template_decl)
2061 tree mem;
2063 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
2065 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
2066 check for bare parameter packs in the TREE_VALUE, because
2067 any parameter packs in the TREE_VALUE have already been
2068 bound as part of the TREE_PURPOSE. See
2069 make_pack_expansion for more information. */
2070 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
2071 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
2072 TREE_VALUE (mem) = error_mark_node;
2075 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
2076 CTOR_INITIALIZER, mem_inits));
2078 else
2079 emit_mem_initializers (mem_inits);
2082 /* Obfuscate EXPR if it looks like an id-expression or member access so
2083 that the call to finish_decltype in do_auto_deduction will give the
2084 right result. If EVEN_UNEVAL, do this even in unevaluated context. */
2086 tree
2087 force_paren_expr (tree expr, bool even_uneval /* = false */)
2089 /* This is only needed for decltype(auto) in C++14. */
2090 if (cxx_dialect < cxx14)
2091 return expr;
2093 /* If we're in unevaluated context, we can't be deducing a
2094 return/initializer type, so we don't need to mess with this. */
2095 if (cp_unevaluated_operand && !even_uneval)
2096 return expr;
2098 if (TREE_CODE (expr) == COMPONENT_REF
2099 || TREE_CODE (expr) == SCOPE_REF
2100 || REFERENCE_REF_P (expr))
2101 REF_PARENTHESIZED_P (expr) = true;
2102 else if (DECL_P (tree_strip_any_location_wrapper (expr)))
2104 location_t loc = cp_expr_location (expr);
2105 const tree_code code = processing_template_decl ? PAREN_EXPR
2106 : VIEW_CONVERT_EXPR;
2107 expr = build1_loc (loc, code, TREE_TYPE (expr), expr);
2108 REF_PARENTHESIZED_P (expr) = true;
2110 return expr;
2113 /* If T is an id-expression obfuscated by force_paren_expr, undo the
2114 obfuscation and return the underlying id-expression. Otherwise
2115 return T. */
2117 tree
2118 maybe_undo_parenthesized_ref (tree t)
2120 if (cxx_dialect < cxx14)
2121 return t;
2123 if ((TREE_CODE (t) == PAREN_EXPR || TREE_CODE (t) == VIEW_CONVERT_EXPR)
2124 && REF_PARENTHESIZED_P (t))
2125 t = TREE_OPERAND (t, 0);
2127 return t;
2130 /* Finish a parenthesized expression EXPR. */
2132 cp_expr
2133 finish_parenthesized_expr (cp_expr expr)
2135 if (EXPR_P (expr))
2136 /* This inhibits warnings in c_common_truthvalue_conversion. */
2137 suppress_warning (expr, OPT_Wparentheses);
2139 if (TREE_CODE (expr) == OFFSET_REF
2140 || TREE_CODE (expr) == SCOPE_REF)
2141 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
2142 enclosed in parentheses. */
2143 PTRMEM_OK_P (expr) = 0;
2145 tree stripped_expr = tree_strip_any_location_wrapper (expr);
2146 if (TREE_CODE (stripped_expr) == STRING_CST)
2147 PAREN_STRING_LITERAL_P (stripped_expr) = 1;
2149 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
2151 return expr;
2154 /* Finish a reference to a non-static data member (DECL) that is not
2155 preceded by `.' or `->'. */
2157 tree
2158 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope,
2159 tsubst_flags_t complain /* = tf_warning_or_error */)
2161 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
2162 bool try_omp_private = !object && omp_private_member_map;
2163 tree ret;
2165 if (!object)
2167 tree scope = qualifying_scope;
2168 if (scope == NULL_TREE)
2170 scope = context_for_name_lookup (decl);
2171 if (!TYPE_P (scope))
2173 /* Can happen during error recovery (c++/85014). */
2174 gcc_assert (seen_error ());
2175 return error_mark_node;
2178 object = maybe_dummy_object (scope, NULL);
2181 object = maybe_resolve_dummy (object, true);
2182 if (object == error_mark_node)
2183 return error_mark_node;
2185 /* DR 613/850: Can use non-static data members without an associated
2186 object in sizeof/decltype/alignof. */
2187 if (is_dummy_object (object)
2188 && !cp_unevaluated_operand
2189 && (!processing_template_decl || !current_class_ref))
2191 if (complain & tf_error)
2193 if (current_function_decl
2194 && DECL_STATIC_FUNCTION_P (current_function_decl))
2195 error ("invalid use of member %qD in static member function", decl);
2196 else if (current_function_decl
2197 && processing_contract_condition
2198 && DECL_CONSTRUCTOR_P (current_function_decl))
2199 error ("invalid use of member %qD in constructor %<pre%> contract", decl);
2200 else if (current_function_decl
2201 && processing_contract_condition
2202 && DECL_DESTRUCTOR_P (current_function_decl))
2203 error ("invalid use of member %qD in destructor %<post%> contract", decl);
2204 else
2205 error ("invalid use of non-static data member %qD", decl);
2206 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2209 return error_mark_node;
2212 if (current_class_ptr)
2213 TREE_USED (current_class_ptr) = 1;
2214 if (processing_template_decl)
2216 tree type = TREE_TYPE (decl);
2218 if (TYPE_REF_P (type))
2219 /* Quals on the object don't matter. */;
2220 else if (PACK_EXPANSION_P (type))
2221 /* Don't bother trying to represent this. */
2222 type = NULL_TREE;
2223 else
2225 /* Set the cv qualifiers. */
2226 int quals = cp_type_quals (TREE_TYPE (object));
2228 if (DECL_MUTABLE_P (decl))
2229 quals &= ~TYPE_QUAL_CONST;
2231 quals |= cp_type_quals (TREE_TYPE (decl));
2232 type = cp_build_qualified_type (type, quals);
2235 if (qualifying_scope)
2236 /* Wrap this in a SCOPE_REF for now. */
2237 ret = build_qualified_name (type, qualifying_scope, decl,
2238 /*template_p=*/false);
2239 else
2240 ret = (convert_from_reference
2241 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
2243 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
2244 QUALIFYING_SCOPE is also non-null. */
2245 else
2247 tree access_type = TREE_TYPE (object);
2249 if (!perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
2250 decl, complain))
2251 return error_mark_node;
2253 /* If the data member was named `C::M', convert `*this' to `C'
2254 first. */
2255 if (qualifying_scope)
2257 tree binfo = NULL_TREE;
2258 object = build_scoped_ref (object, qualifying_scope,
2259 &binfo);
2262 ret = build_class_member_access_expr (object, decl,
2263 /*access_path=*/NULL_TREE,
2264 /*preserve_reference=*/false,
2265 complain);
2267 if (try_omp_private)
2269 tree *v = omp_private_member_map->get (decl);
2270 if (v)
2271 ret = convert_from_reference (*v);
2273 return ret;
2276 /* DECL was the declaration to which a qualified-id resolved. Issue
2277 an error message if it is not accessible. If OBJECT_TYPE is
2278 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
2279 type of `*x', or `x', respectively. If the DECL was named as
2280 `A::B' then NESTED_NAME_SPECIFIER is `A'. Return value is like
2281 perform_access_checks above. */
2283 bool
2284 check_accessibility_of_qualified_id (tree decl,
2285 tree object_type,
2286 tree nested_name_specifier,
2287 tsubst_flags_t complain)
2289 /* If we're not checking, return immediately. */
2290 if (deferred_access_no_check)
2291 return true;
2293 /* Determine the SCOPE of DECL. */
2294 tree scope = context_for_name_lookup (decl);
2295 /* If the SCOPE is not a type, then DECL is not a member. */
2296 if (!TYPE_P (scope)
2297 /* If SCOPE is dependent then we can't perform this access check now,
2298 and since we'll perform this access check again after substitution
2299 there's no need to explicitly defer it. */
2300 || dependent_type_p (scope))
2301 return true;
2303 tree qualifying_type = NULL_TREE;
2304 /* Compute the scope through which DECL is being accessed. */
2305 if (object_type
2306 /* OBJECT_TYPE might not be a class type; consider:
2308 class A { typedef int I; };
2309 I *p;
2310 p->A::I::~I();
2312 In this case, we will have "A::I" as the DECL, but "I" as the
2313 OBJECT_TYPE. */
2314 && CLASS_TYPE_P (object_type)
2315 && DERIVED_FROM_P (scope, object_type))
2316 /* If we are processing a `->' or `.' expression, use the type of the
2317 left-hand side. */
2318 qualifying_type = object_type;
2319 else if (nested_name_specifier)
2321 /* If the reference is to a non-static member of the
2322 current class, treat it as if it were referenced through
2323 `this'. */
2324 if (DECL_NONSTATIC_MEMBER_P (decl)
2325 && current_class_ptr)
2326 if (tree current = current_nonlambda_class_type ())
2328 if (dependent_type_p (current))
2329 /* In general we can't know whether this access goes through
2330 `this' until instantiation time. Punt now, or else we might
2331 create a deferred access check that's not relative to `this'
2332 when it ought to be. We'll check this access again after
2333 substitution, e.g. from tsubst_qualified_id. */
2334 return true;
2336 if (DERIVED_FROM_P (scope, current))
2337 qualifying_type = current;
2339 /* Otherwise, use the type indicated by the
2340 nested-name-specifier. */
2341 if (!qualifying_type)
2342 qualifying_type = nested_name_specifier;
2344 else
2345 /* Otherwise, the name must be from the current class or one of
2346 its bases. */
2347 qualifying_type = currently_open_derived_class (scope);
2349 if (qualifying_type
2350 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
2351 or similar in a default argument value. */
2352 && CLASS_TYPE_P (qualifying_type))
2353 return perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
2354 decl, complain);
2356 return true;
2359 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
2360 class named to the left of the "::" operator. DONE is true if this
2361 expression is a complete postfix-expression; it is false if this
2362 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
2363 iff this expression is the operand of '&'. TEMPLATE_P is true iff
2364 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
2365 is true iff this qualified name appears as a template argument. */
2367 tree
2368 finish_qualified_id_expr (tree qualifying_class,
2369 tree expr,
2370 bool done,
2371 bool address_p,
2372 bool template_p,
2373 bool template_arg_p,
2374 tsubst_flags_t complain)
2376 gcc_assert (TYPE_P (qualifying_class));
2378 if (error_operand_p (expr))
2379 return error_mark_node;
2381 if (DECL_P (expr)
2382 /* Functions are marked after overload resolution; avoid redundant
2383 warnings. */
2384 && TREE_CODE (expr) != FUNCTION_DECL
2385 && !mark_used (expr, complain))
2386 return error_mark_node;
2388 if (template_p)
2390 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
2392 /* cp_parser_lookup_name thought we were looking for a type,
2393 but we're actually looking for a declaration. */
2394 qualifying_class = TYPE_CONTEXT (expr);
2395 expr = TYPE_IDENTIFIER (expr);
2397 else
2398 check_template_keyword (expr);
2401 /* If EXPR occurs as the operand of '&', use special handling that
2402 permits a pointer-to-member. */
2403 if (address_p && done
2404 && TREE_CODE (qualifying_class) != ENUMERAL_TYPE)
2406 if (TREE_CODE (expr) == SCOPE_REF)
2407 expr = TREE_OPERAND (expr, 1);
2408 expr = build_offset_ref (qualifying_class, expr,
2409 /*address_p=*/true, complain);
2410 return expr;
2413 /* No need to check access within an enum. */
2414 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE
2415 && TREE_CODE (expr) != IDENTIFIER_NODE)
2416 return expr;
2418 /* Within the scope of a class, turn references to non-static
2419 members into expression of the form "this->...". */
2420 if (template_arg_p)
2421 /* But, within a template argument, we do not want make the
2422 transformation, as there is no "this" pointer. */
2424 else if (TREE_CODE (expr) == FIELD_DECL)
2426 push_deferring_access_checks (dk_no_check);
2427 expr = finish_non_static_data_member (expr, NULL_TREE,
2428 qualifying_class, complain);
2429 pop_deferring_access_checks ();
2431 else if (BASELINK_P (expr))
2433 /* See if any of the functions are non-static members. */
2434 /* If so, the expression may be relative to 'this'. */
2435 if (!shared_member_p (expr)
2436 && current_class_ptr
2437 && DERIVED_FROM_P (qualifying_class,
2438 current_nonlambda_class_type ()))
2439 expr = (build_class_member_access_expr
2440 (maybe_dummy_object (qualifying_class, NULL),
2441 expr,
2442 BASELINK_ACCESS_BINFO (expr),
2443 /*preserve_reference=*/false,
2444 complain));
2445 else if (done)
2446 /* The expression is a qualified name whose address is not
2447 being taken. */
2448 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2449 complain);
2451 else if (!template_p
2452 && TREE_CODE (expr) == TEMPLATE_DECL
2453 && !DECL_FUNCTION_TEMPLATE_P (expr))
2455 if (complain & tf_error)
2456 error ("%qE missing template arguments", expr);
2457 return error_mark_node;
2459 else
2461 /* In a template, return a SCOPE_REF for most qualified-ids
2462 so that we can check access at instantiation time. But if
2463 we're looking at a member of the current instantiation, we
2464 know we have access and building up the SCOPE_REF confuses
2465 non-type template argument handling. */
2466 if (processing_template_decl
2467 && (!currently_open_class (qualifying_class)
2468 || TREE_CODE (expr) == IDENTIFIER_NODE
2469 || TREE_CODE (expr) == TEMPLATE_ID_EXPR
2470 || TREE_CODE (expr) == BIT_NOT_EXPR))
2471 expr = build_qualified_name (TREE_TYPE (expr),
2472 qualifying_class, expr,
2473 template_p);
2474 else if (tree wrap = maybe_get_tls_wrapper_call (expr))
2475 expr = wrap;
2477 expr = convert_from_reference (expr);
2480 return expr;
2483 /* Begin a statement-expression. The value returned must be passed to
2484 finish_stmt_expr. */
2486 tree
2487 begin_stmt_expr (void)
2489 return push_stmt_list ();
2492 /* Process the final expression of a statement expression. EXPR can be
2493 NULL, if the final expression is empty. Return a STATEMENT_LIST
2494 containing all the statements in the statement-expression, or
2495 ERROR_MARK_NODE if there was an error. */
2497 tree
2498 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2500 if (error_operand_p (expr))
2502 /* The type of the statement-expression is the type of the last
2503 expression. */
2504 TREE_TYPE (stmt_expr) = error_mark_node;
2505 return error_mark_node;
2508 /* If the last statement does not have "void" type, then the value
2509 of the last statement is the value of the entire expression. */
2510 if (expr)
2512 tree type = TREE_TYPE (expr);
2514 if (type && type_unknown_p (type))
2516 error ("a statement expression is an insufficient context"
2517 " for overload resolution");
2518 TREE_TYPE (stmt_expr) = error_mark_node;
2519 return error_mark_node;
2521 else if (processing_template_decl)
2523 expr = build_stmt (input_location, EXPR_STMT, expr);
2524 expr = add_stmt (expr);
2525 /* Mark the last statement so that we can recognize it as such at
2526 template-instantiation time. */
2527 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2529 else if (VOID_TYPE_P (type))
2531 /* Just treat this like an ordinary statement. */
2532 expr = finish_expr_stmt (expr);
2534 else
2536 /* It actually has a value we need to deal with. First, force it
2537 to be an rvalue so that we won't need to build up a copy
2538 constructor call later when we try to assign it to something. */
2539 expr = force_rvalue (expr, tf_warning_or_error);
2540 if (error_operand_p (expr))
2541 return error_mark_node;
2543 /* Update for array-to-pointer decay. */
2544 type = TREE_TYPE (expr);
2546 /* This TARGET_EXPR will initialize the outer one added by
2547 finish_stmt_expr. */
2548 set_target_expr_eliding (expr);
2550 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2551 normal statement, but don't convert to void or actually add
2552 the EXPR_STMT. */
2553 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2554 expr = maybe_cleanup_point_expr (expr);
2555 add_stmt (expr);
2558 /* The type of the statement-expression is the type of the last
2559 expression. */
2560 TREE_TYPE (stmt_expr) = type;
2563 return stmt_expr;
2566 /* Finish a statement-expression. EXPR should be the value returned
2567 by the previous begin_stmt_expr. Returns an expression
2568 representing the statement-expression. */
2570 tree
2571 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2573 tree type;
2574 tree result;
2576 if (error_operand_p (stmt_expr))
2578 pop_stmt_list (stmt_expr);
2579 return error_mark_node;
2582 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2584 type = TREE_TYPE (stmt_expr);
2585 result = pop_stmt_list (stmt_expr);
2586 TREE_TYPE (result) = type;
2588 if (processing_template_decl)
2590 result = build_min (STMT_EXPR, type, result);
2591 TREE_SIDE_EFFECTS (result) = 1;
2592 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2594 else if (CLASS_TYPE_P (type))
2596 /* Wrap the statement-expression in a TARGET_EXPR so that the
2597 temporary object created by the final expression is destroyed at
2598 the end of the full-expression containing the
2599 statement-expression. */
2600 result = force_target_expr (type, result, tf_warning_or_error);
2603 return result;
2606 /* Returns the expression which provides the value of STMT_EXPR. */
2608 tree
2609 stmt_expr_value_expr (tree stmt_expr)
2611 tree t = STMT_EXPR_STMT (stmt_expr);
2613 if (TREE_CODE (t) == BIND_EXPR)
2614 t = BIND_EXPR_BODY (t);
2616 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2617 t = STATEMENT_LIST_TAIL (t)->stmt;
2619 if (TREE_CODE (t) == EXPR_STMT)
2620 t = EXPR_STMT_EXPR (t);
2622 return t;
2625 /* Return TRUE iff EXPR_STMT is an empty list of
2626 expression statements. */
2628 bool
2629 empty_expr_stmt_p (tree expr_stmt)
2631 tree body = NULL_TREE;
2633 if (expr_stmt == void_node)
2634 return true;
2636 if (expr_stmt)
2638 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2639 body = EXPR_STMT_EXPR (expr_stmt);
2640 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2641 body = expr_stmt;
2644 if (body)
2646 if (TREE_CODE (body) == STATEMENT_LIST)
2647 return tsi_end_p (tsi_start (body));
2648 else
2649 return empty_expr_stmt_p (body);
2651 return false;
2654 /* Perform Koenig lookup. FN_EXPR is the postfix-expression representing
2655 the function (or functions) to call; ARGS are the arguments to the
2656 call. Returns the functions to be considered by overload resolution. */
2658 cp_expr
2659 perform_koenig_lookup (cp_expr fn_expr, vec<tree, va_gc> *args,
2660 tsubst_flags_t complain)
2662 tree identifier = NULL_TREE;
2663 tree functions = NULL_TREE;
2664 tree tmpl_args = NULL_TREE;
2665 bool template_id = false;
2666 location_t loc = fn_expr.get_location ();
2667 tree fn = fn_expr.get_value ();
2669 STRIP_ANY_LOCATION_WRAPPER (fn);
2671 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2673 /* Use a separate flag to handle null args. */
2674 template_id = true;
2675 tmpl_args = TREE_OPERAND (fn, 1);
2676 fn = TREE_OPERAND (fn, 0);
2679 /* Find the name of the overloaded function. */
2680 if (identifier_p (fn))
2681 identifier = fn;
2682 else
2684 functions = fn;
2685 identifier = OVL_NAME (functions);
2688 /* A call to a namespace-scope function using an unqualified name.
2690 Do Koenig lookup -- unless any of the arguments are
2691 type-dependent. */
2692 if (!any_type_dependent_arguments_p (args)
2693 && !any_dependent_template_arguments_p (tmpl_args))
2695 fn = lookup_arg_dependent (identifier, functions, args);
2696 if (!fn)
2698 /* The unqualified name could not be resolved. */
2699 if (complain & tf_error)
2700 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2701 else
2702 fn = identifier;
2706 if (fn && template_id && fn != error_mark_node)
2707 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2709 return cp_expr (fn, loc);
2712 /* Generate an expression for `FN (ARGS)'. This may change the
2713 contents of ARGS.
2715 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2716 as a virtual call, even if FN is virtual. (This flag is set when
2717 encountering an expression where the function name is explicitly
2718 qualified. For example a call to `X::f' never generates a virtual
2719 call.)
2721 Returns code for the call. */
2723 tree
2724 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2725 bool koenig_p, tsubst_flags_t complain)
2727 tree result;
2728 tree orig_fn;
2729 vec<tree, va_gc> *orig_args = *args;
2731 if (fn == error_mark_node)
2732 return error_mark_node;
2734 gcc_assert (!TYPE_P (fn));
2736 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2737 it so that we can tell this is a call to a known function. */
2738 fn = maybe_undo_parenthesized_ref (fn);
2740 STRIP_ANY_LOCATION_WRAPPER (fn);
2742 orig_fn = fn;
2744 if (processing_template_decl)
2746 /* If FN is a local extern declaration (or set thereof) in a template,
2747 look it up again at instantiation time. */
2748 if (is_overloaded_fn (fn))
2750 tree ifn = get_first_fn (fn);
2751 if (TREE_CODE (ifn) == FUNCTION_DECL
2752 && dependent_local_decl_p (ifn))
2753 orig_fn = DECL_NAME (ifn);
2756 /* If the call expression is dependent, build a CALL_EXPR node
2757 with no type; type_dependent_expression_p recognizes
2758 expressions with no type as being dependent. */
2759 if (type_dependent_expression_p (fn)
2760 || any_type_dependent_arguments_p (*args))
2762 result = build_min_nt_call_vec (orig_fn, *args);
2763 SET_EXPR_LOCATION (result, cp_expr_loc_or_input_loc (fn));
2764 KOENIG_LOOKUP_P (result) = koenig_p;
2765 /* Disable the std::move warnings since this call was dependent
2766 (c++/89780, c++/107363). This also suppresses the
2767 -Wredundant-move warning. */
2768 suppress_warning (result, OPT_Wpessimizing_move);
2769 if (is_overloaded_fn (fn))
2770 fn = get_fns (fn);
2772 if (cfun)
2774 bool abnormal = true;
2775 for (lkp_iterator iter (fn); abnormal && iter; ++iter)
2777 tree fndecl = STRIP_TEMPLATE (*iter);
2778 if (TREE_CODE (fndecl) != FUNCTION_DECL
2779 || !TREE_THIS_VOLATILE (fndecl))
2780 abnormal = false;
2782 /* FIXME: Stop warning about falling off end of non-void
2783 function. But this is wrong. Even if we only see
2784 no-return fns at this point, we could select a
2785 future-defined return fn during instantiation. Or
2786 vice-versa. */
2787 if (abnormal)
2788 current_function_returns_abnormally = 1;
2790 return result;
2792 orig_args = make_tree_vector_copy (*args);
2793 if (!BASELINK_P (fn)
2794 && TREE_CODE (fn) != PSEUDO_DTOR_EXPR
2795 && TREE_TYPE (fn) != unknown_type_node)
2796 fn = build_non_dependent_expr (fn);
2797 make_args_non_dependent (*args);
2800 if (TREE_CODE (fn) == COMPONENT_REF)
2802 tree member = TREE_OPERAND (fn, 1);
2803 if (BASELINK_P (member))
2805 tree object = TREE_OPERAND (fn, 0);
2806 return build_new_method_call (object, member,
2807 args, NULL_TREE,
2808 (disallow_virtual
2809 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2810 : LOOKUP_NORMAL),
2811 /*fn_p=*/NULL,
2812 complain);
2816 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2817 if (TREE_CODE (fn) == ADDR_EXPR
2818 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2819 fn = TREE_OPERAND (fn, 0);
2821 if (is_overloaded_fn (fn))
2822 fn = baselink_for_fns (fn);
2824 result = NULL_TREE;
2825 if (BASELINK_P (fn))
2827 tree object;
2829 /* A call to a member function. From [over.call.func]:
2831 If the keyword this is in scope and refers to the class of
2832 that member function, or a derived class thereof, then the
2833 function call is transformed into a qualified function call
2834 using (*this) as the postfix-expression to the left of the
2835 . operator.... [Otherwise] a contrived object of type T
2836 becomes the implied object argument.
2838 In this situation:
2840 struct A { void f(); };
2841 struct B : public A {};
2842 struct C : public A { void g() { B::f(); }};
2844 "the class of that member function" refers to `A'. But 11.2
2845 [class.access.base] says that we need to convert 'this' to B* as
2846 part of the access, so we pass 'B' to maybe_dummy_object. */
2848 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2850 /* A constructor call always uses a dummy object. (This constructor
2851 call which has the form A::A () is actually invalid and we are
2852 going to reject it later in build_new_method_call.) */
2853 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2855 else
2856 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2857 NULL);
2859 result = build_new_method_call (object, fn, args, NULL_TREE,
2860 (disallow_virtual
2861 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2862 : LOOKUP_NORMAL),
2863 /*fn_p=*/NULL,
2864 complain);
2866 else if (concept_check_p (fn))
2868 /* FN is actually a template-id referring to a concept definition. */
2869 tree id = unpack_concept_check (fn);
2870 tree tmpl = TREE_OPERAND (id, 0);
2871 tree args = TREE_OPERAND (id, 1);
2873 if (!function_concept_p (tmpl))
2875 error_at (EXPR_LOC_OR_LOC (fn, input_location),
2876 "cannot call a concept as a function");
2877 return error_mark_node;
2880 /* Ensure the result is wrapped as a call expression. */
2881 result = build_concept_check (tmpl, args, tf_warning_or_error);
2883 else if (is_overloaded_fn (fn))
2885 /* If the function is an overloaded builtin, resolve it. */
2886 if (TREE_CODE (fn) == FUNCTION_DECL
2887 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
2888 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
2889 result = resolve_overloaded_builtin (input_location, fn, *args);
2891 if (!result)
2893 if (warn_sizeof_pointer_memaccess
2894 && (complain & tf_warning)
2895 && !vec_safe_is_empty (*args)
2896 && !processing_template_decl)
2898 location_t sizeof_arg_loc[3];
2899 tree sizeof_arg[3];
2900 unsigned int i;
2901 for (i = 0; i < 3; i++)
2903 tree t;
2905 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
2906 sizeof_arg[i] = NULL_TREE;
2907 if (i >= (*args)->length ())
2908 continue;
2909 t = (**args)[i];
2910 if (TREE_CODE (t) != SIZEOF_EXPR)
2911 continue;
2912 if (SIZEOF_EXPR_TYPE_P (t))
2913 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
2914 else
2915 sizeof_arg[i] = TREE_OPERAND (t, 0);
2916 sizeof_arg_loc[i] = EXPR_LOCATION (t);
2918 sizeof_pointer_memaccess_warning
2919 (sizeof_arg_loc, fn, *args,
2920 sizeof_arg, same_type_ignoring_top_level_qualifiers_p);
2923 if ((complain & tf_warning)
2924 && TREE_CODE (fn) == FUNCTION_DECL
2925 && fndecl_built_in_p (fn, BUILT_IN_MEMSET)
2926 && vec_safe_length (*args) == 3
2927 && !any_type_dependent_arguments_p (*args))
2929 tree arg0 = (*orig_args)[0];
2930 tree arg1 = (*orig_args)[1];
2931 tree arg2 = (*orig_args)[2];
2932 int literal_mask = ((literal_integer_zerop (arg1) << 1)
2933 | (literal_integer_zerop (arg2) << 2));
2934 warn_for_memset (input_location, arg0, arg2, literal_mask);
2937 /* A call to a namespace-scope function. */
2938 result = build_new_function_call (fn, args, complain);
2941 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
2943 if (!vec_safe_is_empty (*args))
2944 error ("arguments to destructor are not allowed");
2945 /* C++20/DR: If the postfix-expression names a pseudo-destructor (in
2946 which case the postfix-expression is a possibly-parenthesized class
2947 member access), the function call destroys the object of scalar type
2948 denoted by the object expression of the class member access. */
2949 tree ob = TREE_OPERAND (fn, 0);
2950 if (obvalue_p (ob))
2951 result = build_trivial_dtor_call (ob, true);
2952 else
2953 /* No location to clobber. */
2954 result = convert_to_void (ob, ICV_STATEMENT, complain);
2956 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
2957 /* If the "function" is really an object of class type, it might
2958 have an overloaded `operator ()'. */
2959 result = build_op_call (fn, args, complain);
2961 if (!result)
2962 /* A call where the function is unknown. */
2963 result = cp_build_function_call_vec (fn, args, complain);
2965 if (processing_template_decl && result != error_mark_node)
2967 if (INDIRECT_REF_P (result))
2968 result = TREE_OPERAND (result, 0);
2970 /* Prune all but the selected function from the original overload
2971 set so that we can avoid some duplicate work at instantiation time. */
2972 if (TREE_CODE (result) == CALL_EXPR
2973 && really_overloaded_fn (orig_fn))
2975 tree sel_fn = CALL_EXPR_FN (result);
2976 if (TREE_CODE (sel_fn) == COMPONENT_REF)
2978 /* The non-dependent result of build_new_method_call. */
2979 sel_fn = TREE_OPERAND (sel_fn, 1);
2980 gcc_assert (BASELINK_P (sel_fn));
2982 else if (TREE_CODE (sel_fn) == ADDR_EXPR)
2983 /* Our original callee wasn't wrapped in an ADDR_EXPR,
2984 so strip this ADDR_EXPR added by build_over_call. */
2985 sel_fn = TREE_OPERAND (sel_fn, 0);
2986 orig_fn = sel_fn;
2989 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
2990 SET_EXPR_LOCATION (result, input_location);
2991 KOENIG_LOOKUP_P (result) = koenig_p;
2992 release_tree_vector (orig_args);
2993 result = convert_from_reference (result);
2996 return result;
2999 /* Finish a call to a postfix increment or decrement or EXPR. (Which
3000 is indicated by CODE, which should be POSTINCREMENT_EXPR or
3001 POSTDECREMENT_EXPR.) */
3003 cp_expr
3004 finish_increment_expr (cp_expr expr, enum tree_code code)
3006 /* input_location holds the location of the trailing operator token.
3007 Build a location of the form:
3008 expr++
3009 ~~~~^~
3010 with the caret at the operator token, ranging from the start
3011 of EXPR to the end of the operator token. */
3012 location_t combined_loc = make_location (input_location,
3013 expr.get_start (),
3014 get_finish (input_location));
3015 cp_expr result = build_x_unary_op (combined_loc, code, expr,
3016 NULL_TREE, tf_warning_or_error);
3017 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
3018 result.set_location (combined_loc);
3019 return result;
3022 /* Finish a use of `this'. Returns an expression for `this'. */
3024 tree
3025 finish_this_expr (void)
3027 tree result = NULL_TREE;
3029 if (current_class_ptr)
3031 tree type = TREE_TYPE (current_class_ref);
3033 /* In a lambda expression, 'this' refers to the captured 'this'. */
3034 if (LAMBDA_TYPE_P (type))
3035 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
3036 else
3037 result = current_class_ptr;
3040 if (result)
3041 /* The keyword 'this' is a prvalue expression. */
3042 return rvalue (result);
3044 tree fn = current_nonlambda_function ();
3045 if (fn && DECL_STATIC_FUNCTION_P (fn))
3046 error ("%<this%> is unavailable for static member functions");
3047 else if (fn && processing_contract_condition && DECL_CONSTRUCTOR_P (fn))
3048 error ("invalid use of %<this%> before it is valid");
3049 else if (fn && processing_contract_condition && DECL_DESTRUCTOR_P (fn))
3050 error ("invalid use of %<this%> after it is valid");
3051 else if (fn)
3052 error ("invalid use of %<this%> in non-member function");
3053 else
3054 error ("invalid use of %<this%> at top level");
3055 return error_mark_node;
3058 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
3059 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
3060 the TYPE for the type given. If SCOPE is non-NULL, the expression
3061 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
3063 tree
3064 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
3065 location_t loc)
3067 if (object == error_mark_node || destructor == error_mark_node)
3068 return error_mark_node;
3070 gcc_assert (TYPE_P (destructor));
3072 if (!processing_template_decl)
3074 if (scope == error_mark_node)
3076 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
3077 return error_mark_node;
3079 if (is_auto (destructor))
3080 destructor = TREE_TYPE (object);
3081 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
3083 error_at (loc,
3084 "qualified type %qT does not match destructor name ~%qT",
3085 scope, destructor);
3086 return error_mark_node;
3090 /* [expr.pseudo] says both:
3092 The type designated by the pseudo-destructor-name shall be
3093 the same as the object type.
3095 and:
3097 The cv-unqualified versions of the object type and of the
3098 type designated by the pseudo-destructor-name shall be the
3099 same type.
3101 We implement the more generous second sentence, since that is
3102 what most other compilers do. */
3103 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
3104 destructor))
3106 error_at (loc, "%qE is not of type %qT", object, destructor);
3107 return error_mark_node;
3111 tree type = (type_dependent_expression_p (object)
3112 ? NULL_TREE : void_type_node);
3114 return build3_loc (loc, PSEUDO_DTOR_EXPR, type, object,
3115 scope, destructor);
3118 /* Finish an expression of the form CODE EXPR. */
3120 cp_expr
3121 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
3122 tsubst_flags_t complain)
3124 /* Build a location of the form:
3125 ++expr
3126 ^~~~~~
3127 with the caret at the operator token, ranging from the start
3128 of the operator token to the end of EXPR. */
3129 location_t combined_loc = make_location (op_loc,
3130 op_loc, expr.get_finish ());
3131 cp_expr result = build_x_unary_op (combined_loc, code, expr,
3132 NULL_TREE, complain);
3133 /* TODO: build_x_unary_op doesn't always honor the location. */
3134 result.set_location (combined_loc);
3136 if (result == error_mark_node)
3137 return result;
3139 if (!(complain & tf_warning))
3140 return result;
3142 tree result_ovl = result;
3143 tree expr_ovl = expr;
3145 if (!processing_template_decl)
3146 expr_ovl = cp_fully_fold (expr_ovl);
3148 if (!CONSTANT_CLASS_P (expr_ovl)
3149 || TREE_OVERFLOW_P (expr_ovl))
3150 return result;
3152 if (!processing_template_decl)
3153 result_ovl = cp_fully_fold (result_ovl);
3155 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
3156 overflow_warning (combined_loc, result_ovl);
3158 return result;
3161 /* Return true if CONSTRUCTOR EXPR after pack expansion could have no
3162 elements. */
3164 static bool
3165 maybe_zero_constructor_nelts (tree expr)
3167 if (CONSTRUCTOR_NELTS (expr) == 0)
3168 return true;
3169 if (!processing_template_decl)
3170 return false;
3171 for (constructor_elt &elt : CONSTRUCTOR_ELTS (expr))
3172 if (!PACK_EXPANSION_P (elt.value))
3173 return false;
3174 return true;
3177 /* Finish a compound-literal expression or C++11 functional cast with aggregate
3178 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
3179 is being cast. */
3181 tree
3182 finish_compound_literal (tree type, tree compound_literal,
3183 tsubst_flags_t complain,
3184 fcl_t fcl_context)
3186 if (type == error_mark_node)
3187 return error_mark_node;
3189 if (TYPE_REF_P (type))
3191 compound_literal
3192 = finish_compound_literal (TREE_TYPE (type), compound_literal,
3193 complain, fcl_context);
3194 /* The prvalue is then used to direct-initialize the reference. */
3195 tree r = (perform_implicit_conversion_flags
3196 (type, compound_literal, complain, LOOKUP_NORMAL));
3197 return convert_from_reference (r);
3200 if (!TYPE_OBJ_P (type))
3202 /* DR2351 */
3203 if (VOID_TYPE_P (type) && CONSTRUCTOR_NELTS (compound_literal) == 0)
3205 if (!processing_template_decl)
3206 return void_node;
3207 TREE_TYPE (compound_literal) = type;
3208 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
3209 CONSTRUCTOR_IS_DEPENDENT (compound_literal) = 0;
3210 return compound_literal;
3212 else if (VOID_TYPE_P (type)
3213 && processing_template_decl
3214 && maybe_zero_constructor_nelts (compound_literal))
3215 /* If there are only packs in compound_literal, it could
3216 be void{} after pack expansion. */;
3217 else
3219 if (complain & tf_error)
3220 error ("compound literal of non-object type %qT", type);
3221 return error_mark_node;
3225 if (template_placeholder_p (type))
3227 type = do_auto_deduction (type, compound_literal, type, complain,
3228 adc_variable_type);
3229 if (type == error_mark_node)
3230 return error_mark_node;
3232 /* C++23 auto{x}. */
3233 else if (is_auto (type)
3234 && !AUTO_IS_DECLTYPE (type)
3235 && CONSTRUCTOR_NELTS (compound_literal) == 1)
3237 if (is_constrained_auto (type))
3239 if (complain & tf_error)
3240 error ("%<auto{x}%> cannot be constrained");
3241 return error_mark_node;
3243 else if (cxx_dialect < cxx23)
3244 pedwarn (input_location, OPT_Wc__23_extensions,
3245 "%<auto{x}%> only available with "
3246 "%<-std=c++2b%> or %<-std=gnu++2b%>");
3247 type = do_auto_deduction (type, compound_literal, type, complain,
3248 adc_variable_type);
3249 if (type == error_mark_node)
3250 return error_mark_node;
3253 /* Used to hold a copy of the compound literal in a template. */
3254 tree orig_cl = NULL_TREE;
3256 if (processing_template_decl)
3258 const bool dependent_p
3259 = (instantiation_dependent_expression_p (compound_literal)
3260 || dependent_type_p (type));
3261 if (dependent_p)
3262 /* We're about to return, no need to copy. */
3263 orig_cl = compound_literal;
3264 else
3265 /* We're going to need a copy. */
3266 orig_cl = unshare_constructor (compound_literal);
3267 TREE_TYPE (orig_cl) = type;
3268 /* Mark the expression as a compound literal. */
3269 TREE_HAS_CONSTRUCTOR (orig_cl) = 1;
3270 /* And as instantiation-dependent. */
3271 CONSTRUCTOR_IS_DEPENDENT (orig_cl) = dependent_p;
3272 if (fcl_context == fcl_c99)
3273 CONSTRUCTOR_C99_COMPOUND_LITERAL (orig_cl) = 1;
3274 /* If the compound literal is dependent, we're done for now. */
3275 if (dependent_p)
3276 return orig_cl;
3277 /* Otherwise, do go on to e.g. check narrowing. */
3280 type = complete_type (type);
3282 if (TYPE_NON_AGGREGATE_CLASS (type))
3284 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
3285 everywhere that deals with function arguments would be a pain, so
3286 just wrap it in a TREE_LIST. The parser set a flag so we know
3287 that it came from T{} rather than T({}). */
3288 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
3289 compound_literal = build_tree_list (NULL_TREE, compound_literal);
3290 return build_functional_cast (input_location, type,
3291 compound_literal, complain);
3294 if (TREE_CODE (type) == ARRAY_TYPE
3295 && check_array_initializer (NULL_TREE, type, compound_literal))
3296 return error_mark_node;
3297 compound_literal = reshape_init (type, compound_literal, complain);
3298 if (SCALAR_TYPE_P (type)
3299 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
3300 && !check_narrowing (type, compound_literal, complain))
3301 return error_mark_node;
3302 if (TREE_CODE (type) == ARRAY_TYPE
3303 && TYPE_DOMAIN (type) == NULL_TREE)
3305 cp_complete_array_type_or_error (&type, compound_literal,
3306 false, complain);
3307 if (type == error_mark_node)
3308 return error_mark_node;
3310 compound_literal = digest_init_flags (type, compound_literal,
3311 LOOKUP_NORMAL | LOOKUP_NO_NARROWING,
3312 complain);
3313 if (compound_literal == error_mark_node)
3314 return error_mark_node;
3316 /* If we're in a template, return the original compound literal. */
3317 if (orig_cl)
3318 return orig_cl;
3320 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
3322 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
3323 if (fcl_context == fcl_c99)
3324 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
3327 /* Put static/constant array temporaries in static variables. */
3328 /* FIXME all C99 compound literals should be variables rather than C++
3329 temporaries, unless they are used as an aggregate initializer. */
3330 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
3331 && fcl_context == fcl_c99
3332 && TREE_CODE (type) == ARRAY_TYPE
3333 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3334 && initializer_constant_valid_p (compound_literal, type))
3336 tree decl = create_temporary_var (type);
3337 DECL_CONTEXT (decl) = NULL_TREE;
3338 DECL_INITIAL (decl) = compound_literal;
3339 TREE_STATIC (decl) = 1;
3340 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
3342 /* 5.19 says that a constant expression can include an
3343 lvalue-rvalue conversion applied to "a glvalue of literal type
3344 that refers to a non-volatile temporary object initialized
3345 with a constant expression". Rather than try to communicate
3346 that this VAR_DECL is a temporary, just mark it constexpr. */
3347 DECL_DECLARED_CONSTEXPR_P (decl) = true;
3348 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
3349 TREE_CONSTANT (decl) = true;
3351 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
3352 decl = pushdecl_top_level (decl);
3353 DECL_NAME (decl) = make_anon_name ();
3354 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
3355 /* Make sure the destructor is callable. */
3356 tree clean = cxx_maybe_build_cleanup (decl, complain);
3357 if (clean == error_mark_node)
3358 return error_mark_node;
3359 return decl;
3362 /* Represent other compound literals with TARGET_EXPR so we produce
3363 a prvalue, and can elide copies. */
3364 if (!VECTOR_TYPE_P (type)
3365 && (TREE_CODE (compound_literal) == CONSTRUCTOR
3366 || TREE_CODE (compound_literal) == VEC_INIT_EXPR))
3368 /* The CONSTRUCTOR is now an initializer, not a compound literal. */
3369 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
3370 TREE_HAS_CONSTRUCTOR (compound_literal) = false;
3371 compound_literal = get_target_expr (compound_literal, complain);
3373 else
3374 /* For e.g. int{42} just make sure it's a prvalue. */
3375 compound_literal = rvalue (compound_literal);
3377 return compound_literal;
3380 /* Return the declaration for the function-name variable indicated by
3381 ID. */
3383 tree
3384 finish_fname (tree id)
3386 tree decl;
3388 decl = fname_decl (input_location, C_RID_CODE (id), id);
3389 if (processing_template_decl && current_function_decl
3390 && decl != error_mark_node)
3391 decl = DECL_NAME (decl);
3392 return decl;
3395 /* Finish a translation unit. */
3397 void
3398 finish_translation_unit (void)
3400 /* In case there were missing closebraces,
3401 get us back to the global binding level. */
3402 pop_everything ();
3403 while (current_namespace != global_namespace)
3404 pop_namespace ();
3406 /* Do file scope __FUNCTION__ et al. */
3407 finish_fname_decls ();
3409 if (vec_safe_length (scope_chain->omp_declare_target_attribute))
3411 cp_omp_declare_target_attr
3412 a = scope_chain->omp_declare_target_attribute->pop ();
3413 if (!errorcount)
3414 error ("%qs without corresponding %qs",
3415 a.device_type >= 0 ? "#pragma omp begin declare target"
3416 : "#pragma omp declare target",
3417 "#pragma omp end declare target");
3418 vec_safe_truncate (scope_chain->omp_declare_target_attribute, 0);
3420 if (vec_safe_length (scope_chain->omp_begin_assumes))
3422 if (!errorcount)
3423 error ("%qs without corresponding %qs",
3424 "#pragma omp begin assumes", "#pragma omp end assumes");
3425 vec_safe_truncate (scope_chain->omp_begin_assumes, 0);
3429 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
3430 Returns the parameter. */
3432 tree
3433 finish_template_type_parm (tree aggr, tree identifier)
3435 if (aggr != class_type_node)
3437 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
3438 aggr = class_type_node;
3441 return build_tree_list (aggr, identifier);
3444 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
3445 Returns the parameter. */
3447 tree
3448 finish_template_template_parm (tree aggr, tree identifier)
3450 tree decl = build_decl (input_location,
3451 TYPE_DECL, identifier, NULL_TREE);
3453 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
3454 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
3455 DECL_TEMPLATE_RESULT (tmpl) = decl;
3456 DECL_ARTIFICIAL (decl) = 1;
3458 /* Associate the constraints with the underlying declaration,
3459 not the template. */
3460 tree constr = current_template_constraints ();
3461 set_constraints (decl, constr);
3463 end_template_decl ();
3465 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
3467 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
3468 /*is_primary=*/true, /*is_partial=*/false,
3469 /*is_friend=*/0);
3471 return finish_template_type_parm (aggr, tmpl);
3474 /* ARGUMENT is the default-argument value for a template template
3475 parameter. If ARGUMENT is invalid, issue error messages and return
3476 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
3478 tree
3479 check_template_template_default_arg (tree argument)
3481 if (TREE_CODE (argument) != TEMPLATE_DECL
3482 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
3483 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
3485 if (TREE_CODE (argument) == TYPE_DECL)
3487 if (tree t = maybe_get_template_decl_from_type_decl (argument))
3488 if (TREE_CODE (t) == TEMPLATE_DECL)
3489 return t;
3490 error ("invalid use of type %qT as a default value for a template "
3491 "template-parameter", TREE_TYPE (argument));
3493 else
3494 error ("invalid default argument for a template template parameter");
3495 return error_mark_node;
3498 return argument;
3501 /* Begin a class definition, as indicated by T. */
3503 tree
3504 begin_class_definition (tree t)
3506 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
3507 return error_mark_node;
3509 if (processing_template_parmlist && !LAMBDA_TYPE_P (t))
3511 error ("definition of %q#T inside template parameter list", t);
3512 return error_mark_node;
3515 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
3516 are passed the same as decimal scalar types. */
3517 if (TREE_CODE (t) == RECORD_TYPE
3518 && !processing_template_decl)
3520 tree ns = TYPE_CONTEXT (t);
3521 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
3522 && DECL_CONTEXT (ns) == std_node
3523 && DECL_NAME (ns)
3524 && id_equal (DECL_NAME (ns), "decimal"))
3526 const char *n = TYPE_NAME_STRING (t);
3527 if ((strcmp (n, "decimal32") == 0)
3528 || (strcmp (n, "decimal64") == 0)
3529 || (strcmp (n, "decimal128") == 0))
3530 TYPE_TRANSPARENT_AGGR (t) = 1;
3534 /* A non-implicit typename comes from code like:
3536 template <typename T> struct A {
3537 template <typename U> struct A<T>::B ...
3539 This is erroneous. */
3540 else if (TREE_CODE (t) == TYPENAME_TYPE)
3542 error ("invalid definition of qualified type %qT", t);
3543 t = error_mark_node;
3546 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
3548 t = make_class_type (RECORD_TYPE);
3549 pushtag (make_anon_name (), t);
3552 if (TYPE_BEING_DEFINED (t))
3554 t = make_class_type (TREE_CODE (t));
3555 pushtag (TYPE_IDENTIFIER (t), t);
3558 if (modules_p ())
3560 if (!module_may_redeclare (TYPE_NAME (t)))
3562 error ("cannot declare %qD in a different module", TYPE_NAME (t));
3563 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "declared here");
3564 return error_mark_node;
3566 set_instantiating_module (TYPE_NAME (t));
3567 set_defining_module (TYPE_NAME (t));
3570 maybe_process_partial_specialization (t);
3571 pushclass (t);
3572 TYPE_BEING_DEFINED (t) = 1;
3573 class_binding_level->defining_class_p = 1;
3575 if (flag_pack_struct)
3577 tree v;
3578 TYPE_PACKED (t) = 1;
3579 /* Even though the type is being defined for the first time
3580 here, there might have been a forward declaration, so there
3581 might be cv-qualified variants of T. */
3582 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
3583 TYPE_PACKED (v) = 1;
3585 /* Reset the interface data, at the earliest possible
3586 moment, as it might have been set via a class foo;
3587 before. */
3588 if (! TYPE_UNNAMED_P (t))
3590 struct c_fileinfo *finfo = \
3591 get_fileinfo (LOCATION_FILE (input_location));
3592 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
3593 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
3594 (t, finfo->interface_unknown);
3596 reset_specialization ();
3598 /* Make a declaration for this class in its own scope. */
3599 build_self_reference ();
3601 return t;
3604 /* Finish the member declaration given by DECL. */
3606 void
3607 finish_member_declaration (tree decl)
3609 if (decl == error_mark_node || decl == NULL_TREE)
3610 return;
3612 if (decl == void_type_node)
3613 /* The COMPONENT was a friend, not a member, and so there's
3614 nothing for us to do. */
3615 return;
3617 /* We should see only one DECL at a time. */
3618 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
3620 /* Don't add decls after definition. */
3621 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3622 /* We can add lambda types when late parsing default
3623 arguments. */
3624 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3626 /* Set up access control for DECL. */
3627 TREE_PRIVATE (decl)
3628 = (current_access_specifier == access_private_node);
3629 TREE_PROTECTED (decl)
3630 = (current_access_specifier == access_protected_node);
3631 if (TREE_CODE (decl) == TEMPLATE_DECL)
3633 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
3634 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
3637 /* Mark the DECL as a member of the current class, unless it's
3638 a member of an enumeration. */
3639 if (TREE_CODE (decl) != CONST_DECL)
3640 DECL_CONTEXT (decl) = current_class_type;
3642 /* Remember the single FIELD_DECL an anonymous aggregate type is used for. */
3643 if (TREE_CODE (decl) == FIELD_DECL
3644 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
3646 gcc_assert (!ANON_AGGR_TYPE_FIELD (TYPE_MAIN_VARIANT (TREE_TYPE (decl))));
3647 ANON_AGGR_TYPE_FIELD (TYPE_MAIN_VARIANT (TREE_TYPE (decl))) = decl;
3650 if (TREE_CODE (decl) == USING_DECL)
3651 /* Avoid debug info for class-scope USING_DECLS for now, we'll
3652 call cp_emit_debug_info_for_using later. */
3653 DECL_IGNORED_P (decl) = 1;
3655 /* Check for bare parameter packs in the non-static data member
3656 declaration. */
3657 if (TREE_CODE (decl) == FIELD_DECL)
3659 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3660 TREE_TYPE (decl) = error_mark_node;
3661 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
3662 DECL_ATTRIBUTES (decl) = NULL_TREE;
3665 /* [dcl.link]
3667 A C language linkage is ignored for the names of class members
3668 and the member function type of class member functions. */
3669 if (DECL_LANG_SPECIFIC (decl))
3670 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3672 bool add = false;
3674 /* Functions and non-functions are added differently. */
3675 if (DECL_DECLARES_FUNCTION_P (decl))
3676 add = add_method (current_class_type, decl, false);
3677 /* Enter the DECL into the scope of the class, if the class
3678 isn't a closure (whose fields are supposed to be unnamed). */
3679 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3680 || maybe_push_used_methods (decl)
3681 || pushdecl_class_level (decl))
3682 add = true;
3684 if (add)
3686 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3687 go at the beginning. The reason is that
3688 legacy_nonfn_member_lookup searches the list in order, and we
3689 want a field name to override a type name so that the "struct
3690 stat hack" will work. In particular:
3692 struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
3694 is valid. */
3696 if (TREE_CODE (decl) == TYPE_DECL)
3697 TYPE_FIELDS (current_class_type)
3698 = chainon (TYPE_FIELDS (current_class_type), decl);
3699 else
3701 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3702 TYPE_FIELDS (current_class_type) = decl;
3705 maybe_add_class_template_decl_list (current_class_type, decl,
3706 /*friend_p=*/0);
3710 /* Finish processing a complete template declaration. The PARMS are
3711 the template parameters. */
3713 void
3714 finish_template_decl (tree parms)
3716 if (parms)
3717 end_template_decl ();
3718 else
3719 end_specialization ();
3722 // Returns the template type of the class scope being entered. If we're
3723 // entering a constrained class scope. TYPE is the class template
3724 // scope being entered and we may need to match the intended type with
3725 // a constrained specialization. For example:
3727 // template<Object T>
3728 // struct S { void f(); }; #1
3730 // template<Object T>
3731 // void S<T>::f() { } #2
3733 // We check, in #2, that S<T> refers precisely to the type declared by
3734 // #1 (i.e., that the constraints match). Note that the following should
3735 // be an error since there is no specialization of S<T> that is
3736 // unconstrained, but this is not diagnosed here.
3738 // template<typename T>
3739 // void S<T>::f() { }
3741 // We cannot diagnose this problem here since this function also matches
3742 // qualified template names that are not part of a definition. For example:
3744 // template<Integral T, Floating_point U>
3745 // typename pair<T, U>::first_type void f(T, U);
3747 // Here, it is unlikely that there is a partial specialization of
3748 // pair constrained for for Integral and Floating_point arguments.
3750 // The general rule is: if a constrained specialization with matching
3751 // constraints is found return that type. Also note that if TYPE is not a
3752 // class-type (e.g. a typename type), then no fixup is needed.
3754 static tree
3755 fixup_template_type (tree type)
3757 // Find the template parameter list at the a depth appropriate to
3758 // the scope we're trying to enter.
3759 tree parms = current_template_parms;
3760 int depth = template_class_depth (type);
3761 for (int n = current_template_depth; n > depth && parms; --n)
3762 parms = TREE_CHAIN (parms);
3763 if (!parms)
3764 return type;
3765 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3766 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3768 // Search for a specialization whose type and constraints match.
3769 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3770 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3771 while (specs)
3773 tree spec_constr = get_constraints (TREE_VALUE (specs));
3775 // If the type and constraints match a specialization, then we
3776 // are entering that type.
3777 if (same_type_p (type, TREE_TYPE (specs))
3778 && equivalent_constraints (cur_constr, spec_constr))
3779 return TREE_TYPE (specs);
3780 specs = TREE_CHAIN (specs);
3783 // If no specialization matches, then must return the type
3784 // previously found.
3785 return type;
3788 /* Finish processing a template-id (which names a type) of the form
3789 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3790 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3791 the scope of template-id indicated. */
3793 tree
3794 finish_template_type (tree name, tree args, int entering_scope)
3796 tree type;
3798 type = lookup_template_class (name, args,
3799 NULL_TREE, NULL_TREE, entering_scope,
3800 tf_warning_or_error | tf_user);
3802 /* If we might be entering the scope of a partial specialization,
3803 find the one with the right constraints. */
3804 if (flag_concepts
3805 && entering_scope
3806 && CLASS_TYPE_P (type)
3807 && CLASSTYPE_TEMPLATE_INFO (type)
3808 && dependent_type_p (type)
3809 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3810 type = fixup_template_type (type);
3812 if (type == error_mark_node)
3813 return type;
3814 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3815 return TYPE_STUB_DECL (type);
3816 else
3817 return TYPE_NAME (type);
3820 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3821 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3822 BASE_CLASS, or NULL_TREE if an error occurred. The
3823 ACCESS_SPECIFIER is one of
3824 access_{default,public,protected_private}_node. For a virtual base
3825 we set TREE_TYPE. */
3827 tree
3828 finish_base_specifier (tree base, tree access, bool virtual_p)
3830 tree result;
3832 if (base == error_mark_node)
3834 error ("invalid base-class specification");
3835 result = NULL_TREE;
3837 else if (! MAYBE_CLASS_TYPE_P (base))
3839 error ("%qT is not a class type", base);
3840 result = NULL_TREE;
3842 else
3844 if (cp_type_quals (base) != 0)
3846 /* DR 484: Can a base-specifier name a cv-qualified
3847 class type? */
3848 base = TYPE_MAIN_VARIANT (base);
3850 result = build_tree_list (access, base);
3851 if (virtual_p)
3852 TREE_TYPE (result) = integer_type_node;
3855 return result;
3858 /* If FNS is a member function, a set of member functions, or a
3859 template-id referring to one or more member functions, return a
3860 BASELINK for FNS, incorporating the current access context.
3861 Otherwise, return FNS unchanged. */
3863 tree
3864 baselink_for_fns (tree fns)
3866 tree scope;
3867 tree cl;
3869 if (BASELINK_P (fns)
3870 || error_operand_p (fns))
3871 return fns;
3873 scope = ovl_scope (fns);
3874 if (!CLASS_TYPE_P (scope))
3875 return fns;
3877 cl = currently_open_derived_class (scope);
3878 if (!cl)
3879 cl = scope;
3880 tree access_path = TYPE_BINFO (cl);
3881 tree conv_path = (cl == scope ? access_path
3882 : lookup_base (cl, scope, ba_any, NULL, tf_none));
3883 return build_baselink (conv_path, access_path, fns, /*optype=*/NULL_TREE);
3886 /* Returns true iff DECL is a variable from a function outside
3887 the current one. */
3889 static bool
3890 outer_var_p (tree decl)
3892 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
3893 && DECL_FUNCTION_SCOPE_P (decl)
3894 /* Don't get confused by temporaries. */
3895 && DECL_NAME (decl)
3896 && (DECL_CONTEXT (decl) != current_function_decl
3897 || parsing_nsdmi ()));
3900 /* As above, but also checks that DECL is automatic. */
3902 bool
3903 outer_automatic_var_p (tree decl)
3905 return (outer_var_p (decl)
3906 && !TREE_STATIC (decl));
3909 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
3910 rewrite it for lambda capture.
3912 If ODR_USE is true, we're being called from mark_use, and we complain about
3913 use of constant variables. If ODR_USE is false, we're being called for the
3914 id-expression, and we do lambda capture. */
3916 tree
3917 process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use)
3919 if (cp_unevaluated_operand)
3921 tree type = TREE_TYPE (decl);
3922 if (!dependent_type_p (type)
3923 && variably_modified_type_p (type, NULL_TREE))
3924 /* VLAs are used even in unevaluated context. */;
3925 else
3926 /* It's not a use (3.2) if we're in an unevaluated context. */
3927 return decl;
3929 if (decl == error_mark_node)
3930 return decl;
3932 tree context = DECL_CONTEXT (decl);
3933 tree containing_function = current_function_decl;
3934 tree lambda_stack = NULL_TREE;
3935 tree lambda_expr = NULL_TREE;
3936 tree initializer = convert_from_reference (decl);
3938 /* Mark it as used now even if the use is ill-formed. */
3939 if (!mark_used (decl, complain))
3940 return error_mark_node;
3942 if (parsing_nsdmi ())
3943 containing_function = NULL_TREE;
3945 if (containing_function && LAMBDA_FUNCTION_P (containing_function))
3947 /* Check whether we've already built a proxy. */
3948 tree var = decl;
3949 while (is_normal_capture_proxy (var))
3950 var = DECL_CAPTURED_VARIABLE (var);
3951 tree d = retrieve_local_specialization (var);
3953 if (d && d != decl && is_capture_proxy (d))
3955 if (DECL_CONTEXT (d) == containing_function)
3956 /* We already have an inner proxy. */
3957 return d;
3958 else
3959 /* We need to capture an outer proxy. */
3960 return process_outer_var_ref (d, complain, odr_use);
3964 /* If we are in a lambda function, we can move out until we hit
3965 1. the context,
3966 2. a non-lambda function, or
3967 3. a non-default capturing lambda function. */
3968 while (context != containing_function
3969 /* containing_function can be null with invalid generic lambdas. */
3970 && containing_function
3971 && LAMBDA_FUNCTION_P (containing_function))
3973 tree closure = DECL_CONTEXT (containing_function);
3974 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
3976 if (TYPE_CLASS_SCOPE_P (closure))
3977 /* A lambda in an NSDMI (c++/64496). */
3978 break;
3980 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
3981 break;
3983 lambda_stack = tree_cons (NULL_TREE, lambda_expr, lambda_stack);
3985 containing_function = decl_function_context (containing_function);
3988 /* In a lambda within a template, wait until instantiation time to implicitly
3989 capture a parameter pack. We want to wait because we don't know if we're
3990 capturing the whole pack or a single element, and it's OK to wait because
3991 find_parameter_packs_r walks into the lambda body. */
3992 if (context == containing_function
3993 && DECL_PACK_P (decl))
3994 return decl;
3996 if (lambda_expr && VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))
3998 if (complain & tf_error)
3999 error ("cannot capture member %qD of anonymous union", decl);
4000 return error_mark_node;
4002 /* Do lambda capture when processing the id-expression, not when
4003 odr-using a variable. */
4004 if (!odr_use && context == containing_function)
4005 decl = add_default_capture (lambda_stack,
4006 /*id=*/DECL_NAME (decl), initializer);
4007 /* Only an odr-use of an outer automatic variable causes an
4008 error, and a constant variable can decay to a prvalue
4009 constant without odr-use. So don't complain yet. */
4010 else if (!odr_use && decl_constant_var_p (decl))
4011 return decl;
4012 else if (lambda_expr)
4014 if (complain & tf_error)
4016 error ("%qD is not captured", decl);
4017 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
4018 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
4019 inform (location_of (closure),
4020 "the lambda has no capture-default");
4021 else if (TYPE_CLASS_SCOPE_P (closure))
4022 inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot "
4023 "capture variables from the enclosing context",
4024 TYPE_CONTEXT (closure));
4025 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
4027 return error_mark_node;
4029 else if (processing_contract_condition && (TREE_CODE (decl) == PARM_DECL))
4030 /* Use of a parameter in a contract condition is fine. */
4031 return decl;
4032 else
4034 if (complain & tf_error)
4036 error (VAR_P (decl)
4037 ? G_("use of local variable with automatic storage from "
4038 "containing function")
4039 : G_("use of parameter from containing function"));
4040 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
4042 return error_mark_node;
4044 return decl;
4047 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
4048 id-expression. (See cp_parser_id_expression for details.) SCOPE,
4049 if non-NULL, is the type or namespace used to explicitly qualify
4050 ID_EXPRESSION. DECL is the entity to which that name has been
4051 resolved.
4053 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
4054 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
4055 be set to true if this expression isn't permitted in a
4056 constant-expression, but it is otherwise not set by this function.
4057 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
4058 constant-expression, but a non-constant expression is also
4059 permissible.
4061 DONE is true if this expression is a complete postfix-expression;
4062 it is false if this expression is followed by '->', '[', '(', etc.
4063 ADDRESS_P is true iff this expression is the operand of '&'.
4064 TEMPLATE_P is true iff the qualified-id was of the form
4065 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
4066 appears as a template argument.
4068 If an error occurs, and it is the kind of error that might cause
4069 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
4070 is the caller's responsibility to issue the message. *ERROR_MSG
4071 will be a string with static storage duration, so the caller need
4072 not "free" it.
4074 Return an expression for the entity, after issuing appropriate
4075 diagnostics. This function is also responsible for transforming a
4076 reference to a non-static member into a COMPONENT_REF that makes
4077 the use of "this" explicit.
4079 Upon return, *IDK will be filled in appropriately. */
4080 static cp_expr
4081 finish_id_expression_1 (tree id_expression,
4082 tree decl,
4083 tree scope,
4084 cp_id_kind *idk,
4085 bool integral_constant_expression_p,
4086 bool allow_non_integral_constant_expression_p,
4087 bool *non_integral_constant_expression_p,
4088 bool template_p,
4089 bool done,
4090 bool address_p,
4091 bool template_arg_p,
4092 const char **error_msg,
4093 location_t location)
4095 decl = strip_using_decl (decl);
4097 /* Initialize the output parameters. */
4098 *idk = CP_ID_KIND_NONE;
4099 *error_msg = NULL;
4101 if (id_expression == error_mark_node)
4102 return error_mark_node;
4103 /* If we have a template-id, then no further lookup is
4104 required. If the template-id was for a template-class, we
4105 will sometimes have a TYPE_DECL at this point. */
4106 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
4107 || TREE_CODE (decl) == TYPE_DECL)
4109 /* Look up the name. */
4110 else
4112 if (decl == error_mark_node)
4114 /* Name lookup failed. */
4115 if (scope
4116 && (!TYPE_P (scope)
4117 || (!dependent_type_p (scope)
4118 && !(identifier_p (id_expression)
4119 && IDENTIFIER_CONV_OP_P (id_expression)
4120 && dependent_type_p (TREE_TYPE (id_expression))))))
4122 /* If the qualifying type is non-dependent (and the name
4123 does not name a conversion operator to a dependent
4124 type), issue an error. */
4125 qualified_name_lookup_error (scope, id_expression, decl, location);
4126 return error_mark_node;
4128 else if (!scope)
4130 /* It may be resolved via Koenig lookup. */
4131 *idk = CP_ID_KIND_UNQUALIFIED;
4132 return id_expression;
4134 else
4135 decl = id_expression;
4138 /* Remember that the name was used in the definition of
4139 the current class so that we can check later to see if
4140 the meaning would have been different after the class
4141 was entirely defined. */
4142 if (!scope && decl != error_mark_node && identifier_p (id_expression))
4143 maybe_note_name_used_in_class (id_expression, decl);
4145 /* A use in unevaluated operand might not be instantiated appropriately
4146 if tsubst_copy builds a dummy parm, or if we never instantiate a
4147 generic lambda, so mark it now. */
4148 if (processing_template_decl && cp_unevaluated_operand)
4149 mark_type_use (decl);
4151 /* Disallow uses of local variables from containing functions, except
4152 within lambda-expressions. */
4153 if (outer_automatic_var_p (decl))
4155 decl = process_outer_var_ref (decl, tf_warning_or_error);
4156 if (decl == error_mark_node)
4157 return error_mark_node;
4160 /* Also disallow uses of function parameters outside the function
4161 body, except inside an unevaluated context (i.e. decltype). */
4162 if (TREE_CODE (decl) == PARM_DECL
4163 && DECL_CONTEXT (decl) == NULL_TREE
4164 && !cp_unevaluated_operand
4165 && !processing_contract_condition)
4167 *error_msg = G_("use of parameter outside function body");
4168 return error_mark_node;
4172 /* If we didn't find anything, or what we found was a type,
4173 then this wasn't really an id-expression. */
4174 if (TREE_CODE (decl) == TEMPLATE_DECL
4175 && !DECL_FUNCTION_TEMPLATE_P (decl))
4177 *error_msg = G_("missing template arguments");
4178 return error_mark_node;
4180 else if (TREE_CODE (decl) == TYPE_DECL
4181 || TREE_CODE (decl) == NAMESPACE_DECL)
4183 *error_msg = G_("expected primary-expression");
4184 return error_mark_node;
4187 /* If the name resolved to a template parameter, there is no
4188 need to look it up again later. */
4189 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
4190 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
4192 tree r;
4194 *idk = CP_ID_KIND_NONE;
4195 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
4196 decl = TEMPLATE_PARM_DECL (decl);
4197 r = DECL_INITIAL (decl);
4198 if (CLASS_TYPE_P (TREE_TYPE (r)) && !CP_TYPE_CONST_P (TREE_TYPE (r)))
4200 /* If the entity is a template parameter object for a template
4201 parameter of type T, the type of the expression is const T. */
4202 tree ctype = TREE_TYPE (r);
4203 ctype = cp_build_qualified_type (ctype, (cp_type_quals (ctype)
4204 | TYPE_QUAL_CONST));
4205 r = build1 (VIEW_CONVERT_EXPR, ctype, r);
4207 r = convert_from_reference (r);
4208 if (integral_constant_expression_p
4209 && !dependent_type_p (TREE_TYPE (decl))
4210 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
4212 if (!allow_non_integral_constant_expression_p)
4213 error ("template parameter %qD of type %qT is not allowed in "
4214 "an integral constant expression because it is not of "
4215 "integral or enumeration type", decl, TREE_TYPE (decl));
4216 *non_integral_constant_expression_p = true;
4218 return r;
4220 else if (TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE)
4222 gcc_checking_assert (scope);
4223 *idk = CP_ID_KIND_QUALIFIED;
4224 cp_warn_deprecated_use_scopes (scope);
4225 decl = finish_qualified_id_expr (scope, decl, done, address_p,
4226 template_p, template_arg_p,
4227 tf_warning_or_error);
4229 else
4231 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
4232 && variable_template_p (TREE_OPERAND (decl, 0))
4233 && !concept_check_p (decl))
4234 /* Try resolving this variable TEMPLATE_ID_EXPR (which is always
4235 considered type-dependent) now, so that the dependence test that
4236 follows gives us the right answer: if it represents a non-dependent
4237 variable template-id then finish_template_variable will yield the
4238 corresponding non-dependent VAR_DECL. */
4239 decl = finish_template_variable (decl);
4241 bool dependent_p = type_dependent_expression_p (decl);
4243 /* If the declaration was explicitly qualified indicate
4244 that. The semantics of `A::f(3)' are different than
4245 `f(3)' if `f' is virtual. */
4246 *idk = (scope
4247 ? CP_ID_KIND_QUALIFIED
4248 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
4249 ? CP_ID_KIND_TEMPLATE_ID
4250 : (dependent_p
4251 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
4252 : CP_ID_KIND_UNQUALIFIED)));
4254 if (dependent_p
4255 && !scope
4256 && DECL_P (decl)
4257 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
4258 /* Dependent type attributes on the decl mean that the TREE_TYPE is
4259 wrong, so just return the identifier. */
4260 return id_expression;
4262 if (DECL_CLASS_TEMPLATE_P (decl))
4264 error ("use of class template %qT as expression", decl);
4265 return error_mark_node;
4268 if (TREE_CODE (decl) == TREE_LIST)
4270 /* Ambiguous reference to base members. */
4271 error ("request for member %qD is ambiguous in "
4272 "multiple inheritance lattice", id_expression);
4273 print_candidates (decl);
4274 return error_mark_node;
4277 /* Mark variable-like entities as used. Functions are similarly
4278 marked either below or after overload resolution. */
4279 if ((VAR_P (decl)
4280 || TREE_CODE (decl) == PARM_DECL
4281 || TREE_CODE (decl) == CONST_DECL
4282 || TREE_CODE (decl) == RESULT_DECL)
4283 && !mark_used (decl))
4284 return error_mark_node;
4286 /* Only certain kinds of names are allowed in constant
4287 expression. Template parameters have already
4288 been handled above. */
4289 if (! error_operand_p (decl)
4290 && !dependent_p
4291 && integral_constant_expression_p
4292 && !decl_constant_var_p (decl)
4293 && TREE_CODE (decl) != CONST_DECL
4294 && !builtin_valid_in_constant_expr_p (decl)
4295 && !concept_check_p (decl))
4297 if (!allow_non_integral_constant_expression_p)
4299 error ("%qD cannot appear in a constant-expression", decl);
4300 return error_mark_node;
4302 *non_integral_constant_expression_p = true;
4305 if (tree wrap = maybe_get_tls_wrapper_call (decl))
4306 /* Replace an evaluated use of the thread_local variable with
4307 a call to its wrapper. */
4308 decl = wrap;
4309 else if (concept_check_p (decl))
4311 /* Nothing more to do. All of the analysis for concept checks
4312 is done by build_conept_id, called from the parser. */
4314 else if (scope)
4316 if (TREE_CODE (decl) == SCOPE_REF)
4318 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
4319 decl = TREE_OPERAND (decl, 1);
4322 decl = (adjust_result_of_qualified_name_lookup
4323 (decl, scope, current_nonlambda_class_type()));
4325 cp_warn_deprecated_use_scopes (scope);
4327 if (TYPE_P (scope))
4328 decl = finish_qualified_id_expr (scope,
4329 decl,
4330 done,
4331 address_p,
4332 template_p,
4333 template_arg_p,
4334 tf_warning_or_error);
4335 else
4336 decl = convert_from_reference (decl);
4338 else if (TREE_CODE (decl) == FIELD_DECL)
4340 /* Since SCOPE is NULL here, this is an unqualified name.
4341 Access checking has been performed during name lookup
4342 already. Turn off checking to avoid duplicate errors. */
4343 push_deferring_access_checks (dk_no_check);
4344 decl = finish_non_static_data_member (decl, NULL_TREE,
4345 /*qualifying_scope=*/NULL_TREE);
4346 pop_deferring_access_checks ();
4348 else if (is_overloaded_fn (decl))
4350 /* We only need to look at the first function,
4351 because all the fns share the attribute we're
4352 concerned with (all member fns or all non-members). */
4353 tree first_fn = get_first_fn (decl);
4354 first_fn = STRIP_TEMPLATE (first_fn);
4356 if (!template_arg_p
4357 && (TREE_CODE (first_fn) == USING_DECL
4358 || (TREE_CODE (first_fn) == FUNCTION_DECL
4359 && DECL_FUNCTION_MEMBER_P (first_fn)
4360 && !shared_member_p (decl))))
4362 /* A set of member functions. */
4363 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
4364 return finish_class_member_access_expr (decl, id_expression,
4365 /*template_p=*/false,
4366 tf_warning_or_error);
4369 decl = baselink_for_fns (decl);
4371 else
4373 if (DECL_P (decl) && DECL_NONLOCAL (decl)
4374 && DECL_CLASS_SCOPE_P (decl))
4376 tree context = context_for_name_lookup (decl);
4377 if (context != current_class_type)
4379 tree path = currently_open_derived_class (context);
4380 if (!path)
4381 /* PATH can be null for using an enum of an unrelated
4382 class; we checked its access in lookup_using_decl.
4384 ??? Should this case make a clone instead, like
4385 handle_using_decl? */
4386 gcc_assert (TREE_CODE (decl) == CONST_DECL);
4387 else
4388 perform_or_defer_access_check (TYPE_BINFO (path),
4389 decl, decl,
4390 tf_warning_or_error);
4394 decl = convert_from_reference (decl);
4398 return cp_expr (decl, location);
4401 /* As per finish_id_expression_1, but adding a wrapper node
4402 around the result if needed to express LOCATION. */
4404 cp_expr
4405 finish_id_expression (tree id_expression,
4406 tree decl,
4407 tree scope,
4408 cp_id_kind *idk,
4409 bool integral_constant_expression_p,
4410 bool allow_non_integral_constant_expression_p,
4411 bool *non_integral_constant_expression_p,
4412 bool template_p,
4413 bool done,
4414 bool address_p,
4415 bool template_arg_p,
4416 const char **error_msg,
4417 location_t location)
4419 cp_expr result
4420 = finish_id_expression_1 (id_expression, decl, scope, idk,
4421 integral_constant_expression_p,
4422 allow_non_integral_constant_expression_p,
4423 non_integral_constant_expression_p,
4424 template_p, done, address_p, template_arg_p,
4425 error_msg, location);
4426 return result.maybe_add_location_wrapper ();
4429 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
4430 use as a type-specifier. */
4432 tree
4433 finish_typeof (tree expr)
4435 tree type;
4437 if (type_dependent_expression_p (expr))
4439 type = cxx_make_type (TYPEOF_TYPE);
4440 TYPEOF_TYPE_EXPR (type) = expr;
4441 SET_TYPE_STRUCTURAL_EQUALITY (type);
4443 return type;
4446 expr = mark_type_use (expr);
4448 type = unlowered_expr_type (expr);
4450 if (!type || type == unknown_type_node)
4452 error ("type of %qE is unknown", expr);
4453 return error_mark_node;
4456 return type;
4459 /* Implement the __underlying_type keyword: Return the underlying
4460 type of TYPE, suitable for use as a type-specifier. */
4462 tree
4463 finish_underlying_type (tree type)
4465 if (!complete_type_or_else (type, NULL_TREE))
4466 return error_mark_node;
4468 if (TREE_CODE (type) != ENUMERAL_TYPE)
4470 error ("%qT is not an enumeration type", type);
4471 return error_mark_node;
4474 tree underlying_type = ENUM_UNDERLYING_TYPE (type);
4476 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
4477 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
4478 See finish_enum_value_list for details. */
4479 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
4480 underlying_type
4481 = c_common_type_for_mode (TYPE_MODE (underlying_type),
4482 TYPE_UNSIGNED (underlying_type));
4484 return underlying_type;
4487 /* Implement the __type_pack_element keyword: Return the type
4488 at index IDX within TYPES. */
4490 static tree
4491 finish_type_pack_element (tree idx, tree types, tsubst_flags_t complain)
4493 idx = maybe_constant_value (idx);
4494 if (TREE_CODE (idx) != INTEGER_CST || !INTEGRAL_TYPE_P (TREE_TYPE (idx)))
4496 if (complain & tf_error)
4497 error ("%<__type_pack_element%> index is not an integral constant");
4498 return error_mark_node;
4500 HOST_WIDE_INT val = tree_to_shwi (idx);
4501 if (val < 0)
4503 if (complain & tf_error)
4504 error ("%<__type_pack_element%> index is negative");
4505 return error_mark_node;
4507 if (val >= TREE_VEC_LENGTH (types))
4509 if (complain & tf_error)
4510 error ("%<__type_pack_element%> index is out of range");
4511 return error_mark_node;
4513 return TREE_VEC_ELT (types, val);
4516 /* Implement the __direct_bases keyword: Return the direct base classes
4517 of type. */
4519 tree
4520 calculate_direct_bases (tree type, tsubst_flags_t complain)
4522 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
4523 || !NON_UNION_CLASS_TYPE_P (type))
4524 return make_tree_vec (0);
4526 releasing_vec vector;
4527 vec<tree, va_gc> *base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
4528 tree binfo;
4529 unsigned i;
4531 /* Virtual bases are initialized first */
4532 for (i = 0; base_binfos->iterate (i, &binfo); i++)
4533 if (BINFO_VIRTUAL_P (binfo))
4534 vec_safe_push (vector, binfo);
4536 /* Now non-virtuals */
4537 for (i = 0; base_binfos->iterate (i, &binfo); i++)
4538 if (!BINFO_VIRTUAL_P (binfo))
4539 vec_safe_push (vector, binfo);
4541 tree bases_vec = make_tree_vec (vector->length ());
4543 for (i = 0; i < vector->length (); ++i)
4544 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
4546 return bases_vec;
4549 /* Implement the __bases keyword: Return the base classes
4550 of type */
4552 /* Find morally non-virtual base classes by walking binfo hierarchy */
4553 /* Virtual base classes are handled separately in finish_bases */
4555 static tree
4556 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
4558 /* Don't walk bases of virtual bases */
4559 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
4562 static tree
4563 dfs_calculate_bases_post (tree binfo, void *data_)
4565 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
4566 if (!BINFO_VIRTUAL_P (binfo))
4567 vec_safe_push (*data, BINFO_TYPE (binfo));
4568 return NULL_TREE;
4571 /* Calculates the morally non-virtual base classes of a class */
4572 static vec<tree, va_gc> *
4573 calculate_bases_helper (tree type)
4575 vec<tree, va_gc> *vector = make_tree_vector ();
4577 /* Now add non-virtual base classes in order of construction */
4578 if (TYPE_BINFO (type))
4579 dfs_walk_all (TYPE_BINFO (type),
4580 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
4581 return vector;
4584 tree
4585 calculate_bases (tree type, tsubst_flags_t complain)
4587 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
4588 || !NON_UNION_CLASS_TYPE_P (type))
4589 return make_tree_vec (0);
4591 releasing_vec vector;
4592 tree bases_vec = NULL_TREE;
4593 unsigned i;
4594 vec<tree, va_gc> *vbases;
4595 tree binfo;
4597 /* First go through virtual base classes */
4598 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
4599 vec_safe_iterate (vbases, i, &binfo); i++)
4601 releasing_vec vbase_bases
4602 = calculate_bases_helper (BINFO_TYPE (binfo));
4603 vec_safe_splice (vector, vbase_bases);
4606 /* Now for the non-virtual bases */
4607 releasing_vec nonvbases = calculate_bases_helper (type);
4608 vec_safe_splice (vector, nonvbases);
4610 /* Note that during error recovery vector->length can even be zero. */
4611 if (vector->length () > 1)
4613 /* Last element is entire class, so don't copy */
4614 bases_vec = make_tree_vec (vector->length () - 1);
4616 for (i = 0; i < vector->length () - 1; ++i)
4617 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
4619 else
4620 bases_vec = make_tree_vec (0);
4622 return bases_vec;
4625 tree
4626 finish_bases (tree type, bool direct)
4628 tree bases = NULL_TREE;
4630 if (!processing_template_decl)
4632 /* Parameter packs can only be used in templates */
4633 error ("parameter pack %<__bases%> only valid in template declaration");
4634 return error_mark_node;
4637 bases = cxx_make_type (BASES);
4638 BASES_TYPE (bases) = type;
4639 BASES_DIRECT (bases) = direct;
4640 SET_TYPE_STRUCTURAL_EQUALITY (bases);
4642 return bases;
4645 /* Perform C++-specific checks for __builtin_offsetof before calling
4646 fold_offsetof. */
4648 tree
4649 finish_offsetof (tree object_ptr, tree expr, location_t loc)
4651 /* If we're processing a template, we can't finish the semantics yet.
4652 Otherwise we can fold the entire expression now. */
4653 if (processing_template_decl)
4655 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
4656 SET_EXPR_LOCATION (expr, loc);
4657 return expr;
4660 if (expr == error_mark_node)
4661 return error_mark_node;
4663 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
4665 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
4666 TREE_OPERAND (expr, 2));
4667 return error_mark_node;
4669 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (expr))
4670 || TREE_TYPE (expr) == unknown_type_node)
4672 while (TREE_CODE (expr) == COMPONENT_REF
4673 || TREE_CODE (expr) == COMPOUND_EXPR)
4674 expr = TREE_OPERAND (expr, 1);
4676 if (DECL_P (expr))
4678 error ("cannot apply %<offsetof%> to member function %qD", expr);
4679 inform (DECL_SOURCE_LOCATION (expr), "declared here");
4681 else
4682 error ("cannot apply %<offsetof%> to member function");
4683 return error_mark_node;
4685 if (TREE_CODE (expr) == CONST_DECL)
4687 error ("cannot apply %<offsetof%> to an enumerator %qD", expr);
4688 return error_mark_node;
4690 if (REFERENCE_REF_P (expr))
4691 expr = TREE_OPERAND (expr, 0);
4692 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4693 return error_mark_node;
4694 if (warn_invalid_offsetof
4695 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4696 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4697 && cp_unevaluated_operand == 0)
4698 warning_at (loc, OPT_Winvalid_offsetof, "%<offsetof%> within "
4699 "non-standard-layout type %qT is conditionally-supported",
4700 TREE_TYPE (TREE_TYPE (object_ptr)));
4701 return fold_offsetof (expr);
4704 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4705 function is broken out from the above for the benefit of the tree-ssa
4706 project. */
4708 void
4709 simplify_aggr_init_expr (tree *tp)
4711 tree aggr_init_expr = *tp;
4713 /* Form an appropriate CALL_EXPR. */
4714 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4715 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4716 tree type = TREE_TYPE (slot);
4718 tree call_expr;
4719 enum style_t { ctor, arg, pcc } style;
4721 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4722 style = ctor;
4723 #ifdef PCC_STATIC_STRUCT_RETURN
4724 else if (1)
4725 style = pcc;
4726 #endif
4727 else
4729 gcc_assert (TREE_ADDRESSABLE (type));
4730 style = arg;
4733 call_expr = build_call_array_loc (input_location,
4734 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4736 aggr_init_expr_nargs (aggr_init_expr),
4737 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4738 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4739 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4740 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4741 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4742 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4743 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4745 if (style == ctor)
4747 /* Replace the first argument to the ctor with the address of the
4748 slot. */
4749 cxx_mark_addressable (slot);
4750 CALL_EXPR_ARG (call_expr, 0) =
4751 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4753 else if (style == arg)
4755 /* Just mark it addressable here, and leave the rest to
4756 expand_call{,_inline}. */
4757 cxx_mark_addressable (slot);
4758 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4759 call_expr = cp_build_init_expr (slot, call_expr);
4761 else if (style == pcc)
4763 /* If we're using the non-reentrant PCC calling convention, then we
4764 need to copy the returned value out of the static buffer into the
4765 SLOT. */
4766 push_deferring_access_checks (dk_no_check);
4767 call_expr = build_aggr_init (slot, call_expr,
4768 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4769 tf_warning_or_error);
4770 pop_deferring_access_checks ();
4771 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4774 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4776 tree init = build_zero_init (type, NULL_TREE,
4777 /*static_storage_p=*/false);
4778 init = cp_build_init_expr (slot, init);
4779 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4780 init, call_expr);
4783 *tp = call_expr;
4786 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4788 void
4789 emit_associated_thunks (tree fn)
4791 /* When we use vcall offsets, we emit thunks with the virtual
4792 functions to which they thunk. The whole point of vcall offsets
4793 is so that you can know statically the entire set of thunks that
4794 will ever be needed for a given virtual function, thereby
4795 enabling you to output all the thunks with the function itself. */
4796 if (DECL_VIRTUAL_P (fn)
4797 /* Do not emit thunks for extern template instantiations. */
4798 && ! DECL_REALLY_EXTERN (fn))
4800 tree thunk;
4802 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4804 if (!THUNK_ALIAS (thunk))
4806 use_thunk (thunk, /*emit_p=*/1);
4807 if (DECL_RESULT_THUNK_P (thunk))
4809 tree probe;
4811 for (probe = DECL_THUNKS (thunk);
4812 probe; probe = DECL_CHAIN (probe))
4813 use_thunk (probe, /*emit_p=*/1);
4816 else
4817 gcc_assert (!DECL_THUNKS (thunk));
4822 /* Generate RTL for FN. */
4824 bool
4825 expand_or_defer_fn_1 (tree fn)
4827 /* When the parser calls us after finishing the body of a template
4828 function, we don't really want to expand the body. */
4829 if (processing_template_decl)
4831 /* Normally, collection only occurs in rest_of_compilation. So,
4832 if we don't collect here, we never collect junk generated
4833 during the processing of templates until we hit a
4834 non-template function. It's not safe to do this inside a
4835 nested class, though, as the parser may have local state that
4836 is not a GC root. */
4837 if (!function_depth)
4838 ggc_collect ();
4839 return false;
4842 gcc_assert (DECL_SAVED_TREE (fn));
4844 /* We make a decision about linkage for these functions at the end
4845 of the compilation. Until that point, we do not want the back
4846 end to output them -- but we do want it to see the bodies of
4847 these functions so that it can inline them as appropriate. */
4848 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
4850 if (DECL_INTERFACE_KNOWN (fn))
4851 /* We've already made a decision as to how this function will
4852 be handled. */;
4853 else if (!at_eof
4854 || DECL_IMMEDIATE_FUNCTION_P (fn)
4855 || DECL_OMP_DECLARE_REDUCTION_P (fn))
4856 tentative_decl_linkage (fn);
4857 else
4858 import_export_decl (fn);
4860 /* If the user wants us to keep all inline functions, then mark
4861 this function as needed so that finish_file will make sure to
4862 output it later. Similarly, all dllexport'd functions must
4863 be emitted; there may be callers in other DLLs. */
4864 if (DECL_DECLARED_INLINE_P (fn)
4865 && !DECL_REALLY_EXTERN (fn)
4866 && !DECL_IMMEDIATE_FUNCTION_P (fn)
4867 && !DECL_OMP_DECLARE_REDUCTION_P (fn)
4868 && (flag_keep_inline_functions
4869 || (flag_keep_inline_dllexport
4870 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
4872 mark_needed (fn);
4873 DECL_EXTERNAL (fn) = 0;
4877 /* If this is a constructor or destructor body, we have to clone
4878 it. */
4879 if (maybe_clone_body (fn))
4881 /* We don't want to process FN again, so pretend we've written
4882 it out, even though we haven't. */
4883 TREE_ASM_WRITTEN (fn) = 1;
4884 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
4885 if (!DECL_DECLARED_CONSTEXPR_P (fn)
4886 && !(modules_p () && DECL_DECLARED_INLINE_P (fn)))
4887 DECL_SAVED_TREE (fn) = NULL_TREE;
4888 return false;
4891 /* There's no reason to do any of the work here if we're only doing
4892 semantic analysis; this code just generates RTL. */
4893 if (flag_syntax_only)
4895 /* Pretend that this function has been written out so that we don't try
4896 to expand it again. */
4897 TREE_ASM_WRITTEN (fn) = 1;
4898 return false;
4901 if (DECL_OMP_DECLARE_REDUCTION_P (fn))
4902 return false;
4904 return true;
4907 void
4908 expand_or_defer_fn (tree fn)
4910 if (expand_or_defer_fn_1 (fn))
4912 function_depth++;
4914 /* Expand or defer, at the whim of the compilation unit manager. */
4915 cgraph_node::finalize_function (fn, function_depth > 1);
4916 emit_associated_thunks (fn);
4918 function_depth--;
4920 if (DECL_IMMEDIATE_FUNCTION_P (fn))
4922 if (cgraph_node *node = cgraph_node::get (fn))
4924 node->body_removed = true;
4925 node->analyzed = false;
4926 node->definition = false;
4927 node->force_output = false;
4933 class nrv_data
4935 public:
4936 nrv_data () : visited (37) {}
4938 tree var;
4939 tree result;
4940 hash_table<nofree_ptr_hash <tree_node> > visited;
4943 /* Helper function for walk_tree, used by finalize_nrv below. */
4945 static tree
4946 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4948 class nrv_data *dp = (class nrv_data *)data;
4949 tree_node **slot;
4951 /* No need to walk into types. There wouldn't be any need to walk into
4952 non-statements, except that we have to consider STMT_EXPRs. */
4953 if (TYPE_P (*tp))
4954 *walk_subtrees = 0;
4955 /* Change all returns to just refer to the RESULT_DECL; this is a nop,
4956 but differs from using NULL_TREE in that it indicates that we care
4957 about the value of the RESULT_DECL. But preserve anything appended
4958 by check_return_expr. */
4959 else if (TREE_CODE (*tp) == RETURN_EXPR)
4961 tree *p = &TREE_OPERAND (*tp, 0);
4962 while (TREE_CODE (*p) == COMPOUND_EXPR)
4963 p = &TREE_OPERAND (*p, 0);
4964 gcc_checking_assert (TREE_CODE (*p) == INIT_EXPR
4965 && TREE_OPERAND (*p, 0) == dp->result);
4966 *p = dp->result;
4968 /* Change all cleanups for the NRV to only run when an exception is
4969 thrown. */
4970 else if (TREE_CODE (*tp) == CLEANUP_STMT
4971 && CLEANUP_DECL (*tp) == dp->var)
4972 CLEANUP_EH_ONLY (*tp) = 1;
4973 /* Replace the DECL_EXPR for the NRV with an initialization of the
4974 RESULT_DECL, if needed. */
4975 else if (TREE_CODE (*tp) == DECL_EXPR
4976 && DECL_EXPR_DECL (*tp) == dp->var)
4978 tree init;
4979 if (DECL_INITIAL (dp->var)
4980 && DECL_INITIAL (dp->var) != error_mark_node)
4981 init = cp_build_init_expr (dp->result,
4982 DECL_INITIAL (dp->var));
4983 else
4984 init = build_empty_stmt (EXPR_LOCATION (*tp));
4985 DECL_INITIAL (dp->var) = NULL_TREE;
4986 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
4987 *tp = init;
4989 /* And replace all uses of the NRV with the RESULT_DECL. */
4990 else if (*tp == dp->var)
4991 *tp = dp->result;
4993 /* Avoid walking into the same tree more than once. Unfortunately, we
4994 can't just use walk_tree_without duplicates because it would only call
4995 us for the first occurrence of dp->var in the function body. */
4996 slot = dp->visited.find_slot (*tp, INSERT);
4997 if (*slot)
4998 *walk_subtrees = 0;
4999 else
5000 *slot = *tp;
5002 /* Keep iterating. */
5003 return NULL_TREE;
5006 /* Called from finish_function to implement the named return value
5007 optimization by overriding all the RETURN_EXPRs and pertinent
5008 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
5009 RESULT_DECL for the function. */
5011 void
5012 finalize_nrv (tree *tp, tree var, tree result)
5014 class nrv_data data;
5016 /* Copy name from VAR to RESULT. */
5017 DECL_NAME (result) = DECL_NAME (var);
5018 /* Don't forget that we take its address. */
5019 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
5020 /* Finally set DECL_VALUE_EXPR to avoid assigning
5021 a stack slot at -O0 for the original var and debug info
5022 uses RESULT location for VAR. */
5023 SET_DECL_VALUE_EXPR (var, result);
5024 DECL_HAS_VALUE_EXPR_P (var) = 1;
5026 data.var = var;
5027 data.result = result;
5028 cp_walk_tree (tp, finalize_nrv_r, &data, 0);
5031 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
5033 bool
5034 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
5035 bool need_copy_ctor, bool need_copy_assignment,
5036 bool need_dtor)
5038 int save_errorcount = errorcount;
5039 tree info, t;
5041 /* Always allocate 3 elements for simplicity. These are the
5042 function decls for the ctor, dtor, and assignment op.
5043 This layout is known to the three lang hooks,
5044 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
5045 and cxx_omp_clause_assign_op. */
5046 info = make_tree_vec (3);
5047 CP_OMP_CLAUSE_INFO (c) = info;
5049 if (need_default_ctor || need_copy_ctor)
5051 if (need_default_ctor)
5052 t = get_default_ctor (type);
5053 else
5054 t = get_copy_ctor (type, tf_warning_or_error);
5056 if (t && !trivial_fn_p (t))
5057 TREE_VEC_ELT (info, 0) = t;
5060 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5061 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
5063 if (need_copy_assignment)
5065 t = get_copy_assign (type);
5067 if (t && !trivial_fn_p (t))
5068 TREE_VEC_ELT (info, 2) = t;
5071 return errorcount != save_errorcount;
5074 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
5075 FIELD_DECL, otherwise return DECL itself. */
5077 static tree
5078 omp_clause_decl_field (tree decl)
5080 if (VAR_P (decl)
5081 && DECL_HAS_VALUE_EXPR_P (decl)
5082 && DECL_ARTIFICIAL (decl)
5083 && DECL_LANG_SPECIFIC (decl)
5084 && DECL_OMP_PRIVATIZED_MEMBER (decl))
5086 tree f = DECL_VALUE_EXPR (decl);
5087 if (INDIRECT_REF_P (f))
5088 f = TREE_OPERAND (f, 0);
5089 if (TREE_CODE (f) == COMPONENT_REF)
5091 f = TREE_OPERAND (f, 1);
5092 gcc_assert (TREE_CODE (f) == FIELD_DECL);
5093 return f;
5096 return NULL_TREE;
5099 /* Adjust DECL if needed for printing using %qE. */
5101 static tree
5102 omp_clause_printable_decl (tree decl)
5104 tree t = omp_clause_decl_field (decl);
5105 if (t)
5106 return t;
5107 return decl;
5110 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
5111 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
5112 privatization. */
5114 static void
5115 omp_note_field_privatization (tree f, tree t)
5117 if (!omp_private_member_map)
5118 omp_private_member_map = new hash_map<tree, tree>;
5119 tree &v = omp_private_member_map->get_or_insert (f);
5120 if (v == NULL_TREE)
5122 v = t;
5123 omp_private_member_vec.safe_push (f);
5124 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
5125 omp_private_member_vec.safe_push (integer_zero_node);
5129 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
5130 dummy VAR_DECL. */
5132 tree
5133 omp_privatize_field (tree t, bool shared)
5135 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
5136 if (m == error_mark_node)
5137 return error_mark_node;
5138 if (!omp_private_member_map && !shared)
5139 omp_private_member_map = new hash_map<tree, tree>;
5140 if (TYPE_REF_P (TREE_TYPE (t)))
5142 gcc_assert (INDIRECT_REF_P (m));
5143 m = TREE_OPERAND (m, 0);
5145 tree vb = NULL_TREE;
5146 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
5147 if (v == NULL_TREE)
5149 v = create_temporary_var (TREE_TYPE (m));
5150 retrofit_lang_decl (v);
5151 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
5152 SET_DECL_VALUE_EXPR (v, m);
5153 DECL_HAS_VALUE_EXPR_P (v) = 1;
5154 if (!shared)
5155 omp_private_member_vec.safe_push (t);
5157 return v;
5160 /* Helper function for handle_omp_array_sections. Called recursively
5161 to handle multiple array-section-subscripts. C is the clause,
5162 T current expression (initially OMP_CLAUSE_DECL), which is either
5163 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
5164 expression if specified, TREE_VALUE length expression if specified,
5165 TREE_CHAIN is what it has been specified after, or some decl.
5166 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
5167 set to true if any of the array-section-subscript could have length
5168 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
5169 first array-section-subscript which is known not to have length
5170 of one. Given say:
5171 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
5172 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
5173 all are or may have length of 1, array-section-subscript [:2] is the
5174 first one known not to have length 1. For array-section-subscript
5175 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
5176 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
5177 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
5178 case though, as some lengths could be zero. */
5180 static tree
5181 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
5182 bool &maybe_zero_len, unsigned int &first_non_one,
5183 enum c_omp_region_type ort)
5185 tree ret, low_bound, length, type;
5186 if (TREE_CODE (t) != TREE_LIST)
5188 if (error_operand_p (t))
5189 return error_mark_node;
5190 if (REFERENCE_REF_P (t)
5191 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5192 t = TREE_OPERAND (t, 0);
5193 ret = t;
5194 while (INDIRECT_REF_P (t))
5196 t = TREE_OPERAND (t, 0);
5197 STRIP_NOPS (t);
5198 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5199 t = TREE_OPERAND (t, 0);
5201 while (TREE_CODE (t) == COMPOUND_EXPR)
5203 t = TREE_OPERAND (t, 1);
5204 STRIP_NOPS (t);
5206 if (TREE_CODE (t) == COMPONENT_REF
5207 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5208 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
5209 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
5210 && !type_dependent_expression_p (t))
5212 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
5213 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
5215 error_at (OMP_CLAUSE_LOCATION (c),
5216 "bit-field %qE in %qs clause",
5217 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5218 return error_mark_node;
5220 while (TREE_CODE (t) == COMPONENT_REF)
5222 if (TREE_TYPE (TREE_OPERAND (t, 0))
5223 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
5225 error_at (OMP_CLAUSE_LOCATION (c),
5226 "%qE is a member of a union", t);
5227 return error_mark_node;
5229 t = TREE_OPERAND (t, 0);
5230 while (TREE_CODE (t) == MEM_REF
5231 || INDIRECT_REF_P (t)
5232 || TREE_CODE (t) == ARRAY_REF)
5234 t = TREE_OPERAND (t, 0);
5235 STRIP_NOPS (t);
5236 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5237 t = TREE_OPERAND (t, 0);
5240 if (REFERENCE_REF_P (t))
5241 t = TREE_OPERAND (t, 0);
5243 if (TREE_CODE (t) == FIELD_DECL)
5244 ret = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
5245 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5247 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
5248 return NULL_TREE;
5249 if (DECL_P (t))
5250 error_at (OMP_CLAUSE_LOCATION (c),
5251 "%qD is not a variable in %qs clause", t,
5252 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5253 else
5254 error_at (OMP_CLAUSE_LOCATION (c),
5255 "%qE is not a variable in %qs clause", t,
5256 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5257 return error_mark_node;
5259 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5260 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5261 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
5263 error_at (OMP_CLAUSE_LOCATION (c),
5264 "%qD is threadprivate variable in %qs clause", t,
5265 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5266 return error_mark_node;
5268 if (type_dependent_expression_p (ret))
5269 return NULL_TREE;
5270 ret = convert_from_reference (ret);
5271 return ret;
5274 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
5275 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5276 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5277 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5278 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
5279 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
5280 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
5281 maybe_zero_len, first_non_one, ort);
5282 if (ret == error_mark_node || ret == NULL_TREE)
5283 return ret;
5285 type = TREE_TYPE (ret);
5286 low_bound = TREE_PURPOSE (t);
5287 length = TREE_VALUE (t);
5288 if ((low_bound && type_dependent_expression_p (low_bound))
5289 || (length && type_dependent_expression_p (length)))
5290 return NULL_TREE;
5292 if (low_bound == error_mark_node || length == error_mark_node)
5293 return error_mark_node;
5295 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
5297 error_at (OMP_CLAUSE_LOCATION (c),
5298 "low bound %qE of array section does not have integral type",
5299 low_bound);
5300 return error_mark_node;
5302 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
5304 error_at (OMP_CLAUSE_LOCATION (c),
5305 "length %qE of array section does not have integral type",
5306 length);
5307 return error_mark_node;
5309 if (low_bound)
5310 low_bound = mark_rvalue_use (low_bound);
5311 if (length)
5312 length = mark_rvalue_use (length);
5313 /* We need to reduce to real constant-values for checks below. */
5314 if (length)
5315 length = fold_simple (length);
5316 if (low_bound)
5317 low_bound = fold_simple (low_bound);
5318 if (low_bound
5319 && TREE_CODE (low_bound) == INTEGER_CST
5320 && TYPE_PRECISION (TREE_TYPE (low_bound))
5321 > TYPE_PRECISION (sizetype))
5322 low_bound = fold_convert (sizetype, low_bound);
5323 if (length
5324 && TREE_CODE (length) == INTEGER_CST
5325 && TYPE_PRECISION (TREE_TYPE (length))
5326 > TYPE_PRECISION (sizetype))
5327 length = fold_convert (sizetype, length);
5328 if (low_bound == NULL_TREE)
5329 low_bound = integer_zero_node;
5331 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5332 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
5333 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
5335 if (length != integer_one_node)
5337 error_at (OMP_CLAUSE_LOCATION (c),
5338 "expected single pointer in %qs clause",
5339 user_omp_clause_code_name (c, ort == C_ORT_ACC));
5340 return error_mark_node;
5343 if (length != NULL_TREE)
5345 if (!integer_nonzerop (length))
5347 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
5348 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5349 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5350 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5351 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5353 if (integer_zerop (length))
5355 error_at (OMP_CLAUSE_LOCATION (c),
5356 "zero length array section in %qs clause",
5357 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5358 return error_mark_node;
5361 else
5362 maybe_zero_len = true;
5364 if (first_non_one == types.length ()
5365 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
5366 first_non_one++;
5368 if (TREE_CODE (type) == ARRAY_TYPE)
5370 if (length == NULL_TREE
5371 && (TYPE_DOMAIN (type) == NULL_TREE
5372 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
5374 error_at (OMP_CLAUSE_LOCATION (c),
5375 "for unknown bound array type length expression must "
5376 "be specified");
5377 return error_mark_node;
5379 if (TREE_CODE (low_bound) == INTEGER_CST
5380 && tree_int_cst_sgn (low_bound) == -1)
5382 error_at (OMP_CLAUSE_LOCATION (c),
5383 "negative low bound in array section in %qs clause",
5384 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5385 return error_mark_node;
5387 if (length != NULL_TREE
5388 && TREE_CODE (length) == INTEGER_CST
5389 && tree_int_cst_sgn (length) == -1)
5391 error_at (OMP_CLAUSE_LOCATION (c),
5392 "negative length in array section in %qs clause",
5393 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5394 return error_mark_node;
5396 if (TYPE_DOMAIN (type)
5397 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
5398 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
5399 == INTEGER_CST)
5401 tree size
5402 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
5403 size = size_binop (PLUS_EXPR, size, size_one_node);
5404 if (TREE_CODE (low_bound) == INTEGER_CST)
5406 if (tree_int_cst_lt (size, low_bound))
5408 error_at (OMP_CLAUSE_LOCATION (c),
5409 "low bound %qE above array section size "
5410 "in %qs clause", low_bound,
5411 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5412 return error_mark_node;
5414 if (tree_int_cst_equal (size, low_bound))
5416 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
5417 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5418 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5419 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5420 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5422 error_at (OMP_CLAUSE_LOCATION (c),
5423 "zero length array section in %qs clause",
5424 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5425 return error_mark_node;
5427 maybe_zero_len = true;
5429 else if (length == NULL_TREE
5430 && first_non_one == types.length ()
5431 && tree_int_cst_equal
5432 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
5433 low_bound))
5434 first_non_one++;
5436 else if (length == NULL_TREE)
5438 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5439 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5440 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
5441 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
5442 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
5443 maybe_zero_len = true;
5444 if (first_non_one == types.length ())
5445 first_non_one++;
5447 if (length && TREE_CODE (length) == INTEGER_CST)
5449 if (tree_int_cst_lt (size, length))
5451 error_at (OMP_CLAUSE_LOCATION (c),
5452 "length %qE above array section size "
5453 "in %qs clause", length,
5454 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5455 return error_mark_node;
5457 if (TREE_CODE (low_bound) == INTEGER_CST)
5459 tree lbpluslen
5460 = size_binop (PLUS_EXPR,
5461 fold_convert (sizetype, low_bound),
5462 fold_convert (sizetype, length));
5463 if (TREE_CODE (lbpluslen) == INTEGER_CST
5464 && tree_int_cst_lt (size, lbpluslen))
5466 error_at (OMP_CLAUSE_LOCATION (c),
5467 "high bound %qE above array section size "
5468 "in %qs clause", lbpluslen,
5469 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5470 return error_mark_node;
5475 else if (length == NULL_TREE)
5477 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5478 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5479 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
5480 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
5481 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
5482 maybe_zero_len = true;
5483 if (first_non_one == types.length ())
5484 first_non_one++;
5487 /* For [lb:] we will need to evaluate lb more than once. */
5488 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
5490 tree lb = cp_save_expr (low_bound);
5491 if (lb != low_bound)
5493 TREE_PURPOSE (t) = lb;
5494 low_bound = lb;
5498 else if (TYPE_PTR_P (type))
5500 if (length == NULL_TREE)
5502 if (TREE_CODE (ret) == PARM_DECL && DECL_ARRAY_PARAMETER_P (ret))
5503 error_at (OMP_CLAUSE_LOCATION (c),
5504 "for array function parameter length expression "
5505 "must be specified");
5506 else
5507 error_at (OMP_CLAUSE_LOCATION (c),
5508 "for pointer type length expression must be specified");
5509 return error_mark_node;
5511 if (length != NULL_TREE
5512 && TREE_CODE (length) == INTEGER_CST
5513 && tree_int_cst_sgn (length) == -1)
5515 error_at (OMP_CLAUSE_LOCATION (c),
5516 "negative length in array section in %qs clause",
5517 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5518 return error_mark_node;
5520 /* If there is a pointer type anywhere but in the very first
5521 array-section-subscript, the array section could be non-contiguous. */
5522 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5523 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5524 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
5526 /* If any prior dimension has a non-one length, then deem this
5527 array section as non-contiguous. */
5528 for (tree d = TREE_CHAIN (t); TREE_CODE (d) == TREE_LIST;
5529 d = TREE_CHAIN (d))
5531 tree d_length = TREE_VALUE (d);
5532 if (d_length == NULL_TREE || !integer_onep (d_length))
5534 error_at (OMP_CLAUSE_LOCATION (c),
5535 "array section is not contiguous in %qs clause",
5536 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5537 return error_mark_node;
5542 else
5544 error_at (OMP_CLAUSE_LOCATION (c),
5545 "%qE does not have pointer or array type", ret);
5546 return error_mark_node;
5548 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
5549 types.safe_push (TREE_TYPE (ret));
5550 /* We will need to evaluate lb more than once. */
5551 tree lb = cp_save_expr (low_bound);
5552 if (lb != low_bound)
5554 TREE_PURPOSE (t) = lb;
5555 low_bound = lb;
5557 /* Temporarily disable -fstrong-eval-order for array reductions.
5558 The SAVE_EXPR and COMPOUND_EXPR added if low_bound has side-effects
5559 is something the middle-end can't cope with and more importantly,
5560 it needs to be the actual base variable that is privatized, not some
5561 temporary assigned previous value of it. That, together with OpenMP
5562 saying how many times the side-effects are evaluated is unspecified,
5563 makes int *a, *b; ... reduction(+:a[a = b, 3:10]) really unspecified. */
5564 warning_sentinel s (flag_strong_eval_order,
5565 OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5566 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5567 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION);
5568 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, NULL,
5569 tf_warning_or_error);
5570 return ret;
5573 /* Handle array sections for clause C. */
5575 static bool
5576 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
5578 bool maybe_zero_len = false;
5579 unsigned int first_non_one = 0;
5580 auto_vec<tree, 10> types;
5581 tree *tp = &OMP_CLAUSE_DECL (c);
5582 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5583 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
5584 && TREE_CODE (*tp) == TREE_LIST
5585 && TREE_PURPOSE (*tp)
5586 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
5587 tp = &TREE_VALUE (*tp);
5588 tree first = handle_omp_array_sections_1 (c, *tp, types,
5589 maybe_zero_len, first_non_one,
5590 ort);
5591 if (first == error_mark_node)
5592 return true;
5593 if (first == NULL_TREE)
5594 return false;
5595 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5596 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
5598 tree t = *tp;
5599 tree tem = NULL_TREE;
5600 if (processing_template_decl)
5601 return false;
5602 /* Need to evaluate side effects in the length expressions
5603 if any. */
5604 while (TREE_CODE (t) == TREE_LIST)
5606 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
5608 if (tem == NULL_TREE)
5609 tem = TREE_VALUE (t);
5610 else
5611 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
5612 TREE_VALUE (t), tem);
5614 t = TREE_CHAIN (t);
5616 if (tem)
5617 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
5618 *tp = first;
5620 else
5622 unsigned int num = types.length (), i;
5623 tree t, side_effects = NULL_TREE, size = NULL_TREE;
5624 tree condition = NULL_TREE;
5626 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
5627 maybe_zero_len = true;
5628 if (processing_template_decl && maybe_zero_len)
5629 return false;
5631 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
5632 t = TREE_CHAIN (t))
5634 tree low_bound = TREE_PURPOSE (t);
5635 tree length = TREE_VALUE (t);
5637 i--;
5638 if (low_bound
5639 && TREE_CODE (low_bound) == INTEGER_CST
5640 && TYPE_PRECISION (TREE_TYPE (low_bound))
5641 > TYPE_PRECISION (sizetype))
5642 low_bound = fold_convert (sizetype, low_bound);
5643 if (length
5644 && TREE_CODE (length) == INTEGER_CST
5645 && TYPE_PRECISION (TREE_TYPE (length))
5646 > TYPE_PRECISION (sizetype))
5647 length = fold_convert (sizetype, length);
5648 if (low_bound == NULL_TREE)
5649 low_bound = integer_zero_node;
5650 if (!maybe_zero_len && i > first_non_one)
5652 if (integer_nonzerop (low_bound))
5653 goto do_warn_noncontiguous;
5654 if (length != NULL_TREE
5655 && TREE_CODE (length) == INTEGER_CST
5656 && TYPE_DOMAIN (types[i])
5657 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
5658 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
5659 == INTEGER_CST)
5661 tree size;
5662 size = size_binop (PLUS_EXPR,
5663 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
5664 size_one_node);
5665 if (!tree_int_cst_equal (length, size))
5667 do_warn_noncontiguous:
5668 error_at (OMP_CLAUSE_LOCATION (c),
5669 "array section is not contiguous in %qs "
5670 "clause",
5671 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5672 return true;
5675 if (!processing_template_decl
5676 && length != NULL_TREE
5677 && TREE_SIDE_EFFECTS (length))
5679 if (side_effects == NULL_TREE)
5680 side_effects = length;
5681 else
5682 side_effects = build2 (COMPOUND_EXPR,
5683 TREE_TYPE (side_effects),
5684 length, side_effects);
5687 else if (processing_template_decl)
5688 continue;
5689 else
5691 tree l;
5693 if (i > first_non_one
5694 && ((length && integer_nonzerop (length))
5695 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5696 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5697 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
5698 continue;
5699 if (length)
5700 l = fold_convert (sizetype, length);
5701 else
5703 l = size_binop (PLUS_EXPR,
5704 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
5705 size_one_node);
5706 l = size_binop (MINUS_EXPR, l,
5707 fold_convert (sizetype, low_bound));
5709 if (i > first_non_one)
5711 l = fold_build2 (NE_EXPR, boolean_type_node, l,
5712 size_zero_node);
5713 if (condition == NULL_TREE)
5714 condition = l;
5715 else
5716 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
5717 l, condition);
5719 else if (size == NULL_TREE)
5721 size = size_in_bytes (TREE_TYPE (types[i]));
5722 tree eltype = TREE_TYPE (types[num - 1]);
5723 while (TREE_CODE (eltype) == ARRAY_TYPE)
5724 eltype = TREE_TYPE (eltype);
5725 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5726 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5727 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5728 size = size_binop (EXACT_DIV_EXPR, size,
5729 size_in_bytes (eltype));
5730 size = size_binop (MULT_EXPR, size, l);
5731 if (condition)
5732 size = fold_build3 (COND_EXPR, sizetype, condition,
5733 size, size_zero_node);
5735 else
5736 size = size_binop (MULT_EXPR, size, l);
5739 if (!processing_template_decl)
5741 if (side_effects)
5742 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
5743 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5744 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5745 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5747 size = size_binop (MINUS_EXPR, size, size_one_node);
5748 size = save_expr (size);
5749 tree index_type = build_index_type (size);
5750 tree eltype = TREE_TYPE (first);
5751 while (TREE_CODE (eltype) == ARRAY_TYPE)
5752 eltype = TREE_TYPE (eltype);
5753 tree type = build_array_type (eltype, index_type);
5754 tree ptype = build_pointer_type (eltype);
5755 if (TYPE_REF_P (TREE_TYPE (t))
5756 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
5757 t = convert_from_reference (t);
5758 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5759 t = build_fold_addr_expr (t);
5760 tree t2 = build_fold_addr_expr (first);
5761 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5762 ptrdiff_type_node, t2);
5763 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5764 ptrdiff_type_node, t2,
5765 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5766 ptrdiff_type_node, t));
5767 if (tree_fits_shwi_p (t2))
5768 t = build2 (MEM_REF, type, t,
5769 build_int_cst (ptype, tree_to_shwi (t2)));
5770 else
5772 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5773 sizetype, t2);
5774 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
5775 TREE_TYPE (t), t, t2);
5776 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
5778 OMP_CLAUSE_DECL (c) = t;
5779 return false;
5781 OMP_CLAUSE_DECL (c) = first;
5782 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
5783 return false;
5784 OMP_CLAUSE_SIZE (c) = size;
5785 if (TREE_CODE (t) == FIELD_DECL)
5786 t = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
5787 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5788 || (TREE_CODE (t) == COMPONENT_REF
5789 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5790 return false;
5791 switch (OMP_CLAUSE_MAP_KIND (c))
5793 case GOMP_MAP_ALLOC:
5794 case GOMP_MAP_IF_PRESENT:
5795 case GOMP_MAP_TO:
5796 case GOMP_MAP_FROM:
5797 case GOMP_MAP_TOFROM:
5798 case GOMP_MAP_ALWAYS_TO:
5799 case GOMP_MAP_ALWAYS_FROM:
5800 case GOMP_MAP_ALWAYS_TOFROM:
5801 case GOMP_MAP_RELEASE:
5802 case GOMP_MAP_DELETE:
5803 case GOMP_MAP_FORCE_TO:
5804 case GOMP_MAP_FORCE_FROM:
5805 case GOMP_MAP_FORCE_TOFROM:
5806 case GOMP_MAP_FORCE_PRESENT:
5807 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5808 break;
5809 default:
5810 break;
5812 bool reference_always_pointer = true;
5813 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5814 OMP_CLAUSE_MAP);
5815 if (TREE_CODE (t) == COMPONENT_REF)
5817 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
5819 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
5820 && TYPE_REF_P (TREE_TYPE (t)))
5822 if (TREE_CODE (TREE_TYPE (TREE_TYPE (t))) == ARRAY_TYPE)
5823 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5824 else
5825 t = convert_from_reference (t);
5827 reference_always_pointer = false;
5830 else if (REFERENCE_REF_P (t)
5831 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5833 gomp_map_kind k;
5834 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
5835 && TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
5836 k = GOMP_MAP_ATTACH_DETACH;
5837 else
5839 t = TREE_OPERAND (t, 0);
5840 k = (ort == C_ORT_ACC
5841 ? GOMP_MAP_ATTACH_DETACH : GOMP_MAP_ALWAYS_POINTER);
5843 OMP_CLAUSE_SET_MAP_KIND (c2, k);
5845 else
5846 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5847 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
5848 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5849 && !cxx_mark_addressable (t))
5850 return false;
5851 OMP_CLAUSE_DECL (c2) = t;
5852 t = build_fold_addr_expr (first);
5853 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5854 ptrdiff_type_node, t);
5855 tree ptr = OMP_CLAUSE_DECL (c2);
5856 ptr = convert_from_reference (ptr);
5857 if (!INDIRECT_TYPE_P (TREE_TYPE (ptr)))
5858 ptr = build_fold_addr_expr (ptr);
5859 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5860 ptrdiff_type_node, t,
5861 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5862 ptrdiff_type_node, ptr));
5863 OMP_CLAUSE_SIZE (c2) = t;
5864 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5865 OMP_CLAUSE_CHAIN (c) = c2;
5867 ptr = OMP_CLAUSE_DECL (c2);
5868 if (reference_always_pointer
5869 && OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5870 && TYPE_REF_P (TREE_TYPE (ptr))
5871 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5873 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5874 OMP_CLAUSE_MAP);
5875 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5876 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
5877 OMP_CLAUSE_DECL (c3) = ptr;
5878 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER
5879 || OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ATTACH_DETACH)
5881 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5882 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5884 else
5885 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5886 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5887 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5888 OMP_CLAUSE_CHAIN (c2) = c3;
5892 return false;
5895 /* Return identifier to look up for omp declare reduction. */
5897 tree
5898 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5900 const char *p = NULL;
5901 const char *m = NULL;
5902 switch (reduction_code)
5904 case PLUS_EXPR:
5905 case MULT_EXPR:
5906 case MINUS_EXPR:
5907 case BIT_AND_EXPR:
5908 case BIT_XOR_EXPR:
5909 case BIT_IOR_EXPR:
5910 case TRUTH_ANDIF_EXPR:
5911 case TRUTH_ORIF_EXPR:
5912 reduction_id = ovl_op_identifier (false, reduction_code);
5913 break;
5914 case MIN_EXPR:
5915 p = "min";
5916 break;
5917 case MAX_EXPR:
5918 p = "max";
5919 break;
5920 default:
5921 break;
5924 if (p == NULL)
5926 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5927 return error_mark_node;
5928 p = IDENTIFIER_POINTER (reduction_id);
5931 if (type != NULL_TREE)
5932 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5934 const char prefix[] = "omp declare reduction ";
5935 size_t lenp = sizeof (prefix);
5936 if (strncmp (p, prefix, lenp - 1) == 0)
5937 lenp = 1;
5938 size_t len = strlen (p);
5939 size_t lenm = m ? strlen (m) + 1 : 0;
5940 char *name = XALLOCAVEC (char, lenp + len + lenm);
5941 if (lenp > 1)
5942 memcpy (name, prefix, lenp - 1);
5943 memcpy (name + lenp - 1, p, len + 1);
5944 if (m)
5946 name[lenp + len - 1] = '~';
5947 memcpy (name + lenp + len, m, lenm);
5949 return get_identifier (name);
5952 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5953 FUNCTION_DECL or NULL_TREE if not found. */
5955 static tree
5956 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5957 vec<tree> *ambiguousp)
5959 tree orig_id = id;
5960 tree baselink = NULL_TREE;
5961 if (identifier_p (id))
5963 cp_id_kind idk;
5964 bool nonint_cst_expression_p;
5965 const char *error_msg;
5966 id = omp_reduction_id (ERROR_MARK, id, type);
5967 tree decl = lookup_name (id);
5968 if (decl == NULL_TREE)
5969 decl = error_mark_node;
5970 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5971 &nonint_cst_expression_p, false, true, false,
5972 false, &error_msg, loc);
5973 if (idk == CP_ID_KIND_UNQUALIFIED
5974 && identifier_p (id))
5976 vec<tree, va_gc> *args = NULL;
5977 vec_safe_push (args, build_reference_type (type));
5978 id = perform_koenig_lookup (id, args, tf_none);
5981 else if (TREE_CODE (id) == SCOPE_REF)
5982 id = lookup_qualified_name (TREE_OPERAND (id, 0),
5983 omp_reduction_id (ERROR_MARK,
5984 TREE_OPERAND (id, 1),
5985 type),
5986 LOOK_want::NORMAL, false);
5987 tree fns = id;
5988 id = NULL_TREE;
5989 if (fns && is_overloaded_fn (fns))
5991 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
5993 tree fndecl = *iter;
5994 if (TREE_CODE (fndecl) == FUNCTION_DECL)
5996 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
5997 if (same_type_p (TREE_TYPE (argtype), type))
5999 id = fndecl;
6000 break;
6005 if (id && BASELINK_P (fns))
6007 if (baselinkp)
6008 *baselinkp = fns;
6009 else
6010 baselink = fns;
6014 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
6016 auto_vec<tree> ambiguous;
6017 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
6018 unsigned int ix;
6019 if (ambiguousp == NULL)
6020 ambiguousp = &ambiguous;
6021 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
6023 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
6024 baselinkp ? baselinkp : &baselink,
6025 ambiguousp);
6026 if (id == NULL_TREE)
6027 continue;
6028 if (!ambiguousp->is_empty ())
6029 ambiguousp->safe_push (id);
6030 else if (ret != NULL_TREE)
6032 ambiguousp->safe_push (ret);
6033 ambiguousp->safe_push (id);
6034 ret = NULL_TREE;
6036 else
6037 ret = id;
6039 if (ambiguousp != &ambiguous)
6040 return ret;
6041 if (!ambiguous.is_empty ())
6043 const char *str = _("candidates are:");
6044 unsigned int idx;
6045 tree udr;
6046 error_at (loc, "user defined reduction lookup is ambiguous");
6047 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
6049 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
6050 if (idx == 0)
6051 str = get_spaces (str);
6053 ret = error_mark_node;
6054 baselink = NULL_TREE;
6056 id = ret;
6058 if (id && baselink)
6059 perform_or_defer_access_check (BASELINK_BINFO (baselink),
6060 id, id, tf_warning_or_error);
6061 return id;
6064 /* Helper function for cp_parser_omp_declare_reduction_exprs
6065 and tsubst_omp_udr.
6066 Remove CLEANUP_STMT for data (omp_priv variable).
6067 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
6068 DECL_EXPR. */
6070 tree
6071 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
6073 if (TYPE_P (*tp))
6074 *walk_subtrees = 0;
6075 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
6076 *tp = CLEANUP_BODY (*tp);
6077 else if (TREE_CODE (*tp) == DECL_EXPR)
6079 tree decl = DECL_EXPR_DECL (*tp);
6080 if (!processing_template_decl
6081 && decl == (tree) data
6082 && DECL_INITIAL (decl)
6083 && DECL_INITIAL (decl) != error_mark_node)
6085 tree list = NULL_TREE;
6086 append_to_statement_list_force (*tp, &list);
6087 tree init_expr = build2 (INIT_EXPR, void_type_node,
6088 decl, DECL_INITIAL (decl));
6089 DECL_INITIAL (decl) = NULL_TREE;
6090 append_to_statement_list_force (init_expr, &list);
6091 *tp = list;
6094 return NULL_TREE;
6097 /* Data passed from cp_check_omp_declare_reduction to
6098 cp_check_omp_declare_reduction_r. */
6100 struct cp_check_omp_declare_reduction_data
6102 location_t loc;
6103 tree stmts[7];
6104 bool combiner_p;
6107 /* Helper function for cp_check_omp_declare_reduction, called via
6108 cp_walk_tree. */
6110 static tree
6111 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
6113 struct cp_check_omp_declare_reduction_data *udr_data
6114 = (struct cp_check_omp_declare_reduction_data *) data;
6115 if (SSA_VAR_P (*tp)
6116 && !DECL_ARTIFICIAL (*tp)
6117 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
6118 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
6120 location_t loc = udr_data->loc;
6121 if (udr_data->combiner_p)
6122 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
6123 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
6124 *tp);
6125 else
6126 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
6127 "to variable %qD which is not %<omp_priv%> nor "
6128 "%<omp_orig%>",
6129 *tp);
6130 return *tp;
6132 return NULL_TREE;
6135 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
6137 bool
6138 cp_check_omp_declare_reduction (tree udr)
6140 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
6141 gcc_assert (TYPE_REF_P (type));
6142 type = TREE_TYPE (type);
6143 int i;
6144 location_t loc = DECL_SOURCE_LOCATION (udr);
6146 if (type == error_mark_node)
6147 return false;
6148 if (ARITHMETIC_TYPE_P (type))
6150 static enum tree_code predef_codes[]
6151 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
6152 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
6153 for (i = 0; i < 8; i++)
6155 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
6156 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
6157 const char *n2 = IDENTIFIER_POINTER (id);
6158 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
6159 && (n1[IDENTIFIER_LENGTH (id)] == '~'
6160 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
6161 break;
6164 if (i == 8
6165 && TREE_CODE (type) != COMPLEX_EXPR)
6167 const char prefix_minmax[] = "omp declare reduction m";
6168 size_t prefix_size = sizeof (prefix_minmax) - 1;
6169 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
6170 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
6171 prefix_minmax, prefix_size) == 0
6172 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
6173 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
6174 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
6175 i = 0;
6177 if (i < 8)
6179 error_at (loc, "predeclared arithmetic type %qT in "
6180 "%<#pragma omp declare reduction%>", type);
6181 return false;
6184 else if (FUNC_OR_METHOD_TYPE_P (type)
6185 || TREE_CODE (type) == ARRAY_TYPE)
6187 error_at (loc, "function or array type %qT in "
6188 "%<#pragma omp declare reduction%>", type);
6189 return false;
6191 else if (TYPE_REF_P (type))
6193 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
6194 type);
6195 return false;
6197 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
6199 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
6200 "type %qT in %<#pragma omp declare reduction%>", type);
6201 return false;
6204 tree body = DECL_SAVED_TREE (udr);
6205 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
6206 return true;
6208 tree_stmt_iterator tsi;
6209 struct cp_check_omp_declare_reduction_data data;
6210 memset (data.stmts, 0, sizeof data.stmts);
6211 for (i = 0, tsi = tsi_start (body);
6212 i < 7 && !tsi_end_p (tsi);
6213 i++, tsi_next (&tsi))
6214 data.stmts[i] = tsi_stmt (tsi);
6215 data.loc = loc;
6216 gcc_assert (tsi_end_p (tsi));
6217 if (i >= 3)
6219 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
6220 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
6221 if (warning_suppressed_p (DECL_EXPR_DECL (data.stmts[0]) /* What warning? */))
6222 return true;
6223 data.combiner_p = true;
6224 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
6225 &data, NULL))
6226 suppress_warning (DECL_EXPR_DECL (data.stmts[0]) /* What warning? */);
6228 if (i >= 6)
6230 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
6231 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
6232 data.combiner_p = false;
6233 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
6234 &data, NULL)
6235 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
6236 cp_check_omp_declare_reduction_r, &data, NULL))
6237 suppress_warning (DECL_EXPR_DECL (data.stmts[0]) /* Wat warning? */);
6238 if (i == 7)
6239 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
6241 return true;
6244 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
6245 an inline call. But, remap
6246 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
6247 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
6249 static tree
6250 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
6251 tree decl, tree placeholder)
6253 copy_body_data id;
6254 hash_map<tree, tree> decl_map;
6256 decl_map.put (omp_decl1, placeholder);
6257 decl_map.put (omp_decl2, decl);
6258 memset (&id, 0, sizeof (id));
6259 id.src_fn = DECL_CONTEXT (omp_decl1);
6260 id.dst_fn = current_function_decl;
6261 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
6262 id.decl_map = &decl_map;
6264 id.copy_decl = copy_decl_no_change;
6265 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
6266 id.transform_new_cfg = true;
6267 id.transform_return_to_modify = false;
6268 id.eh_lp_nr = 0;
6269 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
6270 return stmt;
6273 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
6274 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
6276 static tree
6277 find_omp_placeholder_r (tree *tp, int *, void *data)
6279 if (*tp == (tree) data)
6280 return *tp;
6281 return NULL_TREE;
6284 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
6285 Return true if there is some error and the clause should be removed. */
6287 static bool
6288 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
6290 tree t = OMP_CLAUSE_DECL (c);
6291 bool predefined = false;
6292 if (TREE_CODE (t) == TREE_LIST)
6294 gcc_assert (processing_template_decl);
6295 return false;
6297 tree type = TREE_TYPE (t);
6298 if (TREE_CODE (t) == MEM_REF)
6299 type = TREE_TYPE (type);
6300 if (TYPE_REF_P (type))
6301 type = TREE_TYPE (type);
6302 if (TREE_CODE (type) == ARRAY_TYPE)
6304 tree oatype = type;
6305 gcc_assert (TREE_CODE (t) != MEM_REF);
6306 while (TREE_CODE (type) == ARRAY_TYPE)
6307 type = TREE_TYPE (type);
6308 if (!processing_template_decl)
6310 t = require_complete_type (t);
6311 if (t == error_mark_node
6312 || !complete_type_or_else (oatype, NULL_TREE))
6313 return true;
6314 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
6315 TYPE_SIZE_UNIT (type));
6316 if (integer_zerop (size))
6318 error_at (OMP_CLAUSE_LOCATION (c),
6319 "%qE in %<reduction%> clause is a zero size array",
6320 omp_clause_printable_decl (t));
6321 return true;
6323 size = size_binop (MINUS_EXPR, size, size_one_node);
6324 size = save_expr (size);
6325 tree index_type = build_index_type (size);
6326 tree atype = build_array_type (type, index_type);
6327 tree ptype = build_pointer_type (type);
6328 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6329 t = build_fold_addr_expr (t);
6330 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
6331 OMP_CLAUSE_DECL (c) = t;
6334 if (type == error_mark_node)
6335 return true;
6336 else if (ARITHMETIC_TYPE_P (type))
6337 switch (OMP_CLAUSE_REDUCTION_CODE (c))
6339 case PLUS_EXPR:
6340 case MULT_EXPR:
6341 case MINUS_EXPR:
6342 case TRUTH_ANDIF_EXPR:
6343 case TRUTH_ORIF_EXPR:
6344 predefined = true;
6345 break;
6346 case MIN_EXPR:
6347 case MAX_EXPR:
6348 if (TREE_CODE (type) == COMPLEX_TYPE)
6349 break;
6350 predefined = true;
6351 break;
6352 case BIT_AND_EXPR:
6353 case BIT_IOR_EXPR:
6354 case BIT_XOR_EXPR:
6355 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
6356 break;
6357 predefined = true;
6358 break;
6359 default:
6360 break;
6362 else if (TYPE_READONLY (type))
6364 error_at (OMP_CLAUSE_LOCATION (c),
6365 "%qE has const type for %<reduction%>",
6366 omp_clause_printable_decl (t));
6367 return true;
6369 else if (!processing_template_decl)
6371 t = require_complete_type (t);
6372 if (t == error_mark_node)
6373 return true;
6374 OMP_CLAUSE_DECL (c) = t;
6377 if (predefined)
6379 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
6380 return false;
6382 else if (processing_template_decl)
6384 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
6385 return true;
6386 return false;
6389 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
6391 type = TYPE_MAIN_VARIANT (type);
6392 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
6393 if (id == NULL_TREE)
6394 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
6395 NULL_TREE, NULL_TREE);
6396 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
6397 if (id)
6399 if (id == error_mark_node)
6400 return true;
6401 mark_used (id);
6402 tree body = DECL_SAVED_TREE (id);
6403 if (!body)
6404 return true;
6405 if (TREE_CODE (body) == STATEMENT_LIST)
6407 tree_stmt_iterator tsi;
6408 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
6409 int i;
6410 tree stmts[7];
6411 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
6412 atype = TREE_TYPE (atype);
6413 bool need_static_cast = !same_type_p (type, atype);
6414 memset (stmts, 0, sizeof stmts);
6415 for (i = 0, tsi = tsi_start (body);
6416 i < 7 && !tsi_end_p (tsi);
6417 i++, tsi_next (&tsi))
6418 stmts[i] = tsi_stmt (tsi);
6419 gcc_assert (tsi_end_p (tsi));
6421 if (i >= 3)
6423 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
6424 && TREE_CODE (stmts[1]) == DECL_EXPR);
6425 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
6426 DECL_ARTIFICIAL (placeholder) = 1;
6427 DECL_IGNORED_P (placeholder) = 1;
6428 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
6429 if (TREE_CODE (t) == MEM_REF)
6431 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
6432 type);
6433 DECL_ARTIFICIAL (decl_placeholder) = 1;
6434 DECL_IGNORED_P (decl_placeholder) = 1;
6435 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
6437 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
6438 cxx_mark_addressable (placeholder);
6439 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
6440 && (decl_placeholder
6441 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
6442 cxx_mark_addressable (decl_placeholder ? decl_placeholder
6443 : OMP_CLAUSE_DECL (c));
6444 tree omp_out = placeholder;
6445 tree omp_in = decl_placeholder ? decl_placeholder
6446 : convert_from_reference (OMP_CLAUSE_DECL (c));
6447 if (need_static_cast)
6449 tree rtype = build_reference_type (atype);
6450 omp_out = build_static_cast (input_location,
6451 rtype, omp_out,
6452 tf_warning_or_error);
6453 omp_in = build_static_cast (input_location,
6454 rtype, omp_in,
6455 tf_warning_or_error);
6456 if (omp_out == error_mark_node || omp_in == error_mark_node)
6457 return true;
6458 omp_out = convert_from_reference (omp_out);
6459 omp_in = convert_from_reference (omp_in);
6461 OMP_CLAUSE_REDUCTION_MERGE (c)
6462 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
6463 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
6465 if (i >= 6)
6467 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
6468 && TREE_CODE (stmts[4]) == DECL_EXPR);
6469 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3]))
6470 && (decl_placeholder
6471 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
6472 cxx_mark_addressable (decl_placeholder ? decl_placeholder
6473 : OMP_CLAUSE_DECL (c));
6474 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
6475 cxx_mark_addressable (placeholder);
6476 tree omp_priv = decl_placeholder ? decl_placeholder
6477 : convert_from_reference (OMP_CLAUSE_DECL (c));
6478 tree omp_orig = placeholder;
6479 if (need_static_cast)
6481 if (i == 7)
6483 error_at (OMP_CLAUSE_LOCATION (c),
6484 "user defined reduction with constructor "
6485 "initializer for base class %qT", atype);
6486 return true;
6488 tree rtype = build_reference_type (atype);
6489 omp_priv = build_static_cast (input_location,
6490 rtype, omp_priv,
6491 tf_warning_or_error);
6492 omp_orig = build_static_cast (input_location,
6493 rtype, omp_orig,
6494 tf_warning_or_error);
6495 if (omp_priv == error_mark_node
6496 || omp_orig == error_mark_node)
6497 return true;
6498 omp_priv = convert_from_reference (omp_priv);
6499 omp_orig = convert_from_reference (omp_orig);
6501 if (i == 6)
6502 *need_default_ctor = true;
6503 OMP_CLAUSE_REDUCTION_INIT (c)
6504 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
6505 DECL_EXPR_DECL (stmts[3]),
6506 omp_priv, omp_orig);
6507 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
6508 find_omp_placeholder_r, placeholder, NULL))
6509 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
6511 else if (i >= 3)
6513 if (CLASS_TYPE_P (type) && !pod_type_p (type))
6514 *need_default_ctor = true;
6515 else
6517 tree init;
6518 tree v = decl_placeholder ? decl_placeholder
6519 : convert_from_reference (t);
6520 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
6521 init = build_constructor (TREE_TYPE (v), NULL);
6522 else
6523 init = fold_convert (TREE_TYPE (v), integer_zero_node);
6524 OMP_CLAUSE_REDUCTION_INIT (c)
6525 = cp_build_init_expr (v, init);
6530 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6531 *need_dtor = true;
6532 else
6534 error_at (OMP_CLAUSE_LOCATION (c),
6535 "user defined reduction not found for %qE",
6536 omp_clause_printable_decl (t));
6537 return true;
6539 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
6540 gcc_assert (TYPE_SIZE_UNIT (type)
6541 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
6542 return false;
6545 /* Called from finish_struct_1. linear(this) or linear(this:step)
6546 clauses might not be finalized yet because the class has been incomplete
6547 when parsing #pragma omp declare simd methods. Fix those up now. */
6549 void
6550 finish_omp_declare_simd_methods (tree t)
6552 if (processing_template_decl)
6553 return;
6555 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6557 if (TREE_CODE (x) == USING_DECL
6558 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (x))
6559 continue;
6560 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
6561 if (!ods || !TREE_VALUE (ods))
6562 continue;
6563 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
6564 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6565 && integer_zerop (OMP_CLAUSE_DECL (c))
6566 && OMP_CLAUSE_LINEAR_STEP (c)
6567 && TYPE_PTR_P (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c))))
6569 tree s = OMP_CLAUSE_LINEAR_STEP (c);
6570 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
6571 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
6572 sizetype, s, TYPE_SIZE_UNIT (t));
6573 OMP_CLAUSE_LINEAR_STEP (c) = s;
6578 /* Adjust sink depend/doacross clause to take into account pointer offsets.
6580 Return TRUE if there was a problem processing the offset, and the
6581 whole clause should be removed. */
6583 static bool
6584 cp_finish_omp_clause_doacross_sink (tree sink_clause)
6586 tree t = OMP_CLAUSE_DECL (sink_clause);
6587 gcc_assert (TREE_CODE (t) == TREE_LIST);
6589 /* Make sure we don't adjust things twice for templates. */
6590 if (processing_template_decl)
6591 return false;
6593 for (; t; t = TREE_CHAIN (t))
6595 tree decl = TREE_VALUE (t);
6596 if (TYPE_PTR_P (TREE_TYPE (decl)))
6598 tree offset = TREE_PURPOSE (t);
6599 bool neg = wi::neg_p (wi::to_wide (offset));
6600 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
6601 decl = mark_rvalue_use (decl);
6602 decl = convert_from_reference (decl);
6603 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
6604 neg ? MINUS_EXPR : PLUS_EXPR,
6605 decl, offset);
6606 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
6607 MINUS_EXPR, sizetype,
6608 fold_convert (sizetype, t2),
6609 fold_convert (sizetype, decl));
6610 if (t2 == error_mark_node)
6611 return true;
6612 TREE_PURPOSE (t) = t2;
6615 return false;
6618 /* Finish OpenMP iterators ITER. Return true if they are errorneous
6619 and clauses containing them should be removed. */
6621 static bool
6622 cp_omp_finish_iterators (tree iter)
6624 bool ret = false;
6625 for (tree it = iter; it; it = TREE_CHAIN (it))
6627 tree var = TREE_VEC_ELT (it, 0);
6628 tree begin = TREE_VEC_ELT (it, 1);
6629 tree end = TREE_VEC_ELT (it, 2);
6630 tree step = TREE_VEC_ELT (it, 3);
6631 tree orig_step;
6632 tree type = TREE_TYPE (var);
6633 location_t loc = DECL_SOURCE_LOCATION (var);
6634 if (type == error_mark_node)
6636 ret = true;
6637 continue;
6639 if (type_dependent_expression_p (var))
6640 continue;
6641 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
6643 error_at (loc, "iterator %qD has neither integral nor pointer type",
6644 var);
6645 ret = true;
6646 continue;
6648 else if (TYPE_READONLY (type))
6650 error_at (loc, "iterator %qD has const qualified type", var);
6651 ret = true;
6652 continue;
6654 if (type_dependent_expression_p (begin)
6655 || type_dependent_expression_p (end)
6656 || type_dependent_expression_p (step))
6657 continue;
6658 else if (error_operand_p (step))
6660 ret = true;
6661 continue;
6663 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
6665 error_at (EXPR_LOC_OR_LOC (step, loc),
6666 "iterator step with non-integral type");
6667 ret = true;
6668 continue;
6671 begin = mark_rvalue_use (begin);
6672 end = mark_rvalue_use (end);
6673 step = mark_rvalue_use (step);
6674 begin = cp_build_c_cast (input_location, type, begin,
6675 tf_warning_or_error);
6676 end = cp_build_c_cast (input_location, type, end,
6677 tf_warning_or_error);
6678 orig_step = step;
6679 if (!processing_template_decl)
6680 step = orig_step = save_expr (step);
6681 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
6682 step = cp_build_c_cast (input_location, stype, step,
6683 tf_warning_or_error);
6684 if (POINTER_TYPE_P (type) && !processing_template_decl)
6686 begin = save_expr (begin);
6687 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
6688 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
6689 fold_convert (sizetype, step),
6690 fold_convert (sizetype, begin));
6691 step = fold_convert (ssizetype, step);
6693 if (!processing_template_decl)
6695 begin = maybe_constant_value (begin);
6696 end = maybe_constant_value (end);
6697 step = maybe_constant_value (step);
6698 orig_step = maybe_constant_value (orig_step);
6700 if (integer_zerop (step))
6702 error_at (loc, "iterator %qD has zero step", var);
6703 ret = true;
6704 continue;
6707 if (begin == error_mark_node
6708 || end == error_mark_node
6709 || step == error_mark_node
6710 || orig_step == error_mark_node)
6712 ret = true;
6713 continue;
6716 if (!processing_template_decl)
6718 begin = fold_build_cleanup_point_expr (TREE_TYPE (begin), begin);
6719 end = fold_build_cleanup_point_expr (TREE_TYPE (end), end);
6720 step = fold_build_cleanup_point_expr (TREE_TYPE (step), step);
6721 orig_step = fold_build_cleanup_point_expr (TREE_TYPE (orig_step),
6722 orig_step);
6724 hash_set<tree> pset;
6725 tree it2;
6726 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
6728 tree var2 = TREE_VEC_ELT (it2, 0);
6729 tree begin2 = TREE_VEC_ELT (it2, 1);
6730 tree end2 = TREE_VEC_ELT (it2, 2);
6731 tree step2 = TREE_VEC_ELT (it2, 3);
6732 location_t loc2 = DECL_SOURCE_LOCATION (var2);
6733 if (cp_walk_tree (&begin2, find_omp_placeholder_r, var, &pset))
6735 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
6736 "begin expression refers to outer iterator %qD", var);
6737 break;
6739 else if (cp_walk_tree (&end2, find_omp_placeholder_r, var, &pset))
6741 error_at (EXPR_LOC_OR_LOC (end2, loc2),
6742 "end expression refers to outer iterator %qD", var);
6743 break;
6745 else if (cp_walk_tree (&step2, find_omp_placeholder_r, var, &pset))
6747 error_at (EXPR_LOC_OR_LOC (step2, loc2),
6748 "step expression refers to outer iterator %qD", var);
6749 break;
6752 if (it2)
6754 ret = true;
6755 continue;
6757 TREE_VEC_ELT (it, 1) = begin;
6758 TREE_VEC_ELT (it, 2) = end;
6759 if (processing_template_decl)
6760 TREE_VEC_ELT (it, 3) = orig_step;
6761 else
6763 TREE_VEC_ELT (it, 3) = step;
6764 TREE_VEC_ELT (it, 4) = orig_step;
6767 return ret;
6770 /* Ensure that pointers are used in OpenACC attach and detach clauses.
6771 Return true if an error has been detected. */
6773 static bool
6774 cp_oacc_check_attachments (tree c)
6776 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6777 return false;
6779 /* OpenACC attach / detach clauses must be pointers. */
6780 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
6781 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
6783 tree t = OMP_CLAUSE_DECL (c);
6784 tree type;
6786 while (TREE_CODE (t) == TREE_LIST)
6787 t = TREE_CHAIN (t);
6789 type = TREE_TYPE (t);
6791 if (TREE_CODE (type) == REFERENCE_TYPE)
6792 type = TREE_TYPE (type);
6794 if (TREE_CODE (type) != POINTER_TYPE)
6796 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
6797 user_omp_clause_code_name (c, true));
6798 return true;
6802 return false;
6805 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
6806 Remove any elements from the list that are invalid. */
6808 tree
6809 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
6811 bitmap_head generic_head, firstprivate_head, lastprivate_head;
6812 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
6813 bitmap_head oacc_reduction_head, is_on_device_head;
6814 tree c, t, *pc;
6815 tree safelen = NULL_TREE;
6816 bool branch_seen = false;
6817 bool copyprivate_seen = false;
6818 bool ordered_seen = false;
6819 bool order_seen = false;
6820 bool schedule_seen = false;
6821 bool oacc_async = false;
6822 bool indir_component_ref_p = false;
6823 tree last_iterators = NULL_TREE;
6824 bool last_iterators_remove = false;
6825 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
6826 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
6827 int reduction_seen = 0;
6828 bool allocate_seen = false;
6829 tree detach_seen = NULL_TREE;
6830 bool mergeable_seen = false;
6831 bool implicit_moved = false;
6832 bool target_in_reduction_seen = false;
6834 bitmap_obstack_initialize (NULL);
6835 bitmap_initialize (&generic_head, &bitmap_default_obstack);
6836 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
6837 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
6838 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
6839 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
6840 bitmap_initialize (&map_head, &bitmap_default_obstack);
6841 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
6842 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
6843 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
6844 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
6845 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
6846 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
6848 if (ort & C_ORT_ACC)
6849 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
6850 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
6852 oacc_async = true;
6853 break;
6856 tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
6858 for (pc = &clauses, c = clauses; c ; c = *pc)
6860 bool remove = false;
6861 bool field_ok = false;
6863 /* We've reached the end of a list of expanded nodes. Reset the group
6864 start pointer. */
6865 if (c == grp_sentinel)
6866 grp_start_p = NULL;
6868 switch (OMP_CLAUSE_CODE (c))
6870 case OMP_CLAUSE_SHARED:
6871 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6872 goto check_dup_generic;
6873 case OMP_CLAUSE_PRIVATE:
6874 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6875 goto check_dup_generic;
6876 case OMP_CLAUSE_REDUCTION:
6877 if (reduction_seen == 0)
6878 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
6879 else if (reduction_seen != -2
6880 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
6881 ? -1 : 1))
6883 error_at (OMP_CLAUSE_LOCATION (c),
6884 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
6885 "on the same construct");
6886 reduction_seen = -2;
6888 /* FALLTHRU */
6889 case OMP_CLAUSE_IN_REDUCTION:
6890 case OMP_CLAUSE_TASK_REDUCTION:
6891 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6892 t = OMP_CLAUSE_DECL (c);
6893 if (TREE_CODE (t) == TREE_LIST)
6895 if (handle_omp_array_sections (c, ort))
6897 remove = true;
6898 break;
6900 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6901 && OMP_CLAUSE_REDUCTION_INSCAN (c))
6903 error_at (OMP_CLAUSE_LOCATION (c),
6904 "%<inscan%> %<reduction%> clause with array "
6905 "section");
6906 remove = true;
6907 break;
6909 if (TREE_CODE (t) == TREE_LIST)
6911 while (TREE_CODE (t) == TREE_LIST)
6912 t = TREE_CHAIN (t);
6914 else
6916 gcc_assert (TREE_CODE (t) == MEM_REF);
6917 t = TREE_OPERAND (t, 0);
6918 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
6919 t = TREE_OPERAND (t, 0);
6920 if (TREE_CODE (t) == ADDR_EXPR
6921 || INDIRECT_REF_P (t))
6922 t = TREE_OPERAND (t, 0);
6924 tree n = omp_clause_decl_field (t);
6925 if (n)
6926 t = n;
6927 goto check_dup_generic_t;
6929 if (oacc_async)
6930 cxx_mark_addressable (t);
6931 goto check_dup_generic;
6932 case OMP_CLAUSE_COPYPRIVATE:
6933 copyprivate_seen = true;
6934 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6935 goto check_dup_generic;
6936 case OMP_CLAUSE_COPYIN:
6937 goto check_dup_generic;
6938 case OMP_CLAUSE_LINEAR:
6939 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6940 t = OMP_CLAUSE_DECL (c);
6941 if (ort != C_ORT_OMP_DECLARE_SIMD
6942 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
6944 if (OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
6946 error_at (OMP_CLAUSE_LOCATION (c),
6947 "modifier should not be specified in %<linear%> "
6948 "clause on %<simd%> or %<for%> constructs when "
6949 "not using OpenMP 5.2 modifiers");
6950 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
6952 else if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_VAL)
6954 error_at (OMP_CLAUSE_LOCATION (c),
6955 "modifier other than %<val%> specified in "
6956 "%<linear%> clause on %<simd%> or %<for%> "
6957 "constructs when using OpenMP 5.2 modifiers");
6958 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
6961 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6962 && !type_dependent_expression_p (t))
6964 tree type = TREE_TYPE (t);
6965 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
6966 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
6967 && !TYPE_REF_P (type))
6969 error_at (OMP_CLAUSE_LOCATION (c),
6970 "linear clause with %qs modifier applied to "
6971 "non-reference variable with %qT type",
6972 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
6973 ? "ref" : "uval", TREE_TYPE (t));
6974 remove = true;
6975 break;
6977 if (TYPE_REF_P (type))
6978 type = TREE_TYPE (type);
6979 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
6981 if (!INTEGRAL_TYPE_P (type)
6982 && !TYPE_PTR_P (type))
6984 error_at (OMP_CLAUSE_LOCATION (c),
6985 "linear clause applied to non-integral "
6986 "non-pointer variable with %qT type",
6987 TREE_TYPE (t));
6988 remove = true;
6989 break;
6993 t = OMP_CLAUSE_LINEAR_STEP (c);
6994 if (t == NULL_TREE)
6995 t = integer_one_node;
6996 if (t == error_mark_node)
6998 remove = true;
6999 break;
7001 else if (!type_dependent_expression_p (t)
7002 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
7003 && (ort != C_ORT_OMP_DECLARE_SIMD
7004 || TREE_CODE (t) != PARM_DECL
7005 || !TYPE_REF_P (TREE_TYPE (t))
7006 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
7008 error_at (OMP_CLAUSE_LOCATION (c),
7009 "linear step expression must be integral");
7010 remove = true;
7011 break;
7013 else
7015 t = mark_rvalue_use (t);
7016 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
7018 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
7019 goto check_dup_generic;
7021 if (!processing_template_decl
7022 && (VAR_P (OMP_CLAUSE_DECL (c))
7023 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
7025 if (ort == C_ORT_OMP_DECLARE_SIMD)
7027 t = maybe_constant_value (t);
7028 if (TREE_CODE (t) != INTEGER_CST)
7030 error_at (OMP_CLAUSE_LOCATION (c),
7031 "%<linear%> clause step %qE is neither "
7032 "constant nor a parameter", t);
7033 remove = true;
7034 break;
7037 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7038 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
7039 if (TYPE_REF_P (type))
7040 type = TREE_TYPE (type);
7041 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
7043 type = build_pointer_type (type);
7044 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
7045 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
7046 d, t);
7047 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
7048 MINUS_EXPR, sizetype,
7049 fold_convert (sizetype, t),
7050 fold_convert (sizetype, d));
7051 if (t == error_mark_node)
7053 remove = true;
7054 break;
7057 else if (TYPE_PTR_P (type)
7058 /* Can't multiply the step yet if *this
7059 is still incomplete type. */
7060 && (ort != C_ORT_OMP_DECLARE_SIMD
7061 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
7062 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
7063 || DECL_NAME (OMP_CLAUSE_DECL (c))
7064 != this_identifier
7065 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
7067 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
7068 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
7069 d, t);
7070 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
7071 MINUS_EXPR, sizetype,
7072 fold_convert (sizetype, t),
7073 fold_convert (sizetype, d));
7074 if (t == error_mark_node)
7076 remove = true;
7077 break;
7080 else
7081 t = fold_convert (type, t);
7083 OMP_CLAUSE_LINEAR_STEP (c) = t;
7085 goto check_dup_generic;
7086 check_dup_generic:
7087 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7088 if (t)
7090 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
7091 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7093 else
7094 t = OMP_CLAUSE_DECL (c);
7095 check_dup_generic_t:
7096 if (t == current_class_ptr
7097 && ((ort != C_ORT_OMP_DECLARE_SIMD && ort != C_ORT_ACC)
7098 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
7099 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
7101 error_at (OMP_CLAUSE_LOCATION (c),
7102 "%<this%> allowed in OpenMP only in %<declare simd%>"
7103 " clauses");
7104 remove = true;
7105 break;
7107 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
7108 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
7110 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7111 break;
7112 if (DECL_P (t))
7113 error_at (OMP_CLAUSE_LOCATION (c),
7114 "%qD is not a variable in clause %qs", t,
7115 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7116 else
7117 error_at (OMP_CLAUSE_LOCATION (c),
7118 "%qE is not a variable in clause %qs", t,
7119 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7120 remove = true;
7122 else if ((ort == C_ORT_ACC
7123 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7124 || (ort == C_ORT_OMP
7125 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
7126 || (OMP_CLAUSE_CODE (c)
7127 == OMP_CLAUSE_USE_DEVICE_ADDR)))
7128 || (ort == C_ORT_OMP_TARGET
7129 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
7131 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
7132 && (bitmap_bit_p (&generic_head, DECL_UID (t))
7133 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
7135 error_at (OMP_CLAUSE_LOCATION (c),
7136 "%qD appears more than once in data-sharing "
7137 "clauses", t);
7138 remove = true;
7139 break;
7141 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
7142 target_in_reduction_seen = true;
7143 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
7145 error_at (OMP_CLAUSE_LOCATION (c),
7146 ort == C_ORT_ACC
7147 ? "%qD appears more than once in reduction clauses"
7148 : "%qD appears more than once in data clauses",
7150 remove = true;
7152 else
7153 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
7155 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7156 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
7157 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
7158 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
7160 error_at (OMP_CLAUSE_LOCATION (c),
7161 "%qD appears more than once in data clauses", t);
7162 remove = true;
7164 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7165 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
7166 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
7167 && bitmap_bit_p (&map_head, DECL_UID (t)))
7169 if (ort == C_ORT_ACC)
7170 error_at (OMP_CLAUSE_LOCATION (c),
7171 "%qD appears more than once in data clauses", t);
7172 else
7173 error_at (OMP_CLAUSE_LOCATION (c),
7174 "%qD appears both in data and map clauses", t);
7175 remove = true;
7177 else
7178 bitmap_set_bit (&generic_head, DECL_UID (t));
7179 if (!field_ok)
7180 break;
7181 handle_field_decl:
7182 if (!remove
7183 && TREE_CODE (t) == FIELD_DECL
7184 && t == OMP_CLAUSE_DECL (c))
7186 OMP_CLAUSE_DECL (c)
7187 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
7188 == OMP_CLAUSE_SHARED));
7189 if (OMP_CLAUSE_DECL (c) == error_mark_node)
7190 remove = true;
7192 break;
7194 case OMP_CLAUSE_FIRSTPRIVATE:
7195 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
7197 move_implicit:
7198 implicit_moved = true;
7199 /* Move firstprivate and map clauses with
7200 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
7201 clauses chain. */
7202 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
7203 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
7204 while (*pc1)
7205 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
7206 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
7208 *pc3 = *pc1;
7209 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
7210 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
7212 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
7213 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
7215 *pc2 = *pc1;
7216 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
7217 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
7219 else
7220 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
7221 *pc3 = NULL;
7222 *pc2 = cl2;
7223 *pc1 = cl1;
7224 continue;
7226 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7227 if (t)
7228 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7229 else
7230 t = OMP_CLAUSE_DECL (c);
7231 if (ort != C_ORT_ACC && t == current_class_ptr)
7233 error_at (OMP_CLAUSE_LOCATION (c),
7234 "%<this%> allowed in OpenMP only in %<declare simd%>"
7235 " clauses");
7236 remove = true;
7237 break;
7239 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
7240 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
7241 || TREE_CODE (t) != FIELD_DECL))
7243 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7244 break;
7245 if (DECL_P (t))
7246 error_at (OMP_CLAUSE_LOCATION (c),
7247 "%qD is not a variable in clause %<firstprivate%>",
7249 else
7250 error_at (OMP_CLAUSE_LOCATION (c),
7251 "%qE is not a variable in clause %<firstprivate%>",
7253 remove = true;
7255 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
7256 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
7257 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
7258 remove = true;
7259 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7260 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
7261 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
7263 error_at (OMP_CLAUSE_LOCATION (c),
7264 "%qD appears more than once in data clauses", t);
7265 remove = true;
7267 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
7269 if (ort == C_ORT_ACC)
7270 error_at (OMP_CLAUSE_LOCATION (c),
7271 "%qD appears more than once in data clauses", t);
7272 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
7273 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
7274 /* Silently drop the clause. */;
7275 else
7276 error_at (OMP_CLAUSE_LOCATION (c),
7277 "%qD appears both in data and map clauses", t);
7278 remove = true;
7280 else
7281 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
7282 goto handle_field_decl;
7284 case OMP_CLAUSE_LASTPRIVATE:
7285 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7286 if (t)
7287 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7288 else
7289 t = OMP_CLAUSE_DECL (c);
7290 if (ort != C_ORT_ACC && t == current_class_ptr)
7292 error_at (OMP_CLAUSE_LOCATION (c),
7293 "%<this%> allowed in OpenMP only in %<declare simd%>"
7294 " clauses");
7295 remove = true;
7296 break;
7298 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
7299 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
7300 || TREE_CODE (t) != FIELD_DECL))
7302 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7303 break;
7304 if (DECL_P (t))
7305 error_at (OMP_CLAUSE_LOCATION (c),
7306 "%qD is not a variable in clause %<lastprivate%>",
7308 else
7309 error_at (OMP_CLAUSE_LOCATION (c),
7310 "%qE is not a variable in clause %<lastprivate%>",
7312 remove = true;
7314 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7315 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
7317 error_at (OMP_CLAUSE_LOCATION (c),
7318 "%qD appears more than once in data clauses", t);
7319 remove = true;
7321 else
7322 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
7323 goto handle_field_decl;
7325 case OMP_CLAUSE_IF:
7326 t = OMP_CLAUSE_IF_EXPR (c);
7327 t = maybe_convert_cond (t);
7328 if (t == error_mark_node)
7329 remove = true;
7330 else if (!processing_template_decl)
7331 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7332 OMP_CLAUSE_IF_EXPR (c) = t;
7333 break;
7335 case OMP_CLAUSE_FINAL:
7336 t = OMP_CLAUSE_FINAL_EXPR (c);
7337 t = maybe_convert_cond (t);
7338 if (t == error_mark_node)
7339 remove = true;
7340 else if (!processing_template_decl)
7341 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7342 OMP_CLAUSE_FINAL_EXPR (c) = t;
7343 break;
7345 case OMP_CLAUSE_GANG:
7346 /* Operand 1 is the gang static: argument. */
7347 t = OMP_CLAUSE_OPERAND (c, 1);
7348 if (t != NULL_TREE)
7350 if (t == error_mark_node)
7351 remove = true;
7352 else if (!type_dependent_expression_p (t)
7353 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7355 error_at (OMP_CLAUSE_LOCATION (c),
7356 "%<gang%> static expression must be integral");
7357 remove = true;
7359 else
7361 t = mark_rvalue_use (t);
7362 if (!processing_template_decl)
7364 t = maybe_constant_value (t);
7365 if (TREE_CODE (t) == INTEGER_CST
7366 && tree_int_cst_sgn (t) != 1
7367 && t != integer_minus_one_node)
7369 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7370 "%<gang%> static value must be "
7371 "positive");
7372 t = integer_one_node;
7374 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7377 OMP_CLAUSE_OPERAND (c, 1) = t;
7379 /* Check operand 0, the num argument. */
7380 /* FALLTHRU */
7382 case OMP_CLAUSE_WORKER:
7383 case OMP_CLAUSE_VECTOR:
7384 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
7385 break;
7386 /* FALLTHRU */
7388 case OMP_CLAUSE_NUM_TASKS:
7389 case OMP_CLAUSE_NUM_TEAMS:
7390 case OMP_CLAUSE_NUM_THREADS:
7391 case OMP_CLAUSE_NUM_GANGS:
7392 case OMP_CLAUSE_NUM_WORKERS:
7393 case OMP_CLAUSE_VECTOR_LENGTH:
7394 t = OMP_CLAUSE_OPERAND (c, 0);
7395 if (t == error_mark_node)
7396 remove = true;
7397 else if (!type_dependent_expression_p (t)
7398 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7400 switch (OMP_CLAUSE_CODE (c))
7402 case OMP_CLAUSE_GANG:
7403 error_at (OMP_CLAUSE_LOCATION (c),
7404 "%<gang%> num expression must be integral"); break;
7405 case OMP_CLAUSE_VECTOR:
7406 error_at (OMP_CLAUSE_LOCATION (c),
7407 "%<vector%> length expression must be integral");
7408 break;
7409 case OMP_CLAUSE_WORKER:
7410 error_at (OMP_CLAUSE_LOCATION (c),
7411 "%<worker%> num expression must be integral");
7412 break;
7413 default:
7414 error_at (OMP_CLAUSE_LOCATION (c),
7415 "%qs expression must be integral",
7416 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7418 remove = true;
7420 else
7422 t = mark_rvalue_use (t);
7423 if (!processing_template_decl)
7425 t = maybe_constant_value (t);
7426 if (TREE_CODE (t) == INTEGER_CST
7427 && tree_int_cst_sgn (t) != 1)
7429 switch (OMP_CLAUSE_CODE (c))
7431 case OMP_CLAUSE_GANG:
7432 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7433 "%<gang%> num value must be positive");
7434 break;
7435 case OMP_CLAUSE_VECTOR:
7436 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7437 "%<vector%> length value must be "
7438 "positive");
7439 break;
7440 case OMP_CLAUSE_WORKER:
7441 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7442 "%<worker%> num value must be "
7443 "positive");
7444 break;
7445 default:
7446 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7447 "%qs value must be positive",
7448 omp_clause_code_name
7449 [OMP_CLAUSE_CODE (c)]);
7451 t = integer_one_node;
7453 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7455 OMP_CLAUSE_OPERAND (c, 0) = t;
7457 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
7458 && OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
7459 && !remove)
7461 t = OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c);
7462 if (t == error_mark_node)
7463 remove = true;
7464 else if (!type_dependent_expression_p (t)
7465 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7467 error_at (OMP_CLAUSE_LOCATION (c),
7468 "%qs expression must be integral",
7469 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7470 remove = true;
7472 else
7474 t = mark_rvalue_use (t);
7475 if (!processing_template_decl)
7477 t = maybe_constant_value (t);
7478 if (TREE_CODE (t) == INTEGER_CST
7479 && tree_int_cst_sgn (t) != 1)
7481 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7482 "%qs value must be positive",
7483 omp_clause_code_name
7484 [OMP_CLAUSE_CODE (c)]);
7485 t = NULL_TREE;
7487 else
7488 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7489 tree upper = OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c);
7490 if (t
7491 && TREE_CODE (t) == INTEGER_CST
7492 && TREE_CODE (upper) == INTEGER_CST
7493 && tree_int_cst_lt (upper, t))
7495 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7496 "%<num_teams%> lower bound %qE bigger "
7497 "than upper bound %qE", t, upper);
7498 t = NULL_TREE;
7501 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = t;
7504 break;
7506 case OMP_CLAUSE_SCHEDULE:
7507 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
7508 if (t == NULL)
7510 else if (t == error_mark_node)
7511 remove = true;
7512 else if (!type_dependent_expression_p (t)
7513 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7515 error_at (OMP_CLAUSE_LOCATION (c),
7516 "schedule chunk size expression must be integral");
7517 remove = true;
7519 else
7521 t = mark_rvalue_use (t);
7522 if (!processing_template_decl)
7524 t = maybe_constant_value (t);
7525 if (TREE_CODE (t) == INTEGER_CST
7526 && tree_int_cst_sgn (t) != 1)
7528 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7529 "chunk size value must be positive");
7530 t = integer_one_node;
7532 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7534 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7536 if (!remove)
7537 schedule_seen = true;
7538 break;
7540 case OMP_CLAUSE_SIMDLEN:
7541 case OMP_CLAUSE_SAFELEN:
7542 t = OMP_CLAUSE_OPERAND (c, 0);
7543 if (t == error_mark_node)
7544 remove = true;
7545 else if (!type_dependent_expression_p (t)
7546 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7548 error_at (OMP_CLAUSE_LOCATION (c),
7549 "%qs length expression must be integral",
7550 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7551 remove = true;
7553 else
7555 t = mark_rvalue_use (t);
7556 if (!processing_template_decl)
7558 t = maybe_constant_value (t);
7559 if (TREE_CODE (t) != INTEGER_CST
7560 || tree_int_cst_sgn (t) != 1)
7562 error_at (OMP_CLAUSE_LOCATION (c),
7563 "%qs length expression must be positive "
7564 "constant integer expression",
7565 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7566 remove = true;
7569 OMP_CLAUSE_OPERAND (c, 0) = t;
7570 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
7571 safelen = c;
7573 break;
7575 case OMP_CLAUSE_ASYNC:
7576 t = OMP_CLAUSE_ASYNC_EXPR (c);
7577 if (t == error_mark_node)
7578 remove = true;
7579 else if (!type_dependent_expression_p (t)
7580 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7582 error_at (OMP_CLAUSE_LOCATION (c),
7583 "%<async%> expression must be integral");
7584 remove = true;
7586 else
7588 t = mark_rvalue_use (t);
7589 if (!processing_template_decl)
7590 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7591 OMP_CLAUSE_ASYNC_EXPR (c) = t;
7593 break;
7595 case OMP_CLAUSE_WAIT:
7596 t = OMP_CLAUSE_WAIT_EXPR (c);
7597 if (t == error_mark_node)
7598 remove = true;
7599 else if (!processing_template_decl)
7600 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7601 OMP_CLAUSE_WAIT_EXPR (c) = t;
7602 break;
7604 case OMP_CLAUSE_THREAD_LIMIT:
7605 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
7606 if (t == error_mark_node)
7607 remove = true;
7608 else if (!type_dependent_expression_p (t)
7609 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7611 error_at (OMP_CLAUSE_LOCATION (c),
7612 "%<thread_limit%> expression must be integral");
7613 remove = true;
7615 else
7617 t = mark_rvalue_use (t);
7618 if (!processing_template_decl)
7620 t = maybe_constant_value (t);
7621 if (TREE_CODE (t) == INTEGER_CST
7622 && tree_int_cst_sgn (t) != 1)
7624 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7625 "%<thread_limit%> value must be positive");
7626 t = integer_one_node;
7628 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7630 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
7632 break;
7634 case OMP_CLAUSE_DEVICE:
7635 t = OMP_CLAUSE_DEVICE_ID (c);
7636 if (t == error_mark_node)
7637 remove = true;
7638 else if (!type_dependent_expression_p (t)
7639 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7641 error_at (OMP_CLAUSE_LOCATION (c),
7642 "%<device%> id must be integral");
7643 remove = true;
7645 else if (OMP_CLAUSE_DEVICE_ANCESTOR (c)
7646 && TREE_CODE (t) == INTEGER_CST
7647 && !integer_onep (t))
7649 error_at (OMP_CLAUSE_LOCATION (c),
7650 "the %<device%> clause expression must evaluate to "
7651 "%<1%>");
7652 remove = true;
7654 else
7656 t = mark_rvalue_use (t);
7657 if (!processing_template_decl)
7658 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7659 OMP_CLAUSE_DEVICE_ID (c) = t;
7661 break;
7663 case OMP_CLAUSE_DIST_SCHEDULE:
7664 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
7665 if (t == NULL)
7667 else if (t == error_mark_node)
7668 remove = true;
7669 else if (!type_dependent_expression_p (t)
7670 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7672 error_at (OMP_CLAUSE_LOCATION (c),
7673 "%<dist_schedule%> chunk size expression must be "
7674 "integral");
7675 remove = true;
7677 else
7679 t = mark_rvalue_use (t);
7680 if (!processing_template_decl)
7681 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7682 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
7684 break;
7686 case OMP_CLAUSE_ALIGNED:
7687 t = OMP_CLAUSE_DECL (c);
7688 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
7690 error_at (OMP_CLAUSE_LOCATION (c),
7691 "%<this%> allowed in OpenMP only in %<declare simd%>"
7692 " clauses");
7693 remove = true;
7694 break;
7696 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7698 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7699 break;
7700 if (DECL_P (t))
7701 error_at (OMP_CLAUSE_LOCATION (c),
7702 "%qD is not a variable in %<aligned%> clause", t);
7703 else
7704 error_at (OMP_CLAUSE_LOCATION (c),
7705 "%qE is not a variable in %<aligned%> clause", t);
7706 remove = true;
7708 else if (!type_dependent_expression_p (t)
7709 && !TYPE_PTR_P (TREE_TYPE (t))
7710 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
7711 && (!TYPE_REF_P (TREE_TYPE (t))
7712 || (!INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
7713 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
7714 != ARRAY_TYPE))))
7716 error_at (OMP_CLAUSE_LOCATION (c),
7717 "%qE in %<aligned%> clause is neither a pointer nor "
7718 "an array nor a reference to pointer or array", t);
7719 remove = true;
7721 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
7723 error_at (OMP_CLAUSE_LOCATION (c),
7724 "%qD appears more than once in %<aligned%> clauses",
7726 remove = true;
7728 else
7729 bitmap_set_bit (&aligned_head, DECL_UID (t));
7730 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
7731 if (t == error_mark_node)
7732 remove = true;
7733 else if (t == NULL_TREE)
7734 break;
7735 else if (!type_dependent_expression_p (t)
7736 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7738 error_at (OMP_CLAUSE_LOCATION (c),
7739 "%<aligned%> clause alignment expression must "
7740 "be integral");
7741 remove = true;
7743 else
7745 t = mark_rvalue_use (t);
7746 if (!processing_template_decl)
7748 t = maybe_constant_value (t);
7749 if (TREE_CODE (t) != INTEGER_CST
7750 || tree_int_cst_sgn (t) != 1)
7752 error_at (OMP_CLAUSE_LOCATION (c),
7753 "%<aligned%> clause alignment expression must "
7754 "be positive constant integer expression");
7755 remove = true;
7757 else
7758 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7760 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
7762 break;
7764 case OMP_CLAUSE_NONTEMPORAL:
7765 t = OMP_CLAUSE_DECL (c);
7766 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7768 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7769 break;
7770 if (DECL_P (t))
7771 error_at (OMP_CLAUSE_LOCATION (c),
7772 "%qD is not a variable in %<nontemporal%> clause",
7774 else
7775 error_at (OMP_CLAUSE_LOCATION (c),
7776 "%qE is not a variable in %<nontemporal%> clause",
7778 remove = true;
7780 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
7782 error_at (OMP_CLAUSE_LOCATION (c),
7783 "%qD appears more than once in %<nontemporal%> "
7784 "clauses", t);
7785 remove = true;
7787 else
7788 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
7789 break;
7791 case OMP_CLAUSE_ALLOCATE:
7792 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7793 if (t)
7794 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7795 else
7796 t = OMP_CLAUSE_DECL (c);
7797 if (t == current_class_ptr)
7799 error_at (OMP_CLAUSE_LOCATION (c),
7800 "%<this%> not allowed in %<allocate%> clause");
7801 remove = true;
7802 break;
7804 if (!VAR_P (t)
7805 && TREE_CODE (t) != PARM_DECL
7806 && TREE_CODE (t) != FIELD_DECL)
7808 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7809 break;
7810 if (DECL_P (t))
7811 error_at (OMP_CLAUSE_LOCATION (c),
7812 "%qD is not a variable in %<allocate%> clause", t);
7813 else
7814 error_at (OMP_CLAUSE_LOCATION (c),
7815 "%qE is not a variable in %<allocate%> clause", t);
7816 remove = true;
7818 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
7820 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7821 "%qD appears more than once in %<allocate%> clauses",
7823 remove = true;
7825 else
7827 bitmap_set_bit (&aligned_head, DECL_UID (t));
7828 allocate_seen = true;
7830 tree allocator, align;
7831 align = OMP_CLAUSE_ALLOCATE_ALIGN (c);
7832 if (error_operand_p (align))
7834 remove = true;
7835 break;
7837 if (align)
7839 if (!type_dependent_expression_p (align)
7840 && !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7842 error_at (OMP_CLAUSE_LOCATION (c),
7843 "%<allocate%> clause %<align%> modifier "
7844 "argument needs to be positive constant "
7845 "power of two integer expression");
7846 remove = true;
7848 else
7850 align = mark_rvalue_use (align);
7851 if (!processing_template_decl)
7853 align = maybe_constant_value (align);
7854 if (TREE_CODE (align) != INTEGER_CST
7855 || !tree_fits_uhwi_p (align)
7856 || !integer_pow2p (align))
7858 error_at (OMP_CLAUSE_LOCATION (c),
7859 "%<allocate%> clause %<align%> modifier "
7860 "argument needs to be positive constant "
7861 "power of two integer expression");
7862 remove = true;
7866 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
7868 allocator = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
7869 if (error_operand_p (allocator))
7871 remove = true;
7872 break;
7874 if (allocator == NULL_TREE)
7875 goto handle_field_decl;
7876 tree allocatort;
7877 allocatort = TYPE_MAIN_VARIANT (TREE_TYPE (allocator));
7878 if (!type_dependent_expression_p (allocator)
7879 && (TREE_CODE (allocatort) != ENUMERAL_TYPE
7880 || TYPE_NAME (allocatort) == NULL_TREE
7881 || TREE_CODE (TYPE_NAME (allocatort)) != TYPE_DECL
7882 || (DECL_NAME (TYPE_NAME (allocatort))
7883 != get_identifier ("omp_allocator_handle_t"))
7884 || (TYPE_CONTEXT (allocatort)
7885 != DECL_CONTEXT (global_namespace))))
7887 error_at (OMP_CLAUSE_LOCATION (c),
7888 "%<allocate%> clause allocator expression has "
7889 "type %qT rather than %<omp_allocator_handle_t%>",
7890 TREE_TYPE (allocator));
7891 remove = true;
7892 break;
7894 else
7896 allocator = mark_rvalue_use (allocator);
7897 if (!processing_template_decl)
7898 allocator = maybe_constant_value (allocator);
7899 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
7901 goto handle_field_decl;
7903 case OMP_CLAUSE_DOACROSS:
7904 t = OMP_CLAUSE_DECL (c);
7905 if (t == NULL_TREE)
7906 break;
7907 if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
7909 if (cp_finish_omp_clause_doacross_sink (c))
7910 remove = true;
7911 break;
7913 gcc_unreachable ();
7914 case OMP_CLAUSE_DEPEND:
7915 case OMP_CLAUSE_AFFINITY:
7916 t = OMP_CLAUSE_DECL (c);
7917 if (TREE_CODE (t) == TREE_LIST
7918 && TREE_PURPOSE (t)
7919 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
7921 if (TREE_PURPOSE (t) != last_iterators)
7922 last_iterators_remove
7923 = cp_omp_finish_iterators (TREE_PURPOSE (t));
7924 last_iterators = TREE_PURPOSE (t);
7925 t = TREE_VALUE (t);
7926 if (last_iterators_remove)
7927 t = error_mark_node;
7929 else
7930 last_iterators = NULL_TREE;
7932 if (TREE_CODE (t) == TREE_LIST)
7934 if (handle_omp_array_sections (c, ort))
7935 remove = true;
7936 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
7937 && (OMP_CLAUSE_DEPEND_KIND (c)
7938 == OMP_CLAUSE_DEPEND_DEPOBJ))
7940 error_at (OMP_CLAUSE_LOCATION (c),
7941 "%<depend%> clause with %<depobj%> dependence "
7942 "type on array section");
7943 remove = true;
7945 break;
7947 if (t == error_mark_node)
7948 remove = true;
7949 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
7950 && t == ridpointers[RID_OMP_ALL_MEMORY])
7952 if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
7953 && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
7955 error_at (OMP_CLAUSE_LOCATION (c),
7956 "%<omp_all_memory%> used with %<depend%> kind "
7957 "other than %<out%> or %<inout%>");
7958 remove = true;
7960 if (processing_template_decl)
7961 break;
7963 else if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7964 break;
7965 else if (!lvalue_p (t))
7967 if (DECL_P (t))
7968 error_at (OMP_CLAUSE_LOCATION (c),
7969 "%qD is not lvalue expression nor array section "
7970 "in %qs clause", t,
7971 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7972 else
7973 error_at (OMP_CLAUSE_LOCATION (c),
7974 "%qE is not lvalue expression nor array section "
7975 "in %qs clause", t,
7976 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7977 remove = true;
7979 else if (TREE_CODE (t) == COMPONENT_REF
7980 && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
7981 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
7983 error_at (OMP_CLAUSE_LOCATION (c),
7984 "bit-field %qE in %qs clause", t,
7985 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7986 remove = true;
7988 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
7989 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
7991 if (!c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
7992 ? TREE_TYPE (TREE_TYPE (t))
7993 : TREE_TYPE (t)))
7995 error_at (OMP_CLAUSE_LOCATION (c),
7996 "%qE does not have %<omp_depend_t%> type in "
7997 "%<depend%> clause with %<depobj%> dependence "
7998 "type", t);
7999 remove = true;
8002 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
8003 && c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
8004 ? TREE_TYPE (TREE_TYPE (t))
8005 : TREE_TYPE (t)))
8007 error_at (OMP_CLAUSE_LOCATION (c),
8008 "%qE should not have %<omp_depend_t%> type in "
8009 "%<depend%> clause with dependence type other than "
8010 "%<depobj%>", t);
8011 remove = true;
8013 if (!remove)
8015 if (t == ridpointers[RID_OMP_ALL_MEMORY])
8016 t = null_pointer_node;
8017 else
8019 tree addr = cp_build_addr_expr (t, tf_warning_or_error);
8020 if (addr == error_mark_node)
8022 remove = true;
8023 break;
8025 t = cp_build_indirect_ref (OMP_CLAUSE_LOCATION (c),
8026 addr, RO_UNARY_STAR,
8027 tf_warning_or_error);
8028 if (t == error_mark_node)
8030 remove = true;
8031 break;
8034 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
8035 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
8036 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
8037 == TREE_VEC))
8038 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
8039 else
8040 OMP_CLAUSE_DECL (c) = t;
8042 break;
8043 case OMP_CLAUSE_DETACH:
8044 t = OMP_CLAUSE_DECL (c);
8045 if (detach_seen)
8047 error_at (OMP_CLAUSE_LOCATION (c),
8048 "too many %qs clauses on a task construct",
8049 "detach");
8050 remove = true;
8051 break;
8053 else if (error_operand_p (t))
8055 remove = true;
8056 break;
8058 else
8060 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
8061 if (!type_dependent_expression_p (t)
8062 && (!INTEGRAL_TYPE_P (type)
8063 || TREE_CODE (type) != ENUMERAL_TYPE
8064 || TYPE_NAME (type) == NULL_TREE
8065 || (DECL_NAME (TYPE_NAME (type))
8066 != get_identifier ("omp_event_handle_t"))))
8068 error_at (OMP_CLAUSE_LOCATION (c),
8069 "%<detach%> clause event handle "
8070 "has type %qT rather than "
8071 "%<omp_event_handle_t%>",
8072 type);
8073 remove = true;
8075 detach_seen = c;
8076 cxx_mark_addressable (t);
8078 break;
8080 case OMP_CLAUSE_MAP:
8081 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
8082 goto move_implicit;
8083 /* FALLTHRU */
8084 case OMP_CLAUSE_TO:
8085 case OMP_CLAUSE_FROM:
8086 case OMP_CLAUSE__CACHE_:
8087 t = OMP_CLAUSE_DECL (c);
8088 if (TREE_CODE (t) == TREE_LIST)
8090 grp_start_p = pc;
8091 grp_sentinel = OMP_CLAUSE_CHAIN (c);
8093 if (handle_omp_array_sections (c, ort))
8094 remove = true;
8095 else
8097 t = OMP_CLAUSE_DECL (c);
8098 if (TREE_CODE (t) != TREE_LIST
8099 && !type_dependent_expression_p (t)
8100 && !omp_mappable_type (TREE_TYPE (t)))
8102 error_at (OMP_CLAUSE_LOCATION (c),
8103 "array section does not have mappable type "
8104 "in %qs clause",
8105 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8106 if (TREE_TYPE (t) != error_mark_node
8107 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
8108 cxx_incomplete_type_inform (TREE_TYPE (t));
8109 remove = true;
8111 while (TREE_CODE (t) == ARRAY_REF)
8112 t = TREE_OPERAND (t, 0);
8113 if (TREE_CODE (t) == COMPONENT_REF
8114 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8118 t = TREE_OPERAND (t, 0);
8119 if (REFERENCE_REF_P (t))
8120 t = TREE_OPERAND (t, 0);
8121 if (TREE_CODE (t) == MEM_REF
8122 || INDIRECT_REF_P (t))
8124 t = TREE_OPERAND (t, 0);
8125 STRIP_NOPS (t);
8126 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
8127 t = TREE_OPERAND (t, 0);
8130 while (TREE_CODE (t) == COMPONENT_REF
8131 || TREE_CODE (t) == ARRAY_REF);
8133 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8134 && OMP_CLAUSE_MAP_IMPLICIT (c)
8135 && (bitmap_bit_p (&map_head, DECL_UID (t))
8136 || bitmap_bit_p (&map_field_head, DECL_UID (t))
8137 || bitmap_bit_p (&map_firstprivate_head,
8138 DECL_UID (t))))
8140 remove = true;
8141 break;
8143 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
8144 break;
8145 if (bitmap_bit_p (&map_head, DECL_UID (t)))
8147 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
8148 error_at (OMP_CLAUSE_LOCATION (c),
8149 "%qD appears more than once in motion"
8150 " clauses", t);
8151 else if (ort == C_ORT_ACC)
8152 error_at (OMP_CLAUSE_LOCATION (c),
8153 "%qD appears more than once in data"
8154 " clauses", t);
8155 else
8156 error_at (OMP_CLAUSE_LOCATION (c),
8157 "%qD appears more than once in map"
8158 " clauses", t);
8159 remove = true;
8161 else
8163 bitmap_set_bit (&map_head, DECL_UID (t));
8164 bitmap_set_bit (&map_field_head, DECL_UID (t));
8168 if (cp_oacc_check_attachments (c))
8169 remove = true;
8170 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8171 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
8172 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
8173 /* In this case, we have a single array element which is a
8174 pointer, and we already set OMP_CLAUSE_SIZE in
8175 handle_omp_array_sections above. For attach/detach clauses,
8176 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
8177 here. */
8178 OMP_CLAUSE_SIZE (c) = size_zero_node;
8179 break;
8181 if (t == error_mark_node)
8183 remove = true;
8184 break;
8186 /* OpenACC attach / detach clauses must be pointers. */
8187 if (cp_oacc_check_attachments (c))
8189 remove = true;
8190 break;
8192 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8193 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
8194 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
8195 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
8196 bias) to zero here, so it is not set erroneously to the pointer
8197 size later on in gimplify.cc. */
8198 OMP_CLAUSE_SIZE (c) = size_zero_node;
8199 if (REFERENCE_REF_P (t)
8200 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
8202 t = TREE_OPERAND (t, 0);
8203 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8204 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH)
8205 OMP_CLAUSE_DECL (c) = t;
8207 while (INDIRECT_REF_P (t)
8208 || TREE_CODE (t) == ARRAY_REF)
8210 t = TREE_OPERAND (t, 0);
8211 STRIP_NOPS (t);
8212 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
8213 t = TREE_OPERAND (t, 0);
8215 while (TREE_CODE (t) == COMPOUND_EXPR)
8217 t = TREE_OPERAND (t, 1);
8218 STRIP_NOPS (t);
8220 if (TREE_CODE (t) == COMPONENT_REF
8221 && invalid_nonstatic_memfn_p (EXPR_LOCATION (t), t,
8222 tf_warning_or_error))
8223 remove = true;
8224 indir_component_ref_p = false;
8225 if (TREE_CODE (t) == COMPONENT_REF
8226 && (INDIRECT_REF_P (TREE_OPERAND (t, 0))
8227 || TREE_CODE (TREE_OPERAND (t, 0)) == ARRAY_REF))
8229 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
8230 indir_component_ref_p = true;
8231 STRIP_NOPS (t);
8232 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
8233 t = TREE_OPERAND (t, 0);
8235 if (TREE_CODE (t) == COMPONENT_REF
8236 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
8238 if (type_dependent_expression_p (t))
8239 break;
8240 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
8241 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
8243 error_at (OMP_CLAUSE_LOCATION (c),
8244 "bit-field %qE in %qs clause",
8245 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8246 remove = true;
8248 else if (!omp_mappable_type (TREE_TYPE (t)))
8250 error_at (OMP_CLAUSE_LOCATION (c),
8251 "%qE does not have a mappable type in %qs clause",
8252 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8253 if (TREE_TYPE (t) != error_mark_node
8254 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
8255 cxx_incomplete_type_inform (TREE_TYPE (t));
8256 remove = true;
8258 while (TREE_CODE (t) == COMPONENT_REF)
8260 if (TREE_TYPE (TREE_OPERAND (t, 0))
8261 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
8262 == UNION_TYPE))
8264 error_at (OMP_CLAUSE_LOCATION (c),
8265 "%qE is a member of a union", t);
8266 remove = true;
8267 break;
8269 t = TREE_OPERAND (t, 0);
8270 if (TREE_CODE (t) == MEM_REF)
8272 if (maybe_ne (mem_ref_offset (t), 0))
8273 error_at (OMP_CLAUSE_LOCATION (c),
8274 "cannot dereference %qE in %qs clause", t,
8275 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8276 else
8277 t = TREE_OPERAND (t, 0);
8279 while (TREE_CODE (t) == MEM_REF
8280 || INDIRECT_REF_P (t)
8281 || TREE_CODE (t) == ARRAY_REF)
8283 t = TREE_OPERAND (t, 0);
8284 STRIP_NOPS (t);
8285 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
8286 t = TREE_OPERAND (t, 0);
8289 if (remove)
8290 break;
8291 if (REFERENCE_REF_P (t))
8292 t = TREE_OPERAND (t, 0);
8293 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
8295 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
8296 || (ort != C_ORT_ACC
8297 && bitmap_bit_p (&map_head, DECL_UID (t))))
8298 goto handle_map_references;
8301 if (!processing_template_decl
8302 && TREE_CODE (t) == FIELD_DECL)
8304 OMP_CLAUSE_DECL (c) = finish_non_static_data_member (t, NULL_TREE,
8305 NULL_TREE);
8306 break;
8308 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
8310 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
8311 break;
8312 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8313 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
8314 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER
8315 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH))
8316 break;
8317 if (DECL_P (t))
8318 error_at (OMP_CLAUSE_LOCATION (c),
8319 "%qD is not a variable in %qs clause", t,
8320 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8321 else
8322 error_at (OMP_CLAUSE_LOCATION (c),
8323 "%qE is not a variable in %qs clause", t,
8324 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8325 remove = true;
8327 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
8329 error_at (OMP_CLAUSE_LOCATION (c),
8330 "%qD is threadprivate variable in %qs clause", t,
8331 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8332 remove = true;
8334 else if (!processing_template_decl
8335 && !TYPE_REF_P (TREE_TYPE (t))
8336 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
8337 || (OMP_CLAUSE_MAP_KIND (c)
8338 != GOMP_MAP_FIRSTPRIVATE_POINTER))
8339 && !indir_component_ref_p
8340 && !cxx_mark_addressable (t))
8341 remove = true;
8342 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8343 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
8344 || (OMP_CLAUSE_MAP_KIND (c)
8345 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
8346 && t == OMP_CLAUSE_DECL (c)
8347 && !type_dependent_expression_p (t)
8348 && !omp_mappable_type (TYPE_REF_P (TREE_TYPE (t))
8349 ? TREE_TYPE (TREE_TYPE (t))
8350 : TREE_TYPE (t)))
8352 error_at (OMP_CLAUSE_LOCATION (c),
8353 "%qD does not have a mappable type in %qs clause", t,
8354 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8355 if (TREE_TYPE (t) != error_mark_node
8356 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
8357 cxx_incomplete_type_inform (TREE_TYPE (t));
8358 remove = true;
8360 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8361 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
8362 && !type_dependent_expression_p (t)
8363 && !INDIRECT_TYPE_P (TREE_TYPE (t)))
8365 error_at (OMP_CLAUSE_LOCATION (c),
8366 "%qD is not a pointer variable", t);
8367 remove = true;
8369 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8370 && OMP_CLAUSE_MAP_IMPLICIT (c)
8371 && (bitmap_bit_p (&map_head, DECL_UID (t))
8372 || bitmap_bit_p (&map_field_head, DECL_UID (t))
8373 || bitmap_bit_p (&map_firstprivate_head,
8374 DECL_UID (t))))
8375 remove = true;
8376 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8377 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8379 if (bitmap_bit_p (&generic_head, DECL_UID (t))
8380 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
8381 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
8383 error_at (OMP_CLAUSE_LOCATION (c),
8384 "%qD appears more than once in data clauses", t);
8385 remove = true;
8387 else if (bitmap_bit_p (&map_head, DECL_UID (t))
8388 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
8390 if (ort == C_ORT_ACC)
8391 error_at (OMP_CLAUSE_LOCATION (c),
8392 "%qD appears more than once in data clauses", t);
8393 else
8394 error_at (OMP_CLAUSE_LOCATION (c),
8395 "%qD appears both in data and map clauses", t);
8396 remove = true;
8398 else
8399 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
8401 else if (bitmap_bit_p (&map_head, DECL_UID (t))
8402 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
8404 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
8405 error_at (OMP_CLAUSE_LOCATION (c),
8406 "%qD appears more than once in motion clauses", t);
8407 else if (ort == C_ORT_ACC)
8408 error_at (OMP_CLAUSE_LOCATION (c),
8409 "%qD appears more than once in data clauses", t);
8410 else
8411 error_at (OMP_CLAUSE_LOCATION (c),
8412 "%qD appears more than once in map clauses", t);
8413 remove = true;
8415 else if (ort == C_ORT_ACC
8416 && bitmap_bit_p (&generic_head, DECL_UID (t)))
8418 error_at (OMP_CLAUSE_LOCATION (c),
8419 "%qD appears more than once in data clauses", t);
8420 remove = true;
8422 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
8423 || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
8425 if (ort == C_ORT_ACC)
8426 error_at (OMP_CLAUSE_LOCATION (c),
8427 "%qD appears more than once in data clauses", t);
8428 else
8429 error_at (OMP_CLAUSE_LOCATION (c),
8430 "%qD appears both in data and map clauses", t);
8431 remove = true;
8433 else
8435 bitmap_set_bit (&map_head, DECL_UID (t));
8437 tree decl = OMP_CLAUSE_DECL (c);
8438 if (t != decl
8439 && (TREE_CODE (decl) == COMPONENT_REF
8440 || (INDIRECT_REF_P (decl)
8441 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
8442 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (decl, 0))))))
8443 bitmap_set_bit (&map_field_head, DECL_UID (t));
8445 handle_map_references:
8446 if (!remove
8447 && !processing_template_decl
8448 && ort != C_ORT_DECLARE_SIMD
8449 && TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c))))
8451 t = OMP_CLAUSE_DECL (c);
8452 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
8454 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
8455 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8456 OMP_CLAUSE_SIZE (c)
8457 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
8459 else if (OMP_CLAUSE_MAP_KIND (c)
8460 != GOMP_MAP_FIRSTPRIVATE_POINTER
8461 && (OMP_CLAUSE_MAP_KIND (c)
8462 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
8463 && (OMP_CLAUSE_MAP_KIND (c)
8464 != GOMP_MAP_ALWAYS_POINTER)
8465 && (OMP_CLAUSE_MAP_KIND (c)
8466 != GOMP_MAP_ATTACH_DETACH))
8468 grp_start_p = pc;
8469 grp_sentinel = OMP_CLAUSE_CHAIN (c);
8471 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8472 OMP_CLAUSE_MAP);
8473 if (TREE_CODE (t) == COMPONENT_REF)
8474 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
8475 else
8476 OMP_CLAUSE_SET_MAP_KIND (c2,
8477 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
8478 OMP_CLAUSE_DECL (c2) = t;
8479 OMP_CLAUSE_SIZE (c2) = size_zero_node;
8480 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
8481 OMP_CLAUSE_CHAIN (c) = c2;
8482 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
8483 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8484 OMP_CLAUSE_SIZE (c)
8485 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
8486 c = c2;
8489 break;
8491 case OMP_CLAUSE_ENTER:
8492 case OMP_CLAUSE_LINK:
8493 t = OMP_CLAUSE_DECL (c);
8494 const char *cname;
8495 cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
8496 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
8497 && OMP_CLAUSE_ENTER_TO (c))
8498 cname = "to";
8499 if (TREE_CODE (t) == FUNCTION_DECL
8500 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
8502 else if (!VAR_P (t))
8504 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
8506 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
8507 error_at (OMP_CLAUSE_LOCATION (c),
8508 "template %qE in clause %qs", t, cname);
8509 else if (really_overloaded_fn (t))
8510 error_at (OMP_CLAUSE_LOCATION (c),
8511 "overloaded function name %qE in clause %qs", t,
8512 cname);
8513 else
8514 error_at (OMP_CLAUSE_LOCATION (c),
8515 "%qE is neither a variable nor a function name "
8516 "in clause %qs", t, cname);
8518 else
8519 error_at (OMP_CLAUSE_LOCATION (c),
8520 "%qE is not a variable in clause %qs", t, cname);
8521 remove = true;
8523 else if (DECL_THREAD_LOCAL_P (t))
8525 error_at (OMP_CLAUSE_LOCATION (c),
8526 "%qD is threadprivate variable in %qs clause", t,
8527 cname);
8528 remove = true;
8530 else if (!omp_mappable_type (TREE_TYPE (t)))
8532 error_at (OMP_CLAUSE_LOCATION (c),
8533 "%qD does not have a mappable type in %qs clause", t,
8534 cname);
8535 if (TREE_TYPE (t) != error_mark_node
8536 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
8537 cxx_incomplete_type_inform (TREE_TYPE (t));
8538 remove = true;
8540 if (remove)
8541 break;
8542 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
8544 error_at (OMP_CLAUSE_LOCATION (c),
8545 "%qE appears more than once on the same "
8546 "%<declare target%> directive", t);
8547 remove = true;
8549 else
8550 bitmap_set_bit (&generic_head, DECL_UID (t));
8551 break;
8553 case OMP_CLAUSE_UNIFORM:
8554 t = OMP_CLAUSE_DECL (c);
8555 if (TREE_CODE (t) != PARM_DECL)
8557 if (processing_template_decl)
8558 break;
8559 if (DECL_P (t))
8560 error_at (OMP_CLAUSE_LOCATION (c),
8561 "%qD is not an argument in %<uniform%> clause", t);
8562 else
8563 error_at (OMP_CLAUSE_LOCATION (c),
8564 "%qE is not an argument in %<uniform%> clause", t);
8565 remove = true;
8566 break;
8568 /* map_head bitmap is used as uniform_head if declare_simd. */
8569 bitmap_set_bit (&map_head, DECL_UID (t));
8570 goto check_dup_generic;
8572 case OMP_CLAUSE_GRAINSIZE:
8573 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
8574 if (t == error_mark_node)
8575 remove = true;
8576 else if (!type_dependent_expression_p (t)
8577 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8579 error_at (OMP_CLAUSE_LOCATION (c),
8580 "%<grainsize%> expression must be integral");
8581 remove = true;
8583 else
8585 t = mark_rvalue_use (t);
8586 if (!processing_template_decl)
8588 t = maybe_constant_value (t);
8589 if (TREE_CODE (t) == INTEGER_CST
8590 && tree_int_cst_sgn (t) != 1)
8592 warning_at (OMP_CLAUSE_LOCATION (c), 0,
8593 "%<grainsize%> value must be positive");
8594 t = integer_one_node;
8596 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8598 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
8600 break;
8602 case OMP_CLAUSE_PRIORITY:
8603 t = OMP_CLAUSE_PRIORITY_EXPR (c);
8604 if (t == error_mark_node)
8605 remove = true;
8606 else if (!type_dependent_expression_p (t)
8607 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8609 error_at (OMP_CLAUSE_LOCATION (c),
8610 "%<priority%> expression must be integral");
8611 remove = true;
8613 else
8615 t = mark_rvalue_use (t);
8616 if (!processing_template_decl)
8618 t = maybe_constant_value (t);
8619 if (TREE_CODE (t) == INTEGER_CST
8620 && tree_int_cst_sgn (t) == -1)
8622 warning_at (OMP_CLAUSE_LOCATION (c), 0,
8623 "%<priority%> value must be non-negative");
8624 t = integer_one_node;
8626 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8628 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
8630 break;
8632 case OMP_CLAUSE_HINT:
8633 t = OMP_CLAUSE_HINT_EXPR (c);
8634 if (t == error_mark_node)
8635 remove = true;
8636 else if (!type_dependent_expression_p (t)
8637 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8639 error_at (OMP_CLAUSE_LOCATION (c),
8640 "%<hint%> expression must be integral");
8641 remove = true;
8643 else
8645 t = mark_rvalue_use (t);
8646 if (!processing_template_decl)
8648 t = maybe_constant_value (t);
8649 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8650 if (TREE_CODE (t) != INTEGER_CST)
8652 error_at (OMP_CLAUSE_LOCATION (c),
8653 "%<hint%> expression must be constant integer "
8654 "expression");
8655 remove = true;
8658 OMP_CLAUSE_HINT_EXPR (c) = t;
8660 break;
8662 case OMP_CLAUSE_FILTER:
8663 t = OMP_CLAUSE_FILTER_EXPR (c);
8664 if (t == error_mark_node)
8665 remove = true;
8666 else if (!type_dependent_expression_p (t)
8667 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8669 error_at (OMP_CLAUSE_LOCATION (c),
8670 "%<filter%> expression must be integral");
8671 remove = true;
8673 else
8675 t = mark_rvalue_use (t);
8676 if (!processing_template_decl)
8678 t = maybe_constant_value (t);
8679 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8681 OMP_CLAUSE_FILTER_EXPR (c) = t;
8683 break;
8685 case OMP_CLAUSE_IS_DEVICE_PTR:
8686 case OMP_CLAUSE_USE_DEVICE_PTR:
8687 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
8688 t = OMP_CLAUSE_DECL (c);
8689 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
8690 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
8691 if (!type_dependent_expression_p (t))
8693 tree type = TREE_TYPE (t);
8694 if (!TYPE_PTR_P (type)
8695 && (!TYPE_REF_P (type) || !TYPE_PTR_P (TREE_TYPE (type))))
8697 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
8698 && ort == C_ORT_OMP)
8700 error_at (OMP_CLAUSE_LOCATION (c),
8701 "%qs variable is neither a pointer "
8702 "nor reference to pointer",
8703 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8704 remove = true;
8706 else if (TREE_CODE (type) != ARRAY_TYPE
8707 && (!TYPE_REF_P (type)
8708 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8710 error_at (OMP_CLAUSE_LOCATION (c),
8711 "%qs variable is neither a pointer, nor an "
8712 "array nor reference to pointer or array",
8713 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8714 remove = true;
8718 goto check_dup_generic;
8720 case OMP_CLAUSE_HAS_DEVICE_ADDR:
8721 t = OMP_CLAUSE_DECL (c);
8722 if (TREE_CODE (t) == TREE_LIST)
8724 if (handle_omp_array_sections (c, ort))
8725 remove = true;
8726 else
8728 t = OMP_CLAUSE_DECL (c);
8729 while (TREE_CODE (t) == TREE_LIST)
8730 t = TREE_CHAIN (t);
8731 while (INDIRECT_REF_P (t)
8732 || TREE_CODE (t) == ARRAY_REF)
8733 t = TREE_OPERAND (t, 0);
8736 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
8738 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
8739 if (!processing_template_decl
8740 && !cxx_mark_addressable (t))
8741 remove = true;
8743 goto check_dup_generic_t;
8745 case OMP_CLAUSE_USE_DEVICE_ADDR:
8746 field_ok = true;
8747 t = OMP_CLAUSE_DECL (c);
8748 if (!processing_template_decl
8749 && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
8750 && !TYPE_REF_P (TREE_TYPE (t))
8751 && !cxx_mark_addressable (t))
8752 remove = true;
8753 goto check_dup_generic;
8755 case OMP_CLAUSE_NOWAIT:
8756 case OMP_CLAUSE_DEFAULT:
8757 case OMP_CLAUSE_UNTIED:
8758 case OMP_CLAUSE_COLLAPSE:
8759 case OMP_CLAUSE_PARALLEL:
8760 case OMP_CLAUSE_FOR:
8761 case OMP_CLAUSE_SECTIONS:
8762 case OMP_CLAUSE_TASKGROUP:
8763 case OMP_CLAUSE_PROC_BIND:
8764 case OMP_CLAUSE_DEVICE_TYPE:
8765 case OMP_CLAUSE_NOGROUP:
8766 case OMP_CLAUSE_THREADS:
8767 case OMP_CLAUSE_SIMD:
8768 case OMP_CLAUSE_DEFAULTMAP:
8769 case OMP_CLAUSE_BIND:
8770 case OMP_CLAUSE_AUTO:
8771 case OMP_CLAUSE_INDEPENDENT:
8772 case OMP_CLAUSE_SEQ:
8773 case OMP_CLAUSE_IF_PRESENT:
8774 case OMP_CLAUSE_FINALIZE:
8775 case OMP_CLAUSE_NOHOST:
8776 break;
8778 case OMP_CLAUSE_MERGEABLE:
8779 mergeable_seen = true;
8780 break;
8782 case OMP_CLAUSE_TILE:
8783 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
8784 list = TREE_CHAIN (list))
8786 t = TREE_VALUE (list);
8788 if (t == error_mark_node)
8789 remove = true;
8790 else if (!type_dependent_expression_p (t)
8791 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8793 error_at (OMP_CLAUSE_LOCATION (c),
8794 "%<tile%> argument needs integral type");
8795 remove = true;
8797 else
8799 t = mark_rvalue_use (t);
8800 if (!processing_template_decl)
8802 /* Zero is used to indicate '*', we permit you
8803 to get there via an ICE of value zero. */
8804 t = maybe_constant_value (t);
8805 if (!tree_fits_shwi_p (t)
8806 || tree_to_shwi (t) < 0)
8808 error_at (OMP_CLAUSE_LOCATION (c),
8809 "%<tile%> argument needs positive "
8810 "integral constant");
8811 remove = true;
8813 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8817 /* Update list item. */
8818 TREE_VALUE (list) = t;
8820 break;
8822 case OMP_CLAUSE_ORDERED:
8823 ordered_seen = true;
8824 break;
8826 case OMP_CLAUSE_ORDER:
8827 if (order_seen)
8828 remove = true;
8829 else
8830 order_seen = true;
8831 break;
8833 case OMP_CLAUSE_INBRANCH:
8834 case OMP_CLAUSE_NOTINBRANCH:
8835 if (branch_seen)
8837 error_at (OMP_CLAUSE_LOCATION (c),
8838 "%<inbranch%> clause is incompatible with "
8839 "%<notinbranch%>");
8840 remove = true;
8842 branch_seen = true;
8843 break;
8845 case OMP_CLAUSE_INCLUSIVE:
8846 case OMP_CLAUSE_EXCLUSIVE:
8847 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
8848 if (!t)
8849 t = OMP_CLAUSE_DECL (c);
8850 if (t == current_class_ptr)
8852 error_at (OMP_CLAUSE_LOCATION (c),
8853 "%<this%> allowed in OpenMP only in %<declare simd%>"
8854 " clauses");
8855 remove = true;
8856 break;
8858 if (!VAR_P (t)
8859 && TREE_CODE (t) != PARM_DECL
8860 && TREE_CODE (t) != FIELD_DECL)
8862 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
8863 break;
8864 if (DECL_P (t))
8865 error_at (OMP_CLAUSE_LOCATION (c),
8866 "%qD is not a variable in clause %qs", t,
8867 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8868 else
8869 error_at (OMP_CLAUSE_LOCATION (c),
8870 "%qE is not a variable in clause %qs", t,
8871 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8872 remove = true;
8874 break;
8876 default:
8877 gcc_unreachable ();
8880 if (remove)
8882 if (grp_start_p)
8884 /* If we found a clause to remove, we want to remove the whole
8885 expanded group, otherwise gimplify can get confused. */
8886 *grp_start_p = grp_sentinel;
8887 pc = grp_start_p;
8888 grp_start_p = NULL;
8890 else
8891 *pc = OMP_CLAUSE_CHAIN (c);
8893 else
8894 pc = &OMP_CLAUSE_CHAIN (c);
8897 if (reduction_seen < 0 && (ordered_seen || schedule_seen))
8898 reduction_seen = -2;
8900 for (pc = &clauses, c = clauses; c ; c = *pc)
8902 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
8903 bool remove = false;
8904 bool need_complete_type = false;
8905 bool need_default_ctor = false;
8906 bool need_copy_ctor = false;
8907 bool need_copy_assignment = false;
8908 bool need_implicitly_determined = false;
8909 bool need_dtor = false;
8910 tree type, inner_type;
8912 switch (c_kind)
8914 case OMP_CLAUSE_SHARED:
8915 need_implicitly_determined = true;
8916 break;
8917 case OMP_CLAUSE_PRIVATE:
8918 need_complete_type = true;
8919 need_default_ctor = true;
8920 need_dtor = true;
8921 need_implicitly_determined = true;
8922 break;
8923 case OMP_CLAUSE_FIRSTPRIVATE:
8924 need_complete_type = true;
8925 need_copy_ctor = true;
8926 need_dtor = true;
8927 need_implicitly_determined = true;
8928 break;
8929 case OMP_CLAUSE_LASTPRIVATE:
8930 need_complete_type = true;
8931 need_copy_assignment = true;
8932 need_implicitly_determined = true;
8933 break;
8934 case OMP_CLAUSE_REDUCTION:
8935 if (reduction_seen == -2)
8936 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
8937 if (OMP_CLAUSE_REDUCTION_INSCAN (c))
8938 need_copy_assignment = true;
8939 need_implicitly_determined = true;
8940 break;
8941 case OMP_CLAUSE_IN_REDUCTION:
8942 case OMP_CLAUSE_TASK_REDUCTION:
8943 case OMP_CLAUSE_INCLUSIVE:
8944 case OMP_CLAUSE_EXCLUSIVE:
8945 need_implicitly_determined = true;
8946 break;
8947 case OMP_CLAUSE_LINEAR:
8948 if (ort != C_ORT_OMP_DECLARE_SIMD)
8949 need_implicitly_determined = true;
8950 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
8951 && !bitmap_bit_p (&map_head,
8952 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
8954 error_at (OMP_CLAUSE_LOCATION (c),
8955 "%<linear%> clause step is a parameter %qD not "
8956 "specified in %<uniform%> clause",
8957 OMP_CLAUSE_LINEAR_STEP (c));
8958 *pc = OMP_CLAUSE_CHAIN (c);
8959 continue;
8961 break;
8962 case OMP_CLAUSE_COPYPRIVATE:
8963 need_copy_assignment = true;
8964 break;
8965 case OMP_CLAUSE_COPYIN:
8966 need_copy_assignment = true;
8967 break;
8968 case OMP_CLAUSE_SIMDLEN:
8969 if (safelen
8970 && !processing_template_decl
8971 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
8972 OMP_CLAUSE_SIMDLEN_EXPR (c)))
8974 error_at (OMP_CLAUSE_LOCATION (c),
8975 "%<simdlen%> clause value is bigger than "
8976 "%<safelen%> clause value");
8977 OMP_CLAUSE_SIMDLEN_EXPR (c)
8978 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
8980 pc = &OMP_CLAUSE_CHAIN (c);
8981 continue;
8982 case OMP_CLAUSE_SCHEDULE:
8983 if (ordered_seen
8984 && (OMP_CLAUSE_SCHEDULE_KIND (c)
8985 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
8987 error_at (OMP_CLAUSE_LOCATION (c),
8988 "%<nonmonotonic%> schedule modifier specified "
8989 "together with %<ordered%> clause");
8990 OMP_CLAUSE_SCHEDULE_KIND (c)
8991 = (enum omp_clause_schedule_kind)
8992 (OMP_CLAUSE_SCHEDULE_KIND (c)
8993 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
8995 if (reduction_seen == -2)
8996 error_at (OMP_CLAUSE_LOCATION (c),
8997 "%qs clause specified together with %<inscan%> "
8998 "%<reduction%> clause", "schedule");
8999 pc = &OMP_CLAUSE_CHAIN (c);
9000 continue;
9001 case OMP_CLAUSE_NOGROUP:
9002 if (reduction_seen)
9004 error_at (OMP_CLAUSE_LOCATION (c),
9005 "%<nogroup%> clause must not be used together with "
9006 "%<reduction%> clause");
9007 *pc = OMP_CLAUSE_CHAIN (c);
9008 continue;
9010 pc = &OMP_CLAUSE_CHAIN (c);
9011 continue;
9012 case OMP_CLAUSE_ORDERED:
9013 if (reduction_seen == -2)
9014 error_at (OMP_CLAUSE_LOCATION (c),
9015 "%qs clause specified together with %<inscan%> "
9016 "%<reduction%> clause", "ordered");
9017 pc = &OMP_CLAUSE_CHAIN (c);
9018 continue;
9019 case OMP_CLAUSE_ORDER:
9020 if (ordered_seen)
9022 error_at (OMP_CLAUSE_LOCATION (c),
9023 "%<order%> clause must not be used together "
9024 "with %<ordered%>");
9025 *pc = OMP_CLAUSE_CHAIN (c);
9026 continue;
9028 pc = &OMP_CLAUSE_CHAIN (c);
9029 continue;
9030 case OMP_CLAUSE_DETACH:
9031 if (mergeable_seen)
9033 error_at (OMP_CLAUSE_LOCATION (c),
9034 "%<detach%> clause must not be used together with "
9035 "%<mergeable%> clause");
9036 *pc = OMP_CLAUSE_CHAIN (c);
9037 continue;
9039 pc = &OMP_CLAUSE_CHAIN (c);
9040 continue;
9041 case OMP_CLAUSE_MAP:
9042 if (target_in_reduction_seen && !processing_template_decl)
9044 t = OMP_CLAUSE_DECL (c);
9045 while (handled_component_p (t)
9046 || INDIRECT_REF_P (t)
9047 || TREE_CODE (t) == ADDR_EXPR
9048 || TREE_CODE (t) == MEM_REF
9049 || TREE_CODE (t) == NON_LVALUE_EXPR)
9050 t = TREE_OPERAND (t, 0);
9051 if (DECL_P (t)
9052 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
9053 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
9055 pc = &OMP_CLAUSE_CHAIN (c);
9056 continue;
9057 case OMP_CLAUSE_NOWAIT:
9058 if (copyprivate_seen)
9060 error_at (OMP_CLAUSE_LOCATION (c),
9061 "%<nowait%> clause must not be used together "
9062 "with %<copyprivate%>");
9063 *pc = OMP_CLAUSE_CHAIN (c);
9064 continue;
9066 /* FALLTHRU */
9067 default:
9068 pc = &OMP_CLAUSE_CHAIN (c);
9069 continue;
9072 t = OMP_CLAUSE_DECL (c);
9073 switch (c_kind)
9075 case OMP_CLAUSE_LASTPRIVATE:
9076 if (DECL_P (t)
9077 && !bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
9079 need_default_ctor = true;
9080 need_dtor = true;
9082 break;
9084 case OMP_CLAUSE_REDUCTION:
9085 case OMP_CLAUSE_IN_REDUCTION:
9086 case OMP_CLAUSE_TASK_REDUCTION:
9087 if (allocate_seen)
9089 if (TREE_CODE (t) == MEM_REF)
9091 t = TREE_OPERAND (t, 0);
9092 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
9093 t = TREE_OPERAND (t, 0);
9094 if (TREE_CODE (t) == ADDR_EXPR
9095 || INDIRECT_REF_P (t))
9096 t = TREE_OPERAND (t, 0);
9097 if (DECL_P (t))
9098 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9100 else if (TREE_CODE (t) == TREE_LIST)
9102 while (TREE_CODE (t) == TREE_LIST)
9103 t = TREE_CHAIN (t);
9104 if (DECL_P (t))
9105 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9106 t = OMP_CLAUSE_DECL (c);
9108 else if (DECL_P (t))
9109 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9110 t = OMP_CLAUSE_DECL (c);
9112 if (processing_template_decl
9113 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
9114 break;
9115 if (finish_omp_reduction_clause (c, &need_default_ctor,
9116 &need_dtor))
9117 remove = true;
9118 else
9119 t = OMP_CLAUSE_DECL (c);
9120 break;
9122 case OMP_CLAUSE_COPYIN:
9123 if (processing_template_decl
9124 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
9125 break;
9126 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
9128 error_at (OMP_CLAUSE_LOCATION (c),
9129 "%qE must be %<threadprivate%> for %<copyin%>", t);
9130 remove = true;
9132 break;
9134 default:
9135 break;
9138 if (processing_template_decl
9139 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
9141 pc = &OMP_CLAUSE_CHAIN (c);
9142 continue;
9145 if (need_complete_type || need_copy_assignment)
9147 t = require_complete_type (t);
9148 if (t == error_mark_node)
9149 remove = true;
9150 else if (!processing_template_decl
9151 && TYPE_REF_P (TREE_TYPE (t))
9152 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
9153 remove = true;
9155 if (need_implicitly_determined)
9157 const char *share_name = NULL;
9159 if (allocate_seen
9160 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
9161 && DECL_P (t))
9162 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9164 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
9165 share_name = "threadprivate";
9166 else switch (cxx_omp_predetermined_sharing_1 (t))
9168 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
9169 break;
9170 case OMP_CLAUSE_DEFAULT_SHARED:
9171 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9172 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
9173 && c_omp_predefined_variable (t))
9174 /* The __func__ variable and similar function-local predefined
9175 variables may be listed in a shared or firstprivate
9176 clause. */
9177 break;
9178 if (VAR_P (t)
9179 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
9180 && TREE_STATIC (t)
9181 && cxx_omp_const_qual_no_mutable (t))
9183 tree ctx = CP_DECL_CONTEXT (t);
9184 /* const qualified static data members without mutable
9185 member may be specified in firstprivate clause. */
9186 if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
9187 break;
9189 share_name = "shared";
9190 break;
9191 case OMP_CLAUSE_DEFAULT_PRIVATE:
9192 share_name = "private";
9193 break;
9194 default:
9195 gcc_unreachable ();
9197 if (share_name)
9199 error_at (OMP_CLAUSE_LOCATION (c),
9200 "%qE is predetermined %qs for %qs",
9201 omp_clause_printable_decl (t), share_name,
9202 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
9203 remove = true;
9205 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
9206 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE
9207 && cxx_omp_const_qual_no_mutable (t))
9209 error_at (OMP_CLAUSE_LOCATION (c),
9210 "%<const%> qualified %qE without %<mutable%> member "
9211 "may appear only in %<shared%> or %<firstprivate%> "
9212 "clauses", omp_clause_printable_decl (t));
9213 remove = true;
9217 if (detach_seen
9218 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9219 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
9220 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
9221 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
9222 && OMP_CLAUSE_DECL (c) == OMP_CLAUSE_DECL (detach_seen))
9224 error_at (OMP_CLAUSE_LOCATION (c),
9225 "the event handle of a %<detach%> clause "
9226 "should not be in a data-sharing clause");
9227 remove = true;
9230 /* We're interested in the base element, not arrays. */
9231 inner_type = type = TREE_TYPE (t);
9232 if ((need_complete_type
9233 || need_copy_assignment
9234 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
9235 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
9236 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
9237 && TYPE_REF_P (inner_type))
9238 inner_type = TREE_TYPE (inner_type);
9239 while (TREE_CODE (inner_type) == ARRAY_TYPE)
9240 inner_type = TREE_TYPE (inner_type);
9242 /* Check for special function availability by building a call to one.
9243 Save the results, because later we won't be in the right context
9244 for making these queries. */
9245 if (CLASS_TYPE_P (inner_type)
9246 && COMPLETE_TYPE_P (inner_type)
9247 && (need_default_ctor || need_copy_ctor
9248 || need_copy_assignment || need_dtor)
9249 && !type_dependent_expression_p (t)
9250 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
9251 need_copy_ctor, need_copy_assignment,
9252 need_dtor))
9253 remove = true;
9255 if (!remove
9256 && c_kind == OMP_CLAUSE_SHARED
9257 && processing_template_decl)
9259 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
9260 if (t)
9261 OMP_CLAUSE_DECL (c) = t;
9264 if (remove)
9265 *pc = OMP_CLAUSE_CHAIN (c);
9266 else
9267 pc = &OMP_CLAUSE_CHAIN (c);
9270 if (allocate_seen)
9271 for (pc = &clauses, c = clauses; c ; c = *pc)
9273 bool remove = false;
9274 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
9275 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
9276 && DECL_P (OMP_CLAUSE_DECL (c))
9277 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
9279 error_at (OMP_CLAUSE_LOCATION (c),
9280 "%qD specified in %<allocate%> clause but not in "
9281 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
9282 remove = true;
9284 if (remove)
9285 *pc = OMP_CLAUSE_CHAIN (c);
9286 else
9287 pc = &OMP_CLAUSE_CHAIN (c);
9290 bitmap_obstack_release (NULL);
9291 return clauses;
9294 /* Start processing OpenMP clauses that can include any
9295 privatization clauses for non-static data members. */
9297 tree
9298 push_omp_privatization_clauses (bool ignore_next)
9300 if (omp_private_member_ignore_next)
9302 omp_private_member_ignore_next = ignore_next;
9303 return NULL_TREE;
9305 omp_private_member_ignore_next = ignore_next;
9306 if (omp_private_member_map)
9307 omp_private_member_vec.safe_push (error_mark_node);
9308 return push_stmt_list ();
9311 /* Revert remapping of any non-static data members since
9312 the last push_omp_privatization_clauses () call. */
9314 void
9315 pop_omp_privatization_clauses (tree stmt)
9317 if (stmt == NULL_TREE)
9318 return;
9319 stmt = pop_stmt_list (stmt);
9320 if (omp_private_member_map)
9322 while (!omp_private_member_vec.is_empty ())
9324 tree t = omp_private_member_vec.pop ();
9325 if (t == error_mark_node)
9327 add_stmt (stmt);
9328 return;
9330 bool no_decl_expr = t == integer_zero_node;
9331 if (no_decl_expr)
9332 t = omp_private_member_vec.pop ();
9333 tree *v = omp_private_member_map->get (t);
9334 gcc_assert (v);
9335 if (!no_decl_expr)
9336 add_decl_expr (*v);
9337 omp_private_member_map->remove (t);
9339 delete omp_private_member_map;
9340 omp_private_member_map = NULL;
9342 add_stmt (stmt);
9345 /* Remember OpenMP privatization clauses mapping and clear it.
9346 Used for lambdas. */
9348 void
9349 save_omp_privatization_clauses (vec<tree> &save)
9351 save = vNULL;
9352 if (omp_private_member_ignore_next)
9353 save.safe_push (integer_one_node);
9354 omp_private_member_ignore_next = false;
9355 if (!omp_private_member_map)
9356 return;
9358 while (!omp_private_member_vec.is_empty ())
9360 tree t = omp_private_member_vec.pop ();
9361 if (t == error_mark_node)
9363 save.safe_push (t);
9364 continue;
9366 tree n = t;
9367 if (t == integer_zero_node)
9368 t = omp_private_member_vec.pop ();
9369 tree *v = omp_private_member_map->get (t);
9370 gcc_assert (v);
9371 save.safe_push (*v);
9372 save.safe_push (t);
9373 if (n != t)
9374 save.safe_push (n);
9376 delete omp_private_member_map;
9377 omp_private_member_map = NULL;
9380 /* Restore OpenMP privatization clauses mapping saved by the
9381 above function. */
9383 void
9384 restore_omp_privatization_clauses (vec<tree> &save)
9386 gcc_assert (omp_private_member_vec.is_empty ());
9387 omp_private_member_ignore_next = false;
9388 if (save.is_empty ())
9389 return;
9390 if (save.length () == 1 && save[0] == integer_one_node)
9392 omp_private_member_ignore_next = true;
9393 save.release ();
9394 return;
9397 omp_private_member_map = new hash_map <tree, tree>;
9398 while (!save.is_empty ())
9400 tree t = save.pop ();
9401 tree n = t;
9402 if (t != error_mark_node)
9404 if (t == integer_one_node)
9406 omp_private_member_ignore_next = true;
9407 gcc_assert (save.is_empty ());
9408 break;
9410 if (t == integer_zero_node)
9411 t = save.pop ();
9412 tree &v = omp_private_member_map->get_or_insert (t);
9413 v = save.pop ();
9415 omp_private_member_vec.safe_push (t);
9416 if (n != t)
9417 omp_private_member_vec.safe_push (n);
9419 save.release ();
9422 /* For all variables in the tree_list VARS, mark them as thread local. */
9424 void
9425 finish_omp_threadprivate (tree vars)
9427 tree t;
9429 /* Mark every variable in VARS to be assigned thread local storage. */
9430 for (t = vars; t; t = TREE_CHAIN (t))
9432 tree v = TREE_PURPOSE (t);
9434 if (error_operand_p (v))
9436 else if (!VAR_P (v))
9437 error ("%<threadprivate%> %qD is not file, namespace "
9438 "or block scope variable", v);
9439 /* If V had already been marked threadprivate, it doesn't matter
9440 whether it had been used prior to this point. */
9441 else if (TREE_USED (v)
9442 && (DECL_LANG_SPECIFIC (v) == NULL
9443 || !CP_DECL_THREADPRIVATE_P (v)))
9444 error ("%qE declared %<threadprivate%> after first use", v);
9445 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
9446 error ("automatic variable %qE cannot be %<threadprivate%>", v);
9447 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
9448 error ("%<threadprivate%> %qE has incomplete type", v);
9449 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
9450 && CP_DECL_CONTEXT (v) != current_class_type)
9451 error ("%<threadprivate%> %qE directive not "
9452 "in %qT definition", v, CP_DECL_CONTEXT (v));
9453 else
9455 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
9456 if (DECL_LANG_SPECIFIC (v) == NULL)
9457 retrofit_lang_decl (v);
9459 if (! CP_DECL_THREAD_LOCAL_P (v))
9461 CP_DECL_THREAD_LOCAL_P (v) = true;
9462 set_decl_tls_model (v, decl_default_tls_model (v));
9463 /* If rtl has been already set for this var, call
9464 make_decl_rtl once again, so that encode_section_info
9465 has a chance to look at the new decl flags. */
9466 if (DECL_RTL_SET_P (v))
9467 make_decl_rtl (v);
9469 CP_DECL_THREADPRIVATE_P (v) = 1;
9474 /* Build an OpenMP structured block. */
9476 tree
9477 begin_omp_structured_block (void)
9479 return do_pushlevel (sk_omp);
9482 tree
9483 finish_omp_structured_block (tree block)
9485 return do_poplevel (block);
9488 /* Similarly, except force the retention of the BLOCK. */
9490 tree
9491 begin_omp_parallel (void)
9493 keep_next_level (true);
9494 return begin_omp_structured_block ();
9497 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
9498 statement. */
9500 tree
9501 finish_oacc_data (tree clauses, tree block)
9503 tree stmt;
9505 block = finish_omp_structured_block (block);
9507 stmt = make_node (OACC_DATA);
9508 TREE_TYPE (stmt) = void_type_node;
9509 OACC_DATA_CLAUSES (stmt) = clauses;
9510 OACC_DATA_BODY (stmt) = block;
9512 return add_stmt (stmt);
9515 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
9516 statement. */
9518 tree
9519 finish_oacc_host_data (tree clauses, tree block)
9521 tree stmt;
9523 block = finish_omp_structured_block (block);
9525 stmt = make_node (OACC_HOST_DATA);
9526 TREE_TYPE (stmt) = void_type_node;
9527 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
9528 OACC_HOST_DATA_BODY (stmt) = block;
9530 return add_stmt (stmt);
9533 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
9534 statement. */
9536 tree
9537 finish_omp_construct (enum tree_code code, tree body, tree clauses)
9539 body = finish_omp_structured_block (body);
9541 tree stmt = make_node (code);
9542 TREE_TYPE (stmt) = void_type_node;
9543 OMP_BODY (stmt) = body;
9544 OMP_CLAUSES (stmt) = clauses;
9546 return add_stmt (stmt);
9549 /* Used to walk OpenMP target directive body. */
9551 struct omp_target_walk_data
9553 /* Holds the 'this' expression found in current function. */
9554 tree current_object;
9556 /* True if the 'this' expression was accessed in the target body. */
9557 bool this_expr_accessed;
9559 /* For non-static functions, record which pointer-typed members were
9560 accessed, and the whole expression. */
9561 hash_map<tree, tree> ptr_members_accessed;
9563 /* Record which lambda objects were accessed in target body. */
9564 hash_set<tree> lambda_objects_accessed;
9566 /* For lambda functions, the __closure object expression of the current
9567 function, and the set of captured variables accessed in target body. */
9568 tree current_closure;
9569 hash_set<tree> closure_vars_accessed;
9571 /* Local variables declared inside a BIND_EXPR, used to filter out such
9572 variables when recording lambda_objects_accessed. */
9573 hash_set<tree> local_decls;
9576 /* Helper function of finish_omp_target_clauses, called via
9577 cp_walk_tree_without_duplicates. Traverse body of OpenMP target
9578 directive *TP, and fill out omp_target_walk_data passed in *PTR. */
9580 static tree
9581 finish_omp_target_clauses_r (tree *tp, int *walk_subtrees, void *ptr)
9583 tree t = *tp;
9584 struct omp_target_walk_data *data = (struct omp_target_walk_data *) ptr;
9585 tree current_object = data->current_object;
9586 tree current_closure = data->current_closure;
9588 /* References inside of these expression codes shouldn't incur any
9589 form of mapping, so return early. */
9590 if (TREE_CODE (t) == SIZEOF_EXPR
9591 || TREE_CODE (t) == ALIGNOF_EXPR)
9593 *walk_subtrees = 0;
9594 return NULL_TREE;
9597 if (TREE_CODE (t) == OMP_CLAUSE)
9598 return NULL_TREE;
9600 if (current_object)
9602 tree this_expr = TREE_OPERAND (current_object, 0);
9604 if (operand_equal_p (t, this_expr))
9606 data->this_expr_accessed = true;
9607 *walk_subtrees = 0;
9608 return NULL_TREE;
9611 if (TREE_CODE (t) == COMPONENT_REF
9612 && POINTER_TYPE_P (TREE_TYPE (t))
9613 && operand_equal_p (TREE_OPERAND (t, 0), current_object)
9614 && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL)
9616 data->this_expr_accessed = true;
9617 tree fld = TREE_OPERAND (t, 1);
9618 if (data->ptr_members_accessed.get (fld) == NULL)
9620 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
9621 t = convert_from_reference (t);
9622 data->ptr_members_accessed.put (fld, t);
9624 *walk_subtrees = 0;
9625 return NULL_TREE;
9629 /* When the current_function_decl is a lambda function, the closure object
9630 argument's type seems to not yet have fields layed out, so a recording
9631 of DECL_VALUE_EXPRs during the target body walk seems the only way to
9632 find them. */
9633 if (current_closure
9634 && (VAR_P (t)
9635 || TREE_CODE (t) == PARM_DECL
9636 || TREE_CODE (t) == RESULT_DECL)
9637 && DECL_HAS_VALUE_EXPR_P (t)
9638 && TREE_CODE (DECL_VALUE_EXPR (t)) == COMPONENT_REF
9639 && operand_equal_p (current_closure,
9640 TREE_OPERAND (DECL_VALUE_EXPR (t), 0)))
9642 if (!data->closure_vars_accessed.contains (t))
9643 data->closure_vars_accessed.add (t);
9644 *walk_subtrees = 0;
9645 return NULL_TREE;
9648 if (TREE_CODE (t) == BIND_EXPR)
9650 tree block = BIND_EXPR_BLOCK (t);
9651 for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
9652 if (!data->local_decls.contains (var))
9653 data->local_decls.add (var);
9654 return NULL_TREE;
9657 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
9659 tree lt = TREE_TYPE (t);
9660 gcc_assert (CLASS_TYPE_P (lt));
9662 if (!data->lambda_objects_accessed.contains (t)
9663 /* Do not prepare to create target maps for locally declared
9664 lambdas or anonymous ones. */
9665 && !data->local_decls.contains (t)
9666 && TREE_CODE (t) != TARGET_EXPR)
9667 data->lambda_objects_accessed.add (t);
9668 *walk_subtrees = 0;
9669 return NULL_TREE;
9672 return NULL_TREE;
9675 /* Helper function for finish_omp_target, and also from tsubst_expr.
9676 Create additional clauses for mapping of non-static members, lambda objects,
9677 etc. */
9679 void
9680 finish_omp_target_clauses (location_t loc, tree body, tree *clauses_ptr)
9682 omp_target_walk_data data;
9683 data.this_expr_accessed = false;
9684 data.current_object = NULL_TREE;
9686 if (DECL_NONSTATIC_MEMBER_P (current_function_decl) && current_class_ptr)
9687 if (tree ct = current_nonlambda_class_type ())
9689 tree object = maybe_dummy_object (ct, NULL);
9690 object = maybe_resolve_dummy (object, true);
9691 data.current_object = object;
9694 if (DECL_LAMBDA_FUNCTION_P (current_function_decl))
9696 tree closure = DECL_ARGUMENTS (current_function_decl);
9697 data.current_closure = build_indirect_ref (loc, closure, RO_UNARY_STAR);
9699 else
9700 data.current_closure = NULL_TREE;
9702 cp_walk_tree_without_duplicates (&body, finish_omp_target_clauses_r, &data);
9704 auto_vec<tree, 16> new_clauses;
9706 tree omp_target_this_expr = NULL_TREE;
9707 tree *explicit_this_deref_map = NULL;
9708 if (data.this_expr_accessed)
9710 omp_target_this_expr = TREE_OPERAND (data.current_object, 0);
9712 /* See if explicit user-specified map(this[:]) clause already exists.
9713 If not, we create an implicit map(tofrom:this[:1]) clause. */
9714 for (tree *cp = clauses_ptr; *cp; cp = &OMP_CLAUSE_CHAIN (*cp))
9715 if (OMP_CLAUSE_CODE (*cp) == OMP_CLAUSE_MAP
9716 && (TREE_CODE (OMP_CLAUSE_DECL (*cp)) == INDIRECT_REF
9717 || TREE_CODE (OMP_CLAUSE_DECL (*cp)) == MEM_REF)
9718 && operand_equal_p (TREE_OPERAND (OMP_CLAUSE_DECL (*cp), 0),
9719 omp_target_this_expr))
9721 explicit_this_deref_map = cp;
9722 break;
9726 if (DECL_LAMBDA_FUNCTION_P (current_function_decl)
9727 && (data.this_expr_accessed
9728 || !data.closure_vars_accessed.is_empty ()))
9730 /* For lambda functions, we need to first create a copy of the
9731 __closure object. */
9732 tree closure = DECL_ARGUMENTS (current_function_decl);
9733 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9734 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
9735 OMP_CLAUSE_DECL (c)
9736 = build_indirect_ref (loc, closure, RO_UNARY_STAR);
9737 OMP_CLAUSE_SIZE (c)
9738 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (closure)));
9739 new_clauses.safe_push (c);
9741 tree closure_obj = OMP_CLAUSE_DECL (c);
9742 tree closure_type = TREE_TYPE (closure_obj);
9744 gcc_assert (LAMBDA_TYPE_P (closure_type)
9745 && CLASS_TYPE_P (closure_type));
9747 tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
9748 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
9749 OMP_CLAUSE_DECL (c2) = closure;
9750 OMP_CLAUSE_SIZE (c2) = size_zero_node;
9751 new_clauses.safe_push (c2);
9754 if (data.this_expr_accessed)
9756 /* If the this-expr was accessed, create a map(*this) clause. */
9757 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
9758 if (explicit_this_deref_map)
9760 tree this_map = *explicit_this_deref_map;
9761 tree nc = OMP_CLAUSE_CHAIN (this_map);
9762 gcc_assert (nc != NULL_TREE
9763 && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
9764 && (OMP_CLAUSE_MAP_KIND (nc)
9765 == GOMP_MAP_FIRSTPRIVATE_POINTER));
9766 kind = OMP_CLAUSE_MAP_KIND (this_map);
9767 /* Remove the original 'map(*this) map(firstprivate_ptr:this)'
9768 two-map sequence away from the chain. */
9769 *explicit_this_deref_map = OMP_CLAUSE_CHAIN (nc);
9771 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9772 OMP_CLAUSE_SET_MAP_KIND (c, kind);
9773 OMP_CLAUSE_DECL (c)
9774 = build_indirect_ref (loc, omp_target_this_expr, RO_UNARY_STAR);
9775 OMP_CLAUSE_SIZE (c)
9776 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (omp_target_this_expr)));
9777 new_clauses.safe_push (c);
9779 /* If we're in a lambda function, the this-pointer will actually be
9780 '__closure->this', a mapped member of __closure, hence always_pointer.
9781 Otherwise it's a firstprivate pointer. */
9782 enum gomp_map_kind ptr_kind
9783 = (DECL_LAMBDA_FUNCTION_P (current_function_decl)
9784 ? GOMP_MAP_ALWAYS_POINTER
9785 : GOMP_MAP_FIRSTPRIVATE_POINTER);
9786 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9787 OMP_CLAUSE_SET_MAP_KIND (c, ptr_kind);
9788 OMP_CLAUSE_DECL (c) = omp_target_this_expr;
9789 OMP_CLAUSE_SIZE (c) = size_zero_node;
9790 new_clauses.safe_push (c);
9793 if (DECL_LAMBDA_FUNCTION_P (current_function_decl))
9795 if (omp_target_this_expr)
9797 STRIP_NOPS (omp_target_this_expr);
9798 gcc_assert (DECL_HAS_VALUE_EXPR_P (omp_target_this_expr));
9799 omp_target_this_expr = DECL_VALUE_EXPR (omp_target_this_expr);
9802 for (hash_set<tree>::iterator i = data.closure_vars_accessed.begin ();
9803 i != data.closure_vars_accessed.end (); ++i)
9805 tree orig_decl = *i;
9806 tree closure_expr = DECL_VALUE_EXPR (orig_decl);
9808 if (TREE_CODE (TREE_TYPE (orig_decl)) == POINTER_TYPE
9809 || TREE_CODE (TREE_TYPE (orig_decl)) == REFERENCE_TYPE)
9811 /* this-pointer is processed above, outside this loop. */
9812 if (omp_target_this_expr
9813 && operand_equal_p (closure_expr, omp_target_this_expr))
9814 continue;
9816 bool ptr_p = TREE_CODE (TREE_TYPE (orig_decl)) == POINTER_TYPE;
9817 enum gomp_map_kind kind, ptr_kind, nc_kind;
9818 tree size;
9820 if (ptr_p)
9822 /* For pointers, default mapped as zero-length array
9823 section. */
9824 kind = GOMP_MAP_ALLOC;
9825 nc_kind = GOMP_MAP_FIRSTPRIVATE_POINTER;
9826 ptr_kind = GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION;
9827 size = size_zero_node;
9829 else
9831 /* For references, default mapped as appearing on map
9832 clause. */
9833 kind = GOMP_MAP_TOFROM;
9834 nc_kind = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
9835 ptr_kind = GOMP_MAP_ALWAYS_POINTER;
9836 size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (closure_expr)));
9839 for (tree *p = clauses_ptr; *p; p = &OMP_CLAUSE_CHAIN (*p))
9840 if (OMP_CLAUSE_CODE (*p) == OMP_CLAUSE_MAP
9841 && (TREE_CODE (OMP_CLAUSE_DECL (*p)) == INDIRECT_REF
9842 || TREE_CODE (OMP_CLAUSE_DECL (*p)) == MEM_REF)
9843 && operand_equal_p (TREE_OPERAND (OMP_CLAUSE_DECL (*p), 0),
9844 orig_decl))
9846 /* If this was already specified by user as a map,
9847 save the user specified map kind, delete the
9848 "map(*ptr/ref), map(firstprivate ptr/ref)" sequence,
9849 and insert our own sequence:
9850 "map(*__closure->ptr/ref), map(<ptr_kind>:__closure->ref"
9852 tree nc = OMP_CLAUSE_CHAIN (*p);
9853 gcc_assert (nc != NULL_TREE
9854 && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
9855 && OMP_CLAUSE_MAP_KIND (nc) == nc_kind);
9856 /* Update with user specified kind and size. */
9857 kind = OMP_CLAUSE_MAP_KIND (*p);
9858 size = OMP_CLAUSE_SIZE (*p);
9859 *p = OMP_CLAUSE_CHAIN (nc);
9860 break;
9863 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9864 OMP_CLAUSE_SET_MAP_KIND (c, kind);
9865 OMP_CLAUSE_DECL (c)
9866 = build_indirect_ref (loc, closure_expr, RO_UNARY_STAR);
9867 OMP_CLAUSE_SIZE (c) = size;
9868 if (ptr_p)
9869 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
9870 new_clauses.safe_push (c);
9872 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9873 OMP_CLAUSE_SET_MAP_KIND (c, ptr_kind);
9874 OMP_CLAUSE_DECL (c) = closure_expr;
9875 OMP_CLAUSE_SIZE (c) = size_zero_node;
9876 new_clauses.safe_push (c);
9881 if (!data.ptr_members_accessed.is_empty ())
9882 for (hash_map<tree, tree>::iterator i = data.ptr_members_accessed.begin ();
9883 i != data.ptr_members_accessed.end (); ++i)
9885 /* For each referenced member that is of pointer or reference-to-pointer
9886 type, create the equivalent of map(alloc:this->ptr[:0]). */
9887 tree field_decl = (*i).first;
9888 tree ptr_member = (*i).second;
9890 for (tree c = *clauses_ptr; c; c = OMP_CLAUSE_CHAIN (c))
9892 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
9893 continue;
9894 /* If map(this->ptr[:N]) already exists, avoid creating another
9895 such map. */
9896 tree decl = OMP_CLAUSE_DECL (c);
9897 if ((TREE_CODE (decl) == INDIRECT_REF
9898 || TREE_CODE (decl) == MEM_REF)
9899 && operand_equal_p (TREE_OPERAND (decl, 0), ptr_member))
9900 goto next_ptr_member;
9903 if (!cxx_mark_addressable (ptr_member))
9904 gcc_unreachable ();
9906 if (TREE_CODE (TREE_TYPE (field_decl)) == REFERENCE_TYPE)
9908 /* For reference to pointers, we need to map the referenced
9909 pointer first for things to be correct. */
9910 tree ptr_member_type = TREE_TYPE (ptr_member);
9912 /* Map pointer target as zero-length array section. */
9913 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9914 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
9915 OMP_CLAUSE_DECL (c)
9916 = build1 (INDIRECT_REF, TREE_TYPE (ptr_member_type), ptr_member);
9917 OMP_CLAUSE_SIZE (c) = size_zero_node;
9918 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
9920 /* Map pointer to zero-length array section. */
9921 tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
9922 OMP_CLAUSE_SET_MAP_KIND
9923 (c2, GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION);
9924 OMP_CLAUSE_DECL (c2) = ptr_member;
9925 OMP_CLAUSE_SIZE (c2) = size_zero_node;
9927 /* Attach reference-to-pointer field to pointer. */
9928 tree c3 = build_omp_clause (loc, OMP_CLAUSE_MAP);
9929 OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_ATTACH);
9930 OMP_CLAUSE_DECL (c3) = TREE_OPERAND (ptr_member, 0);
9931 OMP_CLAUSE_SIZE (c3) = size_zero_node;
9933 new_clauses.safe_push (c);
9934 new_clauses.safe_push (c2);
9935 new_clauses.safe_push (c3);
9937 else if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9939 /* Map pointer target as zero-length array section. */
9940 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9941 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
9942 OMP_CLAUSE_DECL (c) = build_indirect_ref (loc, ptr_member,
9943 RO_UNARY_STAR);
9944 OMP_CLAUSE_SIZE (c) = size_zero_node;
9945 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
9947 /* Attach zero-length array section to pointer. */
9948 tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
9949 OMP_CLAUSE_SET_MAP_KIND
9950 (c2, GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION);
9951 OMP_CLAUSE_DECL (c2) = ptr_member;
9952 OMP_CLAUSE_SIZE (c2) = size_zero_node;
9954 new_clauses.safe_push (c);
9955 new_clauses.safe_push (c2);
9957 else
9958 gcc_unreachable ();
9960 next_ptr_member:
9964 for (hash_set<tree>::iterator i = data.lambda_objects_accessed.begin ();
9965 i != data.lambda_objects_accessed.end (); ++i)
9967 tree lobj = *i;
9968 if (TREE_CODE (lobj) == TARGET_EXPR)
9969 lobj = TREE_OPERAND (lobj, 0);
9971 tree lt = TREE_TYPE (lobj);
9972 gcc_assert (LAMBDA_TYPE_P (lt) && CLASS_TYPE_P (lt));
9974 tree lc = build_omp_clause (loc, OMP_CLAUSE_MAP);
9975 OMP_CLAUSE_SET_MAP_KIND (lc, GOMP_MAP_TO);
9976 OMP_CLAUSE_DECL (lc) = lobj;
9977 OMP_CLAUSE_SIZE (lc) = TYPE_SIZE_UNIT (lt);
9978 new_clauses.safe_push (lc);
9980 for (tree fld = TYPE_FIELDS (lt); fld; fld = DECL_CHAIN (fld))
9982 if (TREE_CODE (TREE_TYPE (fld)) == POINTER_TYPE)
9984 tree exp = build3 (COMPONENT_REF, TREE_TYPE (fld),
9985 lobj, fld, NULL_TREE);
9986 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9987 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
9988 OMP_CLAUSE_DECL (c)
9989 = build_indirect_ref (loc, exp, RO_UNARY_STAR);
9990 OMP_CLAUSE_SIZE (c) = size_zero_node;
9991 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
9992 new_clauses.safe_push (c);
9994 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9995 OMP_CLAUSE_SET_MAP_KIND
9996 (c, GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION);
9997 OMP_CLAUSE_DECL (c) = exp;
9998 OMP_CLAUSE_SIZE (c) = size_zero_node;
9999 new_clauses.safe_push (c);
10001 else if (TREE_CODE (TREE_TYPE (fld)) == REFERENCE_TYPE)
10003 tree exp = build3 (COMPONENT_REF, TREE_TYPE (fld),
10004 lobj, fld, NULL_TREE);
10005 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10006 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
10007 OMP_CLAUSE_DECL (c)
10008 = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
10009 OMP_CLAUSE_SIZE (c)
10010 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (exp)));
10011 new_clauses.safe_push (c);
10013 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10014 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALWAYS_POINTER);
10015 OMP_CLAUSE_DECL (c) = exp;
10016 OMP_CLAUSE_SIZE (c) = size_zero_node;
10017 new_clauses.safe_push (c);
10022 tree c = *clauses_ptr;
10023 for (int i = new_clauses.length () - 1; i >= 0; i--)
10025 OMP_CLAUSE_CHAIN (new_clauses[i]) = c;
10026 c = new_clauses[i];
10028 *clauses_ptr = c;
10031 /* Called from cp_parser_omp_target. Create additional implicit clauses for
10032 OpenMP target directives, and do sanity checks. */
10034 tree
10035 finish_omp_target (location_t loc, tree clauses, tree body, bool combined_p)
10037 if (!processing_template_decl)
10038 finish_omp_target_clauses (loc, body, &clauses);
10040 tree stmt = make_node (OMP_TARGET);
10041 TREE_TYPE (stmt) = void_type_node;
10042 OMP_TARGET_CLAUSES (stmt) = clauses;
10043 OMP_TARGET_BODY (stmt) = body;
10044 OMP_TARGET_COMBINED (stmt) = combined_p;
10045 SET_EXPR_LOCATION (stmt, loc);
10047 tree c = clauses;
10048 while (c)
10050 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
10051 switch (OMP_CLAUSE_MAP_KIND (c))
10053 case GOMP_MAP_TO:
10054 case GOMP_MAP_ALWAYS_TO:
10055 case GOMP_MAP_PRESENT_TO:
10056 case GOMP_MAP_ALWAYS_PRESENT_TO:
10057 case GOMP_MAP_FROM:
10058 case GOMP_MAP_ALWAYS_FROM:
10059 case GOMP_MAP_PRESENT_FROM:
10060 case GOMP_MAP_ALWAYS_PRESENT_FROM:
10061 case GOMP_MAP_TOFROM:
10062 case GOMP_MAP_ALWAYS_TOFROM:
10063 case GOMP_MAP_PRESENT_TOFROM:
10064 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
10065 case GOMP_MAP_ALLOC:
10066 case GOMP_MAP_PRESENT_ALLOC:
10067 case GOMP_MAP_FIRSTPRIVATE_POINTER:
10068 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
10069 case GOMP_MAP_ALWAYS_POINTER:
10070 case GOMP_MAP_ATTACH_DETACH:
10071 case GOMP_MAP_ATTACH:
10072 case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10073 case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
10074 break;
10075 default:
10076 error_at (OMP_CLAUSE_LOCATION (c),
10077 "%<#pragma omp target%> with map-type other "
10078 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
10079 "on %<map%> clause");
10080 break;
10082 c = OMP_CLAUSE_CHAIN (c);
10084 return add_stmt (stmt);
10087 tree
10088 finish_omp_parallel (tree clauses, tree body)
10090 tree stmt;
10092 body = finish_omp_structured_block (body);
10094 stmt = make_node (OMP_PARALLEL);
10095 TREE_TYPE (stmt) = void_type_node;
10096 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10097 OMP_PARALLEL_BODY (stmt) = body;
10099 return add_stmt (stmt);
10102 tree
10103 begin_omp_task (void)
10105 keep_next_level (true);
10106 return begin_omp_structured_block ();
10109 tree
10110 finish_omp_task (tree clauses, tree body)
10112 tree stmt;
10114 body = finish_omp_structured_block (body);
10116 stmt = make_node (OMP_TASK);
10117 TREE_TYPE (stmt) = void_type_node;
10118 OMP_TASK_CLAUSES (stmt) = clauses;
10119 OMP_TASK_BODY (stmt) = body;
10121 return add_stmt (stmt);
10124 /* Helper function for finish_omp_for. Convert Ith random access iterator
10125 into integral iterator. Return FALSE if successful. */
10127 static bool
10128 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
10129 tree declv, tree orig_declv, tree initv,
10130 tree condv, tree incrv, tree *body,
10131 tree *pre_body, tree &clauses,
10132 int collapse, int ordered)
10134 tree diff, iter_init, iter_incr = NULL, last;
10135 tree incr_var = NULL, orig_pre_body, orig_body, c;
10136 tree decl = TREE_VEC_ELT (declv, i);
10137 tree init = TREE_VEC_ELT (initv, i);
10138 tree cond = TREE_VEC_ELT (condv, i);
10139 tree incr = TREE_VEC_ELT (incrv, i);
10140 tree iter = decl;
10141 location_t elocus = locus;
10143 if (init && EXPR_HAS_LOCATION (init))
10144 elocus = EXPR_LOCATION (init);
10146 switch (TREE_CODE (cond))
10148 case GT_EXPR:
10149 case GE_EXPR:
10150 case LT_EXPR:
10151 case LE_EXPR:
10152 case NE_EXPR:
10153 if (TREE_OPERAND (cond, 1) == iter)
10154 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
10155 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
10156 if (TREE_OPERAND (cond, 0) != iter)
10157 cond = error_mark_node;
10158 else
10160 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
10161 TREE_CODE (cond),
10162 iter, ERROR_MARK,
10163 TREE_OPERAND (cond, 1), ERROR_MARK,
10164 NULL_TREE, NULL, tf_warning_or_error);
10165 if (error_operand_p (tem))
10166 return true;
10168 break;
10169 default:
10170 cond = error_mark_node;
10171 break;
10173 if (cond == error_mark_node)
10175 error_at (elocus, "invalid controlling predicate");
10176 return true;
10178 diff = build_x_binary_op (elocus, MINUS_EXPR,
10179 TREE_OPERAND (cond, 1), ERROR_MARK,
10180 iter, ERROR_MARK,
10181 NULL_TREE, NULL, tf_warning_or_error);
10182 diff = cp_fully_fold (diff);
10183 if (error_operand_p (diff))
10184 return true;
10185 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
10187 error_at (elocus, "difference between %qE and %qD does not have integer type",
10188 TREE_OPERAND (cond, 1), iter);
10189 return true;
10191 if (!c_omp_check_loop_iv_exprs (locus, code, orig_declv, i,
10192 TREE_VEC_ELT (declv, i), NULL_TREE,
10193 cond, cp_walk_subtrees))
10194 return true;
10196 switch (TREE_CODE (incr))
10198 case PREINCREMENT_EXPR:
10199 case PREDECREMENT_EXPR:
10200 case POSTINCREMENT_EXPR:
10201 case POSTDECREMENT_EXPR:
10202 if (TREE_OPERAND (incr, 0) != iter)
10204 incr = error_mark_node;
10205 break;
10207 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
10208 TREE_CODE (incr), iter,
10209 NULL_TREE, tf_warning_or_error);
10210 if (error_operand_p (iter_incr))
10211 return true;
10212 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
10213 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
10214 incr = integer_one_node;
10215 else
10216 incr = integer_minus_one_node;
10217 break;
10218 case MODIFY_EXPR:
10219 if (TREE_OPERAND (incr, 0) != iter)
10220 incr = error_mark_node;
10221 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
10222 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
10224 tree rhs = TREE_OPERAND (incr, 1);
10225 if (TREE_OPERAND (rhs, 0) == iter)
10227 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
10228 != INTEGER_TYPE)
10229 incr = error_mark_node;
10230 else
10232 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
10233 iter, TREE_CODE (rhs),
10234 TREE_OPERAND (rhs, 1),
10235 NULL_TREE,
10236 tf_warning_or_error);
10237 if (error_operand_p (iter_incr))
10238 return true;
10239 incr = TREE_OPERAND (rhs, 1);
10240 incr = cp_convert (TREE_TYPE (diff), incr,
10241 tf_warning_or_error);
10242 if (TREE_CODE (rhs) == MINUS_EXPR)
10244 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
10245 incr = fold_simple (incr);
10247 if (TREE_CODE (incr) != INTEGER_CST
10248 && (TREE_CODE (incr) != NOP_EXPR
10249 || (TREE_CODE (TREE_OPERAND (incr, 0))
10250 != INTEGER_CST)))
10251 iter_incr = NULL;
10254 else if (TREE_OPERAND (rhs, 1) == iter)
10256 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
10257 || TREE_CODE (rhs) != PLUS_EXPR)
10258 incr = error_mark_node;
10259 else
10261 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
10262 PLUS_EXPR,
10263 TREE_OPERAND (rhs, 0),
10264 ERROR_MARK, iter,
10265 ERROR_MARK, NULL_TREE, NULL,
10266 tf_warning_or_error);
10267 if (error_operand_p (iter_incr))
10268 return true;
10269 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
10270 iter, NOP_EXPR,
10271 iter_incr, NULL_TREE,
10272 tf_warning_or_error);
10273 if (error_operand_p (iter_incr))
10274 return true;
10275 incr = TREE_OPERAND (rhs, 0);
10276 iter_incr = NULL;
10279 else
10280 incr = error_mark_node;
10282 else
10283 incr = error_mark_node;
10284 break;
10285 default:
10286 incr = error_mark_node;
10287 break;
10290 if (incr == error_mark_node)
10292 error_at (elocus, "invalid increment expression");
10293 return true;
10296 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
10297 incr = cp_fully_fold (incr);
10298 tree loop_iv_seen = NULL_TREE;
10299 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10300 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
10301 && OMP_CLAUSE_DECL (c) == iter)
10303 if (code == OMP_TASKLOOP || code == OMP_LOOP)
10305 loop_iv_seen = c;
10306 OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c) = 1;
10308 break;
10310 else if ((code == OMP_TASKLOOP || code == OMP_LOOP)
10311 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
10312 && OMP_CLAUSE_DECL (c) == iter)
10314 loop_iv_seen = c;
10315 if (code == OMP_TASKLOOP)
10316 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
10319 decl = create_temporary_var (TREE_TYPE (diff));
10320 pushdecl (decl);
10321 add_decl_expr (decl);
10322 last = create_temporary_var (TREE_TYPE (diff));
10323 pushdecl (last);
10324 add_decl_expr (last);
10325 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
10326 && (!ordered || (i < collapse && collapse > 1)))
10328 incr_var = create_temporary_var (TREE_TYPE (diff));
10329 pushdecl (incr_var);
10330 add_decl_expr (incr_var);
10332 gcc_assert (stmts_are_full_exprs_p ());
10333 tree diffvar = NULL_TREE;
10334 if (code == OMP_TASKLOOP)
10336 if (!loop_iv_seen)
10338 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
10339 OMP_CLAUSE_DECL (ivc) = iter;
10340 cxx_omp_finish_clause (ivc, NULL, false);
10341 OMP_CLAUSE_CHAIN (ivc) = clauses;
10342 clauses = ivc;
10344 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
10345 OMP_CLAUSE_DECL (lvc) = last;
10346 OMP_CLAUSE_CHAIN (lvc) = clauses;
10347 clauses = lvc;
10348 diffvar = create_temporary_var (TREE_TYPE (diff));
10349 pushdecl (diffvar);
10350 add_decl_expr (diffvar);
10352 else if (code == OMP_LOOP)
10354 if (!loop_iv_seen)
10356 /* While iterators on the loop construct are predetermined
10357 lastprivate, if the decl is not declared inside of the
10358 loop, OMP_CLAUSE_LASTPRIVATE should have been added
10359 already. */
10360 loop_iv_seen = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
10361 OMP_CLAUSE_DECL (loop_iv_seen) = iter;
10362 OMP_CLAUSE_CHAIN (loop_iv_seen) = clauses;
10363 clauses = loop_iv_seen;
10365 else if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_PRIVATE)
10367 OMP_CLAUSE_PRIVATE_DEBUG (loop_iv_seen) = 0;
10368 OMP_CLAUSE_PRIVATE_OUTER_REF (loop_iv_seen) = 0;
10369 OMP_CLAUSE_CODE (loop_iv_seen) = OMP_CLAUSE_FIRSTPRIVATE;
10371 if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_FIRSTPRIVATE)
10372 cxx_omp_finish_clause (loop_iv_seen, NULL, false);
10375 orig_pre_body = *pre_body;
10376 *pre_body = push_stmt_list ();
10377 if (orig_pre_body)
10378 add_stmt (orig_pre_body);
10379 if (init != NULL)
10380 finish_expr_stmt (build_x_modify_expr (elocus,
10381 iter, NOP_EXPR, init,
10382 NULL_TREE, tf_warning_or_error));
10383 init = build_int_cst (TREE_TYPE (diff), 0);
10384 if (c && iter_incr == NULL
10385 && (!ordered || (i < collapse && collapse > 1)))
10387 if (incr_var)
10389 finish_expr_stmt (build_x_modify_expr (elocus,
10390 incr_var, NOP_EXPR,
10391 incr, NULL_TREE,
10392 tf_warning_or_error));
10393 incr = incr_var;
10395 iter_incr = build_x_modify_expr (elocus,
10396 iter, PLUS_EXPR, incr,
10397 NULL_TREE, tf_warning_or_error);
10399 if (c && ordered && i < collapse && collapse > 1)
10400 iter_incr = incr;
10401 finish_expr_stmt (build_x_modify_expr (elocus,
10402 last, NOP_EXPR, init,
10403 NULL_TREE, tf_warning_or_error));
10404 if (diffvar)
10406 finish_expr_stmt (build_x_modify_expr (elocus,
10407 diffvar, NOP_EXPR,
10408 diff, NULL_TREE, tf_warning_or_error));
10409 diff = diffvar;
10411 *pre_body = pop_stmt_list (*pre_body);
10413 cond = cp_build_binary_op (elocus,
10414 TREE_CODE (cond), decl, diff,
10415 tf_warning_or_error);
10416 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
10417 elocus, incr, NULL_TREE);
10419 orig_body = *body;
10420 *body = push_stmt_list ();
10421 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
10422 iter_init = build_x_modify_expr (elocus,
10423 iter, PLUS_EXPR, iter_init,
10424 NULL_TREE, tf_warning_or_error);
10425 if (iter_init != error_mark_node)
10426 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
10427 finish_expr_stmt (iter_init);
10428 finish_expr_stmt (build_x_modify_expr (elocus,
10429 last, NOP_EXPR, decl,
10430 NULL_TREE, tf_warning_or_error));
10431 add_stmt (orig_body);
10432 *body = pop_stmt_list (*body);
10434 if (c)
10436 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
10437 if (!ordered)
10438 finish_expr_stmt (iter_incr);
10439 else
10441 iter_init = decl;
10442 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
10443 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
10444 iter_init, iter_incr);
10445 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
10446 iter_init = build_x_modify_expr (elocus,
10447 iter, PLUS_EXPR, iter_init,
10448 NULL_TREE, tf_warning_or_error);
10449 if (iter_init != error_mark_node)
10450 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
10451 finish_expr_stmt (iter_init);
10453 OMP_CLAUSE_LASTPRIVATE_STMT (c)
10454 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
10457 if (TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST)
10459 tree t = TREE_VEC_ELT (orig_declv, i);
10460 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
10461 && TREE_VALUE (t) == NULL_TREE
10462 && TREE_CODE (TREE_CHAIN (t)) == TREE_VEC);
10463 TREE_PURPOSE (t) = TREE_VEC_ELT (declv, i);
10464 TREE_VALUE (t) = last;
10466 else
10467 TREE_VEC_ELT (orig_declv, i)
10468 = tree_cons (TREE_VEC_ELT (declv, i), last, NULL_TREE);
10469 TREE_VEC_ELT (declv, i) = decl;
10470 TREE_VEC_ELT (initv, i) = init;
10471 TREE_VEC_ELT (condv, i) = cond;
10472 TREE_VEC_ELT (incrv, i) = incr;
10474 return false;
10477 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
10478 are directly for their associated operands in the statement. DECL
10479 and INIT are a combo; if DECL is NULL then INIT ought to be a
10480 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
10481 optional statements that need to go before the loop into its
10482 sk_omp scope. */
10484 tree
10485 finish_omp_for (location_t locus, enum tree_code code, tree declv,
10486 tree orig_declv, tree initv, tree condv, tree incrv,
10487 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
10489 tree omp_for = NULL, orig_incr = NULL;
10490 tree decl = NULL, init, cond, incr;
10491 location_t elocus;
10492 int i;
10493 int collapse = 1;
10494 int ordered = 0;
10496 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
10497 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
10498 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
10499 if (TREE_VEC_LENGTH (declv) > 1)
10501 tree c;
10503 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
10504 if (c)
10505 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
10506 else
10508 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
10509 if (c)
10510 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
10511 if (collapse != TREE_VEC_LENGTH (declv))
10512 ordered = TREE_VEC_LENGTH (declv);
10515 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
10517 decl = TREE_VEC_ELT (declv, i);
10518 init = TREE_VEC_ELT (initv, i);
10519 cond = TREE_VEC_ELT (condv, i);
10520 incr = TREE_VEC_ELT (incrv, i);
10521 elocus = locus;
10523 if (decl == NULL)
10525 if (init != NULL)
10526 switch (TREE_CODE (init))
10528 case MODIFY_EXPR:
10529 decl = TREE_OPERAND (init, 0);
10530 init = TREE_OPERAND (init, 1);
10531 break;
10532 case MODOP_EXPR:
10533 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
10535 decl = TREE_OPERAND (init, 0);
10536 init = TREE_OPERAND (init, 2);
10538 break;
10539 default:
10540 break;
10543 if (decl == NULL)
10545 error_at (locus,
10546 "expected iteration declaration or initialization");
10547 return NULL;
10551 if (init && EXPR_HAS_LOCATION (init))
10552 elocus = EXPR_LOCATION (init);
10554 if (cond == global_namespace)
10555 continue;
10557 if (cond == NULL)
10559 error_at (elocus, "missing controlling predicate");
10560 return NULL;
10563 if (incr == NULL)
10565 error_at (elocus, "missing increment expression");
10566 return NULL;
10569 TREE_VEC_ELT (declv, i) = decl;
10570 TREE_VEC_ELT (initv, i) = init;
10573 if (orig_inits)
10575 bool fail = false;
10576 tree orig_init;
10577 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
10578 if (orig_init
10579 && !c_omp_check_loop_iv_exprs (locus, code,
10580 orig_declv ? orig_declv : declv, i,
10581 TREE_VEC_ELT (declv, i), orig_init,
10582 NULL_TREE, cp_walk_subtrees))
10583 fail = true;
10584 if (fail)
10585 return NULL;
10588 if (dependent_omp_for_p (declv, initv, condv, incrv))
10590 tree stmt;
10592 stmt = make_node (code);
10594 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
10596 /* This is really just a place-holder. We'll be decomposing this
10597 again and going through the cp_build_modify_expr path below when
10598 we instantiate the thing. */
10599 TREE_VEC_ELT (initv, i)
10600 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
10601 TREE_VEC_ELT (initv, i));
10604 TREE_TYPE (stmt) = void_type_node;
10605 OMP_FOR_INIT (stmt) = initv;
10606 OMP_FOR_COND (stmt) = condv;
10607 OMP_FOR_INCR (stmt) = incrv;
10608 OMP_FOR_BODY (stmt) = body;
10609 OMP_FOR_PRE_BODY (stmt) = pre_body;
10610 OMP_FOR_CLAUSES (stmt) = clauses;
10612 SET_EXPR_LOCATION (stmt, locus);
10613 return add_stmt (stmt);
10616 if (!orig_declv)
10617 orig_declv = copy_node (declv);
10619 if (processing_template_decl)
10620 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
10622 for (i = 0; i < TREE_VEC_LENGTH (declv); )
10624 decl = TREE_VEC_ELT (declv, i);
10625 init = TREE_VEC_ELT (initv, i);
10626 cond = TREE_VEC_ELT (condv, i);
10627 incr = TREE_VEC_ELT (incrv, i);
10628 if (orig_incr)
10629 TREE_VEC_ELT (orig_incr, i) = incr;
10630 elocus = locus;
10632 if (init && EXPR_HAS_LOCATION (init))
10633 elocus = EXPR_LOCATION (init);
10635 if (!DECL_P (decl))
10637 error_at (elocus, "expected iteration declaration or initialization");
10638 return NULL;
10641 if (incr && TREE_CODE (incr) == MODOP_EXPR)
10643 if (orig_incr)
10644 TREE_VEC_ELT (orig_incr, i) = incr;
10645 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
10646 TREE_CODE (TREE_OPERAND (incr, 1)),
10647 TREE_OPERAND (incr, 2),
10648 tf_warning_or_error);
10651 if (CLASS_TYPE_P (TREE_TYPE (decl)))
10653 if (code == OMP_SIMD)
10655 error_at (elocus, "%<#pragma omp simd%> used with class "
10656 "iteration variable %qE", decl);
10657 return NULL;
10659 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
10660 initv, condv, incrv, &body,
10661 &pre_body, clauses,
10662 collapse, ordered))
10663 return NULL;
10664 continue;
10667 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
10668 && !TYPE_PTR_P (TREE_TYPE (decl)))
10670 error_at (elocus, "invalid type for iteration variable %qE", decl);
10671 return NULL;
10674 if (!processing_template_decl && TREE_CODE (init) != TREE_VEC)
10675 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
10676 tf_warning_or_error);
10677 else
10678 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
10679 if (decl == error_mark_node || init == error_mark_node)
10680 return NULL;
10682 TREE_VEC_ELT (declv, i) = decl;
10683 TREE_VEC_ELT (initv, i) = init;
10684 TREE_VEC_ELT (condv, i) = cond;
10685 TREE_VEC_ELT (incrv, i) = incr;
10686 i++;
10689 if (pre_body && IS_EMPTY_STMT (pre_body))
10690 pre_body = NULL;
10692 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
10693 incrv, body, pre_body,
10694 !processing_template_decl);
10696 /* Check for iterators appearing in lb, b or incr expressions. */
10697 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
10698 omp_for = NULL_TREE;
10700 if (omp_for == NULL)
10701 return NULL;
10703 add_stmt (omp_for);
10705 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
10707 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i);
10708 decl = TREE_OPERAND (init, 0);
10709 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), i);
10710 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
10712 if (!processing_template_decl)
10714 if (TREE_CODE (TREE_OPERAND (init, 1)) == TREE_VEC)
10716 tree t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 1);
10717 TREE_VEC_ELT (TREE_OPERAND (init, 1), 1)
10718 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10719 t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 2);
10720 TREE_VEC_ELT (TREE_OPERAND (init, 1), 2)
10721 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10723 else
10725 tree t = TREE_OPERAND (init, 1);
10726 TREE_OPERAND (init, 1)
10727 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10729 if (TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
10731 tree t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1);
10732 TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1)
10733 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10734 t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2);
10735 TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2)
10736 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10738 else
10740 tree t = TREE_OPERAND (cond, 1);
10741 TREE_OPERAND (cond, 1)
10742 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10746 if (TREE_CODE (incr) != MODIFY_EXPR)
10747 continue;
10749 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
10750 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
10751 && !processing_template_decl)
10753 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
10754 if (TREE_SIDE_EFFECTS (t)
10755 && t != decl
10756 && (TREE_CODE (t) != NOP_EXPR
10757 || TREE_OPERAND (t, 0) != decl))
10758 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
10759 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10761 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
10762 if (TREE_SIDE_EFFECTS (t)
10763 && t != decl
10764 && (TREE_CODE (t) != NOP_EXPR
10765 || TREE_OPERAND (t, 0) != decl))
10766 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
10767 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10770 if (orig_incr)
10771 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
10773 OMP_FOR_CLAUSES (omp_for) = clauses;
10775 /* For simd loops with non-static data member iterators, we could have added
10776 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
10777 step at this point, fill it in. */
10778 if (code == OMP_SIMD && !processing_template_decl
10779 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
10780 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
10781 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
10782 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
10784 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
10785 gcc_assert (decl == OMP_CLAUSE_DECL (c));
10786 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
10787 tree step, stept;
10788 switch (TREE_CODE (incr))
10790 case PREINCREMENT_EXPR:
10791 case POSTINCREMENT_EXPR:
10792 /* c_omp_for_incr_canonicalize_ptr() should have been
10793 called to massage things appropriately. */
10794 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
10795 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
10796 break;
10797 case PREDECREMENT_EXPR:
10798 case POSTDECREMENT_EXPR:
10799 /* c_omp_for_incr_canonicalize_ptr() should have been
10800 called to massage things appropriately. */
10801 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
10802 OMP_CLAUSE_LINEAR_STEP (c)
10803 = build_int_cst (TREE_TYPE (decl), -1);
10804 break;
10805 case MODIFY_EXPR:
10806 gcc_assert (TREE_OPERAND (incr, 0) == decl);
10807 incr = TREE_OPERAND (incr, 1);
10808 switch (TREE_CODE (incr))
10810 case PLUS_EXPR:
10811 if (TREE_OPERAND (incr, 1) == decl)
10812 step = TREE_OPERAND (incr, 0);
10813 else
10814 step = TREE_OPERAND (incr, 1);
10815 break;
10816 case MINUS_EXPR:
10817 case POINTER_PLUS_EXPR:
10818 gcc_assert (TREE_OPERAND (incr, 0) == decl);
10819 step = TREE_OPERAND (incr, 1);
10820 break;
10821 default:
10822 gcc_unreachable ();
10824 stept = TREE_TYPE (decl);
10825 if (INDIRECT_TYPE_P (stept))
10826 stept = sizetype;
10827 step = fold_convert (stept, step);
10828 if (TREE_CODE (incr) == MINUS_EXPR)
10829 step = fold_build1 (NEGATE_EXPR, stept, step);
10830 OMP_CLAUSE_LINEAR_STEP (c) = step;
10831 break;
10832 default:
10833 gcc_unreachable ();
10836 /* Override saved methods on OMP_LOOP's OMP_CLAUSE_LASTPRIVATE_LOOP_IV
10837 clauses, we need copy ctor for those rather than default ctor,
10838 plus as for other lastprivates assignment op and dtor. */
10839 if (code == OMP_LOOP && !processing_template_decl)
10840 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
10841 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
10842 && OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c)
10843 && cxx_omp_create_clause_info (c, TREE_TYPE (OMP_CLAUSE_DECL (c)),
10844 false, true, true, true))
10845 CP_OMP_CLAUSE_INFO (c) = NULL_TREE;
10847 return omp_for;
10850 /* Fix up range for decls. Those decls were pushed into BIND's BIND_EXPR_VARS
10851 and need to be moved into the BIND_EXPR inside of the OMP_FOR's body. */
10853 tree
10854 finish_omp_for_block (tree bind, tree omp_for)
10856 if (omp_for == NULL_TREE
10857 || !OMP_FOR_ORIG_DECLS (omp_for)
10858 || bind == NULL_TREE
10859 || TREE_CODE (bind) != BIND_EXPR)
10860 return bind;
10861 tree b = NULL_TREE;
10862 for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (omp_for)); i++)
10863 if (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)) == TREE_LIST
10864 && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)))
10866 tree v = TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i));
10867 gcc_assert (BIND_EXPR_BLOCK (bind)
10868 && (BIND_EXPR_VARS (bind)
10869 == BLOCK_VARS (BIND_EXPR_BLOCK (bind))));
10870 for (int j = 2; j < TREE_VEC_LENGTH (v); j++)
10871 for (tree *p = &BIND_EXPR_VARS (bind); *p; p = &DECL_CHAIN (*p))
10873 if (*p == TREE_VEC_ELT (v, j))
10875 tree var = *p;
10876 *p = DECL_CHAIN (*p);
10877 if (b == NULL_TREE)
10879 b = make_node (BLOCK);
10880 b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
10881 OMP_FOR_BODY (omp_for), b);
10882 TREE_SIDE_EFFECTS (b) = 1;
10883 OMP_FOR_BODY (omp_for) = b;
10885 DECL_CHAIN (var) = BIND_EXPR_VARS (b);
10886 BIND_EXPR_VARS (b) = var;
10887 BLOCK_VARS (BIND_EXPR_BLOCK (b)) = var;
10890 BLOCK_VARS (BIND_EXPR_BLOCK (bind)) = BIND_EXPR_VARS (bind);
10892 return bind;
10895 void
10896 finish_omp_atomic (location_t loc, enum tree_code code, enum tree_code opcode,
10897 tree lhs, tree rhs, tree v, tree lhs1, tree rhs1, tree r,
10898 tree clauses, enum omp_memory_order mo, bool weak)
10900 tree orig_lhs;
10901 tree orig_rhs;
10902 tree orig_v;
10903 tree orig_lhs1;
10904 tree orig_rhs1;
10905 tree orig_r;
10906 bool dependent_p;
10907 tree stmt;
10909 orig_lhs = lhs;
10910 orig_rhs = rhs;
10911 orig_v = v;
10912 orig_lhs1 = lhs1;
10913 orig_rhs1 = rhs1;
10914 orig_r = r;
10915 dependent_p = false;
10916 stmt = NULL_TREE;
10918 /* Even in a template, we can detect invalid uses of the atomic
10919 pragma if neither LHS nor RHS is type-dependent. */
10920 if (processing_template_decl)
10922 dependent_p = (type_dependent_expression_p (lhs)
10923 || (rhs && type_dependent_expression_p (rhs))
10924 || (v && type_dependent_expression_p (v))
10925 || (lhs1 && type_dependent_expression_p (lhs1))
10926 || (rhs1 && type_dependent_expression_p (rhs1))
10927 || (r
10928 && r != void_list_node
10929 && type_dependent_expression_p (r)));
10930 if (clauses)
10932 gcc_assert (TREE_CODE (clauses) == OMP_CLAUSE
10933 && OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_HINT
10934 && OMP_CLAUSE_CHAIN (clauses) == NULL_TREE);
10935 if (type_dependent_expression_p (OMP_CLAUSE_HINT_EXPR (clauses))
10936 || TREE_CODE (OMP_CLAUSE_HINT_EXPR (clauses)) != INTEGER_CST)
10937 dependent_p = true;
10939 if (!dependent_p)
10941 lhs = build_non_dependent_expr (lhs);
10942 if (rhs)
10943 rhs = build_non_dependent_expr (rhs);
10944 if (v)
10945 v = build_non_dependent_expr (v);
10946 if (lhs1)
10947 lhs1 = build_non_dependent_expr (lhs1);
10948 if (rhs1)
10949 rhs1 = build_non_dependent_expr (rhs1);
10950 if (r && r != void_list_node)
10951 r = build_non_dependent_expr (r);
10954 if (!dependent_p)
10956 bool swapped = false;
10957 if (rhs1 && opcode != COND_EXPR && cp_tree_equal (lhs, rhs))
10959 std::swap (rhs, rhs1);
10960 swapped = !commutative_tree_code (opcode);
10962 if (rhs1 && opcode != COND_EXPR && !cp_tree_equal (lhs, rhs1))
10964 if (code == OMP_ATOMIC)
10965 error ("%<#pragma omp atomic update%> uses two different "
10966 "expressions for memory");
10967 else
10968 error ("%<#pragma omp atomic capture%> uses two different "
10969 "expressions for memory");
10970 return;
10972 if (lhs1 && !cp_tree_equal (lhs, lhs1))
10974 if (code == OMP_ATOMIC)
10975 error ("%<#pragma omp atomic update%> uses two different "
10976 "expressions for memory");
10977 else
10978 error ("%<#pragma omp atomic capture%> uses two different "
10979 "expressions for memory");
10980 return;
10982 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs,
10983 v, lhs1, rhs1, r, swapped, mo, weak,
10984 processing_template_decl != 0);
10985 if (stmt == error_mark_node)
10986 return;
10988 if (processing_template_decl)
10990 if (code == OMP_ATOMIC_READ)
10992 stmt = build_min_nt_loc (loc, OMP_ATOMIC_READ, orig_lhs);
10993 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
10994 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
10996 else
10998 if (opcode == NOP_EXPR)
10999 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
11000 else if (opcode == COND_EXPR)
11002 stmt = build2 (EQ_EXPR, boolean_type_node, orig_lhs, orig_rhs);
11003 if (orig_r)
11004 stmt = build2 (MODIFY_EXPR, boolean_type_node, orig_r,
11005 stmt);
11006 stmt = build3 (COND_EXPR, void_type_node, stmt, orig_rhs1,
11007 orig_lhs);
11008 orig_rhs1 = NULL_TREE;
11010 else
11011 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
11012 if (orig_rhs1)
11013 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
11014 COMPOUND_EXPR, orig_rhs1, stmt);
11015 if (code != OMP_ATOMIC)
11017 stmt = build_min_nt_loc (loc, code, orig_lhs1, stmt);
11018 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
11019 OMP_ATOMIC_WEAK (stmt) = weak;
11020 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
11023 stmt = build2 (OMP_ATOMIC, void_type_node,
11024 clauses ? clauses : integer_zero_node, stmt);
11025 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
11026 OMP_ATOMIC_WEAK (stmt) = weak;
11027 SET_EXPR_LOCATION (stmt, loc);
11030 /* Avoid -Wunused-value warnings here, the whole construct has side-effects
11031 and even if it might be wrapped from fold-const.cc or c-omp.cc wrapped
11032 in some tree that appears to be unused, the value is not unused. */
11033 warning_sentinel w (warn_unused_value);
11034 finish_expr_stmt (stmt);
11037 void
11038 finish_omp_barrier (void)
11040 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
11041 releasing_vec vec;
11042 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11043 finish_expr_stmt (stmt);
11046 void
11047 finish_omp_depobj (location_t loc, tree depobj,
11048 enum omp_clause_depend_kind kind, tree clause)
11050 if (!error_operand_p (depobj) && !type_dependent_expression_p (depobj))
11052 if (!lvalue_p (depobj))
11054 error_at (EXPR_LOC_OR_LOC (depobj, loc),
11055 "%<depobj%> expression is not lvalue expression");
11056 depobj = error_mark_node;
11060 if (processing_template_decl)
11062 if (clause == NULL_TREE)
11063 clause = build_int_cst (integer_type_node, kind);
11064 add_stmt (build_min_nt_loc (loc, OMP_DEPOBJ, depobj, clause));
11065 return;
11068 if (!error_operand_p (depobj))
11070 tree addr = cp_build_addr_expr (depobj, tf_warning_or_error);
11071 if (addr == error_mark_node)
11072 depobj = error_mark_node;
11073 else
11074 depobj = cp_build_indirect_ref (loc, addr, RO_UNARY_STAR,
11075 tf_warning_or_error);
11078 c_finish_omp_depobj (loc, depobj, kind, clause);
11081 void
11082 finish_omp_flush (int mo)
11084 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
11085 releasing_vec vec;
11086 if (mo != MEMMODEL_LAST && mo != MEMMODEL_SEQ_CST)
11088 fn = builtin_decl_explicit (BUILT_IN_ATOMIC_THREAD_FENCE);
11089 vec->quick_push (build_int_cst (integer_type_node, mo));
11091 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11092 finish_expr_stmt (stmt);
11095 void
11096 finish_omp_taskwait (void)
11098 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
11099 releasing_vec vec;
11100 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11101 finish_expr_stmt (stmt);
11104 void
11105 finish_omp_taskyield (void)
11107 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
11108 releasing_vec vec;
11109 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11110 finish_expr_stmt (stmt);
11113 void
11114 finish_omp_cancel (tree clauses)
11116 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11117 int mask = 0;
11118 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
11119 mask = 1;
11120 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
11121 mask = 2;
11122 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
11123 mask = 4;
11124 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
11125 mask = 8;
11126 else
11128 error ("%<#pragma omp cancel%> must specify one of "
11129 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
11130 return;
11132 releasing_vec vec;
11133 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
11134 if (ifc != NULL_TREE)
11136 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
11137 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
11138 error_at (OMP_CLAUSE_LOCATION (ifc),
11139 "expected %<cancel%> %<if%> clause modifier");
11140 else
11142 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
11143 if (ifc2 != NULL_TREE)
11145 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
11146 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
11147 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
11148 error_at (OMP_CLAUSE_LOCATION (ifc2),
11149 "expected %<cancel%> %<if%> clause modifier");
11153 if (!processing_template_decl)
11154 ifc = maybe_convert_cond (OMP_CLAUSE_IF_EXPR (ifc));
11155 else
11156 ifc = build_x_binary_op (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11157 OMP_CLAUSE_IF_EXPR (ifc), ERROR_MARK,
11158 integer_zero_node, ERROR_MARK,
11159 NULL_TREE, NULL, tf_warning_or_error);
11161 else
11162 ifc = boolean_true_node;
11163 vec->quick_push (build_int_cst (integer_type_node, mask));
11164 vec->quick_push (ifc);
11165 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11166 finish_expr_stmt (stmt);
11169 void
11170 finish_omp_cancellation_point (tree clauses)
11172 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11173 int mask = 0;
11174 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
11175 mask = 1;
11176 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
11177 mask = 2;
11178 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
11179 mask = 4;
11180 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
11181 mask = 8;
11182 else
11184 error ("%<#pragma omp cancellation point%> must specify one of "
11185 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
11186 return;
11188 releasing_vec vec
11189 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
11190 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11191 finish_expr_stmt (stmt);
11194 /* Begin a __transaction_atomic or __transaction_relaxed statement.
11195 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
11196 should create an extra compound stmt. */
11198 tree
11199 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
11201 tree r;
11203 if (pcompound)
11204 *pcompound = begin_compound_stmt (0);
11206 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
11208 /* Only add the statement to the function if support enabled. */
11209 if (flag_tm)
11210 add_stmt (r);
11211 else
11212 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
11213 ? G_("%<__transaction_relaxed%> without "
11214 "transactional memory support enabled")
11215 : G_("%<__transaction_atomic%> without "
11216 "transactional memory support enabled")));
11218 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
11219 TREE_SIDE_EFFECTS (r) = 1;
11220 return r;
11223 /* End a __transaction_atomic or __transaction_relaxed statement.
11224 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
11225 and we should end the compound. If NOEX is non-NULL, we wrap the body in
11226 a MUST_NOT_THROW_EXPR with NOEX as condition. */
11228 void
11229 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
11231 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
11232 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
11233 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
11234 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
11236 /* noexcept specifications are not allowed for function transactions. */
11237 gcc_assert (!(noex && compound_stmt));
11238 if (noex)
11240 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
11241 noex);
11242 protected_set_expr_location
11243 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
11244 TREE_SIDE_EFFECTS (body) = 1;
11245 TRANSACTION_EXPR_BODY (stmt) = body;
11248 if (compound_stmt)
11249 finish_compound_stmt (compound_stmt);
11252 /* Build a __transaction_atomic or __transaction_relaxed expression. If
11253 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
11254 condition. */
11256 tree
11257 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
11259 tree ret;
11260 if (noex)
11262 expr = build_must_not_throw_expr (expr, noex);
11263 protected_set_expr_location (expr, loc);
11264 TREE_SIDE_EFFECTS (expr) = 1;
11266 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
11267 if (flags & TM_STMT_ATTR_RELAXED)
11268 TRANSACTION_EXPR_RELAXED (ret) = 1;
11269 TREE_SIDE_EFFECTS (ret) = 1;
11270 SET_EXPR_LOCATION (ret, loc);
11271 return ret;
11274 void
11275 init_cp_semantics (void)
11280 /* Build a STATIC_ASSERT for a static assertion with the condition
11281 CONDITION and the message text MESSAGE. LOCATION is the location
11282 of the static assertion in the source code. When MEMBER_P, this
11283 static assertion is a member of a class. If SHOW_EXPR_P is true,
11284 print the condition (because it was instantiation-dependent). */
11286 void
11287 finish_static_assert (tree condition, tree message, location_t location,
11288 bool member_p, bool show_expr_p)
11290 tsubst_flags_t complain = tf_warning_or_error;
11292 if (message == NULL_TREE
11293 || message == error_mark_node
11294 || condition == NULL_TREE
11295 || condition == error_mark_node)
11296 return;
11298 if (check_for_bare_parameter_packs (condition))
11299 condition = error_mark_node;
11301 /* Save the condition in case it was a concept check. */
11302 tree orig_condition = condition;
11304 if (instantiation_dependent_expression_p (condition))
11306 /* We're in a template; build a STATIC_ASSERT and put it in
11307 the right place. */
11308 defer:
11309 tree assertion = make_node (STATIC_ASSERT);
11310 STATIC_ASSERT_CONDITION (assertion) = orig_condition;
11311 STATIC_ASSERT_MESSAGE (assertion) = message;
11312 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
11314 if (member_p)
11315 maybe_add_class_template_decl_list (current_class_type,
11316 assertion,
11317 /*friend_p=*/0);
11318 else
11319 add_stmt (assertion);
11321 return;
11324 /* Fold the expression and convert it to a boolean value. */
11325 condition = contextual_conv_bool (condition, complain);
11326 condition = fold_non_dependent_expr (condition, complain,
11327 /*manifestly_const_eval=*/true);
11329 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
11330 /* Do nothing; the condition is satisfied. */
11332 else
11334 iloc_sentinel ils (location);
11336 if (integer_zerop (condition))
11338 /* CWG2518: static_assert failure in a template is not IFNDR. */
11339 if (processing_template_decl)
11340 goto defer;
11342 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
11343 (TREE_TYPE (TREE_TYPE (message))));
11344 int len = TREE_STRING_LENGTH (message) / sz - 1;
11346 /* See if we can find which clause was failing (for logical AND). */
11347 tree bad = find_failing_clause (NULL, orig_condition);
11348 /* If not, or its location is unusable, fall back to the previous
11349 location. */
11350 location_t cloc = cp_expr_loc_or_loc (bad, location);
11352 auto_diagnostic_group d;
11354 /* Report the error. */
11355 if (len == 0)
11356 error_at (cloc, "static assertion failed");
11357 else
11358 error_at (cloc, "static assertion failed: %s",
11359 TREE_STRING_POINTER (message));
11361 diagnose_failing_condition (bad, cloc, show_expr_p);
11363 else if (condition && condition != error_mark_node)
11365 error ("non-constant condition for static assertion");
11366 if (require_rvalue_constant_expression (condition))
11367 cxx_constant_value (condition);
11372 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
11373 suitable for use as a type-specifier.
11375 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
11376 id-expression or a class member access, FALSE when it was parsed as
11377 a full expression. */
11379 tree
11380 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
11381 tsubst_flags_t complain)
11383 tree type = NULL_TREE;
11385 if (!expr || error_operand_p (expr))
11386 return error_mark_node;
11388 if (TYPE_P (expr)
11389 || TREE_CODE (expr) == TYPE_DECL
11390 || (TREE_CODE (expr) == BIT_NOT_EXPR
11391 && TYPE_P (TREE_OPERAND (expr, 0))))
11393 if (complain & tf_error)
11394 error ("argument to %<decltype%> must be an expression");
11395 return error_mark_node;
11398 /* decltype is an unevaluated context. */
11399 cp_unevaluated u;
11401 processing_template_decl_sentinel ptds (/*reset=*/false);
11403 /* Depending on the resolution of DR 1172, we may later need to distinguish
11404 instantiation-dependent but not type-dependent expressions so that, say,
11405 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
11406 if (instantiation_dependent_uneval_expression_p (expr))
11408 type = cxx_make_type (DECLTYPE_TYPE);
11409 DECLTYPE_TYPE_EXPR (type) = expr;
11410 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
11411 = id_expression_or_member_access_p;
11412 SET_TYPE_STRUCTURAL_EQUALITY (type);
11414 return type;
11416 else if (processing_template_decl)
11418 expr = instantiate_non_dependent_expr (expr, complain|tf_decltype);
11419 if (expr == error_mark_node)
11420 return error_mark_node;
11421 /* Keep processing_template_decl cleared for the rest of the function
11422 (for sake of the call to lvalue_kind below, which handles templated
11423 and non-templated COND_EXPR differently). */
11424 processing_template_decl = 0;
11427 /* The type denoted by decltype(e) is defined as follows: */
11429 expr = resolve_nondeduced_context (expr, complain);
11430 if (!mark_single_function (expr, complain))
11431 return error_mark_node;
11433 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
11434 return error_mark_node;
11436 if (type_unknown_p (expr))
11438 if (complain & tf_error)
11439 error ("%<decltype%> cannot resolve address of overloaded function");
11440 return error_mark_node;
11443 /* To get the size of a static data member declared as an array of
11444 unknown bound, we need to instantiate it. */
11445 if (VAR_P (expr)
11446 && VAR_HAD_UNKNOWN_BOUND (expr)
11447 && DECL_TEMPLATE_INSTANTIATION (expr))
11448 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
11450 if (id_expression_or_member_access_p)
11452 /* If e is an id-expression or a class member access (5.2.5
11453 [expr.ref]), decltype(e) is defined as the type of the entity
11454 named by e. If there is no such entity, or e names a set of
11455 overloaded functions, the program is ill-formed. */
11456 if (identifier_p (expr))
11457 expr = lookup_name (expr);
11459 if (INDIRECT_REF_P (expr)
11460 || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
11461 /* This can happen when the expression is, e.g., "a.b". Just
11462 look at the underlying operand. */
11463 expr = TREE_OPERAND (expr, 0);
11465 if (TREE_CODE (expr) == OFFSET_REF
11466 || TREE_CODE (expr) == MEMBER_REF
11467 || TREE_CODE (expr) == SCOPE_REF)
11468 /* We're only interested in the field itself. If it is a
11469 BASELINK, we will need to see through it in the next
11470 step. */
11471 expr = TREE_OPERAND (expr, 1);
11473 if (BASELINK_P (expr))
11474 /* See through BASELINK nodes to the underlying function. */
11475 expr = BASELINK_FUNCTIONS (expr);
11477 /* decltype of a decomposition name drops references in the tuple case
11478 (unlike decltype of a normal variable) and keeps cv-qualifiers from
11479 the containing object in the other cases (unlike decltype of a member
11480 access expression). */
11481 if (DECL_DECOMPOSITION_P (expr))
11483 if (DECL_HAS_VALUE_EXPR_P (expr))
11484 /* Expr is an array or struct subobject proxy, handle
11485 bit-fields properly. */
11486 return unlowered_expr_type (expr);
11487 else
11488 /* Expr is a reference variable for the tuple case. */
11489 return lookup_decomp_type (expr);
11492 switch (TREE_CODE (expr))
11494 case FIELD_DECL:
11495 if (DECL_BIT_FIELD_TYPE (expr))
11497 type = DECL_BIT_FIELD_TYPE (expr);
11498 break;
11500 /* Fall through for fields that aren't bitfields. */
11501 gcc_fallthrough ();
11503 case FUNCTION_DECL:
11504 case VAR_DECL:
11505 case CONST_DECL:
11506 case PARM_DECL:
11507 case RESULT_DECL:
11508 case TEMPLATE_PARM_INDEX:
11509 expr = mark_type_use (expr);
11510 type = TREE_TYPE (expr);
11511 break;
11513 case ERROR_MARK:
11514 type = error_mark_node;
11515 break;
11517 case COMPONENT_REF:
11518 case COMPOUND_EXPR:
11519 mark_type_use (expr);
11520 type = is_bitfield_expr_with_lowered_type (expr);
11521 if (!type)
11522 type = TREE_TYPE (TREE_OPERAND (expr, 1));
11523 break;
11525 case BIT_FIELD_REF:
11526 gcc_unreachable ();
11528 case INTEGER_CST:
11529 case PTRMEM_CST:
11530 /* We can get here when the id-expression refers to an
11531 enumerator or non-type template parameter. */
11532 type = TREE_TYPE (expr);
11533 break;
11535 default:
11536 /* Handle instantiated template non-type arguments. */
11537 type = TREE_TYPE (expr);
11538 break;
11541 else
11543 /* Within a lambda-expression:
11545 Every occurrence of decltype((x)) where x is a possibly
11546 parenthesized id-expression that names an entity of
11547 automatic storage duration is treated as if x were
11548 transformed into an access to a corresponding data member
11549 of the closure type that would have been declared if x
11550 were a use of the denoted entity. */
11551 if (outer_automatic_var_p (expr)
11552 && current_function_decl
11553 && LAMBDA_FUNCTION_P (current_function_decl))
11554 type = capture_decltype (expr);
11555 else if (error_operand_p (expr))
11556 type = error_mark_node;
11557 else if (expr == current_class_ptr)
11558 /* If the expression is just "this", we want the
11559 cv-unqualified pointer for the "this" type. */
11560 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
11561 else
11563 /* Otherwise, where T is the type of e, if e is an lvalue,
11564 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
11565 cp_lvalue_kind clk = lvalue_kind (expr);
11566 type = unlowered_expr_type (expr);
11567 gcc_assert (!TYPE_REF_P (type));
11569 /* For vector types, pick a non-opaque variant. */
11570 if (VECTOR_TYPE_P (type))
11571 type = strip_typedefs (type);
11573 if (clk != clk_none && !(clk & clk_class))
11574 type = cp_build_reference_type (type, (clk & clk_rvalueref));
11578 return type;
11581 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
11582 __has_nothrow_copy, depending on assign_p. Returns true iff all
11583 the copy {ctor,assign} fns are nothrow. */
11585 static bool
11586 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
11588 tree fns = NULL_TREE;
11590 if (assign_p || TYPE_HAS_COPY_CTOR (type))
11591 fns = get_class_binding (type, assign_p ? assign_op_identifier
11592 : ctor_identifier);
11594 bool saw_copy = false;
11595 for (ovl_iterator iter (fns); iter; ++iter)
11597 tree fn = *iter;
11599 if (copy_fn_p (fn) > 0)
11601 saw_copy = true;
11602 if (!maybe_instantiate_noexcept (fn)
11603 || !TYPE_NOTHROW_P (TREE_TYPE (fn)))
11604 return false;
11608 return saw_copy;
11611 /* Return true if DERIVED is pointer interconvertible base of BASE. */
11613 static bool
11614 pointer_interconvertible_base_of_p (tree base, tree derived)
11616 if (base == error_mark_node || derived == error_mark_node)
11617 return false;
11618 base = TYPE_MAIN_VARIANT (base);
11619 derived = TYPE_MAIN_VARIANT (derived);
11620 if (!NON_UNION_CLASS_TYPE_P (base)
11621 || !NON_UNION_CLASS_TYPE_P (derived))
11622 return false;
11624 if (same_type_p (base, derived))
11625 return true;
11627 if (!std_layout_type_p (derived))
11628 return false;
11630 return uniquely_derived_from_p (base, derived);
11633 /* Helper function for fold_builtin_is_pointer_inverconvertible_with_class,
11634 return true if MEMBERTYPE is the type of the first non-static data member
11635 of TYPE or for unions of any members. */
11636 static bool
11637 first_nonstatic_data_member_p (tree type, tree membertype)
11639 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
11641 if (TREE_CODE (field) != FIELD_DECL)
11642 continue;
11643 if (DECL_FIELD_IS_BASE (field) && is_empty_field (field))
11644 continue;
11645 if (DECL_FIELD_IS_BASE (field))
11646 return first_nonstatic_data_member_p (TREE_TYPE (field), membertype);
11647 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
11649 if ((TREE_CODE (TREE_TYPE (field)) == UNION_TYPE
11650 || std_layout_type_p (TREE_TYPE (field)))
11651 && first_nonstatic_data_member_p (TREE_TYPE (field), membertype))
11652 return true;
11654 else if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field),
11655 membertype))
11656 return true;
11657 if (TREE_CODE (type) != UNION_TYPE)
11658 return false;
11660 return false;
11663 /* Fold __builtin_is_pointer_interconvertible_with_class call. */
11665 tree
11666 fold_builtin_is_pointer_inverconvertible_with_class (location_t loc, int nargs,
11667 tree *args)
11669 /* Unless users call the builtin directly, the following 3 checks should be
11670 ensured from std::is_pointer_interconvertible_with_class function
11671 template. */
11672 if (nargs != 1)
11674 error_at (loc, "%<__builtin_is_pointer_interconvertible_with_class%> "
11675 "needs a single argument");
11676 return boolean_false_node;
11678 tree arg = args[0];
11679 if (error_operand_p (arg))
11680 return boolean_false_node;
11681 if (!TYPE_PTRMEM_P (TREE_TYPE (arg)))
11683 error_at (loc, "%<__builtin_is_pointer_interconvertible_with_class%> "
11684 "argument is not pointer to member");
11685 return boolean_false_node;
11688 if (!TYPE_PTRDATAMEM_P (TREE_TYPE (arg)))
11689 return boolean_false_node;
11691 tree membertype = TREE_TYPE (TREE_TYPE (arg));
11692 tree basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg));
11693 if (!complete_type_or_else (basetype, NULL_TREE))
11694 return boolean_false_node;
11696 if (TREE_CODE (basetype) != UNION_TYPE
11697 && !std_layout_type_p (basetype))
11698 return boolean_false_node;
11700 if (!first_nonstatic_data_member_p (basetype, membertype))
11701 return boolean_false_node;
11703 if (TREE_CODE (arg) == PTRMEM_CST)
11704 arg = cplus_expand_constant (arg);
11706 if (integer_nonzerop (arg))
11707 return boolean_false_node;
11708 if (integer_zerop (arg))
11709 return boolean_true_node;
11711 return fold_build2 (EQ_EXPR, boolean_type_node, arg,
11712 build_zero_cst (TREE_TYPE (arg)));
11715 /* Helper function for is_corresponding_member_aggr. Return true if
11716 MEMBERTYPE pointer-to-data-member ARG can be found in anonymous
11717 union or structure BASETYPE. */
11719 static bool
11720 is_corresponding_member_union (tree basetype, tree membertype, tree arg)
11722 for (tree field = TYPE_FIELDS (basetype); field; field = DECL_CHAIN (field))
11723 if (TREE_CODE (field) != FIELD_DECL || DECL_BIT_FIELD_TYPE (field))
11724 continue;
11725 else if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field),
11726 membertype))
11728 if (TREE_CODE (arg) != INTEGER_CST
11729 || tree_int_cst_equal (arg, byte_position (field)))
11730 return true;
11732 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
11734 tree narg = arg;
11735 if (TREE_CODE (basetype) != UNION_TYPE
11736 && TREE_CODE (narg) == INTEGER_CST)
11737 narg = size_binop (MINUS_EXPR, arg, byte_position (field));
11738 if (is_corresponding_member_union (TREE_TYPE (field),
11739 membertype, narg))
11740 return true;
11742 return false;
11745 /* Helper function for fold_builtin_is_corresponding_member call.
11746 Return boolean_false_node if MEMBERTYPE1 BASETYPE1::*ARG1 and
11747 MEMBERTYPE2 BASETYPE2::*ARG2 aren't corresponding members,
11748 boolean_true_node if they are corresponding members, or for
11749 non-constant ARG2 the highest member offset for corresponding
11750 members. */
11752 static tree
11753 is_corresponding_member_aggr (location_t loc, tree basetype1, tree membertype1,
11754 tree arg1, tree basetype2, tree membertype2,
11755 tree arg2)
11757 tree field1 = TYPE_FIELDS (basetype1);
11758 tree field2 = TYPE_FIELDS (basetype2);
11759 tree ret = boolean_false_node;
11760 while (1)
11762 bool r = next_common_initial_sequence (field1, field2);
11763 if (field1 == NULL_TREE || field2 == NULL_TREE)
11764 break;
11765 if (r
11766 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field1),
11767 membertype1)
11768 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field2),
11769 membertype2))
11771 tree pos = byte_position (field1);
11772 if (TREE_CODE (arg1) == INTEGER_CST
11773 && tree_int_cst_equal (arg1, pos))
11775 if (TREE_CODE (arg2) == INTEGER_CST)
11776 return boolean_true_node;
11777 return pos;
11779 else if (TREE_CODE (arg1) != INTEGER_CST)
11780 ret = pos;
11782 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field1))
11783 && ANON_AGGR_TYPE_P (TREE_TYPE (field2)))
11785 if ((!lookup_attribute ("no_unique_address",
11786 DECL_ATTRIBUTES (field1)))
11787 != !lookup_attribute ("no_unique_address",
11788 DECL_ATTRIBUTES (field2)))
11789 break;
11790 if (!tree_int_cst_equal (bit_position (field1),
11791 bit_position (field2)))
11792 break;
11793 bool overlap = true;
11794 tree pos = byte_position (field1);
11795 if (TREE_CODE (arg1) == INTEGER_CST)
11797 tree off1 = fold_convert (sizetype, arg1);
11798 tree sz1 = TYPE_SIZE_UNIT (TREE_TYPE (field1));
11799 if (tree_int_cst_lt (off1, pos)
11800 || tree_int_cst_le (size_binop (PLUS_EXPR, pos, sz1), off1))
11801 overlap = false;
11803 if (TREE_CODE (arg2) == INTEGER_CST)
11805 tree off2 = fold_convert (sizetype, arg2);
11806 tree sz2 = TYPE_SIZE_UNIT (TREE_TYPE (field2));
11807 if (tree_int_cst_lt (off2, pos)
11808 || tree_int_cst_le (size_binop (PLUS_EXPR, pos, sz2), off2))
11809 overlap = false;
11811 if (overlap
11812 && NON_UNION_CLASS_TYPE_P (TREE_TYPE (field1))
11813 && NON_UNION_CLASS_TYPE_P (TREE_TYPE (field2)))
11815 tree narg1 = arg1;
11816 if (TREE_CODE (arg1) == INTEGER_CST)
11817 narg1 = size_binop (MINUS_EXPR,
11818 fold_convert (sizetype, arg1), pos);
11819 tree narg2 = arg2;
11820 if (TREE_CODE (arg2) == INTEGER_CST)
11821 narg2 = size_binop (MINUS_EXPR,
11822 fold_convert (sizetype, arg2), pos);
11823 tree t1 = TREE_TYPE (field1);
11824 tree t2 = TREE_TYPE (field2);
11825 tree nret = is_corresponding_member_aggr (loc, t1, membertype1,
11826 narg1, t2, membertype2,
11827 narg2);
11828 if (nret != boolean_false_node)
11830 if (nret == boolean_true_node)
11831 return nret;
11832 if (TREE_CODE (arg1) == INTEGER_CST)
11833 return size_binop (PLUS_EXPR, nret, pos);
11834 ret = size_binop (PLUS_EXPR, nret, pos);
11837 else if (overlap
11838 && TREE_CODE (TREE_TYPE (field1)) == UNION_TYPE
11839 && TREE_CODE (TREE_TYPE (field2)) == UNION_TYPE)
11841 tree narg1 = arg1;
11842 if (TREE_CODE (arg1) == INTEGER_CST)
11843 narg1 = size_binop (MINUS_EXPR,
11844 fold_convert (sizetype, arg1), pos);
11845 tree narg2 = arg2;
11846 if (TREE_CODE (arg2) == INTEGER_CST)
11847 narg2 = size_binop (MINUS_EXPR,
11848 fold_convert (sizetype, arg2), pos);
11849 if (is_corresponding_member_union (TREE_TYPE (field1),
11850 membertype1, narg1)
11851 && is_corresponding_member_union (TREE_TYPE (field2),
11852 membertype2, narg2))
11854 sorry_at (loc, "%<__builtin_is_corresponding_member%> "
11855 "not well defined for anonymous unions");
11856 return boolean_false_node;
11860 if (!r)
11861 break;
11862 field1 = DECL_CHAIN (field1);
11863 field2 = DECL_CHAIN (field2);
11865 return ret;
11868 /* Fold __builtin_is_corresponding_member call. */
11870 tree
11871 fold_builtin_is_corresponding_member (location_t loc, int nargs,
11872 tree *args)
11874 /* Unless users call the builtin directly, the following 3 checks should be
11875 ensured from std::is_corresponding_member function template. */
11876 if (nargs != 2)
11878 error_at (loc, "%<__builtin_is_corresponding_member%> "
11879 "needs two arguments");
11880 return boolean_false_node;
11882 tree arg1 = args[0];
11883 tree arg2 = args[1];
11884 if (error_operand_p (arg1) || error_operand_p (arg2))
11885 return boolean_false_node;
11886 if (!TYPE_PTRMEM_P (TREE_TYPE (arg1))
11887 || !TYPE_PTRMEM_P (TREE_TYPE (arg2)))
11889 error_at (loc, "%<__builtin_is_corresponding_member%> "
11890 "argument is not pointer to member");
11891 return boolean_false_node;
11894 if (!TYPE_PTRDATAMEM_P (TREE_TYPE (arg1))
11895 || !TYPE_PTRDATAMEM_P (TREE_TYPE (arg2)))
11896 return boolean_false_node;
11898 tree membertype1 = TREE_TYPE (TREE_TYPE (arg1));
11899 tree basetype1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg1));
11900 if (!complete_type_or_else (basetype1, NULL_TREE))
11901 return boolean_false_node;
11903 tree membertype2 = TREE_TYPE (TREE_TYPE (arg2));
11904 tree basetype2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg2));
11905 if (!complete_type_or_else (basetype2, NULL_TREE))
11906 return boolean_false_node;
11908 if (!NON_UNION_CLASS_TYPE_P (basetype1)
11909 || !NON_UNION_CLASS_TYPE_P (basetype2)
11910 || !std_layout_type_p (basetype1)
11911 || !std_layout_type_p (basetype2))
11912 return boolean_false_node;
11914 /* If the member types aren't layout compatible, then they
11915 can't be corresponding members. */
11916 if (!layout_compatible_type_p (membertype1, membertype2))
11917 return boolean_false_node;
11919 if (TREE_CODE (arg1) == PTRMEM_CST)
11920 arg1 = cplus_expand_constant (arg1);
11921 if (TREE_CODE (arg2) == PTRMEM_CST)
11922 arg2 = cplus_expand_constant (arg2);
11924 if (null_member_pointer_value_p (arg1)
11925 || null_member_pointer_value_p (arg2))
11926 return boolean_false_node;
11928 if (TREE_CODE (arg1) == INTEGER_CST
11929 && TREE_CODE (arg2) == INTEGER_CST
11930 && !tree_int_cst_equal (arg1, arg2))
11931 return boolean_false_node;
11933 if (TREE_CODE (arg2) == INTEGER_CST
11934 && TREE_CODE (arg1) != INTEGER_CST)
11936 std::swap (arg1, arg2);
11937 std::swap (membertype1, membertype2);
11938 std::swap (basetype1, basetype2);
11941 tree ret = is_corresponding_member_aggr (loc, basetype1, membertype1, arg1,
11942 basetype2, membertype2, arg2);
11943 if (TREE_TYPE (ret) == boolean_type_node)
11944 return ret;
11945 /* If both arg1 and arg2 are INTEGER_CSTs, is_corresponding_member_aggr
11946 already returns boolean_{true,false}_node whether those particular
11947 members are corresponding members or not. Otherwise, if only
11948 one of them is INTEGER_CST (canonicalized to first being INTEGER_CST
11949 above), it returns boolean_false_node if it is certainly not a
11950 corresponding member and otherwise we need to do a runtime check that
11951 those two OFFSET_TYPE offsets are equal.
11952 If neither of the operands is INTEGER_CST, is_corresponding_member_aggr
11953 returns the largest offset at which the members would be corresponding
11954 members, so perform arg1 <= ret && arg1 == arg2 runtime check. */
11955 gcc_assert (TREE_CODE (arg2) != INTEGER_CST);
11956 if (TREE_CODE (arg1) == INTEGER_CST)
11957 return fold_build2 (EQ_EXPR, boolean_type_node, arg1,
11958 fold_convert (TREE_TYPE (arg1), arg2));
11959 ret = fold_build2 (LE_EXPR, boolean_type_node,
11960 fold_convert (pointer_sized_int_node, arg1),
11961 fold_convert (pointer_sized_int_node, ret));
11962 return fold_build2 (TRUTH_AND_EXPR, boolean_type_node, ret,
11963 fold_build2 (EQ_EXPR, boolean_type_node, arg1,
11964 fold_convert (TREE_TYPE (arg1), arg2)));
11967 /* Actually evaluates the trait. */
11969 static bool
11970 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
11972 enum tree_code type_code1;
11973 tree t;
11975 type_code1 = TREE_CODE (type1);
11977 switch (kind)
11979 case CPTK_HAS_NOTHROW_ASSIGN:
11980 type1 = strip_array_types (type1);
11981 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
11982 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
11983 || (CLASS_TYPE_P (type1)
11984 && classtype_has_nothrow_assign_or_copy_p (type1,
11985 true))));
11987 case CPTK_HAS_TRIVIAL_ASSIGN:
11988 /* ??? The standard seems to be missing the "or array of such a class
11989 type" wording for this trait. */
11990 type1 = strip_array_types (type1);
11991 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
11992 && (trivial_type_p (type1)
11993 || (CLASS_TYPE_P (type1)
11994 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
11996 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
11997 type1 = strip_array_types (type1);
11998 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
11999 || (CLASS_TYPE_P (type1)
12000 && (t = locate_ctor (type1))
12001 && maybe_instantiate_noexcept (t)
12002 && TYPE_NOTHROW_P (TREE_TYPE (t))));
12004 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
12005 type1 = strip_array_types (type1);
12006 return (trivial_type_p (type1)
12007 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
12009 case CPTK_HAS_NOTHROW_COPY:
12010 type1 = strip_array_types (type1);
12011 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
12012 || (CLASS_TYPE_P (type1)
12013 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
12015 case CPTK_HAS_TRIVIAL_COPY:
12016 /* ??? The standard seems to be missing the "or array of such a class
12017 type" wording for this trait. */
12018 type1 = strip_array_types (type1);
12019 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
12020 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
12022 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
12023 type1 = strip_array_types (type1);
12024 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
12025 || (CLASS_TYPE_P (type1)
12026 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
12028 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
12029 return type_has_virtual_destructor (type1);
12031 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
12032 return type_has_unique_obj_representations (type1);
12034 case CPTK_IS_ABSTRACT:
12035 return ABSTRACT_CLASS_TYPE_P (type1);
12037 case CPTK_IS_AGGREGATE:
12038 return CP_AGGREGATE_TYPE_P (type1);
12040 case CPTK_IS_BASE_OF:
12041 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
12042 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
12043 || DERIVED_FROM_P (type1, type2)));
12045 case CPTK_IS_CLASS:
12046 return NON_UNION_CLASS_TYPE_P (type1);
12048 case CPTK_IS_EMPTY:
12049 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
12051 case CPTK_IS_ENUM:
12052 return type_code1 == ENUMERAL_TYPE;
12054 case CPTK_IS_FINAL:
12055 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
12057 case CPTK_IS_LAYOUT_COMPATIBLE:
12058 return layout_compatible_type_p (type1, type2);
12060 case CPTK_IS_LITERAL_TYPE:
12061 return literal_type_p (type1);
12063 case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
12064 return pointer_interconvertible_base_of_p (type1, type2);
12066 case CPTK_IS_POD:
12067 return pod_type_p (type1);
12069 case CPTK_IS_POLYMORPHIC:
12070 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
12072 case CPTK_IS_SAME:
12073 return same_type_p (type1, type2);
12075 case CPTK_IS_STD_LAYOUT:
12076 return std_layout_type_p (type1);
12078 case CPTK_IS_TRIVIAL:
12079 return trivial_type_p (type1);
12081 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
12082 return is_trivially_xible (MODIFY_EXPR, type1, type2);
12084 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
12085 return is_trivially_xible (INIT_EXPR, type1, type2);
12087 case CPTK_IS_TRIVIALLY_COPYABLE:
12088 return trivially_copyable_p (type1);
12090 case CPTK_IS_UNION:
12091 return type_code1 == UNION_TYPE;
12093 case CPTK_IS_ASSIGNABLE:
12094 return is_xible (MODIFY_EXPR, type1, type2);
12096 case CPTK_IS_CONSTRUCTIBLE:
12097 return is_xible (INIT_EXPR, type1, type2);
12099 case CPTK_IS_NOTHROW_ASSIGNABLE:
12100 return is_nothrow_xible (MODIFY_EXPR, type1, type2);
12102 case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
12103 return is_nothrow_xible (INIT_EXPR, type1, type2);
12105 case CPTK_IS_CONVERTIBLE:
12106 return is_convertible (type1, type2);
12108 case CPTK_IS_NOTHROW_CONVERTIBLE:
12109 return is_nothrow_convertible (type1, type2);
12111 case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
12112 return ref_xes_from_temporary (type1, type2, /*direct_init=*/true);
12114 case CPTK_REF_CONVERTS_FROM_TEMPORARY:
12115 return ref_xes_from_temporary (type1, type2, /*direct_init=*/false);
12117 case CPTK_IS_DEDUCIBLE:
12118 return type_targs_deducible_from (type1, type2);
12120 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
12121 case CPTK_##CODE:
12122 #include "cp-trait.def"
12123 #undef DEFTRAIT_TYPE
12124 /* Type-yielding traits are handled in finish_trait_type. */
12125 break;
12128 gcc_unreachable ();
12131 /* Returns true if TYPE meets the requirements for the specified KIND,
12132 false otherwise.
12134 When KIND == 1, TYPE must be an array of unknown bound,
12135 or (possibly cv-qualified) void, or a complete type.
12137 When KIND == 2, TYPE must be a complete type, or array of complete type,
12138 or (possibly cv-qualified) void.
12140 When KIND == 3:
12141 If TYPE is a non-union class type, it must be complete.
12143 When KIND == 4:
12144 If TYPE is a class type, it must be complete. */
12146 static bool
12147 check_trait_type (tree type, int kind = 1)
12149 if (type == NULL_TREE)
12150 return true;
12152 if (TREE_CODE (type) == TREE_VEC)
12154 for (tree arg : tree_vec_range (type))
12155 if (!check_trait_type (arg, kind))
12156 return false;
12157 return true;
12160 if (kind == 1 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
12161 return true; // Array of unknown bound. Don't care about completeness.
12163 if (kind == 3 && !NON_UNION_CLASS_TYPE_P (type))
12164 return true; // Not a non-union class type. Don't care about completeness.
12166 if (kind == 4 && TREE_CODE (type) == ARRAY_TYPE)
12167 return true; // Not a class type. Don't care about completeness.
12169 if (VOID_TYPE_P (type))
12170 return true;
12172 type = complete_type (strip_array_types (type));
12173 if (!COMPLETE_TYPE_P (type)
12174 && cxx_incomplete_type_diagnostic (NULL_TREE, type, DK_PERMERROR)
12175 && !flag_permissive)
12176 return false;
12177 return true;
12180 /* Process a trait expression. */
12182 tree
12183 finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
12185 if (type1 == error_mark_node
12186 || type2 == error_mark_node)
12187 return error_mark_node;
12189 if (processing_template_decl)
12191 tree trait_expr = make_node (TRAIT_EXPR);
12192 TREE_TYPE (trait_expr) = boolean_type_node;
12193 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
12194 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
12195 TRAIT_EXPR_KIND (trait_expr) = kind;
12196 TRAIT_EXPR_LOCATION (trait_expr) = loc;
12197 return trait_expr;
12200 switch (kind)
12202 case CPTK_HAS_NOTHROW_ASSIGN:
12203 case CPTK_HAS_TRIVIAL_ASSIGN:
12204 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
12205 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
12206 case CPTK_HAS_NOTHROW_COPY:
12207 case CPTK_HAS_TRIVIAL_COPY:
12208 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
12209 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
12210 if (!check_trait_type (type1))
12211 return error_mark_node;
12212 break;
12214 case CPTK_IS_LITERAL_TYPE:
12215 case CPTK_IS_POD:
12216 case CPTK_IS_STD_LAYOUT:
12217 case CPTK_IS_TRIVIAL:
12218 case CPTK_IS_TRIVIALLY_COPYABLE:
12219 if (!check_trait_type (type1, /* kind = */ 2))
12220 return error_mark_node;
12221 break;
12223 case CPTK_IS_EMPTY:
12224 case CPTK_IS_POLYMORPHIC:
12225 case CPTK_IS_ABSTRACT:
12226 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
12227 if (!check_trait_type (type1, /* kind = */ 3))
12228 return error_mark_node;
12229 break;
12231 /* N.B. std::is_aggregate is kind=2 but we don't need a complete element
12232 type to know whether an array is an aggregate, so use kind=4 here. */
12233 case CPTK_IS_AGGREGATE:
12234 case CPTK_IS_FINAL:
12235 if (!check_trait_type (type1, /* kind = */ 4))
12236 return error_mark_node;
12237 break;
12239 case CPTK_IS_ASSIGNABLE:
12240 case CPTK_IS_CONSTRUCTIBLE:
12241 if (!check_trait_type (type1))
12242 return error_mark_node;
12243 break;
12245 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
12246 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
12247 case CPTK_IS_NOTHROW_ASSIGNABLE:
12248 case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
12249 case CPTK_IS_CONVERTIBLE:
12250 case CPTK_IS_NOTHROW_CONVERTIBLE:
12251 case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
12252 case CPTK_REF_CONVERTS_FROM_TEMPORARY:
12253 if (!check_trait_type (type1)
12254 || !check_trait_type (type2))
12255 return error_mark_node;
12256 break;
12258 case CPTK_IS_BASE_OF:
12259 case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
12260 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
12261 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
12262 && !complete_type_or_else (type2, NULL_TREE))
12263 /* We already issued an error. */
12264 return error_mark_node;
12265 break;
12267 case CPTK_IS_CLASS:
12268 case CPTK_IS_ENUM:
12269 case CPTK_IS_UNION:
12270 case CPTK_IS_SAME:
12271 break;
12273 case CPTK_IS_LAYOUT_COMPATIBLE:
12274 if (!array_of_unknown_bound_p (type1)
12275 && TREE_CODE (type1) != VOID_TYPE
12276 && !complete_type_or_else (type1, NULL_TREE))
12277 /* We already issued an error. */
12278 return error_mark_node;
12279 if (!array_of_unknown_bound_p (type2)
12280 && TREE_CODE (type2) != VOID_TYPE
12281 && !complete_type_or_else (type2, NULL_TREE))
12282 /* We already issued an error. */
12283 return error_mark_node;
12284 break;
12286 case CPTK_IS_DEDUCIBLE:
12287 if (!DECL_TYPE_TEMPLATE_P (type1))
12289 error ("%qD is not a class or alias template", type1);
12290 return error_mark_node;
12292 break;
12294 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
12295 case CPTK_##CODE:
12296 #include "cp-trait.def"
12297 #undef DEFTRAIT_TYPE
12298 /* Type-yielding traits are handled in finish_trait_type. */
12299 gcc_unreachable ();
12302 tree val = (trait_expr_value (kind, type1, type2)
12303 ? boolean_true_node : boolean_false_node);
12304 return maybe_wrap_with_location (val, loc);
12307 /* Process a trait type. */
12309 tree
12310 finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
12311 tsubst_flags_t complain)
12313 if (type1 == error_mark_node
12314 || type2 == error_mark_node)
12315 return error_mark_node;
12317 if (processing_template_decl)
12319 tree type = cxx_make_type (TRAIT_TYPE);
12320 TRAIT_TYPE_TYPE1 (type) = type1;
12321 TRAIT_TYPE_TYPE2 (type) = type2;
12322 TRAIT_TYPE_KIND_RAW (type) = build_int_cstu (integer_type_node, kind);
12323 /* These traits are intended to be used in the definition of the ::type
12324 member of the corresponding standard library type trait and aren't
12325 mangleable (and thus won't appear directly in template signatures),
12326 so structural equality should suffice. */
12327 SET_TYPE_STRUCTURAL_EQUALITY (type);
12328 return type;
12331 switch (kind)
12333 case CPTK_UNDERLYING_TYPE:
12334 return finish_underlying_type (type1);
12336 case CPTK_REMOVE_CV:
12337 return cv_unqualified (type1);
12339 case CPTK_REMOVE_REFERENCE:
12340 if (TYPE_REF_P (type1))
12341 type1 = TREE_TYPE (type1);
12342 return type1;
12344 case CPTK_REMOVE_CVREF:
12345 if (TYPE_REF_P (type1))
12346 type1 = TREE_TYPE (type1);
12347 return cv_unqualified (type1);
12349 case CPTK_TYPE_PACK_ELEMENT:
12350 return finish_type_pack_element (type1, type2, complain);
12352 #define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
12353 case CPTK_##CODE:
12354 #include "cp-trait.def"
12355 #undef DEFTRAIT_EXPR
12356 /* Expression-yielding traits are handled in finish_trait_expr. */
12357 case CPTK_BASES:
12358 case CPTK_DIRECT_BASES:
12359 /* BASES and DIRECT_BASES are handled in finish_bases. */
12360 break;
12363 gcc_unreachable ();
12366 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
12367 which is ignored for C++. */
12369 void
12370 set_float_const_decimal64 (void)
12374 void
12375 clear_float_const_decimal64 (void)
12379 bool
12380 float_const_decimal64_p (void)
12382 return 0;
12386 /* Return true if T designates the implied `this' parameter. */
12388 bool
12389 is_this_parameter (tree t)
12391 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
12392 return false;
12393 gcc_assert (TREE_CODE (t) == PARM_DECL
12394 || (VAR_P (t) && DECL_HAS_VALUE_EXPR_P (t))
12395 || (cp_binding_oracle && VAR_P (t)));
12396 return true;
12399 /* Insert the deduced return type for an auto function. */
12401 void
12402 apply_deduced_return_type (tree fco, tree return_type)
12404 tree result;
12406 if (return_type == error_mark_node)
12407 return;
12409 if (DECL_CONV_FN_P (fco))
12410 DECL_NAME (fco) = make_conv_op_name (return_type);
12412 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
12414 maybe_update_postconditions (fco);
12416 /* Apply the type to the result object. */
12418 result = DECL_RESULT (fco);
12419 if (result == NULL_TREE)
12420 return;
12421 if (TREE_TYPE (result) == return_type)
12422 return;
12424 if (!processing_template_decl && !VOID_TYPE_P (return_type)
12425 && !complete_type_or_else (return_type, NULL_TREE))
12426 return;
12428 /* We already have a DECL_RESULT from start_preparsed_function.
12429 Now we need to redo the work it and allocate_struct_function
12430 did to reflect the new type. */
12431 result = build_decl (DECL_SOURCE_LOCATION (result), RESULT_DECL, NULL_TREE,
12432 TYPE_MAIN_VARIANT (return_type));
12433 DECL_ARTIFICIAL (result) = 1;
12434 DECL_IGNORED_P (result) = 1;
12435 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
12436 result);
12437 DECL_RESULT (fco) = result;
12439 if (!processing_template_decl)
12440 if (function *fun = DECL_STRUCT_FUNCTION (fco))
12442 bool aggr = aggregate_value_p (result, fco);
12443 #ifdef PCC_STATIC_STRUCT_RETURN
12444 fun->returns_pcc_struct = aggr;
12445 #endif
12446 fun->returns_struct = aggr;
12450 /* DECL is a local variable or parameter from the surrounding scope of a
12451 lambda-expression. Returns the decltype for a use of the capture field
12452 for DECL even if it hasn't been captured yet. */
12454 static tree
12455 capture_decltype (tree decl)
12457 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
12458 tree cap = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
12459 LOOK_want::HIDDEN_LAMBDA);
12460 tree type;
12462 if (cap && is_capture_proxy (cap))
12463 type = TREE_TYPE (cap);
12464 else
12465 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
12467 case CPLD_NONE:
12468 error ("%qD is not captured", decl);
12469 return error_mark_node;
12471 case CPLD_COPY:
12472 type = TREE_TYPE (decl);
12473 if (TYPE_REF_P (type)
12474 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
12475 type = TREE_TYPE (type);
12476 break;
12478 case CPLD_REFERENCE:
12479 type = TREE_TYPE (decl);
12480 if (!TYPE_REF_P (type))
12481 type = build_reference_type (TREE_TYPE (decl));
12482 break;
12484 default:
12485 gcc_unreachable ();
12488 if (!TYPE_REF_P (type))
12490 if (!LAMBDA_EXPR_MUTABLE_P (lam))
12491 type = cp_build_qualified_type (type, (cp_type_quals (type)
12492 |TYPE_QUAL_CONST));
12493 type = build_reference_type (type);
12495 return type;
12498 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
12499 this is a right unary fold. Otherwise it is a left unary fold. */
12501 static tree
12502 finish_unary_fold_expr (tree expr, int op, tree_code dir)
12504 /* Build a pack expansion (assuming expr has pack type). */
12505 if (!uses_parameter_packs (expr))
12507 error_at (location_of (expr), "operand of fold expression has no "
12508 "unexpanded parameter packs");
12509 return error_mark_node;
12511 tree pack = make_pack_expansion (expr);
12513 /* Build the fold expression. */
12514 tree code = build_int_cstu (integer_type_node, abs (op));
12515 tree fold = build_min_nt_loc (input_location, dir, code, pack);
12516 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
12517 TREE_TYPE (fold) = build_dependent_operator_type (NULL_TREE,
12518 FOLD_EXPR_OP (fold),
12519 FOLD_EXPR_MODIFY_P (fold));
12520 return fold;
12523 tree
12524 finish_left_unary_fold_expr (tree expr, int op)
12526 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
12529 tree
12530 finish_right_unary_fold_expr (tree expr, int op)
12532 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
12535 /* Build a binary fold expression over EXPR1 and EXPR2. The
12536 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
12537 has an unexpanded parameter pack). */
12539 tree
12540 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
12542 pack = make_pack_expansion (pack);
12543 tree code = build_int_cstu (integer_type_node, abs (op));
12544 tree fold = build_min_nt_loc (input_location, dir, code, pack, init);
12545 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
12546 TREE_TYPE (fold) = build_dependent_operator_type (NULL_TREE,
12547 FOLD_EXPR_OP (fold),
12548 FOLD_EXPR_MODIFY_P (fold));
12549 return fold;
12552 tree
12553 finish_binary_fold_expr (tree expr1, tree expr2, int op)
12555 // Determine which expr has an unexpanded parameter pack and
12556 // set the pack and initial term.
12557 bool pack1 = uses_parameter_packs (expr1);
12558 bool pack2 = uses_parameter_packs (expr2);
12559 if (pack1 && !pack2)
12560 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
12561 else if (pack2 && !pack1)
12562 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
12563 else
12565 if (pack1)
12566 error ("both arguments in binary fold have unexpanded parameter packs");
12567 else
12568 error ("no unexpanded parameter packs in binary fold");
12570 return error_mark_node;
12573 /* Finish __builtin_launder (arg). */
12575 tree
12576 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
12578 tree orig_arg = arg;
12579 if (!type_dependent_expression_p (arg))
12580 arg = decay_conversion (arg, complain);
12581 if (error_operand_p (arg))
12582 return error_mark_node;
12583 if (!type_dependent_expression_p (arg)
12584 && !TYPE_PTR_P (TREE_TYPE (arg)))
12586 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
12587 return error_mark_node;
12589 if (processing_template_decl)
12590 arg = orig_arg;
12591 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
12592 TREE_TYPE (arg), 1, arg);
12595 /* Finish __builtin_convertvector (arg, type). */
12597 tree
12598 cp_build_vec_convert (tree arg, location_t loc, tree type,
12599 tsubst_flags_t complain)
12601 if (error_operand_p (type))
12602 return error_mark_node;
12603 if (error_operand_p (arg))
12604 return error_mark_node;
12606 tree ret = NULL_TREE;
12607 if (!type_dependent_expression_p (arg) && !dependent_type_p (type))
12608 ret = c_build_vec_convert (cp_expr_loc_or_input_loc (arg),
12609 decay_conversion (arg, complain),
12610 loc, type, (complain & tf_error) != 0);
12612 if (!processing_template_decl)
12613 return ret;
12615 return build_call_expr_internal_loc (loc, IFN_VEC_CONVERT, type, 1, arg);
12618 /* Finish __builtin_bit_cast (type, arg). */
12620 tree
12621 cp_build_bit_cast (location_t loc, tree type, tree arg,
12622 tsubst_flags_t complain)
12624 if (error_operand_p (type))
12625 return error_mark_node;
12626 if (!dependent_type_p (type))
12628 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
12629 return error_mark_node;
12630 if (TREE_CODE (type) == ARRAY_TYPE)
12632 /* std::bit_cast for destination ARRAY_TYPE is not possible,
12633 as functions may not return an array, so don't bother trying
12634 to support this (and then deal with VLAs etc.). */
12635 error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
12636 "is an array type", type);
12637 return error_mark_node;
12639 if (!trivially_copyable_p (type))
12641 error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
12642 "is not trivially copyable", type);
12643 return error_mark_node;
12647 if (error_operand_p (arg))
12648 return error_mark_node;
12650 if (!type_dependent_expression_p (arg))
12652 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
12654 /* Don't perform array-to-pointer conversion. */
12655 arg = mark_rvalue_use (arg, loc, true);
12656 if (!complete_type_or_maybe_complain (TREE_TYPE (arg), arg, complain))
12657 return error_mark_node;
12659 else
12660 arg = decay_conversion (arg, complain);
12662 if (error_operand_p (arg))
12663 return error_mark_node;
12665 if (!trivially_copyable_p (TREE_TYPE (arg)))
12667 error_at (cp_expr_loc_or_loc (arg, loc),
12668 "%<__builtin_bit_cast%> source type %qT "
12669 "is not trivially copyable", TREE_TYPE (arg));
12670 return error_mark_node;
12672 if (!dependent_type_p (type)
12673 && !cp_tree_equal (TYPE_SIZE_UNIT (type),
12674 TYPE_SIZE_UNIT (TREE_TYPE (arg))))
12676 error_at (loc, "%<__builtin_bit_cast%> source size %qE "
12677 "not equal to destination type size %qE",
12678 TYPE_SIZE_UNIT (TREE_TYPE (arg)),
12679 TYPE_SIZE_UNIT (type));
12680 return error_mark_node;
12684 tree ret = build_min (BIT_CAST_EXPR, type, arg);
12685 SET_EXPR_LOCATION (ret, loc);
12687 if (!processing_template_decl && CLASS_TYPE_P (type))
12688 ret = get_target_expr (ret, complain);
12690 return ret;
12693 #include "gt-cp-semantics.h"