blob: 859bd30ec8348228935cb6db94aedb4d1c3569c3 [file] [log] [blame]
Peter Collingbournecfd23562011-09-26 01:57:12 +00001//===--- VTableBuilder.cpp - C++ vtable layout builder --------------------===//
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 generation of the layout of virtual tables.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/VTableBuilder.h"
Benjamin Kramer2ef30312012-07-04 18:45:14 +000015#include "clang/AST/ASTContext.h"
Peter Collingbournecfd23562011-09-26 01:57:12 +000016#include "clang/AST/CXXInheritance.h"
17#include "clang/AST/RecordLayout.h"
18#include "clang/Basic/TargetInfo.h"
Reid Kleckner5f080942014-01-03 23:42:00 +000019#include "llvm/ADT/SmallPtrSet.h"
Peter Collingbournecfd23562011-09-26 01:57:12 +000020#include "llvm/Support/Format.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000021#include "llvm/Support/raw_ostream.h"
Peter Collingbournecfd23562011-09-26 01:57:12 +000022#include <algorithm>
23#include <cstdio>
24
25using namespace clang;
26
27#define DUMP_OVERRIDERS 0
28
29namespace {
30
31/// BaseOffset - Represents an offset from a derived class to a direct or
32/// indirect base class.
33struct BaseOffset {
34 /// DerivedClass - The derived class.
35 const CXXRecordDecl *DerivedClass;
36
37 /// VirtualBase - If the path from the derived class to the base class
Timur Iskhodzhanovbb5a17e2013-05-08 08:09:21 +000038 /// involves virtual base classes, this holds the declaration of the last
39 /// virtual base in this path (i.e. closest to the base class).
Peter Collingbournecfd23562011-09-26 01:57:12 +000040 const CXXRecordDecl *VirtualBase;
41
42 /// NonVirtualOffset - The offset from the derived class to the base class.
43 /// (Or the offset from the virtual base class to the base class, if the
44 /// path from the derived class to the base class involves a virtual base
45 /// class.
46 CharUnits NonVirtualOffset;
Craig Topper36250ad2014-05-12 05:36:57 +000047
48 BaseOffset() : DerivedClass(nullptr), VirtualBase(nullptr),
49 NonVirtualOffset(CharUnits::Zero()) { }
Peter Collingbournecfd23562011-09-26 01:57:12 +000050 BaseOffset(const CXXRecordDecl *DerivedClass,
51 const CXXRecordDecl *VirtualBase, CharUnits NonVirtualOffset)
52 : DerivedClass(DerivedClass), VirtualBase(VirtualBase),
53 NonVirtualOffset(NonVirtualOffset) { }
54
55 bool isEmpty() const { return NonVirtualOffset.isZero() && !VirtualBase; }
56};
57
58/// FinalOverriders - Contains the final overrider member functions for all
59/// member functions in the base subobjects of a class.
60class FinalOverriders {
61public:
62 /// OverriderInfo - Information about a final overrider.
63 struct OverriderInfo {
64 /// Method - The method decl of the overrider.
65 const CXXMethodDecl *Method;
66
Timur Iskhodzhanov7d19fc12014-11-17 15:11:05 +000067 /// VirtualBase - The virtual base class subobject of this overrider.
Timur Iskhodzhanov6b128502014-04-22 17:32:02 +000068 /// Note that this records the closest derived virtual base class subobject.
69 const CXXRecordDecl *VirtualBase;
70
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +000071 /// Offset - the base offset of the overrider's parent in the layout class.
Peter Collingbournecfd23562011-09-26 01:57:12 +000072 CharUnits Offset;
Craig Topper36250ad2014-05-12 05:36:57 +000073
74 OverriderInfo() : Method(nullptr), VirtualBase(nullptr),
75 Offset(CharUnits::Zero()) { }
Peter Collingbournecfd23562011-09-26 01:57:12 +000076 };
77
78private:
79 /// MostDerivedClass - The most derived class for which the final overriders
80 /// are stored.
81 const CXXRecordDecl *MostDerivedClass;
82
83 /// MostDerivedClassOffset - If we're building final overriders for a
84 /// construction vtable, this holds the offset from the layout class to the
85 /// most derived class.
86 const CharUnits MostDerivedClassOffset;
87
88 /// LayoutClass - The class we're using for layout information. Will be
89 /// different than the most derived class if the final overriders are for a
90 /// construction vtable.
91 const CXXRecordDecl *LayoutClass;
92
93 ASTContext &Context;
94
95 /// MostDerivedClassLayout - the AST record layout of the most derived class.
96 const ASTRecordLayout &MostDerivedClassLayout;
97
98 /// MethodBaseOffsetPairTy - Uniquely identifies a member function
99 /// in a base subobject.
100 typedef std::pair<const CXXMethodDecl *, CharUnits> MethodBaseOffsetPairTy;
101
102 typedef llvm::DenseMap<MethodBaseOffsetPairTy,
103 OverriderInfo> OverridersMapTy;
104
105 /// OverridersMap - The final overriders for all virtual member functions of
106 /// all the base subobjects of the most derived class.
107 OverridersMapTy OverridersMap;
108
109 /// SubobjectsToOffsetsMapTy - A mapping from a base subobject (represented
110 /// as a record decl and a subobject number) and its offsets in the most
111 /// derived class as well as the layout class.
112 typedef llvm::DenseMap<std::pair<const CXXRecordDecl *, unsigned>,
113 CharUnits> SubobjectOffsetMapTy;
114
115 typedef llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCountMapTy;
116
117 /// ComputeBaseOffsets - Compute the offsets for all base subobjects of the
118 /// given base.
119 void ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual,
120 CharUnits OffsetInLayoutClass,
121 SubobjectOffsetMapTy &SubobjectOffsets,
122 SubobjectOffsetMapTy &SubobjectLayoutClassOffsets,
123 SubobjectCountMapTy &SubobjectCounts);
124
125 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
126
127 /// dump - dump the final overriders for a base subobject, and all its direct
128 /// and indirect base subobjects.
129 void dump(raw_ostream &Out, BaseSubobject Base,
130 VisitedVirtualBasesSetTy& VisitedVirtualBases);
131
132public:
133 FinalOverriders(const CXXRecordDecl *MostDerivedClass,
134 CharUnits MostDerivedClassOffset,
135 const CXXRecordDecl *LayoutClass);
136
137 /// getOverrider - Get the final overrider for the given method declaration in
138 /// the subobject with the given base offset.
139 OverriderInfo getOverrider(const CXXMethodDecl *MD,
140 CharUnits BaseOffset) const {
141 assert(OverridersMap.count(std::make_pair(MD, BaseOffset)) &&
142 "Did not find overrider!");
143
144 return OverridersMap.lookup(std::make_pair(MD, BaseOffset));
145 }
146
147 /// dump - dump the final overriders.
148 void dump() {
149 VisitedVirtualBasesSetTy VisitedVirtualBases;
150 dump(llvm::errs(), BaseSubobject(MostDerivedClass, CharUnits::Zero()),
151 VisitedVirtualBases);
152 }
153
154};
155
Peter Collingbournecfd23562011-09-26 01:57:12 +0000156FinalOverriders::FinalOverriders(const CXXRecordDecl *MostDerivedClass,
157 CharUnits MostDerivedClassOffset,
158 const CXXRecordDecl *LayoutClass)
159 : MostDerivedClass(MostDerivedClass),
160 MostDerivedClassOffset(MostDerivedClassOffset), LayoutClass(LayoutClass),
161 Context(MostDerivedClass->getASTContext()),
162 MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)) {
163
164 // Compute base offsets.
165 SubobjectOffsetMapTy SubobjectOffsets;
166 SubobjectOffsetMapTy SubobjectLayoutClassOffsets;
167 SubobjectCountMapTy SubobjectCounts;
168 ComputeBaseOffsets(BaseSubobject(MostDerivedClass, CharUnits::Zero()),
169 /*IsVirtual=*/false,
170 MostDerivedClassOffset,
171 SubobjectOffsets, SubobjectLayoutClassOffsets,
172 SubobjectCounts);
173
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000174 // Get the final overriders.
Peter Collingbournecfd23562011-09-26 01:57:12 +0000175 CXXFinalOverriderMap FinalOverriders;
176 MostDerivedClass->getFinalOverriders(FinalOverriders);
177
178 for (CXXFinalOverriderMap::const_iterator I = FinalOverriders.begin(),
179 E = FinalOverriders.end(); I != E; ++I) {
180 const CXXMethodDecl *MD = I->first;
181 const OverridingMethods& Methods = I->second;
182
183 for (OverridingMethods::const_iterator I = Methods.begin(),
184 E = Methods.end(); I != E; ++I) {
185 unsigned SubobjectNumber = I->first;
186 assert(SubobjectOffsets.count(std::make_pair(MD->getParent(),
187 SubobjectNumber)) &&
188 "Did not find subobject offset!");
189
190 CharUnits BaseOffset = SubobjectOffsets[std::make_pair(MD->getParent(),
191 SubobjectNumber)];
192
193 assert(I->second.size() == 1 && "Final overrider is not unique!");
194 const UniqueVirtualMethod &Method = I->second.front();
195
196 const CXXRecordDecl *OverriderRD = Method.Method->getParent();
197 assert(SubobjectLayoutClassOffsets.count(
198 std::make_pair(OverriderRD, Method.Subobject))
199 && "Did not find subobject offset!");
200 CharUnits OverriderOffset =
201 SubobjectLayoutClassOffsets[std::make_pair(OverriderRD,
202 Method.Subobject)];
203
204 OverriderInfo& Overrider = OverridersMap[std::make_pair(MD, BaseOffset)];
205 assert(!Overrider.Method && "Overrider should not exist yet!");
206
207 Overrider.Offset = OverriderOffset;
208 Overrider.Method = Method.Method;
Timur Iskhodzhanov6b128502014-04-22 17:32:02 +0000209 Overrider.VirtualBase = Method.InVirtualSubobject;
Peter Collingbournecfd23562011-09-26 01:57:12 +0000210 }
211 }
212
213#if DUMP_OVERRIDERS
214 // And dump them (for now).
215 dump();
216#endif
217}
218
219static BaseOffset ComputeBaseOffset(ASTContext &Context,
220 const CXXRecordDecl *DerivedRD,
221 const CXXBasePath &Path) {
222 CharUnits NonVirtualOffset = CharUnits::Zero();
223
224 unsigned NonVirtualStart = 0;
Craig Topper36250ad2014-05-12 05:36:57 +0000225 const CXXRecordDecl *VirtualBase = nullptr;
226
Peter Collingbournecfd23562011-09-26 01:57:12 +0000227 // First, look for the virtual base class.
Timur Iskhodzhanovbb5a17e2013-05-08 08:09:21 +0000228 for (int I = Path.size(), E = 0; I != E; --I) {
229 const CXXBasePathElement &Element = Path[I - 1];
230
Peter Collingbournecfd23562011-09-26 01:57:12 +0000231 if (Element.Base->isVirtual()) {
Timur Iskhodzhanovbb5a17e2013-05-08 08:09:21 +0000232 NonVirtualStart = I;
Peter Collingbournecfd23562011-09-26 01:57:12 +0000233 QualType VBaseType = Element.Base->getType();
Timur Iskhodzhanov7f55a452013-07-02 16:00:40 +0000234 VirtualBase = VBaseType->getAsCXXRecordDecl();
Timur Iskhodzhanovbb5a17e2013-05-08 08:09:21 +0000235 break;
Peter Collingbournecfd23562011-09-26 01:57:12 +0000236 }
237 }
238
239 // Now compute the non-virtual offset.
240 for (unsigned I = NonVirtualStart, E = Path.size(); I != E; ++I) {
241 const CXXBasePathElement &Element = Path[I];
242
243 // Check the base class offset.
244 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class);
245
Timur Iskhodzhanov7f55a452013-07-02 16:00:40 +0000246 const CXXRecordDecl *Base = Element.Base->getType()->getAsCXXRecordDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +0000247
248 NonVirtualOffset += Layout.getBaseClassOffset(Base);
249 }
250
251 // FIXME: This should probably use CharUnits or something. Maybe we should
252 // even change the base offsets in ASTRecordLayout to be specified in
253 // CharUnits.
254 return BaseOffset(DerivedRD, VirtualBase, NonVirtualOffset);
255
256}
257
258static BaseOffset ComputeBaseOffset(ASTContext &Context,
259 const CXXRecordDecl *BaseRD,
260 const CXXRecordDecl *DerivedRD) {
261 CXXBasePaths Paths(/*FindAmbiguities=*/false,
262 /*RecordPaths=*/true, /*DetectVirtual=*/false);
Benjamin Kramer325d7452013-02-03 18:55:34 +0000263
264 if (!DerivedRD->isDerivedFrom(BaseRD, Paths))
Peter Collingbournecfd23562011-09-26 01:57:12 +0000265 llvm_unreachable("Class must be derived from the passed in base class!");
Peter Collingbournecfd23562011-09-26 01:57:12 +0000266
267 return ComputeBaseOffset(Context, DerivedRD, Paths.front());
268}
269
270static BaseOffset
271ComputeReturnAdjustmentBaseOffset(ASTContext &Context,
272 const CXXMethodDecl *DerivedMD,
273 const CXXMethodDecl *BaseMD) {
274 const FunctionType *BaseFT = BaseMD->getType()->getAs<FunctionType>();
275 const FunctionType *DerivedFT = DerivedMD->getType()->getAs<FunctionType>();
276
277 // Canonicalize the return types.
Alp Toker314cc812014-01-25 16:55:45 +0000278 CanQualType CanDerivedReturnType =
279 Context.getCanonicalType(DerivedFT->getReturnType());
280 CanQualType CanBaseReturnType =
281 Context.getCanonicalType(BaseFT->getReturnType());
282
Peter Collingbournecfd23562011-09-26 01:57:12 +0000283 assert(CanDerivedReturnType->getTypeClass() ==
284 CanBaseReturnType->getTypeClass() &&
285 "Types must have same type class!");
286
287 if (CanDerivedReturnType == CanBaseReturnType) {
288 // No adjustment needed.
289 return BaseOffset();
290 }
291
292 if (isa<ReferenceType>(CanDerivedReturnType)) {
293 CanDerivedReturnType =
294 CanDerivedReturnType->getAs<ReferenceType>()->getPointeeType();
295 CanBaseReturnType =
296 CanBaseReturnType->getAs<ReferenceType>()->getPointeeType();
297 } else if (isa<PointerType>(CanDerivedReturnType)) {
298 CanDerivedReturnType =
299 CanDerivedReturnType->getAs<PointerType>()->getPointeeType();
300 CanBaseReturnType =
301 CanBaseReturnType->getAs<PointerType>()->getPointeeType();
302 } else {
303 llvm_unreachable("Unexpected return type!");
304 }
305
306 // We need to compare unqualified types here; consider
307 // const T *Base::foo();
308 // T *Derived::foo();
309 if (CanDerivedReturnType.getUnqualifiedType() ==
310 CanBaseReturnType.getUnqualifiedType()) {
311 // No adjustment needed.
312 return BaseOffset();
313 }
314
315 const CXXRecordDecl *DerivedRD =
316 cast<CXXRecordDecl>(cast<RecordType>(CanDerivedReturnType)->getDecl());
317
318 const CXXRecordDecl *BaseRD =
319 cast<CXXRecordDecl>(cast<RecordType>(CanBaseReturnType)->getDecl());
320
321 return ComputeBaseOffset(Context, BaseRD, DerivedRD);
322}
323
324void
325FinalOverriders::ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual,
326 CharUnits OffsetInLayoutClass,
327 SubobjectOffsetMapTy &SubobjectOffsets,
328 SubobjectOffsetMapTy &SubobjectLayoutClassOffsets,
329 SubobjectCountMapTy &SubobjectCounts) {
330 const CXXRecordDecl *RD = Base.getBase();
331
332 unsigned SubobjectNumber = 0;
333 if (!IsVirtual)
334 SubobjectNumber = ++SubobjectCounts[RD];
335
336 // Set up the subobject to offset mapping.
337 assert(!SubobjectOffsets.count(std::make_pair(RD, SubobjectNumber))
338 && "Subobject offset already exists!");
339 assert(!SubobjectLayoutClassOffsets.count(std::make_pair(RD, SubobjectNumber))
340 && "Subobject offset already exists!");
341
342 SubobjectOffsets[std::make_pair(RD, SubobjectNumber)] = Base.getBaseOffset();
343 SubobjectLayoutClassOffsets[std::make_pair(RD, SubobjectNumber)] =
344 OffsetInLayoutClass;
345
346 // Traverse our bases.
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +0000347 for (const auto &B : RD->bases()) {
348 const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +0000349
350 CharUnits BaseOffset;
351 CharUnits BaseOffsetInLayoutClass;
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +0000352 if (B.isVirtual()) {
Peter Collingbournecfd23562011-09-26 01:57:12 +0000353 // Check if we've visited this virtual base before.
354 if (SubobjectOffsets.count(std::make_pair(BaseDecl, 0)))
355 continue;
356
357 const ASTRecordLayout &LayoutClassLayout =
358 Context.getASTRecordLayout(LayoutClass);
359
360 BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
361 BaseOffsetInLayoutClass =
362 LayoutClassLayout.getVBaseClassOffset(BaseDecl);
363 } else {
364 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
365 CharUnits Offset = Layout.getBaseClassOffset(BaseDecl);
366
367 BaseOffset = Base.getBaseOffset() + Offset;
368 BaseOffsetInLayoutClass = OffsetInLayoutClass + Offset;
369 }
370
371 ComputeBaseOffsets(BaseSubobject(BaseDecl, BaseOffset),
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +0000372 B.isVirtual(), BaseOffsetInLayoutClass,
Peter Collingbournecfd23562011-09-26 01:57:12 +0000373 SubobjectOffsets, SubobjectLayoutClassOffsets,
374 SubobjectCounts);
375 }
376}
377
378void FinalOverriders::dump(raw_ostream &Out, BaseSubobject Base,
379 VisitedVirtualBasesSetTy &VisitedVirtualBases) {
380 const CXXRecordDecl *RD = Base.getBase();
381 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
382
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +0000383 for (const auto &B : RD->bases()) {
384 const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +0000385
386 // Ignore bases that don't have any virtual member functions.
387 if (!BaseDecl->isPolymorphic())
388 continue;
389
390 CharUnits BaseOffset;
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +0000391 if (B.isVirtual()) {
David Blaikie82e95a32014-11-19 07:49:47 +0000392 if (!VisitedVirtualBases.insert(BaseDecl).second) {
Peter Collingbournecfd23562011-09-26 01:57:12 +0000393 // We've visited this base before.
394 continue;
395 }
396
397 BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
398 } else {
399 BaseOffset = Layout.getBaseClassOffset(BaseDecl) + Base.getBaseOffset();
400 }
401
402 dump(Out, BaseSubobject(BaseDecl, BaseOffset), VisitedVirtualBases);
403 }
404
Aaron Ballman75ee4cc2014-01-03 18:42:48 +0000405 Out << "Final overriders for (";
406 RD->printQualifiedName(Out);
407 Out << ", ";
Peter Collingbournecfd23562011-09-26 01:57:12 +0000408 Out << Base.getBaseOffset().getQuantity() << ")\n";
409
410 // Now dump the overriders for this base subobject.
Aaron Ballman2b124d12014-03-13 16:36:16 +0000411 for (const auto *MD : RD->methods()) {
Peter Collingbournecfd23562011-09-26 01:57:12 +0000412 if (!MD->isVirtual())
413 continue;
Reid Kleckner1cbd9aa2015-02-25 19:17:48 +0000414 MD = MD->getCanonicalDecl();
NAKAMURA Takumi073f2b42015-02-25 10:50:06 +0000415
Peter Collingbournecfd23562011-09-26 01:57:12 +0000416 OverriderInfo Overrider = getOverrider(MD, Base.getBaseOffset());
417
Aaron Ballman75ee4cc2014-01-03 18:42:48 +0000418 Out << " ";
419 MD->printQualifiedName(Out);
420 Out << " - (";
421 Overrider.Method->printQualifiedName(Out);
Timur Iskhodzhanovbe5a3d82013-06-05 06:40:07 +0000422 Out << ", " << Overrider.Offset.getQuantity() << ')';
Peter Collingbournecfd23562011-09-26 01:57:12 +0000423
424 BaseOffset Offset;
425 if (!Overrider.Method->isPure())
426 Offset = ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD);
427
428 if (!Offset.isEmpty()) {
429 Out << " [ret-adj: ";
Aaron Ballman75ee4cc2014-01-03 18:42:48 +0000430 if (Offset.VirtualBase) {
431 Offset.VirtualBase->printQualifiedName(Out);
432 Out << " vbase, ";
433 }
Peter Collingbournecfd23562011-09-26 01:57:12 +0000434
435 Out << Offset.NonVirtualOffset.getQuantity() << " nv]";
436 }
437
438 Out << "\n";
439 }
440}
441
442/// VCallOffsetMap - Keeps track of vcall offsets when building a vtable.
443struct VCallOffsetMap {
444
445 typedef std::pair<const CXXMethodDecl *, CharUnits> MethodAndOffsetPairTy;
446
447 /// Offsets - Keeps track of methods and their offsets.
448 // FIXME: This should be a real map and not a vector.
449 SmallVector<MethodAndOffsetPairTy, 16> Offsets;
450
451 /// MethodsCanShareVCallOffset - Returns whether two virtual member functions
452 /// can share the same vcall offset.
453 static bool MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
454 const CXXMethodDecl *RHS);
455
456public:
457 /// AddVCallOffset - Adds a vcall offset to the map. Returns true if the
458 /// add was successful, or false if there was already a member function with
459 /// the same signature in the map.
460 bool AddVCallOffset(const CXXMethodDecl *MD, CharUnits OffsetOffset);
461
462 /// getVCallOffsetOffset - Returns the vcall offset offset (relative to the
463 /// vtable address point) for the given virtual member function.
464 CharUnits getVCallOffsetOffset(const CXXMethodDecl *MD);
465
466 // empty - Return whether the offset map is empty or not.
467 bool empty() const { return Offsets.empty(); }
468};
469
470static bool HasSameVirtualSignature(const CXXMethodDecl *LHS,
471 const CXXMethodDecl *RHS) {
John McCallb6c4a7e2012-03-21 06:57:19 +0000472 const FunctionProtoType *LT =
473 cast<FunctionProtoType>(LHS->getType().getCanonicalType());
474 const FunctionProtoType *RT =
475 cast<FunctionProtoType>(RHS->getType().getCanonicalType());
Peter Collingbournecfd23562011-09-26 01:57:12 +0000476
477 // Fast-path matches in the canonical types.
478 if (LT == RT) return true;
479
480 // Force the signatures to match. We can't rely on the overrides
481 // list here because there isn't necessarily an inheritance
482 // relationship between the two methods.
John McCallb6c4a7e2012-03-21 06:57:19 +0000483 if (LT->getTypeQuals() != RT->getTypeQuals() ||
Alp Toker9cacbab2014-01-20 20:26:09 +0000484 LT->getNumParams() != RT->getNumParams())
Peter Collingbournecfd23562011-09-26 01:57:12 +0000485 return false;
Alp Toker9cacbab2014-01-20 20:26:09 +0000486 for (unsigned I = 0, E = LT->getNumParams(); I != E; ++I)
487 if (LT->getParamType(I) != RT->getParamType(I))
Peter Collingbournecfd23562011-09-26 01:57:12 +0000488 return false;
489 return true;
490}
491
492bool VCallOffsetMap::MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
493 const CXXMethodDecl *RHS) {
494 assert(LHS->isVirtual() && "LHS must be virtual!");
495 assert(RHS->isVirtual() && "LHS must be virtual!");
496
497 // A destructor can share a vcall offset with another destructor.
498 if (isa<CXXDestructorDecl>(LHS))
499 return isa<CXXDestructorDecl>(RHS);
500
501 // FIXME: We need to check more things here.
502
503 // The methods must have the same name.
504 DeclarationName LHSName = LHS->getDeclName();
505 DeclarationName RHSName = RHS->getDeclName();
506 if (LHSName != RHSName)
507 return false;
508
509 // And the same signatures.
510 return HasSameVirtualSignature(LHS, RHS);
511}
512
513bool VCallOffsetMap::AddVCallOffset(const CXXMethodDecl *MD,
514 CharUnits OffsetOffset) {
515 // Check if we can reuse an offset.
516 for (unsigned I = 0, E = Offsets.size(); I != E; ++I) {
517 if (MethodsCanShareVCallOffset(Offsets[I].first, MD))
518 return false;
519 }
520
521 // Add the offset.
522 Offsets.push_back(MethodAndOffsetPairTy(MD, OffsetOffset));
523 return true;
524}
525
526CharUnits VCallOffsetMap::getVCallOffsetOffset(const CXXMethodDecl *MD) {
527 // Look for an offset.
528 for (unsigned I = 0, E = Offsets.size(); I != E; ++I) {
529 if (MethodsCanShareVCallOffset(Offsets[I].first, MD))
530 return Offsets[I].second;
531 }
532
533 llvm_unreachable("Should always find a vcall offset offset!");
534}
535
536/// VCallAndVBaseOffsetBuilder - Class for building vcall and vbase offsets.
537class VCallAndVBaseOffsetBuilder {
538public:
539 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits>
540 VBaseOffsetOffsetsMapTy;
541
542private:
543 /// MostDerivedClass - The most derived class for which we're building vcall
544 /// and vbase offsets.
545 const CXXRecordDecl *MostDerivedClass;
546
547 /// LayoutClass - The class we're using for layout information. Will be
548 /// different than the most derived class if we're building a construction
549 /// vtable.
550 const CXXRecordDecl *LayoutClass;
551
552 /// Context - The ASTContext which we will use for layout information.
553 ASTContext &Context;
554
555 /// Components - vcall and vbase offset components
556 typedef SmallVector<VTableComponent, 64> VTableComponentVectorTy;
557 VTableComponentVectorTy Components;
558
559 /// VisitedVirtualBases - Visited virtual bases.
560 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
561
562 /// VCallOffsets - Keeps track of vcall offsets.
563 VCallOffsetMap VCallOffsets;
564
565
566 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets,
567 /// relative to the address point.
568 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
569
570 /// FinalOverriders - The final overriders of the most derived class.
571 /// (Can be null when we're not building a vtable of the most derived class).
572 const FinalOverriders *Overriders;
573
574 /// AddVCallAndVBaseOffsets - Add vcall offsets and vbase offsets for the
575 /// given base subobject.
576 void AddVCallAndVBaseOffsets(BaseSubobject Base, bool BaseIsVirtual,
577 CharUnits RealBaseOffset);
578
579 /// AddVCallOffsets - Add vcall offsets for the given base subobject.
580 void AddVCallOffsets(BaseSubobject Base, CharUnits VBaseOffset);
581
582 /// AddVBaseOffsets - Add vbase offsets for the given class.
583 void AddVBaseOffsets(const CXXRecordDecl *Base,
584 CharUnits OffsetInLayoutClass);
585
586 /// getCurrentOffsetOffset - Get the current vcall or vbase offset offset in
587 /// chars, relative to the vtable address point.
588 CharUnits getCurrentOffsetOffset() const;
589
590public:
591 VCallAndVBaseOffsetBuilder(const CXXRecordDecl *MostDerivedClass,
592 const CXXRecordDecl *LayoutClass,
593 const FinalOverriders *Overriders,
594 BaseSubobject Base, bool BaseIsVirtual,
595 CharUnits OffsetInLayoutClass)
596 : MostDerivedClass(MostDerivedClass), LayoutClass(LayoutClass),
597 Context(MostDerivedClass->getASTContext()), Overriders(Overriders) {
598
599 // Add vcall and vbase offsets.
600 AddVCallAndVBaseOffsets(Base, BaseIsVirtual, OffsetInLayoutClass);
601 }
602
603 /// Methods for iterating over the components.
604 typedef VTableComponentVectorTy::const_reverse_iterator const_iterator;
605 const_iterator components_begin() const { return Components.rbegin(); }
606 const_iterator components_end() const { return Components.rend(); }
607
608 const VCallOffsetMap &getVCallOffsets() const { return VCallOffsets; }
609 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
610 return VBaseOffsetOffsets;
611 }
612};
613
614void
615VCallAndVBaseOffsetBuilder::AddVCallAndVBaseOffsets(BaseSubobject Base,
616 bool BaseIsVirtual,
617 CharUnits RealBaseOffset) {
618 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base.getBase());
619
620 // Itanium C++ ABI 2.5.2:
621 // ..in classes sharing a virtual table with a primary base class, the vcall
622 // and vbase offsets added by the derived class all come before the vcall
623 // and vbase offsets required by the base class, so that the latter may be
624 // laid out as required by the base class without regard to additions from
625 // the derived class(es).
626
627 // (Since we're emitting the vcall and vbase offsets in reverse order, we'll
628 // emit them for the primary base first).
629 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
630 bool PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual();
631
632 CharUnits PrimaryBaseOffset;
633
634 // Get the base offset of the primary base.
635 if (PrimaryBaseIsVirtual) {
Benjamin Kramer2ef30312012-07-04 18:45:14 +0000636 assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() &&
Peter Collingbournecfd23562011-09-26 01:57:12 +0000637 "Primary vbase should have a zero offset!");
638
639 const ASTRecordLayout &MostDerivedClassLayout =
640 Context.getASTRecordLayout(MostDerivedClass);
641
642 PrimaryBaseOffset =
643 MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
644 } else {
Benjamin Kramer2ef30312012-07-04 18:45:14 +0000645 assert(Layout.getBaseClassOffset(PrimaryBase).isZero() &&
Peter Collingbournecfd23562011-09-26 01:57:12 +0000646 "Primary base should have a zero offset!");
647
648 PrimaryBaseOffset = Base.getBaseOffset();
649 }
650
651 AddVCallAndVBaseOffsets(
652 BaseSubobject(PrimaryBase,PrimaryBaseOffset),
653 PrimaryBaseIsVirtual, RealBaseOffset);
654 }
655
656 AddVBaseOffsets(Base.getBase(), RealBaseOffset);
657
658 // We only want to add vcall offsets for virtual bases.
659 if (BaseIsVirtual)
660 AddVCallOffsets(Base, RealBaseOffset);
661}
662
663CharUnits VCallAndVBaseOffsetBuilder::getCurrentOffsetOffset() const {
664 // OffsetIndex is the index of this vcall or vbase offset, relative to the
665 // vtable address point. (We subtract 3 to account for the information just
666 // above the address point, the RTTI info, the offset to top, and the
667 // vcall offset itself).
668 int64_t OffsetIndex = -(int64_t)(3 + Components.size());
669
670 CharUnits PointerWidth =
671 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
672 CharUnits OffsetOffset = PointerWidth * OffsetIndex;
673 return OffsetOffset;
674}
675
676void VCallAndVBaseOffsetBuilder::AddVCallOffsets(BaseSubobject Base,
677 CharUnits VBaseOffset) {
678 const CXXRecordDecl *RD = Base.getBase();
679 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
680
681 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
682
683 // Handle the primary base first.
684 // We only want to add vcall offsets if the base is non-virtual; a virtual
685 // primary base will have its vcall and vbase offsets emitted already.
686 if (PrimaryBase && !Layout.isPrimaryBaseVirtual()) {
687 // Get the base offset of the primary base.
Benjamin Kramer2ef30312012-07-04 18:45:14 +0000688 assert(Layout.getBaseClassOffset(PrimaryBase).isZero() &&
Peter Collingbournecfd23562011-09-26 01:57:12 +0000689 "Primary base should have a zero offset!");
690
691 AddVCallOffsets(BaseSubobject(PrimaryBase, Base.getBaseOffset()),
692 VBaseOffset);
693 }
694
695 // Add the vcall offsets.
Aaron Ballman2b124d12014-03-13 16:36:16 +0000696 for (const auto *MD : RD->methods()) {
Peter Collingbournecfd23562011-09-26 01:57:12 +0000697 if (!MD->isVirtual())
698 continue;
Reid Kleckner1cbd9aa2015-02-25 19:17:48 +0000699 MD = MD->getCanonicalDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +0000700
701 CharUnits OffsetOffset = getCurrentOffsetOffset();
702
703 // Don't add a vcall offset if we already have one for this member function
704 // signature.
705 if (!VCallOffsets.AddVCallOffset(MD, OffsetOffset))
706 continue;
707
708 CharUnits Offset = CharUnits::Zero();
709
710 if (Overriders) {
711 // Get the final overrider.
712 FinalOverriders::OverriderInfo Overrider =
713 Overriders->getOverrider(MD, Base.getBaseOffset());
714
715 /// The vcall offset is the offset from the virtual base to the object
716 /// where the function was overridden.
717 Offset = Overrider.Offset - VBaseOffset;
718 }
719
720 Components.push_back(
721 VTableComponent::MakeVCallOffset(Offset));
722 }
723
724 // And iterate over all non-virtual bases (ignoring the primary base).
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +0000725 for (const auto &B : RD->bases()) {
726 if (B.isVirtual())
Peter Collingbournecfd23562011-09-26 01:57:12 +0000727 continue;
728
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +0000729 const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +0000730 if (BaseDecl == PrimaryBase)
731 continue;
732
733 // Get the base offset of this base.
734 CharUnits BaseOffset = Base.getBaseOffset() +
735 Layout.getBaseClassOffset(BaseDecl);
736
737 AddVCallOffsets(BaseSubobject(BaseDecl, BaseOffset),
738 VBaseOffset);
739 }
740}
741
742void
743VCallAndVBaseOffsetBuilder::AddVBaseOffsets(const CXXRecordDecl *RD,
744 CharUnits OffsetInLayoutClass) {
745 const ASTRecordLayout &LayoutClassLayout =
746 Context.getASTRecordLayout(LayoutClass);
747
748 // Add vbase offsets.
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +0000749 for (const auto &B : RD->bases()) {
750 const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +0000751
752 // Check if this is a virtual base that we haven't visited before.
David Blaikie82e95a32014-11-19 07:49:47 +0000753 if (B.isVirtual() && VisitedVirtualBases.insert(BaseDecl).second) {
Peter Collingbournecfd23562011-09-26 01:57:12 +0000754 CharUnits Offset =
755 LayoutClassLayout.getVBaseClassOffset(BaseDecl) - OffsetInLayoutClass;
756
757 // Add the vbase offset offset.
758 assert(!VBaseOffsetOffsets.count(BaseDecl) &&
759 "vbase offset offset already exists!");
760
761 CharUnits VBaseOffsetOffset = getCurrentOffsetOffset();
762 VBaseOffsetOffsets.insert(
763 std::make_pair(BaseDecl, VBaseOffsetOffset));
764
765 Components.push_back(
766 VTableComponent::MakeVBaseOffset(Offset));
767 }
768
769 // Check the base class looking for more vbase offsets.
770 AddVBaseOffsets(BaseDecl, OffsetInLayoutClass);
771 }
772}
773
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +0000774/// ItaniumVTableBuilder - Class for building vtable layout information.
775class ItaniumVTableBuilder {
Peter Collingbournecfd23562011-09-26 01:57:12 +0000776public:
777 /// PrimaryBasesSetVectorTy - A set vector of direct and indirect
778 /// primary bases.
779 typedef llvm::SmallSetVector<const CXXRecordDecl *, 8>
780 PrimaryBasesSetVectorTy;
781
782 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits>
783 VBaseOffsetOffsetsMapTy;
784
785 typedef llvm::DenseMap<BaseSubobject, uint64_t>
786 AddressPointsMapTy;
787
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +0000788 typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVTableIndicesTy;
789
Peter Collingbournecfd23562011-09-26 01:57:12 +0000790private:
791 /// VTables - Global vtable information.
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +0000792 ItaniumVTableContext &VTables;
Peter Collingbournecfd23562011-09-26 01:57:12 +0000793
794 /// MostDerivedClass - The most derived class for which we're building this
795 /// vtable.
796 const CXXRecordDecl *MostDerivedClass;
797
798 /// MostDerivedClassOffset - If we're building a construction vtable, this
799 /// holds the offset from the layout class to the most derived class.
800 const CharUnits MostDerivedClassOffset;
801
802 /// MostDerivedClassIsVirtual - Whether the most derived class is a virtual
803 /// base. (This only makes sense when building a construction vtable).
804 bool MostDerivedClassIsVirtual;
805
806 /// LayoutClass - The class we're using for layout information. Will be
807 /// different than the most derived class if we're building a construction
808 /// vtable.
809 const CXXRecordDecl *LayoutClass;
810
811 /// Context - The ASTContext which we will use for layout information.
812 ASTContext &Context;
813
814 /// FinalOverriders - The final overriders of the most derived class.
815 const FinalOverriders Overriders;
816
817 /// VCallOffsetsForVBases - Keeps track of vcall offsets for the virtual
818 /// bases in this vtable.
819 llvm::DenseMap<const CXXRecordDecl *, VCallOffsetMap> VCallOffsetsForVBases;
820
821 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets for
822 /// the most derived class.
823 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
824
825 /// Components - The components of the vtable being built.
826 SmallVector<VTableComponent, 64> Components;
827
828 /// AddressPoints - Address points for the vtable being built.
829 AddressPointsMapTy AddressPoints;
830
831 /// MethodInfo - Contains information about a method in a vtable.
832 /// (Used for computing 'this' pointer adjustment thunks.
833 struct MethodInfo {
834 /// BaseOffset - The base offset of this method.
835 const CharUnits BaseOffset;
836
837 /// BaseOffsetInLayoutClass - The base offset in the layout class of this
838 /// method.
839 const CharUnits BaseOffsetInLayoutClass;
840
841 /// VTableIndex - The index in the vtable that this method has.
842 /// (For destructors, this is the index of the complete destructor).
843 const uint64_t VTableIndex;
844
845 MethodInfo(CharUnits BaseOffset, CharUnits BaseOffsetInLayoutClass,
846 uint64_t VTableIndex)
847 : BaseOffset(BaseOffset),
848 BaseOffsetInLayoutClass(BaseOffsetInLayoutClass),
849 VTableIndex(VTableIndex) { }
850
851 MethodInfo()
852 : BaseOffset(CharUnits::Zero()),
853 BaseOffsetInLayoutClass(CharUnits::Zero()),
854 VTableIndex(0) { }
855 };
856
857 typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy;
858
859 /// MethodInfoMap - The information for all methods in the vtable we're
860 /// currently building.
861 MethodInfoMapTy MethodInfoMap;
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +0000862
863 /// MethodVTableIndices - Contains the index (relative to the vtable address
864 /// point) where the function pointer for a virtual function is stored.
865 MethodVTableIndicesTy MethodVTableIndices;
866
Peter Collingbournecfd23562011-09-26 01:57:12 +0000867 typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy;
868
869 /// VTableThunks - The thunks by vtable index in the vtable currently being
870 /// built.
871 VTableThunksMapTy VTableThunks;
872
873 typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
874 typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
875
876 /// Thunks - A map that contains all the thunks needed for all methods in the
877 /// most derived class for which the vtable is currently being built.
878 ThunksMapTy Thunks;
879
880 /// AddThunk - Add a thunk for the given method.
881 void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk);
882
883 /// ComputeThisAdjustments - Compute the 'this' pointer adjustments for the
884 /// part of the vtable we're currently building.
885 void ComputeThisAdjustments();
886
887 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
888
889 /// PrimaryVirtualBases - All known virtual bases who are a primary base of
890 /// some other base.
891 VisitedVirtualBasesSetTy PrimaryVirtualBases;
892
893 /// ComputeReturnAdjustment - Compute the return adjustment given a return
894 /// adjustment base offset.
895 ReturnAdjustment ComputeReturnAdjustment(BaseOffset Offset);
896
897 /// ComputeThisAdjustmentBaseOffset - Compute the base offset for adjusting
898 /// the 'this' pointer from the base subobject to the derived subobject.
899 BaseOffset ComputeThisAdjustmentBaseOffset(BaseSubobject Base,
900 BaseSubobject Derived) const;
901
902 /// ComputeThisAdjustment - Compute the 'this' pointer adjustment for the
903 /// given virtual member function, its offset in the layout class and its
904 /// final overrider.
905 ThisAdjustment
906 ComputeThisAdjustment(const CXXMethodDecl *MD,
907 CharUnits BaseOffsetInLayoutClass,
908 FinalOverriders::OverriderInfo Overrider);
909
910 /// AddMethod - Add a single virtual member function to the vtable
911 /// components vector.
912 void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment);
913
914 /// IsOverriderUsed - Returns whether the overrider will ever be used in this
915 /// part of the vtable.
916 ///
917 /// Itanium C++ ABI 2.5.2:
918 ///
919 /// struct A { virtual void f(); };
920 /// struct B : virtual public A { int i; };
921 /// struct C : virtual public A { int j; };
922 /// struct D : public B, public C {};
923 ///
924 /// When B and C are declared, A is a primary base in each case, so although
925 /// vcall offsets are allocated in the A-in-B and A-in-C vtables, no this
926 /// adjustment is required and no thunk is generated. However, inside D
927 /// objects, A is no longer a primary base of C, so if we allowed calls to
928 /// C::f() to use the copy of A's vtable in the C subobject, we would need
929 /// to adjust this from C* to B::A*, which would require a third-party
930 /// thunk. Since we require that a call to C::f() first convert to A*,
931 /// C-in-D's copy of A's vtable is never referenced, so this is not
932 /// necessary.
933 bool IsOverriderUsed(const CXXMethodDecl *Overrider,
934 CharUnits BaseOffsetInLayoutClass,
935 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
936 CharUnits FirstBaseOffsetInLayoutClass) const;
937
938
939 /// AddMethods - Add the methods of this base subobject and all its
940 /// primary bases to the vtable components vector.
941 void AddMethods(BaseSubobject Base, CharUnits BaseOffsetInLayoutClass,
942 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
943 CharUnits FirstBaseOffsetInLayoutClass,
944 PrimaryBasesSetVectorTy &PrimaryBases);
945
946 // LayoutVTable - Layout the vtable for the given base class, including its
947 // secondary vtables and any vtables for virtual bases.
948 void LayoutVTable();
949
950 /// LayoutPrimaryAndSecondaryVTables - Layout the primary vtable for the
951 /// given base subobject, as well as all its secondary vtables.
952 ///
953 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
954 /// or a direct or indirect base of a virtual base.
955 ///
956 /// \param BaseIsVirtualInLayoutClass - Whether the base subobject is virtual
957 /// in the layout class.
958 void LayoutPrimaryAndSecondaryVTables(BaseSubobject Base,
959 bool BaseIsMorallyVirtual,
960 bool BaseIsVirtualInLayoutClass,
961 CharUnits OffsetInLayoutClass);
962
963 /// LayoutSecondaryVTables - Layout the secondary vtables for the given base
964 /// subobject.
965 ///
966 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
967 /// or a direct or indirect base of a virtual base.
968 void LayoutSecondaryVTables(BaseSubobject Base, bool BaseIsMorallyVirtual,
969 CharUnits OffsetInLayoutClass);
970
971 /// DeterminePrimaryVirtualBases - Determine the primary virtual bases in this
972 /// class hierarchy.
973 void DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
974 CharUnits OffsetInLayoutClass,
975 VisitedVirtualBasesSetTy &VBases);
976
977 /// LayoutVTablesForVirtualBases - Layout vtables for all virtual bases of the
978 /// given base (excluding any primary bases).
979 void LayoutVTablesForVirtualBases(const CXXRecordDecl *RD,
980 VisitedVirtualBasesSetTy &VBases);
981
982 /// isBuildingConstructionVTable - Return whether this vtable builder is
983 /// building a construction vtable.
984 bool isBuildingConstructorVTable() const {
985 return MostDerivedClass != LayoutClass;
986 }
987
988public:
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +0000989 ItaniumVTableBuilder(ItaniumVTableContext &VTables,
990 const CXXRecordDecl *MostDerivedClass,
991 CharUnits MostDerivedClassOffset,
992 bool MostDerivedClassIsVirtual,
993 const CXXRecordDecl *LayoutClass)
994 : VTables(VTables), MostDerivedClass(MostDerivedClass),
995 MostDerivedClassOffset(MostDerivedClassOffset),
996 MostDerivedClassIsVirtual(MostDerivedClassIsVirtual),
997 LayoutClass(LayoutClass), Context(MostDerivedClass->getASTContext()),
998 Overriders(MostDerivedClass, MostDerivedClassOffset, LayoutClass) {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000999 assert(!Context.getTargetInfo().getCXXABI().isMicrosoft());
Peter Collingbournecfd23562011-09-26 01:57:12 +00001000
1001 LayoutVTable();
1002
David Blaikiebbafb8a2012-03-11 07:00:24 +00001003 if (Context.getLangOpts().DumpVTableLayouts)
Reid Kleckner5bc6d0f2013-11-08 21:28:00 +00001004 dumpLayout(llvm::outs());
Peter Collingbournecfd23562011-09-26 01:57:12 +00001005 }
1006
1007 uint64_t getNumThunks() const {
1008 return Thunks.size();
1009 }
1010
1011 ThunksMapTy::const_iterator thunks_begin() const {
1012 return Thunks.begin();
1013 }
1014
1015 ThunksMapTy::const_iterator thunks_end() const {
1016 return Thunks.end();
1017 }
1018
1019 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
1020 return VBaseOffsetOffsets;
1021 }
1022
1023 const AddressPointsMapTy &getAddressPoints() const {
1024 return AddressPoints;
1025 }
1026
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00001027 MethodVTableIndicesTy::const_iterator vtable_indices_begin() const {
1028 return MethodVTableIndices.begin();
1029 }
1030
1031 MethodVTableIndicesTy::const_iterator vtable_indices_end() const {
1032 return MethodVTableIndices.end();
1033 }
1034
Peter Collingbournecfd23562011-09-26 01:57:12 +00001035 /// getNumVTableComponents - Return the number of components in the vtable
1036 /// currently built.
1037 uint64_t getNumVTableComponents() const {
1038 return Components.size();
1039 }
1040
1041 const VTableComponent *vtable_component_begin() const {
1042 return Components.begin();
1043 }
1044
1045 const VTableComponent *vtable_component_end() const {
1046 return Components.end();
1047 }
1048
1049 AddressPointsMapTy::const_iterator address_points_begin() const {
1050 return AddressPoints.begin();
1051 }
1052
1053 AddressPointsMapTy::const_iterator address_points_end() const {
1054 return AddressPoints.end();
1055 }
1056
1057 VTableThunksMapTy::const_iterator vtable_thunks_begin() const {
1058 return VTableThunks.begin();
1059 }
1060
1061 VTableThunksMapTy::const_iterator vtable_thunks_end() const {
1062 return VTableThunks.end();
1063 }
1064
1065 /// dumpLayout - Dump the vtable layout.
1066 void dumpLayout(raw_ostream&);
1067};
1068
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001069void ItaniumVTableBuilder::AddThunk(const CXXMethodDecl *MD,
1070 const ThunkInfo &Thunk) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001071 assert(!isBuildingConstructorVTable() &&
1072 "Can't add thunks for construction vtable");
1073
Craig Topper5603df42013-07-05 19:34:19 +00001074 SmallVectorImpl<ThunkInfo> &ThunksVector = Thunks[MD];
1075
Peter Collingbournecfd23562011-09-26 01:57:12 +00001076 // Check if we have this thunk already.
1077 if (std::find(ThunksVector.begin(), ThunksVector.end(), Thunk) !=
1078 ThunksVector.end())
1079 return;
1080
1081 ThunksVector.push_back(Thunk);
1082}
1083
1084typedef llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverriddenMethodsSetTy;
1085
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001086/// Visit all the methods overridden by the given method recursively,
1087/// in a depth-first pre-order. The Visitor's visitor method returns a bool
1088/// indicating whether to continue the recursion for the given overridden
1089/// method (i.e. returning false stops the iteration).
1090template <class VisitorTy>
1091static void
1092visitAllOverriddenMethods(const CXXMethodDecl *MD, VisitorTy &Visitor) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001093 assert(MD->isVirtual() && "Method is not virtual!");
1094
1095 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
1096 E = MD->end_overridden_methods(); I != E; ++I) {
1097 const CXXMethodDecl *OverriddenMD = *I;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001098 if (!Visitor.visit(OverriddenMD))
1099 continue;
1100 visitAllOverriddenMethods(OverriddenMD, Visitor);
Peter Collingbournecfd23562011-09-26 01:57:12 +00001101 }
1102}
1103
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001104namespace {
1105 struct OverriddenMethodsCollector {
1106 OverriddenMethodsSetTy *Methods;
1107
1108 bool visit(const CXXMethodDecl *MD) {
1109 // Don't recurse on this method if we've already collected it.
David Blaikie82e95a32014-11-19 07:49:47 +00001110 return Methods->insert(MD).second;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001111 }
1112 };
1113}
1114
1115/// ComputeAllOverriddenMethods - Given a method decl, will return a set of all
1116/// the overridden methods that the function decl overrides.
1117static void
1118ComputeAllOverriddenMethods(const CXXMethodDecl *MD,
1119 OverriddenMethodsSetTy& OverriddenMethods) {
1120 OverriddenMethodsCollector Collector = { &OverriddenMethods };
1121 visitAllOverriddenMethods(MD, Collector);
1122}
1123
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001124void ItaniumVTableBuilder::ComputeThisAdjustments() {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001125 // Now go through the method info map and see if any of the methods need
1126 // 'this' pointer adjustments.
1127 for (MethodInfoMapTy::const_iterator I = MethodInfoMap.begin(),
1128 E = MethodInfoMap.end(); I != E; ++I) {
1129 const CXXMethodDecl *MD = I->first;
1130 const MethodInfo &MethodInfo = I->second;
1131
1132 // Ignore adjustments for unused function pointers.
1133 uint64_t VTableIndex = MethodInfo.VTableIndex;
1134 if (Components[VTableIndex].getKind() ==
1135 VTableComponent::CK_UnusedFunctionPointer)
1136 continue;
1137
1138 // Get the final overrider for this method.
1139 FinalOverriders::OverriderInfo Overrider =
1140 Overriders.getOverrider(MD, MethodInfo.BaseOffset);
1141
1142 // Check if we need an adjustment at all.
1143 if (MethodInfo.BaseOffsetInLayoutClass == Overrider.Offset) {
1144 // When a return thunk is needed by a derived class that overrides a
1145 // virtual base, gcc uses a virtual 'this' adjustment as well.
1146 // While the thunk itself might be needed by vtables in subclasses or
1147 // in construction vtables, there doesn't seem to be a reason for using
1148 // the thunk in this vtable. Still, we do so to match gcc.
1149 if (VTableThunks.lookup(VTableIndex).Return.isEmpty())
1150 continue;
1151 }
1152
1153 ThisAdjustment ThisAdjustment =
1154 ComputeThisAdjustment(MD, MethodInfo.BaseOffsetInLayoutClass, Overrider);
1155
1156 if (ThisAdjustment.isEmpty())
1157 continue;
1158
1159 // Add it.
1160 VTableThunks[VTableIndex].This = ThisAdjustment;
1161
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001162 if (isa<CXXDestructorDecl>(MD)) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001163 // Add an adjustment for the deleting destructor as well.
1164 VTableThunks[VTableIndex + 1].This = ThisAdjustment;
1165 }
1166 }
1167
1168 /// Clear the method info map.
1169 MethodInfoMap.clear();
1170
1171 if (isBuildingConstructorVTable()) {
1172 // We don't need to store thunk information for construction vtables.
1173 return;
1174 }
1175
1176 for (VTableThunksMapTy::const_iterator I = VTableThunks.begin(),
1177 E = VTableThunks.end(); I != E; ++I) {
1178 const VTableComponent &Component = Components[I->first];
1179 const ThunkInfo &Thunk = I->second;
1180 const CXXMethodDecl *MD;
1181
1182 switch (Component.getKind()) {
1183 default:
1184 llvm_unreachable("Unexpected vtable component kind!");
1185 case VTableComponent::CK_FunctionPointer:
1186 MD = Component.getFunctionDecl();
1187 break;
1188 case VTableComponent::CK_CompleteDtorPointer:
1189 MD = Component.getDestructorDecl();
1190 break;
1191 case VTableComponent::CK_DeletingDtorPointer:
1192 // We've already added the thunk when we saw the complete dtor pointer.
1193 continue;
1194 }
1195
1196 if (MD->getParent() == MostDerivedClass)
1197 AddThunk(MD, Thunk);
1198 }
1199}
1200
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001201ReturnAdjustment
1202ItaniumVTableBuilder::ComputeReturnAdjustment(BaseOffset Offset) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001203 ReturnAdjustment Adjustment;
1204
1205 if (!Offset.isEmpty()) {
1206 if (Offset.VirtualBase) {
1207 // Get the virtual base offset offset.
1208 if (Offset.DerivedClass == MostDerivedClass) {
1209 // We can get the offset offset directly from our map.
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001210 Adjustment.Virtual.Itanium.VBaseOffsetOffset =
Peter Collingbournecfd23562011-09-26 01:57:12 +00001211 VBaseOffsetOffsets.lookup(Offset.VirtualBase).getQuantity();
1212 } else {
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001213 Adjustment.Virtual.Itanium.VBaseOffsetOffset =
Peter Collingbournecfd23562011-09-26 01:57:12 +00001214 VTables.getVirtualBaseOffsetOffset(Offset.DerivedClass,
1215 Offset.VirtualBase).getQuantity();
1216 }
1217 }
1218
1219 Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity();
1220 }
1221
1222 return Adjustment;
1223}
1224
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001225BaseOffset ItaniumVTableBuilder::ComputeThisAdjustmentBaseOffset(
1226 BaseSubobject Base, BaseSubobject Derived) const {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001227 const CXXRecordDecl *BaseRD = Base.getBase();
1228 const CXXRecordDecl *DerivedRD = Derived.getBase();
1229
1230 CXXBasePaths Paths(/*FindAmbiguities=*/true,
1231 /*RecordPaths=*/true, /*DetectVirtual=*/true);
1232
Benjamin Kramer325d7452013-02-03 18:55:34 +00001233 if (!DerivedRD->isDerivedFrom(BaseRD, Paths))
Peter Collingbournecfd23562011-09-26 01:57:12 +00001234 llvm_unreachable("Class must be derived from the passed in base class!");
Peter Collingbournecfd23562011-09-26 01:57:12 +00001235
1236 // We have to go through all the paths, and see which one leads us to the
1237 // right base subobject.
1238 for (CXXBasePaths::const_paths_iterator I = Paths.begin(), E = Paths.end();
1239 I != E; ++I) {
1240 BaseOffset Offset = ComputeBaseOffset(Context, DerivedRD, *I);
1241
1242 CharUnits OffsetToBaseSubobject = Offset.NonVirtualOffset;
1243
1244 if (Offset.VirtualBase) {
1245 // If we have a virtual base class, the non-virtual offset is relative
1246 // to the virtual base class offset.
1247 const ASTRecordLayout &LayoutClassLayout =
1248 Context.getASTRecordLayout(LayoutClass);
1249
1250 /// Get the virtual base offset, relative to the most derived class
1251 /// layout.
1252 OffsetToBaseSubobject +=
1253 LayoutClassLayout.getVBaseClassOffset(Offset.VirtualBase);
1254 } else {
1255 // Otherwise, the non-virtual offset is relative to the derived class
1256 // offset.
1257 OffsetToBaseSubobject += Derived.getBaseOffset();
1258 }
1259
1260 // Check if this path gives us the right base subobject.
1261 if (OffsetToBaseSubobject == Base.getBaseOffset()) {
1262 // Since we're going from the base class _to_ the derived class, we'll
1263 // invert the non-virtual offset here.
1264 Offset.NonVirtualOffset = -Offset.NonVirtualOffset;
1265 return Offset;
1266 }
1267 }
1268
1269 return BaseOffset();
1270}
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001271
1272ThisAdjustment ItaniumVTableBuilder::ComputeThisAdjustment(
1273 const CXXMethodDecl *MD, CharUnits BaseOffsetInLayoutClass,
1274 FinalOverriders::OverriderInfo Overrider) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001275 // Ignore adjustments for pure virtual member functions.
1276 if (Overrider.Method->isPure())
1277 return ThisAdjustment();
1278
1279 BaseSubobject OverriddenBaseSubobject(MD->getParent(),
1280 BaseOffsetInLayoutClass);
1281
1282 BaseSubobject OverriderBaseSubobject(Overrider.Method->getParent(),
1283 Overrider.Offset);
1284
1285 // Compute the adjustment offset.
1286 BaseOffset Offset = ComputeThisAdjustmentBaseOffset(OverriddenBaseSubobject,
1287 OverriderBaseSubobject);
1288 if (Offset.isEmpty())
1289 return ThisAdjustment();
1290
1291 ThisAdjustment Adjustment;
1292
1293 if (Offset.VirtualBase) {
1294 // Get the vcall offset map for this virtual base.
1295 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Offset.VirtualBase];
1296
1297 if (VCallOffsets.empty()) {
1298 // We don't have vcall offsets for this virtual base, go ahead and
1299 // build them.
1300 VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, MostDerivedClass,
Craig Topper36250ad2014-05-12 05:36:57 +00001301 /*FinalOverriders=*/nullptr,
Peter Collingbournecfd23562011-09-26 01:57:12 +00001302 BaseSubobject(Offset.VirtualBase,
1303 CharUnits::Zero()),
1304 /*BaseIsVirtual=*/true,
1305 /*OffsetInLayoutClass=*/
1306 CharUnits::Zero());
1307
1308 VCallOffsets = Builder.getVCallOffsets();
1309 }
1310
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00001311 Adjustment.Virtual.Itanium.VCallOffsetOffset =
Peter Collingbournecfd23562011-09-26 01:57:12 +00001312 VCallOffsets.getVCallOffsetOffset(MD).getQuantity();
1313 }
1314
1315 // Set the non-virtual part of the adjustment.
1316 Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity();
1317
1318 return Adjustment;
1319}
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001320
1321void ItaniumVTableBuilder::AddMethod(const CXXMethodDecl *MD,
1322 ReturnAdjustment ReturnAdjustment) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001323 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1324 assert(ReturnAdjustment.isEmpty() &&
1325 "Destructor can't have return adjustment!");
1326
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001327 // Add both the complete destructor and the deleting destructor.
1328 Components.push_back(VTableComponent::MakeCompleteDtor(DD));
1329 Components.push_back(VTableComponent::MakeDeletingDtor(DD));
Peter Collingbournecfd23562011-09-26 01:57:12 +00001330 } else {
1331 // Add the return adjustment if necessary.
1332 if (!ReturnAdjustment.isEmpty())
1333 VTableThunks[Components.size()].Return = ReturnAdjustment;
1334
1335 // Add the function.
1336 Components.push_back(VTableComponent::MakeFunction(MD));
1337 }
1338}
1339
1340/// OverridesIndirectMethodInBase - Return whether the given member function
1341/// overrides any methods in the set of given bases.
1342/// Unlike OverridesMethodInBase, this checks "overriders of overriders".
1343/// For example, if we have:
1344///
1345/// struct A { virtual void f(); }
1346/// struct B : A { virtual void f(); }
1347/// struct C : B { virtual void f(); }
1348///
1349/// OverridesIndirectMethodInBase will return true if given C::f as the method
1350/// and { A } as the set of bases.
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001351static bool OverridesIndirectMethodInBases(
1352 const CXXMethodDecl *MD,
1353 ItaniumVTableBuilder::PrimaryBasesSetVectorTy &Bases) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001354 if (Bases.count(MD->getParent()))
1355 return true;
1356
1357 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
1358 E = MD->end_overridden_methods(); I != E; ++I) {
1359 const CXXMethodDecl *OverriddenMD = *I;
1360
1361 // Check "indirect overriders".
1362 if (OverridesIndirectMethodInBases(OverriddenMD, Bases))
1363 return true;
1364 }
1365
1366 return false;
1367}
1368
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001369bool ItaniumVTableBuilder::IsOverriderUsed(
1370 const CXXMethodDecl *Overrider, CharUnits BaseOffsetInLayoutClass,
1371 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
1372 CharUnits FirstBaseOffsetInLayoutClass) const {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001373 // If the base and the first base in the primary base chain have the same
1374 // offsets, then this overrider will be used.
1375 if (BaseOffsetInLayoutClass == FirstBaseOffsetInLayoutClass)
1376 return true;
1377
1378 // We know now that Base (or a direct or indirect base of it) is a primary
1379 // base in part of the class hierarchy, but not a primary base in the most
1380 // derived class.
1381
1382 // If the overrider is the first base in the primary base chain, we know
1383 // that the overrider will be used.
1384 if (Overrider->getParent() == FirstBaseInPrimaryBaseChain)
1385 return true;
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001386
1387 ItaniumVTableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
Peter Collingbournecfd23562011-09-26 01:57:12 +00001388
1389 const CXXRecordDecl *RD = FirstBaseInPrimaryBaseChain;
1390 PrimaryBases.insert(RD);
1391
1392 // Now traverse the base chain, starting with the first base, until we find
1393 // the base that is no longer a primary base.
1394 while (true) {
1395 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1396 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1397
1398 if (!PrimaryBase)
1399 break;
1400
1401 if (Layout.isPrimaryBaseVirtual()) {
Benjamin Kramer2ef30312012-07-04 18:45:14 +00001402 assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() &&
Peter Collingbournecfd23562011-09-26 01:57:12 +00001403 "Primary base should always be at offset 0!");
1404
1405 const ASTRecordLayout &LayoutClassLayout =
1406 Context.getASTRecordLayout(LayoutClass);
1407
1408 // Now check if this is the primary base that is not a primary base in the
1409 // most derived class.
1410 if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
1411 FirstBaseOffsetInLayoutClass) {
1412 // We found it, stop walking the chain.
1413 break;
1414 }
1415 } else {
Benjamin Kramer2ef30312012-07-04 18:45:14 +00001416 assert(Layout.getBaseClassOffset(PrimaryBase).isZero() &&
Peter Collingbournecfd23562011-09-26 01:57:12 +00001417 "Primary base should always be at offset 0!");
1418 }
1419
1420 if (!PrimaryBases.insert(PrimaryBase))
1421 llvm_unreachable("Found a duplicate primary base!");
1422
1423 RD = PrimaryBase;
1424 }
1425
1426 // If the final overrider is an override of one of the primary bases,
1427 // then we know that it will be used.
1428 return OverridesIndirectMethodInBases(Overrider, PrimaryBases);
1429}
1430
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001431typedef llvm::SmallSetVector<const CXXRecordDecl *, 8> BasesSetVectorTy;
1432
Peter Collingbournecfd23562011-09-26 01:57:12 +00001433/// FindNearestOverriddenMethod - Given a method, returns the overridden method
1434/// from the nearest base. Returns null if no method was found.
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001435/// The Bases are expected to be sorted in a base-to-derived order.
1436static const CXXMethodDecl *
Peter Collingbournecfd23562011-09-26 01:57:12 +00001437FindNearestOverriddenMethod(const CXXMethodDecl *MD,
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001438 BasesSetVectorTy &Bases) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001439 OverriddenMethodsSetTy OverriddenMethods;
1440 ComputeAllOverriddenMethods(MD, OverriddenMethods);
1441
1442 for (int I = Bases.size(), E = 0; I != E; --I) {
1443 const CXXRecordDecl *PrimaryBase = Bases[I - 1];
1444
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001445 // Now check the overridden methods.
Peter Collingbournecfd23562011-09-26 01:57:12 +00001446 for (OverriddenMethodsSetTy::const_iterator I = OverriddenMethods.begin(),
1447 E = OverriddenMethods.end(); I != E; ++I) {
1448 const CXXMethodDecl *OverriddenMD = *I;
1449
1450 // We found our overridden method.
1451 if (OverriddenMD->getParent() == PrimaryBase)
1452 return OverriddenMD;
1453 }
1454 }
Craig Topper36250ad2014-05-12 05:36:57 +00001455
1456 return nullptr;
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001457}
Peter Collingbournecfd23562011-09-26 01:57:12 +00001458
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001459void ItaniumVTableBuilder::AddMethods(
1460 BaseSubobject Base, CharUnits BaseOffsetInLayoutClass,
1461 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
1462 CharUnits FirstBaseOffsetInLayoutClass,
1463 PrimaryBasesSetVectorTy &PrimaryBases) {
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00001464 // Itanium C++ ABI 2.5.2:
1465 // The order of the virtual function pointers in a virtual table is the
1466 // order of declaration of the corresponding member functions in the class.
1467 //
1468 // There is an entry for any virtual function declared in a class,
1469 // whether it is a new function or overrides a base class function,
1470 // unless it overrides a function from the primary base, and conversion
1471 // between their return types does not require an adjustment.
1472
Peter Collingbournecfd23562011-09-26 01:57:12 +00001473 const CXXRecordDecl *RD = Base.getBase();
1474 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1475
1476 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
1477 CharUnits PrimaryBaseOffset;
1478 CharUnits PrimaryBaseOffsetInLayoutClass;
1479 if (Layout.isPrimaryBaseVirtual()) {
Benjamin Kramer2ef30312012-07-04 18:45:14 +00001480 assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() &&
Peter Collingbournecfd23562011-09-26 01:57:12 +00001481 "Primary vbase should have a zero offset!");
1482
1483 const ASTRecordLayout &MostDerivedClassLayout =
1484 Context.getASTRecordLayout(MostDerivedClass);
1485
1486 PrimaryBaseOffset =
1487 MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
1488
1489 const ASTRecordLayout &LayoutClassLayout =
1490 Context.getASTRecordLayout(LayoutClass);
1491
1492 PrimaryBaseOffsetInLayoutClass =
1493 LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
1494 } else {
Benjamin Kramer2ef30312012-07-04 18:45:14 +00001495 assert(Layout.getBaseClassOffset(PrimaryBase).isZero() &&
Peter Collingbournecfd23562011-09-26 01:57:12 +00001496 "Primary base should have a zero offset!");
1497
1498 PrimaryBaseOffset = Base.getBaseOffset();
1499 PrimaryBaseOffsetInLayoutClass = BaseOffsetInLayoutClass;
1500 }
1501
1502 AddMethods(BaseSubobject(PrimaryBase, PrimaryBaseOffset),
1503 PrimaryBaseOffsetInLayoutClass, FirstBaseInPrimaryBaseChain,
1504 FirstBaseOffsetInLayoutClass, PrimaryBases);
1505
1506 if (!PrimaryBases.insert(PrimaryBase))
1507 llvm_unreachable("Found a duplicate primary base!");
1508 }
1509
Craig Topper36250ad2014-05-12 05:36:57 +00001510 const CXXDestructorDecl *ImplicitVirtualDtor = nullptr;
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00001511
1512 typedef llvm::SmallVector<const CXXMethodDecl *, 8> NewVirtualFunctionsTy;
1513 NewVirtualFunctionsTy NewVirtualFunctions;
1514
Peter Collingbournecfd23562011-09-26 01:57:12 +00001515 // Now go through all virtual member functions and add them.
Aaron Ballman2b124d12014-03-13 16:36:16 +00001516 for (const auto *MD : RD->methods()) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001517 if (!MD->isVirtual())
1518 continue;
Reid Kleckner1cbd9aa2015-02-25 19:17:48 +00001519 MD = MD->getCanonicalDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +00001520
1521 // Get the final overrider.
1522 FinalOverriders::OverriderInfo Overrider =
1523 Overriders.getOverrider(MD, Base.getBaseOffset());
1524
1525 // Check if this virtual member function overrides a method in a primary
1526 // base. If this is the case, and the return type doesn't require adjustment
1527 // then we can just use the member function from the primary base.
1528 if (const CXXMethodDecl *OverriddenMD =
1529 FindNearestOverriddenMethod(MD, PrimaryBases)) {
1530 if (ComputeReturnAdjustmentBaseOffset(Context, MD,
1531 OverriddenMD).isEmpty()) {
1532 // Replace the method info of the overridden method with our own
1533 // method.
1534 assert(MethodInfoMap.count(OverriddenMD) &&
1535 "Did not find the overridden method!");
1536 MethodInfo &OverriddenMethodInfo = MethodInfoMap[OverriddenMD];
1537
1538 MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
1539 OverriddenMethodInfo.VTableIndex);
1540
1541 assert(!MethodInfoMap.count(MD) &&
1542 "Should not have method info for this method yet!");
1543
1544 MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
1545 MethodInfoMap.erase(OverriddenMD);
1546
1547 // If the overridden method exists in a virtual base class or a direct
1548 // or indirect base class of a virtual base class, we need to emit a
1549 // thunk if we ever have a class hierarchy where the base class is not
1550 // a primary base in the complete object.
1551 if (!isBuildingConstructorVTable() && OverriddenMD != MD) {
1552 // Compute the this adjustment.
1553 ThisAdjustment ThisAdjustment =
1554 ComputeThisAdjustment(OverriddenMD, BaseOffsetInLayoutClass,
1555 Overrider);
1556
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00001557 if (ThisAdjustment.Virtual.Itanium.VCallOffsetOffset &&
Peter Collingbournecfd23562011-09-26 01:57:12 +00001558 Overrider.Method->getParent() == MostDerivedClass) {
1559
1560 // There's no return adjustment from OverriddenMD and MD,
1561 // but that doesn't mean there isn't one between MD and
1562 // the final overrider.
1563 BaseOffset ReturnAdjustmentOffset =
1564 ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD);
1565 ReturnAdjustment ReturnAdjustment =
1566 ComputeReturnAdjustment(ReturnAdjustmentOffset);
1567
1568 // This is a virtual thunk for the most derived class, add it.
1569 AddThunk(Overrider.Method,
1570 ThunkInfo(ThisAdjustment, ReturnAdjustment));
1571 }
1572 }
1573
1574 continue;
1575 }
1576 }
1577
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00001578 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1579 if (MD->isImplicit()) {
1580 // Itanium C++ ABI 2.5.2:
1581 // If a class has an implicitly-defined virtual destructor,
1582 // its entries come after the declared virtual function pointers.
1583
1584 assert(!ImplicitVirtualDtor &&
1585 "Did already see an implicit virtual dtor!");
1586 ImplicitVirtualDtor = DD;
1587 continue;
1588 }
1589 }
1590
1591 NewVirtualFunctions.push_back(MD);
1592 }
1593
1594 if (ImplicitVirtualDtor)
1595 NewVirtualFunctions.push_back(ImplicitVirtualDtor);
1596
1597 for (NewVirtualFunctionsTy::const_iterator I = NewVirtualFunctions.begin(),
1598 E = NewVirtualFunctions.end(); I != E; ++I) {
1599 const CXXMethodDecl *MD = *I;
1600
1601 // Get the final overrider.
1602 FinalOverriders::OverriderInfo Overrider =
1603 Overriders.getOverrider(MD, Base.getBaseOffset());
1604
Peter Collingbournecfd23562011-09-26 01:57:12 +00001605 // Insert the method info for this method.
1606 MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
1607 Components.size());
1608
1609 assert(!MethodInfoMap.count(MD) &&
1610 "Should not have method info for this method yet!");
1611 MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
1612
1613 // Check if this overrider is going to be used.
1614 const CXXMethodDecl *OverriderMD = Overrider.Method;
1615 if (!IsOverriderUsed(OverriderMD, BaseOffsetInLayoutClass,
1616 FirstBaseInPrimaryBaseChain,
1617 FirstBaseOffsetInLayoutClass)) {
1618 Components.push_back(VTableComponent::MakeUnusedFunction(OverriderMD));
1619 continue;
1620 }
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00001621
Peter Collingbournecfd23562011-09-26 01:57:12 +00001622 // Check if this overrider needs a return adjustment.
1623 // We don't want to do this for pure virtual member functions.
1624 BaseOffset ReturnAdjustmentOffset;
1625 if (!OverriderMD->isPure()) {
1626 ReturnAdjustmentOffset =
1627 ComputeReturnAdjustmentBaseOffset(Context, OverriderMD, MD);
1628 }
1629
1630 ReturnAdjustment ReturnAdjustment =
1631 ComputeReturnAdjustment(ReturnAdjustmentOffset);
1632
1633 AddMethod(Overrider.Method, ReturnAdjustment);
1634 }
1635}
1636
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001637void ItaniumVTableBuilder::LayoutVTable() {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001638 LayoutPrimaryAndSecondaryVTables(BaseSubobject(MostDerivedClass,
1639 CharUnits::Zero()),
1640 /*BaseIsMorallyVirtual=*/false,
1641 MostDerivedClassIsVirtual,
1642 MostDerivedClassOffset);
1643
1644 VisitedVirtualBasesSetTy VBases;
1645
1646 // Determine the primary virtual bases.
1647 DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset,
1648 VBases);
1649 VBases.clear();
1650
1651 LayoutVTablesForVirtualBases(MostDerivedClass, VBases);
1652
1653 // -fapple-kext adds an extra entry at end of vtbl.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001654 bool IsAppleKext = Context.getLangOpts().AppleKext;
Peter Collingbournecfd23562011-09-26 01:57:12 +00001655 if (IsAppleKext)
1656 Components.push_back(VTableComponent::MakeVCallOffset(CharUnits::Zero()));
1657}
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001658
1659void ItaniumVTableBuilder::LayoutPrimaryAndSecondaryVTables(
1660 BaseSubobject Base, bool BaseIsMorallyVirtual,
1661 bool BaseIsVirtualInLayoutClass, CharUnits OffsetInLayoutClass) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001662 assert(Base.getBase()->isDynamicClass() && "class does not have a vtable!");
1663
1664 // Add vcall and vbase offsets for this vtable.
1665 VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, LayoutClass, &Overriders,
1666 Base, BaseIsVirtualInLayoutClass,
1667 OffsetInLayoutClass);
1668 Components.append(Builder.components_begin(), Builder.components_end());
1669
1670 // Check if we need to add these vcall offsets.
1671 if (BaseIsVirtualInLayoutClass && !Builder.getVCallOffsets().empty()) {
1672 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()];
1673
1674 if (VCallOffsets.empty())
1675 VCallOffsets = Builder.getVCallOffsets();
1676 }
1677
1678 // If we're laying out the most derived class we want to keep track of the
1679 // virtual base class offset offsets.
1680 if (Base.getBase() == MostDerivedClass)
1681 VBaseOffsetOffsets = Builder.getVBaseOffsetOffsets();
1682
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001683 // Add the offset to top.
1684 CharUnits OffsetToTop = MostDerivedClassOffset - OffsetInLayoutClass;
1685 Components.push_back(VTableComponent::MakeOffsetToTop(OffsetToTop));
Timur Iskhodzhanov52b8a052013-01-21 13:02:41 +00001686
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001687 // Next, add the RTTI.
1688 Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass));
Timur Iskhodzhanov52b8a052013-01-21 13:02:41 +00001689
Peter Collingbournecfd23562011-09-26 01:57:12 +00001690 uint64_t AddressPoint = Components.size();
1691
1692 // Now go through all virtual member functions and add them.
1693 PrimaryBasesSetVectorTy PrimaryBases;
1694 AddMethods(Base, OffsetInLayoutClass,
1695 Base.getBase(), OffsetInLayoutClass,
1696 PrimaryBases);
1697
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00001698 const CXXRecordDecl *RD = Base.getBase();
1699 if (RD == MostDerivedClass) {
1700 assert(MethodVTableIndices.empty());
1701 for (MethodInfoMapTy::const_iterator I = MethodInfoMap.begin(),
1702 E = MethodInfoMap.end(); I != E; ++I) {
1703 const CXXMethodDecl *MD = I->first;
1704 const MethodInfo &MI = I->second;
1705 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001706 MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)]
1707 = MI.VTableIndex - AddressPoint;
1708 MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)]
1709 = MI.VTableIndex + 1 - AddressPoint;
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00001710 } else {
1711 MethodVTableIndices[MD] = MI.VTableIndex - AddressPoint;
1712 }
1713 }
1714 }
1715
Peter Collingbournecfd23562011-09-26 01:57:12 +00001716 // Compute 'this' pointer adjustments.
1717 ComputeThisAdjustments();
1718
1719 // Add all address points.
Peter Collingbournecfd23562011-09-26 01:57:12 +00001720 while (true) {
1721 AddressPoints.insert(std::make_pair(
1722 BaseSubobject(RD, OffsetInLayoutClass),
1723 AddressPoint));
1724
1725 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1726 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1727
1728 if (!PrimaryBase)
1729 break;
1730
1731 if (Layout.isPrimaryBaseVirtual()) {
1732 // Check if this virtual primary base is a primary base in the layout
1733 // class. If it's not, we don't want to add it.
1734 const ASTRecordLayout &LayoutClassLayout =
1735 Context.getASTRecordLayout(LayoutClass);
1736
1737 if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
1738 OffsetInLayoutClass) {
1739 // We don't want to add this class (or any of its primary bases).
1740 break;
1741 }
1742 }
1743
1744 RD = PrimaryBase;
1745 }
1746
1747 // Layout secondary vtables.
1748 LayoutSecondaryVTables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass);
1749}
1750
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001751void
1752ItaniumVTableBuilder::LayoutSecondaryVTables(BaseSubobject Base,
1753 bool BaseIsMorallyVirtual,
1754 CharUnits OffsetInLayoutClass) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001755 // Itanium C++ ABI 2.5.2:
1756 // Following the primary virtual table of a derived class are secondary
1757 // virtual tables for each of its proper base classes, except any primary
1758 // base(s) with which it shares its primary virtual table.
1759
1760 const CXXRecordDecl *RD = Base.getBase();
1761 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1762 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1763
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00001764 for (const auto &B : RD->bases()) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001765 // Ignore virtual bases, we'll emit them later.
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00001766 if (B.isVirtual())
Peter Collingbournecfd23562011-09-26 01:57:12 +00001767 continue;
1768
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00001769 const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +00001770
1771 // Ignore bases that don't have a vtable.
1772 if (!BaseDecl->isDynamicClass())
1773 continue;
1774
1775 if (isBuildingConstructorVTable()) {
1776 // Itanium C++ ABI 2.6.4:
1777 // Some of the base class subobjects may not need construction virtual
1778 // tables, which will therefore not be present in the construction
1779 // virtual table group, even though the subobject virtual tables are
1780 // present in the main virtual table group for the complete object.
1781 if (!BaseIsMorallyVirtual && !BaseDecl->getNumVBases())
1782 continue;
1783 }
1784
1785 // Get the base offset of this base.
1786 CharUnits RelativeBaseOffset = Layout.getBaseClassOffset(BaseDecl);
1787 CharUnits BaseOffset = Base.getBaseOffset() + RelativeBaseOffset;
1788
1789 CharUnits BaseOffsetInLayoutClass =
1790 OffsetInLayoutClass + RelativeBaseOffset;
1791
1792 // Don't emit a secondary vtable for a primary base. We might however want
1793 // to emit secondary vtables for other bases of this base.
1794 if (BaseDecl == PrimaryBase) {
1795 LayoutSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset),
1796 BaseIsMorallyVirtual, BaseOffsetInLayoutClass);
1797 continue;
1798 }
1799
1800 // Layout the primary vtable (and any secondary vtables) for this base.
1801 LayoutPrimaryAndSecondaryVTables(
1802 BaseSubobject(BaseDecl, BaseOffset),
1803 BaseIsMorallyVirtual,
1804 /*BaseIsVirtualInLayoutClass=*/false,
1805 BaseOffsetInLayoutClass);
1806 }
1807}
1808
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001809void ItaniumVTableBuilder::DeterminePrimaryVirtualBases(
1810 const CXXRecordDecl *RD, CharUnits OffsetInLayoutClass,
1811 VisitedVirtualBasesSetTy &VBases) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001812 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1813
1814 // Check if this base has a primary base.
1815 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
1816
1817 // Check if it's virtual.
1818 if (Layout.isPrimaryBaseVirtual()) {
1819 bool IsPrimaryVirtualBase = true;
1820
1821 if (isBuildingConstructorVTable()) {
1822 // Check if the base is actually a primary base in the class we use for
1823 // layout.
1824 const ASTRecordLayout &LayoutClassLayout =
1825 Context.getASTRecordLayout(LayoutClass);
1826
1827 CharUnits PrimaryBaseOffsetInLayoutClass =
1828 LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
1829
1830 // We know that the base is not a primary base in the layout class if
1831 // the base offsets are different.
1832 if (PrimaryBaseOffsetInLayoutClass != OffsetInLayoutClass)
1833 IsPrimaryVirtualBase = false;
1834 }
1835
1836 if (IsPrimaryVirtualBase)
1837 PrimaryVirtualBases.insert(PrimaryBase);
1838 }
1839 }
1840
1841 // Traverse bases, looking for more primary virtual bases.
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00001842 for (const auto &B : RD->bases()) {
1843 const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +00001844
1845 CharUnits BaseOffsetInLayoutClass;
1846
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00001847 if (B.isVirtual()) {
David Blaikie82e95a32014-11-19 07:49:47 +00001848 if (!VBases.insert(BaseDecl).second)
Peter Collingbournecfd23562011-09-26 01:57:12 +00001849 continue;
1850
1851 const ASTRecordLayout &LayoutClassLayout =
1852 Context.getASTRecordLayout(LayoutClass);
1853
1854 BaseOffsetInLayoutClass =
1855 LayoutClassLayout.getVBaseClassOffset(BaseDecl);
1856 } else {
1857 BaseOffsetInLayoutClass =
1858 OffsetInLayoutClass + Layout.getBaseClassOffset(BaseDecl);
1859 }
1860
1861 DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases);
1862 }
1863}
1864
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001865void ItaniumVTableBuilder::LayoutVTablesForVirtualBases(
1866 const CXXRecordDecl *RD, VisitedVirtualBasesSetTy &VBases) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001867 // Itanium C++ ABI 2.5.2:
1868 // Then come the virtual base virtual tables, also in inheritance graph
1869 // order, and again excluding primary bases (which share virtual tables with
1870 // the classes for which they are primary).
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00001871 for (const auto &B : RD->bases()) {
1872 const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +00001873
1874 // Check if this base needs a vtable. (If it's virtual, not a primary base
1875 // of some other class, and we haven't visited it before).
David Blaikie82e95a32014-11-19 07:49:47 +00001876 if (B.isVirtual() && BaseDecl->isDynamicClass() &&
1877 !PrimaryVirtualBases.count(BaseDecl) &&
1878 VBases.insert(BaseDecl).second) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001879 const ASTRecordLayout &MostDerivedClassLayout =
1880 Context.getASTRecordLayout(MostDerivedClass);
1881 CharUnits BaseOffset =
1882 MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
1883
1884 const ASTRecordLayout &LayoutClassLayout =
1885 Context.getASTRecordLayout(LayoutClass);
1886 CharUnits BaseOffsetInLayoutClass =
1887 LayoutClassLayout.getVBaseClassOffset(BaseDecl);
1888
1889 LayoutPrimaryAndSecondaryVTables(
1890 BaseSubobject(BaseDecl, BaseOffset),
1891 /*BaseIsMorallyVirtual=*/true,
1892 /*BaseIsVirtualInLayoutClass=*/true,
1893 BaseOffsetInLayoutClass);
1894 }
1895
1896 // We only need to check the base for virtual base vtables if it actually
1897 // has virtual bases.
1898 if (BaseDecl->getNumVBases())
1899 LayoutVTablesForVirtualBases(BaseDecl, VBases);
1900 }
1901}
1902
1903/// dumpLayout - Dump the vtable layout.
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001904void ItaniumVTableBuilder::dumpLayout(raw_ostream &Out) {
Timur Iskhodzhanov11510312013-06-28 15:42:28 +00001905 // FIXME: write more tests that actually use the dumpLayout output to prevent
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001906 // ItaniumVTableBuilder regressions.
Peter Collingbournecfd23562011-09-26 01:57:12 +00001907
1908 if (isBuildingConstructorVTable()) {
1909 Out << "Construction vtable for ('";
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00001910 MostDerivedClass->printQualifiedName(Out);
1911 Out << "', ";
Peter Collingbournecfd23562011-09-26 01:57:12 +00001912 Out << MostDerivedClassOffset.getQuantity() << ") in '";
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00001913 LayoutClass->printQualifiedName(Out);
Peter Collingbournecfd23562011-09-26 01:57:12 +00001914 } else {
1915 Out << "Vtable for '";
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00001916 MostDerivedClass->printQualifiedName(Out);
Peter Collingbournecfd23562011-09-26 01:57:12 +00001917 }
1918 Out << "' (" << Components.size() << " entries).\n";
1919
1920 // Iterate through the address points and insert them into a new map where
1921 // they are keyed by the index and not the base object.
1922 // Since an address point can be shared by multiple subobjects, we use an
1923 // STL multimap.
1924 std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex;
1925 for (AddressPointsMapTy::const_iterator I = AddressPoints.begin(),
1926 E = AddressPoints.end(); I != E; ++I) {
1927 const BaseSubobject& Base = I->first;
1928 uint64_t Index = I->second;
1929
1930 AddressPointsByIndex.insert(std::make_pair(Index, Base));
1931 }
1932
1933 for (unsigned I = 0, E = Components.size(); I != E; ++I) {
1934 uint64_t Index = I;
1935
1936 Out << llvm::format("%4d | ", I);
1937
1938 const VTableComponent &Component = Components[I];
1939
1940 // Dump the component.
1941 switch (Component.getKind()) {
1942
1943 case VTableComponent::CK_VCallOffset:
1944 Out << "vcall_offset ("
1945 << Component.getVCallOffset().getQuantity()
1946 << ")";
1947 break;
1948
1949 case VTableComponent::CK_VBaseOffset:
1950 Out << "vbase_offset ("
1951 << Component.getVBaseOffset().getQuantity()
1952 << ")";
1953 break;
1954
1955 case VTableComponent::CK_OffsetToTop:
1956 Out << "offset_to_top ("
1957 << Component.getOffsetToTop().getQuantity()
1958 << ")";
1959 break;
1960
1961 case VTableComponent::CK_RTTI:
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00001962 Component.getRTTIDecl()->printQualifiedName(Out);
1963 Out << " RTTI";
Peter Collingbournecfd23562011-09-26 01:57:12 +00001964 break;
1965
1966 case VTableComponent::CK_FunctionPointer: {
1967 const CXXMethodDecl *MD = Component.getFunctionDecl();
1968
1969 std::string Str =
1970 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
1971 MD);
1972 Out << Str;
1973 if (MD->isPure())
1974 Out << " [pure]";
1975
David Blaikie596d2ca2012-10-16 20:25:33 +00001976 if (MD->isDeleted())
1977 Out << " [deleted]";
1978
Peter Collingbournecfd23562011-09-26 01:57:12 +00001979 ThunkInfo Thunk = VTableThunks.lookup(I);
1980 if (!Thunk.isEmpty()) {
1981 // If this function pointer has a return adjustment, dump it.
1982 if (!Thunk.Return.isEmpty()) {
1983 Out << "\n [return adjustment: ";
1984 Out << Thunk.Return.NonVirtual << " non-virtual";
1985
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001986 if (Thunk.Return.Virtual.Itanium.VBaseOffsetOffset) {
1987 Out << ", " << Thunk.Return.Virtual.Itanium.VBaseOffsetOffset;
Peter Collingbournecfd23562011-09-26 01:57:12 +00001988 Out << " vbase offset offset";
1989 }
1990
1991 Out << ']';
1992 }
1993
1994 // If this function pointer has a 'this' pointer adjustment, dump it.
1995 if (!Thunk.This.isEmpty()) {
1996 Out << "\n [this adjustment: ";
1997 Out << Thunk.This.NonVirtual << " non-virtual";
1998
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00001999 if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) {
2000 Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset;
Peter Collingbournecfd23562011-09-26 01:57:12 +00002001 Out << " vcall offset offset";
2002 }
2003
2004 Out << ']';
2005 }
2006 }
2007
2008 break;
2009 }
2010
2011 case VTableComponent::CK_CompleteDtorPointer:
2012 case VTableComponent::CK_DeletingDtorPointer: {
2013 bool IsComplete =
2014 Component.getKind() == VTableComponent::CK_CompleteDtorPointer;
2015
2016 const CXXDestructorDecl *DD = Component.getDestructorDecl();
2017
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00002018 DD->printQualifiedName(Out);
Peter Collingbournecfd23562011-09-26 01:57:12 +00002019 if (IsComplete)
2020 Out << "() [complete]";
2021 else
2022 Out << "() [deleting]";
2023
2024 if (DD->isPure())
2025 Out << " [pure]";
2026
2027 ThunkInfo Thunk = VTableThunks.lookup(I);
2028 if (!Thunk.isEmpty()) {
2029 // If this destructor has a 'this' pointer adjustment, dump it.
2030 if (!Thunk.This.isEmpty()) {
2031 Out << "\n [this adjustment: ";
2032 Out << Thunk.This.NonVirtual << " non-virtual";
2033
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002034 if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) {
2035 Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset;
Peter Collingbournecfd23562011-09-26 01:57:12 +00002036 Out << " vcall offset offset";
2037 }
2038
2039 Out << ']';
2040 }
2041 }
2042
2043 break;
2044 }
2045
2046 case VTableComponent::CK_UnusedFunctionPointer: {
2047 const CXXMethodDecl *MD = Component.getUnusedFunctionDecl();
2048
2049 std::string Str =
2050 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2051 MD);
2052 Out << "[unused] " << Str;
2053 if (MD->isPure())
2054 Out << " [pure]";
2055 }
2056
2057 }
2058
2059 Out << '\n';
2060
2061 // Dump the next address point.
2062 uint64_t NextIndex = Index + 1;
2063 if (AddressPointsByIndex.count(NextIndex)) {
2064 if (AddressPointsByIndex.count(NextIndex) == 1) {
2065 const BaseSubobject &Base =
2066 AddressPointsByIndex.find(NextIndex)->second;
2067
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00002068 Out << " -- (";
2069 Base.getBase()->printQualifiedName(Out);
Peter Collingbournecfd23562011-09-26 01:57:12 +00002070 Out << ", " << Base.getBaseOffset().getQuantity();
2071 Out << ") vtable address --\n";
2072 } else {
2073 CharUnits BaseOffset =
2074 AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset();
2075
2076 // We store the class names in a set to get a stable order.
2077 std::set<std::string> ClassNames;
2078 for (std::multimap<uint64_t, BaseSubobject>::const_iterator I =
2079 AddressPointsByIndex.lower_bound(NextIndex), E =
2080 AddressPointsByIndex.upper_bound(NextIndex); I != E; ++I) {
2081 assert(I->second.getBaseOffset() == BaseOffset &&
2082 "Invalid base offset!");
2083 const CXXRecordDecl *RD = I->second.getBase();
2084 ClassNames.insert(RD->getQualifiedNameAsString());
2085 }
2086
2087 for (std::set<std::string>::const_iterator I = ClassNames.begin(),
2088 E = ClassNames.end(); I != E; ++I) {
2089 Out << " -- (" << *I;
2090 Out << ", " << BaseOffset.getQuantity() << ") vtable address --\n";
2091 }
2092 }
2093 }
2094 }
2095
2096 Out << '\n';
2097
2098 if (isBuildingConstructorVTable())
2099 return;
2100
2101 if (MostDerivedClass->getNumVBases()) {
2102 // We store the virtual base class names and their offsets in a map to get
2103 // a stable order.
2104
2105 std::map<std::string, CharUnits> ClassNamesAndOffsets;
2106 for (VBaseOffsetOffsetsMapTy::const_iterator I = VBaseOffsetOffsets.begin(),
2107 E = VBaseOffsetOffsets.end(); I != E; ++I) {
2108 std::string ClassName = I->first->getQualifiedNameAsString();
2109 CharUnits OffsetOffset = I->second;
2110 ClassNamesAndOffsets.insert(
2111 std::make_pair(ClassName, OffsetOffset));
2112 }
2113
2114 Out << "Virtual base offset offsets for '";
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00002115 MostDerivedClass->printQualifiedName(Out);
2116 Out << "' (";
Peter Collingbournecfd23562011-09-26 01:57:12 +00002117 Out << ClassNamesAndOffsets.size();
2118 Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n";
2119
2120 for (std::map<std::string, CharUnits>::const_iterator I =
2121 ClassNamesAndOffsets.begin(), E = ClassNamesAndOffsets.end();
2122 I != E; ++I)
2123 Out << " " << I->first << " | " << I->second.getQuantity() << '\n';
2124
2125 Out << "\n";
2126 }
2127
2128 if (!Thunks.empty()) {
2129 // We store the method names in a map to get a stable order.
2130 std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls;
2131
2132 for (ThunksMapTy::const_iterator I = Thunks.begin(), E = Thunks.end();
2133 I != E; ++I) {
2134 const CXXMethodDecl *MD = I->first;
2135 std::string MethodName =
2136 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2137 MD);
2138
2139 MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
2140 }
2141
2142 for (std::map<std::string, const CXXMethodDecl *>::const_iterator I =
2143 MethodNamesAndDecls.begin(), E = MethodNamesAndDecls.end();
2144 I != E; ++I) {
2145 const std::string &MethodName = I->first;
2146 const CXXMethodDecl *MD = I->second;
2147
2148 ThunkInfoVectorTy ThunksVector = Thunks[MD];
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002149 std::sort(ThunksVector.begin(), ThunksVector.end(),
Benjamin Kramerbbdd7642014-03-01 14:48:57 +00002150 [](const ThunkInfo &LHS, const ThunkInfo &RHS) {
Craig Topper36250ad2014-05-12 05:36:57 +00002151 assert(LHS.Method == nullptr && RHS.Method == nullptr);
Benjamin Kramera741b8c2014-03-03 20:26:46 +00002152 return std::tie(LHS.This, LHS.Return) < std::tie(RHS.This, RHS.Return);
Benjamin Kramerbbdd7642014-03-01 14:48:57 +00002153 });
Peter Collingbournecfd23562011-09-26 01:57:12 +00002154
2155 Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
2156 Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n";
2157
2158 for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) {
2159 const ThunkInfo &Thunk = ThunksVector[I];
2160
2161 Out << llvm::format("%4d | ", I);
2162
2163 // If this function pointer has a return pointer adjustment, dump it.
2164 if (!Thunk.Return.isEmpty()) {
Timur Iskhodzhanov11510312013-06-28 15:42:28 +00002165 Out << "return adjustment: " << Thunk.Return.NonVirtual;
Peter Collingbournecfd23562011-09-26 01:57:12 +00002166 Out << " non-virtual";
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00002167 if (Thunk.Return.Virtual.Itanium.VBaseOffsetOffset) {
2168 Out << ", " << Thunk.Return.Virtual.Itanium.VBaseOffsetOffset;
Peter Collingbournecfd23562011-09-26 01:57:12 +00002169 Out << " vbase offset offset";
2170 }
2171
2172 if (!Thunk.This.isEmpty())
2173 Out << "\n ";
2174 }
2175
2176 // If this function pointer has a 'this' pointer adjustment, dump it.
2177 if (!Thunk.This.isEmpty()) {
2178 Out << "this adjustment: ";
2179 Out << Thunk.This.NonVirtual << " non-virtual";
2180
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002181 if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) {
2182 Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset;
Peter Collingbournecfd23562011-09-26 01:57:12 +00002183 Out << " vcall offset offset";
2184 }
2185 }
2186
2187 Out << '\n';
2188 }
2189
2190 Out << '\n';
2191 }
2192 }
2193
2194 // Compute the vtable indices for all the member functions.
2195 // Store them in a map keyed by the index so we'll get a sorted table.
2196 std::map<uint64_t, std::string> IndicesMap;
2197
Aaron Ballman2b124d12014-03-13 16:36:16 +00002198 for (const auto *MD : MostDerivedClass->methods()) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00002199 // We only want virtual member functions.
2200 if (!MD->isVirtual())
2201 continue;
Reid Kleckner1cbd9aa2015-02-25 19:17:48 +00002202 MD = MD->getCanonicalDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +00002203
2204 std::string MethodName =
2205 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2206 MD);
2207
2208 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00002209 GlobalDecl GD(DD, Dtor_Complete);
2210 assert(MethodVTableIndices.count(GD));
2211 uint64_t VTableIndex = MethodVTableIndices[GD];
2212 IndicesMap[VTableIndex] = MethodName + " [complete]";
2213 IndicesMap[VTableIndex + 1] = MethodName + " [deleting]";
Peter Collingbournecfd23562011-09-26 01:57:12 +00002214 } else {
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00002215 assert(MethodVTableIndices.count(MD));
2216 IndicesMap[MethodVTableIndices[MD]] = MethodName;
Peter Collingbournecfd23562011-09-26 01:57:12 +00002217 }
2218 }
2219
2220 // Print the vtable indices for all the member functions.
2221 if (!IndicesMap.empty()) {
2222 Out << "VTable indices for '";
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00002223 MostDerivedClass->printQualifiedName(Out);
Peter Collingbournecfd23562011-09-26 01:57:12 +00002224 Out << "' (" << IndicesMap.size() << " entries).\n";
2225
2226 for (std::map<uint64_t, std::string>::const_iterator I = IndicesMap.begin(),
2227 E = IndicesMap.end(); I != E; ++I) {
2228 uint64_t VTableIndex = I->first;
2229 const std::string &MethodName = I->second;
2230
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00002231 Out << llvm::format("%4" PRIu64 " | ", VTableIndex) << MethodName
Benjamin Kramer5291e682012-03-10 02:06:27 +00002232 << '\n';
Peter Collingbournecfd23562011-09-26 01:57:12 +00002233 }
2234 }
2235
2236 Out << '\n';
2237}
Peter Collingbournecfd23562011-09-26 01:57:12 +00002238}
2239
2240VTableLayout::VTableLayout(uint64_t NumVTableComponents,
2241 const VTableComponent *VTableComponents,
2242 uint64_t NumVTableThunks,
2243 const VTableThunkTy *VTableThunks,
Timur Iskhodzhanov52b8a052013-01-21 13:02:41 +00002244 const AddressPointsMapTy &AddressPoints,
2245 bool IsMicrosoftABI)
Peter Collingbournecfd23562011-09-26 01:57:12 +00002246 : NumVTableComponents(NumVTableComponents),
2247 VTableComponents(new VTableComponent[NumVTableComponents]),
2248 NumVTableThunks(NumVTableThunks),
2249 VTableThunks(new VTableThunkTy[NumVTableThunks]),
Timur Iskhodzhanov52b8a052013-01-21 13:02:41 +00002250 AddressPoints(AddressPoints),
2251 IsMicrosoftABI(IsMicrosoftABI) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00002252 std::copy(VTableComponents, VTableComponents+NumVTableComponents,
Benjamin Kramere2980632012-04-14 14:13:43 +00002253 this->VTableComponents.get());
2254 std::copy(VTableThunks, VTableThunks+NumVTableThunks,
2255 this->VTableThunks.get());
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002256 std::sort(this->VTableThunks.get(),
2257 this->VTableThunks.get() + NumVTableThunks,
Benjamin Kramerbbdd7642014-03-01 14:48:57 +00002258 [](const VTableLayout::VTableThunkTy &LHS,
2259 const VTableLayout::VTableThunkTy &RHS) {
2260 assert((LHS.first != RHS.first || LHS.second == RHS.second) &&
2261 "Different thunks should have unique indices!");
2262 return LHS.first < RHS.first;
2263 });
Peter Collingbournecfd23562011-09-26 01:57:12 +00002264}
2265
Benjamin Kramere2980632012-04-14 14:13:43 +00002266VTableLayout::~VTableLayout() { }
Peter Collingbournecfd23562011-09-26 01:57:12 +00002267
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002268ItaniumVTableContext::ItaniumVTableContext(ASTContext &Context)
Reid Klecknerb60a3d52013-12-20 23:58:52 +00002269 : VTableContextBase(/*MS=*/false) {}
Timur Iskhodzhanov52b8a052013-01-21 13:02:41 +00002270
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002271ItaniumVTableContext::~ItaniumVTableContext() {
Peter Collingbournecfd23562011-09-26 01:57:12 +00002272 llvm::DeleteContainerSeconds(VTableLayouts);
2273}
2274
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002275uint64_t ItaniumVTableContext::getMethodVTableIndex(GlobalDecl GD) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00002276 MethodVTableIndicesTy::iterator I = MethodVTableIndices.find(GD);
2277 if (I != MethodVTableIndices.end())
2278 return I->second;
2279
2280 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
2281
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002282 computeVTableRelatedInformation(RD);
Peter Collingbournecfd23562011-09-26 01:57:12 +00002283
2284 I = MethodVTableIndices.find(GD);
2285 assert(I != MethodVTableIndices.end() && "Did not find index!");
2286 return I->second;
2287}
2288
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002289CharUnits
2290ItaniumVTableContext::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD,
2291 const CXXRecordDecl *VBase) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00002292 ClassPairTy ClassPair(RD, VBase);
2293
2294 VirtualBaseClassOffsetOffsetsMapTy::iterator I =
2295 VirtualBaseClassOffsetOffsets.find(ClassPair);
2296 if (I != VirtualBaseClassOffsetOffsets.end())
2297 return I->second;
Craig Topper36250ad2014-05-12 05:36:57 +00002298
2299 VCallAndVBaseOffsetBuilder Builder(RD, RD, /*FinalOverriders=*/nullptr,
Peter Collingbournecfd23562011-09-26 01:57:12 +00002300 BaseSubobject(RD, CharUnits::Zero()),
2301 /*BaseIsVirtual=*/false,
2302 /*OffsetInLayoutClass=*/CharUnits::Zero());
2303
2304 for (VCallAndVBaseOffsetBuilder::VBaseOffsetOffsetsMapTy::const_iterator I =
2305 Builder.getVBaseOffsetOffsets().begin(),
2306 E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) {
2307 // Insert all types.
2308 ClassPairTy ClassPair(RD, I->first);
2309
2310 VirtualBaseClassOffsetOffsets.insert(
2311 std::make_pair(ClassPair, I->second));
2312 }
2313
2314 I = VirtualBaseClassOffsetOffsets.find(ClassPair);
2315 assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!");
2316
2317 return I->second;
2318}
2319
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002320static VTableLayout *CreateVTableLayout(const ItaniumVTableBuilder &Builder) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00002321 SmallVector<VTableLayout::VTableThunkTy, 1>
2322 VTableThunks(Builder.vtable_thunks_begin(), Builder.vtable_thunks_end());
Peter Collingbournecfd23562011-09-26 01:57:12 +00002323
2324 return new VTableLayout(Builder.getNumVTableComponents(),
2325 Builder.vtable_component_begin(),
2326 VTableThunks.size(),
2327 VTableThunks.data(),
Timur Iskhodzhanov52b8a052013-01-21 13:02:41 +00002328 Builder.getAddressPoints(),
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00002329 /*IsMicrosoftABI=*/false);
Peter Collingbournecfd23562011-09-26 01:57:12 +00002330}
2331
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002332void
2333ItaniumVTableContext::computeVTableRelatedInformation(const CXXRecordDecl *RD) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00002334 const VTableLayout *&Entry = VTableLayouts[RD];
2335
2336 // Check if we've computed this information before.
2337 if (Entry)
2338 return;
2339
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002340 ItaniumVTableBuilder Builder(*this, RD, CharUnits::Zero(),
2341 /*MostDerivedClassIsVirtual=*/0, RD);
Peter Collingbournecfd23562011-09-26 01:57:12 +00002342 Entry = CreateVTableLayout(Builder);
2343
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00002344 MethodVTableIndices.insert(Builder.vtable_indices_begin(),
2345 Builder.vtable_indices_end());
2346
Peter Collingbournecfd23562011-09-26 01:57:12 +00002347 // Add the known thunks.
2348 Thunks.insert(Builder.thunks_begin(), Builder.thunks_end());
2349
2350 // If we don't have the vbase information for this class, insert it.
2351 // getVirtualBaseOffsetOffset will compute it separately without computing
2352 // the rest of the vtable related information.
2353 if (!RD->getNumVBases())
2354 return;
2355
Timur Iskhodzhanov7f55a452013-07-02 16:00:40 +00002356 const CXXRecordDecl *VBase =
2357 RD->vbases_begin()->getType()->getAsCXXRecordDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +00002358
2359 if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase)))
2360 return;
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002361
2362 for (ItaniumVTableBuilder::VBaseOffsetOffsetsMapTy::const_iterator
2363 I = Builder.getVBaseOffsetOffsets().begin(),
2364 E = Builder.getVBaseOffsetOffsets().end();
2365 I != E; ++I) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00002366 // Insert all types.
2367 ClassPairTy ClassPair(RD, I->first);
2368
2369 VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second));
2370 }
2371}
2372
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002373VTableLayout *ItaniumVTableContext::createConstructionVTableLayout(
2374 const CXXRecordDecl *MostDerivedClass, CharUnits MostDerivedClassOffset,
2375 bool MostDerivedClassIsVirtual, const CXXRecordDecl *LayoutClass) {
2376 ItaniumVTableBuilder Builder(*this, MostDerivedClass, MostDerivedClassOffset,
2377 MostDerivedClassIsVirtual, LayoutClass);
Peter Collingbournecfd23562011-09-26 01:57:12 +00002378 return CreateVTableLayout(Builder);
2379}
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002380
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002381namespace {
2382
2383// Vtables in the Microsoft ABI are different from the Itanium ABI.
2384//
2385// The main differences are:
2386// 1. Separate vftable and vbtable.
2387//
2388// 2. Each subobject with a vfptr gets its own vftable rather than an address
2389// point in a single vtable shared between all the subobjects.
2390// Each vftable is represented by a separate section and virtual calls
2391// must be done using the vftable which has a slot for the function to be
2392// called.
2393//
2394// 3. Virtual method definitions expect their 'this' parameter to point to the
2395// first vfptr whose table provides a compatible overridden method. In many
2396// cases, this permits the original vf-table entry to directly call
2397// the method instead of passing through a thunk.
Timur Iskhodzhanov46a18ef2014-11-14 14:10:15 +00002398// See example before VFTableBuilder::ComputeThisOffset below.
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002399//
2400// A compatible overridden method is one which does not have a non-trivial
2401// covariant-return adjustment.
2402//
2403// The first vfptr is the one with the lowest offset in the complete-object
2404// layout of the defining class, and the method definition will subtract
2405// that constant offset from the parameter value to get the real 'this'
2406// value. Therefore, if the offset isn't really constant (e.g. if a virtual
2407// function defined in a virtual base is overridden in a more derived
2408// virtual base and these bases have a reverse order in the complete
2409// object), the vf-table may require a this-adjustment thunk.
2410//
2411// 4. vftables do not contain new entries for overrides that merely require
2412// this-adjustment. Together with #3, this keeps vf-tables smaller and
2413// eliminates the need for this-adjustment thunks in many cases, at the cost
2414// of often requiring redundant work to adjust the "this" pointer.
2415//
2416// 5. Instead of VTT and constructor vtables, vbtables and vtordisps are used.
2417// Vtordisps are emitted into the class layout if a class has
2418// a) a user-defined ctor/dtor
2419// and
2420// b) a method overriding a method in a virtual base.
Timur Iskhodzhanov46a18ef2014-11-14 14:10:15 +00002421//
2422// To get a better understanding of this code,
2423// you might want to see examples in test/CodeGenCXX/microsoft-abi-vtables-*.cpp
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002424
2425class VFTableBuilder {
2426public:
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00002427 typedef MicrosoftVTableContext::MethodVFTableLocation MethodVFTableLocation;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002428
2429 typedef llvm::DenseMap<GlobalDecl, MethodVFTableLocation>
2430 MethodVFTableLocationsTy;
2431
Timur Iskhodzhanovba557022014-03-20 20:38:34 +00002432 typedef llvm::iterator_range<MethodVFTableLocationsTy::const_iterator>
2433 method_locations_range;
2434
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002435private:
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00002436 /// VTables - Global vtable information.
2437 MicrosoftVTableContext &VTables;
2438
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002439 /// Context - The ASTContext which we will use for layout information.
2440 ASTContext &Context;
2441
2442 /// MostDerivedClass - The most derived class for which we're building this
2443 /// vtable.
2444 const CXXRecordDecl *MostDerivedClass;
2445
2446 const ASTRecordLayout &MostDerivedClassLayout;
2447
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00002448 const VPtrInfo &WhichVFPtr;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002449
2450 /// FinalOverriders - The final overriders of the most derived class.
2451 const FinalOverriders Overriders;
2452
2453 /// Components - The components of the vftable being built.
2454 SmallVector<VTableComponent, 64> Components;
2455
2456 MethodVFTableLocationsTy MethodVFTableLocations;
2457
David Majnemer3c7228e2014-07-01 21:10:07 +00002458 /// \brief Does this class have an RTTI component?
2459 bool HasRTTIComponent;
2460
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002461 /// MethodInfo - Contains information about a method in a vtable.
2462 /// (Used for computing 'this' pointer adjustment thunks.
2463 struct MethodInfo {
2464 /// VBTableIndex - The nonzero index in the vbtable that
2465 /// this method's base has, or zero.
2466 const uint64_t VBTableIndex;
2467
2468 /// VFTableIndex - The index in the vftable that this method has.
2469 const uint64_t VFTableIndex;
2470
2471 /// Shadowed - Indicates if this vftable slot is shadowed by
2472 /// a slot for a covariant-return override. If so, it shouldn't be printed
2473 /// or used for vcalls in the most derived class.
2474 bool Shadowed;
2475
Timur Iskhodzhanov16055e72014-08-10 11:40:51 +00002476 /// UsesExtraSlot - Indicates if this vftable slot was created because
2477 /// any of the overridden slots required a return adjusting thunk.
2478 bool UsesExtraSlot;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002479
Timur Iskhodzhanov16055e72014-08-10 11:40:51 +00002480 MethodInfo(uint64_t VBTableIndex, uint64_t VFTableIndex,
2481 bool UsesExtraSlot = false)
2482 : VBTableIndex(VBTableIndex), VFTableIndex(VFTableIndex),
2483 Shadowed(false), UsesExtraSlot(UsesExtraSlot) {}
2484
2485 MethodInfo()
2486 : VBTableIndex(0), VFTableIndex(0), Shadowed(false),
2487 UsesExtraSlot(false) {}
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002488 };
2489
2490 typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy;
2491
2492 /// MethodInfoMap - The information for all methods in the vftable we're
2493 /// currently building.
2494 MethodInfoMapTy MethodInfoMap;
2495
2496 typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy;
2497
2498 /// VTableThunks - The thunks by vftable index in the vftable currently being
2499 /// built.
2500 VTableThunksMapTy VTableThunks;
2501
2502 typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
2503 typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
2504
2505 /// Thunks - A map that contains all the thunks needed for all methods in the
2506 /// most derived class for which the vftable is currently being built.
2507 ThunksMapTy Thunks;
2508
2509 /// AddThunk - Add a thunk for the given method.
2510 void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk) {
2511 SmallVector<ThunkInfo, 1> &ThunksVector = Thunks[MD];
2512
2513 // Check if we have this thunk already.
2514 if (std::find(ThunksVector.begin(), ThunksVector.end(), Thunk) !=
2515 ThunksVector.end())
2516 return;
2517
2518 ThunksVector.push_back(Thunk);
2519 }
2520
2521 /// ComputeThisOffset - Returns the 'this' argument offset for the given
Timur Iskhodzhanov3a9ac932014-03-04 18:17:38 +00002522 /// method, relative to the beginning of the MostDerivedClass.
2523 CharUnits ComputeThisOffset(FinalOverriders::OverriderInfo Overrider);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002524
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002525 void CalculateVtordispAdjustment(FinalOverriders::OverriderInfo Overrider,
2526 CharUnits ThisOffset, ThisAdjustment &TA);
2527
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002528 /// AddMethod - Add a single virtual member function to the vftable
2529 /// components vector.
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002530 void AddMethod(const CXXMethodDecl *MD, ThunkInfo TI) {
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00002531 if (!TI.isEmpty()) {
2532 VTableThunks[Components.size()] = TI;
2533 AddThunk(MD, TI);
2534 }
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002535 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002536 assert(TI.Return.isEmpty() &&
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002537 "Destructor can't have return adjustment!");
2538 Components.push_back(VTableComponent::MakeDeletingDtor(DD));
2539 } else {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002540 Components.push_back(VTableComponent::MakeFunction(MD));
2541 }
2542 }
2543
2544 /// AddMethods - Add the methods of this base subobject and the relevant
2545 /// subbases to the vftable we're currently laying out.
2546 void AddMethods(BaseSubobject Base, unsigned BaseDepth,
2547 const CXXRecordDecl *LastVBase,
2548 BasesSetVectorTy &VisitedBases);
2549
2550 void LayoutVFTable() {
David Majnemer6e9ae782014-09-22 20:39:37 +00002551 // RTTI data goes before all other entries.
2552 if (HasRTTIComponent)
2553 Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass));
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002554
2555 BasesSetVectorTy VisitedBases;
Craig Topper36250ad2014-05-12 05:36:57 +00002556 AddMethods(BaseSubobject(MostDerivedClass, CharUnits::Zero()), 0, nullptr,
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002557 VisitedBases);
David Majnemera9b7e662014-09-26 08:07:55 +00002558 assert((HasRTTIComponent ? Components.size() - 1 : Components.size()) &&
2559 "vftable can't be empty");
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002560
2561 assert(MethodVFTableLocations.empty());
2562 for (MethodInfoMapTy::const_iterator I = MethodInfoMap.begin(),
2563 E = MethodInfoMap.end(); I != E; ++I) {
2564 const CXXMethodDecl *MD = I->first;
2565 const MethodInfo &MI = I->second;
2566 // Skip the methods that the MostDerivedClass didn't override
2567 // and the entries shadowed by return adjusting thunks.
2568 if (MD->getParent() != MostDerivedClass || MI.Shadowed)
2569 continue;
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00002570 MethodVFTableLocation Loc(MI.VBTableIndex, WhichVFPtr.getVBaseWithVPtr(),
2571 WhichVFPtr.NonVirtualOffset, MI.VFTableIndex);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002572 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2573 MethodVFTableLocations[GlobalDecl(DD, Dtor_Deleting)] = Loc;
2574 } else {
2575 MethodVFTableLocations[MD] = Loc;
2576 }
2577 }
2578 }
2579
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002580public:
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00002581 VFTableBuilder(MicrosoftVTableContext &VTables,
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00002582 const CXXRecordDecl *MostDerivedClass, const VPtrInfo *Which)
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00002583 : VTables(VTables),
2584 Context(MostDerivedClass->getASTContext()),
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002585 MostDerivedClass(MostDerivedClass),
2586 MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)),
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00002587 WhichVFPtr(*Which),
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002588 Overriders(MostDerivedClass, CharUnits(), MostDerivedClass) {
David Majnemerd905da42014-07-01 20:30:31 +00002589 // Only include the RTTI component if we know that we will provide a
2590 // definition of the vftable.
David Majnemerf6072342014-07-01 22:24:56 +00002591 HasRTTIComponent = Context.getLangOpts().RTTIData &&
David Majnemera03849b2015-03-18 22:04:43 +00002592 !MostDerivedClass->hasAttr<DLLImportAttr>() &&
2593 MostDerivedClass->getTemplateSpecializationKind() !=
2594 TSK_ExplicitInstantiationDeclaration;
David Majnemerd905da42014-07-01 20:30:31 +00002595
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002596 LayoutVFTable();
2597
2598 if (Context.getLangOpts().DumpVTableLayouts)
Reid Kleckner5bc6d0f2013-11-08 21:28:00 +00002599 dumpLayout(llvm::outs());
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002600 }
2601
2602 uint64_t getNumThunks() const { return Thunks.size(); }
2603
2604 ThunksMapTy::const_iterator thunks_begin() const { return Thunks.begin(); }
2605
2606 ThunksMapTy::const_iterator thunks_end() const { return Thunks.end(); }
2607
Timur Iskhodzhanovba557022014-03-20 20:38:34 +00002608 method_locations_range vtable_locations() const {
2609 return method_locations_range(MethodVFTableLocations.begin(),
2610 MethodVFTableLocations.end());
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002611 }
2612
2613 uint64_t getNumVTableComponents() const { return Components.size(); }
2614
2615 const VTableComponent *vtable_component_begin() const {
2616 return Components.begin();
2617 }
2618
2619 const VTableComponent *vtable_component_end() const {
2620 return Components.end();
2621 }
2622
2623 VTableThunksMapTy::const_iterator vtable_thunks_begin() const {
2624 return VTableThunks.begin();
2625 }
2626
2627 VTableThunksMapTy::const_iterator vtable_thunks_end() const {
2628 return VTableThunks.end();
2629 }
2630
2631 void dumpLayout(raw_ostream &);
2632};
2633
Reid Klecknerb40a27d2014-01-03 00:14:35 +00002634} // end namespace
2635
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002636/// InitialOverriddenDefinitionCollector - Finds the set of least derived bases
2637/// that define the given method.
2638struct InitialOverriddenDefinitionCollector {
2639 BasesSetVectorTy Bases;
2640 OverriddenMethodsSetTy VisitedOverriddenMethods;
2641
2642 bool visit(const CXXMethodDecl *OverriddenMD) {
2643 if (OverriddenMD->size_overridden_methods() == 0)
2644 Bases.insert(OverriddenMD->getParent());
2645 // Don't recurse on this method if we've already collected it.
David Blaikie82e95a32014-11-19 07:49:47 +00002646 return VisitedOverriddenMethods.insert(OverriddenMD).second;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002647 }
2648};
2649
2650static bool BaseInSet(const CXXBaseSpecifier *Specifier,
2651 CXXBasePath &Path, void *BasesSet) {
2652 BasesSetVectorTy *Bases = (BasesSetVectorTy *)BasesSet;
2653 return Bases->count(Specifier->getType()->getAsCXXRecordDecl());
2654}
2655
Timur Iskhodzhanov46a18ef2014-11-14 14:10:15 +00002656// Let's study one class hierarchy as an example:
2657// struct A {
2658// virtual void f();
2659// int x;
2660// };
2661//
2662// struct B : virtual A {
2663// virtual void f();
2664// };
2665//
2666// Record layouts:
2667// struct A:
Timur Iskhodzhanov4d280022014-11-14 14:16:34 +00002668// 0 | (A vftable pointer)
Timur Iskhodzhanov46a18ef2014-11-14 14:10:15 +00002669// 4 | int x
2670//
2671// struct B:
Timur Iskhodzhanov4d280022014-11-14 14:16:34 +00002672// 0 | (B vbtable pointer)
2673// 4 | struct A (virtual base)
2674// 4 | (A vftable pointer)
Timur Iskhodzhanov46a18ef2014-11-14 14:10:15 +00002675// 8 | int x
2676//
2677// Let's assume we have a pointer to the A part of an object of dynamic type B:
2678// B b;
2679// A *a = (A*)&b;
2680// a->f();
2681//
2682// In this hierarchy, f() belongs to the vftable of A, so B::f() expects
2683// "this" parameter to point at the A subobject, which is B+4.
2684// In the B::f() prologue, it adjusts "this" back to B by subtracting 4,
Nico Webercda5c7c2014-11-29 23:57:35 +00002685// performed as a *static* adjustment.
Timur Iskhodzhanov46a18ef2014-11-14 14:10:15 +00002686//
2687// Interesting thing happens when we alter the relative placement of A and B
2688// subobjects in a class:
2689// struct C : virtual B { };
2690//
2691// C c;
2692// A *a = (A*)&c;
2693// a->f();
2694//
2695// Respective record layout is:
2696// 0 | (C vbtable pointer)
2697// 4 | struct A (virtual base)
2698// 4 | (A vftable pointer)
2699// 8 | int x
2700// 12 | struct B (virtual base)
2701// 12 | (B vbtable pointer)
2702//
2703// The final overrider of f() in class C is still B::f(), so B+4 should be
2704// passed as "this" to that code. However, "a" points at B-8, so the respective
2705// vftable entry should hold a thunk that adds 12 to the "this" argument before
2706// performing a tail call to B::f().
2707//
2708// With this example in mind, we can now calculate the 'this' argument offset
2709// for the given method, relative to the beginning of the MostDerivedClass.
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002710CharUnits
Timur Iskhodzhanov3a9ac932014-03-04 18:17:38 +00002711VFTableBuilder::ComputeThisOffset(FinalOverriders::OverriderInfo Overrider) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002712 InitialOverriddenDefinitionCollector Collector;
Timur Iskhodzhanov3a9ac932014-03-04 18:17:38 +00002713 visitAllOverriddenMethods(Overrider.Method, Collector);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002714
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00002715 // If there are no overrides then 'this' is located
2716 // in the base that defines the method.
2717 if (Collector.Bases.size() == 0)
2718 return Overrider.Offset;
2719
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002720 CXXBasePaths Paths;
Timur Iskhodzhanov3a9ac932014-03-04 18:17:38 +00002721 Overrider.Method->getParent()->lookupInBases(BaseInSet, &Collector.Bases,
2722 Paths);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002723
2724 // This will hold the smallest this offset among overridees of MD.
2725 // This implies that an offset of a non-virtual base will dominate an offset
2726 // of a virtual base to potentially reduce the number of thunks required
2727 // in the derived classes that inherit this method.
2728 CharUnits Ret;
2729 bool First = true;
2730
Timur Iskhodzhanoved11ae32014-04-17 22:01:48 +00002731 const ASTRecordLayout &OverriderRDLayout =
2732 Context.getASTRecordLayout(Overrider.Method->getParent());
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002733 for (CXXBasePaths::paths_iterator I = Paths.begin(), E = Paths.end();
2734 I != E; ++I) {
2735 const CXXBasePath &Path = (*I);
Timur Iskhodzhanov3a9ac932014-03-04 18:17:38 +00002736 CharUnits ThisOffset = Overrider.Offset;
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002737 CharUnits LastVBaseOffset;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002738
2739 // For each path from the overrider to the parents of the overridden methods,
2740 // traverse the path, calculating the this offset in the most derived class.
2741 for (int J = 0, F = Path.size(); J != F; ++J) {
2742 const CXXBasePathElement &Element = Path[J];
2743 QualType CurTy = Element.Base->getType();
2744 const CXXRecordDecl *PrevRD = Element.Class,
2745 *CurRD = CurTy->getAsCXXRecordDecl();
2746 const ASTRecordLayout &Layout = Context.getASTRecordLayout(PrevRD);
2747
2748 if (Element.Base->isVirtual()) {
Timur Iskhodzhanoved11ae32014-04-17 22:01:48 +00002749 // The interesting things begin when you have virtual inheritance.
2750 // The final overrider will use a static adjustment equal to the offset
2751 // of the vbase in the final overrider class.
2752 // For example, if the final overrider is in a vbase B of the most
2753 // derived class and it overrides a method of the B's own vbase A,
2754 // it uses A* as "this". In its prologue, it can cast A* to B* with
2755 // a static offset. This offset is used regardless of the actual
2756 // offset of A from B in the most derived class, requiring an
2757 // this-adjusting thunk in the vftable if A and B are laid out
2758 // differently in the most derived class.
2759 LastVBaseOffset = ThisOffset =
2760 Overrider.Offset + OverriderRDLayout.getVBaseClassOffset(CurRD);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002761 } else {
2762 ThisOffset += Layout.getBaseClassOffset(CurRD);
2763 }
2764 }
2765
Timur Iskhodzhanov3a9ac932014-03-04 18:17:38 +00002766 if (isa<CXXDestructorDecl>(Overrider.Method)) {
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002767 if (LastVBaseOffset.isZero()) {
2768 // If a "Base" class has at least one non-virtual base with a virtual
2769 // destructor, the "Base" virtual destructor will take the address
2770 // of the "Base" subobject as the "this" argument.
Timur Iskhodzhanov3a9ac932014-03-04 18:17:38 +00002771 ThisOffset = Overrider.Offset;
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002772 } else {
2773 // A virtual destructor of a virtual base takes the address of the
2774 // virtual base subobject as the "this" argument.
Timur Iskhodzhanov3a9ac932014-03-04 18:17:38 +00002775 ThisOffset = LastVBaseOffset;
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002776 }
2777 }
Timur Iskhodzhanov62082b72013-10-16 18:24:06 +00002778
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002779 if (Ret > ThisOffset || First) {
2780 First = false;
2781 Ret = ThisOffset;
2782 }
2783 }
2784
2785 assert(!First && "Method not found in the given subobject?");
2786 return Ret;
2787}
2788
Timur Iskhodzhanov7d19fc12014-11-17 15:11:05 +00002789// Things are getting even more complex when the "this" adjustment has to
2790// use a dynamic offset instead of a static one, or even two dynamic offsets.
2791// This is sometimes required when a virtual call happens in the middle of
2792// a non-most-derived class construction or destruction.
2793//
2794// Let's take a look at the following example:
2795// struct A {
2796// virtual void f();
2797// };
2798//
2799// void foo(A *a) { a->f(); } // Knows nothing about siblings of A.
2800//
2801// struct B : virtual A {
2802// virtual void f();
2803// B() {
2804// foo(this);
2805// }
2806// };
2807//
2808// struct C : virtual B {
2809// virtual void f();
2810// };
2811//
2812// Record layouts for these classes are:
2813// struct A
2814// 0 | (A vftable pointer)
2815//
2816// struct B
2817// 0 | (B vbtable pointer)
2818// 4 | (vtordisp for vbase A)
2819// 8 | struct A (virtual base)
2820// 8 | (A vftable pointer)
2821//
2822// struct C
2823// 0 | (C vbtable pointer)
2824// 4 | (vtordisp for vbase A)
2825// 8 | struct A (virtual base) // A precedes B!
2826// 8 | (A vftable pointer)
2827// 12 | struct B (virtual base)
2828// 12 | (B vbtable pointer)
2829//
2830// When one creates an object of type C, the C constructor:
2831// - initializes all the vbptrs, then
2832// - calls the A subobject constructor
2833// (initializes A's vfptr with an address of A vftable), then
2834// - calls the B subobject constructor
2835// (initializes A's vfptr with an address of B vftable and vtordisp for A),
2836// that in turn calls foo(), then
2837// - initializes A's vfptr with an address of C vftable and zeroes out the
2838// vtordisp
2839// FIXME: if a structor knows it belongs to MDC, why doesn't it use a vftable
2840// without vtordisp thunks?
Timur Iskhodzhanov70814602014-11-17 15:53:50 +00002841// FIXME: how are vtordisp handled in the presence of nooverride/final?
Timur Iskhodzhanov7d19fc12014-11-17 15:11:05 +00002842//
2843// When foo() is called, an object with a layout of class C has a vftable
2844// referencing B::f() that assumes a B layout, so the "this" adjustments are
2845// incorrect, unless an extra adjustment is done. This adjustment is called
2846// "vtordisp adjustment". Vtordisp basically holds the difference between the
2847// actual location of a vbase in the layout class and the location assumed by
2848// the vftable of the class being constructed/destructed. Vtordisp is only
2849// needed if "this" escapes a
2850// structor (or we can't prove otherwise).
2851// [i.e. vtordisp is a dynamic adjustment for a static adjustment, which is an
2852// estimation of a dynamic adjustment]
2853//
2854// foo() gets a pointer to the A vbase and doesn't know anything about B or C,
2855// so it just passes that pointer as "this" in a virtual call.
2856// If there was no vtordisp, that would just dispatch to B::f().
2857// However, B::f() assumes B+8 is passed as "this",
2858// yet the pointer foo() passes along is B-4 (i.e. C+8).
2859// An extra adjustment is needed, so we emit a thunk into the B vftable.
2860// This vtordisp thunk subtracts the value of vtordisp
2861// from the "this" argument (-12) before making a tailcall to B::f().
2862//
2863// Let's consider an even more complex example:
2864// struct D : virtual B, virtual C {
2865// D() {
2866// foo(this);
2867// }
2868// };
2869//
2870// struct D
2871// 0 | (D vbtable pointer)
2872// 4 | (vtordisp for vbase A)
2873// 8 | struct A (virtual base) // A precedes both B and C!
2874// 8 | (A vftable pointer)
2875// 12 | struct B (virtual base) // B precedes C!
2876// 12 | (B vbtable pointer)
2877// 16 | struct C (virtual base)
2878// 16 | (C vbtable pointer)
2879//
2880// When D::D() calls foo(), we find ourselves in a thunk that should tailcall
2881// to C::f(), which assumes C+8 as its "this" parameter. This time, foo()
2882// passes along A, which is C-8. The A vtordisp holds
2883// "D.vbptr[index_of_A] - offset_of_A_in_D"
2884// and we statically know offset_of_A_in_D, so can get a pointer to D.
2885// When we know it, we can make an extra vbtable lookup to locate the C vbase
2886// and one extra static adjustment to calculate the expected value of C+8.
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002887void VFTableBuilder::CalculateVtordispAdjustment(
2888 FinalOverriders::OverriderInfo Overrider, CharUnits ThisOffset,
2889 ThisAdjustment &TA) {
2890 const ASTRecordLayout::VBaseOffsetsMapTy &VBaseMap =
2891 MostDerivedClassLayout.getVBaseOffsetsMap();
2892 const ASTRecordLayout::VBaseOffsetsMapTy::const_iterator &VBaseMapEntry =
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00002893 VBaseMap.find(WhichVFPtr.getVBaseWithVPtr());
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002894 assert(VBaseMapEntry != VBaseMap.end());
2895
Timur Iskhodzhanov6b128502014-04-22 17:32:02 +00002896 // If there's no vtordisp or the final overrider is defined in the same vbase
2897 // as the initial declaration, we don't need any vtordisp adjustment.
2898 if (!VBaseMapEntry->second.hasVtorDisp() ||
2899 Overrider.VirtualBase == WhichVFPtr.getVBaseWithVPtr())
Timur Iskhodzhanov057fa3a2014-04-17 11:01:41 +00002900 return;
2901
2902 // OK, now we know we need to use a vtordisp thunk.
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002903 // The implicit vtordisp field is located right before the vbase.
Timur Iskhodzhanov70814602014-11-17 15:53:50 +00002904 CharUnits OffsetOfVBaseWithVFPtr = VBaseMapEntry->second.VBaseOffset;
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002905 TA.Virtual.Microsoft.VtordispOffset =
Timur Iskhodzhanov70814602014-11-17 15:53:50 +00002906 (OffsetOfVBaseWithVFPtr - WhichVFPtr.FullOffsetInMDC).getQuantity() - 4;
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002907
Timur Iskhodzhanov057fa3a2014-04-17 11:01:41 +00002908 // A simple vtordisp thunk will suffice if the final overrider is defined
2909 // in either the most derived class or its non-virtual base.
Timur Iskhodzhanov6b128502014-04-22 17:32:02 +00002910 if (Overrider.Method->getParent() == MostDerivedClass ||
2911 !Overrider.VirtualBase)
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002912 return;
2913
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002914 // Otherwise, we need to do use the dynamic offset of the final overrider
2915 // in order to get "this" adjustment right.
2916 TA.Virtual.Microsoft.VBPtrOffset =
Timur Iskhodzhanov70814602014-11-17 15:53:50 +00002917 (OffsetOfVBaseWithVFPtr + WhichVFPtr.NonVirtualOffset -
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002918 MostDerivedClassLayout.getVBPtrOffset()).getQuantity();
2919 TA.Virtual.Microsoft.VBOffsetOffset =
2920 Context.getTypeSizeInChars(Context.IntTy).getQuantity() *
Timur Iskhodzhanov6b128502014-04-22 17:32:02 +00002921 VTables.getVBTableIndex(MostDerivedClass, Overrider.VirtualBase);
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002922
2923 TA.NonVirtual = (ThisOffset - Overrider.Offset).getQuantity();
2924}
2925
Timur Iskhodzhanov20df98c2013-10-06 15:31:37 +00002926static void GroupNewVirtualOverloads(
2927 const CXXRecordDecl *RD,
2928 SmallVector<const CXXMethodDecl *, 10> &VirtualMethods) {
2929 // Put the virtual methods into VirtualMethods in the proper order:
2930 // 1) Group overloads by declaration name. New groups are added to the
2931 // vftable in the order of their first declarations in this class
Reid Kleckner6701de22014-02-19 22:06:10 +00002932 // (including overrides and non-virtual methods).
Timur Iskhodzhanov20df98c2013-10-06 15:31:37 +00002933 // 2) In each group, new overloads appear in the reverse order of declaration.
2934 typedef SmallVector<const CXXMethodDecl *, 1> MethodGroup;
2935 SmallVector<MethodGroup, 10> Groups;
2936 typedef llvm::DenseMap<DeclarationName, unsigned> VisitedGroupIndicesTy;
2937 VisitedGroupIndicesTy VisitedGroupIndices;
Aaron Ballman2b124d12014-03-13 16:36:16 +00002938 for (const auto *MD : RD->methods()) {
Reid Kleckner240ef572015-02-25 02:16:02 +00002939 MD = MD->getCanonicalDecl();
Timur Iskhodzhanov20df98c2013-10-06 15:31:37 +00002940 VisitedGroupIndicesTy::iterator J;
2941 bool Inserted;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00002942 std::tie(J, Inserted) = VisitedGroupIndices.insert(
Timur Iskhodzhanov20df98c2013-10-06 15:31:37 +00002943 std::make_pair(MD->getDeclName(), Groups.size()));
2944 if (Inserted)
Reid Kleckner6701de22014-02-19 22:06:10 +00002945 Groups.push_back(MethodGroup());
Aaron Ballman2b124d12014-03-13 16:36:16 +00002946 if (MD->isVirtual())
Timur Iskhodzhanov20df98c2013-10-06 15:31:37 +00002947 Groups[J->second].push_back(MD);
2948 }
2949
2950 for (unsigned I = 0, E = Groups.size(); I != E; ++I)
2951 VirtualMethods.append(Groups[I].rbegin(), Groups[I].rend());
2952}
2953
Timur Iskhodzhanovdd0a27662014-03-26 08:12:53 +00002954static bool isDirectVBase(const CXXRecordDecl *Base, const CXXRecordDecl *RD) {
2955 for (const auto &B : RD->bases()) {
2956 if (B.isVirtual() && B.getType()->getAsCXXRecordDecl() == Base)
2957 return true;
2958 }
2959 return false;
2960}
2961
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002962void VFTableBuilder::AddMethods(BaseSubobject Base, unsigned BaseDepth,
2963 const CXXRecordDecl *LastVBase,
2964 BasesSetVectorTy &VisitedBases) {
2965 const CXXRecordDecl *RD = Base.getBase();
2966 if (!RD->isPolymorphic())
2967 return;
2968
2969 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2970
2971 // See if this class expands a vftable of the base we look at, which is either
2972 // the one defined by the vfptr base path or the primary base of the current class.
Craig Topper36250ad2014-05-12 05:36:57 +00002973 const CXXRecordDecl *NextBase = nullptr, *NextLastVBase = LastVBase;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002974 CharUnits NextBaseOffset;
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00002975 if (BaseDepth < WhichVFPtr.PathToBaseWithVPtr.size()) {
2976 NextBase = WhichVFPtr.PathToBaseWithVPtr[BaseDepth];
Timur Iskhodzhanovdd0a27662014-03-26 08:12:53 +00002977 if (isDirectVBase(NextBase, RD)) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002978 NextLastVBase = NextBase;
2979 NextBaseOffset = MostDerivedClassLayout.getVBaseClassOffset(NextBase);
2980 } else {
2981 NextBaseOffset =
2982 Base.getBaseOffset() + Layout.getBaseClassOffset(NextBase);
2983 }
2984 } else if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
2985 assert(!Layout.isPrimaryBaseVirtual() &&
2986 "No primary virtual bases in this ABI");
2987 NextBase = PrimaryBase;
2988 NextBaseOffset = Base.getBaseOffset();
2989 }
2990
2991 if (NextBase) {
2992 AddMethods(BaseSubobject(NextBase, NextBaseOffset), BaseDepth + 1,
2993 NextLastVBase, VisitedBases);
2994 if (!VisitedBases.insert(NextBase))
2995 llvm_unreachable("Found a duplicate primary base!");
2996 }
2997
Timur Iskhodzhanov20df98c2013-10-06 15:31:37 +00002998 SmallVector<const CXXMethodDecl*, 10> VirtualMethods;
2999 // Put virtual methods in the proper order.
3000 GroupNewVirtualOverloads(RD, VirtualMethods);
3001
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003002 // Now go through all virtual member functions and add them to the current
3003 // vftable. This is done by
3004 // - replacing overridden methods in their existing slots, as long as they
3005 // don't require return adjustment; calculating This adjustment if needed.
3006 // - adding new slots for methods of the current base not present in any
3007 // sub-bases;
3008 // - adding new slots for methods that require Return adjustment.
3009 // We keep track of the methods visited in the sub-bases in MethodInfoMap.
Timur Iskhodzhanov20df98c2013-10-06 15:31:37 +00003010 for (unsigned I = 0, E = VirtualMethods.size(); I != E; ++I) {
3011 const CXXMethodDecl *MD = VirtualMethods[I];
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003012
Timur Iskhodzhanov70814602014-11-17 15:53:50 +00003013 FinalOverriders::OverriderInfo FinalOverrider =
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003014 Overriders.getOverrider(MD, Base.getBaseOffset());
Timur Iskhodzhanov70814602014-11-17 15:53:50 +00003015 const CXXMethodDecl *FinalOverriderMD = FinalOverrider.Method;
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00003016 const CXXMethodDecl *OverriddenMD =
3017 FindNearestOverriddenMethod(MD, VisitedBases);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003018
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00003019 ThisAdjustment ThisAdjustmentOffset;
Timur Iskhodzhanov16055e72014-08-10 11:40:51 +00003020 bool ReturnAdjustingThunk = false, ForceReturnAdjustmentMangling = false;
Timur Iskhodzhanov70814602014-11-17 15:53:50 +00003021 CharUnits ThisOffset = ComputeThisOffset(FinalOverrider);
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00003022 ThisAdjustmentOffset.NonVirtual =
3023 (ThisOffset - WhichVFPtr.FullOffsetInMDC).getQuantity();
Timur Iskhodzhanov70814602014-11-17 15:53:50 +00003024 if ((OverriddenMD || FinalOverriderMD != MD) &&
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00003025 WhichVFPtr.getVBaseWithVPtr())
Timur Iskhodzhanov70814602014-11-17 15:53:50 +00003026 CalculateVtordispAdjustment(FinalOverrider, ThisOffset,
3027 ThisAdjustmentOffset);
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00003028
3029 if (OverriddenMD) {
3030 // If MD overrides anything in this vftable, we need to update the entries.
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003031 MethodInfoMapTy::iterator OverriddenMDIterator =
3032 MethodInfoMap.find(OverriddenMD);
3033
3034 // If the overridden method went to a different vftable, skip it.
3035 if (OverriddenMDIterator == MethodInfoMap.end())
3036 continue;
3037
3038 MethodInfo &OverriddenMethodInfo = OverriddenMDIterator->second;
3039
Timur Iskhodzhanov16055e72014-08-10 11:40:51 +00003040 // Let's check if the overrider requires any return adjustments.
3041 // We must create a new slot if the MD's return type is not trivially
3042 // convertible to the OverriddenMD's one.
3043 // Once a chain of method overrides adds a return adjusting vftable slot,
3044 // all subsequent overrides will also use an extra method slot.
3045 ReturnAdjustingThunk = !ComputeReturnAdjustmentBaseOffset(
3046 Context, MD, OverriddenMD).isEmpty() ||
3047 OverriddenMethodInfo.UsesExtraSlot;
3048
3049 if (!ReturnAdjustingThunk) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003050 // No return adjustment needed - just replace the overridden method info
3051 // with the current info.
3052 MethodInfo MI(OverriddenMethodInfo.VBTableIndex,
3053 OverriddenMethodInfo.VFTableIndex);
3054 MethodInfoMap.erase(OverriddenMDIterator);
3055
3056 assert(!MethodInfoMap.count(MD) &&
3057 "Should not have method info for this method yet!");
3058 MethodInfoMap.insert(std::make_pair(MD, MI));
3059 continue;
Reid Kleckner31a9f742013-12-27 20:29:16 +00003060 }
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003061
Reid Kleckner31a9f742013-12-27 20:29:16 +00003062 // In case we need a return adjustment, we'll add a new slot for
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00003063 // the overrider. Mark the overriden method as shadowed by the new slot.
Reid Kleckner31a9f742013-12-27 20:29:16 +00003064 OverriddenMethodInfo.Shadowed = true;
Reid Kleckner31a9f742013-12-27 20:29:16 +00003065
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00003066 // Force a special name mangling for a return-adjusting thunk
3067 // unless the method is the final overrider without this adjustment.
Timur Iskhodzhanov16055e72014-08-10 11:40:51 +00003068 ForceReturnAdjustmentMangling =
Timur Iskhodzhanov70814602014-11-17 15:53:50 +00003069 !(MD == FinalOverriderMD && ThisAdjustmentOffset.isEmpty());
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003070 } else if (Base.getBaseOffset() != WhichVFPtr.FullOffsetInMDC ||
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003071 MD->size_overridden_methods()) {
3072 // Skip methods that don't belong to the vftable of the current class,
3073 // e.g. each method that wasn't seen in any of the visited sub-bases
3074 // but overrides multiple methods of other sub-bases.
3075 continue;
3076 }
3077
3078 // If we got here, MD is a method not seen in any of the sub-bases or
3079 // it requires return adjustment. Insert the method info for this method.
3080 unsigned VBIndex =
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003081 LastVBase ? VTables.getVBTableIndex(MostDerivedClass, LastVBase) : 0;
David Majnemer3c7228e2014-07-01 21:10:07 +00003082 MethodInfo MI(VBIndex,
Timur Iskhodzhanov16055e72014-08-10 11:40:51 +00003083 HasRTTIComponent ? Components.size() - 1 : Components.size(),
3084 ReturnAdjustingThunk);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003085
3086 assert(!MethodInfoMap.count(MD) &&
3087 "Should not have method info for this method yet!");
3088 MethodInfoMap.insert(std::make_pair(MD, MI));
3089
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003090 // Check if this overrider needs a return adjustment.
3091 // We don't want to do this for pure virtual member functions.
3092 BaseOffset ReturnAdjustmentOffset;
3093 ReturnAdjustment ReturnAdjustment;
Timur Iskhodzhanov70814602014-11-17 15:53:50 +00003094 if (!FinalOverriderMD->isPure()) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003095 ReturnAdjustmentOffset =
Timur Iskhodzhanov70814602014-11-17 15:53:50 +00003096 ComputeReturnAdjustmentBaseOffset(Context, FinalOverriderMD, MD);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003097 }
3098 if (!ReturnAdjustmentOffset.isEmpty()) {
Timur Iskhodzhanov16055e72014-08-10 11:40:51 +00003099 ForceReturnAdjustmentMangling = true;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003100 ReturnAdjustment.NonVirtual =
3101 ReturnAdjustmentOffset.NonVirtualOffset.getQuantity();
3102 if (ReturnAdjustmentOffset.VirtualBase) {
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00003103 const ASTRecordLayout &DerivedLayout =
3104 Context.getASTRecordLayout(ReturnAdjustmentOffset.DerivedClass);
3105 ReturnAdjustment.Virtual.Microsoft.VBPtrOffset =
3106 DerivedLayout.getVBPtrOffset().getQuantity();
3107 ReturnAdjustment.Virtual.Microsoft.VBIndex =
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003108 VTables.getVBTableIndex(ReturnAdjustmentOffset.DerivedClass,
3109 ReturnAdjustmentOffset.VirtualBase);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003110 }
3111 }
3112
Timur Iskhodzhanov70814602014-11-17 15:53:50 +00003113 AddMethod(FinalOverriderMD,
Timur Iskhodzhanov16055e72014-08-10 11:40:51 +00003114 ThunkInfo(ThisAdjustmentOffset, ReturnAdjustment,
3115 ForceReturnAdjustmentMangling ? MD : nullptr));
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003116 }
3117}
3118
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003119static void PrintBasePath(const VPtrInfo::BasePath &Path, raw_ostream &Out) {
3120 for (VPtrInfo::BasePath::const_reverse_iterator I = Path.rbegin(),
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003121 E = Path.rend(); I != E; ++I) {
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00003122 Out << "'";
3123 (*I)->printQualifiedName(Out);
3124 Out << "' in ";
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003125 }
3126}
3127
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00003128static void dumpMicrosoftThunkAdjustment(const ThunkInfo &TI, raw_ostream &Out,
3129 bool ContinueFirstLine) {
3130 const ReturnAdjustment &R = TI.Return;
3131 bool Multiline = false;
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00003132 const char *LinePrefix = "\n ";
3133 if (!R.isEmpty() || TI.Method) {
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00003134 if (!ContinueFirstLine)
3135 Out << LinePrefix;
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00003136 Out << "[return adjustment (to type '"
3137 << TI.Method->getReturnType().getCanonicalType().getAsString()
3138 << "'): ";
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00003139 if (R.Virtual.Microsoft.VBPtrOffset)
3140 Out << "vbptr at offset " << R.Virtual.Microsoft.VBPtrOffset << ", ";
3141 if (R.Virtual.Microsoft.VBIndex)
3142 Out << "vbase #" << R.Virtual.Microsoft.VBIndex << ", ";
3143 Out << R.NonVirtual << " non-virtual]";
3144 Multiline = true;
3145 }
3146
3147 const ThisAdjustment &T = TI.This;
3148 if (!T.isEmpty()) {
3149 if (Multiline || !ContinueFirstLine)
3150 Out << LinePrefix;
3151 Out << "[this adjustment: ";
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00003152 if (!TI.This.Virtual.isEmpty()) {
3153 assert(T.Virtual.Microsoft.VtordispOffset < 0);
3154 Out << "vtordisp at " << T.Virtual.Microsoft.VtordispOffset << ", ";
3155 if (T.Virtual.Microsoft.VBPtrOffset) {
3156 Out << "vbptr at " << T.Virtual.Microsoft.VBPtrOffset
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00003157 << " to the left,";
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00003158 assert(T.Virtual.Microsoft.VBOffsetOffset > 0);
3159 Out << LinePrefix << " vboffset at "
3160 << T.Virtual.Microsoft.VBOffsetOffset << " in the vbtable, ";
3161 }
3162 }
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00003163 Out << T.NonVirtual << " non-virtual]";
3164 }
3165}
3166
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003167void VFTableBuilder::dumpLayout(raw_ostream &Out) {
3168 Out << "VFTable for ";
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003169 PrintBasePath(WhichVFPtr.PathToBaseWithVPtr, Out);
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00003170 Out << "'";
3171 MostDerivedClass->printQualifiedName(Out);
Timur Iskhodzhanov77764b62014-03-05 13:54:07 +00003172 Out << "' (" << Components.size()
3173 << (Components.size() == 1 ? " entry" : " entries") << ").\n";
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003174
3175 for (unsigned I = 0, E = Components.size(); I != E; ++I) {
3176 Out << llvm::format("%4d | ", I);
3177
3178 const VTableComponent &Component = Components[I];
3179
3180 // Dump the component.
3181 switch (Component.getKind()) {
3182 case VTableComponent::CK_RTTI:
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00003183 Component.getRTTIDecl()->printQualifiedName(Out);
3184 Out << " RTTI";
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003185 break;
3186
3187 case VTableComponent::CK_FunctionPointer: {
3188 const CXXMethodDecl *MD = Component.getFunctionDecl();
3189
Reid Kleckner604c8b42013-12-27 19:43:59 +00003190 // FIXME: Figure out how to print the real thunk type, since they can
3191 // differ in the return type.
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003192 std::string Str = PredefinedExpr::ComputeName(
3193 PredefinedExpr::PrettyFunctionNoVirtual, MD);
3194 Out << Str;
3195 if (MD->isPure())
3196 Out << " [pure]";
3197
David Majnemerd59becb2014-09-12 04:38:08 +00003198 if (MD->isDeleted())
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003199 Out << " [deleted]";
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003200
3201 ThunkInfo Thunk = VTableThunks.lookup(I);
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00003202 if (!Thunk.isEmpty())
3203 dumpMicrosoftThunkAdjustment(Thunk, Out, /*ContinueFirstLine=*/false);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003204
3205 break;
3206 }
3207
3208 case VTableComponent::CK_DeletingDtorPointer: {
3209 const CXXDestructorDecl *DD = Component.getDestructorDecl();
3210
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00003211 DD->printQualifiedName(Out);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003212 Out << "() [scalar deleting]";
3213
3214 if (DD->isPure())
3215 Out << " [pure]";
3216
3217 ThunkInfo Thunk = VTableThunks.lookup(I);
3218 if (!Thunk.isEmpty()) {
3219 assert(Thunk.Return.isEmpty() &&
3220 "No return adjustment needed for destructors!");
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00003221 dumpMicrosoftThunkAdjustment(Thunk, Out, /*ContinueFirstLine=*/false);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003222 }
3223
3224 break;
3225 }
3226
3227 default:
3228 DiagnosticsEngine &Diags = Context.getDiagnostics();
3229 unsigned DiagID = Diags.getCustomDiagID(
3230 DiagnosticsEngine::Error,
3231 "Unexpected vftable component type %0 for component number %1");
3232 Diags.Report(MostDerivedClass->getLocation(), DiagID)
3233 << I << Component.getKind();
3234 }
3235
3236 Out << '\n';
3237 }
3238
3239 Out << '\n';
3240
3241 if (!Thunks.empty()) {
3242 // We store the method names in a map to get a stable order.
3243 std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls;
3244
3245 for (ThunksMapTy::const_iterator I = Thunks.begin(), E = Thunks.end();
3246 I != E; ++I) {
3247 const CXXMethodDecl *MD = I->first;
3248 std::string MethodName = PredefinedExpr::ComputeName(
3249 PredefinedExpr::PrettyFunctionNoVirtual, MD);
3250
3251 MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
3252 }
3253
3254 for (std::map<std::string, const CXXMethodDecl *>::const_iterator
3255 I = MethodNamesAndDecls.begin(),
3256 E = MethodNamesAndDecls.end();
3257 I != E; ++I) {
3258 const std::string &MethodName = I->first;
3259 const CXXMethodDecl *MD = I->second;
3260
3261 ThunkInfoVectorTy ThunksVector = Thunks[MD];
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00003262 std::stable_sort(ThunksVector.begin(), ThunksVector.end(),
Benjamin Kramerbbdd7642014-03-01 14:48:57 +00003263 [](const ThunkInfo &LHS, const ThunkInfo &RHS) {
3264 // Keep different thunks with the same adjustments in the order they
3265 // were put into the vector.
Benjamin Kramera741b8c2014-03-03 20:26:46 +00003266 return std::tie(LHS.This, LHS.Return) < std::tie(RHS.This, RHS.Return);
Benjamin Kramerbbdd7642014-03-01 14:48:57 +00003267 });
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003268
3269 Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
3270 Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n";
3271
3272 for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) {
3273 const ThunkInfo &Thunk = ThunksVector[I];
3274
3275 Out << llvm::format("%4d | ", I);
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00003276 dumpMicrosoftThunkAdjustment(Thunk, Out, /*ContinueFirstLine=*/true);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003277 Out << '\n';
3278 }
3279
3280 Out << '\n';
3281 }
3282 }
Timur Iskhodzhanov4fea4f92014-03-20 13:42:14 +00003283
3284 Out.flush();
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003285}
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003286
Reid Kleckner5f080942014-01-03 23:42:00 +00003287static bool setsIntersect(const llvm::SmallPtrSet<const CXXRecordDecl *, 4> &A,
Craig Topper3cb91b22014-08-27 06:28:16 +00003288 ArrayRef<const CXXRecordDecl *> B) {
Craig Topper00bbdcf2014-06-28 23:22:23 +00003289 for (ArrayRef<const CXXRecordDecl *>::iterator I = B.begin(), E = B.end();
Reid Kleckner5f080942014-01-03 23:42:00 +00003290 I != E; ++I) {
3291 if (A.count(*I))
3292 return true;
3293 }
3294 return false;
3295}
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003296
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003297static bool rebucketPaths(VPtrInfoVector &Paths);
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003298
Reid Kleckner5f080942014-01-03 23:42:00 +00003299/// Produces MSVC-compatible vbtable data. The symbols produced by this
3300/// algorithm match those produced by MSVC 2012 and newer, which is different
3301/// from MSVC 2010.
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003302///
3303/// MSVC 2012 appears to minimize the vbtable names using the following
3304/// algorithm. First, walk the class hierarchy in the usual order, depth first,
3305/// left to right, to find all of the subobjects which contain a vbptr field.
3306/// Visiting each class node yields a list of inheritance paths to vbptrs. Each
3307/// record with a vbptr creates an initially empty path.
3308///
3309/// To combine paths from child nodes, the paths are compared to check for
3310/// ambiguity. Paths are "ambiguous" if multiple paths have the same set of
3311/// components in the same order. Each group of ambiguous paths is extended by
3312/// appending the class of the base from which it came. If the current class
3313/// node produced an ambiguous path, its path is extended with the current class.
3314/// After extending paths, MSVC again checks for ambiguity, and extends any
3315/// ambiguous path which wasn't already extended. Because each node yields an
3316/// unambiguous set of paths, MSVC doesn't need to extend any path more than once
3317/// to produce an unambiguous set of paths.
3318///
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003319/// TODO: Presumably vftables use the same algorithm.
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003320void MicrosoftVTableContext::computeVTablePaths(bool ForVBTables,
3321 const CXXRecordDecl *RD,
3322 VPtrInfoVector &Paths) {
Reid Kleckner5f080942014-01-03 23:42:00 +00003323 assert(Paths.empty());
3324 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003325
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003326 // Base case: this subobject has its own vptr.
3327 if (ForVBTables ? Layout.hasOwnVBPtr() : Layout.hasOwnVFPtr())
3328 Paths.push_back(new VPtrInfo(RD));
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003329
Reid Kleckner5f080942014-01-03 23:42:00 +00003330 // Recursive case: get all the vbtables from our bases and remove anything
Reid Kleckner15fdcf12014-09-22 23:14:46 +00003331 // that shares a virtual base.
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003332 llvm::SmallPtrSet<const CXXRecordDecl*, 4> VBasesSeen;
Reid Kleckner15fdcf12014-09-22 23:14:46 +00003333 for (const auto &B : RD->bases()) {
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00003334 const CXXRecordDecl *Base = B.getType()->getAsCXXRecordDecl();
3335 if (B.isVirtual() && VBasesSeen.count(Base))
Reid Kleckner5f080942014-01-03 23:42:00 +00003336 continue;
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003337
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003338 if (!Base->isDynamicClass())
3339 continue;
Reid Kleckner5f080942014-01-03 23:42:00 +00003340
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003341 const VPtrInfoVector &BasePaths =
3342 ForVBTables ? enumerateVBTables(Base) : getVFPtrOffsets(Base);
3343
Reid Klecknerfd385402014-04-17 22:47:52 +00003344 for (VPtrInfo *BaseInfo : BasePaths) {
Reid Kleckner5f080942014-01-03 23:42:00 +00003345 // Don't include the path if it goes through a virtual base that we've
3346 // already included.
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003347 if (setsIntersect(VBasesSeen, BaseInfo->ContainingVBases))
Reid Kleckner5f080942014-01-03 23:42:00 +00003348 continue;
3349
3350 // Copy the path and adjust it as necessary.
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003351 VPtrInfo *P = new VPtrInfo(*BaseInfo);
Reid Kleckner5f080942014-01-03 23:42:00 +00003352
3353 // We mangle Base into the path if the path would've been ambiguous and it
3354 // wasn't already extended with Base.
3355 if (P->MangledPath.empty() || P->MangledPath.back() != Base)
3356 P->NextBaseToMangle = Base;
3357
Reid Klecknerfd385402014-04-17 22:47:52 +00003358 // Keep track of which vtable the derived class is going to extend with
3359 // new methods or bases. We append to either the vftable of our primary
3360 // base, or the first non-virtual base that has a vbtable.
3361 if (P->ReusingBase == Base &&
3362 Base == (ForVBTables ? Layout.getBaseSharingVBPtr()
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003363 : Layout.getPrimaryBase()))
Reid Kleckner5f080942014-01-03 23:42:00 +00003364 P->ReusingBase = RD;
Reid Klecknerfd385402014-04-17 22:47:52 +00003365
3366 // Keep track of the full adjustment from the MDC to this vtable. The
3367 // adjustment is captured by an optional vbase and a non-virtual offset.
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00003368 if (B.isVirtual())
Reid Kleckner5f080942014-01-03 23:42:00 +00003369 P->ContainingVBases.push_back(Base);
3370 else if (P->ContainingVBases.empty())
3371 P->NonVirtualOffset += Layout.getBaseClassOffset(Base);
3372
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003373 // Update the full offset in the MDC.
3374 P->FullOffsetInMDC = P->NonVirtualOffset;
3375 if (const CXXRecordDecl *VB = P->getVBaseWithVPtr())
3376 P->FullOffsetInMDC += Layout.getVBaseClassOffset(VB);
3377
Reid Kleckner5f080942014-01-03 23:42:00 +00003378 Paths.push_back(P);
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003379 }
3380
Timur Iskhodzhanov9ae7d3b2014-03-31 11:01:51 +00003381 if (B.isVirtual())
3382 VBasesSeen.insert(Base);
3383
Reid Kleckner5f080942014-01-03 23:42:00 +00003384 // After visiting any direct base, we've transitively visited all of its
3385 // morally virtual bases.
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00003386 for (const auto &VB : Base->vbases())
3387 VBasesSeen.insert(VB.getType()->getAsCXXRecordDecl());
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003388 }
3389
Reid Kleckner5f080942014-01-03 23:42:00 +00003390 // Sort the paths into buckets, and if any of them are ambiguous, extend all
3391 // paths in ambiguous buckets.
3392 bool Changed = true;
3393 while (Changed)
3394 Changed = rebucketPaths(Paths);
3395}
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003396
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003397static bool extendPath(VPtrInfo *P) {
Reid Kleckner5f080942014-01-03 23:42:00 +00003398 if (P->NextBaseToMangle) {
3399 P->MangledPath.push_back(P->NextBaseToMangle);
Craig Topper36250ad2014-05-12 05:36:57 +00003400 P->NextBaseToMangle = nullptr;// Prevent the path from being extended twice.
Reid Kleckner5f080942014-01-03 23:42:00 +00003401 return true;
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003402 }
Reid Kleckner5f080942014-01-03 23:42:00 +00003403 return false;
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003404}
3405
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003406static bool rebucketPaths(VPtrInfoVector &Paths) {
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003407 // What we're essentially doing here is bucketing together ambiguous paths.
3408 // Any bucket with more than one path in it gets extended by NextBase, which
3409 // is usually the direct base of the inherited the vbptr. This code uses a
3410 // sorted vector to implement a multiset to form the buckets. Note that the
3411 // ordering is based on pointers, but it doesn't change our output order. The
3412 // current algorithm is designed to match MSVC 2012's names.
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003413 VPtrInfoVector PathsSorted(Paths);
Benjamin Kramer15ae7832014-03-07 21:35:40 +00003414 std::sort(PathsSorted.begin(), PathsSorted.end(),
3415 [](const VPtrInfo *LHS, const VPtrInfo *RHS) {
3416 return LHS->MangledPath < RHS->MangledPath;
3417 });
Reid Kleckner5f080942014-01-03 23:42:00 +00003418 bool Changed = false;
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003419 for (size_t I = 0, E = PathsSorted.size(); I != E;) {
3420 // Scan forward to find the end of the bucket.
3421 size_t BucketStart = I;
3422 do {
3423 ++I;
Reid Kleckner5f080942014-01-03 23:42:00 +00003424 } while (I != E && PathsSorted[BucketStart]->MangledPath ==
3425 PathsSorted[I]->MangledPath);
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003426
3427 // If this bucket has multiple paths, extend them all.
3428 if (I - BucketStart > 1) {
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003429 for (size_t II = BucketStart; II != I; ++II)
Reid Kleckner5f080942014-01-03 23:42:00 +00003430 Changed |= extendPath(PathsSorted[II]);
3431 assert(Changed && "no paths were extended to fix ambiguity");
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003432 }
3433 }
Reid Kleckner5f080942014-01-03 23:42:00 +00003434 return Changed;
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003435}
3436
3437MicrosoftVTableContext::~MicrosoftVTableContext() {
Nico Weberd19e6a72014-04-24 19:52:12 +00003438 for (auto &P : VFPtrLocations)
3439 llvm::DeleteContainerPointers(*P.second);
Reid Kleckner33311282014-02-28 23:26:22 +00003440 llvm::DeleteContainerSeconds(VFPtrLocations);
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003441 llvm::DeleteContainerSeconds(VFTableLayouts);
3442 llvm::DeleteContainerSeconds(VBaseInfo);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003443}
3444
Reid Kleckner15fdcf12014-09-22 23:14:46 +00003445static bool
3446findPathForVPtr(ASTContext &Context, const ASTRecordLayout &MostDerivedLayout,
3447 const CXXRecordDecl *RD, CharUnits Offset,
3448 llvm::SmallPtrSetImpl<const CXXRecordDecl *> &VBasesSeen,
3449 VPtrInfo::BasePath &FullPath, VPtrInfo *Info) {
3450 if (RD == Info->BaseWithVPtr && Offset == Info->FullOffsetInMDC) {
3451 Info->PathToBaseWithVPtr = FullPath;
3452 return true;
3453 }
3454
3455 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3456
3457 // Recurse with non-virtual bases first.
3458 // FIXME: Does this need to be in layout order? Virtual bases will be in base
3459 // specifier order, which isn't necessarily layout order.
3460 SmallVector<CXXBaseSpecifier, 4> Bases(RD->bases_begin(), RD->bases_end());
3461 std::stable_partition(Bases.begin(), Bases.end(),
3462 [](CXXBaseSpecifier bs) { return !bs.isVirtual(); });
3463
3464 for (const auto &B : Bases) {
3465 const CXXRecordDecl *Base = B.getType()->getAsCXXRecordDecl();
3466 CharUnits NewOffset;
3467 if (!B.isVirtual())
3468 NewOffset = Offset + Layout.getBaseClassOffset(Base);
3469 else {
David Blaikie82e95a32014-11-19 07:49:47 +00003470 if (!VBasesSeen.insert(Base).second)
Reid Kleckner15fdcf12014-09-22 23:14:46 +00003471 return false;
Reid Kleckner15fdcf12014-09-22 23:14:46 +00003472 NewOffset = MostDerivedLayout.getVBaseClassOffset(Base);
3473 }
3474 FullPath.push_back(Base);
3475 if (findPathForVPtr(Context, MostDerivedLayout, Base, NewOffset, VBasesSeen,
3476 FullPath, Info))
3477 return true;
3478 FullPath.pop_back();
3479 }
3480 return false;
3481}
3482
3483static void computeFullPathsForVFTables(ASTContext &Context,
3484 const CXXRecordDecl *RD,
3485 VPtrInfoVector &Paths) {
3486 llvm::SmallPtrSet<const CXXRecordDecl*, 4> VBasesSeen;
3487 const ASTRecordLayout &MostDerivedLayout = Context.getASTRecordLayout(RD);
3488 VPtrInfo::BasePath FullPath;
3489 for (VPtrInfo *Info : Paths) {
3490 findPathForVPtr(Context, MostDerivedLayout, RD, CharUnits::Zero(),
3491 VBasesSeen, FullPath, Info);
3492 VBasesSeen.clear();
3493 FullPath.clear();
3494 }
3495}
3496
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003497void MicrosoftVTableContext::computeVTableRelatedInformation(
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003498 const CXXRecordDecl *RD) {
3499 assert(RD->isDynamicClass());
3500
3501 // Check if we've computed this information before.
3502 if (VFPtrLocations.count(RD))
3503 return;
3504
3505 const VTableLayout::AddressPointsMapTy EmptyAddressPointsMap;
3506
Reid Klecknerd6f9b832014-02-27 22:51:43 +00003507 VPtrInfoVector *VFPtrs = new VPtrInfoVector();
3508 computeVTablePaths(/*ForVBTables=*/false, RD, *VFPtrs);
Reid Kleckner15fdcf12014-09-22 23:14:46 +00003509 computeFullPathsForVFTables(Context, RD, *VFPtrs);
Reid Klecknerd6f9b832014-02-27 22:51:43 +00003510 VFPtrLocations[RD] = VFPtrs;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003511
3512 MethodVFTableLocationsTy NewMethodLocations;
Reid Klecknerd6f9b832014-02-27 22:51:43 +00003513 for (VPtrInfoVector::iterator I = VFPtrs->begin(), E = VFPtrs->end();
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003514 I != E; ++I) {
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003515 VFTableBuilder Builder(*this, RD, *I);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003516
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003517 VFTableIdTy id(RD, (*I)->FullOffsetInMDC);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003518 assert(VFTableLayouts.count(id) == 0);
3519 SmallVector<VTableLayout::VTableThunkTy, 1> VTableThunks(
3520 Builder.vtable_thunks_begin(), Builder.vtable_thunks_end());
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003521 VFTableLayouts[id] = new VTableLayout(
3522 Builder.getNumVTableComponents(), Builder.vtable_component_begin(),
3523 VTableThunks.size(), VTableThunks.data(), EmptyAddressPointsMap, true);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003524 Thunks.insert(Builder.thunks_begin(), Builder.thunks_end());
Timur Iskhodzhanovba557022014-03-20 20:38:34 +00003525
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00003526 for (const auto &Loc : Builder.vtable_locations()) {
3527 GlobalDecl GD = Loc.first;
3528 MethodVFTableLocation NewLoc = Loc.second;
Timur Iskhodzhanovba557022014-03-20 20:38:34 +00003529 auto M = NewMethodLocations.find(GD);
3530 if (M == NewMethodLocations.end() || NewLoc < M->second)
3531 NewMethodLocations[GD] = NewLoc;
3532 }
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003533 }
3534
3535 MethodVFTableLocations.insert(NewMethodLocations.begin(),
3536 NewMethodLocations.end());
3537 if (Context.getLangOpts().DumpVTableLayouts)
Reid Kleckner5bc6d0f2013-11-08 21:28:00 +00003538 dumpMethodLocations(RD, NewMethodLocations, llvm::outs());
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003539}
3540
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003541void MicrosoftVTableContext::dumpMethodLocations(
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003542 const CXXRecordDecl *RD, const MethodVFTableLocationsTy &NewMethods,
3543 raw_ostream &Out) {
3544 // Compute the vtable indices for all the member functions.
3545 // Store them in a map keyed by the location so we'll get a sorted table.
3546 std::map<MethodVFTableLocation, std::string> IndicesMap;
3547 bool HasNonzeroOffset = false;
3548
3549 for (MethodVFTableLocationsTy::const_iterator I = NewMethods.begin(),
3550 E = NewMethods.end(); I != E; ++I) {
3551 const CXXMethodDecl *MD = cast<const CXXMethodDecl>(I->first.getDecl());
3552 assert(MD->isVirtual());
3553
3554 std::string MethodName = PredefinedExpr::ComputeName(
3555 PredefinedExpr::PrettyFunctionNoVirtual, MD);
3556
3557 if (isa<CXXDestructorDecl>(MD)) {
3558 IndicesMap[I->second] = MethodName + " [scalar deleting]";
3559 } else {
3560 IndicesMap[I->second] = MethodName;
3561 }
3562
Timur Iskhodzhanov9e7f5052013-11-07 13:34:02 +00003563 if (!I->second.VFPtrOffset.isZero() || I->second.VBTableIndex != 0)
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003564 HasNonzeroOffset = true;
3565 }
3566
3567 // Print the vtable indices for all the member functions.
3568 if (!IndicesMap.empty()) {
3569 Out << "VFTable indices for ";
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00003570 Out << "'";
3571 RD->printQualifiedName(Out);
Timur Iskhodzhanov77764b62014-03-05 13:54:07 +00003572 Out << "' (" << IndicesMap.size()
3573 << (IndicesMap.size() == 1 ? " entry" : " entries") << ").\n";
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003574
3575 CharUnits LastVFPtrOffset = CharUnits::fromQuantity(-1);
3576 uint64_t LastVBIndex = 0;
3577 for (std::map<MethodVFTableLocation, std::string>::const_iterator
3578 I = IndicesMap.begin(),
3579 E = IndicesMap.end();
3580 I != E; ++I) {
Timur Iskhodzhanov9e7f5052013-11-07 13:34:02 +00003581 CharUnits VFPtrOffset = I->first.VFPtrOffset;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003582 uint64_t VBIndex = I->first.VBTableIndex;
3583 if (HasNonzeroOffset &&
3584 (VFPtrOffset != LastVFPtrOffset || VBIndex != LastVBIndex)) {
3585 assert(VBIndex > LastVBIndex || VFPtrOffset > LastVFPtrOffset);
3586 Out << " -- accessible via ";
3587 if (VBIndex)
3588 Out << "vbtable index " << VBIndex << ", ";
3589 Out << "vfptr at offset " << VFPtrOffset.getQuantity() << " --\n";
3590 LastVFPtrOffset = VFPtrOffset;
3591 LastVBIndex = VBIndex;
3592 }
3593
3594 uint64_t VTableIndex = I->first.Index;
3595 const std::string &MethodName = I->second;
3596 Out << llvm::format("%4" PRIu64 " | ", VTableIndex) << MethodName << '\n';
3597 }
3598 Out << '\n';
3599 }
Timur Iskhodzhanov4fea4f92014-03-20 13:42:14 +00003600
3601 Out.flush();
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003602}
3603
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003604const VirtualBaseInfo *MicrosoftVTableContext::computeVBTableRelatedInformation(
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003605 const CXXRecordDecl *RD) {
Reid Kleckner5f080942014-01-03 23:42:00 +00003606 VirtualBaseInfo *VBI;
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003607
Reid Kleckner5f080942014-01-03 23:42:00 +00003608 {
3609 // Get or create a VBI for RD. Don't hold a reference to the DenseMap cell,
3610 // as it may be modified and rehashed under us.
3611 VirtualBaseInfo *&Entry = VBaseInfo[RD];
3612 if (Entry)
3613 return Entry;
3614 Entry = VBI = new VirtualBaseInfo();
3615 }
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003616
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003617 computeVTablePaths(/*ForVBTables=*/true, RD, VBI->VBPtrPaths);
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003618
Timur Iskhodzhanov2c9341f2013-11-08 11:45:35 +00003619 // First, see if the Derived class shared the vbptr with a non-virtual base.
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003620 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Timur Iskhodzhanov2c9341f2013-11-08 11:45:35 +00003621 if (const CXXRecordDecl *VBPtrBase = Layout.getBaseSharingVBPtr()) {
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003622 // If the Derived class shares the vbptr with a non-virtual base, the shared
3623 // virtual bases come first so that the layout is the same.
3624 const VirtualBaseInfo *BaseInfo =
3625 computeVBTableRelatedInformation(VBPtrBase);
Reid Kleckner5f080942014-01-03 23:42:00 +00003626 VBI->VBTableIndices.insert(BaseInfo->VBTableIndices.begin(),
3627 BaseInfo->VBTableIndices.end());
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003628 }
3629
3630 // New vbases are added to the end of the vbtable.
3631 // Skip the self entry and vbases visited in the non-virtual base, if any.
Reid Kleckner5f080942014-01-03 23:42:00 +00003632 unsigned VBTableIndex = 1 + VBI->VBTableIndices.size();
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00003633 for (const auto &VB : RD->vbases()) {
3634 const CXXRecordDecl *CurVBase = VB.getType()->getAsCXXRecordDecl();
Reid Kleckner5f080942014-01-03 23:42:00 +00003635 if (!VBI->VBTableIndices.count(CurVBase))
3636 VBI->VBTableIndices[CurVBase] = VBTableIndex++;
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003637 }
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003638
Reid Kleckner5f080942014-01-03 23:42:00 +00003639 return VBI;
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003640}
3641
3642unsigned MicrosoftVTableContext::getVBTableIndex(const CXXRecordDecl *Derived,
3643 const CXXRecordDecl *VBase) {
3644 const VirtualBaseInfo *VBInfo = computeVBTableRelatedInformation(Derived);
3645 assert(VBInfo->VBTableIndices.count(VBase));
3646 return VBInfo->VBTableIndices.find(VBase)->second;
3647}
3648
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003649const VPtrInfoVector &
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003650MicrosoftVTableContext::enumerateVBTables(const CXXRecordDecl *RD) {
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003651 return computeVBTableRelatedInformation(RD)->VBPtrPaths;
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003652}
3653
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003654const VPtrInfoVector &
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003655MicrosoftVTableContext::getVFPtrOffsets(const CXXRecordDecl *RD) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003656 computeVTableRelatedInformation(RD);
3657
3658 assert(VFPtrLocations.count(RD) && "Couldn't find vfptr locations");
Reid Klecknerd6f9b832014-02-27 22:51:43 +00003659 return *VFPtrLocations[RD];
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003660}
3661
3662const VTableLayout &
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003663MicrosoftVTableContext::getVFTableLayout(const CXXRecordDecl *RD,
3664 CharUnits VFPtrOffset) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003665 computeVTableRelatedInformation(RD);
3666
3667 VFTableIdTy id(RD, VFPtrOffset);
3668 assert(VFTableLayouts.count(id) && "Couldn't find a VFTable at this offset");
3669 return *VFTableLayouts[id];
3670}
3671
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003672const MicrosoftVTableContext::MethodVFTableLocation &
3673MicrosoftVTableContext::getMethodVFTableLocation(GlobalDecl GD) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003674 assert(cast<CXXMethodDecl>(GD.getDecl())->isVirtual() &&
3675 "Only use this method for virtual methods or dtors");
3676 if (isa<CXXDestructorDecl>(GD.getDecl()))
3677 assert(GD.getDtorType() == Dtor_Deleting);
3678
3679 MethodVFTableLocationsTy::iterator I = MethodVFTableLocations.find(GD);
3680 if (I != MethodVFTableLocations.end())
3681 return I->second;
3682
3683 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
3684
3685 computeVTableRelatedInformation(RD);
3686
3687 I = MethodVFTableLocations.find(GD);
3688 assert(I != MethodVFTableLocations.end() && "Did not find index!");
3689 return I->second;
3690}