blob: b2129489bb303b409e77e863c32fd21574ab1cee [file] [log] [blame]
Mike Stumpde050572009-12-02 18:57:08 +00001//===--- CGCXXRTTI.cpp - Emit LLVM Code for C++ RTTI descriptors ----------===//
Anders Carlsson656e4c12009-10-10 20:49:04 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation of RTTI descriptors.
11//
12//===----------------------------------------------------------------------===//
13
Mike Stump61c38012009-11-17 21:44:24 +000014#include "CodeGenModule.h"
John McCall4c40d982010-08-31 07:33:07 +000015#include "CGCXXABI.h"
John McCall279b5eb2010-08-12 23:36:15 +000016#include "clang/AST/RecordLayout.h"
17#include "clang/AST/Type.h"
18#include "clang/Frontend/CodeGenOptions.h"
19
Anders Carlsson656e4c12009-10-10 20:49:04 +000020using namespace clang;
21using namespace CodeGen;
22
Mike Stump92f2fe22009-12-02 19:07:44 +000023namespace {
Mike Stumpde050572009-12-02 18:57:08 +000024class RTTIBuilder {
Mike Stump2b1bf312009-11-14 14:25:18 +000025 CodeGenModule &CGM; // Per-module state.
26 llvm::LLVMContext &VMContext;
Anders Carlsson8d145152009-12-20 22:30:54 +000027
Anders Carlsson08148092009-12-30 23:47:56 +000028 const llvm::Type *Int8PtrTy;
Anders Carlsson531d55f2009-12-31 17:43:53 +000029
30 /// Fields - The fields of the RTTI descriptor currently being built.
31 llvm::SmallVector<llvm::Constant *, 16> Fields;
Anders Carlssond6baec82009-12-11 01:27:37 +000032
Anders Carlsson9a86a132011-01-29 20:36:11 +000033 /// GetAddrOfTypeName - Returns the mangled type name of the given type.
34 llvm::GlobalVariable *
35 GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage);
36
Anders Carlsson1d7088d2009-12-17 07:09:17 +000037 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
38 /// descriptor of the given type.
39 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
40
Anders Carlsson046c2942010-04-17 20:15:18 +000041 /// BuildVTablePointer - Build the vtable pointer for the given type.
42 void BuildVTablePointer(const Type *Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +000043
Anders Carlssonf64531a2009-12-30 01:00:12 +000044 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
Anders Carlsson08148092009-12-30 23:47:56 +000045 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
Anders Carlssonf64531a2009-12-30 01:00:12 +000046 void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
47
Anders Carlsson08148092009-12-30 23:47:56 +000048 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
49 /// classes with bases that do not satisfy the abi::__si_class_type_info
50 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
51 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
52
Anders Carlssonf64531a2009-12-30 01:00:12 +000053 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
54 /// for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +000055 void BuildPointerTypeInfo(QualType PointeeTy);
56
57 /// BuildObjCObjectTypeInfo - Build the appropriate kind of
58 /// type_info for an object type.
59 void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +000060
61 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
62 /// struct, used for member pointer types.
63 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
64
Mike Stump2b1bf312009-11-14 14:25:18 +000065public:
Mike Stumpde050572009-12-02 18:57:08 +000066 RTTIBuilder(CodeGenModule &cgm)
Mike Stump2b1bf312009-11-14 14:25:18 +000067 : CGM(cgm), VMContext(cgm.getModule().getContext()),
68 Int8PtrTy(llvm::Type::getInt8PtrTy(VMContext)) { }
69
Anders Carlsson31b7f522009-12-11 02:46:30 +000070 llvm::Constant *BuildName(QualType Ty, bool Hidden,
71 llvm::GlobalVariable::LinkageTypes Linkage) {
Mike Stump2b1bf312009-11-14 14:25:18 +000072 llvm::SmallString<256> OutName;
John McCall4c40d982010-08-31 07:33:07 +000073 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, OutName);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +000074 llvm::StringRef Name = OutName.str();
Mike Stumpcbcd4e52009-11-14 23:32:21 +000075
Anders Carlsson8d145152009-12-20 22:30:54 +000076 llvm::GlobalVariable *OGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +000077 if (OGV && !OGV->isDeclaration())
78 return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy);
Mike Stump58588942009-11-19 01:08:19 +000079
Anders Carlsson31b7f522009-12-11 02:46:30 +000080 llvm::Constant *C = llvm::ConstantArray::get(VMContext, Name.substr(4));
Mike Stump2b1bf312009-11-14 14:25:18 +000081
Anders Carlsson31b7f522009-12-11 02:46:30 +000082 llvm::GlobalVariable *GV =
83 new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
84 C, Name);
Mike Stump58588942009-11-19 01:08:19 +000085 if (OGV) {
86 GV->takeName(OGV);
87 llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
88 OGV->getType());
89 OGV->replaceAllUsesWith(NewPtr);
90 OGV->eraseFromParent();
91 }
Argyrios Kyrtzidis6d576052010-10-11 03:25:53 +000092 if (Hidden && Linkage != llvm::GlobalValue::InternalLinkage)
Mike Stump582b0372009-11-18 03:46:51 +000093 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
94 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Daniel Dunbar7177dee2009-12-19 17:50:07 +000095 }
Mike Stumpc7a05bd2009-11-14 15:55:18 +000096
Mike Stump4e6f8ee2009-12-24 02:33:48 +000097 // FIXME: unify with DecideExtern
Mike Stump58588942009-11-19 01:08:19 +000098 bool DecideHidden(QualType Ty) {
99 // For this type, see if all components are never hidden.
100 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>())
101 return (DecideHidden(MPT->getPointeeType())
102 && DecideHidden(QualType(MPT->getClass(), 0)));
103 if (const PointerType *PT = Ty->getAs<PointerType>())
104 return DecideHidden(PT->getPointeeType());
Mike Stump4e6f8ee2009-12-24 02:33:48 +0000105 if (const FunctionType *FT = Ty->getAs<FunctionType>()) {
106 if (DecideHidden(FT->getResultType()) == false)
107 return false;
108 if (const FunctionProtoType *FPT = Ty->getAs<FunctionProtoType>()) {
109 for (unsigned i = 0; i <FPT->getNumArgs(); ++i)
110 if (DecideHidden(FPT->getArgType(i)) == false)
111 return false;
112 for (unsigned i = 0; i <FPT->getNumExceptions(); ++i)
113 if (DecideHidden(FPT->getExceptionType(i)) == false)
114 return false;
115 return true;
116 }
117 }
Mike Stump58588942009-11-19 01:08:19 +0000118 if (const RecordType *RT = Ty->getAs<RecordType>())
119 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
John McCall1fb0caa2010-10-22 21:05:15 +0000120 return RD->getVisibility() == HiddenVisibility;
Mike Stump58588942009-11-19 01:08:19 +0000121 return false;
122 }
Anders Carlsson31b7f522009-12-11 02:46:30 +0000123
Anders Carlsson8d145152009-12-20 22:30:54 +0000124 // Pointer type info flags.
125 enum {
126 /// PTI_Const - Type has const qualifier.
127 PTI_Const = 0x1,
128
129 /// PTI_Volatile - Type has volatile qualifier.
130 PTI_Volatile = 0x2,
131
132 /// PTI_Restrict - Type has restrict qualifier.
133 PTI_Restrict = 0x4,
134
135 /// PTI_Incomplete - Type is incomplete.
136 PTI_Incomplete = 0x8,
137
138 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
139 /// (in pointer to member).
140 PTI_ContainingClassIncomplete = 0x10
141 };
Anders Carlsson08148092009-12-30 23:47:56 +0000142
143 // VMI type info flags.
144 enum {
145 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
146 VMI_NonDiamondRepeat = 0x1,
147
148 /// VMI_DiamondShaped - Class is diamond shaped.
149 VMI_DiamondShaped = 0x2
150 };
151
152 // Base class type info flags.
153 enum {
154 /// BCTI_Virtual - Base class is virtual.
155 BCTI_Virtual = 0x1,
156
157 /// BCTI_Public - Base class is public.
158 BCTI_Public = 0x2
159 };
Anders Carlsson531d55f2009-12-31 17:43:53 +0000160
161 /// BuildTypeInfo - Build the RTTI type info struct for the given type.
John McCall9dffe6f2010-04-30 01:15:21 +0000162 ///
163 /// \param Force - true to force the creation of this RTTI value
164 /// \param ForEH - true if this is for exception handling
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000165 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false);
Mike Stump2b1bf312009-11-14 14:25:18 +0000166};
Mike Stump92f2fe22009-12-02 19:07:44 +0000167}
Mike Stump2b1bf312009-11-14 14:25:18 +0000168
Anders Carlsson9a86a132011-01-29 20:36:11 +0000169llvm::GlobalVariable *
170RTTIBuilder::GetAddrOfTypeName(QualType Ty,
171 llvm::GlobalVariable::LinkageTypes Linkage) {
172 llvm::SmallString<256> OutName;
173 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, OutName);
174 llvm::StringRef Name = OutName.str();
175
176 // We know that the mangled name of the type starts at index 4 of the
177 // mangled name of the typename, so we can just index into it in order to
178 // get the mangled name of the type.
179 llvm::Constant *Init = llvm::ConstantArray::get(VMContext, Name.substr(4));
180
181 llvm::GlobalVariable *GV =
182 CGM.CreateOrReplaceCXXRuntimeVariable(Name, Init->getType(), Linkage);
183
184 GV->setInitializer(Init);
185
186 return GV;
187}
188
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000189llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
190 // Mangle the RTTI name.
191 llvm::SmallString<256> OutName;
John McCall4c40d982010-08-31 07:33:07 +0000192 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, OutName);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000193 llvm::StringRef Name = OutName.str();
194
Anders Carlsson8d145152009-12-20 22:30:54 +0000195 // Look for an existing global.
196 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000197
198 if (!GV) {
199 // Create a new global variable.
200 GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
201 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000202 }
203
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000204 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000205}
206
Anders Carlsson8d145152009-12-20 22:30:54 +0000207/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
208/// info for that type is defined in the standard library.
209static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
210 // Itanium C++ ABI 2.9.2:
211 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
212 // the run-time support library. Specifically, the run-time support
213 // library should contain type_info objects for the types X, X* and
Anders Carlsson2bd62502010-11-04 05:28:09 +0000214 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
215 // unsigned char, signed char, short, unsigned short, int, unsigned int,
216 // long, unsigned long, long long, unsigned long long, float, double,
217 // long double, char16_t, char32_t, and the IEEE 754r decimal and
218 // half-precision floating point types.
Anders Carlsson8d145152009-12-20 22:30:54 +0000219 switch (Ty->getKind()) {
220 case BuiltinType::Void:
Anders Carlsson2bd62502010-11-04 05:28:09 +0000221 case BuiltinType::NullPtr:
Anders Carlsson8d145152009-12-20 22:30:54 +0000222 case BuiltinType::Bool:
Chris Lattner3f59c972010-12-25 23:25:43 +0000223 case BuiltinType::WChar_S:
224 case BuiltinType::WChar_U:
Anders Carlsson8d145152009-12-20 22:30:54 +0000225 case BuiltinType::Char_U:
226 case BuiltinType::Char_S:
227 case BuiltinType::UChar:
228 case BuiltinType::SChar:
229 case BuiltinType::Short:
230 case BuiltinType::UShort:
231 case BuiltinType::Int:
232 case BuiltinType::UInt:
233 case BuiltinType::Long:
234 case BuiltinType::ULong:
235 case BuiltinType::LongLong:
236 case BuiltinType::ULongLong:
237 case BuiltinType::Float:
238 case BuiltinType::Double:
239 case BuiltinType::LongDouble:
240 case BuiltinType::Char16:
241 case BuiltinType::Char32:
242 case BuiltinType::Int128:
243 case BuiltinType::UInt128:
244 return true;
245
246 case BuiltinType::Overload:
247 case BuiltinType::Dependent:
248 case BuiltinType::UndeducedAuto:
249 assert(false && "Should not see this type here!");
250
Anders Carlsson8d145152009-12-20 22:30:54 +0000251 case BuiltinType::ObjCId:
252 case BuiltinType::ObjCClass:
253 case BuiltinType::ObjCSel:
254 assert(false && "FIXME: Objective-C types are unsupported!");
255 }
256
257 // Silent gcc.
258 return false;
259}
260
261static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
262 QualType PointeeTy = PointerTy->getPointeeType();
263 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
264 if (!BuiltinTy)
265 return false;
266
267 // Check the qualifiers.
268 Qualifiers Quals = PointeeTy.getQualifiers();
269 Quals.removeConst();
270
271 if (!Quals.empty())
272 return false;
273
274 return TypeInfoIsInStandardLibrary(BuiltinTy);
275}
276
John McCallcbfe5022010-08-04 08:34:44 +0000277/// IsStandardLibraryRTTIDescriptor - Returns whether the type
278/// information for the given type exists in the standard library.
279static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000280 // Type info for builtin types is defined in the standard library.
281 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
282 return TypeInfoIsInStandardLibrary(BuiltinTy);
283
284 // Type info for some pointer types to builtin types is defined in the
285 // standard library.
286 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
287 return TypeInfoIsInStandardLibrary(PointerTy);
288
John McCallcbfe5022010-08-04 08:34:44 +0000289 return false;
290}
291
292/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
293/// the given type exists somewhere else, and that we should not emit the type
294/// information in this translation unit. Assumes that it is not a
295/// standard-library type.
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000296static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM, QualType Ty) {
297 ASTContext &Context = CGM.getContext();
298
John McCall9dffe6f2010-04-30 01:15:21 +0000299 // If RTTI is disabled, don't consider key functions.
300 if (!Context.getLangOptions().RTTI) return false;
301
Anders Carlsson8d145152009-12-20 22:30:54 +0000302 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000303 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000304 if (!RD->hasDefinition())
305 return false;
306
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000307 if (!RD->isDynamicClass())
308 return false;
309
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000310 return !CGM.getVTables().ShouldEmitVTableInThisTU(RD);
Anders Carlsson8d145152009-12-20 22:30:54 +0000311 }
312
313 return false;
314}
315
316/// IsIncompleteClassType - Returns whether the given record type is incomplete.
317static bool IsIncompleteClassType(const RecordType *RecordTy) {
318 return !RecordTy->getDecl()->isDefinition();
319}
320
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000321/// ContainsIncompleteClassType - Returns whether the given type contains an
322/// incomplete class type. This is true if
323///
324/// * The given type is an incomplete class type.
325/// * The given type is a pointer type whose pointee type contains an
326/// incomplete class type.
327/// * The given type is a member pointer type whose class is an incomplete
328/// class type.
329/// * The given type is a member pointer type whoise pointee type contains an
330/// incomplete class type.
Anders Carlsson8d145152009-12-20 22:30:54 +0000331/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000332static bool ContainsIncompleteClassType(QualType Ty) {
333 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
334 if (IsIncompleteClassType(RecordTy))
335 return true;
336 }
337
338 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
339 return ContainsIncompleteClassType(PointerTy->getPointeeType());
340
341 if (const MemberPointerType *MemberPointerTy =
342 dyn_cast<MemberPointerType>(Ty)) {
343 // Check if the class type is incomplete.
344 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
345 if (IsIncompleteClassType(ClassType))
346 return true;
347
348 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000349 }
350
351 return false;
352}
353
354/// getTypeInfoLinkage - Return the linkage that the type info and type info
355/// name constants should have for the given type.
Anders Carlsson3a717f72011-01-24 02:04:33 +0000356static llvm::GlobalVariable::LinkageTypes
357getTypeInfoLinkage(CodeGenModule &CGM, QualType Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000358 // Itanium C++ ABI 2.9.5p7:
359 // In addition, it and all of the intermediate abi::__pointer_type_info
360 // structs in the chain down to the abi::__class_type_info for the
361 // incomplete class type must be prevented from resolving to the
362 // corresponding type_info structs for the complete class type, possibly
363 // by making them local static objects. Finally, a dummy class RTTI is
364 // generated for the incomplete type that will not resolve to the final
365 // complete class RTTI (because the latter need not exist), possibly by
366 // making it a local static object.
367 if (ContainsIncompleteClassType(Ty))
368 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000369
Douglas Gregor031b3712010-03-31 00:15:35 +0000370 switch (Ty->getLinkage()) {
371 case NoLinkage:
372 case InternalLinkage:
373 case UniqueExternalLinkage:
374 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson978ef682009-12-29 21:58:32 +0000375
Douglas Gregor031b3712010-03-31 00:15:35 +0000376 case ExternalLinkage:
Anders Carlssone34e3aa2011-01-24 02:12:11 +0000377 if (!CGM.getLangOptions().RTTI) {
378 // RTTI is not enabled, which means that this type info struct is going
379 // to be used for exception handling. Give it linkonce_odr linkage.
380 return llvm::GlobalValue::LinkOnceODRLinkage;
381 }
382
Douglas Gregor031b3712010-03-31 00:15:35 +0000383 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
384 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
385 if (RD->isDynamicClass())
Anders Carlsson3a717f72011-01-24 02:04:33 +0000386 return CGM.getVTableLinkage(RD);
Mike Stumpc8f76f52009-12-24 01:10:27 +0000387 }
Douglas Gregor031b3712010-03-31 00:15:35 +0000388
Anders Carlssonf502d932011-01-24 00:46:19 +0000389 return llvm::GlobalValue::LinkOnceODRLinkage;
Mike Stumpc8f76f52009-12-24 01:10:27 +0000390 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000391
Anders Carlssonf502d932011-01-24 00:46:19 +0000392 return llvm::GlobalValue::LinkOnceODRLinkage;
Anders Carlsson8d145152009-12-20 22:30:54 +0000393}
394
Anders Carlssonf64531a2009-12-30 01:00:12 +0000395// CanUseSingleInheritance - Return whether the given record decl has a "single,
396// public, non-virtual base at offset zero (i.e. the derived class is dynamic
397// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
398static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
399 // Check the number of bases.
400 if (RD->getNumBases() != 1)
401 return false;
402
403 // Get the base.
404 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
405
406 // Check that the base is not virtual.
407 if (Base->isVirtual())
408 return false;
409
410 // Check that the base is public.
411 if (Base->getAccessSpecifier() != AS_public)
412 return false;
413
414 // Check that the class is dynamic iff the base is.
415 const CXXRecordDecl *BaseDecl =
416 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
417 if (!BaseDecl->isEmpty() &&
418 BaseDecl->isDynamicClass() != RD->isDynamicClass())
419 return false;
420
421 return true;
422}
423
Anders Carlsson046c2942010-04-17 20:15:18 +0000424void RTTIBuilder::BuildVTablePointer(const Type *Ty) {
John McCalle8dc53e2010-08-12 02:17:33 +0000425 // abi::__class_type_info.
426 static const char * const ClassTypeInfo =
427 "_ZTVN10__cxxabiv117__class_type_infoE";
428 // abi::__si_class_type_info.
429 static const char * const SIClassTypeInfo =
430 "_ZTVN10__cxxabiv120__si_class_type_infoE";
431 // abi::__vmi_class_type_info.
432 static const char * const VMIClassTypeInfo =
433 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
434
Eli Friedman1cf26f52010-08-11 20:41:51 +0000435 const char *VTableName = 0;
Anders Carlsson8d145152009-12-20 22:30:54 +0000436
437 switch (Ty->getTypeClass()) {
Eli Friedman1cf26f52010-08-11 20:41:51 +0000438#define TYPE(Class, Base)
439#define ABSTRACT_TYPE(Class, Base)
440#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
441#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
442#define DEPENDENT_TYPE(Class, Base) case Type::Class:
443#include "clang/AST/TypeNodes.def"
444 assert(false && "Non-canonical and dependent types shouldn't get here");
445
446 case Type::LValueReference:
447 case Type::RValueReference:
448 assert(false && "References shouldn't get here");
Anders Carlsson978ef682009-12-29 21:58:32 +0000449
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000450 case Type::Builtin:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000451 // GCC treats vector and complex types as fundamental types.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000452 case Type::Vector:
453 case Type::ExtVector:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000454 case Type::Complex:
455 // FIXME: GCC treats block pointers as fundamental types?!
456 case Type::BlockPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000457 // abi::__fundamental_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000458 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000459 break;
460
Anders Carlsson978ef682009-12-29 21:58:32 +0000461 case Type::ConstantArray:
462 case Type::IncompleteArray:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000463 case Type::VariableArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000464 // abi::__array_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000465 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000466 break;
467
468 case Type::FunctionNoProto:
469 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000470 // abi::__function_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000471 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000472 break;
473
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000474 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000475 // abi::__enum_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000476 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000477 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000478
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000479 case Type::Record: {
480 const CXXRecordDecl *RD =
481 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000482
John McCall86ff3082010-02-04 22:26:26 +0000483 if (!RD->hasDefinition() || !RD->getNumBases()) {
John McCalle8dc53e2010-08-12 02:17:33 +0000484 VTableName = ClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000485 } else if (CanUseSingleInheritance(RD)) {
John McCalle8dc53e2010-08-12 02:17:33 +0000486 VTableName = SIClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000487 } else {
John McCalle8dc53e2010-08-12 02:17:33 +0000488 VTableName = VMIClassTypeInfo;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000489 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000490
491 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000492 }
493
Eli Friedman1cf26f52010-08-11 20:41:51 +0000494 case Type::ObjCObject:
John McCalle8dc53e2010-08-12 02:17:33 +0000495 // Ignore protocol qualifiers.
496 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
497
498 // Handle id and Class.
499 if (isa<BuiltinType>(Ty)) {
500 VTableName = ClassTypeInfo;
501 break;
502 }
503
504 assert(isa<ObjCInterfaceType>(Ty));
505 // Fall through.
506
Eli Friedman1cf26f52010-08-11 20:41:51 +0000507 case Type::ObjCInterface:
John McCalle8dc53e2010-08-12 02:17:33 +0000508 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
509 VTableName = SIClassTypeInfo;
510 } else {
511 VTableName = ClassTypeInfo;
512 }
Eli Friedman1cf26f52010-08-11 20:41:51 +0000513 break;
514
John McCalle8dc53e2010-08-12 02:17:33 +0000515 case Type::ObjCObjectPointer:
Anders Carlsson8d145152009-12-20 22:30:54 +0000516 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000517 // abi::__pointer_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000518 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000519 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000520
Anders Carlsson8d145152009-12-20 22:30:54 +0000521 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000522 // abi::__pointer_to_member_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000523 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000524 break;
525 }
526
Anders Carlsson046c2942010-04-17 20:15:18 +0000527 llvm::Constant *VTable =
528 CGM.getModule().getOrInsertGlobal(VTableName, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000529
530 const llvm::Type *PtrDiffTy =
531 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
532
533 // The vtable address point is 2.
534 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Anders Carlsson046c2942010-04-17 20:15:18 +0000535 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, &Two, 1);
536 VTable = llvm::ConstantExpr::getBitCast(VTable, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000537
Anders Carlsson046c2942010-04-17 20:15:18 +0000538 Fields.push_back(VTable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000539}
540
John McCallcbfe5022010-08-04 08:34:44 +0000541llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000542 // We want to operate on the canonical type.
543 Ty = CGM.getContext().getCanonicalType(Ty);
544
545 // Check if we've already emitted an RTTI descriptor for this type.
546 llvm::SmallString<256> OutName;
John McCall4c40d982010-08-31 07:33:07 +0000547 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, OutName);
Anders Carlsson8d145152009-12-20 22:30:54 +0000548 llvm::StringRef Name = OutName.str();
Anders Carlsson1cbce122011-01-29 19:16:51 +0000549
Anders Carlsson8d145152009-12-20 22:30:54 +0000550 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
551 if (OldGV && !OldGV->isDeclaration())
552 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
John McCallcbfe5022010-08-04 08:34:44 +0000553
Anders Carlsson8d145152009-12-20 22:30:54 +0000554 // Check if there is already an external RTTI descriptor for this type.
John McCallcbfe5022010-08-04 08:34:44 +0000555 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000556 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
Anders Carlsson8d145152009-12-20 22:30:54 +0000557 return GetAddrOfExternalRTTIDescriptor(Ty);
558
John McCallcbfe5022010-08-04 08:34:44 +0000559 // Emit the standard library with external linkage.
560 llvm::GlobalVariable::LinkageTypes Linkage;
561 if (IsStdLib)
562 Linkage = llvm::GlobalValue::ExternalLinkage;
563 else
Anders Carlsson3a717f72011-01-24 02:04:33 +0000564 Linkage = getTypeInfoLinkage(CGM, Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +0000565
566 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000567 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000568
569 // And the name.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000570 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
571
572 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
573 llvm::Constant *TypeNameAsInt8Ptr =
574 llvm::ConstantExpr::getBitCast(TypeName, Int8PtrTy);
575
John McCall9c39acf2010-12-17 02:58:03 +0000576 bool Hidden = DecideHidden(Ty);
Anders Carlsson9a86a132011-01-29 20:36:11 +0000577 Fields.push_back(TypeNameAsInt8Ptr);
John McCallcbfe5022010-08-04 08:34:44 +0000578
Anders Carlsson8d145152009-12-20 22:30:54 +0000579 switch (Ty->getTypeClass()) {
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000580#define TYPE(Class, Base)
581#define ABSTRACT_TYPE(Class, Base)
582#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
583#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
584#define DEPENDENT_TYPE(Class, Base) case Type::Class:
585#include "clang/AST/TypeNodes.def"
586 assert(false && "Non-canonical and dependent types shouldn't get here");
Anders Carlsson8d145152009-12-20 22:30:54 +0000587
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000588 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000589 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000590 case Type::Vector:
591 case Type::ExtVector:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000592 case Type::Complex:
593 case Type::BlockPointer:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000594 // Itanium C++ ABI 2.9.5p4:
595 // abi::__fundamental_type_info adds no data members to std::type_info.
596 break;
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000597
598 case Type::LValueReference:
599 case Type::RValueReference:
600 assert(false && "References shouldn't get here");
601
Anders Carlsson978ef682009-12-29 21:58:32 +0000602 case Type::ConstantArray:
603 case Type::IncompleteArray:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000604 case Type::VariableArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000605 // Itanium C++ ABI 2.9.5p5:
606 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000607 break;
608
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000609 case Type::FunctionNoProto:
610 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000611 // Itanium C++ ABI 2.9.5p5:
612 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000613 break;
614
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000615 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000616 // Itanium C++ ABI 2.9.5p5:
617 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000618 break;
619
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000620 case Type::Record: {
621 const CXXRecordDecl *RD =
622 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000623 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000624 // We don't need to emit any fields.
625 break;
626 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000627
Anders Carlsson08148092009-12-30 23:47:56 +0000628 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000629 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000630 else
631 BuildVMIClassTypeInfo(RD);
632
633 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000634 }
John McCalle8dc53e2010-08-12 02:17:33 +0000635
636 case Type::ObjCObject:
637 case Type::ObjCInterface:
638 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
639 break;
640
641 case Type::ObjCObjectPointer:
642 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
643 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000644
Anders Carlsson8d145152009-12-20 22:30:54 +0000645 case Type::Pointer:
John McCalle8dc53e2010-08-12 02:17:33 +0000646 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000647 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000648
Anders Carlsson8d145152009-12-20 22:30:54 +0000649 case Type::MemberPointer:
650 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
651 break;
652 }
653
654 llvm::Constant *Init =
Anders Carlsson531d55f2009-12-31 17:43:53 +0000655 llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
Anders Carlsson8d145152009-12-20 22:30:54 +0000656 /*Packed=*/false);
657
658 llvm::GlobalVariable *GV =
659 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
660 /*Constant=*/true, Linkage, Init, Name);
661
662 // If there's already an old global variable, replace it with the new one.
663 if (OldGV) {
664 GV->takeName(OldGV);
665 llvm::Constant *NewPtr =
666 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
667 OldGV->replaceAllUsesWith(NewPtr);
668 OldGV->eraseFromParent();
669 }
John McCallcbfe5022010-08-04 08:34:44 +0000670
671 // GCC only relies on the uniqueness of the type names, not the
672 // type_infos themselves, so we can emit these as hidden symbols.
John McCall279b5eb2010-08-12 23:36:15 +0000673 // But don't do this if we're worried about strict visibility
674 // compatibility.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000675 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) {
676 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
677
678 CGM.setTypeVisibility(GV, RD, CodeGenModule::TVK_ForRTTI);
679 CGM.setTypeVisibility(TypeName, RD, CodeGenModule::TVK_ForRTTIName);
680
681 } else if (Hidden ||
John McCall9c39acf2010-12-17 02:58:03 +0000682 (CGM.getCodeGenOpts().HiddenWeakVTables &&
Anders Carlssonf502d932011-01-24 00:46:19 +0000683 Linkage == llvm::GlobalValue::LinkOnceODRLinkage)) {
John McCallcbfe5022010-08-04 08:34:44 +0000684 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Rafael Espindolab1c65ff2011-01-11 21:44:37 +0000685 }
Rafael Espindola57244f62011-01-11 23:55:05 +0000686 GV->setUnnamedAddr(true);
687
Anders Carlsson8d145152009-12-20 22:30:54 +0000688 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
689}
690
Anders Carlsson08148092009-12-30 23:47:56 +0000691/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000692/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000693static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000694 unsigned Flags = 0;
695
696 if (Quals.hasConst())
697 Flags |= RTTIBuilder::PTI_Const;
698 if (Quals.hasVolatile())
699 Flags |= RTTIBuilder::PTI_Volatile;
700 if (Quals.hasRestrict())
701 Flags |= RTTIBuilder::PTI_Restrict;
702
703 return Flags;
704}
705
John McCalle8dc53e2010-08-12 02:17:33 +0000706/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
707/// for the given Objective-C object type.
708void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
709 // Drop qualifiers.
710 const Type *T = OT->getBaseType().getTypePtr();
711 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
712
713 // The builtin types are abi::__class_type_infos and don't require
714 // extra fields.
715 if (isa<BuiltinType>(T)) return;
716
717 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
718 ObjCInterfaceDecl *Super = Class->getSuperClass();
719
720 // Root classes are also __class_type_info.
721 if (!Super) return;
722
723 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
724
725 // Everything else is single inheritance.
726 llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
727 Fields.push_back(BaseTypeInfo);
728}
729
Anders Carlssonf64531a2009-12-30 01:00:12 +0000730/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
731/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
732void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
733 // Itanium C++ ABI 2.9.5p6b:
734 // It adds to abi::__class_type_info a single member pointing to the
735 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000736 llvm::Constant *BaseTypeInfo =
737 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
738 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000739}
740
Benjamin Kramer79ba2a62010-10-22 16:48:22 +0000741namespace {
742 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
743 /// a class hierarchy.
744 struct SeenBases {
745 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
746 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
747 };
748}
Anders Carlsson08148092009-12-30 23:47:56 +0000749
750/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
751/// abi::__vmi_class_type_info.
752///
753static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
754 SeenBases &Bases) {
755
756 unsigned Flags = 0;
757
758 const CXXRecordDecl *BaseDecl =
759 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
760
761 if (Base->isVirtual()) {
762 if (Bases.VirtualBases.count(BaseDecl)) {
763 // If this virtual base has been seen before, then the class is diamond
764 // shaped.
765 Flags |= RTTIBuilder::VMI_DiamondShaped;
766 } else {
767 if (Bases.NonVirtualBases.count(BaseDecl))
768 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
769
770 // Mark the virtual base as seen.
771 Bases.VirtualBases.insert(BaseDecl);
772 }
773 } else {
774 if (Bases.NonVirtualBases.count(BaseDecl)) {
775 // If this non-virtual base has been seen before, then the class has non-
776 // diamond shaped repeated inheritance.
777 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
778 } else {
779 if (Bases.VirtualBases.count(BaseDecl))
780 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
781
782 // Mark the non-virtual base as seen.
783 Bases.NonVirtualBases.insert(BaseDecl);
784 }
785 }
786
787 // Walk all bases.
788 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
789 E = BaseDecl->bases_end(); I != E; ++I)
790 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
791
792 return Flags;
793}
794
795static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
796 unsigned Flags = 0;
797 SeenBases Bases;
798
799 // Walk all bases.
800 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
801 E = RD->bases_end(); I != E; ++I)
802 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
803
804 return Flags;
805}
806
807/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
808/// classes with bases that do not satisfy the abi::__si_class_type_info
809/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
810void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
811 const llvm::Type *UnsignedIntLTy =
812 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
813
814 // Itanium C++ ABI 2.9.5p6c:
815 // __flags is a word with flags describing details about the class
816 // structure, which may be referenced by using the __flags_masks
817 // enumeration. These flags refer to both direct and indirect bases.
818 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000819 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000820
821 // Itanium C++ ABI 2.9.5p6c:
822 // __base_count is a word with the number of direct proper base class
823 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000824 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000825
826 if (!RD->getNumBases())
827 return;
828
829 const llvm::Type *LongLTy =
830 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
831
832 // Now add the base class descriptions.
833
834 // Itanium C++ ABI 2.9.5p6c:
835 // __base_info[] is an array of base class descriptions -- one for every
836 // direct proper base. Each description is of the type:
837 //
838 // struct abi::__base_class_type_info {
Eli Friedmana7e68452010-08-22 01:00:03 +0000839 // public:
Anders Carlsson08148092009-12-30 23:47:56 +0000840 // const __class_type_info *__base_type;
841 // long __offset_flags;
842 //
843 // enum __offset_flags_masks {
844 // __virtual_mask = 0x1,
845 // __public_mask = 0x2,
846 // __offset_shift = 8
847 // };
848 // };
849 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
850 E = RD->bases_end(); I != E; ++I) {
851 const CXXBaseSpecifier *Base = I;
852
853 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000854 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000855
856 const CXXRecordDecl *BaseDecl =
857 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
858
859 int64_t OffsetFlags = 0;
860
861 // All but the lower 8 bits of __offset_flags are a signed offset.
862 // For a non-virtual base, this is the offset in the object of the base
863 // subobject. For a virtual base, this is the offset in the virtual table of
864 // the virtual base offset for the virtual base referenced (negative).
865 if (Base->isVirtual())
Anders Carlssonaf440352010-03-23 04:11:45 +0000866 OffsetFlags = CGM.getVTables().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000867 else {
868 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Anders Carlssona14f5972010-10-31 23:22:37 +0000869 OffsetFlags = Layout.getBaseClassOffsetInBits(BaseDecl) / 8;
Anders Carlsson08148092009-12-30 23:47:56 +0000870 };
871
872 OffsetFlags <<= 8;
873
874 // The low-order byte of __offset_flags contains flags, as given by the
875 // masks from the enumeration __offset_flags_masks.
876 if (Base->isVirtual())
877 OffsetFlags |= BCTI_Virtual;
878 if (Base->getAccessSpecifier() == AS_public)
879 OffsetFlags |= BCTI_Public;
880
Anders Carlsson531d55f2009-12-31 17:43:53 +0000881 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000882 }
883}
884
Anders Carlsson8d145152009-12-20 22:30:54 +0000885/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
886/// used for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +0000887void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
Anders Carlssonabd6b092010-06-02 15:44:35 +0000888 Qualifiers Quals;
889 QualType UnqualifiedPointeeTy =
890 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
891
Anders Carlsson8d145152009-12-20 22:30:54 +0000892 // Itanium C++ ABI 2.9.5p7:
893 // __flags is a flag word describing the cv-qualification and other
894 // attributes of the type pointed to
Anders Carlssonabd6b092010-06-02 15:44:35 +0000895 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000896
897 // Itanium C++ ABI 2.9.5p7:
898 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
899 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000900 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000901 Flags |= PTI_Incomplete;
902
903 const llvm::Type *UnsignedIntLTy =
904 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000905 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000906
907 // Itanium C++ ABI 2.9.5p7:
908 // __pointee is a pointer to the std::type_info derivation for the
909 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000910 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000911 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000912 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000913}
914
915/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
916/// struct, used for member pointer types.
917void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
918 QualType PointeeTy = Ty->getPointeeType();
919
Anders Carlssonabd6b092010-06-02 15:44:35 +0000920 Qualifiers Quals;
921 QualType UnqualifiedPointeeTy =
922 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
923
Anders Carlsson8d145152009-12-20 22:30:54 +0000924 // Itanium C++ ABI 2.9.5p7:
925 // __flags is a flag word describing the cv-qualification and other
926 // attributes of the type pointed to.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000927 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000928
929 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000930
931 // Itanium C++ ABI 2.9.5p7:
932 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
933 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000934 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000935 Flags |= PTI_Incomplete;
936
Anders Carlsson8d145152009-12-20 22:30:54 +0000937 if (IsIncompleteClassType(ClassType))
938 Flags |= PTI_ContainingClassIncomplete;
939
Anders Carlsson8d145152009-12-20 22:30:54 +0000940 const llvm::Type *UnsignedIntLTy =
941 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000942 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000943
944 // Itanium C++ ABI 2.9.5p7:
945 // __pointee is a pointer to the std::type_info derivation for the
946 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000947 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000948 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000949 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000950
951 // Itanium C++ ABI 2.9.5p9:
952 // __context is a pointer to an abi::__class_type_info corresponding to the
953 // class type containing the member pointed to
954 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000955 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000956}
957
John McCall9dffe6f2010-04-30 01:15:21 +0000958llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
959 bool ForEH) {
960 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
961 // FIXME: should we even be calling this method if RTTI is disabled
962 // and it's not for EH?
963 if (!ForEH && !getContext().getLangOptions().RTTI) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000964 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
965 return llvm::Constant::getNullValue(Int8PtrTy);
966 }
John McCall9dffe6f2010-04-30 01:15:21 +0000967
Anders Carlsson531d55f2009-12-31 17:43:53 +0000968 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000969}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000970
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000971void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
972 QualType PointerType = Context.getPointerType(Type);
973 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
974 RTTIBuilder(*this).BuildTypeInfo(Type, true);
975 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
976 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
977}
978
979void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Anders Carlsson2bd62502010-11-04 05:28:09 +0000980 QualType FundamentalTypes[] = { Context.VoidTy, Context.NullPtrTy,
981 Context.BoolTy, Context.WCharTy,
982 Context.CharTy, Context.UnsignedCharTy,
983 Context.SignedCharTy, Context.ShortTy,
984 Context.UnsignedShortTy, Context.IntTy,
985 Context.UnsignedIntTy, Context.LongTy,
986 Context.UnsignedLongTy, Context.LongLongTy,
987 Context.UnsignedLongLongTy, Context.FloatTy,
988 Context.DoubleTy, Context.LongDoubleTy,
989 Context.Char16Ty, Context.Char32Ty };
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000990 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
991 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
992}