blob: 844009affbc1180277dc5dea42769950b57ce551 [file] [log] [blame]
Mike Stumpc01c2b82009-12-02 18:57:08 +00001//===--- CGCXXRTTI.cpp - Emit LLVM Code for C++ RTTI descriptors ----------===//
Anders Carlsson6ce51fd2009-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 Stump103a0842009-11-17 23:45:57 +000014#include "clang/AST/Type.h"
Mike Stumpf5b28692009-11-14 23:32:21 +000015#include "clang/AST/RecordLayout.h"
Mike Stumpdb72c892009-11-17 21:44:24 +000016#include "CodeGenModule.h"
Anders Carlsson6ce51fd2009-10-10 20:49:04 +000017using namespace clang;
18using namespace CodeGen;
19
Mike Stumpae1b85d2009-12-02 19:07:44 +000020namespace {
Mike Stumpc01c2b82009-12-02 18:57:08 +000021class RTTIBuilder {
Mike Stump14718422009-11-14 14:25:18 +000022 CodeGenModule &CGM; // Per-module state.
23 llvm::LLVMContext &VMContext;
24 const llvm::Type *Int8PtrTy;
Mike Stump4c808df2009-11-15 03:28:10 +000025 llvm::SmallSet<const CXXRecordDecl *, 16> SeenVBase;
26 llvm::SmallSet<const CXXRecordDecl *, 32> SeenBase;
Anders Carlssond0081292009-12-20 22:30:54 +000027
Anders Carlssone5a94102009-12-17 05:06:03 +000028 std::vector<llvm::Constant *> Info;
Anders Carlssonc9882012009-12-11 01:27:37 +000029
Anders Carlsson3f4336c2009-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 Carlssond0081292009-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 Stump14718422009-11-14 14:25:18 +000048public:
Mike Stumpc01c2b82009-12-02 18:57:08 +000049 RTTIBuilder(CodeGenModule &cgm)
Mike Stump14718422009-11-14 14:25:18 +000050 : CGM(cgm), VMContext(cgm.getModule().getContext()),
51 Int8PtrTy(llvm::Type::getInt8PtrTy(VMContext)) { }
52
Mike Stumpf5b28692009-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 Carlssond0081292009-12-20 22:30:54 +000056 llvm::Constant *GV = CGM.getModule().getNamedGlobal(Name);
Mike Stump1acec6a2009-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 Stumpf5b28692009-11-14 23:32:21 +000065 }
Mike Stump1acec6a2009-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 Stumpf5b28692009-11-14 23:32:21 +000071
Anders Carlssonfd7dfeb2009-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 Stump1a139f82009-11-19 01:08:19 +000084 llvm::Constant *BuildName(QualType Ty, bool Hidden, bool Extern) {
Anders Carlssonfd7dfeb2009-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 Stump14718422009-11-14 14:25:18 +000090 llvm::SmallString<256> OutName;
Mike Stumpc01c2b82009-12-02 18:57:08 +000091 CGM.getMangleContext().mangleCXXRTTIName(Ty, OutName);
Daniel Dunbare128dd12009-11-21 09:06:22 +000092 llvm::StringRef Name = OutName.str();
Mike Stumpf5b28692009-11-14 23:32:21 +000093
Anders Carlssond0081292009-12-20 22:30:54 +000094 llvm::GlobalVariable *OGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +000095 if (OGV && !OGV->isDeclaration())
96 return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy);
Mike Stump1a139f82009-11-19 01:08:19 +000097
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +000098 llvm::Constant *C = llvm::ConstantArray::get(VMContext, Name.substr(4));
Mike Stump14718422009-11-14 14:25:18 +000099
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000100 llvm::GlobalVariable *GV =
101 new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
102 C, Name);
Mike Stump1a139f82009-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 Stump83d5e002009-11-18 03:46:51 +0000110 if (Hidden)
111 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
112 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Daniel Dunbare017ecc2009-12-19 17:50:07 +0000113 }
Mike Stump1acec6a2009-11-14 15:55:18 +0000114
Mike Stumpf5b28692009-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 Stump4c808df2009-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 Carlssond0081292009-12-20 22:30:54 +0000129 int CalculateFlags(const CXXRecordDecl *RD) {
Mike Stump4c808df2009-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 Carlsson1fd73422009-12-17 05:10:59 +0000167 llvm::Constant *finish(llvm::GlobalVariable *GV,
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000168 llvm::StringRef Name, bool Hidden,
169 llvm::GlobalVariable::LinkageTypes Linkage) {
170 llvm::Constant *C =
Anders Carlsson1fd73422009-12-17 05:10:59 +0000171 llvm::ConstantStruct::get(VMContext, &Info[0], Info.size(),
172 /*Packed=*/false);
Mike Stump96b96d52009-11-17 23:11:22 +0000173
Mike Stump1a139f82009-11-19 01:08:19 +0000174 llvm::GlobalVariable *OGV = GV;
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000175 GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
Mike Stump1a139f82009-11-19 01:08:19 +0000176 C, Name);
177 if (OGV) {
Mike Stump96b96d52009-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 Stump83d5e002009-11-18 03:46:51 +0000184 if (Hidden)
Mike Stumpc5d2ed72009-11-18 03:21:29 +0000185 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
Mike Stump96b96d52009-11-17 23:11:22 +0000186 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
187 }
188
189
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000190 llvm::Constant *
191 Buildclass_type_info(const CXXRecordDecl *RD,
192 llvm::GlobalVariable::LinkageTypes Linkage) {
Anders Carlssone5a94102009-12-17 05:06:03 +0000193 assert(Info.empty() && "Info vector must be empty!");
Anders Carlsson1d6ad502009-12-11 16:41:51 +0000194
Mike Stump96b96d52009-11-17 23:11:22 +0000195 llvm::Constant *C;
196
Mike Stumpf5b28692009-11-14 23:32:21 +0000197 llvm::SmallString<256> OutName;
Mike Stumpc01c2b82009-12-02 18:57:08 +0000198 CGM.getMangleContext().mangleCXXRTTI(CGM.getContext().getTagDeclType(RD),
Daniel Dunbare128dd12009-11-21 09:06:22 +0000199 OutName);
200 llvm::StringRef Name = OutName.str();
Mike Stumpf5b28692009-11-14 23:32:21 +0000201
202 llvm::GlobalVariable *GV;
Anders Carlssond0081292009-12-20 22:30:54 +0000203 GV = CGM.getModule().getNamedGlobal(Name);
Mike Stumpf5b28692009-11-14 23:32:21 +0000204 if (GV && !GV->isDeclaration())
205 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
206
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000207 // If we're in an anonymous namespace, then we always want internal linkage.
Eli Friedman3ace52b2009-12-11 20:48:18 +0000208 if (RD->isInAnonymousNamespace() || !RD->hasLinkage())
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000209 Linkage = llvm::GlobalVariable::InternalLinkage;
210
Mike Stump83d5e002009-11-18 03:46:51 +0000211 bool Hidden = CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden;
212
Mike Stump4c808df2009-11-15 03:28:10 +0000213 bool simple = false;
Mike Stumpf5b28692009-11-14 23:32:21 +0000214 if (RD->getNumBases() == 0)
215 C = BuildVtableRef("_ZTVN10__cxxabiv117__class_type_infoE");
Mike Stump4c808df2009-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 Stumpf5b28692009-11-14 23:32:21 +0000220 C = BuildVtableRef("_ZTVN10__cxxabiv121__vmi_class_type_infoE");
Anders Carlssone5a94102009-12-17 05:06:03 +0000221 Info.push_back(C);
222 Info.push_back(BuildName(CGM.getContext().getTagDeclType(RD), Hidden,
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000223 Linkage));
Mike Stump1acec6a2009-11-14 15:55:18 +0000224
Mike Stumpf5b28692009-11-14 23:32:21 +0000225 // If we have no bases, there are no more fields.
226 if (RD->getNumBases()) {
Mike Stump4c808df2009-11-15 03:28:10 +0000227 if (!simple) {
Anders Carlssone5a94102009-12-17 05:06:03 +0000228 Info.push_back(BuildFlags(CalculateFlags(RD)));
229 Info.push_back(BuildBaseCount(RD->getNumBases()));
Mike Stump4c808df2009-11-15 03:28:10 +0000230 }
Mike Stumpf5b28692009-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 Carlsson3f4336c2009-12-17 07:09:17 +0000235 QualType BaseType = i->getType();
Mike Stumpf5b28692009-11-14 23:32:21 +0000236 const CXXRecordDecl *Base =
Anders Carlsson3f4336c2009-12-17 07:09:17 +0000237 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
238 Info.push_back(CGM.GetAddrOfRTTIDescriptor(BaseType));
Mike Stump4c808df2009-11-15 03:28:10 +0000239 if (simple)
240 break;
Mike Stumpf5b28692009-11-14 23:32:21 +0000241 int64_t offset;
242 if (!i->isVirtual())
Mike Stump4c808df2009-11-15 03:28:10 +0000243 offset = Layout.getBaseClassOffset(Base)/8;
Mike Stumpf5b28692009-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 Carlssone5a94102009-12-17 05:06:03 +0000253 Info.push_back(C);
Mike Stumpf5b28692009-11-14 23:32:21 +0000254 }
255 }
256
Anders Carlsson1fd73422009-12-17 05:10:59 +0000257 return finish(GV, Name, Hidden, Linkage);
Mike Stump1acec6a2009-11-14 15:55:18 +0000258 }
Mike Stump3f75d552009-11-17 02:16:21 +0000259
Mike Stumpdb72c892009-11-17 21:44:24 +0000260 /// - BuildFlags - Build a __flags value for __pbase_type_info.
Anders Carlssonc9882012009-12-11 01:27:37 +0000261 llvm::Constant *BuildInt(unsigned n) {
262 return llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), n);
Mike Stumpdb72c892009-11-17 21:44:24 +0000263 }
264
Mike Stump1a139f82009-11-19 01:08:19 +0000265 bool DecideExtern(QualType Ty) {
266 // For this type, see if all components are never in an anonymous namespace.
267 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>())
268 return (DecideExtern(MPT->getPointeeType())
269 && DecideExtern(QualType(MPT->getClass(), 0)));
270 if (const PointerType *PT = Ty->getAs<PointerType>())
271 return DecideExtern(PT->getPointeeType());
272 if (const RecordType *RT = Ty->getAs<RecordType>())
273 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
Eli Friedman3ace52b2009-12-11 20:48:18 +0000274 return !RD->isInAnonymousNamespace() && RD->hasLinkage();
Mike Stump1a139f82009-11-19 01:08:19 +0000275 return true;
276 }
277
278 bool DecideHidden(QualType Ty) {
279 // For this type, see if all components are never hidden.
280 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>())
281 return (DecideHidden(MPT->getPointeeType())
282 && DecideHidden(QualType(MPT->getClass(), 0)));
283 if (const PointerType *PT = Ty->getAs<PointerType>())
284 return DecideHidden(PT->getPointeeType());
285 if (const RecordType *RT = Ty->getAs<RecordType>())
286 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
287 return CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden;
288 return false;
289 }
290
Mike Stump103a0842009-11-17 23:45:57 +0000291 llvm::Constant *BuildSimpleType(QualType Ty, const char *vtbl) {
Mike Stump96b96d52009-11-17 23:11:22 +0000292 llvm::SmallString<256> OutName;
Mike Stumpc01c2b82009-12-02 18:57:08 +0000293 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
Daniel Dunbare128dd12009-11-21 09:06:22 +0000294 llvm::StringRef Name = OutName.str();
Mike Stump96b96d52009-11-17 23:11:22 +0000295
296 llvm::GlobalVariable *GV;
Anders Carlssond0081292009-12-20 22:30:54 +0000297 GV = CGM.getModule().getNamedGlobal(Name);
Mike Stump96b96d52009-11-17 23:11:22 +0000298 if (GV && !GV->isDeclaration())
299 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
300
Mike Stump1a139f82009-11-19 01:08:19 +0000301 bool Extern = DecideExtern(Ty);
302 bool Hidden = DecideHidden(Ty);
Mike Stump83d5e002009-11-18 03:46:51 +0000303
Anders Carlsson1fd73422009-12-17 05:10:59 +0000304 Info.push_back(BuildVtableRef(vtbl));
305 Info.push_back(BuildName(Ty, Hidden, Extern));
Anders Carlssonc67974c2009-12-13 23:47:29 +0000306
Mike Stump1a139f82009-11-19 01:08:19 +0000307 // We always generate these as hidden, only the name isn't hidden.
Anders Carlsson1fd73422009-12-17 05:10:59 +0000308 return finish(GV, Name, /*Hidden=*/true,
309 GetLinkageFromExternFlag(Extern));
Mike Stumpdb72c892009-11-17 21:44:24 +0000310 }
311
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000312 /// BuildType - Builds the type info for the given type.
Mike Stump3f75d552009-11-17 02:16:21 +0000313 llvm::Constant *BuildType(QualType Ty) {
314 const clang::Type &Type
315 = *CGM.getContext().getCanonicalType(Ty).getTypePtr();
Mike Stump101f0522009-11-20 00:31:50 +0000316
317 if (const RecordType *RT = Ty.getTypePtr()->getAs<RecordType>())
318 if (const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()))
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000319 return BuildClassTypeInfo(RD);
Mike Stump101f0522009-11-20 00:31:50 +0000320
Mike Stump3f75d552009-11-17 02:16:21 +0000321 switch (Type.getTypeClass()) {
322 default: {
Mike Stump3f75d552009-11-17 02:16:21 +0000323 assert(0 && "typeid expression");
Mike Stump3f75d552009-11-17 02:16:21 +0000324 return llvm::Constant::getNullValue(Int8PtrTy);
325 }
326
327 case Type::Builtin: {
328 // We expect all type_info objects for builtin types to be in the library.
Anders Carlsson3f4336c2009-12-17 07:09:17 +0000329 return GetAddrOfExternalRTTIDescriptor(Ty);
Mike Stump3f75d552009-11-17 02:16:21 +0000330 }
Mike Stump8f5e6772009-11-17 02:57:13 +0000331
Anders Carlssond0081292009-12-20 22:30:54 +0000332 case Type::Pointer:
Mike Stump6fdfea62009-11-17 22:33:00 +0000333 case Type::MemberPointer:
Anders Carlssond0081292009-12-20 22:30:54 +0000334
335 return BuildTypeInfo(Ty);
Mike Stump96b96d52009-11-17 23:11:22 +0000336 case Type::FunctionProto:
Mike Stump103a0842009-11-17 23:45:57 +0000337 case Type::FunctionNoProto:
338 return BuildSimpleType(Ty, "_ZTVN10__cxxabiv120__function_type_infoE");
339 case Type::ConstantArray:
340 case Type::IncompleteArray:
341 case Type::VariableArray:
342 case Type::Vector:
343 case Type::ExtVector:
344 return BuildSimpleType(Ty, "_ZTVN10__cxxabiv117__array_type_infoE");
345 case Type::Enum:
346 return BuildSimpleType(Ty, "_ZTVN10__cxxabiv116__enum_type_infoE");
Mike Stump3f75d552009-11-17 02:16:21 +0000347 }
348 }
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000349
350 /// BuildClassTypeInfo - Builds the class type info (or a reference to it)
351 /// for the given record decl.
352 llvm::Constant *BuildClassTypeInfo(const CXXRecordDecl *RD) {
353 const CXXMethodDecl *KeyFunction = 0;
354
355 if (RD->isDynamicClass())
356 KeyFunction = CGM.getContext().getKeyFunction(RD);
357
358 if (KeyFunction) {
359 // If the key function is defined in this translation unit, then the RTTI
360 // related constants should also be emitted here, with external linkage.
361 if (KeyFunction->getBody())
362 return Buildclass_type_info(RD, llvm::GlobalValue::ExternalLinkage);
363
364 // Otherwise, we just want a reference to the type info.
Anders Carlsson3f4336c2009-12-17 07:09:17 +0000365 QualType Ty = CGM.getContext().getTagDeclType(RD);
366 return GetAddrOfExternalRTTIDescriptor(Ty);
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000367 }
368
369 // If there is no key function (or if the record doesn't have any virtual
370 // member functions or virtual bases), emit the type info with weak_odr
371 // linkage.
372 return Buildclass_type_info(RD, llvm::GlobalValue::WeakODRLinkage);
373 }
Anders Carlssond0081292009-12-20 22:30:54 +0000374
375 // Pointer type info flags.
376 enum {
377 /// PTI_Const - Type has const qualifier.
378 PTI_Const = 0x1,
379
380 /// PTI_Volatile - Type has volatile qualifier.
381 PTI_Volatile = 0x2,
382
383 /// PTI_Restrict - Type has restrict qualifier.
384 PTI_Restrict = 0x4,
385
386 /// PTI_Incomplete - Type is incomplete.
387 PTI_Incomplete = 0x8,
388
389 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
390 /// (in pointer to member).
391 PTI_ContainingClassIncomplete = 0x10
392 };
Mike Stump14718422009-11-14 14:25:18 +0000393};
Mike Stumpae1b85d2009-12-02 19:07:44 +0000394}
Mike Stump14718422009-11-14 14:25:18 +0000395
Anders Carlsson3f4336c2009-12-17 07:09:17 +0000396llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
397 // Mangle the RTTI name.
398 llvm::SmallString<256> OutName;
399 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
400 llvm::StringRef Name = OutName.str();
401
Anders Carlssond0081292009-12-20 22:30:54 +0000402 // Look for an existing global.
403 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson3f4336c2009-12-17 07:09:17 +0000404
405 if (!GV) {
406 // Create a new global variable.
407 GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
408 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000409 }
410
Anders Carlsson3f4336c2009-12-17 07:09:17 +0000411 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000412}
413
Anders Carlssond0081292009-12-20 22:30:54 +0000414/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
415/// info for that type is defined in the standard library.
416static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
417 // Itanium C++ ABI 2.9.2:
418 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
419 // the run-time support library. Specifically, the run-time support
420 // library should contain type_info objects for the types X, X* and
421 // X const*, for every X in: void, bool, wchar_t, char, unsigned char,
422 // signed char, short, unsigned short, int, unsigned int, long,
423 // unsigned long, long long, unsigned long long, float, double, long double,
424 // char16_t, char32_t, and the IEEE 754r decimal and half-precision
425 // floating point types.
426 switch (Ty->getKind()) {
427 case BuiltinType::Void:
428 case BuiltinType::Bool:
429 case BuiltinType::WChar:
430 case BuiltinType::Char_U:
431 case BuiltinType::Char_S:
432 case BuiltinType::UChar:
433 case BuiltinType::SChar:
434 case BuiltinType::Short:
435 case BuiltinType::UShort:
436 case BuiltinType::Int:
437 case BuiltinType::UInt:
438 case BuiltinType::Long:
439 case BuiltinType::ULong:
440 case BuiltinType::LongLong:
441 case BuiltinType::ULongLong:
442 case BuiltinType::Float:
443 case BuiltinType::Double:
444 case BuiltinType::LongDouble:
445 case BuiltinType::Char16:
446 case BuiltinType::Char32:
447 case BuiltinType::Int128:
448 case BuiltinType::UInt128:
449 return true;
450
451 case BuiltinType::Overload:
452 case BuiltinType::Dependent:
453 case BuiltinType::UndeducedAuto:
454 assert(false && "Should not see this type here!");
455
456 case BuiltinType::NullPtr:
457 assert(false && "FIXME: nullptr_t is not handled!");
458
459 case BuiltinType::ObjCId:
460 case BuiltinType::ObjCClass:
461 case BuiltinType::ObjCSel:
462 assert(false && "FIXME: Objective-C types are unsupported!");
463 }
464
465 // Silent gcc.
466 return false;
467}
468
469static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
470 QualType PointeeTy = PointerTy->getPointeeType();
471 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
472 if (!BuiltinTy)
473 return false;
474
475 // Check the qualifiers.
476 Qualifiers Quals = PointeeTy.getQualifiers();
477 Quals.removeConst();
478
479 if (!Quals.empty())
480 return false;
481
482 return TypeInfoIsInStandardLibrary(BuiltinTy);
483}
484
485/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
486/// the given type exists somewhere else, and that we should not emit the typ
487/// information in this translation unit.
488bool ShouldUseExternalRTTIDescriptor(QualType Ty) {
489 // Type info for builtin types is defined in the standard library.
490 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
491 return TypeInfoIsInStandardLibrary(BuiltinTy);
492
493 // Type info for some pointer types to builtin types is defined in the
494 // standard library.
495 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
496 return TypeInfoIsInStandardLibrary(PointerTy);
497
498 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
499 (void)RecordTy;
500 assert(false && "FIXME");
501 }
502
503 return false;
504}
505
506/// IsIncompleteClassType - Returns whether the given record type is incomplete.
507static bool IsIncompleteClassType(const RecordType *RecordTy) {
508 return !RecordTy->getDecl()->isDefinition();
509}
510
511/// IsPointerToIncompleteClassType - Returns whether the given pointer type
512/// is an indirect or direct pointer to an incomplete class type.
513static bool IsPointerToIncompleteClassType(const PointerType *PointerTy) {
514 QualType PointeeTy = PointerTy->getPointeeType();
515 while ((PointerTy = dyn_cast<PointerType>(PointeeTy)))
516 PointeeTy = PointerTy->getPointeeType();
517
518 if (const RecordType *RecordTy = dyn_cast<RecordType>(PointeeTy)) {
519 // Check if the record type is incomplete.
520 return IsIncompleteClassType(RecordTy);
521 }
522
523 return false;
524}
525
526/// getTypeInfoLinkage - Return the linkage that the type info and type info
527/// name constants should have for the given type.
528static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(QualType Ty) {
529 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) {
530 // Itanium C++ ABI 2.9.5p7:
531 // In addition, it and all of the intermediate abi::__pointer_type_info
532 // structs in the chain down to the abi::__class_type_info for the
533 // incomplete class type must be prevented from resolving to the
534 // corresponding type_info structs for the complete class type, possibly
535 // by making them local static objects. Finally, a dummy class RTTI is
536 // generated for the incomplete type that will not resolve to the final
537 // complete class RTTI (because the latter need not exist), possibly by
538 // making it a local static object.
539 if (IsPointerToIncompleteClassType(PointerTy))
540 return llvm::GlobalValue::InternalLinkage;
541
542 // FIXME: Check linkage and anonymous namespace.
543 return llvm::GlobalValue::WeakODRLinkage;
544 } else if (const MemberPointerType *MemberPointerTy =
545 dyn_cast<MemberPointerType>(Ty)) {
546 // If the class type is incomplete, then the type info constants should
547 // have internal linkage.
548 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
549 if (!ClassType->getDecl()->isDefinition())
550 return llvm::GlobalValue::InternalLinkage;
551
552 // FIXME: Check linkage and anonymous namespace.
553 return llvm::GlobalValue::WeakODRLinkage;
554 }
555
556 assert(false && "FIXME!");
557 return llvm::GlobalValue::WeakODRLinkage;
558}
559
560void RTTIBuilder::BuildVtablePointer(const Type *Ty) {
561 const char *VtableName;
562
563 switch (Ty->getTypeClass()) {
564 default: assert(0 && "Unhandled type!");
565 case Type::Pointer:
566 // abi::__pointer_type_info
567 VtableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
568 break;
569 case Type::MemberPointer:
570 // abi::__pointer_to_member_type_info
571 VtableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
572 break;
573 }
574
575 llvm::Constant *Vtable =
576 CGM.getModule().getOrInsertGlobal(VtableName, Int8PtrTy);
577
578 const llvm::Type *PtrDiffTy =
579 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
580
581 // The vtable address point is 2.
582 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
583 Vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(Vtable, &Two, 1);
584 Vtable = llvm::ConstantExpr::getBitCast(Vtable, Int8PtrTy);
585
586 Info.push_back(Vtable);
587}
588
589llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty) {
590 // We want to operate on the canonical type.
591 Ty = CGM.getContext().getCanonicalType(Ty);
592
593 // Check if we've already emitted an RTTI descriptor for this type.
594 llvm::SmallString<256> OutName;
595 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
596 llvm::StringRef Name = OutName.str();
597
598 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
599 if (OldGV && !OldGV->isDeclaration())
600 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
601
602 // Check if there is already an external RTTI descriptor for this type.
603 if (ShouldUseExternalRTTIDescriptor(Ty))
604 return GetAddrOfExternalRTTIDescriptor(Ty);
605
606 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(Ty);
607
608 // Add the vtable pointer.
609 BuildVtablePointer(cast<Type>(Ty));
610
611 // And the name.
612 Info.push_back(BuildName(Ty, DecideHidden(Ty), Linkage));
613
614 switch (Ty->getTypeClass()) {
615 default: assert(false && "Unhandled type class!");
616 case Type::Builtin:
617 assert(false && "Builtin type info must be in the standard library!");
618 break;
619
620 case Type::Pointer:
621 BuildPointerTypeInfo(cast<PointerType>(Ty));
622 break;
623
624 case Type::MemberPointer:
625 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
626 break;
627 }
628
629 llvm::Constant *Init =
630 llvm::ConstantStruct::get(VMContext, &Info[0], Info.size(),
631 /*Packed=*/false);
632
633 llvm::GlobalVariable *GV =
634 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
635 /*Constant=*/true, Linkage, Init, Name);
636
637 // If there's already an old global variable, replace it with the new one.
638 if (OldGV) {
639 GV->takeName(OldGV);
640 llvm::Constant *NewPtr =
641 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
642 OldGV->replaceAllUsesWith(NewPtr);
643 OldGV->eraseFromParent();
644 }
645
646 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
647}
648
649/// DetermineQualifierFlags - Deterine the pointer type info flags from the
650/// given qualifier.
651static unsigned DetermineQualifierFlags(Qualifiers Quals) {
652 unsigned Flags = 0;
653
654 if (Quals.hasConst())
655 Flags |= RTTIBuilder::PTI_Const;
656 if (Quals.hasVolatile())
657 Flags |= RTTIBuilder::PTI_Volatile;
658 if (Quals.hasRestrict())
659 Flags |= RTTIBuilder::PTI_Restrict;
660
661 return Flags;
662}
663
664/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
665/// used for pointer types.
666void RTTIBuilder::BuildPointerTypeInfo(const PointerType *Ty) {
667 const PointerType *PointerTy = cast<PointerType>(Ty);
668 QualType PointeeTy = PointerTy->getPointeeType();
669
670 // Itanium C++ ABI 2.9.5p7:
671 // __flags is a flag word describing the cv-qualification and other
672 // attributes of the type pointed to
673 unsigned Flags = DetermineQualifierFlags(PointeeTy.getQualifiers());
674
675 // Itanium C++ ABI 2.9.5p7:
676 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
677 // incomplete class type, the incomplete target type flag is set.
678 if (IsPointerToIncompleteClassType(PointerTy))
679 Flags |= PTI_Incomplete;
680
681 const llvm::Type *UnsignedIntLTy =
682 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
683 Info.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
684
685 // Itanium C++ ABI 2.9.5p7:
686 // __pointee is a pointer to the std::type_info derivation for the
687 // unqualified type being pointed to.
688 Info.push_back(RTTIBuilder(CGM).BuildType(PointeeTy.getUnqualifiedType()));
689}
690
691/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
692/// struct, used for member pointer types.
693void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
694 QualType PointeeTy = Ty->getPointeeType();
695
696 // Itanium C++ ABI 2.9.5p7:
697 // __flags is a flag word describing the cv-qualification and other
698 // attributes of the type pointed to.
699 unsigned Flags = DetermineQualifierFlags(PointeeTy.getQualifiers());
700
701 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
702
703 if (IsIncompleteClassType(ClassType))
704 Flags |= PTI_ContainingClassIncomplete;
705
706 // FIXME: Handle PTI_Incomplete.
707
708 const llvm::Type *UnsignedIntLTy =
709 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
710 Info.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
711
712 // Itanium C++ ABI 2.9.5p7:
713 // __pointee is a pointer to the std::type_info derivation for the
714 // unqualified type being pointed to.
715 Info.push_back(RTTIBuilder(CGM).BuildType(PointeeTy.getUnqualifiedType()));
716
717 // Itanium C++ ABI 2.9.5p9:
718 // __context is a pointer to an abi::__class_type_info corresponding to the
719 // class type containing the member pointed to
720 // (e.g., the "A" in "int A::*").
721 Info.push_back(RTTIBuilder(CGM).BuildType(QualType(ClassType, 0)));
722}
723
Anders Carlsson3f4336c2009-12-17 07:09:17 +0000724llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty) {
Anders Carlssonfd7dfeb2009-12-11 02:46:30 +0000725 if (!getContext().getLangOptions().RTTI) {
726 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
727 return llvm::Constant::getNullValue(Int8PtrTy);
728 }
729
730 return RTTIBuilder(*this).BuildType(Ty);
731}