blob: 7dce9a0f5fa82b8248d66205c1956412c90868a4 [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.
Anders Carlsson8d145152009-12-20 22:30:54 +000048 void BuildPointerTypeInfo(const PointerType *Ty);
49
50 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
51 /// struct, used for member pointer types.
52 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
53
Mike Stump2b1bf312009-11-14 14:25:18 +000054public:
Mike Stumpde050572009-12-02 18:57:08 +000055 RTTIBuilder(CodeGenModule &cgm)
Mike Stump2b1bf312009-11-14 14:25:18 +000056 : CGM(cgm), VMContext(cgm.getModule().getContext()),
57 Int8PtrTy(llvm::Type::getInt8PtrTy(VMContext)) { }
58
Anders Carlsson31b7f522009-12-11 02:46:30 +000059 llvm::Constant *BuildName(QualType Ty, bool Hidden,
60 llvm::GlobalVariable::LinkageTypes Linkage) {
Mike Stump2b1bf312009-11-14 14:25:18 +000061 llvm::SmallString<256> OutName;
Mike Stumpde050572009-12-02 18:57:08 +000062 CGM.getMangleContext().mangleCXXRTTIName(Ty, OutName);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +000063 llvm::StringRef Name = OutName.str();
Mike Stumpcbcd4e52009-11-14 23:32:21 +000064
Anders Carlsson8d145152009-12-20 22:30:54 +000065 llvm::GlobalVariable *OGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +000066 if (OGV && !OGV->isDeclaration())
67 return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy);
Mike Stump58588942009-11-19 01:08:19 +000068
Anders Carlsson31b7f522009-12-11 02:46:30 +000069 llvm::Constant *C = llvm::ConstantArray::get(VMContext, Name.substr(4));
Mike Stump2b1bf312009-11-14 14:25:18 +000070
Anders Carlsson31b7f522009-12-11 02:46:30 +000071 llvm::GlobalVariable *GV =
72 new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
73 C, Name);
Mike Stump58588942009-11-19 01:08:19 +000074 if (OGV) {
75 GV->takeName(OGV);
76 llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
77 OGV->getType());
78 OGV->replaceAllUsesWith(NewPtr);
79 OGV->eraseFromParent();
80 }
Mike Stump582b0372009-11-18 03:46:51 +000081 if (Hidden)
82 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
83 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Daniel Dunbar7177dee2009-12-19 17:50:07 +000084 }
Mike Stumpc7a05bd2009-11-14 15:55:18 +000085
Mike Stump4e6f8ee2009-12-24 02:33:48 +000086 // FIXME: unify with DecideExtern
Mike Stump58588942009-11-19 01:08:19 +000087 bool DecideHidden(QualType Ty) {
88 // For this type, see if all components are never hidden.
89 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>())
90 return (DecideHidden(MPT->getPointeeType())
91 && DecideHidden(QualType(MPT->getClass(), 0)));
92 if (const PointerType *PT = Ty->getAs<PointerType>())
93 return DecideHidden(PT->getPointeeType());
Mike Stump4e6f8ee2009-12-24 02:33:48 +000094 if (const FunctionType *FT = Ty->getAs<FunctionType>()) {
95 if (DecideHidden(FT->getResultType()) == false)
96 return false;
97 if (const FunctionProtoType *FPT = Ty->getAs<FunctionProtoType>()) {
98 for (unsigned i = 0; i <FPT->getNumArgs(); ++i)
99 if (DecideHidden(FPT->getArgType(i)) == false)
100 return false;
101 for (unsigned i = 0; i <FPT->getNumExceptions(); ++i)
102 if (DecideHidden(FPT->getExceptionType(i)) == false)
103 return false;
104 return true;
105 }
106 }
Mike Stump58588942009-11-19 01:08:19 +0000107 if (const RecordType *RT = Ty->getAs<RecordType>())
108 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
109 return CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden;
110 return false;
111 }
Anders Carlsson31b7f522009-12-11 02:46:30 +0000112
Anders Carlsson8d145152009-12-20 22:30:54 +0000113 // Pointer type info flags.
114 enum {
115 /// PTI_Const - Type has const qualifier.
116 PTI_Const = 0x1,
117
118 /// PTI_Volatile - Type has volatile qualifier.
119 PTI_Volatile = 0x2,
120
121 /// PTI_Restrict - Type has restrict qualifier.
122 PTI_Restrict = 0x4,
123
124 /// PTI_Incomplete - Type is incomplete.
125 PTI_Incomplete = 0x8,
126
127 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
128 /// (in pointer to member).
129 PTI_ContainingClassIncomplete = 0x10
130 };
Anders Carlsson08148092009-12-30 23:47:56 +0000131
132 // VMI type info flags.
133 enum {
134 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
135 VMI_NonDiamondRepeat = 0x1,
136
137 /// VMI_DiamondShaped - Class is diamond shaped.
138 VMI_DiamondShaped = 0x2
139 };
140
141 // Base class type info flags.
142 enum {
143 /// BCTI_Virtual - Base class is virtual.
144 BCTI_Virtual = 0x1,
145
146 /// BCTI_Public - Base class is public.
147 BCTI_Public = 0x2
148 };
Anders Carlsson531d55f2009-12-31 17:43:53 +0000149
150 /// BuildTypeInfo - Build the RTTI type info struct for the given type.
John McCall9dffe6f2010-04-30 01:15:21 +0000151 ///
152 /// \param Force - true to force the creation of this RTTI value
153 /// \param ForEH - true if this is for exception handling
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000154 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false);
Mike Stump2b1bf312009-11-14 14:25:18 +0000155};
Mike Stump92f2fe22009-12-02 19:07:44 +0000156}
Mike Stump2b1bf312009-11-14 14:25:18 +0000157
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000158llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
159 // Mangle the RTTI name.
160 llvm::SmallString<256> OutName;
161 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
162 llvm::StringRef Name = OutName.str();
163
Anders Carlsson8d145152009-12-20 22:30:54 +0000164 // Look for an existing global.
165 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000166
167 if (!GV) {
168 // Create a new global variable.
169 GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
170 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000171 }
172
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000173 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000174}
175
Anders Carlsson8d145152009-12-20 22:30:54 +0000176/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
177/// info for that type is defined in the standard library.
178static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
179 // Itanium C++ ABI 2.9.2:
180 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
181 // the run-time support library. Specifically, the run-time support
182 // library should contain type_info objects for the types X, X* and
183 // X const*, for every X in: void, bool, wchar_t, char, unsigned char,
184 // signed char, short, unsigned short, int, unsigned int, long,
185 // unsigned long, long long, unsigned long long, float, double, long double,
186 // char16_t, char32_t, and the IEEE 754r decimal and half-precision
187 // floating point types.
188 switch (Ty->getKind()) {
189 case BuiltinType::Void:
190 case BuiltinType::Bool:
191 case BuiltinType::WChar:
192 case BuiltinType::Char_U:
193 case BuiltinType::Char_S:
194 case BuiltinType::UChar:
195 case BuiltinType::SChar:
196 case BuiltinType::Short:
197 case BuiltinType::UShort:
198 case BuiltinType::Int:
199 case BuiltinType::UInt:
200 case BuiltinType::Long:
201 case BuiltinType::ULong:
202 case BuiltinType::LongLong:
203 case BuiltinType::ULongLong:
204 case BuiltinType::Float:
205 case BuiltinType::Double:
206 case BuiltinType::LongDouble:
207 case BuiltinType::Char16:
208 case BuiltinType::Char32:
209 case BuiltinType::Int128:
210 case BuiltinType::UInt128:
211 return true;
212
213 case BuiltinType::Overload:
214 case BuiltinType::Dependent:
215 case BuiltinType::UndeducedAuto:
216 assert(false && "Should not see this type here!");
217
218 case BuiltinType::NullPtr:
219 assert(false && "FIXME: nullptr_t is not handled!");
220
221 case BuiltinType::ObjCId:
222 case BuiltinType::ObjCClass:
223 case BuiltinType::ObjCSel:
224 assert(false && "FIXME: Objective-C types are unsupported!");
225 }
226
227 // Silent gcc.
228 return false;
229}
230
231static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
232 QualType PointeeTy = PointerTy->getPointeeType();
233 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
234 if (!BuiltinTy)
235 return false;
236
237 // Check the qualifiers.
238 Qualifiers Quals = PointeeTy.getQualifiers();
239 Quals.removeConst();
240
241 if (!Quals.empty())
242 return false;
243
244 return TypeInfoIsInStandardLibrary(BuiltinTy);
245}
246
John McCallcbfe5022010-08-04 08:34:44 +0000247/// IsStandardLibraryRTTIDescriptor - Returns whether the type
248/// information for the given type exists in the standard library.
249static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000250 // Type info for builtin types is defined in the standard library.
251 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
252 return TypeInfoIsInStandardLibrary(BuiltinTy);
253
254 // Type info for some pointer types to builtin types is defined in the
255 // standard library.
256 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
257 return TypeInfoIsInStandardLibrary(PointerTy);
258
John McCallcbfe5022010-08-04 08:34:44 +0000259 return false;
260}
261
262/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
263/// the given type exists somewhere else, and that we should not emit the type
264/// information in this translation unit. Assumes that it is not a
265/// standard-library type.
266static bool ShouldUseExternalRTTIDescriptor(ASTContext &Context,
267 QualType Ty) {
John McCall9dffe6f2010-04-30 01:15:21 +0000268 // If RTTI is disabled, don't consider key functions.
269 if (!Context.getLangOptions().RTTI) return false;
270
Anders Carlsson8d145152009-12-20 22:30:54 +0000271 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000272 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000273 if (!RD->hasDefinition())
274 return false;
275
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000276 if (!RD->isDynamicClass())
277 return false;
278
279 // Get the key function.
280 const CXXMethodDecl *KeyFunction = RD->getASTContext().getKeyFunction(RD);
Argyrios Kyrtzidis4aedb1c2010-07-07 12:24:18 +0000281 if (KeyFunction && !KeyFunction->hasBody()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000282 // The class has a key function, but it is not defined in this translation
283 // unit, so we should use the external descriptor for it.
284 return true;
285 }
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) {
Eli Friedman1cf26f52010-08-11 20:41:51 +0000393 const char *VTableName = 0;
Anders Carlsson8d145152009-12-20 22:30:54 +0000394
395 switch (Ty->getTypeClass()) {
Eli Friedman1cf26f52010-08-11 20:41:51 +0000396#define TYPE(Class, Base)
397#define ABSTRACT_TYPE(Class, Base)
398#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
399#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
400#define DEPENDENT_TYPE(Class, Base) case Type::Class:
401#include "clang/AST/TypeNodes.def"
402 assert(false && "Non-canonical and dependent types shouldn't get here");
403
404 case Type::LValueReference:
405 case Type::RValueReference:
406 assert(false && "References shouldn't get here");
Anders Carlsson978ef682009-12-29 21:58:32 +0000407
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000408 case Type::Builtin:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000409 // GCC treats vector and complex types as fundamental types.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000410 case Type::Vector:
411 case Type::ExtVector:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000412 case Type::Complex:
413 // FIXME: GCC treats block pointers as fundamental types?!
414 case Type::BlockPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000415 // abi::__fundamental_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000416 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000417 break;
418
Anders Carlsson978ef682009-12-29 21:58:32 +0000419 case Type::ConstantArray:
420 case Type::IncompleteArray:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000421 case Type::VariableArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000422 // abi::__array_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000423 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000424 break;
425
426 case Type::FunctionNoProto:
427 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000428 // abi::__function_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000429 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000430 break;
431
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000432 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000433 // abi::__enum_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000434 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000435 break;
436
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000437 case Type::Record: {
438 const CXXRecordDecl *RD =
439 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000440
John McCall86ff3082010-02-04 22:26:26 +0000441 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson08148092009-12-30 23:47:56 +0000442 // abi::__class_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000443 VTableName = "_ZTVN10__cxxabiv117__class_type_infoE";
Anders Carlssonf64531a2009-12-30 01:00:12 +0000444 } else if (CanUseSingleInheritance(RD)) {
Anders Carlsson08148092009-12-30 23:47:56 +0000445 // abi::__si_class_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000446 VTableName = "_ZTVN10__cxxabiv120__si_class_type_infoE";
Anders Carlssonf64531a2009-12-30 01:00:12 +0000447 } else {
Anders Carlsson08148092009-12-30 23:47:56 +0000448 // abi::__vmi_class_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000449 VTableName = "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000450 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000451
452 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000453 }
454
Eli Friedman1cf26f52010-08-11 20:41:51 +0000455 case Type::ObjCObject:
456 case Type::ObjCInterface:
457 case Type::ObjCObjectPointer:
458 assert(false && "FIXME: Needs to be written!");
459 VTableName = "_ZTVN10__cxxabiv117__class_type_infoE";
460 break;
461
Anders Carlsson8d145152009-12-20 22:30:54 +0000462 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000463 // abi::__pointer_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000464 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000465 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000466
Anders Carlsson8d145152009-12-20 22:30:54 +0000467 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000468 // abi::__pointer_to_member_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000469 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000470 break;
471 }
472
Anders Carlsson046c2942010-04-17 20:15:18 +0000473 llvm::Constant *VTable =
474 CGM.getModule().getOrInsertGlobal(VTableName, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000475
476 const llvm::Type *PtrDiffTy =
477 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
478
479 // The vtable address point is 2.
480 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Anders Carlsson046c2942010-04-17 20:15:18 +0000481 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, &Two, 1);
482 VTable = llvm::ConstantExpr::getBitCast(VTable, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000483
Anders Carlsson046c2942010-04-17 20:15:18 +0000484 Fields.push_back(VTable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000485}
486
John McCallcbfe5022010-08-04 08:34:44 +0000487llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000488 // We want to operate on the canonical type.
489 Ty = CGM.getContext().getCanonicalType(Ty);
490
491 // Check if we've already emitted an RTTI descriptor for this type.
492 llvm::SmallString<256> OutName;
493 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
494 llvm::StringRef Name = OutName.str();
495
496 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
497 if (OldGV && !OldGV->isDeclaration())
498 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
John McCallcbfe5022010-08-04 08:34:44 +0000499
Anders Carlsson8d145152009-12-20 22:30:54 +0000500 // Check if there is already an external RTTI descriptor for this type.
John McCallcbfe5022010-08-04 08:34:44 +0000501 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
502 if (!Force &&
503 (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM.getContext(), Ty)))
Anders Carlsson8d145152009-12-20 22:30:54 +0000504 return GetAddrOfExternalRTTIDescriptor(Ty);
505
John McCallcbfe5022010-08-04 08:34:44 +0000506 // Emit the standard library with external linkage.
507 llvm::GlobalVariable::LinkageTypes Linkage;
508 if (IsStdLib)
509 Linkage = llvm::GlobalValue::ExternalLinkage;
510 else
511 Linkage = getTypeInfoLinkage(Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +0000512
513 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000514 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000515
516 // And the name.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000517 Fields.push_back(BuildName(Ty, DecideHidden(Ty), Linkage));
John McCallcbfe5022010-08-04 08:34:44 +0000518
Anders Carlsson8d145152009-12-20 22:30:54 +0000519 switch (Ty->getTypeClass()) {
520 default: assert(false && "Unhandled type class!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000521
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000522 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000523 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000524 case Type::Vector:
525 case Type::ExtVector:
526 // Itanium C++ ABI 2.9.5p4:
527 // abi::__fundamental_type_info adds no data members to std::type_info.
528 break;
529
Anders Carlsson978ef682009-12-29 21:58:32 +0000530 case Type::ConstantArray:
531 case Type::IncompleteArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000532 // Itanium C++ ABI 2.9.5p5:
533 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000534 break;
535
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000536 case Type::FunctionNoProto:
537 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000538 // Itanium C++ ABI 2.9.5p5:
539 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000540 break;
541
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000542 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000543 // Itanium C++ ABI 2.9.5p5:
544 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000545 break;
546
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000547 case Type::Record: {
548 const CXXRecordDecl *RD =
549 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000550 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000551 // We don't need to emit any fields.
552 break;
553 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000554
Anders Carlsson08148092009-12-30 23:47:56 +0000555 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000556 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000557 else
558 BuildVMIClassTypeInfo(RD);
559
560 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000561 }
562
Anders Carlsson8d145152009-12-20 22:30:54 +0000563 case Type::Pointer:
564 BuildPointerTypeInfo(cast<PointerType>(Ty));
565 break;
566
567 case Type::MemberPointer:
568 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
569 break;
570 }
571
572 llvm::Constant *Init =
Anders Carlsson531d55f2009-12-31 17:43:53 +0000573 llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
Anders Carlsson8d145152009-12-20 22:30:54 +0000574 /*Packed=*/false);
575
576 llvm::GlobalVariable *GV =
577 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
578 /*Constant=*/true, Linkage, Init, Name);
579
580 // If there's already an old global variable, replace it with the new one.
581 if (OldGV) {
582 GV->takeName(OldGV);
583 llvm::Constant *NewPtr =
584 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
585 OldGV->replaceAllUsesWith(NewPtr);
586 OldGV->eraseFromParent();
587 }
John McCallcbfe5022010-08-04 08:34:44 +0000588
589 // GCC only relies on the uniqueness of the type names, not the
590 // type_infos themselves, so we can emit these as hidden symbols.
591 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
592 CGM.setTypeVisibility(GV, cast<CXXRecordDecl>(RT->getDecl()),
593 /*ForRTTI*/ true);
594 else if (Linkage == llvm::GlobalValue::WeakODRLinkage)
595 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
596
Anders Carlsson8d145152009-12-20 22:30:54 +0000597 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
598}
599
Anders Carlsson08148092009-12-30 23:47:56 +0000600/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000601/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000602static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000603 unsigned Flags = 0;
604
605 if (Quals.hasConst())
606 Flags |= RTTIBuilder::PTI_Const;
607 if (Quals.hasVolatile())
608 Flags |= RTTIBuilder::PTI_Volatile;
609 if (Quals.hasRestrict())
610 Flags |= RTTIBuilder::PTI_Restrict;
611
612 return Flags;
613}
614
Anders Carlssonf64531a2009-12-30 01:00:12 +0000615/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
616/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
617void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
618 // Itanium C++ ABI 2.9.5p6b:
619 // It adds to abi::__class_type_info a single member pointing to the
620 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000621 llvm::Constant *BaseTypeInfo =
622 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
623 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000624}
625
Anders Carlsson08148092009-12-30 23:47:56 +0000626/// SeenBases - Contains virtual and non-virtual bases seen when traversing
627/// a class hierarchy.
628struct SeenBases {
629 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
630 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
631};
632
633/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
634/// abi::__vmi_class_type_info.
635///
636static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
637 SeenBases &Bases) {
638
639 unsigned Flags = 0;
640
641 const CXXRecordDecl *BaseDecl =
642 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
643
644 if (Base->isVirtual()) {
645 if (Bases.VirtualBases.count(BaseDecl)) {
646 // If this virtual base has been seen before, then the class is diamond
647 // shaped.
648 Flags |= RTTIBuilder::VMI_DiamondShaped;
649 } else {
650 if (Bases.NonVirtualBases.count(BaseDecl))
651 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
652
653 // Mark the virtual base as seen.
654 Bases.VirtualBases.insert(BaseDecl);
655 }
656 } else {
657 if (Bases.NonVirtualBases.count(BaseDecl)) {
658 // If this non-virtual base has been seen before, then the class has non-
659 // diamond shaped repeated inheritance.
660 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
661 } else {
662 if (Bases.VirtualBases.count(BaseDecl))
663 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
664
665 // Mark the non-virtual base as seen.
666 Bases.NonVirtualBases.insert(BaseDecl);
667 }
668 }
669
670 // Walk all bases.
671 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
672 E = BaseDecl->bases_end(); I != E; ++I)
673 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
674
675 return Flags;
676}
677
678static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
679 unsigned Flags = 0;
680 SeenBases Bases;
681
682 // Walk all bases.
683 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
684 E = RD->bases_end(); I != E; ++I)
685 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
686
687 return Flags;
688}
689
690/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
691/// classes with bases that do not satisfy the abi::__si_class_type_info
692/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
693void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
694 const llvm::Type *UnsignedIntLTy =
695 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
696
697 // Itanium C++ ABI 2.9.5p6c:
698 // __flags is a word with flags describing details about the class
699 // structure, which may be referenced by using the __flags_masks
700 // enumeration. These flags refer to both direct and indirect bases.
701 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000702 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000703
704 // Itanium C++ ABI 2.9.5p6c:
705 // __base_count is a word with the number of direct proper base class
706 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000707 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000708
709 if (!RD->getNumBases())
710 return;
711
712 const llvm::Type *LongLTy =
713 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
714
715 // Now add the base class descriptions.
716
717 // Itanium C++ ABI 2.9.5p6c:
718 // __base_info[] is an array of base class descriptions -- one for every
719 // direct proper base. Each description is of the type:
720 //
721 // struct abi::__base_class_type_info {
722 // public:
723 // const __class_type_info *__base_type;
724 // long __offset_flags;
725 //
726 // enum __offset_flags_masks {
727 // __virtual_mask = 0x1,
728 // __public_mask = 0x2,
729 // __offset_shift = 8
730 // };
731 // };
732 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
733 E = RD->bases_end(); I != E; ++I) {
734 const CXXBaseSpecifier *Base = I;
735
736 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000737 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000738
739 const CXXRecordDecl *BaseDecl =
740 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
741
742 int64_t OffsetFlags = 0;
743
744 // All but the lower 8 bits of __offset_flags are a signed offset.
745 // For a non-virtual base, this is the offset in the object of the base
746 // subobject. For a virtual base, this is the offset in the virtual table of
747 // the virtual base offset for the virtual base referenced (negative).
748 if (Base->isVirtual())
Anders Carlssonaf440352010-03-23 04:11:45 +0000749 OffsetFlags = CGM.getVTables().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000750 else {
751 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
752 OffsetFlags = Layout.getBaseClassOffset(BaseDecl) / 8;
753 };
754
755 OffsetFlags <<= 8;
756
757 // The low-order byte of __offset_flags contains flags, as given by the
758 // masks from the enumeration __offset_flags_masks.
759 if (Base->isVirtual())
760 OffsetFlags |= BCTI_Virtual;
761 if (Base->getAccessSpecifier() == AS_public)
762 OffsetFlags |= BCTI_Public;
763
Anders Carlsson531d55f2009-12-31 17:43:53 +0000764 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000765 }
766}
767
Anders Carlsson8d145152009-12-20 22:30:54 +0000768/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
769/// used for pointer types.
770void RTTIBuilder::BuildPointerTypeInfo(const PointerType *Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000771 QualType PointeeTy = Ty->getPointeeType();
Anders Carlsson8d145152009-12-20 22:30:54 +0000772
Anders Carlssonabd6b092010-06-02 15:44:35 +0000773 Qualifiers Quals;
774 QualType UnqualifiedPointeeTy =
775 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
776
Anders Carlsson8d145152009-12-20 22:30:54 +0000777 // Itanium C++ ABI 2.9.5p7:
778 // __flags is a flag word describing the cv-qualification and other
779 // attributes of the type pointed to
Anders Carlssonabd6b092010-06-02 15:44:35 +0000780 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000781
782 // Itanium C++ ABI 2.9.5p7:
783 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
784 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000785 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000786 Flags |= PTI_Incomplete;
787
788 const llvm::Type *UnsignedIntLTy =
789 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000790 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000791
792 // Itanium C++ ABI 2.9.5p7:
793 // __pointee is a pointer to the std::type_info derivation for the
794 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000795 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000796 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000797 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000798}
799
800/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
801/// struct, used for member pointer types.
802void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
803 QualType PointeeTy = Ty->getPointeeType();
804
Anders Carlssonabd6b092010-06-02 15:44:35 +0000805 Qualifiers Quals;
806 QualType UnqualifiedPointeeTy =
807 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
808
Anders Carlsson8d145152009-12-20 22:30:54 +0000809 // Itanium C++ ABI 2.9.5p7:
810 // __flags is a flag word describing the cv-qualification and other
811 // attributes of the type pointed to.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000812 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000813
814 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000815
816 // Itanium C++ ABI 2.9.5p7:
817 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
818 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000819 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000820 Flags |= PTI_Incomplete;
821
Anders Carlsson8d145152009-12-20 22:30:54 +0000822 if (IsIncompleteClassType(ClassType))
823 Flags |= PTI_ContainingClassIncomplete;
824
Anders Carlsson8d145152009-12-20 22:30:54 +0000825 const llvm::Type *UnsignedIntLTy =
826 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000827 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000828
829 // Itanium C++ ABI 2.9.5p7:
830 // __pointee is a pointer to the std::type_info derivation for the
831 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000832 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000833 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000834 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000835
836 // Itanium C++ ABI 2.9.5p9:
837 // __context is a pointer to an abi::__class_type_info corresponding to the
838 // class type containing the member pointed to
839 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000840 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000841}
842
John McCall9dffe6f2010-04-30 01:15:21 +0000843llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
844 bool ForEH) {
845 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
846 // FIXME: should we even be calling this method if RTTI is disabled
847 // and it's not for EH?
848 if (!ForEH && !getContext().getLangOptions().RTTI) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000849 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
850 return llvm::Constant::getNullValue(Int8PtrTy);
851 }
John McCall9dffe6f2010-04-30 01:15:21 +0000852
Anders Carlsson531d55f2009-12-31 17:43:53 +0000853 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000854}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000855
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000856void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
857 QualType PointerType = Context.getPointerType(Type);
858 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
859 RTTIBuilder(*this).BuildTypeInfo(Type, true);
860 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
861 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
862}
863
864void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000865 QualType FundamentalTypes[] = { Context.VoidTy, Context.Char32Ty,
866 Context.Char16Ty, Context.UnsignedLongLongTy,
867 Context.LongLongTy, Context.WCharTy,
868 Context.UnsignedShortTy, Context.ShortTy,
869 Context.UnsignedLongTy, Context.LongTy,
870 Context.UnsignedIntTy, Context.IntTy,
871 Context.UnsignedCharTy, Context.FloatTy,
872 Context.LongDoubleTy, Context.DoubleTy,
873 Context.CharTy, Context.BoolTy,
874 Context.SignedCharTy };
875 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
876 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
877}