2009-12-03 02:57:08 +08:00
|
|
|
//===--- CGCXXRTTI.cpp - Emit LLVM Code for C++ RTTI descriptors ----------===//
|
2009-10-11 04:49:04 +08: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 RTTI descriptors.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-11-18 07:45:57 +08:00
|
|
|
#include "clang/AST/Type.h"
|
2009-11-15 07:32:21 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
2009-11-18 05:44:24 +08:00
|
|
|
#include "CodeGenModule.h"
|
2009-10-11 04:49:04 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2009-12-03 03:07:44 +08:00
|
|
|
namespace {
|
2009-12-03 02:57:08 +08:00
|
|
|
class RTTIBuilder {
|
2009-11-14 22:25:18 +08:00
|
|
|
CodeGenModule &CGM; // Per-module state.
|
|
|
|
llvm::LLVMContext &VMContext;
|
|
|
|
const llvm::Type *Int8PtrTy;
|
2009-11-15 11:28:10 +08:00
|
|
|
llvm::SmallSet<const CXXRecordDecl *, 16> SeenVBase;
|
|
|
|
llvm::SmallSet<const CXXRecordDecl *, 32> SeenBase;
|
2009-12-12 00:41:51 +08:00
|
|
|
|
2009-12-11 09:27:37 +08:00
|
|
|
// Type info flags.
|
|
|
|
enum {
|
|
|
|
/// TI_Const - Type has const qualifier.
|
|
|
|
TI_Const = 0x1,
|
|
|
|
|
|
|
|
/// TI_Volatile - Type has volatile qualifier.
|
|
|
|
TI_Volatile = 0x2,
|
|
|
|
|
|
|
|
/// TI_Restrict - Type has restrict qualifier.
|
|
|
|
TI_Restrict = 0x4,
|
|
|
|
|
|
|
|
/// TI_Incomplete - Type is incomplete.
|
|
|
|
TI_Incomplete = 0x8,
|
|
|
|
|
|
|
|
/// TI_ContainingClassIncomplete - Containing class is incomplete.
|
|
|
|
/// (in pointer to member).
|
|
|
|
TI_ContainingClassIncomplete = 0x10
|
|
|
|
};
|
|
|
|
|
2009-11-14 22:25:18 +08:00
|
|
|
public:
|
2009-12-03 02:57:08 +08:00
|
|
|
RTTIBuilder(CodeGenModule &cgm)
|
2009-11-14 22:25:18 +08:00
|
|
|
: CGM(cgm), VMContext(cgm.getModule().getContext()),
|
|
|
|
Int8PtrTy(llvm::Type::getInt8PtrTy(VMContext)) { }
|
|
|
|
|
2009-11-15 07:32:21 +08:00
|
|
|
/// BuildVtableRef - Build a reference to a vtable.
|
|
|
|
llvm::Constant *BuildVtableRef(const char *Name) {
|
|
|
|
// Build a descriptor for Name
|
2009-11-14 23:55:18 +08:00
|
|
|
llvm::Constant *GV = CGM.getModule().getGlobalVariable(Name);
|
|
|
|
if (GV)
|
|
|
|
GV = llvm::ConstantExpr::getBitCast(GV,
|
|
|
|
llvm::PointerType::get(Int8PtrTy, 0));
|
|
|
|
else {
|
|
|
|
llvm::GlobalVariable::LinkageTypes linktype;
|
|
|
|
linktype = llvm::GlobalValue::ExternalLinkage;
|
|
|
|
GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy,
|
|
|
|
true, linktype, 0, Name);
|
2009-11-15 07:32:21 +08:00
|
|
|
}
|
2009-11-14 23:55:18 +08:00
|
|
|
llvm::Constant *C;
|
|
|
|
C = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 2);
|
|
|
|
C = llvm::ConstantExpr::getInBoundsGetElementPtr(GV, &C, 1);
|
|
|
|
return llvm::ConstantExpr::getBitCast(C, Int8PtrTy);
|
|
|
|
}
|
2009-11-15 07:32:21 +08:00
|
|
|
|
2009-12-11 10:46:30 +08:00
|
|
|
// FIXME: This should be removed, and clients should pass in the linkage
|
|
|
|
// directly instead.
|
|
|
|
static inline llvm::GlobalVariable::LinkageTypes
|
|
|
|
GetLinkageFromExternFlag(bool Extern) {
|
|
|
|
if (Extern)
|
|
|
|
return llvm::GlobalValue::WeakODRLinkage;
|
|
|
|
|
|
|
|
return llvm::GlobalValue::InternalLinkage;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: This should be removed, and clients should pass in the linkage
|
|
|
|
// directly instead.
|
2009-11-19 09:08:19 +08:00
|
|
|
llvm::Constant *BuildName(QualType Ty, bool Hidden, bool Extern) {
|
2009-12-11 10:46:30 +08:00
|
|
|
return BuildName(Ty, Hidden, GetLinkageFromExternFlag(Extern));
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *BuildName(QualType Ty, bool Hidden,
|
|
|
|
llvm::GlobalVariable::LinkageTypes Linkage) {
|
2009-11-14 22:25:18 +08:00
|
|
|
llvm::SmallString<256> OutName;
|
2009-12-03 02:57:08 +08:00
|
|
|
CGM.getMangleContext().mangleCXXRTTIName(Ty, OutName);
|
2009-11-21 17:06:22 +08:00
|
|
|
llvm::StringRef Name = OutName.str();
|
2009-11-15 07:32:21 +08:00
|
|
|
|
2009-12-11 10:46:30 +08:00
|
|
|
llvm::GlobalVariable *OGV = CGM.getModule().getGlobalVariable(Name);
|
|
|
|
if (OGV && !OGV->isDeclaration())
|
|
|
|
return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy);
|
2009-11-14 22:25:18 +08:00
|
|
|
|
2009-12-11 10:46:30 +08:00
|
|
|
llvm::Constant *C = llvm::ConstantArray::get(VMContext, Name.substr(4));
|
2009-11-15 07:32:21 +08:00
|
|
|
|
2009-12-11 10:46:30 +08:00
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
|
|
|
|
C, Name);
|
2009-11-19 09:08:19 +08:00
|
|
|
if (OGV) {
|
|
|
|
GV->takeName(OGV);
|
|
|
|
llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
|
|
|
|
OGV->getType());
|
|
|
|
OGV->replaceAllUsesWith(NewPtr);
|
|
|
|
OGV->eraseFromParent();
|
|
|
|
}
|
2009-11-18 11:46:51 +08:00
|
|
|
if (Hidden)
|
|
|
|
GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
|
|
|
|
return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
|
2009-11-14 22:25:18 +08:00
|
|
|
};
|
|
|
|
|
2009-11-15 07:32:21 +08:00
|
|
|
/// - BuildFlags - Build a psABI __flags value for __vmi_class_type_info.
|
|
|
|
llvm::Constant *BuildFlags(int f) {
|
|
|
|
return llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// BuildBaseCount - Build a psABI __base_count value for
|
|
|
|
/// __vmi_class_type_info.
|
|
|
|
llvm::Constant *BuildBaseCount(unsigned c) {
|
|
|
|
return llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), c);
|
|
|
|
}
|
|
|
|
|
2009-11-17 10:16:21 +08:00
|
|
|
llvm::Constant *BuildTypeRef(QualType Ty) {
|
2009-11-15 07:32:21 +08:00
|
|
|
llvm::Constant *C;
|
|
|
|
|
|
|
|
llvm::SmallString<256> OutName;
|
2009-12-03 02:57:08 +08:00
|
|
|
CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
|
2009-11-21 17:06:22 +08:00
|
|
|
llvm::StringRef Name = OutName.str();
|
2009-11-15 07:32:21 +08:00
|
|
|
|
2009-11-19 09:08:19 +08:00
|
|
|
C = CGM.getModule().getGlobalVariable(Name);
|
2009-11-15 07:32:21 +08:00
|
|
|
if (C)
|
|
|
|
return llvm::ConstantExpr::getBitCast(C, Int8PtrTy);
|
|
|
|
|
|
|
|
llvm::GlobalVariable::LinkageTypes linktype;
|
|
|
|
linktype = llvm::GlobalValue::ExternalLinkage;;
|
|
|
|
|
|
|
|
C = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, true, linktype,
|
2009-11-19 09:08:19 +08:00
|
|
|
0, Name);
|
2009-11-15 07:32:21 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(C, Int8PtrTy);
|
|
|
|
}
|
|
|
|
|
2009-11-17 10:16:21 +08:00
|
|
|
llvm::Constant *Buildclass_type_infoRef(const CXXRecordDecl *RD) {
|
|
|
|
return BuildTypeRef(CGM.getContext().getTagDeclType(RD));
|
|
|
|
}
|
|
|
|
|
2009-11-15 11:28:10 +08:00
|
|
|
/// CalculateFlags - Calculate the flags for the __vmi_class_type_info
|
|
|
|
/// datastructure. 1 for non-diamond repeated inheritance, 2 for a dimond
|
|
|
|
/// shaped class.
|
|
|
|
int CalculateFlags(const CXXRecordDecl*RD) {
|
|
|
|
int flags = 0;
|
|
|
|
if (SeenBase.count(RD))
|
|
|
|
flags |= 1;
|
|
|
|
else
|
|
|
|
SeenBase.insert(RD);
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
|
|
|
|
e = RD->bases_end(); i != e; ++i) {
|
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
if (i->isVirtual()) {
|
|
|
|
if (SeenVBase.count(Base))
|
|
|
|
flags |= 2;
|
|
|
|
else
|
|
|
|
SeenVBase.insert(Base);
|
|
|
|
}
|
|
|
|
flags |= CalculateFlags(Base);
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleInheritance(const CXXRecordDecl *RD) {
|
|
|
|
if (RD->getNumBases() != 1)
|
|
|
|
return false;
|
|
|
|
CXXRecordDecl::base_class_const_iterator i = RD->bases_begin();
|
|
|
|
if (i->isVirtual())
|
|
|
|
return false;
|
|
|
|
if (i->getAccessSpecifier() != AS_public)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
if (Layout.getBaseClassOffset(Base) != 0)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-12-14 00:38:01 +08:00
|
|
|
llvm::Constant *finish(llvm::Constant *const *Values, unsigned NumValues,
|
2009-11-18 07:11:22 +08:00
|
|
|
llvm::GlobalVariable *GV,
|
2009-12-11 10:46:30 +08:00
|
|
|
llvm::StringRef Name, bool Hidden,
|
|
|
|
llvm::GlobalVariable::LinkageTypes Linkage) {
|
|
|
|
llvm::Constant *C =
|
2009-12-14 00:38:01 +08:00
|
|
|
llvm::ConstantStruct::get(VMContext, Values, NumValues, /*Packed=*/false);
|
2009-11-18 07:11:22 +08:00
|
|
|
|
2009-11-19 09:08:19 +08:00
|
|
|
llvm::GlobalVariable *OGV = GV;
|
2009-12-11 10:46:30 +08:00
|
|
|
GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
|
2009-11-19 09:08:19 +08:00
|
|
|
C, Name);
|
|
|
|
if (OGV) {
|
2009-11-18 07:11:22 +08:00
|
|
|
GV->takeName(OGV);
|
|
|
|
llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
|
|
|
|
OGV->getType());
|
|
|
|
OGV->replaceAllUsesWith(NewPtr);
|
|
|
|
OGV->eraseFromParent();
|
|
|
|
}
|
2009-11-18 11:46:51 +08:00
|
|
|
if (Hidden)
|
2009-11-18 11:21:29 +08:00
|
|
|
GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
|
2009-11-18 07:11:22 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
|
|
|
|
}
|
|
|
|
|
2009-11-14 22:25:18 +08:00
|
|
|
|
2009-12-11 10:46:30 +08:00
|
|
|
llvm::Constant *
|
|
|
|
Buildclass_type_info(const CXXRecordDecl *RD,
|
|
|
|
llvm::GlobalVariable::LinkageTypes Linkage) {
|
2009-12-12 00:41:51 +08:00
|
|
|
std::vector<llvm::Constant *> info;
|
|
|
|
assert(info.empty() && "Info vector must be empty!");
|
|
|
|
|
2009-11-18 07:11:22 +08:00
|
|
|
llvm::Constant *C;
|
|
|
|
|
2009-11-14 23:55:18 +08:00
|
|
|
llvm::SmallString<256> OutName;
|
2009-12-03 02:57:08 +08:00
|
|
|
CGM.getMangleContext().mangleCXXRTTI(CGM.getContext().getTagDeclType(RD),
|
2009-11-21 17:06:22 +08:00
|
|
|
OutName);
|
|
|
|
llvm::StringRef Name = OutName.str();
|
2009-11-15 07:32:21 +08:00
|
|
|
|
|
|
|
llvm::GlobalVariable *GV;
|
2009-11-19 09:08:19 +08:00
|
|
|
GV = CGM.getModule().getGlobalVariable(Name);
|
2009-11-15 07:32:21 +08:00
|
|
|
if (GV && !GV->isDeclaration())
|
|
|
|
return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
|
|
|
|
|
2009-12-11 10:46:30 +08:00
|
|
|
// If we're in an anonymous namespace, then we always want internal linkage.
|
2009-12-12 04:48:18 +08:00
|
|
|
if (RD->isInAnonymousNamespace() || !RD->hasLinkage())
|
2009-12-11 10:46:30 +08:00
|
|
|
Linkage = llvm::GlobalVariable::InternalLinkage;
|
|
|
|
|
2009-11-18 11:46:51 +08:00
|
|
|
bool Hidden = CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden;
|
|
|
|
|
2009-11-15 11:28:10 +08:00
|
|
|
bool simple = false;
|
2009-11-15 07:32:21 +08:00
|
|
|
if (RD->getNumBases() == 0)
|
|
|
|
C = BuildVtableRef("_ZTVN10__cxxabiv117__class_type_infoE");
|
2009-11-15 11:28:10 +08:00
|
|
|
else if (SimpleInheritance(RD)) {
|
|
|
|
simple = true;
|
|
|
|
C = BuildVtableRef("_ZTVN10__cxxabiv120__si_class_type_infoE");
|
|
|
|
} else
|
2009-11-15 07:32:21 +08:00
|
|
|
C = BuildVtableRef("_ZTVN10__cxxabiv121__vmi_class_type_infoE");
|
|
|
|
info.push_back(C);
|
2009-11-19 09:08:19 +08:00
|
|
|
info.push_back(BuildName(CGM.getContext().getTagDeclType(RD), Hidden,
|
2009-12-11 10:46:30 +08:00
|
|
|
Linkage));
|
2009-11-14 23:55:18 +08:00
|
|
|
|
2009-11-15 07:32:21 +08:00
|
|
|
// If we have no bases, there are no more fields.
|
|
|
|
if (RD->getNumBases()) {
|
2009-11-15 11:28:10 +08:00
|
|
|
if (!simple) {
|
|
|
|
info.push_back(BuildFlags(CalculateFlags(RD)));
|
|
|
|
info.push_back(BuildBaseCount(RD->getNumBases()));
|
|
|
|
}
|
2009-11-15 07:32:21 +08:00
|
|
|
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(),
|
|
|
|
e = RD->bases_end(); i != e; ++i) {
|
|
|
|
const CXXRecordDecl *Base =
|
|
|
|
cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl());
|
2009-12-12 00:37:06 +08:00
|
|
|
info.push_back(CGM.GetAddrOfRTTI(Base));
|
2009-11-15 11:28:10 +08:00
|
|
|
if (simple)
|
|
|
|
break;
|
2009-11-15 07:32:21 +08:00
|
|
|
int64_t offset;
|
|
|
|
if (!i->isVirtual())
|
2009-11-15 11:28:10 +08:00
|
|
|
offset = Layout.getBaseClassOffset(Base)/8;
|
2009-11-15 07:32:21 +08:00
|
|
|
else
|
|
|
|
offset = CGM.getVtableInfo().getVirtualBaseOffsetIndex(RD, Base);
|
|
|
|
offset <<= 8;
|
|
|
|
// Now set the flags.
|
|
|
|
offset += i->isVirtual() ? 1 : 0;;
|
|
|
|
offset += i->getAccessSpecifier() == AS_public ? 2 : 0;
|
|
|
|
const llvm::Type *LongTy =
|
|
|
|
CGM.getTypes().ConvertType(CGM.getContext().LongTy);
|
|
|
|
C = llvm::ConstantInt::get(LongTy, offset);
|
|
|
|
info.push_back(C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-14 00:38:01 +08:00
|
|
|
return finish(&info[0], info.size(), GV, Name, Hidden, Linkage);
|
2009-11-14 23:55:18 +08:00
|
|
|
}
|
2009-11-17 10:16:21 +08:00
|
|
|
|
2009-11-18 05:44:24 +08:00
|
|
|
/// - BuildFlags - Build a __flags value for __pbase_type_info.
|
2009-12-11 09:27:37 +08:00
|
|
|
llvm::Constant *BuildInt(unsigned n) {
|
|
|
|
return llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), n);
|
2009-11-18 05:44:24 +08:00
|
|
|
}
|
|
|
|
|
2009-11-19 09:08:19 +08:00
|
|
|
bool DecideExtern(QualType Ty) {
|
|
|
|
// For this type, see if all components are never in an anonymous namespace.
|
|
|
|
if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>())
|
|
|
|
return (DecideExtern(MPT->getPointeeType())
|
|
|
|
&& DecideExtern(QualType(MPT->getClass(), 0)));
|
|
|
|
if (const PointerType *PT = Ty->getAs<PointerType>())
|
|
|
|
return DecideExtern(PT->getPointeeType());
|
|
|
|
if (const RecordType *RT = Ty->getAs<RecordType>())
|
|
|
|
if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
|
2009-12-12 04:48:18 +08:00
|
|
|
return !RD->isInAnonymousNamespace() && RD->hasLinkage();
|
2009-11-19 09:08:19 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DecideHidden(QualType Ty) {
|
|
|
|
// For this type, see if all components are never hidden.
|
|
|
|
if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>())
|
|
|
|
return (DecideHidden(MPT->getPointeeType())
|
|
|
|
&& DecideHidden(QualType(MPT->getClass(), 0)));
|
|
|
|
if (const PointerType *PT = Ty->getAs<PointerType>())
|
|
|
|
return DecideHidden(PT->getPointeeType());
|
|
|
|
if (const RecordType *RT = Ty->getAs<RecordType>())
|
|
|
|
if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
|
|
|
|
return CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-18 05:44:24 +08:00
|
|
|
llvm::Constant *BuildPointerType(QualType Ty) {
|
2009-12-12 00:41:51 +08:00
|
|
|
std::vector<llvm::Constant *> info;
|
|
|
|
assert(info.empty() && "Info vector must be empty!");
|
|
|
|
|
2009-11-18 05:44:24 +08:00
|
|
|
llvm::Constant *C;
|
|
|
|
|
|
|
|
llvm::SmallString<256> OutName;
|
2009-12-03 02:57:08 +08:00
|
|
|
CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
|
2009-11-21 17:06:22 +08:00
|
|
|
llvm::StringRef Name = OutName.str();
|
2009-11-18 05:44:24 +08:00
|
|
|
|
|
|
|
llvm::GlobalVariable *GV;
|
2009-11-19 09:08:19 +08:00
|
|
|
GV = CGM.getModule().getGlobalVariable(Name);
|
2009-11-18 05:44:24 +08:00
|
|
|
if (GV && !GV->isDeclaration())
|
|
|
|
return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
|
|
|
|
|
2009-11-19 09:08:19 +08:00
|
|
|
bool Extern = DecideExtern(Ty);
|
|
|
|
bool Hidden = DecideHidden(Ty);
|
2009-11-18 11:46:51 +08:00
|
|
|
|
2009-12-11 09:27:37 +08:00
|
|
|
const MemberPointerType *PtrMemTy = dyn_cast<MemberPointerType>(Ty);
|
|
|
|
QualType PointeeTy;
|
|
|
|
|
|
|
|
if (PtrMemTy)
|
|
|
|
PointeeTy = PtrMemTy->getPointeeType();
|
|
|
|
else
|
|
|
|
PointeeTy = Ty->getPointeeType();
|
2009-11-18 05:44:24 +08:00
|
|
|
|
2009-12-11 09:27:37 +08:00
|
|
|
if (PtrMemTy)
|
2009-11-18 05:44:24 +08:00
|
|
|
C = BuildVtableRef("_ZTVN10__cxxabiv129__pointer_to_member_type_infoE");
|
|
|
|
else
|
|
|
|
C = BuildVtableRef("_ZTVN10__cxxabiv119__pointer_type_infoE");
|
2009-12-11 09:27:37 +08:00
|
|
|
|
2009-11-18 05:44:24 +08:00
|
|
|
info.push_back(C);
|
2009-11-19 09:08:19 +08:00
|
|
|
info.push_back(BuildName(Ty, Hidden, Extern));
|
2009-12-11 09:27:37 +08:00
|
|
|
Qualifiers Q = PointeeTy.getQualifiers();
|
|
|
|
|
|
|
|
PointeeTy =
|
|
|
|
CGM.getContext().getCanonicalType(PointeeTy).getUnqualifiedType();
|
|
|
|
|
|
|
|
unsigned Flags = 0;
|
|
|
|
if (Q.hasConst())
|
|
|
|
Flags |= TI_Const;
|
|
|
|
if (Q.hasVolatile())
|
|
|
|
Flags |= TI_Volatile;
|
|
|
|
if (Q.hasRestrict())
|
|
|
|
Flags |= TI_Restrict;
|
|
|
|
|
|
|
|
if (Ty->isIncompleteType())
|
|
|
|
Flags |= TI_Incomplete;
|
|
|
|
|
|
|
|
if (PtrMemTy && PtrMemTy->getClass()->isIncompleteType())
|
|
|
|
Flags |= TI_ContainingClassIncomplete;
|
|
|
|
|
|
|
|
info.push_back(BuildInt(Flags));
|
2009-11-18 05:44:24 +08:00
|
|
|
info.push_back(BuildInt(0));
|
2009-12-11 09:27:37 +08:00
|
|
|
info.push_back(BuildType(PointeeTy));
|
2009-11-18 05:44:24 +08:00
|
|
|
|
2009-12-11 09:27:37 +08:00
|
|
|
if (PtrMemTy)
|
|
|
|
info.push_back(BuildType(QualType(PtrMemTy->getClass(), 0)));
|
2009-11-18 07:51:10 +08:00
|
|
|
|
2009-11-19 09:08:19 +08:00
|
|
|
// We always generate these as hidden, only the name isn't hidden.
|
2009-12-14 00:38:01 +08:00
|
|
|
return finish(&info[0], info.size(), GV, Name, /*Hidden=*/true,
|
2009-12-11 10:46:30 +08:00
|
|
|
GetLinkageFromExternFlag(Extern));
|
2009-11-18 07:11:22 +08:00
|
|
|
}
|
2009-11-18 05:44:24 +08:00
|
|
|
|
2009-11-18 07:45:57 +08:00
|
|
|
llvm::Constant *BuildSimpleType(QualType Ty, const char *vtbl) {
|
2009-12-12 00:41:51 +08:00
|
|
|
std::vector<llvm::Constant *> info;
|
|
|
|
assert(info.empty() && "Info vector must be empty!");
|
|
|
|
|
2009-11-18 07:11:22 +08:00
|
|
|
llvm::Constant *C;
|
|
|
|
|
|
|
|
llvm::SmallString<256> OutName;
|
2009-12-03 02:57:08 +08:00
|
|
|
CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
|
2009-11-21 17:06:22 +08:00
|
|
|
llvm::StringRef Name = OutName.str();
|
2009-11-18 07:11:22 +08:00
|
|
|
|
|
|
|
llvm::GlobalVariable *GV;
|
2009-11-19 09:08:19 +08:00
|
|
|
GV = CGM.getModule().getGlobalVariable(Name);
|
2009-11-18 07:11:22 +08:00
|
|
|
if (GV && !GV->isDeclaration())
|
|
|
|
return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
|
|
|
|
|
2009-11-19 09:08:19 +08:00
|
|
|
bool Extern = DecideExtern(Ty);
|
|
|
|
bool Hidden = DecideHidden(Ty);
|
2009-11-18 11:46:51 +08:00
|
|
|
|
2009-11-18 07:45:57 +08:00
|
|
|
C = BuildVtableRef(vtbl);
|
2009-11-18 07:11:22 +08:00
|
|
|
info.push_back(C);
|
2009-11-19 09:08:19 +08:00
|
|
|
info.push_back(BuildName(Ty, Hidden, Extern));
|
2009-11-18 07:51:10 +08:00
|
|
|
|
2009-11-19 09:08:19 +08:00
|
|
|
// We always generate these as hidden, only the name isn't hidden.
|
2009-12-14 00:38:01 +08:00
|
|
|
return finish(&info[0], info.size(), GV, Name, /*Hidden=*/true,
|
2009-12-11 10:46:30 +08:00
|
|
|
GetLinkageFromExternFlag(Extern));
|
2009-11-18 05:44:24 +08:00
|
|
|
}
|
|
|
|
|
2009-12-11 10:46:30 +08:00
|
|
|
/// BuildType - Builds the type info for the given type.
|
2009-11-17 10:16:21 +08:00
|
|
|
llvm::Constant *BuildType(QualType Ty) {
|
|
|
|
const clang::Type &Type
|
|
|
|
= *CGM.getContext().getCanonicalType(Ty).getTypePtr();
|
2009-11-20 08:31:50 +08:00
|
|
|
|
|
|
|
if (const RecordType *RT = Ty.getTypePtr()->getAs<RecordType>())
|
|
|
|
if (const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()))
|
2009-12-11 10:46:30 +08:00
|
|
|
return BuildClassTypeInfo(RD);
|
2009-11-20 08:31:50 +08:00
|
|
|
|
2009-11-17 10:16:21 +08:00
|
|
|
switch (Type.getTypeClass()) {
|
|
|
|
default: {
|
|
|
|
assert(0 && "typeid expression");
|
|
|
|
return llvm::Constant::getNullValue(Int8PtrTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
case Type::Builtin: {
|
|
|
|
// We expect all type_info objects for builtin types to be in the library.
|
|
|
|
return BuildTypeRef(Ty);
|
|
|
|
}
|
2009-11-17 10:57:13 +08:00
|
|
|
|
|
|
|
case Type::Pointer: {
|
|
|
|
QualType PTy = Ty->getPointeeType();
|
|
|
|
Qualifiers Q = PTy.getQualifiers();
|
|
|
|
Q.removeConst();
|
|
|
|
// T* and const T* for all builtin types T are expected in the library.
|
|
|
|
if (isa<BuiltinType>(PTy) && Q.empty())
|
|
|
|
return BuildTypeRef(Ty);
|
|
|
|
|
2009-11-18 05:44:24 +08:00
|
|
|
return BuildPointerType(Ty);
|
2009-11-17 10:57:13 +08:00
|
|
|
}
|
2009-11-18 06:33:00 +08:00
|
|
|
case Type::MemberPointer:
|
|
|
|
return BuildPointerType(Ty);
|
2009-11-18 07:11:22 +08:00
|
|
|
case Type::FunctionProto:
|
2009-11-18 07:45:57 +08:00
|
|
|
case Type::FunctionNoProto:
|
|
|
|
return BuildSimpleType(Ty, "_ZTVN10__cxxabiv120__function_type_infoE");
|
|
|
|
case Type::ConstantArray:
|
|
|
|
case Type::IncompleteArray:
|
|
|
|
case Type::VariableArray:
|
|
|
|
case Type::Vector:
|
|
|
|
case Type::ExtVector:
|
|
|
|
return BuildSimpleType(Ty, "_ZTVN10__cxxabiv117__array_type_infoE");
|
|
|
|
case Type::Enum:
|
|
|
|
return BuildSimpleType(Ty, "_ZTVN10__cxxabiv116__enum_type_infoE");
|
2009-11-17 10:16:21 +08:00
|
|
|
}
|
|
|
|
}
|
2009-12-11 10:46:30 +08:00
|
|
|
|
|
|
|
/// BuildClassTypeInfo - Builds the class type info (or a reference to it)
|
|
|
|
/// for the given record decl.
|
|
|
|
llvm::Constant *BuildClassTypeInfo(const CXXRecordDecl *RD) {
|
|
|
|
const CXXMethodDecl *KeyFunction = 0;
|
|
|
|
|
|
|
|
if (RD->isDynamicClass())
|
|
|
|
KeyFunction = CGM.getContext().getKeyFunction(RD);
|
|
|
|
|
|
|
|
if (KeyFunction) {
|
|
|
|
// If the key function is defined in this translation unit, then the RTTI
|
|
|
|
// related constants should also be emitted here, with external linkage.
|
|
|
|
if (KeyFunction->getBody())
|
|
|
|
return Buildclass_type_info(RD, llvm::GlobalValue::ExternalLinkage);
|
|
|
|
|
|
|
|
// Otherwise, we just want a reference to the type info.
|
|
|
|
return Buildclass_type_infoRef(RD);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no key function (or if the record doesn't have any virtual
|
|
|
|
// member functions or virtual bases), emit the type info with weak_odr
|
|
|
|
// linkage.
|
|
|
|
return Buildclass_type_info(RD, llvm::GlobalValue::WeakODRLinkage);
|
|
|
|
}
|
2009-11-14 23:55:18 +08:00
|
|
|
};
|
2009-12-03 03:07:44 +08:00
|
|
|
}
|
2009-11-14 22:25:18 +08:00
|
|
|
|
2009-12-11 10:46:30 +08:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfRTTI(const CXXRecordDecl *RD) {
|
|
|
|
if (!getContext().getLangOptions().RTTI) {
|
|
|
|
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
|
|
|
|
return llvm::Constant::getNullValue(Int8PtrTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
return RTTIBuilder(*this).BuildClassTypeInfo(RD);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfRTTI(QualType Ty) {
|
|
|
|
if (!getContext().getLangOptions().RTTI) {
|
|
|
|
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
|
|
|
|
return llvm::Constant::getNullValue(Int8PtrTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
return RTTIBuilder(*this).BuildType(Ty);
|
|
|
|
}
|
|
|
|
|
2009-12-03 02:57:08 +08:00
|
|
|
llvm::Constant *CodeGenModule::GenerateRTTIRef(const CXXRecordDecl *RD) {
|
|
|
|
RTTIBuilder b(*this);
|
2009-11-15 07:32:21 +08:00
|
|
|
|
|
|
|
return b.Buildclass_type_infoRef(RD);
|
|
|
|
}
|
|
|
|
|
2009-12-03 02:57:08 +08:00
|
|
|
llvm::Constant *CodeGenModule::GenerateRTTI(const CXXRecordDecl *RD) {
|
|
|
|
RTTIBuilder b(*this);
|
2009-10-11 04:49:04 +08:00
|
|
|
|
2009-12-11 10:46:30 +08:00
|
|
|
return b.Buildclass_type_info(RD, llvm::GlobalValue::ExternalLinkage);
|
2009-10-11 04:49:04 +08:00
|
|
|
}
|
2009-11-17 10:16:21 +08:00
|
|
|
|
2009-12-03 02:57:08 +08:00
|
|
|
llvm::Constant *CodeGenModule::GenerateRTTI(QualType Ty) {
|
|
|
|
RTTIBuilder b(*this);
|
2009-11-17 10:16:21 +08:00
|
|
|
|
|
|
|
return b.BuildType(Ty);
|
|
|
|
}
|