blob: 6518f52e0c474c590de818b19a5492f395c3eff4 [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:
Anders Carlsson472404f2009-12-04 16:19:30 +000030
31 // Vtable - The components of the vtable being built.
Anders Carlsson19462d62009-12-04 16:24:46 +000032 typedef llvm::SmallVector<llvm::Constant *, 64> VtableVectorTy;
33 VtableVectorTy Vtable;
Anders Carlsson472404f2009-12-04 16:19:30 +000034
Anders Carlsson2bb27f52009-10-11 22:13:54 +000035 llvm::Type *Ptr8Ty;
36 /// Class - The most derived class that this vtable is being built for.
37 const CXXRecordDecl *Class;
Mike Stump83066c82009-11-13 01:54:23 +000038 /// LayoutClass - The most derived class used for virtual base layout
39 /// information.
40 const CXXRecordDecl *LayoutClass;
Mike Stump653d0b92009-11-13 02:13:54 +000041 /// LayoutOffset - The offset for Class in LayoutClass.
42 uint64_t LayoutOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000043 /// BLayout - Layout for the most derived class that this vtable is being
44 /// built for.
45 const ASTRecordLayout &BLayout;
46 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
47 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
48 llvm::Constant *rtti;
49 llvm::LLVMContext &VMContext;
50 CodeGenModule &CGM; // Per-module state.
Anders Carlssonf2f31f42009-12-04 03:46:21 +000051
Anders Carlssonfb4dda42009-11-13 17:08:56 +000052 llvm::DenseMap<GlobalDecl, Index_t> VCall;
53 llvm::DenseMap<GlobalDecl, Index_t> VCallOffset;
Mike Stumpcd6f9ed2009-11-06 23:27:42 +000054 // This is the offset to the nearest virtual base
Anders Carlssonfb4dda42009-11-13 17:08:56 +000055 llvm::DenseMap<GlobalDecl, Index_t> NonVirtualOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000056 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stumpbb9ff052009-10-27 23:46:47 +000057
Anders Carlsson323bb042009-11-26 19:54:33 +000058 /// PureVirtualFunction - Points to __cxa_pure_virtual.
59 llvm::Constant *PureVirtualFn;
60
Anders Carlssona84b6e82009-12-04 02:01:07 +000061 /// VtableMethods - A data structure for keeping track of methods in a vtable.
62 /// Can add methods, override methods and iterate in vtable order.
63 class VtableMethods {
64 // MethodToIndexMap - Maps from a global decl to the index it has in the
65 // Methods vector.
66 llvm::DenseMap<GlobalDecl, uint64_t> MethodToIndexMap;
67
68 /// Methods - The methods, in vtable order.
69 typedef llvm::SmallVector<GlobalDecl, 16> MethodsVectorTy;
70 MethodsVectorTy Methods;
71
72 public:
73 /// AddMethod - Add a method to the vtable methods.
74 void AddMethod(GlobalDecl GD) {
75 assert(!MethodToIndexMap.count(GD) &&
76 "Method has already been added!");
77
78 MethodToIndexMap[GD] = Methods.size();
79 Methods.push_back(GD);
80 }
81
82 /// OverrideMethod - Replace a method with another.
83 void OverrideMethod(GlobalDecl OverriddenGD, GlobalDecl GD) {
84 llvm::DenseMap<GlobalDecl, uint64_t>::iterator i
85 = MethodToIndexMap.find(OverriddenGD);
86 assert(i != MethodToIndexMap.end() && "Did not find entry!");
87
88 // Get the index of the old decl.
89 uint64_t Index = i->second;
90
91 // Replace the old decl with the new decl.
92 Methods[Index] = GD;
93
Anders Carlssona84b6e82009-12-04 02:01:07 +000094 // And add the new.
95 MethodToIndexMap[GD] = Index;
96 }
97
Anders Carlsson7bb70762009-12-04 15:49:02 +000098 /// getIndex - Gives the index of a passed in GlobalDecl. Returns false if
99 /// the index couldn't be found.
100 uint64_t getIndex(GlobalDecl GD, uint64_t &Index) const {
101 llvm::DenseMap<GlobalDecl, uint64_t>::const_iterator i
102 = MethodToIndexMap.find(GD);
Eli Friedman3d2e9de2009-12-04 08:34:14 +0000103
Anders Carlsson7bb70762009-12-04 15:49:02 +0000104 if (i == MethodToIndexMap.end())
105 return false;
Anders Carlssone6096362009-12-04 03:41:37 +0000106
Anders Carlsson7bb70762009-12-04 15:49:02 +0000107 Index = i->second;
108 return true;
Anders Carlssone6096362009-12-04 03:41:37 +0000109 }
110
Anders Carlssona84b6e82009-12-04 02:01:07 +0000111 MethodsVectorTy::size_type size() const {
112 return Methods.size();
113 }
114
115 void clear() {
116 MethodToIndexMap.clear();
117 Methods.clear();
118 }
119
Anders Carlssone6096362009-12-04 03:41:37 +0000120 GlobalDecl operator[](uint64_t Index) const {
Anders Carlssona84b6e82009-12-04 02:01:07 +0000121 return Methods[Index];
122 }
123 };
124
125 /// Methods - The vtable methods we're currently building.
126 VtableMethods Methods;
127
Anders Carlsson4c837d22009-12-04 02:26:15 +0000128 /// ThisAdjustments - For a given index in the vtable, contains the 'this'
129 /// pointer adjustment needed for a method.
130 typedef llvm::DenseMap<uint64_t, ThunkAdjustment> ThisAdjustmentsMapTy;
131 ThisAdjustmentsMapTy ThisAdjustments;
Anders Carlssond420a312009-11-26 19:32:45 +0000132
Anders Carlssonc521f952009-12-04 02:22:02 +0000133 /// BaseReturnTypes - Contains the base return types of methods who have been
134 /// overridden with methods whose return types require adjustment. Used for
135 /// generating covariant thunk information.
136 typedef llvm::DenseMap<uint64_t, CanQualType> BaseReturnTypesMapTy;
137 BaseReturnTypesMapTy BaseReturnTypes;
Anders Carlssond420a312009-11-26 19:32:45 +0000138
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000139 std::vector<Index_t> VCalls;
Mike Stump2cefe382009-11-12 20:47:57 +0000140
141 typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t;
Mike Stumpcd2b8212009-11-19 20:52:19 +0000142 // subAddressPoints - Used to hold the AddressPoints (offsets) into the built
143 // vtable for use in computing the initializers for the VTT.
144 llvm::DenseMap<CtorVtable_t, int64_t> &subAddressPoints;
Mike Stump2cefe382009-11-12 20:47:57 +0000145
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000146 typedef CXXRecordDecl::method_iterator method_iter;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000147 const bool Extern;
148 const uint32_t LLVMPointerWidth;
149 Index_t extra;
Mike Stump37dbe962009-10-15 02:04:03 +0000150 typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t;
Mike Stumpcd2b8212009-11-19 20:52:19 +0000151 static llvm::DenseMap<CtorVtable_t, int64_t>&
152 AllocAddressPoint(CodeGenModule &cgm, const CXXRecordDecl *l,
153 const CXXRecordDecl *c) {
154 CodeGenModule::AddrMap_t *&oref = cgm.AddressPoints[l];
155 if (oref == 0)
156 oref = new CodeGenModule::AddrMap_t;
157
158 llvm::DenseMap<CtorVtable_t, int64_t> *&ref = (*oref)[c];
159 if (ref == 0)
160 ref = new llvm::DenseMap<CtorVtable_t, int64_t>;
161 return *ref;
162 }
Anders Carlsson323bb042009-11-26 19:54:33 +0000163
164 /// getPureVirtualFn - Return the __cxa_pure_virtual function.
165 llvm::Constant* getPureVirtualFn() {
166 if (!PureVirtualFn) {
167 const llvm::FunctionType *Ty =
168 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
169 /*isVarArg=*/false);
170 PureVirtualFn = wrap(CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual"));
171 }
172
173 return PureVirtualFn;
174 }
175
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000176public:
Anders Carlsson472404f2009-12-04 16:19:30 +0000177 VtableBuilder(const CXXRecordDecl *c,
Mike Stump653d0b92009-11-13 02:13:54 +0000178 const CXXRecordDecl *l, uint64_t lo, CodeGenModule &cgm)
Anders Carlsson472404f2009-12-04 16:19:30 +0000179 : Class(c), LayoutClass(l), LayoutOffset(lo),
Mike Stump83066c82009-11-13 01:54:23 +0000180 BLayout(cgm.getContext().getASTRecordLayout(l)),
Mike Stumpc01c2b82009-12-02 18:57:08 +0000181 rtti(cgm.GenerateRTTIRef(c)), VMContext(cgm.getModule().getContext()),
Anders Carlsson323bb042009-11-26 19:54:33 +0000182 CGM(cgm), PureVirtualFn(0),subAddressPoints(AllocAddressPoint(cgm, l, c)),
Mike Stump1960b202009-11-19 00:49:05 +0000183 Extern(!l->isInAnonymousNamespace()),
Anders Carlsson323bb042009-11-26 19:54:33 +0000184 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000185 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
186 }
187
Anders Carlsson472404f2009-12-04 16:19:30 +0000188 // getVtable - Returns a reference to the vtable components.
Anders Carlsson19462d62009-12-04 16:24:46 +0000189 const VtableVectorTy &getVtable() const {
Anders Carlsson472404f2009-12-04 16:19:30 +0000190 return Vtable;
191 }
192
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000193 llvm::DenseMap<const CXXRecordDecl *, Index_t> &getVBIndex()
194 { return VBIndex; }
195
196 llvm::Constant *wrap(Index_t i) {
197 llvm::Constant *m;
198 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
199 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
200 }
201
202 llvm::Constant *wrap(llvm::Constant *m) {
203 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
204 }
205
Mike Stump8a96d3a2009-12-02 19:50:41 +0000206#define D1(x)
207//#define D1(X) do { if (getenv("DEBUG")) { X; } } while (0)
Mike Stump75ce5732009-10-31 20:06:59 +0000208
209 void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset,
Mike Stump28431212009-10-13 22:54:56 +0000210 bool updateVBIndex, Index_t current_vbindex) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000211 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
212 e = RD->bases_end(); i != e; ++i) {
213 const CXXRecordDecl *Base =
214 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump28431212009-10-13 22:54:56 +0000215 Index_t next_vbindex = current_vbindex;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000216 if (i->isVirtual() && !SeenVBase.count(Base)) {
217 SeenVBase.insert(Base);
Mike Stump28431212009-10-13 22:54:56 +0000218 if (updateVBIndex) {
Mike Stump75ce5732009-10-31 20:06:59 +0000219 next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8)
Mike Stump28431212009-10-13 22:54:56 +0000220 - 3*LLVMPointerWidth/8);
221 VBIndex[Base] = next_vbindex;
222 }
Mike Stump75ce5732009-10-31 20:06:59 +0000223 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
224 VCalls.push_back((0?700:0) + BaseOffset);
225 D1(printf(" vbase for %s at %d delta %d most derived %s\n",
226 Base->getNameAsCString(),
227 (int)-VCalls.size()-3, (int)BaseOffset,
228 Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000229 }
Mike Stump28431212009-10-13 22:54:56 +0000230 // We also record offsets for non-virtual bases to closest enclosing
231 // virtual base. We do this so that we don't have to search
232 // for the nearst virtual base class when generating thunks.
233 if (updateVBIndex && VBIndex.count(Base) == 0)
234 VBIndex[Base] = next_vbindex;
Mike Stump75ce5732009-10-31 20:06:59 +0000235 GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000236 }
237 }
238
239 void StartNewTable() {
240 SeenVBase.clear();
241 }
242
Mike Stump8bccbfd2009-10-15 09:30:16 +0000243 Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B,
244 Index_t Offset = 0) {
245
246 if (B == D)
247 return Offset;
248
249 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D);
250 for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(),
251 e = D->bases_end(); i != e; ++i) {
252 const CXXRecordDecl *Base =
253 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
254 int64_t BaseOffset = 0;
255 if (!i->isVirtual())
256 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
257 int64_t o = getNVOffset_1(Base, B, BaseOffset);
258 if (o >= 0)
259 return o;
260 }
261
262 return -1;
263 }
264
265 /// getNVOffset - Returns the non-virtual offset for the given (B) base of the
266 /// derived class D.
267 Index_t getNVOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000268 qD = qD->getPointeeType();
269 qB = qB->getPointeeType();
Mike Stump8bccbfd2009-10-15 09:30:16 +0000270 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
271 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
272 int64_t o = getNVOffset_1(D, B);
273 if (o >= 0)
274 return o;
275
276 assert(false && "FIXME: non-virtual base not found");
277 return 0;
278 }
279
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000280 /// getVbaseOffset - Returns the index into the vtable for the virtual base
281 /// offset for the given (B) virtual base of the derived class D.
282 Index_t getVbaseOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000283 qD = qD->getPointeeType();
284 qB = qB->getPointeeType();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000285 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
286 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
287 if (D != Class)
Eli Friedman03aa2f12009-11-30 01:19:33 +0000288 return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000289 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
290 i = VBIndex.find(B);
291 if (i != VBIndex.end())
292 return i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000293
Mike Stump28431212009-10-13 22:54:56 +0000294 assert(false && "FIXME: Base not found");
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000295 return 0;
296 }
297
Eli Friedman81fb0d22009-12-04 08:40:51 +0000298 bool OverrideMethod(GlobalDecl GD, bool MorallyVirtual,
299 Index_t OverrideOffset, Index_t Offset,
300 int64_t CurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000301
Anders Carlsson495634e2009-12-04 02:39:04 +0000302 /// AppendMethods - Append the current methods to the vtable.
Anders Carlssonddf42c82009-12-04 02:56:03 +0000303 void AppendMethodsToVtable();
Anders Carlsson495634e2009-12-04 02:39:04 +0000304
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000305 llvm::Constant *WrapAddrOf(GlobalDecl GD) {
306 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
307
Anders Carlsson64457732009-11-24 05:08:52 +0000308 const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD);
Mike Stump18e8b472009-10-27 23:36:26 +0000309
Mike Stumpcdeb8002009-12-03 16:55:20 +0000310 return wrap(CGM.GetAddrOfFunction(GD, Ty));
Mike Stump18e8b472009-10-27 23:36:26 +0000311 }
312
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000313 void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset,
314 int64_t CurrentVBaseOffset) {
Mike Stump37dbe962009-10-15 02:04:03 +0000315 for (Path_t::reverse_iterator i = Path->rbegin(),
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000316 e = Path->rend(); i != e; ++i) {
317 const CXXRecordDecl *RD = i->first;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000318 int64_t OverrideOffset = i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000319 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
320 ++mi) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000321 const CXXMethodDecl *MD = *mi;
322
323 if (!MD->isVirtual())
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000324 continue;
325
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000326 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
327 // Override both the complete and the deleting destructor.
328 GlobalDecl CompDtor(DD, Dtor_Complete);
Eli Friedman81fb0d22009-12-04 08:40:51 +0000329 OverrideMethod(CompDtor, MorallyVirtual, OverrideOffset, Offset,
330 CurrentVBaseOffset);
331
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000332 GlobalDecl DeletingDtor(DD, Dtor_Deleting);
Eli Friedman81fb0d22009-12-04 08:40:51 +0000333 OverrideMethod(DeletingDtor, MorallyVirtual, OverrideOffset, Offset,
334 CurrentVBaseOffset);
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000335 } else {
Eli Friedman81fb0d22009-12-04 08:40:51 +0000336 OverrideMethod(MD, MorallyVirtual, OverrideOffset, Offset,
337 CurrentVBaseOffset);
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000338 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000339 }
340 }
341 }
342
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000343 void AddMethod(const GlobalDecl GD, bool MorallyVirtual, Index_t Offset,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000344 int64_t CurrentVBaseOffset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000345 // If we can find a previously allocated slot for this, reuse it.
Eli Friedman81fb0d22009-12-04 08:40:51 +0000346 if (OverrideMethod(GD, MorallyVirtual, Offset, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000347 CurrentVBaseOffset))
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000348 return;
349
Anders Carlssoncdf18982009-12-04 02:08:24 +0000350 // We didn't find an entry in the vtable that we could use, add a new
351 // entry.
352 Methods.AddMethod(GD);
353
Mike Stumpc5a332c2009-11-13 23:45:53 +0000354 D1(printf(" vfn for %s at %d\n", MD->getNameAsString().c_str(),
355 (int)Index[GD]));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000356 if (MorallyVirtual) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000357 VCallOffset[GD] = Offset/8;
358 Index_t &idx = VCall[GD];
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000359 // Allocate the first one, after that, we reuse the previous one.
360 if (idx == 0) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000361 NonVirtualOffset[GD] = CurrentVBaseOffset/8 - Offset/8;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000362 idx = VCalls.size()+1;
363 VCalls.push_back(0);
Mike Stump75ce5732009-10-31 20:06:59 +0000364 D1(printf(" vcall for %s at %d with delta %d\n",
Mike Stumpc5a332c2009-11-13 23:45:53 +0000365 MD->getNameAsString().c_str(), (int)-VCalls.size()-3, 0));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000366 }
367 }
368 }
369
370 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000371 Index_t Offset, int64_t CurrentVBaseOffset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000372 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000373 ++mi) {
374 const CXXMethodDecl *MD = *mi;
375 if (!MD->isVirtual())
376 continue;
377
378 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
379 // For destructors, add both the complete and the deleting destructor
380 // to the vtable.
381 AddMethod(GlobalDecl(DD, Dtor_Complete), MorallyVirtual, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000382 CurrentVBaseOffset);
Eli Friedman03aa2f12009-11-30 01:19:33 +0000383 AddMethod(GlobalDecl(DD, Dtor_Deleting), MorallyVirtual, Offset,
384 CurrentVBaseOffset);
385 } else
386 AddMethod(MD, MorallyVirtual, Offset, CurrentVBaseOffset);
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000387 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000388 }
389
390 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
391 const CXXRecordDecl *PrimaryBase,
392 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000393 int64_t Offset, int64_t CurrentVBaseOffset,
394 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000395 Path->push_back(std::make_pair(RD, Offset));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000396 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
397 e = RD->bases_end(); i != e; ++i) {
398 if (i->isVirtual())
399 continue;
400 const CXXRecordDecl *Base =
401 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
402 if (Base != PrimaryBase || PrimaryBaseWasVirtual) {
403 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
404 StartNewTable();
Mike Stump653d0b92009-11-13 02:13:54 +0000405 GenerateVtableForBase(Base, o, MorallyVirtual, false,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000406 CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000407 }
408 }
Mike Stump37dbe962009-10-15 02:04:03 +0000409 Path->pop_back();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000410 }
411
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000412// #define D(X) do { X; } while (0)
413#define D(X)
414
415 void insertVCalls(int InsertionPoint) {
Mike Stump75ce5732009-10-31 20:06:59 +0000416 D1(printf("============= combining vbase/vcall\n"));
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000417 D(VCalls.insert(VCalls.begin(), 673));
418 D(VCalls.push_back(672));
Anders Carlsson472404f2009-12-04 16:19:30 +0000419
420 Vtable.insert(Vtable.begin() + InsertionPoint, VCalls.size(), 0);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000421 // The vcalls come first...
422 for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(),
423 e = VCalls.rend();
424 i != e; ++i)
Anders Carlsson472404f2009-12-04 16:19:30 +0000425 Vtable[InsertionPoint++] = wrap((0?600:0) + *i);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000426 VCalls.clear();
Mike Stump9f23a142009-11-10 02:30:51 +0000427 VCall.clear();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000428 }
429
Mike Stump559387f2009-11-13 23:13:20 +0000430 void AddAddressPoints(const CXXRecordDecl *RD, uint64_t Offset,
431 Index_t AddressPoint) {
432 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
433 RD->getNameAsCString(), Class->getNameAsCString(),
434 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stumpcd2b8212009-11-19 20:52:19 +0000435 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Mike Stump559387f2009-11-13 23:13:20 +0000436
437 // Now also add the address point for all our primary bases.
438 while (1) {
439 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
440 RD = Layout.getPrimaryBase();
441 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
442 // FIXME: Double check this.
443 if (RD == 0)
444 break;
445 if (PrimaryBaseWasVirtual &&
446 BLayout.getVBaseClassOffset(RD) != Offset)
447 break;
448 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
449 RD->getNameAsCString(), Class->getNameAsCString(),
450 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stumpcd2b8212009-11-19 20:52:19 +0000451 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Mike Stump559387f2009-11-13 23:13:20 +0000452 }
453 }
454
455
Eli Friedmanb05eb962009-12-04 03:54:56 +0000456 Index_t FinishGenerateVtable(const CXXRecordDecl *RD,
457 const ASTRecordLayout &Layout,
458 const CXXRecordDecl *PrimaryBase,
459 bool PrimaryBaseWasVirtual,
460 bool MorallyVirtual, int64_t Offset,
461 bool ForVirtualBase, int64_t CurrentVBaseOffset,
462 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000463 bool alloc = false;
464 if (Path == 0) {
465 alloc = true;
466 Path = new Path_t;
467 }
468
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000469 StartNewTable();
470 extra = 0;
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000471 bool DeferVCalls = MorallyVirtual || ForVirtualBase;
Anders Carlsson472404f2009-12-04 16:19:30 +0000472 int VCallInsertionPoint = Vtable.size();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000473 if (!DeferVCalls) {
474 insertVCalls(VCallInsertionPoint);
Mike Stump8bccbfd2009-10-15 09:30:16 +0000475 } else
476 // FIXME: just for extra, or for all uses of VCalls.size post this?
477 extra = -VCalls.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000478
Anders Carlsson472404f2009-12-04 16:19:30 +0000479 // Add the offset to top.
480 Vtable.push_back(wrap(-((Offset-LayoutOffset)/8)));
481
482 // Add the RTTI information.
483 Vtable.push_back(rtti);
484
485 Index_t AddressPoint = Vtable.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000486
Anders Carlssonddf42c82009-12-04 02:56:03 +0000487 AppendMethodsToVtable();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000488
489 // and then the non-virtual bases.
490 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000491 MorallyVirtual, Offset, CurrentVBaseOffset, Path);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000492
493 if (ForVirtualBase) {
Mike Stump2cefe382009-11-12 20:47:57 +0000494 // FIXME: We're adding to VCalls in callers, we need to do the overrides
495 // in the inner part, so that we know the complete set of vcalls during
496 // the build and don't have to insert into methods. Saving out the
497 // AddressPoint here, would need to be fixed, if we didn't do that. Also
498 // retroactively adding vcalls for overrides later wind up in the wrong
499 // place, the vcall slot has to be alloted during the walk of the base
500 // when the function is first introduces.
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000501 AddressPoint += VCalls.size();
Mike Stump2cefe382009-11-12 20:47:57 +0000502 insertVCalls(VCallInsertionPoint);
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000503 }
504
Mike Stump559387f2009-11-13 23:13:20 +0000505 AddAddressPoints(RD, Offset, AddressPoint);
Mike Stump2cefe382009-11-12 20:47:57 +0000506
Mike Stump37dbe962009-10-15 02:04:03 +0000507 if (alloc) {
508 delete Path;
509 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000510 return AddressPoint;
511 }
512
Mike Stump75ce5732009-10-31 20:06:59 +0000513 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
514 bool updateVBIndex, Index_t current_vbindex,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000515 int64_t CurrentVBaseOffset) {
Mike Stump75ce5732009-10-31 20:06:59 +0000516 if (!RD->isDynamicClass())
517 return;
518
519 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
520 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
521 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
522
523 // vtables are composed from the chain of primaries.
Eli Friedman65d87222009-12-04 08:52:11 +0000524 if (PrimaryBase && !PrimaryBaseWasVirtual) {
Mike Stump75ce5732009-10-31 20:06:59 +0000525 D1(printf(" doing primaries for %s most derived %s\n",
526 RD->getNameAsCString(), Class->getNameAsCString()));
Eli Friedman65d87222009-12-04 08:52:11 +0000527 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
528 updateVBIndex, current_vbindex, CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000529 }
530
531 D1(printf(" doing vcall entries for %s most derived %s\n",
532 RD->getNameAsCString(), Class->getNameAsCString()));
533
534 // And add the virtuals for the class to the primary vtable.
Eli Friedman03aa2f12009-11-30 01:19:33 +0000535 AddMethods(RD, MorallyVirtual, Offset, CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000536 }
537
538 void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
539 bool updateVBIndex, Index_t current_vbindex,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000540 bool RDisVirtualBase, int64_t CurrentVBaseOffset,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000541 bool bottom) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000542 if (!RD->isDynamicClass())
543 return;
544
545 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
546 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
547 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
548
549 // vtables are composed from the chain of primaries.
550 if (PrimaryBase) {
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000551 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
552 if (PrimaryBaseWasVirtual) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000553 IndirectPrimary.insert(PrimaryBase);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000554 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
555 }
Mike Stump75ce5732009-10-31 20:06:59 +0000556
557 D1(printf(" doing primaries for %s most derived %s\n",
558 RD->getNameAsCString(), Class->getNameAsCString()));
559
560 VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000561 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000562 BaseCurrentVBaseOffset, false);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000563 }
564
Mike Stump75ce5732009-10-31 20:06:59 +0000565 D1(printf(" doing vbase entries for %s most derived %s\n",
566 RD->getNameAsCString(), Class->getNameAsCString()));
567 GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex);
568
569 if (RDisVirtualBase || bottom) {
570 Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000571 CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +0000572 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000573 }
574
Mike Stump653d0b92009-11-13 02:13:54 +0000575 int64_t GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0,
576 bool MorallyVirtual = false,
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000577 bool ForVirtualBase = false,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000578 int CurrentVBaseOffset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +0000579 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000580 if (!RD->isDynamicClass())
581 return 0;
582
Mike Stumpfa818082009-11-13 02:35:38 +0000583 // Construction vtable don't need parts that have no virtual bases and
584 // aren't morally virtual.
585 if ((LayoutClass != Class) && RD->getNumVBases() == 0 && !MorallyVirtual)
586 return 0;
587
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000588 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
589 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
590 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
591
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000592 extra = 0;
Mike Stump75ce5732009-10-31 20:06:59 +0000593 D1(printf("building entries for base %s most derived %s\n",
594 RD->getNameAsCString(), Class->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000595
Mike Stump75ce5732009-10-31 20:06:59 +0000596 if (ForVirtualBase)
597 extra = VCalls.size();
598
599 VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0, ForVirtualBase,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000600 CurrentVBaseOffset, true);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000601
602 if (Path)
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000603 OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000604
Eli Friedmanb05eb962009-12-04 03:54:56 +0000605 return FinishGenerateVtable(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
606 MorallyVirtual, Offset, ForVirtualBase,
607 CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000608 }
609
610 void GenerateVtableForVBases(const CXXRecordDecl *RD,
611 int64_t Offset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +0000612 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000613 bool alloc = false;
614 if (Path == 0) {
615 alloc = true;
Mike Stump37dbe962009-10-15 02:04:03 +0000616 Path = new Path_t;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000617 }
618 // FIXME: We also need to override using all paths to a virtual base,
619 // right now, we just process the first path
620 Path->push_back(std::make_pair(RD, Offset));
621 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
622 e = RD->bases_end(); i != e; ++i) {
623 const CXXRecordDecl *Base =
624 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
625 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
626 // Mark it so we don't output it twice.
627 IndirectPrimary.insert(Base);
628 StartNewTable();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000629 VCall.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000630 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000631 int64_t CurrentVBaseOffset = BaseOffset;
Mike Stump75ce5732009-10-31 20:06:59 +0000632 D1(printf("vtable %s virtual base %s\n",
633 Class->getNameAsCString(), Base->getNameAsCString()));
Mike Stump653d0b92009-11-13 02:13:54 +0000634 GenerateVtableForBase(Base, BaseOffset, true, true, CurrentVBaseOffset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000635 Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000636 }
Mike Stump2cefe382009-11-12 20:47:57 +0000637 int64_t BaseOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000638 if (i->isVirtual())
639 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump2cefe382009-11-12 20:47:57 +0000640 else {
641 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
642 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
643 }
644
Mike Stump37dbe962009-10-15 02:04:03 +0000645 if (Base->getNumVBases()) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000646 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump37dbe962009-10-15 02:04:03 +0000647 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000648 }
649 Path->pop_back();
650 if (alloc)
651 delete Path;
652 }
653};
Anders Carlssonca1bf682009-12-03 01:54:02 +0000654} // end anonymous namespace
655
Anders Carlsson657f1392009-12-03 02:32:59 +0000656/// TypeConversionRequiresAdjustment - Returns whether conversion from a
657/// derived type to a base type requires adjustment.
658static bool
659TypeConversionRequiresAdjustment(ASTContext &Ctx,
660 const CXXRecordDecl *DerivedDecl,
661 const CXXRecordDecl *BaseDecl) {
662 CXXBasePaths Paths(/*FindAmbiguities=*/false,
663 /*RecordPaths=*/true, /*DetectVirtual=*/true);
664 if (!const_cast<CXXRecordDecl *>(DerivedDecl)->
665 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseDecl), Paths)) {
666 assert(false && "Class must be derived from the passed in base class!");
667 return false;
668 }
669
670 // If we found a virtual base we always want to require adjustment.
671 if (Paths.getDetectedVirtual())
672 return true;
673
674 const CXXBasePath &Path = Paths.front();
675
676 for (size_t Start = 0, End = Path.size(); Start != End; ++Start) {
677 const CXXBasePathElement &Element = Path[Start];
678
679 // Check the base class offset.
680 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Element.Class);
681
682 const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>();
683 const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl());
684
685 if (Layout.getBaseClassOffset(Base) != 0) {
686 // This requires an adjustment.
687 return true;
688 }
689 }
690
691 return false;
692}
693
694static bool
695TypeConversionRequiresAdjustment(ASTContext &Ctx,
696 QualType DerivedType, QualType BaseType) {
697 // Canonicalize the types.
698 QualType CanDerivedType = Ctx.getCanonicalType(DerivedType);
699 QualType CanBaseType = Ctx.getCanonicalType(BaseType);
700
701 assert(CanDerivedType->getTypeClass() == CanBaseType->getTypeClass() &&
702 "Types must have same type class!");
703
704 if (CanDerivedType == CanBaseType) {
705 // No adjustment needed.
706 return false;
707 }
708
709 if (const ReferenceType *RT = dyn_cast<ReferenceType>(CanDerivedType)) {
710 CanDerivedType = RT->getPointeeType();
711 CanBaseType = cast<ReferenceType>(CanBaseType)->getPointeeType();
712 } else if (const PointerType *PT = dyn_cast<PointerType>(CanDerivedType)) {
713 CanDerivedType = PT->getPointeeType();
714 CanBaseType = cast<PointerType>(CanBaseType)->getPointeeType();
715 } else {
716 assert(false && "Unexpected return type!");
717 }
718
719 if (CanDerivedType == CanBaseType) {
720 // No adjustment needed.
721 return false;
722 }
723
724 const CXXRecordDecl *DerivedDecl =
Anders Carlssondabfa3c2009-12-03 03:28:24 +0000725 cast<CXXRecordDecl>(cast<RecordType>(CanDerivedType)->getDecl());
Anders Carlsson657f1392009-12-03 02:32:59 +0000726
727 const CXXRecordDecl *BaseDecl =
728 cast<CXXRecordDecl>(cast<RecordType>(CanBaseType)->getDecl());
729
730 return TypeConversionRequiresAdjustment(Ctx, DerivedDecl, BaseDecl);
731}
732
Eli Friedman81fb0d22009-12-04 08:40:51 +0000733bool VtableBuilder::OverrideMethod(GlobalDecl GD, bool MorallyVirtual,
734 Index_t OverrideOffset, Index_t Offset,
735 int64_t CurrentVBaseOffset) {
Anders Carlssonca1bf682009-12-03 01:54:02 +0000736 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
737
738 const bool isPure = MD->isPure();
739 typedef CXXMethodDecl::method_iterator meth_iter;
740 // FIXME: Should OverrideOffset's be Offset?
741
742 // FIXME: Don't like the nested loops. For very large inheritance
743 // heirarchies we could have a table on the side with the final overridder
744 // and just replace each instance of an overridden method once. Would be
745 // nice to measure the cost/benefit on real code.
746
747 for (meth_iter mi = MD->begin_overridden_methods(),
748 e = MD->end_overridden_methods();
749 mi != e; ++mi) {
750 GlobalDecl OGD;
751
Anders Carlssonf3935b42009-12-04 05:51:56 +0000752 const CXXMethodDecl *OMD = *mi;
Anders Carlssonca1bf682009-12-03 01:54:02 +0000753 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(OMD))
754 OGD = GlobalDecl(DD, GD.getDtorType());
755 else
756 OGD = OMD;
Eli Friedman3d2e9de2009-12-04 08:34:14 +0000757
758 // FIXME: Explain why this is necessary!
Anders Carlsson7bb70762009-12-04 15:49:02 +0000759 uint64_t Index;
760 if (!Methods.getIndex(OGD, Index))
Eli Friedman3d2e9de2009-12-04 08:34:14 +0000761 continue;
762
Eli Friedman3d2e9de2009-12-04 08:34:14 +0000763 QualType ReturnType =
764 MD->getType()->getAs<FunctionType>()->getResultType();
765 QualType OverriddenReturnType =
766 OMD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonca1bf682009-12-03 01:54:02 +0000767
Eli Friedman3d2e9de2009-12-04 08:34:14 +0000768 // Check if we need a return type adjustment.
769 if (TypeConversionRequiresAdjustment(CGM.getContext(), ReturnType,
770 OverriddenReturnType)) {
771 CanQualType &BaseReturnType = BaseReturnTypes[Index];
Anders Carlssonca1bf682009-12-03 01:54:02 +0000772
Eli Friedman3d2e9de2009-12-04 08:34:14 +0000773 // Get the canonical return type.
774 CanQualType CanReturnType =
775 CGM.getContext().getCanonicalType(ReturnType);
Anders Carlssone6096362009-12-04 03:41:37 +0000776
Eli Friedman3d2e9de2009-12-04 08:34:14 +0000777 // Insert the base return type.
778 if (BaseReturnType.isNull())
779 BaseReturnType =
780 CGM.getContext().getCanonicalType(OverriddenReturnType);
781 }
Anders Carlssona93e9802009-12-04 03:52:52 +0000782
Eli Friedman3d2e9de2009-12-04 08:34:14 +0000783 Methods.OverrideMethod(OGD, GD);
784
Eli Friedman3d2e9de2009-12-04 08:34:14 +0000785 ThisAdjustments.erase(Index);
786 if (MorallyVirtual || VCall.count(OGD)) {
787 Index_t &idx = VCall[OGD];
788 if (idx == 0) {
789 NonVirtualOffset[GD] = -OverrideOffset/8 + CurrentVBaseOffset/8;
790 VCallOffset[GD] = OverrideOffset/8;
791 idx = VCalls.size()+1;
792 VCalls.push_back(0);
793 D1(printf(" vcall for %s at %d with delta %d most derived %s\n",
794 MD->getNameAsString().c_str(), (int)-idx-3,
795 (int)VCalls[idx-1], Class->getNameAsCString()));
796 } else {
797 NonVirtualOffset[GD] = NonVirtualOffset[OGD];
798 VCallOffset[GD] = VCallOffset[OGD];
799 VCalls[idx-1] = -VCallOffset[OGD] + OverrideOffset/8;
800 D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n",
801 MD->getNameAsString().c_str(), (int)-idx-3,
802 (int)VCalls[idx-1], Class->getNameAsCString()));
803 }
804 VCall[GD] = idx;
805 int64_t NonVirtualAdjustment = NonVirtualOffset[GD];
806 int64_t VirtualAdjustment =
807 -((idx + extra + 2) * LLVMPointerWidth / 8);
Anders Carlsson657f1392009-12-03 02:32:59 +0000808
Eli Friedman3d2e9de2009-12-04 08:34:14 +0000809 // Optimize out virtual adjustments of 0.
810 if (VCalls[idx-1] == 0)
811 VirtualAdjustment = 0;
Anders Carlsson657f1392009-12-03 02:32:59 +0000812
Eli Friedman3d2e9de2009-12-04 08:34:14 +0000813 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment,
814 VirtualAdjustment);
Anders Carlsson2ca285f2009-12-03 02:22:59 +0000815
Eli Friedman3d2e9de2009-12-04 08:34:14 +0000816 if (!isPure && !ThisAdjustment.isEmpty())
817 ThisAdjustments[Index] = ThisAdjustment;
Anders Carlssonca1bf682009-12-03 01:54:02 +0000818 return true;
819 }
Eli Friedman3d2e9de2009-12-04 08:34:14 +0000820
821 // FIXME: finish off
822 int64_t NonVirtualAdjustment = VCallOffset[OGD] - OverrideOffset/8;
823
824 if (NonVirtualAdjustment) {
825 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, 0);
826
827 if (!isPure)
828 ThisAdjustments[Index] = ThisAdjustment;
829 }
830 return true;
Anders Carlssonca1bf682009-12-03 01:54:02 +0000831 }
832
833 return false;
Anders Carlssond59885022009-11-27 22:21:51 +0000834}
835
Anders Carlssonddf42c82009-12-04 02:56:03 +0000836void VtableBuilder::AppendMethodsToVtable() {
Anders Carlsson472404f2009-12-04 16:19:30 +0000837 // Reserve room in the vtable for our new methods.
838 Vtable.reserve(Vtable.size() + Methods.size());
Anders Carlssonb07567c2009-12-04 03:07:26 +0000839
Anders Carlsson86809cd2009-12-04 02:43:50 +0000840 for (unsigned i = 0, e = Methods.size(); i != e; ++i) {
841 GlobalDecl GD = Methods[i];
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +0000842 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
843
844 // Get the 'this' pointer adjustment.
Anders Carlsson86809cd2009-12-04 02:43:50 +0000845 ThunkAdjustment ThisAdjustment = ThisAdjustments.lookup(i);
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +0000846
847 // Construct the return type adjustment.
848 ThunkAdjustment ReturnAdjustment;
849
850 QualType BaseReturnType = BaseReturnTypes.lookup(i);
851 if (!BaseReturnType.isNull() && !MD->isPure()) {
852 QualType DerivedType =
853 MD->getType()->getAs<FunctionType>()->getResultType();
854
855 int64_t NonVirtualAdjustment =
856 getNVOffset(BaseReturnType, DerivedType) / 8;
857
858 int64_t VirtualAdjustment =
859 getVbaseOffset(BaseReturnType, DerivedType);
860
861 ReturnAdjustment = ThunkAdjustment(NonVirtualAdjustment,
862 VirtualAdjustment);
863 }
864
Anders Carlsson50f14742009-12-04 03:06:03 +0000865 llvm::Constant *Method = 0;
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +0000866 if (!ReturnAdjustment.isEmpty()) {
867 // Build a covariant thunk.
868 CovariantThunkAdjustment Adjustment(ThisAdjustment, ReturnAdjustment);
Anders Carlsson50f14742009-12-04 03:06:03 +0000869 Method = CGM.BuildCovariantThunk(MD, Extern, Adjustment);
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +0000870 } else if (!ThisAdjustment.isEmpty()) {
871 // Build a "regular" thunk.
Anders Carlsson50f14742009-12-04 03:06:03 +0000872 Method = CGM.BuildThunk(GD, Extern, ThisAdjustment);
Anders Carlssonddf42c82009-12-04 02:56:03 +0000873 } else if (MD->isPure()) {
874 // We have a pure virtual method.
Anders Carlsson50f14742009-12-04 03:06:03 +0000875 Method = getPureVirtualFn();
876 } else {
877 // We have a good old regular method.
878 Method = WrapAddrOf(GD);
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +0000879 }
Anders Carlsson50f14742009-12-04 03:06:03 +0000880
881 // Add the method to the vtable.
Anders Carlsson472404f2009-12-04 16:19:30 +0000882 Vtable.push_back(Method);
Anders Carlsson86809cd2009-12-04 02:43:50 +0000883 }
884
Anders Carlsson50f14742009-12-04 03:06:03 +0000885
Anders Carlsson86809cd2009-12-04 02:43:50 +0000886 ThisAdjustments.clear();
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +0000887 BaseReturnTypes.clear();
Anders Carlsson86809cd2009-12-04 02:43:50 +0000888
Anders Carlsson495634e2009-12-04 02:39:04 +0000889 Methods.clear();
Anders Carlsson495634e2009-12-04 02:39:04 +0000890}
891
Anders Carlssonf942ee02009-11-27 20:47:55 +0000892void CGVtableInfo::ComputeMethodVtableIndices(const CXXRecordDecl *RD) {
893
894 // Itanium C++ ABI 2.5.2:
895 // The order of the virtual function pointers in a virtual table is the
896 // order of declaration of the corresponding member functions in the class.
897 //
898 // There is an entry for any virtual function declared in a class,
899 // whether it is a new function or overrides a base class function,
900 // unless it overrides a function from the primary base, and conversion
901 // between their return types does not require an adjustment.
902
903 int64_t CurrentIndex = 0;
904
905 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
906 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
907
908 if (PrimaryBase) {
Anders Carlssonc920fa22009-11-30 19:43:26 +0000909 assert(PrimaryBase->isDefinition() &&
910 "Should have the definition decl of the primary base!");
Anders Carlssonf942ee02009-11-27 20:47:55 +0000911
912 // Since the record decl shares its vtable pointer with the primary base
913 // we need to start counting at the end of the primary base's vtable.
914 CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase);
915 }
916
917 const CXXDestructorDecl *ImplicitVirtualDtor = 0;
918
919 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
920 e = RD->method_end(); i != e; ++i) {
921 const CXXMethodDecl *MD = *i;
922
923 // We only want virtual methods.
924 if (!MD->isVirtual())
925 continue;
926
927 bool ShouldAddEntryForMethod = true;
928
929 // Check if this method overrides a method in the primary base.
930 for (CXXMethodDecl::method_iterator i = MD->begin_overridden_methods(),
931 e = MD->end_overridden_methods(); i != e; ++i) {
Anders Carlssonf3935b42009-12-04 05:51:56 +0000932 const CXXMethodDecl *OverriddenMD = *i;
Anders Carlssonf942ee02009-11-27 20:47:55 +0000933 const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent();
934 assert(OverriddenMD->isCanonicalDecl() &&
935 "Should have the canonical decl of the overridden RD!");
936
937 if (OverriddenRD == PrimaryBase) {
938 // Check if converting from the return type of the method to the
939 // return type of the overridden method requires conversion.
940 QualType ReturnType =
941 MD->getType()->getAs<FunctionType>()->getResultType();
942 QualType OverriddenReturnType =
943 OverriddenMD->getType()->getAs<FunctionType>()->getResultType();
944
945 if (!TypeConversionRequiresAdjustment(CGM.getContext(),
946 ReturnType, OverriddenReturnType)) {
947 // This index is shared between the index in the vtable of the primary
948 // base class.
949 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
950 const CXXDestructorDecl *OverriddenDD =
951 cast<CXXDestructorDecl>(OverriddenMD);
952
953 // Add both the complete and deleting entries.
954 MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] =
955 getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Complete));
956 MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] =
957 getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting));
958 } else {
959 MethodVtableIndices[MD] = getMethodVtableIndex(OverriddenMD);
960 }
961
962 // We don't need to add an entry for this method.
963 ShouldAddEntryForMethod = false;
964 break;
965 }
966 }
967 }
968
969 if (!ShouldAddEntryForMethod)
970 continue;
971
972 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
973 if (MD->isImplicit()) {
974 assert(!ImplicitVirtualDtor &&
975 "Did already see an implicit virtual dtor!");
976 ImplicitVirtualDtor = DD;
977 continue;
978 }
979
980 // Add the complete dtor.
981 MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++;
982
983 // Add the deleting dtor.
984 MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++;
985 } else {
986 // Add the entry.
987 MethodVtableIndices[MD] = CurrentIndex++;
988 }
989 }
990
991 if (ImplicitVirtualDtor) {
992 // Itanium C++ ABI 2.5.2:
993 // If a class has an implicitly-defined virtual destructor,
994 // its entries come after the declared virtual function pointers.
995
996 // Add the complete dtor.
997 MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] =
998 CurrentIndex++;
999
1000 // Add the deleting dtor.
1001 MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] =
1002 CurrentIndex++;
1003 }
1004
1005 NumVirtualFunctionPointers[RD] = CurrentIndex;
1006}
1007
1008uint64_t CGVtableInfo::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) {
1009 llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
1010 NumVirtualFunctionPointers.find(RD);
1011 if (I != NumVirtualFunctionPointers.end())
1012 return I->second;
1013
1014 ComputeMethodVtableIndices(RD);
1015
1016 I = NumVirtualFunctionPointers.find(RD);
1017 assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!");
1018 return I->second;
1019}
1020
1021uint64_t CGVtableInfo::getMethodVtableIndex(GlobalDecl GD) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001022 MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001023 if (I != MethodVtableIndices.end())
1024 return I->second;
1025
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001026 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
Anders Carlssonf942ee02009-11-27 20:47:55 +00001027
1028 ComputeMethodVtableIndices(RD);
1029
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001030 I = MethodVtableIndices.find(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001031 assert(I != MethodVtableIndices.end() && "Did not find index!");
1032 return I->second;
1033}
1034
1035int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
1036 const CXXRecordDecl *VBase) {
1037 ClassPairTy ClassPair(RD, VBase);
1038
1039 VirtualBaseClassIndiciesTy::iterator I =
1040 VirtualBaseClassIndicies.find(ClassPair);
1041 if (I != VirtualBaseClassIndicies.end())
1042 return I->second;
1043
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001044 // FIXME: This seems expensive. Can we do a partial job to get
1045 // just this data.
Anders Carlsson472404f2009-12-04 16:19:30 +00001046 VtableBuilder b(RD, RD, 0, CGM);
Mike Stump75ce5732009-10-31 20:06:59 +00001047 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001048 b.GenerateVtableForBase(RD);
1049 b.GenerateVtableForVBases(RD);
1050
1051 for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
1052 b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) {
1053 // Insert all types.
1054 ClassPairTy ClassPair(RD, I->first);
1055
1056 VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second));
1057 }
1058
1059 I = VirtualBaseClassIndicies.find(ClassPair);
1060 assert(I != VirtualBaseClassIndicies.end() && "Did not find index!");
1061
1062 return I->second;
1063}
1064
Mike Stump2cefe382009-11-12 20:47:57 +00001065llvm::Constant *CodeGenModule::GenerateVtable(const CXXRecordDecl *LayoutClass,
1066 const CXXRecordDecl *RD,
Mike Stumpeac45592009-11-11 20:26:26 +00001067 uint64_t Offset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001068 llvm::SmallString<256> OutName;
Mike Stump2cefe382009-11-12 20:47:57 +00001069 if (LayoutClass != RD)
Daniel Dunbare128dd12009-11-21 09:06:22 +00001070 getMangleContext().mangleCXXCtorVtable(LayoutClass, Offset/8, RD, OutName);
Mike Stumpeac45592009-11-11 20:26:26 +00001071 else
Daniel Dunbare128dd12009-11-21 09:06:22 +00001072 getMangleContext().mangleCXXVtable(RD, OutName);
1073 llvm::StringRef Name = OutName.str();
Benjamin Kramerbb0a07b2009-10-11 22:57:54 +00001074
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001075 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1076 int64_t AddressPoint;
1077
Mike Stumpaa51ad62009-11-19 04:04:36 +00001078 llvm::GlobalVariable *GV = getModule().getGlobalVariable(Name);
Mike Stumpcd2b8212009-11-19 20:52:19 +00001079 if (GV && AddressPoints[LayoutClass] && !GV->isDeclaration()) {
Mike Stumpaa51ad62009-11-19 04:04:36 +00001080 AddressPoint=(*(*(AddressPoints[LayoutClass]))[RD])[std::make_pair(RD,
1081 Offset)];
Mike Stumpcd2b8212009-11-19 20:52:19 +00001082 // FIXME: We can never have 0 address point. Do this for now so gepping
1083 // retains the same structure. Later, we'll just assert.
1084 if (AddressPoint == 0)
1085 AddressPoint = 1;
1086 } else {
Anders Carlsson472404f2009-12-04 16:19:30 +00001087 VtableBuilder b(RD, LayoutClass, Offset, *this);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001088
Mike Stumpaa51ad62009-11-19 04:04:36 +00001089 D1(printf("vtable %s\n", RD->getNameAsCString()));
1090 // First comes the vtables for all the non-virtual bases...
1091 AddressPoint = b.GenerateVtableForBase(RD, Offset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001092
Mike Stumpaa51ad62009-11-19 04:04:36 +00001093 // then the vtables for all the virtual bases.
1094 b.GenerateVtableForVBases(RD, Offset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001095
Mike Stumpaa51ad62009-11-19 04:04:36 +00001096 bool CreateDefinition = true;
1097 if (LayoutClass != RD)
1098 CreateDefinition = true;
1099 else {
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001100 const ASTRecordLayout &Layout =
1101 getContext().getASTRecordLayout(LayoutClass);
1102
1103 if (const CXXMethodDecl *KeyFunction = Layout.getKeyFunction()) {
Mike Stumpaa51ad62009-11-19 04:04:36 +00001104 if (!KeyFunction->getBody()) {
1105 // If there is a KeyFunction, and it isn't defined, just build a
1106 // reference to the vtable.
1107 CreateDefinition = false;
1108 }
1109 }
1110 }
1111
1112 llvm::Constant *C = 0;
1113 llvm::Type *type = Ptr8Ty;
1114 llvm::GlobalVariable::LinkageTypes linktype
1115 = llvm::GlobalValue::ExternalLinkage;
1116 if (CreateDefinition) {
Anders Carlsson472404f2009-12-04 16:19:30 +00001117 llvm::ArrayType *ntype =
1118 llvm::ArrayType::get(Ptr8Ty, b.getVtable().size());
Anders Carlsson4e1d75f2009-12-04 16:22:27 +00001119 C = llvm::ConstantArray::get(ntype, &b.getVtable()[0],
1120 b.getVtable().size());
Mike Stumpaa51ad62009-11-19 04:04:36 +00001121 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
1122 if (LayoutClass->isInAnonymousNamespace())
1123 linktype = llvm::GlobalValue::InternalLinkage;
1124 type = ntype;
1125 }
1126 llvm::GlobalVariable *OGV = GV;
1127 GV = new llvm::GlobalVariable(getModule(), type, true, linktype, C, Name);
1128 if (OGV) {
1129 GV->takeName(OGV);
1130 llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
1131 OGV->getType());
1132 OGV->replaceAllUsesWith(NewPtr);
1133 OGV->eraseFromParent();
1134 }
1135 bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden;
1136 if (Hidden)
1137 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
1138 }
Mike Stumpc0f632d2009-11-18 04:00:48 +00001139 llvm::Constant *vtable = llvm::ConstantExpr::getBitCast(GV, Ptr8Ty);
Mike Stumpd846d082009-11-10 07:44:33 +00001140 llvm::Constant *AddressPointC;
1141 uint32_t LLVMPointerWidth = getContext().Target.getPointerWidth(0);
1142 AddressPointC = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1143 AddressPoint*LLVMPointerWidth/8);
Mike Stump2cefe382009-11-12 20:47:57 +00001144 vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(vtable, &AddressPointC,
1145 1);
Mike Stumpd846d082009-11-10 07:44:33 +00001146
Mike Stumpcd2b8212009-11-19 20:52:19 +00001147 assert(vtable->getType() == Ptr8Ty);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001148 return vtable;
1149}
Mike Stump9f23a142009-11-10 02:30:51 +00001150
Mike Stumpae1b85d2009-12-02 19:07:44 +00001151namespace {
Mike Stump9f23a142009-11-10 02:30:51 +00001152class VTTBuilder {
1153 /// Inits - The list of values built for the VTT.
1154 std::vector<llvm::Constant *> &Inits;
1155 /// Class - The most derived class that this vtable is being built for.
1156 const CXXRecordDecl *Class;
1157 CodeGenModule &CGM; // Per-module state.
Mike Stump8b2d2d02009-11-11 00:35:07 +00001158 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
Mike Stumpc7b9f5e2009-11-11 03:08:24 +00001159 /// BLayout - Layout for the most derived class that this vtable is being
1160 /// built for.
1161 const ASTRecordLayout &BLayout;
Mike Stump83066c82009-11-13 01:54:23 +00001162 CodeGenModule::AddrMap_t &AddressPoints;
Mike Stump2cefe382009-11-12 20:47:57 +00001163 // vtbl - A pointer to the vtable for Class.
1164 llvm::Constant *ClassVtbl;
1165 llvm::LLVMContext &VMContext;
Mike Stump9f23a142009-11-10 02:30:51 +00001166
Mike Stump8677bc22009-11-12 22:56:32 +00001167 /// BuildVtablePtr - Build up a referene to the given secondary vtable
Mike Stump83066c82009-11-13 01:54:23 +00001168 llvm::Constant *BuildVtablePtr(llvm::Constant *vtbl,
1169 const CXXRecordDecl *VtblClass,
1170 const CXXRecordDecl *RD,
Mike Stump8677bc22009-11-12 22:56:32 +00001171 uint64_t Offset) {
1172 int64_t AddressPoint;
Mike Stump83066c82009-11-13 01:54:23 +00001173 AddressPoint = (*AddressPoints[VtblClass])[std::make_pair(RD, Offset)];
Mike Stump2b34bc52009-11-12 23:36:21 +00001174 // FIXME: We can never have 0 address point. Do this for now so gepping
Mike Stumpcd2b8212009-11-19 20:52:19 +00001175 // retains the same structure. Later we'll just assert.
Mike Stump2b34bc52009-11-12 23:36:21 +00001176 if (AddressPoint == 0)
1177 AddressPoint = 1;
Mike Stump83066c82009-11-13 01:54:23 +00001178 D1(printf("XXX address point for %s in %s layout %s at offset %d was %d\n",
1179 RD->getNameAsCString(), VtblClass->getNameAsCString(),
1180 Class->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stump8677bc22009-11-12 22:56:32 +00001181 uint32_t LLVMPointerWidth = CGM.getContext().Target.getPointerWidth(0);
1182 llvm::Constant *init;
1183 init = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
1184 AddressPoint*LLVMPointerWidth/8);
1185 init = llvm::ConstantExpr::getInBoundsGetElementPtr(vtbl, &init, 1);
1186 return init;
1187 }
1188
Mike Stump2cefe382009-11-12 20:47:57 +00001189 /// Secondary - Add the secondary vtable pointers to Inits. Offset is the
1190 /// current offset in bits to the object we're working on.
Mike Stump8677bc22009-11-12 22:56:32 +00001191 void Secondary(const CXXRecordDecl *RD, llvm::Constant *vtbl,
Mike Stump83066c82009-11-13 01:54:23 +00001192 const CXXRecordDecl *VtblClass, uint64_t Offset=0,
1193 bool MorallyVirtual=false) {
Mike Stump8b2d2d02009-11-11 00:35:07 +00001194 if (RD->getNumVBases() == 0 && ! MorallyVirtual)
1195 return;
1196
1197 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1198 e = RD->bases_end(); i != e; ++i) {
1199 const CXXRecordDecl *Base =
1200 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1201 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1202 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1203 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1204 bool NonVirtualPrimaryBase;
1205 NonVirtualPrimaryBase = !PrimaryBaseWasVirtual && Base == PrimaryBase;
1206 bool BaseMorallyVirtual = MorallyVirtual | i->isVirtual();
Mike Stumpc7b9f5e2009-11-11 03:08:24 +00001207 uint64_t BaseOffset;
1208 if (!i->isVirtual()) {
1209 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1210 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
1211 } else
1212 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump2b34bc52009-11-12 23:36:21 +00001213 llvm::Constant *subvtbl = vtbl;
Mike Stump83066c82009-11-13 01:54:23 +00001214 const CXXRecordDecl *subVtblClass = VtblClass;
Mike Stump8b2d2d02009-11-11 00:35:07 +00001215 if ((Base->getNumVBases() || BaseMorallyVirtual)
1216 && !NonVirtualPrimaryBase) {
1217 // FIXME: Slightly too many of these for __ZTT8test8_B2
Mike Stump8677bc22009-11-12 22:56:32 +00001218 llvm::Constant *init;
Mike Stump2b34bc52009-11-12 23:36:21 +00001219 if (BaseMorallyVirtual)
Mike Stump83066c82009-11-13 01:54:23 +00001220 init = BuildVtablePtr(vtbl, VtblClass, RD, Offset);
Mike Stump2b34bc52009-11-12 23:36:21 +00001221 else {
Mike Stump8677bc22009-11-12 22:56:32 +00001222 init = CGM.getVtableInfo().getCtorVtable(Class, Base, BaseOffset);
Mike Stump2b34bc52009-11-12 23:36:21 +00001223 subvtbl = dyn_cast<llvm::Constant>(init->getOperand(0));
Mike Stump83066c82009-11-13 01:54:23 +00001224 subVtblClass = Base;
Mike Stump2b34bc52009-11-12 23:36:21 +00001225 }
Mike Stump8677bc22009-11-12 22:56:32 +00001226 Inits.push_back(init);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001227 }
Mike Stump83066c82009-11-13 01:54:23 +00001228 Secondary(Base, subvtbl, subVtblClass, BaseOffset, BaseMorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001229 }
1230 }
1231
Mike Stump2cefe382009-11-12 20:47:57 +00001232 /// BuiltVTT - Add the VTT to Inits. Offset is the offset in bits to the
1233 /// currnet object we're working on.
1234 void BuildVTT(const CXXRecordDecl *RD, uint64_t Offset, bool MorallyVirtual) {
Mike Stump8b2d2d02009-11-11 00:35:07 +00001235 if (RD->getNumVBases() == 0 && !MorallyVirtual)
1236 return;
1237
Mike Stump2cefe382009-11-12 20:47:57 +00001238 llvm::Constant *init;
Mike Stump83066c82009-11-13 01:54:23 +00001239 const CXXRecordDecl *VtblClass;
1240
Mike Stump8b2d2d02009-11-11 00:35:07 +00001241 // First comes the primary virtual table pointer...
Mike Stump83066c82009-11-13 01:54:23 +00001242 if (MorallyVirtual) {
1243 init = BuildVtablePtr(ClassVtbl, Class, RD, Offset);
1244 VtblClass = Class;
1245 } else {
Mike Stump2cefe382009-11-12 20:47:57 +00001246 init = CGM.getVtableInfo().getCtorVtable(Class, RD, Offset);
Mike Stump83066c82009-11-13 01:54:23 +00001247 VtblClass = RD;
1248 }
Mike Stump8677bc22009-11-12 22:56:32 +00001249 llvm::Constant *vtbl = dyn_cast<llvm::Constant>(init->getOperand(0));
Mike Stump2cefe382009-11-12 20:47:57 +00001250 Inits.push_back(init);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001251
1252 // then the secondary VTTs....
Mike Stump2cefe382009-11-12 20:47:57 +00001253 SecondaryVTTs(RD, Offset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001254
1255 // and last the secondary vtable pointers.
Mike Stump83066c82009-11-13 01:54:23 +00001256 Secondary(RD, vtbl, VtblClass, Offset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001257 }
1258
1259 /// SecondaryVTTs - Add the secondary VTTs to Inits. The secondary VTTs are
1260 /// built from each direct non-virtual proper base that requires a VTT in
1261 /// declaration order.
Mike Stump2cefe382009-11-12 20:47:57 +00001262 void SecondaryVTTs(const CXXRecordDecl *RD, uint64_t Offset=0,
1263 bool MorallyVirtual=false) {
Mike Stump8b2d2d02009-11-11 00:35:07 +00001264 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1265 e = RD->bases_end(); i != e; ++i) {
1266 const CXXRecordDecl *Base =
1267 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1268 if (i->isVirtual())
1269 continue;
Mike Stump2cefe382009-11-12 20:47:57 +00001270 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1271 uint64_t BaseOffset = Offset + Layout.getBaseClassOffset(Base);
1272 BuildVTT(Base, BaseOffset, MorallyVirtual);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001273 }
1274 }
1275
1276 /// VirtualVTTs - Add the VTT for each proper virtual base in inheritance
1277 /// graph preorder.
1278 void VirtualVTTs(const CXXRecordDecl *RD) {
1279 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1280 e = RD->bases_end(); i != e; ++i) {
1281 const CXXRecordDecl *Base =
1282 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1283 if (i->isVirtual() && !SeenVBase.count(Base)) {
1284 SeenVBase.insert(Base);
Mike Stump2cefe382009-11-12 20:47:57 +00001285 uint64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
1286 BuildVTT(Base, BaseOffset, true);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001287 }
1288 VirtualVTTs(Base);
1289 }
1290 }
Mike Stump9f23a142009-11-10 02:30:51 +00001291public:
1292 VTTBuilder(std::vector<llvm::Constant *> &inits, const CXXRecordDecl *c,
Mike Stumpc7b9f5e2009-11-11 03:08:24 +00001293 CodeGenModule &cgm)
1294 : Inits(inits), Class(c), CGM(cgm),
Mike Stump2cefe382009-11-12 20:47:57 +00001295 BLayout(cgm.getContext().getASTRecordLayout(c)),
Mike Stump83066c82009-11-13 01:54:23 +00001296 AddressPoints(*cgm.AddressPoints[c]),
Mike Stump2cefe382009-11-12 20:47:57 +00001297 VMContext(cgm.getModule().getContext()) {
Mike Stumpd846d082009-11-10 07:44:33 +00001298
Mike Stump8b2d2d02009-11-11 00:35:07 +00001299 // First comes the primary virtual table pointer for the complete class...
Mike Stump2cefe382009-11-12 20:47:57 +00001300 ClassVtbl = CGM.getVtableInfo().getVtable(Class);
1301 Inits.push_back(ClassVtbl);
1302 ClassVtbl = dyn_cast<llvm::Constant>(ClassVtbl->getOperand(0));
1303
Mike Stump8b2d2d02009-11-11 00:35:07 +00001304 // then the secondary VTTs...
1305 SecondaryVTTs(Class);
1306
1307 // then the secondary vtable pointers...
Mike Stump83066c82009-11-13 01:54:23 +00001308 Secondary(Class, ClassVtbl, Class);
Mike Stump8b2d2d02009-11-11 00:35:07 +00001309
1310 // and last, the virtual VTTs.
1311 VirtualVTTs(Class);
Mike Stump9f23a142009-11-10 02:30:51 +00001312 }
1313};
Mike Stumpae1b85d2009-12-02 19:07:44 +00001314}
Mike Stump9f23a142009-11-10 02:30:51 +00001315
Mike Stumpd846d082009-11-10 07:44:33 +00001316llvm::Constant *CodeGenModule::GenerateVTT(const CXXRecordDecl *RD) {
Mike Stumpb4722212009-11-10 19:13:04 +00001317 // Only classes that have virtual bases need a VTT.
1318 if (RD->getNumVBases() == 0)
1319 return 0;
1320
Mike Stump9f23a142009-11-10 02:30:51 +00001321 llvm::SmallString<256> OutName;
Daniel Dunbare128dd12009-11-21 09:06:22 +00001322 getMangleContext().mangleCXXVTT(RD, OutName);
1323 llvm::StringRef Name = OutName.str();
Mike Stump9f23a142009-11-10 02:30:51 +00001324
1325 llvm::GlobalVariable::LinkageTypes linktype;
1326 linktype = llvm::GlobalValue::LinkOnceODRLinkage;
Mike Stumpaa51ad62009-11-19 04:04:36 +00001327 if (RD->isInAnonymousNamespace())
1328 linktype = llvm::GlobalValue::InternalLinkage;
Mike Stump9f23a142009-11-10 02:30:51 +00001329 std::vector<llvm::Constant *> inits;
1330 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0);
1331
Mike Stump9f23a142009-11-10 02:30:51 +00001332 D1(printf("vtt %s\n", RD->getNameAsCString()));
1333
Mike Stump8b2d2d02009-11-11 00:35:07 +00001334 VTTBuilder b(inits, RD, *this);
1335
Mike Stump9f23a142009-11-10 02:30:51 +00001336 llvm::Constant *C;
1337 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, inits.size());
1338 C = llvm::ConstantArray::get(type, inits);
Mike Stumpc0f632d2009-11-18 04:00:48 +00001339 llvm::GlobalVariable *vtt = new llvm::GlobalVariable(getModule(), type, true,
Mike Stumpaa51ad62009-11-19 04:04:36 +00001340 linktype, C, Name);
Mike Stumpc0f632d2009-11-18 04:00:48 +00001341 bool Hidden = getDeclVisibilityMode(RD) == LangOptions::Hidden;
1342 if (Hidden)
1343 vtt->setVisibility(llvm::GlobalVariable::HiddenVisibility);
1344 return llvm::ConstantExpr::getBitCast(vtt, Ptr8Ty);
Mike Stump9f23a142009-11-10 02:30:51 +00001345}
Mike Stumpd846d082009-11-10 07:44:33 +00001346
Mike Stump1a139f82009-11-19 01:08:19 +00001347void CGVtableInfo::GenerateClassData(const CXXRecordDecl *RD) {
1348 Vtables[RD] = CGM.GenerateVtable(RD, RD);
Mike Stumpc01c2b82009-12-02 18:57:08 +00001349 CGM.GenerateRTTI(RD);
Mike Stump1a139f82009-11-19 01:08:19 +00001350 CGM.GenerateVTT(RD);
1351}
1352
Mike Stumpeac45592009-11-11 20:26:26 +00001353llvm::Constant *CGVtableInfo::getVtable(const CXXRecordDecl *RD) {
Mike Stumpd846d082009-11-10 07:44:33 +00001354 llvm::Constant *&vtbl = Vtables[RD];
1355 if (vtbl)
1356 return vtbl;
Mike Stump2cefe382009-11-12 20:47:57 +00001357 vtbl = CGM.GenerateVtable(RD, RD);
Mike Stumpaa51ad62009-11-19 04:04:36 +00001358
1359 bool CreateDefinition = true;
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001360
1361 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1362 if (const CXXMethodDecl *KeyFunction = Layout.getKeyFunction()) {
Mike Stumpaa51ad62009-11-19 04:04:36 +00001363 if (!KeyFunction->getBody()) {
1364 // If there is a KeyFunction, and it isn't defined, just build a
1365 // reference to the vtable.
1366 CreateDefinition = false;
1367 }
1368 }
1369
1370 if (CreateDefinition) {
Mike Stumpc01c2b82009-12-02 18:57:08 +00001371 CGM.GenerateRTTI(RD);
Mike Stumpaa51ad62009-11-19 04:04:36 +00001372 CGM.GenerateVTT(RD);
1373 }
Mike Stumpd846d082009-11-10 07:44:33 +00001374 return vtbl;
1375}
Mike Stumpeac45592009-11-11 20:26:26 +00001376
Mike Stump2cefe382009-11-12 20:47:57 +00001377llvm::Constant *CGVtableInfo::getCtorVtable(const CXXRecordDecl *LayoutClass,
1378 const CXXRecordDecl *RD,
Mike Stumpeac45592009-11-11 20:26:26 +00001379 uint64_t Offset) {
Mike Stump2cefe382009-11-12 20:47:57 +00001380 return CGM.GenerateVtable(LayoutClass, RD, Offset);
Mike Stumpeac45592009-11-11 20:26:26 +00001381}
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001382
1383void CGVtableInfo::MaybeEmitVtable(GlobalDecl GD) {
1384 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1385 const CXXRecordDecl *RD = MD->getParent();
1386
1387 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1388
1389 // Get the key function.
1390 const CXXMethodDecl *KeyFunction = Layout.getKeyFunction();
1391
1392 if (!KeyFunction) {
1393 // If there's no key function, we don't want to emit the vtable here.
1394 return;
1395 }
1396
1397 // Check if we have the key function.
1398 if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl())
1399 return;
1400
1401 // If the key function is a destructor, we only want to emit the vtable once,
1402 // so do it for the complete destructor.
1403 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() != Dtor_Complete)
1404 return;
1405
1406 // Emit the data.
1407 GenerateClassData(RD);
1408}
1409