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