blob: 53716a071f2a5d4097a52c4e76800c9aa6916ba5 [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 Stump61c38012009-11-17 21:44:24 +000014#include "CodeGenModule.h"
John McCall4c40d982010-08-31 07:33:07 +000015#include "CGCXXABI.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "CGObjCRuntime.h"
John McCall279b5eb2010-08-12 23:36:15 +000017#include "clang/AST/RecordLayout.h"
18#include "clang/AST/Type.h"
19#include "clang/Frontend/CodeGenOptions.h"
20
Anders Carlsson656e4c12009-10-10 20:49:04 +000021using namespace clang;
22using namespace CodeGen;
23
Mike Stump92f2fe22009-12-02 19:07:44 +000024namespace {
Mike Stumpde050572009-12-02 18:57:08 +000025class RTTIBuilder {
Mike Stump2b1bf312009-11-14 14:25:18 +000026 CodeGenModule &CGM; // Per-module state.
27 llvm::LLVMContext &VMContext;
Anders Carlsson8d145152009-12-20 22:30:54 +000028
Anders Carlsson531d55f2009-12-31 17:43:53 +000029 /// Fields - The fields of the RTTI descriptor currently being built.
Chris Lattner5f9e2722011-07-23 10:55:15 +000030 SmallVector<llvm::Constant *, 16> Fields;
Anders Carlssond6baec82009-12-11 01:27:37 +000031
Anders Carlsson9a86a132011-01-29 20:36:11 +000032 /// GetAddrOfTypeName - Returns the mangled type name of the given type.
33 llvm::GlobalVariable *
34 GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage);
35
Anders Carlsson1d7088d2009-12-17 07:09:17 +000036 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
37 /// descriptor of the given type.
38 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
39
Anders Carlsson046c2942010-04-17 20:15:18 +000040 /// BuildVTablePointer - Build the vtable pointer for the given type.
41 void BuildVTablePointer(const Type *Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +000042
Anders Carlssonf64531a2009-12-30 01:00:12 +000043 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
Anders Carlsson08148092009-12-30 23:47:56 +000044 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
Anders Carlssonf64531a2009-12-30 01:00:12 +000045 void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
46
Anders Carlsson08148092009-12-30 23:47:56 +000047 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
48 /// classes with bases that do not satisfy the abi::__si_class_type_info
49 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
50 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
51
Anders Carlssonf64531a2009-12-30 01:00:12 +000052 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
53 /// for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +000054 void BuildPointerTypeInfo(QualType PointeeTy);
55
56 /// BuildObjCObjectTypeInfo - Build the appropriate kind of
57 /// type_info for an object type.
58 void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +000059
60 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
61 /// struct, used for member pointer types.
62 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
63
Mike Stump2b1bf312009-11-14 14:25:18 +000064public:
Anders Carlsson237f9592011-01-29 22:15:18 +000065 RTTIBuilder(CodeGenModule &CGM) : CGM(CGM),
Chris Lattner8b418682012-02-07 00:39:47 +000066 VMContext(CGM.getModule().getContext()) { }
Mike Stump2b1bf312009-11-14 14:25:18 +000067
Anders Carlsson8d145152009-12-20 22:30:54 +000068 // Pointer type info flags.
69 enum {
70 /// PTI_Const - Type has const qualifier.
71 PTI_Const = 0x1,
72
73 /// PTI_Volatile - Type has volatile qualifier.
74 PTI_Volatile = 0x2,
75
76 /// PTI_Restrict - Type has restrict qualifier.
77 PTI_Restrict = 0x4,
78
79 /// PTI_Incomplete - Type is incomplete.
80 PTI_Incomplete = 0x8,
81
82 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
83 /// (in pointer to member).
84 PTI_ContainingClassIncomplete = 0x10
85 };
Anders Carlsson08148092009-12-30 23:47:56 +000086
87 // VMI type info flags.
88 enum {
89 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
90 VMI_NonDiamondRepeat = 0x1,
91
92 /// VMI_DiamondShaped - Class is diamond shaped.
93 VMI_DiamondShaped = 0x2
94 };
95
96 // Base class type info flags.
97 enum {
98 /// BCTI_Virtual - Base class is virtual.
99 BCTI_Virtual = 0x1,
100
101 /// BCTI_Public - Base class is public.
102 BCTI_Public = 0x2
103 };
Anders Carlsson531d55f2009-12-31 17:43:53 +0000104
105 /// BuildTypeInfo - Build the RTTI type info struct for the given type.
John McCall9dffe6f2010-04-30 01:15:21 +0000106 ///
107 /// \param Force - true to force the creation of this RTTI value
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000108 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false);
Mike Stump2b1bf312009-11-14 14:25:18 +0000109};
Mike Stump92f2fe22009-12-02 19:07:44 +0000110}
Mike Stump2b1bf312009-11-14 14:25:18 +0000111
Anders Carlsson9a86a132011-01-29 20:36:11 +0000112llvm::GlobalVariable *
113RTTIBuilder::GetAddrOfTypeName(QualType Ty,
114 llvm::GlobalVariable::LinkageTypes Linkage) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000115 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000116 llvm::raw_svector_ostream Out(OutName);
117 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
118 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000119 StringRef Name = OutName.str();
Anders Carlsson9a86a132011-01-29 20:36:11 +0000120
121 // We know that the mangled name of the type starts at index 4 of the
122 // mangled name of the typename, so we can just index into it in order to
123 // get the mangled name of the type.
Chris Lattner94010692012-02-05 02:30:40 +0000124 llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext,
125 Name.substr(4));
Anders Carlsson9a86a132011-01-29 20:36:11 +0000126
127 llvm::GlobalVariable *GV =
128 CGM.CreateOrReplaceCXXRuntimeVariable(Name, Init->getType(), Linkage);
129
130 GV->setInitializer(Init);
131
132 return GV;
133}
134
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000135llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
136 // Mangle the RTTI name.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000137 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000138 llvm::raw_svector_ostream Out(OutName);
139 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
140 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000141 StringRef Name = OutName.str();
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000142
Anders Carlsson8d145152009-12-20 22:30:54 +0000143 // Look for an existing global.
144 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000145
146 if (!GV) {
147 // Create a new global variable.
Chris Lattner8b418682012-02-07 00:39:47 +0000148 GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy,
149 /*Constant=*/true,
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000150 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000151 }
152
Chris Lattner8b418682012-02-07 00:39:47 +0000153 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000154}
155
Anders Carlsson8d145152009-12-20 22:30:54 +0000156/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
157/// info for that type is defined in the standard library.
158static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
159 // Itanium C++ ABI 2.9.2:
160 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
161 // the run-time support library. Specifically, the run-time support
162 // library should contain type_info objects for the types X, X* and
Anders Carlsson2bd62502010-11-04 05:28:09 +0000163 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
164 // unsigned char, signed char, short, unsigned short, int, unsigned int,
165 // long, unsigned long, long long, unsigned long long, float, double,
166 // long double, char16_t, char32_t, and the IEEE 754r decimal and
167 // half-precision floating point types.
Anders Carlsson8d145152009-12-20 22:30:54 +0000168 switch (Ty->getKind()) {
169 case BuiltinType::Void:
Anders Carlsson2bd62502010-11-04 05:28:09 +0000170 case BuiltinType::NullPtr:
Anders Carlsson8d145152009-12-20 22:30:54 +0000171 case BuiltinType::Bool:
Chris Lattner3f59c972010-12-25 23:25:43 +0000172 case BuiltinType::WChar_S:
173 case BuiltinType::WChar_U:
Anders Carlsson8d145152009-12-20 22:30:54 +0000174 case BuiltinType::Char_U:
175 case BuiltinType::Char_S:
176 case BuiltinType::UChar:
177 case BuiltinType::SChar:
178 case BuiltinType::Short:
179 case BuiltinType::UShort:
180 case BuiltinType::Int:
181 case BuiltinType::UInt:
182 case BuiltinType::Long:
183 case BuiltinType::ULong:
184 case BuiltinType::LongLong:
185 case BuiltinType::ULongLong:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000186 case BuiltinType::Half:
Anders Carlsson8d145152009-12-20 22:30:54 +0000187 case BuiltinType::Float:
188 case BuiltinType::Double:
189 case BuiltinType::LongDouble:
190 case BuiltinType::Char16:
191 case BuiltinType::Char32:
192 case BuiltinType::Int128:
193 case BuiltinType::UInt128:
194 return true;
195
Anders Carlsson8d145152009-12-20 22:30:54 +0000196 case BuiltinType::Dependent:
John McCall2dde35b2011-10-18 22:28:37 +0000197#define BUILTIN_TYPE(Id, SingletonId)
198#define PLACEHOLDER_TYPE(Id, SingletonId) \
199 case BuiltinType::Id:
200#include "clang/AST/BuiltinTypes.def"
John McCall864c0412011-04-26 20:42:42 +0000201 llvm_unreachable("asking for RRTI for a placeholder type!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000202
Anders Carlsson8d145152009-12-20 22:30:54 +0000203 case BuiltinType::ObjCId:
204 case BuiltinType::ObjCClass:
205 case BuiltinType::ObjCSel:
David Blaikieb219cfc2011-09-23 05:06:16 +0000206 llvm_unreachable("FIXME: Objective-C types are unsupported!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000207 }
David Blaikie30263482012-01-20 21:50:17 +0000208
209 llvm_unreachable("Invalid BuiltinType Kind!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000210}
211
212static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
213 QualType PointeeTy = PointerTy->getPointeeType();
214 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
215 if (!BuiltinTy)
216 return false;
217
218 // Check the qualifiers.
219 Qualifiers Quals = PointeeTy.getQualifiers();
220 Quals.removeConst();
221
222 if (!Quals.empty())
223 return false;
224
225 return TypeInfoIsInStandardLibrary(BuiltinTy);
226}
227
John McCallcbfe5022010-08-04 08:34:44 +0000228/// IsStandardLibraryRTTIDescriptor - Returns whether the type
229/// information for the given type exists in the standard library.
230static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000231 // Type info for builtin types is defined in the standard library.
232 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
233 return TypeInfoIsInStandardLibrary(BuiltinTy);
234
235 // Type info for some pointer types to builtin types is defined in the
236 // standard library.
237 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
238 return TypeInfoIsInStandardLibrary(PointerTy);
239
John McCallcbfe5022010-08-04 08:34:44 +0000240 return false;
241}
242
243/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
244/// the given type exists somewhere else, and that we should not emit the type
245/// information in this translation unit. Assumes that it is not a
246/// standard-library type.
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000247static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM, QualType Ty) {
248 ASTContext &Context = CGM.getContext();
249
John McCall9dffe6f2010-04-30 01:15:21 +0000250 // If RTTI is disabled, don't consider key functions.
David Blaikie4e4d0842012-03-11 07:00:24 +0000251 if (!Context.getLangOpts().RTTI) return false;
John McCall9dffe6f2010-04-30 01:15:21 +0000252
Anders Carlsson8d145152009-12-20 22:30:54 +0000253 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000254 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000255 if (!RD->hasDefinition())
256 return false;
257
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000258 if (!RD->isDynamicClass())
259 return false;
260
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000261 return !CGM.getVTables().ShouldEmitVTableInThisTU(RD);
Anders Carlsson8d145152009-12-20 22:30:54 +0000262 }
263
264 return false;
265}
266
267/// IsIncompleteClassType - Returns whether the given record type is incomplete.
268static bool IsIncompleteClassType(const RecordType *RecordTy) {
John McCall5e1cdac2011-10-07 06:10:15 +0000269 return !RecordTy->getDecl()->isCompleteDefinition();
Anders Carlsson8d145152009-12-20 22:30:54 +0000270}
271
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000272/// ContainsIncompleteClassType - Returns whether the given type contains an
273/// incomplete class type. This is true if
274///
275/// * The given type is an incomplete class type.
276/// * The given type is a pointer type whose pointee type contains an
277/// incomplete class type.
278/// * The given type is a member pointer type whose class is an incomplete
279/// class type.
280/// * The given type is a member pointer type whoise pointee type contains an
281/// incomplete class type.
Anders Carlsson8d145152009-12-20 22:30:54 +0000282/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000283static bool ContainsIncompleteClassType(QualType Ty) {
284 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
285 if (IsIncompleteClassType(RecordTy))
286 return true;
287 }
288
289 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
290 return ContainsIncompleteClassType(PointerTy->getPointeeType());
291
292 if (const MemberPointerType *MemberPointerTy =
293 dyn_cast<MemberPointerType>(Ty)) {
294 // Check if the class type is incomplete.
295 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
296 if (IsIncompleteClassType(ClassType))
297 return true;
298
299 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000300 }
301
302 return false;
303}
304
305/// getTypeInfoLinkage - Return the linkage that the type info and type info
306/// name constants should have for the given type.
Anders Carlsson3a717f72011-01-24 02:04:33 +0000307static llvm::GlobalVariable::LinkageTypes
308getTypeInfoLinkage(CodeGenModule &CGM, QualType Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000309 // Itanium C++ ABI 2.9.5p7:
310 // In addition, it and all of the intermediate abi::__pointer_type_info
311 // structs in the chain down to the abi::__class_type_info for the
312 // incomplete class type must be prevented from resolving to the
313 // corresponding type_info structs for the complete class type, possibly
314 // by making them local static objects. Finally, a dummy class RTTI is
315 // generated for the incomplete type that will not resolve to the final
316 // complete class RTTI (because the latter need not exist), possibly by
317 // making it a local static object.
318 if (ContainsIncompleteClassType(Ty))
319 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000320
Douglas Gregor031b3712010-03-31 00:15:35 +0000321 switch (Ty->getLinkage()) {
322 case NoLinkage:
323 case InternalLinkage:
324 case UniqueExternalLinkage:
325 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson978ef682009-12-29 21:58:32 +0000326
Douglas Gregor031b3712010-03-31 00:15:35 +0000327 case ExternalLinkage:
David Blaikie4e4d0842012-03-11 07:00:24 +0000328 if (!CGM.getLangOpts().RTTI) {
Anders Carlssone34e3aa2011-01-24 02:12:11 +0000329 // RTTI is not enabled, which means that this type info struct is going
330 // to be used for exception handling. Give it linkonce_odr linkage.
331 return llvm::GlobalValue::LinkOnceODRLinkage;
332 }
333
Douglas Gregor031b3712010-03-31 00:15:35 +0000334 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
335 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +0000336 if (RD->hasAttr<WeakAttr>())
337 return llvm::GlobalValue::WeakODRLinkage;
Douglas Gregor031b3712010-03-31 00:15:35 +0000338 if (RD->isDynamicClass())
Anders Carlsson3a717f72011-01-24 02:04:33 +0000339 return CGM.getVTableLinkage(RD);
Mike Stumpc8f76f52009-12-24 01:10:27 +0000340 }
Douglas Gregor031b3712010-03-31 00:15:35 +0000341
Anders Carlssonf502d932011-01-24 00:46:19 +0000342 return llvm::GlobalValue::LinkOnceODRLinkage;
Mike Stumpc8f76f52009-12-24 01:10:27 +0000343 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000344
David Blaikie30263482012-01-20 21:50:17 +0000345 llvm_unreachable("Invalid linkage!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000346}
347
Anders Carlssonf64531a2009-12-30 01:00:12 +0000348// CanUseSingleInheritance - Return whether the given record decl has a "single,
349// public, non-virtual base at offset zero (i.e. the derived class is dynamic
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000350// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
Anders Carlssonf64531a2009-12-30 01:00:12 +0000351static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
352 // Check the number of bases.
353 if (RD->getNumBases() != 1)
354 return false;
355
356 // Get the base.
357 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
358
359 // Check that the base is not virtual.
360 if (Base->isVirtual())
361 return false;
362
363 // Check that the base is public.
364 if (Base->getAccessSpecifier() != AS_public)
365 return false;
366
Sylvestre Ledruf3477c12012-09-27 10:16:10 +0000367 // Check that the class is dynamic iff the base is.
Anders Carlssonf64531a2009-12-30 01:00:12 +0000368 const CXXRecordDecl *BaseDecl =
369 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
370 if (!BaseDecl->isEmpty() &&
371 BaseDecl->isDynamicClass() != RD->isDynamicClass())
372 return false;
373
374 return true;
375}
376
Anders Carlsson046c2942010-04-17 20:15:18 +0000377void RTTIBuilder::BuildVTablePointer(const Type *Ty) {
John McCalle8dc53e2010-08-12 02:17:33 +0000378 // abi::__class_type_info.
379 static const char * const ClassTypeInfo =
380 "_ZTVN10__cxxabiv117__class_type_infoE";
381 // abi::__si_class_type_info.
382 static const char * const SIClassTypeInfo =
383 "_ZTVN10__cxxabiv120__si_class_type_infoE";
384 // abi::__vmi_class_type_info.
385 static const char * const VMIClassTypeInfo =
386 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
387
Eli Friedman1cf26f52010-08-11 20:41:51 +0000388 const char *VTableName = 0;
Anders Carlsson8d145152009-12-20 22:30:54 +0000389
390 switch (Ty->getTypeClass()) {
Eli Friedman1cf26f52010-08-11 20:41:51 +0000391#define TYPE(Class, Base)
392#define ABSTRACT_TYPE(Class, Base)
393#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
394#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
395#define DEPENDENT_TYPE(Class, Base) case Type::Class:
396#include "clang/AST/TypeNodes.def"
David Blaikieb219cfc2011-09-23 05:06:16 +0000397 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
Eli Friedman1cf26f52010-08-11 20:41:51 +0000398
399 case Type::LValueReference:
400 case Type::RValueReference:
David Blaikieb219cfc2011-09-23 05:06:16 +0000401 llvm_unreachable("References shouldn't get here");
Anders Carlsson978ef682009-12-29 21:58:32 +0000402
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000403 case Type::Builtin:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000404 // GCC treats vector and complex types as fundamental types.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000405 case Type::Vector:
406 case Type::ExtVector:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000407 case Type::Complex:
Eli Friedmanb001de72011-10-06 23:00:33 +0000408 case Type::Atomic:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000409 // FIXME: GCC treats block pointers as fundamental types?!
410 case Type::BlockPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000411 // abi::__fundamental_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000412 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000413 break;
414
Anders Carlsson978ef682009-12-29 21:58:32 +0000415 case Type::ConstantArray:
416 case Type::IncompleteArray:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000417 case Type::VariableArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000418 // abi::__array_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000419 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000420 break;
421
422 case Type::FunctionNoProto:
423 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000424 // abi::__function_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000425 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000426 break;
427
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000428 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000429 // abi::__enum_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000430 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000431 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000432
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000433 case Type::Record: {
434 const CXXRecordDecl *RD =
435 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000436
John McCall86ff3082010-02-04 22:26:26 +0000437 if (!RD->hasDefinition() || !RD->getNumBases()) {
John McCalle8dc53e2010-08-12 02:17:33 +0000438 VTableName = ClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000439 } else if (CanUseSingleInheritance(RD)) {
John McCalle8dc53e2010-08-12 02:17:33 +0000440 VTableName = SIClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000441 } else {
John McCalle8dc53e2010-08-12 02:17:33 +0000442 VTableName = VMIClassTypeInfo;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000443 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000444
445 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000446 }
447
Eli Friedman1cf26f52010-08-11 20:41:51 +0000448 case Type::ObjCObject:
John McCalle8dc53e2010-08-12 02:17:33 +0000449 // Ignore protocol qualifiers.
450 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
451
452 // Handle id and Class.
453 if (isa<BuiltinType>(Ty)) {
454 VTableName = ClassTypeInfo;
455 break;
456 }
457
458 assert(isa<ObjCInterfaceType>(Ty));
459 // Fall through.
460
Eli Friedman1cf26f52010-08-11 20:41:51 +0000461 case Type::ObjCInterface:
John McCalle8dc53e2010-08-12 02:17:33 +0000462 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
463 VTableName = SIClassTypeInfo;
464 } else {
465 VTableName = ClassTypeInfo;
466 }
Eli Friedman1cf26f52010-08-11 20:41:51 +0000467 break;
468
John McCalle8dc53e2010-08-12 02:17:33 +0000469 case Type::ObjCObjectPointer:
Anders Carlsson8d145152009-12-20 22:30:54 +0000470 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000471 // abi::__pointer_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000472 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000473 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000474
Anders Carlsson8d145152009-12-20 22:30:54 +0000475 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000476 // abi::__pointer_to_member_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000477 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000478 break;
479 }
480
Anders Carlsson046c2942010-04-17 20:15:18 +0000481 llvm::Constant *VTable =
Chris Lattner8b418682012-02-07 00:39:47 +0000482 CGM.getModule().getOrInsertGlobal(VTableName, CGM.Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000483
Chris Lattner2acc6e32011-07-18 04:24:23 +0000484 llvm::Type *PtrDiffTy =
Anders Carlsson8d145152009-12-20 22:30:54 +0000485 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
486
487 // The vtable address point is 2.
488 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Jay Foada5c04342011-07-21 14:31:17 +0000489 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Two);
Chris Lattner8b418682012-02-07 00:39:47 +0000490 VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000491
Anders Carlsson046c2942010-04-17 20:15:18 +0000492 Fields.push_back(VTable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000493}
494
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000495// maybeUpdateRTTILinkage - Will update the linkage of the RTTI data structures
496// from available_externally to the correct linkage if necessary. An example of
497// this is:
498//
499// struct A {
500// virtual void f();
501// };
502//
503// const std::type_info &g() {
504// return typeid(A);
505// }
506//
507// void A::f() { }
508//
509// When we're generating the typeid(A) expression, we do not yet know that
510// A's key function is defined in this translation unit, so we will give the
511// typeinfo and typename structures available_externally linkage. When A::f
512// forces the vtable to be generated, we need to change the linkage of the
513// typeinfo and typename structs, otherwise we'll end up with undefined
514// externals when linking.
515static void
516maybeUpdateRTTILinkage(CodeGenModule &CGM, llvm::GlobalVariable *GV,
517 QualType Ty) {
518 // We're only interested in globals with available_externally linkage.
519 if (!GV->hasAvailableExternallyLinkage())
520 return;
521
522 // Get the real linkage for the type.
523 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(CGM, Ty);
524
525 // If variable is supposed to have available_externally linkage, we don't
526 // need to do anything.
527 if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
528 return;
529
530 // Update the typeinfo linkage.
531 GV->setLinkage(Linkage);
532
533 // Get the typename global.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000534 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000535 llvm::raw_svector_ostream Out(OutName);
536 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
537 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000538 StringRef Name = OutName.str();
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000539
540 llvm::GlobalVariable *TypeNameGV = CGM.getModule().getNamedGlobal(Name);
541
542 assert(TypeNameGV->hasAvailableExternallyLinkage() &&
543 "Type name has different linkage from type info!");
544
545 // And update its linkage.
546 TypeNameGV->setLinkage(Linkage);
547}
548
John McCallcbfe5022010-08-04 08:34:44 +0000549llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000550 // We want to operate on the canonical type.
551 Ty = CGM.getContext().getCanonicalType(Ty);
552
553 // Check if we've already emitted an RTTI descriptor for this type.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000554 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000555 llvm::raw_svector_ostream Out(OutName);
556 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
557 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000558 StringRef Name = OutName.str();
Anders Carlsson1cbce122011-01-29 19:16:51 +0000559
Anders Carlsson8d145152009-12-20 22:30:54 +0000560 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000561 if (OldGV && !OldGV->isDeclaration()) {
562 maybeUpdateRTTILinkage(CGM, OldGV, Ty);
563
Chris Lattner8b418682012-02-07 00:39:47 +0000564 return llvm::ConstantExpr::getBitCast(OldGV, CGM.Int8PtrTy);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000565 }
John McCallcbfe5022010-08-04 08:34:44 +0000566
Anders Carlsson8d145152009-12-20 22:30:54 +0000567 // Check if there is already an external RTTI descriptor for this type.
John McCallcbfe5022010-08-04 08:34:44 +0000568 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000569 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
Anders Carlsson8d145152009-12-20 22:30:54 +0000570 return GetAddrOfExternalRTTIDescriptor(Ty);
571
John McCallcbfe5022010-08-04 08:34:44 +0000572 // Emit the standard library with external linkage.
573 llvm::GlobalVariable::LinkageTypes Linkage;
574 if (IsStdLib)
575 Linkage = llvm::GlobalValue::ExternalLinkage;
576 else
Anders Carlsson3a717f72011-01-24 02:04:33 +0000577 Linkage = getTypeInfoLinkage(CGM, Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +0000578
579 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000580 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000581
582 // And the name.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000583 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
584
Chris Lattner8b418682012-02-07 00:39:47 +0000585 Fields.push_back(llvm::ConstantExpr::getBitCast(TypeName, CGM.Int8PtrTy));
John McCallcbfe5022010-08-04 08:34:44 +0000586
Anders Carlsson8d145152009-12-20 22:30:54 +0000587 switch (Ty->getTypeClass()) {
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000588#define TYPE(Class, Base)
589#define ABSTRACT_TYPE(Class, Base)
590#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
591#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
592#define DEPENDENT_TYPE(Class, Base) case Type::Class:
593#include "clang/AST/TypeNodes.def"
David Blaikieb219cfc2011-09-23 05:06:16 +0000594 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
Anders Carlsson8d145152009-12-20 22:30:54 +0000595
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000596 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000597 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000598 case Type::Vector:
599 case Type::ExtVector:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000600 case Type::Complex:
601 case Type::BlockPointer:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000602 // Itanium C++ ABI 2.9.5p4:
603 // abi::__fundamental_type_info adds no data members to std::type_info.
604 break;
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000605
606 case Type::LValueReference:
607 case Type::RValueReference:
David Blaikieb219cfc2011-09-23 05:06:16 +0000608 llvm_unreachable("References shouldn't get here");
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000609
Anders Carlsson978ef682009-12-29 21:58:32 +0000610 case Type::ConstantArray:
611 case Type::IncompleteArray:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000612 case Type::VariableArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000613 // Itanium C++ ABI 2.9.5p5:
614 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000615 break;
616
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000617 case Type::FunctionNoProto:
618 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000619 // Itanium C++ ABI 2.9.5p5:
620 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000621 break;
622
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000623 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000624 // Itanium C++ ABI 2.9.5p5:
625 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000626 break;
627
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000628 case Type::Record: {
629 const CXXRecordDecl *RD =
630 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000631 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000632 // We don't need to emit any fields.
633 break;
634 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000635
Anders Carlsson08148092009-12-30 23:47:56 +0000636 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000637 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000638 else
639 BuildVMIClassTypeInfo(RD);
640
641 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000642 }
John McCalle8dc53e2010-08-12 02:17:33 +0000643
644 case Type::ObjCObject:
645 case Type::ObjCInterface:
646 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
647 break;
648
649 case Type::ObjCObjectPointer:
650 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
651 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000652
Anders Carlsson8d145152009-12-20 22:30:54 +0000653 case Type::Pointer:
John McCalle8dc53e2010-08-12 02:17:33 +0000654 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000655 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000656
Anders Carlsson8d145152009-12-20 22:30:54 +0000657 case Type::MemberPointer:
658 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
659 break;
Eli Friedmanb001de72011-10-06 23:00:33 +0000660
661 case Type::Atomic:
662 // No fields, at least for the moment.
663 break;
Anders Carlsson8d145152009-12-20 22:30:54 +0000664 }
665
Chris Lattnerc5cbb902011-06-20 04:01:35 +0000666 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields);
Anders Carlsson8d145152009-12-20 22:30:54 +0000667
668 llvm::GlobalVariable *GV =
669 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
670 /*Constant=*/true, Linkage, Init, Name);
671
672 // If there's already an old global variable, replace it with the new one.
673 if (OldGV) {
674 GV->takeName(OldGV);
675 llvm::Constant *NewPtr =
676 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
677 OldGV->replaceAllUsesWith(NewPtr);
678 OldGV->eraseFromParent();
679 }
John McCallcbfe5022010-08-04 08:34:44 +0000680
681 // GCC only relies on the uniqueness of the type names, not the
682 // type_infos themselves, so we can emit these as hidden symbols.
John McCall279b5eb2010-08-12 23:36:15 +0000683 // But don't do this if we're worried about strict visibility
684 // compatibility.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000685 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) {
686 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
687
688 CGM.setTypeVisibility(GV, RD, CodeGenModule::TVK_ForRTTI);
689 CGM.setTypeVisibility(TypeName, RD, CodeGenModule::TVK_ForRTTIName);
Anders Carlsson907c8282011-01-29 22:10:32 +0000690 } else {
691 Visibility TypeInfoVisibility = DefaultVisibility;
692 if (CGM.getCodeGenOpts().HiddenWeakVTables &&
693 Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
694 TypeInfoVisibility = HiddenVisibility;
Anders Carlsson9a86a132011-01-29 20:36:11 +0000695
Anders Carlsson907c8282011-01-29 22:10:32 +0000696 // The type name should have the same visibility as the type itself.
697 Visibility ExplicitVisibility = Ty->getVisibility();
698 TypeName->setVisibility(CodeGenModule::
699 GetLLVMVisibility(ExplicitVisibility));
700
701 TypeInfoVisibility = minVisibility(TypeInfoVisibility, Ty->getVisibility());
702 GV->setVisibility(CodeGenModule::GetLLVMVisibility(TypeInfoVisibility));
Rafael Espindolab1c65ff2011-01-11 21:44:37 +0000703 }
Anders Carlsson907c8282011-01-29 22:10:32 +0000704
Rafael Espindola57244f62011-01-11 23:55:05 +0000705 GV->setUnnamedAddr(true);
706
Chris Lattner8b418682012-02-07 00:39:47 +0000707 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000708}
709
Anders Carlsson08148092009-12-30 23:47:56 +0000710/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000711/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000712static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000713 unsigned Flags = 0;
714
715 if (Quals.hasConst())
716 Flags |= RTTIBuilder::PTI_Const;
717 if (Quals.hasVolatile())
718 Flags |= RTTIBuilder::PTI_Volatile;
719 if (Quals.hasRestrict())
720 Flags |= RTTIBuilder::PTI_Restrict;
721
722 return Flags;
723}
724
John McCalle8dc53e2010-08-12 02:17:33 +0000725/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
726/// for the given Objective-C object type.
727void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
728 // Drop qualifiers.
729 const Type *T = OT->getBaseType().getTypePtr();
730 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
731
732 // The builtin types are abi::__class_type_infos and don't require
733 // extra fields.
734 if (isa<BuiltinType>(T)) return;
735
736 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
737 ObjCInterfaceDecl *Super = Class->getSuperClass();
738
739 // Root classes are also __class_type_info.
740 if (!Super) return;
741
742 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
743
744 // Everything else is single inheritance.
745 llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
746 Fields.push_back(BaseTypeInfo);
747}
748
Anders Carlssonf64531a2009-12-30 01:00:12 +0000749/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
750/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
751void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
752 // Itanium C++ ABI 2.9.5p6b:
753 // It adds to abi::__class_type_info a single member pointing to the
754 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000755 llvm::Constant *BaseTypeInfo =
756 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
757 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000758}
759
Benjamin Kramer79ba2a62010-10-22 16:48:22 +0000760namespace {
761 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
762 /// a class hierarchy.
763 struct SeenBases {
764 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
765 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
766 };
767}
Anders Carlsson08148092009-12-30 23:47:56 +0000768
769/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
770/// abi::__vmi_class_type_info.
771///
772static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
773 SeenBases &Bases) {
774
775 unsigned Flags = 0;
776
777 const CXXRecordDecl *BaseDecl =
778 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
779
780 if (Base->isVirtual()) {
Benjamin Kramerd48bcb22012-08-22 15:37:55 +0000781 // Mark the virtual base as seen.
782 if (!Bases.VirtualBases.insert(BaseDecl)) {
Anders Carlsson08148092009-12-30 23:47:56 +0000783 // If this virtual base has been seen before, then the class is diamond
784 // shaped.
785 Flags |= RTTIBuilder::VMI_DiamondShaped;
786 } else {
787 if (Bases.NonVirtualBases.count(BaseDecl))
788 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
Anders Carlsson08148092009-12-30 23:47:56 +0000789 }
790 } else {
Benjamin Kramerd48bcb22012-08-22 15:37:55 +0000791 // Mark the non-virtual base as seen.
792 if (!Bases.NonVirtualBases.insert(BaseDecl)) {
Anders Carlsson08148092009-12-30 23:47:56 +0000793 // If this non-virtual base has been seen before, then the class has non-
794 // diamond shaped repeated inheritance.
795 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
796 } else {
797 if (Bases.VirtualBases.count(BaseDecl))
798 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
Anders Carlsson08148092009-12-30 23:47:56 +0000799 }
800 }
801
802 // Walk all bases.
803 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
804 E = BaseDecl->bases_end(); I != E; ++I)
805 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
806
807 return Flags;
808}
809
810static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
811 unsigned Flags = 0;
812 SeenBases Bases;
813
814 // Walk all bases.
815 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
816 E = RD->bases_end(); I != E; ++I)
817 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
818
819 return Flags;
820}
821
822/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
823/// classes with bases that do not satisfy the abi::__si_class_type_info
824/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
825void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000826 llvm::Type *UnsignedIntLTy =
Anders Carlsson08148092009-12-30 23:47:56 +0000827 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
828
829 // Itanium C++ ABI 2.9.5p6c:
830 // __flags is a word with flags describing details about the class
831 // structure, which may be referenced by using the __flags_masks
832 // enumeration. These flags refer to both direct and indirect bases.
833 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000834 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000835
836 // Itanium C++ ABI 2.9.5p6c:
837 // __base_count is a word with the number of direct proper base class
838 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000839 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000840
841 if (!RD->getNumBases())
842 return;
843
Chris Lattner2acc6e32011-07-18 04:24:23 +0000844 llvm::Type *LongLTy =
Anders Carlsson08148092009-12-30 23:47:56 +0000845 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
846
847 // Now add the base class descriptions.
848
849 // Itanium C++ ABI 2.9.5p6c:
850 // __base_info[] is an array of base class descriptions -- one for every
851 // direct proper base. Each description is of the type:
852 //
853 // struct abi::__base_class_type_info {
Eli Friedmana7e68452010-08-22 01:00:03 +0000854 // public:
Anders Carlsson08148092009-12-30 23:47:56 +0000855 // const __class_type_info *__base_type;
856 // long __offset_flags;
857 //
858 // enum __offset_flags_masks {
859 // __virtual_mask = 0x1,
860 // __public_mask = 0x2,
861 // __offset_shift = 8
862 // };
863 // };
864 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
865 E = RD->bases_end(); I != E; ++I) {
866 const CXXBaseSpecifier *Base = I;
867
868 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000869 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000870
871 const CXXRecordDecl *BaseDecl =
872 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
873
874 int64_t OffsetFlags = 0;
875
876 // All but the lower 8 bits of __offset_flags are a signed offset.
877 // For a non-virtual base, this is the offset in the object of the base
878 // subobject. For a virtual base, this is the offset in the virtual table of
879 // the virtual base offset for the virtual base referenced (negative).
Ken Dyckb653d5a2011-04-09 01:09:56 +0000880 CharUnits Offset;
Anders Carlsson08148092009-12-30 23:47:56 +0000881 if (Base->isVirtual())
Ken Dyckb653d5a2011-04-09 01:09:56 +0000882 Offset =
Peter Collingbourne1d2b3172011-09-26 01:56:30 +0000883 CGM.getVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000884 else {
885 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Ken Dyckb653d5a2011-04-09 01:09:56 +0000886 Offset = Layout.getBaseClassOffset(BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000887 };
888
Richard Smithc831d8b2012-08-24 23:43:39 +0000889 OffsetFlags = uint64_t(Offset.getQuantity()) << 8;
Anders Carlsson08148092009-12-30 23:47:56 +0000890
891 // The low-order byte of __offset_flags contains flags, as given by the
892 // masks from the enumeration __offset_flags_masks.
893 if (Base->isVirtual())
894 OffsetFlags |= BCTI_Virtual;
895 if (Base->getAccessSpecifier() == AS_public)
896 OffsetFlags |= BCTI_Public;
897
Anders Carlsson531d55f2009-12-31 17:43:53 +0000898 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000899 }
900}
901
Anders Carlsson8d145152009-12-20 22:30:54 +0000902/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
903/// used for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +0000904void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
Anders Carlssonabd6b092010-06-02 15:44:35 +0000905 Qualifiers Quals;
906 QualType UnqualifiedPointeeTy =
907 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
908
Anders Carlsson8d145152009-12-20 22:30:54 +0000909 // Itanium C++ ABI 2.9.5p7:
910 // __flags is a flag word describing the cv-qualification and other
911 // attributes of the type pointed to
Anders Carlssonabd6b092010-06-02 15:44:35 +0000912 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000913
914 // Itanium C++ ABI 2.9.5p7:
915 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
916 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000917 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000918 Flags |= PTI_Incomplete;
919
Chris Lattner2acc6e32011-07-18 04:24:23 +0000920 llvm::Type *UnsignedIntLTy =
Anders Carlsson8d145152009-12-20 22:30:54 +0000921 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000922 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000923
924 // Itanium C++ ABI 2.9.5p7:
925 // __pointee is a pointer to the std::type_info derivation for the
926 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000927 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000928 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000929 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000930}
931
932/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
933/// struct, used for member pointer types.
934void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
935 QualType PointeeTy = Ty->getPointeeType();
936
Anders Carlssonabd6b092010-06-02 15:44:35 +0000937 Qualifiers Quals;
938 QualType UnqualifiedPointeeTy =
939 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
940
Anders Carlsson8d145152009-12-20 22:30:54 +0000941 // Itanium C++ ABI 2.9.5p7:
942 // __flags is a flag word describing the cv-qualification and other
943 // attributes of the type pointed to.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000944 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000945
946 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000947
948 // Itanium C++ ABI 2.9.5p7:
949 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
950 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000951 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000952 Flags |= PTI_Incomplete;
953
Anders Carlsson8d145152009-12-20 22:30:54 +0000954 if (IsIncompleteClassType(ClassType))
955 Flags |= PTI_ContainingClassIncomplete;
956
Chris Lattner2acc6e32011-07-18 04:24:23 +0000957 llvm::Type *UnsignedIntLTy =
Anders Carlsson8d145152009-12-20 22:30:54 +0000958 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000959 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000960
961 // Itanium C++ ABI 2.9.5p7:
962 // __pointee is a pointer to the std::type_info derivation for the
963 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000964 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000965 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000966 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000967
968 // Itanium C++ ABI 2.9.5p9:
969 // __context is a pointer to an abi::__class_type_info corresponding to the
970 // class type containing the member pointed to
971 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000972 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000973}
974
John McCall9dffe6f2010-04-30 01:15:21 +0000975llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
976 bool ForEH) {
977 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
978 // FIXME: should we even be calling this method if RTTI is disabled
979 // and it's not for EH?
Richard Smith7edf9e32012-11-01 22:30:59 +0000980 if (!ForEH && !getLangOpts().RTTI)
Anders Carlsson31b7f522009-12-11 02:46:30 +0000981 return llvm::Constant::getNullValue(Int8PtrTy);
David Chisnall80558d22011-03-20 21:35:39 +0000982
John McCall260611a2012-06-20 06:18:46 +0000983 if (ForEH && Ty->isObjCObjectPointerType() &&
984 LangOpts.ObjCRuntime.isGNUFamily())
Peter Collingbournee9265232011-07-27 20:29:46 +0000985 return ObjCRuntime->GetEHType(Ty);
John McCall9dffe6f2010-04-30 01:15:21 +0000986
Anders Carlsson531d55f2009-12-31 17:43:53 +0000987 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000988}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000989
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000990void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
991 QualType PointerType = Context.getPointerType(Type);
992 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
993 RTTIBuilder(*this).BuildTypeInfo(Type, true);
994 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
995 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
996}
997
998void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Anders Carlsson2bd62502010-11-04 05:28:09 +0000999 QualType FundamentalTypes[] = { Context.VoidTy, Context.NullPtrTy,
1000 Context.BoolTy, Context.WCharTy,
1001 Context.CharTy, Context.UnsignedCharTy,
1002 Context.SignedCharTy, Context.ShortTy,
1003 Context.UnsignedShortTy, Context.IntTy,
1004 Context.UnsignedIntTy, Context.LongTy,
1005 Context.UnsignedLongTy, Context.LongLongTy,
1006 Context.UnsignedLongLongTy, Context.FloatTy,
1007 Context.DoubleTy, Context.LongDoubleTy,
1008 Context.Char16Ty, Context.Char32Ty };
Rafael Espindolad1a5c312010-03-27 02:52:14 +00001009 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
1010 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
1011}