blob: 8cbd6adb085e551a25733a913b2cfaa5c81950b9 [file] [log] [blame]
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001//===--- CGVtable.cpp - Emit LLVM Code for C++ vtables --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation of virtual tables.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenModule.h"
15#include "CodeGenFunction.h"
Anders Carlssonf942ee02009-11-27 20:47:55 +000016#include "clang/AST/CXXInheritance.h"
Anders Carlsson2bb27f52009-10-11 22:13:54 +000017#include "clang/AST/RecordLayout.h"
Anders Carlssond420a312009-11-26 19:32:45 +000018#include "llvm/ADT/DenseSet.h"
Zhongxing Xu1721ef72009-11-13 05:46:16 +000019#include <cstdio>
Anders Carlsson2bb27f52009-10-11 22:13:54 +000020
21using namespace clang;
22using namespace CodeGen;
23
Anders Carlssond59885022009-11-27 22:21:51 +000024namespace {
Benjamin Kramer337e3a52009-11-28 19:45:26 +000025class VtableBuilder {
Anders Carlsson2bb27f52009-10-11 22:13:54 +000026public:
27 /// Index_t - Vtable index type.
28 typedef uint64_t Index_t;
29private:
30 std::vector<llvm::Constant *> &methods;
31 std::vector<llvm::Constant *> submethods;
32 llvm::Type *Ptr8Ty;
33 /// Class - The most derived class that this vtable is being built for.
34 const CXXRecordDecl *Class;
Mike Stump83066c82009-11-13 01:54:23 +000035 /// LayoutClass - The most derived class used for virtual base layout
36 /// information.
37 const CXXRecordDecl *LayoutClass;
Mike Stump653d0b92009-11-13 02:13:54 +000038 /// LayoutOffset - The offset for Class in LayoutClass.
39 uint64_t LayoutOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000040 /// BLayout - Layout for the most derived class that this vtable is being
41 /// built for.
42 const ASTRecordLayout &BLayout;
43 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
44 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
45 llvm::Constant *rtti;
46 llvm::LLVMContext &VMContext;
47 CodeGenModule &CGM; // Per-module state.
48 /// Index - Maps a method decl into a vtable index. Useful for virtual
49 /// dispatch codegen.
Anders Carlssonfb4dda42009-11-13 17:08:56 +000050 llvm::DenseMap<GlobalDecl, Index_t> Index;
51 llvm::DenseMap<GlobalDecl, Index_t> VCall;
52 llvm::DenseMap<GlobalDecl, Index_t> VCallOffset;
Mike Stumpcd6f9ed2009-11-06 23:27:42 +000053 // This is the offset to the nearest virtual base
Anders Carlssonfb4dda42009-11-13 17:08:56 +000054 llvm::DenseMap<GlobalDecl, Index_t> NonVirtualOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000055 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stumpbb9ff052009-10-27 23:46:47 +000056
Anders Carlsson323bb042009-11-26 19:54:33 +000057 /// PureVirtualFunction - Points to __cxa_pure_virtual.
58 llvm::Constant *PureVirtualFn;
59
Anders Carlsson6d771bc2009-11-26 03:25:13 +000060 /// Thunk - Represents a single thunk.
61 struct Thunk {
Anders Carlsson80bc5d52009-12-03 02:41:55 +000062 Thunk() { }
Anders Carlsson6d771bc2009-11-26 03:25:13 +000063
Anders Carlsson80bc5d52009-12-03 02:41:55 +000064 Thunk(GlobalDecl GD, const ThunkAdjustment &Adjustment)
65 : GD(GD), Adjustment(Adjustment) { }
Anders Carlsson6d771bc2009-11-26 03:25:13 +000066
Anders Carlsson0e1e7632009-12-03 02:36:40 +000067 GlobalDecl GD;
68
Anders Carlssond420a312009-11-26 19:32:45 +000069 /// Adjustment - The thunk adjustment.
Anders Carlsson6d771bc2009-11-26 03:25:13 +000070 ThunkAdjustment Adjustment;
71 };
Anders Carlssond420a312009-11-26 19:32:45 +000072
73 /// Thunks - The thunks in a vtable.
Anders Carlsson29a1f752009-12-03 02:39:59 +000074 typedef llvm::DenseMap<uint64_t, Thunk> ThunksMapTy;
Anders Carlsson6d771bc2009-11-26 03:25:13 +000075 ThunksMapTy Thunks;
Anders Carlssond420a312009-11-26 19:32:45 +000076
77 /// CovariantThunk - Represents a single covariant thunk.
78 struct CovariantThunk {
Anders Carlsson1157e8f2009-12-03 02:20:26 +000079 CovariantThunk() { }
80
Anders Carlsson9f98f7a2009-12-03 02:34:59 +000081 CovariantThunk(GlobalDecl GD, CanQualType ReturnType)
82 : GD(GD), ReturnType(ReturnType) { }
Anders Carlsson06c14b62009-12-03 02:16:14 +000083
Anders Carlssonc38b40a2009-12-03 02:03:29 +000084 GlobalDecl GD;
85
Anders Carlssond420a312009-11-26 19:32:45 +000086 /// ReturnType - The return type of the function.
87 CanQualType ReturnType;
88 };
Anders Carlsson6d771bc2009-11-26 03:25:13 +000089
Anders Carlssond420a312009-11-26 19:32:45 +000090 /// CovariantThunks - The covariant thunks in a vtable.
Anders Carlsson73295f92009-12-03 02:12:03 +000091 typedef llvm::DenseMap<uint64_t, CovariantThunk> CovariantThunksMapTy;
Anders Carlssond420a312009-11-26 19:32:45 +000092 CovariantThunksMapTy CovariantThunks;
93
94 /// PureVirtualMethods - Pure virtual methods.
95 typedef llvm::DenseSet<GlobalDecl> PureVirtualMethodsSetTy;
96 PureVirtualMethodsSetTy PureVirtualMethods;
97
Anders Carlsson2bb27f52009-10-11 22:13:54 +000098 std::vector<Index_t> VCalls;
Mike Stump2cefe382009-11-12 20:47:57 +000099
100 typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t;
Mike Stumpcd2b8212009-11-19 20:52:19 +0000101 // subAddressPoints - Used to hold the AddressPoints (offsets) into the built
102 // vtable for use in computing the initializers for the VTT.
103 llvm::DenseMap<CtorVtable_t, int64_t> &subAddressPoints;
Mike Stump2cefe382009-11-12 20:47:57 +0000104
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000105 typedef CXXRecordDecl::method_iterator method_iter;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000106 const bool Extern;
107 const uint32_t LLVMPointerWidth;
108 Index_t extra;
Mike Stump37dbe962009-10-15 02:04:03 +0000109 typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t;
Mike Stumpcd2b8212009-11-19 20:52:19 +0000110 static llvm::DenseMap<CtorVtable_t, int64_t>&
111 AllocAddressPoint(CodeGenModule &cgm, const CXXRecordDecl *l,
112 const CXXRecordDecl *c) {
113 CodeGenModule::AddrMap_t *&oref = cgm.AddressPoints[l];
114 if (oref == 0)
115 oref = new CodeGenModule::AddrMap_t;
116
117 llvm::DenseMap<CtorVtable_t, int64_t> *&ref = (*oref)[c];
118 if (ref == 0)
119 ref = new llvm::DenseMap<CtorVtable_t, int64_t>;
120 return *ref;
121 }
Anders Carlsson323bb042009-11-26 19:54:33 +0000122
123 /// getPureVirtualFn - Return the __cxa_pure_virtual function.
124 llvm::Constant* getPureVirtualFn() {
125 if (!PureVirtualFn) {
126 const llvm::FunctionType *Ty =
127 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
128 /*isVarArg=*/false);
129 PureVirtualFn = wrap(CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual"));
130 }
131
132 return PureVirtualFn;
133 }
134
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000135public:
Mike Stump653d0b92009-11-13 02:13:54 +0000136 VtableBuilder(std::vector<llvm::Constant *> &meth, const CXXRecordDecl *c,
137 const CXXRecordDecl *l, uint64_t lo, CodeGenModule &cgm)
138 : methods(meth), Class(c), LayoutClass(l), LayoutOffset(lo),
Mike Stump83066c82009-11-13 01:54:23 +0000139 BLayout(cgm.getContext().getASTRecordLayout(l)),
Mike Stumpc01c2b82009-12-02 18:57:08 +0000140 rtti(cgm.GenerateRTTIRef(c)), VMContext(cgm.getModule().getContext()),
Anders Carlsson323bb042009-11-26 19:54:33 +0000141 CGM(cgm), PureVirtualFn(0),subAddressPoints(AllocAddressPoint(cgm, l, c)),
Mike Stump1960b202009-11-19 00:49:05 +0000142 Extern(!l->isInAnonymousNamespace()),
Anders Carlsson323bb042009-11-26 19:54:33 +0000143 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000144 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
145 }
146
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000147 llvm::DenseMap<GlobalDecl, Index_t> &getIndex() { return Index; }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000148 llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex()
149 { return VBIndex; }
150
151 llvm::Constant *wrap(Index_t i) {
152 llvm::Constant *m;
153 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
154 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
155 }
156
157 llvm::Constant *wrap(llvm::Constant *m) {
158 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
159 }
160
Mike Stump8a96d3a2009-12-02 19:50:41 +0000161#define D1(x)
162//#define D1(X) do { if (getenv("DEBUG")) { X; } } while (0)
Mike Stump75ce5732009-10-31 20:06:59 +0000163
164 void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset,
Mike Stump28431212009-10-13 22:54:56 +0000165 bool updateVBIndex, Index_t current_vbindex) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000166 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
167 e = RD->bases_end(); i != e; ++i) {
168 const CXXRecordDecl *Base =
169 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump28431212009-10-13 22:54:56 +0000170 Index_t next_vbindex = current_vbindex;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000171 if (i->isVirtual() && !SeenVBase.count(Base)) {
172 SeenVBase.insert(Base);
Mike Stump28431212009-10-13 22:54:56 +0000173 if (updateVBIndex) {
Mike Stump75ce5732009-10-31 20:06:59 +0000174 next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8)
Mike Stump28431212009-10-13 22:54:56 +0000175 - 3*LLVMPointerWidth/8);
176 VBIndex[Base] = next_vbindex;
177 }
Mike Stump75ce5732009-10-31 20:06:59 +0000178 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
179 VCalls.push_back((0?700:0) + BaseOffset);
180 D1(printf(" vbase for %s at %d delta %d most derived %s\n",
181 Base->getNameAsCString(),
182 (int)-VCalls.size()-3, (int)BaseOffset,
183 Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000184 }
Mike Stump28431212009-10-13 22:54:56 +0000185 // We also record offsets for non-virtual bases to closest enclosing
186 // virtual base. We do this so that we don't have to search
187 // for the nearst virtual base class when generating thunks.
188 if (updateVBIndex && VBIndex.count(Base) == 0)
189 VBIndex[Base] = next_vbindex;
Mike Stump75ce5732009-10-31 20:06:59 +0000190 GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000191 }
192 }
193
194 void StartNewTable() {
195 SeenVBase.clear();
196 }
197
Mike Stump8bccbfd2009-10-15 09:30:16 +0000198 Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B,
199 Index_t Offset = 0) {
200
201 if (B == D)
202 return Offset;
203
204 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D);
205 for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(),
206 e = D->bases_end(); i != e; ++i) {
207 const CXXRecordDecl *Base =
208 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
209 int64_t BaseOffset = 0;
210 if (!i->isVirtual())
211 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
212 int64_t o = getNVOffset_1(Base, B, BaseOffset);
213 if (o >= 0)
214 return o;
215 }
216
217 return -1;
218 }
219
220 /// getNVOffset - Returns the non-virtual offset for the given (B) base of the
221 /// derived class D.
222 Index_t getNVOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000223 qD = qD->getPointeeType();
224 qB = qB->getPointeeType();
Mike Stump8bccbfd2009-10-15 09:30:16 +0000225 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
226 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
227 int64_t o = getNVOffset_1(D, B);
228 if (o >= 0)
229 return o;
230
231 assert(false && "FIXME: non-virtual base not found");
232 return 0;
233 }
234
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000235 /// getVbaseOffset - Returns the index into the vtable for the virtual base
236 /// offset for the given (B) virtual base of the derived class D.
237 Index_t getVbaseOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000238 qD = qD->getPointeeType();
239 qB = qB->getPointeeType();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000240 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
241 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
242 if (D != Class)
Eli Friedman03aa2f12009-11-30 01:19:33 +0000243 return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000244 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
245 i = VBIndex.find(B);
246 if (i != VBIndex.end())
247 return i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000248
Mike Stump28431212009-10-13 22:54:56 +0000249 assert(false && "FIXME: Base not found");
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000250 return 0;
251 }
252
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000253 bool OverrideMethod(GlobalDecl GD, llvm::Constant *m,
Mike Stump8bccbfd2009-10-15 09:30:16 +0000254 bool MorallyVirtual, Index_t OverrideOffset,
Anders Carlssonca1bf682009-12-03 01:54:02 +0000255 Index_t Offset, int64_t CurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000256
257 void InstallThunks() {
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000258 for (CovariantThunksMapTy::const_iterator i = CovariantThunks.begin(),
259 e = CovariantThunks.end(); i != e; ++i) {
Anders Carlsson73295f92009-12-03 02:12:03 +0000260 GlobalDecl GD = i->second.GD;
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000261 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
262 if (MD->isPure())
263 continue;
264
Anders Carlsson06c14b62009-12-03 02:16:14 +0000265 uint64_t Index = i->first;
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000266 const CovariantThunk &Thunk = i->second;
Anders Carlsson06c14b62009-12-03 02:16:14 +0000267 assert(Index == VtableBuilder::Index[GD] && "Thunk index mismatch!");
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000268
269 // Check if there is an adjustment for the 'this' pointer.
270 ThunkAdjustment ThisAdjustment;
Anders Carlssonc6089fd2009-12-03 07:30:40 +0000271 ThunksMapTy::iterator it = Thunks.find(Index);
272 if (it != Thunks.end()) {
273 ThisAdjustment = it->second.Adjustment;
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000274
Anders Carlssonc6089fd2009-12-03 07:30:40 +0000275 Thunks.erase(it);
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000276 }
Anders Carlsson657f1392009-12-03 02:32:59 +0000277
278 // Construct the return adjustment.
Anders Carlssondabfa3c2009-12-03 03:28:24 +0000279 QualType DerivedType =
280 MD->getType()->getAs<FunctionType>()->getResultType();
281
282 int64_t NonVirtualAdjustment =
283 getNVOffset(Thunk.ReturnType, DerivedType) / 8;
284
285 int64_t VirtualAdjustment =
286 getVbaseOffset(Thunk.ReturnType, DerivedType);
287
288 ThunkAdjustment ReturnAdjustment(NonVirtualAdjustment, VirtualAdjustment);
Anders Carlsson657f1392009-12-03 02:32:59 +0000289
290 CovariantThunkAdjustment Adjustment(ThisAdjustment, ReturnAdjustment);
Anders Carlsson06c14b62009-12-03 02:16:14 +0000291 submethods[Index] = CGM.BuildCovariantThunk(MD, Extern, Adjustment);
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000292 }
293 CovariantThunks.clear();
Anders Carlssonc38b40a2009-12-03 02:03:29 +0000294
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000295 for (ThunksMapTy::const_iterator i = Thunks.begin(), e = Thunks.end();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000296 i != e; ++i) {
Anders Carlsson80bc5d52009-12-03 02:41:55 +0000297 uint64_t Index = i->first;
298 const Thunk& Thunk = i->second;
299
300 GlobalDecl GD = Thunk.GD;
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000301 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000302 assert(!MD->isPure() && "Can't thunk pure virtual methods!");
Anders Carlsson29a1f752009-12-03 02:39:59 +0000303
304 assert(Index == VtableBuilder::Index[GD] && "Thunk index mismatch!");
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000305
Mike Stumpe2d4a2c2009-12-03 03:47:56 +0000306 submethods[Index] = CGM.BuildThunk(GD, Extern, Thunk.Adjustment);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000307 }
308 Thunks.clear();
Anders Carlsson6d771bc2009-11-26 03:25:13 +0000309
Anders Carlssond420a312009-11-26 19:32:45 +0000310 for (PureVirtualMethodsSetTy::iterator i = PureVirtualMethods.begin(),
311 e = PureVirtualMethods.end(); i != e; ++i) {
312 GlobalDecl GD = *i;
Anders Carlsson323bb042009-11-26 19:54:33 +0000313 submethods[Index[GD]] = getPureVirtualFn();
Mike Stumpbb9ff052009-10-27 23:46:47 +0000314 }
Anders Carlssond420a312009-11-26 19:32:45 +0000315 PureVirtualMethods.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000316 }
317
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000318 llvm::Constant *WrapAddrOf(GlobalDecl GD) {
319 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
320
Anders Carlsson64457732009-11-24 05:08:52 +0000321 const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD);
Mike Stump18e8b472009-10-27 23:36:26 +0000322
Mike Stumpcdeb8002009-12-03 16:55:20 +0000323 return wrap(CGM.GetAddrOfFunction(GD, Ty));
Mike Stump18e8b472009-10-27 23:36:26 +0000324 }
325
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000326 void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset,
327 int64_t CurrentVBaseOffset) {
Mike Stump37dbe962009-10-15 02:04:03 +0000328 for (Path_t::reverse_iterator i = Path->rbegin(),
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000329 e = Path->rend(); i != e; ++i) {
330 const CXXRecordDecl *RD = i->first;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000331 int64_t OverrideOffset = i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000332 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
333 ++mi) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000334 const CXXMethodDecl *MD = *mi;
335
336 if (!MD->isVirtual())
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000337 continue;
338
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000339 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
340 // Override both the complete and the deleting destructor.
341 GlobalDecl CompDtor(DD, Dtor_Complete);
342 OverrideMethod(CompDtor, WrapAddrOf(CompDtor), MorallyVirtual,
343 OverrideOffset, Offset, CurrentVBaseOffset);
344
345 GlobalDecl DeletingDtor(DD, Dtor_Deleting);
346 OverrideMethod(DeletingDtor, WrapAddrOf(DeletingDtor), MorallyVirtual,
347 OverrideOffset, Offset, CurrentVBaseOffset);
348 } else {
349 OverrideMethod(MD, WrapAddrOf(MD), MorallyVirtual, OverrideOffset,
350 Offset, CurrentVBaseOffset);
351 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000352 }
353 }
354 }
355
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000356 void AddMethod(const GlobalDecl GD, bool MorallyVirtual, Index_t Offset,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000357 int64_t CurrentVBaseOffset) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000358 llvm::Constant *m = WrapAddrOf(GD);
Mike Stump18e8b472009-10-27 23:36:26 +0000359
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000360 // If we can find a previously allocated slot for this, reuse it.
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000361 if (OverrideMethod(GD, m, MorallyVirtual, Offset, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000362 CurrentVBaseOffset))
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000363 return;
364
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000365 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
366
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000367 // else allocate a new slot.
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000368 Index[GD] = submethods.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000369 submethods.push_back(m);
Mike Stumpc5a332c2009-11-13 23:45:53 +0000370 D1(printf(" vfn for %s at %d\n", MD->getNameAsString().c_str(),
371 (int)Index[GD]));
Mike Stump375faa82009-10-28 00:35:46 +0000372 if (MD->isPure())
Anders Carlssond420a312009-11-26 19:32:45 +0000373 PureVirtualMethods.insert(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000374 if (MorallyVirtual) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000375 VCallOffset[GD] = Offset/8;
376 Index_t &idx = VCall[GD];
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000377 // Allocate the first one, after that, we reuse the previous one.
378 if (idx == 0) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000379 NonVirtualOffset[GD] = CurrentVBaseOffset/8 - Offset/8;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000380 idx = VCalls.size()+1;
381 VCalls.push_back(0);
Mike Stump75ce5732009-10-31 20:06:59 +0000382 D1(printf(" vcall for %s at %d with delta %d\n",
Mike Stumpc5a332c2009-11-13 23:45:53 +0000383 MD->getNameAsString().c_str(), (int)-VCalls.size()-3, 0));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000384 }
385 }
386 }
387
388 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000389 Index_t Offset, int64_t CurrentVBaseOffset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000390 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000391 ++mi) {
392 const CXXMethodDecl *MD = *mi;
393 if (!MD->isVirtual())
394 continue;
395
396 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
397 // For destructors, add both the complete and the deleting destructor
398 // to the vtable.
399 AddMethod(GlobalDecl(DD, Dtor_Complete), MorallyVirtual, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000400 CurrentVBaseOffset);
Eli Friedman03aa2f12009-11-30 01:19:33 +0000401 AddMethod(GlobalDecl(DD, Dtor_Deleting), MorallyVirtual, Offset,
402 CurrentVBaseOffset);
403 } else
404 AddMethod(MD, MorallyVirtual, Offset, CurrentVBaseOffset);
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000405 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000406 }
407
408 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
409 const CXXRecordDecl *PrimaryBase,
410 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000411 int64_t Offset, int64_t CurrentVBaseOffset,
412 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000413 Path->push_back(std::make_pair(RD, Offset));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000414 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
415 e = RD->bases_end(); i != e; ++i) {
416 if (i->isVirtual())
417 continue;
418 const CXXRecordDecl *Base =
419 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
420 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
421 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
422 StartNewTable();
Mike Stump653d0b92009-11-13 02:13:54 +0000423 GenerateVtableForBase(Base, o, MorallyVirtual, false,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000424 CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000425 }
426 }
Mike Stump37dbe962009-10-15 02:04:03 +0000427 Path->pop_back();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000428 }
429
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000430// #define D(X) do { X; } while (0)
431#define D(X)
432
433 void insertVCalls(int InsertionPoint) {
434 llvm::Constant *e = 0;
Mike Stump75ce5732009-10-31 20:06:59 +0000435 D1(printf("============= combining vbase/vcall\n"));
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000436 D(VCalls.insert(VCalls.begin(), 673));
437 D(VCalls.push_back(672));
Mike Stump8bccbfd2009-10-15 09:30:16 +0000438 methods.insert(methods.begin() + InsertionPoint, VCalls.size(), e);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000439 // The vcalls come first...
440 for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(),
441 e = VCalls.rend();
442 i != e; ++i)
443 methods[InsertionPoint++] = wrap((0?600:0) + *i);
444 VCalls.clear();
Mike Stump9f23a142009-11-10 02:30:51 +0000445 VCall.clear();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000446 }
447
Mike Stump559387f2009-11-13 23:13:20 +0000448 void AddAddressPoints(const CXXRecordDecl *RD, uint64_t Offset,
449 Index_t AddressPoint) {
450 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
451 RD->getNameAsCString(), Class->getNameAsCString(),
452 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stumpcd2b8212009-11-19 20:52:19 +0000453 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Mike Stump559387f2009-11-13 23:13:20 +0000454
455 // Now also add the address point for all our primary bases.
456 while (1) {
457 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
458 RD = Layout.getPrimaryBase();
459 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
460 // FIXME: Double check this.
461 if (RD == 0)
462 break;
463 if (PrimaryBaseWasVirtual &&
464 BLayout.getVBaseClassOffset(RD) != Offset)
465 break;
466 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
467 RD->getNameAsCString(), Class->getNameAsCString(),
468 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stumpcd2b8212009-11-19 20:52:19 +0000469 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Mike Stump559387f2009-11-13 23:13:20 +0000470 }
471 }
472
473
Mike Stump75ce5732009-10-31 20:06:59 +0000474 Index_t end(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
475 const CXXRecordDecl *PrimaryBase, bool PrimaryBaseWasVirtual,
476 bool MorallyVirtual, int64_t Offset, bool ForVirtualBase,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000477 int64_t CurrentVBaseOffset,
Mike Stump75ce5732009-10-31 20:06:59 +0000478 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000479 bool alloc = false;
480 if (Path == 0) {
481 alloc = true;
482 Path = new Path_t;
483 }
484
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000485 StartNewTable();
486 extra = 0;
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000487 bool DeferVCalls = MorallyVirtual || ForVirtualBase;
488 int VCallInsertionPoint = methods.size();
489 if (!DeferVCalls) {
490 insertVCalls(VCallInsertionPoint);
Mike Stump8bccbfd2009-10-15 09:30:16 +0000491 } else
492 // FIXME: just for extra, or for all uses of VCalls.size post this?
493 extra = -VCalls.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000494
Mike Stump653d0b92009-11-13 02:13:54 +0000495 methods.push_back(wrap(-((Offset-LayoutOffset)/8)));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000496 methods.push_back(rtti);
497 Index_t AddressPoint = methods.size();
498
499 InstallThunks();
Mike Stump75ce5732009-10-31 20:06:59 +0000500 D1(printf("============= combining methods\n"));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000501 methods.insert(methods.end(), submethods.begin(), submethods.end());
502 submethods.clear();
503
504 // and then the non-virtual bases.
505 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000506 MorallyVirtual, Offset, CurrentVBaseOffset, Path);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000507
508 if (ForVirtualBase) {
Mike Stump2cefe382009-11-12 20:47:57 +0000509 // FIXME: We're adding to VCalls in callers, we need to do the overrides
510 // in the inner part, so that we know the complete set of vcalls during
511 // the build and don't have to insert into methods. Saving out the
512 // AddressPoint here, would need to be fixed, if we didn't do that. Also
513 // retroactively adding vcalls for overrides later wind up in the wrong
514 // place, the vcall slot has to be alloted during the walk of the base
515 // when the function is first introduces.
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000516 AddressPoint += VCalls.size();
Mike Stump2cefe382009-11-12 20:47:57 +0000517 insertVCalls(VCallInsertionPoint);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000518 }
519
Mike Stump559387f2009-11-13 23:13:20 +0000520 AddAddressPoints(RD, Offset, AddressPoint);
Mike Stump2cefe382009-11-12 20:47:57 +0000521
Mike Stump37dbe962009-10-15 02:04:03 +0000522 if (alloc) {
523 delete Path;
524 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000525 return AddressPoint;
526 }
527
Mike Stump75ce5732009-10-31 20:06:59 +0000528 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
529 bool updateVBIndex, Index_t current_vbindex,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000530 int64_t CurrentVBaseOffset) {
Mike Stump75ce5732009-10-31 20:06:59 +0000531 if (!RD->isDynamicClass())
532 return;
533
534 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
535 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
536 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
537
538 // vtables are composed from the chain of primaries.
539 if (PrimaryBase) {
540 D1(printf(" doing primaries for %s most derived %s\n",
541 RD->getNameAsCString(), Class->getNameAsCString()));
542
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000543 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
544 if (PrimaryBaseWasVirtual)
545 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
546
Mike Stump75ce5732009-10-31 20:06:59 +0000547 if (!PrimaryBaseWasVirtual)
548 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000549 updateVBIndex, current_vbindex, BaseCurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000550 }
551
552 D1(printf(" doing vcall entries for %s most derived %s\n",
553 RD->getNameAsCString(), Class->getNameAsCString()));
554
555 // And add the virtuals for the class to the primary vtable.
Eli Friedman03aa2f12009-11-30 01:19:33 +0000556 AddMethods(RD, MorallyVirtual, Offset, CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000557 }
558
559 void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
560 bool updateVBIndex, Index_t current_vbindex,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000561 bool RDisVirtualBase, int64_t CurrentVBaseOffset,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000562 bool bottom) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000563 if (!RD->isDynamicClass())
564 return;
565
566 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
567 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
568 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
569
570 // vtables are composed from the chain of primaries.
571 if (PrimaryBase) {
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000572 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
573 if (PrimaryBaseWasVirtual) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000574 IndirectPrimary.insert(PrimaryBase);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000575 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
576 }
Mike Stump75ce5732009-10-31 20:06:59 +0000577
578 D1(printf(" doing primaries for %s most derived %s\n",
579 RD->getNameAsCString(), Class->getNameAsCString()));
580
581 VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000582 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000583 BaseCurrentVBaseOffset, false);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000584 }
585
Mike Stump75ce5732009-10-31 20:06:59 +0000586 D1(printf(" doing vbase entries for %s most derived %s\n",
587 RD->getNameAsCString(), Class->getNameAsCString()));
588 GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex);
589
590 if (RDisVirtualBase || bottom) {
591 Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000592 CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000593 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000594 }
595
Mike Stump653d0b92009-11-13 02:13:54 +0000596 int64_t GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0,
597 bool MorallyVirtual = false,
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000598 bool ForVirtualBase = false,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000599 int CurrentVBaseOffset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +0000600 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000601 if (!RD->isDynamicClass())
602 return 0;
603
Mike Stumpfa818082009-11-13 02:35:38 +0000604 // Construction vtable don't need parts that have no virtual bases and
605 // aren't morally virtual.
606 if ((LayoutClass != Class) && RD->getNumVBases() == 0 && !MorallyVirtual)
607 return 0;
608
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000609 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
610 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
611 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
612
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000613 extra = 0;
Mike Stump75ce5732009-10-31 20:06:59 +0000614 D1(printf("building entries for base %s most derived %s\n",
615 RD->getNameAsCString(), Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000616
Mike Stump75ce5732009-10-31 20:06:59 +0000617 if (ForVirtualBase)
618 extra = VCalls.size();
619
620 VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0, ForVirtualBase,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000621 CurrentVBaseOffset, true);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000622
623 if (Path)
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000624 OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000625
Mike Stump75ce5732009-10-31 20:06:59 +0000626 return end(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000627 Offset, ForVirtualBase, CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000628 }
629
630 void GenerateVtableForVBases(const CXXRecordDecl *RD,
631 int64_t Offset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +0000632 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000633 bool alloc = false;
634 if (Path == 0) {
635 alloc = true;
Mike Stump37dbe962009-10-15 02:04:03 +0000636 Path = new Path_t;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000637 }
638 // FIXME: We also need to override using all paths to a virtual base,
639 // right now, we just process the first path
640 Path->push_back(std::make_pair(RD, Offset));
641 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
642 e = RD->bases_end(); i != e; ++i) {
643 const CXXRecordDecl *Base =
644 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
645 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
646 // Mark it so we don't output it twice.
647 IndirectPrimary.insert(Base);
648 StartNewTable();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000649 VCall.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000650 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000651 int64_t CurrentVBaseOffset = BaseOffset;
Mike Stump75ce5732009-10-31 20:06:59 +0000652 D1(printf("vtable %s virtual base %s\n",
653 Class->getNameAsCString(), Base->getNameAsCString()));
Mike Stump653d0b92009-11-13 02:13:54 +0000654 GenerateVtableForBase(Base, BaseOffset, true, true, CurrentVBaseOffset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000655 Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000656 }
Mike Stump2cefe382009-11-12 20:47:57 +0000657 int64_t BaseOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000658 if (i->isVirtual())
659 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump2cefe382009-11-12 20:47:57 +0000660 else {
661 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
662 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
663 }
664
Mike Stump37dbe962009-10-15 02:04:03 +0000665 if (Base->getNumVBases()) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000666 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump37dbe962009-10-15 02:04:03 +0000667 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000668 }
669 Path->pop_back();
670 if (alloc)
671 delete Path;
672 }
673};
Anders Carlssonca1bf682009-12-03 01:54:02 +0000674} // end anonymous namespace
675
Anders Carlsson657f1392009-12-03 02:32:59 +0000676/// TypeConversionRequiresAdjustment - Returns whether conversion from a
677/// derived type to a base type requires adjustment.
678static bool
679TypeConversionRequiresAdjustment(ASTContext &Ctx,
680 const CXXRecordDecl *DerivedDecl,
681 const CXXRecordDecl *BaseDecl) {
682 CXXBasePaths Paths(/*FindAmbiguities=*/false,
683 /*RecordPaths=*/true, /*DetectVirtual=*/true);
684 if (!const_cast<CXXRecordDecl *>(DerivedDecl)->
685 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseDecl), Paths)) {
686 assert(false && "Class must be derived from the passed in base class!");
687 return false;
688 }
689
690 // If we found a virtual base we always want to require adjustment.
691 if (Paths.getDetectedVirtual())
692 return true;
693
694 const CXXBasePath &Path = Paths.front();
695
696 for (size_t Start = 0, End = Path.size(); Start != End; ++Start) {
697 const CXXBasePathElement &Element = Path[Start];
698
699 // Check the base class offset.
700 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Element.Class);
701
702 const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>();
703 const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl());
704
705 if (Layout.getBaseClassOffset(Base) != 0) {
706 // This requires an adjustment.
707 return true;
708 }
709 }
710
711 return false;
712}
713
714static bool
715TypeConversionRequiresAdjustment(ASTContext &Ctx,
716 QualType DerivedType, QualType BaseType) {
717 // Canonicalize the types.
718 QualType CanDerivedType = Ctx.getCanonicalType(DerivedType);
719 QualType CanBaseType = Ctx.getCanonicalType(BaseType);
720
721 assert(CanDerivedType->getTypeClass() == CanBaseType->getTypeClass() &&
722 "Types must have same type class!");
723
724 if (CanDerivedType == CanBaseType) {
725 // No adjustment needed.
726 return false;
727 }
728
729 if (const ReferenceType *RT = dyn_cast<ReferenceType>(CanDerivedType)) {
730 CanDerivedType = RT->getPointeeType();
731 CanBaseType = cast<ReferenceType>(CanBaseType)->getPointeeType();
732 } else if (const PointerType *PT = dyn_cast<PointerType>(CanDerivedType)) {
733 CanDerivedType = PT->getPointeeType();
734 CanBaseType = cast<PointerType>(CanBaseType)->getPointeeType();
735 } else {
736 assert(false && "Unexpected return type!");
737 }
738
739 if (CanDerivedType == CanBaseType) {
740 // No adjustment needed.
741 return false;
742 }
743
744 const CXXRecordDecl *DerivedDecl =
Anders Carlssondabfa3c2009-12-03 03:28:24 +0000745 cast<CXXRecordDecl>(cast<RecordType>(CanDerivedType)->getDecl());
Anders Carlsson657f1392009-12-03 02:32:59 +0000746
747 const CXXRecordDecl *BaseDecl =
748 cast<CXXRecordDecl>(cast<RecordType>(CanBaseType)->getDecl());
749
750 return TypeConversionRequiresAdjustment(Ctx, DerivedDecl, BaseDecl);
751}
752
Anders Carlssonca1bf682009-12-03 01:54:02 +0000753bool VtableBuilder::OverrideMethod(GlobalDecl GD, llvm::Constant *m,
754 bool MorallyVirtual, Index_t OverrideOffset,
755 Index_t Offset, int64_t CurrentVBaseOffset) {
756 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
757
758 const bool isPure = MD->isPure();
759 typedef CXXMethodDecl::method_iterator meth_iter;
760 // FIXME: Should OverrideOffset's be Offset?
761
762 // FIXME: Don't like the nested loops. For very large inheritance
763 // heirarchies we could have a table on the side with the final overridder
764 // and just replace each instance of an overridden method once. Would be
765 // nice to measure the cost/benefit on real code.
766
767 for (meth_iter mi = MD->begin_overridden_methods(),
768 e = MD->end_overridden_methods();
769 mi != e; ++mi) {
770 GlobalDecl OGD;
771
772 const CXXMethodDecl *OMD = *mi;
773 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(OMD))
774 OGD = GlobalDecl(DD, GD.getDtorType());
775 else
776 OGD = OMD;
777
778 llvm::Constant *om;
779 om = WrapAddrOf(OGD);
780 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty);
781
782 for (Index_t i = 0, e = submethods.size();
783 i != e; ++i) {
784 // FIXME: begin_overridden_methods might be too lax, covariance */
785 if (submethods[i] != om)
786 continue;
Anders Carlsson657f1392009-12-03 02:32:59 +0000787
788 QualType ReturnType =
789 MD->getType()->getAs<FunctionType>()->getResultType();
790 QualType OverriddenReturnType =
791 OMD->getType()->getAs<FunctionType>()->getResultType();
792
793 // Check if we need a return type adjustment.
794 if (TypeConversionRequiresAdjustment(CGM.getContext(), ReturnType,
795 OverriddenReturnType)) {
796 CovariantThunk &Adjustment = CovariantThunks[i];
Anders Carlsson2ca285f2009-12-03 02:22:59 +0000797
Anders Carlsson657f1392009-12-03 02:32:59 +0000798 // Get the canonical return type.
799 CanQualType CanReturnType =
800 CGM.getContext().getCanonicalType(ReturnType);
801
802 // Insert the base return type.
803 if (Adjustment.ReturnType.isNull())
804 Adjustment.ReturnType =
805 CGM.getContext().getCanonicalType(OverriddenReturnType);
806
807 Adjustment.GD = GD;
Anders Carlssonca1bf682009-12-03 01:54:02 +0000808 }
Anders Carlsson657f1392009-12-03 02:32:59 +0000809
Anders Carlssonca1bf682009-12-03 01:54:02 +0000810 Index[GD] = i;
811 submethods[i] = m;
812 if (isPure)
813 PureVirtualMethods.insert(GD);
814 PureVirtualMethods.erase(OGD);
Anders Carlsson29a1f752009-12-03 02:39:59 +0000815 Thunks.erase(i);
Anders Carlssonca1bf682009-12-03 01:54:02 +0000816 if (MorallyVirtual || VCall.count(OGD)) {
817 Index_t &idx = VCall[OGD];
818 if (idx == 0) {
819 NonVirtualOffset[GD] = -OverrideOffset/8 + CurrentVBaseOffset/8;
820 VCallOffset[GD] = OverrideOffset/8;
821 idx = VCalls.size()+1;
822 VCalls.push_back(0);
823 D1(printf(" vcall for %s at %d with delta %d most derived %s\n",
824 MD->getNameAsString().c_str(), (int)-idx-3,
825 (int)VCalls[idx-1], Class->getNameAsCString()));
826 } else {
827 NonVirtualOffset[GD] = NonVirtualOffset[OGD];
828 VCallOffset[GD] = VCallOffset[OGD];
829 VCalls[idx-1] = -VCallOffset[OGD] + OverrideOffset/8;
830 D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n",
831 MD->getNameAsString().c_str(), (int)-idx-3,
832 (int)VCalls[idx-1], Class->getNameAsCString()));
833 }
834 VCall[GD] = idx;
835 int64_t NonVirtualAdjustment = NonVirtualOffset[GD];
836 int64_t VirtualAdjustment =
837 -((idx + extra + 2) * LLVMPointerWidth / 8);
838
839 // Optimize out virtual adjustments of 0.
840 if (VCalls[idx-1] == 0)
841 VirtualAdjustment = 0;
842
843 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment,
844 VirtualAdjustment);
845
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000846 if (!isPure && !ThisAdjustment.isEmpty())
Anders Carlsson80bc5d52009-12-03 02:41:55 +0000847 Thunks[i] = Thunk(GD, ThisAdjustment);
Anders Carlssonca1bf682009-12-03 01:54:02 +0000848 return true;
849 }
850
851 // FIXME: finish off
852 int64_t NonVirtualAdjustment = VCallOffset[OGD] - OverrideOffset/8;
853
Anders Carlsson2ca285f2009-12-03 02:22:59 +0000854 if (NonVirtualAdjustment) {
Anders Carlssonca1bf682009-12-03 01:54:02 +0000855 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, 0);
856
Anders Carlsson2bd3c0f2009-12-03 01:58:20 +0000857 if (!isPure)
Anders Carlsson80bc5d52009-12-03 02:41:55 +0000858 Thunks[i] = Thunk(GD, ThisAdjustment);
Anders Carlssonca1bf682009-12-03 01:54:02 +0000859 }
860 return true;
861 }
862 }
863
864 return false;
Anders Carlssond59885022009-11-27 22:21:51 +0000865}
866
Anders Carlssonf942ee02009-11-27 20:47:55 +0000867void CGVtableInfo::ComputeMethodVtableIndices(const CXXRecordDecl *RD) {
868
869 // Itanium C++ ABI 2.5.2:
870 // The order of the virtual function pointers in a virtual table is the
871 // order of declaration of the corresponding member functions in the class.
872 //
873 // There is an entry for any virtual function declared in a class,
874 // whether it is a new function or overrides a base class function,
875 // unless it overrides a function from the primary base, and conversion
876 // between their return types does not require an adjustment.
877
878 int64_t CurrentIndex = 0;
879
880 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
881 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
882
883 if (PrimaryBase) {
Anders Carlssonc920fa22009-11-30 19:43:26 +0000884 assert(PrimaryBase->isDefinition() &&
885 "Should have the definition decl of the primary base!");
Anders Carlssonf942ee02009-11-27 20:47:55 +0000886
887 // Since the record decl shares its vtable pointer with the primary base
888 // we need to start counting at the end of the primary base's vtable.
889 CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase);
890 }
891
892 const CXXDestructorDecl *ImplicitVirtualDtor = 0;
893
894 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
895 e = RD->method_end(); i != e; ++i) {
896 const CXXMethodDecl *MD = *i;
897
898 // We only want virtual methods.
899 if (!MD->isVirtual())
900 continue;
901
902 bool ShouldAddEntryForMethod = true;
903
904 // Check if this method overrides a method in the primary base.
905 for (CXXMethodDecl::method_iterator i = MD->begin_overridden_methods(),
906 e = MD->end_overridden_methods(); i != e; ++i) {
907 const CXXMethodDecl *OverriddenMD = *i;
908 const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent();
909 assert(OverriddenMD->isCanonicalDecl() &&
910 "Should have the canonical decl of the overridden RD!");
911
912 if (OverriddenRD == PrimaryBase) {
913 // Check if converting from the return type of the method to the
914 // return type of the overridden method requires conversion.
915 QualType ReturnType =
916 MD->getType()->getAs<FunctionType>()->getResultType();
917 QualType OverriddenReturnType =
918 OverriddenMD->getType()->getAs<FunctionType>()->getResultType();
919
920 if (!TypeConversionRequiresAdjustment(CGM.getContext(),
921 ReturnType, OverriddenReturnType)) {
922 // This index is shared between the index in the vtable of the primary
923 // base class.
924 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
925 const CXXDestructorDecl *OverriddenDD =
926 cast<CXXDestructorDecl>(OverriddenMD);
927
928 // Add both the complete and deleting entries.
929 MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] =
930 getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Complete));
931 MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] =
932 getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting));
933 } else {
934 MethodVtableIndices[MD] = getMethodVtableIndex(OverriddenMD);
935 }
936
937 // We don't need to add an entry for this method.
938 ShouldAddEntryForMethod = false;
939 break;
940 }
941 }
942 }
943
944 if (!ShouldAddEntryForMethod)
945 continue;
946
947 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
948 if (MD->isImplicit()) {
949 assert(!ImplicitVirtualDtor &&
950 "Did already see an implicit virtual dtor!");
951 ImplicitVirtualDtor = DD;
952 continue;
953 }
954
955 // Add the complete dtor.
956 MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++;
957
958 // Add the deleting dtor.
959 MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++;
960 } else {
961 // Add the entry.
962 MethodVtableIndices[MD] = CurrentIndex++;
963 }
964 }
965
966 if (ImplicitVirtualDtor) {
967 // Itanium C++ ABI 2.5.2:
968 // If a class has an implicitly-defined virtual destructor,
969 // its entries come after the declared virtual function pointers.
970
971 // Add the complete dtor.
972 MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] =
973 CurrentIndex++;
974
975 // Add the deleting dtor.
976 MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] =
977 CurrentIndex++;
978 }
979
980 NumVirtualFunctionPointers[RD] = CurrentIndex;
981}
982
983uint64_t CGVtableInfo::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) {
984 llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
985 NumVirtualFunctionPointers.find(RD);
986 if (I != NumVirtualFunctionPointers.end())
987 return I->second;
988
989 ComputeMethodVtableIndices(RD);
990
991 I = NumVirtualFunctionPointers.find(RD);
992 assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!");
993 return I->second;
994}
995
996uint64_t CGVtableInfo::getMethodVtableIndex(GlobalDecl GD) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000997 MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000998 if (I != MethodVtableIndices.end())
999 return I->second;
1000
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001001 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
Anders Carlssonf942ee02009-11-27 20:47:55 +00001002
1003 ComputeMethodVtableIndices(RD);
1004
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001005 I = MethodVtableIndices.find(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001006 assert(I != MethodVtableIndices.end() && "Did not find index!");
1007 return I->second;
1008}
1009
1010int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
1011 const CXXRecordDecl *VBase) {
1012 ClassPairTy ClassPair(RD, VBase);
1013
1014 VirtualBaseClassIndiciesTy::iterator I =
1015 VirtualBaseClassIndicies.find(ClassPair);
1016 if (I != VirtualBaseClassIndicies.end())
1017 return I->second;
1018
1019 std::vector<llvm::Constant *> methods;
1020 // FIXME: This seems expensive. Can we do a partial job to get
1021 // just this data.
Mike Stump653d0b92009-11-13 02:13:54 +00001022 VtableBuilder b(methods, RD, RD, 0, CGM);
Mike Stump75ce5732009-10-31 20:06:59 +00001023 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001024 b.GenerateVtableForBase(RD);
1025 b.GenerateVtableForVBases(RD);
1026
1027 for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
1028 b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) {
1029 // Insert all types.
1030 ClassPairTy ClassPair(RD, I->first);
1031
1032 VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second));
1033 }
1034
1035 I = VirtualBaseClassIndicies.find(ClassPair);
1036 assert(I != VirtualBaseClassIndicies.end() && "Did not find index!");
1037
1038 return I->second;
1039}
1040
Mike Stump2cefe382009-11-12 20:47:57 +00001041llvm::Constant *CodeGenModule::GenerateVtable(const CXXRecordDecl *LayoutClass,
1042 const CXXRecordDecl *RD,
Mike Stumpeac45592009-11-11 20:26:26 +00001043 uint64_t Offset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001044 llvm::SmallString<256> OutName;
Mike Stump2cefe382009-11-12 20:47:57 +00001045 if (LayoutClass != RD)
Daniel Dunbare128dd12009-11-21 09:06:22 +00001046 getMangleContext().mangleCXXCtorVtable(LayoutClass, Offset/8, RD, OutName);
Mike Stumpeac45592009-11-11 20:26:26 +00001047 else
Daniel Dunbare128dd12009-11-21 09:06:22 +00001048 getMangleContext().mangleCXXVtable(RD, OutName);
1049 llvm::StringRef Name = OutName.str();
Benjamin Kramerbb0a07b2009-10-11 22:57:54 +00001050
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001051 std::vector<llvm::Constant *> methods;
1052 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1053 int64_t AddressPoint;
1054
Mike Stumpaa51ad62009-11-19 04:04:36 +00001055 llvm::GlobalVariable *GV = getModule().getGlobalVariable(Name);
Mike Stumpcd2b8212009-11-19 20:52:19 +00001056 if (GV && AddressPoints[LayoutClass] && !GV->isDeclaration()) {
Mike Stumpaa51ad62009-11-19 04:04:36 +00001057 AddressPoint=(*(*(AddressPoints[LayoutClass]))[RD])[std::make_pair(RD,
1058 Offset)];
Mike Stumpcd2b8212009-11-19 20:52:19 +00001059 // FIXME: We can never have 0 address point. Do this for now so gepping
1060 // retains the same structure. Later, we'll just assert.
1061 if (AddressPoint == 0)
1062 AddressPoint = 1;
1063 } else {
Mike Stumpaa51ad62009-11-19 04:04:36 +00001064 VtableBuilder b(methods, RD, LayoutClass, Offset, *this);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001065
Mike Stumpaa51ad62009-11-19 04:04:36 +00001066 D1(printf("vtable %s\n", RD->getNameAsCString()));
1067 // First comes the vtables for all the non-virtual bases...
1068 AddressPoint = b.GenerateVtableForBase(RD, Offset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001069
Mike Stumpaa51ad62009-11-19 04:04:36 +00001070 // then the vtables for all the virtual bases.
1071 b.GenerateVtableForVBases(RD, Offset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001072
Mike Stumpaa51ad62009-11-19 04:04:36 +00001073 bool CreateDefinition = true;
1074 if (LayoutClass != RD)
1075 CreateDefinition = true;
1076 else {
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001077 const ASTRecordLayout &Layout =
1078 getContext().getASTRecordLayout(LayoutClass);
1079
1080 if (const CXXMethodDecl *KeyFunction = Layout.getKeyFunction()) {
Mike Stumpaa51ad62009-11-19 04:04:36 +00001081 if (!KeyFunction->getBody()) {
1082 // If there is a KeyFunction, and it isn't defined, just build a
1083 // reference to the vtable.
1084 CreateDefinition = false;
1085 }
1086 }
1087 }
1088
1089 llvm::Constant *C = 0;
1090 llvm::Type *type = Ptr8Ty;
1091 llvm::GlobalVariable::LinkageTypes linktype
1092 = llvm::GlobalValue::ExternalLinkage;
1093 if (CreateDefinition) {
1094 llvm::ArrayType *ntype = llvm::ArrayType::get(Ptr8Ty, methods.size());
1095 C = llvm::ConstantArray::get(ntype, methods);
1096 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
1097 if (LayoutClass->isInAnonymousNamespace())
1098 linktype = llvm::GlobalValue::InternalLinkage;
1099 type = ntype;
1100 }
1101 llvm::GlobalVariable *OGV = GV;
1102 GV = new llvm::GlobalVariable(getModule(), type, true, linktype, C, Name);
1103 if (OGV) {
1104 GV->takeName(OGV);
1105 llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
1106 OGV->getType());
1107 OGV->replaceAllUsesWith(NewPtr);
1108 OGV->eraseFromParent();
1109 }
1110 bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden;
1111 if (Hidden)
1112 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
1113 }
Mike Stumpc0f632d2009-11-18 04:00:48 +00001114 llvm::Constant *vtable = llvm::ConstantExpr::getBitCast(GV, Ptr8Ty);
Mike Stumpd846d082009-11-10 07:44:33 +00001115 llvm::Constant *AddressPointC;
1116 uint32_t LLVMPointerWidth = getContext().Target.getPointerWidth(0);
1117 AddressPointC = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1118 AddressPoint*LLVMPointerWidth/8);
Mike Stump2cefe382009-11-12 20:47:57 +00001119 vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(vtable, &AddressPointC,
1120 1);
Mike Stumpd846d082009-11-10 07:44:33 +00001121
Mike Stumpcd2b8212009-11-19 20:52:19 +00001122 assert(vtable->getType() == Ptr8Ty);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001123 return vtable;
1124}
Mike Stump9f23a142009-11-10 02:30:51 +00001125
Mike Stumpae1b85d2009-12-02 19:07:44 +00001126namespace {
Mike Stump9f23a142009-11-10 02:30:51 +00001127class VTTBuilder {
1128 /// Inits - The list of values built for the VTT.
1129 std::vector<llvm::Constant *> &Inits;
1130 /// Class - The most derived class that this vtable is being built for.
1131 const CXXRecordDecl *Class;
1132 CodeGenModule &CGM; // Per-module state.
Mike Stump8b2d2d02009-11-11 00:35:07 +00001133 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
Mike Stumpc7b9f5e2009-11-11 03:08:24 +00001134 /// BLayout - Layout for the most derived class that this vtable is being
1135 /// built for.
1136 const ASTRecordLayout &BLayout;
Mike Stump83066c82009-11-13 01:54:23 +00001137 CodeGenModule::AddrMap_t &AddressPoints;
Mike Stump2cefe382009-11-12 20:47:57 +00001138 // vtbl - A pointer to the vtable for Class.
1139 llvm::Constant *ClassVtbl;
1140 llvm::LLVMContext &VMContext;
Mike Stump9f23a142009-11-10 02:30:51 +00001141
Mike Stump8677bc22009-11-12 22:56:32 +00001142 /// BuildVtablePtr - Build up a referene to the given secondary vtable
Mike Stump83066c82009-11-13 01:54:23 +00001143 llvm::Constant *BuildVtablePtr(llvm::Constant *vtbl,
1144 const CXXRecordDecl *VtblClass,
1145 const CXXRecordDecl *RD,
Mike Stump8677bc22009-11-12 22:56:32 +00001146 uint64_t Offset) {
1147 int64_t AddressPoint;
Mike Stump83066c82009-11-13 01:54:23 +00001148 AddressPoint = (*AddressPoints[VtblClass])[std::make_pair(RD, Offset)];
Mike Stump2b34bc52009-11-12 23:36:21 +00001149 // FIXME: We can never have 0 address point. Do this for now so gepping
Mike Stumpcd2b8212009-11-19 20:52:19 +00001150 // retains the same structure. Later we'll just assert.
Mike Stump2b34bc52009-11-12 23:36:21 +00001151 if (AddressPoint == 0)
1152 AddressPoint = 1;
Mike Stump83066c82009-11-13 01:54:23 +00001153 D1(printf("XXX address point for %s in %s layout %s at offset %d was %d\n",
1154 RD->getNameAsCString(), VtblClass->getNameAsCString(),
1155 Class->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stump8677bc22009-11-12 22:56:32 +00001156 uint32_t LLVMPointerWidth = CGM.getContext().Target.getPointerWidth(0);
1157 llvm::Constant *init;
1158 init = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1159 AddressPoint*LLVMPointerWidth/8);
1160 init = llvm::ConstantExpr::getInBoundsGetElementPtr(vtbl, &init, 1);
1161 return init;
1162 }
1163
Mike Stump2cefe382009-11-12 20:47:57 +00001164 /// Secondary - Add the secondary vtable pointers to Inits. Offset is the
1165 /// current offset in bits to the object we're working on.
Mike Stump8677bc22009-11-12 22:56:32 +00001166 void Secondary(const CXXRecordDecl *RD, llvm::Constant *vtbl,
Mike Stump83066c82009-11-13 01:54:23 +00001167 const CXXRecordDecl *VtblClass, uint64_t Offset=0,
1168 bool MorallyVirtual=false) {
Mike Stump8b2d2d02009-11-11 00:35:07 +00001169 if (RD->getNumVBases() == 0 && ! MorallyVirtual)
1170 return;
1171
1172 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1173 e = RD->bases_end(); i != e; ++i) {
1174 const CXXRecordDecl *Base =
1175 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1176 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1177 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1178 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1179 bool NonVirtualPrimaryBase;
1180 NonVirtualPrimaryBase = !PrimaryBaseWasVirtual && Base == PrimaryBase;
1181 bool BaseMorallyVirtual = MorallyVirtual | i->isVirtual();
Mike Stumpc7b9f5e2009-11-11 03:08:24 +00001182 uint64_t BaseOffset;
1183 if (!i->isVirtual()) {
1184 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1185 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
1186 } else
1187 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump2b34bc52009-11-12 23:36:21 +00001188 llvm::Constant *subvtbl = vtbl;
Mike Stump83066c82009-11-13 01:54:23 +00001189 const CXXRecordDecl *subVtblClass = VtblClass;
Mike Stump8b2d2d02009-11-11 00:35:07 +00001190 if ((Base->getNumVBases() || BaseMorallyVirtual)
1191 && !NonVirtualPrimaryBase) {
1192 // FIXME: Slightly too many of these for __ZTT8test8_B2
Mike Stump8677bc22009-11-12 22:56:32 +00001193 llvm::Constant *init;
Mike Stump2b34bc52009-11-12 23:36:21 +00001194 if (BaseMorallyVirtual)
Mike Stump83066c82009-11-13 01:54:23 +00001195 init = BuildVtablePtr(vtbl, VtblClass, RD, Offset);
Mike Stump2b34bc52009-11-12 23:36:21 +00001196 else {
Mike Stump8677bc22009-11-12 22:56:32 +00001197 init = CGM.getVtableInfo().getCtorVtable(Class, Base, BaseOffset);
Mike Stump2b34bc52009-11-12 23:36:21 +00001198 subvtbl = dyn_cast<llvm::Constant>(init->getOperand(0));
Mike Stump83066c82009-11-13 01:54:23 +00001199 subVtblClass = Base;
Mike Stump2b34bc52009-11-12 23:36:21 +00001200 }
Mike Stump8677bc22009-11-12 22:56:32 +00001201 Inits.push_back(init);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001202 }
Mike Stump83066c82009-11-13 01:54:23 +00001203 Secondary(Base, subvtbl, subVtblClass, BaseOffset, BaseMorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001204 }
1205 }
1206
Mike Stump2cefe382009-11-12 20:47:57 +00001207 /// BuiltVTT - Add the VTT to Inits. Offset is the offset in bits to the
1208 /// currnet object we're working on.
1209 void BuildVTT(const CXXRecordDecl *RD, uint64_t Offset, bool MorallyVirtual) {
Mike Stump8b2d2d02009-11-11 00:35:07 +00001210 if (RD->getNumVBases() == 0 && !MorallyVirtual)
1211 return;
1212
Mike Stump2cefe382009-11-12 20:47:57 +00001213 llvm::Constant *init;
Mike Stump83066c82009-11-13 01:54:23 +00001214 const CXXRecordDecl *VtblClass;
1215
Mike Stump8b2d2d02009-11-11 00:35:07 +00001216 // First comes the primary virtual table pointer...
Mike Stump83066c82009-11-13 01:54:23 +00001217 if (MorallyVirtual) {
1218 init = BuildVtablePtr(ClassVtbl, Class, RD, Offset);
1219 VtblClass = Class;
1220 } else {
Mike Stump2cefe382009-11-12 20:47:57 +00001221 init = CGM.getVtableInfo().getCtorVtable(Class, RD, Offset);
Mike Stump83066c82009-11-13 01:54:23 +00001222 VtblClass = RD;
1223 }
Mike Stump8677bc22009-11-12 22:56:32 +00001224 llvm::Constant *vtbl = dyn_cast<llvm::Constant>(init->getOperand(0));
Mike Stump2cefe382009-11-12 20:47:57 +00001225 Inits.push_back(init);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001226
1227 // then the secondary VTTs....
Mike Stump2cefe382009-11-12 20:47:57 +00001228 SecondaryVTTs(RD, Offset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001229
1230 // and last the secondary vtable pointers.
Mike Stump83066c82009-11-13 01:54:23 +00001231 Secondary(RD, vtbl, VtblClass, Offset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001232 }
1233
1234 /// SecondaryVTTs - Add the secondary VTTs to Inits. The secondary VTTs are
1235 /// built from each direct non-virtual proper base that requires a VTT in
1236 /// declaration order.
Mike Stump2cefe382009-11-12 20:47:57 +00001237 void SecondaryVTTs(const CXXRecordDecl *RD, uint64_t Offset=0,
1238 bool MorallyVirtual=false) {
Mike Stump8b2d2d02009-11-11 00:35:07 +00001239 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1240 e = RD->bases_end(); i != e; ++i) {
1241 const CXXRecordDecl *Base =
1242 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1243 if (i->isVirtual())
1244 continue;
Mike Stump2cefe382009-11-12 20:47:57 +00001245 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1246 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base);
1247 BuildVTT(Base, BaseOffset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001248 }
1249 }
1250
1251 /// VirtualVTTs - Add the VTT for each proper virtual base in inheritance
1252 /// graph preorder.
1253 void VirtualVTTs(const CXXRecordDecl *RD) {
1254 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1255 e = RD->bases_end(); i != e; ++i) {
1256 const CXXRecordDecl *Base =
1257 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1258 if (i->isVirtual() && !SeenVBase.count(Base)) {
1259 SeenVBase.insert(Base);
Mike Stump2cefe382009-11-12 20:47:57 +00001260 uint64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
1261 BuildVTT(Base, BaseOffset, true);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001262 }
1263 VirtualVTTs(Base);
1264 }
1265 }
Mike Stump9f23a142009-11-10 02:30:51 +00001266public:
1267 VTTBuilder(std::vector<llvm::Constant *> &inits, const CXXRecordDecl *c,
Mike Stumpc7b9f5e2009-11-11 03:08:24 +00001268 CodeGenModule &cgm)
1269 : Inits(inits), Class(c), CGM(cgm),
Mike Stump2cefe382009-11-12 20:47:57 +00001270 BLayout(cgm.getContext().getASTRecordLayout(c)),
Mike Stump83066c82009-11-13 01:54:23 +00001271 AddressPoints(*cgm.AddressPoints[c]),
Mike Stump2cefe382009-11-12 20:47:57 +00001272 VMContext(cgm.getModule().getContext()) {
Mike Stumpd846d082009-11-10 07:44:33 +00001273
Mike Stump8b2d2d02009-11-11 00:35:07 +00001274 // First comes the primary virtual table pointer for the complete class...
Mike Stump2cefe382009-11-12 20:47:57 +00001275 ClassVtbl = CGM.getVtableInfo().getVtable(Class);
1276 Inits.push_back(ClassVtbl);
1277 ClassVtbl = dyn_cast<llvm::Constant>(ClassVtbl->getOperand(0));
1278
Mike Stump8b2d2d02009-11-11 00:35:07 +00001279 // then the secondary VTTs...
1280 SecondaryVTTs(Class);
1281
1282 // then the secondary vtable pointers...
Mike Stump83066c82009-11-13 01:54:23 +00001283 Secondary(Class, ClassVtbl, Class);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001284
1285 // and last, the virtual VTTs.
1286 VirtualVTTs(Class);
Mike Stump9f23a142009-11-10 02:30:51 +00001287 }
1288};
Mike Stumpae1b85d2009-12-02 19:07:44 +00001289}
Mike Stump9f23a142009-11-10 02:30:51 +00001290
Mike Stumpd846d082009-11-10 07:44:33 +00001291llvm::Constant *CodeGenModule::GenerateVTT(const CXXRecordDecl *RD) {
Mike Stumpb4722212009-11-10 19:13:04 +00001292 // Only classes that have virtual bases need a VTT.
1293 if (RD->getNumVBases() == 0)
1294 return 0;
1295
Mike Stump9f23a142009-11-10 02:30:51 +00001296 llvm::SmallString<256> OutName;
Daniel Dunbare128dd12009-11-21 09:06:22 +00001297 getMangleContext().mangleCXXVTT(RD, OutName);
1298 llvm::StringRef Name = OutName.str();
Mike Stump9f23a142009-11-10 02:30:51 +00001299
1300 llvm::GlobalVariable::LinkageTypes linktype;
1301 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
Mike Stumpaa51ad62009-11-19 04:04:36 +00001302 if (RD->isInAnonymousNamespace())
1303 linktype = llvm::GlobalValue::InternalLinkage;
Mike Stump9f23a142009-11-10 02:30:51 +00001304 std::vector<llvm::Constant *> inits;
1305 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1306
Mike Stump9f23a142009-11-10 02:30:51 +00001307 D1(printf("vtt %s\n", RD->getNameAsCString()));
1308
Mike Stump8b2d2d02009-11-11 00:35:07 +00001309 VTTBuilder b(inits, RD, *this);
1310
Mike Stump9f23a142009-11-10 02:30:51 +00001311 llvm::Constant *C;
1312 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, inits.size());
1313 C = llvm::ConstantArray::get(type, inits);
Mike Stumpc0f632d2009-11-18 04:00:48 +00001314 llvm::GlobalVariable *vtt = new llvm::GlobalVariable(getModule(), type, true,
Mike Stumpaa51ad62009-11-19 04:04:36 +00001315 linktype, C, Name);
Mike Stumpc0f632d2009-11-18 04:00:48 +00001316 bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden;
1317 if (Hidden)
1318 vtt->setVisibility(llvm::GlobalVariable::HiddenVisibility);
1319 return llvm::ConstantExpr::getBitCast(vtt, Ptr8Ty);
Mike Stump9f23a142009-11-10 02:30:51 +00001320}
Mike Stumpd846d082009-11-10 07:44:33 +00001321
Mike Stump1a139f82009-11-19 01:08:19 +00001322void CGVtableInfo::GenerateClassData(const CXXRecordDecl *RD) {
1323 Vtables[RD] = CGM.GenerateVtable(RD, RD);
Mike Stumpc01c2b82009-12-02 18:57:08 +00001324 CGM.GenerateRTTI(RD);
Mike Stump1a139f82009-11-19 01:08:19 +00001325 CGM.GenerateVTT(RD);
1326}
1327
Mike Stumpeac45592009-11-11 20:26:26 +00001328llvm::Constant *CGVtableInfo::getVtable(const CXXRecordDecl *RD) {
Mike Stumpd846d082009-11-10 07:44:33 +00001329 llvm::Constant *&vtbl = Vtables[RD];
1330 if (vtbl)
1331 return vtbl;
Mike Stump2cefe382009-11-12 20:47:57 +00001332 vtbl = CGM.GenerateVtable(RD, RD);
Mike Stumpaa51ad62009-11-19 04:04:36 +00001333
1334 bool CreateDefinition = true;
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001335
1336 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1337 if (const CXXMethodDecl *KeyFunction = Layout.getKeyFunction()) {
Mike Stumpaa51ad62009-11-19 04:04:36 +00001338 if (!KeyFunction->getBody()) {
1339 // If there is a KeyFunction, and it isn't defined, just build a
1340 // reference to the vtable.
1341 CreateDefinition = false;
1342 }
1343 }
1344
1345 if (CreateDefinition) {
Mike Stumpc01c2b82009-12-02 18:57:08 +00001346 CGM.GenerateRTTI(RD);
Mike Stumpaa51ad62009-11-19 04:04:36 +00001347 CGM.GenerateVTT(RD);
1348 }
Mike Stumpd846d082009-11-10 07:44:33 +00001349 return vtbl;
1350}
Mike Stumpeac45592009-11-11 20:26:26 +00001351
Mike Stump2cefe382009-11-12 20:47:57 +00001352llvm::Constant *CGVtableInfo::getCtorVtable(const CXXRecordDecl *LayoutClass,
1353 const CXXRecordDecl *RD,
Mike Stumpeac45592009-11-11 20:26:26 +00001354 uint64_t Offset) {
Mike Stump2cefe382009-11-12 20:47:57 +00001355 return CGM.GenerateVtable(LayoutClass, RD, Offset);
Mike Stumpeac45592009-11-11 20:26:26 +00001356}
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001357
1358void CGVtableInfo::MaybeEmitVtable(GlobalDecl GD) {
1359 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1360 const CXXRecordDecl *RD = MD->getParent();
1361
1362 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1363
1364 // Get the key function.
1365 const CXXMethodDecl *KeyFunction = Layout.getKeyFunction();
1366
1367 if (!KeyFunction) {
1368 // If there's no key function, we don't want to emit the vtable here.
1369 return;
1370 }
1371
1372 // Check if we have the key function.
1373 if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl())
1374 return;
1375
1376 // If the key function is a destructor, we only want to emit the vtable once,
1377 // so do it for the complete destructor.
1378 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Complete)
1379 return;
1380
1381 // Emit the data.
1382 GenerateClassData(RD);
1383}
1384