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