Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 1 | //===--- CGCXXRTTI.cpp - Emit LLVM Code for C++ RTTI descriptors ----------===// |
Anders Carlsson | 656e4c1 | 2009-10-10 20:49:04 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code dealing with C++ code generation of RTTI descriptors. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Mike Stump | 61c3801 | 2009-11-17 21:44:24 +0000 | [diff] [blame] | 14 | #include "CodeGenModule.h" |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 15 | #include "CGCXXABI.h" |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame] | 16 | #include "clang/AST/RecordLayout.h" |
| 17 | #include "clang/AST/Type.h" |
| 18 | #include "clang/Frontend/CodeGenOptions.h" |
| 19 | |
Anders Carlsson | 656e4c1 | 2009-10-10 20:49:04 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | using namespace CodeGen; |
| 22 | |
Mike Stump | 92f2fe2 | 2009-12-02 19:07:44 +0000 | [diff] [blame] | 23 | namespace { |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 24 | class RTTIBuilder { |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 25 | CodeGenModule &CGM; // Per-module state. |
| 26 | llvm::LLVMContext &VMContext; |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 27 | |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 28 | const llvm::Type *Int8PtrTy; |
Anders Carlsson | 531d55f | 2009-12-31 17:43:53 +0000 | [diff] [blame] | 29 | |
| 30 | /// Fields - The fields of the RTTI descriptor currently being built. |
| 31 | llvm::SmallVector<llvm::Constant *, 16> Fields; |
Anders Carlsson | d6baec8 | 2009-12-11 01:27:37 +0000 | [diff] [blame] | 32 | |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 33 | /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI |
| 34 | /// descriptor of the given type. |
| 35 | llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty); |
| 36 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 37 | /// BuildVTablePointer - Build the vtable pointer for the given type. |
| 38 | void BuildVTablePointer(const Type *Ty); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 39 | |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 40 | /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 41 | /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b. |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 42 | void BuildSIClassTypeInfo(const CXXRecordDecl *RD); |
| 43 | |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 44 | /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for |
| 45 | /// classes with bases that do not satisfy the abi::__si_class_type_info |
| 46 | /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c. |
| 47 | void BuildVMIClassTypeInfo(const CXXRecordDecl *RD); |
| 48 | |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 49 | /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used |
| 50 | /// for pointer types. |
John McCall | e8dc53e | 2010-08-12 02:17:33 +0000 | [diff] [blame] | 51 | void BuildPointerTypeInfo(QualType PointeeTy); |
| 52 | |
| 53 | /// BuildObjCObjectTypeInfo - Build the appropriate kind of |
| 54 | /// type_info for an object type. |
| 55 | void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 56 | |
| 57 | /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info |
| 58 | /// struct, used for member pointer types. |
| 59 | void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty); |
| 60 | |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 61 | public: |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 62 | RTTIBuilder(CodeGenModule &cgm) |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 63 | : CGM(cgm), VMContext(cgm.getModule().getContext()), |
| 64 | Int8PtrTy(llvm::Type::getInt8PtrTy(VMContext)) { } |
| 65 | |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 66 | llvm::Constant *BuildName(QualType Ty, bool Hidden, |
| 67 | llvm::GlobalVariable::LinkageTypes Linkage) { |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 68 | llvm::SmallString<256> OutName; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 69 | CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, OutName); |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 70 | llvm::StringRef Name = OutName.str(); |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 71 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 72 | llvm::GlobalVariable *OGV = CGM.getModule().getNamedGlobal(Name); |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 73 | if (OGV && !OGV->isDeclaration()) |
| 74 | return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy); |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 75 | |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 76 | llvm::Constant *C = llvm::ConstantArray::get(VMContext, Name.substr(4)); |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 77 | |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 78 | llvm::GlobalVariable *GV = |
| 79 | new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage, |
| 80 | C, Name); |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 81 | if (OGV) { |
| 82 | GV->takeName(OGV); |
| 83 | llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV, |
| 84 | OGV->getType()); |
| 85 | OGV->replaceAllUsesWith(NewPtr); |
| 86 | OGV->eraseFromParent(); |
| 87 | } |
Mike Stump | 582b037 | 2009-11-18 03:46:51 +0000 | [diff] [blame] | 88 | if (Hidden) |
| 89 | GV->setVisibility(llvm::GlobalVariable::HiddenVisibility); |
| 90 | return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy); |
Daniel Dunbar | 7177dee | 2009-12-19 17:50:07 +0000 | [diff] [blame] | 91 | } |
Mike Stump | c7a05bd | 2009-11-14 15:55:18 +0000 | [diff] [blame] | 92 | |
Mike Stump | 4e6f8ee | 2009-12-24 02:33:48 +0000 | [diff] [blame] | 93 | // FIXME: unify with DecideExtern |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 94 | bool DecideHidden(QualType Ty) { |
| 95 | // For this type, see if all components are never hidden. |
| 96 | if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) |
| 97 | return (DecideHidden(MPT->getPointeeType()) |
| 98 | && DecideHidden(QualType(MPT->getClass(), 0))); |
| 99 | if (const PointerType *PT = Ty->getAs<PointerType>()) |
| 100 | return DecideHidden(PT->getPointeeType()); |
Mike Stump | 4e6f8ee | 2009-12-24 02:33:48 +0000 | [diff] [blame] | 101 | if (const FunctionType *FT = Ty->getAs<FunctionType>()) { |
| 102 | if (DecideHidden(FT->getResultType()) == false) |
| 103 | return false; |
| 104 | if (const FunctionProtoType *FPT = Ty->getAs<FunctionProtoType>()) { |
| 105 | for (unsigned i = 0; i <FPT->getNumArgs(); ++i) |
| 106 | if (DecideHidden(FPT->getArgType(i)) == false) |
| 107 | return false; |
| 108 | for (unsigned i = 0; i <FPT->getNumExceptions(); ++i) |
| 109 | if (DecideHidden(FPT->getExceptionType(i)) == false) |
| 110 | return false; |
| 111 | return true; |
| 112 | } |
| 113 | } |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 114 | if (const RecordType *RT = Ty->getAs<RecordType>()) |
| 115 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
| 116 | return CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden; |
| 117 | return false; |
| 118 | } |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 119 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 120 | // Pointer type info flags. |
| 121 | enum { |
| 122 | /// PTI_Const - Type has const qualifier. |
| 123 | PTI_Const = 0x1, |
| 124 | |
| 125 | /// PTI_Volatile - Type has volatile qualifier. |
| 126 | PTI_Volatile = 0x2, |
| 127 | |
| 128 | /// PTI_Restrict - Type has restrict qualifier. |
| 129 | PTI_Restrict = 0x4, |
| 130 | |
| 131 | /// PTI_Incomplete - Type is incomplete. |
| 132 | PTI_Incomplete = 0x8, |
| 133 | |
| 134 | /// PTI_ContainingClassIncomplete - Containing class is incomplete. |
| 135 | /// (in pointer to member). |
| 136 | PTI_ContainingClassIncomplete = 0x10 |
| 137 | }; |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 138 | |
| 139 | // VMI type info flags. |
| 140 | enum { |
| 141 | /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance. |
| 142 | VMI_NonDiamondRepeat = 0x1, |
| 143 | |
| 144 | /// VMI_DiamondShaped - Class is diamond shaped. |
| 145 | VMI_DiamondShaped = 0x2 |
| 146 | }; |
| 147 | |
| 148 | // Base class type info flags. |
| 149 | enum { |
| 150 | /// BCTI_Virtual - Base class is virtual. |
| 151 | BCTI_Virtual = 0x1, |
| 152 | |
| 153 | /// BCTI_Public - Base class is public. |
| 154 | BCTI_Public = 0x2 |
| 155 | }; |
Anders Carlsson | 531d55f | 2009-12-31 17:43:53 +0000 | [diff] [blame] | 156 | |
| 157 | /// BuildTypeInfo - Build the RTTI type info struct for the given type. |
John McCall | 9dffe6f | 2010-04-30 01:15:21 +0000 | [diff] [blame] | 158 | /// |
| 159 | /// \param Force - true to force the creation of this RTTI value |
| 160 | /// \param ForEH - true if this is for exception handling |
Rafael Espindola | d1a5c31 | 2010-03-27 02:52:14 +0000 | [diff] [blame] | 161 | llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false); |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 162 | }; |
Mike Stump | 92f2fe2 | 2009-12-02 19:07:44 +0000 | [diff] [blame] | 163 | } |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 164 | |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 165 | llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) { |
| 166 | // Mangle the RTTI name. |
| 167 | llvm::SmallString<256> OutName; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 168 | CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, OutName); |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 169 | llvm::StringRef Name = OutName.str(); |
| 170 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 171 | // Look for an existing global. |
| 172 | llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name); |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 173 | |
| 174 | if (!GV) { |
| 175 | // Create a new global variable. |
| 176 | GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true, |
| 177 | llvm::GlobalValue::ExternalLinkage, 0, Name); |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 180 | return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy); |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 183 | /// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type |
| 184 | /// info for that type is defined in the standard library. |
| 185 | static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) { |
| 186 | // Itanium C++ ABI 2.9.2: |
| 187 | // Basic type information (e.g. for "int", "bool", etc.) will be kept in |
| 188 | // the run-time support library. Specifically, the run-time support |
| 189 | // library should contain type_info objects for the types X, X* and |
| 190 | // X const*, for every X in: void, bool, wchar_t, char, unsigned char, |
| 191 | // signed char, short, unsigned short, int, unsigned int, long, |
| 192 | // unsigned long, long long, unsigned long long, float, double, long double, |
| 193 | // char16_t, char32_t, and the IEEE 754r decimal and half-precision |
| 194 | // floating point types. |
| 195 | switch (Ty->getKind()) { |
| 196 | case BuiltinType::Void: |
| 197 | case BuiltinType::Bool: |
| 198 | case BuiltinType::WChar: |
| 199 | case BuiltinType::Char_U: |
| 200 | case BuiltinType::Char_S: |
| 201 | case BuiltinType::UChar: |
| 202 | case BuiltinType::SChar: |
| 203 | case BuiltinType::Short: |
| 204 | case BuiltinType::UShort: |
| 205 | case BuiltinType::Int: |
| 206 | case BuiltinType::UInt: |
| 207 | case BuiltinType::Long: |
| 208 | case BuiltinType::ULong: |
| 209 | case BuiltinType::LongLong: |
| 210 | case BuiltinType::ULongLong: |
| 211 | case BuiltinType::Float: |
| 212 | case BuiltinType::Double: |
| 213 | case BuiltinType::LongDouble: |
| 214 | case BuiltinType::Char16: |
| 215 | case BuiltinType::Char32: |
| 216 | case BuiltinType::Int128: |
| 217 | case BuiltinType::UInt128: |
| 218 | return true; |
| 219 | |
| 220 | case BuiltinType::Overload: |
| 221 | case BuiltinType::Dependent: |
| 222 | case BuiltinType::UndeducedAuto: |
| 223 | assert(false && "Should not see this type here!"); |
| 224 | |
| 225 | case BuiltinType::NullPtr: |
| 226 | assert(false && "FIXME: nullptr_t is not handled!"); |
| 227 | |
| 228 | case BuiltinType::ObjCId: |
| 229 | case BuiltinType::ObjCClass: |
| 230 | case BuiltinType::ObjCSel: |
| 231 | assert(false && "FIXME: Objective-C types are unsupported!"); |
| 232 | } |
| 233 | |
| 234 | // Silent gcc. |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) { |
| 239 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 240 | const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy); |
| 241 | if (!BuiltinTy) |
| 242 | return false; |
| 243 | |
| 244 | // Check the qualifiers. |
| 245 | Qualifiers Quals = PointeeTy.getQualifiers(); |
| 246 | Quals.removeConst(); |
| 247 | |
| 248 | if (!Quals.empty()) |
| 249 | return false; |
| 250 | |
| 251 | return TypeInfoIsInStandardLibrary(BuiltinTy); |
| 252 | } |
| 253 | |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 254 | /// IsStandardLibraryRTTIDescriptor - Returns whether the type |
| 255 | /// information for the given type exists in the standard library. |
| 256 | static bool IsStandardLibraryRTTIDescriptor(QualType Ty) { |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 257 | // Type info for builtin types is defined in the standard library. |
| 258 | if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty)) |
| 259 | return TypeInfoIsInStandardLibrary(BuiltinTy); |
| 260 | |
| 261 | // Type info for some pointer types to builtin types is defined in the |
| 262 | // standard library. |
| 263 | if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) |
| 264 | return TypeInfoIsInStandardLibrary(PointerTy); |
| 265 | |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 266 | return false; |
| 267 | } |
| 268 | |
| 269 | /// ShouldUseExternalRTTIDescriptor - Returns whether the type information for |
| 270 | /// the given type exists somewhere else, and that we should not emit the type |
| 271 | /// information in this translation unit. Assumes that it is not a |
| 272 | /// standard-library type. |
| 273 | static bool ShouldUseExternalRTTIDescriptor(ASTContext &Context, |
| 274 | QualType Ty) { |
John McCall | 9dffe6f | 2010-04-30 01:15:21 +0000 | [diff] [blame] | 275 | // If RTTI is disabled, don't consider key functions. |
| 276 | if (!Context.getLangOptions().RTTI) return false; |
| 277 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 278 | if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 279 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 280 | if (!RD->hasDefinition()) |
| 281 | return false; |
| 282 | |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 283 | if (!RD->isDynamicClass()) |
| 284 | return false; |
| 285 | |
| 286 | // Get the key function. |
John McCall | 26fc28d | 2010-09-04 01:26:37 +0000 | [diff] [blame^] | 287 | const CXXMethodDecl *KeyFunction = Context.getKeyFunction(RD); |
Argyrios Kyrtzidis | 4aedb1c | 2010-07-07 12:24:18 +0000 | [diff] [blame] | 288 | if (KeyFunction && !KeyFunction->hasBody()) { |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 289 | // The class has a key function, but it is not defined in this translation |
| 290 | // unit, so we should use the external descriptor for it. |
| 291 | return true; |
| 292 | } |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | /// IsIncompleteClassType - Returns whether the given record type is incomplete. |
| 299 | static bool IsIncompleteClassType(const RecordType *RecordTy) { |
| 300 | return !RecordTy->getDecl()->isDefinition(); |
| 301 | } |
| 302 | |
Anders Carlsson | 17fa6f9 | 2009-12-20 23:37:55 +0000 | [diff] [blame] | 303 | /// ContainsIncompleteClassType - Returns whether the given type contains an |
| 304 | /// incomplete class type. This is true if |
| 305 | /// |
| 306 | /// * The given type is an incomplete class type. |
| 307 | /// * The given type is a pointer type whose pointee type contains an |
| 308 | /// incomplete class type. |
| 309 | /// * The given type is a member pointer type whose class is an incomplete |
| 310 | /// class type. |
| 311 | /// * The given type is a member pointer type whoise pointee type contains an |
| 312 | /// incomplete class type. |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 313 | /// is an indirect or direct pointer to an incomplete class type. |
Anders Carlsson | 17fa6f9 | 2009-12-20 23:37:55 +0000 | [diff] [blame] | 314 | static bool ContainsIncompleteClassType(QualType Ty) { |
| 315 | if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { |
| 316 | if (IsIncompleteClassType(RecordTy)) |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) |
| 321 | return ContainsIncompleteClassType(PointerTy->getPointeeType()); |
| 322 | |
| 323 | if (const MemberPointerType *MemberPointerTy = |
| 324 | dyn_cast<MemberPointerType>(Ty)) { |
| 325 | // Check if the class type is incomplete. |
| 326 | const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass()); |
| 327 | if (IsIncompleteClassType(ClassType)) |
| 328 | return true; |
| 329 | |
| 330 | return ContainsIncompleteClassType(MemberPointerTy->getPointeeType()); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | /// getTypeInfoLinkage - Return the linkage that the type info and type info |
| 337 | /// name constants should have for the given type. |
| 338 | static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(QualType Ty) { |
Anders Carlsson | 17fa6f9 | 2009-12-20 23:37:55 +0000 | [diff] [blame] | 339 | // Itanium C++ ABI 2.9.5p7: |
| 340 | // In addition, it and all of the intermediate abi::__pointer_type_info |
| 341 | // structs in the chain down to the abi::__class_type_info for the |
| 342 | // incomplete class type must be prevented from resolving to the |
| 343 | // corresponding type_info structs for the complete class type, possibly |
| 344 | // by making them local static objects. Finally, a dummy class RTTI is |
| 345 | // generated for the incomplete type that will not resolve to the final |
| 346 | // complete class RTTI (because the latter need not exist), possibly by |
| 347 | // making it a local static object. |
| 348 | if (ContainsIncompleteClassType(Ty)) |
| 349 | return llvm::GlobalValue::InternalLinkage; |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 350 | |
Douglas Gregor | 031b371 | 2010-03-31 00:15:35 +0000 | [diff] [blame] | 351 | switch (Ty->getLinkage()) { |
| 352 | case NoLinkage: |
| 353 | case InternalLinkage: |
| 354 | case UniqueExternalLinkage: |
| 355 | return llvm::GlobalValue::InternalLinkage; |
Anders Carlsson | 978ef68 | 2009-12-29 21:58:32 +0000 | [diff] [blame] | 356 | |
Douglas Gregor | 031b371 | 2010-03-31 00:15:35 +0000 | [diff] [blame] | 357 | case ExternalLinkage: |
| 358 | if (const RecordType *Record = dyn_cast<RecordType>(Ty)) { |
| 359 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); |
| 360 | if (RD->isDynamicClass()) |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 361 | return CodeGenModule::getVTableLinkage(RD); |
Mike Stump | c8f76f5 | 2009-12-24 01:10:27 +0000 | [diff] [blame] | 362 | } |
Douglas Gregor | 031b371 | 2010-03-31 00:15:35 +0000 | [diff] [blame] | 363 | |
Mike Stump | c8f76f5 | 2009-12-24 01:10:27 +0000 | [diff] [blame] | 364 | return llvm::GlobalValue::WeakODRLinkage; |
| 365 | } |
Anders Carlsson | 978ef68 | 2009-12-29 21:58:32 +0000 | [diff] [blame] | 366 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 367 | return llvm::GlobalValue::WeakODRLinkage; |
| 368 | } |
| 369 | |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 370 | // CanUseSingleInheritance - Return whether the given record decl has a "single, |
| 371 | // public, non-virtual base at offset zero (i.e. the derived class is dynamic |
| 372 | // iff the base is)", according to Itanium C++ ABI, 2.95p6b. |
| 373 | static bool CanUseSingleInheritance(const CXXRecordDecl *RD) { |
| 374 | // Check the number of bases. |
| 375 | if (RD->getNumBases() != 1) |
| 376 | return false; |
| 377 | |
| 378 | // Get the base. |
| 379 | CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin(); |
| 380 | |
| 381 | // Check that the base is not virtual. |
| 382 | if (Base->isVirtual()) |
| 383 | return false; |
| 384 | |
| 385 | // Check that the base is public. |
| 386 | if (Base->getAccessSpecifier() != AS_public) |
| 387 | return false; |
| 388 | |
| 389 | // Check that the class is dynamic iff the base is. |
| 390 | const CXXRecordDecl *BaseDecl = |
| 391 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 392 | if (!BaseDecl->isEmpty() && |
| 393 | BaseDecl->isDynamicClass() != RD->isDynamicClass()) |
| 394 | return false; |
| 395 | |
| 396 | return true; |
| 397 | } |
| 398 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 399 | void RTTIBuilder::BuildVTablePointer(const Type *Ty) { |
John McCall | e8dc53e | 2010-08-12 02:17:33 +0000 | [diff] [blame] | 400 | // abi::__class_type_info. |
| 401 | static const char * const ClassTypeInfo = |
| 402 | "_ZTVN10__cxxabiv117__class_type_infoE"; |
| 403 | // abi::__si_class_type_info. |
| 404 | static const char * const SIClassTypeInfo = |
| 405 | "_ZTVN10__cxxabiv120__si_class_type_infoE"; |
| 406 | // abi::__vmi_class_type_info. |
| 407 | static const char * const VMIClassTypeInfo = |
| 408 | "_ZTVN10__cxxabiv121__vmi_class_type_infoE"; |
| 409 | |
Eli Friedman | 1cf26f5 | 2010-08-11 20:41:51 +0000 | [diff] [blame] | 410 | const char *VTableName = 0; |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 411 | |
| 412 | switch (Ty->getTypeClass()) { |
Eli Friedman | 1cf26f5 | 2010-08-11 20:41:51 +0000 | [diff] [blame] | 413 | #define TYPE(Class, Base) |
| 414 | #define ABSTRACT_TYPE(Class, Base) |
| 415 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 416 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 417 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 418 | #include "clang/AST/TypeNodes.def" |
| 419 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
| 420 | |
| 421 | case Type::LValueReference: |
| 422 | case Type::RValueReference: |
| 423 | assert(false && "References shouldn't get here"); |
Anders Carlsson | 978ef68 | 2009-12-29 21:58:32 +0000 | [diff] [blame] | 424 | |
Rafael Espindola | d1a5c31 | 2010-03-27 02:52:14 +0000 | [diff] [blame] | 425 | case Type::Builtin: |
Eli Friedman | 1cf26f5 | 2010-08-11 20:41:51 +0000 | [diff] [blame] | 426 | // GCC treats vector and complex types as fundamental types. |
Anders Carlsson | c8cfd63 | 2009-12-29 22:30:11 +0000 | [diff] [blame] | 427 | case Type::Vector: |
| 428 | case Type::ExtVector: |
Eli Friedman | 1cf26f5 | 2010-08-11 20:41:51 +0000 | [diff] [blame] | 429 | case Type::Complex: |
| 430 | // FIXME: GCC treats block pointers as fundamental types?! |
| 431 | case Type::BlockPointer: |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 432 | // abi::__fundamental_type_info. |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 433 | VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE"; |
Anders Carlsson | c8cfd63 | 2009-12-29 22:30:11 +0000 | [diff] [blame] | 434 | break; |
| 435 | |
Anders Carlsson | 978ef68 | 2009-12-29 21:58:32 +0000 | [diff] [blame] | 436 | case Type::ConstantArray: |
| 437 | case Type::IncompleteArray: |
Eli Friedman | 1cf26f5 | 2010-08-11 20:41:51 +0000 | [diff] [blame] | 438 | case Type::VariableArray: |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 439 | // abi::__array_type_info. |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 440 | VTableName = "_ZTVN10__cxxabiv117__array_type_infoE"; |
Anders Carlsson | 978ef68 | 2009-12-29 21:58:32 +0000 | [diff] [blame] | 441 | break; |
| 442 | |
| 443 | case Type::FunctionNoProto: |
| 444 | case Type::FunctionProto: |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 445 | // abi::__function_type_info. |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 446 | VTableName = "_ZTVN10__cxxabiv120__function_type_infoE"; |
Anders Carlsson | 978ef68 | 2009-12-29 21:58:32 +0000 | [diff] [blame] | 447 | break; |
| 448 | |
Anders Carlsson | 9c7b6bb | 2009-12-29 22:13:01 +0000 | [diff] [blame] | 449 | case Type::Enum: |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 450 | // abi::__enum_type_info. |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 451 | VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE"; |
Anders Carlsson | 9c7b6bb | 2009-12-29 22:13:01 +0000 | [diff] [blame] | 452 | break; |
John McCall | e8dc53e | 2010-08-12 02:17:33 +0000 | [diff] [blame] | 453 | |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 454 | case Type::Record: { |
| 455 | const CXXRecordDecl *RD = |
| 456 | cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl()); |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 457 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 458 | if (!RD->hasDefinition() || !RD->getNumBases()) { |
John McCall | e8dc53e | 2010-08-12 02:17:33 +0000 | [diff] [blame] | 459 | VTableName = ClassTypeInfo; |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 460 | } else if (CanUseSingleInheritance(RD)) { |
John McCall | e8dc53e | 2010-08-12 02:17:33 +0000 | [diff] [blame] | 461 | VTableName = SIClassTypeInfo; |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 462 | } else { |
John McCall | e8dc53e | 2010-08-12 02:17:33 +0000 | [diff] [blame] | 463 | VTableName = VMIClassTypeInfo; |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 464 | } |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 465 | |
| 466 | break; |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Eli Friedman | 1cf26f5 | 2010-08-11 20:41:51 +0000 | [diff] [blame] | 469 | case Type::ObjCObject: |
John McCall | e8dc53e | 2010-08-12 02:17:33 +0000 | [diff] [blame] | 470 | // Ignore protocol qualifiers. |
| 471 | Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr(); |
| 472 | |
| 473 | // Handle id and Class. |
| 474 | if (isa<BuiltinType>(Ty)) { |
| 475 | VTableName = ClassTypeInfo; |
| 476 | break; |
| 477 | } |
| 478 | |
| 479 | assert(isa<ObjCInterfaceType>(Ty)); |
| 480 | // Fall through. |
| 481 | |
Eli Friedman | 1cf26f5 | 2010-08-11 20:41:51 +0000 | [diff] [blame] | 482 | case Type::ObjCInterface: |
John McCall | e8dc53e | 2010-08-12 02:17:33 +0000 | [diff] [blame] | 483 | if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) { |
| 484 | VTableName = SIClassTypeInfo; |
| 485 | } else { |
| 486 | VTableName = ClassTypeInfo; |
| 487 | } |
Eli Friedman | 1cf26f5 | 2010-08-11 20:41:51 +0000 | [diff] [blame] | 488 | break; |
| 489 | |
John McCall | e8dc53e | 2010-08-12 02:17:33 +0000 | [diff] [blame] | 490 | case Type::ObjCObjectPointer: |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 491 | case Type::Pointer: |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 492 | // abi::__pointer_type_info. |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 493 | VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE"; |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 494 | break; |
Anders Carlsson | 978ef68 | 2009-12-29 21:58:32 +0000 | [diff] [blame] | 495 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 496 | case Type::MemberPointer: |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 497 | // abi::__pointer_to_member_type_info. |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 498 | VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE"; |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 499 | break; |
| 500 | } |
| 501 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 502 | llvm::Constant *VTable = |
| 503 | CGM.getModule().getOrInsertGlobal(VTableName, Int8PtrTy); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 504 | |
| 505 | const llvm::Type *PtrDiffTy = |
| 506 | CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); |
| 507 | |
| 508 | // The vtable address point is 2. |
| 509 | llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2); |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 510 | VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, &Two, 1); |
| 511 | VTable = llvm::ConstantExpr::getBitCast(VTable, Int8PtrTy); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 512 | |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 513 | Fields.push_back(VTable); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 514 | } |
| 515 | |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 516 | llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) { |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 517 | // We want to operate on the canonical type. |
| 518 | Ty = CGM.getContext().getCanonicalType(Ty); |
| 519 | |
| 520 | // Check if we've already emitted an RTTI descriptor for this type. |
| 521 | llvm::SmallString<256> OutName; |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 522 | CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, OutName); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 523 | llvm::StringRef Name = OutName.str(); |
| 524 | |
| 525 | llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name); |
| 526 | if (OldGV && !OldGV->isDeclaration()) |
| 527 | return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy); |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 528 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 529 | // Check if there is already an external RTTI descriptor for this type. |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 530 | bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty); |
| 531 | if (!Force && |
| 532 | (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM.getContext(), Ty))) |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 533 | return GetAddrOfExternalRTTIDescriptor(Ty); |
| 534 | |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 535 | // Emit the standard library with external linkage. |
| 536 | llvm::GlobalVariable::LinkageTypes Linkage; |
| 537 | if (IsStdLib) |
| 538 | Linkage = llvm::GlobalValue::ExternalLinkage; |
| 539 | else |
| 540 | Linkage = getTypeInfoLinkage(Ty); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 541 | |
| 542 | // Add the vtable pointer. |
Anders Carlsson | 046c294 | 2010-04-17 20:15:18 +0000 | [diff] [blame] | 543 | BuildVTablePointer(cast<Type>(Ty)); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 544 | |
| 545 | // And the name. |
Anders Carlsson | 531d55f | 2009-12-31 17:43:53 +0000 | [diff] [blame] | 546 | Fields.push_back(BuildName(Ty, DecideHidden(Ty), Linkage)); |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 547 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 548 | switch (Ty->getTypeClass()) { |
Eli Friedman | f2aabe1 | 2010-08-15 00:24:31 +0000 | [diff] [blame] | 549 | #define TYPE(Class, Base) |
| 550 | #define ABSTRACT_TYPE(Class, Base) |
| 551 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 552 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 553 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: |
| 554 | #include "clang/AST/TypeNodes.def" |
| 555 | assert(false && "Non-canonical and dependent types shouldn't get here"); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 556 | |
Anders Carlsson | c8cfd63 | 2009-12-29 22:30:11 +0000 | [diff] [blame] | 557 | // GCC treats vector types as fundamental types. |
Rafael Espindola | d1a5c31 | 2010-03-27 02:52:14 +0000 | [diff] [blame] | 558 | case Type::Builtin: |
Anders Carlsson | c8cfd63 | 2009-12-29 22:30:11 +0000 | [diff] [blame] | 559 | case Type::Vector: |
| 560 | case Type::ExtVector: |
Eli Friedman | f2aabe1 | 2010-08-15 00:24:31 +0000 | [diff] [blame] | 561 | case Type::Complex: |
| 562 | case Type::BlockPointer: |
Anders Carlsson | c8cfd63 | 2009-12-29 22:30:11 +0000 | [diff] [blame] | 563 | // Itanium C++ ABI 2.9.5p4: |
| 564 | // abi::__fundamental_type_info adds no data members to std::type_info. |
| 565 | break; |
Eli Friedman | f2aabe1 | 2010-08-15 00:24:31 +0000 | [diff] [blame] | 566 | |
| 567 | case Type::LValueReference: |
| 568 | case Type::RValueReference: |
| 569 | assert(false && "References shouldn't get here"); |
| 570 | |
Anders Carlsson | 978ef68 | 2009-12-29 21:58:32 +0000 | [diff] [blame] | 571 | case Type::ConstantArray: |
| 572 | case Type::IncompleteArray: |
Eli Friedman | f2aabe1 | 2010-08-15 00:24:31 +0000 | [diff] [blame] | 573 | case Type::VariableArray: |
Anders Carlsson | c8cfd63 | 2009-12-29 22:30:11 +0000 | [diff] [blame] | 574 | // Itanium C++ ABI 2.9.5p5: |
| 575 | // abi::__array_type_info adds no data members to std::type_info. |
Anders Carlsson | 978ef68 | 2009-12-29 21:58:32 +0000 | [diff] [blame] | 576 | break; |
| 577 | |
Anders Carlsson | 09b6e6e | 2009-12-29 20:20:19 +0000 | [diff] [blame] | 578 | case Type::FunctionNoProto: |
| 579 | case Type::FunctionProto: |
Anders Carlsson | c8cfd63 | 2009-12-29 22:30:11 +0000 | [diff] [blame] | 580 | // Itanium C++ ABI 2.9.5p5: |
| 581 | // abi::__function_type_info adds no data members to std::type_info. |
Anders Carlsson | 09b6e6e | 2009-12-29 20:20:19 +0000 | [diff] [blame] | 582 | break; |
| 583 | |
Anders Carlsson | 9c7b6bb | 2009-12-29 22:13:01 +0000 | [diff] [blame] | 584 | case Type::Enum: |
Anders Carlsson | c8cfd63 | 2009-12-29 22:30:11 +0000 | [diff] [blame] | 585 | // Itanium C++ ABI 2.9.5p5: |
| 586 | // abi::__enum_type_info adds no data members to std::type_info. |
Anders Carlsson | 9c7b6bb | 2009-12-29 22:13:01 +0000 | [diff] [blame] | 587 | break; |
| 588 | |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 589 | case Type::Record: { |
| 590 | const CXXRecordDecl *RD = |
| 591 | cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl()); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 592 | if (!RD->hasDefinition() || !RD->getNumBases()) { |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 593 | // We don't need to emit any fields. |
| 594 | break; |
| 595 | } |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 596 | |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 597 | if (CanUseSingleInheritance(RD)) |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 598 | BuildSIClassTypeInfo(RD); |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 599 | else |
| 600 | BuildVMIClassTypeInfo(RD); |
| 601 | |
| 602 | break; |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 603 | } |
John McCall | e8dc53e | 2010-08-12 02:17:33 +0000 | [diff] [blame] | 604 | |
| 605 | case Type::ObjCObject: |
| 606 | case Type::ObjCInterface: |
| 607 | BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty)); |
| 608 | break; |
| 609 | |
| 610 | case Type::ObjCObjectPointer: |
| 611 | BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType()); |
| 612 | break; |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 613 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 614 | case Type::Pointer: |
John McCall | e8dc53e | 2010-08-12 02:17:33 +0000 | [diff] [blame] | 615 | BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType()); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 616 | break; |
John McCall | e8dc53e | 2010-08-12 02:17:33 +0000 | [diff] [blame] | 617 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 618 | case Type::MemberPointer: |
| 619 | BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty)); |
| 620 | break; |
| 621 | } |
| 622 | |
| 623 | llvm::Constant *Init = |
Anders Carlsson | 531d55f | 2009-12-31 17:43:53 +0000 | [diff] [blame] | 624 | llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(), |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 625 | /*Packed=*/false); |
| 626 | |
| 627 | llvm::GlobalVariable *GV = |
| 628 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), |
| 629 | /*Constant=*/true, Linkage, Init, Name); |
| 630 | |
| 631 | // If there's already an old global variable, replace it with the new one. |
| 632 | if (OldGV) { |
| 633 | GV->takeName(OldGV); |
| 634 | llvm::Constant *NewPtr = |
| 635 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 636 | OldGV->replaceAllUsesWith(NewPtr); |
| 637 | OldGV->eraseFromParent(); |
| 638 | } |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 639 | |
| 640 | // GCC only relies on the uniqueness of the type names, not the |
| 641 | // type_infos themselves, so we can emit these as hidden symbols. |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame] | 642 | // But don't do this if we're worried about strict visibility |
| 643 | // compatibility. |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 644 | if (const RecordType *RT = dyn_cast<RecordType>(Ty)) |
| 645 | CGM.setTypeVisibility(GV, cast<CXXRecordDecl>(RT->getDecl()), |
| 646 | /*ForRTTI*/ true); |
John McCall | 279b5eb | 2010-08-12 23:36:15 +0000 | [diff] [blame] | 647 | else if (CGM.getCodeGenOpts().HiddenWeakVTables && |
| 648 | Linkage == llvm::GlobalValue::WeakODRLinkage) |
John McCall | cbfe502 | 2010-08-04 08:34:44 +0000 | [diff] [blame] | 649 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 650 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 651 | return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy); |
| 652 | } |
| 653 | |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 654 | /// ComputeQualifierFlags - Compute the pointer type info flags from the |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 655 | /// given qualifier. |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 656 | static unsigned ComputeQualifierFlags(Qualifiers Quals) { |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 657 | unsigned Flags = 0; |
| 658 | |
| 659 | if (Quals.hasConst()) |
| 660 | Flags |= RTTIBuilder::PTI_Const; |
| 661 | if (Quals.hasVolatile()) |
| 662 | Flags |= RTTIBuilder::PTI_Volatile; |
| 663 | if (Quals.hasRestrict()) |
| 664 | Flags |= RTTIBuilder::PTI_Restrict; |
| 665 | |
| 666 | return Flags; |
| 667 | } |
| 668 | |
John McCall | e8dc53e | 2010-08-12 02:17:33 +0000 | [diff] [blame] | 669 | /// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info |
| 670 | /// for the given Objective-C object type. |
| 671 | void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) { |
| 672 | // Drop qualifiers. |
| 673 | const Type *T = OT->getBaseType().getTypePtr(); |
| 674 | assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T)); |
| 675 | |
| 676 | // The builtin types are abi::__class_type_infos and don't require |
| 677 | // extra fields. |
| 678 | if (isa<BuiltinType>(T)) return; |
| 679 | |
| 680 | ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl(); |
| 681 | ObjCInterfaceDecl *Super = Class->getSuperClass(); |
| 682 | |
| 683 | // Root classes are also __class_type_info. |
| 684 | if (!Super) return; |
| 685 | |
| 686 | QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super); |
| 687 | |
| 688 | // Everything else is single inheritance. |
| 689 | llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy); |
| 690 | Fields.push_back(BaseTypeInfo); |
| 691 | } |
| 692 | |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 693 | /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single |
| 694 | /// inheritance, according to the Itanium C++ ABI, 2.95p6b. |
| 695 | void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) { |
| 696 | // Itanium C++ ABI 2.9.5p6b: |
| 697 | // It adds to abi::__class_type_info a single member pointing to the |
| 698 | // type_info structure for the base type, |
Anders Carlsson | 531d55f | 2009-12-31 17:43:53 +0000 | [diff] [blame] | 699 | llvm::Constant *BaseTypeInfo = |
| 700 | RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType()); |
| 701 | Fields.push_back(BaseTypeInfo); |
Anders Carlsson | f64531a | 2009-12-30 01:00:12 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 704 | /// SeenBases - Contains virtual and non-virtual bases seen when traversing |
| 705 | /// a class hierarchy. |
| 706 | struct SeenBases { |
| 707 | llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases; |
| 708 | llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases; |
| 709 | }; |
| 710 | |
| 711 | /// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in |
| 712 | /// abi::__vmi_class_type_info. |
| 713 | /// |
| 714 | static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base, |
| 715 | SeenBases &Bases) { |
| 716 | |
| 717 | unsigned Flags = 0; |
| 718 | |
| 719 | const CXXRecordDecl *BaseDecl = |
| 720 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 721 | |
| 722 | if (Base->isVirtual()) { |
| 723 | if (Bases.VirtualBases.count(BaseDecl)) { |
| 724 | // If this virtual base has been seen before, then the class is diamond |
| 725 | // shaped. |
| 726 | Flags |= RTTIBuilder::VMI_DiamondShaped; |
| 727 | } else { |
| 728 | if (Bases.NonVirtualBases.count(BaseDecl)) |
| 729 | Flags |= RTTIBuilder::VMI_NonDiamondRepeat; |
| 730 | |
| 731 | // Mark the virtual base as seen. |
| 732 | Bases.VirtualBases.insert(BaseDecl); |
| 733 | } |
| 734 | } else { |
| 735 | if (Bases.NonVirtualBases.count(BaseDecl)) { |
| 736 | // If this non-virtual base has been seen before, then the class has non- |
| 737 | // diamond shaped repeated inheritance. |
| 738 | Flags |= RTTIBuilder::VMI_NonDiamondRepeat; |
| 739 | } else { |
| 740 | if (Bases.VirtualBases.count(BaseDecl)) |
| 741 | Flags |= RTTIBuilder::VMI_NonDiamondRepeat; |
| 742 | |
| 743 | // Mark the non-virtual base as seen. |
| 744 | Bases.NonVirtualBases.insert(BaseDecl); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | // Walk all bases. |
| 749 | for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(), |
| 750 | E = BaseDecl->bases_end(); I != E; ++I) |
| 751 | Flags |= ComputeVMIClassTypeInfoFlags(I, Bases); |
| 752 | |
| 753 | return Flags; |
| 754 | } |
| 755 | |
| 756 | static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) { |
| 757 | unsigned Flags = 0; |
| 758 | SeenBases Bases; |
| 759 | |
| 760 | // Walk all bases. |
| 761 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 762 | E = RD->bases_end(); I != E; ++I) |
| 763 | Flags |= ComputeVMIClassTypeInfoFlags(I, Bases); |
| 764 | |
| 765 | return Flags; |
| 766 | } |
| 767 | |
| 768 | /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for |
| 769 | /// classes with bases that do not satisfy the abi::__si_class_type_info |
| 770 | /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c. |
| 771 | void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) { |
| 772 | const llvm::Type *UnsignedIntLTy = |
| 773 | CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); |
| 774 | |
| 775 | // Itanium C++ ABI 2.9.5p6c: |
| 776 | // __flags is a word with flags describing details about the class |
| 777 | // structure, which may be referenced by using the __flags_masks |
| 778 | // enumeration. These flags refer to both direct and indirect bases. |
| 779 | unsigned Flags = ComputeVMIClassTypeInfoFlags(RD); |
Anders Carlsson | 531d55f | 2009-12-31 17:43:53 +0000 | [diff] [blame] | 780 | Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 781 | |
| 782 | // Itanium C++ ABI 2.9.5p6c: |
| 783 | // __base_count is a word with the number of direct proper base class |
| 784 | // descriptions that follow. |
Anders Carlsson | 531d55f | 2009-12-31 17:43:53 +0000 | [diff] [blame] | 785 | Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases())); |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 786 | |
| 787 | if (!RD->getNumBases()) |
| 788 | return; |
| 789 | |
| 790 | const llvm::Type *LongLTy = |
| 791 | CGM.getTypes().ConvertType(CGM.getContext().LongTy); |
| 792 | |
| 793 | // Now add the base class descriptions. |
| 794 | |
| 795 | // Itanium C++ ABI 2.9.5p6c: |
| 796 | // __base_info[] is an array of base class descriptions -- one for every |
| 797 | // direct proper base. Each description is of the type: |
| 798 | // |
| 799 | // struct abi::__base_class_type_info { |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 800 | // public: |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 801 | // const __class_type_info *__base_type; |
| 802 | // long __offset_flags; |
| 803 | // |
| 804 | // enum __offset_flags_masks { |
| 805 | // __virtual_mask = 0x1, |
| 806 | // __public_mask = 0x2, |
| 807 | // __offset_shift = 8 |
| 808 | // }; |
| 809 | // }; |
| 810 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 811 | E = RD->bases_end(); I != E; ++I) { |
| 812 | const CXXBaseSpecifier *Base = I; |
| 813 | |
| 814 | // The __base_type member points to the RTTI for the base type. |
Anders Carlsson | 531d55f | 2009-12-31 17:43:53 +0000 | [diff] [blame] | 815 | Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType())); |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 816 | |
| 817 | const CXXRecordDecl *BaseDecl = |
| 818 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 819 | |
| 820 | int64_t OffsetFlags = 0; |
| 821 | |
| 822 | // All but the lower 8 bits of __offset_flags are a signed offset. |
| 823 | // For a non-virtual base, this is the offset in the object of the base |
| 824 | // subobject. For a virtual base, this is the offset in the virtual table of |
| 825 | // the virtual base offset for the virtual base referenced (negative). |
| 826 | if (Base->isVirtual()) |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 827 | OffsetFlags = CGM.getVTables().getVirtualBaseOffsetOffset(RD, BaseDecl); |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 828 | else { |
| 829 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 830 | OffsetFlags = Layout.getBaseClassOffset(BaseDecl) / 8; |
| 831 | }; |
| 832 | |
| 833 | OffsetFlags <<= 8; |
| 834 | |
| 835 | // The low-order byte of __offset_flags contains flags, as given by the |
| 836 | // masks from the enumeration __offset_flags_masks. |
| 837 | if (Base->isVirtual()) |
| 838 | OffsetFlags |= BCTI_Virtual; |
| 839 | if (Base->getAccessSpecifier() == AS_public) |
| 840 | OffsetFlags |= BCTI_Public; |
| 841 | |
Anders Carlsson | 531d55f | 2009-12-31 17:43:53 +0000 | [diff] [blame] | 842 | Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags)); |
Anders Carlsson | 0814809 | 2009-12-30 23:47:56 +0000 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 846 | /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, |
| 847 | /// used for pointer types. |
John McCall | e8dc53e | 2010-08-12 02:17:33 +0000 | [diff] [blame] | 848 | void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) { |
Anders Carlsson | abd6b09 | 2010-06-02 15:44:35 +0000 | [diff] [blame] | 849 | Qualifiers Quals; |
| 850 | QualType UnqualifiedPointeeTy = |
| 851 | CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals); |
| 852 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 853 | // Itanium C++ ABI 2.9.5p7: |
| 854 | // __flags is a flag word describing the cv-qualification and other |
| 855 | // attributes of the type pointed to |
Anders Carlsson | abd6b09 | 2010-06-02 15:44:35 +0000 | [diff] [blame] | 856 | unsigned Flags = ComputeQualifierFlags(Quals); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 857 | |
| 858 | // Itanium C++ ABI 2.9.5p7: |
| 859 | // When the abi::__pbase_type_info is for a direct or indirect pointer to an |
| 860 | // incomplete class type, the incomplete target type flag is set. |
Anders Carlsson | abd6b09 | 2010-06-02 15:44:35 +0000 | [diff] [blame] | 861 | if (ContainsIncompleteClassType(UnqualifiedPointeeTy)) |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 862 | Flags |= PTI_Incomplete; |
| 863 | |
| 864 | const llvm::Type *UnsignedIntLTy = |
| 865 | CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); |
Anders Carlsson | 531d55f | 2009-12-31 17:43:53 +0000 | [diff] [blame] | 866 | Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 867 | |
| 868 | // Itanium C++ ABI 2.9.5p7: |
| 869 | // __pointee is a pointer to the std::type_info derivation for the |
| 870 | // unqualified type being pointed to. |
Anders Carlsson | 531d55f | 2009-12-31 17:43:53 +0000 | [diff] [blame] | 871 | llvm::Constant *PointeeTypeInfo = |
Anders Carlsson | abd6b09 | 2010-06-02 15:44:35 +0000 | [diff] [blame] | 872 | RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy); |
Anders Carlsson | 531d55f | 2009-12-31 17:43:53 +0000 | [diff] [blame] | 873 | Fields.push_back(PointeeTypeInfo); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info |
| 877 | /// struct, used for member pointer types. |
| 878 | void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) { |
| 879 | QualType PointeeTy = Ty->getPointeeType(); |
| 880 | |
Anders Carlsson | abd6b09 | 2010-06-02 15:44:35 +0000 | [diff] [blame] | 881 | Qualifiers Quals; |
| 882 | QualType UnqualifiedPointeeTy = |
| 883 | CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals); |
| 884 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 885 | // Itanium C++ ABI 2.9.5p7: |
| 886 | // __flags is a flag word describing the cv-qualification and other |
| 887 | // attributes of the type pointed to. |
Anders Carlsson | abd6b09 | 2010-06-02 15:44:35 +0000 | [diff] [blame] | 888 | unsigned Flags = ComputeQualifierFlags(Quals); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 889 | |
| 890 | const RecordType *ClassType = cast<RecordType>(Ty->getClass()); |
Anders Carlsson | 17fa6f9 | 2009-12-20 23:37:55 +0000 | [diff] [blame] | 891 | |
| 892 | // Itanium C++ ABI 2.9.5p7: |
| 893 | // When the abi::__pbase_type_info is for a direct or indirect pointer to an |
| 894 | // incomplete class type, the incomplete target type flag is set. |
Anders Carlsson | abd6b09 | 2010-06-02 15:44:35 +0000 | [diff] [blame] | 895 | if (ContainsIncompleteClassType(UnqualifiedPointeeTy)) |
Anders Carlsson | 17fa6f9 | 2009-12-20 23:37:55 +0000 | [diff] [blame] | 896 | Flags |= PTI_Incomplete; |
| 897 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 898 | if (IsIncompleteClassType(ClassType)) |
| 899 | Flags |= PTI_ContainingClassIncomplete; |
| 900 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 901 | const llvm::Type *UnsignedIntLTy = |
| 902 | CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); |
Anders Carlsson | 531d55f | 2009-12-31 17:43:53 +0000 | [diff] [blame] | 903 | Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 904 | |
| 905 | // Itanium C++ ABI 2.9.5p7: |
| 906 | // __pointee is a pointer to the std::type_info derivation for the |
| 907 | // unqualified type being pointed to. |
Anders Carlsson | 531d55f | 2009-12-31 17:43:53 +0000 | [diff] [blame] | 908 | llvm::Constant *PointeeTypeInfo = |
Anders Carlsson | abd6b09 | 2010-06-02 15:44:35 +0000 | [diff] [blame] | 909 | RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy); |
Anders Carlsson | 531d55f | 2009-12-31 17:43:53 +0000 | [diff] [blame] | 910 | Fields.push_back(PointeeTypeInfo); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 911 | |
| 912 | // Itanium C++ ABI 2.9.5p9: |
| 913 | // __context is a pointer to an abi::__class_type_info corresponding to the |
| 914 | // class type containing the member pointed to |
| 915 | // (e.g., the "A" in "int A::*"). |
Anders Carlsson | 531d55f | 2009-12-31 17:43:53 +0000 | [diff] [blame] | 916 | Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0))); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 917 | } |
| 918 | |
John McCall | 9dffe6f | 2010-04-30 01:15:21 +0000 | [diff] [blame] | 919 | llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty, |
| 920 | bool ForEH) { |
| 921 | // Return a bogus pointer if RTTI is disabled, unless it's for EH. |
| 922 | // FIXME: should we even be calling this method if RTTI is disabled |
| 923 | // and it's not for EH? |
| 924 | if (!ForEH && !getContext().getLangOptions().RTTI) { |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 925 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
| 926 | return llvm::Constant::getNullValue(Int8PtrTy); |
| 927 | } |
John McCall | 9dffe6f | 2010-04-30 01:15:21 +0000 | [diff] [blame] | 928 | |
Anders Carlsson | 531d55f | 2009-12-31 17:43:53 +0000 | [diff] [blame] | 929 | return RTTIBuilder(*this).BuildTypeInfo(Ty); |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 930 | } |
Rafael Espindola | d1a5c31 | 2010-03-27 02:52:14 +0000 | [diff] [blame] | 931 | |
Rafael Espindola | d1a5c31 | 2010-03-27 02:52:14 +0000 | [diff] [blame] | 932 | void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) { |
| 933 | QualType PointerType = Context.getPointerType(Type); |
| 934 | QualType PointerTypeConst = Context.getPointerType(Type.withConst()); |
| 935 | RTTIBuilder(*this).BuildTypeInfo(Type, true); |
| 936 | RTTIBuilder(*this).BuildTypeInfo(PointerType, true); |
| 937 | RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true); |
| 938 | } |
| 939 | |
| 940 | void CodeGenModule::EmitFundamentalRTTIDescriptors() { |
Rafael Espindola | d1a5c31 | 2010-03-27 02:52:14 +0000 | [diff] [blame] | 941 | QualType FundamentalTypes[] = { Context.VoidTy, Context.Char32Ty, |
| 942 | Context.Char16Ty, Context.UnsignedLongLongTy, |
| 943 | Context.LongLongTy, Context.WCharTy, |
| 944 | Context.UnsignedShortTy, Context.ShortTy, |
| 945 | Context.UnsignedLongTy, Context.LongTy, |
| 946 | Context.UnsignedIntTy, Context.IntTy, |
| 947 | Context.UnsignedCharTy, Context.FloatTy, |
| 948 | Context.LongDoubleTy, Context.DoubleTy, |
| 949 | Context.CharTy, Context.BoolTy, |
| 950 | Context.SignedCharTy }; |
| 951 | for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i) |
| 952 | EmitFundamentalRTTIDescriptor(FundamentalTypes[i]); |
| 953 | } |