blob: fb3e6be3e6b233e7d0e249dd73099b9a8c8fa8e8 [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"
16
17#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
24class VtableBuilder {
25public:
26 /// Index_t - Vtable index type.
27 typedef uint64_t Index_t;
28private:
29 std::vector<llvm::Constant *> &methods;
30 std::vector<llvm::Constant *> submethods;
31 llvm::Type *Ptr8Ty;
32 /// Class - The most derived class that this vtable is being built for.
33 const CXXRecordDecl *Class;
Mike Stump83066c82009-11-13 01:54:23 +000034 /// LayoutClass - The most derived class used for virtual base layout
35 /// information.
36 const CXXRecordDecl *LayoutClass;
Mike Stump653d0b92009-11-13 02:13:54 +000037 /// LayoutOffset - The offset for Class in LayoutClass.
38 uint64_t LayoutOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000039 /// BLayout - Layout for the most derived class that this vtable is being
40 /// built for.
41 const ASTRecordLayout &BLayout;
42 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
43 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
44 llvm::Constant *rtti;
45 llvm::LLVMContext &VMContext;
46 CodeGenModule &CGM; // Per-module state.
47 /// Index - Maps a method decl into a vtable index. Useful for virtual
48 /// dispatch codegen.
Anders Carlssonfb4dda42009-11-13 17:08:56 +000049 llvm::DenseMap<GlobalDecl, Index_t> Index;
50 llvm::DenseMap<GlobalDecl, Index_t> VCall;
51 llvm::DenseMap<GlobalDecl, Index_t> VCallOffset;
Mike Stumpcd6f9ed2009-11-06 23:27:42 +000052 // This is the offset to the nearest virtual base
Anders Carlssonfb4dda42009-11-13 17:08:56 +000053 llvm::DenseMap<GlobalDecl, Index_t> NonVirtualOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000054 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stumpbb9ff052009-10-27 23:46:47 +000055
Anders Carlsson323bb042009-11-26 19:54:33 +000056 /// PureVirtualFunction - Points to __cxa_pure_virtual.
57 llvm::Constant *PureVirtualFn;
58
Anders Carlsson6d771bc2009-11-26 03:25:13 +000059 /// Thunk - Represents a single thunk.
60 struct Thunk {
61 Thunk()
62 : Index(0) { }
63
64 Thunk(uint64_t Index, const ThunkAdjustment &Adjustment)
65 : Index(Index), Adjustment(Adjustment) { }
66
67 /// Index - The index in the vtable.
68 uint64_t Index;
69
Anders Carlssond420a312009-11-26 19:32:45 +000070 /// Adjustment - The thunk adjustment.
Anders Carlsson6d771bc2009-11-26 03:25:13 +000071 ThunkAdjustment Adjustment;
72 };
Anders Carlssond420a312009-11-26 19:32:45 +000073
74 /// Thunks - The thunks in a vtable.
Anders Carlsson6d771bc2009-11-26 03:25:13 +000075 typedef llvm::DenseMap<GlobalDecl, Thunk> ThunksMapTy;
76 ThunksMapTy Thunks;
Anders Carlssond420a312009-11-26 19:32:45 +000077
78 /// CovariantThunk - Represents a single covariant thunk.
79 struct CovariantThunk {
80 CovariantThunk()
81 : Index(0) { }
82
83 CovariantThunk(uint64_t Index, const ThunkAdjustment &ThisAdjustment,
84 const ThunkAdjustment &ReturnAdjustment,
85 CanQualType ReturnType)
86 : Index(Index), Adjustment(ThisAdjustment, ReturnAdjustment),
87 ReturnType(ReturnType) { }
88
89 // Index - The index in the vtable.
90 uint64_t Index;
91
92 /// Adjustment - The covariant thunk adjustment.
93 CovariantThunkAdjustment Adjustment;
94
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 Stumpf5b28692009-11-14 23:32:21 +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 Stump2b34bc52009-11-12 23:36:21 +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
207 Index_t VBlookup(CXXRecordDecl *D, CXXRecordDecl *B);
208
Mike Stump8bccbfd2009-10-15 09:30:16 +0000209 Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B,
210 Index_t Offset = 0) {
211
212 if (B == D)
213 return Offset;
214
215 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D);
216 for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(),
217 e = D->bases_end(); i != e; ++i) {
218 const CXXRecordDecl *Base =
219 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
220 int64_t BaseOffset = 0;
221 if (!i->isVirtual())
222 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
223 int64_t o = getNVOffset_1(Base, B, BaseOffset);
224 if (o >= 0)
225 return o;
226 }
227
228 return -1;
229 }
230
231 /// getNVOffset - Returns the non-virtual offset for the given (B) base of the
232 /// derived class D.
233 Index_t getNVOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000234 qD = qD->getPointeeType();
235 qB = qB->getPointeeType();
Mike Stump8bccbfd2009-10-15 09:30:16 +0000236 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
237 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
238 int64_t o = getNVOffset_1(D, B);
239 if (o >= 0)
240 return o;
241
242 assert(false && "FIXME: non-virtual base not found");
243 return 0;
244 }
245
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000246 /// getVbaseOffset - Returns the index into the vtable for the virtual base
247 /// offset for the given (B) virtual base of the derived class D.
248 Index_t getVbaseOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000249 qD = qD->getPointeeType();
250 qB = qB->getPointeeType();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000251 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
252 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
253 if (D != Class)
254 return VBlookup(D, B);
255 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
256 i = VBIndex.find(B);
257 if (i != VBIndex.end())
258 return i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000259
Mike Stump28431212009-10-13 22:54:56 +0000260 assert(false && "FIXME: Base not found");
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000261 return 0;
262 }
263
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000264 bool OverrideMethod(GlobalDecl GD, llvm::Constant *m,
Mike Stump8bccbfd2009-10-15 09:30:16 +0000265 bool MorallyVirtual, Index_t OverrideOffset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000266 Index_t Offset, int64_t CurrentVBaseOffset) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000267 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
268
Mike Stump375faa82009-10-28 00:35:46 +0000269 const bool isPure = MD->isPure();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000270 typedef CXXMethodDecl::method_iterator meth_iter;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000271 // FIXME: Should OverrideOffset's be Offset?
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000272
273 // FIXME: Don't like the nested loops. For very large inheritance
274 // heirarchies we could have a table on the side with the final overridder
275 // and just replace each instance of an overridden method once. Would be
276 // nice to measure the cost/benefit on real code.
277
278 for (meth_iter mi = MD->begin_overridden_methods(),
279 e = MD->end_overridden_methods();
280 mi != e; ++mi) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000281 GlobalDecl OGD;
282
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000283 const CXXMethodDecl *OMD = *mi;
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000284 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(OMD))
285 OGD = GlobalDecl(DD, GD.getDtorType());
286 else
287 OGD = OMD;
288
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000289 llvm::Constant *om;
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000290 om = WrapAddrOf(OGD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000291 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
292
293 for (Index_t i = 0, e = submethods.size();
294 i != e; ++i) {
295 // FIXME: begin_overridden_methods might be too lax, covariance */
296 if (submethods[i] != om)
297 continue;
298 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
299 CanQualType oret = CGM.getContext().getCanonicalType(nc_oret);
300 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
301 CanQualType ret = CGM.getContext().getCanonicalType(nc_ret);
Anders Carlssond420a312009-11-26 19:32:45 +0000302 ThunkAdjustment ReturnAdjustment;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000303 if (oret != ret) {
304 // FIXME: calculate offsets for covariance
Anders Carlssond420a312009-11-26 19:32:45 +0000305 CovariantThunksMapTy::iterator i = CovariantThunks.find(OMD);
306 if (i != CovariantThunks.end()) {
307 oret = i->second.ReturnType;
308 CovariantThunks.erase(i);
Mike Stump87876a02009-10-13 10:55:21 +0000309 }
Mike Stump8bccbfd2009-10-15 09:30:16 +0000310 // FIXME: Double check oret
311 Index_t nv = getNVOffset(oret, ret)/8;
Anders Carlssond420a312009-11-26 19:32:45 +0000312 ReturnAdjustment = ThunkAdjustment(nv, getVbaseOffset(oret, ret));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000313 }
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000314 Index[GD] = i;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000315 submethods[i] = m;
Mike Stump375faa82009-10-28 00:35:46 +0000316 if (isPure)
Anders Carlssond420a312009-11-26 19:32:45 +0000317 PureVirtualMethods.insert(GD);
318 PureVirtualMethods.erase(OGD);
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000319 Thunks.erase(OGD);
320 if (MorallyVirtual || VCall.count(OGD)) {
321 Index_t &idx = VCall[OGD];
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000322 if (idx == 0) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000323 NonVirtualOffset[GD] = -OverrideOffset/8 + CurrentVBaseOffset/8;
324 VCallOffset[GD] = OverrideOffset/8;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000325 idx = VCalls.size()+1;
326 VCalls.push_back(0);
Mike Stump75ce5732009-10-31 20:06:59 +0000327 D1(printf(" vcall for %s at %d with delta %d most derived %s\n",
Mike Stumpc5a332c2009-11-13 23:45:53 +0000328 MD->getNameAsString().c_str(), (int)-idx-3,
329 (int)VCalls[idx-1], Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000330 } else {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000331 NonVirtualOffset[GD] = NonVirtualOffset[OGD];
332 VCallOffset[GD] = VCallOffset[OGD];
333 VCalls[idx-1] = -VCallOffset[OGD] + OverrideOffset/8;
Mike Stump75ce5732009-10-31 20:06:59 +0000334 D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n",
Mike Stumpc5a332c2009-11-13 23:45:53 +0000335 MD->getNameAsString().c_str(), (int)-idx-3,
336 (int)VCalls[idx-1], Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000337 }
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000338 VCall[GD] = idx;
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000339 int64_t NonVirtualAdjustment = NonVirtualOffset[GD];
340 int64_t VirtualAdjustment =
341 -((idx + extra + 2) * LLVMPointerWidth / 8);
342
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000343 // Optimize out virtual adjustments of 0.
344 if (VCalls[idx-1] == 0)
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000345 VirtualAdjustment = 0;
346
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000347 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment,
348 VirtualAdjustment);
349
Mike Stump37dbe962009-10-15 02:04:03 +0000350 // FIXME: Do we always have to build a covariant thunk to save oret,
351 // which is the containing virtual base class?
Anders Carlssond420a312009-11-26 19:32:45 +0000352 if (!ReturnAdjustment.isEmpty()) {
353 CovariantThunks[GD] =
354 CovariantThunk(i, ThisAdjustment, ReturnAdjustment, oret);
355 } else if (!isPure && !ThisAdjustment.isEmpty())
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000356 Thunks[GD] = Thunk(i, ThisAdjustment);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000357 return true;
358 }
Mike Stump28431212009-10-13 22:54:56 +0000359
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000360 // FIXME: finish off
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000361 int64_t NonVirtualAdjustment = VCallOffset[OGD] - OverrideOffset/8;
Mike Stump72431bd2009-11-06 02:38:24 +0000362
Anders Carlssond420a312009-11-26 19:32:45 +0000363 if (NonVirtualAdjustment || !ReturnAdjustment.isEmpty()) {
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000364 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, 0);
Mike Stump28431212009-10-13 22:54:56 +0000365
Anders Carlssond420a312009-11-26 19:32:45 +0000366 if (!ReturnAdjustment.isEmpty()) {
367 CovariantThunks[GD] =
368 CovariantThunk(i, ThisAdjustment, ReturnAdjustment, oret);
369 } else if (!isPure)
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000370 Thunks[GD] = Thunk(i, ThisAdjustment);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000371 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000372 return true;
373 }
374 }
375
376 return false;
377 }
378
379 void InstallThunks() {
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000380 for (ThunksMapTy::const_iterator i = Thunks.begin(), e = Thunks.end();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000381 i != e; ++i) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000382 GlobalDecl GD = i->first;
383 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000384 assert(!MD->isPure() && "Can't thunk pure virtual methods!");
385
386 const Thunk& Thunk = i->second;
387 assert(Thunk.Index == Index[GD] && "Thunk index mismatch!");
388
389 submethods[Thunk.Index] = CGM.BuildThunk(MD, Extern, Thunk.Adjustment);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000390 }
391 Thunks.clear();
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000392
Anders Carlssond420a312009-11-26 19:32:45 +0000393 for (CovariantThunksMapTy::const_iterator i = CovariantThunks.begin(),
394 e = CovariantThunks.end(); i != e; ++i) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000395 GlobalDecl GD = i->first;
396 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Mike Stump375faa82009-10-28 00:35:46 +0000397 if (MD->isPure())
398 continue;
Anders Carlsson2f87c4f2009-11-26 03:09:37 +0000399
Anders Carlssond420a312009-11-26 19:32:45 +0000400 const CovariantThunk &Thunk = i->second;
401 assert(Thunk.Index == Index[GD] && "Thunk index mismatch!");
402 submethods[Thunk.Index] =
403 CGM.BuildCovariantThunk(MD, Extern, Thunk.Adjustment);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000404 }
405 CovariantThunks.clear();
Anders Carlssond420a312009-11-26 19:32:45 +0000406
407 for (PureVirtualMethodsSetTy::iterator i = PureVirtualMethods.begin(),
408 e = PureVirtualMethods.end(); i != e; ++i) {
409 GlobalDecl GD = *i;
Anders Carlsson323bb042009-11-26 19:54:33 +0000410 submethods[Index[GD]] = getPureVirtualFn();
Mike Stumpbb9ff052009-10-27 23:46:47 +0000411 }
Anders Carlssond420a312009-11-26 19:32:45 +0000412 PureVirtualMethods.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000413 }
414
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000415 llvm::Constant *WrapAddrOf(GlobalDecl GD) {
416 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
417
Mike Stump18e8b472009-10-27 23:36:26 +0000418 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000419 return wrap(CGM.GetAddrOfCXXDestructor(Dtor, GD.getDtorType()));
Mike Stump18e8b472009-10-27 23:36:26 +0000420
Anders Carlsson64457732009-11-24 05:08:52 +0000421 const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD);
Mike Stump18e8b472009-10-27 23:36:26 +0000422
423 return wrap(CGM.GetAddrOfFunction(MD, Ty));
424 }
425
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000426 void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset,
427 int64_t CurrentVBaseOffset) {
Mike Stump37dbe962009-10-15 02:04:03 +0000428 for (Path_t::reverse_iterator i = Path->rbegin(),
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000429 e = Path->rend(); i != e; ++i) {
430 const CXXRecordDecl *RD = i->first;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000431 int64_t OverrideOffset = i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000432 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
433 ++mi) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000434 const CXXMethodDecl *MD = *mi;
435
436 if (!MD->isVirtual())
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000437 continue;
438
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000439 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
440 // Override both the complete and the deleting destructor.
441 GlobalDecl CompDtor(DD, Dtor_Complete);
442 OverrideMethod(CompDtor, WrapAddrOf(CompDtor), MorallyVirtual,
443 OverrideOffset, Offset, CurrentVBaseOffset);
444
445 GlobalDecl DeletingDtor(DD, Dtor_Deleting);
446 OverrideMethod(DeletingDtor, WrapAddrOf(DeletingDtor), MorallyVirtual,
447 OverrideOffset, Offset, CurrentVBaseOffset);
448 } else {
449 OverrideMethod(MD, WrapAddrOf(MD), MorallyVirtual, OverrideOffset,
450 Offset, CurrentVBaseOffset);
451 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000452 }
453 }
454 }
455
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000456 void AddMethod(const GlobalDecl GD, bool MorallyVirtual, Index_t Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000457 bool ForVirtualBase, int64_t CurrentVBaseOffset) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000458 llvm::Constant *m = WrapAddrOf(GD);
Mike Stump18e8b472009-10-27 23:36:26 +0000459
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000460 // If we can find a previously allocated slot for this, reuse it.
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000461 if (OverrideMethod(GD, m, MorallyVirtual, Offset, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000462 CurrentVBaseOffset))
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000463 return;
464
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000465 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
466
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000467 // else allocate a new slot.
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000468 Index[GD] = submethods.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000469 submethods.push_back(m);
Mike Stumpc5a332c2009-11-13 23:45:53 +0000470 D1(printf(" vfn for %s at %d\n", MD->getNameAsString().c_str(),
471 (int)Index[GD]));
Mike Stump375faa82009-10-28 00:35:46 +0000472 if (MD->isPure())
Anders Carlssond420a312009-11-26 19:32:45 +0000473 PureVirtualMethods.insert(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000474 if (MorallyVirtual) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000475 VCallOffset[GD] = Offset/8;
476 Index_t &idx = VCall[GD];
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000477 // Allocate the first one, after that, we reuse the previous one.
478 if (idx == 0) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000479 NonVirtualOffset[GD] = CurrentVBaseOffset/8 - Offset/8;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000480 idx = VCalls.size()+1;
481 VCalls.push_back(0);
Mike Stump75ce5732009-10-31 20:06:59 +0000482 D1(printf(" vcall for %s at %d with delta %d\n",
Mike Stumpc5a332c2009-11-13 23:45:53 +0000483 MD->getNameAsString().c_str(), (int)-VCalls.size()-3, 0));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000484 }
485 }
486 }
487
488 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000489 Index_t Offset, bool RDisVirtualBase,
490 int64_t CurrentVBaseOffset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000491 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000492 ++mi) {
493 const CXXMethodDecl *MD = *mi;
494 if (!MD->isVirtual())
495 continue;
496
497 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
498 // For destructors, add both the complete and the deleting destructor
499 // to the vtable.
500 AddMethod(GlobalDecl(DD, Dtor_Complete), MorallyVirtual, Offset,
501 RDisVirtualBase, CurrentVBaseOffset);
502 AddMethod(GlobalDecl(DD, Dtor_Deleting), MorallyVirtual, Offset,
503 RDisVirtualBase, CurrentVBaseOffset);
504 } else
505 AddMethod(MD, MorallyVirtual, Offset, RDisVirtualBase,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000506 CurrentVBaseOffset);
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000507 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000508 }
509
510 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
511 const CXXRecordDecl *PrimaryBase,
512 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000513 int64_t Offset, int64_t CurrentVBaseOffset,
514 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000515 Path->push_back(std::make_pair(RD, Offset));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000516 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
517 e = RD->bases_end(); i != e; ++i) {
518 if (i->isVirtual())
519 continue;
520 const CXXRecordDecl *Base =
521 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
522 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
523 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
524 StartNewTable();
Mike Stump653d0b92009-11-13 02:13:54 +0000525 GenerateVtableForBase(Base, o, MorallyVirtual, false,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000526 CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000527 }
528 }
Mike Stump37dbe962009-10-15 02:04:03 +0000529 Path->pop_back();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000530 }
531
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000532// #define D(X) do { X; } while (0)
533#define D(X)
534
535 void insertVCalls(int InsertionPoint) {
536 llvm::Constant *e = 0;
Mike Stump75ce5732009-10-31 20:06:59 +0000537 D1(printf("============= combining vbase/vcall\n"));
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000538 D(VCalls.insert(VCalls.begin(), 673));
539 D(VCalls.push_back(672));
Mike Stump8bccbfd2009-10-15 09:30:16 +0000540 methods.insert(methods.begin() + InsertionPoint, VCalls.size(), e);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000541 // The vcalls come first...
542 for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(),
543 e = VCalls.rend();
544 i != e; ++i)
545 methods[InsertionPoint++] = wrap((0?600:0) + *i);
546 VCalls.clear();
Mike Stump9f23a142009-11-10 02:30:51 +0000547 VCall.clear();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000548 }
549
Mike Stump559387f2009-11-13 23:13:20 +0000550 void AddAddressPoints(const CXXRecordDecl *RD, uint64_t Offset,
551 Index_t AddressPoint) {
552 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
553 RD->getNameAsCString(), Class->getNameAsCString(),
554 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stumpcd2b8212009-11-19 20:52:19 +0000555 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Mike Stump559387f2009-11-13 23:13:20 +0000556
557 // Now also add the address point for all our primary bases.
558 while (1) {
559 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
560 RD = Layout.getPrimaryBase();
561 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
562 // FIXME: Double check this.
563 if (RD == 0)
564 break;
565 if (PrimaryBaseWasVirtual &&
566 BLayout.getVBaseClassOffset(RD) != Offset)
567 break;
568 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
569 RD->getNameAsCString(), Class->getNameAsCString(),
570 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stumpcd2b8212009-11-19 20:52:19 +0000571 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Mike Stump559387f2009-11-13 23:13:20 +0000572 }
573 }
574
575
Mike Stump75ce5732009-10-31 20:06:59 +0000576 Index_t end(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
577 const CXXRecordDecl *PrimaryBase, bool PrimaryBaseWasVirtual,
578 bool MorallyVirtual, int64_t Offset, bool ForVirtualBase,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000579 int64_t CurrentVBaseOffset,
Mike Stump75ce5732009-10-31 20:06:59 +0000580 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000581 bool alloc = false;
582 if (Path == 0) {
583 alloc = true;
584 Path = new Path_t;
585 }
586
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000587 StartNewTable();
588 extra = 0;
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000589 bool DeferVCalls = MorallyVirtual || ForVirtualBase;
590 int VCallInsertionPoint = methods.size();
591 if (!DeferVCalls) {
592 insertVCalls(VCallInsertionPoint);
Mike Stump8bccbfd2009-10-15 09:30:16 +0000593 } else
594 // FIXME: just for extra, or for all uses of VCalls.size post this?
595 extra = -VCalls.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000596
Mike Stump653d0b92009-11-13 02:13:54 +0000597 methods.push_back(wrap(-((Offset-LayoutOffset)/8)));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000598 methods.push_back(rtti);
599 Index_t AddressPoint = methods.size();
600
601 InstallThunks();
Mike Stump75ce5732009-10-31 20:06:59 +0000602 D1(printf("============= combining methods\n"));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000603 methods.insert(methods.end(), submethods.begin(), submethods.end());
604 submethods.clear();
605
606 // and then the non-virtual bases.
607 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000608 MorallyVirtual, Offset, CurrentVBaseOffset, Path);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000609
610 if (ForVirtualBase) {
Mike Stump2cefe382009-11-12 20:47:57 +0000611 // FIXME: We're adding to VCalls in callers, we need to do the overrides
612 // in the inner part, so that we know the complete set of vcalls during
613 // the build and don't have to insert into methods. Saving out the
614 // AddressPoint here, would need to be fixed, if we didn't do that. Also
615 // retroactively adding vcalls for overrides later wind up in the wrong
616 // place, the vcall slot has to be alloted during the walk of the base
617 // when the function is first introduces.
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000618 AddressPoint += VCalls.size();
Mike Stump2cefe382009-11-12 20:47:57 +0000619 insertVCalls(VCallInsertionPoint);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000620 }
621
Mike Stump559387f2009-11-13 23:13:20 +0000622 AddAddressPoints(RD, Offset, AddressPoint);
Mike Stump2cefe382009-11-12 20:47:57 +0000623
Mike Stump37dbe962009-10-15 02:04:03 +0000624 if (alloc) {
625 delete Path;
626 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000627 return AddressPoint;
628 }
629
Mike Stump75ce5732009-10-31 20:06:59 +0000630 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
631 bool updateVBIndex, Index_t current_vbindex,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000632 bool RDisVirtualBase, int64_t CurrentVBaseOffset) {
Mike Stump75ce5732009-10-31 20:06:59 +0000633 if (!RD->isDynamicClass())
634 return;
635
636 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
637 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
638 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
639
640 // vtables are composed from the chain of primaries.
641 if (PrimaryBase) {
642 D1(printf(" doing primaries for %s most derived %s\n",
643 RD->getNameAsCString(), Class->getNameAsCString()));
644
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000645 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
646 if (PrimaryBaseWasVirtual)
647 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
648
Mike Stump75ce5732009-10-31 20:06:59 +0000649 if (!PrimaryBaseWasVirtual)
650 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000651 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
652 BaseCurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000653 }
654
655 D1(printf(" doing vcall entries for %s most derived %s\n",
656 RD->getNameAsCString(), Class->getNameAsCString()));
657
658 // And add the virtuals for the class to the primary vtable.
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000659 AddMethods(RD, MorallyVirtual, Offset, RDisVirtualBase, CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000660 }
661
662 void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
663 bool updateVBIndex, Index_t current_vbindex,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000664 bool RDisVirtualBase, int64_t CurrentVBaseOffset,
665 bool bottom=false) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000666 if (!RD->isDynamicClass())
667 return;
668
669 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
670 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
671 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
672
673 // vtables are composed from the chain of primaries.
674 if (PrimaryBase) {
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000675 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
676 if (PrimaryBaseWasVirtual) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000677 IndirectPrimary.insert(PrimaryBase);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000678 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
679 }
Mike Stump75ce5732009-10-31 20:06:59 +0000680
681 D1(printf(" doing primaries for %s most derived %s\n",
682 RD->getNameAsCString(), Class->getNameAsCString()));
683
684 VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000685 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
686 BaseCurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000687 }
688
Mike Stump75ce5732009-10-31 20:06:59 +0000689 D1(printf(" doing vbase entries for %s most derived %s\n",
690 RD->getNameAsCString(), Class->getNameAsCString()));
691 GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex);
692
693 if (RDisVirtualBase || bottom) {
694 Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000695 RDisVirtualBase, CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000696 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000697 }
698
Mike Stump653d0b92009-11-13 02:13:54 +0000699 int64_t GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0,
700 bool MorallyVirtual = false,
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000701 bool ForVirtualBase = false,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000702 int CurrentVBaseOffset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +0000703 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000704 if (!RD->isDynamicClass())
705 return 0;
706
Mike Stumpfa818082009-11-13 02:35:38 +0000707 // Construction vtable don't need parts that have no virtual bases and
708 // aren't morally virtual.
709 if ((LayoutClass != Class) && RD->getNumVBases() == 0 && !MorallyVirtual)
710 return 0;
711
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000712 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
713 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
714 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
715
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000716 extra = 0;
Mike Stump75ce5732009-10-31 20:06:59 +0000717 D1(printf("building entries for base %s most derived %s\n",
718 RD->getNameAsCString(), Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000719
Mike Stump75ce5732009-10-31 20:06:59 +0000720 if (ForVirtualBase)
721 extra = VCalls.size();
722
723 VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0, ForVirtualBase,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000724 CurrentVBaseOffset, true);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000725
726 if (Path)
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000727 OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000728
Mike Stump75ce5732009-10-31 20:06:59 +0000729 return end(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000730 Offset, ForVirtualBase, CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000731 }
732
733 void GenerateVtableForVBases(const CXXRecordDecl *RD,
734 int64_t Offset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +0000735 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000736 bool alloc = false;
737 if (Path == 0) {
738 alloc = true;
Mike Stump37dbe962009-10-15 02:04:03 +0000739 Path = new Path_t;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000740 }
741 // FIXME: We also need to override using all paths to a virtual base,
742 // right now, we just process the first path
743 Path->push_back(std::make_pair(RD, Offset));
744 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
745 e = RD->bases_end(); i != e; ++i) {
746 const CXXRecordDecl *Base =
747 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
748 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
749 // Mark it so we don't output it twice.
750 IndirectPrimary.insert(Base);
751 StartNewTable();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000752 VCall.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000753 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000754 int64_t CurrentVBaseOffset = BaseOffset;
Mike Stump75ce5732009-10-31 20:06:59 +0000755 D1(printf("vtable %s virtual base %s\n",
756 Class->getNameAsCString(), Base->getNameAsCString()));
Mike Stump653d0b92009-11-13 02:13:54 +0000757 GenerateVtableForBase(Base, BaseOffset, true, true, CurrentVBaseOffset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000758 Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000759 }
Mike Stump2cefe382009-11-12 20:47:57 +0000760 int64_t BaseOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000761 if (i->isVirtual())
762 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump2cefe382009-11-12 20:47:57 +0000763 else {
764 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
765 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
766 }
767
Mike Stump37dbe962009-10-15 02:04:03 +0000768 if (Base->getNumVBases()) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000769 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump37dbe962009-10-15 02:04:03 +0000770 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000771 }
772 Path->pop_back();
773 if (alloc)
774 delete Path;
775 }
776};
777
778
779VtableBuilder::Index_t VtableBuilder::VBlookup(CXXRecordDecl *D,
780 CXXRecordDecl *B) {
781 return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B);
782}
783
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000784int64_t CGVtableInfo::getMethodVtableIndex(GlobalDecl GD) {
785 MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000786 if (I != MethodVtableIndices.end())
787 return I->second;
788
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000789 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000790
791 std::vector<llvm::Constant *> methods;
792 // FIXME: This seems expensive. Can we do a partial job to get
793 // just this data.
Mike Stump653d0b92009-11-13 02:13:54 +0000794 VtableBuilder b(methods, RD, RD, 0, CGM);
Mike Stump75ce5732009-10-31 20:06:59 +0000795 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000796 b.GenerateVtableForBase(RD);
797 b.GenerateVtableForVBases(RD);
798
799 MethodVtableIndices.insert(b.getIndex().begin(),
800 b.getIndex().end());
801
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000802 I = MethodVtableIndices.find(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000803 assert(I != MethodVtableIndices.end() && "Did not find index!");
804 return I->second;
805}
806
807int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
808 const CXXRecordDecl *VBase) {
809 ClassPairTy ClassPair(RD, VBase);
810
811 VirtualBaseClassIndiciesTy::iterator I =
812 VirtualBaseClassIndicies.find(ClassPair);
813 if (I != VirtualBaseClassIndicies.end())
814 return I->second;
815
816 std::vector<llvm::Constant *> methods;
817 // FIXME: This seems expensive. Can we do a partial job to get
818 // just this data.
Mike Stump653d0b92009-11-13 02:13:54 +0000819 VtableBuilder b(methods, RD, RD, 0, CGM);
Mike Stump75ce5732009-10-31 20:06:59 +0000820 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000821 b.GenerateVtableForBase(RD);
822 b.GenerateVtableForVBases(RD);
823
824 for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
825 b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) {
826 // Insert all types.
827 ClassPairTy ClassPair(RD, I->first);
828
829 VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second));
830 }
831
832 I = VirtualBaseClassIndicies.find(ClassPair);
833 assert(I != VirtualBaseClassIndicies.end() && "Did not find index!");
834
835 return I->second;
836}
837
Mike Stump2cefe382009-11-12 20:47:57 +0000838llvm::Constant *CodeGenModule::GenerateVtable(const CXXRecordDecl *LayoutClass,
839 const CXXRecordDecl *RD,
Mike Stumpeac45592009-11-11 20:26:26 +0000840 uint64_t Offset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000841 llvm::SmallString<256> OutName;
Mike Stump2cefe382009-11-12 20:47:57 +0000842 if (LayoutClass != RD)
Daniel Dunbare128dd12009-11-21 09:06:22 +0000843 getMangleContext().mangleCXXCtorVtable(LayoutClass, Offset/8, RD, OutName);
Mike Stumpeac45592009-11-11 20:26:26 +0000844 else
Daniel Dunbare128dd12009-11-21 09:06:22 +0000845 getMangleContext().mangleCXXVtable(RD, OutName);
846 llvm::StringRef Name = OutName.str();
Benjamin Kramerbb0a07b2009-10-11 22:57:54 +0000847
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000848 std::vector<llvm::Constant *> methods;
849 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
850 int64_t AddressPoint;
851
Mike Stumpaa51ad62009-11-19 04:04:36 +0000852 llvm::GlobalVariable *GV = getModule().getGlobalVariable(Name);
Mike Stumpcd2b8212009-11-19 20:52:19 +0000853 if (GV && AddressPoints[LayoutClass] && !GV->isDeclaration()) {
Mike Stumpaa51ad62009-11-19 04:04:36 +0000854 AddressPoint=(*(*(AddressPoints[LayoutClass]))[RD])[std::make_pair(RD,
855 Offset)];
Mike Stumpcd2b8212009-11-19 20:52:19 +0000856 // FIXME: We can never have 0 address point. Do this for now so gepping
857 // retains the same structure. Later, we'll just assert.
858 if (AddressPoint == 0)
859 AddressPoint = 1;
860 } else {
Mike Stumpaa51ad62009-11-19 04:04:36 +0000861 VtableBuilder b(methods, RD, LayoutClass, Offset, *this);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000862
Mike Stumpaa51ad62009-11-19 04:04:36 +0000863 D1(printf("vtable %s\n", RD->getNameAsCString()));
864 // First comes the vtables for all the non-virtual bases...
865 AddressPoint = b.GenerateVtableForBase(RD, Offset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000866
Mike Stumpaa51ad62009-11-19 04:04:36 +0000867 // then the vtables for all the virtual bases.
868 b.GenerateVtableForVBases(RD, Offset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000869
Mike Stumpaa51ad62009-11-19 04:04:36 +0000870 bool CreateDefinition = true;
871 if (LayoutClass != RD)
872 CreateDefinition = true;
873 else {
874 // We have to convert it to have a record layout.
875 Types.ConvertTagDeclType(LayoutClass);
876 const CGRecordLayout &CGLayout = Types.getCGRecordLayout(LayoutClass);
877 if (const CXXMethodDecl *KeyFunction = CGLayout.getKeyFunction()) {
878 if (!KeyFunction->getBody()) {
879 // If there is a KeyFunction, and it isn't defined, just build a
880 // reference to the vtable.
881 CreateDefinition = false;
882 }
883 }
884 }
885
886 llvm::Constant *C = 0;
887 llvm::Type *type = Ptr8Ty;
888 llvm::GlobalVariable::LinkageTypes linktype
889 = llvm::GlobalValue::ExternalLinkage;
890 if (CreateDefinition) {
891 llvm::ArrayType *ntype = llvm::ArrayType::get(Ptr8Ty, methods.size());
892 C = llvm::ConstantArray::get(ntype, methods);
893 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
894 if (LayoutClass->isInAnonymousNamespace())
895 linktype = llvm::GlobalValue::InternalLinkage;
896 type = ntype;
897 }
898 llvm::GlobalVariable *OGV = GV;
899 GV = new llvm::GlobalVariable(getModule(), type, true, linktype, C, Name);
900 if (OGV) {
901 GV->takeName(OGV);
902 llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
903 OGV->getType());
904 OGV->replaceAllUsesWith(NewPtr);
905 OGV->eraseFromParent();
906 }
907 bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden;
908 if (Hidden)
909 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
910 }
Mike Stumpc0f632d2009-11-18 04:00:48 +0000911 llvm::Constant *vtable = llvm::ConstantExpr::getBitCast(GV, Ptr8Ty);
Mike Stumpd846d082009-11-10 07:44:33 +0000912 llvm::Constant *AddressPointC;
913 uint32_t LLVMPointerWidth = getContext().Target.getPointerWidth(0);
914 AddressPointC = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
915 AddressPoint*LLVMPointerWidth/8);
Mike Stump2cefe382009-11-12 20:47:57 +0000916 vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(vtable, &AddressPointC,
917 1);
Mike Stumpd846d082009-11-10 07:44:33 +0000918
Mike Stumpcd2b8212009-11-19 20:52:19 +0000919 assert(vtable->getType() == Ptr8Ty);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000920 return vtable;
921}
Mike Stump9f23a142009-11-10 02:30:51 +0000922
923class VTTBuilder {
924 /// Inits - The list of values built for the VTT.
925 std::vector<llvm::Constant *> &Inits;
926 /// Class - The most derived class that this vtable is being built for.
927 const CXXRecordDecl *Class;
928 CodeGenModule &CGM; // Per-module state.
Mike Stump8b2d2d02009-11-11 00:35:07 +0000929 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
Mike Stumpc7b9f5e2009-11-11 03:08:24 +0000930 /// BLayout - Layout for the most derived class that this vtable is being
931 /// built for.
932 const ASTRecordLayout &BLayout;
Mike Stump83066c82009-11-13 01:54:23 +0000933 CodeGenModule::AddrMap_t &AddressPoints;
Mike Stump2cefe382009-11-12 20:47:57 +0000934 // vtbl - A pointer to the vtable for Class.
935 llvm::Constant *ClassVtbl;
936 llvm::LLVMContext &VMContext;
Mike Stump9f23a142009-11-10 02:30:51 +0000937
Mike Stump8677bc22009-11-12 22:56:32 +0000938 /// BuildVtablePtr - Build up a referene to the given secondary vtable
Mike Stump83066c82009-11-13 01:54:23 +0000939 llvm::Constant *BuildVtablePtr(llvm::Constant *vtbl,
940 const CXXRecordDecl *VtblClass,
941 const CXXRecordDecl *RD,
Mike Stump8677bc22009-11-12 22:56:32 +0000942 uint64_t Offset) {
943 int64_t AddressPoint;
Mike Stump83066c82009-11-13 01:54:23 +0000944 AddressPoint = (*AddressPoints[VtblClass])[std::make_pair(RD, Offset)];
Mike Stump2b34bc52009-11-12 23:36:21 +0000945 // FIXME: We can never have 0 address point. Do this for now so gepping
Mike Stumpcd2b8212009-11-19 20:52:19 +0000946 // retains the same structure. Later we'll just assert.
Mike Stump2b34bc52009-11-12 23:36:21 +0000947 if (AddressPoint == 0)
948 AddressPoint = 1;
Mike Stump83066c82009-11-13 01:54:23 +0000949 D1(printf("XXX address point for %s in %s layout %s at offset %d was %d\n",
950 RD->getNameAsCString(), VtblClass->getNameAsCString(),
951 Class->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stump8677bc22009-11-12 22:56:32 +0000952 uint32_t LLVMPointerWidth = CGM.getContext().Target.getPointerWidth(0);
953 llvm::Constant *init;
954 init = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
955 AddressPoint*LLVMPointerWidth/8);
956 init = llvm::ConstantExpr::getInBoundsGetElementPtr(vtbl, &init, 1);
957 return init;
958 }
959
Mike Stump2cefe382009-11-12 20:47:57 +0000960 /// Secondary - Add the secondary vtable pointers to Inits. Offset is the
961 /// current offset in bits to the object we're working on.
Mike Stump8677bc22009-11-12 22:56:32 +0000962 void Secondary(const CXXRecordDecl *RD, llvm::Constant *vtbl,
Mike Stump83066c82009-11-13 01:54:23 +0000963 const CXXRecordDecl *VtblClass, uint64_t Offset=0,
964 bool MorallyVirtual=false) {
Mike Stump8b2d2d02009-11-11 00:35:07 +0000965 if (RD->getNumVBases() == 0 && ! MorallyVirtual)
966 return;
967
968 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
969 e = RD->bases_end(); i != e; ++i) {
970 const CXXRecordDecl *Base =
971 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
972 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
973 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
974 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
975 bool NonVirtualPrimaryBase;
976 NonVirtualPrimaryBase = !PrimaryBaseWasVirtual && Base == PrimaryBase;
977 bool BaseMorallyVirtual = MorallyVirtual | i->isVirtual();
Mike Stumpc7b9f5e2009-11-11 03:08:24 +0000978 uint64_t BaseOffset;
979 if (!i->isVirtual()) {
980 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
981 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
982 } else
983 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump2b34bc52009-11-12 23:36:21 +0000984 llvm::Constant *subvtbl = vtbl;
Mike Stump83066c82009-11-13 01:54:23 +0000985 const CXXRecordDecl *subVtblClass = VtblClass;
Mike Stump8b2d2d02009-11-11 00:35:07 +0000986 if ((Base->getNumVBases() || BaseMorallyVirtual)
987 && !NonVirtualPrimaryBase) {
988 // FIXME: Slightly too many of these for __ZTT8test8_B2
Mike Stump8677bc22009-11-12 22:56:32 +0000989 llvm::Constant *init;
Mike Stump2b34bc52009-11-12 23:36:21 +0000990 if (BaseMorallyVirtual)
Mike Stump83066c82009-11-13 01:54:23 +0000991 init = BuildVtablePtr(vtbl, VtblClass, RD, Offset);
Mike Stump2b34bc52009-11-12 23:36:21 +0000992 else {
Mike Stump8677bc22009-11-12 22:56:32 +0000993 init = CGM.getVtableInfo().getCtorVtable(Class, Base, BaseOffset);
Mike Stump2b34bc52009-11-12 23:36:21 +0000994 subvtbl = dyn_cast<llvm::Constant>(init->getOperand(0));
Mike Stump83066c82009-11-13 01:54:23 +0000995 subVtblClass = Base;
Mike Stump2b34bc52009-11-12 23:36:21 +0000996 }
Mike Stump8677bc22009-11-12 22:56:32 +0000997 Inits.push_back(init);
Mike Stump8b2d2d02009-11-11 00:35:07 +0000998 }
Mike Stump83066c82009-11-13 01:54:23 +0000999 Secondary(Base, subvtbl, subVtblClass, BaseOffset, BaseMorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001000 }
1001 }
1002
Mike Stump2cefe382009-11-12 20:47:57 +00001003 /// BuiltVTT - Add the VTT to Inits. Offset is the offset in bits to the
1004 /// currnet object we're working on.
1005 void BuildVTT(const CXXRecordDecl *RD, uint64_t Offset, bool MorallyVirtual) {
Mike Stump8b2d2d02009-11-11 00:35:07 +00001006 if (RD->getNumVBases() == 0 && !MorallyVirtual)
1007 return;
1008
Mike Stump2cefe382009-11-12 20:47:57 +00001009 llvm::Constant *init;
Mike Stump83066c82009-11-13 01:54:23 +00001010 const CXXRecordDecl *VtblClass;
1011
Mike Stump8b2d2d02009-11-11 00:35:07 +00001012 // First comes the primary virtual table pointer...
Mike Stump83066c82009-11-13 01:54:23 +00001013 if (MorallyVirtual) {
1014 init = BuildVtablePtr(ClassVtbl, Class, RD, Offset);
1015 VtblClass = Class;
1016 } else {
Mike Stump2cefe382009-11-12 20:47:57 +00001017 init = CGM.getVtableInfo().getCtorVtable(Class, RD, Offset);
Mike Stump83066c82009-11-13 01:54:23 +00001018 VtblClass = RD;
1019 }
Mike Stump8677bc22009-11-12 22:56:32 +00001020 llvm::Constant *vtbl = dyn_cast<llvm::Constant>(init->getOperand(0));
Mike Stump2cefe382009-11-12 20:47:57 +00001021 Inits.push_back(init);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001022
1023 // then the secondary VTTs....
Mike Stump2cefe382009-11-12 20:47:57 +00001024 SecondaryVTTs(RD, Offset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001025
1026 // and last the secondary vtable pointers.
Mike Stump83066c82009-11-13 01:54:23 +00001027 Secondary(RD, vtbl, VtblClass, Offset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001028 }
1029
1030 /// SecondaryVTTs - Add the secondary VTTs to Inits. The secondary VTTs are
1031 /// built from each direct non-virtual proper base that requires a VTT in
1032 /// declaration order.
Mike Stump2cefe382009-11-12 20:47:57 +00001033 void SecondaryVTTs(const CXXRecordDecl *RD, uint64_t Offset=0,
1034 bool MorallyVirtual=false) {
Mike Stump8b2d2d02009-11-11 00:35:07 +00001035 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1036 e = RD->bases_end(); i != e; ++i) {
1037 const CXXRecordDecl *Base =
1038 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1039 if (i->isVirtual())
1040 continue;
Mike Stump2cefe382009-11-12 20:47:57 +00001041 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1042 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base);
1043 BuildVTT(Base, BaseOffset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001044 }
1045 }
1046
1047 /// VirtualVTTs - Add the VTT for each proper virtual base in inheritance
1048 /// graph preorder.
1049 void VirtualVTTs(const CXXRecordDecl *RD) {
1050 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1051 e = RD->bases_end(); i != e; ++i) {
1052 const CXXRecordDecl *Base =
1053 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1054 if (i->isVirtual() && !SeenVBase.count(Base)) {
1055 SeenVBase.insert(Base);
Mike Stump2cefe382009-11-12 20:47:57 +00001056 uint64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
1057 BuildVTT(Base, BaseOffset, true);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001058 }
1059 VirtualVTTs(Base);
1060 }
1061 }
Mike Stump9f23a142009-11-10 02:30:51 +00001062public:
1063 VTTBuilder(std::vector<llvm::Constant *> &inits, const CXXRecordDecl *c,
Mike Stumpc7b9f5e2009-11-11 03:08:24 +00001064 CodeGenModule &cgm)
1065 : Inits(inits), Class(c), CGM(cgm),
Mike Stump2cefe382009-11-12 20:47:57 +00001066 BLayout(cgm.getContext().getASTRecordLayout(c)),
Mike Stump83066c82009-11-13 01:54:23 +00001067 AddressPoints(*cgm.AddressPoints[c]),
Mike Stump2cefe382009-11-12 20:47:57 +00001068 VMContext(cgm.getModule().getContext()) {
Mike Stumpd846d082009-11-10 07:44:33 +00001069
Mike Stump8b2d2d02009-11-11 00:35:07 +00001070 // First comes the primary virtual table pointer for the complete class...
Mike Stump2cefe382009-11-12 20:47:57 +00001071 ClassVtbl = CGM.getVtableInfo().getVtable(Class);
1072 Inits.push_back(ClassVtbl);
1073 ClassVtbl = dyn_cast<llvm::Constant>(ClassVtbl->getOperand(0));
1074
Mike Stump8b2d2d02009-11-11 00:35:07 +00001075 // then the secondary VTTs...
1076 SecondaryVTTs(Class);
1077
1078 // then the secondary vtable pointers...
Mike Stump83066c82009-11-13 01:54:23 +00001079 Secondary(Class, ClassVtbl, Class);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001080
1081 // and last, the virtual VTTs.
1082 VirtualVTTs(Class);
Mike Stump9f23a142009-11-10 02:30:51 +00001083 }
1084};
1085
Mike Stumpd846d082009-11-10 07:44:33 +00001086llvm::Constant *CodeGenModule::GenerateVTT(const CXXRecordDecl *RD) {
Mike Stumpb4722212009-11-10 19:13:04 +00001087 // Only classes that have virtual bases need a VTT.
1088 if (RD->getNumVBases() == 0)
1089 return 0;
1090
Mike Stump9f23a142009-11-10 02:30:51 +00001091 llvm::SmallString<256> OutName;
Daniel Dunbare128dd12009-11-21 09:06:22 +00001092 getMangleContext().mangleCXXVTT(RD, OutName);
1093 llvm::StringRef Name = OutName.str();
Mike Stump9f23a142009-11-10 02:30:51 +00001094
1095 llvm::GlobalVariable::LinkageTypes linktype;
1096 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
Mike Stumpaa51ad62009-11-19 04:04:36 +00001097 if (RD->isInAnonymousNamespace())
1098 linktype = llvm::GlobalValue::InternalLinkage;
Mike Stump9f23a142009-11-10 02:30:51 +00001099 std::vector<llvm::Constant *> inits;
1100 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1101
Mike Stump9f23a142009-11-10 02:30:51 +00001102 D1(printf("vtt %s\n", RD->getNameAsCString()));
1103
Mike Stump8b2d2d02009-11-11 00:35:07 +00001104 VTTBuilder b(inits, RD, *this);
1105
Mike Stump9f23a142009-11-10 02:30:51 +00001106 llvm::Constant *C;
1107 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, inits.size());
1108 C = llvm::ConstantArray::get(type, inits);
Mike Stumpc0f632d2009-11-18 04:00:48 +00001109 llvm::GlobalVariable *vtt = new llvm::GlobalVariable(getModule(), type, true,
Mike Stumpaa51ad62009-11-19 04:04:36 +00001110 linktype, C, Name);
Mike Stumpc0f632d2009-11-18 04:00:48 +00001111 bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden;
1112 if (Hidden)
1113 vtt->setVisibility(llvm::GlobalVariable::HiddenVisibility);
1114 return llvm::ConstantExpr::getBitCast(vtt, Ptr8Ty);
Mike Stump9f23a142009-11-10 02:30:51 +00001115}
Mike Stumpd846d082009-11-10 07:44:33 +00001116
Mike Stump1a139f82009-11-19 01:08:19 +00001117void CGVtableInfo::GenerateClassData(const CXXRecordDecl *RD) {
1118 Vtables[RD] = CGM.GenerateVtable(RD, RD);
1119 CGM.GenerateRtti(RD);
1120 CGM.GenerateVTT(RD);
1121}
1122
Mike Stumpeac45592009-11-11 20:26:26 +00001123llvm::Constant *CGVtableInfo::getVtable(const CXXRecordDecl *RD) {
Mike Stumpd846d082009-11-10 07:44:33 +00001124 llvm::Constant *&vtbl = Vtables[RD];
1125 if (vtbl)
1126 return vtbl;
Mike Stump2cefe382009-11-12 20:47:57 +00001127 vtbl = CGM.GenerateVtable(RD, RD);
Mike Stumpaa51ad62009-11-19 04:04:36 +00001128
1129 bool CreateDefinition = true;
1130 // We have to convert it to have a record layout.
1131 CGM.getTypes().ConvertTagDeclType(RD);
1132 const CGRecordLayout &CGLayout = CGM.getTypes().getCGRecordLayout(RD);
1133 if (const CXXMethodDecl *KeyFunction = CGLayout.getKeyFunction()) {
1134 if (!KeyFunction->getBody()) {
1135 // If there is a KeyFunction, and it isn't defined, just build a
1136 // reference to the vtable.
1137 CreateDefinition = false;
1138 }
1139 }
1140
1141 if (CreateDefinition) {
1142 CGM.GenerateRtti(RD);
1143 CGM.GenerateVTT(RD);
1144 }
Mike Stumpd846d082009-11-10 07:44:33 +00001145 return vtbl;
1146}
Mike Stumpeac45592009-11-11 20:26:26 +00001147
Mike Stump2cefe382009-11-12 20:47:57 +00001148llvm::Constant *CGVtableInfo::getCtorVtable(const CXXRecordDecl *LayoutClass,
1149 const CXXRecordDecl *RD,
Mike Stumpeac45592009-11-11 20:26:26 +00001150 uint64_t Offset) {
Mike Stump2cefe382009-11-12 20:47:57 +00001151 return CGM.GenerateVtable(LayoutClass, RD, Offset);
Mike Stumpeac45592009-11-11 20:26:26 +00001152}