blob: 20918920c109ae8b25e6b59da380ba22b126bcd7 [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
Chris Lattner2acc6e32011-07-18 04:24:23 +000029 llvm::Type *Int8PtrTy;
Anders Carlsson531d55f2009-12-31 17:43:53 +000030
31 /// Fields - The fields of the RTTI descriptor currently being built.
Chris Lattner5f9e2722011-07-23 10:55:15 +000032 SmallVector<llvm::Constant *, 16> Fields;
Anders Carlssond6baec82009-12-11 01:27:37 +000033
Anders Carlsson9a86a132011-01-29 20:36:11 +000034 /// GetAddrOfTypeName - Returns the mangled type name of the given type.
35 llvm::GlobalVariable *
36 GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage);
37
Anders Carlsson1d7088d2009-12-17 07:09:17 +000038 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
39 /// descriptor of the given type.
40 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
41
Anders Carlsson046c2942010-04-17 20:15:18 +000042 /// BuildVTablePointer - Build the vtable pointer for the given type.
43 void BuildVTablePointer(const Type *Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +000044
Anders Carlssonf64531a2009-12-30 01:00:12 +000045 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
Anders Carlsson08148092009-12-30 23:47:56 +000046 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
Anders Carlssonf64531a2009-12-30 01:00:12 +000047 void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
48
Anders Carlsson08148092009-12-30 23:47:56 +000049 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
50 /// classes with bases that do not satisfy the abi::__si_class_type_info
51 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
52 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
53
Anders Carlssonf64531a2009-12-30 01:00:12 +000054 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
55 /// for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +000056 void BuildPointerTypeInfo(QualType PointeeTy);
57
58 /// BuildObjCObjectTypeInfo - Build the appropriate kind of
59 /// type_info for an object type.
60 void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +000061
62 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
63 /// struct, used for member pointer types.
64 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
65
Mike Stump2b1bf312009-11-14 14:25:18 +000066public:
Anders Carlsson237f9592011-01-29 22:15:18 +000067 RTTIBuilder(CodeGenModule &CGM) : CGM(CGM),
68 VMContext(CGM.getModule().getContext()),
69 Int8PtrTy(llvm::Type::getInt8PtrTy(VMContext)) { }
Mike Stump2b1bf312009-11-14 14:25:18 +000070
Anders Carlsson8d145152009-12-20 22:30:54 +000071 // Pointer type info flags.
72 enum {
73 /// PTI_Const - Type has const qualifier.
74 PTI_Const = 0x1,
75
76 /// PTI_Volatile - Type has volatile qualifier.
77 PTI_Volatile = 0x2,
78
79 /// PTI_Restrict - Type has restrict qualifier.
80 PTI_Restrict = 0x4,
81
82 /// PTI_Incomplete - Type is incomplete.
83 PTI_Incomplete = 0x8,
84
85 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
86 /// (in pointer to member).
87 PTI_ContainingClassIncomplete = 0x10
88 };
Anders Carlsson08148092009-12-30 23:47:56 +000089
90 // VMI type info flags.
91 enum {
92 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
93 VMI_NonDiamondRepeat = 0x1,
94
95 /// VMI_DiamondShaped - Class is diamond shaped.
96 VMI_DiamondShaped = 0x2
97 };
98
99 // Base class type info flags.
100 enum {
101 /// BCTI_Virtual - Base class is virtual.
102 BCTI_Virtual = 0x1,
103
104 /// BCTI_Public - Base class is public.
105 BCTI_Public = 0x2
106 };
Anders Carlsson531d55f2009-12-31 17:43:53 +0000107
108 /// BuildTypeInfo - Build the RTTI type info struct for the given type.
John McCall9dffe6f2010-04-30 01:15:21 +0000109 ///
110 /// \param Force - true to force the creation of this RTTI value
111 /// \param ForEH - true if this is for exception handling
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000112 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false);
Mike Stump2b1bf312009-11-14 14:25:18 +0000113};
Mike Stump92f2fe22009-12-02 19:07:44 +0000114}
Mike Stump2b1bf312009-11-14 14:25:18 +0000115
Anders Carlsson9a86a132011-01-29 20:36:11 +0000116llvm::GlobalVariable *
117RTTIBuilder::GetAddrOfTypeName(QualType Ty,
118 llvm::GlobalVariable::LinkageTypes Linkage) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000119 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000120 llvm::raw_svector_ostream Out(OutName);
121 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
122 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000123 StringRef Name = OutName.str();
Anders Carlsson9a86a132011-01-29 20:36:11 +0000124
125 // We know that the mangled name of the type starts at index 4 of the
126 // mangled name of the typename, so we can just index into it in order to
127 // get the mangled name of the type.
Chris Lattner94010692012-02-05 02:30:40 +0000128 llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext,
129 Name.substr(4));
Anders Carlsson9a86a132011-01-29 20:36:11 +0000130
131 llvm::GlobalVariable *GV =
132 CGM.CreateOrReplaceCXXRuntimeVariable(Name, Init->getType(), Linkage);
133
134 GV->setInitializer(Init);
135
136 return GV;
137}
138
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000139llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
140 // Mangle the RTTI name.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000141 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000142 llvm::raw_svector_ostream Out(OutName);
143 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
144 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000145 StringRef Name = OutName.str();
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000146
Anders Carlsson8d145152009-12-20 22:30:54 +0000147 // Look for an existing global.
148 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000149
150 if (!GV) {
151 // Create a new global variable.
152 GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
153 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000154 }
155
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000156 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000157}
158
Anders Carlsson8d145152009-12-20 22:30:54 +0000159/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
160/// info for that type is defined in the standard library.
161static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
162 // Itanium C++ ABI 2.9.2:
163 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
164 // the run-time support library. Specifically, the run-time support
165 // library should contain type_info objects for the types X, X* and
Anders Carlsson2bd62502010-11-04 05:28:09 +0000166 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
167 // unsigned char, signed char, short, unsigned short, int, unsigned int,
168 // long, unsigned long, long long, unsigned long long, float, double,
169 // long double, char16_t, char32_t, and the IEEE 754r decimal and
170 // half-precision floating point types.
Anders Carlsson8d145152009-12-20 22:30:54 +0000171 switch (Ty->getKind()) {
172 case BuiltinType::Void:
Anders Carlsson2bd62502010-11-04 05:28:09 +0000173 case BuiltinType::NullPtr:
Anders Carlsson8d145152009-12-20 22:30:54 +0000174 case BuiltinType::Bool:
Chris Lattner3f59c972010-12-25 23:25:43 +0000175 case BuiltinType::WChar_S:
176 case BuiltinType::WChar_U:
Anders Carlsson8d145152009-12-20 22:30:54 +0000177 case BuiltinType::Char_U:
178 case BuiltinType::Char_S:
179 case BuiltinType::UChar:
180 case BuiltinType::SChar:
181 case BuiltinType::Short:
182 case BuiltinType::UShort:
183 case BuiltinType::Int:
184 case BuiltinType::UInt:
185 case BuiltinType::Long:
186 case BuiltinType::ULong:
187 case BuiltinType::LongLong:
188 case BuiltinType::ULongLong:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +0000189 case BuiltinType::Half:
Anders Carlsson8d145152009-12-20 22:30:54 +0000190 case BuiltinType::Float:
191 case BuiltinType::Double:
192 case BuiltinType::LongDouble:
193 case BuiltinType::Char16:
194 case BuiltinType::Char32:
195 case BuiltinType::Int128:
196 case BuiltinType::UInt128:
197 return true;
198
Anders Carlsson8d145152009-12-20 22:30:54 +0000199 case BuiltinType::Dependent:
John McCall2dde35b2011-10-18 22:28:37 +0000200#define BUILTIN_TYPE(Id, SingletonId)
201#define PLACEHOLDER_TYPE(Id, SingletonId) \
202 case BuiltinType::Id:
203#include "clang/AST/BuiltinTypes.def"
John McCall864c0412011-04-26 20:42:42 +0000204 llvm_unreachable("asking for RRTI for a placeholder type!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000205
Anders Carlsson8d145152009-12-20 22:30:54 +0000206 case BuiltinType::ObjCId:
207 case BuiltinType::ObjCClass:
208 case BuiltinType::ObjCSel:
David Blaikieb219cfc2011-09-23 05:06:16 +0000209 llvm_unreachable("FIXME: Objective-C types are unsupported!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000210 }
David Blaikie30263482012-01-20 21:50:17 +0000211
212 llvm_unreachable("Invalid BuiltinType Kind!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000213}
214
215static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
216 QualType PointeeTy = PointerTy->getPointeeType();
217 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
218 if (!BuiltinTy)
219 return false;
220
221 // Check the qualifiers.
222 Qualifiers Quals = PointeeTy.getQualifiers();
223 Quals.removeConst();
224
225 if (!Quals.empty())
226 return false;
227
228 return TypeInfoIsInStandardLibrary(BuiltinTy);
229}
230
John McCallcbfe5022010-08-04 08:34:44 +0000231/// IsStandardLibraryRTTIDescriptor - Returns whether the type
232/// information for the given type exists in the standard library.
233static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000234 // Type info for builtin types is defined in the standard library.
235 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
236 return TypeInfoIsInStandardLibrary(BuiltinTy);
237
238 // Type info for some pointer types to builtin types is defined in the
239 // standard library.
240 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
241 return TypeInfoIsInStandardLibrary(PointerTy);
242
John McCallcbfe5022010-08-04 08:34:44 +0000243 return false;
244}
245
246/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
247/// the given type exists somewhere else, and that we should not emit the type
248/// information in this translation unit. Assumes that it is not a
249/// standard-library type.
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000250static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM, QualType Ty) {
251 ASTContext &Context = CGM.getContext();
252
John McCall9dffe6f2010-04-30 01:15:21 +0000253 // If RTTI is disabled, don't consider key functions.
254 if (!Context.getLangOptions().RTTI) return false;
255
Anders Carlsson8d145152009-12-20 22:30:54 +0000256 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000257 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000258 if (!RD->hasDefinition())
259 return false;
260
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000261 if (!RD->isDynamicClass())
262 return false;
263
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000264 return !CGM.getVTables().ShouldEmitVTableInThisTU(RD);
Anders Carlsson8d145152009-12-20 22:30:54 +0000265 }
266
267 return false;
268}
269
270/// IsIncompleteClassType - Returns whether the given record type is incomplete.
271static bool IsIncompleteClassType(const RecordType *RecordTy) {
John McCall5e1cdac2011-10-07 06:10:15 +0000272 return !RecordTy->getDecl()->isCompleteDefinition();
Anders Carlsson8d145152009-12-20 22:30:54 +0000273}
274
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000275/// ContainsIncompleteClassType - Returns whether the given type contains an
276/// incomplete class type. This is true if
277///
278/// * The given type is an incomplete class type.
279/// * The given type is a pointer type whose pointee type contains an
280/// incomplete class type.
281/// * The given type is a member pointer type whose class is an incomplete
282/// class type.
283/// * The given type is a member pointer type whoise pointee type contains an
284/// incomplete class type.
Anders Carlsson8d145152009-12-20 22:30:54 +0000285/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000286static bool ContainsIncompleteClassType(QualType Ty) {
287 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
288 if (IsIncompleteClassType(RecordTy))
289 return true;
290 }
291
292 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
293 return ContainsIncompleteClassType(PointerTy->getPointeeType());
294
295 if (const MemberPointerType *MemberPointerTy =
296 dyn_cast<MemberPointerType>(Ty)) {
297 // Check if the class type is incomplete.
298 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
299 if (IsIncompleteClassType(ClassType))
300 return true;
301
302 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000303 }
304
305 return false;
306}
307
308/// getTypeInfoLinkage - Return the linkage that the type info and type info
309/// name constants should have for the given type.
Anders Carlsson3a717f72011-01-24 02:04:33 +0000310static llvm::GlobalVariable::LinkageTypes
311getTypeInfoLinkage(CodeGenModule &CGM, QualType Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000312 // Itanium C++ ABI 2.9.5p7:
313 // In addition, it and all of the intermediate abi::__pointer_type_info
314 // structs in the chain down to the abi::__class_type_info for the
315 // incomplete class type must be prevented from resolving to the
316 // corresponding type_info structs for the complete class type, possibly
317 // by making them local static objects. Finally, a dummy class RTTI is
318 // generated for the incomplete type that will not resolve to the final
319 // complete class RTTI (because the latter need not exist), possibly by
320 // making it a local static object.
321 if (ContainsIncompleteClassType(Ty))
322 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000323
Douglas Gregor031b3712010-03-31 00:15:35 +0000324 switch (Ty->getLinkage()) {
325 case NoLinkage:
326 case InternalLinkage:
327 case UniqueExternalLinkage:
328 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson978ef682009-12-29 21:58:32 +0000329
Douglas Gregor031b3712010-03-31 00:15:35 +0000330 case ExternalLinkage:
Anders Carlssone34e3aa2011-01-24 02:12:11 +0000331 if (!CGM.getLangOptions().RTTI) {
332 // RTTI is not enabled, which means that this type info struct is going
333 // to be used for exception handling. Give it linkonce_odr linkage.
334 return llvm::GlobalValue::LinkOnceODRLinkage;
335 }
336
Douglas Gregor031b3712010-03-31 00:15:35 +0000337 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
338 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +0000339 if (RD->hasAttr<WeakAttr>())
340 return llvm::GlobalValue::WeakODRLinkage;
Douglas Gregor031b3712010-03-31 00:15:35 +0000341 if (RD->isDynamicClass())
Anders Carlsson3a717f72011-01-24 02:04:33 +0000342 return CGM.getVTableLinkage(RD);
Mike Stumpc8f76f52009-12-24 01:10:27 +0000343 }
Douglas Gregor031b3712010-03-31 00:15:35 +0000344
Anders Carlssonf502d932011-01-24 00:46:19 +0000345 return llvm::GlobalValue::LinkOnceODRLinkage;
Mike Stumpc8f76f52009-12-24 01:10:27 +0000346 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000347
David Blaikie30263482012-01-20 21:50:17 +0000348 llvm_unreachable("Invalid linkage!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000349}
350
Anders Carlssonf64531a2009-12-30 01:00:12 +0000351// CanUseSingleInheritance - Return whether the given record decl has a "single,
352// public, non-virtual base at offset zero (i.e. the derived class is dynamic
353// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
354static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
355 // Check the number of bases.
356 if (RD->getNumBases() != 1)
357 return false;
358
359 // Get the base.
360 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
361
362 // Check that the base is not virtual.
363 if (Base->isVirtual())
364 return false;
365
366 // Check that the base is public.
367 if (Base->getAccessSpecifier() != AS_public)
368 return false;
369
370 // Check that the class is dynamic iff the base is.
371 const CXXRecordDecl *BaseDecl =
372 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
373 if (!BaseDecl->isEmpty() &&
374 BaseDecl->isDynamicClass() != RD->isDynamicClass())
375 return false;
376
377 return true;
378}
379
Anders Carlsson046c2942010-04-17 20:15:18 +0000380void RTTIBuilder::BuildVTablePointer(const Type *Ty) {
John McCalle8dc53e2010-08-12 02:17:33 +0000381 // abi::__class_type_info.
382 static const char * const ClassTypeInfo =
383 "_ZTVN10__cxxabiv117__class_type_infoE";
384 // abi::__si_class_type_info.
385 static const char * const SIClassTypeInfo =
386 "_ZTVN10__cxxabiv120__si_class_type_infoE";
387 // abi::__vmi_class_type_info.
388 static const char * const VMIClassTypeInfo =
389 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
390
Eli Friedman1cf26f52010-08-11 20:41:51 +0000391 const char *VTableName = 0;
Anders Carlsson8d145152009-12-20 22:30:54 +0000392
393 switch (Ty->getTypeClass()) {
Eli Friedman1cf26f52010-08-11 20:41:51 +0000394#define TYPE(Class, Base)
395#define ABSTRACT_TYPE(Class, Base)
396#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
397#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
398#define DEPENDENT_TYPE(Class, Base) case Type::Class:
399#include "clang/AST/TypeNodes.def"
David Blaikieb219cfc2011-09-23 05:06:16 +0000400 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
Eli Friedman1cf26f52010-08-11 20:41:51 +0000401
402 case Type::LValueReference:
403 case Type::RValueReference:
David Blaikieb219cfc2011-09-23 05:06:16 +0000404 llvm_unreachable("References shouldn't get here");
Anders Carlsson978ef682009-12-29 21:58:32 +0000405
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000406 case Type::Builtin:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000407 // GCC treats vector and complex types as fundamental types.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000408 case Type::Vector:
409 case Type::ExtVector:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000410 case Type::Complex:
Eli Friedmanb001de72011-10-06 23:00:33 +0000411 case Type::Atomic:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000412 // FIXME: GCC treats block pointers as fundamental types?!
413 case Type::BlockPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000414 // abi::__fundamental_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000415 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000416 break;
417
Anders Carlsson978ef682009-12-29 21:58:32 +0000418 case Type::ConstantArray:
419 case Type::IncompleteArray:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000420 case Type::VariableArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000421 // abi::__array_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000422 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000423 break;
424
425 case Type::FunctionNoProto:
426 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000427 // abi::__function_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000428 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000429 break;
430
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000431 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000432 // abi::__enum_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000433 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000434 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000435
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000436 case Type::Record: {
437 const CXXRecordDecl *RD =
438 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000439
John McCall86ff3082010-02-04 22:26:26 +0000440 if (!RD->hasDefinition() || !RD->getNumBases()) {
John McCalle8dc53e2010-08-12 02:17:33 +0000441 VTableName = ClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000442 } else if (CanUseSingleInheritance(RD)) {
John McCalle8dc53e2010-08-12 02:17:33 +0000443 VTableName = SIClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000444 } else {
John McCalle8dc53e2010-08-12 02:17:33 +0000445 VTableName = VMIClassTypeInfo;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000446 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000447
448 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000449 }
450
Eli Friedman1cf26f52010-08-11 20:41:51 +0000451 case Type::ObjCObject:
John McCalle8dc53e2010-08-12 02:17:33 +0000452 // Ignore protocol qualifiers.
453 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
454
455 // Handle id and Class.
456 if (isa<BuiltinType>(Ty)) {
457 VTableName = ClassTypeInfo;
458 break;
459 }
460
461 assert(isa<ObjCInterfaceType>(Ty));
462 // Fall through.
463
Eli Friedman1cf26f52010-08-11 20:41:51 +0000464 case Type::ObjCInterface:
John McCalle8dc53e2010-08-12 02:17:33 +0000465 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
466 VTableName = SIClassTypeInfo;
467 } else {
468 VTableName = ClassTypeInfo;
469 }
Eli Friedman1cf26f52010-08-11 20:41:51 +0000470 break;
471
John McCalle8dc53e2010-08-12 02:17:33 +0000472 case Type::ObjCObjectPointer:
Anders Carlsson8d145152009-12-20 22:30:54 +0000473 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000474 // abi::__pointer_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000475 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000476 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000477
Anders Carlsson8d145152009-12-20 22:30:54 +0000478 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000479 // abi::__pointer_to_member_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000480 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000481 break;
482 }
483
Anders Carlsson046c2942010-04-17 20:15:18 +0000484 llvm::Constant *VTable =
485 CGM.getModule().getOrInsertGlobal(VTableName, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000486
Chris Lattner2acc6e32011-07-18 04:24:23 +0000487 llvm::Type *PtrDiffTy =
Anders Carlsson8d145152009-12-20 22:30:54 +0000488 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
489
490 // The vtable address point is 2.
491 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Jay Foada5c04342011-07-21 14:31:17 +0000492 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Two);
Anders Carlsson046c2942010-04-17 20:15:18 +0000493 VTable = llvm::ConstantExpr::getBitCast(VTable, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000494
Anders Carlsson046c2942010-04-17 20:15:18 +0000495 Fields.push_back(VTable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000496}
497
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000498// maybeUpdateRTTILinkage - Will update the linkage of the RTTI data structures
499// from available_externally to the correct linkage if necessary. An example of
500// this is:
501//
502// struct A {
503// virtual void f();
504// };
505//
506// const std::type_info &g() {
507// return typeid(A);
508// }
509//
510// void A::f() { }
511//
512// When we're generating the typeid(A) expression, we do not yet know that
513// A's key function is defined in this translation unit, so we will give the
514// typeinfo and typename structures available_externally linkage. When A::f
515// forces the vtable to be generated, we need to change the linkage of the
516// typeinfo and typename structs, otherwise we'll end up with undefined
517// externals when linking.
518static void
519maybeUpdateRTTILinkage(CodeGenModule &CGM, llvm::GlobalVariable *GV,
520 QualType Ty) {
521 // We're only interested in globals with available_externally linkage.
522 if (!GV->hasAvailableExternallyLinkage())
523 return;
524
525 // Get the real linkage for the type.
526 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(CGM, Ty);
527
528 // If variable is supposed to have available_externally linkage, we don't
529 // need to do anything.
530 if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
531 return;
532
533 // Update the typeinfo linkage.
534 GV->setLinkage(Linkage);
535
536 // Get the typename global.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000537 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000538 llvm::raw_svector_ostream Out(OutName);
539 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
540 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000541 StringRef Name = OutName.str();
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000542
543 llvm::GlobalVariable *TypeNameGV = CGM.getModule().getNamedGlobal(Name);
544
545 assert(TypeNameGV->hasAvailableExternallyLinkage() &&
546 "Type name has different linkage from type info!");
547
548 // And update its linkage.
549 TypeNameGV->setLinkage(Linkage);
550}
551
John McCallcbfe5022010-08-04 08:34:44 +0000552llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000553 // We want to operate on the canonical type.
554 Ty = CGM.getContext().getCanonicalType(Ty);
555
556 // Check if we've already emitted an RTTI descriptor for this type.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000557 SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000558 llvm::raw_svector_ostream Out(OutName);
559 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
560 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000561 StringRef Name = OutName.str();
Anders Carlsson1cbce122011-01-29 19:16:51 +0000562
Anders Carlsson8d145152009-12-20 22:30:54 +0000563 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000564 if (OldGV && !OldGV->isDeclaration()) {
565 maybeUpdateRTTILinkage(CGM, OldGV, Ty);
566
Anders Carlsson8d145152009-12-20 22:30:54 +0000567 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000568 }
John McCallcbfe5022010-08-04 08:34:44 +0000569
Anders Carlsson8d145152009-12-20 22:30:54 +0000570 // Check if there is already an external RTTI descriptor for this type.
John McCallcbfe5022010-08-04 08:34:44 +0000571 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000572 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
Anders Carlsson8d145152009-12-20 22:30:54 +0000573 return GetAddrOfExternalRTTIDescriptor(Ty);
574
John McCallcbfe5022010-08-04 08:34:44 +0000575 // Emit the standard library with external linkage.
576 llvm::GlobalVariable::LinkageTypes Linkage;
577 if (IsStdLib)
578 Linkage = llvm::GlobalValue::ExternalLinkage;
579 else
Anders Carlsson3a717f72011-01-24 02:04:33 +0000580 Linkage = getTypeInfoLinkage(CGM, Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +0000581
582 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000583 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000584
585 // And the name.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000586 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
587
Chris Lattner2acc6e32011-07-18 04:24:23 +0000588 llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson907c8282011-01-29 22:10:32 +0000589 Fields.push_back(llvm::ConstantExpr::getBitCast(TypeName, Int8PtrTy));
John McCallcbfe5022010-08-04 08:34:44 +0000590
Anders Carlsson8d145152009-12-20 22:30:54 +0000591 switch (Ty->getTypeClass()) {
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000592#define TYPE(Class, Base)
593#define ABSTRACT_TYPE(Class, Base)
594#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
595#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
596#define DEPENDENT_TYPE(Class, Base) case Type::Class:
597#include "clang/AST/TypeNodes.def"
David Blaikieb219cfc2011-09-23 05:06:16 +0000598 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
Anders Carlsson8d145152009-12-20 22:30:54 +0000599
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000600 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000601 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000602 case Type::Vector:
603 case Type::ExtVector:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000604 case Type::Complex:
605 case Type::BlockPointer:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000606 // Itanium C++ ABI 2.9.5p4:
607 // abi::__fundamental_type_info adds no data members to std::type_info.
608 break;
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000609
610 case Type::LValueReference:
611 case Type::RValueReference:
David Blaikieb219cfc2011-09-23 05:06:16 +0000612 llvm_unreachable("References shouldn't get here");
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000613
Anders Carlsson978ef682009-12-29 21:58:32 +0000614 case Type::ConstantArray:
615 case Type::IncompleteArray:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000616 case Type::VariableArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000617 // Itanium C++ ABI 2.9.5p5:
618 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000619 break;
620
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000621 case Type::FunctionNoProto:
622 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000623 // Itanium C++ ABI 2.9.5p5:
624 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000625 break;
626
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000627 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000628 // Itanium C++ ABI 2.9.5p5:
629 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000630 break;
631
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000632 case Type::Record: {
633 const CXXRecordDecl *RD =
634 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000635 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000636 // We don't need to emit any fields.
637 break;
638 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000639
Anders Carlsson08148092009-12-30 23:47:56 +0000640 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000641 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000642 else
643 BuildVMIClassTypeInfo(RD);
644
645 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000646 }
John McCalle8dc53e2010-08-12 02:17:33 +0000647
648 case Type::ObjCObject:
649 case Type::ObjCInterface:
650 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
651 break;
652
653 case Type::ObjCObjectPointer:
654 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
655 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000656
Anders Carlsson8d145152009-12-20 22:30:54 +0000657 case Type::Pointer:
John McCalle8dc53e2010-08-12 02:17:33 +0000658 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000659 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000660
Anders Carlsson8d145152009-12-20 22:30:54 +0000661 case Type::MemberPointer:
662 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
663 break;
Eli Friedmanb001de72011-10-06 23:00:33 +0000664
665 case Type::Atomic:
666 // No fields, at least for the moment.
667 break;
Anders Carlsson8d145152009-12-20 22:30:54 +0000668 }
669
Chris Lattnerc5cbb902011-06-20 04:01:35 +0000670 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields);
Anders Carlsson8d145152009-12-20 22:30:54 +0000671
672 llvm::GlobalVariable *GV =
673 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
674 /*Constant=*/true, Linkage, Init, Name);
675
676 // If there's already an old global variable, replace it with the new one.
677 if (OldGV) {
678 GV->takeName(OldGV);
679 llvm::Constant *NewPtr =
680 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
681 OldGV->replaceAllUsesWith(NewPtr);
682 OldGV->eraseFromParent();
683 }
John McCallcbfe5022010-08-04 08:34:44 +0000684
685 // GCC only relies on the uniqueness of the type names, not the
686 // type_infos themselves, so we can emit these as hidden symbols.
John McCall279b5eb2010-08-12 23:36:15 +0000687 // But don't do this if we're worried about strict visibility
688 // compatibility.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000689 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) {
690 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
691
692 CGM.setTypeVisibility(GV, RD, CodeGenModule::TVK_ForRTTI);
693 CGM.setTypeVisibility(TypeName, RD, CodeGenModule::TVK_ForRTTIName);
Anders Carlsson907c8282011-01-29 22:10:32 +0000694 } else {
695 Visibility TypeInfoVisibility = DefaultVisibility;
696 if (CGM.getCodeGenOpts().HiddenWeakVTables &&
697 Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
698 TypeInfoVisibility = HiddenVisibility;
Anders Carlsson9a86a132011-01-29 20:36:11 +0000699
Anders Carlsson907c8282011-01-29 22:10:32 +0000700 // The type name should have the same visibility as the type itself.
701 Visibility ExplicitVisibility = Ty->getVisibility();
702 TypeName->setVisibility(CodeGenModule::
703 GetLLVMVisibility(ExplicitVisibility));
704
705 TypeInfoVisibility = minVisibility(TypeInfoVisibility, Ty->getVisibility());
706 GV->setVisibility(CodeGenModule::GetLLVMVisibility(TypeInfoVisibility));
Rafael Espindolab1c65ff2011-01-11 21:44:37 +0000707 }
Anders Carlsson907c8282011-01-29 22:10:32 +0000708
Rafael Espindola57244f62011-01-11 23:55:05 +0000709 GV->setUnnamedAddr(true);
710
Anders Carlsson8d145152009-12-20 22:30:54 +0000711 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
712}
713
Anders Carlsson08148092009-12-30 23:47:56 +0000714/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000715/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000716static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000717 unsigned Flags = 0;
718
719 if (Quals.hasConst())
720 Flags |= RTTIBuilder::PTI_Const;
721 if (Quals.hasVolatile())
722 Flags |= RTTIBuilder::PTI_Volatile;
723 if (Quals.hasRestrict())
724 Flags |= RTTIBuilder::PTI_Restrict;
725
726 return Flags;
727}
728
John McCalle8dc53e2010-08-12 02:17:33 +0000729/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
730/// for the given Objective-C object type.
731void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
732 // Drop qualifiers.
733 const Type *T = OT->getBaseType().getTypePtr();
734 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
735
736 // The builtin types are abi::__class_type_infos and don't require
737 // extra fields.
738 if (isa<BuiltinType>(T)) return;
739
740 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
741 ObjCInterfaceDecl *Super = Class->getSuperClass();
742
743 // Root classes are also __class_type_info.
744 if (!Super) return;
745
746 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
747
748 // Everything else is single inheritance.
749 llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
750 Fields.push_back(BaseTypeInfo);
751}
752
Anders Carlssonf64531a2009-12-30 01:00:12 +0000753/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
754/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
755void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
756 // Itanium C++ ABI 2.9.5p6b:
757 // It adds to abi::__class_type_info a single member pointing to the
758 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000759 llvm::Constant *BaseTypeInfo =
760 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
761 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000762}
763
Benjamin Kramer79ba2a62010-10-22 16:48:22 +0000764namespace {
765 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
766 /// a class hierarchy.
767 struct SeenBases {
768 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
769 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
770 };
771}
Anders Carlsson08148092009-12-30 23:47:56 +0000772
773/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
774/// abi::__vmi_class_type_info.
775///
776static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
777 SeenBases &Bases) {
778
779 unsigned Flags = 0;
780
781 const CXXRecordDecl *BaseDecl =
782 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
783
784 if (Base->isVirtual()) {
785 if (Bases.VirtualBases.count(BaseDecl)) {
786 // If this virtual base has been seen before, then the class is diamond
787 // shaped.
788 Flags |= RTTIBuilder::VMI_DiamondShaped;
789 } else {
790 if (Bases.NonVirtualBases.count(BaseDecl))
791 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
792
793 // Mark the virtual base as seen.
794 Bases.VirtualBases.insert(BaseDecl);
795 }
796 } else {
797 if (Bases.NonVirtualBases.count(BaseDecl)) {
798 // If this non-virtual base has been seen before, then the class has non-
799 // diamond shaped repeated inheritance.
800 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
801 } else {
802 if (Bases.VirtualBases.count(BaseDecl))
803 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
804
805 // Mark the non-virtual base as seen.
806 Bases.NonVirtualBases.insert(BaseDecl);
807 }
808 }
809
810 // Walk all bases.
811 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
812 E = BaseDecl->bases_end(); I != E; ++I)
813 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
814
815 return Flags;
816}
817
818static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
819 unsigned Flags = 0;
820 SeenBases Bases;
821
822 // Walk all bases.
823 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
824 E = RD->bases_end(); I != E; ++I)
825 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
826
827 return Flags;
828}
829
830/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
831/// classes with bases that do not satisfy the abi::__si_class_type_info
832/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
833void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000834 llvm::Type *UnsignedIntLTy =
Anders Carlsson08148092009-12-30 23:47:56 +0000835 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
836
837 // Itanium C++ ABI 2.9.5p6c:
838 // __flags is a word with flags describing details about the class
839 // structure, which may be referenced by using the __flags_masks
840 // enumeration. These flags refer to both direct and indirect bases.
841 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000842 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000843
844 // Itanium C++ ABI 2.9.5p6c:
845 // __base_count is a word with the number of direct proper base class
846 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000847 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000848
849 if (!RD->getNumBases())
850 return;
851
Chris Lattner2acc6e32011-07-18 04:24:23 +0000852 llvm::Type *LongLTy =
Anders Carlsson08148092009-12-30 23:47:56 +0000853 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
854
855 // Now add the base class descriptions.
856
857 // Itanium C++ ABI 2.9.5p6c:
858 // __base_info[] is an array of base class descriptions -- one for every
859 // direct proper base. Each description is of the type:
860 //
861 // struct abi::__base_class_type_info {
Eli Friedmana7e68452010-08-22 01:00:03 +0000862 // public:
Anders Carlsson08148092009-12-30 23:47:56 +0000863 // const __class_type_info *__base_type;
864 // long __offset_flags;
865 //
866 // enum __offset_flags_masks {
867 // __virtual_mask = 0x1,
868 // __public_mask = 0x2,
869 // __offset_shift = 8
870 // };
871 // };
872 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
873 E = RD->bases_end(); I != E; ++I) {
874 const CXXBaseSpecifier *Base = I;
875
876 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000877 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000878
879 const CXXRecordDecl *BaseDecl =
880 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
881
882 int64_t OffsetFlags = 0;
883
884 // All but the lower 8 bits of __offset_flags are a signed offset.
885 // For a non-virtual base, this is the offset in the object of the base
886 // subobject. For a virtual base, this is the offset in the virtual table of
887 // the virtual base offset for the virtual base referenced (negative).
Ken Dyckb653d5a2011-04-09 01:09:56 +0000888 CharUnits Offset;
Anders Carlsson08148092009-12-30 23:47:56 +0000889 if (Base->isVirtual())
Ken Dyckb653d5a2011-04-09 01:09:56 +0000890 Offset =
Peter Collingbourne1d2b3172011-09-26 01:56:30 +0000891 CGM.getVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000892 else {
893 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Ken Dyckb653d5a2011-04-09 01:09:56 +0000894 Offset = Layout.getBaseClassOffset(BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000895 };
896
Ken Dyckb653d5a2011-04-09 01:09:56 +0000897 OffsetFlags = Offset.getQuantity() << 8;
Anders Carlsson08148092009-12-30 23:47:56 +0000898
899 // The low-order byte of __offset_flags contains flags, as given by the
900 // masks from the enumeration __offset_flags_masks.
901 if (Base->isVirtual())
902 OffsetFlags |= BCTI_Virtual;
903 if (Base->getAccessSpecifier() == AS_public)
904 OffsetFlags |= BCTI_Public;
905
Anders Carlsson531d55f2009-12-31 17:43:53 +0000906 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000907 }
908}
909
Anders Carlsson8d145152009-12-20 22:30:54 +0000910/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
911/// used for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +0000912void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
Anders Carlssonabd6b092010-06-02 15:44:35 +0000913 Qualifiers Quals;
914 QualType UnqualifiedPointeeTy =
915 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
916
Anders Carlsson8d145152009-12-20 22:30:54 +0000917 // Itanium C++ ABI 2.9.5p7:
918 // __flags is a flag word describing the cv-qualification and other
919 // attributes of the type pointed to
Anders Carlssonabd6b092010-06-02 15:44:35 +0000920 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000921
922 // Itanium C++ ABI 2.9.5p7:
923 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
924 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000925 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000926 Flags |= PTI_Incomplete;
927
Chris Lattner2acc6e32011-07-18 04:24:23 +0000928 llvm::Type *UnsignedIntLTy =
Anders Carlsson8d145152009-12-20 22:30:54 +0000929 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000930 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000931
932 // Itanium C++ ABI 2.9.5p7:
933 // __pointee is a pointer to the std::type_info derivation for the
934 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000935 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000936 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000937 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000938}
939
940/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
941/// struct, used for member pointer types.
942void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
943 QualType PointeeTy = Ty->getPointeeType();
944
Anders Carlssonabd6b092010-06-02 15:44:35 +0000945 Qualifiers Quals;
946 QualType UnqualifiedPointeeTy =
947 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
948
Anders Carlsson8d145152009-12-20 22:30:54 +0000949 // Itanium C++ ABI 2.9.5p7:
950 // __flags is a flag word describing the cv-qualification and other
951 // attributes of the type pointed to.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000952 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000953
954 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000955
956 // Itanium C++ ABI 2.9.5p7:
957 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
958 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000959 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000960 Flags |= PTI_Incomplete;
961
Anders Carlsson8d145152009-12-20 22:30:54 +0000962 if (IsIncompleteClassType(ClassType))
963 Flags |= PTI_ContainingClassIncomplete;
964
Chris Lattner2acc6e32011-07-18 04:24:23 +0000965 llvm::Type *UnsignedIntLTy =
Anders Carlsson8d145152009-12-20 22:30:54 +0000966 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000967 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000968
969 // Itanium C++ ABI 2.9.5p7:
970 // __pointee is a pointer to the std::type_info derivation for the
971 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000972 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000973 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000974 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000975
976 // Itanium C++ ABI 2.9.5p9:
977 // __context is a pointer to an abi::__class_type_info corresponding to the
978 // class type containing the member pointed to
979 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000980 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000981}
982
John McCall9dffe6f2010-04-30 01:15:21 +0000983llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
984 bool ForEH) {
985 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
986 // FIXME: should we even be calling this method if RTTI is disabled
987 // and it's not for EH?
988 if (!ForEH && !getContext().getLangOptions().RTTI) {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000989 llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000990 return llvm::Constant::getNullValue(Int8PtrTy);
991 }
David Chisnall80558d22011-03-20 21:35:39 +0000992
993 if (ForEH && Ty->isObjCObjectPointerType() && !Features.NeXTRuntime) {
Peter Collingbournee9265232011-07-27 20:29:46 +0000994 return ObjCRuntime->GetEHType(Ty);
David Chisnall80558d22011-03-20 21:35:39 +0000995 }
John McCall9dffe6f2010-04-30 01:15:21 +0000996
Anders Carlsson531d55f2009-12-31 17:43:53 +0000997 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000998}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000999
Rafael Espindolad1a5c312010-03-27 02:52:14 +00001000void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
1001 QualType PointerType = Context.getPointerType(Type);
1002 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
1003 RTTIBuilder(*this).BuildTypeInfo(Type, true);
1004 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
1005 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
1006}
1007
1008void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Anders Carlsson2bd62502010-11-04 05:28:09 +00001009 QualType FundamentalTypes[] = { Context.VoidTy, Context.NullPtrTy,
1010 Context.BoolTy, Context.WCharTy,
1011 Context.CharTy, Context.UnsignedCharTy,
1012 Context.SignedCharTy, Context.ShortTy,
1013 Context.UnsignedShortTy, Context.IntTy,
1014 Context.UnsignedIntTy, Context.LongTy,
1015 Context.UnsignedLongTy, Context.LongLongTy,
1016 Context.UnsignedLongLongTy, Context.FloatTy,
1017 Context.DoubleTy, Context.LongDoubleTy,
1018 Context.Char16Ty, Context.Char32Ty };
Rafael Espindolad1a5c312010-03-27 02:52:14 +00001019 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
1020 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
1021}