blob: 957b21893c5571854f20b6ae457018c0a45b810b [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);
Anders Carlsson907c8282011-01-29 22:10:32 +0000573 Fields.push_back(llvm::ConstantExpr::getBitCast(TypeName, Int8PtrTy));
John McCallcbfe5022010-08-04 08:34:44 +0000574
Anders Carlsson8d145152009-12-20 22:30:54 +0000575 switch (Ty->getTypeClass()) {
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000576#define TYPE(Class, Base)
577#define ABSTRACT_TYPE(Class, Base)
578#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
579#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
580#define DEPENDENT_TYPE(Class, Base) case Type::Class:
581#include "clang/AST/TypeNodes.def"
582 assert(false && "Non-canonical and dependent types shouldn't get here");
Anders Carlsson8d145152009-12-20 22:30:54 +0000583
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000584 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000585 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000586 case Type::Vector:
587 case Type::ExtVector:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000588 case Type::Complex:
589 case Type::BlockPointer:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000590 // Itanium C++ ABI 2.9.5p4:
591 // abi::__fundamental_type_info adds no data members to std::type_info.
592 break;
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000593
594 case Type::LValueReference:
595 case Type::RValueReference:
596 assert(false && "References shouldn't get here");
597
Anders Carlsson978ef682009-12-29 21:58:32 +0000598 case Type::ConstantArray:
599 case Type::IncompleteArray:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000600 case Type::VariableArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000601 // Itanium C++ ABI 2.9.5p5:
602 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000603 break;
604
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000605 case Type::FunctionNoProto:
606 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000607 // Itanium C++ ABI 2.9.5p5:
608 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000609 break;
610
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000611 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000612 // Itanium C++ ABI 2.9.5p5:
613 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000614 break;
615
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000616 case Type::Record: {
617 const CXXRecordDecl *RD =
618 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000619 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000620 // We don't need to emit any fields.
621 break;
622 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000623
Anders Carlsson08148092009-12-30 23:47:56 +0000624 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000625 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000626 else
627 BuildVMIClassTypeInfo(RD);
628
629 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000630 }
John McCalle8dc53e2010-08-12 02:17:33 +0000631
632 case Type::ObjCObject:
633 case Type::ObjCInterface:
634 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
635 break;
636
637 case Type::ObjCObjectPointer:
638 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
639 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000640
Anders Carlsson8d145152009-12-20 22:30:54 +0000641 case Type::Pointer:
John McCalle8dc53e2010-08-12 02:17:33 +0000642 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000643 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000644
Anders Carlsson8d145152009-12-20 22:30:54 +0000645 case Type::MemberPointer:
646 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
647 break;
648 }
649
650 llvm::Constant *Init =
Anders Carlsson531d55f2009-12-31 17:43:53 +0000651 llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
Anders Carlsson8d145152009-12-20 22:30:54 +0000652 /*Packed=*/false);
653
654 llvm::GlobalVariable *GV =
655 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
656 /*Constant=*/true, Linkage, Init, Name);
657
658 // If there's already an old global variable, replace it with the new one.
659 if (OldGV) {
660 GV->takeName(OldGV);
661 llvm::Constant *NewPtr =
662 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
663 OldGV->replaceAllUsesWith(NewPtr);
664 OldGV->eraseFromParent();
665 }
John McCallcbfe5022010-08-04 08:34:44 +0000666
667 // GCC only relies on the uniqueness of the type names, not the
668 // type_infos themselves, so we can emit these as hidden symbols.
John McCall279b5eb2010-08-12 23:36:15 +0000669 // But don't do this if we're worried about strict visibility
670 // compatibility.
Anders Carlsson9a86a132011-01-29 20:36:11 +0000671 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) {
672 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
673
674 CGM.setTypeVisibility(GV, RD, CodeGenModule::TVK_ForRTTI);
675 CGM.setTypeVisibility(TypeName, RD, CodeGenModule::TVK_ForRTTIName);
Anders Carlsson907c8282011-01-29 22:10:32 +0000676 } else {
677 Visibility TypeInfoVisibility = DefaultVisibility;
678 if (CGM.getCodeGenOpts().HiddenWeakVTables &&
679 Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
680 TypeInfoVisibility = HiddenVisibility;
Anders Carlsson9a86a132011-01-29 20:36:11 +0000681
Anders Carlsson907c8282011-01-29 22:10:32 +0000682 // The type name should have the same visibility as the type itself.
683 Visibility ExplicitVisibility = Ty->getVisibility();
684 TypeName->setVisibility(CodeGenModule::
685 GetLLVMVisibility(ExplicitVisibility));
686
687 TypeInfoVisibility = minVisibility(TypeInfoVisibility, Ty->getVisibility());
688 GV->setVisibility(CodeGenModule::GetLLVMVisibility(TypeInfoVisibility));
Rafael Espindolab1c65ff2011-01-11 21:44:37 +0000689 }
Anders Carlsson907c8282011-01-29 22:10:32 +0000690
Rafael Espindola57244f62011-01-11 23:55:05 +0000691 GV->setUnnamedAddr(true);
692
Anders Carlsson8d145152009-12-20 22:30:54 +0000693 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
694}
695
Anders Carlsson08148092009-12-30 23:47:56 +0000696/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000697/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000698static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000699 unsigned Flags = 0;
700
701 if (Quals.hasConst())
702 Flags |= RTTIBuilder::PTI_Const;
703 if (Quals.hasVolatile())
704 Flags |= RTTIBuilder::PTI_Volatile;
705 if (Quals.hasRestrict())
706 Flags |= RTTIBuilder::PTI_Restrict;
707
708 return Flags;
709}
710
John McCalle8dc53e2010-08-12 02:17:33 +0000711/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
712/// for the given Objective-C object type.
713void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
714 // Drop qualifiers.
715 const Type *T = OT->getBaseType().getTypePtr();
716 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
717
718 // The builtin types are abi::__class_type_infos and don't require
719 // extra fields.
720 if (isa<BuiltinType>(T)) return;
721
722 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
723 ObjCInterfaceDecl *Super = Class->getSuperClass();
724
725 // Root classes are also __class_type_info.
726 if (!Super) return;
727
728 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
729
730 // Everything else is single inheritance.
731 llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
732 Fields.push_back(BaseTypeInfo);
733}
734
Anders Carlssonf64531a2009-12-30 01:00:12 +0000735/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
736/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
737void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
738 // Itanium C++ ABI 2.9.5p6b:
739 // It adds to abi::__class_type_info a single member pointing to the
740 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000741 llvm::Constant *BaseTypeInfo =
742 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
743 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000744}
745
Benjamin Kramer79ba2a62010-10-22 16:48:22 +0000746namespace {
747 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
748 /// a class hierarchy.
749 struct SeenBases {
750 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
751 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
752 };
753}
Anders Carlsson08148092009-12-30 23:47:56 +0000754
755/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
756/// abi::__vmi_class_type_info.
757///
758static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
759 SeenBases &Bases) {
760
761 unsigned Flags = 0;
762
763 const CXXRecordDecl *BaseDecl =
764 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
765
766 if (Base->isVirtual()) {
767 if (Bases.VirtualBases.count(BaseDecl)) {
768 // If this virtual base has been seen before, then the class is diamond
769 // shaped.
770 Flags |= RTTIBuilder::VMI_DiamondShaped;
771 } else {
772 if (Bases.NonVirtualBases.count(BaseDecl))
773 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
774
775 // Mark the virtual base as seen.
776 Bases.VirtualBases.insert(BaseDecl);
777 }
778 } else {
779 if (Bases.NonVirtualBases.count(BaseDecl)) {
780 // If this non-virtual base has been seen before, then the class has non-
781 // diamond shaped repeated inheritance.
782 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
783 } else {
784 if (Bases.VirtualBases.count(BaseDecl))
785 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
786
787 // Mark the non-virtual base as seen.
788 Bases.NonVirtualBases.insert(BaseDecl);
789 }
790 }
791
792 // Walk all bases.
793 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
794 E = BaseDecl->bases_end(); I != E; ++I)
795 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
796
797 return Flags;
798}
799
800static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
801 unsigned Flags = 0;
802 SeenBases Bases;
803
804 // Walk all bases.
805 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
806 E = RD->bases_end(); I != E; ++I)
807 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
808
809 return Flags;
810}
811
812/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
813/// classes with bases that do not satisfy the abi::__si_class_type_info
814/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
815void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
816 const llvm::Type *UnsignedIntLTy =
817 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
818
819 // Itanium C++ ABI 2.9.5p6c:
820 // __flags is a word with flags describing details about the class
821 // structure, which may be referenced by using the __flags_masks
822 // enumeration. These flags refer to both direct and indirect bases.
823 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000824 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000825
826 // Itanium C++ ABI 2.9.5p6c:
827 // __base_count is a word with the number of direct proper base class
828 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000829 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000830
831 if (!RD->getNumBases())
832 return;
833
834 const llvm::Type *LongLTy =
835 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
836
837 // Now add the base class descriptions.
838
839 // Itanium C++ ABI 2.9.5p6c:
840 // __base_info[] is an array of base class descriptions -- one for every
841 // direct proper base. Each description is of the type:
842 //
843 // struct abi::__base_class_type_info {
Eli Friedmana7e68452010-08-22 01:00:03 +0000844 // public:
Anders Carlsson08148092009-12-30 23:47:56 +0000845 // const __class_type_info *__base_type;
846 // long __offset_flags;
847 //
848 // enum __offset_flags_masks {
849 // __virtual_mask = 0x1,
850 // __public_mask = 0x2,
851 // __offset_shift = 8
852 // };
853 // };
854 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
855 E = RD->bases_end(); I != E; ++I) {
856 const CXXBaseSpecifier *Base = I;
857
858 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000859 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000860
861 const CXXRecordDecl *BaseDecl =
862 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
863
864 int64_t OffsetFlags = 0;
865
866 // All but the lower 8 bits of __offset_flags are a signed offset.
867 // For a non-virtual base, this is the offset in the object of the base
868 // subobject. For a virtual base, this is the offset in the virtual table of
869 // the virtual base offset for the virtual base referenced (negative).
870 if (Base->isVirtual())
Anders Carlssonaf440352010-03-23 04:11:45 +0000871 OffsetFlags = CGM.getVTables().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000872 else {
873 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Anders Carlssona14f5972010-10-31 23:22:37 +0000874 OffsetFlags = Layout.getBaseClassOffsetInBits(BaseDecl) / 8;
Anders Carlsson08148092009-12-30 23:47:56 +0000875 };
876
877 OffsetFlags <<= 8;
878
879 // The low-order byte of __offset_flags contains flags, as given by the
880 // masks from the enumeration __offset_flags_masks.
881 if (Base->isVirtual())
882 OffsetFlags |= BCTI_Virtual;
883 if (Base->getAccessSpecifier() == AS_public)
884 OffsetFlags |= BCTI_Public;
885
Anders Carlsson531d55f2009-12-31 17:43:53 +0000886 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000887 }
888}
889
Anders Carlsson8d145152009-12-20 22:30:54 +0000890/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
891/// used for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +0000892void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
Anders Carlssonabd6b092010-06-02 15:44:35 +0000893 Qualifiers Quals;
894 QualType UnqualifiedPointeeTy =
895 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
896
Anders Carlsson8d145152009-12-20 22:30:54 +0000897 // Itanium C++ ABI 2.9.5p7:
898 // __flags is a flag word describing the cv-qualification and other
899 // attributes of the type pointed to
Anders Carlssonabd6b092010-06-02 15:44:35 +0000900 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000901
902 // Itanium C++ ABI 2.9.5p7:
903 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
904 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000905 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000906 Flags |= PTI_Incomplete;
907
908 const llvm::Type *UnsignedIntLTy =
909 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000910 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000911
912 // Itanium C++ ABI 2.9.5p7:
913 // __pointee is a pointer to the std::type_info derivation for the
914 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000915 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000916 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000917 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000918}
919
920/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
921/// struct, used for member pointer types.
922void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
923 QualType PointeeTy = Ty->getPointeeType();
924
Anders Carlssonabd6b092010-06-02 15:44:35 +0000925 Qualifiers Quals;
926 QualType UnqualifiedPointeeTy =
927 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
928
Anders Carlsson8d145152009-12-20 22:30:54 +0000929 // Itanium C++ ABI 2.9.5p7:
930 // __flags is a flag word describing the cv-qualification and other
931 // attributes of the type pointed to.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000932 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000933
934 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000935
936 // Itanium C++ ABI 2.9.5p7:
937 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
938 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000939 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000940 Flags |= PTI_Incomplete;
941
Anders Carlsson8d145152009-12-20 22:30:54 +0000942 if (IsIncompleteClassType(ClassType))
943 Flags |= PTI_ContainingClassIncomplete;
944
Anders Carlsson8d145152009-12-20 22:30:54 +0000945 const llvm::Type *UnsignedIntLTy =
946 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000947 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000948
949 // Itanium C++ ABI 2.9.5p7:
950 // __pointee is a pointer to the std::type_info derivation for the
951 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000952 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000953 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000954 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000955
956 // Itanium C++ ABI 2.9.5p9:
957 // __context is a pointer to an abi::__class_type_info corresponding to the
958 // class type containing the member pointed to
959 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000960 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000961}
962
John McCall9dffe6f2010-04-30 01:15:21 +0000963llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
964 bool ForEH) {
965 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
966 // FIXME: should we even be calling this method if RTTI is disabled
967 // and it's not for EH?
968 if (!ForEH && !getContext().getLangOptions().RTTI) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000969 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
970 return llvm::Constant::getNullValue(Int8PtrTy);
971 }
John McCall9dffe6f2010-04-30 01:15:21 +0000972
Anders Carlsson531d55f2009-12-31 17:43:53 +0000973 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000974}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000975
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000976void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
977 QualType PointerType = Context.getPointerType(Type);
978 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
979 RTTIBuilder(*this).BuildTypeInfo(Type, true);
980 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
981 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
982}
983
984void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Anders Carlsson2bd62502010-11-04 05:28:09 +0000985 QualType FundamentalTypes[] = { Context.VoidTy, Context.NullPtrTy,
986 Context.BoolTy, Context.WCharTy,
987 Context.CharTy, Context.UnsignedCharTy,
988 Context.SignedCharTy, Context.ShortTy,
989 Context.UnsignedShortTy, Context.IntTy,
990 Context.UnsignedIntTy, Context.LongTy,
991 Context.UnsignedLongTy, Context.LongLongTy,
992 Context.UnsignedLongLongTy, Context.FloatTy,
993 Context.DoubleTy, Context.LongDoubleTy,
994 Context.Char16Ty, Context.Char32Ty };
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000995 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
996 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
997}