blob: c358c48e80ccebbb21a4d9ae4e386c6500322ee0 [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
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000487// maybeUpdateRTTILinkage - Will update the linkage of the RTTI data structures
488// from available_externally to the correct linkage if necessary. An example of
489// this is:
490//
491// struct A {
492// virtual void f();
493// };
494//
495// const std::type_info &g() {
496// return typeid(A);
497// }
498//
499// void A::f() { }
500//
501// When we're generating the typeid(A) expression, we do not yet know that
502// A's key function is defined in this translation unit, so we will give the
503// typeinfo and typename structures available_externally linkage. When A::f
504// forces the vtable to be generated, we need to change the linkage of the
505// typeinfo and typename structs, otherwise we'll end up with undefined
506// externals when linking.
507static void
508maybeUpdateRTTILinkage(CodeGenModule &CGM, llvm::GlobalVariable *GV,
509 QualType Ty) {
510 // We're only interested in globals with available_externally linkage.
511 if (!GV->hasAvailableExternallyLinkage())
512 return;
513
514 // Get the real linkage for the type.
515 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(CGM, Ty);
516
517 // If variable is supposed to have available_externally linkage, we don't
518 // need to do anything.
519 if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
520 return;
521
522 // Update the typeinfo linkage.
523 GV->setLinkage(Linkage);
524
525 // Get the typename global.
526 llvm::SmallString<256> OutName;
527 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, OutName);
528 llvm::StringRef Name = OutName.str();
529
530 llvm::GlobalVariable *TypeNameGV = CGM.getModule().getNamedGlobal(Name);
531
532 assert(TypeNameGV->hasAvailableExternallyLinkage() &&
533 "Type name has different linkage from type info!");
534
535 // And update its linkage.
536 TypeNameGV->setLinkage(Linkage);
537}
538
John McCallcbfe5022010-08-04 08:34:44 +0000539llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000540 // We want to operate on the canonical type.
541 Ty = CGM.getContext().getCanonicalType(Ty);
542
543 // Check if we've already emitted an RTTI descriptor for this type.
544 llvm::SmallString<256> OutName;
John McCall4c40d982010-08-31 07:33:07 +0000545 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, OutName);
Anders Carlsson8d145152009-12-20 22:30:54 +0000546 llvm::StringRef Name = OutName.str();
Anders Carlsson1cbce122011-01-29 19:16:51 +0000547
Anders Carlsson8d145152009-12-20 22:30:54 +0000548 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000549 if (OldGV && !OldGV->isDeclaration()) {
550 maybeUpdateRTTILinkage(CGM, OldGV, Ty);
551
Anders Carlsson8d145152009-12-20 22:30:54 +0000552 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000553 }
John McCallcbfe5022010-08-04 08:34:44 +0000554
Anders Carlsson8d145152009-12-20 22:30:54 +0000555 // Check if there is already an external RTTI descriptor for this type.
John McCallcbfe5022010-08-04 08:34:44 +0000556 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000557 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
Anders Carlsson8d145152009-12-20 22:30:54 +0000558 return GetAddrOfExternalRTTIDescriptor(Ty);
559
John McCallcbfe5022010-08-04 08:34:44 +0000560 // Emit the standard library with external linkage.
561 llvm::GlobalVariable::LinkageTypes Linkage;
562 if (IsStdLib)
563 Linkage = llvm::GlobalValue::ExternalLinkage;
564 else
Anders Carlsson3a717f72011-01-24 02:04:33 +0000565 Linkage = getTypeInfoLinkage(CGM, Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +0000566
567 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000568 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000569
570 // And the name.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000571 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
572
573 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson907c8282011-01-29 22:10:32 +0000574 Fields.push_back(llvm::ConstantExpr::getBitCast(TypeName, Int8PtrTy));
John McCallcbfe5022010-08-04 08:34:44 +0000575
Anders Carlsson8d145152009-12-20 22:30:54 +0000576 switch (Ty->getTypeClass()) {
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000577#define TYPE(Class, Base)
578#define ABSTRACT_TYPE(Class, Base)
579#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
580#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
581#define DEPENDENT_TYPE(Class, Base) case Type::Class:
582#include "clang/AST/TypeNodes.def"
583 assert(false && "Non-canonical and dependent types shouldn't get here");
Anders Carlsson8d145152009-12-20 22:30:54 +0000584
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000585 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000586 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000587 case Type::Vector:
588 case Type::ExtVector:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000589 case Type::Complex:
590 case Type::BlockPointer:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000591 // Itanium C++ ABI 2.9.5p4:
592 // abi::__fundamental_type_info adds no data members to std::type_info.
593 break;
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000594
595 case Type::LValueReference:
596 case Type::RValueReference:
597 assert(false && "References shouldn't get here");
598
Anders Carlsson978ef682009-12-29 21:58:32 +0000599 case Type::ConstantArray:
600 case Type::IncompleteArray:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000601 case Type::VariableArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000602 // Itanium C++ ABI 2.9.5p5:
603 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000604 break;
605
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000606 case Type::FunctionNoProto:
607 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000608 // Itanium C++ ABI 2.9.5p5:
609 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000610 break;
611
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000612 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000613 // Itanium C++ ABI 2.9.5p5:
614 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000615 break;
616
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000617 case Type::Record: {
618 const CXXRecordDecl *RD =
619 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000620 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000621 // We don't need to emit any fields.
622 break;
623 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000624
Anders Carlsson08148092009-12-30 23:47:56 +0000625 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000626 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000627 else
628 BuildVMIClassTypeInfo(RD);
629
630 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000631 }
John McCalle8dc53e2010-08-12 02:17:33 +0000632
633 case Type::ObjCObject:
634 case Type::ObjCInterface:
635 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
636 break;
637
638 case Type::ObjCObjectPointer:
639 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
640 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000641
Anders Carlsson8d145152009-12-20 22:30:54 +0000642 case Type::Pointer:
John McCalle8dc53e2010-08-12 02:17:33 +0000643 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000644 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000645
Anders Carlsson8d145152009-12-20 22:30:54 +0000646 case Type::MemberPointer:
647 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
648 break;
649 }
650
651 llvm::Constant *Init =
Anders Carlsson531d55f2009-12-31 17:43:53 +0000652 llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
Anders Carlsson8d145152009-12-20 22:30:54 +0000653 /*Packed=*/false);
654
655 llvm::GlobalVariable *GV =
656 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
657 /*Constant=*/true, Linkage, Init, Name);
658
659 // If there's already an old global variable, replace it with the new one.
660 if (OldGV) {
661 GV->takeName(OldGV);
662 llvm::Constant *NewPtr =
663 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
664 OldGV->replaceAllUsesWith(NewPtr);
665 OldGV->eraseFromParent();
666 }
John McCallcbfe5022010-08-04 08:34:44 +0000667
668 // GCC only relies on the uniqueness of the type names, not the
669 // type_infos themselves, so we can emit these as hidden symbols.
John McCall279b5eb2010-08-12 23:36:15 +0000670 // But don't do this if we're worried about strict visibility
671 // compatibility.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000672 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) {
673 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
674
675 CGM.setTypeVisibility(GV, RD, CodeGenModule::TVK_ForRTTI);
676 CGM.setTypeVisibility(TypeName, RD, CodeGenModule::TVK_ForRTTIName);
Anders Carlsson907c8282011-01-29 22:10:32 +0000677 } else {
678 Visibility TypeInfoVisibility = DefaultVisibility;
679 if (CGM.getCodeGenOpts().HiddenWeakVTables &&
680 Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
681 TypeInfoVisibility = HiddenVisibility;
Anders Carlsson9a86a132011-01-29 20:36:11 +0000682
Anders Carlsson907c8282011-01-29 22:10:32 +0000683 // The type name should have the same visibility as the type itself.
684 Visibility ExplicitVisibility = Ty->getVisibility();
685 TypeName->setVisibility(CodeGenModule::
686 GetLLVMVisibility(ExplicitVisibility));
687
688 TypeInfoVisibility = minVisibility(TypeInfoVisibility, Ty->getVisibility());
689 GV->setVisibility(CodeGenModule::GetLLVMVisibility(TypeInfoVisibility));
Rafael Espindolab1c65ff2011-01-11 21:44:37 +0000690 }
Anders Carlsson907c8282011-01-29 22:10:32 +0000691
Rafael Espindola57244f62011-01-11 23:55:05 +0000692 GV->setUnnamedAddr(true);
693
Anders Carlsson8d145152009-12-20 22:30:54 +0000694 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
695}
696
Anders Carlsson08148092009-12-30 23:47:56 +0000697/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000698/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000699static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000700 unsigned Flags = 0;
701
702 if (Quals.hasConst())
703 Flags |= RTTIBuilder::PTI_Const;
704 if (Quals.hasVolatile())
705 Flags |= RTTIBuilder::PTI_Volatile;
706 if (Quals.hasRestrict())
707 Flags |= RTTIBuilder::PTI_Restrict;
708
709 return Flags;
710}
711
John McCalle8dc53e2010-08-12 02:17:33 +0000712/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
713/// for the given Objective-C object type.
714void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
715 // Drop qualifiers.
716 const Type *T = OT->getBaseType().getTypePtr();
717 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
718
719 // The builtin types are abi::__class_type_infos and don't require
720 // extra fields.
721 if (isa<BuiltinType>(T)) return;
722
723 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
724 ObjCInterfaceDecl *Super = Class->getSuperClass();
725
726 // Root classes are also __class_type_info.
727 if (!Super) return;
728
729 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
730
731 // Everything else is single inheritance.
732 llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
733 Fields.push_back(BaseTypeInfo);
734}
735
Anders Carlssonf64531a2009-12-30 01:00:12 +0000736/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
737/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
738void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
739 // Itanium C++ ABI 2.9.5p6b:
740 // It adds to abi::__class_type_info a single member pointing to the
741 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000742 llvm::Constant *BaseTypeInfo =
743 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
744 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000745}
746
Benjamin Kramer79ba2a62010-10-22 16:48:22 +0000747namespace {
748 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
749 /// a class hierarchy.
750 struct SeenBases {
751 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
752 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
753 };
754}
Anders Carlsson08148092009-12-30 23:47:56 +0000755
756/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
757/// abi::__vmi_class_type_info.
758///
759static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
760 SeenBases &Bases) {
761
762 unsigned Flags = 0;
763
764 const CXXRecordDecl *BaseDecl =
765 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
766
767 if (Base->isVirtual()) {
768 if (Bases.VirtualBases.count(BaseDecl)) {
769 // If this virtual base has been seen before, then the class is diamond
770 // shaped.
771 Flags |= RTTIBuilder::VMI_DiamondShaped;
772 } else {
773 if (Bases.NonVirtualBases.count(BaseDecl))
774 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
775
776 // Mark the virtual base as seen.
777 Bases.VirtualBases.insert(BaseDecl);
778 }
779 } else {
780 if (Bases.NonVirtualBases.count(BaseDecl)) {
781 // If this non-virtual base has been seen before, then the class has non-
782 // diamond shaped repeated inheritance.
783 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
784 } else {
785 if (Bases.VirtualBases.count(BaseDecl))
786 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
787
788 // Mark the non-virtual base as seen.
789 Bases.NonVirtualBases.insert(BaseDecl);
790 }
791 }
792
793 // Walk all bases.
794 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
795 E = BaseDecl->bases_end(); I != E; ++I)
796 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
797
798 return Flags;
799}
800
801static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
802 unsigned Flags = 0;
803 SeenBases Bases;
804
805 // Walk all bases.
806 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
807 E = RD->bases_end(); I != E; ++I)
808 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
809
810 return Flags;
811}
812
813/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
814/// classes with bases that do not satisfy the abi::__si_class_type_info
815/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
816void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
817 const llvm::Type *UnsignedIntLTy =
818 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
819
820 // Itanium C++ ABI 2.9.5p6c:
821 // __flags is a word with flags describing details about the class
822 // structure, which may be referenced by using the __flags_masks
823 // enumeration. These flags refer to both direct and indirect bases.
824 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000825 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000826
827 // Itanium C++ ABI 2.9.5p6c:
828 // __base_count is a word with the number of direct proper base class
829 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000830 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000831
832 if (!RD->getNumBases())
833 return;
834
835 const llvm::Type *LongLTy =
836 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
837
838 // Now add the base class descriptions.
839
840 // Itanium C++ ABI 2.9.5p6c:
841 // __base_info[] is an array of base class descriptions -- one for every
842 // direct proper base. Each description is of the type:
843 //
844 // struct abi::__base_class_type_info {
Eli Friedmana7e68452010-08-22 01:00:03 +0000845 // public:
Anders Carlsson08148092009-12-30 23:47:56 +0000846 // const __class_type_info *__base_type;
847 // long __offset_flags;
848 //
849 // enum __offset_flags_masks {
850 // __virtual_mask = 0x1,
851 // __public_mask = 0x2,
852 // __offset_shift = 8
853 // };
854 // };
855 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
856 E = RD->bases_end(); I != E; ++I) {
857 const CXXBaseSpecifier *Base = I;
858
859 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000860 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000861
862 const CXXRecordDecl *BaseDecl =
863 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
864
865 int64_t OffsetFlags = 0;
866
867 // All but the lower 8 bits of __offset_flags are a signed offset.
868 // For a non-virtual base, this is the offset in the object of the base
869 // subobject. For a virtual base, this is the offset in the virtual table of
870 // the virtual base offset for the virtual base referenced (negative).
871 if (Base->isVirtual())
Anders Carlssonaf440352010-03-23 04:11:45 +0000872 OffsetFlags = CGM.getVTables().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000873 else {
874 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Anders Carlssona14f5972010-10-31 23:22:37 +0000875 OffsetFlags = Layout.getBaseClassOffsetInBits(BaseDecl) / 8;
Anders Carlsson08148092009-12-30 23:47:56 +0000876 };
877
878 OffsetFlags <<= 8;
879
880 // The low-order byte of __offset_flags contains flags, as given by the
881 // masks from the enumeration __offset_flags_masks.
882 if (Base->isVirtual())
883 OffsetFlags |= BCTI_Virtual;
884 if (Base->getAccessSpecifier() == AS_public)
885 OffsetFlags |= BCTI_Public;
886
Anders Carlsson531d55f2009-12-31 17:43:53 +0000887 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000888 }
889}
890
Anders Carlsson8d145152009-12-20 22:30:54 +0000891/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
892/// used for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +0000893void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
Anders Carlssonabd6b092010-06-02 15:44:35 +0000894 Qualifiers Quals;
895 QualType UnqualifiedPointeeTy =
896 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
897
Anders Carlsson8d145152009-12-20 22:30:54 +0000898 // Itanium C++ ABI 2.9.5p7:
899 // __flags is a flag word describing the cv-qualification and other
900 // attributes of the type pointed to
Anders Carlssonabd6b092010-06-02 15:44:35 +0000901 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000902
903 // Itanium C++ ABI 2.9.5p7:
904 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
905 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000906 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000907 Flags |= PTI_Incomplete;
908
909 const llvm::Type *UnsignedIntLTy =
910 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000911 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000912
913 // Itanium C++ ABI 2.9.5p7:
914 // __pointee is a pointer to the std::type_info derivation for the
915 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000916 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000917 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000918 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000919}
920
921/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
922/// struct, used for member pointer types.
923void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
924 QualType PointeeTy = Ty->getPointeeType();
925
Anders Carlssonabd6b092010-06-02 15:44:35 +0000926 Qualifiers Quals;
927 QualType UnqualifiedPointeeTy =
928 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
929
Anders Carlsson8d145152009-12-20 22:30:54 +0000930 // Itanium C++ ABI 2.9.5p7:
931 // __flags is a flag word describing the cv-qualification and other
932 // attributes of the type pointed to.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000933 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000934
935 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000936
937 // Itanium C++ ABI 2.9.5p7:
938 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
939 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000940 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000941 Flags |= PTI_Incomplete;
942
Anders Carlsson8d145152009-12-20 22:30:54 +0000943 if (IsIncompleteClassType(ClassType))
944 Flags |= PTI_ContainingClassIncomplete;
945
Anders Carlsson8d145152009-12-20 22:30:54 +0000946 const llvm::Type *UnsignedIntLTy =
947 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000948 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000949
950 // Itanium C++ ABI 2.9.5p7:
951 // __pointee is a pointer to the std::type_info derivation for the
952 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000953 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000954 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000955 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000956
957 // Itanium C++ ABI 2.9.5p9:
958 // __context is a pointer to an abi::__class_type_info corresponding to the
959 // class type containing the member pointed to
960 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000961 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000962}
963
John McCall9dffe6f2010-04-30 01:15:21 +0000964llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
965 bool ForEH) {
966 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
967 // FIXME: should we even be calling this method if RTTI is disabled
968 // and it's not for EH?
969 if (!ForEH && !getContext().getLangOptions().RTTI) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000970 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
971 return llvm::Constant::getNullValue(Int8PtrTy);
972 }
John McCall9dffe6f2010-04-30 01:15:21 +0000973
Anders Carlsson531d55f2009-12-31 17:43:53 +0000974 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000975}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000976
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000977void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
978 QualType PointerType = Context.getPointerType(Type);
979 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
980 RTTIBuilder(*this).BuildTypeInfo(Type, true);
981 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
982 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
983}
984
985void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Anders Carlsson2bd62502010-11-04 05:28:09 +0000986 QualType FundamentalTypes[] = { Context.VoidTy, Context.NullPtrTy,
987 Context.BoolTy, Context.WCharTy,
988 Context.CharTy, Context.UnsignedCharTy,
989 Context.SignedCharTy, Context.ShortTy,
990 Context.UnsignedShortTy, Context.IntTy,
991 Context.UnsignedIntTy, Context.LongTy,
992 Context.UnsignedLongTy, Context.LongLongTy,
993 Context.UnsignedLongLongTy, Context.FloatTy,
994 Context.DoubleTy, Context.LongDoubleTy,
995 Context.Char16Ty, Context.Char32Ty };
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000996 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
997 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
998}