blob: 0724fdfba35679b85fc44c79c4d12243d97cbc53 [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) {
393 const char *VTableName;
Anders Carlsson8d145152009-12-20 22:30:54 +0000394
395 switch (Ty->getTypeClass()) {
396 default: assert(0 && "Unhandled type!");
Anders Carlsson978ef682009-12-29 21:58:32 +0000397
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000398 case Type::Builtin:
Douglas Gregor031b3712010-03-31 00:15:35 +0000399 // GCC treats vector types as fundamental types.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000400 case Type::Vector:
401 case Type::ExtVector:
Anders Carlsson08148092009-12-30 23:47:56 +0000402 // abi::__fundamental_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000403 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000404 break;
405
Anders Carlsson978ef682009-12-29 21:58:32 +0000406 case Type::ConstantArray:
407 case Type::IncompleteArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000408 // abi::__array_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000409 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000410 break;
411
412 case Type::FunctionNoProto:
413 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000414 // abi::__function_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000415 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000416 break;
417
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000418 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000419 // abi::__enum_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000420 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000421 break;
422
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000423 case Type::Record: {
424 const CXXRecordDecl *RD =
425 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000426
John McCall86ff3082010-02-04 22:26:26 +0000427 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson08148092009-12-30 23:47:56 +0000428 // abi::__class_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000429 VTableName = "_ZTVN10__cxxabiv117__class_type_infoE";
Anders Carlssonf64531a2009-12-30 01:00:12 +0000430 } else if (CanUseSingleInheritance(RD)) {
Anders Carlsson08148092009-12-30 23:47:56 +0000431 // abi::__si_class_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000432 VTableName = "_ZTVN10__cxxabiv120__si_class_type_infoE";
Anders Carlssonf64531a2009-12-30 01:00:12 +0000433 } else {
Anders Carlsson08148092009-12-30 23:47:56 +0000434 // abi::__vmi_class_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000435 VTableName = "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000436 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000437
438 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000439 }
440
Anders Carlsson8d145152009-12-20 22:30:54 +0000441 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000442 // abi::__pointer_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000443 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000444 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000445
Anders Carlsson8d145152009-12-20 22:30:54 +0000446 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000447 // abi::__pointer_to_member_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000448 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000449 break;
450 }
451
Anders Carlsson046c2942010-04-17 20:15:18 +0000452 llvm::Constant *VTable =
453 CGM.getModule().getOrInsertGlobal(VTableName, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000454
455 const llvm::Type *PtrDiffTy =
456 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
457
458 // The vtable address point is 2.
459 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Anders Carlsson046c2942010-04-17 20:15:18 +0000460 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, &Two, 1);
461 VTable = llvm::ConstantExpr::getBitCast(VTable, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000462
Anders Carlsson046c2942010-04-17 20:15:18 +0000463 Fields.push_back(VTable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000464}
465
John McCallcbfe5022010-08-04 08:34:44 +0000466llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000467 // We want to operate on the canonical type.
468 Ty = CGM.getContext().getCanonicalType(Ty);
469
470 // Check if we've already emitted an RTTI descriptor for this type.
471 llvm::SmallString<256> OutName;
472 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
473 llvm::StringRef Name = OutName.str();
474
475 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
476 if (OldGV && !OldGV->isDeclaration())
477 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
John McCallcbfe5022010-08-04 08:34:44 +0000478
Anders Carlsson8d145152009-12-20 22:30:54 +0000479 // Check if there is already an external RTTI descriptor for this type.
John McCallcbfe5022010-08-04 08:34:44 +0000480 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
481 if (!Force &&
482 (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM.getContext(), Ty)))
Anders Carlsson8d145152009-12-20 22:30:54 +0000483 return GetAddrOfExternalRTTIDescriptor(Ty);
484
John McCallcbfe5022010-08-04 08:34:44 +0000485 // Emit the standard library with external linkage.
486 llvm::GlobalVariable::LinkageTypes Linkage;
487 if (IsStdLib)
488 Linkage = llvm::GlobalValue::ExternalLinkage;
489 else
490 Linkage = getTypeInfoLinkage(Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +0000491
492 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000493 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000494
495 // And the name.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000496 Fields.push_back(BuildName(Ty, DecideHidden(Ty), Linkage));
John McCallcbfe5022010-08-04 08:34:44 +0000497
Anders Carlsson8d145152009-12-20 22:30:54 +0000498 switch (Ty->getTypeClass()) {
499 default: assert(false && "Unhandled type class!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000500
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000501 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000502 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000503 case Type::Vector:
504 case Type::ExtVector:
505 // Itanium C++ ABI 2.9.5p4:
506 // abi::__fundamental_type_info adds no data members to std::type_info.
507 break;
508
Anders Carlsson978ef682009-12-29 21:58:32 +0000509 case Type::ConstantArray:
510 case Type::IncompleteArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000511 // Itanium C++ ABI 2.9.5p5:
512 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000513 break;
514
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000515 case Type::FunctionNoProto:
516 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000517 // Itanium C++ ABI 2.9.5p5:
518 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000519 break;
520
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000521 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000522 // Itanium C++ ABI 2.9.5p5:
523 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000524 break;
525
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000526 case Type::Record: {
527 const CXXRecordDecl *RD =
528 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000529 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000530 // We don't need to emit any fields.
531 break;
532 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000533
Anders Carlsson08148092009-12-30 23:47:56 +0000534 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000535 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000536 else
537 BuildVMIClassTypeInfo(RD);
538
539 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000540 }
541
Anders Carlsson8d145152009-12-20 22:30:54 +0000542 case Type::Pointer:
543 BuildPointerTypeInfo(cast<PointerType>(Ty));
544 break;
545
546 case Type::MemberPointer:
547 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
548 break;
549 }
550
551 llvm::Constant *Init =
Anders Carlsson531d55f2009-12-31 17:43:53 +0000552 llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
Anders Carlsson8d145152009-12-20 22:30:54 +0000553 /*Packed=*/false);
554
555 llvm::GlobalVariable *GV =
556 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
557 /*Constant=*/true, Linkage, Init, Name);
558
559 // If there's already an old global variable, replace it with the new one.
560 if (OldGV) {
561 GV->takeName(OldGV);
562 llvm::Constant *NewPtr =
563 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
564 OldGV->replaceAllUsesWith(NewPtr);
565 OldGV->eraseFromParent();
566 }
John McCallcbfe5022010-08-04 08:34:44 +0000567
568 // GCC only relies on the uniqueness of the type names, not the
569 // type_infos themselves, so we can emit these as hidden symbols.
570 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
571 CGM.setTypeVisibility(GV, cast<CXXRecordDecl>(RT->getDecl()),
572 /*ForRTTI*/ true);
573 else if (Linkage == llvm::GlobalValue::WeakODRLinkage)
574 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
575
Anders Carlsson8d145152009-12-20 22:30:54 +0000576 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
577}
578
Anders Carlsson08148092009-12-30 23:47:56 +0000579/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000580/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000581static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000582 unsigned Flags = 0;
583
584 if (Quals.hasConst())
585 Flags |= RTTIBuilder::PTI_Const;
586 if (Quals.hasVolatile())
587 Flags |= RTTIBuilder::PTI_Volatile;
588 if (Quals.hasRestrict())
589 Flags |= RTTIBuilder::PTI_Restrict;
590
591 return Flags;
592}
593
Anders Carlssonf64531a2009-12-30 01:00:12 +0000594/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
595/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
596void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
597 // Itanium C++ ABI 2.9.5p6b:
598 // It adds to abi::__class_type_info a single member pointing to the
599 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000600 llvm::Constant *BaseTypeInfo =
601 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
602 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000603}
604
Anders Carlsson08148092009-12-30 23:47:56 +0000605/// SeenBases - Contains virtual and non-virtual bases seen when traversing
606/// a class hierarchy.
607struct SeenBases {
608 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
609 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
610};
611
612/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
613/// abi::__vmi_class_type_info.
614///
615static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
616 SeenBases &Bases) {
617
618 unsigned Flags = 0;
619
620 const CXXRecordDecl *BaseDecl =
621 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
622
623 if (Base->isVirtual()) {
624 if (Bases.VirtualBases.count(BaseDecl)) {
625 // If this virtual base has been seen before, then the class is diamond
626 // shaped.
627 Flags |= RTTIBuilder::VMI_DiamondShaped;
628 } else {
629 if (Bases.NonVirtualBases.count(BaseDecl))
630 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
631
632 // Mark the virtual base as seen.
633 Bases.VirtualBases.insert(BaseDecl);
634 }
635 } else {
636 if (Bases.NonVirtualBases.count(BaseDecl)) {
637 // If this non-virtual base has been seen before, then the class has non-
638 // diamond shaped repeated inheritance.
639 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
640 } else {
641 if (Bases.VirtualBases.count(BaseDecl))
642 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
643
644 // Mark the non-virtual base as seen.
645 Bases.NonVirtualBases.insert(BaseDecl);
646 }
647 }
648
649 // Walk all bases.
650 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
651 E = BaseDecl->bases_end(); I != E; ++I)
652 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
653
654 return Flags;
655}
656
657static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
658 unsigned Flags = 0;
659 SeenBases Bases;
660
661 // Walk all bases.
662 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
663 E = RD->bases_end(); I != E; ++I)
664 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
665
666 return Flags;
667}
668
669/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
670/// classes with bases that do not satisfy the abi::__si_class_type_info
671/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
672void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
673 const llvm::Type *UnsignedIntLTy =
674 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
675
676 // Itanium C++ ABI 2.9.5p6c:
677 // __flags is a word with flags describing details about the class
678 // structure, which may be referenced by using the __flags_masks
679 // enumeration. These flags refer to both direct and indirect bases.
680 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000681 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000682
683 // Itanium C++ ABI 2.9.5p6c:
684 // __base_count is a word with the number of direct proper base class
685 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000686 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000687
688 if (!RD->getNumBases())
689 return;
690
691 const llvm::Type *LongLTy =
692 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
693
694 // Now add the base class descriptions.
695
696 // Itanium C++ ABI 2.9.5p6c:
697 // __base_info[] is an array of base class descriptions -- one for every
698 // direct proper base. Each description is of the type:
699 //
700 // struct abi::__base_class_type_info {
701 // public:
702 // const __class_type_info *__base_type;
703 // long __offset_flags;
704 //
705 // enum __offset_flags_masks {
706 // __virtual_mask = 0x1,
707 // __public_mask = 0x2,
708 // __offset_shift = 8
709 // };
710 // };
711 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
712 E = RD->bases_end(); I != E; ++I) {
713 const CXXBaseSpecifier *Base = I;
714
715 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000716 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000717
718 const CXXRecordDecl *BaseDecl =
719 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
720
721 int64_t OffsetFlags = 0;
722
723 // All but the lower 8 bits of __offset_flags are a signed offset.
724 // For a non-virtual base, this is the offset in the object of the base
725 // subobject. For a virtual base, this is the offset in the virtual table of
726 // the virtual base offset for the virtual base referenced (negative).
727 if (Base->isVirtual())
Anders Carlssonaf440352010-03-23 04:11:45 +0000728 OffsetFlags = CGM.getVTables().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000729 else {
730 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
731 OffsetFlags = Layout.getBaseClassOffset(BaseDecl) / 8;
732 };
733
734 OffsetFlags <<= 8;
735
736 // The low-order byte of __offset_flags contains flags, as given by the
737 // masks from the enumeration __offset_flags_masks.
738 if (Base->isVirtual())
739 OffsetFlags |= BCTI_Virtual;
740 if (Base->getAccessSpecifier() == AS_public)
741 OffsetFlags |= BCTI_Public;
742
Anders Carlsson531d55f2009-12-31 17:43:53 +0000743 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000744 }
745}
746
Anders Carlsson8d145152009-12-20 22:30:54 +0000747/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
748/// used for pointer types.
749void RTTIBuilder::BuildPointerTypeInfo(const PointerType *Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000750 QualType PointeeTy = Ty->getPointeeType();
Anders Carlsson8d145152009-12-20 22:30:54 +0000751
Anders Carlssonabd6b092010-06-02 15:44:35 +0000752 Qualifiers Quals;
753 QualType UnqualifiedPointeeTy =
754 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
755
Anders Carlsson8d145152009-12-20 22:30:54 +0000756 // Itanium C++ ABI 2.9.5p7:
757 // __flags is a flag word describing the cv-qualification and other
758 // attributes of the type pointed to
Anders Carlssonabd6b092010-06-02 15:44:35 +0000759 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000760
761 // Itanium C++ ABI 2.9.5p7:
762 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
763 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000764 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000765 Flags |= PTI_Incomplete;
766
767 const llvm::Type *UnsignedIntLTy =
768 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000769 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000770
771 // Itanium C++ ABI 2.9.5p7:
772 // __pointee is a pointer to the std::type_info derivation for the
773 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000774 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000775 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000776 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000777}
778
779/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
780/// struct, used for member pointer types.
781void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
782 QualType PointeeTy = Ty->getPointeeType();
783
Anders Carlssonabd6b092010-06-02 15:44:35 +0000784 Qualifiers Quals;
785 QualType UnqualifiedPointeeTy =
786 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
787
Anders Carlsson8d145152009-12-20 22:30:54 +0000788 // Itanium C++ ABI 2.9.5p7:
789 // __flags is a flag word describing the cv-qualification and other
790 // attributes of the type pointed to.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000791 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000792
793 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000794
795 // Itanium C++ ABI 2.9.5p7:
796 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
797 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000798 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000799 Flags |= PTI_Incomplete;
800
Anders Carlsson8d145152009-12-20 22:30:54 +0000801 if (IsIncompleteClassType(ClassType))
802 Flags |= PTI_ContainingClassIncomplete;
803
Anders Carlsson8d145152009-12-20 22:30:54 +0000804 const llvm::Type *UnsignedIntLTy =
805 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000806 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000807
808 // Itanium C++ ABI 2.9.5p7:
809 // __pointee is a pointer to the std::type_info derivation for the
810 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000811 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000812 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000813 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000814
815 // Itanium C++ ABI 2.9.5p9:
816 // __context is a pointer to an abi::__class_type_info corresponding to the
817 // class type containing the member pointed to
818 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000819 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000820}
821
John McCall9dffe6f2010-04-30 01:15:21 +0000822llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
823 bool ForEH) {
824 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
825 // FIXME: should we even be calling this method if RTTI is disabled
826 // and it's not for EH?
827 if (!ForEH && !getContext().getLangOptions().RTTI) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000828 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
829 return llvm::Constant::getNullValue(Int8PtrTy);
830 }
John McCall9dffe6f2010-04-30 01:15:21 +0000831
Anders Carlsson531d55f2009-12-31 17:43:53 +0000832 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000833}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000834
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000835void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
836 QualType PointerType = Context.getPointerType(Type);
837 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
838 RTTIBuilder(*this).BuildTypeInfo(Type, true);
839 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
840 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
841}
842
843void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000844 QualType FundamentalTypes[] = { Context.VoidTy, Context.Char32Ty,
845 Context.Char16Ty, Context.UnsignedLongLongTy,
846 Context.LongLongTy, Context.WCharTy,
847 Context.UnsignedShortTy, Context.ShortTy,
848 Context.UnsignedLongTy, Context.LongTy,
849 Context.UnsignedIntTy, Context.IntTy,
850 Context.UnsignedCharTy, Context.FloatTy,
851 Context.LongDoubleTy, Context.DoubleTy,
852 Context.CharTy, Context.BoolTy,
853 Context.SignedCharTy };
854 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
855 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
856}