blob: a1105d289401afa3510f235b25d1130dfe7f33a7 [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) {
270 return !RecordTy->getDecl()->isDefinition();
271}
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:
407 // FIXME: GCC treats block pointers as fundamental types?!
408 case Type::BlockPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000409 // abi::__fundamental_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000410 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000411 break;
412
Anders Carlsson978ef682009-12-29 21:58:32 +0000413 case Type::ConstantArray:
414 case Type::IncompleteArray:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000415 case Type::VariableArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000416 // abi::__array_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000417 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000418 break;
419
420 case Type::FunctionNoProto:
421 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000422 // abi::__function_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000423 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000424 break;
425
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000426 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000427 // abi::__enum_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000428 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000429 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000430
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000431 case Type::Record: {
432 const CXXRecordDecl *RD =
433 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000434
John McCall86ff3082010-02-04 22:26:26 +0000435 if (!RD->hasDefinition() || !RD->getNumBases()) {
John McCalle8dc53e2010-08-12 02:17:33 +0000436 VTableName = ClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000437 } else if (CanUseSingleInheritance(RD)) {
John McCalle8dc53e2010-08-12 02:17:33 +0000438 VTableName = SIClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000439 } else {
John McCalle8dc53e2010-08-12 02:17:33 +0000440 VTableName = VMIClassTypeInfo;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000441 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000442
443 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000444 }
445
Eli Friedman1cf26f52010-08-11 20:41:51 +0000446 case Type::ObjCObject:
John McCalle8dc53e2010-08-12 02:17:33 +0000447 // Ignore protocol qualifiers.
448 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
449
450 // Handle id and Class.
451 if (isa<BuiltinType>(Ty)) {
452 VTableName = ClassTypeInfo;
453 break;
454 }
455
456 assert(isa<ObjCInterfaceType>(Ty));
457 // Fall through.
458
Eli Friedman1cf26f52010-08-11 20:41:51 +0000459 case Type::ObjCInterface:
John McCalle8dc53e2010-08-12 02:17:33 +0000460 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
461 VTableName = SIClassTypeInfo;
462 } else {
463 VTableName = ClassTypeInfo;
464 }
Eli Friedman1cf26f52010-08-11 20:41:51 +0000465 break;
466
John McCalle8dc53e2010-08-12 02:17:33 +0000467 case Type::ObjCObjectPointer:
Anders Carlsson8d145152009-12-20 22:30:54 +0000468 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000469 // abi::__pointer_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000470 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000471 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000472
Anders Carlsson8d145152009-12-20 22:30:54 +0000473 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000474 // abi::__pointer_to_member_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000475 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000476 break;
477 }
478
Anders Carlsson046c2942010-04-17 20:15:18 +0000479 llvm::Constant *VTable =
480 CGM.getModule().getOrInsertGlobal(VTableName, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000481
Chris Lattner2acc6e32011-07-18 04:24:23 +0000482 llvm::Type *PtrDiffTy =
Anders Carlsson8d145152009-12-20 22:30:54 +0000483 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
484
485 // The vtable address point is 2.
486 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Jay Foada5c04342011-07-21 14:31:17 +0000487 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Two);
Anders Carlsson046c2942010-04-17 20:15:18 +0000488 VTable = llvm::ConstantExpr::getBitCast(VTable, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000489
Anders Carlsson046c2942010-04-17 20:15:18 +0000490 Fields.push_back(VTable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000491}
492
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000493// maybeUpdateRTTILinkage - Will update the linkage of the RTTI data structures
494// from available_externally to the correct linkage if necessary. An example of
495// this is:
496//
497// struct A {
498// virtual void f();
499// };
500//
501// const std::type_info &g() {
502// return typeid(A);
503// }
504//
505// void A::f() { }
506//
507// When we're generating the typeid(A) expression, we do not yet know that
508// A's key function is defined in this translation unit, so we will give the
509// typeinfo and typename structures available_externally linkage. When A::f
510// forces the vtable to be generated, we need to change the linkage of the
511// typeinfo and typename structs, otherwise we'll end up with undefined
512// externals when linking.
513static void
514maybeUpdateRTTILinkage(CodeGenModule &CGM, llvm::GlobalVariable *GV,
515 QualType Ty) {
516 // We're only interested in globals with available_externally linkage.
517 if (!GV->hasAvailableExternallyLinkage())
518 return;
519
520 // Get the real linkage for the type.
521 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(CGM, Ty);
522
523 // If variable is supposed to have available_externally linkage, we don't
524 // need to do anything.
525 if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
526 return;
527
528 // Update the typeinfo linkage.
529 GV->setLinkage(Linkage);
530
531 // Get the typename global.
532 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000533 llvm::raw_svector_ostream Out(OutName);
534 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
535 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000536 StringRef Name = OutName.str();
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000537
538 llvm::GlobalVariable *TypeNameGV = CGM.getModule().getNamedGlobal(Name);
539
540 assert(TypeNameGV->hasAvailableExternallyLinkage() &&
541 "Type name has different linkage from type info!");
542
543 // And update its linkage.
544 TypeNameGV->setLinkage(Linkage);
545}
546
John McCallcbfe5022010-08-04 08:34:44 +0000547llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000548 // We want to operate on the canonical type.
549 Ty = CGM.getContext().getCanonicalType(Ty);
550
551 // Check if we've already emitted an RTTI descriptor for this type.
552 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000553 llvm::raw_svector_ostream Out(OutName);
554 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
555 Out.flush();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000556 StringRef Name = OutName.str();
Anders Carlsson1cbce122011-01-29 19:16:51 +0000557
Anders Carlsson8d145152009-12-20 22:30:54 +0000558 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000559 if (OldGV && !OldGV->isDeclaration()) {
560 maybeUpdateRTTILinkage(CGM, OldGV, Ty);
561
Anders Carlsson8d145152009-12-20 22:30:54 +0000562 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000563 }
John McCallcbfe5022010-08-04 08:34:44 +0000564
Anders Carlsson8d145152009-12-20 22:30:54 +0000565 // Check if there is already an external RTTI descriptor for this type.
John McCallcbfe5022010-08-04 08:34:44 +0000566 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000567 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
Anders Carlsson8d145152009-12-20 22:30:54 +0000568 return GetAddrOfExternalRTTIDescriptor(Ty);
569
John McCallcbfe5022010-08-04 08:34:44 +0000570 // Emit the standard library with external linkage.
571 llvm::GlobalVariable::LinkageTypes Linkage;
572 if (IsStdLib)
573 Linkage = llvm::GlobalValue::ExternalLinkage;
574 else
Anders Carlsson3a717f72011-01-24 02:04:33 +0000575 Linkage = getTypeInfoLinkage(CGM, Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +0000576
577 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000578 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000579
580 // And the name.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000581 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
582
Chris Lattner2acc6e32011-07-18 04:24:23 +0000583 llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson907c8282011-01-29 22:10:32 +0000584 Fields.push_back(llvm::ConstantExpr::getBitCast(TypeName, Int8PtrTy));
John McCallcbfe5022010-08-04 08:34:44 +0000585
Anders Carlsson8d145152009-12-20 22:30:54 +0000586 switch (Ty->getTypeClass()) {
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000587#define TYPE(Class, Base)
588#define ABSTRACT_TYPE(Class, Base)
589#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
590#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
591#define DEPENDENT_TYPE(Class, Base) case Type::Class:
592#include "clang/AST/TypeNodes.def"
David Blaikieb219cfc2011-09-23 05:06:16 +0000593 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
Anders Carlsson8d145152009-12-20 22:30:54 +0000594
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000595 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000596 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000597 case Type::Vector:
598 case Type::ExtVector:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000599 case Type::Complex:
600 case Type::BlockPointer:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000601 // Itanium C++ ABI 2.9.5p4:
602 // abi::__fundamental_type_info adds no data members to std::type_info.
603 break;
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000604
605 case Type::LValueReference:
606 case Type::RValueReference:
David Blaikieb219cfc2011-09-23 05:06:16 +0000607 llvm_unreachable("References shouldn't get here");
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000608
Anders Carlsson978ef682009-12-29 21:58:32 +0000609 case Type::ConstantArray:
610 case Type::IncompleteArray:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000611 case Type::VariableArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000612 // Itanium C++ ABI 2.9.5p5:
613 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000614 break;
615
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000616 case Type::FunctionNoProto:
617 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000618 // Itanium C++ ABI 2.9.5p5:
619 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000620 break;
621
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000622 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000623 // Itanium C++ ABI 2.9.5p5:
624 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000625 break;
626
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000627 case Type::Record: {
628 const CXXRecordDecl *RD =
629 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000630 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000631 // We don't need to emit any fields.
632 break;
633 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000634
Anders Carlsson08148092009-12-30 23:47:56 +0000635 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000636 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000637 else
638 BuildVMIClassTypeInfo(RD);
639
640 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000641 }
John McCalle8dc53e2010-08-12 02:17:33 +0000642
643 case Type::ObjCObject:
644 case Type::ObjCInterface:
645 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
646 break;
647
648 case Type::ObjCObjectPointer:
649 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
650 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000651
Anders Carlsson8d145152009-12-20 22:30:54 +0000652 case Type::Pointer:
John McCalle8dc53e2010-08-12 02:17:33 +0000653 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000654 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000655
Anders Carlsson8d145152009-12-20 22:30:54 +0000656 case Type::MemberPointer:
657 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
658 break;
659 }
660
Chris Lattnerc5cbb902011-06-20 04:01:35 +0000661 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields);
Anders Carlsson8d145152009-12-20 22:30:54 +0000662
663 llvm::GlobalVariable *GV =
664 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
665 /*Constant=*/true, Linkage, Init, Name);
666
667 // If there's already an old global variable, replace it with the new one.
668 if (OldGV) {
669 GV->takeName(OldGV);
670 llvm::Constant *NewPtr =
671 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
672 OldGV->replaceAllUsesWith(NewPtr);
673 OldGV->eraseFromParent();
674 }
John McCallcbfe5022010-08-04 08:34:44 +0000675
676 // GCC only relies on the uniqueness of the type names, not the
677 // type_infos themselves, so we can emit these as hidden symbols.
John McCall279b5eb2010-08-12 23:36:15 +0000678 // But don't do this if we're worried about strict visibility
679 // compatibility.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000680 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) {
681 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
682
683 CGM.setTypeVisibility(GV, RD, CodeGenModule::TVK_ForRTTI);
684 CGM.setTypeVisibility(TypeName, RD, CodeGenModule::TVK_ForRTTIName);
Anders Carlsson907c8282011-01-29 22:10:32 +0000685 } else {
686 Visibility TypeInfoVisibility = DefaultVisibility;
687 if (CGM.getCodeGenOpts().HiddenWeakVTables &&
688 Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
689 TypeInfoVisibility = HiddenVisibility;
Anders Carlsson9a86a132011-01-29 20:36:11 +0000690
Anders Carlsson907c8282011-01-29 22:10:32 +0000691 // The type name should have the same visibility as the type itself.
692 Visibility ExplicitVisibility = Ty->getVisibility();
693 TypeName->setVisibility(CodeGenModule::
694 GetLLVMVisibility(ExplicitVisibility));
695
696 TypeInfoVisibility = minVisibility(TypeInfoVisibility, Ty->getVisibility());
697 GV->setVisibility(CodeGenModule::GetLLVMVisibility(TypeInfoVisibility));
Rafael Espindolab1c65ff2011-01-11 21:44:37 +0000698 }
Anders Carlsson907c8282011-01-29 22:10:32 +0000699
Rafael Espindola57244f62011-01-11 23:55:05 +0000700 GV->setUnnamedAddr(true);
701
Anders Carlsson8d145152009-12-20 22:30:54 +0000702 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
703}
704
Anders Carlsson08148092009-12-30 23:47:56 +0000705/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000706/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000707static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000708 unsigned Flags = 0;
709
710 if (Quals.hasConst())
711 Flags |= RTTIBuilder::PTI_Const;
712 if (Quals.hasVolatile())
713 Flags |= RTTIBuilder::PTI_Volatile;
714 if (Quals.hasRestrict())
715 Flags |= RTTIBuilder::PTI_Restrict;
716
717 return Flags;
718}
719
John McCalle8dc53e2010-08-12 02:17:33 +0000720/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
721/// for the given Objective-C object type.
722void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
723 // Drop qualifiers.
724 const Type *T = OT->getBaseType().getTypePtr();
725 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
726
727 // The builtin types are abi::__class_type_infos and don't require
728 // extra fields.
729 if (isa<BuiltinType>(T)) return;
730
731 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
732 ObjCInterfaceDecl *Super = Class->getSuperClass();
733
734 // Root classes are also __class_type_info.
735 if (!Super) return;
736
737 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
738
739 // Everything else is single inheritance.
740 llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
741 Fields.push_back(BaseTypeInfo);
742}
743
Anders Carlssonf64531a2009-12-30 01:00:12 +0000744/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
745/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
746void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
747 // Itanium C++ ABI 2.9.5p6b:
748 // It adds to abi::__class_type_info a single member pointing to the
749 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000750 llvm::Constant *BaseTypeInfo =
751 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
752 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000753}
754
Benjamin Kramer79ba2a62010-10-22 16:48:22 +0000755namespace {
756 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
757 /// a class hierarchy.
758 struct SeenBases {
759 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
760 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
761 };
762}
Anders Carlsson08148092009-12-30 23:47:56 +0000763
764/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
765/// abi::__vmi_class_type_info.
766///
767static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
768 SeenBases &Bases) {
769
770 unsigned Flags = 0;
771
772 const CXXRecordDecl *BaseDecl =
773 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
774
775 if (Base->isVirtual()) {
776 if (Bases.VirtualBases.count(BaseDecl)) {
777 // If this virtual base has been seen before, then the class is diamond
778 // shaped.
779 Flags |= RTTIBuilder::VMI_DiamondShaped;
780 } else {
781 if (Bases.NonVirtualBases.count(BaseDecl))
782 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
783
784 // Mark the virtual base as seen.
785 Bases.VirtualBases.insert(BaseDecl);
786 }
787 } else {
788 if (Bases.NonVirtualBases.count(BaseDecl)) {
789 // If this non-virtual base has been seen before, then the class has non-
790 // diamond shaped repeated inheritance.
791 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
792 } else {
793 if (Bases.VirtualBases.count(BaseDecl))
794 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
795
796 // Mark the non-virtual base as seen.
797 Bases.NonVirtualBases.insert(BaseDecl);
798 }
799 }
800
801 // Walk all bases.
802 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
803 E = BaseDecl->bases_end(); I != E; ++I)
804 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
805
806 return Flags;
807}
808
809static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
810 unsigned Flags = 0;
811 SeenBases Bases;
812
813 // Walk all bases.
814 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
815 E = RD->bases_end(); I != E; ++I)
816 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
817
818 return Flags;
819}
820
821/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
822/// classes with bases that do not satisfy the abi::__si_class_type_info
823/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
824void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000825 llvm::Type *UnsignedIntLTy =
Anders Carlsson08148092009-12-30 23:47:56 +0000826 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
827
828 // Itanium C++ ABI 2.9.5p6c:
829 // __flags is a word with flags describing details about the class
830 // structure, which may be referenced by using the __flags_masks
831 // enumeration. These flags refer to both direct and indirect bases.
832 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000833 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000834
835 // Itanium C++ ABI 2.9.5p6c:
836 // __base_count is a word with the number of direct proper base class
837 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000838 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000839
840 if (!RD->getNumBases())
841 return;
842
Chris Lattner2acc6e32011-07-18 04:24:23 +0000843 llvm::Type *LongLTy =
Anders Carlsson08148092009-12-30 23:47:56 +0000844 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
845
846 // Now add the base class descriptions.
847
848 // Itanium C++ ABI 2.9.5p6c:
849 // __base_info[] is an array of base class descriptions -- one for every
850 // direct proper base. Each description is of the type:
851 //
852 // struct abi::__base_class_type_info {
Eli Friedmana7e68452010-08-22 01:00:03 +0000853 // public:
Anders Carlsson08148092009-12-30 23:47:56 +0000854 // const __class_type_info *__base_type;
855 // long __offset_flags;
856 //
857 // enum __offset_flags_masks {
858 // __virtual_mask = 0x1,
859 // __public_mask = 0x2,
860 // __offset_shift = 8
861 // };
862 // };
863 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
864 E = RD->bases_end(); I != E; ++I) {
865 const CXXBaseSpecifier *Base = I;
866
867 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000868 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000869
870 const CXXRecordDecl *BaseDecl =
871 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
872
873 int64_t OffsetFlags = 0;
874
875 // All but the lower 8 bits of __offset_flags are a signed offset.
876 // For a non-virtual base, this is the offset in the object of the base
877 // subobject. For a virtual base, this is the offset in the virtual table of
878 // the virtual base offset for the virtual base referenced (negative).
Ken Dyckb653d5a2011-04-09 01:09:56 +0000879 CharUnits Offset;
Anders Carlsson08148092009-12-30 23:47:56 +0000880 if (Base->isVirtual())
Ken Dyckb653d5a2011-04-09 01:09:56 +0000881 Offset =
Peter Collingbourne1d2b3172011-09-26 01:56:30 +0000882 CGM.getVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000883 else {
884 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Ken Dyckb653d5a2011-04-09 01:09:56 +0000885 Offset = Layout.getBaseClassOffset(BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000886 };
887
Ken Dyckb653d5a2011-04-09 01:09:56 +0000888 OffsetFlags = Offset.getQuantity() << 8;
Anders Carlsson08148092009-12-30 23:47:56 +0000889
890 // The low-order byte of __offset_flags contains flags, as given by the
891 // masks from the enumeration __offset_flags_masks.
892 if (Base->isVirtual())
893 OffsetFlags |= BCTI_Virtual;
894 if (Base->getAccessSpecifier() == AS_public)
895 OffsetFlags |= BCTI_Public;
896
Anders Carlsson531d55f2009-12-31 17:43:53 +0000897 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000898 }
899}
900
Anders Carlsson8d145152009-12-20 22:30:54 +0000901/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
902/// used for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +0000903void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
Anders Carlssonabd6b092010-06-02 15:44:35 +0000904 Qualifiers Quals;
905 QualType UnqualifiedPointeeTy =
906 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
907
Anders Carlsson8d145152009-12-20 22:30:54 +0000908 // Itanium C++ ABI 2.9.5p7:
909 // __flags is a flag word describing the cv-qualification and other
910 // attributes of the type pointed to
Anders Carlssonabd6b092010-06-02 15:44:35 +0000911 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000912
913 // Itanium C++ ABI 2.9.5p7:
914 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
915 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000916 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000917 Flags |= PTI_Incomplete;
918
Chris Lattner2acc6e32011-07-18 04:24:23 +0000919 llvm::Type *UnsignedIntLTy =
Anders Carlsson8d145152009-12-20 22:30:54 +0000920 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000921 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000922
923 // Itanium C++ ABI 2.9.5p7:
924 // __pointee is a pointer to the std::type_info derivation for the
925 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000926 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000927 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000928 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000929}
930
931/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
932/// struct, used for member pointer types.
933void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
934 QualType PointeeTy = Ty->getPointeeType();
935
Anders Carlssonabd6b092010-06-02 15:44:35 +0000936 Qualifiers Quals;
937 QualType UnqualifiedPointeeTy =
938 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
939
Anders Carlsson8d145152009-12-20 22:30:54 +0000940 // Itanium C++ ABI 2.9.5p7:
941 // __flags is a flag word describing the cv-qualification and other
942 // attributes of the type pointed to.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000943 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000944
945 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000946
947 // Itanium C++ ABI 2.9.5p7:
948 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
949 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000950 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000951 Flags |= PTI_Incomplete;
952
Anders Carlsson8d145152009-12-20 22:30:54 +0000953 if (IsIncompleteClassType(ClassType))
954 Flags |= PTI_ContainingClassIncomplete;
955
Chris Lattner2acc6e32011-07-18 04:24:23 +0000956 llvm::Type *UnsignedIntLTy =
Anders Carlsson8d145152009-12-20 22:30:54 +0000957 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000958 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000959
960 // Itanium C++ ABI 2.9.5p7:
961 // __pointee is a pointer to the std::type_info derivation for the
962 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000963 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000964 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000965 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000966
967 // Itanium C++ ABI 2.9.5p9:
968 // __context is a pointer to an abi::__class_type_info corresponding to the
969 // class type containing the member pointed to
970 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000971 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000972}
973
John McCall9dffe6f2010-04-30 01:15:21 +0000974llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
975 bool ForEH) {
976 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
977 // FIXME: should we even be calling this method if RTTI is disabled
978 // and it's not for EH?
979 if (!ForEH && !getContext().getLangOptions().RTTI) {
Chris Lattner2acc6e32011-07-18 04:24:23 +0000980 llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000981 return llvm::Constant::getNullValue(Int8PtrTy);
982 }
David Chisnall80558d22011-03-20 21:35:39 +0000983
984 if (ForEH && Ty->isObjCObjectPointerType() && !Features.NeXTRuntime) {
Peter Collingbournee9265232011-07-27 20:29:46 +0000985 return ObjCRuntime->GetEHType(Ty);
David Chisnall80558d22011-03-20 21:35:39 +0000986 }
John McCall9dffe6f2010-04-30 01:15:21 +0000987
Anders Carlsson531d55f2009-12-31 17:43:53 +0000988 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000989}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000990
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000991void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
992 QualType PointerType = Context.getPointerType(Type);
993 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
994 RTTIBuilder(*this).BuildTypeInfo(Type, true);
995 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
996 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
997}
998
999void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Anders Carlsson2bd62502010-11-04 05:28:09 +00001000 QualType FundamentalTypes[] = { Context.VoidTy, Context.NullPtrTy,
1001 Context.BoolTy, Context.WCharTy,
1002 Context.CharTy, Context.UnsignedCharTy,
1003 Context.SignedCharTy, Context.ShortTy,
1004 Context.UnsignedShortTy, Context.IntTy,
1005 Context.UnsignedIntTy, Context.LongTy,
1006 Context.UnsignedLongTy, Context.LongLongTy,
1007 Context.UnsignedLongLongTy, Context.FloatTy,
1008 Context.DoubleTy, Context.LongDoubleTy,
1009 Context.Char16Ty, Context.Char32Ty };
Rafael Espindolad1a5c312010-03-27 02:52:14 +00001010 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
1011 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
1012}