Skip to content

Commit bc8a4ea

Browse files
committed
[clang][Interp][NFC] Move collectBaseOffset() to Context
We will need this outside of ByteCodeExprGen later.
1 parent c2db883 commit bc8a4ea

File tree

4 files changed

+52
-34
lines changed

4 files changed

+52
-34
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
110110
if (!this->visit(SubExpr))
111111
return false;
112112

113-
unsigned DerivedOffset = collectBaseOffset(getRecordTy(CE->getType()),
114-
getRecordTy(SubExpr->getType()));
113+
unsigned DerivedOffset =
114+
collectBaseOffset(CE->getType(), SubExpr->getType());
115115

116116
return this->emitGetPtrBasePop(DerivedOffset, CE);
117117
}
@@ -120,8 +120,8 @@ bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
120120
if (!this->visit(SubExpr))
121121
return false;
122122

123-
unsigned DerivedOffset = collectBaseOffset(getRecordTy(SubExpr->getType()),
124-
getRecordTy(CE->getType()));
123+
unsigned DerivedOffset =
124+
collectBaseOffset(SubExpr->getType(), CE->getType());
125125

126126
return this->emitGetPtrDerivedPop(DerivedOffset, CE);
127127
}
@@ -3529,35 +3529,17 @@ void ByteCodeExprGen<Emitter>::emitCleanup() {
35293529

35303530
template <class Emitter>
35313531
unsigned
3532-
ByteCodeExprGen<Emitter>::collectBaseOffset(const RecordType *BaseType,
3533-
const RecordType *DerivedType) {
3534-
assert(BaseType);
3535-
assert(DerivedType);
3536-
const auto *FinalDecl = cast<CXXRecordDecl>(BaseType->getDecl());
3537-
const RecordDecl *CurDecl = DerivedType->getDecl();
3538-
const Record *CurRecord = getRecord(CurDecl);
3539-
assert(CurDecl && FinalDecl);
3540-
3541-
unsigned OffsetSum = 0;
3542-
for (;;) {
3543-
assert(CurRecord->getNumBases() > 0);
3544-
// One level up
3545-
for (const Record::Base &B : CurRecord->bases()) {
3546-
const auto *BaseDecl = cast<CXXRecordDecl>(B.Decl);
3547-
3548-
if (BaseDecl == FinalDecl || BaseDecl->isDerivedFrom(FinalDecl)) {
3549-
OffsetSum += B.Offset;
3550-
CurRecord = B.R;
3551-
CurDecl = BaseDecl;
3552-
break;
3553-
}
3554-
}
3555-
if (CurDecl == FinalDecl)
3556-
break;
3557-
}
3532+
ByteCodeExprGen<Emitter>::collectBaseOffset(const QualType BaseType,
3533+
const QualType DerivedType) {
3534+
const auto extractRecordDecl = [](QualType Ty) -> const CXXRecordDecl * {
3535+
if (const auto *PT = dyn_cast<PointerType>(Ty))
3536+
return PT->getPointeeType()->getAsCXXRecordDecl();
3537+
return Ty->getAsCXXRecordDecl();
3538+
};
3539+
const CXXRecordDecl *BaseDecl = extractRecordDecl(BaseType);
3540+
const CXXRecordDecl *DerivedDecl = extractRecordDecl(DerivedType);
35583541

3559-
assert(OffsetSum > 0);
3560-
return OffsetSum;
3542+
return Ctx.collectBaseOffset(BaseDecl, DerivedDecl);
35613543
}
35623544

35633545
/// Emit casts from a PrimType to another PrimType.

clang/lib/AST/Interp/ByteCodeExprGen.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ class ByteCodeExprGen : public ConstStmtVisitor<ByteCodeExprGen<Emitter>, bool>,
283283

284284
bool emitRecordDestruction(const Record *R);
285285
bool emitDestruction(const Descriptor *Desc);
286-
unsigned collectBaseOffset(const RecordType *BaseType,
287-
const RecordType *DerivedType);
286+
unsigned collectBaseOffset(const QualType BaseType,
287+
const QualType DerivedType);
288288

289289
protected:
290290
/// Variable to storage mapping.

clang/lib/AST/Interp/Context.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,36 @@ const Function *Context::getOrCreateFunction(const FunctionDecl *FD) {
262262

263263
return Func;
264264
}
265+
266+
unsigned Context::collectBaseOffset(const RecordDecl *BaseDecl,
267+
const RecordDecl *DerivedDecl) const {
268+
assert(BaseDecl);
269+
assert(DerivedDecl);
270+
const auto *FinalDecl = cast<CXXRecordDecl>(BaseDecl);
271+
const RecordDecl *CurDecl = DerivedDecl;
272+
const Record *CurRecord = P->getOrCreateRecord(CurDecl);
273+
assert(CurDecl && FinalDecl);
274+
275+
unsigned OffsetSum = 0;
276+
for (;;) {
277+
assert(CurRecord->getNumBases() > 0);
278+
// One level up
279+
for (const Record::Base &B : CurRecord->bases()) {
280+
const auto *BaseDecl = cast<CXXRecordDecl>(B.Decl);
281+
282+
if (BaseDecl == FinalDecl || BaseDecl->isDerivedFrom(FinalDecl)) {
283+
OffsetSum += B.Offset;
284+
CurRecord = B.R;
285+
CurDecl = BaseDecl;
286+
break;
287+
}
288+
}
289+
if (CurDecl == FinalDecl)
290+
break;
291+
292+
// break;
293+
}
294+
295+
assert(OffsetSum > 0);
296+
return OffsetSum;
297+
}

clang/lib/AST/Interp/Context.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ class Context final {
104104
/// Returns the program. This is only needed for unittests.
105105
Program &getProgram() const { return *P.get(); }
106106

107+
unsigned collectBaseOffset(const RecordDecl *BaseDecl,
108+
const RecordDecl *DerivedDecl) const;
109+
107110
private:
108111
/// Runs a function.
109112
bool Run(State &Parent, const Function *Func, APValue &Result);

0 commit comments

Comments
 (0)