blob: 100f73377d6799bc0c60091648796a20a6b929b8 [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"
19
Anders Carlsson656e4c12009-10-10 20:49:04 +000020using namespace clang;
21using namespace CodeGen;
22
Mike Stump92f2fe22009-12-02 19:07:44 +000023namespace {
Mike Stumpde050572009-12-02 18:57:08 +000024class RTTIBuilder {
Mike Stump2b1bf312009-11-14 14:25:18 +000025 CodeGenModule &CGM; // Per-module state.
26 llvm::LLVMContext &VMContext;
Anders Carlsson8d145152009-12-20 22:30:54 +000027
Anders Carlsson08148092009-12-30 23:47:56 +000028 const llvm::Type *Int8PtrTy;
Anders Carlsson531d55f2009-12-31 17:43:53 +000029
30 /// Fields - The fields of the RTTI descriptor currently being built.
31 llvm::SmallVector<llvm::Constant *, 16> Fields;
Anders Carlssond6baec82009-12-11 01:27:37 +000032
Anders Carlsson9a86a132011-01-29 20:36:11 +000033 /// GetAddrOfTypeName - Returns the mangled type name of the given type.
34 llvm::GlobalVariable *
35 GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage);
36
Anders Carlsson1d7088d2009-12-17 07:09:17 +000037 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
38 /// descriptor of the given type.
39 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
40
Anders Carlsson046c2942010-04-17 20:15:18 +000041 /// BuildVTablePointer - Build the vtable pointer for the given type.
42 void BuildVTablePointer(const Type *Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +000043
Anders Carlssonf64531a2009-12-30 01:00:12 +000044 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
Anders Carlsson08148092009-12-30 23:47:56 +000045 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
Anders Carlssonf64531a2009-12-30 01:00:12 +000046 void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
47
Anders Carlsson08148092009-12-30 23:47:56 +000048 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
49 /// classes with bases that do not satisfy the abi::__si_class_type_info
50 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
51 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
52
Anders Carlssonf64531a2009-12-30 01:00:12 +000053 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
54 /// for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +000055 void BuildPointerTypeInfo(QualType PointeeTy);
56
57 /// BuildObjCObjectTypeInfo - Build the appropriate kind of
58 /// type_info for an object type.
59 void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +000060
61 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
62 /// struct, used for member pointer types.
63 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
64
Mike Stump2b1bf312009-11-14 14:25:18 +000065public:
Anders Carlsson237f9592011-01-29 22:15:18 +000066 RTTIBuilder(CodeGenModule &CGM) : CGM(CGM),
67 VMContext(CGM.getModule().getContext()),
68 Int8PtrTy(llvm::Type::getInt8PtrTy(VMContext)) { }
Mike Stump2b1bf312009-11-14 14:25:18 +000069
Anders Carlsson8d145152009-12-20 22:30:54 +000070 // Pointer type info flags.
71 enum {
72 /// PTI_Const - Type has const qualifier.
73 PTI_Const = 0x1,
74
75 /// PTI_Volatile - Type has volatile qualifier.
76 PTI_Volatile = 0x2,
77
78 /// PTI_Restrict - Type has restrict qualifier.
79 PTI_Restrict = 0x4,
80
81 /// PTI_Incomplete - Type is incomplete.
82 PTI_Incomplete = 0x8,
83
84 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
85 /// (in pointer to member).
86 PTI_ContainingClassIncomplete = 0x10
87 };
Anders Carlsson08148092009-12-30 23:47:56 +000088
89 // VMI type info flags.
90 enum {
91 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
92 VMI_NonDiamondRepeat = 0x1,
93
94 /// VMI_DiamondShaped - Class is diamond shaped.
95 VMI_DiamondShaped = 0x2
96 };
97
98 // Base class type info flags.
99 enum {
100 /// BCTI_Virtual - Base class is virtual.
101 BCTI_Virtual = 0x1,
102
103 /// BCTI_Public - Base class is public.
104 BCTI_Public = 0x2
105 };
Anders Carlsson531d55f2009-12-31 17:43:53 +0000106
107 /// BuildTypeInfo - Build the RTTI type info struct for the given type.
John McCall9dffe6f2010-04-30 01:15:21 +0000108 ///
109 /// \param Force - true to force the creation of this RTTI value
110 /// \param ForEH - true if this is for exception handling
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000111 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false);
Mike Stump2b1bf312009-11-14 14:25:18 +0000112};
Mike Stump92f2fe22009-12-02 19:07:44 +0000113}
Mike Stump2b1bf312009-11-14 14:25:18 +0000114
Anders Carlsson9a86a132011-01-29 20:36:11 +0000115llvm::GlobalVariable *
116RTTIBuilder::GetAddrOfTypeName(QualType Ty,
117 llvm::GlobalVariable::LinkageTypes Linkage) {
118 llvm::SmallString<256> OutName;
119 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, OutName);
120 llvm::StringRef Name = OutName.str();
121
122 // We know that the mangled name of the type starts at index 4 of the
123 // mangled name of the typename, so we can just index into it in order to
124 // get the mangled name of the type.
125 llvm::Constant *Init = llvm::ConstantArray::get(VMContext, Name.substr(4));
126
127 llvm::GlobalVariable *GV =
128 CGM.CreateOrReplaceCXXRuntimeVariable(Name, Init->getType(), Linkage);
129
130 GV->setInitializer(Init);
131
132 return GV;
133}
134
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000135llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
136 // Mangle the RTTI name.
137 llvm::SmallString<256> OutName;
John McCall4c40d982010-08-31 07:33:07 +0000138 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, OutName);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000139 llvm::StringRef Name = OutName.str();
140
Anders Carlsson8d145152009-12-20 22:30:54 +0000141 // Look for an existing global.
142 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000143
144 if (!GV) {
145 // Create a new global variable.
146 GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
147 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000148 }
149
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000150 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000151}
152
Anders Carlsson8d145152009-12-20 22:30:54 +0000153/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
154/// info for that type is defined in the standard library.
155static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
156 // Itanium C++ ABI 2.9.2:
157 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
158 // the run-time support library. Specifically, the run-time support
159 // library should contain type_info objects for the types X, X* and
Anders Carlsson2bd62502010-11-04 05:28:09 +0000160 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
161 // unsigned char, signed char, short, unsigned short, int, unsigned int,
162 // long, unsigned long, long long, unsigned long long, float, double,
163 // long double, char16_t, char32_t, and the IEEE 754r decimal and
164 // half-precision floating point types.
Anders Carlsson8d145152009-12-20 22:30:54 +0000165 switch (Ty->getKind()) {
166 case BuiltinType::Void:
Anders Carlsson2bd62502010-11-04 05:28:09 +0000167 case BuiltinType::NullPtr:
Anders Carlsson8d145152009-12-20 22:30:54 +0000168 case BuiltinType::Bool:
Chris Lattner3f59c972010-12-25 23:25:43 +0000169 case BuiltinType::WChar_S:
170 case BuiltinType::WChar_U:
Anders Carlsson8d145152009-12-20 22:30:54 +0000171 case BuiltinType::Char_U:
172 case BuiltinType::Char_S:
173 case BuiltinType::UChar:
174 case BuiltinType::SChar:
175 case BuiltinType::Short:
176 case BuiltinType::UShort:
177 case BuiltinType::Int:
178 case BuiltinType::UInt:
179 case BuiltinType::Long:
180 case BuiltinType::ULong:
181 case BuiltinType::LongLong:
182 case BuiltinType::ULongLong:
183 case BuiltinType::Float:
184 case BuiltinType::Double:
185 case BuiltinType::LongDouble:
186 case BuiltinType::Char16:
187 case BuiltinType::Char32:
188 case BuiltinType::Int128:
189 case BuiltinType::UInt128:
190 return true;
191
192 case BuiltinType::Overload:
193 case BuiltinType::Dependent:
194 case BuiltinType::UndeducedAuto:
195 assert(false && "Should not see this type here!");
196
Anders Carlsson8d145152009-12-20 22:30:54 +0000197 case BuiltinType::ObjCId:
198 case BuiltinType::ObjCClass:
199 case BuiltinType::ObjCSel:
200 assert(false && "FIXME: Objective-C types are unsupported!");
201 }
202
203 // Silent gcc.
204 return false;
205}
206
207static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
208 QualType PointeeTy = PointerTy->getPointeeType();
209 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
210 if (!BuiltinTy)
211 return false;
212
213 // Check the qualifiers.
214 Qualifiers Quals = PointeeTy.getQualifiers();
215 Quals.removeConst();
216
217 if (!Quals.empty())
218 return false;
219
220 return TypeInfoIsInStandardLibrary(BuiltinTy);
221}
222
John McCallcbfe5022010-08-04 08:34:44 +0000223/// IsStandardLibraryRTTIDescriptor - Returns whether the type
224/// information for the given type exists in the standard library.
225static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000226 // Type info for builtin types is defined in the standard library.
227 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
228 return TypeInfoIsInStandardLibrary(BuiltinTy);
229
230 // Type info for some pointer types to builtin types is defined in the
231 // standard library.
232 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
233 return TypeInfoIsInStandardLibrary(PointerTy);
234
John McCallcbfe5022010-08-04 08:34:44 +0000235 return false;
236}
237
238/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
239/// the given type exists somewhere else, and that we should not emit the type
240/// information in this translation unit. Assumes that it is not a
241/// standard-library type.
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000242static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM, QualType Ty) {
243 ASTContext &Context = CGM.getContext();
244
John McCall9dffe6f2010-04-30 01:15:21 +0000245 // If RTTI is disabled, don't consider key functions.
246 if (!Context.getLangOptions().RTTI) return false;
247
Anders Carlsson8d145152009-12-20 22:30:54 +0000248 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000249 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000250 if (!RD->hasDefinition())
251 return false;
252
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000253 if (!RD->isDynamicClass())
254 return false;
255
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000256 return !CGM.getVTables().ShouldEmitVTableInThisTU(RD);
Anders Carlsson8d145152009-12-20 22:30:54 +0000257 }
258
259 return false;
260}
261
262/// IsIncompleteClassType - Returns whether the given record type is incomplete.
263static bool IsIncompleteClassType(const RecordType *RecordTy) {
264 return !RecordTy->getDecl()->isDefinition();
265}
266
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000267/// ContainsIncompleteClassType - Returns whether the given type contains an
268/// incomplete class type. This is true if
269///
270/// * The given type is an incomplete class type.
271/// * The given type is a pointer type whose pointee type contains an
272/// incomplete class type.
273/// * The given type is a member pointer type whose class is an incomplete
274/// class type.
275/// * The given type is a member pointer type whoise pointee type contains an
276/// incomplete class type.
Anders Carlsson8d145152009-12-20 22:30:54 +0000277/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000278static bool ContainsIncompleteClassType(QualType Ty) {
279 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
280 if (IsIncompleteClassType(RecordTy))
281 return true;
282 }
283
284 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
285 return ContainsIncompleteClassType(PointerTy->getPointeeType());
286
287 if (const MemberPointerType *MemberPointerTy =
288 dyn_cast<MemberPointerType>(Ty)) {
289 // Check if the class type is incomplete.
290 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
291 if (IsIncompleteClassType(ClassType))
292 return true;
293
294 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000295 }
296
297 return false;
298}
299
300/// getTypeInfoLinkage - Return the linkage that the type info and type info
301/// name constants should have for the given type.
Anders Carlsson3a717f72011-01-24 02:04:33 +0000302static llvm::GlobalVariable::LinkageTypes
303getTypeInfoLinkage(CodeGenModule &CGM, QualType Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000304 // Itanium C++ ABI 2.9.5p7:
305 // In addition, it and all of the intermediate abi::__pointer_type_info
306 // structs in the chain down to the abi::__class_type_info for the
307 // incomplete class type must be prevented from resolving to the
308 // corresponding type_info structs for the complete class type, possibly
309 // by making them local static objects. Finally, a dummy class RTTI is
310 // generated for the incomplete type that will not resolve to the final
311 // complete class RTTI (because the latter need not exist), possibly by
312 // making it a local static object.
313 if (ContainsIncompleteClassType(Ty))
314 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000315
Douglas Gregor031b3712010-03-31 00:15:35 +0000316 switch (Ty->getLinkage()) {
317 case NoLinkage:
318 case InternalLinkage:
319 case UniqueExternalLinkage:
320 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson978ef682009-12-29 21:58:32 +0000321
Douglas Gregor031b3712010-03-31 00:15:35 +0000322 case ExternalLinkage:
Anders Carlssone34e3aa2011-01-24 02:12:11 +0000323 if (!CGM.getLangOptions().RTTI) {
324 // RTTI is not enabled, which means that this type info struct is going
325 // to be used for exception handling. Give it linkonce_odr linkage.
326 return llvm::GlobalValue::LinkOnceODRLinkage;
327 }
328
Douglas Gregor031b3712010-03-31 00:15:35 +0000329 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
330 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
331 if (RD->isDynamicClass())
Anders Carlsson3a717f72011-01-24 02:04:33 +0000332 return CGM.getVTableLinkage(RD);
Mike Stumpc8f76f52009-12-24 01:10:27 +0000333 }
Douglas Gregor031b3712010-03-31 00:15:35 +0000334
Anders Carlssonf502d932011-01-24 00:46:19 +0000335 return llvm::GlobalValue::LinkOnceODRLinkage;
Mike Stumpc8f76f52009-12-24 01:10:27 +0000336 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000337
Anders Carlssonf502d932011-01-24 00:46:19 +0000338 return llvm::GlobalValue::LinkOnceODRLinkage;
Anders Carlsson8d145152009-12-20 22:30:54 +0000339}
340
Anders Carlssonf64531a2009-12-30 01:00:12 +0000341// CanUseSingleInheritance - Return whether the given record decl has a "single,
342// public, non-virtual base at offset zero (i.e. the derived class is dynamic
343// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
344static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
345 // Check the number of bases.
346 if (RD->getNumBases() != 1)
347 return false;
348
349 // Get the base.
350 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
351
352 // Check that the base is not virtual.
353 if (Base->isVirtual())
354 return false;
355
356 // Check that the base is public.
357 if (Base->getAccessSpecifier() != AS_public)
358 return false;
359
360 // Check that the class is dynamic iff the base is.
361 const CXXRecordDecl *BaseDecl =
362 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
363 if (!BaseDecl->isEmpty() &&
364 BaseDecl->isDynamicClass() != RD->isDynamicClass())
365 return false;
366
367 return true;
368}
369
Anders Carlsson046c2942010-04-17 20:15:18 +0000370void RTTIBuilder::BuildVTablePointer(const Type *Ty) {
John McCalle8dc53e2010-08-12 02:17:33 +0000371 // abi::__class_type_info.
372 static const char * const ClassTypeInfo =
373 "_ZTVN10__cxxabiv117__class_type_infoE";
374 // abi::__si_class_type_info.
375 static const char * const SIClassTypeInfo =
376 "_ZTVN10__cxxabiv120__si_class_type_infoE";
377 // abi::__vmi_class_type_info.
378 static const char * const VMIClassTypeInfo =
379 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
380
Eli Friedman1cf26f52010-08-11 20:41:51 +0000381 const char *VTableName = 0;
Anders Carlsson8d145152009-12-20 22:30:54 +0000382
383 switch (Ty->getTypeClass()) {
Eli Friedman1cf26f52010-08-11 20:41:51 +0000384#define TYPE(Class, Base)
385#define ABSTRACT_TYPE(Class, Base)
386#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
387#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
388#define DEPENDENT_TYPE(Class, Base) case Type::Class:
389#include "clang/AST/TypeNodes.def"
390 assert(false && "Non-canonical and dependent types shouldn't get here");
391
392 case Type::LValueReference:
393 case Type::RValueReference:
394 assert(false && "References shouldn't get here");
Anders Carlsson978ef682009-12-29 21:58:32 +0000395
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000396 case Type::Builtin:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000397 // GCC treats vector and complex types as fundamental types.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000398 case Type::Vector:
399 case Type::ExtVector:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000400 case Type::Complex:
401 // FIXME: GCC treats block pointers as fundamental types?!
402 case Type::BlockPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000403 // abi::__fundamental_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000404 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000405 break;
406
Anders Carlsson978ef682009-12-29 21:58:32 +0000407 case Type::ConstantArray:
408 case Type::IncompleteArray:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000409 case Type::VariableArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000410 // abi::__array_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000411 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000412 break;
413
414 case Type::FunctionNoProto:
415 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000416 // abi::__function_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000417 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000418 break;
419
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000420 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000421 // abi::__enum_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000422 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000423 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000424
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000425 case Type::Record: {
426 const CXXRecordDecl *RD =
427 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000428
John McCall86ff3082010-02-04 22:26:26 +0000429 if (!RD->hasDefinition() || !RD->getNumBases()) {
John McCalle8dc53e2010-08-12 02:17:33 +0000430 VTableName = ClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000431 } else if (CanUseSingleInheritance(RD)) {
John McCalle8dc53e2010-08-12 02:17:33 +0000432 VTableName = SIClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000433 } else {
John McCalle8dc53e2010-08-12 02:17:33 +0000434 VTableName = VMIClassTypeInfo;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000435 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000436
437 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000438 }
439
Eli Friedman1cf26f52010-08-11 20:41:51 +0000440 case Type::ObjCObject:
John McCalle8dc53e2010-08-12 02:17:33 +0000441 // Ignore protocol qualifiers.
442 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
443
444 // Handle id and Class.
445 if (isa<BuiltinType>(Ty)) {
446 VTableName = ClassTypeInfo;
447 break;
448 }
449
450 assert(isa<ObjCInterfaceType>(Ty));
451 // Fall through.
452
Eli Friedman1cf26f52010-08-11 20:41:51 +0000453 case Type::ObjCInterface:
John McCalle8dc53e2010-08-12 02:17:33 +0000454 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
455 VTableName = SIClassTypeInfo;
456 } else {
457 VTableName = ClassTypeInfo;
458 }
Eli Friedman1cf26f52010-08-11 20:41:51 +0000459 break;
460
John McCalle8dc53e2010-08-12 02:17:33 +0000461 case Type::ObjCObjectPointer:
Anders Carlsson8d145152009-12-20 22:30:54 +0000462 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000463 // abi::__pointer_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000464 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000465 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000466
Anders Carlsson8d145152009-12-20 22:30:54 +0000467 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000468 // abi::__pointer_to_member_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000469 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000470 break;
471 }
472
Anders Carlsson046c2942010-04-17 20:15:18 +0000473 llvm::Constant *VTable =
474 CGM.getModule().getOrInsertGlobal(VTableName, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000475
476 const llvm::Type *PtrDiffTy =
477 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
478
479 // The vtable address point is 2.
480 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Anders Carlsson046c2942010-04-17 20:15:18 +0000481 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, &Two, 1);
482 VTable = llvm::ConstantExpr::getBitCast(VTable, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000483
Anders Carlsson046c2942010-04-17 20:15:18 +0000484 Fields.push_back(VTable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000485}
486
John McCallcbfe5022010-08-04 08:34:44 +0000487llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000488 // We want to operate on the canonical type.
489 Ty = CGM.getContext().getCanonicalType(Ty);
490
491 // Check if we've already emitted an RTTI descriptor for this type.
492 llvm::SmallString<256> OutName;
John McCall4c40d982010-08-31 07:33:07 +0000493 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, OutName);
Anders Carlsson8d145152009-12-20 22:30:54 +0000494 llvm::StringRef Name = OutName.str();
Anders Carlsson1cbce122011-01-29 19:16:51 +0000495
Anders Carlsson8d145152009-12-20 22:30:54 +0000496 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
497 if (OldGV && !OldGV->isDeclaration())
498 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
John McCallcbfe5022010-08-04 08:34:44 +0000499
Anders Carlsson8d145152009-12-20 22:30:54 +0000500 // Check if there is already an external RTTI descriptor for this type.
John McCallcbfe5022010-08-04 08:34:44 +0000501 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000502 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
Anders Carlsson8d145152009-12-20 22:30:54 +0000503 return GetAddrOfExternalRTTIDescriptor(Ty);
504
John McCallcbfe5022010-08-04 08:34:44 +0000505 // Emit the standard library with external linkage.
506 llvm::GlobalVariable::LinkageTypes Linkage;
507 if (IsStdLib)
508 Linkage = llvm::GlobalValue::ExternalLinkage;
509 else
Anders Carlsson3a717f72011-01-24 02:04:33 +0000510 Linkage = getTypeInfoLinkage(CGM, Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +0000511
512 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000513 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000514
515 // And the name.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000516 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
517
518 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson907c8282011-01-29 22:10:32 +0000519 Fields.push_back(llvm::ConstantExpr::getBitCast(TypeName, Int8PtrTy));
John McCallcbfe5022010-08-04 08:34:44 +0000520
Anders Carlsson8d145152009-12-20 22:30:54 +0000521 switch (Ty->getTypeClass()) {
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000522#define TYPE(Class, Base)
523#define ABSTRACT_TYPE(Class, Base)
524#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
525#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
526#define DEPENDENT_TYPE(Class, Base) case Type::Class:
527#include "clang/AST/TypeNodes.def"
528 assert(false && "Non-canonical and dependent types shouldn't get here");
Anders Carlsson8d145152009-12-20 22:30:54 +0000529
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000530 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000531 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000532 case Type::Vector:
533 case Type::ExtVector:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000534 case Type::Complex:
535 case Type::BlockPointer:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000536 // Itanium C++ ABI 2.9.5p4:
537 // abi::__fundamental_type_info adds no data members to std::type_info.
538 break;
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000539
540 case Type::LValueReference:
541 case Type::RValueReference:
542 assert(false && "References shouldn't get here");
543
Anders Carlsson978ef682009-12-29 21:58:32 +0000544 case Type::ConstantArray:
545 case Type::IncompleteArray:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000546 case Type::VariableArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000547 // Itanium C++ ABI 2.9.5p5:
548 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000549 break;
550
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000551 case Type::FunctionNoProto:
552 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000553 // Itanium C++ ABI 2.9.5p5:
554 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000555 break;
556
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000557 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000558 // Itanium C++ ABI 2.9.5p5:
559 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000560 break;
561
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000562 case Type::Record: {
563 const CXXRecordDecl *RD =
564 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000565 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000566 // We don't need to emit any fields.
567 break;
568 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000569
Anders Carlsson08148092009-12-30 23:47:56 +0000570 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000571 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000572 else
573 BuildVMIClassTypeInfo(RD);
574
575 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000576 }
John McCalle8dc53e2010-08-12 02:17:33 +0000577
578 case Type::ObjCObject:
579 case Type::ObjCInterface:
580 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
581 break;
582
583 case Type::ObjCObjectPointer:
584 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
585 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000586
Anders Carlsson8d145152009-12-20 22:30:54 +0000587 case Type::Pointer:
John McCalle8dc53e2010-08-12 02:17:33 +0000588 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000589 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000590
Anders Carlsson8d145152009-12-20 22:30:54 +0000591 case Type::MemberPointer:
592 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
593 break;
594 }
595
596 llvm::Constant *Init =
Anders Carlsson531d55f2009-12-31 17:43:53 +0000597 llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
Anders Carlsson8d145152009-12-20 22:30:54 +0000598 /*Packed=*/false);
599
600 llvm::GlobalVariable *GV =
601 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
602 /*Constant=*/true, Linkage, Init, Name);
603
604 // If there's already an old global variable, replace it with the new one.
605 if (OldGV) {
606 GV->takeName(OldGV);
607 llvm::Constant *NewPtr =
608 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
609 OldGV->replaceAllUsesWith(NewPtr);
610 OldGV->eraseFromParent();
611 }
John McCallcbfe5022010-08-04 08:34:44 +0000612
613 // GCC only relies on the uniqueness of the type names, not the
614 // type_infos themselves, so we can emit these as hidden symbols.
John McCall279b5eb2010-08-12 23:36:15 +0000615 // But don't do this if we're worried about strict visibility
616 // compatibility.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000617 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) {
618 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
619
620 CGM.setTypeVisibility(GV, RD, CodeGenModule::TVK_ForRTTI);
621 CGM.setTypeVisibility(TypeName, RD, CodeGenModule::TVK_ForRTTIName);
Anders Carlsson907c8282011-01-29 22:10:32 +0000622 } else {
623 Visibility TypeInfoVisibility = DefaultVisibility;
624 if (CGM.getCodeGenOpts().HiddenWeakVTables &&
625 Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
626 TypeInfoVisibility = HiddenVisibility;
Anders Carlsson9a86a132011-01-29 20:36:11 +0000627
Anders Carlsson907c8282011-01-29 22:10:32 +0000628 // The type name should have the same visibility as the type itself.
629 Visibility ExplicitVisibility = Ty->getVisibility();
630 TypeName->setVisibility(CodeGenModule::
631 GetLLVMVisibility(ExplicitVisibility));
632
633 TypeInfoVisibility = minVisibility(TypeInfoVisibility, Ty->getVisibility());
634 GV->setVisibility(CodeGenModule::GetLLVMVisibility(TypeInfoVisibility));
Rafael Espindolab1c65ff2011-01-11 21:44:37 +0000635 }
Anders Carlsson907c8282011-01-29 22:10:32 +0000636
Rafael Espindola57244f62011-01-11 23:55:05 +0000637 GV->setUnnamedAddr(true);
638
Anders Carlsson8d145152009-12-20 22:30:54 +0000639 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
640}
641
Anders Carlsson08148092009-12-30 23:47:56 +0000642/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000643/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000644static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000645 unsigned Flags = 0;
646
647 if (Quals.hasConst())
648 Flags |= RTTIBuilder::PTI_Const;
649 if (Quals.hasVolatile())
650 Flags |= RTTIBuilder::PTI_Volatile;
651 if (Quals.hasRestrict())
652 Flags |= RTTIBuilder::PTI_Restrict;
653
654 return Flags;
655}
656
John McCalle8dc53e2010-08-12 02:17:33 +0000657/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
658/// for the given Objective-C object type.
659void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
660 // Drop qualifiers.
661 const Type *T = OT->getBaseType().getTypePtr();
662 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
663
664 // The builtin types are abi::__class_type_infos and don't require
665 // extra fields.
666 if (isa<BuiltinType>(T)) return;
667
668 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
669 ObjCInterfaceDecl *Super = Class->getSuperClass();
670
671 // Root classes are also __class_type_info.
672 if (!Super) return;
673
674 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
675
676 // Everything else is single inheritance.
677 llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
678 Fields.push_back(BaseTypeInfo);
679}
680
Anders Carlssonf64531a2009-12-30 01:00:12 +0000681/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
682/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
683void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
684 // Itanium C++ ABI 2.9.5p6b:
685 // It adds to abi::__class_type_info a single member pointing to the
686 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000687 llvm::Constant *BaseTypeInfo =
688 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
689 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000690}
691
Benjamin Kramer79ba2a62010-10-22 16:48:22 +0000692namespace {
693 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
694 /// a class hierarchy.
695 struct SeenBases {
696 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
697 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
698 };
699}
Anders Carlsson08148092009-12-30 23:47:56 +0000700
701/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
702/// abi::__vmi_class_type_info.
703///
704static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
705 SeenBases &Bases) {
706
707 unsigned Flags = 0;
708
709 const CXXRecordDecl *BaseDecl =
710 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
711
712 if (Base->isVirtual()) {
713 if (Bases.VirtualBases.count(BaseDecl)) {
714 // If this virtual base has been seen before, then the class is diamond
715 // shaped.
716 Flags |= RTTIBuilder::VMI_DiamondShaped;
717 } else {
718 if (Bases.NonVirtualBases.count(BaseDecl))
719 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
720
721 // Mark the virtual base as seen.
722 Bases.VirtualBases.insert(BaseDecl);
723 }
724 } else {
725 if (Bases.NonVirtualBases.count(BaseDecl)) {
726 // If this non-virtual base has been seen before, then the class has non-
727 // diamond shaped repeated inheritance.
728 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
729 } else {
730 if (Bases.VirtualBases.count(BaseDecl))
731 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
732
733 // Mark the non-virtual base as seen.
734 Bases.NonVirtualBases.insert(BaseDecl);
735 }
736 }
737
738 // Walk all bases.
739 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
740 E = BaseDecl->bases_end(); I != E; ++I)
741 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
742
743 return Flags;
744}
745
746static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
747 unsigned Flags = 0;
748 SeenBases Bases;
749
750 // Walk all bases.
751 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
752 E = RD->bases_end(); I != E; ++I)
753 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
754
755 return Flags;
756}
757
758/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
759/// classes with bases that do not satisfy the abi::__si_class_type_info
760/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
761void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
762 const llvm::Type *UnsignedIntLTy =
763 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
764
765 // Itanium C++ ABI 2.9.5p6c:
766 // __flags is a word with flags describing details about the class
767 // structure, which may be referenced by using the __flags_masks
768 // enumeration. These flags refer to both direct and indirect bases.
769 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000770 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000771
772 // Itanium C++ ABI 2.9.5p6c:
773 // __base_count is a word with the number of direct proper base class
774 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000775 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000776
777 if (!RD->getNumBases())
778 return;
779
780 const llvm::Type *LongLTy =
781 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
782
783 // Now add the base class descriptions.
784
785 // Itanium C++ ABI 2.9.5p6c:
786 // __base_info[] is an array of base class descriptions -- one for every
787 // direct proper base. Each description is of the type:
788 //
789 // struct abi::__base_class_type_info {
Eli Friedmana7e68452010-08-22 01:00:03 +0000790 // public:
Anders Carlsson08148092009-12-30 23:47:56 +0000791 // const __class_type_info *__base_type;
792 // long __offset_flags;
793 //
794 // enum __offset_flags_masks {
795 // __virtual_mask = 0x1,
796 // __public_mask = 0x2,
797 // __offset_shift = 8
798 // };
799 // };
800 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
801 E = RD->bases_end(); I != E; ++I) {
802 const CXXBaseSpecifier *Base = I;
803
804 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000805 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000806
807 const CXXRecordDecl *BaseDecl =
808 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
809
810 int64_t OffsetFlags = 0;
811
812 // All but the lower 8 bits of __offset_flags are a signed offset.
813 // For a non-virtual base, this is the offset in the object of the base
814 // subobject. For a virtual base, this is the offset in the virtual table of
815 // the virtual base offset for the virtual base referenced (negative).
816 if (Base->isVirtual())
Anders Carlssonaf440352010-03-23 04:11:45 +0000817 OffsetFlags = CGM.getVTables().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000818 else {
819 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Anders Carlssona14f5972010-10-31 23:22:37 +0000820 OffsetFlags = Layout.getBaseClassOffsetInBits(BaseDecl) / 8;
Anders Carlsson08148092009-12-30 23:47:56 +0000821 };
822
823 OffsetFlags <<= 8;
824
825 // The low-order byte of __offset_flags contains flags, as given by the
826 // masks from the enumeration __offset_flags_masks.
827 if (Base->isVirtual())
828 OffsetFlags |= BCTI_Virtual;
829 if (Base->getAccessSpecifier() == AS_public)
830 OffsetFlags |= BCTI_Public;
831
Anders Carlsson531d55f2009-12-31 17:43:53 +0000832 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000833 }
834}
835
Anders Carlsson8d145152009-12-20 22:30:54 +0000836/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
837/// used for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +0000838void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
Anders Carlssonabd6b092010-06-02 15:44:35 +0000839 Qualifiers Quals;
840 QualType UnqualifiedPointeeTy =
841 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
842
Anders Carlsson8d145152009-12-20 22:30:54 +0000843 // Itanium C++ ABI 2.9.5p7:
844 // __flags is a flag word describing the cv-qualification and other
845 // attributes of the type pointed to
Anders Carlssonabd6b092010-06-02 15:44:35 +0000846 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000847
848 // Itanium C++ ABI 2.9.5p7:
849 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
850 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000851 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000852 Flags |= PTI_Incomplete;
853
854 const llvm::Type *UnsignedIntLTy =
855 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000856 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000857
858 // Itanium C++ ABI 2.9.5p7:
859 // __pointee is a pointer to the std::type_info derivation for the
860 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000861 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000862 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000863 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000864}
865
866/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
867/// struct, used for member pointer types.
868void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
869 QualType PointeeTy = Ty->getPointeeType();
870
Anders Carlssonabd6b092010-06-02 15:44:35 +0000871 Qualifiers Quals;
872 QualType UnqualifiedPointeeTy =
873 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
874
Anders Carlsson8d145152009-12-20 22:30:54 +0000875 // Itanium C++ ABI 2.9.5p7:
876 // __flags is a flag word describing the cv-qualification and other
877 // attributes of the type pointed to.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000878 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000879
880 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000881
882 // Itanium C++ ABI 2.9.5p7:
883 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
884 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000885 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000886 Flags |= PTI_Incomplete;
887
Anders Carlsson8d145152009-12-20 22:30:54 +0000888 if (IsIncompleteClassType(ClassType))
889 Flags |= PTI_ContainingClassIncomplete;
890
Anders Carlsson8d145152009-12-20 22:30:54 +0000891 const llvm::Type *UnsignedIntLTy =
892 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000893 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000894
895 // Itanium C++ ABI 2.9.5p7:
896 // __pointee is a pointer to the std::type_info derivation for the
897 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000898 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000899 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000900 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000901
902 // Itanium C++ ABI 2.9.5p9:
903 // __context is a pointer to an abi::__class_type_info corresponding to the
904 // class type containing the member pointed to
905 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000906 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000907}
908
John McCall9dffe6f2010-04-30 01:15:21 +0000909llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
910 bool ForEH) {
911 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
912 // FIXME: should we even be calling this method if RTTI is disabled
913 // and it's not for EH?
914 if (!ForEH && !getContext().getLangOptions().RTTI) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000915 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
916 return llvm::Constant::getNullValue(Int8PtrTy);
917 }
John McCall9dffe6f2010-04-30 01:15:21 +0000918
Anders Carlsson531d55f2009-12-31 17:43:53 +0000919 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000920}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000921
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000922void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
923 QualType PointerType = Context.getPointerType(Type);
924 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
925 RTTIBuilder(*this).BuildTypeInfo(Type, true);
926 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
927 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
928}
929
930void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Anders Carlsson2bd62502010-11-04 05:28:09 +0000931 QualType FundamentalTypes[] = { Context.VoidTy, Context.NullPtrTy,
932 Context.BoolTy, Context.WCharTy,
933 Context.CharTy, Context.UnsignedCharTy,
934 Context.SignedCharTy, Context.ShortTy,
935 Context.UnsignedShortTy, Context.IntTy,
936 Context.UnsignedIntTy, Context.LongTy,
937 Context.UnsignedLongTy, Context.LongLongTy,
938 Context.UnsignedLongLongTy, Context.FloatTy,
939 Context.DoubleTy, Context.LongDoubleTy,
940 Context.Char16Ty, Context.Char32Ty };
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000941 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
942 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
943}