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.getBaseClassOffset(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.getVBaseClassOffset(BaseDecl);
362       BaseOffsetInLayoutClass =
363         LayoutClassLayout.getVBaseClassOffset(BaseDecl);
364     } else {
365       const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
366       uint64_t Offset = Layout.getBaseClassOffset(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.getVBaseClassOffset(BaseDecl);
400     } else {
401       BaseOffset = Layout.getBaseClassOffset(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.getPrimaryBaseWasVirtual();
797 
798     uint64_t PrimaryBaseOffset;
799 
800     // Get the base offset of the primary base.
801     if (PrimaryBaseIsVirtual) {
802       assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 &&
803              "Primary vbase should have a zero offset!");
804 
805       const ASTRecordLayout &MostDerivedClassLayout =
806         Context.getASTRecordLayout(MostDerivedClass);
807 
808       PrimaryBaseOffset =
809         MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
810     } else {
811       assert(Layout.getBaseClassOffset(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.getPrimaryBaseWasVirtual()) {
853     // Get the base offset of the primary base.
854     assert(Layout.getBaseClassOffset(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.getBaseClassOffset(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.getVBaseClassOffset(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.getVBaseClassOffset(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.getPrimaryBaseWasVirtual()) {
1524       assert(Layout.getVBaseClassOffset(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.getVBaseClassOffset(PrimaryBase) !=
1533           FirstBaseOffsetInLayoutClass) {
1534         // We found it, stop walking the chain.
1535         break;
1536       }
1537     } else {
1538       assert(Layout.getBaseClassOffset(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.getPrimaryBaseWasVirtual()) {
1590       assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 &&
1591              "Primary vbase should have a zero offset!");
1592 
1593       const ASTRecordLayout &MostDerivedClassLayout =
1594         Context.getASTRecordLayout(MostDerivedClass);
1595 
1596       PrimaryBaseOffset =
1597         MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
1598 
1599       const ASTRecordLayout &LayoutClassLayout =
1600         Context.getASTRecordLayout(LayoutClass);
1601 
1602       PrimaryBaseOffsetInLayoutClass =
1603         LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
1604     } else {
1605       assert(Layout.getBaseClassOffset(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             // This is a virtual thunk for the most derived class, add it.
1668             AddThunk(Overrider.Method,
1669                      ThunkInfo(ThisAdjustment, ReturnAdjustment()));
1670           }
1671         }
1672 
1673         continue;
1674       }
1675     }
1676 
1677     // Insert the method info for this method.
1678     MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
1679                           Components.size());
1680 
1681     assert(!MethodInfoMap.count(MD) &&
1682            "Should not have method info for this method yet!");
1683     MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
1684 
1685     // Check if this overrider is going to be used.
1686     const CXXMethodDecl *OverriderMD = Overrider.Method;
1687     if (!IsOverriderUsed(OverriderMD, BaseOffsetInLayoutClass,
1688                          FirstBaseInPrimaryBaseChain,
1689                          FirstBaseOffsetInLayoutClass)) {
1690       Components.push_back(VTableComponent::MakeUnusedFunction(OverriderMD));
1691       continue;
1692     }
1693 
1694     // Check if this overrider needs a return adjustment.
1695     // We don't want to do this for pure virtual member functions.
1696     BaseOffset ReturnAdjustmentOffset;
1697     if (!OverriderMD->isPure()) {
1698       ReturnAdjustmentOffset =
1699         ComputeReturnAdjustmentBaseOffset(Context, OverriderMD, MD);
1700     }
1701 
1702     ReturnAdjustment ReturnAdjustment =
1703       ComputeReturnAdjustment(ReturnAdjustmentOffset);
1704 
1705     AddMethod(Overrider.Method, ReturnAdjustment);
1706   }
1707 }
1708 
1709 void VTableBuilder::LayoutVTable() {
1710   LayoutPrimaryAndSecondaryVTables(BaseSubobject(MostDerivedClass, 0),
1711                                    /*BaseIsMorallyVirtual=*/false,
1712                                    MostDerivedClassIsVirtual,
1713                                    MostDerivedClassOffset);
1714 
1715   VisitedVirtualBasesSetTy VBases;
1716 
1717   // Determine the primary virtual bases.
1718   DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset,
1719                                VBases);
1720   VBases.clear();
1721 
1722   LayoutVTablesForVirtualBases(MostDerivedClass, VBases);
1723 }
1724 
1725 void
1726 VTableBuilder::LayoutPrimaryAndSecondaryVTables(BaseSubobject Base,
1727                                                 bool BaseIsMorallyVirtual,
1728                                                 bool BaseIsVirtualInLayoutClass,
1729                                                 uint64_t OffsetInLayoutClass) {
1730   assert(Base.getBase()->isDynamicClass() && "class does not have a vtable!");
1731 
1732   // Add vcall and vbase offsets for this vtable.
1733   VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, LayoutClass, &Overriders,
1734                                      Base, BaseIsVirtualInLayoutClass,
1735                                      OffsetInLayoutClass);
1736   Components.append(Builder.components_begin(), Builder.components_end());
1737 
1738   // Check if we need to add these vcall offsets.
1739   if (BaseIsVirtualInLayoutClass && !Builder.getVCallOffsets().empty()) {
1740     VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()];
1741 
1742     if (VCallOffsets.empty())
1743       VCallOffsets = Builder.getVCallOffsets();
1744   }
1745 
1746   // If we're laying out the most derived class we want to keep track of the
1747   // virtual base class offset offsets.
1748   if (Base.getBase() == MostDerivedClass)
1749     VBaseOffsetOffsets = Builder.getVBaseOffsetOffsets();
1750 
1751   // Add the offset to top.
1752   // FIXME: We should not use / 8 here.
1753   int64_t OffsetToTop = -(int64_t)(OffsetInLayoutClass -
1754                                    MostDerivedClassOffset) / 8;
1755   Components.push_back(VTableComponent::MakeOffsetToTop(OffsetToTop));
1756 
1757   // Next, add the RTTI.
1758   Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass));
1759 
1760   uint64_t AddressPoint = Components.size();
1761 
1762   // Now go through all virtual member functions and add them.
1763   PrimaryBasesSetVectorTy PrimaryBases;
1764   AddMethods(Base, OffsetInLayoutClass, Base.getBase(), OffsetInLayoutClass,
1765              PrimaryBases);
1766 
1767   // Compute 'this' pointer adjustments.
1768   ComputeThisAdjustments();
1769 
1770   // Add all address points.
1771   const CXXRecordDecl *RD = Base.getBase();
1772   while (true) {
1773     AddressPoints.insert(std::make_pair(BaseSubobject(RD, OffsetInLayoutClass),
1774                                         AddressPoint));
1775 
1776     const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1777     const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1778 
1779     if (!PrimaryBase)
1780       break;
1781 
1782     if (Layout.getPrimaryBaseWasVirtual()) {
1783       // Check if this virtual primary base is a primary base in the layout
1784       // class. If it's not, we don't want to add it.
1785       const ASTRecordLayout &LayoutClassLayout =
1786         Context.getASTRecordLayout(LayoutClass);
1787 
1788       if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
1789           OffsetInLayoutClass) {
1790         // We don't want to add this class (or any of its primary bases).
1791         break;
1792       }
1793     }
1794 
1795     RD = PrimaryBase;
1796   }
1797 
1798   // Layout secondary vtables.
1799   LayoutSecondaryVTables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass);
1800 }
1801 
1802 void VTableBuilder::LayoutSecondaryVTables(BaseSubobject Base,
1803                                            bool BaseIsMorallyVirtual,
1804                                            uint64_t OffsetInLayoutClass) {
1805   // Itanium C++ ABI 2.5.2:
1806   //   Following the primary virtual table of a derived class are secondary
1807   //   virtual tables for each of its proper base classes, except any primary
1808   //   base(s) with which it shares its primary virtual table.
1809 
1810   const CXXRecordDecl *RD = Base.getBase();
1811   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1812   const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1813 
1814   for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1815        E = RD->bases_end(); I != E; ++I) {
1816     // Ignore virtual bases, we'll emit them later.
1817     if (I->isVirtual())
1818       continue;
1819 
1820     const CXXRecordDecl *BaseDecl =
1821       cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1822 
1823     // Ignore bases that don't have a vtable.
1824     if (!BaseDecl->isDynamicClass())
1825       continue;
1826 
1827     if (isBuildingConstructorVTable()) {
1828       // Itanium C++ ABI 2.6.4:
1829       //   Some of the base class subobjects may not need construction virtual
1830       //   tables, which will therefore not be present in the construction
1831       //   virtual table group, even though the subobject virtual tables are
1832       //   present in the main virtual table group for the complete object.
1833       if (!BaseIsMorallyVirtual && !BaseDecl->getNumVBases())
1834         continue;
1835     }
1836 
1837     // Get the base offset of this base.
1838     uint64_t RelativeBaseOffset = Layout.getBaseClassOffset(BaseDecl);
1839     uint64_t BaseOffset = Base.getBaseOffset() + RelativeBaseOffset;
1840 
1841     uint64_t BaseOffsetInLayoutClass = OffsetInLayoutClass + RelativeBaseOffset;
1842 
1843     // Don't emit a secondary vtable for a primary base. We might however want
1844     // to emit secondary vtables for other bases of this base.
1845     if (BaseDecl == PrimaryBase) {
1846       LayoutSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset),
1847                              BaseIsMorallyVirtual, BaseOffsetInLayoutClass);
1848       continue;
1849     }
1850 
1851     // Layout the primary vtable (and any secondary vtables) for this base.
1852     LayoutPrimaryAndSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset),
1853                                      BaseIsMorallyVirtual,
1854                                      /*BaseIsVirtualInLayoutClass=*/false,
1855                                      BaseOffsetInLayoutClass);
1856   }
1857 }
1858 
1859 void
1860 VTableBuilder::DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
1861                                             uint64_t OffsetInLayoutClass,
1862                                             VisitedVirtualBasesSetTy &VBases) {
1863   const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1864 
1865   // Check if this base has a primary base.
1866   if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
1867 
1868     // Check if it's virtual.
1869     if (Layout.getPrimaryBaseWasVirtual()) {
1870       bool IsPrimaryVirtualBase = true;
1871 
1872       if (isBuildingConstructorVTable()) {
1873         // Check if the base is actually a primary base in the class we use for
1874         // layout.
1875         const ASTRecordLayout &LayoutClassLayout =
1876           Context.getASTRecordLayout(LayoutClass);
1877 
1878         uint64_t PrimaryBaseOffsetInLayoutClass =
1879           LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
1880 
1881         // We know that the base is not a primary base in the layout class if
1882         // the base offsets are different.
1883         if (PrimaryBaseOffsetInLayoutClass != OffsetInLayoutClass)
1884           IsPrimaryVirtualBase = false;
1885       }
1886 
1887       if (IsPrimaryVirtualBase)
1888         PrimaryVirtualBases.insert(PrimaryBase);
1889     }
1890   }
1891 
1892   // Traverse bases, looking for more primary virtual bases.
1893   for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1894        E = RD->bases_end(); I != E; ++I) {
1895     const CXXRecordDecl *BaseDecl =
1896       cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1897 
1898     uint64_t BaseOffsetInLayoutClass;
1899 
1900     if (I->isVirtual()) {
1901       if (!VBases.insert(BaseDecl))
1902         continue;
1903 
1904       const ASTRecordLayout &LayoutClassLayout =
1905         Context.getASTRecordLayout(LayoutClass);
1906 
1907       BaseOffsetInLayoutClass = LayoutClassLayout.getVBaseClassOffset(BaseDecl);
1908     } else {
1909       BaseOffsetInLayoutClass =
1910         OffsetInLayoutClass + Layout.getBaseClassOffset(BaseDecl);
1911     }
1912 
1913     DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases);
1914   }
1915 }
1916 
1917 void
1918 VTableBuilder::LayoutVTablesForVirtualBases(const CXXRecordDecl *RD,
1919                                             VisitedVirtualBasesSetTy &VBases) {
1920   // Itanium C++ ABI 2.5.2:
1921   //   Then come the virtual base virtual tables, also in inheritance graph
1922   //   order, and again excluding primary bases (which share virtual tables with
1923   //   the classes for which they are primary).
1924   for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1925        E = RD->bases_end(); I != E; ++I) {
1926     const CXXRecordDecl *BaseDecl =
1927       cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1928 
1929     // Check if this base needs a vtable. (If it's virtual, not a primary base
1930     // of some other class, and we haven't visited it before).
1931     if (I->isVirtual() && BaseDecl->isDynamicClass() &&
1932         !PrimaryVirtualBases.count(BaseDecl) && VBases.insert(BaseDecl)) {
1933       const ASTRecordLayout &MostDerivedClassLayout =
1934         Context.getASTRecordLayout(MostDerivedClass);
1935       uint64_t BaseOffset =
1936         MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
1937 
1938       const ASTRecordLayout &LayoutClassLayout =
1939         Context.getASTRecordLayout(LayoutClass);
1940       uint64_t BaseOffsetInLayoutClass =
1941         LayoutClassLayout.getVBaseClassOffset(BaseDecl);
1942 
1943       LayoutPrimaryAndSecondaryVTables(BaseSubobject(BaseDecl, BaseOffset),
1944                                        /*BaseIsMorallyVirtual=*/true,
1945                                        /*BaseIsVirtualInLayoutClass=*/true,
1946                                        BaseOffsetInLayoutClass);
1947     }
1948 
1949     // We only need to check the base for virtual base vtables if it actually
1950     // has virtual bases.
1951     if (BaseDecl->getNumVBases())
1952       LayoutVTablesForVirtualBases(BaseDecl, VBases);
1953   }
1954 }
1955 
1956 /// dumpLayout - Dump the vtable layout.
1957 void VTableBuilder::dumpLayout(llvm::raw_ostream& Out) {
1958 
1959   if (isBuildingConstructorVTable()) {
1960     Out << "Construction vtable for ('";
1961     Out << MostDerivedClass->getQualifiedNameAsString() << "', ";
1962     // FIXME: Don't use / 8 .
1963     Out << MostDerivedClassOffset / 8 << ") in '";
1964     Out << LayoutClass->getQualifiedNameAsString();
1965   } else {
1966     Out << "Vtable for '";
1967     Out << MostDerivedClass->getQualifiedNameAsString();
1968   }
1969   Out << "' (" << Components.size() << " entries).\n";
1970 
1971   // Iterate through the address points and insert them into a new map where
1972   // they are keyed by the index and not the base object.
1973   // Since an address point can be shared by multiple subobjects, we use an
1974   // STL multimap.
1975   std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex;
1976   for (AddressPointsMapTy::const_iterator I = AddressPoints.begin(),
1977        E = AddressPoints.end(); I != E; ++I) {
1978     const BaseSubobject& Base = I->first;
1979     uint64_t Index = I->second;
1980 
1981     AddressPointsByIndex.insert(std::make_pair(Index, Base));
1982   }
1983 
1984   for (unsigned I = 0, E = Components.size(); I != E; ++I) {
1985     uint64_t Index = I;
1986 
1987     Out << llvm::format("%4d | ", I);
1988 
1989     const VTableComponent &Component = Components[I];
1990 
1991     // Dump the component.
1992     switch (Component.getKind()) {
1993 
1994     case VTableComponent::CK_VCallOffset:
1995       Out << "vcall_offset (" << Component.getVCallOffset() << ")";
1996       break;
1997 
1998     case VTableComponent::CK_VBaseOffset:
1999       Out << "vbase_offset (" << Component.getVBaseOffset() << ")";
2000       break;
2001 
2002     case VTableComponent::CK_OffsetToTop:
2003       Out << "offset_to_top (" << Component.getOffsetToTop() << ")";
2004       break;
2005 
2006     case VTableComponent::CK_RTTI:
2007       Out << Component.getRTTIDecl()->getQualifiedNameAsString() << " RTTI";
2008       break;
2009 
2010     case VTableComponent::CK_FunctionPointer: {
2011       const CXXMethodDecl *MD = Component.getFunctionDecl();
2012 
2013       std::string Str =
2014         PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2015                                     MD);
2016       Out << Str;
2017       if (MD->isPure())
2018         Out << " [pure]";
2019 
2020       ThunkInfo Thunk = VTableThunks.lookup(I);
2021       if (!Thunk.isEmpty()) {
2022         // If this function pointer has a return adjustment, dump it.
2023         if (!Thunk.Return.isEmpty()) {
2024           Out << "\n       [return adjustment: ";
2025           Out << Thunk.Return.NonVirtual << " non-virtual";
2026 
2027           if (Thunk.Return.VBaseOffsetOffset) {
2028             Out << ", " << Thunk.Return.VBaseOffsetOffset;
2029             Out << " vbase offset offset";
2030           }
2031 
2032           Out << ']';
2033         }
2034 
2035         // If this function pointer has a 'this' pointer adjustment, dump it.
2036         if (!Thunk.This.isEmpty()) {
2037           Out << "\n       [this adjustment: ";
2038           Out << Thunk.This.NonVirtual << " non-virtual";
2039 
2040           if (Thunk.This.VCallOffsetOffset) {
2041             Out << ", " << Thunk.This.VCallOffsetOffset;
2042             Out << " vcall offset offset";
2043           }
2044 
2045           Out << ']';
2046         }
2047       }
2048 
2049       break;
2050     }
2051 
2052     case VTableComponent::CK_CompleteDtorPointer:
2053     case VTableComponent::CK_DeletingDtorPointer: {
2054       bool IsComplete =
2055         Component.getKind() == VTableComponent::CK_CompleteDtorPointer;
2056 
2057       const CXXDestructorDecl *DD = Component.getDestructorDecl();
2058 
2059       Out << DD->getQualifiedNameAsString();
2060       if (IsComplete)
2061         Out << "() [complete]";
2062       else
2063         Out << "() [deleting]";
2064 
2065       if (DD->isPure())
2066         Out << " [pure]";
2067 
2068       ThunkInfo Thunk = VTableThunks.lookup(I);
2069       if (!Thunk.isEmpty()) {
2070         // If this destructor has a 'this' pointer adjustment, dump it.
2071         if (!Thunk.This.isEmpty()) {
2072           Out << "\n       [this adjustment: ";
2073           Out << Thunk.This.NonVirtual << " non-virtual";
2074 
2075           if (Thunk.This.VCallOffsetOffset) {
2076             Out << ", " << Thunk.This.VCallOffsetOffset;
2077             Out << " vcall offset offset";
2078           }
2079 
2080           Out << ']';
2081         }
2082       }
2083 
2084       break;
2085     }
2086 
2087     case VTableComponent::CK_UnusedFunctionPointer: {
2088       const CXXMethodDecl *MD = Component.getUnusedFunctionDecl();
2089 
2090       std::string Str =
2091         PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2092                                     MD);
2093       Out << "[unused] " << Str;
2094       if (MD->isPure())
2095         Out << " [pure]";
2096     }
2097 
2098     }
2099 
2100     Out << '\n';
2101 
2102     // Dump the next address point.
2103     uint64_t NextIndex = Index + 1;
2104     if (AddressPointsByIndex.count(NextIndex)) {
2105       if (AddressPointsByIndex.count(NextIndex) == 1) {
2106         const BaseSubobject &Base =
2107           AddressPointsByIndex.find(NextIndex)->second;
2108 
2109         // FIXME: Instead of dividing by 8, we should be using CharUnits.
2110         Out << "       -- (" << Base.getBase()->getQualifiedNameAsString();
2111         Out << ", " << Base.getBaseOffset() / 8 << ") vtable address --\n";
2112       } else {
2113         uint64_t BaseOffset =
2114           AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset();
2115 
2116         // We store the class names in a set to get a stable order.
2117         std::set<std::string> ClassNames;
2118         for (std::multimap<uint64_t, BaseSubobject>::const_iterator I =
2119              AddressPointsByIndex.lower_bound(NextIndex), E =
2120              AddressPointsByIndex.upper_bound(NextIndex); I != E; ++I) {
2121           assert(I->second.getBaseOffset() == BaseOffset &&
2122                  "Invalid base offset!");
2123           const CXXRecordDecl *RD = I->second.getBase();
2124           ClassNames.insert(RD->getQualifiedNameAsString());
2125         }
2126 
2127         for (std::set<std::string>::const_iterator I = ClassNames.begin(),
2128              E = ClassNames.end(); I != E; ++I) {
2129           // FIXME: Instead of dividing by 8, we should be using CharUnits.
2130           Out << "       -- (" << *I;
2131           Out << ", " << BaseOffset / 8 << ") vtable address --\n";
2132         }
2133       }
2134     }
2135   }
2136 
2137   Out << '\n';
2138 
2139   if (isBuildingConstructorVTable())
2140     return;
2141 
2142   if (MostDerivedClass->getNumVBases()) {
2143     // We store the virtual base class names and their offsets in a map to get
2144     // a stable order.
2145 
2146     std::map<std::string, int64_t> ClassNamesAndOffsets;
2147     for (VBaseOffsetOffsetsMapTy::const_iterator I = VBaseOffsetOffsets.begin(),
2148          E = VBaseOffsetOffsets.end(); I != E; ++I) {
2149       std::string ClassName = I->first->getQualifiedNameAsString();
2150       int64_t OffsetOffset = I->second;
2151       ClassNamesAndOffsets.insert(std::make_pair(ClassName, OffsetOffset));
2152     }
2153 
2154     Out << "Virtual base offset offsets for '";
2155     Out << MostDerivedClass->getQualifiedNameAsString() << "' (";
2156     Out << ClassNamesAndOffsets.size();
2157     Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n";
2158 
2159     for (std::map<std::string, int64_t>::const_iterator I =
2160          ClassNamesAndOffsets.begin(), E = ClassNamesAndOffsets.end();
2161          I != E; ++I)
2162       Out << "   " << I->first << " | " << I->second << '\n';
2163 
2164     Out << "\n";
2165   }
2166 
2167   if (!Thunks.empty()) {
2168     // We store the method names in a map to get a stable order.
2169     std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls;
2170 
2171     for (ThunksMapTy::const_iterator I = Thunks.begin(), E = Thunks.end();
2172          I != E; ++I) {
2173       const CXXMethodDecl *MD = I->first;
2174       std::string MethodName =
2175         PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2176                                     MD);
2177 
2178       MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
2179     }
2180 
2181     for (std::map<std::string, const CXXMethodDecl *>::const_iterator I =
2182          MethodNamesAndDecls.begin(), E = MethodNamesAndDecls.end();
2183          I != E; ++I) {
2184       const std::string &MethodName = I->first;
2185       const CXXMethodDecl *MD = I->second;
2186 
2187       ThunkInfoVectorTy ThunksVector = Thunks[MD];
2188       std::sort(ThunksVector.begin(), ThunksVector.end());
2189 
2190       Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
2191       Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n";
2192 
2193       for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) {
2194         const ThunkInfo &Thunk = ThunksVector[I];
2195 
2196         Out << llvm::format("%4d | ", I);
2197 
2198         // If this function pointer has a return pointer adjustment, dump it.
2199         if (!Thunk.Return.isEmpty()) {
2200           Out << "return adjustment: " << Thunk.This.NonVirtual;
2201           Out << " non-virtual";
2202           if (Thunk.Return.VBaseOffsetOffset) {
2203             Out << ", " << Thunk.Return.VBaseOffsetOffset;
2204             Out << " vbase offset offset";
2205           }
2206 
2207           if (!Thunk.This.isEmpty())
2208             Out << "\n       ";
2209         }
2210 
2211         // If this function pointer has a 'this' pointer adjustment, dump it.
2212         if (!Thunk.This.isEmpty()) {
2213           Out << "this adjustment: ";
2214           Out << Thunk.This.NonVirtual << " non-virtual";
2215 
2216           if (Thunk.This.VCallOffsetOffset) {
2217             Out << ", " << Thunk.This.VCallOffsetOffset;
2218             Out << " vcall offset offset";
2219           }
2220         }
2221 
2222         Out << '\n';
2223       }
2224 
2225       Out << '\n';
2226 
2227     }
2228   }
2229 }
2230 
2231 }
2232 
2233 void CodeGenVTables::ComputeMethodVTableIndices(const CXXRecordDecl *RD) {
2234 
2235   // Itanium C++ ABI 2.5.2:
2236   //   The order of the virtual function pointers in a virtual table is the
2237   //   order of declaration of the corresponding member functions in the class.
2238   //
2239   //   There is an entry for any virtual function declared in a class,
2240   //   whether it is a new function or overrides a base class function,
2241   //   unless it overrides a function from the primary base, and conversion
2242   //   between their return types does not require an adjustment.
2243 
2244   int64_t CurrentIndex = 0;
2245 
2246   const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
2247   const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
2248 
2249   if (PrimaryBase) {
2250     assert(PrimaryBase->isDefinition() &&
2251            "Should have the definition decl of the primary base!");
2252 
2253     // Since the record decl shares its vtable pointer with the primary base
2254     // we need to start counting at the end of the primary base's vtable.
2255     CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase);
2256   }
2257 
2258   // Collect all the primary bases, so we can check whether methods override
2259   // a method from the base.
2260   VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
2261   for (ASTRecordLayout::primary_base_info_iterator
2262        I = Layout.primary_base_begin(), E = Layout.primary_base_end();
2263        I != E; ++I)
2264     PrimaryBases.insert((*I).getBase());
2265 
2266   const CXXDestructorDecl *ImplicitVirtualDtor = 0;
2267 
2268   for (CXXRecordDecl::method_iterator i = RD->method_begin(),
2269        e = RD->method_end(); i != e; ++i) {
2270     const CXXMethodDecl *MD = *i;
2271 
2272     // We only want virtual methods.
2273     if (!MD->isVirtual())
2274       continue;
2275 
2276     // Check if this method overrides a method in the primary base.
2277     if (const CXXMethodDecl *OverriddenMD =
2278           FindNearestOverriddenMethod(MD, PrimaryBases)) {
2279       // Check if converting from the return type of the method to the
2280       // return type of the overridden method requires conversion.
2281       if (ComputeReturnAdjustmentBaseOffset(CGM.getContext(), MD,
2282                                             OverriddenMD).isEmpty()) {
2283         // This index is shared between the index in the vtable of the primary
2284         // base class.
2285         if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2286           const CXXDestructorDecl *OverriddenDD =
2287             cast<CXXDestructorDecl>(OverriddenMD);
2288 
2289           // Add both the complete and deleting entries.
2290           MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] =
2291             getMethodVTableIndex(GlobalDecl(OverriddenDD, Dtor_Complete));
2292           MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] =
2293             getMethodVTableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting));
2294         } else {
2295           MethodVTableIndices[MD] = getMethodVTableIndex(OverriddenMD);
2296         }
2297 
2298         // We don't need to add an entry for this method.
2299         continue;
2300       }
2301     }
2302 
2303     if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2304       if (MD->isImplicit()) {
2305         assert(!ImplicitVirtualDtor &&
2306                "Did already see an implicit virtual dtor!");
2307         ImplicitVirtualDtor = DD;
2308         continue;
2309       }
2310 
2311       // Add the complete dtor.
2312       MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++;
2313 
2314       // Add the deleting dtor.
2315       MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++;
2316     } else {
2317       // Add the entry.
2318       MethodVTableIndices[MD] = CurrentIndex++;
2319     }
2320   }
2321 
2322   if (ImplicitVirtualDtor) {
2323     // Itanium C++ ABI 2.5.2:
2324     //   If a class has an implicitly-defined virtual destructor,
2325     //   its entries come after the declared virtual function pointers.
2326 
2327     // Add the complete dtor.
2328     MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] =
2329       CurrentIndex++;
2330 
2331     // Add the deleting dtor.
2332     MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] =
2333       CurrentIndex++;
2334   }
2335 
2336   NumVirtualFunctionPointers[RD] = CurrentIndex;
2337 }
2338 
2339 bool CodeGenVTables::ShouldEmitVTableInThisTU(const CXXRecordDecl *RD) {
2340   assert(RD->isDynamicClass() && "Non dynamic classes have no VTable.");
2341 
2342   TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
2343   if (TSK == TSK_ExplicitInstantiationDeclaration)
2344     return false;
2345 
2346   const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD);
2347   if (!KeyFunction)
2348     return true;
2349 
2350   // Itanium C++ ABI, 5.2.6 Instantiated Templates:
2351   //    An instantiation of a class template requires:
2352   //        - In the object where instantiated, the virtual table...
2353   if (TSK == TSK_ImplicitInstantiation ||
2354       TSK == TSK_ExplicitInstantiationDefinition)
2355     return true;
2356 
2357   return KeyFunction->hasBody();
2358 }
2359 
2360 uint64_t CodeGenVTables::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) {
2361   llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
2362     NumVirtualFunctionPointers.find(RD);
2363   if (I != NumVirtualFunctionPointers.end())
2364     return I->second;
2365 
2366   ComputeMethodVTableIndices(RD);
2367 
2368   I = NumVirtualFunctionPointers.find(RD);
2369   assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!");
2370   return I->second;
2371 }
2372 
2373 uint64_t CodeGenVTables::getMethodVTableIndex(GlobalDecl GD) {
2374   MethodVTableIndicesTy::iterator I = MethodVTableIndices.find(GD);
2375   if (I != MethodVTableIndices.end())
2376     return I->second;
2377 
2378   const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
2379 
2380   ComputeMethodVTableIndices(RD);
2381 
2382   I = MethodVTableIndices.find(GD);
2383   assert(I != MethodVTableIndices.end() && "Did not find index!");
2384   return I->second;
2385 }
2386 
2387 int64_t CodeGenVTables::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD,
2388                                                    const CXXRecordDecl *VBase) {
2389   ClassPairTy ClassPair(RD, VBase);
2390 
2391   VirtualBaseClassOffsetOffsetsMapTy::iterator I =
2392     VirtualBaseClassOffsetOffsets.find(ClassPair);
2393   if (I != VirtualBaseClassOffsetOffsets.end())
2394     return I->second;
2395 
2396   VCallAndVBaseOffsetBuilder Builder(RD, RD, /*FinalOverriders=*/0,
2397                                      BaseSubobject(RD, 0),
2398                                      /*BaseIsVirtual=*/false,
2399                                      /*OffsetInLayoutClass=*/0);
2400 
2401   for (VCallAndVBaseOffsetBuilder::VBaseOffsetOffsetsMapTy::const_iterator I =
2402        Builder.getVBaseOffsetOffsets().begin(),
2403        E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) {
2404     // Insert all types.
2405     ClassPairTy ClassPair(RD, I->first);
2406 
2407     VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second));
2408   }
2409 
2410   I = VirtualBaseClassOffsetOffsets.find(ClassPair);
2411   assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!");
2412 
2413   return I->second;
2414 }
2415 
2416 uint64_t
2417 CodeGenVTables::getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD) {
2418   assert(AddressPoints.count(std::make_pair(RD, Base)) &&
2419          "Did not find address point!");
2420 
2421   uint64_t AddressPoint = AddressPoints.lookup(std::make_pair(RD, Base));
2422   assert(AddressPoint && "Address point must not be zero!");
2423 
2424   return AddressPoint;
2425 }
2426 
2427 llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
2428                                               const ThunkInfo &Thunk) {
2429   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2430 
2431   // Compute the mangled name.
2432   llvm::SmallString<256> Name;
2433   if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
2434     getCXXABI().getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(),
2435                                                       Thunk.This, Name);
2436   else
2437     getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Name);
2438 
2439   const llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD);
2440   return GetOrCreateLLVMFunction(Name, Ty, GD);
2441 }
2442 
2443 static llvm::Value *PerformTypeAdjustment(CodeGenFunction &CGF,
2444                                           llvm::Value *Ptr,
2445                                           int64_t NonVirtualAdjustment,
2446                                           int64_t VirtualAdjustment) {
2447   if (!NonVirtualAdjustment && !VirtualAdjustment)
2448     return Ptr;
2449 
2450   const llvm::Type *Int8PtrTy =
2451     llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2452 
2453   llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy);
2454 
2455   if (NonVirtualAdjustment) {
2456     // Do the non-virtual adjustment.
2457     V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
2458   }
2459 
2460   if (VirtualAdjustment) {
2461     const llvm::Type *PtrDiffTy =
2462       CGF.ConvertType(CGF.getContext().getPointerDiffType());
2463 
2464     // Do the virtual adjustment.
2465     llvm::Value *VTablePtrPtr =
2466       CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo());
2467 
2468     llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
2469 
2470     llvm::Value *OffsetPtr =
2471       CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
2472 
2473     OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
2474 
2475     // Load the adjustment offset from the vtable.
2476     llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr);
2477 
2478     // Adjust our pointer.
2479     V = CGF.Builder.CreateInBoundsGEP(V, Offset);
2480   }
2481 
2482   // Cast back to the original type.
2483   return CGF.Builder.CreateBitCast(V, Ptr->getType());
2484 }
2485 
2486 static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD,
2487                                const ThunkInfo &Thunk, llvm::Function *Fn) {
2488   CGM.setGlobalVisibility(Fn, MD);
2489 
2490   if (!CGM.getCodeGenOpts().HiddenWeakVTables)
2491     return;
2492 
2493   // If the thunk has weak/linkonce linkage, but the function must be
2494   // emitted in every translation unit that references it, then we can
2495   // emit its thunks with hidden visibility, since its thunks must be
2496   // emitted when the function is.
2497 
2498   // This follows CodeGenModule::setTypeVisibility; see the comments
2499   // there for explanation.
2500 
2501   if ((Fn->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage &&
2502        Fn->getLinkage() != llvm::GlobalVariable::WeakODRLinkage) ||
2503       Fn->getVisibility() != llvm::GlobalVariable::DefaultVisibility)
2504     return;
2505 
2506   if (MD->hasAttr<VisibilityAttr>())
2507     return;
2508 
2509   switch (MD->getTemplateSpecializationKind()) {
2510   case TSK_ExplicitInstantiationDefinition:
2511   case TSK_ExplicitInstantiationDeclaration:
2512     return;
2513 
2514   case TSK_Undeclared:
2515     break;
2516 
2517   case TSK_ExplicitSpecialization:
2518   case TSK_ImplicitInstantiation:
2519     if (!CGM.getCodeGenOpts().HiddenWeakTemplateVTables)
2520       return;
2521     break;
2522   }
2523 
2524   // If there's an explicit definition, and that definition is
2525   // out-of-line, then we can't assume that all users will have a
2526   // definition to emit.
2527   const FunctionDecl *Def = 0;
2528   if (MD->hasBody(Def) && Def->isOutOfLine())
2529     return;
2530 
2531   Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
2532 }
2533 
2534 void CodeGenFunction::GenerateThunk(llvm::Function *Fn, GlobalDecl GD,
2535                                     const ThunkInfo &Thunk) {
2536   const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
2537   const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
2538   QualType ResultType = FPT->getResultType();
2539   QualType ThisType = MD->getThisType(getContext());
2540 
2541   FunctionArgList FunctionArgs;
2542 
2543   // FIXME: It would be nice if more of this code could be shared with
2544   // CodeGenFunction::GenerateCode.
2545 
2546   // Create the implicit 'this' parameter declaration.
2547   CurGD = GD;
2548   CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResultType, FunctionArgs);
2549 
2550   // Add the rest of the parameters.
2551   for (FunctionDecl::param_const_iterator I = MD->param_begin(),
2552        E = MD->param_end(); I != E; ++I) {
2553     ParmVarDecl *Param = *I;
2554 
2555     FunctionArgs.push_back(std::make_pair(Param, Param->getType()));
2556   }
2557 
2558   StartFunction(GlobalDecl(), ResultType, Fn, FunctionArgs, SourceLocation());
2559 
2560   CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
2561 
2562   // Adjust the 'this' pointer if necessary.
2563   llvm::Value *AdjustedThisPtr =
2564     PerformTypeAdjustment(*this, LoadCXXThis(),
2565                           Thunk.This.NonVirtual,
2566                           Thunk.This.VCallOffsetOffset);
2567 
2568   CallArgList CallArgs;
2569 
2570   // Add our adjusted 'this' pointer.
2571   CallArgs.push_back(std::make_pair(RValue::get(AdjustedThisPtr), ThisType));
2572 
2573   // Add the rest of the parameters.
2574   for (FunctionDecl::param_const_iterator I = MD->param_begin(),
2575        E = MD->param_end(); I != E; ++I) {
2576     ParmVarDecl *Param = *I;
2577     QualType ArgType = Param->getType();
2578     RValue Arg = EmitDelegateCallArg(Param);
2579 
2580     CallArgs.push_back(std::make_pair(Arg, ArgType));
2581   }
2582 
2583   // Get our callee.
2584   const llvm::Type *Ty =
2585     CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(GD),
2586                                    FPT->isVariadic());
2587   llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty);
2588 
2589   const CGFunctionInfo &FnInfo =
2590     CGM.getTypes().getFunctionInfo(ResultType, CallArgs,
2591                                    FPT->getExtInfo());
2592 
2593   // Determine whether we have a return value slot to use.
2594   ReturnValueSlot Slot;
2595   if (!ResultType->isVoidType() &&
2596       FnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect &&
2597       hasAggregateLLVMType(CurFnInfo->getReturnType()))
2598     Slot = ReturnValueSlot(ReturnValue, ResultType.isVolatileQualified());
2599 
2600   // Now emit our call.
2601   RValue RV = EmitCall(FnInfo, Callee, Slot, CallArgs, MD);
2602 
2603   if (!Thunk.Return.isEmpty()) {
2604     // Emit the return adjustment.
2605     bool NullCheckValue = !ResultType->isReferenceType();
2606 
2607     llvm::BasicBlock *AdjustNull = 0;
2608     llvm::BasicBlock *AdjustNotNull = 0;
2609     llvm::BasicBlock *AdjustEnd = 0;
2610 
2611     llvm::Value *ReturnValue = RV.getScalarVal();
2612 
2613     if (NullCheckValue) {
2614       AdjustNull = createBasicBlock("adjust.null");
2615       AdjustNotNull = createBasicBlock("adjust.notnull");
2616       AdjustEnd = createBasicBlock("adjust.end");
2617 
2618       llvm::Value *IsNull = Builder.CreateIsNull(ReturnValue);
2619       Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
2620       EmitBlock(AdjustNotNull);
2621     }
2622 
2623     ReturnValue = PerformTypeAdjustment(*this, ReturnValue,
2624                                         Thunk.Return.NonVirtual,
2625                                         Thunk.Return.VBaseOffsetOffset);
2626 
2627     if (NullCheckValue) {
2628       Builder.CreateBr(AdjustEnd);
2629       EmitBlock(AdjustNull);
2630       Builder.CreateBr(AdjustEnd);
2631       EmitBlock(AdjustEnd);
2632 
2633       llvm::PHINode *PHI = Builder.CreatePHI(ReturnValue->getType());
2634       PHI->reserveOperandSpace(2);
2635       PHI->addIncoming(ReturnValue, AdjustNotNull);
2636       PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
2637                        AdjustNull);
2638       ReturnValue = PHI;
2639     }
2640 
2641     RV = RValue::get(ReturnValue);
2642   }
2643 
2644   if (!ResultType->isVoidType() && Slot.isNull())
2645     CGM.getCXXABI().EmitReturnFromThunk(CGF, RV, ResultType);
2646 
2647   FinishFunction();
2648 
2649   // Set the right linkage.
2650   CGM.setFunctionLinkage(MD, Fn);
2651 
2652   // Set the right visibility.
2653   setThunkVisibility(CGM, MD, Thunk, Fn);
2654 }
2655 
2656 void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk)
2657 {
2658   llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk);
2659 
2660   // Strip off a bitcast if we got one back.
2661   if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
2662     assert(CE->getOpcode() == llvm::Instruction::BitCast);
2663     Entry = CE->getOperand(0);
2664   }
2665 
2666   // There's already a declaration with the same name, check if it has the same
2667   // type or if we need to replace it.
2668   if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() !=
2669       CGM.getTypes().GetFunctionTypeForVTable(GD)) {
2670     llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry);
2671 
2672     // If the types mismatch then we have to rewrite the definition.
2673     assert(OldThunkFn->isDeclaration() &&
2674            "Shouldn't replace non-declaration");
2675 
2676     // Remove the name from the old thunk function and get a new thunk.
2677     OldThunkFn->setName(llvm::StringRef());
2678     Entry = CGM.GetAddrOfThunk(GD, Thunk);
2679 
2680     // If needed, replace the old thunk with a bitcast.
2681     if (!OldThunkFn->use_empty()) {
2682       llvm::Constant *NewPtrForOldDecl =
2683         llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
2684       OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
2685     }
2686 
2687     // Remove the old thunk.
2688     OldThunkFn->eraseFromParent();
2689   }
2690 
2691   // Actually generate the thunk body.
2692   llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
2693   CodeGenFunction(CGM).GenerateThunk(ThunkFn, GD, Thunk);
2694 }
2695 
2696 void CodeGenVTables::EmitThunks(GlobalDecl GD)
2697 {
2698   const CXXMethodDecl *MD =
2699     cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
2700 
2701   // We don't need to generate thunks for the base destructor.
2702   if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
2703     return;
2704 
2705   const CXXRecordDecl *RD = MD->getParent();
2706 
2707   // Compute VTable related info for this class.
2708   ComputeVTableRelatedInformation(RD, false);
2709 
2710   ThunksMapTy::const_iterator I = Thunks.find(MD);
2711   if (I == Thunks.end()) {
2712     // We did not find a thunk for this method.
2713     return;
2714   }
2715 
2716   const ThunkInfoVectorTy &ThunkInfoVector = I->second;
2717   for (unsigned I = 0, E = ThunkInfoVector.size(); I != E; ++I)
2718     EmitThunk(GD, ThunkInfoVector[I]);
2719 }
2720 
2721 void CodeGenVTables::ComputeVTableRelatedInformation(const CXXRecordDecl *RD,
2722                                                      bool RequireVTable) {
2723   VTableLayoutData &Entry = VTableLayoutMap[RD];
2724 
2725   // We may need to generate a definition for this vtable.
2726   if (RequireVTable && !Entry.getInt()) {
2727     if (ShouldEmitVTableInThisTU(RD))
2728       CGM.DeferredVTables.push_back(RD);
2729 
2730     Entry.setInt(true);
2731   }
2732 
2733   // Check if we've computed this information before.
2734   if (Entry.getPointer())
2735     return;
2736 
2737   VTableBuilder Builder(*this, RD, 0, /*MostDerivedClassIsVirtual=*/0, RD);
2738 
2739   // Add the VTable layout.
2740   uint64_t NumVTableComponents = Builder.getNumVTableComponents();
2741   uint64_t *LayoutData = new uint64_t[NumVTableComponents + 1];
2742   Entry.setPointer(LayoutData);
2743 
2744   // Store the number of components.
2745   LayoutData[0] = NumVTableComponents;
2746 
2747   // Store the components.
2748   std::copy(Builder.vtable_components_data_begin(),
2749             Builder.vtable_components_data_end(),
2750             &LayoutData[1]);
2751 
2752   // Add the known thunks.
2753   Thunks.insert(Builder.thunks_begin(), Builder.thunks_end());
2754 
2755   // Add the thunks needed in this vtable.
2756   assert(!VTableThunksMap.count(RD) &&
2757          "Thunks already exists for this vtable!");
2758 
2759   VTableThunksTy &VTableThunks = VTableThunksMap[RD];
2760   VTableThunks.append(Builder.vtable_thunks_begin(),
2761                       Builder.vtable_thunks_end());
2762 
2763   // Sort them.
2764   std::sort(VTableThunks.begin(), VTableThunks.end());
2765 
2766   // Add the address points.
2767   for (VTableBuilder::AddressPointsMapTy::const_iterator I =
2768        Builder.address_points_begin(), E = Builder.address_points_end();
2769        I != E; ++I) {
2770 
2771     uint64_t &AddressPoint = AddressPoints[std::make_pair(RD, I->first)];
2772 
2773     // Check if we already have the address points for this base.
2774     assert(!AddressPoint && "Address point already exists for this base!");
2775 
2776     AddressPoint = I->second;
2777   }
2778 
2779   // If we don't have the vbase information for this class, insert it.
2780   // getVirtualBaseOffsetOffset will compute it separately without computing
2781   // the rest of the vtable related information.
2782   if (!RD->getNumVBases())
2783     return;
2784 
2785   const RecordType *VBaseRT =
2786     RD->vbases_begin()->getType()->getAs<RecordType>();
2787   const CXXRecordDecl *VBase = cast<CXXRecordDecl>(VBaseRT->getDecl());
2788 
2789   if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase)))
2790     return;
2791 
2792   for (VTableBuilder::VBaseOffsetOffsetsMapTy::const_iterator I =
2793        Builder.getVBaseOffsetOffsets().begin(),
2794        E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) {
2795     // Insert all types.
2796     ClassPairTy ClassPair(RD, I->first);
2797 
2798     VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second));
2799   }
2800 }
2801 
2802 llvm::Constant *
2803 CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD,
2804                                         const uint64_t *Components,
2805                                         unsigned NumComponents,
2806                                         const VTableThunksTy &VTableThunks) {
2807   llvm::SmallVector<llvm::Constant *, 64> Inits;
2808 
2809   const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
2810 
2811   const llvm::Type *PtrDiffTy =
2812     CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
2813 
2814   QualType ClassType = CGM.getContext().getTagDeclType(RD);
2815   llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType);
2816 
2817   unsigned NextVTableThunkIndex = 0;
2818 
2819   llvm::Constant* PureVirtualFn = 0;
2820 
2821   for (unsigned I = 0; I != NumComponents; ++I) {
2822     VTableComponent Component =
2823       VTableComponent::getFromOpaqueInteger(Components[I]);
2824 
2825     llvm::Constant *Init = 0;
2826 
2827     switch (Component.getKind()) {
2828     case VTableComponent::CK_VCallOffset:
2829       Init = llvm::ConstantInt::get(PtrDiffTy, Component.getVCallOffset());
2830       Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
2831       break;
2832     case VTableComponent::CK_VBaseOffset:
2833       Init = llvm::ConstantInt::get(PtrDiffTy, Component.getVBaseOffset());
2834       Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
2835       break;
2836     case VTableComponent::CK_OffsetToTop:
2837       Init = llvm::ConstantInt::get(PtrDiffTy, Component.getOffsetToTop());
2838       Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
2839       break;
2840     case VTableComponent::CK_RTTI:
2841       Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
2842       break;
2843     case VTableComponent::CK_FunctionPointer:
2844     case VTableComponent::CK_CompleteDtorPointer:
2845     case VTableComponent::CK_DeletingDtorPointer: {
2846       GlobalDecl GD;
2847 
2848       // Get the right global decl.
2849       switch (Component.getKind()) {
2850       default:
2851         llvm_unreachable("Unexpected vtable component kind");
2852       case VTableComponent::CK_FunctionPointer:
2853         GD = Component.getFunctionDecl();
2854         break;
2855       case VTableComponent::CK_CompleteDtorPointer:
2856         GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
2857         break;
2858       case VTableComponent::CK_DeletingDtorPointer:
2859         GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
2860         break;
2861       }
2862 
2863       if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
2864         // We have a pure virtual member function.
2865         if (!PureVirtualFn) {
2866           const llvm::FunctionType *Ty =
2867             llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()),
2868                                     /*isVarArg=*/false);
2869           PureVirtualFn =
2870             CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual");
2871           PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
2872                                                          Int8PtrTy);
2873         }
2874 
2875         Init = PureVirtualFn;
2876       } else {
2877         // Check if we should use a thunk.
2878         if (NextVTableThunkIndex < VTableThunks.size() &&
2879             VTableThunks[NextVTableThunkIndex].first == I) {
2880           const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
2881 
2882           Init = CGM.GetAddrOfThunk(GD, Thunk);
2883 
2884           NextVTableThunkIndex++;
2885         } else {
2886           const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD);
2887 
2888           Init = CGM.GetAddrOfFunction(GD, Ty);
2889         }
2890 
2891         Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
2892       }
2893       break;
2894     }
2895 
2896     case VTableComponent::CK_UnusedFunctionPointer:
2897       Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
2898       break;
2899     };
2900 
2901     Inits.push_back(Init);
2902   }
2903 
2904   llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
2905   return llvm::ConstantArray::get(ArrayType, Inits.data(), Inits.size());
2906 }
2907 
2908 /// GetGlobalVariable - Will return a global variable of the given type.
2909 /// If a variable with a different type already exists then a new variable
2910 /// with the right type will be created.
2911 /// FIXME: We should move this to CodeGenModule and rename it to something
2912 /// better and then use it in CGVTT and CGRTTI.
2913 static llvm::GlobalVariable *
2914 GetGlobalVariable(llvm::Module &Module, llvm::StringRef Name,
2915                   const llvm::Type *Ty,
2916                   llvm::GlobalValue::LinkageTypes Linkage) {
2917 
2918   llvm::GlobalVariable *GV = Module.getNamedGlobal(Name);
2919   llvm::GlobalVariable *OldGV = 0;
2920 
2921   if (GV) {
2922     // Check if the variable has the right type.
2923     if (GV->getType()->getElementType() == Ty)
2924       return GV;
2925 
2926     assert(GV->isDeclaration() && "Declaration has wrong type!");
2927 
2928     OldGV = GV;
2929   }
2930 
2931   // Create a new variable.
2932   GV = new llvm::GlobalVariable(Module, Ty, /*isConstant=*/true,
2933                                 Linkage, 0, Name);
2934 
2935   if (OldGV) {
2936     // Replace occurrences of the old variable if needed.
2937     GV->takeName(OldGV);
2938 
2939     if (!OldGV->use_empty()) {
2940       llvm::Constant *NewPtrForOldDecl =
2941         llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
2942       OldGV->replaceAllUsesWith(NewPtrForOldDecl);
2943     }
2944 
2945     OldGV->eraseFromParent();
2946   }
2947 
2948   return GV;
2949 }
2950 
2951 llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTable(const CXXRecordDecl *RD) {
2952   llvm::SmallString<256> OutName;
2953   CGM.getCXXABI().getMangleContext().mangleCXXVTable(RD, OutName);
2954   llvm::StringRef Name = OutName.str();
2955 
2956   ComputeVTableRelatedInformation(RD, true);
2957 
2958   const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
2959   llvm::ArrayType *ArrayType =
2960     llvm::ArrayType::get(Int8PtrTy, getNumVTableComponents(RD));
2961 
2962   return GetGlobalVariable(CGM.getModule(), Name, ArrayType,
2963                            llvm::GlobalValue::ExternalLinkage);
2964 }
2965 
2966 void
2967 CodeGenVTables::EmitVTableDefinition(llvm::GlobalVariable *VTable,
2968                                      llvm::GlobalVariable::LinkageTypes Linkage,
2969                                      const CXXRecordDecl *RD) {
2970   // Dump the vtable layout if necessary.
2971   if (CGM.getLangOptions().DumpVTableLayouts) {
2972     VTableBuilder Builder(*this, RD, 0, /*MostDerivedClassIsVirtual=*/0, RD);
2973 
2974     Builder.dumpLayout(llvm::errs());
2975   }
2976 
2977   assert(VTableThunksMap.count(RD) &&
2978          "No thunk status for this record decl!");
2979 
2980   const VTableThunksTy& Thunks = VTableThunksMap[RD];
2981 
2982   // Create and set the initializer.
2983   llvm::Constant *Init =
2984     CreateVTableInitializer(RD, getVTableComponentsData(RD),
2985                             getNumVTableComponents(RD), Thunks);
2986   VTable->setInitializer(Init);
2987 
2988   // Set the correct linkage.
2989   VTable->setLinkage(Linkage);
2990 
2991   // Set the right visibility.
2992   CGM.setTypeVisibility(VTable, RD, /*ForRTTI*/ false);
2993 }
2994 
2995 llvm::GlobalVariable *
2996 CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
2997                                       const BaseSubobject &Base,
2998                                       bool BaseIsVirtual,
2999                                       VTableAddressPointsMapTy& AddressPoints) {
3000   VTableBuilder Builder(*this, Base.getBase(), Base.getBaseOffset(),
3001                         /*MostDerivedClassIsVirtual=*/BaseIsVirtual, RD);
3002 
3003   // Dump the vtable layout if necessary.
3004   if (CGM.getLangOptions().DumpVTableLayouts)
3005     Builder.dumpLayout(llvm::errs());
3006 
3007   // Add the address points.
3008   AddressPoints.insert(Builder.address_points_begin(),
3009                        Builder.address_points_end());
3010 
3011   // Get the mangled construction vtable name.
3012   llvm::SmallString<256> OutName;
3013   CGM.getCXXABI().getMangleContext().
3014     mangleCXXCtorVTable(RD, Base.getBaseOffset() / 8, Base.getBase(), OutName);
3015   llvm::StringRef Name = OutName.str();
3016 
3017   const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
3018   llvm::ArrayType *ArrayType =
3019     llvm::ArrayType::get(Int8PtrTy, Builder.getNumVTableComponents());
3020 
3021   // Create the variable that will hold the construction vtable.
3022   llvm::GlobalVariable *VTable =
3023     GetGlobalVariable(CGM.getModule(), Name, ArrayType,
3024                       llvm::GlobalValue::InternalLinkage);
3025 
3026   // Add the thunks.
3027   VTableThunksTy VTableThunks;
3028   VTableThunks.append(Builder.vtable_thunks_begin(),
3029                       Builder.vtable_thunks_end());
3030 
3031   // Sort them.
3032   std::sort(VTableThunks.begin(), VTableThunks.end());
3033 
3034   // Create and set the initializer.
3035   llvm::Constant *Init =
3036     CreateVTableInitializer(Base.getBase(),
3037                             Builder.vtable_components_data_begin(),
3038                             Builder.getNumVTableComponents(), VTableThunks);
3039   VTable->setInitializer(Init);
3040 
3041   return VTable;
3042 }
3043 
3044 void
3045 CodeGenVTables::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
3046                                   const CXXRecordDecl *RD) {
3047   llvm::GlobalVariable *&VTable = VTables[RD];
3048   if (VTable) {
3049     assert(VTable->getInitializer() && "VTable doesn't have a definition!");
3050     return;
3051   }
3052 
3053   VTable = GetAddrOfVTable(RD);
3054   EmitVTableDefinition(VTable, Linkage, RD);
3055 
3056   GenerateVTT(Linkage, /*GenerateDefinition=*/true, RD);
3057 
3058   // If this is the magic class __cxxabiv1::__fundamental_type_info,
3059   // we will emit the typeinfo for the fundamental types. This is the
3060   // same behaviour as GCC.
3061   const DeclContext *DC = RD->getDeclContext();
3062   if (RD->getIdentifier() &&
3063       RD->getIdentifier()->isStr("__fundamental_type_info") &&
3064       isa<NamespaceDecl>(DC) &&
3065       cast<NamespaceDecl>(DC)->getIdentifier() &&
3066       cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
3067       DC->getParent()->isTranslationUnit())
3068     CGM.EmitFundamentalRTTIDescriptors();
3069 }
3070