blob: eb8b30d4a30f77685b5d847f007df3217a046116 [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,
Anders Carlsson74c364e2010-02-13 22:23:31 +0000222 BaseSubobject NewBase,
Anders Carlssonb8358d82010-02-12 01:40:03 +0000223 const CXXMethodDecl *NewMD,
224 SubobjectOffsetsMapTy &Offsets);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000225
Anders Carlssonb8358d82010-02-12 01:40:03 +0000226 static void MergeSubobjectOffsets(const SubobjectOffsetsMapTy &NewOffsets,
227 SubobjectOffsetsMapTy &Offsets);
228
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000229public:
230 explicit FinalOverriders(const CXXRecordDecl *MostDerivedClass);
231
232 /// getOverrider - Get the final overrider for the given method declaration in
233 /// the given base subobject.
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000234 OverriderInfo getOverrider(BaseSubobject Base,
235 const CXXMethodDecl *MD) const {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000236 assert(OverridersMap.count(std::make_pair(Base, MD)) &&
237 "Did not find overrider!");
238
239 return OverridersMap.lookup(std::make_pair(Base, MD));
240 }
241
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000242 BaseOffset getReturnAdjustmentOffset(BaseSubobject Base,
243 const CXXMethodDecl *MD) const {
244 return ReturnAdjustments.lookup(std::make_pair(Base, MD));
245 }
246
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000247 /// dump - dump the final overriders.
248 void dump() const {
249 dump(llvm::errs(), BaseSubobject(MostDerivedClass, 0));
250 }
251
252 /// dump - dump the final overriders for a base subobject, and all its direct
253 /// and indirect base subobjects.
254 void dump(llvm::raw_ostream &Out, BaseSubobject Base) const;
255};
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000256
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000257FinalOverriders::FinalOverriders(const CXXRecordDecl *MostDerivedClass)
258 : MostDerivedClass(MostDerivedClass),
259 Context(MostDerivedClass->getASTContext()),
260 MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)) {
261
262 // Compute the final overriders.
Anders Carlssonb8358d82010-02-12 01:40:03 +0000263 SubobjectOffsetsMapTy Offsets;
264 ComputeFinalOverriders(BaseSubobject(MostDerivedClass, 0), Offsets);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000265
266 // And dump them (for now).
267 dump();
Anders Carlssona4699882010-02-13 21:33:22 +0000268
269 // Also dump the base offsets (for now).
270 for (SubobjectOffsetsMapTy::const_iterator I = Offsets.begin(),
271 E = Offsets.end(); I != E; ++I) {
272 const OffsetVectorTy& OffsetVector = I->second;
273
274 llvm::errs() << "Base offsets for ";
275 llvm::errs() << I->first->getQualifiedNameAsString() << '\n';
276
277 for (unsigned I = 0, E = OffsetVector.size(); I != E; ++I)
278 llvm::errs() << " " << I << " - " << OffsetVector[I] << '\n';
279 }
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000280}
281
Anders Carlssonb8358d82010-02-12 01:40:03 +0000282void FinalOverriders::AddOverriders(BaseSubobject Base,
283 SubobjectOffsetsMapTy &Offsets) {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000284 const CXXRecordDecl *RD = Base.getBase();
285
286 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
287 E = RD->method_end(); I != E; ++I) {
288 const CXXMethodDecl *MD = *I;
289
290 if (!MD->isVirtual())
291 continue;
292
Anders Carlssonb8358d82010-02-12 01:40:03 +0000293 // First, propagate the overrider.
Anders Carlsson74c364e2010-02-13 22:23:31 +0000294 PropagateOverrider(MD, Base, MD, Offsets);
Anders Carlssonb8358d82010-02-12 01:40:03 +0000295
296 // Add the overrider as the final overrider of itself.
Anders Carlssona7575822010-02-12 16:55:34 +0000297 OverriderInfo& Overrider = OverridersMap[std::make_pair(Base, MD)];
298 assert(!Overrider.Method && "Overrider should not exist yet!");
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000299
Anders Carlssona7575822010-02-12 16:55:34 +0000300 Overrider.Method = MD;
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000301 }
302}
303
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000304static FinalOverriders::BaseOffset
305ComputeBaseOffset(ASTContext &Context,
306 const CXXRecordDecl *DerivedRD,
307 const CXXRecordDecl *BaseRD) {
308 CXXBasePaths Paths(/*FindAmbiguities=*/false,
Anders Carlssona4a54172010-02-13 20:41:15 +0000309 /*RecordPaths=*/true, /*DetectVirtual=*/false);
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000310
311 if (!const_cast<CXXRecordDecl *>(DerivedRD)->
312 isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) {
313 assert(false && "Class must be derived from the passed in base class!");
314 return FinalOverriders::BaseOffset();
315 }
316
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000317 uint64_t NonVirtualOffset = 0;
318
319 const CXXBasePath &Path = Paths.front();
320
Anders Carlssona4a54172010-02-13 20:41:15 +0000321 unsigned NonVirtualStart = 0;
322 const CXXRecordDecl *VirtualBase = 0;
323
324 // First, look for the virtual base class.
325 for (unsigned I = 0, E = Path.size(); I != E; ++I) {
326 const CXXBasePathElement &Element = Path[I];
327
328 if (Element.Base->isVirtual()) {
329 // FIXME: Can we break when we find the first virtual base?
330 // (If we can't, can't we just iterate over the path in reverse order?)
331 NonVirtualStart = I + 1;
332 QualType VBaseType = Element.Base->getType();
333 VirtualBase =
334 cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl());
335 }
336 }
337
338 // Now compute the non-virtual offset.
339 for (unsigned I = NonVirtualStart, E = Path.size(); I != E; ++I) {
340 const CXXBasePathElement &Element = Path[I];
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000341
342 // Check the base class offset.
343 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class);
344
345 const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>();
346 const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl());
347
348 NonVirtualOffset += Layout.getBaseClassOffset(Base);
349 }
350
351 // FIXME: This should probably use CharUnits or something. Maybe we should
352 // even change the base offsets in ASTRecordLayout to be specified in
353 // CharUnits.
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000354 return FinalOverriders::BaseOffset(DerivedRD, VirtualBase,
355 NonVirtualOffset / 8);
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000356}
357
358static FinalOverriders::BaseOffset
359ComputeReturnTypeBaseOffset(ASTContext &Context,
360 const CXXMethodDecl *DerivedMD,
361 const CXXMethodDecl *BaseMD) {
362 const FunctionType *BaseFT = BaseMD->getType()->getAs<FunctionType>();
363 const FunctionType *DerivedFT = DerivedMD->getType()->getAs<FunctionType>();
364
365 // Canonicalize the return types.
366 CanQualType CanDerivedReturnType =
367 Context.getCanonicalType(DerivedFT->getResultType());
368 CanQualType CanBaseReturnType =
369 Context.getCanonicalType(BaseFT->getResultType());
370
371 assert(CanDerivedReturnType->getTypeClass() ==
372 CanBaseReturnType->getTypeClass() &&
373 "Types must have same type class!");
374
375 if (CanDerivedReturnType == CanBaseReturnType) {
376 // No adjustment needed.
377 return FinalOverriders::BaseOffset();
378 }
379
380 if (isa<ReferenceType>(CanDerivedReturnType)) {
381 CanDerivedReturnType =
382 CanDerivedReturnType->getAs<ReferenceType>()->getPointeeType();
383 CanBaseReturnType =
384 CanBaseReturnType->getAs<ReferenceType>()->getPointeeType();
385 } else if (isa<PointerType>(CanDerivedReturnType)) {
386 CanDerivedReturnType =
387 CanDerivedReturnType->getAs<PointerType>()->getPointeeType();
388 CanBaseReturnType =
389 CanBaseReturnType->getAs<PointerType>()->getPointeeType();
390 } else {
391 assert(false && "Unexpected return type!");
392 }
393
394 // We need to compare unqualified types here; consider
395 // const T *Base::foo();
396 // T *Derived::foo();
397 if (CanDerivedReturnType.getUnqualifiedType() ==
398 CanBaseReturnType.getUnqualifiedType()) {
399 // No adjustment needed.
400 return FinalOverriders::BaseOffset();
401 }
402
403 const CXXRecordDecl *DerivedRD =
404 cast<CXXRecordDecl>(cast<RecordType>(CanDerivedReturnType)->getDecl());
405
406 const CXXRecordDecl *BaseRD =
407 cast<CXXRecordDecl>(cast<RecordType>(CanBaseReturnType)->getDecl());
408
409 return ComputeBaseOffset(Context, DerivedRD, BaseRD);
410}
411
Anders Carlssonb8358d82010-02-12 01:40:03 +0000412void FinalOverriders::PropagateOverrider(const CXXMethodDecl *OldMD,
Anders Carlsson74c364e2010-02-13 22:23:31 +0000413 BaseSubobject NewBase,
Anders Carlssonb8358d82010-02-12 01:40:03 +0000414 const CXXMethodDecl *NewMD,
415 SubobjectOffsetsMapTy &Offsets) {
416 for (CXXMethodDecl::method_iterator I = OldMD->begin_overridden_methods(),
417 E = OldMD->end_overridden_methods(); I != E; ++I) {
418 const CXXMethodDecl *OverriddenMD = *I;
419 const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent();
420
421 // We want to override OverriddenMD in all subobjects, for example:
422 //
423 /// struct A { virtual void f(); };
424 /// struct B1 : A { };
425 /// struct B2 : A { };
426 /// struct C : B1, B2 { virtual void f(); };
427 ///
428 /// When overriding A::f with C::f we need to do so in both A subobjects.
429 const OffsetVectorTy &OffsetVector = Offsets[OverriddenRD];
430
431 // Go through all the subobjects.
432 for (unsigned I = 0, E = OffsetVector.size(); I != E; ++I) {
433 uint64_t Offset = OffsetVector[I];
434
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000435 BaseSubobjectMethodPairTy SubobjectAndMethod =
436 std::make_pair(BaseSubobject(OverriddenRD, Offset), OverriddenMD);
437
438 OverriderInfo &Overrider = OverridersMap[SubobjectAndMethod];
439
Anders Carlssona7575822010-02-12 16:55:34 +0000440 assert(Overrider.Method && "Did not find existing overrider!");
Anders Carlssonb8358d82010-02-12 01:40:03 +0000441
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000442 // Get the return adjustment base offset.
Anders Carlsson1d05be52010-02-13 21:16:54 +0000443 // (We don't want to do this for pure virtual member functions).
444 if (!NewMD->isPure()) {
445 BaseOffset ReturnBaseOffset =
446 ComputeReturnTypeBaseOffset(Context, NewMD, OverriddenMD);
447 if (!ReturnBaseOffset.isEmpty()) {
448 // Store the return adjustment base offset.
449 ReturnAdjustments[SubobjectAndMethod] = ReturnBaseOffset;
450 }
Anders Carlsson74c364e2010-02-13 22:23:31 +0000451
452 // Check if we need a 'this' adjustment base offset as well.
453 if (Offset != NewBase.getBaseOffset()) {
454 assert(false && "FIXME: Handle 'this' adjustments!");
455 }
Anders Carlsson16b73122010-02-12 17:13:23 +0000456 }
457
Anders Carlssonb8358d82010-02-12 01:40:03 +0000458 // Set the new overrider.
Anders Carlssona7575822010-02-12 16:55:34 +0000459 Overrider.Method = NewMD;
Anders Carlssonb8358d82010-02-12 01:40:03 +0000460
461 // And propagate it further.
Anders Carlsson74c364e2010-02-13 22:23:31 +0000462 PropagateOverrider(OverriddenMD, NewBase, NewMD, Offsets);
Anders Carlssonb8358d82010-02-12 01:40:03 +0000463 }
464 }
465}
466
467void
468FinalOverriders::MergeSubobjectOffsets(const SubobjectOffsetsMapTy &NewOffsets,
469 SubobjectOffsetsMapTy &Offsets) {
470 // Iterate over the new offsets.
471 for (SubobjectOffsetsMapTy::const_iterator I = NewOffsets.begin(),
472 E = NewOffsets.end(); I != E; ++I) {
473 const CXXRecordDecl *NewRD = I->first;
Anders Carlssona4699882010-02-13 21:33:22 +0000474 const OffsetVectorTy& NewOffsetVector = I->second;
Anders Carlssonb8358d82010-02-12 01:40:03 +0000475
Anders Carlssona4699882010-02-13 21:33:22 +0000476 OffsetVectorTy &OffsetVector = Offsets[NewRD];
477 if (OffsetVector.empty()) {
Anders Carlssonb8358d82010-02-12 01:40:03 +0000478 // There were no previous offsets in this vector, just insert all entries
Anders Carlssona4699882010-02-13 21:33:22 +0000479 // from the new offset vector.
480 OffsetVector.append(NewOffsetVector.begin(), NewOffsetVector.end());
Anders Carlssonb8358d82010-02-12 01:40:03 +0000481 continue;
482 }
483
Anders Carlssona4699882010-02-13 21:33:22 +0000484 // We need to merge the new offsets vector into the old, but we don't want
485 // to have duplicate entries. Do this by inserting the old offsets in a set
486 // so they'll be unique. After this, we iterate over the new offset vector
487 // and only append elements that aren't in the set.
488
489 // First, add the existing offsets to the set.
490 llvm::SmallSet<uint64_t, 4> OffsetSet;
491 for (unsigned I = 0, E = OffsetVector.size(); I != E; ++I) {
492 bool Inserted = OffsetSet.insert(OffsetVector[I]);
493 if (!Inserted)
494 assert(false && "Set of offsets should be unique!");
495 }
496
497 // Next, only add the new offsets if they are not already in the set.
498 for (unsigned I = 0, E = NewOffsetVector.size(); I != E; ++I) {
499 uint64_t Offset = NewOffsetVector[I];
500
501 if (OffsetSet.count(Offset)) {
502 // Ignore the offset.
503 continue;
504 }
505
506 // Otherwise, add it to the offsets vector.
507 OffsetVector.push_back(Offset);
508 }
Anders Carlssonb8358d82010-02-12 01:40:03 +0000509 }
510}
511
512void FinalOverriders::ComputeFinalOverriders(BaseSubobject Base,
513 SubobjectOffsetsMapTy &Offsets) {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000514 const CXXRecordDecl *RD = Base.getBase();
Anders Carlssonb8358d82010-02-12 01:40:03 +0000515 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000516
Anders Carlssonb8358d82010-02-12 01:40:03 +0000517 SubobjectOffsetsMapTy NewOffsets;
518
519 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
520 E = RD->bases_end(); I != E; ++I) {
521 const CXXRecordDecl *BaseDecl =
522 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
523
524 assert(!I->isVirtual() && "FIXME: Handle virtual bases!");
525
526 uint64_t BaseOffset = Layout.getBaseClassOffset(BaseDecl) +
527 Base.getBaseOffset();
528
529 // Compute the final overriders for this base.
530 ComputeFinalOverriders(BaseSubobject(BaseDecl, BaseOffset), NewOffsets);
531 }
532
533 /// Now add the overriders for this particular subobject.
534 AddOverriders(Base, NewOffsets);
535
536 // And merge the newly discovered subobject offsets.
537 MergeSubobjectOffsets(NewOffsets, Offsets);
538
539 /// Finally, add the offset for our own subobject.
540 Offsets[RD].push_back(Base.getBaseOffset());
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000541}
542
543void FinalOverriders::dump(llvm::raw_ostream &Out, BaseSubobject Base) const {
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000544 const CXXRecordDecl *RD = Base.getBase();
545 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
546
547 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
548 E = RD->bases_end(); I != E; ++I) {
549 assert(!I->isVirtual() && "FIXME: Handle virtual bases!");
550
551 const CXXRecordDecl *BaseDecl =
552 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
553
554 uint64_t BaseOffset = Layout.getBaseClassOffset(BaseDecl) +
555 Base.getBaseOffset();
556
557 dump(Out, BaseSubobject(BaseDecl, BaseOffset));
558 }
559
560 Out << "Final overriders for (" << RD->getQualifiedNameAsString() << ", ";
561 Out << Base.getBaseOffset() << ")\n";
562
563 // Now dump the overriders for this base subobject.
564 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
565 E = RD->method_end(); I != E; ++I) {
566 const CXXMethodDecl *MD = *I;
567
568 if (!MD->isVirtual())
569 continue;
570
Anders Carlssona7575822010-02-12 16:55:34 +0000571 OverriderInfo Overrider = getOverrider(Base, MD);
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000572
573 Out << " " << MD->getQualifiedNameAsString() << " - ";
Anders Carlsson16b73122010-02-12 17:13:23 +0000574 Out << Overrider.Method->getQualifiedNameAsString();
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000575
Benjamin Kramere3d0b1c2010-02-13 09:11:28 +0000576 AdjustmentOffsetsMapTy::const_iterator AI =
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000577 ReturnAdjustments.find(std::make_pair(Base, MD));
Benjamin Kramere3d0b1c2010-02-13 09:11:28 +0000578 if (AI != ReturnAdjustments.end()) {
579 const BaseOffset &Offset = AI->second;
Anders Carlssona4a54172010-02-13 20:41:15 +0000580
581 Out << " [ret-adj: ";
582 if (Offset.VirtualBase)
583 Out << Offset.VirtualBase->getQualifiedNameAsString() << " vbase, ";
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000584
Anders Carlssona4a54172010-02-13 20:41:15 +0000585 Out << Offset.NonVirtualOffset << " nv]";
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000586 }
Anders Carlsson16b73122010-02-12 17:13:23 +0000587 Out << "\n";
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000588 }
589}
590
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000591/// VtableComponent - Represents a single component in a vtable.
592class VtableComponent {
593public:
594 enum Kind {
595 CK_VCallOffset,
596 CK_VBaseOffset,
597 CK_OffsetToTop,
598 CK_RTTI,
Anders Carlsson848fa642010-02-11 18:20:28 +0000599 CK_FunctionPointer,
600
601 /// CK_CompleteDtorPointer - A pointer to the complete destructor.
602 CK_CompleteDtorPointer,
603
604 /// CK_DeletingDtorPointer - A pointer to the deleting destructor.
605 CK_DeletingDtorPointer
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000606 };
607
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000608 static VtableComponent MakeOffsetToTop(int64_t Offset) {
609 return VtableComponent(CK_OffsetToTop, Offset);
610 }
611
612 static VtableComponent MakeRTTI(const CXXRecordDecl *RD) {
613 return VtableComponent(CK_RTTI, reinterpret_cast<uintptr_t>(RD));
614 }
615
616 static VtableComponent MakeFunction(const CXXMethodDecl *MD) {
617 assert(!isa<CXXDestructorDecl>(MD) &&
Anders Carlsson848fa642010-02-11 18:20:28 +0000618 "Don't use MakeFunction with destructors!");
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000619
Anders Carlsson848fa642010-02-11 18:20:28 +0000620 return VtableComponent(CK_FunctionPointer,
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000621 reinterpret_cast<uintptr_t>(MD));
622 }
623
Anders Carlsson848fa642010-02-11 18:20:28 +0000624 static VtableComponent MakeCompleteDtor(const CXXDestructorDecl *DD) {
625 return VtableComponent(CK_CompleteDtorPointer,
626 reinterpret_cast<uintptr_t>(DD));
627 }
628
629 static VtableComponent MakeDeletingDtor(const CXXDestructorDecl *DD) {
630 return VtableComponent(CK_DeletingDtorPointer,
631 reinterpret_cast<uintptr_t>(DD));
632 }
633
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000634 /// getKind - Get the kind of this vtable component.
635 Kind getKind() const {
636 return (Kind)(Value & 0x7);
637 }
638
639 int64_t getOffsetToTop() const {
640 assert(getKind() == CK_OffsetToTop && "Invalid component kind!");
641
642 return getOffset();
643 }
644
645 const CXXRecordDecl *getRTTIDecl() const {
646 assert(getKind() == CK_RTTI && "Invalid component kind!");
647
648 return reinterpret_cast<CXXRecordDecl *>(getPointer());
649 }
650
651 const CXXMethodDecl *getFunctionDecl() const {
Anders Carlsson848fa642010-02-11 18:20:28 +0000652 assert(getKind() == CK_FunctionPointer);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000653
654 return reinterpret_cast<CXXMethodDecl *>(getPointer());
655 }
Anders Carlsson848fa642010-02-11 18:20:28 +0000656
657 const CXXDestructorDecl *getDestructorDecl() const {
658 assert((getKind() == CK_CompleteDtorPointer ||
659 getKind() == CK_DeletingDtorPointer) && "Invalid component kind!");
660
661 return reinterpret_cast<CXXDestructorDecl *>(getPointer());
662 }
663
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000664private:
665 VtableComponent(Kind ComponentKind, int64_t Offset) {
666 assert((ComponentKind == CK_VCallOffset ||
667 ComponentKind == CK_VBaseOffset ||
668 ComponentKind == CK_OffsetToTop) && "Invalid component kind!");
669 assert(Offset <= ((1LL << 56) - 1) && "Offset is too big!");
670
671 Value = ((Offset << 3) | ComponentKind);
672 }
673
674 VtableComponent(Kind ComponentKind, uintptr_t Ptr) {
675 assert((ComponentKind == CK_RTTI ||
Anders Carlsson848fa642010-02-11 18:20:28 +0000676 ComponentKind == CK_FunctionPointer ||
677 ComponentKind == CK_CompleteDtorPointer ||
678 ComponentKind == CK_DeletingDtorPointer) &&
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000679 "Invalid component kind!");
680
681 assert((Ptr & 7) == 0 && "Pointer not sufficiently aligned!");
682
683 Value = Ptr | ComponentKind;
684 }
685
686 int64_t getOffset() const {
687 assert((getKind() == CK_VCallOffset || getKind() == CK_VBaseOffset ||
688 getKind() == CK_OffsetToTop) && "Invalid component kind!");
689
690 return Value >> 3;
691 }
692
693 uintptr_t getPointer() const {
Anders Carlsson848fa642010-02-11 18:20:28 +0000694 assert((getKind() == CK_RTTI ||
695 getKind() == CK_FunctionPointer ||
696 getKind() == CK_CompleteDtorPointer ||
697 getKind() == CK_DeletingDtorPointer) &&
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000698 "Invalid component kind!");
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000699
700 return static_cast<uintptr_t>(Value & ~7ULL);
701 }
702
703 /// The kind is stored in the lower 3 bits of the value. For offsets, we
704 /// make use of the facts that classes can't be larger than 2^55 bytes,
705 /// so we store the offset in the lower part of the 61 bytes that remain.
706 /// (The reason that we're not simply using a PointerIntPair here is that we
707 /// need the offsets to be 64-bit, even when on a 32-bit machine).
708 int64_t Value;
709};
710
711/// VtableBuilder - Class for building vtable layout information.
Benjamin Kramer85b45212009-11-28 19:45:26 +0000712class VtableBuilder {
Anders Carlsson57071e22010-02-12 05:25:12 +0000713public:
714 /// PrimaryBasesSetTy - A set of direct and indirect primary bases.
715 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 8> PrimaryBasesSetTy;
716
717private:
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000718 /// VtableInfo - Global vtable information.
719 CGVtableInfo &VtableInfo;
720
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000721 /// MostDerivedClass - The most derived class for which we're building this
722 /// vtable.
723 const CXXRecordDecl *MostDerivedClass;
724
725 /// Context - The ASTContext which we will use for layout information.
Anders Carlsson57071e22010-02-12 05:25:12 +0000726 ASTContext &Context;
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000727
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000728 /// FinalOverriders - The final overriders of the most derived class.
729 FinalOverriders Overriders;
730
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000731 /// Components - The components of the vtable being built.
732 llvm::SmallVector<VtableComponent, 64> Components;
733
Anders Carlsson822307b2010-02-11 17:18:51 +0000734 /// AddressPoints - Address points for the vtable being built.
735 CGVtableInfo::AddressPointsMapTy AddressPoints;
736
Anders Carlsson104f45c2010-02-12 17:37:14 +0000737 /// ReturnAdjustment - A return adjustment thunk.
738 struct ReturnAdjustment {
739 /// NonVirtual - The non-virtual adjustment from the derived object to its
740 /// nearest virtual base.
741 int64_t NonVirtual;
742
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000743 /// VBaseOffsetOffset - The offset, in bytes, relative to the address point
744 /// of the virtual base class offset.
745 int64_t VBaseOffsetOffset;
Anders Carlsson104f45c2010-02-12 17:37:14 +0000746
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000747 ReturnAdjustment() : NonVirtual(0), VBaseOffsetOffset(0) { }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000748
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000749 bool isEmpty() const { return !NonVirtual && !VBaseOffsetOffset; }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000750 };
751
752 /// ReturnAdjustments - The return adjustments needed in this vtable.
753 llvm::SmallVector<std::pair<uint64_t, ReturnAdjustment>, 16>
754 ReturnAdjustments;
755
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000756 /// ComputeReturnAdjustment - Compute the return adjustment given a return
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000757 /// adjustment base offset.
758 ReturnAdjustment ComputeReturnAdjustment(FinalOverriders::BaseOffset Offset);
759
760 /// AddMethod - Add a single virtual member function to the vtable
761 /// components vector.
762 void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment);
763
764 /// AddMethods - Add the methods of this base subobject and all its
765 /// primary bases to the vtable components vector.
766 void AddMethods(BaseSubobject Base, PrimaryBasesSetTy &PrimaryBases);
Anders Carlsson57071e22010-02-12 05:25:12 +0000767
Anders Carlsson76f1aa72010-02-13 22:05:23 +0000768 /// layoutVtable - Layout a vtable and all its secondary vtables.
769 void layoutVtable(BaseSubobject Base);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000770
771public:
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000772 VtableBuilder(CGVtableInfo &VtableInfo, const CXXRecordDecl *MostDerivedClass)
773 : VtableInfo(VtableInfo), MostDerivedClass(MostDerivedClass),
Anders Carlssonbdf73d82010-02-11 21:24:32 +0000774 Context(MostDerivedClass->getASTContext()), Overriders(MostDerivedClass) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000775
Anders Carlsson76f1aa72010-02-13 22:05:23 +0000776 layoutVtable(BaseSubobject(MostDerivedClass, 0));
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000777 }
778
779 /// dumpLayout - Dump the vtable layout.
780 void dumpLayout(llvm::raw_ostream&);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000781};
782
Anders Carlsson57071e22010-02-12 05:25:12 +0000783/// OverridesMethodInPrimaryBase - Checks whether whether this virtual member
784/// function overrides a member function in a direct or indirect primary base.
785/// Returns the overridden member function, or null if none was found.
786static const CXXMethodDecl *
787OverridesMethodInPrimaryBase(const CXXMethodDecl *MD,
788 VtableBuilder::PrimaryBasesSetTy &PrimaryBases) {
789 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
790 E = MD->end_overridden_methods(); I != E; ++I) {
791 const CXXMethodDecl *OverriddenMD = *I;
792 const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent();
793 assert(OverriddenMD->isCanonicalDecl() &&
794 "Should have the canonical decl of the overridden RD!");
795
796 if (PrimaryBases.count(OverriddenRD))
797 return OverriddenMD;
798 }
799
800 return 0;
801}
802
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000803VtableBuilder::ReturnAdjustment
804VtableBuilder::ComputeReturnAdjustment(FinalOverriders::BaseOffset Offset) {
805 ReturnAdjustment Adjustment;
806
807 if (!Offset.isEmpty()) {
Anders Carlsson60db0ee2010-02-13 21:07:32 +0000808 if (Offset.VirtualBase) {
809 // Get the virtual base offset offset.
810 Adjustment.VBaseOffsetOffset =
811 VtableInfo.getVirtualBaseOffsetIndex(Offset.DerivedClass,
812 Offset.VirtualBase);
813 // FIXME: Once the assert in getVirtualBaseOffsetIndex is back again,
814 // we can get rid of this assert.
815 assert(Adjustment.VBaseOffsetOffset != 0 &&
816 "Invalid base offset offset!");
817 }
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000818
819 Adjustment.NonVirtual = Offset.NonVirtualOffset;
820 }
821
822 return Adjustment;
823}
824
Anders Carlsson57071e22010-02-12 05:25:12 +0000825void
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000826VtableBuilder::AddMethod(const CXXMethodDecl *MD,
827 ReturnAdjustment ReturnAdjustment) {
828 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
829 assert(ReturnAdjustment.isEmpty() &&
830 "Destructor can't have return adjustment!");
831 // Add both the complete destructor and the deleting destructor.
832 Components.push_back(VtableComponent::MakeCompleteDtor(DD));
833 Components.push_back(VtableComponent::MakeDeletingDtor(DD));
834 } else {
835 // Add the return adjustment if necessary.
836 if (!ReturnAdjustment.isEmpty())
837 ReturnAdjustments.push_back(std::make_pair(Components.size(),
838 ReturnAdjustment));
839
840 // Add the function.
841 Components.push_back(VtableComponent::MakeFunction(MD));
842 }
843}
844
845void
846VtableBuilder::AddMethods(BaseSubobject Base, PrimaryBasesSetTy &PrimaryBases) {
Anders Carlsson57071e22010-02-12 05:25:12 +0000847 const CXXRecordDecl *RD = Base.getBase();
848
849 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
850
851 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
852 if (Layout.getPrimaryBaseWasVirtual())
853 assert(false && "FIXME: Handle vbases here.");
854 else
855 assert(Layout.getBaseClassOffset(PrimaryBase) == 0 &&
856 "Primary base should have a zero offset!");
857
Anders Carlsson76f1aa72010-02-13 22:05:23 +0000858 AddMethods(BaseSubobject(PrimaryBase, Base.getBaseOffset()), PrimaryBases);
Anders Carlsson57071e22010-02-12 05:25:12 +0000859
860 if (!PrimaryBases.insert(PrimaryBase))
861 assert(false && "Found a duplicate primary base!");
862 }
Anders Carlsson822307b2010-02-11 17:18:51 +0000863
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000864 // Now go through all virtual member functions and add them.
865 for (CXXRecordDecl::method_iterator I = RD->method_begin(),
866 E = RD->method_end(); I != E; ++I) {
867 const CXXMethodDecl *MD = *I;
Anders Carlsson57071e22010-02-12 05:25:12 +0000868
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000869 if (!MD->isVirtual())
870 continue;
Anders Carlsson57071e22010-02-12 05:25:12 +0000871
872 // Get the final overrider.
Anders Carlssona7575822010-02-12 16:55:34 +0000873 FinalOverriders::OverriderInfo Overrider =
874 Overriders.getOverrider(Base, MD);
Anders Carlsson57071e22010-02-12 05:25:12 +0000875
876 // Check if this virtual member function overrides a method in a primary
877 // base. If this is the case, and the return type doesn't require adjustment
878 // then we can just use the member function from the primary base.
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000879 if (const CXXMethodDecl *OverriddenMD =
880 OverridesMethodInPrimaryBase(MD, PrimaryBases)) {
881 if (!ReturnTypeConversionRequiresAdjustment(MD, OverriddenMD))
882 continue;
883 }
Anders Carlsson0c0eeb22010-02-13 02:02:03 +0000884
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000885 // Check if this overrider needs a return adjustment.
886 FinalOverriders::BaseOffset ReturnAdjustmentOffset =
887 Overriders.getReturnAdjustmentOffset(Base, MD);
888
889 ReturnAdjustment ReturnAdjustment =
890 ComputeReturnAdjustment(ReturnAdjustmentOffset);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000891
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000892 AddMethod(Overrider.Method, ReturnAdjustment);
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000893 }
894}
895
Anders Carlsson76f1aa72010-02-13 22:05:23 +0000896void VtableBuilder::layoutVtable(BaseSubobject Base) {
Anders Carlsson57071e22010-02-12 05:25:12 +0000897 const CXXRecordDecl *RD = Base.getBase();
898
899 // First, add the offset to top.
Anders Carlsson76f1aa72010-02-13 22:05:23 +0000900 // FIXME: This is not going to be right for construction vtables.
901 // FIXME: We should not use -8 here.
902 int64_t OffsetToTop = -(int64_t)Base.getBaseOffset() / 8;
903 Components.push_back(VtableComponent::MakeOffsetToTop(OffsetToTop));
Anders Carlsson57071e22010-02-12 05:25:12 +0000904
905 // Next, add the RTTI.
Anders Carlsson76f1aa72010-02-13 22:05:23 +0000906 Components.push_back(VtableComponent::MakeRTTI(MostDerivedClass));
Anders Carlsson57071e22010-02-12 05:25:12 +0000907
Anders Carlsson28cbc8b2010-02-12 07:43:48 +0000908 uint64_t AddressPoint = Components.size();
Anders Carlsson57071e22010-02-12 05:25:12 +0000909
910 // Now go through all virtual member functions and add them.
911 PrimaryBasesSetTy PrimaryBases;
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000912 AddMethods(Base, PrimaryBases);
Anders Carlsson57071e22010-02-12 05:25:12 +0000913
Anders Carlsson28cbc8b2010-02-12 07:43:48 +0000914 // Record the address point.
915 AddressPoints.insert(std::make_pair(Base, AddressPoint));
916
917 // Record the address points for all primary bases.
918 for (PrimaryBasesSetTy::const_iterator I = PrimaryBases.begin(),
919 E = PrimaryBases.end(); I != E; ++I) {
920 const CXXRecordDecl *BaseDecl = *I;
921
922 // We know that all the primary bases have the same offset as the base
923 // subobject.
924 BaseSubobject PrimaryBase(BaseDecl, Base.getBaseOffset());
925 AddressPoints.insert(std::make_pair(PrimaryBase, AddressPoint));
926 }
927
Anders Carlsson57071e22010-02-12 05:25:12 +0000928 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
929 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
930
Anders Carlsson76f1aa72010-02-13 22:05:23 +0000931 // Layout secondary vtables.
Anders Carlsson57071e22010-02-12 05:25:12 +0000932 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
933 E = RD->bases_end(); I != E; ++I) {
934 const CXXRecordDecl *BaseDecl =
935 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
936
937 // Ignore the primary base.
938 if (BaseDecl == PrimaryBase)
939 continue;
940
941 assert(!I->isVirtual() && "FIXME: Handle virtual bases");
942
Anders Carlsson76f1aa72010-02-13 22:05:23 +0000943 // Get the base offset of this base.
944 uint64_t BaseOffset = Base.getBaseOffset() +
945 Layout.getBaseClassOffset(BaseDecl);
946
947 // Layout this secondary vtable.
948 layoutVtable(BaseSubobject(BaseDecl, BaseOffset));
Anders Carlsson57071e22010-02-12 05:25:12 +0000949 }
950}
951
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000952/// dumpLayout - Dump the vtable layout.
953void VtableBuilder::dumpLayout(llvm::raw_ostream& Out) {
954
955 Out << "Vtable for '" << MostDerivedClass->getQualifiedNameAsString();
956 Out << "' (" << Components.size() << " entries).\n";
957
Anders Carlsson822307b2010-02-11 17:18:51 +0000958 // Iterate through the address points and insert them into a new map where
959 // they are keyed by the index and not the base object.
960 // Since an address point can be shared by multiple subobjects, we use an
961 // STL multimap.
962 std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex;
963 for (CGVtableInfo::AddressPointsMapTy::const_iterator I =
964 AddressPoints.begin(), E = AddressPoints.end(); I != E; ++I) {
965 const BaseSubobject& Base = I->first;
966 uint64_t Index = I->second;
Anders Carlsson824d7ea2010-02-11 08:02:13 +0000967
Anders Carlsson822307b2010-02-11 17:18:51 +0000968 AddressPointsByIndex.insert(std::make_pair(Index, Base));
969 }
970
Anders Carlsson7dbf47a2010-02-13 20:11:51 +0000971 unsigned NextReturnAdjustmentIndex = 0;
Anders Carlsson822307b2010-02-11 17:18:51 +0000972 for (unsigned I = 0, E = Components.size(); I != E; ++I) {
Anders Carlsson28cbc8b2010-02-12 07:43:48 +0000973 uint64_t Index = I;
974
Anders Carlsson822307b2010-02-11 17:18:51 +0000975 if (AddressPointsByIndex.count(I)) {
Anders Carlsson28cbc8b2010-02-12 07:43:48 +0000976 if (AddressPointsByIndex.count(Index) == 1) {
977 const BaseSubobject &Base = AddressPointsByIndex.find(Index)->second;
978
979 // FIXME: Instead of dividing by 8, we should be using CharUnits.
980 Out << " -- (" << Base.getBase()->getQualifiedNameAsString();
981 Out << ", " << Base.getBaseOffset() / 8 << ") vtable address --\n";
982 } else {
983 uint64_t BaseOffset =
984 AddressPointsByIndex.lower_bound(Index)->second.getBaseOffset();
985
986 // We store the class names in a set to get a stable order.
987 std::set<std::string> ClassNames;
988 for (std::multimap<uint64_t, BaseSubobject>::const_iterator I =
989 AddressPointsByIndex.lower_bound(Index), E =
990 AddressPointsByIndex.upper_bound(Index); I != E; ++I) {
991 assert(I->second.getBaseOffset() == BaseOffset &&
992 "Invalid base offset!");
993 const CXXRecordDecl *RD = I->second.getBase();
994 ClassNames.insert(RD->getQualifiedNameAsString());
995 }
996
997 for (std::set<std::string>::const_iterator I = ClassNames.begin(),
998 E = ClassNames.end(); I != E; ++I) {
999 // FIXME: Instead of dividing by 8, we should be using CharUnits.
1000 Out << " -- (" << *I;
1001 Out << ", " << BaseOffset / 8 << ") vtable address --\n";
1002 }
1003 }
Anders Carlsson822307b2010-02-11 17:18:51 +00001004 }
1005
1006 Out << llvm::format("%4d | ", I);
1007
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001008 const VtableComponent &Component = Components[I];
1009
1010 // Dump the component.
1011 switch (Component.getKind()) {
1012 // FIXME: Remove this default case.
1013 default:
1014 assert(false && "Unhandled component kind!");
1015 break;
1016
1017 case VtableComponent::CK_OffsetToTop:
1018 Out << "offset_to_top (" << Component.getOffsetToTop() << ")";
1019 break;
1020
1021 case VtableComponent::CK_RTTI:
1022 Out << Component.getRTTIDecl()->getQualifiedNameAsString() << " RTTI";
1023 break;
1024
Anders Carlsson848fa642010-02-11 18:20:28 +00001025 case VtableComponent::CK_FunctionPointer: {
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001026 const CXXMethodDecl *MD = Component.getFunctionDecl();
1027
Anders Carlsson848fa642010-02-11 18:20:28 +00001028 std::string Str =
1029 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
1030 MD);
1031 Out << Str;
Anders Carlsson98241422010-02-12 02:38:13 +00001032 if (MD->isPure())
1033 Out << " [pure]";
1034
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001035 // If this function pointer has a return adjustment thunk, dump it.
1036 if (NextReturnAdjustmentIndex < ReturnAdjustments.size() &&
1037 ReturnAdjustments[NextReturnAdjustmentIndex].first == I) {
1038 const ReturnAdjustment Adjustment =
1039 ReturnAdjustments[NextReturnAdjustmentIndex].second;
1040
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001041 Out << "\n [return adjustment: ";
Anders Carlsson60db0ee2010-02-13 21:07:32 +00001042 Out << Adjustment.NonVirtual << " non-virtual";
1043
1044 if (Adjustment.VBaseOffsetOffset)
1045 Out << ", " << Adjustment.VBaseOffsetOffset << " vbase offset offset";
1046 Out << ']';
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00001047
1048 NextReturnAdjustmentIndex++;
1049 }
1050
Anders Carlsson848fa642010-02-11 18:20:28 +00001051 break;
1052 }
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001053
Anders Carlsson848fa642010-02-11 18:20:28 +00001054 case VtableComponent::CK_CompleteDtorPointer: {
1055 const CXXDestructorDecl *DD = Component.getDestructorDecl();
1056
1057 Out << DD->getQualifiedNameAsString() << "() [complete]";
Anders Carlsson98241422010-02-12 02:38:13 +00001058 if (DD->isPure())
1059 Out << " [pure]";
1060
Anders Carlsson848fa642010-02-11 18:20:28 +00001061 break;
1062 }
1063
1064 case VtableComponent::CK_DeletingDtorPointer: {
1065 const CXXDestructorDecl *DD = Component.getDestructorDecl();
1066
1067 Out << DD->getQualifiedNameAsString() << "() [deleting]";
Anders Carlsson98241422010-02-12 02:38:13 +00001068 if (DD->isPure())
1069 Out << " [pure]";
1070
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001071 break;
1072 }
1073
1074 }
Anders Carlsson822307b2010-02-11 17:18:51 +00001075
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001076 Out << '\n';
1077 }
1078
1079}
1080
1081}
1082
1083namespace {
1084class OldVtableBuilder {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001085public:
1086 /// Index_t - Vtable index type.
1087 typedef uint64_t Index_t;
Eli Friedmanb455f0e2009-12-07 23:56:34 +00001088 typedef std::vector<std::pair<GlobalDecl,
1089 std::pair<GlobalDecl, ThunkAdjustment> > >
1090 SavedAdjustmentsVectorTy;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001091private:
Anders Carlsson15318f42009-12-04 16:19:30 +00001092
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00001093 // VtableComponents - The components of the vtable being built.
1094 typedef llvm::SmallVector<llvm::Constant *, 64> VtableComponentsVectorTy;
1095 VtableComponentsVectorTy VtableComponents;
1096
Eli Friedman152b5b12009-12-05 01:05:03 +00001097 const bool BuildVtable;
1098
Anders Carlssondbd920c2009-10-11 22:13:54 +00001099 llvm::Type *Ptr8Ty;
Anders Carlssonac3f7bd2009-12-04 18:36:22 +00001100
1101 /// MostDerivedClass - The most derived class that this vtable is being
1102 /// built for.
1103 const CXXRecordDecl *MostDerivedClass;
1104
Mike Stumpacfd1e52009-11-13 01:54:23 +00001105 /// LayoutClass - The most derived class used for virtual base layout
1106 /// information.
1107 const CXXRecordDecl *LayoutClass;
Mike Stump4cde6262009-11-13 02:13:54 +00001108 /// LayoutOffset - The offset for Class in LayoutClass.
1109 uint64_t LayoutOffset;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001110 /// BLayout - Layout for the most derived class that this vtable is being
1111 /// built for.
1112 const ASTRecordLayout &BLayout;
1113 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary;
1114 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase;
1115 llvm::Constant *rtti;
1116 llvm::LLVMContext &VMContext;
1117 CodeGenModule &CGM; // Per-module state.
Anders Carlsson0e881162009-12-04 03:46:21 +00001118
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001119 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall;
Anders Carlssona0fdd912009-11-13 17:08:56 +00001120 llvm::DenseMap<GlobalDecl, Index_t> VCallOffset;
Mike Stumpd99a4d22010-01-26 03:42:22 +00001121 llvm::DenseMap<GlobalDecl, Index_t> VCallOffsetForVCall;
Mike Stump9e7e3c62009-11-06 23:27:42 +00001122 // This is the offset to the nearest virtual base
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001123 llvm::DenseMap<const CXXMethodDecl *, Index_t> NonVirtualOffset;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001124 llvm::DenseMap<const CXXRecordDecl *, Index_t> VBIndex;
Mike Stump94aff932009-10-27 23:46:47 +00001125
Anders Carlsson6d4ccb72009-11-26 19:54:33 +00001126 /// PureVirtualFunction - Points to __cxa_pure_virtual.
1127 llvm::Constant *PureVirtualFn;
1128
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00001129 /// VtableMethods - A data structure for keeping track of methods in a vtable.
1130 /// Can add methods, override methods and iterate in vtable order.
1131 class VtableMethods {
1132 // MethodToIndexMap - Maps from a global decl to the index it has in the
1133 // Methods vector.
1134 llvm::DenseMap<GlobalDecl, uint64_t> MethodToIndexMap;
1135
1136 /// Methods - The methods, in vtable order.
1137 typedef llvm::SmallVector<GlobalDecl, 16> MethodsVectorTy;
1138 MethodsVectorTy Methods;
Eli Friedman72649ed2009-12-06 22:01:30 +00001139 MethodsVectorTy OrigMethods;
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00001140
1141 public:
1142 /// AddMethod - Add a method to the vtable methods.
1143 void AddMethod(GlobalDecl GD) {
1144 assert(!MethodToIndexMap.count(GD) &&
1145 "Method has already been added!");
1146
1147 MethodToIndexMap[GD] = Methods.size();
1148 Methods.push_back(GD);
Eli Friedman72649ed2009-12-06 22:01:30 +00001149 OrigMethods.push_back(GD);
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00001150 }
1151
1152 /// OverrideMethod - Replace a method with another.
1153 void OverrideMethod(GlobalDecl OverriddenGD, GlobalDecl GD) {
1154 llvm::DenseMap<GlobalDecl, uint64_t>::iterator i
1155 = MethodToIndexMap.find(OverriddenGD);
1156 assert(i != MethodToIndexMap.end() && "Did not find entry!");
1157
1158 // Get the index of the old decl.
1159 uint64_t Index = i->second;
1160
1161 // Replace the old decl with the new decl.
1162 Methods[Index] = GD;
1163
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00001164 // And add the new.
1165 MethodToIndexMap[GD] = Index;
1166 }
1167
Anders Carlsson77c23e52009-12-04 15:49:02 +00001168 /// getIndex - Gives the index of a passed in GlobalDecl. Returns false if
1169 /// the index couldn't be found.
Benjamin Kramercce9fde2009-12-04 22:45:27 +00001170 bool getIndex(GlobalDecl GD, uint64_t &Index) const {
Anders Carlsson77c23e52009-12-04 15:49:02 +00001171 llvm::DenseMap<GlobalDecl, uint64_t>::const_iterator i
1172 = MethodToIndexMap.find(GD);
Eli Friedman47145832009-12-04 08:34:14 +00001173
Anders Carlsson77c23e52009-12-04 15:49:02 +00001174 if (i == MethodToIndexMap.end())
1175 return false;
Anders Carlssonc0c49932009-12-04 03:41:37 +00001176
Anders Carlsson77c23e52009-12-04 15:49:02 +00001177 Index = i->second;
1178 return true;
Anders Carlssonc0c49932009-12-04 03:41:37 +00001179 }
1180
Eli Friedman72649ed2009-12-06 22:01:30 +00001181 GlobalDecl getOrigMethod(uint64_t Index) const {
1182 return OrigMethods[Index];
1183 }
1184
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00001185 MethodsVectorTy::size_type size() const {
1186 return Methods.size();
1187 }
1188
1189 void clear() {
1190 MethodToIndexMap.clear();
1191 Methods.clear();
Eli Friedman72649ed2009-12-06 22:01:30 +00001192 OrigMethods.clear();
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00001193 }
1194
Anders Carlssonc0c49932009-12-04 03:41:37 +00001195 GlobalDecl operator[](uint64_t Index) const {
Anders Carlssonc7ab1a82009-12-04 02:01:07 +00001196 return Methods[Index];
1197 }
1198 };
1199
1200 /// Methods - The vtable methods we're currently building.
1201 VtableMethods Methods;
1202
Anders Carlsson98fdb242009-12-04 02:26:15 +00001203 /// ThisAdjustments - For a given index in the vtable, contains the 'this'
1204 /// pointer adjustment needed for a method.
1205 typedef llvm::DenseMap<uint64_t, ThunkAdjustment> ThisAdjustmentsMapTy;
1206 ThisAdjustmentsMapTy ThisAdjustments;
Anders Carlsson5dd730a2009-11-26 19:32:45 +00001207
Eli Friedmanb455f0e2009-12-07 23:56:34 +00001208 SavedAdjustmentsVectorTy SavedAdjustments;
Eli Friedman72649ed2009-12-06 22:01:30 +00001209
Anders Carlssona8756702009-12-04 02:22:02 +00001210 /// BaseReturnTypes - Contains the base return types of methods who have been
1211 /// overridden with methods whose return types require adjustment. Used for
1212 /// generating covariant thunk information.
1213 typedef llvm::DenseMap<uint64_t, CanQualType> BaseReturnTypesMapTy;
1214 BaseReturnTypesMapTy BaseReturnTypes;
Anders Carlsson5dd730a2009-11-26 19:32:45 +00001215
Anders Carlssondbd920c2009-10-11 22:13:54 +00001216 std::vector<Index_t> VCalls;
Mike Stump9840c702009-11-12 20:47:57 +00001217
1218 typedef std::pair<const CXXRecordDecl *, uint64_t> CtorVtable_t;
Mike Stump23a35422009-11-19 20:52:19 +00001219 // subAddressPoints - Used to hold the AddressPoints (offsets) into the built
1220 // vtable for use in computing the initializers for the VTT.
1221 llvm::DenseMap<CtorVtable_t, int64_t> &subAddressPoints;
Mike Stump9840c702009-11-12 20:47:57 +00001222
Anders Carlsson1bb60992010-01-14 02:29:07 +00001223 /// AddressPoints - Address points for this vtable.
1224 CGVtableInfo::AddressPointsMapTy& AddressPoints;
1225
Anders Carlssondbd920c2009-10-11 22:13:54 +00001226 typedef CXXRecordDecl::method_iterator method_iter;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001227 const uint32_t LLVMPointerWidth;
1228 Index_t extra;
Mike Stump11dea942009-10-15 02:04:03 +00001229 typedef std::vector<std::pair<const CXXRecordDecl *, int64_t> > Path_t;
Mike Stump23a35422009-11-19 20:52:19 +00001230 static llvm::DenseMap<CtorVtable_t, int64_t>&
1231 AllocAddressPoint(CodeGenModule &cgm, const CXXRecordDecl *l,
1232 const CXXRecordDecl *c) {
Anders Carlsson21431c52010-01-02 18:02:32 +00001233 CGVtableInfo::AddrMap_t *&oref = cgm.getVtableInfo().AddressPoints[l];
Mike Stump23a35422009-11-19 20:52:19 +00001234 if (oref == 0)
Anders Carlsson21431c52010-01-02 18:02:32 +00001235 oref = new CGVtableInfo::AddrMap_t;
Mike Stump23a35422009-11-19 20:52:19 +00001236
1237 llvm::DenseMap<CtorVtable_t, int64_t> *&ref = (*oref)[c];
1238 if (ref == 0)
1239 ref = new llvm::DenseMap<CtorVtable_t, int64_t>;
1240 return *ref;
1241 }
Anders Carlsson6d4ccb72009-11-26 19:54:33 +00001242
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001243 bool DclIsSame(const FunctionDecl *New, const FunctionDecl *Old) {
1244 FunctionTemplateDecl *OldTemplate = Old->getDescribedFunctionTemplate();
1245 FunctionTemplateDecl *NewTemplate = New->getDescribedFunctionTemplate();
1246
1247 // C++ [temp.fct]p2:
1248 // A function template can be overloaded with other function templates
1249 // and with normal (non-template) functions.
1250 if ((OldTemplate == 0) != (NewTemplate == 0))
1251 return false;
1252
1253 // Is the function New an overload of the function Old?
1254 QualType OldQType = CGM.getContext().getCanonicalType(Old->getType());
1255 QualType NewQType = CGM.getContext().getCanonicalType(New->getType());
1256
1257 // Compare the signatures (C++ 1.3.10) of the two functions to
1258 // determine whether they are overloads. If we find any mismatch
1259 // in the signature, they are overloads.
1260
1261 // If either of these functions is a K&R-style function (no
1262 // prototype), then we consider them to have matching signatures.
1263 if (isa<FunctionNoProtoType>(OldQType.getTypePtr()) ||
1264 isa<FunctionNoProtoType>(NewQType.getTypePtr()))
1265 return true;
1266
1267 FunctionProtoType* OldType = cast<FunctionProtoType>(OldQType);
1268 FunctionProtoType* NewType = cast<FunctionProtoType>(NewQType);
1269
1270 // The signature of a function includes the types of its
1271 // parameters (C++ 1.3.10), which includes the presence or absence
1272 // of the ellipsis; see C++ DR 357).
1273 if (OldQType != NewQType &&
1274 (OldType->getNumArgs() != NewType->getNumArgs() ||
1275 OldType->isVariadic() != NewType->isVariadic() ||
1276 !std::equal(OldType->arg_type_begin(), OldType->arg_type_end(),
1277 NewType->arg_type_begin())))
1278 return false;
1279
1280#if 0
1281 // C++ [temp.over.link]p4:
1282 // The signature of a function template consists of its function
1283 // signature, its return type and its template parameter list. The names
1284 // of the template parameters are significant only for establishing the
1285 // relationship between the template parameters and the rest of the
1286 // signature.
1287 //
1288 // We check the return type and template parameter lists for function
1289 // templates first; the remaining checks follow.
1290 if (NewTemplate &&
1291 (!TemplateParameterListsAreEqual(NewTemplate->getTemplateParameters(),
1292 OldTemplate->getTemplateParameters(),
1293 TPL_TemplateMatch) ||
1294 OldType->getResultType() != NewType->getResultType()))
1295 return false;
1296#endif
1297
1298 // If the function is a class member, its signature includes the
1299 // cv-qualifiers (if any) on the function itself.
1300 //
1301 // As part of this, also check whether one of the member functions
1302 // is static, in which case they are not overloads (C++
1303 // 13.1p2). While not part of the definition of the signature,
1304 // this check is important to determine whether these functions
1305 // can be overloaded.
1306 const CXXMethodDecl* OldMethod = dyn_cast<CXXMethodDecl>(Old);
1307 const CXXMethodDecl* NewMethod = dyn_cast<CXXMethodDecl>(New);
1308 if (OldMethod && NewMethod &&
1309 !OldMethod->isStatic() && !NewMethod->isStatic() &&
1310 OldMethod->getTypeQualifiers() != NewMethod->getTypeQualifiers())
1311 return false;
1312
1313 // The signatures match; this is not an overload.
1314 return true;
1315 }
1316
1317 typedef llvm::DenseMap<const CXXMethodDecl *, const CXXMethodDecl*>
1318 ForwardUnique_t;
1319 ForwardUnique_t ForwardUnique;
1320 llvm::DenseMap<const CXXMethodDecl*, const CXXMethodDecl*> UniqueOverrider;
1321
1322 void BuildUniqueOverrider(const CXXMethodDecl *U, const CXXMethodDecl *MD) {
1323 const CXXMethodDecl *PrevU = UniqueOverrider[MD];
1324 assert(U && "no unique overrider");
1325 if (PrevU == U)
1326 return;
1327 if (PrevU != U && PrevU != 0) {
1328 // If already set, note the two sets as the same
1329 if (0)
1330 printf("%s::%s same as %s::%s\n",
1331 PrevU->getParent()->getNameAsCString(),
1332 PrevU->getNameAsCString(),
1333 U->getParent()->getNameAsCString(),
1334 U->getNameAsCString());
1335 ForwardUnique[PrevU] = U;
1336 return;
1337 }
1338
1339 // Not set, set it now
1340 if (0)
1341 printf("marking %s::%s %p override as %s::%s\n",
1342 MD->getParent()->getNameAsCString(),
1343 MD->getNameAsCString(),
1344 (void*)MD,
1345 U->getParent()->getNameAsCString(),
1346 U->getNameAsCString());
1347 UniqueOverrider[MD] = U;
1348
1349 for (CXXMethodDecl::method_iterator mi = MD->begin_overridden_methods(),
1350 me = MD->end_overridden_methods(); mi != me; ++mi) {
1351 BuildUniqueOverrider(U, *mi);
1352 }
1353 }
1354
1355 void BuildUniqueOverriders(const CXXRecordDecl *RD) {
1356 if (0) printf("walking %s\n", RD->getNameAsCString());
1357 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
1358 e = RD->method_end(); i != e; ++i) {
1359 const CXXMethodDecl *MD = *i;
1360 if (!MD->isVirtual())
1361 continue;
1362
1363 if (UniqueOverrider[MD] == 0) {
1364 // Only set this, if it hasn't been set yet.
1365 BuildUniqueOverrider(MD, MD);
1366 if (0)
1367 printf("top set is %s::%s %p\n",
1368 MD->getParent()->getNameAsCString(),
1369 MD->getNameAsCString(),
1370 (void*)MD);
1371 ForwardUnique[MD] = MD;
1372 }
1373 }
1374 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1375 e = RD->bases_end(); i != e; ++i) {
1376 const CXXRecordDecl *Base =
1377 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1378 BuildUniqueOverriders(Base);
1379 }
1380 }
1381
1382 static int DclCmp(const void *p1, const void *p2) {
John McCall7fe0b9e2010-02-13 01:04:05 +00001383 const CXXMethodDecl *MD1 = *(const CXXMethodDecl *const *)p1;
1384 const CXXMethodDecl *MD2 = *(const CXXMethodDecl *const *)p2;
1385
1386 return (DeclarationName::compare(MD1->getDeclName(), MD2->getDeclName()));
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001387 }
1388
1389 void MergeForwarding() {
1390 typedef llvm::SmallVector<const CXXMethodDecl *, 100> A_t;
1391 A_t A;
1392 for (ForwardUnique_t::iterator I = ForwardUnique.begin(),
1393 E = ForwardUnique.end(); I != E; ++I) {
1394 if (I->first == I->second)
1395 // Only add the roots of all trees
1396 A.push_back(I->first);
1397 }
1398 llvm::array_pod_sort(A.begin(), A.end(), DclCmp);
1399 for (A_t::iterator I = A.begin(),
1400 E = A.end(); I != E; ++I) {
1401 A_t::iterator J = I;
John McCall7fe0b9e2010-02-13 01:04:05 +00001402 while (++J != E && DclCmp(I, J) == 0)
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001403 if (DclIsSame(*I, *J)) {
Eli Friedman49677622010-02-13 00:03:21 +00001404 if (0) printf("connecting %s\n", (*I)->getNameAsCString());
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001405 ForwardUnique[*J] = *I;
1406 }
1407 }
1408 }
1409
1410 const CXXMethodDecl *getUnique(const CXXMethodDecl *MD) {
1411 const CXXMethodDecl *U = UniqueOverrider[MD];
1412 assert(U && "unique overrider not found");
1413 while (ForwardUnique.count(U)) {
1414 const CXXMethodDecl *NU = ForwardUnique[U];
1415 if (NU == U) break;
1416 U = NU;
1417 }
1418 return U;
1419 }
Anders Carlsson0f0bbbc2010-01-26 17:36:47 +00001420
1421 GlobalDecl getUnique(GlobalDecl GD) {
1422 const CXXMethodDecl *Unique = getUnique(cast<CXXMethodDecl>(GD.getDecl()));
1423
1424 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Unique))
1425 return GlobalDecl(CD, GD.getCtorType());
1426
1427 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(Unique))
1428 return GlobalDecl(DD, GD.getDtorType());
1429
1430 return Unique;
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001431 }
1432
Anders Carlsson6d4ccb72009-11-26 19:54:33 +00001433 /// getPureVirtualFn - Return the __cxa_pure_virtual function.
1434 llvm::Constant* getPureVirtualFn() {
1435 if (!PureVirtualFn) {
1436 const llvm::FunctionType *Ty =
1437 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
1438 /*isVarArg=*/false);
1439 PureVirtualFn = wrap(CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual"));
1440 }
1441
1442 return PureVirtualFn;
1443 }
1444
Anders Carlssondbd920c2009-10-11 22:13:54 +00001445public:
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001446 OldVtableBuilder(const CXXRecordDecl *MostDerivedClass,
Eli Friedman152b5b12009-12-05 01:05:03 +00001447 const CXXRecordDecl *l, uint64_t lo, CodeGenModule &cgm,
Anders Carlsson1bb60992010-01-14 02:29:07 +00001448 bool build, CGVtableInfo::AddressPointsMapTy& AddressPoints)
Eli Friedman152b5b12009-12-05 01:05:03 +00001449 : BuildVtable(build), MostDerivedClass(MostDerivedClass), LayoutClass(l),
1450 LayoutOffset(lo), BLayout(cgm.getContext().getASTRecordLayout(l)),
1451 rtti(0), VMContext(cgm.getModule().getContext()),CGM(cgm),
1452 PureVirtualFn(0),
Anders Carlssonac3f7bd2009-12-04 18:36:22 +00001453 subAddressPoints(AllocAddressPoint(cgm, l, MostDerivedClass)),
Anders Carlsson1bb60992010-01-14 02:29:07 +00001454 AddressPoints(AddressPoints),
1455 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0))
1456 {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001457 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0);
Anders Carlsson1d7088d2009-12-17 07:09:17 +00001458 if (BuildVtable) {
1459 QualType ClassType = CGM.getContext().getTagDeclType(MostDerivedClass);
1460 rtti = CGM.GetAddrOfRTTIDescriptor(ClassType);
1461 }
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001462 BuildUniqueOverriders(MostDerivedClass);
1463 MergeForwarding();
Anders Carlssondbd920c2009-10-11 22:13:54 +00001464 }
1465
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00001466 // getVtableComponents - Returns a reference to the vtable components.
1467 const VtableComponentsVectorTy &getVtableComponents() const {
1468 return VtableComponents;
Anders Carlsson15318f42009-12-04 16:19:30 +00001469 }
1470
Anders Carlssonc997d422010-01-02 01:01:18 +00001471 llvm::DenseMap<const CXXRecordDecl *, uint64_t> &getVBIndex()
Anders Carlssondbd920c2009-10-11 22:13:54 +00001472 { return VBIndex; }
1473
Eli Friedmanb455f0e2009-12-07 23:56:34 +00001474 SavedAdjustmentsVectorTy &getSavedAdjustments()
1475 { return SavedAdjustments; }
Eli Friedman72649ed2009-12-06 22:01:30 +00001476
Anders Carlssondbd920c2009-10-11 22:13:54 +00001477 llvm::Constant *wrap(Index_t i) {
1478 llvm::Constant *m;
1479 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i);
1480 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty);
1481 }
1482
1483 llvm::Constant *wrap(llvm::Constant *m) {
1484 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty);
1485 }
1486
Mike Stumpd6584902010-01-22 02:51:26 +00001487//#define D1(x)
1488#define D1(X) do { if (getenv("DEBUG")) { X; } } while (0)
Mike Stump6a9612f2009-10-31 20:06:59 +00001489
1490 void GenerateVBaseOffsets(const CXXRecordDecl *RD, uint64_t Offset,
Mike Stumpab28c132009-10-13 22:54:56 +00001491 bool updateVBIndex, Index_t current_vbindex) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001492 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1493 e = RD->bases_end(); i != e; ++i) {
1494 const CXXRecordDecl *Base =
1495 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stumpab28c132009-10-13 22:54:56 +00001496 Index_t next_vbindex = current_vbindex;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001497 if (i->isVirtual() && !SeenVBase.count(Base)) {
1498 SeenVBase.insert(Base);
Mike Stumpab28c132009-10-13 22:54:56 +00001499 if (updateVBIndex) {
Mike Stump6a9612f2009-10-31 20:06:59 +00001500 next_vbindex = (ssize_t)(-(VCalls.size()*LLVMPointerWidth/8)
Mike Stumpab28c132009-10-13 22:54:56 +00001501 - 3*LLVMPointerWidth/8);
1502 VBIndex[Base] = next_vbindex;
1503 }
Mike Stump6a9612f2009-10-31 20:06:59 +00001504 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8;
1505 VCalls.push_back((0?700:0) + BaseOffset);
1506 D1(printf(" vbase for %s at %d delta %d most derived %s\n",
1507 Base->getNameAsCString(),
1508 (int)-VCalls.size()-3, (int)BaseOffset,
Mike Stumpd6584902010-01-22 02:51:26 +00001509 MostDerivedClass->getNameAsCString()));
Anders Carlssondbd920c2009-10-11 22:13:54 +00001510 }
Mike Stumpab28c132009-10-13 22:54:56 +00001511 // We also record offsets for non-virtual bases to closest enclosing
1512 // virtual base. We do this so that we don't have to search
1513 // for the nearst virtual base class when generating thunks.
1514 if (updateVBIndex && VBIndex.count(Base) == 0)
1515 VBIndex[Base] = next_vbindex;
Mike Stump6a9612f2009-10-31 20:06:59 +00001516 GenerateVBaseOffsets(Base, Offset, updateVBIndex, next_vbindex);
Anders Carlssondbd920c2009-10-11 22:13:54 +00001517 }
1518 }
1519
1520 void StartNewTable() {
1521 SeenVBase.clear();
1522 }
1523
Mike Stump3425b972009-10-15 09:30:16 +00001524 Index_t getNVOffset_1(const CXXRecordDecl *D, const CXXRecordDecl *B,
1525 Index_t Offset = 0) {
1526
1527 if (B == D)
1528 return Offset;
1529
1530 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(D);
1531 for (CXXRecordDecl::base_class_const_iterator i = D->bases_begin(),
1532 e = D->bases_end(); i != e; ++i) {
1533 const CXXRecordDecl *Base =
1534 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1535 int64_t BaseOffset = 0;
1536 if (!i->isVirtual())
1537 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
1538 int64_t o = getNVOffset_1(Base, B, BaseOffset);
1539 if (o >= 0)
1540 return o;
1541 }
1542
1543 return -1;
1544 }
1545
1546 /// getNVOffset - Returns the non-virtual offset for the given (B) base of the
1547 /// derived class D.
1548 Index_t getNVOffset(QualType qB, QualType qD) {
Mike Stump9c212892009-11-03 19:03:17 +00001549 qD = qD->getPointeeType();
1550 qB = qB->getPointeeType();
Mike Stump3425b972009-10-15 09:30:16 +00001551 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
1552 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
1553 int64_t o = getNVOffset_1(D, B);
1554 if (o >= 0)
1555 return o;
1556
1557 assert(false && "FIXME: non-virtual base not found");
1558 return 0;
1559 }
1560
Anders Carlssondbd920c2009-10-11 22:13:54 +00001561 /// getVbaseOffset - Returns the index into the vtable for the virtual base
1562 /// offset for the given (B) virtual base of the derived class D.
1563 Index_t getVbaseOffset(QualType qB, QualType qD) {
Mike Stump9c212892009-11-03 19:03:17 +00001564 qD = qD->getPointeeType();
1565 qB = qB->getPointeeType();
Anders Carlssondbd920c2009-10-11 22:13:54 +00001566 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl());
1567 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl());
Anders Carlssonac3f7bd2009-12-04 18:36:22 +00001568 if (D != MostDerivedClass)
Eli Friedman76ed1f72009-11-30 01:19:33 +00001569 return CGM.getVtableInfo().getVirtualBaseOffsetIndex(D, B);
Anders Carlssondbd920c2009-10-11 22:13:54 +00001570 llvm::DenseMap<const CXXRecordDecl *, Index_t>::iterator i;
1571 i = VBIndex.find(B);
1572 if (i != VBIndex.end())
1573 return i->second;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001574
Mike Stumpab28c132009-10-13 22:54:56 +00001575 assert(false && "FIXME: Base not found");
Anders Carlssondbd920c2009-10-11 22:13:54 +00001576 return 0;
1577 }
1578
Eli Friedman367d1222009-12-04 08:40:51 +00001579 bool OverrideMethod(GlobalDecl GD, bool MorallyVirtual,
1580 Index_t OverrideOffset, Index_t Offset,
1581 int64_t CurrentVBaseOffset);
Anders Carlssondbd920c2009-10-11 22:13:54 +00001582
Anders Carlssonadfa2672009-12-04 02:39:04 +00001583 /// AppendMethods - Append the current methods to the vtable.
Anders Carlssonbf540272009-12-04 02:56:03 +00001584 void AppendMethodsToVtable();
Anders Carlssonadfa2672009-12-04 02:39:04 +00001585
Anders Carlssona0fdd912009-11-13 17:08:56 +00001586 llvm::Constant *WrapAddrOf(GlobalDecl GD) {
1587 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1588
Anders Carlssonecf282b2009-11-24 05:08:52 +00001589 const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD);
Mike Stump1ae31782009-10-27 23:36:26 +00001590
Mike Stumpc085a982009-12-03 16:55:20 +00001591 return wrap(CGM.GetAddrOfFunction(GD, Ty));
Mike Stump1ae31782009-10-27 23:36:26 +00001592 }
1593
Mike Stump9e7e3c62009-11-06 23:27:42 +00001594 void OverrideMethods(Path_t *Path, bool MorallyVirtual, int64_t Offset,
1595 int64_t CurrentVBaseOffset) {
Mike Stump11dea942009-10-15 02:04:03 +00001596 for (Path_t::reverse_iterator i = Path->rbegin(),
Anders Carlssondbd920c2009-10-11 22:13:54 +00001597 e = Path->rend(); i != e; ++i) {
1598 const CXXRecordDecl *RD = i->first;
Mike Stump3425b972009-10-15 09:30:16 +00001599 int64_t OverrideOffset = i->second;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001600 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
1601 ++mi) {
Anders Carlssona0fdd912009-11-13 17:08:56 +00001602 const CXXMethodDecl *MD = *mi;
1603
1604 if (!MD->isVirtual())
Anders Carlssondbd920c2009-10-11 22:13:54 +00001605 continue;
1606
Anders Carlssona0fdd912009-11-13 17:08:56 +00001607 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1608 // Override both the complete and the deleting destructor.
1609 GlobalDecl CompDtor(DD, Dtor_Complete);
Eli Friedman367d1222009-12-04 08:40:51 +00001610 OverrideMethod(CompDtor, MorallyVirtual, OverrideOffset, Offset,
1611 CurrentVBaseOffset);
1612
Anders Carlssona0fdd912009-11-13 17:08:56 +00001613 GlobalDecl DeletingDtor(DD, Dtor_Deleting);
Eli Friedman367d1222009-12-04 08:40:51 +00001614 OverrideMethod(DeletingDtor, MorallyVirtual, OverrideOffset, Offset,
1615 CurrentVBaseOffset);
Anders Carlssona0fdd912009-11-13 17:08:56 +00001616 } else {
Eli Friedman367d1222009-12-04 08:40:51 +00001617 OverrideMethod(MD, MorallyVirtual, OverrideOffset, Offset,
1618 CurrentVBaseOffset);
Anders Carlssona0fdd912009-11-13 17:08:56 +00001619 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00001620 }
1621 }
1622 }
1623
Anders Carlssona0fdd912009-11-13 17:08:56 +00001624 void AddMethod(const GlobalDecl GD, bool MorallyVirtual, Index_t Offset,
Eli Friedman76ed1f72009-11-30 01:19:33 +00001625 int64_t CurrentVBaseOffset) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001626 // If we can find a previously allocated slot for this, reuse it.
Eli Friedman367d1222009-12-04 08:40:51 +00001627 if (OverrideMethod(GD, MorallyVirtual, Offset, Offset,
Mike Stump9e7e3c62009-11-06 23:27:42 +00001628 CurrentVBaseOffset))
Anders Carlssondbd920c2009-10-11 22:13:54 +00001629 return;
1630
Mike Stump372ade22010-01-22 18:48:47 +00001631 D1(printf(" vfn for %s at %d\n",
1632 dyn_cast<CXXMethodDecl>(GD.getDecl())->getNameAsCString(),
1633 (int)Methods.size()));
1634
Anders Carlssona7f19112009-12-04 02:08:24 +00001635 // We didn't find an entry in the vtable that we could use, add a new
1636 // entry.
1637 Methods.AddMethod(GD);
1638
Mike Stump15189fb2010-01-26 21:35:27 +00001639 VCallOffset[GD] = Offset/8 - CurrentVBaseOffset/8;
1640
Anders Carlssondbd920c2009-10-11 22:13:54 +00001641 if (MorallyVirtual) {
Anders Carlsson0f0bbbc2010-01-26 17:36:47 +00001642 GlobalDecl UGD = getUnique(GD);
1643 const CXXMethodDecl *UMD = cast<CXXMethodDecl>(UGD.getDecl());
1644
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001645 assert(UMD && "final overrider not found");
1646
1647 Index_t &idx = VCall[UMD];
Anders Carlssondbd920c2009-10-11 22:13:54 +00001648 // Allocate the first one, after that, we reuse the previous one.
1649 if (idx == 0) {
Anders Carlsson0f0bbbc2010-01-26 17:36:47 +00001650 VCallOffsetForVCall[UGD] = Offset/8;
Mike Stump15189fb2010-01-26 21:35:27 +00001651 NonVirtualOffset[UMD] = Offset/8 - CurrentVBaseOffset/8;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001652 idx = VCalls.size()+1;
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001653 VCalls.push_back(Offset/8 - CurrentVBaseOffset/8);
Mike Stump6a9612f2009-10-31 20:06:59 +00001654 D1(printf(" vcall for %s at %d with delta %d\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001655 dyn_cast<CXXMethodDecl>(GD.getDecl())->getNameAsCString(),
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00001656 (int)-VCalls.size()-3, (int)VCalls[idx-1]));
Anders Carlssondbd920c2009-10-11 22:13:54 +00001657 }
1658 }
1659 }
1660
1661 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual,
Eli Friedman76ed1f72009-11-30 01:19:33 +00001662 Index_t Offset, int64_t CurrentVBaseOffset) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001663 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me;
Anders Carlssona0fdd912009-11-13 17:08:56 +00001664 ++mi) {
1665 const CXXMethodDecl *MD = *mi;
1666 if (!MD->isVirtual())
1667 continue;
1668
1669 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1670 // For destructors, add both the complete and the deleting destructor
1671 // to the vtable.
1672 AddMethod(GlobalDecl(DD, Dtor_Complete), MorallyVirtual, Offset,
Mike Stump9e7e3c62009-11-06 23:27:42 +00001673 CurrentVBaseOffset);
Eli Friedman76ed1f72009-11-30 01:19:33 +00001674 AddMethod(GlobalDecl(DD, Dtor_Deleting), MorallyVirtual, Offset,
1675 CurrentVBaseOffset);
1676 } else
1677 AddMethod(MD, MorallyVirtual, Offset, CurrentVBaseOffset);
Anders Carlssona0fdd912009-11-13 17:08:56 +00001678 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00001679 }
1680
1681 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout,
1682 const CXXRecordDecl *PrimaryBase,
1683 bool PrimaryBaseWasVirtual, bool MorallyVirtual,
Mike Stump9e7e3c62009-11-06 23:27:42 +00001684 int64_t Offset, int64_t CurrentVBaseOffset,
1685 Path_t *Path) {
Mike Stump11dea942009-10-15 02:04:03 +00001686 Path->push_back(std::make_pair(RD, Offset));
Anders Carlssondbd920c2009-10-11 22:13:54 +00001687 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1688 e = RD->bases_end(); i != e; ++i) {
1689 if (i->isVirtual())
1690 continue;
1691 const CXXRecordDecl *Base =
1692 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001693 uint64_t o = Offset + Layout.getBaseClassOffset(Base);
1694 StartNewTable();
1695 GenerateVtableForBase(Base, o, MorallyVirtual, false,
1696 true, Base == PrimaryBase && !PrimaryBaseWasVirtual,
1697 CurrentVBaseOffset, Path);
Anders Carlssondbd920c2009-10-11 22:13:54 +00001698 }
Mike Stump11dea942009-10-15 02:04:03 +00001699 Path->pop_back();
Anders Carlssondbd920c2009-10-11 22:13:54 +00001700 }
1701
Mike Stump0ca42792009-10-14 18:14:51 +00001702// #define D(X) do { X; } while (0)
1703#define D(X)
1704
1705 void insertVCalls(int InsertionPoint) {
Mike Stump6a9612f2009-10-31 20:06:59 +00001706 D1(printf("============= combining vbase/vcall\n"));
Mike Stump0ca42792009-10-14 18:14:51 +00001707 D(VCalls.insert(VCalls.begin(), 673));
1708 D(VCalls.push_back(672));
Eli Friedman152b5b12009-12-05 01:05:03 +00001709
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00001710 VtableComponents.insert(VtableComponents.begin() + InsertionPoint,
1711 VCalls.size(), 0);
Eli Friedman152b5b12009-12-05 01:05:03 +00001712 if (BuildVtable) {
1713 // The vcalls come first...
1714 for (std::vector<Index_t>::reverse_iterator i = VCalls.rbegin(),
1715 e = VCalls.rend();
1716 i != e; ++i)
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00001717 VtableComponents[InsertionPoint++] = wrap((0?600:0) + *i);
Eli Friedman152b5b12009-12-05 01:05:03 +00001718 }
Mike Stump0ca42792009-10-14 18:14:51 +00001719 VCalls.clear();
Mike Stumpfbfb52d2009-11-10 02:30:51 +00001720 VCall.clear();
Mike Stump15189fb2010-01-26 21:35:27 +00001721 VCallOffsetForVCall.clear();
1722 VCallOffset.clear();
1723 NonVirtualOffset.clear();
Mike Stump0ca42792009-10-14 18:14:51 +00001724 }
1725
Mike Stump65d0e282009-11-13 23:13:20 +00001726 void AddAddressPoints(const CXXRecordDecl *RD, uint64_t Offset,
1727 Index_t AddressPoint) {
1728 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001729 RD->getNameAsCString(), MostDerivedClass->getNameAsCString(),
Mike Stump65d0e282009-11-13 23:13:20 +00001730 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stump23a35422009-11-19 20:52:19 +00001731 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Anders Carlsson1bb60992010-01-14 02:29:07 +00001732 AddressPoints[BaseSubobject(RD, Offset)] = AddressPoint;
Mike Stump65d0e282009-11-13 23:13:20 +00001733
1734 // Now also add the address point for all our primary bases.
1735 while (1) {
1736 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1737 RD = Layout.getPrimaryBase();
1738 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1739 // FIXME: Double check this.
1740 if (RD == 0)
1741 break;
1742 if (PrimaryBaseWasVirtual &&
1743 BLayout.getVBaseClassOffset(RD) != Offset)
1744 break;
1745 D1(printf("XXX address point for %s in %s layout %s at offset %d is %d\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001746 RD->getNameAsCString(), MostDerivedClass->getNameAsCString(),
Mike Stump65d0e282009-11-13 23:13:20 +00001747 LayoutClass->getNameAsCString(), (int)Offset, (int)AddressPoint));
Mike Stump23a35422009-11-19 20:52:19 +00001748 subAddressPoints[std::make_pair(RD, Offset)] = AddressPoint;
Anders Carlsson1bb60992010-01-14 02:29:07 +00001749 AddressPoints[BaseSubobject(RD, Offset)] = AddressPoint;
Mike Stump65d0e282009-11-13 23:13:20 +00001750 }
1751 }
1752
1753
Mike Stump8cc4f102009-12-24 07:29:41 +00001754 void FinishGenerateVtable(const CXXRecordDecl *RD,
1755 const ASTRecordLayout &Layout,
1756 const CXXRecordDecl *PrimaryBase,
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001757 bool ForNPNVBases, bool WasPrimaryBase,
Mike Stump8cc4f102009-12-24 07:29:41 +00001758 bool PrimaryBaseWasVirtual,
1759 bool MorallyVirtual, int64_t Offset,
1760 bool ForVirtualBase, int64_t CurrentVBaseOffset,
1761 Path_t *Path) {
Mike Stump11dea942009-10-15 02:04:03 +00001762 bool alloc = false;
1763 if (Path == 0) {
1764 alloc = true;
1765 Path = new Path_t;
1766 }
1767
Anders Carlssondbd920c2009-10-11 22:13:54 +00001768 StartNewTable();
1769 extra = 0;
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001770 Index_t AddressPoint = 0;
1771 int VCallInsertionPoint = 0;
1772 if (!ForNPNVBases || !WasPrimaryBase) {
1773 bool DeferVCalls = MorallyVirtual || ForVirtualBase;
1774 VCallInsertionPoint = VtableComponents.size();
1775 if (!DeferVCalls) {
1776 insertVCalls(VCallInsertionPoint);
1777 } else
1778 // FIXME: just for extra, or for all uses of VCalls.size post this?
1779 extra = -VCalls.size();
Anders Carlssondbd920c2009-10-11 22:13:54 +00001780
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001781 // Add the offset to top.
1782 VtableComponents.push_back(BuildVtable ? wrap(-((Offset-LayoutOffset)/8)) : 0);
Anders Carlsson15318f42009-12-04 16:19:30 +00001783
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001784 // Add the RTTI information.
1785 VtableComponents.push_back(rtti);
Anders Carlsson15318f42009-12-04 16:19:30 +00001786
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001787 AddressPoint = VtableComponents.size();
Anders Carlssondbd920c2009-10-11 22:13:54 +00001788
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001789 AppendMethodsToVtable();
1790 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00001791
1792 // and then the non-virtual bases.
1793 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual,
Mike Stump9e7e3c62009-11-06 23:27:42 +00001794 MorallyVirtual, Offset, CurrentVBaseOffset, Path);
Mike Stump0ca42792009-10-14 18:14:51 +00001795
1796 if (ForVirtualBase) {
Mike Stump9840c702009-11-12 20:47:57 +00001797 // FIXME: We're adding to VCalls in callers, we need to do the overrides
1798 // in the inner part, so that we know the complete set of vcalls during
1799 // the build and don't have to insert into methods. Saving out the
1800 // AddressPoint here, would need to be fixed, if we didn't do that. Also
1801 // retroactively adding vcalls for overrides later wind up in the wrong
1802 // place, the vcall slot has to be alloted during the walk of the base
1803 // when the function is first introduces.
Mike Stump0ca42792009-10-14 18:14:51 +00001804 AddressPoint += VCalls.size();
Mike Stump9840c702009-11-12 20:47:57 +00001805 insertVCalls(VCallInsertionPoint);
Mike Stump0ca42792009-10-14 18:14:51 +00001806 }
1807
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001808 if (!ForNPNVBases || !WasPrimaryBase)
1809 AddAddressPoints(RD, Offset, AddressPoint);
Mike Stump9840c702009-11-12 20:47:57 +00001810
Mike Stump11dea942009-10-15 02:04:03 +00001811 if (alloc) {
1812 delete Path;
1813 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00001814 }
1815
Mike Stump6a9612f2009-10-31 20:06:59 +00001816 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
1817 bool updateVBIndex, Index_t current_vbindex,
Eli Friedman76ed1f72009-11-30 01:19:33 +00001818 int64_t CurrentVBaseOffset) {
Mike Stump6a9612f2009-10-31 20:06:59 +00001819 if (!RD->isDynamicClass())
1820 return;
1821
1822 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1823 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1824 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1825
1826 // vtables are composed from the chain of primaries.
Eli Friedmandfe33bb2009-12-04 08:52:11 +00001827 if (PrimaryBase && !PrimaryBaseWasVirtual) {
Mike Stump6a9612f2009-10-31 20:06:59 +00001828 D1(printf(" doing primaries for %s most derived %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001829 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Eli Friedmandfe33bb2009-12-04 08:52:11 +00001830 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
1831 updateVBIndex, current_vbindex, CurrentVBaseOffset);
Mike Stump6a9612f2009-10-31 20:06:59 +00001832 }
1833
1834 D1(printf(" doing vcall entries for %s most derived %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001835 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Mike Stump6a9612f2009-10-31 20:06:59 +00001836
1837 // And add the virtuals for the class to the primary vtable.
Eli Friedman76ed1f72009-11-30 01:19:33 +00001838 AddMethods(RD, MorallyVirtual, Offset, CurrentVBaseOffset);
Mike Stump6a9612f2009-10-31 20:06:59 +00001839 }
1840
1841 void VBPrimaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset,
1842 bool updateVBIndex, Index_t current_vbindex,
Mike Stump9e7e3c62009-11-06 23:27:42 +00001843 bool RDisVirtualBase, int64_t CurrentVBaseOffset,
Eli Friedman76ed1f72009-11-30 01:19:33 +00001844 bool bottom) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001845 if (!RD->isDynamicClass())
1846 return;
1847
1848 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1849 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1850 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1851
1852 // vtables are composed from the chain of primaries.
1853 if (PrimaryBase) {
Mike Stump9e7e3c62009-11-06 23:27:42 +00001854 int BaseCurrentVBaseOffset = CurrentVBaseOffset;
1855 if (PrimaryBaseWasVirtual) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001856 IndirectPrimary.insert(PrimaryBase);
Mike Stump9e7e3c62009-11-06 23:27:42 +00001857 BaseCurrentVBaseOffset = BLayout.getVBaseClassOffset(PrimaryBase);
1858 }
Mike Stump6a9612f2009-10-31 20:06:59 +00001859
1860 D1(printf(" doing primaries for %s most derived %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001861 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Mike Stump6a9612f2009-10-31 20:06:59 +00001862
1863 VBPrimaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset,
Mike Stump9e7e3c62009-11-06 23:27:42 +00001864 updateVBIndex, current_vbindex, PrimaryBaseWasVirtual,
Eli Friedman76ed1f72009-11-30 01:19:33 +00001865 BaseCurrentVBaseOffset, false);
Anders Carlssondbd920c2009-10-11 22:13:54 +00001866 }
1867
Mike Stump6a9612f2009-10-31 20:06:59 +00001868 D1(printf(" doing vbase entries for %s most derived %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001869 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Mike Stump6a9612f2009-10-31 20:06:59 +00001870 GenerateVBaseOffsets(RD, Offset, updateVBIndex, current_vbindex);
1871
1872 if (RDisVirtualBase || bottom) {
1873 Primaries(RD, MorallyVirtual, Offset, updateVBIndex, current_vbindex,
Eli Friedman76ed1f72009-11-30 01:19:33 +00001874 CurrentVBaseOffset);
Mike Stump6a9612f2009-10-31 20:06:59 +00001875 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00001876 }
1877
Mike Stump8cc4f102009-12-24 07:29:41 +00001878 void GenerateVtableForBase(const CXXRecordDecl *RD, int64_t Offset = 0,
1879 bool MorallyVirtual = false,
1880 bool ForVirtualBase = false,
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001881 bool ForNPNVBases = false,
1882 bool WasPrimaryBase = true,
Mike Stump8cc4f102009-12-24 07:29:41 +00001883 int CurrentVBaseOffset = 0,
1884 Path_t *Path = 0) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001885 if (!RD->isDynamicClass())
Mike Stump8cc4f102009-12-24 07:29:41 +00001886 return;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001887
Mike Stump92774d12009-11-13 02:35:38 +00001888 // Construction vtable don't need parts that have no virtual bases and
1889 // aren't morally virtual.
Anders Carlssonac3f7bd2009-12-04 18:36:22 +00001890 if ((LayoutClass != MostDerivedClass) &&
1891 RD->getNumVBases() == 0 && !MorallyVirtual)
Mike Stump8cc4f102009-12-24 07:29:41 +00001892 return;
Mike Stump92774d12009-11-13 02:35:38 +00001893
Anders Carlssondbd920c2009-10-11 22:13:54 +00001894 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1895 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1896 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual();
1897
Anders Carlssondbd920c2009-10-11 22:13:54 +00001898 extra = 0;
Mike Stump6a9612f2009-10-31 20:06:59 +00001899 D1(printf("building entries for base %s most derived %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001900 RD->getNameAsCString(), MostDerivedClass->getNameAsCString()));
Anders Carlssondbd920c2009-10-11 22:13:54 +00001901
Mike Stump6a9612f2009-10-31 20:06:59 +00001902 if (ForVirtualBase)
1903 extra = VCalls.size();
1904
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001905 if (!ForNPNVBases || !WasPrimaryBase) {
1906 VBPrimaries(RD, MorallyVirtual, Offset, !ForVirtualBase, 0,
1907 ForVirtualBase, CurrentVBaseOffset, true);
Anders Carlssondbd920c2009-10-11 22:13:54 +00001908
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001909 if (Path)
1910 OverrideMethods(Path, MorallyVirtual, Offset, CurrentVBaseOffset);
1911 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00001912
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001913 FinishGenerateVtable(RD, Layout, PrimaryBase, ForNPNVBases, WasPrimaryBase,
1914 PrimaryBaseWasVirtual, MorallyVirtual, Offset,
1915 ForVirtualBase, CurrentVBaseOffset, Path);
Anders Carlssondbd920c2009-10-11 22:13:54 +00001916 }
1917
1918 void GenerateVtableForVBases(const CXXRecordDecl *RD,
1919 int64_t Offset = 0,
Mike Stump11dea942009-10-15 02:04:03 +00001920 Path_t *Path = 0) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001921 bool alloc = false;
1922 if (Path == 0) {
1923 alloc = true;
Mike Stump11dea942009-10-15 02:04:03 +00001924 Path = new Path_t;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001925 }
1926 // FIXME: We also need to override using all paths to a virtual base,
1927 // right now, we just process the first path
1928 Path->push_back(std::make_pair(RD, Offset));
1929 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
1930 e = RD->bases_end(); i != e; ++i) {
1931 const CXXRecordDecl *Base =
1932 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
1933 if (i->isVirtual() && !IndirectPrimary.count(Base)) {
1934 // Mark it so we don't output it twice.
1935 IndirectPrimary.insert(Base);
1936 StartNewTable();
Mike Stump0ca42792009-10-14 18:14:51 +00001937 VCall.clear();
Anders Carlssondbd920c2009-10-11 22:13:54 +00001938 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump9e7e3c62009-11-06 23:27:42 +00001939 int64_t CurrentVBaseOffset = BaseOffset;
Mike Stump6a9612f2009-10-31 20:06:59 +00001940 D1(printf("vtable %s virtual base %s\n",
Mike Stumpd6584902010-01-22 02:51:26 +00001941 MostDerivedClass->getNameAsCString(), Base->getNameAsCString()));
Mike Stumpfc9f16c2010-01-22 06:45:05 +00001942 GenerateVtableForBase(Base, BaseOffset, true, true, false,
1943 true, CurrentVBaseOffset, Path);
Anders Carlssondbd920c2009-10-11 22:13:54 +00001944 }
Mike Stump9840c702009-11-12 20:47:57 +00001945 int64_t BaseOffset;
Anders Carlssondbd920c2009-10-11 22:13:54 +00001946 if (i->isVirtual())
1947 BaseOffset = BLayout.getVBaseClassOffset(Base);
Mike Stump9840c702009-11-12 20:47:57 +00001948 else {
1949 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1950 BaseOffset = Offset + Layout.getBaseClassOffset(Base);
1951 }
1952
Mike Stump11dea942009-10-15 02:04:03 +00001953 if (Base->getNumVBases()) {
Anders Carlssondbd920c2009-10-11 22:13:54 +00001954 GenerateVtableForVBases(Base, BaseOffset, Path);
Mike Stump11dea942009-10-15 02:04:03 +00001955 }
Anders Carlssondbd920c2009-10-11 22:13:54 +00001956 }
1957 Path->pop_back();
1958 if (alloc)
1959 delete Path;
1960 }
1961};
Anders Carlsson27682a32009-12-03 01:54:02 +00001962} // end anonymous namespace
1963
Anders Carlsson824d7ea2010-02-11 08:02:13 +00001964bool OldVtableBuilder::OverrideMethod(GlobalDecl GD, bool MorallyVirtual,
Eli Friedman367d1222009-12-04 08:40:51 +00001965 Index_t OverrideOffset, Index_t Offset,
1966 int64_t CurrentVBaseOffset) {
Anders Carlsson27682a32009-12-03 01:54:02 +00001967 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
1968
1969 const bool isPure = MD->isPure();
Anders Carlsson7ca46432009-12-05 17:04:47 +00001970
Anders Carlsson27682a32009-12-03 01:54:02 +00001971 // FIXME: Should OverrideOffset's be Offset?
1972
Anders Carlsson7ca46432009-12-05 17:04:47 +00001973 for (CXXMethodDecl::method_iterator mi = MD->begin_overridden_methods(),
1974 e = MD->end_overridden_methods(); mi != e; ++mi) {
Anders Carlsson27682a32009-12-03 01:54:02 +00001975 GlobalDecl OGD;
Mike Stump15189fb2010-01-26 21:35:27 +00001976 GlobalDecl OGD2;
Anders Carlsson27682a32009-12-03 01:54:02 +00001977
Anders Carlsson3aaf4862009-12-04 05:51:56 +00001978 const CXXMethodDecl *OMD = *mi;
Anders Carlsson27682a32009-12-03 01:54:02 +00001979 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(OMD))
1980 OGD = GlobalDecl(DD, GD.getDtorType());
1981 else
1982 OGD = OMD;
Eli Friedman47145832009-12-04 08:34:14 +00001983
Eli Friedman72649ed2009-12-06 22:01:30 +00001984 // Check whether this is the method being overridden in this section of
1985 // the vtable.
Anders Carlsson77c23e52009-12-04 15:49:02 +00001986 uint64_t Index;
1987 if (!Methods.getIndex(OGD, Index))
Eli Friedman47145832009-12-04 08:34:14 +00001988 continue;
1989
Mike Stump15189fb2010-01-26 21:35:27 +00001990 OGD2 = OGD;
1991
Eli Friedman72649ed2009-12-06 22:01:30 +00001992 // Get the original method, which we should be computing thunks, etc,
1993 // against.
1994 OGD = Methods.getOrigMethod(Index);
1995 OMD = cast<CXXMethodDecl>(OGD.getDecl());
1996
Eli Friedman47145832009-12-04 08:34:14 +00001997 QualType ReturnType =
1998 MD->getType()->getAs<FunctionType>()->getResultType();
1999 QualType OverriddenReturnType =
2000 OMD->getType()->getAs<FunctionType>()->getResultType();
Anders Carlsson27682a32009-12-03 01:54:02 +00002001
Eli Friedman47145832009-12-04 08:34:14 +00002002 // Check if we need a return type adjustment.
2003 if (TypeConversionRequiresAdjustment(CGM.getContext(), ReturnType,
2004 OverriddenReturnType)) {
2005 CanQualType &BaseReturnType = BaseReturnTypes[Index];
Anders Carlsson27682a32009-12-03 01:54:02 +00002006
Eli Friedman47145832009-12-04 08:34:14 +00002007 // Insert the base return type.
2008 if (BaseReturnType.isNull())
2009 BaseReturnType =
2010 CGM.getContext().getCanonicalType(OverriddenReturnType);
2011 }
Anders Carlssondd454be2009-12-04 03:52:52 +00002012
Eli Friedman47145832009-12-04 08:34:14 +00002013 Methods.OverrideMethod(OGD, GD);
2014
Anders Carlsson0f0bbbc2010-01-26 17:36:47 +00002015 GlobalDecl UGD = getUnique(GD);
2016 const CXXMethodDecl *UMD = cast<CXXMethodDecl>(UGD.getDecl());
2017 assert(UGD.getDecl() && "unique overrider not found");
2018 assert(UGD == getUnique(OGD) && "unique overrider not unique");
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002019
Eli Friedman47145832009-12-04 08:34:14 +00002020 ThisAdjustments.erase(Index);
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002021 if (MorallyVirtual || VCall.count(UMD)) {
Mike Stump15189fb2010-01-26 21:35:27 +00002022
Mike Stumpcf0b9cc2010-01-26 00:05:04 +00002023 Index_t &idx = VCall[UMD];
Eli Friedman47145832009-12-04 08:34:14 +00002024 if (idx == 0) {
Mike Stump15189fb2010-01-26 21:35:27 +00002025 VCallOffset[GD] = VCallOffset[OGD];
2026 // NonVirtualOffset[UMD] = CurrentVBaseOffset/8 - OverrideOffset/8;
2027 NonVirtualOffset[UMD] = VCallOffset[OGD];
Mike Stumpd99a4d22010-01-26 03:42:22 +00002028 VCallOffsetForVCall[UMD] = OverrideOffset/8;
Eli Friedman47145832009-12-04 08:34:14 +00002029 idx = VCalls.size()+1;
Mike Stumpd99a4d22010-01-26 03:42:22 +00002030 VCalls.push_back(OverrideOffset/8 - CurrentVBaseOffset/8);
Eli Friedman47145832009-12-04 08:34:14 +00002031 D1(printf(" vcall for %s at %d with delta %d most derived %s\n",
2032 MD->getNameAsString().c_str(), (int)-idx-3,
Mike Stumpd6584902010-01-22 02:51:26 +00002033 (int)VCalls[idx-1], MostDerivedClass->getNameAsCString()));
Eli Friedman47145832009-12-04 08:34:14 +00002034 } else {
Mike Stump15189fb2010-01-26 21:35:27 +00002035 VCallOffset[GD] = NonVirtualOffset[UMD];
Anders Carlsson0f0bbbc2010-01-26 17:36:47 +00002036 VCalls[idx-1] = -VCallOffsetForVCall[UGD] + OverrideOffset/8;
Eli Friedman47145832009-12-04 08:34:14 +00002037 D1(printf(" vcall patch for %s at %d with delta %d most derived %s\n",
2038 MD->getNameAsString().c_str(), (int)-idx-3,
Mike Stumpd6584902010-01-22 02:51:26 +00002039 (int)VCalls[idx-1], MostDerivedClass->getNameAsCString()));
Eli Friedman47145832009-12-04 08:34:14 +00002040 }
Mike Stump15189fb2010-01-26 21:35:27 +00002041 int64_t NonVirtualAdjustment = -VCallOffset[OGD];
Mike Stump852f5ce2010-01-26 22:44:01 +00002042 QualType DerivedType = MD->getThisType(CGM.getContext());
2043 QualType BaseType = cast<const CXXMethodDecl>(OGD.getDecl())->getThisType(CGM.getContext());
2044 int64_t NonVirtualAdjustment2 = -(getNVOffset(BaseType, DerivedType)/8);
2045 if (NonVirtualAdjustment2 != NonVirtualAdjustment) {
2046 NonVirtualAdjustment = NonVirtualAdjustment2;
2047 }
Eli Friedman47145832009-12-04 08:34:14 +00002048 int64_t VirtualAdjustment =
2049 -((idx + extra + 2) * LLVMPointerWidth / 8);
Anders Carlsson891bb4b2009-12-03 02:32:59 +00002050
Eli Friedman47145832009-12-04 08:34:14 +00002051 // Optimize out virtual adjustments of 0.
2052 if (VCalls[idx-1] == 0)
2053 VirtualAdjustment = 0;
Anders Carlsson891bb4b2009-12-03 02:32:59 +00002054
Eli Friedman47145832009-12-04 08:34:14 +00002055 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment,
2056 VirtualAdjustment);
Anders Carlssonbc0e3392009-12-03 02:22:59 +00002057
Eli Friedman72649ed2009-12-06 22:01:30 +00002058 if (!isPure && !ThisAdjustment.isEmpty()) {
Eli Friedman47145832009-12-04 08:34:14 +00002059 ThisAdjustments[Index] = ThisAdjustment;
Eli Friedmanb455f0e2009-12-07 23:56:34 +00002060 SavedAdjustments.push_back(
2061 std::make_pair(GD, std::make_pair(OGD, ThisAdjustment)));
Eli Friedman72649ed2009-12-06 22:01:30 +00002062 }
Anders Carlsson27682a32009-12-03 01:54:02 +00002063 return true;
2064 }
Eli Friedman47145832009-12-04 08:34:14 +00002065
Mike Stump15189fb2010-01-26 21:35:27 +00002066 VCallOffset[GD] = VCallOffset[OGD2] - OverrideOffset/8;
Eli Friedman47145832009-12-04 08:34:14 +00002067
Mike Stump15189fb2010-01-26 21:35:27 +00002068 int64_t NonVirtualAdjustment = -VCallOffset[GD];
2069 QualType DerivedType = MD->getThisType(CGM.getContext());
2070 QualType BaseType = cast<const CXXMethodDecl>(OGD.getDecl())->getThisType(CGM.getContext());
2071 int64_t NonVirtualAdjustment2 = -(getNVOffset(BaseType, DerivedType)/8);
2072 if (NonVirtualAdjustment2 != NonVirtualAdjustment) {
2073 NonVirtualAdjustment = NonVirtualAdjustment2;
2074 }
2075
Eli Friedman47145832009-12-04 08:34:14 +00002076 if (NonVirtualAdjustment) {
2077 ThunkAdjustment ThisAdjustment(NonVirtualAdjustment, 0);
2078
Eli Friedmanf062d9d2009-12-06 22:33:51 +00002079 if (!isPure) {
Eli Friedman47145832009-12-04 08:34:14 +00002080 ThisAdjustments[Index] = ThisAdjustment;
Eli Friedmanb455f0e2009-12-07 23:56:34 +00002081 SavedAdjustments.push_back(
2082 std::make_pair(GD, std::make_pair(OGD, ThisAdjustment)));
Eli Friedmanf062d9d2009-12-06 22:33:51 +00002083 }
Eli Friedman47145832009-12-04 08:34:14 +00002084 }
2085 return true;
Anders Carlsson27682a32009-12-03 01:54:02 +00002086 }
2087
2088 return false;
Anders Carlsson27f69d02009-11-27 22:21:51 +00002089}
2090
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002091void OldVtableBuilder::AppendMethodsToVtable() {
Eli Friedman152b5b12009-12-05 01:05:03 +00002092 if (!BuildVtable) {
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00002093 VtableComponents.insert(VtableComponents.end(), Methods.size(),
2094 (llvm::Constant *)0);
Eli Friedman152b5b12009-12-05 01:05:03 +00002095 ThisAdjustments.clear();
2096 BaseReturnTypes.clear();
2097 Methods.clear();
2098 return;
2099 }
2100
Anders Carlsson15318f42009-12-04 16:19:30 +00002101 // Reserve room in the vtable for our new methods.
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00002102 VtableComponents.reserve(VtableComponents.size() + Methods.size());
Anders Carlsson29202d52009-12-04 03:07:26 +00002103
Anders Carlssonb73ba392009-12-04 02:43:50 +00002104 for (unsigned i = 0, e = Methods.size(); i != e; ++i) {
2105 GlobalDecl GD = Methods[i];
Anders Carlssonea357222009-12-04 02:52:22 +00002106 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2107
2108 // Get the 'this' pointer adjustment.
Anders Carlssonb73ba392009-12-04 02:43:50 +00002109 ThunkAdjustment ThisAdjustment = ThisAdjustments.lookup(i);
Anders Carlssonea357222009-12-04 02:52:22 +00002110
2111 // Construct the return type adjustment.
2112 ThunkAdjustment ReturnAdjustment;
2113
2114 QualType BaseReturnType = BaseReturnTypes.lookup(i);
2115 if (!BaseReturnType.isNull() && !MD->isPure()) {
2116 QualType DerivedType =
2117 MD->getType()->getAs<FunctionType>()->getResultType();
2118
2119 int64_t NonVirtualAdjustment =
2120 getNVOffset(BaseReturnType, DerivedType) / 8;
2121
2122 int64_t VirtualAdjustment =
2123 getVbaseOffset(BaseReturnType, DerivedType);
2124
2125 ReturnAdjustment = ThunkAdjustment(NonVirtualAdjustment,
2126 VirtualAdjustment);
2127 }
2128
Anders Carlsson2fce2162009-12-04 03:06:03 +00002129 llvm::Constant *Method = 0;
Anders Carlssonea357222009-12-04 02:52:22 +00002130 if (!ReturnAdjustment.isEmpty()) {
2131 // Build a covariant thunk.
2132 CovariantThunkAdjustment Adjustment(ThisAdjustment, ReturnAdjustment);
Eli Friedman72649ed2009-12-06 22:01:30 +00002133 Method = wrap(CGM.GetAddrOfCovariantThunk(GD, Adjustment));
Anders Carlssonea357222009-12-04 02:52:22 +00002134 } else if (!ThisAdjustment.isEmpty()) {
2135 // Build a "regular" thunk.
Eli Friedman72649ed2009-12-06 22:01:30 +00002136 Method = wrap(CGM.GetAddrOfThunk(GD, ThisAdjustment));
Anders Carlssonbf540272009-12-04 02:56:03 +00002137 } else if (MD->isPure()) {
2138 // We have a pure virtual method.
Anders Carlsson2fce2162009-12-04 03:06:03 +00002139 Method = getPureVirtualFn();
2140 } else {
2141 // We have a good old regular method.
2142 Method = WrapAddrOf(GD);
Anders Carlssonea357222009-12-04 02:52:22 +00002143 }
Anders Carlsson2fce2162009-12-04 03:06:03 +00002144
2145 // Add the method to the vtable.
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00002146 VtableComponents.push_back(Method);
Anders Carlssonb73ba392009-12-04 02:43:50 +00002147 }
2148
Anders Carlsson2fce2162009-12-04 03:06:03 +00002149
Anders Carlssonb73ba392009-12-04 02:43:50 +00002150 ThisAdjustments.clear();
Anders Carlssonea357222009-12-04 02:52:22 +00002151 BaseReturnTypes.clear();
Anders Carlssonb73ba392009-12-04 02:43:50 +00002152
Anders Carlssonadfa2672009-12-04 02:39:04 +00002153 Methods.clear();
Anders Carlssonadfa2672009-12-04 02:39:04 +00002154}
2155
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002156void CGVtableInfo::ComputeMethodVtableIndices(const CXXRecordDecl *RD) {
2157
2158 // Itanium C++ ABI 2.5.2:
Anders Carlsson45147d02010-02-02 03:37:46 +00002159 // The order of the virtual function pointers in a virtual table is the
2160 // order of declaration of the corresponding member functions in the class.
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002161 //
Anders Carlsson45147d02010-02-02 03:37:46 +00002162 // There is an entry for any virtual function declared in a class,
2163 // whether it is a new function or overrides a base class function,
2164 // unless it overrides a function from the primary base, and conversion
2165 // between their return types does not require an adjustment.
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002166
2167 int64_t CurrentIndex = 0;
2168
2169 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
2170 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
2171
2172 if (PrimaryBase) {
Anders Carlsson0121fbd2009-11-30 19:43:26 +00002173 assert(PrimaryBase->isDefinition() &&
2174 "Should have the definition decl of the primary base!");
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002175
2176 // Since the record decl shares its vtable pointer with the primary base
2177 // we need to start counting at the end of the primary base's vtable.
2178 CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase);
2179 }
Eli Friedmanea5ae312009-12-15 03:31:17 +00002180
2181 // Collect all the primary bases, so we can check whether methods override
2182 // a method from the base.
Anders Carlsson57071e22010-02-12 05:25:12 +00002183 VtableBuilder::PrimaryBasesSetTy PrimaryBases;
Eli Friedmanea5ae312009-12-15 03:31:17 +00002184 for (ASTRecordLayout::primary_base_info_iterator
2185 I = Layout.primary_base_begin(), E = Layout.primary_base_end();
2186 I != E; ++I)
2187 PrimaryBases.insert((*I).getBase());
2188
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002189 const CXXDestructorDecl *ImplicitVirtualDtor = 0;
2190
2191 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
2192 e = RD->method_end(); i != e; ++i) {
2193 const CXXMethodDecl *MD = *i;
2194
2195 // We only want virtual methods.
2196 if (!MD->isVirtual())
2197 continue;
2198
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002199 // Check if this method overrides a method in the primary base.
Anders Carlsson57071e22010-02-12 05:25:12 +00002200 if (const CXXMethodDecl *OverriddenMD =
2201 OverridesMethodInPrimaryBase(MD, PrimaryBases)) {
2202 // Check if converting from the return type of the method to the
2203 // return type of the overridden method requires conversion.
Anders Carlsson104f45c2010-02-12 17:37:14 +00002204 if (!ReturnTypeConversionRequiresAdjustment(MD, OverriddenMD)) {
Anders Carlsson57071e22010-02-12 05:25:12 +00002205 // This index is shared between the index in the vtable of the primary
2206 // base class.
2207 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2208 const CXXDestructorDecl *OverriddenDD =
2209 cast<CXXDestructorDecl>(OverriddenMD);
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002210
Anders Carlsson57071e22010-02-12 05:25:12 +00002211 // Add both the complete and deleting entries.
2212 MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] =
2213 getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Complete));
2214 MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] =
2215 getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting));
2216 } else {
2217 MethodVtableIndices[MD] = getMethodVtableIndex(OverriddenMD);
2218 }
2219
2220 // We don't need to add an entry for this method.
Anders Carlssonc1eec892010-02-12 18:14:46 +00002221 continue;
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002222 }
2223 }
2224
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002225 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2226 if (MD->isImplicit()) {
2227 assert(!ImplicitVirtualDtor &&
2228 "Did already see an implicit virtual dtor!");
2229 ImplicitVirtualDtor = DD;
2230 continue;
2231 }
2232
2233 // Add the complete dtor.
2234 MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++;
2235
2236 // Add the deleting dtor.
2237 MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++;
2238 } else {
2239 // Add the entry.
2240 MethodVtableIndices[MD] = CurrentIndex++;
2241 }
2242 }
2243
2244 if (ImplicitVirtualDtor) {
2245 // Itanium C++ ABI 2.5.2:
2246 // If a class has an implicitly-defined virtual destructor,
2247 // its entries come after the declared virtual function pointers.
2248
2249 // Add the complete dtor.
2250 MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] =
2251 CurrentIndex++;
2252
2253 // Add the deleting dtor.
2254 MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] =
2255 CurrentIndex++;
2256 }
2257
2258 NumVirtualFunctionPointers[RD] = CurrentIndex;
2259}
2260
2261uint64_t CGVtableInfo::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) {
2262 llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
2263 NumVirtualFunctionPointers.find(RD);
2264 if (I != NumVirtualFunctionPointers.end())
2265 return I->second;
2266
2267 ComputeMethodVtableIndices(RD);
2268
2269 I = NumVirtualFunctionPointers.find(RD);
2270 assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!");
2271 return I->second;
2272}
2273
2274uint64_t CGVtableInfo::getMethodVtableIndex(GlobalDecl GD) {
Anders Carlssona0fdd912009-11-13 17:08:56 +00002275 MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD);
Anders Carlssondbd920c2009-10-11 22:13:54 +00002276 if (I != MethodVtableIndices.end())
2277 return I->second;
2278
Anders Carlssona0fdd912009-11-13 17:08:56 +00002279 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
Anders Carlssond6b07fb2009-11-27 20:47:55 +00002280
2281 ComputeMethodVtableIndices(RD);
2282
Anders Carlssona0fdd912009-11-13 17:08:56 +00002283 I = MethodVtableIndices.find(GD);
Anders Carlssondbd920c2009-10-11 22:13:54 +00002284 assert(I != MethodVtableIndices.end() && "Did not find index!");
2285 return I->second;
2286}
2287
Eli Friedmanb455f0e2009-12-07 23:56:34 +00002288CGVtableInfo::AdjustmentVectorTy*
2289CGVtableInfo::getAdjustments(GlobalDecl GD) {
2290 SavedAdjustmentsTy::iterator I = SavedAdjustments.find(GD);
2291 if (I != SavedAdjustments.end())
2292 return &I->second;
Eli Friedman72649ed2009-12-06 22:01:30 +00002293
2294 const CXXRecordDecl *RD = cast<CXXRecordDecl>(GD.getDecl()->getDeclContext());
Eli Friedmanb455f0e2009-12-07 23:56:34 +00002295 if (!SavedAdjustmentRecords.insert(RD).second)
2296 return 0;
Eli Friedman72649ed2009-12-06 22:01:30 +00002297
Anders Carlsson1bb60992010-01-14 02:29:07 +00002298 AddressPointsMapTy AddressPoints;
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002299 OldVtableBuilder b(RD, RD, 0, CGM, false, AddressPoints);
Eli Friedman72649ed2009-12-06 22:01:30 +00002300 D1(printf("vtable %s\n", RD->getNameAsCString()));
2301 b.GenerateVtableForBase(RD);
2302 b.GenerateVtableForVBases(RD);
Eli Friedman72649ed2009-12-06 22:01:30 +00002303
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002304 for (OldVtableBuilder::SavedAdjustmentsVectorTy::iterator
Eli Friedmanb455f0e2009-12-07 23:56:34 +00002305 i = b.getSavedAdjustments().begin(),
2306 e = b.getSavedAdjustments().end(); i != e; i++)
2307 SavedAdjustments[i->first].push_back(i->second);
Eli Friedman72649ed2009-12-06 22:01:30 +00002308
Eli Friedmanb455f0e2009-12-07 23:56:34 +00002309 I = SavedAdjustments.find(GD);
2310 if (I != SavedAdjustments.end())
2311 return &I->second;
2312
2313 return 0;
Eli Friedman72649ed2009-12-06 22:01:30 +00002314}
2315
Anders Carlssondbd920c2009-10-11 22:13:54 +00002316int64_t CGVtableInfo::getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
2317 const CXXRecordDecl *VBase) {
2318 ClassPairTy ClassPair(RD, VBase);
2319
2320 VirtualBaseClassIndiciesTy::iterator I =
2321 VirtualBaseClassIndicies.find(ClassPair);
2322 if (I != VirtualBaseClassIndicies.end())
2323 return I->second;
2324
Anders Carlssondbd920c2009-10-11 22:13:54 +00002325 // FIXME: This seems expensive. Can we do a partial job to get
2326 // just this data.
Anders Carlsson1bb60992010-01-14 02:29:07 +00002327 AddressPointsMapTy AddressPoints;
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002328 OldVtableBuilder b(RD, RD, 0, CGM, false, AddressPoints);
Mike Stump6a9612f2009-10-31 20:06:59 +00002329 D1(printf("vtable %s\n", RD->getNameAsCString()));
Anders Carlssondbd920c2009-10-11 22:13:54 +00002330 b.GenerateVtableForBase(RD);
2331 b.GenerateVtableForVBases(RD);
2332
2333 for (llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
2334 b.getVBIndex().begin(), E = b.getVBIndex().end(); I != E; ++I) {
2335 // Insert all types.
2336 ClassPairTy ClassPair(RD, I->first);
2337
2338 VirtualBaseClassIndicies.insert(std::make_pair(ClassPair, I->second));
2339 }
2340
2341 I = VirtualBaseClassIndicies.find(ClassPair);
Anders Carlsson7dbf47a2010-02-13 20:11:51 +00002342 // FIXME: The assertion below assertion currently fails with the old vtable
2343 /// layout code if there is a non-virtual thunk adjustment in a vtable.
2344 // Once the new layout is in place, this return should be removed.
2345 if (I == VirtualBaseClassIndicies.end())
2346 return 0;
2347
Anders Carlssondbd920c2009-10-11 22:13:54 +00002348 assert(I != VirtualBaseClassIndicies.end() && "Did not find index!");
2349
2350 return I->second;
2351}
2352
Anders Carlsson9ac95b92009-12-05 21:03:56 +00002353uint64_t CGVtableInfo::getVtableAddressPoint(const CXXRecordDecl *RD) {
2354 uint64_t AddressPoint =
Anders Carlsson21431c52010-01-02 18:02:32 +00002355 (*(*(CGM.getVtableInfo().AddressPoints[RD]))[RD])[std::make_pair(RD, 0)];
Anders Carlsson9ac95b92009-12-05 21:03:56 +00002356
2357 return AddressPoint;
2358}
2359
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00002360llvm::GlobalVariable *
Anders Carlsson35272252009-12-06 00:23:49 +00002361CGVtableInfo::GenerateVtable(llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlsson5794c972009-12-06 00:53:22 +00002362 bool GenerateDefinition,
Anders Carlsson35272252009-12-06 00:23:49 +00002363 const CXXRecordDecl *LayoutClass,
Anders Carlsson1bb60992010-01-14 02:29:07 +00002364 const CXXRecordDecl *RD, uint64_t Offset,
2365 AddressPointsMapTy& AddressPoints) {
Anders Carlsson0c0eeb22010-02-13 02:02:03 +00002366 if (GenerateDefinition && CGM.getLangOptions().DumpVtableLayouts) {
Anders Carlsson60db0ee2010-02-13 21:07:32 +00002367 VtableBuilder Builder(*this, RD);
Anders Carlsson0c0eeb22010-02-13 02:02:03 +00002368
2369 Builder.dumpLayout(llvm::errs());
2370 }
2371
Anders Carlssondbd920c2009-10-11 22:13:54 +00002372 llvm::SmallString<256> OutName;
Mike Stump9840c702009-11-12 20:47:57 +00002373 if (LayoutClass != RD)
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00002374 CGM.getMangleContext().mangleCXXCtorVtable(LayoutClass, Offset / 8,
2375 RD, OutName);
Mike Stump8cfcb522009-11-11 20:26:26 +00002376 else
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00002377 CGM.getMangleContext().mangleCXXVtable(RD, OutName);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +00002378 llvm::StringRef Name = OutName.str();
Benjamin Kramer7a9474e2009-10-11 22:57:54 +00002379
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00002380 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
Anders Carlsson21431c52010-01-02 18:02:32 +00002381 if (GV == 0 || CGM.getVtableInfo().AddressPoints[LayoutClass] == 0 ||
2382 GV->isDeclaration()) {
Anders Carlsson824d7ea2010-02-11 08:02:13 +00002383 OldVtableBuilder b(RD, LayoutClass, Offset, CGM, GenerateDefinition,
2384 AddressPoints);
Eli Friedman152b5b12009-12-05 01:05:03 +00002385
2386 D1(printf("vtable %s\n", RD->getNameAsCString()));
2387 // First comes the vtables for all the non-virtual bases...
Mike Stump8cc4f102009-12-24 07:29:41 +00002388 b.GenerateVtableForBase(RD, Offset);
Eli Friedman152b5b12009-12-05 01:05:03 +00002389
2390 // then the vtables for all the virtual bases.
2391 b.GenerateVtableForVBases(RD, Offset);
2392
Anders Carlsson152d4dc2009-12-05 22:19:10 +00002393 llvm::Constant *Init = 0;
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00002394 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
Anders Carlsson152d4dc2009-12-05 22:19:10 +00002395 llvm::ArrayType *ArrayType =
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00002396 llvm::ArrayType::get(Int8PtrTy, b.getVtableComponents().size());
Anders Carlssone40477c2009-12-05 21:09:05 +00002397
Anders Carlsson5794c972009-12-06 00:53:22 +00002398 if (GenerateDefinition)
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00002399 Init = llvm::ConstantArray::get(ArrayType, &b.getVtableComponents()[0],
2400 b.getVtableComponents().size());
Anders Carlsson5794c972009-12-06 00:53:22 +00002401
Mike Stump85615df2009-11-19 04:04:36 +00002402 llvm::GlobalVariable *OGV = GV;
Anders Carlsson5794c972009-12-06 00:53:22 +00002403
2404 GV = new llvm::GlobalVariable(CGM.getModule(), ArrayType,
2405 /*isConstant=*/true, Linkage, Init, Name);
Anders Carlssonc3a46ef2009-12-06 01:09:21 +00002406 CGM.setGlobalVisibility(GV, RD);
2407
Mike Stump85615df2009-11-19 04:04:36 +00002408 if (OGV) {
2409 GV->takeName(OGV);
Anders Carlsson152d4dc2009-12-05 22:19:10 +00002410 llvm::Constant *NewPtr =
2411 llvm::ConstantExpr::getBitCast(GV, OGV->getType());
Mike Stump85615df2009-11-19 04:04:36 +00002412 OGV->replaceAllUsesWith(NewPtr);
2413 OGV->eraseFromParent();
2414 }
Mike Stump85615df2009-11-19 04:04:36 +00002415 }
Anders Carlssonbb27d862009-12-05 21:28:12 +00002416
2417 return GV;
Anders Carlssondbd920c2009-10-11 22:13:54 +00002418}
Mike Stumpfbfb52d2009-11-10 02:30:51 +00002419
Anders Carlsson5794c972009-12-06 00:53:22 +00002420void CGVtableInfo::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
2421 const CXXRecordDecl *RD) {
Anders Carlssond58987c2009-12-07 07:59:52 +00002422 llvm::GlobalVariable *&Vtable = Vtables[RD];
2423 if (Vtable) {
2424 assert(Vtable->getInitializer() && "Vtable doesn't have a definition!");
2425 return;
2426 }
2427
Anders Carlsson1bb60992010-01-14 02:29:07 +00002428 AddressPointsMapTy AddressPoints;
2429 Vtable = GenerateVtable(Linkage, /*GenerateDefinition=*/true, RD, RD, 0,
2430 AddressPoints);
Anders Carlssonc997d422010-01-02 01:01:18 +00002431 GenerateVTT(Linkage, /*GenerateDefinition=*/true, RD);
Mike Stump58588942009-11-19 01:08:19 +00002432}
2433
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00002434llvm::GlobalVariable *CGVtableInfo::getVtable(const CXXRecordDecl *RD) {
Anders Carlsson5794c972009-12-06 00:53:22 +00002435 llvm::GlobalVariable *Vtable = Vtables.lookup(RD);
Anders Carlsson8c2d36f2009-12-06 00:01:05 +00002436
Anders Carlsson1bb60992010-01-14 02:29:07 +00002437 if (!Vtable) {
2438 AddressPointsMapTy AddressPoints;
Anders Carlsson5794c972009-12-06 00:53:22 +00002439 Vtable = GenerateVtable(llvm::GlobalValue::ExternalLinkage,
Anders Carlsson1bb60992010-01-14 02:29:07 +00002440 /*GenerateDefinition=*/false, RD, RD, 0,
2441 AddressPoints);
2442 }
Mike Stump85615df2009-11-19 04:04:36 +00002443
Anders Carlsson224c3122009-12-05 22:42:54 +00002444 return Vtable;
Mike Stump380dd752009-11-10 07:44:33 +00002445}
Mike Stump8cfcb522009-11-11 20:26:26 +00002446
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002447void CGVtableInfo::MaybeEmitVtable(GlobalDecl GD) {
2448 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2449 const CXXRecordDecl *RD = MD->getParent();
2450
Anders Carlsson224c3122009-12-05 22:42:54 +00002451 // If the class doesn't have a vtable we don't need to emit one.
2452 if (!RD->isDynamicClass())
2453 return;
2454
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002455 // Get the key function.
Anders Carlssonf53df232009-12-07 04:35:11 +00002456 const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD);
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002457
Anders Carlsson224c3122009-12-05 22:42:54 +00002458 if (KeyFunction) {
2459 // We don't have the right key function.
2460 if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl())
2461 return;
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002462 }
2463
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002464 // Emit the data.
Douglas Gregor4b0f21c2010-01-06 20:27:16 +00002465 GenerateClassData(CGM.getVtableLinkage(RD), RD);
Eli Friedman72649ed2009-12-06 22:01:30 +00002466
2467 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
2468 e = RD->method_end(); i != e; ++i) {
Eli Friedmanb455f0e2009-12-07 23:56:34 +00002469 if ((*i)->isVirtual() && ((*i)->hasInlineBody() || (*i)->isImplicit())) {
Eli Friedman72649ed2009-12-06 22:01:30 +00002470 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(*i)) {
2471 CGM.BuildThunksForVirtual(GlobalDecl(DD, Dtor_Complete));
2472 CGM.BuildThunksForVirtual(GlobalDecl(DD, Dtor_Deleting));
2473 } else {
2474 CGM.BuildThunksForVirtual(GlobalDecl(*i));
2475 }
2476 }
2477 }
Anders Carlsson1a5e0d72009-11-30 23:41:22 +00002478}
2479