blob: 4e24bd26aaf4d492f23995fbabd59e6aa5a219dc [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.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000151 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false);
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
Douglas Gregor031b3712010-03-31 00:15:35 +0000330 switch (Ty->getLinkage()) {
331 case NoLinkage:
332 case InternalLinkage:
333 case UniqueExternalLinkage:
334 return llvm::GlobalValue::InternalLinkage;
Anders Carlsson978ef682009-12-29 21:58:32 +0000335
Douglas Gregor031b3712010-03-31 00:15:35 +0000336 case ExternalLinkage:
337 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
338 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
339 if (RD->isDynamicClass())
340 return CodeGenModule::getVtableLinkage(RD);
Mike Stumpc8f76f52009-12-24 01:10:27 +0000341 }
Douglas Gregor031b3712010-03-31 00:15:35 +0000342
Mike Stumpc8f76f52009-12-24 01:10:27 +0000343 return llvm::GlobalValue::WeakODRLinkage;
344 }
Anders Carlsson978ef682009-12-29 21:58:32 +0000345
Anders Carlsson8d145152009-12-20 22:30:54 +0000346 return llvm::GlobalValue::WeakODRLinkage;
347}
348
Anders Carlssonf64531a2009-12-30 01:00:12 +0000349// CanUseSingleInheritance - Return whether the given record decl has a "single,
350// public, non-virtual base at offset zero (i.e. the derived class is dynamic
351// iff the base is)", according to Itanium C++ ABI, 2.95p6b.
352static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
353 // Check the number of bases.
354 if (RD->getNumBases() != 1)
355 return false;
356
357 // Get the base.
358 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
359
360 // Check that the base is not virtual.
361 if (Base->isVirtual())
362 return false;
363
364 // Check that the base is public.
365 if (Base->getAccessSpecifier() != AS_public)
366 return false;
367
368 // Check that the class is dynamic iff the base is.
369 const CXXRecordDecl *BaseDecl =
370 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
371 if (!BaseDecl->isEmpty() &&
372 BaseDecl->isDynamicClass() != RD->isDynamicClass())
373 return false;
374
375 return true;
376}
377
Anders Carlsson8d145152009-12-20 22:30:54 +0000378void RTTIBuilder::BuildVtablePointer(const Type *Ty) {
379 const char *VtableName;
380
381 switch (Ty->getTypeClass()) {
382 default: assert(0 && "Unhandled type!");
Anders Carlsson978ef682009-12-29 21:58:32 +0000383
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000384 case Type::Builtin:
Douglas Gregor031b3712010-03-31 00:15:35 +0000385 // GCC treats vector types as fundamental types.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000386 case Type::Vector:
387 case Type::ExtVector:
Anders Carlsson08148092009-12-30 23:47:56 +0000388 // abi::__fundamental_type_info.
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000389 VtableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
390 break;
391
Anders Carlsson978ef682009-12-29 21:58:32 +0000392 case Type::ConstantArray:
393 case Type::IncompleteArray:
Anders Carlsson08148092009-12-30 23:47:56 +0000394 // abi::__array_type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000395 VtableName = "_ZTVN10__cxxabiv117__array_type_infoE";
396 break;
397
398 case Type::FunctionNoProto:
399 case Type::FunctionProto:
Anders Carlsson08148092009-12-30 23:47:56 +0000400 // abi::__function_type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000401 VtableName = "_ZTVN10__cxxabiv120__function_type_infoE";
402 break;
403
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000404 case Type::Enum:
Anders Carlsson08148092009-12-30 23:47:56 +0000405 // abi::__enum_type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000406 VtableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
407 break;
408
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000409 case Type::Record: {
410 const CXXRecordDecl *RD =
411 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
Anders Carlsson08148092009-12-30 23:47:56 +0000412
John McCall86ff3082010-02-04 22:26:26 +0000413 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson08148092009-12-30 23:47:56 +0000414 // abi::__class_type_info.
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000415 VtableName = "_ZTVN10__cxxabiv117__class_type_infoE";
Anders Carlssonf64531a2009-12-30 01:00:12 +0000416 } else if (CanUseSingleInheritance(RD)) {
Anders Carlsson08148092009-12-30 23:47:56 +0000417 // abi::__si_class_type_info.
Anders Carlssonf64531a2009-12-30 01:00:12 +0000418 VtableName = "_ZTVN10__cxxabiv120__si_class_type_infoE";
419 } else {
Anders Carlsson08148092009-12-30 23:47:56 +0000420 // abi::__vmi_class_type_info.
421 VtableName = "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000422 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000423
424 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000425 }
426
Anders Carlsson8d145152009-12-20 22:30:54 +0000427 case Type::Pointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000428 // abi::__pointer_type_info.
Anders Carlsson8d145152009-12-20 22:30:54 +0000429 VtableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
430 break;
Anders Carlsson978ef682009-12-29 21:58:32 +0000431
Anders Carlsson8d145152009-12-20 22:30:54 +0000432 case Type::MemberPointer:
Anders Carlsson08148092009-12-30 23:47:56 +0000433 // abi::__pointer_to_member_type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000434 VtableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
Anders Carlsson8d145152009-12-20 22:30:54 +0000435 break;
436 }
437
438 llvm::Constant *Vtable =
439 CGM.getModule().getOrInsertGlobal(VtableName, Int8PtrTy);
440
441 const llvm::Type *PtrDiffTy =
442 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
443
444 // The vtable address point is 2.
445 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
446 Vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(Vtable, &Two, 1);
447 Vtable = llvm::ConstantExpr::getBitCast(Vtable, Int8PtrTy);
448
Anders Carlsson531d55f2009-12-31 17:43:53 +0000449 Fields.push_back(Vtable);
Anders Carlsson8d145152009-12-20 22:30:54 +0000450}
451
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000452llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000453 // We want to operate on the canonical type.
454 Ty = CGM.getContext().getCanonicalType(Ty);
455
456 // Check if we've already emitted an RTTI descriptor for this type.
457 llvm::SmallString<256> OutName;
458 CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
459 llvm::StringRef Name = OutName.str();
460
461 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
462 if (OldGV && !OldGV->isDeclaration())
463 return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
464
465 // Check if there is already an external RTTI descriptor for this type.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000466 if (!Force && ShouldUseExternalRTTIDescriptor(Ty))
Anders Carlsson8d145152009-12-20 22:30:54 +0000467 return GetAddrOfExternalRTTIDescriptor(Ty);
468
469 llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(Ty);
470
471 // Add the vtable pointer.
472 BuildVtablePointer(cast<Type>(Ty));
473
474 // And the name.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000475 Fields.push_back(BuildName(Ty, DecideHidden(Ty), Linkage));
Anders Carlsson8d145152009-12-20 22:30:54 +0000476
477 switch (Ty->getTypeClass()) {
478 default: assert(false && "Unhandled type class!");
Anders Carlsson8d145152009-12-20 22:30:54 +0000479
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000480 // GCC treats vector types as fundamental types.
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000481 case Type::Builtin:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000482 case Type::Vector:
483 case Type::ExtVector:
484 // Itanium C++ ABI 2.9.5p4:
485 // abi::__fundamental_type_info adds no data members to std::type_info.
486 break;
487
Anders Carlsson978ef682009-12-29 21:58:32 +0000488 case Type::ConstantArray:
489 case Type::IncompleteArray:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000490 // Itanium C++ ABI 2.9.5p5:
491 // abi::__array_type_info adds no data members to std::type_info.
Anders Carlsson978ef682009-12-29 21:58:32 +0000492 break;
493
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000494 case Type::FunctionNoProto:
495 case Type::FunctionProto:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000496 // Itanium C++ ABI 2.9.5p5:
497 // abi::__function_type_info adds no data members to std::type_info.
Anders Carlsson09b6e6e2009-12-29 20:20:19 +0000498 break;
499
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000500 case Type::Enum:
Anders Carlssonc8cfd632009-12-29 22:30:11 +0000501 // Itanium C++ ABI 2.9.5p5:
502 // abi::__enum_type_info adds no data members to std::type_info.
Anders Carlsson9c7b6bb2009-12-29 22:13:01 +0000503 break;
504
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000505 case Type::Record: {
506 const CXXRecordDecl *RD =
507 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
John McCall86ff3082010-02-04 22:26:26 +0000508 if (!RD->hasDefinition() || !RD->getNumBases()) {
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000509 // We don't need to emit any fields.
510 break;
511 }
Anders Carlssonf64531a2009-12-30 01:00:12 +0000512
Anders Carlsson08148092009-12-30 23:47:56 +0000513 if (CanUseSingleInheritance(RD))
Anders Carlssonf64531a2009-12-30 01:00:12 +0000514 BuildSIClassTypeInfo(RD);
Anders Carlsson08148092009-12-30 23:47:56 +0000515 else
516 BuildVMIClassTypeInfo(RD);
517
518 break;
Anders Carlsson625c1ae2009-12-21 00:41:42 +0000519 }
520
Anders Carlsson8d145152009-12-20 22:30:54 +0000521 case Type::Pointer:
522 BuildPointerTypeInfo(cast<PointerType>(Ty));
523 break;
524
525 case Type::MemberPointer:
526 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
527 break;
528 }
529
530 llvm::Constant *Init =
Anders Carlsson531d55f2009-12-31 17:43:53 +0000531 llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(),
Anders Carlsson8d145152009-12-20 22:30:54 +0000532 /*Packed=*/false);
533
534 llvm::GlobalVariable *GV =
535 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
536 /*Constant=*/true, Linkage, Init, Name);
537
538 // If there's already an old global variable, replace it with the new one.
539 if (OldGV) {
540 GV->takeName(OldGV);
541 llvm::Constant *NewPtr =
542 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
543 OldGV->replaceAllUsesWith(NewPtr);
544 OldGV->eraseFromParent();
545 }
546
547 return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
548}
549
Anders Carlsson08148092009-12-30 23:47:56 +0000550/// ComputeQualifierFlags - Compute the pointer type info flags from the
Anders Carlsson8d145152009-12-20 22:30:54 +0000551/// given qualifier.
Anders Carlsson08148092009-12-30 23:47:56 +0000552static unsigned ComputeQualifierFlags(Qualifiers Quals) {
Anders Carlsson8d145152009-12-20 22:30:54 +0000553 unsigned Flags = 0;
554
555 if (Quals.hasConst())
556 Flags |= RTTIBuilder::PTI_Const;
557 if (Quals.hasVolatile())
558 Flags |= RTTIBuilder::PTI_Volatile;
559 if (Quals.hasRestrict())
560 Flags |= RTTIBuilder::PTI_Restrict;
561
562 return Flags;
563}
564
Anders Carlssonf64531a2009-12-30 01:00:12 +0000565/// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
566/// inheritance, according to the Itanium C++ ABI, 2.95p6b.
567void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
568 // Itanium C++ ABI 2.9.5p6b:
569 // It adds to abi::__class_type_info a single member pointing to the
570 // type_info structure for the base type,
Anders Carlsson531d55f2009-12-31 17:43:53 +0000571 llvm::Constant *BaseTypeInfo =
572 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
573 Fields.push_back(BaseTypeInfo);
Anders Carlssonf64531a2009-12-30 01:00:12 +0000574}
575
Anders Carlsson08148092009-12-30 23:47:56 +0000576/// SeenBases - Contains virtual and non-virtual bases seen when traversing
577/// a class hierarchy.
578struct SeenBases {
579 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
580 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
581};
582
583/// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
584/// abi::__vmi_class_type_info.
585///
586static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
587 SeenBases &Bases) {
588
589 unsigned Flags = 0;
590
591 const CXXRecordDecl *BaseDecl =
592 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
593
594 if (Base->isVirtual()) {
595 if (Bases.VirtualBases.count(BaseDecl)) {
596 // If this virtual base has been seen before, then the class is diamond
597 // shaped.
598 Flags |= RTTIBuilder::VMI_DiamondShaped;
599 } else {
600 if (Bases.NonVirtualBases.count(BaseDecl))
601 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
602
603 // Mark the virtual base as seen.
604 Bases.VirtualBases.insert(BaseDecl);
605 }
606 } else {
607 if (Bases.NonVirtualBases.count(BaseDecl)) {
608 // If this non-virtual base has been seen before, then the class has non-
609 // diamond shaped repeated inheritance.
610 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
611 } else {
612 if (Bases.VirtualBases.count(BaseDecl))
613 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
614
615 // Mark the non-virtual base as seen.
616 Bases.NonVirtualBases.insert(BaseDecl);
617 }
618 }
619
620 // Walk all bases.
621 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
622 E = BaseDecl->bases_end(); I != E; ++I)
623 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
624
625 return Flags;
626}
627
628static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
629 unsigned Flags = 0;
630 SeenBases Bases;
631
632 // Walk all bases.
633 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
634 E = RD->bases_end(); I != E; ++I)
635 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
636
637 return Flags;
638}
639
640/// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
641/// classes with bases that do not satisfy the abi::__si_class_type_info
642/// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
643void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
644 const llvm::Type *UnsignedIntLTy =
645 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
646
647 // Itanium C++ ABI 2.9.5p6c:
648 // __flags is a word with flags describing details about the class
649 // structure, which may be referenced by using the __flags_masks
650 // enumeration. These flags refer to both direct and indirect bases.
651 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000652 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson08148092009-12-30 23:47:56 +0000653
654 // Itanium C++ ABI 2.9.5p6c:
655 // __base_count is a word with the number of direct proper base class
656 // descriptions that follow.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000657 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
Anders Carlsson08148092009-12-30 23:47:56 +0000658
659 if (!RD->getNumBases())
660 return;
661
662 const llvm::Type *LongLTy =
663 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
664
665 // Now add the base class descriptions.
666
667 // Itanium C++ ABI 2.9.5p6c:
668 // __base_info[] is an array of base class descriptions -- one for every
669 // direct proper base. Each description is of the type:
670 //
671 // struct abi::__base_class_type_info {
672 // public:
673 // const __class_type_info *__base_type;
674 // long __offset_flags;
675 //
676 // enum __offset_flags_masks {
677 // __virtual_mask = 0x1,
678 // __public_mask = 0x2,
679 // __offset_shift = 8
680 // };
681 // };
682 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
683 E = RD->bases_end(); I != E; ++I) {
684 const CXXBaseSpecifier *Base = I;
685
686 // The __base_type member points to the RTTI for the base type.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000687 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
Anders Carlsson08148092009-12-30 23:47:56 +0000688
689 const CXXRecordDecl *BaseDecl =
690 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
691
692 int64_t OffsetFlags = 0;
693
694 // All but the lower 8 bits of __offset_flags are a signed offset.
695 // For a non-virtual base, this is the offset in the object of the base
696 // subobject. For a virtual base, this is the offset in the virtual table of
697 // the virtual base offset for the virtual base referenced (negative).
698 if (Base->isVirtual())
Anders Carlssonaf440352010-03-23 04:11:45 +0000699 OffsetFlags = CGM.getVTables().getVirtualBaseOffsetOffset(RD, BaseDecl);
Anders Carlsson08148092009-12-30 23:47:56 +0000700 else {
701 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
702 OffsetFlags = Layout.getBaseClassOffset(BaseDecl) / 8;
703 };
704
705 OffsetFlags <<= 8;
706
707 // The low-order byte of __offset_flags contains flags, as given by the
708 // masks from the enumeration __offset_flags_masks.
709 if (Base->isVirtual())
710 OffsetFlags |= BCTI_Virtual;
711 if (Base->getAccessSpecifier() == AS_public)
712 OffsetFlags |= BCTI_Public;
713
Anders Carlsson531d55f2009-12-31 17:43:53 +0000714 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
Anders Carlsson08148092009-12-30 23:47:56 +0000715 }
716}
717
Anders Carlsson8d145152009-12-20 22:30:54 +0000718/// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
719/// used for pointer types.
720void RTTIBuilder::BuildPointerTypeInfo(const PointerType *Ty) {
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000721 QualType PointeeTy = Ty->getPointeeType();
Anders Carlsson8d145152009-12-20 22:30:54 +0000722
723 // Itanium C++ ABI 2.9.5p7:
724 // __flags is a flag word describing the cv-qualification and other
725 // attributes of the type pointed to
Anders Carlsson08148092009-12-30 23:47:56 +0000726 unsigned Flags = ComputeQualifierFlags(PointeeTy.getQualifiers());
Anders Carlsson8d145152009-12-20 22:30:54 +0000727
728 // Itanium C++ ABI 2.9.5p7:
729 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
730 // incomplete class type, the incomplete target type flag is set.
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000731 if (ContainsIncompleteClassType(PointeeTy))
Anders Carlsson8d145152009-12-20 22:30:54 +0000732 Flags |= PTI_Incomplete;
733
734 const llvm::Type *UnsignedIntLTy =
735 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000736 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000737
738 // Itanium C++ ABI 2.9.5p7:
739 // __pointee is a pointer to the std::type_info derivation for the
740 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000741 llvm::Constant *PointeeTypeInfo =
742 RTTIBuilder(CGM).BuildTypeInfo(PointeeTy.getUnqualifiedType());
743 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000744}
745
746/// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
747/// struct, used for member pointer types.
748void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
749 QualType PointeeTy = Ty->getPointeeType();
750
751 // Itanium C++ ABI 2.9.5p7:
752 // __flags is a flag word describing the cv-qualification and other
753 // attributes of the type pointed to.
Anders Carlsson08148092009-12-30 23:47:56 +0000754 unsigned Flags = ComputeQualifierFlags(PointeeTy.getQualifiers());
Anders Carlsson8d145152009-12-20 22:30:54 +0000755
756 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
Anders Carlsson17fa6f92009-12-20 23:37:55 +0000757
758 // Itanium C++ ABI 2.9.5p7:
759 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
760 // incomplete class type, the incomplete target type flag is set.
761 if (ContainsIncompleteClassType(PointeeTy))
762 Flags |= PTI_Incomplete;
763
Anders Carlsson8d145152009-12-20 22:30:54 +0000764 if (IsIncompleteClassType(ClassType))
765 Flags |= PTI_ContainingClassIncomplete;
766
Anders Carlsson8d145152009-12-20 22:30:54 +0000767 const llvm::Type *UnsignedIntLTy =
768 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
Anders Carlsson531d55f2009-12-31 17:43:53 +0000769 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
Anders Carlsson8d145152009-12-20 22:30:54 +0000770
771 // Itanium C++ ABI 2.9.5p7:
772 // __pointee is a pointer to the std::type_info derivation for the
773 // unqualified type being pointed to.
Anders Carlsson531d55f2009-12-31 17:43:53 +0000774 llvm::Constant *PointeeTypeInfo =
775 RTTIBuilder(CGM).BuildTypeInfo(PointeeTy.getUnqualifiedType());
776 Fields.push_back(PointeeTypeInfo);
Anders Carlsson8d145152009-12-20 22:30:54 +0000777
778 // Itanium C++ ABI 2.9.5p9:
779 // __context is a pointer to an abi::__class_type_info corresponding to the
780 // class type containing the member pointed to
781 // (e.g., the "A" in "int A::*").
Anders Carlsson531d55f2009-12-31 17:43:53 +0000782 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
Anders Carlsson8d145152009-12-20 22:30:54 +0000783}
784
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000785llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty) {
Anders Carlsson31b7f522009-12-11 02:46:30 +0000786 if (!getContext().getLangOptions().RTTI) {
787 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
788 return llvm::Constant::getNullValue(Int8PtrTy);
789 }
790
Anders Carlsson531d55f2009-12-31 17:43:53 +0000791 return RTTIBuilder(*this).BuildTypeInfo(Ty);
Anders Carlsson31b7f522009-12-11 02:46:30 +0000792}
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000793
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000794void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
795 QualType PointerType = Context.getPointerType(Type);
796 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
797 RTTIBuilder(*this).BuildTypeInfo(Type, true);
798 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
799 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
800}
801
802void CodeGenModule::EmitFundamentalRTTIDescriptors() {
Rafael Espindolad1a5c312010-03-27 02:52:14 +0000803 QualType FundamentalTypes[] = { Context.VoidTy, Context.Char32Ty,
804 Context.Char16Ty, Context.UnsignedLongLongTy,
805 Context.LongLongTy, Context.WCharTy,
806 Context.UnsignedShortTy, Context.ShortTy,
807 Context.UnsignedLongTy, Context.LongTy,
808 Context.UnsignedIntTy, Context.IntTy,
809 Context.UnsignedCharTy, Context.FloatTy,
810 Context.LongDoubleTy, Context.DoubleTy,
811 Context.CharTy, Context.BoolTy,
812 Context.SignedCharTy };
813 for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
814 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
815}