[clang] [clang][bytecode][NFC] Add Pointer::canDeref (PR #179618)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 3 23:15:10 PST 2026
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/179618
None
>From 80fbd3ff1acb2fb4b3509995a871cb9929f67fd2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Wed, 4 Feb 2026 08:07:31 +0100
Subject: [PATCH] [clang][bytecode][NFC] Add Pointer::canDeref
---
clang/lib/AST/ByteCode/Interp.h | 6 ++----
clang/lib/AST/ByteCode/Pointer.cpp | 3 +--
clang/lib/AST/ByteCode/Pointer.h | 9 +++++++++
3 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index d856cd7c0a2d9..3313e819e694b 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -1973,8 +1973,7 @@ bool Load(InterpState &S, CodePtr OpPC) {
return false;
if (!Ptr.isBlockPointer())
return false;
- if (const Descriptor *D = Ptr.getFieldDesc();
- !(D->isPrimitive() || D->isPrimitiveArray()) || D->getPrimType() != Name)
+ if (!Ptr.canDeref(Name))
return false;
S.Stk.push<T>(Ptr.deref<T>());
return true;
@@ -1987,8 +1986,7 @@ bool LoadPop(InterpState &S, CodePtr OpPC) {
return false;
if (!Ptr.isBlockPointer())
return false;
- if (const Descriptor *D = Ptr.getFieldDesc();
- !(D->isPrimitive() || D->isPrimitiveArray()) || D->getPrimType() != Name)
+ if (!Ptr.canDeref(Name))
return false;
S.Stk.push<T>(Ptr.deref<T>());
return true;
diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp
index b625128514f83..fb9202c6d66c8 100644
--- a/clang/lib/AST/ByteCode/Pointer.cpp
+++ b/clang/lib/AST/ByteCode/Pointer.cpp
@@ -947,8 +947,7 @@ std::optional<APValue> Pointer::toRValue(const Context &Ctx,
// Just load primitive types.
if (OptPrimType T = Ctx.classify(ResultType)) {
- if (const Descriptor *D = getFieldDesc();
- (D->isPrimitive() || D->isPrimitiveArray()) && D->getPrimType() != *T)
+ if (!canDeref(*T))
return std::nullopt;
TYPE_SWITCH(*T, return this->deref<T>().toAPValue(ASTCtx));
}
diff --git a/clang/lib/AST/ByteCode/Pointer.h b/clang/lib/AST/ByteCode/Pointer.h
index 7ff1559cd0000..2515b2fe56ab9 100644
--- a/clang/lib/AST/ByteCode/Pointer.h
+++ b/clang/lib/AST/ByteCode/Pointer.h
@@ -666,6 +666,15 @@ class Pointer {
return false;
}
+ /// Checks whether the pointer can be dereferenced to the given PrimType.
+ bool canDeref(PrimType T) const {
+ if (const Descriptor *FieldDesc = getFieldDesc()) {
+ return (FieldDesc->isPrimitive() || FieldDesc->isPrimitiveArray()) &&
+ FieldDesc->getPrimType() == T;
+ }
+ return false;
+ }
+
/// Dereferences the pointer, if it's live.
template <typename T> T &deref() const {
assert(isLive() && "Invalid pointer");
More information about the cfe-commits
mailing list