blob: 93e2cb7fc6a436d0817267a7a0fa8468c1ae7a47 [file] [log] [blame]
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001//===--- CGVtable.cpp - Emit LLVM Code for C++ vtables --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation of virtual tables.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenModule.h"
15#include "CodeGenFunction.h"
Anders Carlssonf942ee02009-11-27 20:47:55 +000016#include "clang/AST/CXXInheritance.h"
Anders Carlsson2bb27f52009-10-11 22:13:54 +000017#include "clang/AST/RecordLayout.h"
Anders Carlssond420a312009-11-26 19:32:45 +000018#include "llvm/ADT/DenseSet.h"
Anders Carlsson5d40c6f2010-02-11 08:02:13 +000019#include "llvm/Support/Format.h"
Zhongxing Xu1721ef72009-11-13 05:46:16 +000020#include <cstdio>
Anders Carlsson2bb27f52009-10-11 22:13:54 +000021
22using namespace clang;
23using namespace CodeGen;
24
Anders Carlssond59885022009-11-27 22:21:51 +000025namespace {
Anders Carlsson5d40c6f2010-02-11 08:02:13 +000026
27/// VtableComponent - Represents a single component in a vtable.
28class VtableComponent {
29public:
30 enum Kind {
31 CK_VCallOffset,
32 CK_VBaseOffset,
33 CK_OffsetToTop,
34 CK_RTTI,
Anders Carlsson5bd8d192010-02-11 18:20:28 +000035 CK_FunctionPointer,
36
37 /// CK_CompleteDtorPointer - A pointer to the complete destructor.
38 CK_CompleteDtorPointer,
39
40 /// CK_DeletingDtorPointer - A pointer to the deleting destructor.
41 CK_DeletingDtorPointer
Anders Carlsson5d40c6f2010-02-11 08:02:13 +000042 };
43
44 /// dump - Dump the contents of this component to the given stream.
45 void dump(llvm::raw_ostream &Out);
46
47 static VtableComponent MakeOffsetToTop(int64_t Offset) {
48 return VtableComponent(CK_OffsetToTop, Offset);
49 }
50
51 static VtableComponent MakeRTTI(const CXXRecordDecl *RD) {
52 return VtableComponent(CK_RTTI, reinterpret_cast<uintptr_t>(RD));
53 }
54
55 static VtableComponent MakeFunction(const CXXMethodDecl *MD) {
56 assert(!isa<CXXDestructorDecl>(MD) &&
Anders Carlsson5bd8d192010-02-11 18:20:28 +000057 "Don't use MakeFunction with destructors!");
Anders Carlsson5d40c6f2010-02-11 08:02:13 +000058
Anders Carlsson5bd8d192010-02-11 18:20:28 +000059 return VtableComponent(CK_FunctionPointer,
Anders Carlsson5d40c6f2010-02-11 08:02:13 +000060 reinterpret_cast<uintptr_t>(MD));
61 }
62
Anders Carlsson5bd8d192010-02-11 18:20:28 +000063 static VtableComponent MakeCompleteDtor(const CXXDestructorDecl *DD) {
64 return VtableComponent(CK_CompleteDtorPointer,
65 reinterpret_cast<uintptr_t>(DD));
66 }
67
68 static VtableComponent MakeDeletingDtor(const CXXDestructorDecl *DD) {
69 return VtableComponent(CK_DeletingDtorPointer,
70 reinterpret_cast<uintptr_t>(DD));
71 }
72
Anders Carlsson5d40c6f2010-02-11 08:02:13 +000073 /// getKind - Get the kind of this vtable component.
74 Kind getKind() const {
75 return (Kind)(Value & 0x7);
76 }
77
78 int64_t getOffsetToTop() const {
79 assert(getKind() == CK_OffsetToTop && "Invalid component kind!");
80
81 return getOffset();
82 }
83
84 const CXXRecordDecl *getRTTIDecl() const {
85 assert(getKind() == CK_RTTI && "Invalid component kind!");
86
87 return reinterpret_cast<CXXRecordDecl *>(getPointer());
88 }
89
90 const CXXMethodDecl *getFunctionDecl() const {
Anders Carlsson5bd8d192010-02-11 18:20:28 +000091 assert(getKind() == CK_FunctionPointer);
Anders Carlsson5d40c6f2010-02-11 08:02:13 +000092
93 return reinterpret_cast<CXXMethodDecl *>(getPointer());
94 }
Anders Carlsson5bd8d192010-02-11 18:20:28 +000095
96 const CXXDestructorDecl *getDestructorDecl() const {
97 assert((getKind() == CK_CompleteDtorPointer ||
98 getKind() == CK_DeletingDtorPointer) && "Invalid component kind!");
99
100 return reinterpret_cast<CXXDestructorDecl *>(getPointer());
101 }
102
Anders Carlsson5d40c6f2010-02-11 08:02:13 +0000103private:
104 VtableComponent(Kind ComponentKind, int64_t Offset) {
105 assert((ComponentKind == CK_VCallOffset ||
106 ComponentKind == CK_VBaseOffset ||
107 ComponentKind == CK_OffsetToTop) && "Invalid component kind!");
108 assert(Offset <= ((1LL << 56) - 1) && "Offset is too big!");
109
110 Value = ((Offset << 3) | ComponentKind);
111 }
112
113 VtableComponent(Kind ComponentKind, uintptr_t Ptr) {
114 assert((ComponentKind == CK_RTTI ||
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000115 ComponentKind == CK_FunctionPointer ||
116 ComponentKind == CK_CompleteDtorPointer ||
117 ComponentKind == CK_DeletingDtorPointer) &&
Anders Carlsson5d40c6f2010-02-11 08:02:13 +0000118 "Invalid component kind!");
119
120 assert((Ptr & 7) == 0 && "Pointer not sufficiently aligned!");
121
122 Value = Ptr | ComponentKind;
123 }
124
125 int64_t getOffset() const {
126 assert((getKind() == CK_VCallOffset || getKind() == CK_VBaseOffset ||
127 getKind() == CK_OffsetToTop) && "Invalid component kind!");
128
129 return Value >> 3;
130 }
131
132 uintptr_t getPointer() const {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000133 assert((getKind() == CK_RTTI ||
134 getKind() == CK_FunctionPointer ||
135 getKind() == CK_CompleteDtorPointer ||
136 getKind() == CK_DeletingDtorPointer) &&
Anders Carlsson5d40c6f2010-02-11 08:02:13 +0000137 "Invalid component kind!");
Anders Carlsson5d40c6f2010-02-11 08:02:13 +0000138
139 return static_cast<uintptr_t>(Value & ~7ULL);
140 }
141
142 /// The kind is stored in the lower 3 bits of the value. For offsets, we
143 /// make use of the facts that classes can't be larger than 2^55 bytes,
144 /// so we store the offset in the lower part of the 61 bytes that remain.
145 /// (The reason that we're not simply using a PointerIntPair here is that we
146 /// need the offsets to be 64-bit, even when on a 32-bit machine).
147 int64_t Value;
148};
149
150/// VtableBuilder - Class for building vtable layout information.
Benjamin Kramer337e3a52009-11-28 19:45:26 +0000151class VtableBuilder {
Anders Carlsson5d40c6f2010-02-11 08:02:13 +0000152 /// MostDerivedClass - The most derived class for which we're building this
153 /// vtable.
154 const CXXRecordDecl *MostDerivedClass;
155
156 /// Context - The ASTContext which we will use for layout information.
157 const ASTContext &Context;
158
159 /// Components - The components of the vtable being built.
160 llvm::SmallVector<VtableComponent, 64> Components;
161
Anders Carlsson932c2f22010-02-11 17:18:51 +0000162 /// AddressPoints - Address points for the vtable being built.
163 CGVtableInfo::AddressPointsMapTy AddressPoints;
164
Anders Carlsson5d40c6f2010-02-11 08:02:13 +0000165 /// layoutSimpleVtable - A test function that will layout very simple vtables
166 /// without any bases. Just used for testing for now.
167 void layoutSimpleVtable(const CXXRecordDecl *RD);
168
169public:
170 VtableBuilder(const CXXRecordDecl *MostDerivedClass)
171 : MostDerivedClass(MostDerivedClass),
172 Context(MostDerivedClass->getASTContext()) {
173
174 layoutSimpleVtable(MostDerivedClass);
175 }
176
177 /// dumpLayout - Dump the vtable layout.
178 void dumpLayout(llvm::raw_ostream&);
179
180};
181
182void VtableBuilder::layoutSimpleVtable(const CXXRecordDecl *RD) {
183 assert(!RD->getNumBases() &&
184 "We don't support layout for vtables with bases right now!");
185
186 // First, add the offset to top.
187 Components.push_back(VtableComponent::MakeOffsetToTop(0));
188
189 // Next, add the RTTI.
190 Components.push_back(VtableComponent::MakeRTTI(RD));
191
Anders Carlsson932c2f22010-02-11 17:18:51 +0000192 // Record the address point.
193 AddressPoints.insert(std::make_pair(BaseSubobject(RD, 0), Components.size()));
194
Anders Carlsson5d40c6f2010-02-11 08:02:13 +0000195 // Now go through all virtual member functions and add them.
196 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
197 E = RD->method_end(); I != E; ++I) {
198 const CXXMethodDecl *MD = *I;
199
200 if (!MD->isVirtual())
201 continue;
202
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000203 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
204 // Add both the complete destructor and the deleting destructor.
205 Components.push_back(VtableComponent::MakeCompleteDtor(DD));
206 Components.push_back(VtableComponent::MakeDeletingDtor(DD));
207 } else {
208 // Add the function.
209 Components.push_back(VtableComponent::MakeFunction(MD));
210 }
Anders Carlsson5d40c6f2010-02-11 08:02:13 +0000211 }
212}
213
214/// dumpLayout - Dump the vtable layout.
215void VtableBuilder::dumpLayout(llvm::raw_ostream& Out) {
216
217 Out << "Vtable for '" << MostDerivedClass->getQualifiedNameAsString();
218 Out << "' (" << Components.size() << " entries).\n";
219
Anders Carlsson932c2f22010-02-11 17:18:51 +0000220 // Iterate through the address points and insert them into a new map where
221 // they are keyed by the index and not the base object.
222 // Since an address point can be shared by multiple subobjects, we use an
223 // STL multimap.
224 std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex;
225 for (CGVtableInfo::AddressPointsMapTy::const_iterator I =
226 AddressPoints.begin(), E = AddressPoints.end(); I != E; ++I) {
227 const BaseSubobject& Base = I->first;
228 uint64_t Index = I->second;
Anders Carlsson5d40c6f2010-02-11 08:02:13 +0000229
Anders Carlsson932c2f22010-02-11 17:18:51 +0000230 AddressPointsByIndex.insert(std::make_pair(Index, Base));
231 }
232
233 for (unsigned I = 0, E = Components.size(); I != E; ++I) {
234 if (AddressPointsByIndex.count(I)) {
235 assert(AddressPointsByIndex.count(I) == 1 &&
236 "FIXME: Handle dumping multiple base subobjects for a single "
237 "address point!");
238
239 const BaseSubobject &Base = AddressPointsByIndex.find(I)->second;
240 Out << " -- (" << Base.getBase()->getQualifiedNameAsString();
241
242 // FIXME: Instead of dividing by 8, we should be using CharUnits.
243 Out << ", " << Base.getBaseOffset() / 8 << ") vtable address --\n";
244 }
245
246 Out << llvm::format("%4d | ", I);
247
Anders Carlsson5d40c6f2010-02-11 08:02:13 +0000248 const VtableComponent &Component = Components[I];
249
250 // Dump the component.
251 switch (Component.getKind()) {
252 // FIXME: Remove this default case.
253 default:
254 assert(false && "Unhandled component kind!");
255 break;
256
257 case VtableComponent::CK_OffsetToTop:
258 Out << "offset_to_top (" << Component.getOffsetToTop() << ")";
259 break;
260
261 case VtableComponent::CK_RTTI:
262 Out << Component.getRTTIDecl()->getQualifiedNameAsString() << " RTTI";
263 break;
264
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000265 case VtableComponent::CK_FunctionPointer: {
Anders Carlsson5d40c6f2010-02-11 08:02:13 +0000266 const CXXMethodDecl *MD = Component.getFunctionDecl();
267
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000268 std::string Str =
269 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
270 MD);
271 Out << Str;
272 break;
273 }
Anders Carlsson5d40c6f2010-02-11 08:02:13 +0000274
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000275 case VtableComponent::CK_CompleteDtorPointer: {
276 const CXXDestructorDecl *DD = Component.getDestructorDecl();
277
278 Out << DD->getQualifiedNameAsString() << "() [complete]";
279 break;
280 }
281
282 case VtableComponent::CK_DeletingDtorPointer: {
283 const CXXDestructorDecl *DD = Component.getDestructorDecl();
284
285 Out << DD->getQualifiedNameAsString() << "() [deleting]";
Anders Carlsson5d40c6f2010-02-11 08:02:13 +0000286 break;
287 }
288
289 }
Anders Carlsson932c2f22010-02-11 17:18:51 +0000290
Anders Carlsson5d40c6f2010-02-11 08:02:13 +0000291 Out << '\n';
292 }
293
294}
295
296}
297
298namespace {
299class OldVtableBuilder {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000300public:
301 /// Index_t - Vtable index type.
302 typedef uint64_t Index_t;
Eli Friedman31bc3ad2009-12-07 23:56:34 +0000303 typedef std::vector<std::pair<GlobalDecl,
304 std::pair<GlobalDecl, ThunkAdjustment> > >
305 SavedAdjustmentsVectorTy;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000306private:
Anders Carlsson472404f2009-12-04 16:19:30 +0000307
Anders Carlssonfe5f7d92009-12-06 01:09:21 +0000308 // VtableComponents - The components of the vtable being built.
309 typedef llvm::SmallVector<llvm::Constant *, 64> VtableComponentsVectorTy;
310 VtableComponentsVectorTy VtableComponents;
311
Eli Friedman6c08ce72009-12-05 01:05:03 +0000312 const bool BuildVtable;
313
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000314 llvm::Type *Ptr8Ty;
Anders Carlssonbad80eb2009-12-04 18:36:22 +0000315
316 /// MostDerivedClass - The most derived class that this vtable is being
317 /// built for.
318 const CXXRecordDecl *MostDerivedClass;
319
Mike Stump83066c82009-11-13 01:54:23 +0000320 /// LayoutClass - The most derived class used for virtual base layout
321 /// information.
322 const CXXRecordDecl *LayoutClass;
Mike Stump653d0b92009-11-13 02:13:54 +0000323 /// LayoutOffset - The offset for Class in LayoutClass.
324 uint64_t LayoutOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000325 /// BLayout - Layout for the most derived class that this vtable is being
326 /// built for.
327 const ASTRecordLayout &BLayout;
328 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
329 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
330 llvm::Constant *rtti;
331 llvm::LLVMContext &VMContext;
332 CodeGenModule &CGM; // Per-module state.
Anders Carlssonf2f31f42009-12-04 03:46:21 +0000333
Mike Stump90181eb2010-01-26 00:05:04 +0000334 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall;
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000335 llvm::DenseMap<GlobalDecl, Index_t> VCallOffset;
Mike Stump77537b12010-01-26 03:42:22 +0000336 llvm::DenseMap<GlobalDecl, Index_t> VCallOffsetForVCall;
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000337 // This is the offset to the nearest virtual base
Mike Stump90181eb2010-01-26 00:05:04 +0000338 llvm::DenseMap<const CXXMethodDecl *, Index_t> NonVirtualOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000339 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stumpbb9ff052009-10-27 23:46:47 +0000340
Anders Carlsson323bb042009-11-26 19:54:33 +0000341 /// PureVirtualFunction - Points to __cxa_pure_virtual.
342 llvm::Constant *PureVirtualFn;
343
Anders Carlssona84b6e82009-12-04 02:01:07 +0000344 /// VtableMethods - A data structure for keeping track of methods in a vtable.
345 /// Can add methods, override methods and iterate in vtable order.
346 class VtableMethods {
347 // MethodToIndexMap - Maps from a global decl to the index it has in the
348 // Methods vector.
349 llvm::DenseMap<GlobalDecl, uint64_t> MethodToIndexMap;
350
351 /// Methods - The methods, in vtable order.
352 typedef llvm::SmallVector<GlobalDecl, 16> MethodsVectorTy;
353 MethodsVectorTy Methods;
Eli Friedman8174f2c2009-12-06 22:01:30 +0000354 MethodsVectorTy OrigMethods;
Anders Carlssona84b6e82009-12-04 02:01:07 +0000355
356 public:
357 /// AddMethod - Add a method to the vtable methods.
358 void AddMethod(GlobalDecl GD) {
359 assert(!MethodToIndexMap.count(GD) &&
360 "Method has already been added!");
361
362 MethodToIndexMap[GD] = Methods.size();
363 Methods.push_back(GD);
Eli Friedman8174f2c2009-12-06 22:01:30 +0000364 OrigMethods.push_back(GD);
Anders Carlssona84b6e82009-12-04 02:01:07 +0000365 }
366
367 /// OverrideMethod - Replace a method with another.
368 void OverrideMethod(GlobalDecl OverriddenGD, GlobalDecl GD) {
369 llvm::DenseMap<GlobalDecl, uint64_t>::iterator i
370 = MethodToIndexMap.find(OverriddenGD);
371 assert(i != MethodToIndexMap.end() && "Did not find entry!");
372
373 // Get the index of the old decl.
374 uint64_t Index = i->second;
375
376 // Replace the old decl with the new decl.
377 Methods[Index] = GD;
378
Anders Carlssona84b6e82009-12-04 02:01:07 +0000379 // And add the new.
380 MethodToIndexMap[GD] = Index;
381 }
382
Anders Carlsson7bb70762009-12-04 15:49:02 +0000383 /// getIndex - Gives the index of a passed in GlobalDecl. Returns false if
384 /// the index couldn't be found.
Benjamin Kramer62ab6162009-12-04 22:45:27 +0000385 bool getIndex(GlobalDecl GD, uint64_t &Index) const {
Anders Carlsson7bb70762009-12-04 15:49:02 +0000386 llvm::DenseMap<GlobalDecl, uint64_t>::const_iterator i
387 = MethodToIndexMap.find(GD);
Eli Friedman3d2e9de2009-12-04 08:34:14 +0000388
Anders Carlsson7bb70762009-12-04 15:49:02 +0000389 if (i == MethodToIndexMap.end())
390 return false;
Anders Carlssone6096362009-12-04 03:41:37 +0000391
Anders Carlsson7bb70762009-12-04 15:49:02 +0000392 Index = i->second;
393 return true;
Anders Carlssone6096362009-12-04 03:41:37 +0000394 }
395
Eli Friedman8174f2c2009-12-06 22:01:30 +0000396 GlobalDecl getOrigMethod(uint64_t Index) const {
397 return OrigMethods[Index];
398 }
399
Anders Carlssona84b6e82009-12-04 02:01:07 +0000400 MethodsVectorTy::size_type size() const {
401 return Methods.size();
402 }
403
404 void clear() {
405 MethodToIndexMap.clear();
406 Methods.clear();
Eli Friedman8174f2c2009-12-06 22:01:30 +0000407 OrigMethods.clear();
Anders Carlssona84b6e82009-12-04 02:01:07 +0000408 }
409
Anders Carlssone6096362009-12-04 03:41:37 +0000410 GlobalDecl operator[](uint64_t Index) const {
Anders Carlssona84b6e82009-12-04 02:01:07 +0000411 return Methods[Index];
412 }
413 };
414
415 /// Methods - The vtable methods we're currently building.
416 VtableMethods Methods;
417
Anders Carlsson4c837d22009-12-04 02:26:15 +0000418 /// ThisAdjustments - For a given index in the vtable, contains the 'this'
419 /// pointer adjustment needed for a method.
420 typedef llvm::DenseMap<uint64_t, ThunkAdjustment> ThisAdjustmentsMapTy;
421 ThisAdjustmentsMapTy ThisAdjustments;
Anders Carlssond420a312009-11-26 19:32:45 +0000422
Eli Friedman31bc3ad2009-12-07 23:56:34 +0000423 SavedAdjustmentsVectorTy SavedAdjustments;
Eli Friedman8174f2c2009-12-06 22:01:30 +0000424
Anders Carlssonc521f952009-12-04 02:22:02 +0000425 /// BaseReturnTypes - Contains the base return types of methods who have been
426 /// overridden with methods whose return types require adjustment. Used for
427 /// generating covariant thunk information.
428 typedef llvm::DenseMap<uint64_t, CanQualType> BaseReturnTypesMapTy;
429 BaseReturnTypesMapTy BaseReturnTypes;
Anders Carlssond420a312009-11-26 19:32:45 +0000430
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000431 std::vector<Index_t> VCalls;
Mike Stump2cefe382009-11-12 20:47:57 +0000432
433 typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t;
Mike Stumpcd2b8212009-11-19 20:52:19 +0000434 // subAddressPoints - Used to hold the AddressPoints (offsets) into the built
435 // vtable for use in computing the initializers for the VTT.
436 llvm::DenseMap<CtorVtable_t, int64_t> &subAddressPoints;
Mike Stump2cefe382009-11-12 20:47:57 +0000437
Anders Carlsson5f9a8812010-01-14 02:29:07 +0000438 /// AddressPoints - Address points for this vtable.
439 CGVtableInfo::AddressPointsMapTy& AddressPoints;
440
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000441 typedef CXXRecordDecl::method_iterator method_iter;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000442 const uint32_t LLVMPointerWidth;
443 Index_t extra;
Mike Stump37dbe962009-10-15 02:04:03 +0000444 typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t;
Mike Stumpcd2b8212009-11-19 20:52:19 +0000445 static llvm::DenseMap<CtorVtable_t, int64_t>&
446 AllocAddressPoint(CodeGenModule &cgm, const CXXRecordDecl *l,
447 const CXXRecordDecl *c) {
Anders Carlsson93a18842010-01-02 18:02:32 +0000448 CGVtableInfo::AddrMap_t *&oref = cgm.getVtableInfo().AddressPoints[l];
Mike Stumpcd2b8212009-11-19 20:52:19 +0000449 if (oref == 0)
Anders Carlsson93a18842010-01-02 18:02:32 +0000450 oref = new CGVtableInfo::AddrMap_t;
Mike Stumpcd2b8212009-11-19 20:52:19 +0000451
452 llvm::DenseMap<CtorVtable_t, int64_t> *&ref = (*oref)[c];
453 if (ref == 0)
454 ref = new llvm::DenseMap<CtorVtable_t, int64_t>;
455 return *ref;
456 }
Anders Carlsson323bb042009-11-26 19:54:33 +0000457
Mike Stump90181eb2010-01-26 00:05:04 +0000458 bool DclIsSame(const FunctionDecl *New, const FunctionDecl *Old) {
459 FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate();
460 FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate();
461
462 // C++ [temp.fct]p2:
463 // A function template can be overloaded with other function templates
464 // and with normal (non-template) functions.
465 if ((OldTemplate == 0) != (NewTemplate == 0))
466 return false;
467
468 // Is the function New an overload of the function Old?
469 QualType OldQType = CGM.getContext().getCanonicalType(Old->getType());
470 QualType NewQType = CGM.getContext().getCanonicalType(New->getType());
471
472 // Compare the signatures (C++ 1.3.10) of the two functions to
473 // determine whether they are overloads. If we find any mismatch
474 // in the signature, they are overloads.
475
476 // If either of these functions is a K&R-style function (no
477 // prototype), then we consider them to have matching signatures.
478 if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) ||
479 isa<FunctionNoProtoType>(NewQType.getTypePtr()))
480 return true;
481
482 FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType);
483 FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType);
484
485 // The signature of a function includes the types of its
486 // parameters (C++ 1.3.10), which includes the presence or absence
487 // of the ellipsis; see C++ DR 357).
488 if (OldQType != NewQType &&
489 (OldType->getNumArgs() != NewType->getNumArgs() ||
490 OldType->isVariadic() != NewType->isVariadic() ||
491 !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(),
492 NewType->arg_type_begin())))
493 return false;
494
495#if 0
496 // C++ [temp.over.link]p4:
497 // The signature of a function template consists of its function
498 // signature, its return type and its template parameter list. The names
499 // of the template parameters are significant only for establishing the
500 // relationship between the template parameters and the rest of the
501 // signature.
502 //
503 // We check the return type and template parameter lists for function
504 // templates first; the remaining checks follow.
505 if (NewTemplate &&
506 (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(),
507 OldTemplate->getTemplateParameters(),
508 TPL_TemplateMatch) ||
509 OldType->getResultType() != NewType->getResultType()))
510 return false;
511#endif
512
513 // If the function is a class member, its signature includes the
514 // cv-qualifiers (if any) on the function itself.
515 //
516 // As part of this, also check whether one of the member functions
517 // is static, in which case they are not overloads (C++
518 // 13.1p2). While not part of the definition of the signature,
519 // this check is important to determine whether these functions
520 // can be overloaded.
521 const CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old);
522 const CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New);
523 if (OldMethod && NewMethod &&
524 !OldMethod->isStatic() && !NewMethod->isStatic() &&
525 OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers())
526 return false;
527
528 // The signatures match; this is not an overload.
529 return true;
530 }
531
532 typedef llvm::DenseMap<const CXXMethodDecl *, const CXXMethodDecl*>
533 ForwardUnique_t;
534 ForwardUnique_t ForwardUnique;
535 llvm::DenseMap<const CXXMethodDecl*, const CXXMethodDecl*> UniqueOverrider;
536
537 void BuildUniqueOverrider(const CXXMethodDecl *U, const CXXMethodDecl *MD) {
538 const CXXMethodDecl *PrevU = UniqueOverrider[MD];
539 assert(U && "no unique overrider");
540 if (PrevU == U)
541 return;
542 if (PrevU != U && PrevU != 0) {
543 // If already set, note the two sets as the same
544 if (0)
545 printf("%s::%s same as %s::%s\n",
546 PrevU->getParent()->getNameAsCString(),
547 PrevU->getNameAsCString(),
548 U->getParent()->getNameAsCString(),
549 U->getNameAsCString());
550 ForwardUnique[PrevU] = U;
551 return;
552 }
553
554 // Not set, set it now
555 if (0)
556 printf("marking %s::%s %p override as %s::%s\n",
557 MD->getParent()->getNameAsCString(),
558 MD->getNameAsCString(),
559 (void*)MD,
560 U->getParent()->getNameAsCString(),
561 U->getNameAsCString());
562 UniqueOverrider[MD] = U;
563
564 for (CXXMethodDecl::method_iterator mi = MD->begin_overridden_methods(),
565 me = MD->end_overridden_methods(); mi != me; ++mi) {
566 BuildUniqueOverrider(U, *mi);
567 }
568 }
569
570 void BuildUniqueOverriders(const CXXRecordDecl *RD) {
571 if (0) printf("walking %s\n", RD->getNameAsCString());
572 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
573 e = RD->method_end(); i != e; ++i) {
574 const CXXMethodDecl *MD = *i;
575 if (!MD->isVirtual())
576 continue;
577
578 if (UniqueOverrider[MD] == 0) {
579 // Only set this, if it hasn't been set yet.
580 BuildUniqueOverrider(MD, MD);
581 if (0)
582 printf("top set is %s::%s %p\n",
583 MD->getParent()->getNameAsCString(),
584 MD->getNameAsCString(),
585 (void*)MD);
586 ForwardUnique[MD] = MD;
587 }
588 }
589 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
590 e = RD->bases_end(); i != e; ++i) {
591 const CXXRecordDecl *Base =
592 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
593 BuildUniqueOverriders(Base);
594 }
595 }
596
597 static int DclCmp(const void *p1, const void *p2) {
598 const CXXMethodDecl *MD1 = (const CXXMethodDecl *)p1;
599 const CXXMethodDecl *MD2 = (const CXXMethodDecl *)p2;
600 return (MD1->getIdentifier() - MD2->getIdentifier());
601 }
602
603 void MergeForwarding() {
604 typedef llvm::SmallVector<const CXXMethodDecl *, 100> A_t;
605 A_t A;
606 for (ForwardUnique_t::iterator I = ForwardUnique.begin(),
607 E = ForwardUnique.end(); I != E; ++I) {
608 if (I->first == I->second)
609 // Only add the roots of all trees
610 A.push_back(I->first);
611 }
612 llvm::array_pod_sort(A.begin(), A.end(), DclCmp);
613 for (A_t::iterator I = A.begin(),
614 E = A.end(); I != E; ++I) {
615 A_t::iterator J = I;
616 while (++J != E && DclCmp(*I, *J) == 0)
617 if (DclIsSame(*I, *J)) {
618 printf("connecting %s\n", (*I)->getNameAsCString());
619 ForwardUnique[*J] = *I;
620 }
621 }
622 }
623
624 const CXXMethodDecl *getUnique(const CXXMethodDecl *MD) {
625 const CXXMethodDecl *U = UniqueOverrider[MD];
626 assert(U && "unique overrider not found");
627 while (ForwardUnique.count(U)) {
628 const CXXMethodDecl *NU = ForwardUnique[U];
629 if (NU == U) break;
630 U = NU;
631 }
632 return U;
633 }
Anders Carlsson72281172010-01-26 17:36:47 +0000634
635 GlobalDecl getUnique(GlobalDecl GD) {
636 const CXXMethodDecl *Unique = getUnique(cast<CXXMethodDecl>(GD.getDecl()));
637
638 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Unique))
639 return GlobalDecl(CD, GD.getCtorType());
640
641 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(Unique))
642 return GlobalDecl(DD, GD.getDtorType());
643
644 return Unique;
Mike Stump90181eb2010-01-26 00:05:04 +0000645 }
646
Anders Carlsson323bb042009-11-26 19:54:33 +0000647 /// getPureVirtualFn - Return the __cxa_pure_virtual function.
648 llvm::Constant* getPureVirtualFn() {
649 if (!PureVirtualFn) {
650 const llvm::FunctionType *Ty =
651 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
652 /*isVarArg=*/false);
653 PureVirtualFn = wrap(CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual"));
654 }
655
656 return PureVirtualFn;
657 }
658
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000659public:
Anders Carlsson5d40c6f2010-02-11 08:02:13 +0000660 OldVtableBuilder(const CXXRecordDecl *MostDerivedClass,
Eli Friedman6c08ce72009-12-05 01:05:03 +0000661 const CXXRecordDecl *l, uint64_t lo, CodeGenModule &cgm,
Anders Carlsson5f9a8812010-01-14 02:29:07 +0000662 bool build, CGVtableInfo::AddressPointsMapTy& AddressPoints)
Eli Friedman6c08ce72009-12-05 01:05:03 +0000663 : BuildVtable(build), MostDerivedClass(MostDerivedClass), LayoutClass(l),
664 LayoutOffset(lo), BLayout(cgm.getContext().getASTRecordLayout(l)),
665 rtti(0), VMContext(cgm.getModule().getContext()),CGM(cgm),
666 PureVirtualFn(0),
Anders Carlssonbad80eb2009-12-04 18:36:22 +0000667 subAddressPoints(AllocAddressPoint(cgm, l, MostDerivedClass)),
Anders Carlsson5f9a8812010-01-14 02:29:07 +0000668 AddressPoints(AddressPoints),
669 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0))
670 {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000671 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Anders Carlsson3f4336c2009-12-17 07:09:17 +0000672 if (BuildVtable) {
673 QualType ClassType = CGM.getContext().getTagDeclType(MostDerivedClass);
674 rtti = CGM.GetAddrOfRTTIDescriptor(ClassType);
675 }
Mike Stump90181eb2010-01-26 00:05:04 +0000676 BuildUniqueOverriders(MostDerivedClass);
677 MergeForwarding();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000678 }
679
Anders Carlssonfe5f7d92009-12-06 01:09:21 +0000680 // getVtableComponents - Returns a reference to the vtable components.
681 const VtableComponentsVectorTy &getVtableComponents() const {
682 return VtableComponents;
Anders Carlsson472404f2009-12-04 16:19:30 +0000683 }
684
Anders Carlssone36a6b32010-01-02 01:01:18 +0000685 llvm::DenseMap<const CXXRecordDecl *, uint64_t> &getVBIndex()
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000686 { return VBIndex; }
687
Eli Friedman31bc3ad2009-12-07 23:56:34 +0000688 SavedAdjustmentsVectorTy &getSavedAdjustments()
689 { return SavedAdjustments; }
Eli Friedman8174f2c2009-12-06 22:01:30 +0000690
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000691 llvm::Constant *wrap(Index_t i) {
692 llvm::Constant *m;
693 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
694 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
695 }
696
697 llvm::Constant *wrap(llvm::Constant *m) {
698 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
699 }
700
Mike Stumpd2808442010-01-22 02:51:26 +0000701//#define D1(x)
702#define D1(X) do { if (getenv("DEBUG")) { X; } } while (0)
Mike Stump75ce5732009-10-31 20:06:59 +0000703
704 void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset,
Mike Stump28431212009-10-13 22:54:56 +0000705 bool updateVBIndex, Index_t current_vbindex) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000706 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
707 e = RD->bases_end(); i != e; ++i) {
708 const CXXRecordDecl *Base =
709 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump28431212009-10-13 22:54:56 +0000710 Index_t next_vbindex = current_vbindex;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000711 if (i->isVirtual() && !SeenVBase.count(Base)) {
712 SeenVBase.insert(Base);
Mike Stump28431212009-10-13 22:54:56 +0000713 if (updateVBIndex) {
Mike Stump75ce5732009-10-31 20:06:59 +0000714 next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8)
Mike Stump28431212009-10-13 22:54:56 +0000715 - 3*LLVMPointerWidth/8);
716 VBIndex[Base] = next_vbindex;
717 }
Mike Stump75ce5732009-10-31 20:06:59 +0000718 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
719 VCalls.push_back((0?700:0) + BaseOffset);
720 D1(printf(" vbase for %s at %d delta %d most derived %s\n",
721 Base->getNameAsCString(),
722 (int)-VCalls.size()-3, (int)BaseOffset,
Mike Stumpd2808442010-01-22 02:51:26 +0000723 MostDerivedClass->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000724 }
Mike Stump28431212009-10-13 22:54:56 +0000725 // We also record offsets for non-virtual bases to closest enclosing
726 // virtual base. We do this so that we don't have to search
727 // for the nearst virtual base class when generating thunks.
728 if (updateVBIndex && VBIndex.count(Base) == 0)
729 VBIndex[Base] = next_vbindex;
Mike Stump75ce5732009-10-31 20:06:59 +0000730 GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000731 }
732 }
733
734 void StartNewTable() {
735 SeenVBase.clear();
736 }
737
Mike Stump8bccbfd2009-10-15 09:30:16 +0000738 Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B,
739 Index_t Offset = 0) {
740
741 if (B == D)
742 return Offset;
743
744 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D);
745 for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(),
746 e = D->bases_end(); i != e; ++i) {
747 const CXXRecordDecl *Base =
748 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
749 int64_t BaseOffset = 0;
750 if (!i->isVirtual())
751 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
752 int64_t o = getNVOffset_1(Base, B, BaseOffset);
753 if (o >= 0)
754 return o;
755 }
756
757 return -1;
758 }
759
760 /// getNVOffset - Returns the non-virtual offset for the given (B) base of the
761 /// derived class D.
762 Index_t getNVOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000763 qD = qD->getPointeeType();
764 qB = qB->getPointeeType();
Mike Stump8bccbfd2009-10-15 09:30:16 +0000765 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
766 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
767 int64_t o = getNVOffset_1(D, B);
768 if (o >= 0)
769 return o;
770
771 assert(false && "FIXME: non-virtual base not found");
772 return 0;
773 }
774
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000775 /// getVbaseOffset - Returns the index into the vtable for the virtual base
776 /// offset for the given (B) virtual base of the derived class D.
777 Index_t getVbaseOffset(QualType qB, QualType qD) {
Mike Stump46271322009-11-03 19:03:17 +0000778 qD = qD->getPointeeType();
779 qB = qB->getPointeeType();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000780 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
781 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
Anders Carlssonbad80eb2009-12-04 18:36:22 +0000782 if (D != MostDerivedClass)
Eli Friedman03aa2f12009-11-30 01:19:33 +0000783 return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000784 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
785 i = VBIndex.find(B);
786 if (i != VBIndex.end())
787 return i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000788
Mike Stump28431212009-10-13 22:54:56 +0000789 assert(false && "FIXME: Base not found");
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000790 return 0;
791 }
792
Eli Friedman81fb0d22009-12-04 08:40:51 +0000793 bool OverrideMethod(GlobalDecl GD, bool MorallyVirtual,
794 Index_t OverrideOffset, Index_t Offset,
795 int64_t CurrentVBaseOffset);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000796
Anders Carlsson495634e2009-12-04 02:39:04 +0000797 /// AppendMethods - Append the current methods to the vtable.
Anders Carlssonddf42c82009-12-04 02:56:03 +0000798 void AppendMethodsToVtable();
Anders Carlsson495634e2009-12-04 02:39:04 +0000799
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000800 llvm::Constant *WrapAddrOf(GlobalDecl GD) {
801 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
802
Anders Carlsson64457732009-11-24 05:08:52 +0000803 const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD);
Mike Stump18e8b472009-10-27 23:36:26 +0000804
Mike Stumpcdeb8002009-12-03 16:55:20 +0000805 return wrap(CGM.GetAddrOfFunction(GD, Ty));
Mike Stump18e8b472009-10-27 23:36:26 +0000806 }
807
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000808 void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset,
809 int64_t CurrentVBaseOffset) {
Mike Stump37dbe962009-10-15 02:04:03 +0000810 for (Path_t::reverse_iterator i = Path->rbegin(),
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000811 e = Path->rend(); i != e; ++i) {
812 const CXXRecordDecl *RD = i->first;
Mike Stump8bccbfd2009-10-15 09:30:16 +0000813 int64_t OverrideOffset = i->second;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000814 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
815 ++mi) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000816 const CXXMethodDecl *MD = *mi;
817
818 if (!MD->isVirtual())
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000819 continue;
820
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000821 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
822 // Override both the complete and the deleting destructor.
823 GlobalDecl CompDtor(DD, Dtor_Complete);
Eli Friedman81fb0d22009-12-04 08:40:51 +0000824 OverrideMethod(CompDtor, MorallyVirtual, OverrideOffset, Offset,
825 CurrentVBaseOffset);
826
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000827 GlobalDecl DeletingDtor(DD, Dtor_Deleting);
Eli Friedman81fb0d22009-12-04 08:40:51 +0000828 OverrideMethod(DeletingDtor, MorallyVirtual, OverrideOffset, Offset,
829 CurrentVBaseOffset);
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000830 } else {
Eli Friedman81fb0d22009-12-04 08:40:51 +0000831 OverrideMethod(MD, MorallyVirtual, OverrideOffset, Offset,
832 CurrentVBaseOffset);
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000833 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000834 }
835 }
836 }
837
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000838 void AddMethod(const GlobalDecl GD, bool MorallyVirtual, Index_t Offset,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000839 int64_t CurrentVBaseOffset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000840 // If we can find a previously allocated slot for this, reuse it.
Eli Friedman81fb0d22009-12-04 08:40:51 +0000841 if (OverrideMethod(GD, MorallyVirtual, Offset, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000842 CurrentVBaseOffset))
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000843 return;
844
Mike Stump1f49d652010-01-22 18:48:47 +0000845 D1(printf(" vfn for %s at %d\n",
846 dyn_cast<CXXMethodDecl>(GD.getDecl())->getNameAsCString(),
847 (int)Methods.size()));
848
Anders Carlssoncdf18982009-12-04 02:08:24 +0000849 // We didn't find an entry in the vtable that we could use, add a new
850 // entry.
851 Methods.AddMethod(GD);
852
Mike Stumpa04ecfb2010-01-26 21:35:27 +0000853 VCallOffset[GD] = Offset/8 - CurrentVBaseOffset/8;
854
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000855 if (MorallyVirtual) {
Anders Carlsson72281172010-01-26 17:36:47 +0000856 GlobalDecl UGD = getUnique(GD);
857 const CXXMethodDecl *UMD = cast<CXXMethodDecl>(UGD.getDecl());
858
Mike Stump90181eb2010-01-26 00:05:04 +0000859 assert(UMD && "final overrider not found");
860
861 Index_t &idx = VCall[UMD];
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000862 // Allocate the first one, after that, we reuse the previous one.
863 if (idx == 0) {
Anders Carlsson72281172010-01-26 17:36:47 +0000864 VCallOffsetForVCall[UGD] = Offset/8;
Mike Stumpa04ecfb2010-01-26 21:35:27 +0000865 NonVirtualOffset[UMD] = Offset/8 - CurrentVBaseOffset/8;
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000866 idx = VCalls.size()+1;
Mike Stump90181eb2010-01-26 00:05:04 +0000867 VCalls.push_back(Offset/8 - CurrentVBaseOffset/8);
Mike Stump75ce5732009-10-31 20:06:59 +0000868 D1(printf(" vcall for %s at %d with delta %d\n",
Mike Stumpd2808442010-01-22 02:51:26 +0000869 dyn_cast<CXXMethodDecl>(GD.getDecl())->getNameAsCString(),
Mike Stump90181eb2010-01-26 00:05:04 +0000870 (int)-VCalls.size()-3, (int)VCalls[idx-1]));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000871 }
872 }
873 }
874
875 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
Eli Friedman03aa2f12009-11-30 01:19:33 +0000876 Index_t Offset, int64_t CurrentVBaseOffset) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000877 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000878 ++mi) {
879 const CXXMethodDecl *MD = *mi;
880 if (!MD->isVirtual())
881 continue;
882
883 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
884 // For destructors, add both the complete and the deleting destructor
885 // to the vtable.
886 AddMethod(GlobalDecl(DD, Dtor_Complete), MorallyVirtual, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000887 CurrentVBaseOffset);
Eli Friedman03aa2f12009-11-30 01:19:33 +0000888 AddMethod(GlobalDecl(DD, Dtor_Deleting), MorallyVirtual, Offset,
889 CurrentVBaseOffset);
890 } else
891 AddMethod(MD, MorallyVirtual, Offset, CurrentVBaseOffset);
Anders Carlssonfb4dda42009-11-13 17:08:56 +0000892 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000893 }
894
895 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
896 const CXXRecordDecl *PrimaryBase,
897 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +0000898 int64_t Offset, int64_t CurrentVBaseOffset,
899 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000900 Path->push_back(std::make_pair(RD, Offset));
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000901 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
902 e = RD->bases_end(); i != e; ++i) {
903 if (i->isVirtual())
904 continue;
905 const CXXRecordDecl *Base =
906 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stump9eb76d42010-01-22 06:45:05 +0000907 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
908 StartNewTable();
909 GenerateVtableForBase(Base, o, MorallyVirtual, false,
910 true, Base == PrimaryBase && !PrimaryBaseWasVirtual,
911 CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000912 }
Mike Stump37dbe962009-10-15 02:04:03 +0000913 Path->pop_back();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000914 }
915
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000916// #define D(X) do { X; } while (0)
917#define D(X)
918
919 void insertVCalls(int InsertionPoint) {
Mike Stump75ce5732009-10-31 20:06:59 +0000920 D1(printf("============= combining vbase/vcall\n"));
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000921 D(VCalls.insert(VCalls.begin(), 673));
922 D(VCalls.push_back(672));
Eli Friedman6c08ce72009-12-05 01:05:03 +0000923
Anders Carlssonfe5f7d92009-12-06 01:09:21 +0000924 VtableComponents.insert(VtableComponents.begin() + InsertionPoint,
925 VCalls.size(), 0);
Eli Friedman6c08ce72009-12-05 01:05:03 +0000926 if (BuildVtable) {
927 // The vcalls come first...
928 for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(),
929 e = VCalls.rend();
930 i != e; ++i)
Anders Carlssonfe5f7d92009-12-06 01:09:21 +0000931 VtableComponents[InsertionPoint++] = wrap((0?600:0) + *i);
Eli Friedman6c08ce72009-12-05 01:05:03 +0000932 }
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000933 VCalls.clear();
Mike Stump9f23a142009-11-10 02:30:51 +0000934 VCall.clear();
Mike Stumpa04ecfb2010-01-26 21:35:27 +0000935 VCallOffsetForVCall.clear();
936 VCallOffset.clear();
937 NonVirtualOffset.clear();
Mike Stumpb21c4ee2009-10-14 18:14:51 +0000938 }
939
Mike Stump559387f2009-11-13 23:13:20 +0000940 void AddAddressPoints(const CXXRecordDecl *RD, uint64_t Offset,
941 Index_t AddressPoint) {
942 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
Mike Stumpd2808442010-01-22 02:51:26 +0000943 RD->getNameAsCString(), MostDerivedClass->getNameAsCString(),
Mike Stump559387f2009-11-13 23:13:20 +0000944 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stumpcd2b8212009-11-19 20:52:19 +0000945 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Anders Carlsson5f9a8812010-01-14 02:29:07 +0000946 AddressPoints[BaseSubobject(RD, Offset)] = AddressPoint;
Mike Stump559387f2009-11-13 23:13:20 +0000947
948 // Now also add the address point for all our primary bases.
949 while (1) {
950 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
951 RD = Layout.getPrimaryBase();
952 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
953 // FIXME: Double check this.
954 if (RD == 0)
955 break;
956 if (PrimaryBaseWasVirtual &&
957 BLayout.getVBaseClassOffset(RD) != Offset)
958 break;
959 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
Mike Stumpd2808442010-01-22 02:51:26 +0000960 RD->getNameAsCString(), MostDerivedClass->getNameAsCString(),
Mike Stump559387f2009-11-13 23:13:20 +0000961 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stumpcd2b8212009-11-19 20:52:19 +0000962 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Anders Carlsson5f9a8812010-01-14 02:29:07 +0000963 AddressPoints[BaseSubobject(RD, Offset)] = AddressPoint;
Mike Stump559387f2009-11-13 23:13:20 +0000964 }
965 }
966
967
Mike Stumpd538a6d2009-12-24 07:29:41 +0000968 void FinishGenerateVtable(const CXXRecordDecl *RD,
969 const ASTRecordLayout &Layout,
970 const CXXRecordDecl *PrimaryBase,
Mike Stump9eb76d42010-01-22 06:45:05 +0000971 bool ForNPNVBases, bool WasPrimaryBase,
Mike Stumpd538a6d2009-12-24 07:29:41 +0000972 bool PrimaryBaseWasVirtual,
973 bool MorallyVirtual, int64_t Offset,
974 bool ForVirtualBase, int64_t CurrentVBaseOffset,
975 Path_t *Path) {
Mike Stump37dbe962009-10-15 02:04:03 +0000976 bool alloc = false;
977 if (Path == 0) {
978 alloc = true;
979 Path = new Path_t;
980 }
981
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000982 StartNewTable();
983 extra = 0;
Mike Stump9eb76d42010-01-22 06:45:05 +0000984 Index_t AddressPoint = 0;
985 int VCallInsertionPoint = 0;
986 if (!ForNPNVBases || !WasPrimaryBase) {
987 bool DeferVCalls = MorallyVirtual || ForVirtualBase;
988 VCallInsertionPoint = VtableComponents.size();
989 if (!DeferVCalls) {
990 insertVCalls(VCallInsertionPoint);
991 } else
992 // FIXME: just for extra, or for all uses of VCalls.size post this?
993 extra = -VCalls.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000994
Mike Stump9eb76d42010-01-22 06:45:05 +0000995 // Add the offset to top.
996 VtableComponents.push_back(BuildVtable ? wrap(-((Offset-LayoutOffset)/8)) : 0);
Anders Carlsson472404f2009-12-04 16:19:30 +0000997
Mike Stump9eb76d42010-01-22 06:45:05 +0000998 // Add the RTTI information.
999 VtableComponents.push_back(rtti);
Anders Carlsson472404f2009-12-04 16:19:30 +00001000
Mike Stump9eb76d42010-01-22 06:45:05 +00001001 AddressPoint = VtableComponents.size();
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001002
Mike Stump9eb76d42010-01-22 06:45:05 +00001003 AppendMethodsToVtable();
1004 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001005
1006 // and then the non-virtual bases.
1007 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +00001008 MorallyVirtual, Offset, CurrentVBaseOffset, Path);
Mike Stumpb21c4ee2009-10-14 18:14:51 +00001009
1010 if (ForVirtualBase) {
Mike Stump2cefe382009-11-12 20:47:57 +00001011 // FIXME: We're adding to VCalls in callers, we need to do the overrides
1012 // in the inner part, so that we know the complete set of vcalls during
1013 // the build and don't have to insert into methods. Saving out the
1014 // AddressPoint here, would need to be fixed, if we didn't do that. Also
1015 // retroactively adding vcalls for overrides later wind up in the wrong
1016 // place, the vcall slot has to be alloted during the walk of the base
1017 // when the function is first introduces.
Mike Stumpb21c4ee2009-10-14 18:14:51 +00001018 AddressPoint += VCalls.size();
Mike Stump2cefe382009-11-12 20:47:57 +00001019 insertVCalls(VCallInsertionPoint);
Mike Stumpb21c4ee2009-10-14 18:14:51 +00001020 }
1021
Mike Stump9eb76d42010-01-22 06:45:05 +00001022 if (!ForNPNVBases || !WasPrimaryBase)
1023 AddAddressPoints(RD, Offset, AddressPoint);
Mike Stump2cefe382009-11-12 20:47:57 +00001024
Mike Stump37dbe962009-10-15 02:04:03 +00001025 if (alloc) {
1026 delete Path;
1027 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001028 }
1029
Mike Stump75ce5732009-10-31 20:06:59 +00001030 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
1031 bool updateVBIndex, Index_t current_vbindex,
Eli Friedman03aa2f12009-11-30 01:19:33 +00001032 int64_t CurrentVBaseOffset) {
Mike Stump75ce5732009-10-31 20:06:59 +00001033 if (!RD->isDynamicClass())
1034 return;
1035
1036 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1037 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1038 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1039
1040 // vtables are composed from the chain of primaries.
Eli Friedman65d87222009-12-04 08:52:11 +00001041 if (PrimaryBase && !PrimaryBaseWasVirtual) {
Mike Stump75ce5732009-10-31 20:06:59 +00001042 D1(printf(" doing primaries for %s most derived %s\n",
Mike Stumpd2808442010-01-22 02:51:26 +00001043 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Eli Friedman65d87222009-12-04 08:52:11 +00001044 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
1045 updateVBIndex, current_vbindex, CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +00001046 }
1047
1048 D1(printf(" doing vcall entries for %s most derived %s\n",
Mike Stumpd2808442010-01-22 02:51:26 +00001049 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Mike Stump75ce5732009-10-31 20:06:59 +00001050
1051 // And add the virtuals for the class to the primary vtable.
Eli Friedman03aa2f12009-11-30 01:19:33 +00001052 AddMethods(RD, MorallyVirtual, Offset, CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +00001053 }
1054
1055 void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
1056 bool updateVBIndex, Index_t current_vbindex,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +00001057 bool RDisVirtualBase, int64_t CurrentVBaseOffset,
Eli Friedman03aa2f12009-11-30 01:19:33 +00001058 bool bottom) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001059 if (!RD->isDynamicClass())
1060 return;
1061
1062 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1063 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1064 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1065
1066 // vtables are composed from the chain of primaries.
1067 if (PrimaryBase) {
Mike Stumpcd6f9ed2009-11-06 23:27:42 +00001068 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
1069 if (PrimaryBaseWasVirtual) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001070 IndirectPrimary.insert(PrimaryBase);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +00001071 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
1072 }
Mike Stump75ce5732009-10-31 20:06:59 +00001073
1074 D1(printf(" doing primaries for %s most derived %s\n",
Mike Stumpd2808442010-01-22 02:51:26 +00001075 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Mike Stump75ce5732009-10-31 20:06:59 +00001076
1077 VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Mike Stumpcd6f9ed2009-11-06 23:27:42 +00001078 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
Eli Friedman03aa2f12009-11-30 01:19:33 +00001079 BaseCurrentVBaseOffset, false);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001080 }
1081
Mike Stump75ce5732009-10-31 20:06:59 +00001082 D1(printf(" doing vbase entries for %s most derived %s\n",
Mike Stumpd2808442010-01-22 02:51:26 +00001083 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Mike Stump75ce5732009-10-31 20:06:59 +00001084 GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex);
1085
1086 if (RDisVirtualBase || bottom) {
1087 Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex,
Eli Friedman03aa2f12009-11-30 01:19:33 +00001088 CurrentVBaseOffset);
Mike Stump75ce5732009-10-31 20:06:59 +00001089 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001090 }
1091
Mike Stumpd538a6d2009-12-24 07:29:41 +00001092 void GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0,
1093 bool MorallyVirtual = false,
1094 bool ForVirtualBase = false,
Mike Stump9eb76d42010-01-22 06:45:05 +00001095 bool ForNPNVBases = false,
1096 bool WasPrimaryBase = true,
Mike Stumpd538a6d2009-12-24 07:29:41 +00001097 int CurrentVBaseOffset = 0,
1098 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001099 if (!RD->isDynamicClass())
Mike Stumpd538a6d2009-12-24 07:29:41 +00001100 return;
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001101
Mike Stumpfa818082009-11-13 02:35:38 +00001102 // Construction vtable don't need parts that have no virtual bases and
1103 // aren't morally virtual.
Anders Carlssonbad80eb2009-12-04 18:36:22 +00001104 if ((LayoutClass != MostDerivedClass) &&
1105 RD->getNumVBases() == 0 && !MorallyVirtual)
Mike Stumpd538a6d2009-12-24 07:29:41 +00001106 return;
Mike Stumpfa818082009-11-13 02:35:38 +00001107
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001108 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1109 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1110 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1111
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001112 extra = 0;
Mike Stump75ce5732009-10-31 20:06:59 +00001113 D1(printf("building entries for base %s most derived %s\n",
Mike Stumpd2808442010-01-22 02:51:26 +00001114 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001115
Mike Stump75ce5732009-10-31 20:06:59 +00001116 if (ForVirtualBase)
1117 extra = VCalls.size();
1118
Mike Stump9eb76d42010-01-22 06:45:05 +00001119 if (!ForNPNVBases || !WasPrimaryBase) {
1120 VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0,
1121 ForVirtualBase, CurrentVBaseOffset, true);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001122
Mike Stump9eb76d42010-01-22 06:45:05 +00001123 if (Path)
1124 OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset);
1125 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001126
Mike Stump9eb76d42010-01-22 06:45:05 +00001127 FinishGenerateVtable(RD, Layout, PrimaryBase, ForNPNVBases, WasPrimaryBase,
1128 PrimaryBaseWasVirtual, MorallyVirtual, Offset,
1129 ForVirtualBase, CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001130 }
1131
1132 void GenerateVtableForVBases(const CXXRecordDecl *RD,
1133 int64_t Offset = 0,
Mike Stump37dbe962009-10-15 02:04:03 +00001134 Path_t *Path = 0) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001135 bool alloc = false;
1136 if (Path == 0) {
1137 alloc = true;
Mike Stump37dbe962009-10-15 02:04:03 +00001138 Path = new Path_t;
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001139 }
1140 // FIXME: We also need to override using all paths to a virtual base,
1141 // right now, we just process the first path
1142 Path->push_back(std::make_pair(RD, Offset));
1143 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1144 e = RD->bases_end(); i != e; ++i) {
1145 const CXXRecordDecl *Base =
1146 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1147 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
1148 // Mark it so we don't output it twice.
1149 IndirectPrimary.insert(Base);
1150 StartNewTable();
Mike Stumpb21c4ee2009-10-14 18:14:51 +00001151 VCall.clear();
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001152 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stumpcd6f9ed2009-11-06 23:27:42 +00001153 int64_t CurrentVBaseOffset = BaseOffset;
Mike Stump75ce5732009-10-31 20:06:59 +00001154 D1(printf("vtable %s virtual base %s\n",
Mike Stumpd2808442010-01-22 02:51:26 +00001155 MostDerivedClass->getNameAsCString(), Base->getNameAsCString()));
Mike Stump9eb76d42010-01-22 06:45:05 +00001156 GenerateVtableForBase(Base, BaseOffset, true, true, false,
1157 true, CurrentVBaseOffset, Path);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001158 }
Mike Stump2cefe382009-11-12 20:47:57 +00001159 int64_t BaseOffset;
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001160 if (i->isVirtual())
1161 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump2cefe382009-11-12 20:47:57 +00001162 else {
1163 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1164 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
1165 }
1166
Mike Stump37dbe962009-10-15 02:04:03 +00001167 if (Base->getNumVBases()) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001168 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump37dbe962009-10-15 02:04:03 +00001169 }
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001170 }
1171 Path->pop_back();
1172 if (alloc)
1173 delete Path;
1174 }
1175};
Anders Carlssonca1bf682009-12-03 01:54:02 +00001176} // end anonymous namespace
1177
Anders Carlsson657f1392009-12-03 02:32:59 +00001178/// TypeConversionRequiresAdjustment - Returns whether conversion from a
1179/// derived type to a base type requires adjustment.
1180static bool
1181TypeConversionRequiresAdjustment(ASTContext &Ctx,
1182 const CXXRecordDecl *DerivedDecl,
1183 const CXXRecordDecl *BaseDecl) {
1184 CXXBasePaths Paths(/*FindAmbiguities=*/false,
1185 /*RecordPaths=*/true, /*DetectVirtual=*/true);
1186 if (!const_cast<CXXRecordDecl *>(DerivedDecl)->
1187 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseDecl), Paths)) {
1188 assert(false && "Class must be derived from the passed in base class!");
1189 return false;
1190 }
1191
1192 // If we found a virtual base we always want to require adjustment.
1193 if (Paths.getDetectedVirtual())
1194 return true;
1195
1196 const CXXBasePath &Path = Paths.front();
1197
1198 for (size_t Start = 0, End = Path.size(); Start != End; ++Start) {
1199 const CXXBasePathElement &Element = Path[Start];
1200
1201 // Check the base class offset.
1202 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Element.Class);
1203
1204 const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>();
1205 const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl());
1206
1207 if (Layout.getBaseClassOffset(Base) != 0) {
1208 // This requires an adjustment.
1209 return true;
1210 }
1211 }
1212
1213 return false;
1214}
1215
1216static bool
1217TypeConversionRequiresAdjustment(ASTContext &Ctx,
1218 QualType DerivedType, QualType BaseType) {
1219 // Canonicalize the types.
1220 QualType CanDerivedType = Ctx.getCanonicalType(DerivedType);
1221 QualType CanBaseType = Ctx.getCanonicalType(BaseType);
1222
1223 assert(CanDerivedType->getTypeClass() == CanBaseType->getTypeClass() &&
1224 "Types must have same type class!");
1225
1226 if (CanDerivedType == CanBaseType) {
1227 // No adjustment needed.
1228 return false;
1229 }
1230
1231 if (const ReferenceType *RT = dyn_cast<ReferenceType>(CanDerivedType)) {
1232 CanDerivedType = RT->getPointeeType();
1233 CanBaseType = cast<ReferenceType>(CanBaseType)->getPointeeType();
1234 } else if (const PointerType *PT = dyn_cast<PointerType>(CanDerivedType)) {
1235 CanDerivedType = PT->getPointeeType();
1236 CanBaseType = cast<PointerType>(CanBaseType)->getPointeeType();
1237 } else {
1238 assert(false && "Unexpected return type!");
1239 }
1240
1241 if (CanDerivedType == CanBaseType) {
1242 // No adjustment needed.
1243 return false;
1244 }
1245
1246 const CXXRecordDecl *DerivedDecl =
Anders Carlssona4424992009-12-30 23:47:56 +00001247 cast<CXXRecordDecl>(cast<RecordType>(CanDerivedType)->getDecl());
Anders Carlsson657f1392009-12-03 02:32:59 +00001248
1249 const CXXRecordDecl *BaseDecl =
Anders Carlssona4424992009-12-30 23:47:56 +00001250 cast<CXXRecordDecl>(cast<RecordType>(CanBaseType)->getDecl());
Anders Carlsson657f1392009-12-03 02:32:59 +00001251
1252 return TypeConversionRequiresAdjustment(Ctx, DerivedDecl, BaseDecl);
1253}
1254
Anders Carlsson5d40c6f2010-02-11 08:02:13 +00001255bool OldVtableBuilder::OverrideMethod(GlobalDecl GD, bool MorallyVirtual,
Eli Friedman81fb0d22009-12-04 08:40:51 +00001256 Index_t OverrideOffset, Index_t Offset,
1257 int64_t CurrentVBaseOffset) {
Anders Carlssonca1bf682009-12-03 01:54:02 +00001258 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1259
1260 const bool isPure = MD->isPure();
Anders Carlsson21bbc1e2009-12-05 17:04:47 +00001261
Anders Carlssonca1bf682009-12-03 01:54:02 +00001262 // FIXME: Should OverrideOffset's be Offset?
1263
Anders Carlsson21bbc1e2009-12-05 17:04:47 +00001264 for (CXXMethodDecl::method_iterator mi = MD->begin_overridden_methods(),
1265 e = MD->end_overridden_methods(); mi != e; ++mi) {
Anders Carlssonca1bf682009-12-03 01:54:02 +00001266 GlobalDecl OGD;
Mike Stumpa04ecfb2010-01-26 21:35:27 +00001267 GlobalDecl OGD2;
Anders Carlssonca1bf682009-12-03 01:54:02 +00001268
Anders Carlssonf3935b42009-12-04 05:51:56 +00001269 const CXXMethodDecl *OMD = *mi;
Anders Carlssonca1bf682009-12-03 01:54:02 +00001270 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(OMD))
1271 OGD = GlobalDecl(DD, GD.getDtorType());
1272 else
1273 OGD = OMD;
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001274
Eli Friedman8174f2c2009-12-06 22:01:30 +00001275 // Check whether this is the method being overridden in this section of
1276 // the vtable.
Anders Carlsson7bb70762009-12-04 15:49:02 +00001277 uint64_t Index;
1278 if (!Methods.getIndex(OGD, Index))
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001279 continue;
1280
Mike Stumpa04ecfb2010-01-26 21:35:27 +00001281 OGD2 = OGD;
1282
Eli Friedman8174f2c2009-12-06 22:01:30 +00001283 // Get the original method, which we should be computing thunks, etc,
1284 // against.
1285 OGD = Methods.getOrigMethod(Index);
1286 OMD = cast<CXXMethodDecl>(OGD.getDecl());
1287
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001288 QualType ReturnType =
1289 MD->getType()->getAs<FunctionType>()->getResultType();
1290 QualType OverriddenReturnType =
1291 OMD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlssonca1bf682009-12-03 01:54:02 +00001292
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001293 // Check if we need a return type adjustment.
1294 if (TypeConversionRequiresAdjustment(CGM.getContext(), ReturnType,
1295 OverriddenReturnType)) {
1296 CanQualType &BaseReturnType = BaseReturnTypes[Index];
Anders Carlssonca1bf682009-12-03 01:54:02 +00001297
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001298 // Insert the base return type.
1299 if (BaseReturnType.isNull())
1300 BaseReturnType =
1301 CGM.getContext().getCanonicalType(OverriddenReturnType);
1302 }
Anders Carlssona93e9802009-12-04 03:52:52 +00001303
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001304 Methods.OverrideMethod(OGD, GD);
1305
Anders Carlsson72281172010-01-26 17:36:47 +00001306 GlobalDecl UGD = getUnique(GD);
1307 const CXXMethodDecl *UMD = cast<CXXMethodDecl>(UGD.getDecl());
1308 assert(UGD.getDecl() && "unique overrider not found");
1309 assert(UGD == getUnique(OGD) && "unique overrider not unique");
Mike Stump90181eb2010-01-26 00:05:04 +00001310
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001311 ThisAdjustments.erase(Index);
Mike Stump90181eb2010-01-26 00:05:04 +00001312 if (MorallyVirtual || VCall.count(UMD)) {
Mike Stumpa04ecfb2010-01-26 21:35:27 +00001313
Mike Stump90181eb2010-01-26 00:05:04 +00001314 Index_t &idx = VCall[UMD];
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001315 if (idx == 0) {
Mike Stumpa04ecfb2010-01-26 21:35:27 +00001316 VCallOffset[GD] = VCallOffset[OGD];
1317 // NonVirtualOffset[UMD] = CurrentVBaseOffset/8 - OverrideOffset/8;
1318 NonVirtualOffset[UMD] = VCallOffset[OGD];
Mike Stump77537b12010-01-26 03:42:22 +00001319 VCallOffsetForVCall[UMD] = OverrideOffset/8;
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001320 idx = VCalls.size()+1;
Mike Stump77537b12010-01-26 03:42:22 +00001321 VCalls.push_back(OverrideOffset/8 - CurrentVBaseOffset/8);
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001322 D1(printf(" vcall for %s at %d with delta %d most derived %s\n",
1323 MD->getNameAsString().c_str(), (int)-idx-3,
Mike Stumpd2808442010-01-22 02:51:26 +00001324 (int)VCalls[idx-1], MostDerivedClass->getNameAsCString()));
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001325 } else {
Mike Stumpa04ecfb2010-01-26 21:35:27 +00001326 VCallOffset[GD] = NonVirtualOffset[UMD];
Anders Carlsson72281172010-01-26 17:36:47 +00001327 VCalls[idx-1] = -VCallOffsetForVCall[UGD] + OverrideOffset/8;
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001328 D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n",
1329 MD->getNameAsString().c_str(), (int)-idx-3,
Mike Stumpd2808442010-01-22 02:51:26 +00001330 (int)VCalls[idx-1], MostDerivedClass->getNameAsCString()));
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001331 }
Mike Stumpa04ecfb2010-01-26 21:35:27 +00001332 int64_t NonVirtualAdjustment = -VCallOffset[OGD];
Mike Stumpded0a402010-01-26 22:44:01 +00001333 QualType DerivedType = MD->getThisType(CGM.getContext());
1334 QualType BaseType = cast<const CXXMethodDecl>(OGD.getDecl())->getThisType(CGM.getContext());
1335 int64_t NonVirtualAdjustment2 = -(getNVOffset(BaseType, DerivedType)/8);
1336 if (NonVirtualAdjustment2 != NonVirtualAdjustment) {
1337 NonVirtualAdjustment = NonVirtualAdjustment2;
1338 }
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001339 int64_t VirtualAdjustment =
1340 -((idx + extra + 2) * LLVMPointerWidth / 8);
Anders Carlsson657f1392009-12-03 02:32:59 +00001341
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001342 // Optimize out virtual adjustments of 0.
1343 if (VCalls[idx-1] == 0)
1344 VirtualAdjustment = 0;
Anders Carlsson657f1392009-12-03 02:32:59 +00001345
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001346 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment,
1347 VirtualAdjustment);
Anders Carlsson2ca285f2009-12-03 02:22:59 +00001348
Eli Friedman8174f2c2009-12-06 22:01:30 +00001349 if (!isPure && !ThisAdjustment.isEmpty()) {
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001350 ThisAdjustments[Index] = ThisAdjustment;
Eli Friedman31bc3ad2009-12-07 23:56:34 +00001351 SavedAdjustments.push_back(
1352 std::make_pair(GD, std::make_pair(OGD, ThisAdjustment)));
Eli Friedman8174f2c2009-12-06 22:01:30 +00001353 }
Anders Carlssonca1bf682009-12-03 01:54:02 +00001354 return true;
1355 }
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001356
Mike Stumpa04ecfb2010-01-26 21:35:27 +00001357 VCallOffset[GD] = VCallOffset[OGD2] - OverrideOffset/8;
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001358
Mike Stumpa04ecfb2010-01-26 21:35:27 +00001359 int64_t NonVirtualAdjustment = -VCallOffset[GD];
1360 QualType DerivedType = MD->getThisType(CGM.getContext());
1361 QualType BaseType = cast<const CXXMethodDecl>(OGD.getDecl())->getThisType(CGM.getContext());
1362 int64_t NonVirtualAdjustment2 = -(getNVOffset(BaseType, DerivedType)/8);
1363 if (NonVirtualAdjustment2 != NonVirtualAdjustment) {
1364 NonVirtualAdjustment = NonVirtualAdjustment2;
1365 }
1366
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001367 if (NonVirtualAdjustment) {
1368 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, 0);
1369
Eli Friedmanf2eda5e2009-12-06 22:33:51 +00001370 if (!isPure) {
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001371 ThisAdjustments[Index] = ThisAdjustment;
Eli Friedman31bc3ad2009-12-07 23:56:34 +00001372 SavedAdjustments.push_back(
1373 std::make_pair(GD, std::make_pair(OGD, ThisAdjustment)));
Eli Friedmanf2eda5e2009-12-06 22:33:51 +00001374 }
Eli Friedman3d2e9de2009-12-04 08:34:14 +00001375 }
1376 return true;
Anders Carlssonca1bf682009-12-03 01:54:02 +00001377 }
1378
1379 return false;
Anders Carlssond59885022009-11-27 22:21:51 +00001380}
1381
Anders Carlsson5d40c6f2010-02-11 08:02:13 +00001382void OldVtableBuilder::AppendMethodsToVtable() {
Eli Friedman6c08ce72009-12-05 01:05:03 +00001383 if (!BuildVtable) {
Anders Carlssonfe5f7d92009-12-06 01:09:21 +00001384 VtableComponents.insert(VtableComponents.end(), Methods.size(),
1385 (llvm::Constant *)0);
Eli Friedman6c08ce72009-12-05 01:05:03 +00001386 ThisAdjustments.clear();
1387 BaseReturnTypes.clear();
1388 Methods.clear();
1389 return;
1390 }
1391
Anders Carlsson472404f2009-12-04 16:19:30 +00001392 // Reserve room in the vtable for our new methods.
Anders Carlssonfe5f7d92009-12-06 01:09:21 +00001393 VtableComponents.reserve(VtableComponents.size() + Methods.size());
Anders Carlssonb07567c2009-12-04 03:07:26 +00001394
Anders Carlsson86809cd2009-12-04 02:43:50 +00001395 for (unsigned i = 0, e = Methods.size(); i != e; ++i) {
1396 GlobalDecl GD = Methods[i];
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +00001397 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1398
1399 // Get the 'this' pointer adjustment.
Anders Carlsson86809cd2009-12-04 02:43:50 +00001400 ThunkAdjustment ThisAdjustment = ThisAdjustments.lookup(i);
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +00001401
1402 // Construct the return type adjustment.
1403 ThunkAdjustment ReturnAdjustment;
1404
1405 QualType BaseReturnType = BaseReturnTypes.lookup(i);
1406 if (!BaseReturnType.isNull() && !MD->isPure()) {
1407 QualType DerivedType =
1408 MD->getType()->getAs<FunctionType>()->getResultType();
1409
1410 int64_t NonVirtualAdjustment =
1411 getNVOffset(BaseReturnType, DerivedType) / 8;
1412
1413 int64_t VirtualAdjustment =
1414 getVbaseOffset(BaseReturnType, DerivedType);
1415
1416 ReturnAdjustment = ThunkAdjustment(NonVirtualAdjustment,
1417 VirtualAdjustment);
1418 }
1419
Anders Carlsson50f14742009-12-04 03:06:03 +00001420 llvm::Constant *Method = 0;
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +00001421 if (!ReturnAdjustment.isEmpty()) {
1422 // Build a covariant thunk.
1423 CovariantThunkAdjustment Adjustment(ThisAdjustment, ReturnAdjustment);
Eli Friedman8174f2c2009-12-06 22:01:30 +00001424 Method = wrap(CGM.GetAddrOfCovariantThunk(GD, Adjustment));
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +00001425 } else if (!ThisAdjustment.isEmpty()) {
1426 // Build a "regular" thunk.
Eli Friedman8174f2c2009-12-06 22:01:30 +00001427 Method = wrap(CGM.GetAddrOfThunk(GD, ThisAdjustment));
Anders Carlssonddf42c82009-12-04 02:56:03 +00001428 } else if (MD->isPure()) {
1429 // We have a pure virtual method.
Anders Carlsson50f14742009-12-04 03:06:03 +00001430 Method = getPureVirtualFn();
1431 } else {
1432 // We have a good old regular method.
1433 Method = WrapAddrOf(GD);
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +00001434 }
Anders Carlsson50f14742009-12-04 03:06:03 +00001435
1436 // Add the method to the vtable.
Anders Carlssonfe5f7d92009-12-06 01:09:21 +00001437 VtableComponents.push_back(Method);
Anders Carlsson86809cd2009-12-04 02:43:50 +00001438 }
1439
Anders Carlsson50f14742009-12-04 03:06:03 +00001440
Anders Carlsson86809cd2009-12-04 02:43:50 +00001441 ThisAdjustments.clear();
Anders Carlsson5b3ea9b2009-12-04 02:52:22 +00001442 BaseReturnTypes.clear();
Anders Carlsson86809cd2009-12-04 02:43:50 +00001443
Anders Carlsson495634e2009-12-04 02:39:04 +00001444 Methods.clear();
Anders Carlsson495634e2009-12-04 02:39:04 +00001445}
1446
Anders Carlssonf942ee02009-11-27 20:47:55 +00001447void CGVtableInfo::ComputeMethodVtableIndices(const CXXRecordDecl *RD) {
1448
1449 // Itanium C++ ABI 2.5.2:
Anders Carlsson259688c2010-02-02 03:37:46 +00001450 // The order of the virtual function pointers in a virtual table is the
1451 // order of declaration of the corresponding member functions in the class.
Anders Carlssonf942ee02009-11-27 20:47:55 +00001452 //
Anders Carlsson259688c2010-02-02 03:37:46 +00001453 // There is an entry for any virtual function declared in a class,
1454 // whether it is a new function or overrides a base class function,
1455 // unless it overrides a function from the primary base, and conversion
1456 // between their return types does not require an adjustment.
Anders Carlssonf942ee02009-11-27 20:47:55 +00001457
1458 int64_t CurrentIndex = 0;
1459
1460 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1461 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1462
1463 if (PrimaryBase) {
Anders Carlssonc920fa22009-11-30 19:43:26 +00001464 assert(PrimaryBase->isDefinition() &&
1465 "Should have the definition decl of the primary base!");
Anders Carlssonf942ee02009-11-27 20:47:55 +00001466
1467 // Since the record decl shares its vtable pointer with the primary base
1468 // we need to start counting at the end of the primary base's vtable.
1469 CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase);
1470 }
Eli Friedman21517252009-12-15 03:31:17 +00001471
1472 // Collect all the primary bases, so we can check whether methods override
1473 // a method from the base.
1474 llvm::SmallPtrSet<const CXXRecordDecl *, 5> PrimaryBases;
1475 for (ASTRecordLayout::primary_base_info_iterator
1476 I = Layout.primary_base_begin(), E = Layout.primary_base_end();
1477 I != E; ++I)
1478 PrimaryBases.insert((*I).getBase());
1479
Anders Carlssonf942ee02009-11-27 20:47:55 +00001480 const CXXDestructorDecl *ImplicitVirtualDtor = 0;
1481
1482 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
1483 e = RD->method_end(); i != e; ++i) {
1484 const CXXMethodDecl *MD = *i;
1485
1486 // We only want virtual methods.
1487 if (!MD->isVirtual())
1488 continue;
1489
1490 bool ShouldAddEntryForMethod = true;
1491
1492 // Check if this method overrides a method in the primary base.
1493 for (CXXMethodDecl::method_iterator i = MD->begin_overridden_methods(),
1494 e = MD->end_overridden_methods(); i != e; ++i) {
Anders Carlssonf3935b42009-12-04 05:51:56 +00001495 const CXXMethodDecl *OverriddenMD = *i;
Anders Carlssonf942ee02009-11-27 20:47:55 +00001496 const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent();
1497 assert(OverriddenMD->isCanonicalDecl() &&
1498 "Should have the canonical decl of the overridden RD!");
1499
Eli Friedman21517252009-12-15 03:31:17 +00001500 if (PrimaryBases.count(OverriddenRD)) {
Anders Carlssonf942ee02009-11-27 20:47:55 +00001501 // Check if converting from the return type of the method to the
1502 // return type of the overridden method requires conversion.
1503 QualType ReturnType =
1504 MD->getType()->getAs<FunctionType>()->getResultType();
1505 QualType OverriddenReturnType =
1506 OverriddenMD->getType()->getAs<FunctionType>()->getResultType();
1507
1508 if (!TypeConversionRequiresAdjustment(CGM.getContext(),
1509 ReturnType, OverriddenReturnType)) {
1510 // This index is shared between the index in the vtable of the primary
1511 // base class.
1512 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1513 const CXXDestructorDecl *OverriddenDD =
1514 cast<CXXDestructorDecl>(OverriddenMD);
1515
1516 // Add both the complete and deleting entries.
1517 MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] =
1518 getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Complete));
1519 MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] =
1520 getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting));
1521 } else {
1522 MethodVtableIndices[MD] = getMethodVtableIndex(OverriddenMD);
1523 }
1524
1525 // We don't need to add an entry for this method.
1526 ShouldAddEntryForMethod = false;
1527 break;
1528 }
1529 }
1530 }
1531
1532 if (!ShouldAddEntryForMethod)
1533 continue;
1534
1535 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1536 if (MD->isImplicit()) {
1537 assert(!ImplicitVirtualDtor &&
1538 "Did already see an implicit virtual dtor!");
1539 ImplicitVirtualDtor = DD;
1540 continue;
1541 }
1542
1543 // Add the complete dtor.
1544 MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++;
1545
1546 // Add the deleting dtor.
1547 MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++;
1548 } else {
1549 // Add the entry.
1550 MethodVtableIndices[MD] = CurrentIndex++;
1551 }
1552 }
1553
1554 if (ImplicitVirtualDtor) {
1555 // Itanium C++ ABI 2.5.2:
1556 // If a class has an implicitly-defined virtual destructor,
1557 // its entries come after the declared virtual function pointers.
1558
1559 // Add the complete dtor.
1560 MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] =
1561 CurrentIndex++;
1562
1563 // Add the deleting dtor.
1564 MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] =
1565 CurrentIndex++;
1566 }
1567
1568 NumVirtualFunctionPointers[RD] = CurrentIndex;
1569}
1570
1571uint64_t CGVtableInfo::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) {
1572 llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
1573 NumVirtualFunctionPointers.find(RD);
1574 if (I != NumVirtualFunctionPointers.end())
1575 return I->second;
1576
1577 ComputeMethodVtableIndices(RD);
1578
1579 I = NumVirtualFunctionPointers.find(RD);
1580 assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!");
1581 return I->second;
1582}
1583
1584uint64_t CGVtableInfo::getMethodVtableIndex(GlobalDecl GD) {
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001585 MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001586 if (I != MethodVtableIndices.end())
1587 return I->second;
1588
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001589 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
Anders Carlssonf942ee02009-11-27 20:47:55 +00001590
1591 ComputeMethodVtableIndices(RD);
1592
Anders Carlssonfb4dda42009-11-13 17:08:56 +00001593 I = MethodVtableIndices.find(GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001594 assert(I != MethodVtableIndices.end() && "Did not find index!");
1595 return I->second;
1596}
1597
Eli Friedman31bc3ad2009-12-07 23:56:34 +00001598CGVtableInfo::AdjustmentVectorTy*
1599CGVtableInfo::getAdjustments(GlobalDecl GD) {
1600 SavedAdjustmentsTy::iterator I = SavedAdjustments.find(GD);
1601 if (I != SavedAdjustments.end())
1602 return &I->second;
Eli Friedman8174f2c2009-12-06 22:01:30 +00001603
1604 const CXXRecordDecl *RD = cast<CXXRecordDecl>(GD.getDecl()->getDeclContext());
Eli Friedman31bc3ad2009-12-07 23:56:34 +00001605 if (!SavedAdjustmentRecords.insert(RD).second)
1606 return 0;
Eli Friedman8174f2c2009-12-06 22:01:30 +00001607
Anders Carlsson5f9a8812010-01-14 02:29:07 +00001608 AddressPointsMapTy AddressPoints;
Anders Carlsson5d40c6f2010-02-11 08:02:13 +00001609 OldVtableBuilder b(RD, RD, 0, CGM, false, AddressPoints);
Eli Friedman8174f2c2009-12-06 22:01:30 +00001610 D1(printf("vtable %s\n", RD->getNameAsCString()));
1611 b.GenerateVtableForBase(RD);
1612 b.GenerateVtableForVBases(RD);
Eli Friedman8174f2c2009-12-06 22:01:30 +00001613
Anders Carlsson5d40c6f2010-02-11 08:02:13 +00001614 for (OldVtableBuilder::SavedAdjustmentsVectorTy::iterator
Eli Friedman31bc3ad2009-12-07 23:56:34 +00001615 i = b.getSavedAdjustments().begin(),
1616 e = b.getSavedAdjustments().end(); i != e; i++)
1617 SavedAdjustments[i->first].push_back(i->second);
Eli Friedman8174f2c2009-12-06 22:01:30 +00001618
Eli Friedman31bc3ad2009-12-07 23:56:34 +00001619 I = SavedAdjustments.find(GD);
1620 if (I != SavedAdjustments.end())
1621 return &I->second;
1622
1623 return 0;
Eli Friedman8174f2c2009-12-06 22:01:30 +00001624}
1625
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001626int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
1627 const CXXRecordDecl *VBase) {
1628 ClassPairTy ClassPair(RD, VBase);
1629
1630 VirtualBaseClassIndiciesTy::iterator I =
1631 VirtualBaseClassIndicies.find(ClassPair);
1632 if (I != VirtualBaseClassIndicies.end())
1633 return I->second;
1634
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001635 // FIXME: This seems expensive. Can we do a partial job to get
1636 // just this data.
Anders Carlsson5f9a8812010-01-14 02:29:07 +00001637 AddressPointsMapTy AddressPoints;
Anders Carlsson5d40c6f2010-02-11 08:02:13 +00001638 OldVtableBuilder b(RD, RD, 0, CGM, false, AddressPoints);
Mike Stump75ce5732009-10-31 20:06:59 +00001639 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001640 b.GenerateVtableForBase(RD);
1641 b.GenerateVtableForVBases(RD);
1642
1643 for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
1644 b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) {
1645 // Insert all types.
1646 ClassPairTy ClassPair(RD, I->first);
1647
1648 VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second));
1649 }
1650
1651 I = VirtualBaseClassIndicies.find(ClassPair);
1652 assert(I != VirtualBaseClassIndicies.end() && "Did not find index!");
1653
1654 return I->second;
1655}
1656
Anders Carlssonc8e39ec2009-12-05 21:03:56 +00001657uint64_t CGVtableInfo::getVtableAddressPoint(const CXXRecordDecl *RD) {
1658 uint64_t AddressPoint =
Anders Carlsson93a18842010-01-02 18:02:32 +00001659 (*(*(CGM.getVtableInfo().AddressPoints[RD]))[RD])[std::make_pair(RD, 0)];
Anders Carlssonc8e39ec2009-12-05 21:03:56 +00001660
1661 return AddressPoint;
1662}
1663
Anders Carlsson7e28c5f2009-12-06 00:01:05 +00001664llvm::GlobalVariable *
Anders Carlsson0911ae82009-12-06 00:23:49 +00001665CGVtableInfo::GenerateVtable(llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlsson232324c2009-12-06 00:53:22 +00001666 bool GenerateDefinition,
Anders Carlsson0911ae82009-12-06 00:23:49 +00001667 const CXXRecordDecl *LayoutClass,
Anders Carlsson5f9a8812010-01-14 02:29:07 +00001668 const CXXRecordDecl *RD, uint64_t Offset,
1669 AddressPointsMapTy& AddressPoints) {
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001670 llvm::SmallString<256> OutName;
Mike Stump2cefe382009-11-12 20:47:57 +00001671 if (LayoutClass != RD)
Anders Carlsson7e28c5f2009-12-06 00:01:05 +00001672 CGM.getMangleContext().mangleCXXCtorVtable(LayoutClass, Offset / 8,
1673 RD, OutName);
Mike Stumpeac45592009-11-11 20:26:26 +00001674 else
Anders Carlsson7e28c5f2009-12-06 00:01:05 +00001675 CGM.getMangleContext().mangleCXXVtable(RD, OutName);
Daniel Dunbare128dd12009-11-21 09:06:22 +00001676 llvm::StringRef Name = OutName.str();
Benjamin Kramerbb0a07b2009-10-11 22:57:54 +00001677
Anders Carlsson7e28c5f2009-12-06 00:01:05 +00001678 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
Anders Carlsson93a18842010-01-02 18:02:32 +00001679 if (GV == 0 || CGM.getVtableInfo().AddressPoints[LayoutClass] == 0 ||
1680 GV->isDeclaration()) {
Anders Carlsson5d40c6f2010-02-11 08:02:13 +00001681 OldVtableBuilder b(RD, LayoutClass, Offset, CGM, GenerateDefinition,
1682 AddressPoints);
Eli Friedman6c08ce72009-12-05 01:05:03 +00001683
1684 D1(printf("vtable %s\n", RD->getNameAsCString()));
1685 // First comes the vtables for all the non-virtual bases...
Mike Stumpd538a6d2009-12-24 07:29:41 +00001686 b.GenerateVtableForBase(RD, Offset);
Eli Friedman6c08ce72009-12-05 01:05:03 +00001687
1688 // then the vtables for all the virtual bases.
1689 b.GenerateVtableForVBases(RD, Offset);
1690
Anders Carlsson58b271d2009-12-05 22:19:10 +00001691 llvm::Constant *Init = 0;
Anders Carlsson7e28c5f2009-12-06 00:01:05 +00001692 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
Anders Carlsson58b271d2009-12-05 22:19:10 +00001693 llvm::ArrayType *ArrayType =
Anders Carlssonfe5f7d92009-12-06 01:09:21 +00001694 llvm::ArrayType::get(Int8PtrTy, b.getVtableComponents().size());
Anders Carlssona95d4c52009-12-05 21:09:05 +00001695
Anders Carlsson232324c2009-12-06 00:53:22 +00001696 if (GenerateDefinition)
Anders Carlssonfe5f7d92009-12-06 01:09:21 +00001697 Init = llvm::ConstantArray::get(ArrayType, &b.getVtableComponents()[0],
1698 b.getVtableComponents().size());
Anders Carlsson232324c2009-12-06 00:53:22 +00001699
Mike Stumpaa51ad62009-11-19 04:04:36 +00001700 llvm::GlobalVariable *OGV = GV;
Anders Carlsson232324c2009-12-06 00:53:22 +00001701
1702 GV = new llvm::GlobalVariable(CGM.getModule(), ArrayType,
1703 /*isConstant=*/true, Linkage, Init, Name);
Anders Carlssonfe5f7d92009-12-06 01:09:21 +00001704 CGM.setGlobalVisibility(GV, RD);
1705
Mike Stumpaa51ad62009-11-19 04:04:36 +00001706 if (OGV) {
1707 GV->takeName(OGV);
Anders Carlsson58b271d2009-12-05 22:19:10 +00001708 llvm::Constant *NewPtr =
1709 llvm::ConstantExpr::getBitCast(GV, OGV->getType());
Mike Stumpaa51ad62009-11-19 04:04:36 +00001710 OGV->replaceAllUsesWith(NewPtr);
1711 OGV->eraseFromParent();
1712 }
Mike Stumpaa51ad62009-11-19 04:04:36 +00001713 }
Anders Carlssonb3f54b72009-12-05 21:28:12 +00001714
Anders Carlsson5d40c6f2010-02-11 08:02:13 +00001715 if (GenerateDefinition && CGM.getLangOptions().DumpVtableLayouts) {
1716 VtableBuilder Builder(RD);
1717
1718 Builder.dumpLayout(llvm::errs());
1719 }
1720
Anders Carlssonb3f54b72009-12-05 21:28:12 +00001721 return GV;
Anders Carlsson2bb27f52009-10-11 22:13:54 +00001722}
Mike Stump9f23a142009-11-10 02:30:51 +00001723
Anders Carlsson232324c2009-12-06 00:53:22 +00001724void CGVtableInfo::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
1725 const CXXRecordDecl *RD) {
Anders Carlssone1b3e622009-12-07 07:59:52 +00001726 llvm::GlobalVariable *&Vtable = Vtables[RD];
1727 if (Vtable) {
1728 assert(Vtable->getInitializer() && "Vtable doesn't have a definition!");
1729 return;
1730 }
1731
Anders Carlsson5f9a8812010-01-14 02:29:07 +00001732 AddressPointsMapTy AddressPoints;
1733 Vtable = GenerateVtable(Linkage, /*GenerateDefinition=*/true, RD, RD, 0,
1734 AddressPoints);
Anders Carlssone36a6b32010-01-02 01:01:18 +00001735 GenerateVTT(Linkage, /*GenerateDefinition=*/true, RD);
Mike Stump1a139f82009-11-19 01:08:19 +00001736}
1737
Anders Carlsson7e28c5f2009-12-06 00:01:05 +00001738llvm::GlobalVariable *CGVtableInfo::getVtable(const CXXRecordDecl *RD) {
Anders Carlsson232324c2009-12-06 00:53:22 +00001739 llvm::GlobalVariable *Vtable = Vtables.lookup(RD);
Anders Carlsson7e28c5f2009-12-06 00:01:05 +00001740
Anders Carlsson5f9a8812010-01-14 02:29:07 +00001741 if (!Vtable) {
1742 AddressPointsMapTy AddressPoints;
Anders Carlsson232324c2009-12-06 00:53:22 +00001743 Vtable = GenerateVtable(llvm::GlobalValue::ExternalLinkage,
Anders Carlsson5f9a8812010-01-14 02:29:07 +00001744 /*GenerateDefinition=*/false, RD, RD, 0,
1745 AddressPoints);
1746 }
Mike Stumpaa51ad62009-11-19 04:04:36 +00001747
Anders Carlsson4ed44eb2009-12-05 22:42:54 +00001748 return Vtable;
Mike Stumpd846d082009-11-10 07:44:33 +00001749}
Mike Stumpeac45592009-11-11 20:26:26 +00001750
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001751void CGVtableInfo::MaybeEmitVtable(GlobalDecl GD) {
1752 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1753 const CXXRecordDecl *RD = MD->getParent();
1754
Anders Carlsson4ed44eb2009-12-05 22:42:54 +00001755 // If the class doesn't have a vtable we don't need to emit one.
1756 if (!RD->isDynamicClass())
1757 return;
1758
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001759 // Get the key function.
Anders Carlsson5ebf8b42009-12-07 04:35:11 +00001760 const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD);
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001761
Anders Carlsson4ed44eb2009-12-05 22:42:54 +00001762 if (KeyFunction) {
1763 // We don't have the right key function.
1764 if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl())
1765 return;
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001766 }
1767
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001768 // Emit the data.
Douglas Gregorccecc1b2010-01-06 20:27:16 +00001769 GenerateClassData(CGM.getVtableLinkage(RD), RD);
Eli Friedman8174f2c2009-12-06 22:01:30 +00001770
1771 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
1772 e = RD->method_end(); i != e; ++i) {
Eli Friedman31bc3ad2009-12-07 23:56:34 +00001773 if ((*i)->isVirtual() && ((*i)->hasInlineBody() || (*i)->isImplicit())) {
Eli Friedman8174f2c2009-12-06 22:01:30 +00001774 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(*i)) {
1775 CGM.BuildThunksForVirtual(GlobalDecl(DD, Dtor_Complete));
1776 CGM.BuildThunksForVirtual(GlobalDecl(DD, Dtor_Deleting));
1777 } else {
1778 CGM.BuildThunksForVirtual(GlobalDecl(*i));
1779 }
1780 }
1781 }
Anders Carlssonb1d3f7c2009-11-30 23:41:22 +00001782}
1783