blob: 53d0ef09f14c578b13debd2de7988875d7502dda [file] [log] [blame]
Eugene Zelenko3d775712017-12-01 22:04:49 +00001//===- VTTBuilder.cpp - C++ VTT layout builder ----------------------------===//
Peter Collingbourne2d259522011-09-26 01:56:24 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Peter Collingbourne2d259522011-09-26 01:56:24 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This contains code dealing with generation of the layout of virtual table
10// tables (VTT).
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/VTTBuilder.h"
Benjamin Kramer2ef30312012-07-04 18:45:14 +000015#include "clang/AST/ASTContext.h"
Eugene Zelenko3d775712017-12-01 22:04:49 +000016#include "clang/AST/BaseSubobject.h"
17#include "clang/AST/CharUnits.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclCXX.h"
Peter Collingbourne2d259522011-09-26 01:56:24 +000020#include "clang/AST/RecordLayout.h"
Eugene Zelenko3d775712017-12-01 22:04:49 +000021#include "clang/AST/Type.h"
22#include "clang/Basic/LLVM.h"
23#include "llvm/Support/Casting.h"
24#include <cassert>
25#include <cstdint>
Peter Collingbourne2d259522011-09-26 01:56:24 +000026
27using namespace clang;
28
29#define DUMP_OVERRIDERS 0
30
31VTTBuilder::VTTBuilder(ASTContext &Ctx,
32 const CXXRecordDecl *MostDerivedClass,
33 bool GenerateDefinition)
Fangrui Song6907ce22018-07-30 19:24:48 +000034 : Ctx(Ctx), MostDerivedClass(MostDerivedClass),
Eugene Zelenko3d775712017-12-01 22:04:49 +000035 MostDerivedClassLayout(Ctx.getASTRecordLayout(MostDerivedClass)),
36 GenerateDefinition(GenerateDefinition) {
Peter Collingbourne2d259522011-09-26 01:56:24 +000037 // Lay out this VTT.
Fangrui Song6907ce22018-07-30 19:24:48 +000038 LayoutVTT(BaseSubobject(MostDerivedClass, CharUnits::Zero()),
Peter Collingbourne2d259522011-09-26 01:56:24 +000039 /*BaseIsVirtual=*/false);
40}
41
42void VTTBuilder::AddVTablePointer(BaseSubobject Base, uint64_t VTableIndex,
43 const CXXRecordDecl *VTableClass) {
44 // Store the vtable pointer index if we're generating the primary VTT.
45 if (VTableClass == MostDerivedClass) {
46 assert(!SecondaryVirtualPointerIndices.count(Base) &&
47 "A virtual pointer index already exists for this base subobject!");
48 SecondaryVirtualPointerIndices[Base] = VTTComponents.size();
49 }
50
51 if (!GenerateDefinition) {
52 VTTComponents.push_back(VTTComponent());
53 return;
54 }
55
56 VTTComponents.push_back(VTTComponent(VTableIndex, Base));
57}
58
59void VTTBuilder::LayoutSecondaryVTTs(BaseSubobject Base) {
60 const CXXRecordDecl *RD = Base.getBase();
61
Eugene Zelenko3d775712017-12-01 22:04:49 +000062 for (const auto &I : RD->bases()) {
Peter Collingbourne2d259522011-09-26 01:56:24 +000063 // Don't layout virtual bases.
Aaron Ballman574705e2014-03-13 15:41:46 +000064 if (I.isVirtual())
Peter Collingbourne2d259522011-09-26 01:56:24 +000065 continue;
66
67 const CXXRecordDecl *BaseDecl =
Aaron Ballman574705e2014-03-13 15:41:46 +000068 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Peter Collingbourne2d259522011-09-26 01:56:24 +000069
70 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
Fangrui Song6907ce22018-07-30 19:24:48 +000071 CharUnits BaseOffset = Base.getBaseOffset() +
Peter Collingbourne2d259522011-09-26 01:56:24 +000072 Layout.getBaseClassOffset(BaseDecl);
Fangrui Song6907ce22018-07-30 19:24:48 +000073
Peter Collingbourne2d259522011-09-26 01:56:24 +000074 // Layout the VTT for this base.
75 LayoutVTT(BaseSubobject(BaseDecl, BaseOffset), /*BaseIsVirtual=*/false);
76 }
77}
78
79void
Fangrui Song6907ce22018-07-30 19:24:48 +000080VTTBuilder::LayoutSecondaryVirtualPointers(BaseSubobject Base,
Peter Collingbourne2d259522011-09-26 01:56:24 +000081 bool BaseIsMorallyVirtual,
82 uint64_t VTableIndex,
83 const CXXRecordDecl *VTableClass,
84 VisitedVirtualBasesSetTy &VBases) {
85 const CXXRecordDecl *RD = Base.getBase();
Fangrui Song6907ce22018-07-30 19:24:48 +000086
Peter Collingbourne2d259522011-09-26 01:56:24 +000087 // We're not interested in bases that don't have virtual bases, and not
88 // morally virtual bases.
89 if (!RD->getNumVBases() && !BaseIsMorallyVirtual)
90 return;
91
Aaron Ballman574705e2014-03-13 15:41:46 +000092 for (const auto &I : RD->bases()) {
Peter Collingbourne2d259522011-09-26 01:56:24 +000093 const CXXRecordDecl *BaseDecl =
Aaron Ballman574705e2014-03-13 15:41:46 +000094 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Peter Collingbourne2d259522011-09-26 01:56:24 +000095
96 // Itanium C++ ABI 2.6.2:
97 // Secondary virtual pointers are present for all bases with either
Fangrui Song6907ce22018-07-30 19:24:48 +000098 // virtual bases or virtual function declarations overridden along a
Peter Collingbourne2d259522011-09-26 01:56:24 +000099 // virtual path.
100 //
101 // If the base class is not dynamic, we don't want to add it, nor any
102 // of its base classes.
103 if (!BaseDecl->isDynamicClass())
104 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +0000105
Peter Collingbourne2d259522011-09-26 01:56:24 +0000106 bool BaseDeclIsMorallyVirtual = BaseIsMorallyVirtual;
107 bool BaseDeclIsNonVirtualPrimaryBase = false;
108 CharUnits BaseOffset;
Aaron Ballman574705e2014-03-13 15:41:46 +0000109 if (I.isVirtual()) {
Peter Collingbourne2d259522011-09-26 01:56:24 +0000110 // Ignore virtual bases that we've already visited.
David Blaikie82e95a32014-11-19 07:49:47 +0000111 if (!VBases.insert(BaseDecl).second)
Peter Collingbourne2d259522011-09-26 01:56:24 +0000112 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +0000113
Peter Collingbourne2d259522011-09-26 01:56:24 +0000114 BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
115 BaseDeclIsMorallyVirtual = true;
116 } else {
117 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(RD);
Fangrui Song6907ce22018-07-30 19:24:48 +0000118
119 BaseOffset = Base.getBaseOffset() +
Peter Collingbourne2d259522011-09-26 01:56:24 +0000120 Layout.getBaseClassOffset(BaseDecl);
Fangrui Song6907ce22018-07-30 19:24:48 +0000121
Peter Collingbourne2d259522011-09-26 01:56:24 +0000122 if (!Layout.isPrimaryBaseVirtual() &&
123 Layout.getPrimaryBase() == BaseDecl)
124 BaseDeclIsNonVirtualPrimaryBase = true;
125 }
126
127 // Itanium C++ ABI 2.6.2:
128 // Secondary virtual pointers: for each base class X which (a) has virtual
129 // bases or is reachable along a virtual path from D, and (b) is not a
130 // non-virtual primary base, the address of the virtual table for X-in-D
131 // or an appropriate construction virtual table.
132 if (!BaseDeclIsNonVirtualPrimaryBase &&
133 (BaseDecl->getNumVBases() || BaseDeclIsMorallyVirtual)) {
134 // Add the vtable pointer.
Fangrui Song6907ce22018-07-30 19:24:48 +0000135 AddVTablePointer(BaseSubobject(BaseDecl, BaseOffset), VTableIndex,
Peter Collingbourne2d259522011-09-26 01:56:24 +0000136 VTableClass);
137 }
138
139 // And lay out the secondary virtual pointers for the base class.
140 LayoutSecondaryVirtualPointers(BaseSubobject(BaseDecl, BaseOffset),
Fangrui Song6907ce22018-07-30 19:24:48 +0000141 BaseDeclIsMorallyVirtual, VTableIndex,
Peter Collingbourne2d259522011-09-26 01:56:24 +0000142 VTableClass, VBases);
143 }
144}
145
Fangrui Song6907ce22018-07-30 19:24:48 +0000146void
147VTTBuilder::LayoutSecondaryVirtualPointers(BaseSubobject Base,
Peter Collingbourne2d259522011-09-26 01:56:24 +0000148 uint64_t VTableIndex) {
149 VisitedVirtualBasesSetTy VBases;
150 LayoutSecondaryVirtualPointers(Base, /*BaseIsMorallyVirtual=*/false,
151 VTableIndex, Base.getBase(), VBases);
152}
153
154void VTTBuilder::LayoutVirtualVTTs(const CXXRecordDecl *RD,
155 VisitedVirtualBasesSetTy &VBases) {
Aaron Ballman574705e2014-03-13 15:41:46 +0000156 for (const auto &I : RD->bases()) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000157 const CXXRecordDecl *BaseDecl =
Aaron Ballman574705e2014-03-13 15:41:46 +0000158 cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Fangrui Song6907ce22018-07-30 19:24:48 +0000159
Peter Collingbourne2d259522011-09-26 01:56:24 +0000160 // Check if this is a virtual base.
Aaron Ballman574705e2014-03-13 15:41:46 +0000161 if (I.isVirtual()) {
Peter Collingbourne2d259522011-09-26 01:56:24 +0000162 // Check if we've seen this base before.
David Blaikie82e95a32014-11-19 07:49:47 +0000163 if (!VBases.insert(BaseDecl).second)
Peter Collingbourne2d259522011-09-26 01:56:24 +0000164 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +0000165
166 CharUnits BaseOffset =
Peter Collingbourne2d259522011-09-26 01:56:24 +0000167 MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
Fangrui Song6907ce22018-07-30 19:24:48 +0000168
Peter Collingbourne2d259522011-09-26 01:56:24 +0000169 LayoutVTT(BaseSubobject(BaseDecl, BaseOffset), /*BaseIsVirtual=*/true);
170 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000171
Peter Collingbourne2d259522011-09-26 01:56:24 +0000172 // We only need to layout virtual VTTs for this base if it actually has
173 // virtual bases.
174 if (BaseDecl->getNumVBases())
175 LayoutVirtualVTTs(BaseDecl, VBases);
176 }
177}
178
179void VTTBuilder::LayoutVTT(BaseSubobject Base, bool BaseIsVirtual) {
180 const CXXRecordDecl *RD = Base.getBase();
181
182 // Itanium C++ ABI 2.6.2:
Fangrui Song6907ce22018-07-30 19:24:48 +0000183 // An array of virtual table addresses, called the VTT, is declared for
Peter Collingbourne2d259522011-09-26 01:56:24 +0000184 // each class type that has indirect or direct virtual base classes.
185 if (RD->getNumVBases() == 0)
186 return;
187
188 bool IsPrimaryVTT = Base.getBase() == MostDerivedClass;
189
190 if (!IsPrimaryVTT) {
191 // Remember the sub-VTT index.
192 SubVTTIndicies[Base] = VTTComponents.size();
193 }
194
195 uint64_t VTableIndex = VTTVTables.size();
196 VTTVTables.push_back(VTTVTable(Base, BaseIsVirtual));
197
198 // Add the primary vtable pointer.
199 AddVTablePointer(Base, VTableIndex, RD);
200
201 // Add the secondary VTTs.
202 LayoutSecondaryVTTs(Base);
Fangrui Song6907ce22018-07-30 19:24:48 +0000203
Peter Collingbourne2d259522011-09-26 01:56:24 +0000204 // Add the secondary virtual pointers.
205 LayoutSecondaryVirtualPointers(Base, VTableIndex);
Fangrui Song6907ce22018-07-30 19:24:48 +0000206
Peter Collingbourne2d259522011-09-26 01:56:24 +0000207 // If this is the primary VTT, we want to lay out virtual VTTs as well.
208 if (IsPrimaryVTT) {
209 VisitedVirtualBasesSetTy VBases;
210 LayoutVirtualVTTs(Base.getBase(), VBases);
211 }
212}