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