blob: 5236d206348950622ce58cde9e27e4f2336f335b [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 Carlsson8d145152009-12-20 22:30:54 +000034 /// BuildVtablePointer - Build the vtable pointer for the given type.
35 void BuildVtablePointer(const Type *Ty);
36
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.
151 llvm::Constant *BuildTypeInfo(QualType Ty);
Mike Stump2b1bf312009-11-14 14:25:18 +0000152};
Mike Stump92f2fe22009-12-02 19:07:44 +0000153}
Mike Stump2b1bf312009-11-14 14:25:18 +0000154
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000155llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
156 // Mangle the RTTI name.
157 llvm::SmallString<256> OutName;
158 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
159 llvm::StringRef Name = OutName.str();
160
Anders Carlsson8d145152009-12-20 22:30:54 +0000161 // Look for an existing global.
162 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000163
164 if (!GV) {
165 // Create a new global variable.
166 GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
167 llvm::GlobalValue::ExternalLinkage, 0, Name);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000168 }
169
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000170 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000171}
172
Anders Carlsson8d145152009-12-20 22:30:54 +0000173/// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
174/// info for that type is defined in the standard library.
175static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
176 // Itanium C++ ABI 2.9.2:
177 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
178 // the run-time support library. Specifically, the run-time support
179 // library should contain type_info objects for the types X, X* and
180 // X const*, for every X in: void, bool, wchar_t, char, unsigned char,
181 // signed char, short, unsigned short, int, unsigned int, long,
182 // unsigned long, long long, unsigned long long, float, double, long double,
183 // char16_t, char32_t, and the IEEE 754r decimal and half-precision
184 // floating point types.
185 switch (Ty->getKind()) {
186 case BuiltinType::Void:
187 case BuiltinType::Bool:
188 case BuiltinType::WChar:
189 case BuiltinType::Char_U:
190 case BuiltinType::Char_S:
191 case BuiltinType::UChar:
192 case BuiltinType::SChar:
193 case BuiltinType::Short:
194 case BuiltinType::UShort:
195 case BuiltinType::Int:
196 case BuiltinType::UInt:
197 case BuiltinType::Long:
198 case BuiltinType::ULong:
199 case BuiltinType::LongLong:
200 case BuiltinType::ULongLong:
201 case BuiltinType::Float:
202 case BuiltinType::Double:
203 case BuiltinType::LongDouble:
204 case BuiltinType::Char16:
205 case BuiltinType::Char32:
206 case BuiltinType::Int128:
207 case BuiltinType::UInt128:
208 return true;
209
210 case BuiltinType::Overload:
211 case BuiltinType::Dependent:
212 case BuiltinType::UndeducedAuto:
213 assert(false && "Should not see this type here!");
214
215 case BuiltinType::NullPtr:
216 assert(false && "FIXME: nullptr_t is not handled!");
217
218 case BuiltinType::ObjCId:
219 case BuiltinType::ObjCClass:
220 case BuiltinType::ObjCSel:
221 assert(false && "FIXME: Objective-C types are unsupported!");
222 }
223
224 // Silent gcc.
225 return false;
226}
227
228static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
229 QualType PointeeTy = PointerTy->getPointeeType();
230 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
231 if (!BuiltinTy)
232 return false;
233
234 // Check the qualifiers.
235 Qualifiers Quals = PointeeTy.getQualifiers();
236 Quals.removeConst();
237
238 if (!Quals.empty())
239 return false;
240
241 return TypeInfoIsInStandardLibrary(BuiltinTy);
242}
243
244/// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
245/// the given type exists somewhere else, and that we should not emit the typ
246/// information in this translation unit.
247bool ShouldUseExternalRTTIDescriptor(QualType Ty) {
248 // Type info for builtin types is defined in the standard library.
249 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
250 return TypeInfoIsInStandardLibrary(BuiltinTy);
251
252 // Type info for some pointer types to builtin types is defined in the
253 // standard library.
254 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
255 return TypeInfoIsInStandardLibrary(PointerTy);
256
257 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000258 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000259 if (!RD->hasDefinition())
260 return false;
261
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000262 if (!RD->isDynamicClass())
263 return false;
264
265 // Get the key function.
266 const CXXMethodDecl *KeyFunction = RD->getASTContext().getKeyFunction(RD);
267 if (KeyFunction && !KeyFunction->getBody()) {
268 // The class has a key function, but it is not defined in this translation
269 // unit, so we should use the external descriptor for it.
270 return true;
271 }
Anders Carlsson8d145152009-12-20 22:30:54 +0000272 }
273
274 return false;
275}
276
277/// IsIncompleteClassType - Returns whether the given record type is incomplete.
278static bool IsIncompleteClassType(const RecordType *RecordTy) {
279 return !RecordTy->getDecl()->isDefinition();
280}
281
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000282/// ContainsIncompleteClassType - Returns whether the given type contains an
283/// incomplete class type. This is true if
284///
285/// * The given type is an incomplete class type.
286/// * The given type is a pointer type whose pointee type contains an
287/// incomplete class type.
288/// * The given type is a member pointer type whose class is an incomplete
289/// class type.
290/// * The given type is a member pointer type whoise pointee type contains an
291/// incomplete class type.
Anders Carlsson8d145152009-12-20 22:30:54 +0000292/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000293static bool ContainsIncompleteClassType(QualType Ty) {
294 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
295 if (IsIncompleteClassType(RecordTy))
296 return true;
297 }
298
299 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
300 return ContainsIncompleteClassType(PointerTy->getPointeeType());
301
302 if (const MemberPointerType *MemberPointerTy =
303 dyn_cast<MemberPointerType>(Ty)) {
304 // Check if the class type is incomplete.
305 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
306 if (IsIncompleteClassType(ClassType))
307 return true;
308
309 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000310 }
311
312 return false;
313}
314
315/// getTypeInfoLinkage - Return the linkage that the type info and type info
316/// name constants should have for the given type.
317static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(QualType Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000318 // Itanium C++ ABI 2.9.5p7:
319 // In addition, it and all of the intermediate abi::__pointer_type_info
320 // structs in the chain down to the abi::__class_type_info for the
321 // incomplete class type must be prevented from resolving to the
322 // corresponding type_info structs for the complete class type, possibly
323 // by making them local static objects. Finally, a dummy class RTTI is
324 // generated for the incomplete type that will not resolve to the final
325 // complete class RTTI (because the latter need not exist), possibly by
326 // making it a local static object.
327 if (ContainsIncompleteClassType(Ty))
328 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000329
Anders Carlsson978ef682009-12-29 21:58:32 +0000330 switch (Ty->getTypeClass()) {
331 default:
332 // FIXME: We need to add code to handle all types.
333 assert(false && "Unhandled type!");
334 break;
335
336 case Type::Pointer: {
337 const PointerType *PointerTy = cast<PointerType>(Ty);
338
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000339 // If the pointee type has internal linkage, then the pointer type needs to
340 // have it as well.
341 if (getTypeInfoLinkage(PointerTy->getPointeeType()) ==
342 llvm::GlobalVariable::InternalLinkage)
343 return llvm::GlobalVariable::InternalLinkage;
344
345 return llvm::GlobalVariable::WeakODRLinkage;
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000346 }
347
348 case Type::Enum: {
349 const EnumType *EnumTy = cast<EnumType>(Ty);
350 const EnumDecl *ED = EnumTy->getDecl();
351
352 // If we're in an anonymous namespace, then we always want internal linkage.
353 if (ED->isInAnonymousNamespace() || !ED->hasLinkage())
354 return llvm::GlobalVariable::InternalLinkage;
355
356 return llvm::GlobalValue::WeakODRLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000357 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000358
359 case Type::Record: {
360 const RecordType *RecordTy = cast<RecordType>(Ty);
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000361 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
362
363 // If we're in an anonymous namespace, then we always want internal linkage.
364 if (RD->isInAnonymousNamespace() || !RD->hasLinkage())
365 return llvm::GlobalVariable::InternalLinkage;
Douglas Gregordffb8012010-01-06 22:00:56 +0000366
367 // If this class does not have a vtable, we want weak linkage.
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000368 if (!RD->isDynamicClass())
369 return llvm::GlobalValue::WeakODRLinkage;
370
Douglas Gregordffb8012010-01-06 22:00:56 +0000371 return CodeGenModule::getVtableLinkage(RD);
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000372 }
373
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000374 case Type::Vector:
375 case Type::ExtVector:
Anders Carlsson978ef682009-12-29 21:58:32 +0000376 case Type::Builtin:
Mike Stump8d9fb9b2009-12-23 22:48:20 +0000377 return llvm::GlobalValue::WeakODRLinkage;
Mike Stump8d9fb9b2009-12-23 22:48:20 +0000378
Anders Carlsson978ef682009-12-29 21:58:32 +0000379 case Type::FunctionProto: {
380 const FunctionProtoType *FPT = cast<FunctionProtoType>(Ty);
381
382 // Check the return type.
383 if (getTypeInfoLinkage(FPT->getResultType()) ==
384 llvm::GlobalValue::InternalLinkage)
Mike Stumpc8f76f52009-12-24 01:10:27 +0000385 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson978ef682009-12-29 21:58:32 +0000386
387 // Check the parameter types.
388 for (unsigned i = 0; i != FPT->getNumArgs(); ++i) {
389 if (getTypeInfoLinkage(FPT->getArgType(i)) ==
390 llvm::GlobalValue::InternalLinkage)
391 return llvm::GlobalValue::InternalLinkage;
Mike Stumpc8f76f52009-12-24 01:10:27 +0000392 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000393
Mike Stumpc8f76f52009-12-24 01:10:27 +0000394 return llvm::GlobalValue::WeakODRLinkage;
395 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000396
397 case Type::ConstantArray:
398 case Type::IncompleteArray: {
399 const ArrayType *AT = cast<ArrayType>(Ty);
Mike Stumpc8f76f52009-12-24 01:10:27 +0000400
Anders Carlsson978ef682009-12-29 21:58:32 +0000401 // Check the element type.
402 if (getTypeInfoLinkage(AT->getElementType()) ==
403 llvm::GlobalValue::InternalLinkage)
404 return llvm::GlobalValue::InternalLinkage;
405 }
406
407 }
408
Anders Carlsson8d145152009-12-20 22:30:54 +0000409 return llvm::GlobalValue::WeakODRLinkage;
410}
411
Anders Carlssonf64531a2009-12-30 01:00:12 +0000412// CanUseSingleInheritance - Return whether the given record decl has a "single,
413// public, non-virtual base at offset zero (i.e. the derived class is dynamic
414// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
415static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
416 // Check the number of bases.
417 if (RD->getNumBases() != 1)
418 return false;
419
420 // Get the base.
421 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
422
423 // Check that the base is not virtual.
424 if (Base->isVirtual())
425 return false;
426
427 // Check that the base is public.
428 if (Base->getAccessSpecifier() != AS_public)
429 return false;
430
431 // Check that the class is dynamic iff the base is.
432 const CXXRecordDecl *BaseDecl =
433 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
434 if (!BaseDecl->isEmpty() &&
435 BaseDecl->isDynamicClass() != RD->isDynamicClass())
436 return false;
437
438 return true;
439}
440
Anders Carlsson8d145152009-12-20 22:30:54 +0000441void RTTIBuilder::BuildVtablePointer(const Type *Ty) {
442 const char *VtableName;
443
444 switch (Ty->getTypeClass()) {
445 default: assert(0 && "Unhandled type!");
Anders Carlsson978ef682009-12-29 21:58:32 +0000446
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000447 // GCC treats vector types as fundamental types.
448 case Type::Vector:
449 case Type::ExtVector:
Anders Carlsson08148092009-12-30 23:47:56 +0000450 // abi::__fundamental_type_info.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000451 VtableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
452 break;
453
Anders Carlsson978ef682009-12-29 21:58:32 +0000454 case Type::ConstantArray:
455 case Type::IncompleteArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000456 // abi::__array_type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000457 VtableName = "_ZTVN10__cxxabiv117__array_type_infoE";
458 break;
459
460 case Type::FunctionNoProto:
461 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000462 // abi::__function_type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000463 VtableName = "_ZTVN10__cxxabiv120__function_type_infoE";
464 break;
465
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000466 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000467 // abi::__enum_type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000468 VtableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
469 break;
470
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000471 case Type::Record: {
472 const CXXRecordDecl *RD =
473 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000474
John McCall86ff3082010-02-04 22:26:26 +0000475 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson08148092009-12-30 23:47:56 +0000476 // abi::__class_type_info.
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000477 VtableName = "_ZTVN10__cxxabiv117__class_type_infoE";
Anders Carlssonf64531a2009-12-30 01:00:12 +0000478 } else if (CanUseSingleInheritance(RD)) {
Anders Carlsson08148092009-12-30 23:47:56 +0000479 // abi::__si_class_type_info.
Anders Carlssonf64531a2009-12-30 01:00:12 +0000480 VtableName = "_ZTVN10__cxxabiv120__si_class_type_infoE";
481 } else {
Anders Carlsson08148092009-12-30 23:47:56 +0000482 // abi::__vmi_class_type_info.
483 VtableName = "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000484 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000485
486 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000487 }
488
Anders Carlsson8d145152009-12-20 22:30:54 +0000489 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000490 // abi::__pointer_type_info.
Anders Carlsson8d145152009-12-20 22:30:54 +0000491 VtableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
492 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000493
Anders Carlsson8d145152009-12-20 22:30:54 +0000494 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000495 // abi::__pointer_to_member_type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000496 VtableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000497 break;
498 }
499
500 llvm::Constant *Vtable =
501 CGM.getModule().getOrInsertGlobal(VtableName, Int8PtrTy);
502
503 const llvm::Type *PtrDiffTy =
504 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
505
506 // The vtable address point is 2.
507 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
508 Vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(Vtable, &Two, 1);
509 Vtable = llvm::ConstantExpr::getBitCast(Vtable, Int8PtrTy);
510
Anders Carlsson531d55f2009-12-31 17:43:53 +0000511 Fields.push_back(Vtable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000512}
513
514llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty) {
515 // We want to operate on the canonical type.
516 Ty = CGM.getContext().getCanonicalType(Ty);
517
518 // Check if we've already emitted an RTTI descriptor for this type.
519 llvm::SmallString<256> OutName;
520 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
521 llvm::StringRef Name = OutName.str();
522
523 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
524 if (OldGV && !OldGV->isDeclaration())
525 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
526
527 // Check if there is already an external RTTI descriptor for this type.
528 if (ShouldUseExternalRTTIDescriptor(Ty))
529 return GetAddrOfExternalRTTIDescriptor(Ty);
530
531 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(Ty);
532
533 // Add the vtable pointer.
534 BuildVtablePointer(cast<Type>(Ty));
535
536 // And the name.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000537 Fields.push_back(BuildName(Ty, DecideHidden(Ty), Linkage));
Anders Carlsson8d145152009-12-20 22:30:54 +0000538
539 switch (Ty->getTypeClass()) {
540 default: assert(false && "Unhandled type class!");
541 case Type::Builtin:
542 assert(false && "Builtin type info must be in the standard library!");
543 break;
544
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000545 // GCC treats vector types as fundamental types.
546 case Type::Vector:
547 case Type::ExtVector:
548 // Itanium C++ ABI 2.9.5p4:
549 // abi::__fundamental_type_info adds no data members to std::type_info.
550 break;
551
Anders Carlsson978ef682009-12-29 21:58:32 +0000552 case Type::ConstantArray:
553 case Type::IncompleteArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000554 // Itanium C++ ABI 2.9.5p5:
555 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000556 break;
557
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000558 case Type::FunctionNoProto:
559 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000560 // Itanium C++ ABI 2.9.5p5:
561 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000562 break;
563
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000564 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000565 // Itanium C++ ABI 2.9.5p5:
566 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000567 break;
568
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000569 case Type::Record: {
570 const CXXRecordDecl *RD =
571 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000572 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000573 // We don't need to emit any fields.
574 break;
575 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000576
Anders Carlsson08148092009-12-30 23:47:56 +0000577 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000578 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000579 else
580 BuildVMIClassTypeInfo(RD);
581
582 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000583 }
584
Anders Carlsson8d145152009-12-20 22:30:54 +0000585 case Type::Pointer:
586 BuildPointerTypeInfo(cast<PointerType>(Ty));
587 break;
588
589 case Type::MemberPointer:
590 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
591 break;
592 }
593
594 llvm::Constant *Init =
Anders Carlsson531d55f2009-12-31 17:43:53 +0000595 llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
Anders Carlsson8d145152009-12-20 22:30:54 +0000596 /*Packed=*/false);
597
598 llvm::GlobalVariable *GV =
599 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
600 /*Constant=*/true, Linkage, Init, Name);
601
602 // If there's already an old global variable, replace it with the new one.
603 if (OldGV) {
604 GV->takeName(OldGV);
605 llvm::Constant *NewPtr =
606 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
607 OldGV->replaceAllUsesWith(NewPtr);
608 OldGV->eraseFromParent();
609 }
610
611 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
612}
613
Anders Carlsson08148092009-12-30 23:47:56 +0000614/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000615/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000616static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000617 unsigned Flags = 0;
618
619 if (Quals.hasConst())
620 Flags |= RTTIBuilder::PTI_Const;
621 if (Quals.hasVolatile())
622 Flags |= RTTIBuilder::PTI_Volatile;
623 if (Quals.hasRestrict())
624 Flags |= RTTIBuilder::PTI_Restrict;
625
626 return Flags;
627}
628
Anders Carlssonf64531a2009-12-30 01:00:12 +0000629/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
630/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
631void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
632 // Itanium C++ ABI 2.9.5p6b:
633 // It adds to abi::__class_type_info a single member pointing to the
634 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000635 llvm::Constant *BaseTypeInfo =
636 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
637 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000638}
639
Anders Carlsson08148092009-12-30 23:47:56 +0000640/// SeenBases - Contains virtual and non-virtual bases seen when traversing
641/// a class hierarchy.
642struct SeenBases {
643 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
644 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
645};
646
647/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
648/// abi::__vmi_class_type_info.
649///
650static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
651 SeenBases &Bases) {
652
653 unsigned Flags = 0;
654
655 const CXXRecordDecl *BaseDecl =
656 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
657
658 if (Base->isVirtual()) {
659 if (Bases.VirtualBases.count(BaseDecl)) {
660 // If this virtual base has been seen before, then the class is diamond
661 // shaped.
662 Flags |= RTTIBuilder::VMI_DiamondShaped;
663 } else {
664 if (Bases.NonVirtualBases.count(BaseDecl))
665 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
666
667 // Mark the virtual base as seen.
668 Bases.VirtualBases.insert(BaseDecl);
669 }
670 } else {
671 if (Bases.NonVirtualBases.count(BaseDecl)) {
672 // If this non-virtual base has been seen before, then the class has non-
673 // diamond shaped repeated inheritance.
674 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
675 } else {
676 if (Bases.VirtualBases.count(BaseDecl))
677 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
678
679 // Mark the non-virtual base as seen.
680 Bases.NonVirtualBases.insert(BaseDecl);
681 }
682 }
683
684 // Walk all bases.
685 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
686 E = BaseDecl->bases_end(); I != E; ++I)
687 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
688
689 return Flags;
690}
691
692static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
693 unsigned Flags = 0;
694 SeenBases Bases;
695
696 // Walk all bases.
697 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
698 E = RD->bases_end(); I != E; ++I)
699 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
700
701 return Flags;
702}
703
704/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
705/// classes with bases that do not satisfy the abi::__si_class_type_info
706/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
707void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
708 const llvm::Type *UnsignedIntLTy =
709 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
710
711 // Itanium C++ ABI 2.9.5p6c:
712 // __flags is a word with flags describing details about the class
713 // structure, which may be referenced by using the __flags_masks
714 // enumeration. These flags refer to both direct and indirect bases.
715 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000716 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000717
718 // Itanium C++ ABI 2.9.5p6c:
719 // __base_count is a word with the number of direct proper base class
720 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000721 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000722
723 if (!RD->getNumBases())
724 return;
725
726 const llvm::Type *LongLTy =
727 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
728
729 // Now add the base class descriptions.
730
731 // Itanium C++ ABI 2.9.5p6c:
732 // __base_info[] is an array of base class descriptions -- one for every
733 // direct proper base. Each description is of the type:
734 //
735 // struct abi::__base_class_type_info {
736 // public:
737 // const __class_type_info *__base_type;
738 // long __offset_flags;
739 //
740 // enum __offset_flags_masks {
741 // __virtual_mask = 0x1,
742 // __public_mask = 0x2,
743 // __offset_shift = 8
744 // };
745 // };
746 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
747 E = RD->bases_end(); I != E; ++I) {
748 const CXXBaseSpecifier *Base = I;
749
750 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000751 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000752
753 const CXXRecordDecl *BaseDecl =
754 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
755
756 int64_t OffsetFlags = 0;
757
758 // All but the lower 8 bits of __offset_flags are a signed offset.
759 // For a non-virtual base, this is the offset in the object of the base
760 // subobject. For a virtual base, this is the offset in the virtual table of
761 // the virtual base offset for the virtual base referenced (negative).
762 if (Base->isVirtual())
763 OffsetFlags = CGM.getVtableInfo().getVirtualBaseOffsetIndex(RD, BaseDecl);
764 else {
765 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
766 OffsetFlags = Layout.getBaseClassOffset(BaseDecl) / 8;
767 };
768
769 OffsetFlags <<= 8;
770
771 // The low-order byte of __offset_flags contains flags, as given by the
772 // masks from the enumeration __offset_flags_masks.
773 if (Base->isVirtual())
774 OffsetFlags |= BCTI_Virtual;
775 if (Base->getAccessSpecifier() == AS_public)
776 OffsetFlags |= BCTI_Public;
777
Anders Carlsson531d55f2009-12-31 17:43:53 +0000778 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000779 }
780}
781
Anders Carlsson8d145152009-12-20 22:30:54 +0000782/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
783/// used for pointer types.
784void RTTIBuilder::BuildPointerTypeInfo(const PointerType *Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000785 QualType PointeeTy = Ty->getPointeeType();
Anders Carlsson8d145152009-12-20 22:30:54 +0000786
787 // Itanium C++ ABI 2.9.5p7:
788 // __flags is a flag word describing the cv-qualification and other
789 // attributes of the type pointed to
Anders Carlsson08148092009-12-30 23:47:56 +0000790 unsigned Flags = ComputeQualifierFlags(PointeeTy.getQualifiers());
Anders Carlsson8d145152009-12-20 22:30:54 +0000791
792 // Itanium C++ ABI 2.9.5p7:
793 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
794 // incomplete class type, the incomplete target type flag is set.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000795 if (ContainsIncompleteClassType(PointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000796 Flags |= PTI_Incomplete;
797
798 const llvm::Type *UnsignedIntLTy =
799 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000800 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000801
802 // Itanium C++ ABI 2.9.5p7:
803 // __pointee is a pointer to the std::type_info derivation for the
804 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000805 llvm::Constant *PointeeTypeInfo =
806 RTTIBuilder(CGM).BuildTypeInfo(PointeeTy.getUnqualifiedType());
807 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000808}
809
810/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
811/// struct, used for member pointer types.
812void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
813 QualType PointeeTy = Ty->getPointeeType();
814
815 // Itanium C++ ABI 2.9.5p7:
816 // __flags is a flag word describing the cv-qualification and other
817 // attributes of the type pointed to.
Anders Carlsson08148092009-12-30 23:47:56 +0000818 unsigned Flags = ComputeQualifierFlags(PointeeTy.getQualifiers());
Anders Carlsson8d145152009-12-20 22:30:54 +0000819
820 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000821
822 // Itanium C++ ABI 2.9.5p7:
823 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
824 // incomplete class type, the incomplete target type flag is set.
825 if (ContainsIncompleteClassType(PointeeTy))
826 Flags |= PTI_Incomplete;
827
Anders Carlsson8d145152009-12-20 22:30:54 +0000828 if (IsIncompleteClassType(ClassType))
829 Flags |= PTI_ContainingClassIncomplete;
830
Anders Carlsson8d145152009-12-20 22:30:54 +0000831 const llvm::Type *UnsignedIntLTy =
832 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000833 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000834
835 // Itanium C++ ABI 2.9.5p7:
836 // __pointee is a pointer to the std::type_info derivation for the
837 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000838 llvm::Constant *PointeeTypeInfo =
839 RTTIBuilder(CGM).BuildTypeInfo(PointeeTy.getUnqualifiedType());
840 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000841
842 // Itanium C++ ABI 2.9.5p9:
843 // __context is a pointer to an abi::__class_type_info corresponding to the
844 // class type containing the member pointed to
845 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000846 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000847}
848
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000849llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000850 if (!getContext().getLangOptions().RTTI) {
851 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
852 return llvm::Constant::getNullValue(Int8PtrTy);
853 }
854
Anders Carlsson531d55f2009-12-31 17:43:53 +0000855 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000856}