xref: /freebsd/contrib/llvm-project/clang/lib/AST/VTableBuilder.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===--- VTableBuilder.cpp - C++ vtable layout builder --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This contains code dealing with generation of the layout of virtual tables.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "clang/AST/VTableBuilder.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/ASTDiagnostic.h"
16 #include "clang/AST/CXXInheritance.h"
17 #include "clang/AST/RecordLayout.h"
18 #include "clang/Basic/TargetInfo.h"
19 #include "llvm/ADT/SetOperations.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <algorithm>
25 #include <cstdio>
26 
27 using namespace clang;
28 
29 #define DUMP_OVERRIDERS 0
30 
31 namespace {
32 
33 /// BaseOffset - Represents an offset from a derived class to a direct or
34 /// indirect base class.
35 struct BaseOffset {
36   /// DerivedClass - The derived class.
37   const CXXRecordDecl *DerivedClass;
38 
39   /// VirtualBase - If the path from the derived class to the base class
40   /// involves virtual base classes, this holds the declaration of the last
41   /// virtual base in this path (i.e. closest to the base class).
42   const CXXRecordDecl *VirtualBase;
43 
44   /// NonVirtualOffset - The offset from the derived class to the base class.
45   /// (Or the offset from the virtual base class to the base class, if the
46   /// path from the derived class to the base class involves a virtual base
47   /// class.
48   CharUnits NonVirtualOffset;
49 
BaseOffset__anon1858bcf60111::BaseOffset50   BaseOffset() : DerivedClass(nullptr), VirtualBase(nullptr),
51                  NonVirtualOffset(CharUnits::Zero()) { }
BaseOffset__anon1858bcf60111::BaseOffset52   BaseOffset(const CXXRecordDecl *DerivedClass,
53              const CXXRecordDecl *VirtualBase, CharUnits NonVirtualOffset)
54     : DerivedClass(DerivedClass), VirtualBase(VirtualBase),
55     NonVirtualOffset(NonVirtualOffset) { }
56 
isEmpty__anon1858bcf60111::BaseOffset57   bool isEmpty() const { return NonVirtualOffset.isZero() && !VirtualBase; }
58 };
59 
60 /// FinalOverriders - Contains the final overrider member functions for all
61 /// member functions in the base subobjects of a class.
62 class FinalOverriders {
63 public:
64   /// OverriderInfo - Information about a final overrider.
65   struct OverriderInfo {
66     /// Method - The method decl of the overrider.
67     const CXXMethodDecl *Method;
68 
69     /// VirtualBase - The virtual base class subobject of this overrider.
70     /// Note that this records the closest derived virtual base class subobject.
71     const CXXRecordDecl *VirtualBase;
72 
73     /// Offset - the base offset of the overrider's parent in the layout class.
74     CharUnits Offset;
75 
OverriderInfo__anon1858bcf60111::FinalOverriders::OverriderInfo76     OverriderInfo() : Method(nullptr), VirtualBase(nullptr),
77                       Offset(CharUnits::Zero()) { }
78   };
79 
80 private:
81   /// MostDerivedClass - The most derived class for which the final overriders
82   /// are stored.
83   const CXXRecordDecl *MostDerivedClass;
84 
85   /// MostDerivedClassOffset - If we're building final overriders for a
86   /// construction vtable, this holds the offset from the layout class to the
87   /// most derived class.
88   const CharUnits MostDerivedClassOffset;
89 
90   /// LayoutClass - The class we're using for layout information. Will be
91   /// different than the most derived class if the final overriders are for a
92   /// construction vtable.
93   const CXXRecordDecl *LayoutClass;
94 
95   ASTContext &Context;
96 
97   /// MostDerivedClassLayout - the AST record layout of the most derived class.
98   const ASTRecordLayout &MostDerivedClassLayout;
99 
100   /// MethodBaseOffsetPairTy - Uniquely identifies a member function
101   /// in a base subobject.
102   typedef std::pair<const CXXMethodDecl *, CharUnits> MethodBaseOffsetPairTy;
103 
104   typedef llvm::DenseMap<MethodBaseOffsetPairTy,
105                          OverriderInfo> OverridersMapTy;
106 
107   /// OverridersMap - The final overriders for all virtual member functions of
108   /// all the base subobjects of the most derived class.
109   OverridersMapTy OverridersMap;
110 
111   /// SubobjectsToOffsetsMapTy - A mapping from a base subobject (represented
112   /// as a record decl and a subobject number) and its offsets in the most
113   /// derived class as well as the layout class.
114   typedef llvm::DenseMap<std::pair<const CXXRecordDecl *, unsigned>,
115                          CharUnits> SubobjectOffsetMapTy;
116 
117   typedef llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCountMapTy;
118 
119   /// ComputeBaseOffsets - Compute the offsets for all base subobjects of the
120   /// given base.
121   void ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual,
122                           CharUnits OffsetInLayoutClass,
123                           SubobjectOffsetMapTy &SubobjectOffsets,
124                           SubobjectOffsetMapTy &SubobjectLayoutClassOffsets,
125                           SubobjectCountMapTy &SubobjectCounts);
126 
127   typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
128 
129   /// dump - dump the final overriders for a base subobject, and all its direct
130   /// and indirect base subobjects.
131   void dump(raw_ostream &Out, BaseSubobject Base,
132             VisitedVirtualBasesSetTy& VisitedVirtualBases);
133 
134 public:
135   FinalOverriders(const CXXRecordDecl *MostDerivedClass,
136                   CharUnits MostDerivedClassOffset,
137                   const CXXRecordDecl *LayoutClass);
138 
139   /// getOverrider - Get the final overrider for the given method declaration in
140   /// the subobject with the given base offset.
getOverrider(const CXXMethodDecl * MD,CharUnits BaseOffset) const141   OverriderInfo getOverrider(const CXXMethodDecl *MD,
142                              CharUnits BaseOffset) const {
143     assert(OverridersMap.count(std::make_pair(MD, BaseOffset)) &&
144            "Did not find overrider!");
145 
146     return OverridersMap.lookup(std::make_pair(MD, BaseOffset));
147   }
148 
149   /// dump - dump the final overriders.
dump()150   void dump() {
151     VisitedVirtualBasesSetTy VisitedVirtualBases;
152     dump(llvm::errs(), BaseSubobject(MostDerivedClass, CharUnits::Zero()),
153          VisitedVirtualBases);
154   }
155 
156 };
157 
FinalOverriders(const CXXRecordDecl * MostDerivedClass,CharUnits MostDerivedClassOffset,const CXXRecordDecl * LayoutClass)158 FinalOverriders::FinalOverriders(const CXXRecordDecl *MostDerivedClass,
159                                  CharUnits MostDerivedClassOffset,
160                                  const CXXRecordDecl *LayoutClass)
161   : MostDerivedClass(MostDerivedClass),
162   MostDerivedClassOffset(MostDerivedClassOffset), LayoutClass(LayoutClass),
163   Context(MostDerivedClass->getASTContext()),
164   MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)) {
165 
166   // Compute base offsets.
167   SubobjectOffsetMapTy SubobjectOffsets;
168   SubobjectOffsetMapTy SubobjectLayoutClassOffsets;
169   SubobjectCountMapTy SubobjectCounts;
170   ComputeBaseOffsets(BaseSubobject(MostDerivedClass, CharUnits::Zero()),
171                      /*IsVirtual=*/false,
172                      MostDerivedClassOffset,
173                      SubobjectOffsets, SubobjectLayoutClassOffsets,
174                      SubobjectCounts);
175 
176   // Get the final overriders.
177   CXXFinalOverriderMap FinalOverriders;
178   MostDerivedClass->getFinalOverriders(FinalOverriders);
179 
180   for (const auto &Overrider : FinalOverriders) {
181     const CXXMethodDecl *MD = Overrider.first;
182     const OverridingMethods &Methods = Overrider.second;
183 
184     for (const auto &M : Methods) {
185       unsigned SubobjectNumber = M.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(M.second.size() == 1 && "Final overrider is not unique!");
194       const UniqueVirtualMethod &Method = M.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;
209       Overrider.VirtualBase = Method.InVirtualSubobject;
210     }
211   }
212 
213 #if DUMP_OVERRIDERS
214   // And dump them (for now).
215   dump();
216 #endif
217 }
218 
ComputeBaseOffset(const ASTContext & Context,const CXXRecordDecl * DerivedRD,const CXXBasePath & Path)219 static BaseOffset ComputeBaseOffset(const ASTContext &Context,
220                                     const CXXRecordDecl *DerivedRD,
221                                     const CXXBasePath &Path) {
222   CharUnits NonVirtualOffset = CharUnits::Zero();
223 
224   unsigned NonVirtualStart = 0;
225   const CXXRecordDecl *VirtualBase = nullptr;
226 
227   // First, look for the virtual base class.
228   for (int I = Path.size(), E = 0; I != E; --I) {
229     const CXXBasePathElement &Element = Path[I - 1];
230 
231     if (Element.Base->isVirtual()) {
232       NonVirtualStart = I;
233       QualType VBaseType = Element.Base->getType();
234       VirtualBase = VBaseType->getAsCXXRecordDecl();
235       break;
236     }
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 
246     const CXXRecordDecl *Base = Element.Base->getType()->getAsCXXRecordDecl();
247 
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 
ComputeBaseOffset(const ASTContext & Context,const CXXRecordDecl * BaseRD,const CXXRecordDecl * DerivedRD)258 static BaseOffset ComputeBaseOffset(const ASTContext &Context,
259                                     const CXXRecordDecl *BaseRD,
260                                     const CXXRecordDecl *DerivedRD) {
261   CXXBasePaths Paths(/*FindAmbiguities=*/false,
262                      /*RecordPaths=*/true, /*DetectVirtual=*/false);
263 
264   if (!DerivedRD->isDerivedFrom(BaseRD, Paths))
265     llvm_unreachable("Class must be derived from the passed in base class!");
266 
267   return ComputeBaseOffset(Context, DerivedRD, Paths.front());
268 }
269 
270 static BaseOffset
ComputeReturnAdjustmentBaseOffset(ASTContext & Context,const CXXMethodDecl * DerivedMD,const CXXMethodDecl * BaseMD)271 ComputeReturnAdjustmentBaseOffset(ASTContext &Context,
272                                   const CXXMethodDecl *DerivedMD,
273                                   const CXXMethodDecl *BaseMD) {
274   const auto *BaseFT = BaseMD->getType()->castAs<FunctionType>();
275   const auto *DerivedFT = DerivedMD->getType()->castAs<FunctionType>();
276 
277   // Canonicalize the return types.
278   CanQualType CanDerivedReturnType =
279       Context.getCanonicalType(DerivedFT->getReturnType());
280   CanQualType CanBaseReturnType =
281       Context.getCanonicalType(BaseFT->getReturnType());
282 
283   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 
324 void
ComputeBaseOffsets(BaseSubobject Base,bool IsVirtual,CharUnits OffsetInLayoutClass,SubobjectOffsetMapTy & SubobjectOffsets,SubobjectOffsetMapTy & SubobjectLayoutClassOffsets,SubobjectCountMapTy & SubobjectCounts)325 FinalOverriders::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.
347   for (const auto &B : RD->bases()) {
348     const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
349 
350     CharUnits BaseOffset;
351     CharUnits BaseOffsetInLayoutClass;
352     if (B.isVirtual()) {
353       // 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),
372                        B.isVirtual(), BaseOffsetInLayoutClass,
373                        SubobjectOffsets, SubobjectLayoutClassOffsets,
374                        SubobjectCounts);
375   }
376 }
377 
dump(raw_ostream & Out,BaseSubobject Base,VisitedVirtualBasesSetTy & VisitedVirtualBases)378 void FinalOverriders::dump(raw_ostream &Out, BaseSubobject Base,
379                            VisitedVirtualBasesSetTy &VisitedVirtualBases) {
380   const CXXRecordDecl *RD = Base.getBase();
381   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
382 
383   for (const auto &B : RD->bases()) {
384     const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
385 
386     // Ignore bases that don't have any virtual member functions.
387     if (!BaseDecl->isPolymorphic())
388       continue;
389 
390     CharUnits BaseOffset;
391     if (B.isVirtual()) {
392       if (!VisitedVirtualBases.insert(BaseDecl).second) {
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 
405   Out << "Final overriders for (";
406   RD->printQualifiedName(Out);
407   Out << ", ";
408   Out << Base.getBaseOffset().getQuantity() << ")\n";
409 
410   // Now dump the overriders for this base subobject.
411   for (const auto *MD : RD->methods()) {
412     if (!VTableContextBase::hasVtableSlot(MD))
413       continue;
414     MD = MD->getCanonicalDecl();
415 
416     OverriderInfo Overrider = getOverrider(MD, Base.getBaseOffset());
417 
418     Out << "  ";
419     MD->printQualifiedName(Out);
420     Out << " - (";
421     Overrider.Method->printQualifiedName(Out);
422     Out << ", " << Overrider.Offset.getQuantity() << ')';
423 
424     BaseOffset Offset;
425     if (!Overrider.Method->isPureVirtual())
426       Offset = ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD);
427 
428     if (!Offset.isEmpty()) {
429       Out << " [ret-adj: ";
430       if (Offset.VirtualBase) {
431         Offset.VirtualBase->printQualifiedName(Out);
432         Out << " vbase, ";
433       }
434 
435       Out << Offset.NonVirtualOffset.getQuantity() << " nv]";
436     }
437 
438     Out << "\n";
439   }
440 }
441 
442 /// VCallOffsetMap - Keeps track of vcall offsets when building a vtable.
443 struct VCallOffsetMap {
444 
445   typedef std::pair<const CXXMethodDecl *, CharUnits> MethodAndOffsetPairTy;
446 
447   /// Offsets - Keeps track of methods and their offsets.
448   // FIXME: This should be a real map and not a vector.
449   SmallVector<MethodAndOffsetPairTy, 16> Offsets;
450 
451   /// MethodsCanShareVCallOffset - Returns whether two virtual member functions
452   /// can share the same vcall offset.
453   static bool MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
454                                          const CXXMethodDecl *RHS);
455 
456 public:
457   /// AddVCallOffset - Adds a vcall offset to the map. Returns true if the
458   /// add was successful, or false if there was already a member function with
459   /// the same signature in the map.
460   bool AddVCallOffset(const CXXMethodDecl *MD, CharUnits OffsetOffset);
461 
462   /// getVCallOffsetOffset - Returns the vcall offset offset (relative to the
463   /// vtable address point) for the given virtual member function.
464   CharUnits getVCallOffsetOffset(const CXXMethodDecl *MD);
465 
466   // empty - Return whether the offset map is empty or not.
empty__anon1858bcf60111::VCallOffsetMap467   bool empty() const { return Offsets.empty(); }
468 };
469 
HasSameVirtualSignature(const CXXMethodDecl * LHS,const CXXMethodDecl * RHS)470 static bool HasSameVirtualSignature(const CXXMethodDecl *LHS,
471                                     const CXXMethodDecl *RHS) {
472   const FunctionProtoType *LT =
473     cast<FunctionProtoType>(LHS->getType().getCanonicalType());
474   const FunctionProtoType *RT =
475     cast<FunctionProtoType>(RHS->getType().getCanonicalType());
476 
477   // Fast-path matches in the canonical types.
478   if (LT == RT) return true;
479 
480   // Force the signatures to match.  We can't rely on the overrides
481   // list here because there isn't necessarily an inheritance
482   // relationship between the two methods.
483   if (LT->getMethodQuals() != RT->getMethodQuals())
484     return false;
485   return LT->getParamTypes() == RT->getParamTypes();
486 }
487 
MethodsCanShareVCallOffset(const CXXMethodDecl * LHS,const CXXMethodDecl * RHS)488 bool VCallOffsetMap::MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
489                                                 const CXXMethodDecl *RHS) {
490   assert(VTableContextBase::hasVtableSlot(LHS) && "LHS must be virtual!");
491   assert(VTableContextBase::hasVtableSlot(RHS) && "RHS must be virtual!");
492 
493   // A destructor can share a vcall offset with another destructor.
494   if (isa<CXXDestructorDecl>(LHS))
495     return isa<CXXDestructorDecl>(RHS);
496 
497   // FIXME: We need to check more things here.
498 
499   // The methods must have the same name.
500   DeclarationName LHSName = LHS->getDeclName();
501   DeclarationName RHSName = RHS->getDeclName();
502   if (LHSName != RHSName)
503     return false;
504 
505   // And the same signatures.
506   return HasSameVirtualSignature(LHS, RHS);
507 }
508 
AddVCallOffset(const CXXMethodDecl * MD,CharUnits OffsetOffset)509 bool VCallOffsetMap::AddVCallOffset(const CXXMethodDecl *MD,
510                                     CharUnits OffsetOffset) {
511   // Check if we can reuse an offset.
512   for (const auto &OffsetPair : Offsets) {
513     if (MethodsCanShareVCallOffset(OffsetPair.first, MD))
514       return false;
515   }
516 
517   // Add the offset.
518   Offsets.push_back(MethodAndOffsetPairTy(MD, OffsetOffset));
519   return true;
520 }
521 
getVCallOffsetOffset(const CXXMethodDecl * MD)522 CharUnits VCallOffsetMap::getVCallOffsetOffset(const CXXMethodDecl *MD) {
523   // Look for an offset.
524   for (const auto &OffsetPair : Offsets) {
525     if (MethodsCanShareVCallOffset(OffsetPair.first, MD))
526       return OffsetPair.second;
527   }
528 
529   llvm_unreachable("Should always find a vcall offset offset!");
530 }
531 
532 /// VCallAndVBaseOffsetBuilder - Class for building vcall and vbase offsets.
533 class VCallAndVBaseOffsetBuilder {
534 public:
535   typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits>
536     VBaseOffsetOffsetsMapTy;
537 
538 private:
539   const ItaniumVTableContext &VTables;
540 
541   /// MostDerivedClass - The most derived class for which we're building vcall
542   /// and vbase offsets.
543   const CXXRecordDecl *MostDerivedClass;
544 
545   /// LayoutClass - The class we're using for layout information. Will be
546   /// different than the most derived class if we're building a construction
547   /// vtable.
548   const CXXRecordDecl *LayoutClass;
549 
550   /// Context - The ASTContext which we will use for layout information.
551   ASTContext &Context;
552 
553   /// Components - vcall and vbase offset components
554   typedef SmallVector<VTableComponent, 64> VTableComponentVectorTy;
555   VTableComponentVectorTy Components;
556 
557   /// VisitedVirtualBases - Visited virtual bases.
558   llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
559 
560   /// VCallOffsets - Keeps track of vcall offsets.
561   VCallOffsetMap VCallOffsets;
562 
563 
564   /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets,
565   /// relative to the address point.
566   VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
567 
568   /// FinalOverriders - The final overriders of the most derived class.
569   /// (Can be null when we're not building a vtable of the most derived class).
570   const FinalOverriders *Overriders;
571 
572   /// AddVCallAndVBaseOffsets - Add vcall offsets and vbase offsets for the
573   /// given base subobject.
574   void AddVCallAndVBaseOffsets(BaseSubobject Base, bool BaseIsVirtual,
575                                CharUnits RealBaseOffset);
576 
577   /// AddVCallOffsets - Add vcall offsets for the given base subobject.
578   void AddVCallOffsets(BaseSubobject Base, CharUnits VBaseOffset);
579 
580   /// AddVBaseOffsets - Add vbase offsets for the given class.
581   void AddVBaseOffsets(const CXXRecordDecl *Base,
582                        CharUnits OffsetInLayoutClass);
583 
584   /// getCurrentOffsetOffset - Get the current vcall or vbase offset offset in
585   /// chars, relative to the vtable address point.
586   CharUnits getCurrentOffsetOffset() const;
587 
588 public:
VCallAndVBaseOffsetBuilder(const ItaniumVTableContext & VTables,const CXXRecordDecl * MostDerivedClass,const CXXRecordDecl * LayoutClass,const FinalOverriders * Overriders,BaseSubobject Base,bool BaseIsVirtual,CharUnits OffsetInLayoutClass)589   VCallAndVBaseOffsetBuilder(const ItaniumVTableContext &VTables,
590                              const CXXRecordDecl *MostDerivedClass,
591                              const CXXRecordDecl *LayoutClass,
592                              const FinalOverriders *Overriders,
593                              BaseSubobject Base, bool BaseIsVirtual,
594                              CharUnits OffsetInLayoutClass)
595       : VTables(VTables), MostDerivedClass(MostDerivedClass),
596         LayoutClass(LayoutClass), Context(MostDerivedClass->getASTContext()),
597         Overriders(Overriders) {
598 
599     // Add vcall and vbase offsets.
600     AddVCallAndVBaseOffsets(Base, BaseIsVirtual, OffsetInLayoutClass);
601   }
602 
603   /// Methods for iterating over the components.
604   typedef VTableComponentVectorTy::const_reverse_iterator const_iterator;
components_begin() const605   const_iterator components_begin() const { return Components.rbegin(); }
components_end() const606   const_iterator components_end() const { return Components.rend(); }
607 
getVCallOffsets() const608   const VCallOffsetMap &getVCallOffsets() const { return VCallOffsets; }
getVBaseOffsetOffsets() const609   const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
610     return VBaseOffsetOffsets;
611   }
612 };
613 
614 void
AddVCallAndVBaseOffsets(BaseSubobject Base,bool BaseIsVirtual,CharUnits RealBaseOffset)615 VCallAndVBaseOffsetBuilder::AddVCallAndVBaseOffsets(BaseSubobject Base,
616                                                     bool BaseIsVirtual,
617                                                     CharUnits RealBaseOffset) {
618   const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base.getBase());
619 
620   // Itanium C++ ABI 2.5.2:
621   //   ..in classes sharing a virtual table with a primary base class, the vcall
622   //   and vbase offsets added by the derived class all come before the vcall
623   //   and vbase offsets required by the base class, so that the latter may be
624   //   laid out as required by the base class without regard to additions from
625   //   the derived class(es).
626 
627   // (Since we're emitting the vcall and vbase offsets in reverse order, we'll
628   // emit them for the primary base first).
629   if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
630     bool PrimaryBaseIsVirtual = Layout.isPrimaryBaseVirtual();
631 
632     CharUnits PrimaryBaseOffset;
633 
634     // Get the base offset of the primary base.
635     if (PrimaryBaseIsVirtual) {
636       assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() &&
637              "Primary vbase should have a zero offset!");
638 
639       const ASTRecordLayout &MostDerivedClassLayout =
640         Context.getASTRecordLayout(MostDerivedClass);
641 
642       PrimaryBaseOffset =
643         MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
644     } else {
645       assert(Layout.getBaseClassOffset(PrimaryBase).isZero() &&
646              "Primary base should have a zero offset!");
647 
648       PrimaryBaseOffset = Base.getBaseOffset();
649     }
650 
651     AddVCallAndVBaseOffsets(
652       BaseSubobject(PrimaryBase,PrimaryBaseOffset),
653       PrimaryBaseIsVirtual, RealBaseOffset);
654   }
655 
656   AddVBaseOffsets(Base.getBase(), RealBaseOffset);
657 
658   // We only want to add vcall offsets for virtual bases.
659   if (BaseIsVirtual)
660     AddVCallOffsets(Base, RealBaseOffset);
661 }
662 
getCurrentOffsetOffset() const663 CharUnits VCallAndVBaseOffsetBuilder::getCurrentOffsetOffset() const {
664   // OffsetIndex is the index of this vcall or vbase offset, relative to the
665   // vtable address point. (We subtract 3 to account for the information just
666   // above the address point, the RTTI info, the offset to top, and the
667   // vcall offset itself).
668   size_t NumComponentsAboveAddrPoint = 3;
669   if (Context.getLangOpts().OmitVTableRTTI)
670     NumComponentsAboveAddrPoint--;
671   int64_t OffsetIndex =
672       -(int64_t)(NumComponentsAboveAddrPoint + Components.size());
673 
674   // Under the relative ABI, the offset widths are 32-bit ints instead of
675   // pointer widths.
676   CharUnits OffsetWidth = Context.toCharUnitsFromBits(
677       VTables.isRelativeLayout()
678           ? 32
679           : Context.getTargetInfo().getPointerWidth(LangAS::Default));
680   CharUnits OffsetOffset = OffsetWidth * OffsetIndex;
681 
682   return OffsetOffset;
683 }
684 
AddVCallOffsets(BaseSubobject Base,CharUnits VBaseOffset)685 void VCallAndVBaseOffsetBuilder::AddVCallOffsets(BaseSubobject Base,
686                                                  CharUnits VBaseOffset) {
687   const CXXRecordDecl *RD = Base.getBase();
688   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
689 
690   const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
691 
692   // Handle the primary base first.
693   // We only want to add vcall offsets if the base is non-virtual; a virtual
694   // primary base will have its vcall and vbase offsets emitted already.
695   if (PrimaryBase && !Layout.isPrimaryBaseVirtual()) {
696     // Get the base offset of the primary base.
697     assert(Layout.getBaseClassOffset(PrimaryBase).isZero() &&
698            "Primary base should have a zero offset!");
699 
700     AddVCallOffsets(BaseSubobject(PrimaryBase, Base.getBaseOffset()),
701                     VBaseOffset);
702   }
703 
704   // Add the vcall offsets.
705   for (const auto *MD : RD->methods()) {
706     if (!VTableContextBase::hasVtableSlot(MD))
707       continue;
708     MD = MD->getCanonicalDecl();
709 
710     CharUnits OffsetOffset = getCurrentOffsetOffset();
711 
712     // Don't add a vcall offset if we already have one for this member function
713     // signature.
714     if (!VCallOffsets.AddVCallOffset(MD, OffsetOffset))
715       continue;
716 
717     CharUnits Offset = CharUnits::Zero();
718 
719     if (Overriders) {
720       // Get the final overrider.
721       FinalOverriders::OverriderInfo Overrider =
722         Overriders->getOverrider(MD, Base.getBaseOffset());
723 
724       /// The vcall offset is the offset from the virtual base to the object
725       /// where the function was overridden.
726       Offset = Overrider.Offset - VBaseOffset;
727     }
728 
729     Components.push_back(
730       VTableComponent::MakeVCallOffset(Offset));
731   }
732 
733   // And iterate over all non-virtual bases (ignoring the primary base).
734   for (const auto &B : RD->bases()) {
735     if (B.isVirtual())
736       continue;
737 
738     const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
739     if (BaseDecl == PrimaryBase)
740       continue;
741 
742     // Get the base offset of this base.
743     CharUnits BaseOffset = Base.getBaseOffset() +
744       Layout.getBaseClassOffset(BaseDecl);
745 
746     AddVCallOffsets(BaseSubobject(BaseDecl, BaseOffset),
747                     VBaseOffset);
748   }
749 }
750 
751 void
AddVBaseOffsets(const CXXRecordDecl * RD,CharUnits OffsetInLayoutClass)752 VCallAndVBaseOffsetBuilder::AddVBaseOffsets(const CXXRecordDecl *RD,
753                                             CharUnits OffsetInLayoutClass) {
754   const ASTRecordLayout &LayoutClassLayout =
755     Context.getASTRecordLayout(LayoutClass);
756 
757   // Add vbase offsets.
758   for (const auto &B : RD->bases()) {
759     const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
760 
761     // Check if this is a virtual base that we haven't visited before.
762     if (B.isVirtual() && VisitedVirtualBases.insert(BaseDecl).second) {
763       CharUnits Offset =
764         LayoutClassLayout.getVBaseClassOffset(BaseDecl) - OffsetInLayoutClass;
765 
766       // Add the vbase offset offset.
767       assert(!VBaseOffsetOffsets.count(BaseDecl) &&
768              "vbase offset offset already exists!");
769 
770       CharUnits VBaseOffsetOffset = getCurrentOffsetOffset();
771       VBaseOffsetOffsets.insert(
772           std::make_pair(BaseDecl, VBaseOffsetOffset));
773 
774       Components.push_back(
775           VTableComponent::MakeVBaseOffset(Offset));
776     }
777 
778     // Check the base class looking for more vbase offsets.
779     AddVBaseOffsets(BaseDecl, OffsetInLayoutClass);
780   }
781 }
782 
783 /// ItaniumVTableBuilder - Class for building vtable layout information.
784 class ItaniumVTableBuilder {
785 public:
786   /// PrimaryBasesSetVectorTy - A set vector of direct and indirect
787   /// primary bases.
788   typedef llvm::SmallSetVector<const CXXRecordDecl *, 8>
789     PrimaryBasesSetVectorTy;
790 
791   typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits>
792     VBaseOffsetOffsetsMapTy;
793 
794   typedef VTableLayout::AddressPointsMapTy AddressPointsMapTy;
795 
796   typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVTableIndicesTy;
797 
798 private:
799   /// VTables - Global vtable information.
800   ItaniumVTableContext &VTables;
801 
802   /// MostDerivedClass - The most derived class for which we're building this
803   /// vtable.
804   const CXXRecordDecl *MostDerivedClass;
805 
806   /// MostDerivedClassOffset - If we're building a construction vtable, this
807   /// holds the offset from the layout class to the most derived class.
808   const CharUnits MostDerivedClassOffset;
809 
810   /// MostDerivedClassIsVirtual - Whether the most derived class is a virtual
811   /// base. (This only makes sense when building a construction vtable).
812   bool MostDerivedClassIsVirtual;
813 
814   /// LayoutClass - The class we're using for layout information. Will be
815   /// different than the most derived class if we're building a construction
816   /// vtable.
817   const CXXRecordDecl *LayoutClass;
818 
819   /// Context - The ASTContext which we will use for layout information.
820   ASTContext &Context;
821 
822   /// FinalOverriders - The final overriders of the most derived class.
823   const FinalOverriders Overriders;
824 
825   /// VCallOffsetsForVBases - Keeps track of vcall offsets for the virtual
826   /// bases in this vtable.
827   llvm::DenseMap<const CXXRecordDecl *, VCallOffsetMap> VCallOffsetsForVBases;
828 
829   /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets for
830   /// the most derived class.
831   VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
832 
833   /// Components - The components of the vtable being built.
834   SmallVector<VTableComponent, 64> Components;
835 
836   /// AddressPoints - Address points for the vtable being built.
837   AddressPointsMapTy AddressPoints;
838 
839   /// MethodInfo - Contains information about a method in a vtable.
840   /// (Used for computing 'this' pointer adjustment thunks.
841   struct MethodInfo {
842     /// BaseOffset - The base offset of this method.
843     const CharUnits BaseOffset;
844 
845     /// BaseOffsetInLayoutClass - The base offset in the layout class of this
846     /// method.
847     const CharUnits BaseOffsetInLayoutClass;
848 
849     /// VTableIndex - The index in the vtable that this method has.
850     /// (For destructors, this is the index of the complete destructor).
851     const uint64_t VTableIndex;
852 
MethodInfo__anon1858bcf60111::ItaniumVTableBuilder::MethodInfo853     MethodInfo(CharUnits BaseOffset, CharUnits BaseOffsetInLayoutClass,
854                uint64_t VTableIndex)
855       : BaseOffset(BaseOffset),
856       BaseOffsetInLayoutClass(BaseOffsetInLayoutClass),
857       VTableIndex(VTableIndex) { }
858 
MethodInfo__anon1858bcf60111::ItaniumVTableBuilder::MethodInfo859     MethodInfo()
860       : BaseOffset(CharUnits::Zero()),
861       BaseOffsetInLayoutClass(CharUnits::Zero()),
862       VTableIndex(0) { }
863 
864     MethodInfo(MethodInfo const&) = default;
865   };
866 
867   typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy;
868 
869   /// MethodInfoMap - The information for all methods in the vtable we're
870   /// currently building.
871   MethodInfoMapTy MethodInfoMap;
872 
873   /// MethodVTableIndices - Contains the index (relative to the vtable address
874   /// point) where the function pointer for a virtual function is stored.
875   MethodVTableIndicesTy MethodVTableIndices;
876 
877   typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy;
878 
879   /// VTableThunks - The thunks by vtable index in the vtable currently being
880   /// built.
881   VTableThunksMapTy VTableThunks;
882 
883   typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
884   typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
885 
886   /// Thunks - A map that contains all the thunks needed for all methods in the
887   /// most derived class for which the vtable is currently being built.
888   ThunksMapTy Thunks;
889 
890   /// AddThunk - Add a thunk for the given method.
891   void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk);
892 
893   /// ComputeThisAdjustments - Compute the 'this' pointer adjustments for the
894   /// part of the vtable we're currently building.
895   void ComputeThisAdjustments();
896 
897   typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
898 
899   /// PrimaryVirtualBases - All known virtual bases who are a primary base of
900   /// some other base.
901   VisitedVirtualBasesSetTy PrimaryVirtualBases;
902 
903   /// ComputeReturnAdjustment - Compute the return adjustment given a return
904   /// adjustment base offset.
905   ReturnAdjustment ComputeReturnAdjustment(BaseOffset Offset);
906 
907   /// ComputeThisAdjustmentBaseOffset - Compute the base offset for adjusting
908   /// the 'this' pointer from the base subobject to the derived subobject.
909   BaseOffset ComputeThisAdjustmentBaseOffset(BaseSubobject Base,
910                                              BaseSubobject Derived) const;
911 
912   /// ComputeThisAdjustment - Compute the 'this' pointer adjustment for the
913   /// given virtual member function, its offset in the layout class and its
914   /// final overrider.
915   ThisAdjustment
916   ComputeThisAdjustment(const CXXMethodDecl *MD,
917                         CharUnits BaseOffsetInLayoutClass,
918                         FinalOverriders::OverriderInfo Overrider);
919 
920   /// AddMethod - Add a single virtual member function to the vtable
921   /// components vector.
922   void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment);
923 
924   /// IsOverriderUsed - Returns whether the overrider will ever be used in this
925   /// part of the vtable.
926   ///
927   /// Itanium C++ ABI 2.5.2:
928   ///
929   ///   struct A { virtual void f(); };
930   ///   struct B : virtual public A { int i; };
931   ///   struct C : virtual public A { int j; };
932   ///   struct D : public B, public C {};
933   ///
934   ///   When B and C are declared, A is a primary base in each case, so although
935   ///   vcall offsets are allocated in the A-in-B and A-in-C vtables, no this
936   ///   adjustment is required and no thunk is generated. However, inside D
937   ///   objects, A is no longer a primary base of C, so if we allowed calls to
938   ///   C::f() to use the copy of A's vtable in the C subobject, we would need
939   ///   to adjust this from C* to B::A*, which would require a third-party
940   ///   thunk. Since we require that a call to C::f() first convert to A*,
941   ///   C-in-D's copy of A's vtable is never referenced, so this is not
942   ///   necessary.
943   bool IsOverriderUsed(const CXXMethodDecl *Overrider,
944                        CharUnits BaseOffsetInLayoutClass,
945                        const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
946                        CharUnits FirstBaseOffsetInLayoutClass) const;
947 
948 
949   /// AddMethods - Add the methods of this base subobject and all its
950   /// primary bases to the vtable components vector.
951   void AddMethods(BaseSubobject Base, CharUnits BaseOffsetInLayoutClass,
952                   const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
953                   CharUnits FirstBaseOffsetInLayoutClass,
954                   PrimaryBasesSetVectorTy &PrimaryBases);
955 
956   // LayoutVTable - Layout the vtable for the given base class, including its
957   // secondary vtables and any vtables for virtual bases.
958   void LayoutVTable();
959 
960   /// LayoutPrimaryAndSecondaryVTables - Layout the primary vtable for the
961   /// given base subobject, as well as all its secondary vtables.
962   ///
963   /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
964   /// or a direct or indirect base of a virtual base.
965   ///
966   /// \param BaseIsVirtualInLayoutClass - Whether the base subobject is virtual
967   /// in the layout class.
968   void LayoutPrimaryAndSecondaryVTables(BaseSubobject Base,
969                                         bool BaseIsMorallyVirtual,
970                                         bool BaseIsVirtualInLayoutClass,
971                                         CharUnits OffsetInLayoutClass);
972 
973   /// LayoutSecondaryVTables - Layout the secondary vtables for the given base
974   /// subobject.
975   ///
976   /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
977   /// or a direct or indirect base of a virtual base.
978   void LayoutSecondaryVTables(BaseSubobject Base, bool BaseIsMorallyVirtual,
979                               CharUnits OffsetInLayoutClass);
980 
981   /// DeterminePrimaryVirtualBases - Determine the primary virtual bases in this
982   /// class hierarchy.
983   void DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
984                                     CharUnits OffsetInLayoutClass,
985                                     VisitedVirtualBasesSetTy &VBases);
986 
987   /// LayoutVTablesForVirtualBases - Layout vtables for all virtual bases of the
988   /// given base (excluding any primary bases).
989   void LayoutVTablesForVirtualBases(const CXXRecordDecl *RD,
990                                     VisitedVirtualBasesSetTy &VBases);
991 
992   /// isBuildingConstructionVTable - Return whether this vtable builder is
993   /// building a construction vtable.
isBuildingConstructorVTable() const994   bool isBuildingConstructorVTable() const {
995     return MostDerivedClass != LayoutClass;
996   }
997 
998 public:
999   /// Component indices of the first component of each of the vtables in the
1000   /// vtable group.
1001   SmallVector<size_t, 4> VTableIndices;
1002 
ItaniumVTableBuilder(ItaniumVTableContext & VTables,const CXXRecordDecl * MostDerivedClass,CharUnits MostDerivedClassOffset,bool MostDerivedClassIsVirtual,const CXXRecordDecl * LayoutClass)1003   ItaniumVTableBuilder(ItaniumVTableContext &VTables,
1004                        const CXXRecordDecl *MostDerivedClass,
1005                        CharUnits MostDerivedClassOffset,
1006                        bool MostDerivedClassIsVirtual,
1007                        const CXXRecordDecl *LayoutClass)
1008       : VTables(VTables), MostDerivedClass(MostDerivedClass),
1009         MostDerivedClassOffset(MostDerivedClassOffset),
1010         MostDerivedClassIsVirtual(MostDerivedClassIsVirtual),
1011         LayoutClass(LayoutClass), Context(MostDerivedClass->getASTContext()),
1012         Overriders(MostDerivedClass, MostDerivedClassOffset, LayoutClass) {
1013     assert(!Context.getTargetInfo().getCXXABI().isMicrosoft());
1014 
1015     LayoutVTable();
1016 
1017     if (Context.getLangOpts().DumpVTableLayouts)
1018       dumpLayout(llvm::outs());
1019   }
1020 
getNumThunks() const1021   uint64_t getNumThunks() const {
1022     return Thunks.size();
1023   }
1024 
thunks_begin() const1025   ThunksMapTy::const_iterator thunks_begin() const {
1026     return Thunks.begin();
1027   }
1028 
thunks_end() const1029   ThunksMapTy::const_iterator thunks_end() const {
1030     return Thunks.end();
1031   }
1032 
getVBaseOffsetOffsets() const1033   const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
1034     return VBaseOffsetOffsets;
1035   }
1036 
getAddressPoints() const1037   const AddressPointsMapTy &getAddressPoints() const {
1038     return AddressPoints;
1039   }
1040 
vtable_indices_begin() const1041   MethodVTableIndicesTy::const_iterator vtable_indices_begin() const {
1042     return MethodVTableIndices.begin();
1043   }
1044 
vtable_indices_end() const1045   MethodVTableIndicesTy::const_iterator vtable_indices_end() const {
1046     return MethodVTableIndices.end();
1047   }
1048 
vtable_components() const1049   ArrayRef<VTableComponent> vtable_components() const { return Components; }
1050 
address_points_begin() const1051   AddressPointsMapTy::const_iterator address_points_begin() const {
1052     return AddressPoints.begin();
1053   }
1054 
address_points_end() const1055   AddressPointsMapTy::const_iterator address_points_end() const {
1056     return AddressPoints.end();
1057   }
1058 
vtable_thunks_begin() const1059   VTableThunksMapTy::const_iterator vtable_thunks_begin() const {
1060     return VTableThunks.begin();
1061   }
1062 
vtable_thunks_end() const1063   VTableThunksMapTy::const_iterator vtable_thunks_end() const {
1064     return VTableThunks.end();
1065   }
1066 
1067   /// dumpLayout - Dump the vtable layout.
1068   void dumpLayout(raw_ostream&);
1069 };
1070 
AddThunk(const CXXMethodDecl * MD,const ThunkInfo & Thunk)1071 void ItaniumVTableBuilder::AddThunk(const CXXMethodDecl *MD,
1072                                     const ThunkInfo &Thunk) {
1073   assert(!isBuildingConstructorVTable() &&
1074          "Can't add thunks for construction vtable");
1075 
1076   SmallVectorImpl<ThunkInfo> &ThunksVector = Thunks[MD];
1077 
1078   // Check if we have this thunk already.
1079   if (llvm::is_contained(ThunksVector, Thunk))
1080     return;
1081 
1082   ThunksVector.push_back(Thunk);
1083 }
1084 
1085 typedef llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverriddenMethodsSetTy;
1086 
1087 /// Visit all the methods overridden by the given method recursively,
1088 /// in a depth-first pre-order. The Visitor's visitor method returns a bool
1089 /// indicating whether to continue the recursion for the given overridden
1090 /// method (i.e. returning false stops the iteration).
1091 template <class VisitorTy>
1092 static void
visitAllOverriddenMethods(const CXXMethodDecl * MD,VisitorTy & Visitor)1093 visitAllOverriddenMethods(const CXXMethodDecl *MD, VisitorTy &Visitor) {
1094   assert(VTableContextBase::hasVtableSlot(MD) && "Method is not virtual!");
1095 
1096   for (const CXXMethodDecl *OverriddenMD : MD->overridden_methods()) {
1097     if (!Visitor(OverriddenMD))
1098       continue;
1099     visitAllOverriddenMethods(OverriddenMD, Visitor);
1100   }
1101 }
1102 
1103 /// ComputeAllOverriddenMethods - Given a method decl, will return a set of all
1104 /// the overridden methods that the function decl overrides.
1105 static void
ComputeAllOverriddenMethods(const CXXMethodDecl * MD,OverriddenMethodsSetTy & OverriddenMethods)1106 ComputeAllOverriddenMethods(const CXXMethodDecl *MD,
1107                             OverriddenMethodsSetTy& OverriddenMethods) {
1108   auto OverriddenMethodsCollector = [&](const CXXMethodDecl *MD) {
1109     // Don't recurse on this method if we've already collected it.
1110     return OverriddenMethods.insert(MD).second;
1111   };
1112   visitAllOverriddenMethods(MD, OverriddenMethodsCollector);
1113 }
1114 
ComputeThisAdjustments()1115 void ItaniumVTableBuilder::ComputeThisAdjustments() {
1116   // Now go through the method info map and see if any of the methods need
1117   // 'this' pointer adjustments.
1118   for (const auto &MI : MethodInfoMap) {
1119     const CXXMethodDecl *MD = MI.first;
1120     const MethodInfo &MethodInfo = MI.second;
1121 
1122     // Ignore adjustments for unused function pointers.
1123     uint64_t VTableIndex = MethodInfo.VTableIndex;
1124     if (Components[VTableIndex].getKind() ==
1125         VTableComponent::CK_UnusedFunctionPointer)
1126       continue;
1127 
1128     // Get the final overrider for this method.
1129     FinalOverriders::OverriderInfo Overrider =
1130       Overriders.getOverrider(MD, MethodInfo.BaseOffset);
1131 
1132     // Check if we need an adjustment at all.
1133     if (MethodInfo.BaseOffsetInLayoutClass == Overrider.Offset) {
1134       // When a return thunk is needed by a derived class that overrides a
1135       // virtual base, gcc uses a virtual 'this' adjustment as well.
1136       // While the thunk itself might be needed by vtables in subclasses or
1137       // in construction vtables, there doesn't seem to be a reason for using
1138       // the thunk in this vtable. Still, we do so to match gcc.
1139       if (VTableThunks.lookup(VTableIndex).Return.isEmpty())
1140         continue;
1141     }
1142 
1143     ThisAdjustment ThisAdjustment =
1144       ComputeThisAdjustment(MD, MethodInfo.BaseOffsetInLayoutClass, Overrider);
1145 
1146     if (ThisAdjustment.isEmpty())
1147       continue;
1148 
1149     // Add it.
1150     auto SetThisAdjustmentThunk = [&](uint64_t Idx) {
1151       // If a this pointer adjustment is required, record the method that
1152       // created the vtable entry. MD is not necessarily the method that
1153       // created the entry since derived classes overwrite base class
1154       // information in MethodInfoMap, hence findOriginalMethodInMap is called
1155       // here.
1156       //
1157       // For example, in the following class hierarchy, if MD = D1::m and
1158       // Overrider = D2:m, the original method that created the entry is B0:m,
1159       // which is what findOriginalMethodInMap(MD) returns:
1160       //
1161       // struct B0 { int a; virtual void m(); };
1162       // struct D0 : B0 { int a; void m() override; };
1163       // struct D1 : B0 { int a; void m() override; };
1164       // struct D2 : D0, D1 { int a; void m() override; };
1165       //
1166       // We need to record the method because we cannot
1167       // call findOriginalMethod to find the method that created the entry if
1168       // the method in the entry requires adjustment.
1169       //
1170       // Do not set ThunkInfo::Method if Idx is already in VTableThunks. This
1171       // can happen when covariant return adjustment is required too.
1172       auto [It, Inserted] = VTableThunks.try_emplace(Idx);
1173       if (Inserted) {
1174         const CXXMethodDecl *Method = VTables.findOriginalMethodInMap(MD);
1175         It->second.Method = Method;
1176         It->second.ThisType = Method->getThisType().getTypePtr();
1177       }
1178       It->second.This = ThisAdjustment;
1179     };
1180 
1181     SetThisAdjustmentThunk(VTableIndex);
1182 
1183     if (isa<CXXDestructorDecl>(MD)) {
1184       // Add an adjustment for the deleting destructor as well.
1185       SetThisAdjustmentThunk(VTableIndex + 1);
1186     }
1187   }
1188 
1189   /// Clear the method info map.
1190   MethodInfoMap.clear();
1191 
1192   if (isBuildingConstructorVTable()) {
1193     // We don't need to store thunk information for construction vtables.
1194     return;
1195   }
1196 
1197   for (const auto &TI : VTableThunks) {
1198     const VTableComponent &Component = Components[TI.first];
1199     const ThunkInfo &Thunk = TI.second;
1200     const CXXMethodDecl *MD;
1201 
1202     switch (Component.getKind()) {
1203     default:
1204       llvm_unreachable("Unexpected vtable component kind!");
1205     case VTableComponent::CK_FunctionPointer:
1206       MD = Component.getFunctionDecl();
1207       break;
1208     case VTableComponent::CK_CompleteDtorPointer:
1209       MD = Component.getDestructorDecl();
1210       break;
1211     case VTableComponent::CK_DeletingDtorPointer:
1212       // We've already added the thunk when we saw the complete dtor pointer.
1213       continue;
1214     }
1215 
1216     if (MD->getParent() == MostDerivedClass)
1217       AddThunk(MD, Thunk);
1218   }
1219 }
1220 
1221 ReturnAdjustment
ComputeReturnAdjustment(BaseOffset Offset)1222 ItaniumVTableBuilder::ComputeReturnAdjustment(BaseOffset Offset) {
1223   ReturnAdjustment Adjustment;
1224 
1225   if (!Offset.isEmpty()) {
1226     if (Offset.VirtualBase) {
1227       // Get the virtual base offset offset.
1228       if (Offset.DerivedClass == MostDerivedClass) {
1229         // We can get the offset offset directly from our map.
1230         Adjustment.Virtual.Itanium.VBaseOffsetOffset =
1231           VBaseOffsetOffsets.lookup(Offset.VirtualBase).getQuantity();
1232       } else {
1233         Adjustment.Virtual.Itanium.VBaseOffsetOffset =
1234           VTables.getVirtualBaseOffsetOffset(Offset.DerivedClass,
1235                                              Offset.VirtualBase).getQuantity();
1236       }
1237     }
1238 
1239     Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity();
1240   }
1241 
1242   return Adjustment;
1243 }
1244 
ComputeThisAdjustmentBaseOffset(BaseSubobject Base,BaseSubobject Derived) const1245 BaseOffset ItaniumVTableBuilder::ComputeThisAdjustmentBaseOffset(
1246     BaseSubobject Base, BaseSubobject Derived) const {
1247   const CXXRecordDecl *BaseRD = Base.getBase();
1248   const CXXRecordDecl *DerivedRD = Derived.getBase();
1249 
1250   CXXBasePaths Paths(/*FindAmbiguities=*/true,
1251                      /*RecordPaths=*/true, /*DetectVirtual=*/true);
1252 
1253   if (!DerivedRD->isDerivedFrom(BaseRD, Paths))
1254     llvm_unreachable("Class must be derived from the passed in base class!");
1255 
1256   // We have to go through all the paths, and see which one leads us to the
1257   // right base subobject.
1258   for (const CXXBasePath &Path : Paths) {
1259     BaseOffset Offset = ComputeBaseOffset(Context, DerivedRD, Path);
1260 
1261     CharUnits OffsetToBaseSubobject = Offset.NonVirtualOffset;
1262 
1263     if (Offset.VirtualBase) {
1264       // If we have a virtual base class, the non-virtual offset is relative
1265       // to the virtual base class offset.
1266       const ASTRecordLayout &LayoutClassLayout =
1267         Context.getASTRecordLayout(LayoutClass);
1268 
1269       /// Get the virtual base offset, relative to the most derived class
1270       /// layout.
1271       OffsetToBaseSubobject +=
1272         LayoutClassLayout.getVBaseClassOffset(Offset.VirtualBase);
1273     } else {
1274       // Otherwise, the non-virtual offset is relative to the derived class
1275       // offset.
1276       OffsetToBaseSubobject += Derived.getBaseOffset();
1277     }
1278 
1279     // Check if this path gives us the right base subobject.
1280     if (OffsetToBaseSubobject == Base.getBaseOffset()) {
1281       // Since we're going from the base class _to_ the derived class, we'll
1282       // invert the non-virtual offset here.
1283       Offset.NonVirtualOffset = -Offset.NonVirtualOffset;
1284       return Offset;
1285     }
1286   }
1287 
1288   return BaseOffset();
1289 }
1290 
ComputeThisAdjustment(const CXXMethodDecl * MD,CharUnits BaseOffsetInLayoutClass,FinalOverriders::OverriderInfo Overrider)1291 ThisAdjustment ItaniumVTableBuilder::ComputeThisAdjustment(
1292     const CXXMethodDecl *MD, CharUnits BaseOffsetInLayoutClass,
1293     FinalOverriders::OverriderInfo Overrider) {
1294   // Ignore adjustments for pure virtual member functions.
1295   if (Overrider.Method->isPureVirtual())
1296     return ThisAdjustment();
1297 
1298   BaseSubobject OverriddenBaseSubobject(MD->getParent(),
1299                                         BaseOffsetInLayoutClass);
1300 
1301   BaseSubobject OverriderBaseSubobject(Overrider.Method->getParent(),
1302                                        Overrider.Offset);
1303 
1304   // Compute the adjustment offset.
1305   BaseOffset Offset = ComputeThisAdjustmentBaseOffset(OverriddenBaseSubobject,
1306                                                       OverriderBaseSubobject);
1307   if (Offset.isEmpty())
1308     return ThisAdjustment();
1309 
1310   ThisAdjustment Adjustment;
1311 
1312   if (Offset.VirtualBase) {
1313     // Get the vcall offset map for this virtual base.
1314     VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Offset.VirtualBase];
1315 
1316     if (VCallOffsets.empty()) {
1317       // We don't have vcall offsets for this virtual base, go ahead and
1318       // build them.
1319       VCallAndVBaseOffsetBuilder Builder(
1320           VTables, MostDerivedClass, MostDerivedClass,
1321           /*Overriders=*/nullptr,
1322           BaseSubobject(Offset.VirtualBase, CharUnits::Zero()),
1323           /*BaseIsVirtual=*/true,
1324           /*OffsetInLayoutClass=*/
1325           CharUnits::Zero());
1326 
1327       VCallOffsets = Builder.getVCallOffsets();
1328     }
1329 
1330     Adjustment.Virtual.Itanium.VCallOffsetOffset =
1331       VCallOffsets.getVCallOffsetOffset(MD).getQuantity();
1332   }
1333 
1334   // Set the non-virtual part of the adjustment.
1335   Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity();
1336 
1337   return Adjustment;
1338 }
1339 
AddMethod(const CXXMethodDecl * MD,ReturnAdjustment ReturnAdjustment)1340 void ItaniumVTableBuilder::AddMethod(const CXXMethodDecl *MD,
1341                                      ReturnAdjustment ReturnAdjustment) {
1342   if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1343     assert(ReturnAdjustment.isEmpty() &&
1344            "Destructor can't have return adjustment!");
1345 
1346     // Add both the complete destructor and the deleting destructor.
1347     Components.push_back(VTableComponent::MakeCompleteDtor(DD));
1348     Components.push_back(VTableComponent::MakeDeletingDtor(DD));
1349   } else {
1350     // Add the return adjustment if necessary.
1351     if (!ReturnAdjustment.isEmpty())
1352       VTableThunks[Components.size()].Return = ReturnAdjustment;
1353 
1354     // Add the function.
1355     Components.push_back(VTableComponent::MakeFunction(MD));
1356   }
1357 }
1358 
1359 /// OverridesIndirectMethodInBase - Return whether the given member function
1360 /// overrides any methods in the set of given bases.
1361 /// Unlike OverridesMethodInBase, this checks "overriders of overriders".
1362 /// For example, if we have:
1363 ///
1364 /// struct A { virtual void f(); }
1365 /// struct B : A { virtual void f(); }
1366 /// struct C : B { virtual void f(); }
1367 ///
1368 /// OverridesIndirectMethodInBase will return true if given C::f as the method
1369 /// and { A } as the set of bases.
OverridesIndirectMethodInBases(const CXXMethodDecl * MD,ItaniumVTableBuilder::PrimaryBasesSetVectorTy & Bases)1370 static bool OverridesIndirectMethodInBases(
1371     const CXXMethodDecl *MD,
1372     ItaniumVTableBuilder::PrimaryBasesSetVectorTy &Bases) {
1373   if (Bases.count(MD->getParent()))
1374     return true;
1375 
1376   for (const CXXMethodDecl *OverriddenMD : MD->overridden_methods()) {
1377     // Check "indirect overriders".
1378     if (OverridesIndirectMethodInBases(OverriddenMD, Bases))
1379       return true;
1380   }
1381 
1382   return false;
1383 }
1384 
IsOverriderUsed(const CXXMethodDecl * Overrider,CharUnits BaseOffsetInLayoutClass,const CXXRecordDecl * FirstBaseInPrimaryBaseChain,CharUnits FirstBaseOffsetInLayoutClass) const1385 bool ItaniumVTableBuilder::IsOverriderUsed(
1386     const CXXMethodDecl *Overrider, CharUnits BaseOffsetInLayoutClass,
1387     const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
1388     CharUnits FirstBaseOffsetInLayoutClass) const {
1389   // If the base and the first base in the primary base chain have the same
1390   // offsets, then this overrider will be used.
1391   if (BaseOffsetInLayoutClass == FirstBaseOffsetInLayoutClass)
1392    return true;
1393 
1394   // We know now that Base (or a direct or indirect base of it) is a primary
1395   // base in part of the class hierarchy, but not a primary base in the most
1396   // derived class.
1397 
1398   // If the overrider is the first base in the primary base chain, we know
1399   // that the overrider will be used.
1400   if (Overrider->getParent() == FirstBaseInPrimaryBaseChain)
1401     return true;
1402 
1403   ItaniumVTableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
1404 
1405   const CXXRecordDecl *RD = FirstBaseInPrimaryBaseChain;
1406   PrimaryBases.insert(RD);
1407 
1408   // Now traverse the base chain, starting with the first base, until we find
1409   // the base that is no longer a primary base.
1410   while (true) {
1411     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1412     const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1413 
1414     if (!PrimaryBase)
1415       break;
1416 
1417     if (Layout.isPrimaryBaseVirtual()) {
1418       assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() &&
1419              "Primary base should always be at offset 0!");
1420 
1421       const ASTRecordLayout &LayoutClassLayout =
1422         Context.getASTRecordLayout(LayoutClass);
1423 
1424       // Now check if this is the primary base that is not a primary base in the
1425       // most derived class.
1426       if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
1427           FirstBaseOffsetInLayoutClass) {
1428         // We found it, stop walking the chain.
1429         break;
1430       }
1431     } else {
1432       assert(Layout.getBaseClassOffset(PrimaryBase).isZero() &&
1433              "Primary base should always be at offset 0!");
1434     }
1435 
1436     if (!PrimaryBases.insert(PrimaryBase))
1437       llvm_unreachable("Found a duplicate primary base!");
1438 
1439     RD = PrimaryBase;
1440   }
1441 
1442   // If the final overrider is an override of one of the primary bases,
1443   // then we know that it will be used.
1444   return OverridesIndirectMethodInBases(Overrider, PrimaryBases);
1445 }
1446 
1447 typedef llvm::SmallSetVector<const CXXRecordDecl *, 8> BasesSetVectorTy;
1448 
1449 /// FindNearestOverriddenMethod - Given a method, returns the overridden method
1450 /// from the nearest base. Returns null if no method was found.
1451 /// The Bases are expected to be sorted in a base-to-derived order.
1452 static const CXXMethodDecl *
FindNearestOverriddenMethod(const CXXMethodDecl * MD,BasesSetVectorTy & Bases)1453 FindNearestOverriddenMethod(const CXXMethodDecl *MD,
1454                             BasesSetVectorTy &Bases) {
1455   OverriddenMethodsSetTy OverriddenMethods;
1456   ComputeAllOverriddenMethods(MD, OverriddenMethods);
1457 
1458   for (const CXXRecordDecl *PrimaryBase : llvm::reverse(Bases)) {
1459     // Now check the overridden methods.
1460     for (const CXXMethodDecl *OverriddenMD : OverriddenMethods) {
1461       // We found our overridden method.
1462       if (OverriddenMD->getParent() == PrimaryBase)
1463         return OverriddenMD;
1464     }
1465   }
1466 
1467   return nullptr;
1468 }
1469 
AddMethods(BaseSubobject Base,CharUnits BaseOffsetInLayoutClass,const CXXRecordDecl * FirstBaseInPrimaryBaseChain,CharUnits FirstBaseOffsetInLayoutClass,PrimaryBasesSetVectorTy & PrimaryBases)1470 void ItaniumVTableBuilder::AddMethods(
1471     BaseSubobject Base, CharUnits BaseOffsetInLayoutClass,
1472     const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
1473     CharUnits FirstBaseOffsetInLayoutClass,
1474     PrimaryBasesSetVectorTy &PrimaryBases) {
1475   // Itanium C++ ABI 2.5.2:
1476   //   The order of the virtual function pointers in a virtual table is the
1477   //   order of declaration of the corresponding member functions in the class.
1478   //
1479   //   There is an entry for any virtual function declared in a class,
1480   //   whether it is a new function or overrides a base class function,
1481   //   unless it overrides a function from the primary base, and conversion
1482   //   between their return types does not require an adjustment.
1483 
1484   const CXXRecordDecl *RD = Base.getBase();
1485   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1486 
1487   if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
1488     CharUnits PrimaryBaseOffset;
1489     CharUnits PrimaryBaseOffsetInLayoutClass;
1490     if (Layout.isPrimaryBaseVirtual()) {
1491       assert(Layout.getVBaseClassOffset(PrimaryBase).isZero() &&
1492              "Primary vbase should have a zero offset!");
1493 
1494       const ASTRecordLayout &MostDerivedClassLayout =
1495         Context.getASTRecordLayout(MostDerivedClass);
1496 
1497       PrimaryBaseOffset =
1498         MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
1499 
1500       const ASTRecordLayout &LayoutClassLayout =
1501         Context.getASTRecordLayout(LayoutClass);
1502 
1503       PrimaryBaseOffsetInLayoutClass =
1504         LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
1505     } else {
1506       assert(Layout.getBaseClassOffset(PrimaryBase).isZero() &&
1507              "Primary base should have a zero offset!");
1508 
1509       PrimaryBaseOffset = Base.getBaseOffset();
1510       PrimaryBaseOffsetInLayoutClass = BaseOffsetInLayoutClass;
1511     }
1512 
1513     AddMethods(BaseSubobject(PrimaryBase, PrimaryBaseOffset),
1514                PrimaryBaseOffsetInLayoutClass, FirstBaseInPrimaryBaseChain,
1515                FirstBaseOffsetInLayoutClass, PrimaryBases);
1516 
1517     if (!PrimaryBases.insert(PrimaryBase))
1518       llvm_unreachable("Found a duplicate primary base!");
1519   }
1520 
1521   typedef llvm::SmallVector<const CXXMethodDecl *, 8> NewVirtualFunctionsTy;
1522   NewVirtualFunctionsTy NewVirtualFunctions;
1523 
1524   llvm::SmallVector<const CXXMethodDecl*, 4> NewImplicitVirtualFunctions;
1525 
1526   // Now go through all virtual member functions and add them.
1527   for (const auto *MD : RD->methods()) {
1528     if (!ItaniumVTableContext::hasVtableSlot(MD))
1529       continue;
1530     MD = MD->getCanonicalDecl();
1531 
1532     // Get the final overrider.
1533     FinalOverriders::OverriderInfo Overrider =
1534       Overriders.getOverrider(MD, Base.getBaseOffset());
1535 
1536     // Check if this virtual member function overrides a method in a primary
1537     // base. If this is the case, and the return type doesn't require adjustment
1538     // then we can just use the member function from the primary base.
1539     if (const CXXMethodDecl *OverriddenMD =
1540           FindNearestOverriddenMethod(MD, PrimaryBases)) {
1541       if (ComputeReturnAdjustmentBaseOffset(Context, MD,
1542                                             OverriddenMD).isEmpty()) {
1543         VTables.setOriginalMethod(MD, OverriddenMD);
1544 
1545         // Replace the method info of the overridden method with our own
1546         // method.
1547         assert(MethodInfoMap.count(OverriddenMD) &&
1548                "Did not find the overridden method!");
1549         MethodInfo &OverriddenMethodInfo = MethodInfoMap[OverriddenMD];
1550 
1551         MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
1552                               OverriddenMethodInfo.VTableIndex);
1553 
1554         assert(!MethodInfoMap.count(MD) &&
1555                "Should not have method info for this method yet!");
1556 
1557         MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
1558         MethodInfoMap.erase(OverriddenMD);
1559 
1560         // If the overridden method exists in a virtual base class or a direct
1561         // or indirect base class of a virtual base class, we need to emit a
1562         // thunk if we ever have a class hierarchy where the base class is not
1563         // a primary base in the complete object.
1564         if (!isBuildingConstructorVTable() && OverriddenMD != MD) {
1565           // Compute the this adjustment.
1566           ThisAdjustment ThisAdjustment =
1567             ComputeThisAdjustment(OverriddenMD, BaseOffsetInLayoutClass,
1568                                   Overrider);
1569 
1570           if (ThisAdjustment.Virtual.Itanium.VCallOffsetOffset &&
1571               Overrider.Method->getParent() == MostDerivedClass) {
1572 
1573             // There's no return adjustment from OverriddenMD and MD,
1574             // but that doesn't mean there isn't one between MD and
1575             // the final overrider.
1576             BaseOffset ReturnAdjustmentOffset =
1577               ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD);
1578             ReturnAdjustment ReturnAdjustment =
1579               ComputeReturnAdjustment(ReturnAdjustmentOffset);
1580 
1581             // This is a virtual thunk for the most derived class, add it.
1582             AddThunk(Overrider.Method,
1583                      ThunkInfo(ThisAdjustment, ReturnAdjustment,
1584                                OverriddenMD->getThisType().getTypePtr()));
1585           }
1586         }
1587 
1588         continue;
1589       }
1590     }
1591 
1592     if (MD->isImplicit())
1593       NewImplicitVirtualFunctions.push_back(MD);
1594     else
1595       NewVirtualFunctions.push_back(MD);
1596   }
1597 
1598   llvm::stable_sort(
1599       NewImplicitVirtualFunctions,
1600       [](const CXXMethodDecl *A, const CXXMethodDecl *B) {
1601         if (A == B)
1602           return false;
1603         if (A->isCopyAssignmentOperator() != B->isCopyAssignmentOperator())
1604           return A->isCopyAssignmentOperator();
1605         if (A->isMoveAssignmentOperator() != B->isMoveAssignmentOperator())
1606           return A->isMoveAssignmentOperator();
1607         if (isa<CXXDestructorDecl>(A) != isa<CXXDestructorDecl>(B))
1608           return isa<CXXDestructorDecl>(A);
1609         assert(A->getOverloadedOperator() == OO_EqualEqual &&
1610                B->getOverloadedOperator() == OO_EqualEqual &&
1611                "unexpected or duplicate implicit virtual function");
1612         // We rely on Sema to have declared the operator== members in the
1613         // same order as the corresponding operator<=> members.
1614         return false;
1615       });
1616   NewVirtualFunctions.append(NewImplicitVirtualFunctions.begin(),
1617                              NewImplicitVirtualFunctions.end());
1618 
1619   for (const CXXMethodDecl *MD : NewVirtualFunctions) {
1620     // Get the final overrider.
1621     FinalOverriders::OverriderInfo Overrider =
1622       Overriders.getOverrider(MD, Base.getBaseOffset());
1623 
1624     // Insert the method info for this method.
1625     MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
1626                           Components.size());
1627 
1628     assert(!MethodInfoMap.count(MD) &&
1629            "Should not have method info for this method yet!");
1630     MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
1631 
1632     // Check if this overrider is going to be used.
1633     const CXXMethodDecl *OverriderMD = Overrider.Method;
1634     if (!IsOverriderUsed(OverriderMD, BaseOffsetInLayoutClass,
1635                          FirstBaseInPrimaryBaseChain,
1636                          FirstBaseOffsetInLayoutClass)) {
1637       Components.push_back(VTableComponent::MakeUnusedFunction(OverriderMD));
1638       continue;
1639     }
1640 
1641     // Check if this overrider needs a return adjustment.
1642     // We don't want to do this for pure virtual member functions.
1643     BaseOffset ReturnAdjustmentOffset;
1644     if (!OverriderMD->isPureVirtual()) {
1645       ReturnAdjustmentOffset =
1646         ComputeReturnAdjustmentBaseOffset(Context, OverriderMD, MD);
1647     }
1648 
1649     ReturnAdjustment ReturnAdjustment =
1650       ComputeReturnAdjustment(ReturnAdjustmentOffset);
1651 
1652     // If a return adjustment is required, record the method that created the
1653     // vtable entry. We need to record the method because we cannot call
1654     // findOriginalMethod to find the method that created the entry if the
1655     // method in the entry requires adjustment.
1656     if (!ReturnAdjustment.isEmpty()) {
1657       auto &VTT = VTableThunks[Components.size()];
1658       VTT.Method = MD;
1659       VTT.ThisType = MD->getThisType().getTypePtr();
1660     }
1661 
1662     AddMethod(Overrider.Method, ReturnAdjustment);
1663   }
1664 }
1665 
LayoutVTable()1666 void ItaniumVTableBuilder::LayoutVTable() {
1667   LayoutPrimaryAndSecondaryVTables(BaseSubobject(MostDerivedClass,
1668                                                  CharUnits::Zero()),
1669                                    /*BaseIsMorallyVirtual=*/false,
1670                                    MostDerivedClassIsVirtual,
1671                                    MostDerivedClassOffset);
1672 
1673   VisitedVirtualBasesSetTy VBases;
1674 
1675   // Determine the primary virtual bases.
1676   DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset,
1677                                VBases);
1678   VBases.clear();
1679 
1680   LayoutVTablesForVirtualBases(MostDerivedClass, VBases);
1681 
1682   // -fapple-kext adds an extra entry at end of vtbl.
1683   bool IsAppleKext = Context.getLangOpts().AppleKext;
1684   if (IsAppleKext)
1685     Components.push_back(VTableComponent::MakeVCallOffset(CharUnits::Zero()));
1686 }
1687 
LayoutPrimaryAndSecondaryVTables(BaseSubobject Base,bool BaseIsMorallyVirtual,bool BaseIsVirtualInLayoutClass,CharUnits OffsetInLayoutClass)1688 void ItaniumVTableBuilder::LayoutPrimaryAndSecondaryVTables(
1689     BaseSubobject Base, bool BaseIsMorallyVirtual,
1690     bool BaseIsVirtualInLayoutClass, CharUnits OffsetInLayoutClass) {
1691   assert(Base.getBase()->isDynamicClass() && "class does not have a vtable!");
1692 
1693   unsigned VTableIndex = Components.size();
1694   VTableIndices.push_back(VTableIndex);
1695 
1696   // Add vcall and vbase offsets for this vtable.
1697   VCallAndVBaseOffsetBuilder Builder(
1698       VTables, MostDerivedClass, LayoutClass, &Overriders, Base,
1699       BaseIsVirtualInLayoutClass, OffsetInLayoutClass);
1700   Components.append(Builder.components_begin(), Builder.components_end());
1701 
1702   // Check if we need to add these vcall offsets.
1703   if (BaseIsVirtualInLayoutClass && !Builder.getVCallOffsets().empty()) {
1704     VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()];
1705 
1706     if (VCallOffsets.empty())
1707       VCallOffsets = Builder.getVCallOffsets();
1708   }
1709 
1710   // If we're laying out the most derived class we want to keep track of the
1711   // virtual base class offset offsets.
1712   if (Base.getBase() == MostDerivedClass)
1713     VBaseOffsetOffsets = Builder.getVBaseOffsetOffsets();
1714 
1715   // Add the offset to top.
1716   CharUnits OffsetToTop = MostDerivedClassOffset - OffsetInLayoutClass;
1717   Components.push_back(VTableComponent::MakeOffsetToTop(OffsetToTop));
1718 
1719   // Next, add the RTTI.
1720   if (!Context.getLangOpts().OmitVTableRTTI)
1721     Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass));
1722 
1723   uint64_t AddressPoint = Components.size();
1724 
1725   // Now go through all virtual member functions and add them.
1726   PrimaryBasesSetVectorTy PrimaryBases;
1727   AddMethods(Base, OffsetInLayoutClass,
1728              Base.getBase(), OffsetInLayoutClass,
1729              PrimaryBases);
1730 
1731   const CXXRecordDecl *RD = Base.getBase();
1732   if (RD == MostDerivedClass) {
1733     assert(MethodVTableIndices.empty());
1734     for (const auto &I : MethodInfoMap) {
1735       const CXXMethodDecl *MD = I.first;
1736       const MethodInfo &MI = I.second;
1737       if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
1738         MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)]
1739             = MI.VTableIndex - AddressPoint;
1740         MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)]
1741             = MI.VTableIndex + 1 - AddressPoint;
1742       } else {
1743         MethodVTableIndices[MD] = MI.VTableIndex - AddressPoint;
1744       }
1745     }
1746   }
1747 
1748   // Compute 'this' pointer adjustments.
1749   ComputeThisAdjustments();
1750 
1751   // Add all address points.
1752   while (true) {
1753     AddressPoints.insert(
1754         std::make_pair(BaseSubobject(RD, OffsetInLayoutClass),
1755                        VTableLayout::AddressPointLocation{
1756                            unsigned(VTableIndices.size() - 1),
1757                            unsigned(AddressPoint - VTableIndex)}));
1758 
1759     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1760     const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1761 
1762     if (!PrimaryBase)
1763       break;
1764 
1765     if (Layout.isPrimaryBaseVirtual()) {
1766       // Check if this virtual primary base is a primary base in the layout
1767       // class. If it's not, we don't want to add it.
1768       const ASTRecordLayout &LayoutClassLayout =
1769         Context.getASTRecordLayout(LayoutClass);
1770 
1771       if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
1772           OffsetInLayoutClass) {
1773         // We don't want to add this class (or any of its primary bases).
1774         break;
1775       }
1776     }
1777 
1778     RD = PrimaryBase;
1779   }
1780 
1781   // Layout secondary vtables.
1782   LayoutSecondaryVTables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass);
1783 }
1784 
1785 void
LayoutSecondaryVTables(BaseSubobject Base,bool BaseIsMorallyVirtual,CharUnits OffsetInLayoutClass)1786 ItaniumVTableBuilder::LayoutSecondaryVTables(BaseSubobject Base,
1787                                              bool BaseIsMorallyVirtual,
1788                                              CharUnits OffsetInLayoutClass) {
1789   // Itanium C++ ABI 2.5.2:
1790   //   Following the primary virtual table of a derived class are secondary
1791   //   virtual tables for each of its proper base classes, except any primary
1792   //   base(s) with which it shares its primary virtual table.
1793 
1794   const CXXRecordDecl *RD = Base.getBase();
1795   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1796   const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1797 
1798   for (const auto &B : RD->bases()) {
1799     // Ignore virtual bases, we'll emit them later.
1800     if (B.isVirtual())
1801       continue;
1802 
1803     const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
1804 
1805     // Ignore bases that don't have a vtable.
1806     if (!BaseDecl->isDynamicClass())
1807       continue;
1808 
1809     if (isBuildingConstructorVTable()) {
1810       // Itanium C++ ABI 2.6.4:
1811       //   Some of the base class subobjects may not need construction virtual
1812       //   tables, which will therefore not be present in the construction
1813       //   virtual table group, even though the subobject virtual tables are
1814       //   present in the main virtual table group for the complete object.
1815       if (!BaseIsMorallyVirtual && !BaseDecl->getNumVBases())
1816         continue;
1817     }
1818 
1819     // Get the base offset of this base.
1820     CharUnits RelativeBaseOffset = Layout.getBaseClassOffset(BaseDecl);
1821     CharUnits BaseOffset = Base.getBaseOffset() + RelativeBaseOffset;
1822 
1823     CharUnits BaseOffsetInLayoutClass =
1824       OffsetInLayoutClass + RelativeBaseOffset;
1825 
1826     // Don't emit a secondary vtable for a primary base. We might however want
1827     // to emit secondary vtables for other bases of this base.
1828     if (BaseDecl == PrimaryBase) {
1829       LayoutSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset),
1830                              BaseIsMorallyVirtual, BaseOffsetInLayoutClass);
1831       continue;
1832     }
1833 
1834     // Layout the primary vtable (and any secondary vtables) for this base.
1835     LayoutPrimaryAndSecondaryVTables(
1836       BaseSubobject(BaseDecl, BaseOffset),
1837       BaseIsMorallyVirtual,
1838       /*BaseIsVirtualInLayoutClass=*/false,
1839       BaseOffsetInLayoutClass);
1840   }
1841 }
1842 
DeterminePrimaryVirtualBases(const CXXRecordDecl * RD,CharUnits OffsetInLayoutClass,VisitedVirtualBasesSetTy & VBases)1843 void ItaniumVTableBuilder::DeterminePrimaryVirtualBases(
1844     const CXXRecordDecl *RD, CharUnits OffsetInLayoutClass,
1845     VisitedVirtualBasesSetTy &VBases) {
1846   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1847 
1848   // Check if this base has a primary base.
1849   if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
1850 
1851     // Check if it's virtual.
1852     if (Layout.isPrimaryBaseVirtual()) {
1853       bool IsPrimaryVirtualBase = true;
1854 
1855       if (isBuildingConstructorVTable()) {
1856         // Check if the base is actually a primary base in the class we use for
1857         // layout.
1858         const ASTRecordLayout &LayoutClassLayout =
1859           Context.getASTRecordLayout(LayoutClass);
1860 
1861         CharUnits PrimaryBaseOffsetInLayoutClass =
1862           LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
1863 
1864         // We know that the base is not a primary base in the layout class if
1865         // the base offsets are different.
1866         if (PrimaryBaseOffsetInLayoutClass != OffsetInLayoutClass)
1867           IsPrimaryVirtualBase = false;
1868       }
1869 
1870       if (IsPrimaryVirtualBase)
1871         PrimaryVirtualBases.insert(PrimaryBase);
1872     }
1873   }
1874 
1875   // Traverse bases, looking for more primary virtual bases.
1876   for (const auto &B : RD->bases()) {
1877     const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
1878 
1879     CharUnits BaseOffsetInLayoutClass;
1880 
1881     if (B.isVirtual()) {
1882       if (!VBases.insert(BaseDecl).second)
1883         continue;
1884 
1885       const ASTRecordLayout &LayoutClassLayout =
1886         Context.getASTRecordLayout(LayoutClass);
1887 
1888       BaseOffsetInLayoutClass =
1889         LayoutClassLayout.getVBaseClassOffset(BaseDecl);
1890     } else {
1891       BaseOffsetInLayoutClass =
1892         OffsetInLayoutClass + Layout.getBaseClassOffset(BaseDecl);
1893     }
1894 
1895     DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases);
1896   }
1897 }
1898 
LayoutVTablesForVirtualBases(const CXXRecordDecl * RD,VisitedVirtualBasesSetTy & VBases)1899 void ItaniumVTableBuilder::LayoutVTablesForVirtualBases(
1900     const CXXRecordDecl *RD, VisitedVirtualBasesSetTy &VBases) {
1901   // Itanium C++ ABI 2.5.2:
1902   //   Then come the virtual base virtual tables, also in inheritance graph
1903   //   order, and again excluding primary bases (which share virtual tables with
1904   //   the classes for which they are primary).
1905   for (const auto &B : RD->bases()) {
1906     const CXXRecordDecl *BaseDecl = B.getType()->getAsCXXRecordDecl();
1907 
1908     // Check if this base needs a vtable. (If it's virtual, not a primary base
1909     // of some other class, and we haven't visited it before).
1910     if (B.isVirtual() && BaseDecl->isDynamicClass() &&
1911         !PrimaryVirtualBases.count(BaseDecl) &&
1912         VBases.insert(BaseDecl).second) {
1913       const ASTRecordLayout &MostDerivedClassLayout =
1914         Context.getASTRecordLayout(MostDerivedClass);
1915       CharUnits BaseOffset =
1916         MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
1917 
1918       const ASTRecordLayout &LayoutClassLayout =
1919         Context.getASTRecordLayout(LayoutClass);
1920       CharUnits BaseOffsetInLayoutClass =
1921         LayoutClassLayout.getVBaseClassOffset(BaseDecl);
1922 
1923       LayoutPrimaryAndSecondaryVTables(
1924         BaseSubobject(BaseDecl, BaseOffset),
1925         /*BaseIsMorallyVirtual=*/true,
1926         /*BaseIsVirtualInLayoutClass=*/true,
1927         BaseOffsetInLayoutClass);
1928     }
1929 
1930     // We only need to check the base for virtual base vtables if it actually
1931     // has virtual bases.
1932     if (BaseDecl->getNumVBases())
1933       LayoutVTablesForVirtualBases(BaseDecl, VBases);
1934   }
1935 }
1936 
printThunkMethod(const ThunkInfo & Info,raw_ostream & Out)1937 static void printThunkMethod(const ThunkInfo &Info, raw_ostream &Out) {
1938   if (!Info.Method)
1939     return;
1940   std::string Str = PredefinedExpr::ComputeName(
1941       PredefinedIdentKind::PrettyFunctionNoVirtual, Info.Method);
1942   Out << " method: " << Str;
1943 }
1944 
1945 /// dumpLayout - Dump the vtable layout.
dumpLayout(raw_ostream & Out)1946 void ItaniumVTableBuilder::dumpLayout(raw_ostream &Out) {
1947   // FIXME: write more tests that actually use the dumpLayout output to prevent
1948   // ItaniumVTableBuilder regressions.
1949 
1950   Out << "Original map\n";
1951 
1952   for (const auto &P : VTables.getOriginalMethodMap()) {
1953     std::string Str0 =
1954         PredefinedExpr::ComputeName(PredefinedIdentKind::PrettyFunctionNoVirtual,
1955                                     P.first);
1956     std::string Str1 =
1957         PredefinedExpr::ComputeName(PredefinedIdentKind::PrettyFunctionNoVirtual,
1958                                     P.second);
1959     Out << " " << Str0 << " -> " << Str1 << "\n";
1960   }
1961 
1962   if (isBuildingConstructorVTable()) {
1963     Out << "Construction vtable for ('";
1964     MostDerivedClass->printQualifiedName(Out);
1965     Out << "', ";
1966     Out << MostDerivedClassOffset.getQuantity() << ") in '";
1967     LayoutClass->printQualifiedName(Out);
1968   } else {
1969     Out << "Vtable for '";
1970     MostDerivedClass->printQualifiedName(Out);
1971   }
1972   Out << "' (" << Components.size() << " entries).\n";
1973 
1974   // Iterate through the address points and insert them into a new map where
1975   // they are keyed by the index and not the base object.
1976   // Since an address point can be shared by multiple subobjects, we use an
1977   // STL multimap.
1978   std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex;
1979   for (const auto &AP : AddressPoints) {
1980     const BaseSubobject &Base = AP.first;
1981     uint64_t Index =
1982         VTableIndices[AP.second.VTableIndex] + AP.second.AddressPointIndex;
1983 
1984     AddressPointsByIndex.insert(std::make_pair(Index, Base));
1985   }
1986 
1987   for (unsigned I = 0, E = Components.size(); I != E; ++I) {
1988     uint64_t Index = I;
1989 
1990     Out << llvm::format("%4d | ", I);
1991 
1992     const VTableComponent &Component = Components[I];
1993 
1994     // Dump the component.
1995     switch (Component.getKind()) {
1996 
1997     case VTableComponent::CK_VCallOffset:
1998       Out << "vcall_offset ("
1999           << Component.getVCallOffset().getQuantity()
2000           << ")";
2001       break;
2002 
2003     case VTableComponent::CK_VBaseOffset:
2004       Out << "vbase_offset ("
2005           << Component.getVBaseOffset().getQuantity()
2006           << ")";
2007       break;
2008 
2009     case VTableComponent::CK_OffsetToTop:
2010       Out << "offset_to_top ("
2011           << Component.getOffsetToTop().getQuantity()
2012           << ")";
2013       break;
2014 
2015     case VTableComponent::CK_RTTI:
2016       Component.getRTTIDecl()->printQualifiedName(Out);
2017       Out << " RTTI";
2018       break;
2019 
2020     case VTableComponent::CK_FunctionPointer: {
2021       const CXXMethodDecl *MD = Component.getFunctionDecl();
2022 
2023       std::string Str = PredefinedExpr::ComputeName(
2024           PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
2025       Out << Str;
2026       if (MD->isPureVirtual())
2027         Out << " [pure]";
2028 
2029       if (MD->isDeleted())
2030         Out << " [deleted]";
2031 
2032       ThunkInfo Thunk = VTableThunks.lookup(I);
2033       if (!Thunk.isEmpty()) {
2034         // If this function pointer has a return adjustment, dump it.
2035         if (!Thunk.Return.isEmpty()) {
2036           Out << "\n       [return adjustment: ";
2037           Out << Thunk.Return.NonVirtual << " non-virtual";
2038 
2039           if (Thunk.Return.Virtual.Itanium.VBaseOffsetOffset) {
2040             Out << ", " << Thunk.Return.Virtual.Itanium.VBaseOffsetOffset;
2041             Out << " vbase offset offset";
2042           }
2043 
2044           Out << ']';
2045           printThunkMethod(Thunk, Out);
2046         }
2047 
2048         // If this function pointer has a 'this' pointer adjustment, dump it.
2049         if (!Thunk.This.isEmpty()) {
2050           Out << "\n       [this adjustment: ";
2051           Out << Thunk.This.NonVirtual << " non-virtual";
2052 
2053           if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) {
2054             Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset;
2055             Out << " vcall offset offset";
2056           }
2057 
2058           Out << ']';
2059           printThunkMethod(Thunk, Out);
2060         }
2061       }
2062 
2063       break;
2064     }
2065 
2066     case VTableComponent::CK_CompleteDtorPointer:
2067     case VTableComponent::CK_DeletingDtorPointer: {
2068       bool IsComplete =
2069         Component.getKind() == VTableComponent::CK_CompleteDtorPointer;
2070 
2071       const CXXDestructorDecl *DD = Component.getDestructorDecl();
2072 
2073       DD->printQualifiedName(Out);
2074       if (IsComplete)
2075         Out << "() [complete]";
2076       else
2077         Out << "() [deleting]";
2078 
2079       if (DD->isPureVirtual())
2080         Out << " [pure]";
2081 
2082       ThunkInfo Thunk = VTableThunks.lookup(I);
2083       if (!Thunk.isEmpty()) {
2084         // If this destructor has a 'this' pointer adjustment, dump it.
2085         if (!Thunk.This.isEmpty()) {
2086           Out << "\n       [this adjustment: ";
2087           Out << Thunk.This.NonVirtual << " non-virtual";
2088 
2089           if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) {
2090             Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset;
2091             Out << " vcall offset offset";
2092           }
2093 
2094           Out << ']';
2095         }
2096         printThunkMethod(Thunk, Out);
2097       }
2098 
2099       break;
2100     }
2101 
2102     case VTableComponent::CK_UnusedFunctionPointer: {
2103       const CXXMethodDecl *MD = Component.getUnusedFunctionDecl();
2104 
2105       std::string Str = PredefinedExpr::ComputeName(
2106           PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
2107       Out << "[unused] " << Str;
2108       if (MD->isPureVirtual())
2109         Out << " [pure]";
2110     }
2111 
2112     }
2113 
2114     Out << '\n';
2115 
2116     // Dump the next address point.
2117     uint64_t NextIndex = Index + 1;
2118     if (unsigned Count = AddressPointsByIndex.count(NextIndex)) {
2119       if (Count == 1) {
2120         const BaseSubobject &Base =
2121           AddressPointsByIndex.find(NextIndex)->second;
2122 
2123         Out << "       -- (";
2124         Base.getBase()->printQualifiedName(Out);
2125         Out << ", " << Base.getBaseOffset().getQuantity();
2126         Out << ") vtable address --\n";
2127       } else {
2128         CharUnits BaseOffset =
2129           AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset();
2130 
2131         // We store the class names in a set to get a stable order.
2132         std::set<std::string> ClassNames;
2133         for (const auto &I :
2134              llvm::make_range(AddressPointsByIndex.equal_range(NextIndex))) {
2135           assert(I.second.getBaseOffset() == BaseOffset &&
2136                  "Invalid base offset!");
2137           const CXXRecordDecl *RD = I.second.getBase();
2138           ClassNames.insert(RD->getQualifiedNameAsString());
2139         }
2140 
2141         for (const std::string &Name : ClassNames) {
2142           Out << "       -- (" << Name;
2143           Out << ", " << BaseOffset.getQuantity() << ") vtable address --\n";
2144         }
2145       }
2146     }
2147   }
2148 
2149   Out << '\n';
2150 
2151   if (isBuildingConstructorVTable())
2152     return;
2153 
2154   if (MostDerivedClass->getNumVBases()) {
2155     // We store the virtual base class names and their offsets in a map to get
2156     // a stable order.
2157 
2158     std::map<std::string, CharUnits> ClassNamesAndOffsets;
2159     for (const auto &I : VBaseOffsetOffsets) {
2160       std::string ClassName = I.first->getQualifiedNameAsString();
2161       CharUnits OffsetOffset = I.second;
2162       ClassNamesAndOffsets.insert(std::make_pair(ClassName, OffsetOffset));
2163     }
2164 
2165     Out << "Virtual base offset offsets for '";
2166     MostDerivedClass->printQualifiedName(Out);
2167     Out << "' (";
2168     Out << ClassNamesAndOffsets.size();
2169     Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n";
2170 
2171     for (const auto &I : ClassNamesAndOffsets)
2172       Out << "   " << I.first << " | " << I.second.getQuantity() << '\n';
2173 
2174     Out << "\n";
2175   }
2176 
2177   if (!Thunks.empty()) {
2178     // We store the method names in a map to get a stable order.
2179     std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls;
2180 
2181     for (const auto &I : Thunks) {
2182       const CXXMethodDecl *MD = I.first;
2183       std::string MethodName = PredefinedExpr::ComputeName(
2184           PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
2185 
2186       MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
2187     }
2188 
2189     for (const auto &I : MethodNamesAndDecls) {
2190       const std::string &MethodName = I.first;
2191       const CXXMethodDecl *MD = I.second;
2192 
2193       ThunkInfoVectorTy ThunksVector = Thunks[MD];
2194       llvm::sort(ThunksVector, [](const ThunkInfo &LHS, const ThunkInfo &RHS) {
2195         return std::tie(LHS.This, LHS.Return) < std::tie(RHS.This, RHS.Return);
2196       });
2197 
2198       Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
2199       Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n";
2200 
2201       for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) {
2202         const ThunkInfo &Thunk = ThunksVector[I];
2203 
2204         Out << llvm::format("%4d | ", I);
2205 
2206         // If this function pointer has a return pointer adjustment, dump it.
2207         if (!Thunk.Return.isEmpty()) {
2208           Out << "return adjustment: " << Thunk.Return.NonVirtual;
2209           Out << " non-virtual";
2210           if (Thunk.Return.Virtual.Itanium.VBaseOffsetOffset) {
2211             Out << ", " << Thunk.Return.Virtual.Itanium.VBaseOffsetOffset;
2212             Out << " vbase offset offset";
2213           }
2214 
2215           if (!Thunk.This.isEmpty())
2216             Out << "\n       ";
2217         }
2218 
2219         // If this function pointer has a 'this' pointer adjustment, dump it.
2220         if (!Thunk.This.isEmpty()) {
2221           Out << "this adjustment: ";
2222           Out << Thunk.This.NonVirtual << " non-virtual";
2223 
2224           if (Thunk.This.Virtual.Itanium.VCallOffsetOffset) {
2225             Out << ", " << Thunk.This.Virtual.Itanium.VCallOffsetOffset;
2226             Out << " vcall offset offset";
2227           }
2228         }
2229 
2230         Out << '\n';
2231       }
2232 
2233       Out << '\n';
2234     }
2235   }
2236 
2237   // Compute the vtable indices for all the member functions.
2238   // Store them in a map keyed by the index so we'll get a sorted table.
2239   std::map<uint64_t, std::string> IndicesMap;
2240 
2241   for (const auto *MD : MostDerivedClass->methods()) {
2242     // We only want virtual member functions.
2243     if (!ItaniumVTableContext::hasVtableSlot(MD))
2244       continue;
2245     MD = MD->getCanonicalDecl();
2246 
2247     std::string MethodName = PredefinedExpr::ComputeName(
2248         PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
2249 
2250     if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2251       GlobalDecl GD(DD, Dtor_Complete);
2252       assert(MethodVTableIndices.count(GD));
2253       uint64_t VTableIndex = MethodVTableIndices[GD];
2254       IndicesMap[VTableIndex] = MethodName + " [complete]";
2255       IndicesMap[VTableIndex + 1] = MethodName + " [deleting]";
2256     } else {
2257       assert(MethodVTableIndices.count(MD));
2258       IndicesMap[MethodVTableIndices[MD]] = MethodName;
2259     }
2260   }
2261 
2262   // Print the vtable indices for all the member functions.
2263   if (!IndicesMap.empty()) {
2264     Out << "VTable indices for '";
2265     MostDerivedClass->printQualifiedName(Out);
2266     Out << "' (" << IndicesMap.size() << " entries).\n";
2267 
2268     for (const auto &I : IndicesMap) {
2269       uint64_t VTableIndex = I.first;
2270       const std::string &MethodName = I.second;
2271 
2272       Out << llvm::format("%4" PRIu64 " | ", VTableIndex) << MethodName
2273           << '\n';
2274     }
2275   }
2276 
2277   Out << '\n';
2278 }
2279 }
2280 
2281 static VTableLayout::AddressPointsIndexMapTy
MakeAddressPointIndices(const VTableLayout::AddressPointsMapTy & addressPoints,unsigned numVTables)2282 MakeAddressPointIndices(const VTableLayout::AddressPointsMapTy &addressPoints,
2283                         unsigned numVTables) {
2284   VTableLayout::AddressPointsIndexMapTy indexMap(numVTables);
2285 
2286   for (auto it = addressPoints.begin(); it != addressPoints.end(); ++it) {
2287     const auto &addressPointLoc = it->second;
2288     unsigned vtableIndex = addressPointLoc.VTableIndex;
2289     unsigned addressPoint = addressPointLoc.AddressPointIndex;
2290     if (indexMap[vtableIndex]) {
2291       // Multiple BaseSubobjects can map to the same AddressPointLocation, but
2292       // every vtable index should have a unique address point.
2293       assert(indexMap[vtableIndex] == addressPoint &&
2294              "Every vtable index should have a unique address point. Found a "
2295              "vtable that has two different address points.");
2296     } else {
2297       indexMap[vtableIndex] = addressPoint;
2298     }
2299   }
2300 
2301   // Note that by this point, not all the address may be initialized if the
2302   // AddressPoints map is empty. This is ok if the map isn't needed. See
2303   // MicrosoftVTableContext::computeVTableRelatedInformation() which uses an
2304   // emprt map.
2305   return indexMap;
2306 }
2307 
VTableLayout(ArrayRef<size_t> VTableIndices,ArrayRef<VTableComponent> VTableComponents,ArrayRef<VTableThunkTy> VTableThunks,const AddressPointsMapTy & AddressPoints)2308 VTableLayout::VTableLayout(ArrayRef<size_t> VTableIndices,
2309                            ArrayRef<VTableComponent> VTableComponents,
2310                            ArrayRef<VTableThunkTy> VTableThunks,
2311                            const AddressPointsMapTy &AddressPoints)
2312     : VTableComponents(VTableComponents), VTableThunks(VTableThunks),
2313       AddressPoints(AddressPoints), AddressPointIndices(MakeAddressPointIndices(
2314                                         AddressPoints, VTableIndices.size())) {
2315   if (VTableIndices.size() <= 1)
2316     assert(VTableIndices.size() == 1 && VTableIndices[0] == 0);
2317   else
2318     this->VTableIndices = OwningArrayRef<size_t>(VTableIndices);
2319 
2320   llvm::sort(this->VTableThunks, [](const VTableLayout::VTableThunkTy &LHS,
2321                                     const VTableLayout::VTableThunkTy &RHS) {
2322     assert((LHS.first != RHS.first || LHS.second == RHS.second) &&
2323            "Different thunks should have unique indices!");
2324     return LHS.first < RHS.first;
2325   });
2326 }
2327 
~VTableLayout()2328 VTableLayout::~VTableLayout() { }
2329 
hasVtableSlot(const CXXMethodDecl * MD)2330 bool VTableContextBase::hasVtableSlot(const CXXMethodDecl *MD) {
2331   return MD->isVirtual() && !MD->isImmediateFunction();
2332 }
2333 
ItaniumVTableContext(ASTContext & Context,VTableComponentLayout ComponentLayout)2334 ItaniumVTableContext::ItaniumVTableContext(
2335     ASTContext &Context, VTableComponentLayout ComponentLayout)
2336     : VTableContextBase(/*MS=*/false), ComponentLayout(ComponentLayout) {}
2337 
~ItaniumVTableContext()2338 ItaniumVTableContext::~ItaniumVTableContext() {}
2339 
getMethodVTableIndex(GlobalDecl GD)2340 uint64_t ItaniumVTableContext::getMethodVTableIndex(GlobalDecl GD) {
2341   GD = GD.getCanonicalDecl();
2342   MethodVTableIndicesTy::iterator I = MethodVTableIndices.find(GD);
2343   if (I != MethodVTableIndices.end())
2344     return I->second;
2345 
2346   const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
2347 
2348   computeVTableRelatedInformation(RD);
2349 
2350   I = MethodVTableIndices.find(GD);
2351   assert(I != MethodVTableIndices.end() && "Did not find index!");
2352   return I->second;
2353 }
2354 
2355 CharUnits
getVirtualBaseOffsetOffset(const CXXRecordDecl * RD,const CXXRecordDecl * VBase)2356 ItaniumVTableContext::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD,
2357                                                  const CXXRecordDecl *VBase) {
2358   ClassPairTy ClassPair(RD, VBase);
2359 
2360   VirtualBaseClassOffsetOffsetsMapTy::iterator I =
2361     VirtualBaseClassOffsetOffsets.find(ClassPair);
2362   if (I != VirtualBaseClassOffsetOffsets.end())
2363     return I->second;
2364 
2365   VCallAndVBaseOffsetBuilder Builder(*this, RD, RD, /*Overriders=*/nullptr,
2366                                      BaseSubobject(RD, CharUnits::Zero()),
2367                                      /*BaseIsVirtual=*/false,
2368                                      /*OffsetInLayoutClass=*/CharUnits::Zero());
2369 
2370   for (const auto &I : Builder.getVBaseOffsetOffsets()) {
2371     // Insert all types.
2372     ClassPairTy ClassPair(RD, I.first);
2373 
2374     VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I.second));
2375   }
2376 
2377   I = VirtualBaseClassOffsetOffsets.find(ClassPair);
2378   assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!");
2379 
2380   return I->second;
2381 }
2382 
findOriginalMethod(GlobalDecl GD)2383 GlobalDecl ItaniumVTableContext::findOriginalMethod(GlobalDecl GD) {
2384   const auto *MD = cast<CXXMethodDecl>(GD.getDecl());
2385   computeVTableRelatedInformation(MD->getParent());
2386   const CXXMethodDecl *OriginalMD = findOriginalMethodInMap(MD);
2387 
2388   if (const auto *DD = dyn_cast<CXXDestructorDecl>(OriginalMD))
2389     return GlobalDecl(DD, GD.getDtorType());
2390   return OriginalMD;
2391 }
2392 
2393 const CXXMethodDecl *
findOriginalMethodInMap(const CXXMethodDecl * MD) const2394 ItaniumVTableContext::findOriginalMethodInMap(const CXXMethodDecl *MD) const {
2395   // Traverse the chain of virtual methods until we find the method that added
2396   // the v-table slot.
2397   while (true) {
2398     auto I = OriginalMethodMap.find(MD);
2399 
2400     // MD doesn't exist in OriginalMethodMap, so it must be the method we are
2401     // looking for.
2402     if (I == OriginalMethodMap.end())
2403       break;
2404 
2405     // Set MD to the overridden method.
2406     MD = I->second;
2407   }
2408 
2409   return MD;
2410 }
2411 
2412 static std::unique_ptr<VTableLayout>
CreateVTableLayout(const ItaniumVTableBuilder & Builder)2413 CreateVTableLayout(const ItaniumVTableBuilder &Builder) {
2414   SmallVector<VTableLayout::VTableThunkTy, 1>
2415     VTableThunks(Builder.vtable_thunks_begin(), Builder.vtable_thunks_end());
2416 
2417   return std::make_unique<VTableLayout>(
2418       Builder.VTableIndices, Builder.vtable_components(), VTableThunks,
2419       Builder.getAddressPoints());
2420 }
2421 
2422 void
computeVTableRelatedInformation(const CXXRecordDecl * RD)2423 ItaniumVTableContext::computeVTableRelatedInformation(const CXXRecordDecl *RD) {
2424   std::unique_ptr<const VTableLayout> &Entry = VTableLayouts[RD];
2425 
2426   // Check if we've computed this information before.
2427   if (Entry)
2428     return;
2429 
2430   ItaniumVTableBuilder Builder(*this, RD, CharUnits::Zero(),
2431                                /*MostDerivedClassIsVirtual=*/false, RD);
2432   Entry = CreateVTableLayout(Builder);
2433 
2434   MethodVTableIndices.insert(Builder.vtable_indices_begin(),
2435                              Builder.vtable_indices_end());
2436 
2437   // Add the known thunks.
2438   Thunks.insert(Builder.thunks_begin(), Builder.thunks_end());
2439 
2440   // If we don't have the vbase information for this class, insert it.
2441   // getVirtualBaseOffsetOffset will compute it separately without computing
2442   // the rest of the vtable related information.
2443   if (!RD->getNumVBases())
2444     return;
2445 
2446   const CXXRecordDecl *VBase =
2447     RD->vbases_begin()->getType()->getAsCXXRecordDecl();
2448 
2449   if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase)))
2450     return;
2451 
2452   for (const auto &I : Builder.getVBaseOffsetOffsets()) {
2453     // Insert all types.
2454     ClassPairTy ClassPair(RD, I.first);
2455 
2456     VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I.second));
2457   }
2458 }
2459 
2460 std::unique_ptr<VTableLayout>
createConstructionVTableLayout(const CXXRecordDecl * MostDerivedClass,CharUnits MostDerivedClassOffset,bool MostDerivedClassIsVirtual,const CXXRecordDecl * LayoutClass)2461 ItaniumVTableContext::createConstructionVTableLayout(
2462     const CXXRecordDecl *MostDerivedClass, CharUnits MostDerivedClassOffset,
2463     bool MostDerivedClassIsVirtual, const CXXRecordDecl *LayoutClass) {
2464   ItaniumVTableBuilder Builder(*this, MostDerivedClass, MostDerivedClassOffset,
2465                                MostDerivedClassIsVirtual, LayoutClass);
2466   return CreateVTableLayout(Builder);
2467 }
2468 
2469 namespace {
2470 
2471 // Vtables in the Microsoft ABI are different from the Itanium ABI.
2472 //
2473 // The main differences are:
2474 //  1. Separate vftable and vbtable.
2475 //
2476 //  2. Each subobject with a vfptr gets its own vftable rather than an address
2477 //     point in a single vtable shared between all the subobjects.
2478 //     Each vftable is represented by a separate section and virtual calls
2479 //     must be done using the vftable which has a slot for the function to be
2480 //     called.
2481 //
2482 //  3. Virtual method definitions expect their 'this' parameter to point to the
2483 //     first vfptr whose table provides a compatible overridden method.  In many
2484 //     cases, this permits the original vf-table entry to directly call
2485 //     the method instead of passing through a thunk.
2486 //     See example before VFTableBuilder::ComputeThisOffset below.
2487 //
2488 //     A compatible overridden method is one which does not have a non-trivial
2489 //     covariant-return adjustment.
2490 //
2491 //     The first vfptr is the one with the lowest offset in the complete-object
2492 //     layout of the defining class, and the method definition will subtract
2493 //     that constant offset from the parameter value to get the real 'this'
2494 //     value.  Therefore, if the offset isn't really constant (e.g. if a virtual
2495 //     function defined in a virtual base is overridden in a more derived
2496 //     virtual base and these bases have a reverse order in the complete
2497 //     object), the vf-table may require a this-adjustment thunk.
2498 //
2499 //  4. vftables do not contain new entries for overrides that merely require
2500 //     this-adjustment.  Together with #3, this keeps vf-tables smaller and
2501 //     eliminates the need for this-adjustment thunks in many cases, at the cost
2502 //     of often requiring redundant work to adjust the "this" pointer.
2503 //
2504 //  5. Instead of VTT and constructor vtables, vbtables and vtordisps are used.
2505 //     Vtordisps are emitted into the class layout if a class has
2506 //      a) a user-defined ctor/dtor
2507 //     and
2508 //      b) a method overriding a method in a virtual base.
2509 //
2510 //  To get a better understanding of this code,
2511 //  you might want to see examples in test/CodeGenCXX/microsoft-abi-vtables-*.cpp
2512 
2513 class VFTableBuilder {
2514 public:
2515   typedef llvm::DenseMap<GlobalDecl, MethodVFTableLocation>
2516     MethodVFTableLocationsTy;
2517 
2518   typedef llvm::iterator_range<MethodVFTableLocationsTy::const_iterator>
2519     method_locations_range;
2520 
2521 private:
2522   /// VTables - Global vtable information.
2523   MicrosoftVTableContext &VTables;
2524 
2525   /// Context - The ASTContext which we will use for layout information.
2526   ASTContext &Context;
2527 
2528   /// MostDerivedClass - The most derived class for which we're building this
2529   /// vtable.
2530   const CXXRecordDecl *MostDerivedClass;
2531 
2532   const ASTRecordLayout &MostDerivedClassLayout;
2533 
2534   const VPtrInfo &WhichVFPtr;
2535 
2536   /// FinalOverriders - The final overriders of the most derived class.
2537   const FinalOverriders Overriders;
2538 
2539   /// Components - The components of the vftable being built.
2540   SmallVector<VTableComponent, 64> Components;
2541 
2542   MethodVFTableLocationsTy MethodVFTableLocations;
2543 
2544   /// Does this class have an RTTI component?
2545   bool HasRTTIComponent = false;
2546 
2547   /// MethodInfo - Contains information about a method in a vtable.
2548   /// (Used for computing 'this' pointer adjustment thunks.
2549   struct MethodInfo {
2550     /// VBTableIndex - The nonzero index in the vbtable that
2551     /// this method's base has, or zero.
2552     const uint64_t VBTableIndex;
2553 
2554     /// VFTableIndex - The index in the vftable that this method has.
2555     const uint64_t VFTableIndex;
2556 
2557     /// Shadowed - Indicates if this vftable slot is shadowed by
2558     /// a slot for a covariant-return override. If so, it shouldn't be printed
2559     /// or used for vcalls in the most derived class.
2560     bool Shadowed;
2561 
2562     /// UsesExtraSlot - Indicates if this vftable slot was created because
2563     /// any of the overridden slots required a return adjusting thunk.
2564     bool UsesExtraSlot;
2565 
MethodInfo__anon1858bcf60711::VFTableBuilder::MethodInfo2566     MethodInfo(uint64_t VBTableIndex, uint64_t VFTableIndex,
2567                bool UsesExtraSlot = false)
2568         : VBTableIndex(VBTableIndex), VFTableIndex(VFTableIndex),
2569           Shadowed(false), UsesExtraSlot(UsesExtraSlot) {}
2570 
MethodInfo__anon1858bcf60711::VFTableBuilder::MethodInfo2571     MethodInfo()
2572         : VBTableIndex(0), VFTableIndex(0), Shadowed(false),
2573           UsesExtraSlot(false) {}
2574   };
2575 
2576   typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy;
2577 
2578   /// MethodInfoMap - The information for all methods in the vftable we're
2579   /// currently building.
2580   MethodInfoMapTy MethodInfoMap;
2581 
2582   typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy;
2583 
2584   /// VTableThunks - The thunks by vftable index in the vftable currently being
2585   /// built.
2586   VTableThunksMapTy VTableThunks;
2587 
2588   typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
2589   typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
2590 
2591   /// Thunks - A map that contains all the thunks needed for all methods in the
2592   /// most derived class for which the vftable is currently being built.
2593   ThunksMapTy Thunks;
2594 
2595   /// AddThunk - Add a thunk for the given method.
AddThunk(const CXXMethodDecl * MD,const ThunkInfo & Thunk)2596   void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk) {
2597     SmallVector<ThunkInfo, 1> &ThunksVector = Thunks[MD];
2598 
2599     // Check if we have this thunk already.
2600     if (llvm::is_contained(ThunksVector, Thunk))
2601       return;
2602 
2603     ThunksVector.push_back(Thunk);
2604   }
2605 
2606   /// ComputeThisOffset - Returns the 'this' argument offset for the given
2607   /// method, relative to the beginning of the MostDerivedClass.
2608   CharUnits ComputeThisOffset(FinalOverriders::OverriderInfo Overrider);
2609 
2610   void CalculateVtordispAdjustment(FinalOverriders::OverriderInfo Overrider,
2611                                    CharUnits ThisOffset, ThisAdjustment &TA);
2612 
2613   /// AddMethod - Add a single virtual member function to the vftable
2614   /// components vector.
AddMethod(const CXXMethodDecl * MD,ThunkInfo TI)2615   void AddMethod(const CXXMethodDecl *MD, ThunkInfo TI) {
2616     if (!TI.isEmpty()) {
2617       VTableThunks[Components.size()] = TI;
2618       AddThunk(MD, TI);
2619     }
2620     if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2621       assert(TI.Return.isEmpty() &&
2622              "Destructor can't have return adjustment!");
2623       Components.push_back(VTableComponent::MakeDeletingDtor(DD));
2624     } else {
2625       Components.push_back(VTableComponent::MakeFunction(MD));
2626     }
2627   }
2628 
2629   /// AddMethods - Add the methods of this base subobject and the relevant
2630   /// subbases to the vftable we're currently laying out.
2631   void AddMethods(BaseSubobject Base, unsigned BaseDepth,
2632                   const CXXRecordDecl *LastVBase,
2633                   BasesSetVectorTy &VisitedBases);
2634 
LayoutVFTable()2635   void LayoutVFTable() {
2636     // RTTI data goes before all other entries.
2637     if (HasRTTIComponent)
2638       Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass));
2639 
2640     BasesSetVectorTy VisitedBases;
2641     AddMethods(BaseSubobject(MostDerivedClass, CharUnits::Zero()), 0, nullptr,
2642                VisitedBases);
2643     // Note that it is possible for the vftable to contain only an RTTI
2644     // pointer, if all virtual functions are constewval.
2645     assert(!Components.empty() && "vftable can't be empty");
2646 
2647     assert(MethodVFTableLocations.empty());
2648     for (const auto &I : MethodInfoMap) {
2649       const CXXMethodDecl *MD = I.first;
2650       const MethodInfo &MI = I.second;
2651       assert(MD == MD->getCanonicalDecl());
2652 
2653       // Skip the methods that the MostDerivedClass didn't override
2654       // and the entries shadowed by return adjusting thunks.
2655       if (MD->getParent() != MostDerivedClass || MI.Shadowed)
2656         continue;
2657       MethodVFTableLocation Loc(MI.VBTableIndex, WhichVFPtr.getVBaseWithVPtr(),
2658                                 WhichVFPtr.NonVirtualOffset, MI.VFTableIndex);
2659       if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2660         MethodVFTableLocations[GlobalDecl(DD, Dtor_Deleting)] = Loc;
2661       } else {
2662         MethodVFTableLocations[MD] = Loc;
2663       }
2664     }
2665   }
2666 
2667 public:
VFTableBuilder(MicrosoftVTableContext & VTables,const CXXRecordDecl * MostDerivedClass,const VPtrInfo & Which)2668   VFTableBuilder(MicrosoftVTableContext &VTables,
2669                  const CXXRecordDecl *MostDerivedClass, const VPtrInfo &Which)
2670       : VTables(VTables),
2671         Context(MostDerivedClass->getASTContext()),
2672         MostDerivedClass(MostDerivedClass),
2673         MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)),
2674         WhichVFPtr(Which),
2675         Overriders(MostDerivedClass, CharUnits(), MostDerivedClass) {
2676     // Provide the RTTI component if RTTIData is enabled. If the vftable would
2677     // be available externally, we should not provide the RTTI componenent. It
2678     // is currently impossible to get available externally vftables with either
2679     // dllimport or extern template instantiations, but eventually we may add a
2680     // flag to support additional devirtualization that needs this.
2681     if (Context.getLangOpts().RTTIData)
2682       HasRTTIComponent = true;
2683 
2684     LayoutVFTable();
2685 
2686     if (Context.getLangOpts().DumpVTableLayouts)
2687       dumpLayout(llvm::outs());
2688   }
2689 
getNumThunks() const2690   uint64_t getNumThunks() const { return Thunks.size(); }
2691 
thunks_begin() const2692   ThunksMapTy::const_iterator thunks_begin() const { return Thunks.begin(); }
2693 
thunks_end() const2694   ThunksMapTy::const_iterator thunks_end() const { return Thunks.end(); }
2695 
vtable_locations() const2696   method_locations_range vtable_locations() const {
2697     return method_locations_range(MethodVFTableLocations.begin(),
2698                                   MethodVFTableLocations.end());
2699   }
2700 
vtable_components() const2701   ArrayRef<VTableComponent> vtable_components() const { return Components; }
2702 
vtable_thunks_begin() const2703   VTableThunksMapTy::const_iterator vtable_thunks_begin() const {
2704     return VTableThunks.begin();
2705   }
2706 
vtable_thunks_end() const2707   VTableThunksMapTy::const_iterator vtable_thunks_end() const {
2708     return VTableThunks.end();
2709   }
2710 
2711   void dumpLayout(raw_ostream &);
2712 };
2713 
2714 } // end namespace
2715 
2716 // Let's study one class hierarchy as an example:
2717 //   struct A {
2718 //     virtual void f();
2719 //     int x;
2720 //   };
2721 //
2722 //   struct B : virtual A {
2723 //     virtual void f();
2724 //   };
2725 //
2726 // Record layouts:
2727 //   struct A:
2728 //   0 |   (A vftable pointer)
2729 //   4 |   int x
2730 //
2731 //   struct B:
2732 //   0 |   (B vbtable pointer)
2733 //   4 |   struct A (virtual base)
2734 //   4 |     (A vftable pointer)
2735 //   8 |     int x
2736 //
2737 // Let's assume we have a pointer to the A part of an object of dynamic type B:
2738 //   B b;
2739 //   A *a = (A*)&b;
2740 //   a->f();
2741 //
2742 // In this hierarchy, f() belongs to the vftable of A, so B::f() expects
2743 // "this" parameter to point at the A subobject, which is B+4.
2744 // In the B::f() prologue, it adjusts "this" back to B by subtracting 4,
2745 // performed as a *static* adjustment.
2746 //
2747 // Interesting thing happens when we alter the relative placement of A and B
2748 // subobjects in a class:
2749 //   struct C : virtual B { };
2750 //
2751 //   C c;
2752 //   A *a = (A*)&c;
2753 //   a->f();
2754 //
2755 // Respective record layout is:
2756 //   0 |   (C vbtable pointer)
2757 //   4 |   struct A (virtual base)
2758 //   4 |     (A vftable pointer)
2759 //   8 |     int x
2760 //  12 |   struct B (virtual base)
2761 //  12 |     (B vbtable pointer)
2762 //
2763 // The final overrider of f() in class C is still B::f(), so B+4 should be
2764 // passed as "this" to that code.  However, "a" points at B-8, so the respective
2765 // vftable entry should hold a thunk that adds 12 to the "this" argument before
2766 // performing a tail call to B::f().
2767 //
2768 // With this example in mind, we can now calculate the 'this' argument offset
2769 // for the given method, relative to the beginning of the MostDerivedClass.
2770 CharUnits
ComputeThisOffset(FinalOverriders::OverriderInfo Overrider)2771 VFTableBuilder::ComputeThisOffset(FinalOverriders::OverriderInfo Overrider) {
2772   BasesSetVectorTy Bases;
2773 
2774   {
2775     // Find the set of least derived bases that define the given method.
2776     OverriddenMethodsSetTy VisitedOverriddenMethods;
2777     auto InitialOverriddenDefinitionCollector = [&](
2778         const CXXMethodDecl *OverriddenMD) {
2779       if (OverriddenMD->size_overridden_methods() == 0)
2780         Bases.insert(OverriddenMD->getParent());
2781       // Don't recurse on this method if we've already collected it.
2782       return VisitedOverriddenMethods.insert(OverriddenMD).second;
2783     };
2784     visitAllOverriddenMethods(Overrider.Method,
2785                               InitialOverriddenDefinitionCollector);
2786   }
2787 
2788   // If there are no overrides then 'this' is located
2789   // in the base that defines the method.
2790   if (Bases.size() == 0)
2791     return Overrider.Offset;
2792 
2793   CXXBasePaths Paths;
2794   Overrider.Method->getParent()->lookupInBases(
2795       [&Bases](const CXXBaseSpecifier *Specifier, CXXBasePath &) {
2796         return Bases.count(Specifier->getType()->getAsCXXRecordDecl());
2797       },
2798       Paths);
2799 
2800   // This will hold the smallest this offset among overridees of MD.
2801   // This implies that an offset of a non-virtual base will dominate an offset
2802   // of a virtual base to potentially reduce the number of thunks required
2803   // in the derived classes that inherit this method.
2804   CharUnits Ret;
2805   bool First = true;
2806 
2807   const ASTRecordLayout &OverriderRDLayout =
2808       Context.getASTRecordLayout(Overrider.Method->getParent());
2809   for (const CXXBasePath &Path : Paths) {
2810     CharUnits ThisOffset = Overrider.Offset;
2811     CharUnits LastVBaseOffset;
2812 
2813     // For each path from the overrider to the parents of the overridden
2814     // methods, traverse the path, calculating the this offset in the most
2815     // derived class.
2816     for (const CXXBasePathElement &Element : Path) {
2817       QualType CurTy = Element.Base->getType();
2818       const CXXRecordDecl *PrevRD = Element.Class,
2819                           *CurRD = CurTy->getAsCXXRecordDecl();
2820       const ASTRecordLayout &Layout = Context.getASTRecordLayout(PrevRD);
2821 
2822       if (Element.Base->isVirtual()) {
2823         // The interesting things begin when you have virtual inheritance.
2824         // The final overrider will use a static adjustment equal to the offset
2825         // of the vbase in the final overrider class.
2826         // For example, if the final overrider is in a vbase B of the most
2827         // derived class and it overrides a method of the B's own vbase A,
2828         // it uses A* as "this".  In its prologue, it can cast A* to B* with
2829         // a static offset.  This offset is used regardless of the actual
2830         // offset of A from B in the most derived class, requiring an
2831         // this-adjusting thunk in the vftable if A and B are laid out
2832         // differently in the most derived class.
2833         LastVBaseOffset = ThisOffset =
2834             Overrider.Offset + OverriderRDLayout.getVBaseClassOffset(CurRD);
2835       } else {
2836         ThisOffset += Layout.getBaseClassOffset(CurRD);
2837       }
2838     }
2839 
2840     if (isa<CXXDestructorDecl>(Overrider.Method)) {
2841       if (LastVBaseOffset.isZero()) {
2842         // If a "Base" class has at least one non-virtual base with a virtual
2843         // destructor, the "Base" virtual destructor will take the address
2844         // of the "Base" subobject as the "this" argument.
2845         ThisOffset = Overrider.Offset;
2846       } else {
2847         // A virtual destructor of a virtual base takes the address of the
2848         // virtual base subobject as the "this" argument.
2849         ThisOffset = LastVBaseOffset;
2850       }
2851     }
2852 
2853     if (Ret > ThisOffset || First) {
2854       First = false;
2855       Ret = ThisOffset;
2856     }
2857   }
2858 
2859   assert(!First && "Method not found in the given subobject?");
2860   return Ret;
2861 }
2862 
2863 // Things are getting even more complex when the "this" adjustment has to
2864 // use a dynamic offset instead of a static one, or even two dynamic offsets.
2865 // This is sometimes required when a virtual call happens in the middle of
2866 // a non-most-derived class construction or destruction.
2867 //
2868 // Let's take a look at the following example:
2869 //   struct A {
2870 //     virtual void f();
2871 //   };
2872 //
2873 //   void foo(A *a) { a->f(); }  // Knows nothing about siblings of A.
2874 //
2875 //   struct B : virtual A {
2876 //     virtual void f();
2877 //     B() {
2878 //       foo(this);
2879 //     }
2880 //   };
2881 //
2882 //   struct C : virtual B {
2883 //     virtual void f();
2884 //   };
2885 //
2886 // Record layouts for these classes are:
2887 //   struct A
2888 //   0 |   (A vftable pointer)
2889 //
2890 //   struct B
2891 //   0 |   (B vbtable pointer)
2892 //   4 |   (vtordisp for vbase A)
2893 //   8 |   struct A (virtual base)
2894 //   8 |     (A vftable pointer)
2895 //
2896 //   struct C
2897 //   0 |   (C vbtable pointer)
2898 //   4 |   (vtordisp for vbase A)
2899 //   8 |   struct A (virtual base)  // A precedes B!
2900 //   8 |     (A vftable pointer)
2901 //  12 |   struct B (virtual base)
2902 //  12 |     (B vbtable pointer)
2903 //
2904 // When one creates an object of type C, the C constructor:
2905 // - initializes all the vbptrs, then
2906 // - calls the A subobject constructor
2907 //   (initializes A's vfptr with an address of A vftable), then
2908 // - calls the B subobject constructor
2909 //   (initializes A's vfptr with an address of B vftable and vtordisp for A),
2910 //   that in turn calls foo(), then
2911 // - initializes A's vfptr with an address of C vftable and zeroes out the
2912 //   vtordisp
2913 //   FIXME: if a structor knows it belongs to MDC, why doesn't it use a vftable
2914 //   without vtordisp thunks?
2915 //   FIXME: how are vtordisp handled in the presence of nooverride/final?
2916 //
2917 // When foo() is called, an object with a layout of class C has a vftable
2918 // referencing B::f() that assumes a B layout, so the "this" adjustments are
2919 // incorrect, unless an extra adjustment is done.  This adjustment is called
2920 // "vtordisp adjustment".  Vtordisp basically holds the difference between the
2921 // actual location of a vbase in the layout class and the location assumed by
2922 // the vftable of the class being constructed/destructed.  Vtordisp is only
2923 // needed if "this" escapes a
2924 // structor (or we can't prove otherwise).
2925 // [i.e. vtordisp is a dynamic adjustment for a static adjustment, which is an
2926 // estimation of a dynamic adjustment]
2927 //
2928 // foo() gets a pointer to the A vbase and doesn't know anything about B or C,
2929 // so it just passes that pointer as "this" in a virtual call.
2930 // If there was no vtordisp, that would just dispatch to B::f().
2931 // However, B::f() assumes B+8 is passed as "this",
2932 // yet the pointer foo() passes along is B-4 (i.e. C+8).
2933 // An extra adjustment is needed, so we emit a thunk into the B vftable.
2934 // This vtordisp thunk subtracts the value of vtordisp
2935 // from the "this" argument (-12) before making a tailcall to B::f().
2936 //
2937 // Let's consider an even more complex example:
2938 //   struct D : virtual B, virtual C {
2939 //     D() {
2940 //       foo(this);
2941 //     }
2942 //   };
2943 //
2944 //   struct D
2945 //   0 |   (D vbtable pointer)
2946 //   4 |   (vtordisp for vbase A)
2947 //   8 |   struct A (virtual base)  // A precedes both B and C!
2948 //   8 |     (A vftable pointer)
2949 //  12 |   struct B (virtual base)  // B precedes C!
2950 //  12 |     (B vbtable pointer)
2951 //  16 |   struct C (virtual base)
2952 //  16 |     (C vbtable pointer)
2953 //
2954 // When D::D() calls foo(), we find ourselves in a thunk that should tailcall
2955 // to C::f(), which assumes C+8 as its "this" parameter.  This time, foo()
2956 // passes along A, which is C-8.  The A vtordisp holds
2957 //   "D.vbptr[index_of_A] - offset_of_A_in_D"
2958 // and we statically know offset_of_A_in_D, so can get a pointer to D.
2959 // When we know it, we can make an extra vbtable lookup to locate the C vbase
2960 // and one extra static adjustment to calculate the expected value of C+8.
CalculateVtordispAdjustment(FinalOverriders::OverriderInfo Overrider,CharUnits ThisOffset,ThisAdjustment & TA)2961 void VFTableBuilder::CalculateVtordispAdjustment(
2962     FinalOverriders::OverriderInfo Overrider, CharUnits ThisOffset,
2963     ThisAdjustment &TA) {
2964   const ASTRecordLayout::VBaseOffsetsMapTy &VBaseMap =
2965       MostDerivedClassLayout.getVBaseOffsetsMap();
2966   const ASTRecordLayout::VBaseOffsetsMapTy::const_iterator &VBaseMapEntry =
2967       VBaseMap.find(WhichVFPtr.getVBaseWithVPtr());
2968   assert(VBaseMapEntry != VBaseMap.end());
2969 
2970   // If there's no vtordisp or the final overrider is defined in the same vbase
2971   // as the initial declaration, we don't need any vtordisp adjustment.
2972   if (!VBaseMapEntry->second.hasVtorDisp() ||
2973       Overrider.VirtualBase == WhichVFPtr.getVBaseWithVPtr())
2974     return;
2975 
2976   // OK, now we know we need to use a vtordisp thunk.
2977   // The implicit vtordisp field is located right before the vbase.
2978   CharUnits OffsetOfVBaseWithVFPtr = VBaseMapEntry->second.VBaseOffset;
2979   TA.Virtual.Microsoft.VtordispOffset =
2980       (OffsetOfVBaseWithVFPtr - WhichVFPtr.FullOffsetInMDC).getQuantity() - 4;
2981 
2982   // A simple vtordisp thunk will suffice if the final overrider is defined
2983   // in either the most derived class or its non-virtual base.
2984   if (Overrider.Method->getParent() == MostDerivedClass ||
2985       !Overrider.VirtualBase)
2986     return;
2987 
2988   // Otherwise, we need to do use the dynamic offset of the final overrider
2989   // in order to get "this" adjustment right.
2990   TA.Virtual.Microsoft.VBPtrOffset =
2991       (OffsetOfVBaseWithVFPtr + WhichVFPtr.NonVirtualOffset -
2992        MostDerivedClassLayout.getVBPtrOffset()).getQuantity();
2993   TA.Virtual.Microsoft.VBOffsetOffset =
2994       Context.getTypeSizeInChars(Context.IntTy).getQuantity() *
2995       VTables.getVBTableIndex(MostDerivedClass, Overrider.VirtualBase);
2996 
2997   TA.NonVirtual = (ThisOffset - Overrider.Offset).getQuantity();
2998 }
2999 
GroupNewVirtualOverloads(const CXXRecordDecl * RD,SmallVector<const CXXMethodDecl *,10> & VirtualMethods)3000 static void GroupNewVirtualOverloads(
3001     const CXXRecordDecl *RD,
3002     SmallVector<const CXXMethodDecl *, 10> &VirtualMethods) {
3003   // Put the virtual methods into VirtualMethods in the proper order:
3004   // 1) Group overloads by declaration name. New groups are added to the
3005   //    vftable in the order of their first declarations in this class
3006   //    (including overrides, non-virtual methods and any other named decl that
3007   //    might be nested within the class).
3008   // 2) In each group, new overloads appear in the reverse order of declaration.
3009   typedef SmallVector<const CXXMethodDecl *, 1> MethodGroup;
3010   SmallVector<MethodGroup, 10> Groups;
3011   typedef llvm::DenseMap<DeclarationName, unsigned> VisitedGroupIndicesTy;
3012   VisitedGroupIndicesTy VisitedGroupIndices;
3013   for (const auto *D : RD->decls()) {
3014     const auto *ND = dyn_cast<NamedDecl>(D);
3015     if (!ND)
3016       continue;
3017     VisitedGroupIndicesTy::iterator J;
3018     bool Inserted;
3019     std::tie(J, Inserted) = VisitedGroupIndices.insert(
3020         std::make_pair(ND->getDeclName(), Groups.size()));
3021     if (Inserted)
3022       Groups.push_back(MethodGroup());
3023     if (const auto *MD = dyn_cast<CXXMethodDecl>(ND))
3024       if (MicrosoftVTableContext::hasVtableSlot(MD))
3025         Groups[J->second].push_back(MD->getCanonicalDecl());
3026   }
3027 
3028   for (const MethodGroup &Group : Groups)
3029     VirtualMethods.append(Group.rbegin(), Group.rend());
3030 }
3031 
isDirectVBase(const CXXRecordDecl * Base,const CXXRecordDecl * RD)3032 static bool isDirectVBase(const CXXRecordDecl *Base, const CXXRecordDecl *RD) {
3033   for (const auto &B : RD->bases()) {
3034     if (B.isVirtual() && B.getType()->getAsCXXRecordDecl() == Base)
3035       return true;
3036   }
3037   return false;
3038 }
3039 
AddMethods(BaseSubobject Base,unsigned BaseDepth,const CXXRecordDecl * LastVBase,BasesSetVectorTy & VisitedBases)3040 void VFTableBuilder::AddMethods(BaseSubobject Base, unsigned BaseDepth,
3041                                 const CXXRecordDecl *LastVBase,
3042                                 BasesSetVectorTy &VisitedBases) {
3043   const CXXRecordDecl *RD = Base.getBase();
3044   if (!RD->isPolymorphic())
3045     return;
3046 
3047   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3048 
3049   // See if this class expands a vftable of the base we look at, which is either
3050   // the one defined by the vfptr base path or the primary base of the current
3051   // class.
3052   const CXXRecordDecl *NextBase = nullptr, *NextLastVBase = LastVBase;
3053   CharUnits NextBaseOffset;
3054   if (BaseDepth < WhichVFPtr.PathToIntroducingObject.size()) {
3055     NextBase = WhichVFPtr.PathToIntroducingObject[BaseDepth];
3056     if (isDirectVBase(NextBase, RD)) {
3057       NextLastVBase = NextBase;
3058       NextBaseOffset = MostDerivedClassLayout.getVBaseClassOffset(NextBase);
3059     } else {
3060       NextBaseOffset =
3061           Base.getBaseOffset() + Layout.getBaseClassOffset(NextBase);
3062     }
3063   } else if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
3064     assert(!Layout.isPrimaryBaseVirtual() &&
3065            "No primary virtual bases in this ABI");
3066     NextBase = PrimaryBase;
3067     NextBaseOffset = Base.getBaseOffset();
3068   }
3069 
3070   if (NextBase) {
3071     AddMethods(BaseSubobject(NextBase, NextBaseOffset), BaseDepth + 1,
3072                NextLastVBase, VisitedBases);
3073     if (!VisitedBases.insert(NextBase))
3074       llvm_unreachable("Found a duplicate primary base!");
3075   }
3076 
3077   SmallVector<const CXXMethodDecl*, 10> VirtualMethods;
3078   // Put virtual methods in the proper order.
3079   GroupNewVirtualOverloads(RD, VirtualMethods);
3080 
3081   // Now go through all virtual member functions and add them to the current
3082   // vftable. This is done by
3083   //  - replacing overridden methods in their existing slots, as long as they
3084   //    don't require return adjustment; calculating This adjustment if needed.
3085   //  - adding new slots for methods of the current base not present in any
3086   //    sub-bases;
3087   //  - adding new slots for methods that require Return adjustment.
3088   // We keep track of the methods visited in the sub-bases in MethodInfoMap.
3089   for (const CXXMethodDecl *MD : VirtualMethods) {
3090     FinalOverriders::OverriderInfo FinalOverrider =
3091         Overriders.getOverrider(MD, Base.getBaseOffset());
3092     const CXXMethodDecl *FinalOverriderMD = FinalOverrider.Method;
3093     const CXXMethodDecl *OverriddenMD =
3094         FindNearestOverriddenMethod(MD, VisitedBases);
3095 
3096     ThisAdjustment ThisAdjustmentOffset;
3097     bool ReturnAdjustingThunk = false, ForceReturnAdjustmentMangling = false;
3098     CharUnits ThisOffset = ComputeThisOffset(FinalOverrider);
3099     ThisAdjustmentOffset.NonVirtual =
3100         (ThisOffset - WhichVFPtr.FullOffsetInMDC).getQuantity();
3101     if ((OverriddenMD || FinalOverriderMD != MD) &&
3102         WhichVFPtr.getVBaseWithVPtr())
3103       CalculateVtordispAdjustment(FinalOverrider, ThisOffset,
3104                                   ThisAdjustmentOffset);
3105 
3106     unsigned VBIndex =
3107         LastVBase ? VTables.getVBTableIndex(MostDerivedClass, LastVBase) : 0;
3108 
3109     if (OverriddenMD) {
3110       // If MD overrides anything in this vftable, we need to update the
3111       // entries.
3112       MethodInfoMapTy::iterator OverriddenMDIterator =
3113           MethodInfoMap.find(OverriddenMD);
3114 
3115       // If the overridden method went to a different vftable, skip it.
3116       if (OverriddenMDIterator == MethodInfoMap.end())
3117         continue;
3118 
3119       MethodInfo &OverriddenMethodInfo = OverriddenMDIterator->second;
3120 
3121       VBIndex = OverriddenMethodInfo.VBTableIndex;
3122 
3123       // Let's check if the overrider requires any return adjustments.
3124       // We must create a new slot if the MD's return type is not trivially
3125       // convertible to the OverriddenMD's one.
3126       // Once a chain of method overrides adds a return adjusting vftable slot,
3127       // all subsequent overrides will also use an extra method slot.
3128       ReturnAdjustingThunk = !ComputeReturnAdjustmentBaseOffset(
3129                                   Context, MD, OverriddenMD).isEmpty() ||
3130                              OverriddenMethodInfo.UsesExtraSlot;
3131 
3132       if (!ReturnAdjustingThunk) {
3133         // No return adjustment needed - just replace the overridden method info
3134         // with the current info.
3135         MethodInfo MI(VBIndex, OverriddenMethodInfo.VFTableIndex);
3136         MethodInfoMap.erase(OverriddenMDIterator);
3137 
3138         assert(!MethodInfoMap.count(MD) &&
3139                "Should not have method info for this method yet!");
3140         MethodInfoMap.insert(std::make_pair(MD, MI));
3141         continue;
3142       }
3143 
3144       // In case we need a return adjustment, we'll add a new slot for
3145       // the overrider. Mark the overridden method as shadowed by the new slot.
3146       OverriddenMethodInfo.Shadowed = true;
3147 
3148       // Force a special name mangling for a return-adjusting thunk
3149       // unless the method is the final overrider without this adjustment.
3150       ForceReturnAdjustmentMangling =
3151           !(MD == FinalOverriderMD && ThisAdjustmentOffset.isEmpty());
3152     } else if (Base.getBaseOffset() != WhichVFPtr.FullOffsetInMDC ||
3153                MD->size_overridden_methods()) {
3154       // Skip methods that don't belong to the vftable of the current class,
3155       // e.g. each method that wasn't seen in any of the visited sub-bases
3156       // but overrides multiple methods of other sub-bases.
3157       continue;
3158     }
3159 
3160     // If we got here, MD is a method not seen in any of the sub-bases or
3161     // it requires return adjustment. Insert the method info for this method.
3162     MethodInfo MI(VBIndex,
3163                   HasRTTIComponent ? Components.size() - 1 : Components.size(),
3164                   ReturnAdjustingThunk);
3165 
3166     assert(!MethodInfoMap.count(MD) &&
3167            "Should not have method info for this method yet!");
3168     MethodInfoMap.insert(std::make_pair(MD, MI));
3169 
3170     // Check if this overrider needs a return adjustment.
3171     // We don't want to do this for pure virtual member functions.
3172     BaseOffset ReturnAdjustmentOffset;
3173     ReturnAdjustment ReturnAdjustment;
3174     if (!FinalOverriderMD->isPureVirtual()) {
3175       ReturnAdjustmentOffset =
3176           ComputeReturnAdjustmentBaseOffset(Context, FinalOverriderMD, MD);
3177     }
3178     if (!ReturnAdjustmentOffset.isEmpty()) {
3179       ForceReturnAdjustmentMangling = true;
3180       ReturnAdjustment.NonVirtual =
3181           ReturnAdjustmentOffset.NonVirtualOffset.getQuantity();
3182       if (ReturnAdjustmentOffset.VirtualBase) {
3183         const ASTRecordLayout &DerivedLayout =
3184             Context.getASTRecordLayout(ReturnAdjustmentOffset.DerivedClass);
3185         ReturnAdjustment.Virtual.Microsoft.VBPtrOffset =
3186             DerivedLayout.getVBPtrOffset().getQuantity();
3187         ReturnAdjustment.Virtual.Microsoft.VBIndex =
3188             VTables.getVBTableIndex(ReturnAdjustmentOffset.DerivedClass,
3189                                     ReturnAdjustmentOffset.VirtualBase);
3190       }
3191     }
3192     auto ThisType = (OverriddenMD ? OverriddenMD : MD)->getThisType().getTypePtr();
3193     AddMethod(FinalOverriderMD,
3194               ThunkInfo(ThisAdjustmentOffset, ReturnAdjustment, ThisType,
3195                         ForceReturnAdjustmentMangling ? MD : nullptr));
3196   }
3197 }
3198 
PrintBasePath(const VPtrInfo::BasePath & Path,raw_ostream & Out)3199 static void PrintBasePath(const VPtrInfo::BasePath &Path, raw_ostream &Out) {
3200   for (const CXXRecordDecl *Elem : llvm::reverse(Path)) {
3201     Out << "'";
3202     Elem->printQualifiedName(Out);
3203     Out << "' in ";
3204   }
3205 }
3206 
dumpMicrosoftThunkAdjustment(const ThunkInfo & TI,raw_ostream & Out,bool ContinueFirstLine)3207 static void dumpMicrosoftThunkAdjustment(const ThunkInfo &TI, raw_ostream &Out,
3208                                          bool ContinueFirstLine) {
3209   const ReturnAdjustment &R = TI.Return;
3210   bool Multiline = false;
3211   const char *LinePrefix = "\n       ";
3212   if (!R.isEmpty() || TI.Method) {
3213     if (!ContinueFirstLine)
3214       Out << LinePrefix;
3215     Out << "[return adjustment (to type '"
3216         << TI.Method->getReturnType().getCanonicalType() << "'): ";
3217     if (R.Virtual.Microsoft.VBPtrOffset)
3218       Out << "vbptr at offset " << R.Virtual.Microsoft.VBPtrOffset << ", ";
3219     if (R.Virtual.Microsoft.VBIndex)
3220       Out << "vbase #" << R.Virtual.Microsoft.VBIndex << ", ";
3221     Out << R.NonVirtual << " non-virtual]";
3222     Multiline = true;
3223   }
3224 
3225   const ThisAdjustment &T = TI.This;
3226   if (!T.isEmpty()) {
3227     if (Multiline || !ContinueFirstLine)
3228       Out << LinePrefix;
3229     Out << "[this adjustment: ";
3230     if (!TI.This.Virtual.isEmpty()) {
3231       assert(T.Virtual.Microsoft.VtordispOffset < 0);
3232       Out << "vtordisp at " << T.Virtual.Microsoft.VtordispOffset << ", ";
3233       if (T.Virtual.Microsoft.VBPtrOffset) {
3234         Out << "vbptr at " << T.Virtual.Microsoft.VBPtrOffset
3235             << " to the left,";
3236         assert(T.Virtual.Microsoft.VBOffsetOffset > 0);
3237         Out << LinePrefix << " vboffset at "
3238             << T.Virtual.Microsoft.VBOffsetOffset << " in the vbtable, ";
3239       }
3240     }
3241     Out << T.NonVirtual << " non-virtual]";
3242   }
3243 }
3244 
dumpLayout(raw_ostream & Out)3245 void VFTableBuilder::dumpLayout(raw_ostream &Out) {
3246   Out << "VFTable for ";
3247   PrintBasePath(WhichVFPtr.PathToIntroducingObject, Out);
3248   Out << "'";
3249   MostDerivedClass->printQualifiedName(Out);
3250   Out << "' (" << Components.size()
3251       << (Components.size() == 1 ? " entry" : " entries") << ").\n";
3252 
3253   for (unsigned I = 0, E = Components.size(); I != E; ++I) {
3254     Out << llvm::format("%4d | ", I);
3255 
3256     const VTableComponent &Component = Components[I];
3257 
3258     // Dump the component.
3259     switch (Component.getKind()) {
3260     case VTableComponent::CK_RTTI:
3261       Component.getRTTIDecl()->printQualifiedName(Out);
3262       Out << " RTTI";
3263       break;
3264 
3265     case VTableComponent::CK_FunctionPointer: {
3266       const CXXMethodDecl *MD = Component.getFunctionDecl();
3267 
3268       // FIXME: Figure out how to print the real thunk type, since they can
3269       // differ in the return type.
3270       std::string Str = PredefinedExpr::ComputeName(
3271           PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
3272       Out << Str;
3273       if (MD->isPureVirtual())
3274         Out << " [pure]";
3275 
3276       if (MD->isDeleted())
3277         Out << " [deleted]";
3278 
3279       ThunkInfo Thunk = VTableThunks.lookup(I);
3280       if (!Thunk.isEmpty())
3281         dumpMicrosoftThunkAdjustment(Thunk, Out, /*ContinueFirstLine=*/false);
3282 
3283       break;
3284     }
3285 
3286     case VTableComponent::CK_DeletingDtorPointer: {
3287       const CXXDestructorDecl *DD = Component.getDestructorDecl();
3288 
3289       DD->printQualifiedName(Out);
3290       Out << "() [scalar deleting]";
3291 
3292       if (DD->isPureVirtual())
3293         Out << " [pure]";
3294 
3295       ThunkInfo Thunk = VTableThunks.lookup(I);
3296       if (!Thunk.isEmpty()) {
3297         assert(Thunk.Return.isEmpty() &&
3298                "No return adjustment needed for destructors!");
3299         dumpMicrosoftThunkAdjustment(Thunk, Out, /*ContinueFirstLine=*/false);
3300       }
3301 
3302       break;
3303     }
3304 
3305     default:
3306       DiagnosticsEngine &Diags = Context.getDiagnostics();
3307       unsigned DiagID = Diags.getCustomDiagID(
3308           DiagnosticsEngine::Error,
3309           "Unexpected vftable component type %0 for component number %1");
3310       Diags.Report(MostDerivedClass->getLocation(), DiagID)
3311           << I << Component.getKind();
3312     }
3313 
3314     Out << '\n';
3315   }
3316 
3317   Out << '\n';
3318 
3319   if (!Thunks.empty()) {
3320     // We store the method names in a map to get a stable order.
3321     std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls;
3322 
3323     for (const auto &I : Thunks) {
3324       const CXXMethodDecl *MD = I.first;
3325       std::string MethodName = PredefinedExpr::ComputeName(
3326           PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
3327 
3328       MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
3329     }
3330 
3331     for (const auto &MethodNameAndDecl : MethodNamesAndDecls) {
3332       const std::string &MethodName = MethodNameAndDecl.first;
3333       const CXXMethodDecl *MD = MethodNameAndDecl.second;
3334 
3335       ThunkInfoVectorTy ThunksVector = Thunks[MD];
3336       llvm::stable_sort(ThunksVector, [](const ThunkInfo &LHS,
3337                                          const ThunkInfo &RHS) {
3338         // Keep different thunks with the same adjustments in the order they
3339         // were put into the vector.
3340         return std::tie(LHS.This, LHS.Return) < std::tie(RHS.This, RHS.Return);
3341       });
3342 
3343       Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
3344       Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n";
3345 
3346       for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) {
3347         const ThunkInfo &Thunk = ThunksVector[I];
3348 
3349         Out << llvm::format("%4d | ", I);
3350         dumpMicrosoftThunkAdjustment(Thunk, Out, /*ContinueFirstLine=*/true);
3351         Out << '\n';
3352       }
3353 
3354       Out << '\n';
3355     }
3356   }
3357 
3358   Out.flush();
3359 }
3360 
setsIntersect(const llvm::SmallPtrSet<const CXXRecordDecl *,4> & A,ArrayRef<const CXXRecordDecl * > B)3361 static bool setsIntersect(const llvm::SmallPtrSet<const CXXRecordDecl *, 4> &A,
3362                           ArrayRef<const CXXRecordDecl *> B) {
3363   for (const CXXRecordDecl *Decl : B) {
3364     if (A.count(Decl))
3365       return true;
3366   }
3367   return false;
3368 }
3369 
3370 static bool rebucketPaths(VPtrInfoVector &Paths);
3371 
3372 /// Produces MSVC-compatible vbtable data.  The symbols produced by this
3373 /// algorithm match those produced by MSVC 2012 and newer, which is different
3374 /// from MSVC 2010.
3375 ///
3376 /// MSVC 2012 appears to minimize the vbtable names using the following
3377 /// algorithm.  First, walk the class hierarchy in the usual order, depth first,
3378 /// left to right, to find all of the subobjects which contain a vbptr field.
3379 /// Visiting each class node yields a list of inheritance paths to vbptrs.  Each
3380 /// record with a vbptr creates an initially empty path.
3381 ///
3382 /// To combine paths from child nodes, the paths are compared to check for
3383 /// ambiguity.  Paths are "ambiguous" if multiple paths have the same set of
3384 /// components in the same order.  Each group of ambiguous paths is extended by
3385 /// appending the class of the base from which it came.  If the current class
3386 /// node produced an ambiguous path, its path is extended with the current class.
3387 /// After extending paths, MSVC again checks for ambiguity, and extends any
3388 /// ambiguous path which wasn't already extended.  Because each node yields an
3389 /// unambiguous set of paths, MSVC doesn't need to extend any path more than once
3390 /// to produce an unambiguous set of paths.
3391 ///
3392 /// TODO: Presumably vftables use the same algorithm.
computeVTablePaths(bool ForVBTables,const CXXRecordDecl * RD,VPtrInfoVector & Paths)3393 void MicrosoftVTableContext::computeVTablePaths(bool ForVBTables,
3394                                                 const CXXRecordDecl *RD,
3395                                                 VPtrInfoVector &Paths) {
3396   assert(Paths.empty());
3397   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3398 
3399   // Base case: this subobject has its own vptr.
3400   if (ForVBTables ? Layout.hasOwnVBPtr() : Layout.hasOwnVFPtr())
3401     Paths.push_back(std::make_unique<VPtrInfo>(RD));
3402 
3403   // Recursive case: get all the vbtables from our bases and remove anything
3404   // that shares a virtual base.
3405   llvm::SmallPtrSet<const CXXRecordDecl*, 4> VBasesSeen;
3406   for (const auto &B : RD->bases()) {
3407     const CXXRecordDecl *Base = B.getType()->getAsCXXRecordDecl();
3408     if (B.isVirtual() && VBasesSeen.count(Base))
3409       continue;
3410 
3411     if (!Base->isDynamicClass())
3412       continue;
3413 
3414     const VPtrInfoVector &BasePaths =
3415         ForVBTables ? enumerateVBTables(Base) : getVFPtrOffsets(Base);
3416 
3417     for (const std::unique_ptr<VPtrInfo> &BaseInfo : BasePaths) {
3418       // Don't include the path if it goes through a virtual base that we've
3419       // already included.
3420       if (setsIntersect(VBasesSeen, BaseInfo->ContainingVBases))
3421         continue;
3422 
3423       // Copy the path and adjust it as necessary.
3424       auto P = std::make_unique<VPtrInfo>(*BaseInfo);
3425 
3426       // We mangle Base into the path if the path would've been ambiguous and it
3427       // wasn't already extended with Base.
3428       if (P->MangledPath.empty() || P->MangledPath.back() != Base)
3429         P->NextBaseToMangle = Base;
3430 
3431       // Keep track of which vtable the derived class is going to extend with
3432       // new methods or bases.  We append to either the vftable of our primary
3433       // base, or the first non-virtual base that has a vbtable.
3434       if (P->ObjectWithVPtr == Base &&
3435           Base == (ForVBTables ? Layout.getBaseSharingVBPtr()
3436                                : Layout.getPrimaryBase()))
3437         P->ObjectWithVPtr = RD;
3438 
3439       // Keep track of the full adjustment from the MDC to this vtable.  The
3440       // adjustment is captured by an optional vbase and a non-virtual offset.
3441       if (B.isVirtual())
3442         P->ContainingVBases.push_back(Base);
3443       else if (P->ContainingVBases.empty())
3444         P->NonVirtualOffset += Layout.getBaseClassOffset(Base);
3445 
3446       // Update the full offset in the MDC.
3447       P->FullOffsetInMDC = P->NonVirtualOffset;
3448       if (const CXXRecordDecl *VB = P->getVBaseWithVPtr())
3449         P->FullOffsetInMDC += Layout.getVBaseClassOffset(VB);
3450 
3451       Paths.push_back(std::move(P));
3452     }
3453 
3454     if (B.isVirtual())
3455       VBasesSeen.insert(Base);
3456 
3457     // After visiting any direct base, we've transitively visited all of its
3458     // morally virtual bases.
3459     for (const auto &VB : Base->vbases())
3460       VBasesSeen.insert(VB.getType()->getAsCXXRecordDecl());
3461   }
3462 
3463   // Sort the paths into buckets, and if any of them are ambiguous, extend all
3464   // paths in ambiguous buckets.
3465   bool Changed = true;
3466   while (Changed)
3467     Changed = rebucketPaths(Paths);
3468 }
3469 
extendPath(VPtrInfo & P)3470 static bool extendPath(VPtrInfo &P) {
3471   if (P.NextBaseToMangle) {
3472     P.MangledPath.push_back(P.NextBaseToMangle);
3473     P.NextBaseToMangle = nullptr;// Prevent the path from being extended twice.
3474     return true;
3475   }
3476   return false;
3477 }
3478 
rebucketPaths(VPtrInfoVector & Paths)3479 static bool rebucketPaths(VPtrInfoVector &Paths) {
3480   // What we're essentially doing here is bucketing together ambiguous paths.
3481   // Any bucket with more than one path in it gets extended by NextBase, which
3482   // is usually the direct base of the inherited the vbptr.  This code uses a
3483   // sorted vector to implement a multiset to form the buckets.  Note that the
3484   // ordering is based on pointers, but it doesn't change our output order.  The
3485   // current algorithm is designed to match MSVC 2012's names.
3486   llvm::SmallVector<std::reference_wrapper<VPtrInfo>, 2> PathsSorted(
3487       llvm::make_pointee_range(Paths));
3488   llvm::sort(PathsSorted, [](const VPtrInfo &LHS, const VPtrInfo &RHS) {
3489     return LHS.MangledPath < RHS.MangledPath;
3490   });
3491   bool Changed = false;
3492   for (size_t I = 0, E = PathsSorted.size(); I != E;) {
3493     // Scan forward to find the end of the bucket.
3494     size_t BucketStart = I;
3495     do {
3496       ++I;
3497     } while (I != E &&
3498              PathsSorted[BucketStart].get().MangledPath ==
3499                  PathsSorted[I].get().MangledPath);
3500 
3501     // If this bucket has multiple paths, extend them all.
3502     if (I - BucketStart > 1) {
3503       for (size_t II = BucketStart; II != I; ++II)
3504         Changed |= extendPath(PathsSorted[II]);
3505       assert(Changed && "no paths were extended to fix ambiguity");
3506     }
3507   }
3508   return Changed;
3509 }
3510 
~MicrosoftVTableContext()3511 MicrosoftVTableContext::~MicrosoftVTableContext() {}
3512 
3513 namespace {
3514 typedef llvm::SetVector<BaseSubobject, std::vector<BaseSubobject>,
3515                         llvm::DenseSet<BaseSubobject>> FullPathTy;
3516 }
3517 
3518 // This recursive function finds all paths from a subobject centered at
3519 // (RD, Offset) to the subobject located at IntroducingObject.
findPathsToSubobject(ASTContext & Context,const ASTRecordLayout & MostDerivedLayout,const CXXRecordDecl * RD,CharUnits Offset,BaseSubobject IntroducingObject,FullPathTy & FullPath,std::list<FullPathTy> & Paths)3520 static void findPathsToSubobject(ASTContext &Context,
3521                                  const ASTRecordLayout &MostDerivedLayout,
3522                                  const CXXRecordDecl *RD, CharUnits Offset,
3523                                  BaseSubobject IntroducingObject,
3524                                  FullPathTy &FullPath,
3525                                  std::list<FullPathTy> &Paths) {
3526   if (BaseSubobject(RD, Offset) == IntroducingObject) {
3527     Paths.push_back(FullPath);
3528     return;
3529   }
3530 
3531   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3532 
3533   for (const CXXBaseSpecifier &BS : RD->bases()) {
3534     const CXXRecordDecl *Base = BS.getType()->getAsCXXRecordDecl();
3535     CharUnits NewOffset = BS.isVirtual()
3536                               ? MostDerivedLayout.getVBaseClassOffset(Base)
3537                               : Offset + Layout.getBaseClassOffset(Base);
3538     FullPath.insert(BaseSubobject(Base, NewOffset));
3539     findPathsToSubobject(Context, MostDerivedLayout, Base, NewOffset,
3540                          IntroducingObject, FullPath, Paths);
3541     FullPath.pop_back();
3542   }
3543 }
3544 
3545 // Return the paths which are not subsets of other paths.
removeRedundantPaths(std::list<FullPathTy> & FullPaths)3546 static void removeRedundantPaths(std::list<FullPathTy> &FullPaths) {
3547   FullPaths.remove_if([&](const FullPathTy &SpecificPath) {
3548     for (const FullPathTy &OtherPath : FullPaths) {
3549       if (&SpecificPath == &OtherPath)
3550         continue;
3551       if (llvm::all_of(SpecificPath, [&](const BaseSubobject &BSO) {
3552             return OtherPath.contains(BSO);
3553           })) {
3554         return true;
3555       }
3556     }
3557     return false;
3558   });
3559 }
3560 
getOffsetOfFullPath(ASTContext & Context,const CXXRecordDecl * RD,const FullPathTy & FullPath)3561 static CharUnits getOffsetOfFullPath(ASTContext &Context,
3562                                      const CXXRecordDecl *RD,
3563                                      const FullPathTy &FullPath) {
3564   const ASTRecordLayout &MostDerivedLayout =
3565       Context.getASTRecordLayout(RD);
3566   CharUnits Offset = CharUnits::fromQuantity(-1);
3567   for (const BaseSubobject &BSO : FullPath) {
3568     const CXXRecordDecl *Base = BSO.getBase();
3569     // The first entry in the path is always the most derived record, skip it.
3570     if (Base == RD) {
3571       assert(Offset.getQuantity() == -1);
3572       Offset = CharUnits::Zero();
3573       continue;
3574     }
3575     assert(Offset.getQuantity() != -1);
3576     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3577     // While we know which base has to be traversed, we don't know if that base
3578     // was a virtual base.
3579     const CXXBaseSpecifier *BaseBS = std::find_if(
3580         RD->bases_begin(), RD->bases_end(), [&](const CXXBaseSpecifier &BS) {
3581           return BS.getType()->getAsCXXRecordDecl() == Base;
3582         });
3583     Offset = BaseBS->isVirtual() ? MostDerivedLayout.getVBaseClassOffset(Base)
3584                                  : Offset + Layout.getBaseClassOffset(Base);
3585     RD = Base;
3586   }
3587   return Offset;
3588 }
3589 
3590 // We want to select the path which introduces the most covariant overrides.  If
3591 // two paths introduce overrides which the other path doesn't contain, issue a
3592 // diagnostic.
selectBestPath(ASTContext & Context,const CXXRecordDecl * RD,const VPtrInfo & Info,std::list<FullPathTy> & FullPaths)3593 static const FullPathTy *selectBestPath(ASTContext &Context,
3594                                         const CXXRecordDecl *RD,
3595                                         const VPtrInfo &Info,
3596                                         std::list<FullPathTy> &FullPaths) {
3597   // Handle some easy cases first.
3598   if (FullPaths.empty())
3599     return nullptr;
3600   if (FullPaths.size() == 1)
3601     return &FullPaths.front();
3602 
3603   const FullPathTy *BestPath = nullptr;
3604   typedef std::set<const CXXMethodDecl *> OverriderSetTy;
3605   OverriderSetTy LastOverrides;
3606   for (const FullPathTy &SpecificPath : FullPaths) {
3607     assert(!SpecificPath.empty());
3608     OverriderSetTy CurrentOverrides;
3609     const CXXRecordDecl *TopLevelRD = SpecificPath.begin()->getBase();
3610     // Find the distance from the start of the path to the subobject with the
3611     // VPtr.
3612     CharUnits BaseOffset =
3613         getOffsetOfFullPath(Context, TopLevelRD, SpecificPath);
3614     FinalOverriders Overriders(TopLevelRD, CharUnits::Zero(), TopLevelRD);
3615     for (const CXXMethodDecl *MD : Info.IntroducingObject->methods()) {
3616       if (!MicrosoftVTableContext::hasVtableSlot(MD))
3617         continue;
3618       FinalOverriders::OverriderInfo OI =
3619           Overriders.getOverrider(MD->getCanonicalDecl(), BaseOffset);
3620       const CXXMethodDecl *OverridingMethod = OI.Method;
3621       // Only overriders which have a return adjustment introduce problematic
3622       // thunks.
3623       if (ComputeReturnAdjustmentBaseOffset(Context, OverridingMethod, MD)
3624               .isEmpty())
3625         continue;
3626       // It's possible that the overrider isn't in this path.  If so, skip it
3627       // because this path didn't introduce it.
3628       const CXXRecordDecl *OverridingParent = OverridingMethod->getParent();
3629       if (llvm::none_of(SpecificPath, [&](const BaseSubobject &BSO) {
3630             return BSO.getBase() == OverridingParent;
3631           }))
3632         continue;
3633       CurrentOverrides.insert(OverridingMethod);
3634     }
3635     OverriderSetTy NewOverrides =
3636         llvm::set_difference(CurrentOverrides, LastOverrides);
3637     if (NewOverrides.empty())
3638       continue;
3639     OverriderSetTy MissingOverrides =
3640         llvm::set_difference(LastOverrides, CurrentOverrides);
3641     if (MissingOverrides.empty()) {
3642       // This path is a strict improvement over the last path, let's use it.
3643       BestPath = &SpecificPath;
3644       std::swap(CurrentOverrides, LastOverrides);
3645     } else {
3646       // This path introduces an overrider with a conflicting covariant thunk.
3647       DiagnosticsEngine &Diags = Context.getDiagnostics();
3648       const CXXMethodDecl *CovariantMD = *NewOverrides.begin();
3649       const CXXMethodDecl *ConflictMD = *MissingOverrides.begin();
3650       Diags.Report(RD->getLocation(), diag::err_vftable_ambiguous_component)
3651           << RD;
3652       Diags.Report(CovariantMD->getLocation(), diag::note_covariant_thunk)
3653           << CovariantMD;
3654       Diags.Report(ConflictMD->getLocation(), diag::note_covariant_thunk)
3655           << ConflictMD;
3656     }
3657   }
3658   // Go with the path that introduced the most covariant overrides.  If there is
3659   // no such path, pick the first path.
3660   return BestPath ? BestPath : &FullPaths.front();
3661 }
3662 
computeFullPathsForVFTables(ASTContext & Context,const CXXRecordDecl * RD,VPtrInfoVector & Paths)3663 static void computeFullPathsForVFTables(ASTContext &Context,
3664                                         const CXXRecordDecl *RD,
3665                                         VPtrInfoVector &Paths) {
3666   const ASTRecordLayout &MostDerivedLayout = Context.getASTRecordLayout(RD);
3667   FullPathTy FullPath;
3668   std::list<FullPathTy> FullPaths;
3669   for (const std::unique_ptr<VPtrInfo>& Info : Paths) {
3670     findPathsToSubobject(
3671         Context, MostDerivedLayout, RD, CharUnits::Zero(),
3672         BaseSubobject(Info->IntroducingObject, Info->FullOffsetInMDC), FullPath,
3673         FullPaths);
3674     FullPath.clear();
3675     removeRedundantPaths(FullPaths);
3676     Info->PathToIntroducingObject.clear();
3677     if (const FullPathTy *BestPath =
3678             selectBestPath(Context, RD, *Info, FullPaths))
3679       for (const BaseSubobject &BSO : *BestPath)
3680         Info->PathToIntroducingObject.push_back(BSO.getBase());
3681     FullPaths.clear();
3682   }
3683 }
3684 
vfptrIsEarlierInMDC(const ASTRecordLayout & Layout,const MethodVFTableLocation & LHS,const MethodVFTableLocation & RHS)3685 static bool vfptrIsEarlierInMDC(const ASTRecordLayout &Layout,
3686                                 const MethodVFTableLocation &LHS,
3687                                 const MethodVFTableLocation &RHS) {
3688   CharUnits L = LHS.VFPtrOffset;
3689   CharUnits R = RHS.VFPtrOffset;
3690   if (LHS.VBase)
3691     L += Layout.getVBaseClassOffset(LHS.VBase);
3692   if (RHS.VBase)
3693     R += Layout.getVBaseClassOffset(RHS.VBase);
3694   return L < R;
3695 }
3696 
computeVTableRelatedInformation(const CXXRecordDecl * RD)3697 void MicrosoftVTableContext::computeVTableRelatedInformation(
3698     const CXXRecordDecl *RD) {
3699   assert(RD->isDynamicClass());
3700 
3701   // Check if we've computed this information before.
3702   if (VFPtrLocations.count(RD))
3703     return;
3704 
3705   const VTableLayout::AddressPointsMapTy EmptyAddressPointsMap;
3706 
3707   {
3708     auto VFPtrs = std::make_unique<VPtrInfoVector>();
3709     computeVTablePaths(/*ForVBTables=*/false, RD, *VFPtrs);
3710     computeFullPathsForVFTables(Context, RD, *VFPtrs);
3711     VFPtrLocations[RD] = std::move(VFPtrs);
3712   }
3713 
3714   MethodVFTableLocationsTy NewMethodLocations;
3715   for (const std::unique_ptr<VPtrInfo> &VFPtr : *VFPtrLocations[RD]) {
3716     VFTableBuilder Builder(*this, RD, *VFPtr);
3717 
3718     VFTableIdTy id(RD, VFPtr->FullOffsetInMDC);
3719     assert(VFTableLayouts.count(id) == 0);
3720     SmallVector<VTableLayout::VTableThunkTy, 1> VTableThunks(
3721         Builder.vtable_thunks_begin(), Builder.vtable_thunks_end());
3722     VFTableLayouts[id] = std::make_unique<VTableLayout>(
3723         ArrayRef<size_t>{0}, Builder.vtable_components(), VTableThunks,
3724         EmptyAddressPointsMap);
3725     Thunks.insert(Builder.thunks_begin(), Builder.thunks_end());
3726 
3727     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3728     for (const auto &Loc : Builder.vtable_locations()) {
3729       auto Insert = NewMethodLocations.insert(Loc);
3730       if (!Insert.second) {
3731         const MethodVFTableLocation &NewLoc = Loc.second;
3732         MethodVFTableLocation &OldLoc = Insert.first->second;
3733         if (vfptrIsEarlierInMDC(Layout, NewLoc, OldLoc))
3734           OldLoc = NewLoc;
3735       }
3736     }
3737   }
3738 
3739   MethodVFTableLocations.insert_range(NewMethodLocations);
3740   if (Context.getLangOpts().DumpVTableLayouts)
3741     dumpMethodLocations(RD, NewMethodLocations, llvm::outs());
3742 }
3743 
dumpMethodLocations(const CXXRecordDecl * RD,const MethodVFTableLocationsTy & NewMethods,raw_ostream & Out)3744 void MicrosoftVTableContext::dumpMethodLocations(
3745     const CXXRecordDecl *RD, const MethodVFTableLocationsTy &NewMethods,
3746     raw_ostream &Out) {
3747   // Compute the vtable indices for all the member functions.
3748   // Store them in a map keyed by the location so we'll get a sorted table.
3749   std::map<MethodVFTableLocation, std::string> IndicesMap;
3750   bool HasNonzeroOffset = false;
3751 
3752   for (const auto &I : NewMethods) {
3753     const CXXMethodDecl *MD = cast<const CXXMethodDecl>(I.first.getDecl());
3754     assert(hasVtableSlot(MD));
3755 
3756     std::string MethodName = PredefinedExpr::ComputeName(
3757         PredefinedIdentKind::PrettyFunctionNoVirtual, MD);
3758 
3759     if (isa<CXXDestructorDecl>(MD)) {
3760       IndicesMap[I.second] = MethodName + " [scalar deleting]";
3761     } else {
3762       IndicesMap[I.second] = MethodName;
3763     }
3764 
3765     if (!I.second.VFPtrOffset.isZero() || I.second.VBTableIndex != 0)
3766       HasNonzeroOffset = true;
3767   }
3768 
3769   // Print the vtable indices for all the member functions.
3770   if (!IndicesMap.empty()) {
3771     Out << "VFTable indices for ";
3772     Out << "'";
3773     RD->printQualifiedName(Out);
3774     Out << "' (" << IndicesMap.size()
3775         << (IndicesMap.size() == 1 ? " entry" : " entries") << ").\n";
3776 
3777     CharUnits LastVFPtrOffset = CharUnits::fromQuantity(-1);
3778     uint64_t LastVBIndex = 0;
3779     for (const auto &I : IndicesMap) {
3780       CharUnits VFPtrOffset = I.first.VFPtrOffset;
3781       uint64_t VBIndex = I.first.VBTableIndex;
3782       if (HasNonzeroOffset &&
3783           (VFPtrOffset != LastVFPtrOffset || VBIndex != LastVBIndex)) {
3784         assert(VBIndex > LastVBIndex || VFPtrOffset > LastVFPtrOffset);
3785         Out << " -- accessible via ";
3786         if (VBIndex)
3787           Out << "vbtable index " << VBIndex << ", ";
3788         Out << "vfptr at offset " << VFPtrOffset.getQuantity() << " --\n";
3789         LastVFPtrOffset = VFPtrOffset;
3790         LastVBIndex = VBIndex;
3791       }
3792 
3793       uint64_t VTableIndex = I.first.Index;
3794       const std::string &MethodName = I.second;
3795       Out << llvm::format("%4" PRIu64 " | ", VTableIndex) << MethodName << '\n';
3796     }
3797     Out << '\n';
3798   }
3799 
3800   Out.flush();
3801 }
3802 
computeVBTableRelatedInformation(const CXXRecordDecl * RD)3803 const VirtualBaseInfo &MicrosoftVTableContext::computeVBTableRelatedInformation(
3804     const CXXRecordDecl *RD) {
3805   VirtualBaseInfo *VBI;
3806 
3807   {
3808     // Get or create a VBI for RD.  Don't hold a reference to the DenseMap cell,
3809     // as it may be modified and rehashed under us.
3810     std::unique_ptr<VirtualBaseInfo> &Entry = VBaseInfo[RD];
3811     if (Entry)
3812       return *Entry;
3813     Entry = std::make_unique<VirtualBaseInfo>();
3814     VBI = Entry.get();
3815   }
3816 
3817   computeVTablePaths(/*ForVBTables=*/true, RD, VBI->VBPtrPaths);
3818 
3819   // First, see if the Derived class shared the vbptr with a non-virtual base.
3820   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
3821   if (const CXXRecordDecl *VBPtrBase = Layout.getBaseSharingVBPtr()) {
3822     // If the Derived class shares the vbptr with a non-virtual base, the shared
3823     // virtual bases come first so that the layout is the same.
3824     const VirtualBaseInfo &BaseInfo =
3825         computeVBTableRelatedInformation(VBPtrBase);
3826     VBI->VBTableIndices.insert_range(BaseInfo.VBTableIndices);
3827   }
3828 
3829   // New vbases are added to the end of the vbtable.
3830   // Skip the self entry and vbases visited in the non-virtual base, if any.
3831   unsigned VBTableIndex = 1 + VBI->VBTableIndices.size();
3832   for (const auto &VB : RD->vbases()) {
3833     const CXXRecordDecl *CurVBase = VB.getType()->getAsCXXRecordDecl();
3834     if (VBI->VBTableIndices.try_emplace(CurVBase, VBTableIndex).second)
3835       ++VBTableIndex;
3836   }
3837 
3838   return *VBI;
3839 }
3840 
getVBTableIndex(const CXXRecordDecl * Derived,const CXXRecordDecl * VBase)3841 unsigned MicrosoftVTableContext::getVBTableIndex(const CXXRecordDecl *Derived,
3842                                                  const CXXRecordDecl *VBase) {
3843   const VirtualBaseInfo &VBInfo = computeVBTableRelatedInformation(Derived);
3844   assert(VBInfo.VBTableIndices.count(VBase));
3845   return VBInfo.VBTableIndices.find(VBase)->second;
3846 }
3847 
3848 const VPtrInfoVector &
enumerateVBTables(const CXXRecordDecl * RD)3849 MicrosoftVTableContext::enumerateVBTables(const CXXRecordDecl *RD) {
3850   return computeVBTableRelatedInformation(RD).VBPtrPaths;
3851 }
3852 
3853 const VPtrInfoVector &
getVFPtrOffsets(const CXXRecordDecl * RD)3854 MicrosoftVTableContext::getVFPtrOffsets(const CXXRecordDecl *RD) {
3855   computeVTableRelatedInformation(RD);
3856 
3857   assert(VFPtrLocations.count(RD) && "Couldn't find vfptr locations");
3858   return *VFPtrLocations[RD];
3859 }
3860 
3861 const VTableLayout &
getVFTableLayout(const CXXRecordDecl * RD,CharUnits VFPtrOffset)3862 MicrosoftVTableContext::getVFTableLayout(const CXXRecordDecl *RD,
3863                                          CharUnits VFPtrOffset) {
3864   computeVTableRelatedInformation(RD);
3865 
3866   VFTableIdTy id(RD, VFPtrOffset);
3867   assert(VFTableLayouts.count(id) && "Couldn't find a VFTable at this offset");
3868   return *VFTableLayouts[id];
3869 }
3870 
3871 MethodVFTableLocation
getMethodVFTableLocation(GlobalDecl GD)3872 MicrosoftVTableContext::getMethodVFTableLocation(GlobalDecl GD) {
3873   assert(hasVtableSlot(cast<CXXMethodDecl>(GD.getDecl())) &&
3874          "Only use this method for virtual methods or dtors");
3875   if (isa<CXXDestructorDecl>(GD.getDecl()))
3876     assert(GD.getDtorType() == Dtor_Deleting);
3877 
3878   GD = GD.getCanonicalDecl();
3879 
3880   MethodVFTableLocationsTy::iterator I = MethodVFTableLocations.find(GD);
3881   if (I != MethodVFTableLocations.end())
3882     return I->second;
3883 
3884   const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
3885 
3886   computeVTableRelatedInformation(RD);
3887 
3888   I = MethodVFTableLocations.find(GD);
3889   assert(I != MethodVFTableLocations.end() && "Did not find index!");
3890   return I->second;
3891 }
3892