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