blob: 7ec0ee4b5cab63cb37c8b5ba73b3a319c7cc6797 [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;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000119 llvm::raw_svector_ostream Out(OutName);
120 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
121 Out.flush();
Anders Carlsson9a86a132011-01-29 20:36:11 +0000122 llvm::StringRef Name = OutName.str();
123
124 // We know that the mangled name of the type starts at index 4 of the
125 // mangled name of the typename, so we can just index into it in order to
126 // get the mangled name of the type.
127 llvm::Constant *Init = llvm::ConstantArray::get(VMContext, Name.substr(4));
128
129 llvm::GlobalVariable *GV =
130 CGM.CreateOrReplaceCXXRuntimeVariable(Name, Init->getType(), Linkage);
131
132 GV->setInitializer(Init);
133
134 return GV;
135}
136
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000137llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
138 // Mangle the RTTI name.
139 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000140 llvm::raw_svector_ostream Out(OutName);
141 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
142 Out.flush();
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000143 llvm::StringRef Name = OutName.str();
144
Anders Carlsson8d145152009-12-20 22:30:54 +0000145 // Look for an existing global.
146 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000147
148 if (!GV) {
149 // Create a new global variable.
150 GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
151 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000152 }
153
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000154 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000155}
156
Anders Carlsson8d145152009-12-20 22:30:54 +0000157/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
158/// info for that type is defined in the standard library.
159static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
160 // Itanium C++ ABI 2.9.2:
161 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
162 // the run-time support library. Specifically, the run-time support
163 // library should contain type_info objects for the types X, X* and
Anders Carlsson2bd62502010-11-04 05:28:09 +0000164 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
165 // unsigned char, signed char, short, unsigned short, int, unsigned int,
166 // long, unsigned long, long long, unsigned long long, float, double,
167 // long double, char16_t, char32_t, and the IEEE 754r decimal and
168 // half-precision floating point types.
Anders Carlsson8d145152009-12-20 22:30:54 +0000169 switch (Ty->getKind()) {
170 case BuiltinType::Void:
Anders Carlsson2bd62502010-11-04 05:28:09 +0000171 case BuiltinType::NullPtr:
Anders Carlsson8d145152009-12-20 22:30:54 +0000172 case BuiltinType::Bool:
Chris Lattner3f59c972010-12-25 23:25:43 +0000173 case BuiltinType::WChar_S:
174 case BuiltinType::WChar_U:
Anders Carlsson8d145152009-12-20 22:30:54 +0000175 case BuiltinType::Char_U:
176 case BuiltinType::Char_S:
177 case BuiltinType::UChar:
178 case BuiltinType::SChar:
179 case BuiltinType::Short:
180 case BuiltinType::UShort:
181 case BuiltinType::Int:
182 case BuiltinType::UInt:
183 case BuiltinType::Long:
184 case BuiltinType::ULong:
185 case BuiltinType::LongLong:
186 case BuiltinType::ULongLong:
187 case BuiltinType::Float:
188 case BuiltinType::Double:
189 case BuiltinType::LongDouble:
190 case BuiltinType::Char16:
191 case BuiltinType::Char32:
192 case BuiltinType::Int128:
193 case BuiltinType::UInt128:
194 return true;
195
196 case BuiltinType::Overload:
197 case BuiltinType::Dependent:
Anders Carlsson8d145152009-12-20 22:30:54 +0000198 assert(false && "Should not see this type here!");
199
Anders Carlsson8d145152009-12-20 22:30:54 +0000200 case BuiltinType::ObjCId:
201 case BuiltinType::ObjCClass:
202 case BuiltinType::ObjCSel:
203 assert(false && "FIXME: Objective-C types are unsupported!");
204 }
205
206 // Silent gcc.
207 return false;
208}
209
210static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
211 QualType PointeeTy = PointerTy->getPointeeType();
212 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
213 if (!BuiltinTy)
214 return false;
215
216 // Check the qualifiers.
217 Qualifiers Quals = PointeeTy.getQualifiers();
218 Quals.removeConst();
219
220 if (!Quals.empty())
221 return false;
222
223 return TypeInfoIsInStandardLibrary(BuiltinTy);
224}
225
John McCallcbfe5022010-08-04 08:34:44 +0000226/// IsStandardLibraryRTTIDescriptor - Returns whether the type
227/// information for the given type exists in the standard library.
228static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000229 // Type info for builtin types is defined in the standard library.
230 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
231 return TypeInfoIsInStandardLibrary(BuiltinTy);
232
233 // Type info for some pointer types to builtin types is defined in the
234 // standard library.
235 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
236 return TypeInfoIsInStandardLibrary(PointerTy);
237
John McCallcbfe5022010-08-04 08:34:44 +0000238 return false;
239}
240
241/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
242/// the given type exists somewhere else, and that we should not emit the type
243/// information in this translation unit. Assumes that it is not a
244/// standard-library type.
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000245static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM, QualType Ty) {
246 ASTContext &Context = CGM.getContext();
247
John McCall9dffe6f2010-04-30 01:15:21 +0000248 // If RTTI is disabled, don't consider key functions.
249 if (!Context.getLangOptions().RTTI) return false;
250
Anders Carlsson8d145152009-12-20 22:30:54 +0000251 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000252 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000253 if (!RD->hasDefinition())
254 return false;
255
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000256 if (!RD->isDynamicClass())
257 return false;
258
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000259 return !CGM.getVTables().ShouldEmitVTableInThisTU(RD);
Anders Carlsson8d145152009-12-20 22:30:54 +0000260 }
261
262 return false;
263}
264
265/// IsIncompleteClassType - Returns whether the given record type is incomplete.
266static bool IsIncompleteClassType(const RecordType *RecordTy) {
267 return !RecordTy->getDecl()->isDefinition();
268}
269
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000270/// ContainsIncompleteClassType - Returns whether the given type contains an
271/// incomplete class type. This is true if
272///
273/// * The given type is an incomplete class type.
274/// * The given type is a pointer type whose pointee type contains an
275/// incomplete class type.
276/// * The given type is a member pointer type whose class is an incomplete
277/// class type.
278/// * The given type is a member pointer type whoise pointee type contains an
279/// incomplete class type.
Anders Carlsson8d145152009-12-20 22:30:54 +0000280/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000281static bool ContainsIncompleteClassType(QualType Ty) {
282 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
283 if (IsIncompleteClassType(RecordTy))
284 return true;
285 }
286
287 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
288 return ContainsIncompleteClassType(PointerTy->getPointeeType());
289
290 if (const MemberPointerType *MemberPointerTy =
291 dyn_cast<MemberPointerType>(Ty)) {
292 // Check if the class type is incomplete.
293 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
294 if (IsIncompleteClassType(ClassType))
295 return true;
296
297 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000298 }
299
300 return false;
301}
302
303/// getTypeInfoLinkage - Return the linkage that the type info and type info
304/// name constants should have for the given type.
Anders Carlsson3a717f72011-01-24 02:04:33 +0000305static llvm::GlobalVariable::LinkageTypes
306getTypeInfoLinkage(CodeGenModule &CGM, QualType Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000307 // Itanium C++ ABI 2.9.5p7:
308 // In addition, it and all of the intermediate abi::__pointer_type_info
309 // structs in the chain down to the abi::__class_type_info for the
310 // incomplete class type must be prevented from resolving to the
311 // corresponding type_info structs for the complete class type, possibly
312 // by making them local static objects. Finally, a dummy class RTTI is
313 // generated for the incomplete type that will not resolve to the final
314 // complete class RTTI (because the latter need not exist), possibly by
315 // making it a local static object.
316 if (ContainsIncompleteClassType(Ty))
317 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000318
Douglas Gregor031b3712010-03-31 00:15:35 +0000319 switch (Ty->getLinkage()) {
320 case NoLinkage:
321 case InternalLinkage:
322 case UniqueExternalLinkage:
323 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson978ef682009-12-29 21:58:32 +0000324
Douglas Gregor031b3712010-03-31 00:15:35 +0000325 case ExternalLinkage:
Anders Carlssone34e3aa2011-01-24 02:12:11 +0000326 if (!CGM.getLangOptions().RTTI) {
327 // RTTI is not enabled, which means that this type info struct is going
328 // to be used for exception handling. Give it linkonce_odr linkage.
329 return llvm::GlobalValue::LinkOnceODRLinkage;
330 }
331
Douglas Gregor031b3712010-03-31 00:15:35 +0000332 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
333 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
334 if (RD->isDynamicClass())
Anders Carlsson3a717f72011-01-24 02:04:33 +0000335 return CGM.getVTableLinkage(RD);
Mike Stumpc8f76f52009-12-24 01:10:27 +0000336 }
Douglas Gregor031b3712010-03-31 00:15:35 +0000337
Anders Carlssonf502d932011-01-24 00:46:19 +0000338 return llvm::GlobalValue::LinkOnceODRLinkage;
Mike Stumpc8f76f52009-12-24 01:10:27 +0000339 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000340
Anders Carlssonf502d932011-01-24 00:46:19 +0000341 return llvm::GlobalValue::LinkOnceODRLinkage;
Anders Carlsson8d145152009-12-20 22:30:54 +0000342}
343
Anders Carlssonf64531a2009-12-30 01:00:12 +0000344// CanUseSingleInheritance - Return whether the given record decl has a "single,
345// public, non-virtual base at offset zero (i.e. the derived class is dynamic
346// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
347static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
348 // Check the number of bases.
349 if (RD->getNumBases() != 1)
350 return false;
351
352 // Get the base.
353 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
354
355 // Check that the base is not virtual.
356 if (Base->isVirtual())
357 return false;
358
359 // Check that the base is public.
360 if (Base->getAccessSpecifier() != AS_public)
361 return false;
362
363 // Check that the class is dynamic iff the base is.
364 const CXXRecordDecl *BaseDecl =
365 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
366 if (!BaseDecl->isEmpty() &&
367 BaseDecl->isDynamicClass() != RD->isDynamicClass())
368 return false;
369
370 return true;
371}
372
Anders Carlsson046c2942010-04-17 20:15:18 +0000373void RTTIBuilder::BuildVTablePointer(const Type *Ty) {
John McCalle8dc53e2010-08-12 02:17:33 +0000374 // abi::__class_type_info.
375 static const char * const ClassTypeInfo =
376 "_ZTVN10__cxxabiv117__class_type_infoE";
377 // abi::__si_class_type_info.
378 static const char * const SIClassTypeInfo =
379 "_ZTVN10__cxxabiv120__si_class_type_infoE";
380 // abi::__vmi_class_type_info.
381 static const char * const VMIClassTypeInfo =
382 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
383
Eli Friedman1cf26f52010-08-11 20:41:51 +0000384 const char *VTableName = 0;
Anders Carlsson8d145152009-12-20 22:30:54 +0000385
386 switch (Ty->getTypeClass()) {
Eli Friedman1cf26f52010-08-11 20:41:51 +0000387#define TYPE(Class, Base)
388#define ABSTRACT_TYPE(Class, Base)
389#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
390#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
391#define DEPENDENT_TYPE(Class, Base) case Type::Class:
392#include "clang/AST/TypeNodes.def"
393 assert(false && "Non-canonical and dependent types shouldn't get here");
394
395 case Type::LValueReference:
396 case Type::RValueReference:
397 assert(false && "References shouldn't get here");
Anders Carlsson978ef682009-12-29 21:58:32 +0000398
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000399 case Type::Builtin:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000400 // GCC treats vector and complex types as fundamental types.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000401 case Type::Vector:
402 case Type::ExtVector:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000403 case Type::Complex:
404 // FIXME: GCC treats block pointers as fundamental types?!
405 case Type::BlockPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000406 // abi::__fundamental_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000407 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000408 break;
409
Anders Carlsson978ef682009-12-29 21:58:32 +0000410 case Type::ConstantArray:
411 case Type::IncompleteArray:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000412 case Type::VariableArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000413 // abi::__array_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000414 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000415 break;
416
417 case Type::FunctionNoProto:
418 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000419 // abi::__function_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000420 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000421 break;
422
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000423 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000424 // abi::__enum_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000425 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000426 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000427
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000428 case Type::Record: {
429 const CXXRecordDecl *RD =
430 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000431
John McCall86ff3082010-02-04 22:26:26 +0000432 if (!RD->hasDefinition() || !RD->getNumBases()) {
John McCalle8dc53e2010-08-12 02:17:33 +0000433 VTableName = ClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000434 } else if (CanUseSingleInheritance(RD)) {
John McCalle8dc53e2010-08-12 02:17:33 +0000435 VTableName = SIClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000436 } else {
John McCalle8dc53e2010-08-12 02:17:33 +0000437 VTableName = VMIClassTypeInfo;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000438 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000439
440 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000441 }
442
Eli Friedman1cf26f52010-08-11 20:41:51 +0000443 case Type::ObjCObject:
John McCalle8dc53e2010-08-12 02:17:33 +0000444 // Ignore protocol qualifiers.
445 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
446
447 // Handle id and Class.
448 if (isa<BuiltinType>(Ty)) {
449 VTableName = ClassTypeInfo;
450 break;
451 }
452
453 assert(isa<ObjCInterfaceType>(Ty));
454 // Fall through.
455
Eli Friedman1cf26f52010-08-11 20:41:51 +0000456 case Type::ObjCInterface:
John McCalle8dc53e2010-08-12 02:17:33 +0000457 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
458 VTableName = SIClassTypeInfo;
459 } else {
460 VTableName = ClassTypeInfo;
461 }
Eli Friedman1cf26f52010-08-11 20:41:51 +0000462 break;
463
John McCalle8dc53e2010-08-12 02:17:33 +0000464 case Type::ObjCObjectPointer:
Anders Carlsson8d145152009-12-20 22:30:54 +0000465 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000466 // abi::__pointer_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000467 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000468 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000469
Anders Carlsson8d145152009-12-20 22:30:54 +0000470 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000471 // abi::__pointer_to_member_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000472 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000473 break;
474 }
475
Anders Carlsson046c2942010-04-17 20:15:18 +0000476 llvm::Constant *VTable =
477 CGM.getModule().getOrInsertGlobal(VTableName, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000478
479 const llvm::Type *PtrDiffTy =
480 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
481
482 // The vtable address point is 2.
483 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Anders Carlsson046c2942010-04-17 20:15:18 +0000484 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, &Two, 1);
485 VTable = llvm::ConstantExpr::getBitCast(VTable, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000486
Anders Carlsson046c2942010-04-17 20:15:18 +0000487 Fields.push_back(VTable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000488}
489
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000490// maybeUpdateRTTILinkage - Will update the linkage of the RTTI data structures
491// from available_externally to the correct linkage if necessary. An example of
492// this is:
493//
494// struct A {
495// virtual void f();
496// };
497//
498// const std::type_info &g() {
499// return typeid(A);
500// }
501//
502// void A::f() { }
503//
504// When we're generating the typeid(A) expression, we do not yet know that
505// A's key function is defined in this translation unit, so we will give the
506// typeinfo and typename structures available_externally linkage. When A::f
507// forces the vtable to be generated, we need to change the linkage of the
508// typeinfo and typename structs, otherwise we'll end up with undefined
509// externals when linking.
510static void
511maybeUpdateRTTILinkage(CodeGenModule &CGM, llvm::GlobalVariable *GV,
512 QualType Ty) {
513 // We're only interested in globals with available_externally linkage.
514 if (!GV->hasAvailableExternallyLinkage())
515 return;
516
517 // Get the real linkage for the type.
518 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(CGM, Ty);
519
520 // If variable is supposed to have available_externally linkage, we don't
521 // need to do anything.
522 if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
523 return;
524
525 // Update the typeinfo linkage.
526 GV->setLinkage(Linkage);
527
528 // Get the typename global.
529 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000530 llvm::raw_svector_ostream Out(OutName);
531 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
532 Out.flush();
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000533 llvm::StringRef Name = OutName.str();
534
535 llvm::GlobalVariable *TypeNameGV = CGM.getModule().getNamedGlobal(Name);
536
537 assert(TypeNameGV->hasAvailableExternallyLinkage() &&
538 "Type name has different linkage from type info!");
539
540 // And update its linkage.
541 TypeNameGV->setLinkage(Linkage);
542}
543
John McCallcbfe5022010-08-04 08:34:44 +0000544llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000545 // We want to operate on the canonical type.
546 Ty = CGM.getContext().getCanonicalType(Ty);
547
548 // Check if we've already emitted an RTTI descriptor for this type.
549 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000550 llvm::raw_svector_ostream Out(OutName);
551 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
552 Out.flush();
Anders Carlsson8d145152009-12-20 22:30:54 +0000553 llvm::StringRef Name = OutName.str();
Anders Carlsson1cbce122011-01-29 19:16:51 +0000554
Anders Carlsson8d145152009-12-20 22:30:54 +0000555 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000556 if (OldGV && !OldGV->isDeclaration()) {
557 maybeUpdateRTTILinkage(CGM, OldGV, Ty);
558
Anders Carlsson8d145152009-12-20 22:30:54 +0000559 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000560 }
John McCallcbfe5022010-08-04 08:34:44 +0000561
Anders Carlsson8d145152009-12-20 22:30:54 +0000562 // Check if there is already an external RTTI descriptor for this type.
John McCallcbfe5022010-08-04 08:34:44 +0000563 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000564 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
Anders Carlsson8d145152009-12-20 22:30:54 +0000565 return GetAddrOfExternalRTTIDescriptor(Ty);
566
John McCallcbfe5022010-08-04 08:34:44 +0000567 // Emit the standard library with external linkage.
568 llvm::GlobalVariable::LinkageTypes Linkage;
569 if (IsStdLib)
570 Linkage = llvm::GlobalValue::ExternalLinkage;
571 else
Anders Carlsson3a717f72011-01-24 02:04:33 +0000572 Linkage = getTypeInfoLinkage(CGM, Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +0000573
574 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000575 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000576
577 // And the name.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000578 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
579
580 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson907c8282011-01-29 22:10:32 +0000581 Fields.push_back(llvm::ConstantExpr::getBitCast(TypeName, Int8PtrTy));
John McCallcbfe5022010-08-04 08:34:44 +0000582
Anders Carlsson8d145152009-12-20 22:30:54 +0000583 switch (Ty->getTypeClass()) {
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000584#define TYPE(Class, Base)
585#define ABSTRACT_TYPE(Class, Base)
586#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
587#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
588#define DEPENDENT_TYPE(Class, Base) case Type::Class:
589#include "clang/AST/TypeNodes.def"
590 assert(false && "Non-canonical and dependent types shouldn't get here");
Anders Carlsson8d145152009-12-20 22:30:54 +0000591
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000592 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000593 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000594 case Type::Vector:
595 case Type::ExtVector:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000596 case Type::Complex:
597 case Type::BlockPointer:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000598 // Itanium C++ ABI 2.9.5p4:
599 // abi::__fundamental_type_info adds no data members to std::type_info.
600 break;
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000601
602 case Type::LValueReference:
603 case Type::RValueReference:
604 assert(false && "References shouldn't get here");
605
Anders Carlsson978ef682009-12-29 21:58:32 +0000606 case Type::ConstantArray:
607 case Type::IncompleteArray:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000608 case Type::VariableArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000609 // Itanium C++ ABI 2.9.5p5:
610 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000611 break;
612
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000613 case Type::FunctionNoProto:
614 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000615 // Itanium C++ ABI 2.9.5p5:
616 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000617 break;
618
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000619 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000620 // Itanium C++ ABI 2.9.5p5:
621 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000622 break;
623
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000624 case Type::Record: {
625 const CXXRecordDecl *RD =
626 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000627 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000628 // We don't need to emit any fields.
629 break;
630 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000631
Anders Carlsson08148092009-12-30 23:47:56 +0000632 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000633 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000634 else
635 BuildVMIClassTypeInfo(RD);
636
637 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000638 }
John McCalle8dc53e2010-08-12 02:17:33 +0000639
640 case Type::ObjCObject:
641 case Type::ObjCInterface:
642 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
643 break;
644
645 case Type::ObjCObjectPointer:
646 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
647 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000648
Anders Carlsson8d145152009-12-20 22:30:54 +0000649 case Type::Pointer:
John McCalle8dc53e2010-08-12 02:17:33 +0000650 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000651 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000652
Anders Carlsson8d145152009-12-20 22:30:54 +0000653 case Type::MemberPointer:
654 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
655 break;
656 }
657
658 llvm::Constant *Init =
Anders Carlsson531d55f2009-12-31 17:43:53 +0000659 llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
Anders Carlsson8d145152009-12-20 22:30:54 +0000660 /*Packed=*/false);
661
662 llvm::GlobalVariable *GV =
663 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
664 /*Constant=*/true, Linkage, Init, Name);
665
666 // If there's already an old global variable, replace it with the new one.
667 if (OldGV) {
668 GV->takeName(OldGV);
669 llvm::Constant *NewPtr =
670 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
671 OldGV->replaceAllUsesWith(NewPtr);
672 OldGV->eraseFromParent();
673 }
John McCallcbfe5022010-08-04 08:34:44 +0000674
675 // GCC only relies on the uniqueness of the type names, not the
676 // type_infos themselves, so we can emit these as hidden symbols.
John McCall279b5eb2010-08-12 23:36:15 +0000677 // But don't do this if we're worried about strict visibility
678 // compatibility.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000679 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) {
680 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
681
682 CGM.setTypeVisibility(GV, RD, CodeGenModule::TVK_ForRTTI);
683 CGM.setTypeVisibility(TypeName, RD, CodeGenModule::TVK_ForRTTIName);
Anders Carlsson907c8282011-01-29 22:10:32 +0000684 } else {
685 Visibility TypeInfoVisibility = DefaultVisibility;
686 if (CGM.getCodeGenOpts().HiddenWeakVTables &&
687 Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
688 TypeInfoVisibility = HiddenVisibility;
Anders Carlsson9a86a132011-01-29 20:36:11 +0000689
Anders Carlsson907c8282011-01-29 22:10:32 +0000690 // The type name should have the same visibility as the type itself.
691 Visibility ExplicitVisibility = Ty->getVisibility();
692 TypeName->setVisibility(CodeGenModule::
693 GetLLVMVisibility(ExplicitVisibility));
694
695 TypeInfoVisibility = minVisibility(TypeInfoVisibility, Ty->getVisibility());
696 GV->setVisibility(CodeGenModule::GetLLVMVisibility(TypeInfoVisibility));
Rafael Espindolab1c65ff2011-01-11 21:44:37 +0000697 }
Anders Carlsson907c8282011-01-29 22:10:32 +0000698
Rafael Espindola57244f62011-01-11 23:55:05 +0000699 GV->setUnnamedAddr(true);
700
Anders Carlsson8d145152009-12-20 22:30:54 +0000701 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
702}
703
Anders Carlsson08148092009-12-30 23:47:56 +0000704/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000705/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000706static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000707 unsigned Flags = 0;
708
709 if (Quals.hasConst())
710 Flags |= RTTIBuilder::PTI_Const;
711 if (Quals.hasVolatile())
712 Flags |= RTTIBuilder::PTI_Volatile;
713 if (Quals.hasRestrict())
714 Flags |= RTTIBuilder::PTI_Restrict;
715
716 return Flags;
717}
718
John McCalle8dc53e2010-08-12 02:17:33 +0000719/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
720/// for the given Objective-C object type.
721void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
722 // Drop qualifiers.
723 const Type *T = OT->getBaseType().getTypePtr();
724 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
725
726 // The builtin types are abi::__class_type_infos and don't require
727 // extra fields.
728 if (isa<BuiltinType>(T)) return;
729
730 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
731 ObjCInterfaceDecl *Super = Class->getSuperClass();
732
733 // Root classes are also __class_type_info.
734 if (!Super) return;
735
736 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
737
738 // Everything else is single inheritance.
739 llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
740 Fields.push_back(BaseTypeInfo);
741}
742
Anders Carlssonf64531a2009-12-30 01:00:12 +0000743/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
744/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
745void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
746 // Itanium C++ ABI 2.9.5p6b:
747 // It adds to abi::__class_type_info a single member pointing to the
748 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000749 llvm::Constant *BaseTypeInfo =
750 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
751 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000752}
753
Benjamin Kramer79ba2a62010-10-22 16:48:22 +0000754namespace {
755 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
756 /// a class hierarchy.
757 struct SeenBases {
758 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
759 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
760 };
761}
Anders Carlsson08148092009-12-30 23:47:56 +0000762
763/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
764/// abi::__vmi_class_type_info.
765///
766static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
767 SeenBases &Bases) {
768
769 unsigned Flags = 0;
770
771 const CXXRecordDecl *BaseDecl =
772 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
773
774 if (Base->isVirtual()) {
775 if (Bases.VirtualBases.count(BaseDecl)) {
776 // If this virtual base has been seen before, then the class is diamond
777 // shaped.
778 Flags |= RTTIBuilder::VMI_DiamondShaped;
779 } else {
780 if (Bases.NonVirtualBases.count(BaseDecl))
781 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
782
783 // Mark the virtual base as seen.
784 Bases.VirtualBases.insert(BaseDecl);
785 }
786 } else {
787 if (Bases.NonVirtualBases.count(BaseDecl)) {
788 // If this non-virtual base has been seen before, then the class has non-
789 // diamond shaped repeated inheritance.
790 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
791 } else {
792 if (Bases.VirtualBases.count(BaseDecl))
793 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
794
795 // Mark the non-virtual base as seen.
796 Bases.NonVirtualBases.insert(BaseDecl);
797 }
798 }
799
800 // Walk all bases.
801 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
802 E = BaseDecl->bases_end(); I != E; ++I)
803 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
804
805 return Flags;
806}
807
808static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
809 unsigned Flags = 0;
810 SeenBases Bases;
811
812 // Walk all bases.
813 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
814 E = RD->bases_end(); I != E; ++I)
815 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
816
817 return Flags;
818}
819
820/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
821/// classes with bases that do not satisfy the abi::__si_class_type_info
822/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
823void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
824 const llvm::Type *UnsignedIntLTy =
825 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
826
827 // Itanium C++ ABI 2.9.5p6c:
828 // __flags is a word with flags describing details about the class
829 // structure, which may be referenced by using the __flags_masks
830 // enumeration. These flags refer to both direct and indirect bases.
831 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000832 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000833
834 // Itanium C++ ABI 2.9.5p6c:
835 // __base_count is a word with the number of direct proper base class
836 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000837 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000838
839 if (!RD->getNumBases())
840 return;
841
842 const llvm::Type *LongLTy =
843 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
844
845 // Now add the base class descriptions.
846
847 // Itanium C++ ABI 2.9.5p6c:
848 // __base_info[] is an array of base class descriptions -- one for every
849 // direct proper base. Each description is of the type:
850 //
851 // struct abi::__base_class_type_info {
Eli Friedmana7e68452010-08-22 01:00:03 +0000852 // public:
Anders Carlsson08148092009-12-30 23:47:56 +0000853 // const __class_type_info *__base_type;
854 // long __offset_flags;
855 //
856 // enum __offset_flags_masks {
857 // __virtual_mask = 0x1,
858 // __public_mask = 0x2,
859 // __offset_shift = 8
860 // };
861 // };
862 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
863 E = RD->bases_end(); I != E; ++I) {
864 const CXXBaseSpecifier *Base = I;
865
866 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000867 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000868
869 const CXXRecordDecl *BaseDecl =
870 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
871
872 int64_t OffsetFlags = 0;
873
874 // All but the lower 8 bits of __offset_flags are a signed offset.
875 // For a non-virtual base, this is the offset in the object of the base
876 // subobject. For a virtual base, this is the offset in the virtual table of
877 // the virtual base offset for the virtual base referenced (negative).
878 if (Base->isVirtual())
Anders Carlssonaf440352010-03-23 04:11:45 +0000879 OffsetFlags = CGM.getVTables().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000880 else {
881 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Anders Carlssona14f5972010-10-31 23:22:37 +0000882 OffsetFlags = Layout.getBaseClassOffsetInBits(BaseDecl) / 8;
Anders Carlsson08148092009-12-30 23:47:56 +0000883 };
884
885 OffsetFlags <<= 8;
886
887 // The low-order byte of __offset_flags contains flags, as given by the
888 // masks from the enumeration __offset_flags_masks.
889 if (Base->isVirtual())
890 OffsetFlags |= BCTI_Virtual;
891 if (Base->getAccessSpecifier() == AS_public)
892 OffsetFlags |= BCTI_Public;
893
Anders Carlsson531d55f2009-12-31 17:43:53 +0000894 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000895 }
896}
897
Anders Carlsson8d145152009-12-20 22:30:54 +0000898/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
899/// used for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +0000900void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
Anders Carlssonabd6b092010-06-02 15:44:35 +0000901 Qualifiers Quals;
902 QualType UnqualifiedPointeeTy =
903 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
904
Anders Carlsson8d145152009-12-20 22:30:54 +0000905 // Itanium C++ ABI 2.9.5p7:
906 // __flags is a flag word describing the cv-qualification and other
907 // attributes of the type pointed to
Anders Carlssonabd6b092010-06-02 15:44:35 +0000908 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000909
910 // Itanium C++ ABI 2.9.5p7:
911 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
912 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000913 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000914 Flags |= PTI_Incomplete;
915
916 const llvm::Type *UnsignedIntLTy =
917 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000918 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000919
920 // Itanium C++ ABI 2.9.5p7:
921 // __pointee is a pointer to the std::type_info derivation for the
922 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000923 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000924 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000925 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000926}
927
928/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
929/// struct, used for member pointer types.
930void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
931 QualType PointeeTy = Ty->getPointeeType();
932
Anders Carlssonabd6b092010-06-02 15:44:35 +0000933 Qualifiers Quals;
934 QualType UnqualifiedPointeeTy =
935 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
936
Anders Carlsson8d145152009-12-20 22:30:54 +0000937 // Itanium C++ ABI 2.9.5p7:
938 // __flags is a flag word describing the cv-qualification and other
939 // attributes of the type pointed to.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000940 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000941
942 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000943
944 // Itanium C++ ABI 2.9.5p7:
945 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
946 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000947 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000948 Flags |= PTI_Incomplete;
949
Anders Carlsson8d145152009-12-20 22:30:54 +0000950 if (IsIncompleteClassType(ClassType))
951 Flags |= PTI_ContainingClassIncomplete;
952
Anders Carlsson8d145152009-12-20 22:30:54 +0000953 const llvm::Type *UnsignedIntLTy =
954 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000955 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000956
957 // Itanium C++ ABI 2.9.5p7:
958 // __pointee is a pointer to the std::type_info derivation for the
959 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000960 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000961 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000962 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000963
964 // Itanium C++ ABI 2.9.5p9:
965 // __context is a pointer to an abi::__class_type_info corresponding to the
966 // class type containing the member pointed to
967 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000968 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000969}
970
John McCall9dffe6f2010-04-30 01:15:21 +0000971llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
972 bool ForEH) {
973 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
974 // FIXME: should we even be calling this method if RTTI is disabled
975 // and it's not for EH?
976 if (!ForEH && !getContext().getLangOptions().RTTI) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000977 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
978 return llvm::Constant::getNullValue(Int8PtrTy);
979 }
John McCall9dffe6f2010-04-30 01:15:21 +0000980
Anders Carlsson531d55f2009-12-31 17:43:53 +0000981 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000982}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000983
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000984void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
985 QualType PointerType = Context.getPointerType(Type);
986 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
987 RTTIBuilder(*this).BuildTypeInfo(Type, true);
988 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
989 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
990}
991
992void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Anders Carlsson2bd62502010-11-04 05:28:09 +0000993 QualType FundamentalTypes[] = { Context.VoidTy, Context.NullPtrTy,
994 Context.BoolTy, Context.WCharTy,
995 Context.CharTy, Context.UnsignedCharTy,
996 Context.SignedCharTy, Context.ShortTy,
997 Context.UnsignedShortTy, Context.IntTy,
998 Context.UnsignedIntTy, Context.LongTy,
999 Context.UnsignedLongTy, Context.LongLongTy,
1000 Context.UnsignedLongLongTy, Context.FloatTy,
1001 Context.DoubleTy, Context.LongDoubleTy,
1002 Context.Char16Ty, Context.Char32Ty };
Rafael Espindolad1a5c312010-03-27 02:52:14 +00001003 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
1004 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
1005}