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