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