blob: aec1c4554a35e8d746485457b4dbab9340dd576d [file] [log] [blame]
Mike Stumpde050572009-12-02 18:57:08 +00001//===--- CGCXXRTTI.cpp - Emit LLVM Code for C++ RTTI descriptors ----------===//
Anders Carlsson656e4c12009-10-10 20:49:04 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ code generation of RTTI descriptors.
11//
12//===----------------------------------------------------------------------===//
13
Mike Stumpae9b2be2009-11-17 23:45:57 +000014#include "clang/AST/Type.h"
Mike Stumpcbcd4e52009-11-14 23:32:21 +000015#include "clang/AST/RecordLayout.h"
Mike Stump61c38012009-11-17 21:44:24 +000016#include "CodeGenModule.h"
Anders Carlsson656e4c12009-10-10 20:49:04 +000017using namespace clang;
18using namespace CodeGen;
19
Mike Stump92f2fe22009-12-02 19:07:44 +000020namespace {
Mike Stumpde050572009-12-02 18:57:08 +000021class RTTIBuilder {
Mike Stump2b1bf312009-11-14 14:25:18 +000022 CodeGenModule &CGM; // Per-module state.
23 llvm::LLVMContext &VMContext;
Anders Carlsson8d145152009-12-20 22:30:54 +000024
Anders Carlsson08148092009-12-30 23:47:56 +000025 const llvm::Type *Int8PtrTy;
Anders Carlsson531d55f2009-12-31 17:43:53 +000026
27 /// Fields - The fields of the RTTI descriptor currently being built.
28 llvm::SmallVector<llvm::Constant *, 16> Fields;
Anders Carlssond6baec82009-12-11 01:27:37 +000029
Anders Carlsson1d7088d2009-12-17 07:09:17 +000030 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
31 /// descriptor of the given type.
32 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
33
Anders Carlsson046c2942010-04-17 20:15:18 +000034 /// BuildVTablePointer - Build the vtable pointer for the given type.
35 void BuildVTablePointer(const Type *Ty);
Anders Carlsson8d145152009-12-20 22:30:54 +000036
Anders Carlssonf64531a2009-12-30 01:00:12 +000037 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
Anders Carlsson08148092009-12-30 23:47:56 +000038 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
Anders Carlssonf64531a2009-12-30 01:00:12 +000039 void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
40
Anders Carlsson08148092009-12-30 23:47:56 +000041 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
42 /// classes with bases that do not satisfy the abi::__si_class_type_info
43 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
44 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
45
Anders Carlssonf64531a2009-12-30 01:00:12 +000046 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
47 /// for pointer types.
Anders Carlsson8d145152009-12-20 22:30:54 +000048 void BuildPointerTypeInfo(const PointerType *Ty);
49
50 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
51 /// struct, used for member pointer types.
52 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
53
Mike Stump2b1bf312009-11-14 14:25:18 +000054public:
Mike Stumpde050572009-12-02 18:57:08 +000055 RTTIBuilder(CodeGenModule &cgm)
Mike Stump2b1bf312009-11-14 14:25:18 +000056 : CGM(cgm), VMContext(cgm.getModule().getContext()),
57 Int8PtrTy(llvm::Type::getInt8PtrTy(VMContext)) { }
58
Anders Carlsson31b7f522009-12-11 02:46:30 +000059 llvm::Constant *BuildName(QualType Ty, bool Hidden,
60 llvm::GlobalVariable::LinkageTypes Linkage) {
Mike Stump2b1bf312009-11-14 14:25:18 +000061 llvm::SmallString<256> OutName;
Mike Stumpde050572009-12-02 18:57:08 +000062 CGM.getMangleContext().mangleCXXRTTIName(Ty, OutName);
Daniel Dunbar94fd26d2009-11-21 09:06:22 +000063 llvm::StringRef Name = OutName.str();
Mike Stumpcbcd4e52009-11-14 23:32:21 +000064
Anders Carlsson8d145152009-12-20 22:30:54 +000065 llvm::GlobalVariable *OGV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +000066 if (OGV && !OGV->isDeclaration())
67 return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy);
Mike Stump58588942009-11-19 01:08:19 +000068
Anders Carlsson31b7f522009-12-11 02:46:30 +000069 llvm::Constant *C = llvm::ConstantArray::get(VMContext, Name.substr(4));
Mike Stump2b1bf312009-11-14 14:25:18 +000070
Anders Carlsson31b7f522009-12-11 02:46:30 +000071 llvm::GlobalVariable *GV =
72 new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
73 C, Name);
Mike Stump58588942009-11-19 01:08:19 +000074 if (OGV) {
75 GV->takeName(OGV);
76 llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
77 OGV->getType());
78 OGV->replaceAllUsesWith(NewPtr);
79 OGV->eraseFromParent();
80 }
Mike Stump582b0372009-11-18 03:46:51 +000081 if (Hidden)
82 GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
83 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Daniel Dunbar7177dee2009-12-19 17:50:07 +000084 }
Mike Stumpc7a05bd2009-11-14 15:55:18 +000085
Mike Stump4e6f8ee2009-12-24 02:33:48 +000086 // FIXME: unify with DecideExtern
Mike Stump58588942009-11-19 01:08:19 +000087 bool DecideHidden(QualType Ty) {
88 // For this type, see if all components are never hidden.
89 if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>())
90 return (DecideHidden(MPT->getPointeeType())
91 && DecideHidden(QualType(MPT->getClass(), 0)));
92 if (const PointerType *PT = Ty->getAs<PointerType>())
93 return DecideHidden(PT->getPointeeType());
Mike Stump4e6f8ee2009-12-24 02:33:48 +000094 if (const FunctionType *FT = Ty->getAs<FunctionType>()) {
95 if (DecideHidden(FT->getResultType()) == false)
96 return false;
97 if (const FunctionProtoType *FPT = Ty->getAs<FunctionProtoType>()) {
98 for (unsigned i = 0; i <FPT->getNumArgs(); ++i)
99 if (DecideHidden(FPT->getArgType(i)) == false)
100 return false;
101 for (unsigned i = 0; i <FPT->getNumExceptions(); ++i)
102 if (DecideHidden(FPT->getExceptionType(i)) == false)
103 return false;
104 return true;
105 }
106 }
Mike Stump58588942009-11-19 01:08:19 +0000107 if (const RecordType *RT = Ty->getAs<RecordType>())
108 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
109 return CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden;
110 return false;
111 }
Anders Carlsson31b7f522009-12-11 02:46:30 +0000112
Anders Carlsson8d145152009-12-20 22:30:54 +0000113 // Pointer type info flags.
114 enum {
115 /// PTI_Const - Type has const qualifier.
116 PTI_Const = 0x1,
117
118 /// PTI_Volatile - Type has volatile qualifier.
119 PTI_Volatile = 0x2,
120
121 /// PTI_Restrict - Type has restrict qualifier.
122 PTI_Restrict = 0x4,
123
124 /// PTI_Incomplete - Type is incomplete.
125 PTI_Incomplete = 0x8,
126
127 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
128 /// (in pointer to member).
129 PTI_ContainingClassIncomplete = 0x10
130 };
Anders Carlsson08148092009-12-30 23:47:56 +0000131
132 // VMI type info flags.
133 enum {
134 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
135 VMI_NonDiamondRepeat = 0x1,
136
137 /// VMI_DiamondShaped - Class is diamond shaped.
138 VMI_DiamondShaped = 0x2
139 };
140
141 // Base class type info flags.
142 enum {
143 /// BCTI_Virtual - Base class is virtual.
144 BCTI_Virtual = 0x1,
145
146 /// BCTI_Public - Base class is public.
147 BCTI_Public = 0x2
148 };
Anders Carlsson531d55f2009-12-31 17:43:53 +0000149
150 /// BuildTypeInfo - Build the RTTI type info struct for the given type.
John McCall9dffe6f2010-04-30 01:15:21 +0000151 ///
152 /// \param Force - true to force the creation of this RTTI value
153 /// \param ForEH - true if this is for exception handling
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000154 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false);
Mike Stump2b1bf312009-11-14 14:25:18 +0000155};
Mike Stump92f2fe22009-12-02 19:07:44 +0000156}
Mike Stump2b1bf312009-11-14 14:25:18 +0000157
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000158llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
159 // Mangle the RTTI name.
160 llvm::SmallString<256> OutName;
161 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
162 llvm::StringRef Name = OutName.str();
163
Anders Carlsson8d145152009-12-20 22:30:54 +0000164 // Look for an existing global.
165 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000166
167 if (!GV) {
168 // Create a new global variable.
169 GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
170 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000171 }
172
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000173 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000174}
175
Anders Carlsson8d145152009-12-20 22:30:54 +0000176/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
177/// info for that type is defined in the standard library.
178static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
179 // Itanium C++ ABI 2.9.2:
180 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
181 // the run-time support library. Specifically, the run-time support
182 // library should contain type_info objects for the types X, X* and
183 // X const*, for every X in: void, bool, wchar_t, char, unsigned char,
184 // signed char, short, unsigned short, int, unsigned int, long,
185 // unsigned long, long long, unsigned long long, float, double, long double,
186 // char16_t, char32_t, and the IEEE 754r decimal and half-precision
187 // floating point types.
188 switch (Ty->getKind()) {
189 case BuiltinType::Void:
190 case BuiltinType::Bool:
191 case BuiltinType::WChar:
192 case BuiltinType::Char_U:
193 case BuiltinType::Char_S:
194 case BuiltinType::UChar:
195 case BuiltinType::SChar:
196 case BuiltinType::Short:
197 case BuiltinType::UShort:
198 case BuiltinType::Int:
199 case BuiltinType::UInt:
200 case BuiltinType::Long:
201 case BuiltinType::ULong:
202 case BuiltinType::LongLong:
203 case BuiltinType::ULongLong:
204 case BuiltinType::Float:
205 case BuiltinType::Double:
206 case BuiltinType::LongDouble:
207 case BuiltinType::Char16:
208 case BuiltinType::Char32:
209 case BuiltinType::Int128:
210 case BuiltinType::UInt128:
211 return true;
212
213 case BuiltinType::Overload:
214 case BuiltinType::Dependent:
215 case BuiltinType::UndeducedAuto:
216 assert(false && "Should not see this type here!");
217
218 case BuiltinType::NullPtr:
219 assert(false && "FIXME: nullptr_t is not handled!");
220
221 case BuiltinType::ObjCId:
222 case BuiltinType::ObjCClass:
223 case BuiltinType::ObjCSel:
224 assert(false && "FIXME: Objective-C types are unsupported!");
225 }
226
227 // Silent gcc.
228 return false;
229}
230
231static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
232 QualType PointeeTy = PointerTy->getPointeeType();
233 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
234 if (!BuiltinTy)
235 return false;
236
237 // Check the qualifiers.
238 Qualifiers Quals = PointeeTy.getQualifiers();
239 Quals.removeConst();
240
241 if (!Quals.empty())
242 return false;
243
244 return TypeInfoIsInStandardLibrary(BuiltinTy);
245}
246
247/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
John McCall9dffe6f2010-04-30 01:15:21 +0000248/// the given type exists somewhere else, and that we should not emit the type
Anders Carlsson8d145152009-12-20 22:30:54 +0000249/// information in this translation unit.
John McCall9dffe6f2010-04-30 01:15:21 +0000250static bool ShouldUseExternalRTTIDescriptor(ASTContext &Context,
251 QualType Ty) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000252 // Type info for builtin types is defined in the standard library.
253 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
254 return TypeInfoIsInStandardLibrary(BuiltinTy);
255
256 // Type info for some pointer types to builtin types is defined in the
257 // standard library.
258 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
259 return TypeInfoIsInStandardLibrary(PointerTy);
260
John McCall9dffe6f2010-04-30 01:15:21 +0000261 // If RTTI is disabled, don't consider key functions.
262 if (!Context.getLangOptions().RTTI) return false;
263
Anders Carlsson8d145152009-12-20 22:30:54 +0000264 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000265 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000266 if (!RD->hasDefinition())
267 return false;
268
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000269 if (!RD->isDynamicClass())
270 return false;
271
272 // Get the key function.
273 const CXXMethodDecl *KeyFunction = RD->getASTContext().getKeyFunction(RD);
274 if (KeyFunction && !KeyFunction->getBody()) {
275 // The class has a key function, but it is not defined in this translation
276 // unit, so we should use the external descriptor for it.
277 return true;
278 }
Anders Carlsson8d145152009-12-20 22:30:54 +0000279 }
280
281 return false;
282}
283
284/// IsIncompleteClassType - Returns whether the given record type is incomplete.
285static bool IsIncompleteClassType(const RecordType *RecordTy) {
286 return !RecordTy->getDecl()->isDefinition();
287}
288
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000289/// ContainsIncompleteClassType - Returns whether the given type contains an
290/// incomplete class type. This is true if
291///
292/// * The given type is an incomplete class type.
293/// * The given type is a pointer type whose pointee type contains an
294/// incomplete class type.
295/// * The given type is a member pointer type whose class is an incomplete
296/// class type.
297/// * The given type is a member pointer type whoise pointee type contains an
298/// incomplete class type.
Anders Carlsson8d145152009-12-20 22:30:54 +0000299/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000300static bool ContainsIncompleteClassType(QualType Ty) {
301 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
302 if (IsIncompleteClassType(RecordTy))
303 return true;
304 }
305
306 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
307 return ContainsIncompleteClassType(PointerTy->getPointeeType());
308
309 if (const MemberPointerType *MemberPointerTy =
310 dyn_cast<MemberPointerType>(Ty)) {
311 // Check if the class type is incomplete.
312 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
313 if (IsIncompleteClassType(ClassType))
314 return true;
315
316 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000317 }
318
319 return false;
320}
321
322/// getTypeInfoLinkage - Return the linkage that the type info and type info
323/// name constants should have for the given type.
324static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(QualType Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000325 // Itanium C++ ABI 2.9.5p7:
326 // In addition, it and all of the intermediate abi::__pointer_type_info
327 // structs in the chain down to the abi::__class_type_info for the
328 // incomplete class type must be prevented from resolving to the
329 // corresponding type_info structs for the complete class type, possibly
330 // by making them local static objects. Finally, a dummy class RTTI is
331 // generated for the incomplete type that will not resolve to the final
332 // complete class RTTI (because the latter need not exist), possibly by
333 // making it a local static object.
334 if (ContainsIncompleteClassType(Ty))
335 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000336
Douglas Gregor031b3712010-03-31 00:15:35 +0000337 switch (Ty->getLinkage()) {
338 case NoLinkage:
339 case InternalLinkage:
340 case UniqueExternalLinkage:
341 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson978ef682009-12-29 21:58:32 +0000342
Douglas Gregor031b3712010-03-31 00:15:35 +0000343 case ExternalLinkage:
344 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
345 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
346 if (RD->isDynamicClass())
Anders Carlsson046c2942010-04-17 20:15:18 +0000347 return CodeGenModule::getVTableLinkage(RD);
Mike Stumpc8f76f52009-12-24 01:10:27 +0000348 }
Douglas Gregor031b3712010-03-31 00:15:35 +0000349
Mike Stumpc8f76f52009-12-24 01:10:27 +0000350 return llvm::GlobalValue::WeakODRLinkage;
351 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000352
Anders Carlsson8d145152009-12-20 22:30:54 +0000353 return llvm::GlobalValue::WeakODRLinkage;
354}
355
Anders Carlssonf64531a2009-12-30 01:00:12 +0000356// CanUseSingleInheritance - Return whether the given record decl has a "single,
357// public, non-virtual base at offset zero (i.e. the derived class is dynamic
358// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
359static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
360 // Check the number of bases.
361 if (RD->getNumBases() != 1)
362 return false;
363
364 // Get the base.
365 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
366
367 // Check that the base is not virtual.
368 if (Base->isVirtual())
369 return false;
370
371 // Check that the base is public.
372 if (Base->getAccessSpecifier() != AS_public)
373 return false;
374
375 // Check that the class is dynamic iff the base is.
376 const CXXRecordDecl *BaseDecl =
377 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
378 if (!BaseDecl->isEmpty() &&
379 BaseDecl->isDynamicClass() != RD->isDynamicClass())
380 return false;
381
382 return true;
383}
384
Anders Carlsson046c2942010-04-17 20:15:18 +0000385void RTTIBuilder::BuildVTablePointer(const Type *Ty) {
386 const char *VTableName;
Anders Carlsson8d145152009-12-20 22:30:54 +0000387
388 switch (Ty->getTypeClass()) {
389 default: assert(0 && "Unhandled type!");
Anders Carlsson978ef682009-12-29 21:58:32 +0000390
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000391 case Type::Builtin:
Douglas Gregor031b3712010-03-31 00:15:35 +0000392 // GCC treats vector types as fundamental types.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000393 case Type::Vector:
394 case Type::ExtVector:
Anders Carlsson08148092009-12-30 23:47:56 +0000395 // abi::__fundamental_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000396 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000397 break;
398
Anders Carlsson978ef682009-12-29 21:58:32 +0000399 case Type::ConstantArray:
400 case Type::IncompleteArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000401 // abi::__array_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000402 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000403 break;
404
405 case Type::FunctionNoProto:
406 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000407 // abi::__function_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000408 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
Anders Carlsson978ef682009-12-29 21:58:32 +0000409 break;
410
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000411 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000412 // abi::__enum_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000413 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000414 break;
415
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000416 case Type::Record: {
417 const CXXRecordDecl *RD =
418 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000419
John McCall86ff3082010-02-04 22:26:26 +0000420 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson08148092009-12-30 23:47:56 +0000421 // abi::__class_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000422 VTableName = "_ZTVN10__cxxabiv117__class_type_infoE";
Anders Carlssonf64531a2009-12-30 01:00:12 +0000423 } else if (CanUseSingleInheritance(RD)) {
Anders Carlsson08148092009-12-30 23:47:56 +0000424 // abi::__si_class_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000425 VTableName = "_ZTVN10__cxxabiv120__si_class_type_infoE";
Anders Carlssonf64531a2009-12-30 01:00:12 +0000426 } else {
Anders Carlsson08148092009-12-30 23:47:56 +0000427 // abi::__vmi_class_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000428 VTableName = "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000429 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000430
431 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000432 }
433
Anders Carlsson8d145152009-12-20 22:30:54 +0000434 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000435 // abi::__pointer_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000436 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000437 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000438
Anders Carlsson8d145152009-12-20 22:30:54 +0000439 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000440 // abi::__pointer_to_member_type_info.
Anders Carlsson046c2942010-04-17 20:15:18 +0000441 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000442 break;
443 }
444
Anders Carlsson046c2942010-04-17 20:15:18 +0000445 llvm::Constant *VTable =
446 CGM.getModule().getOrInsertGlobal(VTableName, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000447
448 const llvm::Type *PtrDiffTy =
449 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
450
451 // The vtable address point is 2.
452 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
Anders Carlsson046c2942010-04-17 20:15:18 +0000453 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, &Two, 1);
454 VTable = llvm::ConstantExpr::getBitCast(VTable, Int8PtrTy);
Anders Carlsson8d145152009-12-20 22:30:54 +0000455
Anders Carlsson046c2942010-04-17 20:15:18 +0000456 Fields.push_back(VTable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000457}
458
John McCall9dffe6f2010-04-30 01:15:21 +0000459llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty,
460 bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000461 // We want to operate on the canonical type.
462 Ty = CGM.getContext().getCanonicalType(Ty);
463
464 // Check if we've already emitted an RTTI descriptor for this type.
465 llvm::SmallString<256> OutName;
466 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
467 llvm::StringRef Name = OutName.str();
468
469 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
470 if (OldGV && !OldGV->isDeclaration())
471 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
472
473 // Check if there is already an external RTTI descriptor for this type.
John McCall9dffe6f2010-04-30 01:15:21 +0000474 if (!Force && ShouldUseExternalRTTIDescriptor(CGM.getContext(), Ty))
Anders Carlsson8d145152009-12-20 22:30:54 +0000475 return GetAddrOfExternalRTTIDescriptor(Ty);
476
477 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(Ty);
478
479 // Add the vtable pointer.
Anders Carlsson046c2942010-04-17 20:15:18 +0000480 BuildVTablePointer(cast<Type>(Ty));
Anders Carlsson8d145152009-12-20 22:30:54 +0000481
482 // And the name.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000483 Fields.push_back(BuildName(Ty, DecideHidden(Ty), Linkage));
Anders Carlsson8d145152009-12-20 22:30:54 +0000484
485 switch (Ty->getTypeClass()) {
486 default: assert(false && "Unhandled type class!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000487
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000488 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000489 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000490 case Type::Vector:
491 case Type::ExtVector:
492 // Itanium C++ ABI 2.9.5p4:
493 // abi::__fundamental_type_info adds no data members to std::type_info.
494 break;
495
Anders Carlsson978ef682009-12-29 21:58:32 +0000496 case Type::ConstantArray:
497 case Type::IncompleteArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000498 // Itanium C++ ABI 2.9.5p5:
499 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000500 break;
501
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000502 case Type::FunctionNoProto:
503 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000504 // Itanium C++ ABI 2.9.5p5:
505 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000506 break;
507
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000508 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000509 // Itanium C++ ABI 2.9.5p5:
510 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000511 break;
512
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000513 case Type::Record: {
514 const CXXRecordDecl *RD =
515 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000516 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000517 // We don't need to emit any fields.
518 break;
519 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000520
Anders Carlsson08148092009-12-30 23:47:56 +0000521 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000522 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000523 else
524 BuildVMIClassTypeInfo(RD);
525
526 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000527 }
528
Anders Carlsson8d145152009-12-20 22:30:54 +0000529 case Type::Pointer:
530 BuildPointerTypeInfo(cast<PointerType>(Ty));
531 break;
532
533 case Type::MemberPointer:
534 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
535 break;
536 }
537
538 llvm::Constant *Init =
Anders Carlsson531d55f2009-12-31 17:43:53 +0000539 llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
Anders Carlsson8d145152009-12-20 22:30:54 +0000540 /*Packed=*/false);
541
542 llvm::GlobalVariable *GV =
543 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
544 /*Constant=*/true, Linkage, Init, Name);
545
546 // If there's already an old global variable, replace it with the new one.
547 if (OldGV) {
548 GV->takeName(OldGV);
549 llvm::Constant *NewPtr =
550 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
551 OldGV->replaceAllUsesWith(NewPtr);
552 OldGV->eraseFromParent();
553 }
554
555 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
556}
557
Anders Carlsson08148092009-12-30 23:47:56 +0000558/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000559/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000560static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000561 unsigned Flags = 0;
562
563 if (Quals.hasConst())
564 Flags |= RTTIBuilder::PTI_Const;
565 if (Quals.hasVolatile())
566 Flags |= RTTIBuilder::PTI_Volatile;
567 if (Quals.hasRestrict())
568 Flags |= RTTIBuilder::PTI_Restrict;
569
570 return Flags;
571}
572
Anders Carlssonf64531a2009-12-30 01:00:12 +0000573/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
574/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
575void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
576 // Itanium C++ ABI 2.9.5p6b:
577 // It adds to abi::__class_type_info a single member pointing to the
578 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000579 llvm::Constant *BaseTypeInfo =
580 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
581 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000582}
583
Anders Carlsson08148092009-12-30 23:47:56 +0000584/// SeenBases - Contains virtual and non-virtual bases seen when traversing
585/// a class hierarchy.
586struct SeenBases {
587 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
588 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
589};
590
591/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
592/// abi::__vmi_class_type_info.
593///
594static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
595 SeenBases &Bases) {
596
597 unsigned Flags = 0;
598
599 const CXXRecordDecl *BaseDecl =
600 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
601
602 if (Base->isVirtual()) {
603 if (Bases.VirtualBases.count(BaseDecl)) {
604 // If this virtual base has been seen before, then the class is diamond
605 // shaped.
606 Flags |= RTTIBuilder::VMI_DiamondShaped;
607 } else {
608 if (Bases.NonVirtualBases.count(BaseDecl))
609 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
610
611 // Mark the virtual base as seen.
612 Bases.VirtualBases.insert(BaseDecl);
613 }
614 } else {
615 if (Bases.NonVirtualBases.count(BaseDecl)) {
616 // If this non-virtual base has been seen before, then the class has non-
617 // diamond shaped repeated inheritance.
618 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
619 } else {
620 if (Bases.VirtualBases.count(BaseDecl))
621 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
622
623 // Mark the non-virtual base as seen.
624 Bases.NonVirtualBases.insert(BaseDecl);
625 }
626 }
627
628 // Walk all bases.
629 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
630 E = BaseDecl->bases_end(); I != E; ++I)
631 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
632
633 return Flags;
634}
635
636static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
637 unsigned Flags = 0;
638 SeenBases Bases;
639
640 // Walk all bases.
641 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
642 E = RD->bases_end(); I != E; ++I)
643 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
644
645 return Flags;
646}
647
648/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
649/// classes with bases that do not satisfy the abi::__si_class_type_info
650/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
651void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
652 const llvm::Type *UnsignedIntLTy =
653 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
654
655 // Itanium C++ ABI 2.9.5p6c:
656 // __flags is a word with flags describing details about the class
657 // structure, which may be referenced by using the __flags_masks
658 // enumeration. These flags refer to both direct and indirect bases.
659 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000660 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000661
662 // Itanium C++ ABI 2.9.5p6c:
663 // __base_count is a word with the number of direct proper base class
664 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000665 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000666
667 if (!RD->getNumBases())
668 return;
669
670 const llvm::Type *LongLTy =
671 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
672
673 // Now add the base class descriptions.
674
675 // Itanium C++ ABI 2.9.5p6c:
676 // __base_info[] is an array of base class descriptions -- one for every
677 // direct proper base. Each description is of the type:
678 //
679 // struct abi::__base_class_type_info {
680 // public:
681 // const __class_type_info *__base_type;
682 // long __offset_flags;
683 //
684 // enum __offset_flags_masks {
685 // __virtual_mask = 0x1,
686 // __public_mask = 0x2,
687 // __offset_shift = 8
688 // };
689 // };
690 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
691 E = RD->bases_end(); I != E; ++I) {
692 const CXXBaseSpecifier *Base = I;
693
694 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000695 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000696
697 const CXXRecordDecl *BaseDecl =
698 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
699
700 int64_t OffsetFlags = 0;
701
702 // All but the lower 8 bits of __offset_flags are a signed offset.
703 // For a non-virtual base, this is the offset in the object of the base
704 // subobject. For a virtual base, this is the offset in the virtual table of
705 // the virtual base offset for the virtual base referenced (negative).
706 if (Base->isVirtual())
Anders Carlssonaf440352010-03-23 04:11:45 +0000707 OffsetFlags = CGM.getVTables().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000708 else {
709 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
710 OffsetFlags = Layout.getBaseClassOffset(BaseDecl) / 8;
711 };
712
713 OffsetFlags <<= 8;
714
715 // The low-order byte of __offset_flags contains flags, as given by the
716 // masks from the enumeration __offset_flags_masks.
717 if (Base->isVirtual())
718 OffsetFlags |= BCTI_Virtual;
719 if (Base->getAccessSpecifier() == AS_public)
720 OffsetFlags |= BCTI_Public;
721
Anders Carlsson531d55f2009-12-31 17:43:53 +0000722 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000723 }
724}
725
Anders Carlsson8d145152009-12-20 22:30:54 +0000726/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
727/// used for pointer types.
728void RTTIBuilder::BuildPointerTypeInfo(const PointerType *Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000729 QualType PointeeTy = Ty->getPointeeType();
Anders Carlsson8d145152009-12-20 22:30:54 +0000730
731 // Itanium C++ ABI 2.9.5p7:
732 // __flags is a flag word describing the cv-qualification and other
733 // attributes of the type pointed to
Anders Carlsson08148092009-12-30 23:47:56 +0000734 unsigned Flags = ComputeQualifierFlags(PointeeTy.getQualifiers());
Anders Carlsson8d145152009-12-20 22:30:54 +0000735
736 // Itanium C++ ABI 2.9.5p7:
737 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
738 // incomplete class type, the incomplete target type flag is set.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000739 if (ContainsIncompleteClassType(PointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000740 Flags |= PTI_Incomplete;
741
742 const llvm::Type *UnsignedIntLTy =
743 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000744 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000745
746 // Itanium C++ ABI 2.9.5p7:
747 // __pointee is a pointer to the std::type_info derivation for the
748 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000749 llvm::Constant *PointeeTypeInfo =
750 RTTIBuilder(CGM).BuildTypeInfo(PointeeTy.getUnqualifiedType());
751 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000752}
753
754/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
755/// struct, used for member pointer types.
756void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
757 QualType PointeeTy = Ty->getPointeeType();
758
759 // Itanium C++ ABI 2.9.5p7:
760 // __flags is a flag word describing the cv-qualification and other
761 // attributes of the type pointed to.
Anders Carlsson08148092009-12-30 23:47:56 +0000762 unsigned Flags = ComputeQualifierFlags(PointeeTy.getQualifiers());
Anders Carlsson8d145152009-12-20 22:30:54 +0000763
764 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000765
766 // Itanium C++ ABI 2.9.5p7:
767 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
768 // incomplete class type, the incomplete target type flag is set.
769 if (ContainsIncompleteClassType(PointeeTy))
770 Flags |= PTI_Incomplete;
771
Anders Carlsson8d145152009-12-20 22:30:54 +0000772 if (IsIncompleteClassType(ClassType))
773 Flags |= PTI_ContainingClassIncomplete;
774
Anders Carlsson8d145152009-12-20 22:30:54 +0000775 const llvm::Type *UnsignedIntLTy =
776 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000777 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000778
779 // Itanium C++ ABI 2.9.5p7:
780 // __pointee is a pointer to the std::type_info derivation for the
781 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000782 llvm::Constant *PointeeTypeInfo =
783 RTTIBuilder(CGM).BuildTypeInfo(PointeeTy.getUnqualifiedType());
784 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000785
786 // Itanium C++ ABI 2.9.5p9:
787 // __context is a pointer to an abi::__class_type_info corresponding to the
788 // class type containing the member pointed to
789 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000790 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000791}
792
John McCall9dffe6f2010-04-30 01:15:21 +0000793llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
794 bool ForEH) {
795 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
796 // FIXME: should we even be calling this method if RTTI is disabled
797 // and it's not for EH?
798 if (!ForEH && !getContext().getLangOptions().RTTI) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000799 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
800 return llvm::Constant::getNullValue(Int8PtrTy);
801 }
John McCall9dffe6f2010-04-30 01:15:21 +0000802
Anders Carlsson531d55f2009-12-31 17:43:53 +0000803 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000804}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000805
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000806void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
807 QualType PointerType = Context.getPointerType(Type);
808 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
809 RTTIBuilder(*this).BuildTypeInfo(Type, true);
810 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
811 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
812}
813
814void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000815 QualType FundamentalTypes[] = { Context.VoidTy, Context.Char32Ty,
816 Context.Char16Ty, Context.UnsignedLongLongTy,
817 Context.LongLongTy, Context.WCharTy,
818 Context.UnsignedShortTy, Context.ShortTy,
819 Context.UnsignedLongTy, Context.LongTy,
820 Context.UnsignedIntTy, Context.IntTy,
821 Context.UnsignedCharTy, Context.FloatTy,
822 Context.LongDoubleTy, Context.DoubleTy,
823 Context.CharTy, Context.BoolTy,
824 Context.SignedCharTy };
825 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
826 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
827}