blob: 914ace850d7da0dbb78f098124066ae71020ea65 [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 McCall279b5eb2010-08-12 23:36:15 +000015#include "clang/AST/RecordLayout.h"
16#include "clang/AST/Type.h"
17#include "clang/Frontend/CodeGenOptions.h"
18
Anders Carlsson656e4c12009-10-10 20:49:04 +000019using namespace clang;
20using namespace CodeGen;
21
Mike Stump92f2fe22009-12-02 19:07:44 +000022namespace {
Mike Stumpde050572009-12-02 18:57:08 +000023class RTTIBuilder {
Mike Stump2b1bf312009-11-14 14:25:18 +000024 CodeGenModule &CGM; // Per-module state.
25 llvm::LLVMContext &VMContext;
Anders Carlsson8d145152009-12-20 22:30:54 +000026
Anders Carlsson08148092009-12-30 23:47:56 +000027 const llvm::Type *Int8PtrTy;
Anders Carlsson531d55f2009-12-31 17:43:53 +000028
29 /// Fields - The fields of the RTTI descriptor currently being built.
30 llvm::SmallVector<llvm::Constant *, 16> Fields;
Anders Carlssond6baec82009-12-11 01:27:37 +000031
Anders Carlsson1d7088d2009-12-17 07:09:17 +000032 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
33 /// descriptor of the given type.
34 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
35
Anders Carlsson046c2942010-04-17 20:15:18 +000036 /// BuildVTablePointer - Build the vtable pointer for the given type.
37 void BuildVTablePointer(const Type *Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +000038
Anders Carlssonf64531a2009-12-30 01:00:12 +000039 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
Anders Carlsson08148092009-12-30 23:47:56 +000040 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
Anders Carlssonf64531a2009-12-30 01:00:12 +000041 void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
42
Anders Carlsson08148092009-12-30 23:47:56 +000043 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
44 /// classes with bases that do not satisfy the abi::__si_class_type_info
45 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
46 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
47
Anders Carlssonf64531a2009-12-30 01:00:12 +000048 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
49 /// for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +000050 void BuildPointerTypeInfo(QualType PointeeTy);
51
52 /// BuildObjCObjectTypeInfo - Build the appropriate kind of
53 /// type_info for an object type.
54 void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +000055
56 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
57 /// struct, used for member pointer types.
58 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
59
Mike Stump2b1bf312009-11-14 14:25:18 +000060public:
Mike Stumpde050572009-12-02 18:57:08 +000061 RTTIBuilder(CodeGenModule &cgm)
Mike Stump2b1bf312009-11-14 14:25:18 +000062 : CGM(cgm), VMContext(cgm.getModule().getContext()),
63 Int8PtrTy(llvm::Type::getInt8PtrTy(VMContext)) { }
64
Anders Carlsson31b7f522009-12-11 02:46:30 +000065 llvm::Constant *BuildName(QualType Ty, bool Hidden,
66 llvm::GlobalVariable::LinkageTypes Linkage) {
Mike Stump2b1bf312009-11-14 14:25:18 +000067 llvm::SmallString<256> OutName;
Mike Stumpde050572009-12-02 18:57:08 +000068 CGM.getMangleContext().mangleCXXRTTIName(Ty, OutName);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +000069 llvm::StringRef Name = OutName.str();
Mike Stumpcbcd4e52009-11-14 23:32:21 +000070
Anders Carlsson8d145152009-12-20 22:30:54 +000071 llvm::GlobalVariable *OGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +000072 if (OGV && !OGV->isDeclaration())
73 return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy);
Mike Stump58588942009-11-19 01:08:19 +000074
Anders Carlsson31b7f522009-12-11 02:46:30 +000075 llvm::Constant *C = llvm::ConstantArray::get(VMContext, Name.substr(4));
Mike Stump2b1bf312009-11-14 14:25:18 +000076
Anders Carlsson31b7f522009-12-11 02:46:30 +000077 llvm::GlobalVariable *GV =
78 new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
79 C, Name);
Mike Stump58588942009-11-19 01:08:19 +000080 if (OGV) {
81 GV->takeName(OGV);
82 llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
83 OGV->getType());
84 OGV->replaceAllUsesWith(NewPtr);
85 OGV->eraseFromParent();
86 }
Mike Stump582b0372009-11-18 03:46:51 +000087 if (Hidden)
88 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
89 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Daniel Dunbar7177dee2009-12-19 17:50:07 +000090 }
Mike Stumpc7a05bd2009-11-14 15:55:18 +000091
Mike Stump4e6f8ee2009-12-24 02:33:48 +000092 // FIXME: unify with DecideExtern
Mike Stump58588942009-11-19 01:08:19 +000093 bool DecideHidden(QualType Ty) {
94 // For this type, see if all components are never hidden.
95 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>())
96 return (DecideHidden(MPT->getPointeeType())
97 && DecideHidden(QualType(MPT->getClass(), 0)));
98 if (const PointerType *PT = Ty->getAs<PointerType>())
99 return DecideHidden(PT->getPointeeType());
Mike Stump4e6f8ee2009-12-24 02:33:48 +0000100 if (const FunctionType *FT = Ty->getAs<FunctionType>()) {
101 if (DecideHidden(FT->getResultType()) == false)
102 return false;
103 if (const FunctionProtoType *FPT = Ty->getAs<FunctionProtoType>()) {
104 for (unsigned i = 0; i <FPT->getNumArgs(); ++i)
105 if (DecideHidden(FPT->getArgType(i)) == false)
106 return false;
107 for (unsigned i = 0; i <FPT->getNumExceptions(); ++i)
108 if (DecideHidden(FPT->getExceptionType(i)) == false)
109 return false;
110 return true;
111 }
112 }
Mike Stump58588942009-11-19 01:08:19 +0000113 if (const RecordType *RT = Ty->getAs<RecordType>())
114 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
115 return CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden;
116 return false;
117 }
Anders Carlsson31b7f522009-12-11 02:46:30 +0000118
Anders Carlsson8d145152009-12-20 22:30:54 +0000119 // Pointer type info flags.
120 enum {
121 /// PTI_Const - Type has const qualifier.
122 PTI_Const = 0x1,
123
124 /// PTI_Volatile - Type has volatile qualifier.
125 PTI_Volatile = 0x2,
126
127 /// PTI_Restrict - Type has restrict qualifier.
128 PTI_Restrict = 0x4,
129
130 /// PTI_Incomplete - Type is incomplete.
131 PTI_Incomplete = 0x8,
132
133 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
134 /// (in pointer to member).
135 PTI_ContainingClassIncomplete = 0x10
136 };
Anders Carlsson08148092009-12-30 23:47:56 +0000137
138 // VMI type info flags.
139 enum {
140 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
141 VMI_NonDiamondRepeat = 0x1,
142
143 /// VMI_DiamondShaped - Class is diamond shaped.
144 VMI_DiamondShaped = 0x2
145 };
146
147 // Base class type info flags.
148 enum {
149 /// BCTI_Virtual - Base class is virtual.
150 BCTI_Virtual = 0x1,
151
152 /// BCTI_Public - Base class is public.
153 BCTI_Public = 0x2
154 };
Anders Carlsson531d55f2009-12-31 17:43:53 +0000155
156 /// BuildTypeInfo - Build the RTTI type info struct for the given type.
John McCall9dffe6f2010-04-30 01:15:21 +0000157 ///
158 /// \param Force - true to force the creation of this RTTI value
159 /// \param ForEH - true if this is for exception handling
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000160 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false);
Mike Stump2b1bf312009-11-14 14:25:18 +0000161};
Mike Stump92f2fe22009-12-02 19:07:44 +0000162}
Mike Stump2b1bf312009-11-14 14:25:18 +0000163
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000164llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
165 // Mangle the RTTI name.
166 llvm::SmallString<256> OutName;
167 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
168 llvm::StringRef Name = OutName.str();
169
Anders Carlsson8d145152009-12-20 22:30:54 +0000170 // Look for an existing global.
171 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000172
173 if (!GV) {
174 // Create a new global variable.
175 GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
176 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000177 }
178
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000179 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000180}
181
Anders Carlsson8d145152009-12-20 22:30:54 +0000182/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
183/// info for that type is defined in the standard library.
184static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
185 // Itanium C++ ABI 2.9.2:
186 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
187 // the run-time support library. Specifically, the run-time support
188 // library should contain type_info objects for the types X, X* and
189 // X const*, for every X in: void, bool, wchar_t, char, unsigned char,
190 // signed char, short, unsigned short, int, unsigned int, long,
191 // unsigned long, long long, unsigned long long, float, double, long double,
192 // char16_t, char32_t, and the IEEE 754r decimal and half-precision
193 // floating point types.
194 switch (Ty->getKind()) {
195 case BuiltinType::Void:
196 case BuiltinType::Bool:
197 case BuiltinType::WChar:
198 case BuiltinType::Char_U:
199 case BuiltinType::Char_S:
200 case BuiltinType::UChar:
201 case BuiltinType::SChar:
202 case BuiltinType::Short:
203 case BuiltinType::UShort:
204 case BuiltinType::Int:
205 case BuiltinType::UInt:
206 case BuiltinType::Long:
207 case BuiltinType::ULong:
208 case BuiltinType::LongLong:
209 case BuiltinType::ULongLong:
210 case BuiltinType::Float:
211 case BuiltinType::Double:
212 case BuiltinType::LongDouble:
213 case BuiltinType::Char16:
214 case BuiltinType::Char32:
215 case BuiltinType::Int128:
216 case BuiltinType::UInt128:
217 return true;
218
219 case BuiltinType::Overload:
220 case BuiltinType::Dependent:
221 case BuiltinType::UndeducedAuto:
222 assert(false && "Should not see this type here!");
223
224 case BuiltinType::NullPtr:
225 assert(false && "FIXME: nullptr_t is not handled!");
226
227 case BuiltinType::ObjCId:
228 case BuiltinType::ObjCClass:
229 case BuiltinType::ObjCSel:
230 assert(false && "FIXME: Objective-C types are unsupported!");
231 }
232
233 // Silent gcc.
234 return false;
235}
236
237static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
238 QualType PointeeTy = PointerTy->getPointeeType();
239 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
240 if (!BuiltinTy)
241 return false;
242
243 // Check the qualifiers.
244 Qualifiers Quals = PointeeTy.getQualifiers();
245 Quals.removeConst();
246
247 if (!Quals.empty())
248 return false;
249
250 return TypeInfoIsInStandardLibrary(BuiltinTy);
251}
252
John McCallcbfe5022010-08-04 08:34:44 +0000253/// IsStandardLibraryRTTIDescriptor - Returns whether the type
254/// information for the given type exists in the standard library.
255static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000256 // Type info for builtin types is defined in the standard library.
257 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
258 return TypeInfoIsInStandardLibrary(BuiltinTy);
259
260 // Type info for some pointer types to builtin types is defined in the
261 // standard library.
262 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
263 return TypeInfoIsInStandardLibrary(PointerTy);
264
John McCallcbfe5022010-08-04 08:34:44 +0000265 return false;
266}
267
268/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
269/// the given type exists somewhere else, and that we should not emit the type
270/// information in this translation unit. Assumes that it is not a
271/// standard-library type.
272static bool ShouldUseExternalRTTIDescriptor(ASTContext &Context,
273 QualType Ty) {
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
285 // Get the key function.
286 const CXXMethodDecl *KeyFunction = RD->getASTContext().getKeyFunction(RD);
Argyrios Kyrtzidis4aedb1c2010-07-07 12:24:18 +0000287 if (KeyFunction && !KeyFunction->hasBody()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000288 // The class has a key function, but it is not defined in this translation
289 // unit, so we should use the external descriptor for it.
290 return true;
291 }
Anders Carlsson8d145152009-12-20 22:30:54 +0000292 }
293
294 return false;
295}
296
297/// IsIncompleteClassType - Returns whether the given record type is incomplete.
298static bool IsIncompleteClassType(const RecordType *RecordTy) {
299 return !RecordTy->getDecl()->isDefinition();
300}
301
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000302/// ContainsIncompleteClassType - Returns whether the given type contains an
303/// incomplete class type. This is true if
304///
305/// * The given type is an incomplete class type.
306/// * The given type is a pointer type whose pointee type contains an
307/// incomplete class type.
308/// * The given type is a member pointer type whose class is an incomplete
309/// class type.
310/// * The given type is a member pointer type whoise pointee type contains an
311/// incomplete class type.
Anders Carlsson8d145152009-12-20 22:30:54 +0000312/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000313static bool ContainsIncompleteClassType(QualType Ty) {
314 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
315 if (IsIncompleteClassType(RecordTy))
316 return true;
317 }
318
319 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
320 return ContainsIncompleteClassType(PointerTy->getPointeeType());
321
322 if (const MemberPointerType *MemberPointerTy =
323 dyn_cast<MemberPointerType>(Ty)) {
324 // Check if the class type is incomplete.
325 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
326 if (IsIncompleteClassType(ClassType))
327 return true;
328
329 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000330 }
331
332 return false;
333}
334
335/// getTypeInfoLinkage - Return the linkage that the type info and type info
336/// name constants should have for the given type.
337static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(QualType Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000338 // Itanium C++ ABI 2.9.5p7:
339 // In addition, it and all of the intermediate abi::__pointer_type_info
340 // structs in the chain down to the abi::__class_type_info for the
341 // incomplete class type must be prevented from resolving to the
342 // corresponding type_info structs for the complete class type, possibly
343 // by making them local static objects. Finally, a dummy class RTTI is
344 // generated for the incomplete type that will not resolve to the final
345 // complete class RTTI (because the latter need not exist), possibly by
346 // making it a local static object.
347 if (ContainsIncompleteClassType(Ty))
348 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000349
Douglas Gregor031b3712010-03-31 00:15:35 +0000350 switch (Ty->getLinkage()) {
351 case NoLinkage:
352 case InternalLinkage:
353 case UniqueExternalLinkage:
354 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson978ef682009-12-29 21:58:32 +0000355
Douglas Gregor031b3712010-03-31 00:15:35 +0000356 case ExternalLinkage:
357 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
358 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
359 if (RD->isDynamicClass())
Anders Carlsson046c2942010-04-17 20:15:18 +0000360 return CodeGenModule::getVTableLinkage(RD);
Mike Stumpc8f76f52009-12-24 01:10:27 +0000361 }
Douglas Gregor031b3712010-03-31 00:15:35 +0000362
Mike Stumpc8f76f52009-12-24 01:10:27 +0000363 return llvm::GlobalValue::WeakODRLinkage;
364 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000365
Anders Carlsson8d145152009-12-20 22:30:54 +0000366 return llvm::GlobalValue::WeakODRLinkage;
367}
368
Anders Carlssonf64531a2009-12-30 01:00:12 +0000369// CanUseSingleInheritance - Return whether the given record decl has a "single,
370// public, non-virtual base at offset zero (i.e. the derived class is dynamic
371// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
372static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
373 // Check the number of bases.
374 if (RD->getNumBases() != 1)
375 return false;
376
377 // Get the base.
378 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
379
380 // Check that the base is not virtual.
381 if (Base->isVirtual())
382 return false;
383
384 // Check that the base is public.
385 if (Base->getAccessSpecifier() != AS_public)
386 return false;
387
388 // Check that the class is dynamic iff the base is.
389 const CXXRecordDecl *BaseDecl =
390 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
391 if (!BaseDecl->isEmpty() &&
392 BaseDecl->isDynamicClass() != RD->isDynamicClass())
393 return false;
394
395 return true;
396}
397
Anders Carlsson046c2942010-04-17 20:15:18 +0000398void RTTIBuilder::BuildVTablePointer(const Type *Ty) {
John McCalle8dc53e2010-08-12 02:17:33 +0000399 // abi::__class_type_info.
400 static const char * const ClassTypeInfo =
401 "_ZTVN10__cxxabiv117__class_type_infoE";
402 // abi::__si_class_type_info.
403 static const char * const SIClassTypeInfo =
404 "_ZTVN10__cxxabiv120__si_class_type_infoE";
405 // abi::__vmi_class_type_info.
406 static const char * const VMIClassTypeInfo =
407 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
408
Eli Friedman1cf26f52010-08-11 20:41:51 +0000409 const char *VTableName = 0;
Anders Carlsson8d145152009-12-20 22:30:54 +0000410
411 switch (Ty->getTypeClass()) {
Eli Friedman1cf26f52010-08-11 20:41:51 +0000412#define TYPE(Class, Base)
413#define ABSTRACT_TYPE(Class, Base)
414#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
415#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
416#define DEPENDENT_TYPE(Class, Base) case Type::Class:
417#include "clang/AST/TypeNodes.def"
418 assert(false && "Non-canonical and dependent types shouldn't get here");
419
420 case Type::LValueReference:
421 case Type::RValueReference:
422 assert(false && "References shouldn't get here");
Anders Carlsson978ef682009-12-29 21:58:32 +0000423
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000424 case Type::Builtin:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000425 // GCC treats vector and complex types as fundamental types.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000426 case Type::Vector:
427 case Type::ExtVector:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000428 case Type::Complex:
429 // FIXME: GCC treats block pointers as fundamental types?!
430 case Type::BlockPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000431 // abi::__fundamental_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000432 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000433 break;
434
Anders Carlsson978ef682009-12-29 21:58:32 +0000435 case Type::ConstantArray:
436 case Type::IncompleteArray:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000437 case Type::VariableArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000438 // abi::__array_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000439 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000440 break;
441
442 case Type::FunctionNoProto:
443 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000444 // abi::__function_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000445 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000446 break;
447
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000448 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000449 // abi::__enum_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000450 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000451 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000452
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000453 case Type::Record: {
454 const CXXRecordDecl *RD =
455 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000456
John McCall86ff3082010-02-04 22:26:26 +0000457 if (!RD->hasDefinition() || !RD->getNumBases()) {
John McCalle8dc53e2010-08-12 02:17:33 +0000458 VTableName = ClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000459 } else if (CanUseSingleInheritance(RD)) {
John McCalle8dc53e2010-08-12 02:17:33 +0000460 VTableName = SIClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000461 } else {
John McCalle8dc53e2010-08-12 02:17:33 +0000462 VTableName = VMIClassTypeInfo;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000463 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000464
465 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000466 }
467
Eli Friedman1cf26f52010-08-11 20:41:51 +0000468 case Type::ObjCObject:
John McCalle8dc53e2010-08-12 02:17:33 +0000469 // Ignore protocol qualifiers.
470 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
471
472 // Handle id and Class.
473 if (isa<BuiltinType>(Ty)) {
474 VTableName = ClassTypeInfo;
475 break;
476 }
477
478 assert(isa<ObjCInterfaceType>(Ty));
479 // Fall through.
480
Eli Friedman1cf26f52010-08-11 20:41:51 +0000481 case Type::ObjCInterface:
John McCalle8dc53e2010-08-12 02:17:33 +0000482 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
483 VTableName = SIClassTypeInfo;
484 } else {
485 VTableName = ClassTypeInfo;
486 }
Eli Friedman1cf26f52010-08-11 20:41:51 +0000487 break;
488
John McCalle8dc53e2010-08-12 02:17:33 +0000489 case Type::ObjCObjectPointer:
Anders Carlsson8d145152009-12-20 22:30:54 +0000490 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000491 // abi::__pointer_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000492 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000493 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000494
Anders Carlsson8d145152009-12-20 22:30:54 +0000495 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000496 // abi::__pointer_to_member_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000497 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000498 break;
499 }
500
Anders Carlsson046c2942010-04-17 20:15:18 +0000501 llvm::Constant *VTable =
502 CGM.getModule().getOrInsertGlobal(VTableName, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000503
504 const llvm::Type *PtrDiffTy =
505 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
506
507 // The vtable address point is 2.
508 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Anders Carlsson046c2942010-04-17 20:15:18 +0000509 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, &Two, 1);
510 VTable = llvm::ConstantExpr::getBitCast(VTable, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000511
Anders Carlsson046c2942010-04-17 20:15:18 +0000512 Fields.push_back(VTable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000513}
514
John McCallcbfe5022010-08-04 08:34:44 +0000515llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000516 // We want to operate on the canonical type.
517 Ty = CGM.getContext().getCanonicalType(Ty);
518
519 // Check if we've already emitted an RTTI descriptor for this type.
520 llvm::SmallString<256> OutName;
521 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
522 llvm::StringRef Name = OutName.str();
523
524 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
525 if (OldGV && !OldGV->isDeclaration())
526 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
John McCallcbfe5022010-08-04 08:34:44 +0000527
Anders Carlsson8d145152009-12-20 22:30:54 +0000528 // Check if there is already an external RTTI descriptor for this type.
John McCallcbfe5022010-08-04 08:34:44 +0000529 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
530 if (!Force &&
531 (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM.getContext(), Ty)))
Anders Carlsson8d145152009-12-20 22:30:54 +0000532 return GetAddrOfExternalRTTIDescriptor(Ty);
533
John McCallcbfe5022010-08-04 08:34:44 +0000534 // Emit the standard library with external linkage.
535 llvm::GlobalVariable::LinkageTypes Linkage;
536 if (IsStdLib)
537 Linkage = llvm::GlobalValue::ExternalLinkage;
538 else
539 Linkage = getTypeInfoLinkage(Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +0000540
541 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000542 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000543
544 // And the name.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000545 Fields.push_back(BuildName(Ty, DecideHidden(Ty), Linkage));
John McCallcbfe5022010-08-04 08:34:44 +0000546
Anders Carlsson8d145152009-12-20 22:30:54 +0000547 switch (Ty->getTypeClass()) {
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000548#define TYPE(Class, Base)
549#define ABSTRACT_TYPE(Class, Base)
550#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
551#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
552#define DEPENDENT_TYPE(Class, Base) case Type::Class:
553#include "clang/AST/TypeNodes.def"
554 assert(false && "Non-canonical and dependent types shouldn't get here");
Anders Carlsson8d145152009-12-20 22:30:54 +0000555
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000556 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000557 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000558 case Type::Vector:
559 case Type::ExtVector:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000560 case Type::Complex:
561 case Type::BlockPointer:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000562 // Itanium C++ ABI 2.9.5p4:
563 // abi::__fundamental_type_info adds no data members to std::type_info.
564 break;
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000565
566 case Type::LValueReference:
567 case Type::RValueReference:
568 assert(false && "References shouldn't get here");
569
Anders Carlsson978ef682009-12-29 21:58:32 +0000570 case Type::ConstantArray:
571 case Type::IncompleteArray:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000572 case Type::VariableArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000573 // Itanium C++ ABI 2.9.5p5:
574 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000575 break;
576
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000577 case Type::FunctionNoProto:
578 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000579 // Itanium C++ ABI 2.9.5p5:
580 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000581 break;
582
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000583 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000584 // Itanium C++ ABI 2.9.5p5:
585 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000586 break;
587
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000588 case Type::Record: {
589 const CXXRecordDecl *RD =
590 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000591 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000592 // We don't need to emit any fields.
593 break;
594 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000595
Anders Carlsson08148092009-12-30 23:47:56 +0000596 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000597 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000598 else
599 BuildVMIClassTypeInfo(RD);
600
601 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000602 }
John McCalle8dc53e2010-08-12 02:17:33 +0000603
604 case Type::ObjCObject:
605 case Type::ObjCInterface:
606 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
607 break;
608
609 case Type::ObjCObjectPointer:
610 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
611 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000612
Anders Carlsson8d145152009-12-20 22:30:54 +0000613 case Type::Pointer:
John McCalle8dc53e2010-08-12 02:17:33 +0000614 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000615 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000616
Anders Carlsson8d145152009-12-20 22:30:54 +0000617 case Type::MemberPointer:
618 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
619 break;
620 }
621
622 llvm::Constant *Init =
Anders Carlsson531d55f2009-12-31 17:43:53 +0000623 llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
Anders Carlsson8d145152009-12-20 22:30:54 +0000624 /*Packed=*/false);
625
626 llvm::GlobalVariable *GV =
627 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
628 /*Constant=*/true, Linkage, Init, Name);
629
630 // If there's already an old global variable, replace it with the new one.
631 if (OldGV) {
632 GV->takeName(OldGV);
633 llvm::Constant *NewPtr =
634 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
635 OldGV->replaceAllUsesWith(NewPtr);
636 OldGV->eraseFromParent();
637 }
John McCallcbfe5022010-08-04 08:34:44 +0000638
639 // GCC only relies on the uniqueness of the type names, not the
640 // type_infos themselves, so we can emit these as hidden symbols.
John McCall279b5eb2010-08-12 23:36:15 +0000641 // But don't do this if we're worried about strict visibility
642 // compatibility.
John McCallcbfe5022010-08-04 08:34:44 +0000643 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
644 CGM.setTypeVisibility(GV, cast<CXXRecordDecl>(RT->getDecl()),
645 /*ForRTTI*/ true);
John McCall279b5eb2010-08-12 23:36:15 +0000646 else if (CGM.getCodeGenOpts().HiddenWeakVTables &&
647 Linkage == llvm::GlobalValue::WeakODRLinkage)
John McCallcbfe5022010-08-04 08:34:44 +0000648 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
649
Anders Carlsson8d145152009-12-20 22:30:54 +0000650 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
651}
652
Anders Carlsson08148092009-12-30 23:47:56 +0000653/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000654/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000655static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000656 unsigned Flags = 0;
657
658 if (Quals.hasConst())
659 Flags |= RTTIBuilder::PTI_Const;
660 if (Quals.hasVolatile())
661 Flags |= RTTIBuilder::PTI_Volatile;
662 if (Quals.hasRestrict())
663 Flags |= RTTIBuilder::PTI_Restrict;
664
665 return Flags;
666}
667
John McCalle8dc53e2010-08-12 02:17:33 +0000668/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
669/// for the given Objective-C object type.
670void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
671 // Drop qualifiers.
672 const Type *T = OT->getBaseType().getTypePtr();
673 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
674
675 // The builtin types are abi::__class_type_infos and don't require
676 // extra fields.
677 if (isa<BuiltinType>(T)) return;
678
679 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
680 ObjCInterfaceDecl *Super = Class->getSuperClass();
681
682 // Root classes are also __class_type_info.
683 if (!Super) return;
684
685 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
686
687 // Everything else is single inheritance.
688 llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
689 Fields.push_back(BaseTypeInfo);
690}
691
Anders Carlssonf64531a2009-12-30 01:00:12 +0000692/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
693/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
694void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
695 // Itanium C++ ABI 2.9.5p6b:
696 // It adds to abi::__class_type_info a single member pointing to the
697 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000698 llvm::Constant *BaseTypeInfo =
699 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
700 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000701}
702
Anders Carlsson08148092009-12-30 23:47:56 +0000703/// SeenBases - Contains virtual and non-virtual bases seen when traversing
704/// a class hierarchy.
705struct SeenBases {
706 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
707 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
708};
709
710/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
711/// abi::__vmi_class_type_info.
712///
713static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
714 SeenBases &Bases) {
715
716 unsigned Flags = 0;
717
718 const CXXRecordDecl *BaseDecl =
719 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
720
721 if (Base->isVirtual()) {
722 if (Bases.VirtualBases.count(BaseDecl)) {
723 // If this virtual base has been seen before, then the class is diamond
724 // shaped.
725 Flags |= RTTIBuilder::VMI_DiamondShaped;
726 } else {
727 if (Bases.NonVirtualBases.count(BaseDecl))
728 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
729
730 // Mark the virtual base as seen.
731 Bases.VirtualBases.insert(BaseDecl);
732 }
733 } else {
734 if (Bases.NonVirtualBases.count(BaseDecl)) {
735 // If this non-virtual base has been seen before, then the class has non-
736 // diamond shaped repeated inheritance.
737 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
738 } else {
739 if (Bases.VirtualBases.count(BaseDecl))
740 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
741
742 // Mark the non-virtual base as seen.
743 Bases.NonVirtualBases.insert(BaseDecl);
744 }
745 }
746
747 // Walk all bases.
748 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
749 E = BaseDecl->bases_end(); I != E; ++I)
750 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
751
752 return Flags;
753}
754
755static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
756 unsigned Flags = 0;
757 SeenBases Bases;
758
759 // Walk all bases.
760 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
761 E = RD->bases_end(); I != E; ++I)
762 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
763
764 return Flags;
765}
766
767/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
768/// classes with bases that do not satisfy the abi::__si_class_type_info
769/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
770void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
771 const llvm::Type *UnsignedIntLTy =
772 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
773
774 // Itanium C++ ABI 2.9.5p6c:
775 // __flags is a word with flags describing details about the class
776 // structure, which may be referenced by using the __flags_masks
777 // enumeration. These flags refer to both direct and indirect bases.
778 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000779 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000780
781 // Itanium C++ ABI 2.9.5p6c:
782 // __base_count is a word with the number of direct proper base class
783 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000784 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000785
786 if (!RD->getNumBases())
787 return;
788
789 const llvm::Type *LongLTy =
790 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
791
792 // Now add the base class descriptions.
793
794 // Itanium C++ ABI 2.9.5p6c:
795 // __base_info[] is an array of base class descriptions -- one for every
796 // direct proper base. Each description is of the type:
797 //
798 // struct abi::__base_class_type_info {
799 // public:
800 // const __class_type_info *__base_type;
801 // long __offset_flags;
802 //
803 // enum __offset_flags_masks {
804 // __virtual_mask = 0x1,
805 // __public_mask = 0x2,
806 // __offset_shift = 8
807 // };
808 // };
809 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
810 E = RD->bases_end(); I != E; ++I) {
811 const CXXBaseSpecifier *Base = I;
812
813 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000814 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000815
816 const CXXRecordDecl *BaseDecl =
817 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
818
819 int64_t OffsetFlags = 0;
820
821 // All but the lower 8 bits of __offset_flags are a signed offset.
822 // For a non-virtual base, this is the offset in the object of the base
823 // subobject. For a virtual base, this is the offset in the virtual table of
824 // the virtual base offset for the virtual base referenced (negative).
825 if (Base->isVirtual())
Anders Carlssonaf440352010-03-23 04:11:45 +0000826 OffsetFlags = CGM.getVTables().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000827 else {
828 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
829 OffsetFlags = Layout.getBaseClassOffset(BaseDecl) / 8;
830 };
831
832 OffsetFlags <<= 8;
833
834 // The low-order byte of __offset_flags contains flags, as given by the
835 // masks from the enumeration __offset_flags_masks.
836 if (Base->isVirtual())
837 OffsetFlags |= BCTI_Virtual;
838 if (Base->getAccessSpecifier() == AS_public)
839 OffsetFlags |= BCTI_Public;
840
Anders Carlsson531d55f2009-12-31 17:43:53 +0000841 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000842 }
843}
844
Anders Carlsson8d145152009-12-20 22:30:54 +0000845/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
846/// used for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +0000847void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
Anders Carlssonabd6b092010-06-02 15:44:35 +0000848 Qualifiers Quals;
849 QualType UnqualifiedPointeeTy =
850 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
851
Anders Carlsson8d145152009-12-20 22:30:54 +0000852 // Itanium C++ ABI 2.9.5p7:
853 // __flags is a flag word describing the cv-qualification and other
854 // attributes of the type pointed to
Anders Carlssonabd6b092010-06-02 15:44:35 +0000855 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000856
857 // Itanium C++ ABI 2.9.5p7:
858 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
859 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000860 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000861 Flags |= PTI_Incomplete;
862
863 const llvm::Type *UnsignedIntLTy =
864 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000865 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000866
867 // Itanium C++ ABI 2.9.5p7:
868 // __pointee is a pointer to the std::type_info derivation for the
869 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000870 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000871 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000872 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000873}
874
875/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
876/// struct, used for member pointer types.
877void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
878 QualType PointeeTy = Ty->getPointeeType();
879
Anders Carlssonabd6b092010-06-02 15:44:35 +0000880 Qualifiers Quals;
881 QualType UnqualifiedPointeeTy =
882 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
883
Anders Carlsson8d145152009-12-20 22:30:54 +0000884 // Itanium C++ ABI 2.9.5p7:
885 // __flags is a flag word describing the cv-qualification and other
886 // attributes of the type pointed to.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000887 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000888
889 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000890
891 // Itanium C++ ABI 2.9.5p7:
892 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
893 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000894 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000895 Flags |= PTI_Incomplete;
896
Anders Carlsson8d145152009-12-20 22:30:54 +0000897 if (IsIncompleteClassType(ClassType))
898 Flags |= PTI_ContainingClassIncomplete;
899
Anders Carlsson8d145152009-12-20 22:30:54 +0000900 const llvm::Type *UnsignedIntLTy =
901 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000902 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000903
904 // Itanium C++ ABI 2.9.5p7:
905 // __pointee is a pointer to the std::type_info derivation for the
906 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000907 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000908 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000909 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000910
911 // Itanium C++ ABI 2.9.5p9:
912 // __context is a pointer to an abi::__class_type_info corresponding to the
913 // class type containing the member pointed to
914 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000915 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000916}
917
John McCall9dffe6f2010-04-30 01:15:21 +0000918llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
919 bool ForEH) {
920 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
921 // FIXME: should we even be calling this method if RTTI is disabled
922 // and it's not for EH?
923 if (!ForEH && !getContext().getLangOptions().RTTI) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000924 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
925 return llvm::Constant::getNullValue(Int8PtrTy);
926 }
John McCall9dffe6f2010-04-30 01:15:21 +0000927
Anders Carlsson531d55f2009-12-31 17:43:53 +0000928 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000929}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000930
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000931void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
932 QualType PointerType = Context.getPointerType(Type);
933 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
934 RTTIBuilder(*this).BuildTypeInfo(Type, true);
935 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
936 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
937}
938
939void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000940 QualType FundamentalTypes[] = { Context.VoidTy, Context.Char32Ty,
941 Context.Char16Ty, Context.UnsignedLongLongTy,
942 Context.LongLongTy, Context.WCharTy,
943 Context.UnsignedShortTy, Context.ShortTy,
944 Context.UnsignedLongTy, Context.LongTy,
945 Context.UnsignedIntTy, Context.IntTy,
946 Context.UnsignedCharTy, Context.FloatTy,
947 Context.LongDoubleTy, Context.DoubleTy,
948 Context.CharTy, Context.BoolTy,
949 Context.SignedCharTy };
950 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
951 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
952}