CGVTables.cpp revision 663218b576d8d79dea546c5726d7c90c216b1358
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  if (Bases.count(MD->getParent()))
1612    return true;
1613
1614  for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
1615       E = MD->end_overridden_methods(); I != E; ++I) {
1616    const CXXMethodDecl *OverriddenMD = *I;
1617
1618    // Check "indirect overriders".
1619    if (OverridesIndirectMethodInBases(OverriddenMD, Bases))
1620      return true;
1621  }
1622
1623  return false;
1624}
1625
1626bool
1627VTableBuilder::IsOverriderUsed(const CXXMethodDecl *Overrider,
1628                               uint64_t BaseOffsetInLayoutClass,
1629                               const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
1630                               uint64_t FirstBaseOffsetInLayoutClass) const {
1631  // If the base and the first base in the primary base chain have the same
1632  // offsets, then this overrider will be used.
1633  if (BaseOffsetInLayoutClass == FirstBaseOffsetInLayoutClass)
1634   return true;
1635
1636  // We know now that Base (or a direct or indirect base of it) is a primary
1637  // base in part of the class hierarchy, but not a primary base in the most
1638  // derived class.
1639
1640  // If the overrider is the first base in the primary base chain, we know
1641  // that the overrider will be used.
1642  if (Overrider->getParent() == FirstBaseInPrimaryBaseChain)
1643    return true;
1644
1645  VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
1646
1647  const CXXRecordDecl *RD = FirstBaseInPrimaryBaseChain;
1648  PrimaryBases.insert(RD);
1649
1650  // Now traverse the base chain, starting with the first base, until we find
1651  // the base that is no longer a primary base.
1652  while (true) {
1653    const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1654    const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1655
1656    if (!PrimaryBase)
1657      break;
1658
1659    if (Layout.getPrimaryBaseWasVirtual()) {
1660      assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 &&
1661             "Primary base should always be at offset 0!");
1662
1663      const ASTRecordLayout &LayoutClassLayout =
1664        Context.getASTRecordLayout(LayoutClass);
1665
1666      // Now check if this is the primary base that is not a primary base in the
1667      // most derived class.
1668      if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
1669          FirstBaseOffsetInLayoutClass) {
1670        // We found it, stop walking the chain.
1671        break;
1672      }
1673    } else {
1674      assert(Layout.getBaseClassOffset(PrimaryBase) == 0 &&
1675             "Primary base should always be at offset 0!");
1676    }
1677
1678    if (!PrimaryBases.insert(PrimaryBase))
1679      assert(false && "Found a duplicate primary base!");
1680
1681    RD = PrimaryBase;
1682  }
1683
1684  // If the final overrider is an override of one of the primary bases,
1685  // then we know that it will be used.
1686  return OverridesIndirectMethodInBases(Overrider, PrimaryBases);
1687}
1688
1689/// FindNearestOverriddenMethod - Given a method, returns the overridden method
1690/// from the nearest base. Returns null if no method was found.
1691static const CXXMethodDecl *
1692FindNearestOverriddenMethod(const CXXMethodDecl *MD,
1693                            VTableBuilder::PrimaryBasesSetVectorTy &Bases) {
1694  OverriddenMethodsSetTy OverriddenMethods;
1695  ComputeAllOverriddenMethods(MD, OverriddenMethods);
1696
1697  for (int I = Bases.size(), E = 0; I != E; --I) {
1698    const CXXRecordDecl *PrimaryBase = Bases[I - 1];
1699
1700    // Now check the overriden methods.
1701    for (OverriddenMethodsSetTy::const_iterator I = OverriddenMethods.begin(),
1702         E = OverriddenMethods.end(); I != E; ++I) {
1703      const CXXMethodDecl *OverriddenMD = *I;
1704
1705      // We found our overridden method.
1706      if (OverriddenMD->getParent() == PrimaryBase)
1707        return OverriddenMD;
1708    }
1709  }
1710
1711  return 0;
1712}
1713
1714void
1715VTableBuilder::AddMethods(BaseSubobject Base, uint64_t BaseOffsetInLayoutClass,
1716                          const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
1717                          uint64_t FirstBaseOffsetInLayoutClass,
1718                          PrimaryBasesSetVectorTy &PrimaryBases) {
1719  const CXXRecordDecl *RD = Base.getBase();
1720  const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1721
1722  if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
1723    uint64_t PrimaryBaseOffset;
1724    uint64_t PrimaryBaseOffsetInLayoutClass;
1725    if (Layout.getPrimaryBaseWasVirtual()) {
1726      assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 &&
1727             "Primary vbase should have a zero offset!");
1728
1729      const ASTRecordLayout &MostDerivedClassLayout =
1730        Context.getASTRecordLayout(MostDerivedClass);
1731
1732      PrimaryBaseOffset =
1733        MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
1734
1735      const ASTRecordLayout &LayoutClassLayout =
1736        Context.getASTRecordLayout(LayoutClass);
1737
1738      PrimaryBaseOffsetInLayoutClass =
1739        LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
1740    } else {
1741      assert(Layout.getBaseClassOffset(PrimaryBase) == 0 &&
1742             "Primary base should have a zero offset!");
1743
1744      PrimaryBaseOffset = Base.getBaseOffset();
1745      PrimaryBaseOffsetInLayoutClass = BaseOffsetInLayoutClass;
1746    }
1747
1748    AddMethods(BaseSubobject(PrimaryBase, PrimaryBaseOffset),
1749               PrimaryBaseOffsetInLayoutClass, FirstBaseInPrimaryBaseChain,
1750               FirstBaseOffsetInLayoutClass, PrimaryBases);
1751
1752    if (!PrimaryBases.insert(PrimaryBase))
1753      assert(false && "Found a duplicate primary base!");
1754  }
1755
1756  // Now go through all virtual member functions and add them.
1757  for (CXXRecordDecl::method_iterator I = RD->method_begin(),
1758       E = RD->method_end(); I != E; ++I) {
1759    const CXXMethodDecl *MD = *I;
1760
1761    if (!MD->isVirtual())
1762      continue;
1763
1764    // Get the final overrider.
1765    FinalOverriders::OverriderInfo Overrider =
1766      Overriders.getOverrider(Base, MD);
1767
1768    // Check if this virtual member function overrides a method in a primary
1769    // base. If this is the case, and the return type doesn't require adjustment
1770    // then we can just use the member function from the primary base.
1771    if (const CXXMethodDecl *OverriddenMD =
1772          FindNearestOverriddenMethod(MD, PrimaryBases)) {
1773      if (ComputeReturnAdjustmentBaseOffset(Context, MD,
1774                                            OverriddenMD).isEmpty()) {
1775        // Replace the method info of the overridden method with our own
1776        // method.
1777        assert(MethodInfoMap.count(OverriddenMD) &&
1778               "Did not find the overridden method!");
1779        MethodInfo &OverriddenMethodInfo = MethodInfoMap[OverriddenMD];
1780
1781        MethodInfo MethodInfo(Base.getBaseOffset(),
1782                              BaseOffsetInLayoutClass,
1783                              OverriddenMethodInfo.VTableIndex);
1784
1785        assert(!MethodInfoMap.count(MD) &&
1786               "Should not have method info for this method yet!");
1787
1788        MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
1789        MethodInfoMap.erase(OverriddenMD);
1790
1791        // If the overridden method exists in a virtual base class or a direct
1792        // or indirect base class of a virtual base class, we need to emit a
1793        // thunk if we ever have a class hierarchy where the base class is not
1794        // a primary base in the complete object.
1795        if (!isBuildingConstructorVTable() && OverriddenMD != MD) {
1796          // Compute the this adjustment.
1797          ThisAdjustment ThisAdjustment =
1798            ComputeThisAdjustment(OverriddenMD, BaseOffsetInLayoutClass,
1799                                  Overrider);
1800
1801          if (ThisAdjustment.VCallOffsetOffset &&
1802              Overrider.Method->getParent() == MostDerivedClass) {
1803            // This is a virtual thunk for the most derived class, add it.
1804            AddThunk(Overrider.Method,
1805                     ThunkInfo(ThisAdjustment, ReturnAdjustment()));
1806          }
1807        }
1808
1809        continue;
1810      }
1811    }
1812
1813    // Insert the method info for this method.
1814    MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
1815                          Components.size());
1816
1817    assert(!MethodInfoMap.count(MD) &&
1818           "Should not have method info for this method yet!");
1819    MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
1820
1821    // Check if this overrider is going to be used.
1822    const CXXMethodDecl *OverriderMD = Overrider.Method;
1823    if (!IsOverriderUsed(OverriderMD, BaseOffsetInLayoutClass,
1824                         FirstBaseInPrimaryBaseChain,
1825                         FirstBaseOffsetInLayoutClass)) {
1826      Components.push_back(VTableComponent::MakeUnusedFunction(OverriderMD));
1827      continue;
1828    }
1829
1830    // Check if this overrider needs a return adjustment.
1831    BaseOffset ReturnAdjustmentOffset =
1832      Overriders.getReturnAdjustmentOffset(Base, MD);
1833
1834    ReturnAdjustment ReturnAdjustment =
1835      ComputeReturnAdjustment(ReturnAdjustmentOffset);
1836
1837    AddMethod(Overrider.Method, ReturnAdjustment);
1838  }
1839}
1840
1841void VTableBuilder::LayoutVTable() {
1842  LayoutPrimaryAndSecondaryVTables(BaseSubobject(MostDerivedClass, 0),
1843                                   /*BaseIsMorallyVirtual=*/false,
1844                                   MostDerivedClassIsVirtual,
1845                                   MostDerivedClassOffset);
1846
1847  VisitedVirtualBasesSetTy VBases;
1848
1849  // Determine the primary virtual bases.
1850  DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset,
1851                               VBases);
1852  VBases.clear();
1853
1854  LayoutVTablesForVirtualBases(MostDerivedClass, VBases);
1855}
1856
1857void
1858VTableBuilder::LayoutPrimaryAndSecondaryVTables(BaseSubobject Base,
1859                                                bool BaseIsMorallyVirtual,
1860                                                bool BaseIsVirtualInLayoutClass,
1861                                                uint64_t OffsetInLayoutClass) {
1862  assert(Base.getBase()->isDynamicClass() && "class does not have a vtable!");
1863
1864  // Add vcall and vbase offsets for this vtable.
1865  VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, LayoutClass, &Overriders,
1866                                     Base, BaseIsVirtualInLayoutClass,
1867                                     OffsetInLayoutClass);
1868  Components.append(Builder.components_begin(), Builder.components_end());
1869
1870  // Check if we need to add these vcall offsets.
1871  if (BaseIsVirtualInLayoutClass && !Builder.getVCallOffsets().empty()) {
1872    VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()];
1873
1874    if (VCallOffsets.empty())
1875      VCallOffsets = Builder.getVCallOffsets();
1876  }
1877
1878  // If we're laying out the most derived class we want to keep track of the
1879  // virtual base class offset offsets.
1880  if (Base.getBase() == MostDerivedClass)
1881    VBaseOffsetOffsets = Builder.getVBaseOffsetOffsets();
1882
1883  // Add the offset to top.
1884  // FIXME: We should not use / 8 here.
1885  int64_t OffsetToTop = -(int64_t)(OffsetInLayoutClass -
1886                                   MostDerivedClassOffset) / 8;
1887  Components.push_back(VTableComponent::MakeOffsetToTop(OffsetToTop));
1888
1889  // Next, add the RTTI.
1890  Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass));
1891
1892  uint64_t AddressPoint = Components.size();
1893
1894  // Now go through all virtual member functions and add them.
1895  PrimaryBasesSetVectorTy PrimaryBases;
1896  AddMethods(Base, OffsetInLayoutClass, Base.getBase(), OffsetInLayoutClass,
1897             PrimaryBases);
1898
1899  // Compute 'this' pointer adjustments.
1900  ComputeThisAdjustments();
1901
1902  // Add all address points.
1903  const CXXRecordDecl *RD = Base.getBase();
1904  while (true) {
1905    AddressPoints.insert(std::make_pair(BaseSubobject(RD, OffsetInLayoutClass),
1906                                        AddressPoint));
1907
1908    const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1909    const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1910
1911    if (!PrimaryBase)
1912      break;
1913
1914    if (Layout.getPrimaryBaseWasVirtual()) {
1915      // Check if this virtual primary base is a primary base in the layout
1916      // class. If it's not, we don't want to add it.
1917      const ASTRecordLayout &LayoutClassLayout =
1918        Context.getASTRecordLayout(LayoutClass);
1919
1920      if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
1921          OffsetInLayoutClass) {
1922        // We don't want to add this class (or any of its primary bases).
1923        break;
1924      }
1925    }
1926
1927    RD = PrimaryBase;
1928  }
1929
1930  // Layout secondary vtables.
1931  LayoutSecondaryVTables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass);
1932}
1933
1934void VTableBuilder::LayoutSecondaryVTables(BaseSubobject Base,
1935                                           bool BaseIsMorallyVirtual,
1936                                           uint64_t OffsetInLayoutClass) {
1937  // Itanium C++ ABI 2.5.2:
1938  //   Following the primary virtual table of a derived class are secondary
1939  //   virtual tables for each of its proper base classes, except any primary
1940  //   base(s) with which it shares its primary virtual table.
1941
1942  const CXXRecordDecl *RD = Base.getBase();
1943  const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1944  const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1945
1946  for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1947       E = RD->bases_end(); I != E; ++I) {
1948    // Ignore virtual bases, we'll emit them later.
1949    if (I->isVirtual())
1950      continue;
1951
1952    const CXXRecordDecl *BaseDecl =
1953      cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1954
1955    // Ignore bases that don't have a vtable.
1956    if (!BaseDecl->isDynamicClass())
1957      continue;
1958
1959    if (isBuildingConstructorVTable()) {
1960      // Itanium C++ ABI 2.6.4:
1961      //   Some of the base class subobjects may not need construction virtual
1962      //   tables, which will therefore not be present in the construction
1963      //   virtual table group, even though the subobject virtual tables are
1964      //   present in the main virtual table group for the complete object.
1965      if (!BaseIsMorallyVirtual && !BaseDecl->getNumVBases())
1966        continue;
1967    }
1968
1969    // Get the base offset of this base.
1970    uint64_t RelativeBaseOffset = Layout.getBaseClassOffset(BaseDecl);
1971    uint64_t BaseOffset = Base.getBaseOffset() + RelativeBaseOffset;
1972
1973    uint64_t BaseOffsetInLayoutClass = OffsetInLayoutClass + RelativeBaseOffset;
1974
1975    // Don't emit a secondary vtable for a primary base. We might however want
1976    // to emit secondary vtables for other bases of this base.
1977    if (BaseDecl == PrimaryBase) {
1978      LayoutSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset),
1979                             BaseIsMorallyVirtual, BaseOffsetInLayoutClass);
1980      continue;
1981    }
1982
1983    // Layout the primary vtable (and any secondary vtables) for this base.
1984    LayoutPrimaryAndSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset),
1985                                     BaseIsMorallyVirtual,
1986                                     /*BaseIsVirtualInLayoutClass=*/false,
1987                                     BaseOffsetInLayoutClass);
1988  }
1989}
1990
1991void
1992VTableBuilder::DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
1993                                            uint64_t OffsetInLayoutClass,
1994                                            VisitedVirtualBasesSetTy &VBases) {
1995  const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1996
1997  // Check if this base has a primary base.
1998  if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
1999
2000    // Check if it's virtual.
2001    if (Layout.getPrimaryBaseWasVirtual()) {
2002      bool IsPrimaryVirtualBase = true;
2003
2004      if (isBuildingConstructorVTable()) {
2005        // Check if the base is actually a primary base in the class we use for
2006        // layout.
2007        const ASTRecordLayout &LayoutClassLayout =
2008          Context.getASTRecordLayout(LayoutClass);
2009
2010        uint64_t PrimaryBaseOffsetInLayoutClass =
2011          LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
2012
2013        // We know that the base is not a primary base in the layout class if
2014        // the base offsets are different.
2015        if (PrimaryBaseOffsetInLayoutClass != OffsetInLayoutClass)
2016          IsPrimaryVirtualBase = false;
2017      }
2018
2019      if (IsPrimaryVirtualBase)
2020        PrimaryVirtualBases.insert(PrimaryBase);
2021    }
2022  }
2023
2024  // Traverse bases, looking for more primary virtual bases.
2025  for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
2026       E = RD->bases_end(); I != E; ++I) {
2027    const CXXRecordDecl *BaseDecl =
2028      cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
2029
2030    uint64_t BaseOffsetInLayoutClass;
2031
2032    if (I->isVirtual()) {
2033      if (!VBases.insert(BaseDecl))
2034        continue;
2035
2036      const ASTRecordLayout &LayoutClassLayout =
2037        Context.getASTRecordLayout(LayoutClass);
2038
2039      BaseOffsetInLayoutClass = LayoutClassLayout.getVBaseClassOffset(BaseDecl);
2040    } else {
2041      BaseOffsetInLayoutClass =
2042        OffsetInLayoutClass + Layout.getBaseClassOffset(BaseDecl);
2043    }
2044
2045    DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases);
2046  }
2047}
2048
2049void
2050VTableBuilder::LayoutVTablesForVirtualBases(const CXXRecordDecl *RD,
2051                                            VisitedVirtualBasesSetTy &VBases) {
2052  // Itanium C++ ABI 2.5.2:
2053  //   Then come the virtual base virtual tables, also in inheritance graph
2054  //   order, and again excluding primary bases (which share virtual tables with
2055  //   the classes for which they are primary).
2056  for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
2057       E = RD->bases_end(); I != E; ++I) {
2058    const CXXRecordDecl *BaseDecl =
2059      cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
2060
2061    // Check if this base needs a vtable. (If it's virtual, not a primary base
2062    // of some other class, and we haven't visited it before).
2063    if (I->isVirtual() && BaseDecl->isDynamicClass() &&
2064        !PrimaryVirtualBases.count(BaseDecl) && VBases.insert(BaseDecl)) {
2065      const ASTRecordLayout &MostDerivedClassLayout =
2066        Context.getASTRecordLayout(MostDerivedClass);
2067      uint64_t BaseOffset =
2068        MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
2069
2070      const ASTRecordLayout &LayoutClassLayout =
2071        Context.getASTRecordLayout(LayoutClass);
2072      uint64_t BaseOffsetInLayoutClass =
2073        LayoutClassLayout.getVBaseClassOffset(BaseDecl);
2074
2075      LayoutPrimaryAndSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset),
2076                                       /*BaseIsMorallyVirtual=*/true,
2077                                       /*BaseIsVirtualInLayoutClass=*/true,
2078                                       BaseOffsetInLayoutClass);
2079    }
2080
2081    // We only need to check the base for virtual base vtables if it actually
2082    // has virtual bases.
2083    if (BaseDecl->getNumVBases())
2084      LayoutVTablesForVirtualBases(BaseDecl, VBases);
2085  }
2086}
2087
2088/// dumpLayout - Dump the vtable layout.
2089void VTableBuilder::dumpLayout(llvm::raw_ostream& Out) {
2090
2091  if (isBuildingConstructorVTable()) {
2092    Out << "Construction vtable for ('";
2093    Out << MostDerivedClass->getQualifiedNameAsString() << "', ";
2094    // FIXME: Don't use / 8 .
2095    Out << MostDerivedClassOffset / 8 << ") in '";
2096    Out << LayoutClass->getQualifiedNameAsString();
2097  } else {
2098    Out << "Vtable for '";
2099    Out << MostDerivedClass->getQualifiedNameAsString();
2100  }
2101  Out << "' (" << Components.size() << " entries).\n";
2102
2103  // Iterate through the address points and insert them into a new map where
2104  // they are keyed by the index and not the base object.
2105  // Since an address point can be shared by multiple subobjects, we use an
2106  // STL multimap.
2107  std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex;
2108  for (AddressPointsMapTy::const_iterator I = AddressPoints.begin(),
2109       E = AddressPoints.end(); I != E; ++I) {
2110    const BaseSubobject& Base = I->first;
2111    uint64_t Index = I->second;
2112
2113    AddressPointsByIndex.insert(std::make_pair(Index, Base));
2114  }
2115
2116  for (unsigned I = 0, E = Components.size(); I != E; ++I) {
2117    uint64_t Index = I;
2118
2119    Out << llvm::format("%4d | ", I);
2120
2121    const VTableComponent &Component = Components[I];
2122
2123    // Dump the component.
2124    switch (Component.getKind()) {
2125
2126    case VTableComponent::CK_VCallOffset:
2127      Out << "vcall_offset (" << Component.getVCallOffset() << ")";
2128      break;
2129
2130    case VTableComponent::CK_VBaseOffset:
2131      Out << "vbase_offset (" << Component.getVBaseOffset() << ")";
2132      break;
2133
2134    case VTableComponent::CK_OffsetToTop:
2135      Out << "offset_to_top (" << Component.getOffsetToTop() << ")";
2136      break;
2137
2138    case VTableComponent::CK_RTTI:
2139      Out << Component.getRTTIDecl()->getQualifiedNameAsString() << " RTTI";
2140      break;
2141
2142    case VTableComponent::CK_FunctionPointer: {
2143      const CXXMethodDecl *MD = Component.getFunctionDecl();
2144
2145      std::string Str =
2146        PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2147                                    MD);
2148      Out << Str;
2149      if (MD->isPure())
2150        Out << " [pure]";
2151
2152      ThunkInfo Thunk = VTableThunks.lookup(I);
2153      if (!Thunk.isEmpty()) {
2154        // If this function pointer has a return adjustment, dump it.
2155        if (!Thunk.Return.isEmpty()) {
2156          Out << "\n       [return adjustment: ";
2157          Out << Thunk.Return.NonVirtual << " non-virtual";
2158
2159          if (Thunk.Return.VBaseOffsetOffset) {
2160            Out << ", " << Thunk.Return.VBaseOffsetOffset;
2161            Out << " vbase offset offset";
2162          }
2163
2164          Out << ']';
2165        }
2166
2167        // If this function pointer has a 'this' pointer adjustment, dump it.
2168        if (!Thunk.This.isEmpty()) {
2169          Out << "\n       [this adjustment: ";
2170          Out << Thunk.This.NonVirtual << " non-virtual";
2171
2172          if (Thunk.This.VCallOffsetOffset) {
2173            Out << ", " << Thunk.This.VCallOffsetOffset;
2174            Out << " vcall offset offset";
2175          }
2176
2177          Out << ']';
2178        }
2179      }
2180
2181      break;
2182    }
2183
2184    case VTableComponent::CK_CompleteDtorPointer:
2185    case VTableComponent::CK_DeletingDtorPointer: {
2186      bool IsComplete =
2187        Component.getKind() == VTableComponent::CK_CompleteDtorPointer;
2188
2189      const CXXDestructorDecl *DD = Component.getDestructorDecl();
2190
2191      Out << DD->getQualifiedNameAsString();
2192      if (IsComplete)
2193        Out << "() [complete]";
2194      else
2195        Out << "() [deleting]";
2196
2197      if (DD->isPure())
2198        Out << " [pure]";
2199
2200      ThunkInfo Thunk = VTableThunks.lookup(I);
2201      if (!Thunk.isEmpty()) {
2202        // If this destructor has a 'this' pointer adjustment, dump it.
2203        if (!Thunk.This.isEmpty()) {
2204          Out << "\n       [this adjustment: ";
2205          Out << Thunk.This.NonVirtual << " non-virtual";
2206
2207          if (Thunk.This.VCallOffsetOffset) {
2208            Out << ", " << Thunk.This.VCallOffsetOffset;
2209            Out << " vcall offset offset";
2210          }
2211
2212          Out << ']';
2213        }
2214      }
2215
2216      break;
2217    }
2218
2219    case VTableComponent::CK_UnusedFunctionPointer: {
2220      const CXXMethodDecl *MD = Component.getUnusedFunctionDecl();
2221
2222      std::string Str =
2223        PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2224                                    MD);
2225      Out << "[unused] " << Str;
2226      if (MD->isPure())
2227        Out << " [pure]";
2228    }
2229
2230    }
2231
2232    Out << '\n';
2233
2234    // Dump the next address point.
2235    uint64_t NextIndex = Index + 1;
2236    if (AddressPointsByIndex.count(NextIndex)) {
2237      if (AddressPointsByIndex.count(NextIndex) == 1) {
2238        const BaseSubobject &Base =
2239          AddressPointsByIndex.find(NextIndex)->second;
2240
2241        // FIXME: Instead of dividing by 8, we should be using CharUnits.
2242        Out << "       -- (" << Base.getBase()->getQualifiedNameAsString();
2243        Out << ", " << Base.getBaseOffset() / 8 << ") vtable address --\n";
2244      } else {
2245        uint64_t BaseOffset =
2246          AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset();
2247
2248        // We store the class names in a set to get a stable order.
2249        std::set<std::string> ClassNames;
2250        for (std::multimap<uint64_t, BaseSubobject>::const_iterator I =
2251             AddressPointsByIndex.lower_bound(NextIndex), E =
2252             AddressPointsByIndex.upper_bound(NextIndex); I != E; ++I) {
2253          assert(I->second.getBaseOffset() == BaseOffset &&
2254                 "Invalid base offset!");
2255          const CXXRecordDecl *RD = I->second.getBase();
2256          ClassNames.insert(RD->getQualifiedNameAsString());
2257        }
2258
2259        for (std::set<std::string>::const_iterator I = ClassNames.begin(),
2260             E = ClassNames.end(); I != E; ++I) {
2261          // FIXME: Instead of dividing by 8, we should be using CharUnits.
2262          Out << "       -- (" << *I;
2263          Out << ", " << BaseOffset / 8 << ") vtable address --\n";
2264        }
2265      }
2266    }
2267  }
2268
2269  Out << '\n';
2270
2271  if (isBuildingConstructorVTable())
2272    return;
2273
2274  if (MostDerivedClass->getNumVBases()) {
2275    // We store the virtual base class names and their offsets in a map to get
2276    // a stable order.
2277
2278    std::map<std::string, int64_t> ClassNamesAndOffsets;
2279    for (VBaseOffsetOffsetsMapTy::const_iterator I = VBaseOffsetOffsets.begin(),
2280         E = VBaseOffsetOffsets.end(); I != E; ++I) {
2281      std::string ClassName = I->first->getQualifiedNameAsString();
2282      int64_t OffsetOffset = I->second;
2283      ClassNamesAndOffsets.insert(std::make_pair(ClassName, OffsetOffset));
2284    }
2285
2286    Out << "Virtual base offset offsets for '";
2287    Out << MostDerivedClass->getQualifiedNameAsString() << "' (";
2288    Out << ClassNamesAndOffsets.size();
2289    Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n";
2290
2291    for (std::map<std::string, int64_t>::const_iterator I =
2292         ClassNamesAndOffsets.begin(), E = ClassNamesAndOffsets.end();
2293         I != E; ++I)
2294      Out << "   " << I->first << " | " << I->second << '\n';
2295
2296    Out << "\n";
2297  }
2298
2299  if (!Thunks.empty()) {
2300    // We store the method names in a map to get a stable order.
2301    std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls;
2302
2303    for (ThunksMapTy::const_iterator I = Thunks.begin(), E = Thunks.end();
2304         I != E; ++I) {
2305      const CXXMethodDecl *MD = I->first;
2306      std::string MethodName =
2307        PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2308                                    MD);
2309
2310      MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
2311    }
2312
2313    for (std::map<std::string, const CXXMethodDecl *>::const_iterator I =
2314         MethodNamesAndDecls.begin(), E = MethodNamesAndDecls.end();
2315         I != E; ++I) {
2316      const std::string &MethodName = I->first;
2317      const CXXMethodDecl *MD = I->second;
2318
2319      ThunkInfoVectorTy ThunksVector = Thunks[MD];
2320      std::sort(ThunksVector.begin(), ThunksVector.end());
2321
2322      Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
2323      Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n";
2324
2325      for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) {
2326        const ThunkInfo &Thunk = ThunksVector[I];
2327
2328        Out << llvm::format("%4d | ", I);
2329
2330        // If this function pointer has a return pointer adjustment, dump it.
2331        if (!Thunk.Return.isEmpty()) {
2332          Out << "return adjustment: " << Thunk.This.NonVirtual;
2333          Out << " non-virtual";
2334          if (Thunk.Return.VBaseOffsetOffset) {
2335            Out << ", " << Thunk.Return.VBaseOffsetOffset;
2336            Out << " vbase offset offset";
2337          }
2338
2339          if (!Thunk.This.isEmpty())
2340            Out << "\n       ";
2341        }
2342
2343        // If this function pointer has a 'this' pointer adjustment, dump it.
2344        if (!Thunk.This.isEmpty()) {
2345          Out << "this adjustment: ";
2346          Out << Thunk.This.NonVirtual << " non-virtual";
2347
2348          if (Thunk.This.VCallOffsetOffset) {
2349            Out << ", " << Thunk.This.VCallOffsetOffset;
2350            Out << " vcall offset offset";
2351          }
2352        }
2353
2354        Out << '\n';
2355      }
2356
2357      Out << '\n';
2358
2359    }
2360  }
2361}
2362
2363}
2364
2365void CodeGenVTables::ComputeMethodVTableIndices(const CXXRecordDecl *RD) {
2366
2367  // Itanium C++ ABI 2.5.2:
2368  //   The order of the virtual function pointers in a virtual table is the
2369  //   order of declaration of the corresponding member functions in the class.
2370  //
2371  //   There is an entry for any virtual function declared in a class,
2372  //   whether it is a new function or overrides a base class function,
2373  //   unless it overrides a function from the primary base, and conversion
2374  //   between their return types does not require an adjustment.
2375
2376  int64_t CurrentIndex = 0;
2377
2378  const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
2379  const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
2380
2381  if (PrimaryBase) {
2382    assert(PrimaryBase->isDefinition() &&
2383           "Should have the definition decl of the primary base!");
2384
2385    // Since the record decl shares its vtable pointer with the primary base
2386    // we need to start counting at the end of the primary base's vtable.
2387    CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase);
2388  }
2389
2390  // Collect all the primary bases, so we can check whether methods override
2391  // a method from the base.
2392  VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
2393  for (ASTRecordLayout::primary_base_info_iterator
2394       I = Layout.primary_base_begin(), E = Layout.primary_base_end();
2395       I != E; ++I)
2396    PrimaryBases.insert((*I).getBase());
2397
2398  const CXXDestructorDecl *ImplicitVirtualDtor = 0;
2399
2400  for (CXXRecordDecl::method_iterator i = RD->method_begin(),
2401       e = RD->method_end(); i != e; ++i) {
2402    const CXXMethodDecl *MD = *i;
2403
2404    // We only want virtual methods.
2405    if (!MD->isVirtual())
2406      continue;
2407
2408    // Check if this method overrides a method in the primary base.
2409    if (const CXXMethodDecl *OverriddenMD =
2410          FindNearestOverriddenMethod(MD, PrimaryBases)) {
2411      // Check if converting from the return type of the method to the
2412      // return type of the overridden method requires conversion.
2413      if (ComputeReturnAdjustmentBaseOffset(CGM.getContext(), MD,
2414                                            OverriddenMD).isEmpty()) {
2415        // This index is shared between the index in the vtable of the primary
2416        // base class.
2417        if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2418          const CXXDestructorDecl *OverriddenDD =
2419            cast<CXXDestructorDecl>(OverriddenMD);
2420
2421          // Add both the complete and deleting entries.
2422          MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] =
2423            getMethodVTableIndex(GlobalDecl(OverriddenDD, Dtor_Complete));
2424          MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] =
2425            getMethodVTableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting));
2426        } else {
2427          MethodVTableIndices[MD] = getMethodVTableIndex(OverriddenMD);
2428        }
2429
2430        // We don't need to add an entry for this method.
2431        continue;
2432      }
2433    }
2434
2435    if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2436      if (MD->isImplicit()) {
2437        assert(!ImplicitVirtualDtor &&
2438               "Did already see an implicit virtual dtor!");
2439        ImplicitVirtualDtor = DD;
2440        continue;
2441      }
2442
2443      // Add the complete dtor.
2444      MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++;
2445
2446      // Add the deleting dtor.
2447      MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++;
2448    } else {
2449      // Add the entry.
2450      MethodVTableIndices[MD] = CurrentIndex++;
2451    }
2452  }
2453
2454  if (ImplicitVirtualDtor) {
2455    // Itanium C++ ABI 2.5.2:
2456    //   If a class has an implicitly-defined virtual destructor,
2457    //   its entries come after the declared virtual function pointers.
2458
2459    // Add the complete dtor.
2460    MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] =
2461      CurrentIndex++;
2462
2463    // Add the deleting dtor.
2464    MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] =
2465      CurrentIndex++;
2466  }
2467
2468  NumVirtualFunctionPointers[RD] = CurrentIndex;
2469}
2470
2471uint64_t CodeGenVTables::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) {
2472  llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
2473    NumVirtualFunctionPointers.find(RD);
2474  if (I != NumVirtualFunctionPointers.end())
2475    return I->second;
2476
2477  ComputeMethodVTableIndices(RD);
2478
2479  I = NumVirtualFunctionPointers.find(RD);
2480  assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!");
2481  return I->second;
2482}
2483
2484uint64_t CodeGenVTables::getMethodVTableIndex(GlobalDecl GD) {
2485  MethodVTableIndicesTy::iterator I = MethodVTableIndices.find(GD);
2486  if (I != MethodVTableIndices.end())
2487    return I->second;
2488
2489  const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
2490
2491  ComputeMethodVTableIndices(RD);
2492
2493  I = MethodVTableIndices.find(GD);
2494  assert(I != MethodVTableIndices.end() && "Did not find index!");
2495  return I->second;
2496}
2497
2498int64_t CodeGenVTables::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD,
2499                                                   const CXXRecordDecl *VBase) {
2500  ClassPairTy ClassPair(RD, VBase);
2501
2502  VirtualBaseClassOffsetOffsetsMapTy::iterator I =
2503    VirtualBaseClassOffsetOffsets.find(ClassPair);
2504  if (I != VirtualBaseClassOffsetOffsets.end())
2505    return I->second;
2506
2507  VCallAndVBaseOffsetBuilder Builder(RD, RD, /*FinalOverriders=*/0,
2508                                     BaseSubobject(RD, 0),
2509                                     /*BaseIsVirtual=*/false,
2510                                     /*OffsetInLayoutClass=*/0);
2511
2512  for (VCallAndVBaseOffsetBuilder::VBaseOffsetOffsetsMapTy::const_iterator I =
2513       Builder.getVBaseOffsetOffsets().begin(),
2514       E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) {
2515    // Insert all types.
2516    ClassPairTy ClassPair(RD, I->first);
2517
2518    VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second));
2519  }
2520
2521  I = VirtualBaseClassOffsetOffsets.find(ClassPair);
2522  assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!");
2523
2524  return I->second;
2525}
2526
2527uint64_t
2528CodeGenVTables::getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD) {
2529  assert(AddressPoints.count(std::make_pair(RD, Base)) &&
2530         "Did not find address point!");
2531
2532  uint64_t AddressPoint = AddressPoints.lookup(std::make_pair(RD, Base));
2533  assert(AddressPoint && "Address point must not be zero!");
2534
2535  return AddressPoint;
2536}
2537
2538llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
2539                                              const ThunkInfo &Thunk) {
2540  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2541
2542  // Compute the mangled name.
2543  llvm::SmallString<256> Name;
2544  if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
2545    getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), Thunk.This,
2546                                          Name);
2547  else
2548    getMangleContext().mangleThunk(MD, Thunk, Name);
2549
2550  const llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(MD);
2551  return GetOrCreateLLVMFunction(Name, Ty, GD);
2552}
2553
2554static llvm::Value *PerformTypeAdjustment(CodeGenFunction &CGF,
2555                                          llvm::Value *Ptr,
2556                                          int64_t NonVirtualAdjustment,
2557                                          int64_t VirtualAdjustment) {
2558  if (!NonVirtualAdjustment && !VirtualAdjustment)
2559    return Ptr;
2560
2561  const llvm::Type *Int8PtrTy =
2562    llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2563
2564  llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy);
2565
2566  if (NonVirtualAdjustment) {
2567    // Do the non-virtual adjustment.
2568    V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
2569  }
2570
2571  if (VirtualAdjustment) {
2572    const llvm::Type *PtrDiffTy =
2573      CGF.ConvertType(CGF.getContext().getPointerDiffType());
2574
2575    // Do the virtual adjustment.
2576    llvm::Value *VTablePtrPtr =
2577      CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo());
2578
2579    llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
2580
2581    llvm::Value *OffsetPtr =
2582      CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
2583
2584    OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
2585
2586    // Load the adjustment offset from the vtable.
2587    llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr);
2588
2589    // Adjust our pointer.
2590    V = CGF.Builder.CreateInBoundsGEP(V, Offset);
2591  }
2592
2593  // Cast back to the original type.
2594  return CGF.Builder.CreateBitCast(V, Ptr->getType());
2595}
2596
2597void CodeGenFunction::GenerateThunk(llvm::Function *Fn, GlobalDecl GD,
2598                                    const ThunkInfo &Thunk) {
2599  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2600  const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
2601  QualType ResultType = FPT->getResultType();
2602  QualType ThisType = MD->getThisType(getContext());
2603
2604  FunctionArgList FunctionArgs;
2605
2606  // FIXME: It would be nice if more of this code could be shared with
2607  // CodeGenFunction::GenerateCode.
2608
2609  // Create the implicit 'this' parameter declaration.
2610  CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0,
2611                                          MD->getLocation(),
2612                                          &getContext().Idents.get("this"),
2613                                          ThisType);
2614
2615  // Add the 'this' parameter.
2616  FunctionArgs.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType()));
2617
2618  // Add the rest of the parameters.
2619  for (FunctionDecl::param_const_iterator I = MD->param_begin(),
2620       E = MD->param_end(); I != E; ++I) {
2621    ParmVarDecl *Param = *I;
2622
2623    FunctionArgs.push_back(std::make_pair(Param, Param->getType()));
2624  }
2625
2626  StartFunction(GlobalDecl(), ResultType, Fn, FunctionArgs, SourceLocation());
2627
2628  // Adjust the 'this' pointer if necessary.
2629  llvm::Value *AdjustedThisPtr =
2630    PerformTypeAdjustment(*this, LoadCXXThis(),
2631                          Thunk.This.NonVirtual,
2632                          Thunk.This.VCallOffsetOffset);
2633
2634  CallArgList CallArgs;
2635
2636  // Add our adjusted 'this' pointer.
2637  CallArgs.push_back(std::make_pair(RValue::get(AdjustedThisPtr), ThisType));
2638
2639  // Add the rest of the parameters.
2640  for (FunctionDecl::param_const_iterator I = MD->param_begin(),
2641       E = MD->param_end(); I != E; ++I) {
2642    ParmVarDecl *Param = *I;
2643    QualType ArgType = Param->getType();
2644
2645    // Load the argument corresponding to this parameter.
2646    RValue Arg;
2647    if (ArgType->isReferenceType() ||
2648        (hasAggregateLLVMType(ArgType) && !ArgType->isAnyComplexType()))
2649      Arg = RValue::get(Builder.CreateLoad(LocalDeclMap[Param]));
2650    else
2651      Arg = RValue::get(EmitLoadOfScalar(LocalDeclMap[Param], false, ArgType));
2652
2653    CallArgs.push_back(std::make_pair(Arg, ArgType));
2654  }
2655
2656  // Get our callee.
2657  const llvm::Type *Ty =
2658    CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
2659                                   FPT->isVariadic());
2660  llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty);
2661
2662  const CGFunctionInfo &FnInfo =
2663    CGM.getTypes().getFunctionInfo(ResultType, CallArgs,
2664                                   FPT->getExtInfo());
2665
2666  // Determine whether we have a return value slot to use.
2667  ReturnValueSlot Slot;
2668  if (!ResultType->isVoidType() &&
2669      FnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect &&
2670      hasAggregateLLVMType(CurFnInfo->getReturnType()))
2671    Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
2672
2673  // Now emit our call.
2674  RValue RV = EmitCall(FnInfo, Callee, Slot, CallArgs, MD);
2675
2676  if (!Thunk.Return.isEmpty()) {
2677    // Emit the return adjustment.
2678    bool NullCheckValue = !ResultType->isReferenceType();
2679
2680    llvm::BasicBlock *AdjustNull = 0;
2681    llvm::BasicBlock *AdjustNotNull = 0;
2682    llvm::BasicBlock *AdjustEnd = 0;
2683
2684    llvm::Value *ReturnValue = RV.getScalarVal();
2685
2686    if (NullCheckValue) {
2687      AdjustNull = createBasicBlock("adjust.null");
2688      AdjustNotNull = createBasicBlock("adjust.notnull");
2689      AdjustEnd = createBasicBlock("adjust.end");
2690
2691      llvm::Value *IsNull = Builder.CreateIsNull(ReturnValue);
2692      Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
2693      EmitBlock(AdjustNotNull);
2694    }
2695
2696    ReturnValue = PerformTypeAdjustment(*this, ReturnValue,
2697                                        Thunk.Return.NonVirtual,
2698                                        Thunk.Return.VBaseOffsetOffset);
2699
2700    if (NullCheckValue) {
2701      Builder.CreateBr(AdjustEnd);
2702      EmitBlock(AdjustNull);
2703      Builder.CreateBr(AdjustEnd);
2704      EmitBlock(AdjustEnd);
2705
2706      llvm::PHINode *PHI = Builder.CreatePHI(ReturnValue->getType());
2707      PHI->reserveOperandSpace(2);
2708      PHI->addIncoming(ReturnValue, AdjustNotNull);
2709      PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
2710                       AdjustNull);
2711      ReturnValue = PHI;
2712    }
2713
2714    RV = RValue::get(ReturnValue);
2715  }
2716
2717  if (!ResultType->isVoidType() && Slot.isNull())
2718    EmitReturnOfRValue(RV, ResultType);
2719
2720  FinishFunction();
2721
2722  // Destroy the 'this' declaration.
2723  CXXThisDecl->Destroy(getContext());
2724
2725  // Set the right linkage.
2726  Fn->setLinkage(CGM.getFunctionLinkage(MD));
2727
2728  // Set the right visibility.
2729  CGM.setGlobalVisibility(Fn, MD);
2730}
2731
2732void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk)
2733{
2734  llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk);
2735  const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2736
2737  // Strip off a bitcast if we got one back.
2738  if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
2739    assert(CE->getOpcode() == llvm::Instruction::BitCast);
2740    Entry = CE->getOperand(0);
2741  }
2742
2743  // There's already a declaration with the same name, check if it has the same
2744  // type or if we need to replace it.
2745  if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() !=
2746      CGM.getTypes().GetFunctionTypeForVTable(MD)) {
2747    llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry);
2748
2749    // If the types mismatch then we have to rewrite the definition.
2750    assert(OldThunkFn->isDeclaration() &&
2751           "Shouldn't replace non-declaration");
2752
2753    // Remove the name from the old thunk function and get a new thunk.
2754    OldThunkFn->setName(llvm::StringRef());
2755    Entry = CGM.GetAddrOfThunk(GD, Thunk);
2756
2757    // If needed, replace the old thunk with a bitcast.
2758    if (!OldThunkFn->use_empty()) {
2759      llvm::Constant *NewPtrForOldDecl =
2760        llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
2761      OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
2762    }
2763
2764    // Remove the old thunk.
2765    OldThunkFn->eraseFromParent();
2766  }
2767
2768  // Actually generate the thunk body.
2769  llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
2770  CodeGenFunction(CGM).GenerateThunk(ThunkFn, GD, Thunk);
2771}
2772
2773void CodeGenVTables::EmitThunks(GlobalDecl GD)
2774{
2775  const CXXMethodDecl *MD =
2776    cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
2777
2778  // We don't need to generate thunks for the base destructor.
2779  if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
2780    return;
2781
2782  const CXXRecordDecl *RD = MD->getParent();
2783
2784  // Compute VTable related info for this class.
2785  ComputeVTableRelatedInformation(RD);
2786
2787  ThunksMapTy::const_iterator I = Thunks.find(MD);
2788  if (I == Thunks.end()) {
2789    // We did not find a thunk for this method.
2790    return;
2791  }
2792
2793  const ThunkInfoVectorTy &ThunkInfoVector = I->second;
2794  for (unsigned I = 0, E = ThunkInfoVector.size(); I != E; ++I)
2795    EmitThunk(GD, ThunkInfoVector[I]);
2796}
2797
2798void CodeGenVTables::ComputeVTableRelatedInformation(const CXXRecordDecl *RD) {
2799  uint64_t *&LayoutData = VTableLayoutMap[RD];
2800
2801  // Check if we've computed this information before.
2802  if (LayoutData)
2803    return;
2804
2805  // We may need to generate a definition for this vtable.
2806  if (!isKeyFunctionInAnotherTU(CGM.getContext(), RD) &&
2807      RD->getTemplateSpecializationKind()
2808                                      != TSK_ExplicitInstantiationDeclaration)
2809    CGM.DeferredVTables.push_back(RD);
2810
2811  VTableBuilder Builder(*this, RD, 0, /*MostDerivedClassIsVirtual=*/0, RD);
2812
2813  // Add the VTable layout.
2814  uint64_t NumVTableComponents = Builder.getNumVTableComponents();
2815  LayoutData = new uint64_t[NumVTableComponents + 1];
2816
2817  // Store the number of components.
2818  LayoutData[0] = NumVTableComponents;
2819
2820  // Store the components.
2821  std::copy(Builder.vtable_components_data_begin(),
2822            Builder.vtable_components_data_end(),
2823            &LayoutData[1]);
2824
2825  // Add the known thunks.
2826  Thunks.insert(Builder.thunks_begin(), Builder.thunks_end());
2827
2828  // Add the thunks needed in this vtable.
2829  assert(!VTableThunksMap.count(RD) &&
2830         "Thunks already exists for this vtable!");
2831
2832  VTableThunksTy &VTableThunks = VTableThunksMap[RD];
2833  VTableThunks.append(Builder.vtable_thunks_begin(),
2834                      Builder.vtable_thunks_end());
2835
2836  // Sort them.
2837  std::sort(VTableThunks.begin(), VTableThunks.end());
2838
2839  // Add the address points.
2840  for (VTableBuilder::AddressPointsMapTy::const_iterator I =
2841       Builder.address_points_begin(), E = Builder.address_points_end();
2842       I != E; ++I) {
2843
2844    uint64_t &AddressPoint = AddressPoints[std::make_pair(RD, I->first)];
2845
2846    // Check if we already have the address points for this base.
2847    assert(!AddressPoint && "Address point already exists for this base!");
2848
2849    AddressPoint = I->second;
2850  }
2851
2852  // If we don't have the vbase information for this class, insert it.
2853  // getVirtualBaseOffsetOffset will compute it separately without computing
2854  // the rest of the vtable related information.
2855  if (!RD->getNumVBases())
2856    return;
2857
2858  const RecordType *VBaseRT =
2859    RD->vbases_begin()->getType()->getAs<RecordType>();
2860  const CXXRecordDecl *VBase = cast<CXXRecordDecl>(VBaseRT->getDecl());
2861
2862  if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase)))
2863    return;
2864
2865  for (VTableBuilder::VBaseOffsetOffsetsMapTy::const_iterator I =
2866       Builder.getVBaseOffsetOffsets().begin(),
2867       E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) {
2868    // Insert all types.
2869    ClassPairTy ClassPair(RD, I->first);
2870
2871    VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second));
2872  }
2873}
2874
2875llvm::Constant *
2876CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD,
2877                                        const uint64_t *Components,
2878                                        unsigned NumComponents,
2879                                        const VTableThunksTy &VTableThunks) {
2880  llvm::SmallVector<llvm::Constant *, 64> Inits;
2881
2882  const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
2883
2884  const llvm::Type *PtrDiffTy =
2885    CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
2886
2887  QualType ClassType = CGM.getContext().getTagDeclType(RD);
2888  llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType);
2889
2890  unsigned NextVTableThunkIndex = 0;
2891
2892  llvm::Constant* PureVirtualFn = 0;
2893
2894  for (unsigned I = 0; I != NumComponents; ++I) {
2895    VTableComponent Component =
2896      VTableComponent::getFromOpaqueInteger(Components[I]);
2897
2898    llvm::Constant *Init = 0;
2899
2900    switch (Component.getKind()) {
2901    case VTableComponent::CK_VCallOffset:
2902      Init = llvm::ConstantInt::get(PtrDiffTy, Component.getVCallOffset());
2903      Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
2904      break;
2905    case VTableComponent::CK_VBaseOffset:
2906      Init = llvm::ConstantInt::get(PtrDiffTy, Component.getVBaseOffset());
2907      Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
2908      break;
2909    case VTableComponent::CK_OffsetToTop:
2910      Init = llvm::ConstantInt::get(PtrDiffTy, Component.getOffsetToTop());
2911      Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
2912      break;
2913    case VTableComponent::CK_RTTI:
2914      Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
2915      break;
2916    case VTableComponent::CK_FunctionPointer:
2917    case VTableComponent::CK_CompleteDtorPointer:
2918    case VTableComponent::CK_DeletingDtorPointer: {
2919      GlobalDecl GD;
2920
2921      // Get the right global decl.
2922      switch (Component.getKind()) {
2923      default:
2924        llvm_unreachable("Unexpected vtable component kind");
2925      case VTableComponent::CK_FunctionPointer:
2926        GD = Component.getFunctionDecl();
2927        break;
2928      case VTableComponent::CK_CompleteDtorPointer:
2929        GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
2930        break;
2931      case VTableComponent::CK_DeletingDtorPointer:
2932        GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
2933        break;
2934      }
2935
2936      if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
2937        // We have a pure virtual member function.
2938        if (!PureVirtualFn) {
2939          const llvm::FunctionType *Ty =
2940            llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()),
2941                                    /*isVarArg=*/false);
2942          PureVirtualFn =
2943            CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual");
2944          PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
2945                                                         Int8PtrTy);
2946        }
2947
2948        Init = PureVirtualFn;
2949      } else {
2950        // Check if we should use a thunk.
2951        if (NextVTableThunkIndex < VTableThunks.size() &&
2952            VTableThunks[NextVTableThunkIndex].first == I) {
2953          const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
2954
2955          Init = CGM.GetAddrOfThunk(GD, Thunk);
2956
2957          NextVTableThunkIndex++;
2958        } else {
2959          const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2960          const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(MD);
2961
2962          Init = CGM.GetAddrOfFunction(GD, Ty);
2963        }
2964
2965        Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
2966      }
2967      break;
2968    }
2969
2970    case VTableComponent::CK_UnusedFunctionPointer:
2971      Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
2972      break;
2973    };
2974
2975    Inits.push_back(Init);
2976  }
2977
2978  llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
2979  return llvm::ConstantArray::get(ArrayType, Inits.data(), Inits.size());
2980}
2981
2982/// GetGlobalVariable - Will return a global variable of the given type.
2983/// If a variable with a different type already exists then a new variable
2984/// with the right type will be created.
2985/// FIXME: We should move this to CodeGenModule and rename it to something
2986/// better and then use it in CGVTT and CGRTTI.
2987static llvm::GlobalVariable *
2988GetGlobalVariable(llvm::Module &Module, llvm::StringRef Name,
2989                  const llvm::Type *Ty,
2990                  llvm::GlobalValue::LinkageTypes Linkage) {
2991
2992  llvm::GlobalVariable *GV = Module.getNamedGlobal(Name);
2993  llvm::GlobalVariable *OldGV = 0;
2994
2995  if (GV) {
2996    // Check if the variable has the right type.
2997    if (GV->getType()->getElementType() == Ty)
2998      return GV;
2999
3000    assert(GV->isDeclaration() && "Declaration has wrong type!");
3001
3002    OldGV = GV;
3003  }
3004
3005  // Create a new variable.
3006  GV = new llvm::GlobalVariable(Module, Ty, /*isConstant=*/true,
3007                                Linkage, 0, Name);
3008
3009  if (OldGV) {
3010    // Replace occurrences of the old variable if needed.
3011    GV->takeName(OldGV);
3012
3013    if (!OldGV->use_empty()) {
3014      llvm::Constant *NewPtrForOldDecl =
3015        llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
3016      OldGV->replaceAllUsesWith(NewPtrForOldDecl);
3017    }
3018
3019    OldGV->eraseFromParent();
3020  }
3021
3022  return GV;
3023}
3024
3025llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTable(const CXXRecordDecl *RD) {
3026  llvm::SmallString<256> OutName;
3027  CGM.getMangleContext().mangleCXXVTable(RD, OutName);
3028  llvm::StringRef Name = OutName.str();
3029
3030  ComputeVTableRelatedInformation(RD);
3031
3032  const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
3033  llvm::ArrayType *ArrayType =
3034    llvm::ArrayType::get(Int8PtrTy, getNumVTableComponents(RD));
3035
3036  return GetGlobalVariable(CGM.getModule(), Name, ArrayType,
3037                           llvm::GlobalValue::ExternalLinkage);
3038}
3039
3040void
3041CodeGenVTables::EmitVTableDefinition(llvm::GlobalVariable *VTable,
3042                                     llvm::GlobalVariable::LinkageTypes Linkage,
3043                                     const CXXRecordDecl *RD) {
3044  // Dump the vtable layout if necessary.
3045  if (CGM.getLangOptions().DumpVTableLayouts) {
3046    VTableBuilder Builder(*this, RD, 0, /*MostDerivedClassIsVirtual=*/0, RD);
3047
3048    Builder.dumpLayout(llvm::errs());
3049  }
3050
3051  assert(VTableThunksMap.count(RD) &&
3052         "No thunk status for this record decl!");
3053
3054  const VTableThunksTy& Thunks = VTableThunksMap[RD];
3055
3056  // Create and set the initializer.
3057  llvm::Constant *Init =
3058    CreateVTableInitializer(RD, getVTableComponentsData(RD),
3059                            getNumVTableComponents(RD), Thunks);
3060  VTable->setInitializer(Init);
3061
3062  // Set the correct linkage.
3063  VTable->setLinkage(Linkage);
3064}
3065
3066llvm::GlobalVariable *
3067CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
3068                                      const BaseSubobject &Base,
3069                                      bool BaseIsVirtual,
3070                                      VTableAddressPointsMapTy& AddressPoints) {
3071  VTableBuilder Builder(*this, Base.getBase(), Base.getBaseOffset(),
3072                        /*MostDerivedClassIsVirtual=*/BaseIsVirtual, RD);
3073
3074  // Dump the vtable layout if necessary.
3075  if (CGM.getLangOptions().DumpVTableLayouts)
3076    Builder.dumpLayout(llvm::errs());
3077
3078  // Add the address points.
3079  AddressPoints.insert(Builder.address_points_begin(),
3080                       Builder.address_points_end());
3081
3082  // Get the mangled construction vtable name.
3083  llvm::SmallString<256> OutName;
3084  CGM.getMangleContext().mangleCXXCtorVTable(RD, Base.getBaseOffset() / 8,
3085                                             Base.getBase(), OutName);
3086  llvm::StringRef Name = OutName.str();
3087
3088  const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
3089  llvm::ArrayType *ArrayType =
3090    llvm::ArrayType::get(Int8PtrTy, Builder.getNumVTableComponents());
3091
3092  // Create the variable that will hold the construction vtable.
3093  llvm::GlobalVariable *VTable =
3094    GetGlobalVariable(CGM.getModule(), Name, ArrayType,
3095                      llvm::GlobalValue::InternalLinkage);
3096
3097  // Add the thunks.
3098  VTableThunksTy VTableThunks;
3099  VTableThunks.append(Builder.vtable_thunks_begin(),
3100                      Builder.vtable_thunks_end());
3101
3102  // Sort them.
3103  std::sort(VTableThunks.begin(), VTableThunks.end());
3104
3105  // Create and set the initializer.
3106  llvm::Constant *Init =
3107    CreateVTableInitializer(Base.getBase(),
3108                            Builder.vtable_components_data_begin(),
3109                            Builder.getNumVTableComponents(), VTableThunks);
3110  VTable->setInitializer(Init);
3111
3112  return VTable;
3113}
3114
3115void
3116CodeGenVTables::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
3117                                  const CXXRecordDecl *RD) {
3118  llvm::GlobalVariable *&VTable = VTables[RD];
3119  if (VTable) {
3120    assert(VTable->getInitializer() && "VTable doesn't have a definition!");
3121    return;
3122  }
3123
3124  VTable = GetAddrOfVTable(RD);
3125  EmitVTableDefinition(VTable, Linkage, RD);
3126
3127  GenerateVTT(Linkage, /*GenerateDefinition=*/true, RD);
3128
3129  // If this is the magic class __cxxabiv1::__fundamental_type_info,
3130  // we will emit the typeinfo for the fundamental types. This is the
3131  // same behaviour as GCC.
3132  const DeclContext *DC = RD->getDeclContext();
3133  if (RD->getIdentifier() &&
3134      RD->getIdentifier()->isStr("__fundamental_type_info") &&
3135      isa<NamespaceDecl>(DC) &&
3136      cast<NamespaceDecl>(DC)->getIdentifier() &&
3137      cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
3138      DC->getParent()->isTranslationUnit())
3139    CGM.EmitFundamentalRTTIDescriptors();
3140}
3141