blob: fb89f4ca0088a7c86b10d72b365d444cb130786c [file] [log] [blame]
Mike Stumpde050572009-12-02 18:57:08 +00001//===--- CGCXXRTTI.cpp - Emit LLVM Code for C++ RTTI descriptors ----------===//
Anders Carlsson656e4c12009-10-10 20:49:04 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation of RTTI descriptors.
11//
12//===----------------------------------------------------------------------===//
13
Mike Stump61c38012009-11-17 21:44:24 +000014#include "CodeGenModule.h"
John McCall4c40d982010-08-31 07:33:07 +000015#include "CGCXXABI.h"
John McCall279b5eb2010-08-12 23:36:15 +000016#include "clang/AST/RecordLayout.h"
17#include "clang/AST/Type.h"
18#include "clang/Frontend/CodeGenOptions.h"
David Chisnall80558d22011-03-20 21:35:39 +000019#include "CGObjCRuntime.h"
John McCall279b5eb2010-08-12 23:36:15 +000020
Anders Carlsson656e4c12009-10-10 20:49:04 +000021using namespace clang;
22using namespace CodeGen;
23
Mike Stump92f2fe22009-12-02 19:07:44 +000024namespace {
Mike Stumpde050572009-12-02 18:57:08 +000025class RTTIBuilder {
Mike Stump2b1bf312009-11-14 14:25:18 +000026 CodeGenModule &CGM; // Per-module state.
27 llvm::LLVMContext &VMContext;
Anders Carlsson8d145152009-12-20 22:30:54 +000028
Anders Carlsson08148092009-12-30 23:47:56 +000029 const llvm::Type *Int8PtrTy;
Anders Carlsson531d55f2009-12-31 17:43:53 +000030
31 /// Fields - The fields of the RTTI descriptor currently being built.
32 llvm::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();
Anders Carlsson9a86a132011-01-29 20:36:11 +0000123 llvm::StringRef Name = OutName.str();
124
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();
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000144 llvm::StringRef Name = OutName.str();
145
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 McCall1de4d4e2011-04-07 08:22:57 +0000199 case BuiltinType::UnknownAny:
Anders Carlsson8d145152009-12-20 22:30:54 +0000200 assert(false && "Should not see this type here!");
201
Anders Carlsson8d145152009-12-20 22:30:54 +0000202 case BuiltinType::ObjCId:
203 case BuiltinType::ObjCClass:
204 case BuiltinType::ObjCSel:
205 assert(false && "FIXME: Objective-C types are unsupported!");
206 }
207
208 // Silent gcc.
209 return false;
210}
211
212static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
213 QualType PointeeTy = PointerTy->getPointeeType();
214 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
215 if (!BuiltinTy)
216 return false;
217
218 // Check the qualifiers.
219 Qualifiers Quals = PointeeTy.getQualifiers();
220 Quals.removeConst();
221
222 if (!Quals.empty())
223 return false;
224
225 return TypeInfoIsInStandardLibrary(BuiltinTy);
226}
227
John McCallcbfe5022010-08-04 08:34:44 +0000228/// IsStandardLibraryRTTIDescriptor - Returns whether the type
229/// information for the given type exists in the standard library.
230static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000231 // Type info for builtin types is defined in the standard library.
232 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
233 return TypeInfoIsInStandardLibrary(BuiltinTy);
234
235 // Type info for some pointer types to builtin types is defined in the
236 // standard library.
237 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
238 return TypeInfoIsInStandardLibrary(PointerTy);
239
John McCallcbfe5022010-08-04 08:34:44 +0000240 return false;
241}
242
243/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
244/// the given type exists somewhere else, and that we should not emit the type
245/// information in this translation unit. Assumes that it is not a
246/// standard-library type.
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000247static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM, QualType Ty) {
248 ASTContext &Context = CGM.getContext();
249
John McCall9dffe6f2010-04-30 01:15:21 +0000250 // If RTTI is disabled, don't consider key functions.
251 if (!Context.getLangOptions().RTTI) return false;
252
Anders Carlsson8d145152009-12-20 22:30:54 +0000253 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000254 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000255 if (!RD->hasDefinition())
256 return false;
257
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000258 if (!RD->isDynamicClass())
259 return false;
260
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000261 return !CGM.getVTables().ShouldEmitVTableInThisTU(RD);
Anders Carlsson8d145152009-12-20 22:30:54 +0000262 }
263
264 return false;
265}
266
267/// IsIncompleteClassType - Returns whether the given record type is incomplete.
268static bool IsIncompleteClassType(const RecordType *RecordTy) {
269 return !RecordTy->getDecl()->isDefinition();
270}
271
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000272/// ContainsIncompleteClassType - Returns whether the given type contains an
273/// incomplete class type. This is true if
274///
275/// * The given type is an incomplete class type.
276/// * The given type is a pointer type whose pointee type contains an
277/// incomplete class type.
278/// * The given type is a member pointer type whose class is an incomplete
279/// class type.
280/// * The given type is a member pointer type whoise pointee type contains an
281/// incomplete class type.
Anders Carlsson8d145152009-12-20 22:30:54 +0000282/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000283static bool ContainsIncompleteClassType(QualType Ty) {
284 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
285 if (IsIncompleteClassType(RecordTy))
286 return true;
287 }
288
289 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
290 return ContainsIncompleteClassType(PointerTy->getPointeeType());
291
292 if (const MemberPointerType *MemberPointerTy =
293 dyn_cast<MemberPointerType>(Ty)) {
294 // Check if the class type is incomplete.
295 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
296 if (IsIncompleteClassType(ClassType))
297 return true;
298
299 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000300 }
301
302 return false;
303}
304
305/// getTypeInfoLinkage - Return the linkage that the type info and type info
306/// name constants should have for the given type.
Anders Carlsson3a717f72011-01-24 02:04:33 +0000307static llvm::GlobalVariable::LinkageTypes
308getTypeInfoLinkage(CodeGenModule &CGM, QualType Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000309 // Itanium C++ ABI 2.9.5p7:
310 // In addition, it and all of the intermediate abi::__pointer_type_info
311 // structs in the chain down to the abi::__class_type_info for the
312 // incomplete class type must be prevented from resolving to the
313 // corresponding type_info structs for the complete class type, possibly
314 // by making them local static objects. Finally, a dummy class RTTI is
315 // generated for the incomplete type that will not resolve to the final
316 // complete class RTTI (because the latter need not exist), possibly by
317 // making it a local static object.
318 if (ContainsIncompleteClassType(Ty))
319 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000320
Douglas Gregor031b3712010-03-31 00:15:35 +0000321 switch (Ty->getLinkage()) {
322 case NoLinkage:
323 case InternalLinkage:
324 case UniqueExternalLinkage:
325 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson978ef682009-12-29 21:58:32 +0000326
Douglas Gregor031b3712010-03-31 00:15:35 +0000327 case ExternalLinkage:
Anders Carlssone34e3aa2011-01-24 02:12:11 +0000328 if (!CGM.getLangOptions().RTTI) {
329 // RTTI is not enabled, which means that this type info struct is going
330 // to be used for exception handling. Give it linkonce_odr linkage.
331 return llvm::GlobalValue::LinkOnceODRLinkage;
332 }
333
Douglas Gregor031b3712010-03-31 00:15:35 +0000334 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
335 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
336 if (RD->isDynamicClass())
Anders Carlsson3a717f72011-01-24 02:04:33 +0000337 return CGM.getVTableLinkage(RD);
Mike Stumpc8f76f52009-12-24 01:10:27 +0000338 }
Douglas Gregor031b3712010-03-31 00:15:35 +0000339
Anders Carlssonf502d932011-01-24 00:46:19 +0000340 return llvm::GlobalValue::LinkOnceODRLinkage;
Mike Stumpc8f76f52009-12-24 01:10:27 +0000341 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000342
Anders Carlssonf502d932011-01-24 00:46:19 +0000343 return llvm::GlobalValue::LinkOnceODRLinkage;
Anders Carlsson8d145152009-12-20 22:30:54 +0000344}
345
Anders Carlssonf64531a2009-12-30 01:00:12 +0000346// CanUseSingleInheritance - Return whether the given record decl has a "single,
347// public, non-virtual base at offset zero (i.e. the derived class is dynamic
348// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
349static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
350 // Check the number of bases.
351 if (RD->getNumBases() != 1)
352 return false;
353
354 // Get the base.
355 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
356
357 // Check that the base is not virtual.
358 if (Base->isVirtual())
359 return false;
360
361 // Check that the base is public.
362 if (Base->getAccessSpecifier() != AS_public)
363 return false;
364
365 // Check that the class is dynamic iff the base is.
366 const CXXRecordDecl *BaseDecl =
367 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
368 if (!BaseDecl->isEmpty() &&
369 BaseDecl->isDynamicClass() != RD->isDynamicClass())
370 return false;
371
372 return true;
373}
374
Anders Carlsson046c2942010-04-17 20:15:18 +0000375void RTTIBuilder::BuildVTablePointer(const Type *Ty) {
John McCalle8dc53e2010-08-12 02:17:33 +0000376 // abi::__class_type_info.
377 static const char * const ClassTypeInfo =
378 "_ZTVN10__cxxabiv117__class_type_infoE";
379 // abi::__si_class_type_info.
380 static const char * const SIClassTypeInfo =
381 "_ZTVN10__cxxabiv120__si_class_type_infoE";
382 // abi::__vmi_class_type_info.
383 static const char * const VMIClassTypeInfo =
384 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
385
Eli Friedman1cf26f52010-08-11 20:41:51 +0000386 const char *VTableName = 0;
Anders Carlsson8d145152009-12-20 22:30:54 +0000387
388 switch (Ty->getTypeClass()) {
Eli Friedman1cf26f52010-08-11 20:41:51 +0000389#define TYPE(Class, Base)
390#define ABSTRACT_TYPE(Class, Base)
391#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
392#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
393#define DEPENDENT_TYPE(Class, Base) case Type::Class:
394#include "clang/AST/TypeNodes.def"
395 assert(false && "Non-canonical and dependent types shouldn't get here");
396
397 case Type::LValueReference:
398 case Type::RValueReference:
399 assert(false && "References shouldn't get here");
Anders Carlsson978ef682009-12-29 21:58:32 +0000400
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000401 case Type::Builtin:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000402 // GCC treats vector and complex types as fundamental types.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000403 case Type::Vector:
404 case Type::ExtVector:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000405 case Type::Complex:
406 // FIXME: GCC treats block pointers as fundamental types?!
407 case Type::BlockPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000408 // abi::__fundamental_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000409 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000410 break;
411
Anders Carlsson978ef682009-12-29 21:58:32 +0000412 case Type::ConstantArray:
413 case Type::IncompleteArray:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000414 case Type::VariableArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000415 // abi::__array_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000416 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000417 break;
418
419 case Type::FunctionNoProto:
420 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000421 // abi::__function_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000422 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000423 break;
424
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000425 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000426 // abi::__enum_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000427 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000428 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000429
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000430 case Type::Record: {
431 const CXXRecordDecl *RD =
432 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000433
John McCall86ff3082010-02-04 22:26:26 +0000434 if (!RD->hasDefinition() || !RD->getNumBases()) {
John McCalle8dc53e2010-08-12 02:17:33 +0000435 VTableName = ClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000436 } else if (CanUseSingleInheritance(RD)) {
John McCalle8dc53e2010-08-12 02:17:33 +0000437 VTableName = SIClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000438 } else {
John McCalle8dc53e2010-08-12 02:17:33 +0000439 VTableName = VMIClassTypeInfo;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000440 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000441
442 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000443 }
444
Eli Friedman1cf26f52010-08-11 20:41:51 +0000445 case Type::ObjCObject:
John McCalle8dc53e2010-08-12 02:17:33 +0000446 // Ignore protocol qualifiers.
447 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
448
449 // Handle id and Class.
450 if (isa<BuiltinType>(Ty)) {
451 VTableName = ClassTypeInfo;
452 break;
453 }
454
455 assert(isa<ObjCInterfaceType>(Ty));
456 // Fall through.
457
Eli Friedman1cf26f52010-08-11 20:41:51 +0000458 case Type::ObjCInterface:
John McCalle8dc53e2010-08-12 02:17:33 +0000459 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
460 VTableName = SIClassTypeInfo;
461 } else {
462 VTableName = ClassTypeInfo;
463 }
Eli Friedman1cf26f52010-08-11 20:41:51 +0000464 break;
465
John McCalle8dc53e2010-08-12 02:17:33 +0000466 case Type::ObjCObjectPointer:
Anders Carlsson8d145152009-12-20 22:30:54 +0000467 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000468 // abi::__pointer_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000469 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000470 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000471
Anders Carlsson8d145152009-12-20 22:30:54 +0000472 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000473 // abi::__pointer_to_member_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000474 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000475 break;
476 }
477
Anders Carlsson046c2942010-04-17 20:15:18 +0000478 llvm::Constant *VTable =
479 CGM.getModule().getOrInsertGlobal(VTableName, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000480
481 const llvm::Type *PtrDiffTy =
482 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
483
484 // The vtable address point is 2.
485 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Anders Carlsson046c2942010-04-17 20:15:18 +0000486 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, &Two, 1);
487 VTable = llvm::ConstantExpr::getBitCast(VTable, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000488
Anders Carlsson046c2942010-04-17 20:15:18 +0000489 Fields.push_back(VTable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000490}
491
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000492// maybeUpdateRTTILinkage - Will update the linkage of the RTTI data structures
493// from available_externally to the correct linkage if necessary. An example of
494// this is:
495//
496// struct A {
497// virtual void f();
498// };
499//
500// const std::type_info &g() {
501// return typeid(A);
502// }
503//
504// void A::f() { }
505//
506// When we're generating the typeid(A) expression, we do not yet know that
507// A's key function is defined in this translation unit, so we will give the
508// typeinfo and typename structures available_externally linkage. When A::f
509// forces the vtable to be generated, we need to change the linkage of the
510// typeinfo and typename structs, otherwise we'll end up with undefined
511// externals when linking.
512static void
513maybeUpdateRTTILinkage(CodeGenModule &CGM, llvm::GlobalVariable *GV,
514 QualType Ty) {
515 // We're only interested in globals with available_externally linkage.
516 if (!GV->hasAvailableExternallyLinkage())
517 return;
518
519 // Get the real linkage for the type.
520 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(CGM, Ty);
521
522 // If variable is supposed to have available_externally linkage, we don't
523 // need to do anything.
524 if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
525 return;
526
527 // Update the typeinfo linkage.
528 GV->setLinkage(Linkage);
529
530 // Get the typename global.
531 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000532 llvm::raw_svector_ostream Out(OutName);
533 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
534 Out.flush();
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000535 llvm::StringRef Name = OutName.str();
536
537 llvm::GlobalVariable *TypeNameGV = CGM.getModule().getNamedGlobal(Name);
538
539 assert(TypeNameGV->hasAvailableExternallyLinkage() &&
540 "Type name has different linkage from type info!");
541
542 // And update its linkage.
543 TypeNameGV->setLinkage(Linkage);
544}
545
John McCallcbfe5022010-08-04 08:34:44 +0000546llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000547 // We want to operate on the canonical type.
548 Ty = CGM.getContext().getCanonicalType(Ty);
549
550 // Check if we've already emitted an RTTI descriptor for this type.
551 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000552 llvm::raw_svector_ostream Out(OutName);
553 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
554 Out.flush();
Anders Carlsson8d145152009-12-20 22:30:54 +0000555 llvm::StringRef Name = OutName.str();
Anders Carlsson1cbce122011-01-29 19:16:51 +0000556
Anders Carlsson8d145152009-12-20 22:30:54 +0000557 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000558 if (OldGV && !OldGV->isDeclaration()) {
559 maybeUpdateRTTILinkage(CGM, OldGV, Ty);
560
Anders Carlsson8d145152009-12-20 22:30:54 +0000561 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000562 }
John McCallcbfe5022010-08-04 08:34:44 +0000563
Anders Carlsson8d145152009-12-20 22:30:54 +0000564 // Check if there is already an external RTTI descriptor for this type.
John McCallcbfe5022010-08-04 08:34:44 +0000565 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000566 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
Anders Carlsson8d145152009-12-20 22:30:54 +0000567 return GetAddrOfExternalRTTIDescriptor(Ty);
568
John McCallcbfe5022010-08-04 08:34:44 +0000569 // Emit the standard library with external linkage.
570 llvm::GlobalVariable::LinkageTypes Linkage;
571 if (IsStdLib)
572 Linkage = llvm::GlobalValue::ExternalLinkage;
573 else
Anders Carlsson3a717f72011-01-24 02:04:33 +0000574 Linkage = getTypeInfoLinkage(CGM, Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +0000575
576 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000577 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000578
579 // And the name.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000580 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
581
582 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson907c8282011-01-29 22:10:32 +0000583 Fields.push_back(llvm::ConstantExpr::getBitCast(TypeName, Int8PtrTy));
John McCallcbfe5022010-08-04 08:34:44 +0000584
Anders Carlsson8d145152009-12-20 22:30:54 +0000585 switch (Ty->getTypeClass()) {
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000586#define TYPE(Class, Base)
587#define ABSTRACT_TYPE(Class, Base)
588#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
589#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
590#define DEPENDENT_TYPE(Class, Base) case Type::Class:
591#include "clang/AST/TypeNodes.def"
592 assert(false && "Non-canonical and dependent types shouldn't get here");
Anders Carlsson8d145152009-12-20 22:30:54 +0000593
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000594 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000595 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000596 case Type::Vector:
597 case Type::ExtVector:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000598 case Type::Complex:
599 case Type::BlockPointer:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000600 // Itanium C++ ABI 2.9.5p4:
601 // abi::__fundamental_type_info adds no data members to std::type_info.
602 break;
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000603
604 case Type::LValueReference:
605 case Type::RValueReference:
606 assert(false && "References shouldn't get here");
607
Anders Carlsson978ef682009-12-29 21:58:32 +0000608 case Type::ConstantArray:
609 case Type::IncompleteArray:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000610 case Type::VariableArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000611 // Itanium C++ ABI 2.9.5p5:
612 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000613 break;
614
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000615 case Type::FunctionNoProto:
616 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000617 // Itanium C++ ABI 2.9.5p5:
618 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000619 break;
620
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000621 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000622 // Itanium C++ ABI 2.9.5p5:
623 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000624 break;
625
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000626 case Type::Record: {
627 const CXXRecordDecl *RD =
628 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000629 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000630 // We don't need to emit any fields.
631 break;
632 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000633
Anders Carlsson08148092009-12-30 23:47:56 +0000634 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000635 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000636 else
637 BuildVMIClassTypeInfo(RD);
638
639 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000640 }
John McCalle8dc53e2010-08-12 02:17:33 +0000641
642 case Type::ObjCObject:
643 case Type::ObjCInterface:
644 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
645 break;
646
647 case Type::ObjCObjectPointer:
648 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
649 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000650
Anders Carlsson8d145152009-12-20 22:30:54 +0000651 case Type::Pointer:
John McCalle8dc53e2010-08-12 02:17:33 +0000652 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000653 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000654
Anders Carlsson8d145152009-12-20 22:30:54 +0000655 case Type::MemberPointer:
656 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
657 break;
658 }
659
660 llvm::Constant *Init =
Anders Carlsson531d55f2009-12-31 17:43:53 +0000661 llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
Anders Carlsson8d145152009-12-20 22:30:54 +0000662 /*Packed=*/false);
663
664 llvm::GlobalVariable *GV =
665 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
666 /*Constant=*/true, Linkage, Init, Name);
667
668 // If there's already an old global variable, replace it with the new one.
669 if (OldGV) {
670 GV->takeName(OldGV);
671 llvm::Constant *NewPtr =
672 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
673 OldGV->replaceAllUsesWith(NewPtr);
674 OldGV->eraseFromParent();
675 }
John McCallcbfe5022010-08-04 08:34:44 +0000676
677 // GCC only relies on the uniqueness of the type names, not the
678 // type_infos themselves, so we can emit these as hidden symbols.
John McCall279b5eb2010-08-12 23:36:15 +0000679 // But don't do this if we're worried about strict visibility
680 // compatibility.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000681 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) {
682 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
683
684 CGM.setTypeVisibility(GV, RD, CodeGenModule::TVK_ForRTTI);
685 CGM.setTypeVisibility(TypeName, RD, CodeGenModule::TVK_ForRTTIName);
Anders Carlsson907c8282011-01-29 22:10:32 +0000686 } else {
687 Visibility TypeInfoVisibility = DefaultVisibility;
688 if (CGM.getCodeGenOpts().HiddenWeakVTables &&
689 Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
690 TypeInfoVisibility = HiddenVisibility;
Anders Carlsson9a86a132011-01-29 20:36:11 +0000691
Anders Carlsson907c8282011-01-29 22:10:32 +0000692 // The type name should have the same visibility as the type itself.
693 Visibility ExplicitVisibility = Ty->getVisibility();
694 TypeName->setVisibility(CodeGenModule::
695 GetLLVMVisibility(ExplicitVisibility));
696
697 TypeInfoVisibility = minVisibility(TypeInfoVisibility, Ty->getVisibility());
698 GV->setVisibility(CodeGenModule::GetLLVMVisibility(TypeInfoVisibility));
Rafael Espindolab1c65ff2011-01-11 21:44:37 +0000699 }
Anders Carlsson907c8282011-01-29 22:10:32 +0000700
Rafael Espindola57244f62011-01-11 23:55:05 +0000701 GV->setUnnamedAddr(true);
702
Anders Carlsson8d145152009-12-20 22:30:54 +0000703 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
704}
705
Anders Carlsson08148092009-12-30 23:47:56 +0000706/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000707/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000708static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000709 unsigned Flags = 0;
710
711 if (Quals.hasConst())
712 Flags |= RTTIBuilder::PTI_Const;
713 if (Quals.hasVolatile())
714 Flags |= RTTIBuilder::PTI_Volatile;
715 if (Quals.hasRestrict())
716 Flags |= RTTIBuilder::PTI_Restrict;
717
718 return Flags;
719}
720
John McCalle8dc53e2010-08-12 02:17:33 +0000721/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
722/// for the given Objective-C object type.
723void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
724 // Drop qualifiers.
725 const Type *T = OT->getBaseType().getTypePtr();
726 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
727
728 // The builtin types are abi::__class_type_infos and don't require
729 // extra fields.
730 if (isa<BuiltinType>(T)) return;
731
732 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
733 ObjCInterfaceDecl *Super = Class->getSuperClass();
734
735 // Root classes are also __class_type_info.
736 if (!Super) return;
737
738 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
739
740 // Everything else is single inheritance.
741 llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
742 Fields.push_back(BaseTypeInfo);
743}
744
Anders Carlssonf64531a2009-12-30 01:00:12 +0000745/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
746/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
747void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
748 // Itanium C++ ABI 2.9.5p6b:
749 // It adds to abi::__class_type_info a single member pointing to the
750 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000751 llvm::Constant *BaseTypeInfo =
752 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
753 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000754}
755
Benjamin Kramer79ba2a62010-10-22 16:48:22 +0000756namespace {
757 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
758 /// a class hierarchy.
759 struct SeenBases {
760 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
761 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
762 };
763}
Anders Carlsson08148092009-12-30 23:47:56 +0000764
765/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
766/// abi::__vmi_class_type_info.
767///
768static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
769 SeenBases &Bases) {
770
771 unsigned Flags = 0;
772
773 const CXXRecordDecl *BaseDecl =
774 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
775
776 if (Base->isVirtual()) {
777 if (Bases.VirtualBases.count(BaseDecl)) {
778 // If this virtual base has been seen before, then the class is diamond
779 // shaped.
780 Flags |= RTTIBuilder::VMI_DiamondShaped;
781 } else {
782 if (Bases.NonVirtualBases.count(BaseDecl))
783 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
784
785 // Mark the virtual base as seen.
786 Bases.VirtualBases.insert(BaseDecl);
787 }
788 } else {
789 if (Bases.NonVirtualBases.count(BaseDecl)) {
790 // If this non-virtual base has been seen before, then the class has non-
791 // diamond shaped repeated inheritance.
792 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
793 } else {
794 if (Bases.VirtualBases.count(BaseDecl))
795 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
796
797 // Mark the non-virtual base as seen.
798 Bases.NonVirtualBases.insert(BaseDecl);
799 }
800 }
801
802 // Walk all bases.
803 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
804 E = BaseDecl->bases_end(); I != E; ++I)
805 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
806
807 return Flags;
808}
809
810static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
811 unsigned Flags = 0;
812 SeenBases Bases;
813
814 // Walk all bases.
815 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
816 E = RD->bases_end(); I != E; ++I)
817 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
818
819 return Flags;
820}
821
822/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
823/// classes with bases that do not satisfy the abi::__si_class_type_info
824/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
825void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
826 const llvm::Type *UnsignedIntLTy =
827 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
828
829 // Itanium C++ ABI 2.9.5p6c:
830 // __flags is a word with flags describing details about the class
831 // structure, which may be referenced by using the __flags_masks
832 // enumeration. These flags refer to both direct and indirect bases.
833 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000834 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000835
836 // Itanium C++ ABI 2.9.5p6c:
837 // __base_count is a word with the number of direct proper base class
838 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000839 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000840
841 if (!RD->getNumBases())
842 return;
843
844 const llvm::Type *LongLTy =
845 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
846
847 // Now add the base class descriptions.
848
849 // Itanium C++ ABI 2.9.5p6c:
850 // __base_info[] is an array of base class descriptions -- one for every
851 // direct proper base. Each description is of the type:
852 //
853 // struct abi::__base_class_type_info {
Eli Friedmana7e68452010-08-22 01:00:03 +0000854 // public:
Anders Carlsson08148092009-12-30 23:47:56 +0000855 // const __class_type_info *__base_type;
856 // long __offset_flags;
857 //
858 // enum __offset_flags_masks {
859 // __virtual_mask = 0x1,
860 // __public_mask = 0x2,
861 // __offset_shift = 8
862 // };
863 // };
864 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
865 E = RD->bases_end(); I != E; ++I) {
866 const CXXBaseSpecifier *Base = I;
867
868 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000869 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000870
871 const CXXRecordDecl *BaseDecl =
872 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
873
874 int64_t OffsetFlags = 0;
875
876 // All but the lower 8 bits of __offset_flags are a signed offset.
877 // For a non-virtual base, this is the offset in the object of the base
878 // subobject. For a virtual base, this is the offset in the virtual table of
879 // the virtual base offset for the virtual base referenced (negative).
880 if (Base->isVirtual())
Ken Dyck14c65ca2011-04-07 12:37:09 +0000881 OffsetFlags =
882 CGM.getVTables().getVirtualBaseOffsetOffset(RD, BaseDecl).getQuantity();
Anders Carlsson08148092009-12-30 23:47:56 +0000883 else {
884 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Anders Carlssona14f5972010-10-31 23:22:37 +0000885 OffsetFlags = Layout.getBaseClassOffsetInBits(BaseDecl) / 8;
Anders Carlsson08148092009-12-30 23:47:56 +0000886 };
887
888 OffsetFlags <<= 8;
889
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
919 const llvm::Type *UnsignedIntLTy =
920 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
Anders Carlsson8d145152009-12-20 22:30:54 +0000956 const llvm::Type *UnsignedIntLTy =
957 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) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000980 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
981 return llvm::Constant::getNullValue(Int8PtrTy);
982 }
David Chisnall80558d22011-03-20 21:35:39 +0000983
984 if (ForEH && Ty->isObjCObjectPointerType() && !Features.NeXTRuntime) {
985 return Runtime->GetEHType(Ty);
986 }
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}