[clang] bc8a4ea - [clang][Interp][NFC] Move collectBaseOffset() to Context
Timm Bäder via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 26 01:22:22 PDT 2024
Author: Timm Bäder
Date: 2024-04-26T10:21:27+02:00
New Revision: bc8a4ea11070d06374b403cd09b771a99cc6ba1a
URL: https://github.com/llvm/llvm-project/commit/bc8a4ea11070d06374b403cd09b771a99cc6ba1a
DIFF: https://github.com/llvm/llvm-project/commit/bc8a4ea11070d06374b403cd09b771a99cc6ba1a.diff
LOG: [clang][Interp][NFC] Move collectBaseOffset() to Context
We will need this outside of ByteCodeExprGen later.
Added:
Modified:
clang/lib/AST/Interp/ByteCodeExprGen.cpp
clang/lib/AST/Interp/ByteCodeExprGen.h
clang/lib/AST/Interp/Context.cpp
clang/lib/AST/Interp/Context.h
Removed:
################################################################################
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 8cd0c198d9a844..bbd2771d37124c 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -110,8 +110,8 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
if (!this->visit(SubExpr))
return false;
- unsigned DerivedOffset = collectBaseOffset(getRecordTy(CE->getType()),
- getRecordTy(SubExpr->getType()));
+ unsigned DerivedOffset =
+ collectBaseOffset(CE->getType(), SubExpr->getType());
return this->emitGetPtrBasePop(DerivedOffset, CE);
}
@@ -120,8 +120,8 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
if (!this->visit(SubExpr))
return false;
- unsigned DerivedOffset = collectBaseOffset(getRecordTy(SubExpr->getType()),
- getRecordTy(CE->getType()));
+ unsigned DerivedOffset =
+ collectBaseOffset(SubExpr->getType(), CE->getType());
return this->emitGetPtrDerivedPop(DerivedOffset, CE);
}
@@ -3529,35 +3529,17 @@ void ByteCodeExprGen<Emitter>::emitCleanup() {
template <class Emitter>
unsigned
-ByteCodeExprGen<Emitter>::collectBaseOffset(const RecordType *BaseType,
- const RecordType *DerivedType) {
- assert(BaseType);
- assert(DerivedType);
- const auto *FinalDecl = cast<CXXRecordDecl>(BaseType->getDecl());
- const RecordDecl *CurDecl = DerivedType->getDecl();
- const Record *CurRecord = getRecord(CurDecl);
- assert(CurDecl && FinalDecl);
-
- unsigned OffsetSum = 0;
- for (;;) {
- assert(CurRecord->getNumBases() > 0);
- // One level up
- for (const Record::Base &B : CurRecord->bases()) {
- const auto *BaseDecl = cast<CXXRecordDecl>(B.Decl);
-
- if (BaseDecl == FinalDecl || BaseDecl->isDerivedFrom(FinalDecl)) {
- OffsetSum += B.Offset;
- CurRecord = B.R;
- CurDecl = BaseDecl;
- break;
- }
- }
- if (CurDecl == FinalDecl)
- break;
- }
+ByteCodeExprGen<Emitter>::collectBaseOffset(const QualType BaseType,
+ const QualType DerivedType) {
+ const auto extractRecordDecl = [](QualType Ty) -> const CXXRecordDecl * {
+ if (const auto *PT = dyn_cast<PointerType>(Ty))
+ return PT->getPointeeType()->getAsCXXRecordDecl();
+ return Ty->getAsCXXRecordDecl();
+ };
+ const CXXRecordDecl *BaseDecl = extractRecordDecl(BaseType);
+ const CXXRecordDecl *DerivedDecl = extractRecordDecl(DerivedType);
- assert(OffsetSum > 0);
- return OffsetSum;
+ return Ctx.collectBaseOffset(BaseDecl, DerivedDecl);
}
/// Emit casts from a PrimType to another PrimType.
diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.h b/clang/lib/AST/Interp/ByteCodeExprGen.h
index 7e9dc8631fc0d3..4a57f76ae5b372 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.h
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.h
@@ -283,8 +283,8 @@ class ByteCodeExprGen : public ConstStmtVisitor<ByteCodeExprGen<Emitter>, bool>,
bool emitRecordDestruction(const Record *R);
bool emitDestruction(const Descriptor *Desc);
- unsigned collectBaseOffset(const RecordType *BaseType,
- const RecordType *DerivedType);
+ unsigned collectBaseOffset(const QualType BaseType,
+ const QualType DerivedType);
protected:
/// Variable to storage mapping.
diff --git a/clang/lib/AST/Interp/Context.cpp b/clang/lib/AST/Interp/Context.cpp
index 274178837bf047..d51a57e5e92eae 100644
--- a/clang/lib/AST/Interp/Context.cpp
+++ b/clang/lib/AST/Interp/Context.cpp
@@ -262,3 +262,36 @@ const Function *Context::getOrCreateFunction(const FunctionDecl *FD) {
return Func;
}
+
+unsigned Context::collectBaseOffset(const RecordDecl *BaseDecl,
+ const RecordDecl *DerivedDecl) const {
+ assert(BaseDecl);
+ assert(DerivedDecl);
+ const auto *FinalDecl = cast<CXXRecordDecl>(BaseDecl);
+ const RecordDecl *CurDecl = DerivedDecl;
+ const Record *CurRecord = P->getOrCreateRecord(CurDecl);
+ assert(CurDecl && FinalDecl);
+
+ unsigned OffsetSum = 0;
+ for (;;) {
+ assert(CurRecord->getNumBases() > 0);
+ // One level up
+ for (const Record::Base &B : CurRecord->bases()) {
+ const auto *BaseDecl = cast<CXXRecordDecl>(B.Decl);
+
+ if (BaseDecl == FinalDecl || BaseDecl->isDerivedFrom(FinalDecl)) {
+ OffsetSum += B.Offset;
+ CurRecord = B.R;
+ CurDecl = BaseDecl;
+ break;
+ }
+ }
+ if (CurDecl == FinalDecl)
+ break;
+
+ // break;
+ }
+
+ assert(OffsetSum > 0);
+ return OffsetSum;
+}
diff --git a/clang/lib/AST/Interp/Context.h b/clang/lib/AST/Interp/Context.h
index 23c439ad8912a7..360e9499d08440 100644
--- a/clang/lib/AST/Interp/Context.h
+++ b/clang/lib/AST/Interp/Context.h
@@ -104,6 +104,9 @@ class Context final {
/// Returns the program. This is only needed for unittests.
Program &getProgram() const { return *P.get(); }
+ unsigned collectBaseOffset(const RecordDecl *BaseDecl,
+ const RecordDecl *DerivedDecl) const;
+
private:
/// Runs a function.
bool Run(State &Parent, const Function *Func, APValue &Result);
More information about the cfe-commits
mailing list