blob: 444062bbfff31f0e0b2eb0ada62287d35a4d2f67 [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 Carlsson1d7088d2009-12-17 07:09:17 +000033 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
34 /// descriptor of the given type.
35 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
36
Anders Carlsson046c2942010-04-17 20:15:18 +000037 /// BuildVTablePointer - Build the vtable pointer for the given type.
38 void BuildVTablePointer(const Type *Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +000039
Anders Carlssonf64531a2009-12-30 01:00:12 +000040 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
Anders Carlsson08148092009-12-30 23:47:56 +000041 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
Anders Carlssonf64531a2009-12-30 01:00:12 +000042 void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
43
Anders Carlsson08148092009-12-30 23:47:56 +000044 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
45 /// classes with bases that do not satisfy the abi::__si_class_type_info
46 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
47 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
48
Anders Carlssonf64531a2009-12-30 01:00:12 +000049 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
50 /// for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +000051 void BuildPointerTypeInfo(QualType PointeeTy);
52
53 /// BuildObjCObjectTypeInfo - Build the appropriate kind of
54 /// type_info for an object type.
55 void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +000056
57 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
58 /// struct, used for member pointer types.
59 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
60
Mike Stump2b1bf312009-11-14 14:25:18 +000061public:
Mike Stumpde050572009-12-02 18:57:08 +000062 RTTIBuilder(CodeGenModule &cgm)
Mike Stump2b1bf312009-11-14 14:25:18 +000063 : CGM(cgm), VMContext(cgm.getModule().getContext()),
64 Int8PtrTy(llvm::Type::getInt8PtrTy(VMContext)) { }
65
Anders Carlsson31b7f522009-12-11 02:46:30 +000066 llvm::Constant *BuildName(QualType Ty, bool Hidden,
67 llvm::GlobalVariable::LinkageTypes Linkage) {
Mike Stump2b1bf312009-11-14 14:25:18 +000068 llvm::SmallString<256> OutName;
John McCall4c40d982010-08-31 07:33:07 +000069 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, OutName);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +000070 llvm::StringRef Name = OutName.str();
Mike Stumpcbcd4e52009-11-14 23:32:21 +000071
Anders Carlsson8d145152009-12-20 22:30:54 +000072 llvm::GlobalVariable *OGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +000073 if (OGV && !OGV->isDeclaration())
74 return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy);
Mike Stump58588942009-11-19 01:08:19 +000075
Anders Carlsson31b7f522009-12-11 02:46:30 +000076 llvm::Constant *C = llvm::ConstantArray::get(VMContext, Name.substr(4));
Mike Stump2b1bf312009-11-14 14:25:18 +000077
Anders Carlsson31b7f522009-12-11 02:46:30 +000078 llvm::GlobalVariable *GV =
79 new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
80 C, Name);
Mike Stump58588942009-11-19 01:08:19 +000081 if (OGV) {
82 GV->takeName(OGV);
83 llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
84 OGV->getType());
85 OGV->replaceAllUsesWith(NewPtr);
86 OGV->eraseFromParent();
87 }
Argyrios Kyrtzidis6d576052010-10-11 03:25:53 +000088 if (Hidden && Linkage != llvm::GlobalValue::InternalLinkage)
Mike Stump582b0372009-11-18 03:46:51 +000089 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
90 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Daniel Dunbar7177dee2009-12-19 17:50:07 +000091 }
Mike Stumpc7a05bd2009-11-14 15:55:18 +000092
Mike Stump4e6f8ee2009-12-24 02:33:48 +000093 // FIXME: unify with DecideExtern
Mike Stump58588942009-11-19 01:08:19 +000094 bool DecideHidden(QualType Ty) {
95 // For this type, see if all components are never hidden.
96 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>())
97 return (DecideHidden(MPT->getPointeeType())
98 && DecideHidden(QualType(MPT->getClass(), 0)));
99 if (const PointerType *PT = Ty->getAs<PointerType>())
100 return DecideHidden(PT->getPointeeType());
Mike Stump4e6f8ee2009-12-24 02:33:48 +0000101 if (const FunctionType *FT = Ty->getAs<FunctionType>()) {
102 if (DecideHidden(FT->getResultType()) == false)
103 return false;
104 if (const FunctionProtoType *FPT = Ty->getAs<FunctionProtoType>()) {
105 for (unsigned i = 0; i <FPT->getNumArgs(); ++i)
106 if (DecideHidden(FPT->getArgType(i)) == false)
107 return false;
108 for (unsigned i = 0; i <FPT->getNumExceptions(); ++i)
109 if (DecideHidden(FPT->getExceptionType(i)) == false)
110 return false;
111 return true;
112 }
113 }
Mike Stump58588942009-11-19 01:08:19 +0000114 if (const RecordType *RT = Ty->getAs<RecordType>())
115 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
John McCall1fb0caa2010-10-22 21:05:15 +0000116 return RD->getVisibility() == HiddenVisibility;
Mike Stump58588942009-11-19 01:08:19 +0000117 return false;
118 }
Anders Carlsson31b7f522009-12-11 02:46:30 +0000119
Anders Carlsson8d145152009-12-20 22:30:54 +0000120 // Pointer type info flags.
121 enum {
122 /// PTI_Const - Type has const qualifier.
123 PTI_Const = 0x1,
124
125 /// PTI_Volatile - Type has volatile qualifier.
126 PTI_Volatile = 0x2,
127
128 /// PTI_Restrict - Type has restrict qualifier.
129 PTI_Restrict = 0x4,
130
131 /// PTI_Incomplete - Type is incomplete.
132 PTI_Incomplete = 0x8,
133
134 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
135 /// (in pointer to member).
136 PTI_ContainingClassIncomplete = 0x10
137 };
Anders Carlsson08148092009-12-30 23:47:56 +0000138
139 // VMI type info flags.
140 enum {
141 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
142 VMI_NonDiamondRepeat = 0x1,
143
144 /// VMI_DiamondShaped - Class is diamond shaped.
145 VMI_DiamondShaped = 0x2
146 };
147
148 // Base class type info flags.
149 enum {
150 /// BCTI_Virtual - Base class is virtual.
151 BCTI_Virtual = 0x1,
152
153 /// BCTI_Public - Base class is public.
154 BCTI_Public = 0x2
155 };
Anders Carlsson531d55f2009-12-31 17:43:53 +0000156
157 /// BuildTypeInfo - Build the RTTI type info struct for the given type.
John McCall9dffe6f2010-04-30 01:15:21 +0000158 ///
159 /// \param Force - true to force the creation of this RTTI value
160 /// \param ForEH - true if this is for exception handling
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000161 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false);
Mike Stump2b1bf312009-11-14 14:25:18 +0000162};
Mike Stump92f2fe22009-12-02 19:07:44 +0000163}
Mike Stump2b1bf312009-11-14 14:25:18 +0000164
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000165llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
166 // Mangle the RTTI name.
167 llvm::SmallString<256> OutName;
John McCall4c40d982010-08-31 07:33:07 +0000168 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, OutName);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000169 llvm::StringRef Name = OutName.str();
170
Anders Carlsson8d145152009-12-20 22:30:54 +0000171 // Look for an existing global.
172 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000173
174 if (!GV) {
175 // Create a new global variable.
176 GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
177 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000178 }
179
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000180 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000181}
182
Anders Carlsson8d145152009-12-20 22:30:54 +0000183/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
184/// info for that type is defined in the standard library.
185static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
186 // Itanium C++ ABI 2.9.2:
187 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
188 // the run-time support library. Specifically, the run-time support
189 // library should contain type_info objects for the types X, X* and
Anders Carlsson2bd62502010-11-04 05:28:09 +0000190 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
191 // unsigned char, signed char, short, unsigned short, int, unsigned int,
192 // long, unsigned long, long long, unsigned long long, float, double,
193 // long double, char16_t, char32_t, and the IEEE 754r decimal and
194 // half-precision floating point types.
Anders Carlsson8d145152009-12-20 22:30:54 +0000195 switch (Ty->getKind()) {
196 case BuiltinType::Void:
Anders Carlsson2bd62502010-11-04 05:28:09 +0000197 case BuiltinType::NullPtr:
Anders Carlsson8d145152009-12-20 22:30:54 +0000198 case BuiltinType::Bool:
Chris Lattner3f59c972010-12-25 23:25:43 +0000199 case BuiltinType::WChar_S:
200 case BuiltinType::WChar_U:
Anders Carlsson8d145152009-12-20 22:30:54 +0000201 case BuiltinType::Char_U:
202 case BuiltinType::Char_S:
203 case BuiltinType::UChar:
204 case BuiltinType::SChar:
205 case BuiltinType::Short:
206 case BuiltinType::UShort:
207 case BuiltinType::Int:
208 case BuiltinType::UInt:
209 case BuiltinType::Long:
210 case BuiltinType::ULong:
211 case BuiltinType::LongLong:
212 case BuiltinType::ULongLong:
213 case BuiltinType::Float:
214 case BuiltinType::Double:
215 case BuiltinType::LongDouble:
216 case BuiltinType::Char16:
217 case BuiltinType::Char32:
218 case BuiltinType::Int128:
219 case BuiltinType::UInt128:
220 return true;
221
222 case BuiltinType::Overload:
223 case BuiltinType::Dependent:
224 case BuiltinType::UndeducedAuto:
225 assert(false && "Should not see this type here!");
226
Anders Carlsson8d145152009-12-20 22:30:54 +0000227 case BuiltinType::ObjCId:
228 case BuiltinType::ObjCClass:
229 case BuiltinType::ObjCSel:
230 assert(false && "FIXME: Objective-C types are unsupported!");
231 }
232
233 // Silent gcc.
234 return false;
235}
236
237static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
238 QualType PointeeTy = PointerTy->getPointeeType();
239 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
240 if (!BuiltinTy)
241 return false;
242
243 // Check the qualifiers.
244 Qualifiers Quals = PointeeTy.getQualifiers();
245 Quals.removeConst();
246
247 if (!Quals.empty())
248 return false;
249
250 return TypeInfoIsInStandardLibrary(BuiltinTy);
251}
252
John McCallcbfe5022010-08-04 08:34:44 +0000253/// IsStandardLibraryRTTIDescriptor - Returns whether the type
254/// information for the given type exists in the standard library.
255static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000256 // Type info for builtin types is defined in the standard library.
257 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
258 return TypeInfoIsInStandardLibrary(BuiltinTy);
259
260 // Type info for some pointer types to builtin types is defined in the
261 // standard library.
262 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
263 return TypeInfoIsInStandardLibrary(PointerTy);
264
John McCallcbfe5022010-08-04 08:34:44 +0000265 return false;
266}
267
268/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
269/// the given type exists somewhere else, and that we should not emit the type
270/// information in this translation unit. Assumes that it is not a
271/// standard-library type.
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000272static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM, QualType Ty) {
273 ASTContext &Context = CGM.getContext();
274
John McCall9dffe6f2010-04-30 01:15:21 +0000275 // If RTTI is disabled, don't consider key functions.
276 if (!Context.getLangOptions().RTTI) return false;
277
Anders Carlsson8d145152009-12-20 22:30:54 +0000278 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000279 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000280 if (!RD->hasDefinition())
281 return false;
282
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000283 if (!RD->isDynamicClass())
284 return false;
285
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000286 return !CGM.getVTables().ShouldEmitVTableInThisTU(RD);
Anders Carlsson8d145152009-12-20 22:30:54 +0000287 }
288
289 return false;
290}
291
292/// IsIncompleteClassType - Returns whether the given record type is incomplete.
293static bool IsIncompleteClassType(const RecordType *RecordTy) {
294 return !RecordTy->getDecl()->isDefinition();
295}
296
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000297/// ContainsIncompleteClassType - Returns whether the given type contains an
298/// incomplete class type. This is true if
299///
300/// * The given type is an incomplete class type.
301/// * The given type is a pointer type whose pointee type contains an
302/// incomplete class type.
303/// * The given type is a member pointer type whose class is an incomplete
304/// class type.
305/// * The given type is a member pointer type whoise pointee type contains an
306/// incomplete class type.
Anders Carlsson8d145152009-12-20 22:30:54 +0000307/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000308static bool ContainsIncompleteClassType(QualType Ty) {
309 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
310 if (IsIncompleteClassType(RecordTy))
311 return true;
312 }
313
314 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
315 return ContainsIncompleteClassType(PointerTy->getPointeeType());
316
317 if (const MemberPointerType *MemberPointerTy =
318 dyn_cast<MemberPointerType>(Ty)) {
319 // Check if the class type is incomplete.
320 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
321 if (IsIncompleteClassType(ClassType))
322 return true;
323
324 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000325 }
326
327 return false;
328}
329
330/// getTypeInfoLinkage - Return the linkage that the type info and type info
331/// name constants should have for the given type.
332static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(QualType Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000333 // Itanium C++ ABI 2.9.5p7:
334 // In addition, it and all of the intermediate abi::__pointer_type_info
335 // structs in the chain down to the abi::__class_type_info for the
336 // incomplete class type must be prevented from resolving to the
337 // corresponding type_info structs for the complete class type, possibly
338 // by making them local static objects. Finally, a dummy class RTTI is
339 // generated for the incomplete type that will not resolve to the final
340 // complete class RTTI (because the latter need not exist), possibly by
341 // making it a local static object.
342 if (ContainsIncompleteClassType(Ty))
343 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000344
Douglas Gregor031b3712010-03-31 00:15:35 +0000345 switch (Ty->getLinkage()) {
346 case NoLinkage:
347 case InternalLinkage:
348 case UniqueExternalLinkage:
349 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson978ef682009-12-29 21:58:32 +0000350
Douglas Gregor031b3712010-03-31 00:15:35 +0000351 case ExternalLinkage:
352 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
353 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
354 if (RD->isDynamicClass())
Anders Carlsson046c2942010-04-17 20:15:18 +0000355 return CodeGenModule::getVTableLinkage(RD);
Mike Stumpc8f76f52009-12-24 01:10:27 +0000356 }
Douglas Gregor031b3712010-03-31 00:15:35 +0000357
Mike Stumpc8f76f52009-12-24 01:10:27 +0000358 return llvm::GlobalValue::WeakODRLinkage;
359 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000360
Anders Carlsson8d145152009-12-20 22:30:54 +0000361 return llvm::GlobalValue::WeakODRLinkage;
362}
363
Anders Carlssonf64531a2009-12-30 01:00:12 +0000364// CanUseSingleInheritance - Return whether the given record decl has a "single,
365// public, non-virtual base at offset zero (i.e. the derived class is dynamic
366// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
367static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
368 // Check the number of bases.
369 if (RD->getNumBases() != 1)
370 return false;
371
372 // Get the base.
373 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
374
375 // Check that the base is not virtual.
376 if (Base->isVirtual())
377 return false;
378
379 // Check that the base is public.
380 if (Base->getAccessSpecifier() != AS_public)
381 return false;
382
383 // Check that the class is dynamic iff the base is.
384 const CXXRecordDecl *BaseDecl =
385 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
386 if (!BaseDecl->isEmpty() &&
387 BaseDecl->isDynamicClass() != RD->isDynamicClass())
388 return false;
389
390 return true;
391}
392
Anders Carlsson046c2942010-04-17 20:15:18 +0000393void RTTIBuilder::BuildVTablePointer(const Type *Ty) {
John McCalle8dc53e2010-08-12 02:17:33 +0000394 // abi::__class_type_info.
395 static const char * const ClassTypeInfo =
396 "_ZTVN10__cxxabiv117__class_type_infoE";
397 // abi::__si_class_type_info.
398 static const char * const SIClassTypeInfo =
399 "_ZTVN10__cxxabiv120__si_class_type_infoE";
400 // abi::__vmi_class_type_info.
401 static const char * const VMIClassTypeInfo =
402 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
403
Eli Friedman1cf26f52010-08-11 20:41:51 +0000404 const char *VTableName = 0;
Anders Carlsson8d145152009-12-20 22:30:54 +0000405
406 switch (Ty->getTypeClass()) {
Eli Friedman1cf26f52010-08-11 20:41:51 +0000407#define TYPE(Class, Base)
408#define ABSTRACT_TYPE(Class, Base)
409#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
410#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
411#define DEPENDENT_TYPE(Class, Base) case Type::Class:
412#include "clang/AST/TypeNodes.def"
413 assert(false && "Non-canonical and dependent types shouldn't get here");
414
415 case Type::LValueReference:
416 case Type::RValueReference:
417 assert(false && "References shouldn't get here");
Anders Carlsson978ef682009-12-29 21:58:32 +0000418
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000419 case Type::Builtin:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000420 // GCC treats vector and complex types as fundamental types.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000421 case Type::Vector:
422 case Type::ExtVector:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000423 case Type::Complex:
424 // FIXME: GCC treats block pointers as fundamental types?!
425 case Type::BlockPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000426 // abi::__fundamental_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000427 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000428 break;
429
Anders Carlsson978ef682009-12-29 21:58:32 +0000430 case Type::ConstantArray:
431 case Type::IncompleteArray:
Eli Friedman1cf26f52010-08-11 20:41:51 +0000432 case Type::VariableArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000433 // abi::__array_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000434 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000435 break;
436
437 case Type::FunctionNoProto:
438 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000439 // abi::__function_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000440 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000441 break;
442
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000443 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000444 // abi::__enum_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000445 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000446 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000447
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000448 case Type::Record: {
449 const CXXRecordDecl *RD =
450 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000451
John McCall86ff3082010-02-04 22:26:26 +0000452 if (!RD->hasDefinition() || !RD->getNumBases()) {
John McCalle8dc53e2010-08-12 02:17:33 +0000453 VTableName = ClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000454 } else if (CanUseSingleInheritance(RD)) {
John McCalle8dc53e2010-08-12 02:17:33 +0000455 VTableName = SIClassTypeInfo;
Anders Carlssonf64531a2009-12-30 01:00:12 +0000456 } else {
John McCalle8dc53e2010-08-12 02:17:33 +0000457 VTableName = VMIClassTypeInfo;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000458 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000459
460 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000461 }
462
Eli Friedman1cf26f52010-08-11 20:41:51 +0000463 case Type::ObjCObject:
John McCalle8dc53e2010-08-12 02:17:33 +0000464 // Ignore protocol qualifiers.
465 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
466
467 // Handle id and Class.
468 if (isa<BuiltinType>(Ty)) {
469 VTableName = ClassTypeInfo;
470 break;
471 }
472
473 assert(isa<ObjCInterfaceType>(Ty));
474 // Fall through.
475
Eli Friedman1cf26f52010-08-11 20:41:51 +0000476 case Type::ObjCInterface:
John McCalle8dc53e2010-08-12 02:17:33 +0000477 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
478 VTableName = SIClassTypeInfo;
479 } else {
480 VTableName = ClassTypeInfo;
481 }
Eli Friedman1cf26f52010-08-11 20:41:51 +0000482 break;
483
John McCalle8dc53e2010-08-12 02:17:33 +0000484 case Type::ObjCObjectPointer:
Anders Carlsson8d145152009-12-20 22:30:54 +0000485 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000486 // abi::__pointer_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000487 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000488 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000489
Anders Carlsson8d145152009-12-20 22:30:54 +0000490 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000491 // abi::__pointer_to_member_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000492 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000493 break;
494 }
495
Anders Carlsson046c2942010-04-17 20:15:18 +0000496 llvm::Constant *VTable =
497 CGM.getModule().getOrInsertGlobal(VTableName, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000498
499 const llvm::Type *PtrDiffTy =
500 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
501
502 // The vtable address point is 2.
503 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Anders Carlsson046c2942010-04-17 20:15:18 +0000504 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, &Two, 1);
505 VTable = llvm::ConstantExpr::getBitCast(VTable, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000506
Anders Carlsson046c2942010-04-17 20:15:18 +0000507 Fields.push_back(VTable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000508}
509
John McCallcbfe5022010-08-04 08:34:44 +0000510llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000511 // We want to operate on the canonical type.
512 Ty = CGM.getContext().getCanonicalType(Ty);
513
514 // Check if we've already emitted an RTTI descriptor for this type.
515 llvm::SmallString<256> OutName;
John McCall4c40d982010-08-31 07:33:07 +0000516 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, OutName);
Anders Carlsson8d145152009-12-20 22:30:54 +0000517 llvm::StringRef Name = OutName.str();
518
519 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
520 if (OldGV && !OldGV->isDeclaration())
521 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
John McCallcbfe5022010-08-04 08:34:44 +0000522
Anders Carlsson8d145152009-12-20 22:30:54 +0000523 // Check if there is already an external RTTI descriptor for this type.
John McCallcbfe5022010-08-04 08:34:44 +0000524 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
Argyrios Kyrtzidisd2c47bd2010-10-11 03:25:57 +0000525 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
Anders Carlsson8d145152009-12-20 22:30:54 +0000526 return GetAddrOfExternalRTTIDescriptor(Ty);
527
John McCallcbfe5022010-08-04 08:34:44 +0000528 // Emit the standard library with external linkage.
529 llvm::GlobalVariable::LinkageTypes Linkage;
530 if (IsStdLib)
531 Linkage = llvm::GlobalValue::ExternalLinkage;
532 else
533 Linkage = getTypeInfoLinkage(Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +0000534
535 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000536 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000537
538 // And the name.
John McCall9c39acf2010-12-17 02:58:03 +0000539 bool Hidden = DecideHidden(Ty);
540 Fields.push_back(BuildName(Ty, Hidden, Linkage));
John McCallcbfe5022010-08-04 08:34:44 +0000541
Anders Carlsson8d145152009-12-20 22:30:54 +0000542 switch (Ty->getTypeClass()) {
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000543#define TYPE(Class, Base)
544#define ABSTRACT_TYPE(Class, Base)
545#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
546#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
547#define DEPENDENT_TYPE(Class, Base) case Type::Class:
548#include "clang/AST/TypeNodes.def"
549 assert(false && "Non-canonical and dependent types shouldn't get here");
Anders Carlsson8d145152009-12-20 22:30:54 +0000550
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000551 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000552 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000553 case Type::Vector:
554 case Type::ExtVector:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000555 case Type::Complex:
556 case Type::BlockPointer:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000557 // Itanium C++ ABI 2.9.5p4:
558 // abi::__fundamental_type_info adds no data members to std::type_info.
559 break;
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000560
561 case Type::LValueReference:
562 case Type::RValueReference:
563 assert(false && "References shouldn't get here");
564
Anders Carlsson978ef682009-12-29 21:58:32 +0000565 case Type::ConstantArray:
566 case Type::IncompleteArray:
Eli Friedmanf2aabe12010-08-15 00:24:31 +0000567 case Type::VariableArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000568 // Itanium C++ ABI 2.9.5p5:
569 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000570 break;
571
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000572 case Type::FunctionNoProto:
573 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000574 // Itanium C++ ABI 2.9.5p5:
575 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000576 break;
577
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000578 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000579 // Itanium C++ ABI 2.9.5p5:
580 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000581 break;
582
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000583 case Type::Record: {
584 const CXXRecordDecl *RD =
585 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000586 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000587 // We don't need to emit any fields.
588 break;
589 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000590
Anders Carlsson08148092009-12-30 23:47:56 +0000591 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000592 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000593 else
594 BuildVMIClassTypeInfo(RD);
595
596 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000597 }
John McCalle8dc53e2010-08-12 02:17:33 +0000598
599 case Type::ObjCObject:
600 case Type::ObjCInterface:
601 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
602 break;
603
604 case Type::ObjCObjectPointer:
605 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
606 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000607
Anders Carlsson8d145152009-12-20 22:30:54 +0000608 case Type::Pointer:
John McCalle8dc53e2010-08-12 02:17:33 +0000609 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000610 break;
John McCalle8dc53e2010-08-12 02:17:33 +0000611
Anders Carlsson8d145152009-12-20 22:30:54 +0000612 case Type::MemberPointer:
613 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
614 break;
615 }
616
617 llvm::Constant *Init =
Anders Carlsson531d55f2009-12-31 17:43:53 +0000618 llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
Anders Carlsson8d145152009-12-20 22:30:54 +0000619 /*Packed=*/false);
620
621 llvm::GlobalVariable *GV =
622 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
623 /*Constant=*/true, Linkage, Init, Name);
624
625 // If there's already an old global variable, replace it with the new one.
626 if (OldGV) {
627 GV->takeName(OldGV);
628 llvm::Constant *NewPtr =
629 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
630 OldGV->replaceAllUsesWith(NewPtr);
631 OldGV->eraseFromParent();
632 }
John McCallcbfe5022010-08-04 08:34:44 +0000633
634 // GCC only relies on the uniqueness of the type names, not the
635 // type_infos themselves, so we can emit these as hidden symbols.
John McCall279b5eb2010-08-12 23:36:15 +0000636 // But don't do this if we're worried about strict visibility
637 // compatibility.
John McCallcbfe5022010-08-04 08:34:44 +0000638 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
639 CGM.setTypeVisibility(GV, cast<CXXRecordDecl>(RT->getDecl()),
John McCallaf146032010-10-30 11:50:40 +0000640 /*ForRTTI*/ true, /*ForDefinition*/ true);
John McCall9c39acf2010-12-17 02:58:03 +0000641 else if (Hidden ||
642 (CGM.getCodeGenOpts().HiddenWeakVTables &&
643 Linkage == llvm::GlobalValue::WeakODRLinkage))
John McCallcbfe5022010-08-04 08:34:44 +0000644 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
645
Anders Carlsson8d145152009-12-20 22:30:54 +0000646 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
647}
648
Anders Carlsson08148092009-12-30 23:47:56 +0000649/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000650/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000651static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000652 unsigned Flags = 0;
653
654 if (Quals.hasConst())
655 Flags |= RTTIBuilder::PTI_Const;
656 if (Quals.hasVolatile())
657 Flags |= RTTIBuilder::PTI_Volatile;
658 if (Quals.hasRestrict())
659 Flags |= RTTIBuilder::PTI_Restrict;
660
661 return Flags;
662}
663
John McCalle8dc53e2010-08-12 02:17:33 +0000664/// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
665/// for the given Objective-C object type.
666void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
667 // Drop qualifiers.
668 const Type *T = OT->getBaseType().getTypePtr();
669 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
670
671 // The builtin types are abi::__class_type_infos and don't require
672 // extra fields.
673 if (isa<BuiltinType>(T)) return;
674
675 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
676 ObjCInterfaceDecl *Super = Class->getSuperClass();
677
678 // Root classes are also __class_type_info.
679 if (!Super) return;
680
681 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
682
683 // Everything else is single inheritance.
684 llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
685 Fields.push_back(BaseTypeInfo);
686}
687
Anders Carlssonf64531a2009-12-30 01:00:12 +0000688/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
689/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
690void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
691 // Itanium C++ ABI 2.9.5p6b:
692 // It adds to abi::__class_type_info a single member pointing to the
693 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000694 llvm::Constant *BaseTypeInfo =
695 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
696 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000697}
698
Benjamin Kramer79ba2a62010-10-22 16:48:22 +0000699namespace {
700 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
701 /// a class hierarchy.
702 struct SeenBases {
703 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
704 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
705 };
706}
Anders Carlsson08148092009-12-30 23:47:56 +0000707
708/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
709/// abi::__vmi_class_type_info.
710///
711static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
712 SeenBases &Bases) {
713
714 unsigned Flags = 0;
715
716 const CXXRecordDecl *BaseDecl =
717 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
718
719 if (Base->isVirtual()) {
720 if (Bases.VirtualBases.count(BaseDecl)) {
721 // If this virtual base has been seen before, then the class is diamond
722 // shaped.
723 Flags |= RTTIBuilder::VMI_DiamondShaped;
724 } else {
725 if (Bases.NonVirtualBases.count(BaseDecl))
726 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
727
728 // Mark the virtual base as seen.
729 Bases.VirtualBases.insert(BaseDecl);
730 }
731 } else {
732 if (Bases.NonVirtualBases.count(BaseDecl)) {
733 // If this non-virtual base has been seen before, then the class has non-
734 // diamond shaped repeated inheritance.
735 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
736 } else {
737 if (Bases.VirtualBases.count(BaseDecl))
738 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
739
740 // Mark the non-virtual base as seen.
741 Bases.NonVirtualBases.insert(BaseDecl);
742 }
743 }
744
745 // Walk all bases.
746 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
747 E = BaseDecl->bases_end(); I != E; ++I)
748 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
749
750 return Flags;
751}
752
753static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
754 unsigned Flags = 0;
755 SeenBases Bases;
756
757 // Walk all bases.
758 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
759 E = RD->bases_end(); I != E; ++I)
760 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
761
762 return Flags;
763}
764
765/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
766/// classes with bases that do not satisfy the abi::__si_class_type_info
767/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
768void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
769 const llvm::Type *UnsignedIntLTy =
770 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
771
772 // Itanium C++ ABI 2.9.5p6c:
773 // __flags is a word with flags describing details about the class
774 // structure, which may be referenced by using the __flags_masks
775 // enumeration. These flags refer to both direct and indirect bases.
776 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000777 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000778
779 // Itanium C++ ABI 2.9.5p6c:
780 // __base_count is a word with the number of direct proper base class
781 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000782 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000783
784 if (!RD->getNumBases())
785 return;
786
787 const llvm::Type *LongLTy =
788 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
789
790 // Now add the base class descriptions.
791
792 // Itanium C++ ABI 2.9.5p6c:
793 // __base_info[] is an array of base class descriptions -- one for every
794 // direct proper base. Each description is of the type:
795 //
796 // struct abi::__base_class_type_info {
Eli Friedmana7e68452010-08-22 01:00:03 +0000797 // public:
Anders Carlsson08148092009-12-30 23:47:56 +0000798 // const __class_type_info *__base_type;
799 // long __offset_flags;
800 //
801 // enum __offset_flags_masks {
802 // __virtual_mask = 0x1,
803 // __public_mask = 0x2,
804 // __offset_shift = 8
805 // };
806 // };
807 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
808 E = RD->bases_end(); I != E; ++I) {
809 const CXXBaseSpecifier *Base = I;
810
811 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000812 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000813
814 const CXXRecordDecl *BaseDecl =
815 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
816
817 int64_t OffsetFlags = 0;
818
819 // All but the lower 8 bits of __offset_flags are a signed offset.
820 // For a non-virtual base, this is the offset in the object of the base
821 // subobject. For a virtual base, this is the offset in the virtual table of
822 // the virtual base offset for the virtual base referenced (negative).
823 if (Base->isVirtual())
Anders Carlssonaf440352010-03-23 04:11:45 +0000824 OffsetFlags = CGM.getVTables().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000825 else {
826 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
Anders Carlssona14f5972010-10-31 23:22:37 +0000827 OffsetFlags = Layout.getBaseClassOffsetInBits(BaseDecl) / 8;
Anders Carlsson08148092009-12-30 23:47:56 +0000828 };
829
830 OffsetFlags <<= 8;
831
832 // The low-order byte of __offset_flags contains flags, as given by the
833 // masks from the enumeration __offset_flags_masks.
834 if (Base->isVirtual())
835 OffsetFlags |= BCTI_Virtual;
836 if (Base->getAccessSpecifier() == AS_public)
837 OffsetFlags |= BCTI_Public;
838
Anders Carlsson531d55f2009-12-31 17:43:53 +0000839 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000840 }
841}
842
Anders Carlsson8d145152009-12-20 22:30:54 +0000843/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
844/// used for pointer types.
John McCalle8dc53e2010-08-12 02:17:33 +0000845void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
Anders Carlssonabd6b092010-06-02 15:44:35 +0000846 Qualifiers Quals;
847 QualType UnqualifiedPointeeTy =
848 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
849
Anders Carlsson8d145152009-12-20 22:30:54 +0000850 // Itanium C++ ABI 2.9.5p7:
851 // __flags is a flag word describing the cv-qualification and other
852 // attributes of the type pointed to
Anders Carlssonabd6b092010-06-02 15:44:35 +0000853 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000854
855 // Itanium C++ ABI 2.9.5p7:
856 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
857 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000858 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000859 Flags |= PTI_Incomplete;
860
861 const llvm::Type *UnsignedIntLTy =
862 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000863 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000864
865 // Itanium C++ ABI 2.9.5p7:
866 // __pointee is a pointer to the std::type_info derivation for the
867 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000868 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000869 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000870 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000871}
872
873/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
874/// struct, used for member pointer types.
875void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
876 QualType PointeeTy = Ty->getPointeeType();
877
Anders Carlssonabd6b092010-06-02 15:44:35 +0000878 Qualifiers Quals;
879 QualType UnqualifiedPointeeTy =
880 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
881
Anders Carlsson8d145152009-12-20 22:30:54 +0000882 // Itanium C++ ABI 2.9.5p7:
883 // __flags is a flag word describing the cv-qualification and other
884 // attributes of the type pointed to.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000885 unsigned Flags = ComputeQualifierFlags(Quals);
Anders Carlsson8d145152009-12-20 22:30:54 +0000886
887 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000888
889 // Itanium C++ ABI 2.9.5p7:
890 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
891 // incomplete class type, the incomplete target type flag is set.
Anders Carlssonabd6b092010-06-02 15:44:35 +0000892 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000893 Flags |= PTI_Incomplete;
894
Anders Carlsson8d145152009-12-20 22:30:54 +0000895 if (IsIncompleteClassType(ClassType))
896 Flags |= PTI_ContainingClassIncomplete;
897
Anders Carlsson8d145152009-12-20 22:30:54 +0000898 const llvm::Type *UnsignedIntLTy =
899 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000900 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000901
902 // Itanium C++ ABI 2.9.5p7:
903 // __pointee is a pointer to the std::type_info derivation for the
904 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000905 llvm::Constant *PointeeTypeInfo =
Anders Carlssonabd6b092010-06-02 15:44:35 +0000906 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000907 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000908
909 // Itanium C++ ABI 2.9.5p9:
910 // __context is a pointer to an abi::__class_type_info corresponding to the
911 // class type containing the member pointed to
912 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000913 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000914}
915
John McCall9dffe6f2010-04-30 01:15:21 +0000916llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
917 bool ForEH) {
918 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
919 // FIXME: should we even be calling this method if RTTI is disabled
920 // and it's not for EH?
921 if (!ForEH && !getContext().getLangOptions().RTTI) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000922 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
923 return llvm::Constant::getNullValue(Int8PtrTy);
924 }
John McCall9dffe6f2010-04-30 01:15:21 +0000925
Anders Carlsson531d55f2009-12-31 17:43:53 +0000926 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000927}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000928
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000929void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
930 QualType PointerType = Context.getPointerType(Type);
931 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
932 RTTIBuilder(*this).BuildTypeInfo(Type, true);
933 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
934 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
935}
936
937void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Anders Carlsson2bd62502010-11-04 05:28:09 +0000938 QualType FundamentalTypes[] = { Context.VoidTy, Context.NullPtrTy,
939 Context.BoolTy, Context.WCharTy,
940 Context.CharTy, Context.UnsignedCharTy,
941 Context.SignedCharTy, Context.ShortTy,
942 Context.UnsignedShortTy, Context.IntTy,
943 Context.UnsignedIntTy, Context.LongTy,
944 Context.UnsignedLongTy, Context.LongLongTy,
945 Context.UnsignedLongLongTy, Context.FloatTy,
946 Context.DoubleTy, Context.LongDoubleTy,
947 Context.Char16Ty, Context.Char32Ty };
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000948 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
949 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
950}