blob: 7a7195aac44e32018aa7dc4a378274d445e5dd1b [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 Carlsson1d7088d2009-12-17 07:09:17 +000033 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
34 /// descriptor of the given type.
35 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
36
Anders Carlsson046c2942010-04-17 20:15:18 +000037 /// BuildVTablePointer - Build the vtable pointer for the given type.
38 void BuildVTablePointer(const Type *Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +000039
Anders Carlssonf64531a2009-12-30 01:00:12 +000040 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
Anders Carlsson08148092009-12-30 23:47:56 +000041 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
Anders Carlssonf64531a2009-12-30 01:00:12 +000042 void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
43
Anders Carlsson08148092009-12-30 23:47:56 +000044 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
45 /// classes with bases that do not satisfy the abi::__si_class_type_info
46 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
47 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
48
Anders Carlssonf64531a2009-12-30 01:00:12 +000049 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
50 /// for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +000051 void BuildPointerTypeInfo(QualType PointeeTy);
52
53 /// BuildObjCObjectTypeInfo - Build the appropriate kind of
54 /// type_info for an object type.
55 void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +000056
57 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
58 /// struct, used for member pointer types.
59 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
60
Mike Stump2b1bf312009-11-14 14:25:18 +000061public:
Mike Stumpde050572009-12-02 18:57:08 +000062 RTTIBuilder(CodeGenModule &cgm)
Mike Stump2b1bf312009-11-14 14:25:18 +000063 : CGM(cgm), VMContext(cgm.getModule().getContext()),
64 Int8PtrTy(llvm::Type::getInt8PtrTy(VMContext)) { }
65
Anders Carlsson31b7f522009-12-11 02:46:30 +000066 llvm::Constant *BuildName(QualType Ty, bool Hidden,
67 llvm::GlobalVariable::LinkageTypes Linkage) {
Mike Stump2b1bf312009-11-14 14:25:18 +000068 llvm::SmallString<256> OutName;
John McCall4c40d982010-08-31 07:33:07 +000069 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, OutName);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +000070 llvm::StringRef Name = OutName.str();
Mike Stumpcbcd4e52009-11-14 23:32:21 +000071
Anders Carlsson8d145152009-12-20 22:30:54 +000072 llvm::GlobalVariable *OGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +000073 if (OGV && !OGV->isDeclaration())
74 return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy);
Mike Stump58588942009-11-19 01:08:19 +000075
Anders Carlsson31b7f522009-12-11 02:46:30 +000076 llvm::Constant *C = llvm::ConstantArray::get(VMContext, Name.substr(4));
Mike Stump2b1bf312009-11-14 14:25:18 +000077
Anders Carlsson31b7f522009-12-11 02:46:30 +000078 llvm::GlobalVariable *GV =
79 new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
80 C, Name);
Mike Stump58588942009-11-19 01:08:19 +000081 if (OGV) {
82 GV->takeName(OGV);
83 llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
84 OGV->getType());
85 OGV->replaceAllUsesWith(NewPtr);
86 OGV->eraseFromParent();
87 }
Argyrios Kyrtzidis6d576052010-10-11 03:25:53 +000088 if (Hidden && Linkage != llvm::GlobalValue::InternalLinkage)
Mike Stump582b0372009-11-18 03:46:51 +000089 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
90 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Daniel Dunbar7177dee2009-12-19 17:50:07 +000091 }
Mike Stumpc7a05bd2009-11-14 15:55:18 +000092
Mike Stump4e6f8ee2009-12-24 02:33:48 +000093 // FIXME: unify with DecideExtern
Mike Stump58588942009-11-19 01:08:19 +000094 bool DecideHidden(QualType Ty) {
95 // For this type, see if all components are never hidden.
96 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>())
97 return (DecideHidden(MPT->getPointeeType())
98 && DecideHidden(QualType(MPT->getClass(), 0)));
99 if (const PointerType *PT = Ty->getAs<PointerType>())
100 return DecideHidden(PT->getPointeeType());
Mike Stump4e6f8ee2009-12-24 02:33:48 +0000101 if (const FunctionType *FT = Ty->getAs<FunctionType>()) {
102 if (DecideHidden(FT->getResultType()) == false)
103 return false;
104 if (const FunctionProtoType *FPT = Ty->getAs<FunctionProtoType>()) {
105 for (unsigned i = 0; i <FPT->getNumArgs(); ++i)
106 if (DecideHidden(FPT->getArgType(i)) == false)
107 return false;
108 for (unsigned i = 0; i <FPT->getNumExceptions(); ++i)
109 if (DecideHidden(FPT->getExceptionType(i)) == false)
110 return false;
111 return true;
112 }
113 }
Mike Stump58588942009-11-19 01:08:19 +0000114 if (const RecordType *RT = Ty->getAs<RecordType>())
115 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
John McCall1fb0caa2010-10-22 21:05:15 +0000116 return RD->getVisibility() == HiddenVisibility;
Mike Stump58588942009-11-19 01:08:19 +0000117 return false;
118 }
Anders Carlsson31b7f522009-12-11 02:46:30 +0000119
Anders Carlsson8d145152009-12-20 22:30:54 +0000120 // Pointer type info flags.
121 enum {
122 /// PTI_Const - Type has const qualifier.
123 PTI_Const = 0x1,
124
125 /// PTI_Volatile - Type has volatile qualifier.
126 PTI_Volatile = 0x2,
127
128 /// PTI_Restrict - Type has restrict qualifier.
129 PTI_Restrict = 0x4,
130
131 /// PTI_Incomplete - Type is incomplete.
132 PTI_Incomplete = 0x8,
133
134 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
135 /// (in pointer to member).
136 PTI_ContainingClassIncomplete = 0x10
137 };
Anders Carlsson08148092009-12-30 23:47:56 +0000138
139 // VMI type info flags.
140 enum {
141 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
142 VMI_NonDiamondRepeat = 0x1,
143
144 /// VMI_DiamondShaped - Class is diamond shaped.
145 VMI_DiamondShaped = 0x2
146 };
147
148 // Base class type info flags.
149 enum {
150 /// BCTI_Virtual - Base class is virtual.
151 BCTI_Virtual = 0x1,
152
153 /// BCTI_Public - Base class is public.
154 BCTI_Public = 0x2
155 };
Anders Carlsson531d55f2009-12-31 17:43:53 +0000156
157 /// BuildTypeInfo - Build the RTTI type info struct for the given type.
John McCall9dffe6f2010-04-30 01:15:21 +0000158 ///
159 /// \param Force - true to force the creation of this RTTI value
160 /// \param ForEH - true if this is for exception handling
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000161 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false);
Mike Stump2b1bf312009-11-14 14:25:18 +0000162};
Mike Stump92f2fe22009-12-02 19:07:44 +0000163}
Mike Stump2b1bf312009-11-14 14:25:18 +0000164
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000165llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
166 // Mangle the RTTI name.
167 llvm::SmallString<256> OutName;
John McCall4c40d982010-08-31 07:33:07 +0000168 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, OutName);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000169 llvm::StringRef Name = OutName.str();
170
Anders Carlsson8d145152009-12-20 22:30:54 +0000171 // Look for an existing global.
172 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000173
174 if (!GV) {
175 // Create a new global variable.
176 GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
177 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000178 }
179
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000180 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000181}
182
Anders Carlsson8d145152009-12-20 22:30:54 +0000183/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
184/// info for that type is defined in the standard library.
185static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
186 // Itanium C++ ABI 2.9.2:
187 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
188 // the run-time support library. Specifically, the run-time support
189 // library should contain type_info objects for the types X, X* and
Anders Carlsson2bd62502010-11-04 05:28:09 +0000190 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
191 // unsigned char, signed char, short, unsigned short, int, unsigned int,
192 // long, unsigned long, long long, unsigned long long, float, double,
193 // long double, char16_t, char32_t, and the IEEE 754r decimal and
194 // half-precision floating point types.
Anders Carlsson8d145152009-12-20 22:30:54 +0000195 switch (Ty->getKind()) {
196 case BuiltinType::Void:
Anders Carlsson2bd62502010-11-04 05:28:09 +0000197 case BuiltinType::NullPtr:
Anders Carlsson8d145152009-12-20 22:30:54 +0000198 case BuiltinType::Bool:
199 case BuiltinType::WChar:
200 case BuiltinType::Char_U:
201 case BuiltinType::Char_S:
202 case BuiltinType::UChar:
203 case BuiltinType::SChar:
204 case BuiltinType::Short:
205 case BuiltinType::UShort:
206 case BuiltinType::Int:
207 case BuiltinType::UInt:
208 case BuiltinType::Long:
209 case BuiltinType::ULong:
210 case BuiltinType::LongLong:
211 case BuiltinType::ULongLong:
212 case BuiltinType::Float:
213 case BuiltinType::Double:
214 case BuiltinType::LongDouble:
215 case BuiltinType::Char16:
216 case BuiltinType::Char32:
217 case BuiltinType::Int128:
218 case BuiltinType::UInt128:
219 return true;
220
221 case BuiltinType::Overload:
222 case BuiltinType::Dependent:
223 case BuiltinType::UndeducedAuto:
224 assert(false && "Should not see this type here!");
225
Anders Carlsson8d145152009-12-20 22:30:54 +0000226 case BuiltinType::ObjCId:
227 case BuiltinType::ObjCClass:
228 case BuiltinType::ObjCSel:
229 assert(false && "FIXME: Objective-C types are unsupported!");
230 }
231
232 // Silent gcc.
233 return false;
234}
235
236static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
237 QualType PointeeTy = PointerTy->getPointeeType();
238 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
239 if (!BuiltinTy)
240 return false;
241
242 // Check the qualifiers.
243 Qualifiers Quals = PointeeTy.getQualifiers();
244 Quals.removeConst();
245
246 if (!Quals.empty())
247 return false;
248
249 return TypeInfoIsInStandardLibrary(BuiltinTy);
250}
251
John McCallcbfe5022010-08-04 08:34:44 +0000252/// IsStandardLibraryRTTIDescriptor - Returns whether the type
253/// information for the given type exists in the standard library.
254static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000255 // Type info for builtin types is defined in the standard library.
256 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
257 return TypeInfoIsInStandardLibrary(BuiltinTy);
258
259 // Type info for some pointer types to builtin types is defined in the
260 // standard library.
261 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
262 return TypeInfoIsInStandardLibrary(PointerTy);
263
John McCallcbfe5022010-08-04 08:34:44 +0000264 return false;
265}
266
267/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
268/// the given type exists somewhere else, and that we should not emit the type
269/// information in this translation unit. Assumes that it is not a
270/// standard-library type.
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000271static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM, QualType Ty) {
272 ASTContext &Context = CGM.getContext();
273
John McCall9dffe6f2010-04-30 01:15:21 +0000274 // If RTTI is disabled, don't consider key functions.
275 if (!Context.getLangOptions().RTTI) return false;
276
Anders Carlsson8d145152009-12-20 22:30:54 +0000277 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000278 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000279 if (!RD->hasDefinition())
280 return false;
281
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000282 if (!RD->isDynamicClass())
283 return false;
284
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000285 return !CGM.getVTables().ShouldEmitVTableInThisTU(RD);
Anders Carlsson8d145152009-12-20 22:30:54 +0000286 }
287
288 return false;
289}
290
291/// IsIncompleteClassType - Returns whether the given record type is incomplete.
292static bool IsIncompleteClassType(const RecordType *RecordTy) {
293 return !RecordTy->getDecl()->isDefinition();
294}
295
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000296/// ContainsIncompleteClassType - Returns whether the given type contains an
297/// incomplete class type. This is true if
298///
299/// * The given type is an incomplete class type.
300/// * The given type is a pointer type whose pointee type contains an
301/// incomplete class type.
302/// * The given type is a member pointer type whose class is an incomplete
303/// class type.
304/// * The given type is a member pointer type whoise pointee type contains an
305/// incomplete class type.
Anders Carlsson8d145152009-12-20 22:30:54 +0000306/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000307static bool ContainsIncompleteClassType(QualType Ty) {
308 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
309 if (IsIncompleteClassType(RecordTy))
310 return true;
311 }
312
313 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
314 return ContainsIncompleteClassType(PointerTy->getPointeeType());
315
316 if (const MemberPointerType *MemberPointerTy =
317 dyn_cast<MemberPointerType>(Ty)) {
318 // Check if the class type is incomplete.
319 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
320 if (IsIncompleteClassType(ClassType))
321 return true;
322
323 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000324 }
325
326 return false;
327}
328
329/// getTypeInfoLinkage - Return the linkage that the type info and type info
330/// name constants should have for the given type.
331static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(QualType Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000332 // Itanium C++ ABI 2.9.5p7:
333 // In addition, it and all of the intermediate abi::__pointer_type_info
334 // structs in the chain down to the abi::__class_type_info for the
335 // incomplete class type must be prevented from resolving to the
336 // corresponding type_info structs for the complete class type, possibly
337 // by making them local static objects. Finally, a dummy class RTTI is
338 // generated for the incomplete type that will not resolve to the final
339 // complete class RTTI (because the latter need not exist), possibly by
340 // making it a local static object.
341 if (ContainsIncompleteClassType(Ty))
342 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000343
Douglas Gregor031b3712010-03-31 00:15:35 +0000344 switch (Ty->getLinkage()) {
345 case NoLinkage:
346 case InternalLinkage:
347 case UniqueExternalLinkage:
348 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson978ef682009-12-29 21:58:32 +0000349
Douglas Gregor031b3712010-03-31 00:15:35 +0000350 case ExternalLinkage:
351 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
352 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
353 if (RD->isDynamicClass())
Anders Carlsson046c2942010-04-17 20:15:18 +0000354 return CodeGenModule::getVTableLinkage(RD);
Mike Stumpc8f76f52009-12-24 01:10:27 +0000355 }
Douglas Gregor031b3712010-03-31 00:15:35 +0000356
Mike Stumpc8f76f52009-12-24 01:10:27 +0000357 return llvm::GlobalValue::WeakODRLinkage;
358 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000359
Anders Carlsson8d145152009-12-20 22:30:54 +0000360 return llvm::GlobalValue::WeakODRLinkage;
361}
362
Anders Carlssonf64531a2009-12-30 01:00:12 +0000363// CanUseSingleInheritance - Return whether the given record decl has a "single,
364// public, non-virtual base at offset zero (i.e. the derived class is dynamic
365// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
366static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
367 // Check the number of bases.
368 if (RD->getNumBases() != 1)
369 return false;
370
371 // Get the base.
372 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
373
374 // Check that the base is not virtual.
375 if (Base->isVirtual())
376 return false;
377
378 // Check that the base is public.
379 if (Base->getAccessSpecifier() != AS_public)
380 return false;
381
382 // Check that the class is dynamic iff the base is.
383 const CXXRecordDecl *BaseDecl =
384 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
385 if (!BaseDecl->isEmpty() &&
386 BaseDecl->isDynamicClass() != RD->isDynamicClass())
387 return false;
388
389 return true;
390}
391
Anders Carlsson046c2942010-04-17 20:15:18 +0000392void RTTIBuilder::BuildVTablePointer(const Type *Ty) {
John McCalle8dc53e2010-08-12 02:17:33 +0000393 // abi::__class_type_info.
394 static const char * const ClassTypeInfo =
395 "_ZTVN10__cxxabiv117__class_type_infoE";
396 // abi::__si_class_type_info.
397 static const char * const SIClassTypeInfo =
398 "_ZTVN10__cxxabiv120__si_class_type_infoE";
399 // abi::__vmi_class_type_info.
400 static const char * const VMIClassTypeInfo =
401 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
402
Eli Friedman1cf26f52010-08-11 20:41:51 +0000403 const char *VTableName = 0;
Anders Carlsson8d145152009-12-20 22:30:54 +0000404
405 switch (Ty->getTypeClass()) {
Eli Friedman1cf26f52010-08-11 20:41:51 +0000406#define TYPE(Class, Base)
407#define ABSTRACT_TYPE(Class, Base)
408#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
409#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
410#define DEPENDENT_TYPE(Class, Base) case Type::Class:
411#include "clang/AST/TypeNodes.def"
412 assert(false && "Non-canonical and dependent types shouldn't get here");
413
414 case Type::LValueReference:
415 case Type::RValueReference:
416 assert(false && "References shouldn't get here");
Anders Carlsson978ef682009-12-29 21:58:32 +0000417
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000418 case Type::Builtin:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000419 // GCC treats vector and complex types as fundamental types.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000420 case Type::Vector:
421 case Type::ExtVector:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000422 case Type::Complex:
423 // FIXME: GCC treats block pointers as fundamental types?!
424 case Type::BlockPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000425 // abi::__fundamental_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000426 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000427 break;
428
Anders Carlsson978ef682009-12-29 21:58:32 +0000429 case Type::ConstantArray:
430 case Type::IncompleteArray:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000431 case Type::VariableArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000432 // abi::__array_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000433 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000434 break;
435
436 case Type::FunctionNoProto:
437 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000438 // abi::__function_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000439 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000440 break;
441
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000442 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000443 // abi::__enum_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000444 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000445 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000446
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000447 case Type::Record: {
448 const CXXRecordDecl *RD =
449 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000450
John McCall86ff3082010-02-04 22:26:26 +0000451 if (!RD->hasDefinition() || !RD->getNumBases()) {
John McCalle8dc53e2010-08-12 02:17:33 +0000452 VTableName = ClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000453 } else if (CanUseSingleInheritance(RD)) {
John McCalle8dc53e2010-08-12 02:17:33 +0000454 VTableName = SIClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000455 } else {
John McCalle8dc53e2010-08-12 02:17:33 +0000456 VTableName = VMIClassTypeInfo;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000457 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000458
459 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000460 }
461
Eli Friedman1cf26f52010-08-11 20:41:51 +0000462 case Type::ObjCObject:
John McCalle8dc53e2010-08-12 02:17:33 +0000463 // Ignore protocol qualifiers.
464 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
465
466 // Handle id and Class.
467 if (isa<BuiltinType>(Ty)) {
468 VTableName = ClassTypeInfo;
469 break;
470 }
471
472 assert(isa<ObjCInterfaceType>(Ty));
473 // Fall through.
474
Eli Friedman1cf26f52010-08-11 20:41:51 +0000475 case Type::ObjCInterface:
John McCalle8dc53e2010-08-12 02:17:33 +0000476 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
477 VTableName = SIClassTypeInfo;
478 } else {
479 VTableName = ClassTypeInfo;
480 }
Eli Friedman1cf26f52010-08-11 20:41:51 +0000481 break;
482
John McCalle8dc53e2010-08-12 02:17:33 +0000483 case Type::ObjCObjectPointer:
Anders Carlsson8d145152009-12-20 22:30:54 +0000484 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000485 // abi::__pointer_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000486 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000487 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000488
Anders Carlsson8d145152009-12-20 22:30:54 +0000489 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000490 // abi::__pointer_to_member_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000491 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000492 break;
493 }
494
Anders Carlsson046c2942010-04-17 20:15:18 +0000495 llvm::Constant *VTable =
496 CGM.getModule().getOrInsertGlobal(VTableName, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000497
498 const llvm::Type *PtrDiffTy =
499 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
500
501 // The vtable address point is 2.
502 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Anders Carlsson046c2942010-04-17 20:15:18 +0000503 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, &Two, 1);
504 VTable = llvm::ConstantExpr::getBitCast(VTable, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000505
Anders Carlsson046c2942010-04-17 20:15:18 +0000506 Fields.push_back(VTable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000507}
508
John McCallcbfe5022010-08-04 08:34:44 +0000509llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000510 // We want to operate on the canonical type.
511 Ty = CGM.getContext().getCanonicalType(Ty);
512
513 // Check if we've already emitted an RTTI descriptor for this type.
514 llvm::SmallString<256> OutName;
John McCall4c40d982010-08-31 07:33:07 +0000515 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, OutName);
Anders Carlsson8d145152009-12-20 22:30:54 +0000516 llvm::StringRef Name = OutName.str();
517
518 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
519 if (OldGV && !OldGV->isDeclaration())
520 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
John McCallcbfe5022010-08-04 08:34:44 +0000521
Anders Carlsson8d145152009-12-20 22:30:54 +0000522 // Check if there is already an external RTTI descriptor for this type.
John McCallcbfe5022010-08-04 08:34:44 +0000523 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000524 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
Anders Carlsson8d145152009-12-20 22:30:54 +0000525 return GetAddrOfExternalRTTIDescriptor(Ty);
526
John McCallcbfe5022010-08-04 08:34:44 +0000527 // Emit the standard library with external linkage.
528 llvm::GlobalVariable::LinkageTypes Linkage;
529 if (IsStdLib)
530 Linkage = llvm::GlobalValue::ExternalLinkage;
531 else
532 Linkage = getTypeInfoLinkage(Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +0000533
534 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000535 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000536
537 // And the name.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000538 Fields.push_back(BuildName(Ty, DecideHidden(Ty), Linkage));
John McCallcbfe5022010-08-04 08:34:44 +0000539
Anders Carlsson8d145152009-12-20 22:30:54 +0000540 switch (Ty->getTypeClass()) {
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000541#define TYPE(Class, Base)
542#define ABSTRACT_TYPE(Class, Base)
543#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
544#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
545#define DEPENDENT_TYPE(Class, Base) case Type::Class:
546#include "clang/AST/TypeNodes.def"
547 assert(false && "Non-canonical and dependent types shouldn't get here");
Anders Carlsson8d145152009-12-20 22:30:54 +0000548
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000549 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000550 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000551 case Type::Vector:
552 case Type::ExtVector:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000553 case Type::Complex:
554 case Type::BlockPointer:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000555 // Itanium C++ ABI 2.9.5p4:
556 // abi::__fundamental_type_info adds no data members to std::type_info.
557 break;
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000558
559 case Type::LValueReference:
560 case Type::RValueReference:
561 assert(false && "References shouldn't get here");
562
Anders Carlsson978ef682009-12-29 21:58:32 +0000563 case Type::ConstantArray:
564 case Type::IncompleteArray:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000565 case Type::VariableArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000566 // Itanium C++ ABI 2.9.5p5:
567 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000568 break;
569
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000570 case Type::FunctionNoProto:
571 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000572 // Itanium C++ ABI 2.9.5p5:
573 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000574 break;
575
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000576 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000577 // Itanium C++ ABI 2.9.5p5:
578 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000579 break;
580
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000581 case Type::Record: {
582 const CXXRecordDecl *RD =
583 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000584 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000585 // We don't need to emit any fields.
586 break;
587 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000588
Anders Carlsson08148092009-12-30 23:47:56 +0000589 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000590 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000591 else
592 BuildVMIClassTypeInfo(RD);
593
594 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000595 }
John McCalle8dc53e2010-08-12 02:17:33 +0000596
597 case Type::ObjCObject:
598 case Type::ObjCInterface:
599 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
600 break;
601
602 case Type::ObjCObjectPointer:
603 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
604 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000605
Anders Carlsson8d145152009-12-20 22:30:54 +0000606 case Type::Pointer:
John McCalle8dc53e2010-08-12 02:17:33 +0000607 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000608 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000609
Anders Carlsson8d145152009-12-20 22:30:54 +0000610 case Type::MemberPointer:
611 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
612 break;
613 }
614
615 llvm::Constant *Init =
Anders Carlsson531d55f2009-12-31 17:43:53 +0000616 llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
Anders Carlsson8d145152009-12-20 22:30:54 +0000617 /*Packed=*/false);
618
619 llvm::GlobalVariable *GV =
620 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
621 /*Constant=*/true, Linkage, Init, Name);
622
623 // If there's already an old global variable, replace it with the new one.
624 if (OldGV) {
625 GV->takeName(OldGV);
626 llvm::Constant *NewPtr =
627 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
628 OldGV->replaceAllUsesWith(NewPtr);
629 OldGV->eraseFromParent();
630 }
John McCallcbfe5022010-08-04 08:34:44 +0000631
632 // GCC only relies on the uniqueness of the type names, not the
633 // type_infos themselves, so we can emit these as hidden symbols.
John McCall279b5eb2010-08-12 23:36:15 +0000634 // But don't do this if we're worried about strict visibility
635 // compatibility.
John McCallcbfe5022010-08-04 08:34:44 +0000636 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
637 CGM.setTypeVisibility(GV, cast<CXXRecordDecl>(RT->getDecl()),
John McCallaf146032010-10-30 11:50:40 +0000638 /*ForRTTI*/ true, /*ForDefinition*/ true);
John McCall279b5eb2010-08-12 23:36:15 +0000639 else if (CGM.getCodeGenOpts().HiddenWeakVTables &&
640 Linkage == llvm::GlobalValue::WeakODRLinkage)
John McCallcbfe5022010-08-04 08:34:44 +0000641 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
642
Anders Carlsson8d145152009-12-20 22:30:54 +0000643 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
644}
645
Anders Carlsson08148092009-12-30 23:47:56 +0000646/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000647/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000648static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000649 unsigned Flags = 0;
650
651 if (Quals.hasConst())
652 Flags |= RTTIBuilder::PTI_Const;
653 if (Quals.hasVolatile())
654 Flags |= RTTIBuilder::PTI_Volatile;
655 if (Quals.hasRestrict())
656 Flags |= RTTIBuilder::PTI_Restrict;
657
658 return Flags;
659}
660
John McCalle8dc53e2010-08-12 02:17:33 +0000661/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
662/// for the given Objective-C object type.
663void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
664 // Drop qualifiers.
665 const Type *T = OT->getBaseType().getTypePtr();
666 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
667
668 // The builtin types are abi::__class_type_infos and don't require
669 // extra fields.
670 if (isa<BuiltinType>(T)) return;
671
672 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
673 ObjCInterfaceDecl *Super = Class->getSuperClass();
674
675 // Root classes are also __class_type_info.
676 if (!Super) return;
677
678 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
679
680 // Everything else is single inheritance.
681 llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
682 Fields.push_back(BaseTypeInfo);
683}
684
Anders Carlssonf64531a2009-12-30 01:00:12 +0000685/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
686/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
687void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
688 // Itanium C++ ABI 2.9.5p6b:
689 // It adds to abi::__class_type_info a single member pointing to the
690 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000691 llvm::Constant *BaseTypeInfo =
692 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
693 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000694}
695
Benjamin Kramer79ba2a62010-10-22 16:48:22 +0000696namespace {
697 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
698 /// a class hierarchy.
699 struct SeenBases {
700 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
701 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
702 };
703}
Anders Carlsson08148092009-12-30 23:47:56 +0000704
705/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
706/// abi::__vmi_class_type_info.
707///
708static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
709 SeenBases &Bases) {
710
711 unsigned Flags = 0;
712
713 const CXXRecordDecl *BaseDecl =
714 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
715
716 if (Base->isVirtual()) {
717 if (Bases.VirtualBases.count(BaseDecl)) {
718 // If this virtual base has been seen before, then the class is diamond
719 // shaped.
720 Flags |= RTTIBuilder::VMI_DiamondShaped;
721 } else {
722 if (Bases.NonVirtualBases.count(BaseDecl))
723 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
724
725 // Mark the virtual base as seen.
726 Bases.VirtualBases.insert(BaseDecl);
727 }
728 } else {
729 if (Bases.NonVirtualBases.count(BaseDecl)) {
730 // If this non-virtual base has been seen before, then the class has non-
731 // diamond shaped repeated inheritance.
732 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
733 } else {
734 if (Bases.VirtualBases.count(BaseDecl))
735 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
736
737 // Mark the non-virtual base as seen.
738 Bases.NonVirtualBases.insert(BaseDecl);
739 }
740 }
741
742 // Walk all bases.
743 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
744 E = BaseDecl->bases_end(); I != E; ++I)
745 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
746
747 return Flags;
748}
749
750static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
751 unsigned Flags = 0;
752 SeenBases Bases;
753
754 // Walk all bases.
755 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
756 E = RD->bases_end(); I != E; ++I)
757 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
758
759 return Flags;
760}
761
762/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
763/// classes with bases that do not satisfy the abi::__si_class_type_info
764/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
765void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
766 const llvm::Type *UnsignedIntLTy =
767 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
768
769 // Itanium C++ ABI 2.9.5p6c:
770 // __flags is a word with flags describing details about the class
771 // structure, which may be referenced by using the __flags_masks
772 // enumeration. These flags refer to both direct and indirect bases.
773 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000774 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000775
776 // Itanium C++ ABI 2.9.5p6c:
777 // __base_count is a word with the number of direct proper base class
778 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000779 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000780
781 if (!RD->getNumBases())
782 return;
783
784 const llvm::Type *LongLTy =
785 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
786
787 // Now add the base class descriptions.
788
789 // Itanium C++ ABI 2.9.5p6c:
790 // __base_info[] is an array of base class descriptions -- one for every
791 // direct proper base. Each description is of the type:
792 //
793 // struct abi::__base_class_type_info {
Eli Friedmana7e68452010-08-22 01:00:03 +0000794 // public:
Anders Carlsson08148092009-12-30 23:47:56 +0000795 // const __class_type_info *__base_type;
796 // long __offset_flags;
797 //
798 // enum __offset_flags_masks {
799 // __virtual_mask = 0x1,
800 // __public_mask = 0x2,
801 // __offset_shift = 8
802 // };
803 // };
804 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
805 E = RD->bases_end(); I != E; ++I) {
806 const CXXBaseSpecifier *Base = I;
807
808 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000809 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000810
811 const CXXRecordDecl *BaseDecl =
812 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
813
814 int64_t OffsetFlags = 0;
815
816 // All but the lower 8 bits of __offset_flags are a signed offset.
817 // For a non-virtual base, this is the offset in the object of the base
818 // subobject. For a virtual base, this is the offset in the virtual table of
819 // the virtual base offset for the virtual base referenced (negative).
820 if (Base->isVirtual())
Anders Carlssonaf440352010-03-23 04:11:45 +0000821 OffsetFlags = CGM.getVTables().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000822 else {
823 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Anders Carlssona14f5972010-10-31 23:22:37 +0000824 OffsetFlags = Layout.getBaseClassOffsetInBits(BaseDecl) / 8;
Anders Carlsson08148092009-12-30 23:47:56 +0000825 };
826
827 OffsetFlags <<= 8;
828
829 // The low-order byte of __offset_flags contains flags, as given by the
830 // masks from the enumeration __offset_flags_masks.
831 if (Base->isVirtual())
832 OffsetFlags |= BCTI_Virtual;
833 if (Base->getAccessSpecifier() == AS_public)
834 OffsetFlags |= BCTI_Public;
835
Anders Carlsson531d55f2009-12-31 17:43:53 +0000836 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000837 }
838}
839
Anders Carlsson8d145152009-12-20 22:30:54 +0000840/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
841/// used for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +0000842void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
Anders Carlssonabd6b092010-06-02 15:44:35 +0000843 Qualifiers Quals;
844 QualType UnqualifiedPointeeTy =
845 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
846
Anders Carlsson8d145152009-12-20 22:30:54 +0000847 // Itanium C++ ABI 2.9.5p7:
848 // __flags is a flag word describing the cv-qualification and other
849 // attributes of the type pointed to
Anders Carlssonabd6b092010-06-02 15:44:35 +0000850 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000851
852 // Itanium C++ ABI 2.9.5p7:
853 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
854 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000855 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000856 Flags |= PTI_Incomplete;
857
858 const llvm::Type *UnsignedIntLTy =
859 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000860 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000861
862 // Itanium C++ ABI 2.9.5p7:
863 // __pointee is a pointer to the std::type_info derivation for the
864 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000865 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000866 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000867 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000868}
869
870/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
871/// struct, used for member pointer types.
872void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
873 QualType PointeeTy = Ty->getPointeeType();
874
Anders Carlssonabd6b092010-06-02 15:44:35 +0000875 Qualifiers Quals;
876 QualType UnqualifiedPointeeTy =
877 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
878
Anders Carlsson8d145152009-12-20 22:30:54 +0000879 // Itanium C++ ABI 2.9.5p7:
880 // __flags is a flag word describing the cv-qualification and other
881 // attributes of the type pointed to.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000882 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000883
884 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000885
886 // Itanium C++ ABI 2.9.5p7:
887 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
888 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000889 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000890 Flags |= PTI_Incomplete;
891
Anders Carlsson8d145152009-12-20 22:30:54 +0000892 if (IsIncompleteClassType(ClassType))
893 Flags |= PTI_ContainingClassIncomplete;
894
Anders Carlsson8d145152009-12-20 22:30:54 +0000895 const llvm::Type *UnsignedIntLTy =
896 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000897 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000898
899 // Itanium C++ ABI 2.9.5p7:
900 // __pointee is a pointer to the std::type_info derivation for the
901 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000902 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000903 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000904 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000905
906 // Itanium C++ ABI 2.9.5p9:
907 // __context is a pointer to an abi::__class_type_info corresponding to the
908 // class type containing the member pointed to
909 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000910 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000911}
912
John McCall9dffe6f2010-04-30 01:15:21 +0000913llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
914 bool ForEH) {
915 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
916 // FIXME: should we even be calling this method if RTTI is disabled
917 // and it's not for EH?
918 if (!ForEH && !getContext().getLangOptions().RTTI) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000919 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
920 return llvm::Constant::getNullValue(Int8PtrTy);
921 }
John McCall9dffe6f2010-04-30 01:15:21 +0000922
Anders Carlsson531d55f2009-12-31 17:43:53 +0000923 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000924}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000925
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000926void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
927 QualType PointerType = Context.getPointerType(Type);
928 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
929 RTTIBuilder(*this).BuildTypeInfo(Type, true);
930 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
931 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
932}
933
934void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Anders Carlsson2bd62502010-11-04 05:28:09 +0000935 QualType FundamentalTypes[] = { Context.VoidTy, Context.NullPtrTy,
936 Context.BoolTy, Context.WCharTy,
937 Context.CharTy, Context.UnsignedCharTy,
938 Context.SignedCharTy, Context.ShortTy,
939 Context.UnsignedShortTy, Context.IntTy,
940 Context.UnsignedIntTy, Context.LongTy,
941 Context.UnsignedLongTy, Context.LongLongTy,
942 Context.UnsignedLongLongTy, Context.FloatTy,
943 Context.DoubleTy, Context.LongDoubleTy,
944 Context.Char16Ty, Context.Char32Ty };
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000945 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
946 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
947}