#include "clang/AST/VTTBuilder.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/BaseSubobject.h"
#include "clang/AST/CharUnits.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/RecordLayout.h"
#include "clang/AST/Type.h"
#include "clang/Basic/LLVM.h"
#include "llvm/Support/Casting.h"
#include <cassert>
#include <cstdint>
using namespace clang;
#define DUMP_OVERRIDERS 0
VTTBuilder::VTTBuilder(ASTContext &Ctx,
const CXXRecordDecl *MostDerivedClass,
bool GenerateDefinition)
: Ctx(Ctx), MostDerivedClass(MostDerivedClass),
MostDerivedClassLayout(Ctx.getASTRecordLayout(MostDerivedClass)),
GenerateDefinition(GenerateDefinition) {
LayoutVTT(BaseSubobject(MostDerivedClass, CharUnits::Zero()),
false);
}
void VTTBuilder::AddVTablePointer(BaseSubobject Base, uint64_t VTableIndex,
const CXXRecordDecl *VTableClass) {
if (VTableClass == MostDerivedClass) {
assert(!SecondaryVirtualPointerIndices.count(Base) &&
"A virtual pointer index already exists for this base subobject!");
SecondaryVirtualPointerIndices[Base] = VTTComponents.size();
}
if (!GenerateDefinition) {
VTTComponents.push_back(VTTComponent());
return;
}
VTTComponents.push_back(VTTComponent(VTableIndex, Base));
}
void VTTBuilder::LayoutSecondaryVTTs(BaseSubobject Base) {
const CXXRecordDecl *RD = Base.getBase();
for (const auto &I : RD->bases()) {
if (I.isVirtual())
continue;
const auto *BaseDecl =
cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
CharUnits BaseOffset = Base.getBaseOffset() +
Layout.getBaseClassOffset(BaseDecl);
LayoutVTT(BaseSubobject(BaseDecl, BaseOffset), false);
}
}
void
VTTBuilder::LayoutSecondaryVirtualPointers(BaseSubobject Base,
bool BaseIsMorallyVirtual,
uint64_t VTableIndex,
const CXXRecordDecl *VTableClass,
VisitedVirtualBasesSetTy &VBases) {
const CXXRecordDecl *RD = Base.getBase();
if (!RD->getNumVBases() && !BaseIsMorallyVirtual)
return;
for (const auto &I : RD->bases()) {
const auto *BaseDecl =
cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
if (!BaseDecl->isDynamicClass())
continue;
bool BaseDeclIsMorallyVirtual = BaseIsMorallyVirtual;
bool BaseDeclIsNonVirtualPrimaryBase = false;
CharUnits BaseOffset;
if (I.isVirtual()) {
if (!VBases.insert(BaseDecl).second)
continue;
BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
BaseDeclIsMorallyVirtual = true;
} else {
const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
BaseOffset = Base.getBaseOffset() +
Layout.getBaseClassOffset(BaseDecl);
if (!Layout.isPrimaryBaseVirtual() &&
Layout.getPrimaryBase() == BaseDecl)
BaseDeclIsNonVirtualPrimaryBase = true;
}
if (!BaseDeclIsNonVirtualPrimaryBase &&
(BaseDecl->getNumVBases() || BaseDeclIsMorallyVirtual)) {
AddVTablePointer(BaseSubobject(BaseDecl, BaseOffset), VTableIndex,
VTableClass);
}
LayoutSecondaryVirtualPointers(BaseSubobject(BaseDecl, BaseOffset),
BaseDeclIsMorallyVirtual, VTableIndex,
VTableClass, VBases);
}
}
void
VTTBuilder::LayoutSecondaryVirtualPointers(BaseSubobject Base,
uint64_t VTableIndex) {
VisitedVirtualBasesSetTy VBases;
LayoutSecondaryVirtualPointers(Base, false,
VTableIndex, Base.getBase(), VBases);
}
void VTTBuilder::LayoutVirtualVTTs(const CXXRecordDecl *RD,
VisitedVirtualBasesSetTy &VBases) {
for (const auto &I : RD->bases()) {
const auto *BaseDecl =
cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
if (I.isVirtual()) {
if (!VBases.insert(BaseDecl).second)
continue;
CharUnits BaseOffset =
MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
LayoutVTT(BaseSubobject(BaseDecl, BaseOffset), true);
}
if (BaseDecl->getNumVBases())
LayoutVirtualVTTs(BaseDecl, VBases);
}
}
void VTTBuilder::LayoutVTT(BaseSubobject Base, bool BaseIsVirtual) {
const CXXRecordDecl *RD = Base.getBase();
if (RD->getNumVBases() == 0)
return;
bool IsPrimaryVTT = Base.getBase() == MostDerivedClass;
if (!IsPrimaryVTT) {
SubVTTIndicies[Base] = VTTComponents.size();
}
uint64_t VTableIndex = VTTVTables.size();
VTTVTables.push_back(VTTVTable(Base, BaseIsVirtual));
AddVTablePointer(Base, VTableIndex, RD);
LayoutSecondaryVTTs(Base);
LayoutSecondaryVirtualPointers(Base, VTableIndex);
if (IsPrimaryVTT) {
VisitedVirtualBasesSetTy VBases;
LayoutVirtualVTTs(Base.getBase(), VBases);
}
}