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 05:44:24 +08:00
|
|
|
#include "CodeGenModule.h"
|
2010-08-31 15:33:07 +08:00
|
|
|
#include "CGCXXABI.h"
|
2010-08-13 07:36:15 +08:00
|
|
|
#include "clang/AST/RecordLayout.h"
|
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "clang/Frontend/CodeGenOptions.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;
|
2009-12-17 13:06:03 +08:00
|
|
|
|
2009-12-31 07:47:56 +08:00
|
|
|
const llvm::Type *Int8PtrTy;
|
2010-01-01 01:43:53 +08:00
|
|
|
|
|
|
|
/// Fields - The fields of the RTTI descriptor currently being built.
|
|
|
|
llvm::SmallVector<llvm::Constant *, 16> Fields;
|
2009-12-11 09:27:37 +08:00
|
|
|
|
2009-12-17 15:09:17 +08:00
|
|
|
/// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
|
|
|
|
/// descriptor of the given type.
|
|
|
|
llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
|
|
|
|
|
2010-04-18 04:15:18 +08:00
|
|
|
/// BuildVTablePointer - Build the vtable pointer for the given type.
|
|
|
|
void BuildVTablePointer(const Type *Ty);
|
2009-12-21 06:30:54 +08:00
|
|
|
|
2009-12-30 09:00:12 +08:00
|
|
|
/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
|
2009-12-31 07:47:56 +08:00
|
|
|
/// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
|
2009-12-30 09:00:12 +08:00
|
|
|
void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
|
|
|
|
|
2009-12-31 07:47:56 +08:00
|
|
|
/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
|
|
|
|
/// classes with bases that do not satisfy the abi::__si_class_type_info
|
|
|
|
/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
|
|
|
|
void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
|
|
|
|
|
2009-12-30 09:00:12 +08:00
|
|
|
/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
|
|
|
|
/// for pointer types.
|
2010-08-12 10:17:33 +08:00
|
|
|
void BuildPointerTypeInfo(QualType PointeeTy);
|
|
|
|
|
|
|
|
/// BuildObjCObjectTypeInfo - Build the appropriate kind of
|
|
|
|
/// type_info for an object type.
|
|
|
|
void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
|
2009-12-21 06:30:54 +08:00
|
|
|
|
|
|
|
/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
|
|
|
|
/// struct, used for member pointer types.
|
|
|
|
void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
|
|
|
|
|
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-12-11 10:46:30 +08:00
|
|
|
llvm::Constant *BuildName(QualType Ty, bool Hidden,
|
|
|
|
llvm::GlobalVariable::LinkageTypes Linkage) {
|
2009-11-14 22:25:18 +08:00
|
|
|
llvm::SmallString<256> OutName;
|
2010-08-31 15:33:07 +08:00
|
|
|
CGM.getCXXABI().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-21 06:30:54 +08:00
|
|
|
llvm::GlobalVariable *OGV = CGM.getModule().getNamedGlobal(Name);
|
2009-12-11 10:46:30 +08:00
|
|
|
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();
|
|
|
|
}
|
2010-10-11 11:25:53 +08:00
|
|
|
if (Hidden && Linkage != llvm::GlobalValue::InternalLinkage)
|
2009-11-18 11:46:51 +08:00
|
|
|
GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
|
|
|
|
return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
|
2009-12-20 01:50:07 +08:00
|
|
|
}
|
2009-11-14 22:25:18 +08:00
|
|
|
|
2009-12-24 10:33:48 +08:00
|
|
|
// FIXME: unify with DecideExtern
|
2009-11-19 09:08:19 +08:00
|
|
|
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());
|
2009-12-24 10:33:48 +08:00
|
|
|
if (const FunctionType *FT = Ty->getAs<FunctionType>()) {
|
|
|
|
if (DecideHidden(FT->getResultType()) == false)
|
|
|
|
return false;
|
|
|
|
if (const FunctionProtoType *FPT = Ty->getAs<FunctionProtoType>()) {
|
|
|
|
for (unsigned i = 0; i <FPT->getNumArgs(); ++i)
|
|
|
|
if (DecideHidden(FPT->getArgType(i)) == false)
|
|
|
|
return false;
|
|
|
|
for (unsigned i = 0; i <FPT->getNumExceptions(); ++i)
|
|
|
|
if (DecideHidden(FPT->getExceptionType(i)) == false)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2009-11-19 09:08:19 +08:00
|
|
|
if (const RecordType *RT = Ty->getAs<RecordType>())
|
|
|
|
if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
|
2010-10-23 05:05:15 +08:00
|
|
|
return RD->getVisibility() == HiddenVisibility;
|
2009-11-19 09:08:19 +08:00
|
|
|
return false;
|
|
|
|
}
|
2009-12-11 10:46:30 +08:00
|
|
|
|
2009-12-21 06:30:54 +08:00
|
|
|
// Pointer type info flags.
|
|
|
|
enum {
|
|
|
|
/// PTI_Const - Type has const qualifier.
|
|
|
|
PTI_Const = 0x1,
|
|
|
|
|
|
|
|
/// PTI_Volatile - Type has volatile qualifier.
|
|
|
|
PTI_Volatile = 0x2,
|
|
|
|
|
|
|
|
/// PTI_Restrict - Type has restrict qualifier.
|
|
|
|
PTI_Restrict = 0x4,
|
|
|
|
|
|
|
|
/// PTI_Incomplete - Type is incomplete.
|
|
|
|
PTI_Incomplete = 0x8,
|
|
|
|
|
|
|
|
/// PTI_ContainingClassIncomplete - Containing class is incomplete.
|
|
|
|
/// (in pointer to member).
|
|
|
|
PTI_ContainingClassIncomplete = 0x10
|
|
|
|
};
|
2009-12-31 07:47:56 +08:00
|
|
|
|
|
|
|
// VMI type info flags.
|
|
|
|
enum {
|
|
|
|
/// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
|
|
|
|
VMI_NonDiamondRepeat = 0x1,
|
|
|
|
|
|
|
|
/// VMI_DiamondShaped - Class is diamond shaped.
|
|
|
|
VMI_DiamondShaped = 0x2
|
|
|
|
};
|
|
|
|
|
|
|
|
// Base class type info flags.
|
|
|
|
enum {
|
|
|
|
/// BCTI_Virtual - Base class is virtual.
|
|
|
|
BCTI_Virtual = 0x1,
|
|
|
|
|
|
|
|
/// BCTI_Public - Base class is public.
|
|
|
|
BCTI_Public = 0x2
|
|
|
|
};
|
2010-01-01 01:43:53 +08:00
|
|
|
|
|
|
|
/// BuildTypeInfo - Build the RTTI type info struct for the given type.
|
2010-04-30 09:15:21 +08:00
|
|
|
///
|
|
|
|
/// \param Force - true to force the creation of this RTTI value
|
|
|
|
/// \param ForEH - true if this is for exception handling
|
2010-03-27 10:52:14 +08:00
|
|
|
llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false);
|
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-17 15:09:17 +08:00
|
|
|
llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
|
|
|
|
// Mangle the RTTI name.
|
|
|
|
llvm::SmallString<256> OutName;
|
2010-08-31 15:33:07 +08:00
|
|
|
CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, OutName);
|
2009-12-17 15:09:17 +08:00
|
|
|
llvm::StringRef Name = OutName.str();
|
|
|
|
|
2009-12-21 06:30:54 +08:00
|
|
|
// Look for an existing global.
|
|
|
|
llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
|
2009-12-17 15:09:17 +08:00
|
|
|
|
|
|
|
if (!GV) {
|
|
|
|
// Create a new global variable.
|
|
|
|
GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
|
|
|
|
llvm::GlobalValue::ExternalLinkage, 0, Name);
|
2009-12-11 10:46:30 +08:00
|
|
|
}
|
|
|
|
|
2009-12-17 15:09:17 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
|
2009-12-11 10:46:30 +08:00
|
|
|
}
|
|
|
|
|
2009-12-21 06:30:54 +08:00
|
|
|
/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
|
|
|
|
/// info for that type is defined in the standard library.
|
|
|
|
static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
|
|
|
|
// Itanium C++ ABI 2.9.2:
|
|
|
|
// Basic type information (e.g. for "int", "bool", etc.) will be kept in
|
|
|
|
// the run-time support library. Specifically, the run-time support
|
|
|
|
// library should contain type_info objects for the types X, X* and
|
|
|
|
// X const*, for every X in: void, bool, wchar_t, char, unsigned char,
|
|
|
|
// signed char, short, unsigned short, int, unsigned int, long,
|
|
|
|
// unsigned long, long long, unsigned long long, float, double, long double,
|
|
|
|
// char16_t, char32_t, and the IEEE 754r decimal and half-precision
|
|
|
|
// floating point types.
|
|
|
|
switch (Ty->getKind()) {
|
|
|
|
case BuiltinType::Void:
|
|
|
|
case BuiltinType::Bool:
|
|
|
|
case BuiltinType::WChar:
|
|
|
|
case BuiltinType::Char_U:
|
|
|
|
case BuiltinType::Char_S:
|
|
|
|
case BuiltinType::UChar:
|
|
|
|
case BuiltinType::SChar:
|
|
|
|
case BuiltinType::Short:
|
|
|
|
case BuiltinType::UShort:
|
|
|
|
case BuiltinType::Int:
|
|
|
|
case BuiltinType::UInt:
|
|
|
|
case BuiltinType::Long:
|
|
|
|
case BuiltinType::ULong:
|
|
|
|
case BuiltinType::LongLong:
|
|
|
|
case BuiltinType::ULongLong:
|
|
|
|
case BuiltinType::Float:
|
|
|
|
case BuiltinType::Double:
|
|
|
|
case BuiltinType::LongDouble:
|
|
|
|
case BuiltinType::Char16:
|
|
|
|
case BuiltinType::Char32:
|
|
|
|
case BuiltinType::Int128:
|
|
|
|
case BuiltinType::UInt128:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case BuiltinType::Overload:
|
|
|
|
case BuiltinType::Dependent:
|
|
|
|
case BuiltinType::UndeducedAuto:
|
|
|
|
assert(false && "Should not see this type here!");
|
|
|
|
|
|
|
|
case BuiltinType::NullPtr:
|
|
|
|
assert(false && "FIXME: nullptr_t is not handled!");
|
|
|
|
|
|
|
|
case BuiltinType::ObjCId:
|
|
|
|
case BuiltinType::ObjCClass:
|
|
|
|
case BuiltinType::ObjCSel:
|
|
|
|
assert(false && "FIXME: Objective-C types are unsupported!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Silent gcc.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
|
|
|
|
QualType PointeeTy = PointerTy->getPointeeType();
|
|
|
|
const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
|
|
|
|
if (!BuiltinTy)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check the qualifiers.
|
|
|
|
Qualifiers Quals = PointeeTy.getQualifiers();
|
|
|
|
Quals.removeConst();
|
|
|
|
|
|
|
|
if (!Quals.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return TypeInfoIsInStandardLibrary(BuiltinTy);
|
|
|
|
}
|
|
|
|
|
2010-08-04 16:34:44 +08:00
|
|
|
/// IsStandardLibraryRTTIDescriptor - Returns whether the type
|
|
|
|
/// information for the given type exists in the standard library.
|
|
|
|
static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
|
2009-12-21 06:30:54 +08:00
|
|
|
// Type info for builtin types is defined in the standard library.
|
|
|
|
if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
|
|
|
|
return TypeInfoIsInStandardLibrary(BuiltinTy);
|
|
|
|
|
|
|
|
// Type info for some pointer types to builtin types is defined in the
|
|
|
|
// standard library.
|
|
|
|
if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
|
|
|
|
return TypeInfoIsInStandardLibrary(PointerTy);
|
|
|
|
|
2010-08-04 16:34:44 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
|
|
|
|
/// the given type exists somewhere else, and that we should not emit the type
|
|
|
|
/// information in this translation unit. Assumes that it is not a
|
|
|
|
/// standard-library type.
|
2010-10-11 11:25:57 +08:00
|
|
|
static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM, QualType Ty) {
|
|
|
|
ASTContext &Context = CGM.getContext();
|
|
|
|
|
2010-04-30 09:15:21 +08:00
|
|
|
// If RTTI is disabled, don't consider key functions.
|
|
|
|
if (!Context.getLangOptions().RTTI) return false;
|
|
|
|
|
2009-12-21 06:30:54 +08:00
|
|
|
if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
|
2009-12-21 08:41:42 +08:00
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
|
2010-02-05 06:26:26 +08:00
|
|
|
if (!RD->hasDefinition())
|
|
|
|
return false;
|
|
|
|
|
2009-12-21 08:41:42 +08:00
|
|
|
if (!RD->isDynamicClass())
|
|
|
|
return false;
|
|
|
|
|
2010-10-11 11:25:57 +08:00
|
|
|
return !CGM.getVTables().ShouldEmitVTableInThisTU(RD);
|
2009-12-21 06:30:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// IsIncompleteClassType - Returns whether the given record type is incomplete.
|
|
|
|
static bool IsIncompleteClassType(const RecordType *RecordTy) {
|
|
|
|
return !RecordTy->getDecl()->isDefinition();
|
|
|
|
}
|
|
|
|
|
2009-12-21 07:37:55 +08:00
|
|
|
/// ContainsIncompleteClassType - Returns whether the given type contains an
|
|
|
|
/// incomplete class type. This is true if
|
|
|
|
///
|
|
|
|
/// * The given type is an incomplete class type.
|
|
|
|
/// * The given type is a pointer type whose pointee type contains an
|
|
|
|
/// incomplete class type.
|
|
|
|
/// * The given type is a member pointer type whose class is an incomplete
|
|
|
|
/// class type.
|
|
|
|
/// * The given type is a member pointer type whoise pointee type contains an
|
|
|
|
/// incomplete class type.
|
2009-12-21 06:30:54 +08:00
|
|
|
/// is an indirect or direct pointer to an incomplete class type.
|
2009-12-21 07:37:55 +08:00
|
|
|
static bool ContainsIncompleteClassType(QualType Ty) {
|
|
|
|
if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
|
|
|
|
if (IsIncompleteClassType(RecordTy))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
|
|
|
|
return ContainsIncompleteClassType(PointerTy->getPointeeType());
|
|
|
|
|
|
|
|
if (const MemberPointerType *MemberPointerTy =
|
|
|
|
dyn_cast<MemberPointerType>(Ty)) {
|
|
|
|
// Check if the class type is incomplete.
|
|
|
|
const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
|
|
|
|
if (IsIncompleteClassType(ClassType))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
|
2009-12-21 06:30:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getTypeInfoLinkage - Return the linkage that the type info and type info
|
|
|
|
/// name constants should have for the given type.
|
|
|
|
static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(QualType Ty) {
|
2009-12-21 07:37:55 +08:00
|
|
|
// Itanium C++ ABI 2.9.5p7:
|
|
|
|
// In addition, it and all of the intermediate abi::__pointer_type_info
|
|
|
|
// structs in the chain down to the abi::__class_type_info for the
|
|
|
|
// incomplete class type must be prevented from resolving to the
|
|
|
|
// corresponding type_info structs for the complete class type, possibly
|
|
|
|
// by making them local static objects. Finally, a dummy class RTTI is
|
|
|
|
// generated for the incomplete type that will not resolve to the final
|
|
|
|
// complete class RTTI (because the latter need not exist), possibly by
|
|
|
|
// making it a local static object.
|
|
|
|
if (ContainsIncompleteClassType(Ty))
|
|
|
|
return llvm::GlobalValue::InternalLinkage;
|
2009-12-21 08:41:42 +08:00
|
|
|
|
2010-03-31 08:15:35 +08:00
|
|
|
switch (Ty->getLinkage()) {
|
|
|
|
case NoLinkage:
|
|
|
|
case InternalLinkage:
|
|
|
|
case UniqueExternalLinkage:
|
|
|
|
return llvm::GlobalValue::InternalLinkage;
|
2009-12-24 09:10:27 +08:00
|
|
|
|
2010-03-31 08:15:35 +08:00
|
|
|
case ExternalLinkage:
|
|
|
|
if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
|
|
|
|
const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
|
|
|
|
if (RD->isDynamicClass())
|
2010-04-18 04:15:18 +08:00
|
|
|
return CodeGenModule::getVTableLinkage(RD);
|
2009-12-24 09:10:27 +08:00
|
|
|
}
|
2009-12-30 05:58:32 +08:00
|
|
|
|
2010-03-31 08:15:35 +08:00
|
|
|
return llvm::GlobalValue::WeakODRLinkage;
|
2009-12-30 05:58:32 +08:00
|
|
|
}
|
2009-12-24 09:10:27 +08:00
|
|
|
|
2009-12-21 06:30:54 +08:00
|
|
|
return llvm::GlobalValue::WeakODRLinkage;
|
|
|
|
}
|
|
|
|
|
2009-12-30 09:00:12 +08:00
|
|
|
// CanUseSingleInheritance - Return whether the given record decl has a "single,
|
|
|
|
// public, non-virtual base at offset zero (i.e. the derived class is dynamic
|
|
|
|
// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
|
|
|
|
static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
|
|
|
|
// Check the number of bases.
|
|
|
|
if (RD->getNumBases() != 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Get the base.
|
|
|
|
CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
|
|
|
|
|
|
|
|
// Check that the base is not virtual.
|
|
|
|
if (Base->isVirtual())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check that the base is public.
|
|
|
|
if (Base->getAccessSpecifier() != AS_public)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check that the class is dynamic iff the base is.
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
if (!BaseDecl->isEmpty() &&
|
|
|
|
BaseDecl->isDynamicClass() != RD->isDynamicClass())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-18 04:15:18 +08:00
|
|
|
void RTTIBuilder::BuildVTablePointer(const Type *Ty) {
|
2010-08-12 10:17:33 +08:00
|
|
|
// abi::__class_type_info.
|
|
|
|
static const char * const ClassTypeInfo =
|
|
|
|
"_ZTVN10__cxxabiv117__class_type_infoE";
|
|
|
|
// abi::__si_class_type_info.
|
|
|
|
static const char * const SIClassTypeInfo =
|
|
|
|
"_ZTVN10__cxxabiv120__si_class_type_infoE";
|
|
|
|
// abi::__vmi_class_type_info.
|
|
|
|
static const char * const VMIClassTypeInfo =
|
|
|
|
"_ZTVN10__cxxabiv121__vmi_class_type_infoE";
|
|
|
|
|
2010-08-12 04:41:51 +08:00
|
|
|
const char *VTableName = 0;
|
2009-12-21 06:30:54 +08:00
|
|
|
|
|
|
|
switch (Ty->getTypeClass()) {
|
2010-08-12 04:41:51 +08:00
|
|
|
#define TYPE(Class, Base)
|
|
|
|
#define ABSTRACT_TYPE(Class, Base)
|
|
|
|
#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
|
|
|
|
#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
|
|
|
|
#define DEPENDENT_TYPE(Class, Base) case Type::Class:
|
|
|
|
#include "clang/AST/TypeNodes.def"
|
|
|
|
assert(false && "Non-canonical and dependent types shouldn't get here");
|
|
|
|
|
|
|
|
case Type::LValueReference:
|
|
|
|
case Type::RValueReference:
|
|
|
|
assert(false && "References shouldn't get here");
|
2009-12-30 05:58:32 +08:00
|
|
|
|
2010-03-27 10:52:14 +08:00
|
|
|
case Type::Builtin:
|
2010-08-12 04:41:51 +08:00
|
|
|
// GCC treats vector and complex types as fundamental types.
|
2009-12-30 06:30:11 +08:00
|
|
|
case Type::Vector:
|
|
|
|
case Type::ExtVector:
|
2010-08-12 04:41:51 +08:00
|
|
|
case Type::Complex:
|
|
|
|
// FIXME: GCC treats block pointers as fundamental types?!
|
|
|
|
case Type::BlockPointer:
|
2009-12-31 07:47:56 +08:00
|
|
|
// abi::__fundamental_type_info.
|
2010-04-18 04:15:18 +08:00
|
|
|
VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
|
2009-12-30 06:30:11 +08:00
|
|
|
break;
|
|
|
|
|
2009-12-30 05:58:32 +08:00
|
|
|
case Type::ConstantArray:
|
|
|
|
case Type::IncompleteArray:
|
2010-08-12 04:41:51 +08:00
|
|
|
case Type::VariableArray:
|
2009-12-31 07:47:56 +08:00
|
|
|
// abi::__array_type_info.
|
2010-04-18 04:15:18 +08:00
|
|
|
VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
|
2009-12-30 05:58:32 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::FunctionNoProto:
|
|
|
|
case Type::FunctionProto:
|
2009-12-31 07:47:56 +08:00
|
|
|
// abi::__function_type_info.
|
2010-04-18 04:15:18 +08:00
|
|
|
VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
|
2009-12-30 05:58:32 +08:00
|
|
|
break;
|
|
|
|
|
2009-12-30 06:13:01 +08:00
|
|
|
case Type::Enum:
|
2009-12-31 07:47:56 +08:00
|
|
|
// abi::__enum_type_info.
|
2010-04-18 04:15:18 +08:00
|
|
|
VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
|
2009-12-30 06:13:01 +08:00
|
|
|
break;
|
2010-08-12 10:17:33 +08:00
|
|
|
|
2009-12-21 08:41:42 +08:00
|
|
|
case Type::Record: {
|
|
|
|
const CXXRecordDecl *RD =
|
|
|
|
cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
|
2009-12-31 07:47:56 +08:00
|
|
|
|
2010-02-05 06:26:26 +08:00
|
|
|
if (!RD->hasDefinition() || !RD->getNumBases()) {
|
2010-08-12 10:17:33 +08:00
|
|
|
VTableName = ClassTypeInfo;
|
2009-12-30 09:00:12 +08:00
|
|
|
} else if (CanUseSingleInheritance(RD)) {
|
2010-08-12 10:17:33 +08:00
|
|
|
VTableName = SIClassTypeInfo;
|
2009-12-30 09:00:12 +08:00
|
|
|
} else {
|
2010-08-12 10:17:33 +08:00
|
|
|
VTableName = VMIClassTypeInfo;
|
2009-12-21 08:41:42 +08:00
|
|
|
}
|
2009-12-30 09:00:12 +08:00
|
|
|
|
|
|
|
break;
|
2009-12-21 08:41:42 +08:00
|
|
|
}
|
|
|
|
|
2010-08-12 04:41:51 +08:00
|
|
|
case Type::ObjCObject:
|
2010-08-12 10:17:33 +08:00
|
|
|
// Ignore protocol qualifiers.
|
|
|
|
Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
|
|
|
|
|
|
|
|
// Handle id and Class.
|
|
|
|
if (isa<BuiltinType>(Ty)) {
|
|
|
|
VTableName = ClassTypeInfo;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(isa<ObjCInterfaceType>(Ty));
|
|
|
|
// Fall through.
|
|
|
|
|
2010-08-12 04:41:51 +08:00
|
|
|
case Type::ObjCInterface:
|
2010-08-12 10:17:33 +08:00
|
|
|
if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
|
|
|
|
VTableName = SIClassTypeInfo;
|
|
|
|
} else {
|
|
|
|
VTableName = ClassTypeInfo;
|
|
|
|
}
|
2010-08-12 04:41:51 +08:00
|
|
|
break;
|
|
|
|
|
2010-08-12 10:17:33 +08:00
|
|
|
case Type::ObjCObjectPointer:
|
2009-12-21 06:30:54 +08:00
|
|
|
case Type::Pointer:
|
2009-12-31 07:47:56 +08:00
|
|
|
// abi::__pointer_type_info.
|
2010-04-18 04:15:18 +08:00
|
|
|
VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
|
2009-12-21 06:30:54 +08:00
|
|
|
break;
|
2009-12-30 05:58:32 +08:00
|
|
|
|
2009-12-21 06:30:54 +08:00
|
|
|
case Type::MemberPointer:
|
2009-12-31 07:47:56 +08:00
|
|
|
// abi::__pointer_to_member_type_info.
|
2010-04-18 04:15:18 +08:00
|
|
|
VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
|
2009-12-21 06:30:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-04-18 04:15:18 +08:00
|
|
|
llvm::Constant *VTable =
|
|
|
|
CGM.getModule().getOrInsertGlobal(VTableName, Int8PtrTy);
|
2009-12-21 06:30:54 +08:00
|
|
|
|
|
|
|
const llvm::Type *PtrDiffTy =
|
|
|
|
CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
|
|
|
|
|
|
|
|
// The vtable address point is 2.
|
|
|
|
llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
|
2010-04-18 04:15:18 +08:00
|
|
|
VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, &Two, 1);
|
|
|
|
VTable = llvm::ConstantExpr::getBitCast(VTable, Int8PtrTy);
|
2009-12-21 06:30:54 +08:00
|
|
|
|
2010-04-18 04:15:18 +08:00
|
|
|
Fields.push_back(VTable);
|
2009-12-21 06:30:54 +08:00
|
|
|
}
|
|
|
|
|
2010-08-04 16:34:44 +08:00
|
|
|
llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
|
2009-12-21 06:30:54 +08:00
|
|
|
// We want to operate on the canonical type.
|
|
|
|
Ty = CGM.getContext().getCanonicalType(Ty);
|
|
|
|
|
|
|
|
// Check if we've already emitted an RTTI descriptor for this type.
|
|
|
|
llvm::SmallString<256> OutName;
|
2010-08-31 15:33:07 +08:00
|
|
|
CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, OutName);
|
2009-12-21 06:30:54 +08:00
|
|
|
llvm::StringRef Name = OutName.str();
|
|
|
|
|
|
|
|
llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
|
|
|
|
if (OldGV && !OldGV->isDeclaration())
|
|
|
|
return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
|
2010-08-04 16:34:44 +08:00
|
|
|
|
2009-12-21 06:30:54 +08:00
|
|
|
// Check if there is already an external RTTI descriptor for this type.
|
2010-08-04 16:34:44 +08:00
|
|
|
bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
|
2010-10-11 11:25:57 +08:00
|
|
|
if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
|
2009-12-21 06:30:54 +08:00
|
|
|
return GetAddrOfExternalRTTIDescriptor(Ty);
|
|
|
|
|
2010-08-04 16:34:44 +08:00
|
|
|
// Emit the standard library with external linkage.
|
|
|
|
llvm::GlobalVariable::LinkageTypes Linkage;
|
|
|
|
if (IsStdLib)
|
|
|
|
Linkage = llvm::GlobalValue::ExternalLinkage;
|
|
|
|
else
|
|
|
|
Linkage = getTypeInfoLinkage(Ty);
|
2009-12-21 06:30:54 +08:00
|
|
|
|
|
|
|
// Add the vtable pointer.
|
2010-04-18 04:15:18 +08:00
|
|
|
BuildVTablePointer(cast<Type>(Ty));
|
2009-12-21 06:30:54 +08:00
|
|
|
|
|
|
|
// And the name.
|
2010-01-01 01:43:53 +08:00
|
|
|
Fields.push_back(BuildName(Ty, DecideHidden(Ty), Linkage));
|
2010-08-04 16:34:44 +08:00
|
|
|
|
2009-12-21 06:30:54 +08:00
|
|
|
switch (Ty->getTypeClass()) {
|
2010-08-15 08:24:31 +08:00
|
|
|
#define TYPE(Class, Base)
|
|
|
|
#define ABSTRACT_TYPE(Class, Base)
|
|
|
|
#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
|
|
|
|
#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
|
|
|
|
#define DEPENDENT_TYPE(Class, Base) case Type::Class:
|
|
|
|
#include "clang/AST/TypeNodes.def"
|
|
|
|
assert(false && "Non-canonical and dependent types shouldn't get here");
|
2009-12-21 06:30:54 +08:00
|
|
|
|
2009-12-30 06:30:11 +08:00
|
|
|
// GCC treats vector types as fundamental types.
|
2010-03-27 10:52:14 +08:00
|
|
|
case Type::Builtin:
|
2009-12-30 06:30:11 +08:00
|
|
|
case Type::Vector:
|
|
|
|
case Type::ExtVector:
|
2010-08-15 08:24:31 +08:00
|
|
|
case Type::Complex:
|
|
|
|
case Type::BlockPointer:
|
2009-12-30 06:30:11 +08:00
|
|
|
// Itanium C++ ABI 2.9.5p4:
|
|
|
|
// abi::__fundamental_type_info adds no data members to std::type_info.
|
|
|
|
break;
|
2010-08-15 08:24:31 +08:00
|
|
|
|
|
|
|
case Type::LValueReference:
|
|
|
|
case Type::RValueReference:
|
|
|
|
assert(false && "References shouldn't get here");
|
|
|
|
|
2009-12-30 05:58:32 +08:00
|
|
|
case Type::ConstantArray:
|
|
|
|
case Type::IncompleteArray:
|
2010-08-15 08:24:31 +08:00
|
|
|
case Type::VariableArray:
|
2009-12-30 06:30:11 +08:00
|
|
|
// Itanium C++ ABI 2.9.5p5:
|
|
|
|
// abi::__array_type_info adds no data members to std::type_info.
|
2009-12-30 05:58:32 +08:00
|
|
|
break;
|
|
|
|
|
2009-12-30 04:20:19 +08:00
|
|
|
case Type::FunctionNoProto:
|
|
|
|
case Type::FunctionProto:
|
2009-12-30 06:30:11 +08:00
|
|
|
// Itanium C++ ABI 2.9.5p5:
|
|
|
|
// abi::__function_type_info adds no data members to std::type_info.
|
2009-12-30 04:20:19 +08:00
|
|
|
break;
|
|
|
|
|
2009-12-30 06:13:01 +08:00
|
|
|
case Type::Enum:
|
2009-12-30 06:30:11 +08:00
|
|
|
// Itanium C++ ABI 2.9.5p5:
|
|
|
|
// abi::__enum_type_info adds no data members to std::type_info.
|
2009-12-30 06:13:01 +08:00
|
|
|
break;
|
|
|
|
|
2009-12-21 08:41:42 +08:00
|
|
|
case Type::Record: {
|
|
|
|
const CXXRecordDecl *RD =
|
|
|
|
cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
|
2010-02-05 06:26:26 +08:00
|
|
|
if (!RD->hasDefinition() || !RD->getNumBases()) {
|
2009-12-21 08:41:42 +08:00
|
|
|
// We don't need to emit any fields.
|
|
|
|
break;
|
|
|
|
}
|
2009-12-30 09:00:12 +08:00
|
|
|
|
2009-12-31 07:47:56 +08:00
|
|
|
if (CanUseSingleInheritance(RD))
|
2009-12-30 09:00:12 +08:00
|
|
|
BuildSIClassTypeInfo(RD);
|
2009-12-31 07:47:56 +08:00
|
|
|
else
|
|
|
|
BuildVMIClassTypeInfo(RD);
|
|
|
|
|
|
|
|
break;
|
2009-12-21 08:41:42 +08:00
|
|
|
}
|
2010-08-12 10:17:33 +08:00
|
|
|
|
|
|
|
case Type::ObjCObject:
|
|
|
|
case Type::ObjCInterface:
|
|
|
|
BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Type::ObjCObjectPointer:
|
|
|
|
BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
|
|
|
|
break;
|
2009-12-21 08:41:42 +08:00
|
|
|
|
2009-12-21 06:30:54 +08:00
|
|
|
case Type::Pointer:
|
2010-08-12 10:17:33 +08:00
|
|
|
BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
|
2009-12-21 06:30:54 +08:00
|
|
|
break;
|
2010-08-12 10:17:33 +08:00
|
|
|
|
2009-12-21 06:30:54 +08:00
|
|
|
case Type::MemberPointer:
|
|
|
|
BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Constant *Init =
|
2010-01-01 01:43:53 +08:00
|
|
|
llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
|
2009-12-21 06:30:54 +08:00
|
|
|
/*Packed=*/false);
|
|
|
|
|
|
|
|
llvm::GlobalVariable *GV =
|
|
|
|
new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
|
|
|
|
/*Constant=*/true, Linkage, Init, Name);
|
|
|
|
|
|
|
|
// If there's already an old global variable, replace it with the new one.
|
|
|
|
if (OldGV) {
|
|
|
|
GV->takeName(OldGV);
|
|
|
|
llvm::Constant *NewPtr =
|
|
|
|
llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
|
|
|
|
OldGV->replaceAllUsesWith(NewPtr);
|
|
|
|
OldGV->eraseFromParent();
|
|
|
|
}
|
2010-08-04 16:34:44 +08:00
|
|
|
|
|
|
|
// GCC only relies on the uniqueness of the type names, not the
|
|
|
|
// type_infos themselves, so we can emit these as hidden symbols.
|
2010-08-13 07:36:15 +08:00
|
|
|
// But don't do this if we're worried about strict visibility
|
|
|
|
// compatibility.
|
2010-08-04 16:34:44 +08:00
|
|
|
if (const RecordType *RT = dyn_cast<RecordType>(Ty))
|
|
|
|
CGM.setTypeVisibility(GV, cast<CXXRecordDecl>(RT->getDecl()),
|
2010-10-30 19:50:40 +08:00
|
|
|
/*ForRTTI*/ true, /*ForDefinition*/ true);
|
2010-08-13 07:36:15 +08:00
|
|
|
else if (CGM.getCodeGenOpts().HiddenWeakVTables &&
|
|
|
|
Linkage == llvm::GlobalValue::WeakODRLinkage)
|
2010-08-04 16:34:44 +08:00
|
|
|
GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
|
|
|
|
|
2009-12-21 06:30:54 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
|
|
|
|
}
|
|
|
|
|
2009-12-31 07:47:56 +08:00
|
|
|
/// ComputeQualifierFlags - Compute the pointer type info flags from the
|
2009-12-21 06:30:54 +08:00
|
|
|
/// given qualifier.
|
2009-12-31 07:47:56 +08:00
|
|
|
static unsigned ComputeQualifierFlags(Qualifiers Quals) {
|
2009-12-21 06:30:54 +08:00
|
|
|
unsigned Flags = 0;
|
|
|
|
|
|
|
|
if (Quals.hasConst())
|
|
|
|
Flags |= RTTIBuilder::PTI_Const;
|
|
|
|
if (Quals.hasVolatile())
|
|
|
|
Flags |= RTTIBuilder::PTI_Volatile;
|
|
|
|
if (Quals.hasRestrict())
|
|
|
|
Flags |= RTTIBuilder::PTI_Restrict;
|
|
|
|
|
|
|
|
return Flags;
|
|
|
|
}
|
|
|
|
|
2010-08-12 10:17:33 +08:00
|
|
|
/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
|
|
|
|
/// for the given Objective-C object type.
|
|
|
|
void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
|
|
|
|
// Drop qualifiers.
|
|
|
|
const Type *T = OT->getBaseType().getTypePtr();
|
|
|
|
assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
|
|
|
|
|
|
|
|
// The builtin types are abi::__class_type_infos and don't require
|
|
|
|
// extra fields.
|
|
|
|
if (isa<BuiltinType>(T)) return;
|
|
|
|
|
|
|
|
ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
|
|
|
|
ObjCInterfaceDecl *Super = Class->getSuperClass();
|
|
|
|
|
|
|
|
// Root classes are also __class_type_info.
|
|
|
|
if (!Super) return;
|
|
|
|
|
|
|
|
QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
|
|
|
|
|
|
|
|
// Everything else is single inheritance.
|
|
|
|
llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
|
|
|
|
Fields.push_back(BaseTypeInfo);
|
|
|
|
}
|
|
|
|
|
2009-12-30 09:00:12 +08:00
|
|
|
/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
|
|
|
|
/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
|
|
|
|
void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
|
|
|
|
// Itanium C++ ABI 2.9.5p6b:
|
|
|
|
// It adds to abi::__class_type_info a single member pointing to the
|
|
|
|
// type_info structure for the base type,
|
2010-01-01 01:43:53 +08:00
|
|
|
llvm::Constant *BaseTypeInfo =
|
|
|
|
RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
|
|
|
|
Fields.push_back(BaseTypeInfo);
|
2009-12-30 09:00:12 +08:00
|
|
|
}
|
|
|
|
|
2010-10-23 00:48:22 +08:00
|
|
|
namespace {
|
|
|
|
/// SeenBases - Contains virtual and non-virtual bases seen when traversing
|
|
|
|
/// a class hierarchy.
|
|
|
|
struct SeenBases {
|
|
|
|
llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
|
|
|
|
llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
|
|
|
|
};
|
|
|
|
}
|
2009-12-31 07:47:56 +08:00
|
|
|
|
|
|
|
/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
|
|
|
|
/// abi::__vmi_class_type_info.
|
|
|
|
///
|
|
|
|
static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
|
|
|
|
SeenBases &Bases) {
|
|
|
|
|
|
|
|
unsigned Flags = 0;
|
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
if (Base->isVirtual()) {
|
|
|
|
if (Bases.VirtualBases.count(BaseDecl)) {
|
|
|
|
// If this virtual base has been seen before, then the class is diamond
|
|
|
|
// shaped.
|
|
|
|
Flags |= RTTIBuilder::VMI_DiamondShaped;
|
|
|
|
} else {
|
|
|
|
if (Bases.NonVirtualBases.count(BaseDecl))
|
|
|
|
Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
|
|
|
|
|
|
|
|
// Mark the virtual base as seen.
|
|
|
|
Bases.VirtualBases.insert(BaseDecl);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (Bases.NonVirtualBases.count(BaseDecl)) {
|
|
|
|
// If this non-virtual base has been seen before, then the class has non-
|
|
|
|
// diamond shaped repeated inheritance.
|
|
|
|
Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
|
|
|
|
} else {
|
|
|
|
if (Bases.VirtualBases.count(BaseDecl))
|
|
|
|
Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
|
|
|
|
|
|
|
|
// Mark the non-virtual base as seen.
|
|
|
|
Bases.NonVirtualBases.insert(BaseDecl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk all bases.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
|
|
|
|
E = BaseDecl->bases_end(); I != E; ++I)
|
|
|
|
Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
|
|
|
|
|
|
|
|
return Flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
|
|
|
|
unsigned Flags = 0;
|
|
|
|
SeenBases Bases;
|
|
|
|
|
|
|
|
// Walk all bases.
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I)
|
|
|
|
Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
|
|
|
|
|
|
|
|
return Flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
|
|
|
|
/// classes with bases that do not satisfy the abi::__si_class_type_info
|
|
|
|
/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
|
|
|
|
void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
|
|
|
|
const llvm::Type *UnsignedIntLTy =
|
|
|
|
CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
|
|
|
|
|
|
|
|
// Itanium C++ ABI 2.9.5p6c:
|
|
|
|
// __flags is a word with flags describing details about the class
|
|
|
|
// structure, which may be referenced by using the __flags_masks
|
|
|
|
// enumeration. These flags refer to both direct and indirect bases.
|
|
|
|
unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
|
2010-01-01 01:43:53 +08:00
|
|
|
Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
|
2009-12-31 07:47:56 +08:00
|
|
|
|
|
|
|
// Itanium C++ ABI 2.9.5p6c:
|
|
|
|
// __base_count is a word with the number of direct proper base class
|
|
|
|
// descriptions that follow.
|
2010-01-01 01:43:53 +08:00
|
|
|
Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
|
2009-12-31 07:47:56 +08:00
|
|
|
|
|
|
|
if (!RD->getNumBases())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const llvm::Type *LongLTy =
|
|
|
|
CGM.getTypes().ConvertType(CGM.getContext().LongTy);
|
|
|
|
|
|
|
|
// Now add the base class descriptions.
|
|
|
|
|
|
|
|
// Itanium C++ ABI 2.9.5p6c:
|
|
|
|
// __base_info[] is an array of base class descriptions -- one for every
|
|
|
|
// direct proper base. Each description is of the type:
|
|
|
|
//
|
|
|
|
// struct abi::__base_class_type_info {
|
2010-08-22 09:00:03 +08:00
|
|
|
// public:
|
2009-12-31 07:47:56 +08:00
|
|
|
// const __class_type_info *__base_type;
|
|
|
|
// long __offset_flags;
|
|
|
|
//
|
|
|
|
// enum __offset_flags_masks {
|
|
|
|
// __virtual_mask = 0x1,
|
|
|
|
// __public_mask = 0x2,
|
|
|
|
// __offset_shift = 8
|
|
|
|
// };
|
|
|
|
// };
|
|
|
|
for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
|
|
|
|
E = RD->bases_end(); I != E; ++I) {
|
|
|
|
const CXXBaseSpecifier *Base = I;
|
|
|
|
|
|
|
|
// The __base_type member points to the RTTI for the base type.
|
2010-01-01 01:43:53 +08:00
|
|
|
Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
|
2009-12-31 07:47:56 +08:00
|
|
|
|
|
|
|
const CXXRecordDecl *BaseDecl =
|
|
|
|
cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
|
|
|
|
|
|
|
|
int64_t OffsetFlags = 0;
|
|
|
|
|
|
|
|
// All but the lower 8 bits of __offset_flags are a signed offset.
|
|
|
|
// For a non-virtual base, this is the offset in the object of the base
|
|
|
|
// subobject. For a virtual base, this is the offset in the virtual table of
|
|
|
|
// the virtual base offset for the virtual base referenced (negative).
|
|
|
|
if (Base->isVirtual())
|
2010-03-23 12:11:45 +08:00
|
|
|
OffsetFlags = CGM.getVTables().getVirtualBaseOffsetOffset(RD, BaseDecl);
|
2009-12-31 07:47:56 +08:00
|
|
|
else {
|
|
|
|
const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
|
2010-11-01 07:22:37 +08:00
|
|
|
OffsetFlags = Layout.getBaseClassOffsetInBits(BaseDecl) / 8;
|
2009-12-31 07:47:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
OffsetFlags <<= 8;
|
|
|
|
|
|
|
|
// The low-order byte of __offset_flags contains flags, as given by the
|
|
|
|
// masks from the enumeration __offset_flags_masks.
|
|
|
|
if (Base->isVirtual())
|
|
|
|
OffsetFlags |= BCTI_Virtual;
|
|
|
|
if (Base->getAccessSpecifier() == AS_public)
|
|
|
|
OffsetFlags |= BCTI_Public;
|
|
|
|
|
2010-01-01 01:43:53 +08:00
|
|
|
Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
|
2009-12-31 07:47:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-21 06:30:54 +08:00
|
|
|
/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
|
|
|
|
/// used for pointer types.
|
2010-08-12 10:17:33 +08:00
|
|
|
void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
|
2010-06-02 23:44:35 +08:00
|
|
|
Qualifiers Quals;
|
|
|
|
QualType UnqualifiedPointeeTy =
|
|
|
|
CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
|
|
|
|
|
2009-12-21 06:30:54 +08:00
|
|
|
// Itanium C++ ABI 2.9.5p7:
|
|
|
|
// __flags is a flag word describing the cv-qualification and other
|
|
|
|
// attributes of the type pointed to
|
2010-06-02 23:44:35 +08:00
|
|
|
unsigned Flags = ComputeQualifierFlags(Quals);
|
2009-12-21 06:30:54 +08:00
|
|
|
|
|
|
|
// Itanium C++ ABI 2.9.5p7:
|
|
|
|
// When the abi::__pbase_type_info is for a direct or indirect pointer to an
|
|
|
|
// incomplete class type, the incomplete target type flag is set.
|
2010-06-02 23:44:35 +08:00
|
|
|
if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
|
2009-12-21 06:30:54 +08:00
|
|
|
Flags |= PTI_Incomplete;
|
|
|
|
|
|
|
|
const llvm::Type *UnsignedIntLTy =
|
|
|
|
CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
|
2010-01-01 01:43:53 +08:00
|
|
|
Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
|
2009-12-21 06:30:54 +08:00
|
|
|
|
|
|
|
// Itanium C++ ABI 2.9.5p7:
|
|
|
|
// __pointee is a pointer to the std::type_info derivation for the
|
|
|
|
// unqualified type being pointed to.
|
2010-01-01 01:43:53 +08:00
|
|
|
llvm::Constant *PointeeTypeInfo =
|
2010-06-02 23:44:35 +08:00
|
|
|
RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
|
2010-01-01 01:43:53 +08:00
|
|
|
Fields.push_back(PointeeTypeInfo);
|
2009-12-21 06:30:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
|
|
|
|
/// struct, used for member pointer types.
|
|
|
|
void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
|
|
|
|
QualType PointeeTy = Ty->getPointeeType();
|
|
|
|
|
2010-06-02 23:44:35 +08:00
|
|
|
Qualifiers Quals;
|
|
|
|
QualType UnqualifiedPointeeTy =
|
|
|
|
CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
|
|
|
|
|
2009-12-21 06:30:54 +08:00
|
|
|
// Itanium C++ ABI 2.9.5p7:
|
|
|
|
// __flags is a flag word describing the cv-qualification and other
|
|
|
|
// attributes of the type pointed to.
|
2010-06-02 23:44:35 +08:00
|
|
|
unsigned Flags = ComputeQualifierFlags(Quals);
|
2009-12-21 06:30:54 +08:00
|
|
|
|
|
|
|
const RecordType *ClassType = cast<RecordType>(Ty->getClass());
|
2009-12-21 07:37:55 +08:00
|
|
|
|
|
|
|
// Itanium C++ ABI 2.9.5p7:
|
|
|
|
// When the abi::__pbase_type_info is for a direct or indirect pointer to an
|
|
|
|
// incomplete class type, the incomplete target type flag is set.
|
2010-06-02 23:44:35 +08:00
|
|
|
if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
|
2009-12-21 07:37:55 +08:00
|
|
|
Flags |= PTI_Incomplete;
|
|
|
|
|
2009-12-21 06:30:54 +08:00
|
|
|
if (IsIncompleteClassType(ClassType))
|
|
|
|
Flags |= PTI_ContainingClassIncomplete;
|
|
|
|
|
|
|
|
const llvm::Type *UnsignedIntLTy =
|
|
|
|
CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
|
2010-01-01 01:43:53 +08:00
|
|
|
Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
|
2009-12-21 06:30:54 +08:00
|
|
|
|
|
|
|
// Itanium C++ ABI 2.9.5p7:
|
|
|
|
// __pointee is a pointer to the std::type_info derivation for the
|
|
|
|
// unqualified type being pointed to.
|
2010-01-01 01:43:53 +08:00
|
|
|
llvm::Constant *PointeeTypeInfo =
|
2010-06-02 23:44:35 +08:00
|
|
|
RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
|
2010-01-01 01:43:53 +08:00
|
|
|
Fields.push_back(PointeeTypeInfo);
|
2009-12-21 06:30:54 +08:00
|
|
|
|
|
|
|
// Itanium C++ ABI 2.9.5p9:
|
|
|
|
// __context is a pointer to an abi::__class_type_info corresponding to the
|
|
|
|
// class type containing the member pointed to
|
|
|
|
// (e.g., the "A" in "int A::*").
|
2010-01-01 01:43:53 +08:00
|
|
|
Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
|
2009-12-21 06:30:54 +08:00
|
|
|
}
|
|
|
|
|
2010-04-30 09:15:21 +08:00
|
|
|
llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
|
|
|
|
bool ForEH) {
|
|
|
|
// Return a bogus pointer if RTTI is disabled, unless it's for EH.
|
|
|
|
// FIXME: should we even be calling this method if RTTI is disabled
|
|
|
|
// and it's not for EH?
|
|
|
|
if (!ForEH && !getContext().getLangOptions().RTTI) {
|
2009-12-11 10:46:30 +08:00
|
|
|
const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
|
|
|
|
return llvm::Constant::getNullValue(Int8PtrTy);
|
|
|
|
}
|
2010-04-30 09:15:21 +08:00
|
|
|
|
2010-01-01 01:43:53 +08:00
|
|
|
return RTTIBuilder(*this).BuildTypeInfo(Ty);
|
2009-12-11 10:46:30 +08:00
|
|
|
}
|
2010-03-27 10:52:14 +08:00
|
|
|
|
|
|
|
void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
|
|
|
|
QualType PointerType = Context.getPointerType(Type);
|
|
|
|
QualType PointerTypeConst = Context.getPointerType(Type.withConst());
|
|
|
|
RTTIBuilder(*this).BuildTypeInfo(Type, true);
|
|
|
|
RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
|
|
|
|
RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenModule::EmitFundamentalRTTIDescriptors() {
|
|
|
|
QualType FundamentalTypes[] = { Context.VoidTy, Context.Char32Ty,
|
|
|
|
Context.Char16Ty, Context.UnsignedLongLongTy,
|
|
|
|
Context.LongLongTy, Context.WCharTy,
|
|
|
|
Context.UnsignedShortTy, Context.ShortTy,
|
|
|
|
Context.UnsignedLongTy, Context.LongTy,
|
|
|
|
Context.UnsignedIntTy, Context.IntTy,
|
|
|
|
Context.UnsignedCharTy, Context.FloatTy,
|
|
|
|
Context.LongDoubleTy, Context.DoubleTy,
|
|
|
|
Context.CharTy, Context.BoolTy,
|
|
|
|
Context.SignedCharTy };
|
|
|
|
for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
|
|
|
|
EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
|
|
|
|
}
|