blob: 69171419b66f3933a5e29eda4f74ef2d0db0595b [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 Stumpae9b2be2009-11-17 23:45:57 +000014#include "clang/AST/Type.h"
Mike Stumpcbcd4e52009-11-14 23:32:21 +000015#include "clang/AST/RecordLayout.h"
Mike Stump61c38012009-11-17 21:44:24 +000016#include "CodeGenModule.h"
Anders Carlsson656e4c12009-10-10 20:49:04 +000017using namespace clang;
18using namespace CodeGen;
19
Mike Stump92f2fe22009-12-02 19:07:44 +000020namespace {
Mike Stumpde050572009-12-02 18:57:08 +000021class RTTIBuilder {
Mike Stump2b1bf312009-11-14 14:25:18 +000022 CodeGenModule &CGM; // Per-module state.
23 llvm::LLVMContext &VMContext;
Anders Carlsson8d145152009-12-20 22:30:54 +000024
Anders Carlsson08148092009-12-30 23:47:56 +000025 const llvm::Type *Int8PtrTy;
Anders Carlsson531d55f2009-12-31 17:43:53 +000026
27 /// Fields - The fields of the RTTI descriptor currently being built.
28 llvm::SmallVector<llvm::Constant *, 16> Fields;
Anders Carlssond6baec82009-12-11 01:27:37 +000029
Anders Carlsson1d7088d2009-12-17 07:09:17 +000030 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
31 /// descriptor of the given type.
32 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
33
Anders Carlsson046c2942010-04-17 20:15:18 +000034 /// BuildVTablePointer - Build the vtable pointer for the given type.
35 void BuildVTablePointer(const Type *Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +000036
Anders Carlssonf64531a2009-12-30 01:00:12 +000037 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
Anders Carlsson08148092009-12-30 23:47:56 +000038 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
Anders Carlssonf64531a2009-12-30 01:00:12 +000039 void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
40
Anders Carlsson08148092009-12-30 23:47:56 +000041 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
42 /// classes with bases that do not satisfy the abi::__si_class_type_info
43 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
44 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
45
Anders Carlssonf64531a2009-12-30 01:00:12 +000046 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
47 /// for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +000048 void BuildPointerTypeInfo(QualType PointeeTy);
49
50 /// BuildObjCObjectTypeInfo - Build the appropriate kind of
51 /// type_info for an object type.
52 void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +000053
54 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
55 /// struct, used for member pointer types.
56 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
57
Mike Stump2b1bf312009-11-14 14:25:18 +000058public:
Mike Stumpde050572009-12-02 18:57:08 +000059 RTTIBuilder(CodeGenModule &cgm)
Mike Stump2b1bf312009-11-14 14:25:18 +000060 : CGM(cgm), VMContext(cgm.getModule().getContext()),
61 Int8PtrTy(llvm::Type::getInt8PtrTy(VMContext)) { }
62
Anders Carlsson31b7f522009-12-11 02:46:30 +000063 llvm::Constant *BuildName(QualType Ty, bool Hidden,
64 llvm::GlobalVariable::LinkageTypes Linkage) {
Mike Stump2b1bf312009-11-14 14:25:18 +000065 llvm::SmallString<256> OutName;
Mike Stumpde050572009-12-02 18:57:08 +000066 CGM.getMangleContext().mangleCXXRTTIName(Ty, OutName);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +000067 llvm::StringRef Name = OutName.str();
Mike Stumpcbcd4e52009-11-14 23:32:21 +000068
Anders Carlsson8d145152009-12-20 22:30:54 +000069 llvm::GlobalVariable *OGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +000070 if (OGV && !OGV->isDeclaration())
71 return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy);
Mike Stump58588942009-11-19 01:08:19 +000072
Anders Carlsson31b7f522009-12-11 02:46:30 +000073 llvm::Constant *C = llvm::ConstantArray::get(VMContext, Name.substr(4));
Mike Stump2b1bf312009-11-14 14:25:18 +000074
Anders Carlsson31b7f522009-12-11 02:46:30 +000075 llvm::GlobalVariable *GV =
76 new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
77 C, Name);
Mike Stump58588942009-11-19 01:08:19 +000078 if (OGV) {
79 GV->takeName(OGV);
80 llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
81 OGV->getType());
82 OGV->replaceAllUsesWith(NewPtr);
83 OGV->eraseFromParent();
84 }
Mike Stump582b0372009-11-18 03:46:51 +000085 if (Hidden)
86 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
87 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Daniel Dunbar7177dee2009-12-19 17:50:07 +000088 }
Mike Stumpc7a05bd2009-11-14 15:55:18 +000089
Mike Stump4e6f8ee2009-12-24 02:33:48 +000090 // FIXME: unify with DecideExtern
Mike Stump58588942009-11-19 01:08:19 +000091 bool DecideHidden(QualType Ty) {
92 // For this type, see if all components are never hidden.
93 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>())
94 return (DecideHidden(MPT->getPointeeType())
95 && DecideHidden(QualType(MPT->getClass(), 0)));
96 if (const PointerType *PT = Ty->getAs<PointerType>())
97 return DecideHidden(PT->getPointeeType());
Mike Stump4e6f8ee2009-12-24 02:33:48 +000098 if (const FunctionType *FT = Ty->getAs<FunctionType>()) {
99 if (DecideHidden(FT->getResultType()) == false)
100 return false;
101 if (const FunctionProtoType *FPT = Ty->getAs<FunctionProtoType>()) {
102 for (unsigned i = 0; i <FPT->getNumArgs(); ++i)
103 if (DecideHidden(FPT->getArgType(i)) == false)
104 return false;
105 for (unsigned i = 0; i <FPT->getNumExceptions(); ++i)
106 if (DecideHidden(FPT->getExceptionType(i)) == false)
107 return false;
108 return true;
109 }
110 }
Mike Stump58588942009-11-19 01:08:19 +0000111 if (const RecordType *RT = Ty->getAs<RecordType>())
112 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
113 return CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden;
114 return false;
115 }
Anders Carlsson31b7f522009-12-11 02:46:30 +0000116
Anders Carlsson8d145152009-12-20 22:30:54 +0000117 // Pointer type info flags.
118 enum {
119 /// PTI_Const - Type has const qualifier.
120 PTI_Const = 0x1,
121
122 /// PTI_Volatile - Type has volatile qualifier.
123 PTI_Volatile = 0x2,
124
125 /// PTI_Restrict - Type has restrict qualifier.
126 PTI_Restrict = 0x4,
127
128 /// PTI_Incomplete - Type is incomplete.
129 PTI_Incomplete = 0x8,
130
131 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
132 /// (in pointer to member).
133 PTI_ContainingClassIncomplete = 0x10
134 };
Anders Carlsson08148092009-12-30 23:47:56 +0000135
136 // VMI type info flags.
137 enum {
138 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
139 VMI_NonDiamondRepeat = 0x1,
140
141 /// VMI_DiamondShaped - Class is diamond shaped.
142 VMI_DiamondShaped = 0x2
143 };
144
145 // Base class type info flags.
146 enum {
147 /// BCTI_Virtual - Base class is virtual.
148 BCTI_Virtual = 0x1,
149
150 /// BCTI_Public - Base class is public.
151 BCTI_Public = 0x2
152 };
Anders Carlsson531d55f2009-12-31 17:43:53 +0000153
154 /// BuildTypeInfo - Build the RTTI type info struct for the given type.
John McCall9dffe6f2010-04-30 01:15:21 +0000155 ///
156 /// \param Force - true to force the creation of this RTTI value
157 /// \param ForEH - true if this is for exception handling
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000158 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false);
Mike Stump2b1bf312009-11-14 14:25:18 +0000159};
Mike Stump92f2fe22009-12-02 19:07:44 +0000160}
Mike Stump2b1bf312009-11-14 14:25:18 +0000161
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000162llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
163 // Mangle the RTTI name.
164 llvm::SmallString<256> OutName;
165 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
166 llvm::StringRef Name = OutName.str();
167
Anders Carlsson8d145152009-12-20 22:30:54 +0000168 // Look for an existing global.
169 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000170
171 if (!GV) {
172 // Create a new global variable.
173 GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
174 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000175 }
176
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000177 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000178}
179
Anders Carlsson8d145152009-12-20 22:30:54 +0000180/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
181/// info for that type is defined in the standard library.
182static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
183 // Itanium C++ ABI 2.9.2:
184 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
185 // the run-time support library. Specifically, the run-time support
186 // library should contain type_info objects for the types X, X* and
187 // X const*, for every X in: void, bool, wchar_t, char, unsigned char,
188 // signed char, short, unsigned short, int, unsigned int, long,
189 // unsigned long, long long, unsigned long long, float, double, long double,
190 // char16_t, char32_t, and the IEEE 754r decimal and half-precision
191 // floating point types.
192 switch (Ty->getKind()) {
193 case BuiltinType::Void:
194 case BuiltinType::Bool:
195 case BuiltinType::WChar:
196 case BuiltinType::Char_U:
197 case BuiltinType::Char_S:
198 case BuiltinType::UChar:
199 case BuiltinType::SChar:
200 case BuiltinType::Short:
201 case BuiltinType::UShort:
202 case BuiltinType::Int:
203 case BuiltinType::UInt:
204 case BuiltinType::Long:
205 case BuiltinType::ULong:
206 case BuiltinType::LongLong:
207 case BuiltinType::ULongLong:
208 case BuiltinType::Float:
209 case BuiltinType::Double:
210 case BuiltinType::LongDouble:
211 case BuiltinType::Char16:
212 case BuiltinType::Char32:
213 case BuiltinType::Int128:
214 case BuiltinType::UInt128:
215 return true;
216
217 case BuiltinType::Overload:
218 case BuiltinType::Dependent:
219 case BuiltinType::UndeducedAuto:
220 assert(false && "Should not see this type here!");
221
222 case BuiltinType::NullPtr:
223 assert(false && "FIXME: nullptr_t is not handled!");
224
225 case BuiltinType::ObjCId:
226 case BuiltinType::ObjCClass:
227 case BuiltinType::ObjCSel:
228 assert(false && "FIXME: Objective-C types are unsupported!");
229 }
230
231 // Silent gcc.
232 return false;
233}
234
235static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
236 QualType PointeeTy = PointerTy->getPointeeType();
237 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
238 if (!BuiltinTy)
239 return false;
240
241 // Check the qualifiers.
242 Qualifiers Quals = PointeeTy.getQualifiers();
243 Quals.removeConst();
244
245 if (!Quals.empty())
246 return false;
247
248 return TypeInfoIsInStandardLibrary(BuiltinTy);
249}
250
John McCallcbfe5022010-08-04 08:34:44 +0000251/// IsStandardLibraryRTTIDescriptor - Returns whether the type
252/// information for the given type exists in the standard library.
253static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000254 // Type info for builtin types is defined in the standard library.
255 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
256 return TypeInfoIsInStandardLibrary(BuiltinTy);
257
258 // Type info for some pointer types to builtin types is defined in the
259 // standard library.
260 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
261 return TypeInfoIsInStandardLibrary(PointerTy);
262
John McCallcbfe5022010-08-04 08:34:44 +0000263 return false;
264}
265
266/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
267/// the given type exists somewhere else, and that we should not emit the type
268/// information in this translation unit. Assumes that it is not a
269/// standard-library type.
270static bool ShouldUseExternalRTTIDescriptor(ASTContext &Context,
271 QualType Ty) {
John McCall9dffe6f2010-04-30 01:15:21 +0000272 // If RTTI is disabled, don't consider key functions.
273 if (!Context.getLangOptions().RTTI) return false;
274
Anders Carlsson8d145152009-12-20 22:30:54 +0000275 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000276 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000277 if (!RD->hasDefinition())
278 return false;
279
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000280 if (!RD->isDynamicClass())
281 return false;
282
283 // Get the key function.
284 const CXXMethodDecl *KeyFunction = RD->getASTContext().getKeyFunction(RD);
Argyrios Kyrtzidis4aedb1c2010-07-07 12:24:18 +0000285 if (KeyFunction && !KeyFunction->hasBody()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000286 // The class has a key function, but it is not defined in this translation
287 // unit, so we should use the external descriptor for it.
288 return true;
289 }
Anders Carlsson8d145152009-12-20 22:30:54 +0000290 }
291
292 return false;
293}
294
295/// IsIncompleteClassType - Returns whether the given record type is incomplete.
296static bool IsIncompleteClassType(const RecordType *RecordTy) {
297 return !RecordTy->getDecl()->isDefinition();
298}
299
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000300/// ContainsIncompleteClassType - Returns whether the given type contains an
301/// incomplete class type. This is true if
302///
303/// * The given type is an incomplete class type.
304/// * The given type is a pointer type whose pointee type contains an
305/// incomplete class type.
306/// * The given type is a member pointer type whose class is an incomplete
307/// class type.
308/// * The given type is a member pointer type whoise pointee type contains an
309/// incomplete class type.
Anders Carlsson8d145152009-12-20 22:30:54 +0000310/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000311static bool ContainsIncompleteClassType(QualType Ty) {
312 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
313 if (IsIncompleteClassType(RecordTy))
314 return true;
315 }
316
317 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
318 return ContainsIncompleteClassType(PointerTy->getPointeeType());
319
320 if (const MemberPointerType *MemberPointerTy =
321 dyn_cast<MemberPointerType>(Ty)) {
322 // Check if the class type is incomplete.
323 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
324 if (IsIncompleteClassType(ClassType))
325 return true;
326
327 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000328 }
329
330 return false;
331}
332
333/// getTypeInfoLinkage - Return the linkage that the type info and type info
334/// name constants should have for the given type.
335static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(QualType Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000336 // Itanium C++ ABI 2.9.5p7:
337 // In addition, it and all of the intermediate abi::__pointer_type_info
338 // structs in the chain down to the abi::__class_type_info for the
339 // incomplete class type must be prevented from resolving to the
340 // corresponding type_info structs for the complete class type, possibly
341 // by making them local static objects. Finally, a dummy class RTTI is
342 // generated for the incomplete type that will not resolve to the final
343 // complete class RTTI (because the latter need not exist), possibly by
344 // making it a local static object.
345 if (ContainsIncompleteClassType(Ty))
346 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000347
Douglas Gregor031b3712010-03-31 00:15:35 +0000348 switch (Ty->getLinkage()) {
349 case NoLinkage:
350 case InternalLinkage:
351 case UniqueExternalLinkage:
352 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson978ef682009-12-29 21:58:32 +0000353
Douglas Gregor031b3712010-03-31 00:15:35 +0000354 case ExternalLinkage:
355 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
356 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
357 if (RD->isDynamicClass())
Anders Carlsson046c2942010-04-17 20:15:18 +0000358 return CodeGenModule::getVTableLinkage(RD);
Mike Stumpc8f76f52009-12-24 01:10:27 +0000359 }
Douglas Gregor031b3712010-03-31 00:15:35 +0000360
Mike Stumpc8f76f52009-12-24 01:10:27 +0000361 return llvm::GlobalValue::WeakODRLinkage;
362 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000363
Anders Carlsson8d145152009-12-20 22:30:54 +0000364 return llvm::GlobalValue::WeakODRLinkage;
365}
366
Anders Carlssonf64531a2009-12-30 01:00:12 +0000367// CanUseSingleInheritance - Return whether the given record decl has a "single,
368// public, non-virtual base at offset zero (i.e. the derived class is dynamic
369// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
370static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
371 // Check the number of bases.
372 if (RD->getNumBases() != 1)
373 return false;
374
375 // Get the base.
376 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
377
378 // Check that the base is not virtual.
379 if (Base->isVirtual())
380 return false;
381
382 // Check that the base is public.
383 if (Base->getAccessSpecifier() != AS_public)
384 return false;
385
386 // Check that the class is dynamic iff the base is.
387 const CXXRecordDecl *BaseDecl =
388 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
389 if (!BaseDecl->isEmpty() &&
390 BaseDecl->isDynamicClass() != RD->isDynamicClass())
391 return false;
392
393 return true;
394}
395
Anders Carlsson046c2942010-04-17 20:15:18 +0000396void RTTIBuilder::BuildVTablePointer(const Type *Ty) {
John McCalle8dc53e2010-08-12 02:17:33 +0000397 // abi::__class_type_info.
398 static const char * const ClassTypeInfo =
399 "_ZTVN10__cxxabiv117__class_type_infoE";
400 // abi::__si_class_type_info.
401 static const char * const SIClassTypeInfo =
402 "_ZTVN10__cxxabiv120__si_class_type_infoE";
403 // abi::__vmi_class_type_info.
404 static const char * const VMIClassTypeInfo =
405 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
406
Eli Friedman1cf26f52010-08-11 20:41:51 +0000407 const char *VTableName = 0;
Anders Carlsson8d145152009-12-20 22:30:54 +0000408
409 switch (Ty->getTypeClass()) {
Eli Friedman1cf26f52010-08-11 20:41:51 +0000410#define TYPE(Class, Base)
411#define ABSTRACT_TYPE(Class, Base)
412#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
413#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
414#define DEPENDENT_TYPE(Class, Base) case Type::Class:
415#include "clang/AST/TypeNodes.def"
416 assert(false && "Non-canonical and dependent types shouldn't get here");
417
418 case Type::LValueReference:
419 case Type::RValueReference:
420 assert(false && "References shouldn't get here");
Anders Carlsson978ef682009-12-29 21:58:32 +0000421
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000422 case Type::Builtin:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000423 // GCC treats vector and complex types as fundamental types.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000424 case Type::Vector:
425 case Type::ExtVector:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000426 case Type::Complex:
427 // FIXME: GCC treats block pointers as fundamental types?!
428 case Type::BlockPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000429 // abi::__fundamental_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000430 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000431 break;
432
Anders Carlsson978ef682009-12-29 21:58:32 +0000433 case Type::ConstantArray:
434 case Type::IncompleteArray:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000435 case Type::VariableArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000436 // abi::__array_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000437 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000438 break;
439
440 case Type::FunctionNoProto:
441 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000442 // abi::__function_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000443 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000444 break;
445
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000446 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000447 // abi::__enum_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000448 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000449 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000450
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000451 case Type::Record: {
452 const CXXRecordDecl *RD =
453 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000454
John McCall86ff3082010-02-04 22:26:26 +0000455 if (!RD->hasDefinition() || !RD->getNumBases()) {
John McCalle8dc53e2010-08-12 02:17:33 +0000456 VTableName = ClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000457 } else if (CanUseSingleInheritance(RD)) {
John McCalle8dc53e2010-08-12 02:17:33 +0000458 VTableName = SIClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000459 } else {
John McCalle8dc53e2010-08-12 02:17:33 +0000460 VTableName = VMIClassTypeInfo;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000461 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000462
463 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000464 }
465
Eli Friedman1cf26f52010-08-11 20:41:51 +0000466 case Type::ObjCObject:
John McCalle8dc53e2010-08-12 02:17:33 +0000467 // Ignore protocol qualifiers.
468 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
469
470 // Handle id and Class.
471 if (isa<BuiltinType>(Ty)) {
472 VTableName = ClassTypeInfo;
473 break;
474 }
475
476 assert(isa<ObjCInterfaceType>(Ty));
477 // Fall through.
478
Eli Friedman1cf26f52010-08-11 20:41:51 +0000479 case Type::ObjCInterface:
John McCalle8dc53e2010-08-12 02:17:33 +0000480 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
481 VTableName = SIClassTypeInfo;
482 } else {
483 VTableName = ClassTypeInfo;
484 }
Eli Friedman1cf26f52010-08-11 20:41:51 +0000485 break;
486
John McCalle8dc53e2010-08-12 02:17:33 +0000487 case Type::ObjCObjectPointer:
Anders Carlsson8d145152009-12-20 22:30:54 +0000488 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000489 // abi::__pointer_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000490 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000491 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000492
Anders Carlsson8d145152009-12-20 22:30:54 +0000493 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000494 // abi::__pointer_to_member_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000495 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000496 break;
497 }
498
Anders Carlsson046c2942010-04-17 20:15:18 +0000499 llvm::Constant *VTable =
500 CGM.getModule().getOrInsertGlobal(VTableName, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000501
502 const llvm::Type *PtrDiffTy =
503 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
504
505 // The vtable address point is 2.
506 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Anders Carlsson046c2942010-04-17 20:15:18 +0000507 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, &Two, 1);
508 VTable = llvm::ConstantExpr::getBitCast(VTable, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000509
Anders Carlsson046c2942010-04-17 20:15:18 +0000510 Fields.push_back(VTable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000511}
512
John McCallcbfe5022010-08-04 08:34:44 +0000513llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000514 // We want to operate on the canonical type.
515 Ty = CGM.getContext().getCanonicalType(Ty);
516
517 // Check if we've already emitted an RTTI descriptor for this type.
518 llvm::SmallString<256> OutName;
519 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
520 llvm::StringRef Name = OutName.str();
521
522 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
523 if (OldGV && !OldGV->isDeclaration())
524 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
John McCallcbfe5022010-08-04 08:34:44 +0000525
Anders Carlsson8d145152009-12-20 22:30:54 +0000526 // Check if there is already an external RTTI descriptor for this type.
John McCallcbfe5022010-08-04 08:34:44 +0000527 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
528 if (!Force &&
529 (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM.getContext(), Ty)))
Anders Carlsson8d145152009-12-20 22:30:54 +0000530 return GetAddrOfExternalRTTIDescriptor(Ty);
531
John McCallcbfe5022010-08-04 08:34:44 +0000532 // Emit the standard library with external linkage.
533 llvm::GlobalVariable::LinkageTypes Linkage;
534 if (IsStdLib)
535 Linkage = llvm::GlobalValue::ExternalLinkage;
536 else
537 Linkage = getTypeInfoLinkage(Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +0000538
539 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000540 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000541
542 // And the name.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000543 Fields.push_back(BuildName(Ty, DecideHidden(Ty), Linkage));
John McCallcbfe5022010-08-04 08:34:44 +0000544
Anders Carlsson8d145152009-12-20 22:30:54 +0000545 switch (Ty->getTypeClass()) {
546 default: assert(false && "Unhandled type class!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000547
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000548 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000549 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000550 case Type::Vector:
551 case Type::ExtVector:
552 // Itanium C++ ABI 2.9.5p4:
553 // abi::__fundamental_type_info adds no data members to std::type_info.
554 break;
555
Anders Carlsson978ef682009-12-29 21:58:32 +0000556 case Type::ConstantArray:
557 case Type::IncompleteArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000558 // Itanium C++ ABI 2.9.5p5:
559 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000560 break;
561
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000562 case Type::FunctionNoProto:
563 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000564 // Itanium C++ ABI 2.9.5p5:
565 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000566 break;
567
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000568 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000569 // Itanium C++ ABI 2.9.5p5:
570 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000571 break;
572
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000573 case Type::Record: {
574 const CXXRecordDecl *RD =
575 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000576 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000577 // We don't need to emit any fields.
578 break;
579 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000580
Anders Carlsson08148092009-12-30 23:47:56 +0000581 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000582 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000583 else
584 BuildVMIClassTypeInfo(RD);
585
586 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000587 }
John McCalle8dc53e2010-08-12 02:17:33 +0000588
589 case Type::ObjCObject:
590 case Type::ObjCInterface:
591 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
592 break;
593
594 case Type::ObjCObjectPointer:
595 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
596 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000597
Anders Carlsson8d145152009-12-20 22:30:54 +0000598 case Type::Pointer:
John McCalle8dc53e2010-08-12 02:17:33 +0000599 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000600 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000601
Anders Carlsson8d145152009-12-20 22:30:54 +0000602 case Type::MemberPointer:
603 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
604 break;
605 }
606
607 llvm::Constant *Init =
Anders Carlsson531d55f2009-12-31 17:43:53 +0000608 llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
Anders Carlsson8d145152009-12-20 22:30:54 +0000609 /*Packed=*/false);
610
611 llvm::GlobalVariable *GV =
612 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
613 /*Constant=*/true, Linkage, Init, Name);
614
615 // If there's already an old global variable, replace it with the new one.
616 if (OldGV) {
617 GV->takeName(OldGV);
618 llvm::Constant *NewPtr =
619 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
620 OldGV->replaceAllUsesWith(NewPtr);
621 OldGV->eraseFromParent();
622 }
John McCallcbfe5022010-08-04 08:34:44 +0000623
624 // GCC only relies on the uniqueness of the type names, not the
625 // type_infos themselves, so we can emit these as hidden symbols.
626 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
627 CGM.setTypeVisibility(GV, cast<CXXRecordDecl>(RT->getDecl()),
628 /*ForRTTI*/ true);
629 else if (Linkage == llvm::GlobalValue::WeakODRLinkage)
630 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
631
Anders Carlsson8d145152009-12-20 22:30:54 +0000632 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
633}
634
Anders Carlsson08148092009-12-30 23:47:56 +0000635/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000636/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000637static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000638 unsigned Flags = 0;
639
640 if (Quals.hasConst())
641 Flags |= RTTIBuilder::PTI_Const;
642 if (Quals.hasVolatile())
643 Flags |= RTTIBuilder::PTI_Volatile;
644 if (Quals.hasRestrict())
645 Flags |= RTTIBuilder::PTI_Restrict;
646
647 return Flags;
648}
649
John McCalle8dc53e2010-08-12 02:17:33 +0000650/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
651/// for the given Objective-C object type.
652void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
653 // Drop qualifiers.
654 const Type *T = OT->getBaseType().getTypePtr();
655 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
656
657 // The builtin types are abi::__class_type_infos and don't require
658 // extra fields.
659 if (isa<BuiltinType>(T)) return;
660
661 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
662 ObjCInterfaceDecl *Super = Class->getSuperClass();
663
664 // Root classes are also __class_type_info.
665 if (!Super) return;
666
667 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
668
669 // Everything else is single inheritance.
670 llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
671 Fields.push_back(BaseTypeInfo);
672}
673
Anders Carlssonf64531a2009-12-30 01:00:12 +0000674/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
675/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
676void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
677 // Itanium C++ ABI 2.9.5p6b:
678 // It adds to abi::__class_type_info a single member pointing to the
679 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000680 llvm::Constant *BaseTypeInfo =
681 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
682 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000683}
684
Anders Carlsson08148092009-12-30 23:47:56 +0000685/// SeenBases - Contains virtual and non-virtual bases seen when traversing
686/// a class hierarchy.
687struct SeenBases {
688 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
689 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
690};
691
692/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
693/// abi::__vmi_class_type_info.
694///
695static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
696 SeenBases &Bases) {
697
698 unsigned Flags = 0;
699
700 const CXXRecordDecl *BaseDecl =
701 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
702
703 if (Base->isVirtual()) {
704 if (Bases.VirtualBases.count(BaseDecl)) {
705 // If this virtual base has been seen before, then the class is diamond
706 // shaped.
707 Flags |= RTTIBuilder::VMI_DiamondShaped;
708 } else {
709 if (Bases.NonVirtualBases.count(BaseDecl))
710 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
711
712 // Mark the virtual base as seen.
713 Bases.VirtualBases.insert(BaseDecl);
714 }
715 } else {
716 if (Bases.NonVirtualBases.count(BaseDecl)) {
717 // If this non-virtual base has been seen before, then the class has non-
718 // diamond shaped repeated inheritance.
719 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
720 } else {
721 if (Bases.VirtualBases.count(BaseDecl))
722 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
723
724 // Mark the non-virtual base as seen.
725 Bases.NonVirtualBases.insert(BaseDecl);
726 }
727 }
728
729 // Walk all bases.
730 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
731 E = BaseDecl->bases_end(); I != E; ++I)
732 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
733
734 return Flags;
735}
736
737static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
738 unsigned Flags = 0;
739 SeenBases Bases;
740
741 // Walk all bases.
742 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
743 E = RD->bases_end(); I != E; ++I)
744 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
745
746 return Flags;
747}
748
749/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
750/// classes with bases that do not satisfy the abi::__si_class_type_info
751/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
752void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
753 const llvm::Type *UnsignedIntLTy =
754 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
755
756 // Itanium C++ ABI 2.9.5p6c:
757 // __flags is a word with flags describing details about the class
758 // structure, which may be referenced by using the __flags_masks
759 // enumeration. These flags refer to both direct and indirect bases.
760 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000761 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000762
763 // Itanium C++ ABI 2.9.5p6c:
764 // __base_count is a word with the number of direct proper base class
765 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000766 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000767
768 if (!RD->getNumBases())
769 return;
770
771 const llvm::Type *LongLTy =
772 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
773
774 // Now add the base class descriptions.
775
776 // Itanium C++ ABI 2.9.5p6c:
777 // __base_info[] is an array of base class descriptions -- one for every
778 // direct proper base. Each description is of the type:
779 //
780 // struct abi::__base_class_type_info {
781 // public:
782 // const __class_type_info *__base_type;
783 // long __offset_flags;
784 //
785 // enum __offset_flags_masks {
786 // __virtual_mask = 0x1,
787 // __public_mask = 0x2,
788 // __offset_shift = 8
789 // };
790 // };
791 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
792 E = RD->bases_end(); I != E; ++I) {
793 const CXXBaseSpecifier *Base = I;
794
795 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000796 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000797
798 const CXXRecordDecl *BaseDecl =
799 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
800
801 int64_t OffsetFlags = 0;
802
803 // All but the lower 8 bits of __offset_flags are a signed offset.
804 // For a non-virtual base, this is the offset in the object of the base
805 // subobject. For a virtual base, this is the offset in the virtual table of
806 // the virtual base offset for the virtual base referenced (negative).
807 if (Base->isVirtual())
Anders Carlssonaf440352010-03-23 04:11:45 +0000808 OffsetFlags = CGM.getVTables().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000809 else {
810 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
811 OffsetFlags = Layout.getBaseClassOffset(BaseDecl) / 8;
812 };
813
814 OffsetFlags <<= 8;
815
816 // The low-order byte of __offset_flags contains flags, as given by the
817 // masks from the enumeration __offset_flags_masks.
818 if (Base->isVirtual())
819 OffsetFlags |= BCTI_Virtual;
820 if (Base->getAccessSpecifier() == AS_public)
821 OffsetFlags |= BCTI_Public;
822
Anders Carlsson531d55f2009-12-31 17:43:53 +0000823 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000824 }
825}
826
Anders Carlsson8d145152009-12-20 22:30:54 +0000827/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
828/// used for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +0000829void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
Anders Carlssonabd6b092010-06-02 15:44:35 +0000830 Qualifiers Quals;
831 QualType UnqualifiedPointeeTy =
832 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
833
Anders Carlsson8d145152009-12-20 22:30:54 +0000834 // Itanium C++ ABI 2.9.5p7:
835 // __flags is a flag word describing the cv-qualification and other
836 // attributes of the type pointed to
Anders Carlssonabd6b092010-06-02 15:44:35 +0000837 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000838
839 // Itanium C++ ABI 2.9.5p7:
840 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
841 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000842 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000843 Flags |= PTI_Incomplete;
844
845 const llvm::Type *UnsignedIntLTy =
846 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000847 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000848
849 // Itanium C++ ABI 2.9.5p7:
850 // __pointee is a pointer to the std::type_info derivation for the
851 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000852 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000853 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000854 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000855}
856
857/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
858/// struct, used for member pointer types.
859void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
860 QualType PointeeTy = Ty->getPointeeType();
861
Anders Carlssonabd6b092010-06-02 15:44:35 +0000862 Qualifiers Quals;
863 QualType UnqualifiedPointeeTy =
864 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
865
Anders Carlsson8d145152009-12-20 22:30:54 +0000866 // Itanium C++ ABI 2.9.5p7:
867 // __flags is a flag word describing the cv-qualification and other
868 // attributes of the type pointed to.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000869 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000870
871 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000872
873 // Itanium C++ ABI 2.9.5p7:
874 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
875 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000876 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000877 Flags |= PTI_Incomplete;
878
Anders Carlsson8d145152009-12-20 22:30:54 +0000879 if (IsIncompleteClassType(ClassType))
880 Flags |= PTI_ContainingClassIncomplete;
881
Anders Carlsson8d145152009-12-20 22:30:54 +0000882 const llvm::Type *UnsignedIntLTy =
883 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000884 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000885
886 // Itanium C++ ABI 2.9.5p7:
887 // __pointee is a pointer to the std::type_info derivation for the
888 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000889 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000890 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000891 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000892
893 // Itanium C++ ABI 2.9.5p9:
894 // __context is a pointer to an abi::__class_type_info corresponding to the
895 // class type containing the member pointed to
896 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000897 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000898}
899
John McCall9dffe6f2010-04-30 01:15:21 +0000900llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
901 bool ForEH) {
902 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
903 // FIXME: should we even be calling this method if RTTI is disabled
904 // and it's not for EH?
905 if (!ForEH && !getContext().getLangOptions().RTTI) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000906 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
907 return llvm::Constant::getNullValue(Int8PtrTy);
908 }
John McCall9dffe6f2010-04-30 01:15:21 +0000909
Anders Carlsson531d55f2009-12-31 17:43:53 +0000910 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000911}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000912
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000913void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
914 QualType PointerType = Context.getPointerType(Type);
915 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
916 RTTIBuilder(*this).BuildTypeInfo(Type, true);
917 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
918 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
919}
920
921void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000922 QualType FundamentalTypes[] = { Context.VoidTy, Context.Char32Ty,
923 Context.Char16Ty, Context.UnsignedLongLongTy,
924 Context.LongLongTy, Context.WCharTy,
925 Context.UnsignedShortTy, Context.ShortTy,
926 Context.UnsignedLongTy, Context.LongTy,
927 Context.UnsignedIntTy, Context.IntTy,
928 Context.UnsignedCharTy, Context.FloatTy,
929 Context.LongDoubleTy, Context.DoubleTy,
930 Context.CharTy, Context.BoolTy,
931 Context.SignedCharTy };
932 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
933 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
934}