c++: P0847R7 (deducing this) - xobj lambdas. [PR102609]
[official-gcc.git] / gcc / cp / semantics.cc
blob86e1adf1e71d15c38e291ee901ca5eabf0be208d
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-2024 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 *);
57 /* Used for OpenMP non-static data member privatization. */
59 static hash_map<tree, tree> *omp_private_member_map;
60 static vec<tree> omp_private_member_vec;
61 static bool omp_private_member_ignore_next;
64 /* Deferred Access Checking Overview
65 ---------------------------------
67 Most C++ expressions and declarations require access checking
68 to be performed during parsing. However, in several cases,
69 this has to be treated differently.
71 For member declarations, access checking has to be deferred
72 until more information about the declaration is known. For
73 example:
75 class A {
76 typedef int X;
77 public:
78 X f();
81 A::X A::f();
82 A::X g();
84 When we are parsing the function return type `A::X', we don't
85 really know if this is allowed until we parse the function name.
87 Furthermore, some contexts require that access checking is
88 never performed at all. These include class heads, and template
89 instantiations.
91 Typical use of access checking functions is described here:
93 1. When we enter a context that requires certain access checking
94 mode, the function `push_deferring_access_checks' is called with
95 DEFERRING argument specifying the desired mode. Access checking
96 may be performed immediately (dk_no_deferred), deferred
97 (dk_deferred), or not performed (dk_no_check).
99 2. When a declaration such as a type, or a variable, is encountered,
100 the function `perform_or_defer_access_check' is called. It
101 maintains a vector of all deferred checks.
103 3. The global `current_class_type' or `current_function_decl' is then
104 setup by the parser. `enforce_access' relies on these information
105 to check access.
107 4. Upon exiting the context mentioned in step 1,
108 `perform_deferred_access_checks' is called to check all declaration
109 stored in the vector. `pop_deferring_access_checks' is then
110 called to restore the previous access checking mode.
112 In case of parsing error, we simply call `pop_deferring_access_checks'
113 without `perform_deferred_access_checks'. */
115 struct GTY(()) deferred_access {
116 /* A vector representing name-lookups for which we have deferred
117 checking access controls. We cannot check the accessibility of
118 names used in a decl-specifier-seq until we know what is being
119 declared because code like:
121 class A {
122 class B {};
123 B* f();
126 A::B* A::f() { return 0; }
128 is valid, even though `A::B' is not generally accessible. */
129 vec<deferred_access_check, va_gc> *deferred_access_checks;
131 /* The current mode of access checks. */
132 enum deferring_kind deferring_access_checks_kind;
135 /* Data for deferred access checking. */
136 static GTY(()) vec<deferred_access, va_gc> *deferred_access_stack;
137 static GTY(()) unsigned deferred_access_no_check;
139 /* Save the current deferred access states and start deferred
140 access checking iff DEFER_P is true. */
142 void
143 push_deferring_access_checks (deferring_kind deferring)
145 /* For context like template instantiation, access checking
146 disabling applies to all nested context. */
147 if (deferred_access_no_check || deferring == dk_no_check)
148 deferred_access_no_check++;
149 else
151 deferred_access e = {NULL, deferring};
152 vec_safe_push (deferred_access_stack, e);
156 /* Save the current deferred access states and start deferred access
157 checking, continuing the set of deferred checks in CHECKS. */
159 void
160 reopen_deferring_access_checks (vec<deferred_access_check, va_gc> * checks)
162 push_deferring_access_checks (dk_deferred);
163 if (!deferred_access_no_check)
164 deferred_access_stack->last().deferred_access_checks = checks;
167 /* Resume deferring access checks again after we stopped doing
168 this previously. */
170 void
171 resume_deferring_access_checks (void)
173 if (!deferred_access_no_check)
174 deferred_access_stack->last().deferring_access_checks_kind = dk_deferred;
177 /* Stop deferring access checks. */
179 void
180 stop_deferring_access_checks (void)
182 if (!deferred_access_no_check)
183 deferred_access_stack->last().deferring_access_checks_kind = dk_no_deferred;
186 /* Discard the current deferred access checks and restore the
187 previous states. */
189 void
190 pop_deferring_access_checks (void)
192 if (deferred_access_no_check)
193 deferred_access_no_check--;
194 else
195 deferred_access_stack->pop ();
198 /* Returns a TREE_LIST representing the deferred checks.
199 The TREE_PURPOSE of each node is the type through which the
200 access occurred; the TREE_VALUE is the declaration named.
203 vec<deferred_access_check, va_gc> *
204 get_deferred_access_checks (void)
206 if (deferred_access_no_check)
207 return NULL;
208 else
209 return (deferred_access_stack->last().deferred_access_checks);
212 /* Take current deferred checks and combine with the
213 previous states if we also defer checks previously.
214 Otherwise perform checks now. */
216 void
217 pop_to_parent_deferring_access_checks (void)
219 if (deferred_access_no_check)
220 deferred_access_no_check--;
221 else
223 vec<deferred_access_check, va_gc> *checks;
224 deferred_access *ptr;
226 checks = (deferred_access_stack->last ().deferred_access_checks);
228 deferred_access_stack->pop ();
229 ptr = &deferred_access_stack->last ();
230 if (ptr->deferring_access_checks_kind == dk_no_deferred)
232 /* Check access. */
233 perform_access_checks (checks, tf_warning_or_error);
235 else
237 /* Merge with parent. */
238 int i, j;
239 deferred_access_check *chk, *probe;
241 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
243 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, j, probe)
245 if (probe->binfo == chk->binfo &&
246 probe->decl == chk->decl &&
247 probe->diag_decl == chk->diag_decl)
248 goto found;
250 /* Insert into parent's checks. */
251 vec_safe_push (ptr->deferred_access_checks, *chk);
252 found:;
258 /* Called from enforce_access. A class has attempted (but failed) to access
259 DECL. It is already established that a baseclass of that class,
260 PARENT_BINFO, has private access to DECL. Examine certain special cases
261 to find a decl that accurately describes the source of the problem. If
262 none of the special cases apply, simply return DECL as the source of the
263 problem. */
265 static tree
266 get_class_access_diagnostic_decl (tree parent_binfo, tree decl)
268 /* When a class is denied access to a decl in a baseclass, most of the
269 time it is because the decl itself was declared as private at the point
270 of declaration.
272 However, in C++, there are (at least) two situations in which a decl
273 can be private even though it was not originally defined as such.
274 These two situations only apply if a baseclass had private access to
275 DECL (this function is only called if that is the case). */
277 /* We should first check whether the reason the parent had private access
278 to DECL was simply because DECL was created and declared as private in
279 the parent. If it was, then DECL is definitively the source of the
280 problem. */
281 if (SAME_BINFO_TYPE_P (context_for_name_lookup (decl),
282 BINFO_TYPE (parent_binfo)))
283 return decl;
285 /* 1. If the "using" keyword is used to inherit DECL within the parent,
286 this may cause DECL to be private, so we should return the using
287 statement as the source of the problem.
289 Scan the fields of PARENT_BINFO and see if there are any using decls. If
290 there are, see if they inherit DECL. If they do, that's where DECL must
291 have been declared private. */
293 for (tree parent_field = TYPE_FIELDS (BINFO_TYPE (parent_binfo));
294 parent_field;
295 parent_field = DECL_CHAIN (parent_field))
296 /* Not necessary, but also check TREE_PRIVATE for the sake of
297 eliminating obviously non-relevant using decls. */
298 if (TREE_CODE (parent_field) == USING_DECL
299 && TREE_PRIVATE (parent_field))
301 tree decl_stripped = strip_using_decl (parent_field);
303 /* The using statement might be overloaded. If so, we need to
304 check all of the overloads. */
305 for (ovl_iterator iter (decl_stripped); iter; ++iter)
306 /* If equal, the using statement inherits DECL, and so is the
307 source of the access failure, so return it. */
308 if (*iter == decl)
309 return parent_field;
312 /* 2. If DECL was privately inherited by the parent class, then DECL will
313 be inaccessible, even though it may originally have been accessible to
314 deriving classes. In that case, the fault lies with the parent, since it
315 used a private inheritance, so we return the parent as the source of the
316 problem.
318 Since this is the last check, we just assume it's true. At worst, it
319 will simply point to the class that failed to give access, which is
320 technically true. */
321 return TYPE_NAME (BINFO_TYPE (parent_binfo));
324 /* If the current scope isn't allowed to access DECL along
325 BASETYPE_PATH, give an error, or if we're parsing a function or class
326 template, defer the access check to be performed at instantiation time.
327 The most derived class in BASETYPE_PATH is the one used to qualify DECL.
328 DIAG_DECL is the declaration to use in the error diagnostic. */
330 static bool
331 enforce_access (tree basetype_path, tree decl, tree diag_decl,
332 tsubst_flags_t complain, access_failure_info *afi = NULL)
334 gcc_assert (TREE_CODE (basetype_path) == TREE_BINFO);
336 if (flag_new_inheriting_ctors
337 && DECL_INHERITED_CTOR (decl))
339 /* 7.3.3/18: The additional constructors are accessible if they would be
340 accessible when used to construct an object of the corresponding base
341 class. */
342 decl = strip_inheriting_ctors (decl);
343 basetype_path = lookup_base (basetype_path, DECL_CONTEXT (decl),
344 ba_any, NULL, complain);
347 tree cs = current_scope ();
348 if (in_template_context
349 && (CLASS_TYPE_P (cs) || TREE_CODE (cs) == FUNCTION_DECL))
350 if (tree template_info = get_template_info (cs))
352 /* When parsing a function or class template, we in general need to
353 defer access checks until template instantiation time, since a friend
354 declaration may grant access only to a particular specialization of
355 the template. */
357 if (accessible_p (basetype_path, decl, /*consider_local_p=*/true))
358 /* But if the member is deemed accessible at parse time, then we can
359 assume it'll be accessible at instantiation time. */
360 return true;
362 /* Access of a dependent decl should be rechecked after tsubst'ing
363 into the user of the decl, rather than explicitly deferring the
364 check here. */
365 gcc_assert (!uses_template_parms (decl));
366 if (TREE_CODE (decl) == FIELD_DECL)
367 gcc_assert (!uses_template_parms (DECL_CONTEXT (decl)));
369 /* Defer this access check until instantiation time. */
370 deferred_access_check access_check;
371 access_check.binfo = basetype_path;
372 access_check.decl = decl;
373 access_check.diag_decl = diag_decl;
374 access_check.loc = input_location;
375 vec_safe_push (TI_DEFERRED_ACCESS_CHECKS (template_info), access_check);
376 return true;
379 if (!accessible_p (basetype_path, decl, /*consider_local_p=*/true))
381 if (flag_new_inheriting_ctors)
382 diag_decl = strip_inheriting_ctors (diag_decl);
383 if (complain & tf_error)
385 access_kind access_failure_reason = ak_none;
387 /* By default, using the decl as the source of the problem will
388 usually give correct results. */
389 tree diag_location = diag_decl;
391 /* However, if a parent of BASETYPE_PATH had private access to decl,
392 then it actually might be the case that the source of the problem
393 is not DECL. */
394 tree parent_binfo = get_parent_with_private_access (decl,
395 basetype_path);
397 /* So if a parent did have private access, then we need to do
398 special checks to obtain the best diagnostic location decl. */
399 if (parent_binfo != NULL_TREE)
401 diag_location = get_class_access_diagnostic_decl (parent_binfo,
402 diag_decl);
404 /* We also at this point know that the reason access failed was
405 because decl was private. */
406 access_failure_reason = ak_private;
409 /* Finally, generate an error message. */
410 complain_about_access (decl, diag_decl, diag_location, true,
411 access_failure_reason);
413 if (afi)
414 afi->record_access_failure (basetype_path, decl, diag_decl);
415 return false;
418 return true;
421 /* Perform the access checks in CHECKS. The TREE_PURPOSE of each node
422 is the BINFO indicating the qualifying scope used to access the
423 DECL node stored in the TREE_VALUE of the node. If CHECKS is empty
424 or we aren't in SFINAE context or all the checks succeed return TRUE,
425 otherwise FALSE. */
427 bool
428 perform_access_checks (vec<deferred_access_check, va_gc> *checks,
429 tsubst_flags_t complain)
431 int i;
432 deferred_access_check *chk;
433 location_t loc = input_location;
434 bool ok = true;
436 if (!checks)
437 return true;
439 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
441 input_location = chk->loc;
442 ok &= enforce_access (chk->binfo, chk->decl, chk->diag_decl, complain);
445 input_location = loc;
446 return (complain & tf_error) ? true : ok;
449 /* Perform the deferred access checks.
451 After performing the checks, we still have to keep the list
452 `deferred_access_stack->deferred_access_checks' since we may want
453 to check access for them again later in a different context.
454 For example:
456 class A {
457 typedef int X;
458 static X a;
460 A::X A::a, x; // No error for `A::a', error for `x'
462 We have to perform deferred access of `A::X', first with `A::a',
463 next with `x'. Return value like perform_access_checks above. */
465 bool
466 perform_deferred_access_checks (tsubst_flags_t complain)
468 return perform_access_checks (get_deferred_access_checks (), complain);
471 /* Defer checking the accessibility of DECL, when looked up in
472 BINFO. DIAG_DECL is the declaration to use to print diagnostics.
473 Return value like perform_access_checks above.
474 If non-NULL, report failures to AFI. */
476 bool
477 perform_or_defer_access_check (tree binfo, tree decl, tree diag_decl,
478 tsubst_flags_t complain,
479 access_failure_info *afi)
481 int i;
482 deferred_access *ptr;
483 deferred_access_check *chk;
485 /* Exit if we are in a context that no access checking is performed. */
486 if (deferred_access_no_check)
487 return true;
489 gcc_assert (TREE_CODE (binfo) == TREE_BINFO);
491 ptr = &deferred_access_stack->last ();
493 /* If we are not supposed to defer access checks, just check now. */
494 if (ptr->deferring_access_checks_kind == dk_no_deferred)
496 bool ok = enforce_access (binfo, decl, diag_decl, complain, afi);
497 return (complain & tf_error) ? true : ok;
500 /* See if we are already going to perform this check. */
501 FOR_EACH_VEC_SAFE_ELT (ptr->deferred_access_checks, i, chk)
503 if (chk->decl == decl && chk->binfo == binfo &&
504 chk->diag_decl == diag_decl)
506 return true;
509 /* If not, record the check. */
510 deferred_access_check new_access = {binfo, decl, diag_decl, input_location};
511 vec_safe_push (ptr->deferred_access_checks, new_access);
513 return true;
516 /* Returns nonzero if the current statement is a full expression,
517 i.e. temporaries created during that statement should be destroyed
518 at the end of the statement. */
521 stmts_are_full_exprs_p (void)
523 return current_stmt_tree ()->stmts_are_full_exprs_p;
526 /* T is a statement. Add it to the statement-tree. This is the C++
527 version. The C/ObjC frontends have a slightly different version of
528 this function. */
530 tree
531 add_stmt (tree t)
533 enum tree_code code = TREE_CODE (t);
535 if (EXPR_P (t) && code != LABEL_EXPR)
537 if (!EXPR_HAS_LOCATION (t))
538 SET_EXPR_LOCATION (t, input_location);
540 /* When we expand a statement-tree, we must know whether or not the
541 statements are full-expressions. We record that fact here. */
542 if (STATEMENT_CODE_P (TREE_CODE (t)))
543 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
546 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
547 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
549 /* Add T to the statement-tree. Non-side-effect statements need to be
550 recorded during statement expressions. */
551 gcc_checking_assert (!stmt_list_stack->is_empty ());
552 append_to_statement_list_force (t, &cur_stmt_list);
554 return t;
557 /* Returns the stmt_tree to which statements are currently being added. */
559 stmt_tree
560 current_stmt_tree (void)
562 return (cfun
563 ? &cfun->language->base.x_stmt_tree
564 : &scope_chain->x_stmt_tree);
567 /* If statements are full expressions, wrap STMT in a CLEANUP_POINT_EXPR. */
569 static tree
570 maybe_cleanup_point_expr (tree expr)
572 if (!processing_template_decl && stmts_are_full_exprs_p ())
573 expr = fold_build_cleanup_point_expr (TREE_TYPE (expr), expr);
574 return expr;
577 /* Like maybe_cleanup_point_expr except have the type of the new expression be
578 void so we don't need to create a temporary variable to hold the inner
579 expression. The reason why we do this is because the original type might be
580 an aggregate and we cannot create a temporary variable for that type. */
582 tree
583 maybe_cleanup_point_expr_void (tree expr)
585 if (!processing_template_decl && stmts_are_full_exprs_p ())
586 expr = fold_build_cleanup_point_expr (void_type_node, expr);
587 return expr;
592 /* Create a declaration statement for the declaration given by the DECL. */
594 void
595 add_decl_expr (tree decl)
597 tree r = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
598 if (DECL_INITIAL (decl)
599 || (DECL_SIZE (decl) && TREE_SIDE_EFFECTS (DECL_SIZE (decl))))
600 r = maybe_cleanup_point_expr_void (r);
601 add_stmt (r);
604 /* Set EXPR_LOCATION of the cleanups of any CLEANUP_STMT in STMTS to LOC. */
606 static void
607 set_cleanup_locs (tree stmts, location_t loc)
609 if (TREE_CODE (stmts) == CLEANUP_STMT)
611 tree t = CLEANUP_EXPR (stmts);
612 if (t && TREE_CODE (t) != POSTCONDITION_STMT)
613 protected_set_expr_location (t, loc);
614 /* Avoid locus differences for C++ cdtor calls depending on whether
615 cdtor_returns_this: a conversion to void is added to discard the return
616 value, and this conversion ends up carrying the location, and when it
617 gets discarded, the location is lost. So hold it in the call as
618 well. */
619 if (TREE_CODE (t) == NOP_EXPR
620 && TREE_TYPE (t) == void_type_node
621 && TREE_CODE (TREE_OPERAND (t, 0)) == CALL_EXPR)
622 protected_set_expr_location (TREE_OPERAND (t, 0), loc);
623 set_cleanup_locs (CLEANUP_BODY (stmts), loc);
625 else if (TREE_CODE (stmts) == STATEMENT_LIST)
626 for (tree stmt : tsi_range (stmts))
627 set_cleanup_locs (stmt, loc);
630 /* True iff the innermost block scope is a try block. */
632 static bool
633 at_try_scope ()
635 cp_binding_level *b = current_binding_level;
636 while (b && b->kind == sk_cleanup)
637 b = b->level_chain;
638 return b && b->kind == sk_try;
641 /* Finish a scope. */
643 tree
644 do_poplevel (tree stmt_list)
646 tree block = NULL;
648 bool was_try = at_try_scope ();
650 if (stmts_are_full_exprs_p ())
651 block = poplevel (kept_level_p (), 1, 0);
653 /* This needs to come after poplevel merges sk_cleanup statement_lists. */
654 maybe_splice_retval_cleanup (stmt_list, was_try);
656 stmt_list = pop_stmt_list (stmt_list);
658 /* input_location is the last token of the scope, usually a }. */
659 set_cleanup_locs (stmt_list, input_location);
661 if (!processing_template_decl)
663 stmt_list = c_build_bind_expr (input_location, block, stmt_list);
664 /* ??? See c_end_compound_stmt re statement expressions. */
667 return stmt_list;
670 /* Begin a new scope. */
672 static tree
673 do_pushlevel (scope_kind sk)
675 tree ret = push_stmt_list ();
676 if (stmts_are_full_exprs_p ())
677 begin_scope (sk, NULL);
678 return ret;
681 /* Queue a cleanup. CLEANUP is an expression/statement to be executed
682 when the current scope is exited. EH_ONLY is true when this is not
683 meant to apply to normal control flow transfer. DECL is the VAR_DECL
684 being cleaned up, if any, or null for temporaries or subobjects. */
686 void
687 push_cleanup (tree decl, tree cleanup, bool eh_only)
689 tree stmt = build_stmt (input_location, CLEANUP_STMT, NULL, cleanup, decl);
690 CLEANUP_EH_ONLY (stmt) = eh_only;
691 add_stmt (stmt);
692 CLEANUP_BODY (stmt) = push_stmt_list ();
695 /* Simple infinite loop tracking for -Wreturn-type. We keep a stack of all
696 the current loops, represented by 'NULL_TREE' if we've seen a possible
697 exit, and 'error_mark_node' if not. This is currently used only to
698 suppress the warning about a function with no return statements, and
699 therefore we don't bother noting returns as possible exits. We also
700 don't bother with gotos. */
702 static void
703 begin_maybe_infinite_loop (tree cond)
705 /* Only track this while parsing a function, not during instantiation. */
706 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
707 && !processing_template_decl))
708 return;
709 bool maybe_infinite = true;
710 if (cond)
712 cond = fold_non_dependent_expr (cond);
713 maybe_infinite = integer_nonzerop (cond);
715 vec_safe_push (cp_function_chain->infinite_loops,
716 maybe_infinite ? error_mark_node : NULL_TREE);
720 /* A break is a possible exit for the current loop. */
722 void
723 break_maybe_infinite_loop (void)
725 if (!cfun)
726 return;
727 cp_function_chain->infinite_loops->last() = NULL_TREE;
730 /* If we reach the end of the loop without seeing a possible exit, we have
731 an infinite loop. */
733 static void
734 end_maybe_infinite_loop (tree cond)
736 if (!cfun || (DECL_TEMPLATE_INSTANTIATION (current_function_decl)
737 && !processing_template_decl))
738 return;
739 tree current = cp_function_chain->infinite_loops->pop();
740 if (current != NULL_TREE)
742 cond = fold_non_dependent_expr (cond);
743 if (integer_nonzerop (cond))
744 current_function_infinite_loop = 1;
748 /* Begin a conditional that might contain a declaration. When generating
749 normal code, we want the declaration to appear before the statement
750 containing the conditional. When generating template code, we want the
751 conditional to be rendered as the raw DECL_EXPR. */
753 static void
754 begin_cond (tree *cond_p)
756 if (processing_template_decl)
757 *cond_p = push_stmt_list ();
760 /* Finish such a conditional. */
762 static void
763 finish_cond (tree *cond_p, tree expr)
765 if (processing_template_decl)
767 tree cond = pop_stmt_list (*cond_p);
769 if (expr == NULL_TREE)
770 /* Empty condition in 'for'. */
771 gcc_assert (empty_expr_stmt_p (cond));
772 else if (check_for_bare_parameter_packs (expr))
773 expr = error_mark_node;
774 else if (!empty_expr_stmt_p (cond))
775 expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
777 *cond_p = expr;
780 /* If *COND_P specifies a conditional with a declaration, transform the
781 loop such that
782 while (A x = 42) { }
783 for (; A x = 42;) { }
784 becomes
785 while (true) { A x = 42; if (!x) break; }
786 for (;;) { A x = 42; if (!x) break; }
787 The statement list for BODY will be empty if the conditional did
788 not declare anything. */
790 static void
791 simplify_loop_decl_cond (tree *cond_p, tree body)
793 tree cond, if_stmt;
795 if (!TREE_SIDE_EFFECTS (body))
796 return;
798 cond = *cond_p;
799 *cond_p = boolean_true_node;
801 if_stmt = begin_if_stmt ();
802 cond = cp_build_unary_op (TRUTH_NOT_EXPR, cond, false, tf_warning_or_error);
803 finish_if_stmt_cond (cond, if_stmt);
804 finish_break_stmt ();
805 finish_then_clause (if_stmt);
806 finish_if_stmt (if_stmt);
809 /* Finish a goto-statement. */
811 tree
812 finish_goto_stmt (tree destination)
814 if (identifier_p (destination))
815 destination = lookup_label (destination);
817 /* We warn about unused labels with -Wunused. That means we have to
818 mark the used labels as used. */
819 if (TREE_CODE (destination) == LABEL_DECL)
820 TREE_USED (destination) = 1;
821 else
823 destination = mark_rvalue_use (destination);
824 if (!processing_template_decl)
826 destination = cp_convert (ptr_type_node, destination,
827 tf_warning_or_error);
828 if (error_operand_p (destination))
829 return NULL_TREE;
830 destination
831 = fold_build_cleanup_point_expr (TREE_TYPE (destination),
832 destination);
836 check_goto (destination);
838 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
839 return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
842 /* Returns true if T corresponds to an assignment operator expression. */
844 static bool
845 is_assignment_op_expr_p (tree t)
847 if (t == NULL_TREE)
848 return false;
850 if (TREE_CODE (t) == MODIFY_EXPR
851 || (TREE_CODE (t) == MODOP_EXPR
852 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR))
853 return true;
855 tree call = extract_call_expr (t);
856 if (call == NULL_TREE
857 || call == error_mark_node
858 || !CALL_EXPR_OPERATOR_SYNTAX (call))
859 return false;
861 tree fndecl = cp_get_callee_fndecl_nofold (call);
862 return fndecl != NULL_TREE
863 && DECL_ASSIGNMENT_OPERATOR_P (fndecl)
864 && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR);
867 /* Return true if TYPE is a class type that is convertible to
868 and assignable from bool. */
870 static GTY((deletable)) hash_map<tree, bool> *boolish_class_type_p_cache;
872 static bool
873 boolish_class_type_p (tree type)
875 type = TYPE_MAIN_VARIANT (type);
876 if (!CLASS_TYPE_P (type) || !COMPLETE_TYPE_P (type))
877 return false;
879 if (bool *r = hash_map_safe_get (boolish_class_type_p_cache, type))
880 return *r;
882 tree ops;
883 bool has_bool_assignment = false;
884 bool has_bool_conversion = false;
886 ops = lookup_fnfields (type, assign_op_identifier, /*protect=*/0, tf_none);
887 for (tree op : ovl_range (BASELINK_FUNCTIONS (ops)))
889 op = STRIP_TEMPLATE (op);
890 if (TREE_CODE (op) != FUNCTION_DECL)
891 continue;
892 tree parm = DECL_CHAIN (DECL_ARGUMENTS (op));
893 tree parm_type = non_reference (TREE_TYPE (parm));
894 if (TREE_CODE (parm_type) == BOOLEAN_TYPE)
896 has_bool_assignment = true;
897 break;
901 if (has_bool_assignment)
903 ops = lookup_conversions (type);
904 for (; ops; ops = TREE_CHAIN (ops))
906 tree op = TREE_VALUE (ops);
907 if (!DECL_NONCONVERTING_P (op)
908 && TREE_CODE (DECL_CONV_FN_TYPE (op)) == BOOLEAN_TYPE)
910 has_bool_conversion = true;
911 break;
916 bool boolish = has_bool_assignment && has_bool_conversion;
917 hash_map_safe_put<true> (boolish_class_type_p_cache, type, boolish);
918 return boolish;
922 /* Maybe warn about an unparenthesized 'a = b' (appearing in a
923 boolean context where 'a == b' might have been intended).
924 NESTED_P is true if T is the RHS of another assignment. */
926 void
927 maybe_warn_unparenthesized_assignment (tree t, bool nested_p,
928 tsubst_flags_t complain)
930 tree type = TREE_TYPE (t);
931 t = STRIP_REFERENCE_REF (t);
933 if ((complain & tf_warning)
934 && warn_parentheses
935 && is_assignment_op_expr_p (t)
936 /* A parenthesized expression would've had this warning
937 suppressed by finish_parenthesized_expr. */
938 && !warning_suppressed_p (t, OPT_Wparentheses)
939 /* In c = a = b, don't warn if a has type bool or bool-like class. */
940 && (!nested_p
941 || (TREE_CODE (type) != BOOLEAN_TYPE
942 && !boolish_class_type_p (type))))
944 warning_at (cp_expr_loc_or_input_loc (t), OPT_Wparentheses,
945 "suggest parentheses around assignment used as truth value");
946 suppress_warning (t, OPT_Wparentheses);
950 /* COND is the condition-expression for an if, while, etc.,
951 statement. Convert it to a boolean value, if appropriate.
952 In addition, verify sequence points if -Wsequence-point is enabled. */
954 static tree
955 maybe_convert_cond (tree cond)
957 /* Empty conditions remain empty. */
958 if (!cond)
959 return NULL_TREE;
961 /* Wait until we instantiate templates before doing conversion. */
962 if (type_dependent_expression_p (cond))
963 return cond;
965 if (warn_sequence_point && !processing_template_decl)
966 verify_sequence_points (cond);
968 maybe_warn_unparenthesized_assignment (cond, /*nested_p=*/false,
969 tf_warning_or_error);
971 /* Do the conversion. */
972 cond = convert_from_reference (cond);
973 return condition_conversion (cond);
976 /* Finish an expression-statement, whose EXPRESSION is as indicated. */
978 tree
979 finish_expr_stmt (tree expr)
981 tree r = NULL_TREE;
982 location_t loc = EXPR_LOCATION (expr);
984 if (expr != NULL_TREE)
986 /* If we ran into a problem, make sure we complained. */
987 gcc_assert (expr != error_mark_node || seen_error ());
989 if (!processing_template_decl)
991 if (warn_sequence_point)
992 verify_sequence_points (expr);
993 expr = convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
995 else if (!type_dependent_expression_p (expr))
996 convert_to_void (expr, ICV_STATEMENT, tf_warning_or_error);
998 if (check_for_bare_parameter_packs (expr))
999 expr = error_mark_node;
1001 /* Simplification of inner statement expressions, compound exprs,
1002 etc can result in us already having an EXPR_STMT. */
1003 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
1005 if (TREE_CODE (expr) != EXPR_STMT)
1006 expr = build_stmt (loc, EXPR_STMT, expr);
1007 expr = maybe_cleanup_point_expr_void (expr);
1010 r = add_stmt (expr);
1013 return r;
1017 /* Begin an if-statement. Returns a newly created IF_STMT if
1018 appropriate. */
1020 tree
1021 begin_if_stmt (void)
1023 tree r, scope;
1024 scope = do_pushlevel (sk_cond);
1025 r = build_stmt (input_location, IF_STMT, NULL_TREE,
1026 NULL_TREE, NULL_TREE, scope);
1027 current_binding_level->this_entity = r;
1028 begin_cond (&IF_COND (r));
1029 return r;
1032 /* Returns true if FN, a CALL_EXPR, is a call to
1033 std::is_constant_evaluated or __builtin_is_constant_evaluated. */
1035 static bool
1036 is_std_constant_evaluated_p (tree fn)
1038 /* std::is_constant_evaluated takes no arguments. */
1039 if (call_expr_nargs (fn) != 0)
1040 return false;
1042 tree fndecl = cp_get_callee_fndecl_nofold (fn);
1043 if (fndecl == NULL_TREE)
1044 return false;
1046 if (fndecl_built_in_p (fndecl, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
1047 BUILT_IN_FRONTEND))
1048 return true;
1050 if (!decl_in_std_namespace_p (fndecl))
1051 return false;
1053 tree name = DECL_NAME (fndecl);
1054 return name && id_equal (name, "is_constant_evaluated");
1057 /* Callback function for maybe_warn_for_constant_evaluated that looks
1058 for calls to std::is_constant_evaluated in TP. */
1060 static tree
1061 find_std_constant_evaluated_r (tree *tp, int *walk_subtrees, void *)
1063 tree t = *tp;
1065 if (TYPE_P (t) || TREE_CONSTANT (t))
1067 *walk_subtrees = false;
1068 return NULL_TREE;
1071 switch (TREE_CODE (t))
1073 case CALL_EXPR:
1074 if (is_std_constant_evaluated_p (t))
1075 return t;
1076 break;
1077 case EXPR_STMT:
1078 /* Don't warn in statement expressions. */
1079 *walk_subtrees = false;
1080 return NULL_TREE;
1081 default:
1082 break;
1085 return NULL_TREE;
1088 /* In certain contexts, std::is_constant_evaluated() is always true (for
1089 instance, in a consteval function or in a constexpr if), or always false
1090 (e.g., in a non-constexpr non-consteval function) so give the user a clue. */
1092 static void
1093 maybe_warn_for_constant_evaluated (tree cond, bool constexpr_if)
1095 if (!warn_tautological_compare)
1096 return;
1098 /* Suppress warning for std::is_constant_evaluated if the conditional
1099 comes from a macro. */
1100 if (from_macro_expansion_at (EXPR_LOCATION (cond)))
1101 return;
1103 cond = cp_walk_tree_without_duplicates (&cond, find_std_constant_evaluated_r,
1104 NULL);
1105 if (cond)
1107 if (constexpr_if)
1108 warning_at (EXPR_LOCATION (cond), OPT_Wtautological_compare,
1109 "%<std::is_constant_evaluated%> always evaluates to "
1110 "true in %<if constexpr%>");
1111 else if (!maybe_constexpr_fn (current_function_decl))
1112 warning_at (EXPR_LOCATION (cond), OPT_Wtautological_compare,
1113 "%<std::is_constant_evaluated%> always evaluates to "
1114 "false in a non-%<constexpr%> function");
1115 else if (DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
1116 warning_at (EXPR_LOCATION (cond), OPT_Wtautological_compare,
1117 "%<std::is_constant_evaluated%> always evaluates to "
1118 "true in a %<consteval%> function");
1122 /* Process the COND of an if-statement, which may be given by
1123 IF_STMT. */
1125 tree
1126 finish_if_stmt_cond (tree orig_cond, tree if_stmt)
1128 tree cond = maybe_convert_cond (orig_cond);
1129 if (IF_STMT_CONSTEXPR_P (if_stmt)
1130 && !type_dependent_expression_p (cond)
1131 && require_constant_expression (cond)
1132 && !instantiation_dependent_expression_p (cond)
1133 /* Wait until instantiation time, since only then COND has been
1134 converted to bool. */
1135 && TYPE_MAIN_VARIANT (TREE_TYPE (cond)) == boolean_type_node)
1137 maybe_warn_for_constant_evaluated (cond, /*constexpr_if=*/true);
1138 cond = instantiate_non_dependent_expr (cond);
1139 cond = cxx_constant_value (cond);
1141 else
1143 maybe_warn_for_constant_evaluated (cond, /*constexpr_if=*/false);
1144 if (processing_template_decl)
1145 cond = orig_cond;
1147 finish_cond (&IF_COND (if_stmt), cond);
1148 add_stmt (if_stmt);
1149 THEN_CLAUSE (if_stmt) = push_stmt_list ();
1150 return cond;
1153 /* Finish the then-clause of an if-statement, which may be given by
1154 IF_STMT. */
1156 tree
1157 finish_then_clause (tree if_stmt)
1159 THEN_CLAUSE (if_stmt) = pop_stmt_list (THEN_CLAUSE (if_stmt));
1160 return if_stmt;
1163 /* Begin the else-clause of an if-statement. */
1165 void
1166 begin_else_clause (tree if_stmt)
1168 ELSE_CLAUSE (if_stmt) = push_stmt_list ();
1171 /* Finish the else-clause of an if-statement, which may be given by
1172 IF_STMT. */
1174 void
1175 finish_else_clause (tree if_stmt)
1177 ELSE_CLAUSE (if_stmt) = pop_stmt_list (ELSE_CLAUSE (if_stmt));
1180 /* Callback for cp_walk_tree to mark all {VAR,PARM}_DECLs in a tree as
1181 read. */
1183 static tree
1184 maybe_mark_exp_read_r (tree *tp, int *, void *)
1186 tree t = *tp;
1187 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
1188 mark_exp_read (t);
1189 return NULL_TREE;
1192 /* Finish an if-statement. */
1194 void
1195 finish_if_stmt (tree if_stmt)
1197 tree scope = IF_SCOPE (if_stmt);
1198 IF_SCOPE (if_stmt) = NULL;
1199 if (IF_STMT_CONSTEXPR_P (if_stmt))
1201 /* Prevent various -Wunused warnings. We might not instantiate
1202 either of these branches, so we would not mark the variables
1203 used in that branch as read. */
1204 cp_walk_tree_without_duplicates (&THEN_CLAUSE (if_stmt),
1205 maybe_mark_exp_read_r, NULL);
1206 cp_walk_tree_without_duplicates (&ELSE_CLAUSE (if_stmt),
1207 maybe_mark_exp_read_r, NULL);
1209 add_stmt (do_poplevel (scope));
1212 /* Begin a while-statement. Returns a newly created WHILE_STMT if
1213 appropriate. */
1215 tree
1216 begin_while_stmt (void)
1218 tree r;
1219 r = build_stmt (input_location, WHILE_STMT, NULL_TREE, NULL_TREE);
1220 add_stmt (r);
1221 WHILE_BODY (r) = do_pushlevel (sk_block);
1222 begin_cond (&WHILE_COND (r));
1223 return r;
1226 /* Process the COND of a while-statement, which may be given by
1227 WHILE_STMT. */
1229 void
1230 finish_while_stmt_cond (tree cond, tree while_stmt, bool ivdep,
1231 tree unroll, bool novector)
1233 cond = maybe_convert_cond (cond);
1234 finish_cond (&WHILE_COND (while_stmt), cond);
1235 begin_maybe_infinite_loop (cond);
1236 if (ivdep && cond != error_mark_node)
1237 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
1238 TREE_TYPE (WHILE_COND (while_stmt)),
1239 WHILE_COND (while_stmt),
1240 build_int_cst (integer_type_node,
1241 annot_expr_ivdep_kind),
1242 integer_zero_node);
1243 if (unroll && cond != error_mark_node)
1244 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
1245 TREE_TYPE (WHILE_COND (while_stmt)),
1246 WHILE_COND (while_stmt),
1247 build_int_cst (integer_type_node,
1248 annot_expr_unroll_kind),
1249 unroll);
1250 if (novector && cond != error_mark_node)
1251 WHILE_COND (while_stmt) = build3 (ANNOTATE_EXPR,
1252 TREE_TYPE (WHILE_COND (while_stmt)),
1253 WHILE_COND (while_stmt),
1254 build_int_cst (integer_type_node,
1255 annot_expr_no_vector_kind),
1256 integer_zero_node);
1257 simplify_loop_decl_cond (&WHILE_COND (while_stmt), WHILE_BODY (while_stmt));
1260 /* Finish a while-statement, which may be given by WHILE_STMT. */
1262 void
1263 finish_while_stmt (tree while_stmt)
1265 end_maybe_infinite_loop (boolean_true_node);
1266 WHILE_BODY (while_stmt) = do_poplevel (WHILE_BODY (while_stmt));
1269 /* Begin a do-statement. Returns a newly created DO_STMT if
1270 appropriate. */
1272 tree
1273 begin_do_stmt (void)
1275 tree r = build_stmt (input_location, DO_STMT, NULL_TREE, NULL_TREE);
1276 begin_maybe_infinite_loop (boolean_true_node);
1277 add_stmt (r);
1278 DO_BODY (r) = push_stmt_list ();
1279 return r;
1282 /* Finish the body of a do-statement, which may be given by DO_STMT. */
1284 void
1285 finish_do_body (tree do_stmt)
1287 tree body = DO_BODY (do_stmt) = pop_stmt_list (DO_BODY (do_stmt));
1289 if (TREE_CODE (body) == STATEMENT_LIST && STATEMENT_LIST_TAIL (body))
1290 body = STATEMENT_LIST_TAIL (body)->stmt;
1292 if (IS_EMPTY_STMT (body))
1293 warning (OPT_Wempty_body,
1294 "suggest explicit braces around empty body in %<do%> statement");
1297 /* Finish a do-statement, which may be given by DO_STMT, and whose
1298 COND is as indicated. */
1300 void
1301 finish_do_stmt (tree cond, tree do_stmt, bool ivdep, tree unroll,
1302 bool novector)
1304 cond = maybe_convert_cond (cond);
1305 end_maybe_infinite_loop (cond);
1306 /* Unlike other iteration statements, the condition may not contain
1307 a declaration, so we don't call finish_cond which checks for
1308 unexpanded parameter packs. */
1309 if (check_for_bare_parameter_packs (cond))
1310 cond = error_mark_node;
1311 if (ivdep && cond != error_mark_node)
1312 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
1313 build_int_cst (integer_type_node, annot_expr_ivdep_kind),
1314 integer_zero_node);
1315 if (unroll && cond != error_mark_node)
1316 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
1317 build_int_cst (integer_type_node, annot_expr_unroll_kind),
1318 unroll);
1319 if (novector && cond != error_mark_node)
1320 cond = build3 (ANNOTATE_EXPR, TREE_TYPE (cond), cond,
1321 build_int_cst (integer_type_node, annot_expr_no_vector_kind),
1322 integer_zero_node);
1323 DO_COND (do_stmt) = cond;
1326 /* Finish a return-statement. The EXPRESSION returned, if any, is as
1327 indicated. */
1329 tree
1330 finish_return_stmt (tree expr)
1332 tree r;
1333 bool no_warning;
1334 bool dangling;
1336 expr = check_return_expr (expr, &no_warning, &dangling);
1338 if (error_operand_p (expr)
1339 || (flag_openmp && !check_omp_return ()))
1341 /* Suppress -Wreturn-type for this function. */
1342 if (warn_return_type)
1343 suppress_warning (current_function_decl, OPT_Wreturn_type);
1344 return error_mark_node;
1347 if (!processing_template_decl)
1349 if (warn_sequence_point)
1350 verify_sequence_points (expr);
1353 r = build_stmt (input_location, RETURN_EXPR, expr);
1354 RETURN_EXPR_LOCAL_ADDR_P (r) = dangling;
1355 if (no_warning)
1356 suppress_warning (r, OPT_Wreturn_type);
1357 r = maybe_cleanup_point_expr_void (r);
1358 r = add_stmt (r);
1360 return r;
1363 /* Begin the scope of a for-statement or a range-for-statement.
1364 Both the returned trees are to be used in a call to
1365 begin_for_stmt or begin_range_for_stmt. */
1367 tree
1368 begin_for_scope (tree *init)
1370 tree scope = do_pushlevel (sk_for);
1372 if (processing_template_decl)
1373 *init = push_stmt_list ();
1374 else
1375 *init = NULL_TREE;
1377 return scope;
1380 /* Begin a for-statement. Returns a new FOR_STMT.
1381 SCOPE and INIT should be the return of begin_for_scope,
1382 or both NULL_TREE */
1384 tree
1385 begin_for_stmt (tree scope, tree init)
1387 tree r;
1389 r = build_stmt (input_location, FOR_STMT, NULL_TREE, NULL_TREE,
1390 NULL_TREE, NULL_TREE, NULL_TREE);
1392 if (scope == NULL_TREE)
1394 gcc_assert (!init);
1395 scope = begin_for_scope (&init);
1398 FOR_INIT_STMT (r) = init;
1399 FOR_SCOPE (r) = scope;
1401 return r;
1404 /* Finish the init-statement of a for-statement, which may be
1405 given by FOR_STMT. */
1407 void
1408 finish_init_stmt (tree for_stmt)
1410 if (processing_template_decl)
1411 FOR_INIT_STMT (for_stmt) = pop_stmt_list (FOR_INIT_STMT (for_stmt));
1412 add_stmt (for_stmt);
1413 FOR_BODY (for_stmt) = do_pushlevel (sk_block);
1414 begin_cond (&FOR_COND (for_stmt));
1417 /* Finish the COND of a for-statement, which may be given by
1418 FOR_STMT. */
1420 void
1421 finish_for_cond (tree cond, tree for_stmt, bool ivdep, tree unroll,
1422 bool novector)
1424 cond = maybe_convert_cond (cond);
1425 finish_cond (&FOR_COND (for_stmt), cond);
1426 begin_maybe_infinite_loop (cond);
1427 if (ivdep && cond != error_mark_node)
1428 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1429 TREE_TYPE (FOR_COND (for_stmt)),
1430 FOR_COND (for_stmt),
1431 build_int_cst (integer_type_node,
1432 annot_expr_ivdep_kind),
1433 integer_zero_node);
1434 if (unroll && cond != error_mark_node)
1435 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1436 TREE_TYPE (FOR_COND (for_stmt)),
1437 FOR_COND (for_stmt),
1438 build_int_cst (integer_type_node,
1439 annot_expr_unroll_kind),
1440 unroll);
1441 if (novector && cond != error_mark_node)
1442 FOR_COND (for_stmt) = build3 (ANNOTATE_EXPR,
1443 TREE_TYPE (FOR_COND (for_stmt)),
1444 FOR_COND (for_stmt),
1445 build_int_cst (integer_type_node,
1446 annot_expr_no_vector_kind),
1447 integer_zero_node);
1448 simplify_loop_decl_cond (&FOR_COND (for_stmt), FOR_BODY (for_stmt));
1451 /* Finish the increment-EXPRESSION in a for-statement, which may be
1452 given by FOR_STMT. */
1454 void
1455 finish_for_expr (tree expr, tree for_stmt)
1457 if (!expr)
1458 return;
1459 /* If EXPR is an overloaded function, issue an error; there is no
1460 context available to use to perform overload resolution. */
1461 if (type_unknown_p (expr))
1463 cxx_incomplete_type_error (expr, TREE_TYPE (expr));
1464 expr = error_mark_node;
1466 if (!processing_template_decl)
1468 if (warn_sequence_point)
1469 verify_sequence_points (expr);
1470 expr = convert_to_void (expr, ICV_THIRD_IN_FOR,
1471 tf_warning_or_error);
1473 else if (!type_dependent_expression_p (expr))
1474 convert_to_void (expr, ICV_THIRD_IN_FOR, tf_warning_or_error);
1475 expr = maybe_cleanup_point_expr_void (expr);
1476 if (check_for_bare_parameter_packs (expr))
1477 expr = error_mark_node;
1478 FOR_EXPR (for_stmt) = expr;
1481 /* Finish the body of a for-statement, which may be given by
1482 FOR_STMT. The increment-EXPR for the loop must be
1483 provided.
1484 It can also finish RANGE_FOR_STMT. */
1486 void
1487 finish_for_stmt (tree for_stmt)
1489 end_maybe_infinite_loop (boolean_true_node);
1491 if (TREE_CODE (for_stmt) == RANGE_FOR_STMT)
1492 RANGE_FOR_BODY (for_stmt) = do_poplevel (RANGE_FOR_BODY (for_stmt));
1493 else
1494 FOR_BODY (for_stmt) = do_poplevel (FOR_BODY (for_stmt));
1496 /* Pop the scope for the body of the loop. */
1497 tree *scope_ptr = (TREE_CODE (for_stmt) == RANGE_FOR_STMT
1498 ? &RANGE_FOR_SCOPE (for_stmt)
1499 : &FOR_SCOPE (for_stmt));
1500 tree scope = *scope_ptr;
1501 *scope_ptr = NULL;
1503 /* During parsing of the body, range for uses "__for_{range,begin,end} "
1504 decl names to make those unaccessible by code in the body.
1505 Change it to ones with underscore instead of space, so that it can
1506 be inspected in the debugger. */
1507 tree range_for_decl[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
1508 gcc_assert (CPTI_FOR_BEGIN__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 1
1509 && CPTI_FOR_END__IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 2
1510 && CPTI_FOR_RANGE_IDENTIFIER == CPTI_FOR_RANGE__IDENTIFIER + 3
1511 && CPTI_FOR_BEGIN_IDENTIFIER == CPTI_FOR_BEGIN__IDENTIFIER + 3
1512 && CPTI_FOR_END_IDENTIFIER == CPTI_FOR_END__IDENTIFIER + 3);
1513 for (int i = 0; i < 3; i++)
1515 tree id = cp_global_trees[CPTI_FOR_RANGE__IDENTIFIER + i];
1516 if (IDENTIFIER_BINDING (id)
1517 && IDENTIFIER_BINDING (id)->scope == current_binding_level)
1519 range_for_decl[i] = IDENTIFIER_BINDING (id)->value;
1520 gcc_assert (VAR_P (range_for_decl[i])
1521 && DECL_ARTIFICIAL (range_for_decl[i]));
1525 add_stmt (do_poplevel (scope));
1527 /* If we're being called from build_vec_init, don't mess with the names of
1528 the variables for an enclosing range-for. */
1529 if (!stmts_are_full_exprs_p ())
1530 return;
1532 for (int i = 0; i < 3; i++)
1533 if (range_for_decl[i])
1534 DECL_NAME (range_for_decl[i])
1535 = cp_global_trees[CPTI_FOR_RANGE_IDENTIFIER + i];
1538 /* Begin a range-for-statement. Returns a new RANGE_FOR_STMT.
1539 SCOPE and INIT should be the return of begin_for_scope,
1540 or both NULL_TREE .
1541 To finish it call finish_for_stmt(). */
1543 tree
1544 begin_range_for_stmt (tree scope, tree init)
1546 begin_maybe_infinite_loop (boolean_false_node);
1548 tree r = build_stmt (input_location, RANGE_FOR_STMT, NULL_TREE, NULL_TREE,
1549 NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE);
1551 if (scope == NULL_TREE)
1553 gcc_assert (!init);
1554 scope = begin_for_scope (&init);
1557 /* Since C++20, RANGE_FOR_STMTs can use the init tree, so save it. */
1558 RANGE_FOR_INIT_STMT (r) = init;
1559 RANGE_FOR_SCOPE (r) = scope;
1561 return r;
1564 /* Finish the head of a range-based for statement, which may
1565 be given by RANGE_FOR_STMT. DECL must be the declaration
1566 and EXPR must be the loop expression. */
1568 void
1569 finish_range_for_decl (tree range_for_stmt, tree decl, tree expr)
1571 if (processing_template_decl)
1572 RANGE_FOR_INIT_STMT (range_for_stmt)
1573 = pop_stmt_list (RANGE_FOR_INIT_STMT (range_for_stmt));
1574 RANGE_FOR_DECL (range_for_stmt) = decl;
1575 RANGE_FOR_EXPR (range_for_stmt) = expr;
1576 add_stmt (range_for_stmt);
1577 RANGE_FOR_BODY (range_for_stmt) = do_pushlevel (sk_block);
1580 /* Finish a break-statement. */
1582 tree
1583 finish_break_stmt (void)
1585 /* In switch statements break is sometimes stylistically used after
1586 a return statement. This can lead to spurious warnings about
1587 control reaching the end of a non-void function when it is
1588 inlined. Note that we are calling block_may_fallthru with
1589 language specific tree nodes; this works because
1590 block_may_fallthru returns true when given something it does not
1591 understand. */
1592 if (!block_may_fallthru (cur_stmt_list))
1593 return void_node;
1594 note_break_stmt ();
1595 return add_stmt (build_stmt (input_location, BREAK_STMT));
1598 /* Finish a continue-statement. */
1600 tree
1601 finish_continue_stmt (void)
1603 return add_stmt (build_stmt (input_location, CONTINUE_STMT));
1606 /* Begin a switch-statement. Returns a new SWITCH_STMT if
1607 appropriate. */
1609 tree
1610 begin_switch_stmt (void)
1612 tree r, scope;
1614 scope = do_pushlevel (sk_cond);
1615 r = build_stmt (input_location, SWITCH_STMT, NULL_TREE, NULL_TREE, NULL_TREE, scope);
1617 begin_cond (&SWITCH_STMT_COND (r));
1619 return r;
1622 /* Finish the cond of a switch-statement. */
1624 void
1625 finish_switch_cond (tree cond, tree switch_stmt)
1627 tree orig_type = NULL;
1629 if (!processing_template_decl)
1631 /* Convert the condition to an integer or enumeration type. */
1632 tree orig_cond = cond;
1633 cond = build_expr_type_conversion (WANT_INT | WANT_ENUM, cond, true);
1634 if (cond == NULL_TREE)
1636 error_at (cp_expr_loc_or_input_loc (orig_cond),
1637 "switch quantity not an integer");
1638 cond = error_mark_node;
1640 /* We want unlowered type here to handle enum bit-fields. */
1641 orig_type = unlowered_expr_type (cond);
1642 if (TREE_CODE (orig_type) != ENUMERAL_TYPE)
1643 orig_type = TREE_TYPE (cond);
1644 if (cond != error_mark_node)
1646 /* [stmt.switch]
1648 Integral promotions are performed. */
1649 cond = perform_integral_promotions (cond);
1650 cond = maybe_cleanup_point_expr (cond);
1653 if (check_for_bare_parameter_packs (cond))
1654 cond = error_mark_node;
1655 else if (!processing_template_decl && warn_sequence_point)
1656 verify_sequence_points (cond);
1658 finish_cond (&SWITCH_STMT_COND (switch_stmt), cond);
1659 SWITCH_STMT_TYPE (switch_stmt) = orig_type;
1660 add_stmt (switch_stmt);
1661 push_switch (switch_stmt);
1662 SWITCH_STMT_BODY (switch_stmt) = push_stmt_list ();
1665 /* Finish the body of a switch-statement, which may be given by
1666 SWITCH_STMT. The COND to switch on is indicated. */
1668 void
1669 finish_switch_stmt (tree switch_stmt)
1671 tree scope;
1673 SWITCH_STMT_BODY (switch_stmt) =
1674 pop_stmt_list (SWITCH_STMT_BODY (switch_stmt));
1675 pop_switch ();
1677 scope = SWITCH_STMT_SCOPE (switch_stmt);
1678 SWITCH_STMT_SCOPE (switch_stmt) = NULL;
1679 add_stmt (do_poplevel (scope));
1682 /* Begin a try-block. Returns a newly-created TRY_BLOCK if
1683 appropriate. */
1685 tree
1686 begin_try_block (void)
1688 tree r = build_stmt (input_location, TRY_BLOCK, NULL_TREE, NULL_TREE);
1689 add_stmt (r);
1690 TRY_STMTS (r) = push_stmt_list ();
1691 return r;
1694 /* Likewise, for a function-try-block. The block returned in
1695 *COMPOUND_STMT is an artificial outer scope, containing the
1696 function-try-block. */
1698 tree
1699 begin_function_try_block (tree *compound_stmt)
1701 tree r;
1702 /* This outer scope does not exist in the C++ standard, but we need
1703 a place to put __FUNCTION__ and similar variables. */
1704 *compound_stmt = begin_compound_stmt (0);
1705 current_binding_level->artificial = 1;
1706 r = begin_try_block ();
1707 FN_TRY_BLOCK_P (r) = 1;
1708 return r;
1711 /* Finish a try-block, which may be given by TRY_BLOCK. */
1713 void
1714 finish_try_block (tree try_block)
1716 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1717 TRY_HANDLERS (try_block) = push_stmt_list ();
1720 /* Finish the body of a cleanup try-block, which may be given by
1721 TRY_BLOCK. */
1723 void
1724 finish_cleanup_try_block (tree try_block)
1726 TRY_STMTS (try_block) = pop_stmt_list (TRY_STMTS (try_block));
1729 /* Finish an implicitly generated try-block, with a cleanup is given
1730 by CLEANUP. */
1732 void
1733 finish_cleanup (tree cleanup, tree try_block)
1735 TRY_HANDLERS (try_block) = cleanup;
1736 CLEANUP_P (try_block) = 1;
1739 /* Likewise, for a function-try-block. */
1741 void
1742 finish_function_try_block (tree try_block)
1744 finish_try_block (try_block);
1745 /* FIXME : something queer about CTOR_INITIALIZER somehow following
1746 the try block, but moving it inside. */
1747 in_function_try_handler = 1;
1750 /* Finish a handler-sequence for a try-block, which may be given by
1751 TRY_BLOCK. */
1753 void
1754 finish_handler_sequence (tree try_block)
1756 TRY_HANDLERS (try_block) = pop_stmt_list (TRY_HANDLERS (try_block));
1757 check_handlers (TRY_HANDLERS (try_block));
1760 /* Finish the handler-seq for a function-try-block, given by
1761 TRY_BLOCK. COMPOUND_STMT is the outer block created by
1762 begin_function_try_block. */
1764 void
1765 finish_function_handler_sequence (tree try_block, tree compound_stmt)
1767 in_function_try_handler = 0;
1768 finish_handler_sequence (try_block);
1769 finish_compound_stmt (compound_stmt);
1772 /* Begin a handler. Returns a HANDLER if appropriate. */
1774 tree
1775 begin_handler (void)
1777 tree r;
1779 r = build_stmt (input_location, HANDLER, NULL_TREE, NULL_TREE);
1780 add_stmt (r);
1782 /* Create a binding level for the eh_info and the exception object
1783 cleanup. */
1784 HANDLER_BODY (r) = do_pushlevel (sk_catch);
1786 return r;
1789 /* Finish the handler-parameters for a handler, which may be given by
1790 HANDLER. DECL is the declaration for the catch parameter, or NULL
1791 if this is a `catch (...)' clause. */
1793 void
1794 finish_handler_parms (tree decl, tree handler)
1796 tree type = NULL_TREE;
1797 if (processing_template_decl)
1799 if (decl)
1801 decl = pushdecl (decl);
1802 decl = push_template_decl (decl);
1803 HANDLER_PARMS (handler) = decl;
1804 type = TREE_TYPE (decl);
1807 else
1809 type = expand_start_catch_block (decl);
1810 if (warn_catch_value
1811 && type != NULL_TREE
1812 && type != error_mark_node
1813 && !TYPE_REF_P (TREE_TYPE (decl)))
1815 tree orig_type = TREE_TYPE (decl);
1816 if (CLASS_TYPE_P (orig_type))
1818 if (TYPE_POLYMORPHIC_P (orig_type))
1819 warning_at (DECL_SOURCE_LOCATION (decl),
1820 OPT_Wcatch_value_,
1821 "catching polymorphic type %q#T by value",
1822 orig_type);
1823 else if (warn_catch_value > 1)
1824 warning_at (DECL_SOURCE_LOCATION (decl),
1825 OPT_Wcatch_value_,
1826 "catching type %q#T by value", orig_type);
1828 else if (warn_catch_value > 2)
1829 warning_at (DECL_SOURCE_LOCATION (decl),
1830 OPT_Wcatch_value_,
1831 "catching non-reference type %q#T", orig_type);
1834 HANDLER_TYPE (handler) = type;
1837 /* Finish a handler, which may be given by HANDLER. The BLOCKs are
1838 the return value from the matching call to finish_handler_parms. */
1840 void
1841 finish_handler (tree handler)
1843 if (!processing_template_decl)
1844 expand_end_catch_block ();
1845 HANDLER_BODY (handler) = do_poplevel (HANDLER_BODY (handler));
1848 /* Begin a compound statement. FLAGS contains some bits that control the
1849 behavior and context. If BCS_NO_SCOPE is set, the compound statement
1850 does not define a scope. If BCS_FN_BODY is set, this is the outermost
1851 block of a function. If BCS_TRY_BLOCK is set, this is the block
1852 created on behalf of a TRY statement. Returns a token to be passed to
1853 finish_compound_stmt. */
1855 tree
1856 begin_compound_stmt (unsigned int flags)
1858 tree r;
1860 if (flags & BCS_NO_SCOPE)
1862 r = push_stmt_list ();
1863 STATEMENT_LIST_NO_SCOPE (r) = 1;
1865 /* Normally, we try hard to keep the BLOCK for a statement-expression.
1866 But, if it's a statement-expression with a scopeless block, there's
1867 nothing to keep, and we don't want to accidentally keep a block
1868 *inside* the scopeless block. */
1869 keep_next_level (false);
1871 else
1873 scope_kind sk = sk_block;
1874 if (flags & BCS_TRY_BLOCK)
1875 sk = sk_try;
1876 else if (flags & BCS_TRANSACTION)
1877 sk = sk_transaction;
1878 else if (flags & BCS_STMT_EXPR)
1879 sk = sk_stmt_expr;
1880 r = do_pushlevel (sk);
1883 /* When processing a template, we need to remember where the braces were,
1884 so that we can set up identical scopes when instantiating the template
1885 later. BIND_EXPR is a handy candidate for this.
1886 Note that do_poplevel won't create a BIND_EXPR itself here (and thus
1887 result in nested BIND_EXPRs), since we don't build BLOCK nodes when
1888 processing templates. */
1889 if (processing_template_decl)
1891 r = build3 (BIND_EXPR, NULL, NULL, r, NULL);
1892 BIND_EXPR_TRY_BLOCK (r) = (flags & BCS_TRY_BLOCK) != 0;
1893 BIND_EXPR_BODY_BLOCK (r) = (flags & BCS_FN_BODY) != 0;
1894 TREE_SIDE_EFFECTS (r) = 1;
1897 return r;
1900 /* Finish a compound-statement, which is given by STMT. */
1902 void
1903 finish_compound_stmt (tree stmt)
1905 if (TREE_CODE (stmt) == BIND_EXPR)
1907 tree body = do_poplevel (BIND_EXPR_BODY (stmt));
1908 /* If the STATEMENT_LIST is empty and this BIND_EXPR isn't special,
1909 discard the BIND_EXPR so it can be merged with the containing
1910 STATEMENT_LIST. */
1911 if (TREE_CODE (body) == STATEMENT_LIST
1912 && STATEMENT_LIST_HEAD (body) == NULL
1913 && !BIND_EXPR_BODY_BLOCK (stmt)
1914 && !BIND_EXPR_TRY_BLOCK (stmt))
1915 stmt = body;
1916 else
1917 BIND_EXPR_BODY (stmt) = body;
1919 else if (STATEMENT_LIST_NO_SCOPE (stmt))
1920 stmt = pop_stmt_list (stmt);
1921 else
1923 /* Destroy any ObjC "super" receivers that may have been
1924 created. */
1925 objc_clear_super_receiver ();
1927 stmt = do_poplevel (stmt);
1930 /* ??? See c_end_compound_stmt wrt statement expressions. */
1931 add_stmt (stmt);
1934 /* Finish an asm-statement, whose components are a STRING, some
1935 OUTPUT_OPERANDS, some INPUT_OPERANDS, some CLOBBERS and some
1936 LABELS. Also note whether the asm-statement should be
1937 considered volatile, and whether it is asm inline. */
1939 tree
1940 finish_asm_stmt (location_t loc, int volatile_p, tree string,
1941 tree output_operands, tree input_operands, tree clobbers,
1942 tree labels, bool inline_p)
1944 tree r;
1945 tree t;
1946 int ninputs = list_length (input_operands);
1947 int noutputs = list_length (output_operands);
1949 if (!processing_template_decl)
1951 const char *constraint;
1952 const char **oconstraints;
1953 bool allows_mem, allows_reg, is_inout;
1954 tree operand;
1955 int i;
1957 oconstraints = XALLOCAVEC (const char *, noutputs);
1959 string = resolve_asm_operand_names (string, output_operands,
1960 input_operands, labels);
1962 for (i = 0, t = output_operands; t; t = TREE_CHAIN (t), ++i)
1964 operand = TREE_VALUE (t);
1966 /* ??? Really, this should not be here. Users should be using a
1967 proper lvalue, dammit. But there's a long history of using
1968 casts in the output operands. In cases like longlong.h, this
1969 becomes a primitive form of typechecking -- if the cast can be
1970 removed, then the output operand had a type of the proper width;
1971 otherwise we'll get an error. Gross, but ... */
1972 STRIP_NOPS (operand);
1974 operand = mark_lvalue_use (operand);
1976 if (!lvalue_or_else (operand, lv_asm, tf_warning_or_error))
1977 operand = error_mark_node;
1979 if (operand != error_mark_node
1980 && (TREE_READONLY (operand)
1981 || CP_TYPE_CONST_P (TREE_TYPE (operand))
1982 /* Functions are not modifiable, even though they are
1983 lvalues. */
1984 || FUNC_OR_METHOD_TYPE_P (TREE_TYPE (operand))
1985 /* If it's an aggregate and any field is const, then it is
1986 effectively const. */
1987 || (CLASS_TYPE_P (TREE_TYPE (operand))
1988 && C_TYPE_FIELDS_READONLY (TREE_TYPE (operand)))))
1989 cxx_readonly_error (loc, operand, lv_asm);
1991 tree *op = &operand;
1992 while (TREE_CODE (*op) == COMPOUND_EXPR)
1993 op = &TREE_OPERAND (*op, 1);
1994 switch (TREE_CODE (*op))
1996 case PREINCREMENT_EXPR:
1997 case PREDECREMENT_EXPR:
1998 case MODIFY_EXPR:
1999 *op = genericize_compound_lvalue (*op);
2000 op = &TREE_OPERAND (*op, 1);
2001 break;
2002 default:
2003 break;
2006 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2007 oconstraints[i] = constraint;
2009 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
2010 &allows_mem, &allows_reg, &is_inout))
2012 /* If the operand is going to end up in memory,
2013 mark it addressable. */
2014 if (!allows_reg && !cxx_mark_addressable (*op))
2015 operand = error_mark_node;
2017 else
2018 operand = error_mark_node;
2020 TREE_VALUE (t) = operand;
2023 for (i = 0, t = input_operands; t; ++i, t = TREE_CHAIN (t))
2025 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
2026 bool constraint_parsed
2027 = parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
2028 oconstraints, &allows_mem, &allows_reg);
2029 /* If the operand is going to end up in memory, don't call
2030 decay_conversion. */
2031 if (constraint_parsed && !allows_reg && allows_mem)
2032 operand = mark_lvalue_use (TREE_VALUE (t));
2033 else
2034 operand = decay_conversion (TREE_VALUE (t), tf_warning_or_error);
2036 /* If the type of the operand hasn't been determined (e.g.,
2037 because it involves an overloaded function), then issue
2038 an error message. There's no context available to
2039 resolve the overloading. */
2040 if (TREE_TYPE (operand) == unknown_type_node)
2042 error_at (loc,
2043 "type of %<asm%> operand %qE could not be determined",
2044 TREE_VALUE (t));
2045 operand = error_mark_node;
2048 if (constraint_parsed)
2050 /* If the operand is going to end up in memory,
2051 mark it addressable. */
2052 if (!allows_reg && allows_mem)
2054 /* Strip the nops as we allow this case. FIXME, this really
2055 should be rejected or made deprecated. */
2056 STRIP_NOPS (operand);
2058 tree *op = &operand;
2059 while (TREE_CODE (*op) == COMPOUND_EXPR)
2060 op = &TREE_OPERAND (*op, 1);
2061 switch (TREE_CODE (*op))
2063 case PREINCREMENT_EXPR:
2064 case PREDECREMENT_EXPR:
2065 case MODIFY_EXPR:
2066 *op = genericize_compound_lvalue (*op);
2067 op = &TREE_OPERAND (*op, 1);
2068 break;
2069 default:
2070 break;
2073 if (!cxx_mark_addressable (*op))
2074 operand = error_mark_node;
2076 else if (!allows_reg && !allows_mem)
2078 /* If constraint allows neither register nor memory,
2079 try harder to get a constant. */
2080 tree constop = maybe_constant_value (operand);
2081 if (TREE_CONSTANT (constop))
2082 operand = constop;
2085 else
2086 operand = error_mark_node;
2088 TREE_VALUE (t) = operand;
2092 r = build_stmt (loc, ASM_EXPR, string,
2093 output_operands, input_operands,
2094 clobbers, labels);
2095 ASM_VOLATILE_P (r) = volatile_p || noutputs == 0;
2096 ASM_INLINE_P (r) = inline_p;
2097 r = maybe_cleanup_point_expr_void (r);
2098 return add_stmt (r);
2101 /* Finish a label with the indicated NAME. Returns the new label. */
2103 tree
2104 finish_label_stmt (tree name)
2106 tree decl = define_label (input_location, name);
2108 if (decl == error_mark_node)
2109 return error_mark_node;
2111 add_stmt (build_stmt (input_location, LABEL_EXPR, decl));
2113 return decl;
2116 /* Finish a series of declarations for local labels. G++ allows users
2117 to declare "local" labels, i.e., labels with scope. This extension
2118 is useful when writing code involving statement-expressions. */
2120 void
2121 finish_label_decl (tree name)
2123 if (!at_function_scope_p ())
2125 error ("%<__label__%> declarations are only allowed in function scopes");
2126 return;
2129 add_decl_expr (declare_local_label (name));
2132 /* When DECL goes out of scope, make sure that CLEANUP is executed. */
2134 void
2135 finish_decl_cleanup (tree decl, tree cleanup)
2137 push_cleanup (decl, cleanup, false);
2140 /* If the current scope exits with an exception, run CLEANUP. */
2142 void
2143 finish_eh_cleanup (tree cleanup)
2145 push_cleanup (NULL, cleanup, true);
2148 /* The MEM_INITS is a list of mem-initializers, in reverse of the
2149 order they were written by the user. Each node is as for
2150 emit_mem_initializers. */
2152 void
2153 finish_mem_initializers (tree mem_inits)
2155 /* Reorder the MEM_INITS so that they are in the order they appeared
2156 in the source program. */
2157 mem_inits = nreverse (mem_inits);
2159 if (processing_template_decl)
2161 tree mem;
2163 for (mem = mem_inits; mem; mem = TREE_CHAIN (mem))
2165 /* If the TREE_PURPOSE is a TYPE_PACK_EXPANSION, skip the
2166 check for bare parameter packs in the TREE_VALUE, because
2167 any parameter packs in the TREE_VALUE have already been
2168 bound as part of the TREE_PURPOSE. See
2169 make_pack_expansion for more information. */
2170 if (TREE_CODE (TREE_PURPOSE (mem)) != TYPE_PACK_EXPANSION
2171 && check_for_bare_parameter_packs (TREE_VALUE (mem)))
2172 TREE_VALUE (mem) = error_mark_node;
2175 add_stmt (build_min_nt_loc (UNKNOWN_LOCATION,
2176 CTOR_INITIALIZER, mem_inits));
2178 else
2179 emit_mem_initializers (mem_inits);
2182 /* Obfuscate EXPR if it looks like an id-expression or member access so
2183 that the call to finish_decltype in do_auto_deduction will give the
2184 right result. If EVEN_UNEVAL, do this even in unevaluated context. */
2186 tree
2187 force_paren_expr (tree expr, bool even_uneval /* = false */)
2189 /* This is only needed for decltype(auto) in C++14. */
2190 if (cxx_dialect < cxx14)
2191 return expr;
2193 /* If we're in unevaluated context, we can't be deducing a
2194 return/initializer type, so we don't need to mess with this. */
2195 if (cp_unevaluated_operand && !even_uneval)
2196 return expr;
2198 if (TREE_CODE (expr) == COMPONENT_REF
2199 || TREE_CODE (expr) == SCOPE_REF
2200 || REFERENCE_REF_P (expr))
2201 REF_PARENTHESIZED_P (expr) = true;
2202 else if (DECL_P (tree_strip_any_location_wrapper (expr)))
2204 location_t loc = cp_expr_location (expr);
2205 const tree_code code = processing_template_decl ? PAREN_EXPR
2206 : VIEW_CONVERT_EXPR;
2207 expr = build1_loc (loc, code, TREE_TYPE (expr), expr);
2208 REF_PARENTHESIZED_P (expr) = true;
2210 return expr;
2213 /* If T is an id-expression obfuscated by force_paren_expr, undo the
2214 obfuscation and return the underlying id-expression. Otherwise
2215 return T. */
2217 tree
2218 maybe_undo_parenthesized_ref (tree t)
2220 if (cxx_dialect < cxx14)
2221 return t;
2223 if ((TREE_CODE (t) == PAREN_EXPR || TREE_CODE (t) == VIEW_CONVERT_EXPR)
2224 && REF_PARENTHESIZED_P (t))
2225 t = TREE_OPERAND (t, 0);
2227 return t;
2230 /* Finish a parenthesized expression EXPR. */
2232 cp_expr
2233 finish_parenthesized_expr (cp_expr expr)
2235 if (EXPR_P (expr))
2237 /* This inhibits warnings in maybe_warn_unparenthesized_assignment
2238 and c_common_truthvalue_conversion. */
2239 suppress_warning (STRIP_REFERENCE_REF (*expr), OPT_Wparentheses);
2242 if (TREE_CODE (expr) == OFFSET_REF
2243 || TREE_CODE (expr) == SCOPE_REF)
2244 /* [expr.unary.op]/3 The qualified id of a pointer-to-member must not be
2245 enclosed in parentheses. */
2246 PTRMEM_OK_P (expr) = 0;
2248 tree stripped_expr = tree_strip_any_location_wrapper (expr);
2249 if (TREE_CODE (stripped_expr) == STRING_CST)
2250 PAREN_STRING_LITERAL_P (stripped_expr) = 1;
2252 expr = cp_expr (force_paren_expr (expr), expr.get_location ());
2254 return expr;
2257 /* Finish a reference to a non-static data member (DECL) that is not
2258 preceded by `.' or `->'. */
2260 tree
2261 finish_non_static_data_member (tree decl, tree object, tree qualifying_scope,
2262 tsubst_flags_t complain /* = tf_warning_or_error */)
2264 gcc_assert (TREE_CODE (decl) == FIELD_DECL);
2265 bool try_omp_private = !object && omp_private_member_map;
2266 tree ret;
2268 if (!object)
2270 tree scope = qualifying_scope;
2271 if (scope == NULL_TREE)
2273 scope = context_for_name_lookup (decl);
2274 if (!TYPE_P (scope))
2276 /* Can happen during error recovery (c++/85014). */
2277 gcc_assert (seen_error ());
2278 return error_mark_node;
2281 object = maybe_dummy_object (scope, NULL);
2284 object = maybe_resolve_dummy (object, true);
2285 if (object == error_mark_node)
2286 return error_mark_node;
2288 /* DR 613/850: Can use non-static data members without an associated
2289 object in sizeof/decltype/alignof. */
2290 if (is_dummy_object (object)
2291 && !cp_unevaluated_operand
2292 && (!processing_template_decl || !current_class_ref))
2294 if (complain & tf_error)
2296 if (current_function_decl
2297 && DECL_STATIC_FUNCTION_P (current_function_decl))
2298 error ("invalid use of member %qD in static member function", decl);
2299 else if (current_function_decl
2300 && processing_contract_condition
2301 && DECL_CONSTRUCTOR_P (current_function_decl))
2302 error ("invalid use of member %qD in constructor %<pre%> contract", decl);
2303 else if (current_function_decl
2304 && processing_contract_condition
2305 && DECL_DESTRUCTOR_P (current_function_decl))
2306 error ("invalid use of member %qD in destructor %<post%> contract", decl);
2307 else
2308 error ("invalid use of non-static data member %qD", decl);
2309 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2312 return error_mark_node;
2315 if (current_class_ptr)
2316 TREE_USED (current_class_ptr) = 1;
2317 if (processing_template_decl)
2319 tree type = TREE_TYPE (decl);
2321 if (TYPE_REF_P (type))
2322 /* Quals on the object don't matter. */;
2323 else if (PACK_EXPANSION_P (type))
2324 /* Don't bother trying to represent this. */
2325 type = NULL_TREE;
2326 else if (WILDCARD_TYPE_P (TREE_TYPE (object)))
2327 /* We don't know what the eventual quals will be, so punt until
2328 instantiation time.
2330 This can happen when called from build_capture_proxy for an explicit
2331 object lambda. It's a bit marginal to call this function in that
2332 case, since this function is for references to members of 'this',
2333 but the deduced type is required to be derived from the closure
2334 type, so it works. */
2335 type = NULL_TREE;
2336 else
2338 /* Set the cv qualifiers. */
2339 int quals = cp_type_quals (TREE_TYPE (object));
2341 if (DECL_MUTABLE_P (decl))
2342 quals &= ~TYPE_QUAL_CONST;
2344 quals |= cp_type_quals (TREE_TYPE (decl));
2345 type = cp_build_qualified_type (type, quals);
2348 if (qualifying_scope)
2349 /* Wrap this in a SCOPE_REF for now. */
2350 ret = build_qualified_name (type, qualifying_scope, decl,
2351 /*template_p=*/false);
2352 else
2353 ret = (convert_from_reference
2354 (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
2356 /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
2357 QUALIFYING_SCOPE is also non-null. */
2358 else
2360 tree access_type = TREE_TYPE (object);
2362 if (!perform_or_defer_access_check (TYPE_BINFO (access_type), decl,
2363 decl, complain))
2364 return error_mark_node;
2366 /* If the data member was named `C::M', convert `*this' to `C'
2367 first. */
2368 if (qualifying_scope)
2370 tree binfo = NULL_TREE;
2371 object = build_scoped_ref (object, qualifying_scope,
2372 &binfo);
2375 ret = build_class_member_access_expr (object, decl,
2376 /*access_path=*/NULL_TREE,
2377 /*preserve_reference=*/false,
2378 complain);
2380 if (try_omp_private)
2382 tree *v = omp_private_member_map->get (decl);
2383 if (v)
2384 ret = convert_from_reference (*v);
2386 return ret;
2389 /* DECL was the declaration to which a qualified-id resolved. Issue
2390 an error message if it is not accessible. If OBJECT_TYPE is
2391 non-NULL, we have just seen `x->' or `x.' and OBJECT_TYPE is the
2392 type of `*x', or `x', respectively. If the DECL was named as
2393 `A::B' then NESTED_NAME_SPECIFIER is `A'. Return value is like
2394 perform_access_checks above. */
2396 bool
2397 check_accessibility_of_qualified_id (tree decl,
2398 tree object_type,
2399 tree nested_name_specifier,
2400 tsubst_flags_t complain)
2402 /* If we're not checking, return immediately. */
2403 if (deferred_access_no_check)
2404 return true;
2406 /* Determine the SCOPE of DECL. */
2407 tree scope = context_for_name_lookup (decl);
2408 /* If the SCOPE is not a type, then DECL is not a member. */
2409 if (!TYPE_P (scope)
2410 /* If SCOPE is dependent then we can't perform this access check now,
2411 and since we'll perform this access check again after substitution
2412 there's no need to explicitly defer it. */
2413 || dependent_type_p (scope))
2414 return true;
2416 tree qualifying_type = NULL_TREE;
2417 /* Compute the scope through which DECL is being accessed. */
2418 if (object_type
2419 /* OBJECT_TYPE might not be a class type; consider:
2421 class A { typedef int I; };
2422 I *p;
2423 p->A::I::~I();
2425 In this case, we will have "A::I" as the DECL, but "I" as the
2426 OBJECT_TYPE. */
2427 && CLASS_TYPE_P (object_type)
2428 && DERIVED_FROM_P (scope, object_type))
2429 /* If we are processing a `->' or `.' expression, use the type of the
2430 left-hand side. */
2431 qualifying_type = object_type;
2432 else if (nested_name_specifier)
2434 /* If the reference is to a non-static member of the
2435 current class, treat it as if it were referenced through
2436 `this'. */
2437 if (DECL_NONSTATIC_MEMBER_P (decl)
2438 && current_class_ptr)
2439 if (tree current = current_nonlambda_class_type ())
2441 if (dependent_type_p (current))
2442 /* In general we can't know whether this access goes through
2443 `this' until instantiation time. Punt now, or else we might
2444 create a deferred access check that's not relative to `this'
2445 when it ought to be. We'll check this access again after
2446 substitution, e.g. from tsubst_qualified_id. */
2447 return true;
2449 if (DERIVED_FROM_P (scope, current))
2450 qualifying_type = current;
2452 /* Otherwise, use the type indicated by the
2453 nested-name-specifier. */
2454 if (!qualifying_type)
2455 qualifying_type = nested_name_specifier;
2457 else
2458 /* Otherwise, the name must be from the current class or one of
2459 its bases. */
2460 qualifying_type = currently_open_derived_class (scope);
2462 if (qualifying_type
2463 /* It is possible for qualifying type to be a TEMPLATE_TYPE_PARM
2464 or similar in a default argument value. */
2465 && CLASS_TYPE_P (qualifying_type))
2466 return perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl,
2467 decl, complain);
2469 return true;
2472 /* EXPR is the result of a qualified-id. The QUALIFYING_CLASS was the
2473 class named to the left of the "::" operator. DONE is true if this
2474 expression is a complete postfix-expression; it is false if this
2475 expression is followed by '->', '[', '(', etc. ADDRESS_P is true
2476 iff this expression is the operand of '&'. TEMPLATE_P is true iff
2477 the qualified-id was of the form "A::template B". TEMPLATE_ARG_P
2478 is true iff this qualified name appears as a template argument. */
2480 tree
2481 finish_qualified_id_expr (tree qualifying_class,
2482 tree expr,
2483 bool done,
2484 bool address_p,
2485 bool template_p,
2486 bool template_arg_p,
2487 tsubst_flags_t complain)
2489 gcc_assert (TYPE_P (qualifying_class));
2491 if (error_operand_p (expr))
2492 return error_mark_node;
2494 if (DECL_P (expr)
2495 /* Functions are marked after overload resolution; avoid redundant
2496 warnings. */
2497 && TREE_CODE (expr) != FUNCTION_DECL
2498 && !mark_used (expr, complain))
2499 return error_mark_node;
2501 if (template_p)
2503 if (TREE_CODE (expr) == UNBOUND_CLASS_TEMPLATE)
2505 /* cp_parser_lookup_name thought we were looking for a type,
2506 but we're actually looking for a declaration. */
2507 qualifying_class = TYPE_CONTEXT (expr);
2508 expr = TYPE_IDENTIFIER (expr);
2510 else
2511 check_template_keyword (expr);
2514 /* If EXPR occurs as the operand of '&', use special handling that
2515 permits a pointer-to-member. */
2516 if (address_p && done
2517 && TREE_CODE (qualifying_class) != ENUMERAL_TYPE)
2519 if (TREE_CODE (expr) == SCOPE_REF)
2520 expr = TREE_OPERAND (expr, 1);
2521 expr = build_offset_ref (qualifying_class, expr,
2522 /*address_p=*/true, complain);
2523 return expr;
2526 /* No need to check access within an enum. */
2527 if (TREE_CODE (qualifying_class) == ENUMERAL_TYPE
2528 && TREE_CODE (expr) != IDENTIFIER_NODE)
2529 return expr;
2531 /* Within the scope of a class, turn references to non-static
2532 members into expression of the form "this->...". */
2533 if (template_arg_p)
2534 /* But, within a template argument, we do not want make the
2535 transformation, as there is no "this" pointer. */
2537 else if (TREE_CODE (expr) == FIELD_DECL)
2539 push_deferring_access_checks (dk_no_check);
2540 expr = finish_non_static_data_member (expr, NULL_TREE,
2541 qualifying_class, complain);
2542 pop_deferring_access_checks ();
2544 else if (BASELINK_P (expr))
2546 /* See if any of the functions are non-static members. */
2547 /* If so, the expression may be relative to 'this'. */
2548 if (!shared_member_p (expr)
2549 && current_class_ptr
2550 && DERIVED_FROM_P (qualifying_class,
2551 current_nonlambda_class_type ()))
2552 expr = (build_class_member_access_expr
2553 (maybe_dummy_object (qualifying_class, NULL),
2554 expr,
2555 BASELINK_ACCESS_BINFO (expr),
2556 /*preserve_reference=*/false,
2557 complain));
2558 else if (done)
2559 /* The expression is a qualified name whose address is not
2560 being taken. */
2561 expr = build_offset_ref (qualifying_class, expr, /*address_p=*/false,
2562 complain);
2564 else if (!template_p
2565 && TREE_CODE (expr) == TEMPLATE_DECL
2566 && !DECL_FUNCTION_TEMPLATE_P (expr))
2568 if (complain & tf_error)
2569 error ("%qE missing template arguments", expr);
2570 return error_mark_node;
2572 else
2574 /* In a template, return a SCOPE_REF for most qualified-ids
2575 so that we can check access at instantiation time. But if
2576 we're looking at a member of the current instantiation, we
2577 know we have access and building up the SCOPE_REF confuses
2578 non-type template argument handling. */
2579 if (processing_template_decl
2580 && (!currently_open_class (qualifying_class)
2581 || TREE_CODE (expr) == IDENTIFIER_NODE
2582 || TREE_CODE (expr) == TEMPLATE_ID_EXPR
2583 || TREE_CODE (expr) == BIT_NOT_EXPR))
2584 expr = build_qualified_name (TREE_TYPE (expr),
2585 qualifying_class, expr,
2586 template_p);
2587 else if (tree wrap = maybe_get_tls_wrapper_call (expr))
2588 expr = wrap;
2590 expr = convert_from_reference (expr);
2593 return expr;
2596 /* Begin a statement-expression. The value returned must be passed to
2597 finish_stmt_expr. */
2599 tree
2600 begin_stmt_expr (void)
2602 return push_stmt_list ();
2605 /* Process the final expression of a statement expression. EXPR can be
2606 NULL, if the final expression is empty. Return a STATEMENT_LIST
2607 containing all the statements in the statement-expression, or
2608 ERROR_MARK_NODE if there was an error. */
2610 tree
2611 finish_stmt_expr_expr (tree expr, tree stmt_expr)
2613 if (error_operand_p (expr))
2615 /* The type of the statement-expression is the type of the last
2616 expression. */
2617 TREE_TYPE (stmt_expr) = error_mark_node;
2618 return error_mark_node;
2621 /* If the last statement does not have "void" type, then the value
2622 of the last statement is the value of the entire expression. */
2623 if (expr)
2625 tree type = TREE_TYPE (expr);
2627 if (type && type_unknown_p (type))
2629 error ("a statement expression is an insufficient context"
2630 " for overload resolution");
2631 TREE_TYPE (stmt_expr) = error_mark_node;
2632 return error_mark_node;
2634 else if (processing_template_decl)
2636 expr = build_stmt (input_location, EXPR_STMT, expr);
2637 expr = add_stmt (expr);
2638 /* Mark the last statement so that we can recognize it as such at
2639 template-instantiation time. */
2640 EXPR_STMT_STMT_EXPR_RESULT (expr) = 1;
2642 else if (VOID_TYPE_P (type))
2644 /* Just treat this like an ordinary statement. */
2645 expr = finish_expr_stmt (expr);
2647 else
2649 /* It actually has a value we need to deal with. First, force it
2650 to be an rvalue so that we won't need to build up a copy
2651 constructor call later when we try to assign it to something. */
2652 expr = force_rvalue (expr, tf_warning_or_error);
2653 if (error_operand_p (expr))
2654 return error_mark_node;
2656 /* Update for array-to-pointer decay. */
2657 type = TREE_TYPE (expr);
2659 /* This TARGET_EXPR will initialize the outer one added by
2660 finish_stmt_expr. */
2661 set_target_expr_eliding (expr);
2663 /* Wrap it in a CLEANUP_POINT_EXPR and add it to the list like a
2664 normal statement, but don't convert to void or actually add
2665 the EXPR_STMT. */
2666 if (TREE_CODE (expr) != CLEANUP_POINT_EXPR)
2667 expr = maybe_cleanup_point_expr (expr);
2668 add_stmt (expr);
2671 /* The type of the statement-expression is the type of the last
2672 expression. */
2673 TREE_TYPE (stmt_expr) = type;
2676 return stmt_expr;
2679 /* Finish a statement-expression. EXPR should be the value returned
2680 by the previous begin_stmt_expr. Returns an expression
2681 representing the statement-expression. */
2683 tree
2684 finish_stmt_expr (tree stmt_expr, bool has_no_scope)
2686 tree type;
2687 tree result;
2689 if (error_operand_p (stmt_expr))
2691 pop_stmt_list (stmt_expr);
2692 return error_mark_node;
2695 gcc_assert (TREE_CODE (stmt_expr) == STATEMENT_LIST);
2697 type = TREE_TYPE (stmt_expr);
2698 result = pop_stmt_list (stmt_expr);
2699 TREE_TYPE (result) = type;
2701 if (processing_template_decl)
2703 result = build_min (STMT_EXPR, type, result);
2704 TREE_SIDE_EFFECTS (result) = 1;
2705 STMT_EXPR_NO_SCOPE (result) = has_no_scope;
2707 else if (CLASS_TYPE_P (type))
2709 /* Wrap the statement-expression in a TARGET_EXPR so that the
2710 temporary object created by the final expression is destroyed at
2711 the end of the full-expression containing the
2712 statement-expression. */
2713 result = force_target_expr (type, result, tf_warning_or_error);
2716 return result;
2719 /* Returns the expression which provides the value of STMT_EXPR. */
2721 tree
2722 stmt_expr_value_expr (tree stmt_expr)
2724 tree t = STMT_EXPR_STMT (stmt_expr);
2726 if (TREE_CODE (t) == BIND_EXPR)
2727 t = BIND_EXPR_BODY (t);
2729 if (TREE_CODE (t) == STATEMENT_LIST && STATEMENT_LIST_TAIL (t))
2730 t = STATEMENT_LIST_TAIL (t)->stmt;
2732 if (TREE_CODE (t) == EXPR_STMT)
2733 t = EXPR_STMT_EXPR (t);
2735 return t;
2738 /* Return TRUE iff EXPR_STMT is an empty list of
2739 expression statements. */
2741 bool
2742 empty_expr_stmt_p (tree expr_stmt)
2744 tree body = NULL_TREE;
2746 if (expr_stmt == void_node)
2747 return true;
2749 if (expr_stmt)
2751 if (TREE_CODE (expr_stmt) == EXPR_STMT)
2752 body = EXPR_STMT_EXPR (expr_stmt);
2753 else if (TREE_CODE (expr_stmt) == STATEMENT_LIST)
2754 body = expr_stmt;
2757 if (body)
2759 if (TREE_CODE (body) == STATEMENT_LIST)
2760 return tsi_end_p (tsi_start (body));
2761 else
2762 return empty_expr_stmt_p (body);
2764 return false;
2767 /* Perform Koenig lookup. FN_EXPR is the postfix-expression representing
2768 the function (or functions) to call; ARGS are the arguments to the
2769 call. Returns the functions to be considered by overload resolution. */
2771 cp_expr
2772 perform_koenig_lookup (cp_expr fn_expr, vec<tree, va_gc> *args,
2773 tsubst_flags_t complain)
2775 tree identifier = NULL_TREE;
2776 tree functions = NULL_TREE;
2777 tree tmpl_args = NULL_TREE;
2778 bool template_id = false;
2779 location_t loc = fn_expr.get_location ();
2780 tree fn = fn_expr.get_value ();
2782 STRIP_ANY_LOCATION_WRAPPER (fn);
2784 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
2786 /* Use a separate flag to handle null args. */
2787 template_id = true;
2788 tmpl_args = TREE_OPERAND (fn, 1);
2789 fn = TREE_OPERAND (fn, 0);
2792 /* Find the name of the overloaded function. */
2793 if (identifier_p (fn))
2794 identifier = fn;
2795 else
2797 functions = fn;
2798 identifier = OVL_NAME (functions);
2801 /* A call to a namespace-scope function using an unqualified name.
2803 Do Koenig lookup -- unless any of the arguments are
2804 type-dependent. */
2805 if (!any_type_dependent_arguments_p (args)
2806 && !any_dependent_template_arguments_p (tmpl_args))
2808 fn = lookup_arg_dependent (identifier, functions, args);
2809 if (!fn)
2811 /* The unqualified name could not be resolved. */
2812 if (complain & tf_error)
2813 fn = unqualified_fn_lookup_error (cp_expr (identifier, loc));
2814 else
2815 fn = identifier;
2819 if (fn && template_id && fn != error_mark_node)
2820 fn = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fn, tmpl_args);
2822 return cp_expr (fn, loc);
2825 /* Generate an expression for `FN (ARGS)'. This may change the
2826 contents of ARGS.
2828 If DISALLOW_VIRTUAL is true, the call to FN will be not generated
2829 as a virtual call, even if FN is virtual. (This flag is set when
2830 encountering an expression where the function name is explicitly
2831 qualified. For example a call to `X::f' never generates a virtual
2832 call.)
2834 Returns code for the call. */
2836 tree
2837 finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
2838 bool koenig_p, tsubst_flags_t complain)
2840 tree result;
2841 tree orig_fn;
2842 vec<tree, va_gc> *orig_args = *args;
2844 if (fn == error_mark_node)
2845 return error_mark_node;
2847 gcc_assert (!TYPE_P (fn));
2849 /* If FN may be a FUNCTION_DECL obfuscated by force_paren_expr, undo
2850 it so that we can tell this is a call to a known function. */
2851 fn = maybe_undo_parenthesized_ref (fn);
2853 STRIP_ANY_LOCATION_WRAPPER (fn);
2855 orig_fn = fn;
2857 if (processing_template_decl)
2859 /* If FN is a local extern declaration (or set thereof) in a template,
2860 look it up again at instantiation time. */
2861 if (is_overloaded_fn (fn))
2863 tree ifn = get_first_fn (fn);
2864 if (TREE_CODE (ifn) == FUNCTION_DECL
2865 && dependent_local_decl_p (ifn))
2866 orig_fn = DECL_NAME (ifn);
2869 /* If the call expression is dependent, build a CALL_EXPR node
2870 with no type; type_dependent_expression_p recognizes
2871 expressions with no type as being dependent. */
2872 if (type_dependent_expression_p (fn)
2873 || any_type_dependent_arguments_p (*args))
2875 result = build_min_nt_call_vec (orig_fn, *args);
2876 SET_EXPR_LOCATION (result, cp_expr_loc_or_input_loc (fn));
2877 KOENIG_LOOKUP_P (result) = koenig_p;
2878 /* Disable the std::move warnings since this call was dependent
2879 (c++/89780, c++/107363). This also suppresses the
2880 -Wredundant-move warning. */
2881 suppress_warning (result, OPT_Wpessimizing_move);
2883 if (cfun)
2885 bool abnormal = true;
2886 for (lkp_iterator iter (maybe_get_fns (fn)); iter; ++iter)
2888 tree fndecl = STRIP_TEMPLATE (*iter);
2889 if (TREE_CODE (fndecl) != FUNCTION_DECL
2890 || !TREE_THIS_VOLATILE (fndecl))
2892 abnormal = false;
2893 break;
2896 /* FIXME: Stop warning about falling off end of non-void
2897 function. But this is wrong. Even if we only see
2898 no-return fns at this point, we could select a
2899 future-defined return fn during instantiation. Or
2900 vice-versa. */
2901 if (abnormal)
2902 current_function_returns_abnormally = 1;
2904 if (TREE_CODE (fn) == COMPONENT_REF)
2905 maybe_generic_this_capture (TREE_OPERAND (fn, 0),
2906 TREE_OPERAND (fn, 1));
2907 return result;
2909 orig_args = make_tree_vector_copy (*args);
2912 if (TREE_CODE (fn) == COMPONENT_REF)
2914 tree member = TREE_OPERAND (fn, 1);
2915 if (BASELINK_P (member))
2917 tree object = TREE_OPERAND (fn, 0);
2918 return build_new_method_call (object, member,
2919 args, NULL_TREE,
2920 (disallow_virtual
2921 ? LOOKUP_NORMAL | LOOKUP_NONVIRTUAL
2922 : LOOKUP_NORMAL),
2923 /*fn_p=*/NULL,
2924 complain);
2928 /* Per 13.3.1.1, '(&f)(...)' is the same as '(f)(...)'. */
2929 if (TREE_CODE (fn) == ADDR_EXPR
2930 && TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
2931 fn = TREE_OPERAND (fn, 0);
2933 if (is_overloaded_fn (fn))
2934 fn = baselink_for_fns (fn);
2936 result = NULL_TREE;
2937 if (BASELINK_P (fn))
2939 tree object;
2941 /* A call to a member function. From [over.call.func]:
2943 If the keyword this is in scope and refers to the class of
2944 that member function, or a derived class thereof, then the
2945 function call is transformed into a qualified function call
2946 using (*this) as the postfix-expression to the left of the
2947 . operator.... [Otherwise] a contrived object of type T
2948 becomes the implied object argument.
2950 In this situation:
2952 struct A { void f(); };
2953 struct B : public A {};
2954 struct C : public A { void g() { B::f(); }};
2956 "the class of that member function" refers to `A'. But 11.2
2957 [class.access.base] says that we need to convert 'this' to B* as
2958 part of the access, so we pass 'B' to maybe_dummy_object. */
2960 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
2962 /* A constructor call always uses a dummy object. (This constructor
2963 call which has the form A::A () is actually invalid and we are
2964 going to reject it later in build_new_method_call.) */
2965 object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
2967 else
2968 object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
2969 NULL);
2971 result = build_new_method_call (object, fn, args, NULL_TREE,
2972 (disallow_virtual
2973 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
2974 : LOOKUP_NORMAL),
2975 /*fn_p=*/NULL,
2976 complain);
2978 else if (concept_check_p (fn))
2980 /* FN is actually a template-id referring to a concept definition. */
2981 tree id = unpack_concept_check (fn);
2982 tree tmpl = TREE_OPERAND (id, 0);
2983 tree args = TREE_OPERAND (id, 1);
2985 if (!function_concept_p (tmpl))
2987 error_at (EXPR_LOC_OR_LOC (fn, input_location),
2988 "cannot call a concept as a function");
2989 return error_mark_node;
2992 /* Ensure the result is wrapped as a call expression. */
2993 result = build_concept_check (tmpl, args, tf_warning_or_error);
2995 else if (is_overloaded_fn (fn))
2997 /* If the function is an overloaded builtin, resolve it. */
2998 if (TREE_CODE (fn) == FUNCTION_DECL
2999 && (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL
3000 || DECL_BUILT_IN_CLASS (fn) == BUILT_IN_MD))
3001 result = resolve_overloaded_builtin (input_location, fn, *args);
3003 if (!result)
3005 tree alloc_size_attr = NULL_TREE;
3006 if (warn_calloc_transposed_args
3007 && TREE_CODE (fn) == FUNCTION_DECL
3008 && (alloc_size_attr
3009 = lookup_attribute ("alloc_size",
3010 TYPE_ATTRIBUTES (TREE_TYPE (fn)))))
3011 if (TREE_VALUE (alloc_size_attr) == NULL_TREE
3012 || TREE_CHAIN (TREE_VALUE (alloc_size_attr)) == NULL_TREE)
3013 alloc_size_attr = NULL_TREE;
3014 if ((warn_sizeof_pointer_memaccess || alloc_size_attr)
3015 && (complain & tf_warning)
3016 && !vec_safe_is_empty (*args)
3017 && !processing_template_decl)
3019 location_t sizeof_arg_loc[6];
3020 tree sizeof_arg[6];
3021 unsigned int i;
3022 for (i = 0; i < (alloc_size_attr ? 6 : 3); i++)
3024 tree t;
3026 sizeof_arg_loc[i] = UNKNOWN_LOCATION;
3027 sizeof_arg[i] = NULL_TREE;
3028 if (i >= (*args)->length ())
3029 continue;
3030 t = (**args)[i];
3031 if (TREE_CODE (t) != SIZEOF_EXPR)
3032 continue;
3033 if (SIZEOF_EXPR_TYPE_P (t))
3034 sizeof_arg[i] = TREE_TYPE (TREE_OPERAND (t, 0));
3035 else
3036 sizeof_arg[i] = TREE_OPERAND (t, 0);
3037 sizeof_arg_loc[i] = EXPR_LOCATION (t);
3039 if (warn_sizeof_pointer_memaccess)
3041 auto same_p = same_type_ignoring_top_level_qualifiers_p;
3042 sizeof_pointer_memaccess_warning (sizeof_arg_loc, fn, *args,
3043 sizeof_arg, same_p);
3045 if (alloc_size_attr)
3046 warn_for_calloc (sizeof_arg_loc, fn, *args, sizeof_arg,
3047 alloc_size_attr);
3050 if ((complain & tf_warning)
3051 && TREE_CODE (fn) == FUNCTION_DECL
3052 && fndecl_built_in_p (fn, BUILT_IN_MEMSET)
3053 && vec_safe_length (*args) == 3
3054 && !any_type_dependent_arguments_p (*args))
3056 tree arg0 = (*orig_args)[0];
3057 tree arg1 = (*orig_args)[1];
3058 tree arg2 = (*orig_args)[2];
3059 int literal_mask = ((literal_integer_zerop (arg1) << 1)
3060 | (literal_integer_zerop (arg2) << 2));
3061 warn_for_memset (input_location, arg0, arg2, literal_mask);
3064 /* A call to a namespace-scope function. */
3065 result = build_new_function_call (fn, args, complain);
3068 else if (TREE_CODE (fn) == PSEUDO_DTOR_EXPR)
3070 if (!vec_safe_is_empty (*args))
3071 error ("arguments to destructor are not allowed");
3072 /* C++20/DR: If the postfix-expression names a pseudo-destructor (in
3073 which case the postfix-expression is a possibly-parenthesized class
3074 member access), the function call destroys the object of scalar type
3075 denoted by the object expression of the class member access. */
3076 tree ob = TREE_OPERAND (fn, 0);
3077 if (obvalue_p (ob))
3078 result = build_trivial_dtor_call (ob, true);
3079 else
3080 /* No location to clobber. */
3081 result = convert_to_void (ob, ICV_STATEMENT, complain);
3083 else if (CLASS_TYPE_P (TREE_TYPE (fn)))
3084 /* If the "function" is really an object of class type, it might
3085 have an overloaded `operator ()'. */
3086 result = build_op_call (fn, args, complain);
3088 if (!result)
3089 /* A call where the function is unknown. */
3090 result = cp_build_function_call_vec (fn, args, complain);
3092 if (processing_template_decl && result != error_mark_node)
3094 if (INDIRECT_REF_P (result))
3095 result = TREE_OPERAND (result, 0);
3097 /* Prune all but the selected function from the original overload
3098 set so that we can avoid some duplicate work at instantiation time. */
3099 if (TREE_CODE (result) == CALL_EXPR
3100 && really_overloaded_fn (orig_fn))
3102 tree sel_fn = CALL_EXPR_FN (result);
3103 if (TREE_CODE (sel_fn) == COMPONENT_REF)
3105 /* The non-dependent result of build_new_method_call. */
3106 sel_fn = TREE_OPERAND (sel_fn, 1);
3107 gcc_assert (BASELINK_P (sel_fn));
3109 else if (TREE_CODE (sel_fn) == ADDR_EXPR)
3110 /* Our original callee wasn't wrapped in an ADDR_EXPR,
3111 so strip this ADDR_EXPR added by build_over_call. */
3112 sel_fn = TREE_OPERAND (sel_fn, 0);
3113 orig_fn = sel_fn;
3116 result = build_call_vec (TREE_TYPE (result), orig_fn, orig_args);
3117 SET_EXPR_LOCATION (result, input_location);
3118 KOENIG_LOOKUP_P (result) = koenig_p;
3119 release_tree_vector (orig_args);
3120 result = convert_from_reference (result);
3123 return result;
3126 /* Finish a call to a postfix increment or decrement or EXPR. (Which
3127 is indicated by CODE, which should be POSTINCREMENT_EXPR or
3128 POSTDECREMENT_EXPR.) */
3130 cp_expr
3131 finish_increment_expr (cp_expr expr, enum tree_code code)
3133 /* input_location holds the location of the trailing operator token.
3134 Build a location of the form:
3135 expr++
3136 ~~~~^~
3137 with the caret at the operator token, ranging from the start
3138 of EXPR to the end of the operator token. */
3139 location_t combined_loc = make_location (input_location,
3140 expr.get_start (),
3141 get_finish (input_location));
3142 cp_expr result = build_x_unary_op (combined_loc, code, expr,
3143 NULL_TREE, tf_warning_or_error);
3144 /* TODO: build_x_unary_op doesn't honor the location, so set it here. */
3145 result.set_location (combined_loc);
3146 return result;
3149 /* Finish a use of `this'. Returns an expression for `this'. */
3151 tree
3152 finish_this_expr (void)
3154 tree result = NULL_TREE;
3156 if (current_class_ptr)
3158 tree type = TREE_TYPE (current_class_ref);
3160 /* In a lambda expression, 'this' refers to the captured 'this'. */
3161 if (LAMBDA_TYPE_P (type))
3162 result = lambda_expr_this_capture (CLASSTYPE_LAMBDA_EXPR (type), true);
3163 else
3164 result = current_class_ptr;
3167 if (result)
3168 /* The keyword 'this' is a prvalue expression. */
3169 return rvalue (result);
3171 tree fn = current_nonlambda_function ();
3172 if (fn && DECL_XOBJ_MEMBER_FUNCTION_P (fn))
3174 auto_diagnostic_group d;
3175 error ("%<this%> is unavailable for explicit object member "
3176 "functions");
3177 tree xobj_parm = DECL_ARGUMENTS (fn);
3178 gcc_assert (xobj_parm);
3179 tree parm_name = DECL_NAME (xobj_parm);
3181 static tree remembered_fn = NULL_TREE;
3182 /* Only output this diagnostic once per function. */
3183 if (remembered_fn == fn)
3184 /* Early escape. */;
3185 else if (parm_name)
3186 inform (DECL_SOURCE_LOCATION (xobj_parm),
3187 "use explicit object parameter %qs instead",
3188 IDENTIFIER_POINTER (parm_name));
3189 else
3190 inform (DECL_SOURCE_LOCATION (xobj_parm),
3191 "name the explicit object parameter");
3193 remembered_fn = fn;
3195 else if (fn && DECL_STATIC_FUNCTION_P (fn))
3196 error ("%<this%> is unavailable for static member functions");
3197 else if (fn && processing_contract_condition && DECL_CONSTRUCTOR_P (fn))
3198 error ("invalid use of %<this%> before it is valid");
3199 else if (fn && processing_contract_condition && DECL_DESTRUCTOR_P (fn))
3200 error ("invalid use of %<this%> after it is valid");
3201 else if (fn)
3202 error ("invalid use of %<this%> in non-member function");
3203 else
3204 error ("invalid use of %<this%> at top level");
3205 return error_mark_node;
3208 /* Finish a pseudo-destructor expression. If SCOPE is NULL, the
3209 expression was of the form `OBJECT.~DESTRUCTOR' where DESTRUCTOR is
3210 the TYPE for the type given. If SCOPE is non-NULL, the expression
3211 was of the form `OBJECT.SCOPE::~DESTRUCTOR'. */
3213 tree
3214 finish_pseudo_destructor_expr (tree object, tree scope, tree destructor,
3215 location_t loc)
3217 if (object == error_mark_node || destructor == error_mark_node)
3218 return error_mark_node;
3220 gcc_assert (TYPE_P (destructor));
3222 if (!processing_template_decl)
3224 if (scope == error_mark_node)
3226 error_at (loc, "invalid qualifying scope in pseudo-destructor name");
3227 return error_mark_node;
3229 if (is_auto (destructor))
3230 destructor = TREE_TYPE (object);
3231 if (scope && TYPE_P (scope) && !check_dtor_name (scope, destructor))
3233 error_at (loc,
3234 "qualified type %qT does not match destructor name ~%qT",
3235 scope, destructor);
3236 return error_mark_node;
3240 /* [expr.pseudo] says both:
3242 The type designated by the pseudo-destructor-name shall be
3243 the same as the object type.
3245 and:
3247 The cv-unqualified versions of the object type and of the
3248 type designated by the pseudo-destructor-name shall be the
3249 same type.
3251 We implement the more generous second sentence, since that is
3252 what most other compilers do. */
3253 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (object),
3254 destructor))
3256 error_at (loc, "%qE is not of type %qT", object, destructor);
3257 return error_mark_node;
3261 tree type = (type_dependent_expression_p (object)
3262 ? NULL_TREE : void_type_node);
3264 return build3_loc (loc, PSEUDO_DTOR_EXPR, type, object,
3265 scope, destructor);
3268 /* Finish an expression of the form CODE EXPR. */
3270 cp_expr
3271 finish_unary_op_expr (location_t op_loc, enum tree_code code, cp_expr expr,
3272 tsubst_flags_t complain)
3274 /* Build a location of the form:
3275 ++expr
3276 ^~~~~~
3277 with the caret at the operator token, ranging from the start
3278 of the operator token to the end of EXPR. */
3279 location_t combined_loc = make_location (op_loc,
3280 op_loc, expr.get_finish ());
3281 cp_expr result = build_x_unary_op (combined_loc, code, expr,
3282 NULL_TREE, complain);
3283 /* TODO: build_x_unary_op doesn't always honor the location. */
3284 result.set_location (combined_loc);
3286 if (result == error_mark_node)
3287 return result;
3289 if (!(complain & tf_warning))
3290 return result;
3292 tree result_ovl = result;
3293 tree expr_ovl = expr;
3295 if (!processing_template_decl)
3296 expr_ovl = cp_fully_fold (expr_ovl);
3298 if (!CONSTANT_CLASS_P (expr_ovl)
3299 || TREE_OVERFLOW_P (expr_ovl))
3300 return result;
3302 if (!processing_template_decl)
3303 result_ovl = cp_fully_fold (result_ovl);
3305 if (CONSTANT_CLASS_P (result_ovl) && TREE_OVERFLOW_P (result_ovl))
3306 overflow_warning (combined_loc, result_ovl);
3308 return result;
3311 /* Return true if CONSTRUCTOR EXPR after pack expansion could have no
3312 elements. */
3314 static bool
3315 maybe_zero_constructor_nelts (tree expr)
3317 if (CONSTRUCTOR_NELTS (expr) == 0)
3318 return true;
3319 if (!processing_template_decl)
3320 return false;
3321 for (constructor_elt &elt : CONSTRUCTOR_ELTS (expr))
3322 if (!PACK_EXPANSION_P (elt.value))
3323 return false;
3324 return true;
3327 /* Finish a compound-literal expression or C++11 functional cast with aggregate
3328 initializer. TYPE is the type to which the CONSTRUCTOR in COMPOUND_LITERAL
3329 is being cast. */
3331 tree
3332 finish_compound_literal (tree type, tree compound_literal,
3333 tsubst_flags_t complain,
3334 fcl_t fcl_context)
3336 if (type == error_mark_node)
3337 return error_mark_node;
3339 if (TYPE_REF_P (type))
3341 compound_literal
3342 = finish_compound_literal (TREE_TYPE (type), compound_literal,
3343 complain, fcl_context);
3344 /* The prvalue is then used to direct-initialize the reference. */
3345 tree r = (perform_implicit_conversion_flags
3346 (type, compound_literal, complain, LOOKUP_NORMAL));
3347 return convert_from_reference (r);
3350 if (!TYPE_OBJ_P (type))
3352 /* DR2351 */
3353 if (VOID_TYPE_P (type) && CONSTRUCTOR_NELTS (compound_literal) == 0)
3355 if (!processing_template_decl)
3356 return void_node;
3357 TREE_TYPE (compound_literal) = type;
3358 TREE_HAS_CONSTRUCTOR (compound_literal) = 1;
3359 CONSTRUCTOR_IS_DEPENDENT (compound_literal) = 0;
3360 return compound_literal;
3362 else if (VOID_TYPE_P (type)
3363 && processing_template_decl
3364 && maybe_zero_constructor_nelts (compound_literal))
3365 /* If there are only packs in compound_literal, it could
3366 be void{} after pack expansion. */;
3367 else
3369 if (complain & tf_error)
3370 error ("compound literal of non-object type %qT", type);
3371 return error_mark_node;
3375 if (template_placeholder_p (type))
3377 type = do_auto_deduction (type, compound_literal, type, complain,
3378 adc_variable_type);
3379 if (type == error_mark_node)
3380 return error_mark_node;
3382 /* C++23 auto{x}. */
3383 else if (is_auto (type)
3384 && !AUTO_IS_DECLTYPE (type)
3385 && CONSTRUCTOR_NELTS (compound_literal) == 1)
3387 if (is_constrained_auto (type))
3389 if (complain & tf_error)
3390 error ("%<auto{x}%> cannot be constrained");
3391 return error_mark_node;
3393 else if (cxx_dialect < cxx23)
3394 pedwarn (input_location, OPT_Wc__23_extensions,
3395 "%<auto{x}%> only available with "
3396 "%<-std=c++2b%> or %<-std=gnu++2b%>");
3397 type = do_auto_deduction (type, compound_literal, type, complain,
3398 adc_variable_type);
3399 if (type == error_mark_node)
3400 return error_mark_node;
3403 /* Used to hold a copy of the compound literal in a template. */
3404 tree orig_cl = NULL_TREE;
3406 if (processing_template_decl)
3408 const bool dependent_p
3409 = (instantiation_dependent_expression_p (compound_literal)
3410 || dependent_type_p (type));
3411 if (dependent_p)
3412 /* We're about to return, no need to copy. */
3413 orig_cl = compound_literal;
3414 else
3415 /* We're going to need a copy. */
3416 orig_cl = unshare_constructor (compound_literal);
3417 TREE_TYPE (orig_cl) = type;
3418 /* Mark the expression as a compound literal. */
3419 TREE_HAS_CONSTRUCTOR (orig_cl) = 1;
3420 /* And as instantiation-dependent. */
3421 CONSTRUCTOR_IS_DEPENDENT (orig_cl) = dependent_p;
3422 if (fcl_context == fcl_c99)
3423 CONSTRUCTOR_C99_COMPOUND_LITERAL (orig_cl) = 1;
3424 /* If the compound literal is dependent, we're done for now. */
3425 if (dependent_p)
3426 return orig_cl;
3427 /* Otherwise, do go on to e.g. check narrowing. */
3430 type = complete_type (type);
3432 if (TYPE_NON_AGGREGATE_CLASS (type))
3434 /* Trying to deal with a CONSTRUCTOR instead of a TREE_LIST
3435 everywhere that deals with function arguments would be a pain, so
3436 just wrap it in a TREE_LIST. The parser set a flag so we know
3437 that it came from T{} rather than T({}). */
3438 CONSTRUCTOR_IS_DIRECT_INIT (compound_literal) = 1;
3439 compound_literal = build_tree_list (NULL_TREE, compound_literal);
3440 return build_functional_cast (input_location, type,
3441 compound_literal, complain);
3444 if (TREE_CODE (type) == ARRAY_TYPE
3445 && check_array_initializer (NULL_TREE, type, compound_literal))
3446 return error_mark_node;
3447 compound_literal = reshape_init (type, compound_literal, complain);
3448 if (SCALAR_TYPE_P (type)
3449 && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
3450 && !check_narrowing (type, compound_literal, complain))
3451 return error_mark_node;
3452 if (TREE_CODE (type) == ARRAY_TYPE
3453 && TYPE_DOMAIN (type) == NULL_TREE)
3455 cp_complete_array_type_or_error (&type, compound_literal,
3456 false, complain);
3457 if (type == error_mark_node)
3458 return error_mark_node;
3460 compound_literal = digest_init_flags (type, compound_literal,
3461 LOOKUP_NORMAL | LOOKUP_NO_NARROWING,
3462 complain);
3463 if (compound_literal == error_mark_node)
3464 return error_mark_node;
3466 /* If we're in a template, return the original compound literal. */
3467 if (orig_cl)
3468 return orig_cl;
3470 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
3472 TREE_HAS_CONSTRUCTOR (compound_literal) = true;
3473 if (fcl_context == fcl_c99)
3474 CONSTRUCTOR_C99_COMPOUND_LITERAL (compound_literal) = 1;
3477 /* Put static/constant array temporaries in static variables. */
3478 /* FIXME all C99 compound literals should be variables rather than C++
3479 temporaries, unless they are used as an aggregate initializer. */
3480 if ((!at_function_scope_p () || CP_TYPE_CONST_P (type))
3481 && fcl_context == fcl_c99
3482 && TREE_CODE (type) == ARRAY_TYPE
3483 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
3484 && initializer_constant_valid_p (compound_literal, type))
3486 tree decl = create_temporary_var (type);
3487 DECL_CONTEXT (decl) = NULL_TREE;
3488 DECL_INITIAL (decl) = compound_literal;
3489 TREE_STATIC (decl) = 1;
3490 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
3492 /* 5.19 says that a constant expression can include an
3493 lvalue-rvalue conversion applied to "a glvalue of literal type
3494 that refers to a non-volatile temporary object initialized
3495 with a constant expression". Rather than try to communicate
3496 that this VAR_DECL is a temporary, just mark it constexpr. */
3497 DECL_DECLARED_CONSTEXPR_P (decl) = true;
3498 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
3499 TREE_CONSTANT (decl) = true;
3501 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
3502 decl = pushdecl_top_level (decl);
3503 DECL_NAME (decl) = make_anon_name ();
3504 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
3505 /* Make sure the destructor is callable. */
3506 tree clean = cxx_maybe_build_cleanup (decl, complain);
3507 if (clean == error_mark_node)
3508 return error_mark_node;
3509 return decl;
3512 /* Represent other compound literals with TARGET_EXPR so we produce
3513 a prvalue, and can elide copies. */
3514 if (!VECTOR_TYPE_P (type)
3515 && (TREE_CODE (compound_literal) == CONSTRUCTOR
3516 || TREE_CODE (compound_literal) == VEC_INIT_EXPR))
3518 /* The CONSTRUCTOR is now an initializer, not a compound literal. */
3519 if (TREE_CODE (compound_literal) == CONSTRUCTOR)
3520 TREE_HAS_CONSTRUCTOR (compound_literal) = false;
3521 compound_literal = get_target_expr (compound_literal, complain);
3523 else
3524 /* For e.g. int{42} just make sure it's a prvalue. */
3525 compound_literal = rvalue (compound_literal);
3527 return compound_literal;
3530 /* Return the declaration for the function-name variable indicated by
3531 ID. */
3533 tree
3534 finish_fname (tree id)
3536 tree decl;
3538 decl = fname_decl (input_location, C_RID_CODE (id), id);
3539 if (processing_template_decl && current_function_decl
3540 && decl != error_mark_node)
3541 decl = DECL_NAME (decl);
3542 return decl;
3545 /* Finish a translation unit. */
3547 void
3548 finish_translation_unit (void)
3550 /* In case there were missing closebraces,
3551 get us back to the global binding level. */
3552 pop_everything ();
3553 while (current_namespace != global_namespace)
3554 pop_namespace ();
3556 /* Do file scope __FUNCTION__ et al. */
3557 finish_fname_decls ();
3559 if (vec_safe_length (scope_chain->omp_declare_target_attribute))
3561 cp_omp_declare_target_attr
3562 a = scope_chain->omp_declare_target_attribute->pop ();
3563 if (!errorcount)
3564 error ("%qs without corresponding %qs",
3565 a.device_type >= 0 ? "#pragma omp begin declare target"
3566 : "#pragma omp declare target",
3567 "#pragma omp end declare target");
3568 vec_safe_truncate (scope_chain->omp_declare_target_attribute, 0);
3570 if (vec_safe_length (scope_chain->omp_begin_assumes))
3572 if (!errorcount)
3573 error ("%qs without corresponding %qs",
3574 "#pragma omp begin assumes", "#pragma omp end assumes");
3575 vec_safe_truncate (scope_chain->omp_begin_assumes, 0);
3579 /* Finish a template type parameter, specified as AGGR IDENTIFIER.
3580 Returns the parameter. */
3582 tree
3583 finish_template_type_parm (tree aggr, tree identifier)
3585 if (aggr != class_type_node)
3587 permerror (input_location, "template type parameters must use the keyword %<class%> or %<typename%>");
3588 aggr = class_type_node;
3591 return build_tree_list (aggr, identifier);
3594 /* Finish a template template parameter, specified as AGGR IDENTIFIER.
3595 Returns the parameter. */
3597 tree
3598 finish_template_template_parm (tree aggr, tree identifier)
3600 tree decl = build_decl (input_location,
3601 TYPE_DECL, identifier, NULL_TREE);
3603 tree tmpl = build_lang_decl (TEMPLATE_DECL, identifier, NULL_TREE);
3604 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
3605 DECL_TEMPLATE_RESULT (tmpl) = decl;
3606 DECL_ARTIFICIAL (decl) = 1;
3608 /* Associate the constraints with the underlying declaration,
3609 not the template. */
3610 tree constr = current_template_constraints ();
3611 set_constraints (decl, constr);
3613 end_template_decl ();
3615 gcc_assert (DECL_TEMPLATE_PARMS (tmpl));
3617 check_default_tmpl_args (decl, DECL_TEMPLATE_PARMS (tmpl),
3618 /*is_primary=*/true, /*is_partial=*/false,
3619 /*is_friend=*/0);
3621 return finish_template_type_parm (aggr, tmpl);
3624 /* ARGUMENT is the default-argument value for a template template
3625 parameter. If ARGUMENT is invalid, issue error messages and return
3626 the ERROR_MARK_NODE. Otherwise, ARGUMENT itself is returned. */
3628 tree
3629 check_template_template_default_arg (tree argument)
3631 if (TREE_CODE (argument) != TEMPLATE_DECL
3632 && TREE_CODE (argument) != TEMPLATE_TEMPLATE_PARM
3633 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
3635 if (TREE_CODE (argument) == TYPE_DECL)
3637 if (tree t = maybe_get_template_decl_from_type_decl (argument))
3638 if (TREE_CODE (t) == TEMPLATE_DECL)
3639 return t;
3640 error ("invalid use of type %qT as a default value for a template "
3641 "template-parameter", TREE_TYPE (argument));
3643 else
3644 error ("invalid default argument for a template template parameter");
3645 return error_mark_node;
3648 return argument;
3651 /* Begin a class definition, as indicated by T. */
3653 tree
3654 begin_class_definition (tree t)
3656 if (error_operand_p (t) || error_operand_p (TYPE_MAIN_DECL (t)))
3657 return error_mark_node;
3659 if (processing_template_parmlist && !LAMBDA_TYPE_P (t))
3661 error ("definition of %q#T inside template parameter list", t);
3662 return error_mark_node;
3665 /* According to the C++ ABI, decimal classes defined in ISO/IEC TR 24733
3666 are passed the same as decimal scalar types. */
3667 if (TREE_CODE (t) == RECORD_TYPE
3668 && !processing_template_decl)
3670 tree ns = TYPE_CONTEXT (t);
3671 if (ns && TREE_CODE (ns) == NAMESPACE_DECL
3672 && DECL_CONTEXT (ns) == std_node
3673 && DECL_NAME (ns)
3674 && id_equal (DECL_NAME (ns), "decimal"))
3676 const char *n = TYPE_NAME_STRING (t);
3677 if ((strcmp (n, "decimal32") == 0)
3678 || (strcmp (n, "decimal64") == 0)
3679 || (strcmp (n, "decimal128") == 0))
3680 TYPE_TRANSPARENT_AGGR (t) = 1;
3684 /* A non-implicit typename comes from code like:
3686 template <typename T> struct A {
3687 template <typename U> struct A<T>::B ...
3689 This is erroneous. */
3690 else if (TREE_CODE (t) == TYPENAME_TYPE)
3692 error ("invalid definition of qualified type %qT", t);
3693 t = error_mark_node;
3696 if (t == error_mark_node || ! MAYBE_CLASS_TYPE_P (t))
3698 t = make_class_type (RECORD_TYPE);
3699 pushtag (make_anon_name (), t);
3702 if (TYPE_BEING_DEFINED (t))
3704 t = make_class_type (TREE_CODE (t));
3705 pushtag (TYPE_IDENTIFIER (t), t);
3708 if (modules_p ())
3710 if (!module_may_redeclare (TYPE_NAME (t)))
3712 error ("cannot declare %qD in a different module", TYPE_NAME (t));
3713 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "declared here");
3714 return error_mark_node;
3716 set_instantiating_module (TYPE_NAME (t));
3717 set_defining_module (TYPE_NAME (t));
3720 maybe_process_partial_specialization (t);
3721 pushclass (t);
3722 TYPE_BEING_DEFINED (t) = 1;
3723 class_binding_level->defining_class_p = 1;
3725 if (flag_pack_struct)
3727 tree v;
3728 TYPE_PACKED (t) = 1;
3729 /* Even though the type is being defined for the first time
3730 here, there might have been a forward declaration, so there
3731 might be cv-qualified variants of T. */
3732 for (v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
3733 TYPE_PACKED (v) = 1;
3735 /* Reset the interface data, at the earliest possible
3736 moment, as it might have been set via a class foo;
3737 before. */
3738 if (! TYPE_UNNAMED_P (t))
3740 struct c_fileinfo *finfo = \
3741 get_fileinfo (LOCATION_FILE (input_location));
3742 CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
3743 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
3744 (t, finfo->interface_unknown);
3746 reset_specialization ();
3748 /* Make a declaration for this class in its own scope. */
3749 build_self_reference ();
3751 return t;
3754 /* Finish the member declaration given by DECL. */
3756 void
3757 finish_member_declaration (tree decl)
3759 if (decl == error_mark_node || decl == NULL_TREE)
3760 return;
3762 if (decl == void_type_node)
3763 /* The COMPONENT was a friend, not a member, and so there's
3764 nothing for us to do. */
3765 return;
3767 /* We should see only one DECL at a time. */
3768 gcc_assert (DECL_CHAIN (decl) == NULL_TREE);
3770 /* Don't add decls after definition. */
3771 gcc_assert (TYPE_BEING_DEFINED (current_class_type)
3772 /* We can add lambda types when late parsing default
3773 arguments. */
3774 || LAMBDA_TYPE_P (TREE_TYPE (decl)));
3776 /* Set up access control for DECL. */
3777 TREE_PRIVATE (decl)
3778 = (current_access_specifier == access_private_node);
3779 TREE_PROTECTED (decl)
3780 = (current_access_specifier == access_protected_node);
3781 if (TREE_CODE (decl) == TEMPLATE_DECL)
3783 TREE_PRIVATE (DECL_TEMPLATE_RESULT (decl)) = TREE_PRIVATE (decl);
3784 TREE_PROTECTED (DECL_TEMPLATE_RESULT (decl)) = TREE_PROTECTED (decl);
3787 /* Mark the DECL as a member of the current class, unless it's
3788 a member of an enumeration. */
3789 if (TREE_CODE (decl) != CONST_DECL)
3790 DECL_CONTEXT (decl) = current_class_type;
3792 /* Remember the single FIELD_DECL an anonymous aggregate type is used for. */
3793 if (TREE_CODE (decl) == FIELD_DECL
3794 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
3796 gcc_assert (!ANON_AGGR_TYPE_FIELD (TYPE_MAIN_VARIANT (TREE_TYPE (decl))));
3797 ANON_AGGR_TYPE_FIELD (TYPE_MAIN_VARIANT (TREE_TYPE (decl))) = decl;
3800 if (TREE_CODE (decl) == USING_DECL)
3801 /* Avoid debug info for class-scope USING_DECLS for now, we'll
3802 call cp_emit_debug_info_for_using later. */
3803 DECL_IGNORED_P (decl) = 1;
3805 /* Check for bare parameter packs in the non-static data member
3806 declaration. */
3807 if (TREE_CODE (decl) == FIELD_DECL)
3809 if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
3810 TREE_TYPE (decl) = error_mark_node;
3811 if (check_for_bare_parameter_packs (DECL_ATTRIBUTES (decl)))
3812 DECL_ATTRIBUTES (decl) = NULL_TREE;
3815 /* [dcl.link]
3817 A C language linkage is ignored for the names of class members
3818 and the member function type of class member functions. */
3819 if (DECL_LANG_SPECIFIC (decl))
3820 SET_DECL_LANGUAGE (decl, lang_cplusplus);
3822 bool add = false;
3824 /* Functions and non-functions are added differently. */
3825 if (DECL_DECLARES_FUNCTION_P (decl))
3826 add = add_method (current_class_type, decl, false);
3827 /* Enter the DECL into the scope of the class, if the class
3828 isn't a closure (whose fields are supposed to be unnamed). */
3829 else if (CLASSTYPE_LAMBDA_EXPR (current_class_type)
3830 || maybe_push_used_methods (decl)
3831 || pushdecl_class_level (decl))
3832 add = true;
3834 if (add)
3836 /* All TYPE_DECLs go at the end of TYPE_FIELDS. Ordinary fields
3837 go at the beginning. The reason is that
3838 legacy_nonfn_member_lookup searches the list in order, and we
3839 want a field name to override a type name so that the "struct
3840 stat hack" will work. In particular:
3842 struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
3844 is valid. */
3846 if (TREE_CODE (decl) == TYPE_DECL)
3847 TYPE_FIELDS (current_class_type)
3848 = chainon (TYPE_FIELDS (current_class_type), decl);
3849 else
3851 DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
3852 TYPE_FIELDS (current_class_type) = decl;
3855 maybe_add_class_template_decl_list (current_class_type, decl,
3856 /*friend_p=*/0);
3860 /* Finish processing a complete template declaration. The PARMS are
3861 the template parameters. */
3863 void
3864 finish_template_decl (tree parms)
3866 if (parms)
3867 end_template_decl ();
3868 else
3869 end_specialization ();
3872 // Returns the template type of the class scope being entered. If we're
3873 // entering a constrained class scope. TYPE is the class template
3874 // scope being entered and we may need to match the intended type with
3875 // a constrained specialization. For example:
3877 // template<Object T>
3878 // struct S { void f(); }; #1
3880 // template<Object T>
3881 // void S<T>::f() { } #2
3883 // We check, in #2, that S<T> refers precisely to the type declared by
3884 // #1 (i.e., that the constraints match). Note that the following should
3885 // be an error since there is no specialization of S<T> that is
3886 // unconstrained, but this is not diagnosed here.
3888 // template<typename T>
3889 // void S<T>::f() { }
3891 // We cannot diagnose this problem here since this function also matches
3892 // qualified template names that are not part of a definition. For example:
3894 // template<Integral T, Floating_point U>
3895 // typename pair<T, U>::first_type void f(T, U);
3897 // Here, it is unlikely that there is a partial specialization of
3898 // pair constrained for for Integral and Floating_point arguments.
3900 // The general rule is: if a constrained specialization with matching
3901 // constraints is found return that type. Also note that if TYPE is not a
3902 // class-type (e.g. a typename type), then no fixup is needed.
3904 static tree
3905 fixup_template_type (tree type)
3907 // Find the template parameter list at the a depth appropriate to
3908 // the scope we're trying to enter.
3909 tree parms = current_template_parms;
3910 int depth = template_class_depth (type);
3911 for (int n = current_template_depth; n > depth && parms; --n)
3912 parms = TREE_CHAIN (parms);
3913 if (!parms)
3914 return type;
3915 tree cur_reqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
3916 tree cur_constr = build_constraints (cur_reqs, NULL_TREE);
3918 // Search for a specialization whose type and constraints match.
3919 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
3920 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3921 while (specs)
3923 tree spec_constr = get_constraints (TREE_VALUE (specs));
3925 // If the type and constraints match a specialization, then we
3926 // are entering that type.
3927 if (same_type_p (type, TREE_TYPE (specs))
3928 && equivalent_constraints (cur_constr, spec_constr))
3929 return TREE_TYPE (specs);
3930 specs = TREE_CHAIN (specs);
3933 // If no specialization matches, then must return the type
3934 // previously found.
3935 return type;
3938 /* Finish processing a template-id (which names a type) of the form
3939 NAME < ARGS >. Return the TYPE_DECL for the type named by the
3940 template-id. If ENTERING_SCOPE is nonzero we are about to enter
3941 the scope of template-id indicated. */
3943 tree
3944 finish_template_type (tree name, tree args, int entering_scope)
3946 tree type;
3948 type = lookup_template_class (name, args,
3949 NULL_TREE, NULL_TREE, entering_scope,
3950 tf_warning_or_error | tf_user);
3952 /* If we might be entering the scope of a partial specialization,
3953 find the one with the right constraints. */
3954 if (flag_concepts
3955 && entering_scope
3956 && CLASS_TYPE_P (type)
3957 && CLASSTYPE_TEMPLATE_INFO (type)
3958 && dependent_type_p (type)
3959 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
3960 type = fixup_template_type (type);
3962 if (type == error_mark_node)
3963 return type;
3964 else if (CLASS_TYPE_P (type) && !alias_type_or_template_p (type))
3965 return TYPE_STUB_DECL (type);
3966 else
3967 return TYPE_NAME (type);
3970 /* Finish processing a BASE_CLASS with the indicated ACCESS_SPECIFIER.
3971 Return a TREE_LIST containing the ACCESS_SPECIFIER and the
3972 BASE_CLASS, or NULL_TREE if an error occurred. The
3973 ACCESS_SPECIFIER is one of
3974 access_{default,public,protected_private}_node. For a virtual base
3975 we set TREE_TYPE. */
3977 tree
3978 finish_base_specifier (tree base, tree access, bool virtual_p)
3980 tree result;
3982 if (base == error_mark_node)
3984 error ("invalid base-class specification");
3985 result = NULL_TREE;
3987 else if (! MAYBE_CLASS_TYPE_P (base))
3989 error ("%qT is not a class type", base);
3990 result = NULL_TREE;
3992 else
3994 if (cp_type_quals (base) != 0)
3996 /* DR 484: Can a base-specifier name a cv-qualified
3997 class type? */
3998 base = TYPE_MAIN_VARIANT (base);
4000 result = build_tree_list (access, base);
4001 if (virtual_p)
4002 TREE_TYPE (result) = integer_type_node;
4005 return result;
4008 /* If FNS is a member function, a set of member functions, or a
4009 template-id referring to one or more member functions, return a
4010 BASELINK for FNS, incorporating the current access context.
4011 Otherwise, return FNS unchanged. */
4013 tree
4014 baselink_for_fns (tree fns)
4016 tree scope;
4017 tree cl;
4019 if (BASELINK_P (fns)
4020 || error_operand_p (fns))
4021 return fns;
4023 scope = ovl_scope (fns);
4024 if (!CLASS_TYPE_P (scope))
4025 return fns;
4027 cl = currently_open_derived_class (scope);
4028 if (!cl)
4029 cl = scope;
4030 tree access_path = TYPE_BINFO (cl);
4031 tree conv_path = (cl == scope ? access_path
4032 : lookup_base (cl, scope, ba_any, NULL, tf_none));
4033 return build_baselink (conv_path, access_path, fns, /*optype=*/NULL_TREE);
4036 /* Returns true iff DECL is a variable from a function outside
4037 the current one. */
4039 static bool
4040 outer_var_p (tree decl)
4042 /* These should have been stripped or otherwise handled by the caller. */
4043 gcc_checking_assert (!REFERENCE_REF_P (decl));
4045 return ((VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
4046 && DECL_FUNCTION_SCOPE_P (decl)
4047 /* Don't get confused by temporaries. */
4048 && DECL_NAME (decl)
4049 && (DECL_CONTEXT (decl) != current_function_decl
4050 || parsing_nsdmi ()));
4053 /* As above, but also checks that DECL is automatic. */
4055 bool
4056 outer_automatic_var_p (tree decl)
4058 return (outer_var_p (decl)
4059 && !TREE_STATIC (decl));
4062 /* DECL satisfies outer_automatic_var_p. Possibly complain about it or
4063 rewrite it for lambda capture.
4065 If ODR_USE is true, we're being called from mark_use, and we complain about
4066 use of constant variables. If ODR_USE is false, we're being called for the
4067 id-expression, and we do lambda capture. */
4069 tree
4070 process_outer_var_ref (tree decl, tsubst_flags_t complain, bool odr_use)
4072 if (cp_unevaluated_operand)
4074 tree type = TREE_TYPE (decl);
4075 if (!dependent_type_p (type)
4076 && variably_modified_type_p (type, NULL_TREE))
4077 /* VLAs are used even in unevaluated context. */;
4078 else
4079 /* It's not a use (3.2) if we're in an unevaluated context. */
4080 return decl;
4082 if (decl == error_mark_node)
4083 return decl;
4085 tree context = DECL_CONTEXT (decl);
4086 tree containing_function = current_function_decl;
4087 tree lambda_stack = NULL_TREE;
4088 tree lambda_expr = NULL_TREE;
4089 tree initializer = convert_from_reference (decl);
4091 /* Mark it as used now even if the use is ill-formed. */
4092 if (!mark_used (decl, complain))
4093 return error_mark_node;
4095 if (parsing_nsdmi ())
4096 containing_function = NULL_TREE;
4098 if (containing_function && LAMBDA_FUNCTION_P (containing_function))
4100 /* Check whether we've already built a proxy. */
4101 tree var = decl;
4102 while (is_normal_capture_proxy (var))
4103 var = DECL_CAPTURED_VARIABLE (var);
4104 tree d = retrieve_local_specialization (var);
4106 if (d && d != decl && is_capture_proxy (d))
4108 if (DECL_CONTEXT (d) == containing_function)
4109 /* We already have an inner proxy. */
4110 return d;
4111 else
4112 /* We need to capture an outer proxy. */
4113 return process_outer_var_ref (d, complain, odr_use);
4117 /* If we are in a lambda function, we can move out until we hit
4118 1. the context,
4119 2. a non-lambda function, or
4120 3. a non-default capturing lambda function. */
4121 while (context != containing_function
4122 /* containing_function can be null with invalid generic lambdas. */
4123 && containing_function
4124 && LAMBDA_FUNCTION_P (containing_function))
4126 tree closure = DECL_CONTEXT (containing_function);
4127 lambda_expr = CLASSTYPE_LAMBDA_EXPR (closure);
4129 if (TYPE_CLASS_SCOPE_P (closure))
4130 /* A lambda in an NSDMI (c++/64496). */
4131 break;
4133 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
4134 break;
4136 lambda_stack = tree_cons (NULL_TREE, lambda_expr, lambda_stack);
4138 containing_function = decl_function_context (containing_function);
4141 /* In a lambda within a template, wait until instantiation time to implicitly
4142 capture a parameter pack. We want to wait because we don't know if we're
4143 capturing the whole pack or a single element, and it's OK to wait because
4144 find_parameter_packs_r walks into the lambda body. */
4145 if (context == containing_function
4146 && DECL_PACK_P (decl))
4147 return decl;
4149 if (lambda_expr && VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl))
4151 if (complain & tf_error)
4152 error ("cannot capture member %qD of anonymous union", decl);
4153 return error_mark_node;
4155 /* Do lambda capture when processing the id-expression, not when
4156 odr-using a variable. */
4157 if (!odr_use && context == containing_function)
4158 decl = add_default_capture (lambda_stack,
4159 /*id=*/DECL_NAME (decl), initializer);
4160 /* Only an odr-use of an outer automatic variable causes an
4161 error, and a constant variable can decay to a prvalue
4162 constant without odr-use. So don't complain yet. */
4163 else if (!odr_use && decl_constant_var_p (decl))
4164 return decl;
4165 else if (lambda_expr)
4167 if (complain & tf_error)
4169 error ("%qD is not captured", decl);
4170 tree closure = LAMBDA_EXPR_CLOSURE (lambda_expr);
4171 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
4172 inform (location_of (closure),
4173 "the lambda has no capture-default");
4174 else if (TYPE_CLASS_SCOPE_P (closure))
4175 inform (UNKNOWN_LOCATION, "lambda in local class %q+T cannot "
4176 "capture variables from the enclosing context",
4177 TYPE_CONTEXT (closure));
4178 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
4180 return error_mark_node;
4182 else if (processing_contract_condition && (TREE_CODE (decl) == PARM_DECL))
4183 /* Use of a parameter in a contract condition is fine. */
4184 return decl;
4185 else
4187 if (complain & tf_error)
4189 error (VAR_P (decl)
4190 ? G_("use of local variable with automatic storage from "
4191 "containing function")
4192 : G_("use of parameter from containing function"));
4193 inform (DECL_SOURCE_LOCATION (decl), "%q#D declared here", decl);
4195 return error_mark_node;
4197 return decl;
4200 /* ID_EXPRESSION is a representation of parsed, but unprocessed,
4201 id-expression. (See cp_parser_id_expression for details.) SCOPE,
4202 if non-NULL, is the type or namespace used to explicitly qualify
4203 ID_EXPRESSION. DECL is the entity to which that name has been
4204 resolved.
4206 *CONSTANT_EXPRESSION_P is true if we are presently parsing a
4207 constant-expression. In that case, *NON_CONSTANT_EXPRESSION_P will
4208 be set to true if this expression isn't permitted in a
4209 constant-expression, but it is otherwise not set by this function.
4210 *ALLOW_NON_CONSTANT_EXPRESSION_P is true if we are parsing a
4211 constant-expression, but a non-constant expression is also
4212 permissible.
4214 DONE is true if this expression is a complete postfix-expression;
4215 it is false if this expression is followed by '->', '[', '(', etc.
4216 ADDRESS_P is true iff this expression is the operand of '&'.
4217 TEMPLATE_P is true iff the qualified-id was of the form
4218 "A::template B". TEMPLATE_ARG_P is true iff this qualified name
4219 appears as a template argument.
4221 If an error occurs, and it is the kind of error that might cause
4222 the parser to abort a tentative parse, *ERROR_MSG is filled in. It
4223 is the caller's responsibility to issue the message. *ERROR_MSG
4224 will be a string with static storage duration, so the caller need
4225 not "free" it.
4227 Return an expression for the entity, after issuing appropriate
4228 diagnostics. This function is also responsible for transforming a
4229 reference to a non-static member into a COMPONENT_REF that makes
4230 the use of "this" explicit.
4232 Upon return, *IDK will be filled in appropriately. */
4233 static cp_expr
4234 finish_id_expression_1 (tree id_expression,
4235 tree decl,
4236 tree scope,
4237 cp_id_kind *idk,
4238 bool integral_constant_expression_p,
4239 bool allow_non_integral_constant_expression_p,
4240 bool *non_integral_constant_expression_p,
4241 bool template_p,
4242 bool done,
4243 bool address_p,
4244 bool template_arg_p,
4245 const char **error_msg,
4246 location_t location)
4248 decl = strip_using_decl (decl);
4250 /* Initialize the output parameters. */
4251 *idk = CP_ID_KIND_NONE;
4252 *error_msg = NULL;
4254 if (id_expression == error_mark_node)
4255 return error_mark_node;
4256 /* If we have a template-id, then no further lookup is
4257 required. If the template-id was for a template-class, we
4258 will sometimes have a TYPE_DECL at this point. */
4259 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
4260 || TREE_CODE (decl) == TYPE_DECL)
4262 /* Look up the name. */
4263 else
4265 if (decl == error_mark_node)
4267 /* Name lookup failed. */
4268 if (scope
4269 && (!TYPE_P (scope)
4270 || (!dependent_type_p (scope)
4271 && !(identifier_p (id_expression)
4272 && IDENTIFIER_CONV_OP_P (id_expression)
4273 && dependent_type_p (TREE_TYPE (id_expression))))))
4275 /* If the qualifying type is non-dependent (and the name
4276 does not name a conversion operator to a dependent
4277 type), issue an error. */
4278 qualified_name_lookup_error (scope, id_expression, decl, location);
4279 return error_mark_node;
4281 else if (!scope)
4283 /* It may be resolved via Koenig lookup. */
4284 *idk = CP_ID_KIND_UNQUALIFIED;
4285 return id_expression;
4287 else
4288 decl = id_expression;
4291 /* Remember that the name was used in the definition of
4292 the current class so that we can check later to see if
4293 the meaning would have been different after the class
4294 was entirely defined. */
4295 if (!scope && decl != error_mark_node && identifier_p (id_expression))
4296 maybe_note_name_used_in_class (id_expression, decl);
4298 /* A use in unevaluated operand might not be instantiated appropriately
4299 if tsubst_copy builds a dummy parm, or if we never instantiate a
4300 generic lambda, so mark it now. */
4301 if (processing_template_decl && cp_unevaluated_operand)
4302 mark_type_use (decl);
4304 /* Disallow uses of local variables from containing functions, except
4305 within lambda-expressions. */
4306 if (outer_automatic_var_p (decl))
4308 decl = process_outer_var_ref (decl, tf_warning_or_error);
4309 if (decl == error_mark_node)
4310 return error_mark_node;
4313 /* Also disallow uses of function parameters outside the function
4314 body, except inside an unevaluated context (i.e. decltype). */
4315 if (TREE_CODE (decl) == PARM_DECL
4316 && DECL_CONTEXT (decl) == NULL_TREE
4317 && !cp_unevaluated_operand
4318 && !processing_contract_condition)
4320 *error_msg = G_("use of parameter outside function body");
4321 return error_mark_node;
4325 /* If we didn't find anything, or what we found was a type,
4326 then this wasn't really an id-expression. */
4327 if (TREE_CODE (decl) == TEMPLATE_DECL
4328 && !DECL_FUNCTION_TEMPLATE_P (decl))
4330 *error_msg = G_("missing template arguments");
4331 return error_mark_node;
4333 else if (TREE_CODE (decl) == TYPE_DECL
4334 || TREE_CODE (decl) == NAMESPACE_DECL)
4336 *error_msg = G_("expected primary-expression");
4337 return error_mark_node;
4340 /* If the name resolved to a template parameter, there is no
4341 need to look it up again later. */
4342 if ((TREE_CODE (decl) == CONST_DECL && DECL_TEMPLATE_PARM_P (decl))
4343 || TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
4345 tree r;
4347 *idk = CP_ID_KIND_NONE;
4348 if (TREE_CODE (decl) == TEMPLATE_PARM_INDEX)
4349 decl = TEMPLATE_PARM_DECL (decl);
4350 r = DECL_INITIAL (decl);
4351 if (CLASS_TYPE_P (TREE_TYPE (r)) && !CP_TYPE_CONST_P (TREE_TYPE (r)))
4353 /* If the entity is a template parameter object for a template
4354 parameter of type T, the type of the expression is const T. */
4355 tree ctype = TREE_TYPE (r);
4356 ctype = cp_build_qualified_type (ctype, (cp_type_quals (ctype)
4357 | TYPE_QUAL_CONST));
4358 r = build1 (VIEW_CONVERT_EXPR, ctype, r);
4360 r = convert_from_reference (r);
4361 if (integral_constant_expression_p
4362 && !dependent_type_p (TREE_TYPE (decl))
4363 && !(INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (r))))
4365 if (!allow_non_integral_constant_expression_p)
4366 error ("template parameter %qD of type %qT is not allowed in "
4367 "an integral constant expression because it is not of "
4368 "integral or enumeration type", decl, TREE_TYPE (decl));
4369 *non_integral_constant_expression_p = true;
4371 return r;
4373 else if (TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE)
4375 gcc_checking_assert (scope);
4376 *idk = CP_ID_KIND_QUALIFIED;
4377 cp_warn_deprecated_use_scopes (scope);
4378 decl = finish_qualified_id_expr (scope, decl, done, address_p,
4379 template_p, template_arg_p,
4380 tf_warning_or_error);
4382 else
4384 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
4385 && variable_template_p (TREE_OPERAND (decl, 0))
4386 && !concept_check_p (decl))
4387 /* Try resolving this variable TEMPLATE_ID_EXPR (which is always
4388 considered type-dependent) now, so that the dependence test that
4389 follows gives us the right answer: if it represents a non-dependent
4390 variable template-id then finish_template_variable will yield the
4391 corresponding non-dependent VAR_DECL. */
4392 decl = finish_template_variable (decl);
4394 bool dependent_p = type_dependent_expression_p (decl);
4396 /* If the declaration was explicitly qualified indicate
4397 that. The semantics of `A::f(3)' are different than
4398 `f(3)' if `f' is virtual. */
4399 *idk = (scope
4400 ? CP_ID_KIND_QUALIFIED
4401 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
4402 ? CP_ID_KIND_TEMPLATE_ID
4403 : (dependent_p
4404 ? CP_ID_KIND_UNQUALIFIED_DEPENDENT
4405 : CP_ID_KIND_UNQUALIFIED)));
4407 if (dependent_p
4408 && !scope
4409 && DECL_P (decl)
4410 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (decl)))
4411 /* Dependent type attributes on the decl mean that the TREE_TYPE is
4412 wrong, so just return the identifier. */
4413 return id_expression;
4415 if (DECL_CLASS_TEMPLATE_P (decl))
4417 error ("use of class template %qT as expression", decl);
4418 return error_mark_node;
4421 if (TREE_CODE (decl) == TREE_LIST)
4423 /* Ambiguous reference to base members. */
4424 error ("request for member %qD is ambiguous in "
4425 "multiple inheritance lattice", id_expression);
4426 print_candidates (decl);
4427 return error_mark_node;
4430 /* Mark variable-like entities as used. Functions are similarly
4431 marked either below or after overload resolution. */
4432 if ((VAR_P (decl)
4433 || TREE_CODE (decl) == PARM_DECL
4434 || TREE_CODE (decl) == CONST_DECL
4435 || TREE_CODE (decl) == RESULT_DECL)
4436 && !mark_used (decl))
4437 return error_mark_node;
4439 /* Only certain kinds of names are allowed in constant
4440 expression. Template parameters have already
4441 been handled above. */
4442 if (! error_operand_p (decl)
4443 && !dependent_p
4444 && integral_constant_expression_p
4445 && !decl_constant_var_p (decl)
4446 && TREE_CODE (decl) != CONST_DECL
4447 && !builtin_valid_in_constant_expr_p (decl)
4448 && !concept_check_p (decl))
4450 if (!allow_non_integral_constant_expression_p)
4452 error ("%qD cannot appear in a constant-expression", decl);
4453 return error_mark_node;
4455 *non_integral_constant_expression_p = true;
4458 if (tree wrap = maybe_get_tls_wrapper_call (decl))
4459 /* Replace an evaluated use of the thread_local variable with
4460 a call to its wrapper. */
4461 decl = wrap;
4462 else if (concept_check_p (decl))
4464 /* Nothing more to do. All of the analysis for concept checks
4465 is done by build_conept_id, called from the parser. */
4467 else if (scope)
4469 if (TREE_CODE (decl) == SCOPE_REF)
4471 gcc_assert (same_type_p (scope, TREE_OPERAND (decl, 0)));
4472 decl = TREE_OPERAND (decl, 1);
4475 decl = (adjust_result_of_qualified_name_lookup
4476 (decl, scope, current_nonlambda_class_type()));
4478 cp_warn_deprecated_use_scopes (scope);
4480 if (TYPE_P (scope))
4481 decl = finish_qualified_id_expr (scope,
4482 decl,
4483 done,
4484 address_p,
4485 template_p,
4486 template_arg_p,
4487 tf_warning_or_error);
4488 else
4489 decl = convert_from_reference (decl);
4491 else if (TREE_CODE (decl) == FIELD_DECL)
4493 /* Since SCOPE is NULL here, this is an unqualified name.
4494 Access checking has been performed during name lookup
4495 already. Turn off checking to avoid duplicate errors. */
4496 push_deferring_access_checks (dk_no_check);
4497 decl = finish_non_static_data_member (decl, NULL_TREE,
4498 /*qualifying_scope=*/NULL_TREE);
4499 pop_deferring_access_checks ();
4501 else if (is_overloaded_fn (decl))
4503 /* We only need to look at the first function,
4504 because all the fns share the attribute we're
4505 concerned with (all member fns or all non-members). */
4506 tree first_fn = get_first_fn (decl);
4507 first_fn = STRIP_TEMPLATE (first_fn);
4509 if (!template_arg_p
4510 && (TREE_CODE (first_fn) == USING_DECL
4511 || (TREE_CODE (first_fn) == FUNCTION_DECL
4512 && DECL_FUNCTION_MEMBER_P (first_fn)
4513 && !shared_member_p (decl))))
4515 /* A set of member functions. */
4516 decl = maybe_dummy_object (DECL_CONTEXT (first_fn), 0);
4517 return finish_class_member_access_expr (decl, id_expression,
4518 /*template_p=*/false,
4519 tf_warning_or_error);
4522 decl = baselink_for_fns (decl);
4524 else
4526 if (DECL_P (decl) && DECL_NONLOCAL (decl)
4527 && DECL_CLASS_SCOPE_P (decl))
4529 tree context = context_for_name_lookup (decl);
4530 if (context != current_class_type)
4532 tree path = currently_open_derived_class (context);
4533 if (!path)
4534 /* PATH can be null for using an enum of an unrelated
4535 class; we checked its access in lookup_using_decl.
4537 ??? Should this case make a clone instead, like
4538 handle_using_decl? */
4539 gcc_assert (TREE_CODE (decl) == CONST_DECL);
4540 else
4541 perform_or_defer_access_check (TYPE_BINFO (path),
4542 decl, decl,
4543 tf_warning_or_error);
4547 decl = convert_from_reference (decl);
4551 return cp_expr (decl, location);
4554 /* As per finish_id_expression_1, but adding a wrapper node
4555 around the result if needed to express LOCATION. */
4557 cp_expr
4558 finish_id_expression (tree id_expression,
4559 tree decl,
4560 tree scope,
4561 cp_id_kind *idk,
4562 bool integral_constant_expression_p,
4563 bool allow_non_integral_constant_expression_p,
4564 bool *non_integral_constant_expression_p,
4565 bool template_p,
4566 bool done,
4567 bool address_p,
4568 bool template_arg_p,
4569 const char **error_msg,
4570 location_t location)
4572 cp_expr result
4573 = finish_id_expression_1 (id_expression, decl, scope, idk,
4574 integral_constant_expression_p,
4575 allow_non_integral_constant_expression_p,
4576 non_integral_constant_expression_p,
4577 template_p, done, address_p, template_arg_p,
4578 error_msg, location);
4579 return result.maybe_add_location_wrapper ();
4582 /* Implement the __typeof keyword: Return the type of EXPR, suitable for
4583 use as a type-specifier. */
4585 tree
4586 finish_typeof (tree expr)
4588 tree type;
4590 if (type_dependent_expression_p (expr))
4592 type = cxx_make_type (TYPEOF_TYPE);
4593 TYPEOF_TYPE_EXPR (type) = expr;
4594 SET_TYPE_STRUCTURAL_EQUALITY (type);
4596 return type;
4599 expr = mark_type_use (expr);
4601 type = unlowered_expr_type (expr);
4603 if (!type || type == unknown_type_node)
4605 error ("type of %qE is unknown", expr);
4606 return error_mark_node;
4609 return type;
4612 /* Implement the __underlying_type keyword: Return the underlying
4613 type of TYPE, suitable for use as a type-specifier. */
4615 tree
4616 finish_underlying_type (tree type)
4618 if (!complete_type_or_else (type, NULL_TREE))
4619 return error_mark_node;
4621 if (TREE_CODE (type) != ENUMERAL_TYPE)
4623 error ("%qT is not an enumeration type", type);
4624 return error_mark_node;
4627 tree underlying_type = ENUM_UNDERLYING_TYPE (type);
4629 /* Fixup necessary in this case because ENUM_UNDERLYING_TYPE
4630 includes TYPE_MIN_VALUE and TYPE_MAX_VALUE information.
4631 See finish_enum_value_list for details. */
4632 if (!ENUM_FIXED_UNDERLYING_TYPE_P (type))
4633 underlying_type
4634 = c_common_type_for_mode (TYPE_MODE (underlying_type),
4635 TYPE_UNSIGNED (underlying_type));
4637 return underlying_type;
4640 /* Implement the __type_pack_element keyword: Return the type
4641 at index IDX within TYPES. */
4643 static tree
4644 finish_type_pack_element (tree idx, tree types, tsubst_flags_t complain)
4646 idx = maybe_constant_value (idx);
4647 if (TREE_CODE (idx) != INTEGER_CST || !INTEGRAL_TYPE_P (TREE_TYPE (idx)))
4649 if (complain & tf_error)
4650 error ("%<__type_pack_element%> index is not an integral constant");
4651 return error_mark_node;
4653 HOST_WIDE_INT val = tree_to_shwi (idx);
4654 if (val < 0)
4656 if (complain & tf_error)
4657 error ("%<__type_pack_element%> index is negative");
4658 return error_mark_node;
4660 if (val >= TREE_VEC_LENGTH (types))
4662 if (complain & tf_error)
4663 error ("%<__type_pack_element%> index is out of range");
4664 return error_mark_node;
4666 return TREE_VEC_ELT (types, val);
4669 /* Implement the __direct_bases keyword: Return the direct base classes
4670 of type. */
4672 tree
4673 calculate_direct_bases (tree type, tsubst_flags_t complain)
4675 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
4676 || !NON_UNION_CLASS_TYPE_P (type))
4677 return make_tree_vec (0);
4679 releasing_vec vector;
4680 vec<tree, va_gc> *base_binfos = BINFO_BASE_BINFOS (TYPE_BINFO (type));
4681 tree binfo;
4682 unsigned i;
4684 /* Virtual bases are initialized first */
4685 for (i = 0; base_binfos->iterate (i, &binfo); i++)
4686 if (BINFO_VIRTUAL_P (binfo))
4687 vec_safe_push (vector, binfo);
4689 /* Now non-virtuals */
4690 for (i = 0; base_binfos->iterate (i, &binfo); i++)
4691 if (!BINFO_VIRTUAL_P (binfo))
4692 vec_safe_push (vector, binfo);
4694 tree bases_vec = make_tree_vec (vector->length ());
4696 for (i = 0; i < vector->length (); ++i)
4697 TREE_VEC_ELT (bases_vec, i) = BINFO_TYPE ((*vector)[i]);
4699 return bases_vec;
4702 /* Implement the __bases keyword: Return the base classes
4703 of type */
4705 /* Find morally non-virtual base classes by walking binfo hierarchy */
4706 /* Virtual base classes are handled separately in finish_bases */
4708 static tree
4709 dfs_calculate_bases_pre (tree binfo, void * /*data_*/)
4711 /* Don't walk bases of virtual bases */
4712 return BINFO_VIRTUAL_P (binfo) ? dfs_skip_bases : NULL_TREE;
4715 static tree
4716 dfs_calculate_bases_post (tree binfo, void *data_)
4718 vec<tree, va_gc> **data = ((vec<tree, va_gc> **) data_);
4719 if (!BINFO_VIRTUAL_P (binfo))
4720 vec_safe_push (*data, BINFO_TYPE (binfo));
4721 return NULL_TREE;
4724 /* Calculates the morally non-virtual base classes of a class */
4725 static vec<tree, va_gc> *
4726 calculate_bases_helper (tree type)
4728 vec<tree, va_gc> *vector = make_tree_vector ();
4730 /* Now add non-virtual base classes in order of construction */
4731 if (TYPE_BINFO (type))
4732 dfs_walk_all (TYPE_BINFO (type),
4733 dfs_calculate_bases_pre, dfs_calculate_bases_post, &vector);
4734 return vector;
4737 tree
4738 calculate_bases (tree type, tsubst_flags_t complain)
4740 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain)
4741 || !NON_UNION_CLASS_TYPE_P (type))
4742 return make_tree_vec (0);
4744 releasing_vec vector;
4745 tree bases_vec = NULL_TREE;
4746 unsigned i;
4747 vec<tree, va_gc> *vbases;
4748 tree binfo;
4750 /* First go through virtual base classes */
4751 for (vbases = CLASSTYPE_VBASECLASSES (type), i = 0;
4752 vec_safe_iterate (vbases, i, &binfo); i++)
4754 releasing_vec vbase_bases
4755 = calculate_bases_helper (BINFO_TYPE (binfo));
4756 vec_safe_splice (vector, vbase_bases);
4759 /* Now for the non-virtual bases */
4760 releasing_vec nonvbases = calculate_bases_helper (type);
4761 vec_safe_splice (vector, nonvbases);
4763 /* Note that during error recovery vector->length can even be zero. */
4764 if (vector->length () > 1)
4766 /* Last element is entire class, so don't copy */
4767 bases_vec = make_tree_vec (vector->length () - 1);
4769 for (i = 0; i < vector->length () - 1; ++i)
4770 TREE_VEC_ELT (bases_vec, i) = (*vector)[i];
4772 else
4773 bases_vec = make_tree_vec (0);
4775 return bases_vec;
4778 tree
4779 finish_bases (tree type, bool direct)
4781 tree bases = NULL_TREE;
4783 if (!processing_template_decl)
4785 /* Parameter packs can only be used in templates */
4786 error ("parameter pack %<__bases%> only valid in template declaration");
4787 return error_mark_node;
4790 bases = cxx_make_type (BASES);
4791 BASES_TYPE (bases) = type;
4792 BASES_DIRECT (bases) = direct;
4793 SET_TYPE_STRUCTURAL_EQUALITY (bases);
4795 return bases;
4798 /* Perform C++-specific checks for __builtin_offsetof before calling
4799 fold_offsetof. */
4801 tree
4802 finish_offsetof (tree object_ptr, tree expr, location_t loc)
4804 /* If we're processing a template, we can't finish the semantics yet.
4805 Otherwise we can fold the entire expression now. */
4806 if (processing_template_decl)
4808 expr = build2 (OFFSETOF_EXPR, size_type_node, expr, object_ptr);
4809 SET_EXPR_LOCATION (expr, loc);
4810 return expr;
4813 if (expr == error_mark_node)
4814 return error_mark_node;
4816 if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
4818 error ("cannot apply %<offsetof%> to destructor %<~%T%>",
4819 TREE_OPERAND (expr, 2));
4820 return error_mark_node;
4822 if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (expr))
4823 || TREE_TYPE (expr) == unknown_type_node)
4825 while (TREE_CODE (expr) == COMPONENT_REF
4826 || TREE_CODE (expr) == COMPOUND_EXPR)
4827 expr = TREE_OPERAND (expr, 1);
4829 if (DECL_P (expr))
4831 error ("cannot apply %<offsetof%> to member function %qD", expr);
4832 inform (DECL_SOURCE_LOCATION (expr), "declared here");
4834 else
4835 error ("cannot apply %<offsetof%> to member function");
4836 return error_mark_node;
4838 if (TREE_CODE (expr) == CONST_DECL)
4840 error ("cannot apply %<offsetof%> to an enumerator %qD", expr);
4841 return error_mark_node;
4843 if (REFERENCE_REF_P (expr))
4844 expr = TREE_OPERAND (expr, 0);
4845 if (!complete_type_or_else (TREE_TYPE (TREE_TYPE (object_ptr)), object_ptr))
4846 return error_mark_node;
4847 if (warn_invalid_offsetof
4848 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (object_ptr)))
4849 && CLASSTYPE_NON_STD_LAYOUT (TREE_TYPE (TREE_TYPE (object_ptr)))
4850 && cp_unevaluated_operand == 0)
4851 warning_at (loc, OPT_Winvalid_offsetof, "%<offsetof%> within "
4852 "non-standard-layout type %qT is conditionally-supported",
4853 TREE_TYPE (TREE_TYPE (object_ptr)));
4854 return fold_offsetof (expr);
4857 /* Replace the AGGR_INIT_EXPR at *TP with an equivalent CALL_EXPR. This
4858 function is broken out from the above for the benefit of the tree-ssa
4859 project. */
4861 void
4862 simplify_aggr_init_expr (tree *tp)
4864 tree aggr_init_expr = *tp;
4866 /* Form an appropriate CALL_EXPR. */
4867 tree fn = AGGR_INIT_EXPR_FN (aggr_init_expr);
4868 tree slot = AGGR_INIT_EXPR_SLOT (aggr_init_expr);
4869 tree type = TREE_TYPE (slot);
4871 tree call_expr;
4872 enum style_t { ctor, arg, pcc } style;
4874 if (AGGR_INIT_VIA_CTOR_P (aggr_init_expr))
4875 style = ctor;
4876 #ifdef PCC_STATIC_STRUCT_RETURN
4877 else if (1)
4878 style = pcc;
4879 #endif
4880 else
4882 gcc_assert (TREE_ADDRESSABLE (type));
4883 style = arg;
4886 call_expr = build_call_array_loc (input_location,
4887 TREE_TYPE (TREE_TYPE (TREE_TYPE (fn))),
4889 aggr_init_expr_nargs (aggr_init_expr),
4890 AGGR_INIT_EXPR_ARGP (aggr_init_expr));
4891 TREE_NOTHROW (call_expr) = TREE_NOTHROW (aggr_init_expr);
4892 CALL_FROM_THUNK_P (call_expr) = AGGR_INIT_FROM_THUNK_P (aggr_init_expr);
4893 CALL_EXPR_OPERATOR_SYNTAX (call_expr)
4894 = CALL_EXPR_OPERATOR_SYNTAX (aggr_init_expr);
4895 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (aggr_init_expr);
4896 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (aggr_init_expr);
4898 if (style == ctor)
4900 /* Replace the first argument to the ctor with the address of the
4901 slot. */
4902 cxx_mark_addressable (slot);
4903 CALL_EXPR_ARG (call_expr, 0) =
4904 build1 (ADDR_EXPR, build_pointer_type (type), slot);
4906 else if (style == arg)
4908 /* Just mark it addressable here, and leave the rest to
4909 expand_call{,_inline}. */
4910 cxx_mark_addressable (slot);
4911 CALL_EXPR_RETURN_SLOT_OPT (call_expr) = true;
4912 call_expr = cp_build_init_expr (slot, call_expr);
4914 else if (style == pcc)
4916 /* If we're using the non-reentrant PCC calling convention, then we
4917 need to copy the returned value out of the static buffer into the
4918 SLOT. */
4919 push_deferring_access_checks (dk_no_check);
4920 call_expr = build_aggr_init (slot, call_expr,
4921 DIRECT_BIND | LOOKUP_ONLYCONVERTING,
4922 tf_warning_or_error);
4923 pop_deferring_access_checks ();
4924 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (slot), call_expr, slot);
4927 if (AGGR_INIT_ZERO_FIRST (aggr_init_expr))
4929 tree init = build_zero_init (type, NULL_TREE,
4930 /*static_storage_p=*/false);
4931 init = cp_build_init_expr (slot, init);
4932 call_expr = build2 (COMPOUND_EXPR, TREE_TYPE (call_expr),
4933 init, call_expr);
4936 *tp = call_expr;
4939 /* Emit all thunks to FN that should be emitted when FN is emitted. */
4941 void
4942 emit_associated_thunks (tree fn)
4944 /* When we use vcall offsets, we emit thunks with the virtual
4945 functions to which they thunk. The whole point of vcall offsets
4946 is so that you can know statically the entire set of thunks that
4947 will ever be needed for a given virtual function, thereby
4948 enabling you to output all the thunks with the function itself. */
4949 if (DECL_VIRTUAL_P (fn)
4950 /* Do not emit thunks for extern template instantiations. */
4951 && ! DECL_REALLY_EXTERN (fn))
4953 tree thunk;
4955 for (thunk = DECL_THUNKS (fn); thunk; thunk = DECL_CHAIN (thunk))
4957 if (!THUNK_ALIAS (thunk))
4959 use_thunk (thunk, /*emit_p=*/1);
4960 if (DECL_RESULT_THUNK_P (thunk))
4962 tree probe;
4964 for (probe = DECL_THUNKS (thunk);
4965 probe; probe = DECL_CHAIN (probe))
4966 use_thunk (probe, /*emit_p=*/1);
4969 else
4970 gcc_assert (!DECL_THUNKS (thunk));
4975 /* Generate RTL for FN. */
4977 bool
4978 expand_or_defer_fn_1 (tree fn)
4980 /* When the parser calls us after finishing the body of a template
4981 function, we don't really want to expand the body. */
4982 if (processing_template_decl)
4984 /* Normally, collection only occurs in rest_of_compilation. So,
4985 if we don't collect here, we never collect junk generated
4986 during the processing of templates until we hit a
4987 non-template function. It's not safe to do this inside a
4988 nested class, though, as the parser may have local state that
4989 is not a GC root. */
4990 if (!function_depth)
4991 ggc_collect ();
4992 return false;
4995 gcc_assert (DECL_SAVED_TREE (fn));
4997 /* We make a decision about linkage for these functions at the end
4998 of the compilation. Until that point, we do not want the back
4999 end to output them -- but we do want it to see the bodies of
5000 these functions so that it can inline them as appropriate. */
5001 if (DECL_DECLARED_INLINE_P (fn) || DECL_IMPLICIT_INSTANTIATION (fn))
5003 if (DECL_INTERFACE_KNOWN (fn))
5004 /* We've already made a decision as to how this function will
5005 be handled. */;
5006 else if (!at_eof
5007 || DECL_IMMEDIATE_FUNCTION_P (fn)
5008 || DECL_OMP_DECLARE_REDUCTION_P (fn))
5009 tentative_decl_linkage (fn);
5010 else
5011 import_export_decl (fn);
5013 /* If the user wants us to keep all inline functions, then mark
5014 this function as needed so that finish_file will make sure to
5015 output it later. Similarly, all dllexport'd functions must
5016 be emitted; there may be callers in other DLLs. */
5017 if (DECL_DECLARED_INLINE_P (fn)
5018 && !DECL_REALLY_EXTERN (fn)
5019 && !DECL_IMMEDIATE_FUNCTION_P (fn)
5020 && !DECL_OMP_DECLARE_REDUCTION_P (fn)
5021 && (flag_keep_inline_functions
5022 || (flag_keep_inline_dllexport
5023 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (fn)))))
5025 mark_needed (fn);
5026 DECL_EXTERNAL (fn) = 0;
5030 /* If this is a constructor or destructor body, we have to clone
5031 it. */
5032 if (maybe_clone_body (fn))
5034 /* We don't want to process FN again, so pretend we've written
5035 it out, even though we haven't. */
5036 TREE_ASM_WRITTEN (fn) = 1;
5037 /* If this is a constexpr function, keep DECL_SAVED_TREE. */
5038 if (!DECL_DECLARED_CONSTEXPR_P (fn)
5039 && !(modules_p () && DECL_DECLARED_INLINE_P (fn)))
5040 DECL_SAVED_TREE (fn) = NULL_TREE;
5041 return false;
5044 /* There's no reason to do any of the work here if we're only doing
5045 semantic analysis; this code just generates RTL. */
5046 if (flag_syntax_only)
5048 /* Pretend that this function has been written out so that we don't try
5049 to expand it again. */
5050 TREE_ASM_WRITTEN (fn) = 1;
5051 return false;
5054 if (DECL_OMP_DECLARE_REDUCTION_P (fn))
5055 return false;
5057 return true;
5060 void
5061 expand_or_defer_fn (tree fn)
5063 if (expand_or_defer_fn_1 (fn))
5065 function_depth++;
5067 /* Expand or defer, at the whim of the compilation unit manager. */
5068 cgraph_node::finalize_function (fn, function_depth > 1);
5069 emit_associated_thunks (fn);
5071 function_depth--;
5073 if (DECL_IMMEDIATE_FUNCTION_P (fn))
5075 if (cgraph_node *node = cgraph_node::get (fn))
5077 node->body_removed = true;
5078 node->analyzed = false;
5079 node->definition = false;
5080 node->force_output = false;
5086 class nrv_data
5088 public:
5089 nrv_data () : visited (37) {}
5091 tree var;
5092 tree result;
5093 hash_set<tree> visited;
5094 bool simple;
5095 bool in_nrv_cleanup;
5098 /* Helper function for walk_tree, used by finalize_nrv below. */
5100 static tree
5101 finalize_nrv_r (tree* tp, int* walk_subtrees, void* data)
5103 class nrv_data *dp = (class nrv_data *)data;
5105 /* No need to walk into types. There wouldn't be any need to walk into
5106 non-statements, except that we have to consider STMT_EXPRs. */
5107 if (TYPE_P (*tp))
5108 *walk_subtrees = 0;
5110 /* Replace all uses of the NRV with the RESULT_DECL. */
5111 else if (*tp == dp->var)
5112 *tp = dp->result;
5114 /* Avoid walking into the same tree more than once. Unfortunately, we
5115 can't just use walk_tree_without duplicates because it would only call
5116 us for the first occurrence of dp->var in the function body. */
5117 else if (dp->visited.add (*tp))
5118 *walk_subtrees = 0;
5120 /* If there's a label, we might need to destroy the NRV on goto (92407). */
5121 else if (TREE_CODE (*tp) == LABEL_EXPR && !dp->in_nrv_cleanup)
5122 dp->simple = false;
5123 /* Change NRV returns to just refer to the RESULT_DECL; this is a nop,
5124 but differs from using NULL_TREE in that it indicates that we care
5125 about the value of the RESULT_DECL. But preserve anything appended
5126 by check_return_expr. */
5127 else if (TREE_CODE (*tp) == RETURN_EXPR)
5129 tree *p = &TREE_OPERAND (*tp, 0);
5130 while (TREE_CODE (*p) == COMPOUND_EXPR)
5131 p = &TREE_OPERAND (*p, 0);
5132 if (TREE_CODE (*p) == INIT_EXPR
5133 && INIT_EXPR_NRV_P (*p))
5134 *p = dp->result;
5136 /* Change all cleanups for the NRV to only run when not returning. */
5137 else if (TREE_CODE (*tp) == CLEANUP_STMT
5138 && CLEANUP_DECL (*tp) == dp->var)
5140 dp->in_nrv_cleanup = true;
5141 cp_walk_tree (&CLEANUP_BODY (*tp), finalize_nrv_r, data, 0);
5142 dp->in_nrv_cleanup = false;
5143 cp_walk_tree (&CLEANUP_EXPR (*tp), finalize_nrv_r, data, 0);
5144 *walk_subtrees = 0;
5146 if (dp->simple)
5147 /* For a simple NRV, just run it on the EH path. */
5148 CLEANUP_EH_ONLY (*tp) = true;
5149 else
5151 /* Not simple, we need to check current_retval_sentinel to decide
5152 whether to run it. If it's set, we're returning normally and
5153 don't want to destroy the NRV. If the sentinel is not set, we're
5154 leaving scope some other way, either by flowing off the end of its
5155 scope or throwing an exception. */
5156 tree cond = build3 (COND_EXPR, void_type_node,
5157 current_retval_sentinel,
5158 void_node, CLEANUP_EXPR (*tp));
5159 CLEANUP_EXPR (*tp) = cond;
5162 /* If a cleanup might throw, we need to clear current_retval_sentinel on
5163 the exception path, both so the check above succeeds and so an outer
5164 cleanup added by maybe_splice_retval_cleanup doesn't run. */
5165 if (cp_function_chain->throwing_cleanup)
5167 tree clear = build2 (MODIFY_EXPR, boolean_type_node,
5168 current_retval_sentinel,
5169 boolean_false_node);
5170 if (dp->simple)
5172 /* We're already only on the EH path, just prepend it. */
5173 tree &exp = CLEANUP_EXPR (*tp);
5174 exp = build2 (COMPOUND_EXPR, void_type_node, clear, exp);
5176 else
5178 /* The cleanup runs on both normal and EH paths, we need another
5179 CLEANUP_STMT to clear the flag only on the EH path. */
5180 tree &bod = CLEANUP_BODY (*tp);
5181 bod = build_stmt (EXPR_LOCATION (*tp), CLEANUP_STMT,
5182 bod, clear, current_retval_sentinel);
5183 CLEANUP_EH_ONLY (bod) = true;
5187 /* Disable maybe_splice_retval_cleanup within the NRV cleanup scope, we don't
5188 want to destroy the retval before the variable goes out of scope. */
5189 else if (TREE_CODE (*tp) == CLEANUP_STMT
5190 && dp->in_nrv_cleanup
5191 && CLEANUP_DECL (*tp) == dp->result)
5192 CLEANUP_EXPR (*tp) = void_node;
5193 /* Replace the DECL_EXPR for the NRV with an initialization of the
5194 RESULT_DECL, if needed. */
5195 else if (TREE_CODE (*tp) == DECL_EXPR
5196 && DECL_EXPR_DECL (*tp) == dp->var)
5198 tree init;
5199 if (DECL_INITIAL (dp->var)
5200 && DECL_INITIAL (dp->var) != error_mark_node)
5201 init = cp_build_init_expr (dp->result,
5202 DECL_INITIAL (dp->var));
5203 else
5204 init = build_empty_stmt (EXPR_LOCATION (*tp));
5205 DECL_INITIAL (dp->var) = NULL_TREE;
5206 SET_EXPR_LOCATION (init, EXPR_LOCATION (*tp));
5207 *tp = init;
5210 /* Keep iterating. */
5211 return NULL_TREE;
5214 /* Called from finish_function to implement the named return value
5215 optimization by overriding all the RETURN_EXPRs and pertinent
5216 CLEANUP_STMTs and replacing all occurrences of VAR with RESULT, the
5217 RESULT_DECL for the function. */
5219 void
5220 finalize_nrv (tree fndecl, tree var)
5222 class nrv_data data;
5223 tree result = DECL_RESULT (fndecl);
5225 /* Copy name from VAR to RESULT. */
5226 DECL_NAME (result) = DECL_NAME (var);
5227 /* Don't forget that we take its address. */
5228 TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (var);
5229 /* Finally set DECL_VALUE_EXPR to avoid assigning
5230 a stack slot at -O0 for the original var and debug info
5231 uses RESULT location for VAR. */
5232 SET_DECL_VALUE_EXPR (var, result);
5233 DECL_HAS_VALUE_EXPR_P (var) = 1;
5235 data.var = var;
5236 data.result = result;
5237 data.in_nrv_cleanup = false;
5239 /* This is simpler for variables declared in the outer scope of
5240 the function so we know that their lifetime always ends with a
5241 return; see g++.dg/opt/nrv6.C. */
5242 tree outer = outer_curly_brace_block (fndecl);
5243 data.simple = chain_member (var, BLOCK_VARS (outer));
5245 cp_walk_tree (&DECL_SAVED_TREE (fndecl), finalize_nrv_r, &data, 0);
5248 /* Create CP_OMP_CLAUSE_INFO for clause C. Returns true if it is invalid. */
5250 bool
5251 cxx_omp_create_clause_info (tree c, tree type, bool need_default_ctor,
5252 bool need_copy_ctor, bool need_copy_assignment,
5253 bool need_dtor)
5255 int save_errorcount = errorcount;
5256 tree info, t;
5258 /* Always allocate 3 elements for simplicity. These are the
5259 function decls for the ctor, dtor, and assignment op.
5260 This layout is known to the three lang hooks,
5261 cxx_omp_clause_default_init, cxx_omp_clause_copy_init,
5262 and cxx_omp_clause_assign_op. */
5263 info = make_tree_vec (3);
5264 CP_OMP_CLAUSE_INFO (c) = info;
5266 if (need_default_ctor || need_copy_ctor)
5268 if (need_default_ctor)
5269 t = get_default_ctor (type);
5270 else
5271 t = get_copy_ctor (type, tf_warning_or_error);
5273 if (t && !trivial_fn_p (t))
5274 TREE_VEC_ELT (info, 0) = t;
5277 if (need_dtor && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
5278 TREE_VEC_ELT (info, 1) = get_dtor (type, tf_warning_or_error);
5280 if (need_copy_assignment)
5282 t = get_copy_assign (type);
5284 if (t && !trivial_fn_p (t))
5285 TREE_VEC_ELT (info, 2) = t;
5288 return errorcount != save_errorcount;
5291 /* If DECL is DECL_OMP_PRIVATIZED_MEMBER, return corresponding
5292 FIELD_DECL, otherwise return DECL itself. */
5294 static tree
5295 omp_clause_decl_field (tree decl)
5297 if (VAR_P (decl)
5298 && DECL_HAS_VALUE_EXPR_P (decl)
5299 && DECL_ARTIFICIAL (decl)
5300 && DECL_LANG_SPECIFIC (decl)
5301 && DECL_OMP_PRIVATIZED_MEMBER (decl))
5303 tree f = DECL_VALUE_EXPR (decl);
5304 if (INDIRECT_REF_P (f))
5305 f = TREE_OPERAND (f, 0);
5306 if (TREE_CODE (f) == COMPONENT_REF)
5308 f = TREE_OPERAND (f, 1);
5309 gcc_assert (TREE_CODE (f) == FIELD_DECL);
5310 return f;
5313 return NULL_TREE;
5316 /* Adjust DECL if needed for printing using %qE. */
5318 static tree
5319 omp_clause_printable_decl (tree decl)
5321 tree t = omp_clause_decl_field (decl);
5322 if (t)
5323 return t;
5324 return decl;
5327 /* For a FIELD_DECL F and corresponding DECL_OMP_PRIVATIZED_MEMBER
5328 VAR_DECL T that doesn't need a DECL_EXPR added, record it for
5329 privatization. */
5331 static void
5332 omp_note_field_privatization (tree f, tree t)
5334 if (!omp_private_member_map)
5335 omp_private_member_map = new hash_map<tree, tree>;
5336 tree &v = omp_private_member_map->get_or_insert (f);
5337 if (v == NULL_TREE)
5339 v = t;
5340 omp_private_member_vec.safe_push (f);
5341 /* Signal that we don't want to create DECL_EXPR for this dummy var. */
5342 omp_private_member_vec.safe_push (integer_zero_node);
5346 /* Privatize FIELD_DECL T, return corresponding DECL_OMP_PRIVATIZED_MEMBER
5347 dummy VAR_DECL. */
5349 tree
5350 omp_privatize_field (tree t, bool shared)
5352 tree m = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
5353 if (m == error_mark_node)
5354 return error_mark_node;
5355 if (!omp_private_member_map && !shared)
5356 omp_private_member_map = new hash_map<tree, tree>;
5357 if (TYPE_REF_P (TREE_TYPE (t)))
5359 gcc_assert (INDIRECT_REF_P (m));
5360 m = TREE_OPERAND (m, 0);
5362 tree vb = NULL_TREE;
5363 tree &v = shared ? vb : omp_private_member_map->get_or_insert (t);
5364 if (v == NULL_TREE)
5366 v = create_temporary_var (TREE_TYPE (m));
5367 retrofit_lang_decl (v);
5368 DECL_OMP_PRIVATIZED_MEMBER (v) = 1;
5369 SET_DECL_VALUE_EXPR (v, m);
5370 DECL_HAS_VALUE_EXPR_P (v) = 1;
5371 if (!shared)
5372 omp_private_member_vec.safe_push (t);
5374 return v;
5377 /* C++ specialisation of the c_omp_address_inspector class. */
5379 class cp_omp_address_inspector : public c_omp_address_inspector
5381 public:
5382 cp_omp_address_inspector (location_t loc, tree t)
5383 : c_omp_address_inspector (loc, t)
5387 ~cp_omp_address_inspector ()
5391 bool processing_template_decl_p ()
5393 return processing_template_decl;
5396 void emit_unmappable_type_notes (tree t)
5398 if (TREE_TYPE (t) != error_mark_node
5399 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
5400 cxx_incomplete_type_inform (TREE_TYPE (t));
5403 tree convert_from_reference (tree x)
5405 return ::convert_from_reference (x);
5408 tree build_array_ref (location_t loc, tree arr, tree idx)
5410 return ::build_array_ref (loc, arr, idx);
5413 bool check_clause (tree clause)
5415 if (TREE_CODE (orig) == COMPONENT_REF
5416 && invalid_nonstatic_memfn_p (EXPR_LOCATION (orig), orig,
5417 tf_warning_or_error))
5418 return false;
5419 if (!c_omp_address_inspector::check_clause (clause))
5420 return false;
5421 return true;
5425 /* Helper function for handle_omp_array_sections. Called recursively
5426 to handle multiple array-section-subscripts. C is the clause,
5427 T current expression (initially OMP_CLAUSE_DECL), which is either
5428 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
5429 expression if specified, TREE_VALUE length expression if specified,
5430 TREE_CHAIN is what it has been specified after, or some decl.
5431 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
5432 set to true if any of the array-section-subscript could have length
5433 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
5434 first array-section-subscript which is known not to have length
5435 of one. Given say:
5436 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
5437 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
5438 all are or may have length of 1, array-section-subscript [:2] is the
5439 first one known not to have length 1. For array-section-subscript
5440 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
5441 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
5442 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
5443 case though, as some lengths could be zero. */
5445 static tree
5446 handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
5447 bool &maybe_zero_len, unsigned int &first_non_one,
5448 enum c_omp_region_type ort)
5450 tree ret, low_bound, length, type;
5451 bool openacc = (ort & C_ORT_ACC) != 0;
5452 if (TREE_CODE (t) != OMP_ARRAY_SECTION)
5454 if (error_operand_p (t))
5455 return error_mark_node;
5457 cp_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
5458 tree t_refto = ai.maybe_unconvert_ref (t);
5460 if (!ai.check_clause (c))
5461 return error_mark_node;
5462 else if (ai.component_access_p ()
5463 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5464 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
5465 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
5466 t = ai.get_root_term (true);
5467 else
5468 t = ai.unconverted_ref_origin ();
5469 if (t == error_mark_node)
5470 return error_mark_node;
5471 ret = t_refto;
5472 if (TREE_CODE (t) == FIELD_DECL)
5473 ret = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
5474 else if (!VAR_P (t)
5475 && (openacc || !EXPR_P (t))
5476 && TREE_CODE (t) != PARM_DECL)
5478 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
5479 return NULL_TREE;
5480 if (DECL_P (t))
5481 error_at (OMP_CLAUSE_LOCATION (c),
5482 "%qD is not a variable in %qs clause", t,
5483 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5484 else
5485 error_at (OMP_CLAUSE_LOCATION (c),
5486 "%qE is not a variable in %qs clause", t,
5487 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5488 return error_mark_node;
5490 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5491 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5492 && VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
5494 error_at (OMP_CLAUSE_LOCATION (c),
5495 "%qD is threadprivate variable in %qs clause", t,
5496 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5497 return error_mark_node;
5499 if (type_dependent_expression_p (ret))
5500 return NULL_TREE;
5501 ret = convert_from_reference (ret);
5502 return ret;
5505 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP
5506 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5507 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5508 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5509 && TREE_CODE (TREE_OPERAND (t, 0)) == FIELD_DECL)
5510 TREE_OPERAND (t, 0) = omp_privatize_field (TREE_OPERAND (t, 0), false);
5511 ret = handle_omp_array_sections_1 (c, TREE_OPERAND (t, 0), types,
5512 maybe_zero_len, first_non_one, ort);
5513 if (ret == error_mark_node || ret == NULL_TREE)
5514 return ret;
5516 type = TREE_TYPE (ret);
5517 low_bound = TREE_OPERAND (t, 1);
5518 length = TREE_OPERAND (t, 2);
5519 if ((low_bound && type_dependent_expression_p (low_bound))
5520 || (length && type_dependent_expression_p (length)))
5521 return NULL_TREE;
5523 if (low_bound == error_mark_node || length == error_mark_node)
5524 return error_mark_node;
5526 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
5528 error_at (OMP_CLAUSE_LOCATION (c),
5529 "low bound %qE of array section does not have integral type",
5530 low_bound);
5531 return error_mark_node;
5533 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
5535 error_at (OMP_CLAUSE_LOCATION (c),
5536 "length %qE of array section does not have integral type",
5537 length);
5538 return error_mark_node;
5540 if (low_bound)
5541 low_bound = mark_rvalue_use (low_bound);
5542 if (length)
5543 length = mark_rvalue_use (length);
5544 /* We need to reduce to real constant-values for checks below. */
5545 if (length)
5546 length = fold_simple (length);
5547 if (low_bound)
5548 low_bound = fold_simple (low_bound);
5549 if (low_bound
5550 && TREE_CODE (low_bound) == INTEGER_CST
5551 && TYPE_PRECISION (TREE_TYPE (low_bound))
5552 > TYPE_PRECISION (sizetype))
5553 low_bound = fold_convert (sizetype, low_bound);
5554 if (length
5555 && TREE_CODE (length) == INTEGER_CST
5556 && TYPE_PRECISION (TREE_TYPE (length))
5557 > TYPE_PRECISION (sizetype))
5558 length = fold_convert (sizetype, length);
5559 if (low_bound == NULL_TREE)
5560 low_bound = integer_zero_node;
5562 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
5563 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
5564 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH))
5566 if (length != integer_one_node)
5568 error_at (OMP_CLAUSE_LOCATION (c),
5569 "expected single pointer in %qs clause",
5570 user_omp_clause_code_name (c, openacc));
5571 return error_mark_node;
5574 if (length != NULL_TREE)
5576 if (!integer_nonzerop (length))
5578 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
5579 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5580 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5581 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5582 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5584 if (integer_zerop (length))
5586 error_at (OMP_CLAUSE_LOCATION (c),
5587 "zero length array section in %qs clause",
5588 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5589 return error_mark_node;
5592 else
5593 maybe_zero_len = true;
5595 if (first_non_one == types.length ()
5596 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
5597 first_non_one++;
5599 if (TREE_CODE (type) == ARRAY_TYPE)
5601 if (length == NULL_TREE
5602 && (TYPE_DOMAIN (type) == NULL_TREE
5603 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
5605 error_at (OMP_CLAUSE_LOCATION (c),
5606 "for unknown bound array type length expression must "
5607 "be specified");
5608 return error_mark_node;
5610 if (TREE_CODE (low_bound) == INTEGER_CST
5611 && tree_int_cst_sgn (low_bound) == -1)
5613 error_at (OMP_CLAUSE_LOCATION (c),
5614 "negative low bound in array section in %qs clause",
5615 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5616 return error_mark_node;
5618 if (length != NULL_TREE
5619 && TREE_CODE (length) == INTEGER_CST
5620 && tree_int_cst_sgn (length) == -1)
5622 error_at (OMP_CLAUSE_LOCATION (c),
5623 "negative length in array section in %qs clause",
5624 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5625 return error_mark_node;
5627 if (TYPE_DOMAIN (type)
5628 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
5629 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
5630 == INTEGER_CST)
5632 tree size
5633 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
5634 size = size_binop (PLUS_EXPR, size, size_one_node);
5635 if (TREE_CODE (low_bound) == INTEGER_CST)
5637 if (tree_int_cst_lt (size, low_bound))
5639 error_at (OMP_CLAUSE_LOCATION (c),
5640 "low bound %qE above array section size "
5641 "in %qs clause", low_bound,
5642 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5643 return error_mark_node;
5645 if (tree_int_cst_equal (size, low_bound))
5647 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY
5648 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5649 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5650 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5651 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5653 error_at (OMP_CLAUSE_LOCATION (c),
5654 "zero length array section in %qs clause",
5655 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5656 return error_mark_node;
5658 maybe_zero_len = true;
5660 else if (length == NULL_TREE
5661 && first_non_one == types.length ()
5662 && tree_int_cst_equal
5663 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
5664 low_bound))
5665 first_non_one++;
5667 else if (length == NULL_TREE)
5669 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5670 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5671 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
5672 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
5673 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
5674 maybe_zero_len = true;
5675 if (first_non_one == types.length ())
5676 first_non_one++;
5678 if (length && TREE_CODE (length) == INTEGER_CST)
5680 if (tree_int_cst_lt (size, length))
5682 error_at (OMP_CLAUSE_LOCATION (c),
5683 "length %qE above array section size "
5684 "in %qs clause", length,
5685 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5686 return error_mark_node;
5688 if (TREE_CODE (low_bound) == INTEGER_CST)
5690 tree lbpluslen
5691 = size_binop (PLUS_EXPR,
5692 fold_convert (sizetype, low_bound),
5693 fold_convert (sizetype, length));
5694 if (TREE_CODE (lbpluslen) == INTEGER_CST
5695 && tree_int_cst_lt (size, lbpluslen))
5697 error_at (OMP_CLAUSE_LOCATION (c),
5698 "high bound %qE above array section size "
5699 "in %qs clause", lbpluslen,
5700 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5701 return error_mark_node;
5706 else if (length == NULL_TREE)
5708 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5709 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5710 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION
5711 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_IN_REDUCTION
5712 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_TASK_REDUCTION)
5713 maybe_zero_len = true;
5714 if (first_non_one == types.length ())
5715 first_non_one++;
5718 /* For [lb:] we will need to evaluate lb more than once. */
5719 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
5721 tree lb = cp_save_expr (low_bound);
5722 if (lb != low_bound)
5724 TREE_OPERAND (t, 1) = lb;
5725 low_bound = lb;
5729 else if (TYPE_PTR_P (type))
5731 if (length == NULL_TREE)
5733 if (TREE_CODE (ret) == PARM_DECL && DECL_ARRAY_PARAMETER_P (ret))
5734 error_at (OMP_CLAUSE_LOCATION (c),
5735 "for array function parameter length expression "
5736 "must be specified");
5737 else
5738 error_at (OMP_CLAUSE_LOCATION (c),
5739 "for pointer type length expression must be specified");
5740 return error_mark_node;
5742 if (length != NULL_TREE
5743 && TREE_CODE (length) == INTEGER_CST
5744 && tree_int_cst_sgn (length) == -1)
5746 error_at (OMP_CLAUSE_LOCATION (c),
5747 "negative length in array section in %qs clause",
5748 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5749 return error_mark_node;
5751 /* If there is a pointer type anywhere but in the very first
5752 array-section-subscript, the array section could be non-contiguous. */
5753 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_AFFINITY
5754 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
5755 && TREE_CODE (TREE_OPERAND (t, 0)) == OMP_ARRAY_SECTION)
5757 /* If any prior dimension has a non-one length, then deem this
5758 array section as non-contiguous. */
5759 for (tree d = TREE_OPERAND (t, 0); TREE_CODE (d) == OMP_ARRAY_SECTION;
5760 d = TREE_OPERAND (d, 0))
5762 tree d_length = TREE_OPERAND (d, 2);
5763 if (d_length == NULL_TREE || !integer_onep (d_length))
5765 error_at (OMP_CLAUSE_LOCATION (c),
5766 "array section is not contiguous in %qs clause",
5767 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5768 return error_mark_node;
5773 else
5775 error_at (OMP_CLAUSE_LOCATION (c),
5776 "%qE does not have pointer or array type", ret);
5777 return error_mark_node;
5779 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
5780 types.safe_push (TREE_TYPE (ret));
5781 /* We will need to evaluate lb more than once. */
5782 tree lb = cp_save_expr (low_bound);
5783 if (lb != low_bound)
5785 TREE_OPERAND (t, 1) = lb;
5786 low_bound = lb;
5788 /* Temporarily disable -fstrong-eval-order for array reductions.
5789 The SAVE_EXPR and COMPOUND_EXPR added if low_bound has side-effects
5790 is something the middle-end can't cope with and more importantly,
5791 it needs to be the actual base variable that is privatized, not some
5792 temporary assigned previous value of it. That, together with OpenMP
5793 saying how many times the side-effects are evaluated is unspecified,
5794 makes int *a, *b; ... reduction(+:a[a = b, 3:10]) really unspecified. */
5795 warning_sentinel s (flag_strong_eval_order,
5796 OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5797 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5798 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION);
5799 ret = grok_array_decl (OMP_CLAUSE_LOCATION (c), ret, low_bound, NULL,
5800 tf_warning_or_error);
5801 return ret;
5804 /* Handle array sections for clause C. */
5806 static bool
5807 handle_omp_array_sections (tree &c, enum c_omp_region_type ort)
5809 bool maybe_zero_len = false;
5810 unsigned int first_non_one = 0;
5811 auto_vec<tree, 10> types;
5812 tree *tp = &OMP_CLAUSE_DECL (c);
5813 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5814 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
5815 && TREE_CODE (*tp) == TREE_LIST
5816 && TREE_PURPOSE (*tp)
5817 && TREE_CODE (TREE_PURPOSE (*tp)) == TREE_VEC)
5818 tp = &TREE_VALUE (*tp);
5819 tree first = handle_omp_array_sections_1 (c, *tp, types,
5820 maybe_zero_len, first_non_one,
5821 ort);
5822 if (first == error_mark_node)
5823 return true;
5824 if (first == NULL_TREE)
5825 return false;
5826 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
5827 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_AFFINITY)
5829 tree t = *tp;
5830 tree tem = NULL_TREE;
5831 if (processing_template_decl)
5832 return false;
5833 /* Need to evaluate side effects in the length expressions
5834 if any. */
5835 while (TREE_CODE (t) == TREE_LIST)
5837 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
5839 if (tem == NULL_TREE)
5840 tem = TREE_VALUE (t);
5841 else
5842 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
5843 TREE_VALUE (t), tem);
5845 t = TREE_CHAIN (t);
5847 if (tem)
5848 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
5849 *tp = first;
5851 else
5853 unsigned int num = types.length (), i;
5854 tree t, side_effects = NULL_TREE, size = NULL_TREE;
5855 tree condition = NULL_TREE;
5857 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
5858 maybe_zero_len = true;
5859 if (processing_template_decl && maybe_zero_len)
5860 return false;
5862 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
5863 t = TREE_OPERAND (t, 0))
5865 gcc_assert (TREE_CODE (t) == OMP_ARRAY_SECTION);
5867 tree low_bound = TREE_OPERAND (t, 1);
5868 tree length = TREE_OPERAND (t, 2);
5870 i--;
5871 if (low_bound
5872 && TREE_CODE (low_bound) == INTEGER_CST
5873 && TYPE_PRECISION (TREE_TYPE (low_bound))
5874 > TYPE_PRECISION (sizetype))
5875 low_bound = fold_convert (sizetype, low_bound);
5876 if (length
5877 && TREE_CODE (length) == INTEGER_CST
5878 && TYPE_PRECISION (TREE_TYPE (length))
5879 > TYPE_PRECISION (sizetype))
5880 length = fold_convert (sizetype, length);
5881 if (low_bound == NULL_TREE)
5882 low_bound = integer_zero_node;
5883 if (!maybe_zero_len && i > first_non_one)
5885 if (integer_nonzerop (low_bound))
5886 goto do_warn_noncontiguous;
5887 if (length != NULL_TREE
5888 && TREE_CODE (length) == INTEGER_CST
5889 && TYPE_DOMAIN (types[i])
5890 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
5891 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
5892 == INTEGER_CST)
5894 tree size;
5895 size = size_binop (PLUS_EXPR,
5896 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
5897 size_one_node);
5898 if (!tree_int_cst_equal (length, size))
5900 do_warn_noncontiguous:
5901 error_at (OMP_CLAUSE_LOCATION (c),
5902 "array section is not contiguous in %qs "
5903 "clause",
5904 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
5905 return true;
5908 if (!processing_template_decl
5909 && length != NULL_TREE
5910 && TREE_SIDE_EFFECTS (length))
5912 if (side_effects == NULL_TREE)
5913 side_effects = length;
5914 else
5915 side_effects = build2 (COMPOUND_EXPR,
5916 TREE_TYPE (side_effects),
5917 length, side_effects);
5920 else if (processing_template_decl)
5921 continue;
5922 else
5924 tree l;
5926 if (i > first_non_one
5927 && ((length && integer_nonzerop (length))
5928 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5929 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5930 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION))
5931 continue;
5932 if (length)
5933 l = fold_convert (sizetype, length);
5934 else
5936 l = size_binop (PLUS_EXPR,
5937 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
5938 size_one_node);
5939 l = size_binop (MINUS_EXPR, l,
5940 fold_convert (sizetype, low_bound));
5942 if (i > first_non_one)
5944 l = fold_build2 (NE_EXPR, boolean_type_node, l,
5945 size_zero_node);
5946 if (condition == NULL_TREE)
5947 condition = l;
5948 else
5949 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
5950 l, condition);
5952 else if (size == NULL_TREE)
5954 size = size_in_bytes (TREE_TYPE (types[i]));
5955 tree eltype = TREE_TYPE (types[num - 1]);
5956 while (TREE_CODE (eltype) == ARRAY_TYPE)
5957 eltype = TREE_TYPE (eltype);
5958 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5959 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5960 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5961 size = size_binop (EXACT_DIV_EXPR, size,
5962 size_in_bytes (eltype));
5963 size = size_binop (MULT_EXPR, size, l);
5964 if (condition)
5965 size = fold_build3 (COND_EXPR, sizetype, condition,
5966 size, size_zero_node);
5968 else
5969 size = size_binop (MULT_EXPR, size, l);
5972 if (!processing_template_decl)
5974 if (side_effects)
5975 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
5976 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
5977 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
5978 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
5980 size = size_binop (MINUS_EXPR, size, size_one_node);
5981 size = save_expr (size);
5982 tree index_type = build_index_type (size);
5983 tree eltype = TREE_TYPE (first);
5984 while (TREE_CODE (eltype) == ARRAY_TYPE)
5985 eltype = TREE_TYPE (eltype);
5986 tree type = build_array_type (eltype, index_type);
5987 tree ptype = build_pointer_type (eltype);
5988 if (TYPE_REF_P (TREE_TYPE (t))
5989 && INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t))))
5990 t = convert_from_reference (t);
5991 else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5992 t = build_fold_addr_expr (t);
5993 tree t2 = build_fold_addr_expr (first);
5994 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5995 ptrdiff_type_node, t2);
5996 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
5997 ptrdiff_type_node, t2,
5998 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
5999 ptrdiff_type_node, t));
6000 if (tree_fits_shwi_p (t2))
6001 t = build2 (MEM_REF, type, t,
6002 build_int_cst (ptype, tree_to_shwi (t2)));
6003 else
6005 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
6006 sizetype, t2);
6007 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
6008 TREE_TYPE (t), t, t2);
6009 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
6011 OMP_CLAUSE_DECL (c) = t;
6012 return false;
6014 OMP_CLAUSE_DECL (c) = first;
6015 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR)
6016 return false;
6017 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
6018 || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
6019 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH
6020 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DETACH))
6021 OMP_CLAUSE_SIZE (c) = size;
6022 if (TREE_CODE (t) == FIELD_DECL)
6023 t = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
6025 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6026 return false;
6028 if (TREE_CODE (first) == INDIRECT_REF)
6030 /* Detect and skip adding extra nodes for pointer-to-member
6031 mappings. These are unsupported for now. */
6032 tree tmp = TREE_OPERAND (first, 0);
6034 if (TREE_CODE (tmp) == NON_LVALUE_EXPR)
6035 tmp = TREE_OPERAND (tmp, 0);
6037 if (TREE_CODE (tmp) == INDIRECT_REF)
6038 tmp = TREE_OPERAND (tmp, 0);
6040 if (TREE_CODE (tmp) == POINTER_PLUS_EXPR)
6042 tree offset = TREE_OPERAND (tmp, 1);
6043 STRIP_NOPS (offset);
6044 if (TYPE_PTRMEM_P (TREE_TYPE (offset)))
6046 sorry_at (OMP_CLAUSE_LOCATION (c),
6047 "pointer-to-member mapping %qE not supported",
6048 OMP_CLAUSE_DECL (c));
6049 return true;
6054 /* FIRST represents the first item of data that we are mapping.
6055 E.g. if we're mapping an array, FIRST might resemble
6056 "foo.bar.myarray[0]". */
6058 auto_vec<omp_addr_token *, 10> addr_tokens;
6060 if (!omp_parse_expr (addr_tokens, first))
6061 return true;
6063 cp_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
6065 tree nc = ai.expand_map_clause (c, first, addr_tokens, ort);
6066 if (nc != error_mark_node)
6068 using namespace omp_addr_tokenizer;
6070 if (ai.maybe_zero_length_array_section (c))
6071 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
6073 /* !!! If we're accessing a base decl via chained access
6074 methods (e.g. multiple indirections), duplicate clause
6075 detection won't work properly. Skip it in that case. */
6076 if ((addr_tokens[0]->type == STRUCTURE_BASE
6077 || addr_tokens[0]->type == ARRAY_BASE)
6078 && addr_tokens[0]->u.structure_base_kind == BASE_DECL
6079 && addr_tokens[1]->type == ACCESS_METHOD
6080 && omp_access_chain_p (addr_tokens, 1))
6081 c = nc;
6083 return false;
6087 return false;
6090 /* Return identifier to look up for omp declare reduction. */
6092 tree
6093 omp_reduction_id (enum tree_code reduction_code, tree reduction_id, tree type)
6095 const char *p = NULL;
6096 const char *m = NULL;
6097 switch (reduction_code)
6099 case PLUS_EXPR:
6100 case MULT_EXPR:
6101 case MINUS_EXPR:
6102 case BIT_AND_EXPR:
6103 case BIT_XOR_EXPR:
6104 case BIT_IOR_EXPR:
6105 case TRUTH_ANDIF_EXPR:
6106 case TRUTH_ORIF_EXPR:
6107 reduction_id = ovl_op_identifier (false, reduction_code);
6108 break;
6109 case MIN_EXPR:
6110 p = "min";
6111 break;
6112 case MAX_EXPR:
6113 p = "max";
6114 break;
6115 default:
6116 break;
6119 if (p == NULL)
6121 if (TREE_CODE (reduction_id) != IDENTIFIER_NODE)
6122 return error_mark_node;
6123 p = IDENTIFIER_POINTER (reduction_id);
6126 if (type != NULL_TREE)
6127 m = mangle_type_string (TYPE_MAIN_VARIANT (type));
6129 const char prefix[] = "omp declare reduction ";
6130 size_t lenp = sizeof (prefix);
6131 if (strncmp (p, prefix, lenp - 1) == 0)
6132 lenp = 1;
6133 size_t len = strlen (p);
6134 size_t lenm = m ? strlen (m) + 1 : 0;
6135 char *name = XALLOCAVEC (char, lenp + len + lenm);
6136 if (lenp > 1)
6137 memcpy (name, prefix, lenp - 1);
6138 memcpy (name + lenp - 1, p, len + 1);
6139 if (m)
6141 name[lenp + len - 1] = '~';
6142 memcpy (name + lenp + len, m, lenm);
6144 return get_identifier (name);
6147 /* Lookup OpenMP UDR ID for TYPE, return the corresponding artificial
6148 FUNCTION_DECL or NULL_TREE if not found. */
6150 static tree
6151 omp_reduction_lookup (location_t loc, tree id, tree type, tree *baselinkp,
6152 vec<tree> *ambiguousp)
6154 tree orig_id = id;
6155 tree baselink = NULL_TREE;
6156 if (identifier_p (id))
6158 cp_id_kind idk;
6159 bool nonint_cst_expression_p;
6160 const char *error_msg;
6161 id = omp_reduction_id (ERROR_MARK, id, type);
6162 tree decl = lookup_name (id);
6163 if (decl == NULL_TREE)
6164 decl = error_mark_node;
6165 id = finish_id_expression (id, decl, NULL_TREE, &idk, false, true,
6166 &nonint_cst_expression_p, false, true, false,
6167 false, &error_msg, loc);
6168 if (idk == CP_ID_KIND_UNQUALIFIED
6169 && identifier_p (id))
6171 vec<tree, va_gc> *args = NULL;
6172 vec_safe_push (args, build_reference_type (type));
6173 id = perform_koenig_lookup (id, args, tf_none);
6176 else if (TREE_CODE (id) == SCOPE_REF)
6177 id = lookup_qualified_name (TREE_OPERAND (id, 0),
6178 omp_reduction_id (ERROR_MARK,
6179 TREE_OPERAND (id, 1),
6180 type),
6181 LOOK_want::NORMAL, false);
6182 tree fns = id;
6183 id = NULL_TREE;
6184 if (fns && is_overloaded_fn (fns))
6186 for (lkp_iterator iter (get_fns (fns)); iter; ++iter)
6188 tree fndecl = *iter;
6189 if (TREE_CODE (fndecl) == FUNCTION_DECL)
6191 tree argtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
6192 if (same_type_p (TREE_TYPE (argtype), type))
6194 id = fndecl;
6195 break;
6200 if (id && BASELINK_P (fns))
6202 if (baselinkp)
6203 *baselinkp = fns;
6204 else
6205 baselink = fns;
6209 if (!id && CLASS_TYPE_P (type) && TYPE_BINFO (type))
6211 auto_vec<tree> ambiguous;
6212 tree binfo = TYPE_BINFO (type), base_binfo, ret = NULL_TREE;
6213 unsigned int ix;
6214 if (ambiguousp == NULL)
6215 ambiguousp = &ambiguous;
6216 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
6218 id = omp_reduction_lookup (loc, orig_id, BINFO_TYPE (base_binfo),
6219 baselinkp ? baselinkp : &baselink,
6220 ambiguousp);
6221 if (id == NULL_TREE)
6222 continue;
6223 if (!ambiguousp->is_empty ())
6224 ambiguousp->safe_push (id);
6225 else if (ret != NULL_TREE)
6227 ambiguousp->safe_push (ret);
6228 ambiguousp->safe_push (id);
6229 ret = NULL_TREE;
6231 else
6232 ret = id;
6234 if (ambiguousp != &ambiguous)
6235 return ret;
6236 if (!ambiguous.is_empty ())
6238 const char *str = _("candidates are:");
6239 unsigned int idx;
6240 tree udr;
6241 error_at (loc, "user defined reduction lookup is ambiguous");
6242 FOR_EACH_VEC_ELT (ambiguous, idx, udr)
6244 inform (DECL_SOURCE_LOCATION (udr), "%s %#qD", str, udr);
6245 if (idx == 0)
6246 str = get_spaces (str);
6248 ret = error_mark_node;
6249 baselink = NULL_TREE;
6251 id = ret;
6253 if (id && baselink)
6254 perform_or_defer_access_check (BASELINK_BINFO (baselink),
6255 id, id, tf_warning_or_error);
6256 return id;
6259 /* Helper function for cp_parser_omp_declare_reduction_exprs
6260 and tsubst_omp_udr.
6261 Remove CLEANUP_STMT for data (omp_priv variable).
6262 Also append INIT_EXPR for DECL_INITIAL of omp_priv after its
6263 DECL_EXPR. */
6265 tree
6266 cp_remove_omp_priv_cleanup_stmt (tree *tp, int *walk_subtrees, void *data)
6268 if (TYPE_P (*tp))
6269 *walk_subtrees = 0;
6270 else if (TREE_CODE (*tp) == CLEANUP_STMT && CLEANUP_DECL (*tp) == (tree) data)
6271 *tp = CLEANUP_BODY (*tp);
6272 else if (TREE_CODE (*tp) == DECL_EXPR)
6274 tree decl = DECL_EXPR_DECL (*tp);
6275 if (!processing_template_decl
6276 && decl == (tree) data
6277 && DECL_INITIAL (decl)
6278 && DECL_INITIAL (decl) != error_mark_node)
6280 tree list = NULL_TREE;
6281 append_to_statement_list_force (*tp, &list);
6282 tree init_expr = build2 (INIT_EXPR, void_type_node,
6283 decl, DECL_INITIAL (decl));
6284 DECL_INITIAL (decl) = NULL_TREE;
6285 append_to_statement_list_force (init_expr, &list);
6286 *tp = list;
6289 return NULL_TREE;
6292 /* Data passed from cp_check_omp_declare_reduction to
6293 cp_check_omp_declare_reduction_r. */
6295 struct cp_check_omp_declare_reduction_data
6297 location_t loc;
6298 tree stmts[7];
6299 bool combiner_p;
6302 /* Helper function for cp_check_omp_declare_reduction, called via
6303 cp_walk_tree. */
6305 static tree
6306 cp_check_omp_declare_reduction_r (tree *tp, int *, void *data)
6308 struct cp_check_omp_declare_reduction_data *udr_data
6309 = (struct cp_check_omp_declare_reduction_data *) data;
6310 if (SSA_VAR_P (*tp)
6311 && !DECL_ARTIFICIAL (*tp)
6312 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 0 : 3])
6313 && *tp != DECL_EXPR_DECL (udr_data->stmts[udr_data->combiner_p ? 1 : 4]))
6315 location_t loc = udr_data->loc;
6316 if (udr_data->combiner_p)
6317 error_at (loc, "%<#pragma omp declare reduction%> combiner refers to "
6318 "variable %qD which is not %<omp_out%> nor %<omp_in%>",
6319 *tp);
6320 else
6321 error_at (loc, "%<#pragma omp declare reduction%> initializer refers "
6322 "to variable %qD which is not %<omp_priv%> nor "
6323 "%<omp_orig%>",
6324 *tp);
6325 return *tp;
6327 return NULL_TREE;
6330 /* Diagnose violation of OpenMP #pragma omp declare reduction restrictions. */
6332 bool
6333 cp_check_omp_declare_reduction (tree udr)
6335 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (udr)));
6336 gcc_assert (TYPE_REF_P (type));
6337 type = TREE_TYPE (type);
6338 int i;
6339 location_t loc = DECL_SOURCE_LOCATION (udr);
6341 if (type == error_mark_node)
6342 return false;
6343 if (ARITHMETIC_TYPE_P (type))
6345 static enum tree_code predef_codes[]
6346 = { PLUS_EXPR, MULT_EXPR, MINUS_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR,
6347 BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR };
6348 for (i = 0; i < 8; i++)
6350 tree id = omp_reduction_id (predef_codes[i], NULL_TREE, NULL_TREE);
6351 const char *n1 = IDENTIFIER_POINTER (DECL_NAME (udr));
6352 const char *n2 = IDENTIFIER_POINTER (id);
6353 if (strncmp (n1, n2, IDENTIFIER_LENGTH (id)) == 0
6354 && (n1[IDENTIFIER_LENGTH (id)] == '~'
6355 || n1[IDENTIFIER_LENGTH (id)] == '\0'))
6356 break;
6359 if (i == 8
6360 && TREE_CODE (type) != COMPLEX_EXPR)
6362 const char prefix_minmax[] = "omp declare reduction m";
6363 size_t prefix_size = sizeof (prefix_minmax) - 1;
6364 const char *n = IDENTIFIER_POINTER (DECL_NAME (udr));
6365 if (strncmp (IDENTIFIER_POINTER (DECL_NAME (udr)),
6366 prefix_minmax, prefix_size) == 0
6367 && ((n[prefix_size] == 'i' && n[prefix_size + 1] == 'n')
6368 || (n[prefix_size] == 'a' && n[prefix_size + 1] == 'x'))
6369 && (n[prefix_size + 2] == '~' || n[prefix_size + 2] == '\0'))
6370 i = 0;
6372 if (i < 8)
6374 error_at (loc, "predeclared arithmetic type %qT in "
6375 "%<#pragma omp declare reduction%>", type);
6376 return false;
6379 else if (FUNC_OR_METHOD_TYPE_P (type)
6380 || TREE_CODE (type) == ARRAY_TYPE)
6382 error_at (loc, "function or array type %qT in "
6383 "%<#pragma omp declare reduction%>", type);
6384 return false;
6386 else if (TYPE_REF_P (type))
6388 error_at (loc, "reference type %qT in %<#pragma omp declare reduction%>",
6389 type);
6390 return false;
6392 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
6394 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
6395 "type %qT in %<#pragma omp declare reduction%>", type);
6396 return false;
6399 tree body = DECL_SAVED_TREE (udr);
6400 if (body == NULL_TREE || TREE_CODE (body) != STATEMENT_LIST)
6401 return true;
6403 tree_stmt_iterator tsi;
6404 struct cp_check_omp_declare_reduction_data data;
6405 memset (data.stmts, 0, sizeof data.stmts);
6406 for (i = 0, tsi = tsi_start (body);
6407 i < 7 && !tsi_end_p (tsi);
6408 i++, tsi_next (&tsi))
6409 data.stmts[i] = tsi_stmt (tsi);
6410 data.loc = loc;
6411 gcc_assert (tsi_end_p (tsi));
6412 if (i >= 3)
6414 gcc_assert (TREE_CODE (data.stmts[0]) == DECL_EXPR
6415 && TREE_CODE (data.stmts[1]) == DECL_EXPR);
6416 if (warning_suppressed_p (DECL_EXPR_DECL (data.stmts[0]) /* What warning? */))
6417 return true;
6418 data.combiner_p = true;
6419 if (cp_walk_tree (&data.stmts[2], cp_check_omp_declare_reduction_r,
6420 &data, NULL))
6421 suppress_warning (DECL_EXPR_DECL (data.stmts[0]) /* What warning? */);
6423 if (i >= 6)
6425 gcc_assert (TREE_CODE (data.stmts[3]) == DECL_EXPR
6426 && TREE_CODE (data.stmts[4]) == DECL_EXPR);
6427 data.combiner_p = false;
6428 if (cp_walk_tree (&data.stmts[5], cp_check_omp_declare_reduction_r,
6429 &data, NULL)
6430 || cp_walk_tree (&DECL_INITIAL (DECL_EXPR_DECL (data.stmts[3])),
6431 cp_check_omp_declare_reduction_r, &data, NULL))
6432 suppress_warning (DECL_EXPR_DECL (data.stmts[0]) /* Wat warning? */);
6433 if (i == 7)
6434 gcc_assert (TREE_CODE (data.stmts[6]) == DECL_EXPR);
6436 return true;
6439 /* Helper function of finish_omp_clauses. Clone STMT as if we were making
6440 an inline call. But, remap
6441 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
6442 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
6444 static tree
6445 clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
6446 tree decl, tree placeholder)
6448 copy_body_data id;
6449 hash_map<tree, tree> decl_map;
6451 decl_map.put (omp_decl1, placeholder);
6452 decl_map.put (omp_decl2, decl);
6453 memset (&id, 0, sizeof (id));
6454 id.src_fn = DECL_CONTEXT (omp_decl1);
6455 id.dst_fn = current_function_decl;
6456 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
6457 id.decl_map = &decl_map;
6459 id.copy_decl = copy_decl_no_change;
6460 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
6461 id.transform_new_cfg = true;
6462 id.transform_return_to_modify = false;
6463 id.eh_lp_nr = 0;
6464 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
6465 return stmt;
6468 /* Helper function of finish_omp_clauses, called via cp_walk_tree.
6469 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
6471 static tree
6472 find_omp_placeholder_r (tree *tp, int *, void *data)
6474 if (*tp == (tree) data)
6475 return *tp;
6476 return NULL_TREE;
6479 /* Helper function of finish_omp_clauses. Handle OMP_CLAUSE_REDUCTION C.
6480 Return true if there is some error and the clause should be removed. */
6482 static bool
6483 finish_omp_reduction_clause (tree c, bool *need_default_ctor, bool *need_dtor)
6485 tree t = OMP_CLAUSE_DECL (c);
6486 bool predefined = false;
6487 if (TREE_CODE (t) == TREE_LIST)
6489 gcc_assert (processing_template_decl);
6490 return false;
6492 tree type = TREE_TYPE (t);
6493 if (TREE_CODE (t) == MEM_REF)
6494 type = TREE_TYPE (type);
6495 if (TYPE_REF_P (type))
6496 type = TREE_TYPE (type);
6497 if (TREE_CODE (type) == ARRAY_TYPE)
6499 tree oatype = type;
6500 gcc_assert (TREE_CODE (t) != MEM_REF);
6501 while (TREE_CODE (type) == ARRAY_TYPE)
6502 type = TREE_TYPE (type);
6503 if (!processing_template_decl)
6505 t = require_complete_type (t);
6506 if (t == error_mark_node
6507 || !complete_type_or_else (oatype, NULL_TREE))
6508 return true;
6509 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
6510 TYPE_SIZE_UNIT (type));
6511 if (integer_zerop (size))
6513 error_at (OMP_CLAUSE_LOCATION (c),
6514 "%qE in %<reduction%> clause is a zero size array",
6515 omp_clause_printable_decl (t));
6516 return true;
6518 size = size_binop (MINUS_EXPR, size, size_one_node);
6519 size = save_expr (size);
6520 tree index_type = build_index_type (size);
6521 tree atype = build_array_type (type, index_type);
6522 tree ptype = build_pointer_type (type);
6523 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6524 t = build_fold_addr_expr (t);
6525 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
6526 OMP_CLAUSE_DECL (c) = t;
6529 if (type == error_mark_node)
6530 return true;
6531 else if (ARITHMETIC_TYPE_P (type))
6532 switch (OMP_CLAUSE_REDUCTION_CODE (c))
6534 case PLUS_EXPR:
6535 case MULT_EXPR:
6536 case MINUS_EXPR:
6537 case TRUTH_ANDIF_EXPR:
6538 case TRUTH_ORIF_EXPR:
6539 predefined = true;
6540 break;
6541 case MIN_EXPR:
6542 case MAX_EXPR:
6543 if (TREE_CODE (type) == COMPLEX_TYPE)
6544 break;
6545 predefined = true;
6546 break;
6547 case BIT_AND_EXPR:
6548 case BIT_IOR_EXPR:
6549 case BIT_XOR_EXPR:
6550 if (FLOAT_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
6551 break;
6552 predefined = true;
6553 break;
6554 default:
6555 break;
6557 else if (TYPE_READONLY (type))
6559 error_at (OMP_CLAUSE_LOCATION (c),
6560 "%qE has const type for %<reduction%>",
6561 omp_clause_printable_decl (t));
6562 return true;
6564 else if (!processing_template_decl)
6566 t = require_complete_type (t);
6567 if (t == error_mark_node)
6568 return true;
6569 OMP_CLAUSE_DECL (c) = t;
6572 if (predefined)
6574 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
6575 return false;
6577 else if (processing_template_decl)
6579 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
6580 return true;
6581 return false;
6584 tree id = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
6586 type = TYPE_MAIN_VARIANT (type);
6587 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = NULL_TREE;
6588 if (id == NULL_TREE)
6589 id = omp_reduction_id (OMP_CLAUSE_REDUCTION_CODE (c),
6590 NULL_TREE, NULL_TREE);
6591 id = omp_reduction_lookup (OMP_CLAUSE_LOCATION (c), id, type, NULL, NULL);
6592 if (id)
6594 if (id == error_mark_node)
6595 return true;
6596 mark_used (id);
6597 tree body = DECL_SAVED_TREE (id);
6598 if (!body)
6599 return true;
6600 if (TREE_CODE (body) == STATEMENT_LIST)
6602 tree_stmt_iterator tsi;
6603 tree placeholder = NULL_TREE, decl_placeholder = NULL_TREE;
6604 int i;
6605 tree stmts[7];
6606 tree atype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (id)));
6607 atype = TREE_TYPE (atype);
6608 bool need_static_cast = !same_type_p (type, atype);
6609 memset (stmts, 0, sizeof stmts);
6610 for (i = 0, tsi = tsi_start (body);
6611 i < 7 && !tsi_end_p (tsi);
6612 i++, tsi_next (&tsi))
6613 stmts[i] = tsi_stmt (tsi);
6614 gcc_assert (tsi_end_p (tsi));
6616 if (i >= 3)
6618 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
6619 && TREE_CODE (stmts[1]) == DECL_EXPR);
6620 placeholder = build_lang_decl (VAR_DECL, NULL_TREE, type);
6621 DECL_ARTIFICIAL (placeholder) = 1;
6622 DECL_IGNORED_P (placeholder) = 1;
6623 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
6624 if (TREE_CODE (t) == MEM_REF)
6626 decl_placeholder = build_lang_decl (VAR_DECL, NULL_TREE,
6627 type);
6628 DECL_ARTIFICIAL (decl_placeholder) = 1;
6629 DECL_IGNORED_P (decl_placeholder) = 1;
6630 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
6632 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[0])))
6633 cxx_mark_addressable (placeholder);
6634 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[1]))
6635 && (decl_placeholder
6636 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
6637 cxx_mark_addressable (decl_placeholder ? decl_placeholder
6638 : OMP_CLAUSE_DECL (c));
6639 tree omp_out = placeholder;
6640 tree omp_in = decl_placeholder ? decl_placeholder
6641 : convert_from_reference (OMP_CLAUSE_DECL (c));
6642 if (need_static_cast)
6644 tree rtype = build_reference_type (atype);
6645 omp_out = build_static_cast (input_location,
6646 rtype, omp_out,
6647 tf_warning_or_error);
6648 omp_in = build_static_cast (input_location,
6649 rtype, omp_in,
6650 tf_warning_or_error);
6651 if (omp_out == error_mark_node || omp_in == error_mark_node)
6652 return true;
6653 omp_out = convert_from_reference (omp_out);
6654 omp_in = convert_from_reference (omp_in);
6656 OMP_CLAUSE_REDUCTION_MERGE (c)
6657 = clone_omp_udr (stmts[2], DECL_EXPR_DECL (stmts[0]),
6658 DECL_EXPR_DECL (stmts[1]), omp_in, omp_out);
6660 if (i >= 6)
6662 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
6663 && TREE_CODE (stmts[4]) == DECL_EXPR);
6664 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[3]))
6665 && (decl_placeholder
6666 || !TYPE_REF_P (TREE_TYPE (OMP_CLAUSE_DECL (c)))))
6667 cxx_mark_addressable (decl_placeholder ? decl_placeholder
6668 : OMP_CLAUSE_DECL (c));
6669 if (TREE_ADDRESSABLE (DECL_EXPR_DECL (stmts[4])))
6670 cxx_mark_addressable (placeholder);
6671 tree omp_priv = decl_placeholder ? decl_placeholder
6672 : convert_from_reference (OMP_CLAUSE_DECL (c));
6673 tree omp_orig = placeholder;
6674 if (need_static_cast)
6676 if (i == 7)
6678 error_at (OMP_CLAUSE_LOCATION (c),
6679 "user defined reduction with constructor "
6680 "initializer for base class %qT", atype);
6681 return true;
6683 tree rtype = build_reference_type (atype);
6684 omp_priv = build_static_cast (input_location,
6685 rtype, omp_priv,
6686 tf_warning_or_error);
6687 omp_orig = build_static_cast (input_location,
6688 rtype, omp_orig,
6689 tf_warning_or_error);
6690 if (omp_priv == error_mark_node
6691 || omp_orig == error_mark_node)
6692 return true;
6693 omp_priv = convert_from_reference (omp_priv);
6694 omp_orig = convert_from_reference (omp_orig);
6696 if (i == 6)
6697 *need_default_ctor = true;
6698 OMP_CLAUSE_REDUCTION_INIT (c)
6699 = clone_omp_udr (stmts[5], DECL_EXPR_DECL (stmts[4]),
6700 DECL_EXPR_DECL (stmts[3]),
6701 omp_priv, omp_orig);
6702 if (cp_walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
6703 find_omp_placeholder_r, placeholder, NULL))
6704 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
6706 else if (i >= 3)
6708 if (CLASS_TYPE_P (type) && !pod_type_p (type))
6709 *need_default_ctor = true;
6710 else
6712 tree init;
6713 tree v = decl_placeholder ? decl_placeholder
6714 : convert_from_reference (t);
6715 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
6716 init = build_constructor (TREE_TYPE (v), NULL);
6717 else
6718 init = fold_convert (TREE_TYPE (v), integer_zero_node);
6719 OMP_CLAUSE_REDUCTION_INIT (c)
6720 = cp_build_init_expr (v, init);
6725 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
6726 *need_dtor = true;
6727 else
6729 error_at (OMP_CLAUSE_LOCATION (c),
6730 "user defined reduction not found for %qE",
6731 omp_clause_printable_decl (t));
6732 return true;
6734 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == MEM_REF)
6735 gcc_assert (TYPE_SIZE_UNIT (type)
6736 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST);
6737 return false;
6740 /* Called from finish_struct_1. linear(this) or linear(this:step)
6741 clauses might not be finalized yet because the class has been incomplete
6742 when parsing #pragma omp declare simd methods. Fix those up now. */
6744 void
6745 finish_omp_declare_simd_methods (tree t)
6747 if (processing_template_decl)
6748 return;
6750 for (tree x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x))
6752 if (TREE_CODE (x) == USING_DECL
6753 || !DECL_IOBJ_MEMBER_FUNCTION_P (x))
6754 continue;
6755 tree ods = lookup_attribute ("omp declare simd", DECL_ATTRIBUTES (x));
6756 if (!ods || !TREE_VALUE (ods))
6757 continue;
6758 for (tree c = TREE_VALUE (TREE_VALUE (ods)); c; c = OMP_CLAUSE_CHAIN (c))
6759 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
6760 && integer_zerop (OMP_CLAUSE_DECL (c))
6761 && OMP_CLAUSE_LINEAR_STEP (c)
6762 && TYPE_PTR_P (TREE_TYPE (OMP_CLAUSE_LINEAR_STEP (c))))
6764 tree s = OMP_CLAUSE_LINEAR_STEP (c);
6765 s = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, s);
6766 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MULT_EXPR,
6767 sizetype, s, TYPE_SIZE_UNIT (t));
6768 OMP_CLAUSE_LINEAR_STEP (c) = s;
6773 /* Adjust sink depend/doacross clause to take into account pointer offsets.
6775 Return TRUE if there was a problem processing the offset, and the
6776 whole clause should be removed. */
6778 static bool
6779 cp_finish_omp_clause_doacross_sink (tree sink_clause)
6781 tree t = OMP_CLAUSE_DECL (sink_clause);
6782 gcc_assert (TREE_CODE (t) == TREE_LIST);
6784 /* Make sure we don't adjust things twice for templates. */
6785 if (processing_template_decl)
6786 return false;
6788 for (; t; t = TREE_CHAIN (t))
6790 tree decl = TREE_VALUE (t);
6791 if (TYPE_PTR_P (TREE_TYPE (decl)))
6793 tree offset = TREE_PURPOSE (t);
6794 bool neg = wi::neg_p (wi::to_wide (offset));
6795 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
6796 decl = mark_rvalue_use (decl);
6797 decl = convert_from_reference (decl);
6798 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (sink_clause),
6799 neg ? MINUS_EXPR : PLUS_EXPR,
6800 decl, offset);
6801 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (sink_clause),
6802 MINUS_EXPR, sizetype,
6803 fold_convert (sizetype, t2),
6804 fold_convert (sizetype, decl));
6805 if (t2 == error_mark_node)
6806 return true;
6807 TREE_PURPOSE (t) = t2;
6810 return false;
6813 /* Finish OpenMP iterators ITER. Return true if they are errorneous
6814 and clauses containing them should be removed. */
6816 static bool
6817 cp_omp_finish_iterators (tree iter)
6819 bool ret = false;
6820 for (tree it = iter; it; it = TREE_CHAIN (it))
6822 tree var = TREE_VEC_ELT (it, 0);
6823 tree begin = TREE_VEC_ELT (it, 1);
6824 tree end = TREE_VEC_ELT (it, 2);
6825 tree step = TREE_VEC_ELT (it, 3);
6826 tree orig_step;
6827 tree type = TREE_TYPE (var);
6828 location_t loc = DECL_SOURCE_LOCATION (var);
6829 if (type == error_mark_node)
6831 ret = true;
6832 continue;
6834 if (type_dependent_expression_p (var))
6835 continue;
6836 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
6838 error_at (loc, "iterator %qD has neither integral nor pointer type",
6839 var);
6840 ret = true;
6841 continue;
6843 else if (TYPE_READONLY (type))
6845 error_at (loc, "iterator %qD has const qualified type", var);
6846 ret = true;
6847 continue;
6849 if (type_dependent_expression_p (begin)
6850 || type_dependent_expression_p (end)
6851 || type_dependent_expression_p (step))
6852 continue;
6853 else if (error_operand_p (step))
6855 ret = true;
6856 continue;
6858 else if (!INTEGRAL_TYPE_P (TREE_TYPE (step)))
6860 error_at (EXPR_LOC_OR_LOC (step, loc),
6861 "iterator step with non-integral type");
6862 ret = true;
6863 continue;
6866 begin = mark_rvalue_use (begin);
6867 end = mark_rvalue_use (end);
6868 step = mark_rvalue_use (step);
6869 begin = cp_build_c_cast (input_location, type, begin,
6870 tf_warning_or_error);
6871 end = cp_build_c_cast (input_location, type, end,
6872 tf_warning_or_error);
6873 orig_step = step;
6874 if (!processing_template_decl)
6875 step = orig_step = save_expr (step);
6876 tree stype = POINTER_TYPE_P (type) ? sizetype : type;
6877 step = cp_build_c_cast (input_location, stype, step,
6878 tf_warning_or_error);
6879 if (POINTER_TYPE_P (type) && !processing_template_decl)
6881 begin = save_expr (begin);
6882 step = pointer_int_sum (loc, PLUS_EXPR, begin, step);
6883 step = fold_build2_loc (loc, MINUS_EXPR, sizetype,
6884 fold_convert (sizetype, step),
6885 fold_convert (sizetype, begin));
6886 step = fold_convert (ssizetype, step);
6888 if (!processing_template_decl)
6890 begin = maybe_constant_value (begin);
6891 end = maybe_constant_value (end);
6892 step = maybe_constant_value (step);
6893 orig_step = maybe_constant_value (orig_step);
6895 if (integer_zerop (step))
6897 error_at (loc, "iterator %qD has zero step", var);
6898 ret = true;
6899 continue;
6902 if (begin == error_mark_node
6903 || end == error_mark_node
6904 || step == error_mark_node
6905 || orig_step == error_mark_node)
6907 ret = true;
6908 continue;
6911 if (!processing_template_decl)
6913 begin = fold_build_cleanup_point_expr (TREE_TYPE (begin), begin);
6914 end = fold_build_cleanup_point_expr (TREE_TYPE (end), end);
6915 step = fold_build_cleanup_point_expr (TREE_TYPE (step), step);
6916 orig_step = fold_build_cleanup_point_expr (TREE_TYPE (orig_step),
6917 orig_step);
6919 hash_set<tree> pset;
6920 tree it2;
6921 for (it2 = TREE_CHAIN (it); it2; it2 = TREE_CHAIN (it2))
6923 tree var2 = TREE_VEC_ELT (it2, 0);
6924 tree begin2 = TREE_VEC_ELT (it2, 1);
6925 tree end2 = TREE_VEC_ELT (it2, 2);
6926 tree step2 = TREE_VEC_ELT (it2, 3);
6927 location_t loc2 = DECL_SOURCE_LOCATION (var2);
6928 if (cp_walk_tree (&begin2, find_omp_placeholder_r, var, &pset))
6930 error_at (EXPR_LOC_OR_LOC (begin2, loc2),
6931 "begin expression refers to outer iterator %qD", var);
6932 break;
6934 else if (cp_walk_tree (&end2, find_omp_placeholder_r, var, &pset))
6936 error_at (EXPR_LOC_OR_LOC (end2, loc2),
6937 "end expression refers to outer iterator %qD", var);
6938 break;
6940 else if (cp_walk_tree (&step2, find_omp_placeholder_r, var, &pset))
6942 error_at (EXPR_LOC_OR_LOC (step2, loc2),
6943 "step expression refers to outer iterator %qD", var);
6944 break;
6947 if (it2)
6949 ret = true;
6950 continue;
6952 TREE_VEC_ELT (it, 1) = begin;
6953 TREE_VEC_ELT (it, 2) = end;
6954 if (processing_template_decl)
6955 TREE_VEC_ELT (it, 3) = orig_step;
6956 else
6958 TREE_VEC_ELT (it, 3) = step;
6959 TREE_VEC_ELT (it, 4) = orig_step;
6962 return ret;
6965 /* Ensure that pointers are used in OpenACC attach and detach clauses.
6966 Return true if an error has been detected. */
6968 static bool
6969 cp_oacc_check_attachments (tree c)
6971 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
6972 return false;
6974 /* OpenACC attach / detach clauses must be pointers. */
6975 if (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
6976 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
6978 tree t = OMP_CLAUSE_DECL (c);
6979 tree type;
6981 while (TREE_CODE (t) == OMP_ARRAY_SECTION)
6982 t = TREE_OPERAND (t, 0);
6984 type = TREE_TYPE (t);
6986 if (TREE_CODE (type) == REFERENCE_TYPE)
6987 type = TREE_TYPE (type);
6989 if (TREE_CODE (type) != POINTER_TYPE)
6991 error_at (OMP_CLAUSE_LOCATION (c), "expected pointer in %qs clause",
6992 user_omp_clause_code_name (c, true));
6993 return true;
6997 return false;
7000 /* For all elements of CLAUSES, validate them vs OpenMP constraints.
7001 Remove any elements from the list that are invalid. */
7003 tree
7004 finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
7006 bitmap_head generic_head, firstprivate_head, lastprivate_head;
7007 bitmap_head aligned_head, map_head, map_field_head, map_firstprivate_head;
7008 bitmap_head oacc_reduction_head, is_on_device_head;
7009 tree c, t, *pc;
7010 tree safelen = NULL_TREE;
7011 bool openacc = (ort & C_ORT_ACC) != 0;
7012 bool branch_seen = false;
7013 bool copyprivate_seen = false;
7014 bool ordered_seen = false;
7015 bool order_seen = false;
7016 bool schedule_seen = false;
7017 bool oacc_async = false;
7018 bool indir_component_ref_p = false;
7019 tree last_iterators = NULL_TREE;
7020 bool last_iterators_remove = false;
7021 /* 1 if normal/task reduction has been seen, -1 if inscan reduction
7022 has been seen, -2 if mixed inscan/normal reduction diagnosed. */
7023 int reduction_seen = 0;
7024 bool allocate_seen = false;
7025 tree detach_seen = NULL_TREE;
7026 bool mergeable_seen = false;
7027 bool implicit_moved = false;
7028 bool target_in_reduction_seen = false;
7030 bitmap_obstack_initialize (NULL);
7031 bitmap_initialize (&generic_head, &bitmap_default_obstack);
7032 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
7033 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
7034 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
7035 /* If ort == C_ORT_OMP_DECLARE_SIMD used as uniform_head instead. */
7036 bitmap_initialize (&map_head, &bitmap_default_obstack);
7037 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
7038 bitmap_initialize (&map_firstprivate_head, &bitmap_default_obstack);
7039 /* If ort == C_ORT_OMP used as nontemporal_head or use_device_xxx_head
7040 instead and for ort == C_ORT_OMP_TARGET used as in_reduction_head. */
7041 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
7042 bitmap_initialize (&is_on_device_head, &bitmap_default_obstack);
7044 if (openacc)
7045 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
7046 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
7048 oacc_async = true;
7049 break;
7052 tree *grp_start_p = NULL, grp_sentinel = NULL_TREE;
7054 for (pc = &clauses, c = clauses; c ; c = *pc)
7056 bool remove = false;
7057 bool field_ok = false;
7059 /* We've reached the end of a list of expanded nodes. Reset the group
7060 start pointer. */
7061 if (c == grp_sentinel)
7062 grp_start_p = NULL;
7064 switch (OMP_CLAUSE_CODE (c))
7066 case OMP_CLAUSE_SHARED:
7067 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
7068 goto check_dup_generic;
7069 case OMP_CLAUSE_PRIVATE:
7070 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
7071 goto check_dup_generic;
7072 case OMP_CLAUSE_REDUCTION:
7073 if (reduction_seen == 0)
7074 reduction_seen = OMP_CLAUSE_REDUCTION_INSCAN (c) ? -1 : 1;
7075 else if (reduction_seen != -2
7076 && reduction_seen != (OMP_CLAUSE_REDUCTION_INSCAN (c)
7077 ? -1 : 1))
7079 error_at (OMP_CLAUSE_LOCATION (c),
7080 "%<inscan%> and non-%<inscan%> %<reduction%> clauses "
7081 "on the same construct");
7082 reduction_seen = -2;
7084 /* FALLTHRU */
7085 case OMP_CLAUSE_IN_REDUCTION:
7086 case OMP_CLAUSE_TASK_REDUCTION:
7087 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
7088 t = OMP_CLAUSE_DECL (c);
7089 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
7091 if (handle_omp_array_sections (c, ort))
7093 remove = true;
7094 break;
7096 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
7097 && OMP_CLAUSE_REDUCTION_INSCAN (c))
7099 error_at (OMP_CLAUSE_LOCATION (c),
7100 "%<inscan%> %<reduction%> clause with array "
7101 "section");
7102 remove = true;
7103 break;
7105 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
7107 while (TREE_CODE (t) == OMP_ARRAY_SECTION)
7108 t = TREE_OPERAND (t, 0);
7110 else
7112 gcc_assert (TREE_CODE (t) == MEM_REF);
7113 t = TREE_OPERAND (t, 0);
7114 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
7115 t = TREE_OPERAND (t, 0);
7116 if (TREE_CODE (t) == ADDR_EXPR
7117 || INDIRECT_REF_P (t))
7118 t = TREE_OPERAND (t, 0);
7120 tree n = omp_clause_decl_field (t);
7121 if (n)
7122 t = n;
7123 goto check_dup_generic_t;
7125 if (oacc_async)
7126 cxx_mark_addressable (t);
7127 goto check_dup_generic;
7128 case OMP_CLAUSE_COPYPRIVATE:
7129 copyprivate_seen = true;
7130 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
7131 goto check_dup_generic;
7132 case OMP_CLAUSE_COPYIN:
7133 goto check_dup_generic;
7134 case OMP_CLAUSE_LINEAR:
7135 field_ok = ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP);
7136 t = OMP_CLAUSE_DECL (c);
7137 if (ort != C_ORT_OMP_DECLARE_SIMD
7138 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
7140 if (OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c))
7142 error_at (OMP_CLAUSE_LOCATION (c),
7143 "modifier should not be specified in %<linear%> "
7144 "clause on %<simd%> or %<for%> constructs when "
7145 "not using OpenMP 5.2 modifiers");
7146 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
7148 else if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_VAL)
7150 error_at (OMP_CLAUSE_LOCATION (c),
7151 "modifier other than %<val%> specified in "
7152 "%<linear%> clause on %<simd%> or %<for%> "
7153 "constructs when using OpenMP 5.2 modifiers");
7154 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
7157 if ((VAR_P (t) || TREE_CODE (t) == PARM_DECL)
7158 && !type_dependent_expression_p (t))
7160 tree type = TREE_TYPE (t);
7161 if ((OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
7162 || OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_UVAL)
7163 && !TYPE_REF_P (type))
7165 error_at (OMP_CLAUSE_LOCATION (c),
7166 "linear clause with %qs modifier applied to "
7167 "non-reference variable with %qT type",
7168 OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF
7169 ? "ref" : "uval", TREE_TYPE (t));
7170 remove = true;
7171 break;
7173 if (TYPE_REF_P (type))
7174 type = TREE_TYPE (type);
7175 if (OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_REF)
7177 if (!INTEGRAL_TYPE_P (type)
7178 && !TYPE_PTR_P (type))
7180 error_at (OMP_CLAUSE_LOCATION (c),
7181 "linear clause applied to non-integral "
7182 "non-pointer variable with %qT type",
7183 TREE_TYPE (t));
7184 remove = true;
7185 break;
7189 t = OMP_CLAUSE_LINEAR_STEP (c);
7190 if (t == NULL_TREE)
7191 t = integer_one_node;
7192 if (t == error_mark_node)
7194 remove = true;
7195 break;
7197 else if (!type_dependent_expression_p (t)
7198 && !INTEGRAL_TYPE_P (TREE_TYPE (t))
7199 && (ort != C_ORT_OMP_DECLARE_SIMD
7200 || TREE_CODE (t) != PARM_DECL
7201 || !TYPE_REF_P (TREE_TYPE (t))
7202 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (t)))))
7204 error_at (OMP_CLAUSE_LOCATION (c),
7205 "linear step expression must be integral");
7206 remove = true;
7207 break;
7209 else
7211 t = mark_rvalue_use (t);
7212 if (ort == C_ORT_OMP_DECLARE_SIMD && TREE_CODE (t) == PARM_DECL)
7214 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
7215 goto check_dup_generic;
7217 if (!processing_template_decl
7218 && (VAR_P (OMP_CLAUSE_DECL (c))
7219 || TREE_CODE (OMP_CLAUSE_DECL (c)) == PARM_DECL))
7221 if (ort == C_ORT_OMP_DECLARE_SIMD)
7223 t = maybe_constant_value (t);
7224 if (TREE_CODE (t) != INTEGER_CST)
7226 error_at (OMP_CLAUSE_LOCATION (c),
7227 "%<linear%> clause step %qE is neither "
7228 "constant nor a parameter", t);
7229 remove = true;
7230 break;
7233 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7234 tree type = TREE_TYPE (OMP_CLAUSE_DECL (c));
7235 if (TYPE_REF_P (type))
7236 type = TREE_TYPE (type);
7237 if (OMP_CLAUSE_LINEAR_KIND (c) == OMP_CLAUSE_LINEAR_REF)
7239 type = build_pointer_type (type);
7240 tree d = fold_convert (type, OMP_CLAUSE_DECL (c));
7241 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
7242 d, t);
7243 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
7244 MINUS_EXPR, sizetype,
7245 fold_convert (sizetype, t),
7246 fold_convert (sizetype, d));
7247 if (t == error_mark_node)
7249 remove = true;
7250 break;
7253 else if (TYPE_PTR_P (type)
7254 /* Can't multiply the step yet if *this
7255 is still incomplete type. */
7256 && (ort != C_ORT_OMP_DECLARE_SIMD
7257 || TREE_CODE (OMP_CLAUSE_DECL (c)) != PARM_DECL
7258 || !DECL_ARTIFICIAL (OMP_CLAUSE_DECL (c))
7259 || DECL_NAME (OMP_CLAUSE_DECL (c))
7260 != this_identifier
7261 || !TYPE_BEING_DEFINED (TREE_TYPE (type))))
7263 tree d = convert_from_reference (OMP_CLAUSE_DECL (c));
7264 t = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
7265 d, t);
7266 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c),
7267 MINUS_EXPR, sizetype,
7268 fold_convert (sizetype, t),
7269 fold_convert (sizetype, d));
7270 if (t == error_mark_node)
7272 remove = true;
7273 break;
7276 else
7277 t = fold_convert (type, t);
7279 OMP_CLAUSE_LINEAR_STEP (c) = t;
7281 goto check_dup_generic;
7282 check_dup_generic:
7283 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7284 if (t)
7286 if (!remove && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED)
7287 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7289 else
7290 t = OMP_CLAUSE_DECL (c);
7291 check_dup_generic_t:
7292 if (t == current_class_ptr
7293 && ((ort != C_ORT_OMP_DECLARE_SIMD && !openacc)
7294 || (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_LINEAR
7295 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_UNIFORM)))
7297 error_at (OMP_CLAUSE_LOCATION (c),
7298 "%<this%> allowed in OpenMP only in %<declare simd%>"
7299 " clauses");
7300 remove = true;
7301 break;
7303 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
7304 && (!field_ok || TREE_CODE (t) != FIELD_DECL))
7306 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7307 break;
7308 if (DECL_P (t))
7309 error_at (OMP_CLAUSE_LOCATION (c),
7310 "%qD is not a variable in clause %qs", t,
7311 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7312 else
7313 error_at (OMP_CLAUSE_LOCATION (c),
7314 "%qE is not a variable in clause %qs", t,
7315 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7316 remove = true;
7318 else if ((openacc
7319 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
7320 || (ort == C_ORT_OMP
7321 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
7322 || (OMP_CLAUSE_CODE (c)
7323 == OMP_CLAUSE_USE_DEVICE_ADDR)))
7324 || (ort == C_ORT_OMP_TARGET
7325 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION))
7327 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
7328 && (bitmap_bit_p (&generic_head, DECL_UID (t))
7329 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))))
7331 error_at (OMP_CLAUSE_LOCATION (c),
7332 "%qD appears more than once in data-sharing "
7333 "clauses", t);
7334 remove = true;
7335 break;
7337 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
7338 target_in_reduction_seen = true;
7339 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
7341 error_at (OMP_CLAUSE_LOCATION (c),
7342 openacc
7343 ? "%qD appears more than once in reduction clauses"
7344 : "%qD appears more than once in data clauses",
7346 remove = true;
7348 else
7349 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
7351 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7352 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
7353 || bitmap_bit_p (&lastprivate_head, DECL_UID (t))
7354 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
7356 error_at (OMP_CLAUSE_LOCATION (c),
7357 "%qD appears more than once in data clauses", t);
7358 remove = true;
7360 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
7361 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_HAS_DEVICE_ADDR
7362 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
7363 && bitmap_bit_p (&map_head, DECL_UID (t)))
7365 if (openacc)
7366 error_at (OMP_CLAUSE_LOCATION (c),
7367 "%qD appears more than once in data clauses", t);
7368 else
7369 error_at (OMP_CLAUSE_LOCATION (c),
7370 "%qD appears both in data and map clauses", t);
7371 remove = true;
7373 else
7374 bitmap_set_bit (&generic_head, DECL_UID (t));
7375 if (!field_ok)
7376 break;
7377 handle_field_decl:
7378 if (!remove
7379 && TREE_CODE (t) == FIELD_DECL
7380 && t == OMP_CLAUSE_DECL (c))
7382 OMP_CLAUSE_DECL (c)
7383 = omp_privatize_field (t, (OMP_CLAUSE_CODE (c)
7384 == OMP_CLAUSE_SHARED));
7385 if (OMP_CLAUSE_DECL (c) == error_mark_node)
7386 remove = true;
7388 break;
7390 case OMP_CLAUSE_FIRSTPRIVATE:
7391 if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c) && !implicit_moved)
7393 move_implicit:
7394 implicit_moved = true;
7395 /* Move firstprivate and map clauses with
7396 OMP_CLAUSE_{FIRSTPRIVATE,MAP}_IMPLICIT set to the end of
7397 clauses chain. */
7398 tree cl1 = NULL_TREE, cl2 = NULL_TREE;
7399 tree *pc1 = pc, *pc2 = &cl1, *pc3 = &cl2;
7400 while (*pc1)
7401 if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_FIRSTPRIVATE
7402 && OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (*pc1))
7404 *pc3 = *pc1;
7405 pc3 = &OMP_CLAUSE_CHAIN (*pc3);
7406 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
7408 else if (OMP_CLAUSE_CODE (*pc1) == OMP_CLAUSE_MAP
7409 && OMP_CLAUSE_MAP_IMPLICIT (*pc1))
7411 *pc2 = *pc1;
7412 pc2 = &OMP_CLAUSE_CHAIN (*pc2);
7413 *pc1 = OMP_CLAUSE_CHAIN (*pc1);
7415 else
7416 pc1 = &OMP_CLAUSE_CHAIN (*pc1);
7417 *pc3 = NULL;
7418 *pc2 = cl2;
7419 *pc1 = cl1;
7420 continue;
7422 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7423 if (t)
7424 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7425 else
7426 t = OMP_CLAUSE_DECL (c);
7427 if (!openacc && t == current_class_ptr)
7429 error_at (OMP_CLAUSE_LOCATION (c),
7430 "%<this%> allowed in OpenMP only in %<declare simd%>"
7431 " clauses");
7432 remove = true;
7433 break;
7435 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
7436 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
7437 || TREE_CODE (t) != FIELD_DECL))
7439 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7440 break;
7441 if (DECL_P (t))
7442 error_at (OMP_CLAUSE_LOCATION (c),
7443 "%qD is not a variable in clause %<firstprivate%>",
7445 else
7446 error_at (OMP_CLAUSE_LOCATION (c),
7447 "%qE is not a variable in clause %<firstprivate%>",
7449 remove = true;
7451 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
7452 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c)
7453 && bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
7454 remove = true;
7455 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7456 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
7457 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
7459 error_at (OMP_CLAUSE_LOCATION (c),
7460 "%qD appears more than once in data clauses", t);
7461 remove = true;
7463 else if (bitmap_bit_p (&map_head, DECL_UID (t))
7464 || bitmap_bit_p (&map_field_head, DECL_UID (t)))
7466 if (openacc)
7467 error_at (OMP_CLAUSE_LOCATION (c),
7468 "%qD appears more than once in data clauses", t);
7469 else if (OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT (c)
7470 && !OMP_CLAUSE_FIRSTPRIVATE_IMPLICIT_TARGET (c))
7471 /* Silently drop the clause. */;
7472 else
7473 error_at (OMP_CLAUSE_LOCATION (c),
7474 "%qD appears both in data and map clauses", t);
7475 remove = true;
7477 else
7478 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
7479 goto handle_field_decl;
7481 case OMP_CLAUSE_LASTPRIVATE:
7482 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7483 if (t)
7484 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7485 else
7486 t = OMP_CLAUSE_DECL (c);
7487 if (!openacc && t == current_class_ptr)
7489 error_at (OMP_CLAUSE_LOCATION (c),
7490 "%<this%> allowed in OpenMP only in %<declare simd%>"
7491 " clauses");
7492 remove = true;
7493 break;
7495 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL
7496 && ((ort & C_ORT_OMP_DECLARE_SIMD) != C_ORT_OMP
7497 || TREE_CODE (t) != FIELD_DECL))
7499 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7500 break;
7501 if (DECL_P (t))
7502 error_at (OMP_CLAUSE_LOCATION (c),
7503 "%qD is not a variable in clause %<lastprivate%>",
7505 else
7506 error_at (OMP_CLAUSE_LOCATION (c),
7507 "%qE is not a variable in clause %<lastprivate%>",
7509 remove = true;
7511 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
7512 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
7514 error_at (OMP_CLAUSE_LOCATION (c),
7515 "%qD appears more than once in data clauses", t);
7516 remove = true;
7518 else
7519 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
7520 goto handle_field_decl;
7522 case OMP_CLAUSE_IF:
7523 case OMP_CLAUSE_SELF:
7524 t = OMP_CLAUSE_OPERAND (c, 0);
7525 t = maybe_convert_cond (t);
7526 if (t == error_mark_node)
7527 remove = true;
7528 else if (!processing_template_decl)
7529 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7530 OMP_CLAUSE_OPERAND (c, 0) = t;
7531 break;
7533 case OMP_CLAUSE_FINAL:
7534 t = OMP_CLAUSE_FINAL_EXPR (c);
7535 t = maybe_convert_cond (t);
7536 if (t == error_mark_node)
7537 remove = true;
7538 else if (!processing_template_decl)
7539 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7540 OMP_CLAUSE_FINAL_EXPR (c) = t;
7541 break;
7543 case OMP_CLAUSE_GANG:
7544 /* Operand 1 is the gang static: argument. */
7545 t = OMP_CLAUSE_OPERAND (c, 1);
7546 if (t != NULL_TREE)
7548 if (t == error_mark_node)
7549 remove = true;
7550 else if (!type_dependent_expression_p (t)
7551 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7553 error_at (OMP_CLAUSE_LOCATION (c),
7554 "%<gang%> static expression must be integral");
7555 remove = true;
7557 else
7559 t = mark_rvalue_use (t);
7560 if (!processing_template_decl)
7562 t = maybe_constant_value (t);
7563 if (TREE_CODE (t) == INTEGER_CST
7564 && tree_int_cst_sgn (t) != 1
7565 && t != integer_minus_one_node)
7567 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7568 "%<gang%> static value must be "
7569 "positive");
7570 t = integer_one_node;
7572 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7575 OMP_CLAUSE_OPERAND (c, 1) = t;
7577 /* Check operand 0, the num argument. */
7578 /* FALLTHRU */
7580 case OMP_CLAUSE_WORKER:
7581 case OMP_CLAUSE_VECTOR:
7582 if (OMP_CLAUSE_OPERAND (c, 0) == NULL_TREE)
7583 break;
7584 /* FALLTHRU */
7586 case OMP_CLAUSE_NUM_TASKS:
7587 case OMP_CLAUSE_NUM_TEAMS:
7588 case OMP_CLAUSE_NUM_THREADS:
7589 case OMP_CLAUSE_NUM_GANGS:
7590 case OMP_CLAUSE_NUM_WORKERS:
7591 case OMP_CLAUSE_VECTOR_LENGTH:
7592 t = OMP_CLAUSE_OPERAND (c, 0);
7593 if (t == error_mark_node)
7594 remove = true;
7595 else if (!type_dependent_expression_p (t)
7596 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7598 switch (OMP_CLAUSE_CODE (c))
7600 case OMP_CLAUSE_GANG:
7601 error_at (OMP_CLAUSE_LOCATION (c),
7602 "%<gang%> num expression must be integral"); break;
7603 case OMP_CLAUSE_VECTOR:
7604 error_at (OMP_CLAUSE_LOCATION (c),
7605 "%<vector%> length expression must be integral");
7606 break;
7607 case OMP_CLAUSE_WORKER:
7608 error_at (OMP_CLAUSE_LOCATION (c),
7609 "%<worker%> num expression must be integral");
7610 break;
7611 default:
7612 error_at (OMP_CLAUSE_LOCATION (c),
7613 "%qs expression must be integral",
7614 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7616 remove = true;
7618 else
7620 t = mark_rvalue_use (t);
7621 if (!processing_template_decl)
7623 t = maybe_constant_value (t);
7624 if (TREE_CODE (t) == INTEGER_CST
7625 && tree_int_cst_sgn (t) != 1)
7627 switch (OMP_CLAUSE_CODE (c))
7629 case OMP_CLAUSE_GANG:
7630 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7631 "%<gang%> num value must be positive");
7632 break;
7633 case OMP_CLAUSE_VECTOR:
7634 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7635 "%<vector%> length value must be "
7636 "positive");
7637 break;
7638 case OMP_CLAUSE_WORKER:
7639 warning_at (OMP_CLAUSE_LOCATION (c), 0,
7640 "%<worker%> num value must be "
7641 "positive");
7642 break;
7643 default:
7644 warning_at (OMP_CLAUSE_LOCATION (c),
7645 (flag_openmp || flag_openmp_simd)
7646 ? OPT_Wopenmp : 0,
7647 "%qs value must be positive",
7648 omp_clause_code_name
7649 [OMP_CLAUSE_CODE (c)]);
7651 t = integer_one_node;
7653 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7655 OMP_CLAUSE_OPERAND (c, 0) = t;
7657 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
7658 && OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c)
7659 && !remove)
7661 t = OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c);
7662 if (t == error_mark_node)
7663 remove = true;
7664 else if (!type_dependent_expression_p (t)
7665 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7667 error_at (OMP_CLAUSE_LOCATION (c),
7668 "%qs expression must be integral",
7669 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7670 remove = true;
7672 else
7674 t = mark_rvalue_use (t);
7675 if (!processing_template_decl)
7677 t = maybe_constant_value (t);
7678 if (TREE_CODE (t) == INTEGER_CST
7679 && tree_int_cst_sgn (t) != 1)
7681 warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
7682 "%qs value must be positive",
7683 omp_clause_code_name
7684 [OMP_CLAUSE_CODE (c)]);
7685 t = NULL_TREE;
7687 else
7688 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7689 tree upper = OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c);
7690 if (t
7691 && TREE_CODE (t) == INTEGER_CST
7692 && TREE_CODE (upper) == INTEGER_CST
7693 && tree_int_cst_lt (upper, t))
7695 warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
7696 "%<num_teams%> lower bound %qE bigger "
7697 "than upper bound %qE", t, upper);
7698 t = NULL_TREE;
7701 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = t;
7704 break;
7706 case OMP_CLAUSE_SCHEDULE:
7707 t = OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c);
7708 if (t == NULL)
7710 else if (t == error_mark_node)
7711 remove = true;
7712 else if (!type_dependent_expression_p (t)
7713 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7715 error_at (OMP_CLAUSE_LOCATION (c),
7716 "schedule chunk size expression must be integral");
7717 remove = true;
7719 else
7721 t = mark_rvalue_use (t);
7722 if (!processing_template_decl)
7724 t = maybe_constant_value (t);
7725 if (TREE_CODE (t) == INTEGER_CST
7726 && tree_int_cst_sgn (t) != 1)
7728 warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
7729 "chunk size value must be positive");
7730 t = integer_one_node;
7732 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7734 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
7736 if (!remove)
7737 schedule_seen = true;
7738 break;
7740 case OMP_CLAUSE_SIMDLEN:
7741 case OMP_CLAUSE_SAFELEN:
7742 t = OMP_CLAUSE_OPERAND (c, 0);
7743 if (t == error_mark_node)
7744 remove = true;
7745 else if (!type_dependent_expression_p (t)
7746 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7748 error_at (OMP_CLAUSE_LOCATION (c),
7749 "%qs length expression must be integral",
7750 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7751 remove = true;
7753 else
7755 t = mark_rvalue_use (t);
7756 if (!processing_template_decl)
7758 t = maybe_constant_value (t);
7759 if (TREE_CODE (t) != INTEGER_CST
7760 || tree_int_cst_sgn (t) != 1)
7762 error_at (OMP_CLAUSE_LOCATION (c),
7763 "%qs length expression must be positive "
7764 "constant integer expression",
7765 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
7766 remove = true;
7769 OMP_CLAUSE_OPERAND (c, 0) = t;
7770 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SAFELEN)
7771 safelen = c;
7773 break;
7775 case OMP_CLAUSE_ASYNC:
7776 t = OMP_CLAUSE_ASYNC_EXPR (c);
7777 if (t == error_mark_node)
7778 remove = true;
7779 else if (!type_dependent_expression_p (t)
7780 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7782 error_at (OMP_CLAUSE_LOCATION (c),
7783 "%<async%> expression must be integral");
7784 remove = true;
7786 else
7788 t = mark_rvalue_use (t);
7789 if (!processing_template_decl)
7790 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7791 OMP_CLAUSE_ASYNC_EXPR (c) = t;
7793 break;
7795 case OMP_CLAUSE_WAIT:
7796 t = OMP_CLAUSE_WAIT_EXPR (c);
7797 if (t == error_mark_node)
7798 remove = true;
7799 else if (!processing_template_decl)
7800 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7801 OMP_CLAUSE_WAIT_EXPR (c) = t;
7802 break;
7804 case OMP_CLAUSE_THREAD_LIMIT:
7805 t = OMP_CLAUSE_THREAD_LIMIT_EXPR (c);
7806 if (t == error_mark_node)
7807 remove = true;
7808 else if (!type_dependent_expression_p (t)
7809 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7811 error_at (OMP_CLAUSE_LOCATION (c),
7812 "%<thread_limit%> expression must be integral");
7813 remove = true;
7815 else
7817 t = mark_rvalue_use (t);
7818 if (!processing_template_decl)
7820 t = maybe_constant_value (t);
7821 if (TREE_CODE (t) == INTEGER_CST
7822 && tree_int_cst_sgn (t) != 1)
7824 warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
7825 "%<thread_limit%> value must be positive");
7826 t = integer_one_node;
7828 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7830 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
7832 break;
7834 case OMP_CLAUSE_DEVICE:
7835 t = OMP_CLAUSE_DEVICE_ID (c);
7836 if (t == error_mark_node)
7837 remove = true;
7838 else if (!type_dependent_expression_p (t)
7839 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7841 error_at (OMP_CLAUSE_LOCATION (c),
7842 "%<device%> id must be integral");
7843 remove = true;
7845 else if (OMP_CLAUSE_DEVICE_ANCESTOR (c)
7846 && TREE_CODE (t) == INTEGER_CST
7847 && !integer_onep (t))
7849 error_at (OMP_CLAUSE_LOCATION (c),
7850 "the %<device%> clause expression must evaluate to "
7851 "%<1%>");
7852 remove = true;
7854 else
7856 t = mark_rvalue_use (t);
7857 if (!processing_template_decl)
7858 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7859 OMP_CLAUSE_DEVICE_ID (c) = t;
7861 break;
7863 case OMP_CLAUSE_DIST_SCHEDULE:
7864 t = OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c);
7865 if (t == NULL)
7867 else if (t == error_mark_node)
7868 remove = true;
7869 else if (!type_dependent_expression_p (t)
7870 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7872 error_at (OMP_CLAUSE_LOCATION (c),
7873 "%<dist_schedule%> chunk size expression must be "
7874 "integral");
7875 remove = true;
7877 else
7879 t = mark_rvalue_use (t);
7880 if (!processing_template_decl)
7881 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7882 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
7884 break;
7886 case OMP_CLAUSE_ALIGNED:
7887 t = OMP_CLAUSE_DECL (c);
7888 if (t == current_class_ptr && ort != C_ORT_OMP_DECLARE_SIMD)
7890 error_at (OMP_CLAUSE_LOCATION (c),
7891 "%<this%> allowed in OpenMP only in %<declare simd%>"
7892 " clauses");
7893 remove = true;
7894 break;
7896 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7898 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7899 break;
7900 if (DECL_P (t))
7901 error_at (OMP_CLAUSE_LOCATION (c),
7902 "%qD is not a variable in %<aligned%> clause", t);
7903 else
7904 error_at (OMP_CLAUSE_LOCATION (c),
7905 "%qE is not a variable in %<aligned%> clause", t);
7906 remove = true;
7908 else if (!type_dependent_expression_p (t)
7909 && !TYPE_PTR_P (TREE_TYPE (t))
7910 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
7911 && (!TYPE_REF_P (TREE_TYPE (t))
7912 || (!INDIRECT_TYPE_P (TREE_TYPE (TREE_TYPE (t)))
7913 && (TREE_CODE (TREE_TYPE (TREE_TYPE (t)))
7914 != ARRAY_TYPE))))
7916 error_at (OMP_CLAUSE_LOCATION (c),
7917 "%qE in %<aligned%> clause is neither a pointer nor "
7918 "an array nor a reference to pointer or array", t);
7919 remove = true;
7921 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
7923 error_at (OMP_CLAUSE_LOCATION (c),
7924 "%qD appears more than once in %<aligned%> clauses",
7926 remove = true;
7928 else
7929 bitmap_set_bit (&aligned_head, DECL_UID (t));
7930 t = OMP_CLAUSE_ALIGNED_ALIGNMENT (c);
7931 if (t == error_mark_node)
7932 remove = true;
7933 else if (t == NULL_TREE)
7934 break;
7935 else if (!type_dependent_expression_p (t)
7936 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
7938 error_at (OMP_CLAUSE_LOCATION (c),
7939 "%<aligned%> clause alignment expression must "
7940 "be integral");
7941 remove = true;
7943 else
7945 t = mark_rvalue_use (t);
7946 if (!processing_template_decl)
7948 t = maybe_constant_value (t);
7949 if (TREE_CODE (t) != INTEGER_CST
7950 || tree_int_cst_sgn (t) != 1)
7952 error_at (OMP_CLAUSE_LOCATION (c),
7953 "%<aligned%> clause alignment expression must "
7954 "be positive constant integer expression");
7955 remove = true;
7957 else
7958 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
7960 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = t;
7962 break;
7964 case OMP_CLAUSE_NONTEMPORAL:
7965 t = OMP_CLAUSE_DECL (c);
7966 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
7968 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
7969 break;
7970 if (DECL_P (t))
7971 error_at (OMP_CLAUSE_LOCATION (c),
7972 "%qD is not a variable in %<nontemporal%> clause",
7974 else
7975 error_at (OMP_CLAUSE_LOCATION (c),
7976 "%qE is not a variable in %<nontemporal%> clause",
7978 remove = true;
7980 else if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
7982 error_at (OMP_CLAUSE_LOCATION (c),
7983 "%qD appears more than once in %<nontemporal%> "
7984 "clauses", t);
7985 remove = true;
7987 else
7988 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
7989 break;
7991 case OMP_CLAUSE_ALLOCATE:
7992 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
7993 if (t)
7994 omp_note_field_privatization (t, OMP_CLAUSE_DECL (c));
7995 else
7996 t = OMP_CLAUSE_DECL (c);
7997 if (t == current_class_ptr)
7999 error_at (OMP_CLAUSE_LOCATION (c),
8000 "%<this%> not allowed in %<allocate%> clause");
8001 remove = true;
8002 break;
8004 if (!VAR_P (t)
8005 && TREE_CODE (t) != PARM_DECL
8006 && TREE_CODE (t) != FIELD_DECL)
8008 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
8009 break;
8010 if (DECL_P (t))
8011 error_at (OMP_CLAUSE_LOCATION (c),
8012 "%qD is not a variable in %<allocate%> clause", t);
8013 else
8014 error_at (OMP_CLAUSE_LOCATION (c),
8015 "%qE is not a variable in %<allocate%> clause", t);
8016 remove = true;
8018 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
8020 warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
8021 "%qD appears more than once in %<allocate%> clauses",
8023 remove = true;
8025 else
8027 bitmap_set_bit (&aligned_head, DECL_UID (t));
8028 allocate_seen = true;
8030 tree allocator, align;
8031 align = OMP_CLAUSE_ALLOCATE_ALIGN (c);
8032 if (error_operand_p (align))
8034 remove = true;
8035 break;
8037 if (align)
8039 if (!type_dependent_expression_p (align)
8040 && !INTEGRAL_TYPE_P (TREE_TYPE (align)))
8042 error_at (OMP_CLAUSE_LOCATION (c),
8043 "%<allocate%> clause %<align%> modifier "
8044 "argument needs to be positive constant "
8045 "power of two integer expression");
8046 remove = true;
8048 else
8050 align = mark_rvalue_use (align);
8051 if (!processing_template_decl)
8053 align = maybe_constant_value (align);
8054 if (TREE_CODE (align) != INTEGER_CST
8055 || !tree_fits_uhwi_p (align)
8056 || !integer_pow2p (align))
8058 error_at (OMP_CLAUSE_LOCATION (c),
8059 "%<allocate%> clause %<align%> modifier "
8060 "argument needs to be positive constant "
8061 "power of two integer expression");
8062 remove = true;
8066 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
8068 allocator = OMP_CLAUSE_ALLOCATE_ALLOCATOR (c);
8069 if (error_operand_p (allocator))
8071 remove = true;
8072 break;
8074 if (allocator == NULL_TREE)
8075 goto handle_field_decl;
8076 tree allocatort;
8077 allocatort = TYPE_MAIN_VARIANT (TREE_TYPE (allocator));
8078 if (!type_dependent_expression_p (allocator)
8079 && (TREE_CODE (allocatort) != ENUMERAL_TYPE
8080 || TYPE_NAME (allocatort) == NULL_TREE
8081 || TREE_CODE (TYPE_NAME (allocatort)) != TYPE_DECL
8082 || (DECL_NAME (TYPE_NAME (allocatort))
8083 != get_identifier ("omp_allocator_handle_t"))
8084 || (TYPE_CONTEXT (allocatort)
8085 != DECL_CONTEXT (global_namespace))))
8087 error_at (OMP_CLAUSE_LOCATION (c),
8088 "%<allocate%> clause allocator expression has "
8089 "type %qT rather than %<omp_allocator_handle_t%>",
8090 TREE_TYPE (allocator));
8091 remove = true;
8092 break;
8094 else
8096 allocator = mark_rvalue_use (allocator);
8097 if (!processing_template_decl)
8098 allocator = maybe_constant_value (allocator);
8099 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
8101 goto handle_field_decl;
8103 case OMP_CLAUSE_DOACROSS:
8104 t = OMP_CLAUSE_DECL (c);
8105 if (t == NULL_TREE)
8106 break;
8107 if (OMP_CLAUSE_DOACROSS_KIND (c) == OMP_CLAUSE_DOACROSS_SINK)
8109 if (cp_finish_omp_clause_doacross_sink (c))
8110 remove = true;
8111 break;
8113 gcc_unreachable ();
8114 case OMP_CLAUSE_DEPEND:
8115 case OMP_CLAUSE_AFFINITY:
8116 t = OMP_CLAUSE_DECL (c);
8117 if (TREE_CODE (t) == TREE_LIST
8118 && TREE_PURPOSE (t)
8119 && TREE_CODE (TREE_PURPOSE (t)) == TREE_VEC)
8121 if (TREE_PURPOSE (t) != last_iterators)
8122 last_iterators_remove
8123 = cp_omp_finish_iterators (TREE_PURPOSE (t));
8124 last_iterators = TREE_PURPOSE (t);
8125 t = TREE_VALUE (t);
8126 if (last_iterators_remove)
8127 t = error_mark_node;
8129 else
8130 last_iterators = NULL_TREE;
8132 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
8134 if (handle_omp_array_sections (c, ort))
8135 remove = true;
8136 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
8137 && (OMP_CLAUSE_DEPEND_KIND (c)
8138 == OMP_CLAUSE_DEPEND_DEPOBJ))
8140 error_at (OMP_CLAUSE_LOCATION (c),
8141 "%<depend%> clause with %<depobj%> dependence "
8142 "type on array section");
8143 remove = true;
8145 break;
8147 if (t == error_mark_node)
8148 remove = true;
8149 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
8150 && t == ridpointers[RID_OMP_ALL_MEMORY])
8152 if (OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_OUT
8153 && OMP_CLAUSE_DEPEND_KIND (c) != OMP_CLAUSE_DEPEND_INOUT)
8155 error_at (OMP_CLAUSE_LOCATION (c),
8156 "%<omp_all_memory%> used with %<depend%> kind "
8157 "other than %<out%> or %<inout%>");
8158 remove = true;
8160 if (processing_template_decl)
8161 break;
8163 else if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
8164 break;
8165 else if (!lvalue_p (t))
8167 if (DECL_P (t))
8168 error_at (OMP_CLAUSE_LOCATION (c),
8169 "%qD is not lvalue expression nor array section "
8170 "in %qs clause", t,
8171 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8172 else
8173 error_at (OMP_CLAUSE_LOCATION (c),
8174 "%qE is not lvalue expression nor array section "
8175 "in %qs clause", t,
8176 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8177 remove = true;
8179 else if (TREE_CODE (t) == COMPONENT_REF
8180 && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL
8181 && DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
8183 error_at (OMP_CLAUSE_LOCATION (c),
8184 "bit-field %qE in %qs clause", t,
8185 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8186 remove = true;
8188 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
8189 && OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_DEPOBJ)
8191 if (!c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
8192 ? TREE_TYPE (TREE_TYPE (t))
8193 : TREE_TYPE (t)))
8195 error_at (OMP_CLAUSE_LOCATION (c),
8196 "%qE does not have %<omp_depend_t%> type in "
8197 "%<depend%> clause with %<depobj%> dependence "
8198 "type", t);
8199 remove = true;
8202 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
8203 && c_omp_depend_t_p (TYPE_REF_P (TREE_TYPE (t))
8204 ? TREE_TYPE (TREE_TYPE (t))
8205 : TREE_TYPE (t)))
8207 error_at (OMP_CLAUSE_LOCATION (c),
8208 "%qE should not have %<omp_depend_t%> type in "
8209 "%<depend%> clause with dependence type other than "
8210 "%<depobj%>", t);
8211 remove = true;
8213 if (!remove)
8215 if (t == ridpointers[RID_OMP_ALL_MEMORY])
8216 t = null_pointer_node;
8217 else
8219 tree addr = cp_build_addr_expr (t, tf_warning_or_error);
8220 if (addr == error_mark_node)
8222 remove = true;
8223 break;
8225 t = cp_build_indirect_ref (OMP_CLAUSE_LOCATION (c),
8226 addr, RO_UNARY_STAR,
8227 tf_warning_or_error);
8228 if (t == error_mark_node)
8230 remove = true;
8231 break;
8234 if (TREE_CODE (OMP_CLAUSE_DECL (c)) == TREE_LIST
8235 && TREE_PURPOSE (OMP_CLAUSE_DECL (c))
8236 && (TREE_CODE (TREE_PURPOSE (OMP_CLAUSE_DECL (c)))
8237 == TREE_VEC))
8238 TREE_VALUE (OMP_CLAUSE_DECL (c)) = t;
8239 else
8240 OMP_CLAUSE_DECL (c) = t;
8242 break;
8243 case OMP_CLAUSE_DETACH:
8244 t = OMP_CLAUSE_DECL (c);
8245 if (detach_seen)
8247 error_at (OMP_CLAUSE_LOCATION (c),
8248 "too many %qs clauses on a task construct",
8249 "detach");
8250 remove = true;
8251 break;
8253 else if (error_operand_p (t))
8255 remove = true;
8256 break;
8258 else
8260 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
8261 if (!type_dependent_expression_p (t)
8262 && (!INTEGRAL_TYPE_P (type)
8263 || TREE_CODE (type) != ENUMERAL_TYPE
8264 || TYPE_NAME (type) == NULL_TREE
8265 || (DECL_NAME (TYPE_NAME (type))
8266 != get_identifier ("omp_event_handle_t"))))
8268 error_at (OMP_CLAUSE_LOCATION (c),
8269 "%<detach%> clause event handle "
8270 "has type %qT rather than "
8271 "%<omp_event_handle_t%>",
8272 type);
8273 remove = true;
8275 detach_seen = c;
8276 cxx_mark_addressable (t);
8278 break;
8280 case OMP_CLAUSE_MAP:
8281 if (OMP_CLAUSE_MAP_IMPLICIT (c) && !implicit_moved)
8282 goto move_implicit;
8283 /* FALLTHRU */
8284 case OMP_CLAUSE_TO:
8285 case OMP_CLAUSE_FROM:
8286 case OMP_CLAUSE__CACHE_:
8288 using namespace omp_addr_tokenizer;
8289 auto_vec<omp_addr_token *, 10> addr_tokens;
8291 t = OMP_CLAUSE_DECL (c);
8292 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
8294 grp_start_p = pc;
8295 grp_sentinel = OMP_CLAUSE_CHAIN (c);
8297 if (handle_omp_array_sections (c, ort))
8298 remove = true;
8299 else
8301 t = OMP_CLAUSE_DECL (c);
8302 if (TREE_CODE (t) != OMP_ARRAY_SECTION
8303 && !type_dependent_expression_p (t)
8304 && !omp_mappable_type (TREE_TYPE (t)))
8306 error_at (OMP_CLAUSE_LOCATION (c),
8307 "array section does not have mappable type "
8308 "in %qs clause",
8309 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8310 if (TREE_TYPE (t) != error_mark_node
8311 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
8312 cxx_incomplete_type_inform (TREE_TYPE (t));
8313 remove = true;
8315 while (TREE_CODE (t) == ARRAY_REF)
8316 t = TREE_OPERAND (t, 0);
8318 if (type_dependent_expression_p (t))
8319 break;
8321 cp_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
8323 if (!ai.map_supported_p ()
8324 || !omp_parse_expr (addr_tokens, t))
8326 sorry_at (OMP_CLAUSE_LOCATION (c),
8327 "unsupported map expression %qE",
8328 OMP_CLAUSE_DECL (c));
8329 remove = true;
8330 break;
8333 /* This check is to determine if this will be the only map
8334 node created for this clause. Otherwise, we'll check
8335 the following FIRSTPRIVATE_POINTER,
8336 FIRSTPRIVATE_REFERENCE or ATTACH_DETACH node on the next
8337 iteration(s) of the loop. */
8338 if (addr_tokens.length () >= 4
8339 && addr_tokens[0]->type == STRUCTURE_BASE
8340 && addr_tokens[0]->u.structure_base_kind == BASE_DECL
8341 && addr_tokens[1]->type == ACCESS_METHOD
8342 && addr_tokens[2]->type == COMPONENT_SELECTOR
8343 && addr_tokens[3]->type == ACCESS_METHOD
8344 && (addr_tokens[3]->u.access_kind == ACCESS_DIRECT
8345 || (addr_tokens[3]->u.access_kind
8346 == ACCESS_INDEXED_ARRAY)))
8348 tree rt = addr_tokens[1]->expr;
8350 gcc_assert (DECL_P (rt));
8352 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8353 && OMP_CLAUSE_MAP_IMPLICIT (c)
8354 && (bitmap_bit_p (&map_head, DECL_UID (rt))
8355 || bitmap_bit_p (&map_field_head, DECL_UID (rt))
8356 || bitmap_bit_p (&map_firstprivate_head,
8357 DECL_UID (rt))))
8359 remove = true;
8360 break;
8362 if (bitmap_bit_p (&map_field_head, DECL_UID (rt)))
8363 break;
8364 if (bitmap_bit_p (&map_head, DECL_UID (rt)))
8366 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
8367 error_at (OMP_CLAUSE_LOCATION (c),
8368 "%qD appears more than once in motion"
8369 " clauses", rt);
8370 else if (openacc)
8371 error_at (OMP_CLAUSE_LOCATION (c),
8372 "%qD appears more than once in data"
8373 " clauses", rt);
8374 else
8375 error_at (OMP_CLAUSE_LOCATION (c),
8376 "%qD appears more than once in map"
8377 " clauses", rt);
8378 remove = true;
8380 else
8382 bitmap_set_bit (&map_head, DECL_UID (rt));
8383 bitmap_set_bit (&map_field_head, DECL_UID (rt));
8387 if (cp_oacc_check_attachments (c))
8388 remove = true;
8389 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8390 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
8391 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
8392 && !OMP_CLAUSE_SIZE (c))
8393 /* In this case, we have a single array element which is a
8394 pointer, and we already set OMP_CLAUSE_SIZE in
8395 handle_omp_array_sections above. For attach/detach
8396 clauses, reset the OMP_CLAUSE_SIZE (representing a bias)
8397 to zero here. */
8398 OMP_CLAUSE_SIZE (c) = size_zero_node;
8399 break;
8401 else if (type_dependent_expression_p (t))
8402 break;
8403 else if (!omp_parse_expr (addr_tokens, t))
8405 sorry_at (OMP_CLAUSE_LOCATION (c),
8406 "unsupported map expression %qE",
8407 OMP_CLAUSE_DECL (c));
8408 remove = true;
8409 break;
8411 if (t == error_mark_node)
8413 remove = true;
8414 break;
8416 /* OpenACC attach / detach clauses must be pointers. */
8417 if (cp_oacc_check_attachments (c))
8419 remove = true;
8420 break;
8422 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8423 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH
8424 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_DETACH)
8425 && !OMP_CLAUSE_SIZE (c))
8426 /* For attach/detach clauses, set OMP_CLAUSE_SIZE (representing a
8427 bias) to zero here, so it is not set erroneously to the
8428 pointer size later on in gimplify.cc. */
8429 OMP_CLAUSE_SIZE (c) = size_zero_node;
8431 cp_omp_address_inspector ai (OMP_CLAUSE_LOCATION (c), t);
8433 if (!ai.check_clause (c))
8435 remove = true;
8436 break;
8439 if (!ai.map_supported_p ())
8441 sorry_at (OMP_CLAUSE_LOCATION (c),
8442 "unsupported map expression %qE",
8443 OMP_CLAUSE_DECL (c));
8444 remove = true;
8445 break;
8448 gcc_assert ((addr_tokens[0]->type == ARRAY_BASE
8449 || addr_tokens[0]->type == STRUCTURE_BASE)
8450 && addr_tokens[1]->type == ACCESS_METHOD);
8452 t = addr_tokens[1]->expr;
8454 /* This is used to prevent cxx_mark_addressable from being called
8455 on 'this' for expressions like 'this->a', i.e. typical member
8456 accesses. */
8457 indir_component_ref_p
8458 = (addr_tokens[0]->type == STRUCTURE_BASE
8459 && addr_tokens[1]->u.access_kind != ACCESS_DIRECT);
8461 if (addr_tokens[0]->u.structure_base_kind != BASE_DECL)
8462 goto skip_decl_checks;
8464 /* For OpenMP, we can access a struct "t" and "t.d" on the same
8465 mapping. OpenACC allows multiple fields of the same structure
8466 to be written. */
8467 if (addr_tokens[0]->type == STRUCTURE_BASE
8468 && (bitmap_bit_p (&map_field_head, DECL_UID (t))
8469 || (!openacc && bitmap_bit_p (&map_head, DECL_UID (t)))))
8470 goto skip_decl_checks;
8472 if (!processing_template_decl && TREE_CODE (t) == FIELD_DECL)
8474 OMP_CLAUSE_DECL (c)
8475 = finish_non_static_data_member (t, NULL_TREE, NULL_TREE);
8476 break;
8478 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
8480 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
8481 break;
8482 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8483 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
8484 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ALWAYS_POINTER
8485 || OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_ATTACH_DETACH
8486 || (!openacc && EXPR_P (t))))
8487 break;
8488 if (DECL_P (t))
8489 error_at (OMP_CLAUSE_LOCATION (c),
8490 "%qD is not a variable in %qs clause", t,
8491 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8492 else
8493 error_at (OMP_CLAUSE_LOCATION (c),
8494 "%qE is not a variable in %qs clause", t,
8495 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8496 remove = true;
8498 else if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
8500 error_at (OMP_CLAUSE_LOCATION (c),
8501 "%qD is threadprivate variable in %qs clause", t,
8502 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8503 remove = true;
8505 else if (!processing_template_decl
8506 && !TYPE_REF_P (TREE_TYPE (t))
8507 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
8508 || (OMP_CLAUSE_MAP_KIND (c)
8509 != GOMP_MAP_FIRSTPRIVATE_POINTER))
8510 && !indir_component_ref_p
8511 && (t != current_class_ptr
8512 || OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
8513 || OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH)
8514 && !cxx_mark_addressable (t))
8515 remove = true;
8516 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8517 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
8518 || (OMP_CLAUSE_MAP_KIND (c)
8519 == GOMP_MAP_FIRSTPRIVATE_POINTER)
8520 || (OMP_CLAUSE_MAP_KIND (c)
8521 == GOMP_MAP_ATTACH_DETACH)))
8522 && t == OMP_CLAUSE_DECL (c)
8523 && !type_dependent_expression_p (t)
8524 && !omp_mappable_type (TYPE_REF_P (TREE_TYPE (t))
8525 ? TREE_TYPE (TREE_TYPE (t))
8526 : TREE_TYPE (t)))
8528 error_at (OMP_CLAUSE_LOCATION (c),
8529 "%qD does not have a mappable type in %qs clause", t,
8530 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8531 if (TREE_TYPE (t) != error_mark_node
8532 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
8533 cxx_incomplete_type_inform (TREE_TYPE (t));
8534 remove = true;
8536 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8537 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FORCE_DEVICEPTR
8538 && !type_dependent_expression_p (t)
8539 && !INDIRECT_TYPE_P (TREE_TYPE (t)))
8541 error_at (OMP_CLAUSE_LOCATION (c),
8542 "%qD is not a pointer variable", t);
8543 remove = true;
8545 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8546 && OMP_CLAUSE_MAP_IMPLICIT (c)
8547 && (bitmap_bit_p (&map_head, DECL_UID (t))
8548 || bitmap_bit_p (&map_field_head, DECL_UID (t))
8549 || bitmap_bit_p (&map_firstprivate_head,
8550 DECL_UID (t))))
8551 remove = true;
8552 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8553 && (OMP_CLAUSE_MAP_KIND (c)
8554 == GOMP_MAP_FIRSTPRIVATE_POINTER))
8556 if (bitmap_bit_p (&generic_head, DECL_UID (t))
8557 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
8558 || bitmap_bit_p (&map_firstprivate_head, DECL_UID (t)))
8560 error_at (OMP_CLAUSE_LOCATION (c),
8561 "%qD appears more than once in data clauses", t);
8562 remove = true;
8564 else if (bitmap_bit_p (&map_head, DECL_UID (t))
8565 && !bitmap_bit_p (&map_field_head, DECL_UID (t))
8566 && openacc)
8568 error_at (OMP_CLAUSE_LOCATION (c),
8569 "%qD appears more than once in data clauses", t);
8570 remove = true;
8572 else
8573 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
8575 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
8576 && (OMP_CLAUSE_MAP_KIND (c)
8577 == GOMP_MAP_FIRSTPRIVATE_REFERENCE))
8578 bitmap_set_bit (&map_firstprivate_head, DECL_UID (t));
8579 else if (bitmap_bit_p (&map_head, DECL_UID (t))
8580 && !bitmap_bit_p (&map_field_head, DECL_UID (t))
8581 && ort != C_ORT_OMP
8582 && ort != C_ORT_OMP_EXIT_DATA)
8584 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
8585 error_at (OMP_CLAUSE_LOCATION (c),
8586 "%qD appears more than once in motion clauses", t);
8587 else if (openacc)
8588 error_at (OMP_CLAUSE_LOCATION (c),
8589 "%qD appears more than once in data clauses", t);
8590 else
8591 error_at (OMP_CLAUSE_LOCATION (c),
8592 "%qD appears more than once in map clauses", t);
8593 remove = true;
8595 else if (openacc && bitmap_bit_p (&generic_head, DECL_UID (t)))
8597 error_at (OMP_CLAUSE_LOCATION (c),
8598 "%qD appears more than once in data clauses", t);
8599 remove = true;
8601 else if (bitmap_bit_p (&firstprivate_head, DECL_UID (t))
8602 || bitmap_bit_p (&is_on_device_head, DECL_UID (t)))
8604 if (openacc)
8605 error_at (OMP_CLAUSE_LOCATION (c),
8606 "%qD appears more than once in data clauses", t);
8607 else
8608 error_at (OMP_CLAUSE_LOCATION (c),
8609 "%qD appears both in data and map clauses", t);
8610 remove = true;
8612 else if (!omp_access_chain_p (addr_tokens, 1))
8614 bitmap_set_bit (&map_head, DECL_UID (t));
8616 tree decl = OMP_CLAUSE_DECL (c);
8617 if (t != decl
8618 && (TREE_CODE (decl) == COMPONENT_REF
8619 || (INDIRECT_REF_P (decl)
8620 && (TREE_CODE (TREE_OPERAND (decl, 0))
8621 == COMPONENT_REF)
8622 && TYPE_REF_P (TREE_TYPE (TREE_OPERAND (decl,
8623 0))))))
8624 bitmap_set_bit (&map_field_head, DECL_UID (t));
8627 skip_decl_checks:
8628 /* If we call ai.expand_map_clause in handle_omp_array_sections,
8629 the containing loop (here) iterates through the new nodes
8630 created by that expansion. Avoid expanding those again (just
8631 by checking the node type). */
8632 if (!remove
8633 && !processing_template_decl
8634 && ort != C_ORT_DECLARE_SIMD
8635 && (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
8636 || (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FIRSTPRIVATE_POINTER
8637 && (OMP_CLAUSE_MAP_KIND (c)
8638 != GOMP_MAP_FIRSTPRIVATE_REFERENCE)
8639 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ALWAYS_POINTER
8640 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH_DETACH
8641 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_ATTACH
8642 && OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_DETACH)))
8644 grp_start_p = pc;
8645 grp_sentinel = OMP_CLAUSE_CHAIN (c);
8646 tree nc = ai.expand_map_clause (c, OMP_CLAUSE_DECL (c),
8647 addr_tokens, ort);
8648 if (nc != error_mark_node)
8649 c = nc;
8652 break;
8654 case OMP_CLAUSE_ENTER:
8655 case OMP_CLAUSE_LINK:
8656 t = OMP_CLAUSE_DECL (c);
8657 const char *cname;
8658 cname = omp_clause_code_name[OMP_CLAUSE_CODE (c)];
8659 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER
8660 && OMP_CLAUSE_ENTER_TO (c))
8661 cname = "to";
8662 if (TREE_CODE (t) == FUNCTION_DECL
8663 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
8665 else if (!VAR_P (t))
8667 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
8669 if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
8670 error_at (OMP_CLAUSE_LOCATION (c),
8671 "template %qE in clause %qs", t, cname);
8672 else if (really_overloaded_fn (t))
8673 error_at (OMP_CLAUSE_LOCATION (c),
8674 "overloaded function name %qE in clause %qs", t,
8675 cname);
8676 else
8677 error_at (OMP_CLAUSE_LOCATION (c),
8678 "%qE is neither a variable nor a function name "
8679 "in clause %qs", t, cname);
8681 else
8682 error_at (OMP_CLAUSE_LOCATION (c),
8683 "%qE is not a variable in clause %qs", t, cname);
8684 remove = true;
8686 else if (DECL_THREAD_LOCAL_P (t))
8688 error_at (OMP_CLAUSE_LOCATION (c),
8689 "%qD is threadprivate variable in %qs clause", t,
8690 cname);
8691 remove = true;
8693 else if (!omp_mappable_type (TREE_TYPE (t)))
8695 error_at (OMP_CLAUSE_LOCATION (c),
8696 "%qD does not have a mappable type in %qs clause", t,
8697 cname);
8698 if (TREE_TYPE (t) != error_mark_node
8699 && !COMPLETE_TYPE_P (TREE_TYPE (t)))
8700 cxx_incomplete_type_inform (TREE_TYPE (t));
8701 remove = true;
8703 if (remove)
8704 break;
8705 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
8707 error_at (OMP_CLAUSE_LOCATION (c),
8708 "%qE appears more than once on the same "
8709 "%<declare target%> directive", t);
8710 remove = true;
8712 else
8713 bitmap_set_bit (&generic_head, DECL_UID (t));
8714 break;
8716 case OMP_CLAUSE_UNIFORM:
8717 t = OMP_CLAUSE_DECL (c);
8718 if (TREE_CODE (t) != PARM_DECL)
8720 if (processing_template_decl)
8721 break;
8722 if (DECL_P (t))
8723 error_at (OMP_CLAUSE_LOCATION (c),
8724 "%qD is not an argument in %<uniform%> clause", t);
8725 else
8726 error_at (OMP_CLAUSE_LOCATION (c),
8727 "%qE is not an argument in %<uniform%> clause", t);
8728 remove = true;
8729 break;
8731 /* map_head bitmap is used as uniform_head if declare_simd. */
8732 bitmap_set_bit (&map_head, DECL_UID (t));
8733 goto check_dup_generic;
8735 case OMP_CLAUSE_GRAINSIZE:
8736 t = OMP_CLAUSE_GRAINSIZE_EXPR (c);
8737 if (t == error_mark_node)
8738 remove = true;
8739 else if (!type_dependent_expression_p (t)
8740 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8742 error_at (OMP_CLAUSE_LOCATION (c),
8743 "%<grainsize%> expression must be integral");
8744 remove = true;
8746 else
8748 t = mark_rvalue_use (t);
8749 if (!processing_template_decl)
8751 t = maybe_constant_value (t);
8752 if (TREE_CODE (t) == INTEGER_CST
8753 && tree_int_cst_sgn (t) != 1)
8755 warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
8756 "%<grainsize%> value must be positive");
8757 t = integer_one_node;
8759 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8761 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
8763 break;
8765 case OMP_CLAUSE_PRIORITY:
8766 t = OMP_CLAUSE_PRIORITY_EXPR (c);
8767 if (t == error_mark_node)
8768 remove = true;
8769 else if (!type_dependent_expression_p (t)
8770 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8772 error_at (OMP_CLAUSE_LOCATION (c),
8773 "%<priority%> expression must be integral");
8774 remove = true;
8776 else
8778 t = mark_rvalue_use (t);
8779 if (!processing_template_decl)
8781 t = maybe_constant_value (t);
8782 if (TREE_CODE (t) == INTEGER_CST
8783 && tree_int_cst_sgn (t) == -1)
8785 warning_at (OMP_CLAUSE_LOCATION (c), OPT_Wopenmp,
8786 "%<priority%> value must be non-negative");
8787 t = integer_one_node;
8789 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8791 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
8793 break;
8795 case OMP_CLAUSE_HINT:
8796 t = OMP_CLAUSE_HINT_EXPR (c);
8797 if (t == error_mark_node)
8798 remove = true;
8799 else if (!type_dependent_expression_p (t)
8800 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8802 error_at (OMP_CLAUSE_LOCATION (c),
8803 "%<hint%> expression must be integral");
8804 remove = true;
8806 else
8808 t = mark_rvalue_use (t);
8809 if (!processing_template_decl)
8811 t = maybe_constant_value (t);
8812 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8813 if (TREE_CODE (t) != INTEGER_CST)
8815 error_at (OMP_CLAUSE_LOCATION (c),
8816 "%<hint%> expression must be constant integer "
8817 "expression");
8818 remove = true;
8821 OMP_CLAUSE_HINT_EXPR (c) = t;
8823 break;
8825 case OMP_CLAUSE_FILTER:
8826 t = OMP_CLAUSE_FILTER_EXPR (c);
8827 if (t == error_mark_node)
8828 remove = true;
8829 else if (!type_dependent_expression_p (t)
8830 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8832 error_at (OMP_CLAUSE_LOCATION (c),
8833 "%<filter%> expression must be integral");
8834 remove = true;
8836 else
8838 t = mark_rvalue_use (t);
8839 if (!processing_template_decl)
8841 t = maybe_constant_value (t);
8842 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8844 OMP_CLAUSE_FILTER_EXPR (c) = t;
8846 break;
8848 case OMP_CLAUSE_IS_DEVICE_PTR:
8849 case OMP_CLAUSE_USE_DEVICE_PTR:
8850 field_ok = (ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP;
8851 t = OMP_CLAUSE_DECL (c);
8852 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IS_DEVICE_PTR)
8853 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
8854 if (!type_dependent_expression_p (t))
8856 tree type = TREE_TYPE (t);
8857 if (!TYPE_PTR_P (type)
8858 && (!TYPE_REF_P (type) || !TYPE_PTR_P (TREE_TYPE (type))))
8860 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_USE_DEVICE_PTR
8861 && ort == C_ORT_OMP)
8863 error_at (OMP_CLAUSE_LOCATION (c),
8864 "%qs variable is neither a pointer "
8865 "nor reference to pointer",
8866 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8867 remove = true;
8869 else if (TREE_CODE (type) != ARRAY_TYPE
8870 && (!TYPE_REF_P (type)
8871 || TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8873 error_at (OMP_CLAUSE_LOCATION (c),
8874 "%qs variable is neither a pointer, nor an "
8875 "array nor reference to pointer or array",
8876 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
8877 remove = true;
8881 goto check_dup_generic;
8883 case OMP_CLAUSE_HAS_DEVICE_ADDR:
8884 t = OMP_CLAUSE_DECL (c);
8885 if (TREE_CODE (t) == OMP_ARRAY_SECTION)
8887 if (handle_omp_array_sections (c, ort))
8888 remove = true;
8889 else
8891 t = OMP_CLAUSE_DECL (c);
8892 while (TREE_CODE (t) == OMP_ARRAY_SECTION)
8893 t = TREE_OPERAND (t, 0);
8894 while (INDIRECT_REF_P (t)
8895 || TREE_CODE (t) == ARRAY_REF)
8896 t = TREE_OPERAND (t, 0);
8899 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
8901 bitmap_set_bit (&is_on_device_head, DECL_UID (t));
8902 if (!processing_template_decl
8903 && !cxx_mark_addressable (t))
8904 remove = true;
8906 goto check_dup_generic_t;
8908 case OMP_CLAUSE_USE_DEVICE_ADDR:
8909 field_ok = true;
8910 t = OMP_CLAUSE_DECL (c);
8911 if (!processing_template_decl
8912 && (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
8913 && !TYPE_REF_P (TREE_TYPE (t))
8914 && !cxx_mark_addressable (t))
8915 remove = true;
8916 goto check_dup_generic;
8918 case OMP_CLAUSE_NOWAIT:
8919 case OMP_CLAUSE_DEFAULT:
8920 case OMP_CLAUSE_UNTIED:
8921 case OMP_CLAUSE_COLLAPSE:
8922 case OMP_CLAUSE_PARALLEL:
8923 case OMP_CLAUSE_FOR:
8924 case OMP_CLAUSE_SECTIONS:
8925 case OMP_CLAUSE_TASKGROUP:
8926 case OMP_CLAUSE_PROC_BIND:
8927 case OMP_CLAUSE_DEVICE_TYPE:
8928 case OMP_CLAUSE_NOGROUP:
8929 case OMP_CLAUSE_THREADS:
8930 case OMP_CLAUSE_SIMD:
8931 case OMP_CLAUSE_DEFAULTMAP:
8932 case OMP_CLAUSE_BIND:
8933 case OMP_CLAUSE_AUTO:
8934 case OMP_CLAUSE_INDEPENDENT:
8935 case OMP_CLAUSE_SEQ:
8936 case OMP_CLAUSE_IF_PRESENT:
8937 case OMP_CLAUSE_FINALIZE:
8938 case OMP_CLAUSE_NOHOST:
8939 case OMP_CLAUSE_INDIRECT:
8940 break;
8942 case OMP_CLAUSE_MERGEABLE:
8943 mergeable_seen = true;
8944 break;
8946 case OMP_CLAUSE_TILE:
8947 for (tree list = OMP_CLAUSE_TILE_LIST (c); !remove && list;
8948 list = TREE_CHAIN (list))
8950 t = TREE_VALUE (list);
8952 if (t == error_mark_node)
8953 remove = true;
8954 else if (!type_dependent_expression_p (t)
8955 && !INTEGRAL_TYPE_P (TREE_TYPE (t)))
8957 error_at (OMP_CLAUSE_LOCATION (c),
8958 "%<tile%> argument needs integral type");
8959 remove = true;
8961 else
8963 t = mark_rvalue_use (t);
8964 if (!processing_template_decl)
8966 /* Zero is used to indicate '*', we permit you
8967 to get there via an ICE of value zero. */
8968 t = maybe_constant_value (t);
8969 if (!tree_fits_shwi_p (t)
8970 || tree_to_shwi (t) < 0)
8972 error_at (OMP_CLAUSE_LOCATION (c),
8973 "%<tile%> argument needs positive "
8974 "integral constant");
8975 remove = true;
8977 t = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
8981 /* Update list item. */
8982 TREE_VALUE (list) = t;
8984 break;
8986 case OMP_CLAUSE_ORDERED:
8987 ordered_seen = true;
8988 break;
8990 case OMP_CLAUSE_ORDER:
8991 if (order_seen)
8992 remove = true;
8993 else
8994 order_seen = true;
8995 break;
8997 case OMP_CLAUSE_INBRANCH:
8998 case OMP_CLAUSE_NOTINBRANCH:
8999 if (branch_seen)
9001 error_at (OMP_CLAUSE_LOCATION (c),
9002 "%<inbranch%> clause is incompatible with "
9003 "%<notinbranch%>");
9004 remove = true;
9006 branch_seen = true;
9007 break;
9009 case OMP_CLAUSE_INCLUSIVE:
9010 case OMP_CLAUSE_EXCLUSIVE:
9011 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
9012 if (!t)
9013 t = OMP_CLAUSE_DECL (c);
9014 if (t == current_class_ptr)
9016 error_at (OMP_CLAUSE_LOCATION (c),
9017 "%<this%> allowed in OpenMP only in %<declare simd%>"
9018 " clauses");
9019 remove = true;
9020 break;
9022 if (!VAR_P (t)
9023 && TREE_CODE (t) != PARM_DECL
9024 && TREE_CODE (t) != FIELD_DECL)
9026 if (processing_template_decl && TREE_CODE (t) != OVERLOAD)
9027 break;
9028 if (DECL_P (t))
9029 error_at (OMP_CLAUSE_LOCATION (c),
9030 "%qD is not a variable in clause %qs", t,
9031 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
9032 else
9033 error_at (OMP_CLAUSE_LOCATION (c),
9034 "%qE is not a variable in clause %qs", t,
9035 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
9036 remove = true;
9038 break;
9040 default:
9041 gcc_unreachable ();
9044 if (remove)
9046 if (grp_start_p)
9048 /* If we found a clause to remove, we want to remove the whole
9049 expanded group, otherwise gimplify
9050 (omp_resolve_clause_dependencies) can get confused. */
9051 *grp_start_p = grp_sentinel;
9052 pc = grp_start_p;
9053 grp_start_p = NULL;
9055 else
9056 *pc = OMP_CLAUSE_CHAIN (c);
9058 else
9059 pc = &OMP_CLAUSE_CHAIN (c);
9062 if (reduction_seen < 0 && (ordered_seen || schedule_seen))
9063 reduction_seen = -2;
9065 for (pc = &clauses, c = clauses; c ; c = *pc)
9067 enum omp_clause_code c_kind = OMP_CLAUSE_CODE (c);
9068 bool remove = false;
9069 bool need_complete_type = false;
9070 bool need_default_ctor = false;
9071 bool need_copy_ctor = false;
9072 bool need_copy_assignment = false;
9073 bool need_implicitly_determined = false;
9074 bool need_dtor = false;
9075 tree type, inner_type;
9077 switch (c_kind)
9079 case OMP_CLAUSE_SHARED:
9080 need_implicitly_determined = true;
9081 break;
9082 case OMP_CLAUSE_PRIVATE:
9083 need_complete_type = true;
9084 need_default_ctor = true;
9085 need_dtor = true;
9086 need_implicitly_determined = true;
9087 break;
9088 case OMP_CLAUSE_FIRSTPRIVATE:
9089 need_complete_type = true;
9090 need_copy_ctor = true;
9091 need_dtor = true;
9092 need_implicitly_determined = true;
9093 break;
9094 case OMP_CLAUSE_LASTPRIVATE:
9095 need_complete_type = true;
9096 need_copy_assignment = true;
9097 need_implicitly_determined = true;
9098 break;
9099 case OMP_CLAUSE_REDUCTION:
9100 if (reduction_seen == -2)
9101 OMP_CLAUSE_REDUCTION_INSCAN (c) = 0;
9102 if (OMP_CLAUSE_REDUCTION_INSCAN (c))
9103 need_copy_assignment = true;
9104 need_implicitly_determined = true;
9105 break;
9106 case OMP_CLAUSE_IN_REDUCTION:
9107 case OMP_CLAUSE_TASK_REDUCTION:
9108 case OMP_CLAUSE_INCLUSIVE:
9109 case OMP_CLAUSE_EXCLUSIVE:
9110 need_implicitly_determined = true;
9111 break;
9112 case OMP_CLAUSE_LINEAR:
9113 if (ort != C_ORT_OMP_DECLARE_SIMD)
9114 need_implicitly_determined = true;
9115 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
9116 && !bitmap_bit_p (&map_head,
9117 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
9119 error_at (OMP_CLAUSE_LOCATION (c),
9120 "%<linear%> clause step is a parameter %qD not "
9121 "specified in %<uniform%> clause",
9122 OMP_CLAUSE_LINEAR_STEP (c));
9123 *pc = OMP_CLAUSE_CHAIN (c);
9124 continue;
9126 break;
9127 case OMP_CLAUSE_COPYPRIVATE:
9128 need_copy_assignment = true;
9129 break;
9130 case OMP_CLAUSE_COPYIN:
9131 need_copy_assignment = true;
9132 break;
9133 case OMP_CLAUSE_SIMDLEN:
9134 if (safelen
9135 && !processing_template_decl
9136 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
9137 OMP_CLAUSE_SIMDLEN_EXPR (c)))
9139 error_at (OMP_CLAUSE_LOCATION (c),
9140 "%<simdlen%> clause value is bigger than "
9141 "%<safelen%> clause value");
9142 OMP_CLAUSE_SIMDLEN_EXPR (c)
9143 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
9145 pc = &OMP_CLAUSE_CHAIN (c);
9146 continue;
9147 case OMP_CLAUSE_SCHEDULE:
9148 if (ordered_seen
9149 && (OMP_CLAUSE_SCHEDULE_KIND (c)
9150 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
9152 error_at (OMP_CLAUSE_LOCATION (c),
9153 "%<nonmonotonic%> schedule modifier specified "
9154 "together with %<ordered%> clause");
9155 OMP_CLAUSE_SCHEDULE_KIND (c)
9156 = (enum omp_clause_schedule_kind)
9157 (OMP_CLAUSE_SCHEDULE_KIND (c)
9158 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
9160 if (reduction_seen == -2)
9161 error_at (OMP_CLAUSE_LOCATION (c),
9162 "%qs clause specified together with %<inscan%> "
9163 "%<reduction%> clause", "schedule");
9164 pc = &OMP_CLAUSE_CHAIN (c);
9165 continue;
9166 case OMP_CLAUSE_NOGROUP:
9167 if (reduction_seen)
9169 error_at (OMP_CLAUSE_LOCATION (c),
9170 "%<nogroup%> clause must not be used together with "
9171 "%<reduction%> clause");
9172 *pc = OMP_CLAUSE_CHAIN (c);
9173 continue;
9175 pc = &OMP_CLAUSE_CHAIN (c);
9176 continue;
9177 case OMP_CLAUSE_ORDERED:
9178 if (reduction_seen == -2)
9179 error_at (OMP_CLAUSE_LOCATION (c),
9180 "%qs clause specified together with %<inscan%> "
9181 "%<reduction%> clause", "ordered");
9182 pc = &OMP_CLAUSE_CHAIN (c);
9183 continue;
9184 case OMP_CLAUSE_ORDER:
9185 if (ordered_seen)
9187 error_at (OMP_CLAUSE_LOCATION (c),
9188 "%<order%> clause must not be used together "
9189 "with %<ordered%>");
9190 *pc = OMP_CLAUSE_CHAIN (c);
9191 continue;
9193 pc = &OMP_CLAUSE_CHAIN (c);
9194 continue;
9195 case OMP_CLAUSE_DETACH:
9196 if (mergeable_seen)
9198 error_at (OMP_CLAUSE_LOCATION (c),
9199 "%<detach%> clause must not be used together with "
9200 "%<mergeable%> clause");
9201 *pc = OMP_CLAUSE_CHAIN (c);
9202 continue;
9204 pc = &OMP_CLAUSE_CHAIN (c);
9205 continue;
9206 case OMP_CLAUSE_MAP:
9207 if (target_in_reduction_seen && !processing_template_decl)
9209 t = OMP_CLAUSE_DECL (c);
9210 while (handled_component_p (t)
9211 || INDIRECT_REF_P (t)
9212 || TREE_CODE (t) == ADDR_EXPR
9213 || TREE_CODE (t) == MEM_REF
9214 || TREE_CODE (t) == NON_LVALUE_EXPR)
9215 t = TREE_OPERAND (t, 0);
9216 if (DECL_P (t)
9217 && bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
9218 OMP_CLAUSE_MAP_IN_REDUCTION (c) = 1;
9220 pc = &OMP_CLAUSE_CHAIN (c);
9221 continue;
9222 case OMP_CLAUSE_NOWAIT:
9223 if (copyprivate_seen)
9225 error_at (OMP_CLAUSE_LOCATION (c),
9226 "%<nowait%> clause must not be used together "
9227 "with %<copyprivate%>");
9228 *pc = OMP_CLAUSE_CHAIN (c);
9229 continue;
9231 /* FALLTHRU */
9232 default:
9233 pc = &OMP_CLAUSE_CHAIN (c);
9234 continue;
9237 t = OMP_CLAUSE_DECL (c);
9238 switch (c_kind)
9240 case OMP_CLAUSE_LASTPRIVATE:
9241 if (DECL_P (t)
9242 && !bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
9244 need_default_ctor = true;
9245 need_dtor = true;
9247 break;
9249 case OMP_CLAUSE_REDUCTION:
9250 case OMP_CLAUSE_IN_REDUCTION:
9251 case OMP_CLAUSE_TASK_REDUCTION:
9252 if (allocate_seen)
9254 if (TREE_CODE (t) == MEM_REF)
9256 t = TREE_OPERAND (t, 0);
9257 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
9258 t = TREE_OPERAND (t, 0);
9259 if (TREE_CODE (t) == ADDR_EXPR
9260 || INDIRECT_REF_P (t))
9261 t = TREE_OPERAND (t, 0);
9262 if (DECL_P (t))
9263 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9265 else if (TREE_CODE (t) == OMP_ARRAY_SECTION)
9267 while (TREE_CODE (t) == OMP_ARRAY_SECTION)
9268 t = TREE_OPERAND (t, 0);
9269 if (DECL_P (t))
9270 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9271 t = OMP_CLAUSE_DECL (c);
9273 else if (DECL_P (t))
9274 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9275 t = OMP_CLAUSE_DECL (c);
9277 if (processing_template_decl
9278 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
9279 break;
9280 if (finish_omp_reduction_clause (c, &need_default_ctor,
9281 &need_dtor))
9282 remove = true;
9283 else
9284 t = OMP_CLAUSE_DECL (c);
9285 break;
9287 case OMP_CLAUSE_COPYIN:
9288 if (processing_template_decl
9289 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
9290 break;
9291 if (!VAR_P (t) || !CP_DECL_THREAD_LOCAL_P (t))
9293 error_at (OMP_CLAUSE_LOCATION (c),
9294 "%qE must be %<threadprivate%> for %<copyin%>", t);
9295 remove = true;
9297 break;
9299 default:
9300 break;
9303 if (processing_template_decl
9304 && !VAR_P (t) && TREE_CODE (t) != PARM_DECL)
9306 pc = &OMP_CLAUSE_CHAIN (c);
9307 continue;
9310 if (need_complete_type || need_copy_assignment)
9312 t = require_complete_type (t);
9313 if (t == error_mark_node)
9314 remove = true;
9315 else if (!processing_template_decl
9316 && TYPE_REF_P (TREE_TYPE (t))
9317 && !complete_type_or_else (TREE_TYPE (TREE_TYPE (t)), t))
9318 remove = true;
9320 if (need_implicitly_determined)
9322 const char *share_name = NULL;
9324 if (allocate_seen
9325 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
9326 && DECL_P (t))
9327 bitmap_clear_bit (&aligned_head, DECL_UID (t));
9329 if (VAR_P (t) && CP_DECL_THREAD_LOCAL_P (t))
9330 share_name = "threadprivate";
9331 else switch (cxx_omp_predetermined_sharing_1 (t))
9333 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
9334 break;
9335 case OMP_CLAUSE_DEFAULT_SHARED:
9336 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9337 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE)
9338 && c_omp_predefined_variable (t))
9339 /* The __func__ variable and similar function-local predefined
9340 variables may be listed in a shared or firstprivate
9341 clause. */
9342 break;
9343 if (VAR_P (t)
9344 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
9345 && TREE_STATIC (t)
9346 && cxx_omp_const_qual_no_mutable (t))
9348 tree ctx = CP_DECL_CONTEXT (t);
9349 /* const qualified static data members without mutable
9350 member may be specified in firstprivate clause. */
9351 if (TYPE_P (ctx) && MAYBE_CLASS_TYPE_P (ctx))
9352 break;
9354 share_name = "shared";
9355 break;
9356 case OMP_CLAUSE_DEFAULT_PRIVATE:
9357 share_name = "private";
9358 break;
9359 default:
9360 gcc_unreachable ();
9362 if (share_name)
9364 error_at (OMP_CLAUSE_LOCATION (c),
9365 "%qE is predetermined %qs for %qs",
9366 omp_clause_printable_decl (t), share_name,
9367 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
9368 remove = true;
9370 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_SHARED
9371 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_FIRSTPRIVATE
9372 && cxx_omp_const_qual_no_mutable (t))
9374 error_at (OMP_CLAUSE_LOCATION (c),
9375 "%<const%> qualified %qE without %<mutable%> member "
9376 "may appear only in %<shared%> or %<firstprivate%> "
9377 "clauses", omp_clause_printable_decl (t));
9378 remove = true;
9382 if (detach_seen
9383 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_SHARED
9384 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
9385 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
9386 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
9387 && OMP_CLAUSE_DECL (c) == OMP_CLAUSE_DECL (detach_seen))
9389 error_at (OMP_CLAUSE_LOCATION (c),
9390 "the event handle of a %<detach%> clause "
9391 "should not be in a data-sharing clause");
9392 remove = true;
9395 /* We're interested in the base element, not arrays. */
9396 inner_type = type = TREE_TYPE (t);
9397 if ((need_complete_type
9398 || need_copy_assignment
9399 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
9400 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION
9401 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TASK_REDUCTION)
9402 && TYPE_REF_P (inner_type))
9403 inner_type = TREE_TYPE (inner_type);
9404 while (TREE_CODE (inner_type) == ARRAY_TYPE)
9405 inner_type = TREE_TYPE (inner_type);
9407 /* Check for special function availability by building a call to one.
9408 Save the results, because later we won't be in the right context
9409 for making these queries. */
9410 if (CLASS_TYPE_P (inner_type)
9411 && COMPLETE_TYPE_P (inner_type)
9412 && (need_default_ctor || need_copy_ctor
9413 || need_copy_assignment || need_dtor)
9414 && !type_dependent_expression_p (t)
9415 && cxx_omp_create_clause_info (c, inner_type, need_default_ctor,
9416 need_copy_ctor, need_copy_assignment,
9417 need_dtor))
9418 remove = true;
9420 if (!remove
9421 && c_kind == OMP_CLAUSE_SHARED
9422 && processing_template_decl)
9424 t = omp_clause_decl_field (OMP_CLAUSE_DECL (c));
9425 if (t)
9426 OMP_CLAUSE_DECL (c) = t;
9429 if (remove)
9430 *pc = OMP_CLAUSE_CHAIN (c);
9431 else
9432 pc = &OMP_CLAUSE_CHAIN (c);
9435 if (allocate_seen)
9436 for (pc = &clauses, c = clauses; c ; c = *pc)
9438 bool remove = false;
9439 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ALLOCATE
9440 && !OMP_CLAUSE_ALLOCATE_COMBINED (c)
9441 && DECL_P (OMP_CLAUSE_DECL (c))
9442 && bitmap_bit_p (&aligned_head, DECL_UID (OMP_CLAUSE_DECL (c))))
9444 error_at (OMP_CLAUSE_LOCATION (c),
9445 "%qD specified in %<allocate%> clause but not in "
9446 "an explicit privatization clause", OMP_CLAUSE_DECL (c));
9447 remove = true;
9449 if (remove)
9450 *pc = OMP_CLAUSE_CHAIN (c);
9451 else
9452 pc = &OMP_CLAUSE_CHAIN (c);
9455 bitmap_obstack_release (NULL);
9456 return clauses;
9459 /* Start processing OpenMP clauses that can include any
9460 privatization clauses for non-static data members. */
9462 tree
9463 push_omp_privatization_clauses (bool ignore_next)
9465 if (omp_private_member_ignore_next)
9467 omp_private_member_ignore_next = ignore_next;
9468 return NULL_TREE;
9470 omp_private_member_ignore_next = ignore_next;
9471 if (omp_private_member_map)
9472 omp_private_member_vec.safe_push (error_mark_node);
9473 return push_stmt_list ();
9476 /* Revert remapping of any non-static data members since
9477 the last push_omp_privatization_clauses () call. */
9479 void
9480 pop_omp_privatization_clauses (tree stmt)
9482 if (stmt == NULL_TREE)
9483 return;
9484 stmt = pop_stmt_list (stmt);
9485 if (omp_private_member_map)
9487 while (!omp_private_member_vec.is_empty ())
9489 tree t = omp_private_member_vec.pop ();
9490 if (t == error_mark_node)
9492 add_stmt (stmt);
9493 return;
9495 bool no_decl_expr = t == integer_zero_node;
9496 if (no_decl_expr)
9497 t = omp_private_member_vec.pop ();
9498 tree *v = omp_private_member_map->get (t);
9499 gcc_assert (v);
9500 if (!no_decl_expr)
9501 add_decl_expr (*v);
9502 omp_private_member_map->remove (t);
9504 delete omp_private_member_map;
9505 omp_private_member_map = NULL;
9507 add_stmt (stmt);
9510 /* Remember OpenMP privatization clauses mapping and clear it.
9511 Used for lambdas. */
9513 void
9514 save_omp_privatization_clauses (vec<tree> &save)
9516 save = vNULL;
9517 if (omp_private_member_ignore_next)
9518 save.safe_push (integer_one_node);
9519 omp_private_member_ignore_next = false;
9520 if (!omp_private_member_map)
9521 return;
9523 while (!omp_private_member_vec.is_empty ())
9525 tree t = omp_private_member_vec.pop ();
9526 if (t == error_mark_node)
9528 save.safe_push (t);
9529 continue;
9531 tree n = t;
9532 if (t == integer_zero_node)
9533 t = omp_private_member_vec.pop ();
9534 tree *v = omp_private_member_map->get (t);
9535 gcc_assert (v);
9536 save.safe_push (*v);
9537 save.safe_push (t);
9538 if (n != t)
9539 save.safe_push (n);
9541 delete omp_private_member_map;
9542 omp_private_member_map = NULL;
9545 /* Restore OpenMP privatization clauses mapping saved by the
9546 above function. */
9548 void
9549 restore_omp_privatization_clauses (vec<tree> &save)
9551 gcc_assert (omp_private_member_vec.is_empty ());
9552 omp_private_member_ignore_next = false;
9553 if (save.is_empty ())
9554 return;
9555 if (save.length () == 1 && save[0] == integer_one_node)
9557 omp_private_member_ignore_next = true;
9558 save.release ();
9559 return;
9562 omp_private_member_map = new hash_map <tree, tree>;
9563 while (!save.is_empty ())
9565 tree t = save.pop ();
9566 tree n = t;
9567 if (t != error_mark_node)
9569 if (t == integer_one_node)
9571 omp_private_member_ignore_next = true;
9572 gcc_assert (save.is_empty ());
9573 break;
9575 if (t == integer_zero_node)
9576 t = save.pop ();
9577 tree &v = omp_private_member_map->get_or_insert (t);
9578 v = save.pop ();
9580 omp_private_member_vec.safe_push (t);
9581 if (n != t)
9582 omp_private_member_vec.safe_push (n);
9584 save.release ();
9587 /* For all variables in the tree_list VARS, mark them as thread local. */
9589 void
9590 finish_omp_threadprivate (tree vars)
9592 tree t;
9594 /* Mark every variable in VARS to be assigned thread local storage. */
9595 for (t = vars; t; t = TREE_CHAIN (t))
9597 tree v = TREE_PURPOSE (t);
9599 if (error_operand_p (v))
9601 else if (!VAR_P (v))
9602 error ("%<threadprivate%> %qD is not file, namespace "
9603 "or block scope variable", v);
9604 /* If V had already been marked threadprivate, it doesn't matter
9605 whether it had been used prior to this point. */
9606 else if (TREE_USED (v)
9607 && (DECL_LANG_SPECIFIC (v) == NULL
9608 || !CP_DECL_THREADPRIVATE_P (v)))
9609 error ("%qE declared %<threadprivate%> after first use", v);
9610 else if (! TREE_STATIC (v) && ! DECL_EXTERNAL (v))
9611 error ("automatic variable %qE cannot be %<threadprivate%>", v);
9612 else if (! COMPLETE_TYPE_P (complete_type (TREE_TYPE (v))))
9613 error ("%<threadprivate%> %qE has incomplete type", v);
9614 else if (TREE_STATIC (v) && TYPE_P (CP_DECL_CONTEXT (v))
9615 && CP_DECL_CONTEXT (v) != current_class_type)
9616 error ("%<threadprivate%> %qE directive not "
9617 "in %qT definition", v, CP_DECL_CONTEXT (v));
9618 else
9620 /* Allocate a LANG_SPECIFIC structure for V, if needed. */
9621 if (DECL_LANG_SPECIFIC (v) == NULL)
9622 retrofit_lang_decl (v);
9624 if (! CP_DECL_THREAD_LOCAL_P (v))
9626 CP_DECL_THREAD_LOCAL_P (v) = true;
9627 set_decl_tls_model (v, decl_default_tls_model (v));
9628 /* If rtl has been already set for this var, call
9629 make_decl_rtl once again, so that encode_section_info
9630 has a chance to look at the new decl flags. */
9631 if (DECL_RTL_SET_P (v))
9632 make_decl_rtl (v);
9634 CP_DECL_THREADPRIVATE_P (v) = 1;
9639 /* Build an OpenMP structured block. */
9641 tree
9642 begin_omp_structured_block (void)
9644 return do_pushlevel (sk_omp);
9647 tree
9648 finish_omp_structured_block (tree block)
9650 return do_poplevel (block);
9653 /* Similarly, except force the retention of the BLOCK. */
9655 tree
9656 begin_omp_parallel (void)
9658 keep_next_level (true);
9659 return begin_omp_structured_block ();
9662 /* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
9663 statement. */
9665 tree
9666 finish_oacc_data (tree clauses, tree block)
9668 tree stmt;
9670 block = finish_omp_structured_block (block);
9672 stmt = make_node (OACC_DATA);
9673 TREE_TYPE (stmt) = void_type_node;
9674 OACC_DATA_CLAUSES (stmt) = clauses;
9675 OACC_DATA_BODY (stmt) = block;
9677 return add_stmt (stmt);
9680 /* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
9681 statement. */
9683 tree
9684 finish_oacc_host_data (tree clauses, tree block)
9686 tree stmt;
9688 block = finish_omp_structured_block (block);
9690 stmt = make_node (OACC_HOST_DATA);
9691 TREE_TYPE (stmt) = void_type_node;
9692 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
9693 OACC_HOST_DATA_BODY (stmt) = block;
9695 return add_stmt (stmt);
9698 /* Generate OMP construct CODE, with BODY and CLAUSES as its compound
9699 statement. */
9701 tree
9702 finish_omp_construct (enum tree_code code, tree body, tree clauses)
9704 body = finish_omp_structured_block (body);
9706 tree stmt = make_node (code);
9707 TREE_TYPE (stmt) = void_type_node;
9708 OMP_BODY (stmt) = body;
9709 OMP_CLAUSES (stmt) = clauses;
9711 return add_stmt (stmt);
9714 /* Used to walk OpenMP target directive body. */
9716 struct omp_target_walk_data
9718 /* Holds the 'this' expression found in current function. */
9719 tree current_object;
9721 /* True if the 'this' expression was accessed in the target body. */
9722 bool this_expr_accessed;
9724 /* For non-static functions, record which pointer-typed members were
9725 accessed, and the whole expression. */
9726 hash_map<tree, tree> ptr_members_accessed;
9728 /* Record which lambda objects were accessed in target body. */
9729 hash_set<tree> lambda_objects_accessed;
9731 /* For lambda functions, the __closure object expression of the current
9732 function, and the set of captured variables accessed in target body. */
9733 tree current_closure;
9734 hash_set<tree> closure_vars_accessed;
9736 /* Local variables declared inside a BIND_EXPR, used to filter out such
9737 variables when recording lambda_objects_accessed. */
9738 hash_set<tree> local_decls;
9741 /* Helper function of finish_omp_target_clauses, called via
9742 cp_walk_tree_without_duplicates. Traverse body of OpenMP target
9743 directive *TP, and fill out omp_target_walk_data passed in *PTR. */
9745 static tree
9746 finish_omp_target_clauses_r (tree *tp, int *walk_subtrees, void *ptr)
9748 tree t = *tp;
9749 struct omp_target_walk_data *data = (struct omp_target_walk_data *) ptr;
9750 tree current_object = data->current_object;
9751 tree current_closure = data->current_closure;
9753 /* References inside of these expression codes shouldn't incur any
9754 form of mapping, so return early. */
9755 if (TREE_CODE (t) == SIZEOF_EXPR
9756 || TREE_CODE (t) == ALIGNOF_EXPR)
9758 *walk_subtrees = 0;
9759 return NULL_TREE;
9762 if (TREE_CODE (t) == OMP_CLAUSE)
9763 return NULL_TREE;
9765 if (current_object)
9767 tree this_expr = TREE_OPERAND (current_object, 0);
9769 if (operand_equal_p (t, this_expr))
9771 data->this_expr_accessed = true;
9772 *walk_subtrees = 0;
9773 return NULL_TREE;
9776 if (TREE_CODE (t) == COMPONENT_REF
9777 && POINTER_TYPE_P (TREE_TYPE (t))
9778 && operand_equal_p (TREE_OPERAND (t, 0), current_object)
9779 && TREE_CODE (TREE_OPERAND (t, 1)) == FIELD_DECL)
9781 data->this_expr_accessed = true;
9782 tree fld = TREE_OPERAND (t, 1);
9783 if (data->ptr_members_accessed.get (fld) == NULL)
9785 if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
9786 t = convert_from_reference (t);
9787 data->ptr_members_accessed.put (fld, t);
9789 *walk_subtrees = 0;
9790 return NULL_TREE;
9794 /* When the current_function_decl is a lambda function, the closure object
9795 argument's type seems to not yet have fields layed out, so a recording
9796 of DECL_VALUE_EXPRs during the target body walk seems the only way to
9797 find them. */
9798 if (current_closure
9799 && (VAR_P (t)
9800 || TREE_CODE (t) == PARM_DECL
9801 || TREE_CODE (t) == RESULT_DECL)
9802 && DECL_HAS_VALUE_EXPR_P (t)
9803 && TREE_CODE (DECL_VALUE_EXPR (t)) == COMPONENT_REF
9804 && operand_equal_p (current_closure,
9805 TREE_OPERAND (DECL_VALUE_EXPR (t), 0)))
9807 if (!data->closure_vars_accessed.contains (t))
9808 data->closure_vars_accessed.add (t);
9809 *walk_subtrees = 0;
9810 return NULL_TREE;
9813 if (TREE_CODE (t) == BIND_EXPR)
9815 tree block = BIND_EXPR_BLOCK (t);
9816 for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
9817 if (!data->local_decls.contains (var))
9818 data->local_decls.add (var);
9819 return NULL_TREE;
9822 if (TREE_TYPE (t) && LAMBDA_TYPE_P (TREE_TYPE (t)))
9824 tree lt = TREE_TYPE (t);
9825 gcc_assert (CLASS_TYPE_P (lt));
9827 if (!data->lambda_objects_accessed.contains (t)
9828 /* Do not prepare to create target maps for locally declared
9829 lambdas or anonymous ones. */
9830 && !data->local_decls.contains (t)
9831 && TREE_CODE (t) != TARGET_EXPR)
9832 data->lambda_objects_accessed.add (t);
9833 *walk_subtrees = 0;
9834 return NULL_TREE;
9837 return NULL_TREE;
9840 /* Helper function for finish_omp_target, and also from tsubst_expr.
9841 Create additional clauses for mapping of non-static members, lambda objects,
9842 etc. */
9844 void
9845 finish_omp_target_clauses (location_t loc, tree body, tree *clauses_ptr)
9847 omp_target_walk_data data;
9848 data.this_expr_accessed = false;
9849 data.current_object = NULL_TREE;
9851 if (DECL_NONSTATIC_MEMBER_P (current_function_decl) && current_class_ptr)
9852 if (tree ct = current_nonlambda_class_type ())
9854 tree object = maybe_dummy_object (ct, NULL);
9855 object = maybe_resolve_dummy (object, true);
9856 data.current_object = object;
9859 if (DECL_LAMBDA_FUNCTION_P (current_function_decl))
9861 tree closure = DECL_ARGUMENTS (current_function_decl);
9862 data.current_closure = build_indirect_ref (loc, closure, RO_UNARY_STAR);
9864 else
9865 data.current_closure = NULL_TREE;
9867 cp_walk_tree_without_duplicates (&body, finish_omp_target_clauses_r, &data);
9869 auto_vec<tree, 16> new_clauses;
9871 tree omp_target_this_expr = NULL_TREE;
9872 tree *explicit_this_deref_map = NULL;
9873 if (data.this_expr_accessed)
9875 omp_target_this_expr = TREE_OPERAND (data.current_object, 0);
9877 /* See if explicit user-specified map(this[:]) clause already exists.
9878 If not, we create an implicit map(tofrom:this[:1]) clause. */
9879 for (tree *cp = clauses_ptr; *cp; cp = &OMP_CLAUSE_CHAIN (*cp))
9880 if (OMP_CLAUSE_CODE (*cp) == OMP_CLAUSE_MAP
9881 && (TREE_CODE (OMP_CLAUSE_DECL (*cp)) == INDIRECT_REF
9882 || TREE_CODE (OMP_CLAUSE_DECL (*cp)) == MEM_REF)
9883 && operand_equal_p (TREE_OPERAND (OMP_CLAUSE_DECL (*cp), 0),
9884 omp_target_this_expr))
9886 explicit_this_deref_map = cp;
9887 break;
9891 if (DECL_LAMBDA_FUNCTION_P (current_function_decl)
9892 && (data.this_expr_accessed
9893 || !data.closure_vars_accessed.is_empty ()))
9895 /* For lambda functions, we need to first create a copy of the
9896 __closure object. */
9897 tree closure = DECL_ARGUMENTS (current_function_decl);
9898 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9899 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TO);
9900 OMP_CLAUSE_DECL (c)
9901 = build_indirect_ref (loc, closure, RO_UNARY_STAR);
9902 OMP_CLAUSE_SIZE (c)
9903 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (closure)));
9904 new_clauses.safe_push (c);
9906 tree closure_obj = OMP_CLAUSE_DECL (c);
9907 tree closure_type = TREE_TYPE (closure_obj);
9909 gcc_assert (LAMBDA_TYPE_P (closure_type)
9910 && CLASS_TYPE_P (closure_type));
9912 tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
9913 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
9914 OMP_CLAUSE_DECL (c2) = closure;
9915 OMP_CLAUSE_SIZE (c2) = size_zero_node;
9916 new_clauses.safe_push (c2);
9919 if (data.this_expr_accessed)
9921 /* If the this-expr was accessed, create a map(*this) clause. */
9922 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
9923 if (explicit_this_deref_map)
9925 tree this_map = *explicit_this_deref_map;
9926 tree nc = OMP_CLAUSE_CHAIN (this_map);
9927 gcc_assert (nc != NULL_TREE
9928 && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
9929 && (OMP_CLAUSE_MAP_KIND (nc)
9930 == GOMP_MAP_FIRSTPRIVATE_POINTER));
9931 kind = OMP_CLAUSE_MAP_KIND (this_map);
9932 /* Remove the original 'map(*this) map(firstprivate_ptr:this)'
9933 two-map sequence away from the chain. */
9934 *explicit_this_deref_map = OMP_CLAUSE_CHAIN (nc);
9936 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9937 OMP_CLAUSE_SET_MAP_KIND (c, kind);
9938 OMP_CLAUSE_DECL (c)
9939 = build_indirect_ref (loc, omp_target_this_expr, RO_UNARY_STAR);
9940 OMP_CLAUSE_SIZE (c)
9941 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (omp_target_this_expr)));
9942 new_clauses.safe_push (c);
9944 /* If we're in a lambda function, the this-pointer will actually be
9945 '__closure->this', a mapped member of __closure, hence always_pointer.
9946 Otherwise it's a firstprivate pointer. */
9947 enum gomp_map_kind ptr_kind
9948 = (DECL_LAMBDA_FUNCTION_P (current_function_decl)
9949 ? GOMP_MAP_ALWAYS_POINTER
9950 : GOMP_MAP_FIRSTPRIVATE_POINTER);
9951 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
9952 OMP_CLAUSE_SET_MAP_KIND (c, ptr_kind);
9953 OMP_CLAUSE_DECL (c) = omp_target_this_expr;
9954 OMP_CLAUSE_SIZE (c) = size_zero_node;
9955 new_clauses.safe_push (c);
9958 if (DECL_LAMBDA_FUNCTION_P (current_function_decl))
9960 if (omp_target_this_expr)
9962 STRIP_NOPS (omp_target_this_expr);
9963 gcc_assert (DECL_HAS_VALUE_EXPR_P (omp_target_this_expr));
9964 omp_target_this_expr = DECL_VALUE_EXPR (omp_target_this_expr);
9967 for (hash_set<tree>::iterator i = data.closure_vars_accessed.begin ();
9968 i != data.closure_vars_accessed.end (); ++i)
9970 tree orig_decl = *i;
9971 tree closure_expr = DECL_VALUE_EXPR (orig_decl);
9973 if (TREE_CODE (TREE_TYPE (orig_decl)) == POINTER_TYPE
9974 || TREE_CODE (TREE_TYPE (orig_decl)) == REFERENCE_TYPE)
9976 /* this-pointer is processed above, outside this loop. */
9977 if (omp_target_this_expr
9978 && operand_equal_p (closure_expr, omp_target_this_expr))
9979 continue;
9981 bool ptr_p = TREE_CODE (TREE_TYPE (orig_decl)) == POINTER_TYPE;
9982 enum gomp_map_kind kind, ptr_kind, nc_kind;
9983 tree size;
9985 if (ptr_p)
9987 /* For pointers, default mapped as zero-length array
9988 section. */
9989 kind = GOMP_MAP_ALLOC;
9990 nc_kind = GOMP_MAP_FIRSTPRIVATE_POINTER;
9991 ptr_kind = GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION;
9992 size = size_zero_node;
9994 else
9996 /* For references, default mapped as appearing on map
9997 clause. */
9998 kind = GOMP_MAP_TOFROM;
9999 nc_kind = GOMP_MAP_FIRSTPRIVATE_REFERENCE;
10000 ptr_kind = GOMP_MAP_ALWAYS_POINTER;
10001 size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (closure_expr)));
10004 for (tree *p = clauses_ptr; *p; p = &OMP_CLAUSE_CHAIN (*p))
10005 if (OMP_CLAUSE_CODE (*p) == OMP_CLAUSE_MAP
10006 && (TREE_CODE (OMP_CLAUSE_DECL (*p)) == INDIRECT_REF
10007 || TREE_CODE (OMP_CLAUSE_DECL (*p)) == MEM_REF)
10008 && operand_equal_p (TREE_OPERAND (OMP_CLAUSE_DECL (*p), 0),
10009 orig_decl))
10011 /* If this was already specified by user as a map,
10012 save the user specified map kind, delete the
10013 "map(*ptr/ref), map(firstprivate ptr/ref)" sequence,
10014 and insert our own sequence:
10015 "map(*__closure->ptr/ref), map(<ptr_kind>:__closure->ref"
10017 tree nc = OMP_CLAUSE_CHAIN (*p);
10018 gcc_assert (nc != NULL_TREE
10019 && OMP_CLAUSE_CODE (nc) == OMP_CLAUSE_MAP
10020 && OMP_CLAUSE_MAP_KIND (nc) == nc_kind);
10021 /* Update with user specified kind and size. */
10022 kind = OMP_CLAUSE_MAP_KIND (*p);
10023 size = OMP_CLAUSE_SIZE (*p);
10024 *p = OMP_CLAUSE_CHAIN (nc);
10025 break;
10028 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10029 OMP_CLAUSE_SET_MAP_KIND (c, kind);
10030 OMP_CLAUSE_DECL (c)
10031 = build_indirect_ref (loc, closure_expr, RO_UNARY_STAR);
10032 OMP_CLAUSE_SIZE (c) = size;
10033 if (ptr_p)
10034 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
10035 new_clauses.safe_push (c);
10037 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10038 OMP_CLAUSE_SET_MAP_KIND (c, ptr_kind);
10039 OMP_CLAUSE_DECL (c) = closure_expr;
10040 OMP_CLAUSE_SIZE (c) = size_zero_node;
10041 new_clauses.safe_push (c);
10046 if (!data.ptr_members_accessed.is_empty ())
10047 for (hash_map<tree, tree>::iterator i = data.ptr_members_accessed.begin ();
10048 i != data.ptr_members_accessed.end (); ++i)
10050 /* For each referenced member that is of pointer or reference-to-pointer
10051 type, create the equivalent of map(alloc:this->ptr[:0]). */
10052 tree field_decl = (*i).first;
10053 tree ptr_member = (*i).second;
10055 for (tree c = *clauses_ptr; c; c = OMP_CLAUSE_CHAIN (c))
10057 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
10058 continue;
10059 /* If map(this->ptr[:N]) already exists, avoid creating another
10060 such map. */
10061 tree decl = OMP_CLAUSE_DECL (c);
10062 if ((TREE_CODE (decl) == INDIRECT_REF
10063 || TREE_CODE (decl) == MEM_REF)
10064 && operand_equal_p (TREE_OPERAND (decl, 0), ptr_member))
10065 goto next_ptr_member;
10068 if (!cxx_mark_addressable (ptr_member))
10069 gcc_unreachable ();
10071 if (TREE_CODE (TREE_TYPE (field_decl)) == REFERENCE_TYPE)
10073 /* For reference to pointers, we need to map the referenced
10074 pointer first for things to be correct. */
10075 tree ptr_member_type = TREE_TYPE (ptr_member);
10077 /* Map pointer target as zero-length array section. */
10078 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10079 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
10080 OMP_CLAUSE_DECL (c)
10081 = build1 (INDIRECT_REF, TREE_TYPE (ptr_member_type), ptr_member);
10082 OMP_CLAUSE_SIZE (c) = size_zero_node;
10083 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
10085 /* Map pointer to zero-length array section. */
10086 tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
10087 OMP_CLAUSE_SET_MAP_KIND
10088 (c2, GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION);
10089 OMP_CLAUSE_DECL (c2) = ptr_member;
10090 OMP_CLAUSE_SIZE (c2) = size_zero_node;
10092 /* Attach reference-to-pointer field to pointer. */
10093 tree c3 = build_omp_clause (loc, OMP_CLAUSE_MAP);
10094 OMP_CLAUSE_SET_MAP_KIND (c3, GOMP_MAP_ATTACH);
10095 OMP_CLAUSE_DECL (c3) = TREE_OPERAND (ptr_member, 0);
10096 OMP_CLAUSE_SIZE (c3) = size_zero_node;
10098 new_clauses.safe_push (c);
10099 new_clauses.safe_push (c2);
10100 new_clauses.safe_push (c3);
10102 else if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
10104 /* Map pointer target as zero-length array section. */
10105 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10106 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
10107 OMP_CLAUSE_DECL (c) = build_indirect_ref (loc, ptr_member,
10108 RO_UNARY_STAR);
10109 OMP_CLAUSE_SIZE (c) = size_zero_node;
10110 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
10112 /* Attach zero-length array section to pointer. */
10113 tree c2 = build_omp_clause (loc, OMP_CLAUSE_MAP);
10114 OMP_CLAUSE_SET_MAP_KIND
10115 (c2, GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION);
10116 OMP_CLAUSE_DECL (c2) = ptr_member;
10117 OMP_CLAUSE_SIZE (c2) = size_zero_node;
10119 new_clauses.safe_push (c);
10120 new_clauses.safe_push (c2);
10122 else
10123 gcc_unreachable ();
10125 next_ptr_member:
10129 for (hash_set<tree>::iterator i = data.lambda_objects_accessed.begin ();
10130 i != data.lambda_objects_accessed.end (); ++i)
10132 tree lobj = *i;
10133 if (TREE_CODE (lobj) == TARGET_EXPR)
10134 lobj = TREE_OPERAND (lobj, 0);
10136 tree lt = TREE_TYPE (lobj);
10137 gcc_assert (LAMBDA_TYPE_P (lt) && CLASS_TYPE_P (lt));
10139 tree lc = build_omp_clause (loc, OMP_CLAUSE_MAP);
10140 OMP_CLAUSE_SET_MAP_KIND (lc, GOMP_MAP_TO);
10141 OMP_CLAUSE_DECL (lc) = lobj;
10142 OMP_CLAUSE_SIZE (lc) = TYPE_SIZE_UNIT (lt);
10143 new_clauses.safe_push (lc);
10145 for (tree fld = TYPE_FIELDS (lt); fld; fld = DECL_CHAIN (fld))
10147 if (TREE_CODE (TREE_TYPE (fld)) == POINTER_TYPE)
10149 tree exp = build3 (COMPONENT_REF, TREE_TYPE (fld),
10150 lobj, fld, NULL_TREE);
10151 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10152 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALLOC);
10153 OMP_CLAUSE_DECL (c)
10154 = build_indirect_ref (loc, exp, RO_UNARY_STAR);
10155 OMP_CLAUSE_SIZE (c) = size_zero_node;
10156 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
10157 new_clauses.safe_push (c);
10159 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10160 OMP_CLAUSE_SET_MAP_KIND
10161 (c, GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION);
10162 OMP_CLAUSE_DECL (c) = exp;
10163 OMP_CLAUSE_SIZE (c) = size_zero_node;
10164 new_clauses.safe_push (c);
10166 else if (TREE_CODE (TREE_TYPE (fld)) == REFERENCE_TYPE)
10168 tree exp = build3 (COMPONENT_REF, TREE_TYPE (fld),
10169 lobj, fld, NULL_TREE);
10170 tree c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10171 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_TOFROM);
10172 OMP_CLAUSE_DECL (c)
10173 = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (exp)), exp);
10174 OMP_CLAUSE_SIZE (c)
10175 = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (exp)));
10176 new_clauses.safe_push (c);
10178 c = build_omp_clause (loc, OMP_CLAUSE_MAP);
10179 OMP_CLAUSE_SET_MAP_KIND (c, GOMP_MAP_ALWAYS_POINTER);
10180 OMP_CLAUSE_DECL (c) = exp;
10181 OMP_CLAUSE_SIZE (c) = size_zero_node;
10182 new_clauses.safe_push (c);
10187 tree c = *clauses_ptr;
10188 for (int i = new_clauses.length () - 1; i >= 0; i--)
10190 OMP_CLAUSE_CHAIN (new_clauses[i]) = c;
10191 c = new_clauses[i];
10193 *clauses_ptr = c;
10196 /* Called from cp_parser_omp_target. Create additional implicit clauses for
10197 OpenMP target directives, and do sanity checks. */
10199 tree
10200 finish_omp_target (location_t loc, tree clauses, tree body, bool combined_p)
10202 if (!processing_template_decl)
10203 finish_omp_target_clauses (loc, body, &clauses);
10205 tree stmt = make_node (OMP_TARGET);
10206 TREE_TYPE (stmt) = void_type_node;
10207 OMP_TARGET_CLAUSES (stmt) = clauses;
10208 OMP_TARGET_BODY (stmt) = body;
10209 OMP_TARGET_COMBINED (stmt) = combined_p;
10210 SET_EXPR_LOCATION (stmt, loc);
10212 tree c = clauses;
10213 while (c)
10215 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP)
10216 switch (OMP_CLAUSE_MAP_KIND (c))
10218 case GOMP_MAP_TO:
10219 case GOMP_MAP_ALWAYS_TO:
10220 case GOMP_MAP_PRESENT_TO:
10221 case GOMP_MAP_ALWAYS_PRESENT_TO:
10222 case GOMP_MAP_FROM:
10223 case GOMP_MAP_ALWAYS_FROM:
10224 case GOMP_MAP_PRESENT_FROM:
10225 case GOMP_MAP_ALWAYS_PRESENT_FROM:
10226 case GOMP_MAP_TOFROM:
10227 case GOMP_MAP_ALWAYS_TOFROM:
10228 case GOMP_MAP_PRESENT_TOFROM:
10229 case GOMP_MAP_ALWAYS_PRESENT_TOFROM:
10230 case GOMP_MAP_ALLOC:
10231 case GOMP_MAP_PRESENT_ALLOC:
10232 case GOMP_MAP_FIRSTPRIVATE_POINTER:
10233 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
10234 case GOMP_MAP_ALWAYS_POINTER:
10235 case GOMP_MAP_ATTACH_DETACH:
10236 case GOMP_MAP_ATTACH:
10237 case GOMP_MAP_ATTACH_ZERO_LENGTH_ARRAY_SECTION:
10238 case GOMP_MAP_POINTER:
10239 case GOMP_MAP_POINTER_TO_ZERO_LENGTH_ARRAY_SECTION:
10240 break;
10241 default:
10242 error_at (OMP_CLAUSE_LOCATION (c),
10243 "%<#pragma omp target%> with map-type other "
10244 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
10245 "on %<map%> clause");
10246 break;
10248 c = OMP_CLAUSE_CHAIN (c);
10250 return add_stmt (stmt);
10253 tree
10254 finish_omp_parallel (tree clauses, tree body)
10256 tree stmt;
10258 body = finish_omp_structured_block (body);
10260 stmt = make_node (OMP_PARALLEL);
10261 TREE_TYPE (stmt) = void_type_node;
10262 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10263 OMP_PARALLEL_BODY (stmt) = body;
10265 return add_stmt (stmt);
10268 tree
10269 begin_omp_task (void)
10271 keep_next_level (true);
10272 return begin_omp_structured_block ();
10275 tree
10276 finish_omp_task (tree clauses, tree body)
10278 tree stmt;
10280 body = finish_omp_structured_block (body);
10282 stmt = make_node (OMP_TASK);
10283 TREE_TYPE (stmt) = void_type_node;
10284 OMP_TASK_CLAUSES (stmt) = clauses;
10285 OMP_TASK_BODY (stmt) = body;
10287 return add_stmt (stmt);
10290 /* Helper function for finish_omp_for. Convert Ith random access iterator
10291 into integral iterator. Return FALSE if successful. */
10293 static bool
10294 handle_omp_for_class_iterator (int i, location_t locus, enum tree_code code,
10295 tree declv, tree orig_declv, tree initv,
10296 tree condv, tree incrv, tree *body,
10297 tree *pre_body, tree &clauses,
10298 int collapse, int ordered)
10300 tree diff, iter_init, iter_incr = NULL, last;
10301 tree incr_var = NULL, orig_pre_body, orig_body, c;
10302 tree decl = TREE_VEC_ELT (declv, i);
10303 tree init = TREE_VEC_ELT (initv, i);
10304 tree cond = TREE_VEC_ELT (condv, i);
10305 tree incr = TREE_VEC_ELT (incrv, i);
10306 tree iter = decl;
10307 location_t elocus = locus;
10309 if (init && EXPR_HAS_LOCATION (init))
10310 elocus = EXPR_LOCATION (init);
10312 switch (TREE_CODE (cond))
10314 case GT_EXPR:
10315 case GE_EXPR:
10316 case LT_EXPR:
10317 case LE_EXPR:
10318 case NE_EXPR:
10319 if (TREE_OPERAND (cond, 1) == iter)
10320 cond = build2 (swap_tree_comparison (TREE_CODE (cond)),
10321 TREE_TYPE (cond), iter, TREE_OPERAND (cond, 0));
10322 if (TREE_OPERAND (cond, 0) != iter)
10323 cond = error_mark_node;
10324 else
10326 tree tem = build_x_binary_op (EXPR_LOCATION (cond),
10327 TREE_CODE (cond),
10328 iter, ERROR_MARK,
10329 TREE_OPERAND (cond, 1), ERROR_MARK,
10330 NULL_TREE, NULL, tf_warning_or_error);
10331 if (error_operand_p (tem))
10332 return true;
10334 break;
10335 default:
10336 cond = error_mark_node;
10337 break;
10339 if (cond == error_mark_node)
10341 error_at (elocus, "invalid controlling predicate");
10342 return true;
10344 diff = build_x_binary_op (elocus, MINUS_EXPR,
10345 TREE_OPERAND (cond, 1), ERROR_MARK,
10346 iter, ERROR_MARK,
10347 NULL_TREE, NULL, tf_warning_or_error);
10348 diff = cp_fully_fold (diff);
10349 if (error_operand_p (diff))
10350 return true;
10351 if (TREE_CODE (TREE_TYPE (diff)) != INTEGER_TYPE)
10353 error_at (elocus, "difference between %qE and %qD does not have integer type",
10354 TREE_OPERAND (cond, 1), iter);
10355 return true;
10357 if (!c_omp_check_loop_iv_exprs (locus, code, orig_declv, i,
10358 TREE_VEC_ELT (declv, i), NULL_TREE,
10359 cond, cp_walk_subtrees))
10360 return true;
10362 switch (TREE_CODE (incr))
10364 case PREINCREMENT_EXPR:
10365 case PREDECREMENT_EXPR:
10366 case POSTINCREMENT_EXPR:
10367 case POSTDECREMENT_EXPR:
10368 if (TREE_OPERAND (incr, 0) != iter)
10370 incr = error_mark_node;
10371 break;
10373 iter_incr = build_x_unary_op (EXPR_LOCATION (incr),
10374 TREE_CODE (incr), iter,
10375 NULL_TREE, tf_warning_or_error);
10376 if (error_operand_p (iter_incr))
10377 return true;
10378 else if (TREE_CODE (incr) == PREINCREMENT_EXPR
10379 || TREE_CODE (incr) == POSTINCREMENT_EXPR)
10380 incr = integer_one_node;
10381 else
10382 incr = integer_minus_one_node;
10383 break;
10384 case MODIFY_EXPR:
10385 if (TREE_OPERAND (incr, 0) != iter)
10386 incr = error_mark_node;
10387 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
10388 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
10390 tree rhs = TREE_OPERAND (incr, 1);
10391 if (TREE_OPERAND (rhs, 0) == iter)
10393 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 1)))
10394 != INTEGER_TYPE)
10395 incr = error_mark_node;
10396 else
10398 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
10399 iter, TREE_CODE (rhs),
10400 TREE_OPERAND (rhs, 1),
10401 NULL_TREE,
10402 tf_warning_or_error);
10403 if (error_operand_p (iter_incr))
10404 return true;
10405 incr = TREE_OPERAND (rhs, 1);
10406 incr = cp_convert (TREE_TYPE (diff), incr,
10407 tf_warning_or_error);
10408 if (TREE_CODE (rhs) == MINUS_EXPR)
10410 incr = build1 (NEGATE_EXPR, TREE_TYPE (diff), incr);
10411 incr = fold_simple (incr);
10413 if (TREE_CODE (incr) != INTEGER_CST
10414 && (TREE_CODE (incr) != NOP_EXPR
10415 || (TREE_CODE (TREE_OPERAND (incr, 0))
10416 != INTEGER_CST)))
10417 iter_incr = NULL;
10420 else if (TREE_OPERAND (rhs, 1) == iter)
10422 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs, 0))) != INTEGER_TYPE
10423 || TREE_CODE (rhs) != PLUS_EXPR)
10424 incr = error_mark_node;
10425 else
10427 iter_incr = build_x_binary_op (EXPR_LOCATION (rhs),
10428 PLUS_EXPR,
10429 TREE_OPERAND (rhs, 0),
10430 ERROR_MARK, iter,
10431 ERROR_MARK, NULL_TREE, NULL,
10432 tf_warning_or_error);
10433 if (error_operand_p (iter_incr))
10434 return true;
10435 iter_incr = build_x_modify_expr (EXPR_LOCATION (rhs),
10436 iter, NOP_EXPR,
10437 iter_incr, NULL_TREE,
10438 tf_warning_or_error);
10439 if (error_operand_p (iter_incr))
10440 return true;
10441 incr = TREE_OPERAND (rhs, 0);
10442 iter_incr = NULL;
10445 else
10446 incr = error_mark_node;
10448 else
10449 incr = error_mark_node;
10450 break;
10451 default:
10452 incr = error_mark_node;
10453 break;
10456 if (incr == error_mark_node)
10458 error_at (elocus, "invalid increment expression");
10459 return true;
10462 incr = cp_convert (TREE_TYPE (diff), incr, tf_warning_or_error);
10463 incr = cp_fully_fold (incr);
10464 tree loop_iv_seen = NULL_TREE;
10465 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10466 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
10467 && OMP_CLAUSE_DECL (c) == iter)
10469 if (code == OMP_TASKLOOP || code == OMP_LOOP)
10471 loop_iv_seen = c;
10472 OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c) = 1;
10474 break;
10476 else if ((code == OMP_TASKLOOP || code == OMP_LOOP)
10477 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
10478 && OMP_CLAUSE_DECL (c) == iter)
10480 loop_iv_seen = c;
10481 if (code == OMP_TASKLOOP)
10482 OMP_CLAUSE_PRIVATE_TASKLOOP_IV (c) = 1;
10485 decl = create_temporary_var (TREE_TYPE (diff));
10486 pushdecl (decl);
10487 add_decl_expr (decl);
10488 last = create_temporary_var (TREE_TYPE (diff));
10489 pushdecl (last);
10490 add_decl_expr (last);
10491 if (c && iter_incr == NULL && TREE_CODE (incr) != INTEGER_CST
10492 && (!ordered || (i < collapse && collapse > 1)))
10494 incr_var = create_temporary_var (TREE_TYPE (diff));
10495 pushdecl (incr_var);
10496 add_decl_expr (incr_var);
10498 gcc_assert (stmts_are_full_exprs_p ());
10499 tree diffvar = NULL_TREE;
10500 if (code == OMP_TASKLOOP)
10502 if (!loop_iv_seen)
10504 tree ivc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
10505 OMP_CLAUSE_DECL (ivc) = iter;
10506 cxx_omp_finish_clause (ivc, NULL, false);
10507 OMP_CLAUSE_CHAIN (ivc) = clauses;
10508 clauses = ivc;
10510 tree lvc = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
10511 OMP_CLAUSE_DECL (lvc) = last;
10512 OMP_CLAUSE_CHAIN (lvc) = clauses;
10513 clauses = lvc;
10514 diffvar = create_temporary_var (TREE_TYPE (diff));
10515 pushdecl (diffvar);
10516 add_decl_expr (diffvar);
10518 else if (code == OMP_LOOP)
10520 if (!loop_iv_seen)
10522 /* While iterators on the loop construct are predetermined
10523 lastprivate, if the decl is not declared inside of the
10524 loop, OMP_CLAUSE_LASTPRIVATE should have been added
10525 already. */
10526 loop_iv_seen = build_omp_clause (locus, OMP_CLAUSE_FIRSTPRIVATE);
10527 OMP_CLAUSE_DECL (loop_iv_seen) = iter;
10528 OMP_CLAUSE_CHAIN (loop_iv_seen) = clauses;
10529 clauses = loop_iv_seen;
10531 else if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_PRIVATE)
10533 OMP_CLAUSE_PRIVATE_DEBUG (loop_iv_seen) = 0;
10534 OMP_CLAUSE_PRIVATE_OUTER_REF (loop_iv_seen) = 0;
10535 OMP_CLAUSE_CODE (loop_iv_seen) = OMP_CLAUSE_FIRSTPRIVATE;
10537 if (OMP_CLAUSE_CODE (loop_iv_seen) == OMP_CLAUSE_FIRSTPRIVATE)
10538 cxx_omp_finish_clause (loop_iv_seen, NULL, false);
10541 orig_pre_body = *pre_body;
10542 *pre_body = push_stmt_list ();
10543 if (orig_pre_body)
10544 add_stmt (orig_pre_body);
10545 if (init != NULL)
10546 finish_expr_stmt (build_x_modify_expr (elocus,
10547 iter, NOP_EXPR, init,
10548 NULL_TREE, tf_warning_or_error));
10549 init = build_int_cst (TREE_TYPE (diff), 0);
10550 if (c && iter_incr == NULL
10551 && (!ordered || (i < collapse && collapse > 1)))
10553 if (incr_var)
10555 finish_expr_stmt (build_x_modify_expr (elocus,
10556 incr_var, NOP_EXPR,
10557 incr, NULL_TREE,
10558 tf_warning_or_error));
10559 incr = incr_var;
10561 iter_incr = build_x_modify_expr (elocus,
10562 iter, PLUS_EXPR, incr,
10563 NULL_TREE, tf_warning_or_error);
10565 if (c && ordered && i < collapse && collapse > 1)
10566 iter_incr = incr;
10567 finish_expr_stmt (build_x_modify_expr (elocus,
10568 last, NOP_EXPR, init,
10569 NULL_TREE, tf_warning_or_error));
10570 if (diffvar)
10572 finish_expr_stmt (build_x_modify_expr (elocus,
10573 diffvar, NOP_EXPR,
10574 diff, NULL_TREE, tf_warning_or_error));
10575 diff = diffvar;
10577 *pre_body = pop_stmt_list (*pre_body);
10579 cond = cp_build_binary_op (elocus,
10580 TREE_CODE (cond), decl, diff,
10581 tf_warning_or_error);
10582 incr = build_modify_expr (elocus, decl, NULL_TREE, PLUS_EXPR,
10583 elocus, incr, NULL_TREE);
10585 orig_body = *body;
10586 *body = push_stmt_list ();
10587 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), decl, last);
10588 iter_init = build_x_modify_expr (elocus,
10589 iter, PLUS_EXPR, iter_init,
10590 NULL_TREE, tf_warning_or_error);
10591 if (iter_init != error_mark_node)
10592 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
10593 finish_expr_stmt (iter_init);
10594 finish_expr_stmt (build_x_modify_expr (elocus,
10595 last, NOP_EXPR, decl,
10596 NULL_TREE, tf_warning_or_error));
10597 add_stmt (orig_body);
10598 *body = pop_stmt_list (*body);
10600 if (c)
10602 OMP_CLAUSE_LASTPRIVATE_STMT (c) = push_stmt_list ();
10603 if (!ordered)
10604 finish_expr_stmt (iter_incr);
10605 else
10607 iter_init = decl;
10608 if (i < collapse && collapse > 1 && !error_operand_p (iter_incr))
10609 iter_init = build2 (PLUS_EXPR, TREE_TYPE (diff),
10610 iter_init, iter_incr);
10611 iter_init = build2 (MINUS_EXPR, TREE_TYPE (diff), iter_init, last);
10612 iter_init = build_x_modify_expr (elocus,
10613 iter, PLUS_EXPR, iter_init,
10614 NULL_TREE, tf_warning_or_error);
10615 if (iter_init != error_mark_node)
10616 iter_init = build1 (NOP_EXPR, void_type_node, iter_init);
10617 finish_expr_stmt (iter_init);
10619 OMP_CLAUSE_LASTPRIVATE_STMT (c)
10620 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (c));
10623 if (TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST)
10625 tree t = TREE_VEC_ELT (orig_declv, i);
10626 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
10627 && TREE_VALUE (t) == NULL_TREE
10628 && TREE_CODE (TREE_CHAIN (t)) == TREE_VEC);
10629 TREE_PURPOSE (t) = TREE_VEC_ELT (declv, i);
10630 TREE_VALUE (t) = last;
10632 else
10633 TREE_VEC_ELT (orig_declv, i)
10634 = tree_cons (TREE_VEC_ELT (declv, i), last, NULL_TREE);
10635 TREE_VEC_ELT (declv, i) = decl;
10636 TREE_VEC_ELT (initv, i) = init;
10637 TREE_VEC_ELT (condv, i) = cond;
10638 TREE_VEC_ELT (incrv, i) = incr;
10640 return false;
10643 /* Build and validate an OMP_FOR statement. CLAUSES, BODY, COND, INCR
10644 are directly for their associated operands in the statement. DECL
10645 and INIT are a combo; if DECL is NULL then INIT ought to be a
10646 MODIFY_EXPR, and the DECL should be extracted. PRE_BODY are
10647 optional statements that need to go before the loop into its
10648 sk_omp scope. */
10650 tree
10651 finish_omp_for (location_t locus, enum tree_code code, tree declv,
10652 tree orig_declv, tree initv, tree condv, tree incrv,
10653 tree body, tree pre_body, vec<tree> *orig_inits, tree clauses)
10655 tree omp_for = NULL, orig_incr = NULL;
10656 tree decl = NULL, init, cond, incr;
10657 location_t elocus;
10658 int i;
10659 int collapse = 1;
10660 int ordered = 0;
10661 auto_vec<location_t> init_locv;
10663 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
10664 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
10665 gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (incrv));
10666 if (TREE_VEC_LENGTH (declv) > 1)
10668 tree c;
10670 c = omp_find_clause (clauses, OMP_CLAUSE_TILE);
10671 if (c)
10672 collapse = list_length (OMP_CLAUSE_TILE_LIST (c));
10673 else
10675 c = omp_find_clause (clauses, OMP_CLAUSE_COLLAPSE);
10676 if (c)
10677 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (c));
10678 if (collapse != TREE_VEC_LENGTH (declv))
10679 ordered = TREE_VEC_LENGTH (declv);
10682 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
10684 decl = TREE_VEC_ELT (declv, i);
10685 init = TREE_VEC_ELT (initv, i);
10686 cond = TREE_VEC_ELT (condv, i);
10687 incr = TREE_VEC_ELT (incrv, i);
10688 elocus = locus;
10690 /* We are going to throw out the init's original MODIFY_EXPR or
10691 MODOP_EXPR below. Save its location so we can use it when
10692 reconstructing the expression farther down. Alternatively, if the
10693 initializer is a binding of the iteration variable, save
10694 that location. Any of these locations in the initialization clause
10695 for the current nested loop are better than using the argument locus,
10696 that points to the "for" of the the outermost loop in the nest. */
10697 if (init && EXPR_HAS_LOCATION (init))
10698 elocus = EXPR_LOCATION (init);
10699 else if (decl && INDIRECT_REF_P (decl) && EXPR_HAS_LOCATION (decl))
10700 /* This can happen for class iterators. */
10701 elocus = EXPR_LOCATION (decl);
10702 else if (decl && DECL_P (decl))
10704 if (DECL_SOURCE_LOCATION (decl) != UNKNOWN_LOCATION)
10705 elocus = DECL_SOURCE_LOCATION (decl);
10706 else if (DECL_INITIAL (decl)
10707 && EXPR_HAS_LOCATION (DECL_INITIAL (decl)))
10708 elocus = EXPR_LOCATION (DECL_INITIAL (decl));
10710 init_locv.safe_push (elocus);
10712 if (decl == NULL)
10714 if (init != NULL)
10715 switch (TREE_CODE (init))
10717 case MODIFY_EXPR:
10718 decl = TREE_OPERAND (init, 0);
10719 init = TREE_OPERAND (init, 1);
10720 break;
10721 case MODOP_EXPR:
10722 if (TREE_CODE (TREE_OPERAND (init, 1)) == NOP_EXPR)
10724 decl = TREE_OPERAND (init, 0);
10725 init = TREE_OPERAND (init, 2);
10727 break;
10728 default:
10729 break;
10732 if (decl == NULL)
10734 error_at (locus,
10735 "expected iteration declaration or initialization");
10736 return NULL;
10740 if (cond == global_namespace)
10741 continue;
10743 if (cond == NULL)
10745 error_at (elocus, "missing controlling predicate");
10746 return NULL;
10749 if (incr == NULL)
10751 error_at (elocus, "missing increment expression");
10752 return NULL;
10755 TREE_VEC_ELT (declv, i) = decl;
10756 TREE_VEC_ELT (initv, i) = init;
10759 if (orig_inits)
10761 bool fail = false;
10762 tree orig_init;
10763 FOR_EACH_VEC_ELT (*orig_inits, i, orig_init)
10764 if (orig_init
10765 && !c_omp_check_loop_iv_exprs (locus, code,
10766 orig_declv ? orig_declv : declv, i,
10767 TREE_VEC_ELT (declv, i), orig_init,
10768 NULL_TREE, cp_walk_subtrees))
10769 fail = true;
10770 if (fail)
10771 return NULL;
10774 if (dependent_omp_for_p (declv, initv, condv, incrv))
10776 tree stmt;
10778 stmt = make_node (code);
10780 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
10782 /* This is really just a place-holder. We'll be decomposing this
10783 again and going through the cp_build_modify_expr path below when
10784 we instantiate the thing. */
10785 TREE_VEC_ELT (initv, i)
10786 = build2_loc (init_locv[i], MODIFY_EXPR, void_type_node,
10787 TREE_VEC_ELT (declv, i), TREE_VEC_ELT (initv, i));
10790 TREE_TYPE (stmt) = void_type_node;
10791 OMP_FOR_INIT (stmt) = initv;
10792 OMP_FOR_COND (stmt) = condv;
10793 OMP_FOR_INCR (stmt) = incrv;
10794 OMP_FOR_BODY (stmt) = body;
10795 OMP_FOR_PRE_BODY (stmt) = pre_body;
10796 OMP_FOR_CLAUSES (stmt) = clauses;
10798 SET_EXPR_LOCATION (stmt, locus);
10799 return add_stmt (stmt);
10802 if (!orig_declv)
10803 orig_declv = copy_node (declv);
10805 if (processing_template_decl)
10806 orig_incr = make_tree_vec (TREE_VEC_LENGTH (incrv));
10808 for (i = 0; i < TREE_VEC_LENGTH (declv); )
10810 decl = TREE_VEC_ELT (declv, i);
10811 init = TREE_VEC_ELT (initv, i);
10812 cond = TREE_VEC_ELT (condv, i);
10813 incr = TREE_VEC_ELT (incrv, i);
10814 if (orig_incr)
10815 TREE_VEC_ELT (orig_incr, i) = incr;
10816 elocus = init_locv[i];
10818 if (!DECL_P (decl))
10820 error_at (elocus, "expected iteration declaration or initialization");
10821 return NULL;
10824 if (incr && TREE_CODE (incr) == MODOP_EXPR)
10826 if (orig_incr)
10827 TREE_VEC_ELT (orig_incr, i) = incr;
10828 incr = cp_build_modify_expr (elocus, TREE_OPERAND (incr, 0),
10829 TREE_CODE (TREE_OPERAND (incr, 1)),
10830 TREE_OPERAND (incr, 2),
10831 tf_warning_or_error);
10834 if (CLASS_TYPE_P (TREE_TYPE (decl)))
10836 if (code == OMP_SIMD)
10838 error_at (elocus, "%<#pragma omp simd%> used with class "
10839 "iteration variable %qE", decl);
10840 return NULL;
10842 if (handle_omp_for_class_iterator (i, locus, code, declv, orig_declv,
10843 initv, condv, incrv, &body,
10844 &pre_body, clauses,
10845 collapse, ordered))
10846 return NULL;
10847 continue;
10850 if (!INTEGRAL_TYPE_P (TREE_TYPE (decl))
10851 && !TYPE_PTR_P (TREE_TYPE (decl)))
10853 error_at (elocus, "invalid type for iteration variable %qE", decl);
10854 return NULL;
10857 if (!processing_template_decl && TREE_CODE (init) != TREE_VEC)
10858 init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
10859 tf_warning_or_error);
10860 else
10861 init = build2_loc (elocus, MODIFY_EXPR, void_type_node, decl, init);
10862 if (decl == error_mark_node || init == error_mark_node)
10863 return NULL;
10865 TREE_VEC_ELT (declv, i) = decl;
10866 TREE_VEC_ELT (initv, i) = init;
10867 TREE_VEC_ELT (condv, i) = cond;
10868 TREE_VEC_ELT (incrv, i) = incr;
10869 i++;
10872 if (pre_body && IS_EMPTY_STMT (pre_body))
10873 pre_body = NULL;
10875 omp_for = c_finish_omp_for (locus, code, declv, orig_declv, initv, condv,
10876 incrv, body, pre_body,
10877 !processing_template_decl);
10879 /* Check for iterators appearing in lb, b or incr expressions. */
10880 if (omp_for && !c_omp_check_loop_iv (omp_for, orig_declv, cp_walk_subtrees))
10881 omp_for = NULL_TREE;
10883 if (omp_for == NULL)
10884 return NULL;
10886 add_stmt (omp_for);
10888 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)); i++)
10890 init = TREE_VEC_ELT (OMP_FOR_INIT (omp_for), i);
10891 decl = TREE_OPERAND (init, 0);
10892 cond = TREE_VEC_ELT (OMP_FOR_COND (omp_for), i);
10893 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i);
10895 if (!processing_template_decl)
10897 if (TREE_CODE (TREE_OPERAND (init, 1)) == TREE_VEC)
10899 tree t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 1);
10900 TREE_VEC_ELT (TREE_OPERAND (init, 1), 1)
10901 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10902 t = TREE_VEC_ELT (TREE_OPERAND (init, 1), 2);
10903 TREE_VEC_ELT (TREE_OPERAND (init, 1), 2)
10904 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10906 else
10908 tree t = TREE_OPERAND (init, 1);
10909 TREE_OPERAND (init, 1)
10910 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10912 if (TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
10914 tree t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1);
10915 TREE_VEC_ELT (TREE_OPERAND (cond, 1), 1)
10916 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10917 t = TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2);
10918 TREE_VEC_ELT (TREE_OPERAND (cond, 1), 2)
10919 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10921 else
10923 tree t = TREE_OPERAND (cond, 1);
10924 TREE_OPERAND (cond, 1)
10925 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10929 if (TREE_CODE (incr) != MODIFY_EXPR)
10930 continue;
10932 if (TREE_SIDE_EFFECTS (TREE_OPERAND (incr, 1))
10933 && BINARY_CLASS_P (TREE_OPERAND (incr, 1))
10934 && !processing_template_decl)
10936 tree t = TREE_OPERAND (TREE_OPERAND (incr, 1), 0);
10937 if (TREE_SIDE_EFFECTS (t)
10938 && t != decl
10939 && (TREE_CODE (t) != NOP_EXPR
10940 || TREE_OPERAND (t, 0) != decl))
10941 TREE_OPERAND (TREE_OPERAND (incr, 1), 0)
10942 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10944 t = TREE_OPERAND (TREE_OPERAND (incr, 1), 1);
10945 if (TREE_SIDE_EFFECTS (t)
10946 && t != decl
10947 && (TREE_CODE (t) != NOP_EXPR
10948 || TREE_OPERAND (t, 0) != decl))
10949 TREE_OPERAND (TREE_OPERAND (incr, 1), 1)
10950 = fold_build_cleanup_point_expr (TREE_TYPE (t), t);
10953 if (orig_incr)
10954 TREE_VEC_ELT (OMP_FOR_INCR (omp_for), i) = TREE_VEC_ELT (orig_incr, i);
10956 OMP_FOR_CLAUSES (omp_for) = clauses;
10958 /* For simd loops with non-static data member iterators, we could have added
10959 OMP_CLAUSE_LINEAR clauses without OMP_CLAUSE_LINEAR_STEP. As we know the
10960 step at this point, fill it in. */
10961 if (code == OMP_SIMD && !processing_template_decl
10962 && TREE_VEC_LENGTH (OMP_FOR_INCR (omp_for)) == 1)
10963 for (tree c = omp_find_clause (clauses, OMP_CLAUSE_LINEAR); c;
10964 c = omp_find_clause (OMP_CLAUSE_CHAIN (c), OMP_CLAUSE_LINEAR))
10965 if (OMP_CLAUSE_LINEAR_STEP (c) == NULL_TREE)
10967 decl = TREE_OPERAND (TREE_VEC_ELT (OMP_FOR_INIT (omp_for), 0), 0);
10968 gcc_assert (decl == OMP_CLAUSE_DECL (c));
10969 incr = TREE_VEC_ELT (OMP_FOR_INCR (omp_for), 0);
10970 tree step, stept;
10971 switch (TREE_CODE (incr))
10973 case PREINCREMENT_EXPR:
10974 case POSTINCREMENT_EXPR:
10975 /* c_omp_for_incr_canonicalize_ptr() should have been
10976 called to massage things appropriately. */
10977 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
10978 OMP_CLAUSE_LINEAR_STEP (c) = build_int_cst (TREE_TYPE (decl), 1);
10979 break;
10980 case PREDECREMENT_EXPR:
10981 case POSTDECREMENT_EXPR:
10982 /* c_omp_for_incr_canonicalize_ptr() should have been
10983 called to massage things appropriately. */
10984 gcc_assert (!INDIRECT_TYPE_P (TREE_TYPE (decl)));
10985 OMP_CLAUSE_LINEAR_STEP (c)
10986 = build_int_cst (TREE_TYPE (decl), -1);
10987 break;
10988 case MODIFY_EXPR:
10989 gcc_assert (TREE_OPERAND (incr, 0) == decl);
10990 incr = TREE_OPERAND (incr, 1);
10991 switch (TREE_CODE (incr))
10993 case PLUS_EXPR:
10994 if (TREE_OPERAND (incr, 1) == decl)
10995 step = TREE_OPERAND (incr, 0);
10996 else
10997 step = TREE_OPERAND (incr, 1);
10998 break;
10999 case MINUS_EXPR:
11000 case POINTER_PLUS_EXPR:
11001 gcc_assert (TREE_OPERAND (incr, 0) == decl);
11002 step = TREE_OPERAND (incr, 1);
11003 break;
11004 default:
11005 gcc_unreachable ();
11007 stept = TREE_TYPE (decl);
11008 if (INDIRECT_TYPE_P (stept))
11009 stept = sizetype;
11010 step = fold_convert (stept, step);
11011 if (TREE_CODE (incr) == MINUS_EXPR)
11012 step = fold_build1 (NEGATE_EXPR, stept, step);
11013 OMP_CLAUSE_LINEAR_STEP (c) = step;
11014 break;
11015 default:
11016 gcc_unreachable ();
11019 /* Override saved methods on OMP_LOOP's OMP_CLAUSE_LASTPRIVATE_LOOP_IV
11020 clauses, we need copy ctor for those rather than default ctor,
11021 plus as for other lastprivates assignment op and dtor. */
11022 if (code == OMP_LOOP && !processing_template_decl)
11023 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
11024 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE
11025 && OMP_CLAUSE_LASTPRIVATE_LOOP_IV (c)
11026 && cxx_omp_create_clause_info (c, TREE_TYPE (OMP_CLAUSE_DECL (c)),
11027 false, true, true, true))
11028 CP_OMP_CLAUSE_INFO (c) = NULL_TREE;
11030 return omp_for;
11033 /* Code walker for finish_omp_for_block: extract binding of DP->var
11034 from its current block and move it to a new BIND_EXPR DP->b
11035 surrounding the body of DP->omp_for. */
11037 struct fofb_data {
11038 tree var;
11039 tree b;
11040 tree omp_for;
11043 static tree
11044 finish_omp_for_block_walker (tree *tp, int *walk_subtrees, void *dp)
11046 struct fofb_data *fofb = (struct fofb_data *)dp;
11047 if (TREE_CODE (*tp) == BIND_EXPR)
11048 for (tree *p = &BIND_EXPR_VARS (*tp); *p; p = &DECL_CHAIN (*p))
11050 if (*p == fofb->var)
11052 *p = DECL_CHAIN (*p);
11053 if (fofb->b == NULL_TREE)
11055 fofb->b = make_node (BLOCK);
11056 fofb->b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
11057 OMP_FOR_BODY (fofb->omp_for), fofb->b);
11058 TREE_SIDE_EFFECTS (fofb->b) = 1;
11059 OMP_FOR_BODY (fofb->omp_for) = fofb->b;
11061 DECL_CHAIN (fofb->var) = BIND_EXPR_VARS (fofb->b);
11062 BIND_EXPR_VARS (fofb->b) = fofb->var;
11063 BLOCK_VARS (BIND_EXPR_BLOCK (fofb->b)) = fofb->var;
11064 BLOCK_VARS (BIND_EXPR_BLOCK (*tp)) = BIND_EXPR_VARS (*tp);
11065 return *tp;
11068 if (TREE_CODE (*tp) != BIND_EXPR && TREE_CODE (*tp) != STATEMENT_LIST)
11069 *walk_subtrees = false;
11070 return NULL_TREE;
11073 /* Fix up range for decls. Those decls were pushed into BIND's
11074 BIND_EXPR_VARS, or that of a nested BIND_EXPR inside its body,
11075 and need to be moved into a new BIND_EXPR surrounding OMP_FOR's body
11076 so that processing of combined loop directives can find them. */
11077 tree
11078 finish_omp_for_block (tree bind, tree omp_for)
11080 if (omp_for == NULL_TREE
11081 || !OMP_FOR_ORIG_DECLS (omp_for)
11082 || bind == NULL_TREE)
11083 return bind;
11084 struct fofb_data fofb;
11085 fofb.b = NULL_TREE;
11086 fofb.omp_for = omp_for;
11087 for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (omp_for)); i++)
11088 if (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)) == TREE_LIST
11089 && TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)))
11091 tree v = TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i));
11092 for (int j = 2; j < TREE_VEC_LENGTH (v); j++)
11094 fofb.var = TREE_VEC_ELT (v, j);
11095 cp_walk_tree (&bind, finish_omp_for_block_walker,
11096 (void *)&fofb, NULL);
11099 return bind;
11102 void
11103 finish_omp_atomic (location_t loc, enum tree_code code, enum tree_code opcode,
11104 tree lhs, tree rhs, tree v, tree lhs1, tree rhs1, tree r,
11105 tree clauses, enum omp_memory_order mo, bool weak)
11107 tree orig_lhs;
11108 tree orig_rhs;
11109 tree orig_v;
11110 tree orig_lhs1;
11111 tree orig_rhs1;
11112 tree orig_r;
11113 bool dependent_p;
11114 tree stmt;
11116 orig_lhs = lhs;
11117 orig_rhs = rhs;
11118 orig_v = v;
11119 orig_lhs1 = lhs1;
11120 orig_rhs1 = rhs1;
11121 orig_r = r;
11122 dependent_p = false;
11123 stmt = NULL_TREE;
11125 /* Even in a template, we can detect invalid uses of the atomic
11126 pragma if neither LHS nor RHS is type-dependent. */
11127 if (processing_template_decl)
11129 dependent_p = (type_dependent_expression_p (lhs)
11130 || (rhs && type_dependent_expression_p (rhs))
11131 || (v && type_dependent_expression_p (v))
11132 || (lhs1 && type_dependent_expression_p (lhs1))
11133 || (rhs1 && type_dependent_expression_p (rhs1))
11134 || (r
11135 && r != void_list_node
11136 && type_dependent_expression_p (r)));
11137 if (clauses)
11139 gcc_assert (TREE_CODE (clauses) == OMP_CLAUSE
11140 && OMP_CLAUSE_CODE (clauses) == OMP_CLAUSE_HINT
11141 && OMP_CLAUSE_CHAIN (clauses) == NULL_TREE);
11142 if (type_dependent_expression_p (OMP_CLAUSE_HINT_EXPR (clauses))
11143 || TREE_CODE (OMP_CLAUSE_HINT_EXPR (clauses)) != INTEGER_CST)
11144 dependent_p = true;
11147 if (!dependent_p)
11149 bool swapped = false;
11150 if (rhs1 && opcode != COND_EXPR && cp_tree_equal (lhs, rhs))
11152 std::swap (rhs, rhs1);
11153 swapped = !commutative_tree_code (opcode);
11155 if (rhs1 && opcode != COND_EXPR && !cp_tree_equal (lhs, rhs1))
11157 if (code == OMP_ATOMIC)
11158 error ("%<#pragma omp atomic update%> uses two different "
11159 "expressions for memory");
11160 else
11161 error ("%<#pragma omp atomic capture%> uses two different "
11162 "expressions for memory");
11163 return;
11165 if (lhs1 && !cp_tree_equal (lhs, lhs1))
11167 if (code == OMP_ATOMIC)
11168 error ("%<#pragma omp atomic update%> uses two different "
11169 "expressions for memory");
11170 else
11171 error ("%<#pragma omp atomic capture%> uses two different "
11172 "expressions for memory");
11173 return;
11175 stmt = c_finish_omp_atomic (loc, code, opcode, lhs, rhs,
11176 v, lhs1, rhs1, r, swapped, mo, weak,
11177 processing_template_decl != 0);
11178 if (stmt == error_mark_node)
11179 return;
11181 if (processing_template_decl)
11183 if (code == OMP_ATOMIC_READ)
11185 stmt = build_min_nt_loc (loc, OMP_ATOMIC_READ, orig_lhs);
11186 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
11187 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
11189 else
11191 if (opcode == NOP_EXPR)
11192 stmt = build2 (MODIFY_EXPR, void_type_node, orig_lhs, orig_rhs);
11193 else if (opcode == COND_EXPR)
11195 stmt = build2 (EQ_EXPR, boolean_type_node, orig_lhs, orig_rhs);
11196 if (orig_r)
11197 stmt = build2 (MODIFY_EXPR, boolean_type_node, orig_r,
11198 stmt);
11199 stmt = build3 (COND_EXPR, void_type_node, stmt, orig_rhs1,
11200 orig_lhs);
11201 orig_rhs1 = NULL_TREE;
11203 else
11204 stmt = build2 (opcode, void_type_node, orig_lhs, orig_rhs);
11205 if (orig_rhs1)
11206 stmt = build_min_nt_loc (EXPR_LOCATION (orig_rhs1),
11207 COMPOUND_EXPR, orig_rhs1, stmt);
11208 if (code != OMP_ATOMIC)
11210 stmt = build_min_nt_loc (loc, code, orig_lhs1, stmt);
11211 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
11212 OMP_ATOMIC_WEAK (stmt) = weak;
11213 stmt = build2 (MODIFY_EXPR, void_type_node, orig_v, stmt);
11216 stmt = build2 (OMP_ATOMIC, void_type_node,
11217 clauses ? clauses : integer_zero_node, stmt);
11218 OMP_ATOMIC_MEMORY_ORDER (stmt) = mo;
11219 OMP_ATOMIC_WEAK (stmt) = weak;
11220 SET_EXPR_LOCATION (stmt, loc);
11223 /* Avoid -Wunused-value warnings here, the whole construct has side-effects
11224 and even if it might be wrapped from fold-const.cc or c-omp.cc wrapped
11225 in some tree that appears to be unused, the value is not unused. */
11226 warning_sentinel w (warn_unused_value);
11227 finish_expr_stmt (stmt);
11230 void
11231 finish_omp_barrier (void)
11233 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_BARRIER);
11234 releasing_vec vec;
11235 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11236 finish_expr_stmt (stmt);
11239 void
11240 finish_omp_depobj (location_t loc, tree depobj,
11241 enum omp_clause_depend_kind kind, tree clause)
11243 if (!error_operand_p (depobj) && !type_dependent_expression_p (depobj))
11245 if (!lvalue_p (depobj))
11247 error_at (EXPR_LOC_OR_LOC (depobj, loc),
11248 "%<depobj%> expression is not lvalue expression");
11249 depobj = error_mark_node;
11253 if (processing_template_decl)
11255 if (clause == NULL_TREE)
11256 clause = build_int_cst (integer_type_node, kind);
11257 add_stmt (build_min_nt_loc (loc, OMP_DEPOBJ, depobj, clause));
11258 return;
11261 if (!error_operand_p (depobj))
11263 tree addr = cp_build_addr_expr (depobj, tf_warning_or_error);
11264 if (addr == error_mark_node)
11265 depobj = error_mark_node;
11266 else
11267 depobj = cp_build_indirect_ref (loc, addr, RO_UNARY_STAR,
11268 tf_warning_or_error);
11271 c_finish_omp_depobj (loc, depobj, kind, clause);
11274 void
11275 finish_omp_flush (int mo)
11277 tree fn = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
11278 releasing_vec vec;
11279 if (mo != MEMMODEL_LAST && mo != MEMMODEL_SEQ_CST)
11281 fn = builtin_decl_explicit (BUILT_IN_ATOMIC_THREAD_FENCE);
11282 vec->quick_push (build_int_cst (integer_type_node, mo));
11284 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11285 finish_expr_stmt (stmt);
11288 void
11289 finish_omp_taskwait (void)
11291 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKWAIT);
11292 releasing_vec vec;
11293 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11294 finish_expr_stmt (stmt);
11297 void
11298 finish_omp_taskyield (void)
11300 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_TASKYIELD);
11301 releasing_vec vec;
11302 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11303 finish_expr_stmt (stmt);
11306 void
11307 finish_omp_cancel (tree clauses)
11309 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11310 int mask = 0;
11311 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
11312 mask = 1;
11313 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
11314 mask = 2;
11315 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
11316 mask = 4;
11317 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
11318 mask = 8;
11319 else
11321 error ("%<#pragma omp cancel%> must specify one of "
11322 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
11323 return;
11325 releasing_vec vec;
11326 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
11327 if (ifc != NULL_TREE)
11329 if (OMP_CLAUSE_IF_MODIFIER (ifc) != ERROR_MARK
11330 && OMP_CLAUSE_IF_MODIFIER (ifc) != VOID_CST)
11331 error_at (OMP_CLAUSE_LOCATION (ifc),
11332 "expected %<cancel%> %<if%> clause modifier");
11333 else
11335 tree ifc2 = omp_find_clause (OMP_CLAUSE_CHAIN (ifc), OMP_CLAUSE_IF);
11336 if (ifc2 != NULL_TREE)
11338 gcc_assert (OMP_CLAUSE_IF_MODIFIER (ifc) == VOID_CST
11339 && OMP_CLAUSE_IF_MODIFIER (ifc2) != ERROR_MARK
11340 && OMP_CLAUSE_IF_MODIFIER (ifc2) != VOID_CST);
11341 error_at (OMP_CLAUSE_LOCATION (ifc2),
11342 "expected %<cancel%> %<if%> clause modifier");
11346 if (!processing_template_decl)
11347 ifc = maybe_convert_cond (OMP_CLAUSE_IF_EXPR (ifc));
11348 else
11349 ifc = build_x_binary_op (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11350 OMP_CLAUSE_IF_EXPR (ifc), ERROR_MARK,
11351 integer_zero_node, ERROR_MARK,
11352 NULL_TREE, NULL, tf_warning_or_error);
11354 else
11355 ifc = boolean_true_node;
11356 vec->quick_push (build_int_cst (integer_type_node, mask));
11357 vec->quick_push (ifc);
11358 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11359 finish_expr_stmt (stmt);
11362 void
11363 finish_omp_cancellation_point (tree clauses)
11365 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11366 int mask = 0;
11367 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
11368 mask = 1;
11369 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
11370 mask = 2;
11371 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
11372 mask = 4;
11373 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
11374 mask = 8;
11375 else
11377 error ("%<#pragma omp cancellation point%> must specify one of "
11378 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> clauses");
11379 return;
11381 releasing_vec vec
11382 = make_tree_vector_single (build_int_cst (integer_type_node, mask));
11383 tree stmt = finish_call_expr (fn, &vec, false, false, tf_warning_or_error);
11384 finish_expr_stmt (stmt);
11387 /* Begin a __transaction_atomic or __transaction_relaxed statement.
11388 If PCOMPOUND is non-null, this is for a function-transaction-block, and we
11389 should create an extra compound stmt. */
11391 tree
11392 begin_transaction_stmt (location_t loc, tree *pcompound, int flags)
11394 tree r;
11396 if (pcompound)
11397 *pcompound = begin_compound_stmt (0);
11399 r = build_stmt (loc, TRANSACTION_EXPR, NULL_TREE);
11401 /* Only add the statement to the function if support enabled. */
11402 if (flag_tm)
11403 add_stmt (r);
11404 else
11405 error_at (loc, ((flags & TM_STMT_ATTR_RELAXED) != 0
11406 ? G_("%<__transaction_relaxed%> without "
11407 "transactional memory support enabled")
11408 : G_("%<__transaction_atomic%> without "
11409 "transactional memory support enabled")));
11411 TRANSACTION_EXPR_BODY (r) = push_stmt_list ();
11412 TREE_SIDE_EFFECTS (r) = 1;
11413 return r;
11416 /* End a __transaction_atomic or __transaction_relaxed statement.
11417 If COMPOUND_STMT is non-null, this is for a function-transaction-block,
11418 and we should end the compound. If NOEX is non-NULL, we wrap the body in
11419 a MUST_NOT_THROW_EXPR with NOEX as condition. */
11421 void
11422 finish_transaction_stmt (tree stmt, tree compound_stmt, int flags, tree noex)
11424 TRANSACTION_EXPR_BODY (stmt) = pop_stmt_list (TRANSACTION_EXPR_BODY (stmt));
11425 TRANSACTION_EXPR_OUTER (stmt) = (flags & TM_STMT_ATTR_OUTER) != 0;
11426 TRANSACTION_EXPR_RELAXED (stmt) = (flags & TM_STMT_ATTR_RELAXED) != 0;
11427 TRANSACTION_EXPR_IS_STMT (stmt) = 1;
11429 /* noexcept specifications are not allowed for function transactions. */
11430 gcc_assert (!(noex && compound_stmt));
11431 if (noex)
11433 tree body = build_must_not_throw_expr (TRANSACTION_EXPR_BODY (stmt),
11434 noex);
11435 protected_set_expr_location
11436 (body, EXPR_LOCATION (TRANSACTION_EXPR_BODY (stmt)));
11437 TREE_SIDE_EFFECTS (body) = 1;
11438 TRANSACTION_EXPR_BODY (stmt) = body;
11441 if (compound_stmt)
11442 finish_compound_stmt (compound_stmt);
11445 /* Build a __transaction_atomic or __transaction_relaxed expression. If
11446 NOEX is non-NULL, we wrap the body in a MUST_NOT_THROW_EXPR with NOEX as
11447 condition. */
11449 tree
11450 build_transaction_expr (location_t loc, tree expr, int flags, tree noex)
11452 tree ret;
11453 if (noex)
11455 expr = build_must_not_throw_expr (expr, noex);
11456 protected_set_expr_location (expr, loc);
11457 TREE_SIDE_EFFECTS (expr) = 1;
11459 ret = build1 (TRANSACTION_EXPR, TREE_TYPE (expr), expr);
11460 if (flags & TM_STMT_ATTR_RELAXED)
11461 TRANSACTION_EXPR_RELAXED (ret) = 1;
11462 TREE_SIDE_EFFECTS (ret) = 1;
11463 SET_EXPR_LOCATION (ret, loc);
11464 return ret;
11467 void
11468 init_cp_semantics (void)
11473 /* Build a STATIC_ASSERT for a static assertion with the condition
11474 CONDITION and the message text MESSAGE. LOCATION is the location
11475 of the static assertion in the source code. When MEMBER_P, this
11476 static assertion is a member of a class. If SHOW_EXPR_P is true,
11477 print the condition (because it was instantiation-dependent). */
11479 void
11480 finish_static_assert (tree condition, tree message, location_t location,
11481 bool member_p, bool show_expr_p)
11483 tsubst_flags_t complain = tf_warning_or_error;
11484 tree message_sz = NULL_TREE, message_data = NULL_TREE;
11486 if (message == NULL_TREE
11487 || message == error_mark_node
11488 || condition == NULL_TREE
11489 || condition == error_mark_node)
11490 return;
11492 if (check_for_bare_parameter_packs (condition)
11493 || check_for_bare_parameter_packs (message))
11494 return;
11496 if (TREE_CODE (message) != STRING_CST
11497 && !type_dependent_expression_p (message))
11499 message_sz
11500 = finish_class_member_access_expr (message,
11501 get_identifier ("size"),
11502 false, complain);
11503 if (message_sz != error_mark_node)
11504 message_data
11505 = finish_class_member_access_expr (message,
11506 get_identifier ("data"),
11507 false, complain);
11508 if (message_sz == error_mark_node || message_data == error_mark_node)
11510 error_at (location, "%<static_assert%> message must be a string "
11511 "literal or object with %<size%> and "
11512 "%<data%> members");
11513 return;
11515 releasing_vec size_args, data_args;
11516 message_sz = finish_call_expr (message_sz, &size_args, false, false,
11517 complain);
11518 message_data = finish_call_expr (message_data, &data_args, false, false,
11519 complain);
11520 if (message_sz == error_mark_node || message_data == error_mark_node)
11521 return;
11522 message_sz = build_converted_constant_expr (size_type_node, message_sz,
11523 complain);
11524 if (message_sz == error_mark_node)
11526 error_at (location, "%<static_assert%> message %<size()%> "
11527 "must be implicitly convertible to "
11528 "%<std::size_t%>");
11529 return;
11531 message_data = build_converted_constant_expr (const_string_type_node,
11532 message_data, complain);
11533 if (message_data == error_mark_node)
11535 error_at (location, "%<static_assert%> message %<data()%> "
11536 "must be implicitly convertible to "
11537 "%<const char*%>");
11538 return;
11542 /* Save the condition in case it was a concept check. */
11543 tree orig_condition = condition;
11545 if (instantiation_dependent_expression_p (condition)
11546 || instantiation_dependent_expression_p (message))
11548 /* We're in a template; build a STATIC_ASSERT and put it in
11549 the right place. */
11550 defer:
11551 tree assertion = make_node (STATIC_ASSERT);
11552 STATIC_ASSERT_CONDITION (assertion) = orig_condition;
11553 STATIC_ASSERT_MESSAGE (assertion) = message;
11554 STATIC_ASSERT_SOURCE_LOCATION (assertion) = location;
11556 if (member_p)
11557 maybe_add_class_template_decl_list (current_class_type,
11558 assertion,
11559 /*friend_p=*/0);
11560 else
11561 add_stmt (assertion);
11563 return;
11566 /* Fold the expression and convert it to a boolean value. */
11567 condition = contextual_conv_bool (condition, complain);
11568 condition = fold_non_dependent_expr (condition, complain,
11569 /*manifestly_const_eval=*/true);
11571 if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
11572 /* Do nothing; the condition is satisfied. */
11574 else
11576 iloc_sentinel ils (location);
11578 if (integer_zerop (condition))
11580 /* CWG2518: static_assert failure in a template is not IFNDR. */
11581 if (processing_template_decl)
11582 goto defer;
11584 int len;
11585 const char *msg = NULL;
11586 char *buf = NULL;
11587 if (message_sz && message_data)
11589 tree msz = cxx_constant_value (message_sz, NULL_TREE, complain);
11590 if (!tree_fits_uhwi_p (msz))
11592 error_at (location,
11593 "%<static_assert%> message %<size()%> "
11594 "must be a constant expression");
11595 return;
11597 else if ((unsigned HOST_WIDE_INT) (int) tree_to_uhwi (msz)
11598 != tree_to_uhwi (msz))
11600 error_at (location,
11601 "%<static_assert%> message %<size()%> "
11602 "%qE too large", msz);
11603 return;
11605 len = tree_to_uhwi (msz);
11606 tree data = maybe_constant_value (message_data, NULL_TREE,
11607 mce_true);
11608 if (!reduced_constant_expression_p (data))
11609 data = NULL_TREE;
11610 if (len)
11612 if (data)
11613 msg = c_getstr (data);
11614 if (msg == NULL)
11615 buf = XNEWVEC (char, len);
11616 for (int i = 0; i < len; ++i)
11618 tree t = message_data;
11619 if (i)
11620 t = build2 (POINTER_PLUS_EXPR,
11621 TREE_TYPE (message_data), message_data,
11622 size_int (i));
11623 t = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
11624 tree t2 = cxx_constant_value (t, NULL_TREE, complain);
11625 if (!tree_fits_shwi_p (t2))
11627 error_at (location,
11628 "%<static_assert%> message %<data()[%d]%> "
11629 "must be a constant expression", i);
11630 XDELETEVEC (buf);
11631 return;
11633 if (msg == NULL)
11634 buf[i] = tree_to_shwi (t2);
11635 /* If c_getstr worked, just verify the first and
11636 last characters using constant evaluation. */
11637 else if (len > 2 && i == 0)
11638 i = len - 2;
11640 if (msg == NULL)
11641 msg = buf;
11643 else if (!data)
11645 /* We don't have any function to test whether some
11646 expression is a core constant expression. So, instead
11647 test whether (message.data (), 0) is a constant
11648 expression. */
11649 data = build2 (COMPOUND_EXPR, integer_type_node,
11650 message_data, integer_zero_node);
11651 tree t = cxx_constant_value (data, NULL_TREE, complain);
11652 if (!integer_zerop (t))
11654 error_at (location,
11655 "%<static_assert%> message %<data()%> "
11656 "must be a core constant expression");
11657 return;
11661 else
11663 tree eltype = TREE_TYPE (TREE_TYPE (message));
11664 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (eltype));
11665 msg = TREE_STRING_POINTER (message);
11666 len = TREE_STRING_LENGTH (message) / sz - 1;
11669 /* See if we can find which clause was failing (for logical AND). */
11670 tree bad = find_failing_clause (NULL, orig_condition);
11671 /* If not, or its location is unusable, fall back to the previous
11672 location. */
11673 location_t cloc = cp_expr_loc_or_loc (bad, location);
11675 auto_diagnostic_group d;
11677 /* Report the error. */
11678 if (len == 0)
11679 error_at (cloc, "static assertion failed");
11680 else
11681 error_at (cloc, "static assertion failed: %.*s", len, msg);
11683 XDELETEVEC (buf);
11685 diagnose_failing_condition (bad, cloc, show_expr_p);
11687 else if (condition && condition != error_mark_node)
11689 error ("non-constant condition for static assertion");
11690 if (require_rvalue_constant_expression (condition))
11691 cxx_constant_value (condition);
11696 /* Implements the C++0x decltype keyword. Returns the type of EXPR,
11697 suitable for use as a type-specifier.
11699 ID_EXPRESSION_OR_MEMBER_ACCESS_P is true when EXPR was parsed as an
11700 id-expression or a class member access, FALSE when it was parsed as
11701 a full expression. */
11703 tree
11704 finish_decltype_type (tree expr, bool id_expression_or_member_access_p,
11705 tsubst_flags_t complain)
11707 tree type = NULL_TREE;
11709 if (!expr || error_operand_p (expr))
11710 return error_mark_node;
11712 if (TYPE_P (expr)
11713 || TREE_CODE (expr) == TYPE_DECL
11714 || (TREE_CODE (expr) == BIT_NOT_EXPR
11715 && TYPE_P (TREE_OPERAND (expr, 0))))
11717 if (complain & tf_error)
11718 error ("argument to %<decltype%> must be an expression");
11719 return error_mark_node;
11722 /* decltype is an unevaluated context. */
11723 cp_unevaluated u;
11725 processing_template_decl_sentinel ptds (/*reset=*/false);
11727 /* Depending on the resolution of DR 1172, we may later need to distinguish
11728 instantiation-dependent but not type-dependent expressions so that, say,
11729 A<decltype(sizeof(T))>::U doesn't require 'typename'. */
11730 if (instantiation_dependent_uneval_expression_p (expr))
11732 dependent:
11733 type = cxx_make_type (DECLTYPE_TYPE);
11734 DECLTYPE_TYPE_EXPR (type) = expr;
11735 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (type)
11736 = id_expression_or_member_access_p;
11737 SET_TYPE_STRUCTURAL_EQUALITY (type);
11739 return type;
11741 else if (processing_template_decl)
11743 expr = instantiate_non_dependent_expr (expr, complain|tf_decltype);
11744 if (expr == error_mark_node)
11745 return error_mark_node;
11746 /* Keep processing_template_decl cleared for the rest of the function
11747 (for sake of the call to lvalue_kind below, which handles templated
11748 and non-templated COND_EXPR differently). */
11749 processing_template_decl = 0;
11752 /* The type denoted by decltype(e) is defined as follows: */
11754 expr = resolve_nondeduced_context (expr, complain);
11755 if (!mark_single_function (expr, complain))
11756 return error_mark_node;
11758 if (invalid_nonstatic_memfn_p (input_location, expr, complain))
11759 return error_mark_node;
11761 if (type_unknown_p (expr))
11763 if (complain & tf_error)
11764 error ("%<decltype%> cannot resolve address of overloaded function");
11765 return error_mark_node;
11768 /* To get the size of a static data member declared as an array of
11769 unknown bound, we need to instantiate it. */
11770 if (VAR_P (expr)
11771 && VAR_HAD_UNKNOWN_BOUND (expr)
11772 && DECL_TEMPLATE_INSTANTIATION (expr))
11773 instantiate_decl (expr, /*defer_ok*/true, /*expl_inst_mem*/false);
11775 if (id_expression_or_member_access_p)
11777 /* If e is an id-expression or a class member access (5.2.5
11778 [expr.ref]), decltype(e) is defined as the type of the entity
11779 named by e. If there is no such entity, or e names a set of
11780 overloaded functions, the program is ill-formed. */
11781 if (identifier_p (expr))
11782 expr = lookup_name (expr);
11784 if (INDIRECT_REF_P (expr)
11785 || TREE_CODE (expr) == VIEW_CONVERT_EXPR)
11786 /* This can happen when the expression is, e.g., "a.b". Just
11787 look at the underlying operand. */
11788 expr = TREE_OPERAND (expr, 0);
11790 if (TREE_CODE (expr) == OFFSET_REF
11791 || TREE_CODE (expr) == MEMBER_REF
11792 || TREE_CODE (expr) == SCOPE_REF)
11793 /* We're only interested in the field itself. If it is a
11794 BASELINK, we will need to see through it in the next
11795 step. */
11796 expr = TREE_OPERAND (expr, 1);
11798 if (BASELINK_P (expr))
11799 /* See through BASELINK nodes to the underlying function. */
11800 expr = BASELINK_FUNCTIONS (expr);
11802 /* decltype of a decomposition name drops references in the tuple case
11803 (unlike decltype of a normal variable) and keeps cv-qualifiers from
11804 the containing object in the other cases (unlike decltype of a member
11805 access expression). */
11806 if (DECL_DECOMPOSITION_P (expr))
11808 if (DECL_HAS_VALUE_EXPR_P (expr))
11809 /* Expr is an array or struct subobject proxy, handle
11810 bit-fields properly. */
11811 return unlowered_expr_type (expr);
11812 else
11813 /* Expr is a reference variable for the tuple case. */
11814 return lookup_decomp_type (expr);
11817 switch (TREE_CODE (expr))
11819 case FIELD_DECL:
11820 if (DECL_BIT_FIELD_TYPE (expr))
11822 type = DECL_BIT_FIELD_TYPE (expr);
11823 break;
11825 /* Fall through for fields that aren't bitfields. */
11826 gcc_fallthrough ();
11828 case VAR_DECL:
11829 if (is_capture_proxy (expr))
11831 if (is_normal_capture_proxy (expr))
11833 expr = DECL_CAPTURED_VARIABLE (expr);
11834 type = TREE_TYPE (expr);
11835 type = non_reference (type);
11837 else
11839 expr = DECL_VALUE_EXPR (expr);
11840 gcc_assert (TREE_CODE (expr) == COMPONENT_REF);
11841 expr = TREE_OPERAND (expr, 1);
11842 type = TREE_TYPE (expr);
11844 break;
11846 /* Fall through for variables that aren't capture proxies. */
11847 gcc_fallthrough ();
11849 case FUNCTION_DECL:
11850 case CONST_DECL:
11851 case PARM_DECL:
11852 case RESULT_DECL:
11853 case TEMPLATE_PARM_INDEX:
11854 expr = mark_type_use (expr);
11855 type = TREE_TYPE (expr);
11856 if (VAR_P (expr) && DECL_NTTP_OBJECT_P (expr))
11858 /* decltype of an NTTP object is the type of the template
11859 parameter, which is the object type modulo cv-quals. */
11860 int quals = cp_type_quals (type);
11861 gcc_checking_assert (quals & TYPE_QUAL_CONST);
11862 type = cv_unqualified (type);
11864 break;
11866 case ERROR_MARK:
11867 type = error_mark_node;
11868 break;
11870 case COMPONENT_REF:
11871 case COMPOUND_EXPR:
11872 mark_type_use (expr);
11873 type = is_bitfield_expr_with_lowered_type (expr);
11874 if (!type)
11875 type = TREE_TYPE (TREE_OPERAND (expr, 1));
11876 break;
11878 case BIT_FIELD_REF:
11879 gcc_unreachable ();
11881 case INTEGER_CST:
11882 case PTRMEM_CST:
11883 /* We can get here when the id-expression refers to an
11884 enumerator or non-type template parameter. */
11885 type = TREE_TYPE (expr);
11886 break;
11888 default:
11889 /* Handle instantiated template non-type arguments. */
11890 type = TREE_TYPE (expr);
11891 break;
11894 else
11896 if (outer_automatic_var_p (STRIP_REFERENCE_REF (expr))
11897 && current_function_decl
11898 && LAMBDA_FUNCTION_P (current_function_decl))
11900 /* [expr.prim.id.unqual]/3: If naming the entity from outside of an
11901 unevaluated operand within S would refer to an entity captured by
11902 copy in some intervening lambda-expression, then let E be the
11903 innermost such lambda-expression.
11905 If there is such a lambda-expression and if P is in E's function
11906 parameter scope but not its parameter-declaration-clause, then the
11907 type of the expression is the type of a class member access
11908 expression naming the non-static data member that would be declared
11909 for such a capture in the object parameter of the function call
11910 operator of E." */
11911 /* FIXME: This transformation needs to happen for all uses of an outer
11912 local variable inside decltype, not just decltype((x)) (PR83167).
11913 And we don't handle nested lambdas properly, where we need to
11914 consider the outer lambdas as well (PR112926). */
11915 tree decl = STRIP_REFERENCE_REF (expr);
11916 tree lam = CLASSTYPE_LAMBDA_EXPR (DECL_CONTEXT (current_function_decl));
11917 tree cap = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
11918 LOOK_want::HIDDEN_LAMBDA);
11920 if (cap && is_capture_proxy (cap))
11921 type = TREE_TYPE (cap);
11922 else if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_COPY)
11924 type = TREE_TYPE (decl);
11925 if (TYPE_REF_P (type)
11926 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
11927 type = TREE_TYPE (type);
11930 if (type && !TYPE_REF_P (type))
11932 tree obtype = TREE_TYPE (DECL_ARGUMENTS (current_function_decl));
11933 if (WILDCARD_TYPE_P (non_reference (obtype)))
11934 /* We don't know what the eventual obtype quals will be. */
11935 goto dependent;
11936 auto direct_type = [](tree t){
11937 if (INDIRECT_TYPE_P (t))
11938 return TREE_TYPE (t);
11939 return t;
11941 int const quals = cp_type_quals (type)
11942 | cp_type_quals (direct_type (obtype));
11943 type = cp_build_qualified_type (type, quals);
11944 type = build_reference_type (type);
11947 else if (error_operand_p (expr))
11948 type = error_mark_node;
11949 else if (expr == current_class_ptr)
11950 /* If the expression is just "this", we want the
11951 cv-unqualified pointer for the "this" type. */
11952 type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
11954 if (!type)
11956 /* Otherwise, where T is the type of e, if e is an lvalue,
11957 decltype(e) is defined as T&; if an xvalue, T&&; otherwise, T. */
11958 cp_lvalue_kind clk = lvalue_kind (expr);
11959 type = unlowered_expr_type (expr);
11960 gcc_assert (!TYPE_REF_P (type));
11962 /* For vector types, pick a non-opaque variant. */
11963 if (VECTOR_TYPE_P (type))
11964 type = strip_typedefs (type);
11966 if (clk != clk_none && !(clk & clk_class))
11967 type = cp_build_reference_type (type, (clk & clk_rvalueref));
11971 return type;
11974 /* Called from trait_expr_value to evaluate either __has_nothrow_assign or
11975 __has_nothrow_copy, depending on assign_p. Returns true iff all
11976 the copy {ctor,assign} fns are nothrow. */
11978 static bool
11979 classtype_has_nothrow_assign_or_copy_p (tree type, bool assign_p)
11981 tree fns = NULL_TREE;
11983 if (assign_p || TYPE_HAS_COPY_CTOR (type))
11984 fns = get_class_binding (type, assign_p ? assign_op_identifier
11985 : ctor_identifier);
11987 bool saw_copy = false;
11988 for (ovl_iterator iter (fns); iter; ++iter)
11990 tree fn = *iter;
11992 if (copy_fn_p (fn) > 0)
11994 saw_copy = true;
11995 if (!maybe_instantiate_noexcept (fn)
11996 || !TYPE_NOTHROW_P (TREE_TYPE (fn)))
11997 return false;
12001 return saw_copy;
12004 /* Return true if DERIVED is pointer interconvertible base of BASE. */
12006 static bool
12007 pointer_interconvertible_base_of_p (tree base, tree derived)
12009 if (base == error_mark_node || derived == error_mark_node)
12010 return false;
12011 base = TYPE_MAIN_VARIANT (base);
12012 derived = TYPE_MAIN_VARIANT (derived);
12013 if (!NON_UNION_CLASS_TYPE_P (base)
12014 || !NON_UNION_CLASS_TYPE_P (derived))
12015 return false;
12017 if (same_type_p (base, derived))
12018 return true;
12020 if (!std_layout_type_p (derived))
12021 return false;
12023 return uniquely_derived_from_p (base, derived);
12026 /* Helper function for fold_builtin_is_pointer_inverconvertible_with_class,
12027 return true if MEMBERTYPE is the type of the first non-static data member
12028 of TYPE or for unions of any members. */
12029 static bool
12030 first_nonstatic_data_member_p (tree type, tree membertype)
12032 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
12034 if (TREE_CODE (field) != FIELD_DECL)
12035 continue;
12036 if (DECL_FIELD_IS_BASE (field) && is_empty_field (field))
12037 continue;
12038 if (DECL_FIELD_IS_BASE (field))
12039 return first_nonstatic_data_member_p (TREE_TYPE (field), membertype);
12040 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
12042 if ((TREE_CODE (TREE_TYPE (field)) == UNION_TYPE
12043 || std_layout_type_p (TREE_TYPE (field)))
12044 && first_nonstatic_data_member_p (TREE_TYPE (field), membertype))
12045 return true;
12047 else if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field),
12048 membertype))
12049 return true;
12050 if (TREE_CODE (type) != UNION_TYPE)
12051 return false;
12053 return false;
12056 /* Fold __builtin_is_pointer_interconvertible_with_class call. */
12058 tree
12059 fold_builtin_is_pointer_inverconvertible_with_class (location_t loc, int nargs,
12060 tree *args)
12062 /* Unless users call the builtin directly, the following 3 checks should be
12063 ensured from std::is_pointer_interconvertible_with_class function
12064 template. */
12065 if (nargs != 1)
12067 error_at (loc, "%<__builtin_is_pointer_interconvertible_with_class%> "
12068 "needs a single argument");
12069 return boolean_false_node;
12071 tree arg = args[0];
12072 if (error_operand_p (arg))
12073 return boolean_false_node;
12074 if (!TYPE_PTRMEM_P (TREE_TYPE (arg)))
12076 error_at (loc, "%<__builtin_is_pointer_interconvertible_with_class%> "
12077 "argument is not pointer to member");
12078 return boolean_false_node;
12081 if (!TYPE_PTRDATAMEM_P (TREE_TYPE (arg)))
12082 return boolean_false_node;
12084 tree membertype = TREE_TYPE (TREE_TYPE (arg));
12085 tree basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg));
12086 if (!complete_type_or_else (basetype, NULL_TREE))
12087 return boolean_false_node;
12089 if (TREE_CODE (basetype) != UNION_TYPE
12090 && !std_layout_type_p (basetype))
12091 return boolean_false_node;
12093 if (!first_nonstatic_data_member_p (basetype, membertype))
12094 return boolean_false_node;
12096 if (TREE_CODE (arg) == PTRMEM_CST)
12097 arg = cplus_expand_constant (arg);
12099 if (integer_nonzerop (arg))
12100 return boolean_false_node;
12101 if (integer_zerop (arg))
12102 return boolean_true_node;
12104 return fold_build2 (EQ_EXPR, boolean_type_node, arg,
12105 build_zero_cst (TREE_TYPE (arg)));
12108 /* Helper function for is_corresponding_member_aggr. Return true if
12109 MEMBERTYPE pointer-to-data-member ARG can be found in anonymous
12110 union or structure BASETYPE. */
12112 static bool
12113 is_corresponding_member_union (tree basetype, tree membertype, tree arg)
12115 for (tree field = TYPE_FIELDS (basetype); field; field = DECL_CHAIN (field))
12116 if (TREE_CODE (field) != FIELD_DECL || DECL_BIT_FIELD_TYPE (field))
12117 continue;
12118 else if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field),
12119 membertype))
12121 if (TREE_CODE (arg) != INTEGER_CST
12122 || tree_int_cst_equal (arg, byte_position (field)))
12123 return true;
12125 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
12127 tree narg = arg;
12128 if (TREE_CODE (basetype) != UNION_TYPE
12129 && TREE_CODE (narg) == INTEGER_CST)
12130 narg = size_binop (MINUS_EXPR, arg, byte_position (field));
12131 if (is_corresponding_member_union (TREE_TYPE (field),
12132 membertype, narg))
12133 return true;
12135 return false;
12138 /* Helper function for fold_builtin_is_corresponding_member call.
12139 Return boolean_false_node if MEMBERTYPE1 BASETYPE1::*ARG1 and
12140 MEMBERTYPE2 BASETYPE2::*ARG2 aren't corresponding members,
12141 boolean_true_node if they are corresponding members, or for
12142 non-constant ARG2 the highest member offset for corresponding
12143 members. */
12145 static tree
12146 is_corresponding_member_aggr (location_t loc, tree basetype1, tree membertype1,
12147 tree arg1, tree basetype2, tree membertype2,
12148 tree arg2)
12150 tree field1 = TYPE_FIELDS (basetype1);
12151 tree field2 = TYPE_FIELDS (basetype2);
12152 tree ret = boolean_false_node;
12153 while (1)
12155 bool r = next_common_initial_sequence (field1, field2);
12156 if (field1 == NULL_TREE || field2 == NULL_TREE)
12157 break;
12158 if (r
12159 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field1),
12160 membertype1)
12161 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (field2),
12162 membertype2))
12164 tree pos = byte_position (field1);
12165 if (TREE_CODE (arg1) == INTEGER_CST
12166 && tree_int_cst_equal (arg1, pos))
12168 if (TREE_CODE (arg2) == INTEGER_CST)
12169 return boolean_true_node;
12170 return pos;
12172 else if (TREE_CODE (arg1) != INTEGER_CST)
12173 ret = pos;
12175 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field1))
12176 && ANON_AGGR_TYPE_P (TREE_TYPE (field2)))
12178 if ((!lookup_attribute ("no_unique_address",
12179 DECL_ATTRIBUTES (field1)))
12180 != !lookup_attribute ("no_unique_address",
12181 DECL_ATTRIBUTES (field2)))
12182 break;
12183 if (!tree_int_cst_equal (bit_position (field1),
12184 bit_position (field2)))
12185 break;
12186 bool overlap = true;
12187 tree pos = byte_position (field1);
12188 if (TREE_CODE (arg1) == INTEGER_CST)
12190 tree off1 = fold_convert (sizetype, arg1);
12191 tree sz1 = TYPE_SIZE_UNIT (TREE_TYPE (field1));
12192 if (tree_int_cst_lt (off1, pos)
12193 || tree_int_cst_le (size_binop (PLUS_EXPR, pos, sz1), off1))
12194 overlap = false;
12196 if (TREE_CODE (arg2) == INTEGER_CST)
12198 tree off2 = fold_convert (sizetype, arg2);
12199 tree sz2 = TYPE_SIZE_UNIT (TREE_TYPE (field2));
12200 if (tree_int_cst_lt (off2, pos)
12201 || tree_int_cst_le (size_binop (PLUS_EXPR, pos, sz2), off2))
12202 overlap = false;
12204 if (overlap
12205 && NON_UNION_CLASS_TYPE_P (TREE_TYPE (field1))
12206 && NON_UNION_CLASS_TYPE_P (TREE_TYPE (field2)))
12208 tree narg1 = arg1;
12209 if (TREE_CODE (arg1) == INTEGER_CST)
12210 narg1 = size_binop (MINUS_EXPR,
12211 fold_convert (sizetype, arg1), pos);
12212 tree narg2 = arg2;
12213 if (TREE_CODE (arg2) == INTEGER_CST)
12214 narg2 = size_binop (MINUS_EXPR,
12215 fold_convert (sizetype, arg2), pos);
12216 tree t1 = TREE_TYPE (field1);
12217 tree t2 = TREE_TYPE (field2);
12218 tree nret = is_corresponding_member_aggr (loc, t1, membertype1,
12219 narg1, t2, membertype2,
12220 narg2);
12221 if (nret != boolean_false_node)
12223 if (nret == boolean_true_node)
12224 return nret;
12225 if (TREE_CODE (arg1) == INTEGER_CST)
12226 return size_binop (PLUS_EXPR, nret, pos);
12227 ret = size_binop (PLUS_EXPR, nret, pos);
12230 else if (overlap
12231 && TREE_CODE (TREE_TYPE (field1)) == UNION_TYPE
12232 && TREE_CODE (TREE_TYPE (field2)) == UNION_TYPE)
12234 tree narg1 = arg1;
12235 if (TREE_CODE (arg1) == INTEGER_CST)
12236 narg1 = size_binop (MINUS_EXPR,
12237 fold_convert (sizetype, arg1), pos);
12238 tree narg2 = arg2;
12239 if (TREE_CODE (arg2) == INTEGER_CST)
12240 narg2 = size_binop (MINUS_EXPR,
12241 fold_convert (sizetype, arg2), pos);
12242 if (is_corresponding_member_union (TREE_TYPE (field1),
12243 membertype1, narg1)
12244 && is_corresponding_member_union (TREE_TYPE (field2),
12245 membertype2, narg2))
12247 sorry_at (loc, "%<__builtin_is_corresponding_member%> "
12248 "not well defined for anonymous unions");
12249 return boolean_false_node;
12253 if (!r)
12254 break;
12255 field1 = DECL_CHAIN (field1);
12256 field2 = DECL_CHAIN (field2);
12258 return ret;
12261 /* Fold __builtin_is_corresponding_member call. */
12263 tree
12264 fold_builtin_is_corresponding_member (location_t loc, int nargs,
12265 tree *args)
12267 /* Unless users call the builtin directly, the following 3 checks should be
12268 ensured from std::is_corresponding_member function template. */
12269 if (nargs != 2)
12271 error_at (loc, "%<__builtin_is_corresponding_member%> "
12272 "needs two arguments");
12273 return boolean_false_node;
12275 tree arg1 = args[0];
12276 tree arg2 = args[1];
12277 if (error_operand_p (arg1) || error_operand_p (arg2))
12278 return boolean_false_node;
12279 if (!TYPE_PTRMEM_P (TREE_TYPE (arg1))
12280 || !TYPE_PTRMEM_P (TREE_TYPE (arg2)))
12282 error_at (loc, "%<__builtin_is_corresponding_member%> "
12283 "argument is not pointer to member");
12284 return boolean_false_node;
12287 if (!TYPE_PTRDATAMEM_P (TREE_TYPE (arg1))
12288 || !TYPE_PTRDATAMEM_P (TREE_TYPE (arg2)))
12289 return boolean_false_node;
12291 tree membertype1 = TREE_TYPE (TREE_TYPE (arg1));
12292 tree basetype1 = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg1));
12293 if (!complete_type_or_else (basetype1, NULL_TREE))
12294 return boolean_false_node;
12296 tree membertype2 = TREE_TYPE (TREE_TYPE (arg2));
12297 tree basetype2 = TYPE_OFFSET_BASETYPE (TREE_TYPE (arg2));
12298 if (!complete_type_or_else (basetype2, NULL_TREE))
12299 return boolean_false_node;
12301 if (!NON_UNION_CLASS_TYPE_P (basetype1)
12302 || !NON_UNION_CLASS_TYPE_P (basetype2)
12303 || !std_layout_type_p (basetype1)
12304 || !std_layout_type_p (basetype2))
12305 return boolean_false_node;
12307 /* If the member types aren't layout compatible, then they
12308 can't be corresponding members. */
12309 if (!layout_compatible_type_p (membertype1, membertype2))
12310 return boolean_false_node;
12312 if (TREE_CODE (arg1) == PTRMEM_CST)
12313 arg1 = cplus_expand_constant (arg1);
12314 if (TREE_CODE (arg2) == PTRMEM_CST)
12315 arg2 = cplus_expand_constant (arg2);
12317 if (null_member_pointer_value_p (arg1)
12318 || null_member_pointer_value_p (arg2))
12319 return boolean_false_node;
12321 if (TREE_CODE (arg1) == INTEGER_CST
12322 && TREE_CODE (arg2) == INTEGER_CST
12323 && !tree_int_cst_equal (arg1, arg2))
12324 return boolean_false_node;
12326 if (TREE_CODE (arg2) == INTEGER_CST
12327 && TREE_CODE (arg1) != INTEGER_CST)
12329 std::swap (arg1, arg2);
12330 std::swap (membertype1, membertype2);
12331 std::swap (basetype1, basetype2);
12334 tree ret = is_corresponding_member_aggr (loc, basetype1, membertype1, arg1,
12335 basetype2, membertype2, arg2);
12336 if (TREE_TYPE (ret) == boolean_type_node)
12337 return ret;
12338 /* If both arg1 and arg2 are INTEGER_CSTs, is_corresponding_member_aggr
12339 already returns boolean_{true,false}_node whether those particular
12340 members are corresponding members or not. Otherwise, if only
12341 one of them is INTEGER_CST (canonicalized to first being INTEGER_CST
12342 above), it returns boolean_false_node if it is certainly not a
12343 corresponding member and otherwise we need to do a runtime check that
12344 those two OFFSET_TYPE offsets are equal.
12345 If neither of the operands is INTEGER_CST, is_corresponding_member_aggr
12346 returns the largest offset at which the members would be corresponding
12347 members, so perform arg1 <= ret && arg1 == arg2 runtime check. */
12348 gcc_assert (TREE_CODE (arg2) != INTEGER_CST);
12349 if (TREE_CODE (arg1) == INTEGER_CST)
12350 return fold_build2 (EQ_EXPR, boolean_type_node, arg1,
12351 fold_convert (TREE_TYPE (arg1), arg2));
12352 ret = fold_build2 (LE_EXPR, boolean_type_node,
12353 fold_convert (pointer_sized_int_node, arg1),
12354 fold_convert (pointer_sized_int_node, ret));
12355 return fold_build2 (TRUTH_AND_EXPR, boolean_type_node, ret,
12356 fold_build2 (EQ_EXPR, boolean_type_node, arg1,
12357 fold_convert (TREE_TYPE (arg1), arg2)));
12360 /* Actually evaluates the trait. */
12362 static bool
12363 trait_expr_value (cp_trait_kind kind, tree type1, tree type2)
12365 enum tree_code type_code1;
12366 tree t;
12368 type_code1 = TREE_CODE (type1);
12370 switch (kind)
12372 case CPTK_HAS_NOTHROW_ASSIGN:
12373 type1 = strip_array_types (type1);
12374 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
12375 && (trait_expr_value (CPTK_HAS_TRIVIAL_ASSIGN, type1, type2)
12376 || (CLASS_TYPE_P (type1)
12377 && classtype_has_nothrow_assign_or_copy_p (type1,
12378 true))));
12380 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
12381 type1 = strip_array_types (type1);
12382 return (trait_expr_value (CPTK_HAS_TRIVIAL_CONSTRUCTOR, type1, type2)
12383 || (CLASS_TYPE_P (type1)
12384 && (t = locate_ctor (type1))
12385 && maybe_instantiate_noexcept (t)
12386 && TYPE_NOTHROW_P (TREE_TYPE (t))));
12388 case CPTK_HAS_NOTHROW_COPY:
12389 type1 = strip_array_types (type1);
12390 return (trait_expr_value (CPTK_HAS_TRIVIAL_COPY, type1, type2)
12391 || (CLASS_TYPE_P (type1)
12392 && classtype_has_nothrow_assign_or_copy_p (type1, false)));
12394 case CPTK_HAS_TRIVIAL_ASSIGN:
12395 /* ??? The standard seems to be missing the "or array of such a class
12396 type" wording for this trait. */
12397 type1 = strip_array_types (type1);
12398 return (!CP_TYPE_CONST_P (type1) && type_code1 != REFERENCE_TYPE
12399 && (trivial_type_p (type1)
12400 || (CLASS_TYPE_P (type1)
12401 && TYPE_HAS_TRIVIAL_COPY_ASSIGN (type1))));
12403 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
12404 type1 = strip_array_types (type1);
12405 return (trivial_type_p (type1)
12406 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_DFLT (type1)));
12408 case CPTK_HAS_TRIVIAL_COPY:
12409 /* ??? The standard seems to be missing the "or array of such a class
12410 type" wording for this trait. */
12411 type1 = strip_array_types (type1);
12412 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
12413 || (CLASS_TYPE_P (type1) && TYPE_HAS_TRIVIAL_COPY_CTOR (type1)));
12415 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
12416 type1 = strip_array_types (type1);
12417 return (trivial_type_p (type1) || type_code1 == REFERENCE_TYPE
12418 || (CLASS_TYPE_P (type1)
12419 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type1)));
12421 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
12422 return type_has_unique_obj_representations (type1);
12424 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
12425 return type_has_virtual_destructor (type1);
12427 case CPTK_IS_ABSTRACT:
12428 return ABSTRACT_CLASS_TYPE_P (type1);
12430 case CPTK_IS_AGGREGATE:
12431 return CP_AGGREGATE_TYPE_P (type1);
12433 case CPTK_IS_ARRAY:
12434 return type_code1 == ARRAY_TYPE;
12436 case CPTK_IS_ASSIGNABLE:
12437 return is_xible (MODIFY_EXPR, type1, type2);
12439 case CPTK_IS_BASE_OF:
12440 return (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
12441 && (same_type_ignoring_top_level_qualifiers_p (type1, type2)
12442 || DERIVED_FROM_P (type1, type2)));
12444 case CPTK_IS_BOUNDED_ARRAY:
12445 return type_code1 == ARRAY_TYPE && TYPE_DOMAIN (type1);
12447 case CPTK_IS_CLASS:
12448 return NON_UNION_CLASS_TYPE_P (type1);
12450 case CPTK_IS_CONSTRUCTIBLE:
12451 return is_xible (INIT_EXPR, type1, type2);
12453 case CPTK_IS_CONVERTIBLE:
12454 return is_convertible (type1, type2);
12456 case CPTK_IS_EMPTY:
12457 return NON_UNION_CLASS_TYPE_P (type1) && CLASSTYPE_EMPTY_P (type1);
12459 case CPTK_IS_ENUM:
12460 return type_code1 == ENUMERAL_TYPE;
12462 case CPTK_IS_FINAL:
12463 return CLASS_TYPE_P (type1) && CLASSTYPE_FINAL (type1);
12465 case CPTK_IS_FUNCTION:
12466 return type_code1 == FUNCTION_TYPE;
12468 case CPTK_IS_LAYOUT_COMPATIBLE:
12469 return layout_compatible_type_p (type1, type2);
12471 case CPTK_IS_LITERAL_TYPE:
12472 return literal_type_p (type1);
12474 case CPTK_IS_MEMBER_FUNCTION_POINTER:
12475 return TYPE_PTRMEMFUNC_P (type1);
12477 case CPTK_IS_MEMBER_OBJECT_POINTER:
12478 return TYPE_PTRDATAMEM_P (type1);
12480 case CPTK_IS_MEMBER_POINTER:
12481 return TYPE_PTRMEM_P (type1);
12483 case CPTK_IS_NOTHROW_ASSIGNABLE:
12484 return is_nothrow_xible (MODIFY_EXPR, type1, type2);
12486 case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
12487 return is_nothrow_xible (INIT_EXPR, type1, type2);
12489 case CPTK_IS_NOTHROW_CONVERTIBLE:
12490 return is_nothrow_convertible (type1, type2);
12492 case CPTK_IS_OBJECT:
12493 return (type_code1 != FUNCTION_TYPE
12494 && type_code1 != REFERENCE_TYPE
12495 && type_code1 != VOID_TYPE);
12497 case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
12498 return pointer_interconvertible_base_of_p (type1, type2);
12500 case CPTK_IS_POD:
12501 return pod_type_p (type1);
12503 case CPTK_IS_POLYMORPHIC:
12504 return CLASS_TYPE_P (type1) && TYPE_POLYMORPHIC_P (type1);
12506 case CPTK_IS_REFERENCE:
12507 return type_code1 == REFERENCE_TYPE;
12509 case CPTK_IS_SAME:
12510 return same_type_p (type1, type2);
12512 case CPTK_IS_SCOPED_ENUM:
12513 return SCOPED_ENUM_P (type1);
12515 case CPTK_IS_STD_LAYOUT:
12516 return std_layout_type_p (type1);
12518 case CPTK_IS_TRIVIAL:
12519 return trivial_type_p (type1);
12521 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
12522 return is_trivially_xible (MODIFY_EXPR, type1, type2);
12524 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
12525 return is_trivially_xible (INIT_EXPR, type1, type2);
12527 case CPTK_IS_TRIVIALLY_COPYABLE:
12528 return trivially_copyable_p (type1);
12530 case CPTK_IS_UNION:
12531 return type_code1 == UNION_TYPE;
12533 case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
12534 return ref_xes_from_temporary (type1, type2, /*direct_init=*/true);
12536 case CPTK_REF_CONVERTS_FROM_TEMPORARY:
12537 return ref_xes_from_temporary (type1, type2, /*direct_init=*/false);
12539 case CPTK_IS_DEDUCIBLE:
12540 return type_targs_deducible_from (type1, type2);
12542 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
12543 case CPTK_##CODE:
12544 #include "cp-trait.def"
12545 #undef DEFTRAIT_TYPE
12546 /* Type-yielding traits are handled in finish_trait_type. */
12547 break;
12550 gcc_unreachable ();
12553 /* Returns true if TYPE meets the requirements for the specified KIND,
12554 false otherwise.
12556 When KIND == 1, TYPE must be an array of unknown bound,
12557 or (possibly cv-qualified) void, or a complete type.
12559 When KIND == 2, TYPE must be a complete type, or array of complete type,
12560 or (possibly cv-qualified) void.
12562 When KIND == 3:
12563 If TYPE is a non-union class type, it must be complete.
12565 When KIND == 4:
12566 If TYPE is a class type, it must be complete. */
12568 static bool
12569 check_trait_type (tree type, int kind = 1)
12571 if (type == NULL_TREE)
12572 return true;
12574 if (TREE_CODE (type) == TREE_VEC)
12576 for (tree arg : tree_vec_range (type))
12577 if (!check_trait_type (arg, kind))
12578 return false;
12579 return true;
12582 if (kind == 1 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
12583 return true; // Array of unknown bound. Don't care about completeness.
12585 if (kind == 3 && !NON_UNION_CLASS_TYPE_P (type))
12586 return true; // Not a non-union class type. Don't care about completeness.
12588 if (kind == 4 && TREE_CODE (type) == ARRAY_TYPE)
12589 return true; // Not a class type. Don't care about completeness.
12591 if (VOID_TYPE_P (type))
12592 return true;
12594 type = complete_type (strip_array_types (type));
12595 if (!COMPLETE_TYPE_P (type)
12596 && cxx_incomplete_type_diagnostic (NULL_TREE, type, DK_PERMERROR)
12597 && !flag_permissive)
12598 return false;
12599 return true;
12602 /* Process a trait expression. */
12604 tree
12605 finish_trait_expr (location_t loc, cp_trait_kind kind, tree type1, tree type2)
12607 if (type1 == error_mark_node
12608 || type2 == error_mark_node)
12609 return error_mark_node;
12611 if (processing_template_decl)
12613 tree trait_expr = make_node (TRAIT_EXPR);
12614 TREE_TYPE (trait_expr) = boolean_type_node;
12615 TRAIT_EXPR_TYPE1 (trait_expr) = type1;
12616 TRAIT_EXPR_TYPE2 (trait_expr) = type2;
12617 TRAIT_EXPR_KIND (trait_expr) = kind;
12618 TRAIT_EXPR_LOCATION (trait_expr) = loc;
12619 return trait_expr;
12622 switch (kind)
12624 case CPTK_HAS_NOTHROW_ASSIGN:
12625 case CPTK_HAS_TRIVIAL_ASSIGN:
12626 case CPTK_HAS_NOTHROW_CONSTRUCTOR:
12627 case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
12628 case CPTK_HAS_NOTHROW_COPY:
12629 case CPTK_HAS_TRIVIAL_COPY:
12630 case CPTK_HAS_TRIVIAL_DESTRUCTOR:
12631 case CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS:
12632 if (!check_trait_type (type1))
12633 return error_mark_node;
12634 break;
12636 case CPTK_IS_LITERAL_TYPE:
12637 case CPTK_IS_POD:
12638 case CPTK_IS_STD_LAYOUT:
12639 case CPTK_IS_TRIVIAL:
12640 case CPTK_IS_TRIVIALLY_COPYABLE:
12641 if (!check_trait_type (type1, /* kind = */ 2))
12642 return error_mark_node;
12643 break;
12645 case CPTK_IS_ABSTRACT:
12646 case CPTK_IS_EMPTY:
12647 case CPTK_IS_POLYMORPHIC:
12648 case CPTK_HAS_VIRTUAL_DESTRUCTOR:
12649 if (!check_trait_type (type1, /* kind = */ 3))
12650 return error_mark_node;
12651 break;
12653 /* N.B. std::is_aggregate is kind=2 but we don't need a complete element
12654 type to know whether an array is an aggregate, so use kind=4 here. */
12655 case CPTK_IS_AGGREGATE:
12656 case CPTK_IS_FINAL:
12657 if (!check_trait_type (type1, /* kind = */ 4))
12658 return error_mark_node;
12659 break;
12661 case CPTK_IS_ASSIGNABLE:
12662 case CPTK_IS_CONSTRUCTIBLE:
12663 if (!check_trait_type (type1))
12664 return error_mark_node;
12665 break;
12667 case CPTK_IS_CONVERTIBLE:
12668 case CPTK_IS_NOTHROW_ASSIGNABLE:
12669 case CPTK_IS_NOTHROW_CONSTRUCTIBLE:
12670 case CPTK_IS_NOTHROW_CONVERTIBLE:
12671 case CPTK_IS_TRIVIALLY_ASSIGNABLE:
12672 case CPTK_IS_TRIVIALLY_CONSTRUCTIBLE:
12673 case CPTK_REF_CONSTRUCTS_FROM_TEMPORARY:
12674 case CPTK_REF_CONVERTS_FROM_TEMPORARY:
12675 if (!check_trait_type (type1)
12676 || !check_trait_type (type2))
12677 return error_mark_node;
12678 break;
12680 case CPTK_IS_BASE_OF:
12681 case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
12682 if (NON_UNION_CLASS_TYPE_P (type1) && NON_UNION_CLASS_TYPE_P (type2)
12683 && !same_type_ignoring_top_level_qualifiers_p (type1, type2)
12684 && !complete_type_or_else (type2, NULL_TREE))
12685 /* We already issued an error. */
12686 return error_mark_node;
12687 break;
12689 case CPTK_IS_ARRAY:
12690 case CPTK_IS_BOUNDED_ARRAY:
12691 case CPTK_IS_CLASS:
12692 case CPTK_IS_ENUM:
12693 case CPTK_IS_FUNCTION:
12694 case CPTK_IS_MEMBER_FUNCTION_POINTER:
12695 case CPTK_IS_MEMBER_OBJECT_POINTER:
12696 case CPTK_IS_MEMBER_POINTER:
12697 case CPTK_IS_OBJECT:
12698 case CPTK_IS_REFERENCE:
12699 case CPTK_IS_SAME:
12700 case CPTK_IS_SCOPED_ENUM:
12701 case CPTK_IS_UNION:
12702 break;
12704 case CPTK_IS_LAYOUT_COMPATIBLE:
12705 if (!array_of_unknown_bound_p (type1)
12706 && TREE_CODE (type1) != VOID_TYPE
12707 && !complete_type_or_else (type1, NULL_TREE))
12708 /* We already issued an error. */
12709 return error_mark_node;
12710 if (!array_of_unknown_bound_p (type2)
12711 && TREE_CODE (type2) != VOID_TYPE
12712 && !complete_type_or_else (type2, NULL_TREE))
12713 /* We already issued an error. */
12714 return error_mark_node;
12715 break;
12717 case CPTK_IS_DEDUCIBLE:
12718 if (!DECL_TYPE_TEMPLATE_P (type1))
12720 error ("%qD is not a class or alias template", type1);
12721 return error_mark_node;
12723 break;
12725 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
12726 case CPTK_##CODE:
12727 #include "cp-trait.def"
12728 #undef DEFTRAIT_TYPE
12729 /* Type-yielding traits are handled in finish_trait_type. */
12730 gcc_unreachable ();
12733 tree val = (trait_expr_value (kind, type1, type2)
12734 ? boolean_true_node : boolean_false_node);
12735 return maybe_wrap_with_location (val, loc);
12738 /* Process a trait type. */
12740 tree
12741 finish_trait_type (cp_trait_kind kind, tree type1, tree type2,
12742 tsubst_flags_t complain)
12744 if (type1 == error_mark_node
12745 || type2 == error_mark_node)
12746 return error_mark_node;
12748 if (processing_template_decl)
12750 tree type = cxx_make_type (TRAIT_TYPE);
12751 TRAIT_TYPE_TYPE1 (type) = type1;
12752 TRAIT_TYPE_TYPE2 (type) = type2;
12753 TRAIT_TYPE_KIND_RAW (type) = build_int_cstu (integer_type_node, kind);
12754 /* These traits are intended to be used in the definition of the ::type
12755 member of the corresponding standard library type trait and aren't
12756 mangleable (and thus won't appear directly in template signatures),
12757 so structural equality should suffice. */
12758 SET_TYPE_STRUCTURAL_EQUALITY (type);
12759 return type;
12762 switch (kind)
12764 case CPTK_REMOVE_CV:
12765 return cv_unqualified (type1);
12767 case CPTK_REMOVE_CVREF:
12768 if (TYPE_REF_P (type1))
12769 type1 = TREE_TYPE (type1);
12770 return cv_unqualified (type1);
12772 case CPTK_REMOVE_POINTER:
12773 if (TYPE_PTR_P (type1))
12774 type1 = TREE_TYPE (type1);
12775 return type1;
12777 case CPTK_REMOVE_REFERENCE:
12778 if (TYPE_REF_P (type1))
12779 type1 = TREE_TYPE (type1);
12780 return type1;
12782 case CPTK_TYPE_PACK_ELEMENT:
12783 return finish_type_pack_element (type1, type2, complain);
12785 case CPTK_UNDERLYING_TYPE:
12786 return finish_underlying_type (type1);
12788 #define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
12789 case CPTK_##CODE:
12790 #include "cp-trait.def"
12791 #undef DEFTRAIT_EXPR
12792 /* Expression-yielding traits are handled in finish_trait_expr. */
12793 case CPTK_BASES:
12794 case CPTK_DIRECT_BASES:
12795 /* BASES and DIRECT_BASES are handled in finish_bases. */
12796 break;
12799 gcc_unreachable ();
12802 /* Do-nothing variants of functions to handle pragma FLOAT_CONST_DECIMAL64,
12803 which is ignored for C++. */
12805 void
12806 set_float_const_decimal64 (void)
12810 void
12811 clear_float_const_decimal64 (void)
12815 bool
12816 float_const_decimal64_p (void)
12818 return 0;
12822 /* Return true if T designates the implied `this' parameter. */
12824 bool
12825 is_this_parameter (tree t)
12827 if (!DECL_P (t) || DECL_NAME (t) != this_identifier)
12828 return false;
12829 gcc_assert (TREE_CODE (t) == PARM_DECL
12830 || (VAR_P (t) && DECL_HAS_VALUE_EXPR_P (t))
12831 || (cp_binding_oracle && VAR_P (t)));
12832 return true;
12835 /* Insert the deduced return type for an auto function. */
12837 void
12838 apply_deduced_return_type (tree fco, tree return_type)
12840 tree result;
12842 if (return_type == error_mark_node)
12843 return;
12845 if (DECL_CONV_FN_P (fco))
12846 DECL_NAME (fco) = make_conv_op_name (return_type);
12848 TREE_TYPE (fco) = change_return_type (return_type, TREE_TYPE (fco));
12850 maybe_update_postconditions (fco);
12852 /* Apply the type to the result object. */
12854 result = DECL_RESULT (fco);
12855 if (result == NULL_TREE)
12856 return;
12857 if (TREE_TYPE (result) == return_type)
12858 return;
12860 if (!processing_template_decl && !VOID_TYPE_P (return_type)
12861 && !complete_type_or_else (return_type, NULL_TREE))
12862 return;
12864 /* We already have a DECL_RESULT from start_preparsed_function.
12865 Now we need to redo the work it and allocate_struct_function
12866 did to reflect the new type. */
12867 result = build_decl (DECL_SOURCE_LOCATION (result), RESULT_DECL, NULL_TREE,
12868 TYPE_MAIN_VARIANT (return_type));
12869 DECL_ARTIFICIAL (result) = 1;
12870 DECL_IGNORED_P (result) = 1;
12871 cp_apply_type_quals_to_decl (cp_type_quals (return_type),
12872 result);
12873 DECL_RESULT (fco) = result;
12875 if (!processing_template_decl)
12876 if (function *fun = DECL_STRUCT_FUNCTION (fco))
12878 bool aggr = aggregate_value_p (result, fco);
12879 #ifdef PCC_STATIC_STRUCT_RETURN
12880 fun->returns_pcc_struct = aggr;
12881 #endif
12882 fun->returns_struct = aggr;
12886 /* Build a unary fold expression of EXPR over OP. If IS_RIGHT is true,
12887 this is a right unary fold. Otherwise it is a left unary fold. */
12889 static tree
12890 finish_unary_fold_expr (location_t loc, tree expr, int op, tree_code dir)
12892 /* Build a pack expansion (assuming expr has pack type). */
12893 if (!uses_parameter_packs (expr))
12895 error_at (location_of (expr), "operand of fold expression has no "
12896 "unexpanded parameter packs");
12897 return error_mark_node;
12899 tree pack = make_pack_expansion (expr);
12901 /* Build the fold expression. */
12902 tree code = build_int_cstu (integer_type_node, abs (op));
12903 tree fold = build_min_nt_loc (loc, dir, code, pack);
12904 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
12905 TREE_TYPE (fold) = build_dependent_operator_type (NULL_TREE,
12906 FOLD_EXPR_OP (fold),
12907 FOLD_EXPR_MODIFY_P (fold));
12908 return fold;
12911 tree
12912 finish_left_unary_fold_expr (location_t loc, tree expr, int op)
12914 return finish_unary_fold_expr (loc, expr, op, UNARY_LEFT_FOLD_EXPR);
12917 tree
12918 finish_right_unary_fold_expr (location_t loc, tree expr, int op)
12920 return finish_unary_fold_expr (loc, expr, op, UNARY_RIGHT_FOLD_EXPR);
12923 /* Build a binary fold expression over EXPR1 and EXPR2. The
12924 associativity of the fold is determined by EXPR1 and EXPR2 (whichever
12925 has an unexpanded parameter pack). */
12927 static tree
12928 finish_binary_fold_expr (location_t loc, tree pack, tree init,
12929 int op, tree_code dir)
12931 pack = make_pack_expansion (pack);
12932 tree code = build_int_cstu (integer_type_node, abs (op));
12933 tree fold = build_min_nt_loc (loc, dir, code, pack, init);
12934 FOLD_EXPR_MODIFY_P (fold) = (op < 0);
12935 TREE_TYPE (fold) = build_dependent_operator_type (NULL_TREE,
12936 FOLD_EXPR_OP (fold),
12937 FOLD_EXPR_MODIFY_P (fold));
12938 return fold;
12941 tree
12942 finish_binary_fold_expr (location_t loc, tree expr1, tree expr2, int op)
12944 // Determine which expr has an unexpanded parameter pack and
12945 // set the pack and initial term.
12946 bool pack1 = uses_parameter_packs (expr1);
12947 bool pack2 = uses_parameter_packs (expr2);
12948 if (pack1 && !pack2)
12949 return finish_binary_fold_expr (loc, expr1, expr2, op, BINARY_RIGHT_FOLD_EXPR);
12950 else if (pack2 && !pack1)
12951 return finish_binary_fold_expr (loc, expr2, expr1, op, BINARY_LEFT_FOLD_EXPR);
12952 else
12954 if (pack1)
12955 error ("both arguments in binary fold have unexpanded parameter packs");
12956 else
12957 error ("no unexpanded parameter packs in binary fold");
12959 return error_mark_node;
12962 /* Finish __builtin_launder (arg). */
12964 tree
12965 finish_builtin_launder (location_t loc, tree arg, tsubst_flags_t complain)
12967 tree orig_arg = arg;
12968 if (!type_dependent_expression_p (arg))
12969 arg = decay_conversion (arg, complain);
12970 if (error_operand_p (arg))
12971 return error_mark_node;
12972 if (!type_dependent_expression_p (arg)
12973 && !TYPE_PTR_P (TREE_TYPE (arg)))
12975 error_at (loc, "non-pointer argument to %<__builtin_launder%>");
12976 return error_mark_node;
12978 if (processing_template_decl)
12979 arg = orig_arg;
12980 return build_call_expr_internal_loc (loc, IFN_LAUNDER,
12981 TREE_TYPE (arg), 1, arg);
12984 /* Finish __builtin_convertvector (arg, type). */
12986 tree
12987 cp_build_vec_convert (tree arg, location_t loc, tree type,
12988 tsubst_flags_t complain)
12990 if (error_operand_p (type))
12991 return error_mark_node;
12992 if (error_operand_p (arg))
12993 return error_mark_node;
12995 tree ret = NULL_TREE;
12996 if (!type_dependent_expression_p (arg) && !dependent_type_p (type))
12997 ret = c_build_vec_convert (cp_expr_loc_or_input_loc (arg),
12998 decay_conversion (arg, complain),
12999 loc, type, (complain & tf_error) != 0);
13001 if (!processing_template_decl)
13002 return ret;
13004 return build_call_expr_internal_loc (loc, IFN_VEC_CONVERT, type, 1, arg);
13007 /* Finish __builtin_bit_cast (type, arg). */
13009 tree
13010 cp_build_bit_cast (location_t loc, tree type, tree arg,
13011 tsubst_flags_t complain)
13013 if (error_operand_p (type))
13014 return error_mark_node;
13015 if (!dependent_type_p (type))
13017 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
13018 return error_mark_node;
13019 if (TREE_CODE (type) == ARRAY_TYPE)
13021 /* std::bit_cast for destination ARRAY_TYPE is not possible,
13022 as functions may not return an array, so don't bother trying
13023 to support this (and then deal with VLAs etc.). */
13024 error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
13025 "is an array type", type);
13026 return error_mark_node;
13028 if (!trivially_copyable_p (type))
13030 error_at (loc, "%<__builtin_bit_cast%> destination type %qT "
13031 "is not trivially copyable", type);
13032 return error_mark_node;
13036 if (error_operand_p (arg))
13037 return error_mark_node;
13039 if (!type_dependent_expression_p (arg))
13041 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
13043 /* Don't perform array-to-pointer conversion. */
13044 arg = mark_rvalue_use (arg, loc, true);
13045 if (!complete_type_or_maybe_complain (TREE_TYPE (arg), arg, complain))
13046 return error_mark_node;
13048 else
13049 arg = decay_conversion (arg, complain);
13051 if (error_operand_p (arg))
13052 return error_mark_node;
13054 if (!trivially_copyable_p (TREE_TYPE (arg)))
13056 error_at (cp_expr_loc_or_loc (arg, loc),
13057 "%<__builtin_bit_cast%> source type %qT "
13058 "is not trivially copyable", TREE_TYPE (arg));
13059 return error_mark_node;
13061 if (!dependent_type_p (type)
13062 && !cp_tree_equal (TYPE_SIZE_UNIT (type),
13063 TYPE_SIZE_UNIT (TREE_TYPE (arg))))
13065 error_at (loc, "%<__builtin_bit_cast%> source size %qE "
13066 "not equal to destination type size %qE",
13067 TYPE_SIZE_UNIT (TREE_TYPE (arg)),
13068 TYPE_SIZE_UNIT (type));
13069 return error_mark_node;
13073 tree ret = build_min (BIT_CAST_EXPR, type, arg);
13074 SET_EXPR_LOCATION (ret, loc);
13076 if (!processing_template_decl && CLASS_TYPE_P (type))
13077 ret = get_target_expr (ret, complain);
13079 return ret;
13082 /* Diagnose invalid #pragma GCC unroll argument and adjust
13083 it if needed. */
13085 tree
13086 cp_check_pragma_unroll (location_t loc, tree unroll)
13088 HOST_WIDE_INT lunroll = 0;
13089 if (type_dependent_expression_p (unroll))
13091 else if (!INTEGRAL_TYPE_P (TREE_TYPE (unroll))
13092 || (!value_dependent_expression_p (unroll)
13093 && (!tree_fits_shwi_p (unroll)
13094 || (lunroll = tree_to_shwi (unroll)) < 0
13095 || lunroll >= USHRT_MAX)))
13097 error_at (loc, "%<#pragma GCC unroll%> requires an"
13098 " assignment-expression that evaluates to a non-negative"
13099 " integral constant less than %u", USHRT_MAX);
13100 unroll = integer_one_node;
13102 else if (TREE_CODE (unroll) == INTEGER_CST)
13104 unroll = fold_convert (integer_type_node, unroll);
13105 if (integer_zerop (unroll))
13106 unroll = integer_one_node;
13108 return unroll;
13111 #include "gt-cp-semantics.h"