blob: 4d3c3a595dda969619daffc0a4018e27e6754d33 [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"
18
19using namespace clang;
20using namespace CodeGen;
21
22class VtableBuilder {
23public:
24 /// Index_t - Vtable index type.
25 typedef uint64_t Index_t;
26private:
27 std::vector<llvm::Constant *> &methods;
28 std::vector<llvm::Constant *> submethods;
29 llvm::Type *Ptr8Ty;
30 /// Class - The most derived class that this vtable is being built for.
31 const CXXRecordDecl *Class;
32 /// BLayout - Layout for the most derived class that this vtable is being
33 /// built for.
34 const ASTRecordLayout &BLayout;
35 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
36 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
37 llvm::Constant *rtti;
38 llvm::LLVMContext &VMContext;
39 CodeGenModule &CGM; // Per-module state.
40 /// Index - Maps a method decl into a vtable index. Useful for virtual
41 /// dispatch codegen.
42 llvm::DenseMap<const CXXMethodDecl *, Index_t> Index;
43 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall;
44 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCallOffset;
Mike Stumpcd6f9ed2009-11-06 23:27:42 +000045 // This is the offset to the nearest virtual base
46 llvm::DenseMap<const CXXMethodDecl *, Index_t> NonVirtualOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000047 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stumpbb9ff052009-10-27 23:46:47 +000048
49 typedef llvm::DenseMap<const CXXMethodDecl *, int> Pures_t;
50 Pures_t Pures;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000051 typedef std::pair<Index_t, Index_t> CallOffset;
52 typedef llvm::DenseMap<const CXXMethodDecl *, CallOffset> Thunks_t;
53 Thunks_t Thunks;
54 typedef llvm::DenseMap<const CXXMethodDecl *,
Mike Stump87876a02009-10-13 10:55:21 +000055 std::pair<std::pair<CallOffset, CallOffset>,
56 CanQualType> > CovariantThunks_t;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000057 CovariantThunks_t CovariantThunks;
58 std::vector<Index_t> VCalls;
59 typedef CXXRecordDecl::method_iterator method_iter;
60 // FIXME: Linkage should follow vtable
61 const bool Extern;
62 const uint32_t LLVMPointerWidth;
63 Index_t extra;
Mike Stump37dbe962009-10-15 02:04:03 +000064 typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t;
Mike Stumpbb9ff052009-10-27 23:46:47 +000065 llvm::Constant *cxa_pure;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000066public:
67 VtableBuilder(std::vector<llvm::Constant *> &meth,
68 const CXXRecordDecl *c,
69 CodeGenModule &cgm)
70 : methods(meth), Class(c), BLayout(cgm.getContext().getASTRecordLayout(c)),
71 rtti(cgm.GenerateRtti(c)), VMContext(cgm.getModule().getContext()),
72 CGM(cgm), Extern(true),
Mike Stumpcd6f9ed2009-11-06 23:27:42 +000073 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +000074 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Mike Stump375faa82009-10-28 00:35:46 +000075
76 // Calculate pointer for ___cxa_pure_virtual.
77 const llvm::FunctionType *FTy;
78 std::vector<const llvm::Type*> ArgTys;
79 const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
80 FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
81 cxa_pure = wrap(CGM.CreateRuntimeFunction(FTy, "__cxa_pure_virtual"));
Anders Carlsson2bb27f52009-10-11 22:13:54 +000082 }
83
84 llvm::DenseMap<const CXXMethodDecl *, Index_t> &getIndex() { return Index; }
85 llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex()
86 { return VBIndex; }
87
88 llvm::Constant *wrap(Index_t i) {
89 llvm::Constant *m;
90 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
91 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
92 }
93
94 llvm::Constant *wrap(llvm::Constant *m) {
95 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
96 }
97
Mike Stump75ce5732009-10-31 20:06:59 +000098#define D1(x)
99//#define D1(X) do { if (getenv("DEBUG")) { X; } } while (0)
100
101 void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset,
Mike Stump28431212009-10-13 22:54:56 +0000102 bool updateVBIndex, Index_t current_vbindex) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000103 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
104 e = RD->bases_end(); i != e; ++i) {
105 const CXXRecordDecl *Base =
106 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump28431212009-10-13 22:54:56 +0000107 Index_t next_vbindex = current_vbindex;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000108 if (i->isVirtual() && !SeenVBase.count(Base)) {
109 SeenVBase.insert(Base);
Mike Stump28431212009-10-13 22:54:56 +0000110 if (updateVBIndex) {
Mike Stump75ce5732009-10-31 20:06:59 +0000111 next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8)
Mike Stump28431212009-10-13 22:54:56 +0000112 - 3*LLVMPointerWidth/8);
113 VBIndex[Base] = next_vbindex;
114 }
Mike Stump75ce5732009-10-31 20:06:59 +0000115 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
116 VCalls.push_back((0?700:0) + BaseOffset);
117 D1(printf(" vbase for %s at %d delta %d most derived %s\n",
118 Base->getNameAsCString(),
119 (int)-VCalls.size()-3, (int)BaseOffset,
120 Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000121 }
Mike Stump28431212009-10-13 22:54:56 +0000122 // We also record offsets for non-virtual bases to closest enclosing
123 // virtual base. We do this so that we don't have to search
124 // for the nearst virtual base class when generating thunks.
125 if (updateVBIndex && VBIndex.count(Base) == 0)
126 VBIndex[Base] = next_vbindex;
Mike Stump75ce5732009-10-31 20:06:59 +0000127 GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000128 }
129 }
130
131 void StartNewTable() {
132 SeenVBase.clear();
133 }
134
135 Index_t VBlookup(CXXRecordDecl *D, CXXRecordDecl *B);
136
Mike Stump8bccbfd2009-10-15 09:30:16 +0000137 Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B,
138 Index_t Offset = 0) {
139
140 if (B == D)
141 return Offset;
142
143 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D);
144 for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(),
145 e = D->bases_end(); i != e; ++i) {
146 const CXXRecordDecl *Base =
147 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
148 int64_t BaseOffset = 0;
149 if (!i->isVirtual())
150 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
151 int64_t o = getNVOffset_1(Base, B, BaseOffset);
152 if (o >= 0)
153 return o;
154 }
155
156 return -1;
157 }
158
159 /// getNVOffset - Returns the non-virtual offset for the given (B) base of the
160 /// derived class D.
161 Index_t getNVOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000162 qD = qD->getPointeeType();
163 qB = qB->getPointeeType();
Mike Stump8bccbfd2009-10-15 09:30:16 +0000164 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
165 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
166 int64_t o = getNVOffset_1(D, B);
167 if (o >= 0)
168 return o;
169
170 assert(false && "FIXME: non-virtual base not found");
171 return 0;
172 }
173
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000174 /// getVbaseOffset - Returns the index into the vtable for the virtual base
175 /// offset for the given (B) virtual base of the derived class D.
176 Index_t getVbaseOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000177 qD = qD->getPointeeType();
178 qB = qB->getPointeeType();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000179 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
180 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
181 if (D != Class)
182 return VBlookup(D, B);
183 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
184 i = VBIndex.find(B);
185 if (i != VBIndex.end())
186 return i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000187
Mike Stump28431212009-10-13 22:54:56 +0000188 assert(false && "FIXME: Base not found");
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000189 return 0;
190 }
191
192 bool OverrideMethod(const CXXMethodDecl *MD, llvm::Constant *m,
Mike Stump8bccbfd2009-10-15 09:30:16 +0000193 bool MorallyVirtual, Index_t OverrideOffset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000194 Index_t Offset, int64_t CurrentVBaseOffset) {
Mike Stump375faa82009-10-28 00:35:46 +0000195 const bool isPure = MD->isPure();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000196 typedef CXXMethodDecl::method_iterator meth_iter;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000197 // FIXME: Should OverrideOffset's be Offset?
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000198
199 // FIXME: Don't like the nested loops. For very large inheritance
200 // heirarchies we could have a table on the side with the final overridder
201 // and just replace each instance of an overridden method once. Would be
202 // nice to measure the cost/benefit on real code.
203
204 for (meth_iter mi = MD->begin_overridden_methods(),
205 e = MD->end_overridden_methods();
206 mi != e; ++mi) {
207 const CXXMethodDecl *OMD = *mi;
208 llvm::Constant *om;
209 om = CGM.GetAddrOfFunction(OMD, Ptr8Ty);
210 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
211
212 for (Index_t i = 0, e = submethods.size();
213 i != e; ++i) {
214 // FIXME: begin_overridden_methods might be too lax, covariance */
215 if (submethods[i] != om)
216 continue;
217 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType();
218 CanQualType oret = CGM.getContext().getCanonicalType(nc_oret);
219 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType();
220 CanQualType ret = CGM.getContext().getCanonicalType(nc_ret);
221 CallOffset ReturnOffset = std::make_pair(0, 0);
222 if (oret != ret) {
223 // FIXME: calculate offsets for covariance
Mike Stump87876a02009-10-13 10:55:21 +0000224 if (CovariantThunks.count(OMD)) {
225 oret = CovariantThunks[OMD].second;
226 CovariantThunks.erase(OMD);
227 }
Mike Stump8bccbfd2009-10-15 09:30:16 +0000228 // FIXME: Double check oret
229 Index_t nv = getNVOffset(oret, ret)/8;
Mike Stump87876a02009-10-13 10:55:21 +0000230 ReturnOffset = std::make_pair(nv, getVbaseOffset(oret, ret));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000231 }
232 Index[MD] = i;
233 submethods[i] = m;
Mike Stump375faa82009-10-28 00:35:46 +0000234 if (isPure)
235 Pures[MD] = 1;
236 Pures.erase(OMD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000237 Thunks.erase(OMD);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000238 if (MorallyVirtual || VCall.count(OMD)) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000239 Index_t &idx = VCall[OMD];
240 if (idx == 0) {
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000241 NonVirtualOffset[MD] = -OverrideOffset/8 + CurrentVBaseOffset/8;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000242 VCallOffset[MD] = OverrideOffset/8;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000243 idx = VCalls.size()+1;
244 VCalls.push_back(0);
Mike Stump75ce5732009-10-31 20:06:59 +0000245 D1(printf(" vcall for %s at %d with delta %d most derived %s\n",
Mike Stump72431bd2009-11-06 02:38:24 +0000246 MD->getNameAsCString(), (int)-idx-3, (int)VCalls[idx-1],
Mike Stump75ce5732009-10-31 20:06:59 +0000247 Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000248 } else {
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000249 NonVirtualOffset[MD] = NonVirtualOffset[OMD];
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000250 VCallOffset[MD] = VCallOffset[OMD];
Mike Stump8bccbfd2009-10-15 09:30:16 +0000251 VCalls[idx-1] = -VCallOffset[OMD] + OverrideOffset/8;
Mike Stump75ce5732009-10-31 20:06:59 +0000252 D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n",
Mike Stump72431bd2009-11-06 02:38:24 +0000253 MD->getNameAsCString(), (int)-idx-3, (int)VCalls[idx-1],
Mike Stump75ce5732009-10-31 20:06:59 +0000254 Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000255 }
256 VCall[MD] = idx;
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000257 int64_t O = NonVirtualOffset[MD];
258 int v = -((idx+extra+2)*LLVMPointerWidth/8);
259 // Optimize out virtual adjustments of 0.
260 if (VCalls[idx-1] == 0)
261 v = 0;
262 CallOffset ThisOffset = std::make_pair(O, v);
Mike Stump37dbe962009-10-15 02:04:03 +0000263 // FIXME: Do we always have to build a covariant thunk to save oret,
264 // which is the containing virtual base class?
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000265 if (ReturnOffset.first || ReturnOffset.second)
Mike Stump87876a02009-10-13 10:55:21 +0000266 CovariantThunks[MD] = std::make_pair(std::make_pair(ThisOffset,
267 ReturnOffset),
268 oret);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000269 else if (!isPure && (ThisOffset.first || ThisOffset.second))
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000270 Thunks[MD] = ThisOffset;
271 return true;
272 }
Mike Stump28431212009-10-13 22:54:56 +0000273
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000274 // FIXME: finish off
Mike Stump8bccbfd2009-10-15 09:30:16 +0000275 int64_t O = VCallOffset[OMD] - OverrideOffset/8;
Mike Stump72431bd2009-11-06 02:38:24 +0000276
Mike Stump28431212009-10-13 22:54:56 +0000277 if (O || ReturnOffset.first || ReturnOffset.second) {
278 CallOffset ThisOffset = std::make_pair(O, 0);
279
280 if (ReturnOffset.first || ReturnOffset.second)
281 CovariantThunks[MD] = std::make_pair(std::make_pair(ThisOffset,
282 ReturnOffset),
283 oret);
Mike Stump375faa82009-10-28 00:35:46 +0000284 else if (!isPure)
Mike Stump28431212009-10-13 22:54:56 +0000285 Thunks[MD] = ThisOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000286 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000287 return true;
288 }
289 }
290
291 return false;
292 }
293
294 void InstallThunks() {
295 for (Thunks_t::iterator i = Thunks.begin(), e = Thunks.end();
296 i != e; ++i) {
297 const CXXMethodDecl *MD = i->first;
Mike Stump375faa82009-10-28 00:35:46 +0000298 assert(!MD->isPure() && "Trying to thunk a pure");
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000299 Index_t idx = Index[MD];
300 Index_t nv_O = i->second.first;
301 Index_t v_O = i->second.second;
302 submethods[idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O);
303 }
304 Thunks.clear();
305 for (CovariantThunks_t::iterator i = CovariantThunks.begin(),
306 e = CovariantThunks.end();
307 i != e; ++i) {
308 const CXXMethodDecl *MD = i->first;
Mike Stump375faa82009-10-28 00:35:46 +0000309 if (MD->isPure())
310 continue;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000311 Index_t idx = Index[MD];
Mike Stump87876a02009-10-13 10:55:21 +0000312 Index_t nv_t = i->second.first.first.first;
313 Index_t v_t = i->second.first.first.second;
314 Index_t nv_r = i->second.first.second.first;
315 Index_t v_r = i->second.first.second.second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000316 submethods[idx] = CGM.BuildCovariantThunk(MD, Extern, nv_t, v_t, nv_r,
317 v_r);
318 }
319 CovariantThunks.clear();
Mike Stumpbb9ff052009-10-27 23:46:47 +0000320 for (Pures_t::iterator i = Pures.begin(), e = Pures.end();
321 i != e; ++i) {
322 const CXXMethodDecl *MD = i->first;
323 Index_t idx = Index[MD];
324 submethods[idx] = cxa_pure;
325 }
326 Pures.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000327 }
328
Mike Stump18e8b472009-10-27 23:36:26 +0000329 llvm::Constant *WrapAddrOf(const CXXMethodDecl *MD) {
330 if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD))
331 return wrap(CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete));
332
333 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
334 const llvm::Type *Ty =
335 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
336 FPT->isVariadic());
337
338 return wrap(CGM.GetAddrOfFunction(MD, Ty));
339 }
340
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000341 void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset,
342 int64_t CurrentVBaseOffset) {
Mike Stump37dbe962009-10-15 02:04:03 +0000343 for (Path_t::reverse_iterator i = Path->rbegin(),
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000344 e = Path->rend(); i != e; ++i) {
345 const CXXRecordDecl *RD = i->first;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000346 int64_t OverrideOffset = i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000347 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
348 ++mi) {
349 if (!mi->isVirtual())
350 continue;
351
352 const CXXMethodDecl *MD = *mi;
Mike Stump18e8b472009-10-27 23:36:26 +0000353 llvm::Constant *m = WrapAddrOf(MD);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000354 OverrideMethod(MD, m, MorallyVirtual, OverrideOffset, Offset,
355 CurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000356 }
357 }
358 }
359
Mike Stump75ce5732009-10-31 20:06:59 +0000360 void AddMethod(const CXXMethodDecl *MD, bool MorallyVirtual, Index_t Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000361 bool ForVirtualBase, int64_t CurrentVBaseOffset) {
Mike Stump18e8b472009-10-27 23:36:26 +0000362 llvm::Constant *m = WrapAddrOf(MD);
363
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000364 // If we can find a previously allocated slot for this, reuse it.
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000365 if (OverrideMethod(MD, m, MorallyVirtual, Offset, Offset,
366 CurrentVBaseOffset))
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000367 return;
368
369 // else allocate a new slot.
370 Index[MD] = submethods.size();
371 submethods.push_back(m);
Mike Stump75ce5732009-10-31 20:06:59 +0000372 D1(printf(" vfn for %s at %d\n", MD->getNameAsCString(), (int)Index[MD]));
Mike Stump375faa82009-10-28 00:35:46 +0000373 if (MD->isPure())
374 Pures[MD] = 1;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000375 if (MorallyVirtual) {
376 VCallOffset[MD] = Offset/8;
377 Index_t &idx = VCall[MD];
378 // Allocate the first one, after that, we reuse the previous one.
379 if (idx == 0) {
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000380 NonVirtualOffset[MD] = CurrentVBaseOffset/8 - Offset/8;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000381 idx = VCalls.size()+1;
382 VCalls.push_back(0);
Mike Stump75ce5732009-10-31 20:06:59 +0000383 D1(printf(" vcall for %s at %d with delta %d\n",
Mike Stump72431bd2009-11-06 02:38:24 +0000384 MD->getNameAsCString(), (int)-VCalls.size()-3, 0));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000385 }
386 }
387 }
388
389 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000390 Index_t Offset, bool RDisVirtualBase,
391 int64_t CurrentVBaseOffset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000392 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
393 ++mi)
394 if (mi->isVirtual())
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000395 AddMethod(*mi, MorallyVirtual, Offset, RDisVirtualBase,
396 CurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000397 }
398
399 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
400 const CXXRecordDecl *PrimaryBase,
401 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000402 int64_t Offset, int64_t CurrentVBaseOffset,
403 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000404 Path->push_back(std::make_pair(RD, Offset));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000405 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
406 e = RD->bases_end(); i != e; ++i) {
407 if (i->isVirtual())
408 continue;
409 const CXXRecordDecl *Base =
410 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
411 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
412 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
413 StartNewTable();
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000414 GenerateVtableForBase(Base, MorallyVirtual, o, false,
415 CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000416 }
417 }
Mike Stump37dbe962009-10-15 02:04:03 +0000418 Path->pop_back();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000419 }
420
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000421// #define D(X) do { X; } while (0)
422#define D(X)
423
424 void insertVCalls(int InsertionPoint) {
425 llvm::Constant *e = 0;
Mike Stump75ce5732009-10-31 20:06:59 +0000426 D1(printf("============= combining vbase/vcall\n"));
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000427 D(VCalls.insert(VCalls.begin(), 673));
428 D(VCalls.push_back(672));
Mike Stump8bccbfd2009-10-15 09:30:16 +0000429 methods.insert(methods.begin() + InsertionPoint, VCalls.size(), e);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000430 // The vcalls come first...
431 for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(),
432 e = VCalls.rend();
433 i != e; ++i)
434 methods[InsertionPoint++] = wrap((0?600:0) + *i);
435 VCalls.clear();
Mike Stump9f23a142009-11-10 02:30:51 +0000436 VCall.clear();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000437 }
438
Mike Stump75ce5732009-10-31 20:06:59 +0000439 Index_t end(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
440 const CXXRecordDecl *PrimaryBase, bool PrimaryBaseWasVirtual,
441 bool MorallyVirtual, int64_t Offset, bool ForVirtualBase,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000442 int64_t CurrentVBaseOffset,
Mike Stump75ce5732009-10-31 20:06:59 +0000443 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000444 bool alloc = false;
445 if (Path == 0) {
446 alloc = true;
447 Path = new Path_t;
448 }
449
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000450 StartNewTable();
451 extra = 0;
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000452 bool DeferVCalls = MorallyVirtual || ForVirtualBase;
453 int VCallInsertionPoint = methods.size();
454 if (!DeferVCalls) {
455 insertVCalls(VCallInsertionPoint);
Mike Stump8bccbfd2009-10-15 09:30:16 +0000456 } else
457 // FIXME: just for extra, or for all uses of VCalls.size post this?
458 extra = -VCalls.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000459
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000460 methods.push_back(wrap(-(Offset/8)));
461 methods.push_back(rtti);
462 Index_t AddressPoint = methods.size();
463
464 InstallThunks();
Mike Stump75ce5732009-10-31 20:06:59 +0000465 D1(printf("============= combining methods\n"));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000466 methods.insert(methods.end(), submethods.begin(), submethods.end());
467 submethods.clear();
468
469 // and then the non-virtual bases.
470 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000471 MorallyVirtual, Offset, CurrentVBaseOffset, Path);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000472
473 if (ForVirtualBase) {
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000474 insertVCalls(VCallInsertionPoint);
475 AddressPoint += VCalls.size();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000476 }
477
Mike Stump37dbe962009-10-15 02:04:03 +0000478 if (alloc) {
479 delete Path;
480 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000481 return AddressPoint;
482 }
483
Mike Stump75ce5732009-10-31 20:06:59 +0000484 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
485 bool updateVBIndex, Index_t current_vbindex,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000486 bool RDisVirtualBase, int64_t CurrentVBaseOffset) {
Mike Stump75ce5732009-10-31 20:06:59 +0000487 if (!RD->isDynamicClass())
488 return;
489
490 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
491 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
492 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
493
494 // vtables are composed from the chain of primaries.
495 if (PrimaryBase) {
496 D1(printf(" doing primaries for %s most derived %s\n",
497 RD->getNameAsCString(), Class->getNameAsCString()));
498
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000499 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
500 if (PrimaryBaseWasVirtual)
501 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
502
Mike Stump75ce5732009-10-31 20:06:59 +0000503 if (!PrimaryBaseWasVirtual)
504 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000505 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
506 BaseCurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000507 }
508
509 D1(printf(" doing vcall entries for %s most derived %s\n",
510 RD->getNameAsCString(), Class->getNameAsCString()));
511
512 // And add the virtuals for the class to the primary vtable.
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000513 AddMethods(RD, MorallyVirtual, Offset, RDisVirtualBase, CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000514 }
515
516 void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
517 bool updateVBIndex, Index_t current_vbindex,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000518 bool RDisVirtualBase, int64_t CurrentVBaseOffset,
519 bool bottom=false) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000520 if (!RD->isDynamicClass())
521 return;
522
523 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
524 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
525 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
526
527 // vtables are composed from the chain of primaries.
528 if (PrimaryBase) {
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000529 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
530 if (PrimaryBaseWasVirtual) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000531 IndirectPrimary.insert(PrimaryBase);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000532 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
533 }
Mike Stump75ce5732009-10-31 20:06:59 +0000534
535 D1(printf(" doing primaries for %s most derived %s\n",
536 RD->getNameAsCString(), Class->getNameAsCString()));
537
538 VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000539 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
540 BaseCurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000541 }
542
Mike Stump75ce5732009-10-31 20:06:59 +0000543 D1(printf(" doing vbase entries for %s most derived %s\n",
544 RD->getNameAsCString(), Class->getNameAsCString()));
545 GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex);
546
547 if (RDisVirtualBase || bottom) {
548 Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000549 RDisVirtualBase, CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000550 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000551 }
552
553 int64_t GenerateVtableForBase(const CXXRecordDecl *RD,
554 bool MorallyVirtual = false, int64_t Offset = 0,
555 bool ForVirtualBase = false,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000556 int CurrentVBaseOffset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +0000557 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000558 if (!RD->isDynamicClass())
559 return 0;
560
561 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
562 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
563 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
564
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000565 extra = 0;
Mike Stump75ce5732009-10-31 20:06:59 +0000566 D1(printf("building entries for base %s most derived %s\n",
567 RD->getNameAsCString(), Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000568
Mike Stump75ce5732009-10-31 20:06:59 +0000569 if (ForVirtualBase)
570 extra = VCalls.size();
571
572 VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0, ForVirtualBase,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000573 CurrentVBaseOffset, true);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000574
575 if (Path)
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000576 OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000577
Mike Stump75ce5732009-10-31 20:06:59 +0000578 return end(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000579 Offset, ForVirtualBase, CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000580 }
581
582 void GenerateVtableForVBases(const CXXRecordDecl *RD,
583 int64_t Offset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +0000584 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000585 bool alloc = false;
586 if (Path == 0) {
587 alloc = true;
Mike Stump37dbe962009-10-15 02:04:03 +0000588 Path = new Path_t;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000589 }
590 // FIXME: We also need to override using all paths to a virtual base,
591 // right now, we just process the first path
592 Path->push_back(std::make_pair(RD, Offset));
593 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
594 e = RD->bases_end(); i != e; ++i) {
595 const CXXRecordDecl *Base =
596 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
597 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
598 // Mark it so we don't output it twice.
599 IndirectPrimary.insert(Base);
600 StartNewTable();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000601 VCall.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000602 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000603 int64_t CurrentVBaseOffset = BaseOffset;
Mike Stump75ce5732009-10-31 20:06:59 +0000604 D1(printf("vtable %s virtual base %s\n",
605 Class->getNameAsCString(), Base->getNameAsCString()));
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000606 GenerateVtableForBase(Base, true, BaseOffset, true, CurrentVBaseOffset,
607 Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000608 }
609 int64_t BaseOffset = Offset;
610 if (i->isVirtual())
611 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump37dbe962009-10-15 02:04:03 +0000612 if (Base->getNumVBases()) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000613 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump37dbe962009-10-15 02:04:03 +0000614 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000615 }
616 Path->pop_back();
617 if (alloc)
618 delete Path;
619 }
620};
621
622
623VtableBuilder::Index_t VtableBuilder::VBlookup(CXXRecordDecl *D,
624 CXXRecordDecl *B) {
625 return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B);
626}
627
628int64_t CGVtableInfo::getMethodVtableIndex(const CXXMethodDecl *MD) {
629 MD = MD->getCanonicalDecl();
630
631 MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(MD);
632 if (I != MethodVtableIndices.end())
633 return I->second;
634
635 const CXXRecordDecl *RD = MD->getParent();
636
637 std::vector<llvm::Constant *> methods;
638 // FIXME: This seems expensive. Can we do a partial job to get
639 // just this data.
640 VtableBuilder b(methods, RD, CGM);
Mike Stump75ce5732009-10-31 20:06:59 +0000641 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000642 b.GenerateVtableForBase(RD);
643 b.GenerateVtableForVBases(RD);
644
645 MethodVtableIndices.insert(b.getIndex().begin(),
646 b.getIndex().end());
647
648 I = MethodVtableIndices.find(MD);
649 assert(I != MethodVtableIndices.end() && "Did not find index!");
650 return I->second;
651}
652
653int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
654 const CXXRecordDecl *VBase) {
655 ClassPairTy ClassPair(RD, VBase);
656
657 VirtualBaseClassIndiciesTy::iterator I =
658 VirtualBaseClassIndicies.find(ClassPair);
659 if (I != VirtualBaseClassIndicies.end())
660 return I->second;
661
662 std::vector<llvm::Constant *> methods;
663 // FIXME: This seems expensive. Can we do a partial job to get
664 // just this data.
665 VtableBuilder b(methods, RD, CGM);
Mike Stump75ce5732009-10-31 20:06:59 +0000666 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000667 b.GenerateVtableForBase(RD);
668 b.GenerateVtableForVBases(RD);
669
670 for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
671 b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) {
672 // Insert all types.
673 ClassPairTy ClassPair(RD, I->first);
674
675 VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second));
676 }
677
678 I = VirtualBaseClassIndicies.find(ClassPair);
679 assert(I != VirtualBaseClassIndicies.end() && "Did not find index!");
680
681 return I->second;
682}
683
Mike Stumpd846d082009-11-10 07:44:33 +0000684llvm::Constant *CodeGenModule::GenerateVtable(const CXXRecordDecl *RD) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000685 llvm::SmallString<256> OutName;
686 llvm::raw_svector_ostream Out(OutName);
Mike Stumpd846d082009-11-10 07:44:33 +0000687 mangleCXXVtable(getMangleContext(), RD, Out);
Benjamin Kramerbb0a07b2009-10-11 22:57:54 +0000688
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000689 llvm::GlobalVariable::LinkageTypes linktype;
Chandler Carruth6e0df532009-10-26 17:14:14 +0000690 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000691 std::vector<llvm::Constant *> methods;
692 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
693 int64_t AddressPoint;
694
Mike Stumpd846d082009-11-10 07:44:33 +0000695 VtableBuilder b(methods, RD, *this);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000696
Mike Stump75ce5732009-10-31 20:06:59 +0000697 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000698 // First comes the vtables for all the non-virtual bases...
699 AddressPoint = b.GenerateVtableForBase(RD);
700
701 // then the vtables for all the virtual bases.
702 b.GenerateVtableForVBases(RD);
703
704 llvm::Constant *C;
705 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size());
706 C = llvm::ConstantArray::get(type, methods);
Mike Stumpd846d082009-11-10 07:44:33 +0000707 llvm::Constant *vtable = new llvm::GlobalVariable(getModule(), type,
708 true, linktype, C,
709 Out.str());
710 vtable = llvm::ConstantExpr::getBitCast(vtable, Ptr8Ty);
711 llvm::Constant *AddressPointC;
712 uint32_t LLVMPointerWidth = getContext().Target.getPointerWidth(0);
713 AddressPointC = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
714 AddressPoint*LLVMPointerWidth/8);
715 vtable = llvm::ConstantExpr::getGetElementPtr(vtable, &AddressPointC, 1);
716
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000717 return vtable;
718}
Mike Stump9f23a142009-11-10 02:30:51 +0000719
720class VTTBuilder {
721 /// Inits - The list of values built for the VTT.
722 std::vector<llvm::Constant *> &Inits;
723 /// Class - The most derived class that this vtable is being built for.
724 const CXXRecordDecl *Class;
725 CodeGenModule &CGM; // Per-module state.
Mike Stump8b2d2d02009-11-11 00:35:07 +0000726 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
Mike Stumpc7b9f5e2009-11-11 03:08:24 +0000727 /// BLayout - Layout for the most derived class that this vtable is being
728 /// built for.
729 const ASTRecordLayout &BLayout;
Mike Stump9f23a142009-11-10 02:30:51 +0000730
Mike Stump8b2d2d02009-11-11 00:35:07 +0000731 /// Secondary - Add the secondary vtable pointers to Inits.
Mike Stumpc7b9f5e2009-11-11 03:08:24 +0000732 void Secondary(const CXXRecordDecl *RD, uint64_t Offset=0,
733 bool MorallyVirtual=false) {
Mike Stump8b2d2d02009-11-11 00:35:07 +0000734 if (RD->getNumVBases() == 0 && ! MorallyVirtual)
735 return;
736
737 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
738 e = RD->bases_end(); i != e; ++i) {
739 const CXXRecordDecl *Base =
740 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
741 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
742 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
743 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
744 bool NonVirtualPrimaryBase;
745 NonVirtualPrimaryBase = !PrimaryBaseWasVirtual && Base == PrimaryBase;
746 bool BaseMorallyVirtual = MorallyVirtual | i->isVirtual();
Mike Stumpc7b9f5e2009-11-11 03:08:24 +0000747 uint64_t BaseOffset;
748 if (!i->isVirtual()) {
749 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
750 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
751 } else
752 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump8b2d2d02009-11-11 00:35:07 +0000753 if ((Base->getNumVBases() || BaseMorallyVirtual)
754 && !NonVirtualPrimaryBase) {
755 // FIXME: Slightly too many of these for __ZTT8test8_B2
Mike Stumpc7b9f5e2009-11-11 03:08:24 +0000756 llvm::Constant *vtbl;
757 vtbl = CGM.getVtableInfo().getVtable(Base, Class, BaseOffset/8);
Mike Stump8b2d2d02009-11-11 00:35:07 +0000758 Inits.push_back(vtbl);
759 }
Mike Stumpc7b9f5e2009-11-11 03:08:24 +0000760 Secondary(Base, BaseOffset, BaseMorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +0000761 }
762 }
763
764 /// BuiltVTT - Add the VTT to Inits.
Mike Stumpc7b9f5e2009-11-11 03:08:24 +0000765 void BuildVTT(const CXXRecordDecl *RD, uint64_t Offset,
766 bool MorallyVirtual=false) {
Mike Stump8b2d2d02009-11-11 00:35:07 +0000767 if (RD->getNumVBases() == 0 && !MorallyVirtual)
768 return;
769
770 // First comes the primary virtual table pointer...
Mike Stumpc7b9f5e2009-11-11 03:08:24 +0000771 Inits.push_back(CGM.getVtableInfo().getVtable(RD, Class, Offset));
Mike Stump8b2d2d02009-11-11 00:35:07 +0000772
773 // then the secondary VTTs....
774 SecondaryVTTs(RD, MorallyVirtual);
775
776 // and last the secondary vtable pointers.
Mike Stumpc7b9f5e2009-11-11 03:08:24 +0000777 Secondary(RD, MorallyVirtual, Offset);
Mike Stump8b2d2d02009-11-11 00:35:07 +0000778 }
779
780 /// SecondaryVTTs - Add the secondary VTTs to Inits. The secondary VTTs are
781 /// built from each direct non-virtual proper base that requires a VTT in
782 /// declaration order.
783 void SecondaryVTTs(const CXXRecordDecl *RD, bool MorallyVirtual=false) {
784 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
785 e = RD->bases_end(); i != e; ++i) {
786 const CXXRecordDecl *Base =
787 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
788 if (i->isVirtual())
789 continue;
790 BuildVTT(Base, MorallyVirtual);
791 }
792 }
793
794 /// VirtualVTTs - Add the VTT for each proper virtual base in inheritance
795 /// graph preorder.
796 void VirtualVTTs(const CXXRecordDecl *RD) {
797 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
798 e = RD->bases_end(); i != e; ++i) {
799 const CXXRecordDecl *Base =
800 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
801 if (i->isVirtual() && !SeenVBase.count(Base)) {
802 SeenVBase.insert(Base);
803 BuildVTT(Base, true);
804 }
805 VirtualVTTs(Base);
806 }
807 }
Mike Stump9f23a142009-11-10 02:30:51 +0000808public:
809 VTTBuilder(std::vector<llvm::Constant *> &inits, const CXXRecordDecl *c,
Mike Stumpc7b9f5e2009-11-11 03:08:24 +0000810 CodeGenModule &cgm)
811 : Inits(inits), Class(c), CGM(cgm),
812 BLayout(cgm.getContext().getASTRecordLayout(c)) {
Mike Stumpd846d082009-11-10 07:44:33 +0000813
Mike Stump8b2d2d02009-11-11 00:35:07 +0000814 // First comes the primary virtual table pointer for the complete class...
Mike Stumpd846d082009-11-10 07:44:33 +0000815 Inits.push_back(CGM.getVtableInfo().getVtable(Class));
Mike Stump8b2d2d02009-11-11 00:35:07 +0000816
817 // then the secondary VTTs...
818 SecondaryVTTs(Class);
819
820 // then the secondary vtable pointers...
821 Secondary(Class);
822
823 // and last, the virtual VTTs.
824 VirtualVTTs(Class);
Mike Stump9f23a142009-11-10 02:30:51 +0000825 }
826};
827
Mike Stumpd846d082009-11-10 07:44:33 +0000828llvm::Constant *CodeGenModule::GenerateVTT(const CXXRecordDecl *RD) {
Mike Stumpb4722212009-11-10 19:13:04 +0000829 // Only classes that have virtual bases need a VTT.
830 if (RD->getNumVBases() == 0)
831 return 0;
832
Mike Stump9f23a142009-11-10 02:30:51 +0000833 llvm::SmallString<256> OutName;
834 llvm::raw_svector_ostream Out(OutName);
Mike Stumpd846d082009-11-10 07:44:33 +0000835 mangleCXXVTT(getMangleContext(), RD, Out);
Mike Stump9f23a142009-11-10 02:30:51 +0000836
837 llvm::GlobalVariable::LinkageTypes linktype;
838 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
839 std::vector<llvm::Constant *> inits;
840 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
841
Mike Stump9f23a142009-11-10 02:30:51 +0000842 D1(printf("vtt %s\n", RD->getNameAsCString()));
843
Mike Stump8b2d2d02009-11-11 00:35:07 +0000844 VTTBuilder b(inits, RD, *this);
845
Mike Stump9f23a142009-11-10 02:30:51 +0000846 llvm::Constant *C;
847 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, inits.size());
848 C = llvm::ConstantArray::get(type, inits);
Mike Stumpd846d082009-11-10 07:44:33 +0000849 llvm::Constant *vtt = new llvm::GlobalVariable(getModule(), type, true,
850 linktype, C, Out.str());
851 vtt = llvm::ConstantExpr::getBitCast(vtt, Ptr8Ty);
Mike Stump9f23a142009-11-10 02:30:51 +0000852 return vtt;
853}
Mike Stumpd846d082009-11-10 07:44:33 +0000854
Mike Stumpc7b9f5e2009-11-11 03:08:24 +0000855llvm::Constant *CGVtableInfo::getVtable(const CXXRecordDecl *RD,
856 const CXXRecordDecl *Class,
857 uint64_t Offset) {
858 // FIXME: Add ctor vtable support
Mike Stumpd846d082009-11-10 07:44:33 +0000859 llvm::Constant *&vtbl = Vtables[RD];
860 if (vtbl)
861 return vtbl;
862 vtbl = CGM.GenerateVtable(RD);
863 CGM.GenerateVTT(RD);
864 return vtbl;
865}