blob: 49e13559d7fc1d152ac31ec50300168e41a42460 [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 Iskhodzhanov6b128502014-04-22 17:32:02 +000067 /// VirtualBase - The virtual base class subobject of this overridder.
68 /// 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()) {
Peter Collingbournecfd23562011-09-26 01:57:12 +0000392 if (!VisitedVirtualBases.insert(BaseDecl)) {
393 // 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;
414
415 OverriderInfo Overrider = getOverrider(MD, Base.getBaseOffset());
416
Aaron Ballman75ee4cc2014-01-03 18:42:48 +0000417 Out << " ";
418 MD->printQualifiedName(Out);
419 Out << " - (";
420 Overrider.Method->printQualifiedName(Out);
Timur Iskhodzhanovbe5a3d82013-06-05 06:40:07 +0000421 Out << ", " << Overrider.Offset.getQuantity() << ')';
Peter Collingbournecfd23562011-09-26 01:57:12 +0000422
423 BaseOffset Offset;
424 if (!Overrider.Method->isPure())
425 Offset = ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD);
426
427 if (!Offset.isEmpty()) {
428 Out << " [ret-adj: ";
Aaron Ballman75ee4cc2014-01-03 18:42:48 +0000429 if (Offset.VirtualBase) {
430 Offset.VirtualBase->printQualifiedName(Out);
431 Out << " vbase, ";
432 }
Peter Collingbournecfd23562011-09-26 01:57:12 +0000433
434 Out << Offset.NonVirtualOffset.getQuantity() << " nv]";
435 }
436
437 Out << "\n";
438 }
439}
440
441/// VCallOffsetMap - Keeps track of vcall offsets when building a vtable.
442struct VCallOffsetMap {
443
444 typedef std::pair<const CXXMethodDecl *, CharUnits> MethodAndOffsetPairTy;
445
446 /// Offsets - Keeps track of methods and their offsets.
447 // FIXME: This should be a real map and not a vector.
448 SmallVector<MethodAndOffsetPairTy, 16> Offsets;
449
450 /// MethodsCanShareVCallOffset - Returns whether two virtual member functions
451 /// can share the same vcall offset.
452 static bool MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
453 const CXXMethodDecl *RHS);
454
455public:
456 /// AddVCallOffset - Adds a vcall offset to the map. Returns true if the
457 /// add was successful, or false if there was already a member function with
458 /// the same signature in the map.
459 bool AddVCallOffset(const CXXMethodDecl *MD, CharUnits OffsetOffset);
460
461 /// getVCallOffsetOffset - Returns the vcall offset offset (relative to the
462 /// vtable address point) for the given virtual member function.
463 CharUnits getVCallOffsetOffset(const CXXMethodDecl *MD);
464
465 // empty - Return whether the offset map is empty or not.
466 bool empty() const { return Offsets.empty(); }
467};
468
469static bool HasSameVirtualSignature(const CXXMethodDecl *LHS,
470 const CXXMethodDecl *RHS) {
John McCallb6c4a7e2012-03-21 06:57:19 +0000471 const FunctionProtoType *LT =
472 cast<FunctionProtoType>(LHS->getType().getCanonicalType());
473 const FunctionProtoType *RT =
474 cast<FunctionProtoType>(RHS->getType().getCanonicalType());
Peter Collingbournecfd23562011-09-26 01:57:12 +0000475
476 // Fast-path matches in the canonical types.
477 if (LT == RT) return true;
478
479 // Force the signatures to match. We can't rely on the overrides
480 // list here because there isn't necessarily an inheritance
481 // relationship between the two methods.
John McCallb6c4a7e2012-03-21 06:57:19 +0000482 if (LT->getTypeQuals() != RT->getTypeQuals() ||
Alp Toker9cacbab2014-01-20 20:26:09 +0000483 LT->getNumParams() != RT->getNumParams())
Peter Collingbournecfd23562011-09-26 01:57:12 +0000484 return false;
Alp Toker9cacbab2014-01-20 20:26:09 +0000485 for (unsigned I = 0, E = LT->getNumParams(); I != E; ++I)
486 if (LT->getParamType(I) != RT->getParamType(I))
Peter Collingbournecfd23562011-09-26 01:57:12 +0000487 return false;
488 return true;
489}
490
491bool VCallOffsetMap::MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
492 const CXXMethodDecl *RHS) {
493 assert(LHS->isVirtual() && "LHS must be virtual!");
494 assert(RHS->isVirtual() && "LHS must be virtual!");
495
496 // A destructor can share a vcall offset with another destructor.
497 if (isa<CXXDestructorDecl>(LHS))
498 return isa<CXXDestructorDecl>(RHS);
499
500 // FIXME: We need to check more things here.
501
502 // The methods must have the same name.
503 DeclarationName LHSName = LHS->getDeclName();
504 DeclarationName RHSName = RHS->getDeclName();
505 if (LHSName != RHSName)
506 return false;
507
508 // And the same signatures.
509 return HasSameVirtualSignature(LHS, RHS);
510}
511
512bool VCallOffsetMap::AddVCallOffset(const CXXMethodDecl *MD,
513 CharUnits OffsetOffset) {
514 // Check if we can reuse an offset.
515 for (unsigned I = 0, E = Offsets.size(); I != E; ++I) {
516 if (MethodsCanShareVCallOffset(Offsets[I].first, MD))
517 return false;
518 }
519
520 // Add the offset.
521 Offsets.push_back(MethodAndOffsetPairTy(MD, OffsetOffset));
522 return true;
523}
524
525CharUnits VCallOffsetMap::getVCallOffsetOffset(const CXXMethodDecl *MD) {
526 // Look for an offset.
527 for (unsigned I = 0, E = Offsets.size(); I != E; ++I) {
528 if (MethodsCanShareVCallOffset(Offsets[I].first, MD))
529 return Offsets[I].second;
530 }
531
532 llvm_unreachable("Should always find a vcall offset offset!");
533}
534
535/// VCallAndVBaseOffsetBuilder - Class for building vcall and vbase offsets.
536class VCallAndVBaseOffsetBuilder {
537public:
538 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits>
539 VBaseOffsetOffsetsMapTy;
540
541private:
542 /// MostDerivedClass - The most derived class for which we're building vcall
543 /// and vbase offsets.
544 const CXXRecordDecl *MostDerivedClass;
545
546 /// LayoutClass - The class we're using for layout information. Will be
547 /// different than the most derived class if we're building a construction
548 /// vtable.
549 const CXXRecordDecl *LayoutClass;
550
551 /// Context - The ASTContext which we will use for layout information.
552 ASTContext &Context;
553
554 /// Components - vcall and vbase offset components
555 typedef SmallVector<VTableComponent, 64> VTableComponentVectorTy;
556 VTableComponentVectorTy Components;
557
558 /// VisitedVirtualBases - Visited virtual bases.
559 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
560
561 /// VCallOffsets - Keeps track of vcall offsets.
562 VCallOffsetMap VCallOffsets;
563
564
565 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets,
566 /// relative to the address point.
567 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
568
569 /// FinalOverriders - The final overriders of the most derived class.
570 /// (Can be null when we're not building a vtable of the most derived class).
571 const FinalOverriders *Overriders;
572
573 /// AddVCallAndVBaseOffsets - Add vcall offsets and vbase offsets for the
574 /// given base subobject.
575 void AddVCallAndVBaseOffsets(BaseSubobject Base, bool BaseIsVirtual,
576 CharUnits RealBaseOffset);
577
578 /// AddVCallOffsets - Add vcall offsets for the given base subobject.
579 void AddVCallOffsets(BaseSubobject Base, CharUnits VBaseOffset);
580
581 /// AddVBaseOffsets - Add vbase offsets for the given class.
582 void AddVBaseOffsets(const CXXRecordDecl *Base,
583 CharUnits OffsetInLayoutClass);
584
585 /// getCurrentOffsetOffset - Get the current vcall or vbase offset offset in
586 /// chars, relative to the vtable address point.
587 CharUnits getCurrentOffsetOffset() const;
588
589public:
590 VCallAndVBaseOffsetBuilder(const CXXRecordDecl *MostDerivedClass,
591 const CXXRecordDecl *LayoutClass,
592 const FinalOverriders *Overriders,
593 BaseSubobject Base, bool BaseIsVirtual,
594 CharUnits OffsetInLayoutClass)
595 : MostDerivedClass(MostDerivedClass), LayoutClass(LayoutClass),
596 Context(MostDerivedClass->getASTContext()), Overriders(Overriders) {
597
598 // Add vcall and vbase offsets.
599 AddVCallAndVBaseOffsets(Base, BaseIsVirtual, OffsetInLayoutClass);
600 }
601
602 /// Methods for iterating over the components.
603 typedef VTableComponentVectorTy::const_reverse_iterator const_iterator;
604 const_iterator components_begin() const { return Components.rbegin(); }
605 const_iterator components_end() const { return Components.rend(); }
606
607 const VCallOffsetMap &getVCallOffsets() const { return VCallOffsets; }
608 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
609 return VBaseOffsetOffsets;
610 }
611};
612
613void
614VCallAndVBaseOffsetBuilder::AddVCallAndVBaseOffsets(BaseSubobject Base,
615 bool BaseIsVirtual,
616 CharUnits RealBaseOffset) {
617 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base.getBase());
618
619 // Itanium C++ ABI 2.5.2:
620 // ..in classes sharing a virtual table with a primary base class, the vcall
621 // and vbase offsets added by the derived class all come before the vcall
622 // and vbase offsets required by the base class, so that the latter may be
623 // laid out as required by the base class without regard to additions from
624 // the derived class(es).
625
626 // (Since we're emitting the vcall and vbase offsets in reverse order, we'll
627 // emit them for the primary base first).
628 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
629 bool PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual();
630
631 CharUnits PrimaryBaseOffset;
632
633 // Get the base offset of the primary base.
634 if (PrimaryBaseIsVirtual) {
Benjamin Kramer2ef30312012-07-04 18:45:14 +0000635 assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() &&
Peter Collingbournecfd23562011-09-26 01:57:12 +0000636 "Primary vbase should have a zero offset!");
637
638 const ASTRecordLayout &MostDerivedClassLayout =
639 Context.getASTRecordLayout(MostDerivedClass);
640
641 PrimaryBaseOffset =
642 MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
643 } else {
Benjamin Kramer2ef30312012-07-04 18:45:14 +0000644 assert(Layout.getBaseClassOffset(PrimaryBase).isZero() &&
Peter Collingbournecfd23562011-09-26 01:57:12 +0000645 "Primary base should have a zero offset!");
646
647 PrimaryBaseOffset = Base.getBaseOffset();
648 }
649
650 AddVCallAndVBaseOffsets(
651 BaseSubobject(PrimaryBase,PrimaryBaseOffset),
652 PrimaryBaseIsVirtual, RealBaseOffset);
653 }
654
655 AddVBaseOffsets(Base.getBase(), RealBaseOffset);
656
657 // We only want to add vcall offsets for virtual bases.
658 if (BaseIsVirtual)
659 AddVCallOffsets(Base, RealBaseOffset);
660}
661
662CharUnits VCallAndVBaseOffsetBuilder::getCurrentOffsetOffset() const {
663 // OffsetIndex is the index of this vcall or vbase offset, relative to the
664 // vtable address point. (We subtract 3 to account for the information just
665 // above the address point, the RTTI info, the offset to top, and the
666 // vcall offset itself).
667 int64_t OffsetIndex = -(int64_t)(3 + Components.size());
668
669 CharUnits PointerWidth =
670 Context.toCharUnitsFromBits(Context.getTargetInfo().getPointerWidth(0));
671 CharUnits OffsetOffset = PointerWidth * OffsetIndex;
672 return OffsetOffset;
673}
674
675void VCallAndVBaseOffsetBuilder::AddVCallOffsets(BaseSubobject Base,
676 CharUnits VBaseOffset) {
677 const CXXRecordDecl *RD = Base.getBase();
678 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
679
680 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
681
682 // Handle the primary base first.
683 // We only want to add vcall offsets if the base is non-virtual; a virtual
684 // primary base will have its vcall and vbase offsets emitted already.
685 if (PrimaryBase && !Layout.isPrimaryBaseVirtual()) {
686 // Get the base offset of the primary base.
Benjamin Kramer2ef30312012-07-04 18:45:14 +0000687 assert(Layout.getBaseClassOffset(PrimaryBase).isZero() &&
Peter Collingbournecfd23562011-09-26 01:57:12 +0000688 "Primary base should have a zero offset!");
689
690 AddVCallOffsets(BaseSubobject(PrimaryBase, Base.getBaseOffset()),
691 VBaseOffset);
692 }
693
694 // Add the vcall offsets.
Aaron Ballman2b124d12014-03-13 16:36:16 +0000695 for (const auto *MD : RD->methods()) {
Peter Collingbournecfd23562011-09-26 01:57:12 +0000696 if (!MD->isVirtual())
697 continue;
698
699 CharUnits OffsetOffset = getCurrentOffsetOffset();
700
701 // Don't add a vcall offset if we already have one for this member function
702 // signature.
703 if (!VCallOffsets.AddVCallOffset(MD, OffsetOffset))
704 continue;
705
706 CharUnits Offset = CharUnits::Zero();
707
708 if (Overriders) {
709 // Get the final overrider.
710 FinalOverriders::OverriderInfo Overrider =
711 Overriders->getOverrider(MD, Base.getBaseOffset());
712
713 /// The vcall offset is the offset from the virtual base to the object
714 /// where the function was overridden.
715 Offset = Overrider.Offset - VBaseOffset;
716 }
717
718 Components.push_back(
719 VTableComponent::MakeVCallOffset(Offset));
720 }
721
722 // And iterate over all non-virtual bases (ignoring the primary base).
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +0000723 for (const auto &B : RD->bases()) {
724 if (B.isVirtual())
Peter Collingbournecfd23562011-09-26 01:57:12 +0000725 continue;
726
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +0000727 const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +0000728 if (BaseDecl == PrimaryBase)
729 continue;
730
731 // Get the base offset of this base.
732 CharUnits BaseOffset = Base.getBaseOffset() +
733 Layout.getBaseClassOffset(BaseDecl);
734
735 AddVCallOffsets(BaseSubobject(BaseDecl, BaseOffset),
736 VBaseOffset);
737 }
738}
739
740void
741VCallAndVBaseOffsetBuilder::AddVBaseOffsets(const CXXRecordDecl *RD,
742 CharUnits OffsetInLayoutClass) {
743 const ASTRecordLayout &LayoutClassLayout =
744 Context.getASTRecordLayout(LayoutClass);
745
746 // Add vbase offsets.
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +0000747 for (const auto &B : RD->bases()) {
748 const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +0000749
750 // Check if this is a virtual base that we haven't visited before.
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +0000751 if (B.isVirtual() && VisitedVirtualBases.insert(BaseDecl)) {
Peter Collingbournecfd23562011-09-26 01:57:12 +0000752 CharUnits Offset =
753 LayoutClassLayout.getVBaseClassOffset(BaseDecl) - OffsetInLayoutClass;
754
755 // Add the vbase offset offset.
756 assert(!VBaseOffsetOffsets.count(BaseDecl) &&
757 "vbase offset offset already exists!");
758
759 CharUnits VBaseOffsetOffset = getCurrentOffsetOffset();
760 VBaseOffsetOffsets.insert(
761 std::make_pair(BaseDecl, VBaseOffsetOffset));
762
763 Components.push_back(
764 VTableComponent::MakeVBaseOffset(Offset));
765 }
766
767 // Check the base class looking for more vbase offsets.
768 AddVBaseOffsets(BaseDecl, OffsetInLayoutClass);
769 }
770}
771
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +0000772/// ItaniumVTableBuilder - Class for building vtable layout information.
773class ItaniumVTableBuilder {
Peter Collingbournecfd23562011-09-26 01:57:12 +0000774public:
775 /// PrimaryBasesSetVectorTy - A set vector of direct and indirect
776 /// primary bases.
777 typedef llvm::SmallSetVector<const CXXRecordDecl *, 8>
778 PrimaryBasesSetVectorTy;
779
780 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits>
781 VBaseOffsetOffsetsMapTy;
782
783 typedef llvm::DenseMap<BaseSubobject, uint64_t>
784 AddressPointsMapTy;
785
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +0000786 typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVTableIndicesTy;
787
Peter Collingbournecfd23562011-09-26 01:57:12 +0000788private:
789 /// VTables - Global vtable information.
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +0000790 ItaniumVTableContext &VTables;
Peter Collingbournecfd23562011-09-26 01:57:12 +0000791
792 /// MostDerivedClass - The most derived class for which we're building this
793 /// vtable.
794 const CXXRecordDecl *MostDerivedClass;
795
796 /// MostDerivedClassOffset - If we're building a construction vtable, this
797 /// holds the offset from the layout class to the most derived class.
798 const CharUnits MostDerivedClassOffset;
799
800 /// MostDerivedClassIsVirtual - Whether the most derived class is a virtual
801 /// base. (This only makes sense when building a construction vtable).
802 bool MostDerivedClassIsVirtual;
803
804 /// LayoutClass - The class we're using for layout information. Will be
805 /// different than the most derived class if we're building a construction
806 /// vtable.
807 const CXXRecordDecl *LayoutClass;
808
809 /// Context - The ASTContext which we will use for layout information.
810 ASTContext &Context;
811
812 /// FinalOverriders - The final overriders of the most derived class.
813 const FinalOverriders Overriders;
814
815 /// VCallOffsetsForVBases - Keeps track of vcall offsets for the virtual
816 /// bases in this vtable.
817 llvm::DenseMap<const CXXRecordDecl *, VCallOffsetMap> VCallOffsetsForVBases;
818
819 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets for
820 /// the most derived class.
821 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
822
823 /// Components - The components of the vtable being built.
824 SmallVector<VTableComponent, 64> Components;
825
826 /// AddressPoints - Address points for the vtable being built.
827 AddressPointsMapTy AddressPoints;
828
829 /// MethodInfo - Contains information about a method in a vtable.
830 /// (Used for computing 'this' pointer adjustment thunks.
831 struct MethodInfo {
832 /// BaseOffset - The base offset of this method.
833 const CharUnits BaseOffset;
834
835 /// BaseOffsetInLayoutClass - The base offset in the layout class of this
836 /// method.
837 const CharUnits BaseOffsetInLayoutClass;
838
839 /// VTableIndex - The index in the vtable that this method has.
840 /// (For destructors, this is the index of the complete destructor).
841 const uint64_t VTableIndex;
842
843 MethodInfo(CharUnits BaseOffset, CharUnits BaseOffsetInLayoutClass,
844 uint64_t VTableIndex)
845 : BaseOffset(BaseOffset),
846 BaseOffsetInLayoutClass(BaseOffsetInLayoutClass),
847 VTableIndex(VTableIndex) { }
848
849 MethodInfo()
850 : BaseOffset(CharUnits::Zero()),
851 BaseOffsetInLayoutClass(CharUnits::Zero()),
852 VTableIndex(0) { }
853 };
854
855 typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy;
856
857 /// MethodInfoMap - The information for all methods in the vtable we're
858 /// currently building.
859 MethodInfoMapTy MethodInfoMap;
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +0000860
861 /// MethodVTableIndices - Contains the index (relative to the vtable address
862 /// point) where the function pointer for a virtual function is stored.
863 MethodVTableIndicesTy MethodVTableIndices;
864
Peter Collingbournecfd23562011-09-26 01:57:12 +0000865 typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy;
866
867 /// VTableThunks - The thunks by vtable index in the vtable currently being
868 /// built.
869 VTableThunksMapTy VTableThunks;
870
871 typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
872 typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
873
874 /// Thunks - A map that contains all the thunks needed for all methods in the
875 /// most derived class for which the vtable is currently being built.
876 ThunksMapTy Thunks;
877
878 /// AddThunk - Add a thunk for the given method.
879 void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk);
880
881 /// ComputeThisAdjustments - Compute the 'this' pointer adjustments for the
882 /// part of the vtable we're currently building.
883 void ComputeThisAdjustments();
884
885 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
886
887 /// PrimaryVirtualBases - All known virtual bases who are a primary base of
888 /// some other base.
889 VisitedVirtualBasesSetTy PrimaryVirtualBases;
890
891 /// ComputeReturnAdjustment - Compute the return adjustment given a return
892 /// adjustment base offset.
893 ReturnAdjustment ComputeReturnAdjustment(BaseOffset Offset);
894
895 /// ComputeThisAdjustmentBaseOffset - Compute the base offset for adjusting
896 /// the 'this' pointer from the base subobject to the derived subobject.
897 BaseOffset ComputeThisAdjustmentBaseOffset(BaseSubobject Base,
898 BaseSubobject Derived) const;
899
900 /// ComputeThisAdjustment - Compute the 'this' pointer adjustment for the
901 /// given virtual member function, its offset in the layout class and its
902 /// final overrider.
903 ThisAdjustment
904 ComputeThisAdjustment(const CXXMethodDecl *MD,
905 CharUnits BaseOffsetInLayoutClass,
906 FinalOverriders::OverriderInfo Overrider);
907
908 /// AddMethod - Add a single virtual member function to the vtable
909 /// components vector.
910 void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment);
911
912 /// IsOverriderUsed - Returns whether the overrider will ever be used in this
913 /// part of the vtable.
914 ///
915 /// Itanium C++ ABI 2.5.2:
916 ///
917 /// struct A { virtual void f(); };
918 /// struct B : virtual public A { int i; };
919 /// struct C : virtual public A { int j; };
920 /// struct D : public B, public C {};
921 ///
922 /// When B and C are declared, A is a primary base in each case, so although
923 /// vcall offsets are allocated in the A-in-B and A-in-C vtables, no this
924 /// adjustment is required and no thunk is generated. However, inside D
925 /// objects, A is no longer a primary base of C, so if we allowed calls to
926 /// C::f() to use the copy of A's vtable in the C subobject, we would need
927 /// to adjust this from C* to B::A*, which would require a third-party
928 /// thunk. Since we require that a call to C::f() first convert to A*,
929 /// C-in-D's copy of A's vtable is never referenced, so this is not
930 /// necessary.
931 bool IsOverriderUsed(const CXXMethodDecl *Overrider,
932 CharUnits BaseOffsetInLayoutClass,
933 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
934 CharUnits FirstBaseOffsetInLayoutClass) const;
935
936
937 /// AddMethods - Add the methods of this base subobject and all its
938 /// primary bases to the vtable components vector.
939 void AddMethods(BaseSubobject Base, CharUnits BaseOffsetInLayoutClass,
940 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
941 CharUnits FirstBaseOffsetInLayoutClass,
942 PrimaryBasesSetVectorTy &PrimaryBases);
943
944 // LayoutVTable - Layout the vtable for the given base class, including its
945 // secondary vtables and any vtables for virtual bases.
946 void LayoutVTable();
947
948 /// LayoutPrimaryAndSecondaryVTables - Layout the primary vtable for the
949 /// given base subobject, as well as all its secondary vtables.
950 ///
951 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
952 /// or a direct or indirect base of a virtual base.
953 ///
954 /// \param BaseIsVirtualInLayoutClass - Whether the base subobject is virtual
955 /// in the layout class.
956 void LayoutPrimaryAndSecondaryVTables(BaseSubobject Base,
957 bool BaseIsMorallyVirtual,
958 bool BaseIsVirtualInLayoutClass,
959 CharUnits OffsetInLayoutClass);
960
961 /// LayoutSecondaryVTables - Layout the secondary vtables for the given base
962 /// subobject.
963 ///
964 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
965 /// or a direct or indirect base of a virtual base.
966 void LayoutSecondaryVTables(BaseSubobject Base, bool BaseIsMorallyVirtual,
967 CharUnits OffsetInLayoutClass);
968
969 /// DeterminePrimaryVirtualBases - Determine the primary virtual bases in this
970 /// class hierarchy.
971 void DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
972 CharUnits OffsetInLayoutClass,
973 VisitedVirtualBasesSetTy &VBases);
974
975 /// LayoutVTablesForVirtualBases - Layout vtables for all virtual bases of the
976 /// given base (excluding any primary bases).
977 void LayoutVTablesForVirtualBases(const CXXRecordDecl *RD,
978 VisitedVirtualBasesSetTy &VBases);
979
980 /// isBuildingConstructionVTable - Return whether this vtable builder is
981 /// building a construction vtable.
982 bool isBuildingConstructorVTable() const {
983 return MostDerivedClass != LayoutClass;
984 }
985
986public:
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +0000987 ItaniumVTableBuilder(ItaniumVTableContext &VTables,
988 const CXXRecordDecl *MostDerivedClass,
989 CharUnits MostDerivedClassOffset,
990 bool MostDerivedClassIsVirtual,
991 const CXXRecordDecl *LayoutClass)
992 : VTables(VTables), MostDerivedClass(MostDerivedClass),
993 MostDerivedClassOffset(MostDerivedClassOffset),
994 MostDerivedClassIsVirtual(MostDerivedClassIsVirtual),
995 LayoutClass(LayoutClass), Context(MostDerivedClass->getASTContext()),
996 Overriders(MostDerivedClass, MostDerivedClassOffset, LayoutClass) {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +0000997 assert(!Context.getTargetInfo().getCXXABI().isMicrosoft());
Peter Collingbournecfd23562011-09-26 01:57:12 +0000998
999 LayoutVTable();
1000
David Blaikiebbafb8a2012-03-11 07:00:24 +00001001 if (Context.getLangOpts().DumpVTableLayouts)
Reid Kleckner5bc6d0f2013-11-08 21:28:00 +00001002 dumpLayout(llvm::outs());
Peter Collingbournecfd23562011-09-26 01:57:12 +00001003 }
1004
1005 uint64_t getNumThunks() const {
1006 return Thunks.size();
1007 }
1008
1009 ThunksMapTy::const_iterator thunks_begin() const {
1010 return Thunks.begin();
1011 }
1012
1013 ThunksMapTy::const_iterator thunks_end() const {
1014 return Thunks.end();
1015 }
1016
1017 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
1018 return VBaseOffsetOffsets;
1019 }
1020
1021 const AddressPointsMapTy &getAddressPoints() const {
1022 return AddressPoints;
1023 }
1024
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00001025 MethodVTableIndicesTy::const_iterator vtable_indices_begin() const {
1026 return MethodVTableIndices.begin();
1027 }
1028
1029 MethodVTableIndicesTy::const_iterator vtable_indices_end() const {
1030 return MethodVTableIndices.end();
1031 }
1032
Peter Collingbournecfd23562011-09-26 01:57:12 +00001033 /// getNumVTableComponents - Return the number of components in the vtable
1034 /// currently built.
1035 uint64_t getNumVTableComponents() const {
1036 return Components.size();
1037 }
1038
1039 const VTableComponent *vtable_component_begin() const {
1040 return Components.begin();
1041 }
1042
1043 const VTableComponent *vtable_component_end() const {
1044 return Components.end();
1045 }
1046
1047 AddressPointsMapTy::const_iterator address_points_begin() const {
1048 return AddressPoints.begin();
1049 }
1050
1051 AddressPointsMapTy::const_iterator address_points_end() const {
1052 return AddressPoints.end();
1053 }
1054
1055 VTableThunksMapTy::const_iterator vtable_thunks_begin() const {
1056 return VTableThunks.begin();
1057 }
1058
1059 VTableThunksMapTy::const_iterator vtable_thunks_end() const {
1060 return VTableThunks.end();
1061 }
1062
1063 /// dumpLayout - Dump the vtable layout.
1064 void dumpLayout(raw_ostream&);
1065};
1066
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001067void ItaniumVTableBuilder::AddThunk(const CXXMethodDecl *MD,
1068 const ThunkInfo &Thunk) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001069 assert(!isBuildingConstructorVTable() &&
1070 "Can't add thunks for construction vtable");
1071
Craig Topper5603df42013-07-05 19:34:19 +00001072 SmallVectorImpl<ThunkInfo> &ThunksVector = Thunks[MD];
1073
Peter Collingbournecfd23562011-09-26 01:57:12 +00001074 // Check if we have this thunk already.
1075 if (std::find(ThunksVector.begin(), ThunksVector.end(), Thunk) !=
1076 ThunksVector.end())
1077 return;
1078
1079 ThunksVector.push_back(Thunk);
1080}
1081
1082typedef llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverriddenMethodsSetTy;
1083
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001084/// Visit all the methods overridden by the given method recursively,
1085/// in a depth-first pre-order. The Visitor's visitor method returns a bool
1086/// indicating whether to continue the recursion for the given overridden
1087/// method (i.e. returning false stops the iteration).
1088template <class VisitorTy>
1089static void
1090visitAllOverriddenMethods(const CXXMethodDecl *MD, VisitorTy &Visitor) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001091 assert(MD->isVirtual() && "Method is not virtual!");
1092
1093 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
1094 E = MD->end_overridden_methods(); I != E; ++I) {
1095 const CXXMethodDecl *OverriddenMD = *I;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001096 if (!Visitor.visit(OverriddenMD))
1097 continue;
1098 visitAllOverriddenMethods(OverriddenMD, Visitor);
Peter Collingbournecfd23562011-09-26 01:57:12 +00001099 }
1100}
1101
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001102namespace {
1103 struct OverriddenMethodsCollector {
1104 OverriddenMethodsSetTy *Methods;
1105
1106 bool visit(const CXXMethodDecl *MD) {
1107 // Don't recurse on this method if we've already collected it.
1108 return Methods->insert(MD);
1109 }
1110 };
1111}
1112
1113/// ComputeAllOverriddenMethods - Given a method decl, will return a set of all
1114/// the overridden methods that the function decl overrides.
1115static void
1116ComputeAllOverriddenMethods(const CXXMethodDecl *MD,
1117 OverriddenMethodsSetTy& OverriddenMethods) {
1118 OverriddenMethodsCollector Collector = { &OverriddenMethods };
1119 visitAllOverriddenMethods(MD, Collector);
1120}
1121
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001122void ItaniumVTableBuilder::ComputeThisAdjustments() {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001123 // Now go through the method info map and see if any of the methods need
1124 // 'this' pointer adjustments.
1125 for (MethodInfoMapTy::const_iterator I = MethodInfoMap.begin(),
1126 E = MethodInfoMap.end(); I != E; ++I) {
1127 const CXXMethodDecl *MD = I->first;
1128 const MethodInfo &MethodInfo = I->second;
1129
1130 // Ignore adjustments for unused function pointers.
1131 uint64_t VTableIndex = MethodInfo.VTableIndex;
1132 if (Components[VTableIndex].getKind() ==
1133 VTableComponent::CK_UnusedFunctionPointer)
1134 continue;
1135
1136 // Get the final overrider for this method.
1137 FinalOverriders::OverriderInfo Overrider =
1138 Overriders.getOverrider(MD, MethodInfo.BaseOffset);
1139
1140 // Check if we need an adjustment at all.
1141 if (MethodInfo.BaseOffsetInLayoutClass == Overrider.Offset) {
1142 // When a return thunk is needed by a derived class that overrides a
1143 // virtual base, gcc uses a virtual 'this' adjustment as well.
1144 // While the thunk itself might be needed by vtables in subclasses or
1145 // in construction vtables, there doesn't seem to be a reason for using
1146 // the thunk in this vtable. Still, we do so to match gcc.
1147 if (VTableThunks.lookup(VTableIndex).Return.isEmpty())
1148 continue;
1149 }
1150
1151 ThisAdjustment ThisAdjustment =
1152 ComputeThisAdjustment(MD, MethodInfo.BaseOffsetInLayoutClass, Overrider);
1153
1154 if (ThisAdjustment.isEmpty())
1155 continue;
1156
1157 // Add it.
1158 VTableThunks[VTableIndex].This = ThisAdjustment;
1159
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001160 if (isa<CXXDestructorDecl>(MD)) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001161 // Add an adjustment for the deleting destructor as well.
1162 VTableThunks[VTableIndex + 1].This = ThisAdjustment;
1163 }
1164 }
1165
1166 /// Clear the method info map.
1167 MethodInfoMap.clear();
1168
1169 if (isBuildingConstructorVTable()) {
1170 // We don't need to store thunk information for construction vtables.
1171 return;
1172 }
1173
1174 for (VTableThunksMapTy::const_iterator I = VTableThunks.begin(),
1175 E = VTableThunks.end(); I != E; ++I) {
1176 const VTableComponent &Component = Components[I->first];
1177 const ThunkInfo &Thunk = I->second;
1178 const CXXMethodDecl *MD;
1179
1180 switch (Component.getKind()) {
1181 default:
1182 llvm_unreachable("Unexpected vtable component kind!");
1183 case VTableComponent::CK_FunctionPointer:
1184 MD = Component.getFunctionDecl();
1185 break;
1186 case VTableComponent::CK_CompleteDtorPointer:
1187 MD = Component.getDestructorDecl();
1188 break;
1189 case VTableComponent::CK_DeletingDtorPointer:
1190 // We've already added the thunk when we saw the complete dtor pointer.
1191 continue;
1192 }
1193
1194 if (MD->getParent() == MostDerivedClass)
1195 AddThunk(MD, Thunk);
1196 }
1197}
1198
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001199ReturnAdjustment
1200ItaniumVTableBuilder::ComputeReturnAdjustment(BaseOffset Offset) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001201 ReturnAdjustment Adjustment;
1202
1203 if (!Offset.isEmpty()) {
1204 if (Offset.VirtualBase) {
1205 // Get the virtual base offset offset.
1206 if (Offset.DerivedClass == MostDerivedClass) {
1207 // We can get the offset offset directly from our map.
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001208 Adjustment.Virtual.Itanium.VBaseOffsetOffset =
Peter Collingbournecfd23562011-09-26 01:57:12 +00001209 VBaseOffsetOffsets.lookup(Offset.VirtualBase).getQuantity();
1210 } else {
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001211 Adjustment.Virtual.Itanium.VBaseOffsetOffset =
Peter Collingbournecfd23562011-09-26 01:57:12 +00001212 VTables.getVirtualBaseOffsetOffset(Offset.DerivedClass,
1213 Offset.VirtualBase).getQuantity();
1214 }
1215 }
1216
1217 Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity();
1218 }
1219
1220 return Adjustment;
1221}
1222
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001223BaseOffset ItaniumVTableBuilder::ComputeThisAdjustmentBaseOffset(
1224 BaseSubobject Base, BaseSubobject Derived) const {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001225 const CXXRecordDecl *BaseRD = Base.getBase();
1226 const CXXRecordDecl *DerivedRD = Derived.getBase();
1227
1228 CXXBasePaths Paths(/*FindAmbiguities=*/true,
1229 /*RecordPaths=*/true, /*DetectVirtual=*/true);
1230
Benjamin Kramer325d7452013-02-03 18:55:34 +00001231 if (!DerivedRD->isDerivedFrom(BaseRD, Paths))
Peter Collingbournecfd23562011-09-26 01:57:12 +00001232 llvm_unreachable("Class must be derived from the passed in base class!");
Peter Collingbournecfd23562011-09-26 01:57:12 +00001233
1234 // We have to go through all the paths, and see which one leads us to the
1235 // right base subobject.
1236 for (CXXBasePaths::const_paths_iterator I = Paths.begin(), E = Paths.end();
1237 I != E; ++I) {
1238 BaseOffset Offset = ComputeBaseOffset(Context, DerivedRD, *I);
1239
1240 CharUnits OffsetToBaseSubobject = Offset.NonVirtualOffset;
1241
1242 if (Offset.VirtualBase) {
1243 // If we have a virtual base class, the non-virtual offset is relative
1244 // to the virtual base class offset.
1245 const ASTRecordLayout &LayoutClassLayout =
1246 Context.getASTRecordLayout(LayoutClass);
1247
1248 /// Get the virtual base offset, relative to the most derived class
1249 /// layout.
1250 OffsetToBaseSubobject +=
1251 LayoutClassLayout.getVBaseClassOffset(Offset.VirtualBase);
1252 } else {
1253 // Otherwise, the non-virtual offset is relative to the derived class
1254 // offset.
1255 OffsetToBaseSubobject += Derived.getBaseOffset();
1256 }
1257
1258 // Check if this path gives us the right base subobject.
1259 if (OffsetToBaseSubobject == Base.getBaseOffset()) {
1260 // Since we're going from the base class _to_ the derived class, we'll
1261 // invert the non-virtual offset here.
1262 Offset.NonVirtualOffset = -Offset.NonVirtualOffset;
1263 return Offset;
1264 }
1265 }
1266
1267 return BaseOffset();
1268}
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001269
1270ThisAdjustment ItaniumVTableBuilder::ComputeThisAdjustment(
1271 const CXXMethodDecl *MD, CharUnits BaseOffsetInLayoutClass,
1272 FinalOverriders::OverriderInfo Overrider) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001273 // Ignore adjustments for pure virtual member functions.
1274 if (Overrider.Method->isPure())
1275 return ThisAdjustment();
1276
1277 BaseSubobject OverriddenBaseSubobject(MD->getParent(),
1278 BaseOffsetInLayoutClass);
1279
1280 BaseSubobject OverriderBaseSubobject(Overrider.Method->getParent(),
1281 Overrider.Offset);
1282
1283 // Compute the adjustment offset.
1284 BaseOffset Offset = ComputeThisAdjustmentBaseOffset(OverriddenBaseSubobject,
1285 OverriderBaseSubobject);
1286 if (Offset.isEmpty())
1287 return ThisAdjustment();
1288
1289 ThisAdjustment Adjustment;
1290
1291 if (Offset.VirtualBase) {
1292 // Get the vcall offset map for this virtual base.
1293 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Offset.VirtualBase];
1294
1295 if (VCallOffsets.empty()) {
1296 // We don't have vcall offsets for this virtual base, go ahead and
1297 // build them.
1298 VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, MostDerivedClass,
Craig Topper36250ad2014-05-12 05:36:57 +00001299 /*FinalOverriders=*/nullptr,
Peter Collingbournecfd23562011-09-26 01:57:12 +00001300 BaseSubobject(Offset.VirtualBase,
1301 CharUnits::Zero()),
1302 /*BaseIsVirtual=*/true,
1303 /*OffsetInLayoutClass=*/
1304 CharUnits::Zero());
1305
1306 VCallOffsets = Builder.getVCallOffsets();
1307 }
1308
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00001309 Adjustment.Virtual.Itanium.VCallOffsetOffset =
Peter Collingbournecfd23562011-09-26 01:57:12 +00001310 VCallOffsets.getVCallOffsetOffset(MD).getQuantity();
1311 }
1312
1313 // Set the non-virtual part of the adjustment.
1314 Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity();
1315
1316 return Adjustment;
1317}
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001318
1319void ItaniumVTableBuilder::AddMethod(const CXXMethodDecl *MD,
1320 ReturnAdjustment ReturnAdjustment) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001321 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1322 assert(ReturnAdjustment.isEmpty() &&
1323 "Destructor can't have return adjustment!");
1324
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001325 // Add both the complete destructor and the deleting destructor.
1326 Components.push_back(VTableComponent::MakeCompleteDtor(DD));
1327 Components.push_back(VTableComponent::MakeDeletingDtor(DD));
Peter Collingbournecfd23562011-09-26 01:57:12 +00001328 } else {
1329 // Add the return adjustment if necessary.
1330 if (!ReturnAdjustment.isEmpty())
1331 VTableThunks[Components.size()].Return = ReturnAdjustment;
1332
1333 // Add the function.
1334 Components.push_back(VTableComponent::MakeFunction(MD));
1335 }
1336}
1337
1338/// OverridesIndirectMethodInBase - Return whether the given member function
1339/// overrides any methods in the set of given bases.
1340/// Unlike OverridesMethodInBase, this checks "overriders of overriders".
1341/// For example, if we have:
1342///
1343/// struct A { virtual void f(); }
1344/// struct B : A { virtual void f(); }
1345/// struct C : B { virtual void f(); }
1346///
1347/// OverridesIndirectMethodInBase will return true if given C::f as the method
1348/// and { A } as the set of bases.
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001349static bool OverridesIndirectMethodInBases(
1350 const CXXMethodDecl *MD,
1351 ItaniumVTableBuilder::PrimaryBasesSetVectorTy &Bases) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001352 if (Bases.count(MD->getParent()))
1353 return true;
1354
1355 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
1356 E = MD->end_overridden_methods(); I != E; ++I) {
1357 const CXXMethodDecl *OverriddenMD = *I;
1358
1359 // Check "indirect overriders".
1360 if (OverridesIndirectMethodInBases(OverriddenMD, Bases))
1361 return true;
1362 }
1363
1364 return false;
1365}
1366
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001367bool ItaniumVTableBuilder::IsOverriderUsed(
1368 const CXXMethodDecl *Overrider, CharUnits BaseOffsetInLayoutClass,
1369 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
1370 CharUnits FirstBaseOffsetInLayoutClass) const {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001371 // If the base and the first base in the primary base chain have the same
1372 // offsets, then this overrider will be used.
1373 if (BaseOffsetInLayoutClass == FirstBaseOffsetInLayoutClass)
1374 return true;
1375
1376 // We know now that Base (or a direct or indirect base of it) is a primary
1377 // base in part of the class hierarchy, but not a primary base in the most
1378 // derived class.
1379
1380 // If the overrider is the first base in the primary base chain, we know
1381 // that the overrider will be used.
1382 if (Overrider->getParent() == FirstBaseInPrimaryBaseChain)
1383 return true;
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001384
1385 ItaniumVTableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
Peter Collingbournecfd23562011-09-26 01:57:12 +00001386
1387 const CXXRecordDecl *RD = FirstBaseInPrimaryBaseChain;
1388 PrimaryBases.insert(RD);
1389
1390 // Now traverse the base chain, starting with the first base, until we find
1391 // the base that is no longer a primary base.
1392 while (true) {
1393 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1394 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1395
1396 if (!PrimaryBase)
1397 break;
1398
1399 if (Layout.isPrimaryBaseVirtual()) {
Benjamin Kramer2ef30312012-07-04 18:45:14 +00001400 assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() &&
Peter Collingbournecfd23562011-09-26 01:57:12 +00001401 "Primary base should always be at offset 0!");
1402
1403 const ASTRecordLayout &LayoutClassLayout =
1404 Context.getASTRecordLayout(LayoutClass);
1405
1406 // Now check if this is the primary base that is not a primary base in the
1407 // most derived class.
1408 if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
1409 FirstBaseOffsetInLayoutClass) {
1410 // We found it, stop walking the chain.
1411 break;
1412 }
1413 } else {
Benjamin Kramer2ef30312012-07-04 18:45:14 +00001414 assert(Layout.getBaseClassOffset(PrimaryBase).isZero() &&
Peter Collingbournecfd23562011-09-26 01:57:12 +00001415 "Primary base should always be at offset 0!");
1416 }
1417
1418 if (!PrimaryBases.insert(PrimaryBase))
1419 llvm_unreachable("Found a duplicate primary base!");
1420
1421 RD = PrimaryBase;
1422 }
1423
1424 // If the final overrider is an override of one of the primary bases,
1425 // then we know that it will be used.
1426 return OverridesIndirectMethodInBases(Overrider, PrimaryBases);
1427}
1428
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001429typedef llvm::SmallSetVector<const CXXRecordDecl *, 8> BasesSetVectorTy;
1430
Peter Collingbournecfd23562011-09-26 01:57:12 +00001431/// FindNearestOverriddenMethod - Given a method, returns the overridden method
1432/// from the nearest base. Returns null if no method was found.
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001433/// The Bases are expected to be sorted in a base-to-derived order.
1434static const CXXMethodDecl *
Peter Collingbournecfd23562011-09-26 01:57:12 +00001435FindNearestOverriddenMethod(const CXXMethodDecl *MD,
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001436 BasesSetVectorTy &Bases) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001437 OverriddenMethodsSetTy OverriddenMethods;
1438 ComputeAllOverriddenMethods(MD, OverriddenMethods);
1439
1440 for (int I = Bases.size(), E = 0; I != E; --I) {
1441 const CXXRecordDecl *PrimaryBase = Bases[I - 1];
1442
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00001443 // Now check the overridden methods.
Peter Collingbournecfd23562011-09-26 01:57:12 +00001444 for (OverriddenMethodsSetTy::const_iterator I = OverriddenMethods.begin(),
1445 E = OverriddenMethods.end(); I != E; ++I) {
1446 const CXXMethodDecl *OverriddenMD = *I;
1447
1448 // We found our overridden method.
1449 if (OverriddenMD->getParent() == PrimaryBase)
1450 return OverriddenMD;
1451 }
1452 }
Craig Topper36250ad2014-05-12 05:36:57 +00001453
1454 return nullptr;
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001455}
Peter Collingbournecfd23562011-09-26 01:57:12 +00001456
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001457void ItaniumVTableBuilder::AddMethods(
1458 BaseSubobject Base, CharUnits BaseOffsetInLayoutClass,
1459 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
1460 CharUnits FirstBaseOffsetInLayoutClass,
1461 PrimaryBasesSetVectorTy &PrimaryBases) {
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00001462 // Itanium C++ ABI 2.5.2:
1463 // The order of the virtual function pointers in a virtual table is the
1464 // order of declaration of the corresponding member functions in the class.
1465 //
1466 // There is an entry for any virtual function declared in a class,
1467 // whether it is a new function or overrides a base class function,
1468 // unless it overrides a function from the primary base, and conversion
1469 // between their return types does not require an adjustment.
1470
Peter Collingbournecfd23562011-09-26 01:57:12 +00001471 const CXXRecordDecl *RD = Base.getBase();
1472 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1473
1474 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
1475 CharUnits PrimaryBaseOffset;
1476 CharUnits PrimaryBaseOffsetInLayoutClass;
1477 if (Layout.isPrimaryBaseVirtual()) {
Benjamin Kramer2ef30312012-07-04 18:45:14 +00001478 assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() &&
Peter Collingbournecfd23562011-09-26 01:57:12 +00001479 "Primary vbase should have a zero offset!");
1480
1481 const ASTRecordLayout &MostDerivedClassLayout =
1482 Context.getASTRecordLayout(MostDerivedClass);
1483
1484 PrimaryBaseOffset =
1485 MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
1486
1487 const ASTRecordLayout &LayoutClassLayout =
1488 Context.getASTRecordLayout(LayoutClass);
1489
1490 PrimaryBaseOffsetInLayoutClass =
1491 LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
1492 } else {
Benjamin Kramer2ef30312012-07-04 18:45:14 +00001493 assert(Layout.getBaseClassOffset(PrimaryBase).isZero() &&
Peter Collingbournecfd23562011-09-26 01:57:12 +00001494 "Primary base should have a zero offset!");
1495
1496 PrimaryBaseOffset = Base.getBaseOffset();
1497 PrimaryBaseOffsetInLayoutClass = BaseOffsetInLayoutClass;
1498 }
1499
1500 AddMethods(BaseSubobject(PrimaryBase, PrimaryBaseOffset),
1501 PrimaryBaseOffsetInLayoutClass, FirstBaseInPrimaryBaseChain,
1502 FirstBaseOffsetInLayoutClass, PrimaryBases);
1503
1504 if (!PrimaryBases.insert(PrimaryBase))
1505 llvm_unreachable("Found a duplicate primary base!");
1506 }
1507
Craig Topper36250ad2014-05-12 05:36:57 +00001508 const CXXDestructorDecl *ImplicitVirtualDtor = nullptr;
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00001509
1510 typedef llvm::SmallVector<const CXXMethodDecl *, 8> NewVirtualFunctionsTy;
1511 NewVirtualFunctionsTy NewVirtualFunctions;
1512
Peter Collingbournecfd23562011-09-26 01:57:12 +00001513 // Now go through all virtual member functions and add them.
Aaron Ballman2b124d12014-03-13 16:36:16 +00001514 for (const auto *MD : RD->methods()) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001515 if (!MD->isVirtual())
1516 continue;
1517
1518 // Get the final overrider.
1519 FinalOverriders::OverriderInfo Overrider =
1520 Overriders.getOverrider(MD, Base.getBaseOffset());
1521
1522 // Check if this virtual member function overrides a method in a primary
1523 // base. If this is the case, and the return type doesn't require adjustment
1524 // then we can just use the member function from the primary base.
1525 if (const CXXMethodDecl *OverriddenMD =
1526 FindNearestOverriddenMethod(MD, PrimaryBases)) {
1527 if (ComputeReturnAdjustmentBaseOffset(Context, MD,
1528 OverriddenMD).isEmpty()) {
1529 // Replace the method info of the overridden method with our own
1530 // method.
1531 assert(MethodInfoMap.count(OverriddenMD) &&
1532 "Did not find the overridden method!");
1533 MethodInfo &OverriddenMethodInfo = MethodInfoMap[OverriddenMD];
1534
1535 MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
1536 OverriddenMethodInfo.VTableIndex);
1537
1538 assert(!MethodInfoMap.count(MD) &&
1539 "Should not have method info for this method yet!");
1540
1541 MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
1542 MethodInfoMap.erase(OverriddenMD);
1543
1544 // If the overridden method exists in a virtual base class or a direct
1545 // or indirect base class of a virtual base class, we need to emit a
1546 // thunk if we ever have a class hierarchy where the base class is not
1547 // a primary base in the complete object.
1548 if (!isBuildingConstructorVTable() && OverriddenMD != MD) {
1549 // Compute the this adjustment.
1550 ThisAdjustment ThisAdjustment =
1551 ComputeThisAdjustment(OverriddenMD, BaseOffsetInLayoutClass,
1552 Overrider);
1553
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00001554 if (ThisAdjustment.Virtual.Itanium.VCallOffsetOffset &&
Peter Collingbournecfd23562011-09-26 01:57:12 +00001555 Overrider.Method->getParent() == MostDerivedClass) {
1556
1557 // There's no return adjustment from OverriddenMD and MD,
1558 // but that doesn't mean there isn't one between MD and
1559 // the final overrider.
1560 BaseOffset ReturnAdjustmentOffset =
1561 ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD);
1562 ReturnAdjustment ReturnAdjustment =
1563 ComputeReturnAdjustment(ReturnAdjustmentOffset);
1564
1565 // This is a virtual thunk for the most derived class, add it.
1566 AddThunk(Overrider.Method,
1567 ThunkInfo(ThisAdjustment, ReturnAdjustment));
1568 }
1569 }
1570
1571 continue;
1572 }
1573 }
1574
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00001575 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1576 if (MD->isImplicit()) {
1577 // Itanium C++ ABI 2.5.2:
1578 // If a class has an implicitly-defined virtual destructor,
1579 // its entries come after the declared virtual function pointers.
1580
1581 assert(!ImplicitVirtualDtor &&
1582 "Did already see an implicit virtual dtor!");
1583 ImplicitVirtualDtor = DD;
1584 continue;
1585 }
1586 }
1587
1588 NewVirtualFunctions.push_back(MD);
1589 }
1590
1591 if (ImplicitVirtualDtor)
1592 NewVirtualFunctions.push_back(ImplicitVirtualDtor);
1593
1594 for (NewVirtualFunctionsTy::const_iterator I = NewVirtualFunctions.begin(),
1595 E = NewVirtualFunctions.end(); I != E; ++I) {
1596 const CXXMethodDecl *MD = *I;
1597
1598 // Get the final overrider.
1599 FinalOverriders::OverriderInfo Overrider =
1600 Overriders.getOverrider(MD, Base.getBaseOffset());
1601
Peter Collingbournecfd23562011-09-26 01:57:12 +00001602 // Insert the method info for this method.
1603 MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
1604 Components.size());
1605
1606 assert(!MethodInfoMap.count(MD) &&
1607 "Should not have method info for this method yet!");
1608 MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
1609
1610 // Check if this overrider is going to be used.
1611 const CXXMethodDecl *OverriderMD = Overrider.Method;
1612 if (!IsOverriderUsed(OverriderMD, BaseOffsetInLayoutClass,
1613 FirstBaseInPrimaryBaseChain,
1614 FirstBaseOffsetInLayoutClass)) {
1615 Components.push_back(VTableComponent::MakeUnusedFunction(OverriderMD));
1616 continue;
1617 }
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00001618
Peter Collingbournecfd23562011-09-26 01:57:12 +00001619 // Check if this overrider needs a return adjustment.
1620 // We don't want to do this for pure virtual member functions.
1621 BaseOffset ReturnAdjustmentOffset;
1622 if (!OverriderMD->isPure()) {
1623 ReturnAdjustmentOffset =
1624 ComputeReturnAdjustmentBaseOffset(Context, OverriderMD, MD);
1625 }
1626
1627 ReturnAdjustment ReturnAdjustment =
1628 ComputeReturnAdjustment(ReturnAdjustmentOffset);
1629
1630 AddMethod(Overrider.Method, ReturnAdjustment);
1631 }
1632}
1633
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001634void ItaniumVTableBuilder::LayoutVTable() {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001635 LayoutPrimaryAndSecondaryVTables(BaseSubobject(MostDerivedClass,
1636 CharUnits::Zero()),
1637 /*BaseIsMorallyVirtual=*/false,
1638 MostDerivedClassIsVirtual,
1639 MostDerivedClassOffset);
1640
1641 VisitedVirtualBasesSetTy VBases;
1642
1643 // Determine the primary virtual bases.
1644 DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset,
1645 VBases);
1646 VBases.clear();
1647
1648 LayoutVTablesForVirtualBases(MostDerivedClass, VBases);
1649
1650 // -fapple-kext adds an extra entry at end of vtbl.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001651 bool IsAppleKext = Context.getLangOpts().AppleKext;
Peter Collingbournecfd23562011-09-26 01:57:12 +00001652 if (IsAppleKext)
1653 Components.push_back(VTableComponent::MakeVCallOffset(CharUnits::Zero()));
1654}
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001655
1656void ItaniumVTableBuilder::LayoutPrimaryAndSecondaryVTables(
1657 BaseSubobject Base, bool BaseIsMorallyVirtual,
1658 bool BaseIsVirtualInLayoutClass, CharUnits OffsetInLayoutClass) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001659 assert(Base.getBase()->isDynamicClass() && "class does not have a vtable!");
1660
1661 // Add vcall and vbase offsets for this vtable.
1662 VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, LayoutClass, &Overriders,
1663 Base, BaseIsVirtualInLayoutClass,
1664 OffsetInLayoutClass);
1665 Components.append(Builder.components_begin(), Builder.components_end());
1666
1667 // Check if we need to add these vcall offsets.
1668 if (BaseIsVirtualInLayoutClass && !Builder.getVCallOffsets().empty()) {
1669 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()];
1670
1671 if (VCallOffsets.empty())
1672 VCallOffsets = Builder.getVCallOffsets();
1673 }
1674
1675 // If we're laying out the most derived class we want to keep track of the
1676 // virtual base class offset offsets.
1677 if (Base.getBase() == MostDerivedClass)
1678 VBaseOffsetOffsets = Builder.getVBaseOffsetOffsets();
1679
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001680 // Add the offset to top.
1681 CharUnits OffsetToTop = MostDerivedClassOffset - OffsetInLayoutClass;
1682 Components.push_back(VTableComponent::MakeOffsetToTop(OffsetToTop));
Timur Iskhodzhanov52b8a052013-01-21 13:02:41 +00001683
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001684 // Next, add the RTTI.
1685 Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass));
Timur Iskhodzhanov52b8a052013-01-21 13:02:41 +00001686
Peter Collingbournecfd23562011-09-26 01:57:12 +00001687 uint64_t AddressPoint = Components.size();
1688
1689 // Now go through all virtual member functions and add them.
1690 PrimaryBasesSetVectorTy PrimaryBases;
1691 AddMethods(Base, OffsetInLayoutClass,
1692 Base.getBase(), OffsetInLayoutClass,
1693 PrimaryBases);
1694
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00001695 const CXXRecordDecl *RD = Base.getBase();
1696 if (RD == MostDerivedClass) {
1697 assert(MethodVTableIndices.empty());
1698 for (MethodInfoMapTy::const_iterator I = MethodInfoMap.begin(),
1699 E = MethodInfoMap.end(); I != E; ++I) {
1700 const CXXMethodDecl *MD = I->first;
1701 const MethodInfo &MI = I->second;
1702 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00001703 MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)]
1704 = MI.VTableIndex - AddressPoint;
1705 MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)]
1706 = MI.VTableIndex + 1 - AddressPoint;
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00001707 } else {
1708 MethodVTableIndices[MD] = MI.VTableIndex - AddressPoint;
1709 }
1710 }
1711 }
1712
Peter Collingbournecfd23562011-09-26 01:57:12 +00001713 // Compute 'this' pointer adjustments.
1714 ComputeThisAdjustments();
1715
1716 // Add all address points.
Peter Collingbournecfd23562011-09-26 01:57:12 +00001717 while (true) {
1718 AddressPoints.insert(std::make_pair(
1719 BaseSubobject(RD, OffsetInLayoutClass),
1720 AddressPoint));
1721
1722 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1723 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1724
1725 if (!PrimaryBase)
1726 break;
1727
1728 if (Layout.isPrimaryBaseVirtual()) {
1729 // Check if this virtual primary base is a primary base in the layout
1730 // class. If it's not, we don't want to add it.
1731 const ASTRecordLayout &LayoutClassLayout =
1732 Context.getASTRecordLayout(LayoutClass);
1733
1734 if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
1735 OffsetInLayoutClass) {
1736 // We don't want to add this class (or any of its primary bases).
1737 break;
1738 }
1739 }
1740
1741 RD = PrimaryBase;
1742 }
1743
1744 // Layout secondary vtables.
1745 LayoutSecondaryVTables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass);
1746}
1747
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001748void
1749ItaniumVTableBuilder::LayoutSecondaryVTables(BaseSubobject Base,
1750 bool BaseIsMorallyVirtual,
1751 CharUnits OffsetInLayoutClass) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001752 // Itanium C++ ABI 2.5.2:
1753 // Following the primary virtual table of a derived class are secondary
1754 // virtual tables for each of its proper base classes, except any primary
1755 // base(s) with which it shares its primary virtual table.
1756
1757 const CXXRecordDecl *RD = Base.getBase();
1758 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1759 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1760
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00001761 for (const auto &B : RD->bases()) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001762 // Ignore virtual bases, we'll emit them later.
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00001763 if (B.isVirtual())
Peter Collingbournecfd23562011-09-26 01:57:12 +00001764 continue;
1765
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00001766 const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +00001767
1768 // Ignore bases that don't have a vtable.
1769 if (!BaseDecl->isDynamicClass())
1770 continue;
1771
1772 if (isBuildingConstructorVTable()) {
1773 // Itanium C++ ABI 2.6.4:
1774 // Some of the base class subobjects may not need construction virtual
1775 // tables, which will therefore not be present in the construction
1776 // virtual table group, even though the subobject virtual tables are
1777 // present in the main virtual table group for the complete object.
1778 if (!BaseIsMorallyVirtual && !BaseDecl->getNumVBases())
1779 continue;
1780 }
1781
1782 // Get the base offset of this base.
1783 CharUnits RelativeBaseOffset = Layout.getBaseClassOffset(BaseDecl);
1784 CharUnits BaseOffset = Base.getBaseOffset() + RelativeBaseOffset;
1785
1786 CharUnits BaseOffsetInLayoutClass =
1787 OffsetInLayoutClass + RelativeBaseOffset;
1788
1789 // Don't emit a secondary vtable for a primary base. We might however want
1790 // to emit secondary vtables for other bases of this base.
1791 if (BaseDecl == PrimaryBase) {
1792 LayoutSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset),
1793 BaseIsMorallyVirtual, BaseOffsetInLayoutClass);
1794 continue;
1795 }
1796
1797 // Layout the primary vtable (and any secondary vtables) for this base.
1798 LayoutPrimaryAndSecondaryVTables(
1799 BaseSubobject(BaseDecl, BaseOffset),
1800 BaseIsMorallyVirtual,
1801 /*BaseIsVirtualInLayoutClass=*/false,
1802 BaseOffsetInLayoutClass);
1803 }
1804}
1805
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001806void ItaniumVTableBuilder::DeterminePrimaryVirtualBases(
1807 const CXXRecordDecl *RD, CharUnits OffsetInLayoutClass,
1808 VisitedVirtualBasesSetTy &VBases) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001809 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1810
1811 // Check if this base has a primary base.
1812 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
1813
1814 // Check if it's virtual.
1815 if (Layout.isPrimaryBaseVirtual()) {
1816 bool IsPrimaryVirtualBase = true;
1817
1818 if (isBuildingConstructorVTable()) {
1819 // Check if the base is actually a primary base in the class we use for
1820 // layout.
1821 const ASTRecordLayout &LayoutClassLayout =
1822 Context.getASTRecordLayout(LayoutClass);
1823
1824 CharUnits PrimaryBaseOffsetInLayoutClass =
1825 LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
1826
1827 // We know that the base is not a primary base in the layout class if
1828 // the base offsets are different.
1829 if (PrimaryBaseOffsetInLayoutClass != OffsetInLayoutClass)
1830 IsPrimaryVirtualBase = false;
1831 }
1832
1833 if (IsPrimaryVirtualBase)
1834 PrimaryVirtualBases.insert(PrimaryBase);
1835 }
1836 }
1837
1838 // Traverse bases, looking for more primary virtual bases.
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00001839 for (const auto &B : RD->bases()) {
1840 const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +00001841
1842 CharUnits BaseOffsetInLayoutClass;
1843
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00001844 if (B.isVirtual()) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001845 if (!VBases.insert(BaseDecl))
1846 continue;
1847
1848 const ASTRecordLayout &LayoutClassLayout =
1849 Context.getASTRecordLayout(LayoutClass);
1850
1851 BaseOffsetInLayoutClass =
1852 LayoutClassLayout.getVBaseClassOffset(BaseDecl);
1853 } else {
1854 BaseOffsetInLayoutClass =
1855 OffsetInLayoutClass + Layout.getBaseClassOffset(BaseDecl);
1856 }
1857
1858 DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases);
1859 }
1860}
1861
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001862void ItaniumVTableBuilder::LayoutVTablesForVirtualBases(
1863 const CXXRecordDecl *RD, VisitedVirtualBasesSetTy &VBases) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00001864 // Itanium C++ ABI 2.5.2:
1865 // Then come the virtual base virtual tables, also in inheritance graph
1866 // order, and again excluding primary bases (which share virtual tables with
1867 // the classes for which they are primary).
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00001868 for (const auto &B : RD->bases()) {
1869 const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +00001870
1871 // Check if this base needs a vtable. (If it's virtual, not a primary base
1872 // of some other class, and we haven't visited it before).
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00001873 if (B.isVirtual() && BaseDecl->isDynamicClass() &&
Peter Collingbournecfd23562011-09-26 01:57:12 +00001874 !PrimaryVirtualBases.count(BaseDecl) && VBases.insert(BaseDecl)) {
1875 const ASTRecordLayout &MostDerivedClassLayout =
1876 Context.getASTRecordLayout(MostDerivedClass);
1877 CharUnits BaseOffset =
1878 MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
1879
1880 const ASTRecordLayout &LayoutClassLayout =
1881 Context.getASTRecordLayout(LayoutClass);
1882 CharUnits BaseOffsetInLayoutClass =
1883 LayoutClassLayout.getVBaseClassOffset(BaseDecl);
1884
1885 LayoutPrimaryAndSecondaryVTables(
1886 BaseSubobject(BaseDecl, BaseOffset),
1887 /*BaseIsMorallyVirtual=*/true,
1888 /*BaseIsVirtualInLayoutClass=*/true,
1889 BaseOffsetInLayoutClass);
1890 }
1891
1892 // We only need to check the base for virtual base vtables if it actually
1893 // has virtual bases.
1894 if (BaseDecl->getNumVBases())
1895 LayoutVTablesForVirtualBases(BaseDecl, VBases);
1896 }
1897}
1898
1899/// dumpLayout - Dump the vtable layout.
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001900void ItaniumVTableBuilder::dumpLayout(raw_ostream &Out) {
Timur Iskhodzhanov11510312013-06-28 15:42:28 +00001901 // FIXME: write more tests that actually use the dumpLayout output to prevent
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00001902 // ItaniumVTableBuilder regressions.
Peter Collingbournecfd23562011-09-26 01:57:12 +00001903
1904 if (isBuildingConstructorVTable()) {
1905 Out << "Construction vtable for ('";
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00001906 MostDerivedClass->printQualifiedName(Out);
1907 Out << "', ";
Peter Collingbournecfd23562011-09-26 01:57:12 +00001908 Out << MostDerivedClassOffset.getQuantity() << ") in '";
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00001909 LayoutClass->printQualifiedName(Out);
Peter Collingbournecfd23562011-09-26 01:57:12 +00001910 } else {
1911 Out << "Vtable for '";
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00001912 MostDerivedClass->printQualifiedName(Out);
Peter Collingbournecfd23562011-09-26 01:57:12 +00001913 }
1914 Out << "' (" << Components.size() << " entries).\n";
1915
1916 // Iterate through the address points and insert them into a new map where
1917 // they are keyed by the index and not the base object.
1918 // Since an address point can be shared by multiple subobjects, we use an
1919 // STL multimap.
1920 std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex;
1921 for (AddressPointsMapTy::const_iterator I = AddressPoints.begin(),
1922 E = AddressPoints.end(); I != E; ++I) {
1923 const BaseSubobject& Base = I->first;
1924 uint64_t Index = I->second;
1925
1926 AddressPointsByIndex.insert(std::make_pair(Index, Base));
1927 }
1928
1929 for (unsigned I = 0, E = Components.size(); I != E; ++I) {
1930 uint64_t Index = I;
1931
1932 Out << llvm::format("%4d | ", I);
1933
1934 const VTableComponent &Component = Components[I];
1935
1936 // Dump the component.
1937 switch (Component.getKind()) {
1938
1939 case VTableComponent::CK_VCallOffset:
1940 Out << "vcall_offset ("
1941 << Component.getVCallOffset().getQuantity()
1942 << ")";
1943 break;
1944
1945 case VTableComponent::CK_VBaseOffset:
1946 Out << "vbase_offset ("
1947 << Component.getVBaseOffset().getQuantity()
1948 << ")";
1949 break;
1950
1951 case VTableComponent::CK_OffsetToTop:
1952 Out << "offset_to_top ("
1953 << Component.getOffsetToTop().getQuantity()
1954 << ")";
1955 break;
1956
1957 case VTableComponent::CK_RTTI:
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00001958 Component.getRTTIDecl()->printQualifiedName(Out);
1959 Out << " RTTI";
Peter Collingbournecfd23562011-09-26 01:57:12 +00001960 break;
1961
1962 case VTableComponent::CK_FunctionPointer: {
1963 const CXXMethodDecl *MD = Component.getFunctionDecl();
1964
1965 std::string Str =
1966 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
1967 MD);
1968 Out << Str;
1969 if (MD->isPure())
1970 Out << " [pure]";
1971
David Blaikie596d2ca2012-10-16 20:25:33 +00001972 if (MD->isDeleted())
1973 Out << " [deleted]";
1974
Peter Collingbournecfd23562011-09-26 01:57:12 +00001975 ThunkInfo Thunk = VTableThunks.lookup(I);
1976 if (!Thunk.isEmpty()) {
1977 // If this function pointer has a return adjustment, dump it.
1978 if (!Thunk.Return.isEmpty()) {
1979 Out << "\n [return adjustment: ";
1980 Out << Thunk.Return.NonVirtual << " non-virtual";
1981
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00001982 if (Thunk.Return.Virtual.Itanium.VBaseOffsetOffset) {
1983 Out << ", " << Thunk.Return.Virtual.Itanium.VBaseOffsetOffset;
Peter Collingbournecfd23562011-09-26 01:57:12 +00001984 Out << " vbase offset offset";
1985 }
1986
1987 Out << ']';
1988 }
1989
1990 // If this function pointer has a 'this' pointer adjustment, dump it.
1991 if (!Thunk.This.isEmpty()) {
1992 Out << "\n [this adjustment: ";
1993 Out << Thunk.This.NonVirtual << " non-virtual";
1994
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00001995 if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) {
1996 Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset;
Peter Collingbournecfd23562011-09-26 01:57:12 +00001997 Out << " vcall offset offset";
1998 }
1999
2000 Out << ']';
2001 }
2002 }
2003
2004 break;
2005 }
2006
2007 case VTableComponent::CK_CompleteDtorPointer:
2008 case VTableComponent::CK_DeletingDtorPointer: {
2009 bool IsComplete =
2010 Component.getKind() == VTableComponent::CK_CompleteDtorPointer;
2011
2012 const CXXDestructorDecl *DD = Component.getDestructorDecl();
2013
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00002014 DD->printQualifiedName(Out);
Peter Collingbournecfd23562011-09-26 01:57:12 +00002015 if (IsComplete)
2016 Out << "() [complete]";
2017 else
2018 Out << "() [deleting]";
2019
2020 if (DD->isPure())
2021 Out << " [pure]";
2022
2023 ThunkInfo Thunk = VTableThunks.lookup(I);
2024 if (!Thunk.isEmpty()) {
2025 // If this destructor has a 'this' pointer adjustment, dump it.
2026 if (!Thunk.This.isEmpty()) {
2027 Out << "\n [this adjustment: ";
2028 Out << Thunk.This.NonVirtual << " non-virtual";
2029
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002030 if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) {
2031 Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset;
Peter Collingbournecfd23562011-09-26 01:57:12 +00002032 Out << " vcall offset offset";
2033 }
2034
2035 Out << ']';
2036 }
2037 }
2038
2039 break;
2040 }
2041
2042 case VTableComponent::CK_UnusedFunctionPointer: {
2043 const CXXMethodDecl *MD = Component.getUnusedFunctionDecl();
2044
2045 std::string Str =
2046 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2047 MD);
2048 Out << "[unused] " << Str;
2049 if (MD->isPure())
2050 Out << " [pure]";
2051 }
2052
2053 }
2054
2055 Out << '\n';
2056
2057 // Dump the next address point.
2058 uint64_t NextIndex = Index + 1;
2059 if (AddressPointsByIndex.count(NextIndex)) {
2060 if (AddressPointsByIndex.count(NextIndex) == 1) {
2061 const BaseSubobject &Base =
2062 AddressPointsByIndex.find(NextIndex)->second;
2063
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00002064 Out << " -- (";
2065 Base.getBase()->printQualifiedName(Out);
Peter Collingbournecfd23562011-09-26 01:57:12 +00002066 Out << ", " << Base.getBaseOffset().getQuantity();
2067 Out << ") vtable address --\n";
2068 } else {
2069 CharUnits BaseOffset =
2070 AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset();
2071
2072 // We store the class names in a set to get a stable order.
2073 std::set<std::string> ClassNames;
2074 for (std::multimap<uint64_t, BaseSubobject>::const_iterator I =
2075 AddressPointsByIndex.lower_bound(NextIndex), E =
2076 AddressPointsByIndex.upper_bound(NextIndex); I != E; ++I) {
2077 assert(I->second.getBaseOffset() == BaseOffset &&
2078 "Invalid base offset!");
2079 const CXXRecordDecl *RD = I->second.getBase();
2080 ClassNames.insert(RD->getQualifiedNameAsString());
2081 }
2082
2083 for (std::set<std::string>::const_iterator I = ClassNames.begin(),
2084 E = ClassNames.end(); I != E; ++I) {
2085 Out << " -- (" << *I;
2086 Out << ", " << BaseOffset.getQuantity() << ") vtable address --\n";
2087 }
2088 }
2089 }
2090 }
2091
2092 Out << '\n';
2093
2094 if (isBuildingConstructorVTable())
2095 return;
2096
2097 if (MostDerivedClass->getNumVBases()) {
2098 // We store the virtual base class names and their offsets in a map to get
2099 // a stable order.
2100
2101 std::map<std::string, CharUnits> ClassNamesAndOffsets;
2102 for (VBaseOffsetOffsetsMapTy::const_iterator I = VBaseOffsetOffsets.begin(),
2103 E = VBaseOffsetOffsets.end(); I != E; ++I) {
2104 std::string ClassName = I->first->getQualifiedNameAsString();
2105 CharUnits OffsetOffset = I->second;
2106 ClassNamesAndOffsets.insert(
2107 std::make_pair(ClassName, OffsetOffset));
2108 }
2109
2110 Out << "Virtual base offset offsets for '";
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00002111 MostDerivedClass->printQualifiedName(Out);
2112 Out << "' (";
Peter Collingbournecfd23562011-09-26 01:57:12 +00002113 Out << ClassNamesAndOffsets.size();
2114 Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n";
2115
2116 for (std::map<std::string, CharUnits>::const_iterator I =
2117 ClassNamesAndOffsets.begin(), E = ClassNamesAndOffsets.end();
2118 I != E; ++I)
2119 Out << " " << I->first << " | " << I->second.getQuantity() << '\n';
2120
2121 Out << "\n";
2122 }
2123
2124 if (!Thunks.empty()) {
2125 // We store the method names in a map to get a stable order.
2126 std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls;
2127
2128 for (ThunksMapTy::const_iterator I = Thunks.begin(), E = Thunks.end();
2129 I != E; ++I) {
2130 const CXXMethodDecl *MD = I->first;
2131 std::string MethodName =
2132 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2133 MD);
2134
2135 MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
2136 }
2137
2138 for (std::map<std::string, const CXXMethodDecl *>::const_iterator I =
2139 MethodNamesAndDecls.begin(), E = MethodNamesAndDecls.end();
2140 I != E; ++I) {
2141 const std::string &MethodName = I->first;
2142 const CXXMethodDecl *MD = I->second;
2143
2144 ThunkInfoVectorTy ThunksVector = Thunks[MD];
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002145 std::sort(ThunksVector.begin(), ThunksVector.end(),
Benjamin Kramerbbdd7642014-03-01 14:48:57 +00002146 [](const ThunkInfo &LHS, const ThunkInfo &RHS) {
Craig Topper36250ad2014-05-12 05:36:57 +00002147 assert(LHS.Method == nullptr && RHS.Method == nullptr);
Benjamin Kramera741b8c2014-03-03 20:26:46 +00002148 return std::tie(LHS.This, LHS.Return) < std::tie(RHS.This, RHS.Return);
Benjamin Kramerbbdd7642014-03-01 14:48:57 +00002149 });
Peter Collingbournecfd23562011-09-26 01:57:12 +00002150
2151 Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
2152 Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n";
2153
2154 for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) {
2155 const ThunkInfo &Thunk = ThunksVector[I];
2156
2157 Out << llvm::format("%4d | ", I);
2158
2159 // If this function pointer has a return pointer adjustment, dump it.
2160 if (!Thunk.Return.isEmpty()) {
Timur Iskhodzhanov11510312013-06-28 15:42:28 +00002161 Out << "return adjustment: " << Thunk.Return.NonVirtual;
Peter Collingbournecfd23562011-09-26 01:57:12 +00002162 Out << " non-virtual";
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00002163 if (Thunk.Return.Virtual.Itanium.VBaseOffsetOffset) {
2164 Out << ", " << Thunk.Return.Virtual.Itanium.VBaseOffsetOffset;
Peter Collingbournecfd23562011-09-26 01:57:12 +00002165 Out << " vbase offset offset";
2166 }
2167
2168 if (!Thunk.This.isEmpty())
2169 Out << "\n ";
2170 }
2171
2172 // If this function pointer has a 'this' pointer adjustment, dump it.
2173 if (!Thunk.This.isEmpty()) {
2174 Out << "this adjustment: ";
2175 Out << Thunk.This.NonVirtual << " non-virtual";
2176
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002177 if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) {
2178 Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset;
Peter Collingbournecfd23562011-09-26 01:57:12 +00002179 Out << " vcall offset offset";
2180 }
2181 }
2182
2183 Out << '\n';
2184 }
2185
2186 Out << '\n';
2187 }
2188 }
2189
2190 // Compute the vtable indices for all the member functions.
2191 // Store them in a map keyed by the index so we'll get a sorted table.
2192 std::map<uint64_t, std::string> IndicesMap;
2193
Aaron Ballman2b124d12014-03-13 16:36:16 +00002194 for (const auto *MD : MostDerivedClass->methods()) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00002195 // We only want virtual member functions.
2196 if (!MD->isVirtual())
2197 continue;
2198
2199 std::string MethodName =
2200 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2201 MD);
2202
2203 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00002204 GlobalDecl GD(DD, Dtor_Complete);
2205 assert(MethodVTableIndices.count(GD));
2206 uint64_t VTableIndex = MethodVTableIndices[GD];
2207 IndicesMap[VTableIndex] = MethodName + " [complete]";
2208 IndicesMap[VTableIndex + 1] = MethodName + " [deleting]";
Peter Collingbournecfd23562011-09-26 01:57:12 +00002209 } else {
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00002210 assert(MethodVTableIndices.count(MD));
2211 IndicesMap[MethodVTableIndices[MD]] = MethodName;
Peter Collingbournecfd23562011-09-26 01:57:12 +00002212 }
2213 }
2214
2215 // Print the vtable indices for all the member functions.
2216 if (!IndicesMap.empty()) {
2217 Out << "VTable indices for '";
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00002218 MostDerivedClass->printQualifiedName(Out);
Peter Collingbournecfd23562011-09-26 01:57:12 +00002219 Out << "' (" << IndicesMap.size() << " entries).\n";
2220
2221 for (std::map<uint64_t, std::string>::const_iterator I = IndicesMap.begin(),
2222 E = IndicesMap.end(); I != E; ++I) {
2223 uint64_t VTableIndex = I->first;
2224 const std::string &MethodName = I->second;
2225
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00002226 Out << llvm::format("%4" PRIu64 " | ", VTableIndex) << MethodName
Benjamin Kramer5291e682012-03-10 02:06:27 +00002227 << '\n';
Peter Collingbournecfd23562011-09-26 01:57:12 +00002228 }
2229 }
2230
2231 Out << '\n';
2232}
Peter Collingbournecfd23562011-09-26 01:57:12 +00002233}
2234
2235VTableLayout::VTableLayout(uint64_t NumVTableComponents,
2236 const VTableComponent *VTableComponents,
2237 uint64_t NumVTableThunks,
2238 const VTableThunkTy *VTableThunks,
Timur Iskhodzhanov52b8a052013-01-21 13:02:41 +00002239 const AddressPointsMapTy &AddressPoints,
2240 bool IsMicrosoftABI)
Peter Collingbournecfd23562011-09-26 01:57:12 +00002241 : NumVTableComponents(NumVTableComponents),
2242 VTableComponents(new VTableComponent[NumVTableComponents]),
2243 NumVTableThunks(NumVTableThunks),
2244 VTableThunks(new VTableThunkTy[NumVTableThunks]),
Timur Iskhodzhanov52b8a052013-01-21 13:02:41 +00002245 AddressPoints(AddressPoints),
2246 IsMicrosoftABI(IsMicrosoftABI) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00002247 std::copy(VTableComponents, VTableComponents+NumVTableComponents,
Benjamin Kramere2980632012-04-14 14:13:43 +00002248 this->VTableComponents.get());
2249 std::copy(VTableThunks, VTableThunks+NumVTableThunks,
2250 this->VTableThunks.get());
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002251 std::sort(this->VTableThunks.get(),
2252 this->VTableThunks.get() + NumVTableThunks,
Benjamin Kramerbbdd7642014-03-01 14:48:57 +00002253 [](const VTableLayout::VTableThunkTy &LHS,
2254 const VTableLayout::VTableThunkTy &RHS) {
2255 assert((LHS.first != RHS.first || LHS.second == RHS.second) &&
2256 "Different thunks should have unique indices!");
2257 return LHS.first < RHS.first;
2258 });
Peter Collingbournecfd23562011-09-26 01:57:12 +00002259}
2260
Benjamin Kramere2980632012-04-14 14:13:43 +00002261VTableLayout::~VTableLayout() { }
Peter Collingbournecfd23562011-09-26 01:57:12 +00002262
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002263ItaniumVTableContext::ItaniumVTableContext(ASTContext &Context)
Reid Klecknerb60a3d52013-12-20 23:58:52 +00002264 : VTableContextBase(/*MS=*/false) {}
Timur Iskhodzhanov52b8a052013-01-21 13:02:41 +00002265
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002266ItaniumVTableContext::~ItaniumVTableContext() {
Peter Collingbournecfd23562011-09-26 01:57:12 +00002267 llvm::DeleteContainerSeconds(VTableLayouts);
2268}
2269
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002270uint64_t ItaniumVTableContext::getMethodVTableIndex(GlobalDecl GD) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00002271 MethodVTableIndicesTy::iterator I = MethodVTableIndices.find(GD);
2272 if (I != MethodVTableIndices.end())
2273 return I->second;
2274
2275 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
2276
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002277 computeVTableRelatedInformation(RD);
Peter Collingbournecfd23562011-09-26 01:57:12 +00002278
2279 I = MethodVTableIndices.find(GD);
2280 assert(I != MethodVTableIndices.end() && "Did not find index!");
2281 return I->second;
2282}
2283
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002284CharUnits
2285ItaniumVTableContext::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD,
2286 const CXXRecordDecl *VBase) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00002287 ClassPairTy ClassPair(RD, VBase);
2288
2289 VirtualBaseClassOffsetOffsetsMapTy::iterator I =
2290 VirtualBaseClassOffsetOffsets.find(ClassPair);
2291 if (I != VirtualBaseClassOffsetOffsets.end())
2292 return I->second;
Craig Topper36250ad2014-05-12 05:36:57 +00002293
2294 VCallAndVBaseOffsetBuilder Builder(RD, RD, /*FinalOverriders=*/nullptr,
Peter Collingbournecfd23562011-09-26 01:57:12 +00002295 BaseSubobject(RD, CharUnits::Zero()),
2296 /*BaseIsVirtual=*/false,
2297 /*OffsetInLayoutClass=*/CharUnits::Zero());
2298
2299 for (VCallAndVBaseOffsetBuilder::VBaseOffsetOffsetsMapTy::const_iterator I =
2300 Builder.getVBaseOffsetOffsets().begin(),
2301 E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) {
2302 // Insert all types.
2303 ClassPairTy ClassPair(RD, I->first);
2304
2305 VirtualBaseClassOffsetOffsets.insert(
2306 std::make_pair(ClassPair, I->second));
2307 }
2308
2309 I = VirtualBaseClassOffsetOffsets.find(ClassPair);
2310 assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!");
2311
2312 return I->second;
2313}
2314
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002315static VTableLayout *CreateVTableLayout(const ItaniumVTableBuilder &Builder) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00002316 SmallVector<VTableLayout::VTableThunkTy, 1>
2317 VTableThunks(Builder.vtable_thunks_begin(), Builder.vtable_thunks_end());
Peter Collingbournecfd23562011-09-26 01:57:12 +00002318
2319 return new VTableLayout(Builder.getNumVTableComponents(),
2320 Builder.vtable_component_begin(),
2321 VTableThunks.size(),
2322 VTableThunks.data(),
Timur Iskhodzhanov52b8a052013-01-21 13:02:41 +00002323 Builder.getAddressPoints(),
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00002324 /*IsMicrosoftABI=*/false);
Peter Collingbournecfd23562011-09-26 01:57:12 +00002325}
2326
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002327void
2328ItaniumVTableContext::computeVTableRelatedInformation(const CXXRecordDecl *RD) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00002329 const VTableLayout *&Entry = VTableLayouts[RD];
2330
2331 // Check if we've computed this information before.
2332 if (Entry)
2333 return;
2334
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002335 ItaniumVTableBuilder Builder(*this, RD, CharUnits::Zero(),
2336 /*MostDerivedClassIsVirtual=*/0, RD);
Peter Collingbournecfd23562011-09-26 01:57:12 +00002337 Entry = CreateVTableLayout(Builder);
2338
Timur Iskhodzhanov05e36702013-06-05 14:05:50 +00002339 MethodVTableIndices.insert(Builder.vtable_indices_begin(),
2340 Builder.vtable_indices_end());
2341
Peter Collingbournecfd23562011-09-26 01:57:12 +00002342 // Add the known thunks.
2343 Thunks.insert(Builder.thunks_begin(), Builder.thunks_end());
2344
2345 // If we don't have the vbase information for this class, insert it.
2346 // getVirtualBaseOffsetOffset will compute it separately without computing
2347 // the rest of the vtable related information.
2348 if (!RD->getNumVBases())
2349 return;
2350
Timur Iskhodzhanov7f55a452013-07-02 16:00:40 +00002351 const CXXRecordDecl *VBase =
2352 RD->vbases_begin()->getType()->getAsCXXRecordDecl();
Peter Collingbournecfd23562011-09-26 01:57:12 +00002353
2354 if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase)))
2355 return;
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002356
2357 for (ItaniumVTableBuilder::VBaseOffsetOffsetsMapTy::const_iterator
2358 I = Builder.getVBaseOffsetOffsets().begin(),
2359 E = Builder.getVBaseOffsetOffsets().end();
2360 I != E; ++I) {
Peter Collingbournecfd23562011-09-26 01:57:12 +00002361 // Insert all types.
2362 ClassPairTy ClassPair(RD, I->first);
2363
2364 VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second));
2365 }
2366}
2367
Timur Iskhodzhanove1ebc5f2013-10-09 11:33:51 +00002368VTableLayout *ItaniumVTableContext::createConstructionVTableLayout(
2369 const CXXRecordDecl *MostDerivedClass, CharUnits MostDerivedClassOffset,
2370 bool MostDerivedClassIsVirtual, const CXXRecordDecl *LayoutClass) {
2371 ItaniumVTableBuilder Builder(*this, MostDerivedClass, MostDerivedClassOffset,
2372 MostDerivedClassIsVirtual, LayoutClass);
Peter Collingbournecfd23562011-09-26 01:57:12 +00002373 return CreateVTableLayout(Builder);
2374}
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002375
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002376namespace {
2377
2378// Vtables in the Microsoft ABI are different from the Itanium ABI.
2379//
2380// The main differences are:
2381// 1. Separate vftable and vbtable.
2382//
2383// 2. Each subobject with a vfptr gets its own vftable rather than an address
2384// point in a single vtable shared between all the subobjects.
2385// Each vftable is represented by a separate section and virtual calls
2386// must be done using the vftable which has a slot for the function to be
2387// called.
2388//
2389// 3. Virtual method definitions expect their 'this' parameter to point to the
2390// first vfptr whose table provides a compatible overridden method. In many
2391// cases, this permits the original vf-table entry to directly call
2392// the method instead of passing through a thunk.
2393//
2394// A compatible overridden method is one which does not have a non-trivial
2395// covariant-return adjustment.
2396//
2397// The first vfptr is the one with the lowest offset in the complete-object
2398// layout of the defining class, and the method definition will subtract
2399// that constant offset from the parameter value to get the real 'this'
2400// value. Therefore, if the offset isn't really constant (e.g. if a virtual
2401// function defined in a virtual base is overridden in a more derived
2402// virtual base and these bases have a reverse order in the complete
2403// object), the vf-table may require a this-adjustment thunk.
2404//
2405// 4. vftables do not contain new entries for overrides that merely require
2406// this-adjustment. Together with #3, this keeps vf-tables smaller and
2407// eliminates the need for this-adjustment thunks in many cases, at the cost
2408// of often requiring redundant work to adjust the "this" pointer.
2409//
2410// 5. Instead of VTT and constructor vtables, vbtables and vtordisps are used.
2411// Vtordisps are emitted into the class layout if a class has
2412// a) a user-defined ctor/dtor
2413// and
2414// b) a method overriding a method in a virtual base.
2415
2416class VFTableBuilder {
2417public:
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00002418 typedef MicrosoftVTableContext::MethodVFTableLocation MethodVFTableLocation;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002419
2420 typedef llvm::DenseMap<GlobalDecl, MethodVFTableLocation>
2421 MethodVFTableLocationsTy;
2422
Timur Iskhodzhanovba557022014-03-20 20:38:34 +00002423 typedef llvm::iterator_range<MethodVFTableLocationsTy::const_iterator>
2424 method_locations_range;
2425
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002426private:
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00002427 /// VTables - Global vtable information.
2428 MicrosoftVTableContext &VTables;
2429
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002430 /// Context - The ASTContext which we will use for layout information.
2431 ASTContext &Context;
2432
2433 /// MostDerivedClass - The most derived class for which we're building this
2434 /// vtable.
2435 const CXXRecordDecl *MostDerivedClass;
2436
2437 const ASTRecordLayout &MostDerivedClassLayout;
2438
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00002439 const VPtrInfo &WhichVFPtr;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002440
2441 /// FinalOverriders - The final overriders of the most derived class.
2442 const FinalOverriders Overriders;
2443
2444 /// Components - The components of the vftable being built.
2445 SmallVector<VTableComponent, 64> Components;
2446
2447 MethodVFTableLocationsTy MethodVFTableLocations;
2448
2449 /// MethodInfo - Contains information about a method in a vtable.
2450 /// (Used for computing 'this' pointer adjustment thunks.
2451 struct MethodInfo {
2452 /// VBTableIndex - The nonzero index in the vbtable that
2453 /// this method's base has, or zero.
2454 const uint64_t VBTableIndex;
2455
2456 /// VFTableIndex - The index in the vftable that this method has.
2457 const uint64_t VFTableIndex;
2458
2459 /// Shadowed - Indicates if this vftable slot is shadowed by
2460 /// a slot for a covariant-return override. If so, it shouldn't be printed
2461 /// or used for vcalls in the most derived class.
2462 bool Shadowed;
2463
Timur Iskhodzhanov8b142422013-10-22 14:50:20 +00002464 MethodInfo(uint64_t VBTableIndex, uint64_t VFTableIndex)
2465 : VBTableIndex(VBTableIndex), VFTableIndex(VFTableIndex),
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002466 Shadowed(false) {}
2467
Timur Iskhodzhanov8b142422013-10-22 14:50:20 +00002468 MethodInfo() : VBTableIndex(0), VFTableIndex(0), Shadowed(false) {}
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002469 };
2470
2471 typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy;
2472
2473 /// MethodInfoMap - The information for all methods in the vftable we're
2474 /// currently building.
2475 MethodInfoMapTy MethodInfoMap;
2476
2477 typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy;
2478
2479 /// VTableThunks - The thunks by vftable index in the vftable currently being
2480 /// built.
2481 VTableThunksMapTy VTableThunks;
2482
2483 typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
2484 typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
2485
2486 /// Thunks - A map that contains all the thunks needed for all methods in the
2487 /// most derived class for which the vftable is currently being built.
2488 ThunksMapTy Thunks;
2489
2490 /// AddThunk - Add a thunk for the given method.
2491 void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk) {
2492 SmallVector<ThunkInfo, 1> &ThunksVector = Thunks[MD];
2493
2494 // Check if we have this thunk already.
2495 if (std::find(ThunksVector.begin(), ThunksVector.end(), Thunk) !=
2496 ThunksVector.end())
2497 return;
2498
2499 ThunksVector.push_back(Thunk);
2500 }
2501
2502 /// ComputeThisOffset - Returns the 'this' argument offset for the given
Timur Iskhodzhanov3a9ac932014-03-04 18:17:38 +00002503 /// method, relative to the beginning of the MostDerivedClass.
2504 CharUnits ComputeThisOffset(FinalOverriders::OverriderInfo Overrider);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002505
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002506 void CalculateVtordispAdjustment(FinalOverriders::OverriderInfo Overrider,
2507 CharUnits ThisOffset, ThisAdjustment &TA);
2508
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002509 /// AddMethod - Add a single virtual member function to the vftable
2510 /// components vector.
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002511 void AddMethod(const CXXMethodDecl *MD, ThunkInfo TI) {
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00002512 if (!TI.isEmpty()) {
2513 VTableThunks[Components.size()] = TI;
2514 AddThunk(MD, TI);
2515 }
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002516 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002517 assert(TI.Return.isEmpty() &&
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002518 "Destructor can't have return adjustment!");
2519 Components.push_back(VTableComponent::MakeDeletingDtor(DD));
2520 } else {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002521 Components.push_back(VTableComponent::MakeFunction(MD));
2522 }
2523 }
2524
Reid Kleckner558cab22013-12-27 19:45:53 +00002525 bool NeedsReturnAdjustingThunk(const CXXMethodDecl *MD);
Reid Kleckner604c8b42013-12-27 19:43:59 +00002526
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002527 /// AddMethods - Add the methods of this base subobject and the relevant
2528 /// subbases to the vftable we're currently laying out.
2529 void AddMethods(BaseSubobject Base, unsigned BaseDepth,
2530 const CXXRecordDecl *LastVBase,
2531 BasesSetVectorTy &VisitedBases);
2532
2533 void LayoutVFTable() {
2534 // FIXME: add support for RTTI when we have proper LLVM support for symbols
2535 // pointing to the middle of a section.
2536
2537 BasesSetVectorTy VisitedBases;
Craig Topper36250ad2014-05-12 05:36:57 +00002538 AddMethods(BaseSubobject(MostDerivedClass, CharUnits::Zero()), 0, nullptr,
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002539 VisitedBases);
Timur Iskhodzhanovdd0a27662014-03-26 08:12:53 +00002540 assert(Components.size() && "vftable can't be empty");
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002541
2542 assert(MethodVFTableLocations.empty());
2543 for (MethodInfoMapTy::const_iterator I = MethodInfoMap.begin(),
2544 E = MethodInfoMap.end(); I != E; ++I) {
2545 const CXXMethodDecl *MD = I->first;
2546 const MethodInfo &MI = I->second;
2547 // Skip the methods that the MostDerivedClass didn't override
2548 // and the entries shadowed by return adjusting thunks.
2549 if (MD->getParent() != MostDerivedClass || MI.Shadowed)
2550 continue;
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00002551 MethodVFTableLocation Loc(MI.VBTableIndex, WhichVFPtr.getVBaseWithVPtr(),
2552 WhichVFPtr.NonVirtualOffset, MI.VFTableIndex);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002553 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2554 MethodVFTableLocations[GlobalDecl(DD, Dtor_Deleting)] = Loc;
2555 } else {
2556 MethodVFTableLocations[MD] = Loc;
2557 }
2558 }
2559 }
2560
2561 void ErrorUnsupported(StringRef Feature, SourceLocation Location) {
2562 clang::DiagnosticsEngine &Diags = Context.getDiagnostics();
2563 unsigned DiagID = Diags.getCustomDiagID(
2564 DiagnosticsEngine::Error, "v-table layout for %0 is not supported yet");
2565 Diags.Report(Context.getFullLoc(Location), DiagID) << Feature;
2566 }
2567
2568public:
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00002569 VFTableBuilder(MicrosoftVTableContext &VTables,
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00002570 const CXXRecordDecl *MostDerivedClass, const VPtrInfo *Which)
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00002571 : VTables(VTables),
2572 Context(MostDerivedClass->getASTContext()),
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002573 MostDerivedClass(MostDerivedClass),
2574 MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)),
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00002575 WhichVFPtr(*Which),
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002576 Overriders(MostDerivedClass, CharUnits(), MostDerivedClass) {
David Majnemerd905da42014-07-01 20:30:31 +00002577 // Only include the RTTI component if we know that we will provide a
2578 // definition of the vftable.
2579 if (Context.getLangOpts().RTTI &&
2580 !MostDerivedClass->hasAttr<DLLImportAttr>())
2581 Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass));
2582
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002583 LayoutVFTable();
2584
2585 if (Context.getLangOpts().DumpVTableLayouts)
Reid Kleckner5bc6d0f2013-11-08 21:28:00 +00002586 dumpLayout(llvm::outs());
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002587 }
2588
2589 uint64_t getNumThunks() const { return Thunks.size(); }
2590
2591 ThunksMapTy::const_iterator thunks_begin() const { return Thunks.begin(); }
2592
2593 ThunksMapTy::const_iterator thunks_end() const { return Thunks.end(); }
2594
Timur Iskhodzhanovba557022014-03-20 20:38:34 +00002595 method_locations_range vtable_locations() const {
2596 return method_locations_range(MethodVFTableLocations.begin(),
2597 MethodVFTableLocations.end());
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002598 }
2599
2600 uint64_t getNumVTableComponents() const { return Components.size(); }
2601
2602 const VTableComponent *vtable_component_begin() const {
2603 return Components.begin();
2604 }
2605
2606 const VTableComponent *vtable_component_end() const {
2607 return Components.end();
2608 }
2609
2610 VTableThunksMapTy::const_iterator vtable_thunks_begin() const {
2611 return VTableThunks.begin();
2612 }
2613
2614 VTableThunksMapTy::const_iterator vtable_thunks_end() const {
2615 return VTableThunks.end();
2616 }
2617
2618 void dumpLayout(raw_ostream &);
2619};
2620
Reid Klecknerb40a27d2014-01-03 00:14:35 +00002621} // end namespace
2622
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002623/// InitialOverriddenDefinitionCollector - Finds the set of least derived bases
2624/// that define the given method.
2625struct InitialOverriddenDefinitionCollector {
2626 BasesSetVectorTy Bases;
2627 OverriddenMethodsSetTy VisitedOverriddenMethods;
2628
2629 bool visit(const CXXMethodDecl *OverriddenMD) {
2630 if (OverriddenMD->size_overridden_methods() == 0)
2631 Bases.insert(OverriddenMD->getParent());
2632 // Don't recurse on this method if we've already collected it.
2633 return VisitedOverriddenMethods.insert(OverriddenMD);
2634 }
2635};
2636
2637static bool BaseInSet(const CXXBaseSpecifier *Specifier,
2638 CXXBasePath &Path, void *BasesSet) {
2639 BasesSetVectorTy *Bases = (BasesSetVectorTy *)BasesSet;
2640 return Bases->count(Specifier->getType()->getAsCXXRecordDecl());
2641}
2642
2643CharUnits
Timur Iskhodzhanov3a9ac932014-03-04 18:17:38 +00002644VFTableBuilder::ComputeThisOffset(FinalOverriders::OverriderInfo Overrider) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002645 InitialOverriddenDefinitionCollector Collector;
Timur Iskhodzhanov3a9ac932014-03-04 18:17:38 +00002646 visitAllOverriddenMethods(Overrider.Method, Collector);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002647
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00002648 // If there are no overrides then 'this' is located
2649 // in the base that defines the method.
2650 if (Collector.Bases.size() == 0)
2651 return Overrider.Offset;
2652
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002653 CXXBasePaths Paths;
Timur Iskhodzhanov3a9ac932014-03-04 18:17:38 +00002654 Overrider.Method->getParent()->lookupInBases(BaseInSet, &Collector.Bases,
2655 Paths);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002656
2657 // This will hold the smallest this offset among overridees of MD.
2658 // This implies that an offset of a non-virtual base will dominate an offset
2659 // of a virtual base to potentially reduce the number of thunks required
2660 // in the derived classes that inherit this method.
2661 CharUnits Ret;
2662 bool First = true;
2663
Timur Iskhodzhanoved11ae32014-04-17 22:01:48 +00002664 const ASTRecordLayout &OverriderRDLayout =
2665 Context.getASTRecordLayout(Overrider.Method->getParent());
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002666 for (CXXBasePaths::paths_iterator I = Paths.begin(), E = Paths.end();
2667 I != E; ++I) {
2668 const CXXBasePath &Path = (*I);
Timur Iskhodzhanov3a9ac932014-03-04 18:17:38 +00002669 CharUnits ThisOffset = Overrider.Offset;
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002670 CharUnits LastVBaseOffset;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002671
2672 // For each path from the overrider to the parents of the overridden methods,
2673 // traverse the path, calculating the this offset in the most derived class.
2674 for (int J = 0, F = Path.size(); J != F; ++J) {
2675 const CXXBasePathElement &Element = Path[J];
2676 QualType CurTy = Element.Base->getType();
2677 const CXXRecordDecl *PrevRD = Element.Class,
2678 *CurRD = CurTy->getAsCXXRecordDecl();
2679 const ASTRecordLayout &Layout = Context.getASTRecordLayout(PrevRD);
2680
2681 if (Element.Base->isVirtual()) {
Timur Iskhodzhanoved11ae32014-04-17 22:01:48 +00002682 // The interesting things begin when you have virtual inheritance.
2683 // The final overrider will use a static adjustment equal to the offset
2684 // of the vbase in the final overrider class.
2685 // For example, if the final overrider is in a vbase B of the most
2686 // derived class and it overrides a method of the B's own vbase A,
2687 // it uses A* as "this". In its prologue, it can cast A* to B* with
2688 // a static offset. This offset is used regardless of the actual
2689 // offset of A from B in the most derived class, requiring an
2690 // this-adjusting thunk in the vftable if A and B are laid out
2691 // differently in the most derived class.
2692 LastVBaseOffset = ThisOffset =
2693 Overrider.Offset + OverriderRDLayout.getVBaseClassOffset(CurRD);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002694 } else {
2695 ThisOffset += Layout.getBaseClassOffset(CurRD);
2696 }
2697 }
2698
Timur Iskhodzhanov3a9ac932014-03-04 18:17:38 +00002699 if (isa<CXXDestructorDecl>(Overrider.Method)) {
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002700 if (LastVBaseOffset.isZero()) {
2701 // If a "Base" class has at least one non-virtual base with a virtual
2702 // destructor, the "Base" virtual destructor will take the address
2703 // of the "Base" subobject as the "this" argument.
Timur Iskhodzhanov3a9ac932014-03-04 18:17:38 +00002704 ThisOffset = Overrider.Offset;
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002705 } else {
2706 // A virtual destructor of a virtual base takes the address of the
2707 // virtual base subobject as the "this" argument.
Timur Iskhodzhanov3a9ac932014-03-04 18:17:38 +00002708 ThisOffset = LastVBaseOffset;
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002709 }
2710 }
Timur Iskhodzhanov62082b72013-10-16 18:24:06 +00002711
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002712 if (Ret > ThisOffset || First) {
2713 First = false;
2714 Ret = ThisOffset;
2715 }
2716 }
2717
2718 assert(!First && "Method not found in the given subobject?");
2719 return Ret;
2720}
2721
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002722void VFTableBuilder::CalculateVtordispAdjustment(
2723 FinalOverriders::OverriderInfo Overrider, CharUnits ThisOffset,
2724 ThisAdjustment &TA) {
2725 const ASTRecordLayout::VBaseOffsetsMapTy &VBaseMap =
2726 MostDerivedClassLayout.getVBaseOffsetsMap();
2727 const ASTRecordLayout::VBaseOffsetsMapTy::const_iterator &VBaseMapEntry =
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00002728 VBaseMap.find(WhichVFPtr.getVBaseWithVPtr());
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002729 assert(VBaseMapEntry != VBaseMap.end());
2730
Timur Iskhodzhanov6b128502014-04-22 17:32:02 +00002731 // If there's no vtordisp or the final overrider is defined in the same vbase
2732 // as the initial declaration, we don't need any vtordisp adjustment.
2733 if (!VBaseMapEntry->second.hasVtorDisp() ||
2734 Overrider.VirtualBase == WhichVFPtr.getVBaseWithVPtr())
Timur Iskhodzhanov057fa3a2014-04-17 11:01:41 +00002735 return;
2736
2737 // OK, now we know we need to use a vtordisp thunk.
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002738 // The implicit vtordisp field is located right before the vbase.
Timur Iskhodzhanov057fa3a2014-04-17 11:01:41 +00002739 CharUnits VFPtrVBaseOffset = VBaseMapEntry->second.VBaseOffset;
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002740 TA.Virtual.Microsoft.VtordispOffset =
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00002741 (VFPtrVBaseOffset - WhichVFPtr.FullOffsetInMDC).getQuantity() - 4;
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002742
Timur Iskhodzhanov057fa3a2014-04-17 11:01:41 +00002743 // A simple vtordisp thunk will suffice if the final overrider is defined
2744 // in either the most derived class or its non-virtual base.
Timur Iskhodzhanov6b128502014-04-22 17:32:02 +00002745 if (Overrider.Method->getParent() == MostDerivedClass ||
2746 !Overrider.VirtualBase)
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002747 return;
2748
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002749 // Otherwise, we need to do use the dynamic offset of the final overrider
2750 // in order to get "this" adjustment right.
2751 TA.Virtual.Microsoft.VBPtrOffset =
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00002752 (VFPtrVBaseOffset + WhichVFPtr.NonVirtualOffset -
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002753 MostDerivedClassLayout.getVBPtrOffset()).getQuantity();
2754 TA.Virtual.Microsoft.VBOffsetOffset =
2755 Context.getTypeSizeInChars(Context.IntTy).getQuantity() *
Timur Iskhodzhanov6b128502014-04-22 17:32:02 +00002756 VTables.getVBTableIndex(MostDerivedClass, Overrider.VirtualBase);
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002757
2758 TA.NonVirtual = (ThisOffset - Overrider.Offset).getQuantity();
2759}
2760
Timur Iskhodzhanov20df98c2013-10-06 15:31:37 +00002761static void GroupNewVirtualOverloads(
2762 const CXXRecordDecl *RD,
2763 SmallVector<const CXXMethodDecl *, 10> &VirtualMethods) {
2764 // Put the virtual methods into VirtualMethods in the proper order:
2765 // 1) Group overloads by declaration name. New groups are added to the
2766 // vftable in the order of their first declarations in this class
Reid Kleckner6701de22014-02-19 22:06:10 +00002767 // (including overrides and non-virtual methods).
Timur Iskhodzhanov20df98c2013-10-06 15:31:37 +00002768 // 2) In each group, new overloads appear in the reverse order of declaration.
2769 typedef SmallVector<const CXXMethodDecl *, 1> MethodGroup;
2770 SmallVector<MethodGroup, 10> Groups;
2771 typedef llvm::DenseMap<DeclarationName, unsigned> VisitedGroupIndicesTy;
2772 VisitedGroupIndicesTy VisitedGroupIndices;
Aaron Ballman2b124d12014-03-13 16:36:16 +00002773 for (const auto *MD : RD->methods()) {
Timur Iskhodzhanov20df98c2013-10-06 15:31:37 +00002774 VisitedGroupIndicesTy::iterator J;
2775 bool Inserted;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +00002776 std::tie(J, Inserted) = VisitedGroupIndices.insert(
Timur Iskhodzhanov20df98c2013-10-06 15:31:37 +00002777 std::make_pair(MD->getDeclName(), Groups.size()));
2778 if (Inserted)
Reid Kleckner6701de22014-02-19 22:06:10 +00002779 Groups.push_back(MethodGroup());
Aaron Ballman2b124d12014-03-13 16:36:16 +00002780 if (MD->isVirtual())
Timur Iskhodzhanov20df98c2013-10-06 15:31:37 +00002781 Groups[J->second].push_back(MD);
2782 }
2783
2784 for (unsigned I = 0, E = Groups.size(); I != E; ++I)
2785 VirtualMethods.append(Groups[I].rbegin(), Groups[I].rend());
2786}
2787
Reid Kleckner604c8b42013-12-27 19:43:59 +00002788/// We need a return adjusting thunk for this method if its return type is
2789/// not trivially convertible to the return type of any of its overridden
2790/// methods.
2791bool VFTableBuilder::NeedsReturnAdjustingThunk(const CXXMethodDecl *MD) {
2792 OverriddenMethodsSetTy OverriddenMethods;
2793 ComputeAllOverriddenMethods(MD, OverriddenMethods);
2794 for (OverriddenMethodsSetTy::iterator I = OverriddenMethods.begin(),
2795 E = OverriddenMethods.end();
2796 I != E; ++I) {
2797 const CXXMethodDecl *OverriddenMD = *I;
2798 BaseOffset Adjustment =
2799 ComputeReturnAdjustmentBaseOffset(Context, MD, OverriddenMD);
2800 if (!Adjustment.isEmpty())
2801 return true;
2802 }
2803 return false;
2804}
2805
Timur Iskhodzhanovdd0a27662014-03-26 08:12:53 +00002806static bool isDirectVBase(const CXXRecordDecl *Base, const CXXRecordDecl *RD) {
2807 for (const auto &B : RD->bases()) {
2808 if (B.isVirtual() && B.getType()->getAsCXXRecordDecl() == Base)
2809 return true;
2810 }
2811 return false;
2812}
2813
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002814void VFTableBuilder::AddMethods(BaseSubobject Base, unsigned BaseDepth,
2815 const CXXRecordDecl *LastVBase,
2816 BasesSetVectorTy &VisitedBases) {
2817 const CXXRecordDecl *RD = Base.getBase();
2818 if (!RD->isPolymorphic())
2819 return;
2820
2821 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2822
2823 // See if this class expands a vftable of the base we look at, which is either
2824 // the one defined by the vfptr base path or the primary base of the current class.
Craig Topper36250ad2014-05-12 05:36:57 +00002825 const CXXRecordDecl *NextBase = nullptr, *NextLastVBase = LastVBase;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002826 CharUnits NextBaseOffset;
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00002827 if (BaseDepth < WhichVFPtr.PathToBaseWithVPtr.size()) {
2828 NextBase = WhichVFPtr.PathToBaseWithVPtr[BaseDepth];
Timur Iskhodzhanovdd0a27662014-03-26 08:12:53 +00002829 if (isDirectVBase(NextBase, RD)) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002830 NextLastVBase = NextBase;
2831 NextBaseOffset = MostDerivedClassLayout.getVBaseClassOffset(NextBase);
2832 } else {
2833 NextBaseOffset =
2834 Base.getBaseOffset() + Layout.getBaseClassOffset(NextBase);
2835 }
2836 } else if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
2837 assert(!Layout.isPrimaryBaseVirtual() &&
2838 "No primary virtual bases in this ABI");
2839 NextBase = PrimaryBase;
2840 NextBaseOffset = Base.getBaseOffset();
2841 }
2842
2843 if (NextBase) {
2844 AddMethods(BaseSubobject(NextBase, NextBaseOffset), BaseDepth + 1,
2845 NextLastVBase, VisitedBases);
2846 if (!VisitedBases.insert(NextBase))
2847 llvm_unreachable("Found a duplicate primary base!");
2848 }
2849
Timur Iskhodzhanov20df98c2013-10-06 15:31:37 +00002850 SmallVector<const CXXMethodDecl*, 10> VirtualMethods;
2851 // Put virtual methods in the proper order.
2852 GroupNewVirtualOverloads(RD, VirtualMethods);
2853
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002854 // Now go through all virtual member functions and add them to the current
2855 // vftable. This is done by
2856 // - replacing overridden methods in their existing slots, as long as they
2857 // don't require return adjustment; calculating This adjustment if needed.
2858 // - adding new slots for methods of the current base not present in any
2859 // sub-bases;
2860 // - adding new slots for methods that require Return adjustment.
2861 // We keep track of the methods visited in the sub-bases in MethodInfoMap.
Timur Iskhodzhanov20df98c2013-10-06 15:31:37 +00002862 for (unsigned I = 0, E = VirtualMethods.size(); I != E; ++I) {
2863 const CXXMethodDecl *MD = VirtualMethods[I];
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002864
2865 FinalOverriders::OverriderInfo Overrider =
2866 Overriders.getOverrider(MD, Base.getBaseOffset());
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00002867 const CXXMethodDecl *OverriderMD = Overrider.Method;
2868 const CXXMethodDecl *OverriddenMD =
2869 FindNearestOverriddenMethod(MD, VisitedBases);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002870
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00002871 ThisAdjustment ThisAdjustmentOffset;
2872 bool ReturnAdjustingThunk = false;
2873 CharUnits ThisOffset = ComputeThisOffset(Overrider);
2874 ThisAdjustmentOffset.NonVirtual =
2875 (ThisOffset - WhichVFPtr.FullOffsetInMDC).getQuantity();
2876 if ((OverriddenMD || OverriderMD != MD) &&
2877 WhichVFPtr.getVBaseWithVPtr())
2878 CalculateVtordispAdjustment(Overrider, ThisOffset, ThisAdjustmentOffset);
2879
2880 if (OverriddenMD) {
2881 // If MD overrides anything in this vftable, we need to update the entries.
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002882 MethodInfoMapTy::iterator OverriddenMDIterator =
2883 MethodInfoMap.find(OverriddenMD);
2884
2885 // If the overridden method went to a different vftable, skip it.
2886 if (OverriddenMDIterator == MethodInfoMap.end())
2887 continue;
2888
2889 MethodInfo &OverriddenMethodInfo = OverriddenMDIterator->second;
2890
Reid Kleckner604c8b42013-12-27 19:43:59 +00002891 if (!NeedsReturnAdjustingThunk(MD)) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002892 // No return adjustment needed - just replace the overridden method info
2893 // with the current info.
2894 MethodInfo MI(OverriddenMethodInfo.VBTableIndex,
2895 OverriddenMethodInfo.VFTableIndex);
2896 MethodInfoMap.erase(OverriddenMDIterator);
2897
2898 assert(!MethodInfoMap.count(MD) &&
2899 "Should not have method info for this method yet!");
2900 MethodInfoMap.insert(std::make_pair(MD, MI));
2901 continue;
Reid Kleckner31a9f742013-12-27 20:29:16 +00002902 }
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002903
Reid Kleckner31a9f742013-12-27 20:29:16 +00002904 // In case we need a return adjustment, we'll add a new slot for
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00002905 // the overrider. Mark the overriden method as shadowed by the new slot.
Reid Kleckner31a9f742013-12-27 20:29:16 +00002906 OverriddenMethodInfo.Shadowed = true;
Reid Kleckner31a9f742013-12-27 20:29:16 +00002907
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00002908 // Force a special name mangling for a return-adjusting thunk
2909 // unless the method is the final overrider without this adjustment.
2910 ReturnAdjustingThunk =
2911 !(MD == OverriderMD && ThisAdjustmentOffset.isEmpty());
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00002912 } else if (Base.getBaseOffset() != WhichVFPtr.FullOffsetInMDC ||
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002913 MD->size_overridden_methods()) {
2914 // Skip methods that don't belong to the vftable of the current class,
2915 // e.g. each method that wasn't seen in any of the visited sub-bases
2916 // but overrides multiple methods of other sub-bases.
2917 continue;
2918 }
2919
2920 // If we got here, MD is a method not seen in any of the sub-bases or
2921 // it requires return adjustment. Insert the method info for this method.
2922 unsigned VBIndex =
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00002923 LastVBase ? VTables.getVBTableIndex(MostDerivedClass, LastVBase) : 0;
David Majnemerd905da42014-07-01 20:30:31 +00002924 MethodInfo MI(VBIndex, Context.getLangOpts().RTTI ? Components.size() - 1
2925 : Components.size());
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002926
2927 assert(!MethodInfoMap.count(MD) &&
2928 "Should not have method info for this method yet!");
2929 MethodInfoMap.insert(std::make_pair(MD, MI));
2930
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002931 // Check if this overrider needs a return adjustment.
2932 // We don't want to do this for pure virtual member functions.
2933 BaseOffset ReturnAdjustmentOffset;
2934 ReturnAdjustment ReturnAdjustment;
2935 if (!OverriderMD->isPure()) {
2936 ReturnAdjustmentOffset =
2937 ComputeReturnAdjustmentBaseOffset(Context, OverriderMD, MD);
2938 }
2939 if (!ReturnAdjustmentOffset.isEmpty()) {
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00002940 ReturnAdjustingThunk = true;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002941 ReturnAdjustment.NonVirtual =
2942 ReturnAdjustmentOffset.NonVirtualOffset.getQuantity();
2943 if (ReturnAdjustmentOffset.VirtualBase) {
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00002944 const ASTRecordLayout &DerivedLayout =
2945 Context.getASTRecordLayout(ReturnAdjustmentOffset.DerivedClass);
2946 ReturnAdjustment.Virtual.Microsoft.VBPtrOffset =
2947 DerivedLayout.getVBPtrOffset().getQuantity();
2948 ReturnAdjustment.Virtual.Microsoft.VBIndex =
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00002949 VTables.getVBTableIndex(ReturnAdjustmentOffset.DerivedClass,
2950 ReturnAdjustmentOffset.VirtualBase);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002951 }
2952 }
2953
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00002954 AddMethod(OverriderMD, ThunkInfo(ThisAdjustmentOffset, ReturnAdjustment,
Craig Topper36250ad2014-05-12 05:36:57 +00002955 ReturnAdjustingThunk ? MD : nullptr));
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002956 }
2957}
2958
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00002959static void PrintBasePath(const VPtrInfo::BasePath &Path, raw_ostream &Out) {
2960 for (VPtrInfo::BasePath::const_reverse_iterator I = Path.rbegin(),
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002961 E = Path.rend(); I != E; ++I) {
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00002962 Out << "'";
2963 (*I)->printQualifiedName(Out);
2964 Out << "' in ";
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00002965 }
2966}
2967
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00002968static void dumpMicrosoftThunkAdjustment(const ThunkInfo &TI, raw_ostream &Out,
2969 bool ContinueFirstLine) {
2970 const ReturnAdjustment &R = TI.Return;
2971 bool Multiline = false;
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00002972 const char *LinePrefix = "\n ";
2973 if (!R.isEmpty() || TI.Method) {
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00002974 if (!ContinueFirstLine)
2975 Out << LinePrefix;
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00002976 Out << "[return adjustment (to type '"
2977 << TI.Method->getReturnType().getCanonicalType().getAsString()
2978 << "'): ";
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00002979 if (R.Virtual.Microsoft.VBPtrOffset)
2980 Out << "vbptr at offset " << R.Virtual.Microsoft.VBPtrOffset << ", ";
2981 if (R.Virtual.Microsoft.VBIndex)
2982 Out << "vbase #" << R.Virtual.Microsoft.VBIndex << ", ";
2983 Out << R.NonVirtual << " non-virtual]";
2984 Multiline = true;
2985 }
2986
2987 const ThisAdjustment &T = TI.This;
2988 if (!T.isEmpty()) {
2989 if (Multiline || !ContinueFirstLine)
2990 Out << LinePrefix;
2991 Out << "[this adjustment: ";
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002992 if (!TI.This.Virtual.isEmpty()) {
2993 assert(T.Virtual.Microsoft.VtordispOffset < 0);
2994 Out << "vtordisp at " << T.Virtual.Microsoft.VtordispOffset << ", ";
2995 if (T.Virtual.Microsoft.VBPtrOffset) {
2996 Out << "vbptr at " << T.Virtual.Microsoft.VBPtrOffset
Timur Iskhodzhanova8957582014-03-07 09:34:59 +00002997 << " to the left,";
Timur Iskhodzhanov053142a2013-11-06 06:24:31 +00002998 assert(T.Virtual.Microsoft.VBOffsetOffset > 0);
2999 Out << LinePrefix << " vboffset at "
3000 << T.Virtual.Microsoft.VBOffsetOffset << " in the vbtable, ";
3001 }
3002 }
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00003003 Out << T.NonVirtual << " non-virtual]";
3004 }
3005}
3006
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003007void VFTableBuilder::dumpLayout(raw_ostream &Out) {
3008 Out << "VFTable for ";
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003009 PrintBasePath(WhichVFPtr.PathToBaseWithVPtr, Out);
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00003010 Out << "'";
3011 MostDerivedClass->printQualifiedName(Out);
Timur Iskhodzhanov77764b62014-03-05 13:54:07 +00003012 Out << "' (" << Components.size()
3013 << (Components.size() == 1 ? " entry" : " entries") << ").\n";
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003014
3015 for (unsigned I = 0, E = Components.size(); I != E; ++I) {
3016 Out << llvm::format("%4d | ", I);
3017
3018 const VTableComponent &Component = Components[I];
3019
3020 // Dump the component.
3021 switch (Component.getKind()) {
3022 case VTableComponent::CK_RTTI:
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00003023 Component.getRTTIDecl()->printQualifiedName(Out);
3024 Out << " RTTI";
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003025 break;
3026
3027 case VTableComponent::CK_FunctionPointer: {
3028 const CXXMethodDecl *MD = Component.getFunctionDecl();
3029
Reid Kleckner604c8b42013-12-27 19:43:59 +00003030 // FIXME: Figure out how to print the real thunk type, since they can
3031 // differ in the return type.
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003032 std::string Str = PredefinedExpr::ComputeName(
3033 PredefinedExpr::PrettyFunctionNoVirtual, MD);
3034 Out << Str;
3035 if (MD->isPure())
3036 Out << " [pure]";
3037
3038 if (MD->isDeleted()) {
3039 ErrorUnsupported("deleted methods", MD->getLocation());
3040 Out << " [deleted]";
3041 }
3042
3043 ThunkInfo Thunk = VTableThunks.lookup(I);
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00003044 if (!Thunk.isEmpty())
3045 dumpMicrosoftThunkAdjustment(Thunk, Out, /*ContinueFirstLine=*/false);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003046
3047 break;
3048 }
3049
3050 case VTableComponent::CK_DeletingDtorPointer: {
3051 const CXXDestructorDecl *DD = Component.getDestructorDecl();
3052
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00003053 DD->printQualifiedName(Out);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003054 Out << "() [scalar deleting]";
3055
3056 if (DD->isPure())
3057 Out << " [pure]";
3058
3059 ThunkInfo Thunk = VTableThunks.lookup(I);
3060 if (!Thunk.isEmpty()) {
3061 assert(Thunk.Return.isEmpty() &&
3062 "No return adjustment needed for destructors!");
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00003063 dumpMicrosoftThunkAdjustment(Thunk, Out, /*ContinueFirstLine=*/false);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003064 }
3065
3066 break;
3067 }
3068
3069 default:
3070 DiagnosticsEngine &Diags = Context.getDiagnostics();
3071 unsigned DiagID = Diags.getCustomDiagID(
3072 DiagnosticsEngine::Error,
3073 "Unexpected vftable component type %0 for component number %1");
3074 Diags.Report(MostDerivedClass->getLocation(), DiagID)
3075 << I << Component.getKind();
3076 }
3077
3078 Out << '\n';
3079 }
3080
3081 Out << '\n';
3082
3083 if (!Thunks.empty()) {
3084 // We store the method names in a map to get a stable order.
3085 std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls;
3086
3087 for (ThunksMapTy::const_iterator I = Thunks.begin(), E = Thunks.end();
3088 I != E; ++I) {
3089 const CXXMethodDecl *MD = I->first;
3090 std::string MethodName = PredefinedExpr::ComputeName(
3091 PredefinedExpr::PrettyFunctionNoVirtual, MD);
3092
3093 MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
3094 }
3095
3096 for (std::map<std::string, const CXXMethodDecl *>::const_iterator
3097 I = MethodNamesAndDecls.begin(),
3098 E = MethodNamesAndDecls.end();
3099 I != E; ++I) {
3100 const std::string &MethodName = I->first;
3101 const CXXMethodDecl *MD = I->second;
3102
3103 ThunkInfoVectorTy ThunksVector = Thunks[MD];
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +00003104 std::stable_sort(ThunksVector.begin(), ThunksVector.end(),
Benjamin Kramerbbdd7642014-03-01 14:48:57 +00003105 [](const ThunkInfo &LHS, const ThunkInfo &RHS) {
3106 // Keep different thunks with the same adjustments in the order they
3107 // were put into the vector.
Benjamin Kramera741b8c2014-03-03 20:26:46 +00003108 return std::tie(LHS.This, LHS.Return) < std::tie(RHS.This, RHS.Return);
Benjamin Kramerbbdd7642014-03-01 14:48:57 +00003109 });
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003110
3111 Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
3112 Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n";
3113
3114 for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) {
3115 const ThunkInfo &Thunk = ThunksVector[I];
3116
3117 Out << llvm::format("%4d | ", I);
Timur Iskhodzhanov02014322013-10-30 11:55:43 +00003118 dumpMicrosoftThunkAdjustment(Thunk, Out, /*ContinueFirstLine=*/true);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003119 Out << '\n';
3120 }
3121
3122 Out << '\n';
3123 }
3124 }
Timur Iskhodzhanov4fea4f92014-03-20 13:42:14 +00003125
3126 Out.flush();
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003127}
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003128
Reid Kleckner5f080942014-01-03 23:42:00 +00003129static bool setsIntersect(const llvm::SmallPtrSet<const CXXRecordDecl *, 4> &A,
Craig Topper00bbdcf2014-06-28 23:22:23 +00003130 const ArrayRef<const CXXRecordDecl *> &B) {
3131 for (ArrayRef<const CXXRecordDecl *>::iterator I = B.begin(), E = B.end();
Reid Kleckner5f080942014-01-03 23:42:00 +00003132 I != E; ++I) {
3133 if (A.count(*I))
3134 return true;
3135 }
3136 return false;
3137}
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003138
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003139static bool rebucketPaths(VPtrInfoVector &Paths);
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003140
Reid Kleckner5f080942014-01-03 23:42:00 +00003141/// Produces MSVC-compatible vbtable data. The symbols produced by this
3142/// algorithm match those produced by MSVC 2012 and newer, which is different
3143/// from MSVC 2010.
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003144///
3145/// MSVC 2012 appears to minimize the vbtable names using the following
3146/// algorithm. First, walk the class hierarchy in the usual order, depth first,
3147/// left to right, to find all of the subobjects which contain a vbptr field.
3148/// Visiting each class node yields a list of inheritance paths to vbptrs. Each
3149/// record with a vbptr creates an initially empty path.
3150///
3151/// To combine paths from child nodes, the paths are compared to check for
3152/// ambiguity. Paths are "ambiguous" if multiple paths have the same set of
3153/// components in the same order. Each group of ambiguous paths is extended by
3154/// appending the class of the base from which it came. If the current class
3155/// node produced an ambiguous path, its path is extended with the current class.
3156/// After extending paths, MSVC again checks for ambiguity, and extends any
3157/// ambiguous path which wasn't already extended. Because each node yields an
3158/// unambiguous set of paths, MSVC doesn't need to extend any path more than once
3159/// to produce an unambiguous set of paths.
3160///
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003161/// TODO: Presumably vftables use the same algorithm.
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003162void MicrosoftVTableContext::computeVTablePaths(bool ForVBTables,
3163 const CXXRecordDecl *RD,
3164 VPtrInfoVector &Paths) {
Reid Kleckner5f080942014-01-03 23:42:00 +00003165 assert(Paths.empty());
3166 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003167
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003168 // Base case: this subobject has its own vptr.
3169 if (ForVBTables ? Layout.hasOwnVBPtr() : Layout.hasOwnVFPtr())
3170 Paths.push_back(new VPtrInfo(RD));
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003171
Reid Kleckner5f080942014-01-03 23:42:00 +00003172 // Recursive case: get all the vbtables from our bases and remove anything
3173 // that shares a virtual base.
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003174 llvm::SmallPtrSet<const CXXRecordDecl*, 4> VBasesSeen;
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00003175 for (const auto &B : RD->bases()) {
3176 const CXXRecordDecl *Base = B.getType()->getAsCXXRecordDecl();
3177 if (B.isVirtual() && VBasesSeen.count(Base))
Reid Kleckner5f080942014-01-03 23:42:00 +00003178 continue;
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003179
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003180 if (!Base->isDynamicClass())
3181 continue;
Reid Kleckner5f080942014-01-03 23:42:00 +00003182
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003183 const VPtrInfoVector &BasePaths =
3184 ForVBTables ? enumerateVBTables(Base) : getVFPtrOffsets(Base);
3185
Reid Klecknerfd385402014-04-17 22:47:52 +00003186 for (VPtrInfo *BaseInfo : BasePaths) {
Reid Kleckner5f080942014-01-03 23:42:00 +00003187 // Don't include the path if it goes through a virtual base that we've
3188 // already included.
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003189 if (setsIntersect(VBasesSeen, BaseInfo->ContainingVBases))
Reid Kleckner5f080942014-01-03 23:42:00 +00003190 continue;
3191
3192 // Copy the path and adjust it as necessary.
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003193 VPtrInfo *P = new VPtrInfo(*BaseInfo);
Reid Kleckner5f080942014-01-03 23:42:00 +00003194
3195 // We mangle Base into the path if the path would've been ambiguous and it
3196 // wasn't already extended with Base.
3197 if (P->MangledPath.empty() || P->MangledPath.back() != Base)
3198 P->NextBaseToMangle = Base;
3199
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003200 // Keep track of the full path.
3201 // FIXME: Why do we need this?
3202 P->PathToBaseWithVPtr.insert(P->PathToBaseWithVPtr.begin(), Base);
3203
Reid Klecknerfd385402014-04-17 22:47:52 +00003204 // Keep track of which vtable the derived class is going to extend with
3205 // new methods or bases. We append to either the vftable of our primary
3206 // base, or the first non-virtual base that has a vbtable.
3207 if (P->ReusingBase == Base &&
3208 Base == (ForVBTables ? Layout.getBaseSharingVBPtr()
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003209 : Layout.getPrimaryBase()))
Reid Kleckner5f080942014-01-03 23:42:00 +00003210 P->ReusingBase = RD;
Reid Klecknerfd385402014-04-17 22:47:52 +00003211
3212 // Keep track of the full adjustment from the MDC to this vtable. The
3213 // adjustment is captured by an optional vbase and a non-virtual offset.
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00003214 if (B.isVirtual())
Reid Kleckner5f080942014-01-03 23:42:00 +00003215 P->ContainingVBases.push_back(Base);
3216 else if (P->ContainingVBases.empty())
3217 P->NonVirtualOffset += Layout.getBaseClassOffset(Base);
3218
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003219 // Update the full offset in the MDC.
3220 P->FullOffsetInMDC = P->NonVirtualOffset;
3221 if (const CXXRecordDecl *VB = P->getVBaseWithVPtr())
3222 P->FullOffsetInMDC += Layout.getVBaseClassOffset(VB);
3223
Reid Kleckner5f080942014-01-03 23:42:00 +00003224 Paths.push_back(P);
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003225 }
3226
Timur Iskhodzhanov9ae7d3b2014-03-31 11:01:51 +00003227 if (B.isVirtual())
3228 VBasesSeen.insert(Base);
3229
Reid Kleckner5f080942014-01-03 23:42:00 +00003230 // After visiting any direct base, we've transitively visited all of its
3231 // morally virtual bases.
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00003232 for (const auto &VB : Base->vbases())
3233 VBasesSeen.insert(VB.getType()->getAsCXXRecordDecl());
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003234 }
3235
Reid Kleckner5f080942014-01-03 23:42:00 +00003236 // Sort the paths into buckets, and if any of them are ambiguous, extend all
3237 // paths in ambiguous buckets.
3238 bool Changed = true;
3239 while (Changed)
3240 Changed = rebucketPaths(Paths);
3241}
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003242
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003243static bool extendPath(VPtrInfo *P) {
Reid Kleckner5f080942014-01-03 23:42:00 +00003244 if (P->NextBaseToMangle) {
3245 P->MangledPath.push_back(P->NextBaseToMangle);
Craig Topper36250ad2014-05-12 05:36:57 +00003246 P->NextBaseToMangle = nullptr;// Prevent the path from being extended twice.
Reid Kleckner5f080942014-01-03 23:42:00 +00003247 return true;
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003248 }
Reid Kleckner5f080942014-01-03 23:42:00 +00003249 return false;
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003250}
3251
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003252static bool rebucketPaths(VPtrInfoVector &Paths) {
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003253 // What we're essentially doing here is bucketing together ambiguous paths.
3254 // Any bucket with more than one path in it gets extended by NextBase, which
3255 // is usually the direct base of the inherited the vbptr. This code uses a
3256 // sorted vector to implement a multiset to form the buckets. Note that the
3257 // ordering is based on pointers, but it doesn't change our output order. The
3258 // current algorithm is designed to match MSVC 2012's names.
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003259 VPtrInfoVector PathsSorted(Paths);
Benjamin Kramer15ae7832014-03-07 21:35:40 +00003260 std::sort(PathsSorted.begin(), PathsSorted.end(),
3261 [](const VPtrInfo *LHS, const VPtrInfo *RHS) {
3262 return LHS->MangledPath < RHS->MangledPath;
3263 });
Reid Kleckner5f080942014-01-03 23:42:00 +00003264 bool Changed = false;
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003265 for (size_t I = 0, E = PathsSorted.size(); I != E;) {
3266 // Scan forward to find the end of the bucket.
3267 size_t BucketStart = I;
3268 do {
3269 ++I;
Reid Kleckner5f080942014-01-03 23:42:00 +00003270 } while (I != E && PathsSorted[BucketStart]->MangledPath ==
3271 PathsSorted[I]->MangledPath);
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003272
3273 // If this bucket has multiple paths, extend them all.
3274 if (I - BucketStart > 1) {
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003275 for (size_t II = BucketStart; II != I; ++II)
Reid Kleckner5f080942014-01-03 23:42:00 +00003276 Changed |= extendPath(PathsSorted[II]);
3277 assert(Changed && "no paths were extended to fix ambiguity");
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003278 }
3279 }
Reid Kleckner5f080942014-01-03 23:42:00 +00003280 return Changed;
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003281}
3282
3283MicrosoftVTableContext::~MicrosoftVTableContext() {
Nico Weberd19e6a72014-04-24 19:52:12 +00003284 for (auto &P : VFPtrLocations)
3285 llvm::DeleteContainerPointers(*P.second);
Reid Kleckner33311282014-02-28 23:26:22 +00003286 llvm::DeleteContainerSeconds(VFPtrLocations);
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003287 llvm::DeleteContainerSeconds(VFTableLayouts);
3288 llvm::DeleteContainerSeconds(VBaseInfo);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003289}
3290
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003291void MicrosoftVTableContext::computeVTableRelatedInformation(
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003292 const CXXRecordDecl *RD) {
3293 assert(RD->isDynamicClass());
3294
3295 // Check if we've computed this information before.
3296 if (VFPtrLocations.count(RD))
3297 return;
3298
3299 const VTableLayout::AddressPointsMapTy EmptyAddressPointsMap;
3300
Reid Klecknerd6f9b832014-02-27 22:51:43 +00003301 VPtrInfoVector *VFPtrs = new VPtrInfoVector();
3302 computeVTablePaths(/*ForVBTables=*/false, RD, *VFPtrs);
3303 VFPtrLocations[RD] = VFPtrs;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003304
3305 MethodVFTableLocationsTy NewMethodLocations;
Reid Klecknerd6f9b832014-02-27 22:51:43 +00003306 for (VPtrInfoVector::iterator I = VFPtrs->begin(), E = VFPtrs->end();
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003307 I != E; ++I) {
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003308 VFTableBuilder Builder(*this, RD, *I);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003309
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003310 VFTableIdTy id(RD, (*I)->FullOffsetInMDC);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003311 assert(VFTableLayouts.count(id) == 0);
3312 SmallVector<VTableLayout::VTableThunkTy, 1> VTableThunks(
3313 Builder.vtable_thunks_begin(), Builder.vtable_thunks_end());
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003314 VFTableLayouts[id] = new VTableLayout(
3315 Builder.getNumVTableComponents(), Builder.vtable_component_begin(),
3316 VTableThunks.size(), VTableThunks.data(), EmptyAddressPointsMap, true);
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003317 Thunks.insert(Builder.thunks_begin(), Builder.thunks_end());
Timur Iskhodzhanovba557022014-03-20 20:38:34 +00003318
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00003319 for (const auto &Loc : Builder.vtable_locations()) {
3320 GlobalDecl GD = Loc.first;
3321 MethodVFTableLocation NewLoc = Loc.second;
Timur Iskhodzhanovba557022014-03-20 20:38:34 +00003322 auto M = NewMethodLocations.find(GD);
3323 if (M == NewMethodLocations.end() || NewLoc < M->second)
3324 NewMethodLocations[GD] = NewLoc;
3325 }
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003326 }
3327
3328 MethodVFTableLocations.insert(NewMethodLocations.begin(),
3329 NewMethodLocations.end());
3330 if (Context.getLangOpts().DumpVTableLayouts)
Reid Kleckner5bc6d0f2013-11-08 21:28:00 +00003331 dumpMethodLocations(RD, NewMethodLocations, llvm::outs());
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003332}
3333
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003334void MicrosoftVTableContext::dumpMethodLocations(
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003335 const CXXRecordDecl *RD, const MethodVFTableLocationsTy &NewMethods,
3336 raw_ostream &Out) {
3337 // Compute the vtable indices for all the member functions.
3338 // Store them in a map keyed by the location so we'll get a sorted table.
3339 std::map<MethodVFTableLocation, std::string> IndicesMap;
3340 bool HasNonzeroOffset = false;
3341
3342 for (MethodVFTableLocationsTy::const_iterator I = NewMethods.begin(),
3343 E = NewMethods.end(); I != E; ++I) {
3344 const CXXMethodDecl *MD = cast<const CXXMethodDecl>(I->first.getDecl());
3345 assert(MD->isVirtual());
3346
3347 std::string MethodName = PredefinedExpr::ComputeName(
3348 PredefinedExpr::PrettyFunctionNoVirtual, MD);
3349
3350 if (isa<CXXDestructorDecl>(MD)) {
3351 IndicesMap[I->second] = MethodName + " [scalar deleting]";
3352 } else {
3353 IndicesMap[I->second] = MethodName;
3354 }
3355
Timur Iskhodzhanov9e7f5052013-11-07 13:34:02 +00003356 if (!I->second.VFPtrOffset.isZero() || I->second.VBTableIndex != 0)
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003357 HasNonzeroOffset = true;
3358 }
3359
3360 // Print the vtable indices for all the member functions.
3361 if (!IndicesMap.empty()) {
3362 Out << "VFTable indices for ";
Aaron Ballman75ee4cc2014-01-03 18:42:48 +00003363 Out << "'";
3364 RD->printQualifiedName(Out);
Timur Iskhodzhanov77764b62014-03-05 13:54:07 +00003365 Out << "' (" << IndicesMap.size()
3366 << (IndicesMap.size() == 1 ? " entry" : " entries") << ").\n";
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003367
3368 CharUnits LastVFPtrOffset = CharUnits::fromQuantity(-1);
3369 uint64_t LastVBIndex = 0;
3370 for (std::map<MethodVFTableLocation, std::string>::const_iterator
3371 I = IndicesMap.begin(),
3372 E = IndicesMap.end();
3373 I != E; ++I) {
Timur Iskhodzhanov9e7f5052013-11-07 13:34:02 +00003374 CharUnits VFPtrOffset = I->first.VFPtrOffset;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003375 uint64_t VBIndex = I->first.VBTableIndex;
3376 if (HasNonzeroOffset &&
3377 (VFPtrOffset != LastVFPtrOffset || VBIndex != LastVBIndex)) {
3378 assert(VBIndex > LastVBIndex || VFPtrOffset > LastVFPtrOffset);
3379 Out << " -- accessible via ";
3380 if (VBIndex)
3381 Out << "vbtable index " << VBIndex << ", ";
3382 Out << "vfptr at offset " << VFPtrOffset.getQuantity() << " --\n";
3383 LastVFPtrOffset = VFPtrOffset;
3384 LastVBIndex = VBIndex;
3385 }
3386
3387 uint64_t VTableIndex = I->first.Index;
3388 const std::string &MethodName = I->second;
3389 Out << llvm::format("%4" PRIu64 " | ", VTableIndex) << MethodName << '\n';
3390 }
3391 Out << '\n';
3392 }
Timur Iskhodzhanov4fea4f92014-03-20 13:42:14 +00003393
3394 Out.flush();
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003395}
3396
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003397const VirtualBaseInfo *MicrosoftVTableContext::computeVBTableRelatedInformation(
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003398 const CXXRecordDecl *RD) {
Reid Kleckner5f080942014-01-03 23:42:00 +00003399 VirtualBaseInfo *VBI;
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003400
Reid Kleckner5f080942014-01-03 23:42:00 +00003401 {
3402 // Get or create a VBI for RD. Don't hold a reference to the DenseMap cell,
3403 // as it may be modified and rehashed under us.
3404 VirtualBaseInfo *&Entry = VBaseInfo[RD];
3405 if (Entry)
3406 return Entry;
3407 Entry = VBI = new VirtualBaseInfo();
3408 }
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003409
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003410 computeVTablePaths(/*ForVBTables=*/true, RD, VBI->VBPtrPaths);
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003411
Timur Iskhodzhanov2c9341f2013-11-08 11:45:35 +00003412 // First, see if the Derived class shared the vbptr with a non-virtual base.
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003413 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Timur Iskhodzhanov2c9341f2013-11-08 11:45:35 +00003414 if (const CXXRecordDecl *VBPtrBase = Layout.getBaseSharingVBPtr()) {
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003415 // If the Derived class shares the vbptr with a non-virtual base, the shared
3416 // virtual bases come first so that the layout is the same.
3417 const VirtualBaseInfo *BaseInfo =
3418 computeVBTableRelatedInformation(VBPtrBase);
Reid Kleckner5f080942014-01-03 23:42:00 +00003419 VBI->VBTableIndices.insert(BaseInfo->VBTableIndices.begin(),
3420 BaseInfo->VBTableIndices.end());
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003421 }
3422
3423 // New vbases are added to the end of the vbtable.
3424 // Skip the self entry and vbases visited in the non-virtual base, if any.
Reid Kleckner5f080942014-01-03 23:42:00 +00003425 unsigned VBTableIndex = 1 + VBI->VBTableIndices.size();
Timur Iskhodzhanov1523c612014-03-26 08:22:48 +00003426 for (const auto &VB : RD->vbases()) {
3427 const CXXRecordDecl *CurVBase = VB.getType()->getAsCXXRecordDecl();
Reid Kleckner5f080942014-01-03 23:42:00 +00003428 if (!VBI->VBTableIndices.count(CurVBase))
3429 VBI->VBTableIndices[CurVBase] = VBTableIndex++;
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003430 }
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003431
Reid Kleckner5f080942014-01-03 23:42:00 +00003432 return VBI;
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003433}
3434
3435unsigned MicrosoftVTableContext::getVBTableIndex(const CXXRecordDecl *Derived,
3436 const CXXRecordDecl *VBase) {
3437 const VirtualBaseInfo *VBInfo = computeVBTableRelatedInformation(Derived);
3438 assert(VBInfo->VBTableIndices.count(VBase));
3439 return VBInfo->VBTableIndices.find(VBase)->second;
3440}
3441
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003442const VPtrInfoVector &
Reid Klecknerb40a27d2014-01-03 00:14:35 +00003443MicrosoftVTableContext::enumerateVBTables(const CXXRecordDecl *RD) {
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003444 return computeVBTableRelatedInformation(RD)->VBPtrPaths;
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003445}
3446
Reid Kleckner9c6e9e32014-02-27 19:40:09 +00003447const VPtrInfoVector &
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003448MicrosoftVTableContext::getVFPtrOffsets(const CXXRecordDecl *RD) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003449 computeVTableRelatedInformation(RD);
3450
3451 assert(VFPtrLocations.count(RD) && "Couldn't find vfptr locations");
Reid Klecknerd6f9b832014-02-27 22:51:43 +00003452 return *VFPtrLocations[RD];
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003453}
3454
3455const VTableLayout &
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003456MicrosoftVTableContext::getVFTableLayout(const CXXRecordDecl *RD,
3457 CharUnits VFPtrOffset) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003458 computeVTableRelatedInformation(RD);
3459
3460 VFTableIdTy id(RD, VFPtrOffset);
3461 assert(VFTableLayouts.count(id) && "Couldn't find a VFTable at this offset");
3462 return *VFTableLayouts[id];
3463}
3464
Timur Iskhodzhanov58776632013-11-05 15:54:58 +00003465const MicrosoftVTableContext::MethodVFTableLocation &
3466MicrosoftVTableContext::getMethodVFTableLocation(GlobalDecl GD) {
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +00003467 assert(cast<CXXMethodDecl>(GD.getDecl())->isVirtual() &&
3468 "Only use this method for virtual methods or dtors");
3469 if (isa<CXXDestructorDecl>(GD.getDecl()))
3470 assert(GD.getDtorType() == Dtor_Deleting);
3471
3472 MethodVFTableLocationsTy::iterator I = MethodVFTableLocations.find(GD);
3473 if (I != MethodVFTableLocations.end())
3474 return I->second;
3475
3476 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
3477
3478 computeVTableRelatedInformation(RD);
3479
3480 I = MethodVFTableLocations.find(GD);
3481 assert(I != MethodVFTableLocations.end() && "Did not find index!");
3482 return I->second;
3483}