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