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