1 //===--- CGVTables.cpp - Emit LLVM Code for C++ vtables -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code dealing with C++ code generation of virtual tables.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenModule.h"
15 #include "CodeGenFunction.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"
28 using namespace clang;
29 using namespace CodeGen;
33 /// BaseOffset - Represents an offset from a derived class to a direct or
34 /// indirect base class.
36 /// DerivedClass - The derived class.
37 const CXXRecordDecl *DerivedClass;
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;
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
47 CharUnits NonVirtualOffset;
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) { }
56 bool isEmpty() const { return NonVirtualOffset.isZero() && !VirtualBase; }
59 /// FinalOverriders - Contains the final overrider member functions for all
60 /// member functions in the base subobjects of a class.
61 class FinalOverriders {
63 /// OverriderInfo - Information about a final overrider.
64 struct OverriderInfo {
65 /// Method - The method decl of the overrider.
66 const CXXMethodDecl *Method;
68 /// Offset - the base offset of the overrider in the layout class.
71 OverriderInfo() : Method(0), Offset(CharUnits::Zero()) { }
75 /// MostDerivedClass - The most derived class for which the final overriders
77 const CXXRecordDecl *MostDerivedClass;
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;
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;
91 /// MostDerivedClassLayout - the AST record layout of the most derived class.
92 const ASTRecordLayout &MostDerivedClassLayout;
94 /// MethodBaseOffsetPairTy - Uniquely identifies a member function
95 /// in a base subobject.
96 typedef std::pair<const CXXMethodDecl *, CharUnits> MethodBaseOffsetPairTy;
98 typedef llvm::DenseMap<MethodBaseOffsetPairTy,
99 OverriderInfo> OverridersMapTy;
101 /// OverridersMap - The final overriders for all virtual member functions of
102 /// all the base subobjects of the most derived class.
103 OverridersMapTy OverridersMap;
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;
111 typedef llvm::DenseMap<const CXXRecordDecl *, unsigned> SubobjectCountMapTy;
113 /// ComputeBaseOffsets - Compute the offsets for all base subobjects of the
115 void ComputeBaseOffsets(BaseSubobject Base, bool IsVirtual,
116 CharUnits OffsetInLayoutClass,
117 SubobjectOffsetMapTy &SubobjectOffsets,
118 SubobjectOffsetMapTy &SubobjectLayoutClassOffsets,
119 SubobjectCountMapTy &SubobjectCounts);
121 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
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);
129 FinalOverriders(const CXXRecordDecl *MostDerivedClass,
130 CharUnits MostDerivedClassOffset,
131 const CXXRecordDecl *LayoutClass);
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!");
140 return OverridersMap.lookup(std::make_pair(MD, BaseOffset));
143 /// dump - dump the final overriders.
145 VisitedVirtualBasesSetTy VisitedVirtualBases;
146 dump(llvm::errs(), BaseSubobject(MostDerivedClass, CharUnits::Zero()),
147 VisitedVirtualBases);
152 #define DUMP_OVERRIDERS 0
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)) {
162 // Compute base offsets.
163 SubobjectOffsetMapTy SubobjectOffsets;
164 SubobjectOffsetMapTy SubobjectLayoutClassOffsets;
165 SubobjectCountMapTy SubobjectCounts;
166 ComputeBaseOffsets(BaseSubobject(MostDerivedClass, CharUnits::Zero()),
168 MostDerivedClassOffset,
169 SubobjectOffsets, SubobjectLayoutClassOffsets,
172 // Get the the final overriders.
173 CXXFinalOverriderMap FinalOverriders;
174 MostDerivedClass->getFinalOverriders(FinalOverriders);
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;
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(),
186 "Did not find subobject offset!");
188 CharUnits BaseOffset = SubobjectOffsets[std::make_pair(MD->getParent(),
191 assert(I->second.size() == 1 && "Final overrider is not unique!");
192 const UniqueVirtualMethod &Method = I->second.front();
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,
202 OverriderInfo& Overrider = OverridersMap[std::make_pair(MD, BaseOffset)];
203 assert(!Overrider.Method && "Overrider should not exist yet!");
205 Overrider.Offset = OverriderOffset;
206 Overrider.Method = Method.Method;
211 // And dump them (for now).
216 static BaseOffset ComputeBaseOffset(ASTContext &Context,
217 const CXXRecordDecl *DerivedRD,
218 const CXXBasePath &Path) {
219 CharUnits NonVirtualOffset = CharUnits::Zero();
221 unsigned NonVirtualStart = 0;
222 const CXXRecordDecl *VirtualBase = 0;
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];
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();
234 cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl());
238 // Now compute the non-virtual offset.
239 for (unsigned I = NonVirtualStart, E = Path.size(); I != E; ++I) {
240 const CXXBasePathElement &Element = Path[I];
242 // Check the base class offset.
243 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class);
245 const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>();
246 const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl());
248 NonVirtualOffset += Layout.getBaseClassOffset(Base);
251 // FIXME: This should probably use CharUnits or something. Maybe we should
252 // even change the base offsets in ASTRecordLayout to be specified in
254 return BaseOffset(DerivedRD, VirtualBase, NonVirtualOffset);
258 static BaseOffset ComputeBaseOffset(ASTContext &Context,
259 const CXXRecordDecl *BaseRD,
260 const CXXRecordDecl *DerivedRD) {
261 CXXBasePaths Paths(/*FindAmbiguities=*/false,
262 /*RecordPaths=*/true, /*DetectVirtual=*/false);
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!");
270 return ComputeBaseOffset(Context, DerivedRD, Paths.front());
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>();
280 // Canonicalize the return types.
281 CanQualType CanDerivedReturnType =
282 Context.getCanonicalType(DerivedFT->getResultType());
283 CanQualType CanBaseReturnType =
284 Context.getCanonicalType(BaseFT->getResultType());
286 assert(CanDerivedReturnType->getTypeClass() ==
287 CanBaseReturnType->getTypeClass() &&
288 "Types must have same type class!");
290 if (CanDerivedReturnType == CanBaseReturnType) {
291 // No adjustment needed.
295 if (isa<ReferenceType>(CanDerivedReturnType)) {
296 CanDerivedReturnType =
297 CanDerivedReturnType->getAs<ReferenceType>()->getPointeeType();
299 CanBaseReturnType->getAs<ReferenceType>()->getPointeeType();
300 } else if (isa<PointerType>(CanDerivedReturnType)) {
301 CanDerivedReturnType =
302 CanDerivedReturnType->getAs<PointerType>()->getPointeeType();
304 CanBaseReturnType->getAs<PointerType>()->getPointeeType();
306 assert(false && "Unexpected return type!");
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.
318 const CXXRecordDecl *DerivedRD =
319 cast<CXXRecordDecl>(cast<RecordType>(CanDerivedReturnType)->getDecl());
321 const CXXRecordDecl *BaseRD =
322 cast<CXXRecordDecl>(cast<RecordType>(CanBaseReturnType)->getDecl());
324 return ComputeBaseOffset(Context, BaseRD, DerivedRD);
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();
335 unsigned SubobjectNumber = 0;
337 SubobjectNumber = ++SubobjectCounts[RD];
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!");
345 SubobjectOffsets[std::make_pair(RD, SubobjectNumber)] = Base.getBaseOffset();
346 SubobjectLayoutClassOffsets[std::make_pair(RD, SubobjectNumber)] =
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());
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)))
362 const ASTRecordLayout &LayoutClassLayout =
363 Context.getASTRecordLayout(LayoutClass);
365 BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
366 BaseOffsetInLayoutClass =
367 LayoutClassLayout.getVBaseClassOffset(BaseDecl);
369 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
370 CharUnits Offset = Layout.getBaseClassOffset(BaseDecl);
372 BaseOffset = Base.getBaseOffset() + Offset;
373 BaseOffsetInLayoutClass = OffsetInLayoutClass + Offset;
376 ComputeBaseOffsets(BaseSubobject(BaseDecl, BaseOffset),
377 I->isVirtual(), BaseOffsetInLayoutClass,
378 SubobjectOffsets, SubobjectLayoutClassOffsets,
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);
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());
393 // Ignore bases that don't have any virtual member functions.
394 if (!BaseDecl->isPolymorphic())
397 CharUnits BaseOffset;
398 if (I->isVirtual()) {
399 if (!VisitedVirtualBases.insert(BaseDecl)) {
400 // We've visited this base before.
404 BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
406 BaseOffset = Layout.getBaseClassOffset(BaseDecl) + Base.getBaseOffset();
409 dump(Out, BaseSubobject(BaseDecl, BaseOffset), VisitedVirtualBases);
412 Out << "Final overriders for (" << RD->getQualifiedNameAsString() << ", ";
413 Out << Base.getBaseOffset().getQuantity() << ")\n";
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;
420 if (!MD->isVirtual())
423 OverriderInfo Overrider = getOverrider(MD, Base.getBaseOffset());
425 Out << " " << MD->getQualifiedNameAsString() << " - (";
426 Out << Overrider.Method->getQualifiedNameAsString();
427 Out << ", " << ", " << Overrider.Offset.getQuantity() << ')';
430 if (!Overrider.Method->isPure())
431 Offset = ComputeReturnAdjustmentBaseOffset(Context, Overrider.Method, MD);
433 if (!Offset.isEmpty()) {
434 Out << " [ret-adj: ";
435 if (Offset.VirtualBase)
436 Out << Offset.VirtualBase->getQualifiedNameAsString() << " vbase, ";
438 Out << Offset.NonVirtualOffset.getQuantity() << " nv]";
445 /// VTableComponent - Represents a single component in a vtable.
446 class VTableComponent {
455 /// CK_CompleteDtorPointer - A pointer to the complete destructor.
456 CK_CompleteDtorPointer,
458 /// CK_DeletingDtorPointer - A pointer to the deleting destructor.
459 CK_DeletingDtorPointer,
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
467 static VTableComponent MakeVCallOffset(CharUnits Offset) {
468 return VTableComponent(CK_VCallOffset, Offset);
471 static VTableComponent MakeVBaseOffset(CharUnits Offset) {
472 return VTableComponent(CK_VBaseOffset, Offset);
475 static VTableComponent MakeOffsetToTop(CharUnits Offset) {
476 return VTableComponent(CK_OffsetToTop, Offset);
479 static VTableComponent MakeRTTI(const CXXRecordDecl *RD) {
480 return VTableComponent(CK_RTTI, reinterpret_cast<uintptr_t>(RD));
483 static VTableComponent MakeFunction(const CXXMethodDecl *MD) {
484 assert(!isa<CXXDestructorDecl>(MD) &&
485 "Don't use MakeFunction with destructors!");
487 return VTableComponent(CK_FunctionPointer,
488 reinterpret_cast<uintptr_t>(MD));
491 static VTableComponent MakeCompleteDtor(const CXXDestructorDecl *DD) {
492 return VTableComponent(CK_CompleteDtorPointer,
493 reinterpret_cast<uintptr_t>(DD));
496 static VTableComponent MakeDeletingDtor(const CXXDestructorDecl *DD) {
497 return VTableComponent(CK_DeletingDtorPointer,
498 reinterpret_cast<uintptr_t>(DD));
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));
508 static VTableComponent getFromOpaqueInteger(uint64_t I) {
509 return VTableComponent(I);
512 /// getKind - Get the kind of this vtable component.
513 Kind getKind() const {
514 return (Kind)(Value & 0x7);
517 CharUnits getVCallOffset() const {
518 assert(getKind() == CK_VCallOffset && "Invalid component kind!");
523 CharUnits getVBaseOffset() const {
524 assert(getKind() == CK_VBaseOffset && "Invalid component kind!");
529 CharUnits getOffsetToTop() const {
530 assert(getKind() == CK_OffsetToTop && "Invalid component kind!");
535 const CXXRecordDecl *getRTTIDecl() const {
536 assert(getKind() == CK_RTTI && "Invalid component kind!");
538 return reinterpret_cast<CXXRecordDecl *>(getPointer());
541 const CXXMethodDecl *getFunctionDecl() const {
542 assert(getKind() == CK_FunctionPointer);
544 return reinterpret_cast<CXXMethodDecl *>(getPointer());
547 const CXXDestructorDecl *getDestructorDecl() const {
548 assert((getKind() == CK_CompleteDtorPointer ||
549 getKind() == CK_DeletingDtorPointer) && "Invalid component kind!");
551 return reinterpret_cast<CXXDestructorDecl *>(getPointer());
554 const CXXMethodDecl *getUnusedFunctionDecl() const {
555 assert(getKind() == CK_UnusedFunctionPointer);
557 return reinterpret_cast<CXXMethodDecl *>(getPointer());
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!");
567 Value = ((Offset.getQuantity() << 3) | ComponentKind);
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!");
578 assert((Ptr & 7) == 0 && "Pointer not sufficiently aligned!");
580 Value = Ptr | ComponentKind;
583 CharUnits getOffset() const {
584 assert((getKind() == CK_VCallOffset || getKind() == CK_VBaseOffset ||
585 getKind() == CK_OffsetToTop) && "Invalid component kind!");
587 return CharUnits::fromQuantity(Value >> 3);
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!");
598 return static_cast<uintptr_t>(Value & ~7ULL);
601 explicit VTableComponent(uint64_t Value)
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).
612 /// VCallOffsetMap - Keeps track of vcall offsets when building a vtable.
613 struct VCallOffsetMap {
615 typedef std::pair<const CXXMethodDecl *, CharUnits> MethodAndOffsetPairTy;
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;
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);
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);
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);
636 // empty - Return whether the offset map is empty or not.
637 bool empty() const { return Offsets.empty(); }
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>();
647 // Fast-path matches in the canonical types.
648 if (LT == RT) return true;
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())
656 for (unsigned I = 0, E = LT->getNumArgs(); I != E; ++I)
657 if (LT->getArgType(I) != RT->getArgType(I))
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!");
667 // A destructor can share a vcall offset with another destructor.
668 if (isa<CXXDestructorDecl>(LHS))
669 return isa<CXXDestructorDecl>(RHS);
671 // FIXME: We need to check more things here.
673 // The methods must have the same name.
674 DeclarationName LHSName = LHS->getDeclName();
675 DeclarationName RHSName = RHS->getDeclName();
676 if (LHSName != RHSName)
679 // And the same signatures.
680 return HasSameVirtualSignature(LHS, RHS);
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))
692 Offsets.push_back(MethodAndOffsetPairTy(MD, OffsetOffset));
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;
703 assert(false && "Should always find a vcall offset offset!");
704 return CharUnits::Zero();
707 /// VCallAndVBaseOffsetBuilder - Class for building vcall and vbase offsets.
708 class VCallAndVBaseOffsetBuilder {
710 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits>
711 VBaseOffsetOffsetsMapTy;
714 /// MostDerivedClass - The most derived class for which we're building vcall
715 /// and vbase offsets.
716 const CXXRecordDecl *MostDerivedClass;
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
721 const CXXRecordDecl *LayoutClass;
723 /// Context - The ASTContext which we will use for layout information.
726 /// Components - vcall and vbase offset components
727 typedef llvm::SmallVector<VTableComponent, 64> VTableComponentVectorTy;
728 VTableComponentVectorTy Components;
730 /// VisitedVirtualBases - Visited virtual bases.
731 llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
733 /// VCallOffsets - Keeps track of vcall offsets.
734 VCallOffsetMap VCallOffsets;
737 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets,
738 /// relative to the address point.
739 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
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;
745 /// AddVCallAndVBaseOffsets - Add vcall offsets and vbase offsets for the
746 /// given base subobject.
747 void AddVCallAndVBaseOffsets(BaseSubobject Base, bool BaseIsVirtual,
748 CharUnits RealBaseOffset);
750 /// AddVCallOffsets - Add vcall offsets for the given base subobject.
751 void AddVCallOffsets(BaseSubobject Base, CharUnits VBaseOffset);
753 /// AddVBaseOffsets - Add vbase offsets for the given class.
754 void AddVBaseOffsets(const CXXRecordDecl *Base,
755 CharUnits OffsetInLayoutClass);
757 /// getCurrentOffsetOffset - Get the current vcall or vbase offset offset in
758 /// chars, relative to the vtable address point.
759 CharUnits getCurrentOffsetOffset() const;
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) {
770 // Add vcall and vbase offsets.
771 AddVCallAndVBaseOffsets(Base, BaseIsVirtual, OffsetInLayoutClass);
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(); }
779 const VCallOffsetMap &getVCallOffsets() const { return VCallOffsets; }
780 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
781 return VBaseOffsetOffsets;
786 VCallAndVBaseOffsetBuilder::AddVCallAndVBaseOffsets(BaseSubobject Base,
788 CharUnits RealBaseOffset) {
789 const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base.getBase());
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).
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();
803 CharUnits PrimaryBaseOffset;
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!");
810 const ASTRecordLayout &MostDerivedClassLayout =
811 Context.getASTRecordLayout(MostDerivedClass);
814 MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
816 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 &&
817 "Primary base should have a zero offset!");
819 PrimaryBaseOffset = Base.getBaseOffset();
822 AddVCallAndVBaseOffsets(
823 BaseSubobject(PrimaryBase,PrimaryBaseOffset),
824 PrimaryBaseIsVirtual, RealBaseOffset);
827 AddVBaseOffsets(Base.getBase(), RealBaseOffset);
829 // We only want to add vcall offsets for virtual bases.
831 AddVCallOffsets(Base, RealBaseOffset);
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());
841 CharUnits PointerWidth =
842 Context.toCharUnitsFromBits(Context.Target.getPointerWidth(0));
843 CharUnits OffsetOffset = PointerWidth * OffsetIndex;
847 void VCallAndVBaseOffsetBuilder::AddVCallOffsets(BaseSubobject Base,
848 CharUnits VBaseOffset) {
849 const CXXRecordDecl *RD = Base.getBase();
850 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
852 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
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!");
862 AddVCallOffsets(BaseSubobject(PrimaryBase, Base.getBaseOffset()),
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;
871 if (!MD->isVirtual())
874 CharUnits OffsetOffset = getCurrentOffsetOffset();
876 // Don't add a vcall offset if we already have one for this member function
878 if (!VCallOffsets.AddVCallOffset(MD, OffsetOffset))
881 CharUnits Offset = CharUnits::Zero();
884 // Get the final overrider.
885 FinalOverriders::OverriderInfo Overrider =
886 Overriders->getOverrider(MD, Base.getBaseOffset());
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;
893 Components.push_back(
894 VTableComponent::MakeVCallOffset(Offset));
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) {
904 const CXXRecordDecl *BaseDecl =
905 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
906 if (BaseDecl == PrimaryBase)
909 // Get the base offset of this base.
910 CharUnits BaseOffset = Base.getBaseOffset() +
911 Layout.getBaseClassOffset(BaseDecl);
913 AddVCallOffsets(BaseSubobject(BaseDecl, BaseOffset),
919 VCallAndVBaseOffsetBuilder::AddVBaseOffsets(const CXXRecordDecl *RD,
920 CharUnits OffsetInLayoutClass) {
921 const ASTRecordLayout &LayoutClassLayout =
922 Context.getASTRecordLayout(LayoutClass);
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());
930 // Check if this is a virtual base that we haven't visited before.
931 if (I->isVirtual() && VisitedVirtualBases.insert(BaseDecl)) {
933 LayoutClassLayout.getVBaseClassOffset(BaseDecl) - OffsetInLayoutClass;
935 // Add the vbase offset offset.
936 assert(!VBaseOffsetOffsets.count(BaseDecl) &&
937 "vbase offset offset already exists!");
939 CharUnits VBaseOffsetOffset = getCurrentOffsetOffset();
940 VBaseOffsetOffsets.insert(
941 std::make_pair(BaseDecl, VBaseOffsetOffset));
943 Components.push_back(
944 VTableComponent::MakeVBaseOffset(Offset));
947 // Check the base class looking for more vbase offsets.
948 AddVBaseOffsets(BaseDecl, OffsetInLayoutClass);
952 /// VTableBuilder - Class for building vtable layout information.
953 class VTableBuilder {
955 /// PrimaryBasesSetVectorTy - A set vector of direct and indirect
957 typedef llvm::SmallSetVector<const CXXRecordDecl *, 8>
958 PrimaryBasesSetVectorTy;
960 typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits>
961 VBaseOffsetOffsetsMapTy;
963 typedef llvm::DenseMap<BaseSubobject, uint64_t>
967 /// VTables - Global vtable information.
968 CodeGenVTables &VTables;
970 /// MostDerivedClass - The most derived class for which we're building this
972 const CXXRecordDecl *MostDerivedClass;
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;
978 /// MostDerivedClassIsVirtual - Whether the most derived class is a virtual
979 /// base. (This only makes sense when building a construction vtable).
980 bool MostDerivedClassIsVirtual;
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
985 const CXXRecordDecl *LayoutClass;
987 /// Context - The ASTContext which we will use for layout information.
990 /// FinalOverriders - The final overriders of the most derived class.
991 const FinalOverriders Overriders;
993 /// VCallOffsetsForVBases - Keeps track of vcall offsets for the virtual
994 /// bases in this vtable.
995 llvm::DenseMap<const CXXRecordDecl *, VCallOffsetMap> VCallOffsetsForVBases;
997 /// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets for
998 /// the most derived class.
999 VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
1001 /// Components - The components of the vtable being built.
1002 llvm::SmallVector<VTableComponent, 64> Components;
1004 /// AddressPoints - Address points for the vtable being built.
1005 AddressPointsMapTy AddressPoints;
1007 /// MethodInfo - Contains information about a method in a vtable.
1008 /// (Used for computing 'this' pointer adjustment thunks.
1010 /// BaseOffset - The base offset of this method.
1011 const CharUnits BaseOffset;
1013 /// BaseOffsetInLayoutClass - The base offset in the layout class of this
1015 const CharUnits BaseOffsetInLayoutClass;
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;
1021 MethodInfo(CharUnits BaseOffset, CharUnits BaseOffsetInLayoutClass,
1022 uint64_t VTableIndex)
1023 : BaseOffset(BaseOffset),
1024 BaseOffsetInLayoutClass(BaseOffsetInLayoutClass),
1025 VTableIndex(VTableIndex) { }
1028 : BaseOffset(CharUnits::Zero()),
1029 BaseOffsetInLayoutClass(CharUnits::Zero()),
1033 typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy;
1035 /// MethodInfoMap - The information for all methods in the vtable we're
1036 /// currently building.
1037 MethodInfoMapTy MethodInfoMap;
1039 typedef llvm::DenseMap<uint64_t, ThunkInfo> VTableThunksMapTy;
1041 /// VTableThunks - The thunks by vtable index in the vtable currently being
1043 VTableThunksMapTy VTableThunks;
1045 typedef llvm::SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
1046 typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
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.
1052 /// AddThunk - Add a thunk for the given method.
1053 void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk);
1055 /// ComputeThisAdjustments - Compute the 'this' pointer adjustments for the
1056 /// part of the vtable we're currently building.
1057 void ComputeThisAdjustments();
1059 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
1061 /// PrimaryVirtualBases - All known virtual bases who are a primary base of
1062 /// some other base.
1063 VisitedVirtualBasesSetTy PrimaryVirtualBases;
1065 /// ComputeReturnAdjustment - Compute the return adjustment given a return
1066 /// adjustment base offset.
1067 ReturnAdjustment ComputeReturnAdjustment(BaseOffset Offset);
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;
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.
1078 ComputeThisAdjustment(const CXXMethodDecl *MD,
1079 CharUnits BaseOffsetInLayoutClass,
1080 FinalOverriders::OverriderInfo Overrider);
1082 /// AddMethod - Add a single virtual member function to the vtable
1083 /// components vector.
1084 void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment);
1086 /// IsOverriderUsed - Returns whether the overrider will ever be used in this
1087 /// part of the vtable.
1089 /// Itanium C++ ABI 2.5.2:
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 {};
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
1105 bool IsOverriderUsed(const CXXMethodDecl *Overrider,
1106 CharUnits BaseOffsetInLayoutClass,
1107 const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
1108 CharUnits FirstBaseOffsetInLayoutClass) const;
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);
1118 // LayoutVTable - Layout the vtable for the given base class, including its
1119 // secondary vtables and any vtables for virtual bases.
1120 void LayoutVTable();
1122 /// LayoutPrimaryAndSecondaryVTables - Layout the primary vtable for the
1123 /// given base subobject, as well as all its secondary vtables.
1125 /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
1126 /// or a direct or indirect base of a virtual base.
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);
1135 /// LayoutSecondaryVTables - Layout the secondary vtables for the given base
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);
1143 /// DeterminePrimaryVirtualBases - Determine the primary virtual bases in this
1144 /// class hierarchy.
1145 void DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
1146 CharUnits OffsetInLayoutClass,
1147 VisitedVirtualBasesSetTy &VBases);
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);
1154 /// isBuildingConstructionVTable - Return whether this vtable builder is
1155 /// building a construction vtable.
1156 bool isBuildingConstructorVTable() const {
1157 return MostDerivedClass != LayoutClass;
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) {
1174 ThunksMapTy::const_iterator thunks_begin() const {
1175 return Thunks.begin();
1178 ThunksMapTy::const_iterator thunks_end() const {
1179 return Thunks.end();
1182 const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
1183 return VBaseOffsetOffsets;
1186 /// getNumVTableComponents - Return the number of components in the vtable
1187 /// currently built.
1188 uint64_t getNumVTableComponents() const {
1189 return Components.size();
1192 const uint64_t *vtable_components_data_begin() const {
1193 return reinterpret_cast<const uint64_t *>(Components.begin());
1196 const uint64_t *vtable_components_data_end() const {
1197 return reinterpret_cast<const uint64_t *>(Components.end());
1200 AddressPointsMapTy::const_iterator address_points_begin() const {
1201 return AddressPoints.begin();
1204 AddressPointsMapTy::const_iterator address_points_end() const {
1205 return AddressPoints.end();
1208 VTableThunksMapTy::const_iterator vtable_thunks_begin() const {
1209 return VTableThunks.begin();
1212 VTableThunksMapTy::const_iterator vtable_thunks_end() const {
1213 return VTableThunks.end();
1216 /// dumpLayout - Dump the vtable layout.
1217 void dumpLayout(llvm::raw_ostream&);
1220 void VTableBuilder::AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk) {
1221 assert(!isBuildingConstructorVTable() &&
1222 "Can't add thunks for construction vtable");
1224 llvm::SmallVector<ThunkInfo, 1> &ThunksVector = Thunks[MD];
1226 // Check if we have this thunk already.
1227 if (std::find(ThunksVector.begin(), ThunksVector.end(), Thunk) !=
1231 ThunksVector.push_back(Thunk);
1234 typedef llvm::SmallPtrSet<const CXXMethodDecl *, 8> OverriddenMethodsSetTy;
1236 /// ComputeAllOverriddenMethods - Given a method decl, will return a set of all
1237 /// the overridden methods that the function decl overrides.
1239 ComputeAllOverriddenMethods(const CXXMethodDecl *MD,
1240 OverriddenMethodsSetTy& OverriddenMethods) {
1241 assert(MD->isVirtual() && "Method is not virtual!");
1243 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
1244 E = MD->end_overridden_methods(); I != E; ++I) {
1245 const CXXMethodDecl *OverriddenMD = *I;
1247 OverriddenMethods.insert(OverriddenMD);
1249 ComputeAllOverriddenMethods(OverriddenMD, OverriddenMethods);
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;
1261 // Ignore adjustments for unused function pointers.
1262 uint64_t VTableIndex = MethodInfo.VTableIndex;
1263 if (Components[VTableIndex].getKind() ==
1264 VTableComponent::CK_UnusedFunctionPointer)
1267 // Get the final overrider for this method.
1268 FinalOverriders::OverriderInfo Overrider =
1269 Overriders.getOverrider(MD, MethodInfo.BaseOffset);
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())
1282 ThisAdjustment ThisAdjustment =
1283 ComputeThisAdjustment(MD, MethodInfo.BaseOffsetInLayoutClass, Overrider);
1285 if (ThisAdjustment.isEmpty())
1289 VTableThunks[VTableIndex].This = ThisAdjustment;
1291 if (isa<CXXDestructorDecl>(MD)) {
1292 // Add an adjustment for the deleting destructor as well.
1293 VTableThunks[VTableIndex + 1].This = ThisAdjustment;
1297 /// Clear the method info map.
1298 MethodInfoMap.clear();
1300 if (isBuildingConstructorVTable()) {
1301 // We don't need to store thunk information for construction vtables.
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;
1311 switch (Component.getKind()) {
1313 llvm_unreachable("Unexpected vtable component kind!");
1314 case VTableComponent::CK_FunctionPointer:
1315 MD = Component.getFunctionDecl();
1317 case VTableComponent::CK_CompleteDtorPointer:
1318 MD = Component.getDestructorDecl();
1320 case VTableComponent::CK_DeletingDtorPointer:
1321 // We've already added the thunk when we saw the complete dtor pointer.
1325 if (MD->getParent() == MostDerivedClass)
1326 AddThunk(MD, Thunk);
1330 ReturnAdjustment VTableBuilder::ComputeReturnAdjustment(BaseOffset Offset) {
1331 ReturnAdjustment Adjustment;
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();
1341 Adjustment.VBaseOffsetOffset =
1342 VTables.getVirtualBaseOffsetOffset(Offset.DerivedClass,
1343 Offset.VirtualBase).getQuantity();
1347 Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity();
1354 VTableBuilder::ComputeThisAdjustmentBaseOffset(BaseSubobject Base,
1355 BaseSubobject Derived) const {
1356 const CXXRecordDecl *BaseRD = Base.getBase();
1357 const CXXRecordDecl *DerivedRD = Derived.getBase();
1359 CXXBasePaths Paths(/*FindAmbiguities=*/true,
1360 /*RecordPaths=*/true, /*DetectVirtual=*/true);
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();
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();
1372 BaseOffset Offset = ComputeBaseOffset(Context, DerivedRD, *I);
1374 CharUnits OffsetToBaseSubobject = Offset.NonVirtualOffset;
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);
1382 /// Get the virtual base offset, relative to the most derived class
1384 OffsetToBaseSubobject +=
1385 LayoutClassLayout.getVBaseClassOffset(Offset.VirtualBase);
1387 // Otherwise, the non-virtual offset is relative to the derived class
1389 OffsetToBaseSubobject += Derived.getBaseOffset();
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;
1401 return BaseOffset();
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();
1412 BaseSubobject OverriddenBaseSubobject(MD->getParent(),
1413 BaseOffsetInLayoutClass);
1415 BaseSubobject OverriderBaseSubobject(Overrider.Method->getParent(),
1418 // Compute the adjustment offset.
1419 BaseOffset Offset = ComputeThisAdjustmentBaseOffset(OverriddenBaseSubobject,
1420 OverriderBaseSubobject);
1421 if (Offset.isEmpty())
1422 return ThisAdjustment();
1424 ThisAdjustment Adjustment;
1426 if (Offset.VirtualBase) {
1427 // Get the vcall offset map for this virtual base.
1428 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Offset.VirtualBase];
1430 if (VCallOffsets.empty()) {
1431 // We don't have vcall offsets for this virtual base, go ahead and
1433 VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, MostDerivedClass,
1434 /*FinalOverriders=*/0,
1435 BaseSubobject(Offset.VirtualBase,
1437 /*BaseIsVirtual=*/true,
1438 /*OffsetInLayoutClass=*/
1441 VCallOffsets = Builder.getVCallOffsets();
1444 Adjustment.VCallOffsetOffset =
1445 VCallOffsets.getVCallOffsetOffset(MD).getQuantity();
1448 // Set the non-virtual part of the adjustment.
1449 Adjustment.NonVirtual = Offset.NonVirtualOffset.getQuantity();
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!");
1461 // Add both the complete destructor and the deleting destructor.
1462 Components.push_back(VTableComponent::MakeCompleteDtor(DD));
1463 Components.push_back(VTableComponent::MakeDeletingDtor(DD));
1465 // Add the return adjustment if necessary.
1466 if (!ReturnAdjustment.isEmpty())
1467 VTableThunks[Components.size()].Return = ReturnAdjustment;
1469 // Add the function.
1470 Components.push_back(VTableComponent::MakeFunction(MD));
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:
1479 /// struct A { virtual void f(); }
1480 /// struct B : A { virtual void f(); }
1481 /// struct C : B { virtual void f(); }
1483 /// OverridesIndirectMethodInBase will return true if given C::f as the method
1484 /// and { A } as the set of bases.
1486 OverridesIndirectMethodInBases(const CXXMethodDecl *MD,
1487 VTableBuilder::PrimaryBasesSetVectorTy &Bases) {
1488 if (Bases.count(MD->getParent()))
1491 for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
1492 E = MD->end_overridden_methods(); I != E; ++I) {
1493 const CXXMethodDecl *OverriddenMD = *I;
1495 // Check "indirect overriders".
1496 if (OverridesIndirectMethodInBases(OverriddenMD, Bases))
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)
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
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)
1522 VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
1524 const CXXRecordDecl *RD = FirstBaseInPrimaryBaseChain;
1525 PrimaryBases.insert(RD);
1527 // Now traverse the base chain, starting with the first base, until we find
1528 // the base that is no longer a primary base.
1530 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1531 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
1536 if (Layout.isPrimaryBaseVirtual()) {
1537 assert(Layout.getVBaseClassOffsetInBits(PrimaryBase) == 0 &&
1538 "Primary base should always be at offset 0!");
1540 const ASTRecordLayout &LayoutClassLayout =
1541 Context.getASTRecordLayout(LayoutClass);
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.
1551 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 &&
1552 "Primary base should always be at offset 0!");
1555 if (!PrimaryBases.insert(PrimaryBase))
1556 assert(false && "Found a duplicate primary base!");
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);
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);
1574 for (int I = Bases.size(), E = 0; I != E; --I) {
1575 const CXXRecordDecl *PrimaryBase = Bases[I - 1];
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;
1582 // We found our overridden method.
1583 if (OverriddenMD->getParent() == PrimaryBase)
1584 return OverriddenMD;
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);
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!");
1606 const ASTRecordLayout &MostDerivedClassLayout =
1607 Context.getASTRecordLayout(MostDerivedClass);
1610 MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
1612 const ASTRecordLayout &LayoutClassLayout =
1613 Context.getASTRecordLayout(LayoutClass);
1615 PrimaryBaseOffsetInLayoutClass =
1616 LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
1618 assert(Layout.getBaseClassOffsetInBits(PrimaryBase) == 0 &&
1619 "Primary base should have a zero offset!");
1621 PrimaryBaseOffset = Base.getBaseOffset();
1622 PrimaryBaseOffsetInLayoutClass = BaseOffsetInLayoutClass;
1625 AddMethods(BaseSubobject(PrimaryBase, PrimaryBaseOffset),
1626 PrimaryBaseOffsetInLayoutClass, FirstBaseInPrimaryBaseChain,
1627 FirstBaseOffsetInLayoutClass, PrimaryBases);
1629 if (!PrimaryBases.insert(PrimaryBase))
1630 assert(false && "Found a duplicate primary base!");
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;
1638 if (!MD->isVirtual())
1641 // Get the final overrider.
1642 FinalOverriders::OverriderInfo Overrider =
1643 Overriders.getOverrider(MD, Base.getBaseOffset());
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
1654 assert(MethodInfoMap.count(OverriddenMD) &&
1655 "Did not find the overridden method!");
1656 MethodInfo &OverriddenMethodInfo = MethodInfoMap[OverriddenMD];
1658 MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
1659 OverriddenMethodInfo.VTableIndex);
1661 assert(!MethodInfoMap.count(MD) &&
1662 "Should not have method info for this method yet!");
1664 MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
1665 MethodInfoMap.erase(OverriddenMD);
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,
1677 if (ThisAdjustment.VCallOffsetOffset &&
1678 Overrider.Method->getParent() == MostDerivedClass) {
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);
1688 // This is a virtual thunk for the most derived class, add it.
1689 AddThunk(Overrider.Method,
1690 ThunkInfo(ThisAdjustment, ReturnAdjustment));
1698 // Insert the method info for this method.
1699 MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
1702 assert(!MethodInfoMap.count(MD) &&
1703 "Should not have method info for this method yet!");
1704 MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
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));
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);
1723 ReturnAdjustment ReturnAdjustment =
1724 ComputeReturnAdjustment(ReturnAdjustmentOffset);
1726 AddMethod(Overrider.Method, ReturnAdjustment);
1730 void VTableBuilder::LayoutVTable() {
1731 LayoutPrimaryAndSecondaryVTables(BaseSubobject(MostDerivedClass,
1733 /*BaseIsMorallyVirtual=*/false,
1734 MostDerivedClassIsVirtual,
1735 MostDerivedClassOffset);
1737 VisitedVirtualBasesSetTy VBases;
1739 // Determine the primary virtual bases.
1740 DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset,
1744 LayoutVTablesForVirtualBases(MostDerivedClass, VBases);
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!");
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());
1760 // Check if we need to add these vcall offsets.
1761 if (BaseIsVirtualInLayoutClass && !Builder.getVCallOffsets().empty()) {
1762 VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()];
1764 if (VCallOffsets.empty())
1765 VCallOffsets = Builder.getVCallOffsets();
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();
1773 // Add the offset to top.
1774 CharUnits OffsetToTop = MostDerivedClassOffset - OffsetInLayoutClass;
1775 Components.push_back(
1776 VTableComponent::MakeOffsetToTop(OffsetToTop));
1778 // Next, add the RTTI.
1779 Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass));
1781 uint64_t AddressPoint = Components.size();
1783 // Now go through all virtual member functions and add them.
1784 PrimaryBasesSetVectorTy PrimaryBases;
1785 AddMethods(Base, OffsetInLayoutClass,
1786 Base.getBase(), OffsetInLayoutClass,
1789 // Compute 'this' pointer adjustments.
1790 ComputeThisAdjustments();
1792 // Add all address points.
1793 const CXXRecordDecl *RD = Base.getBase();
1795 AddressPoints.insert(std::make_pair(
1796 BaseSubobject(RD, OffsetInLayoutClass),
1799 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1800 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
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);
1811 if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
1812 OffsetInLayoutClass) {
1813 // We don't want to add this class (or any of its primary bases).
1821 // Layout secondary vtables.
1822 LayoutSecondaryVTables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass);
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.
1833 const CXXRecordDecl *RD = Base.getBase();
1834 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1835 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
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.
1843 const CXXRecordDecl *BaseDecl =
1844 cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1846 // Ignore bases that don't have a vtable.
1847 if (!BaseDecl->isDynamicClass())
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())
1860 // Get the base offset of this base.
1861 CharUnits RelativeBaseOffset = Layout.getBaseClassOffset(BaseDecl);
1862 CharUnits BaseOffset = Base.getBaseOffset() + RelativeBaseOffset;
1864 CharUnits BaseOffsetInLayoutClass =
1865 OffsetInLayoutClass + RelativeBaseOffset;
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);
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);
1885 VTableBuilder::DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
1886 CharUnits OffsetInLayoutClass,
1887 VisitedVirtualBasesSetTy &VBases) {
1888 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
1890 // Check if this base has a primary base.
1891 if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
1893 // Check if it's virtual.
1894 if (Layout.isPrimaryBaseVirtual()) {
1895 bool IsPrimaryVirtualBase = true;
1897 if (isBuildingConstructorVTable()) {
1898 // Check if the base is actually a primary base in the class we use for
1900 const ASTRecordLayout &LayoutClassLayout =
1901 Context.getASTRecordLayout(LayoutClass);
1903 CharUnits PrimaryBaseOffsetInLayoutClass =
1904 LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
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;
1912 if (IsPrimaryVirtualBase)
1913 PrimaryVirtualBases.insert(PrimaryBase);
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());
1923 CharUnits BaseOffsetInLayoutClass;
1925 if (I->isVirtual()) {
1926 if (!VBases.insert(BaseDecl))
1929 const ASTRecordLayout &LayoutClassLayout =
1930 Context.getASTRecordLayout(LayoutClass);
1932 BaseOffsetInLayoutClass =
1933 LayoutClassLayout.getVBaseClassOffset(BaseDecl);
1935 BaseOffsetInLayoutClass =
1936 OffsetInLayoutClass + Layout.getBaseClassOffset(BaseDecl);
1939 DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases);
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());
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);
1964 const ASTRecordLayout &LayoutClassLayout =
1965 Context.getASTRecordLayout(LayoutClass);
1966 CharUnits BaseOffsetInLayoutClass =
1967 LayoutClassLayout.getVBaseClassOffset(BaseDecl);
1969 LayoutPrimaryAndSecondaryVTables(
1970 BaseSubobject(BaseDecl, BaseOffset),
1971 /*BaseIsMorallyVirtual=*/true,
1972 /*BaseIsVirtualInLayoutClass=*/true,
1973 BaseOffsetInLayoutClass);
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);
1983 /// dumpLayout - Dump the vtable layout.
1984 void VTableBuilder::dumpLayout(llvm::raw_ostream& Out) {
1986 if (isBuildingConstructorVTable()) {
1987 Out << "Construction vtable for ('";
1988 Out << MostDerivedClass->getQualifiedNameAsString() << "', ";
1989 Out << MostDerivedClassOffset.getQuantity() << ") in '";
1990 Out << LayoutClass->getQualifiedNameAsString();
1992 Out << "Vtable for '";
1993 Out << MostDerivedClass->getQualifiedNameAsString();
1995 Out << "' (" << Components.size() << " entries).\n";
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
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;
2007 AddressPointsByIndex.insert(std::make_pair(Index, Base));
2010 for (unsigned I = 0, E = Components.size(); I != E; ++I) {
2013 Out << llvm::format("%4d | ", I);
2015 const VTableComponent &Component = Components[I];
2017 // Dump the component.
2018 switch (Component.getKind()) {
2020 case VTableComponent::CK_VCallOffset:
2021 Out << "vcall_offset ("
2022 << Component.getVCallOffset().getQuantity()
2026 case VTableComponent::CK_VBaseOffset:
2027 Out << "vbase_offset ("
2028 << Component.getVBaseOffset().getQuantity()
2032 case VTableComponent::CK_OffsetToTop:
2033 Out << "offset_to_top ("
2034 << Component.getOffsetToTop().getQuantity()
2038 case VTableComponent::CK_RTTI:
2039 Out << Component.getRTTIDecl()->getQualifiedNameAsString() << " RTTI";
2042 case VTableComponent::CK_FunctionPointer: {
2043 const CXXMethodDecl *MD = Component.getFunctionDecl();
2046 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
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";
2059 if (Thunk.Return.VBaseOffsetOffset) {
2060 Out << ", " << Thunk.Return.VBaseOffsetOffset;
2061 Out << " vbase offset offset";
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";
2072 if (Thunk.This.VCallOffsetOffset) {
2073 Out << ", " << Thunk.This.VCallOffsetOffset;
2074 Out << " vcall offset offset";
2084 case VTableComponent::CK_CompleteDtorPointer:
2085 case VTableComponent::CK_DeletingDtorPointer: {
2087 Component.getKind() == VTableComponent::CK_CompleteDtorPointer;
2089 const CXXDestructorDecl *DD = Component.getDestructorDecl();
2091 Out << DD->getQualifiedNameAsString();
2093 Out << "() [complete]";
2095 Out << "() [deleting]";
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";
2107 if (Thunk.This.VCallOffsetOffset) {
2108 Out << ", " << Thunk.This.VCallOffsetOffset;
2109 Out << " vcall offset offset";
2119 case VTableComponent::CK_UnusedFunctionPointer: {
2120 const CXXMethodDecl *MD = Component.getUnusedFunctionDecl();
2123 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2125 Out << "[unused] " << Str;
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;
2141 Out << " -- (" << Base.getBase()->getQualifiedNameAsString();
2142 Out << ", " << Base.getBaseOffset().getQuantity();
2143 Out << ") vtable address --\n";
2145 CharUnits BaseOffset =
2146 AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset();
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());
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";
2170 if (isBuildingConstructorVTable())
2173 if (MostDerivedClass->getNumVBases()) {
2174 // We store the virtual base class names and their offsets in a map to get
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));
2186 Out << "Virtual base offset offsets for '";
2187 Out << MostDerivedClass->getQualifiedNameAsString() << "' (";
2188 Out << ClassNamesAndOffsets.size();
2189 Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n";
2191 for (std::map<std::string, CharUnits>::const_iterator I =
2192 ClassNamesAndOffsets.begin(), E = ClassNamesAndOffsets.end();
2194 Out << " " << I->first << " | " << I->second.getQuantity() << '\n';
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;
2203 for (ThunksMapTy::const_iterator I = Thunks.begin(), E = Thunks.end();
2205 const CXXMethodDecl *MD = I->first;
2206 std::string MethodName =
2207 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
2210 MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
2213 for (std::map<std::string, const CXXMethodDecl *>::const_iterator I =
2214 MethodNamesAndDecls.begin(), E = MethodNamesAndDecls.end();
2216 const std::string &MethodName = I->first;
2217 const CXXMethodDecl *MD = I->second;
2219 ThunkInfoVectorTy ThunksVector = Thunks[MD];
2220 std::sort(ThunksVector.begin(), ThunksVector.end());
2222 Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
2223 Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n";
2225 for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) {
2226 const ThunkInfo &Thunk = ThunksVector[I];
2228 Out << llvm::format("%4d | ", I);
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";
2239 if (!Thunk.This.isEmpty())
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";
2248 if (Thunk.This.VCallOffsetOffset) {
2249 Out << ", " << Thunk.This.VCallOffsetOffset;
2250 Out << " vcall offset offset";
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;
2265 for (CXXRecordDecl::method_iterator i = MostDerivedClass->method_begin(),
2266 e = MostDerivedClass->method_end(); i != e; ++i) {
2267 const CXXMethodDecl *MD = *i;
2269 // We only want virtual member functions.
2270 if (!MD->isVirtual())
2273 std::string MethodName =
2274 PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
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]";
2283 IndicesMap[VTables.getMethodVTableIndex(MD)] = MethodName;
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";
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;
2298 Out << llvm::format(" %4u | ", VTableIndex) << MethodName << '\n';
2308 CollectPrimaryBases(const CXXRecordDecl *RD, ASTContext &Context,
2309 VTableBuilder::PrimaryBasesSetVectorTy &PrimaryBases) {
2310 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
2311 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
2316 CollectPrimaryBases(PrimaryBase, Context, PrimaryBases);
2318 if (!PrimaryBases.insert(PrimaryBase))
2319 assert(false && "Found a duplicate primary base!");
2322 void CodeGenVTables::ComputeMethodVTableIndices(const CXXRecordDecl *RD) {
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.
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.
2333 int64_t CurrentIndex = 0;
2335 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
2336 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
2339 assert(PrimaryBase->isDefinition() &&
2340 "Should have the definition decl of the primary base!");
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);
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);
2352 const CXXDestructorDecl *ImplicitVirtualDtor = 0;
2354 for (CXXRecordDecl::method_iterator i = RD->method_begin(),
2355 e = RD->method_end(); i != e; ++i) {
2356 const CXXMethodDecl *MD = *i;
2358 // We only want virtual methods.
2359 if (!MD->isVirtual())
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
2371 if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
2372 const CXXDestructorDecl *OverriddenDD =
2373 cast<CXXDestructorDecl>(OverriddenMD);
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));
2381 MethodVTableIndices[MD] = getMethodVTableIndex(OverriddenMD);
2384 // We don't need to add an entry for this method.
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;
2397 // Add the complete dtor.
2398 MethodVTableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++;
2400 // Add the deleting dtor.
2401 MethodVTableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++;
2404 MethodVTableIndices[MD] = CurrentIndex++;
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.
2413 // Add the complete dtor.
2414 MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] =
2417 // Add the deleting dtor.
2418 MethodVTableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] =
2422 NumVirtualFunctionPointers[RD] = CurrentIndex;
2425 bool CodeGenVTables::ShouldEmitVTableInThisTU(const CXXRecordDecl *RD) {
2426 assert(RD->isDynamicClass() && "Non dynamic classes have no VTable.");
2428 TemplateSpecializationKind TSK = RD->getTemplateSpecializationKind();
2429 if (TSK == TSK_ExplicitInstantiationDeclaration)
2432 const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD);
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)
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)
2449 return KeyFunction->hasBody();
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())
2458 ComputeMethodVTableIndices(RD);
2460 I = NumVirtualFunctionPointers.find(RD);
2461 assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!");
2465 uint64_t CodeGenVTables::getMethodVTableIndex(GlobalDecl GD) {
2466 MethodVTableIndicesTy::iterator I = MethodVTableIndices.find(GD);
2467 if (I != MethodVTableIndices.end())
2470 const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
2472 ComputeMethodVTableIndices(RD);
2474 I = MethodVTableIndices.find(GD);
2475 assert(I != MethodVTableIndices.end() && "Did not find index!");
2480 CodeGenVTables::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD,
2481 const CXXRecordDecl *VBase) {
2482 ClassPairTy ClassPair(RD, VBase);
2484 VirtualBaseClassOffsetOffsetsMapTy::iterator I =
2485 VirtualBaseClassOffsetOffsets.find(ClassPair);
2486 if (I != VirtualBaseClassOffsetOffsets.end())
2489 VCallAndVBaseOffsetBuilder Builder(RD, RD, /*FinalOverriders=*/0,
2490 BaseSubobject(RD, CharUnits::Zero()),
2491 /*BaseIsVirtual=*/false,
2492 /*OffsetInLayoutClass=*/CharUnits::Zero());
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);
2500 VirtualBaseClassOffsetOffsets.insert(
2501 std::make_pair(ClassPair, I->second));
2504 I = VirtualBaseClassOffsetOffsets.find(ClassPair);
2505 assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!");
2511 CodeGenVTables::getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD) {
2512 assert(AddressPoints.count(std::make_pair(RD, Base)) &&
2513 "Did not find address point!");
2515 uint64_t AddressPoint = AddressPoints.lookup(std::make_pair(RD, Base));
2516 assert(AddressPoint && "Address point must not be zero!");
2518 return AddressPoint;
2521 llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
2522 const ThunkInfo &Thunk) {
2523 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
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(),
2532 getCXXABI().getMangleContext().mangleThunk(MD, Thunk, Out);
2535 const llvm::Type *Ty = getTypes().GetFunctionTypeForVTable(GD);
2536 return GetOrCreateLLVMFunction(Name, Ty, GD, /*ForVTable=*/true);
2539 static llvm::Value *PerformTypeAdjustment(CodeGenFunction &CGF,
2541 int64_t NonVirtualAdjustment,
2542 int64_t VirtualAdjustment) {
2543 if (!NonVirtualAdjustment && !VirtualAdjustment)
2546 const llvm::Type *Int8PtrTy =
2547 llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
2549 llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy);
2551 if (NonVirtualAdjustment) {
2552 // Do the non-virtual adjustment.
2553 V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
2556 if (VirtualAdjustment) {
2557 const llvm::Type *PtrDiffTy =
2558 CGF.ConvertType(CGF.getContext().getPointerDiffType());
2560 // Do the virtual adjustment.
2561 llvm::Value *VTablePtrPtr =
2562 CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo());
2564 llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
2566 llvm::Value *OffsetPtr =
2567 CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
2569 OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
2571 // Load the adjustment offset from the vtable.
2572 llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr);
2574 // Adjust our pointer.
2575 V = CGF.Builder.CreateInBoundsGEP(V, Offset);
2578 // Cast back to the original type.
2579 return CGF.Builder.CreateBitCast(V, Ptr->getType());
2582 static void setThunkVisibility(CodeGenModule &CGM, const CXXMethodDecl *MD,
2583 const ThunkInfo &Thunk, llvm::Function *Fn) {
2584 CGM.setGlobalVisibility(Fn, MD);
2586 if (!CGM.getCodeGenOpts().HiddenWeakVTables)
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.
2594 // This follows CodeGenModule::setTypeVisibility; see the comments
2595 // there for explanation.
2597 if ((Fn->getLinkage() != llvm::GlobalVariable::LinkOnceODRLinkage &&
2598 Fn->getLinkage() != llvm::GlobalVariable::WeakODRLinkage) ||
2599 Fn->getVisibility() != llvm::GlobalVariable::DefaultVisibility)
2602 if (MD->getExplicitVisibility())
2605 switch (MD->getTemplateSpecializationKind()) {
2606 case TSK_ExplicitInstantiationDefinition:
2607 case TSK_ExplicitInstantiationDeclaration:
2610 case TSK_Undeclared:
2613 case TSK_ExplicitSpecialization:
2614 case TSK_ImplicitInstantiation:
2615 if (!CGM.getCodeGenOpts().HiddenWeakTemplateVTables)
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())
2627 Fn->setVisibility(llvm::GlobalValue::HiddenVisibility);
2631 static bool similar(const ABIArgInfo &infoL, CanQualType typeL,
2632 const ABIArgInfo &infoR, CanQualType typeR) {
2633 return (infoL.getKind() == infoR.getKind() &&
2635 (isa<PointerType>(typeL) && isa<PointerType>(typeR)) ||
2636 (isa<ReferenceType>(typeL) && isa<ReferenceType>(typeR))));
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();
2646 llvm::BasicBlock *AdjustNull = 0;
2647 llvm::BasicBlock *AdjustNotNull = 0;
2648 llvm::BasicBlock *AdjustEnd = 0;
2650 llvm::Value *ReturnValue = RV.getScalarVal();
2652 if (NullCheckValue) {
2653 AdjustNull = CGF.createBasicBlock("adjust.null");
2654 AdjustNotNull = CGF.createBasicBlock("adjust.notnull");
2655 AdjustEnd = CGF.createBasicBlock("adjust.end");
2657 llvm::Value *IsNull = CGF.Builder.CreateIsNull(ReturnValue);
2658 CGF.Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
2659 CGF.EmitBlock(AdjustNotNull);
2662 ReturnValue = PerformTypeAdjustment(CGF, ReturnValue,
2663 Thunk.Return.NonVirtual,
2664 Thunk.Return.VBaseOffsetOffset);
2666 if (NullCheckValue) {
2667 CGF.Builder.CreateBr(AdjustEnd);
2668 CGF.EmitBlock(AdjustNull);
2669 CGF.Builder.CreateBr(AdjustEnd);
2670 CGF.EmitBlock(AdjustEnd);
2672 llvm::PHINode *PHI = CGF.Builder.CreatePHI(ReturnValue->getType(), 2);
2673 PHI->addIncoming(ReturnValue, AdjustNotNull);
2674 PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
2679 return RValue::get(ReturnValue);
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(
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();
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);
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();
2720 // "Initialize" CGF (minimally).
2723 // Get the "this" value
2724 llvm::Function::arg_iterator AI = Fn->arg_begin();
2725 if (CGM.ReturnTypeUsesSRet(FnInfo))
2728 // Find the first store of "this", which will be to the alloca associated
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();
2735 if (isa<llvm::StoreInst>(I) && I->getOperand(0) == ThisPtr) {
2736 ThisStore = cast<llvm::StoreInst>(I);
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);
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());
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());
2773 FunctionArgList FunctionArgs;
2775 // FIXME: It would be nice if more of this code could be shared with
2776 // CodeGenFunction::GenerateCode.
2778 // Create the implicit 'this' parameter declaration.
2780 CGM.getCXXABI().BuildInstanceFunctionParams(*this, ResultType, FunctionArgs);
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;
2787 FunctionArgs.push_back(Param);
2790 StartFunction(GlobalDecl(), ResultType, Fn, FnInfo, FunctionArgs,
2793 CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
2795 // Adjust the 'this' pointer if necessary.
2796 llvm::Value *AdjustedThisPtr =
2797 PerformTypeAdjustment(*this, LoadCXXThis(),
2798 Thunk.This.NonVirtual,
2799 Thunk.This.VCallOffsetOffset);
2801 CallArgList CallArgs;
2803 // Add our adjusted 'this' pointer.
2804 CallArgs.add(RValue::get(AdjustedThisPtr), ThisType);
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);
2814 const llvm::Type *Ty =
2815 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(GD),
2817 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
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));
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());
2841 // Now emit our call.
2842 RValue RV = EmitCall(FnInfo, Callee, Slot, CallArgs, MD);
2844 if (!Thunk.Return.isEmpty())
2845 RV = PerformReturnAdjustment(*this, ResultType, RV, Thunk);
2847 if (!ResultType->isVoidType() && Slot.isNull())
2848 CGM.getCXXABI().EmitReturnFromThunk(*this, RV, ResultType);
2852 // Set the right linkage.
2853 CGM.setFunctionLinkage(MD, Fn);
2855 // Set the right visibility.
2856 setThunkVisibility(CGM, MD, Thunk, Fn);
2859 void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk,
2860 bool UseAvailableExternallyLinkage)
2862 const CGFunctionInfo &FnInfo = CGM.getTypes().getFunctionInfo(GD);
2864 // FIXME: re-use FnInfo in this computation.
2865 llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk);
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);
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);
2879 // If the types mismatch then we have to rewrite the definition.
2880 assert(OldThunkFn->isDeclaration() &&
2881 "Shouldn't replace non-declaration");
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);
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);
2894 // Remove the old thunk.
2895 OldThunkFn->eraseFromParent();
2898 llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
2900 if (!ThunkFn->isDeclaration()) {
2901 if (UseAvailableExternallyLinkage) {
2902 // There is already a thunk emitted for this function, do nothing.
2906 // If a function has a body, it should have available_externally linkage.
2907 assert(ThunkFn->hasAvailableExternallyLinkage() &&
2908 "Function should have available_externally linkage!");
2910 // Change the linkage.
2911 CGM.setFunctionLinkage(cast<CXXMethodDecl>(GD.getDecl()), ThunkFn);
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
2920 // FIXME: Do something better here; GenerateVarArgsThunk is extremely ugly.
2921 if (!UseAvailableExternallyLinkage)
2922 CodeGenFunction(CGM).GenerateVarArgsThunk(ThunkFn, FnInfo, GD, Thunk);
2924 // Normal thunk body generation.
2925 CodeGenFunction(CGM).GenerateThunk(ThunkFn, FnInfo, GD, Thunk);
2928 if (UseAvailableExternallyLinkage)
2929 ThunkFn->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
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)
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())))
2944 EmitThunk(GD, Thunk, /*UseAvailableExternallyLinkage=*/true);
2947 void CodeGenVTables::EmitThunks(GlobalDecl GD)
2949 const CXXMethodDecl *MD =
2950 cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
2952 // We don't need to generate thunks for the base destructor.
2953 if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
2956 const CXXRecordDecl *RD = MD->getParent();
2958 // Compute VTable related info for this class.
2959 ComputeVTableRelatedInformation(RD, false);
2961 ThunksMapTy::const_iterator I = Thunks.find(MD);
2962 if (I == Thunks.end()) {
2963 // We did not find a thunk for this method.
2967 const ThunkInfoVectorTy &ThunkInfoVector = I->second;
2968 for (unsigned I = 0, E = ThunkInfoVector.size(); I != E; ++I)
2969 EmitThunk(GD, ThunkInfoVector[I], /*UseAvailableExternallyLinkage=*/false);
2972 void CodeGenVTables::ComputeVTableRelatedInformation(const CXXRecordDecl *RD,
2973 bool RequireVTable) {
2974 VTableLayoutData &Entry = VTableLayoutMap[RD];
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);
2984 // Check if we've computed this information before.
2985 if (Entry.getPointer())
2988 VTableBuilder Builder(*this, RD, CharUnits::Zero(),
2989 /*MostDerivedClassIsVirtual=*/0, RD);
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;
2996 NumVTableComponents += 1;
2998 uint64_t *LayoutData = new uint64_t[NumVTableComponents + 1];
3000 LayoutData[NumVTableComponents] = 0;
3001 Entry.setPointer(LayoutData);
3003 // Store the number of components.
3004 LayoutData[0] = NumVTableComponents;
3006 // Store the components.
3007 std::copy(Builder.vtable_components_data_begin(),
3008 Builder.vtable_components_data_end(),
3011 // Add the known thunks.
3012 Thunks.insert(Builder.thunks_begin(), Builder.thunks_end());
3014 // Add the thunks needed in this vtable.
3015 assert(!VTableThunksMap.count(RD) &&
3016 "Thunks already exists for this vtable!");
3018 VTableThunksTy &VTableThunks = VTableThunksMap[RD];
3019 VTableThunks.append(Builder.vtable_thunks_begin(),
3020 Builder.vtable_thunks_end());
3023 std::sort(VTableThunks.begin(), VTableThunks.end());
3025 // Add the address points.
3026 for (VTableBuilder::AddressPointsMapTy::const_iterator I =
3027 Builder.address_points_begin(), E = Builder.address_points_end();
3030 uint64_t &AddressPoint = AddressPoints[std::make_pair(RD, I->first)];
3032 // Check if we already have the address points for this base.
3033 assert(!AddressPoint && "Address point already exists for this base!");
3035 AddressPoint = I->second;
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())
3044 const RecordType *VBaseRT =
3045 RD->vbases_begin()->getType()->getAs<RecordType>();
3046 const CXXRecordDecl *VBase = cast<CXXRecordDecl>(VBaseRT->getDecl());
3048 if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase)))
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);
3057 VirtualBaseClassOffsetOffsets.insert(
3058 std::make_pair(ClassPair, I->second));
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;
3069 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
3071 const llvm::Type *PtrDiffTy =
3072 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
3074 QualType ClassType = CGM.getContext().getTagDeclType(RD);
3075 llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType);
3077 unsigned NextVTableThunkIndex = 0;
3079 llvm::Constant* PureVirtualFn = 0;
3081 for (unsigned I = 0; I != NumComponents; ++I) {
3082 VTableComponent Component =
3083 VTableComponent::getFromOpaqueInteger(Components[I]);
3085 llvm::Constant *Init = 0;
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);
3093 case VTableComponent::CK_VBaseOffset:
3094 Init = llvm::ConstantInt::get(PtrDiffTy,
3095 Component.getVBaseOffset().getQuantity());
3096 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
3098 case VTableComponent::CK_OffsetToTop:
3099 Init = llvm::ConstantInt::get(PtrDiffTy,
3100 Component.getOffsetToTop().getQuantity());
3101 Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
3103 case VTableComponent::CK_RTTI:
3104 Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
3106 case VTableComponent::CK_FunctionPointer:
3107 case VTableComponent::CK_CompleteDtorPointer:
3108 case VTableComponent::CK_DeletingDtorPointer: {
3111 // Get the right global decl.
3112 switch (Component.getKind()) {
3114 llvm_unreachable("Unexpected vtable component kind");
3115 case VTableComponent::CK_FunctionPointer:
3116 GD = Component.getFunctionDecl();
3118 case VTableComponent::CK_CompleteDtorPointer:
3119 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
3121 case VTableComponent::CK_DeletingDtorPointer:
3122 GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
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);
3133 CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual");
3134 PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
3138 Init = PureVirtualFn;
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;
3145 Init = CGM.GetAddrOfThunk(GD, Thunk);
3146 MaybeEmitThunkAvailableExternally(GD, Thunk);
3148 NextVTableThunkIndex++;
3150 const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVTable(GD);
3152 Init = CGM.GetAddrOfFunction(GD, Ty, /*ForVTable=*/true);
3155 Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
3160 case VTableComponent::CK_UnusedFunctionPointer:
3161 Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
3165 Inits.push_back(Init);
3168 llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
3169 return llvm::ConstantArray::get(ArrayType, Inits);
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);
3177 llvm::StringRef Name = OutName.str();
3179 ComputeVTableRelatedInformation(RD, /*VTableRequired=*/true);
3181 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
3182 llvm::ArrayType *ArrayType =
3183 llvm::ArrayType::get(Int8PtrTy, getNumVTableComponents(RD));
3185 llvm::GlobalVariable *GV =
3186 CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType,
3187 llvm::GlobalValue::ExternalLinkage);
3188 GV->setUnnamedAddr(true);
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);
3201 Builder.dumpLayout(llvm::errs());
3204 assert(VTableThunksMap.count(RD) &&
3205 "No thunk status for this record decl!");
3207 const VTableThunksTy& Thunks = VTableThunksMap[RD];
3209 // Create and set the initializer.
3210 llvm::Constant *Init =
3211 CreateVTableInitializer(RD, getVTableComponentsData(RD),
3212 getNumVTableComponents(RD), Thunks);
3213 VTable->setInitializer(Init);
3215 // Set the correct linkage.
3216 VTable->setLinkage(Linkage);
3218 // Set the right visibility.
3219 CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForVTable);
3222 llvm::GlobalVariable *
3223 CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
3224 const BaseSubobject &Base,
3226 llvm::GlobalVariable::LinkageTypes Linkage,
3227 VTableAddressPointsMapTy& AddressPoints) {
3228 VTableBuilder Builder(*this, Base.getBase(),
3229 Base.getBaseOffset(),
3230 /*MostDerivedClassIsVirtual=*/BaseIsVirtual, RD);
3232 // Dump the vtable layout if necessary.
3233 if (CGM.getLangOptions().DumpVTableLayouts)
3234 Builder.dumpLayout(llvm::errs());
3236 // Add the address points.
3237 AddressPoints.insert(Builder.address_points_begin(),
3238 Builder.address_points_end());
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(),
3247 llvm::StringRef Name = OutName.str();
3249 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
3250 llvm::ArrayType *ArrayType =
3251 llvm::ArrayType::get(Int8PtrTy, Builder.getNumVTableComponents());
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);
3258 // V-tables are always unnamed_addr.
3259 VTable->setUnnamedAddr(true);
3262 VTableThunksTy VTableThunks;
3263 VTableThunks.append(Builder.vtable_thunks_begin(),
3264 Builder.vtable_thunks_end());
3267 std::sort(VTableThunks.begin(), VTableThunks.end());
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);
3280 CodeGenVTables::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
3281 const CXXRecordDecl *RD) {
3282 llvm::GlobalVariable *&VTable = VTables[RD];
3284 assert(VTable->getInitializer() && "VTable doesn't have a definition!");
3288 VTable = GetAddrOfVTable(RD);
3289 EmitVTableDefinition(VTable, Linkage, RD);
3291 if (RD->getNumVBases()) {
3292 llvm::GlobalVariable *VTT = GetAddrOfVTT(RD);
3293 EmitVTTDefinition(VTT, Linkage, RD);
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();