[clang] [clang][bytecode] Allow constexpr-unknown values in GetPtrBase{,Pop} (PR #193903)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 24 03:29:44 PDT 2026


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/193903

>From 4abe54219ebc06e57ecc021bde444609d8ee37ca Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 24 Apr 2026 08:48:02 +0200
Subject: [PATCH] [clang][bytecode] Allow constexpr-unknown values in
 GetPtrBase{,Pop}

---
 clang/lib/AST/ByteCode/Interp.cpp             | 38 +++++++++++++
 clang/lib/AST/ByteCode/Interp.h               | 53 ++-----------------
 .../libcxx/constexpr-unknown-getbase.cpp      | 35 ++++++++++++
 3 files changed, 76 insertions(+), 50 deletions(-)
 create mode 100644 clang/test/AST/ByteCode/libcxx/constexpr-unknown-getbase.cpp

diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 6d1598655659d..344d012e78dee 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1484,6 +1484,44 @@ bool GetPtrFieldPop(InterpState &S, CodePtr OpPC, uint32_t Off) {
   return getField(S, OpPC, Ptr, Off);
 }
 
+static bool getBase(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
+                    uint32_t Off, bool NullOK) {
+  if (!NullOK && !CheckNull(S, OpPC, Ptr, CSK_Base))
+    return false;
+
+  if (!Ptr.isBlockPointer()) {
+    if (!Ptr.isIntegralPointer())
+      return false;
+    S.Stk.push<Pointer>(Ptr.asIntPointer().baseCast(S.getASTContext(), Off));
+    return true;
+  }
+
+  if (!CheckSubobject(S, OpPC, Ptr, CSK_Base))
+    return false;
+
+  // In case this isn't something we can get the base of at all,
+  // just return the pointer itself so it can be diagnosed later.
+  if (!Ptr.getFieldDesc()->isRecord()) {
+    S.Stk.push<Pointer>(Ptr);
+    return true;
+  }
+
+  const Pointer &Result = Ptr.atField(Off);
+  if (Result.isPastEnd() || !Result.isBaseClass())
+    return false;
+  S.Stk.push<Pointer>(Result);
+  return true;
+}
+
+bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
+  const auto &Ptr = S.Stk.peek<Pointer>();
+  return getBase(S, OpPC, Ptr, Off, /*NullOK=*/true);
+}
+bool GetPtrBasePop(InterpState &S, CodePtr OpPC, uint32_t Off, bool NullOK) {
+  const auto &Ptr = S.Stk.pop<Pointer>();
+  return getBase(S, OpPC, Ptr, Off, NullOK);
+}
+
 static bool checkConstructor(InterpState &S, CodePtr OpPC, const Function *Func,
                              const Pointer &ThisPtr) {
   assert(Func->isConstructor());
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 0f7f2c4e1fdec..07eca27bee145 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -1975,6 +1975,9 @@ inline bool GetPtrGlobal(InterpState &S, CodePtr OpPC, uint32_t I) {
 bool GetPtrField(InterpState &S, CodePtr OpPC, uint32_t Off);
 bool GetPtrFieldPop(InterpState &S, CodePtr OpPC, uint32_t Off);
 
+bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off);
+bool GetPtrBasePop(InterpState &S, CodePtr OpPC, uint32_t Off, bool NullOK);
+
 inline bool GetPtrThisField(InterpState &S, CodePtr OpPC, uint32_t Off) {
   if (S.checkingPotentialConstantExpression() && S.Current->getDepth() == 0)
     return false;
@@ -2019,56 +2022,6 @@ inline bool GetPtrDerivedPop(InterpState &S, CodePtr OpPC, uint32_t Off,
   return true;
 }
 
-inline bool GetPtrBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
-  const Pointer &Ptr = S.Stk.peek<Pointer>();
-  if (!CheckNull(S, OpPC, Ptr, CSK_Base))
-    return false;
-
-  if (!Ptr.isBlockPointer()) {
-    if (!Ptr.isIntegralPointer())
-      return false;
-    S.Stk.push<Pointer>(Ptr.asIntPointer().baseCast(S.getASTContext(), Off));
-    return true;
-  }
-
-  if (isConstexprUnknown(Ptr))
-    return false;
-
-  if (!CheckSubobject(S, OpPC, Ptr, CSK_Base))
-    return false;
-  const Pointer &Result = Ptr.atField(Off);
-  if (Result.isPastEnd() || !Result.isBaseClass())
-    return false;
-  S.Stk.push<Pointer>(Result);
-  return true;
-}
-
-inline bool GetPtrBasePop(InterpState &S, CodePtr OpPC, uint32_t Off,
-                          bool NullOK) {
-  const Pointer &Ptr = S.Stk.pop<Pointer>();
-
-  if (!NullOK && !CheckNull(S, OpPC, Ptr, CSK_Base))
-    return false;
-
-  if (!Ptr.isBlockPointer()) {
-    if (!Ptr.isIntegralPointer())
-      return false;
-    S.Stk.push<Pointer>(Ptr.asIntPointer().baseCast(S.getASTContext(), Off));
-    return true;
-  }
-
-  if (isConstexprUnknown(Ptr))
-    return false;
-
-  if (!CheckSubobject(S, OpPC, Ptr, CSK_Base))
-    return false;
-  const Pointer &Result = Ptr.atField(Off);
-  if (Result.isPastEnd() || !Result.isBaseClass())
-    return false;
-  S.Stk.push<Pointer>(Result);
-  return true;
-}
-
 inline bool GetPtrThisBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
   if (S.checkingPotentialConstantExpression() && S.Current->isBottomFrame())
     return false;
diff --git a/clang/test/AST/ByteCode/libcxx/constexpr-unknown-getbase.cpp b/clang/test/AST/ByteCode/libcxx/constexpr-unknown-getbase.cpp
new file mode 100644
index 0000000000000..740a01c09517a
--- /dev/null
+++ b/clang/test/AST/ByteCode/libcxx/constexpr-unknown-getbase.cpp
@@ -0,0 +1,35 @@
+// RUN: %clang_cc1 -std=c++2c -fexperimental-new-constant-interpreter -verify=expected,both %s
+// RUN: %clang_cc1 -std=c++2c  -verify=ref,both                                             %s
+
+// both-no-diagnostics
+
+namespace std {
+template <class _Tp> struct __cw_fixed_value {
+  constexpr __cw_fixed_value(_Tp) : __data() {}
+  _Tp __data;
+};
+template <__cw_fixed_value> struct constant_wrapper;
+template <class _Tp>
+concept __constexpr_param = requires { typename constant_wrapper<_Tp::value>; };
+template <__cw_fixed_value _Xp> auto cw = constant_wrapper<_Xp>{};
+struct __cw_operators {
+  template <__constexpr_param _Lp, __constexpr_param _Rp>
+  friend constexpr auto operator==(_Lp, _Rp) -> constant_wrapper<_Rp::value> {
+    return {};
+  }
+};
+template <__cw_fixed_value _Xp> struct constant_wrapper : __cw_operators {
+  static constexpr auto value = _Xp.__data;
+  constexpr operator decltype(value)() { return value; }
+};
+} // namespace std
+void final_phase(auto gathered, auto available) {
+  if constexpr (gathered == available)
+    ;
+}
+void impeccable_underground_planning() {
+  auto gathered_quantity(std::cw<3>), all_available = std::cw<5>;
+  final_phase(gathered_quantity, all_available);
+}
+
+



More information about the cfe-commits mailing list