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