blob: fc1098c98589ea3b79f4d9360c2b84aaf7e8d399 [file] [log] [blame]
Mike Stumpde050572009-12-02 18:57:08 +00001//===--- CGCXXRTTI.cpp - Emit LLVM Code for C++ RTTI descriptors ----------===//
Anders Carlsson656e4c12009-10-10 20:49:04 +00002//
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 Stumpae9b2be2009-11-17 23:45:57 +000014#include "clang/AST/Type.h"
Mike Stumpcbcd4e52009-11-14 23:32:21 +000015#include "clang/AST/RecordLayout.h"
Mike Stump61c38012009-11-17 21:44:24 +000016#include "CodeGenModule.h"
Anders Carlsson656e4c12009-10-10 20:49:04 +000017using namespace clang;
18using namespace CodeGen;
19
Mike Stump92f2fe22009-12-02 19:07:44 +000020namespace {
Mike Stumpde050572009-12-02 18:57:08 +000021class RTTIBuilder {
Mike Stump2b1bf312009-11-14 14:25:18 +000022 CodeGenModule &CGM; // Per-module state.
23 llvm::LLVMContext &VMContext;
24 const llvm::Type *Int8PtrTy;
Mike Stumpa8285a82009-11-15 03:28:10 +000025 llvm::SmallSet<const CXXRecordDecl *, 16> SeenVBase;
26 llvm::SmallSet<const CXXRecordDecl *, 32> SeenBase;
Anders Carlsson8d145152009-12-20 22:30:54 +000027
Anders Carlsson23440772009-12-17 05:06:03 +000028 std::vector<llvm::Constant *> Info;
Anders Carlssond6baec82009-12-11 01:27:37 +000029
Anders Carlsson1d7088d2009-12-17 07:09:17 +000030 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
31 /// descriptor of the given type.
32 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
33
Anders Carlsson8d145152009-12-20 22:30:54 +000034 /// 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 Stump2b1bf312009-11-14 14:25:18 +000048public:
Mike Stumpde050572009-12-02 18:57:08 +000049 RTTIBuilder(CodeGenModule &cgm)
Mike Stump2b1bf312009-11-14 14:25:18 +000050 : CGM(cgm), VMContext(cgm.getModule().getContext()),
51 Int8PtrTy(llvm::Type::getInt8PtrTy(VMContext)) { }
52
Mike Stumpcbcd4e52009-11-14 23:32:21 +000053 /// BuildVtableRef - Build a reference to a vtable.
54 llvm::Constant *BuildVtableRef(const char *Name) {
55 // Build a descriptor for Name
Anders Carlsson8d145152009-12-20 22:30:54 +000056 llvm::Constant *GV = CGM.getModule().getNamedGlobal(Name);
Mike Stumpc7a05bd2009-11-14 15:55:18 +000057 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 Stumpcbcd4e52009-11-14 23:32:21 +000065 }
Mike Stumpc7a05bd2009-11-14 15:55:18 +000066 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 Stumpcbcd4e52009-11-14 23:32:21 +000071
Anders Carlsson31b7f522009-12-11 02:46:30 +000072 // 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 Stump58588942009-11-19 01:08:19 +000084 llvm::Constant *BuildName(QualType Ty, bool Hidden, bool Extern) {
Anders Carlsson31b7f522009-12-11 02:46:30 +000085 return BuildName(Ty, Hidden, GetLinkageFromExternFlag(Extern));
86 }
87
88 llvm::Constant *BuildName(QualType Ty, bool Hidden,
89 llvm::GlobalVariable::LinkageTypes Linkage) {
Mike Stump2b1bf312009-11-14 14:25:18 +000090 llvm::SmallString<256> OutName;
Mike Stumpde050572009-12-02 18:57:08 +000091 CGM.getMangleContext().mangleCXXRTTIName(Ty, OutName);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +000092 llvm::StringRef Name = OutName.str();
Mike Stumpcbcd4e52009-11-14 23:32:21 +000093
Anders Carlsson8d145152009-12-20 22:30:54 +000094 llvm::GlobalVariable *OGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +000095 if (OGV && !OGV->isDeclaration())
96 return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy);
Mike Stump58588942009-11-19 01:08:19 +000097
Anders Carlsson31b7f522009-12-11 02:46:30 +000098 llvm::Constant *C = llvm::ConstantArray::get(VMContext, Name.substr(4));
Mike Stump2b1bf312009-11-14 14:25:18 +000099
Anders Carlsson31b7f522009-12-11 02:46:30 +0000100 llvm::GlobalVariable *GV =
101 new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
102 C, Name);
Mike Stump58588942009-11-19 01:08:19 +0000103 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 Stump582b0372009-11-18 03:46:51 +0000110 if (Hidden)
111 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
112 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Daniel Dunbar7177dee2009-12-19 17:50:07 +0000113 }
Mike Stumpc7a05bd2009-11-14 15:55:18 +0000114
Mike Stumpcbcd4e52009-11-14 23:32:21 +0000115 /// - 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 Stumpa8285a82009-11-15 03:28:10 +0000126 /// 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 Carlsson8d145152009-12-20 22:30:54 +0000129 int CalculateFlags(const CXXRecordDecl *RD) {
Mike Stumpa8285a82009-11-15 03:28:10 +0000130 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 Carlssonab6faf32009-12-17 05:10:59 +0000167 llvm::Constant *finish(llvm::GlobalVariable *GV,
Anders Carlsson31b7f522009-12-11 02:46:30 +0000168 llvm::StringRef Name, bool Hidden,
169 llvm::GlobalVariable::LinkageTypes Linkage) {
170 llvm::Constant *C =
Anders Carlssonab6faf32009-12-17 05:10:59 +0000171 llvm::ConstantStruct::get(VMContext, &Info[0], Info.size(),
172 /*Packed=*/false);
Mike Stump64989f02009-11-17 23:11:22 +0000173
Mike Stump58588942009-11-19 01:08:19 +0000174 llvm::GlobalVariable *OGV = GV;
Anders Carlsson31b7f522009-12-11 02:46:30 +0000175 GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
Mike Stump58588942009-11-19 01:08:19 +0000176 C, Name);
177 if (OGV) {
Mike Stump64989f02009-11-17 23:11:22 +0000178 GV->takeName(OGV);
179 llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
180 OGV->getType());
181 OGV->replaceAllUsesWith(NewPtr);
182 OGV->eraseFromParent();
183 }
Mike Stump582b0372009-11-18 03:46:51 +0000184 if (Hidden)
Mike Stump88a4a622009-11-18 03:21:29 +0000185 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
Mike Stump64989f02009-11-17 23:11:22 +0000186 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
187 }
188
189
Anders Carlsson31b7f522009-12-11 02:46:30 +0000190 llvm::Constant *
191 Buildclass_type_info(const CXXRecordDecl *RD,
192 llvm::GlobalVariable::LinkageTypes Linkage) {
Anders Carlsson23440772009-12-17 05:06:03 +0000193 assert(Info.empty() && "Info vector must be empty!");
Anders Carlsson44636d12009-12-11 16:41:51 +0000194
Mike Stump64989f02009-11-17 23:11:22 +0000195 llvm::Constant *C;
196
Mike Stumpcbcd4e52009-11-14 23:32:21 +0000197 llvm::SmallString<256> OutName;
Mike Stumpde050572009-12-02 18:57:08 +0000198 CGM.getMangleContext().mangleCXXRTTI(CGM.getContext().getTagDeclType(RD),
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000199 OutName);
200 llvm::StringRef Name = OutName.str();
Mike Stumpcbcd4e52009-11-14 23:32:21 +0000201
202 llvm::GlobalVariable *GV;
Anders Carlsson8d145152009-12-20 22:30:54 +0000203 GV = CGM.getModule().getNamedGlobal(Name);
Mike Stumpcbcd4e52009-11-14 23:32:21 +0000204 if (GV && !GV->isDeclaration())
205 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
206
Anders Carlsson31b7f522009-12-11 02:46:30 +0000207 // If we're in an anonymous namespace, then we always want internal linkage.
Eli Friedman470fb732009-12-11 20:48:18 +0000208 if (RD->isInAnonymousNamespace() || !RD->hasLinkage())
Anders Carlsson31b7f522009-12-11 02:46:30 +0000209 Linkage = llvm::GlobalVariable::InternalLinkage;
210
Mike Stump582b0372009-11-18 03:46:51 +0000211 bool Hidden = CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden;
212
Mike Stumpa8285a82009-11-15 03:28:10 +0000213 bool simple = false;
Mike Stumpcbcd4e52009-11-14 23:32:21 +0000214 if (RD->getNumBases() == 0)
215 C = BuildVtableRef("_ZTVN10__cxxabiv117__class_type_infoE");
Mike Stumpa8285a82009-11-15 03:28:10 +0000216 else if (SimpleInheritance(RD)) {
217 simple = true;
218 C = BuildVtableRef("_ZTVN10__cxxabiv120__si_class_type_infoE");
219 } else
Mike Stumpcbcd4e52009-11-14 23:32:21 +0000220 C = BuildVtableRef("_ZTVN10__cxxabiv121__vmi_class_type_infoE");
Anders Carlsson23440772009-12-17 05:06:03 +0000221 Info.push_back(C);
222 Info.push_back(BuildName(CGM.getContext().getTagDeclType(RD), Hidden,
Anders Carlsson31b7f522009-12-11 02:46:30 +0000223 Linkage));
Mike Stumpc7a05bd2009-11-14 15:55:18 +0000224
Mike Stumpcbcd4e52009-11-14 23:32:21 +0000225 // If we have no bases, there are no more fields.
226 if (RD->getNumBases()) {
Mike Stumpa8285a82009-11-15 03:28:10 +0000227 if (!simple) {
Anders Carlsson23440772009-12-17 05:06:03 +0000228 Info.push_back(BuildFlags(CalculateFlags(RD)));
229 Info.push_back(BuildBaseCount(RD->getNumBases()));
Mike Stumpa8285a82009-11-15 03:28:10 +0000230 }
Mike Stumpcbcd4e52009-11-14 23:32:21 +0000231
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 Carlsson1d7088d2009-12-17 07:09:17 +0000235 QualType BaseType = i->getType();
Mike Stumpcbcd4e52009-11-14 23:32:21 +0000236 const CXXRecordDecl *Base =
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000237 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
238 Info.push_back(CGM.GetAddrOfRTTIDescriptor(BaseType));
Mike Stumpa8285a82009-11-15 03:28:10 +0000239 if (simple)
240 break;
Mike Stumpcbcd4e52009-11-14 23:32:21 +0000241 int64_t offset;
242 if (!i->isVirtual())
Mike Stumpa8285a82009-11-15 03:28:10 +0000243 offset = Layout.getBaseClassOffset(Base)/8;
Mike Stumpcbcd4e52009-11-14 23:32:21 +0000244 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 Carlsson23440772009-12-17 05:06:03 +0000253 Info.push_back(C);
Mike Stumpcbcd4e52009-11-14 23:32:21 +0000254 }
255 }
256
Anders Carlssonab6faf32009-12-17 05:10:59 +0000257 return finish(GV, Name, Hidden, Linkage);
Mike Stumpc7a05bd2009-11-14 15:55:18 +0000258 }
Mike Stumpea2c0b52009-11-17 02:16:21 +0000259
Mike Stump61c38012009-11-17 21:44:24 +0000260 /// - BuildFlags - Build a __flags value for __pbase_type_info.
Anders Carlssond6baec82009-12-11 01:27:37 +0000261 llvm::Constant *BuildInt(unsigned n) {
262 return llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), n);
Mike Stump61c38012009-11-17 21:44:24 +0000263 }
264
Mike Stump4e6f8ee2009-12-24 02:33:48 +0000265 // FIXME: unify with getTypeInfoLinkage
Mike Stump58588942009-11-19 01:08:19 +0000266 bool DecideExtern(QualType Ty) {
267 // For this type, see if all components are never in an anonymous namespace.
268 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>())
269 return (DecideExtern(MPT->getPointeeType())
270 && DecideExtern(QualType(MPT->getClass(), 0)));
271 if (const PointerType *PT = Ty->getAs<PointerType>())
272 return DecideExtern(PT->getPointeeType());
Mike Stump4e6f8ee2009-12-24 02:33:48 +0000273 if (const FunctionType *FT = Ty->getAs<FunctionType>()) {
274 if (DecideExtern(FT->getResultType()) == false)
275 return false;
276 if (const FunctionProtoType *FPT = Ty->getAs<FunctionProtoType>()) {
277 for (unsigned i = 0; i <FPT->getNumArgs(); ++i)
278 if (DecideExtern(FPT->getArgType(i)) == false)
279 return false;
280 for (unsigned i = 0; i <FPT->getNumExceptions(); ++i)
281 if (DecideExtern(FPT->getExceptionType(i)) == false)
282 return false;
283 return true;
284 }
285 }
Mike Stump58588942009-11-19 01:08:19 +0000286 if (const RecordType *RT = Ty->getAs<RecordType>())
287 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
Eli Friedman470fb732009-12-11 20:48:18 +0000288 return !RD->isInAnonymousNamespace() && RD->hasLinkage();
Mike Stump58588942009-11-19 01:08:19 +0000289 return true;
290 }
291
Mike Stump4e6f8ee2009-12-24 02:33:48 +0000292 // FIXME: unify with DecideExtern
Mike Stump58588942009-11-19 01:08:19 +0000293 bool DecideHidden(QualType Ty) {
294 // For this type, see if all components are never hidden.
295 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>())
296 return (DecideHidden(MPT->getPointeeType())
297 && DecideHidden(QualType(MPT->getClass(), 0)));
298 if (const PointerType *PT = Ty->getAs<PointerType>())
299 return DecideHidden(PT->getPointeeType());
Mike Stump4e6f8ee2009-12-24 02:33:48 +0000300 if (const FunctionType *FT = Ty->getAs<FunctionType>()) {
301 if (DecideHidden(FT->getResultType()) == false)
302 return false;
303 if (const FunctionProtoType *FPT = Ty->getAs<FunctionProtoType>()) {
304 for (unsigned i = 0; i <FPT->getNumArgs(); ++i)
305 if (DecideHidden(FPT->getArgType(i)) == false)
306 return false;
307 for (unsigned i = 0; i <FPT->getNumExceptions(); ++i)
308 if (DecideHidden(FPT->getExceptionType(i)) == false)
309 return false;
310 return true;
311 }
312 }
Mike Stump58588942009-11-19 01:08:19 +0000313 if (const RecordType *RT = Ty->getAs<RecordType>())
314 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
315 return CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden;
316 return false;
317 }
318
Mike Stumpae9b2be2009-11-17 23:45:57 +0000319 llvm::Constant *BuildSimpleType(QualType Ty, const char *vtbl) {
Mike Stump64989f02009-11-17 23:11:22 +0000320 llvm::SmallString<256> OutName;
Mike Stumpde050572009-12-02 18:57:08 +0000321 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000322 llvm::StringRef Name = OutName.str();
Mike Stump64989f02009-11-17 23:11:22 +0000323
324 llvm::GlobalVariable *GV;
Anders Carlsson8d145152009-12-20 22:30:54 +0000325 GV = CGM.getModule().getNamedGlobal(Name);
Mike Stump64989f02009-11-17 23:11:22 +0000326 if (GV && !GV->isDeclaration())
327 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
328
Mike Stump58588942009-11-19 01:08:19 +0000329 bool Extern = DecideExtern(Ty);
330 bool Hidden = DecideHidden(Ty);
Mike Stump582b0372009-11-18 03:46:51 +0000331
Anders Carlssonab6faf32009-12-17 05:10:59 +0000332 Info.push_back(BuildVtableRef(vtbl));
333 Info.push_back(BuildName(Ty, Hidden, Extern));
Anders Carlsson3bf190e2009-12-13 23:47:29 +0000334
Mike Stump58588942009-11-19 01:08:19 +0000335 // We always generate these as hidden, only the name isn't hidden.
Mike Stump4e6f8ee2009-12-24 02:33:48 +0000336 return finish(GV, Name, /*Hidden=*/Extern ? true : false,
Anders Carlssonab6faf32009-12-17 05:10:59 +0000337 GetLinkageFromExternFlag(Extern));
Mike Stump61c38012009-11-17 21:44:24 +0000338 }
339
Anders Carlsson31b7f522009-12-11 02:46:30 +0000340 /// BuildType - Builds the type info for the given type.
Mike Stumpea2c0b52009-11-17 02:16:21 +0000341 llvm::Constant *BuildType(QualType Ty) {
342 const clang::Type &Type
343 = *CGM.getContext().getCanonicalType(Ty).getTypePtr();
Mike Stump7e1365a2009-11-20 00:31:50 +0000344
Mike Stumpea2c0b52009-11-17 02:16:21 +0000345 switch (Type.getTypeClass()) {
346 default: {
Mike Stumpea2c0b52009-11-17 02:16:21 +0000347 assert(0 && "typeid expression");
Mike Stumpea2c0b52009-11-17 02:16:21 +0000348 return llvm::Constant::getNullValue(Int8PtrTy);
349 }
350
351 case Type::Builtin: {
352 // We expect all type_info objects for builtin types to be in the library.
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000353 return GetAddrOfExternalRTTIDescriptor(Ty);
Mike Stumpea2c0b52009-11-17 02:16:21 +0000354 }
Mike Stump21f5d5d2009-11-17 02:57:13 +0000355
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000356 case Type::Record: {
357 const RecordType *RT = cast<RecordType>(&Type);
358
359 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
360 if (RD->getNumBases())
361 return BuildClassTypeInfo(RD);
362
363 // Fall through.
364 }
365
Anders Carlsson8d145152009-12-20 22:30:54 +0000366 case Type::Pointer:
Mike Stump5fae8562009-11-17 22:33:00 +0000367 case Type::MemberPointer:
Mike Stump64989f02009-11-17 23:11:22 +0000368 case Type::FunctionProto:
Mike Stumpae9b2be2009-11-17 23:45:57 +0000369 case Type::FunctionNoProto:
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000370 return BuildTypeInfo(Ty);
371
Mike Stumpae9b2be2009-11-17 23:45:57 +0000372 case Type::ConstantArray:
373 case Type::IncompleteArray:
374 case Type::VariableArray:
375 case Type::Vector:
376 case Type::ExtVector:
377 return BuildSimpleType(Ty, "_ZTVN10__cxxabiv117__array_type_infoE");
378 case Type::Enum:
379 return BuildSimpleType(Ty, "_ZTVN10__cxxabiv116__enum_type_infoE");
Mike Stumpea2c0b52009-11-17 02:16:21 +0000380 }
381 }
Anders Carlsson31b7f522009-12-11 02:46:30 +0000382
383 /// BuildClassTypeInfo - Builds the class type info (or a reference to it)
384 /// for the given record decl.
385 llvm::Constant *BuildClassTypeInfo(const CXXRecordDecl *RD) {
386 const CXXMethodDecl *KeyFunction = 0;
387
388 if (RD->isDynamicClass())
389 KeyFunction = CGM.getContext().getKeyFunction(RD);
390
391 if (KeyFunction) {
392 // If the key function is defined in this translation unit, then the RTTI
393 // related constants should also be emitted here, with external linkage.
394 if (KeyFunction->getBody())
395 return Buildclass_type_info(RD, llvm::GlobalValue::ExternalLinkage);
396
397 // Otherwise, we just want a reference to the type info.
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000398 QualType Ty = CGM.getContext().getTagDeclType(RD);
399 return GetAddrOfExternalRTTIDescriptor(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000400 }
401
402 // If there is no key function (or if the record doesn't have any virtual
403 // member functions or virtual bases), emit the type info with weak_odr
404 // linkage.
405 return Buildclass_type_info(RD, llvm::GlobalValue::WeakODRLinkage);
406 }
Anders Carlsson8d145152009-12-20 22:30:54 +0000407
408 // Pointer type info flags.
409 enum {
410 /// PTI_Const - Type has const qualifier.
411 PTI_Const = 0x1,
412
413 /// PTI_Volatile - Type has volatile qualifier.
414 PTI_Volatile = 0x2,
415
416 /// PTI_Restrict - Type has restrict qualifier.
417 PTI_Restrict = 0x4,
418
419 /// PTI_Incomplete - Type is incomplete.
420 PTI_Incomplete = 0x8,
421
422 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
423 /// (in pointer to member).
424 PTI_ContainingClassIncomplete = 0x10
425 };
Mike Stump2b1bf312009-11-14 14:25:18 +0000426};
Mike Stump92f2fe22009-12-02 19:07:44 +0000427}
Mike Stump2b1bf312009-11-14 14:25:18 +0000428
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000429llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
430 // Mangle the RTTI name.
431 llvm::SmallString<256> OutName;
432 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
433 llvm::StringRef Name = OutName.str();
434
Anders Carlsson8d145152009-12-20 22:30:54 +0000435 // Look for an existing global.
436 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000437
438 if (!GV) {
439 // Create a new global variable.
440 GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
441 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000442 }
443
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000444 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000445}
446
Anders Carlsson8d145152009-12-20 22:30:54 +0000447/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
448/// info for that type is defined in the standard library.
449static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
450 // Itanium C++ ABI 2.9.2:
451 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
452 // the run-time support library. Specifically, the run-time support
453 // library should contain type_info objects for the types X, X* and
454 // X const*, for every X in: void, bool, wchar_t, char, unsigned char,
455 // signed char, short, unsigned short, int, unsigned int, long,
456 // unsigned long, long long, unsigned long long, float, double, long double,
457 // char16_t, char32_t, and the IEEE 754r decimal and half-precision
458 // floating point types.
459 switch (Ty->getKind()) {
460 case BuiltinType::Void:
461 case BuiltinType::Bool:
462 case BuiltinType::WChar:
463 case BuiltinType::Char_U:
464 case BuiltinType::Char_S:
465 case BuiltinType::UChar:
466 case BuiltinType::SChar:
467 case BuiltinType::Short:
468 case BuiltinType::UShort:
469 case BuiltinType::Int:
470 case BuiltinType::UInt:
471 case BuiltinType::Long:
472 case BuiltinType::ULong:
473 case BuiltinType::LongLong:
474 case BuiltinType::ULongLong:
475 case BuiltinType::Float:
476 case BuiltinType::Double:
477 case BuiltinType::LongDouble:
478 case BuiltinType::Char16:
479 case BuiltinType::Char32:
480 case BuiltinType::Int128:
481 case BuiltinType::UInt128:
482 return true;
483
484 case BuiltinType::Overload:
485 case BuiltinType::Dependent:
486 case BuiltinType::UndeducedAuto:
487 assert(false && "Should not see this type here!");
488
489 case BuiltinType::NullPtr:
490 assert(false && "FIXME: nullptr_t is not handled!");
491
492 case BuiltinType::ObjCId:
493 case BuiltinType::ObjCClass:
494 case BuiltinType::ObjCSel:
495 assert(false && "FIXME: Objective-C types are unsupported!");
496 }
497
498 // Silent gcc.
499 return false;
500}
501
502static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
503 QualType PointeeTy = PointerTy->getPointeeType();
504 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
505 if (!BuiltinTy)
506 return false;
507
508 // Check the qualifiers.
509 Qualifiers Quals = PointeeTy.getQualifiers();
510 Quals.removeConst();
511
512 if (!Quals.empty())
513 return false;
514
515 return TypeInfoIsInStandardLibrary(BuiltinTy);
516}
517
518/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
519/// the given type exists somewhere else, and that we should not emit the typ
520/// information in this translation unit.
521bool ShouldUseExternalRTTIDescriptor(QualType Ty) {
522 // Type info for builtin types is defined in the standard library.
523 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
524 return TypeInfoIsInStandardLibrary(BuiltinTy);
525
526 // Type info for some pointer types to builtin types is defined in the
527 // standard library.
528 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
529 return TypeInfoIsInStandardLibrary(PointerTy);
530
531 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000532 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
533 if (!RD->isDynamicClass())
534 return false;
535
536 // Get the key function.
537 const CXXMethodDecl *KeyFunction = RD->getASTContext().getKeyFunction(RD);
538 if (KeyFunction && !KeyFunction->getBody()) {
539 // The class has a key function, but it is not defined in this translation
540 // unit, so we should use the external descriptor for it.
541 return true;
542 }
Anders Carlsson8d145152009-12-20 22:30:54 +0000543 }
544
545 return false;
546}
547
548/// IsIncompleteClassType - Returns whether the given record type is incomplete.
549static bool IsIncompleteClassType(const RecordType *RecordTy) {
550 return !RecordTy->getDecl()->isDefinition();
551}
552
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000553/// ContainsIncompleteClassType - Returns whether the given type contains an
554/// incomplete class type. This is true if
555///
556/// * The given type is an incomplete class type.
557/// * The given type is a pointer type whose pointee type contains an
558/// incomplete class type.
559/// * The given type is a member pointer type whose class is an incomplete
560/// class type.
561/// * The given type is a member pointer type whoise pointee type contains an
562/// incomplete class type.
Anders Carlsson8d145152009-12-20 22:30:54 +0000563/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000564static bool ContainsIncompleteClassType(QualType Ty) {
565 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
566 if (IsIncompleteClassType(RecordTy))
567 return true;
568 }
569
570 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
571 return ContainsIncompleteClassType(PointerTy->getPointeeType());
572
573 if (const MemberPointerType *MemberPointerTy =
574 dyn_cast<MemberPointerType>(Ty)) {
575 // Check if the class type is incomplete.
576 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
577 if (IsIncompleteClassType(ClassType))
578 return true;
579
580 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000581 }
582
583 return false;
584}
585
586/// getTypeInfoLinkage - Return the linkage that the type info and type info
587/// name constants should have for the given type.
588static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(QualType Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000589 // Itanium C++ ABI 2.9.5p7:
590 // In addition, it and all of the intermediate abi::__pointer_type_info
591 // structs in the chain down to the abi::__class_type_info for the
592 // incomplete class type must be prevented from resolving to the
593 // corresponding type_info structs for the complete class type, possibly
594 // by making them local static objects. Finally, a dummy class RTTI is
595 // generated for the incomplete type that will not resolve to the final
596 // complete class RTTI (because the latter need not exist), possibly by
597 // making it a local static object.
598 if (ContainsIncompleteClassType(Ty))
599 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000600
601 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) {
602 // If the pointee type has internal linkage, then the pointer type needs to
603 // have it as well.
604 if (getTypeInfoLinkage(PointerTy->getPointeeType()) ==
605 llvm::GlobalVariable::InternalLinkage)
606 return llvm::GlobalVariable::InternalLinkage;
607
608 return llvm::GlobalVariable::WeakODRLinkage;
609 }
610
611 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
612 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
613
614 // If we're in an anonymous namespace, then we always want internal linkage.
615 if (RD->isInAnonymousNamespace() || !RD->hasLinkage())
616 return llvm::GlobalVariable::InternalLinkage;
617
618 if (!RD->isDynamicClass())
619 return llvm::GlobalValue::WeakODRLinkage;
620
621 // Get the key function.
622 const CXXMethodDecl *KeyFunction = RD->getASTContext().getKeyFunction(RD);
623 if (!KeyFunction) {
624 // There is no key function, the RTTI descriptor is emitted with weak_odr
625 // linkage.
626 return llvm::GlobalValue::WeakODRLinkage;
627 }
628
629 // Otherwise, the RTTI descriptor is emitted with external linkage.
630 return llvm::GlobalValue::ExternalLinkage;
631 }
632
Mike Stump8d9fb9b2009-12-23 22:48:20 +0000633 if (Ty->getTypeClass() == Type::Builtin) {
634 return llvm::GlobalValue::WeakODRLinkage;
635 }
636
Mike Stumpc8f76f52009-12-24 01:10:27 +0000637 if (const FunctionType *FT = dyn_cast<FunctionType>(Ty)) {
638 if (getTypeInfoLinkage(FT->getResultType())
639 == llvm::GlobalValue::InternalLinkage)
640 return llvm::GlobalValue::InternalLinkage;
641
642 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
643 for (unsigned i = 0; i < FPT->getNumArgs(); ++i)
644 if (getTypeInfoLinkage(FPT->getArgType(i))
645 == llvm::GlobalValue::InternalLinkage)
646 return llvm::GlobalValue::InternalLinkage;
647 for (unsigned i = 0; i < FPT->getNumExceptions(); ++i)
648 if (getTypeInfoLinkage(FPT->getExceptionType(i))
649 == llvm::GlobalValue::InternalLinkage)
650 return llvm::GlobalValue::InternalLinkage;
651 }
652
653 return llvm::GlobalValue::WeakODRLinkage;
654 }
655
656 // FIXME: We need to add code to handle all types.
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000657 assert(false && "Unhandled type!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000658 return llvm::GlobalValue::WeakODRLinkage;
659}
660
661void RTTIBuilder::BuildVtablePointer(const Type *Ty) {
662 const char *VtableName;
663
664 switch (Ty->getTypeClass()) {
665 default: assert(0 && "Unhandled type!");
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000666
667 case Type::Record: {
668 const CXXRecordDecl *RD =
669 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
670 if (!RD->getNumBases()) {
671 // abi::__class_type_info
672 VtableName = "_ZTVN10__cxxabiv117__class_type_infoE";
673 break;
674 }
675 }
676
Anders Carlsson8d145152009-12-20 22:30:54 +0000677 case Type::Pointer:
678 // abi::__pointer_type_info
679 VtableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
680 break;
681 case Type::MemberPointer:
682 // abi::__pointer_to_member_type_info
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000683 VtableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000684 break;
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000685
686 case Type::FunctionNoProto:
687 case Type::FunctionProto:
688 // abi::__function_type_info
689 VtableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000690 }
691
692 llvm::Constant *Vtable =
693 CGM.getModule().getOrInsertGlobal(VtableName, Int8PtrTy);
694
695 const llvm::Type *PtrDiffTy =
696 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
697
698 // The vtable address point is 2.
699 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
700 Vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(Vtable, &Two, 1);
701 Vtable = llvm::ConstantExpr::getBitCast(Vtable, Int8PtrTy);
702
703 Info.push_back(Vtable);
704}
705
706llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty) {
707 // We want to operate on the canonical type.
708 Ty = CGM.getContext().getCanonicalType(Ty);
709
710 // Check if we've already emitted an RTTI descriptor for this type.
711 llvm::SmallString<256> OutName;
712 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
713 llvm::StringRef Name = OutName.str();
714
715 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
716 if (OldGV && !OldGV->isDeclaration())
717 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
718
719 // Check if there is already an external RTTI descriptor for this type.
720 if (ShouldUseExternalRTTIDescriptor(Ty))
721 return GetAddrOfExternalRTTIDescriptor(Ty);
722
723 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(Ty);
724
725 // Add the vtable pointer.
726 BuildVtablePointer(cast<Type>(Ty));
727
728 // And the name.
729 Info.push_back(BuildName(Ty, DecideHidden(Ty), Linkage));
730
731 switch (Ty->getTypeClass()) {
732 default: assert(false && "Unhandled type class!");
733 case Type::Builtin:
734 assert(false && "Builtin type info must be in the standard library!");
735 break;
736
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000737 case Type::FunctionNoProto:
738 case Type::FunctionProto:
739 // Itanium C++ ABI 2.9.5p4:
740 // abi::__function_type_info adds no data members to std::type_info;
741 break;
742
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000743 case Type::Record: {
744 const CXXRecordDecl *RD =
745 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
746 if (!RD->getNumBases()) {
747 // We don't need to emit any fields.
748 break;
749 }
750 }
751
Anders Carlsson8d145152009-12-20 22:30:54 +0000752 case Type::Pointer:
753 BuildPointerTypeInfo(cast<PointerType>(Ty));
754 break;
755
756 case Type::MemberPointer:
757 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
758 break;
759 }
760
761 llvm::Constant *Init =
762 llvm::ConstantStruct::get(VMContext, &Info[0], Info.size(),
763 /*Packed=*/false);
764
765 llvm::GlobalVariable *GV =
766 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
767 /*Constant=*/true, Linkage, Init, Name);
768
769 // If there's already an old global variable, replace it with the new one.
770 if (OldGV) {
771 GV->takeName(OldGV);
772 llvm::Constant *NewPtr =
773 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
774 OldGV->replaceAllUsesWith(NewPtr);
775 OldGV->eraseFromParent();
776 }
777
778 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
779}
780
781/// DetermineQualifierFlags - Deterine the pointer type info flags from the
782/// given qualifier.
783static unsigned DetermineQualifierFlags(Qualifiers Quals) {
784 unsigned Flags = 0;
785
786 if (Quals.hasConst())
787 Flags |= RTTIBuilder::PTI_Const;
788 if (Quals.hasVolatile())
789 Flags |= RTTIBuilder::PTI_Volatile;
790 if (Quals.hasRestrict())
791 Flags |= RTTIBuilder::PTI_Restrict;
792
793 return Flags;
794}
795
796/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
797/// used for pointer types.
798void RTTIBuilder::BuildPointerTypeInfo(const PointerType *Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000799 QualType PointeeTy = Ty->getPointeeType();
Anders Carlsson8d145152009-12-20 22:30:54 +0000800
801 // Itanium C++ ABI 2.9.5p7:
802 // __flags is a flag word describing the cv-qualification and other
803 // attributes of the type pointed to
804 unsigned Flags = DetermineQualifierFlags(PointeeTy.getQualifiers());
805
806 // Itanium C++ ABI 2.9.5p7:
807 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
808 // incomplete class type, the incomplete target type flag is set.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000809 if (ContainsIncompleteClassType(PointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000810 Flags |= PTI_Incomplete;
811
812 const llvm::Type *UnsignedIntLTy =
813 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
814 Info.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
815
816 // Itanium C++ ABI 2.9.5p7:
817 // __pointee is a pointer to the std::type_info derivation for the
818 // unqualified type being pointed to.
819 Info.push_back(RTTIBuilder(CGM).BuildType(PointeeTy.getUnqualifiedType()));
820}
821
822/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
823/// struct, used for member pointer types.
824void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
825 QualType PointeeTy = Ty->getPointeeType();
826
827 // Itanium C++ ABI 2.9.5p7:
828 // __flags is a flag word describing the cv-qualification and other
829 // attributes of the type pointed to.
830 unsigned Flags = DetermineQualifierFlags(PointeeTy.getQualifiers());
831
832 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000833
834 // Itanium C++ ABI 2.9.5p7:
835 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
836 // incomplete class type, the incomplete target type flag is set.
837 if (ContainsIncompleteClassType(PointeeTy))
838 Flags |= PTI_Incomplete;
839
Anders Carlsson8d145152009-12-20 22:30:54 +0000840 if (IsIncompleteClassType(ClassType))
841 Flags |= PTI_ContainingClassIncomplete;
842
Anders Carlsson8d145152009-12-20 22:30:54 +0000843 const llvm::Type *UnsignedIntLTy =
844 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
845 Info.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
846
847 // Itanium C++ ABI 2.9.5p7:
848 // __pointee is a pointer to the std::type_info derivation for the
849 // unqualified type being pointed to.
850 Info.push_back(RTTIBuilder(CGM).BuildType(PointeeTy.getUnqualifiedType()));
851
852 // Itanium C++ ABI 2.9.5p9:
853 // __context is a pointer to an abi::__class_type_info corresponding to the
854 // class type containing the member pointed to
855 // (e.g., the "A" in "int A::*").
856 Info.push_back(RTTIBuilder(CGM).BuildType(QualType(ClassType, 0)));
857}
858
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000859llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000860 if (!getContext().getLangOptions().RTTI) {
861 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
862 return llvm::Constant::getNullValue(Int8PtrTy);
863 }
864
865 return RTTIBuilder(*this).BuildType(Ty);
866}