blob: f16f66af2af634c69e192bf295051e6060ee46fb [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"
Chandler Carruthe087f072010-02-13 10:38:52 +000019#include "llvm/Support/Compiler.h"
Anders Carlsson824d7ea2010-02-11 08:02:13 +000020#include "llvm/Support/Format.h"
Zhongxing Xu7fe26ac2009-11-13 05:46:16 +000021#include <cstdio>
Anders Carlssondbd920c2009-10-11 22:13:54 +000022
23using namespace clang;
24using namespace CodeGen;
25
Anders Carlsson9a140602010-02-11 19:39:49 +000026/// TypeConversionRequiresAdjustment - Returns whether conversion from a
27/// derived type to a base type requires adjustment.
28static bool
29TypeConversionRequiresAdjustment(ASTContext &Ctx,
30 const CXXRecordDecl *DerivedDecl,
31 const CXXRecordDecl *BaseDecl) {
32 CXXBasePaths Paths(/*FindAmbiguities=*/false,
33 /*RecordPaths=*/true, /*DetectVirtual=*/true);
34 if (!const_cast<CXXRecordDecl *>(DerivedDecl)->
35 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseDecl), Paths)) {
36 assert(false && "Class must be derived from the passed in base class!");
37 return false;
38 }
39
40 // If we found a virtual base we always want to require adjustment.
41 if (Paths.getDetectedVirtual())
42 return true;
43
44 const CXXBasePath &Path = Paths.front();
45
46 for (size_t Start = 0, End = Path.size(); Start != End; ++Start) {
47 const CXXBasePathElement &Element = Path[Start];
48
49 // Check the base class offset.
50 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Element.Class);
51
52 const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>();
53 const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl());
54
55 if (Layout.getBaseClassOffset(Base) != 0) {
56 // This requires an adjustment.
57 return true;
58 }
59 }
60
61 return false;
62}
63
64static bool
65TypeConversionRequiresAdjustment(ASTContext &Ctx,
66 QualType DerivedType, QualType BaseType) {
67 // Canonicalize the types.
John McCall96058952010-02-12 06:15:07 +000068 CanQualType CanDerivedType = Ctx.getCanonicalType(DerivedType);
69 CanQualType CanBaseType = Ctx.getCanonicalType(BaseType);
Anders Carlsson9a140602010-02-11 19:39:49 +000070
71 assert(CanDerivedType->getTypeClass() == CanBaseType->getTypeClass() &&
72 "Types must have same type class!");
73
74 if (CanDerivedType == CanBaseType) {
75 // No adjustment needed.
76 return false;
77 }
78
John McCall96058952010-02-12 06:15:07 +000079 if (isa<ReferenceType>(CanDerivedType)) {
80 CanDerivedType = CanDerivedType->getAs<ReferenceType>()->getPointeeType();
Anders Carlsson9abb2732010-02-11 19:45:15 +000081 CanBaseType = CanBaseType->getAs<ReferenceType>()->getPointeeType();
John McCall96058952010-02-12 06:15:07 +000082 } else if (isa<PointerType>(CanDerivedType)) {
83 CanDerivedType = CanDerivedType->getAs<PointerType>()->getPointeeType();
Anders Carlsson9abb2732010-02-11 19:45:15 +000084 CanBaseType = CanBaseType->getAs<PointerType>()->getPointeeType();
Anders Carlsson9a140602010-02-11 19:39:49 +000085 } else {
86 assert(false && "Unexpected return type!");
87 }
88
John McCall96058952010-02-12 06:15:07 +000089 // We need to compare unqualified types here; consider
90 // const T *Base::foo();
91 // T *Derived::foo();
92 if (CanDerivedType.getUnqualifiedType() == CanBaseType.getUnqualifiedType()) {
Anders Carlsson9a140602010-02-11 19:39:49 +000093 // No adjustment needed.
94 return false;
95 }
96
97 const CXXRecordDecl *DerivedDecl =
John McCall96058952010-02-12 06:15:07 +000098 cast<CXXRecordDecl>(cast<RecordType>(CanDerivedType)->getDecl());
Anders Carlsson9a140602010-02-11 19:39:49 +000099
100 const CXXRecordDecl *BaseDecl =
John McCall96058952010-02-12 06:15:07 +0000101 cast<CXXRecordDecl>(cast<RecordType>(CanBaseType)->getDecl());
Anders Carlsson9a140602010-02-11 19:39:49 +0000102
103 return TypeConversionRequiresAdjustment(Ctx, DerivedDecl, BaseDecl);
104}
105
Anders Carlsson57071e22010-02-12 05:25:12 +0000106static bool
Anders Carlsson104f45c2010-02-12 17:37:14 +0000107ReturnTypeConversionRequiresAdjustment(const CXXMethodDecl *DerivedMD,
Anders Carlsson57071e22010-02-12 05:25:12 +0000108 const CXXMethodDecl *BaseMD) {
Anders Carlsson104f45c2010-02-12 17:37:14 +0000109 ASTContext &Context = DerivedMD->getASTContext();
110
Anders Carlsson57071e22010-02-12 05:25:12 +0000111 const FunctionType *BaseFT = BaseMD->getType()->getAs<FunctionType>();
112 const FunctionType *DerivedFT = DerivedMD->getType()->getAs<FunctionType>();
113
114 return TypeConversionRequiresAdjustment(Context, DerivedFT->getResultType(),
115 BaseFT->getResultType());
116}
117
Anders Carlsson27f69d02009-11-27 22:21:51 +0000118namespace {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000119
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000120/// FinalOverriders - Contains the final overrider member functions for all
121/// member functions in the base subobjects of a class.
122class FinalOverriders {
Anders Carlssona7575822010-02-12 16:55:34 +0000123public:
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000124 /// BaseOffset - Represents an offset from a derived class to a direct or
125 /// indirect base class.
126 struct BaseOffset {
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000127 /// DerivedClass - The derived class.
128 const CXXRecordDecl *DerivedClass;
129
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000130 /// VirtualBase - If the path from the derived class to the base class
131 /// involves a virtual base class, this holds its declaration.
132 const CXXRecordDecl *VirtualBase;
133
134 /// NonVirtualOffset - The offset from the derived class to the base class.
135 /// Or the offset from the virtual base class to the base class, if the path
136 /// from the derived class to the base class involves a virtual base class.
137 uint64_t NonVirtualOffset;
138
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000139 BaseOffset() : DerivedClass(0), VirtualBase(0), NonVirtualOffset(0) { }
140 BaseOffset(const CXXRecordDecl *DerivedClass,
141 const CXXRecordDecl *VirtualBase, uint64_t NonVirtualOffset)
142 : DerivedClass(DerivedClass), VirtualBase(VirtualBase),
143 NonVirtualOffset(NonVirtualOffset) { }
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000144
Benjamin Kramer03d15f02010-02-13 09:15:07 +0000145 bool isEmpty() const { return !NonVirtualOffset && !VirtualBase; }
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000146 };
147
Anders Carlssona7575822010-02-12 16:55:34 +0000148 /// OverriderInfo - Information about a final overrider.
149 struct OverriderInfo {
150 /// Method - The method decl of the overrider.
151 const CXXMethodDecl *Method;
152
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000153 OverriderInfo() : Method(0) { }
Anders Carlssona7575822010-02-12 16:55:34 +0000154 };
155
156private:
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000157 /// MostDerivedClass - The most derived class for which the final overriders
158 /// are stored.
159 const CXXRecordDecl *MostDerivedClass;
160
161 ASTContext &Context;
162
163 /// MostDerivedClassLayout - the AST record layout of the most derived class.
164 const ASTRecordLayout &MostDerivedClassLayout;
165
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000166 /// BaseSubobjectMethodPairTy - Uniquely identifies a member function
167 /// in a base subobject.
168 typedef std::pair<BaseSubobject, const CXXMethodDecl *>
169 BaseSubobjectMethodPairTy;
170
171 typedef llvm::DenseMap<BaseSubobjectMethodPairTy,
Anders Carlssona7575822010-02-12 16:55:34 +0000172 OverriderInfo> OverridersMapTy;
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000173
174 /// OverridersMap - The final overriders for all virtual member functions of
175 /// all the base subobjects of the most derived class.
176 OverridersMapTy OverridersMap;
177
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000178 typedef llvm::DenseMap<BaseSubobjectMethodPairTy, BaseOffset>
179 AdjustmentOffsetsMapTy;
180
181 /// ReturnAdjustments - Holds return adjustments for all the overriders that
182 /// need to perform return value adjustments.
183 AdjustmentOffsetsMapTy ReturnAdjustments;
184
Anders Carlssonb8358d82010-02-12 01:40:03 +0000185 typedef llvm::SmallVector<uint64_t, 1> OffsetVectorTy;
186
187 /// SubobjectOffsetsMapTy - This map is used for keeping track of all the
188 /// base subobject offsets that a single class declaration might refer to.
189 ///
190 /// For example, in:
191 ///
192 /// struct A { virtual void f(); };
193 /// struct B1 : A { };
194 /// struct B2 : A { };
195 /// struct C : B1, B2 { virtual void f(); };
196 ///
197 /// when we determine that C::f() overrides A::f(), we need to update the
198 /// overriders map for both A-in-B1 and A-in-B2 and the subobject offsets map
199 /// will have the subobject offsets for both A copies.
200 typedef llvm::DenseMap<const CXXRecordDecl *, OffsetVectorTy>
201 SubobjectOffsetsMapTy;
202
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000203 /// ComputeFinalOverriders - Compute the final overriders for a given base
204 /// subobject (and all its direct and indirect bases).
Anders Carlssonb8358d82010-02-12 01:40:03 +0000205 void ComputeFinalOverriders(BaseSubobject Base,
206 SubobjectOffsetsMapTy &Offsets);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000207
208 /// AddOverriders - Add the final overriders for this base subobject to the
209 /// map of final overriders.
Anders Carlssonb8358d82010-02-12 01:40:03 +0000210 void AddOverriders(BaseSubobject Base, SubobjectOffsetsMapTy &Offsets);
211
212 /// PropagateOverrider - Propagate the NewMD overrider to all the functions
213 /// that OldMD overrides. For example, if we have:
214 ///
215 /// struct A { virtual void f(); };
216 /// struct B : A { virtual void f(); };
217 /// struct C : B { virtual void f(); };
218 ///
219 /// and we want to override B::f with C::f, we also need to override A::f with
220 /// C::f.
221 void PropagateOverrider(const CXXMethodDecl *OldMD,
222 const CXXMethodDecl *NewMD,
223 SubobjectOffsetsMapTy &Offsets);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000224
Anders Carlssonb8358d82010-02-12 01:40:03 +0000225 static void MergeSubobjectOffsets(const SubobjectOffsetsMapTy &NewOffsets,
226 SubobjectOffsetsMapTy &Offsets);
227
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000228public:
229 explicit FinalOverriders(const CXXRecordDecl *MostDerivedClass);
230
231 /// getOverrider - Get the final overrider for the given method declaration in
232 /// the given base subobject.
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000233 OverriderInfo getOverrider(BaseSubobject Base,
234 const CXXMethodDecl *MD) const {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000235 assert(OverridersMap.count(std::make_pair(Base, MD)) &&
236 "Did not find overrider!");
237
238 return OverridersMap.lookup(std::make_pair(Base, MD));
239 }
240
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000241 BaseOffset getReturnAdjustmentOffset(BaseSubobject Base,
242 const CXXMethodDecl *MD) const {
243 return ReturnAdjustments.lookup(std::make_pair(Base, MD));
244 }
245
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000246 /// dump - dump the final overriders.
247 void dump() const {
248 dump(llvm::errs(), BaseSubobject(MostDerivedClass, 0));
249 }
250
251 /// dump - dump the final overriders for a base subobject, and all its direct
252 /// and indirect base subobjects.
253 void dump(llvm::raw_ostream &Out, BaseSubobject Base) const;
254};
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000255
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000256FinalOverriders::FinalOverriders(const CXXRecordDecl *MostDerivedClass)
257 : MostDerivedClass(MostDerivedClass),
258 Context(MostDerivedClass->getASTContext()),
259 MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)) {
260
261 // Compute the final overriders.
Anders Carlssonb8358d82010-02-12 01:40:03 +0000262 SubobjectOffsetsMapTy Offsets;
263 ComputeFinalOverriders(BaseSubobject(MostDerivedClass, 0), Offsets);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000264
265 // And dump them (for now).
266 dump();
267}
268
Anders Carlssonb8358d82010-02-12 01:40:03 +0000269void FinalOverriders::AddOverriders(BaseSubobject Base,
270 SubobjectOffsetsMapTy &Offsets) {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000271 const CXXRecordDecl *RD = Base.getBase();
272
273 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
274 E = RD->method_end(); I != E; ++I) {
275 const CXXMethodDecl *MD = *I;
276
277 if (!MD->isVirtual())
278 continue;
279
Anders Carlssonb8358d82010-02-12 01:40:03 +0000280 // First, propagate the overrider.
281 PropagateOverrider(MD, MD, Offsets);
282
283 // Add the overrider as the final overrider of itself.
Anders Carlssona7575822010-02-12 16:55:34 +0000284 OverriderInfo& Overrider = OverridersMap[std::make_pair(Base, MD)];
285 assert(!Overrider.Method && "Overrider should not exist yet!");
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000286
Anders Carlssona7575822010-02-12 16:55:34 +0000287 Overrider.Method = MD;
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000288 }
289}
290
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000291static FinalOverriders::BaseOffset
292ComputeBaseOffset(ASTContext &Context,
293 const CXXRecordDecl *DerivedRD,
294 const CXXRecordDecl *BaseRD) {
295 CXXBasePaths Paths(/*FindAmbiguities=*/false,
Anders Carlssona4a54172010-02-13 20:41:15 +0000296 /*RecordPaths=*/true, /*DetectVirtual=*/false);
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000297
298 if (!const_cast<CXXRecordDecl *>(DerivedRD)->
299 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) {
300 assert(false && "Class must be derived from the passed in base class!");
301 return FinalOverriders::BaseOffset();
302 }
303
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000304 uint64_t NonVirtualOffset = 0;
305
306 const CXXBasePath &Path = Paths.front();
307
Anders Carlssona4a54172010-02-13 20:41:15 +0000308 unsigned NonVirtualStart = 0;
309 const CXXRecordDecl *VirtualBase = 0;
310
311 // First, look for the virtual base class.
312 for (unsigned I = 0, E = Path.size(); I != E; ++I) {
313 const CXXBasePathElement &Element = Path[I];
314
315 if (Element.Base->isVirtual()) {
316 // FIXME: Can we break when we find the first virtual base?
317 // (If we can't, can't we just iterate over the path in reverse order?)
318 NonVirtualStart = I + 1;
319 QualType VBaseType = Element.Base->getType();
320 VirtualBase =
321 cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl());
322 }
323 }
324
325 // Now compute the non-virtual offset.
326 for (unsigned I = NonVirtualStart, E = Path.size(); I != E; ++I) {
327 const CXXBasePathElement &Element = Path[I];
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000328
329 // Check the base class offset.
330 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class);
331
332 const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>();
333 const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl());
334
335 NonVirtualOffset += Layout.getBaseClassOffset(Base);
336 }
337
338 // FIXME: This should probably use CharUnits or something. Maybe we should
339 // even change the base offsets in ASTRecordLayout to be specified in
340 // CharUnits.
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000341 return FinalOverriders::BaseOffset(DerivedRD, VirtualBase,
342 NonVirtualOffset / 8);
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000343}
344
345static FinalOverriders::BaseOffset
346ComputeReturnTypeBaseOffset(ASTContext &Context,
347 const CXXMethodDecl *DerivedMD,
348 const CXXMethodDecl *BaseMD) {
349 const FunctionType *BaseFT = BaseMD->getType()->getAs<FunctionType>();
350 const FunctionType *DerivedFT = DerivedMD->getType()->getAs<FunctionType>();
351
352 // Canonicalize the return types.
353 CanQualType CanDerivedReturnType =
354 Context.getCanonicalType(DerivedFT->getResultType());
355 CanQualType CanBaseReturnType =
356 Context.getCanonicalType(BaseFT->getResultType());
357
358 assert(CanDerivedReturnType->getTypeClass() ==
359 CanBaseReturnType->getTypeClass() &&
360 "Types must have same type class!");
361
362 if (CanDerivedReturnType == CanBaseReturnType) {
363 // No adjustment needed.
364 return FinalOverriders::BaseOffset();
365 }
366
367 if (isa<ReferenceType>(CanDerivedReturnType)) {
368 CanDerivedReturnType =
369 CanDerivedReturnType->getAs<ReferenceType>()->getPointeeType();
370 CanBaseReturnType =
371 CanBaseReturnType->getAs<ReferenceType>()->getPointeeType();
372 } else if (isa<PointerType>(CanDerivedReturnType)) {
373 CanDerivedReturnType =
374 CanDerivedReturnType->getAs<PointerType>()->getPointeeType();
375 CanBaseReturnType =
376 CanBaseReturnType->getAs<PointerType>()->getPointeeType();
377 } else {
378 assert(false && "Unexpected return type!");
379 }
380
381 // We need to compare unqualified types here; consider
382 // const T *Base::foo();
383 // T *Derived::foo();
384 if (CanDerivedReturnType.getUnqualifiedType() ==
385 CanBaseReturnType.getUnqualifiedType()) {
386 // No adjustment needed.
387 return FinalOverriders::BaseOffset();
388 }
389
390 const CXXRecordDecl *DerivedRD =
391 cast<CXXRecordDecl>(cast<RecordType>(CanDerivedReturnType)->getDecl());
392
393 const CXXRecordDecl *BaseRD =
394 cast<CXXRecordDecl>(cast<RecordType>(CanBaseReturnType)->getDecl());
395
396 return ComputeBaseOffset(Context, DerivedRD, BaseRD);
397}
398
Anders Carlssonb8358d82010-02-12 01:40:03 +0000399void FinalOverriders::PropagateOverrider(const CXXMethodDecl *OldMD,
400 const CXXMethodDecl *NewMD,
401 SubobjectOffsetsMapTy &Offsets) {
402 for (CXXMethodDecl::method_iterator I = OldMD->begin_overridden_methods(),
403 E = OldMD->end_overridden_methods(); I != E; ++I) {
404 const CXXMethodDecl *OverriddenMD = *I;
405 const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent();
406
407 // We want to override OverriddenMD in all subobjects, for example:
408 //
409 /// struct A { virtual void f(); };
410 /// struct B1 : A { };
411 /// struct B2 : A { };
412 /// struct C : B1, B2 { virtual void f(); };
413 ///
414 /// When overriding A::f with C::f we need to do so in both A subobjects.
415 const OffsetVectorTy &OffsetVector = Offsets[OverriddenRD];
416
417 // Go through all the subobjects.
418 for (unsigned I = 0, E = OffsetVector.size(); I != E; ++I) {
419 uint64_t Offset = OffsetVector[I];
420
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000421 BaseSubobjectMethodPairTy SubobjectAndMethod =
422 std::make_pair(BaseSubobject(OverriddenRD, Offset), OverriddenMD);
423
424 OverriderInfo &Overrider = OverridersMap[SubobjectAndMethod];
425
Anders Carlssona7575822010-02-12 16:55:34 +0000426 assert(Overrider.Method && "Did not find existing overrider!");
Anders Carlssonb8358d82010-02-12 01:40:03 +0000427
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000428 // Get the return adjustment base offset.
Anders Carlsson1d05be52010-02-13 21:16:54 +0000429 // (We don't want to do this for pure virtual member functions).
430 if (!NewMD->isPure()) {
431 BaseOffset ReturnBaseOffset =
432 ComputeReturnTypeBaseOffset(Context, NewMD, OverriddenMD);
433 if (!ReturnBaseOffset.isEmpty()) {
434 // Store the return adjustment base offset.
435 ReturnAdjustments[SubobjectAndMethod] = ReturnBaseOffset;
436 }
Anders Carlsson16b73122010-02-12 17:13:23 +0000437 }
438
Anders Carlssonb8358d82010-02-12 01:40:03 +0000439 // Set the new overrider.
Anders Carlssona7575822010-02-12 16:55:34 +0000440 Overrider.Method = NewMD;
Anders Carlssonb8358d82010-02-12 01:40:03 +0000441
442 // And propagate it further.
443 PropagateOverrider(OverriddenMD, NewMD, Offsets);
444 }
445 }
446}
447
448void
449FinalOverriders::MergeSubobjectOffsets(const SubobjectOffsetsMapTy &NewOffsets,
450 SubobjectOffsetsMapTy &Offsets) {
451 // Iterate over the new offsets.
452 for (SubobjectOffsetsMapTy::const_iterator I = NewOffsets.begin(),
453 E = NewOffsets.end(); I != E; ++I) {
454 const CXXRecordDecl *NewRD = I->first;
455 const OffsetVectorTy& NewOffsetsVector = I->second;
456
457 OffsetVectorTy &OffsetsVector = Offsets[NewRD];
458 if (OffsetsVector.empty()) {
459 // There were no previous offsets in this vector, just insert all entries
460 // from the new offsets vector.
461 OffsetsVector.append(NewOffsetsVector.begin(), NewOffsetsVector.end());
462 continue;
463 }
464
465 assert(false && "FIXME: Handle merging the subobject offsets!");
466 }
467}
468
469void FinalOverriders::ComputeFinalOverriders(BaseSubobject Base,
470 SubobjectOffsetsMapTy &Offsets) {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000471 const CXXRecordDecl *RD = Base.getBase();
Anders Carlssonb8358d82010-02-12 01:40:03 +0000472 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000473
Anders Carlssonb8358d82010-02-12 01:40:03 +0000474 SubobjectOffsetsMapTy NewOffsets;
475
476 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
477 E = RD->bases_end(); I != E; ++I) {
478 const CXXRecordDecl *BaseDecl =
479 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
480
481 assert(!I->isVirtual() && "FIXME: Handle virtual bases!");
482
483 uint64_t BaseOffset = Layout.getBaseClassOffset(BaseDecl) +
484 Base.getBaseOffset();
485
486 // Compute the final overriders for this base.
487 ComputeFinalOverriders(BaseSubobject(BaseDecl, BaseOffset), NewOffsets);
488 }
489
490 /// Now add the overriders for this particular subobject.
491 AddOverriders(Base, NewOffsets);
492
493 // And merge the newly discovered subobject offsets.
494 MergeSubobjectOffsets(NewOffsets, Offsets);
495
496 /// Finally, add the offset for our own subobject.
497 Offsets[RD].push_back(Base.getBaseOffset());
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000498}
499
500void FinalOverriders::dump(llvm::raw_ostream &Out, BaseSubobject Base) const {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000501 const CXXRecordDecl *RD = Base.getBase();
502 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
503
504 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
505 E = RD->bases_end(); I != E; ++I) {
506 assert(!I->isVirtual() && "FIXME: Handle virtual bases!");
507
508 const CXXRecordDecl *BaseDecl =
509 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
510
511 uint64_t BaseOffset = Layout.getBaseClassOffset(BaseDecl) +
512 Base.getBaseOffset();
513
514 dump(Out, BaseSubobject(BaseDecl, BaseOffset));
515 }
516
517 Out << "Final overriders for (" << RD->getQualifiedNameAsString() << ", ";
518 Out << Base.getBaseOffset() << ")\n";
519
520 // Now dump the overriders for this base subobject.
521 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
522 E = RD->method_end(); I != E; ++I) {
523 const CXXMethodDecl *MD = *I;
524
525 if (!MD->isVirtual())
526 continue;
527
Anders Carlssona7575822010-02-12 16:55:34 +0000528 OverriderInfo Overrider = getOverrider(Base, MD);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000529
530 Out << " " << MD->getQualifiedNameAsString() << " - ";
Anders Carlsson16b73122010-02-12 17:13:23 +0000531 Out << Overrider.Method->getQualifiedNameAsString();
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000532
Benjamin Kramere3d0b1c2010-02-13 09:11:28 +0000533 AdjustmentOffsetsMapTy::const_iterator AI =
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000534 ReturnAdjustments.find(std::make_pair(Base, MD));
Benjamin Kramere3d0b1c2010-02-13 09:11:28 +0000535 if (AI != ReturnAdjustments.end()) {
536 const BaseOffset &Offset = AI->second;
Anders Carlssona4a54172010-02-13 20:41:15 +0000537
538 Out << " [ret-adj: ";
539 if (Offset.VirtualBase)
540 Out << Offset.VirtualBase->getQualifiedNameAsString() << " vbase, ";
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000541
Anders Carlssona4a54172010-02-13 20:41:15 +0000542 Out << Offset.NonVirtualOffset << " nv]";
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000543 }
Anders Carlsson16b73122010-02-12 17:13:23 +0000544 Out << "\n";
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000545 }
546}
547
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000548/// VtableComponent - Represents a single component in a vtable.
549class VtableComponent {
550public:
551 enum Kind {
552 CK_VCallOffset,
553 CK_VBaseOffset,
554 CK_OffsetToTop,
555 CK_RTTI,
Anders Carlsson848fa642010-02-11 18:20:28 +0000556 CK_FunctionPointer,
557
558 /// CK_CompleteDtorPointer - A pointer to the complete destructor.
559 CK_CompleteDtorPointer,
560
561 /// CK_DeletingDtorPointer - A pointer to the deleting destructor.
562 CK_DeletingDtorPointer
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000563 };
564
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000565 static VtableComponent MakeOffsetToTop(int64_t Offset) {
566 return VtableComponent(CK_OffsetToTop, Offset);
567 }
568
569 static VtableComponent MakeRTTI(const CXXRecordDecl *RD) {
570 return VtableComponent(CK_RTTI, reinterpret_cast<uintptr_t>(RD));
571 }
572
573 static VtableComponent MakeFunction(const CXXMethodDecl *MD) {
574 assert(!isa<CXXDestructorDecl>(MD) &&
Anders Carlsson848fa642010-02-11 18:20:28 +0000575 "Don't use MakeFunction with destructors!");
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000576
Anders Carlsson848fa642010-02-11 18:20:28 +0000577 return VtableComponent(CK_FunctionPointer,
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000578 reinterpret_cast<uintptr_t>(MD));
579 }
580
Anders Carlsson848fa642010-02-11 18:20:28 +0000581 static VtableComponent MakeCompleteDtor(const CXXDestructorDecl *DD) {
582 return VtableComponent(CK_CompleteDtorPointer,
583 reinterpret_cast<uintptr_t>(DD));
584 }
585
586 static VtableComponent MakeDeletingDtor(const CXXDestructorDecl *DD) {
587 return VtableComponent(CK_DeletingDtorPointer,
588 reinterpret_cast<uintptr_t>(DD));
589 }
590
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000591 /// getKind - Get the kind of this vtable component.
592 Kind getKind() const {
593 return (Kind)(Value & 0x7);
594 }
595
596 int64_t getOffsetToTop() const {
597 assert(getKind() == CK_OffsetToTop && "Invalid component kind!");
598
599 return getOffset();
600 }
601
602 const CXXRecordDecl *getRTTIDecl() const {
603 assert(getKind() == CK_RTTI && "Invalid component kind!");
604
605 return reinterpret_cast<CXXRecordDecl *>(getPointer());
606 }
607
608 const CXXMethodDecl *getFunctionDecl() const {
Anders Carlsson848fa642010-02-11 18:20:28 +0000609 assert(getKind() == CK_FunctionPointer);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000610
611 return reinterpret_cast<CXXMethodDecl *>(getPointer());
612 }
Anders Carlsson848fa642010-02-11 18:20:28 +0000613
614 const CXXDestructorDecl *getDestructorDecl() const {
615 assert((getKind() == CK_CompleteDtorPointer ||
616 getKind() == CK_DeletingDtorPointer) && "Invalid component kind!");
617
618 return reinterpret_cast<CXXDestructorDecl *>(getPointer());
619 }
620
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000621private:
622 VtableComponent(Kind ComponentKind, int64_t Offset) {
623 assert((ComponentKind == CK_VCallOffset ||
624 ComponentKind == CK_VBaseOffset ||
625 ComponentKind == CK_OffsetToTop) && "Invalid component kind!");
626 assert(Offset <= ((1LL << 56) - 1) && "Offset is too big!");
627
628 Value = ((Offset << 3) | ComponentKind);
629 }
630
631 VtableComponent(Kind ComponentKind, uintptr_t Ptr) {
632 assert((ComponentKind == CK_RTTI ||
Anders Carlsson848fa642010-02-11 18:20:28 +0000633 ComponentKind == CK_FunctionPointer ||
634 ComponentKind == CK_CompleteDtorPointer ||
635 ComponentKind == CK_DeletingDtorPointer) &&
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000636 "Invalid component kind!");
637
638 assert((Ptr & 7) == 0 && "Pointer not sufficiently aligned!");
639
640 Value = Ptr | ComponentKind;
641 }
642
643 int64_t getOffset() const {
644 assert((getKind() == CK_VCallOffset || getKind() == CK_VBaseOffset ||
645 getKind() == CK_OffsetToTop) && "Invalid component kind!");
646
647 return Value >> 3;
648 }
649
650 uintptr_t getPointer() const {
Anders Carlsson848fa642010-02-11 18:20:28 +0000651 assert((getKind() == CK_RTTI ||
652 getKind() == CK_FunctionPointer ||
653 getKind() == CK_CompleteDtorPointer ||
654 getKind() == CK_DeletingDtorPointer) &&
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000655 "Invalid component kind!");
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000656
657 return static_cast<uintptr_t>(Value & ~7ULL);
658 }
659
660 /// The kind is stored in the lower 3 bits of the value. For offsets, we
661 /// make use of the facts that classes can't be larger than 2^55 bytes,
662 /// so we store the offset in the lower part of the 61 bytes that remain.
663 /// (The reason that we're not simply using a PointerIntPair here is that we
664 /// need the offsets to be 64-bit, even when on a 32-bit machine).
665 int64_t Value;
666};
667
668/// VtableBuilder - Class for building vtable layout information.
Benjamin Kramer85b45212009-11-28 19:45:26 +0000669class VtableBuilder {
Anders Carlsson57071e22010-02-12 05:25:12 +0000670public:
671 /// PrimaryBasesSetTy - A set of direct and indirect primary bases.
672 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 8> PrimaryBasesSetTy;
673
674private:
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000675 /// VtableInfo - Global vtable information.
676 CGVtableInfo &VtableInfo;
677
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000678 /// MostDerivedClass - The most derived class for which we're building this
679 /// vtable.
680 const CXXRecordDecl *MostDerivedClass;
681
682 /// Context - The ASTContext which we will use for layout information.
Anders Carlsson57071e22010-02-12 05:25:12 +0000683 ASTContext &Context;
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000684
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000685 /// FinalOverriders - The final overriders of the most derived class.
686 FinalOverriders Overriders;
687
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000688 /// Components - The components of the vtable being built.
689 llvm::SmallVector<VtableComponent, 64> Components;
690
Anders Carlsson822307b2010-02-11 17:18:51 +0000691 /// AddressPoints - Address points for the vtable being built.
692 CGVtableInfo::AddressPointsMapTy AddressPoints;
693
Anders Carlsson104f45c2010-02-12 17:37:14 +0000694 /// ReturnAdjustment - A return adjustment thunk.
695 struct ReturnAdjustment {
696 /// NonVirtual - The non-virtual adjustment from the derived object to its
697 /// nearest virtual base.
698 int64_t NonVirtual;
699
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000700 /// VBaseOffsetOffset - The offset, in bytes, relative to the address point
701 /// of the virtual base class offset.
702 int64_t VBaseOffsetOffset;
Anders Carlsson104f45c2010-02-12 17:37:14 +0000703
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000704 ReturnAdjustment() : NonVirtual(0), VBaseOffsetOffset(0) { }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000705
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000706 bool isEmpty() const { return !NonVirtual && !VBaseOffsetOffset; }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000707 };
708
709 /// ReturnAdjustments - The return adjustments needed in this vtable.
710 llvm::SmallVector<std::pair<uint64_t, ReturnAdjustment>, 16>
711 ReturnAdjustments;
712
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000713 /// ComputeReturnAdjustment - Compute the return adjustment given a return
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000714 /// adjustment base offset.
715 ReturnAdjustment ComputeReturnAdjustment(FinalOverriders::BaseOffset Offset);
716
717 /// AddMethod - Add a single virtual member function to the vtable
718 /// components vector.
719 void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment);
720
721 /// AddMethods - Add the methods of this base subobject and all its
722 /// primary bases to the vtable components vector.
723 void AddMethods(BaseSubobject Base, PrimaryBasesSetTy &PrimaryBases);
Anders Carlsson57071e22010-02-12 05:25:12 +0000724
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000725 /// layoutSimpleVtable - A test function that will layout very simple vtables
726 /// without any bases. Just used for testing for now.
Anders Carlsson57071e22010-02-12 05:25:12 +0000727 void layoutSimpleVtable(BaseSubobject Base);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000728
729public:
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000730 VtableBuilder(CGVtableInfo &VtableInfo, const CXXRecordDecl *MostDerivedClass)
731 : VtableInfo(VtableInfo), MostDerivedClass(MostDerivedClass),
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000732 Context(MostDerivedClass->getASTContext()), Overriders(MostDerivedClass) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000733
Anders Carlsson57071e22010-02-12 05:25:12 +0000734 layoutSimpleVtable(BaseSubobject(MostDerivedClass, 0));
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000735 }
736
737 /// dumpLayout - Dump the vtable layout.
738 void dumpLayout(llvm::raw_ostream&);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000739};
740
Anders Carlsson57071e22010-02-12 05:25:12 +0000741/// OverridesMethodInPrimaryBase - Checks whether whether this virtual member
742/// function overrides a member function in a direct or indirect primary base.
743/// Returns the overridden member function, or null if none was found.
744static const CXXMethodDecl *
745OverridesMethodInPrimaryBase(const CXXMethodDecl *MD,
746 VtableBuilder::PrimaryBasesSetTy &PrimaryBases) {
747 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
748 E = MD->end_overridden_methods(); I != E; ++I) {
749 const CXXMethodDecl *OverriddenMD = *I;
750 const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent();
751 assert(OverriddenMD->isCanonicalDecl() &&
752 "Should have the canonical decl of the overridden RD!");
753
754 if (PrimaryBases.count(OverriddenRD))
755 return OverriddenMD;
756 }
757
758 return 0;
759}
760
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000761VtableBuilder::ReturnAdjustment
762VtableBuilder::ComputeReturnAdjustment(FinalOverriders::BaseOffset Offset) {
763 ReturnAdjustment Adjustment;
764
765 if (!Offset.isEmpty()) {
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000766 if (Offset.VirtualBase) {
767 // Get the virtual base offset offset.
768 Adjustment.VBaseOffsetOffset =
769 VtableInfo.getVirtualBaseOffsetIndex(Offset.DerivedClass,
770 Offset.VirtualBase);
771 // FIXME: Once the assert in getVirtualBaseOffsetIndex is back again,
772 // we can get rid of this assert.
773 assert(Adjustment.VBaseOffsetOffset != 0 &&
774 "Invalid base offset offset!");
775 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000776
777 Adjustment.NonVirtual = Offset.NonVirtualOffset;
778 }
779
780 return Adjustment;
781}
782
Anders Carlsson57071e22010-02-12 05:25:12 +0000783void
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000784VtableBuilder::AddMethod(const CXXMethodDecl *MD,
785 ReturnAdjustment ReturnAdjustment) {
786 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
787 assert(ReturnAdjustment.isEmpty() &&
788 "Destructor can't have return adjustment!");
789 // Add both the complete destructor and the deleting destructor.
790 Components.push_back(VtableComponent::MakeCompleteDtor(DD));
791 Components.push_back(VtableComponent::MakeDeletingDtor(DD));
792 } else {
793 // Add the return adjustment if necessary.
794 if (!ReturnAdjustment.isEmpty())
795 ReturnAdjustments.push_back(std::make_pair(Components.size(),
796 ReturnAdjustment));
797
798 // Add the function.
799 Components.push_back(VtableComponent::MakeFunction(MD));
800 }
801}
802
803void
804VtableBuilder::AddMethods(BaseSubobject Base, PrimaryBasesSetTy &PrimaryBases) {
Anders Carlsson57071e22010-02-12 05:25:12 +0000805 const CXXRecordDecl *RD = Base.getBase();
806
807 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
808
809 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
810 if (Layout.getPrimaryBaseWasVirtual())
811 assert(false && "FIXME: Handle vbases here.");
812 else
813 assert(Layout.getBaseClassOffset(PrimaryBase) == 0 &&
814 "Primary base should have a zero offset!");
815
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000816 AddMethods(BaseSubobject(PrimaryBase, 0), PrimaryBases);
Anders Carlsson57071e22010-02-12 05:25:12 +0000817
818 if (!PrimaryBases.insert(PrimaryBase))
819 assert(false && "Found a duplicate primary base!");
820 }
Anders Carlsson822307b2010-02-11 17:18:51 +0000821
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000822 // Now go through all virtual member functions and add them.
823 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
824 E = RD->method_end(); I != E; ++I) {
825 const CXXMethodDecl *MD = *I;
Anders Carlsson57071e22010-02-12 05:25:12 +0000826
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000827 if (!MD->isVirtual())
828 continue;
Anders Carlsson57071e22010-02-12 05:25:12 +0000829
830 // Get the final overrider.
Anders Carlssona7575822010-02-12 16:55:34 +0000831 FinalOverriders::OverriderInfo Overrider =
832 Overriders.getOverrider(Base, MD);
Anders Carlsson57071e22010-02-12 05:25:12 +0000833
834 // Check if this virtual member function overrides a method in a primary
835 // base. If this is the case, and the return type doesn't require adjustment
836 // then we can just use the member function from the primary base.
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000837 if (const CXXMethodDecl *OverriddenMD =
838 OverridesMethodInPrimaryBase(MD, PrimaryBases)) {
839 if (!ReturnTypeConversionRequiresAdjustment(MD, OverriddenMD))
840 continue;
841 }
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000842
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000843 // Check if this overrider needs a return adjustment.
844 FinalOverriders::BaseOffset ReturnAdjustmentOffset =
845 Overriders.getReturnAdjustmentOffset(Base, MD);
846
847 ReturnAdjustment ReturnAdjustment =
848 ComputeReturnAdjustment(ReturnAdjustmentOffset);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000849
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000850 AddMethod(Overrider.Method, ReturnAdjustment);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000851 }
852}
853
Anders Carlsson57071e22010-02-12 05:25:12 +0000854void VtableBuilder::layoutSimpleVtable(BaseSubobject Base) {
855 const CXXRecordDecl *RD = Base.getBase();
856
857 // First, add the offset to top.
858 Components.push_back(VtableComponent::MakeOffsetToTop(0));
859
860 // Next, add the RTTI.
861 Components.push_back(VtableComponent::MakeRTTI(RD));
862
Anders Carlsson28cbc8b2010-02-12 07:43:48 +0000863 uint64_t AddressPoint = Components.size();
Anders Carlsson57071e22010-02-12 05:25:12 +0000864
865 // Now go through all virtual member functions and add them.
866 PrimaryBasesSetTy PrimaryBases;
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000867 AddMethods(Base, PrimaryBases);
Anders Carlsson57071e22010-02-12 05:25:12 +0000868
Anders Carlsson28cbc8b2010-02-12 07:43:48 +0000869 // Record the address point.
870 AddressPoints.insert(std::make_pair(Base, AddressPoint));
871
872 // Record the address points for all primary bases.
873 for (PrimaryBasesSetTy::const_iterator I = PrimaryBases.begin(),
874 E = PrimaryBases.end(); I != E; ++I) {
875 const CXXRecordDecl *BaseDecl = *I;
876
877 // We know that all the primary bases have the same offset as the base
878 // subobject.
879 BaseSubobject PrimaryBase(BaseDecl, Base.getBaseOffset());
880 AddressPoints.insert(std::make_pair(PrimaryBase, AddressPoint));
881 }
882
Anders Carlsson57071e22010-02-12 05:25:12 +0000883 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
884 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
885
886 // Traverse bases.
887 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
888 E = RD->bases_end(); I != E; ++I) {
889 const CXXRecordDecl *BaseDecl =
890 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
891
892 // Ignore the primary base.
893 if (BaseDecl == PrimaryBase)
894 continue;
895
896 assert(!I->isVirtual() && "FIXME: Handle virtual bases");
897
898 assert(false && "FIXME: Handle secondary virtual tables!");
899 }
900}
901
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000902/// dumpLayout - Dump the vtable layout.
903void VtableBuilder::dumpLayout(llvm::raw_ostream& Out) {
904
905 Out << "Vtable for '" << MostDerivedClass->getQualifiedNameAsString();
906 Out << "' (" << Components.size() << " entries).\n";
907
Anders Carlsson822307b2010-02-11 17:18:51 +0000908 // Iterate through the address points and insert them into a new map where
909 // they are keyed by the index and not the base object.
910 // Since an address point can be shared by multiple subobjects, we use an
911 // STL multimap.
912 std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex;
913 for (CGVtableInfo::AddressPointsMapTy::const_iterator I =
914 AddressPoints.begin(), E = AddressPoints.end(); I != E; ++I) {
915 const BaseSubobject& Base = I->first;
916 uint64_t Index = I->second;
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000917
Anders Carlsson822307b2010-02-11 17:18:51 +0000918 AddressPointsByIndex.insert(std::make_pair(Index, Base));
919 }
920
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000921 unsigned NextReturnAdjustmentIndex = 0;
Anders Carlsson822307b2010-02-11 17:18:51 +0000922 for (unsigned I = 0, E = Components.size(); I != E; ++I) {
Anders Carlsson28cbc8b2010-02-12 07:43:48 +0000923 uint64_t Index = I;
924
Anders Carlsson822307b2010-02-11 17:18:51 +0000925 if (AddressPointsByIndex.count(I)) {
Anders Carlsson28cbc8b2010-02-12 07:43:48 +0000926 if (AddressPointsByIndex.count(Index) == 1) {
927 const BaseSubobject &Base = AddressPointsByIndex.find(Index)->second;
928
929 // FIXME: Instead of dividing by 8, we should be using CharUnits.
930 Out << " -- (" << Base.getBase()->getQualifiedNameAsString();
931 Out << ", " << Base.getBaseOffset() / 8 << ") vtable address --\n";
932 } else {
933 uint64_t BaseOffset =
934 AddressPointsByIndex.lower_bound(Index)->second.getBaseOffset();
935
936 // We store the class names in a set to get a stable order.
937 std::set<std::string> ClassNames;
938 for (std::multimap<uint64_t, BaseSubobject>::const_iterator I =
939 AddressPointsByIndex.lower_bound(Index), E =
940 AddressPointsByIndex.upper_bound(Index); I != E; ++I) {
941 assert(I->second.getBaseOffset() == BaseOffset &&
942 "Invalid base offset!");
943 const CXXRecordDecl *RD = I->second.getBase();
944 ClassNames.insert(RD->getQualifiedNameAsString());
945 }
946
947 for (std::set<std::string>::const_iterator I = ClassNames.begin(),
948 E = ClassNames.end(); I != E; ++I) {
949 // FIXME: Instead of dividing by 8, we should be using CharUnits.
950 Out << " -- (" << *I;
951 Out << ", " << BaseOffset / 8 << ") vtable address --\n";
952 }
953 }
Anders Carlsson822307b2010-02-11 17:18:51 +0000954 }
955
956 Out << llvm::format("%4d | ", I);
957
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000958 const VtableComponent &Component = Components[I];
959
960 // Dump the component.
961 switch (Component.getKind()) {
962 // FIXME: Remove this default case.
963 default:
964 assert(false && "Unhandled component kind!");
965 break;
966
967 case VtableComponent::CK_OffsetToTop:
968 Out << "offset_to_top (" << Component.getOffsetToTop() << ")";
969 break;
970
971 case VtableComponent::CK_RTTI:
972 Out << Component.getRTTIDecl()->getQualifiedNameAsString() << " RTTI";
973 break;
974
Anders Carlsson848fa642010-02-11 18:20:28 +0000975 case VtableComponent::CK_FunctionPointer: {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000976 const CXXMethodDecl *MD = Component.getFunctionDecl();
977
Anders Carlsson848fa642010-02-11 18:20:28 +0000978 std::string Str =
979 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
980 MD);
981 Out << Str;
Anders Carlsson98241422010-02-12 02:38:13 +0000982 if (MD->isPure())
983 Out << " [pure]";
984
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000985 // If this function pointer has a return adjustment thunk, dump it.
986 if (NextReturnAdjustmentIndex < ReturnAdjustments.size() &&
987 ReturnAdjustments[NextReturnAdjustmentIndex].first == I) {
988 const ReturnAdjustment Adjustment =
989 ReturnAdjustments[NextReturnAdjustmentIndex].second;
990
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000991 Out << "\n [return adjustment: ";
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000992 Out << Adjustment.NonVirtual << " non-virtual";
993
994 if (Adjustment.VBaseOffsetOffset)
995 Out << ", " << Adjustment.VBaseOffsetOffset << " vbase offset offset";
996 Out << ']';
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000997
998 NextReturnAdjustmentIndex++;
999 }
1000
Anders Carlsson848fa642010-02-11 18:20:28 +00001001 break;
1002 }
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001003
Anders Carlsson848fa642010-02-11 18:20:28 +00001004 case VtableComponent::CK_CompleteDtorPointer: {
1005 const CXXDestructorDecl *DD = Component.getDestructorDecl();
1006
1007 Out << DD->getQualifiedNameAsString() << "() [complete]";
Anders Carlsson98241422010-02-12 02:38:13 +00001008 if (DD->isPure())
1009 Out << " [pure]";
1010
Anders Carlsson848fa642010-02-11 18:20:28 +00001011 break;
1012 }
1013
1014 case VtableComponent::CK_DeletingDtorPointer: {
1015 const CXXDestructorDecl *DD = Component.getDestructorDecl();
1016
1017 Out << DD->getQualifiedNameAsString() << "() [deleting]";
Anders Carlsson98241422010-02-12 02:38:13 +00001018 if (DD->isPure())
1019 Out << " [pure]";
1020
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001021 break;
1022 }
1023
1024 }
Anders Carlsson822307b2010-02-11 17:18:51 +00001025
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001026 Out << '\n';
1027 }
1028
1029}
1030
1031}
1032
1033namespace {
1034class OldVtableBuilder {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001035public:
1036 /// Index_t - Vtable index type.
1037 typedef uint64_t Index_t;
Eli Friedmanb455f0e2009-12-07 23:56:34 +00001038 typedef std::vector<std::pair<GlobalDecl,
1039 std::pair<GlobalDecl, ThunkAdjustment> > >
1040 SavedAdjustmentsVectorTy;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001041private:
Anders Carlsson15318f42009-12-04 16:19:30 +00001042
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00001043 // VtableComponents - The components of the vtable being built.
1044 typedef llvm::SmallVector<llvm::Constant *, 64> VtableComponentsVectorTy;
1045 VtableComponentsVectorTy VtableComponents;
1046
Eli Friedman152b5b12009-12-05 01:05:03 +00001047 const bool BuildVtable;
1048
Anders Carlssondbd920c2009-10-11 22:13:54 +00001049 llvm::Type *Ptr8Ty;
Anders Carlssonac3f7bd2009-12-04 18:36:22 +00001050
1051 /// MostDerivedClass - The most derived class that this vtable is being
1052 /// built for.
1053 const CXXRecordDecl *MostDerivedClass;
1054
Mike Stumpacfd1e52009-11-13 01:54:23 +00001055 /// LayoutClass - The most derived class used for virtual base layout
1056 /// information.
1057 const CXXRecordDecl *LayoutClass;
Mike Stump4cde6262009-11-13 02:13:54 +00001058 /// LayoutOffset - The offset for Class in LayoutClass.
1059 uint64_t LayoutOffset;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001060 /// BLayout - Layout for the most derived class that this vtable is being
1061 /// built for.
1062 const ASTRecordLayout &BLayout;
1063 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
1064 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
1065 llvm::Constant *rtti;
1066 llvm::LLVMContext &VMContext;
1067 CodeGenModule &CGM; // Per-module state.
Anders Carlsson0e881162009-12-04 03:46:21 +00001068
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001069 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall;
Anders Carlssona0fdd912009-11-13 17:08:56 +00001070 llvm::DenseMap<GlobalDecl, Index_t> VCallOffset;
Mike Stumpd99a4d22010-01-26 03:42:22 +00001071 llvm::DenseMap<GlobalDecl, Index_t> VCallOffsetForVCall;
Mike Stump9e7e3c62009-11-06 23:27:42 +00001072 // This is the offset to the nearest virtual base
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001073 llvm::DenseMap<const CXXMethodDecl *, Index_t> NonVirtualOffset;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001074 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stump94aff932009-10-27 23:46:47 +00001075
Anders Carlsson6d4ccb72009-11-26 19:54:33 +00001076 /// PureVirtualFunction - Points to __cxa_pure_virtual.
1077 llvm::Constant *PureVirtualFn;
1078
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00001079 /// VtableMethods - A data structure for keeping track of methods in a vtable.
1080 /// Can add methods, override methods and iterate in vtable order.
1081 class VtableMethods {
1082 // MethodToIndexMap - Maps from a global decl to the index it has in the
1083 // Methods vector.
1084 llvm::DenseMap<GlobalDecl, uint64_t> MethodToIndexMap;
1085
1086 /// Methods - The methods, in vtable order.
1087 typedef llvm::SmallVector<GlobalDecl, 16> MethodsVectorTy;
1088 MethodsVectorTy Methods;
Eli Friedman72649ed2009-12-06 22:01:30 +00001089 MethodsVectorTy OrigMethods;
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00001090
1091 public:
1092 /// AddMethod - Add a method to the vtable methods.
1093 void AddMethod(GlobalDecl GD) {
1094 assert(!MethodToIndexMap.count(GD) &&
1095 "Method has already been added!");
1096
1097 MethodToIndexMap[GD] = Methods.size();
1098 Methods.push_back(GD);
Eli Friedman72649ed2009-12-06 22:01:30 +00001099 OrigMethods.push_back(GD);
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00001100 }
1101
1102 /// OverrideMethod - Replace a method with another.
1103 void OverrideMethod(GlobalDecl OverriddenGD, GlobalDecl GD) {
1104 llvm::DenseMap<GlobalDecl, uint64_t>::iterator i
1105 = MethodToIndexMap.find(OverriddenGD);
1106 assert(i != MethodToIndexMap.end() && "Did not find entry!");
1107
1108 // Get the index of the old decl.
1109 uint64_t Index = i->second;
1110
1111 // Replace the old decl with the new decl.
1112 Methods[Index] = GD;
1113
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00001114 // And add the new.
1115 MethodToIndexMap[GD] = Index;
1116 }
1117
Anders Carlsson77c23e52009-12-04 15:49:02 +00001118 /// getIndex - Gives the index of a passed in GlobalDecl. Returns false if
1119 /// the index couldn't be found.
Benjamin Kramercce9fde2009-12-04 22:45:27 +00001120 bool getIndex(GlobalDecl GD, uint64_t &Index) const {
Anders Carlsson77c23e52009-12-04 15:49:02 +00001121 llvm::DenseMap<GlobalDecl, uint64_t>::const_iterator i
1122 = MethodToIndexMap.find(GD);
Eli Friedman47145832009-12-04 08:34:14 +00001123
Anders Carlsson77c23e52009-12-04 15:49:02 +00001124 if (i == MethodToIndexMap.end())
1125 return false;
Anders Carlssonc0c49932009-12-04 03:41:37 +00001126
Anders Carlsson77c23e52009-12-04 15:49:02 +00001127 Index = i->second;
1128 return true;
Anders Carlssonc0c49932009-12-04 03:41:37 +00001129 }
1130
Eli Friedman72649ed2009-12-06 22:01:30 +00001131 GlobalDecl getOrigMethod(uint64_t Index) const {
1132 return OrigMethods[Index];
1133 }
1134
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00001135 MethodsVectorTy::size_type size() const {
1136 return Methods.size();
1137 }
1138
1139 void clear() {
1140 MethodToIndexMap.clear();
1141 Methods.clear();
Eli Friedman72649ed2009-12-06 22:01:30 +00001142 OrigMethods.clear();
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00001143 }
1144
Anders Carlssonc0c49932009-12-04 03:41:37 +00001145 GlobalDecl operator[](uint64_t Index) const {
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00001146 return Methods[Index];
1147 }
1148 };
1149
1150 /// Methods - The vtable methods we're currently building.
1151 VtableMethods Methods;
1152
Anders Carlsson98fdb242009-12-04 02:26:15 +00001153 /// ThisAdjustments - For a given index in the vtable, contains the 'this'
1154 /// pointer adjustment needed for a method.
1155 typedef llvm::DenseMap<uint64_t, ThunkAdjustment> ThisAdjustmentsMapTy;
1156 ThisAdjustmentsMapTy ThisAdjustments;
Anders Carlsson5dd730a2009-11-26 19:32:45 +00001157
Eli Friedmanb455f0e2009-12-07 23:56:34 +00001158 SavedAdjustmentsVectorTy SavedAdjustments;
Eli Friedman72649ed2009-12-06 22:01:30 +00001159
Anders Carlssona8756702009-12-04 02:22:02 +00001160 /// BaseReturnTypes - Contains the base return types of methods who have been
1161 /// overridden with methods whose return types require adjustment. Used for
1162 /// generating covariant thunk information.
1163 typedef llvm::DenseMap<uint64_t, CanQualType> BaseReturnTypesMapTy;
1164 BaseReturnTypesMapTy BaseReturnTypes;
Anders Carlsson5dd730a2009-11-26 19:32:45 +00001165
Anders Carlssondbd920c2009-10-11 22:13:54 +00001166 std::vector<Index_t> VCalls;
Mike Stump9840c702009-11-12 20:47:57 +00001167
1168 typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t;
Mike Stump23a35422009-11-19 20:52:19 +00001169 // subAddressPoints - Used to hold the AddressPoints (offsets) into the built
1170 // vtable for use in computing the initializers for the VTT.
1171 llvm::DenseMap<CtorVtable_t, int64_t> &subAddressPoints;
Mike Stump9840c702009-11-12 20:47:57 +00001172
Anders Carlsson1bb60992010-01-14 02:29:07 +00001173 /// AddressPoints - Address points for this vtable.
1174 CGVtableInfo::AddressPointsMapTy& AddressPoints;
1175
Anders Carlssondbd920c2009-10-11 22:13:54 +00001176 typedef CXXRecordDecl::method_iterator method_iter;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001177 const uint32_t LLVMPointerWidth;
1178 Index_t extra;
Mike Stump11dea942009-10-15 02:04:03 +00001179 typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t;
Mike Stump23a35422009-11-19 20:52:19 +00001180 static llvm::DenseMap<CtorVtable_t, int64_t>&
1181 AllocAddressPoint(CodeGenModule &cgm, const CXXRecordDecl *l,
1182 const CXXRecordDecl *c) {
Anders Carlsson21431c52010-01-02 18:02:32 +00001183 CGVtableInfo::AddrMap_t *&oref = cgm.getVtableInfo().AddressPoints[l];
Mike Stump23a35422009-11-19 20:52:19 +00001184 if (oref == 0)
Anders Carlsson21431c52010-01-02 18:02:32 +00001185 oref = new CGVtableInfo::AddrMap_t;
Mike Stump23a35422009-11-19 20:52:19 +00001186
1187 llvm::DenseMap<CtorVtable_t, int64_t> *&ref = (*oref)[c];
1188 if (ref == 0)
1189 ref = new llvm::DenseMap<CtorVtable_t, int64_t>;
1190 return *ref;
1191 }
Anders Carlsson6d4ccb72009-11-26 19:54:33 +00001192
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001193 bool DclIsSame(const FunctionDecl *New, const FunctionDecl *Old) {
1194 FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate();
1195 FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate();
1196
1197 // C++ [temp.fct]p2:
1198 // A function template can be overloaded with other function templates
1199 // and with normal (non-template) functions.
1200 if ((OldTemplate == 0) != (NewTemplate == 0))
1201 return false;
1202
1203 // Is the function New an overload of the function Old?
1204 QualType OldQType = CGM.getContext().getCanonicalType(Old->getType());
1205 QualType NewQType = CGM.getContext().getCanonicalType(New->getType());
1206
1207 // Compare the signatures (C++ 1.3.10) of the two functions to
1208 // determine whether they are overloads. If we find any mismatch
1209 // in the signature, they are overloads.
1210
1211 // If either of these functions is a K&R-style function (no
1212 // prototype), then we consider them to have matching signatures.
1213 if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) ||
1214 isa<FunctionNoProtoType>(NewQType.getTypePtr()))
1215 return true;
1216
1217 FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType);
1218 FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType);
1219
1220 // The signature of a function includes the types of its
1221 // parameters (C++ 1.3.10), which includes the presence or absence
1222 // of the ellipsis; see C++ DR 357).
1223 if (OldQType != NewQType &&
1224 (OldType->getNumArgs() != NewType->getNumArgs() ||
1225 OldType->isVariadic() != NewType->isVariadic() ||
1226 !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(),
1227 NewType->arg_type_begin())))
1228 return false;
1229
1230#if 0
1231 // C++ [temp.over.link]p4:
1232 // The signature of a function template consists of its function
1233 // signature, its return type and its template parameter list. The names
1234 // of the template parameters are significant only for establishing the
1235 // relationship between the template parameters and the rest of the
1236 // signature.
1237 //
1238 // We check the return type and template parameter lists for function
1239 // templates first; the remaining checks follow.
1240 if (NewTemplate &&
1241 (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(),
1242 OldTemplate->getTemplateParameters(),
1243 TPL_TemplateMatch) ||
1244 OldType->getResultType() != NewType->getResultType()))
1245 return false;
1246#endif
1247
1248 // If the function is a class member, its signature includes the
1249 // cv-qualifiers (if any) on the function itself.
1250 //
1251 // As part of this, also check whether one of the member functions
1252 // is static, in which case they are not overloads (C++
1253 // 13.1p2). While not part of the definition of the signature,
1254 // this check is important to determine whether these functions
1255 // can be overloaded.
1256 const CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old);
1257 const CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New);
1258 if (OldMethod && NewMethod &&
1259 !OldMethod->isStatic() && !NewMethod->isStatic() &&
1260 OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers())
1261 return false;
1262
1263 // The signatures match; this is not an overload.
1264 return true;
1265 }
1266
1267 typedef llvm::DenseMap<const CXXMethodDecl *, const CXXMethodDecl*>
1268 ForwardUnique_t;
1269 ForwardUnique_t ForwardUnique;
1270 llvm::DenseMap<const CXXMethodDecl*, const CXXMethodDecl*> UniqueOverrider;
1271
1272 void BuildUniqueOverrider(const CXXMethodDecl *U, const CXXMethodDecl *MD) {
1273 const CXXMethodDecl *PrevU = UniqueOverrider[MD];
1274 assert(U && "no unique overrider");
1275 if (PrevU == U)
1276 return;
1277 if (PrevU != U && PrevU != 0) {
1278 // If already set, note the two sets as the same
1279 if (0)
1280 printf("%s::%s same as %s::%s\n",
1281 PrevU->getParent()->getNameAsCString(),
1282 PrevU->getNameAsCString(),
1283 U->getParent()->getNameAsCString(),
1284 U->getNameAsCString());
1285 ForwardUnique[PrevU] = U;
1286 return;
1287 }
1288
1289 // Not set, set it now
1290 if (0)
1291 printf("marking %s::%s %p override as %s::%s\n",
1292 MD->getParent()->getNameAsCString(),
1293 MD->getNameAsCString(),
1294 (void*)MD,
1295 U->getParent()->getNameAsCString(),
1296 U->getNameAsCString());
1297 UniqueOverrider[MD] = U;
1298
1299 for (CXXMethodDecl::method_iterator mi = MD->begin_overridden_methods(),
1300 me = MD->end_overridden_methods(); mi != me; ++mi) {
1301 BuildUniqueOverrider(U, *mi);
1302 }
1303 }
1304
1305 void BuildUniqueOverriders(const CXXRecordDecl *RD) {
1306 if (0) printf("walking %s\n", RD->getNameAsCString());
1307 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
1308 e = RD->method_end(); i != e; ++i) {
1309 const CXXMethodDecl *MD = *i;
1310 if (!MD->isVirtual())
1311 continue;
1312
1313 if (UniqueOverrider[MD] == 0) {
1314 // Only set this, if it hasn't been set yet.
1315 BuildUniqueOverrider(MD, MD);
1316 if (0)
1317 printf("top set is %s::%s %p\n",
1318 MD->getParent()->getNameAsCString(),
1319 MD->getNameAsCString(),
1320 (void*)MD);
1321 ForwardUnique[MD] = MD;
1322 }
1323 }
1324 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1325 e = RD->bases_end(); i != e; ++i) {
1326 const CXXRecordDecl *Base =
1327 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1328 BuildUniqueOverriders(Base);
1329 }
1330 }
1331
1332 static int DclCmp(const void *p1, const void *p2) {
John McCall7fe0b9e2010-02-13 01:04:05 +00001333 const CXXMethodDecl *MD1 = *(const CXXMethodDecl *const *)p1;
1334 const CXXMethodDecl *MD2 = *(const CXXMethodDecl *const *)p2;
1335
1336 return (DeclarationName::compare(MD1->getDeclName(), MD2->getDeclName()));
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001337 }
1338
1339 void MergeForwarding() {
1340 typedef llvm::SmallVector<const CXXMethodDecl *, 100> A_t;
1341 A_t A;
1342 for (ForwardUnique_t::iterator I = ForwardUnique.begin(),
1343 E = ForwardUnique.end(); I != E; ++I) {
1344 if (I->first == I->second)
1345 // Only add the roots of all trees
1346 A.push_back(I->first);
1347 }
1348 llvm::array_pod_sort(A.begin(), A.end(), DclCmp);
1349 for (A_t::iterator I = A.begin(),
1350 E = A.end(); I != E; ++I) {
1351 A_t::iterator J = I;
John McCall7fe0b9e2010-02-13 01:04:05 +00001352 while (++J != E && DclCmp(I, J) == 0)
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001353 if (DclIsSame(*I, *J)) {
Eli Friedman49677622010-02-13 00:03:21 +00001354 if (0) printf("connecting %s\n", (*I)->getNameAsCString());
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001355 ForwardUnique[*J] = *I;
1356 }
1357 }
1358 }
1359
1360 const CXXMethodDecl *getUnique(const CXXMethodDecl *MD) {
1361 const CXXMethodDecl *U = UniqueOverrider[MD];
1362 assert(U && "unique overrider not found");
1363 while (ForwardUnique.count(U)) {
1364 const CXXMethodDecl *NU = ForwardUnique[U];
1365 if (NU == U) break;
1366 U = NU;
1367 }
1368 return U;
1369 }
Anders Carlsson0f0bbbc2010-01-26 17:36:47 +00001370
1371 GlobalDecl getUnique(GlobalDecl GD) {
1372 const CXXMethodDecl *Unique = getUnique(cast<CXXMethodDecl>(GD.getDecl()));
1373
1374 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Unique))
1375 return GlobalDecl(CD, GD.getCtorType());
1376
1377 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(Unique))
1378 return GlobalDecl(DD, GD.getDtorType());
1379
1380 return Unique;
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001381 }
1382
Anders Carlsson6d4ccb72009-11-26 19:54:33 +00001383 /// getPureVirtualFn - Return the __cxa_pure_virtual function.
1384 llvm::Constant* getPureVirtualFn() {
1385 if (!PureVirtualFn) {
1386 const llvm::FunctionType *Ty =
1387 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
1388 /*isVarArg=*/false);
1389 PureVirtualFn = wrap(CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual"));
1390 }
1391
1392 return PureVirtualFn;
1393 }
1394
Anders Carlssondbd920c2009-10-11 22:13:54 +00001395public:
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001396 OldVtableBuilder(const CXXRecordDecl *MostDerivedClass,
Eli Friedman152b5b12009-12-05 01:05:03 +00001397 const CXXRecordDecl *l, uint64_t lo, CodeGenModule &cgm,
Anders Carlsson1bb60992010-01-14 02:29:07 +00001398 bool build, CGVtableInfo::AddressPointsMapTy& AddressPoints)
Eli Friedman152b5b12009-12-05 01:05:03 +00001399 : BuildVtable(build), MostDerivedClass(MostDerivedClass), LayoutClass(l),
1400 LayoutOffset(lo), BLayout(cgm.getContext().getASTRecordLayout(l)),
1401 rtti(0), VMContext(cgm.getModule().getContext()),CGM(cgm),
1402 PureVirtualFn(0),
Anders Carlssonac3f7bd2009-12-04 18:36:22 +00001403 subAddressPoints(AllocAddressPoint(cgm, l, MostDerivedClass)),
Anders Carlsson1bb60992010-01-14 02:29:07 +00001404 AddressPoints(AddressPoints),
1405 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0))
1406 {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001407 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001408 if (BuildVtable) {
1409 QualType ClassType = CGM.getContext().getTagDeclType(MostDerivedClass);
1410 rtti = CGM.GetAddrOfRTTIDescriptor(ClassType);
1411 }
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001412 BuildUniqueOverriders(MostDerivedClass);
1413 MergeForwarding();
Anders Carlssondbd920c2009-10-11 22:13:54 +00001414 }
1415
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00001416 // getVtableComponents - Returns a reference to the vtable components.
1417 const VtableComponentsVectorTy &getVtableComponents() const {
1418 return VtableComponents;
Anders Carlsson15318f42009-12-04 16:19:30 +00001419 }
1420
Anders Carlssonc997d422010-01-02 01:01:18 +00001421 llvm::DenseMap<const CXXRecordDecl *, uint64_t> &getVBIndex()
Anders Carlssondbd920c2009-10-11 22:13:54 +00001422 { return VBIndex; }
1423
Eli Friedmanb455f0e2009-12-07 23:56:34 +00001424 SavedAdjustmentsVectorTy &getSavedAdjustments()
1425 { return SavedAdjustments; }
Eli Friedman72649ed2009-12-06 22:01:30 +00001426
Anders Carlssondbd920c2009-10-11 22:13:54 +00001427 llvm::Constant *wrap(Index_t i) {
1428 llvm::Constant *m;
1429 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
1430 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
1431 }
1432
1433 llvm::Constant *wrap(llvm::Constant *m) {
1434 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
1435 }
1436
Mike Stumpd6584902010-01-22 02:51:26 +00001437//#define D1(x)
1438#define D1(X) do { if (getenv("DEBUG")) { X; } } while (0)
Mike Stump6a9612f2009-10-31 20:06:59 +00001439
1440 void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset,
Mike Stumpab28c132009-10-13 22:54:56 +00001441 bool updateVBIndex, Index_t current_vbindex) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001442 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1443 e = RD->bases_end(); i != e; ++i) {
1444 const CXXRecordDecl *Base =
1445 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stumpab28c132009-10-13 22:54:56 +00001446 Index_t next_vbindex = current_vbindex;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001447 if (i->isVirtual() && !SeenVBase.count(Base)) {
1448 SeenVBase.insert(Base);
Mike Stumpab28c132009-10-13 22:54:56 +00001449 if (updateVBIndex) {
Mike Stump6a9612f2009-10-31 20:06:59 +00001450 next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8)
Mike Stumpab28c132009-10-13 22:54:56 +00001451 - 3*LLVMPointerWidth/8);
1452 VBIndex[Base] = next_vbindex;
1453 }
Mike Stump6a9612f2009-10-31 20:06:59 +00001454 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
1455 VCalls.push_back((0?700:0) + BaseOffset);
1456 D1(printf(" vbase for %s at %d delta %d most derived %s\n",
1457 Base->getNameAsCString(),
1458 (int)-VCalls.size()-3, (int)BaseOffset,
Mike Stumpd6584902010-01-22 02:51:26 +00001459 MostDerivedClass->getNameAsCString()));
Anders Carlssondbd920c2009-10-11 22:13:54 +00001460 }
Mike Stumpab28c132009-10-13 22:54:56 +00001461 // We also record offsets for non-virtual bases to closest enclosing
1462 // virtual base. We do this so that we don't have to search
1463 // for the nearst virtual base class when generating thunks.
1464 if (updateVBIndex && VBIndex.count(Base) == 0)
1465 VBIndex[Base] = next_vbindex;
Mike Stump6a9612f2009-10-31 20:06:59 +00001466 GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex);
Anders Carlssondbd920c2009-10-11 22:13:54 +00001467 }
1468 }
1469
1470 void StartNewTable() {
1471 SeenVBase.clear();
1472 }
1473
Mike Stump3425b972009-10-15 09:30:16 +00001474 Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B,
1475 Index_t Offset = 0) {
1476
1477 if (B == D)
1478 return Offset;
1479
1480 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D);
1481 for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(),
1482 e = D->bases_end(); i != e; ++i) {
1483 const CXXRecordDecl *Base =
1484 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1485 int64_t BaseOffset = 0;
1486 if (!i->isVirtual())
1487 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
1488 int64_t o = getNVOffset_1(Base, B, BaseOffset);
1489 if (o >= 0)
1490 return o;
1491 }
1492
1493 return -1;
1494 }
1495
1496 /// getNVOffset - Returns the non-virtual offset for the given (B) base of the
1497 /// derived class D.
1498 Index_t getNVOffset(QualType qB, QualType qD) {
Mike Stump9c212892009-11-03 19:03:17 +00001499 qD = qD->getPointeeType();
1500 qB = qB->getPointeeType();
Mike Stump3425b972009-10-15 09:30:16 +00001501 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
1502 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
1503 int64_t o = getNVOffset_1(D, B);
1504 if (o >= 0)
1505 return o;
1506
1507 assert(false && "FIXME: non-virtual base not found");
1508 return 0;
1509 }
1510
Anders Carlssondbd920c2009-10-11 22:13:54 +00001511 /// getVbaseOffset - Returns the index into the vtable for the virtual base
1512 /// offset for the given (B) virtual base of the derived class D.
1513 Index_t getVbaseOffset(QualType qB, QualType qD) {
Mike Stump9c212892009-11-03 19:03:17 +00001514 qD = qD->getPointeeType();
1515 qB = qB->getPointeeType();
Anders Carlssondbd920c2009-10-11 22:13:54 +00001516 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
1517 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
Anders Carlssonac3f7bd2009-12-04 18:36:22 +00001518 if (D != MostDerivedClass)
Eli Friedman76ed1f72009-11-30 01:19:33 +00001519 return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B);
Anders Carlssondbd920c2009-10-11 22:13:54 +00001520 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
1521 i = VBIndex.find(B);
1522 if (i != VBIndex.end())
1523 return i->second;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001524
Mike Stumpab28c132009-10-13 22:54:56 +00001525 assert(false && "FIXME: Base not found");
Anders Carlssondbd920c2009-10-11 22:13:54 +00001526 return 0;
1527 }
1528
Eli Friedman367d1222009-12-04 08:40:51 +00001529 bool OverrideMethod(GlobalDecl GD, bool MorallyVirtual,
1530 Index_t OverrideOffset, Index_t Offset,
1531 int64_t CurrentVBaseOffset);
Anders Carlssondbd920c2009-10-11 22:13:54 +00001532
Anders Carlssonadfa2672009-12-04 02:39:04 +00001533 /// AppendMethods - Append the current methods to the vtable.
Anders Carlssonbf540272009-12-04 02:56:03 +00001534 void AppendMethodsToVtable();
Anders Carlssonadfa2672009-12-04 02:39:04 +00001535
Anders Carlssona0fdd912009-11-13 17:08:56 +00001536 llvm::Constant *WrapAddrOf(GlobalDecl GD) {
1537 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1538
Anders Carlssonecf282b2009-11-24 05:08:52 +00001539 const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD);
Mike Stump1ae31782009-10-27 23:36:26 +00001540
Mike Stumpc085a982009-12-03 16:55:20 +00001541 return wrap(CGM.GetAddrOfFunction(GD, Ty));
Mike Stump1ae31782009-10-27 23:36:26 +00001542 }
1543
Mike Stump9e7e3c62009-11-06 23:27:42 +00001544 void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset,
1545 int64_t CurrentVBaseOffset) {
Mike Stump11dea942009-10-15 02:04:03 +00001546 for (Path_t::reverse_iterator i = Path->rbegin(),
Anders Carlssondbd920c2009-10-11 22:13:54 +00001547 e = Path->rend(); i != e; ++i) {
1548 const CXXRecordDecl *RD = i->first;
Mike Stump3425b972009-10-15 09:30:16 +00001549 int64_t OverrideOffset = i->second;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001550 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
1551 ++mi) {
Anders Carlssona0fdd912009-11-13 17:08:56 +00001552 const CXXMethodDecl *MD = *mi;
1553
1554 if (!MD->isVirtual())
Anders Carlssondbd920c2009-10-11 22:13:54 +00001555 continue;
1556
Anders Carlssona0fdd912009-11-13 17:08:56 +00001557 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1558 // Override both the complete and the deleting destructor.
1559 GlobalDecl CompDtor(DD, Dtor_Complete);
Eli Friedman367d1222009-12-04 08:40:51 +00001560 OverrideMethod(CompDtor, MorallyVirtual, OverrideOffset, Offset,
1561 CurrentVBaseOffset);
1562
Anders Carlssona0fdd912009-11-13 17:08:56 +00001563 GlobalDecl DeletingDtor(DD, Dtor_Deleting);
Eli Friedman367d1222009-12-04 08:40:51 +00001564 OverrideMethod(DeletingDtor, MorallyVirtual, OverrideOffset, Offset,
1565 CurrentVBaseOffset);
Anders Carlssona0fdd912009-11-13 17:08:56 +00001566 } else {
Eli Friedman367d1222009-12-04 08:40:51 +00001567 OverrideMethod(MD, MorallyVirtual, OverrideOffset, Offset,
1568 CurrentVBaseOffset);
Anders Carlssona0fdd912009-11-13 17:08:56 +00001569 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00001570 }
1571 }
1572 }
1573
Anders Carlssona0fdd912009-11-13 17:08:56 +00001574 void AddMethod(const GlobalDecl GD, bool MorallyVirtual, Index_t Offset,
Eli Friedman76ed1f72009-11-30 01:19:33 +00001575 int64_t CurrentVBaseOffset) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001576 // If we can find a previously allocated slot for this, reuse it.
Eli Friedman367d1222009-12-04 08:40:51 +00001577 if (OverrideMethod(GD, MorallyVirtual, Offset, Offset,
Mike Stump9e7e3c62009-11-06 23:27:42 +00001578 CurrentVBaseOffset))
Anders Carlssondbd920c2009-10-11 22:13:54 +00001579 return;
1580
Mike Stump372ade22010-01-22 18:48:47 +00001581 D1(printf(" vfn for %s at %d\n",
1582 dyn_cast<CXXMethodDecl>(GD.getDecl())->getNameAsCString(),
1583 (int)Methods.size()));
1584
Anders Carlssona7f19112009-12-04 02:08:24 +00001585 // We didn't find an entry in the vtable that we could use, add a new
1586 // entry.
1587 Methods.AddMethod(GD);
1588
Mike Stump15189fb2010-01-26 21:35:27 +00001589 VCallOffset[GD] = Offset/8 - CurrentVBaseOffset/8;
1590
Anders Carlssondbd920c2009-10-11 22:13:54 +00001591 if (MorallyVirtual) {
Anders Carlsson0f0bbbc2010-01-26 17:36:47 +00001592 GlobalDecl UGD = getUnique(GD);
1593 const CXXMethodDecl *UMD = cast<CXXMethodDecl>(UGD.getDecl());
1594
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001595 assert(UMD && "final overrider not found");
1596
1597 Index_t &idx = VCall[UMD];
Anders Carlssondbd920c2009-10-11 22:13:54 +00001598 // Allocate the first one, after that, we reuse the previous one.
1599 if (idx == 0) {
Anders Carlsson0f0bbbc2010-01-26 17:36:47 +00001600 VCallOffsetForVCall[UGD] = Offset/8;
Mike Stump15189fb2010-01-26 21:35:27 +00001601 NonVirtualOffset[UMD] = Offset/8 - CurrentVBaseOffset/8;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001602 idx = VCalls.size()+1;
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001603 VCalls.push_back(Offset/8 - CurrentVBaseOffset/8);
Mike Stump6a9612f2009-10-31 20:06:59 +00001604 D1(printf(" vcall for %s at %d with delta %d\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001605 dyn_cast<CXXMethodDecl>(GD.getDecl())->getNameAsCString(),
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001606 (int)-VCalls.size()-3, (int)VCalls[idx-1]));
Anders Carlssondbd920c2009-10-11 22:13:54 +00001607 }
1608 }
1609 }
1610
1611 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
Eli Friedman76ed1f72009-11-30 01:19:33 +00001612 Index_t Offset, int64_t CurrentVBaseOffset) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001613 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
Anders Carlssona0fdd912009-11-13 17:08:56 +00001614 ++mi) {
1615 const CXXMethodDecl *MD = *mi;
1616 if (!MD->isVirtual())
1617 continue;
1618
1619 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1620 // For destructors, add both the complete and the deleting destructor
1621 // to the vtable.
1622 AddMethod(GlobalDecl(DD, Dtor_Complete), MorallyVirtual, Offset,
Mike Stump9e7e3c62009-11-06 23:27:42 +00001623 CurrentVBaseOffset);
Eli Friedman76ed1f72009-11-30 01:19:33 +00001624 AddMethod(GlobalDecl(DD, Dtor_Deleting), MorallyVirtual, Offset,
1625 CurrentVBaseOffset);
1626 } else
1627 AddMethod(MD, MorallyVirtual, Offset, CurrentVBaseOffset);
Anders Carlssona0fdd912009-11-13 17:08:56 +00001628 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00001629 }
1630
1631 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
1632 const CXXRecordDecl *PrimaryBase,
1633 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
Mike Stump9e7e3c62009-11-06 23:27:42 +00001634 int64_t Offset, int64_t CurrentVBaseOffset,
1635 Path_t *Path) {
Mike Stump11dea942009-10-15 02:04:03 +00001636 Path->push_back(std::make_pair(RD, Offset));
Anders Carlssondbd920c2009-10-11 22:13:54 +00001637 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1638 e = RD->bases_end(); i != e; ++i) {
1639 if (i->isVirtual())
1640 continue;
1641 const CXXRecordDecl *Base =
1642 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001643 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
1644 StartNewTable();
1645 GenerateVtableForBase(Base, o, MorallyVirtual, false,
1646 true, Base == PrimaryBase && !PrimaryBaseWasVirtual,
1647 CurrentVBaseOffset, Path);
Anders Carlssondbd920c2009-10-11 22:13:54 +00001648 }
Mike Stump11dea942009-10-15 02:04:03 +00001649 Path->pop_back();
Anders Carlssondbd920c2009-10-11 22:13:54 +00001650 }
1651
Mike Stump0ca42792009-10-14 18:14:51 +00001652// #define D(X) do { X; } while (0)
1653#define D(X)
1654
1655 void insertVCalls(int InsertionPoint) {
Mike Stump6a9612f2009-10-31 20:06:59 +00001656 D1(printf("============= combining vbase/vcall\n"));
Mike Stump0ca42792009-10-14 18:14:51 +00001657 D(VCalls.insert(VCalls.begin(), 673));
1658 D(VCalls.push_back(672));
Eli Friedman152b5b12009-12-05 01:05:03 +00001659
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00001660 VtableComponents.insert(VtableComponents.begin() + InsertionPoint,
1661 VCalls.size(), 0);
Eli Friedman152b5b12009-12-05 01:05:03 +00001662 if (BuildVtable) {
1663 // The vcalls come first...
1664 for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(),
1665 e = VCalls.rend();
1666 i != e; ++i)
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00001667 VtableComponents[InsertionPoint++] = wrap((0?600:0) + *i);
Eli Friedman152b5b12009-12-05 01:05:03 +00001668 }
Mike Stump0ca42792009-10-14 18:14:51 +00001669 VCalls.clear();
Mike Stumpfbfb52d2009-11-10 02:30:51 +00001670 VCall.clear();
Mike Stump15189fb2010-01-26 21:35:27 +00001671 VCallOffsetForVCall.clear();
1672 VCallOffset.clear();
1673 NonVirtualOffset.clear();
Mike Stump0ca42792009-10-14 18:14:51 +00001674 }
1675
Mike Stump65d0e282009-11-13 23:13:20 +00001676 void AddAddressPoints(const CXXRecordDecl *RD, uint64_t Offset,
1677 Index_t AddressPoint) {
1678 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001679 RD->getNameAsCString(), MostDerivedClass->getNameAsCString(),
Mike Stump65d0e282009-11-13 23:13:20 +00001680 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stump23a35422009-11-19 20:52:19 +00001681 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Anders Carlsson1bb60992010-01-14 02:29:07 +00001682 AddressPoints[BaseSubobject(RD, Offset)] = AddressPoint;
Mike Stump65d0e282009-11-13 23:13:20 +00001683
1684 // Now also add the address point for all our primary bases.
1685 while (1) {
1686 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1687 RD = Layout.getPrimaryBase();
1688 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1689 // FIXME: Double check this.
1690 if (RD == 0)
1691 break;
1692 if (PrimaryBaseWasVirtual &&
1693 BLayout.getVBaseClassOffset(RD) != Offset)
1694 break;
1695 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001696 RD->getNameAsCString(), MostDerivedClass->getNameAsCString(),
Mike Stump65d0e282009-11-13 23:13:20 +00001697 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stump23a35422009-11-19 20:52:19 +00001698 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Anders Carlsson1bb60992010-01-14 02:29:07 +00001699 AddressPoints[BaseSubobject(RD, Offset)] = AddressPoint;
Mike Stump65d0e282009-11-13 23:13:20 +00001700 }
1701 }
1702
1703
Mike Stump8cc4f102009-12-24 07:29:41 +00001704 void FinishGenerateVtable(const CXXRecordDecl *RD,
1705 const ASTRecordLayout &Layout,
1706 const CXXRecordDecl *PrimaryBase,
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001707 bool ForNPNVBases, bool WasPrimaryBase,
Mike Stump8cc4f102009-12-24 07:29:41 +00001708 bool PrimaryBaseWasVirtual,
1709 bool MorallyVirtual, int64_t Offset,
1710 bool ForVirtualBase, int64_t CurrentVBaseOffset,
1711 Path_t *Path) {
Mike Stump11dea942009-10-15 02:04:03 +00001712 bool alloc = false;
1713 if (Path == 0) {
1714 alloc = true;
1715 Path = new Path_t;
1716 }
1717
Anders Carlssondbd920c2009-10-11 22:13:54 +00001718 StartNewTable();
1719 extra = 0;
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001720 Index_t AddressPoint = 0;
1721 int VCallInsertionPoint = 0;
1722 if (!ForNPNVBases || !WasPrimaryBase) {
1723 bool DeferVCalls = MorallyVirtual || ForVirtualBase;
1724 VCallInsertionPoint = VtableComponents.size();
1725 if (!DeferVCalls) {
1726 insertVCalls(VCallInsertionPoint);
1727 } else
1728 // FIXME: just for extra, or for all uses of VCalls.size post this?
1729 extra = -VCalls.size();
Anders Carlssondbd920c2009-10-11 22:13:54 +00001730
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001731 // Add the offset to top.
1732 VtableComponents.push_back(BuildVtable ? wrap(-((Offset-LayoutOffset)/8)) : 0);
Anders Carlsson15318f42009-12-04 16:19:30 +00001733
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001734 // Add the RTTI information.
1735 VtableComponents.push_back(rtti);
Anders Carlsson15318f42009-12-04 16:19:30 +00001736
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001737 AddressPoint = VtableComponents.size();
Anders Carlssondbd920c2009-10-11 22:13:54 +00001738
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001739 AppendMethodsToVtable();
1740 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00001741
1742 // and then the non-virtual bases.
1743 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
Mike Stump9e7e3c62009-11-06 23:27:42 +00001744 MorallyVirtual, Offset, CurrentVBaseOffset, Path);
Mike Stump0ca42792009-10-14 18:14:51 +00001745
1746 if (ForVirtualBase) {
Mike Stump9840c702009-11-12 20:47:57 +00001747 // FIXME: We're adding to VCalls in callers, we need to do the overrides
1748 // in the inner part, so that we know the complete set of vcalls during
1749 // the build and don't have to insert into methods. Saving out the
1750 // AddressPoint here, would need to be fixed, if we didn't do that. Also
1751 // retroactively adding vcalls for overrides later wind up in the wrong
1752 // place, the vcall slot has to be alloted during the walk of the base
1753 // when the function is first introduces.
Mike Stump0ca42792009-10-14 18:14:51 +00001754 AddressPoint += VCalls.size();
Mike Stump9840c702009-11-12 20:47:57 +00001755 insertVCalls(VCallInsertionPoint);
Mike Stump0ca42792009-10-14 18:14:51 +00001756 }
1757
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001758 if (!ForNPNVBases || !WasPrimaryBase)
1759 AddAddressPoints(RD, Offset, AddressPoint);
Mike Stump9840c702009-11-12 20:47:57 +00001760
Mike Stump11dea942009-10-15 02:04:03 +00001761 if (alloc) {
1762 delete Path;
1763 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00001764 }
1765
Mike Stump6a9612f2009-10-31 20:06:59 +00001766 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
1767 bool updateVBIndex, Index_t current_vbindex,
Eli Friedman76ed1f72009-11-30 01:19:33 +00001768 int64_t CurrentVBaseOffset) {
Mike Stump6a9612f2009-10-31 20:06:59 +00001769 if (!RD->isDynamicClass())
1770 return;
1771
1772 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1773 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1774 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1775
1776 // vtables are composed from the chain of primaries.
Eli Friedmandfe33bb2009-12-04 08:52:11 +00001777 if (PrimaryBase && !PrimaryBaseWasVirtual) {
Mike Stump6a9612f2009-10-31 20:06:59 +00001778 D1(printf(" doing primaries for %s most derived %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001779 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Eli Friedmandfe33bb2009-12-04 08:52:11 +00001780 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
1781 updateVBIndex, current_vbindex, CurrentVBaseOffset);
Mike Stump6a9612f2009-10-31 20:06:59 +00001782 }
1783
1784 D1(printf(" doing vcall entries for %s most derived %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001785 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Mike Stump6a9612f2009-10-31 20:06:59 +00001786
1787 // And add the virtuals for the class to the primary vtable.
Eli Friedman76ed1f72009-11-30 01:19:33 +00001788 AddMethods(RD, MorallyVirtual, Offset, CurrentVBaseOffset);
Mike Stump6a9612f2009-10-31 20:06:59 +00001789 }
1790
1791 void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
1792 bool updateVBIndex, Index_t current_vbindex,
Mike Stump9e7e3c62009-11-06 23:27:42 +00001793 bool RDisVirtualBase, int64_t CurrentVBaseOffset,
Eli Friedman76ed1f72009-11-30 01:19:33 +00001794 bool bottom) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001795 if (!RD->isDynamicClass())
1796 return;
1797
1798 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1799 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1800 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1801
1802 // vtables are composed from the chain of primaries.
1803 if (PrimaryBase) {
Mike Stump9e7e3c62009-11-06 23:27:42 +00001804 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
1805 if (PrimaryBaseWasVirtual) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001806 IndirectPrimary.insert(PrimaryBase);
Mike Stump9e7e3c62009-11-06 23:27:42 +00001807 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
1808 }
Mike Stump6a9612f2009-10-31 20:06:59 +00001809
1810 D1(printf(" doing primaries for %s most derived %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001811 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Mike Stump6a9612f2009-10-31 20:06:59 +00001812
1813 VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Mike Stump9e7e3c62009-11-06 23:27:42 +00001814 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
Eli Friedman76ed1f72009-11-30 01:19:33 +00001815 BaseCurrentVBaseOffset, false);
Anders Carlssondbd920c2009-10-11 22:13:54 +00001816 }
1817
Mike Stump6a9612f2009-10-31 20:06:59 +00001818 D1(printf(" doing vbase entries for %s most derived %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001819 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Mike Stump6a9612f2009-10-31 20:06:59 +00001820 GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex);
1821
1822 if (RDisVirtualBase || bottom) {
1823 Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex,
Eli Friedman76ed1f72009-11-30 01:19:33 +00001824 CurrentVBaseOffset);
Mike Stump6a9612f2009-10-31 20:06:59 +00001825 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00001826 }
1827
Mike Stump8cc4f102009-12-24 07:29:41 +00001828 void GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0,
1829 bool MorallyVirtual = false,
1830 bool ForVirtualBase = false,
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001831 bool ForNPNVBases = false,
1832 bool WasPrimaryBase = true,
Mike Stump8cc4f102009-12-24 07:29:41 +00001833 int CurrentVBaseOffset = 0,
1834 Path_t *Path = 0) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001835 if (!RD->isDynamicClass())
Mike Stump8cc4f102009-12-24 07:29:41 +00001836 return;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001837
Mike Stump92774d12009-11-13 02:35:38 +00001838 // Construction vtable don't need parts that have no virtual bases and
1839 // aren't morally virtual.
Anders Carlssonac3f7bd2009-12-04 18:36:22 +00001840 if ((LayoutClass != MostDerivedClass) &&
1841 RD->getNumVBases() == 0 && !MorallyVirtual)
Mike Stump8cc4f102009-12-24 07:29:41 +00001842 return;
Mike Stump92774d12009-11-13 02:35:38 +00001843
Anders Carlssondbd920c2009-10-11 22:13:54 +00001844 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1845 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1846 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1847
Anders Carlssondbd920c2009-10-11 22:13:54 +00001848 extra = 0;
Mike Stump6a9612f2009-10-31 20:06:59 +00001849 D1(printf("building entries for base %s most derived %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001850 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Anders Carlssondbd920c2009-10-11 22:13:54 +00001851
Mike Stump6a9612f2009-10-31 20:06:59 +00001852 if (ForVirtualBase)
1853 extra = VCalls.size();
1854
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001855 if (!ForNPNVBases || !WasPrimaryBase) {
1856 VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0,
1857 ForVirtualBase, CurrentVBaseOffset, true);
Anders Carlssondbd920c2009-10-11 22:13:54 +00001858
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001859 if (Path)
1860 OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset);
1861 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00001862
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001863 FinishGenerateVtable(RD, Layout, PrimaryBase, ForNPNVBases, WasPrimaryBase,
1864 PrimaryBaseWasVirtual, MorallyVirtual, Offset,
1865 ForVirtualBase, CurrentVBaseOffset, Path);
Anders Carlssondbd920c2009-10-11 22:13:54 +00001866 }
1867
1868 void GenerateVtableForVBases(const CXXRecordDecl *RD,
1869 int64_t Offset = 0,
Mike Stump11dea942009-10-15 02:04:03 +00001870 Path_t *Path = 0) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001871 bool alloc = false;
1872 if (Path == 0) {
1873 alloc = true;
Mike Stump11dea942009-10-15 02:04:03 +00001874 Path = new Path_t;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001875 }
1876 // FIXME: We also need to override using all paths to a virtual base,
1877 // right now, we just process the first path
1878 Path->push_back(std::make_pair(RD, Offset));
1879 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1880 e = RD->bases_end(); i != e; ++i) {
1881 const CXXRecordDecl *Base =
1882 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1883 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
1884 // Mark it so we don't output it twice.
1885 IndirectPrimary.insert(Base);
1886 StartNewTable();
Mike Stump0ca42792009-10-14 18:14:51 +00001887 VCall.clear();
Anders Carlssondbd920c2009-10-11 22:13:54 +00001888 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump9e7e3c62009-11-06 23:27:42 +00001889 int64_t CurrentVBaseOffset = BaseOffset;
Mike Stump6a9612f2009-10-31 20:06:59 +00001890 D1(printf("vtable %s virtual base %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001891 MostDerivedClass->getNameAsCString(), Base->getNameAsCString()));
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001892 GenerateVtableForBase(Base, BaseOffset, true, true, false,
1893 true, CurrentVBaseOffset, Path);
Anders Carlssondbd920c2009-10-11 22:13:54 +00001894 }
Mike Stump9840c702009-11-12 20:47:57 +00001895 int64_t BaseOffset;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001896 if (i->isVirtual())
1897 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump9840c702009-11-12 20:47:57 +00001898 else {
1899 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1900 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
1901 }
1902
Mike Stump11dea942009-10-15 02:04:03 +00001903 if (Base->getNumVBases()) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001904 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump11dea942009-10-15 02:04:03 +00001905 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00001906 }
1907 Path->pop_back();
1908 if (alloc)
1909 delete Path;
1910 }
1911};
Anders Carlsson27682a32009-12-03 01:54:02 +00001912} // end anonymous namespace
1913
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001914bool OldVtableBuilder::OverrideMethod(GlobalDecl GD, bool MorallyVirtual,
Eli Friedman367d1222009-12-04 08:40:51 +00001915 Index_t OverrideOffset, Index_t Offset,
1916 int64_t CurrentVBaseOffset) {
Anders Carlsson27682a32009-12-03 01:54:02 +00001917 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1918
1919 const bool isPure = MD->isPure();
Anders Carlsson7ca46432009-12-05 17:04:47 +00001920
Anders Carlsson27682a32009-12-03 01:54:02 +00001921 // FIXME: Should OverrideOffset's be Offset?
1922
Anders Carlsson7ca46432009-12-05 17:04:47 +00001923 for (CXXMethodDecl::method_iterator mi = MD->begin_overridden_methods(),
1924 e = MD->end_overridden_methods(); mi != e; ++mi) {
Anders Carlsson27682a32009-12-03 01:54:02 +00001925 GlobalDecl OGD;
Mike Stump15189fb2010-01-26 21:35:27 +00001926 GlobalDecl OGD2;
Anders Carlsson27682a32009-12-03 01:54:02 +00001927
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001928 const CXXMethodDecl *OMD = *mi;
Anders Carlsson27682a32009-12-03 01:54:02 +00001929 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(OMD))
1930 OGD = GlobalDecl(DD, GD.getDtorType());
1931 else
1932 OGD = OMD;
Eli Friedman47145832009-12-04 08:34:14 +00001933
Eli Friedman72649ed2009-12-06 22:01:30 +00001934 // Check whether this is the method being overridden in this section of
1935 // the vtable.
Anders Carlsson77c23e52009-12-04 15:49:02 +00001936 uint64_t Index;
1937 if (!Methods.getIndex(OGD, Index))
Eli Friedman47145832009-12-04 08:34:14 +00001938 continue;
1939
Mike Stump15189fb2010-01-26 21:35:27 +00001940 OGD2 = OGD;
1941
Eli Friedman72649ed2009-12-06 22:01:30 +00001942 // Get the original method, which we should be computing thunks, etc,
1943 // against.
1944 OGD = Methods.getOrigMethod(Index);
1945 OMD = cast<CXXMethodDecl>(OGD.getDecl());
1946
Eli Friedman47145832009-12-04 08:34:14 +00001947 QualType ReturnType =
1948 MD->getType()->getAs<FunctionType>()->getResultType();
1949 QualType OverriddenReturnType =
1950 OMD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlsson27682a32009-12-03 01:54:02 +00001951
Eli Friedman47145832009-12-04 08:34:14 +00001952 // Check if we need a return type adjustment.
1953 if (TypeConversionRequiresAdjustment(CGM.getContext(), ReturnType,
1954 OverriddenReturnType)) {
1955 CanQualType &BaseReturnType = BaseReturnTypes[Index];
Anders Carlsson27682a32009-12-03 01:54:02 +00001956
Eli Friedman47145832009-12-04 08:34:14 +00001957 // Insert the base return type.
1958 if (BaseReturnType.isNull())
1959 BaseReturnType =
1960 CGM.getContext().getCanonicalType(OverriddenReturnType);
1961 }
Anders Carlssondd454be2009-12-04 03:52:52 +00001962
Eli Friedman47145832009-12-04 08:34:14 +00001963 Methods.OverrideMethod(OGD, GD);
1964
Anders Carlsson0f0bbbc2010-01-26 17:36:47 +00001965 GlobalDecl UGD = getUnique(GD);
1966 const CXXMethodDecl *UMD = cast<CXXMethodDecl>(UGD.getDecl());
1967 assert(UGD.getDecl() && "unique overrider not found");
1968 assert(UGD == getUnique(OGD) && "unique overrider not unique");
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001969
Eli Friedman47145832009-12-04 08:34:14 +00001970 ThisAdjustments.erase(Index);
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001971 if (MorallyVirtual || VCall.count(UMD)) {
Mike Stump15189fb2010-01-26 21:35:27 +00001972
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001973 Index_t &idx = VCall[UMD];
Eli Friedman47145832009-12-04 08:34:14 +00001974 if (idx == 0) {
Mike Stump15189fb2010-01-26 21:35:27 +00001975 VCallOffset[GD] = VCallOffset[OGD];
1976 // NonVirtualOffset[UMD] = CurrentVBaseOffset/8 - OverrideOffset/8;
1977 NonVirtualOffset[UMD] = VCallOffset[OGD];
Mike Stumpd99a4d22010-01-26 03:42:22 +00001978 VCallOffsetForVCall[UMD] = OverrideOffset/8;
Eli Friedman47145832009-12-04 08:34:14 +00001979 idx = VCalls.size()+1;
Mike Stumpd99a4d22010-01-26 03:42:22 +00001980 VCalls.push_back(OverrideOffset/8 - CurrentVBaseOffset/8);
Eli Friedman47145832009-12-04 08:34:14 +00001981 D1(printf(" vcall for %s at %d with delta %d most derived %s\n",
1982 MD->getNameAsString().c_str(), (int)-idx-3,
Mike Stumpd6584902010-01-22 02:51:26 +00001983 (int)VCalls[idx-1], MostDerivedClass->getNameAsCString()));
Eli Friedman47145832009-12-04 08:34:14 +00001984 } else {
Mike Stump15189fb2010-01-26 21:35:27 +00001985 VCallOffset[GD] = NonVirtualOffset[UMD];
Anders Carlsson0f0bbbc2010-01-26 17:36:47 +00001986 VCalls[idx-1] = -VCallOffsetForVCall[UGD] + OverrideOffset/8;
Eli Friedman47145832009-12-04 08:34:14 +00001987 D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n",
1988 MD->getNameAsString().c_str(), (int)-idx-3,
Mike Stumpd6584902010-01-22 02:51:26 +00001989 (int)VCalls[idx-1], MostDerivedClass->getNameAsCString()));
Eli Friedman47145832009-12-04 08:34:14 +00001990 }
Mike Stump15189fb2010-01-26 21:35:27 +00001991 int64_t NonVirtualAdjustment = -VCallOffset[OGD];
Mike Stump852f5ce2010-01-26 22:44:01 +00001992 QualType DerivedType = MD->getThisType(CGM.getContext());
1993 QualType BaseType = cast<const CXXMethodDecl>(OGD.getDecl())->getThisType(CGM.getContext());
1994 int64_t NonVirtualAdjustment2 = -(getNVOffset(BaseType, DerivedType)/8);
1995 if (NonVirtualAdjustment2 != NonVirtualAdjustment) {
1996 NonVirtualAdjustment = NonVirtualAdjustment2;
1997 }
Eli Friedman47145832009-12-04 08:34:14 +00001998 int64_t VirtualAdjustment =
1999 -((idx + extra + 2) * LLVMPointerWidth / 8);
Anders Carlsson891bb4b2009-12-03 02:32:59 +00002000
Eli Friedman47145832009-12-04 08:34:14 +00002001 // Optimize out virtual adjustments of 0.
2002 if (VCalls[idx-1] == 0)
2003 VirtualAdjustment = 0;
Anders Carlsson891bb4b2009-12-03 02:32:59 +00002004
Eli Friedman47145832009-12-04 08:34:14 +00002005 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment,
2006 VirtualAdjustment);
Anders Carlssonbc0e3392009-12-03 02:22:59 +00002007
Eli Friedman72649ed2009-12-06 22:01:30 +00002008 if (!isPure && !ThisAdjustment.isEmpty()) {
Eli Friedman47145832009-12-04 08:34:14 +00002009 ThisAdjustments[Index] = ThisAdjustment;
Eli Friedmanb455f0e2009-12-07 23:56:34 +00002010 SavedAdjustments.push_back(
2011 std::make_pair(GD, std::make_pair(OGD, ThisAdjustment)));
Eli Friedman72649ed2009-12-06 22:01:30 +00002012 }
Anders Carlsson27682a32009-12-03 01:54:02 +00002013 return true;
2014 }
Eli Friedman47145832009-12-04 08:34:14 +00002015
Mike Stump15189fb2010-01-26 21:35:27 +00002016 VCallOffset[GD] = VCallOffset[OGD2] - OverrideOffset/8;
Eli Friedman47145832009-12-04 08:34:14 +00002017
Mike Stump15189fb2010-01-26 21:35:27 +00002018 int64_t NonVirtualAdjustment = -VCallOffset[GD];
2019 QualType DerivedType = MD->getThisType(CGM.getContext());
2020 QualType BaseType = cast<const CXXMethodDecl>(OGD.getDecl())->getThisType(CGM.getContext());
2021 int64_t NonVirtualAdjustment2 = -(getNVOffset(BaseType, DerivedType)/8);
2022 if (NonVirtualAdjustment2 != NonVirtualAdjustment) {
2023 NonVirtualAdjustment = NonVirtualAdjustment2;
2024 }
2025
Eli Friedman47145832009-12-04 08:34:14 +00002026 if (NonVirtualAdjustment) {
2027 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, 0);
2028
Eli Friedmanf062d9d2009-12-06 22:33:51 +00002029 if (!isPure) {
Eli Friedman47145832009-12-04 08:34:14 +00002030 ThisAdjustments[Index] = ThisAdjustment;
Eli Friedmanb455f0e2009-12-07 23:56:34 +00002031 SavedAdjustments.push_back(
2032 std::make_pair(GD, std::make_pair(OGD, ThisAdjustment)));
Eli Friedmanf062d9d2009-12-06 22:33:51 +00002033 }
Eli Friedman47145832009-12-04 08:34:14 +00002034 }
2035 return true;
Anders Carlsson27682a32009-12-03 01:54:02 +00002036 }
2037
2038 return false;
Anders Carlsson27f69d02009-11-27 22:21:51 +00002039}
2040
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002041void OldVtableBuilder::AppendMethodsToVtable() {
Eli Friedman152b5b12009-12-05 01:05:03 +00002042 if (!BuildVtable) {
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00002043 VtableComponents.insert(VtableComponents.end(), Methods.size(),
2044 (llvm::Constant *)0);
Eli Friedman152b5b12009-12-05 01:05:03 +00002045 ThisAdjustments.clear();
2046 BaseReturnTypes.clear();
2047 Methods.clear();
2048 return;
2049 }
2050
Anders Carlsson15318f42009-12-04 16:19:30 +00002051 // Reserve room in the vtable for our new methods.
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00002052 VtableComponents.reserve(VtableComponents.size() + Methods.size());
Anders Carlsson29202d52009-12-04 03:07:26 +00002053
Anders Carlssonb73ba392009-12-04 02:43:50 +00002054 for (unsigned i = 0, e = Methods.size(); i != e; ++i) {
2055 GlobalDecl GD = Methods[i];
Anders Carlssonea357222009-12-04 02:52:22 +00002056 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2057
2058 // Get the 'this' pointer adjustment.
Anders Carlssonb73ba392009-12-04 02:43:50 +00002059 ThunkAdjustment ThisAdjustment = ThisAdjustments.lookup(i);
Anders Carlssonea357222009-12-04 02:52:22 +00002060
2061 // Construct the return type adjustment.
2062 ThunkAdjustment ReturnAdjustment;
2063
2064 QualType BaseReturnType = BaseReturnTypes.lookup(i);
2065 if (!BaseReturnType.isNull() && !MD->isPure()) {
2066 QualType DerivedType =
2067 MD->getType()->getAs<FunctionType>()->getResultType();
2068
2069 int64_t NonVirtualAdjustment =
2070 getNVOffset(BaseReturnType, DerivedType) / 8;
2071
2072 int64_t VirtualAdjustment =
2073 getVbaseOffset(BaseReturnType, DerivedType);
2074
2075 ReturnAdjustment = ThunkAdjustment(NonVirtualAdjustment,
2076 VirtualAdjustment);
2077 }
2078
Anders Carlsson2fce2162009-12-04 03:06:03 +00002079 llvm::Constant *Method = 0;
Anders Carlssonea357222009-12-04 02:52:22 +00002080 if (!ReturnAdjustment.isEmpty()) {
2081 // Build a covariant thunk.
2082 CovariantThunkAdjustment Adjustment(ThisAdjustment, ReturnAdjustment);
Eli Friedman72649ed2009-12-06 22:01:30 +00002083 Method = wrap(CGM.GetAddrOfCovariantThunk(GD, Adjustment));
Anders Carlssonea357222009-12-04 02:52:22 +00002084 } else if (!ThisAdjustment.isEmpty()) {
2085 // Build a "regular" thunk.
Eli Friedman72649ed2009-12-06 22:01:30 +00002086 Method = wrap(CGM.GetAddrOfThunk(GD, ThisAdjustment));
Anders Carlssonbf540272009-12-04 02:56:03 +00002087 } else if (MD->isPure()) {
2088 // We have a pure virtual method.
Anders Carlsson2fce2162009-12-04 03:06:03 +00002089 Method = getPureVirtualFn();
2090 } else {
2091 // We have a good old regular method.
2092 Method = WrapAddrOf(GD);
Anders Carlssonea357222009-12-04 02:52:22 +00002093 }
Anders Carlsson2fce2162009-12-04 03:06:03 +00002094
2095 // Add the method to the vtable.
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00002096 VtableComponents.push_back(Method);
Anders Carlssonb73ba392009-12-04 02:43:50 +00002097 }
2098
Anders Carlsson2fce2162009-12-04 03:06:03 +00002099
Anders Carlssonb73ba392009-12-04 02:43:50 +00002100 ThisAdjustments.clear();
Anders Carlssonea357222009-12-04 02:52:22 +00002101 BaseReturnTypes.clear();
Anders Carlssonb73ba392009-12-04 02:43:50 +00002102
Anders Carlssonadfa2672009-12-04 02:39:04 +00002103 Methods.clear();
Anders Carlssonadfa2672009-12-04 02:39:04 +00002104}
2105
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002106void CGVtableInfo::ComputeMethodVtableIndices(const CXXRecordDecl *RD) {
2107
2108 // Itanium C++ ABI 2.5.2:
Anders Carlsson45147d02010-02-02 03:37:46 +00002109 // The order of the virtual function pointers in a virtual table is the
2110 // order of declaration of the corresponding member functions in the class.
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002111 //
Anders Carlsson45147d02010-02-02 03:37:46 +00002112 // There is an entry for any virtual function declared in a class,
2113 // whether it is a new function or overrides a base class function,
2114 // unless it overrides a function from the primary base, and conversion
2115 // between their return types does not require an adjustment.
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002116
2117 int64_t CurrentIndex = 0;
2118
2119 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
2120 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
2121
2122 if (PrimaryBase) {
Anders Carlsson0121fbd2009-11-30 19:43:26 +00002123 assert(PrimaryBase->isDefinition() &&
2124 "Should have the definition decl of the primary base!");
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002125
2126 // Since the record decl shares its vtable pointer with the primary base
2127 // we need to start counting at the end of the primary base's vtable.
2128 CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase);
2129 }
Eli Friedmanea5ae312009-12-15 03:31:17 +00002130
2131 // Collect all the primary bases, so we can check whether methods override
2132 // a method from the base.
Anders Carlsson57071e22010-02-12 05:25:12 +00002133 VtableBuilder::PrimaryBasesSetTy PrimaryBases;
Eli Friedmanea5ae312009-12-15 03:31:17 +00002134 for (ASTRecordLayout::primary_base_info_iterator
2135 I = Layout.primary_base_begin(), E = Layout.primary_base_end();
2136 I != E; ++I)
2137 PrimaryBases.insert((*I).getBase());
2138
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002139 const CXXDestructorDecl *ImplicitVirtualDtor = 0;
2140
2141 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
2142 e = RD->method_end(); i != e; ++i) {
2143 const CXXMethodDecl *MD = *i;
2144
2145 // We only want virtual methods.
2146 if (!MD->isVirtual())
2147 continue;
2148
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002149 // Check if this method overrides a method in the primary base.
Anders Carlsson57071e22010-02-12 05:25:12 +00002150 if (const CXXMethodDecl *OverriddenMD =
2151 OverridesMethodInPrimaryBase(MD, PrimaryBases)) {
2152 // Check if converting from the return type of the method to the
2153 // return type of the overridden method requires conversion.
Anders Carlsson104f45c2010-02-12 17:37:14 +00002154 if (!ReturnTypeConversionRequiresAdjustment(MD, OverriddenMD)) {
Anders Carlsson57071e22010-02-12 05:25:12 +00002155 // This index is shared between the index in the vtable of the primary
2156 // base class.
2157 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2158 const CXXDestructorDecl *OverriddenDD =
2159 cast<CXXDestructorDecl>(OverriddenMD);
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002160
Anders Carlsson57071e22010-02-12 05:25:12 +00002161 // Add both the complete and deleting entries.
2162 MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] =
2163 getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Complete));
2164 MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] =
2165 getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting));
2166 } else {
2167 MethodVtableIndices[MD] = getMethodVtableIndex(OverriddenMD);
2168 }
2169
2170 // We don't need to add an entry for this method.
Anders Carlssonc1eec892010-02-12 18:14:46 +00002171 continue;
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002172 }
2173 }
2174
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002175 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2176 if (MD->isImplicit()) {
2177 assert(!ImplicitVirtualDtor &&
2178 "Did already see an implicit virtual dtor!");
2179 ImplicitVirtualDtor = DD;
2180 continue;
2181 }
2182
2183 // Add the complete dtor.
2184 MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++;
2185
2186 // Add the deleting dtor.
2187 MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++;
2188 } else {
2189 // Add the entry.
2190 MethodVtableIndices[MD] = CurrentIndex++;
2191 }
2192 }
2193
2194 if (ImplicitVirtualDtor) {
2195 // Itanium C++ ABI 2.5.2:
2196 // If a class has an implicitly-defined virtual destructor,
2197 // its entries come after the declared virtual function pointers.
2198
2199 // Add the complete dtor.
2200 MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] =
2201 CurrentIndex++;
2202
2203 // Add the deleting dtor.
2204 MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] =
2205 CurrentIndex++;
2206 }
2207
2208 NumVirtualFunctionPointers[RD] = CurrentIndex;
2209}
2210
2211uint64_t CGVtableInfo::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) {
2212 llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
2213 NumVirtualFunctionPointers.find(RD);
2214 if (I != NumVirtualFunctionPointers.end())
2215 return I->second;
2216
2217 ComputeMethodVtableIndices(RD);
2218
2219 I = NumVirtualFunctionPointers.find(RD);
2220 assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!");
2221 return I->second;
2222}
2223
2224uint64_t CGVtableInfo::getMethodVtableIndex(GlobalDecl GD) {
Anders Carlssona0fdd912009-11-13 17:08:56 +00002225 MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD);
Anders Carlssondbd920c2009-10-11 22:13:54 +00002226 if (I != MethodVtableIndices.end())
2227 return I->second;
2228
Anders Carlssona0fdd912009-11-13 17:08:56 +00002229 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002230
2231 ComputeMethodVtableIndices(RD);
2232
Anders Carlssona0fdd912009-11-13 17:08:56 +00002233 I = MethodVtableIndices.find(GD);
Anders Carlssondbd920c2009-10-11 22:13:54 +00002234 assert(I != MethodVtableIndices.end() && "Did not find index!");
2235 return I->second;
2236}
2237
Eli Friedmanb455f0e2009-12-07 23:56:34 +00002238CGVtableInfo::AdjustmentVectorTy*
2239CGVtableInfo::getAdjustments(GlobalDecl GD) {
2240 SavedAdjustmentsTy::iterator I = SavedAdjustments.find(GD);
2241 if (I != SavedAdjustments.end())
2242 return &I->second;
Eli Friedman72649ed2009-12-06 22:01:30 +00002243
2244 const CXXRecordDecl *RD = cast<CXXRecordDecl>(GD.getDecl()->getDeclContext());
Eli Friedmanb455f0e2009-12-07 23:56:34 +00002245 if (!SavedAdjustmentRecords.insert(RD).second)
2246 return 0;
Eli Friedman72649ed2009-12-06 22:01:30 +00002247
Anders Carlsson1bb60992010-01-14 02:29:07 +00002248 AddressPointsMapTy AddressPoints;
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002249 OldVtableBuilder b(RD, RD, 0, CGM, false, AddressPoints);
Eli Friedman72649ed2009-12-06 22:01:30 +00002250 D1(printf("vtable %s\n", RD->getNameAsCString()));
2251 b.GenerateVtableForBase(RD);
2252 b.GenerateVtableForVBases(RD);
Eli Friedman72649ed2009-12-06 22:01:30 +00002253
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002254 for (OldVtableBuilder::SavedAdjustmentsVectorTy::iterator
Eli Friedmanb455f0e2009-12-07 23:56:34 +00002255 i = b.getSavedAdjustments().begin(),
2256 e = b.getSavedAdjustments().end(); i != e; i++)
2257 SavedAdjustments[i->first].push_back(i->second);
Eli Friedman72649ed2009-12-06 22:01:30 +00002258
Eli Friedmanb455f0e2009-12-07 23:56:34 +00002259 I = SavedAdjustments.find(GD);
2260 if (I != SavedAdjustments.end())
2261 return &I->second;
2262
2263 return 0;
Eli Friedman72649ed2009-12-06 22:01:30 +00002264}
2265
Anders Carlssondbd920c2009-10-11 22:13:54 +00002266int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
2267 const CXXRecordDecl *VBase) {
2268 ClassPairTy ClassPair(RD, VBase);
2269
2270 VirtualBaseClassIndiciesTy::iterator I =
2271 VirtualBaseClassIndicies.find(ClassPair);
2272 if (I != VirtualBaseClassIndicies.end())
2273 return I->second;
2274
Anders Carlssondbd920c2009-10-11 22:13:54 +00002275 // FIXME: This seems expensive. Can we do a partial job to get
2276 // just this data.
Anders Carlsson1bb60992010-01-14 02:29:07 +00002277 AddressPointsMapTy AddressPoints;
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002278 OldVtableBuilder b(RD, RD, 0, CGM, false, AddressPoints);
Mike Stump6a9612f2009-10-31 20:06:59 +00002279 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlssondbd920c2009-10-11 22:13:54 +00002280 b.GenerateVtableForBase(RD);
2281 b.GenerateVtableForVBases(RD);
2282
2283 for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
2284 b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) {
2285 // Insert all types.
2286 ClassPairTy ClassPair(RD, I->first);
2287
2288 VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second));
2289 }
2290
2291 I = VirtualBaseClassIndicies.find(ClassPair);
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00002292 // FIXME: The assertion below assertion currently fails with the old vtable
2293 /// layout code if there is a non-virtual thunk adjustment in a vtable.
2294 // Once the new layout is in place, this return should be removed.
2295 if (I == VirtualBaseClassIndicies.end())
2296 return 0;
2297
Anders Carlssondbd920c2009-10-11 22:13:54 +00002298 assert(I != VirtualBaseClassIndicies.end() && "Did not find index!");
2299
2300 return I->second;
2301}
2302
Anders Carlsson9ac95b92009-12-05 21:03:56 +00002303uint64_t CGVtableInfo::getVtableAddressPoint(const CXXRecordDecl *RD) {
2304 uint64_t AddressPoint =
Anders Carlsson21431c52010-01-02 18:02:32 +00002305 (*(*(CGM.getVtableInfo().AddressPoints[RD]))[RD])[std::make_pair(RD, 0)];
Anders Carlsson9ac95b92009-12-05 21:03:56 +00002306
2307 return AddressPoint;
2308}
2309
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00002310llvm::GlobalVariable *
Anders Carlsson35272252009-12-06 00:23:49 +00002311CGVtableInfo::GenerateVtable(llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlsson5794c972009-12-06 00:53:22 +00002312 bool GenerateDefinition,
Anders Carlsson35272252009-12-06 00:23:49 +00002313 const CXXRecordDecl *LayoutClass,
Anders Carlsson1bb60992010-01-14 02:29:07 +00002314 const CXXRecordDecl *RD, uint64_t Offset,
2315 AddressPointsMapTy& AddressPoints) {
Anders Carlsson0c0eeb22010-02-13 02:02:03 +00002316 if (GenerateDefinition && CGM.getLangOptions().DumpVtableLayouts) {
Anders Carlsson60db0ee2010-02-13 21:07:32 +00002317 VtableBuilder Builder(*this, RD);
Anders Carlsson0c0eeb22010-02-13 02:02:03 +00002318
2319 Builder.dumpLayout(llvm::errs());
2320 }
2321
Anders Carlssondbd920c2009-10-11 22:13:54 +00002322 llvm::SmallString<256> OutName;
Mike Stump9840c702009-11-12 20:47:57 +00002323 if (LayoutClass != RD)
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00002324 CGM.getMangleContext().mangleCXXCtorVtable(LayoutClass, Offset / 8,
2325 RD, OutName);
Mike Stump8cfcb522009-11-11 20:26:26 +00002326 else
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00002327 CGM.getMangleContext().mangleCXXVtable(RD, OutName);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002328 llvm::StringRef Name = OutName.str();
Benjamin Kramer7a9474e2009-10-11 22:57:54 +00002329
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00002330 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
Anders Carlsson21431c52010-01-02 18:02:32 +00002331 if (GV == 0 || CGM.getVtableInfo().AddressPoints[LayoutClass] == 0 ||
2332 GV->isDeclaration()) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002333 OldVtableBuilder b(RD, LayoutClass, Offset, CGM, GenerateDefinition,
2334 AddressPoints);
Eli Friedman152b5b12009-12-05 01:05:03 +00002335
2336 D1(printf("vtable %s\n", RD->getNameAsCString()));
2337 // First comes the vtables for all the non-virtual bases...
Mike Stump8cc4f102009-12-24 07:29:41 +00002338 b.GenerateVtableForBase(RD, Offset);
Eli Friedman152b5b12009-12-05 01:05:03 +00002339
2340 // then the vtables for all the virtual bases.
2341 b.GenerateVtableForVBases(RD, Offset);
2342
Anders Carlsson152d4dc2009-12-05 22:19:10 +00002343 llvm::Constant *Init = 0;
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00002344 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
Anders Carlsson152d4dc2009-12-05 22:19:10 +00002345 llvm::ArrayType *ArrayType =
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00002346 llvm::ArrayType::get(Int8PtrTy, b.getVtableComponents().size());
Anders Carlssone40477c2009-12-05 21:09:05 +00002347
Anders Carlsson5794c972009-12-06 00:53:22 +00002348 if (GenerateDefinition)
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00002349 Init = llvm::ConstantArray::get(ArrayType, &b.getVtableComponents()[0],
2350 b.getVtableComponents().size());
Anders Carlsson5794c972009-12-06 00:53:22 +00002351
Mike Stump85615df2009-11-19 04:04:36 +00002352 llvm::GlobalVariable *OGV = GV;
Anders Carlsson5794c972009-12-06 00:53:22 +00002353
2354 GV = new llvm::GlobalVariable(CGM.getModule(), ArrayType,
2355 /*isConstant=*/true, Linkage, Init, Name);
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00002356 CGM.setGlobalVisibility(GV, RD);
2357
Mike Stump85615df2009-11-19 04:04:36 +00002358 if (OGV) {
2359 GV->takeName(OGV);
Anders Carlsson152d4dc2009-12-05 22:19:10 +00002360 llvm::Constant *NewPtr =
2361 llvm::ConstantExpr::getBitCast(GV, OGV->getType());
Mike Stump85615df2009-11-19 04:04:36 +00002362 OGV->replaceAllUsesWith(NewPtr);
2363 OGV->eraseFromParent();
2364 }
Mike Stump85615df2009-11-19 04:04:36 +00002365 }
Anders Carlssonbb27d862009-12-05 21:28:12 +00002366
2367 return GV;
Anders Carlssondbd920c2009-10-11 22:13:54 +00002368}
Mike Stumpfbfb52d2009-11-10 02:30:51 +00002369
Anders Carlsson5794c972009-12-06 00:53:22 +00002370void CGVtableInfo::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
2371 const CXXRecordDecl *RD) {
Anders Carlssond58987c2009-12-07 07:59:52 +00002372 llvm::GlobalVariable *&Vtable = Vtables[RD];
2373 if (Vtable) {
2374 assert(Vtable->getInitializer() && "Vtable doesn't have a definition!");
2375 return;
2376 }
2377
Anders Carlsson1bb60992010-01-14 02:29:07 +00002378 AddressPointsMapTy AddressPoints;
2379 Vtable = GenerateVtable(Linkage, /*GenerateDefinition=*/true, RD, RD, 0,
2380 AddressPoints);
Anders Carlssonc997d422010-01-02 01:01:18 +00002381 GenerateVTT(Linkage, /*GenerateDefinition=*/true, RD);
Mike Stump58588942009-11-19 01:08:19 +00002382}
2383
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00002384llvm::GlobalVariable *CGVtableInfo::getVtable(const CXXRecordDecl *RD) {
Anders Carlsson5794c972009-12-06 00:53:22 +00002385 llvm::GlobalVariable *Vtable = Vtables.lookup(RD);
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00002386
Anders Carlsson1bb60992010-01-14 02:29:07 +00002387 if (!Vtable) {
2388 AddressPointsMapTy AddressPoints;
Anders Carlsson5794c972009-12-06 00:53:22 +00002389 Vtable = GenerateVtable(llvm::GlobalValue::ExternalLinkage,
Anders Carlsson1bb60992010-01-14 02:29:07 +00002390 /*GenerateDefinition=*/false, RD, RD, 0,
2391 AddressPoints);
2392 }
Mike Stump85615df2009-11-19 04:04:36 +00002393
Anders Carlsson224c3122009-12-05 22:42:54 +00002394 return Vtable;
Mike Stump380dd752009-11-10 07:44:33 +00002395}
Mike Stump8cfcb522009-11-11 20:26:26 +00002396
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002397void CGVtableInfo::MaybeEmitVtable(GlobalDecl GD) {
2398 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2399 const CXXRecordDecl *RD = MD->getParent();
2400
Anders Carlsson224c3122009-12-05 22:42:54 +00002401 // If the class doesn't have a vtable we don't need to emit one.
2402 if (!RD->isDynamicClass())
2403 return;
2404
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002405 // Get the key function.
Anders Carlssonf53df232009-12-07 04:35:11 +00002406 const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD);
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002407
Anders Carlsson224c3122009-12-05 22:42:54 +00002408 if (KeyFunction) {
2409 // We don't have the right key function.
2410 if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl())
2411 return;
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002412 }
2413
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002414 // Emit the data.
Douglas Gregor4b0f21c2010-01-06 20:27:16 +00002415 GenerateClassData(CGM.getVtableLinkage(RD), RD);
Eli Friedman72649ed2009-12-06 22:01:30 +00002416
2417 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
2418 e = RD->method_end(); i != e; ++i) {
Eli Friedmanb455f0e2009-12-07 23:56:34 +00002419 if ((*i)->isVirtual() && ((*i)->hasInlineBody() || (*i)->isImplicit())) {
Eli Friedman72649ed2009-12-06 22:01:30 +00002420 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(*i)) {
2421 CGM.BuildThunksForVirtual(GlobalDecl(DD, Dtor_Complete));
2422 CGM.BuildThunksForVirtual(GlobalDecl(DD, Dtor_Deleting));
2423 } else {
2424 CGM.BuildThunksForVirtual(GlobalDecl(*i));
2425 }
2426 }
2427 }
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002428}
2429