1 //===--- VTTBuilder.cpp - C++ VTT layout builder --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code dealing with generation of the layout of virtual table
13 //===----------------------------------------------------------------------===//
15 #include "clang/AST/VTTBuilder.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/CXXInheritance.h"
18 #include "clang/AST/RecordLayout.h"
19 #include "clang/Basic/TargetInfo.h"
20 #include "llvm/Support/Format.h"
24 using namespace clang;
26 #define DUMP_OVERRIDERS 0
28 VTTBuilder::VTTBuilder(ASTContext &Ctx,
29 const CXXRecordDecl *MostDerivedClass,
30 bool GenerateDefinition)
31 : Ctx(Ctx), MostDerivedClass(MostDerivedClass),
32 MostDerivedClassLayout(Ctx.getASTRecordLayout(MostDerivedClass)),
33 GenerateDefinition(GenerateDefinition) {
35 LayoutVTT(BaseSubobject(MostDerivedClass, CharUnits::Zero()),
36 /*BaseIsVirtual=*/false);
39 void VTTBuilder::AddVTablePointer(BaseSubobject Base, uint64_t VTableIndex,
40 const CXXRecordDecl *VTableClass) {
41 // Store the vtable pointer index if we're generating the primary VTT.
42 if (VTableClass == MostDerivedClass) {
43 assert(!SecondaryVirtualPointerIndices.count(Base) &&
44 "A virtual pointer index already exists for this base subobject!");
45 SecondaryVirtualPointerIndices[Base] = VTTComponents.size();
48 if (!GenerateDefinition) {
49 VTTComponents.push_back(VTTComponent());
53 VTTComponents.push_back(VTTComponent(VTableIndex, Base));
56 void VTTBuilder::LayoutSecondaryVTTs(BaseSubobject Base) {
57 const CXXRecordDecl *RD = Base.getBase();
59 for (const auto &I : RD->bases()) {
60 // Don't layout virtual bases.
64 const CXXRecordDecl *BaseDecl =
65 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
67 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
68 CharUnits BaseOffset = Base.getBaseOffset() +
69 Layout.getBaseClassOffset(BaseDecl);
71 // Layout the VTT for this base.
72 LayoutVTT(BaseSubobject(BaseDecl, BaseOffset), /*BaseIsVirtual=*/false);
77 VTTBuilder::LayoutSecondaryVirtualPointers(BaseSubobject Base,
78 bool BaseIsMorallyVirtual,
80 const CXXRecordDecl *VTableClass,
81 VisitedVirtualBasesSetTy &VBases) {
82 const CXXRecordDecl *RD = Base.getBase();
84 // We're not interested in bases that don't have virtual bases, and not
85 // morally virtual bases.
86 if (!RD->getNumVBases() && !BaseIsMorallyVirtual)
89 for (const auto &I : RD->bases()) {
90 const CXXRecordDecl *BaseDecl =
91 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
93 // Itanium C++ ABI 2.6.2:
94 // Secondary virtual pointers are present for all bases with either
95 // virtual bases or virtual function declarations overridden along a
98 // If the base class is not dynamic, we don't want to add it, nor any
99 // of its base classes.
100 if (!BaseDecl->isDynamicClass())
103 bool BaseDeclIsMorallyVirtual = BaseIsMorallyVirtual;
104 bool BaseDeclIsNonVirtualPrimaryBase = false;
105 CharUnits BaseOffset;
107 // Ignore virtual bases that we've already visited.
108 if (!VBases.insert(BaseDecl).second)
111 BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
112 BaseDeclIsMorallyVirtual = true;
114 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
116 BaseOffset = Base.getBaseOffset() +
117 Layout.getBaseClassOffset(BaseDecl);
119 if (!Layout.isPrimaryBaseVirtual() &&
120 Layout.getPrimaryBase() == BaseDecl)
121 BaseDeclIsNonVirtualPrimaryBase = true;
124 // Itanium C++ ABI 2.6.2:
125 // Secondary virtual pointers: for each base class X which (a) has virtual
126 // bases or is reachable along a virtual path from D, and (b) is not a
127 // non-virtual primary base, the address of the virtual table for X-in-D
128 // or an appropriate construction virtual table.
129 if (!BaseDeclIsNonVirtualPrimaryBase &&
130 (BaseDecl->getNumVBases() || BaseDeclIsMorallyVirtual)) {
131 // Add the vtable pointer.
132 AddVTablePointer(BaseSubobject(BaseDecl, BaseOffset), VTableIndex,
136 // And lay out the secondary virtual pointers for the base class.
137 LayoutSecondaryVirtualPointers(BaseSubobject(BaseDecl, BaseOffset),
138 BaseDeclIsMorallyVirtual, VTableIndex,
139 VTableClass, VBases);
144 VTTBuilder::LayoutSecondaryVirtualPointers(BaseSubobject Base,
145 uint64_t VTableIndex) {
146 VisitedVirtualBasesSetTy VBases;
147 LayoutSecondaryVirtualPointers(Base, /*BaseIsMorallyVirtual=*/false,
148 VTableIndex, Base.getBase(), VBases);
151 void VTTBuilder::LayoutVirtualVTTs(const CXXRecordDecl *RD,
152 VisitedVirtualBasesSetTy &VBases) {
153 for (const auto &I : RD->bases()) {
154 const CXXRecordDecl *BaseDecl =
155 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
157 // Check if this is a virtual base.
159 // Check if we've seen this base before.
160 if (!VBases.insert(BaseDecl).second)
163 CharUnits BaseOffset =
164 MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
166 LayoutVTT(BaseSubobject(BaseDecl, BaseOffset), /*BaseIsVirtual=*/true);
169 // We only need to layout virtual VTTs for this base if it actually has
171 if (BaseDecl->getNumVBases())
172 LayoutVirtualVTTs(BaseDecl, VBases);
176 void VTTBuilder::LayoutVTT(BaseSubobject Base, bool BaseIsVirtual) {
177 const CXXRecordDecl *RD = Base.getBase();
179 // Itanium C++ ABI 2.6.2:
180 // An array of virtual table addresses, called the VTT, is declared for
181 // each class type that has indirect or direct virtual base classes.
182 if (RD->getNumVBases() == 0)
185 bool IsPrimaryVTT = Base.getBase() == MostDerivedClass;
188 // Remember the sub-VTT index.
189 SubVTTIndicies[Base] = VTTComponents.size();
192 uint64_t VTableIndex = VTTVTables.size();
193 VTTVTables.push_back(VTTVTable(Base, BaseIsVirtual));
195 // Add the primary vtable pointer.
196 AddVTablePointer(Base, VTableIndex, RD);
198 // Add the secondary VTTs.
199 LayoutSecondaryVTTs(Base);
201 // Add the secondary virtual pointers.
202 LayoutSecondaryVirtualPointers(Base, VTableIndex);
204 // If this is the primary VTT, we want to lay out virtual VTTs as well.
206 VisitedVirtualBasesSetTy VBases;
207 LayoutVirtualVTTs(Base.getBase(), VBases);