blob: f1e87aea496b6c000785f2bc7a7ef8cfdd20a3e2 [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"
Zhongxing Xu1721ef72009-11-13 05:46:16 +000018#include <cstdio>
Anders Carlsson2bb27f52009-10-11 22:13:54 +000019
20using namespace clang;
21using namespace CodeGen;
22
23class VtableBuilder {
24public:
25 /// Index_t - Vtable index type.
26 typedef uint64_t Index_t;
27private:
28 std::vector<llvm::Constant *> &methods;
29 std::vector<llvm::Constant *> submethods;
30 llvm::Type *Ptr8Ty;
31 /// Class - The most derived class that this vtable is being built for.
32 const CXXRecordDecl *Class;
Mike Stump83066c82009-11-13 01:54:23 +000033 /// LayoutClass - The most derived class used for virtual base layout
34 /// information.
35 const CXXRecordDecl *LayoutClass;
Mike Stump653d0b92009-11-13 02:13:54 +000036 /// LayoutOffset - The offset for Class in LayoutClass.
37 uint64_t LayoutOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000038 /// BLayout - Layout for the most derived class that this vtable is being
39 /// built for.
40 const ASTRecordLayout &BLayout;
41 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
42 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
43 llvm::Constant *rtti;
44 llvm::LLVMContext &VMContext;
45 CodeGenModule &CGM; // Per-module state.
46 /// Index - Maps a method decl into a vtable index. Useful for virtual
47 /// dispatch codegen.
Anders Carlssonfb4dda42009-11-13 17:08:56 +000048 llvm::DenseMap<GlobalDecl, Index_t> Index;
49 llvm::DenseMap<GlobalDecl, Index_t> VCall;
50 llvm::DenseMap<GlobalDecl, Index_t> VCallOffset;
Mike Stumpcd6f9ed2009-11-06 23:27:42 +000051 // This is the offset to the nearest virtual base
Anders Carlssonfb4dda42009-11-13 17:08:56 +000052 llvm::DenseMap<GlobalDecl, Index_t> NonVirtualOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000053 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stumpbb9ff052009-10-27 23:46:47 +000054
Anders Carlsson6d771bc2009-11-26 03:25:13 +000055 /// Thunk - Represents a single thunk.
56 struct Thunk {
57 Thunk()
58 : Index(0) { }
59
60 Thunk(uint64_t Index, const ThunkAdjustment &Adjustment)
61 : Index(Index), Adjustment(Adjustment) { }
62
63 /// Index - The index in the vtable.
64 uint64_t Index;
65
66 /// ThisAdjustment - The thunk adjustment.
67 ThunkAdjustment Adjustment;
68 };
69
Anders Carlssonfb4dda42009-11-13 17:08:56 +000070 typedef llvm::DenseMap<GlobalDecl, int> Pures_t;
Mike Stumpbb9ff052009-10-27 23:46:47 +000071 Pures_t Pures;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000072 typedef std::pair<Index_t, Index_t> CallOffset;
Anders Carlsson6d771bc2009-11-26 03:25:13 +000073
74 typedef llvm::DenseMap<GlobalDecl, Thunk> ThunksMapTy;
75 ThunksMapTy Thunks;
76
Anders Carlssonfb4dda42009-11-13 17:08:56 +000077 typedef llvm::DenseMap<GlobalDecl,
Mike Stump87876a02009-10-13 10:55:21 +000078 std::pair<std::pair<CallOffset, CallOffset>,
79 CanQualType> > CovariantThunks_t;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000080 CovariantThunks_t CovariantThunks;
81 std::vector<Index_t> VCalls;
Mike Stump2cefe382009-11-12 20:47:57 +000082
83 typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t;
Mike Stumpcd2b8212009-11-19 20:52:19 +000084 // subAddressPoints - Used to hold the AddressPoints (offsets) into the built
85 // vtable for use in computing the initializers for the VTT.
86 llvm::DenseMap<CtorVtable_t, int64_t> &subAddressPoints;
Mike Stump2cefe382009-11-12 20:47:57 +000087
Anders Carlsson2bb27f52009-10-11 22:13:54 +000088 typedef CXXRecordDecl::method_iterator method_iter;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000089 const bool Extern;
90 const uint32_t LLVMPointerWidth;
91 Index_t extra;
Mike Stump37dbe962009-10-15 02:04:03 +000092 typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t;
Mike Stumpbb9ff052009-10-27 23:46:47 +000093 llvm::Constant *cxa_pure;
Mike Stumpcd2b8212009-11-19 20:52:19 +000094 static llvm::DenseMap<CtorVtable_t, int64_t>&
95 AllocAddressPoint(CodeGenModule &cgm, const CXXRecordDecl *l,
96 const CXXRecordDecl *c) {
97 CodeGenModule::AddrMap_t *&oref = cgm.AddressPoints[l];
98 if (oref == 0)
99 oref = new CodeGenModule::AddrMap_t;
100
101 llvm::DenseMap<CtorVtable_t, int64_t> *&ref = (*oref)[c];
102 if (ref == 0)
103 ref = new llvm::DenseMap<CtorVtable_t, int64_t>;
104 return *ref;
105 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000106public:
Mike Stump653d0b92009-11-13 02:13:54 +0000107 VtableBuilder(std::vector<llvm::Constant *> &meth, const CXXRecordDecl *c,
108 const CXXRecordDecl *l, uint64_t lo, CodeGenModule &cgm)
109 : methods(meth), Class(c), LayoutClass(l), LayoutOffset(lo),
Mike Stump83066c82009-11-13 01:54:23 +0000110 BLayout(cgm.getContext().getASTRecordLayout(l)),
Mike Stumpf5b28692009-11-14 23:32:21 +0000111 rtti(cgm.GenerateRttiRef(c)), VMContext(cgm.getModule().getContext()),
Mike Stumpcd2b8212009-11-19 20:52:19 +0000112 CGM(cgm), subAddressPoints(AllocAddressPoint(cgm, l, c)),
Mike Stump1960b202009-11-19 00:49:05 +0000113 Extern(!l->isInAnonymousNamespace()),
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000114 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000115 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump375faa82009-10-28 00:35:46 +0000116
117 // Calculate pointer for ___cxa_pure_virtual.
118 const llvm::FunctionType *FTy;
119 std::vector<const llvm::Type*> ArgTys;
120 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
121 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
122 cxa_pure = wrap(CGM.CreateRuntimeFunction(FTy, "__cxa_pure_virtual"));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000123 }
124
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000125 llvm::DenseMap<GlobalDecl, Index_t> &getIndex() { return Index; }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000126 llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex()
127 { return VBIndex; }
128
129 llvm::Constant *wrap(Index_t i) {
130 llvm::Constant *m;
131 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
132 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
133 }
134
135 llvm::Constant *wrap(llvm::Constant *m) {
136 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
137 }
138
Mike Stump2b34bc52009-11-12 23:36:21 +0000139//#define D1(x)
140#define D1(X) do { if (getenv("DEBUG")) { X; } } while (0)
Mike Stump75ce5732009-10-31 20:06:59 +0000141
142 void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset,
Mike Stump28431212009-10-13 22:54:56 +0000143 bool updateVBIndex, Index_t current_vbindex) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000144 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
145 e = RD->bases_end(); i != e; ++i) {
146 const CXXRecordDecl *Base =
147 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump28431212009-10-13 22:54:56 +0000148 Index_t next_vbindex = current_vbindex;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000149 if (i->isVirtual() && !SeenVBase.count(Base)) {
150 SeenVBase.insert(Base);
Mike Stump28431212009-10-13 22:54:56 +0000151 if (updateVBIndex) {
Mike Stump75ce5732009-10-31 20:06:59 +0000152 next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8)
Mike Stump28431212009-10-13 22:54:56 +0000153 - 3*LLVMPointerWidth/8);
154 VBIndex[Base] = next_vbindex;
155 }
Mike Stump75ce5732009-10-31 20:06:59 +0000156 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
157 VCalls.push_back((0?700:0) + BaseOffset);
158 D1(printf(" vbase for %s at %d delta %d most derived %s\n",
159 Base->getNameAsCString(),
160 (int)-VCalls.size()-3, (int)BaseOffset,
161 Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000162 }
Mike Stump28431212009-10-13 22:54:56 +0000163 // We also record offsets for non-virtual bases to closest enclosing
164 // virtual base. We do this so that we don't have to search
165 // for the nearst virtual base class when generating thunks.
166 if (updateVBIndex && VBIndex.count(Base) == 0)
167 VBIndex[Base] = next_vbindex;
Mike Stump75ce5732009-10-31 20:06:59 +0000168 GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000169 }
170 }
171
172 void StartNewTable() {
173 SeenVBase.clear();
174 }
175
176 Index_t VBlookup(CXXRecordDecl *D, CXXRecordDecl *B);
177
Mike Stump8bccbfd2009-10-15 09:30:16 +0000178 Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B,
179 Index_t Offset = 0) {
180
181 if (B == D)
182 return Offset;
183
184 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D);
185 for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(),
186 e = D->bases_end(); i != e; ++i) {
187 const CXXRecordDecl *Base =
188 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
189 int64_t BaseOffset = 0;
190 if (!i->isVirtual())
191 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
192 int64_t o = getNVOffset_1(Base, B, BaseOffset);
193 if (o >= 0)
194 return o;
195 }
196
197 return -1;
198 }
199
200 /// getNVOffset - Returns the non-virtual offset for the given (B) base of the
201 /// derived class D.
202 Index_t getNVOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000203 qD = qD->getPointeeType();
204 qB = qB->getPointeeType();
Mike Stump8bccbfd2009-10-15 09:30:16 +0000205 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
206 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
207 int64_t o = getNVOffset_1(D, B);
208 if (o >= 0)
209 return o;
210
211 assert(false && "FIXME: non-virtual base not found");
212 return 0;
213 }
214
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000215 /// getVbaseOffset - Returns the index into the vtable for the virtual base
216 /// offset for the given (B) virtual base of the derived class D.
217 Index_t getVbaseOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000218 qD = qD->getPointeeType();
219 qB = qB->getPointeeType();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000220 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
221 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
222 if (D != Class)
223 return VBlookup(D, B);
224 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
225 i = VBIndex.find(B);
226 if (i != VBIndex.end())
227 return i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000228
Mike Stump28431212009-10-13 22:54:56 +0000229 assert(false && "FIXME: Base not found");
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000230 return 0;
231 }
232
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000233 bool OverrideMethod(GlobalDecl GD, llvm::Constant *m,
Mike Stump8bccbfd2009-10-15 09:30:16 +0000234 bool MorallyVirtual, Index_t OverrideOffset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000235 Index_t Offset, int64_t CurrentVBaseOffset) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000236 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
237
Mike Stump375faa82009-10-28 00:35:46 +0000238 const bool isPure = MD->isPure();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000239 typedef CXXMethodDecl::method_iterator meth_iter;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000240 // FIXME: Should OverrideOffset's be Offset?
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000241
242 // FIXME: Don't like the nested loops. For very large inheritance
243 // heirarchies we could have a table on the side with the final overridder
244 // and just replace each instance of an overridden method once. Would be
245 // nice to measure the cost/benefit on real code.
246
247 for (meth_iter mi = MD->begin_overridden_methods(),
248 e = MD->end_overridden_methods();
249 mi != e; ++mi) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000250 GlobalDecl OGD;
251
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000252 const CXXMethodDecl *OMD = *mi;
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000253 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(OMD))
254 OGD = GlobalDecl(DD, GD.getDtorType());
255 else
256 OGD = OMD;
257
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000258 llvm::Constant *om;
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000259 om = WrapAddrOf(OGD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000260 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
261
262 for (Index_t i = 0, e = submethods.size();
263 i != e; ++i) {
264 // FIXME: begin_overridden_methods might be too lax, covariance */
265 if (submethods[i] != om)
266 continue;
267 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
268 CanQualType oret = CGM.getContext().getCanonicalType(nc_oret);
269 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
270 CanQualType ret = CGM.getContext().getCanonicalType(nc_ret);
271 CallOffset ReturnOffset = std::make_pair(0, 0);
272 if (oret != ret) {
273 // FIXME: calculate offsets for covariance
Mike Stump87876a02009-10-13 10:55:21 +0000274 if (CovariantThunks.count(OMD)) {
275 oret = CovariantThunks[OMD].second;
276 CovariantThunks.erase(OMD);
277 }
Mike Stump8bccbfd2009-10-15 09:30:16 +0000278 // FIXME: Double check oret
279 Index_t nv = getNVOffset(oret, ret)/8;
Mike Stump87876a02009-10-13 10:55:21 +0000280 ReturnOffset = std::make_pair(nv, getVbaseOffset(oret, ret));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000281 }
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000282 Index[GD] = i;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000283 submethods[i] = m;
Mike Stump375faa82009-10-28 00:35:46 +0000284 if (isPure)
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000285 Pures[GD] = 1;
286 Pures.erase(OGD);
287 Thunks.erase(OGD);
288 if (MorallyVirtual || VCall.count(OGD)) {
289 Index_t &idx = VCall[OGD];
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000290 if (idx == 0) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000291 NonVirtualOffset[GD] = -OverrideOffset/8 + CurrentVBaseOffset/8;
292 VCallOffset[GD] = OverrideOffset/8;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000293 idx = VCalls.size()+1;
294 VCalls.push_back(0);
Mike Stump75ce5732009-10-31 20:06:59 +0000295 D1(printf(" vcall for %s at %d with delta %d most derived %s\n",
Mike Stumpc5a332c2009-11-13 23:45:53 +0000296 MD->getNameAsString().c_str(), (int)-idx-3,
297 (int)VCalls[idx-1], Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000298 } else {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000299 NonVirtualOffset[GD] = NonVirtualOffset[OGD];
300 VCallOffset[GD] = VCallOffset[OGD];
301 VCalls[idx-1] = -VCallOffset[OGD] + OverrideOffset/8;
Mike Stump75ce5732009-10-31 20:06:59 +0000302 D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n",
Mike Stumpc5a332c2009-11-13 23:45:53 +0000303 MD->getNameAsString().c_str(), (int)-idx-3,
304 (int)VCalls[idx-1], Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000305 }
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000306 VCall[GD] = idx;
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000307 int64_t NonVirtualAdjustment = NonVirtualOffset[GD];
308 int64_t VirtualAdjustment =
309 -((idx + extra + 2) * LLVMPointerWidth / 8);
310
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000311 // Optimize out virtual adjustments of 0.
312 if (VCalls[idx-1] == 0)
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000313 VirtualAdjustment = 0;
314
315 CallOffset ThisOffset = std::make_pair(NonVirtualAdjustment,
316 VirtualAdjustment);
317 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment,
318 VirtualAdjustment);
319
Mike Stump37dbe962009-10-15 02:04:03 +0000320 // FIXME: Do we always have to build a covariant thunk to save oret,
321 // which is the containing virtual base class?
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000322 if (ReturnOffset.first || ReturnOffset.second)
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000323 CovariantThunks[GD] = std::make_pair(std::make_pair(ThisOffset,
Mike Stump87876a02009-10-13 10:55:21 +0000324 ReturnOffset),
325 oret);
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000326 else if (!isPure && !ThisAdjustment.isEmpty())
327 Thunks[GD] = Thunk(i, ThisAdjustment);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000328 return true;
329 }
Mike Stump28431212009-10-13 22:54:56 +0000330
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000331 // FIXME: finish off
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000332 int64_t NonVirtualAdjustment = VCallOffset[OGD] - OverrideOffset/8;
Mike Stump72431bd2009-11-06 02:38:24 +0000333
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000334 if (NonVirtualAdjustment || ReturnOffset.first || ReturnOffset.second) {
335 CallOffset ThisOffset = std::make_pair(NonVirtualAdjustment, 0);
336 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, 0);
Mike Stump28431212009-10-13 22:54:56 +0000337
338 if (ReturnOffset.first || ReturnOffset.second)
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000339 CovariantThunks[GD] = std::make_pair(std::make_pair(ThisOffset,
Mike Stump28431212009-10-13 22:54:56 +0000340 ReturnOffset),
341 oret);
Mike Stump375faa82009-10-28 00:35:46 +0000342 else if (!isPure)
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000343 Thunks[GD] = Thunk(i, ThisAdjustment);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000344 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000345 return true;
346 }
347 }
348
349 return false;
350 }
351
352 void InstallThunks() {
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000353 for (ThunksMapTy::const_iterator i = Thunks.begin(), e = Thunks.end();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000354 i != e; ++i) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000355 GlobalDecl GD = i->first;
356 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000357 assert(!MD->isPure() && "Can't thunk pure virtual methods!");
358
359 const Thunk& Thunk = i->second;
360 assert(Thunk.Index == Index[GD] && "Thunk index mismatch!");
361
362 submethods[Thunk.Index] = CGM.BuildThunk(MD, Extern, Thunk.Adjustment);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000363 }
364 Thunks.clear();
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000365
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000366 for (CovariantThunks_t::iterator i = CovariantThunks.begin(),
367 e = CovariantThunks.end();
368 i != e; ++i) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000369 GlobalDecl GD = i->first;
370 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Mike Stump375faa82009-10-28 00:35:46 +0000371 if (MD->isPure())
372 continue;
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000373 Index_t idx = Index[GD];
Mike Stump87876a02009-10-13 10:55:21 +0000374 Index_t nv_t = i->second.first.first.first;
375 Index_t v_t = i->second.first.first.second;
376 Index_t nv_r = i->second.first.second.first;
377 Index_t v_r = i->second.first.second.second;
Anders Carlsson2f87c4f2009-11-26 03:09:37 +0000378
379 CovariantThunkAdjustment Adjustment(ThunkAdjustment(nv_t, v_t),
380 ThunkAdjustment(nv_r, v_r));
381 submethods[idx] = CGM.BuildCovariantThunk(MD, Extern, Adjustment);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000382 }
383 CovariantThunks.clear();
Mike Stumpbb9ff052009-10-27 23:46:47 +0000384 for (Pures_t::iterator i = Pures.begin(), e = Pures.end();
385 i != e; ++i) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000386 GlobalDecl GD = i->first;
387 Index_t idx = Index[GD];
Mike Stumpbb9ff052009-10-27 23:46:47 +0000388 submethods[idx] = cxa_pure;
389 }
390 Pures.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000391 }
392
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000393 llvm::Constant *WrapAddrOf(GlobalDecl GD) {
394 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
395
Mike Stump18e8b472009-10-27 23:36:26 +0000396 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000397 return wrap(CGM.GetAddrOfCXXDestructor(Dtor, GD.getDtorType()));
Mike Stump18e8b472009-10-27 23:36:26 +0000398
Anders Carlsson64457732009-11-24 05:08:52 +0000399 const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD);
Mike Stump18e8b472009-10-27 23:36:26 +0000400
401 return wrap(CGM.GetAddrOfFunction(MD, Ty));
402 }
403
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000404 void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset,
405 int64_t CurrentVBaseOffset) {
Mike Stump37dbe962009-10-15 02:04:03 +0000406 for (Path_t::reverse_iterator i = Path->rbegin(),
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000407 e = Path->rend(); i != e; ++i) {
408 const CXXRecordDecl *RD = i->first;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000409 int64_t OverrideOffset = i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000410 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
411 ++mi) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000412 const CXXMethodDecl *MD = *mi;
413
414 if (!MD->isVirtual())
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000415 continue;
416
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000417 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
418 // Override both the complete and the deleting destructor.
419 GlobalDecl CompDtor(DD, Dtor_Complete);
420 OverrideMethod(CompDtor, WrapAddrOf(CompDtor), MorallyVirtual,
421 OverrideOffset, Offset, CurrentVBaseOffset);
422
423 GlobalDecl DeletingDtor(DD, Dtor_Deleting);
424 OverrideMethod(DeletingDtor, WrapAddrOf(DeletingDtor), MorallyVirtual,
425 OverrideOffset, Offset, CurrentVBaseOffset);
426 } else {
427 OverrideMethod(MD, WrapAddrOf(MD), MorallyVirtual, OverrideOffset,
428 Offset, CurrentVBaseOffset);
429 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000430 }
431 }
432 }
433
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000434 void AddMethod(const GlobalDecl GD, bool MorallyVirtual, Index_t Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000435 bool ForVirtualBase, int64_t CurrentVBaseOffset) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000436 llvm::Constant *m = WrapAddrOf(GD);
Mike Stump18e8b472009-10-27 23:36:26 +0000437
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000438 // If we can find a previously allocated slot for this, reuse it.
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000439 if (OverrideMethod(GD, m, MorallyVirtual, Offset, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000440 CurrentVBaseOffset))
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000441 return;
442
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000443 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
444
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000445 // else allocate a new slot.
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000446 Index[GD] = submethods.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000447 submethods.push_back(m);
Mike Stumpc5a332c2009-11-13 23:45:53 +0000448 D1(printf(" vfn for %s at %d\n", MD->getNameAsString().c_str(),
449 (int)Index[GD]));
Mike Stump375faa82009-10-28 00:35:46 +0000450 if (MD->isPure())
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000451 Pures[GD] = 1;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000452 if (MorallyVirtual) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000453 VCallOffset[GD] = Offset/8;
454 Index_t &idx = VCall[GD];
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000455 // Allocate the first one, after that, we reuse the previous one.
456 if (idx == 0) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000457 NonVirtualOffset[GD] = CurrentVBaseOffset/8 - Offset/8;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000458 idx = VCalls.size()+1;
459 VCalls.push_back(0);
Mike Stump75ce5732009-10-31 20:06:59 +0000460 D1(printf(" vcall for %s at %d with delta %d\n",
Mike Stumpc5a332c2009-11-13 23:45:53 +0000461 MD->getNameAsString().c_str(), (int)-VCalls.size()-3, 0));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000462 }
463 }
464 }
465
466 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000467 Index_t Offset, bool RDisVirtualBase,
468 int64_t CurrentVBaseOffset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000469 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000470 ++mi) {
471 const CXXMethodDecl *MD = *mi;
472 if (!MD->isVirtual())
473 continue;
474
475 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
476 // For destructors, add both the complete and the deleting destructor
477 // to the vtable.
478 AddMethod(GlobalDecl(DD, Dtor_Complete), MorallyVirtual, Offset,
479 RDisVirtualBase, CurrentVBaseOffset);
480 AddMethod(GlobalDecl(DD, Dtor_Deleting), MorallyVirtual, Offset,
481 RDisVirtualBase, CurrentVBaseOffset);
482 } else
483 AddMethod(MD, MorallyVirtual, Offset, RDisVirtualBase,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000484 CurrentVBaseOffset);
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000485 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000486 }
487
488 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
489 const CXXRecordDecl *PrimaryBase,
490 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000491 int64_t Offset, int64_t CurrentVBaseOffset,
492 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000493 Path->push_back(std::make_pair(RD, Offset));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000494 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
495 e = RD->bases_end(); i != e; ++i) {
496 if (i->isVirtual())
497 continue;
498 const CXXRecordDecl *Base =
499 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
500 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
501 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
502 StartNewTable();
Mike Stump653d0b92009-11-13 02:13:54 +0000503 GenerateVtableForBase(Base, o, MorallyVirtual, false,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000504 CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000505 }
506 }
Mike Stump37dbe962009-10-15 02:04:03 +0000507 Path->pop_back();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000508 }
509
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000510// #define D(X) do { X; } while (0)
511#define D(X)
512
513 void insertVCalls(int InsertionPoint) {
514 llvm::Constant *e = 0;
Mike Stump75ce5732009-10-31 20:06:59 +0000515 D1(printf("============= combining vbase/vcall\n"));
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000516 D(VCalls.insert(VCalls.begin(), 673));
517 D(VCalls.push_back(672));
Mike Stump8bccbfd2009-10-15 09:30:16 +0000518 methods.insert(methods.begin() + InsertionPoint, VCalls.size(), e);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000519 // The vcalls come first...
520 for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(),
521 e = VCalls.rend();
522 i != e; ++i)
523 methods[InsertionPoint++] = wrap((0?600:0) + *i);
524 VCalls.clear();
Mike Stump9f23a142009-11-10 02:30:51 +0000525 VCall.clear();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000526 }
527
Mike Stump559387f2009-11-13 23:13:20 +0000528 void AddAddressPoints(const CXXRecordDecl *RD, uint64_t Offset,
529 Index_t AddressPoint) {
530 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
531 RD->getNameAsCString(), Class->getNameAsCString(),
532 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stumpcd2b8212009-11-19 20:52:19 +0000533 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Mike Stump559387f2009-11-13 23:13:20 +0000534
535 // Now also add the address point for all our primary bases.
536 while (1) {
537 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
538 RD = Layout.getPrimaryBase();
539 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
540 // FIXME: Double check this.
541 if (RD == 0)
542 break;
543 if (PrimaryBaseWasVirtual &&
544 BLayout.getVBaseClassOffset(RD) != Offset)
545 break;
546 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
547 RD->getNameAsCString(), Class->getNameAsCString(),
548 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stumpcd2b8212009-11-19 20:52:19 +0000549 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Mike Stump559387f2009-11-13 23:13:20 +0000550 }
551 }
552
553
Mike Stump75ce5732009-10-31 20:06:59 +0000554 Index_t end(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
555 const CXXRecordDecl *PrimaryBase, bool PrimaryBaseWasVirtual,
556 bool MorallyVirtual, int64_t Offset, bool ForVirtualBase,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000557 int64_t CurrentVBaseOffset,
Mike Stump75ce5732009-10-31 20:06:59 +0000558 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000559 bool alloc = false;
560 if (Path == 0) {
561 alloc = true;
562 Path = new Path_t;
563 }
564
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000565 StartNewTable();
566 extra = 0;
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000567 bool DeferVCalls = MorallyVirtual || ForVirtualBase;
568 int VCallInsertionPoint = methods.size();
569 if (!DeferVCalls) {
570 insertVCalls(VCallInsertionPoint);
Mike Stump8bccbfd2009-10-15 09:30:16 +0000571 } else
572 // FIXME: just for extra, or for all uses of VCalls.size post this?
573 extra = -VCalls.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000574
Mike Stump653d0b92009-11-13 02:13:54 +0000575 methods.push_back(wrap(-((Offset-LayoutOffset)/8)));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000576 methods.push_back(rtti);
577 Index_t AddressPoint = methods.size();
578
579 InstallThunks();
Mike Stump75ce5732009-10-31 20:06:59 +0000580 D1(printf("============= combining methods\n"));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000581 methods.insert(methods.end(), submethods.begin(), submethods.end());
582 submethods.clear();
583
584 // and then the non-virtual bases.
585 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000586 MorallyVirtual, Offset, CurrentVBaseOffset, Path);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000587
588 if (ForVirtualBase) {
Mike Stump2cefe382009-11-12 20:47:57 +0000589 // FIXME: We're adding to VCalls in callers, we need to do the overrides
590 // in the inner part, so that we know the complete set of vcalls during
591 // the build and don't have to insert into methods. Saving out the
592 // AddressPoint here, would need to be fixed, if we didn't do that. Also
593 // retroactively adding vcalls for overrides later wind up in the wrong
594 // place, the vcall slot has to be alloted during the walk of the base
595 // when the function is first introduces.
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000596 AddressPoint += VCalls.size();
Mike Stump2cefe382009-11-12 20:47:57 +0000597 insertVCalls(VCallInsertionPoint);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000598 }
599
Mike Stump559387f2009-11-13 23:13:20 +0000600 AddAddressPoints(RD, Offset, AddressPoint);
Mike Stump2cefe382009-11-12 20:47:57 +0000601
Mike Stump37dbe962009-10-15 02:04:03 +0000602 if (alloc) {
603 delete Path;
604 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000605 return AddressPoint;
606 }
607
Mike Stump75ce5732009-10-31 20:06:59 +0000608 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
609 bool updateVBIndex, Index_t current_vbindex,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000610 bool RDisVirtualBase, int64_t CurrentVBaseOffset) {
Mike Stump75ce5732009-10-31 20:06:59 +0000611 if (!RD->isDynamicClass())
612 return;
613
614 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
615 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
616 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
617
618 // vtables are composed from the chain of primaries.
619 if (PrimaryBase) {
620 D1(printf(" doing primaries for %s most derived %s\n",
621 RD->getNameAsCString(), Class->getNameAsCString()));
622
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000623 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
624 if (PrimaryBaseWasVirtual)
625 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
626
Mike Stump75ce5732009-10-31 20:06:59 +0000627 if (!PrimaryBaseWasVirtual)
628 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000629 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
630 BaseCurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000631 }
632
633 D1(printf(" doing vcall entries for %s most derived %s\n",
634 RD->getNameAsCString(), Class->getNameAsCString()));
635
636 // And add the virtuals for the class to the primary vtable.
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000637 AddMethods(RD, MorallyVirtual, Offset, RDisVirtualBase, CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000638 }
639
640 void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
641 bool updateVBIndex, Index_t current_vbindex,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000642 bool RDisVirtualBase, int64_t CurrentVBaseOffset,
643 bool bottom=false) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000644 if (!RD->isDynamicClass())
645 return;
646
647 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
648 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
649 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
650
651 // vtables are composed from the chain of primaries.
652 if (PrimaryBase) {
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000653 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
654 if (PrimaryBaseWasVirtual) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000655 IndirectPrimary.insert(PrimaryBase);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000656 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
657 }
Mike Stump75ce5732009-10-31 20:06:59 +0000658
659 D1(printf(" doing primaries for %s most derived %s\n",
660 RD->getNameAsCString(), Class->getNameAsCString()));
661
662 VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000663 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
664 BaseCurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000665 }
666
Mike Stump75ce5732009-10-31 20:06:59 +0000667 D1(printf(" doing vbase entries for %s most derived %s\n",
668 RD->getNameAsCString(), Class->getNameAsCString()));
669 GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex);
670
671 if (RDisVirtualBase || bottom) {
672 Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000673 RDisVirtualBase, CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000674 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000675 }
676
Mike Stump653d0b92009-11-13 02:13:54 +0000677 int64_t GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0,
678 bool MorallyVirtual = false,
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000679 bool ForVirtualBase = false,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000680 int CurrentVBaseOffset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +0000681 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000682 if (!RD->isDynamicClass())
683 return 0;
684
Mike Stumpfa818082009-11-13 02:35:38 +0000685 // Construction vtable don't need parts that have no virtual bases and
686 // aren't morally virtual.
687 if ((LayoutClass != Class) && RD->getNumVBases() == 0 && !MorallyVirtual)
688 return 0;
689
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000690 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
691 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
692 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
693
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000694 extra = 0;
Mike Stump75ce5732009-10-31 20:06:59 +0000695 D1(printf("building entries for base %s most derived %s\n",
696 RD->getNameAsCString(), Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000697
Mike Stump75ce5732009-10-31 20:06:59 +0000698 if (ForVirtualBase)
699 extra = VCalls.size();
700
701 VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0, ForVirtualBase,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000702 CurrentVBaseOffset, true);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000703
704 if (Path)
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000705 OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000706
Mike Stump75ce5732009-10-31 20:06:59 +0000707 return end(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000708 Offset, ForVirtualBase, CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000709 }
710
711 void GenerateVtableForVBases(const CXXRecordDecl *RD,
712 int64_t Offset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +0000713 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000714 bool alloc = false;
715 if (Path == 0) {
716 alloc = true;
Mike Stump37dbe962009-10-15 02:04:03 +0000717 Path = new Path_t;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000718 }
719 // FIXME: We also need to override using all paths to a virtual base,
720 // right now, we just process the first path
721 Path->push_back(std::make_pair(RD, Offset));
722 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
723 e = RD->bases_end(); i != e; ++i) {
724 const CXXRecordDecl *Base =
725 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
726 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
727 // Mark it so we don't output it twice.
728 IndirectPrimary.insert(Base);
729 StartNewTable();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000730 VCall.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000731 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000732 int64_t CurrentVBaseOffset = BaseOffset;
Mike Stump75ce5732009-10-31 20:06:59 +0000733 D1(printf("vtable %s virtual base %s\n",
734 Class->getNameAsCString(), Base->getNameAsCString()));
Mike Stump653d0b92009-11-13 02:13:54 +0000735 GenerateVtableForBase(Base, BaseOffset, true, true, CurrentVBaseOffset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000736 Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000737 }
Mike Stump2cefe382009-11-12 20:47:57 +0000738 int64_t BaseOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000739 if (i->isVirtual())
740 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump2cefe382009-11-12 20:47:57 +0000741 else {
742 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
743 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
744 }
745
Mike Stump37dbe962009-10-15 02:04:03 +0000746 if (Base->getNumVBases()) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000747 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump37dbe962009-10-15 02:04:03 +0000748 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000749 }
750 Path->pop_back();
751 if (alloc)
752 delete Path;
753 }
754};
755
756
757VtableBuilder::Index_t VtableBuilder::VBlookup(CXXRecordDecl *D,
758 CXXRecordDecl *B) {
759 return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B);
760}
761
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000762int64_t CGVtableInfo::getMethodVtableIndex(GlobalDecl GD) {
763 MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000764 if (I != MethodVtableIndices.end())
765 return I->second;
766
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000767 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000768
769 std::vector<llvm::Constant *> methods;
770 // FIXME: This seems expensive. Can we do a partial job to get
771 // just this data.
Mike Stump653d0b92009-11-13 02:13:54 +0000772 VtableBuilder b(methods, RD, RD, 0, CGM);
Mike Stump75ce5732009-10-31 20:06:59 +0000773 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000774 b.GenerateVtableForBase(RD);
775 b.GenerateVtableForVBases(RD);
776
777 MethodVtableIndices.insert(b.getIndex().begin(),
778 b.getIndex().end());
779
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000780 I = MethodVtableIndices.find(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000781 assert(I != MethodVtableIndices.end() && "Did not find index!");
782 return I->second;
783}
784
785int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
786 const CXXRecordDecl *VBase) {
787 ClassPairTy ClassPair(RD, VBase);
788
789 VirtualBaseClassIndiciesTy::iterator I =
790 VirtualBaseClassIndicies.find(ClassPair);
791 if (I != VirtualBaseClassIndicies.end())
792 return I->second;
793
794 std::vector<llvm::Constant *> methods;
795 // FIXME: This seems expensive. Can we do a partial job to get
796 // just this data.
Mike Stump653d0b92009-11-13 02:13:54 +0000797 VtableBuilder b(methods, RD, RD, 0, CGM);
Mike Stump75ce5732009-10-31 20:06:59 +0000798 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000799 b.GenerateVtableForBase(RD);
800 b.GenerateVtableForVBases(RD);
801
802 for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
803 b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) {
804 // Insert all types.
805 ClassPairTy ClassPair(RD, I->first);
806
807 VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second));
808 }
809
810 I = VirtualBaseClassIndicies.find(ClassPair);
811 assert(I != VirtualBaseClassIndicies.end() && "Did not find index!");
812
813 return I->second;
814}
815
Mike Stump2cefe382009-11-12 20:47:57 +0000816llvm::Constant *CodeGenModule::GenerateVtable(const CXXRecordDecl *LayoutClass,
817 const CXXRecordDecl *RD,
Mike Stumpeac45592009-11-11 20:26:26 +0000818 uint64_t Offset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000819 llvm::SmallString<256> OutName;
Mike Stump2cefe382009-11-12 20:47:57 +0000820 if (LayoutClass != RD)
Daniel Dunbare128dd12009-11-21 09:06:22 +0000821 getMangleContext().mangleCXXCtorVtable(LayoutClass, Offset/8, RD, OutName);
Mike Stumpeac45592009-11-11 20:26:26 +0000822 else
Daniel Dunbare128dd12009-11-21 09:06:22 +0000823 getMangleContext().mangleCXXVtable(RD, OutName);
824 llvm::StringRef Name = OutName.str();
Benjamin Kramerbb0a07b2009-10-11 22:57:54 +0000825
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000826 std::vector<llvm::Constant *> methods;
827 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
828 int64_t AddressPoint;
829
Mike Stumpaa51ad62009-11-19 04:04:36 +0000830 llvm::GlobalVariable *GV = getModule().getGlobalVariable(Name);
Mike Stumpcd2b8212009-11-19 20:52:19 +0000831 if (GV && AddressPoints[LayoutClass] && !GV->isDeclaration()) {
Mike Stumpaa51ad62009-11-19 04:04:36 +0000832 AddressPoint=(*(*(AddressPoints[LayoutClass]))[RD])[std::make_pair(RD,
833 Offset)];
Mike Stumpcd2b8212009-11-19 20:52:19 +0000834 // FIXME: We can never have 0 address point. Do this for now so gepping
835 // retains the same structure. Later, we'll just assert.
836 if (AddressPoint == 0)
837 AddressPoint = 1;
838 } else {
Mike Stumpaa51ad62009-11-19 04:04:36 +0000839 VtableBuilder b(methods, RD, LayoutClass, Offset, *this);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000840
Mike Stumpaa51ad62009-11-19 04:04:36 +0000841 D1(printf("vtable %s\n", RD->getNameAsCString()));
842 // First comes the vtables for all the non-virtual bases...
843 AddressPoint = b.GenerateVtableForBase(RD, Offset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000844
Mike Stumpaa51ad62009-11-19 04:04:36 +0000845 // then the vtables for all the virtual bases.
846 b.GenerateVtableForVBases(RD, Offset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000847
Mike Stumpaa51ad62009-11-19 04:04:36 +0000848 bool CreateDefinition = true;
849 if (LayoutClass != RD)
850 CreateDefinition = true;
851 else {
852 // We have to convert it to have a record layout.
853 Types.ConvertTagDeclType(LayoutClass);
854 const CGRecordLayout &CGLayout = Types.getCGRecordLayout(LayoutClass);
855 if (const CXXMethodDecl *KeyFunction = CGLayout.getKeyFunction()) {
856 if (!KeyFunction->getBody()) {
857 // If there is a KeyFunction, and it isn't defined, just build a
858 // reference to the vtable.
859 CreateDefinition = false;
860 }
861 }
862 }
863
864 llvm::Constant *C = 0;
865 llvm::Type *type = Ptr8Ty;
866 llvm::GlobalVariable::LinkageTypes linktype
867 = llvm::GlobalValue::ExternalLinkage;
868 if (CreateDefinition) {
869 llvm::ArrayType *ntype = llvm::ArrayType::get(Ptr8Ty, methods.size());
870 C = llvm::ConstantArray::get(ntype, methods);
871 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
872 if (LayoutClass->isInAnonymousNamespace())
873 linktype = llvm::GlobalValue::InternalLinkage;
874 type = ntype;
875 }
876 llvm::GlobalVariable *OGV = GV;
877 GV = new llvm::GlobalVariable(getModule(), type, true, linktype, C, Name);
878 if (OGV) {
879 GV->takeName(OGV);
880 llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
881 OGV->getType());
882 OGV->replaceAllUsesWith(NewPtr);
883 OGV->eraseFromParent();
884 }
885 bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden;
886 if (Hidden)
887 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
888 }
Mike Stumpc0f632d2009-11-18 04:00:48 +0000889 llvm::Constant *vtable = llvm::ConstantExpr::getBitCast(GV, Ptr8Ty);
Mike Stumpd846d082009-11-10 07:44:33 +0000890 llvm::Constant *AddressPointC;
891 uint32_t LLVMPointerWidth = getContext().Target.getPointerWidth(0);
892 AddressPointC = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
893 AddressPoint*LLVMPointerWidth/8);
Mike Stump2cefe382009-11-12 20:47:57 +0000894 vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(vtable, &AddressPointC,
895 1);
Mike Stumpd846d082009-11-10 07:44:33 +0000896
Mike Stumpcd2b8212009-11-19 20:52:19 +0000897 assert(vtable->getType() == Ptr8Ty);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000898 return vtable;
899}
Mike Stump9f23a142009-11-10 02:30:51 +0000900
901class VTTBuilder {
902 /// Inits - The list of values built for the VTT.
903 std::vector<llvm::Constant *> &Inits;
904 /// Class - The most derived class that this vtable is being built for.
905 const CXXRecordDecl *Class;
906 CodeGenModule &CGM; // Per-module state.
Mike Stump8b2d2d02009-11-11 00:35:07 +0000907 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
Mike Stumpc7b9f5e2009-11-11 03:08:24 +0000908 /// BLayout - Layout for the most derived class that this vtable is being
909 /// built for.
910 const ASTRecordLayout &BLayout;
Mike Stump83066c82009-11-13 01:54:23 +0000911 CodeGenModule::AddrMap_t &AddressPoints;
Mike Stump2cefe382009-11-12 20:47:57 +0000912 // vtbl - A pointer to the vtable for Class.
913 llvm::Constant *ClassVtbl;
914 llvm::LLVMContext &VMContext;
Mike Stump9f23a142009-11-10 02:30:51 +0000915
Mike Stump8677bc22009-11-12 22:56:32 +0000916 /// BuildVtablePtr - Build up a referene to the given secondary vtable
Mike Stump83066c82009-11-13 01:54:23 +0000917 llvm::Constant *BuildVtablePtr(llvm::Constant *vtbl,
918 const CXXRecordDecl *VtblClass,
919 const CXXRecordDecl *RD,
Mike Stump8677bc22009-11-12 22:56:32 +0000920 uint64_t Offset) {
921 int64_t AddressPoint;
Mike Stump83066c82009-11-13 01:54:23 +0000922 AddressPoint = (*AddressPoints[VtblClass])[std::make_pair(RD, Offset)];
Mike Stump2b34bc52009-11-12 23:36:21 +0000923 // FIXME: We can never have 0 address point. Do this for now so gepping
Mike Stumpcd2b8212009-11-19 20:52:19 +0000924 // retains the same structure. Later we'll just assert.
Mike Stump2b34bc52009-11-12 23:36:21 +0000925 if (AddressPoint == 0)
926 AddressPoint = 1;
Mike Stump83066c82009-11-13 01:54:23 +0000927 D1(printf("XXX address point for %s in %s layout %s at offset %d was %d\n",
928 RD->getNameAsCString(), VtblClass->getNameAsCString(),
929 Class->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stump8677bc22009-11-12 22:56:32 +0000930 uint32_t LLVMPointerWidth = CGM.getContext().Target.getPointerWidth(0);
931 llvm::Constant *init;
932 init = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
933 AddressPoint*LLVMPointerWidth/8);
934 init = llvm::ConstantExpr::getInBoundsGetElementPtr(vtbl, &init, 1);
935 return init;
936 }
937
Mike Stump2cefe382009-11-12 20:47:57 +0000938 /// Secondary - Add the secondary vtable pointers to Inits. Offset is the
939 /// current offset in bits to the object we're working on.
Mike Stump8677bc22009-11-12 22:56:32 +0000940 void Secondary(const CXXRecordDecl *RD, llvm::Constant *vtbl,
Mike Stump83066c82009-11-13 01:54:23 +0000941 const CXXRecordDecl *VtblClass, uint64_t Offset=0,
942 bool MorallyVirtual=false) {
Mike Stump8b2d2d02009-11-11 00:35:07 +0000943 if (RD->getNumVBases() == 0 && ! MorallyVirtual)
944 return;
945
946 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
947 e = RD->bases_end(); i != e; ++i) {
948 const CXXRecordDecl *Base =
949 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
950 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
951 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
952 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
953 bool NonVirtualPrimaryBase;
954 NonVirtualPrimaryBase = !PrimaryBaseWasVirtual && Base == PrimaryBase;
955 bool BaseMorallyVirtual = MorallyVirtual | i->isVirtual();
Mike Stumpc7b9f5e2009-11-11 03:08:24 +0000956 uint64_t BaseOffset;
957 if (!i->isVirtual()) {
958 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
959 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
960 } else
961 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump2b34bc52009-11-12 23:36:21 +0000962 llvm::Constant *subvtbl = vtbl;
Mike Stump83066c82009-11-13 01:54:23 +0000963 const CXXRecordDecl *subVtblClass = VtblClass;
Mike Stump8b2d2d02009-11-11 00:35:07 +0000964 if ((Base->getNumVBases() || BaseMorallyVirtual)
965 && !NonVirtualPrimaryBase) {
966 // FIXME: Slightly too many of these for __ZTT8test8_B2
Mike Stump8677bc22009-11-12 22:56:32 +0000967 llvm::Constant *init;
Mike Stump2b34bc52009-11-12 23:36:21 +0000968 if (BaseMorallyVirtual)
Mike Stump83066c82009-11-13 01:54:23 +0000969 init = BuildVtablePtr(vtbl, VtblClass, RD, Offset);
Mike Stump2b34bc52009-11-12 23:36:21 +0000970 else {
Mike Stump8677bc22009-11-12 22:56:32 +0000971 init = CGM.getVtableInfo().getCtorVtable(Class, Base, BaseOffset);
Mike Stump2b34bc52009-11-12 23:36:21 +0000972 subvtbl = dyn_cast<llvm::Constant>(init->getOperand(0));
Mike Stump83066c82009-11-13 01:54:23 +0000973 subVtblClass = Base;
Mike Stump2b34bc52009-11-12 23:36:21 +0000974 }
Mike Stump8677bc22009-11-12 22:56:32 +0000975 Inits.push_back(init);
Mike Stump8b2d2d02009-11-11 00:35:07 +0000976 }
Mike Stump83066c82009-11-13 01:54:23 +0000977 Secondary(Base, subvtbl, subVtblClass, BaseOffset, BaseMorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +0000978 }
979 }
980
Mike Stump2cefe382009-11-12 20:47:57 +0000981 /// BuiltVTT - Add the VTT to Inits. Offset is the offset in bits to the
982 /// currnet object we're working on.
983 void BuildVTT(const CXXRecordDecl *RD, uint64_t Offset, bool MorallyVirtual) {
Mike Stump8b2d2d02009-11-11 00:35:07 +0000984 if (RD->getNumVBases() == 0 && !MorallyVirtual)
985 return;
986
Mike Stump2cefe382009-11-12 20:47:57 +0000987 llvm::Constant *init;
Mike Stump83066c82009-11-13 01:54:23 +0000988 const CXXRecordDecl *VtblClass;
989
Mike Stump8b2d2d02009-11-11 00:35:07 +0000990 // First comes the primary virtual table pointer...
Mike Stump83066c82009-11-13 01:54:23 +0000991 if (MorallyVirtual) {
992 init = BuildVtablePtr(ClassVtbl, Class, RD, Offset);
993 VtblClass = Class;
994 } else {
Mike Stump2cefe382009-11-12 20:47:57 +0000995 init = CGM.getVtableInfo().getCtorVtable(Class, RD, Offset);
Mike Stump83066c82009-11-13 01:54:23 +0000996 VtblClass = RD;
997 }
Mike Stump8677bc22009-11-12 22:56:32 +0000998 llvm::Constant *vtbl = dyn_cast<llvm::Constant>(init->getOperand(0));
Mike Stump2cefe382009-11-12 20:47:57 +0000999 Inits.push_back(init);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001000
1001 // then the secondary VTTs....
Mike Stump2cefe382009-11-12 20:47:57 +00001002 SecondaryVTTs(RD, Offset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001003
1004 // and last the secondary vtable pointers.
Mike Stump83066c82009-11-13 01:54:23 +00001005 Secondary(RD, vtbl, VtblClass, Offset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001006 }
1007
1008 /// SecondaryVTTs - Add the secondary VTTs to Inits. The secondary VTTs are
1009 /// built from each direct non-virtual proper base that requires a VTT in
1010 /// declaration order.
Mike Stump2cefe382009-11-12 20:47:57 +00001011 void SecondaryVTTs(const CXXRecordDecl *RD, uint64_t Offset=0,
1012 bool MorallyVirtual=false) {
Mike Stump8b2d2d02009-11-11 00:35:07 +00001013 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1014 e = RD->bases_end(); i != e; ++i) {
1015 const CXXRecordDecl *Base =
1016 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1017 if (i->isVirtual())
1018 continue;
Mike Stump2cefe382009-11-12 20:47:57 +00001019 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1020 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base);
1021 BuildVTT(Base, BaseOffset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001022 }
1023 }
1024
1025 /// VirtualVTTs - Add the VTT for each proper virtual base in inheritance
1026 /// graph preorder.
1027 void VirtualVTTs(const CXXRecordDecl *RD) {
1028 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1029 e = RD->bases_end(); i != e; ++i) {
1030 const CXXRecordDecl *Base =
1031 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1032 if (i->isVirtual() && !SeenVBase.count(Base)) {
1033 SeenVBase.insert(Base);
Mike Stump2cefe382009-11-12 20:47:57 +00001034 uint64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
1035 BuildVTT(Base, BaseOffset, true);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001036 }
1037 VirtualVTTs(Base);
1038 }
1039 }
Mike Stump9f23a142009-11-10 02:30:51 +00001040public:
1041 VTTBuilder(std::vector<llvm::Constant *> &inits, const CXXRecordDecl *c,
Mike Stumpc7b9f5e2009-11-11 03:08:24 +00001042 CodeGenModule &cgm)
1043 : Inits(inits), Class(c), CGM(cgm),
Mike Stump2cefe382009-11-12 20:47:57 +00001044 BLayout(cgm.getContext().getASTRecordLayout(c)),
Mike Stump83066c82009-11-13 01:54:23 +00001045 AddressPoints(*cgm.AddressPoints[c]),
Mike Stump2cefe382009-11-12 20:47:57 +00001046 VMContext(cgm.getModule().getContext()) {
Mike Stumpd846d082009-11-10 07:44:33 +00001047
Mike Stump8b2d2d02009-11-11 00:35:07 +00001048 // First comes the primary virtual table pointer for the complete class...
Mike Stump2cefe382009-11-12 20:47:57 +00001049 ClassVtbl = CGM.getVtableInfo().getVtable(Class);
1050 Inits.push_back(ClassVtbl);
1051 ClassVtbl = dyn_cast<llvm::Constant>(ClassVtbl->getOperand(0));
1052
Mike Stump8b2d2d02009-11-11 00:35:07 +00001053 // then the secondary VTTs...
1054 SecondaryVTTs(Class);
1055
1056 // then the secondary vtable pointers...
Mike Stump83066c82009-11-13 01:54:23 +00001057 Secondary(Class, ClassVtbl, Class);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001058
1059 // and last, the virtual VTTs.
1060 VirtualVTTs(Class);
Mike Stump9f23a142009-11-10 02:30:51 +00001061 }
1062};
1063
Mike Stumpd846d082009-11-10 07:44:33 +00001064llvm::Constant *CodeGenModule::GenerateVTT(const CXXRecordDecl *RD) {
Mike Stumpb4722212009-11-10 19:13:04 +00001065 // Only classes that have virtual bases need a VTT.
1066 if (RD->getNumVBases() == 0)
1067 return 0;
1068
Mike Stump9f23a142009-11-10 02:30:51 +00001069 llvm::SmallString<256> OutName;
Daniel Dunbare128dd12009-11-21 09:06:22 +00001070 getMangleContext().mangleCXXVTT(RD, OutName);
1071 llvm::StringRef Name = OutName.str();
Mike Stump9f23a142009-11-10 02:30:51 +00001072
1073 llvm::GlobalVariable::LinkageTypes linktype;
1074 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
Mike Stumpaa51ad62009-11-19 04:04:36 +00001075 if (RD->isInAnonymousNamespace())
1076 linktype = llvm::GlobalValue::InternalLinkage;
Mike Stump9f23a142009-11-10 02:30:51 +00001077 std::vector<llvm::Constant *> inits;
1078 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1079
Mike Stump9f23a142009-11-10 02:30:51 +00001080 D1(printf("vtt %s\n", RD->getNameAsCString()));
1081
Mike Stump8b2d2d02009-11-11 00:35:07 +00001082 VTTBuilder b(inits, RD, *this);
1083
Mike Stump9f23a142009-11-10 02:30:51 +00001084 llvm::Constant *C;
1085 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, inits.size());
1086 C = llvm::ConstantArray::get(type, inits);
Mike Stumpc0f632d2009-11-18 04:00:48 +00001087 llvm::GlobalVariable *vtt = new llvm::GlobalVariable(getModule(), type, true,
Mike Stumpaa51ad62009-11-19 04:04:36 +00001088 linktype, C, Name);
Mike Stumpc0f632d2009-11-18 04:00:48 +00001089 bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden;
1090 if (Hidden)
1091 vtt->setVisibility(llvm::GlobalVariable::HiddenVisibility);
1092 return llvm::ConstantExpr::getBitCast(vtt, Ptr8Ty);
Mike Stump9f23a142009-11-10 02:30:51 +00001093}
Mike Stumpd846d082009-11-10 07:44:33 +00001094
Mike Stump1a139f82009-11-19 01:08:19 +00001095void CGVtableInfo::GenerateClassData(const CXXRecordDecl *RD) {
1096 Vtables[RD] = CGM.GenerateVtable(RD, RD);
1097 CGM.GenerateRtti(RD);
1098 CGM.GenerateVTT(RD);
1099}
1100
Mike Stumpeac45592009-11-11 20:26:26 +00001101llvm::Constant *CGVtableInfo::getVtable(const CXXRecordDecl *RD) {
Mike Stumpd846d082009-11-10 07:44:33 +00001102 llvm::Constant *&vtbl = Vtables[RD];
1103 if (vtbl)
1104 return vtbl;
Mike Stump2cefe382009-11-12 20:47:57 +00001105 vtbl = CGM.GenerateVtable(RD, RD);
Mike Stumpaa51ad62009-11-19 04:04:36 +00001106
1107 bool CreateDefinition = true;
1108 // We have to convert it to have a record layout.
1109 CGM.getTypes().ConvertTagDeclType(RD);
1110 const CGRecordLayout &CGLayout = CGM.getTypes().getCGRecordLayout(RD);
1111 if (const CXXMethodDecl *KeyFunction = CGLayout.getKeyFunction()) {
1112 if (!KeyFunction->getBody()) {
1113 // If there is a KeyFunction, and it isn't defined, just build a
1114 // reference to the vtable.
1115 CreateDefinition = false;
1116 }
1117 }
1118
1119 if (CreateDefinition) {
1120 CGM.GenerateRtti(RD);
1121 CGM.GenerateVTT(RD);
1122 }
Mike Stumpd846d082009-11-10 07:44:33 +00001123 return vtbl;
1124}
Mike Stumpeac45592009-11-11 20:26:26 +00001125
Mike Stump2cefe382009-11-12 20:47:57 +00001126llvm::Constant *CGVtableInfo::getCtorVtable(const CXXRecordDecl *LayoutClass,
1127 const CXXRecordDecl *RD,
Mike Stumpeac45592009-11-11 20:26:26 +00001128 uint64_t Offset) {
Mike Stump2cefe382009-11-12 20:47:57 +00001129 return CGM.GenerateVtable(LayoutClass, RD, Offset);
Mike Stumpeac45592009-11-11 20:26:26 +00001130}