blob: efad6289c025e1f5cb49a137e4636c7ce06c15f3 [file] [log] [blame]
Anders Carlssondbd920c2009-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 Carlssond6b07fb2009-11-27 20:47:55 +000016#include "clang/AST/CXXInheritance.h"
Anders Carlssondbd920c2009-10-11 22:13:54 +000017#include "clang/AST/RecordLayout.h"
Anders Carlsson5dd730a2009-11-26 19:32:45 +000018#include "llvm/ADT/DenseSet.h"
Anders Carlssonb9021e92010-02-27 16:18:19 +000019#include "llvm/ADT/SetVector.h"
Chandler Carruthe087f072010-02-13 10:38:52 +000020#include "llvm/Support/Compiler.h"
Anders Carlsson824d7ea2010-02-11 08:02:13 +000021#include "llvm/Support/Format.h"
Anders Carlsson5e454aa2010-03-17 20:06:32 +000022#include <algorithm>
Zhongxing Xu7fe26ac2009-11-13 05:46:16 +000023#include <cstdio>
Anders Carlssondbd920c2009-10-11 22:13:54 +000024
25using namespace clang;
26using namespace CodeGen;
27
Anders Carlsson27f69d02009-11-27 22:21:51 +000028namespace {
Anders Carlsson824d7ea2010-02-11 08:02:13 +000029
Anders Carlsson2fca9be2010-02-23 01:34:28 +000030/// BaseOffset - Represents an offset from a derived class to a direct or
31/// indirect base class.
32struct BaseOffset {
33 /// DerivedClass - The derived class.
34 const CXXRecordDecl *DerivedClass;
35
36 /// VirtualBase - If the path from the derived class to the base class
37 /// involves a virtual base class, this holds its declaration.
38 const CXXRecordDecl *VirtualBase;
39
40 /// NonVirtualOffset - The offset from the derived class to the base class.
41 /// (Or the offset from the virtual base class to the base class, if the
42 /// path from the derived class to the base class involves a virtual base
43 /// class.
44 int64_t NonVirtualOffset;
45
46 BaseOffset() : DerivedClass(0), VirtualBase(0), NonVirtualOffset(0) { }
47 BaseOffset(const CXXRecordDecl *DerivedClass,
48 const CXXRecordDecl *VirtualBase, int64_t NonVirtualOffset)
49 : DerivedClass(DerivedClass), VirtualBase(VirtualBase),
50 NonVirtualOffset(NonVirtualOffset) { }
51
52 bool isEmpty() const { return !NonVirtualOffset && !VirtualBase; }
53};
54
Anders Carlssonbdf73d82010-02-11 21:24:32 +000055/// FinalOverriders - Contains the final overrider member functions for all
56/// member functions in the base subobjects of a class.
57class FinalOverriders {
Anders Carlssona7575822010-02-12 16:55:34 +000058public:
59 /// OverriderInfo - Information about a final overrider.
60 struct OverriderInfo {
61 /// Method - The method decl of the overrider.
62 const CXXMethodDecl *Method;
Anders Carlsson96309c42010-02-18 16:29:24 +000063
Anders Carlssondb4022c2010-03-10 02:33:41 +000064 /// Offset - the base offset of the overrider in the layout class.
65 uint64_t Offset;
Anders Carlssona7575822010-02-12 16:55:34 +000066
Anders Carlssoncd0d27f2010-03-12 05:02:01 +000067 OverriderInfo() : Method(0), Offset(0) { }
Anders Carlssona7575822010-02-12 16:55:34 +000068 };
69
70private:
Anders Carlssonbdf73d82010-02-11 21:24:32 +000071 /// MostDerivedClass - The most derived class for which the final overriders
72 /// are stored.
73 const CXXRecordDecl *MostDerivedClass;
74
Anders Carlssondb4022c2010-03-10 02:33:41 +000075 /// MostDerivedClassOffset - If we're building final overriders for a
76 /// construction vtable, this holds the offset from the layout class to the
77 /// most derived class.
78 const uint64_t MostDerivedClassOffset;
79
80 /// LayoutClass - The class we're using for layout information. Will be
81 /// different than the most derived class if the final overriders are for a
82 /// construction vtable.
83 const CXXRecordDecl *LayoutClass;
84
Anders Carlssonbdf73d82010-02-11 21:24:32 +000085 ASTContext &Context;
86
87 /// MostDerivedClassLayout - the AST record layout of the most derived class.
88 const ASTRecordLayout &MostDerivedClassLayout;
89
Anders Carlsson0c0eeb22010-02-13 02:02:03 +000090 /// BaseSubobjectMethodPairTy - Uniquely identifies a member function
91 /// in a base subobject.
92 typedef std::pair<BaseSubobject, const CXXMethodDecl *>
93 BaseSubobjectMethodPairTy;
94
95 typedef llvm::DenseMap<BaseSubobjectMethodPairTy,
Anders Carlssona7575822010-02-12 16:55:34 +000096 OverriderInfo> OverridersMapTy;
Anders Carlssonbdf73d82010-02-11 21:24:32 +000097
98 /// OverridersMap - The final overriders for all virtual member functions of
99 /// all the base subobjects of the most derived class.
100 OverridersMapTy OverridersMap;
101
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000102 /// VisitedVirtualBases - A set of all the visited virtual bases, used to
103 /// avoid visiting virtual bases more than once.
104 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
105
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000106 typedef llvm::DenseMap<BaseSubobjectMethodPairTy, BaseOffset>
107 AdjustmentOffsetsMapTy;
108
109 /// ReturnAdjustments - Holds return adjustments for all the overriders that
110 /// need to perform return value adjustments.
111 AdjustmentOffsetsMapTy ReturnAdjustments;
Anders Carlssondf31af12010-02-13 23:17:31 +0000112
Anders Carlssonce57dd52010-03-03 04:58:02 +0000113 // FIXME: We might be able to get away with making this a SmallSet.
114 typedef llvm::SmallSetVector<uint64_t, 2> OffsetSetVectorTy;
Anders Carlssonb8358d82010-02-12 01:40:03 +0000115
116 /// SubobjectOffsetsMapTy - This map is used for keeping track of all the
117 /// base subobject offsets that a single class declaration might refer to.
118 ///
119 /// For example, in:
120 ///
121 /// struct A { virtual void f(); };
122 /// struct B1 : A { };
123 /// struct B2 : A { };
124 /// struct C : B1, B2 { virtual void f(); };
125 ///
126 /// when we determine that C::f() overrides A::f(), we need to update the
127 /// overriders map for both A-in-B1 and A-in-B2 and the subobject offsets map
128 /// will have the subobject offsets for both A copies.
Anders Carlssonce57dd52010-03-03 04:58:02 +0000129 typedef llvm::DenseMap<const CXXRecordDecl *, OffsetSetVectorTy>
Anders Carlssonb8358d82010-02-12 01:40:03 +0000130 SubobjectOffsetsMapTy;
131
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000132 /// ComputeFinalOverriders - Compute the final overriders for a given base
133 /// subobject (and all its direct and indirect bases).
Anders Carlssonb8358d82010-02-12 01:40:03 +0000134 void ComputeFinalOverriders(BaseSubobject Base,
Anders Carlssond5c7cca2010-02-17 17:48:25 +0000135 bool BaseSubobjectIsVisitedVBase,
Anders Carlssondb4022c2010-03-10 02:33:41 +0000136 uint64_t OffsetInLayoutClass,
Anders Carlssonb8358d82010-02-12 01:40:03 +0000137 SubobjectOffsetsMapTy &Offsets);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000138
139 /// AddOverriders - Add the final overriders for this base subobject to the
140 /// map of final overriders.
Anders Carlssonc1246c82010-03-26 00:35:45 +0000141 void AddOverriders(BaseSubobject Base, uint64_t OffsetInLayoutClass,
Anders Carlssondb4022c2010-03-10 02:33:41 +0000142 SubobjectOffsetsMapTy &Offsets);
Anders Carlssonb8358d82010-02-12 01:40:03 +0000143
144 /// PropagateOverrider - Propagate the NewMD overrider to all the functions
145 /// that OldMD overrides. For example, if we have:
146 ///
147 /// struct A { virtual void f(); };
148 /// struct B : A { virtual void f(); };
149 /// struct C : B { virtual void f(); };
150 ///
151 /// and we want to override B::f with C::f, we also need to override A::f with
152 /// C::f.
153 void PropagateOverrider(const CXXMethodDecl *OldMD,
Anders Carlsson74c364e2010-02-13 22:23:31 +0000154 BaseSubobject NewBase,
Anders Carlssondb4022c2010-03-10 02:33:41 +0000155 uint64_t OverriderOffsetInLayoutClass,
Anders Carlssonb8358d82010-02-12 01:40:03 +0000156 const CXXMethodDecl *NewMD,
157 SubobjectOffsetsMapTy &Offsets);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000158
Anders Carlssonb8358d82010-02-12 01:40:03 +0000159 static void MergeSubobjectOffsets(const SubobjectOffsetsMapTy &NewOffsets,
160 SubobjectOffsetsMapTy &Offsets);
161
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000162public:
Anders Carlssondb4022c2010-03-10 02:33:41 +0000163 FinalOverriders(const CXXRecordDecl *MostDerivedClass,
164 uint64_t MostDerivedClassOffset,
165 const CXXRecordDecl *LayoutClass);
Anders Carlsson2157d302010-02-24 22:18:01 +0000166
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000167 /// getOverrider - Get the final overrider for the given method declaration in
168 /// the given base subobject.
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000169 OverriderInfo getOverrider(BaseSubobject Base,
170 const CXXMethodDecl *MD) const {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000171 assert(OverridersMap.count(std::make_pair(Base, MD)) &&
172 "Did not find overrider!");
173
174 return OverridersMap.lookup(std::make_pair(Base, MD));
175 }
176
Anders Carlsson49bac9a2010-02-13 23:40:17 +0000177 /// getReturnAdjustmentOffset - Get the return adjustment offset for the
178 /// method decl in the given base subobject. Returns an empty base offset if
179 /// no adjustment is needed.
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000180 BaseOffset getReturnAdjustmentOffset(BaseSubobject Base,
181 const CXXMethodDecl *MD) const {
182 return ReturnAdjustments.lookup(std::make_pair(Base, MD));
183 }
184
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000185 /// dump - dump the final overriders.
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000186 void dump() {
187 assert(VisitedVirtualBases.empty() &&
188 "Visited virtual bases aren't empty!");
189 dump(llvm::errs(), BaseSubobject(MostDerivedClass, 0));
190 VisitedVirtualBases.clear();
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000191 }
192
193 /// dump - dump the final overriders for a base subobject, and all its direct
194 /// and indirect base subobjects.
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000195 void dump(llvm::raw_ostream &Out, BaseSubobject Base);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000196};
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000197
Anders Carlsson327568e2010-02-27 20:02:53 +0000198#define DUMP_OVERRIDERS 0
199
Anders Carlssondb4022c2010-03-10 02:33:41 +0000200FinalOverriders::FinalOverriders(const CXXRecordDecl *MostDerivedClass,
201 uint64_t MostDerivedClassOffset,
202 const CXXRecordDecl *LayoutClass)
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000203 : MostDerivedClass(MostDerivedClass),
Anders Carlssondb4022c2010-03-10 02:33:41 +0000204 MostDerivedClassOffset(MostDerivedClassOffset), LayoutClass(LayoutClass),
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000205 Context(MostDerivedClass->getASTContext()),
206 MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)) {
207
208 // Compute the final overriders.
Anders Carlssonb8358d82010-02-12 01:40:03 +0000209 SubobjectOffsetsMapTy Offsets;
Anders Carlssond5c7cca2010-02-17 17:48:25 +0000210 ComputeFinalOverriders(BaseSubobject(MostDerivedClass, 0),
Anders Carlssondb4022c2010-03-10 02:33:41 +0000211 /*BaseSubobjectIsVisitedVBase=*/false,
212 MostDerivedClassOffset, Offsets);
Anders Carlssond5c7cca2010-02-17 17:48:25 +0000213 VisitedVirtualBases.clear();
Anders Carlsson327568e2010-02-27 20:02:53 +0000214
215#if DUMP_OVERRIDERS
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000216 // And dump them (for now).
217 dump();
Anders Carlssona4699882010-02-13 21:33:22 +0000218
219 // Also dump the base offsets (for now).
220 for (SubobjectOffsetsMapTy::const_iterator I = Offsets.begin(),
221 E = Offsets.end(); I != E; ++I) {
Anders Carlssondb4022c2010-03-10 02:33:41 +0000222 const OffsetSetVectorTy& OffsetSetVector = I->second;
Anders Carlssona4699882010-02-13 21:33:22 +0000223
224 llvm::errs() << "Base offsets for ";
225 llvm::errs() << I->first->getQualifiedNameAsString() << '\n';
226
Anders Carlssondb4022c2010-03-10 02:33:41 +0000227 for (unsigned I = 0, E = OffsetSetVector.size(); I != E; ++I)
228 llvm::errs() << " " << I << " - " << OffsetSetVector[I] / 8 << '\n';
Anders Carlssona4699882010-02-13 21:33:22 +0000229 }
Anders Carlsson327568e2010-02-27 20:02:53 +0000230#endif
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000231}
232
Anders Carlssonb8358d82010-02-12 01:40:03 +0000233void FinalOverriders::AddOverriders(BaseSubobject Base,
Anders Carlssondb4022c2010-03-10 02:33:41 +0000234 uint64_t OffsetInLayoutClass,
Anders Carlssonb8358d82010-02-12 01:40:03 +0000235 SubobjectOffsetsMapTy &Offsets) {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000236 const CXXRecordDecl *RD = Base.getBase();
237
238 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
239 E = RD->method_end(); I != E; ++I) {
240 const CXXMethodDecl *MD = *I;
241
242 if (!MD->isVirtual())
243 continue;
244
Anders Carlssonb8358d82010-02-12 01:40:03 +0000245 // First, propagate the overrider.
Anders Carlssondb4022c2010-03-10 02:33:41 +0000246 PropagateOverrider(MD, Base, OffsetInLayoutClass, MD, Offsets);
Anders Carlssonb8358d82010-02-12 01:40:03 +0000247
248 // Add the overrider as the final overrider of itself.
Anders Carlssona7575822010-02-12 16:55:34 +0000249 OverriderInfo& Overrider = OverridersMap[std::make_pair(Base, MD)];
250 assert(!Overrider.Method && "Overrider should not exist yet!");
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000251
Anders Carlssondb4022c2010-03-10 02:33:41 +0000252 Overrider.Offset = OffsetInLayoutClass;
Anders Carlssona7575822010-02-12 16:55:34 +0000253 Overrider.Method = MD;
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000254 }
255}
256
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000257static BaseOffset ComputeBaseOffset(ASTContext &Context,
258 const CXXRecordDecl *DerivedRD,
259 const CXXBasePath &Path) {
Anders Carlssondf31af12010-02-13 23:17:31 +0000260 int64_t NonVirtualOffset = 0;
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000261
Anders Carlssona4a54172010-02-13 20:41:15 +0000262 unsigned NonVirtualStart = 0;
263 const CXXRecordDecl *VirtualBase = 0;
264
265 // First, look for the virtual base class.
266 for (unsigned I = 0, E = Path.size(); I != E; ++I) {
267 const CXXBasePathElement &Element = Path[I];
268
269 if (Element.Base->isVirtual()) {
270 // FIXME: Can we break when we find the first virtual base?
271 // (If we can't, can't we just iterate over the path in reverse order?)
272 NonVirtualStart = I + 1;
273 QualType VBaseType = Element.Base->getType();
274 VirtualBase =
275 cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl());
276 }
277 }
278
279 // Now compute the non-virtual offset.
280 for (unsigned I = NonVirtualStart, E = Path.size(); I != E; ++I) {
281 const CXXBasePathElement &Element = Path[I];
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000282
283 // Check the base class offset.
284 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class);
Anders Carlssonbf554f62010-02-25 22:18:35 +0000285
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000286 const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>();
287 const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl());
Anders Carlssonbf554f62010-02-25 22:18:35 +0000288
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000289 NonVirtualOffset += Layout.getBaseClassOffset(Base);
290 }
291
292 // FIXME: This should probably use CharUnits or something. Maybe we should
293 // even change the base offsets in ASTRecordLayout to be specified in
294 // CharUnits.
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000295 return BaseOffset(DerivedRD, VirtualBase, NonVirtualOffset / 8);
Anders Carlssone67dc302010-02-14 00:16:19 +0000296
297}
298
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000299static BaseOffset ComputeBaseOffset(ASTContext &Context,
300 const CXXRecordDecl *BaseRD,
301 const CXXRecordDecl *DerivedRD) {
Anders Carlssone67dc302010-02-14 00:16:19 +0000302 CXXBasePaths Paths(/*FindAmbiguities=*/false,
303 /*RecordPaths=*/true, /*DetectVirtual=*/false);
304
305 if (!const_cast<CXXRecordDecl *>(DerivedRD)->
306 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) {
307 assert(false && "Class must be derived from the passed in base class!");
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000308 return BaseOffset();
Anders Carlssone67dc302010-02-14 00:16:19 +0000309 }
310
311 return ComputeBaseOffset(Context, DerivedRD, Paths.front());
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000312}
313
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000314static BaseOffset
Anders Carlssondf31af12010-02-13 23:17:31 +0000315ComputeReturnAdjustmentBaseOffset(ASTContext &Context,
316 const CXXMethodDecl *DerivedMD,
317 const CXXMethodDecl *BaseMD) {
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000318 const FunctionType *BaseFT = BaseMD->getType()->getAs<FunctionType>();
319 const FunctionType *DerivedFT = DerivedMD->getType()->getAs<FunctionType>();
320
321 // Canonicalize the return types.
322 CanQualType CanDerivedReturnType =
323 Context.getCanonicalType(DerivedFT->getResultType());
324 CanQualType CanBaseReturnType =
325 Context.getCanonicalType(BaseFT->getResultType());
326
327 assert(CanDerivedReturnType->getTypeClass() ==
328 CanBaseReturnType->getTypeClass() &&
329 "Types must have same type class!");
330
331 if (CanDerivedReturnType == CanBaseReturnType) {
332 // No adjustment needed.
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000333 return BaseOffset();
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000334 }
335
336 if (isa<ReferenceType>(CanDerivedReturnType)) {
337 CanDerivedReturnType =
338 CanDerivedReturnType->getAs<ReferenceType>()->getPointeeType();
339 CanBaseReturnType =
340 CanBaseReturnType->getAs<ReferenceType>()->getPointeeType();
341 } else if (isa<PointerType>(CanDerivedReturnType)) {
342 CanDerivedReturnType =
343 CanDerivedReturnType->getAs<PointerType>()->getPointeeType();
344 CanBaseReturnType =
345 CanBaseReturnType->getAs<PointerType>()->getPointeeType();
346 } else {
347 assert(false && "Unexpected return type!");
348 }
349
350 // We need to compare unqualified types here; consider
351 // const T *Base::foo();
352 // T *Derived::foo();
353 if (CanDerivedReturnType.getUnqualifiedType() ==
354 CanBaseReturnType.getUnqualifiedType()) {
355 // No adjustment needed.
Anders Carlsson2fca9be2010-02-23 01:34:28 +0000356 return BaseOffset();
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000357 }
358
359 const CXXRecordDecl *DerivedRD =
360 cast<CXXRecordDecl>(cast<RecordType>(CanDerivedReturnType)->getDecl());
361
362 const CXXRecordDecl *BaseRD =
363 cast<CXXRecordDecl>(cast<RecordType>(CanBaseReturnType)->getDecl());
364
Anders Carlssondf31af12010-02-13 23:17:31 +0000365 return ComputeBaseOffset(Context, BaseRD, DerivedRD);
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000366}
367
Anders Carlssonb8358d82010-02-12 01:40:03 +0000368void FinalOverriders::PropagateOverrider(const CXXMethodDecl *OldMD,
Anders Carlsson74c364e2010-02-13 22:23:31 +0000369 BaseSubobject NewBase,
Anders Carlssondb4022c2010-03-10 02:33:41 +0000370 uint64_t OverriderOffsetInLayoutClass,
Anders Carlssonb8358d82010-02-12 01:40:03 +0000371 const CXXMethodDecl *NewMD,
372 SubobjectOffsetsMapTy &Offsets) {
373 for (CXXMethodDecl::method_iterator I = OldMD->begin_overridden_methods(),
374 E = OldMD->end_overridden_methods(); I != E; ++I) {
375 const CXXMethodDecl *OverriddenMD = *I;
376 const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent();
377
378 // We want to override OverriddenMD in all subobjects, for example:
379 //
380 /// struct A { virtual void f(); };
381 /// struct B1 : A { };
382 /// struct B2 : A { };
383 /// struct C : B1, B2 { virtual void f(); };
384 ///
385 /// When overriding A::f with C::f we need to do so in both A subobjects.
Anders Carlssonce57dd52010-03-03 04:58:02 +0000386 const OffsetSetVectorTy &OffsetVector = Offsets[OverriddenRD];
Anders Carlssonb8358d82010-02-12 01:40:03 +0000387
388 // Go through all the subobjects.
389 for (unsigned I = 0, E = OffsetVector.size(); I != E; ++I) {
390 uint64_t Offset = OffsetVector[I];
391
Anders Carlssone67dc302010-02-14 00:16:19 +0000392 BaseSubobject OverriddenSubobject = BaseSubobject(OverriddenRD, Offset);
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000393 BaseSubobjectMethodPairTy SubobjectAndMethod =
Anders Carlssone67dc302010-02-14 00:16:19 +0000394 std::make_pair(OverriddenSubobject, OverriddenMD);
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000395
396 OverriderInfo &Overrider = OverridersMap[SubobjectAndMethod];
397
Anders Carlssona7575822010-02-12 16:55:34 +0000398 assert(Overrider.Method && "Did not find existing overrider!");
Anders Carlssonb8358d82010-02-12 01:40:03 +0000399
Anders Carlssondf31af12010-02-13 23:17:31 +0000400 // Check if we need return adjustments or base adjustments.
Anders Carlsson1d05be52010-02-13 21:16:54 +0000401 // (We don't want to do this for pure virtual member functions).
402 if (!NewMD->isPure()) {
Anders Carlssondf31af12010-02-13 23:17:31 +0000403 // Get the return adjustment base offset.
Anders Carlsson1d05be52010-02-13 21:16:54 +0000404 BaseOffset ReturnBaseOffset =
Anders Carlssondf31af12010-02-13 23:17:31 +0000405 ComputeReturnAdjustmentBaseOffset(Context, NewMD, OverriddenMD);
406
Anders Carlsson1d05be52010-02-13 21:16:54 +0000407 if (!ReturnBaseOffset.isEmpty()) {
408 // Store the return adjustment base offset.
409 ReturnAdjustments[SubobjectAndMethod] = ReturnBaseOffset;
410 }
Anders Carlsson16b73122010-02-12 17:13:23 +0000411 }
412
Anders Carlssonb8358d82010-02-12 01:40:03 +0000413 // Set the new overrider.
Anders Carlssondb4022c2010-03-10 02:33:41 +0000414 Overrider.Offset = OverriderOffsetInLayoutClass;
Anders Carlssona7575822010-02-12 16:55:34 +0000415 Overrider.Method = NewMD;
Anders Carlssonb8358d82010-02-12 01:40:03 +0000416
417 // And propagate it further.
Anders Carlssondb4022c2010-03-10 02:33:41 +0000418 PropagateOverrider(OverriddenMD, NewBase, OverriderOffsetInLayoutClass,
419 NewMD, Offsets);
Anders Carlssonb8358d82010-02-12 01:40:03 +0000420 }
421 }
422}
423
424void
425FinalOverriders::MergeSubobjectOffsets(const SubobjectOffsetsMapTy &NewOffsets,
426 SubobjectOffsetsMapTy &Offsets) {
427 // Iterate over the new offsets.
428 for (SubobjectOffsetsMapTy::const_iterator I = NewOffsets.begin(),
429 E = NewOffsets.end(); I != E; ++I) {
430 const CXXRecordDecl *NewRD = I->first;
Anders Carlssonce57dd52010-03-03 04:58:02 +0000431 const OffsetSetVectorTy& NewOffsetVector = I->second;
Anders Carlssonb8358d82010-02-12 01:40:03 +0000432
Anders Carlssonce57dd52010-03-03 04:58:02 +0000433 OffsetSetVectorTy &OffsetVector = Offsets[NewRD];
Anders Carlssonb8358d82010-02-12 01:40:03 +0000434
Anders Carlssonce57dd52010-03-03 04:58:02 +0000435 // Merge the new offsets set vector into the old.
436 OffsetVector.insert(NewOffsetVector.begin(), NewOffsetVector.end());
Anders Carlssonb8358d82010-02-12 01:40:03 +0000437 }
438}
439
440void FinalOverriders::ComputeFinalOverriders(BaseSubobject Base,
Anders Carlssond5c7cca2010-02-17 17:48:25 +0000441 bool BaseSubobjectIsVisitedVBase,
Anders Carlssondb4022c2010-03-10 02:33:41 +0000442 uint64_t OffsetInLayoutClass,
Anders Carlssonb8358d82010-02-12 01:40:03 +0000443 SubobjectOffsetsMapTy &Offsets) {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000444 const CXXRecordDecl *RD = Base.getBase();
Anders Carlssonb8358d82010-02-12 01:40:03 +0000445 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000446
Anders Carlssonb8358d82010-02-12 01:40:03 +0000447 SubobjectOffsetsMapTy NewOffsets;
448
449 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
450 E = RD->bases_end(); I != E; ++I) {
451 const CXXRecordDecl *BaseDecl =
452 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
453
Anders Carlssonb828afa2010-02-14 17:05:59 +0000454 // Ignore bases that don't have any virtual member functions.
455 if (!BaseDecl->isPolymorphic())
456 continue;
457
Anders Carlssonbf554f62010-02-25 22:18:35 +0000458 bool IsVisitedVirtualBase = BaseSubobjectIsVisitedVBase;
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000459 uint64_t BaseOffset;
Anders Carlssondb4022c2010-03-10 02:33:41 +0000460 uint64_t BaseOffsetInLayoutClass;
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000461 if (I->isVirtual()) {
Anders Carlssond5c7cca2010-02-17 17:48:25 +0000462 if (!VisitedVirtualBases.insert(BaseDecl))
463 IsVisitedVirtualBase = true;
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000464 BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
Anders Carlssondb4022c2010-03-10 02:33:41 +0000465
466 const ASTRecordLayout &LayoutClassLayout =
467 Context.getASTRecordLayout(LayoutClass);
468 BaseOffsetInLayoutClass =
469 LayoutClassLayout.getVBaseClassOffset(BaseDecl);
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000470 } else {
471 BaseOffset = Layout.getBaseClassOffset(BaseDecl) + Base.getBaseOffset();
Anders Carlssondb4022c2010-03-10 02:33:41 +0000472 BaseOffsetInLayoutClass = Layout.getBaseClassOffset(BaseDecl) +
473 OffsetInLayoutClass;
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000474 }
475
Anders Carlssonb8358d82010-02-12 01:40:03 +0000476 // Compute the final overriders for this base.
Anders Carlssond5c7cca2010-02-17 17:48:25 +0000477 // We always want to compute the final overriders, even if the base is a
478 // visited virtual base. Consider:
479 //
480 // struct A {
481 // virtual void f();
482 // virtual void g();
483 // };
484 //
485 // struct B : virtual A {
486 // void f();
487 // };
488 //
489 // struct C : virtual A {
490 // void g ();
491 // };
492 //
493 // struct D : B, C { };
494 //
495 // Here, we still want to compute the overriders for A as a base of C,
496 // because otherwise we'll miss that C::g overrides A::f.
497 ComputeFinalOverriders(BaseSubobject(BaseDecl, BaseOffset),
Anders Carlssondb4022c2010-03-10 02:33:41 +0000498 IsVisitedVirtualBase, BaseOffsetInLayoutClass,
499 NewOffsets);
Anders Carlssonb8358d82010-02-12 01:40:03 +0000500 }
501
502 /// Now add the overriders for this particular subobject.
Anders Carlssond5c7cca2010-02-17 17:48:25 +0000503 /// (We don't want to do this more than once for a virtual base).
504 if (!BaseSubobjectIsVisitedVBase)
Anders Carlssondb4022c2010-03-10 02:33:41 +0000505 AddOverriders(Base, OffsetInLayoutClass, NewOffsets);
Anders Carlssonb8358d82010-02-12 01:40:03 +0000506
507 // And merge the newly discovered subobject offsets.
508 MergeSubobjectOffsets(NewOffsets, Offsets);
509
510 /// Finally, add the offset for our own subobject.
Anders Carlssonce57dd52010-03-03 04:58:02 +0000511 Offsets[RD].insert(Base.getBaseOffset());
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000512}
513
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000514void FinalOverriders::dump(llvm::raw_ostream &Out, BaseSubobject Base) {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000515 const CXXRecordDecl *RD = Base.getBase();
516 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
517
518 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
519 E = RD->bases_end(); I != E; ++I) {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000520 const CXXRecordDecl *BaseDecl =
521 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
522
Anders Carlssonb828afa2010-02-14 17:05:59 +0000523 // Ignore bases that don't have any virtual member functions.
524 if (!BaseDecl->isPolymorphic())
525 continue;
526
Anders Carlsson9d6f0d52010-02-14 00:37:35 +0000527 uint64_t BaseOffset;
528 if (I->isVirtual()) {
529 if (!VisitedVirtualBases.insert(BaseDecl)) {
530 // We've visited this base before.
531 continue;
532 }
533
534 BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
535 } else {
536 BaseOffset = Layout.getBaseClassOffset(BaseDecl) +
537 Base.getBaseOffset();
538 }
539
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000540 dump(Out, BaseSubobject(BaseDecl, BaseOffset));
541 }
542
543 Out << "Final overriders for (" << RD->getQualifiedNameAsString() << ", ";
Anders Carlssondb4022c2010-03-10 02:33:41 +0000544 Out << Base.getBaseOffset() / 8 << ")\n";
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000545
546 // Now dump the overriders for this base subobject.
547 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
548 E = RD->method_end(); I != E; ++I) {
549 const CXXMethodDecl *MD = *I;
550
551 if (!MD->isVirtual())
552 continue;
553
Anders Carlssona7575822010-02-12 16:55:34 +0000554 OverriderInfo Overrider = getOverrider(Base, MD);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000555
Anders Carlssond86e5d82010-03-02 03:44:06 +0000556 Out << " " << MD->getQualifiedNameAsString() << " - (";
Anders Carlsson16b73122010-02-12 17:13:23 +0000557 Out << Overrider.Method->getQualifiedNameAsString();
Anders Carlssoncd0d27f2010-03-12 05:02:01 +0000558 Out << ", " << ", " << Overrider.Offset / 8 << ')';
Anders Carlssond86e5d82010-03-02 03:44:06 +0000559
Benjamin Kramere3d0b1c2010-02-13 09:11:28 +0000560 AdjustmentOffsetsMapTy::const_iterator AI =
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000561 ReturnAdjustments.find(std::make_pair(Base, MD));
Benjamin Kramere3d0b1c2010-02-13 09:11:28 +0000562 if (AI != ReturnAdjustments.end()) {
563 const BaseOffset &Offset = AI->second;
Anders Carlssona4a54172010-02-13 20:41:15 +0000564
565 Out << " [ret-adj: ";
566 if (Offset.VirtualBase)
567 Out << Offset.VirtualBase->getQualifiedNameAsString() << " vbase, ";
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000568
Anders Carlssona4a54172010-02-13 20:41:15 +0000569 Out << Offset.NonVirtualOffset << " nv]";
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000570 }
Anders Carlssondf31af12010-02-13 23:17:31 +0000571
Anders Carlsson16b73122010-02-12 17:13:23 +0000572 Out << "\n";
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000573 }
574}
575
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000576/// VtableComponent - Represents a single component in a vtable.
577class VtableComponent {
578public:
579 enum Kind {
580 CK_VCallOffset,
581 CK_VBaseOffset,
582 CK_OffsetToTop,
583 CK_RTTI,
Anders Carlsson848fa642010-02-11 18:20:28 +0000584 CK_FunctionPointer,
585
586 /// CK_CompleteDtorPointer - A pointer to the complete destructor.
587 CK_CompleteDtorPointer,
588
589 /// CK_DeletingDtorPointer - A pointer to the deleting destructor.
Anders Carlsson1f464662010-02-19 20:08:13 +0000590 CK_DeletingDtorPointer,
591
592 /// CK_UnusedFunctionPointer - In some cases, a vtable function pointer
593 /// will end up never being called. Such vtable function pointers are
594 /// represented as a CK_UnusedFunctionPointer.
595 CK_UnusedFunctionPointer
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000596 };
597
Anders Carlssond9a67042010-02-17 06:07:19 +0000598 static VtableComponent MakeVCallOffset(int64_t Offset) {
599 return VtableComponent(CK_VCallOffset, Offset);
600 }
601
Anders Carlssona661a542010-02-16 04:49:44 +0000602 static VtableComponent MakeVBaseOffset(int64_t Offset) {
603 return VtableComponent(CK_VBaseOffset, Offset);
604 }
605
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000606 static VtableComponent MakeOffsetToTop(int64_t Offset) {
607 return VtableComponent(CK_OffsetToTop, Offset);
608 }
609
610 static VtableComponent MakeRTTI(const CXXRecordDecl *RD) {
611 return VtableComponent(CK_RTTI, reinterpret_cast<uintptr_t>(RD));
612 }
613
614 static VtableComponent MakeFunction(const CXXMethodDecl *MD) {
615 assert(!isa<CXXDestructorDecl>(MD) &&
Anders Carlsson848fa642010-02-11 18:20:28 +0000616 "Don't use MakeFunction with destructors!");
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000617
Anders Carlsson848fa642010-02-11 18:20:28 +0000618 return VtableComponent(CK_FunctionPointer,
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000619 reinterpret_cast<uintptr_t>(MD));
620 }
621
Anders Carlsson848fa642010-02-11 18:20:28 +0000622 static VtableComponent MakeCompleteDtor(const CXXDestructorDecl *DD) {
623 return VtableComponent(CK_CompleteDtorPointer,
624 reinterpret_cast<uintptr_t>(DD));
625 }
626
627 static VtableComponent MakeDeletingDtor(const CXXDestructorDecl *DD) {
628 return VtableComponent(CK_DeletingDtorPointer,
629 reinterpret_cast<uintptr_t>(DD));
630 }
631
Anders Carlsson1f464662010-02-19 20:08:13 +0000632 static VtableComponent MakeUnusedFunction(const CXXMethodDecl *MD) {
633 assert(!isa<CXXDestructorDecl>(MD) &&
634 "Don't use MakeUnusedFunction with destructors!");
635 return VtableComponent(CK_UnusedFunctionPointer,
636 reinterpret_cast<uintptr_t>(MD));
637 }
638
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000639 static VtableComponent getFromOpaqueInteger(uint64_t I) {
640 return VtableComponent(I);
641 }
642
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000643 /// getKind - Get the kind of this vtable component.
644 Kind getKind() const {
645 return (Kind)(Value & 0x7);
646 }
647
Anders Carlssond9a67042010-02-17 06:07:19 +0000648 int64_t getVCallOffset() const {
649 assert(getKind() == CK_VCallOffset && "Invalid component kind!");
650
651 return getOffset();
652 }
653
Anders Carlssona661a542010-02-16 04:49:44 +0000654 int64_t getVBaseOffset() const {
655 assert(getKind() == CK_VBaseOffset && "Invalid component kind!");
656
657 return getOffset();
658 }
659
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000660 int64_t getOffsetToTop() const {
661 assert(getKind() == CK_OffsetToTop && "Invalid component kind!");
662
663 return getOffset();
664 }
665
666 const CXXRecordDecl *getRTTIDecl() const {
667 assert(getKind() == CK_RTTI && "Invalid component kind!");
668
669 return reinterpret_cast<CXXRecordDecl *>(getPointer());
670 }
671
672 const CXXMethodDecl *getFunctionDecl() const {
Anders Carlsson848fa642010-02-11 18:20:28 +0000673 assert(getKind() == CK_FunctionPointer);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000674
675 return reinterpret_cast<CXXMethodDecl *>(getPointer());
676 }
Anders Carlsson848fa642010-02-11 18:20:28 +0000677
678 const CXXDestructorDecl *getDestructorDecl() const {
679 assert((getKind() == CK_CompleteDtorPointer ||
680 getKind() == CK_DeletingDtorPointer) && "Invalid component kind!");
681
682 return reinterpret_cast<CXXDestructorDecl *>(getPointer());
683 }
684
Anders Carlsson1f464662010-02-19 20:08:13 +0000685 const CXXMethodDecl *getUnusedFunctionDecl() const {
686 assert(getKind() == CK_UnusedFunctionPointer);
687
688 return reinterpret_cast<CXXMethodDecl *>(getPointer());
689 }
690
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000691private:
692 VtableComponent(Kind ComponentKind, int64_t Offset) {
693 assert((ComponentKind == CK_VCallOffset ||
694 ComponentKind == CK_VBaseOffset ||
695 ComponentKind == CK_OffsetToTop) && "Invalid component kind!");
696 assert(Offset <= ((1LL << 56) - 1) && "Offset is too big!");
697
698 Value = ((Offset << 3) | ComponentKind);
699 }
700
701 VtableComponent(Kind ComponentKind, uintptr_t Ptr) {
702 assert((ComponentKind == CK_RTTI ||
Anders Carlsson848fa642010-02-11 18:20:28 +0000703 ComponentKind == CK_FunctionPointer ||
704 ComponentKind == CK_CompleteDtorPointer ||
Anders Carlsson1f464662010-02-19 20:08:13 +0000705 ComponentKind == CK_DeletingDtorPointer ||
706 ComponentKind == CK_UnusedFunctionPointer) &&
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000707 "Invalid component kind!");
708
709 assert((Ptr & 7) == 0 && "Pointer not sufficiently aligned!");
710
711 Value = Ptr | ComponentKind;
712 }
713
714 int64_t getOffset() const {
715 assert((getKind() == CK_VCallOffset || getKind() == CK_VBaseOffset ||
716 getKind() == CK_OffsetToTop) && "Invalid component kind!");
717
718 return Value >> 3;
719 }
720
721 uintptr_t getPointer() const {
Anders Carlsson848fa642010-02-11 18:20:28 +0000722 assert((getKind() == CK_RTTI ||
723 getKind() == CK_FunctionPointer ||
724 getKind() == CK_CompleteDtorPointer ||
Anders Carlsson1f464662010-02-19 20:08:13 +0000725 getKind() == CK_DeletingDtorPointer ||
726 getKind() == CK_UnusedFunctionPointer) &&
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000727 "Invalid component kind!");
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000728
729 return static_cast<uintptr_t>(Value & ~7ULL);
730 }
731
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +0000732 explicit VtableComponent(uint64_t Value)
733 : Value(Value) { }
734
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000735 /// The kind is stored in the lower 3 bits of the value. For offsets, we
736 /// make use of the facts that classes can't be larger than 2^55 bytes,
737 /// so we store the offset in the lower part of the 61 bytes that remain.
738 /// (The reason that we're not simply using a PointerIntPair here is that we
739 /// need the offsets to be 64-bit, even when on a 32-bit machine).
740 int64_t Value;
741};
742
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000743/// VCallOffsetMap - Keeps track of vcall offsets when building a vtable.
744struct VCallOffsetMap {
745
746 typedef std::pair<const CXXMethodDecl *, int64_t> MethodAndOffsetPairTy;
747
748 /// Offsets - Keeps track of methods and their offsets.
749 // FIXME: This should be a real map and not a vector.
750 llvm::SmallVector<MethodAndOffsetPairTy, 16> Offsets;
751
Anders Carlssoneb577d02010-02-18 17:26:40 +0000752 /// MethodsCanShareVCallOffset - Returns whether two virtual member functions
753 /// can share the same vcall offset.
754 static bool MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
755 const CXXMethodDecl *RHS);
756
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000757public:
758 /// AddVCallOffset - Adds a vcall offset to the map. Returns true if the
759 /// add was successful, or false if there was already a member function with
760 /// the same signature in the map.
761 bool AddVCallOffset(const CXXMethodDecl *MD, int64_t OffsetOffset);
762
763 /// getVCallOffsetOffset - Returns the vcall offset offset (relative to the
764 /// vtable address point) for the given virtual member function.
765 int64_t getVCallOffsetOffset(const CXXMethodDecl *MD);
766
Anders Carlssondba6ef82010-02-25 22:23:13 +0000767 // empty - Return whether the offset map is empty or not.
768 bool empty() const { return Offsets.empty(); }
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000769};
770
John McCall331a03a2010-02-26 20:22:44 +0000771static bool HasSameVirtualSignature(const CXXMethodDecl *LHS,
772 const CXXMethodDecl *RHS) {
773 ASTContext &C = LHS->getASTContext(); // TODO: thread this down
774 CanQual<FunctionProtoType>
775 LT = C.getCanonicalType(LHS->getType()).getAs<FunctionProtoType>(),
776 RT = C.getCanonicalType(RHS->getType()).getAs<FunctionProtoType>();
777
778 // Fast-path matches in the canonical types.
779 if (LT == RT) return true;
780
781 // Force the signatures to match. We can't rely on the overrides
782 // list here because there isn't necessarily an inheritance
783 // relationship between the two methods.
784 if (LT.getQualifiers() != RT.getQualifiers() ||
785 LT->getNumArgs() != RT->getNumArgs())
786 return false;
787 for (unsigned I = 0, E = LT->getNumArgs(); I != E; ++I)
788 if (LT->getArgType(I) != RT->getArgType(I))
789 return false;
790 return true;
791}
792
Anders Carlssoneb577d02010-02-18 17:26:40 +0000793bool VCallOffsetMap::MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
794 const CXXMethodDecl *RHS) {
795 assert(LHS->isVirtual() && "LHS must be virtual!");
796 assert(RHS->isVirtual() && "LHS must be virtual!");
797
Anders Carlsson55609692010-02-27 04:12:52 +0000798 // A destructor can share a vcall offset with another destructor.
799 if (isa<CXXDestructorDecl>(LHS))
800 return isa<CXXDestructorDecl>(RHS);
801
Anders Carlssoneb577d02010-02-18 17:26:40 +0000802 // FIXME: We need to check more things here.
803
Anders Carlsson55609692010-02-27 04:12:52 +0000804 // The methods must have the same name.
Anders Carlssoneb577d02010-02-18 17:26:40 +0000805 DeclarationName LHSName = LHS->getDeclName();
806 DeclarationName RHSName = RHS->getDeclName();
John McCall331a03a2010-02-26 20:22:44 +0000807 if (LHSName != RHSName)
Anders Carlssoneb577d02010-02-18 17:26:40 +0000808 return false;
John McCall331a03a2010-02-26 20:22:44 +0000809
Anders Carlsson55609692010-02-27 04:12:52 +0000810 // And the same signatures.
811 return HasSameVirtualSignature(LHS, RHS);
Anders Carlssoneb577d02010-02-18 17:26:40 +0000812}
813
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000814bool VCallOffsetMap::AddVCallOffset(const CXXMethodDecl *MD,
815 int64_t OffsetOffset) {
Anders Carlssoneb577d02010-02-18 17:26:40 +0000816 // Check if we can reuse an offset.
817 for (unsigned I = 0, E = Offsets.size(); I != E; ++I) {
818 if (MethodsCanShareVCallOffset(Offsets[I].first, MD))
819 return false;
820 }
821
822 // Add the offset.
823 Offsets.push_back(MethodAndOffsetPairTy(MD, OffsetOffset));
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000824 return true;
825}
826
827int64_t VCallOffsetMap::getVCallOffsetOffset(const CXXMethodDecl *MD) {
Anders Carlssoneb577d02010-02-18 17:26:40 +0000828 // Look for an offset.
829 for (unsigned I = 0, E = Offsets.size(); I != E; ++I) {
830 if (MethodsCanShareVCallOffset(Offsets[I].first, MD))
831 return Offsets[I].second;
832 }
833
834 assert(false && "Should always find a vcall offset offset!");
Anders Carlsson60cc72a2010-02-18 17:00:09 +0000835 return 0;
836}
837
Anders Carlssond68c0832010-02-25 03:45:56 +0000838/// VCallAndVBaseOffsetBuilder - Class for building vcall and vbase offsets.
839class VCallAndVBaseOffsetBuilder {
Anders Carlssonc89a7cf2010-03-11 16:06:20 +0000840public:
841 typedef llvm::DenseMap<const CXXRecordDecl *, int64_t>
842 VBaseOffsetOffsetsMapTy;
843
844private:
Anders Carlsson530c40c2010-02-28 01:43:58 +0000845 /// MostDerivedClass - The most derived class for which we're building vcall
846 /// and vbase offsets.
Anders Carlssond68c0832010-02-25 03:45:56 +0000847 const CXXRecordDecl *MostDerivedClass;
848
Anders Carlsson0041a642010-02-27 21:09:00 +0000849 /// LayoutClass - The class we're using for layout information. Will be
850 /// different than the most derived class if we're building a construction
851 /// vtable.
852 const CXXRecordDecl *LayoutClass;
853
Anders Carlssond68c0832010-02-25 03:45:56 +0000854 /// Context - The ASTContext which we will use for layout information.
855 ASTContext &Context;
856
857 /// Components - vcall and vbase offset components
858 typedef llvm::SmallVector<VtableComponent, 64> VtableComponentVectorTy;
859 VtableComponentVectorTy Components;
860
861 /// VisitedVirtualBases - Visited virtual bases.
862 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
863
864 /// VCallOffsets - Keeps track of vcall offsets.
865 VCallOffsetMap VCallOffsets;
866
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000867
868 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets,
869 /// relative to the address point.
870 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
871
Anders Carlssond68c0832010-02-25 03:45:56 +0000872 /// FinalOverriders - The final overriders of the most derived class.
873 /// (Can be null when we're not building a vtable of the most derived class).
874 const FinalOverriders *Overriders;
875
876 /// AddVCallAndVBaseOffsets - Add vcall offsets and vbase offsets for the
877 /// given base subobject.
878 void AddVCallAndVBaseOffsets(BaseSubobject Base, bool BaseIsVirtual,
879 uint64_t RealBaseOffset);
880
881 /// AddVCallOffsets - Add vcall offsets for the given base subobject.
882 void AddVCallOffsets(BaseSubobject Base, uint64_t VBaseOffset);
883
884 /// AddVBaseOffsets - Add vbase offsets for the given class.
Anders Carlsson88800662010-03-02 05:40:45 +0000885 void AddVBaseOffsets(const CXXRecordDecl *Base, uint64_t OffsetInLayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +0000886
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000887 /// getCurrentOffsetOffset - Get the current vcall or vbase offset offset in
888 /// bytes, relative to the vtable address point.
889 int64_t getCurrentOffsetOffset() const;
890
Anders Carlssond68c0832010-02-25 03:45:56 +0000891public:
892 VCallAndVBaseOffsetBuilder(const CXXRecordDecl *MostDerivedClass,
Anders Carlsson0041a642010-02-27 21:09:00 +0000893 const CXXRecordDecl *LayoutClass,
Anders Carlssond68c0832010-02-25 03:45:56 +0000894 const FinalOverriders *Overriders,
Anders Carlsson530c40c2010-02-28 01:43:58 +0000895 BaseSubobject Base, bool BaseIsVirtual,
896 uint64_t OffsetInLayoutClass)
897 : MostDerivedClass(MostDerivedClass), LayoutClass(LayoutClass),
Anders Carlssond68c0832010-02-25 03:45:56 +0000898 Context(MostDerivedClass->getASTContext()), Overriders(Overriders) {
899
900 // Add vcall and vbase offsets.
Anders Carlsson530c40c2010-02-28 01:43:58 +0000901 AddVCallAndVBaseOffsets(Base, BaseIsVirtual, OffsetInLayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +0000902 }
903
904 /// Methods for iterating over the components.
905 typedef VtableComponentVectorTy::const_reverse_iterator const_iterator;
906 const_iterator components_begin() const { return Components.rbegin(); }
907 const_iterator components_end() const { return Components.rend(); }
908
Anders Carlssonc89a7cf2010-03-11 16:06:20 +0000909 const VCallOffsetMap &getVCallOffsets() const { return VCallOffsets; }
910 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000911 return VBaseOffsetOffsets;
912 }
Anders Carlssond68c0832010-02-25 03:45:56 +0000913};
914
915void
916VCallAndVBaseOffsetBuilder::AddVCallAndVBaseOffsets(BaseSubobject Base,
917 bool BaseIsVirtual,
918 uint64_t RealBaseOffset) {
919 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base.getBase());
920
921 // Itanium C++ ABI 2.5.2:
922 // ..in classes sharing a virtual table with a primary base class, the vcall
923 // and vbase offsets added by the derived class all come before the vcall
924 // and vbase offsets required by the base class, so that the latter may be
925 // laid out as required by the base class without regard to additions from
926 // the derived class(es).
927
928 // (Since we're emitting the vcall and vbase offsets in reverse order, we'll
929 // emit them for the primary base first).
930 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
931 bool PrimaryBaseIsVirtual = Layout.getPrimaryBaseWasVirtual();
932
933 uint64_t PrimaryBaseOffset;
934
935 // Get the base offset of the primary base.
936 if (PrimaryBaseIsVirtual) {
937 assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 &&
938 "Primary vbase should have a zero offset!");
939
940 const ASTRecordLayout &MostDerivedClassLayout =
941 Context.getASTRecordLayout(MostDerivedClass);
942
943 PrimaryBaseOffset =
944 MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
945 } else {
946 assert(Layout.getBaseClassOffset(PrimaryBase) == 0 &&
947 "Primary base should have a zero offset!");
948
949 PrimaryBaseOffset = Base.getBaseOffset();
950 }
951
952 AddVCallAndVBaseOffsets(BaseSubobject(PrimaryBase, PrimaryBaseOffset),
953 PrimaryBaseIsVirtual, RealBaseOffset);
954 }
955
Anders Carlsson88800662010-03-02 05:40:45 +0000956 AddVBaseOffsets(Base.getBase(), RealBaseOffset);
Anders Carlssond68c0832010-02-25 03:45:56 +0000957
958 // We only want to add vcall offsets for virtual bases.
959 if (BaseIsVirtual)
960 AddVCallOffsets(Base, RealBaseOffset);
961}
962
Anders Carlssonbb6305b2010-03-11 06:43:12 +0000963int64_t VCallAndVBaseOffsetBuilder::getCurrentOffsetOffset() const {
964 // OffsetIndex is the index of this vcall or vbase offset, relative to the
965 // vtable address point. (We subtract 3 to account for the information just
966 // above the address point, the RTTI info, the offset to top, and the
967 // vcall offset itself).
968 int64_t OffsetIndex = -(int64_t)(3 + Components.size());
969
970 // FIXME: We shouldn't use / 8 here.
971 int64_t OffsetOffset = OffsetIndex *
972 (int64_t)Context.Target.getPointerWidth(0) / 8;
973
974 return OffsetOffset;
975}
976
Anders Carlssond68c0832010-02-25 03:45:56 +0000977void VCallAndVBaseOffsetBuilder::AddVCallOffsets(BaseSubobject Base,
978 uint64_t VBaseOffset) {
979 const CXXRecordDecl *RD = Base.getBase();
980 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
981
John McCall331a03a2010-02-26 20:22:44 +0000982 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
983
Anders Carlssond68c0832010-02-25 03:45:56 +0000984 // Handle the primary base first.
John McCall331a03a2010-02-26 20:22:44 +0000985 if (PrimaryBase) {
Anders Carlssond68c0832010-02-25 03:45:56 +0000986 uint64_t PrimaryBaseOffset;
987
988 // Get the base offset of the primary base.
989 if (Layout.getPrimaryBaseWasVirtual()) {
990 assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 &&
991 "Primary vbase should have a zero offset!");
992
993 const ASTRecordLayout &MostDerivedClassLayout =
994 Context.getASTRecordLayout(MostDerivedClass);
995
996 PrimaryBaseOffset =
997 MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
998 } else {
999 assert(Layout.getBaseClassOffset(PrimaryBase) == 0 &&
1000 "Primary base should have a zero offset!");
1001
1002 PrimaryBaseOffset = Base.getBaseOffset();
1003 }
1004
1005 AddVCallOffsets(BaseSubobject(PrimaryBase, PrimaryBaseOffset),
1006 VBaseOffset);
1007 }
1008
1009 // Add the vcall offsets.
1010 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
1011 E = RD->method_end(); I != E; ++I) {
1012 const CXXMethodDecl *MD = *I;
1013
1014 if (!MD->isVirtual())
1015 continue;
1016
Anders Carlssonbb6305b2010-03-11 06:43:12 +00001017 int64_t OffsetOffset = getCurrentOffsetOffset();
Anders Carlssond68c0832010-02-25 03:45:56 +00001018
1019 // Don't add a vcall offset if we already have one for this member function
1020 // signature.
1021 if (!VCallOffsets.AddVCallOffset(MD, OffsetOffset))
1022 continue;
1023
1024 int64_t Offset = 0;
1025
1026 if (Overriders) {
1027 // Get the final overrider.
1028 FinalOverriders::OverriderInfo Overrider =
1029 Overriders->getOverrider(Base, MD);
1030
Anders Carlsson438bc422010-02-28 00:10:58 +00001031 /// The vcall offset is the offset from the virtual base to the object
1032 /// where the function was overridden.
Anders Carlssond68c0832010-02-25 03:45:56 +00001033 // FIXME: We should not use / 8 here.
Anders Carlssond86e5d82010-03-02 03:44:06 +00001034 Offset = (int64_t)(Overrider.Offset - VBaseOffset) / 8;
Anders Carlssond68c0832010-02-25 03:45:56 +00001035 }
1036
1037 Components.push_back(VtableComponent::MakeVCallOffset(Offset));
1038 }
1039
1040 // And iterate over all non-virtual bases (ignoring the primary base).
1041 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1042 E = RD->bases_end(); I != E; ++I) {
1043
1044 if (I->isVirtual())
1045 continue;
1046
1047 const CXXRecordDecl *BaseDecl =
1048 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
John McCall331a03a2010-02-26 20:22:44 +00001049 if (BaseDecl == PrimaryBase)
1050 continue;
Anders Carlssond68c0832010-02-25 03:45:56 +00001051
1052 // Get the base offset of this base.
1053 uint64_t BaseOffset = Base.getBaseOffset() +
1054 Layout.getBaseClassOffset(BaseDecl);
1055
1056 AddVCallOffsets(BaseSubobject(BaseDecl, BaseOffset), VBaseOffset);
1057 }
1058}
1059
Anders Carlssond68c0832010-02-25 03:45:56 +00001060void VCallAndVBaseOffsetBuilder::AddVBaseOffsets(const CXXRecordDecl *RD,
Anders Carlsson88800662010-03-02 05:40:45 +00001061 uint64_t OffsetInLayoutClass) {
Anders Carlsson0041a642010-02-27 21:09:00 +00001062 const ASTRecordLayout &LayoutClassLayout =
1063 Context.getASTRecordLayout(LayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +00001064
1065 // Add vbase offsets.
1066 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1067 E = RD->bases_end(); I != E; ++I) {
1068 const CXXRecordDecl *BaseDecl =
1069 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1070
1071 // Check if this is a virtual base that we haven't visited before.
1072 if (I->isVirtual() && VisitedVirtualBases.insert(BaseDecl)) {
1073 // FIXME: We shouldn't use / 8 here.
Anders Carlsson88800662010-03-02 05:40:45 +00001074 int64_t Offset =
1075 (int64_t)(LayoutClassLayout.getVBaseClassOffset(BaseDecl) -
1076 OffsetInLayoutClass) / 8;
Anders Carlssonbb6305b2010-03-11 06:43:12 +00001077
1078 // Add the vbase offset offset.
1079 assert(!VBaseOffsetOffsets.count(BaseDecl) &&
1080 "vbase offset offset already exists!");
1081
1082 int64_t VBaseOffsetOffset = getCurrentOffsetOffset();
1083 VBaseOffsetOffsets.insert(std::make_pair(BaseDecl, VBaseOffsetOffset));
1084
Anders Carlssond68c0832010-02-25 03:45:56 +00001085 Components.push_back(VtableComponent::MakeVBaseOffset(Offset));
1086 }
Anders Carlssonbb6305b2010-03-11 06:43:12 +00001087
Anders Carlssond68c0832010-02-25 03:45:56 +00001088 // Check the base class looking for more vbase offsets.
Anders Carlsson88800662010-03-02 05:40:45 +00001089 AddVBaseOffsets(BaseDecl, OffsetInLayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +00001090 }
1091}
1092
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001093/// VtableBuilder - Class for building vtable layout information.
Benjamin Kramer85b45212009-11-28 19:45:26 +00001094class VtableBuilder {
Anders Carlsson57071e22010-02-12 05:25:12 +00001095public:
Anders Carlssonb9021e92010-02-27 16:18:19 +00001096 /// PrimaryBasesSetVectorTy - A set vector of direct and indirect
1097 /// primary bases.
1098 typedef llvm::SmallSetVector<const CXXRecordDecl *, 8>
1099 PrimaryBasesSetVectorTy;
1100
Anders Carlssonccd83d72010-03-24 16:42:11 +00001101 typedef llvm::DenseMap<const CXXRecordDecl *, int64_t>
1102 VBaseOffsetOffsetsMapTy;
Anders Carlsson66d567d2010-03-25 00:51:13 +00001103
1104 typedef llvm::DenseMap<BaseSubobject, uint64_t>
1105 AddressPointsMapTy;
Anders Carlssonccd83d72010-03-24 16:42:11 +00001106
Anders Carlsson57071e22010-02-12 05:25:12 +00001107private:
Anders Carlssonaf440352010-03-23 04:11:45 +00001108 /// VTables - Global vtable information.
1109 CodeGenVTables &VTables;
Anders Carlsson60db0ee2010-02-13 21:07:32 +00001110
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001111 /// MostDerivedClass - The most derived class for which we're building this
1112 /// vtable.
1113 const CXXRecordDecl *MostDerivedClass;
1114
Anders Carlssond03a3262010-02-27 20:39:05 +00001115 /// MostDerivedClassOffset - If we're building a construction vtable, this
1116 /// holds the offset from the layout class to the most derived class.
1117 const uint64_t MostDerivedClassOffset;
1118
Anders Carlsson5d7af6b2010-02-28 00:36:23 +00001119 /// MostDerivedClassIsVirtual - Whether the most derived class is a virtual
1120 /// base. (This only makes sense when building a construction vtable).
1121 bool MostDerivedClassIsVirtual;
1122
Anders Carlssond03a3262010-02-27 20:39:05 +00001123 /// LayoutClass - The class we're using for layout information. Will be
1124 /// different than the most derived class if we're building a construction
1125 /// vtable.
1126 const CXXRecordDecl *LayoutClass;
1127
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001128 /// Context - The ASTContext which we will use for layout information.
Anders Carlsson57071e22010-02-12 05:25:12 +00001129 ASTContext &Context;
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001130
Anders Carlssonbdf73d82010-02-11 21:24:32 +00001131 /// FinalOverriders - The final overriders of the most derived class.
Anders Carlssond68c0832010-02-25 03:45:56 +00001132 const FinalOverriders Overriders;
Anders Carlssonbdf73d82010-02-11 21:24:32 +00001133
Anders Carlssondba6ef82010-02-25 22:23:13 +00001134 /// VCallOffsetsForVBases - Keeps track of vcall offsets for the virtual
1135 /// bases in this vtable.
1136 llvm::DenseMap<const CXXRecordDecl *, VCallOffsetMap> VCallOffsetsForVBases;
Anders Carlsson60cc72a2010-02-18 17:00:09 +00001137
Anders Carlssonbb6305b2010-03-11 06:43:12 +00001138 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets for
1139 /// the most derived class.
1140 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
1141
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001142 /// Components - The components of the vtable being built.
1143 llvm::SmallVector<VtableComponent, 64> Components;
1144
Anders Carlsson822307b2010-02-11 17:18:51 +00001145 /// AddressPoints - Address points for the vtable being built.
Anders Carlssonff143f82010-03-25 00:35:49 +00001146 AddressPointsMapTy AddressPoints;
Anders Carlsson822307b2010-02-11 17:18:51 +00001147
Anders Carlssonb9021e92010-02-27 16:18:19 +00001148 /// MethodInfo - Contains information about a method in a vtable.
1149 /// (Used for computing 'this' pointer adjustment thunks.
1150 struct MethodInfo {
1151 /// BaseOffset - The base offset of this method.
1152 const uint64_t BaseOffset;
1153
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001154 /// BaseOffsetInLayoutClass - The base offset in the layout class of this
1155 /// method.
1156 const uint64_t BaseOffsetInLayoutClass;
1157
Anders Carlssonb9021e92010-02-27 16:18:19 +00001158 /// VtableIndex - The index in the vtable that this method has.
1159 /// (For destructors, this is the index of the complete destructor).
1160 const uint64_t VtableIndex;
1161
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001162 MethodInfo(uint64_t BaseOffset, uint64_t BaseOffsetInLayoutClass,
1163 uint64_t VtableIndex)
1164 : BaseOffset(BaseOffset),
1165 BaseOffsetInLayoutClass(BaseOffsetInLayoutClass),
1166 VtableIndex(VtableIndex) { }
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001167
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001168 MethodInfo() : BaseOffset(0), BaseOffsetInLayoutClass(0), VtableIndex(0) { }
Anders Carlssonb9021e92010-02-27 16:18:19 +00001169 };
1170
1171 typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy;
1172
1173 /// MethodInfoMap - The information for all methods in the vtable we're
1174 /// currently building.
1175 MethodInfoMapTy MethodInfoMap;
1176
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00001177 typedef llvm::DenseMap<uint64_t, ThunkInfo> VtableThunksMapTy;
Anders Carlssondd366812010-03-12 04:54:20 +00001178
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001179 /// VTableThunks - The thunks by vtable index in the vtable currently being
1180 /// built.
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00001181 VtableThunksMapTy VTableThunks;
Anders Carlssonb9021e92010-02-27 16:18:19 +00001182
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00001183 typedef llvm::SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
1184 typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
Anders Carlsson5e454aa2010-03-17 20:06:32 +00001185
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001186 /// Thunks - A map that contains all the thunks needed for all methods in the
1187 /// most derived class for which the vtable is currently being built.
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00001188 ThunksMapTy Thunks;
Anders Carlsson5e454aa2010-03-17 20:06:32 +00001189
1190 /// AddThunk - Add a thunk for the given method.
Anders Carlsson9d734f32010-03-21 20:27:14 +00001191 void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk);
Anders Carlsson5e454aa2010-03-17 20:06:32 +00001192
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001193 /// ComputeThisAdjustments - Compute the 'this' pointer adjustments for the
1194 /// part of the vtable we're currently building.
1195 void ComputeThisAdjustments();
1196
Anders Carlssona661a542010-02-16 04:49:44 +00001197 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
1198
Anders Carlssonb9021e92010-02-27 16:18:19 +00001199 /// PrimaryVirtualBases - All known virtual bases who are a primary base of
Anders Carlssonaf280c02010-02-23 03:48:14 +00001200 /// some other base.
1201 VisitedVirtualBasesSetTy PrimaryVirtualBases;
1202
Anders Carlsson60db0ee2010-02-13 21:07:32 +00001203 /// ComputeReturnAdjustment - Compute the return adjustment given a return
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001204 /// adjustment base offset.
Anders Carlsson2fca9be2010-02-23 01:34:28 +00001205 ReturnAdjustment ComputeReturnAdjustment(BaseOffset Offset);
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001206
Anders Carlsson379017f2010-02-27 19:57:44 +00001207 /// ComputeThisAdjustmentBaseOffset - Compute the base offset for adjusting
1208 /// the 'this' pointer from the base subobject to the derived subobject.
1209 BaseOffset ComputeThisAdjustmentBaseOffset(BaseSubobject Base,
1210 BaseSubobject Derived) const;
1211
Anders Carlsson60cc72a2010-02-18 17:00:09 +00001212 /// ComputeThisAdjustment - Compute the 'this' pointer adjustment for the
Anders Carlsson617def32010-03-12 05:28:07 +00001213 /// given virtual member function, its offset in the layout class and its
1214 /// final overrider.
1215 ThisAdjustment
1216 ComputeThisAdjustment(const CXXMethodDecl *MD,
1217 uint64_t BaseOffsetInLayoutClass,
1218 FinalOverriders::OverriderInfo Overrider);
1219
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001220 /// AddMethod - Add a single virtual member function to the vtable
1221 /// components vector.
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001222 void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment);
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001223
Anders Carlsson464c8f12010-02-23 04:26:39 +00001224 /// IsOverriderUsed - Returns whether the overrider will ever be used in this
1225 /// part of the vtable.
1226 ///
1227 /// Itanium C++ ABI 2.5.2:
1228 ///
1229 /// struct A { virtual void f(); };
1230 /// struct B : virtual public A { int i; };
1231 /// struct C : virtual public A { int j; };
1232 /// struct D : public B, public C {};
1233 ///
1234 /// When B and C are declared, A is a primary base in each case, so although
1235 /// vcall offsets are allocated in the A-in-B and A-in-C vtables, no this
1236 /// adjustment is required and no thunk is generated. However, inside D
1237 /// objects, A is no longer a primary base of C, so if we allowed calls to
1238 /// C::f() to use the copy of A's vtable in the C subobject, we would need
1239 /// to adjust this from C* to B::A*, which would require a third-party
1240 /// thunk. Since we require that a call to C::f() first convert to A*,
1241 /// C-in-D's copy of A's vtable is never referenced, so this is not
1242 /// necessary.
Anders Carlsson60396612010-03-10 06:51:42 +00001243 bool IsOverriderUsed(const CXXMethodDecl *Overrider,
1244 uint64_t BaseOffsetInLayoutClass,
1245 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
1246 uint64_t FirstBaseOffsetInLayoutClass) const;
1247
Anders Carlsson464c8f12010-02-23 04:26:39 +00001248
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001249 /// AddMethods - Add the methods of this base subobject and all its
1250 /// primary bases to the vtable components vector.
Anders Carlsson60396612010-03-10 06:51:42 +00001251 void AddMethods(BaseSubobject Base, uint64_t BaseOffsetInLayoutClass,
1252 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
1253 uint64_t FirstBaseOffsetInLayoutClass,
Anders Carlssonb9021e92010-02-27 16:18:19 +00001254 PrimaryBasesSetVectorTy &PrimaryBases);
Anders Carlsson89ad4212010-02-16 16:26:28 +00001255
Anders Carlsson5d7af6b2010-02-28 00:36:23 +00001256 // LayoutVtable - Layout the vtable for the given base class, including its
Anders Carlsson89ad4212010-02-16 16:26:28 +00001257 // secondary vtables and any vtables for virtual bases.
1258 void LayoutVtable();
1259
Anders Carlsson530c40c2010-02-28 01:43:58 +00001260 /// LayoutPrimaryAndSecondaryVtables - Layout the primary vtable for the
Anders Carlsson89ad4212010-02-16 16:26:28 +00001261 /// given base subobject, as well as all its secondary vtables.
Anders Carlsson530c40c2010-02-28 01:43:58 +00001262 void LayoutPrimaryAndSecondaryVtables(BaseSubobject Base,
1263 bool BaseIsVirtual,
1264 uint64_t OffsetInLayoutClass);
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001265
Anders Carlssonc7b63162010-02-16 16:02:57 +00001266 /// LayoutSecondaryVtables - Layout the secondary vtables for the given base
1267 /// subobject.
Anders Carlssona96a2e92010-03-10 19:39:11 +00001268 ///
1269 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
1270 /// or a direct or indirect base of a virtual base.
1271 void LayoutSecondaryVtables(BaseSubobject Base, bool BaseIsMorallyVirtual,
1272 uint64_t OffsetInLayoutClass);
Anders Carlsson08c26752010-02-27 04:05:52 +00001273
1274 /// DeterminePrimaryVirtualBases - Determine the primary virtual bases in this
1275 /// class hierarchy.
1276 void DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
Anders Carlssond7fdae52010-03-10 03:02:01 +00001277 uint64_t OffsetInLayoutClass,
Anders Carlsson08c26752010-02-27 04:05:52 +00001278 VisitedVirtualBasesSetTy &VBases);
1279
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001280 /// LayoutVtablesForVirtualBases - Layout vtables for all virtual bases of the
1281 /// given base (excluding any primary bases).
1282 void LayoutVtablesForVirtualBases(const CXXRecordDecl *RD,
1283 VisitedVirtualBasesSetTy &VBases);
1284
Anders Carlsson438bc422010-02-28 00:10:58 +00001285 /// isBuildingConstructionVtable - Return whether this vtable builder is
1286 /// building a construction vtable.
1287 bool isBuildingConstructorVtable() const {
1288 return MostDerivedClass != LayoutClass;
1289 }
1290
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001291public:
Anders Carlssonaf440352010-03-23 04:11:45 +00001292 VtableBuilder(CodeGenVTables &VTables, const CXXRecordDecl *MostDerivedClass,
Anders Carlsson5d7af6b2010-02-28 00:36:23 +00001293 uint64_t MostDerivedClassOffset, bool MostDerivedClassIsVirtual,
Anders Carlssond03a3262010-02-27 20:39:05 +00001294 const CXXRecordDecl *LayoutClass)
Anders Carlssonaf440352010-03-23 04:11:45 +00001295 : VTables(VTables), MostDerivedClass(MostDerivedClass),
Anders Carlsson5d7af6b2010-02-28 00:36:23 +00001296 MostDerivedClassOffset(MostDerivedClassOffset),
1297 MostDerivedClassIsVirtual(MostDerivedClassIsVirtual),
1298 LayoutClass(LayoutClass), Context(MostDerivedClass->getASTContext()),
Anders Carlssondb4022c2010-03-10 02:33:41 +00001299 Overriders(MostDerivedClass, MostDerivedClassOffset, LayoutClass) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001300
Anders Carlsson89ad4212010-02-16 16:26:28 +00001301 LayoutVtable();
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001302 }
1303
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00001304 ThunksMapTy::const_iterator thunks_begin() const {
1305 return Thunks.begin();
1306 }
1307
1308 ThunksMapTy::const_iterator thunks_end() const {
1309 return Thunks.end();
1310 }
1311
Anders Carlssonccd83d72010-03-24 16:42:11 +00001312 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
1313 return VBaseOffsetOffsets;
1314 }
1315
1316 /// getNumVTableComponents - Return the number of components in the vtable
1317 /// currently built.
1318 uint64_t getNumVTableComponents() const {
1319 return Components.size();
1320 }
1321
Anders Carlsson66d567d2010-03-25 00:51:13 +00001322 const uint64_t *vtable_components_data_begin() const {
Anders Carlssonccd83d72010-03-24 16:42:11 +00001323 return reinterpret_cast<const uint64_t *>(Components.begin());
1324 }
1325
Anders Carlsson66d567d2010-03-25 00:51:13 +00001326 const uint64_t *vtable_components_data_end() const {
Anders Carlssonccd83d72010-03-24 16:42:11 +00001327 return reinterpret_cast<const uint64_t *>(Components.end());
1328 }
1329
Anders Carlsson66d567d2010-03-25 00:51:13 +00001330 AddressPointsMapTy::const_iterator address_points_begin() const {
1331 return AddressPoints.begin();
1332 }
1333
1334 AddressPointsMapTy::const_iterator address_points_end() const {
1335 return AddressPoints.end();
1336 }
1337
Anders Carlsson0d1407e2010-03-25 15:26:28 +00001338 VtableThunksMapTy::const_iterator vtable_thunks_begin() const {
1339 return VTableThunks.begin();
1340 }
1341
1342 VtableThunksMapTy::const_iterator vtable_thunks_end() const {
1343 return VTableThunks.end();
1344 }
1345
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001346 /// dumpLayout - Dump the vtable layout.
1347 void dumpLayout(llvm::raw_ostream&);
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001348};
1349
Anders Carlsson9d734f32010-03-21 20:27:14 +00001350void VtableBuilder::AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk) {
Anders Carlssonbca5d372010-03-22 20:06:40 +00001351 assert(!isBuildingConstructorVtable() &&
1352 "Can't add thunks for construction vtable");
Anders Carlsson9135a842010-03-18 02:44:19 +00001353
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001354 llvm::SmallVector<ThunkInfo, 1> &ThunksVector = Thunks[MD];
Anders Carlsson5e454aa2010-03-17 20:06:32 +00001355
1356 // Check if we have this thunk already.
1357 if (std::find(ThunksVector.begin(), ThunksVector.end(), Thunk) !=
1358 ThunksVector.end())
1359 return;
1360
1361 ThunksVector.push_back(Thunk);
1362}
1363
Anders Carlssonc784ba22010-02-27 06:38:03 +00001364/// OverridesMethodInBases - Checks whether whether this virtual member
1365/// function overrides a member function in any of the given bases.
Anders Carlsson57071e22010-02-12 05:25:12 +00001366/// Returns the overridden member function, or null if none was found.
1367static const CXXMethodDecl *
Anders Carlssonc784ba22010-02-27 06:38:03 +00001368OverridesMethodInBases(const CXXMethodDecl *MD,
Anders Carlssonb9021e92010-02-27 16:18:19 +00001369 VtableBuilder::PrimaryBasesSetVectorTy &Bases) {
Anders Carlsson57071e22010-02-12 05:25:12 +00001370 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
1371 E = MD->end_overridden_methods(); I != E; ++I) {
1372 const CXXMethodDecl *OverriddenMD = *I;
1373 const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent();
1374 assert(OverriddenMD->isCanonicalDecl() &&
1375 "Should have the canonical decl of the overridden RD!");
1376
Anders Carlssonc784ba22010-02-27 06:38:03 +00001377 if (Bases.count(OverriddenRD))
Anders Carlsson57071e22010-02-12 05:25:12 +00001378 return OverriddenMD;
1379 }
1380
1381 return 0;
1382}
1383
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001384void VtableBuilder::ComputeThisAdjustments() {
1385 // Now go through the method info map and see if any of the methods need
1386 // 'this' pointer adjustments.
1387 for (MethodInfoMapTy::const_iterator I = MethodInfoMap.begin(),
1388 E = MethodInfoMap.end(); I != E; ++I) {
1389 const CXXMethodDecl *MD = I->first;
1390 const MethodInfo &MethodInfo = I->second;
1391
Anders Carlsson617def32010-03-12 05:28:07 +00001392 // Ignore adjustments for unused function pointers.
1393 uint64_t VtableIndex = MethodInfo.VtableIndex;
1394 if (Components[VtableIndex].getKind() ==
1395 VtableComponent::CK_UnusedFunctionPointer)
1396 continue;
1397
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001398 // Get the final overrider for this method.
1399 FinalOverriders::OverriderInfo Overrider =
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001400 Overriders.getOverrider(BaseSubobject(MD->getParent(),
1401 MethodInfo.BaseOffset), MD);
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001402
Anders Carlssonbca5d372010-03-22 20:06:40 +00001403 // Check if we need an adjustment at all.
1404 if (MethodInfo.BaseOffsetInLayoutClass == Overrider.Offset)
1405 continue;
1406
Anders Carlsson617def32010-03-12 05:28:07 +00001407 ThisAdjustment ThisAdjustment =
1408 ComputeThisAdjustment(MD, MethodInfo.BaseOffsetInLayoutClass, Overrider);
1409
1410 if (ThisAdjustment.isEmpty())
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001411 continue;
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001412
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001413 // Add it.
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001414 VTableThunks[VtableIndex].This = ThisAdjustment;
Anders Carlsson617def32010-03-12 05:28:07 +00001415
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001416 if (isa<CXXDestructorDecl>(MD)) {
1417 // Add an adjustment for the deleting destructor as well.
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001418 VTableThunks[VtableIndex + 1].This = ThisAdjustment;
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001419 }
1420 }
1421
1422 /// Clear the method info map.
1423 MethodInfoMap.clear();
Anders Carlsson25394212010-03-22 15:47:01 +00001424
1425 if (isBuildingConstructorVtable()) {
1426 // We don't need to store thunk information for construction vtables.
1427 return;
1428 }
1429
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00001430 for (VtableThunksMapTy::const_iterator I = VTableThunks.begin(),
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001431 E = VTableThunks.end(); I != E; ++I) {
Anders Carlsson25394212010-03-22 15:47:01 +00001432 const VtableComponent &Component = Components[I->first];
1433 const ThunkInfo &Thunk = I->second;
Anders Carlssonbca5d372010-03-22 20:06:40 +00001434 const CXXMethodDecl *MD;
Anders Carlsson25394212010-03-22 15:47:01 +00001435
1436 switch (Component.getKind()) {
1437 default:
1438 llvm_unreachable("Unexpected vtable component kind!");
1439 case VtableComponent::CK_FunctionPointer:
Anders Carlssonbca5d372010-03-22 20:06:40 +00001440 MD = Component.getFunctionDecl();
Anders Carlsson25394212010-03-22 15:47:01 +00001441 break;
1442 case VtableComponent::CK_CompleteDtorPointer:
Anders Carlssonbca5d372010-03-22 20:06:40 +00001443 MD = Component.getDestructorDecl();
Anders Carlsson25394212010-03-22 15:47:01 +00001444 break;
1445 case VtableComponent::CK_DeletingDtorPointer:
1446 // We've already added the thunk when we saw the complete dtor pointer.
Anders Carlssonbca5d372010-03-22 20:06:40 +00001447 continue;
Anders Carlsson25394212010-03-22 15:47:01 +00001448 }
Anders Carlssonbca5d372010-03-22 20:06:40 +00001449
1450 if (MD->getParent() == MostDerivedClass)
1451 AddThunk(MD, Thunk);
Anders Carlsson25394212010-03-22 15:47:01 +00001452 }
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001453}
1454
Anders Carlsson80d8d7d2010-03-23 15:13:06 +00001455ReturnAdjustment VtableBuilder::ComputeReturnAdjustment(BaseOffset Offset) {
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001456 ReturnAdjustment Adjustment;
1457
1458 if (!Offset.isEmpty()) {
Anders Carlsson60db0ee2010-02-13 21:07:32 +00001459 if (Offset.VirtualBase) {
1460 // Get the virtual base offset offset.
Anders Carlsson1972ced2010-03-11 07:00:45 +00001461 if (Offset.DerivedClass == MostDerivedClass) {
1462 // We can get the offset offset directly from our map.
1463 Adjustment.VBaseOffsetOffset =
1464 VBaseOffsetOffsets.lookup(Offset.VirtualBase);
1465 } else {
1466 Adjustment.VBaseOffsetOffset =
Anders Carlssonaf440352010-03-23 04:11:45 +00001467 VTables.getVirtualBaseOffsetOffset(Offset.DerivedClass,
1468 Offset.VirtualBase);
Anders Carlsson1972ced2010-03-11 07:00:45 +00001469 }
1470
Anders Carlssonc89a7cf2010-03-11 16:06:20 +00001471 // FIXME: Once the assert in getVirtualBaseOffsetOffset is back again,
Anders Carlsson60db0ee2010-02-13 21:07:32 +00001472 // we can get rid of this assert.
1473 assert(Adjustment.VBaseOffsetOffset != 0 &&
Anders Carlssonc89a7cf2010-03-11 16:06:20 +00001474 "Invalid vbase offset offset!");
Anders Carlsson60db0ee2010-02-13 21:07:32 +00001475 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001476
1477 Adjustment.NonVirtual = Offset.NonVirtualOffset;
1478 }
1479
1480 return Adjustment;
1481}
1482
Anders Carlsson379017f2010-02-27 19:57:44 +00001483BaseOffset
1484VtableBuilder::ComputeThisAdjustmentBaseOffset(BaseSubobject Base,
1485 BaseSubobject Derived) const {
1486 const CXXRecordDecl *BaseRD = Base.getBase();
1487 const CXXRecordDecl *DerivedRD = Derived.getBase();
1488
1489 CXXBasePaths Paths(/*FindAmbiguities=*/true,
1490 /*RecordPaths=*/true, /*DetectVirtual=*/true);
1491
1492 if (!const_cast<CXXRecordDecl *>(DerivedRD)->
1493 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) {
1494 assert(false && "Class must be derived from the passed in base class!");
1495 return BaseOffset();
1496 }
1497
1498 // We have to go through all the paths, and see which one leads us to the
1499 // right base subobject.
1500 for (CXXBasePaths::const_paths_iterator I = Paths.begin(), E = Paths.end();
1501 I != E; ++I) {
1502 BaseOffset Offset = ComputeBaseOffset(Context, DerivedRD, *I);
1503
1504 // FIXME: Should not use * 8 here.
1505 uint64_t OffsetToBaseSubobject = Offset.NonVirtualOffset * 8;
1506
1507 if (Offset.VirtualBase) {
1508 // If we have a virtual base class, the non-virtual offset is relative
1509 // to the virtual base class offset.
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001510 const ASTRecordLayout &LayoutClassLayout =
1511 Context.getASTRecordLayout(LayoutClass);
Anders Carlsson379017f2010-02-27 19:57:44 +00001512
1513 /// Get the virtual base offset, relative to the most derived class
1514 /// layout.
1515 OffsetToBaseSubobject +=
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001516 LayoutClassLayout.getVBaseClassOffset(Offset.VirtualBase);
Anders Carlsson379017f2010-02-27 19:57:44 +00001517 } else {
1518 // Otherwise, the non-virtual offset is relative to the derived class
1519 // offset.
1520 OffsetToBaseSubobject += Derived.getBaseOffset();
1521 }
1522
1523 // Check if this path gives us the right base subobject.
1524 if (OffsetToBaseSubobject == Base.getBaseOffset()) {
1525 // Since we're going from the base class _to_ the derived class, we'll
1526 // invert the non-virtual offset here.
1527 Offset.NonVirtualOffset = -Offset.NonVirtualOffset;
1528 return Offset;
1529 }
1530 }
1531
1532 return BaseOffset();
1533}
1534
Anders Carlsson80d8d7d2010-03-23 15:13:06 +00001535ThisAdjustment
Anders Carlsson617def32010-03-12 05:28:07 +00001536VtableBuilder::ComputeThisAdjustment(const CXXMethodDecl *MD,
1537 uint64_t BaseOffsetInLayoutClass,
1538 FinalOverriders::OverriderInfo Overrider) {
Anders Carlsson617def32010-03-12 05:28:07 +00001539 // Ignore adjustments for pure virtual member functions.
1540 if (Overrider.Method->isPure())
1541 return ThisAdjustment();
1542
1543 BaseSubobject OverriddenBaseSubobject(MD->getParent(),
1544 BaseOffsetInLayoutClass);
1545
1546 BaseSubobject OverriderBaseSubobject(Overrider.Method->getParent(),
1547 Overrider.Offset);
1548
1549 // Compute the adjustment offset.
1550 BaseOffset Offset = ComputeThisAdjustmentBaseOffset(OverriddenBaseSubobject,
1551 OverriderBaseSubobject);
1552 if (Offset.isEmpty())
1553 return ThisAdjustment();
1554
Anders Carlsson49bac9a2010-02-13 23:40:17 +00001555 ThisAdjustment Adjustment;
1556
Anders Carlsson617def32010-03-12 05:28:07 +00001557 if (Offset.VirtualBase) {
1558 // Get the vcall offset map for this virtual base.
1559 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Offset.VirtualBase];
Anders Carlssoneb577d02010-02-18 17:26:40 +00001560
Anders Carlsson617def32010-03-12 05:28:07 +00001561 if (VCallOffsets.empty()) {
1562 // We don't have vcall offsets for this virtual base, go ahead and
1563 // build them.
1564 VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, MostDerivedClass,
1565 /*FinalOverriders=*/0,
1566 BaseSubobject(Offset.VirtualBase, 0),
1567 /*BaseIsVirtual=*/true,
1568 /*OffsetInLayoutClass=*/0);
1569
1570 VCallOffsets = Builder.getVCallOffsets();
1571 }
1572
1573 Adjustment.VCallOffsetOffset = VCallOffsets.getVCallOffsetOffset(MD);
Anders Carlsson49bac9a2010-02-13 23:40:17 +00001574 }
Anders Carlsson617def32010-03-12 05:28:07 +00001575
1576 // Set the non-virtual part of the adjustment.
1577 Adjustment.NonVirtual = Offset.NonVirtualOffset;
Anders Carlsson49bac9a2010-02-13 23:40:17 +00001578
1579 return Adjustment;
1580}
Anders Carlsson617def32010-03-12 05:28:07 +00001581
Anders Carlsson57071e22010-02-12 05:25:12 +00001582void
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001583VtableBuilder::AddMethod(const CXXMethodDecl *MD,
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001584 ReturnAdjustment ReturnAdjustment) {
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001585 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1586 assert(ReturnAdjustment.isEmpty() &&
1587 "Destructor can't have return adjustment!");
Anders Carlsson49bac9a2010-02-13 23:40:17 +00001588
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001589 // Add both the complete destructor and the deleting destructor.
1590 Components.push_back(VtableComponent::MakeCompleteDtor(DD));
1591 Components.push_back(VtableComponent::MakeDeletingDtor(DD));
1592 } else {
1593 // Add the return adjustment if necessary.
1594 if (!ReturnAdjustment.isEmpty())
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00001595 VTableThunks[Components.size()].Return = ReturnAdjustment;
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001596
1597 // Add the function.
1598 Components.push_back(VtableComponent::MakeFunction(MD));
1599 }
1600}
1601
Anders Carlssonc784ba22010-02-27 06:38:03 +00001602/// OverridesIndirectMethodInBase - Return whether the given member function
1603/// overrides any methods in the set of given bases.
1604/// Unlike OverridesMethodInBase, this checks "overriders of overriders".
1605/// For example, if we have:
1606///
1607/// struct A { virtual void f(); }
1608/// struct B : A { virtual void f(); }
1609/// struct C : B { virtual void f(); }
1610///
1611/// OverridesIndirectMethodInBase will return true if given C::f as the method
1612/// and { A } as the set of bases.
1613static bool
1614OverridesIndirectMethodInBases(const CXXMethodDecl *MD,
Anders Carlssonb9021e92010-02-27 16:18:19 +00001615 VtableBuilder::PrimaryBasesSetVectorTy &Bases) {
Anders Carlssonc784ba22010-02-27 06:38:03 +00001616 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
1617 E = MD->end_overridden_methods(); I != E; ++I) {
1618 const CXXMethodDecl *OverriddenMD = *I;
1619 const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent();
1620 assert(OverriddenMD->isCanonicalDecl() &&
1621 "Should have the canonical decl of the overridden RD!");
1622
1623 if (Bases.count(OverriddenRD))
1624 return true;
1625
1626 // Check "indirect overriders".
1627 if (OverridesIndirectMethodInBases(OverriddenMD, Bases))
1628 return true;
1629 }
1630
1631 return false;
1632}
1633
Anders Carlsson464c8f12010-02-23 04:26:39 +00001634bool
Anders Carlsson60396612010-03-10 06:51:42 +00001635VtableBuilder::IsOverriderUsed(const CXXMethodDecl *Overrider,
1636 uint64_t BaseOffsetInLayoutClass,
1637 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
1638 uint64_t FirstBaseOffsetInLayoutClass) const {
Anders Carlsson464c8f12010-02-23 04:26:39 +00001639 // If the base and the first base in the primary base chain have the same
1640 // offsets, then this overrider will be used.
Anders Carlsson60396612010-03-10 06:51:42 +00001641 if (BaseOffsetInLayoutClass == FirstBaseOffsetInLayoutClass)
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001642 return true;
1643
1644 // We know now that Base (or a direct or indirect base of it) is a primary
1645 // base in part of the class hierarchy, but not a primary base in the most
1646 // derived class.
1647
1648 // If the overrider is the first base in the primary base chain, we know
1649 // that the overrider will be used.
Anders Carlsson60396612010-03-10 06:51:42 +00001650 if (Overrider->getParent() == FirstBaseInPrimaryBaseChain)
Anders Carlsson464c8f12010-02-23 04:26:39 +00001651 return true;
1652
Anders Carlssonb9021e92010-02-27 16:18:19 +00001653 VtableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001654
Anders Carlsson60396612010-03-10 06:51:42 +00001655 const CXXRecordDecl *RD = FirstBaseInPrimaryBaseChain;
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001656 PrimaryBases.insert(RD);
1657
1658 // Now traverse the base chain, starting with the first base, until we find
1659 // the base that is no longer a primary base.
1660 while (true) {
1661 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1662 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1663
1664 if (!PrimaryBase)
1665 break;
1666
1667 if (Layout.getPrimaryBaseWasVirtual()) {
1668 assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 &&
1669 "Primary base should always be at offset 0!");
1670
Anders Carlssondad0f992010-02-28 18:37:33 +00001671 const ASTRecordLayout &LayoutClassLayout =
1672 Context.getASTRecordLayout(LayoutClass);
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001673
1674 // Now check if this is the primary base that is not a primary base in the
1675 // most derived class.
Anders Carlssondad0f992010-02-28 18:37:33 +00001676 if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
Anders Carlsson60396612010-03-10 06:51:42 +00001677 FirstBaseOffsetInLayoutClass) {
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001678 // We found it, stop walking the chain.
1679 break;
1680 }
1681 } else {
1682 assert(Layout.getBaseClassOffset(PrimaryBase) == 0 &&
1683 "Primary base should always be at offset 0!");
1684 }
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001685
Anders Carlssonf89bb6c2010-02-27 19:51:04 +00001686 if (!PrimaryBases.insert(PrimaryBase))
1687 assert(false && "Found a duplicate primary base!");
1688
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001689 RD = PrimaryBase;
Anders Carlssonf9fbc712010-02-23 06:34:44 +00001690 }
1691
1692 // If the final overrider is an override of one of the primary bases,
1693 // then we know that it will be used.
Anders Carlsson60396612010-03-10 06:51:42 +00001694 return OverridesIndirectMethodInBases(Overrider, PrimaryBases);
Anders Carlsson464c8f12010-02-23 04:26:39 +00001695}
1696
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001697/// FindNearestOverriddenMethod - Given a method, returns the overridden method
1698/// from the nearest base. Returns null if no method was found.
1699static const CXXMethodDecl *
1700FindNearestOverriddenMethod(const CXXMethodDecl *MD,
1701 VtableBuilder::PrimaryBasesSetVectorTy &Bases) {
1702 for (int I = Bases.size(), E = 0; I != E; --I) {
1703 const CXXRecordDecl *PrimaryBase = Bases[I - 1];
1704
1705 // Now check the overriden methods.
1706 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
1707 E = MD->end_overridden_methods(); I != E; ++I) {
1708 const CXXMethodDecl *OverriddenMD = *I;
1709
1710 // We found our overridden method.
1711 if (OverriddenMD->getParent() == PrimaryBase)
1712 return OverriddenMD;
1713 }
1714 }
1715
1716 return 0;
1717}
1718
Anders Carlsson60396612010-03-10 06:51:42 +00001719void
1720VtableBuilder::AddMethods(BaseSubobject Base, uint64_t BaseOffsetInLayoutClass,
1721 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
1722 uint64_t FirstBaseOffsetInLayoutClass,
Anders Carlssonb9021e92010-02-27 16:18:19 +00001723 PrimaryBasesSetVectorTy &PrimaryBases) {
Anders Carlsson57071e22010-02-12 05:25:12 +00001724 const CXXRecordDecl *RD = Base.getBase();
Anders Carlsson57071e22010-02-12 05:25:12 +00001725 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1726
1727 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
Anders Carlsson60396612010-03-10 06:51:42 +00001728 uint64_t PrimaryBaseOffset;
1729 uint64_t PrimaryBaseOffsetInLayoutClass;
Anders Carlssonaf280c02010-02-23 03:48:14 +00001730 if (Layout.getPrimaryBaseWasVirtual()) {
1731 assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 &&
1732 "Primary vbase should have a zero offset!");
1733
1734 const ASTRecordLayout &MostDerivedClassLayout =
1735 Context.getASTRecordLayout(MostDerivedClass);
1736
Anders Carlsson60396612010-03-10 06:51:42 +00001737 PrimaryBaseOffset =
1738 MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
1739
1740 const ASTRecordLayout &LayoutClassLayout =
1741 Context.getASTRecordLayout(LayoutClass);
1742
1743 PrimaryBaseOffsetInLayoutClass =
1744 LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
Anders Carlssonaf280c02010-02-23 03:48:14 +00001745 } else {
Anders Carlsson57071e22010-02-12 05:25:12 +00001746 assert(Layout.getBaseClassOffset(PrimaryBase) == 0 &&
1747 "Primary base should have a zero offset!");
Anders Carlssonaf280c02010-02-23 03:48:14 +00001748
Anders Carlsson60396612010-03-10 06:51:42 +00001749 PrimaryBaseOffset = Base.getBaseOffset();
1750 PrimaryBaseOffsetInLayoutClass = BaseOffsetInLayoutClass;
Anders Carlssonaf280c02010-02-23 03:48:14 +00001751 }
Anders Carlssondad0f992010-02-28 18:37:33 +00001752
Anders Carlsson60396612010-03-10 06:51:42 +00001753 AddMethods(BaseSubobject(PrimaryBase, PrimaryBaseOffset),
1754 PrimaryBaseOffsetInLayoutClass, FirstBaseInPrimaryBaseChain,
1755 FirstBaseOffsetInLayoutClass, PrimaryBases);
Anders Carlsson57071e22010-02-12 05:25:12 +00001756
1757 if (!PrimaryBases.insert(PrimaryBase))
1758 assert(false && "Found a duplicate primary base!");
1759 }
Anders Carlsson822307b2010-02-11 17:18:51 +00001760
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001761 // Now go through all virtual member functions and add them.
1762 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
1763 E = RD->method_end(); I != E; ++I) {
1764 const CXXMethodDecl *MD = *I;
Anders Carlsson57071e22010-02-12 05:25:12 +00001765
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001766 if (!MD->isVirtual())
1767 continue;
Anders Carlsson57071e22010-02-12 05:25:12 +00001768
1769 // Get the final overrider.
Anders Carlssona7575822010-02-12 16:55:34 +00001770 FinalOverriders::OverriderInfo Overrider =
1771 Overriders.getOverrider(Base, MD);
Anders Carlsson57071e22010-02-12 05:25:12 +00001772
1773 // Check if this virtual member function overrides a method in a primary
1774 // base. If this is the case, and the return type doesn't require adjustment
1775 // then we can just use the member function from the primary base.
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001776 if (const CXXMethodDecl *OverriddenMD =
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001777 FindNearestOverriddenMethod(MD, PrimaryBases)) {
Anders Carlssondf31af12010-02-13 23:17:31 +00001778 if (ComputeReturnAdjustmentBaseOffset(Context, MD,
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001779 OverriddenMD).isEmpty()) {
1780 // Replace the method info of the overridden method with our own
1781 // method.
1782 assert(MethodInfoMap.count(OverriddenMD) &&
1783 "Did not find the overridden method!");
1784 MethodInfo &OverriddenMethodInfo = MethodInfoMap[OverriddenMD];
1785
1786 MethodInfo MethodInfo(Base.getBaseOffset(),
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001787 BaseOffsetInLayoutClass,
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001788 OverriddenMethodInfo.VtableIndex);
1789
1790 assert(!MethodInfoMap.count(MD) &&
1791 "Should not have method info for this method yet!");
1792
1793 MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
1794 MethodInfoMap.erase(OverriddenMD);
Anders Carlssonbca5d372010-03-22 20:06:40 +00001795
1796 // If the overridden method exists in a virtual base class or a direct
1797 // or indirect base class of a virtual base class, we need to emit a
1798 // thunk if we ever have a class hierarchy where the base class is not
1799 // a primary base in the complete object.
1800 if (!isBuildingConstructorVtable() && OverriddenMD != MD) {
1801 // Compute the this adjustment.
1802 ThisAdjustment ThisAdjustment =
1803 ComputeThisAdjustment(OverriddenMD, BaseOffsetInLayoutClass,
1804 Overrider);
1805
1806 if (ThisAdjustment.VCallOffsetOffset) {
1807 // This is a virtual thunk, add it.
1808 AddThunk(Overrider.Method,
1809 ThunkInfo(ThisAdjustment, ReturnAdjustment()));
1810 }
1811 }
1812
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001813 continue;
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001814 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001815 }
Anders Carlsson0c0eeb22010-02-13 02:02:03 +00001816
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001817 // Insert the method info for this method.
Anders Carlsson2bc1d3a2010-03-10 21:25:37 +00001818 MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
1819 Components.size());
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001820
1821 assert(!MethodInfoMap.count(MD) &&
1822 "Should not have method info for this method yet!");
1823 MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001824
1825 // Check if this overrider is going to be used.
Anders Carlsson60396612010-03-10 06:51:42 +00001826 const CXXMethodDecl *OverriderMD = Overrider.Method;
1827 if (!IsOverriderUsed(OverriderMD, BaseOffsetInLayoutClass,
1828 FirstBaseInPrimaryBaseChain,
1829 FirstBaseOffsetInLayoutClass)) {
Anders Carlssoncd4e6372010-02-27 18:09:40 +00001830 Components.push_back(VtableComponent::MakeUnusedFunction(OverriderMD));
1831 continue;
1832 }
Anders Carlsson464c8f12010-02-23 04:26:39 +00001833
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001834 // Check if this overrider needs a return adjustment.
Anders Carlsson2fca9be2010-02-23 01:34:28 +00001835 BaseOffset ReturnAdjustmentOffset =
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001836 Overriders.getReturnAdjustmentOffset(Base, MD);
1837
1838 ReturnAdjustment ReturnAdjustment =
1839 ComputeReturnAdjustment(ReturnAdjustmentOffset);
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001840
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001841 AddMethod(Overrider.Method, ReturnAdjustment);
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001842 }
1843}
1844
Anders Carlsson89ad4212010-02-16 16:26:28 +00001845void VtableBuilder::LayoutVtable() {
Anders Carlsson530c40c2010-02-28 01:43:58 +00001846 LayoutPrimaryAndSecondaryVtables(BaseSubobject(MostDerivedClass, 0),
1847 MostDerivedClassIsVirtual,
1848 MostDerivedClassOffset);
Anders Carlsson89ad4212010-02-16 16:26:28 +00001849
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001850 VisitedVirtualBasesSetTy VBases;
Anders Carlsson08c26752010-02-27 04:05:52 +00001851
1852 // Determine the primary virtual bases.
Anders Carlssond7fdae52010-03-10 03:02:01 +00001853 DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset,
1854 VBases);
Anders Carlsson08c26752010-02-27 04:05:52 +00001855 VBases.clear();
1856
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001857 LayoutVtablesForVirtualBases(MostDerivedClass, VBases);
Anders Carlsson89ad4212010-02-16 16:26:28 +00001858}
1859
Anders Carlsson530c40c2010-02-28 01:43:58 +00001860void
1861VtableBuilder::LayoutPrimaryAndSecondaryVtables(BaseSubobject Base,
1862 bool BaseIsVirtual,
1863 uint64_t OffsetInLayoutClass) {
Chris Lattner04a23112010-02-17 21:42:34 +00001864 assert(Base.getBase()->isDynamicClass() && "class does not have a vtable!");
Anders Carlssonb828afa2010-02-14 17:05:59 +00001865
Anders Carlsson852213e2010-02-16 04:59:55 +00001866 // Add vcall and vbase offsets for this vtable.
Anders Carlsson530c40c2010-02-28 01:43:58 +00001867 VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, LayoutClass, &Overriders,
1868 Base, BaseIsVirtual, OffsetInLayoutClass);
Anders Carlssond68c0832010-02-25 03:45:56 +00001869 Components.append(Builder.components_begin(), Builder.components_end());
1870
Anders Carlssondba6ef82010-02-25 22:23:13 +00001871 // Check if we need to add these vcall offsets.
1872 if (BaseIsVirtual && !Builder.getVCallOffsets().empty()) {
1873 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()];
1874
1875 if (VCallOffsets.empty())
1876 VCallOffsets = Builder.getVCallOffsets();
1877 }
1878
Anders Carlssonbb6305b2010-03-11 06:43:12 +00001879 // If we're laying out the most derived class we want to keep track of the
1880 // virtual base class offset offsets.
1881 if (Base.getBase() == MostDerivedClass)
1882 VBaseOffsetOffsets = Builder.getVBaseOffsetOffsets();
1883
Anders Carlsson852213e2010-02-16 04:59:55 +00001884 // Add the offset to top.
Anders Carlssone67dc302010-02-14 00:16:19 +00001885 // FIXME: We should not use / 8 here.
Anders Carlsson530c40c2010-02-28 01:43:58 +00001886 int64_t OffsetToTop = -(int64_t)(OffsetInLayoutClass -
1887 MostDerivedClassOffset) / 8;
Anders Carlsson76f1aa72010-02-13 22:05:23 +00001888 Components.push_back(VtableComponent::MakeOffsetToTop(OffsetToTop));
Anders Carlsson57071e22010-02-12 05:25:12 +00001889
1890 // Next, add the RTTI.
Anders Carlsson76f1aa72010-02-13 22:05:23 +00001891 Components.push_back(VtableComponent::MakeRTTI(MostDerivedClass));
Anders Carlsson57071e22010-02-12 05:25:12 +00001892
Anders Carlsson28cbc8b2010-02-12 07:43:48 +00001893 uint64_t AddressPoint = Components.size();
Anders Carlsson57071e22010-02-12 05:25:12 +00001894
1895 // Now go through all virtual member functions and add them.
Anders Carlssonb9021e92010-02-27 16:18:19 +00001896 PrimaryBasesSetVectorTy PrimaryBases;
Anders Carlsson60396612010-03-10 06:51:42 +00001897 AddMethods(Base, OffsetInLayoutClass, Base.getBase(), OffsetInLayoutClass,
1898 PrimaryBases);
Anders Carlsson57071e22010-02-12 05:25:12 +00001899
Anders Carlsson2ef9d6b2010-02-27 16:52:49 +00001900 // Compute 'this' pointer adjustments.
1901 ComputeThisAdjustments();
1902
Anders Carlsson014a3582010-03-25 21:45:14 +00001903 // Add all address points.
1904 const CXXRecordDecl *RD = Base.getBase();
1905 while (true) {
1906 AddressPoints.insert(std::make_pair(BaseSubobject(RD, OffsetInLayoutClass),
1907 AddressPoint));
1908
1909 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1910 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Anders Carlsson28cbc8b2010-02-12 07:43:48 +00001911
Anders Carlsson014a3582010-03-25 21:45:14 +00001912 if (!PrimaryBase)
1913 break;
1914
1915 if (Layout.getPrimaryBaseWasVirtual()) {
1916 // Check if this virtual primary base is a primary base in the layout
1917 // class. If it's not, we don't want to add it.
1918 const ASTRecordLayout &LayoutClassLayout =
1919 Context.getASTRecordLayout(LayoutClass);
1920
1921 if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
1922 OffsetInLayoutClass) {
1923 // We don't want to add this class (or any of its primary bases).
1924 break;
1925 }
1926 }
1927
1928 RD = PrimaryBase;
Anders Carlsson28cbc8b2010-02-12 07:43:48 +00001929 }
1930
Anders Carlssona96a2e92010-03-10 19:39:11 +00001931 bool BaseIsMorallyVirtual = BaseIsVirtual;
1932 if (isBuildingConstructorVtable() && Base.getBase() == MostDerivedClass)
1933 BaseIsMorallyVirtual = false;
1934
Anders Carlsson5edcc372010-02-24 16:43:12 +00001935 // Layout secondary vtables.
Anders Carlssona96a2e92010-03-10 19:39:11 +00001936 LayoutSecondaryVtables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass);
Anders Carlssonc7b63162010-02-16 16:02:57 +00001937}
1938
Anders Carlsson530c40c2010-02-28 01:43:58 +00001939void VtableBuilder::LayoutSecondaryVtables(BaseSubobject Base,
Anders Carlssona96a2e92010-03-10 19:39:11 +00001940 bool BaseIsMorallyVirtual,
Anders Carlsson530c40c2010-02-28 01:43:58 +00001941 uint64_t OffsetInLayoutClass) {
Anders Carlssonc7b63162010-02-16 16:02:57 +00001942 // Itanium C++ ABI 2.5.2:
1943 // Following the primary virtual table of a derived class are secondary
1944 // virtual tables for each of its proper base classes, except any primary
1945 // base(s) with which it shares its primary virtual table.
1946
1947 const CXXRecordDecl *RD = Base.getBase();
Anders Carlsson57071e22010-02-12 05:25:12 +00001948 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1949 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
Anders Carlssonc7b63162010-02-16 16:02:57 +00001950
Anders Carlsson57071e22010-02-12 05:25:12 +00001951 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1952 E = RD->bases_end(); I != E; ++I) {
Anders Carlssonc7b63162010-02-16 16:02:57 +00001953 // Ignore virtual bases, we'll emit them later.
1954 if (I->isVirtual())
1955 continue;
1956
Anders Carlsson57071e22010-02-12 05:25:12 +00001957 const CXXRecordDecl *BaseDecl =
1958 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
Anders Carlssonb828afa2010-02-14 17:05:59 +00001959
Anders Carlssona661a542010-02-16 04:49:44 +00001960 // Ignore bases that don't have a vtable.
1961 if (!BaseDecl->isDynamicClass())
1962 continue;
Anders Carlssonb828afa2010-02-14 17:05:59 +00001963
Anders Carlssonf2c98ce2010-03-10 19:15:26 +00001964 if (isBuildingConstructorVtable()) {
1965 // Itanium C++ ABI 2.6.4:
1966 // Some of the base class subobjects may not need construction virtual
1967 // tables, which will therefore not be present in the construction
1968 // virtual table group, even though the subobject virtual tables are
1969 // present in the main virtual table group for the complete object.
Anders Carlssona96a2e92010-03-10 19:39:11 +00001970 if (!BaseIsMorallyVirtual && !BaseDecl->getNumVBases())
Anders Carlssonf2c98ce2010-03-10 19:15:26 +00001971 continue;
Anders Carlssonf2c98ce2010-03-10 19:15:26 +00001972 }
1973
Anders Carlsson76f1aa72010-02-13 22:05:23 +00001974 // Get the base offset of this base.
Anders Carlsson530c40c2010-02-28 01:43:58 +00001975 uint64_t RelativeBaseOffset = Layout.getBaseClassOffset(BaseDecl);
1976 uint64_t BaseOffset = Base.getBaseOffset() + RelativeBaseOffset;
1977
1978 uint64_t BaseOffsetInLayoutClass = OffsetInLayoutClass + RelativeBaseOffset;
Anders Carlssonc7b63162010-02-16 16:02:57 +00001979
1980 // Don't emit a secondary vtable for a primary base. We might however want
1981 // to emit secondary vtables for other bases of this base.
1982 if (BaseDecl == PrimaryBase) {
Anders Carlsson530c40c2010-02-28 01:43:58 +00001983 LayoutSecondaryVtables(BaseSubobject(BaseDecl, BaseOffset),
Anders Carlssona96a2e92010-03-10 19:39:11 +00001984 BaseIsMorallyVirtual, BaseOffsetInLayoutClass);
Anders Carlssonc7b63162010-02-16 16:02:57 +00001985 continue;
1986 }
Anders Carlsson76f1aa72010-02-13 22:05:23 +00001987
Anders Carlsson89ad4212010-02-16 16:26:28 +00001988 // Layout the primary vtable (and any secondary vtables) for this base.
Anders Carlsson530c40c2010-02-28 01:43:58 +00001989 LayoutPrimaryAndSecondaryVtables(BaseSubobject(BaseDecl, BaseOffset),
1990 /*BaseIsVirtual=*/false,
1991 BaseOffsetInLayoutClass);
Anders Carlsson57071e22010-02-12 05:25:12 +00001992 }
1993}
1994
Anders Carlsson69dc04e2010-02-16 16:49:35 +00001995void
Anders Carlssond7fdae52010-03-10 03:02:01 +00001996VtableBuilder::DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
1997 uint64_t OffsetInLayoutClass,
Anders Carlsson08c26752010-02-27 04:05:52 +00001998 VisitedVirtualBasesSetTy &VBases) {
1999 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2000
2001 // Check if this base has a primary base.
2002 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
Anders Carlsson0378bf02010-02-28 18:08:38 +00002003
2004 // Check if it's virtual.
2005 if (Layout.getPrimaryBaseWasVirtual()) {
2006 bool IsPrimaryVirtualBase = true;
2007
2008 if (isBuildingConstructorVtable()) {
2009 // Check if the base is actually a primary base in the class we use for
2010 // layout.
Anders Carlssond7fdae52010-03-10 03:02:01 +00002011 const ASTRecordLayout &LayoutClassLayout =
2012 Context.getASTRecordLayout(LayoutClass);
2013
2014 uint64_t PrimaryBaseOffsetInLayoutClass =
2015 LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
2016
2017 // We know that the base is not a primary base in the layout class if
2018 // the base offsets are different.
2019 if (PrimaryBaseOffsetInLayoutClass != OffsetInLayoutClass)
Anders Carlsson0378bf02010-02-28 18:08:38 +00002020 IsPrimaryVirtualBase = false;
2021 }
2022
2023 if (IsPrimaryVirtualBase)
2024 PrimaryVirtualBases.insert(PrimaryBase);
2025 }
Anders Carlsson08c26752010-02-27 04:05:52 +00002026 }
2027
2028 // Traverse bases, looking for more primary virtual bases.
2029 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
2030 E = RD->bases_end(); I != E; ++I) {
2031 const CXXRecordDecl *BaseDecl =
2032 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
2033
Anders Carlssond7fdae52010-03-10 03:02:01 +00002034 uint64_t BaseOffsetInLayoutClass;
2035
2036 if (I->isVirtual()) {
2037 if (!VBases.insert(BaseDecl))
2038 continue;
2039
2040 const ASTRecordLayout &LayoutClassLayout =
2041 Context.getASTRecordLayout(LayoutClass);
Anders Carlsson08c26752010-02-27 04:05:52 +00002042
Anders Carlssond7fdae52010-03-10 03:02:01 +00002043 BaseOffsetInLayoutClass = LayoutClassLayout.getVBaseClassOffset(BaseDecl);
2044 } else {
2045 BaseOffsetInLayoutClass =
2046 OffsetInLayoutClass + Layout.getBaseClassOffset(BaseDecl);
2047 }
2048
2049 DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases);
Anders Carlsson08c26752010-02-27 04:05:52 +00002050 }
2051}
2052
2053void
Anders Carlsson69dc04e2010-02-16 16:49:35 +00002054VtableBuilder::LayoutVtablesForVirtualBases(const CXXRecordDecl *RD,
2055 VisitedVirtualBasesSetTy &VBases) {
2056 // Itanium C++ ABI 2.5.2:
2057 // Then come the virtual base virtual tables, also in inheritance graph
2058 // order, and again excluding primary bases (which share virtual tables with
2059 // the classes for which they are primary).
Anders Carlsson69dc04e2010-02-16 16:49:35 +00002060 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
2061 E = RD->bases_end(); I != E; ++I) {
2062 const CXXRecordDecl *BaseDecl =
2063 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
2064
Anders Carlssonaf280c02010-02-23 03:48:14 +00002065 // Check if this base needs a vtable. (If it's virtual, not a primary base
2066 // of some other class, and we haven't visited it before).
Anders Carlsson69dc04e2010-02-16 16:49:35 +00002067 if (I->isVirtual() && BaseDecl->isDynamicClass() &&
Anders Carlsson08c26752010-02-27 04:05:52 +00002068 !PrimaryVirtualBases.count(BaseDecl) && VBases.insert(BaseDecl)) {
Anders Carlsson69dc04e2010-02-16 16:49:35 +00002069 const ASTRecordLayout &MostDerivedClassLayout =
2070 Context.getASTRecordLayout(MostDerivedClass);
2071 uint64_t BaseOffset =
2072 MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
2073
Anders Carlsson530c40c2010-02-28 01:43:58 +00002074 const ASTRecordLayout &LayoutClassLayout =
2075 Context.getASTRecordLayout(LayoutClass);
2076 uint64_t BaseOffsetInLayoutClass =
2077 LayoutClassLayout.getVBaseClassOffset(BaseDecl);
2078
2079 LayoutPrimaryAndSecondaryVtables(BaseSubobject(BaseDecl, BaseOffset),
2080 /*BaseIsVirtual=*/true,
2081 BaseOffsetInLayoutClass);
Anders Carlsson69dc04e2010-02-16 16:49:35 +00002082 }
2083
2084 // We only need to check the base for virtual base vtables if it actually
2085 // has virtual bases.
2086 if (BaseDecl->getNumVBases())
2087 LayoutVtablesForVirtualBases(BaseDecl, VBases);
2088 }
2089}
2090
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002091/// dumpLayout - Dump the vtable layout.
2092void VtableBuilder::dumpLayout(llvm::raw_ostream& Out) {
Anders Carlssond03a3262010-02-27 20:39:05 +00002093
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002094 if (isBuildingConstructorVtable()) {
Anders Carlssond03a3262010-02-27 20:39:05 +00002095 Out << "Construction vtable for ('";
2096 Out << MostDerivedClass->getQualifiedNameAsString() << "', ";
2097 // FIXME: Don't use / 8 .
2098 Out << MostDerivedClassOffset / 8 << ") in '";
2099 Out << LayoutClass->getQualifiedNameAsString();
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002100 } else {
2101 Out << "Vtable for '";
2102 Out << MostDerivedClass->getQualifiedNameAsString();
Anders Carlssond03a3262010-02-27 20:39:05 +00002103 }
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002104 Out << "' (" << Components.size() << " entries).\n";
2105
Anders Carlsson822307b2010-02-11 17:18:51 +00002106 // Iterate through the address points and insert them into a new map where
2107 // they are keyed by the index and not the base object.
2108 // Since an address point can be shared by multiple subobjects, we use an
2109 // STL multimap.
2110 std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex;
Anders Carlssonff143f82010-03-25 00:35:49 +00002111 for (AddressPointsMapTy::const_iterator I = AddressPoints.begin(),
2112 E = AddressPoints.end(); I != E; ++I) {
Anders Carlsson822307b2010-02-11 17:18:51 +00002113 const BaseSubobject& Base = I->first;
2114 uint64_t Index = I->second;
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002115
Anders Carlsson822307b2010-02-11 17:18:51 +00002116 AddressPointsByIndex.insert(std::make_pair(Index, Base));
2117 }
2118
2119 for (unsigned I = 0, E = Components.size(); I != E; ++I) {
Anders Carlsson28cbc8b2010-02-12 07:43:48 +00002120 uint64_t Index = I;
Anders Carlsson822307b2010-02-11 17:18:51 +00002121
2122 Out << llvm::format("%4d | ", I);
2123
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002124 const VtableComponent &Component = Components[I];
2125
2126 // Dump the component.
2127 switch (Component.getKind()) {
Anders Carlssond9a67042010-02-17 06:07:19 +00002128
2129 case VtableComponent::CK_VCallOffset:
2130 Out << "vcall_offset (" << Component.getVCallOffset() << ")";
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002131 break;
Anders Carlsson852213e2010-02-16 04:59:55 +00002132
2133 case VtableComponent::CK_VBaseOffset:
2134 Out << "vbase_offset (" << Component.getVBaseOffset() << ")";
2135 break;
2136
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002137 case VtableComponent::CK_OffsetToTop:
2138 Out << "offset_to_top (" << Component.getOffsetToTop() << ")";
2139 break;
2140
2141 case VtableComponent::CK_RTTI:
2142 Out << Component.getRTTIDecl()->getQualifiedNameAsString() << " RTTI";
2143 break;
2144
Anders Carlsson848fa642010-02-11 18:20:28 +00002145 case VtableComponent::CK_FunctionPointer: {
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002146 const CXXMethodDecl *MD = Component.getFunctionDecl();
2147
Anders Carlsson848fa642010-02-11 18:20:28 +00002148 std::string Str =
2149 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2150 MD);
2151 Out << Str;
Anders Carlsson98241422010-02-12 02:38:13 +00002152 if (MD->isPure())
2153 Out << " [pure]";
2154
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00002155 ThunkInfo Thunk = VTableThunks.lookup(I);
Anders Carlssondd366812010-03-12 04:54:20 +00002156 if (!Thunk.isEmpty()) {
2157 // If this function pointer has a return adjustment, dump it.
2158 if (!Thunk.Return.isEmpty()) {
2159 Out << "\n [return adjustment: ";
2160 Out << Thunk.Return.NonVirtual << " non-virtual";
2161
2162 if (Thunk.Return.VBaseOffsetOffset) {
2163 Out << ", " << Thunk.Return.VBaseOffsetOffset;
2164 Out << " vbase offset offset";
2165 }
Anders Carlsson60cc72a2010-02-18 17:00:09 +00002166
Anders Carlssondd366812010-03-12 04:54:20 +00002167 Out << ']';
2168 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00002169
Anders Carlssondd366812010-03-12 04:54:20 +00002170 // If this function pointer has a 'this' pointer adjustment, dump it.
2171 if (!Thunk.This.isEmpty()) {
2172 Out << "\n [this adjustment: ";
2173 Out << Thunk.This.NonVirtual << " non-virtual";
2174
2175 if (Thunk.This.VCallOffsetOffset) {
2176 Out << ", " << Thunk.This.VCallOffsetOffset;
2177 Out << " vcall offset offset";
2178 }
Anders Carlsson60cc72a2010-02-18 17:00:09 +00002179
Anders Carlssondd366812010-03-12 04:54:20 +00002180 Out << ']';
2181 }
Anders Carlsson49bac9a2010-02-13 23:40:17 +00002182 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00002183
Anders Carlsson848fa642010-02-11 18:20:28 +00002184 break;
2185 }
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002186
Anders Carlsson6065e022010-02-19 06:03:53 +00002187 case VtableComponent::CK_CompleteDtorPointer:
Anders Carlsson848fa642010-02-11 18:20:28 +00002188 case VtableComponent::CK_DeletingDtorPointer: {
Anders Carlsson6065e022010-02-19 06:03:53 +00002189 bool IsComplete =
2190 Component.getKind() == VtableComponent::CK_CompleteDtorPointer;
2191
Anders Carlsson848fa642010-02-11 18:20:28 +00002192 const CXXDestructorDecl *DD = Component.getDestructorDecl();
2193
Anders Carlsson6065e022010-02-19 06:03:53 +00002194 Out << DD->getQualifiedNameAsString();
2195 if (IsComplete)
2196 Out << "() [complete]";
2197 else
2198 Out << "() [deleting]";
2199
Anders Carlsson98241422010-02-12 02:38:13 +00002200 if (DD->isPure())
2201 Out << " [pure]";
2202
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00002203 ThunkInfo Thunk = VTableThunks.lookup(I);
Anders Carlssondd366812010-03-12 04:54:20 +00002204 if (!Thunk.isEmpty()) {
2205 // If this destructor has a 'this' pointer adjustment, dump it.
2206 if (!Thunk.This.isEmpty()) {
2207 Out << "\n [this adjustment: ";
2208 Out << Thunk.This.NonVirtual << " non-virtual";
2209
2210 if (Thunk.This.VCallOffsetOffset) {
2211 Out << ", " << Thunk.This.VCallOffsetOffset;
2212 Out << " vcall offset offset";
2213 }
2214
2215 Out << ']';
2216 }
2217 }
Anders Carlsson6065e022010-02-19 06:03:53 +00002218
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002219 break;
2220 }
2221
Anders Carlsson1f464662010-02-19 20:08:13 +00002222 case VtableComponent::CK_UnusedFunctionPointer: {
2223 const CXXMethodDecl *MD = Component.getUnusedFunctionDecl();
2224
2225 std::string Str =
2226 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2227 MD);
2228 Out << "[unused] " << Str;
2229 if (MD->isPure())
2230 Out << " [pure]";
2231 }
2232
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002233 }
Anders Carlsson822307b2010-02-11 17:18:51 +00002234
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002235 Out << '\n';
Anders Carlssond2c3a7c2010-02-27 23:58:01 +00002236
2237 // Dump the next address point.
2238 uint64_t NextIndex = Index + 1;
2239 if (AddressPointsByIndex.count(NextIndex)) {
2240 if (AddressPointsByIndex.count(NextIndex) == 1) {
2241 const BaseSubobject &Base =
2242 AddressPointsByIndex.find(NextIndex)->second;
2243
2244 // FIXME: Instead of dividing by 8, we should be using CharUnits.
2245 Out << " -- (" << Base.getBase()->getQualifiedNameAsString();
2246 Out << ", " << Base.getBaseOffset() / 8 << ") vtable address --\n";
2247 } else {
2248 uint64_t BaseOffset =
2249 AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset();
2250
2251 // We store the class names in a set to get a stable order.
2252 std::set<std::string> ClassNames;
2253 for (std::multimap<uint64_t, BaseSubobject>::const_iterator I =
2254 AddressPointsByIndex.lower_bound(NextIndex), E =
2255 AddressPointsByIndex.upper_bound(NextIndex); I != E; ++I) {
2256 assert(I->second.getBaseOffset() == BaseOffset &&
2257 "Invalid base offset!");
2258 const CXXRecordDecl *RD = I->second.getBase();
2259 ClassNames.insert(RD->getQualifiedNameAsString());
2260 }
2261
2262 for (std::set<std::string>::const_iterator I = ClassNames.begin(),
2263 E = ClassNames.end(); I != E; ++I) {
2264 // FIXME: Instead of dividing by 8, we should be using CharUnits.
2265 Out << " -- (" << *I;
2266 Out << ", " << BaseOffset / 8 << ") vtable address --\n";
2267 }
2268 }
2269 }
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002270 }
Anders Carlssond03a3262010-02-27 20:39:05 +00002271
2272 Out << '\n';
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002273
Anders Carlsson9135a842010-03-18 02:44:19 +00002274 if (isBuildingConstructorVtable())
2275 return;
2276
2277 if (MostDerivedClass->getNumVBases()) {
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002278 // We store the virtual base class names and their offsets in a map to get
2279 // a stable order.
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002280
Anders Carlsson9135a842010-03-18 02:44:19 +00002281 std::map<std::string, int64_t> ClassNamesAndOffsets;
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002282 for (VBaseOffsetOffsetsMapTy::const_iterator I = VBaseOffsetOffsets.begin(),
2283 E = VBaseOffsetOffsets.end(); I != E; ++I) {
2284 std::string ClassName = I->first->getQualifiedNameAsString();
2285 int64_t OffsetOffset = I->second;
2286 ClassNamesAndOffsets.insert(std::make_pair(ClassName, OffsetOffset));
2287 }
Anders Carlsson9135a842010-03-18 02:44:19 +00002288
2289 Out << "Virtual base offset offsets for '";
2290 Out << MostDerivedClass->getQualifiedNameAsString() << "' (";
2291 Out << ClassNamesAndOffsets.size();
2292 Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n";
Anders Carlssonbb6305b2010-03-11 06:43:12 +00002293
2294 for (std::map<std::string, int64_t>::const_iterator I =
2295 ClassNamesAndOffsets.begin(), E = ClassNamesAndOffsets.end();
2296 I != E; ++I)
2297 Out << " " << I->first << " | " << I->second << '\n';
2298
2299 Out << "\n";
2300 }
Anders Carlsson9135a842010-03-18 02:44:19 +00002301
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00002302 if (!Thunks.empty()) {
Anders Carlsson9135a842010-03-18 02:44:19 +00002303 // We store the method names in a map to get a stable order.
2304 std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls;
2305
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00002306 for (ThunksMapTy::const_iterator I = Thunks.begin(), E = Thunks.end();
Anders Carlsson3a5df3f2010-03-23 04:44:10 +00002307 I != E; ++I) {
Anders Carlsson9135a842010-03-18 02:44:19 +00002308 const CXXMethodDecl *MD = I->first;
2309 std::string MethodName =
2310 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2311 MD);
2312
2313 MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
2314 }
2315
2316 for (std::map<std::string, const CXXMethodDecl *>::const_iterator I =
2317 MethodNamesAndDecls.begin(), E = MethodNamesAndDecls.end();
2318 I != E; ++I) {
2319 const std::string &MethodName = I->first;
2320 const CXXMethodDecl *MD = I->second;
Anders Carlsson87e0ac82010-03-22 16:30:44 +00002321
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00002322 ThunkInfoVectorTy ThunksVector = Thunks[MD];
Anders Carlsson87e0ac82010-03-22 16:30:44 +00002323 std::sort(ThunksVector.begin(), ThunksVector.end());
Anders Carlsson9135a842010-03-18 02:44:19 +00002324
2325 Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
2326 Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n";
2327
2328 for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) {
2329 const ThunkInfo &Thunk = ThunksVector[I];
2330
2331 Out << llvm::format("%4d | ", I);
2332
Anders Carlsson09daa502010-03-22 16:10:59 +00002333 // If this function pointer has a return pointer adjustment, dump it.
2334 if (!Thunk.Return.isEmpty()) {
2335 Out << "return adjustment: " << Thunk.This.NonVirtual;
2336 Out << " non-virtual";
2337 if (Thunk.Return.VBaseOffsetOffset) {
2338 Out << ", " << Thunk.Return.VBaseOffsetOffset;
2339 Out << " vbase offset offset";
2340 }
2341
2342 if (!Thunk.This.isEmpty())
2343 Out << "\n ";
2344 }
2345
Anders Carlsson9135a842010-03-18 02:44:19 +00002346 // If this function pointer has a 'this' pointer adjustment, dump it.
2347 if (!Thunk.This.isEmpty()) {
Anders Carlsson09daa502010-03-22 16:10:59 +00002348 Out << "this adjustment: ";
2349 Out << Thunk.This.NonVirtual << " non-virtual";
Anders Carlsson9135a842010-03-18 02:44:19 +00002350
2351 if (Thunk.This.VCallOffsetOffset) {
2352 Out << ", " << Thunk.This.VCallOffsetOffset;
Anders Carlsson09daa502010-03-22 16:10:59 +00002353 Out << " vcall offset offset";
Anders Carlsson9135a842010-03-18 02:44:19 +00002354 }
2355 }
2356
2357 Out << '\n';
2358 }
Anders Carlssonbca5d372010-03-22 20:06:40 +00002359
2360 Out << '\n';
2361
Anders Carlsson9135a842010-03-18 02:44:19 +00002362 }
2363 }
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002364}
2365
2366}
2367
2368namespace {
2369class OldVtableBuilder {
Anders Carlssondbd920c2009-10-11 22:13:54 +00002370public:
2371 /// Index_t - Vtable index type.
2372 typedef uint64_t Index_t;
Eli Friedmanb455f0e2009-12-07 23:56:34 +00002373 typedef std::vector<std::pair<GlobalDecl,
2374 std::pair<GlobalDecl, ThunkAdjustment> > >
2375 SavedAdjustmentsVectorTy;
Anders Carlssonff143f82010-03-25 00:35:49 +00002376 typedef llvm::DenseMap<BaseSubobject, uint64_t> AddressPointsMapTy;
2377
Anders Carlssondbd920c2009-10-11 22:13:54 +00002378private:
Anders Carlsson15318f42009-12-04 16:19:30 +00002379
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00002380 // VtableComponents - The components of the vtable being built.
2381 typedef llvm::SmallVector<llvm::Constant *, 64> VtableComponentsVectorTy;
2382 VtableComponentsVectorTy VtableComponents;
2383
Eli Friedman152b5b12009-12-05 01:05:03 +00002384 const bool BuildVtable;
2385
Anders Carlssondbd920c2009-10-11 22:13:54 +00002386 llvm::Type *Ptr8Ty;
Anders Carlssonac3f7bd2009-12-04 18:36:22 +00002387
2388 /// MostDerivedClass - The most derived class that this vtable is being
2389 /// built for.
2390 const CXXRecordDecl *MostDerivedClass;
2391
Mike Stumpacfd1e52009-11-13 01:54:23 +00002392 /// LayoutClass - The most derived class used for virtual base layout
2393 /// information.
2394 const CXXRecordDecl *LayoutClass;
Mike Stump4cde6262009-11-13 02:13:54 +00002395 /// LayoutOffset - The offset for Class in LayoutClass.
2396 uint64_t LayoutOffset;
Anders Carlssondbd920c2009-10-11 22:13:54 +00002397 /// BLayout - Layout for the most derived class that this vtable is being
2398 /// built for.
2399 const ASTRecordLayout &BLayout;
2400 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
2401 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
2402 llvm::Constant *rtti;
2403 llvm::LLVMContext &VMContext;
2404 CodeGenModule &CGM; // Per-module state.
Anders Carlsson0e881162009-12-04 03:46:21 +00002405
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002406 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall;
Anders Carlssona0fdd912009-11-13 17:08:56 +00002407 llvm::DenseMap<GlobalDecl, Index_t> VCallOffset;
Mike Stumpd99a4d22010-01-26 03:42:22 +00002408 llvm::DenseMap<GlobalDecl, Index_t> VCallOffsetForVCall;
Mike Stump9e7e3c62009-11-06 23:27:42 +00002409 // This is the offset to the nearest virtual base
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002410 llvm::DenseMap<const CXXMethodDecl *, Index_t> NonVirtualOffset;
Anders Carlssondbd920c2009-10-11 22:13:54 +00002411 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stump94aff932009-10-27 23:46:47 +00002412
Anders Carlsson6d4ccb72009-11-26 19:54:33 +00002413 /// PureVirtualFunction - Points to __cxa_pure_virtual.
2414 llvm::Constant *PureVirtualFn;
2415
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00002416 /// VtableMethods - A data structure for keeping track of methods in a vtable.
2417 /// Can add methods, override methods and iterate in vtable order.
2418 class VtableMethods {
2419 // MethodToIndexMap - Maps from a global decl to the index it has in the
2420 // Methods vector.
2421 llvm::DenseMap<GlobalDecl, uint64_t> MethodToIndexMap;
2422
2423 /// Methods - The methods, in vtable order.
2424 typedef llvm::SmallVector<GlobalDecl, 16> MethodsVectorTy;
2425 MethodsVectorTy Methods;
Eli Friedman72649ed2009-12-06 22:01:30 +00002426 MethodsVectorTy OrigMethods;
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00002427
2428 public:
2429 /// AddMethod - Add a method to the vtable methods.
2430 void AddMethod(GlobalDecl GD) {
2431 assert(!MethodToIndexMap.count(GD) &&
2432 "Method has already been added!");
2433
2434 MethodToIndexMap[GD] = Methods.size();
2435 Methods.push_back(GD);
Eli Friedman72649ed2009-12-06 22:01:30 +00002436 OrigMethods.push_back(GD);
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00002437 }
2438
2439 /// OverrideMethod - Replace a method with another.
2440 void OverrideMethod(GlobalDecl OverriddenGD, GlobalDecl GD) {
2441 llvm::DenseMap<GlobalDecl, uint64_t>::iterator i
2442 = MethodToIndexMap.find(OverriddenGD);
2443 assert(i != MethodToIndexMap.end() && "Did not find entry!");
2444
2445 // Get the index of the old decl.
2446 uint64_t Index = i->second;
2447
2448 // Replace the old decl with the new decl.
2449 Methods[Index] = GD;
2450
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00002451 // And add the new.
2452 MethodToIndexMap[GD] = Index;
2453 }
2454
Anders Carlsson77c23e52009-12-04 15:49:02 +00002455 /// getIndex - Gives the index of a passed in GlobalDecl. Returns false if
2456 /// the index couldn't be found.
Benjamin Kramercce9fde2009-12-04 22:45:27 +00002457 bool getIndex(GlobalDecl GD, uint64_t &Index) const {
Anders Carlsson77c23e52009-12-04 15:49:02 +00002458 llvm::DenseMap<GlobalDecl, uint64_t>::const_iterator i
2459 = MethodToIndexMap.find(GD);
Eli Friedman47145832009-12-04 08:34:14 +00002460
Anders Carlsson77c23e52009-12-04 15:49:02 +00002461 if (i == MethodToIndexMap.end())
2462 return false;
Anders Carlssonc0c49932009-12-04 03:41:37 +00002463
Anders Carlsson77c23e52009-12-04 15:49:02 +00002464 Index = i->second;
2465 return true;
Anders Carlssonc0c49932009-12-04 03:41:37 +00002466 }
2467
Eli Friedman72649ed2009-12-06 22:01:30 +00002468 GlobalDecl getOrigMethod(uint64_t Index) const {
2469 return OrigMethods[Index];
2470 }
2471
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00002472 MethodsVectorTy::size_type size() const {
2473 return Methods.size();
2474 }
2475
2476 void clear() {
2477 MethodToIndexMap.clear();
2478 Methods.clear();
Eli Friedman72649ed2009-12-06 22:01:30 +00002479 OrigMethods.clear();
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00002480 }
2481
Anders Carlssonc0c49932009-12-04 03:41:37 +00002482 GlobalDecl operator[](uint64_t Index) const {
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00002483 return Methods[Index];
2484 }
2485 };
2486
2487 /// Methods - The vtable methods we're currently building.
2488 VtableMethods Methods;
2489
Anders Carlsson98fdb242009-12-04 02:26:15 +00002490 /// ThisAdjustments - For a given index in the vtable, contains the 'this'
2491 /// pointer adjustment needed for a method.
2492 typedef llvm::DenseMap<uint64_t, ThunkAdjustment> ThisAdjustmentsMapTy;
2493 ThisAdjustmentsMapTy ThisAdjustments;
Anders Carlsson5dd730a2009-11-26 19:32:45 +00002494
Eli Friedmanb455f0e2009-12-07 23:56:34 +00002495 SavedAdjustmentsVectorTy SavedAdjustments;
Eli Friedman72649ed2009-12-06 22:01:30 +00002496
Anders Carlssona8756702009-12-04 02:22:02 +00002497 /// BaseReturnTypes - Contains the base return types of methods who have been
2498 /// overridden with methods whose return types require adjustment. Used for
2499 /// generating covariant thunk information.
2500 typedef llvm::DenseMap<uint64_t, CanQualType> BaseReturnTypesMapTy;
2501 BaseReturnTypesMapTy BaseReturnTypes;
Anders Carlsson5dd730a2009-11-26 19:32:45 +00002502
Anders Carlssondbd920c2009-10-11 22:13:54 +00002503 std::vector<Index_t> VCalls;
Mike Stump9840c702009-11-12 20:47:57 +00002504
2505 typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t;
Mike Stump23a35422009-11-19 20:52:19 +00002506 // subAddressPoints - Used to hold the AddressPoints (offsets) into the built
2507 // vtable for use in computing the initializers for the VTT.
2508 llvm::DenseMap<CtorVtable_t, int64_t> &subAddressPoints;
Mike Stump9840c702009-11-12 20:47:57 +00002509
Anders Carlsson1bb60992010-01-14 02:29:07 +00002510 /// AddressPoints - Address points for this vtable.
Anders Carlssonff143f82010-03-25 00:35:49 +00002511 AddressPointsMapTy& AddressPoints;
Anders Carlsson1bb60992010-01-14 02:29:07 +00002512
Anders Carlssondbd920c2009-10-11 22:13:54 +00002513 typedef CXXRecordDecl::method_iterator method_iter;
Anders Carlssondbd920c2009-10-11 22:13:54 +00002514 const uint32_t LLVMPointerWidth;
2515 Index_t extra;
Mike Stump11dea942009-10-15 02:04:03 +00002516 typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t;
Mike Stump23a35422009-11-19 20:52:19 +00002517 static llvm::DenseMap<CtorVtable_t, int64_t>&
2518 AllocAddressPoint(CodeGenModule &cgm, const CXXRecordDecl *l,
2519 const CXXRecordDecl *c) {
Anders Carlsson66d567d2010-03-25 00:51:13 +00002520 CodeGenVTables::AddrMap_t *&oref = cgm.getVTables().OldAddressPoints[l];
Mike Stump23a35422009-11-19 20:52:19 +00002521 if (oref == 0)
Anders Carlssonaf440352010-03-23 04:11:45 +00002522 oref = new CodeGenVTables::AddrMap_t;
Mike Stump23a35422009-11-19 20:52:19 +00002523
2524 llvm::DenseMap<CtorVtable_t, int64_t> *&ref = (*oref)[c];
2525 if (ref == 0)
2526 ref = new llvm::DenseMap<CtorVtable_t, int64_t>;
2527 return *ref;
2528 }
Anders Carlsson6d4ccb72009-11-26 19:54:33 +00002529
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002530 bool DclIsSame(const FunctionDecl *New, const FunctionDecl *Old) {
2531 FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate();
2532 FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate();
2533
2534 // C++ [temp.fct]p2:
2535 // A function template can be overloaded with other function templates
2536 // and with normal (non-template) functions.
2537 if ((OldTemplate == 0) != (NewTemplate == 0))
2538 return false;
2539
2540 // Is the function New an overload of the function Old?
2541 QualType OldQType = CGM.getContext().getCanonicalType(Old->getType());
2542 QualType NewQType = CGM.getContext().getCanonicalType(New->getType());
2543
2544 // Compare the signatures (C++ 1.3.10) of the two functions to
2545 // determine whether they are overloads. If we find any mismatch
2546 // in the signature, they are overloads.
2547
2548 // If either of these functions is a K&R-style function (no
2549 // prototype), then we consider them to have matching signatures.
2550 if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) ||
2551 isa<FunctionNoProtoType>(NewQType.getTypePtr()))
2552 return true;
2553
2554 FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType);
2555 FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType);
2556
2557 // The signature of a function includes the types of its
2558 // parameters (C++ 1.3.10), which includes the presence or absence
2559 // of the ellipsis; see C++ DR 357).
2560 if (OldQType != NewQType &&
2561 (OldType->getNumArgs() != NewType->getNumArgs() ||
2562 OldType->isVariadic() != NewType->isVariadic() ||
2563 !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(),
2564 NewType->arg_type_begin())))
2565 return false;
2566
2567#if 0
2568 // C++ [temp.over.link]p4:
2569 // The signature of a function template consists of its function
2570 // signature, its return type and its template parameter list. The names
2571 // of the template parameters are significant only for establishing the
2572 // relationship between the template parameters and the rest of the
2573 // signature.
2574 //
2575 // We check the return type and template parameter lists for function
2576 // templates first; the remaining checks follow.
2577 if (NewTemplate &&
2578 (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(),
2579 OldTemplate->getTemplateParameters(),
2580 TPL_TemplateMatch) ||
2581 OldType->getResultType() != NewType->getResultType()))
2582 return false;
2583#endif
2584
2585 // If the function is a class member, its signature includes the
2586 // cv-qualifiers (if any) on the function itself.
2587 //
2588 // As part of this, also check whether one of the member functions
2589 // is static, in which case they are not overloads (C++
2590 // 13.1p2). While not part of the definition of the signature,
2591 // this check is important to determine whether these functions
2592 // can be overloaded.
2593 const CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old);
2594 const CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New);
2595 if (OldMethod && NewMethod &&
2596 !OldMethod->isStatic() && !NewMethod->isStatic() &&
2597 OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers())
2598 return false;
2599
2600 // The signatures match; this is not an overload.
2601 return true;
2602 }
2603
2604 typedef llvm::DenseMap<const CXXMethodDecl *, const CXXMethodDecl*>
2605 ForwardUnique_t;
2606 ForwardUnique_t ForwardUnique;
2607 llvm::DenseMap<const CXXMethodDecl*, const CXXMethodDecl*> UniqueOverrider;
2608
2609 void BuildUniqueOverrider(const CXXMethodDecl *U, const CXXMethodDecl *MD) {
2610 const CXXMethodDecl *PrevU = UniqueOverrider[MD];
2611 assert(U && "no unique overrider");
2612 if (PrevU == U)
2613 return;
2614 if (PrevU != U && PrevU != 0) {
2615 // If already set, note the two sets as the same
2616 if (0)
2617 printf("%s::%s same as %s::%s\n",
Douglas Gregoraef605d2010-02-22 16:44:27 +00002618 PrevU->getParent()->getNameAsString().c_str(),
2619 PrevU->getNameAsString().c_str(),
2620 U->getParent()->getNameAsString().c_str(),
2621 U->getNameAsString().c_str());
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002622 ForwardUnique[PrevU] = U;
2623 return;
2624 }
2625
2626 // Not set, set it now
2627 if (0)
2628 printf("marking %s::%s %p override as %s::%s\n",
Douglas Gregoraef605d2010-02-22 16:44:27 +00002629 MD->getParent()->getNameAsString().c_str(),
2630 MD->getNameAsString().c_str(),
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002631 (void*)MD,
Douglas Gregoraef605d2010-02-22 16:44:27 +00002632 U->getParent()->getNameAsString().c_str(),
2633 U->getNameAsString().c_str());
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002634 UniqueOverrider[MD] = U;
2635
2636 for (CXXMethodDecl::method_iterator mi = MD->begin_overridden_methods(),
2637 me = MD->end_overridden_methods(); mi != me; ++mi) {
2638 BuildUniqueOverrider(U, *mi);
2639 }
2640 }
2641
2642 void BuildUniqueOverriders(const CXXRecordDecl *RD) {
2643 if (0) printf("walking %s\n", RD->getNameAsCString());
2644 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
2645 e = RD->method_end(); i != e; ++i) {
2646 const CXXMethodDecl *MD = *i;
2647 if (!MD->isVirtual())
2648 continue;
2649
2650 if (UniqueOverrider[MD] == 0) {
2651 // Only set this, if it hasn't been set yet.
2652 BuildUniqueOverrider(MD, MD);
2653 if (0)
2654 printf("top set is %s::%s %p\n",
Douglas Gregoraef605d2010-02-22 16:44:27 +00002655 MD->getParent()->getNameAsString().c_str(),
2656 MD->getNameAsString().c_str(),
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002657 (void*)MD);
2658 ForwardUnique[MD] = MD;
2659 }
2660 }
2661 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
2662 e = RD->bases_end(); i != e; ++i) {
2663 const CXXRecordDecl *Base =
2664 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
2665 BuildUniqueOverriders(Base);
2666 }
2667 }
2668
2669 static int DclCmp(const void *p1, const void *p2) {
John McCall7fe0b9e2010-02-13 01:04:05 +00002670 const CXXMethodDecl *MD1 = *(const CXXMethodDecl *const *)p1;
2671 const CXXMethodDecl *MD2 = *(const CXXMethodDecl *const *)p2;
2672
2673 return (DeclarationName::compare(MD1->getDeclName(), MD2->getDeclName()));
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002674 }
2675
2676 void MergeForwarding() {
2677 typedef llvm::SmallVector<const CXXMethodDecl *, 100> A_t;
2678 A_t A;
2679 for (ForwardUnique_t::iterator I = ForwardUnique.begin(),
2680 E = ForwardUnique.end(); I != E; ++I) {
2681 if (I->first == I->second)
2682 // Only add the roots of all trees
2683 A.push_back(I->first);
2684 }
2685 llvm::array_pod_sort(A.begin(), A.end(), DclCmp);
2686 for (A_t::iterator I = A.begin(),
2687 E = A.end(); I != E; ++I) {
2688 A_t::iterator J = I;
John McCall7fe0b9e2010-02-13 01:04:05 +00002689 while (++J != E && DclCmp(I, J) == 0)
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002690 if (DclIsSame(*I, *J)) {
Douglas Gregoraef605d2010-02-22 16:44:27 +00002691 if (0) printf("connecting %s\n", (*I)->getNameAsString().c_str());
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002692 ForwardUnique[*J] = *I;
2693 }
2694 }
2695 }
2696
2697 const CXXMethodDecl *getUnique(const CXXMethodDecl *MD) {
2698 const CXXMethodDecl *U = UniqueOverrider[MD];
2699 assert(U && "unique overrider not found");
2700 while (ForwardUnique.count(U)) {
2701 const CXXMethodDecl *NU = ForwardUnique[U];
2702 if (NU == U) break;
2703 U = NU;
2704 }
2705 return U;
2706 }
Anders Carlsson0f0bbbc2010-01-26 17:36:47 +00002707
2708 GlobalDecl getUnique(GlobalDecl GD) {
2709 const CXXMethodDecl *Unique = getUnique(cast<CXXMethodDecl>(GD.getDecl()));
2710
2711 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Unique))
2712 return GlobalDecl(CD, GD.getCtorType());
2713
2714 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(Unique))
2715 return GlobalDecl(DD, GD.getDtorType());
2716
2717 return Unique;
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002718 }
2719
Anders Carlsson6d4ccb72009-11-26 19:54:33 +00002720 /// getPureVirtualFn - Return the __cxa_pure_virtual function.
2721 llvm::Constant* getPureVirtualFn() {
2722 if (!PureVirtualFn) {
2723 const llvm::FunctionType *Ty =
2724 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
2725 /*isVarArg=*/false);
2726 PureVirtualFn = wrap(CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual"));
2727 }
2728
2729 return PureVirtualFn;
2730 }
2731
Anders Carlssondbd920c2009-10-11 22:13:54 +00002732public:
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002733 OldVtableBuilder(const CXXRecordDecl *MostDerivedClass,
Eli Friedman152b5b12009-12-05 01:05:03 +00002734 const CXXRecordDecl *l, uint64_t lo, CodeGenModule &cgm,
Anders Carlssonff143f82010-03-25 00:35:49 +00002735 bool build, AddressPointsMapTy& AddressPoints)
Eli Friedman152b5b12009-12-05 01:05:03 +00002736 : BuildVtable(build), MostDerivedClass(MostDerivedClass), LayoutClass(l),
2737 LayoutOffset(lo), BLayout(cgm.getContext().getASTRecordLayout(l)),
2738 rtti(0), VMContext(cgm.getModule().getContext()),CGM(cgm),
2739 PureVirtualFn(0),
Anders Carlssonac3f7bd2009-12-04 18:36:22 +00002740 subAddressPoints(AllocAddressPoint(cgm, l, MostDerivedClass)),
Anders Carlsson1bb60992010-01-14 02:29:07 +00002741 AddressPoints(AddressPoints),
2742 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0))
2743 {
Anders Carlssondbd920c2009-10-11 22:13:54 +00002744 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Anders Carlsson1d7088d2009-12-17 07:09:17 +00002745 if (BuildVtable) {
2746 QualType ClassType = CGM.getContext().getTagDeclType(MostDerivedClass);
2747 rtti = CGM.GetAddrOfRTTIDescriptor(ClassType);
2748 }
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002749 BuildUniqueOverriders(MostDerivedClass);
2750 MergeForwarding();
Anders Carlssondbd920c2009-10-11 22:13:54 +00002751 }
2752
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00002753 // getVtableComponents - Returns a reference to the vtable components.
2754 const VtableComponentsVectorTy &getVtableComponents() const {
2755 return VtableComponents;
Anders Carlsson15318f42009-12-04 16:19:30 +00002756 }
2757
Anders Carlssonc997d422010-01-02 01:01:18 +00002758 llvm::DenseMap<const CXXRecordDecl *, uint64_t> &getVBIndex()
Anders Carlssondbd920c2009-10-11 22:13:54 +00002759 { return VBIndex; }
2760
Eli Friedmanb455f0e2009-12-07 23:56:34 +00002761 SavedAdjustmentsVectorTy &getSavedAdjustments()
2762 { return SavedAdjustments; }
Eli Friedman72649ed2009-12-06 22:01:30 +00002763
Anders Carlssondbd920c2009-10-11 22:13:54 +00002764 llvm::Constant *wrap(Index_t i) {
2765 llvm::Constant *m;
2766 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
2767 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
2768 }
2769
2770 llvm::Constant *wrap(llvm::Constant *m) {
2771 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
2772 }
2773
Mike Stumpd6584902010-01-22 02:51:26 +00002774//#define D1(x)
Douglas Gregor12a91092010-02-22 16:48:26 +00002775#define D1(X) do { if (getenv("CLANG_VTABLE_DEBUG")) { X; } } while (0)
Mike Stump6a9612f2009-10-31 20:06:59 +00002776
2777 void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset,
Mike Stumpab28c132009-10-13 22:54:56 +00002778 bool updateVBIndex, Index_t current_vbindex) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00002779 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
2780 e = RD->bases_end(); i != e; ++i) {
2781 const CXXRecordDecl *Base =
2782 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stumpab28c132009-10-13 22:54:56 +00002783 Index_t next_vbindex = current_vbindex;
Anders Carlssondbd920c2009-10-11 22:13:54 +00002784 if (i->isVirtual() && !SeenVBase.count(Base)) {
2785 SeenVBase.insert(Base);
Mike Stumpab28c132009-10-13 22:54:56 +00002786 if (updateVBIndex) {
Mike Stump6a9612f2009-10-31 20:06:59 +00002787 next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8)
Mike Stumpab28c132009-10-13 22:54:56 +00002788 - 3*LLVMPointerWidth/8);
2789 VBIndex[Base] = next_vbindex;
2790 }
Mike Stump6a9612f2009-10-31 20:06:59 +00002791 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
2792 VCalls.push_back((0?700:0) + BaseOffset);
2793 D1(printf(" vbase for %s at %d delta %d most derived %s\n",
2794 Base->getNameAsCString(),
2795 (int)-VCalls.size()-3, (int)BaseOffset,
Mike Stumpd6584902010-01-22 02:51:26 +00002796 MostDerivedClass->getNameAsCString()));
Anders Carlssondbd920c2009-10-11 22:13:54 +00002797 }
Mike Stumpab28c132009-10-13 22:54:56 +00002798 // We also record offsets for non-virtual bases to closest enclosing
2799 // virtual base. We do this so that we don't have to search
2800 // for the nearst virtual base class when generating thunks.
2801 if (updateVBIndex && VBIndex.count(Base) == 0)
2802 VBIndex[Base] = next_vbindex;
Mike Stump6a9612f2009-10-31 20:06:59 +00002803 GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex);
Anders Carlssondbd920c2009-10-11 22:13:54 +00002804 }
2805 }
2806
2807 void StartNewTable() {
2808 SeenVBase.clear();
2809 }
2810
Mike Stump3425b972009-10-15 09:30:16 +00002811 Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B,
2812 Index_t Offset = 0) {
2813
2814 if (B == D)
2815 return Offset;
2816
2817 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D);
2818 for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(),
2819 e = D->bases_end(); i != e; ++i) {
2820 const CXXRecordDecl *Base =
2821 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
2822 int64_t BaseOffset = 0;
2823 if (!i->isVirtual())
2824 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
2825 int64_t o = getNVOffset_1(Base, B, BaseOffset);
2826 if (o >= 0)
2827 return o;
2828 }
2829
2830 return -1;
2831 }
2832
2833 /// getNVOffset - Returns the non-virtual offset for the given (B) base of the
2834 /// derived class D.
2835 Index_t getNVOffset(QualType qB, QualType qD) {
Mike Stump9c212892009-11-03 19:03:17 +00002836 qD = qD->getPointeeType();
2837 qB = qB->getPointeeType();
Mike Stump3425b972009-10-15 09:30:16 +00002838 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
2839 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
2840 int64_t o = getNVOffset_1(D, B);
2841 if (o >= 0)
2842 return o;
2843
2844 assert(false && "FIXME: non-virtual base not found");
2845 return 0;
2846 }
2847
Anders Carlssondbd920c2009-10-11 22:13:54 +00002848 /// getVbaseOffset - Returns the index into the vtable for the virtual base
2849 /// offset for the given (B) virtual base of the derived class D.
2850 Index_t getVbaseOffset(QualType qB, QualType qD) {
Mike Stump9c212892009-11-03 19:03:17 +00002851 qD = qD->getPointeeType();
2852 qB = qB->getPointeeType();
Anders Carlssondbd920c2009-10-11 22:13:54 +00002853 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
2854 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
Anders Carlssonac3f7bd2009-12-04 18:36:22 +00002855 if (D != MostDerivedClass)
Anders Carlssonaf440352010-03-23 04:11:45 +00002856 return CGM.getVTables().getVirtualBaseOffsetOffset(D, B);
Anders Carlssondbd920c2009-10-11 22:13:54 +00002857 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
2858 i = VBIndex.find(B);
2859 if (i != VBIndex.end())
2860 return i->second;
Anders Carlssondbd920c2009-10-11 22:13:54 +00002861
Mike Stumpab28c132009-10-13 22:54:56 +00002862 assert(false && "FIXME: Base not found");
Anders Carlssondbd920c2009-10-11 22:13:54 +00002863 return 0;
2864 }
2865
Eli Friedman367d1222009-12-04 08:40:51 +00002866 bool OverrideMethod(GlobalDecl GD, bool MorallyVirtual,
2867 Index_t OverrideOffset, Index_t Offset,
2868 int64_t CurrentVBaseOffset);
Anders Carlssondbd920c2009-10-11 22:13:54 +00002869
Anders Carlssonadfa2672009-12-04 02:39:04 +00002870 /// AppendMethods - Append the current methods to the vtable.
Anders Carlssonbf540272009-12-04 02:56:03 +00002871 void AppendMethodsToVtable();
Anders Carlssonadfa2672009-12-04 02:39:04 +00002872
Anders Carlssona0fdd912009-11-13 17:08:56 +00002873 llvm::Constant *WrapAddrOf(GlobalDecl GD) {
2874 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2875
Anders Carlssonecf282b2009-11-24 05:08:52 +00002876 const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD);
Mike Stump1ae31782009-10-27 23:36:26 +00002877
Mike Stumpc085a982009-12-03 16:55:20 +00002878 return wrap(CGM.GetAddrOfFunction(GD, Ty));
Mike Stump1ae31782009-10-27 23:36:26 +00002879 }
2880
Mike Stump9e7e3c62009-11-06 23:27:42 +00002881 void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset,
2882 int64_t CurrentVBaseOffset) {
Mike Stump11dea942009-10-15 02:04:03 +00002883 for (Path_t::reverse_iterator i = Path->rbegin(),
Anders Carlssondbd920c2009-10-11 22:13:54 +00002884 e = Path->rend(); i != e; ++i) {
2885 const CXXRecordDecl *RD = i->first;
Mike Stump3425b972009-10-15 09:30:16 +00002886 int64_t OverrideOffset = i->second;
Anders Carlssondbd920c2009-10-11 22:13:54 +00002887 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
2888 ++mi) {
Anders Carlssona0fdd912009-11-13 17:08:56 +00002889 const CXXMethodDecl *MD = *mi;
2890
2891 if (!MD->isVirtual())
Anders Carlssondbd920c2009-10-11 22:13:54 +00002892 continue;
2893
Anders Carlssona0fdd912009-11-13 17:08:56 +00002894 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2895 // Override both the complete and the deleting destructor.
2896 GlobalDecl CompDtor(DD, Dtor_Complete);
Eli Friedman367d1222009-12-04 08:40:51 +00002897 OverrideMethod(CompDtor, MorallyVirtual, OverrideOffset, Offset,
2898 CurrentVBaseOffset);
2899
Anders Carlssona0fdd912009-11-13 17:08:56 +00002900 GlobalDecl DeletingDtor(DD, Dtor_Deleting);
Eli Friedman367d1222009-12-04 08:40:51 +00002901 OverrideMethod(DeletingDtor, MorallyVirtual, OverrideOffset, Offset,
2902 CurrentVBaseOffset);
Anders Carlssona0fdd912009-11-13 17:08:56 +00002903 } else {
Eli Friedman367d1222009-12-04 08:40:51 +00002904 OverrideMethod(MD, MorallyVirtual, OverrideOffset, Offset,
2905 CurrentVBaseOffset);
Anders Carlssona0fdd912009-11-13 17:08:56 +00002906 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00002907 }
2908 }
2909 }
2910
Anders Carlssona0fdd912009-11-13 17:08:56 +00002911 void AddMethod(const GlobalDecl GD, bool MorallyVirtual, Index_t Offset,
Eli Friedman76ed1f72009-11-30 01:19:33 +00002912 int64_t CurrentVBaseOffset) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00002913 // If we can find a previously allocated slot for this, reuse it.
Eli Friedman367d1222009-12-04 08:40:51 +00002914 if (OverrideMethod(GD, MorallyVirtual, Offset, Offset,
Mike Stump9e7e3c62009-11-06 23:27:42 +00002915 CurrentVBaseOffset))
Anders Carlssondbd920c2009-10-11 22:13:54 +00002916 return;
2917
Mike Stump372ade22010-01-22 18:48:47 +00002918 D1(printf(" vfn for %s at %d\n",
Douglas Gregoraef605d2010-02-22 16:44:27 +00002919 dyn_cast<CXXMethodDecl>(GD.getDecl())->getNameAsString().c_str(),
Mike Stump372ade22010-01-22 18:48:47 +00002920 (int)Methods.size()));
2921
Anders Carlssona7f19112009-12-04 02:08:24 +00002922 // We didn't find an entry in the vtable that we could use, add a new
2923 // entry.
2924 Methods.AddMethod(GD);
2925
Mike Stump15189fb2010-01-26 21:35:27 +00002926 VCallOffset[GD] = Offset/8 - CurrentVBaseOffset/8;
2927
Anders Carlssondbd920c2009-10-11 22:13:54 +00002928 if (MorallyVirtual) {
Anders Carlsson0f0bbbc2010-01-26 17:36:47 +00002929 GlobalDecl UGD = getUnique(GD);
2930 const CXXMethodDecl *UMD = cast<CXXMethodDecl>(UGD.getDecl());
2931
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002932 assert(UMD && "final overrider not found");
2933
2934 Index_t &idx = VCall[UMD];
Anders Carlssondbd920c2009-10-11 22:13:54 +00002935 // Allocate the first one, after that, we reuse the previous one.
2936 if (idx == 0) {
Anders Carlsson0f0bbbc2010-01-26 17:36:47 +00002937 VCallOffsetForVCall[UGD] = Offset/8;
Mike Stump15189fb2010-01-26 21:35:27 +00002938 NonVirtualOffset[UMD] = Offset/8 - CurrentVBaseOffset/8;
Anders Carlssondbd920c2009-10-11 22:13:54 +00002939 idx = VCalls.size()+1;
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002940 VCalls.push_back(Offset/8 - CurrentVBaseOffset/8);
Mike Stump6a9612f2009-10-31 20:06:59 +00002941 D1(printf(" vcall for %s at %d with delta %d\n",
Douglas Gregoraef605d2010-02-22 16:44:27 +00002942 dyn_cast<CXXMethodDecl>(GD.getDecl())->getNameAsString().c_str(),
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002943 (int)-VCalls.size()-3, (int)VCalls[idx-1]));
Anders Carlssondbd920c2009-10-11 22:13:54 +00002944 }
2945 }
2946 }
2947
2948 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
Eli Friedman76ed1f72009-11-30 01:19:33 +00002949 Index_t Offset, int64_t CurrentVBaseOffset) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00002950 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
Anders Carlssona0fdd912009-11-13 17:08:56 +00002951 ++mi) {
2952 const CXXMethodDecl *MD = *mi;
2953 if (!MD->isVirtual())
2954 continue;
2955
2956 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2957 // For destructors, add both the complete and the deleting destructor
2958 // to the vtable.
2959 AddMethod(GlobalDecl(DD, Dtor_Complete), MorallyVirtual, Offset,
Mike Stump9e7e3c62009-11-06 23:27:42 +00002960 CurrentVBaseOffset);
Eli Friedman76ed1f72009-11-30 01:19:33 +00002961 AddMethod(GlobalDecl(DD, Dtor_Deleting), MorallyVirtual, Offset,
2962 CurrentVBaseOffset);
2963 } else
2964 AddMethod(MD, MorallyVirtual, Offset, CurrentVBaseOffset);
Anders Carlssona0fdd912009-11-13 17:08:56 +00002965 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00002966 }
2967
2968 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
2969 const CXXRecordDecl *PrimaryBase,
2970 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
Mike Stump9e7e3c62009-11-06 23:27:42 +00002971 int64_t Offset, int64_t CurrentVBaseOffset,
2972 Path_t *Path) {
Mike Stump11dea942009-10-15 02:04:03 +00002973 Path->push_back(std::make_pair(RD, Offset));
Anders Carlssondbd920c2009-10-11 22:13:54 +00002974 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
2975 e = RD->bases_end(); i != e; ++i) {
2976 if (i->isVirtual())
2977 continue;
2978 const CXXRecordDecl *Base =
2979 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stumpfc9f16c2010-01-22 06:45:05 +00002980 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
2981 StartNewTable();
2982 GenerateVtableForBase(Base, o, MorallyVirtual, false,
2983 true, Base == PrimaryBase && !PrimaryBaseWasVirtual,
2984 CurrentVBaseOffset, Path);
Anders Carlssondbd920c2009-10-11 22:13:54 +00002985 }
Mike Stump11dea942009-10-15 02:04:03 +00002986 Path->pop_back();
Anders Carlssondbd920c2009-10-11 22:13:54 +00002987 }
2988
Mike Stump0ca42792009-10-14 18:14:51 +00002989// #define D(X) do { X; } while (0)
2990#define D(X)
2991
2992 void insertVCalls(int InsertionPoint) {
Mike Stump6a9612f2009-10-31 20:06:59 +00002993 D1(printf("============= combining vbase/vcall\n"));
Mike Stump0ca42792009-10-14 18:14:51 +00002994 D(VCalls.insert(VCalls.begin(), 673));
2995 D(VCalls.push_back(672));
Eli Friedman152b5b12009-12-05 01:05:03 +00002996
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00002997 VtableComponents.insert(VtableComponents.begin() + InsertionPoint,
2998 VCalls.size(), 0);
Eli Friedman152b5b12009-12-05 01:05:03 +00002999 if (BuildVtable) {
3000 // The vcalls come first...
3001 for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(),
3002 e = VCalls.rend();
3003 i != e; ++i)
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00003004 VtableComponents[InsertionPoint++] = wrap((0?600:0) + *i);
Eli Friedman152b5b12009-12-05 01:05:03 +00003005 }
Mike Stump0ca42792009-10-14 18:14:51 +00003006 VCalls.clear();
Mike Stumpfbfb52d2009-11-10 02:30:51 +00003007 VCall.clear();
Mike Stump15189fb2010-01-26 21:35:27 +00003008 VCallOffsetForVCall.clear();
3009 VCallOffset.clear();
3010 NonVirtualOffset.clear();
Mike Stump0ca42792009-10-14 18:14:51 +00003011 }
3012
Mike Stump65d0e282009-11-13 23:13:20 +00003013 void AddAddressPoints(const CXXRecordDecl *RD, uint64_t Offset,
3014 Index_t AddressPoint) {
3015 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
Mike Stumpd6584902010-01-22 02:51:26 +00003016 RD->getNameAsCString(), MostDerivedClass->getNameAsCString(),
Mike Stump65d0e282009-11-13 23:13:20 +00003017 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stump23a35422009-11-19 20:52:19 +00003018 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Anders Carlsson1bb60992010-01-14 02:29:07 +00003019 AddressPoints[BaseSubobject(RD, Offset)] = AddressPoint;
Mike Stump65d0e282009-11-13 23:13:20 +00003020
3021 // Now also add the address point for all our primary bases.
3022 while (1) {
3023 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
3024 RD = Layout.getPrimaryBase();
3025 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
3026 // FIXME: Double check this.
3027 if (RD == 0)
3028 break;
3029 if (PrimaryBaseWasVirtual &&
3030 BLayout.getVBaseClassOffset(RD) != Offset)
3031 break;
3032 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
Mike Stumpd6584902010-01-22 02:51:26 +00003033 RD->getNameAsCString(), MostDerivedClass->getNameAsCString(),
Mike Stump65d0e282009-11-13 23:13:20 +00003034 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stump23a35422009-11-19 20:52:19 +00003035 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Anders Carlsson1bb60992010-01-14 02:29:07 +00003036 AddressPoints[BaseSubobject(RD, Offset)] = AddressPoint;
Mike Stump65d0e282009-11-13 23:13:20 +00003037 }
3038 }
3039
3040
Mike Stump8cc4f102009-12-24 07:29:41 +00003041 void FinishGenerateVtable(const CXXRecordDecl *RD,
3042 const ASTRecordLayout &Layout,
3043 const CXXRecordDecl *PrimaryBase,
Mike Stumpfc9f16c2010-01-22 06:45:05 +00003044 bool ForNPNVBases, bool WasPrimaryBase,
Mike Stump8cc4f102009-12-24 07:29:41 +00003045 bool PrimaryBaseWasVirtual,
3046 bool MorallyVirtual, int64_t Offset,
3047 bool ForVirtualBase, int64_t CurrentVBaseOffset,
3048 Path_t *Path) {
Mike Stump11dea942009-10-15 02:04:03 +00003049 bool alloc = false;
3050 if (Path == 0) {
3051 alloc = true;
3052 Path = new Path_t;
3053 }
3054
Anders Carlssondbd920c2009-10-11 22:13:54 +00003055 StartNewTable();
3056 extra = 0;
Mike Stumpfc9f16c2010-01-22 06:45:05 +00003057 Index_t AddressPoint = 0;
3058 int VCallInsertionPoint = 0;
3059 if (!ForNPNVBases || !WasPrimaryBase) {
3060 bool DeferVCalls = MorallyVirtual || ForVirtualBase;
3061 VCallInsertionPoint = VtableComponents.size();
3062 if (!DeferVCalls) {
3063 insertVCalls(VCallInsertionPoint);
3064 } else
3065 // FIXME: just for extra, or for all uses of VCalls.size post this?
3066 extra = -VCalls.size();
Anders Carlssondbd920c2009-10-11 22:13:54 +00003067
Mike Stumpfc9f16c2010-01-22 06:45:05 +00003068 // Add the offset to top.
3069 VtableComponents.push_back(BuildVtable ? wrap(-((Offset-LayoutOffset)/8)) : 0);
Anders Carlsson15318f42009-12-04 16:19:30 +00003070
Mike Stumpfc9f16c2010-01-22 06:45:05 +00003071 // Add the RTTI information.
3072 VtableComponents.push_back(rtti);
Anders Carlsson15318f42009-12-04 16:19:30 +00003073
Mike Stumpfc9f16c2010-01-22 06:45:05 +00003074 AddressPoint = VtableComponents.size();
Anders Carlssondbd920c2009-10-11 22:13:54 +00003075
Mike Stumpfc9f16c2010-01-22 06:45:05 +00003076 AppendMethodsToVtable();
3077 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00003078
3079 // and then the non-virtual bases.
3080 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
Mike Stump9e7e3c62009-11-06 23:27:42 +00003081 MorallyVirtual, Offset, CurrentVBaseOffset, Path);
Mike Stump0ca42792009-10-14 18:14:51 +00003082
3083 if (ForVirtualBase) {
Mike Stump9840c702009-11-12 20:47:57 +00003084 // FIXME: We're adding to VCalls in callers, we need to do the overrides
3085 // in the inner part, so that we know the complete set of vcalls during
3086 // the build and don't have to insert into methods. Saving out the
3087 // AddressPoint here, would need to be fixed, if we didn't do that. Also
3088 // retroactively adding vcalls for overrides later wind up in the wrong
3089 // place, the vcall slot has to be alloted during the walk of the base
3090 // when the function is first introduces.
Mike Stump0ca42792009-10-14 18:14:51 +00003091 AddressPoint += VCalls.size();
Mike Stump9840c702009-11-12 20:47:57 +00003092 insertVCalls(VCallInsertionPoint);
Mike Stump0ca42792009-10-14 18:14:51 +00003093 }
3094
Mike Stumpfc9f16c2010-01-22 06:45:05 +00003095 if (!ForNPNVBases || !WasPrimaryBase)
3096 AddAddressPoints(RD, Offset, AddressPoint);
Mike Stump9840c702009-11-12 20:47:57 +00003097
Mike Stump11dea942009-10-15 02:04:03 +00003098 if (alloc) {
3099 delete Path;
3100 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00003101 }
3102
Mike Stump6a9612f2009-10-31 20:06:59 +00003103 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
3104 bool updateVBIndex, Index_t current_vbindex,
Eli Friedman76ed1f72009-11-30 01:19:33 +00003105 int64_t CurrentVBaseOffset) {
Mike Stump6a9612f2009-10-31 20:06:59 +00003106 if (!RD->isDynamicClass())
3107 return;
3108
3109 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
3110 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
3111 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
3112
3113 // vtables are composed from the chain of primaries.
Eli Friedmandfe33bb2009-12-04 08:52:11 +00003114 if (PrimaryBase && !PrimaryBaseWasVirtual) {
Mike Stump6a9612f2009-10-31 20:06:59 +00003115 D1(printf(" doing primaries for %s most derived %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00003116 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Eli Friedmandfe33bb2009-12-04 08:52:11 +00003117 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
3118 updateVBIndex, current_vbindex, CurrentVBaseOffset);
Mike Stump6a9612f2009-10-31 20:06:59 +00003119 }
3120
3121 D1(printf(" doing vcall entries for %s most derived %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00003122 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Mike Stump6a9612f2009-10-31 20:06:59 +00003123
3124 // And add the virtuals for the class to the primary vtable.
Eli Friedman76ed1f72009-11-30 01:19:33 +00003125 AddMethods(RD, MorallyVirtual, Offset, CurrentVBaseOffset);
Mike Stump6a9612f2009-10-31 20:06:59 +00003126 }
3127
3128 void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
3129 bool updateVBIndex, Index_t current_vbindex,
Mike Stump9e7e3c62009-11-06 23:27:42 +00003130 bool RDisVirtualBase, int64_t CurrentVBaseOffset,
Eli Friedman76ed1f72009-11-30 01:19:33 +00003131 bool bottom) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00003132 if (!RD->isDynamicClass())
3133 return;
3134
3135 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
3136 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
3137 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
3138
3139 // vtables are composed from the chain of primaries.
3140 if (PrimaryBase) {
Mike Stump9e7e3c62009-11-06 23:27:42 +00003141 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
3142 if (PrimaryBaseWasVirtual) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00003143 IndirectPrimary.insert(PrimaryBase);
Mike Stump9e7e3c62009-11-06 23:27:42 +00003144 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
3145 }
Mike Stump6a9612f2009-10-31 20:06:59 +00003146
3147 D1(printf(" doing primaries for %s most derived %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00003148 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Mike Stump6a9612f2009-10-31 20:06:59 +00003149
3150 VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Mike Stump9e7e3c62009-11-06 23:27:42 +00003151 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
Eli Friedman76ed1f72009-11-30 01:19:33 +00003152 BaseCurrentVBaseOffset, false);
Anders Carlssondbd920c2009-10-11 22:13:54 +00003153 }
3154
Mike Stump6a9612f2009-10-31 20:06:59 +00003155 D1(printf(" doing vbase entries for %s most derived %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00003156 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Mike Stump6a9612f2009-10-31 20:06:59 +00003157 GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex);
3158
3159 if (RDisVirtualBase || bottom) {
3160 Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex,
Eli Friedman76ed1f72009-11-30 01:19:33 +00003161 CurrentVBaseOffset);
Mike Stump6a9612f2009-10-31 20:06:59 +00003162 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00003163 }
3164
Mike Stump8cc4f102009-12-24 07:29:41 +00003165 void GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0,
3166 bool MorallyVirtual = false,
3167 bool ForVirtualBase = false,
Mike Stumpfc9f16c2010-01-22 06:45:05 +00003168 bool ForNPNVBases = false,
3169 bool WasPrimaryBase = true,
Mike Stump8cc4f102009-12-24 07:29:41 +00003170 int CurrentVBaseOffset = 0,
3171 Path_t *Path = 0) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00003172 if (!RD->isDynamicClass())
Mike Stump8cc4f102009-12-24 07:29:41 +00003173 return;
Anders Carlssondbd920c2009-10-11 22:13:54 +00003174
Mike Stump92774d12009-11-13 02:35:38 +00003175 // Construction vtable don't need parts that have no virtual bases and
3176 // aren't morally virtual.
Anders Carlssonac3f7bd2009-12-04 18:36:22 +00003177 if ((LayoutClass != MostDerivedClass) &&
3178 RD->getNumVBases() == 0 && !MorallyVirtual)
Mike Stump8cc4f102009-12-24 07:29:41 +00003179 return;
Mike Stump92774d12009-11-13 02:35:38 +00003180
Anders Carlssondbd920c2009-10-11 22:13:54 +00003181 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
3182 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
3183 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
3184
Anders Carlssondbd920c2009-10-11 22:13:54 +00003185 extra = 0;
Mike Stump6a9612f2009-10-31 20:06:59 +00003186 D1(printf("building entries for base %s most derived %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00003187 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Anders Carlssondbd920c2009-10-11 22:13:54 +00003188
Mike Stump6a9612f2009-10-31 20:06:59 +00003189 if (ForVirtualBase)
3190 extra = VCalls.size();
3191
Mike Stumpfc9f16c2010-01-22 06:45:05 +00003192 if (!ForNPNVBases || !WasPrimaryBase) {
3193 VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0,
3194 ForVirtualBase, CurrentVBaseOffset, true);
Anders Carlssondbd920c2009-10-11 22:13:54 +00003195
Mike Stumpfc9f16c2010-01-22 06:45:05 +00003196 if (Path)
3197 OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset);
3198 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00003199
Mike Stumpfc9f16c2010-01-22 06:45:05 +00003200 FinishGenerateVtable(RD, Layout, PrimaryBase, ForNPNVBases, WasPrimaryBase,
3201 PrimaryBaseWasVirtual, MorallyVirtual, Offset,
3202 ForVirtualBase, CurrentVBaseOffset, Path);
Anders Carlssondbd920c2009-10-11 22:13:54 +00003203 }
3204
3205 void GenerateVtableForVBases(const CXXRecordDecl *RD,
3206 int64_t Offset = 0,
Mike Stump11dea942009-10-15 02:04:03 +00003207 Path_t *Path = 0) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00003208 bool alloc = false;
3209 if (Path == 0) {
3210 alloc = true;
Mike Stump11dea942009-10-15 02:04:03 +00003211 Path = new Path_t;
Anders Carlssondbd920c2009-10-11 22:13:54 +00003212 }
3213 // FIXME: We also need to override using all paths to a virtual base,
3214 // right now, we just process the first path
3215 Path->push_back(std::make_pair(RD, Offset));
3216 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
3217 e = RD->bases_end(); i != e; ++i) {
3218 const CXXRecordDecl *Base =
3219 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
3220 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
3221 // Mark it so we don't output it twice.
3222 IndirectPrimary.insert(Base);
3223 StartNewTable();
Mike Stump0ca42792009-10-14 18:14:51 +00003224 VCall.clear();
Anders Carlssondbd920c2009-10-11 22:13:54 +00003225 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump9e7e3c62009-11-06 23:27:42 +00003226 int64_t CurrentVBaseOffset = BaseOffset;
Mike Stump6a9612f2009-10-31 20:06:59 +00003227 D1(printf("vtable %s virtual base %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00003228 MostDerivedClass->getNameAsCString(), Base->getNameAsCString()));
Mike Stumpfc9f16c2010-01-22 06:45:05 +00003229 GenerateVtableForBase(Base, BaseOffset, true, true, false,
3230 true, CurrentVBaseOffset, Path);
Anders Carlssondbd920c2009-10-11 22:13:54 +00003231 }
Mike Stump9840c702009-11-12 20:47:57 +00003232 int64_t BaseOffset;
Anders Carlssondbd920c2009-10-11 22:13:54 +00003233 if (i->isVirtual())
3234 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump9840c702009-11-12 20:47:57 +00003235 else {
3236 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
3237 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
3238 }
3239
Mike Stump11dea942009-10-15 02:04:03 +00003240 if (Base->getNumVBases()) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00003241 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump11dea942009-10-15 02:04:03 +00003242 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00003243 }
3244 Path->pop_back();
3245 if (alloc)
3246 delete Path;
3247 }
3248};
Anders Carlsson27682a32009-12-03 01:54:02 +00003249} // end anonymous namespace
3250
Anders Carlsson824d7ea2010-02-11 08:02:13 +00003251bool OldVtableBuilder::OverrideMethod(GlobalDecl GD, bool MorallyVirtual,
Eli Friedman367d1222009-12-04 08:40:51 +00003252 Index_t OverrideOffset, Index_t Offset,
3253 int64_t CurrentVBaseOffset) {
Anders Carlsson27682a32009-12-03 01:54:02 +00003254 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
3255
3256 const bool isPure = MD->isPure();
Anders Carlsson7ca46432009-12-05 17:04:47 +00003257
Anders Carlsson27682a32009-12-03 01:54:02 +00003258 // FIXME: Should OverrideOffset's be Offset?
3259
Anders Carlsson7ca46432009-12-05 17:04:47 +00003260 for (CXXMethodDecl::method_iterator mi = MD->begin_overridden_methods(),
3261 e = MD->end_overridden_methods(); mi != e; ++mi) {
Anders Carlsson27682a32009-12-03 01:54:02 +00003262 GlobalDecl OGD;
Mike Stump15189fb2010-01-26 21:35:27 +00003263 GlobalDecl OGD2;
Anders Carlsson27682a32009-12-03 01:54:02 +00003264
Anders Carlsson3aaf4862009-12-04 05:51:56 +00003265 const CXXMethodDecl *OMD = *mi;
Anders Carlsson27682a32009-12-03 01:54:02 +00003266 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(OMD))
3267 OGD = GlobalDecl(DD, GD.getDtorType());
3268 else
3269 OGD = OMD;
Eli Friedman47145832009-12-04 08:34:14 +00003270
Eli Friedman72649ed2009-12-06 22:01:30 +00003271 // Check whether this is the method being overridden in this section of
3272 // the vtable.
Anders Carlsson77c23e52009-12-04 15:49:02 +00003273 uint64_t Index;
3274 if (!Methods.getIndex(OGD, Index))
Eli Friedman47145832009-12-04 08:34:14 +00003275 continue;
3276
Mike Stump15189fb2010-01-26 21:35:27 +00003277 OGD2 = OGD;
3278
Eli Friedman72649ed2009-12-06 22:01:30 +00003279 // Get the original method, which we should be computing thunks, etc,
3280 // against.
3281 OGD = Methods.getOrigMethod(Index);
3282 OMD = cast<CXXMethodDecl>(OGD.getDecl());
3283
Eli Friedman47145832009-12-04 08:34:14 +00003284 QualType ReturnType =
3285 MD->getType()->getAs<FunctionType>()->getResultType();
3286 QualType OverriddenReturnType =
3287 OMD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlsson27682a32009-12-03 01:54:02 +00003288
Eli Friedman47145832009-12-04 08:34:14 +00003289 // Check if we need a return type adjustment.
Anders Carlssondf31af12010-02-13 23:17:31 +00003290 if (!ComputeReturnAdjustmentBaseOffset(CGM.getContext(), MD,
3291 OMD).isEmpty()) {
Eli Friedman47145832009-12-04 08:34:14 +00003292 CanQualType &BaseReturnType = BaseReturnTypes[Index];
Anders Carlsson27682a32009-12-03 01:54:02 +00003293
Eli Friedman47145832009-12-04 08:34:14 +00003294 // Insert the base return type.
3295 if (BaseReturnType.isNull())
3296 BaseReturnType =
3297 CGM.getContext().getCanonicalType(OverriddenReturnType);
3298 }
Anders Carlssondd454be2009-12-04 03:52:52 +00003299
Eli Friedman47145832009-12-04 08:34:14 +00003300 Methods.OverrideMethod(OGD, GD);
3301
Anders Carlsson0f0bbbc2010-01-26 17:36:47 +00003302 GlobalDecl UGD = getUnique(GD);
3303 const CXXMethodDecl *UMD = cast<CXXMethodDecl>(UGD.getDecl());
3304 assert(UGD.getDecl() && "unique overrider not found");
3305 assert(UGD == getUnique(OGD) && "unique overrider not unique");
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00003306
Eli Friedman47145832009-12-04 08:34:14 +00003307 ThisAdjustments.erase(Index);
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00003308 if (MorallyVirtual || VCall.count(UMD)) {
Mike Stump15189fb2010-01-26 21:35:27 +00003309
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00003310 Index_t &idx = VCall[UMD];
Eli Friedman47145832009-12-04 08:34:14 +00003311 if (idx == 0) {
Mike Stump15189fb2010-01-26 21:35:27 +00003312 VCallOffset[GD] = VCallOffset[OGD];
3313 // NonVirtualOffset[UMD] = CurrentVBaseOffset/8 - OverrideOffset/8;
3314 NonVirtualOffset[UMD] = VCallOffset[OGD];
Mike Stumpd99a4d22010-01-26 03:42:22 +00003315 VCallOffsetForVCall[UMD] = OverrideOffset/8;
Eli Friedman47145832009-12-04 08:34:14 +00003316 idx = VCalls.size()+1;
Mike Stumpd99a4d22010-01-26 03:42:22 +00003317 VCalls.push_back(OverrideOffset/8 - CurrentVBaseOffset/8);
Eli Friedman47145832009-12-04 08:34:14 +00003318 D1(printf(" vcall for %s at %d with delta %d most derived %s\n",
3319 MD->getNameAsString().c_str(), (int)-idx-3,
Mike Stumpd6584902010-01-22 02:51:26 +00003320 (int)VCalls[idx-1], MostDerivedClass->getNameAsCString()));
Eli Friedman47145832009-12-04 08:34:14 +00003321 } else {
Mike Stump15189fb2010-01-26 21:35:27 +00003322 VCallOffset[GD] = NonVirtualOffset[UMD];
Anders Carlsson0f0bbbc2010-01-26 17:36:47 +00003323 VCalls[idx-1] = -VCallOffsetForVCall[UGD] + OverrideOffset/8;
Eli Friedman47145832009-12-04 08:34:14 +00003324 D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n",
3325 MD->getNameAsString().c_str(), (int)-idx-3,
Mike Stumpd6584902010-01-22 02:51:26 +00003326 (int)VCalls[idx-1], MostDerivedClass->getNameAsCString()));
Eli Friedman47145832009-12-04 08:34:14 +00003327 }
Mike Stump15189fb2010-01-26 21:35:27 +00003328 int64_t NonVirtualAdjustment = -VCallOffset[OGD];
Mike Stump852f5ce2010-01-26 22:44:01 +00003329 QualType DerivedType = MD->getThisType(CGM.getContext());
3330 QualType BaseType = cast<const CXXMethodDecl>(OGD.getDecl())->getThisType(CGM.getContext());
3331 int64_t NonVirtualAdjustment2 = -(getNVOffset(BaseType, DerivedType)/8);
3332 if (NonVirtualAdjustment2 != NonVirtualAdjustment) {
3333 NonVirtualAdjustment = NonVirtualAdjustment2;
3334 }
Eli Friedman47145832009-12-04 08:34:14 +00003335 int64_t VirtualAdjustment =
3336 -((idx + extra + 2) * LLVMPointerWidth / 8);
Anders Carlsson891bb4b2009-12-03 02:32:59 +00003337
Eli Friedman47145832009-12-04 08:34:14 +00003338 // Optimize out virtual adjustments of 0.
3339 if (VCalls[idx-1] == 0)
3340 VirtualAdjustment = 0;
Anders Carlsson891bb4b2009-12-03 02:32:59 +00003341
Eli Friedman47145832009-12-04 08:34:14 +00003342 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment,
3343 VirtualAdjustment);
Anders Carlssonbc0e3392009-12-03 02:22:59 +00003344
Eli Friedman72649ed2009-12-06 22:01:30 +00003345 if (!isPure && !ThisAdjustment.isEmpty()) {
Eli Friedman47145832009-12-04 08:34:14 +00003346 ThisAdjustments[Index] = ThisAdjustment;
Eli Friedmanb455f0e2009-12-07 23:56:34 +00003347 SavedAdjustments.push_back(
3348 std::make_pair(GD, std::make_pair(OGD, ThisAdjustment)));
Eli Friedman72649ed2009-12-06 22:01:30 +00003349 }
Anders Carlsson27682a32009-12-03 01:54:02 +00003350 return true;
3351 }
Eli Friedman47145832009-12-04 08:34:14 +00003352
Mike Stump15189fb2010-01-26 21:35:27 +00003353 VCallOffset[GD] = VCallOffset[OGD2] - OverrideOffset/8;
Eli Friedman47145832009-12-04 08:34:14 +00003354
Mike Stump15189fb2010-01-26 21:35:27 +00003355 int64_t NonVirtualAdjustment = -VCallOffset[GD];
3356 QualType DerivedType = MD->getThisType(CGM.getContext());
3357 QualType BaseType = cast<const CXXMethodDecl>(OGD.getDecl())->getThisType(CGM.getContext());
3358 int64_t NonVirtualAdjustment2 = -(getNVOffset(BaseType, DerivedType)/8);
3359 if (NonVirtualAdjustment2 != NonVirtualAdjustment) {
3360 NonVirtualAdjustment = NonVirtualAdjustment2;
3361 }
3362
Eli Friedman47145832009-12-04 08:34:14 +00003363 if (NonVirtualAdjustment) {
3364 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, 0);
3365
Eli Friedmanf062d9d2009-12-06 22:33:51 +00003366 if (!isPure) {
Eli Friedman47145832009-12-04 08:34:14 +00003367 ThisAdjustments[Index] = ThisAdjustment;
Eli Friedmanb455f0e2009-12-07 23:56:34 +00003368 SavedAdjustments.push_back(
3369 std::make_pair(GD, std::make_pair(OGD, ThisAdjustment)));
Eli Friedmanf062d9d2009-12-06 22:33:51 +00003370 }
Eli Friedman47145832009-12-04 08:34:14 +00003371 }
3372 return true;
Anders Carlsson27682a32009-12-03 01:54:02 +00003373 }
3374
3375 return false;
Anders Carlsson27f69d02009-11-27 22:21:51 +00003376}
3377
Anders Carlsson824d7ea2010-02-11 08:02:13 +00003378void OldVtableBuilder::AppendMethodsToVtable() {
Eli Friedman152b5b12009-12-05 01:05:03 +00003379 if (!BuildVtable) {
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00003380 VtableComponents.insert(VtableComponents.end(), Methods.size(),
3381 (llvm::Constant *)0);
Eli Friedman152b5b12009-12-05 01:05:03 +00003382 ThisAdjustments.clear();
3383 BaseReturnTypes.clear();
3384 Methods.clear();
3385 return;
3386 }
3387
Anders Carlsson15318f42009-12-04 16:19:30 +00003388 // Reserve room in the vtable for our new methods.
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00003389 VtableComponents.reserve(VtableComponents.size() + Methods.size());
Anders Carlsson29202d52009-12-04 03:07:26 +00003390
Anders Carlssonb73ba392009-12-04 02:43:50 +00003391 for (unsigned i = 0, e = Methods.size(); i != e; ++i) {
3392 GlobalDecl GD = Methods[i];
Anders Carlssonea357222009-12-04 02:52:22 +00003393 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
3394
3395 // Get the 'this' pointer adjustment.
Anders Carlssonb73ba392009-12-04 02:43:50 +00003396 ThunkAdjustment ThisAdjustment = ThisAdjustments.lookup(i);
Anders Carlssonea357222009-12-04 02:52:22 +00003397
3398 // Construct the return type adjustment.
3399 ThunkAdjustment ReturnAdjustment;
3400
3401 QualType BaseReturnType = BaseReturnTypes.lookup(i);
3402 if (!BaseReturnType.isNull() && !MD->isPure()) {
3403 QualType DerivedType =
3404 MD->getType()->getAs<FunctionType>()->getResultType();
3405
3406 int64_t NonVirtualAdjustment =
3407 getNVOffset(BaseReturnType, DerivedType) / 8;
3408
3409 int64_t VirtualAdjustment =
3410 getVbaseOffset(BaseReturnType, DerivedType);
3411
3412 ReturnAdjustment = ThunkAdjustment(NonVirtualAdjustment,
3413 VirtualAdjustment);
3414 }
3415
Anders Carlsson2fce2162009-12-04 03:06:03 +00003416 llvm::Constant *Method = 0;
Anders Carlssonea357222009-12-04 02:52:22 +00003417 if (!ReturnAdjustment.isEmpty()) {
3418 // Build a covariant thunk.
3419 CovariantThunkAdjustment Adjustment(ThisAdjustment, ReturnAdjustment);
Eli Friedman72649ed2009-12-06 22:01:30 +00003420 Method = wrap(CGM.GetAddrOfCovariantThunk(GD, Adjustment));
Anders Carlssonea357222009-12-04 02:52:22 +00003421 } else if (!ThisAdjustment.isEmpty()) {
3422 // Build a "regular" thunk.
Eli Friedman72649ed2009-12-06 22:01:30 +00003423 Method = wrap(CGM.GetAddrOfThunk(GD, ThisAdjustment));
Anders Carlssonbf540272009-12-04 02:56:03 +00003424 } else if (MD->isPure()) {
3425 // We have a pure virtual method.
Anders Carlsson2fce2162009-12-04 03:06:03 +00003426 Method = getPureVirtualFn();
3427 } else {
3428 // We have a good old regular method.
3429 Method = WrapAddrOf(GD);
Anders Carlssonea357222009-12-04 02:52:22 +00003430 }
Anders Carlsson2fce2162009-12-04 03:06:03 +00003431
3432 // Add the method to the vtable.
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00003433 VtableComponents.push_back(Method);
Anders Carlssonb73ba392009-12-04 02:43:50 +00003434 }
3435
Anders Carlsson2fce2162009-12-04 03:06:03 +00003436
Anders Carlssonb73ba392009-12-04 02:43:50 +00003437 ThisAdjustments.clear();
Anders Carlssonea357222009-12-04 02:52:22 +00003438 BaseReturnTypes.clear();
Anders Carlssonb73ba392009-12-04 02:43:50 +00003439
Anders Carlssonadfa2672009-12-04 02:39:04 +00003440 Methods.clear();
Anders Carlssonadfa2672009-12-04 02:39:04 +00003441}
3442
Anders Carlssonaf440352010-03-23 04:11:45 +00003443void CodeGenVTables::ComputeMethodVtableIndices(const CXXRecordDecl *RD) {
Anders Carlssond6b07fb2009-11-27 20:47:55 +00003444
3445 // Itanium C++ ABI 2.5.2:
Anders Carlsson45147d02010-02-02 03:37:46 +00003446 // The order of the virtual function pointers in a virtual table is the
3447 // order of declaration of the corresponding member functions in the class.
Anders Carlssond6b07fb2009-11-27 20:47:55 +00003448 //
Anders Carlsson45147d02010-02-02 03:37:46 +00003449 // There is an entry for any virtual function declared in a class,
3450 // whether it is a new function or overrides a base class function,
3451 // unless it overrides a function from the primary base, and conversion
3452 // between their return types does not require an adjustment.
Anders Carlssond6b07fb2009-11-27 20:47:55 +00003453
3454 int64_t CurrentIndex = 0;
3455
3456 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
3457 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
3458
3459 if (PrimaryBase) {
Anders Carlsson0121fbd2009-11-30 19:43:26 +00003460 assert(PrimaryBase->isDefinition() &&
3461 "Should have the definition decl of the primary base!");
Anders Carlssond6b07fb2009-11-27 20:47:55 +00003462
3463 // Since the record decl shares its vtable pointer with the primary base
3464 // we need to start counting at the end of the primary base's vtable.
3465 CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase);
3466 }
Eli Friedmanea5ae312009-12-15 03:31:17 +00003467
3468 // Collect all the primary bases, so we can check whether methods override
3469 // a method from the base.
Anders Carlssonb9021e92010-02-27 16:18:19 +00003470 VtableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
Eli Friedmanea5ae312009-12-15 03:31:17 +00003471 for (ASTRecordLayout::primary_base_info_iterator
3472 I = Layout.primary_base_begin(), E = Layout.primary_base_end();
3473 I != E; ++I)
3474 PrimaryBases.insert((*I).getBase());
3475
Anders Carlssond6b07fb2009-11-27 20:47:55 +00003476 const CXXDestructorDecl *ImplicitVirtualDtor = 0;
3477
3478 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
3479 e = RD->method_end(); i != e; ++i) {
3480 const CXXMethodDecl *MD = *i;
3481
3482 // We only want virtual methods.
3483 if (!MD->isVirtual())
3484 continue;
3485
Anders Carlssond6b07fb2009-11-27 20:47:55 +00003486 // Check if this method overrides a method in the primary base.
Anders Carlsson57071e22010-02-12 05:25:12 +00003487 if (const CXXMethodDecl *OverriddenMD =
Anders Carlssonc784ba22010-02-27 06:38:03 +00003488 OverridesMethodInBases(MD, PrimaryBases)) {
Anders Carlsson57071e22010-02-12 05:25:12 +00003489 // Check if converting from the return type of the method to the
3490 // return type of the overridden method requires conversion.
Anders Carlssondf31af12010-02-13 23:17:31 +00003491 if (ComputeReturnAdjustmentBaseOffset(CGM.getContext(), MD,
3492 OverriddenMD).isEmpty()) {
Anders Carlsson57071e22010-02-12 05:25:12 +00003493 // This index is shared between the index in the vtable of the primary
3494 // base class.
3495 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
3496 const CXXDestructorDecl *OverriddenDD =
3497 cast<CXXDestructorDecl>(OverriddenMD);
Anders Carlssond6b07fb2009-11-27 20:47:55 +00003498
Anders Carlsson57071e22010-02-12 05:25:12 +00003499 // Add both the complete and deleting entries.
3500 MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] =
3501 getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Complete));
3502 MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] =
3503 getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting));
3504 } else {
3505 MethodVtableIndices[MD] = getMethodVtableIndex(OverriddenMD);
3506 }
3507
3508 // We don't need to add an entry for this method.
Anders Carlssonc1eec892010-02-12 18:14:46 +00003509 continue;
Anders Carlssond6b07fb2009-11-27 20:47:55 +00003510 }
3511 }
3512
Anders Carlssond6b07fb2009-11-27 20:47:55 +00003513 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
3514 if (MD->isImplicit()) {
3515 assert(!ImplicitVirtualDtor &&
3516 "Did already see an implicit virtual dtor!");
3517 ImplicitVirtualDtor = DD;
3518 continue;
3519 }
3520
3521 // Add the complete dtor.
3522 MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++;
3523
3524 // Add the deleting dtor.
3525 MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++;
3526 } else {
3527 // Add the entry.
3528 MethodVtableIndices[MD] = CurrentIndex++;
3529 }
3530 }
3531
3532 if (ImplicitVirtualDtor) {
3533 // Itanium C++ ABI 2.5.2:
Anders Carlssona661a542010-02-16 04:49:44 +00003534 // If a class has an implicitly-defined virtual destructor,
3535 // its entries come after the declared virtual function pointers.
Anders Carlssond6b07fb2009-11-27 20:47:55 +00003536
3537 // Add the complete dtor.
3538 MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] =
3539 CurrentIndex++;
3540
3541 // Add the deleting dtor.
3542 MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] =
3543 CurrentIndex++;
3544 }
3545
3546 NumVirtualFunctionPointers[RD] = CurrentIndex;
3547}
3548
Anders Carlssonaf440352010-03-23 04:11:45 +00003549uint64_t CodeGenVTables::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) {
Anders Carlssond6b07fb2009-11-27 20:47:55 +00003550 llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
3551 NumVirtualFunctionPointers.find(RD);
3552 if (I != NumVirtualFunctionPointers.end())
3553 return I->second;
3554
3555 ComputeMethodVtableIndices(RD);
3556
3557 I = NumVirtualFunctionPointers.find(RD);
3558 assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!");
3559 return I->second;
3560}
3561
Anders Carlssonaf440352010-03-23 04:11:45 +00003562uint64_t CodeGenVTables::getMethodVtableIndex(GlobalDecl GD) {
Anders Carlssona0fdd912009-11-13 17:08:56 +00003563 MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD);
Anders Carlssondbd920c2009-10-11 22:13:54 +00003564 if (I != MethodVtableIndices.end())
3565 return I->second;
3566
Anders Carlssona0fdd912009-11-13 17:08:56 +00003567 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
Anders Carlssond6b07fb2009-11-27 20:47:55 +00003568
3569 ComputeMethodVtableIndices(RD);
3570
Anders Carlssona0fdd912009-11-13 17:08:56 +00003571 I = MethodVtableIndices.find(GD);
Anders Carlssondbd920c2009-10-11 22:13:54 +00003572 assert(I != MethodVtableIndices.end() && "Did not find index!");
3573 return I->second;
3574}
3575
Anders Carlssonaf440352010-03-23 04:11:45 +00003576int64_t CodeGenVTables::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD,
3577 const CXXRecordDecl *VBase) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00003578 ClassPairTy ClassPair(RD, VBase);
3579
Anders Carlssonbba16072010-03-11 07:15:17 +00003580 VirtualBaseClassOffsetOffsetsMapTy::iterator I =
3581 VirtualBaseClassOffsetOffsets.find(ClassPair);
3582 if (I != VirtualBaseClassOffsetOffsets.end())
Anders Carlssondbd920c2009-10-11 22:13:54 +00003583 return I->second;
3584
Anders Carlssonc89a7cf2010-03-11 16:06:20 +00003585 VCallAndVBaseOffsetBuilder Builder(RD, RD, /*FinalOverriders=*/0,
3586 BaseSubobject(RD, 0),
3587 /*BaseIsVirtual=*/false,
3588 /*OffsetInLayoutClass=*/0);
Anders Carlssonc89a7cf2010-03-11 16:06:20 +00003589
3590 for (VCallAndVBaseOffsetBuilder::VBaseOffsetOffsetsMapTy::const_iterator I =
3591 Builder.getVBaseOffsetOffsets().begin(),
3592 E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00003593 // Insert all types.
3594 ClassPairTy ClassPair(RD, I->first);
3595
Anders Carlssonbba16072010-03-11 07:15:17 +00003596 VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second));
Anders Carlssondbd920c2009-10-11 22:13:54 +00003597 }
3598
Anders Carlssonbba16072010-03-11 07:15:17 +00003599 I = VirtualBaseClassOffsetOffsets.find(ClassPair);
Anders Carlssonc89a7cf2010-03-11 16:06:20 +00003600
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00003601 // FIXME: The assertion below assertion currently fails with the old vtable
3602 /// layout code if there is a non-virtual thunk adjustment in a vtable.
3603 // Once the new layout is in place, this return should be removed.
Anders Carlssonbba16072010-03-11 07:15:17 +00003604 if (I == VirtualBaseClassOffsetOffsets.end())
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00003605 return 0;
3606
Anders Carlssonbba16072010-03-11 07:15:17 +00003607 assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!");
Anders Carlssondbd920c2009-10-11 22:13:54 +00003608
3609 return I->second;
3610}
3611
Anders Carlsson5c6c1d92010-03-24 03:57:14 +00003612const CodeGenVTables::AddrSubMap_t &
3613CodeGenVTables::getAddressPoints(const CXXRecordDecl *RD) {
Anders Carlsson66d567d2010-03-25 00:51:13 +00003614 if (!OldAddressPoints[RD]) {
Anders Carlssonff143f82010-03-25 00:35:49 +00003615 OldVtableBuilder::AddressPointsMapTy AddressPoints;
Anders Carlsson5c6c1d92010-03-24 03:57:14 +00003616 OldVtableBuilder b(RD, RD, 0, CGM, false, AddressPoints);
3617
3618 b.GenerateVtableForBase(RD, 0);
3619 b.GenerateVtableForVBases(RD, 0);
3620 }
3621
Anders Carlsson66d567d2010-03-25 00:51:13 +00003622 return *(*OldAddressPoints[RD])[RD];
Anders Carlsson5c6c1d92010-03-24 03:57:14 +00003623}
3624
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00003625llvm::GlobalVariable *
Anders Carlssonaf440352010-03-23 04:11:45 +00003626CodeGenVTables::GenerateVtable(llvm::GlobalVariable::LinkageTypes Linkage,
3627 bool GenerateDefinition,
3628 const CXXRecordDecl *LayoutClass,
3629 const CXXRecordDecl *RD, uint64_t Offset,
3630 bool IsVirtual,
Anders Carlssonff143f82010-03-25 00:35:49 +00003631 llvm::DenseMap<BaseSubobject, uint64_t> &AddressPoints) {
Anders Carlssond03a3262010-02-27 20:39:05 +00003632 if (GenerateDefinition) {
3633 if (LayoutClass == RD) {
Anders Carlsson5d7af6b2010-02-28 00:36:23 +00003634 assert(!IsVirtual &&
Anders Carlssone02fc0a2010-03-11 05:48:21 +00003635 "Can only have a virtual base in construction vtables!");
3636 assert(!Offset &&
3637 "Can only have a base offset in construction vtables!");
Anders Carlssond03a3262010-02-27 20:39:05 +00003638 }
Anders Carlssone02fc0a2010-03-11 05:48:21 +00003639
3640 VtableBuilder Builder(*this, RD, Offset,
Anders Carlsson1ffd79b2010-03-11 06:10:52 +00003641 /*MostDerivedClassIsVirtual=*/IsVirtual,
Anders Carlssone02fc0a2010-03-11 05:48:21 +00003642 LayoutClass);
3643
3644 if (CGM.getLangOptions().DumpVtableLayouts)
3645 Builder.dumpLayout(llvm::errs());
Anders Carlsson0c0eeb22010-02-13 02:02:03 +00003646 }
3647
Anders Carlssondbd920c2009-10-11 22:13:54 +00003648 llvm::SmallString<256> OutName;
Mike Stump9840c702009-11-12 20:47:57 +00003649 if (LayoutClass != RD)
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00003650 CGM.getMangleContext().mangleCXXCtorVtable(LayoutClass, Offset / 8,
3651 RD, OutName);
Mike Stump8cfcb522009-11-11 20:26:26 +00003652 else
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00003653 CGM.getMangleContext().mangleCXXVtable(RD, OutName);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00003654 llvm::StringRef Name = OutName.str();
Benjamin Kramer7a9474e2009-10-11 22:57:54 +00003655
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00003656 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
Anders Carlsson5c6c1d92010-03-24 03:57:14 +00003657 if (GV == 0 || GV->isDeclaration()) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +00003658 OldVtableBuilder b(RD, LayoutClass, Offset, CGM, GenerateDefinition,
3659 AddressPoints);
Eli Friedman152b5b12009-12-05 01:05:03 +00003660
3661 D1(printf("vtable %s\n", RD->getNameAsCString()));
3662 // First comes the vtables for all the non-virtual bases...
Mike Stump8cc4f102009-12-24 07:29:41 +00003663 b.GenerateVtableForBase(RD, Offset);
Eli Friedman152b5b12009-12-05 01:05:03 +00003664
3665 // then the vtables for all the virtual bases.
3666 b.GenerateVtableForVBases(RD, Offset);
3667
Anders Carlsson152d4dc2009-12-05 22:19:10 +00003668 llvm::Constant *Init = 0;
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00003669 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
Anders Carlsson152d4dc2009-12-05 22:19:10 +00003670 llvm::ArrayType *ArrayType =
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00003671 llvm::ArrayType::get(Int8PtrTy, b.getVtableComponents().size());
Anders Carlssone40477c2009-12-05 21:09:05 +00003672
Anders Carlsson5794c972009-12-06 00:53:22 +00003673 if (GenerateDefinition)
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00003674 Init = llvm::ConstantArray::get(ArrayType, &b.getVtableComponents()[0],
3675 b.getVtableComponents().size());
Anders Carlsson5794c972009-12-06 00:53:22 +00003676
Mike Stump85615df2009-11-19 04:04:36 +00003677 llvm::GlobalVariable *OGV = GV;
Anders Carlsson5794c972009-12-06 00:53:22 +00003678
3679 GV = new llvm::GlobalVariable(CGM.getModule(), ArrayType,
3680 /*isConstant=*/true, Linkage, Init, Name);
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00003681 CGM.setGlobalVisibility(GV, RD);
3682
Mike Stump85615df2009-11-19 04:04:36 +00003683 if (OGV) {
3684 GV->takeName(OGV);
Anders Carlsson152d4dc2009-12-05 22:19:10 +00003685 llvm::Constant *NewPtr =
3686 llvm::ConstantExpr::getBitCast(GV, OGV->getType());
Mike Stump85615df2009-11-19 04:04:36 +00003687 OGV->replaceAllUsesWith(NewPtr);
3688 OGV->eraseFromParent();
3689 }
Mike Stump85615df2009-11-19 04:04:36 +00003690 }
Anders Carlssonbb27d862009-12-05 21:28:12 +00003691
3692 return GV;
Anders Carlssondbd920c2009-10-11 22:13:54 +00003693}
Mike Stumpfbfb52d2009-11-10 02:30:51 +00003694
Anders Carlsson19879c92010-03-23 17:17:29 +00003695llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
3696 const ThunkInfo &Thunk) {
3697 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
3698
3699 // Compute the mangled name.
3700 llvm::SmallString<256> Name;
3701 if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
3702 getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), Thunk.This,
3703 Name);
3704 else
3705 getMangleContext().mangleThunk(MD, Thunk, Name);
3706
3707 const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD);
3708 return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl());
3709}
3710
Anders Carlsson519c3282010-03-24 00:39:18 +00003711static llvm::Value *PerformTypeAdjustment(CodeGenFunction &CGF,
3712 llvm::Value *Ptr,
3713 int64_t NonVirtualAdjustment,
3714 int64_t VirtualAdjustment) {
3715 if (!NonVirtualAdjustment && !VirtualAdjustment)
3716 return Ptr;
3717
3718 const llvm::Type *Int8PtrTy =
3719 llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
3720
3721 llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy);
3722
3723 if (NonVirtualAdjustment) {
3724 // Do the non-virtual adjustment.
3725 V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
3726 }
3727
3728 if (VirtualAdjustment) {
3729 const llvm::Type *PtrDiffTy =
3730 CGF.ConvertType(CGF.getContext().getPointerDiffType());
3731
3732 // Do the virtual adjustment.
3733 llvm::Value *VTablePtrPtr =
3734 CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo());
3735
3736 llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
3737
3738 llvm::Value *OffsetPtr =
3739 CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
3740
3741 OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
3742
3743 // Load the adjustment offset from the vtable.
3744 llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr);
3745
3746 // Adjust our pointer.
3747 V = CGF.Builder.CreateInBoundsGEP(V, Offset);
3748 }
3749
3750 // Cast back to the original type.
3751 return CGF.Builder.CreateBitCast(V, Ptr->getType());
3752}
3753
3754void CodeGenFunction::GenerateThunk(llvm::Function *Fn, GlobalDecl GD,
3755 const ThunkInfo &Thunk) {
3756 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
3757 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
3758 QualType ResultType = FPT->getResultType();
3759 QualType ThisType = MD->getThisType(getContext());
3760
3761 FunctionArgList FunctionArgs;
3762
3763 // FIXME: It would be nice if more of this code could be shared with
3764 // CodeGenFunction::GenerateCode.
3765
3766 // Create the implicit 'this' parameter declaration.
3767 CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0,
3768 MD->getLocation(),
3769 &getContext().Idents.get("this"),
3770 ThisType);
3771
3772 // Add the 'this' parameter.
3773 FunctionArgs.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType()));
3774
3775 // Add the rest of the parameters.
3776 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
3777 E = MD->param_end(); I != E; ++I) {
3778 ParmVarDecl *Param = *I;
3779
3780 FunctionArgs.push_back(std::make_pair(Param, Param->getType()));
3781 }
3782
3783 StartFunction(GlobalDecl(), ResultType, Fn, FunctionArgs, SourceLocation());
3784
3785 // Adjust the 'this' pointer if necessary.
3786 llvm::Value *AdjustedThisPtr =
3787 PerformTypeAdjustment(*this, LoadCXXThis(),
3788 Thunk.This.NonVirtual,
3789 Thunk.This.VCallOffsetOffset);
3790
3791 CallArgList CallArgs;
3792
3793 // Add our adjusted 'this' pointer.
3794 CallArgs.push_back(std::make_pair(RValue::get(AdjustedThisPtr), ThisType));
3795
3796 // Add the rest of the parameters.
3797 for (FunctionDecl::param_const_iterator I = MD->param_begin(),
3798 E = MD->param_end(); I != E; ++I) {
3799 ParmVarDecl *Param = *I;
3800 QualType ArgType = Param->getType();
3801
3802 // FIXME: Declaring a DeclRefExpr on the stack is kinda icky.
3803 DeclRefExpr ArgExpr(Param, ArgType.getNonReferenceType(), SourceLocation());
3804 CallArgs.push_back(std::make_pair(EmitCallArg(&ArgExpr, ArgType), ArgType));
3805 }
3806
3807 // Get our callee.
3808 const llvm::Type *Ty =
3809 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
3810 FPT->isVariadic());
3811 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty);
3812
3813 const CGFunctionInfo &FnInfo =
3814 CGM.getTypes().getFunctionInfo(ResultType, CallArgs, FPT->getCallConv(),
3815 FPT->getNoReturnAttr());
3816
3817 // Now emit our call.
3818 RValue RV = EmitCall(FnInfo, Callee, ReturnValueSlot(), CallArgs, MD);
3819
3820 if (!Thunk.Return.isEmpty()) {
3821 // Emit the return adjustment.
3822 bool NullCheckValue = !ResultType->isReferenceType();
3823
3824 llvm::BasicBlock *AdjustNull = 0;
3825 llvm::BasicBlock *AdjustNotNull = 0;
3826 llvm::BasicBlock *AdjustEnd = 0;
3827
3828 llvm::Value *ReturnValue = RV.getScalarVal();
3829
3830 if (NullCheckValue) {
3831 AdjustNull = createBasicBlock("adjust.null");
3832 AdjustNotNull = createBasicBlock("adjust.notnull");
3833 AdjustEnd = createBasicBlock("adjust.end");
3834
3835 llvm::Value *IsNull = Builder.CreateIsNull(ReturnValue);
3836 Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
3837 EmitBlock(AdjustNotNull);
3838 }
3839
3840 ReturnValue = PerformTypeAdjustment(*this, ReturnValue,
3841 Thunk.Return.NonVirtual,
3842 Thunk.Return.VBaseOffsetOffset);
3843
3844 if (NullCheckValue) {
3845 Builder.CreateBr(AdjustEnd);
3846 EmitBlock(AdjustNull);
3847 Builder.CreateBr(AdjustEnd);
3848 EmitBlock(AdjustEnd);
3849
3850 llvm::PHINode *PHI = Builder.CreatePHI(ReturnValue->getType());
3851 PHI->reserveOperandSpace(2);
3852 PHI->addIncoming(ReturnValue, AdjustNotNull);
3853 PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
3854 AdjustNull);
3855 ReturnValue = PHI;
3856 }
3857
3858 RV = RValue::get(ReturnValue);
3859 }
3860
3861 if (!ResultType->isVoidType())
3862 EmitReturnOfRValue(RV, ResultType);
3863
3864 FinishFunction();
3865
3866 // Destroy the 'this' declaration.
3867 CXXThisDecl->Destroy(getContext());
3868
3869 // Set the right linkage.
3870 llvm::GlobalValue::LinkageTypes ThunkLinkage;
3871
3872 if (CGM.getFunctionLinkage(MD) == llvm::Function::InternalLinkage)
3873 ThunkLinkage = llvm::Function::InternalLinkage;
3874 else
3875 ThunkLinkage = llvm::Function::WeakAnyLinkage;
3876
3877 Fn->setLinkage(ThunkLinkage);
3878
3879 // Set the right visibility.
3880 CGM.setGlobalVisibility(Fn, MD);
3881}
3882
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00003883void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk)
3884{
Anders Carlsson13d68982010-03-24 00:35:44 +00003885 llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk);
Anders Carlsson7986ad52010-03-23 18:18:41 +00003886 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
Anders Carlsson19879c92010-03-23 17:17:29 +00003887
Anders Carlsson7986ad52010-03-23 18:18:41 +00003888 // Strip off a bitcast if we got one back.
Anders Carlsson13d68982010-03-24 00:35:44 +00003889 if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
Anders Carlsson7986ad52010-03-23 18:18:41 +00003890 assert(CE->getOpcode() == llvm::Instruction::BitCast);
Anders Carlsson13d68982010-03-24 00:35:44 +00003891 Entry = CE->getOperand(0);
Anders Carlsson7986ad52010-03-23 18:18:41 +00003892 }
3893
Anders Carlsson7986ad52010-03-23 18:18:41 +00003894 // There's already a declaration with the same name, check if it has the same
3895 // type or if we need to replace it.
Anders Carlsson13d68982010-03-24 00:35:44 +00003896 if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() !=
3897 CGM.getTypes().GetFunctionTypeForVtable(MD)) {
3898 llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry);
Anders Carlsson7986ad52010-03-23 18:18:41 +00003899
3900 // If the types mismatch then we have to rewrite the definition.
3901 assert(OldThunkFn->isDeclaration() &&
3902 "Shouldn't replace non-declaration");
3903
3904 // Remove the name from the old thunk function and get a new thunk.
3905 OldThunkFn->setName(llvm::StringRef());
Anders Carlsson13d68982010-03-24 00:35:44 +00003906 Entry = CGM.GetAddrOfThunk(GD, Thunk);
Anders Carlsson7986ad52010-03-23 18:18:41 +00003907
3908 // If needed, replace the old thunk with a bitcast.
3909 if (!OldThunkFn->use_empty()) {
3910 llvm::Constant *NewPtrForOldDecl =
Anders Carlsson13d68982010-03-24 00:35:44 +00003911 llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
Anders Carlsson7986ad52010-03-23 18:18:41 +00003912 OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
3913 }
3914
3915 // Remove the old thunk.
3916 OldThunkFn->eraseFromParent();
3917 }
Anders Carlsson519c3282010-03-24 00:39:18 +00003918
3919 // Actually generate the thunk body.
3920 llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
3921 CodeGenFunction(CGM).GenerateThunk(ThunkFn, GD, Thunk);
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00003922}
3923
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00003924void CodeGenVTables::EmitThunks(GlobalDecl GD)
3925{
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00003926 const CXXMethodDecl *MD =
3927 cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
3928
3929 // We don't need to generate thunks for the base destructor.
3930 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
3931 return;
3932
3933 const CXXRecordDecl *RD = MD->getParent();
3934
Anders Carlssonccd83d72010-03-24 16:42:11 +00003935 // Compute VTable related info for this class.
3936 ComputeVTableRelatedInformation(RD);
3937
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00003938 ThunksMapTy::const_iterator I = Thunks.find(MD);
3939 if (I == Thunks.end()) {
Anders Carlssonccd83d72010-03-24 16:42:11 +00003940 // We did not find a thunk for this method.
3941 return;
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00003942 }
Anders Carlssonccd83d72010-03-24 16:42:11 +00003943
Anders Carlssonfbf6ed42010-03-23 16:36:50 +00003944 const ThunkInfoVectorTy &ThunkInfoVector = I->second;
3945 for (unsigned I = 0, E = ThunkInfoVector.size(); I != E; ++I)
3946 EmitThunk(GD, ThunkInfoVector[I]);
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00003947}
3948
Anders Carlssonccd83d72010-03-24 16:42:11 +00003949void CodeGenVTables::ComputeVTableRelatedInformation(const CXXRecordDecl *RD) {
3950 uint64_t *&LayoutData = VTableLayoutMap[RD];
3951
3952 // Check if we've computed this information before.
3953 if (LayoutData)
3954 return;
3955
3956 VtableBuilder Builder(*this, RD, 0, /*MostDerivedClassIsVirtual=*/0, RD);
3957
3958 // Add the VTable layout.
3959 uint64_t NumVTableComponents = Builder.getNumVTableComponents();
3960 LayoutData = new uint64_t[NumVTableComponents + 1];
3961
3962 // Store the number of components.
3963 LayoutData[0] = NumVTableComponents;
3964
3965 // Store the components.
3966 std::copy(Builder.vtable_components_data_begin(),
3967 Builder.vtable_components_data_end(),
3968 &LayoutData[1]);
3969
3970 // Add the known thunks.
3971 Thunks.insert(Builder.thunks_begin(), Builder.thunks_end());
3972
Anders Carlsson66d567d2010-03-25 00:51:13 +00003973 // Add the address points.
3974 for (VtableBuilder::AddressPointsMapTy::const_iterator I =
3975 Builder.address_points_begin(), E = Builder.address_points_end();
3976 I != E; ++I) {
3977
3978 uint64_t &AddressPoint = AddressPoints[std::make_pair(RD, I->first)];
3979
3980 // Check if we already have the address points for this base.
3981 assert(!AddressPoint && "Address point already exists for this base!");
3982
3983 AddressPoint = I->second;
3984 }
3985
Anders Carlssonccd83d72010-03-24 16:42:11 +00003986 // If we don't have the vbase information for this class, insert it.
3987 // getVirtualBaseOffsetOffset will compute it separately without computing
3988 // the rest of the vtable related information.
3989 if (!RD->getNumVBases())
3990 return;
3991
3992 const RecordType *VBaseRT =
3993 RD->vbases_begin()->getType()->getAs<RecordType>();
3994 const CXXRecordDecl *VBase = cast<CXXRecordDecl>(VBaseRT->getDecl());
3995
3996 if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase)))
3997 return;
3998
3999 for (VtableBuilder::VBaseOffsetOffsetsMapTy::const_iterator I =
4000 Builder.getVBaseOffsetOffsets().begin(),
4001 E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) {
4002 // Insert all types.
4003 ClassPairTy ClassPair(RD, I->first);
4004
4005 VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second));
4006 }
4007}
4008
Anders Carlssonaf440352010-03-23 04:11:45 +00004009void
4010CodeGenVTables::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
4011 const CXXRecordDecl *RD) {
Anders Carlssond58987c2009-12-07 07:59:52 +00004012 llvm::GlobalVariable *&Vtable = Vtables[RD];
4013 if (Vtable) {
4014 assert(Vtable->getInitializer() && "Vtable doesn't have a definition!");
4015 return;
4016 }
4017
Anders Carlssonff143f82010-03-25 00:35:49 +00004018 llvm::DenseMap<BaseSubobject, uint64_t> AddressPoints;
Anders Carlsson1bb60992010-01-14 02:29:07 +00004019 Vtable = GenerateVtable(Linkage, /*GenerateDefinition=*/true, RD, RD, 0,
Anders Carlsson5d7af6b2010-02-28 00:36:23 +00004020 /*IsVirtual=*/false,
Anders Carlsson1bb60992010-01-14 02:29:07 +00004021 AddressPoints);
Rafael Espindolabbf58bb2010-03-10 02:19:29 +00004022 GenerateVTT(Linkage, /*GenerateDefinition=*/true, RD);
Mike Stump58588942009-11-19 01:08:19 +00004023}
4024
Anders Carlsson0d1407e2010-03-25 15:26:28 +00004025llvm::Constant *
4026CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD,
4027 const uint64_t *Components,
4028 unsigned NumComponents,
4029 const VTableThunksTy &VTableThunks) {
4030 llvm::SmallVector<llvm::Constant *, 64> Inits;
4031
4032 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
4033
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00004034 const llvm::Type *PtrDiffTy =
4035 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
4036
4037 QualType ClassType = CGM.getContext().getTagDeclType(RD);
4038 llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType);
4039
4040 unsigned NextVTableThunkIndex = 0;
4041
Anders Carlsson0d1407e2010-03-25 15:26:28 +00004042 for (unsigned I = 0; I != NumComponents; ++I) {
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00004043 VtableComponent Component =
4044 VtableComponent::getFromOpaqueInteger(Components[I]);
4045
4046 llvm::Constant *Init = 0;
4047
4048 switch (Component.getKind()) {
4049 case VtableComponent::CK_VCallOffset:
4050 Init = llvm::ConstantInt::get(PtrDiffTy, Component.getVCallOffset());
4051 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
4052 break;
4053 case VtableComponent::CK_VBaseOffset:
4054 Init = llvm::ConstantInt::get(PtrDiffTy, Component.getVBaseOffset());
4055 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
4056 break;
4057 case VtableComponent::CK_OffsetToTop:
4058 Init = llvm::ConstantInt::get(PtrDiffTy, Component.getOffsetToTop());
4059 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
4060 break;
4061 case VtableComponent::CK_RTTI:
4062 Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
4063 break;
4064 case VtableComponent::CK_FunctionPointer:
4065 case VtableComponent::CK_CompleteDtorPointer:
4066 case VtableComponent::CK_DeletingDtorPointer: {
4067 GlobalDecl GD;
4068
4069 // Get the right global decl.
4070 switch (Component.getKind()) {
4071 default:
4072 llvm_unreachable("Unexpected vtable component kind");
4073 case VtableComponent::CK_FunctionPointer:
4074 GD = Component.getFunctionDecl();
4075 break;
4076 case VtableComponent::CK_CompleteDtorPointer:
4077 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
4078 break;
4079 case VtableComponent::CK_DeletingDtorPointer:
4080 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
4081 break;
4082 }
4083
4084 // Check if we should use a thunk.
4085 if (NextVTableThunkIndex < VTableThunks.size() &&
4086 VTableThunks[NextVTableThunkIndex].first == I) {
4087 const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
4088
4089 Init = CGM.GetAddrOfThunk(GD, Thunk);
4090 } else {
4091 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
4092 const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD);
4093
4094 Init = CGM.GetAddrOfFunction(GD, Ty);
4095 }
4096
4097 Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
4098 break;
4099 }
4100
4101 case VtableComponent::CK_UnusedFunctionPointer:
4102 Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
4103 break;
4104 };
Anders Carlsson0d1407e2010-03-25 15:26:28 +00004105
4106 Inits.push_back(Init);
4107 }
4108
4109 llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
4110 return llvm::ConstantArray::get(ArrayType, Inits.data(), Inits.size());
4111}
4112
4113/// GetGlobalVariable - Will return a global variable of the given type.
4114/// If a variable with a different type already exists then a new variable
4115/// with the right type will be created.
4116/// FIXME: We should move this to CodeGenModule and rename it to something
4117/// better and then use it in CGVTT and CGRTTI.
4118static llvm::GlobalVariable *
4119GetGlobalVariable(llvm::Module &Module, llvm::StringRef Name,
4120 const llvm::Type *Ty,
4121 llvm::GlobalValue::LinkageTypes Linkage) {
4122
4123 llvm::GlobalVariable *GV = Module.getNamedGlobal(Name);
4124 llvm::GlobalVariable *OldGV = 0;
4125
4126 if (GV) {
4127 // Check if the variable has the right type.
4128 if (GV->getType()->getElementType() == Ty) {
4129 // Set the correct linkage.
4130 GV->setLinkage(Linkage);
4131 return GV;
4132 }
4133
4134 assert(GV->isDeclaration() && "Declaration has wrong type!");
4135
4136 OldGV = GV;
4137 }
4138
4139 // Create a new variable.
4140 GV = new llvm::GlobalVariable(Module, Ty, /*isConstant=*/true,
4141 Linkage, 0, Name);
4142
4143 if (OldGV) {
4144 // Replace occurrences of the old variable if needed.
4145 GV->takeName(OldGV);
4146
4147 if (!OldGV->use_empty()) {
4148 llvm::Constant *NewPtrForOldDecl =
4149 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
4150 OldGV->replaceAllUsesWith(NewPtrForOldDecl);
4151 }
4152
4153 OldGV->eraseFromParent();
4154 }
4155
4156 return GV;
4157}
4158
Anders Carlsson5eea8762010-03-24 05:32:05 +00004159llvm::Constant *CodeGenVTables::GetAddrOfVTable(const CXXRecordDecl *RD) {
Anders Carlsson5c6c1d92010-03-24 03:57:14 +00004160 llvm::SmallString<256> OutName;
4161 CGM.getMangleContext().mangleCXXVtable(RD, OutName);
4162 llvm::StringRef Name = OutName.str();
Mike Stump85615df2009-11-19 04:04:36 +00004163
Anders Carlssonccd83d72010-03-24 16:42:11 +00004164 ComputeVTableRelatedInformation(RD);
4165
Anders Carlsson5c6c1d92010-03-24 03:57:14 +00004166 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
Anders Carlssonccd83d72010-03-24 16:42:11 +00004167 llvm::ArrayType *ArrayType =
4168 llvm::ArrayType::get(Int8PtrTy, getNumVTableComponents(RD));
Anders Carlsson5c6c1d92010-03-24 03:57:14 +00004169
Anders Carlsson5eea8762010-03-24 05:32:05 +00004170 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson5c6c1d92010-03-24 03:57:14 +00004171 if (GV) {
4172 if (!GV->isDeclaration() || GV->getType()->getElementType() == ArrayType)
4173 return GV;
4174
4175 return llvm::ConstantExpr::getBitCast(GV, ArrayType->getPointerTo());
4176 }
4177
4178 GV = new llvm::GlobalVariable(CGM.getModule(), ArrayType, /*isConstant=*/true,
4179 llvm::GlobalValue::ExternalLinkage, 0, Name);
4180
4181 return GV;
Mike Stump380dd752009-11-10 07:44:33 +00004182}
Mike Stump8cfcb522009-11-11 20:26:26 +00004183
Anders Carlssonff143f82010-03-25 00:35:49 +00004184llvm::GlobalVariable *
4185CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
4186 const BaseSubobject &Base,
4187 bool BaseIsVirtual,
4188 AddressPointsMapTy& AddressPoints) {
Anders Carlsson0d1407e2010-03-25 15:26:28 +00004189 if (!CGM.getLangOptions().DumpVtableLayouts) {
4190 llvm::DenseMap<BaseSubobject, uint64_t> VTableAddressPoints;
Anders Carlssonff143f82010-03-25 00:35:49 +00004191
Anders Carlsson0d1407e2010-03-25 15:26:28 +00004192 llvm::GlobalVariable *VTable =
4193 GenerateVtable(llvm::GlobalValue::InternalLinkage,
4194 /*GenerateDefinition=*/true,
4195 RD, Base.getBase(), Base.getBaseOffset(),
4196 BaseIsVirtual, VTableAddressPoints);
Anders Carlssonff143f82010-03-25 00:35:49 +00004197
Anders Carlsson0d1407e2010-03-25 15:26:28 +00004198 // Add the address points for this base.
4199 for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
4200 VTableAddressPoints.begin(), E = VTableAddressPoints.end();
4201 I != E; ++I) {
4202
4203 uint64_t &AddressPoint =
4204 AddressPoints[std::make_pair(Base.getBase(), I->first)];
4205
4206 // Check if we already have the address points for this base.
4207 assert(!AddressPoint && "Address point already exists for this base!");
4208
4209 AddressPoint = I->second;
4210 }
Anders Carlssonff143f82010-03-25 00:35:49 +00004211
Anders Carlsson0d1407e2010-03-25 15:26:28 +00004212 return VTable;
Anders Carlssonff143f82010-03-25 00:35:49 +00004213 }
4214
Anders Carlsson0d1407e2010-03-25 15:26:28 +00004215 VtableBuilder Builder(*this, Base.getBase(), Base.getBaseOffset(),
4216 /*MostDerivedClassIsVirtual=*/BaseIsVirtual, RD);
4217
Anders Carlsson6a5ab5d2010-03-25 16:49:53 +00004218 // Dump the vtable layout if necessary.
4219 if (CGM.getLangOptions().DumpVtableLayouts)
4220 Builder.dumpLayout(llvm::errs());
4221
4222 // Add the address points.
4223 for (VtableBuilder::AddressPointsMapTy::const_iterator I =
4224 Builder.address_points_begin(), E = Builder.address_points_end();
4225 I != E; ++I) {
4226 uint64_t &AddressPoint =
4227 AddressPoints[std::make_pair(Base.getBase(), I->first)];
4228
4229#if 0
4230 // FIXME: Figure out why this assert fires.
4231 // Check if we already have the address points for this base.
4232 assert(!AddressPoint && "Address point already exists for this base!");
4233#endif
4234 AddressPoint = I->second;
4235 }
Anders Carlsson0d1407e2010-03-25 15:26:28 +00004236
4237 // Get the mangled construction vtable name.
4238 llvm::SmallString<256> OutName;
4239 CGM.getMangleContext().mangleCXXCtorVtable(RD, Base.getBaseOffset() / 8,
4240 Base.getBase(), OutName);
4241 llvm::StringRef Name = OutName.str();
4242
4243 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
4244 llvm::ArrayType *ArrayType =
4245 llvm::ArrayType::get(Int8PtrTy, Builder.getNumVTableComponents());
4246
4247 // Create the variable that will hold the construction vtable.
4248 llvm::GlobalVariable *VTable =
4249 GetGlobalVariable(CGM.getModule(), Name, ArrayType,
4250 llvm::GlobalValue::InternalLinkage);
4251
4252 // Add the thunks.
4253 VTableThunksTy VTableThunks;
4254 VTableThunks.append(Builder.vtable_thunks_begin(),
4255 Builder.vtable_thunks_end());
4256
4257 // Create and set the initializer.
4258 llvm::Constant *Init =
4259 CreateVTableInitializer(Base.getBase(),
4260 Builder.vtable_components_data_begin(),
4261 Builder.getNumVTableComponents(), VTableThunks);
4262 VTable->setInitializer(Init);
4263
Anders Carlssonff143f82010-03-25 00:35:49 +00004264 return VTable;
4265}
4266
Anders Carlsson13189d02010-03-23 04:15:00 +00004267void CodeGenVTables::EmitVTableRelatedData(GlobalDecl GD) {
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00004268 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
4269 const CXXRecordDecl *RD = MD->getParent();
4270
Anders Carlsson224c3122009-12-05 22:42:54 +00004271 // If the class doesn't have a vtable we don't need to emit one.
4272 if (!RD->isDynamicClass())
4273 return;
4274
Anders Carlsson7270ee42010-03-23 04:31:31 +00004275 // Check if we need to emit thunks for this function.
4276 if (MD->isVirtual())
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00004277 EmitThunks(GD);
Anders Carlsson7270ee42010-03-23 04:31:31 +00004278
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00004279 // Get the key function.
Anders Carlssonf53df232009-12-07 04:35:11 +00004280 const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD);
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00004281
Anders Carlsson224c3122009-12-05 22:42:54 +00004282 if (KeyFunction) {
4283 // We don't have the right key function.
4284 if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl())
4285 return;
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00004286 }
4287
Rafael Espindolabbf58bb2010-03-10 02:19:29 +00004288 if (Vtables.count(RD))
4289 return;
Eli Friedman72649ed2009-12-06 22:01:30 +00004290
Rafael Espindolabbf58bb2010-03-10 02:19:29 +00004291 TemplateSpecializationKind kind = RD->getTemplateSpecializationKind();
Rafael Espindolaf075b222010-03-23 19:55:22 +00004292 if (kind == TSK_ImplicitInstantiation)
Rafael Espindolabbf58bb2010-03-10 02:19:29 +00004293 CGM.DeferredVtables.push_back(RD);
4294 else
4295 GenerateClassData(CGM.getVtableLinkage(RD), RD);
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00004296}
Anders Carlssonee5ab9f2010-03-23 04:59:02 +00004297