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