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 | 44636d1 | 2009-12-11 16:41:51 +0000 | [diff] [blame] | 27 | |
Anders Carlsson | 2344077 | 2009-12-17 05:06:03 +0000 | [diff] [blame^] | 28 | std::vector<llvm::Constant *> Info; |
| 29 | |
Anders Carlsson | d6baec8 | 2009-12-11 01:27:37 +0000 | [diff] [blame] | 30 | // Type info flags. |
| 31 | enum { |
| 32 | /// TI_Const - Type has const qualifier. |
| 33 | TI_Const = 0x1, |
| 34 | |
| 35 | /// TI_Volatile - Type has volatile qualifier. |
| 36 | TI_Volatile = 0x2, |
| 37 | |
| 38 | /// TI_Restrict - Type has restrict qualifier. |
| 39 | TI_Restrict = 0x4, |
| 40 | |
| 41 | /// TI_Incomplete - Type is incomplete. |
| 42 | TI_Incomplete = 0x8, |
| 43 | |
| 44 | /// TI_ContainingClassIncomplete - Containing class is incomplete. |
| 45 | /// (in pointer to member). |
| 46 | TI_ContainingClassIncomplete = 0x10 |
| 47 | }; |
| 48 | |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 49 | public: |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 50 | RTTIBuilder(CodeGenModule &cgm) |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 51 | : CGM(cgm), VMContext(cgm.getModule().getContext()), |
| 52 | Int8PtrTy(llvm::Type::getInt8PtrTy(VMContext)) { } |
| 53 | |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 54 | /// BuildVtableRef - Build a reference to a vtable. |
| 55 | llvm::Constant *BuildVtableRef(const char *Name) { |
| 56 | // Build a descriptor for Name |
Mike Stump | c7a05bd | 2009-11-14 15:55:18 +0000 | [diff] [blame] | 57 | llvm::Constant *GV = CGM.getModule().getGlobalVariable(Name); |
| 58 | if (GV) |
| 59 | GV = llvm::ConstantExpr::getBitCast(GV, |
| 60 | llvm::PointerType::get(Int8PtrTy, 0)); |
| 61 | else { |
| 62 | llvm::GlobalVariable::LinkageTypes linktype; |
| 63 | linktype = llvm::GlobalValue::ExternalLinkage; |
| 64 | GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, |
| 65 | true, linktype, 0, Name); |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 66 | } |
Mike Stump | c7a05bd | 2009-11-14 15:55:18 +0000 | [diff] [blame] | 67 | llvm::Constant *C; |
| 68 | C = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 2); |
| 69 | C = llvm::ConstantExpr::getInBoundsGetElementPtr(GV, &C, 1); |
| 70 | return llvm::ConstantExpr::getBitCast(C, Int8PtrTy); |
| 71 | } |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 72 | |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 73 | // FIXME: This should be removed, and clients should pass in the linkage |
| 74 | // directly instead. |
| 75 | static inline llvm::GlobalVariable::LinkageTypes |
| 76 | GetLinkageFromExternFlag(bool Extern) { |
| 77 | if (Extern) |
| 78 | return llvm::GlobalValue::WeakODRLinkage; |
| 79 | |
| 80 | return llvm::GlobalValue::InternalLinkage; |
| 81 | } |
| 82 | |
| 83 | // FIXME: This should be removed, and clients should pass in the linkage |
| 84 | // directly instead. |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 85 | llvm::Constant *BuildName(QualType Ty, bool Hidden, bool Extern) { |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 86 | return BuildName(Ty, Hidden, GetLinkageFromExternFlag(Extern)); |
| 87 | } |
| 88 | |
| 89 | llvm::Constant *BuildName(QualType Ty, bool Hidden, |
| 90 | llvm::GlobalVariable::LinkageTypes Linkage) { |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 91 | llvm::SmallString<256> OutName; |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 92 | CGM.getMangleContext().mangleCXXRTTIName(Ty, OutName); |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 93 | llvm::StringRef Name = OutName.str(); |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 94 | |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 95 | llvm::GlobalVariable *OGV = CGM.getModule().getGlobalVariable(Name); |
| 96 | if (OGV && !OGV->isDeclaration()) |
| 97 | return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy); |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 98 | |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 99 | llvm::Constant *C = llvm::ConstantArray::get(VMContext, Name.substr(4)); |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 100 | |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 101 | llvm::GlobalVariable *GV = |
| 102 | new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage, |
| 103 | C, Name); |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 104 | if (OGV) { |
| 105 | GV->takeName(OGV); |
| 106 | llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV, |
| 107 | OGV->getType()); |
| 108 | OGV->replaceAllUsesWith(NewPtr); |
| 109 | OGV->eraseFromParent(); |
| 110 | } |
Mike Stump | 582b037 | 2009-11-18 03:46:51 +0000 | [diff] [blame] | 111 | if (Hidden) |
| 112 | GV->setVisibility(llvm::GlobalVariable::HiddenVisibility); |
| 113 | return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy); |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 114 | }; |
Mike Stump | c7a05bd | 2009-11-14 15:55:18 +0000 | [diff] [blame] | 115 | |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 116 | /// - BuildFlags - Build a psABI __flags value for __vmi_class_type_info. |
| 117 | llvm::Constant *BuildFlags(int f) { |
| 118 | return llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), f); |
| 119 | } |
| 120 | |
| 121 | /// BuildBaseCount - Build a psABI __base_count value for |
| 122 | /// __vmi_class_type_info. |
| 123 | llvm::Constant *BuildBaseCount(unsigned c) { |
| 124 | return llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), c); |
| 125 | } |
| 126 | |
Mike Stump | ea2c0b5 | 2009-11-17 02:16:21 +0000 | [diff] [blame] | 127 | llvm::Constant *BuildTypeRef(QualType Ty) { |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 128 | llvm::Constant *C; |
Mike Stump | c7a05bd | 2009-11-14 15:55:18 +0000 | [diff] [blame] | 129 | |
Mike Stump | c7a05bd | 2009-11-14 15:55:18 +0000 | [diff] [blame] | 130 | llvm::SmallString<256> OutName; |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 131 | CGM.getMangleContext().mangleCXXRTTI(Ty, OutName); |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 132 | llvm::StringRef Name = OutName.str(); |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 133 | |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 134 | C = CGM.getModule().getGlobalVariable(Name); |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 135 | if (C) |
| 136 | return llvm::ConstantExpr::getBitCast(C, Int8PtrTy); |
| 137 | |
| 138 | llvm::GlobalVariable::LinkageTypes linktype; |
| 139 | linktype = llvm::GlobalValue::ExternalLinkage;; |
| 140 | |
| 141 | C = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, true, linktype, |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 142 | 0, Name); |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 143 | return llvm::ConstantExpr::getBitCast(C, Int8PtrTy); |
| 144 | } |
| 145 | |
Mike Stump | ea2c0b5 | 2009-11-17 02:16:21 +0000 | [diff] [blame] | 146 | llvm::Constant *Buildclass_type_infoRef(const CXXRecordDecl *RD) { |
| 147 | return BuildTypeRef(CGM.getContext().getTagDeclType(RD)); |
| 148 | } |
| 149 | |
Mike Stump | a8285a8 | 2009-11-15 03:28:10 +0000 | [diff] [blame] | 150 | /// CalculateFlags - Calculate the flags for the __vmi_class_type_info |
| 151 | /// datastructure. 1 for non-diamond repeated inheritance, 2 for a dimond |
| 152 | /// shaped class. |
| 153 | int CalculateFlags(const CXXRecordDecl*RD) { |
| 154 | int flags = 0; |
| 155 | if (SeenBase.count(RD)) |
| 156 | flags |= 1; |
| 157 | else |
| 158 | SeenBase.insert(RD); |
| 159 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 160 | e = RD->bases_end(); i != e; ++i) { |
| 161 | const CXXRecordDecl *Base = |
| 162 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 163 | if (i->isVirtual()) { |
| 164 | if (SeenVBase.count(Base)) |
| 165 | flags |= 2; |
| 166 | else |
| 167 | SeenVBase.insert(Base); |
| 168 | } |
| 169 | flags |= CalculateFlags(Base); |
| 170 | } |
| 171 | return flags; |
| 172 | } |
| 173 | |
| 174 | bool SimpleInheritance(const CXXRecordDecl *RD) { |
| 175 | if (RD->getNumBases() != 1) |
| 176 | return false; |
| 177 | CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(); |
| 178 | if (i->isVirtual()) |
| 179 | return false; |
| 180 | if (i->getAccessSpecifier() != AS_public) |
| 181 | return false; |
| 182 | |
| 183 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 184 | const CXXRecordDecl *Base = |
| 185 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
| 186 | if (Layout.getBaseClassOffset(Base) != 0) |
| 187 | return false; |
| 188 | return true; |
| 189 | } |
| 190 | |
Anders Carlsson | 5ba7d21 | 2009-12-13 16:38:01 +0000 | [diff] [blame] | 191 | llvm::Constant *finish(llvm::Constant *const *Values, unsigned NumValues, |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 192 | llvm::GlobalVariable *GV, |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 193 | llvm::StringRef Name, bool Hidden, |
| 194 | llvm::GlobalVariable::LinkageTypes Linkage) { |
| 195 | llvm::Constant *C = |
Anders Carlsson | 5ba7d21 | 2009-12-13 16:38:01 +0000 | [diff] [blame] | 196 | llvm::ConstantStruct::get(VMContext, Values, NumValues, /*Packed=*/false); |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 197 | |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 198 | llvm::GlobalVariable *OGV = GV; |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 199 | GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage, |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 200 | C, Name); |
| 201 | if (OGV) { |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 202 | GV->takeName(OGV); |
| 203 | llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV, |
| 204 | OGV->getType()); |
| 205 | OGV->replaceAllUsesWith(NewPtr); |
| 206 | OGV->eraseFromParent(); |
| 207 | } |
Mike Stump | 582b037 | 2009-11-18 03:46:51 +0000 | [diff] [blame] | 208 | if (Hidden) |
Mike Stump | 88a4a62 | 2009-11-18 03:21:29 +0000 | [diff] [blame] | 209 | GV->setVisibility(llvm::GlobalVariable::HiddenVisibility); |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 210 | return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy); |
| 211 | } |
| 212 | |
| 213 | |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 214 | llvm::Constant * |
| 215 | Buildclass_type_info(const CXXRecordDecl *RD, |
| 216 | llvm::GlobalVariable::LinkageTypes Linkage) { |
Anders Carlsson | 2344077 | 2009-12-17 05:06:03 +0000 | [diff] [blame^] | 217 | assert(Info.empty() && "Info vector must be empty!"); |
Anders Carlsson | 44636d1 | 2009-12-11 16:41:51 +0000 | [diff] [blame] | 218 | |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 219 | llvm::Constant *C; |
| 220 | |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 221 | llvm::SmallString<256> OutName; |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 222 | CGM.getMangleContext().mangleCXXRTTI(CGM.getContext().getTagDeclType(RD), |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 223 | OutName); |
| 224 | llvm::StringRef Name = OutName.str(); |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 225 | |
| 226 | llvm::GlobalVariable *GV; |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 227 | GV = CGM.getModule().getGlobalVariable(Name); |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 228 | if (GV && !GV->isDeclaration()) |
| 229 | return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy); |
| 230 | |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 231 | // 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] | 232 | if (RD->isInAnonymousNamespace() || !RD->hasLinkage()) |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 233 | Linkage = llvm::GlobalVariable::InternalLinkage; |
| 234 | |
Mike Stump | 582b037 | 2009-11-18 03:46:51 +0000 | [diff] [blame] | 235 | bool Hidden = CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden; |
| 236 | |
Mike Stump | a8285a8 | 2009-11-15 03:28:10 +0000 | [diff] [blame] | 237 | bool simple = false; |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 238 | if (RD->getNumBases() == 0) |
| 239 | C = BuildVtableRef("_ZTVN10__cxxabiv117__class_type_infoE"); |
Mike Stump | a8285a8 | 2009-11-15 03:28:10 +0000 | [diff] [blame] | 240 | else if (SimpleInheritance(RD)) { |
| 241 | simple = true; |
| 242 | C = BuildVtableRef("_ZTVN10__cxxabiv120__si_class_type_infoE"); |
| 243 | } else |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 244 | C = BuildVtableRef("_ZTVN10__cxxabiv121__vmi_class_type_infoE"); |
Anders Carlsson | 2344077 | 2009-12-17 05:06:03 +0000 | [diff] [blame^] | 245 | Info.push_back(C); |
| 246 | Info.push_back(BuildName(CGM.getContext().getTagDeclType(RD), Hidden, |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 247 | Linkage)); |
Mike Stump | c7a05bd | 2009-11-14 15:55:18 +0000 | [diff] [blame] | 248 | |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 249 | // If we have no bases, there are no more fields. |
| 250 | if (RD->getNumBases()) { |
Mike Stump | a8285a8 | 2009-11-15 03:28:10 +0000 | [diff] [blame] | 251 | if (!simple) { |
Anders Carlsson | 2344077 | 2009-12-17 05:06:03 +0000 | [diff] [blame^] | 252 | Info.push_back(BuildFlags(CalculateFlags(RD))); |
| 253 | Info.push_back(BuildBaseCount(RD->getNumBases())); |
Mike Stump | a8285a8 | 2009-11-15 03:28:10 +0000 | [diff] [blame] | 254 | } |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 255 | |
| 256 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
| 257 | for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), |
| 258 | e = RD->bases_end(); i != e; ++i) { |
| 259 | const CXXRecordDecl *Base = |
| 260 | cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 2344077 | 2009-12-17 05:06:03 +0000 | [diff] [blame^] | 261 | Info.push_back(CGM.GetAddrOfRTTI(Base)); |
Mike Stump | a8285a8 | 2009-11-15 03:28:10 +0000 | [diff] [blame] | 262 | if (simple) |
| 263 | break; |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 264 | int64_t offset; |
| 265 | if (!i->isVirtual()) |
Mike Stump | a8285a8 | 2009-11-15 03:28:10 +0000 | [diff] [blame] | 266 | offset = Layout.getBaseClassOffset(Base)/8; |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 267 | else |
| 268 | offset = CGM.getVtableInfo().getVirtualBaseOffsetIndex(RD, Base); |
| 269 | offset <<= 8; |
| 270 | // Now set the flags. |
| 271 | offset += i->isVirtual() ? 1 : 0;; |
| 272 | offset += i->getAccessSpecifier() == AS_public ? 2 : 0; |
| 273 | const llvm::Type *LongTy = |
| 274 | CGM.getTypes().ConvertType(CGM.getContext().LongTy); |
| 275 | C = llvm::ConstantInt::get(LongTy, offset); |
Anders Carlsson | 2344077 | 2009-12-17 05:06:03 +0000 | [diff] [blame^] | 276 | Info.push_back(C); |
Mike Stump | cbcd4e5 | 2009-11-14 23:32:21 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
Anders Carlsson | 2344077 | 2009-12-17 05:06:03 +0000 | [diff] [blame^] | 280 | return finish(&Info[0], Info.size(), GV, Name, Hidden, Linkage); |
Mike Stump | c7a05bd | 2009-11-14 15:55:18 +0000 | [diff] [blame] | 281 | } |
Mike Stump | ea2c0b5 | 2009-11-17 02:16:21 +0000 | [diff] [blame] | 282 | |
Mike Stump | 61c3801 | 2009-11-17 21:44:24 +0000 | [diff] [blame] | 283 | /// - BuildFlags - Build a __flags value for __pbase_type_info. |
Anders Carlsson | d6baec8 | 2009-12-11 01:27:37 +0000 | [diff] [blame] | 284 | llvm::Constant *BuildInt(unsigned n) { |
| 285 | return llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), n); |
Mike Stump | 61c3801 | 2009-11-17 21:44:24 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 288 | bool DecideExtern(QualType Ty) { |
| 289 | // For this type, see if all components are never in an anonymous namespace. |
| 290 | if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) |
| 291 | return (DecideExtern(MPT->getPointeeType()) |
| 292 | && DecideExtern(QualType(MPT->getClass(), 0))); |
| 293 | if (const PointerType *PT = Ty->getAs<PointerType>()) |
| 294 | return DecideExtern(PT->getPointeeType()); |
| 295 | if (const RecordType *RT = Ty->getAs<RecordType>()) |
| 296 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
Eli Friedman | 470fb73 | 2009-12-11 20:48:18 +0000 | [diff] [blame] | 297 | return !RD->isInAnonymousNamespace() && RD->hasLinkage(); |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 298 | return true; |
| 299 | } |
| 300 | |
| 301 | bool DecideHidden(QualType Ty) { |
| 302 | // For this type, see if all components are never hidden. |
| 303 | if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>()) |
| 304 | return (DecideHidden(MPT->getPointeeType()) |
| 305 | && DecideHidden(QualType(MPT->getClass(), 0))); |
| 306 | if (const PointerType *PT = Ty->getAs<PointerType>()) |
| 307 | return DecideHidden(PT->getPointeeType()); |
| 308 | if (const RecordType *RT = Ty->getAs<RecordType>()) |
| 309 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) |
| 310 | return CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden; |
| 311 | return false; |
| 312 | } |
| 313 | |
Mike Stump | 61c3801 | 2009-11-17 21:44:24 +0000 | [diff] [blame] | 314 | llvm::Constant *BuildPointerType(QualType Ty) { |
Anders Carlsson | 2344077 | 2009-12-17 05:06:03 +0000 | [diff] [blame^] | 315 | assert(Info.empty() && "Info vector must be empty!"); |
Anders Carlsson | 44636d1 | 2009-12-11 16:41:51 +0000 | [diff] [blame] | 316 | |
Mike Stump | 61c3801 | 2009-11-17 21:44:24 +0000 | [diff] [blame] | 317 | llvm::Constant *C; |
| 318 | |
| 319 | llvm::SmallString<256> OutName; |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 320 | CGM.getMangleContext().mangleCXXRTTI(Ty, OutName); |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 321 | llvm::StringRef Name = OutName.str(); |
Mike Stump | 61c3801 | 2009-11-17 21:44:24 +0000 | [diff] [blame] | 322 | |
| 323 | llvm::GlobalVariable *GV; |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 324 | GV = CGM.getModule().getGlobalVariable(Name); |
Mike Stump | 61c3801 | 2009-11-17 21:44:24 +0000 | [diff] [blame] | 325 | if (GV && !GV->isDeclaration()) |
| 326 | return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy); |
| 327 | |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 328 | bool Extern = DecideExtern(Ty); |
| 329 | bool Hidden = DecideHidden(Ty); |
Mike Stump | 582b037 | 2009-11-18 03:46:51 +0000 | [diff] [blame] | 330 | |
Anders Carlsson | d6baec8 | 2009-12-11 01:27:37 +0000 | [diff] [blame] | 331 | const MemberPointerType *PtrMemTy = dyn_cast<MemberPointerType>(Ty); |
| 332 | QualType PointeeTy; |
| 333 | |
| 334 | if (PtrMemTy) |
| 335 | PointeeTy = PtrMemTy->getPointeeType(); |
| 336 | else |
| 337 | PointeeTy = Ty->getPointeeType(); |
Mike Stump | 61c3801 | 2009-11-17 21:44:24 +0000 | [diff] [blame] | 338 | |
Anders Carlsson | d6baec8 | 2009-12-11 01:27:37 +0000 | [diff] [blame] | 339 | if (PtrMemTy) |
Mike Stump | 61c3801 | 2009-11-17 21:44:24 +0000 | [diff] [blame] | 340 | C = BuildVtableRef("_ZTVN10__cxxabiv129__pointer_to_member_type_infoE"); |
| 341 | else |
| 342 | C = BuildVtableRef("_ZTVN10__cxxabiv119__pointer_type_infoE"); |
Anders Carlsson | d6baec8 | 2009-12-11 01:27:37 +0000 | [diff] [blame] | 343 | |
Anders Carlsson | 2344077 | 2009-12-17 05:06:03 +0000 | [diff] [blame^] | 344 | Info.push_back(C); |
| 345 | Info.push_back(BuildName(Ty, Hidden, Extern)); |
Anders Carlsson | d6baec8 | 2009-12-11 01:27:37 +0000 | [diff] [blame] | 346 | Qualifiers Q = PointeeTy.getQualifiers(); |
| 347 | |
| 348 | PointeeTy = |
| 349 | CGM.getContext().getCanonicalType(PointeeTy).getUnqualifiedType(); |
| 350 | |
| 351 | unsigned Flags = 0; |
| 352 | if (Q.hasConst()) |
| 353 | Flags |= TI_Const; |
| 354 | if (Q.hasVolatile()) |
| 355 | Flags |= TI_Volatile; |
| 356 | if (Q.hasRestrict()) |
| 357 | Flags |= TI_Restrict; |
| 358 | |
| 359 | if (Ty->isIncompleteType()) |
| 360 | Flags |= TI_Incomplete; |
| 361 | |
| 362 | if (PtrMemTy && PtrMemTy->getClass()->isIncompleteType()) |
| 363 | Flags |= TI_ContainingClassIncomplete; |
| 364 | |
Anders Carlsson | 2344077 | 2009-12-17 05:06:03 +0000 | [diff] [blame^] | 365 | Info.push_back(BuildInt(Flags)); |
| 366 | Info.push_back(BuildInt(0)); |
| 367 | Info.push_back(RTTIBuilder(CGM).BuildType(PointeeTy)); |
Mike Stump | 61c3801 | 2009-11-17 21:44:24 +0000 | [diff] [blame] | 368 | |
Anders Carlsson | d6baec8 | 2009-12-11 01:27:37 +0000 | [diff] [blame] | 369 | if (PtrMemTy) |
Anders Carlsson | 2344077 | 2009-12-17 05:06:03 +0000 | [diff] [blame^] | 370 | Info.push_back(RTTIBuilder(CGM).BuildType( |
| 371 | QualType(PtrMemTy->getClass(), 0))); |
Mike Stump | 265df62 | 2009-11-17 23:51:10 +0000 | [diff] [blame] | 372 | |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 373 | // We always generate these as hidden, only the name isn't hidden. |
Anders Carlsson | 2344077 | 2009-12-17 05:06:03 +0000 | [diff] [blame^] | 374 | return finish(&Info[0], Info.size(), GV, Name, /*Hidden=*/true, |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 375 | GetLinkageFromExternFlag(Extern)); |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 376 | } |
Mike Stump | 61c3801 | 2009-11-17 21:44:24 +0000 | [diff] [blame] | 377 | |
Mike Stump | ae9b2be | 2009-11-17 23:45:57 +0000 | [diff] [blame] | 378 | llvm::Constant *BuildSimpleType(QualType Ty, const char *vtbl) { |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 379 | llvm::SmallString<256> OutName; |
Mike Stump | de05057 | 2009-12-02 18:57:08 +0000 | [diff] [blame] | 380 | CGM.getMangleContext().mangleCXXRTTI(Ty, OutName); |
Daniel Dunbar | 94fd26d | 2009-11-21 09:06:22 +0000 | [diff] [blame] | 381 | llvm::StringRef Name = OutName.str(); |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 382 | |
| 383 | llvm::GlobalVariable *GV; |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 384 | GV = CGM.getModule().getGlobalVariable(Name); |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 385 | if (GV && !GV->isDeclaration()) |
| 386 | return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy); |
| 387 | |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 388 | bool Extern = DecideExtern(Ty); |
| 389 | bool Hidden = DecideHidden(Ty); |
Mike Stump | 582b037 | 2009-11-18 03:46:51 +0000 | [diff] [blame] | 390 | |
Anders Carlsson | 3bf190e | 2009-12-13 23:47:29 +0000 | [diff] [blame] | 391 | llvm::Constant *Info[] = { |
| 392 | BuildVtableRef(vtbl), BuildName(Ty, Hidden, Extern) |
| 393 | }; |
| 394 | |
Mike Stump | 5858894 | 2009-11-19 01:08:19 +0000 | [diff] [blame] | 395 | // We always generate these as hidden, only the name isn't hidden. |
Anders Carlsson | 3bf190e | 2009-12-13 23:47:29 +0000 | [diff] [blame] | 396 | return finish(&Info[0], llvm::array_lengthof(Info), GV, Name, |
| 397 | /*Hidden=*/true, GetLinkageFromExternFlag(Extern)); |
Mike Stump | 61c3801 | 2009-11-17 21:44:24 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 400 | /// BuildType - Builds the type info for the given type. |
Mike Stump | ea2c0b5 | 2009-11-17 02:16:21 +0000 | [diff] [blame] | 401 | llvm::Constant *BuildType(QualType Ty) { |
| 402 | const clang::Type &Type |
| 403 | = *CGM.getContext().getCanonicalType(Ty).getTypePtr(); |
Mike Stump | 7e1365a | 2009-11-20 00:31:50 +0000 | [diff] [blame] | 404 | |
| 405 | if (const RecordType *RT = Ty.getTypePtr()->getAs<RecordType>()) |
| 406 | if (const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl())) |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 407 | return BuildClassTypeInfo(RD); |
Mike Stump | 7e1365a | 2009-11-20 00:31:50 +0000 | [diff] [blame] | 408 | |
Mike Stump | ea2c0b5 | 2009-11-17 02:16:21 +0000 | [diff] [blame] | 409 | switch (Type.getTypeClass()) { |
| 410 | default: { |
Mike Stump | ea2c0b5 | 2009-11-17 02:16:21 +0000 | [diff] [blame] | 411 | assert(0 && "typeid expression"); |
Mike Stump | ea2c0b5 | 2009-11-17 02:16:21 +0000 | [diff] [blame] | 412 | return llvm::Constant::getNullValue(Int8PtrTy); |
| 413 | } |
| 414 | |
| 415 | case Type::Builtin: { |
| 416 | // We expect all type_info objects for builtin types to be in the library. |
| 417 | return BuildTypeRef(Ty); |
| 418 | } |
Mike Stump | 21f5d5d | 2009-11-17 02:57:13 +0000 | [diff] [blame] | 419 | |
| 420 | case Type::Pointer: { |
| 421 | QualType PTy = Ty->getPointeeType(); |
| 422 | Qualifiers Q = PTy.getQualifiers(); |
| 423 | Q.removeConst(); |
| 424 | // T* and const T* for all builtin types T are expected in the library. |
| 425 | if (isa<BuiltinType>(PTy) && Q.empty()) |
| 426 | return BuildTypeRef(Ty); |
| 427 | |
Mike Stump | 61c3801 | 2009-11-17 21:44:24 +0000 | [diff] [blame] | 428 | return BuildPointerType(Ty); |
Mike Stump | 21f5d5d | 2009-11-17 02:57:13 +0000 | [diff] [blame] | 429 | } |
Mike Stump | 5fae856 | 2009-11-17 22:33:00 +0000 | [diff] [blame] | 430 | case Type::MemberPointer: |
| 431 | return BuildPointerType(Ty); |
Mike Stump | 64989f0 | 2009-11-17 23:11:22 +0000 | [diff] [blame] | 432 | case Type::FunctionProto: |
Mike Stump | ae9b2be | 2009-11-17 23:45:57 +0000 | [diff] [blame] | 433 | case Type::FunctionNoProto: |
| 434 | return BuildSimpleType(Ty, "_ZTVN10__cxxabiv120__function_type_infoE"); |
| 435 | case Type::ConstantArray: |
| 436 | case Type::IncompleteArray: |
| 437 | case Type::VariableArray: |
| 438 | case Type::Vector: |
| 439 | case Type::ExtVector: |
| 440 | return BuildSimpleType(Ty, "_ZTVN10__cxxabiv117__array_type_infoE"); |
| 441 | case Type::Enum: |
| 442 | return BuildSimpleType(Ty, "_ZTVN10__cxxabiv116__enum_type_infoE"); |
Mike Stump | ea2c0b5 | 2009-11-17 02:16:21 +0000 | [diff] [blame] | 443 | } |
| 444 | } |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 445 | |
| 446 | /// BuildClassTypeInfo - Builds the class type info (or a reference to it) |
| 447 | /// for the given record decl. |
| 448 | llvm::Constant *BuildClassTypeInfo(const CXXRecordDecl *RD) { |
| 449 | const CXXMethodDecl *KeyFunction = 0; |
| 450 | |
| 451 | if (RD->isDynamicClass()) |
| 452 | KeyFunction = CGM.getContext().getKeyFunction(RD); |
| 453 | |
| 454 | if (KeyFunction) { |
| 455 | // If the key function is defined in this translation unit, then the RTTI |
| 456 | // related constants should also be emitted here, with external linkage. |
| 457 | if (KeyFunction->getBody()) |
| 458 | return Buildclass_type_info(RD, llvm::GlobalValue::ExternalLinkage); |
| 459 | |
| 460 | // Otherwise, we just want a reference to the type info. |
| 461 | return Buildclass_type_infoRef(RD); |
| 462 | } |
| 463 | |
| 464 | // If there is no key function (or if the record doesn't have any virtual |
| 465 | // member functions or virtual bases), emit the type info with weak_odr |
| 466 | // linkage. |
| 467 | return Buildclass_type_info(RD, llvm::GlobalValue::WeakODRLinkage); |
| 468 | } |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 469 | }; |
Mike Stump | 92f2fe2 | 2009-12-02 19:07:44 +0000 | [diff] [blame] | 470 | } |
Mike Stump | 2b1bf31 | 2009-11-14 14:25:18 +0000 | [diff] [blame] | 471 | |
Anders Carlsson | 31b7f52 | 2009-12-11 02:46:30 +0000 | [diff] [blame] | 472 | llvm::Constant *CodeGenModule::GetAddrOfRTTI(const CXXRecordDecl *RD) { |
| 473 | if (!getContext().getLangOptions().RTTI) { |
| 474 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
| 475 | return llvm::Constant::getNullValue(Int8PtrTy); |
| 476 | } |
| 477 | |
| 478 | return RTTIBuilder(*this).BuildClassTypeInfo(RD); |
| 479 | } |
| 480 | |
| 481 | llvm::Constant *CodeGenModule::GetAddrOfRTTI(QualType Ty) { |
| 482 | if (!getContext().getLangOptions().RTTI) { |
| 483 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
| 484 | return llvm::Constant::getNullValue(Int8PtrTy); |
| 485 | } |
| 486 | |
| 487 | return RTTIBuilder(*this).BuildType(Ty); |
| 488 | } |