blob: 58d9a552a2312dec30edd3285b303cfc6c23202f [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 Carlsson6d771bc2009-11-26 03:25:13 +000060 /// Thunk - Represents a single thunk.
61 struct Thunk {
62 Thunk()
63 : Index(0) { }
64
65 Thunk(uint64_t Index, const ThunkAdjustment &Adjustment)
66 : Index(Index), Adjustment(Adjustment) { }
67
68 /// Index - The index in the vtable.
69 uint64_t Index;
70
Anders Carlssond420a312009-11-26 19:32:45 +000071 /// Adjustment - The thunk adjustment.
Anders Carlsson6d771bc2009-11-26 03:25:13 +000072 ThunkAdjustment Adjustment;
73 };
Anders Carlssond420a312009-11-26 19:32:45 +000074
75 /// Thunks - The thunks in a vtable.
Anders Carlsson6d771bc2009-11-26 03:25:13 +000076 typedef llvm::DenseMap<GlobalDecl, Thunk> ThunksMapTy;
77 ThunksMapTy Thunks;
Anders Carlssond420a312009-11-26 19:32:45 +000078
79 /// CovariantThunk - Represents a single covariant thunk.
80 struct CovariantThunk {
81 CovariantThunk()
82 : Index(0) { }
83
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +000084 CovariantThunk(uint64_t Index, const ThunkAdjustment &ReturnAdjustment,
Anders Carlssond420a312009-11-26 19:32:45 +000085 CanQualType ReturnType)
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +000086 : Index(Index), ReturnAdjustment(ReturnAdjustment),
Anders Carlssond420a312009-11-26 19:32:45 +000087 ReturnType(ReturnType) { }
88
89 // Index - The index in the vtable.
90 uint64_t Index;
91
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +000092 /// ReturnAdjustment - The covariant thunk return adjustment.
93 ThunkAdjustment ReturnAdjustment;
Anders Carlssond420a312009-11-26 19:32:45 +000094
95 /// ReturnType - The return type of the function.
96 CanQualType ReturnType;
97 };
Anders Carlsson6d771bc2009-11-26 03:25:13 +000098
Anders Carlssond420a312009-11-26 19:32:45 +000099 /// CovariantThunks - The covariant thunks in a vtable.
100 typedef llvm::DenseMap<GlobalDecl, CovariantThunk> CovariantThunksMapTy;
101 CovariantThunksMapTy CovariantThunks;
102
103 /// PureVirtualMethods - Pure virtual methods.
104 typedef llvm::DenseSet<GlobalDecl> PureVirtualMethodsSetTy;
105 PureVirtualMethodsSetTy PureVirtualMethods;
106
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000107 std::vector<Index_t> VCalls;
Mike Stump2cefe382009-11-12 20:47:57 +0000108
109 typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t;
Mike Stumpcd2b8212009-11-19 20:52:19 +0000110 // subAddressPoints - Used to hold the AddressPoints (offsets) into the built
111 // vtable for use in computing the initializers for the VTT.
112 llvm::DenseMap<CtorVtable_t, int64_t> &subAddressPoints;
Mike Stump2cefe382009-11-12 20:47:57 +0000113
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000114 typedef CXXRecordDecl::method_iterator method_iter;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000115 const bool Extern;
116 const uint32_t LLVMPointerWidth;
117 Index_t extra;
Mike Stump37dbe962009-10-15 02:04:03 +0000118 typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t;
Mike Stumpcd2b8212009-11-19 20:52:19 +0000119 static llvm::DenseMap<CtorVtable_t, int64_t>&
120 AllocAddressPoint(CodeGenModule &cgm, const CXXRecordDecl *l,
121 const CXXRecordDecl *c) {
122 CodeGenModule::AddrMap_t *&oref = cgm.AddressPoints[l];
123 if (oref == 0)
124 oref = new CodeGenModule::AddrMap_t;
125
126 llvm::DenseMap<CtorVtable_t, int64_t> *&ref = (*oref)[c];
127 if (ref == 0)
128 ref = new llvm::DenseMap<CtorVtable_t, int64_t>;
129 return *ref;
130 }
Anders Carlsson323bb042009-11-26 19:54:33 +0000131
132 /// getPureVirtualFn - Return the __cxa_pure_virtual function.
133 llvm::Constant* getPureVirtualFn() {
134 if (!PureVirtualFn) {
135 const llvm::FunctionType *Ty =
136 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
137 /*isVarArg=*/false);
138 PureVirtualFn = wrap(CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual"));
139 }
140
141 return PureVirtualFn;
142 }
143
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000144public:
Mike Stump653d0b92009-11-13 02:13:54 +0000145 VtableBuilder(std::vector<llvm::Constant *> &meth, const CXXRecordDecl *c,
146 const CXXRecordDecl *l, uint64_t lo, CodeGenModule &cgm)
147 : methods(meth), Class(c), LayoutClass(l), LayoutOffset(lo),
Mike Stump83066c82009-11-13 01:54:23 +0000148 BLayout(cgm.getContext().getASTRecordLayout(l)),
Mike Stumpc01c2b82009-12-02 18:57:08 +0000149 rtti(cgm.GenerateRTTIRef(c)), VMContext(cgm.getModule().getContext()),
Anders Carlsson323bb042009-11-26 19:54:33 +0000150 CGM(cgm), PureVirtualFn(0),subAddressPoints(AllocAddressPoint(cgm, l, c)),
Mike Stump1960b202009-11-19 00:49:05 +0000151 Extern(!l->isInAnonymousNamespace()),
Anders Carlsson323bb042009-11-26 19:54:33 +0000152 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000153 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
154 }
155
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000156 llvm::DenseMap<GlobalDecl, Index_t> &getIndex() { return Index; }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000157 llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex()
158 { return VBIndex; }
159
160 llvm::Constant *wrap(Index_t i) {
161 llvm::Constant *m;
162 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
163 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
164 }
165
166 llvm::Constant *wrap(llvm::Constant *m) {
167 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
168 }
169
Mike Stump8a96d3a2009-12-02 19:50:41 +0000170#define D1(x)
171//#define D1(X) do { if (getenv("DEBUG")) { X; } } while (0)
Mike Stump75ce5732009-10-31 20:06:59 +0000172
173 void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset,
Mike Stump28431212009-10-13 22:54:56 +0000174 bool updateVBIndex, Index_t current_vbindex) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000175 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
176 e = RD->bases_end(); i != e; ++i) {
177 const CXXRecordDecl *Base =
178 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump28431212009-10-13 22:54:56 +0000179 Index_t next_vbindex = current_vbindex;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000180 if (i->isVirtual() && !SeenVBase.count(Base)) {
181 SeenVBase.insert(Base);
Mike Stump28431212009-10-13 22:54:56 +0000182 if (updateVBIndex) {
Mike Stump75ce5732009-10-31 20:06:59 +0000183 next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8)
Mike Stump28431212009-10-13 22:54:56 +0000184 - 3*LLVMPointerWidth/8);
185 VBIndex[Base] = next_vbindex;
186 }
Mike Stump75ce5732009-10-31 20:06:59 +0000187 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
188 VCalls.push_back((0?700:0) + BaseOffset);
189 D1(printf(" vbase for %s at %d delta %d most derived %s\n",
190 Base->getNameAsCString(),
191 (int)-VCalls.size()-3, (int)BaseOffset,
192 Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000193 }
Mike Stump28431212009-10-13 22:54:56 +0000194 // We also record offsets for non-virtual bases to closest enclosing
195 // virtual base. We do this so that we don't have to search
196 // for the nearst virtual base class when generating thunks.
197 if (updateVBIndex && VBIndex.count(Base) == 0)
198 VBIndex[Base] = next_vbindex;
Mike Stump75ce5732009-10-31 20:06:59 +0000199 GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000200 }
201 }
202
203 void StartNewTable() {
204 SeenVBase.clear();
205 }
206
Mike Stump8bccbfd2009-10-15 09:30:16 +0000207 Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B,
208 Index_t Offset = 0) {
209
210 if (B == D)
211 return Offset;
212
213 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D);
214 for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(),
215 e = D->bases_end(); i != e; ++i) {
216 const CXXRecordDecl *Base =
217 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
218 int64_t BaseOffset = 0;
219 if (!i->isVirtual())
220 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
221 int64_t o = getNVOffset_1(Base, B, BaseOffset);
222 if (o >= 0)
223 return o;
224 }
225
226 return -1;
227 }
228
229 /// getNVOffset - Returns the non-virtual offset for the given (B) base of the
230 /// derived class D.
231 Index_t getNVOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000232 qD = qD->getPointeeType();
233 qB = qB->getPointeeType();
Mike Stump8bccbfd2009-10-15 09:30:16 +0000234 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
235 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
236 int64_t o = getNVOffset_1(D, B);
237 if (o >= 0)
238 return o;
239
240 assert(false && "FIXME: non-virtual base not found");
241 return 0;
242 }
243
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000244 /// getVbaseOffset - Returns the index into the vtable for the virtual base
245 /// offset for the given (B) virtual base of the derived class D.
246 Index_t getVbaseOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000247 qD = qD->getPointeeType();
248 qB = qB->getPointeeType();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000249 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
250 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
251 if (D != Class)
Eli Friedman03aa2f12009-11-30 01:19:33 +0000252 return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000253 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
254 i = VBIndex.find(B);
255 if (i != VBIndex.end())
256 return i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000257
Mike Stump28431212009-10-13 22:54:56 +0000258 assert(false && "FIXME: Base not found");
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000259 return 0;
260 }
261
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000262 bool OverrideMethod(GlobalDecl GD, llvm::Constant *m,
Mike Stump8bccbfd2009-10-15 09:30:16 +0000263 bool MorallyVirtual, Index_t OverrideOffset,
Anders Carlssonca1bf682009-12-03 01:54:02 +0000264 Index_t Offset, int64_t CurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000265
266 void InstallThunks() {
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000267 for (CovariantThunksMapTy::const_iterator i = CovariantThunks.begin(),
268 e = CovariantThunks.end(); i != e; ++i) {
269 GlobalDecl GD = i->first;
270 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
271 if (MD->isPure())
272 continue;
273
274 const CovariantThunk &Thunk = i->second;
275 assert(Thunk.Index == Index[GD] && "Thunk index mismatch!");
276
277 // Check if there is an adjustment for the 'this' pointer.
278 ThunkAdjustment ThisAdjustment;
279 ThunksMapTy::iterator i = Thunks.find(GD);
280 if (i != Thunks.end()) {
281 assert(i->second.Index == Thunk.Index && "Thunk index mismatch!");
282 ThisAdjustment = i->second.Adjustment;
283
284 Thunks.erase(i);
285 }
286
287 CovariantThunkAdjustment Adjustment(ThisAdjustment,
288 Thunk.ReturnAdjustment);
289 submethods[Thunk.Index] =
290 CGM.BuildCovariantThunk(MD, Extern, Adjustment);
291 }
292 CovariantThunks.clear();
293
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000294 for (ThunksMapTy::const_iterator i = Thunks.begin(), e = Thunks.end();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000295 i != e; ++i) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000296 GlobalDecl GD = i->first;
297 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000298 assert(!MD->isPure() && "Can't thunk pure virtual methods!");
299
300 const Thunk& Thunk = i->second;
301 assert(Thunk.Index == Index[GD] && "Thunk index mismatch!");
302
303 submethods[Thunk.Index] = CGM.BuildThunk(MD, Extern, Thunk.Adjustment);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000304 }
305 Thunks.clear();
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000306
Anders Carlssond420a312009-11-26 19:32:45 +0000307 for (PureVirtualMethodsSetTy::iterator i = PureVirtualMethods.begin(),
308 e = PureVirtualMethods.end(); i != e; ++i) {
309 GlobalDecl GD = *i;
Anders Carlsson323bb042009-11-26 19:54:33 +0000310 submethods[Index[GD]] = getPureVirtualFn();
Mike Stumpbb9ff052009-10-27 23:46:47 +0000311 }
Anders Carlssond420a312009-11-26 19:32:45 +0000312 PureVirtualMethods.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000313 }
314
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000315 llvm::Constant *WrapAddrOf(GlobalDecl GD) {
316 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
317
Mike Stump18e8b472009-10-27 23:36:26 +0000318 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000319 return wrap(CGM.GetAddrOfCXXDestructor(Dtor, GD.getDtorType()));
Mike Stump18e8b472009-10-27 23:36:26 +0000320
Anders Carlsson64457732009-11-24 05:08:52 +0000321 const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD);
Mike Stump18e8b472009-10-27 23:36:26 +0000322
323 return wrap(CGM.GetAddrOfFunction(MD, Ty));
324 }
325
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000326 void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset,
327 int64_t CurrentVBaseOffset) {
Mike Stump37dbe962009-10-15 02:04:03 +0000328 for (Path_t::reverse_iterator i = Path->rbegin(),
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000329 e = Path->rend(); i != e; ++i) {
330 const CXXRecordDecl *RD = i->first;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000331 int64_t OverrideOffset = i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000332 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
333 ++mi) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000334 const CXXMethodDecl *MD = *mi;
335
336 if (!MD->isVirtual())
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000337 continue;
338
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000339 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
340 // Override both the complete and the deleting destructor.
341 GlobalDecl CompDtor(DD, Dtor_Complete);
342 OverrideMethod(CompDtor, WrapAddrOf(CompDtor), MorallyVirtual,
343 OverrideOffset, Offset, CurrentVBaseOffset);
344
345 GlobalDecl DeletingDtor(DD, Dtor_Deleting);
346 OverrideMethod(DeletingDtor, WrapAddrOf(DeletingDtor), MorallyVirtual,
347 OverrideOffset, Offset, CurrentVBaseOffset);
348 } else {
349 OverrideMethod(MD, WrapAddrOf(MD), MorallyVirtual, OverrideOffset,
350 Offset, CurrentVBaseOffset);
351 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000352 }
353 }
354 }
355
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000356 void AddMethod(const GlobalDecl GD, bool MorallyVirtual, Index_t Offset,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000357 int64_t CurrentVBaseOffset) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000358 llvm::Constant *m = WrapAddrOf(GD);
Mike Stump18e8b472009-10-27 23:36:26 +0000359
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000360 // If we can find a previously allocated slot for this, reuse it.
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000361 if (OverrideMethod(GD, m, MorallyVirtual, Offset, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000362 CurrentVBaseOffset))
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000363 return;
364
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000365 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
366
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000367 // else allocate a new slot.
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000368 Index[GD] = submethods.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000369 submethods.push_back(m);
Mike Stumpc5a332c2009-11-13 23:45:53 +0000370 D1(printf(" vfn for %s at %d\n", MD->getNameAsString().c_str(),
371 (int)Index[GD]));
Mike Stump375faa82009-10-28 00:35:46 +0000372 if (MD->isPure())
Anders Carlssond420a312009-11-26 19:32:45 +0000373 PureVirtualMethods.insert(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000374 if (MorallyVirtual) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000375 VCallOffset[GD] = Offset/8;
376 Index_t &idx = VCall[GD];
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000377 // Allocate the first one, after that, we reuse the previous one.
378 if (idx == 0) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000379 NonVirtualOffset[GD] = CurrentVBaseOffset/8 - Offset/8;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000380 idx = VCalls.size()+1;
381 VCalls.push_back(0);
Mike Stump75ce5732009-10-31 20:06:59 +0000382 D1(printf(" vcall for %s at %d with delta %d\n",
Mike Stumpc5a332c2009-11-13 23:45:53 +0000383 MD->getNameAsString().c_str(), (int)-VCalls.size()-3, 0));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000384 }
385 }
386 }
387
388 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000389 Index_t Offset, int64_t CurrentVBaseOffset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000390 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000391 ++mi) {
392 const CXXMethodDecl *MD = *mi;
393 if (!MD->isVirtual())
394 continue;
395
396 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
397 // For destructors, add both the complete and the deleting destructor
398 // to the vtable.
399 AddMethod(GlobalDecl(DD, Dtor_Complete), MorallyVirtual, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000400 CurrentVBaseOffset);
Eli Friedman03aa2f12009-11-30 01:19:33 +0000401 AddMethod(GlobalDecl(DD, Dtor_Deleting), MorallyVirtual, Offset,
402 CurrentVBaseOffset);
403 } else
404 AddMethod(MD, MorallyVirtual, Offset, CurrentVBaseOffset);
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000405 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000406 }
407
408 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
409 const CXXRecordDecl *PrimaryBase,
410 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000411 int64_t Offset, int64_t CurrentVBaseOffset,
412 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000413 Path->push_back(std::make_pair(RD, Offset));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000414 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
415 e = RD->bases_end(); i != e; ++i) {
416 if (i->isVirtual())
417 continue;
418 const CXXRecordDecl *Base =
419 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
420 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
421 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
422 StartNewTable();
Mike Stump653d0b92009-11-13 02:13:54 +0000423 GenerateVtableForBase(Base, o, MorallyVirtual, false,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000424 CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000425 }
426 }
Mike Stump37dbe962009-10-15 02:04:03 +0000427 Path->pop_back();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000428 }
429
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000430// #define D(X) do { X; } while (0)
431#define D(X)
432
433 void insertVCalls(int InsertionPoint) {
434 llvm::Constant *e = 0;
Mike Stump75ce5732009-10-31 20:06:59 +0000435 D1(printf("============= combining vbase/vcall\n"));
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000436 D(VCalls.insert(VCalls.begin(), 673));
437 D(VCalls.push_back(672));
Mike Stump8bccbfd2009-10-15 09:30:16 +0000438 methods.insert(methods.begin() + InsertionPoint, VCalls.size(), e);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000439 // The vcalls come first...
440 for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(),
441 e = VCalls.rend();
442 i != e; ++i)
443 methods[InsertionPoint++] = wrap((0?600:0) + *i);
444 VCalls.clear();
Mike Stump9f23a142009-11-10 02:30:51 +0000445 VCall.clear();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000446 }
447
Mike Stump559387f2009-11-13 23:13:20 +0000448 void AddAddressPoints(const CXXRecordDecl *RD, uint64_t Offset,
449 Index_t AddressPoint) {
450 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
451 RD->getNameAsCString(), Class->getNameAsCString(),
452 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stumpcd2b8212009-11-19 20:52:19 +0000453 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Mike Stump559387f2009-11-13 23:13:20 +0000454
455 // Now also add the address point for all our primary bases.
456 while (1) {
457 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
458 RD = Layout.getPrimaryBase();
459 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
460 // FIXME: Double check this.
461 if (RD == 0)
462 break;
463 if (PrimaryBaseWasVirtual &&
464 BLayout.getVBaseClassOffset(RD) != Offset)
465 break;
466 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
467 RD->getNameAsCString(), Class->getNameAsCString(),
468 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stumpcd2b8212009-11-19 20:52:19 +0000469 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Mike Stump559387f2009-11-13 23:13:20 +0000470 }
471 }
472
473
Mike Stump75ce5732009-10-31 20:06:59 +0000474 Index_t end(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
475 const CXXRecordDecl *PrimaryBase, bool PrimaryBaseWasVirtual,
476 bool MorallyVirtual, int64_t Offset, bool ForVirtualBase,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000477 int64_t CurrentVBaseOffset,
Mike Stump75ce5732009-10-31 20:06:59 +0000478 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000479 bool alloc = false;
480 if (Path == 0) {
481 alloc = true;
482 Path = new Path_t;
483 }
484
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000485 StartNewTable();
486 extra = 0;
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000487 bool DeferVCalls = MorallyVirtual || ForVirtualBase;
488 int VCallInsertionPoint = methods.size();
489 if (!DeferVCalls) {
490 insertVCalls(VCallInsertionPoint);
Mike Stump8bccbfd2009-10-15 09:30:16 +0000491 } else
492 // FIXME: just for extra, or for all uses of VCalls.size post this?
493 extra = -VCalls.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000494
Mike Stump653d0b92009-11-13 02:13:54 +0000495 methods.push_back(wrap(-((Offset-LayoutOffset)/8)));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000496 methods.push_back(rtti);
497 Index_t AddressPoint = methods.size();
498
499 InstallThunks();
Mike Stump75ce5732009-10-31 20:06:59 +0000500 D1(printf("============= combining methods\n"));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000501 methods.insert(methods.end(), submethods.begin(), submethods.end());
502 submethods.clear();
503
504 // and then the non-virtual bases.
505 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000506 MorallyVirtual, Offset, CurrentVBaseOffset, Path);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000507
508 if (ForVirtualBase) {
Mike Stump2cefe382009-11-12 20:47:57 +0000509 // FIXME: We're adding to VCalls in callers, we need to do the overrides
510 // in the inner part, so that we know the complete set of vcalls during
511 // the build and don't have to insert into methods. Saving out the
512 // AddressPoint here, would need to be fixed, if we didn't do that. Also
513 // retroactively adding vcalls for overrides later wind up in the wrong
514 // place, the vcall slot has to be alloted during the walk of the base
515 // when the function is first introduces.
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000516 AddressPoint += VCalls.size();
Mike Stump2cefe382009-11-12 20:47:57 +0000517 insertVCalls(VCallInsertionPoint);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000518 }
519
Mike Stump559387f2009-11-13 23:13:20 +0000520 AddAddressPoints(RD, Offset, AddressPoint);
Mike Stump2cefe382009-11-12 20:47:57 +0000521
Mike Stump37dbe962009-10-15 02:04:03 +0000522 if (alloc) {
523 delete Path;
524 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000525 return AddressPoint;
526 }
527
Mike Stump75ce5732009-10-31 20:06:59 +0000528 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
529 bool updateVBIndex, Index_t current_vbindex,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000530 int64_t CurrentVBaseOffset) {
Mike Stump75ce5732009-10-31 20:06:59 +0000531 if (!RD->isDynamicClass())
532 return;
533
534 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
535 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
536 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
537
538 // vtables are composed from the chain of primaries.
539 if (PrimaryBase) {
540 D1(printf(" doing primaries for %s most derived %s\n",
541 RD->getNameAsCString(), Class->getNameAsCString()));
542
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000543 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
544 if (PrimaryBaseWasVirtual)
545 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
546
Mike Stump75ce5732009-10-31 20:06:59 +0000547 if (!PrimaryBaseWasVirtual)
548 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000549 updateVBIndex, current_vbindex, BaseCurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000550 }
551
552 D1(printf(" doing vcall entries for %s most derived %s\n",
553 RD->getNameAsCString(), Class->getNameAsCString()));
554
555 // And add the virtuals for the class to the primary vtable.
Eli Friedman03aa2f12009-11-30 01:19:33 +0000556 AddMethods(RD, MorallyVirtual, Offset, CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000557 }
558
559 void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
560 bool updateVBIndex, Index_t current_vbindex,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000561 bool RDisVirtualBase, int64_t CurrentVBaseOffset,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000562 bool bottom) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000563 if (!RD->isDynamicClass())
564 return;
565
566 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
567 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
568 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
569
570 // vtables are composed from the chain of primaries.
571 if (PrimaryBase) {
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000572 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
573 if (PrimaryBaseWasVirtual) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000574 IndirectPrimary.insert(PrimaryBase);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000575 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
576 }
Mike Stump75ce5732009-10-31 20:06:59 +0000577
578 D1(printf(" doing primaries for %s most derived %s\n",
579 RD->getNameAsCString(), Class->getNameAsCString()));
580
581 VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000582 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000583 BaseCurrentVBaseOffset, false);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000584 }
585
Mike Stump75ce5732009-10-31 20:06:59 +0000586 D1(printf(" doing vbase entries for %s most derived %s\n",
587 RD->getNameAsCString(), Class->getNameAsCString()));
588 GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex);
589
590 if (RDisVirtualBase || bottom) {
591 Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000592 CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000593 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000594 }
595
Mike Stump653d0b92009-11-13 02:13:54 +0000596 int64_t GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0,
597 bool MorallyVirtual = false,
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000598 bool ForVirtualBase = false,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000599 int CurrentVBaseOffset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +0000600 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000601 if (!RD->isDynamicClass())
602 return 0;
603
Mike Stumpfa818082009-11-13 02:35:38 +0000604 // Construction vtable don't need parts that have no virtual bases and
605 // aren't morally virtual.
606 if ((LayoutClass != Class) && RD->getNumVBases() == 0 && !MorallyVirtual)
607 return 0;
608
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000609 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
610 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
611 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
612
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000613 extra = 0;
Mike Stump75ce5732009-10-31 20:06:59 +0000614 D1(printf("building entries for base %s most derived %s\n",
615 RD->getNameAsCString(), Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000616
Mike Stump75ce5732009-10-31 20:06:59 +0000617 if (ForVirtualBase)
618 extra = VCalls.size();
619
620 VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0, ForVirtualBase,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000621 CurrentVBaseOffset, true);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000622
623 if (Path)
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000624 OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000625
Mike Stump75ce5732009-10-31 20:06:59 +0000626 return end(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000627 Offset, ForVirtualBase, CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000628 }
629
630 void GenerateVtableForVBases(const CXXRecordDecl *RD,
631 int64_t Offset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +0000632 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000633 bool alloc = false;
634 if (Path == 0) {
635 alloc = true;
Mike Stump37dbe962009-10-15 02:04:03 +0000636 Path = new Path_t;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000637 }
638 // FIXME: We also need to override using all paths to a virtual base,
639 // right now, we just process the first path
640 Path->push_back(std::make_pair(RD, Offset));
641 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
642 e = RD->bases_end(); i != e; ++i) {
643 const CXXRecordDecl *Base =
644 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
645 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
646 // Mark it so we don't output it twice.
647 IndirectPrimary.insert(Base);
648 StartNewTable();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000649 VCall.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000650 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000651 int64_t CurrentVBaseOffset = BaseOffset;
Mike Stump75ce5732009-10-31 20:06:59 +0000652 D1(printf("vtable %s virtual base %s\n",
653 Class->getNameAsCString(), Base->getNameAsCString()));
Mike Stump653d0b92009-11-13 02:13:54 +0000654 GenerateVtableForBase(Base, BaseOffset, true, true, CurrentVBaseOffset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000655 Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000656 }
Mike Stump2cefe382009-11-12 20:47:57 +0000657 int64_t BaseOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000658 if (i->isVirtual())
659 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump2cefe382009-11-12 20:47:57 +0000660 else {
661 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
662 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
663 }
664
Mike Stump37dbe962009-10-15 02:04:03 +0000665 if (Base->getNumVBases()) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000666 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump37dbe962009-10-15 02:04:03 +0000667 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000668 }
669 Path->pop_back();
670 if (alloc)
671 delete Path;
672 }
673};
Anders Carlssonca1bf682009-12-03 01:54:02 +0000674} // end anonymous namespace
675
676bool VtableBuilder::OverrideMethod(GlobalDecl GD, llvm::Constant *m,
677 bool MorallyVirtual, Index_t OverrideOffset,
678 Index_t Offset, int64_t CurrentVBaseOffset) {
679 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
680
681 const bool isPure = MD->isPure();
682 typedef CXXMethodDecl::method_iterator meth_iter;
683 // FIXME: Should OverrideOffset's be Offset?
684
685 // FIXME: Don't like the nested loops. For very large inheritance
686 // heirarchies we could have a table on the side with the final overridder
687 // and just replace each instance of an overridden method once. Would be
688 // nice to measure the cost/benefit on real code.
689
690 for (meth_iter mi = MD->begin_overridden_methods(),
691 e = MD->end_overridden_methods();
692 mi != e; ++mi) {
693 GlobalDecl OGD;
694
695 const CXXMethodDecl *OMD = *mi;
696 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(OMD))
697 OGD = GlobalDecl(DD, GD.getDtorType());
698 else
699 OGD = OMD;
700
701 llvm::Constant *om;
702 om = WrapAddrOf(OGD);
703 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
704
705 for (Index_t i = 0, e = submethods.size();
706 i != e; ++i) {
707 // FIXME: begin_overridden_methods might be too lax, covariance */
708 if (submethods[i] != om)
709 continue;
710 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
711 CanQualType oret = CGM.getContext().getCanonicalType(nc_oret);
712 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
713 CanQualType ret = CGM.getContext().getCanonicalType(nc_ret);
714 ThunkAdjustment ReturnAdjustment;
715 if (oret != ret) {
716 // FIXME: calculate offsets for covariance
717 CovariantThunksMapTy::iterator i = CovariantThunks.find(OMD);
718 if (i != CovariantThunks.end()) {
719 oret = i->second.ReturnType;
720 CovariantThunks.erase(i);
721 }
722 // FIXME: Double check oret
723 Index_t nv = getNVOffset(oret, ret)/8;
724 ReturnAdjustment = ThunkAdjustment(nv, getVbaseOffset(oret, ret));
725 }
726 Index[GD] = i;
727 submethods[i] = m;
728 if (isPure)
729 PureVirtualMethods.insert(GD);
730 PureVirtualMethods.erase(OGD);
731 Thunks.erase(OGD);
732 if (MorallyVirtual || VCall.count(OGD)) {
733 Index_t &idx = VCall[OGD];
734 if (idx == 0) {
735 NonVirtualOffset[GD] = -OverrideOffset/8 + CurrentVBaseOffset/8;
736 VCallOffset[GD] = OverrideOffset/8;
737 idx = VCalls.size()+1;
738 VCalls.push_back(0);
739 D1(printf(" vcall for %s at %d with delta %d most derived %s\n",
740 MD->getNameAsString().c_str(), (int)-idx-3,
741 (int)VCalls[idx-1], Class->getNameAsCString()));
742 } else {
743 NonVirtualOffset[GD] = NonVirtualOffset[OGD];
744 VCallOffset[GD] = VCallOffset[OGD];
745 VCalls[idx-1] = -VCallOffset[OGD] + OverrideOffset/8;
746 D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n",
747 MD->getNameAsString().c_str(), (int)-idx-3,
748 (int)VCalls[idx-1], Class->getNameAsCString()));
749 }
750 VCall[GD] = idx;
751 int64_t NonVirtualAdjustment = NonVirtualOffset[GD];
752 int64_t VirtualAdjustment =
753 -((idx + extra + 2) * LLVMPointerWidth / 8);
754
755 // Optimize out virtual adjustments of 0.
756 if (VCalls[idx-1] == 0)
757 VirtualAdjustment = 0;
758
759 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment,
760 VirtualAdjustment);
761
762 // FIXME: Do we always have to build a covariant thunk to save oret,
763 // which is the containing virtual base class?
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000764 if (!ReturnAdjustment.isEmpty())
765 CovariantThunks[GD] = CovariantThunk(i, ReturnAdjustment, oret);
766
767 if (!isPure && !ThisAdjustment.isEmpty())
Anders Carlssonca1bf682009-12-03 01:54:02 +0000768 Thunks[GD] = Thunk(i, ThisAdjustment);
769 return true;
770 }
771
772 // FIXME: finish off
773 int64_t NonVirtualAdjustment = VCallOffset[OGD] - OverrideOffset/8;
774
775 if (NonVirtualAdjustment || !ReturnAdjustment.isEmpty()) {
776 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, 0);
777
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000778 if (!ReturnAdjustment.isEmpty())
Anders Carlssonca1bf682009-12-03 01:54:02 +0000779 CovariantThunks[GD] =
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000780 CovariantThunk(i, ReturnAdjustment, oret);
781
782 if (!isPure)
Anders Carlssonca1bf682009-12-03 01:54:02 +0000783 Thunks[GD] = Thunk(i, ThisAdjustment);
784 }
785 return true;
786 }
787 }
788
789 return false;
Anders Carlssond59885022009-11-27 22:21:51 +0000790}
791
Anders Carlssonca1bf682009-12-03 01:54:02 +0000792
Anders Carlssonf942ee02009-11-27 20:47:55 +0000793/// TypeConversionRequiresAdjustment - Returns whether conversion from a
794/// derived type to a base type requires adjustment.
795static bool
796TypeConversionRequiresAdjustment(ASTContext &Ctx,
797 const CXXRecordDecl *DerivedDecl,
798 const CXXRecordDecl *BaseDecl) {
799 CXXBasePaths Paths(/*FindAmbiguities=*/false,
800 /*RecordPaths=*/true, /*DetectVirtual=*/true);
801 if (!const_cast<CXXRecordDecl *>(DerivedDecl)->
802 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseDecl), Paths)) {
803 assert(false && "Class must be derived from the passed in base class!");
804 return false;
805 }
806
Anders Carlsson548cc9d2009-11-28 03:03:52 +0000807 // If we found a virtual base we always want to require adjustment.
808 if (Paths.getDetectedVirtual())
809 return true;
810
Anders Carlssonf942ee02009-11-27 20:47:55 +0000811 const CXXBasePath &Path = Paths.front();
812
Anders Carlsson548cc9d2009-11-28 03:03:52 +0000813 for (size_t Start = 0, End = Path.size(); Start != End; ++Start) {
Anders Carlssonf942ee02009-11-27 20:47:55 +0000814 const CXXBasePathElement &Element = Path[Start];
815
816 // Check the base class offset.
817 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Element.Class);
818
819 const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>();
820 const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl());
821
822 if (Layout.getBaseClassOffset(Base) != 0) {
823 // This requires an adjustment.
824 return true;
825 }
826 }
827
828 return false;
829}
830
831static bool
832TypeConversionRequiresAdjustment(ASTContext &Ctx,
833 QualType DerivedType, QualType BaseType) {
834 // Canonicalize the types.
835 QualType CanDerivedType = Ctx.getCanonicalType(DerivedType);
836 QualType CanBaseType = Ctx.getCanonicalType(BaseType);
837
838 assert(CanDerivedType->getTypeClass() == CanBaseType->getTypeClass() &&
839 "Types must have same type class!");
840
841 if (CanDerivedType == CanBaseType) {
842 // No adjustment needed.
843 return false;
844 }
845
846 if (const ReferenceType *RT = dyn_cast<ReferenceType>(CanDerivedType)) {
847 CanDerivedType = RT->getPointeeType();
848 CanBaseType = cast<ReferenceType>(CanBaseType)->getPointeeType();
849 } else if (const PointerType *PT = dyn_cast<PointerType>(CanDerivedType)) {
850 CanDerivedType = PT->getPointeeType();
851 CanBaseType = cast<PointerType>(CanBaseType)->getPointeeType();
852 } else {
853 assert(false && "Unexpected return type!");
854 }
855
856 if (CanDerivedType == CanBaseType) {
857 // No adjustment needed.
858 return false;
859 }
860
861 const CXXRecordDecl *DerivedDecl =
862 cast<CXXRecordDecl>(cast<RecordType>(CanDerivedType)->getDecl());
863
864 const CXXRecordDecl *BaseDecl =
865 cast<CXXRecordDecl>(cast<RecordType>(CanBaseType)->getDecl());
866
867 return TypeConversionRequiresAdjustment(Ctx, DerivedDecl, BaseDecl);
868}
869
870void CGVtableInfo::ComputeMethodVtableIndices(const CXXRecordDecl *RD) {
871
872 // Itanium C++ ABI 2.5.2:
873 // The order of the virtual function pointers in a virtual table is the
874 // order of declaration of the corresponding member functions in the class.
875 //
876 // There is an entry for any virtual function declared in a class,
877 // whether it is a new function or overrides a base class function,
878 // unless it overrides a function from the primary base, and conversion
879 // between their return types does not require an adjustment.
880
881 int64_t CurrentIndex = 0;
882
883 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
884 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
885
886 if (PrimaryBase) {
Anders Carlssonc920fa22009-11-30 19:43:26 +0000887 assert(PrimaryBase->isDefinition() &&
888 "Should have the definition decl of the primary base!");
Anders Carlssonf942ee02009-11-27 20:47:55 +0000889
890 // Since the record decl shares its vtable pointer with the primary base
891 // we need to start counting at the end of the primary base's vtable.
892 CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase);
893 }
894
895 const CXXDestructorDecl *ImplicitVirtualDtor = 0;
896
897 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
898 e = RD->method_end(); i != e; ++i) {
899 const CXXMethodDecl *MD = *i;
900
901 // We only want virtual methods.
902 if (!MD->isVirtual())
903 continue;
904
905 bool ShouldAddEntryForMethod = true;
906
907 // Check if this method overrides a method in the primary base.
908 for (CXXMethodDecl::method_iterator i = MD->begin_overridden_methods(),
909 e = MD->end_overridden_methods(); i != e; ++i) {
910 const CXXMethodDecl *OverriddenMD = *i;
911 const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent();
912 assert(OverriddenMD->isCanonicalDecl() &&
913 "Should have the canonical decl of the overridden RD!");
914
915 if (OverriddenRD == PrimaryBase) {
916 // Check if converting from the return type of the method to the
917 // return type of the overridden method requires conversion.
918 QualType ReturnType =
919 MD->getType()->getAs<FunctionType>()->getResultType();
920 QualType OverriddenReturnType =
921 OverriddenMD->getType()->getAs<FunctionType>()->getResultType();
922
923 if (!TypeConversionRequiresAdjustment(CGM.getContext(),
924 ReturnType, OverriddenReturnType)) {
925 // This index is shared between the index in the vtable of the primary
926 // base class.
927 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
928 const CXXDestructorDecl *OverriddenDD =
929 cast<CXXDestructorDecl>(OverriddenMD);
930
931 // Add both the complete and deleting entries.
932 MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] =
933 getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Complete));
934 MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] =
935 getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting));
936 } else {
937 MethodVtableIndices[MD] = getMethodVtableIndex(OverriddenMD);
938 }
939
940 // We don't need to add an entry for this method.
941 ShouldAddEntryForMethod = false;
942 break;
943 }
944 }
945 }
946
947 if (!ShouldAddEntryForMethod)
948 continue;
949
950 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
951 if (MD->isImplicit()) {
952 assert(!ImplicitVirtualDtor &&
953 "Did already see an implicit virtual dtor!");
954 ImplicitVirtualDtor = DD;
955 continue;
956 }
957
958 // Add the complete dtor.
959 MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++;
960
961 // Add the deleting dtor.
962 MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++;
963 } else {
964 // Add the entry.
965 MethodVtableIndices[MD] = CurrentIndex++;
966 }
967 }
968
969 if (ImplicitVirtualDtor) {
970 // Itanium C++ ABI 2.5.2:
971 // If a class has an implicitly-defined virtual destructor,
972 // its entries come after the declared virtual function pointers.
973
974 // Add the complete dtor.
975 MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] =
976 CurrentIndex++;
977
978 // Add the deleting dtor.
979 MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] =
980 CurrentIndex++;
981 }
982
983 NumVirtualFunctionPointers[RD] = CurrentIndex;
984}
985
986uint64_t CGVtableInfo::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) {
987 llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
988 NumVirtualFunctionPointers.find(RD);
989 if (I != NumVirtualFunctionPointers.end())
990 return I->second;
991
992 ComputeMethodVtableIndices(RD);
993
994 I = NumVirtualFunctionPointers.find(RD);
995 assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!");
996 return I->second;
997}
998
999uint64_t CGVtableInfo::getMethodVtableIndex(GlobalDecl GD) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001000 MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001001 if (I != MethodVtableIndices.end())
1002 return I->second;
1003
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001004 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
Anders Carlssonf942ee02009-11-27 20:47:55 +00001005
1006 ComputeMethodVtableIndices(RD);
1007
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001008 I = MethodVtableIndices.find(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001009 assert(I != MethodVtableIndices.end() && "Did not find index!");
1010 return I->second;
1011}
1012
1013int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
1014 const CXXRecordDecl *VBase) {
1015 ClassPairTy ClassPair(RD, VBase);
1016
1017 VirtualBaseClassIndiciesTy::iterator I =
1018 VirtualBaseClassIndicies.find(ClassPair);
1019 if (I != VirtualBaseClassIndicies.end())
1020 return I->second;
1021
1022 std::vector<llvm::Constant *> methods;
1023 // FIXME: This seems expensive. Can we do a partial job to get
1024 // just this data.
Mike Stump653d0b92009-11-13 02:13:54 +00001025 VtableBuilder b(methods, RD, RD, 0, CGM);
Mike Stump75ce5732009-10-31 20:06:59 +00001026 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001027 b.GenerateVtableForBase(RD);
1028 b.GenerateVtableForVBases(RD);
1029
1030 for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
1031 b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) {
1032 // Insert all types.
1033 ClassPairTy ClassPair(RD, I->first);
1034
1035 VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second));
1036 }
1037
1038 I = VirtualBaseClassIndicies.find(ClassPair);
1039 assert(I != VirtualBaseClassIndicies.end() && "Did not find index!");
1040
1041 return I->second;
1042}
1043
Mike Stump2cefe382009-11-12 20:47:57 +00001044llvm::Constant *CodeGenModule::GenerateVtable(const CXXRecordDecl *LayoutClass,
1045 const CXXRecordDecl *RD,
Mike Stumpeac45592009-11-11 20:26:26 +00001046 uint64_t Offset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001047 llvm::SmallString<256> OutName;
Mike Stump2cefe382009-11-12 20:47:57 +00001048 if (LayoutClass != RD)
Daniel Dunbare128dd12009-11-21 09:06:22 +00001049 getMangleContext().mangleCXXCtorVtable(LayoutClass, Offset/8, RD, OutName);
Mike Stumpeac45592009-11-11 20:26:26 +00001050 else
Daniel Dunbare128dd12009-11-21 09:06:22 +00001051 getMangleContext().mangleCXXVtable(RD, OutName);
1052 llvm::StringRef Name = OutName.str();
Benjamin Kramerbb0a07b2009-10-11 22:57:54 +00001053
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001054 std::vector<llvm::Constant *> methods;
1055 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1056 int64_t AddressPoint;
1057
Mike Stumpaa51ad62009-11-19 04:04:36 +00001058 llvm::GlobalVariable *GV = getModule().getGlobalVariable(Name);
Mike Stumpcd2b8212009-11-19 20:52:19 +00001059 if (GV && AddressPoints[LayoutClass] && !GV->isDeclaration()) {
Mike Stumpaa51ad62009-11-19 04:04:36 +00001060 AddressPoint=(*(*(AddressPoints[LayoutClass]))[RD])[std::make_pair(RD,
1061 Offset)];
Mike Stumpcd2b8212009-11-19 20:52:19 +00001062 // FIXME: We can never have 0 address point. Do this for now so gepping
1063 // retains the same structure. Later, we'll just assert.
1064 if (AddressPoint == 0)
1065 AddressPoint = 1;
1066 } else {
Mike Stumpaa51ad62009-11-19 04:04:36 +00001067 VtableBuilder b(methods, RD, LayoutClass, Offset, *this);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001068
Mike Stumpaa51ad62009-11-19 04:04:36 +00001069 D1(printf("vtable %s\n", RD->getNameAsCString()));
1070 // First comes the vtables for all the non-virtual bases...
1071 AddressPoint = b.GenerateVtableForBase(RD, Offset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001072
Mike Stumpaa51ad62009-11-19 04:04:36 +00001073 // then the vtables for all the virtual bases.
1074 b.GenerateVtableForVBases(RD, Offset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001075
Mike Stumpaa51ad62009-11-19 04:04:36 +00001076 bool CreateDefinition = true;
1077 if (LayoutClass != RD)
1078 CreateDefinition = true;
1079 else {
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001080 const ASTRecordLayout &Layout =
1081 getContext().getASTRecordLayout(LayoutClass);
1082
1083 if (const CXXMethodDecl *KeyFunction = Layout.getKeyFunction()) {
Mike Stumpaa51ad62009-11-19 04:04:36 +00001084 if (!KeyFunction->getBody()) {
1085 // If there is a KeyFunction, and it isn't defined, just build a
1086 // reference to the vtable.
1087 CreateDefinition = false;
1088 }
1089 }
1090 }
1091
1092 llvm::Constant *C = 0;
1093 llvm::Type *type = Ptr8Ty;
1094 llvm::GlobalVariable::LinkageTypes linktype
1095 = llvm::GlobalValue::ExternalLinkage;
1096 if (CreateDefinition) {
1097 llvm::ArrayType *ntype = llvm::ArrayType::get(Ptr8Ty, methods.size());
1098 C = llvm::ConstantArray::get(ntype, methods);
1099 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
1100 if (LayoutClass->isInAnonymousNamespace())
1101 linktype = llvm::GlobalValue::InternalLinkage;
1102 type = ntype;
1103 }
1104 llvm::GlobalVariable *OGV = GV;
1105 GV = new llvm::GlobalVariable(getModule(), type, true, linktype, C, Name);
1106 if (OGV) {
1107 GV->takeName(OGV);
1108 llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
1109 OGV->getType());
1110 OGV->replaceAllUsesWith(NewPtr);
1111 OGV->eraseFromParent();
1112 }
1113 bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden;
1114 if (Hidden)
1115 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
1116 }
Mike Stumpc0f632d2009-11-18 04:00:48 +00001117 llvm::Constant *vtable = llvm::ConstantExpr::getBitCast(GV, Ptr8Ty);
Mike Stumpd846d082009-11-10 07:44:33 +00001118 llvm::Constant *AddressPointC;
1119 uint32_t LLVMPointerWidth = getContext().Target.getPointerWidth(0);
1120 AddressPointC = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1121 AddressPoint*LLVMPointerWidth/8);
Mike Stump2cefe382009-11-12 20:47:57 +00001122 vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(vtable, &AddressPointC,
1123 1);
Mike Stumpd846d082009-11-10 07:44:33 +00001124
Mike Stumpcd2b8212009-11-19 20:52:19 +00001125 assert(vtable->getType() == Ptr8Ty);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001126 return vtable;
1127}
Mike Stump9f23a142009-11-10 02:30:51 +00001128
Mike Stumpae1b85d2009-12-02 19:07:44 +00001129namespace {
Mike Stump9f23a142009-11-10 02:30:51 +00001130class VTTBuilder {
1131 /// Inits - The list of values built for the VTT.
1132 std::vector<llvm::Constant *> &Inits;
1133 /// Class - The most derived class that this vtable is being built for.
1134 const CXXRecordDecl *Class;
1135 CodeGenModule &CGM; // Per-module state.
Mike Stump8b2d2d02009-11-11 00:35:07 +00001136 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
Mike Stumpc7b9f5e2009-11-11 03:08:24 +00001137 /// BLayout - Layout for the most derived class that this vtable is being
1138 /// built for.
1139 const ASTRecordLayout &BLayout;
Mike Stump83066c82009-11-13 01:54:23 +00001140 CodeGenModule::AddrMap_t &AddressPoints;
Mike Stump2cefe382009-11-12 20:47:57 +00001141 // vtbl - A pointer to the vtable for Class.
1142 llvm::Constant *ClassVtbl;
1143 llvm::LLVMContext &VMContext;
Mike Stump9f23a142009-11-10 02:30:51 +00001144
Mike Stump8677bc22009-11-12 22:56:32 +00001145 /// BuildVtablePtr - Build up a referene to the given secondary vtable
Mike Stump83066c82009-11-13 01:54:23 +00001146 llvm::Constant *BuildVtablePtr(llvm::Constant *vtbl,
1147 const CXXRecordDecl *VtblClass,
1148 const CXXRecordDecl *RD,
Mike Stump8677bc22009-11-12 22:56:32 +00001149 uint64_t Offset) {
1150 int64_t AddressPoint;
Mike Stump83066c82009-11-13 01:54:23 +00001151 AddressPoint = (*AddressPoints[VtblClass])[std::make_pair(RD, Offset)];
Mike Stump2b34bc52009-11-12 23:36:21 +00001152 // FIXME: We can never have 0 address point. Do this for now so gepping
Mike Stumpcd2b8212009-11-19 20:52:19 +00001153 // retains the same structure. Later we'll just assert.
Mike Stump2b34bc52009-11-12 23:36:21 +00001154 if (AddressPoint == 0)
1155 AddressPoint = 1;
Mike Stump83066c82009-11-13 01:54:23 +00001156 D1(printf("XXX address point for %s in %s layout %s at offset %d was %d\n",
1157 RD->getNameAsCString(), VtblClass->getNameAsCString(),
1158 Class->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stump8677bc22009-11-12 22:56:32 +00001159 uint32_t LLVMPointerWidth = CGM.getContext().Target.getPointerWidth(0);
1160 llvm::Constant *init;
1161 init = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1162 AddressPoint*LLVMPointerWidth/8);
1163 init = llvm::ConstantExpr::getInBoundsGetElementPtr(vtbl, &init, 1);
1164 return init;
1165 }
1166
Mike Stump2cefe382009-11-12 20:47:57 +00001167 /// Secondary - Add the secondary vtable pointers to Inits. Offset is the
1168 /// current offset in bits to the object we're working on.
Mike Stump8677bc22009-11-12 22:56:32 +00001169 void Secondary(const CXXRecordDecl *RD, llvm::Constant *vtbl,
Mike Stump83066c82009-11-13 01:54:23 +00001170 const CXXRecordDecl *VtblClass, uint64_t Offset=0,
1171 bool MorallyVirtual=false) {
Mike Stump8b2d2d02009-11-11 00:35:07 +00001172 if (RD->getNumVBases() == 0 && ! MorallyVirtual)
1173 return;
1174
1175 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1176 e = RD->bases_end(); i != e; ++i) {
1177 const CXXRecordDecl *Base =
1178 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1179 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1180 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1181 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1182 bool NonVirtualPrimaryBase;
1183 NonVirtualPrimaryBase = !PrimaryBaseWasVirtual && Base == PrimaryBase;
1184 bool BaseMorallyVirtual = MorallyVirtual | i->isVirtual();
Mike Stumpc7b9f5e2009-11-11 03:08:24 +00001185 uint64_t BaseOffset;
1186 if (!i->isVirtual()) {
1187 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1188 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
1189 } else
1190 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump2b34bc52009-11-12 23:36:21 +00001191 llvm::Constant *subvtbl = vtbl;
Mike Stump83066c82009-11-13 01:54:23 +00001192 const CXXRecordDecl *subVtblClass = VtblClass;
Mike Stump8b2d2d02009-11-11 00:35:07 +00001193 if ((Base->getNumVBases() || BaseMorallyVirtual)
1194 && !NonVirtualPrimaryBase) {
1195 // FIXME: Slightly too many of these for __ZTT8test8_B2
Mike Stump8677bc22009-11-12 22:56:32 +00001196 llvm::Constant *init;
Mike Stump2b34bc52009-11-12 23:36:21 +00001197 if (BaseMorallyVirtual)
Mike Stump83066c82009-11-13 01:54:23 +00001198 init = BuildVtablePtr(vtbl, VtblClass, RD, Offset);
Mike Stump2b34bc52009-11-12 23:36:21 +00001199 else {
Mike Stump8677bc22009-11-12 22:56:32 +00001200 init = CGM.getVtableInfo().getCtorVtable(Class, Base, BaseOffset);
Mike Stump2b34bc52009-11-12 23:36:21 +00001201 subvtbl = dyn_cast<llvm::Constant>(init->getOperand(0));
Mike Stump83066c82009-11-13 01:54:23 +00001202 subVtblClass = Base;
Mike Stump2b34bc52009-11-12 23:36:21 +00001203 }
Mike Stump8677bc22009-11-12 22:56:32 +00001204 Inits.push_back(init);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001205 }
Mike Stump83066c82009-11-13 01:54:23 +00001206 Secondary(Base, subvtbl, subVtblClass, BaseOffset, BaseMorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001207 }
1208 }
1209
Mike Stump2cefe382009-11-12 20:47:57 +00001210 /// BuiltVTT - Add the VTT to Inits. Offset is the offset in bits to the
1211 /// currnet object we're working on.
1212 void BuildVTT(const CXXRecordDecl *RD, uint64_t Offset, bool MorallyVirtual) {
Mike Stump8b2d2d02009-11-11 00:35:07 +00001213 if (RD->getNumVBases() == 0 && !MorallyVirtual)
1214 return;
1215
Mike Stump2cefe382009-11-12 20:47:57 +00001216 llvm::Constant *init;
Mike Stump83066c82009-11-13 01:54:23 +00001217 const CXXRecordDecl *VtblClass;
1218
Mike Stump8b2d2d02009-11-11 00:35:07 +00001219 // First comes the primary virtual table pointer...
Mike Stump83066c82009-11-13 01:54:23 +00001220 if (MorallyVirtual) {
1221 init = BuildVtablePtr(ClassVtbl, Class, RD, Offset);
1222 VtblClass = Class;
1223 } else {
Mike Stump2cefe382009-11-12 20:47:57 +00001224 init = CGM.getVtableInfo().getCtorVtable(Class, RD, Offset);
Mike Stump83066c82009-11-13 01:54:23 +00001225 VtblClass = RD;
1226 }
Mike Stump8677bc22009-11-12 22:56:32 +00001227 llvm::Constant *vtbl = dyn_cast<llvm::Constant>(init->getOperand(0));
Mike Stump2cefe382009-11-12 20:47:57 +00001228 Inits.push_back(init);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001229
1230 // then the secondary VTTs....
Mike Stump2cefe382009-11-12 20:47:57 +00001231 SecondaryVTTs(RD, Offset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001232
1233 // and last the secondary vtable pointers.
Mike Stump83066c82009-11-13 01:54:23 +00001234 Secondary(RD, vtbl, VtblClass, Offset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001235 }
1236
1237 /// SecondaryVTTs - Add the secondary VTTs to Inits. The secondary VTTs are
1238 /// built from each direct non-virtual proper base that requires a VTT in
1239 /// declaration order.
Mike Stump2cefe382009-11-12 20:47:57 +00001240 void SecondaryVTTs(const CXXRecordDecl *RD, uint64_t Offset=0,
1241 bool MorallyVirtual=false) {
Mike Stump8b2d2d02009-11-11 00:35:07 +00001242 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1243 e = RD->bases_end(); i != e; ++i) {
1244 const CXXRecordDecl *Base =
1245 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1246 if (i->isVirtual())
1247 continue;
Mike Stump2cefe382009-11-12 20:47:57 +00001248 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1249 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base);
1250 BuildVTT(Base, BaseOffset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001251 }
1252 }
1253
1254 /// VirtualVTTs - Add the VTT for each proper virtual base in inheritance
1255 /// graph preorder.
1256 void VirtualVTTs(const CXXRecordDecl *RD) {
1257 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1258 e = RD->bases_end(); i != e; ++i) {
1259 const CXXRecordDecl *Base =
1260 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1261 if (i->isVirtual() && !SeenVBase.count(Base)) {
1262 SeenVBase.insert(Base);
Mike Stump2cefe382009-11-12 20:47:57 +00001263 uint64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
1264 BuildVTT(Base, BaseOffset, true);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001265 }
1266 VirtualVTTs(Base);
1267 }
1268 }
Mike Stump9f23a142009-11-10 02:30:51 +00001269public:
1270 VTTBuilder(std::vector<llvm::Constant *> &inits, const CXXRecordDecl *c,
Mike Stumpc7b9f5e2009-11-11 03:08:24 +00001271 CodeGenModule &cgm)
1272 : Inits(inits), Class(c), CGM(cgm),
Mike Stump2cefe382009-11-12 20:47:57 +00001273 BLayout(cgm.getContext().getASTRecordLayout(c)),
Mike Stump83066c82009-11-13 01:54:23 +00001274 AddressPoints(*cgm.AddressPoints[c]),
Mike Stump2cefe382009-11-12 20:47:57 +00001275 VMContext(cgm.getModule().getContext()) {
Mike Stumpd846d082009-11-10 07:44:33 +00001276
Mike Stump8b2d2d02009-11-11 00:35:07 +00001277 // First comes the primary virtual table pointer for the complete class...
Mike Stump2cefe382009-11-12 20:47:57 +00001278 ClassVtbl = CGM.getVtableInfo().getVtable(Class);
1279 Inits.push_back(ClassVtbl);
1280 ClassVtbl = dyn_cast<llvm::Constant>(ClassVtbl->getOperand(0));
1281
Mike Stump8b2d2d02009-11-11 00:35:07 +00001282 // then the secondary VTTs...
1283 SecondaryVTTs(Class);
1284
1285 // then the secondary vtable pointers...
Mike Stump83066c82009-11-13 01:54:23 +00001286 Secondary(Class, ClassVtbl, Class);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001287
1288 // and last, the virtual VTTs.
1289 VirtualVTTs(Class);
Mike Stump9f23a142009-11-10 02:30:51 +00001290 }
1291};
Mike Stumpae1b85d2009-12-02 19:07:44 +00001292}
Mike Stump9f23a142009-11-10 02:30:51 +00001293
Mike Stumpd846d082009-11-10 07:44:33 +00001294llvm::Constant *CodeGenModule::GenerateVTT(const CXXRecordDecl *RD) {
Mike Stumpb4722212009-11-10 19:13:04 +00001295 // Only classes that have virtual bases need a VTT.
1296 if (RD->getNumVBases() == 0)
1297 return 0;
1298
Mike Stump9f23a142009-11-10 02:30:51 +00001299 llvm::SmallString<256> OutName;
Daniel Dunbare128dd12009-11-21 09:06:22 +00001300 getMangleContext().mangleCXXVTT(RD, OutName);
1301 llvm::StringRef Name = OutName.str();
Mike Stump9f23a142009-11-10 02:30:51 +00001302
1303 llvm::GlobalVariable::LinkageTypes linktype;
1304 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
Mike Stumpaa51ad62009-11-19 04:04:36 +00001305 if (RD->isInAnonymousNamespace())
1306 linktype = llvm::GlobalValue::InternalLinkage;
Mike Stump9f23a142009-11-10 02:30:51 +00001307 std::vector<llvm::Constant *> inits;
1308 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1309
Mike Stump9f23a142009-11-10 02:30:51 +00001310 D1(printf("vtt %s\n", RD->getNameAsCString()));
1311
Mike Stump8b2d2d02009-11-11 00:35:07 +00001312 VTTBuilder b(inits, RD, *this);
1313
Mike Stump9f23a142009-11-10 02:30:51 +00001314 llvm::Constant *C;
1315 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, inits.size());
1316 C = llvm::ConstantArray::get(type, inits);
Mike Stumpc0f632d2009-11-18 04:00:48 +00001317 llvm::GlobalVariable *vtt = new llvm::GlobalVariable(getModule(), type, true,
Mike Stumpaa51ad62009-11-19 04:04:36 +00001318 linktype, C, Name);
Mike Stumpc0f632d2009-11-18 04:00:48 +00001319 bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden;
1320 if (Hidden)
1321 vtt->setVisibility(llvm::GlobalVariable::HiddenVisibility);
1322 return llvm::ConstantExpr::getBitCast(vtt, Ptr8Ty);
Mike Stump9f23a142009-11-10 02:30:51 +00001323}
Mike Stumpd846d082009-11-10 07:44:33 +00001324
Mike Stump1a139f82009-11-19 01:08:19 +00001325void CGVtableInfo::GenerateClassData(const CXXRecordDecl *RD) {
1326 Vtables[RD] = CGM.GenerateVtable(RD, RD);
Mike Stumpc01c2b82009-12-02 18:57:08 +00001327 CGM.GenerateRTTI(RD);
Mike Stump1a139f82009-11-19 01:08:19 +00001328 CGM.GenerateVTT(RD);
1329}
1330
Mike Stumpeac45592009-11-11 20:26:26 +00001331llvm::Constant *CGVtableInfo::getVtable(const CXXRecordDecl *RD) {
Mike Stumpd846d082009-11-10 07:44:33 +00001332 llvm::Constant *&vtbl = Vtables[RD];
1333 if (vtbl)
1334 return vtbl;
Mike Stump2cefe382009-11-12 20:47:57 +00001335 vtbl = CGM.GenerateVtable(RD, RD);
Mike Stumpaa51ad62009-11-19 04:04:36 +00001336
1337 bool CreateDefinition = true;
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001338
1339 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1340 if (const CXXMethodDecl *KeyFunction = Layout.getKeyFunction()) {
Mike Stumpaa51ad62009-11-19 04:04:36 +00001341 if (!KeyFunction->getBody()) {
1342 // If there is a KeyFunction, and it isn't defined, just build a
1343 // reference to the vtable.
1344 CreateDefinition = false;
1345 }
1346 }
1347
1348 if (CreateDefinition) {
Mike Stumpc01c2b82009-12-02 18:57:08 +00001349 CGM.GenerateRTTI(RD);
Mike Stumpaa51ad62009-11-19 04:04:36 +00001350 CGM.GenerateVTT(RD);
1351 }
Mike Stumpd846d082009-11-10 07:44:33 +00001352 return vtbl;
1353}
Mike Stumpeac45592009-11-11 20:26:26 +00001354
Mike Stump2cefe382009-11-12 20:47:57 +00001355llvm::Constant *CGVtableInfo::getCtorVtable(const CXXRecordDecl *LayoutClass,
1356 const CXXRecordDecl *RD,
Mike Stumpeac45592009-11-11 20:26:26 +00001357 uint64_t Offset) {
Mike Stump2cefe382009-11-12 20:47:57 +00001358 return CGM.GenerateVtable(LayoutClass, RD, Offset);
Mike Stumpeac45592009-11-11 20:26:26 +00001359}
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001360
1361void CGVtableInfo::MaybeEmitVtable(GlobalDecl GD) {
1362 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1363 const CXXRecordDecl *RD = MD->getParent();
1364
1365 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1366
1367 // Get the key function.
1368 const CXXMethodDecl *KeyFunction = Layout.getKeyFunction();
1369
1370 if (!KeyFunction) {
1371 // If there's no key function, we don't want to emit the vtable here.
1372 return;
1373 }
1374
1375 // Check if we have the key function.
1376 if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl())
1377 return;
1378
1379 // If the key function is a destructor, we only want to emit the vtable once,
1380 // so do it for the complete destructor.
1381 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Complete)
1382 return;
1383
1384 // Emit the data.
1385 GenerateClassData(RD);
1386}
1387