blob: 46a0bab035b2eeca7ec12b68017130852015c2ca [file] [log] [blame]
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001//===--- CGVtable.cpp - Emit LLVM Code for C++ vtables --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation of virtual tables.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenModule.h"
15#include "CodeGenFunction.h"
Anders Carlssonf942ee02009-11-27 20:47:55 +000016#include "clang/AST/CXXInheritance.h"
Anders Carlsson2bb27f52009-10-11 22:13:54 +000017#include "clang/AST/RecordLayout.h"
Anders Carlssond420a312009-11-26 19:32:45 +000018#include "llvm/ADT/DenseSet.h"
Zhongxing Xu1721ef72009-11-13 05:46:16 +000019#include <cstdio>
Anders Carlsson2bb27f52009-10-11 22:13:54 +000020
21using namespace clang;
22using namespace CodeGen;
23
Anders Carlssond59885022009-11-27 22:21:51 +000024namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +000025class VtableBuilder {
Anders Carlsson2bb27f52009-10-11 22:13:54 +000026public:
27 /// Index_t - Vtable index type.
28 typedef uint64_t Index_t;
29private:
30 std::vector<llvm::Constant *> &methods;
31 std::vector<llvm::Constant *> submethods;
32 llvm::Type *Ptr8Ty;
33 /// Class - The most derived class that this vtable is being built for.
34 const CXXRecordDecl *Class;
Mike Stump83066c82009-11-13 01:54:23 +000035 /// LayoutClass - The most derived class used for virtual base layout
36 /// information.
37 const CXXRecordDecl *LayoutClass;
Mike Stump653d0b92009-11-13 02:13:54 +000038 /// LayoutOffset - The offset for Class in LayoutClass.
39 uint64_t LayoutOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000040 /// BLayout - Layout for the most derived class that this vtable is being
41 /// built for.
42 const ASTRecordLayout &BLayout;
43 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
44 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
45 llvm::Constant *rtti;
46 llvm::LLVMContext &VMContext;
47 CodeGenModule &CGM; // Per-module state.
48 /// Index - Maps a method decl into a vtable index. Useful for virtual
49 /// dispatch codegen.
Anders Carlssonfb4dda42009-11-13 17:08:56 +000050 llvm::DenseMap<GlobalDecl, Index_t> Index;
51 llvm::DenseMap<GlobalDecl, Index_t> VCall;
52 llvm::DenseMap<GlobalDecl, Index_t> VCallOffset;
Mike Stumpcd6f9ed2009-11-06 23:27:42 +000053 // This is the offset to the nearest virtual base
Anders Carlssonfb4dda42009-11-13 17:08:56 +000054 llvm::DenseMap<GlobalDecl, Index_t> NonVirtualOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000055 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stumpbb9ff052009-10-27 23:46:47 +000056
Anders Carlsson323bb042009-11-26 19:54:33 +000057 /// PureVirtualFunction - Points to __cxa_pure_virtual.
58 llvm::Constant *PureVirtualFn;
59
Anders Carlssona84b6e82009-12-04 02:01:07 +000060 /// VtableMethods - A data structure for keeping track of methods in a vtable.
61 /// Can add methods, override methods and iterate in vtable order.
62 class VtableMethods {
63 // MethodToIndexMap - Maps from a global decl to the index it has in the
64 // Methods vector.
65 llvm::DenseMap<GlobalDecl, uint64_t> MethodToIndexMap;
66
67 /// Methods - The methods, in vtable order.
68 typedef llvm::SmallVector<GlobalDecl, 16> MethodsVectorTy;
69 MethodsVectorTy Methods;
70
71 public:
72 /// AddMethod - Add a method to the vtable methods.
73 void AddMethod(GlobalDecl GD) {
74 assert(!MethodToIndexMap.count(GD) &&
75 "Method has already been added!");
76
77 MethodToIndexMap[GD] = Methods.size();
78 Methods.push_back(GD);
79 }
80
81 /// OverrideMethod - Replace a method with another.
82 void OverrideMethod(GlobalDecl OverriddenGD, GlobalDecl GD) {
83 llvm::DenseMap<GlobalDecl, uint64_t>::iterator i
84 = MethodToIndexMap.find(OverriddenGD);
85 assert(i != MethodToIndexMap.end() && "Did not find entry!");
86
87 // Get the index of the old decl.
88 uint64_t Index = i->second;
89
90 // Replace the old decl with the new decl.
91 Methods[Index] = GD;
92
93 // Now remove the old decl from the method to index map.
94 MethodToIndexMap.erase(i);
95
96 // And add the new.
97 MethodToIndexMap[GD] = Index;
98 }
99
100 MethodsVectorTy::size_type size() const {
101 return Methods.size();
102 }
103
104 void clear() {
105 MethodToIndexMap.clear();
106 Methods.clear();
107 }
108
109 GlobalDecl operator[](unsigned Index) const {
110 return Methods[Index];
111 }
112 };
113
114 /// Methods - The vtable methods we're currently building.
115 VtableMethods Methods;
116
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000117 /// Thunk - Represents a single thunk.
118 struct Thunk {
Anders Carlsson80bc5d52009-12-03 02:41:55 +0000119 Thunk() { }
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000120
Anders Carlsson80bc5d52009-12-03 02:41:55 +0000121 Thunk(GlobalDecl GD, const ThunkAdjustment &Adjustment)
122 : GD(GD), Adjustment(Adjustment) { }
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000123
Anders Carlsson0e1e7632009-12-03 02:36:40 +0000124 GlobalDecl GD;
125
Anders Carlssond420a312009-11-26 19:32:45 +0000126 /// Adjustment - The thunk adjustment.
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000127 ThunkAdjustment Adjustment;
128 };
Anders Carlssond420a312009-11-26 19:32:45 +0000129
130 /// Thunks - The thunks in a vtable.
Anders Carlsson29a1f752009-12-03 02:39:59 +0000131 typedef llvm::DenseMap<uint64_t, Thunk> ThunksMapTy;
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000132 ThunksMapTy Thunks;
Anders Carlssond420a312009-11-26 19:32:45 +0000133
134 /// CovariantThunk - Represents a single covariant thunk.
135 struct CovariantThunk {
Anders Carlsson1157e8f2009-12-03 02:20:26 +0000136 CovariantThunk() { }
137
Anders Carlsson9f98f7a2009-12-03 02:34:59 +0000138 CovariantThunk(GlobalDecl GD, CanQualType ReturnType)
139 : GD(GD), ReturnType(ReturnType) { }
Anders Carlsson06c14b62009-12-03 02:16:14 +0000140
Anders Carlssonc38b40a2009-12-03 02:03:29 +0000141 GlobalDecl GD;
142
Anders Carlssond420a312009-11-26 19:32:45 +0000143 /// ReturnType - The return type of the function.
144 CanQualType ReturnType;
145 };
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000146
Anders Carlssond420a312009-11-26 19:32:45 +0000147 /// CovariantThunks - The covariant thunks in a vtable.
Anders Carlsson73295f92009-12-03 02:12:03 +0000148 typedef llvm::DenseMap<uint64_t, CovariantThunk> CovariantThunksMapTy;
Anders Carlssond420a312009-11-26 19:32:45 +0000149 CovariantThunksMapTy CovariantThunks;
150
151 /// PureVirtualMethods - Pure virtual methods.
152 typedef llvm::DenseSet<GlobalDecl> PureVirtualMethodsSetTy;
153 PureVirtualMethodsSetTy PureVirtualMethods;
154
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000155 std::vector<Index_t> VCalls;
Mike Stump2cefe382009-11-12 20:47:57 +0000156
157 typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t;
Mike Stumpcd2b8212009-11-19 20:52:19 +0000158 // subAddressPoints - Used to hold the AddressPoints (offsets) into the built
159 // vtable for use in computing the initializers for the VTT.
160 llvm::DenseMap<CtorVtable_t, int64_t> &subAddressPoints;
Mike Stump2cefe382009-11-12 20:47:57 +0000161
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000162 typedef CXXRecordDecl::method_iterator method_iter;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000163 const bool Extern;
164 const uint32_t LLVMPointerWidth;
165 Index_t extra;
Mike Stump37dbe962009-10-15 02:04:03 +0000166 typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t;
Mike Stumpcd2b8212009-11-19 20:52:19 +0000167 static llvm::DenseMap<CtorVtable_t, int64_t>&
168 AllocAddressPoint(CodeGenModule &cgm, const CXXRecordDecl *l,
169 const CXXRecordDecl *c) {
170 CodeGenModule::AddrMap_t *&oref = cgm.AddressPoints[l];
171 if (oref == 0)
172 oref = new CodeGenModule::AddrMap_t;
173
174 llvm::DenseMap<CtorVtable_t, int64_t> *&ref = (*oref)[c];
175 if (ref == 0)
176 ref = new llvm::DenseMap<CtorVtable_t, int64_t>;
177 return *ref;
178 }
Anders Carlsson323bb042009-11-26 19:54:33 +0000179
180 /// getPureVirtualFn - Return the __cxa_pure_virtual function.
181 llvm::Constant* getPureVirtualFn() {
182 if (!PureVirtualFn) {
183 const llvm::FunctionType *Ty =
184 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
185 /*isVarArg=*/false);
186 PureVirtualFn = wrap(CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual"));
187 }
188
189 return PureVirtualFn;
190 }
191
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000192public:
Mike Stump653d0b92009-11-13 02:13:54 +0000193 VtableBuilder(std::vector<llvm::Constant *> &meth, const CXXRecordDecl *c,
194 const CXXRecordDecl *l, uint64_t lo, CodeGenModule &cgm)
195 : methods(meth), Class(c), LayoutClass(l), LayoutOffset(lo),
Mike Stump83066c82009-11-13 01:54:23 +0000196 BLayout(cgm.getContext().getASTRecordLayout(l)),
Mike Stumpc01c2b82009-12-02 18:57:08 +0000197 rtti(cgm.GenerateRTTIRef(c)), VMContext(cgm.getModule().getContext()),
Anders Carlsson323bb042009-11-26 19:54:33 +0000198 CGM(cgm), PureVirtualFn(0),subAddressPoints(AllocAddressPoint(cgm, l, c)),
Mike Stump1960b202009-11-19 00:49:05 +0000199 Extern(!l->isInAnonymousNamespace()),
Anders Carlsson323bb042009-11-26 19:54:33 +0000200 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000201 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
202 }
203
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000204 llvm::DenseMap<GlobalDecl, Index_t> &getIndex() { return Index; }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000205 llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex()
206 { return VBIndex; }
207
208 llvm::Constant *wrap(Index_t i) {
209 llvm::Constant *m;
210 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
211 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
212 }
213
214 llvm::Constant *wrap(llvm::Constant *m) {
215 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
216 }
217
Mike Stump8a96d3a2009-12-02 19:50:41 +0000218#define D1(x)
219//#define D1(X) do { if (getenv("DEBUG")) { X; } } while (0)
Mike Stump75ce5732009-10-31 20:06:59 +0000220
221 void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset,
Mike Stump28431212009-10-13 22:54:56 +0000222 bool updateVBIndex, Index_t current_vbindex) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000223 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
224 e = RD->bases_end(); i != e; ++i) {
225 const CXXRecordDecl *Base =
226 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump28431212009-10-13 22:54:56 +0000227 Index_t next_vbindex = current_vbindex;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000228 if (i->isVirtual() && !SeenVBase.count(Base)) {
229 SeenVBase.insert(Base);
Mike Stump28431212009-10-13 22:54:56 +0000230 if (updateVBIndex) {
Mike Stump75ce5732009-10-31 20:06:59 +0000231 next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8)
Mike Stump28431212009-10-13 22:54:56 +0000232 - 3*LLVMPointerWidth/8);
233 VBIndex[Base] = next_vbindex;
234 }
Mike Stump75ce5732009-10-31 20:06:59 +0000235 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
236 VCalls.push_back((0?700:0) + BaseOffset);
237 D1(printf(" vbase for %s at %d delta %d most derived %s\n",
238 Base->getNameAsCString(),
239 (int)-VCalls.size()-3, (int)BaseOffset,
240 Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000241 }
Mike Stump28431212009-10-13 22:54:56 +0000242 // We also record offsets for non-virtual bases to closest enclosing
243 // virtual base. We do this so that we don't have to search
244 // for the nearst virtual base class when generating thunks.
245 if (updateVBIndex && VBIndex.count(Base) == 0)
246 VBIndex[Base] = next_vbindex;
Mike Stump75ce5732009-10-31 20:06:59 +0000247 GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000248 }
249 }
250
251 void StartNewTable() {
252 SeenVBase.clear();
253 }
254
Mike Stump8bccbfd2009-10-15 09:30:16 +0000255 Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B,
256 Index_t Offset = 0) {
257
258 if (B == D)
259 return Offset;
260
261 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D);
262 for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(),
263 e = D->bases_end(); i != e; ++i) {
264 const CXXRecordDecl *Base =
265 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
266 int64_t BaseOffset = 0;
267 if (!i->isVirtual())
268 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
269 int64_t o = getNVOffset_1(Base, B, BaseOffset);
270 if (o >= 0)
271 return o;
272 }
273
274 return -1;
275 }
276
277 /// getNVOffset - Returns the non-virtual offset for the given (B) base of the
278 /// derived class D.
279 Index_t getNVOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000280 qD = qD->getPointeeType();
281 qB = qB->getPointeeType();
Mike Stump8bccbfd2009-10-15 09:30:16 +0000282 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
283 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
284 int64_t o = getNVOffset_1(D, B);
285 if (o >= 0)
286 return o;
287
288 assert(false && "FIXME: non-virtual base not found");
289 return 0;
290 }
291
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000292 /// getVbaseOffset - Returns the index into the vtable for the virtual base
293 /// offset for the given (B) virtual base of the derived class D.
294 Index_t getVbaseOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000295 qD = qD->getPointeeType();
296 qB = qB->getPointeeType();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000297 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
298 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
299 if (D != Class)
Eli Friedman03aa2f12009-11-30 01:19:33 +0000300 return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000301 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
302 i = VBIndex.find(B);
303 if (i != VBIndex.end())
304 return i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000305
Mike Stump28431212009-10-13 22:54:56 +0000306 assert(false && "FIXME: Base not found");
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000307 return 0;
308 }
309
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000310 bool OverrideMethod(GlobalDecl GD, llvm::Constant *m,
Mike Stump8bccbfd2009-10-15 09:30:16 +0000311 bool MorallyVirtual, Index_t OverrideOffset,
Anders Carlssonca1bf682009-12-03 01:54:02 +0000312 Index_t Offset, int64_t CurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000313
314 void InstallThunks() {
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000315 for (CovariantThunksMapTy::const_iterator i = CovariantThunks.begin(),
316 e = CovariantThunks.end(); i != e; ++i) {
Anders Carlsson73295f92009-12-03 02:12:03 +0000317 GlobalDecl GD = i->second.GD;
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000318 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
319 if (MD->isPure())
320 continue;
321
Anders Carlsson06c14b62009-12-03 02:16:14 +0000322 uint64_t Index = i->first;
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000323 const CovariantThunk &Thunk = i->second;
Anders Carlsson06c14b62009-12-03 02:16:14 +0000324 assert(Index == VtableBuilder::Index[GD] && "Thunk index mismatch!");
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000325
326 // Check if there is an adjustment for the 'this' pointer.
327 ThunkAdjustment ThisAdjustment;
Anders Carlssonc6089fd2009-12-03 07:30:40 +0000328 ThunksMapTy::iterator it = Thunks.find(Index);
329 if (it != Thunks.end()) {
330 ThisAdjustment = it->second.Adjustment;
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000331
Anders Carlssonc6089fd2009-12-03 07:30:40 +0000332 Thunks.erase(it);
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000333 }
Anders Carlsson657f1392009-12-03 02:32:59 +0000334
335 // Construct the return adjustment.
Anders Carlssondabfa3c2009-12-03 03:28:24 +0000336 QualType DerivedType =
337 MD->getType()->getAs<FunctionType>()->getResultType();
338
339 int64_t NonVirtualAdjustment =
340 getNVOffset(Thunk.ReturnType, DerivedType) / 8;
341
342 int64_t VirtualAdjustment =
343 getVbaseOffset(Thunk.ReturnType, DerivedType);
344
345 ThunkAdjustment ReturnAdjustment(NonVirtualAdjustment, VirtualAdjustment);
Anders Carlsson657f1392009-12-03 02:32:59 +0000346
347 CovariantThunkAdjustment Adjustment(ThisAdjustment, ReturnAdjustment);
Anders Carlsson06c14b62009-12-03 02:16:14 +0000348 submethods[Index] = CGM.BuildCovariantThunk(MD, Extern, Adjustment);
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000349 }
350 CovariantThunks.clear();
Anders Carlssonc38b40a2009-12-03 02:03:29 +0000351
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000352 for (ThunksMapTy::const_iterator i = Thunks.begin(), e = Thunks.end();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000353 i != e; ++i) {
Anders Carlsson80bc5d52009-12-03 02:41:55 +0000354 uint64_t Index = i->first;
355 const Thunk& Thunk = i->second;
356
357 GlobalDecl GD = Thunk.GD;
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000358 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000359 assert(!MD->isPure() && "Can't thunk pure virtual methods!");
Anders Carlsson29a1f752009-12-03 02:39:59 +0000360
361 assert(Index == VtableBuilder::Index[GD] && "Thunk index mismatch!");
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000362
Mike Stumpe2d4a2c2009-12-03 03:47:56 +0000363 submethods[Index] = CGM.BuildThunk(GD, Extern, Thunk.Adjustment);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000364 }
365 Thunks.clear();
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000366
Anders Carlssond420a312009-11-26 19:32:45 +0000367 for (PureVirtualMethodsSetTy::iterator i = PureVirtualMethods.begin(),
368 e = PureVirtualMethods.end(); i != e; ++i) {
369 GlobalDecl GD = *i;
Anders Carlsson323bb042009-11-26 19:54:33 +0000370 submethods[Index[GD]] = getPureVirtualFn();
Mike Stumpbb9ff052009-10-27 23:46:47 +0000371 }
Anders Carlssond420a312009-11-26 19:32:45 +0000372 PureVirtualMethods.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000373 }
374
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000375 llvm::Constant *WrapAddrOf(GlobalDecl GD) {
376 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
377
Anders Carlsson64457732009-11-24 05:08:52 +0000378 const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD);
Mike Stump18e8b472009-10-27 23:36:26 +0000379
Mike Stumpcdeb8002009-12-03 16:55:20 +0000380 return wrap(CGM.GetAddrOfFunction(GD, Ty));
Mike Stump18e8b472009-10-27 23:36:26 +0000381 }
382
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000383 void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset,
384 int64_t CurrentVBaseOffset) {
Mike Stump37dbe962009-10-15 02:04:03 +0000385 for (Path_t::reverse_iterator i = Path->rbegin(),
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000386 e = Path->rend(); i != e; ++i) {
387 const CXXRecordDecl *RD = i->first;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000388 int64_t OverrideOffset = i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000389 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
390 ++mi) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000391 const CXXMethodDecl *MD = *mi;
392
393 if (!MD->isVirtual())
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000394 continue;
395
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000396 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
397 // Override both the complete and the deleting destructor.
398 GlobalDecl CompDtor(DD, Dtor_Complete);
399 OverrideMethod(CompDtor, WrapAddrOf(CompDtor), MorallyVirtual,
400 OverrideOffset, Offset, CurrentVBaseOffset);
401
402 GlobalDecl DeletingDtor(DD, Dtor_Deleting);
403 OverrideMethod(DeletingDtor, WrapAddrOf(DeletingDtor), MorallyVirtual,
404 OverrideOffset, Offset, CurrentVBaseOffset);
405 } else {
406 OverrideMethod(MD, WrapAddrOf(MD), MorallyVirtual, OverrideOffset,
407 Offset, CurrentVBaseOffset);
408 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000409 }
410 }
411 }
412
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000413 void AddMethod(const GlobalDecl GD, bool MorallyVirtual, Index_t Offset,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000414 int64_t CurrentVBaseOffset) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000415 llvm::Constant *m = WrapAddrOf(GD);
Mike Stump18e8b472009-10-27 23:36:26 +0000416
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000417 // If we can find a previously allocated slot for this, reuse it.
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000418 if (OverrideMethod(GD, m, MorallyVirtual, Offset, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000419 CurrentVBaseOffset))
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000420 return;
421
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000422 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
423
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000424 // else allocate a new slot.
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000425 Index[GD] = submethods.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000426 submethods.push_back(m);
Mike Stumpc5a332c2009-11-13 23:45:53 +0000427 D1(printf(" vfn for %s at %d\n", MD->getNameAsString().c_str(),
428 (int)Index[GD]));
Mike Stump375faa82009-10-28 00:35:46 +0000429 if (MD->isPure())
Anders Carlssond420a312009-11-26 19:32:45 +0000430 PureVirtualMethods.insert(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000431 if (MorallyVirtual) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000432 VCallOffset[GD] = Offset/8;
433 Index_t &idx = VCall[GD];
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000434 // Allocate the first one, after that, we reuse the previous one.
435 if (idx == 0) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000436 NonVirtualOffset[GD] = CurrentVBaseOffset/8 - Offset/8;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000437 idx = VCalls.size()+1;
438 VCalls.push_back(0);
Mike Stump75ce5732009-10-31 20:06:59 +0000439 D1(printf(" vcall for %s at %d with delta %d\n",
Mike Stumpc5a332c2009-11-13 23:45:53 +0000440 MD->getNameAsString().c_str(), (int)-VCalls.size()-3, 0));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000441 }
442 }
443 }
444
445 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000446 Index_t Offset, int64_t CurrentVBaseOffset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000447 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000448 ++mi) {
449 const CXXMethodDecl *MD = *mi;
450 if (!MD->isVirtual())
451 continue;
452
453 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
454 // For destructors, add both the complete and the deleting destructor
455 // to the vtable.
456 AddMethod(GlobalDecl(DD, Dtor_Complete), MorallyVirtual, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000457 CurrentVBaseOffset);
Eli Friedman03aa2f12009-11-30 01:19:33 +0000458 AddMethod(GlobalDecl(DD, Dtor_Deleting), MorallyVirtual, Offset,
459 CurrentVBaseOffset);
460 } else
461 AddMethod(MD, MorallyVirtual, Offset, CurrentVBaseOffset);
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000462 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000463 }
464
465 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
466 const CXXRecordDecl *PrimaryBase,
467 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000468 int64_t Offset, int64_t CurrentVBaseOffset,
469 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000470 Path->push_back(std::make_pair(RD, Offset));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000471 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
472 e = RD->bases_end(); i != e; ++i) {
473 if (i->isVirtual())
474 continue;
475 const CXXRecordDecl *Base =
476 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
477 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
478 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
479 StartNewTable();
Mike Stump653d0b92009-11-13 02:13:54 +0000480 GenerateVtableForBase(Base, o, MorallyVirtual, false,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000481 CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000482 }
483 }
Mike Stump37dbe962009-10-15 02:04:03 +0000484 Path->pop_back();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000485 }
486
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000487// #define D(X) do { X; } while (0)
488#define D(X)
489
490 void insertVCalls(int InsertionPoint) {
491 llvm::Constant *e = 0;
Mike Stump75ce5732009-10-31 20:06:59 +0000492 D1(printf("============= combining vbase/vcall\n"));
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000493 D(VCalls.insert(VCalls.begin(), 673));
494 D(VCalls.push_back(672));
Mike Stump8bccbfd2009-10-15 09:30:16 +0000495 methods.insert(methods.begin() + InsertionPoint, VCalls.size(), e);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000496 // The vcalls come first...
497 for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(),
498 e = VCalls.rend();
499 i != e; ++i)
500 methods[InsertionPoint++] = wrap((0?600:0) + *i);
501 VCalls.clear();
Mike Stump9f23a142009-11-10 02:30:51 +0000502 VCall.clear();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000503 }
504
Mike Stump559387f2009-11-13 23:13:20 +0000505 void AddAddressPoints(const CXXRecordDecl *RD, uint64_t Offset,
506 Index_t AddressPoint) {
507 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
508 RD->getNameAsCString(), Class->getNameAsCString(),
509 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stumpcd2b8212009-11-19 20:52:19 +0000510 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Mike Stump559387f2009-11-13 23:13:20 +0000511
512 // Now also add the address point for all our primary bases.
513 while (1) {
514 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
515 RD = Layout.getPrimaryBase();
516 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
517 // FIXME: Double check this.
518 if (RD == 0)
519 break;
520 if (PrimaryBaseWasVirtual &&
521 BLayout.getVBaseClassOffset(RD) != Offset)
522 break;
523 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
524 RD->getNameAsCString(), Class->getNameAsCString(),
525 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stumpcd2b8212009-11-19 20:52:19 +0000526 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Mike Stump559387f2009-11-13 23:13:20 +0000527 }
528 }
529
530
Mike Stump75ce5732009-10-31 20:06:59 +0000531 Index_t end(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
532 const CXXRecordDecl *PrimaryBase, bool PrimaryBaseWasVirtual,
533 bool MorallyVirtual, int64_t Offset, bool ForVirtualBase,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000534 int64_t CurrentVBaseOffset,
Mike Stump75ce5732009-10-31 20:06:59 +0000535 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000536 bool alloc = false;
537 if (Path == 0) {
538 alloc = true;
539 Path = new Path_t;
540 }
541
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000542 StartNewTable();
543 extra = 0;
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000544 bool DeferVCalls = MorallyVirtual || ForVirtualBase;
545 int VCallInsertionPoint = methods.size();
546 if (!DeferVCalls) {
547 insertVCalls(VCallInsertionPoint);
Mike Stump8bccbfd2009-10-15 09:30:16 +0000548 } else
549 // FIXME: just for extra, or for all uses of VCalls.size post this?
550 extra = -VCalls.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000551
Mike Stump653d0b92009-11-13 02:13:54 +0000552 methods.push_back(wrap(-((Offset-LayoutOffset)/8)));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000553 methods.push_back(rtti);
554 Index_t AddressPoint = methods.size();
555
556 InstallThunks();
Mike Stump75ce5732009-10-31 20:06:59 +0000557 D1(printf("============= combining methods\n"));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000558 methods.insert(methods.end(), submethods.begin(), submethods.end());
559 submethods.clear();
560
561 // and then the non-virtual bases.
562 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000563 MorallyVirtual, Offset, CurrentVBaseOffset, Path);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000564
565 if (ForVirtualBase) {
Mike Stump2cefe382009-11-12 20:47:57 +0000566 // FIXME: We're adding to VCalls in callers, we need to do the overrides
567 // in the inner part, so that we know the complete set of vcalls during
568 // the build and don't have to insert into methods. Saving out the
569 // AddressPoint here, would need to be fixed, if we didn't do that. Also
570 // retroactively adding vcalls for overrides later wind up in the wrong
571 // place, the vcall slot has to be alloted during the walk of the base
572 // when the function is first introduces.
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000573 AddressPoint += VCalls.size();
Mike Stump2cefe382009-11-12 20:47:57 +0000574 insertVCalls(VCallInsertionPoint);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000575 }
576
Mike Stump559387f2009-11-13 23:13:20 +0000577 AddAddressPoints(RD, Offset, AddressPoint);
Mike Stump2cefe382009-11-12 20:47:57 +0000578
Mike Stump37dbe962009-10-15 02:04:03 +0000579 if (alloc) {
580 delete Path;
581 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000582 return AddressPoint;
583 }
584
Mike Stump75ce5732009-10-31 20:06:59 +0000585 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
586 bool updateVBIndex, Index_t current_vbindex,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000587 int64_t CurrentVBaseOffset) {
Mike Stump75ce5732009-10-31 20:06:59 +0000588 if (!RD->isDynamicClass())
589 return;
590
591 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
592 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
593 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
594
595 // vtables are composed from the chain of primaries.
596 if (PrimaryBase) {
597 D1(printf(" doing primaries for %s most derived %s\n",
598 RD->getNameAsCString(), Class->getNameAsCString()));
599
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000600 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
601 if (PrimaryBaseWasVirtual)
602 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
603
Mike Stump75ce5732009-10-31 20:06:59 +0000604 if (!PrimaryBaseWasVirtual)
605 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000606 updateVBIndex, current_vbindex, BaseCurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000607 }
608
609 D1(printf(" doing vcall entries for %s most derived %s\n",
610 RD->getNameAsCString(), Class->getNameAsCString()));
611
612 // And add the virtuals for the class to the primary vtable.
Eli Friedman03aa2f12009-11-30 01:19:33 +0000613 AddMethods(RD, MorallyVirtual, Offset, CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000614 }
615
616 void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
617 bool updateVBIndex, Index_t current_vbindex,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000618 bool RDisVirtualBase, int64_t CurrentVBaseOffset,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000619 bool bottom) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000620 if (!RD->isDynamicClass())
621 return;
622
623 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
624 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
625 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
626
627 // vtables are composed from the chain of primaries.
628 if (PrimaryBase) {
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000629 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
630 if (PrimaryBaseWasVirtual) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000631 IndirectPrimary.insert(PrimaryBase);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000632 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
633 }
Mike Stump75ce5732009-10-31 20:06:59 +0000634
635 D1(printf(" doing primaries for %s most derived %s\n",
636 RD->getNameAsCString(), Class->getNameAsCString()));
637
638 VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000639 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000640 BaseCurrentVBaseOffset, false);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000641 }
642
Mike Stump75ce5732009-10-31 20:06:59 +0000643 D1(printf(" doing vbase entries for %s most derived %s\n",
644 RD->getNameAsCString(), Class->getNameAsCString()));
645 GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex);
646
647 if (RDisVirtualBase || bottom) {
648 Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000649 CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000650 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000651 }
652
Mike Stump653d0b92009-11-13 02:13:54 +0000653 int64_t GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0,
654 bool MorallyVirtual = false,
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000655 bool ForVirtualBase = false,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000656 int CurrentVBaseOffset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +0000657 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000658 if (!RD->isDynamicClass())
659 return 0;
660
Mike Stumpfa818082009-11-13 02:35:38 +0000661 // Construction vtable don't need parts that have no virtual bases and
662 // aren't morally virtual.
663 if ((LayoutClass != Class) && RD->getNumVBases() == 0 && !MorallyVirtual)
664 return 0;
665
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000666 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
667 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
668 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
669
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000670 extra = 0;
Mike Stump75ce5732009-10-31 20:06:59 +0000671 D1(printf("building entries for base %s most derived %s\n",
672 RD->getNameAsCString(), Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000673
Mike Stump75ce5732009-10-31 20:06:59 +0000674 if (ForVirtualBase)
675 extra = VCalls.size();
676
677 VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0, ForVirtualBase,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000678 CurrentVBaseOffset, true);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000679
680 if (Path)
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000681 OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000682
Mike Stump75ce5732009-10-31 20:06:59 +0000683 return end(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000684 Offset, ForVirtualBase, CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000685 }
686
687 void GenerateVtableForVBases(const CXXRecordDecl *RD,
688 int64_t Offset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +0000689 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000690 bool alloc = false;
691 if (Path == 0) {
692 alloc = true;
Mike Stump37dbe962009-10-15 02:04:03 +0000693 Path = new Path_t;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000694 }
695 // FIXME: We also need to override using all paths to a virtual base,
696 // right now, we just process the first path
697 Path->push_back(std::make_pair(RD, Offset));
698 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
699 e = RD->bases_end(); i != e; ++i) {
700 const CXXRecordDecl *Base =
701 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
702 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
703 // Mark it so we don't output it twice.
704 IndirectPrimary.insert(Base);
705 StartNewTable();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000706 VCall.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000707 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000708 int64_t CurrentVBaseOffset = BaseOffset;
Mike Stump75ce5732009-10-31 20:06:59 +0000709 D1(printf("vtable %s virtual base %s\n",
710 Class->getNameAsCString(), Base->getNameAsCString()));
Mike Stump653d0b92009-11-13 02:13:54 +0000711 GenerateVtableForBase(Base, BaseOffset, true, true, CurrentVBaseOffset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000712 Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000713 }
Mike Stump2cefe382009-11-12 20:47:57 +0000714 int64_t BaseOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000715 if (i->isVirtual())
716 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump2cefe382009-11-12 20:47:57 +0000717 else {
718 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
719 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
720 }
721
Mike Stump37dbe962009-10-15 02:04:03 +0000722 if (Base->getNumVBases()) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000723 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump37dbe962009-10-15 02:04:03 +0000724 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000725 }
726 Path->pop_back();
727 if (alloc)
728 delete Path;
729 }
730};
Anders Carlssonca1bf682009-12-03 01:54:02 +0000731} // end anonymous namespace
732
Anders Carlsson657f1392009-12-03 02:32:59 +0000733/// TypeConversionRequiresAdjustment - Returns whether conversion from a
734/// derived type to a base type requires adjustment.
735static bool
736TypeConversionRequiresAdjustment(ASTContext &Ctx,
737 const CXXRecordDecl *DerivedDecl,
738 const CXXRecordDecl *BaseDecl) {
739 CXXBasePaths Paths(/*FindAmbiguities=*/false,
740 /*RecordPaths=*/true, /*DetectVirtual=*/true);
741 if (!const_cast<CXXRecordDecl *>(DerivedDecl)->
742 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseDecl), Paths)) {
743 assert(false && "Class must be derived from the passed in base class!");
744 return false;
745 }
746
747 // If we found a virtual base we always want to require adjustment.
748 if (Paths.getDetectedVirtual())
749 return true;
750
751 const CXXBasePath &Path = Paths.front();
752
753 for (size_t Start = 0, End = Path.size(); Start != End; ++Start) {
754 const CXXBasePathElement &Element = Path[Start];
755
756 // Check the base class offset.
757 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Element.Class);
758
759 const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>();
760 const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl());
761
762 if (Layout.getBaseClassOffset(Base) != 0) {
763 // This requires an adjustment.
764 return true;
765 }
766 }
767
768 return false;
769}
770
771static bool
772TypeConversionRequiresAdjustment(ASTContext &Ctx,
773 QualType DerivedType, QualType BaseType) {
774 // Canonicalize the types.
775 QualType CanDerivedType = Ctx.getCanonicalType(DerivedType);
776 QualType CanBaseType = Ctx.getCanonicalType(BaseType);
777
778 assert(CanDerivedType->getTypeClass() == CanBaseType->getTypeClass() &&
779 "Types must have same type class!");
780
781 if (CanDerivedType == CanBaseType) {
782 // No adjustment needed.
783 return false;
784 }
785
786 if (const ReferenceType *RT = dyn_cast<ReferenceType>(CanDerivedType)) {
787 CanDerivedType = RT->getPointeeType();
788 CanBaseType = cast<ReferenceType>(CanBaseType)->getPointeeType();
789 } else if (const PointerType *PT = dyn_cast<PointerType>(CanDerivedType)) {
790 CanDerivedType = PT->getPointeeType();
791 CanBaseType = cast<PointerType>(CanBaseType)->getPointeeType();
792 } else {
793 assert(false && "Unexpected return type!");
794 }
795
796 if (CanDerivedType == CanBaseType) {
797 // No adjustment needed.
798 return false;
799 }
800
801 const CXXRecordDecl *DerivedDecl =
Anders Carlssondabfa3c2009-12-03 03:28:24 +0000802 cast<CXXRecordDecl>(cast<RecordType>(CanDerivedType)->getDecl());
Anders Carlsson657f1392009-12-03 02:32:59 +0000803
804 const CXXRecordDecl *BaseDecl =
805 cast<CXXRecordDecl>(cast<RecordType>(CanBaseType)->getDecl());
806
807 return TypeConversionRequiresAdjustment(Ctx, DerivedDecl, BaseDecl);
808}
809
Anders Carlssonca1bf682009-12-03 01:54:02 +0000810bool VtableBuilder::OverrideMethod(GlobalDecl GD, llvm::Constant *m,
811 bool MorallyVirtual, Index_t OverrideOffset,
812 Index_t Offset, int64_t CurrentVBaseOffset) {
813 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
814
815 const bool isPure = MD->isPure();
816 typedef CXXMethodDecl::method_iterator meth_iter;
817 // FIXME: Should OverrideOffset's be Offset?
818
819 // FIXME: Don't like the nested loops. For very large inheritance
820 // heirarchies we could have a table on the side with the final overridder
821 // and just replace each instance of an overridden method once. Would be
822 // nice to measure the cost/benefit on real code.
823
824 for (meth_iter mi = MD->begin_overridden_methods(),
825 e = MD->end_overridden_methods();
826 mi != e; ++mi) {
827 GlobalDecl OGD;
828
829 const CXXMethodDecl *OMD = *mi;
830 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(OMD))
831 OGD = GlobalDecl(DD, GD.getDtorType());
832 else
833 OGD = OMD;
834
835 llvm::Constant *om;
836 om = WrapAddrOf(OGD);
837 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
838
839 for (Index_t i = 0, e = submethods.size();
840 i != e; ++i) {
841 // FIXME: begin_overridden_methods might be too lax, covariance */
842 if (submethods[i] != om)
843 continue;
Anders Carlsson657f1392009-12-03 02:32:59 +0000844
845 QualType ReturnType =
846 MD->getType()->getAs<FunctionType>()->getResultType();
847 QualType OverriddenReturnType =
848 OMD->getType()->getAs<FunctionType>()->getResultType();
849
850 // Check if we need a return type adjustment.
851 if (TypeConversionRequiresAdjustment(CGM.getContext(), ReturnType,
852 OverriddenReturnType)) {
853 CovariantThunk &Adjustment = CovariantThunks[i];
Anders Carlsson2ca285f2009-12-03 02:22:59 +0000854
Anders Carlsson657f1392009-12-03 02:32:59 +0000855 // Get the canonical return type.
856 CanQualType CanReturnType =
857 CGM.getContext().getCanonicalType(ReturnType);
858
859 // Insert the base return type.
860 if (Adjustment.ReturnType.isNull())
861 Adjustment.ReturnType =
862 CGM.getContext().getCanonicalType(OverriddenReturnType);
863
864 Adjustment.GD = GD;
Anders Carlssonca1bf682009-12-03 01:54:02 +0000865 }
Anders Carlsson657f1392009-12-03 02:32:59 +0000866
Anders Carlssonca1bf682009-12-03 01:54:02 +0000867 Index[GD] = i;
868 submethods[i] = m;
869 if (isPure)
870 PureVirtualMethods.insert(GD);
871 PureVirtualMethods.erase(OGD);
Anders Carlsson29a1f752009-12-03 02:39:59 +0000872 Thunks.erase(i);
Anders Carlssonca1bf682009-12-03 01:54:02 +0000873 if (MorallyVirtual || VCall.count(OGD)) {
874 Index_t &idx = VCall[OGD];
875 if (idx == 0) {
876 NonVirtualOffset[GD] = -OverrideOffset/8 + CurrentVBaseOffset/8;
877 VCallOffset[GD] = OverrideOffset/8;
878 idx = VCalls.size()+1;
879 VCalls.push_back(0);
880 D1(printf(" vcall for %s at %d with delta %d most derived %s\n",
881 MD->getNameAsString().c_str(), (int)-idx-3,
882 (int)VCalls[idx-1], Class->getNameAsCString()));
883 } else {
884 NonVirtualOffset[GD] = NonVirtualOffset[OGD];
885 VCallOffset[GD] = VCallOffset[OGD];
886 VCalls[idx-1] = -VCallOffset[OGD] + OverrideOffset/8;
887 D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n",
888 MD->getNameAsString().c_str(), (int)-idx-3,
889 (int)VCalls[idx-1], Class->getNameAsCString()));
890 }
891 VCall[GD] = idx;
892 int64_t NonVirtualAdjustment = NonVirtualOffset[GD];
893 int64_t VirtualAdjustment =
894 -((idx + extra + 2) * LLVMPointerWidth / 8);
895
896 // Optimize out virtual adjustments of 0.
897 if (VCalls[idx-1] == 0)
898 VirtualAdjustment = 0;
899
900 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment,
901 VirtualAdjustment);
902
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000903 if (!isPure && !ThisAdjustment.isEmpty())
Anders Carlsson80bc5d52009-12-03 02:41:55 +0000904 Thunks[i] = Thunk(GD, ThisAdjustment);
Anders Carlssonca1bf682009-12-03 01:54:02 +0000905 return true;
906 }
907
908 // FIXME: finish off
909 int64_t NonVirtualAdjustment = VCallOffset[OGD] - OverrideOffset/8;
910
Anders Carlsson2ca285f2009-12-03 02:22:59 +0000911 if (NonVirtualAdjustment) {
Anders Carlssonca1bf682009-12-03 01:54:02 +0000912 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, 0);
913
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000914 if (!isPure)
Anders Carlsson80bc5d52009-12-03 02:41:55 +0000915 Thunks[i] = Thunk(GD, ThisAdjustment);
Anders Carlssonca1bf682009-12-03 01:54:02 +0000916 }
917 return true;
918 }
919 }
920
921 return false;
Anders Carlssond59885022009-11-27 22:21:51 +0000922}
923
Anders Carlssonf942ee02009-11-27 20:47:55 +0000924void CGVtableInfo::ComputeMethodVtableIndices(const CXXRecordDecl *RD) {
925
926 // Itanium C++ ABI 2.5.2:
927 // The order of the virtual function pointers in a virtual table is the
928 // order of declaration of the corresponding member functions in the class.
929 //
930 // There is an entry for any virtual function declared in a class,
931 // whether it is a new function or overrides a base class function,
932 // unless it overrides a function from the primary base, and conversion
933 // between their return types does not require an adjustment.
934
935 int64_t CurrentIndex = 0;
936
937 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
938 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
939
940 if (PrimaryBase) {
Anders Carlssonc920fa22009-11-30 19:43:26 +0000941 assert(PrimaryBase->isDefinition() &&
942 "Should have the definition decl of the primary base!");
Anders Carlssonf942ee02009-11-27 20:47:55 +0000943
944 // Since the record decl shares its vtable pointer with the primary base
945 // we need to start counting at the end of the primary base's vtable.
946 CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase);
947 }
948
949 const CXXDestructorDecl *ImplicitVirtualDtor = 0;
950
951 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
952 e = RD->method_end(); i != e; ++i) {
953 const CXXMethodDecl *MD = *i;
954
955 // We only want virtual methods.
956 if (!MD->isVirtual())
957 continue;
958
959 bool ShouldAddEntryForMethod = true;
960
961 // Check if this method overrides a method in the primary base.
962 for (CXXMethodDecl::method_iterator i = MD->begin_overridden_methods(),
963 e = MD->end_overridden_methods(); i != e; ++i) {
964 const CXXMethodDecl *OverriddenMD = *i;
965 const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent();
966 assert(OverriddenMD->isCanonicalDecl() &&
967 "Should have the canonical decl of the overridden RD!");
968
969 if (OverriddenRD == PrimaryBase) {
970 // Check if converting from the return type of the method to the
971 // return type of the overridden method requires conversion.
972 QualType ReturnType =
973 MD->getType()->getAs<FunctionType>()->getResultType();
974 QualType OverriddenReturnType =
975 OverriddenMD->getType()->getAs<FunctionType>()->getResultType();
976
977 if (!TypeConversionRequiresAdjustment(CGM.getContext(),
978 ReturnType, OverriddenReturnType)) {
979 // This index is shared between the index in the vtable of the primary
980 // base class.
981 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
982 const CXXDestructorDecl *OverriddenDD =
983 cast<CXXDestructorDecl>(OverriddenMD);
984
985 // Add both the complete and deleting entries.
986 MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] =
987 getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Complete));
988 MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] =
989 getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting));
990 } else {
991 MethodVtableIndices[MD] = getMethodVtableIndex(OverriddenMD);
992 }
993
994 // We don't need to add an entry for this method.
995 ShouldAddEntryForMethod = false;
996 break;
997 }
998 }
999 }
1000
1001 if (!ShouldAddEntryForMethod)
1002 continue;
1003
1004 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1005 if (MD->isImplicit()) {
1006 assert(!ImplicitVirtualDtor &&
1007 "Did already see an implicit virtual dtor!");
1008 ImplicitVirtualDtor = DD;
1009 continue;
1010 }
1011
1012 // Add the complete dtor.
1013 MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++;
1014
1015 // Add the deleting dtor.
1016 MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++;
1017 } else {
1018 // Add the entry.
1019 MethodVtableIndices[MD] = CurrentIndex++;
1020 }
1021 }
1022
1023 if (ImplicitVirtualDtor) {
1024 // Itanium C++ ABI 2.5.2:
1025 // If a class has an implicitly-defined virtual destructor,
1026 // its entries come after the declared virtual function pointers.
1027
1028 // Add the complete dtor.
1029 MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] =
1030 CurrentIndex++;
1031
1032 // Add the deleting dtor.
1033 MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] =
1034 CurrentIndex++;
1035 }
1036
1037 NumVirtualFunctionPointers[RD] = CurrentIndex;
1038}
1039
1040uint64_t CGVtableInfo::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) {
1041 llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
1042 NumVirtualFunctionPointers.find(RD);
1043 if (I != NumVirtualFunctionPointers.end())
1044 return I->second;
1045
1046 ComputeMethodVtableIndices(RD);
1047
1048 I = NumVirtualFunctionPointers.find(RD);
1049 assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!");
1050 return I->second;
1051}
1052
1053uint64_t CGVtableInfo::getMethodVtableIndex(GlobalDecl GD) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001054 MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001055 if (I != MethodVtableIndices.end())
1056 return I->second;
1057
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001058 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
Anders Carlssonf942ee02009-11-27 20:47:55 +00001059
1060 ComputeMethodVtableIndices(RD);
1061
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001062 I = MethodVtableIndices.find(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001063 assert(I != MethodVtableIndices.end() && "Did not find index!");
1064 return I->second;
1065}
1066
1067int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
1068 const CXXRecordDecl *VBase) {
1069 ClassPairTy ClassPair(RD, VBase);
1070
1071 VirtualBaseClassIndiciesTy::iterator I =
1072 VirtualBaseClassIndicies.find(ClassPair);
1073 if (I != VirtualBaseClassIndicies.end())
1074 return I->second;
1075
1076 std::vector<llvm::Constant *> methods;
1077 // FIXME: This seems expensive. Can we do a partial job to get
1078 // just this data.
Mike Stump653d0b92009-11-13 02:13:54 +00001079 VtableBuilder b(methods, RD, RD, 0, CGM);
Mike Stump75ce5732009-10-31 20:06:59 +00001080 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001081 b.GenerateVtableForBase(RD);
1082 b.GenerateVtableForVBases(RD);
1083
1084 for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
1085 b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) {
1086 // Insert all types.
1087 ClassPairTy ClassPair(RD, I->first);
1088
1089 VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second));
1090 }
1091
1092 I = VirtualBaseClassIndicies.find(ClassPair);
1093 assert(I != VirtualBaseClassIndicies.end() && "Did not find index!");
1094
1095 return I->second;
1096}
1097
Mike Stump2cefe382009-11-12 20:47:57 +00001098llvm::Constant *CodeGenModule::GenerateVtable(const CXXRecordDecl *LayoutClass,
1099 const CXXRecordDecl *RD,
Mike Stumpeac45592009-11-11 20:26:26 +00001100 uint64_t Offset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001101 llvm::SmallString<256> OutName;
Mike Stump2cefe382009-11-12 20:47:57 +00001102 if (LayoutClass != RD)
Daniel Dunbare128dd12009-11-21 09:06:22 +00001103 getMangleContext().mangleCXXCtorVtable(LayoutClass, Offset/8, RD, OutName);
Mike Stumpeac45592009-11-11 20:26:26 +00001104 else
Daniel Dunbare128dd12009-11-21 09:06:22 +00001105 getMangleContext().mangleCXXVtable(RD, OutName);
1106 llvm::StringRef Name = OutName.str();
Benjamin Kramerbb0a07b2009-10-11 22:57:54 +00001107
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001108 std::vector<llvm::Constant *> methods;
1109 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1110 int64_t AddressPoint;
1111
Mike Stumpaa51ad62009-11-19 04:04:36 +00001112 llvm::GlobalVariable *GV = getModule().getGlobalVariable(Name);
Mike Stumpcd2b8212009-11-19 20:52:19 +00001113 if (GV && AddressPoints[LayoutClass] && !GV->isDeclaration()) {
Mike Stumpaa51ad62009-11-19 04:04:36 +00001114 AddressPoint=(*(*(AddressPoints[LayoutClass]))[RD])[std::make_pair(RD,
1115 Offset)];
Mike Stumpcd2b8212009-11-19 20:52:19 +00001116 // FIXME: We can never have 0 address point. Do this for now so gepping
1117 // retains the same structure. Later, we'll just assert.
1118 if (AddressPoint == 0)
1119 AddressPoint = 1;
1120 } else {
Mike Stumpaa51ad62009-11-19 04:04:36 +00001121 VtableBuilder b(methods, RD, LayoutClass, Offset, *this);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001122
Mike Stumpaa51ad62009-11-19 04:04:36 +00001123 D1(printf("vtable %s\n", RD->getNameAsCString()));
1124 // First comes the vtables for all the non-virtual bases...
1125 AddressPoint = b.GenerateVtableForBase(RD, Offset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001126
Mike Stumpaa51ad62009-11-19 04:04:36 +00001127 // then the vtables for all the virtual bases.
1128 b.GenerateVtableForVBases(RD, Offset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001129
Mike Stumpaa51ad62009-11-19 04:04:36 +00001130 bool CreateDefinition = true;
1131 if (LayoutClass != RD)
1132 CreateDefinition = true;
1133 else {
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001134 const ASTRecordLayout &Layout =
1135 getContext().getASTRecordLayout(LayoutClass);
1136
1137 if (const CXXMethodDecl *KeyFunction = Layout.getKeyFunction()) {
Mike Stumpaa51ad62009-11-19 04:04:36 +00001138 if (!KeyFunction->getBody()) {
1139 // If there is a KeyFunction, and it isn't defined, just build a
1140 // reference to the vtable.
1141 CreateDefinition = false;
1142 }
1143 }
1144 }
1145
1146 llvm::Constant *C = 0;
1147 llvm::Type *type = Ptr8Ty;
1148 llvm::GlobalVariable::LinkageTypes linktype
1149 = llvm::GlobalValue::ExternalLinkage;
1150 if (CreateDefinition) {
1151 llvm::ArrayType *ntype = llvm::ArrayType::get(Ptr8Ty, methods.size());
1152 C = llvm::ConstantArray::get(ntype, methods);
1153 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
1154 if (LayoutClass->isInAnonymousNamespace())
1155 linktype = llvm::GlobalValue::InternalLinkage;
1156 type = ntype;
1157 }
1158 llvm::GlobalVariable *OGV = GV;
1159 GV = new llvm::GlobalVariable(getModule(), type, true, linktype, C, Name);
1160 if (OGV) {
1161 GV->takeName(OGV);
1162 llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
1163 OGV->getType());
1164 OGV->replaceAllUsesWith(NewPtr);
1165 OGV->eraseFromParent();
1166 }
1167 bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden;
1168 if (Hidden)
1169 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
1170 }
Mike Stumpc0f632d2009-11-18 04:00:48 +00001171 llvm::Constant *vtable = llvm::ConstantExpr::getBitCast(GV, Ptr8Ty);
Mike Stumpd846d082009-11-10 07:44:33 +00001172 llvm::Constant *AddressPointC;
1173 uint32_t LLVMPointerWidth = getContext().Target.getPointerWidth(0);
1174 AddressPointC = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1175 AddressPoint*LLVMPointerWidth/8);
Mike Stump2cefe382009-11-12 20:47:57 +00001176 vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(vtable, &AddressPointC,
1177 1);
Mike Stumpd846d082009-11-10 07:44:33 +00001178
Mike Stumpcd2b8212009-11-19 20:52:19 +00001179 assert(vtable->getType() == Ptr8Ty);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001180 return vtable;
1181}
Mike Stump9f23a142009-11-10 02:30:51 +00001182
Mike Stumpae1b85d2009-12-02 19:07:44 +00001183namespace {
Mike Stump9f23a142009-11-10 02:30:51 +00001184class VTTBuilder {
1185 /// Inits - The list of values built for the VTT.
1186 std::vector<llvm::Constant *> &Inits;
1187 /// Class - The most derived class that this vtable is being built for.
1188 const CXXRecordDecl *Class;
1189 CodeGenModule &CGM; // Per-module state.
Mike Stump8b2d2d02009-11-11 00:35:07 +00001190 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
Mike Stumpc7b9f5e2009-11-11 03:08:24 +00001191 /// BLayout - Layout for the most derived class that this vtable is being
1192 /// built for.
1193 const ASTRecordLayout &BLayout;
Mike Stump83066c82009-11-13 01:54:23 +00001194 CodeGenModule::AddrMap_t &AddressPoints;
Mike Stump2cefe382009-11-12 20:47:57 +00001195 // vtbl - A pointer to the vtable for Class.
1196 llvm::Constant *ClassVtbl;
1197 llvm::LLVMContext &VMContext;
Mike Stump9f23a142009-11-10 02:30:51 +00001198
Mike Stump8677bc22009-11-12 22:56:32 +00001199 /// BuildVtablePtr - Build up a referene to the given secondary vtable
Mike Stump83066c82009-11-13 01:54:23 +00001200 llvm::Constant *BuildVtablePtr(llvm::Constant *vtbl,
1201 const CXXRecordDecl *VtblClass,
1202 const CXXRecordDecl *RD,
Mike Stump8677bc22009-11-12 22:56:32 +00001203 uint64_t Offset) {
1204 int64_t AddressPoint;
Mike Stump83066c82009-11-13 01:54:23 +00001205 AddressPoint = (*AddressPoints[VtblClass])[std::make_pair(RD, Offset)];
Mike Stump2b34bc52009-11-12 23:36:21 +00001206 // FIXME: We can never have 0 address point. Do this for now so gepping
Mike Stumpcd2b8212009-11-19 20:52:19 +00001207 // retains the same structure. Later we'll just assert.
Mike Stump2b34bc52009-11-12 23:36:21 +00001208 if (AddressPoint == 0)
1209 AddressPoint = 1;
Mike Stump83066c82009-11-13 01:54:23 +00001210 D1(printf("XXX address point for %s in %s layout %s at offset %d was %d\n",
1211 RD->getNameAsCString(), VtblClass->getNameAsCString(),
1212 Class->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stump8677bc22009-11-12 22:56:32 +00001213 uint32_t LLVMPointerWidth = CGM.getContext().Target.getPointerWidth(0);
1214 llvm::Constant *init;
1215 init = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1216 AddressPoint*LLVMPointerWidth/8);
1217 init = llvm::ConstantExpr::getInBoundsGetElementPtr(vtbl, &init, 1);
1218 return init;
1219 }
1220
Mike Stump2cefe382009-11-12 20:47:57 +00001221 /// Secondary - Add the secondary vtable pointers to Inits. Offset is the
1222 /// current offset in bits to the object we're working on.
Mike Stump8677bc22009-11-12 22:56:32 +00001223 void Secondary(const CXXRecordDecl *RD, llvm::Constant *vtbl,
Mike Stump83066c82009-11-13 01:54:23 +00001224 const CXXRecordDecl *VtblClass, uint64_t Offset=0,
1225 bool MorallyVirtual=false) {
Mike Stump8b2d2d02009-11-11 00:35:07 +00001226 if (RD->getNumVBases() == 0 && ! MorallyVirtual)
1227 return;
1228
1229 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1230 e = RD->bases_end(); i != e; ++i) {
1231 const CXXRecordDecl *Base =
1232 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1233 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1234 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1235 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1236 bool NonVirtualPrimaryBase;
1237 NonVirtualPrimaryBase = !PrimaryBaseWasVirtual && Base == PrimaryBase;
1238 bool BaseMorallyVirtual = MorallyVirtual | i->isVirtual();
Mike Stumpc7b9f5e2009-11-11 03:08:24 +00001239 uint64_t BaseOffset;
1240 if (!i->isVirtual()) {
1241 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1242 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
1243 } else
1244 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump2b34bc52009-11-12 23:36:21 +00001245 llvm::Constant *subvtbl = vtbl;
Mike Stump83066c82009-11-13 01:54:23 +00001246 const CXXRecordDecl *subVtblClass = VtblClass;
Mike Stump8b2d2d02009-11-11 00:35:07 +00001247 if ((Base->getNumVBases() || BaseMorallyVirtual)
1248 && !NonVirtualPrimaryBase) {
1249 // FIXME: Slightly too many of these for __ZTT8test8_B2
Mike Stump8677bc22009-11-12 22:56:32 +00001250 llvm::Constant *init;
Mike Stump2b34bc52009-11-12 23:36:21 +00001251 if (BaseMorallyVirtual)
Mike Stump83066c82009-11-13 01:54:23 +00001252 init = BuildVtablePtr(vtbl, VtblClass, RD, Offset);
Mike Stump2b34bc52009-11-12 23:36:21 +00001253 else {
Mike Stump8677bc22009-11-12 22:56:32 +00001254 init = CGM.getVtableInfo().getCtorVtable(Class, Base, BaseOffset);
Mike Stump2b34bc52009-11-12 23:36:21 +00001255 subvtbl = dyn_cast<llvm::Constant>(init->getOperand(0));
Mike Stump83066c82009-11-13 01:54:23 +00001256 subVtblClass = Base;
Mike Stump2b34bc52009-11-12 23:36:21 +00001257 }
Mike Stump8677bc22009-11-12 22:56:32 +00001258 Inits.push_back(init);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001259 }
Mike Stump83066c82009-11-13 01:54:23 +00001260 Secondary(Base, subvtbl, subVtblClass, BaseOffset, BaseMorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001261 }
1262 }
1263
Mike Stump2cefe382009-11-12 20:47:57 +00001264 /// BuiltVTT - Add the VTT to Inits. Offset is the offset in bits to the
1265 /// currnet object we're working on.
1266 void BuildVTT(const CXXRecordDecl *RD, uint64_t Offset, bool MorallyVirtual) {
Mike Stump8b2d2d02009-11-11 00:35:07 +00001267 if (RD->getNumVBases() == 0 && !MorallyVirtual)
1268 return;
1269
Mike Stump2cefe382009-11-12 20:47:57 +00001270 llvm::Constant *init;
Mike Stump83066c82009-11-13 01:54:23 +00001271 const CXXRecordDecl *VtblClass;
1272
Mike Stump8b2d2d02009-11-11 00:35:07 +00001273 // First comes the primary virtual table pointer...
Mike Stump83066c82009-11-13 01:54:23 +00001274 if (MorallyVirtual) {
1275 init = BuildVtablePtr(ClassVtbl, Class, RD, Offset);
1276 VtblClass = Class;
1277 } else {
Mike Stump2cefe382009-11-12 20:47:57 +00001278 init = CGM.getVtableInfo().getCtorVtable(Class, RD, Offset);
Mike Stump83066c82009-11-13 01:54:23 +00001279 VtblClass = RD;
1280 }
Mike Stump8677bc22009-11-12 22:56:32 +00001281 llvm::Constant *vtbl = dyn_cast<llvm::Constant>(init->getOperand(0));
Mike Stump2cefe382009-11-12 20:47:57 +00001282 Inits.push_back(init);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001283
1284 // then the secondary VTTs....
Mike Stump2cefe382009-11-12 20:47:57 +00001285 SecondaryVTTs(RD, Offset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001286
1287 // and last the secondary vtable pointers.
Mike Stump83066c82009-11-13 01:54:23 +00001288 Secondary(RD, vtbl, VtblClass, Offset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001289 }
1290
1291 /// SecondaryVTTs - Add the secondary VTTs to Inits. The secondary VTTs are
1292 /// built from each direct non-virtual proper base that requires a VTT in
1293 /// declaration order.
Mike Stump2cefe382009-11-12 20:47:57 +00001294 void SecondaryVTTs(const CXXRecordDecl *RD, uint64_t Offset=0,
1295 bool MorallyVirtual=false) {
Mike Stump8b2d2d02009-11-11 00:35:07 +00001296 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1297 e = RD->bases_end(); i != e; ++i) {
1298 const CXXRecordDecl *Base =
1299 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1300 if (i->isVirtual())
1301 continue;
Mike Stump2cefe382009-11-12 20:47:57 +00001302 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1303 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base);
1304 BuildVTT(Base, BaseOffset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001305 }
1306 }
1307
1308 /// VirtualVTTs - Add the VTT for each proper virtual base in inheritance
1309 /// graph preorder.
1310 void VirtualVTTs(const CXXRecordDecl *RD) {
1311 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1312 e = RD->bases_end(); i != e; ++i) {
1313 const CXXRecordDecl *Base =
1314 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1315 if (i->isVirtual() && !SeenVBase.count(Base)) {
1316 SeenVBase.insert(Base);
Mike Stump2cefe382009-11-12 20:47:57 +00001317 uint64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
1318 BuildVTT(Base, BaseOffset, true);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001319 }
1320 VirtualVTTs(Base);
1321 }
1322 }
Mike Stump9f23a142009-11-10 02:30:51 +00001323public:
1324 VTTBuilder(std::vector<llvm::Constant *> &inits, const CXXRecordDecl *c,
Mike Stumpc7b9f5e2009-11-11 03:08:24 +00001325 CodeGenModule &cgm)
1326 : Inits(inits), Class(c), CGM(cgm),
Mike Stump2cefe382009-11-12 20:47:57 +00001327 BLayout(cgm.getContext().getASTRecordLayout(c)),
Mike Stump83066c82009-11-13 01:54:23 +00001328 AddressPoints(*cgm.AddressPoints[c]),
Mike Stump2cefe382009-11-12 20:47:57 +00001329 VMContext(cgm.getModule().getContext()) {
Mike Stumpd846d082009-11-10 07:44:33 +00001330
Mike Stump8b2d2d02009-11-11 00:35:07 +00001331 // First comes the primary virtual table pointer for the complete class...
Mike Stump2cefe382009-11-12 20:47:57 +00001332 ClassVtbl = CGM.getVtableInfo().getVtable(Class);
1333 Inits.push_back(ClassVtbl);
1334 ClassVtbl = dyn_cast<llvm::Constant>(ClassVtbl->getOperand(0));
1335
Mike Stump8b2d2d02009-11-11 00:35:07 +00001336 // then the secondary VTTs...
1337 SecondaryVTTs(Class);
1338
1339 // then the secondary vtable pointers...
Mike Stump83066c82009-11-13 01:54:23 +00001340 Secondary(Class, ClassVtbl, Class);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001341
1342 // and last, the virtual VTTs.
1343 VirtualVTTs(Class);
Mike Stump9f23a142009-11-10 02:30:51 +00001344 }
1345};
Mike Stumpae1b85d2009-12-02 19:07:44 +00001346}
Mike Stump9f23a142009-11-10 02:30:51 +00001347
Mike Stumpd846d082009-11-10 07:44:33 +00001348llvm::Constant *CodeGenModule::GenerateVTT(const CXXRecordDecl *RD) {
Mike Stumpb4722212009-11-10 19:13:04 +00001349 // Only classes that have virtual bases need a VTT.
1350 if (RD->getNumVBases() == 0)
1351 return 0;
1352
Mike Stump9f23a142009-11-10 02:30:51 +00001353 llvm::SmallString<256> OutName;
Daniel Dunbare128dd12009-11-21 09:06:22 +00001354 getMangleContext().mangleCXXVTT(RD, OutName);
1355 llvm::StringRef Name = OutName.str();
Mike Stump9f23a142009-11-10 02:30:51 +00001356
1357 llvm::GlobalVariable::LinkageTypes linktype;
1358 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
Mike Stumpaa51ad62009-11-19 04:04:36 +00001359 if (RD->isInAnonymousNamespace())
1360 linktype = llvm::GlobalValue::InternalLinkage;
Mike Stump9f23a142009-11-10 02:30:51 +00001361 std::vector<llvm::Constant *> inits;
1362 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1363
Mike Stump9f23a142009-11-10 02:30:51 +00001364 D1(printf("vtt %s\n", RD->getNameAsCString()));
1365
Mike Stump8b2d2d02009-11-11 00:35:07 +00001366 VTTBuilder b(inits, RD, *this);
1367
Mike Stump9f23a142009-11-10 02:30:51 +00001368 llvm::Constant *C;
1369 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, inits.size());
1370 C = llvm::ConstantArray::get(type, inits);
Mike Stumpc0f632d2009-11-18 04:00:48 +00001371 llvm::GlobalVariable *vtt = new llvm::GlobalVariable(getModule(), type, true,
Mike Stumpaa51ad62009-11-19 04:04:36 +00001372 linktype, C, Name);
Mike Stumpc0f632d2009-11-18 04:00:48 +00001373 bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden;
1374 if (Hidden)
1375 vtt->setVisibility(llvm::GlobalVariable::HiddenVisibility);
1376 return llvm::ConstantExpr::getBitCast(vtt, Ptr8Ty);
Mike Stump9f23a142009-11-10 02:30:51 +00001377}
Mike Stumpd846d082009-11-10 07:44:33 +00001378
Mike Stump1a139f82009-11-19 01:08:19 +00001379void CGVtableInfo::GenerateClassData(const CXXRecordDecl *RD) {
1380 Vtables[RD] = CGM.GenerateVtable(RD, RD);
Mike Stumpc01c2b82009-12-02 18:57:08 +00001381 CGM.GenerateRTTI(RD);
Mike Stump1a139f82009-11-19 01:08:19 +00001382 CGM.GenerateVTT(RD);
1383}
1384
Mike Stumpeac45592009-11-11 20:26:26 +00001385llvm::Constant *CGVtableInfo::getVtable(const CXXRecordDecl *RD) {
Mike Stumpd846d082009-11-10 07:44:33 +00001386 llvm::Constant *&vtbl = Vtables[RD];
1387 if (vtbl)
1388 return vtbl;
Mike Stump2cefe382009-11-12 20:47:57 +00001389 vtbl = CGM.GenerateVtable(RD, RD);
Mike Stumpaa51ad62009-11-19 04:04:36 +00001390
1391 bool CreateDefinition = true;
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001392
1393 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1394 if (const CXXMethodDecl *KeyFunction = Layout.getKeyFunction()) {
Mike Stumpaa51ad62009-11-19 04:04:36 +00001395 if (!KeyFunction->getBody()) {
1396 // If there is a KeyFunction, and it isn't defined, just build a
1397 // reference to the vtable.
1398 CreateDefinition = false;
1399 }
1400 }
1401
1402 if (CreateDefinition) {
Mike Stumpc01c2b82009-12-02 18:57:08 +00001403 CGM.GenerateRTTI(RD);
Mike Stumpaa51ad62009-11-19 04:04:36 +00001404 CGM.GenerateVTT(RD);
1405 }
Mike Stumpd846d082009-11-10 07:44:33 +00001406 return vtbl;
1407}
Mike Stumpeac45592009-11-11 20:26:26 +00001408
Mike Stump2cefe382009-11-12 20:47:57 +00001409llvm::Constant *CGVtableInfo::getCtorVtable(const CXXRecordDecl *LayoutClass,
1410 const CXXRecordDecl *RD,
Mike Stumpeac45592009-11-11 20:26:26 +00001411 uint64_t Offset) {
Mike Stump2cefe382009-11-12 20:47:57 +00001412 return CGM.GenerateVtable(LayoutClass, RD, Offset);
Mike Stumpeac45592009-11-11 20:26:26 +00001413}
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001414
1415void CGVtableInfo::MaybeEmitVtable(GlobalDecl GD) {
1416 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1417 const CXXRecordDecl *RD = MD->getParent();
1418
1419 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1420
1421 // Get the key function.
1422 const CXXMethodDecl *KeyFunction = Layout.getKeyFunction();
1423
1424 if (!KeyFunction) {
1425 // If there's no key function, we don't want to emit the vtable here.
1426 return;
1427 }
1428
1429 // Check if we have the key function.
1430 if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl())
1431 return;
1432
1433 // If the key function is a destructor, we only want to emit the vtable once,
1434 // so do it for the complete destructor.
1435 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Complete)
1436 return;
1437
1438 // Emit the data.
1439 GenerateClassData(RD);
1440}
1441