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