aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathaniel Shead <nathanieloshead@gmail.com>2025-02-11 22:59:36 +1100
committerNathaniel Shead <nathanieloshead@gmail.com>2025-03-07 21:13:29 +1100
commit95e23e74ccb1fe7decf916c12e74486b692372cd (patch)
tree25e165e9632705a5f90c3065fceaafe24e64ae2d
parente1c49f413c8c892a61612b3b89de0607ff7ba893 (diff)
downloadgcc-95e23e74ccb1fe7decf916c12e74486b692372cd.zip
gcc-95e23e74ccb1fe7decf916c12e74486b692372cd.tar.gz
gcc-95e23e74ccb1fe7decf916c12e74486b692372cd.tar.bz2
c++: Handle TU_LOCAL_ENTITY in tsubst_expr and potential_constant_expression
This cleans up the TU_LOCAL_ENTITY handling to avoid unnecessary tree walks and make the logic more robust. gcc/cp/ChangeLog: * constexpr.cc (potential_constant_expression_1): Handle TU_LOCAL_ENTITY. * pt.cc (expr_contains_tu_local_entity): Remove. (function_contains_tu_local_entity): Remove. (dependent_operand_p): Remove special handling for TU_LOCAL_ENTITY. (tsubst_expr): Handle TU_LOCAL_ENTITY when tsubsting OVERLOADs; remove now-unnecessary extra handling. (type_dependent_expression_p): Handle TU_LOCAL_ENTITY. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com> Reviewed-by: Patrick Palka <ppalka@redhat.com> Reviewed-by: Jason Merrill <jason@redhat.com>
-rw-r--r--gcc/cp/constexpr.cc5
-rw-r--r--gcc/cp/pt.cc80
2 files changed, 19 insertions, 66 deletions
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index aa2fa44..88b070d 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -10938,6 +10938,11 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
case CO_RETURN_EXPR:
return false;
+ /* Assume a TU-local entity is not constant, we'll error later when
+ instantiating. */
+ case TU_LOCAL_ENTITY:
+ return false;
+
case NONTYPE_ARGUMENT_PACK:
{
tree args = ARGUMENT_PACK_ARGS (t);
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 2775af7..2378f89 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -9935,61 +9935,6 @@ complain_about_tu_local_entity (tree e)
inform (TU_LOCAL_ENTITY_LOCATION (e), "declared here");
}
-/* Checks if T contains a TU-local entity. */
-
-static bool
-expr_contains_tu_local_entity (tree t)
-{
- if (!modules_p ())
- return false;
-
- auto walker = [](tree *tp, int *walk_subtrees, void *) -> tree
- {
- if (TREE_CODE (*tp) == TU_LOCAL_ENTITY)
- return *tp;
- if (!EXPR_P (*tp))
- *walk_subtrees = false;
- return NULL_TREE;
- };
- return cp_walk_tree (&t, walker, nullptr, nullptr);
-}
-
-/* Errors and returns TRUE if X is a function that contains a TU-local
- entity in its overload set. */
-
-static bool
-function_contains_tu_local_entity (tree x)
-{
- if (!modules_p ())
- return false;
-
- if (!x || x == error_mark_node)
- return false;
-
- if (TREE_CODE (x) == OFFSET_REF
- || TREE_CODE (x) == COMPONENT_REF)
- x = TREE_OPERAND (x, 1);
- x = MAYBE_BASELINK_FUNCTIONS (x);
- if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
- x = TREE_OPERAND (x, 0);
-
- if (OVL_P (x))
- for (tree ovl : lkp_range (x))
- if (TREE_CODE (ovl) == TU_LOCAL_ENTITY)
- {
- x = ovl;
- break;
- }
-
- if (TREE_CODE (x) == TU_LOCAL_ENTITY)
- {
- complain_about_tu_local_entity (x);
- return true;
- }
-
- return false;
-}
-
/* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
ARGLIST. Valid choices for FNS are given in the cp-tree.def
documentation for TEMPLATE_ID_EXPR. */
@@ -18814,11 +18759,6 @@ dependent_operand_p (tree t)
while (TREE_CODE (t) == IMPLICIT_CONV_EXPR)
t = TREE_OPERAND (t, 0);
- /* If we contain a TU_LOCAL_ENTITY assume we're non-dependent; we'll error
- later when instantiating. */
- if (expr_contains_tu_local_entity (t))
- return false;
-
++processing_template_decl;
bool r = (potential_constant_expression (t)
? value_dependent_expression_p (t)
@@ -20624,9 +20564,6 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
else
object = NULL_TREE;
- if (function_contains_tu_local_entity (templ))
- RETURN (error_mark_node);
-
tree tid = lookup_template_function (templ, targs);
protected_set_expr_location (tid, EXPR_LOCATION (t));
@@ -21319,9 +21256,6 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
qualified_p = true;
}
- if (function_contains_tu_local_entity (function))
- RETURN (error_mark_node);
-
nargs = call_expr_nargs (t);
releasing_vec call_args;
tsubst_call_args (t, args, complain, in_decl, call_args);
@@ -22117,7 +22051,16 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
RETURN (t);
case NAMESPACE_DECL:
+ RETURN (t);
+
case OVERLOAD:
+ if (modules_p ())
+ for (tree ovl : lkp_range (t))
+ if (TREE_CODE (ovl) == TU_LOCAL_ENTITY)
+ {
+ complain_about_tu_local_entity (ovl);
+ RETURN (error_mark_node);
+ }
RETURN (t);
case TEMPLATE_DECL:
@@ -28985,6 +28928,11 @@ type_dependent_expression_p (tree expression)
STRIP_ANY_LOCATION_WRAPPER (expression);
+ /* Assume a TU-local entity is not dependent, we'll error later when
+ instantiating anyway. */
+ if (TREE_CODE (expression) == TU_LOCAL_ENTITY)
+ return false;
+
/* An unresolved name is always dependent. */
if (identifier_p (expression)
|| TREE_CODE (expression) == USING_DECL)