]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Handle TU_LOCAL_ENTITY in tsubst_expr and potential_constant_expression
authorNathaniel Shead <nathanieloshead@gmail.com>
Tue, 11 Feb 2025 11:59:36 +0000 (22:59 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Fri, 7 Mar 2025 10:13:29 +0000 (21:13 +1100)
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>
gcc/cp/constexpr.cc
gcc/cp/pt.cc

index aa2fa44613a0cc60b3dc5fcb9db919b412fc7bb0..88b070d8a8e63f6f8ffe36ea66df60e64bf012b1 100644 (file)
@@ -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);
index 2775af7701b8a29f0f8ca97a132da208706903fb..2378f892a535f486309367a75bc024159ea408c6 100644 (file)
@@ -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)