blob: de403b65596c8abef5db70d4e248fe404d0287ad [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:
198 case BuiltinType::UndeducedAuto:
199 assert(false && "Should not see this type here!");
200
Anders Carlsson8d145152009-12-20 22:30:54 +0000201 case BuiltinType::ObjCId:
202 case BuiltinType::ObjCClass:
203 case BuiltinType::ObjCSel:
204 assert(false && "FIXME: Objective-C types are unsupported!");
205 }
206
207 // Silent gcc.
208 return false;
209}
210
211static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
212 QualType PointeeTy = PointerTy->getPointeeType();
213 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
214 if (!BuiltinTy)
215 return false;
216
217 // Check the qualifiers.
218 Qualifiers Quals = PointeeTy.getQualifiers();
219 Quals.removeConst();
220
221 if (!Quals.empty())
222 return false;
223
224 return TypeInfoIsInStandardLibrary(BuiltinTy);
225}
226
John McCallcbfe5022010-08-04 08:34:44 +0000227/// IsStandardLibraryRTTIDescriptor - Returns whether the type
228/// information for the given type exists in the standard library.
229static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000230 // Type info for builtin types is defined in the standard library.
231 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
232 return TypeInfoIsInStandardLibrary(BuiltinTy);
233
234 // Type info for some pointer types to builtin types is defined in the
235 // standard library.
236 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
237 return TypeInfoIsInStandardLibrary(PointerTy);
238
John McCallcbfe5022010-08-04 08:34:44 +0000239 return false;
240}
241
242/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
243/// the given type exists somewhere else, and that we should not emit the type
244/// information in this translation unit. Assumes that it is not a
245/// standard-library type.
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000246static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM, QualType Ty) {
247 ASTContext &Context = CGM.getContext();
248
John McCall9dffe6f2010-04-30 01:15:21 +0000249 // If RTTI is disabled, don't consider key functions.
250 if (!Context.getLangOptions().RTTI) return false;
251
Anders Carlsson8d145152009-12-20 22:30:54 +0000252 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000253 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000254 if (!RD->hasDefinition())
255 return false;
256
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000257 if (!RD->isDynamicClass())
258 return false;
259
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000260 return !CGM.getVTables().ShouldEmitVTableInThisTU(RD);
Anders Carlsson8d145152009-12-20 22:30:54 +0000261 }
262
263 return false;
264}
265
266/// IsIncompleteClassType - Returns whether the given record type is incomplete.
267static bool IsIncompleteClassType(const RecordType *RecordTy) {
268 return !RecordTy->getDecl()->isDefinition();
269}
270
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000271/// ContainsIncompleteClassType - Returns whether the given type contains an
272/// incomplete class type. This is true if
273///
274/// * The given type is an incomplete class type.
275/// * The given type is a pointer type whose pointee type contains an
276/// incomplete class type.
277/// * The given type is a member pointer type whose class is an incomplete
278/// class type.
279/// * The given type is a member pointer type whoise pointee type contains an
280/// incomplete class type.
Anders Carlsson8d145152009-12-20 22:30:54 +0000281/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000282static bool ContainsIncompleteClassType(QualType Ty) {
283 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
284 if (IsIncompleteClassType(RecordTy))
285 return true;
286 }
287
288 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
289 return ContainsIncompleteClassType(PointerTy->getPointeeType());
290
291 if (const MemberPointerType *MemberPointerTy =
292 dyn_cast<MemberPointerType>(Ty)) {
293 // Check if the class type is incomplete.
294 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
295 if (IsIncompleteClassType(ClassType))
296 return true;
297
298 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000299 }
300
301 return false;
302}
303
304/// getTypeInfoLinkage - Return the linkage that the type info and type info
305/// name constants should have for the given type.
Anders Carlsson3a717f72011-01-24 02:04:33 +0000306static llvm::GlobalVariable::LinkageTypes
307getTypeInfoLinkage(CodeGenModule &CGM, QualType Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000308 // Itanium C++ ABI 2.9.5p7:
309 // In addition, it and all of the intermediate abi::__pointer_type_info
310 // structs in the chain down to the abi::__class_type_info for the
311 // incomplete class type must be prevented from resolving to the
312 // corresponding type_info structs for the complete class type, possibly
313 // by making them local static objects. Finally, a dummy class RTTI is
314 // generated for the incomplete type that will not resolve to the final
315 // complete class RTTI (because the latter need not exist), possibly by
316 // making it a local static object.
317 if (ContainsIncompleteClassType(Ty))
318 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000319
Douglas Gregor031b3712010-03-31 00:15:35 +0000320 switch (Ty->getLinkage()) {
321 case NoLinkage:
322 case InternalLinkage:
323 case UniqueExternalLinkage:
324 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson978ef682009-12-29 21:58:32 +0000325
Douglas Gregor031b3712010-03-31 00:15:35 +0000326 case ExternalLinkage:
Anders Carlssone34e3aa2011-01-24 02:12:11 +0000327 if (!CGM.getLangOptions().RTTI) {
328 // RTTI is not enabled, which means that this type info struct is going
329 // to be used for exception handling. Give it linkonce_odr linkage.
330 return llvm::GlobalValue::LinkOnceODRLinkage;
331 }
332
Douglas Gregor031b3712010-03-31 00:15:35 +0000333 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
334 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
335 if (RD->isDynamicClass())
Anders Carlsson3a717f72011-01-24 02:04:33 +0000336 return CGM.getVTableLinkage(RD);
Mike Stumpc8f76f52009-12-24 01:10:27 +0000337 }
Douglas Gregor031b3712010-03-31 00:15:35 +0000338
Anders Carlssonf502d932011-01-24 00:46:19 +0000339 return llvm::GlobalValue::LinkOnceODRLinkage;
Mike Stumpc8f76f52009-12-24 01:10:27 +0000340 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000341
Anders Carlssonf502d932011-01-24 00:46:19 +0000342 return llvm::GlobalValue::LinkOnceODRLinkage;
Anders Carlsson8d145152009-12-20 22:30:54 +0000343}
344
Anders Carlssonf64531a2009-12-30 01:00:12 +0000345// CanUseSingleInheritance - Return whether the given record decl has a "single,
346// public, non-virtual base at offset zero (i.e. the derived class is dynamic
347// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
348static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
349 // Check the number of bases.
350 if (RD->getNumBases() != 1)
351 return false;
352
353 // Get the base.
354 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
355
356 // Check that the base is not virtual.
357 if (Base->isVirtual())
358 return false;
359
360 // Check that the base is public.
361 if (Base->getAccessSpecifier() != AS_public)
362 return false;
363
364 // Check that the class is dynamic iff the base is.
365 const CXXRecordDecl *BaseDecl =
366 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
367 if (!BaseDecl->isEmpty() &&
368 BaseDecl->isDynamicClass() != RD->isDynamicClass())
369 return false;
370
371 return true;
372}
373
Anders Carlsson046c2942010-04-17 20:15:18 +0000374void RTTIBuilder::BuildVTablePointer(const Type *Ty) {
John McCalle8dc53e2010-08-12 02:17:33 +0000375 // abi::__class_type_info.
376 static const char * const ClassTypeInfo =
377 "_ZTVN10__cxxabiv117__class_type_infoE";
378 // abi::__si_class_type_info.
379 static const char * const SIClassTypeInfo =
380 "_ZTVN10__cxxabiv120__si_class_type_infoE";
381 // abi::__vmi_class_type_info.
382 static const char * const VMIClassTypeInfo =
383 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
384
Eli Friedman1cf26f52010-08-11 20:41:51 +0000385 const char *VTableName = 0;
Anders Carlsson8d145152009-12-20 22:30:54 +0000386
387 switch (Ty->getTypeClass()) {
Eli Friedman1cf26f52010-08-11 20:41:51 +0000388#define TYPE(Class, Base)
389#define ABSTRACT_TYPE(Class, Base)
390#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
391#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
392#define DEPENDENT_TYPE(Class, Base) case Type::Class:
393#include "clang/AST/TypeNodes.def"
394 assert(false && "Non-canonical and dependent types shouldn't get here");
395
396 case Type::LValueReference:
397 case Type::RValueReference:
398 assert(false && "References shouldn't get here");
Anders Carlsson978ef682009-12-29 21:58:32 +0000399
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000400 case Type::Builtin:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000401 // GCC treats vector and complex types as fundamental types.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000402 case Type::Vector:
403 case Type::ExtVector:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000404 case Type::Complex:
405 // FIXME: GCC treats block pointers as fundamental types?!
406 case Type::BlockPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000407 // abi::__fundamental_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000408 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000409 break;
410
Anders Carlsson978ef682009-12-29 21:58:32 +0000411 case Type::ConstantArray:
412 case Type::IncompleteArray:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000413 case Type::VariableArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000414 // abi::__array_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000415 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000416 break;
417
418 case Type::FunctionNoProto:
419 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000420 // abi::__function_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000421 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000422 break;
423
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000424 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000425 // abi::__enum_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000426 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000427 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000428
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000429 case Type::Record: {
430 const CXXRecordDecl *RD =
431 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000432
John McCall86ff3082010-02-04 22:26:26 +0000433 if (!RD->hasDefinition() || !RD->getNumBases()) {
John McCalle8dc53e2010-08-12 02:17:33 +0000434 VTableName = ClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000435 } else if (CanUseSingleInheritance(RD)) {
John McCalle8dc53e2010-08-12 02:17:33 +0000436 VTableName = SIClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000437 } else {
John McCalle8dc53e2010-08-12 02:17:33 +0000438 VTableName = VMIClassTypeInfo;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000439 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000440
441 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000442 }
443
Eli Friedman1cf26f52010-08-11 20:41:51 +0000444 case Type::ObjCObject:
John McCalle8dc53e2010-08-12 02:17:33 +0000445 // Ignore protocol qualifiers.
446 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
447
448 // Handle id and Class.
449 if (isa<BuiltinType>(Ty)) {
450 VTableName = ClassTypeInfo;
451 break;
452 }
453
454 assert(isa<ObjCInterfaceType>(Ty));
455 // Fall through.
456
Eli Friedman1cf26f52010-08-11 20:41:51 +0000457 case Type::ObjCInterface:
John McCalle8dc53e2010-08-12 02:17:33 +0000458 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
459 VTableName = SIClassTypeInfo;
460 } else {
461 VTableName = ClassTypeInfo;
462 }
Eli Friedman1cf26f52010-08-11 20:41:51 +0000463 break;
464
John McCalle8dc53e2010-08-12 02:17:33 +0000465 case Type::ObjCObjectPointer:
Anders Carlsson8d145152009-12-20 22:30:54 +0000466 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000467 // abi::__pointer_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000468 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000469 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000470
Anders Carlsson8d145152009-12-20 22:30:54 +0000471 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000472 // abi::__pointer_to_member_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000473 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000474 break;
475 }
476
Anders Carlsson046c2942010-04-17 20:15:18 +0000477 llvm::Constant *VTable =
478 CGM.getModule().getOrInsertGlobal(VTableName, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000479
480 const llvm::Type *PtrDiffTy =
481 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
482
483 // The vtable address point is 2.
484 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Anders Carlsson046c2942010-04-17 20:15:18 +0000485 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, &Two, 1);
486 VTable = llvm::ConstantExpr::getBitCast(VTable, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000487
Anders Carlsson046c2942010-04-17 20:15:18 +0000488 Fields.push_back(VTable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000489}
490
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000491// maybeUpdateRTTILinkage - Will update the linkage of the RTTI data structures
492// from available_externally to the correct linkage if necessary. An example of
493// this is:
494//
495// struct A {
496// virtual void f();
497// };
498//
499// const std::type_info &g() {
500// return typeid(A);
501// }
502//
503// void A::f() { }
504//
505// When we're generating the typeid(A) expression, we do not yet know that
506// A's key function is defined in this translation unit, so we will give the
507// typeinfo and typename structures available_externally linkage. When A::f
508// forces the vtable to be generated, we need to change the linkage of the
509// typeinfo and typename structs, otherwise we'll end up with undefined
510// externals when linking.
511static void
512maybeUpdateRTTILinkage(CodeGenModule &CGM, llvm::GlobalVariable *GV,
513 QualType Ty) {
514 // We're only interested in globals with available_externally linkage.
515 if (!GV->hasAvailableExternallyLinkage())
516 return;
517
518 // Get the real linkage for the type.
519 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(CGM, Ty);
520
521 // If variable is supposed to have available_externally linkage, we don't
522 // need to do anything.
523 if (Linkage == llvm::GlobalVariable::AvailableExternallyLinkage)
524 return;
525
526 // Update the typeinfo linkage.
527 GV->setLinkage(Linkage);
528
529 // Get the typename global.
530 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000531 llvm::raw_svector_ostream Out(OutName);
532 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
533 Out.flush();
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000534 llvm::StringRef Name = OutName.str();
535
536 llvm::GlobalVariable *TypeNameGV = CGM.getModule().getNamedGlobal(Name);
537
538 assert(TypeNameGV->hasAvailableExternallyLinkage() &&
539 "Type name has different linkage from type info!");
540
541 // And update its linkage.
542 TypeNameGV->setLinkage(Linkage);
543}
544
John McCallcbfe5022010-08-04 08:34:44 +0000545llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000546 // We want to operate on the canonical type.
547 Ty = CGM.getContext().getCanonicalType(Ty);
548
549 // Check if we've already emitted an RTTI descriptor for this type.
550 llvm::SmallString<256> OutName;
Rafael Espindolaf0be9792011-02-11 02:52:17 +0000551 llvm::raw_svector_ostream Out(OutName);
552 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
553 Out.flush();
Anders Carlsson8d145152009-12-20 22:30:54 +0000554 llvm::StringRef Name = OutName.str();
Anders Carlsson1cbce122011-01-29 19:16:51 +0000555
Anders Carlsson8d145152009-12-20 22:30:54 +0000556 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000557 if (OldGV && !OldGV->isDeclaration()) {
558 maybeUpdateRTTILinkage(CGM, OldGV, Ty);
559
Anders Carlsson8d145152009-12-20 22:30:54 +0000560 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
Anders Carlsson6d7f8472011-01-30 20:45:54 +0000561 }
John McCallcbfe5022010-08-04 08:34:44 +0000562
Anders Carlsson8d145152009-12-20 22:30:54 +0000563 // Check if there is already an external RTTI descriptor for this type.
John McCallcbfe5022010-08-04 08:34:44 +0000564 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000565 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
Anders Carlsson8d145152009-12-20 22:30:54 +0000566 return GetAddrOfExternalRTTIDescriptor(Ty);
567
John McCallcbfe5022010-08-04 08:34:44 +0000568 // Emit the standard library with external linkage.
569 llvm::GlobalVariable::LinkageTypes Linkage;
570 if (IsStdLib)
571 Linkage = llvm::GlobalValue::ExternalLinkage;
572 else
Anders Carlsson3a717f72011-01-24 02:04:33 +0000573 Linkage = getTypeInfoLinkage(CGM, Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +0000574
575 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000576 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000577
578 // And the name.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000579 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
580
581 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Anders Carlsson907c8282011-01-29 22:10:32 +0000582 Fields.push_back(llvm::ConstantExpr::getBitCast(TypeName, Int8PtrTy));
John McCallcbfe5022010-08-04 08:34:44 +0000583
Anders Carlsson8d145152009-12-20 22:30:54 +0000584 switch (Ty->getTypeClass()) {
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000585#define TYPE(Class, Base)
586#define ABSTRACT_TYPE(Class, Base)
587#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
588#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
589#define DEPENDENT_TYPE(Class, Base) case Type::Class:
590#include "clang/AST/TypeNodes.def"
591 assert(false && "Non-canonical and dependent types shouldn't get here");
Anders Carlsson8d145152009-12-20 22:30:54 +0000592
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000593 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000594 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000595 case Type::Vector:
596 case Type::ExtVector:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000597 case Type::Complex:
598 case Type::BlockPointer:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000599 // Itanium C++ ABI 2.9.5p4:
600 // abi::__fundamental_type_info adds no data members to std::type_info.
601 break;
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000602
603 case Type::LValueReference:
604 case Type::RValueReference:
605 assert(false && "References shouldn't get here");
606
Anders Carlsson978ef682009-12-29 21:58:32 +0000607 case Type::ConstantArray:
608 case Type::IncompleteArray:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000609 case Type::VariableArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000610 // Itanium C++ ABI 2.9.5p5:
611 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000612 break;
613
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000614 case Type::FunctionNoProto:
615 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000616 // Itanium C++ ABI 2.9.5p5:
617 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000618 break;
619
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000620 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000621 // Itanium C++ ABI 2.9.5p5:
622 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000623 break;
624
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000625 case Type::Record: {
626 const CXXRecordDecl *RD =
627 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000628 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000629 // We don't need to emit any fields.
630 break;
631 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000632
Anders Carlsson08148092009-12-30 23:47:56 +0000633 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000634 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000635 else
636 BuildVMIClassTypeInfo(RD);
637
638 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000639 }
John McCalle8dc53e2010-08-12 02:17:33 +0000640
641 case Type::ObjCObject:
642 case Type::ObjCInterface:
643 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
644 break;
645
646 case Type::ObjCObjectPointer:
647 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
648 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000649
Anders Carlsson8d145152009-12-20 22:30:54 +0000650 case Type::Pointer:
John McCalle8dc53e2010-08-12 02:17:33 +0000651 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000652 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000653
Anders Carlsson8d145152009-12-20 22:30:54 +0000654 case Type::MemberPointer:
655 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
656 break;
657 }
658
659 llvm::Constant *Init =
Anders Carlsson531d55f2009-12-31 17:43:53 +0000660 llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
Anders Carlsson8d145152009-12-20 22:30:54 +0000661 /*Packed=*/false);
662
663 llvm::GlobalVariable *GV =
664 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
665 /*Constant=*/true, Linkage, Init, Name);
666
667 // If there's already an old global variable, replace it with the new one.
668 if (OldGV) {
669 GV->takeName(OldGV);
670 llvm::Constant *NewPtr =
671 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
672 OldGV->replaceAllUsesWith(NewPtr);
673 OldGV->eraseFromParent();
674 }
John McCallcbfe5022010-08-04 08:34:44 +0000675
676 // GCC only relies on the uniqueness of the type names, not the
677 // type_infos themselves, so we can emit these as hidden symbols.
John McCall279b5eb2010-08-12 23:36:15 +0000678 // But don't do this if we're worried about strict visibility
679 // compatibility.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000680 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) {
681 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
682
683 CGM.setTypeVisibility(GV, RD, CodeGenModule::TVK_ForRTTI);
684 CGM.setTypeVisibility(TypeName, RD, CodeGenModule::TVK_ForRTTIName);
Anders Carlsson907c8282011-01-29 22:10:32 +0000685 } else {
686 Visibility TypeInfoVisibility = DefaultVisibility;
687 if (CGM.getCodeGenOpts().HiddenWeakVTables &&
688 Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
689 TypeInfoVisibility = HiddenVisibility;
Anders Carlsson9a86a132011-01-29 20:36:11 +0000690
Anders Carlsson907c8282011-01-29 22:10:32 +0000691 // The type name should have the same visibility as the type itself.
692 Visibility ExplicitVisibility = Ty->getVisibility();
693 TypeName->setVisibility(CodeGenModule::
694 GetLLVMVisibility(ExplicitVisibility));
695
696 TypeInfoVisibility = minVisibility(TypeInfoVisibility, Ty->getVisibility());
697 GV->setVisibility(CodeGenModule::GetLLVMVisibility(TypeInfoVisibility));
Rafael Espindolab1c65ff2011-01-11 21:44:37 +0000698 }
Anders Carlsson907c8282011-01-29 22:10:32 +0000699
Rafael Espindola57244f62011-01-11 23:55:05 +0000700 GV->setUnnamedAddr(true);
701
Anders Carlsson8d145152009-12-20 22:30:54 +0000702 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
703}
704
Anders Carlsson08148092009-12-30 23:47:56 +0000705/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000706/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000707static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000708 unsigned Flags = 0;
709
710 if (Quals.hasConst())
711 Flags |= RTTIBuilder::PTI_Const;
712 if (Quals.hasVolatile())
713 Flags |= RTTIBuilder::PTI_Volatile;
714 if (Quals.hasRestrict())
715 Flags |= RTTIBuilder::PTI_Restrict;
716
717 return Flags;
718}
719
John McCalle8dc53e2010-08-12 02:17:33 +0000720/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
721/// for the given Objective-C object type.
722void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
723 // Drop qualifiers.
724 const Type *T = OT->getBaseType().getTypePtr();
725 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
726
727 // The builtin types are abi::__class_type_infos and don't require
728 // extra fields.
729 if (isa<BuiltinType>(T)) return;
730
731 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
732 ObjCInterfaceDecl *Super = Class->getSuperClass();
733
734 // Root classes are also __class_type_info.
735 if (!Super) return;
736
737 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
738
739 // Everything else is single inheritance.
740 llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
741 Fields.push_back(BaseTypeInfo);
742}
743
Anders Carlssonf64531a2009-12-30 01:00:12 +0000744/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
745/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
746void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
747 // Itanium C++ ABI 2.9.5p6b:
748 // It adds to abi::__class_type_info a single member pointing to the
749 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000750 llvm::Constant *BaseTypeInfo =
751 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
752 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000753}
754
Benjamin Kramer79ba2a62010-10-22 16:48:22 +0000755namespace {
756 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
757 /// a class hierarchy.
758 struct SeenBases {
759 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
760 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
761 };
762}
Anders Carlsson08148092009-12-30 23:47:56 +0000763
764/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
765/// abi::__vmi_class_type_info.
766///
767static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
768 SeenBases &Bases) {
769
770 unsigned Flags = 0;
771
772 const CXXRecordDecl *BaseDecl =
773 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
774
775 if (Base->isVirtual()) {
776 if (Bases.VirtualBases.count(BaseDecl)) {
777 // If this virtual base has been seen before, then the class is diamond
778 // shaped.
779 Flags |= RTTIBuilder::VMI_DiamondShaped;
780 } else {
781 if (Bases.NonVirtualBases.count(BaseDecl))
782 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
783
784 // Mark the virtual base as seen.
785 Bases.VirtualBases.insert(BaseDecl);
786 }
787 } else {
788 if (Bases.NonVirtualBases.count(BaseDecl)) {
789 // If this non-virtual base has been seen before, then the class has non-
790 // diamond shaped repeated inheritance.
791 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
792 } else {
793 if (Bases.VirtualBases.count(BaseDecl))
794 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
795
796 // Mark the non-virtual base as seen.
797 Bases.NonVirtualBases.insert(BaseDecl);
798 }
799 }
800
801 // Walk all bases.
802 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
803 E = BaseDecl->bases_end(); I != E; ++I)
804 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
805
806 return Flags;
807}
808
809static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
810 unsigned Flags = 0;
811 SeenBases Bases;
812
813 // Walk all bases.
814 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
815 E = RD->bases_end(); I != E; ++I)
816 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
817
818 return Flags;
819}
820
821/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
822/// classes with bases that do not satisfy the abi::__si_class_type_info
823/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
824void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
825 const llvm::Type *UnsignedIntLTy =
826 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
827
828 // Itanium C++ ABI 2.9.5p6c:
829 // __flags is a word with flags describing details about the class
830 // structure, which may be referenced by using the __flags_masks
831 // enumeration. These flags refer to both direct and indirect bases.
832 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000833 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000834
835 // Itanium C++ ABI 2.9.5p6c:
836 // __base_count is a word with the number of direct proper base class
837 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000838 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000839
840 if (!RD->getNumBases())
841 return;
842
843 const llvm::Type *LongLTy =
844 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
845
846 // Now add the base class descriptions.
847
848 // Itanium C++ ABI 2.9.5p6c:
849 // __base_info[] is an array of base class descriptions -- one for every
850 // direct proper base. Each description is of the type:
851 //
852 // struct abi::__base_class_type_info {
Eli Friedmana7e68452010-08-22 01:00:03 +0000853 // public:
Anders Carlsson08148092009-12-30 23:47:56 +0000854 // const __class_type_info *__base_type;
855 // long __offset_flags;
856 //
857 // enum __offset_flags_masks {
858 // __virtual_mask = 0x1,
859 // __public_mask = 0x2,
860 // __offset_shift = 8
861 // };
862 // };
863 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
864 E = RD->bases_end(); I != E; ++I) {
865 const CXXBaseSpecifier *Base = I;
866
867 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000868 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000869
870 const CXXRecordDecl *BaseDecl =
871 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
872
873 int64_t OffsetFlags = 0;
874
875 // All but the lower 8 bits of __offset_flags are a signed offset.
876 // For a non-virtual base, this is the offset in the object of the base
877 // subobject. For a virtual base, this is the offset in the virtual table of
878 // the virtual base offset for the virtual base referenced (negative).
879 if (Base->isVirtual())
Anders Carlssonaf440352010-03-23 04:11:45 +0000880 OffsetFlags = CGM.getVTables().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000881 else {
882 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Anders Carlssona14f5972010-10-31 23:22:37 +0000883 OffsetFlags = Layout.getBaseClassOffsetInBits(BaseDecl) / 8;
Anders Carlsson08148092009-12-30 23:47:56 +0000884 };
885
886 OffsetFlags <<= 8;
887
888 // The low-order byte of __offset_flags contains flags, as given by the
889 // masks from the enumeration __offset_flags_masks.
890 if (Base->isVirtual())
891 OffsetFlags |= BCTI_Virtual;
892 if (Base->getAccessSpecifier() == AS_public)
893 OffsetFlags |= BCTI_Public;
894
Anders Carlsson531d55f2009-12-31 17:43:53 +0000895 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000896 }
897}
898
Anders Carlsson8d145152009-12-20 22:30:54 +0000899/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
900/// used for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +0000901void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
Anders Carlssonabd6b092010-06-02 15:44:35 +0000902 Qualifiers Quals;
903 QualType UnqualifiedPointeeTy =
904 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
905
Anders Carlsson8d145152009-12-20 22:30:54 +0000906 // Itanium C++ ABI 2.9.5p7:
907 // __flags is a flag word describing the cv-qualification and other
908 // attributes of the type pointed to
Anders Carlssonabd6b092010-06-02 15:44:35 +0000909 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000910
911 // Itanium C++ ABI 2.9.5p7:
912 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
913 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000914 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000915 Flags |= PTI_Incomplete;
916
917 const llvm::Type *UnsignedIntLTy =
918 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000919 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000920
921 // Itanium C++ ABI 2.9.5p7:
922 // __pointee is a pointer to the std::type_info derivation for the
923 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000924 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000925 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000926 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000927}
928
929/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
930/// struct, used for member pointer types.
931void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
932 QualType PointeeTy = Ty->getPointeeType();
933
Anders Carlssonabd6b092010-06-02 15:44:35 +0000934 Qualifiers Quals;
935 QualType UnqualifiedPointeeTy =
936 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
937
Anders Carlsson8d145152009-12-20 22:30:54 +0000938 // Itanium C++ ABI 2.9.5p7:
939 // __flags is a flag word describing the cv-qualification and other
940 // attributes of the type pointed to.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000941 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000942
943 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000944
945 // Itanium C++ ABI 2.9.5p7:
946 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
947 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000948 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000949 Flags |= PTI_Incomplete;
950
Anders Carlsson8d145152009-12-20 22:30:54 +0000951 if (IsIncompleteClassType(ClassType))
952 Flags |= PTI_ContainingClassIncomplete;
953
Anders Carlsson8d145152009-12-20 22:30:54 +0000954 const llvm::Type *UnsignedIntLTy =
955 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000956 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000957
958 // Itanium C++ ABI 2.9.5p7:
959 // __pointee is a pointer to the std::type_info derivation for the
960 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000961 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000962 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000963 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000964
965 // Itanium C++ ABI 2.9.5p9:
966 // __context is a pointer to an abi::__class_type_info corresponding to the
967 // class type containing the member pointed to
968 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000969 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000970}
971
John McCall9dffe6f2010-04-30 01:15:21 +0000972llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
973 bool ForEH) {
974 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
975 // FIXME: should we even be calling this method if RTTI is disabled
976 // and it's not for EH?
977 if (!ForEH && !getContext().getLangOptions().RTTI) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000978 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
979 return llvm::Constant::getNullValue(Int8PtrTy);
980 }
John McCall9dffe6f2010-04-30 01:15:21 +0000981
Anders Carlsson531d55f2009-12-31 17:43:53 +0000982 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000983}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000984
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000985void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
986 QualType PointerType = Context.getPointerType(Type);
987 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
988 RTTIBuilder(*this).BuildTypeInfo(Type, true);
989 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
990 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
991}
992
993void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Anders Carlsson2bd62502010-11-04 05:28:09 +0000994 QualType FundamentalTypes[] = { Context.VoidTy, Context.NullPtrTy,
995 Context.BoolTy, Context.WCharTy,
996 Context.CharTy, Context.UnsignedCharTy,
997 Context.SignedCharTy, Context.ShortTy,
998 Context.UnsignedShortTy, Context.IntTy,
999 Context.UnsignedIntTy, Context.LongTy,
1000 Context.UnsignedLongTy, Context.LongLongTy,
1001 Context.UnsignedLongLongTy, Context.FloatTy,
1002 Context.DoubleTy, Context.LongDoubleTy,
1003 Context.Char16Ty, Context.Char32Ty };
Rafael Espindolad1a5c312010-03-27 02:52:14 +00001004 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
1005 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
1006}