blob: e2410898453843a0c446cce8cb21054bbb73c1f0 [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 Stump58588942009-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 Friedman470fb732009-12-11 20:48:18 +0000274 return !RD->isInAnonymousNamespace() && RD->hasLinkage();
Mike Stump58588942009-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 Stumpae9b2be2009-11-17 23:45:57 +0000291 llvm::Constant *BuildSimpleType(QualType Ty, const char *vtbl) {
Mike Stump64989f02009-11-17 23:11:22 +0000292 llvm::SmallString<256> OutName;
Mike Stumpde050572009-12-02 18:57:08 +0000293 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +0000294 llvm::StringRef Name = OutName.str();
Mike Stump64989f02009-11-17 23:11:22 +0000295
296 llvm::GlobalVariable *GV;
Anders Carlsson8d145152009-12-20 22:30:54 +0000297 GV = CGM.getModule().getNamedGlobal(Name);
Mike Stump64989f02009-11-17 23:11:22 +0000298 if (GV && !GV->isDeclaration())
299 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
300
Mike Stump58588942009-11-19 01:08:19 +0000301 bool Extern = DecideExtern(Ty);
302 bool Hidden = DecideHidden(Ty);
Mike Stump582b0372009-11-18 03:46:51 +0000303
Anders Carlssonab6faf32009-12-17 05:10:59 +0000304 Info.push_back(BuildVtableRef(vtbl));
305 Info.push_back(BuildName(Ty, Hidden, Extern));
Anders Carlsson3bf190e2009-12-13 23:47:29 +0000306
Mike Stump58588942009-11-19 01:08:19 +0000307 // We always generate these as hidden, only the name isn't hidden.
Anders Carlssonab6faf32009-12-17 05:10:59 +0000308 return finish(GV, Name, /*Hidden=*/true,
309 GetLinkageFromExternFlag(Extern));
Mike Stump61c38012009-11-17 21:44:24 +0000310 }
311
Anders Carlsson31b7f522009-12-11 02:46:30 +0000312 /// BuildType - Builds the type info for the given type.
Mike Stumpea2c0b52009-11-17 02:16:21 +0000313 llvm::Constant *BuildType(QualType Ty) {
314 const clang::Type &Type
315 = *CGM.getContext().getCanonicalType(Ty).getTypePtr();
Mike Stump7e1365a2009-11-20 00:31:50 +0000316
Mike Stumpea2c0b52009-11-17 02:16:21 +0000317 switch (Type.getTypeClass()) {
318 default: {
Mike Stumpea2c0b52009-11-17 02:16:21 +0000319 assert(0 && "typeid expression");
Mike Stumpea2c0b52009-11-17 02:16:21 +0000320 return llvm::Constant::getNullValue(Int8PtrTy);
321 }
322
323 case Type::Builtin: {
324 // We expect all type_info objects for builtin types to be in the library.
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000325 return GetAddrOfExternalRTTIDescriptor(Ty);
Mike Stumpea2c0b52009-11-17 02:16:21 +0000326 }
Mike Stump21f5d5d2009-11-17 02:57:13 +0000327
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000328 case Type::Record: {
329 const RecordType *RT = cast<RecordType>(&Type);
330
331 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
332 if (RD->getNumBases())
333 return BuildClassTypeInfo(RD);
334
335 // Fall through.
336 }
337
Anders Carlsson8d145152009-12-20 22:30:54 +0000338 case Type::Pointer:
Mike Stump5fae8562009-11-17 22:33:00 +0000339 case Type::MemberPointer:
Anders Carlsson8d145152009-12-20 22:30:54 +0000340 return BuildTypeInfo(Ty);
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000341
Mike Stump64989f02009-11-17 23:11:22 +0000342 case Type::FunctionProto:
Mike Stumpae9b2be2009-11-17 23:45:57 +0000343 case Type::FunctionNoProto:
344 return BuildSimpleType(Ty, "_ZTVN10__cxxabiv120__function_type_infoE");
345 case Type::ConstantArray:
346 case Type::IncompleteArray:
347 case Type::VariableArray:
348 case Type::Vector:
349 case Type::ExtVector:
350 return BuildSimpleType(Ty, "_ZTVN10__cxxabiv117__array_type_infoE");
351 case Type::Enum:
352 return BuildSimpleType(Ty, "_ZTVN10__cxxabiv116__enum_type_infoE");
Mike Stumpea2c0b52009-11-17 02:16:21 +0000353 }
354 }
Anders Carlsson31b7f522009-12-11 02:46:30 +0000355
356 /// BuildClassTypeInfo - Builds the class type info (or a reference to it)
357 /// for the given record decl.
358 llvm::Constant *BuildClassTypeInfo(const CXXRecordDecl *RD) {
359 const CXXMethodDecl *KeyFunction = 0;
360
361 if (RD->isDynamicClass())
362 KeyFunction = CGM.getContext().getKeyFunction(RD);
363
364 if (KeyFunction) {
365 // If the key function is defined in this translation unit, then the RTTI
366 // related constants should also be emitted here, with external linkage.
367 if (KeyFunction->getBody())
368 return Buildclass_type_info(RD, llvm::GlobalValue::ExternalLinkage);
369
370 // Otherwise, we just want a reference to the type info.
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000371 QualType Ty = CGM.getContext().getTagDeclType(RD);
372 return GetAddrOfExternalRTTIDescriptor(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000373 }
374
375 // If there is no key function (or if the record doesn't have any virtual
376 // member functions or virtual bases), emit the type info with weak_odr
377 // linkage.
378 return Buildclass_type_info(RD, llvm::GlobalValue::WeakODRLinkage);
379 }
Anders Carlsson8d145152009-12-20 22:30:54 +0000380
381 // Pointer type info flags.
382 enum {
383 /// PTI_Const - Type has const qualifier.
384 PTI_Const = 0x1,
385
386 /// PTI_Volatile - Type has volatile qualifier.
387 PTI_Volatile = 0x2,
388
389 /// PTI_Restrict - Type has restrict qualifier.
390 PTI_Restrict = 0x4,
391
392 /// PTI_Incomplete - Type is incomplete.
393 PTI_Incomplete = 0x8,
394
395 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
396 /// (in pointer to member).
397 PTI_ContainingClassIncomplete = 0x10
398 };
Mike Stump2b1bf312009-11-14 14:25:18 +0000399};
Mike Stump92f2fe22009-12-02 19:07:44 +0000400}
Mike Stump2b1bf312009-11-14 14:25:18 +0000401
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000402llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
403 // Mangle the RTTI name.
404 llvm::SmallString<256> OutName;
405 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
406 llvm::StringRef Name = OutName.str();
407
Anders Carlsson8d145152009-12-20 22:30:54 +0000408 // Look for an existing global.
409 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000410
411 if (!GV) {
412 // Create a new global variable.
413 GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
414 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000415 }
416
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000417 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000418}
419
Anders Carlsson8d145152009-12-20 22:30:54 +0000420/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
421/// info for that type is defined in the standard library.
422static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
423 // Itanium C++ ABI 2.9.2:
424 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
425 // the run-time support library. Specifically, the run-time support
426 // library should contain type_info objects for the types X, X* and
427 // X const*, for every X in: void, bool, wchar_t, char, unsigned char,
428 // signed char, short, unsigned short, int, unsigned int, long,
429 // unsigned long, long long, unsigned long long, float, double, long double,
430 // char16_t, char32_t, and the IEEE 754r decimal and half-precision
431 // floating point types.
432 switch (Ty->getKind()) {
433 case BuiltinType::Void:
434 case BuiltinType::Bool:
435 case BuiltinType::WChar:
436 case BuiltinType::Char_U:
437 case BuiltinType::Char_S:
438 case BuiltinType::UChar:
439 case BuiltinType::SChar:
440 case BuiltinType::Short:
441 case BuiltinType::UShort:
442 case BuiltinType::Int:
443 case BuiltinType::UInt:
444 case BuiltinType::Long:
445 case BuiltinType::ULong:
446 case BuiltinType::LongLong:
447 case BuiltinType::ULongLong:
448 case BuiltinType::Float:
449 case BuiltinType::Double:
450 case BuiltinType::LongDouble:
451 case BuiltinType::Char16:
452 case BuiltinType::Char32:
453 case BuiltinType::Int128:
454 case BuiltinType::UInt128:
455 return true;
456
457 case BuiltinType::Overload:
458 case BuiltinType::Dependent:
459 case BuiltinType::UndeducedAuto:
460 assert(false && "Should not see this type here!");
461
462 case BuiltinType::NullPtr:
463 assert(false && "FIXME: nullptr_t is not handled!");
464
465 case BuiltinType::ObjCId:
466 case BuiltinType::ObjCClass:
467 case BuiltinType::ObjCSel:
468 assert(false && "FIXME: Objective-C types are unsupported!");
469 }
470
471 // Silent gcc.
472 return false;
473}
474
475static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
476 QualType PointeeTy = PointerTy->getPointeeType();
477 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
478 if (!BuiltinTy)
479 return false;
480
481 // Check the qualifiers.
482 Qualifiers Quals = PointeeTy.getQualifiers();
483 Quals.removeConst();
484
485 if (!Quals.empty())
486 return false;
487
488 return TypeInfoIsInStandardLibrary(BuiltinTy);
489}
490
491/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
492/// the given type exists somewhere else, and that we should not emit the typ
493/// information in this translation unit.
494bool ShouldUseExternalRTTIDescriptor(QualType Ty) {
495 // Type info for builtin types is defined in the standard library.
496 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
497 return TypeInfoIsInStandardLibrary(BuiltinTy);
498
499 // Type info for some pointer types to builtin types is defined in the
500 // standard library.
501 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
502 return TypeInfoIsInStandardLibrary(PointerTy);
503
504 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000505 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
506 if (!RD->isDynamicClass())
507 return false;
508
509 // Get the key function.
510 const CXXMethodDecl *KeyFunction = RD->getASTContext().getKeyFunction(RD);
511 if (KeyFunction && !KeyFunction->getBody()) {
512 // The class has a key function, but it is not defined in this translation
513 // unit, so we should use the external descriptor for it.
514 return true;
515 }
Anders Carlsson8d145152009-12-20 22:30:54 +0000516 }
517
518 return false;
519}
520
521/// IsIncompleteClassType - Returns whether the given record type is incomplete.
522static bool IsIncompleteClassType(const RecordType *RecordTy) {
523 return !RecordTy->getDecl()->isDefinition();
524}
525
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000526/// ContainsIncompleteClassType - Returns whether the given type contains an
527/// incomplete class type. This is true if
528///
529/// * The given type is an incomplete class type.
530/// * The given type is a pointer type whose pointee type contains an
531/// incomplete class type.
532/// * The given type is a member pointer type whose class is an incomplete
533/// class type.
534/// * The given type is a member pointer type whoise pointee type contains an
535/// incomplete class type.
Anders Carlsson8d145152009-12-20 22:30:54 +0000536/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000537static bool ContainsIncompleteClassType(QualType Ty) {
538 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
539 if (IsIncompleteClassType(RecordTy))
540 return true;
541 }
542
543 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
544 return ContainsIncompleteClassType(PointerTy->getPointeeType());
545
546 if (const MemberPointerType *MemberPointerTy =
547 dyn_cast<MemberPointerType>(Ty)) {
548 // Check if the class type is incomplete.
549 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
550 if (IsIncompleteClassType(ClassType))
551 return true;
552
553 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000554 }
555
556 return false;
557}
558
559/// getTypeInfoLinkage - Return the linkage that the type info and type info
560/// name constants should have for the given type.
561static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(QualType Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000562 // Itanium C++ ABI 2.9.5p7:
563 // In addition, it and all of the intermediate abi::__pointer_type_info
564 // structs in the chain down to the abi::__class_type_info for the
565 // incomplete class type must be prevented from resolving to the
566 // corresponding type_info structs for the complete class type, possibly
567 // by making them local static objects. Finally, a dummy class RTTI is
568 // generated for the incomplete type that will not resolve to the final
569 // complete class RTTI (because the latter need not exist), possibly by
570 // making it a local static object.
571 if (ContainsIncompleteClassType(Ty))
572 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000573
574 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty)) {
575 // If the pointee type has internal linkage, then the pointer type needs to
576 // have it as well.
577 if (getTypeInfoLinkage(PointerTy->getPointeeType()) ==
578 llvm::GlobalVariable::InternalLinkage)
579 return llvm::GlobalVariable::InternalLinkage;
580
581 return llvm::GlobalVariable::WeakODRLinkage;
582 }
583
584 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
585 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
586
587 // If we're in an anonymous namespace, then we always want internal linkage.
588 if (RD->isInAnonymousNamespace() || !RD->hasLinkage())
589 return llvm::GlobalVariable::InternalLinkage;
590
591 if (!RD->isDynamicClass())
592 return llvm::GlobalValue::WeakODRLinkage;
593
594 // Get the key function.
595 const CXXMethodDecl *KeyFunction = RD->getASTContext().getKeyFunction(RD);
596 if (!KeyFunction) {
597 // There is no key function, the RTTI descriptor is emitted with weak_odr
598 // linkage.
599 return llvm::GlobalValue::WeakODRLinkage;
600 }
601
602 // Otherwise, the RTTI descriptor is emitted with external linkage.
603 return llvm::GlobalValue::ExternalLinkage;
604 }
605
Mike Stump8d9fb9b2009-12-23 22:48:20 +0000606 if (Ty->getTypeClass() == Type::Builtin) {
607 return llvm::GlobalValue::WeakODRLinkage;
608 }
609
Mike Stumpc8f76f52009-12-24 01:10:27 +0000610 if (const FunctionType *FT = dyn_cast<FunctionType>(Ty)) {
611 if (getTypeInfoLinkage(FT->getResultType())
612 == llvm::GlobalValue::InternalLinkage)
613 return llvm::GlobalValue::InternalLinkage;
614
615 if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Ty)) {
616 for (unsigned i = 0; i < FPT->getNumArgs(); ++i)
617 if (getTypeInfoLinkage(FPT->getArgType(i))
618 == llvm::GlobalValue::InternalLinkage)
619 return llvm::GlobalValue::InternalLinkage;
620 for (unsigned i = 0; i < FPT->getNumExceptions(); ++i)
621 if (getTypeInfoLinkage(FPT->getExceptionType(i))
622 == llvm::GlobalValue::InternalLinkage)
623 return llvm::GlobalValue::InternalLinkage;
624 }
625
626 return llvm::GlobalValue::WeakODRLinkage;
627 }
628
629 // FIXME: We need to add code to handle all types.
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000630 assert(false && "Unhandled type!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000631 return llvm::GlobalValue::WeakODRLinkage;
632}
633
634void RTTIBuilder::BuildVtablePointer(const Type *Ty) {
635 const char *VtableName;
636
637 switch (Ty->getTypeClass()) {
638 default: assert(0 && "Unhandled type!");
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000639
640 case Type::Record: {
641 const CXXRecordDecl *RD =
642 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
643 if (!RD->getNumBases()) {
644 // abi::__class_type_info
645 VtableName = "_ZTVN10__cxxabiv117__class_type_infoE";
646 break;
647 }
648 }
649
Anders Carlsson8d145152009-12-20 22:30:54 +0000650 case Type::Pointer:
651 // abi::__pointer_type_info
652 VtableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
653 break;
654 case Type::MemberPointer:
655 // abi::__pointer_to_member_type_info
656 VtableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
657 break;
658 }
659
660 llvm::Constant *Vtable =
661 CGM.getModule().getOrInsertGlobal(VtableName, Int8PtrTy);
662
663 const llvm::Type *PtrDiffTy =
664 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
665
666 // The vtable address point is 2.
667 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
668 Vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(Vtable, &Two, 1);
669 Vtable = llvm::ConstantExpr::getBitCast(Vtable, Int8PtrTy);
670
671 Info.push_back(Vtable);
672}
673
674llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty) {
675 // We want to operate on the canonical type.
676 Ty = CGM.getContext().getCanonicalType(Ty);
677
678 // Check if we've already emitted an RTTI descriptor for this type.
679 llvm::SmallString<256> OutName;
680 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
681 llvm::StringRef Name = OutName.str();
682
683 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
684 if (OldGV && !OldGV->isDeclaration())
685 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
686
687 // Check if there is already an external RTTI descriptor for this type.
688 if (ShouldUseExternalRTTIDescriptor(Ty))
689 return GetAddrOfExternalRTTIDescriptor(Ty);
690
691 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(Ty);
692
693 // Add the vtable pointer.
694 BuildVtablePointer(cast<Type>(Ty));
695
696 // And the name.
697 Info.push_back(BuildName(Ty, DecideHidden(Ty), Linkage));
698
699 switch (Ty->getTypeClass()) {
700 default: assert(false && "Unhandled type class!");
701 case Type::Builtin:
702 assert(false && "Builtin type info must be in the standard library!");
703 break;
704
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000705 case Type::Record: {
706 const CXXRecordDecl *RD =
707 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
708 if (!RD->getNumBases()) {
709 // We don't need to emit any fields.
710 break;
711 }
712 }
713
Anders Carlsson8d145152009-12-20 22:30:54 +0000714 case Type::Pointer:
715 BuildPointerTypeInfo(cast<PointerType>(Ty));
716 break;
717
718 case Type::MemberPointer:
719 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
720 break;
721 }
722
723 llvm::Constant *Init =
724 llvm::ConstantStruct::get(VMContext, &Info[0], Info.size(),
725 /*Packed=*/false);
726
727 llvm::GlobalVariable *GV =
728 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
729 /*Constant=*/true, Linkage, Init, Name);
730
731 // If there's already an old global variable, replace it with the new one.
732 if (OldGV) {
733 GV->takeName(OldGV);
734 llvm::Constant *NewPtr =
735 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
736 OldGV->replaceAllUsesWith(NewPtr);
737 OldGV->eraseFromParent();
738 }
739
740 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
741}
742
743/// DetermineQualifierFlags - Deterine the pointer type info flags from the
744/// given qualifier.
745static unsigned DetermineQualifierFlags(Qualifiers Quals) {
746 unsigned Flags = 0;
747
748 if (Quals.hasConst())
749 Flags |= RTTIBuilder::PTI_Const;
750 if (Quals.hasVolatile())
751 Flags |= RTTIBuilder::PTI_Volatile;
752 if (Quals.hasRestrict())
753 Flags |= RTTIBuilder::PTI_Restrict;
754
755 return Flags;
756}
757
758/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
759/// used for pointer types.
760void RTTIBuilder::BuildPointerTypeInfo(const PointerType *Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000761 QualType PointeeTy = Ty->getPointeeType();
Anders Carlsson8d145152009-12-20 22:30:54 +0000762
763 // Itanium C++ ABI 2.9.5p7:
764 // __flags is a flag word describing the cv-qualification and other
765 // attributes of the type pointed to
766 unsigned Flags = DetermineQualifierFlags(PointeeTy.getQualifiers());
767
768 // Itanium C++ ABI 2.9.5p7:
769 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
770 // incomplete class type, the incomplete target type flag is set.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000771 if (ContainsIncompleteClassType(PointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000772 Flags |= PTI_Incomplete;
773
774 const llvm::Type *UnsignedIntLTy =
775 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
776 Info.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
777
778 // Itanium C++ ABI 2.9.5p7:
779 // __pointee is a pointer to the std::type_info derivation for the
780 // unqualified type being pointed to.
781 Info.push_back(RTTIBuilder(CGM).BuildType(PointeeTy.getUnqualifiedType()));
782}
783
784/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
785/// struct, used for member pointer types.
786void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
787 QualType PointeeTy = Ty->getPointeeType();
788
789 // Itanium C++ ABI 2.9.5p7:
790 // __flags is a flag word describing the cv-qualification and other
791 // attributes of the type pointed to.
792 unsigned Flags = DetermineQualifierFlags(PointeeTy.getQualifiers());
793
794 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000795
796 // Itanium C++ ABI 2.9.5p7:
797 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
798 // incomplete class type, the incomplete target type flag is set.
799 if (ContainsIncompleteClassType(PointeeTy))
800 Flags |= PTI_Incomplete;
801
Anders Carlsson8d145152009-12-20 22:30:54 +0000802 if (IsIncompleteClassType(ClassType))
803 Flags |= PTI_ContainingClassIncomplete;
804
Anders Carlsson8d145152009-12-20 22:30:54 +0000805 const llvm::Type *UnsignedIntLTy =
806 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
807 Info.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
808
809 // Itanium C++ ABI 2.9.5p7:
810 // __pointee is a pointer to the std::type_info derivation for the
811 // unqualified type being pointed to.
812 Info.push_back(RTTIBuilder(CGM).BuildType(PointeeTy.getUnqualifiedType()));
813
814 // Itanium C++ ABI 2.9.5p9:
815 // __context is a pointer to an abi::__class_type_info corresponding to the
816 // class type containing the member pointed to
817 // (e.g., the "A" in "int A::*").
818 Info.push_back(RTTIBuilder(CGM).BuildType(QualType(ClassType, 0)));
819}
820
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000821llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000822 if (!getContext().getLangOptions().RTTI) {
823 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
824 return llvm::Constant::getNullValue(Int8PtrTy);
825 }
826
827 return RTTIBuilder(*this).BuildType(Ty);
828}