Require target lra in gcc.c-torture/compile/asmgoto-6.c
[official-gcc.git] / gcc / cp / semantics.cc
blob8fb47fd179eb2af2e82bf31d188023e9b9d41de9
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;
4941 bool simple;
4944 /* Helper function for walk_tree, used by finalize_nrv below. */
4946 static tree
4947 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
4949 class nrv_data *dp = (class nrv_data *)data;
4950 tree_node **slot;
4952 /* No need to walk into types. There wouldn't be any need to walk into
4953 non-statements, except that we have to consider STMT_EXPRs. */
4954 if (TYPE_P (*tp))
4955 *walk_subtrees = 0;
4956 /* If there's a label, we might need to destroy the NRV on goto (92407). */
4957 else if (TREE_CODE (*tp) == LABEL_EXPR)
4958 dp->simple = false;
4959 /* Change NRV returns to just refer to the RESULT_DECL; this is a nop,
4960 but differs from using NULL_TREE in that it indicates that we care
4961 about the value of the RESULT_DECL. But preserve anything appended
4962 by check_return_expr. */
4963 else if (TREE_CODE (*tp) == RETURN_EXPR)
4965 tree *p = &TREE_OPERAND (*tp, 0);
4966 while (TREE_CODE (*p) == COMPOUND_EXPR)
4967 p = &TREE_OPERAND (*p, 0);
4968 if (TREE_CODE (*p) == INIT_EXPR
4969 && INIT_EXPR_NRV_P (*p))
4970 *p = dp->result;
4972 /* Change all cleanups for the NRV to only run when not returning. */
4973 else if (TREE_CODE (*tp) == CLEANUP_STMT
4974 && CLEANUP_DECL (*tp) == dp->var)
4976 if (dp->simple)
4977 CLEANUP_EH_ONLY (*tp) = true;
4978 else
4980 tree cond = build3 (COND_EXPR, void_type_node,
4981 current_retval_sentinel,
4982 void_node, CLEANUP_EXPR (*tp));
4983 CLEANUP_EXPR (*tp) = cond;
4986 /* Replace the DECL_EXPR for the NRV with an initialization of the
4987 RESULT_DECL, if needed. */
4988 else if (TREE_CODE (*tp) == DECL_EXPR
4989 && DECL_EXPR_DECL (*tp) == dp->var)
4991 tree init;
4992 if (DECL_INITIAL (dp->var)
4993 && DECL_INITIAL (dp->var) != error_mark_node)
4994 init = cp_build_init_expr (dp->result,
4995 DECL_INITIAL (dp->var));
4996 else
4997 init = build_empty_stmt (EXPR_LOCATION (*tp));
4998 DECL_INITIAL (dp->var) = NULL_TREE;
4999 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
5000 *tp = init;
5002 /* And replace all uses of the NRV with the RESULT_DECL. */
5003 else if (*tp == dp->var)
5004 *tp = dp->result;
5006 /* Avoid walking into the same tree more than once. Unfortunately, we
5007 can't just use walk_tree_without duplicates because it would only call
5008 us for the first occurrence of dp->var in the function body. */
5009 slot = dp->visited.find_slot (*tp, INSERT);
5010 if (*slot)
5011 *walk_subtrees = 0;
5012 else
5013 *slot = *tp;
5015 /* Keep iterating. */
5016 return NULL_TREE;
5019 /* Called from finish_function to implement the named return value
5020 optimization by overriding all the RETURN_EXPRs and pertinent
5021 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
5022 RESULT_DECL for the function. */
5024 void
5025 finalize_nrv (tree fndecl, tree var)
5027 class nrv_data data;
5028 tree result = DECL_RESULT (fndecl);
5030 /* Copy name from VAR to RESULT. */
5031 DECL_NAME (result) = DECL_NAME (var);
5032 /* Don't forget that we take its address. */
5033 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
5034 /* Finally set DECL_VALUE_EXPR to avoid assigning
5035 a stack slot at -O0 for the original var and debug info
5036 uses RESULT location for VAR. */
5037 SET_DECL_VALUE_EXPR (var, result);
5038 DECL_HAS_VALUE_EXPR_P (var) = 1;
5040 data.var = var;
5041 data.result = result;
5043 /* This is simpler for variables declared in the outer scope of
5044 the function so we know that their lifetime always ends with a
5045 return; see g++.dg/opt/nrv6.C. */
5046 tree outer = outer_curly_brace_block (fndecl);
5047 data.simple = chain_member (var, BLOCK_VARS (outer));
5049 cp_walk_tree (&DECL_SAVED_TREE (fndecl), finalize_nrv_r, &data, 0);
5052 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
5054 bool
5055 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
5056 bool need_copy_ctor, bool need_copy_assignment,
5057 bool need_dtor)
5059 int save_errorcount = errorcount;
5060 tree info, t;
5062 /* Always allocate 3 elements for simplicity. These are the
5063 function decls for the ctor, dtor, and assignment op.
5064 This layout is known to the three lang hooks,
5065 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
5066 and cxx_omp_clause_assign_op. */
5067 info = make_tree_vec (3);
5068 CP_OMP_CLAUSE_INFO (c) = info;
5070 if (need_default_ctor || need_copy_ctor)
5072 if (need_default_ctor)
5073 t = get_default_ctor (type);
5074 else
5075 t = get_copy_ctor (type, tf_warning_or_error);
5077 if (t && !trivial_fn_p (t))
5078 TREE_VEC_ELT (info, 0) = t;
5081 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5082 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
5084 if (need_copy_assignment)
5086 t = get_copy_assign (type);
5088 if (t && !trivial_fn_p (t))
5089 TREE_VEC_ELT (info, 2) = t;
5092 return errorcount != save_errorcount;
5095 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
5096 FIELD_DECL, otherwise return DECL itself. */
5098 static tree
5099 omp_clause_decl_field (tree decl)
5101 if (VAR_P (decl)
5102 && DECL_HAS_VALUE_EXPR_P (decl)
5103 && DECL_ARTIFICIAL (decl)
5104 && DECL_LANG_SPECIFIC (decl)
5105 && DECL_OMP_PRIVATIZED_MEMBER (decl))
5107 tree f = DECL_VALUE_EXPR (decl);
5108 if (INDIRECT_REF_P (f))
5109 f = TREE_OPERAND (f, 0);
5110 if (TREE_CODE (f) == COMPONENT_REF)
5112 f = TREE_OPERAND (f, 1);
5113 gcc_assert (TREE_CODE (f) == FIELD_DECL);
5114 return f;
5117 return NULL_TREE;
5120 /* Adjust DECL if needed for printing using %qE. */
5122 static tree
5123 omp_clause_printable_decl (tree decl)
5125 tree t = omp_clause_decl_field (decl);
5126 if (t)
5127 return t;
5128 return decl;
5131 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
5132 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
5133 privatization. */
5135 static void
5136 omp_note_field_privatization (tree f, tree t)
5138 if (!omp_private_member_map)
5139 omp_private_member_map = new hash_map<tree, tree>;
5140 tree &v = omp_private_member_map->get_or_insert (f);
5141 if (v == NULL_TREE)
5143 v = t;
5144 omp_private_member_vec.safe_push (f);
5145 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
5146 omp_private_member_vec.safe_push (integer_zero_node);
5150 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
5151 dummy VAR_DECL. */
5153 tree
5154 omp_privatize_field (tree t, bool shared)
5156 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
5157 if (m == error_mark_node)
5158 return error_mark_node;
5159 if (!omp_private_member_map && !shared)
5160 omp_private_member_map = new hash_map<tree, tree>;
5161 if (TYPE_REF_P (TREE_TYPE (t)))
5163 gcc_assert (INDIRECT_REF_P (m));
5164 m = TREE_OPERAND (m, 0);
5166 tree vb = NULL_TREE;
5167 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
5168 if (v == NULL_TREE)
5170 v = create_temporary_var (TREE_TYPE (m));
5171 retrofit_lang_decl (v);
5172 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
5173 SET_DECL_VALUE_EXPR (v, m);
5174 DECL_HAS_VALUE_EXPR_P (v) = 1;
5175 if (!shared)
5176 omp_private_member_vec.safe_push (t);
5178 return v;
5181 /* Helper function for handle_omp_array_sections. Called recursively
5182 to handle multiple array-section-subscripts. C is the clause,
5183 T current expression (initially OMP_CLAUSE_DECL), which is either
5184 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
5185 expression if specified, TREE_VALUE length expression if specified,
5186 TREE_CHAIN is what it has been specified after, or some decl.
5187 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
5188 set to true if any of the array-section-subscript could have length
5189 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
5190 first array-section-subscript which is known not to have length
5191 of one. Given say:
5192 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
5193 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
5194 all are or may have length of 1, array-section-subscript [:2] is the
5195 first one known not to have length 1. For array-section-subscript
5196 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
5197 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
5198 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
5199 case though, as some lengths could be zero. */
5201 static tree
5202 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
5203 bool &maybe_zero_len, unsigned int &first_non_one,
5204 enum c_omp_region_type ort)
5206 tree ret, low_bound, length, type;
5207 if (TREE_CODE (t) != TREE_LIST)
5209 if (error_operand_p (t))
5210 return error_mark_node;
5211 if (REFERENCE_REF_P (t)
5212 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5213 t = TREE_OPERAND (t, 0);
5214 ret = t;
5215 while (INDIRECT_REF_P (t))
5217 t = TREE_OPERAND (t, 0);
5218 STRIP_NOPS (t);
5219 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5220 t = TREE_OPERAND (t, 0);
5222 while (TREE_CODE (t) == COMPOUND_EXPR)
5224 t = TREE_OPERAND (t, 1);
5225 STRIP_NOPS (t);
5227 if (TREE_CODE (t) == COMPONENT_REF
5228 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5229 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
5230 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM)
5231 && !type_dependent_expression_p (t))
5233 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
5234 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
5236 error_at (OMP_CLAUSE_LOCATION (c),
5237 "bit-field %qE in %qs clause",
5238 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5239 return error_mark_node;
5241 while (TREE_CODE (t) == COMPONENT_REF)
5243 if (TREE_TYPE (TREE_OPERAND (t, 0))
5244 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
5246 error_at (OMP_CLAUSE_LOCATION (c),
5247 "%qE is a member of a union", t);
5248 return error_mark_node;
5250 t = TREE_OPERAND (t, 0);
5251 while (TREE_CODE (t) == MEM_REF
5252 || INDIRECT_REF_P (t)
5253 || TREE_CODE (t) == ARRAY_REF)
5255 t = TREE_OPERAND (t, 0);
5256 STRIP_NOPS (t);
5257 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
5258 t = TREE_OPERAND (t, 0);
5261 if (REFERENCE_REF_P (t))
5262 t = TREE_OPERAND (t, 0);
5264 if (TREE_CODE (t) == FIELD_DECL)
5265 ret = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
5266 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
5268 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
5269 return NULL_TREE;
5270 if (DECL_P (t))
5271 error_at (OMP_CLAUSE_LOCATION (c),
5272 "%qD is not a variable in %qs clause", t,
5273 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5274 else
5275 error_at (OMP_CLAUSE_LOCATION (c),
5276 "%qE is not a variable in %qs clause", t,
5277 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5278 return error_mark_node;
5280 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5281 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5282 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
5284 error_at (OMP_CLAUSE_LOCATION (c),
5285 "%qD is threadprivate variable in %qs clause", t,
5286 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5287 return error_mark_node;
5289 if (type_dependent_expression_p (ret))
5290 return NULL_TREE;
5291 ret = convert_from_reference (ret);
5292 return ret;
5295 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
5296 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5297 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5298 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5299 && TREE_CODE (TREE_CHAIN (t)) == FIELD_DECL)
5300 TREE_CHAIN (t) = omp_privatize_field (TREE_CHAIN (t), false);
5301 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
5302 maybe_zero_len, first_non_one, ort);
5303 if (ret == error_mark_node || ret == NULL_TREE)
5304 return ret;
5306 type = TREE_TYPE (ret);
5307 low_bound = TREE_PURPOSE (t);
5308 length = TREE_VALUE (t);
5309 if ((low_bound && type_dependent_expression_p (low_bound))
5310 || (length && type_dependent_expression_p (length)))
5311 return NULL_TREE;
5313 if (low_bound == error_mark_node || length == error_mark_node)
5314 return error_mark_node;
5316 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
5318 error_at (OMP_CLAUSE_LOCATION (c),
5319 "low bound %qE of array section does not have integral type",
5320 low_bound);
5321 return error_mark_node;
5323 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
5325 error_at (OMP_CLAUSE_LOCATION (c),
5326 "length %qE of array section does not have integral type",
5327 length);
5328 return error_mark_node;
5330 if (low_bound)
5331 low_bound = mark_rvalue_use (low_bound);
5332 if (length)
5333 length = mark_rvalue_use (length);
5334 /* We need to reduce to real constant-values for checks below. */
5335 if (length)
5336 length = fold_simple (length);
5337 if (low_bound)
5338 low_bound = fold_simple (low_bound);
5339 if (low_bound
5340 && TREE_CODE (low_bound) == INTEGER_CST
5341 && TYPE_PRECISION (TREE_TYPE (low_bound))
5342 > TYPE_PRECISION (sizetype))
5343 low_bound = fold_convert (sizetype, low_bound);
5344 if (length
5345 && TREE_CODE (length) == INTEGER_CST
5346 && TYPE_PRECISION (TREE_TYPE (length))
5347 > TYPE_PRECISION (sizetype))
5348 length = fold_convert (sizetype, length);
5349 if (low_bound == NULL_TREE)
5350 low_bound = integer_zero_node;
5352 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5353 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
5354 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
5356 if (length != integer_one_node)
5358 error_at (OMP_CLAUSE_LOCATION (c),
5359 "expected single pointer in %qs clause",
5360 user_omp_clause_code_name (c, ort == C_ORT_ACC));
5361 return error_mark_node;
5364 if (length != NULL_TREE)
5366 if (!integer_nonzerop (length))
5368 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
5369 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5370 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5371 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5372 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5374 if (integer_zerop (length))
5376 error_at (OMP_CLAUSE_LOCATION (c),
5377 "zero length array section in %qs clause",
5378 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5379 return error_mark_node;
5382 else
5383 maybe_zero_len = true;
5385 if (first_non_one == types.length ()
5386 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
5387 first_non_one++;
5389 if (TREE_CODE (type) == ARRAY_TYPE)
5391 if (length == NULL_TREE
5392 && (TYPE_DOMAIN (type) == NULL_TREE
5393 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
5395 error_at (OMP_CLAUSE_LOCATION (c),
5396 "for unknown bound array type length expression must "
5397 "be specified");
5398 return error_mark_node;
5400 if (TREE_CODE (low_bound) == INTEGER_CST
5401 && tree_int_cst_sgn (low_bound) == -1)
5403 error_at (OMP_CLAUSE_LOCATION (c),
5404 "negative low bound in array section in %qs clause",
5405 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5406 return error_mark_node;
5408 if (length != NULL_TREE
5409 && TREE_CODE (length) == INTEGER_CST
5410 && tree_int_cst_sgn (length) == -1)
5412 error_at (OMP_CLAUSE_LOCATION (c),
5413 "negative length in array section in %qs clause",
5414 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5415 return error_mark_node;
5417 if (TYPE_DOMAIN (type)
5418 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
5419 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
5420 == INTEGER_CST)
5422 tree size
5423 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
5424 size = size_binop (PLUS_EXPR, size, size_one_node);
5425 if (TREE_CODE (low_bound) == INTEGER_CST)
5427 if (tree_int_cst_lt (size, low_bound))
5429 error_at (OMP_CLAUSE_LOCATION (c),
5430 "low bound %qE above array section size "
5431 "in %qs clause", low_bound,
5432 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5433 return error_mark_node;
5435 if (tree_int_cst_equal (size, low_bound))
5437 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
5438 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5439 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5440 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5441 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5443 error_at (OMP_CLAUSE_LOCATION (c),
5444 "zero length array section in %qs clause",
5445 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5446 return error_mark_node;
5448 maybe_zero_len = true;
5450 else if (length == NULL_TREE
5451 && first_non_one == types.length ()
5452 && tree_int_cst_equal
5453 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
5454 low_bound))
5455 first_non_one++;
5457 else if (length == NULL_TREE)
5459 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5460 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5461 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
5462 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
5463 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
5464 maybe_zero_len = true;
5465 if (first_non_one == types.length ())
5466 first_non_one++;
5468 if (length && TREE_CODE (length) == INTEGER_CST)
5470 if (tree_int_cst_lt (size, length))
5472 error_at (OMP_CLAUSE_LOCATION (c),
5473 "length %qE above array section size "
5474 "in %qs clause", length,
5475 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5476 return error_mark_node;
5478 if (TREE_CODE (low_bound) == INTEGER_CST)
5480 tree lbpluslen
5481 = size_binop (PLUS_EXPR,
5482 fold_convert (sizetype, low_bound),
5483 fold_convert (sizetype, length));
5484 if (TREE_CODE (lbpluslen) == INTEGER_CST
5485 && tree_int_cst_lt (size, lbpluslen))
5487 error_at (OMP_CLAUSE_LOCATION (c),
5488 "high bound %qE above array section size "
5489 "in %qs clause", lbpluslen,
5490 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5491 return error_mark_node;
5496 else if (length == NULL_TREE)
5498 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5499 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5500 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
5501 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
5502 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
5503 maybe_zero_len = true;
5504 if (first_non_one == types.length ())
5505 first_non_one++;
5508 /* For [lb:] we will need to evaluate lb more than once. */
5509 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
5511 tree lb = cp_save_expr (low_bound);
5512 if (lb != low_bound)
5514 TREE_PURPOSE (t) = lb;
5515 low_bound = lb;
5519 else if (TYPE_PTR_P (type))
5521 if (length == NULL_TREE)
5523 if (TREE_CODE (ret) == PARM_DECL && DECL_ARRAY_PARAMETER_P (ret))
5524 error_at (OMP_CLAUSE_LOCATION (c),
5525 "for array function parameter length expression "
5526 "must be specified");
5527 else
5528 error_at (OMP_CLAUSE_LOCATION (c),
5529 "for pointer type length expression must be specified");
5530 return error_mark_node;
5532 if (length != NULL_TREE
5533 && TREE_CODE (length) == INTEGER_CST
5534 && tree_int_cst_sgn (length) == -1)
5536 error_at (OMP_CLAUSE_LOCATION (c),
5537 "negative length in array section in %qs clause",
5538 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5539 return error_mark_node;
5541 /* If there is a pointer type anywhere but in the very first
5542 array-section-subscript, the array section could be non-contiguous. */
5543 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5544 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5545 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
5547 /* If any prior dimension has a non-one length, then deem this
5548 array section as non-contiguous. */
5549 for (tree d = TREE_CHAIN (t); TREE_CODE (d) == TREE_LIST;
5550 d = TREE_CHAIN (d))
5552 tree d_length = TREE_VALUE (d);
5553 if (d_length == NULL_TREE || !integer_onep (d_length))
5555 error_at (OMP_CLAUSE_LOCATION (c),
5556 "array section is not contiguous in %qs clause",
5557 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5558 return error_mark_node;
5563 else
5565 error_at (OMP_CLAUSE_LOCATION (c),
5566 "%qE does not have pointer or array type", ret);
5567 return error_mark_node;
5569 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
5570 types.safe_push (TREE_TYPE (ret));
5571 /* We will need to evaluate lb more than once. */
5572 tree lb = cp_save_expr (low_bound);
5573 if (lb != low_bound)
5575 TREE_PURPOSE (t) = lb;
5576 low_bound = lb;
5578 /* Temporarily disable -fstrong-eval-order for array reductions.
5579 The SAVE_EXPR and COMPOUND_EXPR added if low_bound has side-effects
5580 is something the middle-end can't cope with and more importantly,
5581 it needs to be the actual base variable that is privatized, not some
5582 temporary assigned previous value of it. That, together with OpenMP
5583 saying how many times the side-effects are evaluated is unspecified,
5584 makes int *a, *b; ... reduction(+:a[a = b, 3:10]) really unspecified. */
5585 warning_sentinel s (flag_strong_eval_order,
5586 OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5587 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5588 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION);
5589 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, NULL,
5590 tf_warning_or_error);
5591 return ret;
5594 /* Handle array sections for clause C. */
5596 static bool
5597 handle_omp_array_sections (tree c, enum c_omp_region_type ort)
5599 bool maybe_zero_len = false;
5600 unsigned int first_non_one = 0;
5601 auto_vec<tree, 10> types;
5602 tree *tp = &OMP_CLAUSE_DECL (c);
5603 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5604 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
5605 && TREE_CODE (*tp) == TREE_LIST
5606 && TREE_PURPOSE (*tp)
5607 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
5608 tp = &TREE_VALUE (*tp);
5609 tree first = handle_omp_array_sections_1 (c, *tp, types,
5610 maybe_zero_len, first_non_one,
5611 ort);
5612 if (first == error_mark_node)
5613 return true;
5614 if (first == NULL_TREE)
5615 return false;
5616 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5617 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
5619 tree t = *tp;
5620 tree tem = NULL_TREE;
5621 if (processing_template_decl)
5622 return false;
5623 /* Need to evaluate side effects in the length expressions
5624 if any. */
5625 while (TREE_CODE (t) == TREE_LIST)
5627 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
5629 if (tem == NULL_TREE)
5630 tem = TREE_VALUE (t);
5631 else
5632 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
5633 TREE_VALUE (t), tem);
5635 t = TREE_CHAIN (t);
5637 if (tem)
5638 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
5639 *tp = first;
5641 else
5643 unsigned int num = types.length (), i;
5644 tree t, side_effects = NULL_TREE, size = NULL_TREE;
5645 tree condition = NULL_TREE;
5647 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
5648 maybe_zero_len = true;
5649 if (processing_template_decl && maybe_zero_len)
5650 return false;
5652 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
5653 t = TREE_CHAIN (t))
5655 tree low_bound = TREE_PURPOSE (t);
5656 tree length = TREE_VALUE (t);
5658 i--;
5659 if (low_bound
5660 && TREE_CODE (low_bound) == INTEGER_CST
5661 && TYPE_PRECISION (TREE_TYPE (low_bound))
5662 > TYPE_PRECISION (sizetype))
5663 low_bound = fold_convert (sizetype, low_bound);
5664 if (length
5665 && TREE_CODE (length) == INTEGER_CST
5666 && TYPE_PRECISION (TREE_TYPE (length))
5667 > TYPE_PRECISION (sizetype))
5668 length = fold_convert (sizetype, length);
5669 if (low_bound == NULL_TREE)
5670 low_bound = integer_zero_node;
5671 if (!maybe_zero_len && i > first_non_one)
5673 if (integer_nonzerop (low_bound))
5674 goto do_warn_noncontiguous;
5675 if (length != NULL_TREE
5676 && TREE_CODE (length) == INTEGER_CST
5677 && TYPE_DOMAIN (types[i])
5678 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
5679 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
5680 == INTEGER_CST)
5682 tree size;
5683 size = size_binop (PLUS_EXPR,
5684 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
5685 size_one_node);
5686 if (!tree_int_cst_equal (length, size))
5688 do_warn_noncontiguous:
5689 error_at (OMP_CLAUSE_LOCATION (c),
5690 "array section is not contiguous in %qs "
5691 "clause",
5692 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5693 return true;
5696 if (!processing_template_decl
5697 && length != NULL_TREE
5698 && TREE_SIDE_EFFECTS (length))
5700 if (side_effects == NULL_TREE)
5701 side_effects = length;
5702 else
5703 side_effects = build2 (COMPOUND_EXPR,
5704 TREE_TYPE (side_effects),
5705 length, side_effects);
5708 else if (processing_template_decl)
5709 continue;
5710 else
5712 tree l;
5714 if (i > first_non_one
5715 && ((length && integer_nonzerop (length))
5716 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5717 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5718 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
5719 continue;
5720 if (length)
5721 l = fold_convert (sizetype, length);
5722 else
5724 l = size_binop (PLUS_EXPR,
5725 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
5726 size_one_node);
5727 l = size_binop (MINUS_EXPR, l,
5728 fold_convert (sizetype, low_bound));
5730 if (i > first_non_one)
5732 l = fold_build2 (NE_EXPR, boolean_type_node, l,
5733 size_zero_node);
5734 if (condition == NULL_TREE)
5735 condition = l;
5736 else
5737 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
5738 l, condition);
5740 else if (size == NULL_TREE)
5742 size = size_in_bytes (TREE_TYPE (types[i]));
5743 tree eltype = TREE_TYPE (types[num - 1]);
5744 while (TREE_CODE (eltype) == ARRAY_TYPE)
5745 eltype = TREE_TYPE (eltype);
5746 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5747 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5748 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5749 size = size_binop (EXACT_DIV_EXPR, size,
5750 size_in_bytes (eltype));
5751 size = size_binop (MULT_EXPR, size, l);
5752 if (condition)
5753 size = fold_build3 (COND_EXPR, sizetype, condition,
5754 size, size_zero_node);
5756 else
5757 size = size_binop (MULT_EXPR, size, l);
5760 if (!processing_template_decl)
5762 if (side_effects)
5763 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
5764 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5765 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5766 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5768 size = size_binop (MINUS_EXPR, size, size_one_node);
5769 size = save_expr (size);
5770 tree index_type = build_index_type (size);
5771 tree eltype = TREE_TYPE (first);
5772 while (TREE_CODE (eltype) == ARRAY_TYPE)
5773 eltype = TREE_TYPE (eltype);
5774 tree type = build_array_type (eltype, index_type);
5775 tree ptype = build_pointer_type (eltype);
5776 if (TYPE_REF_P (TREE_TYPE (t))
5777 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
5778 t = convert_from_reference (t);
5779 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5780 t = build_fold_addr_expr (t);
5781 tree t2 = build_fold_addr_expr (first);
5782 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5783 ptrdiff_type_node, t2);
5784 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5785 ptrdiff_type_node, t2,
5786 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5787 ptrdiff_type_node, t));
5788 if (tree_fits_shwi_p (t2))
5789 t = build2 (MEM_REF, type, t,
5790 build_int_cst (ptype, tree_to_shwi (t2)));
5791 else
5793 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5794 sizetype, t2);
5795 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
5796 TREE_TYPE (t), t, t2);
5797 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
5799 OMP_CLAUSE_DECL (c) = t;
5800 return false;
5802 OMP_CLAUSE_DECL (c) = first;
5803 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
5804 return false;
5805 OMP_CLAUSE_SIZE (c) = size;
5806 if (TREE_CODE (t) == FIELD_DECL)
5807 t = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
5808 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
5809 || (TREE_CODE (t) == COMPONENT_REF
5810 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
5811 return false;
5812 switch (OMP_CLAUSE_MAP_KIND (c))
5814 case GOMP_MAP_ALLOC:
5815 case GOMP_MAP_IF_PRESENT:
5816 case GOMP_MAP_TO:
5817 case GOMP_MAP_FROM:
5818 case GOMP_MAP_TOFROM:
5819 case GOMP_MAP_ALWAYS_TO:
5820 case GOMP_MAP_ALWAYS_FROM:
5821 case GOMP_MAP_ALWAYS_TOFROM:
5822 case GOMP_MAP_PRESENT_ALLOC:
5823 case GOMP_MAP_PRESENT_TO:
5824 case GOMP_MAP_PRESENT_FROM:
5825 case GOMP_MAP_PRESENT_TOFROM:
5826 case GOMP_MAP_ALWAYS_PRESENT_TO:
5827 case GOMP_MAP_ALWAYS_PRESENT_FROM:
5828 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
5829 case GOMP_MAP_RELEASE:
5830 case GOMP_MAP_DELETE:
5831 case GOMP_MAP_FORCE_TO:
5832 case GOMP_MAP_FORCE_FROM:
5833 case GOMP_MAP_FORCE_TOFROM:
5834 case GOMP_MAP_FORCE_PRESENT:
5835 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
5836 break;
5837 default:
5838 break;
5840 bool reference_always_pointer = true;
5841 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5842 OMP_CLAUSE_MAP);
5843 if (TREE_CODE (t) == COMPONENT_REF)
5845 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ATTACH_DETACH);
5847 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
5848 && TYPE_REF_P (TREE_TYPE (t)))
5850 if (TREE_CODE (TREE_TYPE (TREE_TYPE (t))) == ARRAY_TYPE)
5851 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5852 else
5853 t = convert_from_reference (t);
5855 reference_always_pointer = false;
5858 else if (REFERENCE_REF_P (t)
5859 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
5861 gomp_map_kind k;
5862 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
5863 && TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
5864 k = GOMP_MAP_ATTACH_DETACH;
5865 else
5867 t = TREE_OPERAND (t, 0);
5868 k = (ort == C_ORT_ACC
5869 ? GOMP_MAP_ATTACH_DETACH : GOMP_MAP_ALWAYS_POINTER);
5871 OMP_CLAUSE_SET_MAP_KIND (c2, k);
5873 else
5874 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
5875 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
5876 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5877 && !cxx_mark_addressable (t))
5878 return false;
5879 OMP_CLAUSE_DECL (c2) = t;
5880 t = build_fold_addr_expr (first);
5881 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5882 ptrdiff_type_node, t);
5883 tree ptr = OMP_CLAUSE_DECL (c2);
5884 ptr = convert_from_reference (ptr);
5885 if (!INDIRECT_TYPE_P (TREE_TYPE (ptr)))
5886 ptr = build_fold_addr_expr (ptr);
5887 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5888 ptrdiff_type_node, t,
5889 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5890 ptrdiff_type_node, ptr));
5891 OMP_CLAUSE_SIZE (c2) = t;
5892 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
5893 OMP_CLAUSE_CHAIN (c) = c2;
5895 ptr = OMP_CLAUSE_DECL (c2);
5896 if (reference_always_pointer
5897 && OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
5898 && TYPE_REF_P (TREE_TYPE (ptr))
5899 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (ptr))))
5901 tree c3 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
5902 OMP_CLAUSE_MAP);
5903 OMP_CLAUSE_SET_MAP_KIND (c3, OMP_CLAUSE_MAP_KIND (c2));
5904 OMP_CLAUSE_MAP_IMPLICIT (c2) = OMP_CLAUSE_MAP_IMPLICIT (c);
5905 OMP_CLAUSE_DECL (c3) = ptr;
5906 if (OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ALWAYS_POINTER
5907 || OMP_CLAUSE_MAP_KIND (c2) == GOMP_MAP_ATTACH_DETACH)
5909 OMP_CLAUSE_DECL (c2) = build_simple_mem_ref (ptr);
5910 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
5912 else
5913 OMP_CLAUSE_DECL (c2) = convert_from_reference (ptr);
5914 OMP_CLAUSE_SIZE (c3) = size_zero_node;
5915 OMP_CLAUSE_CHAIN (c3) = OMP_CLAUSE_CHAIN (c2);
5916 OMP_CLAUSE_CHAIN (c2) = c3;
5920 return false;
5923 /* Return identifier to look up for omp declare reduction. */
5925 tree
5926 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
5928 const char *p = NULL;
5929 const char *m = NULL;
5930 switch (reduction_code)
5932 case PLUS_EXPR:
5933 case MULT_EXPR:
5934 case MINUS_EXPR:
5935 case BIT_AND_EXPR:
5936 case BIT_XOR_EXPR:
5937 case BIT_IOR_EXPR:
5938 case TRUTH_ANDIF_EXPR:
5939 case TRUTH_ORIF_EXPR:
5940 reduction_id = ovl_op_identifier (false, reduction_code);
5941 break;
5942 case MIN_EXPR:
5943 p = "min";
5944 break;
5945 case MAX_EXPR:
5946 p = "max";
5947 break;
5948 default:
5949 break;
5952 if (p == NULL)
5954 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
5955 return error_mark_node;
5956 p = IDENTIFIER_POINTER (reduction_id);
5959 if (type != NULL_TREE)
5960 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
5962 const char prefix[] = "omp declare reduction ";
5963 size_t lenp = sizeof (prefix);
5964 if (strncmp (p, prefix, lenp - 1) == 0)
5965 lenp = 1;
5966 size_t len = strlen (p);
5967 size_t lenm = m ? strlen (m) + 1 : 0;
5968 char *name = XALLOCAVEC (char, lenp + len + lenm);
5969 if (lenp > 1)
5970 memcpy (name, prefix, lenp - 1);
5971 memcpy (name + lenp - 1, p, len + 1);
5972 if (m)
5974 name[lenp + len - 1] = '~';
5975 memcpy (name + lenp + len, m, lenm);
5977 return get_identifier (name);
5980 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
5981 FUNCTION_DECL or NULL_TREE if not found. */
5983 static tree
5984 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
5985 vec<tree> *ambiguousp)
5987 tree orig_id = id;
5988 tree baselink = NULL_TREE;
5989 if (identifier_p (id))
5991 cp_id_kind idk;
5992 bool nonint_cst_expression_p;
5993 const char *error_msg;
5994 id = omp_reduction_id (ERROR_MARK, id, type);
5995 tree decl = lookup_name (id);
5996 if (decl == NULL_TREE)
5997 decl = error_mark_node;
5998 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
5999 &nonint_cst_expression_p, false, true, false,
6000 false, &error_msg, loc);
6001 if (idk == CP_ID_KIND_UNQUALIFIED
6002 && identifier_p (id))
6004 vec<tree, va_gc> *args = NULL;
6005 vec_safe_push (args, build_reference_type (type));
6006 id = perform_koenig_lookup (id, args, tf_none);
6009 else if (TREE_CODE (id) == SCOPE_REF)
6010 id = lookup_qualified_name (TREE_OPERAND (id, 0),
6011 omp_reduction_id (ERROR_MARK,
6012 TREE_OPERAND (id, 1),
6013 type),
6014 LOOK_want::NORMAL, false);
6015 tree fns = id;
6016 id = NULL_TREE;
6017 if (fns && is_overloaded_fn (fns))
6019 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
6021 tree fndecl = *iter;
6022 if (TREE_CODE (fndecl) == FUNCTION_DECL)
6024 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
6025 if (same_type_p (TREE_TYPE (argtype), type))
6027 id = fndecl;
6028 break;
6033 if (id && BASELINK_P (fns))
6035 if (baselinkp)
6036 *baselinkp = fns;
6037 else
6038 baselink = fns;
6042 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
6044 auto_vec<tree> ambiguous;
6045 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
6046 unsigned int ix;
6047 if (ambiguousp == NULL)
6048 ambiguousp = &ambiguous;
6049 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
6051 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
6052 baselinkp ? baselinkp : &baselink,
6053 ambiguousp);
6054 if (id == NULL_TREE)
6055 continue;
6056 if (!ambiguousp->is_empty ())
6057 ambiguousp->safe_push (id);
6058 else if (ret != NULL_TREE)
6060 ambiguousp->safe_push (ret);
6061 ambiguousp->safe_push (id);
6062 ret = NULL_TREE;
6064 else
6065 ret = id;
6067 if (ambiguousp != &ambiguous)
6068 return ret;
6069 if (!ambiguous.is_empty ())
6071 const char *str = _("candidates are:");
6072 unsigned int idx;
6073 tree udr;
6074 error_at (loc, "user defined reduction lookup is ambiguous");
6075 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
6077 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
6078 if (idx == 0)
6079 str = get_spaces (str);
6081 ret = error_mark_node;
6082 baselink = NULL_TREE;
6084 id = ret;
6086 if (id && baselink)
6087 perform_or_defer_access_check (BASELINK_BINFO (baselink),
6088 id, id, tf_warning_or_error);
6089 return id;
6092 /* Helper function for cp_parser_omp_declare_reduction_exprs
6093 and tsubst_omp_udr.
6094 Remove CLEANUP_STMT for data (omp_priv variable).
6095 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
6096 DECL_EXPR. */
6098 tree
6099 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
6101 if (TYPE_P (*tp))
6102 *walk_subtrees = 0;
6103 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
6104 *tp = CLEANUP_BODY (*tp);
6105 else if (TREE_CODE (*tp) == DECL_EXPR)
6107 tree decl = DECL_EXPR_DECL (*tp);
6108 if (!processing_template_decl
6109 && decl == (tree) data
6110 && DECL_INITIAL (decl)
6111 && DECL_INITIAL (decl) != error_mark_node)
6113 tree list = NULL_TREE;
6114 append_to_statement_list_force (*tp, &list);
6115 tree init_expr = build2 (INIT_EXPR, void_type_node,
6116 decl, DECL_INITIAL (decl));
6117 DECL_INITIAL (decl) = NULL_TREE;
6118 append_to_statement_list_force (init_expr, &list);
6119 *tp = list;
6122 return NULL_TREE;
6125 /* Data passed from cp_check_omp_declare_reduction to
6126 cp_check_omp_declare_reduction_r. */
6128 struct cp_check_omp_declare_reduction_data
6130 location_t loc;
6131 tree stmts[7];
6132 bool combiner_p;
6135 /* Helper function for cp_check_omp_declare_reduction, called via
6136 cp_walk_tree. */
6138 static tree
6139 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
6141 struct cp_check_omp_declare_reduction_data *udr_data
6142 = (struct cp_check_omp_declare_reduction_data *) data;
6143 if (SSA_VAR_P (*tp)
6144 && !DECL_ARTIFICIAL (*tp)
6145 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
6146 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
6148 location_t loc = udr_data->loc;
6149 if (udr_data->combiner_p)
6150 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
6151 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
6152 *tp);
6153 else
6154 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
6155 "to variable %qD which is not %<omp_priv%> nor "
6156 "%<omp_orig%>",
6157 *tp);
6158 return *tp;
6160 return NULL_TREE;
6163 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
6165 bool
6166 cp_check_omp_declare_reduction (tree udr)
6168 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
6169 gcc_assert (TYPE_REF_P (type));
6170 type = TREE_TYPE (type);
6171 int i;
6172 location_t loc = DECL_SOURCE_LOCATION (udr);
6174 if (type == error_mark_node)
6175 return false;
6176 if (ARITHMETIC_TYPE_P (type))
6178 static enum tree_code predef_codes[]
6179 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
6180 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
6181 for (i = 0; i < 8; i++)
6183 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
6184 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
6185 const char *n2 = IDENTIFIER_POINTER (id);
6186 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
6187 && (n1[IDENTIFIER_LENGTH (id)] == '~'
6188 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
6189 break;
6192 if (i == 8
6193 && TREE_CODE (type) != COMPLEX_EXPR)
6195 const char prefix_minmax[] = "omp declare reduction m";
6196 size_t prefix_size = sizeof (prefix_minmax) - 1;
6197 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
6198 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
6199 prefix_minmax, prefix_size) == 0
6200 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
6201 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
6202 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
6203 i = 0;
6205 if (i < 8)
6207 error_at (loc, "predeclared arithmetic type %qT in "
6208 "%<#pragma omp declare reduction%>", type);
6209 return false;
6212 else if (FUNC_OR_METHOD_TYPE_P (type)
6213 || TREE_CODE (type) == ARRAY_TYPE)
6215 error_at (loc, "function or array type %qT in "
6216 "%<#pragma omp declare reduction%>", type);
6217 return false;
6219 else if (TYPE_REF_P (type))
6221 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
6222 type);
6223 return false;
6225 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
6227 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
6228 "type %qT in %<#pragma omp declare reduction%>", type);
6229 return false;
6232 tree body = DECL_SAVED_TREE (udr);
6233 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
6234 return true;
6236 tree_stmt_iterator tsi;
6237 struct cp_check_omp_declare_reduction_data data;
6238 memset (data.stmts, 0, sizeof data.stmts);
6239 for (i = 0, tsi = tsi_start (body);
6240 i < 7 && !tsi_end_p (tsi);
6241 i++, tsi_next (&tsi))
6242 data.stmts[i] = tsi_stmt (tsi);
6243 data.loc = loc;
6244 gcc_assert (tsi_end_p (tsi));
6245 if (i >= 3)
6247 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
6248 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
6249 if (warning_suppressed_p (DECL_EXPR_DECL (data.stmts[0]) /* What warning? */))
6250 return true;
6251 data.combiner_p = true;
6252 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
6253 &data, NULL))
6254 suppress_warning (DECL_EXPR_DECL (data.stmts[0]) /* What warning? */);
6256 if (i >= 6)
6258 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
6259 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
6260 data.combiner_p = false;
6261 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
6262 &data, NULL)
6263 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
6264 cp_check_omp_declare_reduction_r, &data, NULL))
6265 suppress_warning (DECL_EXPR_DECL (data.stmts[0]) /* Wat warning? */);
6266 if (i == 7)
6267 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
6269 return true;
6272 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
6273 an inline call. But, remap
6274 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
6275 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
6277 static tree
6278 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
6279 tree decl, tree placeholder)
6281 copy_body_data id;
6282 hash_map<tree, tree> decl_map;
6284 decl_map.put (omp_decl1, placeholder);
6285 decl_map.put (omp_decl2, decl);
6286 memset (&id, 0, sizeof (id));
6287 id.src_fn = DECL_CONTEXT (omp_decl1);
6288 id.dst_fn = current_function_decl;
6289 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
6290 id.decl_map = &decl_map;
6292 id.copy_decl = copy_decl_no_change;
6293 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
6294 id.transform_new_cfg = true;
6295 id.transform_return_to_modify = false;
6296 id.eh_lp_nr = 0;
6297 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
6298 return stmt;
6301 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
6302 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
6304 static tree
6305 find_omp_placeholder_r (tree *tp, int *, void *data)
6307 if (*tp == (tree) data)
6308 return *tp;
6309 return NULL_TREE;
6312 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
6313 Return true if there is some error and the clause should be removed. */
6315 static bool
6316 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
6318 tree t = OMP_CLAUSE_DECL (c);
6319 bool predefined = false;
6320 if (TREE_CODE (t) == TREE_LIST)
6322 gcc_assert (processing_template_decl);
6323 return false;
6325 tree type = TREE_TYPE (t);
6326 if (TREE_CODE (t) == MEM_REF)
6327 type = TREE_TYPE (type);
6328 if (TYPE_REF_P (type))
6329 type = TREE_TYPE (type);
6330 if (TREE_CODE (type) == ARRAY_TYPE)
6332 tree oatype = type;
6333 gcc_assert (TREE_CODE (t) != MEM_REF);
6334 while (TREE_CODE (type) == ARRAY_TYPE)
6335 type = TREE_TYPE (type);
6336 if (!processing_template_decl)
6338 t = require_complete_type (t);
6339 if (t == error_mark_node
6340 || !complete_type_or_else (oatype, NULL_TREE))
6341 return true;
6342 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
6343 TYPE_SIZE_UNIT (type));
6344 if (integer_zerop (size))
6346 error_at (OMP_CLAUSE_LOCATION (c),
6347 "%qE in %<reduction%> clause is a zero size array",
6348 omp_clause_printable_decl (t));
6349 return true;
6351 size = size_binop (MINUS_EXPR, size, size_one_node);
6352 size = save_expr (size);
6353 tree index_type = build_index_type (size);
6354 tree atype = build_array_type (type, index_type);
6355 tree ptype = build_pointer_type (type);
6356 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6357 t = build_fold_addr_expr (t);
6358 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
6359 OMP_CLAUSE_DECL (c) = t;
6362 if (type == error_mark_node)
6363 return true;
6364 else if (ARITHMETIC_TYPE_P (type))
6365 switch (OMP_CLAUSE_REDUCTION_CODE (c))
6367 case PLUS_EXPR:
6368 case MULT_EXPR:
6369 case MINUS_EXPR:
6370 case TRUTH_ANDIF_EXPR:
6371 case TRUTH_ORIF_EXPR:
6372 predefined = true;
6373 break;
6374 case MIN_EXPR:
6375 case MAX_EXPR:
6376 if (TREE_CODE (type) == COMPLEX_TYPE)
6377 break;
6378 predefined = true;
6379 break;
6380 case BIT_AND_EXPR:
6381 case BIT_IOR_EXPR:
6382 case BIT_XOR_EXPR:
6383 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
6384 break;
6385 predefined = true;
6386 break;
6387 default:
6388 break;
6390 else if (TYPE_READONLY (type))
6392 error_at (OMP_CLAUSE_LOCATION (c),
6393 "%qE has const type for %<reduction%>",
6394 omp_clause_printable_decl (t));
6395 return true;
6397 else if (!processing_template_decl)
6399 t = require_complete_type (t);
6400 if (t == error_mark_node)
6401 return true;
6402 OMP_CLAUSE_DECL (c) = t;
6405 if (predefined)
6407 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
6408 return false;
6410 else if (processing_template_decl)
6412 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
6413 return true;
6414 return false;
6417 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
6419 type = TYPE_MAIN_VARIANT (type);
6420 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
6421 if (id == NULL_TREE)
6422 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
6423 NULL_TREE, NULL_TREE);
6424 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
6425 if (id)
6427 if (id == error_mark_node)
6428 return true;
6429 mark_used (id);
6430 tree body = DECL_SAVED_TREE (id);
6431 if (!body)
6432 return true;
6433 if (TREE_CODE (body) == STATEMENT_LIST)
6435 tree_stmt_iterator tsi;
6436 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
6437 int i;
6438 tree stmts[7];
6439 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
6440 atype = TREE_TYPE (atype);
6441 bool need_static_cast = !same_type_p (type, atype);
6442 memset (stmts, 0, sizeof stmts);
6443 for (i = 0, tsi = tsi_start (body);
6444 i < 7 && !tsi_end_p (tsi);
6445 i++, tsi_next (&tsi))
6446 stmts[i] = tsi_stmt (tsi);
6447 gcc_assert (tsi_end_p (tsi));
6449 if (i >= 3)
6451 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
6452 && TREE_CODE (stmts[1]) == DECL_EXPR);
6453 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
6454 DECL_ARTIFICIAL (placeholder) = 1;
6455 DECL_IGNORED_P (placeholder) = 1;
6456 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
6457 if (TREE_CODE (t) == MEM_REF)
6459 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
6460 type);
6461 DECL_ARTIFICIAL (decl_placeholder) = 1;
6462 DECL_IGNORED_P (decl_placeholder) = 1;
6463 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
6465 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
6466 cxx_mark_addressable (placeholder);
6467 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
6468 && (decl_placeholder
6469 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
6470 cxx_mark_addressable (decl_placeholder ? decl_placeholder
6471 : OMP_CLAUSE_DECL (c));
6472 tree omp_out = placeholder;
6473 tree omp_in = decl_placeholder ? decl_placeholder
6474 : convert_from_reference (OMP_CLAUSE_DECL (c));
6475 if (need_static_cast)
6477 tree rtype = build_reference_type (atype);
6478 omp_out = build_static_cast (input_location,
6479 rtype, omp_out,
6480 tf_warning_or_error);
6481 omp_in = build_static_cast (input_location,
6482 rtype, omp_in,
6483 tf_warning_or_error);
6484 if (omp_out == error_mark_node || omp_in == error_mark_node)
6485 return true;
6486 omp_out = convert_from_reference (omp_out);
6487 omp_in = convert_from_reference (omp_in);
6489 OMP_CLAUSE_REDUCTION_MERGE (c)
6490 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
6491 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
6493 if (i >= 6)
6495 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
6496 && TREE_CODE (stmts[4]) == DECL_EXPR);
6497 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3]))
6498 && (decl_placeholder
6499 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
6500 cxx_mark_addressable (decl_placeholder ? decl_placeholder
6501 : OMP_CLAUSE_DECL (c));
6502 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
6503 cxx_mark_addressable (placeholder);
6504 tree omp_priv = decl_placeholder ? decl_placeholder
6505 : convert_from_reference (OMP_CLAUSE_DECL (c));
6506 tree omp_orig = placeholder;
6507 if (need_static_cast)
6509 if (i == 7)
6511 error_at (OMP_CLAUSE_LOCATION (c),
6512 "user defined reduction with constructor "
6513 "initializer for base class %qT", atype);
6514 return true;
6516 tree rtype = build_reference_type (atype);
6517 omp_priv = build_static_cast (input_location,
6518 rtype, omp_priv,
6519 tf_warning_or_error);
6520 omp_orig = build_static_cast (input_location,
6521 rtype, omp_orig,
6522 tf_warning_or_error);
6523 if (omp_priv == error_mark_node
6524 || omp_orig == error_mark_node)
6525 return true;
6526 omp_priv = convert_from_reference (omp_priv);
6527 omp_orig = convert_from_reference (omp_orig);
6529 if (i == 6)
6530 *need_default_ctor = true;
6531 OMP_CLAUSE_REDUCTION_INIT (c)
6532 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
6533 DECL_EXPR_DECL (stmts[3]),
6534 omp_priv, omp_orig);
6535 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
6536 find_omp_placeholder_r, placeholder, NULL))
6537 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
6539 else if (i >= 3)
6541 if (CLASS_TYPE_P (type) && !pod_type_p (type))
6542 *need_default_ctor = true;
6543 else
6545 tree init;
6546 tree v = decl_placeholder ? decl_placeholder
6547 : convert_from_reference (t);
6548 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
6549 init = build_constructor (TREE_TYPE (v), NULL);
6550 else
6551 init = fold_convert (TREE_TYPE (v), integer_zero_node);
6552 OMP_CLAUSE_REDUCTION_INIT (c)
6553 = cp_build_init_expr (v, init);
6558 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6559 *need_dtor = true;
6560 else
6562 error_at (OMP_CLAUSE_LOCATION (c),
6563 "user defined reduction not found for %qE",
6564 omp_clause_printable_decl (t));
6565 return true;
6567 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
6568 gcc_assert (TYPE_SIZE_UNIT (type)
6569 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
6570 return false;
6573 /* Called from finish_struct_1. linear(this) or linear(this:step)
6574 clauses might not be finalized yet because the class has been incomplete
6575 when parsing #pragma omp declare simd methods. Fix those up now. */
6577 void
6578 finish_omp_declare_simd_methods (tree t)
6580 if (processing_template_decl)
6581 return;
6583 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6585 if (TREE_CODE (x) == USING_DECL
6586 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (x))
6587 continue;
6588 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
6589 if (!ods || !TREE_VALUE (ods))
6590 continue;
6591 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
6592 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6593 && integer_zerop (OMP_CLAUSE_DECL (c))
6594 && OMP_CLAUSE_LINEAR_STEP (c)
6595 && TYPE_PTR_P (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c))))
6597 tree s = OMP_CLAUSE_LINEAR_STEP (c);
6598 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
6599 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
6600 sizetype, s, TYPE_SIZE_UNIT (t));
6601 OMP_CLAUSE_LINEAR_STEP (c) = s;
6606 /* Adjust sink depend/doacross clause to take into account pointer offsets.
6608 Return TRUE if there was a problem processing the offset, and the
6609 whole clause should be removed. */
6611 static bool
6612 cp_finish_omp_clause_doacross_sink (tree sink_clause)
6614 tree t = OMP_CLAUSE_DECL (sink_clause);
6615 gcc_assert (TREE_CODE (t) == TREE_LIST);
6617 /* Make sure we don't adjust things twice for templates. */
6618 if (processing_template_decl)
6619 return false;
6621 for (; t; t = TREE_CHAIN (t))
6623 tree decl = TREE_VALUE (t);
6624 if (TYPE_PTR_P (TREE_TYPE (decl)))
6626 tree offset = TREE_PURPOSE (t);
6627 bool neg = wi::neg_p (wi::to_wide (offset));
6628 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
6629 decl = mark_rvalue_use (decl);
6630 decl = convert_from_reference (decl);
6631 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
6632 neg ? MINUS_EXPR : PLUS_EXPR,
6633 decl, offset);
6634 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
6635 MINUS_EXPR, sizetype,
6636 fold_convert (sizetype, t2),
6637 fold_convert (sizetype, decl));
6638 if (t2 == error_mark_node)
6639 return true;
6640 TREE_PURPOSE (t) = t2;
6643 return false;
6646 /* Finish OpenMP iterators ITER. Return true if they are errorneous
6647 and clauses containing them should be removed. */
6649 static bool
6650 cp_omp_finish_iterators (tree iter)
6652 bool ret = false;
6653 for (tree it = iter; it; it = TREE_CHAIN (it))
6655 tree var = TREE_VEC_ELT (it, 0);
6656 tree begin = TREE_VEC_ELT (it, 1);
6657 tree end = TREE_VEC_ELT (it, 2);
6658 tree step = TREE_VEC_ELT (it, 3);
6659 tree orig_step;
6660 tree type = TREE_TYPE (var);
6661 location_t loc = DECL_SOURCE_LOCATION (var);
6662 if (type == error_mark_node)
6664 ret = true;
6665 continue;
6667 if (type_dependent_expression_p (var))
6668 continue;
6669 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
6671 error_at (loc, "iterator %qD has neither integral nor pointer type",
6672 var);
6673 ret = true;
6674 continue;
6676 else if (TYPE_READONLY (type))
6678 error_at (loc, "iterator %qD has const qualified type", var);
6679 ret = true;
6680 continue;
6682 if (type_dependent_expression_p (begin)
6683 || type_dependent_expression_p (end)
6684 || type_dependent_expression_p (step))
6685 continue;
6686 else if (error_operand_p (step))
6688 ret = true;
6689 continue;
6691 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
6693 error_at (EXPR_LOC_OR_LOC (step, loc),
6694 "iterator step with non-integral type");
6695 ret = true;
6696 continue;
6699 begin = mark_rvalue_use (begin);
6700 end = mark_rvalue_use (end);
6701 step = mark_rvalue_use (step);
6702 begin = cp_build_c_cast (input_location, type, begin,
6703 tf_warning_or_error);
6704 end = cp_build_c_cast (input_location, type, end,
6705 tf_warning_or_error);
6706 orig_step = step;
6707 if (!processing_template_decl)
6708 step = orig_step = save_expr (step);
6709 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
6710 step = cp_build_c_cast (input_location, stype, step,
6711 tf_warning_or_error);
6712 if (POINTER_TYPE_P (type) && !processing_template_decl)
6714 begin = save_expr (begin);
6715 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
6716 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
6717 fold_convert (sizetype, step),
6718 fold_convert (sizetype, begin));
6719 step = fold_convert (ssizetype, step);
6721 if (!processing_template_decl)
6723 begin = maybe_constant_value (begin);
6724 end = maybe_constant_value (end);
6725 step = maybe_constant_value (step);
6726 orig_step = maybe_constant_value (orig_step);
6728 if (integer_zerop (step))
6730 error_at (loc, "iterator %qD has zero step", var);
6731 ret = true;
6732 continue;
6735 if (begin == error_mark_node
6736 || end == error_mark_node
6737 || step == error_mark_node
6738 || orig_step == error_mark_node)
6740 ret = true;
6741 continue;
6744 if (!processing_template_decl)
6746 begin = fold_build_cleanup_point_expr (TREE_TYPE (begin), begin);
6747 end = fold_build_cleanup_point_expr (TREE_TYPE (end), end);
6748 step = fold_build_cleanup_point_expr (TREE_TYPE (step), step);
6749 orig_step = fold_build_cleanup_point_expr (TREE_TYPE (orig_step),
6750 orig_step);
6752 hash_set<tree> pset;
6753 tree it2;
6754 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
6756 tree var2 = TREE_VEC_ELT (it2, 0);
6757 tree begin2 = TREE_VEC_ELT (it2, 1);
6758 tree end2 = TREE_VEC_ELT (it2, 2);
6759 tree step2 = TREE_VEC_ELT (it2, 3);
6760 location_t loc2 = DECL_SOURCE_LOCATION (var2);
6761 if (cp_walk_tree (&begin2, find_omp_placeholder_r, var, &pset))
6763 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
6764 "begin expression refers to outer iterator %qD", var);
6765 break;
6767 else if (cp_walk_tree (&end2, find_omp_placeholder_r, var, &pset))
6769 error_at (EXPR_LOC_OR_LOC (end2, loc2),
6770 "end expression refers to outer iterator %qD", var);
6771 break;
6773 else if (cp_walk_tree (&step2, find_omp_placeholder_r, var, &pset))
6775 error_at (EXPR_LOC_OR_LOC (step2, loc2),
6776 "step expression refers to outer iterator %qD", var);
6777 break;
6780 if (it2)
6782 ret = true;
6783 continue;
6785 TREE_VEC_ELT (it, 1) = begin;
6786 TREE_VEC_ELT (it, 2) = end;
6787 if (processing_template_decl)
6788 TREE_VEC_ELT (it, 3) = orig_step;
6789 else
6791 TREE_VEC_ELT (it, 3) = step;
6792 TREE_VEC_ELT (it, 4) = orig_step;
6795 return ret;
6798 /* Ensure that pointers are used in OpenACC attach and detach clauses.
6799 Return true if an error has been detected. */
6801 static bool
6802 cp_oacc_check_attachments (tree c)
6804 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6805 return false;
6807 /* OpenACC attach / detach clauses must be pointers. */
6808 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
6809 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
6811 tree t = OMP_CLAUSE_DECL (c);
6812 tree type;
6814 while (TREE_CODE (t) == TREE_LIST)
6815 t = TREE_CHAIN (t);
6817 type = TREE_TYPE (t);
6819 if (TREE_CODE (type) == REFERENCE_TYPE)
6820 type = TREE_TYPE (type);
6822 if (TREE_CODE (type) != POINTER_TYPE)
6824 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
6825 user_omp_clause_code_name (c, true));
6826 return true;
6830 return false;
6833 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
6834 Remove any elements from the list that are invalid. */
6836 tree
6837 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
6839 bitmap_head generic_head, firstprivate_head, lastprivate_head;
6840 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
6841 bitmap_head oacc_reduction_head, is_on_device_head;
6842 tree c, t, *pc;
6843 tree safelen = NULL_TREE;
6844 bool branch_seen = false;
6845 bool copyprivate_seen = false;
6846 bool ordered_seen = false;
6847 bool order_seen = false;
6848 bool schedule_seen = false;
6849 bool oacc_async = false;
6850 bool indir_component_ref_p = false;
6851 tree last_iterators = NULL_TREE;
6852 bool last_iterators_remove = false;
6853 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
6854 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
6855 int reduction_seen = 0;
6856 bool allocate_seen = false;
6857 tree detach_seen = NULL_TREE;
6858 bool mergeable_seen = false;
6859 bool implicit_moved = false;
6860 bool target_in_reduction_seen = false;
6862 bitmap_obstack_initialize (NULL);
6863 bitmap_initialize (&generic_head, &bitmap_default_obstack);
6864 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
6865 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
6866 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
6867 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
6868 bitmap_initialize (&map_head, &bitmap_default_obstack);
6869 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
6870 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
6871 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
6872 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
6873 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
6874 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
6876 if (ort & C_ORT_ACC)
6877 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
6878 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
6880 oacc_async = true;
6881 break;
6884 tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
6886 for (pc = &clauses, c = clauses; c ; c = *pc)
6888 bool remove = false;
6889 bool field_ok = false;
6891 /* We've reached the end of a list of expanded nodes. Reset the group
6892 start pointer. */
6893 if (c == grp_sentinel)
6894 grp_start_p = NULL;
6896 switch (OMP_CLAUSE_CODE (c))
6898 case OMP_CLAUSE_SHARED:
6899 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6900 goto check_dup_generic;
6901 case OMP_CLAUSE_PRIVATE:
6902 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6903 goto check_dup_generic;
6904 case OMP_CLAUSE_REDUCTION:
6905 if (reduction_seen == 0)
6906 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
6907 else if (reduction_seen != -2
6908 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
6909 ? -1 : 1))
6911 error_at (OMP_CLAUSE_LOCATION (c),
6912 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
6913 "on the same construct");
6914 reduction_seen = -2;
6916 /* FALLTHRU */
6917 case OMP_CLAUSE_IN_REDUCTION:
6918 case OMP_CLAUSE_TASK_REDUCTION:
6919 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6920 t = OMP_CLAUSE_DECL (c);
6921 if (TREE_CODE (t) == TREE_LIST)
6923 if (handle_omp_array_sections (c, ort))
6925 remove = true;
6926 break;
6928 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
6929 && OMP_CLAUSE_REDUCTION_INSCAN (c))
6931 error_at (OMP_CLAUSE_LOCATION (c),
6932 "%<inscan%> %<reduction%> clause with array "
6933 "section");
6934 remove = true;
6935 break;
6937 if (TREE_CODE (t) == TREE_LIST)
6939 while (TREE_CODE (t) == TREE_LIST)
6940 t = TREE_CHAIN (t);
6942 else
6944 gcc_assert (TREE_CODE (t) == MEM_REF);
6945 t = TREE_OPERAND (t, 0);
6946 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
6947 t = TREE_OPERAND (t, 0);
6948 if (TREE_CODE (t) == ADDR_EXPR
6949 || INDIRECT_REF_P (t))
6950 t = TREE_OPERAND (t, 0);
6952 tree n = omp_clause_decl_field (t);
6953 if (n)
6954 t = n;
6955 goto check_dup_generic_t;
6957 if (oacc_async)
6958 cxx_mark_addressable (t);
6959 goto check_dup_generic;
6960 case OMP_CLAUSE_COPYPRIVATE:
6961 copyprivate_seen = true;
6962 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6963 goto check_dup_generic;
6964 case OMP_CLAUSE_COPYIN:
6965 goto check_dup_generic;
6966 case OMP_CLAUSE_LINEAR:
6967 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
6968 t = OMP_CLAUSE_DECL (c);
6969 if (ort != C_ORT_OMP_DECLARE_SIMD
6970 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
6972 if (OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
6974 error_at (OMP_CLAUSE_LOCATION (c),
6975 "modifier should not be specified in %<linear%> "
6976 "clause on %<simd%> or %<for%> constructs when "
6977 "not using OpenMP 5.2 modifiers");
6978 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
6980 else if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_VAL)
6982 error_at (OMP_CLAUSE_LOCATION (c),
6983 "modifier other than %<val%> specified in "
6984 "%<linear%> clause on %<simd%> or %<for%> "
6985 "constructs when using OpenMP 5.2 modifiers");
6986 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
6989 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
6990 && !type_dependent_expression_p (t))
6992 tree type = TREE_TYPE (t);
6993 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
6994 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
6995 && !TYPE_REF_P (type))
6997 error_at (OMP_CLAUSE_LOCATION (c),
6998 "linear clause with %qs modifier applied to "
6999 "non-reference variable with %qT type",
7000 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
7001 ? "ref" : "uval", TREE_TYPE (t));
7002 remove = true;
7003 break;
7005 if (TYPE_REF_P (type))
7006 type = TREE_TYPE (type);
7007 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
7009 if (!INTEGRAL_TYPE_P (type)
7010 && !TYPE_PTR_P (type))
7012 error_at (OMP_CLAUSE_LOCATION (c),
7013 "linear clause applied to non-integral "
7014 "non-pointer variable with %qT type",
7015 TREE_TYPE (t));
7016 remove = true;
7017 break;
7021 t = OMP_CLAUSE_LINEAR_STEP (c);
7022 if (t == NULL_TREE)
7023 t = integer_one_node;
7024 if (t == error_mark_node)
7026 remove = true;
7027 break;
7029 else if (!type_dependent_expression_p (t)
7030 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
7031 && (ort != C_ORT_OMP_DECLARE_SIMD
7032 || TREE_CODE (t) != PARM_DECL
7033 || !TYPE_REF_P (TREE_TYPE (t))
7034 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
7036 error_at (OMP_CLAUSE_LOCATION (c),
7037 "linear step expression must be integral");
7038 remove = true;
7039 break;
7041 else
7043 t = mark_rvalue_use (t);
7044 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
7046 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
7047 goto check_dup_generic;
7049 if (!processing_template_decl
7050 && (VAR_P (OMP_CLAUSE_DECL (c))
7051 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
7053 if (ort == C_ORT_OMP_DECLARE_SIMD)
7055 t = maybe_constant_value (t);
7056 if (TREE_CODE (t) != INTEGER_CST)
7058 error_at (OMP_CLAUSE_LOCATION (c),
7059 "%<linear%> clause step %qE is neither "
7060 "constant nor a parameter", t);
7061 remove = true;
7062 break;
7065 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7066 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
7067 if (TYPE_REF_P (type))
7068 type = TREE_TYPE (type);
7069 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
7071 type = build_pointer_type (type);
7072 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
7073 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
7074 d, t);
7075 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
7076 MINUS_EXPR, sizetype,
7077 fold_convert (sizetype, t),
7078 fold_convert (sizetype, d));
7079 if (t == error_mark_node)
7081 remove = true;
7082 break;
7085 else if (TYPE_PTR_P (type)
7086 /* Can't multiply the step yet if *this
7087 is still incomplete type. */
7088 && (ort != C_ORT_OMP_DECLARE_SIMD
7089 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
7090 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
7091 || DECL_NAME (OMP_CLAUSE_DECL (c))
7092 != this_identifier
7093 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
7095 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
7096 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
7097 d, t);
7098 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
7099 MINUS_EXPR, sizetype,
7100 fold_convert (sizetype, t),
7101 fold_convert (sizetype, d));
7102 if (t == error_mark_node)
7104 remove = true;
7105 break;
7108 else
7109 t = fold_convert (type, t);
7111 OMP_CLAUSE_LINEAR_STEP (c) = t;
7113 goto check_dup_generic;
7114 check_dup_generic:
7115 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7116 if (t)
7118 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
7119 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7121 else
7122 t = OMP_CLAUSE_DECL (c);
7123 check_dup_generic_t:
7124 if (t == current_class_ptr
7125 && ((ort != C_ORT_OMP_DECLARE_SIMD && ort != C_ORT_ACC)
7126 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
7127 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
7129 error_at (OMP_CLAUSE_LOCATION (c),
7130 "%<this%> allowed in OpenMP only in %<declare simd%>"
7131 " clauses");
7132 remove = true;
7133 break;
7135 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
7136 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
7138 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7139 break;
7140 if (DECL_P (t))
7141 error_at (OMP_CLAUSE_LOCATION (c),
7142 "%qD is not a variable in clause %qs", t,
7143 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7144 else
7145 error_at (OMP_CLAUSE_LOCATION (c),
7146 "%qE is not a variable in clause %qs", t,
7147 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7148 remove = true;
7150 else if ((ort == C_ORT_ACC
7151 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7152 || (ort == C_ORT_OMP
7153 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
7154 || (OMP_CLAUSE_CODE (c)
7155 == OMP_CLAUSE_USE_DEVICE_ADDR)))
7156 || (ort == C_ORT_OMP_TARGET
7157 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
7159 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
7160 && (bitmap_bit_p (&generic_head, DECL_UID (t))
7161 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
7163 error_at (OMP_CLAUSE_LOCATION (c),
7164 "%qD appears more than once in data-sharing "
7165 "clauses", t);
7166 remove = true;
7167 break;
7169 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
7170 target_in_reduction_seen = true;
7171 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
7173 error_at (OMP_CLAUSE_LOCATION (c),
7174 ort == C_ORT_ACC
7175 ? "%qD appears more than once in reduction clauses"
7176 : "%qD appears more than once in data clauses",
7178 remove = true;
7180 else
7181 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
7183 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7184 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
7185 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
7186 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
7188 error_at (OMP_CLAUSE_LOCATION (c),
7189 "%qD appears more than once in data clauses", t);
7190 remove = true;
7192 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7193 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
7194 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
7195 && bitmap_bit_p (&map_head, DECL_UID (t)))
7197 if (ort == C_ORT_ACC)
7198 error_at (OMP_CLAUSE_LOCATION (c),
7199 "%qD appears more than once in data clauses", t);
7200 else
7201 error_at (OMP_CLAUSE_LOCATION (c),
7202 "%qD appears both in data and map clauses", t);
7203 remove = true;
7205 else
7206 bitmap_set_bit (&generic_head, DECL_UID (t));
7207 if (!field_ok)
7208 break;
7209 handle_field_decl:
7210 if (!remove
7211 && TREE_CODE (t) == FIELD_DECL
7212 && t == OMP_CLAUSE_DECL (c))
7214 OMP_CLAUSE_DECL (c)
7215 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
7216 == OMP_CLAUSE_SHARED));
7217 if (OMP_CLAUSE_DECL (c) == error_mark_node)
7218 remove = true;
7220 break;
7222 case OMP_CLAUSE_FIRSTPRIVATE:
7223 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
7225 move_implicit:
7226 implicit_moved = true;
7227 /* Move firstprivate and map clauses with
7228 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
7229 clauses chain. */
7230 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
7231 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
7232 while (*pc1)
7233 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
7234 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
7236 *pc3 = *pc1;
7237 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
7238 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
7240 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
7241 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
7243 *pc2 = *pc1;
7244 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
7245 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
7247 else
7248 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
7249 *pc3 = NULL;
7250 *pc2 = cl2;
7251 *pc1 = cl1;
7252 continue;
7254 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7255 if (t)
7256 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7257 else
7258 t = OMP_CLAUSE_DECL (c);
7259 if (ort != C_ORT_ACC && t == current_class_ptr)
7261 error_at (OMP_CLAUSE_LOCATION (c),
7262 "%<this%> allowed in OpenMP only in %<declare simd%>"
7263 " clauses");
7264 remove = true;
7265 break;
7267 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
7268 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
7269 || TREE_CODE (t) != FIELD_DECL))
7271 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7272 break;
7273 if (DECL_P (t))
7274 error_at (OMP_CLAUSE_LOCATION (c),
7275 "%qD is not a variable in clause %<firstprivate%>",
7277 else
7278 error_at (OMP_CLAUSE_LOCATION (c),
7279 "%qE is not a variable in clause %<firstprivate%>",
7281 remove = true;
7283 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
7284 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
7285 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
7286 remove = true;
7287 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7288 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
7289 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
7291 error_at (OMP_CLAUSE_LOCATION (c),
7292 "%qD appears more than once in data clauses", t);
7293 remove = true;
7295 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
7297 if (ort == C_ORT_ACC)
7298 error_at (OMP_CLAUSE_LOCATION (c),
7299 "%qD appears more than once in data clauses", t);
7300 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
7301 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
7302 /* Silently drop the clause. */;
7303 else
7304 error_at (OMP_CLAUSE_LOCATION (c),
7305 "%qD appears both in data and map clauses", t);
7306 remove = true;
7308 else
7309 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
7310 goto handle_field_decl;
7312 case OMP_CLAUSE_LASTPRIVATE:
7313 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7314 if (t)
7315 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7316 else
7317 t = OMP_CLAUSE_DECL (c);
7318 if (ort != C_ORT_ACC && t == current_class_ptr)
7320 error_at (OMP_CLAUSE_LOCATION (c),
7321 "%<this%> allowed in OpenMP only in %<declare simd%>"
7322 " clauses");
7323 remove = true;
7324 break;
7326 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
7327 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
7328 || TREE_CODE (t) != FIELD_DECL))
7330 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7331 break;
7332 if (DECL_P (t))
7333 error_at (OMP_CLAUSE_LOCATION (c),
7334 "%qD is not a variable in clause %<lastprivate%>",
7336 else
7337 error_at (OMP_CLAUSE_LOCATION (c),
7338 "%qE is not a variable in clause %<lastprivate%>",
7340 remove = true;
7342 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7343 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
7345 error_at (OMP_CLAUSE_LOCATION (c),
7346 "%qD appears more than once in data clauses", t);
7347 remove = true;
7349 else
7350 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
7351 goto handle_field_decl;
7353 case OMP_CLAUSE_IF:
7354 t = OMP_CLAUSE_IF_EXPR (c);
7355 t = maybe_convert_cond (t);
7356 if (t == error_mark_node)
7357 remove = true;
7358 else if (!processing_template_decl)
7359 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7360 OMP_CLAUSE_IF_EXPR (c) = t;
7361 break;
7363 case OMP_CLAUSE_FINAL:
7364 t = OMP_CLAUSE_FINAL_EXPR (c);
7365 t = maybe_convert_cond (t);
7366 if (t == error_mark_node)
7367 remove = true;
7368 else if (!processing_template_decl)
7369 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7370 OMP_CLAUSE_FINAL_EXPR (c) = t;
7371 break;
7373 case OMP_CLAUSE_GANG:
7374 /* Operand 1 is the gang static: argument. */
7375 t = OMP_CLAUSE_OPERAND (c, 1);
7376 if (t != NULL_TREE)
7378 if (t == error_mark_node)
7379 remove = true;
7380 else if (!type_dependent_expression_p (t)
7381 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7383 error_at (OMP_CLAUSE_LOCATION (c),
7384 "%<gang%> static expression must be integral");
7385 remove = true;
7387 else
7389 t = mark_rvalue_use (t);
7390 if (!processing_template_decl)
7392 t = maybe_constant_value (t);
7393 if (TREE_CODE (t) == INTEGER_CST
7394 && tree_int_cst_sgn (t) != 1
7395 && t != integer_minus_one_node)
7397 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7398 "%<gang%> static value must be "
7399 "positive");
7400 t = integer_one_node;
7402 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7405 OMP_CLAUSE_OPERAND (c, 1) = t;
7407 /* Check operand 0, the num argument. */
7408 /* FALLTHRU */
7410 case OMP_CLAUSE_WORKER:
7411 case OMP_CLAUSE_VECTOR:
7412 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
7413 break;
7414 /* FALLTHRU */
7416 case OMP_CLAUSE_NUM_TASKS:
7417 case OMP_CLAUSE_NUM_TEAMS:
7418 case OMP_CLAUSE_NUM_THREADS:
7419 case OMP_CLAUSE_NUM_GANGS:
7420 case OMP_CLAUSE_NUM_WORKERS:
7421 case OMP_CLAUSE_VECTOR_LENGTH:
7422 t = OMP_CLAUSE_OPERAND (c, 0);
7423 if (t == error_mark_node)
7424 remove = true;
7425 else if (!type_dependent_expression_p (t)
7426 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7428 switch (OMP_CLAUSE_CODE (c))
7430 case OMP_CLAUSE_GANG:
7431 error_at (OMP_CLAUSE_LOCATION (c),
7432 "%<gang%> num expression must be integral"); break;
7433 case OMP_CLAUSE_VECTOR:
7434 error_at (OMP_CLAUSE_LOCATION (c),
7435 "%<vector%> length expression must be integral");
7436 break;
7437 case OMP_CLAUSE_WORKER:
7438 error_at (OMP_CLAUSE_LOCATION (c),
7439 "%<worker%> num expression must be integral");
7440 break;
7441 default:
7442 error_at (OMP_CLAUSE_LOCATION (c),
7443 "%qs expression must be integral",
7444 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7446 remove = true;
7448 else
7450 t = mark_rvalue_use (t);
7451 if (!processing_template_decl)
7453 t = maybe_constant_value (t);
7454 if (TREE_CODE (t) == INTEGER_CST
7455 && tree_int_cst_sgn (t) != 1)
7457 switch (OMP_CLAUSE_CODE (c))
7459 case OMP_CLAUSE_GANG:
7460 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7461 "%<gang%> num value must be positive");
7462 break;
7463 case OMP_CLAUSE_VECTOR:
7464 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7465 "%<vector%> length value must be "
7466 "positive");
7467 break;
7468 case OMP_CLAUSE_WORKER:
7469 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7470 "%<worker%> num value must be "
7471 "positive");
7472 break;
7473 default:
7474 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7475 "%qs value must be positive",
7476 omp_clause_code_name
7477 [OMP_CLAUSE_CODE (c)]);
7479 t = integer_one_node;
7481 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7483 OMP_CLAUSE_OPERAND (c, 0) = t;
7485 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
7486 && OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
7487 && !remove)
7489 t = OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c);
7490 if (t == error_mark_node)
7491 remove = true;
7492 else if (!type_dependent_expression_p (t)
7493 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7495 error_at (OMP_CLAUSE_LOCATION (c),
7496 "%qs expression must be integral",
7497 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7498 remove = true;
7500 else
7502 t = mark_rvalue_use (t);
7503 if (!processing_template_decl)
7505 t = maybe_constant_value (t);
7506 if (TREE_CODE (t) == INTEGER_CST
7507 && tree_int_cst_sgn (t) != 1)
7509 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7510 "%qs value must be positive",
7511 omp_clause_code_name
7512 [OMP_CLAUSE_CODE (c)]);
7513 t = NULL_TREE;
7515 else
7516 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7517 tree upper = OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c);
7518 if (t
7519 && TREE_CODE (t) == INTEGER_CST
7520 && TREE_CODE (upper) == INTEGER_CST
7521 && tree_int_cst_lt (upper, t))
7523 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7524 "%<num_teams%> lower bound %qE bigger "
7525 "than upper bound %qE", t, upper);
7526 t = NULL_TREE;
7529 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = t;
7532 break;
7534 case OMP_CLAUSE_SCHEDULE:
7535 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
7536 if (t == NULL)
7538 else if (t == error_mark_node)
7539 remove = true;
7540 else if (!type_dependent_expression_p (t)
7541 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7543 error_at (OMP_CLAUSE_LOCATION (c),
7544 "schedule chunk size expression must be integral");
7545 remove = true;
7547 else
7549 t = mark_rvalue_use (t);
7550 if (!processing_template_decl)
7552 t = maybe_constant_value (t);
7553 if (TREE_CODE (t) == INTEGER_CST
7554 && tree_int_cst_sgn (t) != 1)
7556 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7557 "chunk size value must be positive");
7558 t = integer_one_node;
7560 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7562 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7564 if (!remove)
7565 schedule_seen = true;
7566 break;
7568 case OMP_CLAUSE_SIMDLEN:
7569 case OMP_CLAUSE_SAFELEN:
7570 t = OMP_CLAUSE_OPERAND (c, 0);
7571 if (t == error_mark_node)
7572 remove = true;
7573 else if (!type_dependent_expression_p (t)
7574 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7576 error_at (OMP_CLAUSE_LOCATION (c),
7577 "%qs length expression must be integral",
7578 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7579 remove = true;
7581 else
7583 t = mark_rvalue_use (t);
7584 if (!processing_template_decl)
7586 t = maybe_constant_value (t);
7587 if (TREE_CODE (t) != INTEGER_CST
7588 || tree_int_cst_sgn (t) != 1)
7590 error_at (OMP_CLAUSE_LOCATION (c),
7591 "%qs length expression must be positive "
7592 "constant integer expression",
7593 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7594 remove = true;
7597 OMP_CLAUSE_OPERAND (c, 0) = t;
7598 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
7599 safelen = c;
7601 break;
7603 case OMP_CLAUSE_ASYNC:
7604 t = OMP_CLAUSE_ASYNC_EXPR (c);
7605 if (t == error_mark_node)
7606 remove = true;
7607 else if (!type_dependent_expression_p (t)
7608 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7610 error_at (OMP_CLAUSE_LOCATION (c),
7611 "%<async%> expression must be integral");
7612 remove = true;
7614 else
7616 t = mark_rvalue_use (t);
7617 if (!processing_template_decl)
7618 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7619 OMP_CLAUSE_ASYNC_EXPR (c) = t;
7621 break;
7623 case OMP_CLAUSE_WAIT:
7624 t = OMP_CLAUSE_WAIT_EXPR (c);
7625 if (t == error_mark_node)
7626 remove = true;
7627 else if (!processing_template_decl)
7628 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7629 OMP_CLAUSE_WAIT_EXPR (c) = t;
7630 break;
7632 case OMP_CLAUSE_THREAD_LIMIT:
7633 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
7634 if (t == error_mark_node)
7635 remove = true;
7636 else if (!type_dependent_expression_p (t)
7637 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7639 error_at (OMP_CLAUSE_LOCATION (c),
7640 "%<thread_limit%> expression must be integral");
7641 remove = true;
7643 else
7645 t = mark_rvalue_use (t);
7646 if (!processing_template_decl)
7648 t = maybe_constant_value (t);
7649 if (TREE_CODE (t) == INTEGER_CST
7650 && tree_int_cst_sgn (t) != 1)
7652 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7653 "%<thread_limit%> value must be positive");
7654 t = integer_one_node;
7656 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7658 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
7660 break;
7662 case OMP_CLAUSE_DEVICE:
7663 t = OMP_CLAUSE_DEVICE_ID (c);
7664 if (t == error_mark_node)
7665 remove = true;
7666 else if (!type_dependent_expression_p (t)
7667 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7669 error_at (OMP_CLAUSE_LOCATION (c),
7670 "%<device%> id must be integral");
7671 remove = true;
7673 else if (OMP_CLAUSE_DEVICE_ANCESTOR (c)
7674 && TREE_CODE (t) == INTEGER_CST
7675 && !integer_onep (t))
7677 error_at (OMP_CLAUSE_LOCATION (c),
7678 "the %<device%> clause expression must evaluate to "
7679 "%<1%>");
7680 remove = true;
7682 else
7684 t = mark_rvalue_use (t);
7685 if (!processing_template_decl)
7686 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7687 OMP_CLAUSE_DEVICE_ID (c) = t;
7689 break;
7691 case OMP_CLAUSE_DIST_SCHEDULE:
7692 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
7693 if (t == NULL)
7695 else if (t == error_mark_node)
7696 remove = true;
7697 else if (!type_dependent_expression_p (t)
7698 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7700 error_at (OMP_CLAUSE_LOCATION (c),
7701 "%<dist_schedule%> chunk size expression must be "
7702 "integral");
7703 remove = true;
7705 else
7707 t = mark_rvalue_use (t);
7708 if (!processing_template_decl)
7709 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7710 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
7712 break;
7714 case OMP_CLAUSE_ALIGNED:
7715 t = OMP_CLAUSE_DECL (c);
7716 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
7718 error_at (OMP_CLAUSE_LOCATION (c),
7719 "%<this%> allowed in OpenMP only in %<declare simd%>"
7720 " clauses");
7721 remove = true;
7722 break;
7724 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7726 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7727 break;
7728 if (DECL_P (t))
7729 error_at (OMP_CLAUSE_LOCATION (c),
7730 "%qD is not a variable in %<aligned%> clause", t);
7731 else
7732 error_at (OMP_CLAUSE_LOCATION (c),
7733 "%qE is not a variable in %<aligned%> clause", t);
7734 remove = true;
7736 else if (!type_dependent_expression_p (t)
7737 && !TYPE_PTR_P (TREE_TYPE (t))
7738 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
7739 && (!TYPE_REF_P (TREE_TYPE (t))
7740 || (!INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
7741 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
7742 != ARRAY_TYPE))))
7744 error_at (OMP_CLAUSE_LOCATION (c),
7745 "%qE in %<aligned%> clause is neither a pointer nor "
7746 "an array nor a reference to pointer or array", t);
7747 remove = true;
7749 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
7751 error_at (OMP_CLAUSE_LOCATION (c),
7752 "%qD appears more than once in %<aligned%> clauses",
7754 remove = true;
7756 else
7757 bitmap_set_bit (&aligned_head, DECL_UID (t));
7758 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
7759 if (t == error_mark_node)
7760 remove = true;
7761 else if (t == NULL_TREE)
7762 break;
7763 else if (!type_dependent_expression_p (t)
7764 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7766 error_at (OMP_CLAUSE_LOCATION (c),
7767 "%<aligned%> clause alignment expression must "
7768 "be integral");
7769 remove = true;
7771 else
7773 t = mark_rvalue_use (t);
7774 if (!processing_template_decl)
7776 t = maybe_constant_value (t);
7777 if (TREE_CODE (t) != INTEGER_CST
7778 || tree_int_cst_sgn (t) != 1)
7780 error_at (OMP_CLAUSE_LOCATION (c),
7781 "%<aligned%> clause alignment expression must "
7782 "be positive constant integer expression");
7783 remove = true;
7785 else
7786 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7788 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
7790 break;
7792 case OMP_CLAUSE_NONTEMPORAL:
7793 t = OMP_CLAUSE_DECL (c);
7794 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7796 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7797 break;
7798 if (DECL_P (t))
7799 error_at (OMP_CLAUSE_LOCATION (c),
7800 "%qD is not a variable in %<nontemporal%> clause",
7802 else
7803 error_at (OMP_CLAUSE_LOCATION (c),
7804 "%qE is not a variable in %<nontemporal%> clause",
7806 remove = true;
7808 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
7810 error_at (OMP_CLAUSE_LOCATION (c),
7811 "%qD appears more than once in %<nontemporal%> "
7812 "clauses", t);
7813 remove = true;
7815 else
7816 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
7817 break;
7819 case OMP_CLAUSE_ALLOCATE:
7820 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7821 if (t)
7822 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7823 else
7824 t = OMP_CLAUSE_DECL (c);
7825 if (t == current_class_ptr)
7827 error_at (OMP_CLAUSE_LOCATION (c),
7828 "%<this%> not allowed in %<allocate%> clause");
7829 remove = true;
7830 break;
7832 if (!VAR_P (t)
7833 && TREE_CODE (t) != PARM_DECL
7834 && TREE_CODE (t) != FIELD_DECL)
7836 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7837 break;
7838 if (DECL_P (t))
7839 error_at (OMP_CLAUSE_LOCATION (c),
7840 "%qD is not a variable in %<allocate%> clause", t);
7841 else
7842 error_at (OMP_CLAUSE_LOCATION (c),
7843 "%qE is not a variable in %<allocate%> clause", t);
7844 remove = true;
7846 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
7848 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7849 "%qD appears more than once in %<allocate%> clauses",
7851 remove = true;
7853 else
7855 bitmap_set_bit (&aligned_head, DECL_UID (t));
7856 allocate_seen = true;
7858 tree allocator, align;
7859 align = OMP_CLAUSE_ALLOCATE_ALIGN (c);
7860 if (error_operand_p (align))
7862 remove = true;
7863 break;
7865 if (align)
7867 if (!type_dependent_expression_p (align)
7868 && !INTEGRAL_TYPE_P (TREE_TYPE (align)))
7870 error_at (OMP_CLAUSE_LOCATION (c),
7871 "%<allocate%> clause %<align%> modifier "
7872 "argument needs to be positive constant "
7873 "power of two integer expression");
7874 remove = true;
7876 else
7878 align = mark_rvalue_use (align);
7879 if (!processing_template_decl)
7881 align = maybe_constant_value (align);
7882 if (TREE_CODE (align) != INTEGER_CST
7883 || !tree_fits_uhwi_p (align)
7884 || !integer_pow2p (align))
7886 error_at (OMP_CLAUSE_LOCATION (c),
7887 "%<allocate%> clause %<align%> modifier "
7888 "argument needs to be positive constant "
7889 "power of two integer expression");
7890 remove = true;
7894 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
7896 allocator = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
7897 if (error_operand_p (allocator))
7899 remove = true;
7900 break;
7902 if (allocator == NULL_TREE)
7903 goto handle_field_decl;
7904 tree allocatort;
7905 allocatort = TYPE_MAIN_VARIANT (TREE_TYPE (allocator));
7906 if (!type_dependent_expression_p (allocator)
7907 && (TREE_CODE (allocatort) != ENUMERAL_TYPE
7908 || TYPE_NAME (allocatort) == NULL_TREE
7909 || TREE_CODE (TYPE_NAME (allocatort)) != TYPE_DECL
7910 || (DECL_NAME (TYPE_NAME (allocatort))
7911 != get_identifier ("omp_allocator_handle_t"))
7912 || (TYPE_CONTEXT (allocatort)
7913 != DECL_CONTEXT (global_namespace))))
7915 error_at (OMP_CLAUSE_LOCATION (c),
7916 "%<allocate%> clause allocator expression has "
7917 "type %qT rather than %<omp_allocator_handle_t%>",
7918 TREE_TYPE (allocator));
7919 remove = true;
7920 break;
7922 else
7924 allocator = mark_rvalue_use (allocator);
7925 if (!processing_template_decl)
7926 allocator = maybe_constant_value (allocator);
7927 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
7929 goto handle_field_decl;
7931 case OMP_CLAUSE_DOACROSS:
7932 t = OMP_CLAUSE_DECL (c);
7933 if (t == NULL_TREE)
7934 break;
7935 if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
7937 if (cp_finish_omp_clause_doacross_sink (c))
7938 remove = true;
7939 break;
7941 gcc_unreachable ();
7942 case OMP_CLAUSE_DEPEND:
7943 case OMP_CLAUSE_AFFINITY:
7944 t = OMP_CLAUSE_DECL (c);
7945 if (TREE_CODE (t) == TREE_LIST
7946 && TREE_PURPOSE (t)
7947 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
7949 if (TREE_PURPOSE (t) != last_iterators)
7950 last_iterators_remove
7951 = cp_omp_finish_iterators (TREE_PURPOSE (t));
7952 last_iterators = TREE_PURPOSE (t);
7953 t = TREE_VALUE (t);
7954 if (last_iterators_remove)
7955 t = error_mark_node;
7957 else
7958 last_iterators = NULL_TREE;
7960 if (TREE_CODE (t) == TREE_LIST)
7962 if (handle_omp_array_sections (c, ort))
7963 remove = true;
7964 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
7965 && (OMP_CLAUSE_DEPEND_KIND (c)
7966 == OMP_CLAUSE_DEPEND_DEPOBJ))
7968 error_at (OMP_CLAUSE_LOCATION (c),
7969 "%<depend%> clause with %<depobj%> dependence "
7970 "type on array section");
7971 remove = true;
7973 break;
7975 if (t == error_mark_node)
7976 remove = true;
7977 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
7978 && t == ridpointers[RID_OMP_ALL_MEMORY])
7980 if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
7981 && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
7983 error_at (OMP_CLAUSE_LOCATION (c),
7984 "%<omp_all_memory%> used with %<depend%> kind "
7985 "other than %<out%> or %<inout%>");
7986 remove = true;
7988 if (processing_template_decl)
7989 break;
7991 else if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7992 break;
7993 else if (!lvalue_p (t))
7995 if (DECL_P (t))
7996 error_at (OMP_CLAUSE_LOCATION (c),
7997 "%qD is not lvalue expression nor array section "
7998 "in %qs clause", t,
7999 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8000 else
8001 error_at (OMP_CLAUSE_LOCATION (c),
8002 "%qE is not lvalue expression nor array section "
8003 "in %qs clause", t,
8004 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8005 remove = true;
8007 else if (TREE_CODE (t) == COMPONENT_REF
8008 && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
8009 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
8011 error_at (OMP_CLAUSE_LOCATION (c),
8012 "bit-field %qE in %qs clause", t,
8013 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8014 remove = true;
8016 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
8017 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
8019 if (!c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
8020 ? TREE_TYPE (TREE_TYPE (t))
8021 : TREE_TYPE (t)))
8023 error_at (OMP_CLAUSE_LOCATION (c),
8024 "%qE does not have %<omp_depend_t%> type in "
8025 "%<depend%> clause with %<depobj%> dependence "
8026 "type", t);
8027 remove = true;
8030 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
8031 && c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
8032 ? TREE_TYPE (TREE_TYPE (t))
8033 : TREE_TYPE (t)))
8035 error_at (OMP_CLAUSE_LOCATION (c),
8036 "%qE should not have %<omp_depend_t%> type in "
8037 "%<depend%> clause with dependence type other than "
8038 "%<depobj%>", t);
8039 remove = true;
8041 if (!remove)
8043 if (t == ridpointers[RID_OMP_ALL_MEMORY])
8044 t = null_pointer_node;
8045 else
8047 tree addr = cp_build_addr_expr (t, tf_warning_or_error);
8048 if (addr == error_mark_node)
8050 remove = true;
8051 break;
8053 t = cp_build_indirect_ref (OMP_CLAUSE_LOCATION (c),
8054 addr, RO_UNARY_STAR,
8055 tf_warning_or_error);
8056 if (t == error_mark_node)
8058 remove = true;
8059 break;
8062 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
8063 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
8064 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
8065 == TREE_VEC))
8066 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
8067 else
8068 OMP_CLAUSE_DECL (c) = t;
8070 break;
8071 case OMP_CLAUSE_DETACH:
8072 t = OMP_CLAUSE_DECL (c);
8073 if (detach_seen)
8075 error_at (OMP_CLAUSE_LOCATION (c),
8076 "too many %qs clauses on a task construct",
8077 "detach");
8078 remove = true;
8079 break;
8081 else if (error_operand_p (t))
8083 remove = true;
8084 break;
8086 else
8088 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
8089 if (!type_dependent_expression_p (t)
8090 && (!INTEGRAL_TYPE_P (type)
8091 || TREE_CODE (type) != ENUMERAL_TYPE
8092 || TYPE_NAME (type) == NULL_TREE
8093 || (DECL_NAME (TYPE_NAME (type))
8094 != get_identifier ("omp_event_handle_t"))))
8096 error_at (OMP_CLAUSE_LOCATION (c),
8097 "%<detach%> clause event handle "
8098 "has type %qT rather than "
8099 "%<omp_event_handle_t%>",
8100 type);
8101 remove = true;
8103 detach_seen = c;
8104 cxx_mark_addressable (t);
8106 break;
8108 case OMP_CLAUSE_MAP:
8109 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
8110 goto move_implicit;
8111 /* FALLTHRU */
8112 case OMP_CLAUSE_TO:
8113 case OMP_CLAUSE_FROM:
8114 case OMP_CLAUSE__CACHE_:
8115 t = OMP_CLAUSE_DECL (c);
8116 if (TREE_CODE (t) == TREE_LIST)
8118 grp_start_p = pc;
8119 grp_sentinel = OMP_CLAUSE_CHAIN (c);
8121 if (handle_omp_array_sections (c, ort))
8122 remove = true;
8123 else
8125 t = OMP_CLAUSE_DECL (c);
8126 if (TREE_CODE (t) != TREE_LIST
8127 && !type_dependent_expression_p (t)
8128 && !omp_mappable_type (TREE_TYPE (t)))
8130 error_at (OMP_CLAUSE_LOCATION (c),
8131 "array section does not have mappable type "
8132 "in %qs clause",
8133 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8134 if (TREE_TYPE (t) != error_mark_node
8135 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
8136 cxx_incomplete_type_inform (TREE_TYPE (t));
8137 remove = true;
8139 while (TREE_CODE (t) == ARRAY_REF)
8140 t = TREE_OPERAND (t, 0);
8141 if (TREE_CODE (t) == COMPONENT_REF
8142 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
8146 t = TREE_OPERAND (t, 0);
8147 if (REFERENCE_REF_P (t))
8148 t = TREE_OPERAND (t, 0);
8149 if (TREE_CODE (t) == MEM_REF
8150 || INDIRECT_REF_P (t))
8152 t = TREE_OPERAND (t, 0);
8153 STRIP_NOPS (t);
8154 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
8155 t = TREE_OPERAND (t, 0);
8158 while (TREE_CODE (t) == COMPONENT_REF
8159 || TREE_CODE (t) == ARRAY_REF);
8161 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8162 && OMP_CLAUSE_MAP_IMPLICIT (c)
8163 && (bitmap_bit_p (&map_head, DECL_UID (t))
8164 || bitmap_bit_p (&map_field_head, DECL_UID (t))
8165 || bitmap_bit_p (&map_firstprivate_head,
8166 DECL_UID (t))))
8168 remove = true;
8169 break;
8171 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
8172 break;
8173 if (bitmap_bit_p (&map_head, DECL_UID (t)))
8175 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
8176 error_at (OMP_CLAUSE_LOCATION (c),
8177 "%qD appears more than once in motion"
8178 " clauses", t);
8179 else if (ort == C_ORT_ACC)
8180 error_at (OMP_CLAUSE_LOCATION (c),
8181 "%qD appears more than once in data"
8182 " clauses", t);
8183 else
8184 error_at (OMP_CLAUSE_LOCATION (c),
8185 "%qD appears more than once in map"
8186 " clauses", t);
8187 remove = true;
8189 else
8191 bitmap_set_bit (&map_head, DECL_UID (t));
8192 bitmap_set_bit (&map_field_head, DECL_UID (t));
8196 if (cp_oacc_check_attachments (c))
8197 remove = true;
8198 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8199 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
8200 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
8201 /* In this case, we have a single array element which is a
8202 pointer, and we already set OMP_CLAUSE_SIZE in
8203 handle_omp_array_sections above. For attach/detach clauses,
8204 reset the OMP_CLAUSE_SIZE (representing a bias) to zero
8205 here. */
8206 OMP_CLAUSE_SIZE (c) = size_zero_node;
8207 break;
8209 if (t == error_mark_node)
8211 remove = true;
8212 break;
8214 /* OpenACC attach / detach clauses must be pointers. */
8215 if (cp_oacc_check_attachments (c))
8217 remove = true;
8218 break;
8220 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8221 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
8222 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
8223 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
8224 bias) to zero here, so it is not set erroneously to the pointer
8225 size later on in gimplify.cc. */
8226 OMP_CLAUSE_SIZE (c) = size_zero_node;
8227 if (REFERENCE_REF_P (t)
8228 && TREE_CODE (TREE_OPERAND (t, 0)) == COMPONENT_REF)
8230 t = TREE_OPERAND (t, 0);
8231 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8232 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH)
8233 OMP_CLAUSE_DECL (c) = t;
8235 while (INDIRECT_REF_P (t)
8236 || TREE_CODE (t) == ARRAY_REF)
8238 t = TREE_OPERAND (t, 0);
8239 STRIP_NOPS (t);
8240 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
8241 t = TREE_OPERAND (t, 0);
8243 while (TREE_CODE (t) == COMPOUND_EXPR)
8245 t = TREE_OPERAND (t, 1);
8246 STRIP_NOPS (t);
8248 if (TREE_CODE (t) == COMPONENT_REF
8249 && invalid_nonstatic_memfn_p (EXPR_LOCATION (t), t,
8250 tf_warning_or_error))
8251 remove = true;
8252 indir_component_ref_p = false;
8253 if (TREE_CODE (t) == COMPONENT_REF
8254 && (INDIRECT_REF_P (TREE_OPERAND (t, 0))
8255 || TREE_CODE (TREE_OPERAND (t, 0)) == ARRAY_REF))
8257 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
8258 indir_component_ref_p = true;
8259 STRIP_NOPS (t);
8260 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
8261 t = TREE_OPERAND (t, 0);
8263 if (TREE_CODE (t) == COMPONENT_REF
8264 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
8266 if (type_dependent_expression_p (t))
8267 break;
8268 if (TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
8269 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
8271 error_at (OMP_CLAUSE_LOCATION (c),
8272 "bit-field %qE in %qs clause",
8273 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8274 remove = true;
8276 else if (!omp_mappable_type (TREE_TYPE (t)))
8278 error_at (OMP_CLAUSE_LOCATION (c),
8279 "%qE does not have a mappable type in %qs clause",
8280 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8281 if (TREE_TYPE (t) != error_mark_node
8282 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
8283 cxx_incomplete_type_inform (TREE_TYPE (t));
8284 remove = true;
8286 while (TREE_CODE (t) == COMPONENT_REF)
8288 if (TREE_TYPE (TREE_OPERAND (t, 0))
8289 && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
8290 == UNION_TYPE))
8292 error_at (OMP_CLAUSE_LOCATION (c),
8293 "%qE is a member of a union", t);
8294 remove = true;
8295 break;
8297 t = TREE_OPERAND (t, 0);
8298 if (TREE_CODE (t) == MEM_REF)
8300 if (maybe_ne (mem_ref_offset (t), 0))
8301 error_at (OMP_CLAUSE_LOCATION (c),
8302 "cannot dereference %qE in %qs clause", t,
8303 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8304 else
8305 t = TREE_OPERAND (t, 0);
8307 while (TREE_CODE (t) == MEM_REF
8308 || INDIRECT_REF_P (t)
8309 || TREE_CODE (t) == ARRAY_REF)
8311 t = TREE_OPERAND (t, 0);
8312 STRIP_NOPS (t);
8313 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
8314 t = TREE_OPERAND (t, 0);
8317 if (remove)
8318 break;
8319 if (REFERENCE_REF_P (t))
8320 t = TREE_OPERAND (t, 0);
8321 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
8323 if (bitmap_bit_p (&map_field_head, DECL_UID (t))
8324 || (ort != C_ORT_ACC
8325 && bitmap_bit_p (&map_head, DECL_UID (t))))
8326 goto handle_map_references;
8329 if (!processing_template_decl
8330 && TREE_CODE (t) == FIELD_DECL)
8332 OMP_CLAUSE_DECL (c) = finish_non_static_data_member (t, NULL_TREE,
8333 NULL_TREE);
8334 break;
8336 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
8338 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
8339 break;
8340 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8341 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
8342 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER
8343 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH))
8344 break;
8345 if (DECL_P (t))
8346 error_at (OMP_CLAUSE_LOCATION (c),
8347 "%qD is not a variable in %qs clause", t,
8348 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8349 else
8350 error_at (OMP_CLAUSE_LOCATION (c),
8351 "%qE is not a variable in %qs clause", t,
8352 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8353 remove = true;
8355 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
8357 error_at (OMP_CLAUSE_LOCATION (c),
8358 "%qD is threadprivate variable in %qs clause", t,
8359 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8360 remove = true;
8362 else if (!processing_template_decl
8363 && !TYPE_REF_P (TREE_TYPE (t))
8364 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
8365 || (OMP_CLAUSE_MAP_KIND (c)
8366 != GOMP_MAP_FIRSTPRIVATE_POINTER))
8367 && !indir_component_ref_p
8368 && !cxx_mark_addressable (t))
8369 remove = true;
8370 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8371 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
8372 || (OMP_CLAUSE_MAP_KIND (c)
8373 == GOMP_MAP_FIRSTPRIVATE_POINTER)))
8374 && t == OMP_CLAUSE_DECL (c)
8375 && !type_dependent_expression_p (t)
8376 && !omp_mappable_type (TYPE_REF_P (TREE_TYPE (t))
8377 ? TREE_TYPE (TREE_TYPE (t))
8378 : TREE_TYPE (t)))
8380 error_at (OMP_CLAUSE_LOCATION (c),
8381 "%qD does not have a mappable type in %qs clause", t,
8382 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8383 if (TREE_TYPE (t) != error_mark_node
8384 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
8385 cxx_incomplete_type_inform (TREE_TYPE (t));
8386 remove = true;
8388 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8389 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
8390 && !type_dependent_expression_p (t)
8391 && !INDIRECT_TYPE_P (TREE_TYPE (t)))
8393 error_at (OMP_CLAUSE_LOCATION (c),
8394 "%qD is not a pointer variable", t);
8395 remove = true;
8397 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8398 && OMP_CLAUSE_MAP_IMPLICIT (c)
8399 && (bitmap_bit_p (&map_head, DECL_UID (t))
8400 || bitmap_bit_p (&map_field_head, DECL_UID (t))
8401 || bitmap_bit_p (&map_firstprivate_head,
8402 DECL_UID (t))))
8403 remove = true;
8404 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8405 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
8407 if (bitmap_bit_p (&generic_head, DECL_UID (t))
8408 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
8409 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
8411 error_at (OMP_CLAUSE_LOCATION (c),
8412 "%qD appears more than once in data clauses", t);
8413 remove = true;
8415 else if (bitmap_bit_p (&map_head, DECL_UID (t))
8416 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
8418 if (ort == C_ORT_ACC)
8419 error_at (OMP_CLAUSE_LOCATION (c),
8420 "%qD appears more than once in data clauses", t);
8421 else
8422 error_at (OMP_CLAUSE_LOCATION (c),
8423 "%qD appears both in data and map clauses", t);
8424 remove = true;
8426 else
8427 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
8429 else if (bitmap_bit_p (&map_head, DECL_UID (t))
8430 && !bitmap_bit_p (&map_field_head, DECL_UID (t)))
8432 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
8433 error_at (OMP_CLAUSE_LOCATION (c),
8434 "%qD appears more than once in motion clauses", t);
8435 else if (ort == C_ORT_ACC)
8436 error_at (OMP_CLAUSE_LOCATION (c),
8437 "%qD appears more than once in data clauses", t);
8438 else
8439 error_at (OMP_CLAUSE_LOCATION (c),
8440 "%qD appears more than once in map clauses", t);
8441 remove = true;
8443 else if (ort == C_ORT_ACC
8444 && bitmap_bit_p (&generic_head, DECL_UID (t)))
8446 error_at (OMP_CLAUSE_LOCATION (c),
8447 "%qD appears more than once in data clauses", t);
8448 remove = true;
8450 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
8451 || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
8453 if (ort == C_ORT_ACC)
8454 error_at (OMP_CLAUSE_LOCATION (c),
8455 "%qD appears more than once in data clauses", t);
8456 else
8457 error_at (OMP_CLAUSE_LOCATION (c),
8458 "%qD appears both in data and map clauses", t);
8459 remove = true;
8461 else
8463 bitmap_set_bit (&map_head, DECL_UID (t));
8465 tree decl = OMP_CLAUSE_DECL (c);
8466 if (t != decl
8467 && (TREE_CODE (decl) == COMPONENT_REF
8468 || (INDIRECT_REF_P (decl)
8469 && TREE_CODE (TREE_OPERAND (decl, 0)) == COMPONENT_REF
8470 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (decl, 0))))))
8471 bitmap_set_bit (&map_field_head, DECL_UID (t));
8473 handle_map_references:
8474 if (!remove
8475 && !processing_template_decl
8476 && ort != C_ORT_DECLARE_SIMD
8477 && TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c))))
8479 t = OMP_CLAUSE_DECL (c);
8480 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
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)));
8487 else if (OMP_CLAUSE_MAP_KIND (c)
8488 != GOMP_MAP_FIRSTPRIVATE_POINTER
8489 && (OMP_CLAUSE_MAP_KIND (c)
8490 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
8491 && (OMP_CLAUSE_MAP_KIND (c)
8492 != GOMP_MAP_ALWAYS_POINTER)
8493 && (OMP_CLAUSE_MAP_KIND (c)
8494 != GOMP_MAP_ATTACH_DETACH))
8496 grp_start_p = pc;
8497 grp_sentinel = OMP_CLAUSE_CHAIN (c);
8499 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c),
8500 OMP_CLAUSE_MAP);
8501 if (TREE_CODE (t) == COMPONENT_REF)
8502 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
8503 else
8504 OMP_CLAUSE_SET_MAP_KIND (c2,
8505 GOMP_MAP_FIRSTPRIVATE_REFERENCE);
8506 OMP_CLAUSE_DECL (c2) = t;
8507 OMP_CLAUSE_SIZE (c2) = size_zero_node;
8508 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
8509 OMP_CLAUSE_CHAIN (c) = c2;
8510 OMP_CLAUSE_DECL (c) = build_simple_mem_ref (t);
8511 if (OMP_CLAUSE_SIZE (c) == NULL_TREE)
8512 OMP_CLAUSE_SIZE (c)
8513 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t)));
8514 c = c2;
8517 break;
8519 case OMP_CLAUSE_ENTER:
8520 case OMP_CLAUSE_LINK:
8521 t = OMP_CLAUSE_DECL (c);
8522 const char *cname;
8523 cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
8524 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
8525 && OMP_CLAUSE_ENTER_TO (c))
8526 cname = "to";
8527 if (TREE_CODE (t) == FUNCTION_DECL
8528 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
8530 else if (!VAR_P (t))
8532 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
8534 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
8535 error_at (OMP_CLAUSE_LOCATION (c),
8536 "template %qE in clause %qs", t, cname);
8537 else if (really_overloaded_fn (t))
8538 error_at (OMP_CLAUSE_LOCATION (c),
8539 "overloaded function name %qE in clause %qs", t,
8540 cname);
8541 else
8542 error_at (OMP_CLAUSE_LOCATION (c),
8543 "%qE is neither a variable nor a function name "
8544 "in clause %qs", t, cname);
8546 else
8547 error_at (OMP_CLAUSE_LOCATION (c),
8548 "%qE is not a variable in clause %qs", t, cname);
8549 remove = true;
8551 else if (DECL_THREAD_LOCAL_P (t))
8553 error_at (OMP_CLAUSE_LOCATION (c),
8554 "%qD is threadprivate variable in %qs clause", t,
8555 cname);
8556 remove = true;
8558 else if (!omp_mappable_type (TREE_TYPE (t)))
8560 error_at (OMP_CLAUSE_LOCATION (c),
8561 "%qD does not have a mappable type in %qs clause", t,
8562 cname);
8563 if (TREE_TYPE (t) != error_mark_node
8564 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
8565 cxx_incomplete_type_inform (TREE_TYPE (t));
8566 remove = true;
8568 if (remove)
8569 break;
8570 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
8572 error_at (OMP_CLAUSE_LOCATION (c),
8573 "%qE appears more than once on the same "
8574 "%<declare target%> directive", t);
8575 remove = true;
8577 else
8578 bitmap_set_bit (&generic_head, DECL_UID (t));
8579 break;
8581 case OMP_CLAUSE_UNIFORM:
8582 t = OMP_CLAUSE_DECL (c);
8583 if (TREE_CODE (t) != PARM_DECL)
8585 if (processing_template_decl)
8586 break;
8587 if (DECL_P (t))
8588 error_at (OMP_CLAUSE_LOCATION (c),
8589 "%qD is not an argument in %<uniform%> clause", t);
8590 else
8591 error_at (OMP_CLAUSE_LOCATION (c),
8592 "%qE is not an argument in %<uniform%> clause", t);
8593 remove = true;
8594 break;
8596 /* map_head bitmap is used as uniform_head if declare_simd. */
8597 bitmap_set_bit (&map_head, DECL_UID (t));
8598 goto check_dup_generic;
8600 case OMP_CLAUSE_GRAINSIZE:
8601 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
8602 if (t == error_mark_node)
8603 remove = true;
8604 else if (!type_dependent_expression_p (t)
8605 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8607 error_at (OMP_CLAUSE_LOCATION (c),
8608 "%<grainsize%> expression must be integral");
8609 remove = true;
8611 else
8613 t = mark_rvalue_use (t);
8614 if (!processing_template_decl)
8616 t = maybe_constant_value (t);
8617 if (TREE_CODE (t) == INTEGER_CST
8618 && tree_int_cst_sgn (t) != 1)
8620 warning_at (OMP_CLAUSE_LOCATION (c), 0,
8621 "%<grainsize%> value must be positive");
8622 t = integer_one_node;
8624 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8626 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
8628 break;
8630 case OMP_CLAUSE_PRIORITY:
8631 t = OMP_CLAUSE_PRIORITY_EXPR (c);
8632 if (t == error_mark_node)
8633 remove = true;
8634 else if (!type_dependent_expression_p (t)
8635 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8637 error_at (OMP_CLAUSE_LOCATION (c),
8638 "%<priority%> expression must be integral");
8639 remove = true;
8641 else
8643 t = mark_rvalue_use (t);
8644 if (!processing_template_decl)
8646 t = maybe_constant_value (t);
8647 if (TREE_CODE (t) == INTEGER_CST
8648 && tree_int_cst_sgn (t) == -1)
8650 warning_at (OMP_CLAUSE_LOCATION (c), 0,
8651 "%<priority%> value must be non-negative");
8652 t = integer_one_node;
8654 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8656 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
8658 break;
8660 case OMP_CLAUSE_HINT:
8661 t = OMP_CLAUSE_HINT_EXPR (c);
8662 if (t == error_mark_node)
8663 remove = true;
8664 else if (!type_dependent_expression_p (t)
8665 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8667 error_at (OMP_CLAUSE_LOCATION (c),
8668 "%<hint%> expression must be integral");
8669 remove = true;
8671 else
8673 t = mark_rvalue_use (t);
8674 if (!processing_template_decl)
8676 t = maybe_constant_value (t);
8677 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8678 if (TREE_CODE (t) != INTEGER_CST)
8680 error_at (OMP_CLAUSE_LOCATION (c),
8681 "%<hint%> expression must be constant integer "
8682 "expression");
8683 remove = true;
8686 OMP_CLAUSE_HINT_EXPR (c) = t;
8688 break;
8690 case OMP_CLAUSE_FILTER:
8691 t = OMP_CLAUSE_FILTER_EXPR (c);
8692 if (t == error_mark_node)
8693 remove = true;
8694 else if (!type_dependent_expression_p (t)
8695 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8697 error_at (OMP_CLAUSE_LOCATION (c),
8698 "%<filter%> expression must be integral");
8699 remove = true;
8701 else
8703 t = mark_rvalue_use (t);
8704 if (!processing_template_decl)
8706 t = maybe_constant_value (t);
8707 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8709 OMP_CLAUSE_FILTER_EXPR (c) = t;
8711 break;
8713 case OMP_CLAUSE_IS_DEVICE_PTR:
8714 case OMP_CLAUSE_USE_DEVICE_PTR:
8715 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
8716 t = OMP_CLAUSE_DECL (c);
8717 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
8718 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
8719 if (!type_dependent_expression_p (t))
8721 tree type = TREE_TYPE (t);
8722 if (!TYPE_PTR_P (type)
8723 && (!TYPE_REF_P (type) || !TYPE_PTR_P (TREE_TYPE (type))))
8725 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
8726 && ort == C_ORT_OMP)
8728 error_at (OMP_CLAUSE_LOCATION (c),
8729 "%qs variable is neither a pointer "
8730 "nor reference to pointer",
8731 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8732 remove = true;
8734 else if (TREE_CODE (type) != ARRAY_TYPE
8735 && (!TYPE_REF_P (type)
8736 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8738 error_at (OMP_CLAUSE_LOCATION (c),
8739 "%qs variable is neither a pointer, nor an "
8740 "array nor reference to pointer or array",
8741 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8742 remove = true;
8746 goto check_dup_generic;
8748 case OMP_CLAUSE_HAS_DEVICE_ADDR:
8749 t = OMP_CLAUSE_DECL (c);
8750 if (TREE_CODE (t) == TREE_LIST)
8752 if (handle_omp_array_sections (c, ort))
8753 remove = true;
8754 else
8756 t = OMP_CLAUSE_DECL (c);
8757 while (TREE_CODE (t) == TREE_LIST)
8758 t = TREE_CHAIN (t);
8759 while (INDIRECT_REF_P (t)
8760 || TREE_CODE (t) == ARRAY_REF)
8761 t = TREE_OPERAND (t, 0);
8764 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
8766 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
8767 if (!processing_template_decl
8768 && !cxx_mark_addressable (t))
8769 remove = true;
8771 goto check_dup_generic_t;
8773 case OMP_CLAUSE_USE_DEVICE_ADDR:
8774 field_ok = true;
8775 t = OMP_CLAUSE_DECL (c);
8776 if (!processing_template_decl
8777 && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
8778 && !TYPE_REF_P (TREE_TYPE (t))
8779 && !cxx_mark_addressable (t))
8780 remove = true;
8781 goto check_dup_generic;
8783 case OMP_CLAUSE_NOWAIT:
8784 case OMP_CLAUSE_DEFAULT:
8785 case OMP_CLAUSE_UNTIED:
8786 case OMP_CLAUSE_COLLAPSE:
8787 case OMP_CLAUSE_PARALLEL:
8788 case OMP_CLAUSE_FOR:
8789 case OMP_CLAUSE_SECTIONS:
8790 case OMP_CLAUSE_TASKGROUP:
8791 case OMP_CLAUSE_PROC_BIND:
8792 case OMP_CLAUSE_DEVICE_TYPE:
8793 case OMP_CLAUSE_NOGROUP:
8794 case OMP_CLAUSE_THREADS:
8795 case OMP_CLAUSE_SIMD:
8796 case OMP_CLAUSE_DEFAULTMAP:
8797 case OMP_CLAUSE_BIND:
8798 case OMP_CLAUSE_AUTO:
8799 case OMP_CLAUSE_INDEPENDENT:
8800 case OMP_CLAUSE_SEQ:
8801 case OMP_CLAUSE_IF_PRESENT:
8802 case OMP_CLAUSE_FINALIZE:
8803 case OMP_CLAUSE_NOHOST:
8804 break;
8806 case OMP_CLAUSE_MERGEABLE:
8807 mergeable_seen = true;
8808 break;
8810 case OMP_CLAUSE_TILE:
8811 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
8812 list = TREE_CHAIN (list))
8814 t = TREE_VALUE (list);
8816 if (t == error_mark_node)
8817 remove = true;
8818 else if (!type_dependent_expression_p (t)
8819 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8821 error_at (OMP_CLAUSE_LOCATION (c),
8822 "%<tile%> argument needs integral type");
8823 remove = true;
8825 else
8827 t = mark_rvalue_use (t);
8828 if (!processing_template_decl)
8830 /* Zero is used to indicate '*', we permit you
8831 to get there via an ICE of value zero. */
8832 t = maybe_constant_value (t);
8833 if (!tree_fits_shwi_p (t)
8834 || tree_to_shwi (t) < 0)
8836 error_at (OMP_CLAUSE_LOCATION (c),
8837 "%<tile%> argument needs positive "
8838 "integral constant");
8839 remove = true;
8841 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8845 /* Update list item. */
8846 TREE_VALUE (list) = t;
8848 break;
8850 case OMP_CLAUSE_ORDERED:
8851 ordered_seen = true;
8852 break;
8854 case OMP_CLAUSE_ORDER:
8855 if (order_seen)
8856 remove = true;
8857 else
8858 order_seen = true;
8859 break;
8861 case OMP_CLAUSE_INBRANCH:
8862 case OMP_CLAUSE_NOTINBRANCH:
8863 if (branch_seen)
8865 error_at (OMP_CLAUSE_LOCATION (c),
8866 "%<inbranch%> clause is incompatible with "
8867 "%<notinbranch%>");
8868 remove = true;
8870 branch_seen = true;
8871 break;
8873 case OMP_CLAUSE_INCLUSIVE:
8874 case OMP_CLAUSE_EXCLUSIVE:
8875 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
8876 if (!t)
8877 t = OMP_CLAUSE_DECL (c);
8878 if (t == current_class_ptr)
8880 error_at (OMP_CLAUSE_LOCATION (c),
8881 "%<this%> allowed in OpenMP only in %<declare simd%>"
8882 " clauses");
8883 remove = true;
8884 break;
8886 if (!VAR_P (t)
8887 && TREE_CODE (t) != PARM_DECL
8888 && TREE_CODE (t) != FIELD_DECL)
8890 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
8891 break;
8892 if (DECL_P (t))
8893 error_at (OMP_CLAUSE_LOCATION (c),
8894 "%qD is not a variable in clause %qs", t,
8895 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8896 else
8897 error_at (OMP_CLAUSE_LOCATION (c),
8898 "%qE is not a variable in clause %qs", t,
8899 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8900 remove = true;
8902 break;
8904 default:
8905 gcc_unreachable ();
8908 if (remove)
8910 if (grp_start_p)
8912 /* If we found a clause to remove, we want to remove the whole
8913 expanded group, otherwise gimplify can get confused. */
8914 *grp_start_p = grp_sentinel;
8915 pc = grp_start_p;
8916 grp_start_p = NULL;
8918 else
8919 *pc = OMP_CLAUSE_CHAIN (c);
8921 else
8922 pc = &OMP_CLAUSE_CHAIN (c);
8925 if (reduction_seen < 0 && (ordered_seen || schedule_seen))
8926 reduction_seen = -2;
8928 for (pc = &clauses, c = clauses; c ; c = *pc)
8930 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
8931 bool remove = false;
8932 bool need_complete_type = false;
8933 bool need_default_ctor = false;
8934 bool need_copy_ctor = false;
8935 bool need_copy_assignment = false;
8936 bool need_implicitly_determined = false;
8937 bool need_dtor = false;
8938 tree type, inner_type;
8940 switch (c_kind)
8942 case OMP_CLAUSE_SHARED:
8943 need_implicitly_determined = true;
8944 break;
8945 case OMP_CLAUSE_PRIVATE:
8946 need_complete_type = true;
8947 need_default_ctor = true;
8948 need_dtor = true;
8949 need_implicitly_determined = true;
8950 break;
8951 case OMP_CLAUSE_FIRSTPRIVATE:
8952 need_complete_type = true;
8953 need_copy_ctor = true;
8954 need_dtor = true;
8955 need_implicitly_determined = true;
8956 break;
8957 case OMP_CLAUSE_LASTPRIVATE:
8958 need_complete_type = true;
8959 need_copy_assignment = true;
8960 need_implicitly_determined = true;
8961 break;
8962 case OMP_CLAUSE_REDUCTION:
8963 if (reduction_seen == -2)
8964 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
8965 if (OMP_CLAUSE_REDUCTION_INSCAN (c))
8966 need_copy_assignment = true;
8967 need_implicitly_determined = true;
8968 break;
8969 case OMP_CLAUSE_IN_REDUCTION:
8970 case OMP_CLAUSE_TASK_REDUCTION:
8971 case OMP_CLAUSE_INCLUSIVE:
8972 case OMP_CLAUSE_EXCLUSIVE:
8973 need_implicitly_determined = true;
8974 break;
8975 case OMP_CLAUSE_LINEAR:
8976 if (ort != C_ORT_OMP_DECLARE_SIMD)
8977 need_implicitly_determined = true;
8978 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
8979 && !bitmap_bit_p (&map_head,
8980 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
8982 error_at (OMP_CLAUSE_LOCATION (c),
8983 "%<linear%> clause step is a parameter %qD not "
8984 "specified in %<uniform%> clause",
8985 OMP_CLAUSE_LINEAR_STEP (c));
8986 *pc = OMP_CLAUSE_CHAIN (c);
8987 continue;
8989 break;
8990 case OMP_CLAUSE_COPYPRIVATE:
8991 need_copy_assignment = true;
8992 break;
8993 case OMP_CLAUSE_COPYIN:
8994 need_copy_assignment = true;
8995 break;
8996 case OMP_CLAUSE_SIMDLEN:
8997 if (safelen
8998 && !processing_template_decl
8999 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
9000 OMP_CLAUSE_SIMDLEN_EXPR (c)))
9002 error_at (OMP_CLAUSE_LOCATION (c),
9003 "%<simdlen%> clause value is bigger than "
9004 "%<safelen%> clause value");
9005 OMP_CLAUSE_SIMDLEN_EXPR (c)
9006 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
9008 pc = &OMP_CLAUSE_CHAIN (c);
9009 continue;
9010 case OMP_CLAUSE_SCHEDULE:
9011 if (ordered_seen
9012 && (OMP_CLAUSE_SCHEDULE_KIND (c)
9013 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
9015 error_at (OMP_CLAUSE_LOCATION (c),
9016 "%<nonmonotonic%> schedule modifier specified "
9017 "together with %<ordered%> clause");
9018 OMP_CLAUSE_SCHEDULE_KIND (c)
9019 = (enum omp_clause_schedule_kind)
9020 (OMP_CLAUSE_SCHEDULE_KIND (c)
9021 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
9023 if (reduction_seen == -2)
9024 error_at (OMP_CLAUSE_LOCATION (c),
9025 "%qs clause specified together with %<inscan%> "
9026 "%<reduction%> clause", "schedule");
9027 pc = &OMP_CLAUSE_CHAIN (c);
9028 continue;
9029 case OMP_CLAUSE_NOGROUP:
9030 if (reduction_seen)
9032 error_at (OMP_CLAUSE_LOCATION (c),
9033 "%<nogroup%> clause must not be used together with "
9034 "%<reduction%> clause");
9035 *pc = OMP_CLAUSE_CHAIN (c);
9036 continue;
9038 pc = &OMP_CLAUSE_CHAIN (c);
9039 continue;
9040 case OMP_CLAUSE_ORDERED:
9041 if (reduction_seen == -2)
9042 error_at (OMP_CLAUSE_LOCATION (c),
9043 "%qs clause specified together with %<inscan%> "
9044 "%<reduction%> clause", "ordered");
9045 pc = &OMP_CLAUSE_CHAIN (c);
9046 continue;
9047 case OMP_CLAUSE_ORDER:
9048 if (ordered_seen)
9050 error_at (OMP_CLAUSE_LOCATION (c),
9051 "%<order%> clause must not be used together "
9052 "with %<ordered%>");
9053 *pc = OMP_CLAUSE_CHAIN (c);
9054 continue;
9056 pc = &OMP_CLAUSE_CHAIN (c);
9057 continue;
9058 case OMP_CLAUSE_DETACH:
9059 if (mergeable_seen)
9061 error_at (OMP_CLAUSE_LOCATION (c),
9062 "%<detach%> clause must not be used together with "
9063 "%<mergeable%> clause");
9064 *pc = OMP_CLAUSE_CHAIN (c);
9065 continue;
9067 pc = &OMP_CLAUSE_CHAIN (c);
9068 continue;
9069 case OMP_CLAUSE_MAP:
9070 if (target_in_reduction_seen && !processing_template_decl)
9072 t = OMP_CLAUSE_DECL (c);
9073 while (handled_component_p (t)
9074 || INDIRECT_REF_P (t)
9075 || TREE_CODE (t) == ADDR_EXPR
9076 || TREE_CODE (t) == MEM_REF
9077 || TREE_CODE (t) == NON_LVALUE_EXPR)
9078 t = TREE_OPERAND (t, 0);
9079 if (DECL_P (t)
9080 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
9081 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
9083 pc = &OMP_CLAUSE_CHAIN (c);
9084 continue;
9085 case OMP_CLAUSE_NOWAIT:
9086 if (copyprivate_seen)
9088 error_at (OMP_CLAUSE_LOCATION (c),
9089 "%<nowait%> clause must not be used together "
9090 "with %<copyprivate%>");
9091 *pc = OMP_CLAUSE_CHAIN (c);
9092 continue;
9094 /* FALLTHRU */
9095 default:
9096 pc = &OMP_CLAUSE_CHAIN (c);
9097 continue;
9100 t = OMP_CLAUSE_DECL (c);
9101 switch (c_kind)
9103 case OMP_CLAUSE_LASTPRIVATE:
9104 if (DECL_P (t)
9105 && !bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
9107 need_default_ctor = true;
9108 need_dtor = true;
9110 break;
9112 case OMP_CLAUSE_REDUCTION:
9113 case OMP_CLAUSE_IN_REDUCTION:
9114 case OMP_CLAUSE_TASK_REDUCTION:
9115 if (allocate_seen)
9117 if (TREE_CODE (t) == MEM_REF)
9119 t = TREE_OPERAND (t, 0);
9120 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
9121 t = TREE_OPERAND (t, 0);
9122 if (TREE_CODE (t) == ADDR_EXPR
9123 || INDIRECT_REF_P (t))
9124 t = TREE_OPERAND (t, 0);
9125 if (DECL_P (t))
9126 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9128 else if (TREE_CODE (t) == TREE_LIST)
9130 while (TREE_CODE (t) == TREE_LIST)
9131 t = TREE_CHAIN (t);
9132 if (DECL_P (t))
9133 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9134 t = OMP_CLAUSE_DECL (c);
9136 else if (DECL_P (t))
9137 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9138 t = OMP_CLAUSE_DECL (c);
9140 if (processing_template_decl
9141 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
9142 break;
9143 if (finish_omp_reduction_clause (c, &need_default_ctor,
9144 &need_dtor))
9145 remove = true;
9146 else
9147 t = OMP_CLAUSE_DECL (c);
9148 break;
9150 case OMP_CLAUSE_COPYIN:
9151 if (processing_template_decl
9152 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
9153 break;
9154 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
9156 error_at (OMP_CLAUSE_LOCATION (c),
9157 "%qE must be %<threadprivate%> for %<copyin%>", t);
9158 remove = true;
9160 break;
9162 default:
9163 break;
9166 if (processing_template_decl
9167 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
9169 pc = &OMP_CLAUSE_CHAIN (c);
9170 continue;
9173 if (need_complete_type || need_copy_assignment)
9175 t = require_complete_type (t);
9176 if (t == error_mark_node)
9177 remove = true;
9178 else if (!processing_template_decl
9179 && TYPE_REF_P (TREE_TYPE (t))
9180 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
9181 remove = true;
9183 if (need_implicitly_determined)
9185 const char *share_name = NULL;
9187 if (allocate_seen
9188 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
9189 && DECL_P (t))
9190 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9192 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
9193 share_name = "threadprivate";
9194 else switch (cxx_omp_predetermined_sharing_1 (t))
9196 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
9197 break;
9198 case OMP_CLAUSE_DEFAULT_SHARED:
9199 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9200 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
9201 && c_omp_predefined_variable (t))
9202 /* The __func__ variable and similar function-local predefined
9203 variables may be listed in a shared or firstprivate
9204 clause. */
9205 break;
9206 if (VAR_P (t)
9207 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
9208 && TREE_STATIC (t)
9209 && cxx_omp_const_qual_no_mutable (t))
9211 tree ctx = CP_DECL_CONTEXT (t);
9212 /* const qualified static data members without mutable
9213 member may be specified in firstprivate clause. */
9214 if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
9215 break;
9217 share_name = "shared";
9218 break;
9219 case OMP_CLAUSE_DEFAULT_PRIVATE:
9220 share_name = "private";
9221 break;
9222 default:
9223 gcc_unreachable ();
9225 if (share_name)
9227 error_at (OMP_CLAUSE_LOCATION (c),
9228 "%qE is predetermined %qs for %qs",
9229 omp_clause_printable_decl (t), share_name,
9230 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
9231 remove = true;
9233 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
9234 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE
9235 && cxx_omp_const_qual_no_mutable (t))
9237 error_at (OMP_CLAUSE_LOCATION (c),
9238 "%<const%> qualified %qE without %<mutable%> member "
9239 "may appear only in %<shared%> or %<firstprivate%> "
9240 "clauses", omp_clause_printable_decl (t));
9241 remove = true;
9245 if (detach_seen
9246 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9247 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
9248 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
9249 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
9250 && OMP_CLAUSE_DECL (c) == OMP_CLAUSE_DECL (detach_seen))
9252 error_at (OMP_CLAUSE_LOCATION (c),
9253 "the event handle of a %<detach%> clause "
9254 "should not be in a data-sharing clause");
9255 remove = true;
9258 /* We're interested in the base element, not arrays. */
9259 inner_type = type = TREE_TYPE (t);
9260 if ((need_complete_type
9261 || need_copy_assignment
9262 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
9263 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
9264 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
9265 && TYPE_REF_P (inner_type))
9266 inner_type = TREE_TYPE (inner_type);
9267 while (TREE_CODE (inner_type) == ARRAY_TYPE)
9268 inner_type = TREE_TYPE (inner_type);
9270 /* Check for special function availability by building a call to one.
9271 Save the results, because later we won't be in the right context
9272 for making these queries. */
9273 if (CLASS_TYPE_P (inner_type)
9274 && COMPLETE_TYPE_P (inner_type)
9275 && (need_default_ctor || need_copy_ctor
9276 || need_copy_assignment || need_dtor)
9277 && !type_dependent_expression_p (t)
9278 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
9279 need_copy_ctor, need_copy_assignment,
9280 need_dtor))
9281 remove = true;
9283 if (!remove
9284 && c_kind == OMP_CLAUSE_SHARED
9285 && processing_template_decl)
9287 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
9288 if (t)
9289 OMP_CLAUSE_DECL (c) = t;
9292 if (remove)
9293 *pc = OMP_CLAUSE_CHAIN (c);
9294 else
9295 pc = &OMP_CLAUSE_CHAIN (c);
9298 if (allocate_seen)
9299 for (pc = &clauses, c = clauses; c ; c = *pc)
9301 bool remove = false;
9302 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
9303 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
9304 && DECL_P (OMP_CLAUSE_DECL (c))
9305 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
9307 error_at (OMP_CLAUSE_LOCATION (c),
9308 "%qD specified in %<allocate%> clause but not in "
9309 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
9310 remove = true;
9312 if (remove)
9313 *pc = OMP_CLAUSE_CHAIN (c);
9314 else
9315 pc = &OMP_CLAUSE_CHAIN (c);
9318 bitmap_obstack_release (NULL);
9319 return clauses;
9322 /* Start processing OpenMP clauses that can include any
9323 privatization clauses for non-static data members. */
9325 tree
9326 push_omp_privatization_clauses (bool ignore_next)
9328 if (omp_private_member_ignore_next)
9330 omp_private_member_ignore_next = ignore_next;
9331 return NULL_TREE;
9333 omp_private_member_ignore_next = ignore_next;
9334 if (omp_private_member_map)
9335 omp_private_member_vec.safe_push (error_mark_node);
9336 return push_stmt_list ();
9339 /* Revert remapping of any non-static data members since
9340 the last push_omp_privatization_clauses () call. */
9342 void
9343 pop_omp_privatization_clauses (tree stmt)
9345 if (stmt == NULL_TREE)
9346 return;
9347 stmt = pop_stmt_list (stmt);
9348 if (omp_private_member_map)
9350 while (!omp_private_member_vec.is_empty ())
9352 tree t = omp_private_member_vec.pop ();
9353 if (t == error_mark_node)
9355 add_stmt (stmt);
9356 return;
9358 bool no_decl_expr = t == integer_zero_node;
9359 if (no_decl_expr)
9360 t = omp_private_member_vec.pop ();
9361 tree *v = omp_private_member_map->get (t);
9362 gcc_assert (v);
9363 if (!no_decl_expr)
9364 add_decl_expr (*v);
9365 omp_private_member_map->remove (t);
9367 delete omp_private_member_map;
9368 omp_private_member_map = NULL;
9370 add_stmt (stmt);
9373 /* Remember OpenMP privatization clauses mapping and clear it.
9374 Used for lambdas. */
9376 void
9377 save_omp_privatization_clauses (vec<tree> &save)
9379 save = vNULL;
9380 if (omp_private_member_ignore_next)
9381 save.safe_push (integer_one_node);
9382 omp_private_member_ignore_next = false;
9383 if (!omp_private_member_map)
9384 return;
9386 while (!omp_private_member_vec.is_empty ())
9388 tree t = omp_private_member_vec.pop ();
9389 if (t == error_mark_node)
9391 save.safe_push (t);
9392 continue;
9394 tree n = t;
9395 if (t == integer_zero_node)
9396 t = omp_private_member_vec.pop ();
9397 tree *v = omp_private_member_map->get (t);
9398 gcc_assert (v);
9399 save.safe_push (*v);
9400 save.safe_push (t);
9401 if (n != t)
9402 save.safe_push (n);
9404 delete omp_private_member_map;
9405 omp_private_member_map = NULL;
9408 /* Restore OpenMP privatization clauses mapping saved by the
9409 above function. */
9411 void
9412 restore_omp_privatization_clauses (vec<tree> &save)
9414 gcc_assert (omp_private_member_vec.is_empty ());
9415 omp_private_member_ignore_next = false;
9416 if (save.is_empty ())
9417 return;
9418 if (save.length () == 1 && save[0] == integer_one_node)
9420 omp_private_member_ignore_next = true;
9421 save.release ();
9422 return;
9425 omp_private_member_map = new hash_map <tree, tree>;
9426 while (!save.is_empty ())
9428 tree t = save.pop ();
9429 tree n = t;
9430 if (t != error_mark_node)
9432 if (t == integer_one_node)
9434 omp_private_member_ignore_next = true;
9435 gcc_assert (save.is_empty ());
9436 break;
9438 if (t == integer_zero_node)
9439 t = save.pop ();
9440 tree &v = omp_private_member_map->get_or_insert (t);
9441 v = save.pop ();
9443 omp_private_member_vec.safe_push (t);
9444 if (n != t)
9445 omp_private_member_vec.safe_push (n);
9447 save.release ();
9450 /* For all variables in the tree_list VARS, mark them as thread local. */
9452 void
9453 finish_omp_threadprivate (tree vars)
9455 tree t;
9457 /* Mark every variable in VARS to be assigned thread local storage. */
9458 for (t = vars; t; t = TREE_CHAIN (t))
9460 tree v = TREE_PURPOSE (t);
9462 if (error_operand_p (v))
9464 else if (!VAR_P (v))
9465 error ("%<threadprivate%> %qD is not file, namespace "
9466 "or block scope variable", v);
9467 /* If V had already been marked threadprivate, it doesn't matter
9468 whether it had been used prior to this point. */
9469 else if (TREE_USED (v)
9470 && (DECL_LANG_SPECIFIC (v) == NULL
9471 || !CP_DECL_THREADPRIVATE_P (v)))
9472 error ("%qE declared %<threadprivate%> after first use", v);
9473 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
9474 error ("automatic variable %qE cannot be %<threadprivate%>", v);
9475 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
9476 error ("%<threadprivate%> %qE has incomplete type", v);
9477 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
9478 && CP_DECL_CONTEXT (v) != current_class_type)
9479 error ("%<threadprivate%> %qE directive not "
9480 "in %qT definition", v, CP_DECL_CONTEXT (v));
9481 else
9483 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
9484 if (DECL_LANG_SPECIFIC (v) == NULL)
9485 retrofit_lang_decl (v);
9487 if (! CP_DECL_THREAD_LOCAL_P (v))
9489 CP_DECL_THREAD_LOCAL_P (v) = true;
9490 set_decl_tls_model (v, decl_default_tls_model (v));
9491 /* If rtl has been already set for this var, call
9492 make_decl_rtl once again, so that encode_section_info
9493 has a chance to look at the new decl flags. */
9494 if (DECL_RTL_SET_P (v))
9495 make_decl_rtl (v);
9497 CP_DECL_THREADPRIVATE_P (v) = 1;
9502 /* Build an OpenMP structured block. */
9504 tree
9505 begin_omp_structured_block (void)
9507 return do_pushlevel (sk_omp);
9510 tree
9511 finish_omp_structured_block (tree block)
9513 return do_poplevel (block);
9516 /* Similarly, except force the retention of the BLOCK. */
9518 tree
9519 begin_omp_parallel (void)
9521 keep_next_level (true);
9522 return begin_omp_structured_block ();
9525 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
9526 statement. */
9528 tree
9529 finish_oacc_data (tree clauses, tree block)
9531 tree stmt;
9533 block = finish_omp_structured_block (block);
9535 stmt = make_node (OACC_DATA);
9536 TREE_TYPE (stmt) = void_type_node;
9537 OACC_DATA_CLAUSES (stmt) = clauses;
9538 OACC_DATA_BODY (stmt) = block;
9540 return add_stmt (stmt);
9543 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
9544 statement. */
9546 tree
9547 finish_oacc_host_data (tree clauses, tree block)
9549 tree stmt;
9551 block = finish_omp_structured_block (block);
9553 stmt = make_node (OACC_HOST_DATA);
9554 TREE_TYPE (stmt) = void_type_node;
9555 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
9556 OACC_HOST_DATA_BODY (stmt) = block;
9558 return add_stmt (stmt);
9561 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
9562 statement. */
9564 tree
9565 finish_omp_construct (enum tree_code code, tree body, tree clauses)
9567 body = finish_omp_structured_block (body);
9569 tree stmt = make_node (code);
9570 TREE_TYPE (stmt) = void_type_node;
9571 OMP_BODY (stmt) = body;
9572 OMP_CLAUSES (stmt) = clauses;
9574 return add_stmt (stmt);
9577 /* Used to walk OpenMP target directive body. */
9579 struct omp_target_walk_data
9581 /* Holds the 'this' expression found in current function. */
9582 tree current_object;
9584 /* True if the 'this' expression was accessed in the target body. */
9585 bool this_expr_accessed;
9587 /* For non-static functions, record which pointer-typed members were
9588 accessed, and the whole expression. */
9589 hash_map<tree, tree> ptr_members_accessed;
9591 /* Record which lambda objects were accessed in target body. */
9592 hash_set<tree> lambda_objects_accessed;
9594 /* For lambda functions, the __closure object expression of the current
9595 function, and the set of captured variables accessed in target body. */
9596 tree current_closure;
9597 hash_set<tree> closure_vars_accessed;
9599 /* Local variables declared inside a BIND_EXPR, used to filter out such
9600 variables when recording lambda_objects_accessed. */
9601 hash_set<tree> local_decls;
9604 /* Helper function of finish_omp_target_clauses, called via
9605 cp_walk_tree_without_duplicates. Traverse body of OpenMP target
9606 directive *TP, and fill out omp_target_walk_data passed in *PTR. */
9608 static tree
9609 finish_omp_target_clauses_r (tree *tp, int *walk_subtrees, void *ptr)
9611 tree t = *tp;
9612 struct omp_target_walk_data *data = (struct omp_target_walk_data *) ptr;
9613 tree current_object = data->current_object;
9614 tree current_closure = data->current_closure;
9616 /* References inside of these expression codes shouldn't incur any
9617 form of mapping, so return early. */
9618 if (TREE_CODE (t) == SIZEOF_EXPR
9619 || TREE_CODE (t) == ALIGNOF_EXPR)
9621 *walk_subtrees = 0;
9622 return NULL_TREE;
9625 if (TREE_CODE (t) == OMP_CLAUSE)
9626 return NULL_TREE;
9628 if (current_object)
9630 tree this_expr = TREE_OPERAND (current_object, 0);
9632 if (operand_equal_p (t, this_expr))
9634 data->this_expr_accessed = true;
9635 *walk_subtrees = 0;
9636 return NULL_TREE;
9639 if (TREE_CODE (t) == COMPONENT_REF
9640 && POINTER_TYPE_P (TREE_TYPE (t))
9641 && operand_equal_p (TREE_OPERAND (t, 0), current_object)
9642 && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL)
9644 data->this_expr_accessed = true;
9645 tree fld = TREE_OPERAND (t, 1);
9646 if (data->ptr_members_accessed.get (fld) == NULL)
9648 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
9649 t = convert_from_reference (t);
9650 data->ptr_members_accessed.put (fld, t);
9652 *walk_subtrees = 0;
9653 return NULL_TREE;
9657 /* When the current_function_decl is a lambda function, the closure object
9658 argument's type seems to not yet have fields layed out, so a recording
9659 of DECL_VALUE_EXPRs during the target body walk seems the only way to
9660 find them. */
9661 if (current_closure
9662 && (VAR_P (t)
9663 || TREE_CODE (t) == PARM_DECL
9664 || TREE_CODE (t) == RESULT_DECL)
9665 && DECL_HAS_VALUE_EXPR_P (t)
9666 && TREE_CODE (DECL_VALUE_EXPR (t)) == COMPONENT_REF
9667 && operand_equal_p (current_closure,
9668 TREE_OPERAND (DECL_VALUE_EXPR (t), 0)))
9670 if (!data->closure_vars_accessed.contains (t))
9671 data->closure_vars_accessed.add (t);
9672 *walk_subtrees = 0;
9673 return NULL_TREE;
9676 if (TREE_CODE (t) == BIND_EXPR)
9678 tree block = BIND_EXPR_BLOCK (t);
9679 for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
9680 if (!data->local_decls.contains (var))
9681 data->local_decls.add (var);
9682 return NULL_TREE;
9685 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
9687 tree lt = TREE_TYPE (t);
9688 gcc_assert (CLASS_TYPE_P (lt));
9690 if (!data->lambda_objects_accessed.contains (t)
9691 /* Do not prepare to create target maps for locally declared
9692 lambdas or anonymous ones. */
9693 && !data->local_decls.contains (t)
9694 && TREE_CODE (t) != TARGET_EXPR)
9695 data->lambda_objects_accessed.add (t);
9696 *walk_subtrees = 0;
9697 return NULL_TREE;
9700 return NULL_TREE;
9703 /* Helper function for finish_omp_target, and also from tsubst_expr.
9704 Create additional clauses for mapping of non-static members, lambda objects,
9705 etc. */
9707 void
9708 finish_omp_target_clauses (location_t loc, tree body, tree *clauses_ptr)
9710 omp_target_walk_data data;
9711 data.this_expr_accessed = false;
9712 data.current_object = NULL_TREE;
9714 if (DECL_NONSTATIC_MEMBER_P (current_function_decl) && current_class_ptr)
9715 if (tree ct = current_nonlambda_class_type ())
9717 tree object = maybe_dummy_object (ct, NULL);
9718 object = maybe_resolve_dummy (object, true);
9719 data.current_object = object;
9722 if (DECL_LAMBDA_FUNCTION_P (current_function_decl))
9724 tree closure = DECL_ARGUMENTS (current_function_decl);
9725 data.current_closure = build_indirect_ref (loc, closure, RO_UNARY_STAR);
9727 else
9728 data.current_closure = NULL_TREE;
9730 cp_walk_tree_without_duplicates (&body, finish_omp_target_clauses_r, &data);
9732 auto_vec<tree, 16> new_clauses;
9734 tree omp_target_this_expr = NULL_TREE;
9735 tree *explicit_this_deref_map = NULL;
9736 if (data.this_expr_accessed)
9738 omp_target_this_expr = TREE_OPERAND (data.current_object, 0);
9740 /* See if explicit user-specified map(this[:]) clause already exists.
9741 If not, we create an implicit map(tofrom:this[:1]) clause. */
9742 for (tree *cp = clauses_ptr; *cp; cp = &OMP_CLAUSE_CHAIN (*cp))
9743 if (OMP_CLAUSE_CODE (*cp) == OMP_CLAUSE_MAP
9744 && (TREE_CODE (OMP_CLAUSE_DECL (*cp)) == INDIRECT_REF
9745 || TREE_CODE (OMP_CLAUSE_DECL (*cp)) == MEM_REF)
9746 && operand_equal_p (TREE_OPERAND (OMP_CLAUSE_DECL (*cp), 0),
9747 omp_target_this_expr))
9749 explicit_this_deref_map = cp;
9750 break;
9754 if (DECL_LAMBDA_FUNCTION_P (current_function_decl)
9755 && (data.this_expr_accessed
9756 || !data.closure_vars_accessed.is_empty ()))
9758 /* For lambda functions, we need to first create a copy of the
9759 __closure object. */
9760 tree closure = DECL_ARGUMENTS (current_function_decl);
9761 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9762 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
9763 OMP_CLAUSE_DECL (c)
9764 = build_indirect_ref (loc, closure, RO_UNARY_STAR);
9765 OMP_CLAUSE_SIZE (c)
9766 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (closure)));
9767 new_clauses.safe_push (c);
9769 tree closure_obj = OMP_CLAUSE_DECL (c);
9770 tree closure_type = TREE_TYPE (closure_obj);
9772 gcc_assert (LAMBDA_TYPE_P (closure_type)
9773 && CLASS_TYPE_P (closure_type));
9775 tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
9776 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
9777 OMP_CLAUSE_DECL (c2) = closure;
9778 OMP_CLAUSE_SIZE (c2) = size_zero_node;
9779 new_clauses.safe_push (c2);
9782 if (data.this_expr_accessed)
9784 /* If the this-expr was accessed, create a map(*this) clause. */
9785 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
9786 if (explicit_this_deref_map)
9788 tree this_map = *explicit_this_deref_map;
9789 tree nc = OMP_CLAUSE_CHAIN (this_map);
9790 gcc_assert (nc != NULL_TREE
9791 && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
9792 && (OMP_CLAUSE_MAP_KIND (nc)
9793 == GOMP_MAP_FIRSTPRIVATE_POINTER));
9794 kind = OMP_CLAUSE_MAP_KIND (this_map);
9795 /* Remove the original 'map(*this) map(firstprivate_ptr:this)'
9796 two-map sequence away from the chain. */
9797 *explicit_this_deref_map = OMP_CLAUSE_CHAIN (nc);
9799 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9800 OMP_CLAUSE_SET_MAP_KIND (c, kind);
9801 OMP_CLAUSE_DECL (c)
9802 = build_indirect_ref (loc, omp_target_this_expr, RO_UNARY_STAR);
9803 OMP_CLAUSE_SIZE (c)
9804 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (omp_target_this_expr)));
9805 new_clauses.safe_push (c);
9807 /* If we're in a lambda function, the this-pointer will actually be
9808 '__closure->this', a mapped member of __closure, hence always_pointer.
9809 Otherwise it's a firstprivate pointer. */
9810 enum gomp_map_kind ptr_kind
9811 = (DECL_LAMBDA_FUNCTION_P (current_function_decl)
9812 ? GOMP_MAP_ALWAYS_POINTER
9813 : GOMP_MAP_FIRSTPRIVATE_POINTER);
9814 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9815 OMP_CLAUSE_SET_MAP_KIND (c, ptr_kind);
9816 OMP_CLAUSE_DECL (c) = omp_target_this_expr;
9817 OMP_CLAUSE_SIZE (c) = size_zero_node;
9818 new_clauses.safe_push (c);
9821 if (DECL_LAMBDA_FUNCTION_P (current_function_decl))
9823 if (omp_target_this_expr)
9825 STRIP_NOPS (omp_target_this_expr);
9826 gcc_assert (DECL_HAS_VALUE_EXPR_P (omp_target_this_expr));
9827 omp_target_this_expr = DECL_VALUE_EXPR (omp_target_this_expr);
9830 for (hash_set<tree>::iterator i = data.closure_vars_accessed.begin ();
9831 i != data.closure_vars_accessed.end (); ++i)
9833 tree orig_decl = *i;
9834 tree closure_expr = DECL_VALUE_EXPR (orig_decl);
9836 if (TREE_CODE (TREE_TYPE (orig_decl)) == POINTER_TYPE
9837 || TREE_CODE (TREE_TYPE (orig_decl)) == REFERENCE_TYPE)
9839 /* this-pointer is processed above, outside this loop. */
9840 if (omp_target_this_expr
9841 && operand_equal_p (closure_expr, omp_target_this_expr))
9842 continue;
9844 bool ptr_p = TREE_CODE (TREE_TYPE (orig_decl)) == POINTER_TYPE;
9845 enum gomp_map_kind kind, ptr_kind, nc_kind;
9846 tree size;
9848 if (ptr_p)
9850 /* For pointers, default mapped as zero-length array
9851 section. */
9852 kind = GOMP_MAP_ALLOC;
9853 nc_kind = GOMP_MAP_FIRSTPRIVATE_POINTER;
9854 ptr_kind = GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION;
9855 size = size_zero_node;
9857 else
9859 /* For references, default mapped as appearing on map
9860 clause. */
9861 kind = GOMP_MAP_TOFROM;
9862 nc_kind = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
9863 ptr_kind = GOMP_MAP_ALWAYS_POINTER;
9864 size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (closure_expr)));
9867 for (tree *p = clauses_ptr; *p; p = &OMP_CLAUSE_CHAIN (*p))
9868 if (OMP_CLAUSE_CODE (*p) == OMP_CLAUSE_MAP
9869 && (TREE_CODE (OMP_CLAUSE_DECL (*p)) == INDIRECT_REF
9870 || TREE_CODE (OMP_CLAUSE_DECL (*p)) == MEM_REF)
9871 && operand_equal_p (TREE_OPERAND (OMP_CLAUSE_DECL (*p), 0),
9872 orig_decl))
9874 /* If this was already specified by user as a map,
9875 save the user specified map kind, delete the
9876 "map(*ptr/ref), map(firstprivate ptr/ref)" sequence,
9877 and insert our own sequence:
9878 "map(*__closure->ptr/ref), map(<ptr_kind>:__closure->ref"
9880 tree nc = OMP_CLAUSE_CHAIN (*p);
9881 gcc_assert (nc != NULL_TREE
9882 && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
9883 && OMP_CLAUSE_MAP_KIND (nc) == nc_kind);
9884 /* Update with user specified kind and size. */
9885 kind = OMP_CLAUSE_MAP_KIND (*p);
9886 size = OMP_CLAUSE_SIZE (*p);
9887 *p = OMP_CLAUSE_CHAIN (nc);
9888 break;
9891 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9892 OMP_CLAUSE_SET_MAP_KIND (c, kind);
9893 OMP_CLAUSE_DECL (c)
9894 = build_indirect_ref (loc, closure_expr, RO_UNARY_STAR);
9895 OMP_CLAUSE_SIZE (c) = size;
9896 if (ptr_p)
9897 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
9898 new_clauses.safe_push (c);
9900 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9901 OMP_CLAUSE_SET_MAP_KIND (c, ptr_kind);
9902 OMP_CLAUSE_DECL (c) = closure_expr;
9903 OMP_CLAUSE_SIZE (c) = size_zero_node;
9904 new_clauses.safe_push (c);
9909 if (!data.ptr_members_accessed.is_empty ())
9910 for (hash_map<tree, tree>::iterator i = data.ptr_members_accessed.begin ();
9911 i != data.ptr_members_accessed.end (); ++i)
9913 /* For each referenced member that is of pointer or reference-to-pointer
9914 type, create the equivalent of map(alloc:this->ptr[:0]). */
9915 tree field_decl = (*i).first;
9916 tree ptr_member = (*i).second;
9918 for (tree c = *clauses_ptr; c; c = OMP_CLAUSE_CHAIN (c))
9920 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
9921 continue;
9922 /* If map(this->ptr[:N]) already exists, avoid creating another
9923 such map. */
9924 tree decl = OMP_CLAUSE_DECL (c);
9925 if ((TREE_CODE (decl) == INDIRECT_REF
9926 || TREE_CODE (decl) == MEM_REF)
9927 && operand_equal_p (TREE_OPERAND (decl, 0), ptr_member))
9928 goto next_ptr_member;
9931 if (!cxx_mark_addressable (ptr_member))
9932 gcc_unreachable ();
9934 if (TREE_CODE (TREE_TYPE (field_decl)) == REFERENCE_TYPE)
9936 /* For reference to pointers, we need to map the referenced
9937 pointer first for things to be correct. */
9938 tree ptr_member_type = TREE_TYPE (ptr_member);
9940 /* Map pointer target as zero-length array section. */
9941 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9942 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
9943 OMP_CLAUSE_DECL (c)
9944 = build1 (INDIRECT_REF, TREE_TYPE (ptr_member_type), ptr_member);
9945 OMP_CLAUSE_SIZE (c) = size_zero_node;
9946 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
9948 /* Map pointer to zero-length array section. */
9949 tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
9950 OMP_CLAUSE_SET_MAP_KIND
9951 (c2, GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION);
9952 OMP_CLAUSE_DECL (c2) = ptr_member;
9953 OMP_CLAUSE_SIZE (c2) = size_zero_node;
9955 /* Attach reference-to-pointer field to pointer. */
9956 tree c3 = build_omp_clause (loc, OMP_CLAUSE_MAP);
9957 OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_ATTACH);
9958 OMP_CLAUSE_DECL (c3) = TREE_OPERAND (ptr_member, 0);
9959 OMP_CLAUSE_SIZE (c3) = size_zero_node;
9961 new_clauses.safe_push (c);
9962 new_clauses.safe_push (c2);
9963 new_clauses.safe_push (c3);
9965 else if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
9967 /* Map pointer target as zero-length array section. */
9968 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9969 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
9970 OMP_CLAUSE_DECL (c) = build_indirect_ref (loc, ptr_member,
9971 RO_UNARY_STAR);
9972 OMP_CLAUSE_SIZE (c) = size_zero_node;
9973 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
9975 /* Attach zero-length array section to pointer. */
9976 tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
9977 OMP_CLAUSE_SET_MAP_KIND
9978 (c2, GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION);
9979 OMP_CLAUSE_DECL (c2) = ptr_member;
9980 OMP_CLAUSE_SIZE (c2) = size_zero_node;
9982 new_clauses.safe_push (c);
9983 new_clauses.safe_push (c2);
9985 else
9986 gcc_unreachable ();
9988 next_ptr_member:
9992 for (hash_set<tree>::iterator i = data.lambda_objects_accessed.begin ();
9993 i != data.lambda_objects_accessed.end (); ++i)
9995 tree lobj = *i;
9996 if (TREE_CODE (lobj) == TARGET_EXPR)
9997 lobj = TREE_OPERAND (lobj, 0);
9999 tree lt = TREE_TYPE (lobj);
10000 gcc_assert (LAMBDA_TYPE_P (lt) && CLASS_TYPE_P (lt));
10002 tree lc = build_omp_clause (loc, OMP_CLAUSE_MAP);
10003 OMP_CLAUSE_SET_MAP_KIND (lc, GOMP_MAP_TO);
10004 OMP_CLAUSE_DECL (lc) = lobj;
10005 OMP_CLAUSE_SIZE (lc) = TYPE_SIZE_UNIT (lt);
10006 new_clauses.safe_push (lc);
10008 for (tree fld = TYPE_FIELDS (lt); fld; fld = DECL_CHAIN (fld))
10010 if (TREE_CODE (TREE_TYPE (fld)) == POINTER_TYPE)
10012 tree exp = build3 (COMPONENT_REF, TREE_TYPE (fld),
10013 lobj, fld, NULL_TREE);
10014 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10015 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
10016 OMP_CLAUSE_DECL (c)
10017 = build_indirect_ref (loc, exp, RO_UNARY_STAR);
10018 OMP_CLAUSE_SIZE (c) = size_zero_node;
10019 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
10020 new_clauses.safe_push (c);
10022 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10023 OMP_CLAUSE_SET_MAP_KIND
10024 (c, GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION);
10025 OMP_CLAUSE_DECL (c) = exp;
10026 OMP_CLAUSE_SIZE (c) = size_zero_node;
10027 new_clauses.safe_push (c);
10029 else if (TREE_CODE (TREE_TYPE (fld)) == REFERENCE_TYPE)
10031 tree exp = build3 (COMPONENT_REF, TREE_TYPE (fld),
10032 lobj, fld, NULL_TREE);
10033 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10034 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
10035 OMP_CLAUSE_DECL (c)
10036 = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
10037 OMP_CLAUSE_SIZE (c)
10038 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (exp)));
10039 new_clauses.safe_push (c);
10041 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10042 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALWAYS_POINTER);
10043 OMP_CLAUSE_DECL (c) = exp;
10044 OMP_CLAUSE_SIZE (c) = size_zero_node;
10045 new_clauses.safe_push (c);
10050 tree c = *clauses_ptr;
10051 for (int i = new_clauses.length () - 1; i >= 0; i--)
10053 OMP_CLAUSE_CHAIN (new_clauses[i]) = c;
10054 c = new_clauses[i];
10056 *clauses_ptr = c;
10059 /* Called from cp_parser_omp_target. Create additional implicit clauses for
10060 OpenMP target directives, and do sanity checks. */
10062 tree
10063 finish_omp_target (location_t loc, tree clauses, tree body, bool combined_p)
10065 if (!processing_template_decl)
10066 finish_omp_target_clauses (loc, body, &clauses);
10068 tree stmt = make_node (OMP_TARGET);
10069 TREE_TYPE (stmt) = void_type_node;
10070 OMP_TARGET_CLAUSES (stmt) = clauses;
10071 OMP_TARGET_BODY (stmt) = body;
10072 OMP_TARGET_COMBINED (stmt) = combined_p;
10073 SET_EXPR_LOCATION (stmt, loc);
10075 tree c = clauses;
10076 while (c)
10078 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
10079 switch (OMP_CLAUSE_MAP_KIND (c))
10081 case GOMP_MAP_TO:
10082 case GOMP_MAP_ALWAYS_TO:
10083 case GOMP_MAP_PRESENT_TO:
10084 case GOMP_MAP_ALWAYS_PRESENT_TO:
10085 case GOMP_MAP_FROM:
10086 case GOMP_MAP_ALWAYS_FROM:
10087 case GOMP_MAP_PRESENT_FROM:
10088 case GOMP_MAP_ALWAYS_PRESENT_FROM:
10089 case GOMP_MAP_TOFROM:
10090 case GOMP_MAP_ALWAYS_TOFROM:
10091 case GOMP_MAP_PRESENT_TOFROM:
10092 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
10093 case GOMP_MAP_ALLOC:
10094 case GOMP_MAP_PRESENT_ALLOC:
10095 case GOMP_MAP_FIRSTPRIVATE_POINTER:
10096 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
10097 case GOMP_MAP_ALWAYS_POINTER:
10098 case GOMP_MAP_ATTACH_DETACH:
10099 case GOMP_MAP_ATTACH:
10100 case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10101 case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
10102 break;
10103 default:
10104 error_at (OMP_CLAUSE_LOCATION (c),
10105 "%<#pragma omp target%> with map-type other "
10106 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
10107 "on %<map%> clause");
10108 break;
10110 c = OMP_CLAUSE_CHAIN (c);
10112 return add_stmt (stmt);
10115 tree
10116 finish_omp_parallel (tree clauses, tree body)
10118 tree stmt;
10120 body = finish_omp_structured_block (body);
10122 stmt = make_node (OMP_PARALLEL);
10123 TREE_TYPE (stmt) = void_type_node;
10124 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10125 OMP_PARALLEL_BODY (stmt) = body;
10127 return add_stmt (stmt);
10130 tree
10131 begin_omp_task (void)
10133 keep_next_level (true);
10134 return begin_omp_structured_block ();
10137 tree
10138 finish_omp_task (tree clauses, tree body)
10140 tree stmt;
10142 body = finish_omp_structured_block (body);
10144 stmt = make_node (OMP_TASK);
10145 TREE_TYPE (stmt) = void_type_node;
10146 OMP_TASK_CLAUSES (stmt) = clauses;
10147 OMP_TASK_BODY (stmt) = body;
10149 return add_stmt (stmt);
10152 /* Helper function for finish_omp_for. Convert Ith random access iterator
10153 into integral iterator. Return FALSE if successful. */
10155 static bool
10156 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
10157 tree declv, tree orig_declv, tree initv,
10158 tree condv, tree incrv, tree *body,
10159 tree *pre_body, tree &clauses,
10160 int collapse, int ordered)
10162 tree diff, iter_init, iter_incr = NULL, last;
10163 tree incr_var = NULL, orig_pre_body, orig_body, c;
10164 tree decl = TREE_VEC_ELT (declv, i);
10165 tree init = TREE_VEC_ELT (initv, i);
10166 tree cond = TREE_VEC_ELT (condv, i);
10167 tree incr = TREE_VEC_ELT (incrv, i);
10168 tree iter = decl;
10169 location_t elocus = locus;
10171 if (init && EXPR_HAS_LOCATION (init))
10172 elocus = EXPR_LOCATION (init);
10174 switch (TREE_CODE (cond))
10176 case GT_EXPR:
10177 case GE_EXPR:
10178 case LT_EXPR:
10179 case LE_EXPR:
10180 case NE_EXPR:
10181 if (TREE_OPERAND (cond, 1) == iter)
10182 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
10183 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
10184 if (TREE_OPERAND (cond, 0) != iter)
10185 cond = error_mark_node;
10186 else
10188 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
10189 TREE_CODE (cond),
10190 iter, ERROR_MARK,
10191 TREE_OPERAND (cond, 1), ERROR_MARK,
10192 NULL_TREE, NULL, tf_warning_or_error);
10193 if (error_operand_p (tem))
10194 return true;
10196 break;
10197 default:
10198 cond = error_mark_node;
10199 break;
10201 if (cond == error_mark_node)
10203 error_at (elocus, "invalid controlling predicate");
10204 return true;
10206 diff = build_x_binary_op (elocus, MINUS_EXPR,
10207 TREE_OPERAND (cond, 1), ERROR_MARK,
10208 iter, ERROR_MARK,
10209 NULL_TREE, NULL, tf_warning_or_error);
10210 diff = cp_fully_fold (diff);
10211 if (error_operand_p (diff))
10212 return true;
10213 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
10215 error_at (elocus, "difference between %qE and %qD does not have integer type",
10216 TREE_OPERAND (cond, 1), iter);
10217 return true;
10219 if (!c_omp_check_loop_iv_exprs (locus, code, orig_declv, i,
10220 TREE_VEC_ELT (declv, i), NULL_TREE,
10221 cond, cp_walk_subtrees))
10222 return true;
10224 switch (TREE_CODE (incr))
10226 case PREINCREMENT_EXPR:
10227 case PREDECREMENT_EXPR:
10228 case POSTINCREMENT_EXPR:
10229 case POSTDECREMENT_EXPR:
10230 if (TREE_OPERAND (incr, 0) != iter)
10232 incr = error_mark_node;
10233 break;
10235 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
10236 TREE_CODE (incr), iter,
10237 NULL_TREE, tf_warning_or_error);
10238 if (error_operand_p (iter_incr))
10239 return true;
10240 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
10241 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
10242 incr = integer_one_node;
10243 else
10244 incr = integer_minus_one_node;
10245 break;
10246 case MODIFY_EXPR:
10247 if (TREE_OPERAND (incr, 0) != iter)
10248 incr = error_mark_node;
10249 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
10250 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
10252 tree rhs = TREE_OPERAND (incr, 1);
10253 if (TREE_OPERAND (rhs, 0) == iter)
10255 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
10256 != INTEGER_TYPE)
10257 incr = error_mark_node;
10258 else
10260 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
10261 iter, TREE_CODE (rhs),
10262 TREE_OPERAND (rhs, 1),
10263 NULL_TREE,
10264 tf_warning_or_error);
10265 if (error_operand_p (iter_incr))
10266 return true;
10267 incr = TREE_OPERAND (rhs, 1);
10268 incr = cp_convert (TREE_TYPE (diff), incr,
10269 tf_warning_or_error);
10270 if (TREE_CODE (rhs) == MINUS_EXPR)
10272 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
10273 incr = fold_simple (incr);
10275 if (TREE_CODE (incr) != INTEGER_CST
10276 && (TREE_CODE (incr) != NOP_EXPR
10277 || (TREE_CODE (TREE_OPERAND (incr, 0))
10278 != INTEGER_CST)))
10279 iter_incr = NULL;
10282 else if (TREE_OPERAND (rhs, 1) == iter)
10284 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
10285 || TREE_CODE (rhs) != PLUS_EXPR)
10286 incr = error_mark_node;
10287 else
10289 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
10290 PLUS_EXPR,
10291 TREE_OPERAND (rhs, 0),
10292 ERROR_MARK, iter,
10293 ERROR_MARK, NULL_TREE, NULL,
10294 tf_warning_or_error);
10295 if (error_operand_p (iter_incr))
10296 return true;
10297 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
10298 iter, NOP_EXPR,
10299 iter_incr, NULL_TREE,
10300 tf_warning_or_error);
10301 if (error_operand_p (iter_incr))
10302 return true;
10303 incr = TREE_OPERAND (rhs, 0);
10304 iter_incr = NULL;
10307 else
10308 incr = error_mark_node;
10310 else
10311 incr = error_mark_node;
10312 break;
10313 default:
10314 incr = error_mark_node;
10315 break;
10318 if (incr == error_mark_node)
10320 error_at (elocus, "invalid increment expression");
10321 return true;
10324 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
10325 incr = cp_fully_fold (incr);
10326 tree loop_iv_seen = NULL_TREE;
10327 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10328 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
10329 && OMP_CLAUSE_DECL (c) == iter)
10331 if (code == OMP_TASKLOOP || code == OMP_LOOP)
10333 loop_iv_seen = c;
10334 OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c) = 1;
10336 break;
10338 else if ((code == OMP_TASKLOOP || code == OMP_LOOP)
10339 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
10340 && OMP_CLAUSE_DECL (c) == iter)
10342 loop_iv_seen = c;
10343 if (code == OMP_TASKLOOP)
10344 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
10347 decl = create_temporary_var (TREE_TYPE (diff));
10348 pushdecl (decl);
10349 add_decl_expr (decl);
10350 last = create_temporary_var (TREE_TYPE (diff));
10351 pushdecl (last);
10352 add_decl_expr (last);
10353 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
10354 && (!ordered || (i < collapse && collapse > 1)))
10356 incr_var = create_temporary_var (TREE_TYPE (diff));
10357 pushdecl (incr_var);
10358 add_decl_expr (incr_var);
10360 gcc_assert (stmts_are_full_exprs_p ());
10361 tree diffvar = NULL_TREE;
10362 if (code == OMP_TASKLOOP)
10364 if (!loop_iv_seen)
10366 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
10367 OMP_CLAUSE_DECL (ivc) = iter;
10368 cxx_omp_finish_clause (ivc, NULL, false);
10369 OMP_CLAUSE_CHAIN (ivc) = clauses;
10370 clauses = ivc;
10372 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
10373 OMP_CLAUSE_DECL (lvc) = last;
10374 OMP_CLAUSE_CHAIN (lvc) = clauses;
10375 clauses = lvc;
10376 diffvar = create_temporary_var (TREE_TYPE (diff));
10377 pushdecl (diffvar);
10378 add_decl_expr (diffvar);
10380 else if (code == OMP_LOOP)
10382 if (!loop_iv_seen)
10384 /* While iterators on the loop construct are predetermined
10385 lastprivate, if the decl is not declared inside of the
10386 loop, OMP_CLAUSE_LASTPRIVATE should have been added
10387 already. */
10388 loop_iv_seen = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
10389 OMP_CLAUSE_DECL (loop_iv_seen) = iter;
10390 OMP_CLAUSE_CHAIN (loop_iv_seen) = clauses;
10391 clauses = loop_iv_seen;
10393 else if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_PRIVATE)
10395 OMP_CLAUSE_PRIVATE_DEBUG (loop_iv_seen) = 0;
10396 OMP_CLAUSE_PRIVATE_OUTER_REF (loop_iv_seen) = 0;
10397 OMP_CLAUSE_CODE (loop_iv_seen) = OMP_CLAUSE_FIRSTPRIVATE;
10399 if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_FIRSTPRIVATE)
10400 cxx_omp_finish_clause (loop_iv_seen, NULL, false);
10403 orig_pre_body = *pre_body;
10404 *pre_body = push_stmt_list ();
10405 if (orig_pre_body)
10406 add_stmt (orig_pre_body);
10407 if (init != NULL)
10408 finish_expr_stmt (build_x_modify_expr (elocus,
10409 iter, NOP_EXPR, init,
10410 NULL_TREE, tf_warning_or_error));
10411 init = build_int_cst (TREE_TYPE (diff), 0);
10412 if (c && iter_incr == NULL
10413 && (!ordered || (i < collapse && collapse > 1)))
10415 if (incr_var)
10417 finish_expr_stmt (build_x_modify_expr (elocus,
10418 incr_var, NOP_EXPR,
10419 incr, NULL_TREE,
10420 tf_warning_or_error));
10421 incr = incr_var;
10423 iter_incr = build_x_modify_expr (elocus,
10424 iter, PLUS_EXPR, incr,
10425 NULL_TREE, tf_warning_or_error);
10427 if (c && ordered && i < collapse && collapse > 1)
10428 iter_incr = incr;
10429 finish_expr_stmt (build_x_modify_expr (elocus,
10430 last, NOP_EXPR, init,
10431 NULL_TREE, tf_warning_or_error));
10432 if (diffvar)
10434 finish_expr_stmt (build_x_modify_expr (elocus,
10435 diffvar, NOP_EXPR,
10436 diff, NULL_TREE, tf_warning_or_error));
10437 diff = diffvar;
10439 *pre_body = pop_stmt_list (*pre_body);
10441 cond = cp_build_binary_op (elocus,
10442 TREE_CODE (cond), decl, diff,
10443 tf_warning_or_error);
10444 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
10445 elocus, incr, NULL_TREE);
10447 orig_body = *body;
10448 *body = push_stmt_list ();
10449 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
10450 iter_init = build_x_modify_expr (elocus,
10451 iter, PLUS_EXPR, iter_init,
10452 NULL_TREE, tf_warning_or_error);
10453 if (iter_init != error_mark_node)
10454 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
10455 finish_expr_stmt (iter_init);
10456 finish_expr_stmt (build_x_modify_expr (elocus,
10457 last, NOP_EXPR, decl,
10458 NULL_TREE, tf_warning_or_error));
10459 add_stmt (orig_body);
10460 *body = pop_stmt_list (*body);
10462 if (c)
10464 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
10465 if (!ordered)
10466 finish_expr_stmt (iter_incr);
10467 else
10469 iter_init = decl;
10470 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
10471 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
10472 iter_init, iter_incr);
10473 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
10474 iter_init = build_x_modify_expr (elocus,
10475 iter, PLUS_EXPR, iter_init,
10476 NULL_TREE, tf_warning_or_error);
10477 if (iter_init != error_mark_node)
10478 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
10479 finish_expr_stmt (iter_init);
10481 OMP_CLAUSE_LASTPRIVATE_STMT (c)
10482 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
10485 if (TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST)
10487 tree t = TREE_VEC_ELT (orig_declv, i);
10488 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
10489 && TREE_VALUE (t) == NULL_TREE
10490 && TREE_CODE (TREE_CHAIN (t)) == TREE_VEC);
10491 TREE_PURPOSE (t) = TREE_VEC_ELT (declv, i);
10492 TREE_VALUE (t) = last;
10494 else
10495 TREE_VEC_ELT (orig_declv, i)
10496 = tree_cons (TREE_VEC_ELT (declv, i), last, NULL_TREE);
10497 TREE_VEC_ELT (declv, i) = decl;
10498 TREE_VEC_ELT (initv, i) = init;
10499 TREE_VEC_ELT (condv, i) = cond;
10500 TREE_VEC_ELT (incrv, i) = incr;
10502 return false;
10505 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
10506 are directly for their associated operands in the statement. DECL
10507 and INIT are a combo; if DECL is NULL then INIT ought to be a
10508 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
10509 optional statements that need to go before the loop into its
10510 sk_omp scope. */
10512 tree
10513 finish_omp_for (location_t locus, enum tree_code code, tree declv,
10514 tree orig_declv, tree initv, tree condv, tree incrv,
10515 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
10517 tree omp_for = NULL, orig_incr = NULL;
10518 tree decl = NULL, init, cond, incr;
10519 location_t elocus;
10520 int i;
10521 int collapse = 1;
10522 int ordered = 0;
10524 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
10525 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
10526 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
10527 if (TREE_VEC_LENGTH (declv) > 1)
10529 tree c;
10531 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
10532 if (c)
10533 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
10534 else
10536 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
10537 if (c)
10538 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
10539 if (collapse != TREE_VEC_LENGTH (declv))
10540 ordered = TREE_VEC_LENGTH (declv);
10543 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
10545 decl = TREE_VEC_ELT (declv, i);
10546 init = TREE_VEC_ELT (initv, i);
10547 cond = TREE_VEC_ELT (condv, i);
10548 incr = TREE_VEC_ELT (incrv, i);
10549 elocus = locus;
10551 if (decl == NULL)
10553 if (init != NULL)
10554 switch (TREE_CODE (init))
10556 case MODIFY_EXPR:
10557 decl = TREE_OPERAND (init, 0);
10558 init = TREE_OPERAND (init, 1);
10559 break;
10560 case MODOP_EXPR:
10561 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
10563 decl = TREE_OPERAND (init, 0);
10564 init = TREE_OPERAND (init, 2);
10566 break;
10567 default:
10568 break;
10571 if (decl == NULL)
10573 error_at (locus,
10574 "expected iteration declaration or initialization");
10575 return NULL;
10579 if (init && EXPR_HAS_LOCATION (init))
10580 elocus = EXPR_LOCATION (init);
10582 if (cond == global_namespace)
10583 continue;
10585 if (cond == NULL)
10587 error_at (elocus, "missing controlling predicate");
10588 return NULL;
10591 if (incr == NULL)
10593 error_at (elocus, "missing increment expression");
10594 return NULL;
10597 TREE_VEC_ELT (declv, i) = decl;
10598 TREE_VEC_ELT (initv, i) = init;
10601 if (orig_inits)
10603 bool fail = false;
10604 tree orig_init;
10605 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
10606 if (orig_init
10607 && !c_omp_check_loop_iv_exprs (locus, code,
10608 orig_declv ? orig_declv : declv, i,
10609 TREE_VEC_ELT (declv, i), orig_init,
10610 NULL_TREE, cp_walk_subtrees))
10611 fail = true;
10612 if (fail)
10613 return NULL;
10616 if (dependent_omp_for_p (declv, initv, condv, incrv))
10618 tree stmt;
10620 stmt = make_node (code);
10622 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
10624 /* This is really just a place-holder. We'll be decomposing this
10625 again and going through the cp_build_modify_expr path below when
10626 we instantiate the thing. */
10627 TREE_VEC_ELT (initv, i)
10628 = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
10629 TREE_VEC_ELT (initv, i));
10632 TREE_TYPE (stmt) = void_type_node;
10633 OMP_FOR_INIT (stmt) = initv;
10634 OMP_FOR_COND (stmt) = condv;
10635 OMP_FOR_INCR (stmt) = incrv;
10636 OMP_FOR_BODY (stmt) = body;
10637 OMP_FOR_PRE_BODY (stmt) = pre_body;
10638 OMP_FOR_CLAUSES (stmt) = clauses;
10640 SET_EXPR_LOCATION (stmt, locus);
10641 return add_stmt (stmt);
10644 if (!orig_declv)
10645 orig_declv = copy_node (declv);
10647 if (processing_template_decl)
10648 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
10650 for (i = 0; i < TREE_VEC_LENGTH (declv); )
10652 decl = TREE_VEC_ELT (declv, i);
10653 init = TREE_VEC_ELT (initv, i);
10654 cond = TREE_VEC_ELT (condv, i);
10655 incr = TREE_VEC_ELT (incrv, i);
10656 if (orig_incr)
10657 TREE_VEC_ELT (orig_incr, i) = incr;
10658 elocus = locus;
10660 if (init && EXPR_HAS_LOCATION (init))
10661 elocus = EXPR_LOCATION (init);
10663 if (!DECL_P (decl))
10665 error_at (elocus, "expected iteration declaration or initialization");
10666 return NULL;
10669 if (incr && TREE_CODE (incr) == MODOP_EXPR)
10671 if (orig_incr)
10672 TREE_VEC_ELT (orig_incr, i) = incr;
10673 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
10674 TREE_CODE (TREE_OPERAND (incr, 1)),
10675 TREE_OPERAND (incr, 2),
10676 tf_warning_or_error);
10679 if (CLASS_TYPE_P (TREE_TYPE (decl)))
10681 if (code == OMP_SIMD)
10683 error_at (elocus, "%<#pragma omp simd%> used with class "
10684 "iteration variable %qE", decl);
10685 return NULL;
10687 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
10688 initv, condv, incrv, &body,
10689 &pre_body, clauses,
10690 collapse, ordered))
10691 return NULL;
10692 continue;
10695 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
10696 && !TYPE_PTR_P (TREE_TYPE (decl)))
10698 error_at (elocus, "invalid type for iteration variable %qE", decl);
10699 return NULL;
10702 if (!processing_template_decl && TREE_CODE (init) != TREE_VEC)
10703 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
10704 tf_warning_or_error);
10705 else
10706 init = build2 (MODIFY_EXPR, void_type_node, decl, init);
10707 if (decl == error_mark_node || init == error_mark_node)
10708 return NULL;
10710 TREE_VEC_ELT (declv, i) = decl;
10711 TREE_VEC_ELT (initv, i) = init;
10712 TREE_VEC_ELT (condv, i) = cond;
10713 TREE_VEC_ELT (incrv, i) = incr;
10714 i++;
10717 if (pre_body && IS_EMPTY_STMT (pre_body))
10718 pre_body = NULL;
10720 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
10721 incrv, body, pre_body,
10722 !processing_template_decl);
10724 /* Check for iterators appearing in lb, b or incr expressions. */
10725 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
10726 omp_for = NULL_TREE;
10728 if (omp_for == NULL)
10729 return NULL;
10731 add_stmt (omp_for);
10733 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
10735 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i);
10736 decl = TREE_OPERAND (init, 0);
10737 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), i);
10738 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
10740 if (!processing_template_decl)
10742 if (TREE_CODE (TREE_OPERAND (init, 1)) == TREE_VEC)
10744 tree t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 1);
10745 TREE_VEC_ELT (TREE_OPERAND (init, 1), 1)
10746 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10747 t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 2);
10748 TREE_VEC_ELT (TREE_OPERAND (init, 1), 2)
10749 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10751 else
10753 tree t = TREE_OPERAND (init, 1);
10754 TREE_OPERAND (init, 1)
10755 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10757 if (TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
10759 tree t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1);
10760 TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1)
10761 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10762 t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2);
10763 TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2)
10764 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10766 else
10768 tree t = TREE_OPERAND (cond, 1);
10769 TREE_OPERAND (cond, 1)
10770 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10774 if (TREE_CODE (incr) != MODIFY_EXPR)
10775 continue;
10777 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
10778 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
10779 && !processing_template_decl)
10781 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
10782 if (TREE_SIDE_EFFECTS (t)
10783 && t != decl
10784 && (TREE_CODE (t) != NOP_EXPR
10785 || TREE_OPERAND (t, 0) != decl))
10786 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
10787 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10789 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
10790 if (TREE_SIDE_EFFECTS (t)
10791 && t != decl
10792 && (TREE_CODE (t) != NOP_EXPR
10793 || TREE_OPERAND (t, 0) != decl))
10794 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
10795 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10798 if (orig_incr)
10799 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
10801 OMP_FOR_CLAUSES (omp_for) = clauses;
10803 /* For simd loops with non-static data member iterators, we could have added
10804 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
10805 step at this point, fill it in. */
10806 if (code == OMP_SIMD && !processing_template_decl
10807 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
10808 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
10809 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
10810 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
10812 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
10813 gcc_assert (decl == OMP_CLAUSE_DECL (c));
10814 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
10815 tree step, stept;
10816 switch (TREE_CODE (incr))
10818 case PREINCREMENT_EXPR:
10819 case POSTINCREMENT_EXPR:
10820 /* c_omp_for_incr_canonicalize_ptr() should have been
10821 called to massage things appropriately. */
10822 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
10823 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
10824 break;
10825 case PREDECREMENT_EXPR:
10826 case POSTDECREMENT_EXPR:
10827 /* c_omp_for_incr_canonicalize_ptr() should have been
10828 called to massage things appropriately. */
10829 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
10830 OMP_CLAUSE_LINEAR_STEP (c)
10831 = build_int_cst (TREE_TYPE (decl), -1);
10832 break;
10833 case MODIFY_EXPR:
10834 gcc_assert (TREE_OPERAND (incr, 0) == decl);
10835 incr = TREE_OPERAND (incr, 1);
10836 switch (TREE_CODE (incr))
10838 case PLUS_EXPR:
10839 if (TREE_OPERAND (incr, 1) == decl)
10840 step = TREE_OPERAND (incr, 0);
10841 else
10842 step = TREE_OPERAND (incr, 1);
10843 break;
10844 case MINUS_EXPR:
10845 case POINTER_PLUS_EXPR:
10846 gcc_assert (TREE_OPERAND (incr, 0) == decl);
10847 step = TREE_OPERAND (incr, 1);
10848 break;
10849 default:
10850 gcc_unreachable ();
10852 stept = TREE_TYPE (decl);
10853 if (INDIRECT_TYPE_P (stept))
10854 stept = sizetype;
10855 step = fold_convert (stept, step);
10856 if (TREE_CODE (incr) == MINUS_EXPR)
10857 step = fold_build1 (NEGATE_EXPR, stept, step);
10858 OMP_CLAUSE_LINEAR_STEP (c) = step;
10859 break;
10860 default:
10861 gcc_unreachable ();
10864 /* Override saved methods on OMP_LOOP's OMP_CLAUSE_LASTPRIVATE_LOOP_IV
10865 clauses, we need copy ctor for those rather than default ctor,
10866 plus as for other lastprivates assignment op and dtor. */
10867 if (code == OMP_LOOP && !processing_template_decl)
10868 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
10869 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
10870 && OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c)
10871 && cxx_omp_create_clause_info (c, TREE_TYPE (OMP_CLAUSE_DECL (c)),
10872 false, true, true, true))
10873 CP_OMP_CLAUSE_INFO (c) = NULL_TREE;
10875 return omp_for;
10878 /* Fix up range for decls. Those decls were pushed into BIND's BIND_EXPR_VARS
10879 and need to be moved into the BIND_EXPR inside of the OMP_FOR's body. */
10881 tree
10882 finish_omp_for_block (tree bind, tree omp_for)
10884 if (omp_for == NULL_TREE
10885 || !OMP_FOR_ORIG_DECLS (omp_for)
10886 || bind == NULL_TREE
10887 || TREE_CODE (bind) != BIND_EXPR)
10888 return bind;
10889 tree b = NULL_TREE;
10890 for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (omp_for)); i++)
10891 if (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)) == TREE_LIST
10892 && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)))
10894 tree v = TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i));
10895 gcc_assert (BIND_EXPR_BLOCK (bind)
10896 && (BIND_EXPR_VARS (bind)
10897 == BLOCK_VARS (BIND_EXPR_BLOCK (bind))));
10898 for (int j = 2; j < TREE_VEC_LENGTH (v); j++)
10899 for (tree *p = &BIND_EXPR_VARS (bind); *p; p = &DECL_CHAIN (*p))
10901 if (*p == TREE_VEC_ELT (v, j))
10903 tree var = *p;
10904 *p = DECL_CHAIN (*p);
10905 if (b == NULL_TREE)
10907 b = make_node (BLOCK);
10908 b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
10909 OMP_FOR_BODY (omp_for), b);
10910 TREE_SIDE_EFFECTS (b) = 1;
10911 OMP_FOR_BODY (omp_for) = b;
10913 DECL_CHAIN (var) = BIND_EXPR_VARS (b);
10914 BIND_EXPR_VARS (b) = var;
10915 BLOCK_VARS (BIND_EXPR_BLOCK (b)) = var;
10918 BLOCK_VARS (BIND_EXPR_BLOCK (bind)) = BIND_EXPR_VARS (bind);
10920 return bind;
10923 void
10924 finish_omp_atomic (location_t loc, enum tree_code code, enum tree_code opcode,
10925 tree lhs, tree rhs, tree v, tree lhs1, tree rhs1, tree r,
10926 tree clauses, enum omp_memory_order mo, bool weak)
10928 tree orig_lhs;
10929 tree orig_rhs;
10930 tree orig_v;
10931 tree orig_lhs1;
10932 tree orig_rhs1;
10933 tree orig_r;
10934 bool dependent_p;
10935 tree stmt;
10937 orig_lhs = lhs;
10938 orig_rhs = rhs;
10939 orig_v = v;
10940 orig_lhs1 = lhs1;
10941 orig_rhs1 = rhs1;
10942 orig_r = r;
10943 dependent_p = false;
10944 stmt = NULL_TREE;
10946 /* Even in a template, we can detect invalid uses of the atomic
10947 pragma if neither LHS nor RHS is type-dependent. */
10948 if (processing_template_decl)
10950 dependent_p = (type_dependent_expression_p (lhs)
10951 || (rhs && type_dependent_expression_p (rhs))
10952 || (v && type_dependent_expression_p (v))
10953 || (lhs1 && type_dependent_expression_p (lhs1))
10954 || (rhs1 && type_dependent_expression_p (rhs1))
10955 || (r
10956 && r != void_list_node
10957 && type_dependent_expression_p (r)));
10958 if (clauses)
10960 gcc_assert (TREE_CODE (clauses) == OMP_CLAUSE
10961 && OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_HINT
10962 && OMP_CLAUSE_CHAIN (clauses) == NULL_TREE);
10963 if (type_dependent_expression_p (OMP_CLAUSE_HINT_EXPR (clauses))
10964 || TREE_CODE (OMP_CLAUSE_HINT_EXPR (clauses)) != INTEGER_CST)
10965 dependent_p = true;
10967 if (!dependent_p)
10969 lhs = build_non_dependent_expr (lhs);
10970 if (rhs)
10971 rhs = build_non_dependent_expr (rhs);
10972 if (v)
10973 v = build_non_dependent_expr (v);
10974 if (lhs1)
10975 lhs1 = build_non_dependent_expr (lhs1);
10976 if (rhs1)
10977 rhs1 = build_non_dependent_expr (rhs1);
10978 if (r && r != void_list_node)
10979 r = build_non_dependent_expr (r);
10982 if (!dependent_p)
10984 bool swapped = false;
10985 if (rhs1 && opcode != COND_EXPR && cp_tree_equal (lhs, rhs))
10987 std::swap (rhs, rhs1);
10988 swapped = !commutative_tree_code (opcode);
10990 if (rhs1 && opcode != COND_EXPR && !cp_tree_equal (lhs, rhs1))
10992 if (code == OMP_ATOMIC)
10993 error ("%<#pragma omp atomic update%> uses two different "
10994 "expressions for memory");
10995 else
10996 error ("%<#pragma omp atomic capture%> uses two different "
10997 "expressions for memory");
10998 return;
11000 if (lhs1 && !cp_tree_equal (lhs, lhs1))
11002 if (code == OMP_ATOMIC)
11003 error ("%<#pragma omp atomic update%> uses two different "
11004 "expressions for memory");
11005 else
11006 error ("%<#pragma omp atomic capture%> uses two different "
11007 "expressions for memory");
11008 return;
11010 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs,
11011 v, lhs1, rhs1, r, swapped, mo, weak,
11012 processing_template_decl != 0);
11013 if (stmt == error_mark_node)
11014 return;
11016 if (processing_template_decl)
11018 if (code == OMP_ATOMIC_READ)
11020 stmt = build_min_nt_loc (loc, OMP_ATOMIC_READ, orig_lhs);
11021 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
11022 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
11024 else
11026 if (opcode == NOP_EXPR)
11027 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
11028 else if (opcode == COND_EXPR)
11030 stmt = build2 (EQ_EXPR, boolean_type_node, orig_lhs, orig_rhs);
11031 if (orig_r)
11032 stmt = build2 (MODIFY_EXPR, boolean_type_node, orig_r,
11033 stmt);
11034 stmt = build3 (COND_EXPR, void_type_node, stmt, orig_rhs1,
11035 orig_lhs);
11036 orig_rhs1 = NULL_TREE;
11038 else
11039 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
11040 if (orig_rhs1)
11041 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
11042 COMPOUND_EXPR, orig_rhs1, stmt);
11043 if (code != OMP_ATOMIC)
11045 stmt = build_min_nt_loc (loc, code, orig_lhs1, stmt);
11046 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
11047 OMP_ATOMIC_WEAK (stmt) = weak;
11048 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
11051 stmt = build2 (OMP_ATOMIC, void_type_node,
11052 clauses ? clauses : integer_zero_node, stmt);
11053 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
11054 OMP_ATOMIC_WEAK (stmt) = weak;
11055 SET_EXPR_LOCATION (stmt, loc);
11058 /* Avoid -Wunused-value warnings here, the whole construct has side-effects
11059 and even if it might be wrapped from fold-const.cc or c-omp.cc wrapped
11060 in some tree that appears to be unused, the value is not unused. */
11061 warning_sentinel w (warn_unused_value);
11062 finish_expr_stmt (stmt);
11065 void
11066 finish_omp_barrier (void)
11068 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
11069 releasing_vec vec;
11070 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11071 finish_expr_stmt (stmt);
11074 void
11075 finish_omp_depobj (location_t loc, tree depobj,
11076 enum omp_clause_depend_kind kind, tree clause)
11078 if (!error_operand_p (depobj) && !type_dependent_expression_p (depobj))
11080 if (!lvalue_p (depobj))
11082 error_at (EXPR_LOC_OR_LOC (depobj, loc),
11083 "%<depobj%> expression is not lvalue expression");
11084 depobj = error_mark_node;
11088 if (processing_template_decl)
11090 if (clause == NULL_TREE)
11091 clause = build_int_cst (integer_type_node, kind);
11092 add_stmt (build_min_nt_loc (loc, OMP_DEPOBJ, depobj, clause));
11093 return;
11096 if (!error_operand_p (depobj))
11098 tree addr = cp_build_addr_expr (depobj, tf_warning_or_error);
11099 if (addr == error_mark_node)
11100 depobj = error_mark_node;
11101 else
11102 depobj = cp_build_indirect_ref (loc, addr, RO_UNARY_STAR,
11103 tf_warning_or_error);
11106 c_finish_omp_depobj (loc, depobj, kind, clause);
11109 void
11110 finish_omp_flush (int mo)
11112 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
11113 releasing_vec vec;
11114 if (mo != MEMMODEL_LAST && mo != MEMMODEL_SEQ_CST)
11116 fn = builtin_decl_explicit (BUILT_IN_ATOMIC_THREAD_FENCE);
11117 vec->quick_push (build_int_cst (integer_type_node, mo));
11119 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11120 finish_expr_stmt (stmt);
11123 void
11124 finish_omp_taskwait (void)
11126 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
11127 releasing_vec vec;
11128 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11129 finish_expr_stmt (stmt);
11132 void
11133 finish_omp_taskyield (void)
11135 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
11136 releasing_vec vec;
11137 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11138 finish_expr_stmt (stmt);
11141 void
11142 finish_omp_cancel (tree clauses)
11144 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11145 int mask = 0;
11146 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
11147 mask = 1;
11148 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
11149 mask = 2;
11150 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
11151 mask = 4;
11152 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
11153 mask = 8;
11154 else
11156 error ("%<#pragma omp cancel%> must specify one of "
11157 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
11158 return;
11160 releasing_vec vec;
11161 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
11162 if (ifc != NULL_TREE)
11164 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
11165 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
11166 error_at (OMP_CLAUSE_LOCATION (ifc),
11167 "expected %<cancel%> %<if%> clause modifier");
11168 else
11170 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
11171 if (ifc2 != NULL_TREE)
11173 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
11174 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
11175 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
11176 error_at (OMP_CLAUSE_LOCATION (ifc2),
11177 "expected %<cancel%> %<if%> clause modifier");
11181 if (!processing_template_decl)
11182 ifc = maybe_convert_cond (OMP_CLAUSE_IF_EXPR (ifc));
11183 else
11184 ifc = build_x_binary_op (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11185 OMP_CLAUSE_IF_EXPR (ifc), ERROR_MARK,
11186 integer_zero_node, ERROR_MARK,
11187 NULL_TREE, NULL, tf_warning_or_error);
11189 else
11190 ifc = boolean_true_node;
11191 vec->quick_push (build_int_cst (integer_type_node, mask));
11192 vec->quick_push (ifc);
11193 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11194 finish_expr_stmt (stmt);
11197 void
11198 finish_omp_cancellation_point (tree clauses)
11200 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11201 int mask = 0;
11202 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
11203 mask = 1;
11204 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
11205 mask = 2;
11206 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
11207 mask = 4;
11208 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
11209 mask = 8;
11210 else
11212 error ("%<#pragma omp cancellation point%> must specify one of "
11213 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
11214 return;
11216 releasing_vec vec
11217 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
11218 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11219 finish_expr_stmt (stmt);
11222 /* Begin a __transaction_atomic or __transaction_relaxed statement.
11223 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
11224 should create an extra compound stmt. */
11226 tree
11227 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
11229 tree r;
11231 if (pcompound)
11232 *pcompound = begin_compound_stmt (0);
11234 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
11236 /* Only add the statement to the function if support enabled. */
11237 if (flag_tm)
11238 add_stmt (r);
11239 else
11240 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
11241 ? G_("%<__transaction_relaxed%> without "
11242 "transactional memory support enabled")
11243 : G_("%<__transaction_atomic%> without "
11244 "transactional memory support enabled")));
11246 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
11247 TREE_SIDE_EFFECTS (r) = 1;
11248 return r;
11251 /* End a __transaction_atomic or __transaction_relaxed statement.
11252 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
11253 and we should end the compound. If NOEX is non-NULL, we wrap the body in
11254 a MUST_NOT_THROW_EXPR with NOEX as condition. */
11256 void
11257 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
11259 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
11260 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
11261 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
11262 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
11264 /* noexcept specifications are not allowed for function transactions. */
11265 gcc_assert (!(noex && compound_stmt));
11266 if (noex)
11268 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
11269 noex);
11270 protected_set_expr_location
11271 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
11272 TREE_SIDE_EFFECTS (body) = 1;
11273 TRANSACTION_EXPR_BODY (stmt) = body;
11276 if (compound_stmt)
11277 finish_compound_stmt (compound_stmt);
11280 /* Build a __transaction_atomic or __transaction_relaxed expression. If
11281 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
11282 condition. */
11284 tree
11285 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
11287 tree ret;
11288 if (noex)
11290 expr = build_must_not_throw_expr (expr, noex);
11291 protected_set_expr_location (expr, loc);
11292 TREE_SIDE_EFFECTS (expr) = 1;
11294 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
11295 if (flags & TM_STMT_ATTR_RELAXED)
11296 TRANSACTION_EXPR_RELAXED (ret) = 1;
11297 TREE_SIDE_EFFECTS (ret) = 1;
11298 SET_EXPR_LOCATION (ret, loc);
11299 return ret;
11302 void
11303 init_cp_semantics (void)
11308 /* Build a STATIC_ASSERT for a static assertion with the condition
11309 CONDITION and the message text MESSAGE. LOCATION is the location
11310 of the static assertion in the source code. When MEMBER_P, this
11311 static assertion is a member of a class. If SHOW_EXPR_P is true,
11312 print the condition (because it was instantiation-dependent). */
11314 void
11315 finish_static_assert (tree condition, tree message, location_t location,
11316 bool member_p, bool show_expr_p)
11318 tsubst_flags_t complain = tf_warning_or_error;
11320 if (message == NULL_TREE
11321 || message == error_mark_node
11322 || condition == NULL_TREE
11323 || condition == error_mark_node)
11324 return;
11326 if (check_for_bare_parameter_packs (condition))
11327 condition = error_mark_node;
11329 /* Save the condition in case it was a concept check. */
11330 tree orig_condition = condition;
11332 if (instantiation_dependent_expression_p (condition))
11334 /* We're in a template; build a STATIC_ASSERT and put it in
11335 the right place. */
11336 defer:
11337 tree assertion = make_node (STATIC_ASSERT);
11338 STATIC_ASSERT_CONDITION (assertion) = orig_condition;
11339 STATIC_ASSERT_MESSAGE (assertion) = message;
11340 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
11342 if (member_p)
11343 maybe_add_class_template_decl_list (current_class_type,
11344 assertion,
11345 /*friend_p=*/0);
11346 else
11347 add_stmt (assertion);
11349 return;
11352 /* Fold the expression and convert it to a boolean value. */
11353 condition = contextual_conv_bool (condition, complain);
11354 condition = fold_non_dependent_expr (condition, complain,
11355 /*manifestly_const_eval=*/true);
11357 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
11358 /* Do nothing; the condition is satisfied. */
11360 else
11362 iloc_sentinel ils (location);
11364 if (integer_zerop (condition))
11366 /* CWG2518: static_assert failure in a template is not IFNDR. */
11367 if (processing_template_decl)
11368 goto defer;
11370 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
11371 (TREE_TYPE (TREE_TYPE (message))));
11372 int len = TREE_STRING_LENGTH (message) / sz - 1;
11374 /* See if we can find which clause was failing (for logical AND). */
11375 tree bad = find_failing_clause (NULL, orig_condition);
11376 /* If not, or its location is unusable, fall back to the previous
11377 location. */
11378 location_t cloc = cp_expr_loc_or_loc (bad, location);
11380 auto_diagnostic_group d;
11382 /* Report the error. */
11383 if (len == 0)
11384 error_at (cloc, "static assertion failed");
11385 else
11386 error_at (cloc, "static assertion failed: %s",
11387 TREE_STRING_POINTER (message));
11389 diagnose_failing_condition (bad, cloc, show_expr_p);
11391 else if (condition && condition != error_mark_node)
11393 error ("non-constant condition for static assertion");
11394 if (require_rvalue_constant_expression (condition))
11395 cxx_constant_value (condition);
11400 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
11401 suitable for use as a type-specifier.
11403 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
11404 id-expression or a class member access, FALSE when it was parsed as
11405 a full expression. */
11407 tree
11408 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
11409 tsubst_flags_t complain)
11411 tree type = NULL_TREE;
11413 if (!expr || error_operand_p (expr))
11414 return error_mark_node;
11416 if (TYPE_P (expr)
11417 || TREE_CODE (expr) == TYPE_DECL
11418 || (TREE_CODE (expr) == BIT_NOT_EXPR
11419 && TYPE_P (TREE_OPERAND (expr, 0))))
11421 if (complain & tf_error)
11422 error ("argument to %<decltype%> must be an expression");
11423 return error_mark_node;
11426 /* decltype is an unevaluated context. */
11427 cp_unevaluated u;
11429 processing_template_decl_sentinel ptds (/*reset=*/false);
11431 /* Depending on the resolution of DR 1172, we may later need to distinguish
11432 instantiation-dependent but not type-dependent expressions so that, say,
11433 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
11434 if (instantiation_dependent_uneval_expression_p (expr))
11436 type = cxx_make_type (DECLTYPE_TYPE);
11437 DECLTYPE_TYPE_EXPR (type) = expr;
11438 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
11439 = id_expression_or_member_access_p;
11440 SET_TYPE_STRUCTURAL_EQUALITY (type);
11442 return type;
11444 else if (processing_template_decl)
11446 expr = instantiate_non_dependent_expr (expr, complain|tf_decltype);
11447 if (expr == error_mark_node)
11448 return error_mark_node;
11449 /* Keep processing_template_decl cleared for the rest of the function
11450 (for sake of the call to lvalue_kind below, which handles templated
11451 and non-templated COND_EXPR differently). */
11452 processing_template_decl = 0;
11455 /* The type denoted by decltype(e) is defined as follows: */
11457 expr = resolve_nondeduced_context (expr, complain);
11458 if (!mark_single_function (expr, complain))
11459 return error_mark_node;
11461 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
11462 return error_mark_node;
11464 if (type_unknown_p (expr))
11466 if (complain & tf_error)
11467 error ("%<decltype%> cannot resolve address of overloaded function");
11468 return error_mark_node;
11471 /* To get the size of a static data member declared as an array of
11472 unknown bound, we need to instantiate it. */
11473 if (VAR_P (expr)
11474 && VAR_HAD_UNKNOWN_BOUND (expr)
11475 && DECL_TEMPLATE_INSTANTIATION (expr))
11476 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
11478 if (id_expression_or_member_access_p)
11480 /* If e is an id-expression or a class member access (5.2.5
11481 [expr.ref]), decltype(e) is defined as the type of the entity
11482 named by e. If there is no such entity, or e names a set of
11483 overloaded functions, the program is ill-formed. */
11484 if (identifier_p (expr))
11485 expr = lookup_name (expr);
11487 if (INDIRECT_REF_P (expr)
11488 || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
11489 /* This can happen when the expression is, e.g., "a.b". Just
11490 look at the underlying operand. */
11491 expr = TREE_OPERAND (expr, 0);
11493 if (TREE_CODE (expr) == OFFSET_REF
11494 || TREE_CODE (expr) == MEMBER_REF
11495 || TREE_CODE (expr) == SCOPE_REF)
11496 /* We're only interested in the field itself. If it is a
11497 BASELINK, we will need to see through it in the next
11498 step. */
11499 expr = TREE_OPERAND (expr, 1);
11501 if (BASELINK_P (expr))
11502 /* See through BASELINK nodes to the underlying function. */
11503 expr = BASELINK_FUNCTIONS (expr);
11505 /* decltype of a decomposition name drops references in the tuple case
11506 (unlike decltype of a normal variable) and keeps cv-qualifiers from
11507 the containing object in the other cases (unlike decltype of a member
11508 access expression). */
11509 if (DECL_DECOMPOSITION_P (expr))
11511 if (DECL_HAS_VALUE_EXPR_P (expr))
11512 /* Expr is an array or struct subobject proxy, handle
11513 bit-fields properly. */
11514 return unlowered_expr_type (expr);
11515 else
11516 /* Expr is a reference variable for the tuple case. */
11517 return lookup_decomp_type (expr);
11520 switch (TREE_CODE (expr))
11522 case FIELD_DECL:
11523 if (DECL_BIT_FIELD_TYPE (expr))
11525 type = DECL_BIT_FIELD_TYPE (expr);
11526 break;
11528 /* Fall through for fields that aren't bitfields. */
11529 gcc_fallthrough ();
11531 case FUNCTION_DECL:
11532 case VAR_DECL:
11533 case CONST_DECL:
11534 case PARM_DECL:
11535 case RESULT_DECL:
11536 case TEMPLATE_PARM_INDEX:
11537 expr = mark_type_use (expr);
11538 type = TREE_TYPE (expr);
11539 break;
11541 case ERROR_MARK:
11542 type = error_mark_node;
11543 break;
11545 case COMPONENT_REF:
11546 case COMPOUND_EXPR:
11547 mark_type_use (expr);
11548 type = is_bitfield_expr_with_lowered_type (expr);
11549 if (!type)
11550 type = TREE_TYPE (TREE_OPERAND (expr, 1));
11551 break;
11553 case BIT_FIELD_REF:
11554 gcc_unreachable ();
11556 case INTEGER_CST:
11557 case PTRMEM_CST:
11558 /* We can get here when the id-expression refers to an
11559 enumerator or non-type template parameter. */
11560 type = TREE_TYPE (expr);
11561 break;
11563 default:
11564 /* Handle instantiated template non-type arguments. */
11565 type = TREE_TYPE (expr);
11566 break;
11569 else
11571 /* Within a lambda-expression:
11573 Every occurrence of decltype((x)) where x is a possibly
11574 parenthesized id-expression that names an entity of
11575 automatic storage duration is treated as if x were
11576 transformed into an access to a corresponding data member
11577 of the closure type that would have been declared if x
11578 were a use of the denoted entity. */
11579 if (outer_automatic_var_p (expr)
11580 && current_function_decl
11581 && LAMBDA_FUNCTION_P (current_function_decl))
11582 type = capture_decltype (expr);
11583 else if (error_operand_p (expr))
11584 type = error_mark_node;
11585 else if (expr == current_class_ptr)
11586 /* If the expression is just "this", we want the
11587 cv-unqualified pointer for the "this" type. */
11588 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
11589 else
11591 /* Otherwise, where T is the type of e, if e is an lvalue,
11592 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
11593 cp_lvalue_kind clk = lvalue_kind (expr);
11594 type = unlowered_expr_type (expr);
11595 gcc_assert (!TYPE_REF_P (type));
11597 /* For vector types, pick a non-opaque variant. */
11598 if (VECTOR_TYPE_P (type))
11599 type = strip_typedefs (type);
11601 if (clk != clk_none && !(clk & clk_class))
11602 type = cp_build_reference_type (type, (clk & clk_rvalueref));
11606 return type;
11609 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
11610 __has_nothrow_copy, depending on assign_p. Returns true iff all
11611 the copy {ctor,assign} fns are nothrow. */
11613 static bool
11614 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
11616 tree fns = NULL_TREE;
11618 if (assign_p || TYPE_HAS_COPY_CTOR (type))
11619 fns = get_class_binding (type, assign_p ? assign_op_identifier
11620 : ctor_identifier);
11622 bool saw_copy = false;
11623 for (ovl_iterator iter (fns); iter; ++iter)
11625 tree fn = *iter;
11627 if (copy_fn_p (fn) > 0)
11629 saw_copy = true;
11630 if (!maybe_instantiate_noexcept (fn)
11631 || !TYPE_NOTHROW_P (TREE_TYPE (fn)))
11632 return false;
11636 return saw_copy;
11639 /* Return true if DERIVED is pointer interconvertible base of BASE. */
11641 static bool
11642 pointer_interconvertible_base_of_p (tree base, tree derived)
11644 if (base == error_mark_node || derived == error_mark_node)
11645 return false;
11646 base = TYPE_MAIN_VARIANT (base);
11647 derived = TYPE_MAIN_VARIANT (derived);
11648 if (!NON_UNION_CLASS_TYPE_P (base)
11649 || !NON_UNION_CLASS_TYPE_P (derived))
11650 return false;
11652 if (same_type_p (base, derived))
11653 return true;
11655 if (!std_layout_type_p (derived))
11656 return false;
11658 return uniquely_derived_from_p (base, derived);
11661 /* Helper function for fold_builtin_is_pointer_inverconvertible_with_class,
11662 return true if MEMBERTYPE is the type of the first non-static data member
11663 of TYPE or for unions of any members. */
11664 static bool
11665 first_nonstatic_data_member_p (tree type, tree membertype)
11667 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
11669 if (TREE_CODE (field) != FIELD_DECL)
11670 continue;
11671 if (DECL_FIELD_IS_BASE (field) && is_empty_field (field))
11672 continue;
11673 if (DECL_FIELD_IS_BASE (field))
11674 return first_nonstatic_data_member_p (TREE_TYPE (field), membertype);
11675 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
11677 if ((TREE_CODE (TREE_TYPE (field)) == UNION_TYPE
11678 || std_layout_type_p (TREE_TYPE (field)))
11679 && first_nonstatic_data_member_p (TREE_TYPE (field), membertype))
11680 return true;
11682 else if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field),
11683 membertype))
11684 return true;
11685 if (TREE_CODE (type) != UNION_TYPE)
11686 return false;
11688 return false;
11691 /* Fold __builtin_is_pointer_interconvertible_with_class call. */
11693 tree
11694 fold_builtin_is_pointer_inverconvertible_with_class (location_t loc, int nargs,
11695 tree *args)
11697 /* Unless users call the builtin directly, the following 3 checks should be
11698 ensured from std::is_pointer_interconvertible_with_class function
11699 template. */
11700 if (nargs != 1)
11702 error_at (loc, "%<__builtin_is_pointer_interconvertible_with_class%> "
11703 "needs a single argument");
11704 return boolean_false_node;
11706 tree arg = args[0];
11707 if (error_operand_p (arg))
11708 return boolean_false_node;
11709 if (!TYPE_PTRMEM_P (TREE_TYPE (arg)))
11711 error_at (loc, "%<__builtin_is_pointer_interconvertible_with_class%> "
11712 "argument is not pointer to member");
11713 return boolean_false_node;
11716 if (!TYPE_PTRDATAMEM_P (TREE_TYPE (arg)))
11717 return boolean_false_node;
11719 tree membertype = TREE_TYPE (TREE_TYPE (arg));
11720 tree basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg));
11721 if (!complete_type_or_else (basetype, NULL_TREE))
11722 return boolean_false_node;
11724 if (TREE_CODE (basetype) != UNION_TYPE
11725 && !std_layout_type_p (basetype))
11726 return boolean_false_node;
11728 if (!first_nonstatic_data_member_p (basetype, membertype))
11729 return boolean_false_node;
11731 if (TREE_CODE (arg) == PTRMEM_CST)
11732 arg = cplus_expand_constant (arg);
11734 if (integer_nonzerop (arg))
11735 return boolean_false_node;
11736 if (integer_zerop (arg))
11737 return boolean_true_node;
11739 return fold_build2 (EQ_EXPR, boolean_type_node, arg,
11740 build_zero_cst (TREE_TYPE (arg)));
11743 /* Helper function for is_corresponding_member_aggr. Return true if
11744 MEMBERTYPE pointer-to-data-member ARG can be found in anonymous
11745 union or structure BASETYPE. */
11747 static bool
11748 is_corresponding_member_union (tree basetype, tree membertype, tree arg)
11750 for (tree field = TYPE_FIELDS (basetype); field; field = DECL_CHAIN (field))
11751 if (TREE_CODE (field) != FIELD_DECL || DECL_BIT_FIELD_TYPE (field))
11752 continue;
11753 else if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field),
11754 membertype))
11756 if (TREE_CODE (arg) != INTEGER_CST
11757 || tree_int_cst_equal (arg, byte_position (field)))
11758 return true;
11760 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
11762 tree narg = arg;
11763 if (TREE_CODE (basetype) != UNION_TYPE
11764 && TREE_CODE (narg) == INTEGER_CST)
11765 narg = size_binop (MINUS_EXPR, arg, byte_position (field));
11766 if (is_corresponding_member_union (TREE_TYPE (field),
11767 membertype, narg))
11768 return true;
11770 return false;
11773 /* Helper function for fold_builtin_is_corresponding_member call.
11774 Return boolean_false_node if MEMBERTYPE1 BASETYPE1::*ARG1 and
11775 MEMBERTYPE2 BASETYPE2::*ARG2 aren't corresponding members,
11776 boolean_true_node if they are corresponding members, or for
11777 non-constant ARG2 the highest member offset for corresponding
11778 members. */
11780 static tree
11781 is_corresponding_member_aggr (location_t loc, tree basetype1, tree membertype1,
11782 tree arg1, tree basetype2, tree membertype2,
11783 tree arg2)
11785 tree field1 = TYPE_FIELDS (basetype1);
11786 tree field2 = TYPE_FIELDS (basetype2);
11787 tree ret = boolean_false_node;
11788 while (1)
11790 bool r = next_common_initial_sequence (field1, field2);
11791 if (field1 == NULL_TREE || field2 == NULL_TREE)
11792 break;
11793 if (r
11794 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field1),
11795 membertype1)
11796 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field2),
11797 membertype2))
11799 tree pos = byte_position (field1);
11800 if (TREE_CODE (arg1) == INTEGER_CST
11801 && tree_int_cst_equal (arg1, pos))
11803 if (TREE_CODE (arg2) == INTEGER_CST)
11804 return boolean_true_node;
11805 return pos;
11807 else if (TREE_CODE (arg1) != INTEGER_CST)
11808 ret = pos;
11810 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field1))
11811 && ANON_AGGR_TYPE_P (TREE_TYPE (field2)))
11813 if ((!lookup_attribute ("no_unique_address",
11814 DECL_ATTRIBUTES (field1)))
11815 != !lookup_attribute ("no_unique_address",
11816 DECL_ATTRIBUTES (field2)))
11817 break;
11818 if (!tree_int_cst_equal (bit_position (field1),
11819 bit_position (field2)))
11820 break;
11821 bool overlap = true;
11822 tree pos = byte_position (field1);
11823 if (TREE_CODE (arg1) == INTEGER_CST)
11825 tree off1 = fold_convert (sizetype, arg1);
11826 tree sz1 = TYPE_SIZE_UNIT (TREE_TYPE (field1));
11827 if (tree_int_cst_lt (off1, pos)
11828 || tree_int_cst_le (size_binop (PLUS_EXPR, pos, sz1), off1))
11829 overlap = false;
11831 if (TREE_CODE (arg2) == INTEGER_CST)
11833 tree off2 = fold_convert (sizetype, arg2);
11834 tree sz2 = TYPE_SIZE_UNIT (TREE_TYPE (field2));
11835 if (tree_int_cst_lt (off2, pos)
11836 || tree_int_cst_le (size_binop (PLUS_EXPR, pos, sz2), off2))
11837 overlap = false;
11839 if (overlap
11840 && NON_UNION_CLASS_TYPE_P (TREE_TYPE (field1))
11841 && NON_UNION_CLASS_TYPE_P (TREE_TYPE (field2)))
11843 tree narg1 = arg1;
11844 if (TREE_CODE (arg1) == INTEGER_CST)
11845 narg1 = size_binop (MINUS_EXPR,
11846 fold_convert (sizetype, arg1), pos);
11847 tree narg2 = arg2;
11848 if (TREE_CODE (arg2) == INTEGER_CST)
11849 narg2 = size_binop (MINUS_EXPR,
11850 fold_convert (sizetype, arg2), pos);
11851 tree t1 = TREE_TYPE (field1);
11852 tree t2 = TREE_TYPE (field2);
11853 tree nret = is_corresponding_member_aggr (loc, t1, membertype1,
11854 narg1, t2, membertype2,
11855 narg2);
11856 if (nret != boolean_false_node)
11858 if (nret == boolean_true_node)
11859 return nret;
11860 if (TREE_CODE (arg1) == INTEGER_CST)
11861 return size_binop (PLUS_EXPR, nret, pos);
11862 ret = size_binop (PLUS_EXPR, nret, pos);
11865 else if (overlap
11866 && TREE_CODE (TREE_TYPE (field1)) == UNION_TYPE
11867 && TREE_CODE (TREE_TYPE (field2)) == UNION_TYPE)
11869 tree narg1 = arg1;
11870 if (TREE_CODE (arg1) == INTEGER_CST)
11871 narg1 = size_binop (MINUS_EXPR,
11872 fold_convert (sizetype, arg1), pos);
11873 tree narg2 = arg2;
11874 if (TREE_CODE (arg2) == INTEGER_CST)
11875 narg2 = size_binop (MINUS_EXPR,
11876 fold_convert (sizetype, arg2), pos);
11877 if (is_corresponding_member_union (TREE_TYPE (field1),
11878 membertype1, narg1)
11879 && is_corresponding_member_union (TREE_TYPE (field2),
11880 membertype2, narg2))
11882 sorry_at (loc, "%<__builtin_is_corresponding_member%> "
11883 "not well defined for anonymous unions");
11884 return boolean_false_node;
11888 if (!r)
11889 break;
11890 field1 = DECL_CHAIN (field1);
11891 field2 = DECL_CHAIN (field2);
11893 return ret;
11896 /* Fold __builtin_is_corresponding_member call. */
11898 tree
11899 fold_builtin_is_corresponding_member (location_t loc, int nargs,
11900 tree *args)
11902 /* Unless users call the builtin directly, the following 3 checks should be
11903 ensured from std::is_corresponding_member function template. */
11904 if (nargs != 2)
11906 error_at (loc, "%<__builtin_is_corresponding_member%> "
11907 "needs two arguments");
11908 return boolean_false_node;
11910 tree arg1 = args[0];
11911 tree arg2 = args[1];
11912 if (error_operand_p (arg1) || error_operand_p (arg2))
11913 return boolean_false_node;
11914 if (!TYPE_PTRMEM_P (TREE_TYPE (arg1))
11915 || !TYPE_PTRMEM_P (TREE_TYPE (arg2)))
11917 error_at (loc, "%<__builtin_is_corresponding_member%> "
11918 "argument is not pointer to member");
11919 return boolean_false_node;
11922 if (!TYPE_PTRDATAMEM_P (TREE_TYPE (arg1))
11923 || !TYPE_PTRDATAMEM_P (TREE_TYPE (arg2)))
11924 return boolean_false_node;
11926 tree membertype1 = TREE_TYPE (TREE_TYPE (arg1));
11927 tree basetype1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg1));
11928 if (!complete_type_or_else (basetype1, NULL_TREE))
11929 return boolean_false_node;
11931 tree membertype2 = TREE_TYPE (TREE_TYPE (arg2));
11932 tree basetype2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg2));
11933 if (!complete_type_or_else (basetype2, NULL_TREE))
11934 return boolean_false_node;
11936 if (!NON_UNION_CLASS_TYPE_P (basetype1)
11937 || !NON_UNION_CLASS_TYPE_P (basetype2)
11938 || !std_layout_type_p (basetype1)
11939 || !std_layout_type_p (basetype2))
11940 return boolean_false_node;
11942 /* If the member types aren't layout compatible, then they
11943 can't be corresponding members. */
11944 if (!layout_compatible_type_p (membertype1, membertype2))
11945 return boolean_false_node;
11947 if (TREE_CODE (arg1) == PTRMEM_CST)
11948 arg1 = cplus_expand_constant (arg1);
11949 if (TREE_CODE (arg2) == PTRMEM_CST)
11950 arg2 = cplus_expand_constant (arg2);
11952 if (null_member_pointer_value_p (arg1)
11953 || null_member_pointer_value_p (arg2))
11954 return boolean_false_node;
11956 if (TREE_CODE (arg1) == INTEGER_CST
11957 && TREE_CODE (arg2) == INTEGER_CST
11958 && !tree_int_cst_equal (arg1, arg2))
11959 return boolean_false_node;
11961 if (TREE_CODE (arg2) == INTEGER_CST
11962 && TREE_CODE (arg1) != INTEGER_CST)
11964 std::swap (arg1, arg2);
11965 std::swap (membertype1, membertype2);
11966 std::swap (basetype1, basetype2);
11969 tree ret = is_corresponding_member_aggr (loc, basetype1, membertype1, arg1,
11970 basetype2, membertype2, arg2);
11971 if (TREE_TYPE (ret) == boolean_type_node)
11972 return ret;
11973 /* If both arg1 and arg2 are INTEGER_CSTs, is_corresponding_member_aggr
11974 already returns boolean_{true,false}_node whether those particular
11975 members are corresponding members or not. Otherwise, if only
11976 one of them is INTEGER_CST (canonicalized to first being INTEGER_CST
11977 above), it returns boolean_false_node if it is certainly not a
11978 corresponding member and otherwise we need to do a runtime check that
11979 those two OFFSET_TYPE offsets are equal.
11980 If neither of the operands is INTEGER_CST, is_corresponding_member_aggr
11981 returns the largest offset at which the members would be corresponding
11982 members, so perform arg1 <= ret && arg1 == arg2 runtime check. */
11983 gcc_assert (TREE_CODE (arg2) != INTEGER_CST);
11984 if (TREE_CODE (arg1) == INTEGER_CST)
11985 return fold_build2 (EQ_EXPR, boolean_type_node, arg1,
11986 fold_convert (TREE_TYPE (arg1), arg2));
11987 ret = fold_build2 (LE_EXPR, boolean_type_node,
11988 fold_convert (pointer_sized_int_node, arg1),
11989 fold_convert (pointer_sized_int_node, ret));
11990 return fold_build2 (TRUTH_AND_EXPR, boolean_type_node, ret,
11991 fold_build2 (EQ_EXPR, boolean_type_node, arg1,
11992 fold_convert (TREE_TYPE (arg1), arg2)));
11995 /* Actually evaluates the trait. */
11997 static bool
11998 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
12000 enum tree_code type_code1;
12001 tree t;
12003 type_code1 = TREE_CODE (type1);
12005 switch (kind)
12007 case CPTK_HAS_NOTHROW_ASSIGN:
12008 type1 = strip_array_types (type1);
12009 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
12010 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
12011 || (CLASS_TYPE_P (type1)
12012 && classtype_has_nothrow_assign_or_copy_p (type1,
12013 true))));
12015 case CPTK_HAS_TRIVIAL_ASSIGN:
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 (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
12020 && (trivial_type_p (type1)
12021 || (CLASS_TYPE_P (type1)
12022 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
12024 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
12025 type1 = strip_array_types (type1);
12026 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
12027 || (CLASS_TYPE_P (type1)
12028 && (t = locate_ctor (type1))
12029 && maybe_instantiate_noexcept (t)
12030 && TYPE_NOTHROW_P (TREE_TYPE (t))));
12032 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
12033 type1 = strip_array_types (type1);
12034 return (trivial_type_p (type1)
12035 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
12037 case CPTK_HAS_NOTHROW_COPY:
12038 type1 = strip_array_types (type1);
12039 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
12040 || (CLASS_TYPE_P (type1)
12041 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
12043 case CPTK_HAS_TRIVIAL_COPY:
12044 /* ??? The standard seems to be missing the "or array of such a class
12045 type" wording for this trait. */
12046 type1 = strip_array_types (type1);
12047 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
12048 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
12050 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
12051 type1 = strip_array_types (type1);
12052 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
12053 || (CLASS_TYPE_P (type1)
12054 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
12056 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
12057 return type_has_virtual_destructor (type1);
12059 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
12060 return type_has_unique_obj_representations (type1);
12062 case CPTK_IS_ABSTRACT:
12063 return ABSTRACT_CLASS_TYPE_P (type1);
12065 case CPTK_IS_AGGREGATE:
12066 return CP_AGGREGATE_TYPE_P (type1);
12068 case CPTK_IS_BASE_OF:
12069 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
12070 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
12071 || DERIVED_FROM_P (type1, type2)));
12073 case CPTK_IS_CLASS:
12074 return NON_UNION_CLASS_TYPE_P (type1);
12076 case CPTK_IS_EMPTY:
12077 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
12079 case CPTK_IS_ENUM:
12080 return type_code1 == ENUMERAL_TYPE;
12082 case CPTK_IS_FINAL:
12083 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
12085 case CPTK_IS_LAYOUT_COMPATIBLE:
12086 return layout_compatible_type_p (type1, type2);
12088 case CPTK_IS_LITERAL_TYPE:
12089 return literal_type_p (type1);
12091 case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
12092 return pointer_interconvertible_base_of_p (type1, type2);
12094 case CPTK_IS_POD:
12095 return pod_type_p (type1);
12097 case CPTK_IS_POLYMORPHIC:
12098 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
12100 case CPTK_IS_SAME:
12101 return same_type_p (type1, type2);
12103 case CPTK_IS_STD_LAYOUT:
12104 return std_layout_type_p (type1);
12106 case CPTK_IS_TRIVIAL:
12107 return trivial_type_p (type1);
12109 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
12110 return is_trivially_xible (MODIFY_EXPR, type1, type2);
12112 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
12113 return is_trivially_xible (INIT_EXPR, type1, type2);
12115 case CPTK_IS_TRIVIALLY_COPYABLE:
12116 return trivially_copyable_p (type1);
12118 case CPTK_IS_UNION:
12119 return type_code1 == UNION_TYPE;
12121 case CPTK_IS_ASSIGNABLE:
12122 return is_xible (MODIFY_EXPR, type1, type2);
12124 case CPTK_IS_CONSTRUCTIBLE:
12125 return is_xible (INIT_EXPR, type1, type2);
12127 case CPTK_IS_NOTHROW_ASSIGNABLE:
12128 return is_nothrow_xible (MODIFY_EXPR, type1, type2);
12130 case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
12131 return is_nothrow_xible (INIT_EXPR, type1, type2);
12133 case CPTK_IS_CONVERTIBLE:
12134 return is_convertible (type1, type2);
12136 case CPTK_IS_NOTHROW_CONVERTIBLE:
12137 return is_nothrow_convertible (type1, type2);
12139 case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
12140 return ref_xes_from_temporary (type1, type2, /*direct_init=*/true);
12142 case CPTK_REF_CONVERTS_FROM_TEMPORARY:
12143 return ref_xes_from_temporary (type1, type2, /*direct_init=*/false);
12145 case CPTK_IS_DEDUCIBLE:
12146 return type_targs_deducible_from (type1, type2);
12148 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
12149 case CPTK_##CODE:
12150 #include "cp-trait.def"
12151 #undef DEFTRAIT_TYPE
12152 /* Type-yielding traits are handled in finish_trait_type. */
12153 break;
12156 gcc_unreachable ();
12159 /* Returns true if TYPE meets the requirements for the specified KIND,
12160 false otherwise.
12162 When KIND == 1, TYPE must be an array of unknown bound,
12163 or (possibly cv-qualified) void, or a complete type.
12165 When KIND == 2, TYPE must be a complete type, or array of complete type,
12166 or (possibly cv-qualified) void.
12168 When KIND == 3:
12169 If TYPE is a non-union class type, it must be complete.
12171 When KIND == 4:
12172 If TYPE is a class type, it must be complete. */
12174 static bool
12175 check_trait_type (tree type, int kind = 1)
12177 if (type == NULL_TREE)
12178 return true;
12180 if (TREE_CODE (type) == TREE_VEC)
12182 for (tree arg : tree_vec_range (type))
12183 if (!check_trait_type (arg, kind))
12184 return false;
12185 return true;
12188 if (kind == 1 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
12189 return true; // Array of unknown bound. Don't care about completeness.
12191 if (kind == 3 && !NON_UNION_CLASS_TYPE_P (type))
12192 return true; // Not a non-union class type. Don't care about completeness.
12194 if (kind == 4 && TREE_CODE (type) == ARRAY_TYPE)
12195 return true; // Not a class type. Don't care about completeness.
12197 if (VOID_TYPE_P (type))
12198 return true;
12200 type = complete_type (strip_array_types (type));
12201 if (!COMPLETE_TYPE_P (type)
12202 && cxx_incomplete_type_diagnostic (NULL_TREE, type, DK_PERMERROR)
12203 && !flag_permissive)
12204 return false;
12205 return true;
12208 /* Process a trait expression. */
12210 tree
12211 finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
12213 if (type1 == error_mark_node
12214 || type2 == error_mark_node)
12215 return error_mark_node;
12217 if (processing_template_decl)
12219 tree trait_expr = make_node (TRAIT_EXPR);
12220 TREE_TYPE (trait_expr) = boolean_type_node;
12221 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
12222 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
12223 TRAIT_EXPR_KIND (trait_expr) = kind;
12224 TRAIT_EXPR_LOCATION (trait_expr) = loc;
12225 return trait_expr;
12228 switch (kind)
12230 case CPTK_HAS_NOTHROW_ASSIGN:
12231 case CPTK_HAS_TRIVIAL_ASSIGN:
12232 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
12233 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
12234 case CPTK_HAS_NOTHROW_COPY:
12235 case CPTK_HAS_TRIVIAL_COPY:
12236 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
12237 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
12238 if (!check_trait_type (type1))
12239 return error_mark_node;
12240 break;
12242 case CPTK_IS_LITERAL_TYPE:
12243 case CPTK_IS_POD:
12244 case CPTK_IS_STD_LAYOUT:
12245 case CPTK_IS_TRIVIAL:
12246 case CPTK_IS_TRIVIALLY_COPYABLE:
12247 if (!check_trait_type (type1, /* kind = */ 2))
12248 return error_mark_node;
12249 break;
12251 case CPTK_IS_EMPTY:
12252 case CPTK_IS_POLYMORPHIC:
12253 case CPTK_IS_ABSTRACT:
12254 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
12255 if (!check_trait_type (type1, /* kind = */ 3))
12256 return error_mark_node;
12257 break;
12259 /* N.B. std::is_aggregate is kind=2 but we don't need a complete element
12260 type to know whether an array is an aggregate, so use kind=4 here. */
12261 case CPTK_IS_AGGREGATE:
12262 case CPTK_IS_FINAL:
12263 if (!check_trait_type (type1, /* kind = */ 4))
12264 return error_mark_node;
12265 break;
12267 case CPTK_IS_ASSIGNABLE:
12268 case CPTK_IS_CONSTRUCTIBLE:
12269 if (!check_trait_type (type1))
12270 return error_mark_node;
12271 break;
12273 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
12274 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
12275 case CPTK_IS_NOTHROW_ASSIGNABLE:
12276 case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
12277 case CPTK_IS_CONVERTIBLE:
12278 case CPTK_IS_NOTHROW_CONVERTIBLE:
12279 case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
12280 case CPTK_REF_CONVERTS_FROM_TEMPORARY:
12281 if (!check_trait_type (type1)
12282 || !check_trait_type (type2))
12283 return error_mark_node;
12284 break;
12286 case CPTK_IS_BASE_OF:
12287 case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
12288 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
12289 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
12290 && !complete_type_or_else (type2, NULL_TREE))
12291 /* We already issued an error. */
12292 return error_mark_node;
12293 break;
12295 case CPTK_IS_CLASS:
12296 case CPTK_IS_ENUM:
12297 case CPTK_IS_UNION:
12298 case CPTK_IS_SAME:
12299 break;
12301 case CPTK_IS_LAYOUT_COMPATIBLE:
12302 if (!array_of_unknown_bound_p (type1)
12303 && TREE_CODE (type1) != VOID_TYPE
12304 && !complete_type_or_else (type1, NULL_TREE))
12305 /* We already issued an error. */
12306 return error_mark_node;
12307 if (!array_of_unknown_bound_p (type2)
12308 && TREE_CODE (type2) != VOID_TYPE
12309 && !complete_type_or_else (type2, NULL_TREE))
12310 /* We already issued an error. */
12311 return error_mark_node;
12312 break;
12314 case CPTK_IS_DEDUCIBLE:
12315 if (!DECL_TYPE_TEMPLATE_P (type1))
12317 error ("%qD is not a class or alias template", type1);
12318 return error_mark_node;
12320 break;
12322 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
12323 case CPTK_##CODE:
12324 #include "cp-trait.def"
12325 #undef DEFTRAIT_TYPE
12326 /* Type-yielding traits are handled in finish_trait_type. */
12327 gcc_unreachable ();
12330 tree val = (trait_expr_value (kind, type1, type2)
12331 ? boolean_true_node : boolean_false_node);
12332 return maybe_wrap_with_location (val, loc);
12335 /* Process a trait type. */
12337 tree
12338 finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
12339 tsubst_flags_t complain)
12341 if (type1 == error_mark_node
12342 || type2 == error_mark_node)
12343 return error_mark_node;
12345 if (processing_template_decl)
12347 tree type = cxx_make_type (TRAIT_TYPE);
12348 TRAIT_TYPE_TYPE1 (type) = type1;
12349 TRAIT_TYPE_TYPE2 (type) = type2;
12350 TRAIT_TYPE_KIND_RAW (type) = build_int_cstu (integer_type_node, kind);
12351 /* These traits are intended to be used in the definition of the ::type
12352 member of the corresponding standard library type trait and aren't
12353 mangleable (and thus won't appear directly in template signatures),
12354 so structural equality should suffice. */
12355 SET_TYPE_STRUCTURAL_EQUALITY (type);
12356 return type;
12359 switch (kind)
12361 case CPTK_UNDERLYING_TYPE:
12362 return finish_underlying_type (type1);
12364 case CPTK_REMOVE_CV:
12365 return cv_unqualified (type1);
12367 case CPTK_REMOVE_REFERENCE:
12368 if (TYPE_REF_P (type1))
12369 type1 = TREE_TYPE (type1);
12370 return type1;
12372 case CPTK_REMOVE_CVREF:
12373 if (TYPE_REF_P (type1))
12374 type1 = TREE_TYPE (type1);
12375 return cv_unqualified (type1);
12377 case CPTK_TYPE_PACK_ELEMENT:
12378 return finish_type_pack_element (type1, type2, complain);
12380 #define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
12381 case CPTK_##CODE:
12382 #include "cp-trait.def"
12383 #undef DEFTRAIT_EXPR
12384 /* Expression-yielding traits are handled in finish_trait_expr. */
12385 case CPTK_BASES:
12386 case CPTK_DIRECT_BASES:
12387 /* BASES and DIRECT_BASES are handled in finish_bases. */
12388 break;
12391 gcc_unreachable ();
12394 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
12395 which is ignored for C++. */
12397 void
12398 set_float_const_decimal64 (void)
12402 void
12403 clear_float_const_decimal64 (void)
12407 bool
12408 float_const_decimal64_p (void)
12410 return 0;
12414 /* Return true if T designates the implied `this' parameter. */
12416 bool
12417 is_this_parameter (tree t)
12419 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
12420 return false;
12421 gcc_assert (TREE_CODE (t) == PARM_DECL
12422 || (VAR_P (t) && DECL_HAS_VALUE_EXPR_P (t))
12423 || (cp_binding_oracle && VAR_P (t)));
12424 return true;
12427 /* Insert the deduced return type for an auto function. */
12429 void
12430 apply_deduced_return_type (tree fco, tree return_type)
12432 tree result;
12434 if (return_type == error_mark_node)
12435 return;
12437 if (DECL_CONV_FN_P (fco))
12438 DECL_NAME (fco) = make_conv_op_name (return_type);
12440 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
12442 maybe_update_postconditions (fco);
12444 /* Apply the type to the result object. */
12446 result = DECL_RESULT (fco);
12447 if (result == NULL_TREE)
12448 return;
12449 if (TREE_TYPE (result) == return_type)
12450 return;
12452 if (!processing_template_decl && !VOID_TYPE_P (return_type)
12453 && !complete_type_or_else (return_type, NULL_TREE))
12454 return;
12456 /* We already have a DECL_RESULT from start_preparsed_function.
12457 Now we need to redo the work it and allocate_struct_function
12458 did to reflect the new type. */
12459 result = build_decl (DECL_SOURCE_LOCATION (result), RESULT_DECL, NULL_TREE,
12460 TYPE_MAIN_VARIANT (return_type));
12461 DECL_ARTIFICIAL (result) = 1;
12462 DECL_IGNORED_P (result) = 1;
12463 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
12464 result);
12465 DECL_RESULT (fco) = result;
12467 if (!processing_template_decl)
12468 if (function *fun = DECL_STRUCT_FUNCTION (fco))
12470 bool aggr = aggregate_value_p (result, fco);
12471 #ifdef PCC_STATIC_STRUCT_RETURN
12472 fun->returns_pcc_struct = aggr;
12473 #endif
12474 fun->returns_struct = aggr;
12478 /* DECL is a local variable or parameter from the surrounding scope of a
12479 lambda-expression. Returns the decltype for a use of the capture field
12480 for DECL even if it hasn't been captured yet. */
12482 static tree
12483 capture_decltype (tree decl)
12485 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
12486 tree cap = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
12487 LOOK_want::HIDDEN_LAMBDA);
12488 tree type;
12490 if (cap && is_capture_proxy (cap))
12491 type = TREE_TYPE (cap);
12492 else
12493 switch (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam))
12495 case CPLD_NONE:
12496 error ("%qD is not captured", decl);
12497 return error_mark_node;
12499 case CPLD_COPY:
12500 type = TREE_TYPE (decl);
12501 if (TYPE_REF_P (type)
12502 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
12503 type = TREE_TYPE (type);
12504 break;
12506 case CPLD_REFERENCE:
12507 type = TREE_TYPE (decl);
12508 if (!TYPE_REF_P (type))
12509 type = build_reference_type (TREE_TYPE (decl));
12510 break;
12512 default:
12513 gcc_unreachable ();
12516 if (!TYPE_REF_P (type))
12518 if (!LAMBDA_EXPR_MUTABLE_P (lam))
12519 type = cp_build_qualified_type (type, (cp_type_quals (type)
12520 |TYPE_QUAL_CONST));
12521 type = build_reference_type (type);
12523 return type;
12526 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
12527 this is a right unary fold. Otherwise it is a left unary fold. */
12529 static tree
12530 finish_unary_fold_expr (tree expr, int op, tree_code dir)
12532 /* Build a pack expansion (assuming expr has pack type). */
12533 if (!uses_parameter_packs (expr))
12535 error_at (location_of (expr), "operand of fold expression has no "
12536 "unexpanded parameter packs");
12537 return error_mark_node;
12539 tree pack = make_pack_expansion (expr);
12541 /* Build the fold expression. */
12542 tree code = build_int_cstu (integer_type_node, abs (op));
12543 tree fold = build_min_nt_loc (input_location, dir, code, pack);
12544 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
12545 TREE_TYPE (fold) = build_dependent_operator_type (NULL_TREE,
12546 FOLD_EXPR_OP (fold),
12547 FOLD_EXPR_MODIFY_P (fold));
12548 return fold;
12551 tree
12552 finish_left_unary_fold_expr (tree expr, int op)
12554 return finish_unary_fold_expr (expr, op, UNARY_LEFT_FOLD_EXPR);
12557 tree
12558 finish_right_unary_fold_expr (tree expr, int op)
12560 return finish_unary_fold_expr (expr, op, UNARY_RIGHT_FOLD_EXPR);
12563 /* Build a binary fold expression over EXPR1 and EXPR2. The
12564 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
12565 has an unexpanded parameter pack). */
12567 tree
12568 finish_binary_fold_expr (tree pack, tree init, int op, tree_code dir)
12570 pack = make_pack_expansion (pack);
12571 tree code = build_int_cstu (integer_type_node, abs (op));
12572 tree fold = build_min_nt_loc (input_location, dir, code, pack, init);
12573 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
12574 TREE_TYPE (fold) = build_dependent_operator_type (NULL_TREE,
12575 FOLD_EXPR_OP (fold),
12576 FOLD_EXPR_MODIFY_P (fold));
12577 return fold;
12580 tree
12581 finish_binary_fold_expr (tree expr1, tree expr2, int op)
12583 // Determine which expr has an unexpanded parameter pack and
12584 // set the pack and initial term.
12585 bool pack1 = uses_parameter_packs (expr1);
12586 bool pack2 = uses_parameter_packs (expr2);
12587 if (pack1 && !pack2)
12588 return finish_binary_fold_expr (expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
12589 else if (pack2 && !pack1)
12590 return finish_binary_fold_expr (expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
12591 else
12593 if (pack1)
12594 error ("both arguments in binary fold have unexpanded parameter packs");
12595 else
12596 error ("no unexpanded parameter packs in binary fold");
12598 return error_mark_node;
12601 /* Finish __builtin_launder (arg). */
12603 tree
12604 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
12606 tree orig_arg = arg;
12607 if (!type_dependent_expression_p (arg))
12608 arg = decay_conversion (arg, complain);
12609 if (error_operand_p (arg))
12610 return error_mark_node;
12611 if (!type_dependent_expression_p (arg)
12612 && !TYPE_PTR_P (TREE_TYPE (arg)))
12614 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
12615 return error_mark_node;
12617 if (processing_template_decl)
12618 arg = orig_arg;
12619 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
12620 TREE_TYPE (arg), 1, arg);
12623 /* Finish __builtin_convertvector (arg, type). */
12625 tree
12626 cp_build_vec_convert (tree arg, location_t loc, tree type,
12627 tsubst_flags_t complain)
12629 if (error_operand_p (type))
12630 return error_mark_node;
12631 if (error_operand_p (arg))
12632 return error_mark_node;
12634 tree ret = NULL_TREE;
12635 if (!type_dependent_expression_p (arg) && !dependent_type_p (type))
12636 ret = c_build_vec_convert (cp_expr_loc_or_input_loc (arg),
12637 decay_conversion (arg, complain),
12638 loc, type, (complain & tf_error) != 0);
12640 if (!processing_template_decl)
12641 return ret;
12643 return build_call_expr_internal_loc (loc, IFN_VEC_CONVERT, type, 1, arg);
12646 /* Finish __builtin_bit_cast (type, arg). */
12648 tree
12649 cp_build_bit_cast (location_t loc, tree type, tree arg,
12650 tsubst_flags_t complain)
12652 if (error_operand_p (type))
12653 return error_mark_node;
12654 if (!dependent_type_p (type))
12656 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
12657 return error_mark_node;
12658 if (TREE_CODE (type) == ARRAY_TYPE)
12660 /* std::bit_cast for destination ARRAY_TYPE is not possible,
12661 as functions may not return an array, so don't bother trying
12662 to support this (and then deal with VLAs etc.). */
12663 error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
12664 "is an array type", type);
12665 return error_mark_node;
12667 if (!trivially_copyable_p (type))
12669 error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
12670 "is not trivially copyable", type);
12671 return error_mark_node;
12675 if (error_operand_p (arg))
12676 return error_mark_node;
12678 if (!type_dependent_expression_p (arg))
12680 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
12682 /* Don't perform array-to-pointer conversion. */
12683 arg = mark_rvalue_use (arg, loc, true);
12684 if (!complete_type_or_maybe_complain (TREE_TYPE (arg), arg, complain))
12685 return error_mark_node;
12687 else
12688 arg = decay_conversion (arg, complain);
12690 if (error_operand_p (arg))
12691 return error_mark_node;
12693 if (!trivially_copyable_p (TREE_TYPE (arg)))
12695 error_at (cp_expr_loc_or_loc (arg, loc),
12696 "%<__builtin_bit_cast%> source type %qT "
12697 "is not trivially copyable", TREE_TYPE (arg));
12698 return error_mark_node;
12700 if (!dependent_type_p (type)
12701 && !cp_tree_equal (TYPE_SIZE_UNIT (type),
12702 TYPE_SIZE_UNIT (TREE_TYPE (arg))))
12704 error_at (loc, "%<__builtin_bit_cast%> source size %qE "
12705 "not equal to destination type size %qE",
12706 TYPE_SIZE_UNIT (TREE_TYPE (arg)),
12707 TYPE_SIZE_UNIT (type));
12708 return error_mark_node;
12712 tree ret = build_min (BIT_CAST_EXPR, type, arg);
12713 SET_EXPR_LOCATION (ret, loc);
12715 if (!processing_template_decl && CLASS_TYPE_P (type))
12716 ret = get_target_expr (ret, complain);
12718 return ret;
12721 #include "gt-cp-semantics.h"