blob: a8c4ac46349ed79a54f3605a56134d2bd7181962 [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"
John McCall279b5eb2010-08-12 23:36:15 +000016#include "clang/AST/RecordLayout.h"
17#include "clang/AST/Type.h"
18#include "clang/Frontend/CodeGenOptions.h"
David Chisnall80558d22011-03-20 21:35:39 +000019#include "CGObjCRuntime.h"
John McCall279b5eb2010-08-12 23:36:15 +000020
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
108 /// \param ForEH - true if this is for exception handling
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000109 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false);
Mike Stump2b1bf312009-11-14 14:25:18 +0000110};
Mike Stump92f2fe22009-12-02 19:07:44 +0000111}
Mike Stump2b1bf312009-11-14 14:25:18 +0000112
Anders Carlsson9a86a132011-01-29 20:36:11 +0000113llvm::GlobalVariable *
114RTTIBuilder::GetAddrOfTypeName(QualType Ty,
115 llvm::GlobalVariable::LinkageTypes Linkage) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000116 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000117 llvm::raw_svector_ostream Out(OutName);
118 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
119 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000120 StringRef Name = OutName.str();
Anders Carlsson9a86a132011-01-29 20:36:11 +0000121
122 // We know that the mangled name of the type starts at index 4 of the
123 // mangled name of the typename, so we can just index into it in order to
124 // get the mangled name of the type.
Chris Lattner94010692012-02-05 02:30:40 +0000125 llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext,
126 Name.substr(4));
Anders Carlsson9a86a132011-01-29 20:36:11 +0000127
128 llvm::GlobalVariable *GV =
129 CGM.CreateOrReplaceCXXRuntimeVariable(Name, Init->getType(), Linkage);
130
131 GV->setInitializer(Init);
132
133 return GV;
134}
135
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000136llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
137 // Mangle the RTTI name.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000138 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000139 llvm::raw_svector_ostream Out(OutName);
140 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
141 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000142 StringRef Name = OutName.str();
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000143
Anders Carlsson8d145152009-12-20 22:30:54 +0000144 // Look for an existing global.
145 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000146
147 if (!GV) {
148 // Create a new global variable.
Chris Lattner8b418682012-02-07 00:39:47 +0000149 GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy,
150 /*Constant=*/true,
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000151 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000152 }
153
Chris Lattner8b418682012-02-07 00:39:47 +0000154 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000155}
156
Anders Carlsson8d145152009-12-20 22:30:54 +0000157/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
158/// info for that type is defined in the standard library.
159static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
160 // Itanium C++ ABI 2.9.2:
161 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
162 // the run-time support library. Specifically, the run-time support
163 // library should contain type_info objects for the types X, X* and
Anders Carlsson2bd62502010-11-04 05:28:09 +0000164 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
165 // unsigned char, signed char, short, unsigned short, int, unsigned int,
166 // long, unsigned long, long long, unsigned long long, float, double,
167 // long double, char16_t, char32_t, and the IEEE 754r decimal and
168 // half-precision floating point types.
Anders Carlsson8d145152009-12-20 22:30:54 +0000169 switch (Ty->getKind()) {
170 case BuiltinType::Void:
Anders Carlsson2bd62502010-11-04 05:28:09 +0000171 case BuiltinType::NullPtr:
Anders Carlsson8d145152009-12-20 22:30:54 +0000172 case BuiltinType::Bool:
Chris Lattner3f59c972010-12-25 23:25:43 +0000173 case BuiltinType::WChar_S:
174 case BuiltinType::WChar_U:
Anders Carlsson8d145152009-12-20 22:30:54 +0000175 case BuiltinType::Char_U:
176 case BuiltinType::Char_S:
177 case BuiltinType::UChar:
178 case BuiltinType::SChar:
179 case BuiltinType::Short:
180 case BuiltinType::UShort:
181 case BuiltinType::Int:
182 case BuiltinType::UInt:
183 case BuiltinType::Long:
184 case BuiltinType::ULong:
185 case BuiltinType::LongLong:
186 case BuiltinType::ULongLong:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000187 case BuiltinType::Half:
Anders Carlsson8d145152009-12-20 22:30:54 +0000188 case BuiltinType::Float:
189 case BuiltinType::Double:
190 case BuiltinType::LongDouble:
191 case BuiltinType::Char16:
192 case BuiltinType::Char32:
193 case BuiltinType::Int128:
194 case BuiltinType::UInt128:
195 return true;
196
Anders Carlsson8d145152009-12-20 22:30:54 +0000197 case BuiltinType::Dependent:
John McCall2dde35b2011-10-18 22:28:37 +0000198#define BUILTIN_TYPE(Id, SingletonId)
199#define PLACEHOLDER_TYPE(Id, SingletonId) \
200 case BuiltinType::Id:
201#include "clang/AST/BuiltinTypes.def"
John McCall864c0412011-04-26 20:42:42 +0000202 llvm_unreachable("asking for RRTI for a placeholder type!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000203
Anders Carlsson8d145152009-12-20 22:30:54 +0000204 case BuiltinType::ObjCId:
205 case BuiltinType::ObjCClass:
206 case BuiltinType::ObjCSel:
David Blaikieb219cfc2011-09-23 05:06:16 +0000207 llvm_unreachable("FIXME: Objective-C types are unsupported!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000208 }
David Blaikie30263482012-01-20 21:50:17 +0000209
210 llvm_unreachable("Invalid BuiltinType Kind!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000211}
212
213static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
214 QualType PointeeTy = PointerTy->getPointeeType();
215 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
216 if (!BuiltinTy)
217 return false;
218
219 // Check the qualifiers.
220 Qualifiers Quals = PointeeTy.getQualifiers();
221 Quals.removeConst();
222
223 if (!Quals.empty())
224 return false;
225
226 return TypeInfoIsInStandardLibrary(BuiltinTy);
227}
228
John McCallcbfe5022010-08-04 08:34:44 +0000229/// IsStandardLibraryRTTIDescriptor - Returns whether the type
230/// information for the given type exists in the standard library.
231static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000232 // Type info for builtin types is defined in the standard library.
233 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
234 return TypeInfoIsInStandardLibrary(BuiltinTy);
235
236 // Type info for some pointer types to builtin types is defined in the
237 // standard library.
238 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
239 return TypeInfoIsInStandardLibrary(PointerTy);
240
John McCallcbfe5022010-08-04 08:34:44 +0000241 return false;
242}
243
244/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
245/// the given type exists somewhere else, and that we should not emit the type
246/// information in this translation unit. Assumes that it is not a
247/// standard-library type.
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000248static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM, QualType Ty) {
249 ASTContext &Context = CGM.getContext();
250
John McCall9dffe6f2010-04-30 01:15:21 +0000251 // If RTTI is disabled, don't consider key functions.
252 if (!Context.getLangOptions().RTTI) return false;
253
Anders Carlsson8d145152009-12-20 22:30:54 +0000254 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000255 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000256 if (!RD->hasDefinition())
257 return false;
258
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000259 if (!RD->isDynamicClass())
260 return false;
261
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000262 return !CGM.getVTables().ShouldEmitVTableInThisTU(RD);
Anders Carlsson8d145152009-12-20 22:30:54 +0000263 }
264
265 return false;
266}
267
268/// IsIncompleteClassType - Returns whether the given record type is incomplete.
269static bool IsIncompleteClassType(const RecordType *RecordTy) {
John McCall5e1cdac2011-10-07 06:10:15 +0000270 return !RecordTy->getDecl()->isCompleteDefinition();
Anders Carlsson8d145152009-12-20 22:30:54 +0000271}
272
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000273/// ContainsIncompleteClassType - Returns whether the given type contains an
274/// incomplete class type. This is true if
275///
276/// * The given type is an incomplete class type.
277/// * The given type is a pointer type whose pointee type contains an
278/// incomplete class type.
279/// * The given type is a member pointer type whose class is an incomplete
280/// class type.
281/// * The given type is a member pointer type whoise pointee type contains an
282/// incomplete class type.
Anders Carlsson8d145152009-12-20 22:30:54 +0000283/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000284static bool ContainsIncompleteClassType(QualType Ty) {
285 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
286 if (IsIncompleteClassType(RecordTy))
287 return true;
288 }
289
290 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
291 return ContainsIncompleteClassType(PointerTy->getPointeeType());
292
293 if (const MemberPointerType *MemberPointerTy =
294 dyn_cast<MemberPointerType>(Ty)) {
295 // Check if the class type is incomplete.
296 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
297 if (IsIncompleteClassType(ClassType))
298 return true;
299
300 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000301 }
302
303 return false;
304}
305
306/// getTypeInfoLinkage - Return the linkage that the type info and type info
307/// name constants should have for the given type.
Anders Carlsson3a717f72011-01-24 02:04:33 +0000308static llvm::GlobalVariable::LinkageTypes
309getTypeInfoLinkage(CodeGenModule &CGM, QualType Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000310 // Itanium C++ ABI 2.9.5p7:
311 // In addition, it and all of the intermediate abi::__pointer_type_info
312 // structs in the chain down to the abi::__class_type_info for the
313 // incomplete class type must be prevented from resolving to the
314 // corresponding type_info structs for the complete class type, possibly
315 // by making them local static objects. Finally, a dummy class RTTI is
316 // generated for the incomplete type that will not resolve to the final
317 // complete class RTTI (because the latter need not exist), possibly by
318 // making it a local static object.
319 if (ContainsIncompleteClassType(Ty))
320 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000321
Douglas Gregor031b3712010-03-31 00:15:35 +0000322 switch (Ty->getLinkage()) {
323 case NoLinkage:
324 case InternalLinkage:
325 case UniqueExternalLinkage:
326 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson978ef682009-12-29 21:58:32 +0000327
Douglas Gregor031b3712010-03-31 00:15:35 +0000328 case ExternalLinkage:
Anders Carlssone34e3aa2011-01-24 02:12:11 +0000329 if (!CGM.getLangOptions().RTTI) {
330 // RTTI is not enabled, which means that this type info struct is going
331 // to be used for exception handling. Give it linkonce_odr linkage.
332 return llvm::GlobalValue::LinkOnceODRLinkage;
333 }
334
Douglas Gregor031b3712010-03-31 00:15:35 +0000335 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
336 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +0000337 if (RD->hasAttr<WeakAttr>())
338 return llvm::GlobalValue::WeakODRLinkage;
Douglas Gregor031b3712010-03-31 00:15:35 +0000339 if (RD->isDynamicClass())
Anders Carlsson3a717f72011-01-24 02:04:33 +0000340 return CGM.getVTableLinkage(RD);
Mike Stumpc8f76f52009-12-24 01:10:27 +0000341 }
Douglas Gregor031b3712010-03-31 00:15:35 +0000342
Anders Carlssonf502d932011-01-24 00:46:19 +0000343 return llvm::GlobalValue::LinkOnceODRLinkage;
Mike Stumpc8f76f52009-12-24 01:10:27 +0000344 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000345
David Blaikie30263482012-01-20 21:50:17 +0000346 llvm_unreachable("Invalid linkage!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000347}
348
Anders Carlssonf64531a2009-12-30 01:00:12 +0000349// CanUseSingleInheritance - Return whether the given record decl has a "single,
350// public, non-virtual base at offset zero (i.e. the derived class is dynamic
351// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
352static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
353 // Check the number of bases.
354 if (RD->getNumBases() != 1)
355 return false;
356
357 // Get the base.
358 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
359
360 // Check that the base is not virtual.
361 if (Base->isVirtual())
362 return false;
363
364 // Check that the base is public.
365 if (Base->getAccessSpecifier() != AS_public)
366 return false;
367
368 // Check that the class is dynamic iff the base is.
369 const CXXRecordDecl *BaseDecl =
370 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
371 if (!BaseDecl->isEmpty() &&
372 BaseDecl->isDynamicClass() != RD->isDynamicClass())
373 return false;
374
375 return true;
376}
377
Anders Carlsson046c2942010-04-17 20:15:18 +0000378void RTTIBuilder::BuildVTablePointer(const Type *Ty) {
John McCalle8dc53e2010-08-12 02:17:33 +0000379 // abi::__class_type_info.
380 static const char * const ClassTypeInfo =
381 "_ZTVN10__cxxabiv117__class_type_infoE";
382 // abi::__si_class_type_info.
383 static const char * const SIClassTypeInfo =
384 "_ZTVN10__cxxabiv120__si_class_type_infoE";
385 // abi::__vmi_class_type_info.
386 static const char * const VMIClassTypeInfo =
387 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
388
Eli Friedman1cf26f52010-08-11 20:41:51 +0000389 const char *VTableName = 0;
Anders Carlsson8d145152009-12-20 22:30:54 +0000390
391 switch (Ty->getTypeClass()) {
Eli Friedman1cf26f52010-08-11 20:41:51 +0000392#define TYPE(Class, Base)
393#define ABSTRACT_TYPE(Class, Base)
394#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
395#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
396#define DEPENDENT_TYPE(Class, Base) case Type::Class:
397#include "clang/AST/TypeNodes.def"
David Blaikieb219cfc2011-09-23 05:06:16 +0000398 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
Eli Friedman1cf26f52010-08-11 20:41:51 +0000399
400 case Type::LValueReference:
401 case Type::RValueReference:
David Blaikieb219cfc2011-09-23 05:06:16 +0000402 llvm_unreachable("References shouldn't get here");
Anders Carlsson978ef682009-12-29 21:58:32 +0000403
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000404 case Type::Builtin:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000405 // GCC treats vector and complex types as fundamental types.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000406 case Type::Vector:
407 case Type::ExtVector:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000408 case Type::Complex:
Eli Friedmanb001de72011-10-06 23:00:33 +0000409 case Type::Atomic:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000410 // FIXME: GCC treats block pointers as fundamental types?!
411 case Type::BlockPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000412 // abi::__fundamental_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000413 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000414 break;
415
Anders Carlsson978ef682009-12-29 21:58:32 +0000416 case Type::ConstantArray:
417 case Type::IncompleteArray:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000418 case Type::VariableArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000419 // abi::__array_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000420 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000421 break;
422
423 case Type::FunctionNoProto:
424 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000425 // abi::__function_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000426 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000427 break;
428
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000429 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000430 // abi::__enum_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000431 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000432 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000433
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000434 case Type::Record: {
435 const CXXRecordDecl *RD =
436 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000437
John McCall86ff3082010-02-04 22:26:26 +0000438 if (!RD->hasDefinition() || !RD->getNumBases()) {
John McCalle8dc53e2010-08-12 02:17:33 +0000439 VTableName = ClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000440 } else if (CanUseSingleInheritance(RD)) {
John McCalle8dc53e2010-08-12 02:17:33 +0000441 VTableName = SIClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000442 } else {
John McCalle8dc53e2010-08-12 02:17:33 +0000443 VTableName = VMIClassTypeInfo;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000444 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000445
446 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000447 }
448
Eli Friedman1cf26f52010-08-11 20:41:51 +0000449 case Type::ObjCObject:
John McCalle8dc53e2010-08-12 02:17:33 +0000450 // Ignore protocol qualifiers.
451 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
452
453 // Handle id and Class.
454 if (isa<BuiltinType>(Ty)) {
455 VTableName = ClassTypeInfo;
456 break;
457 }
458
459 assert(isa<ObjCInterfaceType>(Ty));
460 // Fall through.
461
Eli Friedman1cf26f52010-08-11 20:41:51 +0000462 case Type::ObjCInterface:
John McCalle8dc53e2010-08-12 02:17:33 +0000463 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
464 VTableName = SIClassTypeInfo;
465 } else {
466 VTableName = ClassTypeInfo;
467 }
Eli Friedman1cf26f52010-08-11 20:41:51 +0000468 break;
469
John McCalle8dc53e2010-08-12 02:17:33 +0000470 case Type::ObjCObjectPointer:
Anders Carlsson8d145152009-12-20 22:30:54 +0000471 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000472 // abi::__pointer_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000473 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000474 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000475
Anders Carlsson8d145152009-12-20 22:30:54 +0000476 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000477 // abi::__pointer_to_member_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000478 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000479 break;
480 }
481
Anders Carlsson046c2942010-04-17 20:15:18 +0000482 llvm::Constant *VTable =
Chris Lattner8b418682012-02-07 00:39:47 +0000483 CGM.getModule().getOrInsertGlobal(VTableName, CGM.Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000484
Chris Lattner2acc6e32011-07-18 04:24:23 +0000485 llvm::Type *PtrDiffTy =
Anders Carlsson8d145152009-12-20 22:30:54 +0000486 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
487
488 // The vtable address point is 2.
489 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Jay Foada5c04342011-07-21 14:31:17 +0000490 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Two);
Chris Lattner8b418682012-02-07 00:39:47 +0000491 VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000492
Anders Carlsson046c2942010-04-17 20:15:18 +0000493 Fields.push_back(VTable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000494}
495
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000496// maybeUpdateRTTILinkage - Will update the linkage of the RTTI data structures
497// from available_externally to the correct linkage if necessary. An example of
498// this is:
499//
500// struct A {
501// virtual void f();
502// };
503//
504// const std::type_info &g() {
505// return typeid(A);
506// }
507//
508// void A::f() { }
509//
510// When we're generating the typeid(A) expression, we do not yet know that
511// A's key function is defined in this translation unit, so we will give the
512// typeinfo and typename structures available_externally linkage. When A::f
513// forces the vtable to be generated, we need to change the linkage of the
514// typeinfo and typename structs, otherwise we'll end up with undefined
515// externals when linking.
516static void
517maybeUpdateRTTILinkage(CodeGenModule &CGM, llvm::GlobalVariable *GV,
518 QualType Ty) {
519 // We're only interested in globals with available_externally linkage.
520 if (!GV->hasAvailableExternallyLinkage())
521 return;
522
523 // Get the real linkage for the type.
524 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(CGM, Ty);
525
526 // If variable is supposed to have available_externally linkage, we don't
527 // need to do anything.
528 if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
529 return;
530
531 // Update the typeinfo linkage.
532 GV->setLinkage(Linkage);
533
534 // Get the typename global.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000535 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000536 llvm::raw_svector_ostream Out(OutName);
537 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
538 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000539 StringRef Name = OutName.str();
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000540
541 llvm::GlobalVariable *TypeNameGV = CGM.getModule().getNamedGlobal(Name);
542
543 assert(TypeNameGV->hasAvailableExternallyLinkage() &&
544 "Type name has different linkage from type info!");
545
546 // And update its linkage.
547 TypeNameGV->setLinkage(Linkage);
548}
549
John McCallcbfe5022010-08-04 08:34:44 +0000550llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000551 // We want to operate on the canonical type.
552 Ty = CGM.getContext().getCanonicalType(Ty);
553
554 // Check if we've already emitted an RTTI descriptor for this type.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000555 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000556 llvm::raw_svector_ostream Out(OutName);
557 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
558 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000559 StringRef Name = OutName.str();
Anders Carlsson1cbce122011-01-29 19:16:51 +0000560
Anders Carlsson8d145152009-12-20 22:30:54 +0000561 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000562 if (OldGV && !OldGV->isDeclaration()) {
563 maybeUpdateRTTILinkage(CGM, OldGV, Ty);
564
Chris Lattner8b418682012-02-07 00:39:47 +0000565 return llvm::ConstantExpr::getBitCast(OldGV, CGM.Int8PtrTy);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000566 }
John McCallcbfe5022010-08-04 08:34:44 +0000567
Anders Carlsson8d145152009-12-20 22:30:54 +0000568 // Check if there is already an external RTTI descriptor for this type.
John McCallcbfe5022010-08-04 08:34:44 +0000569 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000570 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
Anders Carlsson8d145152009-12-20 22:30:54 +0000571 return GetAddrOfExternalRTTIDescriptor(Ty);
572
John McCallcbfe5022010-08-04 08:34:44 +0000573 // Emit the standard library with external linkage.
574 llvm::GlobalVariable::LinkageTypes Linkage;
575 if (IsStdLib)
576 Linkage = llvm::GlobalValue::ExternalLinkage;
577 else
Anders Carlsson3a717f72011-01-24 02:04:33 +0000578 Linkage = getTypeInfoLinkage(CGM, Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +0000579
580 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000581 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000582
583 // And the name.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000584 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
585
Chris Lattner8b418682012-02-07 00:39:47 +0000586 Fields.push_back(llvm::ConstantExpr::getBitCast(TypeName, CGM.Int8PtrTy));
John McCallcbfe5022010-08-04 08:34:44 +0000587
Anders Carlsson8d145152009-12-20 22:30:54 +0000588 switch (Ty->getTypeClass()) {
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000589#define TYPE(Class, Base)
590#define ABSTRACT_TYPE(Class, Base)
591#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
592#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
593#define DEPENDENT_TYPE(Class, Base) case Type::Class:
594#include "clang/AST/TypeNodes.def"
David Blaikieb219cfc2011-09-23 05:06:16 +0000595 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
Anders Carlsson8d145152009-12-20 22:30:54 +0000596
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000597 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000598 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000599 case Type::Vector:
600 case Type::ExtVector:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000601 case Type::Complex:
602 case Type::BlockPointer:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000603 // Itanium C++ ABI 2.9.5p4:
604 // abi::__fundamental_type_info adds no data members to std::type_info.
605 break;
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000606
607 case Type::LValueReference:
608 case Type::RValueReference:
David Blaikieb219cfc2011-09-23 05:06:16 +0000609 llvm_unreachable("References shouldn't get here");
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000610
Anders Carlsson978ef682009-12-29 21:58:32 +0000611 case Type::ConstantArray:
612 case Type::IncompleteArray:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000613 case Type::VariableArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000614 // Itanium C++ ABI 2.9.5p5:
615 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000616 break;
617
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000618 case Type::FunctionNoProto:
619 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000620 // Itanium C++ ABI 2.9.5p5:
621 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000622 break;
623
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000624 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000625 // Itanium C++ ABI 2.9.5p5:
626 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000627 break;
628
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000629 case Type::Record: {
630 const CXXRecordDecl *RD =
631 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000632 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000633 // We don't need to emit any fields.
634 break;
635 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000636
Anders Carlsson08148092009-12-30 23:47:56 +0000637 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000638 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000639 else
640 BuildVMIClassTypeInfo(RD);
641
642 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000643 }
John McCalle8dc53e2010-08-12 02:17:33 +0000644
645 case Type::ObjCObject:
646 case Type::ObjCInterface:
647 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
648 break;
649
650 case Type::ObjCObjectPointer:
651 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
652 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000653
Anders Carlsson8d145152009-12-20 22:30:54 +0000654 case Type::Pointer:
John McCalle8dc53e2010-08-12 02:17:33 +0000655 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000656 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000657
Anders Carlsson8d145152009-12-20 22:30:54 +0000658 case Type::MemberPointer:
659 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
660 break;
Eli Friedmanb001de72011-10-06 23:00:33 +0000661
662 case Type::Atomic:
663 // No fields, at least for the moment.
664 break;
Anders Carlsson8d145152009-12-20 22:30:54 +0000665 }
666
Chris Lattnerc5cbb902011-06-20 04:01:35 +0000667 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields);
Anders Carlsson8d145152009-12-20 22:30:54 +0000668
669 llvm::GlobalVariable *GV =
670 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
671 /*Constant=*/true, Linkage, Init, Name);
672
673 // If there's already an old global variable, replace it with the new one.
674 if (OldGV) {
675 GV->takeName(OldGV);
676 llvm::Constant *NewPtr =
677 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
678 OldGV->replaceAllUsesWith(NewPtr);
679 OldGV->eraseFromParent();
680 }
John McCallcbfe5022010-08-04 08:34:44 +0000681
682 // GCC only relies on the uniqueness of the type names, not the
683 // type_infos themselves, so we can emit these as hidden symbols.
John McCall279b5eb2010-08-12 23:36:15 +0000684 // But don't do this if we're worried about strict visibility
685 // compatibility.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000686 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) {
687 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
688
689 CGM.setTypeVisibility(GV, RD, CodeGenModule::TVK_ForRTTI);
690 CGM.setTypeVisibility(TypeName, RD, CodeGenModule::TVK_ForRTTIName);
Anders Carlsson907c8282011-01-29 22:10:32 +0000691 } else {
692 Visibility TypeInfoVisibility = DefaultVisibility;
693 if (CGM.getCodeGenOpts().HiddenWeakVTables &&
694 Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
695 TypeInfoVisibility = HiddenVisibility;
Anders Carlsson9a86a132011-01-29 20:36:11 +0000696
Anders Carlsson907c8282011-01-29 22:10:32 +0000697 // The type name should have the same visibility as the type itself.
698 Visibility ExplicitVisibility = Ty->getVisibility();
699 TypeName->setVisibility(CodeGenModule::
700 GetLLVMVisibility(ExplicitVisibility));
701
702 TypeInfoVisibility = minVisibility(TypeInfoVisibility, Ty->getVisibility());
703 GV->setVisibility(CodeGenModule::GetLLVMVisibility(TypeInfoVisibility));
Rafael Espindolab1c65ff2011-01-11 21:44:37 +0000704 }
Anders Carlsson907c8282011-01-29 22:10:32 +0000705
Rafael Espindola57244f62011-01-11 23:55:05 +0000706 GV->setUnnamedAddr(true);
707
Chris Lattner8b418682012-02-07 00:39:47 +0000708 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000709}
710
Anders Carlsson08148092009-12-30 23:47:56 +0000711/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000712/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000713static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000714 unsigned Flags = 0;
715
716 if (Quals.hasConst())
717 Flags |= RTTIBuilder::PTI_Const;
718 if (Quals.hasVolatile())
719 Flags |= RTTIBuilder::PTI_Volatile;
720 if (Quals.hasRestrict())
721 Flags |= RTTIBuilder::PTI_Restrict;
722
723 return Flags;
724}
725
John McCalle8dc53e2010-08-12 02:17:33 +0000726/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
727/// for the given Objective-C object type.
728void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
729 // Drop qualifiers.
730 const Type *T = OT->getBaseType().getTypePtr();
731 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
732
733 // The builtin types are abi::__class_type_infos and don't require
734 // extra fields.
735 if (isa<BuiltinType>(T)) return;
736
737 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
738 ObjCInterfaceDecl *Super = Class->getSuperClass();
739
740 // Root classes are also __class_type_info.
741 if (!Super) return;
742
743 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
744
745 // Everything else is single inheritance.
746 llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
747 Fields.push_back(BaseTypeInfo);
748}
749
Anders Carlssonf64531a2009-12-30 01:00:12 +0000750/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
751/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
752void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
753 // Itanium C++ ABI 2.9.5p6b:
754 // It adds to abi::__class_type_info a single member pointing to the
755 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000756 llvm::Constant *BaseTypeInfo =
757 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
758 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000759}
760
Benjamin Kramer79ba2a62010-10-22 16:48:22 +0000761namespace {
762 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
763 /// a class hierarchy.
764 struct SeenBases {
765 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
766 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
767 };
768}
Anders Carlsson08148092009-12-30 23:47:56 +0000769
770/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
771/// abi::__vmi_class_type_info.
772///
773static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
774 SeenBases &Bases) {
775
776 unsigned Flags = 0;
777
778 const CXXRecordDecl *BaseDecl =
779 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
780
781 if (Base->isVirtual()) {
782 if (Bases.VirtualBases.count(BaseDecl)) {
783 // 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;
789
790 // Mark the virtual base as seen.
791 Bases.VirtualBases.insert(BaseDecl);
792 }
793 } else {
794 if (Bases.NonVirtualBases.count(BaseDecl)) {
795 // If this non-virtual base has been seen before, then the class has non-
796 // diamond shaped repeated inheritance.
797 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
798 } else {
799 if (Bases.VirtualBases.count(BaseDecl))
800 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
801
802 // Mark the non-virtual base as seen.
803 Bases.NonVirtualBases.insert(BaseDecl);
804 }
805 }
806
807 // Walk all bases.
808 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
809 E = BaseDecl->bases_end(); I != E; ++I)
810 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
811
812 return Flags;
813}
814
815static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
816 unsigned Flags = 0;
817 SeenBases Bases;
818
819 // Walk all bases.
820 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
821 E = RD->bases_end(); I != E; ++I)
822 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
823
824 return Flags;
825}
826
827/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
828/// classes with bases that do not satisfy the abi::__si_class_type_info
829/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
830void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000831 llvm::Type *UnsignedIntLTy =
Anders Carlsson08148092009-12-30 23:47:56 +0000832 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
833
834 // Itanium C++ ABI 2.9.5p6c:
835 // __flags is a word with flags describing details about the class
836 // structure, which may be referenced by using the __flags_masks
837 // enumeration. These flags refer to both direct and indirect bases.
838 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000839 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000840
841 // Itanium C++ ABI 2.9.5p6c:
842 // __base_count is a word with the number of direct proper base class
843 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000844 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000845
846 if (!RD->getNumBases())
847 return;
848
Chris Lattner2acc6e32011-07-18 04:24:23 +0000849 llvm::Type *LongLTy =
Anders Carlsson08148092009-12-30 23:47:56 +0000850 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
851
852 // Now add the base class descriptions.
853
854 // Itanium C++ ABI 2.9.5p6c:
855 // __base_info[] is an array of base class descriptions -- one for every
856 // direct proper base. Each description is of the type:
857 //
858 // struct abi::__base_class_type_info {
Eli Friedmana7e68452010-08-22 01:00:03 +0000859 // public:
Anders Carlsson08148092009-12-30 23:47:56 +0000860 // const __class_type_info *__base_type;
861 // long __offset_flags;
862 //
863 // enum __offset_flags_masks {
864 // __virtual_mask = 0x1,
865 // __public_mask = 0x2,
866 // __offset_shift = 8
867 // };
868 // };
869 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
870 E = RD->bases_end(); I != E; ++I) {
871 const CXXBaseSpecifier *Base = I;
872
873 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000874 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000875
876 const CXXRecordDecl *BaseDecl =
877 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
878
879 int64_t OffsetFlags = 0;
880
881 // All but the lower 8 bits of __offset_flags are a signed offset.
882 // For a non-virtual base, this is the offset in the object of the base
883 // subobject. For a virtual base, this is the offset in the virtual table of
884 // the virtual base offset for the virtual base referenced (negative).
Ken Dyckb653d5a2011-04-09 01:09:56 +0000885 CharUnits Offset;
Anders Carlsson08148092009-12-30 23:47:56 +0000886 if (Base->isVirtual())
Ken Dyckb653d5a2011-04-09 01:09:56 +0000887 Offset =
Peter Collingbourne1d2b3172011-09-26 01:56:30 +0000888 CGM.getVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000889 else {
890 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Ken Dyckb653d5a2011-04-09 01:09:56 +0000891 Offset = Layout.getBaseClassOffset(BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000892 };
893
Ken Dyckb653d5a2011-04-09 01:09:56 +0000894 OffsetFlags = Offset.getQuantity() << 8;
Anders Carlsson08148092009-12-30 23:47:56 +0000895
896 // The low-order byte of __offset_flags contains flags, as given by the
897 // masks from the enumeration __offset_flags_masks.
898 if (Base->isVirtual())
899 OffsetFlags |= BCTI_Virtual;
900 if (Base->getAccessSpecifier() == AS_public)
901 OffsetFlags |= BCTI_Public;
902
Anders Carlsson531d55f2009-12-31 17:43:53 +0000903 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000904 }
905}
906
Anders Carlsson8d145152009-12-20 22:30:54 +0000907/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
908/// used for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +0000909void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
Anders Carlssonabd6b092010-06-02 15:44:35 +0000910 Qualifiers Quals;
911 QualType UnqualifiedPointeeTy =
912 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
913
Anders Carlsson8d145152009-12-20 22:30:54 +0000914 // Itanium C++ ABI 2.9.5p7:
915 // __flags is a flag word describing the cv-qualification and other
916 // attributes of the type pointed to
Anders Carlssonabd6b092010-06-02 15:44:35 +0000917 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000918
919 // Itanium C++ ABI 2.9.5p7:
920 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
921 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000922 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000923 Flags |= PTI_Incomplete;
924
Chris Lattner2acc6e32011-07-18 04:24:23 +0000925 llvm::Type *UnsignedIntLTy =
Anders Carlsson8d145152009-12-20 22:30:54 +0000926 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000927 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000928
929 // Itanium C++ ABI 2.9.5p7:
930 // __pointee is a pointer to the std::type_info derivation for the
931 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000932 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000933 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000934 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000935}
936
937/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
938/// struct, used for member pointer types.
939void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
940 QualType PointeeTy = Ty->getPointeeType();
941
Anders Carlssonabd6b092010-06-02 15:44:35 +0000942 Qualifiers Quals;
943 QualType UnqualifiedPointeeTy =
944 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
945
Anders Carlsson8d145152009-12-20 22:30:54 +0000946 // Itanium C++ ABI 2.9.5p7:
947 // __flags is a flag word describing the cv-qualification and other
948 // attributes of the type pointed to.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000949 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000950
951 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000952
953 // Itanium C++ ABI 2.9.5p7:
954 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
955 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000956 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000957 Flags |= PTI_Incomplete;
958
Anders Carlsson8d145152009-12-20 22:30:54 +0000959 if (IsIncompleteClassType(ClassType))
960 Flags |= PTI_ContainingClassIncomplete;
961
Chris Lattner2acc6e32011-07-18 04:24:23 +0000962 llvm::Type *UnsignedIntLTy =
Anders Carlsson8d145152009-12-20 22:30:54 +0000963 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000964 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000965
966 // Itanium C++ ABI 2.9.5p7:
967 // __pointee is a pointer to the std::type_info derivation for the
968 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000969 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000970 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000971 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000972
973 // Itanium C++ ABI 2.9.5p9:
974 // __context is a pointer to an abi::__class_type_info corresponding to the
975 // class type containing the member pointed to
976 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000977 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000978}
979
John McCall9dffe6f2010-04-30 01:15:21 +0000980llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
981 bool ForEH) {
982 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
983 // FIXME: should we even be calling this method if RTTI is disabled
984 // and it's not for EH?
Chris Lattner8b418682012-02-07 00:39:47 +0000985 if (!ForEH && !getContext().getLangOptions().RTTI)
Anders Carlsson31b7f522009-12-11 02:46:30 +0000986 return llvm::Constant::getNullValue(Int8PtrTy);
David Chisnall80558d22011-03-20 21:35:39 +0000987
Chris Lattner8b418682012-02-07 00:39:47 +0000988 if (ForEH && Ty->isObjCObjectPointerType() && !Features.NeXTRuntime)
Peter Collingbournee9265232011-07-27 20:29:46 +0000989 return ObjCRuntime->GetEHType(Ty);
John McCall9dffe6f2010-04-30 01:15:21 +0000990
Anders Carlsson531d55f2009-12-31 17:43:53 +0000991 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000992}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000993
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000994void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
995 QualType PointerType = Context.getPointerType(Type);
996 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
997 RTTIBuilder(*this).BuildTypeInfo(Type, true);
998 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
999 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
1000}
1001
1002void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Anders Carlsson2bd62502010-11-04 05:28:09 +00001003 QualType FundamentalTypes[] = { Context.VoidTy, Context.NullPtrTy,
1004 Context.BoolTy, Context.WCharTy,
1005 Context.CharTy, Context.UnsignedCharTy,
1006 Context.SignedCharTy, Context.ShortTy,
1007 Context.UnsignedShortTy, Context.IntTy,
1008 Context.UnsignedIntTy, Context.LongTy,
1009 Context.UnsignedLongTy, Context.LongLongTy,
1010 Context.UnsignedLongLongTy, Context.FloatTy,
1011 Context.DoubleTy, Context.LongDoubleTy,
1012 Context.Char16Ty, Context.Char32Ty };
Rafael Espindolad1a5c312010-03-27 02:52:14 +00001013 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
1014 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
1015}