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