blob: db6c5075ede4ec827f27e98c4f7ed9612d7aeecf [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());
259 if (!RD->isDynamicClass())
260 return false;
261
262 // Get the key function.
263 const CXXMethodDecl *KeyFunction = RD->getASTContext().getKeyFunction(RD);
264 if (KeyFunction && !KeyFunction->getBody()) {
265 // The class has a key function, but it is not defined in this translation
266 // unit, so we should use the external descriptor for it.
267 return true;
268 }
Anders Carlsson8d145152009-12-20 22:30:54 +0000269 }
270
271 return false;
272}
273
274/// IsIncompleteClassType - Returns whether the given record type is incomplete.
275static bool IsIncompleteClassType(const RecordType *RecordTy) {
276 return !RecordTy->getDecl()->isDefinition();
277}
278
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000279/// ContainsIncompleteClassType - Returns whether the given type contains an
280/// incomplete class type. This is true if
281///
282/// * The given type is an incomplete class type.
283/// * The given type is a pointer type whose pointee type contains an
284/// incomplete class type.
285/// * The given type is a member pointer type whose class is an incomplete
286/// class type.
287/// * The given type is a member pointer type whoise pointee type contains an
288/// incomplete class type.
Anders Carlsson8d145152009-12-20 22:30:54 +0000289/// is an indirect or direct pointer to an incomplete class type.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000290static bool ContainsIncompleteClassType(QualType Ty) {
291 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
292 if (IsIncompleteClassType(RecordTy))
293 return true;
294 }
295
296 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
297 return ContainsIncompleteClassType(PointerTy->getPointeeType());
298
299 if (const MemberPointerType *MemberPointerTy =
300 dyn_cast<MemberPointerType>(Ty)) {
301 // Check if the class type is incomplete.
302 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
303 if (IsIncompleteClassType(ClassType))
304 return true;
305
306 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
Anders Carlsson8d145152009-12-20 22:30:54 +0000307 }
308
309 return false;
310}
311
312/// getTypeInfoLinkage - Return the linkage that the type info and type info
313/// name constants should have for the given type.
314static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(QualType Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000315 // Itanium C++ ABI 2.9.5p7:
316 // In addition, it and all of the intermediate abi::__pointer_type_info
317 // structs in the chain down to the abi::__class_type_info for the
318 // incomplete class type must be prevented from resolving to the
319 // corresponding type_info structs for the complete class type, possibly
320 // by making them local static objects. Finally, a dummy class RTTI is
321 // generated for the incomplete type that will not resolve to the final
322 // complete class RTTI (because the latter need not exist), possibly by
323 // making it a local static object.
324 if (ContainsIncompleteClassType(Ty))
325 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000326
Anders Carlsson978ef682009-12-29 21:58:32 +0000327 switch (Ty->getTypeClass()) {
328 default:
329 // FIXME: We need to add code to handle all types.
330 assert(false && "Unhandled type!");
331 break;
332
333 case Type::Pointer: {
334 const PointerType *PointerTy = cast<PointerType>(Ty);
335
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000336 // If the pointee type has internal linkage, then the pointer type needs to
337 // have it as well.
338 if (getTypeInfoLinkage(PointerTy->getPointeeType()) ==
339 llvm::GlobalVariable::InternalLinkage)
340 return llvm::GlobalVariable::InternalLinkage;
341
342 return llvm::GlobalVariable::WeakODRLinkage;
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000343 }
344
345 case Type::Enum: {
346 const EnumType *EnumTy = cast<EnumType>(Ty);
347 const EnumDecl *ED = EnumTy->getDecl();
348
349 // If we're in an anonymous namespace, then we always want internal linkage.
350 if (ED->isInAnonymousNamespace() || !ED->hasLinkage())
351 return llvm::GlobalVariable::InternalLinkage;
352
353 return llvm::GlobalValue::WeakODRLinkage;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000354 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000355
356 case Type::Record: {
357 const RecordType *RecordTy = cast<RecordType>(Ty);
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000358 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
359
360 // If we're in an anonymous namespace, then we always want internal linkage.
361 if (RD->isInAnonymousNamespace() || !RD->hasLinkage())
362 return llvm::GlobalVariable::InternalLinkage;
363
364 if (!RD->isDynamicClass())
365 return llvm::GlobalValue::WeakODRLinkage;
366
367 // Get the key function.
368 const CXXMethodDecl *KeyFunction = RD->getASTContext().getKeyFunction(RD);
369 if (!KeyFunction) {
370 // There is no key function, the RTTI descriptor is emitted with weak_odr
371 // linkage.
372 return llvm::GlobalValue::WeakODRLinkage;
373 }
374
Anders Carlssone8f90382009-12-31 19:36:25 +0000375 // If the key function is defined, but inlined, then the RTTI descriptor is
376 // emitted with weak_odr linkage.
377 const FunctionDecl* KeyFunctionDefinition;
378 KeyFunction->getBody(KeyFunctionDefinition);
379
380 if (KeyFunctionDefinition->isInlined())
381 return llvm::GlobalValue::WeakODRLinkage;
382
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000383 // Otherwise, the RTTI descriptor is emitted with external linkage.
384 return llvm::GlobalValue::ExternalLinkage;
385 }
386
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000387 case Type::Vector:
388 case Type::ExtVector:
Anders Carlsson978ef682009-12-29 21:58:32 +0000389 case Type::Builtin:
Mike Stump8d9fb9b2009-12-23 22:48:20 +0000390 return llvm::GlobalValue::WeakODRLinkage;
Mike Stump8d9fb9b2009-12-23 22:48:20 +0000391
Anders Carlsson978ef682009-12-29 21:58:32 +0000392 case Type::FunctionProto: {
393 const FunctionProtoType *FPT = cast<FunctionProtoType>(Ty);
394
395 // Check the return type.
396 if (getTypeInfoLinkage(FPT->getResultType()) ==
397 llvm::GlobalValue::InternalLinkage)
Mike Stumpc8f76f52009-12-24 01:10:27 +0000398 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson978ef682009-12-29 21:58:32 +0000399
400 // Check the parameter types.
401 for (unsigned i = 0; i != FPT->getNumArgs(); ++i) {
402 if (getTypeInfoLinkage(FPT->getArgType(i)) ==
403 llvm::GlobalValue::InternalLinkage)
404 return llvm::GlobalValue::InternalLinkage;
Mike Stumpc8f76f52009-12-24 01:10:27 +0000405 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000406
Mike Stumpc8f76f52009-12-24 01:10:27 +0000407 return llvm::GlobalValue::WeakODRLinkage;
408 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000409
410 case Type::ConstantArray:
411 case Type::IncompleteArray: {
412 const ArrayType *AT = cast<ArrayType>(Ty);
Mike Stumpc8f76f52009-12-24 01:10:27 +0000413
Anders Carlsson978ef682009-12-29 21:58:32 +0000414 // Check the element type.
415 if (getTypeInfoLinkage(AT->getElementType()) ==
416 llvm::GlobalValue::InternalLinkage)
417 return llvm::GlobalValue::InternalLinkage;
418 }
419
420 }
421
Anders Carlsson8d145152009-12-20 22:30:54 +0000422 return llvm::GlobalValue::WeakODRLinkage;
423}
424
Anders Carlssonf64531a2009-12-30 01:00:12 +0000425// CanUseSingleInheritance - Return whether the given record decl has a "single,
426// public, non-virtual base at offset zero (i.e. the derived class is dynamic
427// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
428static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
429 // Check the number of bases.
430 if (RD->getNumBases() != 1)
431 return false;
432
433 // Get the base.
434 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
435
436 // Check that the base is not virtual.
437 if (Base->isVirtual())
438 return false;
439
440 // Check that the base is public.
441 if (Base->getAccessSpecifier() != AS_public)
442 return false;
443
444 // Check that the class is dynamic iff the base is.
445 const CXXRecordDecl *BaseDecl =
446 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
447 if (!BaseDecl->isEmpty() &&
448 BaseDecl->isDynamicClass() != RD->isDynamicClass())
449 return false;
450
451 return true;
452}
453
Anders Carlsson8d145152009-12-20 22:30:54 +0000454void RTTIBuilder::BuildVtablePointer(const Type *Ty) {
455 const char *VtableName;
456
457 switch (Ty->getTypeClass()) {
458 default: assert(0 && "Unhandled type!");
Anders Carlsson978ef682009-12-29 21:58:32 +0000459
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000460 // GCC treats vector types as fundamental types.
461 case Type::Vector:
462 case Type::ExtVector:
Anders Carlsson08148092009-12-30 23:47:56 +0000463 // abi::__fundamental_type_info.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000464 VtableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
465 break;
466
Anders Carlsson978ef682009-12-29 21:58:32 +0000467 case Type::ConstantArray:
468 case Type::IncompleteArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000469 // abi::__array_type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000470 VtableName = "_ZTVN10__cxxabiv117__array_type_infoE";
471 break;
472
473 case Type::FunctionNoProto:
474 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000475 // abi::__function_type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000476 VtableName = "_ZTVN10__cxxabiv120__function_type_infoE";
477 break;
478
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000479 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000480 // abi::__enum_type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000481 VtableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
482 break;
483
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000484 case Type::Record: {
485 const CXXRecordDecl *RD =
486 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000487
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000488 if (!RD->getNumBases()) {
Anders Carlsson08148092009-12-30 23:47:56 +0000489 // abi::__class_type_info.
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000490 VtableName = "_ZTVN10__cxxabiv117__class_type_infoE";
Anders Carlssonf64531a2009-12-30 01:00:12 +0000491 } else if (CanUseSingleInheritance(RD)) {
Anders Carlsson08148092009-12-30 23:47:56 +0000492 // abi::__si_class_type_info.
Anders Carlssonf64531a2009-12-30 01:00:12 +0000493 VtableName = "_ZTVN10__cxxabiv120__si_class_type_infoE";
494 } else {
Anders Carlsson08148092009-12-30 23:47:56 +0000495 // abi::__vmi_class_type_info.
496 VtableName = "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000497 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000498
499 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000500 }
501
Anders Carlsson8d145152009-12-20 22:30:54 +0000502 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000503 // abi::__pointer_type_info.
Anders Carlsson8d145152009-12-20 22:30:54 +0000504 VtableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
505 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000506
Anders Carlsson8d145152009-12-20 22:30:54 +0000507 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000508 // abi::__pointer_to_member_type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000509 VtableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000510 break;
511 }
512
513 llvm::Constant *Vtable =
514 CGM.getModule().getOrInsertGlobal(VtableName, Int8PtrTy);
515
516 const llvm::Type *PtrDiffTy =
517 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
518
519 // The vtable address point is 2.
520 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
521 Vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(Vtable, &Two, 1);
522 Vtable = llvm::ConstantExpr::getBitCast(Vtable, Int8PtrTy);
523
Anders Carlsson531d55f2009-12-31 17:43:53 +0000524 Fields.push_back(Vtable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000525}
526
527llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty) {
528 // We want to operate on the canonical type.
529 Ty = CGM.getContext().getCanonicalType(Ty);
530
531 // Check if we've already emitted an RTTI descriptor for this type.
532 llvm::SmallString<256> OutName;
533 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
534 llvm::StringRef Name = OutName.str();
535
536 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
537 if (OldGV && !OldGV->isDeclaration())
538 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
539
540 // Check if there is already an external RTTI descriptor for this type.
541 if (ShouldUseExternalRTTIDescriptor(Ty))
542 return GetAddrOfExternalRTTIDescriptor(Ty);
543
544 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(Ty);
545
546 // Add the vtable pointer.
547 BuildVtablePointer(cast<Type>(Ty));
548
549 // And the name.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000550 Fields.push_back(BuildName(Ty, DecideHidden(Ty), Linkage));
Anders Carlsson8d145152009-12-20 22:30:54 +0000551
552 switch (Ty->getTypeClass()) {
553 default: assert(false && "Unhandled type class!");
554 case Type::Builtin:
555 assert(false && "Builtin type info must be in the standard library!");
556 break;
557
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000558 // GCC treats vector types as fundamental types.
559 case Type::Vector:
560 case Type::ExtVector:
561 // Itanium C++ ABI 2.9.5p4:
562 // abi::__fundamental_type_info adds no data members to std::type_info.
563 break;
564
Anders Carlsson978ef682009-12-29 21:58:32 +0000565 case Type::ConstantArray:
566 case Type::IncompleteArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000567 // Itanium C++ ABI 2.9.5p5:
568 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000569 break;
570
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000571 case Type::FunctionNoProto:
572 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000573 // Itanium C++ ABI 2.9.5p5:
574 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000575 break;
576
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000577 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000578 // Itanium C++ ABI 2.9.5p5:
579 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000580 break;
581
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000582 case Type::Record: {
583 const CXXRecordDecl *RD =
584 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
585 if (!RD->getNumBases()) {
586 // We don't need to emit any fields.
587 break;
588 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000589
Anders Carlsson08148092009-12-30 23:47:56 +0000590 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000591 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000592 else
593 BuildVMIClassTypeInfo(RD);
594
595 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000596 }
597
Anders Carlsson8d145152009-12-20 22:30:54 +0000598 case Type::Pointer:
599 BuildPointerTypeInfo(cast<PointerType>(Ty));
600 break;
601
602 case Type::MemberPointer:
603 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
604 break;
605 }
606
607 llvm::Constant *Init =
Anders Carlsson531d55f2009-12-31 17:43:53 +0000608 llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
Anders Carlsson8d145152009-12-20 22:30:54 +0000609 /*Packed=*/false);
610
611 llvm::GlobalVariable *GV =
612 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
613 /*Constant=*/true, Linkage, Init, Name);
614
615 // If there's already an old global variable, replace it with the new one.
616 if (OldGV) {
617 GV->takeName(OldGV);
618 llvm::Constant *NewPtr =
619 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
620 OldGV->replaceAllUsesWith(NewPtr);
621 OldGV->eraseFromParent();
622 }
623
624 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
625}
626
Anders Carlsson08148092009-12-30 23:47:56 +0000627/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000628/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000629static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000630 unsigned Flags = 0;
631
632 if (Quals.hasConst())
633 Flags |= RTTIBuilder::PTI_Const;
634 if (Quals.hasVolatile())
635 Flags |= RTTIBuilder::PTI_Volatile;
636 if (Quals.hasRestrict())
637 Flags |= RTTIBuilder::PTI_Restrict;
638
639 return Flags;
640}
641
Anders Carlssonf64531a2009-12-30 01:00:12 +0000642/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
643/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
644void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
645 // Itanium C++ ABI 2.9.5p6b:
646 // It adds to abi::__class_type_info a single member pointing to the
647 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000648 llvm::Constant *BaseTypeInfo =
649 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
650 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000651}
652
Anders Carlsson08148092009-12-30 23:47:56 +0000653/// SeenBases - Contains virtual and non-virtual bases seen when traversing
654/// a class hierarchy.
655struct SeenBases {
656 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
657 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
658};
659
660/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
661/// abi::__vmi_class_type_info.
662///
663static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
664 SeenBases &Bases) {
665
666 unsigned Flags = 0;
667
668 const CXXRecordDecl *BaseDecl =
669 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
670
671 if (Base->isVirtual()) {
672 if (Bases.VirtualBases.count(BaseDecl)) {
673 // If this virtual base has been seen before, then the class is diamond
674 // shaped.
675 Flags |= RTTIBuilder::VMI_DiamondShaped;
676 } else {
677 if (Bases.NonVirtualBases.count(BaseDecl))
678 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
679
680 // Mark the virtual base as seen.
681 Bases.VirtualBases.insert(BaseDecl);
682 }
683 } else {
684 if (Bases.NonVirtualBases.count(BaseDecl)) {
685 // If this non-virtual base has been seen before, then the class has non-
686 // diamond shaped repeated inheritance.
687 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
688 } else {
689 if (Bases.VirtualBases.count(BaseDecl))
690 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
691
692 // Mark the non-virtual base as seen.
693 Bases.NonVirtualBases.insert(BaseDecl);
694 }
695 }
696
697 // Walk all bases.
698 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
699 E = BaseDecl->bases_end(); I != E; ++I)
700 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
701
702 return Flags;
703}
704
705static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
706 unsigned Flags = 0;
707 SeenBases Bases;
708
709 // Walk all bases.
710 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
711 E = RD->bases_end(); I != E; ++I)
712 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
713
714 return Flags;
715}
716
717/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
718/// classes with bases that do not satisfy the abi::__si_class_type_info
719/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
720void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
721 const llvm::Type *UnsignedIntLTy =
722 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
723
724 // Itanium C++ ABI 2.9.5p6c:
725 // __flags is a word with flags describing details about the class
726 // structure, which may be referenced by using the __flags_masks
727 // enumeration. These flags refer to both direct and indirect bases.
728 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000729 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000730
731 // Itanium C++ ABI 2.9.5p6c:
732 // __base_count is a word with the number of direct proper base class
733 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000734 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000735
736 if (!RD->getNumBases())
737 return;
738
739 const llvm::Type *LongLTy =
740 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
741
742 // Now add the base class descriptions.
743
744 // Itanium C++ ABI 2.9.5p6c:
745 // __base_info[] is an array of base class descriptions -- one for every
746 // direct proper base. Each description is of the type:
747 //
748 // struct abi::__base_class_type_info {
749 // public:
750 // const __class_type_info *__base_type;
751 // long __offset_flags;
752 //
753 // enum __offset_flags_masks {
754 // __virtual_mask = 0x1,
755 // __public_mask = 0x2,
756 // __offset_shift = 8
757 // };
758 // };
759 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
760 E = RD->bases_end(); I != E; ++I) {
761 const CXXBaseSpecifier *Base = I;
762
763 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000764 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000765
766 const CXXRecordDecl *BaseDecl =
767 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
768
769 int64_t OffsetFlags = 0;
770
771 // All but the lower 8 bits of __offset_flags are a signed offset.
772 // For a non-virtual base, this is the offset in the object of the base
773 // subobject. For a virtual base, this is the offset in the virtual table of
774 // the virtual base offset for the virtual base referenced (negative).
775 if (Base->isVirtual())
776 OffsetFlags = CGM.getVtableInfo().getVirtualBaseOffsetIndex(RD, BaseDecl);
777 else {
778 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
779 OffsetFlags = Layout.getBaseClassOffset(BaseDecl) / 8;
780 };
781
782 OffsetFlags <<= 8;
783
784 // The low-order byte of __offset_flags contains flags, as given by the
785 // masks from the enumeration __offset_flags_masks.
786 if (Base->isVirtual())
787 OffsetFlags |= BCTI_Virtual;
788 if (Base->getAccessSpecifier() == AS_public)
789 OffsetFlags |= BCTI_Public;
790
Anders Carlsson531d55f2009-12-31 17:43:53 +0000791 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000792 }
793}
794
Anders Carlsson8d145152009-12-20 22:30:54 +0000795/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
796/// used for pointer types.
797void RTTIBuilder::BuildPointerTypeInfo(const PointerType *Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000798 QualType PointeeTy = Ty->getPointeeType();
Anders Carlsson8d145152009-12-20 22:30:54 +0000799
800 // Itanium C++ ABI 2.9.5p7:
801 // __flags is a flag word describing the cv-qualification and other
802 // attributes of the type pointed to
Anders Carlsson08148092009-12-30 23:47:56 +0000803 unsigned Flags = ComputeQualifierFlags(PointeeTy.getQualifiers());
Anders Carlsson8d145152009-12-20 22:30:54 +0000804
805 // Itanium C++ ABI 2.9.5p7:
806 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
807 // incomplete class type, the incomplete target type flag is set.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000808 if (ContainsIncompleteClassType(PointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000809 Flags |= PTI_Incomplete;
810
811 const llvm::Type *UnsignedIntLTy =
812 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000813 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000814
815 // Itanium C++ ABI 2.9.5p7:
816 // __pointee is a pointer to the std::type_info derivation for the
817 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000818 llvm::Constant *PointeeTypeInfo =
819 RTTIBuilder(CGM).BuildTypeInfo(PointeeTy.getUnqualifiedType());
820 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000821}
822
823/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
824/// struct, used for member pointer types.
825void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
826 QualType PointeeTy = Ty->getPointeeType();
827
828 // Itanium C++ ABI 2.9.5p7:
829 // __flags is a flag word describing the cv-qualification and other
830 // attributes of the type pointed to.
Anders Carlsson08148092009-12-30 23:47:56 +0000831 unsigned Flags = ComputeQualifierFlags(PointeeTy.getQualifiers());
Anders Carlsson8d145152009-12-20 22:30:54 +0000832
833 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000834
835 // Itanium C++ ABI 2.9.5p7:
836 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
837 // incomplete class type, the incomplete target type flag is set.
838 if (ContainsIncompleteClassType(PointeeTy))
839 Flags |= PTI_Incomplete;
840
Anders Carlsson8d145152009-12-20 22:30:54 +0000841 if (IsIncompleteClassType(ClassType))
842 Flags |= PTI_ContainingClassIncomplete;
843
Anders Carlsson8d145152009-12-20 22:30:54 +0000844 const llvm::Type *UnsignedIntLTy =
845 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000846 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000847
848 // Itanium C++ ABI 2.9.5p7:
849 // __pointee is a pointer to the std::type_info derivation for the
850 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000851 llvm::Constant *PointeeTypeInfo =
852 RTTIBuilder(CGM).BuildTypeInfo(PointeeTy.getUnqualifiedType());
853 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000854
855 // Itanium C++ ABI 2.9.5p9:
856 // __context is a pointer to an abi::__class_type_info corresponding to the
857 // class type containing the member pointed to
858 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000859 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000860}
861
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000862llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000863 if (!getContext().getLangOptions().RTTI) {
864 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
865 return llvm::Constant::getNullValue(Int8PtrTy);
866 }
867
Anders Carlsson531d55f2009-12-31 17:43:53 +0000868 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000869}