blob: dbf99fa5d76015793d7dedcbf1a8fa233d2bf318 [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) {
119 llvm::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.
128 llvm::Constant *Init = llvm::ConstantArray::get(VMContext, Name.substr(4));
129
130 llvm::GlobalVariable *GV =
131 CGM.CreateOrReplaceCXXRuntimeVariable(Name, Init->getType(), Linkage);
132
133 GV->setInitializer(Init);
134
135 return GV;
136}
137
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000138llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
139 // Mangle the RTTI name.
140 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000141 llvm::raw_svector_ostream Out(OutName);
142 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
143 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000144 StringRef Name = OutName.str();
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000145
Anders Carlsson8d145152009-12-20 22:30:54 +0000146 // Look for an existing global.
147 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000148
149 if (!GV) {
150 // Create a new global variable.
151 GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
152 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000153 }
154
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000155 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000156}
157
Anders Carlsson8d145152009-12-20 22:30:54 +0000158/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
159/// info for that type is defined in the standard library.
160static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
161 // Itanium C++ ABI 2.9.2:
162 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
163 // the run-time support library. Specifically, the run-time support
164 // library should contain type_info objects for the types X, X* and
Anders Carlsson2bd62502010-11-04 05:28:09 +0000165 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
166 // unsigned char, signed char, short, unsigned short, int, unsigned int,
167 // long, unsigned long, long long, unsigned long long, float, double,
168 // long double, char16_t, char32_t, and the IEEE 754r decimal and
169 // half-precision floating point types.
Anders Carlsson8d145152009-12-20 22:30:54 +0000170 switch (Ty->getKind()) {
171 case BuiltinType::Void:
Anders Carlsson2bd62502010-11-04 05:28:09 +0000172 case BuiltinType::NullPtr:
Anders Carlsson8d145152009-12-20 22:30:54 +0000173 case BuiltinType::Bool:
Chris Lattner3f59c972010-12-25 23:25:43 +0000174 case BuiltinType::WChar_S:
175 case BuiltinType::WChar_U:
Anders Carlsson8d145152009-12-20 22:30:54 +0000176 case BuiltinType::Char_U:
177 case BuiltinType::Char_S:
178 case BuiltinType::UChar:
179 case BuiltinType::SChar:
180 case BuiltinType::Short:
181 case BuiltinType::UShort:
182 case BuiltinType::Int:
183 case BuiltinType::UInt:
184 case BuiltinType::Long:
185 case BuiltinType::ULong:
186 case BuiltinType::LongLong:
187 case BuiltinType::ULongLong:
188 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
197 case BuiltinType::Overload:
198 case BuiltinType::Dependent:
John McCall864c0412011-04-26 20:42:42 +0000199 case BuiltinType::BoundMember:
John McCall1de4d4e2011-04-07 08:22:57 +0000200 case BuiltinType::UnknownAny:
John McCall864c0412011-04-26 20:42:42 +0000201 llvm_unreachable("asking for RRTI for a placeholder type!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000202
Anders Carlsson8d145152009-12-20 22:30:54 +0000203 case BuiltinType::ObjCId:
204 case BuiltinType::ObjCClass:
205 case BuiltinType::ObjCSel:
David Blaikieb219cfc2011-09-23 05:06:16 +0000206 llvm_unreachable("FIXME: Objective-C types are unsupported!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000207 }
208
209 // Silent gcc.
210 return false;
211}
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());
337 if (RD->isDynamicClass())
Anders Carlsson3a717f72011-01-24 02:04:33 +0000338 return CGM.getVTableLinkage(RD);
Mike Stumpc8f76f52009-12-24 01:10:27 +0000339 }
Douglas Gregor031b3712010-03-31 00:15:35 +0000340
Anders Carlssonf502d932011-01-24 00:46:19 +0000341 return llvm::GlobalValue::LinkOnceODRLinkage;
Mike Stumpc8f76f52009-12-24 01:10:27 +0000342 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000343
Anders Carlssonf502d932011-01-24 00:46:19 +0000344 return llvm::GlobalValue::LinkOnceODRLinkage;
Anders Carlsson8d145152009-12-20 22:30:54 +0000345}
346
Anders Carlssonf64531a2009-12-30 01:00:12 +0000347// CanUseSingleInheritance - Return whether the given record decl has a "single,
348// public, non-virtual base at offset zero (i.e. the derived class is dynamic
349// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
350static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
351 // Check the number of bases.
352 if (RD->getNumBases() != 1)
353 return false;
354
355 // Get the base.
356 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
357
358 // Check that the base is not virtual.
359 if (Base->isVirtual())
360 return false;
361
362 // Check that the base is public.
363 if (Base->getAccessSpecifier() != AS_public)
364 return false;
365
366 // Check that the class is dynamic iff the base is.
367 const CXXRecordDecl *BaseDecl =
368 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
369 if (!BaseDecl->isEmpty() &&
370 BaseDecl->isDynamicClass() != RD->isDynamicClass())
371 return false;
372
373 return true;
374}
375
Anders Carlsson046c2942010-04-17 20:15:18 +0000376void RTTIBuilder::BuildVTablePointer(const Type *Ty) {
John McCalle8dc53e2010-08-12 02:17:33 +0000377 // abi::__class_type_info.
378 static const char * const ClassTypeInfo =
379 "_ZTVN10__cxxabiv117__class_type_infoE";
380 // abi::__si_class_type_info.
381 static const char * const SIClassTypeInfo =
382 "_ZTVN10__cxxabiv120__si_class_type_infoE";
383 // abi::__vmi_class_type_info.
384 static const char * const VMIClassTypeInfo =
385 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
386
Eli Friedman1cf26f52010-08-11 20:41:51 +0000387 const char *VTableName = 0;
Anders Carlsson8d145152009-12-20 22:30:54 +0000388
389 switch (Ty->getTypeClass()) {
Eli Friedman1cf26f52010-08-11 20:41:51 +0000390#define TYPE(Class, Base)
391#define ABSTRACT_TYPE(Class, Base)
392#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
393#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
394#define DEPENDENT_TYPE(Class, Base) case Type::Class:
395#include "clang/AST/TypeNodes.def"
David Blaikieb219cfc2011-09-23 05:06:16 +0000396 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
Eli Friedman1cf26f52010-08-11 20:41:51 +0000397
398 case Type::LValueReference:
399 case Type::RValueReference:
David Blaikieb219cfc2011-09-23 05:06:16 +0000400 llvm_unreachable("References shouldn't get here");
Anders Carlsson978ef682009-12-29 21:58:32 +0000401
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000402 case Type::Builtin:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000403 // GCC treats vector and complex types as fundamental types.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000404 case Type::Vector:
405 case Type::ExtVector:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000406 case Type::Complex:
Eli Friedmanb001de72011-10-06 23:00:33 +0000407 case Type::Atomic:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000408 // FIXME: GCC treats block pointers as fundamental types?!
409 case Type::BlockPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000410 // abi::__fundamental_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000411 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000412 break;
413
Anders Carlsson978ef682009-12-29 21:58:32 +0000414 case Type::ConstantArray:
415 case Type::IncompleteArray:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000416 case Type::VariableArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000417 // abi::__array_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000418 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000419 break;
420
421 case Type::FunctionNoProto:
422 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000423 // abi::__function_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000424 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000425 break;
426
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000427 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000428 // abi::__enum_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000429 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000430 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000431
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000432 case Type::Record: {
433 const CXXRecordDecl *RD =
434 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000435
John McCall86ff3082010-02-04 22:26:26 +0000436 if (!RD->hasDefinition() || !RD->getNumBases()) {
John McCalle8dc53e2010-08-12 02:17:33 +0000437 VTableName = ClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000438 } else if (CanUseSingleInheritance(RD)) {
John McCalle8dc53e2010-08-12 02:17:33 +0000439 VTableName = SIClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000440 } else {
John McCalle8dc53e2010-08-12 02:17:33 +0000441 VTableName = VMIClassTypeInfo;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000442 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000443
444 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000445 }
446
Eli Friedman1cf26f52010-08-11 20:41:51 +0000447 case Type::ObjCObject:
John McCalle8dc53e2010-08-12 02:17:33 +0000448 // Ignore protocol qualifiers.
449 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
450
451 // Handle id and Class.
452 if (isa<BuiltinType>(Ty)) {
453 VTableName = ClassTypeInfo;
454 break;
455 }
456
457 assert(isa<ObjCInterfaceType>(Ty));
458 // Fall through.
459
Eli Friedman1cf26f52010-08-11 20:41:51 +0000460 case Type::ObjCInterface:
John McCalle8dc53e2010-08-12 02:17:33 +0000461 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
462 VTableName = SIClassTypeInfo;
463 } else {
464 VTableName = ClassTypeInfo;
465 }
Eli Friedman1cf26f52010-08-11 20:41:51 +0000466 break;
467
John McCalle8dc53e2010-08-12 02:17:33 +0000468 case Type::ObjCObjectPointer:
Anders Carlsson8d145152009-12-20 22:30:54 +0000469 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000470 // abi::__pointer_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000471 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000472 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000473
Anders Carlsson8d145152009-12-20 22:30:54 +0000474 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000475 // abi::__pointer_to_member_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000476 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000477 break;
478 }
479
Anders Carlsson046c2942010-04-17 20:15:18 +0000480 llvm::Constant *VTable =
481 CGM.getModule().getOrInsertGlobal(VTableName, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000482
Chris Lattner2acc6e32011-07-18 04:24:23 +0000483 llvm::Type *PtrDiffTy =
Anders Carlsson8d145152009-12-20 22:30:54 +0000484 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
485
486 // The vtable address point is 2.
487 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Jay Foada5c04342011-07-21 14:31:17 +0000488 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Two);
Anders Carlsson046c2942010-04-17 20:15:18 +0000489 VTable = llvm::ConstantExpr::getBitCast(VTable, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000490
Anders Carlsson046c2942010-04-17 20:15:18 +0000491 Fields.push_back(VTable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000492}
493
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000494// maybeUpdateRTTILinkage - Will update the linkage of the RTTI data structures
495// from available_externally to the correct linkage if necessary. An example of
496// this is:
497//
498// struct A {
499// virtual void f();
500// };
501//
502// const std::type_info &g() {
503// return typeid(A);
504// }
505//
506// void A::f() { }
507//
508// When we're generating the typeid(A) expression, we do not yet know that
509// A's key function is defined in this translation unit, so we will give the
510// typeinfo and typename structures available_externally linkage. When A::f
511// forces the vtable to be generated, we need to change the linkage of the
512// typeinfo and typename structs, otherwise we'll end up with undefined
513// externals when linking.
514static void
515maybeUpdateRTTILinkage(CodeGenModule &CGM, llvm::GlobalVariable *GV,
516 QualType Ty) {
517 // We're only interested in globals with available_externally linkage.
518 if (!GV->hasAvailableExternallyLinkage())
519 return;
520
521 // Get the real linkage for the type.
522 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(CGM, Ty);
523
524 // If variable is supposed to have available_externally linkage, we don't
525 // need to do anything.
526 if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
527 return;
528
529 // Update the typeinfo linkage.
530 GV->setLinkage(Linkage);
531
532 // Get the typename global.
533 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000534 llvm::raw_svector_ostream Out(OutName);
535 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
536 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000537 StringRef Name = OutName.str();
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000538
539 llvm::GlobalVariable *TypeNameGV = CGM.getModule().getNamedGlobal(Name);
540
541 assert(TypeNameGV->hasAvailableExternallyLinkage() &&
542 "Type name has different linkage from type info!");
543
544 // And update its linkage.
545 TypeNameGV->setLinkage(Linkage);
546}
547
John McCallcbfe5022010-08-04 08:34:44 +0000548llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000549 // We want to operate on the canonical type.
550 Ty = CGM.getContext().getCanonicalType(Ty);
551
552 // Check if we've already emitted an RTTI descriptor for this type.
553 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000554 llvm::raw_svector_ostream Out(OutName);
555 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
556 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000557 StringRef Name = OutName.str();
Anders Carlsson1cbce122011-01-29 19:16:51 +0000558
Anders Carlsson8d145152009-12-20 22:30:54 +0000559 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000560 if (OldGV && !OldGV->isDeclaration()) {
561 maybeUpdateRTTILinkage(CGM, OldGV, Ty);
562
Anders Carlsson8d145152009-12-20 22:30:54 +0000563 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000564 }
John McCallcbfe5022010-08-04 08:34:44 +0000565
Anders Carlsson8d145152009-12-20 22:30:54 +0000566 // Check if there is already an external RTTI descriptor for this type.
John McCallcbfe5022010-08-04 08:34:44 +0000567 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000568 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
Anders Carlsson8d145152009-12-20 22:30:54 +0000569 return GetAddrOfExternalRTTIDescriptor(Ty);
570
John McCallcbfe5022010-08-04 08:34:44 +0000571 // Emit the standard library with external linkage.
572 llvm::GlobalVariable::LinkageTypes Linkage;
573 if (IsStdLib)
574 Linkage = llvm::GlobalValue::ExternalLinkage;
575 else
Anders Carlsson3a717f72011-01-24 02:04:33 +0000576 Linkage = getTypeInfoLinkage(CGM, Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +0000577
578 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000579 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000580
581 // And the name.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000582 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
583
Chris Lattner2acc6e32011-07-18 04:24:23 +0000584 llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson907c8282011-01-29 22:10:32 +0000585 Fields.push_back(llvm::ConstantExpr::getBitCast(TypeName, Int8PtrTy));
John McCallcbfe5022010-08-04 08:34:44 +0000586
Anders Carlsson8d145152009-12-20 22:30:54 +0000587 switch (Ty->getTypeClass()) {
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000588#define TYPE(Class, Base)
589#define ABSTRACT_TYPE(Class, Base)
590#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
591#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
592#define DEPENDENT_TYPE(Class, Base) case Type::Class:
593#include "clang/AST/TypeNodes.def"
David Blaikieb219cfc2011-09-23 05:06:16 +0000594 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
Anders Carlsson8d145152009-12-20 22:30:54 +0000595
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000596 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000597 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000598 case Type::Vector:
599 case Type::ExtVector:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000600 case Type::Complex:
601 case Type::BlockPointer:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000602 // Itanium C++ ABI 2.9.5p4:
603 // abi::__fundamental_type_info adds no data members to std::type_info.
604 break;
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000605
606 case Type::LValueReference:
607 case Type::RValueReference:
David Blaikieb219cfc2011-09-23 05:06:16 +0000608 llvm_unreachable("References shouldn't get here");
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000609
Anders Carlsson978ef682009-12-29 21:58:32 +0000610 case Type::ConstantArray:
611 case Type::IncompleteArray:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000612 case Type::VariableArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000613 // Itanium C++ ABI 2.9.5p5:
614 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000615 break;
616
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000617 case Type::FunctionNoProto:
618 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000619 // Itanium C++ ABI 2.9.5p5:
620 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000621 break;
622
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000623 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000624 // Itanium C++ ABI 2.9.5p5:
625 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000626 break;
627
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000628 case Type::Record: {
629 const CXXRecordDecl *RD =
630 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000631 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000632 // We don't need to emit any fields.
633 break;
634 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000635
Anders Carlsson08148092009-12-30 23:47:56 +0000636 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000637 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000638 else
639 BuildVMIClassTypeInfo(RD);
640
641 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000642 }
John McCalle8dc53e2010-08-12 02:17:33 +0000643
644 case Type::ObjCObject:
645 case Type::ObjCInterface:
646 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
647 break;
648
649 case Type::ObjCObjectPointer:
650 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
651 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000652
Anders Carlsson8d145152009-12-20 22:30:54 +0000653 case Type::Pointer:
John McCalle8dc53e2010-08-12 02:17:33 +0000654 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000655 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000656
Anders Carlsson8d145152009-12-20 22:30:54 +0000657 case Type::MemberPointer:
658 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
659 break;
Eli Friedmanb001de72011-10-06 23:00:33 +0000660
661 case Type::Atomic:
662 // No fields, at least for the moment.
663 break;
Anders Carlsson8d145152009-12-20 22:30:54 +0000664 }
665
Chris Lattnerc5cbb902011-06-20 04:01:35 +0000666 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields);
Anders Carlsson8d145152009-12-20 22:30:54 +0000667
668 llvm::GlobalVariable *GV =
669 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
670 /*Constant=*/true, Linkage, Init, Name);
671
672 // If there's already an old global variable, replace it with the new one.
673 if (OldGV) {
674 GV->takeName(OldGV);
675 llvm::Constant *NewPtr =
676 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
677 OldGV->replaceAllUsesWith(NewPtr);
678 OldGV->eraseFromParent();
679 }
John McCallcbfe5022010-08-04 08:34:44 +0000680
681 // GCC only relies on the uniqueness of the type names, not the
682 // type_infos themselves, so we can emit these as hidden symbols.
John McCall279b5eb2010-08-12 23:36:15 +0000683 // But don't do this if we're worried about strict visibility
684 // compatibility.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000685 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) {
686 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
687
688 CGM.setTypeVisibility(GV, RD, CodeGenModule::TVK_ForRTTI);
689 CGM.setTypeVisibility(TypeName, RD, CodeGenModule::TVK_ForRTTIName);
Anders Carlsson907c8282011-01-29 22:10:32 +0000690 } else {
691 Visibility TypeInfoVisibility = DefaultVisibility;
692 if (CGM.getCodeGenOpts().HiddenWeakVTables &&
693 Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
694 TypeInfoVisibility = HiddenVisibility;
Anders Carlsson9a86a132011-01-29 20:36:11 +0000695
Anders Carlsson907c8282011-01-29 22:10:32 +0000696 // The type name should have the same visibility as the type itself.
697 Visibility ExplicitVisibility = Ty->getVisibility();
698 TypeName->setVisibility(CodeGenModule::
699 GetLLVMVisibility(ExplicitVisibility));
700
701 TypeInfoVisibility = minVisibility(TypeInfoVisibility, Ty->getVisibility());
702 GV->setVisibility(CodeGenModule::GetLLVMVisibility(TypeInfoVisibility));
Rafael Espindolab1c65ff2011-01-11 21:44:37 +0000703 }
Anders Carlsson907c8282011-01-29 22:10:32 +0000704
Rafael Espindola57244f62011-01-11 23:55:05 +0000705 GV->setUnnamedAddr(true);
706
Anders Carlsson8d145152009-12-20 22:30:54 +0000707 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
708}
709
Anders Carlsson08148092009-12-30 23:47:56 +0000710/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000711/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000712static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000713 unsigned Flags = 0;
714
715 if (Quals.hasConst())
716 Flags |= RTTIBuilder::PTI_Const;
717 if (Quals.hasVolatile())
718 Flags |= RTTIBuilder::PTI_Volatile;
719 if (Quals.hasRestrict())
720 Flags |= RTTIBuilder::PTI_Restrict;
721
722 return Flags;
723}
724
John McCalle8dc53e2010-08-12 02:17:33 +0000725/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
726/// for the given Objective-C object type.
727void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
728 // Drop qualifiers.
729 const Type *T = OT->getBaseType().getTypePtr();
730 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
731
732 // The builtin types are abi::__class_type_infos and don't require
733 // extra fields.
734 if (isa<BuiltinType>(T)) return;
735
736 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
737 ObjCInterfaceDecl *Super = Class->getSuperClass();
738
739 // Root classes are also __class_type_info.
740 if (!Super) return;
741
742 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
743
744 // Everything else is single inheritance.
745 llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
746 Fields.push_back(BaseTypeInfo);
747}
748
Anders Carlssonf64531a2009-12-30 01:00:12 +0000749/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
750/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
751void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
752 // Itanium C++ ABI 2.9.5p6b:
753 // It adds to abi::__class_type_info a single member pointing to the
754 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000755 llvm::Constant *BaseTypeInfo =
756 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
757 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000758}
759
Benjamin Kramer79ba2a62010-10-22 16:48:22 +0000760namespace {
761 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
762 /// a class hierarchy.
763 struct SeenBases {
764 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
765 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
766 };
767}
Anders Carlsson08148092009-12-30 23:47:56 +0000768
769/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
770/// abi::__vmi_class_type_info.
771///
772static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
773 SeenBases &Bases) {
774
775 unsigned Flags = 0;
776
777 const CXXRecordDecl *BaseDecl =
778 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
779
780 if (Base->isVirtual()) {
781 if (Bases.VirtualBases.count(BaseDecl)) {
782 // If this virtual base has been seen before, then the class is diamond
783 // shaped.
784 Flags |= RTTIBuilder::VMI_DiamondShaped;
785 } else {
786 if (Bases.NonVirtualBases.count(BaseDecl))
787 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
788
789 // Mark the virtual base as seen.
790 Bases.VirtualBases.insert(BaseDecl);
791 }
792 } else {
793 if (Bases.NonVirtualBases.count(BaseDecl)) {
794 // If this non-virtual base has been seen before, then the class has non-
795 // diamond shaped repeated inheritance.
796 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
797 } else {
798 if (Bases.VirtualBases.count(BaseDecl))
799 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
800
801 // Mark the non-virtual base as seen.
802 Bases.NonVirtualBases.insert(BaseDecl);
803 }
804 }
805
806 // Walk all bases.
807 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
808 E = BaseDecl->bases_end(); I != E; ++I)
809 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
810
811 return Flags;
812}
813
814static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
815 unsigned Flags = 0;
816 SeenBases Bases;
817
818 // Walk all bases.
819 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
820 E = RD->bases_end(); I != E; ++I)
821 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
822
823 return Flags;
824}
825
826/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
827/// classes with bases that do not satisfy the abi::__si_class_type_info
828/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
829void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000830 llvm::Type *UnsignedIntLTy =
Anders Carlsson08148092009-12-30 23:47:56 +0000831 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
832
833 // Itanium C++ ABI 2.9.5p6c:
834 // __flags is a word with flags describing details about the class
835 // structure, which may be referenced by using the __flags_masks
836 // enumeration. These flags refer to both direct and indirect bases.
837 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000838 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000839
840 // Itanium C++ ABI 2.9.5p6c:
841 // __base_count is a word with the number of direct proper base class
842 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000843 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000844
845 if (!RD->getNumBases())
846 return;
847
Chris Lattner2acc6e32011-07-18 04:24:23 +0000848 llvm::Type *LongLTy =
Anders Carlsson08148092009-12-30 23:47:56 +0000849 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
850
851 // Now add the base class descriptions.
852
853 // Itanium C++ ABI 2.9.5p6c:
854 // __base_info[] is an array of base class descriptions -- one for every
855 // direct proper base. Each description is of the type:
856 //
857 // struct abi::__base_class_type_info {
Eli Friedmana7e68452010-08-22 01:00:03 +0000858 // public:
Anders Carlsson08148092009-12-30 23:47:56 +0000859 // const __class_type_info *__base_type;
860 // long __offset_flags;
861 //
862 // enum __offset_flags_masks {
863 // __virtual_mask = 0x1,
864 // __public_mask = 0x2,
865 // __offset_shift = 8
866 // };
867 // };
868 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
869 E = RD->bases_end(); I != E; ++I) {
870 const CXXBaseSpecifier *Base = I;
871
872 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000873 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000874
875 const CXXRecordDecl *BaseDecl =
876 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
877
878 int64_t OffsetFlags = 0;
879
880 // All but the lower 8 bits of __offset_flags are a signed offset.
881 // For a non-virtual base, this is the offset in the object of the base
882 // subobject. For a virtual base, this is the offset in the virtual table of
883 // the virtual base offset for the virtual base referenced (negative).
Ken Dyckb653d5a2011-04-09 01:09:56 +0000884 CharUnits Offset;
Anders Carlsson08148092009-12-30 23:47:56 +0000885 if (Base->isVirtual())
Ken Dyckb653d5a2011-04-09 01:09:56 +0000886 Offset =
Peter Collingbourne1d2b3172011-09-26 01:56:30 +0000887 CGM.getVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000888 else {
889 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Ken Dyckb653d5a2011-04-09 01:09:56 +0000890 Offset = Layout.getBaseClassOffset(BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000891 };
892
Ken Dyckb653d5a2011-04-09 01:09:56 +0000893 OffsetFlags = Offset.getQuantity() << 8;
Anders Carlsson08148092009-12-30 23:47:56 +0000894
895 // The low-order byte of __offset_flags contains flags, as given by the
896 // masks from the enumeration __offset_flags_masks.
897 if (Base->isVirtual())
898 OffsetFlags |= BCTI_Virtual;
899 if (Base->getAccessSpecifier() == AS_public)
900 OffsetFlags |= BCTI_Public;
901
Anders Carlsson531d55f2009-12-31 17:43:53 +0000902 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000903 }
904}
905
Anders Carlsson8d145152009-12-20 22:30:54 +0000906/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
907/// used for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +0000908void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
Anders Carlssonabd6b092010-06-02 15:44:35 +0000909 Qualifiers Quals;
910 QualType UnqualifiedPointeeTy =
911 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
912
Anders Carlsson8d145152009-12-20 22:30:54 +0000913 // Itanium C++ ABI 2.9.5p7:
914 // __flags is a flag word describing the cv-qualification and other
915 // attributes of the type pointed to
Anders Carlssonabd6b092010-06-02 15:44:35 +0000916 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000917
918 // Itanium C++ ABI 2.9.5p7:
919 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
920 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000921 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000922 Flags |= PTI_Incomplete;
923
Chris Lattner2acc6e32011-07-18 04:24:23 +0000924 llvm::Type *UnsignedIntLTy =
Anders Carlsson8d145152009-12-20 22:30:54 +0000925 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000926 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000927
928 // Itanium C++ ABI 2.9.5p7:
929 // __pointee is a pointer to the std::type_info derivation for the
930 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000931 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000932 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000933 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000934}
935
936/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
937/// struct, used for member pointer types.
938void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
939 QualType PointeeTy = Ty->getPointeeType();
940
Anders Carlssonabd6b092010-06-02 15:44:35 +0000941 Qualifiers Quals;
942 QualType UnqualifiedPointeeTy =
943 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
944
Anders Carlsson8d145152009-12-20 22:30:54 +0000945 // Itanium C++ ABI 2.9.5p7:
946 // __flags is a flag word describing the cv-qualification and other
947 // attributes of the type pointed to.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000948 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000949
950 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000951
952 // Itanium C++ ABI 2.9.5p7:
953 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
954 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000955 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000956 Flags |= PTI_Incomplete;
957
Anders Carlsson8d145152009-12-20 22:30:54 +0000958 if (IsIncompleteClassType(ClassType))
959 Flags |= PTI_ContainingClassIncomplete;
960
Chris Lattner2acc6e32011-07-18 04:24:23 +0000961 llvm::Type *UnsignedIntLTy =
Anders Carlsson8d145152009-12-20 22:30:54 +0000962 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000963 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000964
965 // Itanium C++ ABI 2.9.5p7:
966 // __pointee is a pointer to the std::type_info derivation for the
967 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000968 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000969 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000970 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000971
972 // Itanium C++ ABI 2.9.5p9:
973 // __context is a pointer to an abi::__class_type_info corresponding to the
974 // class type containing the member pointed to
975 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000976 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000977}
978
John McCall9dffe6f2010-04-30 01:15:21 +0000979llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
980 bool ForEH) {
981 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
982 // FIXME: should we even be calling this method if RTTI is disabled
983 // and it's not for EH?
984 if (!ForEH && !getContext().getLangOptions().RTTI) {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000985 llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000986 return llvm::Constant::getNullValue(Int8PtrTy);
987 }
David Chisnall80558d22011-03-20 21:35:39 +0000988
989 if (ForEH && Ty->isObjCObjectPointerType() && !Features.NeXTRuntime) {
Peter Collingbournee9265232011-07-27 20:29:46 +0000990 return ObjCRuntime->GetEHType(Ty);
David Chisnall80558d22011-03-20 21:35:39 +0000991 }
John McCall9dffe6f2010-04-30 01:15:21 +0000992
Anders Carlsson531d55f2009-12-31 17:43:53 +0000993 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000994}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000995
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000996void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
997 QualType PointerType = Context.getPointerType(Type);
998 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
999 RTTIBuilder(*this).BuildTypeInfo(Type, true);
1000 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
1001 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
1002}
1003
1004void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Anders Carlsson2bd62502010-11-04 05:28:09 +00001005 QualType FundamentalTypes[] = { Context.VoidTy, Context.NullPtrTy,
1006 Context.BoolTy, Context.WCharTy,
1007 Context.CharTy, Context.UnsignedCharTy,
1008 Context.SignedCharTy, Context.ShortTy,
1009 Context.UnsignedShortTy, Context.IntTy,
1010 Context.UnsignedIntTy, Context.LongTy,
1011 Context.UnsignedLongTy, Context.LongLongTy,
1012 Context.UnsignedLongLongTy, Context.FloatTy,
1013 Context.DoubleTy, Context.LongDoubleTy,
1014 Context.Char16Ty, Context.Char32Ty };
Rafael Espindolad1a5c312010-03-27 02:52:14 +00001015 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
1016 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
1017}