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