2010-04-08 20:30:25 +04:00
|
|
|
//===--- CGVtables.cpp - Emit LLVM Code for C++ vtables -------------------===//
|
2009-10-12 02:13:54 +04:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code dealing with C++ code generation of virtual tables.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenModule.h"
|
|
|
|
#include "CodeGenFunction.h"
|
2009-11-27 23:47:55 +03:00
|
|
|
#include "clang/AST/CXXInheritance.h"
|
2009-10-12 02:13:54 +04:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2009-11-26 22:32:45 +03:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2010-02-27 19:18:19 +03:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2010-02-13 13:38:52 +03:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2010-02-11 11:02:13 +03:00
|
|
|
#include "llvm/Support/Format.h"
|
2010-03-17 23:06:32 +03:00
|
|
|
#include <algorithm>
|
2009-11-13 08:46:16 +03:00
|
|
|
#include <cstdio>
|
2009-10-12 02:13:54 +04:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2009-11-28 01:21:51 +03:00
|
|
|
namespace {
|
2010-02-11 11:02:13 +03:00
|
|
|
|
2010-02-23 04:34:28 +03:00
|
|
|
/// BaseOffset - Represents an offset from a derived class to a direct or
|
|
|
|
/// indirect base class.
|
|
|
|
struct BaseOffset {
|
|
|
|
/// DerivedClass - The derived class.
|
|
|
|
const CXXRecordDecl *DerivedClass;
|
|
|
|
|
|
|
|
/// VirtualBase - If the path from the derived class to the base class
|
|
|
|
/// involves a virtual base class, this holds its declaration.
|
|
|
|
const CXXRecordDecl *VirtualBase;
|
|
|
|
|
|
|
|
/// NonVirtualOffset - The offset from the derived class to the base class.
|
|
|
|
/// (Or the offset from the virtual base class to the base class, if the
|
|
|
|
/// path from the derived class to the base class involves a virtual base
|
|
|
|
/// class.
|
|
|
|
int64_t NonVirtualOffset;
|
|
|
|
|
|
|
|
BaseOffset() : DerivedClass(0), VirtualBase(0), NonVirtualOffset(0) { }
|
|
|
|
BaseOffset(const CXXRecordDecl *DerivedClass,
|
|
|
|
const CXXRecordDecl *VirtualBase, int64_t NonVirtualOffset)
|
|
|
|
: DerivedClass(DerivedClass), VirtualBase(VirtualBase),
|
|
|
|
NonVirtualOffset(NonVirtualOffset) { }
|
|
|
|
|
|
|
|
bool isEmpty() const { return !NonVirtualOffset && !VirtualBase; }
|
|
|
|
};
|
|
|
|
|
2010-02-12 00:24:32 +03:00
|
|
|
/// FinalOverriders - Contains the final overrider member functions for all
|
|
|
|
/// member functions in the base subobjects of a class.
|
|
|
|
class FinalOverriders {
|
2010-02-12 19:55:34 +03:00
|
|
|
public:
|
|
|
|
/// OverriderInfo - Information about a final overrider.
|
|
|
|
struct OverriderInfo {
|
|
|
|
/// Method - The method decl of the overrider.
|
|
|
|
const CXXMethodDecl *Method;
|
2010-02-18 19:29:24 +03:00
|
|
|
|
2010-03-10 05:33:41 +03:00
|
|
|
/// Offset - the base offset of the overrider in the layout class.
|
|
|
|
uint64_t Offset;
|
2010-02-12 19:55:34 +03:00
|
|
|
|
2010-03-12 08:02:01 +03:00
|
|
|
OverriderInfo() : Method(0), Offset(0) { }
|
2010-02-12 19:55:34 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2010-02-12 00:24:32 +03:00
|
|
|
/// MostDerivedClass - The most derived class for which the final overriders
|
|
|
|
/// are stored.
|
|
|
|
const CXXRecordDecl *MostDerivedClass;
|
|
|
|
|
2010-03-10 05:33:41 +03:00
|
|
|
/// MostDerivedClassOffset - If we're building final overriders for a
|
|
|
|
/// construction vtable, this holds the offset from the layout class to the
|
|
|
|
/// most derived class.
|
|
|
|
const uint64_t MostDerivedClassOffset;
|
|
|
|
|
|
|
|
/// LayoutClass - The class we're using for layout information. Will be
|
|
|
|
/// different than the most derived class if the final overriders are for a
|
|
|
|
/// construction vtable.
|
|
|
|
const CXXRecordDecl *LayoutClass;
|
|
|
|
|
2010-02-12 00:24:32 +03:00
|
|
|
ASTContext &Context;
|
|
|
|
|
|
|
|
/// MostDerivedClassLayout - the AST record layout of the most derived class.
|
|
|
|
const ASTRecordLayout &MostDerivedClassLayout;
|
|
|
|
|
2010-02-13 05:02:03 +03:00
|
|
|
/// BaseSubobjectMethodPairTy - Uniquely identifies a member function
|
|
|
|
/// in a base subobject.
|
|
|
|
typedef std::pair<BaseSubobject, const CXXMethodDecl *>
|
|
|
|
BaseSubobjectMethodPairTy;
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<BaseSubobjectMethodPairTy,
|
2010-02-12 19:55:34 +03:00
|
|
|
OverriderInfo> OverridersMapTy;
|
2010-02-12 00:24:32 +03:00
|
|
|
|
|
|
|
/// OverridersMap - The final overriders for all virtual member functions of
|
|
|
|
/// all the base subobjects of the most derived class.
|
|
|
|
OverridersMapTy OverridersMap;
|
|
|
|
|
2010-02-14 03:37:35 +03:00
|
|
|
/// VisitedVirtualBases - A set of all the visited virtual bases, used to
|
|
|
|
/// avoid visiting virtual bases more than once.
|
|
|
|
llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
|
|
|
|
|
2010-02-13 05:02:03 +03:00
|
|
|
typedef llvm::DenseMap<BaseSubobjectMethodPairTy, BaseOffset>
|
|
|
|
AdjustmentOffsetsMapTy;
|
|
|
|
|
|
|
|
/// ReturnAdjustments - Holds return adjustments for all the overriders that
|
|
|
|
/// need to perform return value adjustments.
|
|
|
|
AdjustmentOffsetsMapTy ReturnAdjustments;
|
2010-02-14 02:17:31 +03:00
|
|
|
|
2010-03-03 07:58:02 +03:00
|
|
|
// FIXME: We might be able to get away with making this a SmallSet.
|
|
|
|
typedef llvm::SmallSetVector<uint64_t, 2> OffsetSetVectorTy;
|
2010-02-12 04:40:03 +03:00
|
|
|
|
|
|
|
/// SubobjectOffsetsMapTy - This map is used for keeping track of all the
|
|
|
|
/// base subobject offsets that a single class declaration might refer to.
|
|
|
|
///
|
|
|
|
/// For example, in:
|
|
|
|
///
|
|
|
|
/// struct A { virtual void f(); };
|
|
|
|
/// struct B1 : A { };
|
|
|
|
/// struct B2 : A { };
|
|
|
|
/// struct C : B1, B2 { virtual void f(); };
|
|
|
|
///
|
|
|
|
/// when we determine that C::f() overrides A::f(), we need to update the
|
|
|
|
/// overriders map for both A-in-B1 and A-in-B2 and the subobject offsets map
|
|
|
|
/// will have the subobject offsets for both A copies.
|
2010-03-03 07:58:02 +03:00
|
|
|
typedef llvm::DenseMap<const CXXRecordDecl *, OffsetSetVectorTy>
|
2010-02-12 04:40:03 +03:00
|
|
|
SubobjectOffsetsMapTy;
|
|
|
|
|
2010-02-12 00:24:32 +03:00
|
|
|
/// ComputeFinalOverriders - Compute the final overriders for a given base
|
|
|
|
/// subobject (and all its direct and indirect bases).
|
2010-02-12 04:40:03 +03:00
|
|
|
void ComputeFinalOverriders(BaseSubobject Base,
|
2010-02-17 20:48:25 +03:00
|
|
|
bool BaseSubobjectIsVisitedVBase,
|
2010-03-10 05:33:41 +03:00
|
|
|
uint64_t OffsetInLayoutClass,
|
2010-02-12 04:40:03 +03:00
|
|
|
SubobjectOffsetsMapTy &Offsets);
|
2010-02-12 00:24:32 +03:00
|
|
|
|
|
|
|
/// AddOverriders - Add the final overriders for this base subobject to the
|
|
|
|
/// map of final overriders.
|
2010-03-26 03:35:45 +03:00
|
|
|
void AddOverriders(BaseSubobject Base, uint64_t OffsetInLayoutClass,
|
2010-03-10 05:33:41 +03:00
|
|
|
SubobjectOffsetsMapTy &Offsets);
|
2010-02-12 04:40:03 +03:00
|
|
|
|
|
|
|
/// PropagateOverrider - Propagate the NewMD overrider to all the functions
|
|
|
|
/// that OldMD overrides. For example, if we have:
|
|
|
|
///
|
|
|
|
/// struct A { virtual void f(); };
|
|
|
|
/// struct B : A { virtual void f(); };
|
|
|
|
/// struct C : B { virtual void f(); };
|
|
|
|
///
|
|
|
|
/// and we want to override B::f with C::f, we also need to override A::f with
|
|
|
|
/// C::f.
|
|
|
|
void PropagateOverrider(const CXXMethodDecl *OldMD,
|
2010-02-14 01:23:31 +03:00
|
|
|
BaseSubobject NewBase,
|
2010-03-10 05:33:41 +03:00
|
|
|
uint64_t OverriderOffsetInLayoutClass,
|
2010-02-12 04:40:03 +03:00
|
|
|
const CXXMethodDecl *NewMD,
|
|
|
|
SubobjectOffsetsMapTy &Offsets);
|
2010-02-12 00:24:32 +03:00
|
|
|
|
2010-02-12 04:40:03 +03:00
|
|
|
static void MergeSubobjectOffsets(const SubobjectOffsetsMapTy &NewOffsets,
|
|
|
|
SubobjectOffsetsMapTy &Offsets);
|
|
|
|
|
2010-02-12 00:24:32 +03:00
|
|
|
public:
|
2010-03-10 05:33:41 +03:00
|
|
|
FinalOverriders(const CXXRecordDecl *MostDerivedClass,
|
|
|
|
uint64_t MostDerivedClassOffset,
|
|
|
|
const CXXRecordDecl *LayoutClass);
|
2010-02-25 01:18:01 +03:00
|
|
|
|
2010-02-12 00:24:32 +03:00
|
|
|
/// getOverrider - Get the final overrider for the given method declaration in
|
|
|
|
/// the given base subobject.
|
2010-02-13 23:11:51 +03:00
|
|
|
OverriderInfo getOverrider(BaseSubobject Base,
|
|
|
|
const CXXMethodDecl *MD) const {
|
2010-02-12 00:24:32 +03:00
|
|
|
assert(OverridersMap.count(std::make_pair(Base, MD)) &&
|
|
|
|
"Did not find overrider!");
|
|
|
|
|
|
|
|
return OverridersMap.lookup(std::make_pair(Base, MD));
|
|
|
|
}
|
|
|
|
|
2010-02-14 02:40:17 +03:00
|
|
|
/// getReturnAdjustmentOffset - Get the return adjustment offset for the
|
|
|
|
/// method decl in the given base subobject. Returns an empty base offset if
|
|
|
|
/// no adjustment is needed.
|
2010-02-13 23:11:51 +03:00
|
|
|
BaseOffset getReturnAdjustmentOffset(BaseSubobject Base,
|
|
|
|
const CXXMethodDecl *MD) const {
|
|
|
|
return ReturnAdjustments.lookup(std::make_pair(Base, MD));
|
|
|
|
}
|
|
|
|
|
2010-02-12 00:24:32 +03:00
|
|
|
/// dump - dump the final overriders.
|
2010-02-14 03:37:35 +03:00
|
|
|
void dump() {
|
|
|
|
assert(VisitedVirtualBases.empty() &&
|
|
|
|
"Visited virtual bases aren't empty!");
|
|
|
|
dump(llvm::errs(), BaseSubobject(MostDerivedClass, 0));
|
|
|
|
VisitedVirtualBases.clear();
|
2010-02-12 00:24:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// dump - dump the final overriders for a base subobject, and all its direct
|
|
|
|
/// and indirect base subobjects.
|
2010-02-14 03:37:35 +03:00
|
|
|
void dump(llvm::raw_ostream &Out, BaseSubobject Base);
|
2010-02-12 00:24:32 +03:00
|
|
|
};
|
2010-02-13 05:02:03 +03:00
|
|
|
|
2010-02-27 23:02:53 +03:00
|
|
|
#define DUMP_OVERRIDERS 0
|
|
|
|
|
2010-03-10 05:33:41 +03:00
|
|
|
FinalOverriders::FinalOverriders(const CXXRecordDecl *MostDerivedClass,
|
|
|
|
uint64_t MostDerivedClassOffset,
|
|
|
|
const CXXRecordDecl *LayoutClass)
|
2010-02-12 00:24:32 +03:00
|
|
|
: MostDerivedClass(MostDerivedClass),
|
2010-03-10 05:33:41 +03:00
|
|
|
MostDerivedClassOffset(MostDerivedClassOffset), LayoutClass(LayoutClass),
|
2010-02-12 00:24:32 +03:00
|
|
|
Context(MostDerivedClass->getASTContext()),
|
|
|
|
MostDerivedClassLayout(Context.getASTRecordLayout(MostDerivedClass)) {
|
|
|
|
|
|
|
|
// Compute the final overriders.
|
2010-02-12 04:40:03 +03:00
|
|
|
SubobjectOffsetsMapTy Offsets;
|
2010-02-17 20:48:25 +03:00
|
|
|
ComputeFinalOverriders(BaseSubobject(MostDerivedClass, 0),
|
2010-03-10 05:33:41 +03:00
|
|
|
/*BaseSubobjectIsVisitedVBase=*/false,
|
|
|
|
MostDerivedClassOffset, Offsets);
|
2010-02-17 20:48:25 +03:00
|
|
|
VisitedVirtualBases.clear();
|
2010-02-27 23:02:53 +03:00
|
|
|
|
|
|
|
#if DUMP_OVERRIDERS
|
2010-02-12 00:24:32 +03:00
|
|
|
// And dump them (for now).
|
|
|
|
dump();
|
2010-02-14 00:33:22 +03:00
|
|
|
|
|
|
|
// Also dump the base offsets (for now).
|
|
|
|
for (SubobjectOffsetsMapTy::const_iterator I = Offsets.begin(),
|
|
|
|
E = Offsets.end(); I != E; ++I) {
|
2010-03-10 05:33:41 +03:00
|
|
|
const OffsetSetVectorTy& OffsetSetVector = I->second;
|
2010-02-14 00:33:22 +03:00
|
|
|
|
|
|
|
llvm::errs() << "Base offsets for ";
|
|
|
|
llvm::errs() << I->first->getQualifiedNameAsString() << '\n';
|
|
|
|
|
2010-03-10 05:33:41 +03:00
|
|
|
for (unsigned I = 0, E = OffsetSetVector.size(); I != E; ++I)
|
|
|
|
llvm::errs() << " " << I << " - " << OffsetSetVector[I] / 8 << '\n';
|
2010-02-14 00:33:22 +03:00
|
|
|
}
|
2010-02-27 23:02:53 +03:00
|
|
|
#endif
|
2010-02-12 00:24:32 +03:00
|
|
|
}
|
|
|
|
|
2010-02-12 04:40:03 +03:00
|
|
|
void FinalOverriders::AddOverriders(BaseSubobject Base,
|
2010-03-10 05:33:41 +03:00
|
|
|
uint64_t OffsetInLayoutClass,
|
2010-02-12 04:40:03 +03:00
|
|
|
SubobjectOffsetsMapTy &Offsets) {
|
2010-02-12 00:24:32 +03:00
|
|
|
const CXXRecordDecl *RD = Base.getBase();
|
|
|
|
|
|
|
|
for (CXXRecordDecl::method_iterator I = RD->method_begin(),
|
|
|
|
E = RD->method_end(); I != E; ++I) {
|
|
|
|
const CXXMethodDecl *MD = *I;
|
|
|
|
|
|
|
|
if (!MD->isVirtual())
|
|
|
|
continue;
|
|
|
|
|
2010-02-12 04:40:03 +03:00
|
|
|
// First, propagate the overrider.
|
2010-03-10 05:33:41 +03:00
|
|
|
PropagateOverrider(MD, Base, OffsetInLayoutClass, MD, Offsets);
|
2010-02-12 04:40:03 +03:00
|
|
|
|
|
|
|
// Add the overrider as the final overrider of itself.
|
2010-02-12 19:55:34 +03:00
|
|
|
OverriderInfo& Overrider = OverridersMap[std::make_pair(Base, MD)];
|
|
|
|
assert(!Overrider.Method && "Overrider should not exist yet!");
|
2010-02-12 00:24:32 +03:00
|
|
|
|
2010-03-10 05:33:41 +03:00
|
|
|
Overrider.Offset = OffsetInLayoutClass;
|
2010-02-12 19:55:34 +03:00
|
|
|
Overrider.Method = MD;
|
2010-02-12 00:24:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-23 04:34:28 +03:00
|
|
|
static BaseOffset ComputeBaseOffset(ASTContext &Context,
|
|
|
|
const CXXRecordDecl *DerivedRD,
|
|
|
|
const CXXBasePath &Path) {
|
2010-02-14 02:17:31 +03:00
|
|
|
int64_t NonVirtualOffset = 0;
|
2010-02-13 05:02:03 +03:00
|
|
|
|
2010-02-13 23:41:15 +03:00
|
|
|
unsigned NonVirtualStart = 0;
|
|
|
|
const CXXRecordDecl *VirtualBase = 0;
|
|
|
|
|
|
|
|
// First, look for the virtual base class.
|
|
|
|
for (unsigned I = 0, E = Path.size(); I != E; ++I) {
|
|
|
|
const CXXBasePathElement &Element = Path[I];
|
|
|
|
|
|
|
|
if (Element.Base->isVirtual()) {
|
|
|
|
// FIXME: Can we break when we find the first virtual base?
|
|
|
|
// (If we can't, can't we just iterate over the path in reverse order?)
|
|
|
|
NonVirtualStart = I + 1;
|
|
|
|
QualType VBaseType = Element.Base->getType();
|
|
|
|
VirtualBase =
|
|
|
|
cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now compute the non-virtual offset.
|
|
|
|
for (unsigned I = NonVirtualStart, E = Path.size(); I != E; ++I) {
|
|
|
|
const CXXBasePathElement &Element = Path[I];
|
2010-02-13 05:02:03 +03:00
|
|
|
|
|
|
|
// Check the base class offset.
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class);
|
2010-02-26 01:18:35 +03:00
|
|
|
|
2010-02-13 05:02:03 +03:00
|
|
|
const RecordType *BaseType = Element.Base->getType()->getAs<RecordType>();
|
|
|
|
const CXXRecordDecl *Base = cast<CXXRecordDecl>(BaseType->getDecl());
|
2010-02-26 01:18:35 +03:00
|
|
|
|
2010-02-13 05:02:03 +03:00
|
|
|
NonVirtualOffset += Layout.getBaseClassOffset(Base);
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: This should probably use CharUnits or something. Maybe we should
|
|
|
|
// even change the base offsets in ASTRecordLayout to be specified in
|
|
|
|
// CharUnits.
|
2010-02-23 04:34:28 +03:00
|
|
|
return BaseOffset(DerivedRD, VirtualBase, NonVirtualOffset / 8);
|
2010-02-14 03:16:19 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-02-23 04:34:28 +03:00
|
|
|
static BaseOffset ComputeBaseOffset(ASTContext &Context,
|
|
|
|
const CXXRecordDecl *BaseRD,
|
|
|
|
const CXXRecordDecl *DerivedRD) {
|
2010-02-14 03:16:19 +03:00
|
|
|
CXXBasePaths Paths(/*FindAmbiguities=*/false,
|
|
|
|
/*RecordPaths=*/true, /*DetectVirtual=*/false);
|
|
|
|
|
|
|
|
if (!const_cast<CXXRecordDecl *>(DerivedRD)->
|
|
|
|
isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) {
|
|
|
|
assert(false && "Class must be derived from the passed in base class!");
|
2010-02-23 04:34:28 +03:00
|
|
|
return BaseOffset();
|
2010-02-14 03:16:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ComputeBaseOffset(Context, DerivedRD, Paths.front());
|
2010-02-13 05:02:03 +03:00
|
|
|
}
|
|
|
|
|
2010-02-23 04:34:28 +03:00
|
|
|
static BaseOffset
|
2010-02-14 02:17:31 +03:00
|
|
|
ComputeReturnAdjustmentBaseOffset(ASTContext &Context,
|
|
|
|
const CXXMethodDecl *DerivedMD,
|
|
|
|
const CXXMethodDecl *BaseMD) {
|
2010-02-13 05:02:03 +03:00
|
|
|
const FunctionType *BaseFT = BaseMD->getType()->getAs<FunctionType>();
|
|
|
|
const FunctionType *DerivedFT = DerivedMD->getType()->getAs<FunctionType>();
|
|
|
|
|
|
|
|
// Canonicalize the return types.
|
|
|
|
CanQualType CanDerivedReturnType =
|
|
|
|
Context.getCanonicalType(DerivedFT->getResultType());
|
|
|
|
CanQualType CanBaseReturnType =
|
|
|
|
Context.getCanonicalType(BaseFT->getResultType());
|
|
|
|
|
|
|
|
assert(CanDerivedReturnType->getTypeClass() ==
|
|
|
|
CanBaseReturnType->getTypeClass() &&
|
|
|
|
"Types must have same type class!");
|
|
|
|
|
|
|
|
if (CanDerivedReturnType == CanBaseReturnType) {
|
|
|
|
// No adjustment needed.
|
2010-02-23 04:34:28 +03:00
|
|
|
return BaseOffset();
|
2010-02-13 05:02:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<ReferenceType>(CanDerivedReturnType)) {
|
|
|
|
CanDerivedReturnType =
|
|
|
|
CanDerivedReturnType->getAs<ReferenceType>()->getPointeeType();
|
|
|
|
CanBaseReturnType =
|
|
|
|
CanBaseReturnType->getAs<ReferenceType>()->getPointeeType();
|
|
|
|
} else if (isa<PointerType>(CanDerivedReturnType)) {
|
|
|
|
CanDerivedReturnType =
|
|
|
|
CanDerivedReturnType->getAs<PointerType>()->getPointeeType();
|
|
|
|
CanBaseReturnType =
|
|
|
|
CanBaseReturnType->getAs<PointerType>()->getPointeeType();
|
|
|
|
} else {
|
|
|
|
assert(false && "Unexpected return type!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to compare unqualified types here; consider
|
|
|
|
// const T *Base::foo();
|
|
|
|
// T *Derived::foo();
|
|
|
|
if (CanDerivedReturnType.getUnqualifiedType() ==
|
|
|
|
CanBaseReturnType.getUnqualifiedType()) {
|
|
|
|
// No adjustment needed.
|
2010-02-23 04:34:28 +03:00
|
|
|
return BaseOffset();
|
2010-02-13 05:02:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const CXXRecordDecl *DerivedRD =
|
|
|
|
cast<CXXRecordDecl>(cast<RecordType>(CanDerivedReturnType)->getDecl());
|
|
|
|
|
|
|
|
const CXXRecordDecl *BaseRD =
|
|
|
|
cast<CXXRecordDecl>(cast<RecordType>(CanBaseReturnType)->getDecl());
|
|
|
|
|
2010-02-14 02:17:31 +03:00
|
|
|
return ComputeBaseOffset(Context, BaseRD, DerivedRD);
|
2010-02-13 05:02:03 +03:00
|
|
|
}
|
|
|
|
|
2010-02-12 04:40:03 +03:00
|
|
|
void FinalOverriders::PropagateOverrider(const CXXMethodDecl *OldMD,
|
2010-02-14 01:23:31 +03:00
|
|
|
BaseSubobject NewBase,
|
2010-03-10 05:33:41 +03:00
|
|
|
uint64_t OverriderOffsetInLayoutClass,
|
2010-02-12 04:40:03 +03:00
|
|
|
const CXXMethodDecl *NewMD,
|
|
|
|
SubobjectOffsetsMapTy &Offsets) {
|
|
|
|
for (CXXMethodDecl::method_iterator I = OldMD->begin_overridden_methods(),
|
|
|
|
E = OldMD->end_overridden_methods(); I != E; ++I) {
|
|
|
|
const CXXMethodDecl *OverriddenMD = *I;
|
|
|
|
const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent();
|
|
|
|
|
|
|
|
// We want to override OverriddenMD in all subobjects, for example:
|
|
|
|
//
|
|
|
|
/// struct A { virtual void f(); };
|
|
|
|
/// struct B1 : A { };
|
|
|
|
/// struct B2 : A { };
|
|
|
|
/// struct C : B1, B2 { virtual void f(); };
|
|
|
|
///
|
|
|
|
/// When overriding A::f with C::f we need to do so in both A subobjects.
|
2010-03-03 07:58:02 +03:00
|
|
|
const OffsetSetVectorTy &OffsetVector = Offsets[OverriddenRD];
|
2010-02-12 04:40:03 +03:00
|
|
|
|
|
|
|
// Go through all the subobjects.
|
|
|
|
for (unsigned I = 0, E = OffsetVector.size(); I != E; ++I) {
|
|
|
|
uint64_t Offset = OffsetVector[I];
|
|
|
|
|
2010-02-14 03:16:19 +03:00
|
|
|
BaseSubobject OverriddenSubobject = BaseSubobject(OverriddenRD, Offset);
|
2010-02-13 05:02:03 +03:00
|
|
|
BaseSubobjectMethodPairTy SubobjectAndMethod =
|
2010-02-14 03:16:19 +03:00
|
|
|
std::make_pair(OverriddenSubobject, OverriddenMD);
|
2010-02-13 05:02:03 +03:00
|
|
|
|
|
|
|
OverriderInfo &Overrider = OverridersMap[SubobjectAndMethod];
|
|
|
|
|
2010-02-12 19:55:34 +03:00
|
|
|
assert(Overrider.Method && "Did not find existing overrider!");
|
2010-02-12 04:40:03 +03:00
|
|
|
|
2010-02-14 02:17:31 +03:00
|
|
|
// Check if we need return adjustments or base adjustments.
|
2010-02-14 00:16:54 +03:00
|
|
|
// (We don't want to do this for pure virtual member functions).
|
|
|
|
if (!NewMD->isPure()) {
|
2010-02-14 02:17:31 +03:00
|
|
|
// Get the return adjustment base offset.
|
2010-02-14 00:16:54 +03:00
|
|
|
BaseOffset ReturnBaseOffset =
|
2010-02-14 02:17:31 +03:00
|
|
|
ComputeReturnAdjustmentBaseOffset(Context, NewMD, OverriddenMD);
|
|
|
|
|
2010-02-14 00:16:54 +03:00
|
|
|
if (!ReturnBaseOffset.isEmpty()) {
|
|
|
|
// Store the return adjustment base offset.
|
|
|
|
ReturnAdjustments[SubobjectAndMethod] = ReturnBaseOffset;
|
|
|
|
}
|
2010-02-12 20:13:23 +03:00
|
|
|
}
|
|
|
|
|
2010-02-12 04:40:03 +03:00
|
|
|
// Set the new overrider.
|
2010-03-10 05:33:41 +03:00
|
|
|
Overrider.Offset = OverriderOffsetInLayoutClass;
|
2010-02-12 19:55:34 +03:00
|
|
|
Overrider.Method = NewMD;
|
2010-02-12 04:40:03 +03:00
|
|
|
|
|
|
|
// And propagate it further.
|
2010-03-10 05:33:41 +03:00
|
|
|
PropagateOverrider(OverriddenMD, NewBase, OverriderOffsetInLayoutClass,
|
|
|
|
NewMD, Offsets);
|
2010-02-12 04:40:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FinalOverriders::MergeSubobjectOffsets(const SubobjectOffsetsMapTy &NewOffsets,
|
|
|
|
SubobjectOffsetsMapTy &Offsets) {
|
|
|
|
// Iterate over the new offsets.
|
|
|
|
for (SubobjectOffsetsMapTy::const_iterator I = NewOffsets.begin(),
|
|
|
|
E = NewOffsets.end(); I != E; ++I) {
|
|
|
|
const CXXRecordDecl *NewRD = I->first;
|
2010-03-03 07:58:02 +03:00
|
|
|
const OffsetSetVectorTy& NewOffsetVector = I->second;
|
2010-02-12 04:40:03 +03:00
|
|
|
|
2010-03-03 07:58:02 +03:00
|
|
|
OffsetSetVectorTy &OffsetVector = Offsets[NewRD];
|
2010-02-14 00:33:22 +03:00
|
|
|
|
2010-03-03 07:58:02 +03:00
|
|
|
// Merge the new offsets set vector into the old.
|
|
|
|
OffsetVector.insert(NewOffsetVector.begin(), NewOffsetVector.end());
|
2010-02-12 04:40:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FinalOverriders::ComputeFinalOverriders(BaseSubobject Base,
|
2010-02-17 20:48:25 +03:00
|
|
|
bool BaseSubobjectIsVisitedVBase,
|
2010-03-10 05:33:41 +03:00
|
|
|
uint64_t OffsetInLayoutClass,
|
2010-02-12 04:40:03 +03:00
|
|
|
SubobjectOffsetsMapTy &Offsets) {
|
2010-02-12 00:24:32 +03:00
|
|
|
const CXXRecordDecl *RD = Base.getBase();
|
2010-02-12 04:40:03 +03:00
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
SubobjectOffsetsMapTy NewOffsets;
|
|
|
|
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
2010-02-14 20:05:59 +03:00
|
|
|
// Ignore bases that don't have any virtual member functions.
|
|
|
|
if (!BaseDecl->isPolymorphic())
|
|
|
|
continue;
|
|
|
|
|
2010-02-26 01:18:35 +03:00
|
|
|
bool IsVisitedVirtualBase = BaseSubobjectIsVisitedVBase;
|
2010-02-14 03:37:35 +03:00
|
|
|
uint64_t BaseOffset;
|
2010-03-10 05:33:41 +03:00
|
|
|
uint64_t BaseOffsetInLayoutClass;
|
2010-02-14 03:37:35 +03:00
|
|
|
if (I->isVirtual()) {
|
2010-02-17 20:48:25 +03:00
|
|
|
if (!VisitedVirtualBases.insert(BaseDecl))
|
|
|
|
IsVisitedVirtualBase = true;
|
2010-02-14 03:37:35 +03:00
|
|
|
BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
|
2010-03-10 05:33:41 +03:00
|
|
|
|
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
|
|
|
BaseOffsetInLayoutClass =
|
|
|
|
LayoutClassLayout.getVBaseClassOffset(BaseDecl);
|
2010-02-14 03:37:35 +03:00
|
|
|
} else {
|
|
|
|
BaseOffset = Layout.getBaseClassOffset(BaseDecl) + Base.getBaseOffset();
|
2010-03-10 05:33:41 +03:00
|
|
|
BaseOffsetInLayoutClass = Layout.getBaseClassOffset(BaseDecl) +
|
|
|
|
OffsetInLayoutClass;
|
2010-02-14 03:37:35 +03:00
|
|
|
}
|
|
|
|
|
2010-02-12 04:40:03 +03:00
|
|
|
// Compute the final overriders for this base.
|
2010-02-17 20:48:25 +03:00
|
|
|
// We always want to compute the final overriders, even if the base is a
|
|
|
|
// visited virtual base. Consider:
|
|
|
|
//
|
|
|
|
// struct A {
|
|
|
|
// virtual void f();
|
|
|
|
// virtual void g();
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// struct B : virtual A {
|
|
|
|
// void f();
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// struct C : virtual A {
|
|
|
|
// void g ();
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// struct D : B, C { };
|
|
|
|
//
|
|
|
|
// Here, we still want to compute the overriders for A as a base of C,
|
|
|
|
// because otherwise we'll miss that C::g overrides A::f.
|
|
|
|
ComputeFinalOverriders(BaseSubobject(BaseDecl, BaseOffset),
|
2010-03-10 05:33:41 +03:00
|
|
|
IsVisitedVirtualBase, BaseOffsetInLayoutClass,
|
|
|
|
NewOffsets);
|
2010-02-12 04:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Now add the overriders for this particular subobject.
|
2010-02-17 20:48:25 +03:00
|
|
|
/// (We don't want to do this more than once for a virtual base).
|
|
|
|
if (!BaseSubobjectIsVisitedVBase)
|
2010-03-10 05:33:41 +03:00
|
|
|
AddOverriders(Base, OffsetInLayoutClass, NewOffsets);
|
2010-02-12 00:24:32 +03:00
|
|
|
|
2010-02-12 04:40:03 +03:00
|
|
|
// And merge the newly discovered subobject offsets.
|
|
|
|
MergeSubobjectOffsets(NewOffsets, Offsets);
|
|
|
|
|
|
|
|
/// Finally, add the offset for our own subobject.
|
2010-03-03 07:58:02 +03:00
|
|
|
Offsets[RD].insert(Base.getBaseOffset());
|
2010-02-12 00:24:32 +03:00
|
|
|
}
|
|
|
|
|
2010-02-14 03:37:35 +03:00
|
|
|
void FinalOverriders::dump(llvm::raw_ostream &Out, BaseSubobject Base) {
|
2010-02-12 00:24:32 +03:00
|
|
|
const CXXRecordDecl *RD = Base.getBase();
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
2010-02-14 20:05:59 +03:00
|
|
|
// Ignore bases that don't have any virtual member functions.
|
|
|
|
if (!BaseDecl->isPolymorphic())
|
|
|
|
continue;
|
|
|
|
|
2010-02-14 03:37:35 +03:00
|
|
|
uint64_t BaseOffset;
|
|
|
|
if (I->isVirtual()) {
|
|
|
|
if (!VisitedVirtualBases.insert(BaseDecl)) {
|
|
|
|
// We've visited this base before.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseOffset = MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
|
|
|
|
} else {
|
|
|
|
BaseOffset = Layout.getBaseClassOffset(BaseDecl) +
|
|
|
|
Base.getBaseOffset();
|
|
|
|
}
|
|
|
|
|
2010-02-12 00:24:32 +03:00
|
|
|
dump(Out, BaseSubobject(BaseDecl, BaseOffset));
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << "Final overriders for (" << RD->getQualifiedNameAsString() << ", ";
|
2010-03-10 05:33:41 +03:00
|
|
|
Out << Base.getBaseOffset() / 8 << ")\n";
|
2010-02-12 00:24:32 +03:00
|
|
|
|
|
|
|
// Now dump the overriders for this base subobject.
|
|
|
|
for (CXXRecordDecl::method_iterator I = RD->method_begin(),
|
|
|
|
E = RD->method_end(); I != E; ++I) {
|
|
|
|
const CXXMethodDecl *MD = *I;
|
|
|
|
|
|
|
|
if (!MD->isVirtual())
|
|
|
|
continue;
|
|
|
|
|
2010-02-12 19:55:34 +03:00
|
|
|
OverriderInfo Overrider = getOverrider(Base, MD);
|
2010-02-12 00:24:32 +03:00
|
|
|
|
2010-03-02 06:44:06 +03:00
|
|
|
Out << " " << MD->getQualifiedNameAsString() << " - (";
|
2010-02-12 20:13:23 +03:00
|
|
|
Out << Overrider.Method->getQualifiedNameAsString();
|
2010-03-12 08:02:01 +03:00
|
|
|
Out << ", " << ", " << Overrider.Offset / 8 << ')';
|
2010-03-02 06:44:06 +03:00
|
|
|
|
2010-02-13 12:11:28 +03:00
|
|
|
AdjustmentOffsetsMapTy::const_iterator AI =
|
2010-02-13 05:02:03 +03:00
|
|
|
ReturnAdjustments.find(std::make_pair(Base, MD));
|
2010-02-13 12:11:28 +03:00
|
|
|
if (AI != ReturnAdjustments.end()) {
|
|
|
|
const BaseOffset &Offset = AI->second;
|
2010-02-13 23:41:15 +03:00
|
|
|
|
|
|
|
Out << " [ret-adj: ";
|
|
|
|
if (Offset.VirtualBase)
|
|
|
|
Out << Offset.VirtualBase->getQualifiedNameAsString() << " vbase, ";
|
2010-02-13 05:02:03 +03:00
|
|
|
|
2010-02-13 23:41:15 +03:00
|
|
|
Out << Offset.NonVirtualOffset << " nv]";
|
2010-02-13 05:02:03 +03:00
|
|
|
}
|
2010-02-14 02:17:31 +03:00
|
|
|
|
2010-02-12 20:13:23 +03:00
|
|
|
Out << "\n";
|
2010-02-12 00:24:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
/// VTableComponent - Represents a single component in a vtable.
|
|
|
|
class VTableComponent {
|
2010-02-11 11:02:13 +03:00
|
|
|
public:
|
|
|
|
enum Kind {
|
|
|
|
CK_VCallOffset,
|
|
|
|
CK_VBaseOffset,
|
|
|
|
CK_OffsetToTop,
|
|
|
|
CK_RTTI,
|
2010-02-11 21:20:28 +03:00
|
|
|
CK_FunctionPointer,
|
|
|
|
|
|
|
|
/// CK_CompleteDtorPointer - A pointer to the complete destructor.
|
|
|
|
CK_CompleteDtorPointer,
|
|
|
|
|
|
|
|
/// CK_DeletingDtorPointer - A pointer to the deleting destructor.
|
2010-02-19 23:08:13 +03:00
|
|
|
CK_DeletingDtorPointer,
|
|
|
|
|
|
|
|
/// CK_UnusedFunctionPointer - In some cases, a vtable function pointer
|
|
|
|
/// will end up never being called. Such vtable function pointers are
|
|
|
|
/// represented as a CK_UnusedFunctionPointer.
|
|
|
|
CK_UnusedFunctionPointer
|
2010-02-11 11:02:13 +03:00
|
|
|
};
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
static VTableComponent MakeVCallOffset(int64_t Offset) {
|
|
|
|
return VTableComponent(CK_VCallOffset, Offset);
|
2010-02-17 09:07:19 +03:00
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
static VTableComponent MakeVBaseOffset(int64_t Offset) {
|
|
|
|
return VTableComponent(CK_VBaseOffset, Offset);
|
2010-02-16 07:49:44 +03:00
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
static VTableComponent MakeOffsetToTop(int64_t Offset) {
|
|
|
|
return VTableComponent(CK_OffsetToTop, Offset);
|
2010-02-11 11:02:13 +03:00
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
static VTableComponent MakeRTTI(const CXXRecordDecl *RD) {
|
|
|
|
return VTableComponent(CK_RTTI, reinterpret_cast<uintptr_t>(RD));
|
2010-02-11 11:02:13 +03:00
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
static VTableComponent MakeFunction(const CXXMethodDecl *MD) {
|
2010-02-11 11:02:13 +03:00
|
|
|
assert(!isa<CXXDestructorDecl>(MD) &&
|
2010-02-11 21:20:28 +03:00
|
|
|
"Don't use MakeFunction with destructors!");
|
2010-02-11 11:02:13 +03:00
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
return VTableComponent(CK_FunctionPointer,
|
2010-02-11 11:02:13 +03:00
|
|
|
reinterpret_cast<uintptr_t>(MD));
|
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
static VTableComponent MakeCompleteDtor(const CXXDestructorDecl *DD) {
|
|
|
|
return VTableComponent(CK_CompleteDtorPointer,
|
2010-02-11 21:20:28 +03:00
|
|
|
reinterpret_cast<uintptr_t>(DD));
|
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
static VTableComponent MakeDeletingDtor(const CXXDestructorDecl *DD) {
|
|
|
|
return VTableComponent(CK_DeletingDtorPointer,
|
2010-02-11 21:20:28 +03:00
|
|
|
reinterpret_cast<uintptr_t>(DD));
|
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
static VTableComponent MakeUnusedFunction(const CXXMethodDecl *MD) {
|
2010-02-19 23:08:13 +03:00
|
|
|
assert(!isa<CXXDestructorDecl>(MD) &&
|
|
|
|
"Don't use MakeUnusedFunction with destructors!");
|
2010-04-10 23:13:06 +04:00
|
|
|
return VTableComponent(CK_UnusedFunctionPointer,
|
2010-02-19 23:08:13 +03:00
|
|
|
reinterpret_cast<uintptr_t>(MD));
|
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
static VTableComponent getFromOpaqueInteger(uint64_t I) {
|
|
|
|
return VTableComponent(I);
|
2010-03-25 19:49:53 +03:00
|
|
|
}
|
|
|
|
|
2010-02-11 11:02:13 +03:00
|
|
|
/// getKind - Get the kind of this vtable component.
|
|
|
|
Kind getKind() const {
|
|
|
|
return (Kind)(Value & 0x7);
|
|
|
|
}
|
|
|
|
|
2010-02-17 09:07:19 +03:00
|
|
|
int64_t getVCallOffset() const {
|
|
|
|
assert(getKind() == CK_VCallOffset && "Invalid component kind!");
|
|
|
|
|
|
|
|
return getOffset();
|
|
|
|
}
|
|
|
|
|
2010-02-16 07:49:44 +03:00
|
|
|
int64_t getVBaseOffset() const {
|
|
|
|
assert(getKind() == CK_VBaseOffset && "Invalid component kind!");
|
|
|
|
|
|
|
|
return getOffset();
|
|
|
|
}
|
|
|
|
|
2010-02-11 11:02:13 +03:00
|
|
|
int64_t getOffsetToTop() const {
|
|
|
|
assert(getKind() == CK_OffsetToTop && "Invalid component kind!");
|
|
|
|
|
|
|
|
return getOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXRecordDecl *getRTTIDecl() const {
|
|
|
|
assert(getKind() == CK_RTTI && "Invalid component kind!");
|
|
|
|
|
|
|
|
return reinterpret_cast<CXXRecordDecl *>(getPointer());
|
|
|
|
}
|
|
|
|
|
|
|
|
const CXXMethodDecl *getFunctionDecl() const {
|
2010-02-11 21:20:28 +03:00
|
|
|
assert(getKind() == CK_FunctionPointer);
|
2010-02-11 11:02:13 +03:00
|
|
|
|
|
|
|
return reinterpret_cast<CXXMethodDecl *>(getPointer());
|
|
|
|
}
|
2010-02-11 21:20:28 +03:00
|
|
|
|
|
|
|
const CXXDestructorDecl *getDestructorDecl() const {
|
|
|
|
assert((getKind() == CK_CompleteDtorPointer ||
|
|
|
|
getKind() == CK_DeletingDtorPointer) && "Invalid component kind!");
|
|
|
|
|
|
|
|
return reinterpret_cast<CXXDestructorDecl *>(getPointer());
|
|
|
|
}
|
|
|
|
|
2010-02-19 23:08:13 +03:00
|
|
|
const CXXMethodDecl *getUnusedFunctionDecl() const {
|
|
|
|
assert(getKind() == CK_UnusedFunctionPointer);
|
|
|
|
|
|
|
|
return reinterpret_cast<CXXMethodDecl *>(getPointer());
|
|
|
|
}
|
|
|
|
|
2010-02-11 11:02:13 +03:00
|
|
|
private:
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableComponent(Kind ComponentKind, int64_t Offset) {
|
2010-02-11 11:02:13 +03:00
|
|
|
assert((ComponentKind == CK_VCallOffset ||
|
|
|
|
ComponentKind == CK_VBaseOffset ||
|
|
|
|
ComponentKind == CK_OffsetToTop) && "Invalid component kind!");
|
|
|
|
assert(Offset <= ((1LL << 56) - 1) && "Offset is too big!");
|
|
|
|
|
|
|
|
Value = ((Offset << 3) | ComponentKind);
|
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableComponent(Kind ComponentKind, uintptr_t Ptr) {
|
2010-02-11 11:02:13 +03:00
|
|
|
assert((ComponentKind == CK_RTTI ||
|
2010-02-11 21:20:28 +03:00
|
|
|
ComponentKind == CK_FunctionPointer ||
|
|
|
|
ComponentKind == CK_CompleteDtorPointer ||
|
2010-02-19 23:08:13 +03:00
|
|
|
ComponentKind == CK_DeletingDtorPointer ||
|
|
|
|
ComponentKind == CK_UnusedFunctionPointer) &&
|
2010-02-11 11:02:13 +03:00
|
|
|
"Invalid component kind!");
|
|
|
|
|
|
|
|
assert((Ptr & 7) == 0 && "Pointer not sufficiently aligned!");
|
|
|
|
|
|
|
|
Value = Ptr | ComponentKind;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t getOffset() const {
|
|
|
|
assert((getKind() == CK_VCallOffset || getKind() == CK_VBaseOffset ||
|
|
|
|
getKind() == CK_OffsetToTop) && "Invalid component kind!");
|
|
|
|
|
|
|
|
return Value >> 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
uintptr_t getPointer() const {
|
2010-02-11 21:20:28 +03:00
|
|
|
assert((getKind() == CK_RTTI ||
|
|
|
|
getKind() == CK_FunctionPointer ||
|
|
|
|
getKind() == CK_CompleteDtorPointer ||
|
2010-02-19 23:08:13 +03:00
|
|
|
getKind() == CK_DeletingDtorPointer ||
|
|
|
|
getKind() == CK_UnusedFunctionPointer) &&
|
2010-02-11 11:02:13 +03:00
|
|
|
"Invalid component kind!");
|
|
|
|
|
|
|
|
return static_cast<uintptr_t>(Value & ~7ULL);
|
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
explicit VTableComponent(uint64_t Value)
|
2010-03-25 19:49:53 +03:00
|
|
|
: Value(Value) { }
|
|
|
|
|
2010-02-11 11:02:13 +03:00
|
|
|
/// The kind is stored in the lower 3 bits of the value. For offsets, we
|
|
|
|
/// make use of the facts that classes can't be larger than 2^55 bytes,
|
|
|
|
/// so we store the offset in the lower part of the 61 bytes that remain.
|
|
|
|
/// (The reason that we're not simply using a PointerIntPair here is that we
|
|
|
|
/// need the offsets to be 64-bit, even when on a 32-bit machine).
|
|
|
|
int64_t Value;
|
|
|
|
};
|
|
|
|
|
2010-02-18 20:00:09 +03:00
|
|
|
/// VCallOffsetMap - Keeps track of vcall offsets when building a vtable.
|
|
|
|
struct VCallOffsetMap {
|
|
|
|
|
|
|
|
typedef std::pair<const CXXMethodDecl *, int64_t> MethodAndOffsetPairTy;
|
|
|
|
|
|
|
|
/// Offsets - Keeps track of methods and their offsets.
|
|
|
|
// FIXME: This should be a real map and not a vector.
|
|
|
|
llvm::SmallVector<MethodAndOffsetPairTy, 16> Offsets;
|
|
|
|
|
2010-02-18 20:26:40 +03:00
|
|
|
/// MethodsCanShareVCallOffset - Returns whether two virtual member functions
|
|
|
|
/// can share the same vcall offset.
|
|
|
|
static bool MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
|
|
|
|
const CXXMethodDecl *RHS);
|
|
|
|
|
2010-02-18 20:00:09 +03:00
|
|
|
public:
|
|
|
|
/// AddVCallOffset - Adds a vcall offset to the map. Returns true if the
|
|
|
|
/// add was successful, or false if there was already a member function with
|
|
|
|
/// the same signature in the map.
|
|
|
|
bool AddVCallOffset(const CXXMethodDecl *MD, int64_t OffsetOffset);
|
|
|
|
|
|
|
|
/// getVCallOffsetOffset - Returns the vcall offset offset (relative to the
|
|
|
|
/// vtable address point) for the given virtual member function.
|
|
|
|
int64_t getVCallOffsetOffset(const CXXMethodDecl *MD);
|
|
|
|
|
2010-02-26 01:23:13 +03:00
|
|
|
// empty - Return whether the offset map is empty or not.
|
|
|
|
bool empty() const { return Offsets.empty(); }
|
2010-02-18 20:00:09 +03:00
|
|
|
};
|
|
|
|
|
2010-02-26 23:22:44 +03:00
|
|
|
static bool HasSameVirtualSignature(const CXXMethodDecl *LHS,
|
|
|
|
const CXXMethodDecl *RHS) {
|
|
|
|
ASTContext &C = LHS->getASTContext(); // TODO: thread this down
|
|
|
|
CanQual<FunctionProtoType>
|
|
|
|
LT = C.getCanonicalType(LHS->getType()).getAs<FunctionProtoType>(),
|
|
|
|
RT = C.getCanonicalType(RHS->getType()).getAs<FunctionProtoType>();
|
|
|
|
|
|
|
|
// Fast-path matches in the canonical types.
|
|
|
|
if (LT == RT) return true;
|
|
|
|
|
|
|
|
// Force the signatures to match. We can't rely on the overrides
|
|
|
|
// list here because there isn't necessarily an inheritance
|
|
|
|
// relationship between the two methods.
|
|
|
|
if (LT.getQualifiers() != RT.getQualifiers() ||
|
|
|
|
LT->getNumArgs() != RT->getNumArgs())
|
|
|
|
return false;
|
|
|
|
for (unsigned I = 0, E = LT->getNumArgs(); I != E; ++I)
|
|
|
|
if (LT->getArgType(I) != RT->getArgType(I))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-18 20:26:40 +03:00
|
|
|
bool VCallOffsetMap::MethodsCanShareVCallOffset(const CXXMethodDecl *LHS,
|
|
|
|
const CXXMethodDecl *RHS) {
|
|
|
|
assert(LHS->isVirtual() && "LHS must be virtual!");
|
|
|
|
assert(RHS->isVirtual() && "LHS must be virtual!");
|
|
|
|
|
2010-02-27 07:12:52 +03:00
|
|
|
// A destructor can share a vcall offset with another destructor.
|
|
|
|
if (isa<CXXDestructorDecl>(LHS))
|
|
|
|
return isa<CXXDestructorDecl>(RHS);
|
|
|
|
|
2010-02-18 20:26:40 +03:00
|
|
|
// FIXME: We need to check more things here.
|
|
|
|
|
2010-02-27 07:12:52 +03:00
|
|
|
// The methods must have the same name.
|
2010-02-18 20:26:40 +03:00
|
|
|
DeclarationName LHSName = LHS->getDeclName();
|
|
|
|
DeclarationName RHSName = RHS->getDeclName();
|
2010-02-26 23:22:44 +03:00
|
|
|
if (LHSName != RHSName)
|
2010-02-18 20:26:40 +03:00
|
|
|
return false;
|
2010-02-26 23:22:44 +03:00
|
|
|
|
2010-02-27 07:12:52 +03:00
|
|
|
// And the same signatures.
|
|
|
|
return HasSameVirtualSignature(LHS, RHS);
|
2010-02-18 20:26:40 +03:00
|
|
|
}
|
|
|
|
|
2010-02-18 20:00:09 +03:00
|
|
|
bool VCallOffsetMap::AddVCallOffset(const CXXMethodDecl *MD,
|
|
|
|
int64_t OffsetOffset) {
|
2010-02-18 20:26:40 +03:00
|
|
|
// Check if we can reuse an offset.
|
|
|
|
for (unsigned I = 0, E = Offsets.size(); I != E; ++I) {
|
|
|
|
if (MethodsCanShareVCallOffset(Offsets[I].first, MD))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the offset.
|
|
|
|
Offsets.push_back(MethodAndOffsetPairTy(MD, OffsetOffset));
|
2010-02-18 20:00:09 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t VCallOffsetMap::getVCallOffsetOffset(const CXXMethodDecl *MD) {
|
2010-02-18 20:26:40 +03:00
|
|
|
// Look for an offset.
|
|
|
|
for (unsigned I = 0, E = Offsets.size(); I != E; ++I) {
|
|
|
|
if (MethodsCanShareVCallOffset(Offsets[I].first, MD))
|
|
|
|
return Offsets[I].second;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(false && "Should always find a vcall offset offset!");
|
2010-02-18 20:00:09 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-25 06:45:56 +03:00
|
|
|
/// VCallAndVBaseOffsetBuilder - Class for building vcall and vbase offsets.
|
|
|
|
class VCallAndVBaseOffsetBuilder {
|
2010-03-11 19:06:20 +03:00
|
|
|
public:
|
|
|
|
typedef llvm::DenseMap<const CXXRecordDecl *, int64_t>
|
|
|
|
VBaseOffsetOffsetsMapTy;
|
|
|
|
|
|
|
|
private:
|
2010-02-28 04:43:58 +03:00
|
|
|
/// MostDerivedClass - The most derived class for which we're building vcall
|
|
|
|
/// and vbase offsets.
|
2010-02-25 06:45:56 +03:00
|
|
|
const CXXRecordDecl *MostDerivedClass;
|
|
|
|
|
2010-02-28 00:09:00 +03:00
|
|
|
/// LayoutClass - The class we're using for layout information. Will be
|
|
|
|
/// different than the most derived class if we're building a construction
|
|
|
|
/// vtable.
|
|
|
|
const CXXRecordDecl *LayoutClass;
|
|
|
|
|
2010-02-25 06:45:56 +03:00
|
|
|
/// Context - The ASTContext which we will use for layout information.
|
|
|
|
ASTContext &Context;
|
|
|
|
|
|
|
|
/// Components - vcall and vbase offset components
|
2010-04-10 23:13:06 +04:00
|
|
|
typedef llvm::SmallVector<VTableComponent, 64> VTableComponentVectorTy;
|
|
|
|
VTableComponentVectorTy Components;
|
2010-02-25 06:45:56 +03:00
|
|
|
|
|
|
|
/// VisitedVirtualBases - Visited virtual bases.
|
|
|
|
llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBases;
|
|
|
|
|
|
|
|
/// VCallOffsets - Keeps track of vcall offsets.
|
|
|
|
VCallOffsetMap VCallOffsets;
|
|
|
|
|
2010-03-11 09:43:12 +03:00
|
|
|
|
|
|
|
/// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets,
|
|
|
|
/// relative to the address point.
|
|
|
|
VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
|
|
|
|
|
2010-02-25 06:45:56 +03:00
|
|
|
/// FinalOverriders - The final overriders of the most derived class.
|
|
|
|
/// (Can be null when we're not building a vtable of the most derived class).
|
|
|
|
const FinalOverriders *Overriders;
|
|
|
|
|
|
|
|
/// AddVCallAndVBaseOffsets - Add vcall offsets and vbase offsets for the
|
|
|
|
/// given base subobject.
|
|
|
|
void AddVCallAndVBaseOffsets(BaseSubobject Base, bool BaseIsVirtual,
|
|
|
|
uint64_t RealBaseOffset);
|
|
|
|
|
|
|
|
/// AddVCallOffsets - Add vcall offsets for the given base subobject.
|
|
|
|
void AddVCallOffsets(BaseSubobject Base, uint64_t VBaseOffset);
|
|
|
|
|
|
|
|
/// AddVBaseOffsets - Add vbase offsets for the given class.
|
2010-03-02 08:40:45 +03:00
|
|
|
void AddVBaseOffsets(const CXXRecordDecl *Base, uint64_t OffsetInLayoutClass);
|
2010-02-25 06:45:56 +03:00
|
|
|
|
2010-03-11 09:43:12 +03:00
|
|
|
/// getCurrentOffsetOffset - Get the current vcall or vbase offset offset in
|
|
|
|
/// bytes, relative to the vtable address point.
|
|
|
|
int64_t getCurrentOffsetOffset() const;
|
|
|
|
|
2010-02-25 06:45:56 +03:00
|
|
|
public:
|
|
|
|
VCallAndVBaseOffsetBuilder(const CXXRecordDecl *MostDerivedClass,
|
2010-02-28 00:09:00 +03:00
|
|
|
const CXXRecordDecl *LayoutClass,
|
2010-02-25 06:45:56 +03:00
|
|
|
const FinalOverriders *Overriders,
|
2010-02-28 04:43:58 +03:00
|
|
|
BaseSubobject Base, bool BaseIsVirtual,
|
|
|
|
uint64_t OffsetInLayoutClass)
|
|
|
|
: MostDerivedClass(MostDerivedClass), LayoutClass(LayoutClass),
|
2010-02-25 06:45:56 +03:00
|
|
|
Context(MostDerivedClass->getASTContext()), Overriders(Overriders) {
|
|
|
|
|
|
|
|
// Add vcall and vbase offsets.
|
2010-02-28 04:43:58 +03:00
|
|
|
AddVCallAndVBaseOffsets(Base, BaseIsVirtual, OffsetInLayoutClass);
|
2010-02-25 06:45:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Methods for iterating over the components.
|
2010-04-10 23:13:06 +04:00
|
|
|
typedef VTableComponentVectorTy::const_reverse_iterator const_iterator;
|
2010-02-25 06:45:56 +03:00
|
|
|
const_iterator components_begin() const { return Components.rbegin(); }
|
|
|
|
const_iterator components_end() const { return Components.rend(); }
|
|
|
|
|
2010-03-11 19:06:20 +03:00
|
|
|
const VCallOffsetMap &getVCallOffsets() const { return VCallOffsets; }
|
|
|
|
const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
|
2010-03-11 09:43:12 +03:00
|
|
|
return VBaseOffsetOffsets;
|
|
|
|
}
|
2010-02-25 06:45:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
VCallAndVBaseOffsetBuilder::AddVCallAndVBaseOffsets(BaseSubobject Base,
|
|
|
|
bool BaseIsVirtual,
|
|
|
|
uint64_t RealBaseOffset) {
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(Base.getBase());
|
|
|
|
|
|
|
|
// Itanium C++ ABI 2.5.2:
|
|
|
|
// ..in classes sharing a virtual table with a primary base class, the vcall
|
|
|
|
// and vbase offsets added by the derived class all come before the vcall
|
|
|
|
// and vbase offsets required by the base class, so that the latter may be
|
|
|
|
// laid out as required by the base class without regard to additions from
|
|
|
|
// the derived class(es).
|
|
|
|
|
|
|
|
// (Since we're emitting the vcall and vbase offsets in reverse order, we'll
|
|
|
|
// emit them for the primary base first).
|
|
|
|
if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
|
|
|
|
bool PrimaryBaseIsVirtual = Layout.getPrimaryBaseWasVirtual();
|
|
|
|
|
|
|
|
uint64_t PrimaryBaseOffset;
|
|
|
|
|
|
|
|
// Get the base offset of the primary base.
|
|
|
|
if (PrimaryBaseIsVirtual) {
|
|
|
|
assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 &&
|
|
|
|
"Primary vbase should have a zero offset!");
|
|
|
|
|
|
|
|
const ASTRecordLayout &MostDerivedClassLayout =
|
|
|
|
Context.getASTRecordLayout(MostDerivedClass);
|
|
|
|
|
|
|
|
PrimaryBaseOffset =
|
|
|
|
MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
|
|
|
|
} else {
|
|
|
|
assert(Layout.getBaseClassOffset(PrimaryBase) == 0 &&
|
|
|
|
"Primary base should have a zero offset!");
|
|
|
|
|
|
|
|
PrimaryBaseOffset = Base.getBaseOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
AddVCallAndVBaseOffsets(BaseSubobject(PrimaryBase, PrimaryBaseOffset),
|
|
|
|
PrimaryBaseIsVirtual, RealBaseOffset);
|
|
|
|
}
|
|
|
|
|
2010-03-02 08:40:45 +03:00
|
|
|
AddVBaseOffsets(Base.getBase(), RealBaseOffset);
|
2010-02-25 06:45:56 +03:00
|
|
|
|
|
|
|
// We only want to add vcall offsets for virtual bases.
|
|
|
|
if (BaseIsVirtual)
|
|
|
|
AddVCallOffsets(Base, RealBaseOffset);
|
|
|
|
}
|
|
|
|
|
2010-03-11 09:43:12 +03:00
|
|
|
int64_t VCallAndVBaseOffsetBuilder::getCurrentOffsetOffset() const {
|
|
|
|
// OffsetIndex is the index of this vcall or vbase offset, relative to the
|
|
|
|
// vtable address point. (We subtract 3 to account for the information just
|
|
|
|
// above the address point, the RTTI info, the offset to top, and the
|
|
|
|
// vcall offset itself).
|
|
|
|
int64_t OffsetIndex = -(int64_t)(3 + Components.size());
|
|
|
|
|
|
|
|
// FIXME: We shouldn't use / 8 here.
|
|
|
|
int64_t OffsetOffset = OffsetIndex *
|
|
|
|
(int64_t)Context.Target.getPointerWidth(0) / 8;
|
|
|
|
|
|
|
|
return OffsetOffset;
|
|
|
|
}
|
|
|
|
|
2010-02-25 06:45:56 +03:00
|
|
|
void VCallAndVBaseOffsetBuilder::AddVCallOffsets(BaseSubobject Base,
|
|
|
|
uint64_t VBaseOffset) {
|
|
|
|
const CXXRecordDecl *RD = Base.getBase();
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
|
2010-02-26 23:22:44 +03:00
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
|
|
|
|
2010-02-25 06:45:56 +03:00
|
|
|
// Handle the primary base first.
|
2010-02-26 23:22:44 +03:00
|
|
|
if (PrimaryBase) {
|
2010-02-25 06:45:56 +03:00
|
|
|
uint64_t PrimaryBaseOffset;
|
|
|
|
|
|
|
|
// Get the base offset of the primary base.
|
|
|
|
if (Layout.getPrimaryBaseWasVirtual()) {
|
|
|
|
assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 &&
|
|
|
|
"Primary vbase should have a zero offset!");
|
|
|
|
|
|
|
|
const ASTRecordLayout &MostDerivedClassLayout =
|
|
|
|
Context.getASTRecordLayout(MostDerivedClass);
|
|
|
|
|
|
|
|
PrimaryBaseOffset =
|
|
|
|
MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
|
|
|
|
} else {
|
|
|
|
assert(Layout.getBaseClassOffset(PrimaryBase) == 0 &&
|
|
|
|
"Primary base should have a zero offset!");
|
|
|
|
|
|
|
|
PrimaryBaseOffset = Base.getBaseOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
AddVCallOffsets(BaseSubobject(PrimaryBase, PrimaryBaseOffset),
|
|
|
|
VBaseOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the vcall offsets.
|
|
|
|
for (CXXRecordDecl::method_iterator I = RD->method_begin(),
|
|
|
|
E = RD->method_end(); I != E; ++I) {
|
|
|
|
const CXXMethodDecl *MD = *I;
|
|
|
|
|
|
|
|
if (!MD->isVirtual())
|
|
|
|
continue;
|
|
|
|
|
2010-03-11 09:43:12 +03:00
|
|
|
int64_t OffsetOffset = getCurrentOffsetOffset();
|
2010-02-25 06:45:56 +03:00
|
|
|
|
|
|
|
// Don't add a vcall offset if we already have one for this member function
|
|
|
|
// signature.
|
|
|
|
if (!VCallOffsets.AddVCallOffset(MD, OffsetOffset))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int64_t Offset = 0;
|
|
|
|
|
|
|
|
if (Overriders) {
|
|
|
|
// Get the final overrider.
|
|
|
|
FinalOverriders::OverriderInfo Overrider =
|
|
|
|
Overriders->getOverrider(Base, MD);
|
|
|
|
|
2010-02-28 03:10:58 +03:00
|
|
|
/// The vcall offset is the offset from the virtual base to the object
|
|
|
|
/// where the function was overridden.
|
2010-02-25 06:45:56 +03:00
|
|
|
// FIXME: We should not use / 8 here.
|
2010-03-02 06:44:06 +03:00
|
|
|
Offset = (int64_t)(Overrider.Offset - VBaseOffset) / 8;
|
2010-02-25 06:45:56 +03:00
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
Components.push_back(VTableComponent::MakeVCallOffset(Offset));
|
2010-02-25 06:45:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// And iterate over all non-virtual bases (ignoring the primary base).
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
|
|
|
|
if (I->isVirtual())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
2010-02-26 23:22:44 +03:00
|
|
|
if (BaseDecl == PrimaryBase)
|
|
|
|
continue;
|
2010-02-25 06:45:56 +03:00
|
|
|
|
|
|
|
// Get the base offset of this base.
|
|
|
|
uint64_t BaseOffset = Base.getBaseOffset() +
|
|
|
|
Layout.getBaseClassOffset(BaseDecl);
|
|
|
|
|
|
|
|
AddVCallOffsets(BaseSubobject(BaseDecl, BaseOffset), VBaseOffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VCallAndVBaseOffsetBuilder::AddVBaseOffsets(const CXXRecordDecl *RD,
|
2010-03-02 08:40:45 +03:00
|
|
|
uint64_t OffsetInLayoutClass) {
|
2010-02-28 00:09:00 +03:00
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
2010-02-25 06:45:56 +03:00
|
|
|
|
|
|
|
// Add vbase offsets.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
// Check if this is a virtual base that we haven't visited before.
|
|
|
|
if (I->isVirtual() && VisitedVirtualBases.insert(BaseDecl)) {
|
|
|
|
// FIXME: We shouldn't use / 8 here.
|
2010-03-02 08:40:45 +03:00
|
|
|
int64_t Offset =
|
|
|
|
(int64_t)(LayoutClassLayout.getVBaseClassOffset(BaseDecl) -
|
|
|
|
OffsetInLayoutClass) / 8;
|
2010-03-11 09:43:12 +03:00
|
|
|
|
|
|
|
// Add the vbase offset offset.
|
|
|
|
assert(!VBaseOffsetOffsets.count(BaseDecl) &&
|
|
|
|
"vbase offset offset already exists!");
|
|
|
|
|
|
|
|
int64_t VBaseOffsetOffset = getCurrentOffsetOffset();
|
|
|
|
VBaseOffsetOffsets.insert(std::make_pair(BaseDecl, VBaseOffsetOffset));
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
Components.push_back(VTableComponent::MakeVBaseOffset(Offset));
|
2010-02-25 06:45:56 +03:00
|
|
|
}
|
2010-03-11 09:43:12 +03:00
|
|
|
|
2010-02-25 06:45:56 +03:00
|
|
|
// Check the base class looking for more vbase offsets.
|
2010-03-02 08:40:45 +03:00
|
|
|
AddVBaseOffsets(BaseDecl, OffsetInLayoutClass);
|
2010-02-25 06:45:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
/// VTableBuilder - Class for building vtable layout information.
|
|
|
|
class VTableBuilder {
|
2010-02-12 08:25:12 +03:00
|
|
|
public:
|
2010-02-27 19:18:19 +03:00
|
|
|
/// PrimaryBasesSetVectorTy - A set vector of direct and indirect
|
|
|
|
/// primary bases.
|
|
|
|
typedef llvm::SmallSetVector<const CXXRecordDecl *, 8>
|
|
|
|
PrimaryBasesSetVectorTy;
|
|
|
|
|
2010-03-24 19:42:11 +03:00
|
|
|
typedef llvm::DenseMap<const CXXRecordDecl *, int64_t>
|
|
|
|
VBaseOffsetOffsetsMapTy;
|
2010-03-25 03:51:13 +03:00
|
|
|
|
|
|
|
typedef llvm::DenseMap<BaseSubobject, uint64_t>
|
|
|
|
AddressPointsMapTy;
|
2010-03-24 19:42:11 +03:00
|
|
|
|
2010-02-12 08:25:12 +03:00
|
|
|
private:
|
2010-03-23 07:11:45 +03:00
|
|
|
/// VTables - Global vtable information.
|
|
|
|
CodeGenVTables &VTables;
|
2010-02-14 00:07:32 +03:00
|
|
|
|
2010-02-11 11:02:13 +03:00
|
|
|
/// MostDerivedClass - The most derived class for which we're building this
|
|
|
|
/// vtable.
|
|
|
|
const CXXRecordDecl *MostDerivedClass;
|
|
|
|
|
2010-02-27 23:39:05 +03:00
|
|
|
/// MostDerivedClassOffset - If we're building a construction vtable, this
|
|
|
|
/// holds the offset from the layout class to the most derived class.
|
|
|
|
const uint64_t MostDerivedClassOffset;
|
|
|
|
|
2010-02-28 03:36:23 +03:00
|
|
|
/// MostDerivedClassIsVirtual - Whether the most derived class is a virtual
|
|
|
|
/// base. (This only makes sense when building a construction vtable).
|
|
|
|
bool MostDerivedClassIsVirtual;
|
|
|
|
|
2010-02-27 23:39:05 +03:00
|
|
|
/// LayoutClass - The class we're using for layout information. Will be
|
|
|
|
/// different than the most derived class if we're building a construction
|
|
|
|
/// vtable.
|
|
|
|
const CXXRecordDecl *LayoutClass;
|
|
|
|
|
2010-02-11 11:02:13 +03:00
|
|
|
/// Context - The ASTContext which we will use for layout information.
|
2010-02-12 08:25:12 +03:00
|
|
|
ASTContext &Context;
|
2010-02-11 11:02:13 +03:00
|
|
|
|
2010-02-12 00:24:32 +03:00
|
|
|
/// FinalOverriders - The final overriders of the most derived class.
|
2010-02-25 06:45:56 +03:00
|
|
|
const FinalOverriders Overriders;
|
2010-02-12 00:24:32 +03:00
|
|
|
|
2010-02-26 01:23:13 +03:00
|
|
|
/// VCallOffsetsForVBases - Keeps track of vcall offsets for the virtual
|
|
|
|
/// bases in this vtable.
|
|
|
|
llvm::DenseMap<const CXXRecordDecl *, VCallOffsetMap> VCallOffsetsForVBases;
|
2010-02-18 20:00:09 +03:00
|
|
|
|
2010-03-11 09:43:12 +03:00
|
|
|
/// VBaseOffsetOffsets - Contains the offsets of the virtual base offsets for
|
|
|
|
/// the most derived class.
|
|
|
|
VBaseOffsetOffsetsMapTy VBaseOffsetOffsets;
|
|
|
|
|
2010-02-11 11:02:13 +03:00
|
|
|
/// Components - The components of the vtable being built.
|
2010-04-10 23:13:06 +04:00
|
|
|
llvm::SmallVector<VTableComponent, 64> Components;
|
2010-02-11 11:02:13 +03:00
|
|
|
|
2010-02-11 20:18:51 +03:00
|
|
|
/// AddressPoints - Address points for the vtable being built.
|
2010-03-25 03:35:49 +03:00
|
|
|
AddressPointsMapTy AddressPoints;
|
2010-02-11 20:18:51 +03:00
|
|
|
|
2010-02-27 19:18:19 +03:00
|
|
|
/// MethodInfo - Contains information about a method in a vtable.
|
|
|
|
/// (Used for computing 'this' pointer adjustment thunks.
|
|
|
|
struct MethodInfo {
|
|
|
|
/// BaseOffset - The base offset of this method.
|
|
|
|
const uint64_t BaseOffset;
|
|
|
|
|
2010-03-11 00:25:37 +03:00
|
|
|
/// BaseOffsetInLayoutClass - The base offset in the layout class of this
|
|
|
|
/// method.
|
|
|
|
const uint64_t BaseOffsetInLayoutClass;
|
|
|
|
|
2010-02-27 19:18:19 +03:00
|
|
|
/// VtableIndex - The index in the vtable that this method has.
|
|
|
|
/// (For destructors, this is the index of the complete destructor).
|
|
|
|
const uint64_t VtableIndex;
|
|
|
|
|
2010-03-11 00:25:37 +03:00
|
|
|
MethodInfo(uint64_t BaseOffset, uint64_t BaseOffsetInLayoutClass,
|
|
|
|
uint64_t VtableIndex)
|
|
|
|
: BaseOffset(BaseOffset),
|
|
|
|
BaseOffsetInLayoutClass(BaseOffsetInLayoutClass),
|
|
|
|
VtableIndex(VtableIndex) { }
|
2010-02-27 19:52:49 +03:00
|
|
|
|
2010-03-11 00:25:37 +03:00
|
|
|
MethodInfo() : BaseOffset(0), BaseOffsetInLayoutClass(0), VtableIndex(0) { }
|
2010-02-27 19:18:19 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<const CXXMethodDecl *, MethodInfo> MethodInfoMapTy;
|
|
|
|
|
|
|
|
/// MethodInfoMap - The information for all methods in the vtable we're
|
|
|
|
/// currently building.
|
|
|
|
MethodInfoMapTy MethodInfoMap;
|
|
|
|
|
2010-03-23 07:59:02 +03:00
|
|
|
typedef llvm::DenseMap<uint64_t, ThunkInfo> VtableThunksMapTy;
|
2010-03-12 07:54:20 +03:00
|
|
|
|
2010-03-23 07:44:10 +03:00
|
|
|
/// VTableThunks - The thunks by vtable index in the vtable currently being
|
|
|
|
/// built.
|
2010-03-23 07:59:02 +03:00
|
|
|
VtableThunksMapTy VTableThunks;
|
2010-02-27 19:18:19 +03:00
|
|
|
|
2010-03-23 07:59:02 +03:00
|
|
|
typedef llvm::SmallVector<ThunkInfo, 1> ThunkInfoVectorTy;
|
|
|
|
typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy;
|
2010-03-17 23:06:32 +03:00
|
|
|
|
2010-03-23 07:44:10 +03:00
|
|
|
/// Thunks - A map that contains all the thunks needed for all methods in the
|
|
|
|
/// most derived class for which the vtable is currently being built.
|
2010-03-23 07:59:02 +03:00
|
|
|
ThunksMapTy Thunks;
|
2010-03-17 23:06:32 +03:00
|
|
|
|
|
|
|
/// AddThunk - Add a thunk for the given method.
|
2010-03-21 23:27:14 +03:00
|
|
|
void AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk);
|
2010-03-17 23:06:32 +03:00
|
|
|
|
2010-02-27 19:52:49 +03:00
|
|
|
/// ComputeThisAdjustments - Compute the 'this' pointer adjustments for the
|
|
|
|
/// part of the vtable we're currently building.
|
|
|
|
void ComputeThisAdjustments();
|
|
|
|
|
2010-02-16 07:49:44 +03:00
|
|
|
typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
|
|
|
|
|
2010-02-27 19:18:19 +03:00
|
|
|
/// PrimaryVirtualBases - All known virtual bases who are a primary base of
|
2010-02-23 06:48:14 +03:00
|
|
|
/// some other base.
|
|
|
|
VisitedVirtualBasesSetTy PrimaryVirtualBases;
|
|
|
|
|
2010-02-14 00:07:32 +03:00
|
|
|
/// ComputeReturnAdjustment - Compute the return adjustment given a return
|
2010-02-13 23:11:51 +03:00
|
|
|
/// adjustment base offset.
|
2010-02-23 04:34:28 +03:00
|
|
|
ReturnAdjustment ComputeReturnAdjustment(BaseOffset Offset);
|
2010-02-13 23:11:51 +03:00
|
|
|
|
2010-02-27 22:57:44 +03:00
|
|
|
/// ComputeThisAdjustmentBaseOffset - Compute the base offset for adjusting
|
|
|
|
/// the 'this' pointer from the base subobject to the derived subobject.
|
|
|
|
BaseOffset ComputeThisAdjustmentBaseOffset(BaseSubobject Base,
|
|
|
|
BaseSubobject Derived) const;
|
|
|
|
|
2010-02-18 20:00:09 +03:00
|
|
|
/// ComputeThisAdjustment - Compute the 'this' pointer adjustment for the
|
2010-03-12 08:28:07 +03:00
|
|
|
/// given virtual member function, its offset in the layout class and its
|
|
|
|
/// final overrider.
|
|
|
|
ThisAdjustment
|
|
|
|
ComputeThisAdjustment(const CXXMethodDecl *MD,
|
|
|
|
uint64_t BaseOffsetInLayoutClass,
|
|
|
|
FinalOverriders::OverriderInfo Overrider);
|
|
|
|
|
2010-02-13 23:11:51 +03:00
|
|
|
/// AddMethod - Add a single virtual member function to the vtable
|
|
|
|
/// components vector.
|
2010-02-27 19:52:49 +03:00
|
|
|
void AddMethod(const CXXMethodDecl *MD, ReturnAdjustment ReturnAdjustment);
|
2010-02-13 23:11:51 +03:00
|
|
|
|
2010-02-23 07:26:39 +03:00
|
|
|
/// IsOverriderUsed - Returns whether the overrider will ever be used in this
|
|
|
|
/// part of the vtable.
|
|
|
|
///
|
|
|
|
/// Itanium C++ ABI 2.5.2:
|
|
|
|
///
|
|
|
|
/// struct A { virtual void f(); };
|
|
|
|
/// struct B : virtual public A { int i; };
|
|
|
|
/// struct C : virtual public A { int j; };
|
|
|
|
/// struct D : public B, public C {};
|
|
|
|
///
|
|
|
|
/// When B and C are declared, A is a primary base in each case, so although
|
|
|
|
/// vcall offsets are allocated in the A-in-B and A-in-C vtables, no this
|
|
|
|
/// adjustment is required and no thunk is generated. However, inside D
|
|
|
|
/// objects, A is no longer a primary base of C, so if we allowed calls to
|
|
|
|
/// C::f() to use the copy of A's vtable in the C subobject, we would need
|
|
|
|
/// to adjust this from C* to B::A*, which would require a third-party
|
|
|
|
/// thunk. Since we require that a call to C::f() first convert to A*,
|
|
|
|
/// C-in-D's copy of A's vtable is never referenced, so this is not
|
|
|
|
/// necessary.
|
2010-03-10 09:51:42 +03:00
|
|
|
bool IsOverriderUsed(const CXXMethodDecl *Overrider,
|
|
|
|
uint64_t BaseOffsetInLayoutClass,
|
|
|
|
const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
|
|
|
|
uint64_t FirstBaseOffsetInLayoutClass) const;
|
|
|
|
|
2010-02-23 07:26:39 +03:00
|
|
|
|
2010-02-13 23:11:51 +03:00
|
|
|
/// AddMethods - Add the methods of this base subobject and all its
|
|
|
|
/// primary bases to the vtable components vector.
|
2010-03-10 09:51:42 +03:00
|
|
|
void AddMethods(BaseSubobject Base, uint64_t BaseOffsetInLayoutClass,
|
|
|
|
const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
|
|
|
|
uint64_t FirstBaseOffsetInLayoutClass,
|
2010-02-27 19:18:19 +03:00
|
|
|
PrimaryBasesSetVectorTy &PrimaryBases);
|
2010-02-16 19:26:28 +03:00
|
|
|
|
2010-02-28 03:36:23 +03:00
|
|
|
// LayoutVtable - Layout the vtable for the given base class, including its
|
2010-02-16 19:26:28 +03:00
|
|
|
// secondary vtables and any vtables for virtual bases.
|
|
|
|
void LayoutVtable();
|
|
|
|
|
2010-02-28 04:43:58 +03:00
|
|
|
/// LayoutPrimaryAndSecondaryVtables - Layout the primary vtable for the
|
2010-02-16 19:26:28 +03:00
|
|
|
/// given base subobject, as well as all its secondary vtables.
|
2010-02-28 04:43:58 +03:00
|
|
|
void LayoutPrimaryAndSecondaryVtables(BaseSubobject Base,
|
|
|
|
bool BaseIsVirtual,
|
|
|
|
uint64_t OffsetInLayoutClass);
|
2010-02-11 11:02:13 +03:00
|
|
|
|
2010-02-16 19:02:57 +03:00
|
|
|
/// LayoutSecondaryVtables - Layout the secondary vtables for the given base
|
|
|
|
/// subobject.
|
2010-03-10 22:39:11 +03:00
|
|
|
///
|
|
|
|
/// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
|
|
|
|
/// or a direct or indirect base of a virtual base.
|
|
|
|
void LayoutSecondaryVtables(BaseSubobject Base, bool BaseIsMorallyVirtual,
|
|
|
|
uint64_t OffsetInLayoutClass);
|
2010-02-27 07:05:52 +03:00
|
|
|
|
|
|
|
/// DeterminePrimaryVirtualBases - Determine the primary virtual bases in this
|
|
|
|
/// class hierarchy.
|
|
|
|
void DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
|
2010-03-10 06:02:01 +03:00
|
|
|
uint64_t OffsetInLayoutClass,
|
2010-02-27 07:05:52 +03:00
|
|
|
VisitedVirtualBasesSetTy &VBases);
|
|
|
|
|
2010-02-16 19:49:35 +03:00
|
|
|
/// LayoutVtablesForVirtualBases - Layout vtables for all virtual bases of the
|
|
|
|
/// given base (excluding any primary bases).
|
|
|
|
void LayoutVtablesForVirtualBases(const CXXRecordDecl *RD,
|
|
|
|
VisitedVirtualBasesSetTy &VBases);
|
|
|
|
|
2010-02-28 03:10:58 +03:00
|
|
|
/// isBuildingConstructionVtable - Return whether this vtable builder is
|
|
|
|
/// building a construction vtable.
|
|
|
|
bool isBuildingConstructorVtable() const {
|
|
|
|
return MostDerivedClass != LayoutClass;
|
|
|
|
}
|
|
|
|
|
2010-02-11 11:02:13 +03:00
|
|
|
public:
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableBuilder(CodeGenVTables &VTables, const CXXRecordDecl *MostDerivedClass,
|
2010-02-28 03:36:23 +03:00
|
|
|
uint64_t MostDerivedClassOffset, bool MostDerivedClassIsVirtual,
|
2010-02-27 23:39:05 +03:00
|
|
|
const CXXRecordDecl *LayoutClass)
|
2010-03-23 07:11:45 +03:00
|
|
|
: VTables(VTables), MostDerivedClass(MostDerivedClass),
|
2010-02-28 03:36:23 +03:00
|
|
|
MostDerivedClassOffset(MostDerivedClassOffset),
|
|
|
|
MostDerivedClassIsVirtual(MostDerivedClassIsVirtual),
|
|
|
|
LayoutClass(LayoutClass), Context(MostDerivedClass->getASTContext()),
|
2010-03-10 05:33:41 +03:00
|
|
|
Overriders(MostDerivedClass, MostDerivedClassOffset, LayoutClass) {
|
2010-02-11 11:02:13 +03:00
|
|
|
|
2010-02-16 19:26:28 +03:00
|
|
|
LayoutVtable();
|
2010-02-11 11:02:13 +03:00
|
|
|
}
|
|
|
|
|
2010-03-23 19:36:50 +03:00
|
|
|
ThunksMapTy::const_iterator thunks_begin() const {
|
|
|
|
return Thunks.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
ThunksMapTy::const_iterator thunks_end() const {
|
|
|
|
return Thunks.end();
|
|
|
|
}
|
|
|
|
|
2010-03-24 19:42:11 +03:00
|
|
|
const VBaseOffsetOffsetsMapTy &getVBaseOffsetOffsets() const {
|
|
|
|
return VBaseOffsetOffsets;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getNumVTableComponents - Return the number of components in the vtable
|
|
|
|
/// currently built.
|
|
|
|
uint64_t getNumVTableComponents() const {
|
|
|
|
return Components.size();
|
|
|
|
}
|
|
|
|
|
2010-03-25 03:51:13 +03:00
|
|
|
const uint64_t *vtable_components_data_begin() const {
|
2010-03-24 19:42:11 +03:00
|
|
|
return reinterpret_cast<const uint64_t *>(Components.begin());
|
|
|
|
}
|
|
|
|
|
2010-03-25 03:51:13 +03:00
|
|
|
const uint64_t *vtable_components_data_end() const {
|
2010-03-24 19:42:11 +03:00
|
|
|
return reinterpret_cast<const uint64_t *>(Components.end());
|
|
|
|
}
|
|
|
|
|
2010-03-25 03:51:13 +03:00
|
|
|
AddressPointsMapTy::const_iterator address_points_begin() const {
|
|
|
|
return AddressPoints.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
AddressPointsMapTy::const_iterator address_points_end() const {
|
|
|
|
return AddressPoints.end();
|
|
|
|
}
|
|
|
|
|
2010-03-25 18:26:28 +03:00
|
|
|
VtableThunksMapTy::const_iterator vtable_thunks_begin() const {
|
|
|
|
return VTableThunks.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
VtableThunksMapTy::const_iterator vtable_thunks_end() const {
|
|
|
|
return VTableThunks.end();
|
|
|
|
}
|
|
|
|
|
2010-02-11 11:02:13 +03:00
|
|
|
/// dumpLayout - Dump the vtable layout.
|
|
|
|
void dumpLayout(llvm::raw_ostream&);
|
|
|
|
};
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
void VTableBuilder::AddThunk(const CXXMethodDecl *MD, const ThunkInfo &Thunk) {
|
2010-03-22 23:06:40 +03:00
|
|
|
assert(!isBuildingConstructorVtable() &&
|
|
|
|
"Can't add thunks for construction vtable");
|
2010-03-18 05:44:19 +03:00
|
|
|
|
2010-03-23 07:44:10 +03:00
|
|
|
llvm::SmallVector<ThunkInfo, 1> &ThunksVector = Thunks[MD];
|
2010-03-17 23:06:32 +03:00
|
|
|
|
|
|
|
// Check if we have this thunk already.
|
|
|
|
if (std::find(ThunksVector.begin(), ThunksVector.end(), Thunk) !=
|
|
|
|
ThunksVector.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ThunksVector.push_back(Thunk);
|
|
|
|
}
|
|
|
|
|
2010-02-27 09:38:03 +03:00
|
|
|
/// OverridesMethodInBases - Checks whether whether this virtual member
|
|
|
|
/// function overrides a member function in any of the given bases.
|
2010-02-12 08:25:12 +03:00
|
|
|
/// Returns the overridden member function, or null if none was found.
|
|
|
|
static const CXXMethodDecl *
|
2010-02-27 09:38:03 +03:00
|
|
|
OverridesMethodInBases(const CXXMethodDecl *MD,
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableBuilder::PrimaryBasesSetVectorTy &Bases) {
|
2010-02-12 08:25:12 +03:00
|
|
|
for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
|
|
|
|
E = MD->end_overridden_methods(); I != E; ++I) {
|
|
|
|
const CXXMethodDecl *OverriddenMD = *I;
|
|
|
|
const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent();
|
|
|
|
assert(OverriddenMD->isCanonicalDecl() &&
|
|
|
|
"Should have the canonical decl of the overridden RD!");
|
|
|
|
|
2010-02-27 09:38:03 +03:00
|
|
|
if (Bases.count(OverriddenRD))
|
2010-02-12 08:25:12 +03:00
|
|
|
return OverriddenMD;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
void VTableBuilder::ComputeThisAdjustments() {
|
2010-02-27 19:52:49 +03:00
|
|
|
// Now go through the method info map and see if any of the methods need
|
|
|
|
// 'this' pointer adjustments.
|
|
|
|
for (MethodInfoMapTy::const_iterator I = MethodInfoMap.begin(),
|
|
|
|
E = MethodInfoMap.end(); I != E; ++I) {
|
|
|
|
const CXXMethodDecl *MD = I->first;
|
|
|
|
const MethodInfo &MethodInfo = I->second;
|
|
|
|
|
2010-03-12 08:28:07 +03:00
|
|
|
// Ignore adjustments for unused function pointers.
|
|
|
|
uint64_t VtableIndex = MethodInfo.VtableIndex;
|
|
|
|
if (Components[VtableIndex].getKind() ==
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableComponent::CK_UnusedFunctionPointer)
|
2010-03-12 08:28:07 +03:00
|
|
|
continue;
|
|
|
|
|
2010-02-27 19:52:49 +03:00
|
|
|
// Get the final overrider for this method.
|
|
|
|
FinalOverriders::OverriderInfo Overrider =
|
2010-03-11 00:25:37 +03:00
|
|
|
Overriders.getOverrider(BaseSubobject(MD->getParent(),
|
|
|
|
MethodInfo.BaseOffset), MD);
|
2010-02-27 19:52:49 +03:00
|
|
|
|
2010-03-22 23:06:40 +03:00
|
|
|
// Check if we need an adjustment at all.
|
2010-03-29 19:08:41 +04:00
|
|
|
if (MethodInfo.BaseOffsetInLayoutClass == Overrider.Offset) {
|
|
|
|
// When a return thunk is needed by a derived class that overrides a
|
|
|
|
// virtual base, gcc uses a virtual 'this' adjustment as well.
|
|
|
|
// While the thunk itself might be needed by vtables in subclasses or
|
|
|
|
// in construction vtables, there doesn't seem to be a reason for using
|
|
|
|
// the thunk in this vtable. Still, we do so to match gcc.
|
|
|
|
if (VTableThunks.lookup(VtableIndex).Return.isEmpty())
|
|
|
|
continue;
|
|
|
|
}
|
2010-03-22 23:06:40 +03:00
|
|
|
|
2010-03-12 08:28:07 +03:00
|
|
|
ThisAdjustment ThisAdjustment =
|
|
|
|
ComputeThisAdjustment(MD, MethodInfo.BaseOffsetInLayoutClass, Overrider);
|
2010-02-27 21:16:50 +03:00
|
|
|
|
2010-03-12 08:28:07 +03:00
|
|
|
if (ThisAdjustment.isEmpty())
|
2010-02-27 21:16:50 +03:00
|
|
|
continue;
|
|
|
|
|
2010-02-27 21:09:40 +03:00
|
|
|
// Add it.
|
2010-03-23 07:44:10 +03:00
|
|
|
VTableThunks[VtableIndex].This = ThisAdjustment;
|
2010-03-12 08:28:07 +03:00
|
|
|
|
2010-02-27 19:52:49 +03:00
|
|
|
if (isa<CXXDestructorDecl>(MD)) {
|
|
|
|
// Add an adjustment for the deleting destructor as well.
|
2010-03-23 07:44:10 +03:00
|
|
|
VTableThunks[VtableIndex + 1].This = ThisAdjustment;
|
2010-02-27 19:52:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clear the method info map.
|
|
|
|
MethodInfoMap.clear();
|
2010-03-22 18:47:01 +03:00
|
|
|
|
|
|
|
if (isBuildingConstructorVtable()) {
|
|
|
|
// We don't need to store thunk information for construction vtables.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-23 07:59:02 +03:00
|
|
|
for (VtableThunksMapTy::const_iterator I = VTableThunks.begin(),
|
2010-03-23 07:44:10 +03:00
|
|
|
E = VTableThunks.end(); I != E; ++I) {
|
2010-04-10 23:13:06 +04:00
|
|
|
const VTableComponent &Component = Components[I->first];
|
2010-03-22 18:47:01 +03:00
|
|
|
const ThunkInfo &Thunk = I->second;
|
2010-03-22 23:06:40 +03:00
|
|
|
const CXXMethodDecl *MD;
|
2010-03-22 18:47:01 +03:00
|
|
|
|
|
|
|
switch (Component.getKind()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unexpected vtable component kind!");
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_FunctionPointer:
|
2010-03-22 23:06:40 +03:00
|
|
|
MD = Component.getFunctionDecl();
|
2010-03-22 18:47:01 +03:00
|
|
|
break;
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_CompleteDtorPointer:
|
2010-03-22 23:06:40 +03:00
|
|
|
MD = Component.getDestructorDecl();
|
2010-03-22 18:47:01 +03:00
|
|
|
break;
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_DeletingDtorPointer:
|
2010-03-22 18:47:01 +03:00
|
|
|
// We've already added the thunk when we saw the complete dtor pointer.
|
2010-03-22 23:06:40 +03:00
|
|
|
continue;
|
2010-03-22 18:47:01 +03:00
|
|
|
}
|
2010-03-22 23:06:40 +03:00
|
|
|
|
|
|
|
if (MD->getParent() == MostDerivedClass)
|
|
|
|
AddThunk(MD, Thunk);
|
2010-03-22 18:47:01 +03:00
|
|
|
}
|
2010-02-27 19:52:49 +03:00
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
ReturnAdjustment VTableBuilder::ComputeReturnAdjustment(BaseOffset Offset) {
|
2010-02-13 23:11:51 +03:00
|
|
|
ReturnAdjustment Adjustment;
|
|
|
|
|
|
|
|
if (!Offset.isEmpty()) {
|
2010-02-14 00:07:32 +03:00
|
|
|
if (Offset.VirtualBase) {
|
|
|
|
// Get the virtual base offset offset.
|
2010-03-11 10:00:45 +03:00
|
|
|
if (Offset.DerivedClass == MostDerivedClass) {
|
|
|
|
// We can get the offset offset directly from our map.
|
|
|
|
Adjustment.VBaseOffsetOffset =
|
|
|
|
VBaseOffsetOffsets.lookup(Offset.VirtualBase);
|
|
|
|
} else {
|
|
|
|
Adjustment.VBaseOffsetOffset =
|
2010-03-23 07:11:45 +03:00
|
|
|
VTables.getVirtualBaseOffsetOffset(Offset.DerivedClass,
|
|
|
|
Offset.VirtualBase);
|
2010-03-11 10:00:45 +03:00
|
|
|
}
|
|
|
|
|
2010-03-11 19:06:20 +03:00
|
|
|
// FIXME: Once the assert in getVirtualBaseOffsetOffset is back again,
|
2010-02-14 00:07:32 +03:00
|
|
|
// we can get rid of this assert.
|
|
|
|
assert(Adjustment.VBaseOffsetOffset != 0 &&
|
2010-03-11 19:06:20 +03:00
|
|
|
"Invalid vbase offset offset!");
|
2010-02-14 00:07:32 +03:00
|
|
|
}
|
2010-02-13 23:11:51 +03:00
|
|
|
|
|
|
|
Adjustment.NonVirtual = Offset.NonVirtualOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Adjustment;
|
|
|
|
}
|
|
|
|
|
2010-02-27 22:57:44 +03:00
|
|
|
BaseOffset
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableBuilder::ComputeThisAdjustmentBaseOffset(BaseSubobject Base,
|
2010-02-27 22:57:44 +03:00
|
|
|
BaseSubobject Derived) const {
|
|
|
|
const CXXRecordDecl *BaseRD = Base.getBase();
|
|
|
|
const CXXRecordDecl *DerivedRD = Derived.getBase();
|
|
|
|
|
|
|
|
CXXBasePaths Paths(/*FindAmbiguities=*/true,
|
|
|
|
/*RecordPaths=*/true, /*DetectVirtual=*/true);
|
|
|
|
|
|
|
|
if (!const_cast<CXXRecordDecl *>(DerivedRD)->
|
|
|
|
isDerivedFrom(const_cast<CXXRecordDecl *>(BaseRD), Paths)) {
|
|
|
|
assert(false && "Class must be derived from the passed in base class!");
|
|
|
|
return BaseOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to go through all the paths, and see which one leads us to the
|
|
|
|
// right base subobject.
|
|
|
|
for (CXXBasePaths::const_paths_iterator I = Paths.begin(), E = Paths.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
BaseOffset Offset = ComputeBaseOffset(Context, DerivedRD, *I);
|
|
|
|
|
|
|
|
// FIXME: Should not use * 8 here.
|
|
|
|
uint64_t OffsetToBaseSubobject = Offset.NonVirtualOffset * 8;
|
|
|
|
|
|
|
|
if (Offset.VirtualBase) {
|
|
|
|
// If we have a virtual base class, the non-virtual offset is relative
|
|
|
|
// to the virtual base class offset.
|
2010-03-11 00:25:37 +03:00
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
2010-02-27 22:57:44 +03:00
|
|
|
|
|
|
|
/// Get the virtual base offset, relative to the most derived class
|
|
|
|
/// layout.
|
|
|
|
OffsetToBaseSubobject +=
|
2010-03-11 00:25:37 +03:00
|
|
|
LayoutClassLayout.getVBaseClassOffset(Offset.VirtualBase);
|
2010-02-27 22:57:44 +03:00
|
|
|
} else {
|
|
|
|
// Otherwise, the non-virtual offset is relative to the derived class
|
|
|
|
// offset.
|
|
|
|
OffsetToBaseSubobject += Derived.getBaseOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this path gives us the right base subobject.
|
|
|
|
if (OffsetToBaseSubobject == Base.getBaseOffset()) {
|
|
|
|
// Since we're going from the base class _to_ the derived class, we'll
|
|
|
|
// invert the non-virtual offset here.
|
|
|
|
Offset.NonVirtualOffset = -Offset.NonVirtualOffset;
|
|
|
|
return Offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return BaseOffset();
|
|
|
|
}
|
|
|
|
|
2010-03-23 18:13:06 +03:00
|
|
|
ThisAdjustment
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableBuilder::ComputeThisAdjustment(const CXXMethodDecl *MD,
|
2010-03-12 08:28:07 +03:00
|
|
|
uint64_t BaseOffsetInLayoutClass,
|
|
|
|
FinalOverriders::OverriderInfo Overrider) {
|
|
|
|
// Ignore adjustments for pure virtual member functions.
|
|
|
|
if (Overrider.Method->isPure())
|
|
|
|
return ThisAdjustment();
|
|
|
|
|
|
|
|
BaseSubobject OverriddenBaseSubobject(MD->getParent(),
|
|
|
|
BaseOffsetInLayoutClass);
|
|
|
|
|
|
|
|
BaseSubobject OverriderBaseSubobject(Overrider.Method->getParent(),
|
|
|
|
Overrider.Offset);
|
|
|
|
|
|
|
|
// Compute the adjustment offset.
|
|
|
|
BaseOffset Offset = ComputeThisAdjustmentBaseOffset(OverriddenBaseSubobject,
|
|
|
|
OverriderBaseSubobject);
|
|
|
|
if (Offset.isEmpty())
|
|
|
|
return ThisAdjustment();
|
2010-02-27 22:57:44 +03:00
|
|
|
|
2010-02-14 02:40:17 +03:00
|
|
|
ThisAdjustment Adjustment;
|
|
|
|
|
2010-03-12 08:28:07 +03:00
|
|
|
if (Offset.VirtualBase) {
|
|
|
|
// Get the vcall offset map for this virtual base.
|
|
|
|
VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Offset.VirtualBase];
|
|
|
|
|
|
|
|
if (VCallOffsets.empty()) {
|
|
|
|
// We don't have vcall offsets for this virtual base, go ahead and
|
|
|
|
// build them.
|
|
|
|
VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, MostDerivedClass,
|
|
|
|
/*FinalOverriders=*/0,
|
|
|
|
BaseSubobject(Offset.VirtualBase, 0),
|
|
|
|
/*BaseIsVirtual=*/true,
|
|
|
|
/*OffsetInLayoutClass=*/0);
|
2010-02-26 01:23:13 +03:00
|
|
|
|
2010-03-12 08:28:07 +03:00
|
|
|
VCallOffsets = Builder.getVCallOffsets();
|
2010-02-18 20:26:40 +03:00
|
|
|
}
|
2010-03-12 08:28:07 +03:00
|
|
|
|
|
|
|
Adjustment.VCallOffsetOffset = VCallOffsets.getVCallOffsetOffset(MD);
|
2010-02-14 02:40:17 +03:00
|
|
|
}
|
2010-03-12 08:28:07 +03:00
|
|
|
|
|
|
|
// Set the non-virtual part of the adjustment.
|
|
|
|
Adjustment.NonVirtual = Offset.NonVirtualOffset;
|
2010-02-14 02:40:17 +03:00
|
|
|
|
|
|
|
return Adjustment;
|
|
|
|
}
|
2010-03-12 08:28:07 +03:00
|
|
|
|
2010-02-13 23:11:51 +03:00
|
|
|
void
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableBuilder::AddMethod(const CXXMethodDecl *MD,
|
2010-02-27 19:52:49 +03:00
|
|
|
ReturnAdjustment ReturnAdjustment) {
|
2010-02-13 23:11:51 +03:00
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
|
|
|
|
assert(ReturnAdjustment.isEmpty() &&
|
|
|
|
"Destructor can't have return adjustment!");
|
2010-02-14 02:40:17 +03:00
|
|
|
|
2010-02-13 23:11:51 +03:00
|
|
|
// Add both the complete destructor and the deleting destructor.
|
2010-04-10 23:13:06 +04:00
|
|
|
Components.push_back(VTableComponent::MakeCompleteDtor(DD));
|
|
|
|
Components.push_back(VTableComponent::MakeDeletingDtor(DD));
|
2010-02-13 23:11:51 +03:00
|
|
|
} else {
|
|
|
|
// Add the return adjustment if necessary.
|
|
|
|
if (!ReturnAdjustment.isEmpty())
|
2010-03-23 07:44:10 +03:00
|
|
|
VTableThunks[Components.size()].Return = ReturnAdjustment;
|
2010-02-13 23:11:51 +03:00
|
|
|
|
|
|
|
// Add the function.
|
2010-04-10 23:13:06 +04:00
|
|
|
Components.push_back(VTableComponent::MakeFunction(MD));
|
2010-02-13 23:11:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-27 09:38:03 +03:00
|
|
|
/// OverridesIndirectMethodInBase - Return whether the given member function
|
|
|
|
/// overrides any methods in the set of given bases.
|
|
|
|
/// Unlike OverridesMethodInBase, this checks "overriders of overriders".
|
|
|
|
/// For example, if we have:
|
|
|
|
///
|
|
|
|
/// struct A { virtual void f(); }
|
|
|
|
/// struct B : A { virtual void f(); }
|
|
|
|
/// struct C : B { virtual void f(); }
|
|
|
|
///
|
|
|
|
/// OverridesIndirectMethodInBase will return true if given C::f as the method
|
|
|
|
/// and { A } as the set of bases.
|
|
|
|
static bool
|
|
|
|
OverridesIndirectMethodInBases(const CXXMethodDecl *MD,
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableBuilder::PrimaryBasesSetVectorTy &Bases) {
|
2010-02-27 09:38:03 +03:00
|
|
|
for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
|
|
|
|
E = MD->end_overridden_methods(); I != E; ++I) {
|
|
|
|
const CXXMethodDecl *OverriddenMD = *I;
|
|
|
|
const CXXRecordDecl *OverriddenRD = OverriddenMD->getParent();
|
|
|
|
assert(OverriddenMD->isCanonicalDecl() &&
|
|
|
|
"Should have the canonical decl of the overridden RD!");
|
|
|
|
|
|
|
|
if (Bases.count(OverriddenRD))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Check "indirect overriders".
|
|
|
|
if (OverridesIndirectMethodInBases(OverriddenMD, Bases))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-23 07:26:39 +03:00
|
|
|
bool
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableBuilder::IsOverriderUsed(const CXXMethodDecl *Overrider,
|
2010-03-10 09:51:42 +03:00
|
|
|
uint64_t BaseOffsetInLayoutClass,
|
|
|
|
const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
|
|
|
|
uint64_t FirstBaseOffsetInLayoutClass) const {
|
2010-02-23 07:26:39 +03:00
|
|
|
// If the base and the first base in the primary base chain have the same
|
|
|
|
// offsets, then this overrider will be used.
|
2010-03-10 09:51:42 +03:00
|
|
|
if (BaseOffsetInLayoutClass == FirstBaseOffsetInLayoutClass)
|
2010-02-23 09:34:44 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// We know now that Base (or a direct or indirect base of it) is a primary
|
|
|
|
// base in part of the class hierarchy, but not a primary base in the most
|
|
|
|
// derived class.
|
|
|
|
|
|
|
|
// If the overrider is the first base in the primary base chain, we know
|
|
|
|
// that the overrider will be used.
|
2010-03-10 09:51:42 +03:00
|
|
|
if (Overrider->getParent() == FirstBaseInPrimaryBaseChain)
|
2010-02-23 07:26:39 +03:00
|
|
|
return true;
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
|
2010-02-23 09:34:44 +03:00
|
|
|
|
2010-03-10 09:51:42 +03:00
|
|
|
const CXXRecordDecl *RD = FirstBaseInPrimaryBaseChain;
|
2010-02-23 09:34:44 +03:00
|
|
|
PrimaryBases.insert(RD);
|
|
|
|
|
|
|
|
// Now traverse the base chain, starting with the first base, until we find
|
|
|
|
// the base that is no longer a primary base.
|
|
|
|
while (true) {
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
|
|
|
|
|
|
|
if (!PrimaryBase)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (Layout.getPrimaryBaseWasVirtual()) {
|
|
|
|
assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 &&
|
|
|
|
"Primary base should always be at offset 0!");
|
|
|
|
|
2010-02-28 21:37:33 +03:00
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
2010-02-23 09:34:44 +03:00
|
|
|
|
|
|
|
// Now check if this is the primary base that is not a primary base in the
|
|
|
|
// most derived class.
|
2010-02-28 21:37:33 +03:00
|
|
|
if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
|
2010-03-10 09:51:42 +03:00
|
|
|
FirstBaseOffsetInLayoutClass) {
|
2010-02-23 09:34:44 +03:00
|
|
|
// We found it, stop walking the chain.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(Layout.getBaseClassOffset(PrimaryBase) == 0 &&
|
|
|
|
"Primary base should always be at offset 0!");
|
|
|
|
}
|
2010-02-27 21:09:40 +03:00
|
|
|
|
2010-02-27 22:51:04 +03:00
|
|
|
if (!PrimaryBases.insert(PrimaryBase))
|
|
|
|
assert(false && "Found a duplicate primary base!");
|
|
|
|
|
2010-02-27 21:09:40 +03:00
|
|
|
RD = PrimaryBase;
|
2010-02-23 09:34:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the final overrider is an override of one of the primary bases,
|
|
|
|
// then we know that it will be used.
|
2010-03-10 09:51:42 +03:00
|
|
|
return OverridesIndirectMethodInBases(Overrider, PrimaryBases);
|
2010-02-23 07:26:39 +03:00
|
|
|
}
|
|
|
|
|
2010-02-27 19:52:49 +03:00
|
|
|
/// FindNearestOverriddenMethod - Given a method, returns the overridden method
|
|
|
|
/// from the nearest base. Returns null if no method was found.
|
|
|
|
static const CXXMethodDecl *
|
|
|
|
FindNearestOverriddenMethod(const CXXMethodDecl *MD,
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableBuilder::PrimaryBasesSetVectorTy &Bases) {
|
2010-02-27 19:52:49 +03:00
|
|
|
for (int I = Bases.size(), E = 0; I != E; --I) {
|
|
|
|
const CXXRecordDecl *PrimaryBase = Bases[I - 1];
|
|
|
|
|
|
|
|
// Now check the overriden methods.
|
|
|
|
for (CXXMethodDecl::method_iterator I = MD->begin_overridden_methods(),
|
|
|
|
E = MD->end_overridden_methods(); I != E; ++I) {
|
|
|
|
const CXXMethodDecl *OverriddenMD = *I;
|
|
|
|
|
|
|
|
// We found our overridden method.
|
|
|
|
if (OverriddenMD->getParent() == PrimaryBase)
|
|
|
|
return OverriddenMD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-10 09:51:42 +03:00
|
|
|
void
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableBuilder::AddMethods(BaseSubobject Base, uint64_t BaseOffsetInLayoutClass,
|
2010-03-10 09:51:42 +03:00
|
|
|
const CXXRecordDecl *FirstBaseInPrimaryBaseChain,
|
|
|
|
uint64_t FirstBaseOffsetInLayoutClass,
|
2010-02-27 19:18:19 +03:00
|
|
|
PrimaryBasesSetVectorTy &PrimaryBases) {
|
2010-02-12 08:25:12 +03:00
|
|
|
const CXXRecordDecl *RD = Base.getBase();
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
|
2010-03-10 09:51:42 +03:00
|
|
|
uint64_t PrimaryBaseOffset;
|
|
|
|
uint64_t PrimaryBaseOffsetInLayoutClass;
|
2010-02-23 06:48:14 +03:00
|
|
|
if (Layout.getPrimaryBaseWasVirtual()) {
|
|
|
|
assert(Layout.getVBaseClassOffset(PrimaryBase) == 0 &&
|
|
|
|
"Primary vbase should have a zero offset!");
|
|
|
|
|
|
|
|
const ASTRecordLayout &MostDerivedClassLayout =
|
|
|
|
Context.getASTRecordLayout(MostDerivedClass);
|
|
|
|
|
2010-03-10 09:51:42 +03:00
|
|
|
PrimaryBaseOffset =
|
|
|
|
MostDerivedClassLayout.getVBaseClassOffset(PrimaryBase);
|
|
|
|
|
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
|
|
|
|
|
|
|
PrimaryBaseOffsetInLayoutClass =
|
|
|
|
LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
|
2010-02-23 06:48:14 +03:00
|
|
|
} else {
|
2010-02-12 08:25:12 +03:00
|
|
|
assert(Layout.getBaseClassOffset(PrimaryBase) == 0 &&
|
|
|
|
"Primary base should have a zero offset!");
|
2010-02-23 06:48:14 +03:00
|
|
|
|
2010-03-10 09:51:42 +03:00
|
|
|
PrimaryBaseOffset = Base.getBaseOffset();
|
|
|
|
PrimaryBaseOffsetInLayoutClass = BaseOffsetInLayoutClass;
|
2010-02-23 06:48:14 +03:00
|
|
|
}
|
2010-02-28 21:37:33 +03:00
|
|
|
|
2010-03-10 09:51:42 +03:00
|
|
|
AddMethods(BaseSubobject(PrimaryBase, PrimaryBaseOffset),
|
|
|
|
PrimaryBaseOffsetInLayoutClass, FirstBaseInPrimaryBaseChain,
|
|
|
|
FirstBaseOffsetInLayoutClass, PrimaryBases);
|
2010-02-12 08:25:12 +03:00
|
|
|
|
|
|
|
if (!PrimaryBases.insert(PrimaryBase))
|
|
|
|
assert(false && "Found a duplicate primary base!");
|
|
|
|
}
|
2010-02-11 20:18:51 +03:00
|
|
|
|
2010-02-11 11:02:13 +03:00
|
|
|
// Now go through all virtual member functions and add them.
|
|
|
|
for (CXXRecordDecl::method_iterator I = RD->method_begin(),
|
|
|
|
E = RD->method_end(); I != E; ++I) {
|
|
|
|
const CXXMethodDecl *MD = *I;
|
2010-02-12 08:25:12 +03:00
|
|
|
|
2010-02-11 11:02:13 +03:00
|
|
|
if (!MD->isVirtual())
|
|
|
|
continue;
|
2010-02-12 08:25:12 +03:00
|
|
|
|
|
|
|
// Get the final overrider.
|
2010-02-12 19:55:34 +03:00
|
|
|
FinalOverriders::OverriderInfo Overrider =
|
|
|
|
Overriders.getOverrider(Base, MD);
|
2010-02-12 08:25:12 +03:00
|
|
|
|
|
|
|
// Check if this virtual member function overrides a method in a primary
|
|
|
|
// base. If this is the case, and the return type doesn't require adjustment
|
|
|
|
// then we can just use the member function from the primary base.
|
2010-02-13 23:11:51 +03:00
|
|
|
if (const CXXMethodDecl *OverriddenMD =
|
2010-02-27 19:52:49 +03:00
|
|
|
FindNearestOverriddenMethod(MD, PrimaryBases)) {
|
2010-02-14 02:17:31 +03:00
|
|
|
if (ComputeReturnAdjustmentBaseOffset(Context, MD,
|
2010-02-27 19:52:49 +03:00
|
|
|
OverriddenMD).isEmpty()) {
|
|
|
|
// Replace the method info of the overridden method with our own
|
|
|
|
// method.
|
|
|
|
assert(MethodInfoMap.count(OverriddenMD) &&
|
|
|
|
"Did not find the overridden method!");
|
|
|
|
MethodInfo &OverriddenMethodInfo = MethodInfoMap[OverriddenMD];
|
|
|
|
|
|
|
|
MethodInfo MethodInfo(Base.getBaseOffset(),
|
2010-03-11 00:25:37 +03:00
|
|
|
BaseOffsetInLayoutClass,
|
2010-02-27 19:52:49 +03:00
|
|
|
OverriddenMethodInfo.VtableIndex);
|
|
|
|
|
|
|
|
assert(!MethodInfoMap.count(MD) &&
|
|
|
|
"Should not have method info for this method yet!");
|
|
|
|
|
|
|
|
MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
|
|
|
|
MethodInfoMap.erase(OverriddenMD);
|
2010-03-22 23:06:40 +03:00
|
|
|
|
|
|
|
// If the overridden method exists in a virtual base class or a direct
|
|
|
|
// or indirect base class of a virtual base class, we need to emit a
|
|
|
|
// thunk if we ever have a class hierarchy where the base class is not
|
|
|
|
// a primary base in the complete object.
|
|
|
|
if (!isBuildingConstructorVtable() && OverriddenMD != MD) {
|
|
|
|
// Compute the this adjustment.
|
|
|
|
ThisAdjustment ThisAdjustment =
|
|
|
|
ComputeThisAdjustment(OverriddenMD, BaseOffsetInLayoutClass,
|
|
|
|
Overrider);
|
|
|
|
|
2010-03-29 06:53:58 +04:00
|
|
|
if (ThisAdjustment.VCallOffsetOffset &&
|
|
|
|
Overrider.Method->getParent() == MostDerivedClass) {
|
|
|
|
// This is a virtual thunk for the most derived class, add it.
|
2010-03-22 23:06:40 +03:00
|
|
|
AddThunk(Overrider.Method,
|
|
|
|
ThunkInfo(ThisAdjustment, ReturnAdjustment()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-13 23:11:51 +03:00
|
|
|
continue;
|
2010-02-27 19:52:49 +03:00
|
|
|
}
|
2010-02-12 08:25:12 +03:00
|
|
|
}
|
2010-02-13 23:11:51 +03:00
|
|
|
|
2010-02-27 19:52:49 +03:00
|
|
|
// Insert the method info for this method.
|
2010-03-11 00:25:37 +03:00
|
|
|
MethodInfo MethodInfo(Base.getBaseOffset(), BaseOffsetInLayoutClass,
|
|
|
|
Components.size());
|
2010-02-27 19:52:49 +03:00
|
|
|
|
|
|
|
assert(!MethodInfoMap.count(MD) &&
|
|
|
|
"Should not have method info for this method yet!");
|
|
|
|
MethodInfoMap.insert(std::make_pair(MD, MethodInfo));
|
2010-02-27 21:09:40 +03:00
|
|
|
|
|
|
|
// Check if this overrider is going to be used.
|
2010-03-10 09:51:42 +03:00
|
|
|
const CXXMethodDecl *OverriderMD = Overrider.Method;
|
|
|
|
if (!IsOverriderUsed(OverriderMD, BaseOffsetInLayoutClass,
|
|
|
|
FirstBaseInPrimaryBaseChain,
|
|
|
|
FirstBaseOffsetInLayoutClass)) {
|
2010-04-10 23:13:06 +04:00
|
|
|
Components.push_back(VTableComponent::MakeUnusedFunction(OverriderMD));
|
2010-02-27 21:09:40 +03:00
|
|
|
continue;
|
|
|
|
}
|
2010-02-23 07:26:39 +03:00
|
|
|
|
2010-02-13 23:11:51 +03:00
|
|
|
// Check if this overrider needs a return adjustment.
|
2010-02-23 04:34:28 +03:00
|
|
|
BaseOffset ReturnAdjustmentOffset =
|
2010-02-13 23:11:51 +03:00
|
|
|
Overriders.getReturnAdjustmentOffset(Base, MD);
|
|
|
|
|
|
|
|
ReturnAdjustment ReturnAdjustment =
|
|
|
|
ComputeReturnAdjustment(ReturnAdjustmentOffset);
|
2010-02-11 11:02:13 +03:00
|
|
|
|
2010-02-27 19:52:49 +03:00
|
|
|
AddMethod(Overrider.Method, ReturnAdjustment);
|
2010-02-11 11:02:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
void VTableBuilder::LayoutVtable() {
|
2010-02-28 04:43:58 +03:00
|
|
|
LayoutPrimaryAndSecondaryVtables(BaseSubobject(MostDerivedClass, 0),
|
|
|
|
MostDerivedClassIsVirtual,
|
|
|
|
MostDerivedClassOffset);
|
2010-02-16 19:26:28 +03:00
|
|
|
|
2010-02-16 19:49:35 +03:00
|
|
|
VisitedVirtualBasesSetTy VBases;
|
2010-02-27 07:05:52 +03:00
|
|
|
|
|
|
|
// Determine the primary virtual bases.
|
2010-03-10 06:02:01 +03:00
|
|
|
DeterminePrimaryVirtualBases(MostDerivedClass, MostDerivedClassOffset,
|
|
|
|
VBases);
|
2010-02-27 07:05:52 +03:00
|
|
|
VBases.clear();
|
|
|
|
|
2010-02-16 19:49:35 +03:00
|
|
|
LayoutVtablesForVirtualBases(MostDerivedClass, VBases);
|
2010-02-16 19:26:28 +03:00
|
|
|
}
|
|
|
|
|
2010-02-28 04:43:58 +03:00
|
|
|
void
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableBuilder::LayoutPrimaryAndSecondaryVtables(BaseSubobject Base,
|
2010-02-28 04:43:58 +03:00
|
|
|
bool BaseIsVirtual,
|
|
|
|
uint64_t OffsetInLayoutClass) {
|
2010-02-18 00:42:34 +03:00
|
|
|
assert(Base.getBase()->isDynamicClass() && "class does not have a vtable!");
|
2010-02-14 20:05:59 +03:00
|
|
|
|
2010-02-16 07:59:55 +03:00
|
|
|
// Add vcall and vbase offsets for this vtable.
|
2010-02-28 04:43:58 +03:00
|
|
|
VCallAndVBaseOffsetBuilder Builder(MostDerivedClass, LayoutClass, &Overriders,
|
|
|
|
Base, BaseIsVirtual, OffsetInLayoutClass);
|
2010-02-25 06:45:56 +03:00
|
|
|
Components.append(Builder.components_begin(), Builder.components_end());
|
|
|
|
|
2010-02-26 01:23:13 +03:00
|
|
|
// Check if we need to add these vcall offsets.
|
|
|
|
if (BaseIsVirtual && !Builder.getVCallOffsets().empty()) {
|
|
|
|
VCallOffsetMap &VCallOffsets = VCallOffsetsForVBases[Base.getBase()];
|
|
|
|
|
|
|
|
if (VCallOffsets.empty())
|
|
|
|
VCallOffsets = Builder.getVCallOffsets();
|
|
|
|
}
|
|
|
|
|
2010-03-11 09:43:12 +03:00
|
|
|
// If we're laying out the most derived class we want to keep track of the
|
|
|
|
// virtual base class offset offsets.
|
|
|
|
if (Base.getBase() == MostDerivedClass)
|
|
|
|
VBaseOffsetOffsets = Builder.getVBaseOffsetOffsets();
|
|
|
|
|
2010-02-16 07:59:55 +03:00
|
|
|
// Add the offset to top.
|
2010-02-14 03:16:19 +03:00
|
|
|
// FIXME: We should not use / 8 here.
|
2010-02-28 04:43:58 +03:00
|
|
|
int64_t OffsetToTop = -(int64_t)(OffsetInLayoutClass -
|
|
|
|
MostDerivedClassOffset) / 8;
|
2010-04-10 23:13:06 +04:00
|
|
|
Components.push_back(VTableComponent::MakeOffsetToTop(OffsetToTop));
|
2010-02-12 08:25:12 +03:00
|
|
|
|
|
|
|
// Next, add the RTTI.
|
2010-04-10 23:13:06 +04:00
|
|
|
Components.push_back(VTableComponent::MakeRTTI(MostDerivedClass));
|
2010-02-12 08:25:12 +03:00
|
|
|
|
2010-02-12 10:43:48 +03:00
|
|
|
uint64_t AddressPoint = Components.size();
|
2010-02-12 08:25:12 +03:00
|
|
|
|
|
|
|
// Now go through all virtual member functions and add them.
|
2010-02-27 19:18:19 +03:00
|
|
|
PrimaryBasesSetVectorTy PrimaryBases;
|
2010-03-10 09:51:42 +03:00
|
|
|
AddMethods(Base, OffsetInLayoutClass, Base.getBase(), OffsetInLayoutClass,
|
|
|
|
PrimaryBases);
|
2010-02-12 08:25:12 +03:00
|
|
|
|
2010-02-27 19:52:49 +03:00
|
|
|
// Compute 'this' pointer adjustments.
|
|
|
|
ComputeThisAdjustments();
|
|
|
|
|
2010-03-26 00:45:14 +03:00
|
|
|
// Add all address points.
|
|
|
|
const CXXRecordDecl *RD = Base.getBase();
|
|
|
|
while (true) {
|
|
|
|
AddressPoints.insert(std::make_pair(BaseSubobject(RD, OffsetInLayoutClass),
|
|
|
|
AddressPoint));
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
2010-02-12 10:43:48 +03:00
|
|
|
|
2010-03-26 00:45:14 +03:00
|
|
|
if (!PrimaryBase)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (Layout.getPrimaryBaseWasVirtual()) {
|
|
|
|
// Check if this virtual primary base is a primary base in the layout
|
|
|
|
// class. If it's not, we don't want to add it.
|
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
|
|
|
|
|
|
|
if (LayoutClassLayout.getVBaseClassOffset(PrimaryBase) !=
|
|
|
|
OffsetInLayoutClass) {
|
|
|
|
// We don't want to add this class (or any of its primary bases).
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RD = PrimaryBase;
|
2010-02-12 10:43:48 +03:00
|
|
|
}
|
|
|
|
|
2010-03-10 22:39:11 +03:00
|
|
|
bool BaseIsMorallyVirtual = BaseIsVirtual;
|
|
|
|
if (isBuildingConstructorVtable() && Base.getBase() == MostDerivedClass)
|
|
|
|
BaseIsMorallyVirtual = false;
|
|
|
|
|
2010-02-24 19:43:12 +03:00
|
|
|
// Layout secondary vtables.
|
2010-03-10 22:39:11 +03:00
|
|
|
LayoutSecondaryVtables(Base, BaseIsMorallyVirtual, OffsetInLayoutClass);
|
2010-02-16 19:02:57 +03:00
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
void VTableBuilder::LayoutSecondaryVtables(BaseSubobject Base,
|
2010-03-10 22:39:11 +03:00
|
|
|
bool BaseIsMorallyVirtual,
|
2010-02-28 04:43:58 +03:00
|
|
|
uint64_t OffsetInLayoutClass) {
|
2010-02-16 19:02:57 +03:00
|
|
|
// Itanium C++ ABI 2.5.2:
|
|
|
|
// Following the primary virtual table of a derived class are secondary
|
|
|
|
// virtual tables for each of its proper base classes, except any primary
|
|
|
|
// base(s) with which it shares its primary virtual table.
|
|
|
|
|
|
|
|
const CXXRecordDecl *RD = Base.getBase();
|
2010-02-12 08:25:12 +03:00
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
2010-02-16 19:02:57 +03:00
|
|
|
|
2010-02-12 08:25:12 +03:00
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
2010-02-16 19:02:57 +03:00
|
|
|
// Ignore virtual bases, we'll emit them later.
|
|
|
|
if (I->isVirtual())
|
|
|
|
continue;
|
|
|
|
|
2010-02-12 08:25:12 +03:00
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
2010-02-14 20:05:59 +03:00
|
|
|
|
2010-02-16 07:49:44 +03:00
|
|
|
// Ignore bases that don't have a vtable.
|
|
|
|
if (!BaseDecl->isDynamicClass())
|
|
|
|
continue;
|
2010-02-14 20:05:59 +03:00
|
|
|
|
2010-03-10 22:15:26 +03:00
|
|
|
if (isBuildingConstructorVtable()) {
|
|
|
|
// Itanium C++ ABI 2.6.4:
|
|
|
|
// Some of the base class subobjects may not need construction virtual
|
|
|
|
// tables, which will therefore not be present in the construction
|
|
|
|
// virtual table group, even though the subobject virtual tables are
|
|
|
|
// present in the main virtual table group for the complete object.
|
2010-03-10 22:39:11 +03:00
|
|
|
if (!BaseIsMorallyVirtual && !BaseDecl->getNumVBases())
|
2010-03-10 22:15:26 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-02-14 01:05:23 +03:00
|
|
|
// Get the base offset of this base.
|
2010-02-28 04:43:58 +03:00
|
|
|
uint64_t RelativeBaseOffset = Layout.getBaseClassOffset(BaseDecl);
|
|
|
|
uint64_t BaseOffset = Base.getBaseOffset() + RelativeBaseOffset;
|
|
|
|
|
|
|
|
uint64_t BaseOffsetInLayoutClass = OffsetInLayoutClass + RelativeBaseOffset;
|
2010-02-16 19:02:57 +03:00
|
|
|
|
|
|
|
// Don't emit a secondary vtable for a primary base. We might however want
|
|
|
|
// to emit secondary vtables for other bases of this base.
|
|
|
|
if (BaseDecl == PrimaryBase) {
|
2010-02-28 04:43:58 +03:00
|
|
|
LayoutSecondaryVtables(BaseSubobject(BaseDecl, BaseOffset),
|
2010-03-10 22:39:11 +03:00
|
|
|
BaseIsMorallyVirtual, BaseOffsetInLayoutClass);
|
2010-02-16 19:02:57 +03:00
|
|
|
continue;
|
|
|
|
}
|
2010-02-14 01:05:23 +03:00
|
|
|
|
2010-02-16 19:26:28 +03:00
|
|
|
// Layout the primary vtable (and any secondary vtables) for this base.
|
2010-02-28 04:43:58 +03:00
|
|
|
LayoutPrimaryAndSecondaryVtables(BaseSubobject(BaseDecl, BaseOffset),
|
|
|
|
/*BaseIsVirtual=*/false,
|
|
|
|
BaseOffsetInLayoutClass);
|
2010-02-12 08:25:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-27 07:05:52 +03:00
|
|
|
void
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableBuilder::DeterminePrimaryVirtualBases(const CXXRecordDecl *RD,
|
2010-03-10 06:02:01 +03:00
|
|
|
uint64_t OffsetInLayoutClass,
|
2010-02-27 07:05:52 +03:00
|
|
|
VisitedVirtualBasesSetTy &VBases) {
|
|
|
|
const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
|
|
|
|
|
|
|
|
// Check if this base has a primary base.
|
|
|
|
if (const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase()) {
|
2010-02-28 21:08:38 +03:00
|
|
|
|
|
|
|
// Check if it's virtual.
|
|
|
|
if (Layout.getPrimaryBaseWasVirtual()) {
|
|
|
|
bool IsPrimaryVirtualBase = true;
|
|
|
|
|
|
|
|
if (isBuildingConstructorVtable()) {
|
|
|
|
// Check if the base is actually a primary base in the class we use for
|
|
|
|
// layout.
|
2010-03-10 06:02:01 +03:00
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
|
|
|
|
|
|
|
uint64_t PrimaryBaseOffsetInLayoutClass =
|
|
|
|
LayoutClassLayout.getVBaseClassOffset(PrimaryBase);
|
|
|
|
|
|
|
|
// We know that the base is not a primary base in the layout class if
|
|
|
|
// the base offsets are different.
|
|
|
|
if (PrimaryBaseOffsetInLayoutClass != OffsetInLayoutClass)
|
2010-02-28 21:08:38 +03:00
|
|
|
IsPrimaryVirtualBase = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsPrimaryVirtualBase)
|
|
|
|
PrimaryVirtualBases.insert(PrimaryBase);
|
|
|
|
}
|
2010-02-27 07:05:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Traverse bases, looking for more primary virtual bases.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
2010-03-10 06:02:01 +03:00
|
|
|
uint64_t BaseOffsetInLayoutClass;
|
|
|
|
|
|
|
|
if (I->isVirtual()) {
|
|
|
|
if (!VBases.insert(BaseDecl))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
|
|
|
|
|
|
|
BaseOffsetInLayoutClass = LayoutClassLayout.getVBaseClassOffset(BaseDecl);
|
|
|
|
} else {
|
|
|
|
BaseOffsetInLayoutClass =
|
|
|
|
OffsetInLayoutClass + Layout.getBaseClassOffset(BaseDecl);
|
|
|
|
}
|
2010-02-27 07:05:52 +03:00
|
|
|
|
2010-03-10 06:02:01 +03:00
|
|
|
DeterminePrimaryVirtualBases(BaseDecl, BaseOffsetInLayoutClass, VBases);
|
2010-02-27 07:05:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-16 19:49:35 +03:00
|
|
|
void
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableBuilder::LayoutVtablesForVirtualBases(const CXXRecordDecl *RD,
|
2010-02-16 19:49:35 +03:00
|
|
|
VisitedVirtualBasesSetTy &VBases) {
|
|
|
|
// Itanium C++ ABI 2.5.2:
|
|
|
|
// Then come the virtual base virtual tables, also in inheritance graph
|
|
|
|
// order, and again excluding primary bases (which share virtual tables with
|
|
|
|
// the classes for which they are primary).
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
2010-02-23 06:48:14 +03:00
|
|
|
// Check if this base needs a vtable. (If it's virtual, not a primary base
|
|
|
|
// of some other class, and we haven't visited it before).
|
2010-02-16 19:49:35 +03:00
|
|
|
if (I->isVirtual() && BaseDecl->isDynamicClass() &&
|
2010-02-27 07:05:52 +03:00
|
|
|
!PrimaryVirtualBases.count(BaseDecl) && VBases.insert(BaseDecl)) {
|
2010-02-16 19:49:35 +03:00
|
|
|
const ASTRecordLayout &MostDerivedClassLayout =
|
|
|
|
Context.getASTRecordLayout(MostDerivedClass);
|
|
|
|
uint64_t BaseOffset =
|
|
|
|
MostDerivedClassLayout.getVBaseClassOffset(BaseDecl);
|
|
|
|
|
2010-02-28 04:43:58 +03:00
|
|
|
const ASTRecordLayout &LayoutClassLayout =
|
|
|
|
Context.getASTRecordLayout(LayoutClass);
|
|
|
|
uint64_t BaseOffsetInLayoutClass =
|
|
|
|
LayoutClassLayout.getVBaseClassOffset(BaseDecl);
|
|
|
|
|
|
|
|
LayoutPrimaryAndSecondaryVtables(BaseSubobject(BaseDecl, BaseOffset),
|
|
|
|
/*BaseIsVirtual=*/true,
|
|
|
|
BaseOffsetInLayoutClass);
|
2010-02-16 19:49:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We only need to check the base for virtual base vtables if it actually
|
|
|
|
// has virtual bases.
|
|
|
|
if (BaseDecl->getNumVBases())
|
|
|
|
LayoutVtablesForVirtualBases(BaseDecl, VBases);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-11 11:02:13 +03:00
|
|
|
/// dumpLayout - Dump the vtable layout.
|
2010-04-10 23:13:06 +04:00
|
|
|
void VTableBuilder::dumpLayout(llvm::raw_ostream& Out) {
|
2010-02-27 23:39:05 +03:00
|
|
|
|
2010-03-11 09:43:12 +03:00
|
|
|
if (isBuildingConstructorVtable()) {
|
2010-02-27 23:39:05 +03:00
|
|
|
Out << "Construction vtable for ('";
|
|
|
|
Out << MostDerivedClass->getQualifiedNameAsString() << "', ";
|
|
|
|
// FIXME: Don't use / 8 .
|
|
|
|
Out << MostDerivedClassOffset / 8 << ") in '";
|
|
|
|
Out << LayoutClass->getQualifiedNameAsString();
|
2010-03-11 09:43:12 +03:00
|
|
|
} else {
|
|
|
|
Out << "Vtable for '";
|
|
|
|
Out << MostDerivedClass->getQualifiedNameAsString();
|
2010-02-27 23:39:05 +03:00
|
|
|
}
|
2010-02-11 11:02:13 +03:00
|
|
|
Out << "' (" << Components.size() << " entries).\n";
|
|
|
|
|
2010-02-11 20:18:51 +03:00
|
|
|
// Iterate through the address points and insert them into a new map where
|
|
|
|
// they are keyed by the index and not the base object.
|
|
|
|
// Since an address point can be shared by multiple subobjects, we use an
|
|
|
|
// STL multimap.
|
|
|
|
std::multimap<uint64_t, BaseSubobject> AddressPointsByIndex;
|
2010-03-25 03:35:49 +03:00
|
|
|
for (AddressPointsMapTy::const_iterator I = AddressPoints.begin(),
|
|
|
|
E = AddressPoints.end(); I != E; ++I) {
|
2010-02-11 20:18:51 +03:00
|
|
|
const BaseSubobject& Base = I->first;
|
|
|
|
uint64_t Index = I->second;
|
|
|
|
|
|
|
|
AddressPointsByIndex.insert(std::make_pair(Index, Base));
|
|
|
|
}
|
|
|
|
|
2010-02-11 11:02:13 +03:00
|
|
|
for (unsigned I = 0, E = Components.size(); I != E; ++I) {
|
2010-02-12 10:43:48 +03:00
|
|
|
uint64_t Index = I;
|
2010-02-11 20:18:51 +03:00
|
|
|
|
2010-02-11 11:02:13 +03:00
|
|
|
Out << llvm::format("%4d | ", I);
|
2010-02-11 20:18:51 +03:00
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
const VTableComponent &Component = Components[I];
|
2010-02-11 11:02:13 +03:00
|
|
|
|
|
|
|
// Dump the component.
|
|
|
|
switch (Component.getKind()) {
|
2010-02-17 09:07:19 +03:00
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_VCallOffset:
|
2010-02-17 09:07:19 +03:00
|
|
|
Out << "vcall_offset (" << Component.getVCallOffset() << ")";
|
2010-02-11 11:02:13 +03:00
|
|
|
break;
|
2010-02-16 07:59:55 +03:00
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_VBaseOffset:
|
2010-02-16 07:59:55 +03:00
|
|
|
Out << "vbase_offset (" << Component.getVBaseOffset() << ")";
|
|
|
|
break;
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_OffsetToTop:
|
2010-02-11 11:02:13 +03:00
|
|
|
Out << "offset_to_top (" << Component.getOffsetToTop() << ")";
|
|
|
|
break;
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_RTTI:
|
2010-02-11 11:02:13 +03:00
|
|
|
Out << Component.getRTTIDecl()->getQualifiedNameAsString() << " RTTI";
|
|
|
|
break;
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_FunctionPointer: {
|
2010-02-11 11:02:13 +03:00
|
|
|
const CXXMethodDecl *MD = Component.getFunctionDecl();
|
|
|
|
|
2010-02-11 21:20:28 +03:00
|
|
|
std::string Str =
|
|
|
|
PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
|
|
|
|
MD);
|
|
|
|
Out << Str;
|
2010-02-12 05:38:13 +03:00
|
|
|
if (MD->isPure())
|
|
|
|
Out << " [pure]";
|
|
|
|
|
2010-03-23 07:44:10 +03:00
|
|
|
ThunkInfo Thunk = VTableThunks.lookup(I);
|
2010-03-12 07:54:20 +03:00
|
|
|
if (!Thunk.isEmpty()) {
|
|
|
|
// If this function pointer has a return adjustment, dump it.
|
|
|
|
if (!Thunk.Return.isEmpty()) {
|
|
|
|
Out << "\n [return adjustment: ";
|
|
|
|
Out << Thunk.Return.NonVirtual << " non-virtual";
|
|
|
|
|
|
|
|
if (Thunk.Return.VBaseOffsetOffset) {
|
|
|
|
Out << ", " << Thunk.Return.VBaseOffsetOffset;
|
|
|
|
Out << " vbase offset offset";
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << ']';
|
|
|
|
}
|
2010-02-18 20:00:09 +03:00
|
|
|
|
2010-03-12 07:54:20 +03:00
|
|
|
// If this function pointer has a 'this' pointer adjustment, dump it.
|
|
|
|
if (!Thunk.This.isEmpty()) {
|
|
|
|
Out << "\n [this adjustment: ";
|
|
|
|
Out << Thunk.This.NonVirtual << " non-virtual";
|
|
|
|
|
|
|
|
if (Thunk.This.VCallOffsetOffset) {
|
|
|
|
Out << ", " << Thunk.This.VCallOffsetOffset;
|
|
|
|
Out << " vcall offset offset";
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << ']';
|
|
|
|
}
|
2010-02-14 02:40:17 +03:00
|
|
|
}
|
2010-02-13 23:11:51 +03:00
|
|
|
|
2010-02-11 21:20:28 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_CompleteDtorPointer:
|
|
|
|
case VTableComponent::CK_DeletingDtorPointer: {
|
2010-02-19 09:03:53 +03:00
|
|
|
bool IsComplete =
|
2010-04-10 23:13:06 +04:00
|
|
|
Component.getKind() == VTableComponent::CK_CompleteDtorPointer;
|
2010-02-19 09:03:53 +03:00
|
|
|
|
2010-02-11 21:20:28 +03:00
|
|
|
const CXXDestructorDecl *DD = Component.getDestructorDecl();
|
|
|
|
|
2010-02-19 09:03:53 +03:00
|
|
|
Out << DD->getQualifiedNameAsString();
|
|
|
|
if (IsComplete)
|
|
|
|
Out << "() [complete]";
|
|
|
|
else
|
|
|
|
Out << "() [deleting]";
|
|
|
|
|
2010-02-12 05:38:13 +03:00
|
|
|
if (DD->isPure())
|
|
|
|
Out << " [pure]";
|
|
|
|
|
2010-03-23 07:44:10 +03:00
|
|
|
ThunkInfo Thunk = VTableThunks.lookup(I);
|
2010-03-12 07:54:20 +03:00
|
|
|
if (!Thunk.isEmpty()) {
|
|
|
|
// If this destructor has a 'this' pointer adjustment, dump it.
|
|
|
|
if (!Thunk.This.isEmpty()) {
|
|
|
|
Out << "\n [this adjustment: ";
|
|
|
|
Out << Thunk.This.NonVirtual << " non-virtual";
|
|
|
|
|
|
|
|
if (Thunk.This.VCallOffsetOffset) {
|
|
|
|
Out << ", " << Thunk.This.VCallOffsetOffset;
|
|
|
|
Out << " vcall offset offset";
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << ']';
|
|
|
|
}
|
|
|
|
}
|
2010-02-12 05:38:13 +03:00
|
|
|
|
2010-02-11 11:02:13 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_UnusedFunctionPointer: {
|
2010-02-19 23:08:13 +03:00
|
|
|
const CXXMethodDecl *MD = Component.getUnusedFunctionDecl();
|
|
|
|
|
|
|
|
std::string Str =
|
|
|
|
PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
|
|
|
|
MD);
|
|
|
|
Out << "[unused] " << Str;
|
|
|
|
if (MD->isPure())
|
|
|
|
Out << " [pure]";
|
|
|
|
}
|
|
|
|
|
2010-02-11 11:02:13 +03:00
|
|
|
}
|
2010-02-11 20:18:51 +03:00
|
|
|
|
2010-02-11 11:02:13 +03:00
|
|
|
Out << '\n';
|
2010-02-28 02:58:01 +03:00
|
|
|
|
|
|
|
// Dump the next address point.
|
|
|
|
uint64_t NextIndex = Index + 1;
|
|
|
|
if (AddressPointsByIndex.count(NextIndex)) {
|
|
|
|
if (AddressPointsByIndex.count(NextIndex) == 1) {
|
|
|
|
const BaseSubobject &Base =
|
|
|
|
AddressPointsByIndex.find(NextIndex)->second;
|
|
|
|
|
|
|
|
// FIXME: Instead of dividing by 8, we should be using CharUnits.
|
|
|
|
Out << " -- (" << Base.getBase()->getQualifiedNameAsString();
|
|
|
|
Out << ", " << Base.getBaseOffset() / 8 << ") vtable address --\n";
|
|
|
|
} else {
|
|
|
|
uint64_t BaseOffset =
|
|
|
|
AddressPointsByIndex.lower_bound(NextIndex)->second.getBaseOffset();
|
|
|
|
|
|
|
|
// We store the class names in a set to get a stable order.
|
|
|
|
std::set<std::string> ClassNames;
|
|
|
|
for (std::multimap<uint64_t, BaseSubobject>::const_iterator I =
|
|
|
|
AddressPointsByIndex.lower_bound(NextIndex), E =
|
|
|
|
AddressPointsByIndex.upper_bound(NextIndex); I != E; ++I) {
|
|
|
|
assert(I->second.getBaseOffset() == BaseOffset &&
|
|
|
|
"Invalid base offset!");
|
|
|
|
const CXXRecordDecl *RD = I->second.getBase();
|
|
|
|
ClassNames.insert(RD->getQualifiedNameAsString());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::set<std::string>::const_iterator I = ClassNames.begin(),
|
|
|
|
E = ClassNames.end(); I != E; ++I) {
|
|
|
|
// FIXME: Instead of dividing by 8, we should be using CharUnits.
|
|
|
|
Out << " -- (" << *I;
|
|
|
|
Out << ", " << BaseOffset / 8 << ") vtable address --\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-02-11 11:02:13 +03:00
|
|
|
}
|
2010-02-27 23:39:05 +03:00
|
|
|
|
|
|
|
Out << '\n';
|
2010-03-11 09:43:12 +03:00
|
|
|
|
2010-03-18 05:44:19 +03:00
|
|
|
if (isBuildingConstructorVtable())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (MostDerivedClass->getNumVBases()) {
|
2010-03-11 09:43:12 +03:00
|
|
|
// We store the virtual base class names and their offsets in a map to get
|
|
|
|
// a stable order.
|
|
|
|
|
2010-03-18 05:44:19 +03:00
|
|
|
std::map<std::string, int64_t> ClassNamesAndOffsets;
|
2010-03-11 09:43:12 +03:00
|
|
|
for (VBaseOffsetOffsetsMapTy::const_iterator I = VBaseOffsetOffsets.begin(),
|
|
|
|
E = VBaseOffsetOffsets.end(); I != E; ++I) {
|
|
|
|
std::string ClassName = I->first->getQualifiedNameAsString();
|
|
|
|
int64_t OffsetOffset = I->second;
|
|
|
|
ClassNamesAndOffsets.insert(std::make_pair(ClassName, OffsetOffset));
|
|
|
|
}
|
2010-03-18 05:44:19 +03:00
|
|
|
|
|
|
|
Out << "Virtual base offset offsets for '";
|
|
|
|
Out << MostDerivedClass->getQualifiedNameAsString() << "' (";
|
|
|
|
Out << ClassNamesAndOffsets.size();
|
|
|
|
Out << (ClassNamesAndOffsets.size() == 1 ? " entry" : " entries") << ").\n";
|
2010-03-11 09:43:12 +03:00
|
|
|
|
|
|
|
for (std::map<std::string, int64_t>::const_iterator I =
|
|
|
|
ClassNamesAndOffsets.begin(), E = ClassNamesAndOffsets.end();
|
|
|
|
I != E; ++I)
|
|
|
|
Out << " " << I->first << " | " << I->second << '\n';
|
|
|
|
|
|
|
|
Out << "\n";
|
|
|
|
}
|
2010-03-18 05:44:19 +03:00
|
|
|
|
2010-03-23 07:44:10 +03:00
|
|
|
if (!Thunks.empty()) {
|
2010-03-18 05:44:19 +03:00
|
|
|
// We store the method names in a map to get a stable order.
|
|
|
|
std::map<std::string, const CXXMethodDecl *> MethodNamesAndDecls;
|
|
|
|
|
2010-03-23 07:59:02 +03:00
|
|
|
for (ThunksMapTy::const_iterator I = Thunks.begin(), E = Thunks.end();
|
2010-03-23 07:44:10 +03:00
|
|
|
I != E; ++I) {
|
2010-03-18 05:44:19 +03:00
|
|
|
const CXXMethodDecl *MD = I->first;
|
|
|
|
std::string MethodName =
|
|
|
|
PredefinedExpr::ComputeName(PredefinedExpr::PrettyFunctionNoVirtual,
|
|
|
|
MD);
|
|
|
|
|
|
|
|
MethodNamesAndDecls.insert(std::make_pair(MethodName, MD));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::map<std::string, const CXXMethodDecl *>::const_iterator I =
|
|
|
|
MethodNamesAndDecls.begin(), E = MethodNamesAndDecls.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const std::string &MethodName = I->first;
|
|
|
|
const CXXMethodDecl *MD = I->second;
|
2010-03-22 19:30:44 +03:00
|
|
|
|
2010-03-23 07:59:02 +03:00
|
|
|
ThunkInfoVectorTy ThunksVector = Thunks[MD];
|
2010-03-22 19:30:44 +03:00
|
|
|
std::sort(ThunksVector.begin(), ThunksVector.end());
|
2010-03-18 05:44:19 +03:00
|
|
|
|
|
|
|
Out << "Thunks for '" << MethodName << "' (" << ThunksVector.size();
|
|
|
|
Out << (ThunksVector.size() == 1 ? " entry" : " entries") << ").\n";
|
|
|
|
|
|
|
|
for (unsigned I = 0, E = ThunksVector.size(); I != E; ++I) {
|
|
|
|
const ThunkInfo &Thunk = ThunksVector[I];
|
|
|
|
|
|
|
|
Out << llvm::format("%4d | ", I);
|
|
|
|
|
2010-03-22 19:10:59 +03:00
|
|
|
// If this function pointer has a return pointer adjustment, dump it.
|
|
|
|
if (!Thunk.Return.isEmpty()) {
|
|
|
|
Out << "return adjustment: " << Thunk.This.NonVirtual;
|
|
|
|
Out << " non-virtual";
|
|
|
|
if (Thunk.Return.VBaseOffsetOffset) {
|
|
|
|
Out << ", " << Thunk.Return.VBaseOffsetOffset;
|
|
|
|
Out << " vbase offset offset";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Thunk.This.isEmpty())
|
|
|
|
Out << "\n ";
|
|
|
|
}
|
|
|
|
|
2010-03-18 05:44:19 +03:00
|
|
|
// If this function pointer has a 'this' pointer adjustment, dump it.
|
|
|
|
if (!Thunk.This.isEmpty()) {
|
2010-03-22 19:10:59 +03:00
|
|
|
Out << "this adjustment: ";
|
|
|
|
Out << Thunk.This.NonVirtual << " non-virtual";
|
2010-03-18 05:44:19 +03:00
|
|
|
|
|
|
|
if (Thunk.This.VCallOffsetOffset) {
|
|
|
|
Out << ", " << Thunk.This.VCallOffsetOffset;
|
2010-03-22 19:10:59 +03:00
|
|
|
Out << " vcall offset offset";
|
2010-03-18 05:44:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << '\n';
|
|
|
|
}
|
2010-03-22 23:06:40 +03:00
|
|
|
|
|
|
|
Out << '\n';
|
|
|
|
|
2010-03-18 05:44:19 +03:00
|
|
|
}
|
|
|
|
}
|
2010-02-11 11:02:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-03-23 07:11:45 +03:00
|
|
|
void CodeGenVTables::ComputeMethodVtableIndices(const CXXRecordDecl *RD) {
|
2009-11-27 23:47:55 +03:00
|
|
|
|
|
|
|
// Itanium C++ ABI 2.5.2:
|
2010-02-02 06:37:46 +03:00
|
|
|
// The order of the virtual function pointers in a virtual table is the
|
|
|
|
// order of declaration of the corresponding member functions in the class.
|
2009-11-27 23:47:55 +03:00
|
|
|
//
|
2010-02-02 06:37:46 +03:00
|
|
|
// There is an entry for any virtual function declared in a class,
|
|
|
|
// whether it is a new function or overrides a base class function,
|
|
|
|
// unless it overrides a function from the primary base, and conversion
|
|
|
|
// between their return types does not require an adjustment.
|
2009-11-27 23:47:55 +03:00
|
|
|
|
|
|
|
int64_t CurrentIndex = 0;
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
|
|
|
|
|
|
|
|
if (PrimaryBase) {
|
2009-11-30 22:43:26 +03:00
|
|
|
assert(PrimaryBase->isDefinition() &&
|
|
|
|
"Should have the definition decl of the primary base!");
|
2009-11-27 23:47:55 +03:00
|
|
|
|
|
|
|
// Since the record decl shares its vtable pointer with the primary base
|
|
|
|
// we need to start counting at the end of the primary base's vtable.
|
|
|
|
CurrentIndex = getNumVirtualFunctionPointers(PrimaryBase);
|
|
|
|
}
|
2009-12-15 06:31:17 +03:00
|
|
|
|
|
|
|
// Collect all the primary bases, so we can check whether methods override
|
|
|
|
// a method from the base.
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableBuilder::PrimaryBasesSetVectorTy PrimaryBases;
|
2009-12-15 06:31:17 +03:00
|
|
|
for (ASTRecordLayout::primary_base_info_iterator
|
|
|
|
I = Layout.primary_base_begin(), E = Layout.primary_base_end();
|
|
|
|
I != E; ++I)
|
|
|
|
PrimaryBases.insert((*I).getBase());
|
|
|
|
|
2009-11-27 23:47:55 +03:00
|
|
|
const CXXDestructorDecl *ImplicitVirtualDtor = 0;
|
|
|
|
|
|
|
|
for (CXXRecordDecl::method_iterator i = RD->method_begin(),
|
|
|
|
e = RD->method_end(); i != e; ++i) {
|
|
|
|
const CXXMethodDecl *MD = *i;
|
|
|
|
|
|
|
|
// We only want virtual methods.
|
|
|
|
if (!MD->isVirtual())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check if this method overrides a method in the primary base.
|
2010-02-12 08:25:12 +03:00
|
|
|
if (const CXXMethodDecl *OverriddenMD =
|
2010-02-27 09:38:03 +03:00
|
|
|
OverridesMethodInBases(MD, PrimaryBases)) {
|
2010-02-12 08:25:12 +03:00
|
|
|
// Check if converting from the return type of the method to the
|
|
|
|
// return type of the overridden method requires conversion.
|
2010-02-14 02:17:31 +03:00
|
|
|
if (ComputeReturnAdjustmentBaseOffset(CGM.getContext(), MD,
|
|
|
|
OverriddenMD).isEmpty()) {
|
2010-02-12 08:25:12 +03:00
|
|
|
// This index is shared between the index in the vtable of the primary
|
|
|
|
// base class.
|
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
|
|
|
|
const CXXDestructorDecl *OverriddenDD =
|
|
|
|
cast<CXXDestructorDecl>(OverriddenMD);
|
2009-11-27 23:47:55 +03:00
|
|
|
|
2010-02-12 08:25:12 +03:00
|
|
|
// Add both the complete and deleting entries.
|
|
|
|
MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] =
|
|
|
|
getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Complete));
|
|
|
|
MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] =
|
|
|
|
getMethodVtableIndex(GlobalDecl(OverriddenDD, Dtor_Deleting));
|
|
|
|
} else {
|
|
|
|
MethodVtableIndices[MD] = getMethodVtableIndex(OverriddenMD);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need to add an entry for this method.
|
2010-02-12 21:14:46 +03:00
|
|
|
continue;
|
2009-11-27 23:47:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) {
|
|
|
|
if (MD->isImplicit()) {
|
|
|
|
assert(!ImplicitVirtualDtor &&
|
|
|
|
"Did already see an implicit virtual dtor!");
|
|
|
|
ImplicitVirtualDtor = DD;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the complete dtor.
|
|
|
|
MethodVtableIndices[GlobalDecl(DD, Dtor_Complete)] = CurrentIndex++;
|
|
|
|
|
|
|
|
// Add the deleting dtor.
|
|
|
|
MethodVtableIndices[GlobalDecl(DD, Dtor_Deleting)] = CurrentIndex++;
|
|
|
|
} else {
|
|
|
|
// Add the entry.
|
|
|
|
MethodVtableIndices[MD] = CurrentIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImplicitVirtualDtor) {
|
|
|
|
// Itanium C++ ABI 2.5.2:
|
2010-02-16 07:49:44 +03:00
|
|
|
// If a class has an implicitly-defined virtual destructor,
|
|
|
|
// its entries come after the declared virtual function pointers.
|
2009-11-27 23:47:55 +03:00
|
|
|
|
|
|
|
// Add the complete dtor.
|
|
|
|
MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Complete)] =
|
|
|
|
CurrentIndex++;
|
|
|
|
|
|
|
|
// Add the deleting dtor.
|
|
|
|
MethodVtableIndices[GlobalDecl(ImplicitVirtualDtor, Dtor_Deleting)] =
|
|
|
|
CurrentIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
NumVirtualFunctionPointers[RD] = CurrentIndex;
|
|
|
|
}
|
|
|
|
|
2010-03-23 07:11:45 +03:00
|
|
|
uint64_t CodeGenVTables::getNumVirtualFunctionPointers(const CXXRecordDecl *RD) {
|
2009-11-27 23:47:55 +03:00
|
|
|
llvm::DenseMap<const CXXRecordDecl *, uint64_t>::iterator I =
|
|
|
|
NumVirtualFunctionPointers.find(RD);
|
|
|
|
if (I != NumVirtualFunctionPointers.end())
|
|
|
|
return I->second;
|
|
|
|
|
|
|
|
ComputeMethodVtableIndices(RD);
|
|
|
|
|
|
|
|
I = NumVirtualFunctionPointers.find(RD);
|
|
|
|
assert(I != NumVirtualFunctionPointers.end() && "Did not find entry!");
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2010-03-23 07:11:45 +03:00
|
|
|
uint64_t CodeGenVTables::getMethodVtableIndex(GlobalDecl GD) {
|
2009-11-13 20:08:56 +03:00
|
|
|
MethodVtableIndicesTy::iterator I = MethodVtableIndices.find(GD);
|
2009-10-12 02:13:54 +04:00
|
|
|
if (I != MethodVtableIndices.end())
|
|
|
|
return I->second;
|
|
|
|
|
2009-11-13 20:08:56 +03:00
|
|
|
const CXXRecordDecl *RD = cast<CXXMethodDecl>(GD.getDecl())->getParent();
|
2009-11-27 23:47:55 +03:00
|
|
|
|
|
|
|
ComputeMethodVtableIndices(RD);
|
|
|
|
|
2009-11-13 20:08:56 +03:00
|
|
|
I = MethodVtableIndices.find(GD);
|
2009-10-12 02:13:54 +04:00
|
|
|
assert(I != MethodVtableIndices.end() && "Did not find index!");
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2010-03-23 07:11:45 +03:00
|
|
|
int64_t CodeGenVTables::getVirtualBaseOffsetOffset(const CXXRecordDecl *RD,
|
|
|
|
const CXXRecordDecl *VBase) {
|
2009-10-12 02:13:54 +04:00
|
|
|
ClassPairTy ClassPair(RD, VBase);
|
|
|
|
|
2010-03-11 10:15:17 +03:00
|
|
|
VirtualBaseClassOffsetOffsetsMapTy::iterator I =
|
|
|
|
VirtualBaseClassOffsetOffsets.find(ClassPair);
|
|
|
|
if (I != VirtualBaseClassOffsetOffsets.end())
|
2009-10-12 02:13:54 +04:00
|
|
|
return I->second;
|
|
|
|
|
2010-03-11 19:06:20 +03:00
|
|
|
VCallAndVBaseOffsetBuilder Builder(RD, RD, /*FinalOverriders=*/0,
|
|
|
|
BaseSubobject(RD, 0),
|
|
|
|
/*BaseIsVirtual=*/false,
|
|
|
|
/*OffsetInLayoutClass=*/0);
|
|
|
|
|
|
|
|
for (VCallAndVBaseOffsetBuilder::VBaseOffsetOffsetsMapTy::const_iterator I =
|
|
|
|
Builder.getVBaseOffsetOffsets().begin(),
|
|
|
|
E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) {
|
2009-10-12 02:13:54 +04:00
|
|
|
// Insert all types.
|
|
|
|
ClassPairTy ClassPair(RD, I->first);
|
|
|
|
|
2010-03-11 10:15:17 +03:00
|
|
|
VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second));
|
2009-10-12 02:13:54 +04:00
|
|
|
}
|
|
|
|
|
2010-03-11 10:15:17 +03:00
|
|
|
I = VirtualBaseClassOffsetOffsets.find(ClassPair);
|
2010-03-11 19:06:20 +03:00
|
|
|
|
2010-02-13 23:11:51 +03:00
|
|
|
// FIXME: The assertion below assertion currently fails with the old vtable
|
|
|
|
/// layout code if there is a non-virtual thunk adjustment in a vtable.
|
|
|
|
// Once the new layout is in place, this return should be removed.
|
2010-03-11 10:15:17 +03:00
|
|
|
if (I == VirtualBaseClassOffsetOffsets.end())
|
2010-02-13 23:11:51 +03:00
|
|
|
return 0;
|
|
|
|
|
2010-03-11 10:15:17 +03:00
|
|
|
assert(I != VirtualBaseClassOffsetOffsets.end() && "Did not find index!");
|
2009-10-12 02:13:54 +04:00
|
|
|
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2010-03-29 06:08:26 +04:00
|
|
|
uint64_t
|
|
|
|
CodeGenVTables::getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD) {
|
2010-04-08 20:18:36 +04:00
|
|
|
assert(AddressPoints.count(std::make_pair(RD, Base)) &&
|
|
|
|
"Did not find address point!");
|
|
|
|
|
2010-03-29 07:38:52 +04:00
|
|
|
uint64_t AddressPoint = AddressPoints.lookup(std::make_pair(RD, Base));
|
|
|
|
assert(AddressPoint && "Address point must not be zero!");
|
|
|
|
|
2010-03-29 06:08:26 +04:00
|
|
|
return AddressPoint;
|
|
|
|
}
|
|
|
|
|
2010-03-23 20:17:29 +03:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfThunk(GlobalDecl GD,
|
|
|
|
const ThunkInfo &Thunk) {
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
|
|
|
|
// Compute the mangled name.
|
|
|
|
llvm::SmallString<256> Name;
|
|
|
|
if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD))
|
|
|
|
getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), Thunk.This,
|
|
|
|
Name);
|
|
|
|
else
|
|
|
|
getMangleContext().mangleThunk(MD, Thunk, Name);
|
|
|
|
|
|
|
|
const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD);
|
|
|
|
return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl());
|
|
|
|
}
|
|
|
|
|
2010-03-24 03:39:18 +03:00
|
|
|
static llvm::Value *PerformTypeAdjustment(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *Ptr,
|
|
|
|
int64_t NonVirtualAdjustment,
|
|
|
|
int64_t VirtualAdjustment) {
|
|
|
|
if (!NonVirtualAdjustment && !VirtualAdjustment)
|
|
|
|
return Ptr;
|
|
|
|
|
|
|
|
const llvm::Type *Int8PtrTy =
|
|
|
|
llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
|
|
|
|
|
|
|
|
llvm::Value *V = CGF.Builder.CreateBitCast(Ptr, Int8PtrTy);
|
|
|
|
|
|
|
|
if (NonVirtualAdjustment) {
|
|
|
|
// Do the non-virtual adjustment.
|
|
|
|
V = CGF.Builder.CreateConstInBoundsGEP1_64(V, NonVirtualAdjustment);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VirtualAdjustment) {
|
|
|
|
const llvm::Type *PtrDiffTy =
|
|
|
|
CGF.ConvertType(CGF.getContext().getPointerDiffType());
|
|
|
|
|
|
|
|
// Do the virtual adjustment.
|
|
|
|
llvm::Value *VTablePtrPtr =
|
|
|
|
CGF.Builder.CreateBitCast(V, Int8PtrTy->getPointerTo());
|
|
|
|
|
|
|
|
llvm::Value *VTablePtr = CGF.Builder.CreateLoad(VTablePtrPtr);
|
|
|
|
|
|
|
|
llvm::Value *OffsetPtr =
|
|
|
|
CGF.Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment);
|
|
|
|
|
|
|
|
OffsetPtr = CGF.Builder.CreateBitCast(OffsetPtr, PtrDiffTy->getPointerTo());
|
|
|
|
|
|
|
|
// Load the adjustment offset from the vtable.
|
|
|
|
llvm::Value *Offset = CGF.Builder.CreateLoad(OffsetPtr);
|
|
|
|
|
|
|
|
// Adjust our pointer.
|
|
|
|
V = CGF.Builder.CreateInBoundsGEP(V, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cast back to the original type.
|
|
|
|
return CGF.Builder.CreateBitCast(V, Ptr->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::GenerateThunk(llvm::Function *Fn, GlobalDecl GD,
|
|
|
|
const ThunkInfo &Thunk) {
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
|
|
|
|
QualType ResultType = FPT->getResultType();
|
|
|
|
QualType ThisType = MD->getThisType(getContext());
|
|
|
|
|
|
|
|
FunctionArgList FunctionArgs;
|
|
|
|
|
|
|
|
// FIXME: It would be nice if more of this code could be shared with
|
|
|
|
// CodeGenFunction::GenerateCode.
|
|
|
|
|
|
|
|
// Create the implicit 'this' parameter declaration.
|
|
|
|
CXXThisDecl = ImplicitParamDecl::Create(getContext(), 0,
|
|
|
|
MD->getLocation(),
|
|
|
|
&getContext().Idents.get("this"),
|
|
|
|
ThisType);
|
|
|
|
|
|
|
|
// Add the 'this' parameter.
|
|
|
|
FunctionArgs.push_back(std::make_pair(CXXThisDecl, CXXThisDecl->getType()));
|
|
|
|
|
|
|
|
// Add the rest of the parameters.
|
|
|
|
for (FunctionDecl::param_const_iterator I = MD->param_begin(),
|
|
|
|
E = MD->param_end(); I != E; ++I) {
|
|
|
|
ParmVarDecl *Param = *I;
|
|
|
|
|
|
|
|
FunctionArgs.push_back(std::make_pair(Param, Param->getType()));
|
|
|
|
}
|
|
|
|
|
|
|
|
StartFunction(GlobalDecl(), ResultType, Fn, FunctionArgs, SourceLocation());
|
|
|
|
|
|
|
|
// Adjust the 'this' pointer if necessary.
|
|
|
|
llvm::Value *AdjustedThisPtr =
|
|
|
|
PerformTypeAdjustment(*this, LoadCXXThis(),
|
|
|
|
Thunk.This.NonVirtual,
|
|
|
|
Thunk.This.VCallOffsetOffset);
|
|
|
|
|
|
|
|
CallArgList CallArgs;
|
|
|
|
|
|
|
|
// Add our adjusted 'this' pointer.
|
|
|
|
CallArgs.push_back(std::make_pair(RValue::get(AdjustedThisPtr), ThisType));
|
|
|
|
|
|
|
|
// Add the rest of the parameters.
|
|
|
|
for (FunctionDecl::param_const_iterator I = MD->param_begin(),
|
|
|
|
E = MD->param_end(); I != E; ++I) {
|
|
|
|
ParmVarDecl *Param = *I;
|
|
|
|
QualType ArgType = Param->getType();
|
|
|
|
|
|
|
|
// FIXME: Declaring a DeclRefExpr on the stack is kinda icky.
|
|
|
|
DeclRefExpr ArgExpr(Param, ArgType.getNonReferenceType(), SourceLocation());
|
|
|
|
CallArgs.push_back(std::make_pair(EmitCallArg(&ArgExpr, ArgType), ArgType));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get our callee.
|
|
|
|
const llvm::Type *Ty =
|
|
|
|
CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD),
|
|
|
|
FPT->isVariadic());
|
|
|
|
llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty);
|
|
|
|
|
|
|
|
const CGFunctionInfo &FnInfo =
|
2010-03-31 00:24:48 +04:00
|
|
|
CGM.getTypes().getFunctionInfo(ResultType, CallArgs,
|
|
|
|
FPT->getExtInfo());
|
2010-03-24 03:39:18 +03:00
|
|
|
|
|
|
|
// Now emit our call.
|
|
|
|
RValue RV = EmitCall(FnInfo, Callee, ReturnValueSlot(), CallArgs, MD);
|
|
|
|
|
|
|
|
if (!Thunk.Return.isEmpty()) {
|
|
|
|
// Emit the return adjustment.
|
|
|
|
bool NullCheckValue = !ResultType->isReferenceType();
|
|
|
|
|
|
|
|
llvm::BasicBlock *AdjustNull = 0;
|
|
|
|
llvm::BasicBlock *AdjustNotNull = 0;
|
|
|
|
llvm::BasicBlock *AdjustEnd = 0;
|
|
|
|
|
|
|
|
llvm::Value *ReturnValue = RV.getScalarVal();
|
|
|
|
|
|
|
|
if (NullCheckValue) {
|
|
|
|
AdjustNull = createBasicBlock("adjust.null");
|
|
|
|
AdjustNotNull = createBasicBlock("adjust.notnull");
|
|
|
|
AdjustEnd = createBasicBlock("adjust.end");
|
|
|
|
|
|
|
|
llvm::Value *IsNull = Builder.CreateIsNull(ReturnValue);
|
|
|
|
Builder.CreateCondBr(IsNull, AdjustNull, AdjustNotNull);
|
|
|
|
EmitBlock(AdjustNotNull);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue = PerformTypeAdjustment(*this, ReturnValue,
|
|
|
|
Thunk.Return.NonVirtual,
|
|
|
|
Thunk.Return.VBaseOffsetOffset);
|
|
|
|
|
|
|
|
if (NullCheckValue) {
|
|
|
|
Builder.CreateBr(AdjustEnd);
|
|
|
|
EmitBlock(AdjustNull);
|
|
|
|
Builder.CreateBr(AdjustEnd);
|
|
|
|
EmitBlock(AdjustEnd);
|
|
|
|
|
|
|
|
llvm::PHINode *PHI = Builder.CreatePHI(ReturnValue->getType());
|
|
|
|
PHI->reserveOperandSpace(2);
|
|
|
|
PHI->addIncoming(ReturnValue, AdjustNotNull);
|
|
|
|
PHI->addIncoming(llvm::Constant::getNullValue(ReturnValue->getType()),
|
|
|
|
AdjustNull);
|
|
|
|
ReturnValue = PHI;
|
|
|
|
}
|
|
|
|
|
|
|
|
RV = RValue::get(ReturnValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ResultType->isVoidType())
|
|
|
|
EmitReturnOfRValue(RV, ResultType);
|
|
|
|
|
|
|
|
FinishFunction();
|
|
|
|
|
|
|
|
// Destroy the 'this' declaration.
|
|
|
|
CXXThisDecl->Destroy(getContext());
|
|
|
|
|
|
|
|
// Set the right linkage.
|
2010-03-27 23:50:27 +03:00
|
|
|
Fn->setLinkage(CGM.getFunctionLinkage(MD));
|
2010-03-24 03:39:18 +03:00
|
|
|
|
|
|
|
// Set the right visibility.
|
|
|
|
CGM.setGlobalVisibility(Fn, MD);
|
|
|
|
}
|
|
|
|
|
2010-03-23 19:36:50 +03:00
|
|
|
void CodeGenVTables::EmitThunk(GlobalDecl GD, const ThunkInfo &Thunk)
|
|
|
|
{
|
2010-03-24 03:35:44 +03:00
|
|
|
llvm::Constant *Entry = CGM.GetAddrOfThunk(GD, Thunk);
|
2010-03-23 21:18:41 +03:00
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
|
|
|
|
// Strip off a bitcast if we got one back.
|
2010-03-24 03:35:44 +03:00
|
|
|
if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
|
2010-03-23 21:18:41 +03:00
|
|
|
assert(CE->getOpcode() == llvm::Instruction::BitCast);
|
2010-03-24 03:35:44 +03:00
|
|
|
Entry = CE->getOperand(0);
|
2010-03-23 21:18:41 +03:00
|
|
|
}
|
2010-03-23 20:17:29 +03:00
|
|
|
|
2010-03-23 21:18:41 +03:00
|
|
|
// There's already a declaration with the same name, check if it has the same
|
|
|
|
// type or if we need to replace it.
|
2010-03-24 03:35:44 +03:00
|
|
|
if (cast<llvm::GlobalValue>(Entry)->getType()->getElementType() !=
|
|
|
|
CGM.getTypes().GetFunctionTypeForVtable(MD)) {
|
|
|
|
llvm::GlobalValue *OldThunkFn = cast<llvm::GlobalValue>(Entry);
|
2010-03-23 21:18:41 +03:00
|
|
|
|
|
|
|
// If the types mismatch then we have to rewrite the definition.
|
|
|
|
assert(OldThunkFn->isDeclaration() &&
|
|
|
|
"Shouldn't replace non-declaration");
|
|
|
|
|
|
|
|
// Remove the name from the old thunk function and get a new thunk.
|
|
|
|
OldThunkFn->setName(llvm::StringRef());
|
2010-03-24 03:35:44 +03:00
|
|
|
Entry = CGM.GetAddrOfThunk(GD, Thunk);
|
2010-03-23 21:18:41 +03:00
|
|
|
|
|
|
|
// If needed, replace the old thunk with a bitcast.
|
|
|
|
if (!OldThunkFn->use_empty()) {
|
|
|
|
llvm::Constant *NewPtrForOldDecl =
|
2010-03-24 03:35:44 +03:00
|
|
|
llvm::ConstantExpr::getBitCast(Entry, OldThunkFn->getType());
|
2010-03-23 21:18:41 +03:00
|
|
|
OldThunkFn->replaceAllUsesWith(NewPtrForOldDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the old thunk.
|
|
|
|
OldThunkFn->eraseFromParent();
|
|
|
|
}
|
2010-03-24 03:39:18 +03:00
|
|
|
|
|
|
|
// Actually generate the thunk body.
|
|
|
|
llvm::Function *ThunkFn = cast<llvm::Function>(Entry);
|
|
|
|
CodeGenFunction(CGM).GenerateThunk(ThunkFn, GD, Thunk);
|
2010-03-23 19:36:50 +03:00
|
|
|
}
|
|
|
|
|
2010-03-23 07:59:02 +03:00
|
|
|
void CodeGenVTables::EmitThunks(GlobalDecl GD)
|
|
|
|
{
|
2010-03-23 19:36:50 +03:00
|
|
|
const CXXMethodDecl *MD =
|
|
|
|
cast<CXXMethodDecl>(GD.getDecl())->getCanonicalDecl();
|
|
|
|
|
|
|
|
// We don't need to generate thunks for the base destructor.
|
|
|
|
if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const CXXRecordDecl *RD = MD->getParent();
|
|
|
|
|
2010-03-24 19:42:11 +03:00
|
|
|
// Compute VTable related info for this class.
|
|
|
|
ComputeVTableRelatedInformation(RD);
|
|
|
|
|
2010-03-23 19:36:50 +03:00
|
|
|
ThunksMapTy::const_iterator I = Thunks.find(MD);
|
|
|
|
if (I == Thunks.end()) {
|
2010-03-24 19:42:11 +03:00
|
|
|
// We did not find a thunk for this method.
|
|
|
|
return;
|
2010-03-23 19:36:50 +03:00
|
|
|
}
|
2010-03-24 19:42:11 +03:00
|
|
|
|
2010-03-23 19:36:50 +03:00
|
|
|
const ThunkInfoVectorTy &ThunkInfoVector = I->second;
|
|
|
|
for (unsigned I = 0, E = ThunkInfoVector.size(); I != E; ++I)
|
|
|
|
EmitThunk(GD, ThunkInfoVector[I]);
|
2010-03-23 07:59:02 +03:00
|
|
|
}
|
|
|
|
|
2010-03-24 19:42:11 +03:00
|
|
|
void CodeGenVTables::ComputeVTableRelatedInformation(const CXXRecordDecl *RD) {
|
|
|
|
uint64_t *&LayoutData = VTableLayoutMap[RD];
|
|
|
|
|
|
|
|
// Check if we've computed this information before.
|
|
|
|
if (LayoutData)
|
|
|
|
return;
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableBuilder Builder(*this, RD, 0, /*MostDerivedClassIsVirtual=*/0, RD);
|
2010-03-24 19:42:11 +03:00
|
|
|
|
|
|
|
// Add the VTable layout.
|
|
|
|
uint64_t NumVTableComponents = Builder.getNumVTableComponents();
|
|
|
|
LayoutData = new uint64_t[NumVTableComponents + 1];
|
|
|
|
|
|
|
|
// Store the number of components.
|
|
|
|
LayoutData[0] = NumVTableComponents;
|
|
|
|
|
|
|
|
// Store the components.
|
|
|
|
std::copy(Builder.vtable_components_data_begin(),
|
|
|
|
Builder.vtable_components_data_end(),
|
|
|
|
&LayoutData[1]);
|
|
|
|
|
|
|
|
// Add the known thunks.
|
|
|
|
Thunks.insert(Builder.thunks_begin(), Builder.thunks_end());
|
|
|
|
|
2010-03-29 05:38:05 +04:00
|
|
|
// Add the thunks needed in this vtable.
|
|
|
|
assert(!VTableThunksMap.count(RD) &&
|
|
|
|
"Thunks already exists for this vtable!");
|
|
|
|
|
|
|
|
VTableThunksTy &VTableThunks = VTableThunksMap[RD];
|
|
|
|
VTableThunks.append(Builder.vtable_thunks_begin(),
|
|
|
|
Builder.vtable_thunks_end());
|
|
|
|
|
|
|
|
// Sort them.
|
|
|
|
std::sort(VTableThunks.begin(), VTableThunks.end());
|
|
|
|
|
2010-03-25 03:51:13 +03:00
|
|
|
// Add the address points.
|
2010-04-10 23:13:06 +04:00
|
|
|
for (VTableBuilder::AddressPointsMapTy::const_iterator I =
|
2010-03-25 03:51:13 +03:00
|
|
|
Builder.address_points_begin(), E = Builder.address_points_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
|
|
|
|
uint64_t &AddressPoint = AddressPoints[std::make_pair(RD, I->first)];
|
|
|
|
|
|
|
|
// Check if we already have the address points for this base.
|
|
|
|
assert(!AddressPoint && "Address point already exists for this base!");
|
|
|
|
|
|
|
|
AddressPoint = I->second;
|
|
|
|
}
|
|
|
|
|
2010-03-24 19:42:11 +03:00
|
|
|
// If we don't have the vbase information for this class, insert it.
|
|
|
|
// getVirtualBaseOffsetOffset will compute it separately without computing
|
|
|
|
// the rest of the vtable related information.
|
|
|
|
if (!RD->getNumVBases())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const RecordType *VBaseRT =
|
|
|
|
RD->vbases_begin()->getType()->getAs<RecordType>();
|
|
|
|
const CXXRecordDecl *VBase = cast<CXXRecordDecl>(VBaseRT->getDecl());
|
|
|
|
|
|
|
|
if (VirtualBaseClassOffsetOffsets.count(std::make_pair(RD, VBase)))
|
|
|
|
return;
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
for (VTableBuilder::VBaseOffsetOffsetsMapTy::const_iterator I =
|
2010-03-24 19:42:11 +03:00
|
|
|
Builder.getVBaseOffsetOffsets().begin(),
|
|
|
|
E = Builder.getVBaseOffsetOffsets().end(); I != E; ++I) {
|
|
|
|
// Insert all types.
|
|
|
|
ClassPairTy ClassPair(RD, I->first);
|
|
|
|
|
|
|
|
VirtualBaseClassOffsetOffsets.insert(std::make_pair(ClassPair, I->second));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-25 18:26:28 +03:00
|
|
|
llvm::Constant *
|
|
|
|
CodeGenVTables::CreateVTableInitializer(const CXXRecordDecl *RD,
|
|
|
|
const uint64_t *Components,
|
|
|
|
unsigned NumComponents,
|
|
|
|
const VTableThunksTy &VTableThunks) {
|
|
|
|
llvm::SmallVector<llvm::Constant *, 64> Inits;
|
|
|
|
|
|
|
|
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
|
|
|
|
|
2010-03-25 19:49:53 +03:00
|
|
|
const llvm::Type *PtrDiffTy =
|
|
|
|
CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
|
|
|
|
|
|
|
|
QualType ClassType = CGM.getContext().getTagDeclType(RD);
|
|
|
|
llvm::Constant *RTTI = CGM.GetAddrOfRTTIDescriptor(ClassType);
|
|
|
|
|
|
|
|
unsigned NextVTableThunkIndex = 0;
|
|
|
|
|
2010-03-29 09:40:50 +04:00
|
|
|
llvm::Constant* PureVirtualFn = 0;
|
|
|
|
|
2010-03-25 18:26:28 +03:00
|
|
|
for (unsigned I = 0; I != NumComponents; ++I) {
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableComponent Component =
|
|
|
|
VTableComponent::getFromOpaqueInteger(Components[I]);
|
2010-03-25 19:49:53 +03:00
|
|
|
|
|
|
|
llvm::Constant *Init = 0;
|
|
|
|
|
|
|
|
switch (Component.getKind()) {
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_VCallOffset:
|
2010-03-25 19:49:53 +03:00
|
|
|
Init = llvm::ConstantInt::get(PtrDiffTy, Component.getVCallOffset());
|
|
|
|
Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
|
|
|
|
break;
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_VBaseOffset:
|
2010-03-25 19:49:53 +03:00
|
|
|
Init = llvm::ConstantInt::get(PtrDiffTy, Component.getVBaseOffset());
|
|
|
|
Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
|
|
|
|
break;
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_OffsetToTop:
|
2010-03-25 19:49:53 +03:00
|
|
|
Init = llvm::ConstantInt::get(PtrDiffTy, Component.getOffsetToTop());
|
|
|
|
Init = llvm::ConstantExpr::getIntToPtr(Init, Int8PtrTy);
|
|
|
|
break;
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_RTTI:
|
2010-03-25 19:49:53 +03:00
|
|
|
Init = llvm::ConstantExpr::getBitCast(RTTI, Int8PtrTy);
|
|
|
|
break;
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_FunctionPointer:
|
|
|
|
case VTableComponent::CK_CompleteDtorPointer:
|
|
|
|
case VTableComponent::CK_DeletingDtorPointer: {
|
2010-03-25 19:49:53 +03:00
|
|
|
GlobalDecl GD;
|
|
|
|
|
|
|
|
// Get the right global decl.
|
|
|
|
switch (Component.getKind()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unexpected vtable component kind");
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_FunctionPointer:
|
2010-03-25 19:49:53 +03:00
|
|
|
GD = Component.getFunctionDecl();
|
|
|
|
break;
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_CompleteDtorPointer:
|
2010-03-25 19:49:53 +03:00
|
|
|
GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Complete);
|
|
|
|
break;
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_DeletingDtorPointer:
|
2010-03-25 19:49:53 +03:00
|
|
|
GD = GlobalDecl(Component.getDestructorDecl(), Dtor_Deleting);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-03-29 09:40:50 +04:00
|
|
|
if (cast<CXXMethodDecl>(GD.getDecl())->isPure()) {
|
|
|
|
// We have a pure virtual member function.
|
|
|
|
if (!PureVirtualFn) {
|
|
|
|
const llvm::FunctionType *Ty =
|
|
|
|
llvm::FunctionType::get(llvm::Type::getVoidTy(CGM.getLLVMContext()),
|
|
|
|
/*isVarArg=*/false);
|
|
|
|
PureVirtualFn =
|
|
|
|
CGM.CreateRuntimeFunction(Ty, "__cxa_pure_virtual");
|
|
|
|
PureVirtualFn = llvm::ConstantExpr::getBitCast(PureVirtualFn,
|
|
|
|
Int8PtrTy);
|
|
|
|
}
|
2010-03-29 05:04:16 +04:00
|
|
|
|
2010-03-29 09:40:50 +04:00
|
|
|
Init = PureVirtualFn;
|
2010-03-25 19:49:53 +03:00
|
|
|
} else {
|
2010-03-29 09:40:50 +04:00
|
|
|
// Check if we should use a thunk.
|
|
|
|
if (NextVTableThunkIndex < VTableThunks.size() &&
|
|
|
|
VTableThunks[NextVTableThunkIndex].first == I) {
|
|
|
|
const ThunkInfo &Thunk = VTableThunks[NextVTableThunkIndex].second;
|
|
|
|
|
|
|
|
Init = CGM.GetAddrOfThunk(GD, Thunk);
|
|
|
|
|
|
|
|
NextVTableThunkIndex++;
|
|
|
|
} else {
|
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
const llvm::Type *Ty = CGM.getTypes().GetFunctionTypeForVtable(MD);
|
2010-03-25 19:49:53 +03:00
|
|
|
|
2010-03-29 09:40:50 +04:00
|
|
|
Init = CGM.GetAddrOfFunction(GD, Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
|
2010-03-25 19:49:53 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-10 23:13:06 +04:00
|
|
|
case VTableComponent::CK_UnusedFunctionPointer:
|
2010-03-25 19:49:53 +03:00
|
|
|
Init = llvm::ConstantExpr::getNullValue(Int8PtrTy);
|
|
|
|
break;
|
|
|
|
};
|
2010-03-25 18:26:28 +03:00
|
|
|
|
|
|
|
Inits.push_back(Init);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::ArrayType *ArrayType = llvm::ArrayType::get(Int8PtrTy, NumComponents);
|
|
|
|
return llvm::ConstantArray::get(ArrayType, Inits.data(), Inits.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// GetGlobalVariable - Will return a global variable of the given type.
|
|
|
|
/// If a variable with a different type already exists then a new variable
|
|
|
|
/// with the right type will be created.
|
|
|
|
/// FIXME: We should move this to CodeGenModule and rename it to something
|
|
|
|
/// better and then use it in CGVTT and CGRTTI.
|
|
|
|
static llvm::GlobalVariable *
|
|
|
|
GetGlobalVariable(llvm::Module &Module, llvm::StringRef Name,
|
|
|
|
const llvm::Type *Ty,
|
|
|
|
llvm::GlobalValue::LinkageTypes Linkage) {
|
|
|
|
|
|
|
|
llvm::GlobalVariable *GV = Module.getNamedGlobal(Name);
|
|
|
|
llvm::GlobalVariable *OldGV = 0;
|
|
|
|
|
|
|
|
if (GV) {
|
|
|
|
// Check if the variable has the right type.
|
2010-03-29 09:40:50 +04:00
|
|
|
if (GV->getType()->getElementType() == Ty)
|
2010-03-25 18:26:28 +03:00
|
|
|
return GV;
|
|
|
|
|
|
|
|
assert(GV->isDeclaration() && "Declaration has wrong type!");
|
|
|
|
|
|
|
|
OldGV = GV;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new variable.
|
|
|
|
GV = new llvm::GlobalVariable(Module, Ty, /*isConstant=*/true,
|
|
|
|
Linkage, 0, Name);
|
|
|
|
|
|
|
|
if (OldGV) {
|
|
|
|
// Replace occurrences of the old variable if needed.
|
|
|
|
GV->takeName(OldGV);
|
|
|
|
|
|
|
|
if (!OldGV->use_empty()) {
|
|
|
|
llvm::Constant *NewPtrForOldDecl =
|
|
|
|
llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
|
|
|
|
OldGV->replaceAllUsesWith(NewPtrForOldDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
OldGV->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return GV;
|
|
|
|
}
|
|
|
|
|
2010-03-30 07:35:35 +04:00
|
|
|
llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTable(const CXXRecordDecl *RD) {
|
2010-03-24 06:57:14 +03:00
|
|
|
llvm::SmallString<256> OutName;
|
|
|
|
CGM.getMangleContext().mangleCXXVtable(RD, OutName);
|
|
|
|
llvm::StringRef Name = OutName.str();
|
|
|
|
|
2010-03-24 19:42:11 +03:00
|
|
|
ComputeVTableRelatedInformation(RD);
|
|
|
|
|
2010-03-24 06:57:14 +03:00
|
|
|
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
|
2010-03-24 19:42:11 +03:00
|
|
|
llvm::ArrayType *ArrayType =
|
|
|
|
llvm::ArrayType::get(Int8PtrTy, getNumVTableComponents(RD));
|
2010-03-29 07:38:52 +04:00
|
|
|
|
2010-03-30 07:35:35 +04:00
|
|
|
return GetGlobalVariable(CGM.getModule(), Name, ArrayType,
|
|
|
|
llvm::GlobalValue::ExternalLinkage);
|
2009-11-10 10:44:33 +03:00
|
|
|
}
|
2009-11-11 23:26:26 +03:00
|
|
|
|
2010-03-29 07:38:52 +04:00
|
|
|
void
|
|
|
|
CodeGenVTables::EmitVTableDefinition(llvm::GlobalVariable *VTable,
|
|
|
|
llvm::GlobalVariable::LinkageTypes Linkage,
|
|
|
|
const CXXRecordDecl *RD) {
|
|
|
|
// Dump the vtable layout if necessary.
|
|
|
|
if (CGM.getLangOptions().DumpVtableLayouts) {
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableBuilder Builder(*this, RD, 0, /*MostDerivedClassIsVirtual=*/0, RD);
|
2010-03-29 07:38:52 +04:00
|
|
|
|
|
|
|
Builder.dumpLayout(llvm::errs());
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(VTableThunksMap.count(RD) &&
|
|
|
|
"No thunk status for this record decl!");
|
|
|
|
|
|
|
|
const VTableThunksTy& Thunks = VTableThunksMap[RD];
|
|
|
|
|
|
|
|
// Create and set the initializer.
|
|
|
|
llvm::Constant *Init =
|
|
|
|
CreateVTableInitializer(RD, getVTableComponentsData(RD),
|
|
|
|
getNumVTableComponents(RD), Thunks);
|
|
|
|
VTable->setInitializer(Init);
|
2010-03-29 09:40:50 +04:00
|
|
|
|
|
|
|
// Set the correct linkage.
|
|
|
|
VTable->setLinkage(Linkage);
|
2010-03-29 07:38:52 +04:00
|
|
|
}
|
|
|
|
|
2010-03-25 03:35:49 +03:00
|
|
|
llvm::GlobalVariable *
|
|
|
|
CodeGenVTables::GenerateConstructionVTable(const CXXRecordDecl *RD,
|
2010-03-26 06:56:54 +03:00
|
|
|
const BaseSubobject &Base,
|
|
|
|
bool BaseIsVirtual,
|
|
|
|
VTableAddressPointsMapTy& AddressPoints) {
|
2010-04-10 23:13:06 +04:00
|
|
|
VTableBuilder Builder(*this, Base.getBase(), Base.getBaseOffset(),
|
2010-03-25 18:26:28 +03:00
|
|
|
/*MostDerivedClassIsVirtual=*/BaseIsVirtual, RD);
|
|
|
|
|
2010-03-25 19:49:53 +03:00
|
|
|
// Dump the vtable layout if necessary.
|
|
|
|
if (CGM.getLangOptions().DumpVtableLayouts)
|
|
|
|
Builder.dumpLayout(llvm::errs());
|
|
|
|
|
|
|
|
// Add the address points.
|
2010-03-26 06:56:54 +03:00
|
|
|
AddressPoints.insert(Builder.address_points_begin(),
|
|
|
|
Builder.address_points_end());
|
2010-03-25 18:26:28 +03:00
|
|
|
|
|
|
|
// Get the mangled construction vtable name.
|
|
|
|
llvm::SmallString<256> OutName;
|
|
|
|
CGM.getMangleContext().mangleCXXCtorVtable(RD, Base.getBaseOffset() / 8,
|
|
|
|
Base.getBase(), OutName);
|
|
|
|
llvm::StringRef Name = OutName.str();
|
|
|
|
|
|
|
|
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext());
|
|
|
|
llvm::ArrayType *ArrayType =
|
|
|
|
llvm::ArrayType::get(Int8PtrTy, Builder.getNumVTableComponents());
|
|
|
|
|
|
|
|
// Create the variable that will hold the construction vtable.
|
|
|
|
llvm::GlobalVariable *VTable =
|
|
|
|
GetGlobalVariable(CGM.getModule(), Name, ArrayType,
|
|
|
|
llvm::GlobalValue::InternalLinkage);
|
|
|
|
|
|
|
|
// Add the thunks.
|
|
|
|
VTableThunksTy VTableThunks;
|
|
|
|
VTableThunks.append(Builder.vtable_thunks_begin(),
|
|
|
|
Builder.vtable_thunks_end());
|
|
|
|
|
2010-03-29 05:28:05 +04:00
|
|
|
// Sort them.
|
|
|
|
std::sort(VTableThunks.begin(), VTableThunks.end());
|
|
|
|
|
2010-03-25 18:26:28 +03:00
|
|
|
// Create and set the initializer.
|
|
|
|
llvm::Constant *Init =
|
|
|
|
CreateVTableInitializer(Base.getBase(),
|
|
|
|
Builder.vtable_components_data_begin(),
|
|
|
|
Builder.getNumVTableComponents(), VTableThunks);
|
|
|
|
VTable->setInitializer(Init);
|
|
|
|
|
2010-03-25 03:35:49 +03:00
|
|
|
return VTable;
|
|
|
|
}
|
|
|
|
|
2010-03-29 07:38:52 +04:00
|
|
|
void
|
|
|
|
CodeGenVTables::GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
|
|
|
|
const CXXRecordDecl *RD) {
|
|
|
|
llvm::GlobalVariable *&VTable = Vtables[RD];
|
|
|
|
if (VTable) {
|
|
|
|
assert(VTable->getInitializer() && "Vtable doesn't have a definition!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-30 07:35:35 +04:00
|
|
|
VTable = GetAddrOfVTable(RD);
|
|
|
|
EmitVTableDefinition(VTable, Linkage, RD);
|
|
|
|
|
2010-03-29 07:38:52 +04:00
|
|
|
GenerateVTT(Linkage, /*GenerateDefinition=*/true, RD);
|
2010-04-08 19:52:03 +04:00
|
|
|
|
|
|
|
// If this is the magic class __cxxabiv1::__fundamental_type_info,
|
|
|
|
// we will emit the typeinfo for the fundamental types. This is the
|
|
|
|
// same behaviour as GCC.
|
|
|
|
const DeclContext *DC = RD->getDeclContext();
|
|
|
|
if (RD->getIdentifier() &&
|
|
|
|
RD->getIdentifier()->isStr("__fundamental_type_info") &&
|
|
|
|
isa<NamespaceDecl>(DC) &&
|
|
|
|
cast<NamespaceDecl>(DC)->getIdentifier() &&
|
|
|
|
cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__cxxabiv1") &&
|
|
|
|
DC->getParent()->isTranslationUnit())
|
|
|
|
CGM.EmitFundamentalRTTIDescriptors();
|
2010-03-29 07:38:52 +04:00
|
|
|
}
|
|
|
|
|
2010-03-23 07:15:00 +03:00
|
|
|
void CodeGenVTables::EmitVTableRelatedData(GlobalDecl GD) {
|
2009-12-01 02:41:22 +03:00
|
|
|
const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl());
|
|
|
|
const CXXRecordDecl *RD = MD->getParent();
|
|
|
|
|
2009-12-06 01:42:54 +03:00
|
|
|
// If the class doesn't have a vtable we don't need to emit one.
|
|
|
|
if (!RD->isDynamicClass())
|
|
|
|
return;
|
|
|
|
|
2010-03-23 07:31:31 +03:00
|
|
|
// Check if we need to emit thunks for this function.
|
|
|
|
if (MD->isVirtual())
|
2010-03-23 07:59:02 +03:00
|
|
|
EmitThunks(GD);
|
2010-03-23 07:31:31 +03:00
|
|
|
|
2009-12-01 02:41:22 +03:00
|
|
|
// Get the key function.
|
2009-12-07 07:35:11 +03:00
|
|
|
const CXXMethodDecl *KeyFunction = CGM.getContext().getKeyFunction(RD);
|
2009-12-01 02:41:22 +03:00
|
|
|
|
2010-03-30 22:07:27 +04:00
|
|
|
TemplateSpecializationKind RDKind = RD->getTemplateSpecializationKind();
|
|
|
|
TemplateSpecializationKind MDKind = MD->getTemplateSpecializationKind();
|
|
|
|
|
2009-12-06 01:42:54 +03:00
|
|
|
if (KeyFunction) {
|
|
|
|
// We don't have the right key function.
|
|
|
|
if (KeyFunction->getCanonicalDecl() != MD->getCanonicalDecl())
|
|
|
|
return;
|
2010-03-30 22:07:27 +04:00
|
|
|
} else {
|
2010-04-03 08:26:42 +04:00
|
|
|
// If we have no key funcion and this is a explicit instantiation declaration,
|
|
|
|
// we will produce a vtable at the explicit instantiation. We don't need one
|
|
|
|
// here.
|
|
|
|
if (RDKind == clang::TSK_ExplicitInstantiationDeclaration)
|
|
|
|
return;
|
|
|
|
|
2010-03-30 22:07:27 +04:00
|
|
|
// If this is an explicit instantiation of a method, we don't need a vtable.
|
|
|
|
// Since we have no key function, we will emit the vtable when we see
|
|
|
|
// a use, and just defining a function is not an use.
|
2010-04-03 08:26:42 +04:00
|
|
|
if (RDKind == TSK_ImplicitInstantiation &&
|
2010-03-30 22:07:27 +04:00
|
|
|
MDKind == TSK_ExplicitInstantiationDefinition)
|
|
|
|
return;
|
2009-12-01 02:41:22 +03:00
|
|
|
}
|
|
|
|
|
2010-03-10 05:19:29 +03:00
|
|
|
if (Vtables.count(RD))
|
|
|
|
return;
|
2009-12-07 01:01:30 +03:00
|
|
|
|
2010-03-30 22:07:27 +04:00
|
|
|
if (RDKind == TSK_ImplicitInstantiation)
|
2010-03-10 05:19:29 +03:00
|
|
|
CGM.DeferredVtables.push_back(RD);
|
|
|
|
else
|
|
|
|
GenerateClassData(CGM.getVtableLinkage(RD), RD);
|
2009-12-01 02:41:22 +03:00
|
|
|
}
|