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