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; |
| 24 | const llvm::Type *Int8PtrTy; |
Mike Stump | a8285a8 | 2009-11-15 03:28:10 +0000 | [diff] [blame] | 25 | llvm::SmallSet<const CXXRecordDecl *, 16> SeenVBase; |
| 26 | llvm::SmallSet<const CXXRecordDecl *, 32> SeenBase; |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 27 | |
Anders Carlsson | 2344077 | 2009-12-17 05:06:03 +0000 | [diff] [blame] | 28 | std::vector<llvm::Constant *> Info; |
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 | /// BuildTypeInfo - Build the RTTI type info struct for the given type. |
| 35 | llvm::Constant *BuildTypeInfo(QualType Ty); |
| 36 | |
| 37 | /// BuildVtablePointer - Build the vtable pointer for the given type. |
| 38 | void BuildVtablePointer(const Type *Ty); |
| 39 | |
| 40 | /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, |
| 41 | /// used for pointer types. |
| 42 | void BuildPointerTypeInfo(const PointerType *Ty); |
| 43 | |
| 44 | /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info |
| 45 | /// struct, used for member pointer types. |
| 46 | void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty); |
| 47 | |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 48 | public: |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 49 | RTTIBuilder(CodeGenModule &cgm) |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 50 | : CGM(cgm), VMContext(cgm.getModule().getContext()), |
| 51 | Int8PtrTy(llvm::Type::getInt8PtrTy(VMContext)) { } |
| 52 | |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 53 | /// BuildVtableRef - Build a reference to a vtable. |
| 54 | llvm::Constant *BuildVtableRef(const char *Name) { |
| 55 | // Build a descriptor for Name |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 56 | llvm::Constant *GV = CGM.getModule().getNamedGlobal(Name); |
Mike Stump | c7a05bd | 2009-11-14 15:55:18 +0000 | [diff] [blame] | 57 | if (GV) |
| 58 | GV = llvm::ConstantExpr::getBitCast(GV, |
| 59 | llvm::PointerType::get(Int8PtrTy, 0)); |
| 60 | else { |
| 61 | llvm::GlobalVariable::LinkageTypes linktype; |
| 62 | linktype = llvm::GlobalValue::ExternalLinkage; |
| 63 | GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, |
| 64 | true, linktype, 0, Name); |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 65 | } |
Mike Stump | c7a05bd | 2009-11-14 15:55:18 +0000 | [diff] [blame] | 66 | llvm::Constant *C; |
| 67 | C = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 2); |
| 68 | C = llvm::ConstantExpr::getInBoundsGetElementPtr(GV, &C, 1); |
| 69 | return llvm::ConstantExpr::getBitCast(C, Int8PtrTy); |
| 70 | } |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 71 | |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 72 | // FIXME: This should be removed, and clients should pass in the linkage |
| 73 | // directly instead. |
| 74 | static inline llvm::GlobalVariable::LinkageTypes |
| 75 | GetLinkageFromExternFlag(bool Extern) { |
| 76 | if (Extern) |
| 77 | return llvm::GlobalValue::WeakODRLinkage; |
| 78 | |
| 79 | return llvm::GlobalValue::InternalLinkage; |
| 80 | } |
| 81 | |
| 82 | // FIXME: This should be removed, and clients should pass in the linkage |
| 83 | // directly instead. |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 84 | llvm::Constant *BuildName(QualType Ty, bool Hidden, bool Extern) { |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 85 | return BuildName(Ty, Hidden, GetLinkageFromExternFlag(Extern)); |
| 86 | } |
| 87 | |
| 88 | llvm::Constant *BuildName(QualType Ty, bool Hidden, |
| 89 | llvm::GlobalVariable::LinkageTypes Linkage) { |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 90 | llvm::SmallString<256> OutName; |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 91 | CGM.getMangleContext().mangleCXXRTTIName(Ty, OutName); |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 92 | llvm::StringRef Name = OutName.str(); |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 93 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 94 | llvm::GlobalVariable *OGV = CGM.getModule().getNamedGlobal(Name); |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 95 | if (OGV && !OGV->isDeclaration()) |
| 96 | return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy); |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 97 | |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 98 | llvm::Constant *C = llvm::ConstantArray::get(VMContext, Name.substr(4)); |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 99 | |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 100 | llvm::GlobalVariable *GV = |
| 101 | new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage, |
| 102 | C, Name); |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 103 | if (OGV) { |
| 104 | GV->takeName(OGV); |
| 105 | llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV, |
| 106 | OGV->getType()); |
| 107 | OGV->replaceAllUsesWith(NewPtr); |
| 108 | OGV->eraseFromParent(); |
| 109 | } |
Mike Stump | 582b037 | 2009-11-18 03:46:51 +0000 | [diff] [blame] | 110 | if (Hidden) |
| 111 | GV->setVisibility(llvm::GlobalVariable::HiddenVisibility); |
| 112 | return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy); |
Daniel Dunbar | 7177dee | 2009-12-19 17:50:07 +0000 | [diff] [blame] | 113 | } |
Mike Stump | c7a05bd | 2009-11-14 15:55:18 +0000 | [diff] [blame] | 114 | |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 115 | /// - BuildFlags - Build a psABI __flags value for __vmi_class_type_info. |
| 116 | llvm::Constant *BuildFlags(int f) { |
| 117 | return llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), f); |
| 118 | } |
| 119 | |
| 120 | /// BuildBaseCount - Build a psABI __base_count value for |
| 121 | /// __vmi_class_type_info. |
| 122 | llvm::Constant *BuildBaseCount(unsigned c) { |
| 123 | return llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), c); |
| 124 | } |
| 125 | |
Mike Stump | a8285a8 | 2009-11-15 03:28:10 +0000 | [diff] [blame] | 126 | /// CalculateFlags - Calculate the flags for the __vmi_class_type_info |
| 127 | /// datastructure. 1 for non-diamond repeated inheritance, 2 for a dimond |
| 128 | /// shaped class. |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 129 | int CalculateFlags(const CXXRecordDecl *RD) { |
Mike Stump | a8285a8 | 2009-11-15 03:28:10 +0000 | [diff] [blame] | 130 | int flags = 0; |
| 131 | if (SeenBase.count(RD)) |
| 132 | flags |= 1; |
| 133 | else |
| 134 | SeenBase.insert(RD); |
| 135 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 136 | e = RD->bases_end(); i != e; ++i) { |
| 137 | const CXXRecordDecl *Base = |
| 138 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 139 | if (i->isVirtual()) { |
| 140 | if (SeenVBase.count(Base)) |
| 141 | flags |= 2; |
| 142 | else |
| 143 | SeenVBase.insert(Base); |
| 144 | } |
| 145 | flags |= CalculateFlags(Base); |
| 146 | } |
| 147 | return flags; |
| 148 | } |
| 149 | |
| 150 | bool SimpleInheritance(const CXXRecordDecl *RD) { |
| 151 | if (RD->getNumBases() != 1) |
| 152 | return false; |
| 153 | CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(); |
| 154 | if (i->isVirtual()) |
| 155 | return false; |
| 156 | if (i->getAccessSpecifier() != AS_public) |
| 157 | return false; |
| 158 | |
| 159 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 160 | const CXXRecordDecl *Base = |
| 161 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 162 | if (Layout.getBaseClassOffset(Base) != 0) |
| 163 | return false; |
| 164 | return true; |
| 165 | } |
| 166 | |
Anders Carlsson | ab6faf3 | 2009-12-17 05:10:59 +0000 | [diff] [blame] | 167 | llvm::Constant *finish(llvm::GlobalVariable *GV, |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 168 | llvm::StringRef Name, bool Hidden, |
| 169 | llvm::GlobalVariable::LinkageTypes Linkage) { |
| 170 | llvm::Constant *C = |
Anders Carlsson | ab6faf3 | 2009-12-17 05:10:59 +0000 | [diff] [blame] | 171 | llvm::ConstantStruct::get(VMContext, &Info[0], Info.size(), |
| 172 | /*Packed=*/false); |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 173 | |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 174 | llvm::GlobalVariable *OGV = GV; |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 175 | GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage, |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 176 | C, Name); |
| 177 | if (OGV) { |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 178 | GV->takeName(OGV); |
| 179 | llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV, |
| 180 | OGV->getType()); |
| 181 | OGV->replaceAllUsesWith(NewPtr); |
| 182 | OGV->eraseFromParent(); |
| 183 | } |
Mike Stump | 582b037 | 2009-11-18 03:46:51 +0000 | [diff] [blame] | 184 | if (Hidden) |
Mike Stump | 88a4a62 | 2009-11-18 03:21:29 +0000 | [diff] [blame] | 185 | GV->setVisibility(llvm::GlobalVariable::HiddenVisibility); |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 186 | return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy); |
| 187 | } |
| 188 | |
| 189 | |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 190 | llvm::Constant * |
| 191 | Buildclass_type_info(const CXXRecordDecl *RD, |
| 192 | llvm::GlobalVariable::LinkageTypes Linkage) { |
Anders Carlsson | 2344077 | 2009-12-17 05:06:03 +0000 | [diff] [blame] | 193 | assert(Info.empty() && "Info vector must be empty!"); |
Anders Carlsson | 44636d1 | 2009-12-11 16:41:51 +0000 | [diff] [blame] | 194 | |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 195 | llvm::Constant *C; |
| 196 | |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 197 | llvm::SmallString<256> OutName; |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 198 | CGM.getMangleContext().mangleCXXRTTI(CGM.getContext().getTagDeclType(RD), |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 199 | OutName); |
| 200 | llvm::StringRef Name = OutName.str(); |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 201 | |
| 202 | llvm::GlobalVariable *GV; |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 203 | GV = CGM.getModule().getNamedGlobal(Name); |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 204 | if (GV && !GV->isDeclaration()) |
| 205 | return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy); |
| 206 | |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 207 | // If we're in an anonymous namespace, then we always want internal linkage. |
Eli Friedman | 470fb73 | 2009-12-11 20:48:18 +0000 | [diff] [blame] | 208 | if (RD->isInAnonymousNamespace() || !RD->hasLinkage()) |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 209 | Linkage = llvm::GlobalVariable::InternalLinkage; |
| 210 | |
Mike Stump | 582b037 | 2009-11-18 03:46:51 +0000 | [diff] [blame] | 211 | bool Hidden = CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden; |
| 212 | |
Mike Stump | a8285a8 | 2009-11-15 03:28:10 +0000 | [diff] [blame] | 213 | bool simple = false; |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 214 | if (RD->getNumBases() == 0) |
| 215 | C = BuildVtableRef("_ZTVN10__cxxabiv117__class_type_infoE"); |
Mike Stump | a8285a8 | 2009-11-15 03:28:10 +0000 | [diff] [blame] | 216 | else if (SimpleInheritance(RD)) { |
| 217 | simple = true; |
| 218 | C = BuildVtableRef("_ZTVN10__cxxabiv120__si_class_type_infoE"); |
| 219 | } else |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 220 | C = BuildVtableRef("_ZTVN10__cxxabiv121__vmi_class_type_infoE"); |
Anders Carlsson | 2344077 | 2009-12-17 05:06:03 +0000 | [diff] [blame] | 221 | Info.push_back(C); |
| 222 | Info.push_back(BuildName(CGM.getContext().getTagDeclType(RD), Hidden, |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 223 | Linkage)); |
Mike Stump | c7a05bd | 2009-11-14 15:55:18 +0000 | [diff] [blame] | 224 | |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 225 | // If we have no bases, there are no more fields. |
| 226 | if (RD->getNumBases()) { |
Mike Stump | a8285a8 | 2009-11-15 03:28:10 +0000 | [diff] [blame] | 227 | if (!simple) { |
Anders Carlsson | 2344077 | 2009-12-17 05:06:03 +0000 | [diff] [blame] | 228 | Info.push_back(BuildFlags(CalculateFlags(RD))); |
| 229 | Info.push_back(BuildBaseCount(RD->getNumBases())); |
Mike Stump | a8285a8 | 2009-11-15 03:28:10 +0000 | [diff] [blame] | 230 | } |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 231 | |
| 232 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 233 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 234 | e = RD->bases_end(); i != e; ++i) { |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 235 | QualType BaseType = i->getType(); |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 236 | const CXXRecordDecl *Base = |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 237 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
| 238 | Info.push_back(CGM.GetAddrOfRTTIDescriptor(BaseType)); |
Mike Stump | a8285a8 | 2009-11-15 03:28:10 +0000 | [diff] [blame] | 239 | if (simple) |
| 240 | break; |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 241 | int64_t offset; |
| 242 | if (!i->isVirtual()) |
Mike Stump | a8285a8 | 2009-11-15 03:28:10 +0000 | [diff] [blame] | 243 | offset = Layout.getBaseClassOffset(Base)/8; |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 244 | else |
| 245 | offset = CGM.getVtableInfo().getVirtualBaseOffsetIndex(RD, Base); |
| 246 | offset <<= 8; |
| 247 | // Now set the flags. |
| 248 | offset += i->isVirtual() ? 1 : 0;; |
| 249 | offset += i->getAccessSpecifier() == AS_public ? 2 : 0; |
| 250 | const llvm::Type *LongTy = |
| 251 | CGM.getTypes().ConvertType(CGM.getContext().LongTy); |
| 252 | C = llvm::ConstantInt::get(LongTy, offset); |
Anders Carlsson | 2344077 | 2009-12-17 05:06:03 +0000 | [diff] [blame] | 253 | Info.push_back(C); |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Anders Carlsson | ab6faf3 | 2009-12-17 05:10:59 +0000 | [diff] [blame] | 257 | return finish(GV, Name, Hidden, Linkage); |
Mike Stump | c7a05bd | 2009-11-14 15:55:18 +0000 | [diff] [blame] | 258 | } |
Mike Stump | ea2c0b5 | 2009-11-17 02:16:21 +0000 | [diff] [blame] | 259 | |
Mike Stump | 61c3801 | 2009-11-17 21:44:24 +0000 | [diff] [blame] | 260 | /// - BuildFlags - Build a __flags value for __pbase_type_info. |
Anders Carlsson | d6baec8 | 2009-12-11 01:27:37 +0000 | [diff] [blame] | 261 | llvm::Constant *BuildInt(unsigned n) { |
| 262 | return llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), n); |
Mike Stump | 61c3801 | 2009-11-17 21:44:24 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 265 | bool DecideExtern(QualType Ty) { |
| 266 | // For this type, see if all components are never in an anonymous namespace. |
| 267 | if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) |
| 268 | return (DecideExtern(MPT->getPointeeType()) |
| 269 | && DecideExtern(QualType(MPT->getClass(), 0))); |
| 270 | if (const PointerType *PT = Ty->getAs<PointerType>()) |
| 271 | return DecideExtern(PT->getPointeeType()); |
| 272 | if (const RecordType *RT = Ty->getAs<RecordType>()) |
| 273 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
Eli Friedman | 470fb73 | 2009-12-11 20:48:18 +0000 | [diff] [blame] | 274 | return !RD->isInAnonymousNamespace() && RD->hasLinkage(); |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 275 | return true; |
| 276 | } |
| 277 | |
| 278 | bool DecideHidden(QualType Ty) { |
| 279 | // For this type, see if all components are never hidden. |
| 280 | if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) |
| 281 | return (DecideHidden(MPT->getPointeeType()) |
| 282 | && DecideHidden(QualType(MPT->getClass(), 0))); |
| 283 | if (const PointerType *PT = Ty->getAs<PointerType>()) |
| 284 | return DecideHidden(PT->getPointeeType()); |
| 285 | if (const RecordType *RT = Ty->getAs<RecordType>()) |
| 286 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
| 287 | return CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden; |
| 288 | return false; |
| 289 | } |
| 290 | |
Mike Stump | ae9b2be | 2009-11-17 23:45:57 +0000 | [diff] [blame] | 291 | llvm::Constant *BuildSimpleType(QualType Ty, const char *vtbl) { |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 292 | llvm::SmallString<256> OutName; |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 293 | CGM.getMangleContext().mangleCXXRTTI(Ty, OutName); |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 294 | llvm::StringRef Name = OutName.str(); |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 295 | |
| 296 | llvm::GlobalVariable *GV; |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 297 | GV = CGM.getModule().getNamedGlobal(Name); |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 298 | if (GV && !GV->isDeclaration()) |
| 299 | return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy); |
| 300 | |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 301 | bool Extern = DecideExtern(Ty); |
| 302 | bool Hidden = DecideHidden(Ty); |
Mike Stump | 582b037 | 2009-11-18 03:46:51 +0000 | [diff] [blame] | 303 | |
Anders Carlsson | ab6faf3 | 2009-12-17 05:10:59 +0000 | [diff] [blame] | 304 | Info.push_back(BuildVtableRef(vtbl)); |
| 305 | Info.push_back(BuildName(Ty, Hidden, Extern)); |
Anders Carlsson | 3bf190e | 2009-12-13 23:47:29 +0000 | [diff] [blame] | 306 | |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 307 | // We always generate these as hidden, only the name isn't hidden. |
Anders Carlsson | ab6faf3 | 2009-12-17 05:10:59 +0000 | [diff] [blame] | 308 | return finish(GV, Name, /*Hidden=*/true, |
| 309 | GetLinkageFromExternFlag(Extern)); |
Mike Stump | 61c3801 | 2009-11-17 21:44:24 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 312 | /// BuildType - Builds the type info for the given type. |
Mike Stump | ea2c0b5 | 2009-11-17 02:16:21 +0000 | [diff] [blame] | 313 | llvm::Constant *BuildType(QualType Ty) { |
| 314 | const clang::Type &Type |
| 315 | = *CGM.getContext().getCanonicalType(Ty).getTypePtr(); |
Mike Stump | 7e1365a | 2009-11-20 00:31:50 +0000 | [diff] [blame] | 316 | |
Mike Stump | ea2c0b5 | 2009-11-17 02:16:21 +0000 | [diff] [blame] | 317 | switch (Type.getTypeClass()) { |
| 318 | default: { |
Mike Stump | ea2c0b5 | 2009-11-17 02:16:21 +0000 | [diff] [blame] | 319 | assert(0 && "typeid expression"); |
Mike Stump | ea2c0b5 | 2009-11-17 02:16:21 +0000 | [diff] [blame] | 320 | return llvm::Constant::getNullValue(Int8PtrTy); |
| 321 | } |
| 322 | |
| 323 | case Type::Builtin: { |
| 324 | // We expect all type_info objects for builtin types to be in the library. |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 325 | return GetAddrOfExternalRTTIDescriptor(Ty); |
Mike Stump | ea2c0b5 | 2009-11-17 02:16:21 +0000 | [diff] [blame] | 326 | } |
Mike Stump | 21f5d5d | 2009-11-17 02:57:13 +0000 | [diff] [blame] | 327 | |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 328 | case Type::Record: { |
| 329 | const RecordType *RT = cast<RecordType>(&Type); |
| 330 | |
| 331 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 332 | if (RD->getNumBases()) |
| 333 | return BuildClassTypeInfo(RD); |
| 334 | |
| 335 | // Fall through. |
| 336 | } |
| 337 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 338 | case Type::Pointer: |
Mike Stump | 5fae856 | 2009-11-17 22:33:00 +0000 | [diff] [blame] | 339 | case Type::MemberPointer: |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 340 | return BuildTypeInfo(Ty); |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 341 | |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 342 | case Type::FunctionProto: |
Mike Stump | ae9b2be | 2009-11-17 23:45:57 +0000 | [diff] [blame] | 343 | case Type::FunctionNoProto: |
| 344 | return BuildSimpleType(Ty, "_ZTVN10__cxxabiv120__function_type_infoE"); |
| 345 | case Type::ConstantArray: |
| 346 | case Type::IncompleteArray: |
| 347 | case Type::VariableArray: |
| 348 | case Type::Vector: |
| 349 | case Type::ExtVector: |
| 350 | return BuildSimpleType(Ty, "_ZTVN10__cxxabiv117__array_type_infoE"); |
| 351 | case Type::Enum: |
| 352 | return BuildSimpleType(Ty, "_ZTVN10__cxxabiv116__enum_type_infoE"); |
Mike Stump | ea2c0b5 | 2009-11-17 02:16:21 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 355 | |
| 356 | /// BuildClassTypeInfo - Builds the class type info (or a reference to it) |
| 357 | /// for the given record decl. |
| 358 | llvm::Constant *BuildClassTypeInfo(const CXXRecordDecl *RD) { |
| 359 | const CXXMethodDecl *KeyFunction = 0; |
| 360 | |
| 361 | if (RD->isDynamicClass()) |
| 362 | KeyFunction = CGM.getContext().getKeyFunction(RD); |
| 363 | |
| 364 | if (KeyFunction) { |
| 365 | // If the key function is defined in this translation unit, then the RTTI |
| 366 | // related constants should also be emitted here, with external linkage. |
| 367 | if (KeyFunction->getBody()) |
| 368 | return Buildclass_type_info(RD, llvm::GlobalValue::ExternalLinkage); |
| 369 | |
| 370 | // Otherwise, we just want a reference to the type info. |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 371 | QualType Ty = CGM.getContext().getTagDeclType(RD); |
| 372 | return GetAddrOfExternalRTTIDescriptor(Ty); |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | // If there is no key function (or if the record doesn't have any virtual |
| 376 | // member functions or virtual bases), emit the type info with weak_odr |
| 377 | // linkage. |
| 378 | return Buildclass_type_info(RD, llvm::GlobalValue::WeakODRLinkage); |
| 379 | } |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 380 | |
| 381 | // Pointer type info flags. |
| 382 | enum { |
| 383 | /// PTI_Const - Type has const qualifier. |
| 384 | PTI_Const = 0x1, |
| 385 | |
| 386 | /// PTI_Volatile - Type has volatile qualifier. |
| 387 | PTI_Volatile = 0x2, |
| 388 | |
| 389 | /// PTI_Restrict - Type has restrict qualifier. |
| 390 | PTI_Restrict = 0x4, |
| 391 | |
| 392 | /// PTI_Incomplete - Type is incomplete. |
| 393 | PTI_Incomplete = 0x8, |
| 394 | |
| 395 | /// PTI_ContainingClassIncomplete - Containing class is incomplete. |
| 396 | /// (in pointer to member). |
| 397 | PTI_ContainingClassIncomplete = 0x10 |
| 398 | }; |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 399 | }; |
Mike Stump | 92f2fe2 | 2009-12-02 19:07:44 +0000 | [diff] [blame] | 400 | } |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 401 | |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 402 | llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) { |
| 403 | // Mangle the RTTI name. |
| 404 | llvm::SmallString<256> OutName; |
| 405 | CGM.getMangleContext().mangleCXXRTTI(Ty, OutName); |
| 406 | llvm::StringRef Name = OutName.str(); |
| 407 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 408 | // Look for an existing global. |
| 409 | llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name); |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 410 | |
| 411 | if (!GV) { |
| 412 | // Create a new global variable. |
| 413 | GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true, |
| 414 | llvm::GlobalValue::ExternalLinkage, 0, Name); |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 417 | return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy); |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 420 | /// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type |
| 421 | /// info for that type is defined in the standard library. |
| 422 | static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) { |
| 423 | // Itanium C++ ABI 2.9.2: |
| 424 | // Basic type information (e.g. for "int", "bool", etc.) will be kept in |
| 425 | // the run-time support library. Specifically, the run-time support |
| 426 | // library should contain type_info objects for the types X, X* and |
| 427 | // X const*, for every X in: void, bool, wchar_t, char, unsigned char, |
| 428 | // signed char, short, unsigned short, int, unsigned int, long, |
| 429 | // unsigned long, long long, unsigned long long, float, double, long double, |
| 430 | // char16_t, char32_t, and the IEEE 754r decimal and half-precision |
| 431 | // floating point types. |
| 432 | switch (Ty->getKind()) { |
| 433 | case BuiltinType::Void: |
| 434 | case BuiltinType::Bool: |
| 435 | case BuiltinType::WChar: |
| 436 | case BuiltinType::Char_U: |
| 437 | case BuiltinType::Char_S: |
| 438 | case BuiltinType::UChar: |
| 439 | case BuiltinType::SChar: |
| 440 | case BuiltinType::Short: |
| 441 | case BuiltinType::UShort: |
| 442 | case BuiltinType::Int: |
| 443 | case BuiltinType::UInt: |
| 444 | case BuiltinType::Long: |
| 445 | case BuiltinType::ULong: |
| 446 | case BuiltinType::LongLong: |
| 447 | case BuiltinType::ULongLong: |
| 448 | case BuiltinType::Float: |
| 449 | case BuiltinType::Double: |
| 450 | case BuiltinType::LongDouble: |
| 451 | case BuiltinType::Char16: |
| 452 | case BuiltinType::Char32: |
| 453 | case BuiltinType::Int128: |
| 454 | case BuiltinType::UInt128: |
| 455 | return true; |
| 456 | |
| 457 | case BuiltinType::Overload: |
| 458 | case BuiltinType::Dependent: |
| 459 | case BuiltinType::UndeducedAuto: |
| 460 | assert(false && "Should not see this type here!"); |
| 461 | |
| 462 | case BuiltinType::NullPtr: |
| 463 | assert(false && "FIXME: nullptr_t is not handled!"); |
| 464 | |
| 465 | case BuiltinType::ObjCId: |
| 466 | case BuiltinType::ObjCClass: |
| 467 | case BuiltinType::ObjCSel: |
| 468 | assert(false && "FIXME: Objective-C types are unsupported!"); |
| 469 | } |
| 470 | |
| 471 | // Silent gcc. |
| 472 | return false; |
| 473 | } |
| 474 | |
| 475 | static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) { |
| 476 | QualType PointeeTy = PointerTy->getPointeeType(); |
| 477 | const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy); |
| 478 | if (!BuiltinTy) |
| 479 | return false; |
| 480 | |
| 481 | // Check the qualifiers. |
| 482 | Qualifiers Quals = PointeeTy.getQualifiers(); |
| 483 | Quals.removeConst(); |
| 484 | |
| 485 | if (!Quals.empty()) |
| 486 | return false; |
| 487 | |
| 488 | return TypeInfoIsInStandardLibrary(BuiltinTy); |
| 489 | } |
| 490 | |
| 491 | /// ShouldUseExternalRTTIDescriptor - Returns whether the type information for |
| 492 | /// the given type exists somewhere else, and that we should not emit the typ |
| 493 | /// information in this translation unit. |
| 494 | bool ShouldUseExternalRTTIDescriptor(QualType Ty) { |
| 495 | // Type info for builtin types is defined in the standard library. |
| 496 | if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty)) |
| 497 | return TypeInfoIsInStandardLibrary(BuiltinTy); |
| 498 | |
| 499 | // Type info for some pointer types to builtin types is defined in the |
| 500 | // standard library. |
| 501 | if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) |
| 502 | return TypeInfoIsInStandardLibrary(PointerTy); |
| 503 | |
| 504 | if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 505 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 506 | if (!RD->isDynamicClass()) |
| 507 | return false; |
| 508 | |
| 509 | // Get the key function. |
| 510 | const CXXMethodDecl *KeyFunction = RD->getASTContext().getKeyFunction(RD); |
| 511 | if (KeyFunction && !KeyFunction->getBody()) { |
| 512 | // The class has a key function, but it is not defined in this translation |
| 513 | // unit, so we should use the external descriptor for it. |
| 514 | return true; |
| 515 | } |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | return false; |
| 519 | } |
| 520 | |
| 521 | /// IsIncompleteClassType - Returns whether the given record type is incomplete. |
| 522 | static bool IsIncompleteClassType(const RecordType *RecordTy) { |
| 523 | return !RecordTy->getDecl()->isDefinition(); |
| 524 | } |
| 525 | |
Anders Carlsson | 17fa6f9 | 2009-12-20 23:37:55 +0000 | [diff] [blame] | 526 | /// ContainsIncompleteClassType - Returns whether the given type contains an |
| 527 | /// incomplete class type. This is true if |
| 528 | /// |
| 529 | /// * The given type is an incomplete class type. |
| 530 | /// * The given type is a pointer type whose pointee type contains an |
| 531 | /// incomplete class type. |
| 532 | /// * The given type is a member pointer type whose class is an incomplete |
| 533 | /// class type. |
| 534 | /// * The given type is a member pointer type whoise pointee type contains an |
| 535 | /// incomplete class type. |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 536 | /// is an indirect or direct pointer to an incomplete class type. |
Anders Carlsson | 17fa6f9 | 2009-12-20 23:37:55 +0000 | [diff] [blame] | 537 | static bool ContainsIncompleteClassType(QualType Ty) { |
| 538 | if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { |
| 539 | if (IsIncompleteClassType(RecordTy)) |
| 540 | return true; |
| 541 | } |
| 542 | |
| 543 | if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) |
| 544 | return ContainsIncompleteClassType(PointerTy->getPointeeType()); |
| 545 | |
| 546 | if (const MemberPointerType *MemberPointerTy = |
| 547 | dyn_cast<MemberPointerType>(Ty)) { |
| 548 | // Check if the class type is incomplete. |
| 549 | const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass()); |
| 550 | if (IsIncompleteClassType(ClassType)) |
| 551 | return true; |
| 552 | |
| 553 | return ContainsIncompleteClassType(MemberPointerTy->getPointeeType()); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | return false; |
| 557 | } |
| 558 | |
| 559 | /// getTypeInfoLinkage - Return the linkage that the type info and type info |
| 560 | /// name constants should have for the given type. |
| 561 | static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(QualType Ty) { |
Anders Carlsson | 17fa6f9 | 2009-12-20 23:37:55 +0000 | [diff] [blame] | 562 | // Itanium C++ ABI 2.9.5p7: |
| 563 | // In addition, it and all of the intermediate abi::__pointer_type_info |
| 564 | // structs in the chain down to the abi::__class_type_info for the |
| 565 | // incomplete class type must be prevented from resolving to the |
| 566 | // corresponding type_info structs for the complete class type, possibly |
| 567 | // by making them local static objects. Finally, a dummy class RTTI is |
| 568 | // generated for the incomplete type that will not resolve to the final |
| 569 | // complete class RTTI (because the latter need not exist), possibly by |
| 570 | // making it a local static object. |
| 571 | if (ContainsIncompleteClassType(Ty)) |
| 572 | return llvm::GlobalValue::InternalLinkage; |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 573 | |
| 574 | if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) { |
| 575 | // If the pointee type has internal linkage, then the pointer type needs to |
| 576 | // have it as well. |
| 577 | if (getTypeInfoLinkage(PointerTy->getPointeeType()) == |
| 578 | llvm::GlobalVariable::InternalLinkage) |
| 579 | return llvm::GlobalVariable::InternalLinkage; |
| 580 | |
| 581 | return llvm::GlobalVariable::WeakODRLinkage; |
| 582 | } |
| 583 | |
| 584 | if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) { |
| 585 | const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 586 | |
| 587 | // If we're in an anonymous namespace, then we always want internal linkage. |
| 588 | if (RD->isInAnonymousNamespace() || !RD->hasLinkage()) |
| 589 | return llvm::GlobalVariable::InternalLinkage; |
| 590 | |
| 591 | if (!RD->isDynamicClass()) |
| 592 | return llvm::GlobalValue::WeakODRLinkage; |
| 593 | |
| 594 | // Get the key function. |
| 595 | const CXXMethodDecl *KeyFunction = RD->getASTContext().getKeyFunction(RD); |
| 596 | if (!KeyFunction) { |
| 597 | // There is no key function, the RTTI descriptor is emitted with weak_odr |
| 598 | // linkage. |
| 599 | return llvm::GlobalValue::WeakODRLinkage; |
| 600 | } |
| 601 | |
| 602 | // Otherwise, the RTTI descriptor is emitted with external linkage. |
| 603 | return llvm::GlobalValue::ExternalLinkage; |
| 604 | } |
| 605 | |
Mike Stump | 8d9fb9b | 2009-12-23 22:48:20 +0000 | [diff] [blame] | 606 | if (Ty->getTypeClass() == Type::Builtin) { |
| 607 | return llvm::GlobalValue::WeakODRLinkage; |
| 608 | } |
| 609 | |
Mike Stump | c8f76f5 | 2009-12-24 01:10:27 +0000 | [diff] [blame^] | 610 | if (const FunctionType *FT = dyn_cast<FunctionType>(Ty)) { |
| 611 | if (getTypeInfoLinkage(FT->getResultType()) |
| 612 | == llvm::GlobalValue::InternalLinkage) |
| 613 | return llvm::GlobalValue::InternalLinkage; |
| 614 | |
| 615 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) { |
| 616 | for (unsigned i = 0; i < FPT->getNumArgs(); ++i) |
| 617 | if (getTypeInfoLinkage(FPT->getArgType(i)) |
| 618 | == llvm::GlobalValue::InternalLinkage) |
| 619 | return llvm::GlobalValue::InternalLinkage; |
| 620 | for (unsigned i = 0; i < FPT->getNumExceptions(); ++i) |
| 621 | if (getTypeInfoLinkage(FPT->getExceptionType(i)) |
| 622 | == llvm::GlobalValue::InternalLinkage) |
| 623 | return llvm::GlobalValue::InternalLinkage; |
| 624 | } |
| 625 | |
| 626 | return llvm::GlobalValue::WeakODRLinkage; |
| 627 | } |
| 628 | |
| 629 | // FIXME: We need to add code to handle all types. |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 630 | assert(false && "Unhandled type!"); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 631 | return llvm::GlobalValue::WeakODRLinkage; |
| 632 | } |
| 633 | |
| 634 | void RTTIBuilder::BuildVtablePointer(const Type *Ty) { |
| 635 | const char *VtableName; |
| 636 | |
| 637 | switch (Ty->getTypeClass()) { |
| 638 | default: assert(0 && "Unhandled type!"); |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 639 | |
| 640 | case Type::Record: { |
| 641 | const CXXRecordDecl *RD = |
| 642 | cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl()); |
| 643 | if (!RD->getNumBases()) { |
| 644 | // abi::__class_type_info |
| 645 | VtableName = "_ZTVN10__cxxabiv117__class_type_infoE"; |
| 646 | break; |
| 647 | } |
| 648 | } |
| 649 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 650 | case Type::Pointer: |
| 651 | // abi::__pointer_type_info |
| 652 | VtableName = "_ZTVN10__cxxabiv119__pointer_type_infoE"; |
| 653 | break; |
| 654 | case Type::MemberPointer: |
| 655 | // abi::__pointer_to_member_type_info |
| 656 | VtableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE"; |
| 657 | break; |
| 658 | } |
| 659 | |
| 660 | llvm::Constant *Vtable = |
| 661 | CGM.getModule().getOrInsertGlobal(VtableName, Int8PtrTy); |
| 662 | |
| 663 | const llvm::Type *PtrDiffTy = |
| 664 | CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType()); |
| 665 | |
| 666 | // The vtable address point is 2. |
| 667 | llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2); |
| 668 | Vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(Vtable, &Two, 1); |
| 669 | Vtable = llvm::ConstantExpr::getBitCast(Vtable, Int8PtrTy); |
| 670 | |
| 671 | Info.push_back(Vtable); |
| 672 | } |
| 673 | |
| 674 | llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty) { |
| 675 | // We want to operate on the canonical type. |
| 676 | Ty = CGM.getContext().getCanonicalType(Ty); |
| 677 | |
| 678 | // Check if we've already emitted an RTTI descriptor for this type. |
| 679 | llvm::SmallString<256> OutName; |
| 680 | CGM.getMangleContext().mangleCXXRTTI(Ty, OutName); |
| 681 | llvm::StringRef Name = OutName.str(); |
| 682 | |
| 683 | llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name); |
| 684 | if (OldGV && !OldGV->isDeclaration()) |
| 685 | return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy); |
| 686 | |
| 687 | // Check if there is already an external RTTI descriptor for this type. |
| 688 | if (ShouldUseExternalRTTIDescriptor(Ty)) |
| 689 | return GetAddrOfExternalRTTIDescriptor(Ty); |
| 690 | |
| 691 | llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(Ty); |
| 692 | |
| 693 | // Add the vtable pointer. |
| 694 | BuildVtablePointer(cast<Type>(Ty)); |
| 695 | |
| 696 | // And the name. |
| 697 | Info.push_back(BuildName(Ty, DecideHidden(Ty), Linkage)); |
| 698 | |
| 699 | switch (Ty->getTypeClass()) { |
| 700 | default: assert(false && "Unhandled type class!"); |
| 701 | case Type::Builtin: |
| 702 | assert(false && "Builtin type info must be in the standard library!"); |
| 703 | break; |
| 704 | |
Anders Carlsson | 625c1ae | 2009-12-21 00:41:42 +0000 | [diff] [blame] | 705 | case Type::Record: { |
| 706 | const CXXRecordDecl *RD = |
| 707 | cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl()); |
| 708 | if (!RD->getNumBases()) { |
| 709 | // We don't need to emit any fields. |
| 710 | break; |
| 711 | } |
| 712 | } |
| 713 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 714 | case Type::Pointer: |
| 715 | BuildPointerTypeInfo(cast<PointerType>(Ty)); |
| 716 | break; |
| 717 | |
| 718 | case Type::MemberPointer: |
| 719 | BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty)); |
| 720 | break; |
| 721 | } |
| 722 | |
| 723 | llvm::Constant *Init = |
| 724 | llvm::ConstantStruct::get(VMContext, &Info[0], Info.size(), |
| 725 | /*Packed=*/false); |
| 726 | |
| 727 | llvm::GlobalVariable *GV = |
| 728 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), |
| 729 | /*Constant=*/true, Linkage, Init, Name); |
| 730 | |
| 731 | // If there's already an old global variable, replace it with the new one. |
| 732 | if (OldGV) { |
| 733 | GV->takeName(OldGV); |
| 734 | llvm::Constant *NewPtr = |
| 735 | llvm::ConstantExpr::getBitCast(GV, OldGV->getType()); |
| 736 | OldGV->replaceAllUsesWith(NewPtr); |
| 737 | OldGV->eraseFromParent(); |
| 738 | } |
| 739 | |
| 740 | return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy); |
| 741 | } |
| 742 | |
| 743 | /// DetermineQualifierFlags - Deterine the pointer type info flags from the |
| 744 | /// given qualifier. |
| 745 | static unsigned DetermineQualifierFlags(Qualifiers Quals) { |
| 746 | unsigned Flags = 0; |
| 747 | |
| 748 | if (Quals.hasConst()) |
| 749 | Flags |= RTTIBuilder::PTI_Const; |
| 750 | if (Quals.hasVolatile()) |
| 751 | Flags |= RTTIBuilder::PTI_Volatile; |
| 752 | if (Quals.hasRestrict()) |
| 753 | Flags |= RTTIBuilder::PTI_Restrict; |
| 754 | |
| 755 | return Flags; |
| 756 | } |
| 757 | |
| 758 | /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, |
| 759 | /// used for pointer types. |
| 760 | void RTTIBuilder::BuildPointerTypeInfo(const PointerType *Ty) { |
Anders Carlsson | 17fa6f9 | 2009-12-20 23:37:55 +0000 | [diff] [blame] | 761 | QualType PointeeTy = Ty->getPointeeType(); |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 762 | |
| 763 | // Itanium C++ ABI 2.9.5p7: |
| 764 | // __flags is a flag word describing the cv-qualification and other |
| 765 | // attributes of the type pointed to |
| 766 | unsigned Flags = DetermineQualifierFlags(PointeeTy.getQualifiers()); |
| 767 | |
| 768 | // Itanium C++ ABI 2.9.5p7: |
| 769 | // When the abi::__pbase_type_info is for a direct or indirect pointer to an |
| 770 | // incomplete class type, the incomplete target type flag is set. |
Anders Carlsson | 17fa6f9 | 2009-12-20 23:37:55 +0000 | [diff] [blame] | 771 | if (ContainsIncompleteClassType(PointeeTy)) |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 772 | Flags |= PTI_Incomplete; |
| 773 | |
| 774 | const llvm::Type *UnsignedIntLTy = |
| 775 | CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); |
| 776 | Info.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); |
| 777 | |
| 778 | // Itanium C++ ABI 2.9.5p7: |
| 779 | // __pointee is a pointer to the std::type_info derivation for the |
| 780 | // unqualified type being pointed to. |
| 781 | Info.push_back(RTTIBuilder(CGM).BuildType(PointeeTy.getUnqualifiedType())); |
| 782 | } |
| 783 | |
| 784 | /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info |
| 785 | /// struct, used for member pointer types. |
| 786 | void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) { |
| 787 | QualType PointeeTy = Ty->getPointeeType(); |
| 788 | |
| 789 | // Itanium C++ ABI 2.9.5p7: |
| 790 | // __flags is a flag word describing the cv-qualification and other |
| 791 | // attributes of the type pointed to. |
| 792 | unsigned Flags = DetermineQualifierFlags(PointeeTy.getQualifiers()); |
| 793 | |
| 794 | const RecordType *ClassType = cast<RecordType>(Ty->getClass()); |
Anders Carlsson | 17fa6f9 | 2009-12-20 23:37:55 +0000 | [diff] [blame] | 795 | |
| 796 | // Itanium C++ ABI 2.9.5p7: |
| 797 | // When the abi::__pbase_type_info is for a direct or indirect pointer to an |
| 798 | // incomplete class type, the incomplete target type flag is set. |
| 799 | if (ContainsIncompleteClassType(PointeeTy)) |
| 800 | Flags |= PTI_Incomplete; |
| 801 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 802 | if (IsIncompleteClassType(ClassType)) |
| 803 | Flags |= PTI_ContainingClassIncomplete; |
| 804 | |
Anders Carlsson | 8d14515 | 2009-12-20 22:30:54 +0000 | [diff] [blame] | 805 | const llvm::Type *UnsignedIntLTy = |
| 806 | CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy); |
| 807 | Info.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags)); |
| 808 | |
| 809 | // Itanium C++ ABI 2.9.5p7: |
| 810 | // __pointee is a pointer to the std::type_info derivation for the |
| 811 | // unqualified type being pointed to. |
| 812 | Info.push_back(RTTIBuilder(CGM).BuildType(PointeeTy.getUnqualifiedType())); |
| 813 | |
| 814 | // Itanium C++ ABI 2.9.5p9: |
| 815 | // __context is a pointer to an abi::__class_type_info corresponding to the |
| 816 | // class type containing the member pointed to |
| 817 | // (e.g., the "A" in "int A::*"). |
| 818 | Info.push_back(RTTIBuilder(CGM).BuildType(QualType(ClassType, 0))); |
| 819 | } |
| 820 | |
Anders Carlsson | 1d7088d | 2009-12-17 07:09:17 +0000 | [diff] [blame] | 821 | llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty) { |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 822 | if (!getContext().getLangOptions().RTTI) { |
| 823 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
| 824 | return llvm::Constant::getNullValue(Int8PtrTy); |
| 825 | } |
| 826 | |
| 827 | return RTTIBuilder(*this).BuildType(Ty); |
| 828 | } |