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