1 //===--- CGCXXRTTI.cpp - Emit LLVM Code for C++ RTTI descriptors ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This contains code dealing with C++ code generation of RTTI descriptors.
12 //===----------------------------------------------------------------------===//
14 #include "CodeGenModule.h"
16 #include "CGObjCRuntime.h"
17 #include "clang/AST/RecordLayout.h"
18 #include "clang/AST/Type.h"
19 #include "clang/Frontend/CodeGenOptions.h"
21 using namespace clang;
22 using namespace CodeGen;
26 CodeGenModule &CGM; // Per-module state.
27 llvm::LLVMContext &VMContext;
29 /// Fields - The fields of the RTTI descriptor currently being built.
30 SmallVector<llvm::Constant *, 16> Fields;
32 /// GetAddrOfTypeName - Returns the mangled type name of the given type.
33 llvm::GlobalVariable *
34 GetAddrOfTypeName(QualType Ty, llvm::GlobalVariable::LinkageTypes Linkage);
36 /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI
37 /// descriptor of the given type.
38 llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
40 /// BuildVTablePointer - Build the vtable pointer for the given type.
41 void BuildVTablePointer(const Type *Ty);
43 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
44 /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
45 void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
47 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
48 /// classes with bases that do not satisfy the abi::__si_class_type_info
49 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
50 void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
52 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
53 /// for pointer types.
54 void BuildPointerTypeInfo(QualType PointeeTy);
56 /// BuildObjCObjectTypeInfo - Build the appropriate kind of
57 /// type_info for an object type.
58 void BuildObjCObjectTypeInfo(const ObjCObjectType *Ty);
60 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
61 /// struct, used for member pointer types.
62 void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
65 RTTIBuilder(CodeGenModule &CGM) : CGM(CGM),
66 VMContext(CGM.getModule().getContext()) { }
68 // Pointer type info flags.
70 /// PTI_Const - Type has const qualifier.
73 /// PTI_Volatile - Type has volatile qualifier.
76 /// PTI_Restrict - Type has restrict qualifier.
79 /// PTI_Incomplete - Type is incomplete.
82 /// PTI_ContainingClassIncomplete - Containing class is incomplete.
83 /// (in pointer to member).
84 PTI_ContainingClassIncomplete = 0x10
87 // VMI type info flags.
89 /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
90 VMI_NonDiamondRepeat = 0x1,
92 /// VMI_DiamondShaped - Class is diamond shaped.
93 VMI_DiamondShaped = 0x2
96 // Base class type info flags.
98 /// BCTI_Virtual - Base class is virtual.
101 /// BCTI_Public - Base class is public.
105 /// BuildTypeInfo - Build the RTTI type info struct for the given type.
107 /// \param Force - true to force the creation of this RTTI value
108 llvm::Constant *BuildTypeInfo(QualType Ty, bool Force = false);
112 llvm::GlobalVariable *
113 RTTIBuilder::GetAddrOfTypeName(QualType Ty,
114 llvm::GlobalVariable::LinkageTypes Linkage) {
115 SmallString<256> OutName;
116 llvm::raw_svector_ostream Out(OutName);
117 CGM.getCXXABI().getMangleContext().mangleCXXRTTIName(Ty, Out);
119 StringRef Name = OutName.str();
121 // We know that the mangled name of the type starts at index 4 of the
122 // mangled name of the typename, so we can just index into it in order to
123 // get the mangled name of the type.
124 llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext,
127 llvm::GlobalVariable *GV =
128 CGM.CreateOrReplaceCXXRuntimeVariable(Name, Init->getType(), Linkage);
130 GV->setInitializer(Init);
135 llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
136 // Mangle the RTTI name.
137 SmallString<256> OutName;
138 llvm::raw_svector_ostream Out(OutName);
139 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
141 StringRef Name = OutName.str();
143 // Look for an existing global.
144 llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
147 // Create a new global variable.
148 GV = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy,
150 llvm::GlobalValue::ExternalLinkage, 0, Name);
153 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
156 /// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
157 /// info for that type is defined in the standard library.
158 static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
159 // Itanium C++ ABI 2.9.2:
160 // Basic type information (e.g. for "int", "bool", etc.) will be kept in
161 // the run-time support library. Specifically, the run-time support
162 // library should contain type_info objects for the types X, X* and
163 // X const*, for every X in: void, std::nullptr_t, bool, wchar_t, char,
164 // unsigned char, signed char, short, unsigned short, int, unsigned int,
165 // long, unsigned long, long long, unsigned long long, float, double,
166 // long double, char16_t, char32_t, and the IEEE 754r decimal and
167 // half-precision floating point types.
168 switch (Ty->getKind()) {
169 case BuiltinType::Void:
170 case BuiltinType::NullPtr:
171 case BuiltinType::Bool:
172 case BuiltinType::WChar_S:
173 case BuiltinType::WChar_U:
174 case BuiltinType::Char_U:
175 case BuiltinType::Char_S:
176 case BuiltinType::UChar:
177 case BuiltinType::SChar:
178 case BuiltinType::Short:
179 case BuiltinType::UShort:
180 case BuiltinType::Int:
181 case BuiltinType::UInt:
182 case BuiltinType::Long:
183 case BuiltinType::ULong:
184 case BuiltinType::LongLong:
185 case BuiltinType::ULongLong:
186 case BuiltinType::Half:
187 case BuiltinType::Float:
188 case BuiltinType::Double:
189 case BuiltinType::LongDouble:
190 case BuiltinType::Char16:
191 case BuiltinType::Char32:
192 case BuiltinType::Int128:
193 case BuiltinType::UInt128:
194 case BuiltinType::OCLImage1d:
195 case BuiltinType::OCLImage1dArray:
196 case BuiltinType::OCLImage1dBuffer:
197 case BuiltinType::OCLImage2d:
198 case BuiltinType::OCLImage2dArray:
199 case BuiltinType::OCLImage3d:
200 case BuiltinType::OCLSampler:
201 case BuiltinType::OCLEvent:
204 case BuiltinType::Dependent:
205 #define BUILTIN_TYPE(Id, SingletonId)
206 #define PLACEHOLDER_TYPE(Id, SingletonId) \
207 case BuiltinType::Id:
208 #include "clang/AST/BuiltinTypes.def"
209 llvm_unreachable("asking for RRTI for a placeholder type!");
211 case BuiltinType::ObjCId:
212 case BuiltinType::ObjCClass:
213 case BuiltinType::ObjCSel:
214 llvm_unreachable("FIXME: Objective-C types are unsupported!");
217 llvm_unreachable("Invalid BuiltinType Kind!");
220 static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
221 QualType PointeeTy = PointerTy->getPointeeType();
222 const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
226 // Check the qualifiers.
227 Qualifiers Quals = PointeeTy.getQualifiers();
233 return TypeInfoIsInStandardLibrary(BuiltinTy);
236 /// IsStandardLibraryRTTIDescriptor - Returns whether the type
237 /// information for the given type exists in the standard library.
238 static bool IsStandardLibraryRTTIDescriptor(QualType Ty) {
239 // Type info for builtin types is defined in the standard library.
240 if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
241 return TypeInfoIsInStandardLibrary(BuiltinTy);
243 // Type info for some pointer types to builtin types is defined in the
245 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
246 return TypeInfoIsInStandardLibrary(PointerTy);
251 /// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
252 /// the given type exists somewhere else, and that we should not emit the type
253 /// information in this translation unit. Assumes that it is not a
254 /// standard-library type.
255 static bool ShouldUseExternalRTTIDescriptor(CodeGenModule &CGM,
257 ASTContext &Context = CGM.getContext();
259 // If RTTI is disabled, assume it might be disabled in the
260 // translation unit that defines any potential key function, too.
261 if (!Context.getLangOpts().RTTI) return false;
263 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
264 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
265 if (!RD->hasDefinition())
268 if (!RD->isDynamicClass())
271 // FIXME: this may need to be reconsidered if the key function
273 return CGM.getVTables().isVTableExternal(RD);
279 /// IsIncompleteClassType - Returns whether the given record type is incomplete.
280 static bool IsIncompleteClassType(const RecordType *RecordTy) {
281 return !RecordTy->getDecl()->isCompleteDefinition();
284 /// ContainsIncompleteClassType - Returns whether the given type contains an
285 /// incomplete class type. This is true if
287 /// * The given type is an incomplete class type.
288 /// * The given type is a pointer type whose pointee type contains an
289 /// incomplete class type.
290 /// * The given type is a member pointer type whose class is an incomplete
292 /// * The given type is a member pointer type whoise pointee type contains an
293 /// incomplete class type.
294 /// is an indirect or direct pointer to an incomplete class type.
295 static bool ContainsIncompleteClassType(QualType Ty) {
296 if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
297 if (IsIncompleteClassType(RecordTy))
301 if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
302 return ContainsIncompleteClassType(PointerTy->getPointeeType());
304 if (const MemberPointerType *MemberPointerTy =
305 dyn_cast<MemberPointerType>(Ty)) {
306 // Check if the class type is incomplete.
307 const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
308 if (IsIncompleteClassType(ClassType))
311 return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
317 /// getTypeInfoLinkage - Return the linkage that the type info and type info
318 /// name constants should have for the given type.
319 static llvm::GlobalVariable::LinkageTypes
320 getTypeInfoLinkage(CodeGenModule &CGM, QualType Ty) {
321 // Itanium C++ ABI 2.9.5p7:
322 // In addition, it and all of the intermediate abi::__pointer_type_info
323 // structs in the chain down to the abi::__class_type_info for the
324 // incomplete class type must be prevented from resolving to the
325 // corresponding type_info structs for the complete class type, possibly
326 // by making them local static objects. Finally, a dummy class RTTI is
327 // generated for the incomplete type that will not resolve to the final
328 // complete class RTTI (because the latter need not exist), possibly by
329 // making it a local static object.
330 if (ContainsIncompleteClassType(Ty))
331 return llvm::GlobalValue::InternalLinkage;
333 switch (Ty->getLinkage()) {
335 case VisibleNoLinkage:
336 case InternalLinkage:
337 case UniqueExternalLinkage:
338 return llvm::GlobalValue::InternalLinkage;
340 case ExternalLinkage:
341 if (!CGM.getLangOpts().RTTI) {
342 // RTTI is not enabled, which means that this type info struct is going
343 // to be used for exception handling. Give it linkonce_odr linkage.
344 return llvm::GlobalValue::LinkOnceODRLinkage;
347 if (const RecordType *Record = dyn_cast<RecordType>(Ty)) {
348 const CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl());
349 if (RD->hasAttr<WeakAttr>())
350 return llvm::GlobalValue::WeakODRLinkage;
351 if (RD->isDynamicClass())
352 return CGM.getVTableLinkage(RD);
355 return llvm::GlobalValue::LinkOnceODRLinkage;
358 llvm_unreachable("Invalid linkage!");
361 // CanUseSingleInheritance - Return whether the given record decl has a "single,
362 // public, non-virtual base at offset zero (i.e. the derived class is dynamic
363 // iff the base is)", according to Itanium C++ ABI, 2.95p6b.
364 static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
365 // Check the number of bases.
366 if (RD->getNumBases() != 1)
370 CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
372 // Check that the base is not virtual.
373 if (Base->isVirtual())
376 // Check that the base is public.
377 if (Base->getAccessSpecifier() != AS_public)
380 // Check that the class is dynamic iff the base is.
381 const CXXRecordDecl *BaseDecl =
382 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
383 if (!BaseDecl->isEmpty() &&
384 BaseDecl->isDynamicClass() != RD->isDynamicClass())
390 void RTTIBuilder::BuildVTablePointer(const Type *Ty) {
391 // abi::__class_type_info.
392 static const char * const ClassTypeInfo =
393 "_ZTVN10__cxxabiv117__class_type_infoE";
394 // abi::__si_class_type_info.
395 static const char * const SIClassTypeInfo =
396 "_ZTVN10__cxxabiv120__si_class_type_infoE";
397 // abi::__vmi_class_type_info.
398 static const char * const VMIClassTypeInfo =
399 "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
401 const char *VTableName = 0;
403 switch (Ty->getTypeClass()) {
404 #define TYPE(Class, Base)
405 #define ABSTRACT_TYPE(Class, Base)
406 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
407 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
408 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
409 #include "clang/AST/TypeNodes.def"
410 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
412 case Type::LValueReference:
413 case Type::RValueReference:
414 llvm_unreachable("References shouldn't get here");
417 llvm_unreachable("Undeduced auto type shouldn't get here");
420 // GCC treats vector and complex types as fundamental types.
422 case Type::ExtVector:
425 // FIXME: GCC treats block pointers as fundamental types?!
426 case Type::BlockPointer:
427 // abi::__fundamental_type_info.
428 VTableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
431 case Type::ConstantArray:
432 case Type::IncompleteArray:
433 case Type::VariableArray:
434 // abi::__array_type_info.
435 VTableName = "_ZTVN10__cxxabiv117__array_type_infoE";
438 case Type::FunctionNoProto:
439 case Type::FunctionProto:
440 // abi::__function_type_info.
441 VTableName = "_ZTVN10__cxxabiv120__function_type_infoE";
445 // abi::__enum_type_info.
446 VTableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
450 const CXXRecordDecl *RD =
451 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
453 if (!RD->hasDefinition() || !RD->getNumBases()) {
454 VTableName = ClassTypeInfo;
455 } else if (CanUseSingleInheritance(RD)) {
456 VTableName = SIClassTypeInfo;
458 VTableName = VMIClassTypeInfo;
464 case Type::ObjCObject:
465 // Ignore protocol qualifiers.
466 Ty = cast<ObjCObjectType>(Ty)->getBaseType().getTypePtr();
468 // Handle id and Class.
469 if (isa<BuiltinType>(Ty)) {
470 VTableName = ClassTypeInfo;
474 assert(isa<ObjCInterfaceType>(Ty));
477 case Type::ObjCInterface:
478 if (cast<ObjCInterfaceType>(Ty)->getDecl()->getSuperClass()) {
479 VTableName = SIClassTypeInfo;
481 VTableName = ClassTypeInfo;
485 case Type::ObjCObjectPointer:
487 // abi::__pointer_type_info.
488 VTableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
491 case Type::MemberPointer:
492 // abi::__pointer_to_member_type_info.
493 VTableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
497 llvm::Constant *VTable =
498 CGM.getModule().getOrInsertGlobal(VTableName, CGM.Int8PtrTy);
500 llvm::Type *PtrDiffTy =
501 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
503 // The vtable address point is 2.
504 llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
505 VTable = llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, Two);
506 VTable = llvm::ConstantExpr::getBitCast(VTable, CGM.Int8PtrTy);
508 Fields.push_back(VTable);
511 llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty, bool Force) {
512 // We want to operate on the canonical type.
513 Ty = CGM.getContext().getCanonicalType(Ty);
515 // Check if we've already emitted an RTTI descriptor for this type.
516 SmallString<256> OutName;
517 llvm::raw_svector_ostream Out(OutName);
518 CGM.getCXXABI().getMangleContext().mangleCXXRTTI(Ty, Out);
520 StringRef Name = OutName.str();
522 llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
523 if (OldGV && !OldGV->isDeclaration()) {
524 assert(!OldGV->hasAvailableExternallyLinkage() &&
525 "available_externally typeinfos not yet implemented");
527 return llvm::ConstantExpr::getBitCast(OldGV, CGM.Int8PtrTy);
530 // Check if there is already an external RTTI descriptor for this type.
531 bool IsStdLib = IsStandardLibraryRTTIDescriptor(Ty);
532 if (!Force && (IsStdLib || ShouldUseExternalRTTIDescriptor(CGM, Ty)))
533 return GetAddrOfExternalRTTIDescriptor(Ty);
535 // Emit the standard library with external linkage.
536 llvm::GlobalVariable::LinkageTypes Linkage;
538 Linkage = llvm::GlobalValue::ExternalLinkage;
540 Linkage = getTypeInfoLinkage(CGM, Ty);
542 // Add the vtable pointer.
543 BuildVTablePointer(cast<Type>(Ty));
546 llvm::GlobalVariable *TypeName = GetAddrOfTypeName(Ty, Linkage);
548 Fields.push_back(llvm::ConstantExpr::getBitCast(TypeName, CGM.Int8PtrTy));
550 switch (Ty->getTypeClass()) {
551 #define TYPE(Class, Base)
552 #define ABSTRACT_TYPE(Class, Base)
553 #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
554 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
555 #define DEPENDENT_TYPE(Class, Base) case Type::Class:
556 #include "clang/AST/TypeNodes.def"
557 llvm_unreachable("Non-canonical and dependent types shouldn't get here");
559 // GCC treats vector types as fundamental types.
562 case Type::ExtVector:
564 case Type::BlockPointer:
565 // Itanium C++ ABI 2.9.5p4:
566 // abi::__fundamental_type_info adds no data members to std::type_info.
569 case Type::LValueReference:
570 case Type::RValueReference:
571 llvm_unreachable("References shouldn't get here");
574 llvm_unreachable("Undeduced auto type shouldn't get here");
576 case Type::ConstantArray:
577 case Type::IncompleteArray:
578 case Type::VariableArray:
579 // Itanium C++ ABI 2.9.5p5:
580 // abi::__array_type_info adds no data members to std::type_info.
583 case Type::FunctionNoProto:
584 case Type::FunctionProto:
585 // Itanium C++ ABI 2.9.5p5:
586 // abi::__function_type_info adds no data members to std::type_info.
590 // Itanium C++ ABI 2.9.5p5:
591 // abi::__enum_type_info adds no data members to std::type_info.
595 const CXXRecordDecl *RD =
596 cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
597 if (!RD->hasDefinition() || !RD->getNumBases()) {
598 // We don't need to emit any fields.
602 if (CanUseSingleInheritance(RD))
603 BuildSIClassTypeInfo(RD);
605 BuildVMIClassTypeInfo(RD);
610 case Type::ObjCObject:
611 case Type::ObjCInterface:
612 BuildObjCObjectTypeInfo(cast<ObjCObjectType>(Ty));
615 case Type::ObjCObjectPointer:
616 BuildPointerTypeInfo(cast<ObjCObjectPointerType>(Ty)->getPointeeType());
620 BuildPointerTypeInfo(cast<PointerType>(Ty)->getPointeeType());
623 case Type::MemberPointer:
624 BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
628 // No fields, at least for the moment.
632 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Fields);
634 llvm::GlobalVariable *GV =
635 new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
636 /*Constant=*/true, Linkage, Init, Name);
638 // If there's already an old global variable, replace it with the new one.
641 llvm::Constant *NewPtr =
642 llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
643 OldGV->replaceAllUsesWith(NewPtr);
644 OldGV->eraseFromParent();
647 // GCC only relies on the uniqueness of the type names, not the
648 // type_infos themselves, so we can emit these as hidden symbols.
649 // But don't do this if we're worried about strict visibility
651 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) {
652 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
654 CGM.setTypeVisibility(GV, RD, CodeGenModule::TVK_ForRTTI);
655 CGM.setTypeVisibility(TypeName, RD, CodeGenModule::TVK_ForRTTIName);
657 Visibility TypeInfoVisibility = DefaultVisibility;
658 if (CGM.getCodeGenOpts().HiddenWeakVTables &&
659 Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
660 TypeInfoVisibility = HiddenVisibility;
662 // The type name should have the same visibility as the type itself.
663 Visibility ExplicitVisibility = Ty->getVisibility();
664 TypeName->setVisibility(CodeGenModule::
665 GetLLVMVisibility(ExplicitVisibility));
667 TypeInfoVisibility = minVisibility(TypeInfoVisibility, Ty->getVisibility());
668 GV->setVisibility(CodeGenModule::GetLLVMVisibility(TypeInfoVisibility));
671 GV->setUnnamedAddr(true);
673 return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy);
676 /// ComputeQualifierFlags - Compute the pointer type info flags from the
678 static unsigned ComputeQualifierFlags(Qualifiers Quals) {
681 if (Quals.hasConst())
682 Flags |= RTTIBuilder::PTI_Const;
683 if (Quals.hasVolatile())
684 Flags |= RTTIBuilder::PTI_Volatile;
685 if (Quals.hasRestrict())
686 Flags |= RTTIBuilder::PTI_Restrict;
691 /// BuildObjCObjectTypeInfo - Build the appropriate kind of type_info
692 /// for the given Objective-C object type.
693 void RTTIBuilder::BuildObjCObjectTypeInfo(const ObjCObjectType *OT) {
695 const Type *T = OT->getBaseType().getTypePtr();
696 assert(isa<BuiltinType>(T) || isa<ObjCInterfaceType>(T));
698 // The builtin types are abi::__class_type_infos and don't require
700 if (isa<BuiltinType>(T)) return;
702 ObjCInterfaceDecl *Class = cast<ObjCInterfaceType>(T)->getDecl();
703 ObjCInterfaceDecl *Super = Class->getSuperClass();
705 // Root classes are also __class_type_info.
708 QualType SuperTy = CGM.getContext().getObjCInterfaceType(Super);
710 // Everything else is single inheritance.
711 llvm::Constant *BaseTypeInfo = RTTIBuilder(CGM).BuildTypeInfo(SuperTy);
712 Fields.push_back(BaseTypeInfo);
715 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
716 /// inheritance, according to the Itanium C++ ABI, 2.95p6b.
717 void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
718 // Itanium C++ ABI 2.9.5p6b:
719 // It adds to abi::__class_type_info a single member pointing to the
720 // type_info structure for the base type,
721 llvm::Constant *BaseTypeInfo =
722 RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
723 Fields.push_back(BaseTypeInfo);
727 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
728 /// a class hierarchy.
730 llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
731 llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
735 /// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
736 /// abi::__vmi_class_type_info.
738 static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base,
743 const CXXRecordDecl *BaseDecl =
744 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
746 if (Base->isVirtual()) {
747 // Mark the virtual base as seen.
748 if (!Bases.VirtualBases.insert(BaseDecl)) {
749 // If this virtual base has been seen before, then the class is diamond
751 Flags |= RTTIBuilder::VMI_DiamondShaped;
753 if (Bases.NonVirtualBases.count(BaseDecl))
754 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
757 // Mark the non-virtual base as seen.
758 if (!Bases.NonVirtualBases.insert(BaseDecl)) {
759 // If this non-virtual base has been seen before, then the class has non-
760 // diamond shaped repeated inheritance.
761 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
763 if (Bases.VirtualBases.count(BaseDecl))
764 Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
769 for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
770 E = BaseDecl->bases_end(); I != E; ++I)
771 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
776 static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
781 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
782 E = RD->bases_end(); I != E; ++I)
783 Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
788 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
789 /// classes with bases that do not satisfy the abi::__si_class_type_info
790 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
791 void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
792 llvm::Type *UnsignedIntLTy =
793 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
795 // Itanium C++ ABI 2.9.5p6c:
796 // __flags is a word with flags describing details about the class
797 // structure, which may be referenced by using the __flags_masks
798 // enumeration. These flags refer to both direct and indirect bases.
799 unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
800 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
802 // Itanium C++ ABI 2.9.5p6c:
803 // __base_count is a word with the number of direct proper base class
804 // descriptions that follow.
805 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
807 if (!RD->getNumBases())
810 llvm::Type *LongLTy =
811 CGM.getTypes().ConvertType(CGM.getContext().LongTy);
813 // Now add the base class descriptions.
815 // Itanium C++ ABI 2.9.5p6c:
816 // __base_info[] is an array of base class descriptions -- one for every
817 // direct proper base. Each description is of the type:
819 // struct abi::__base_class_type_info {
821 // const __class_type_info *__base_type;
822 // long __offset_flags;
824 // enum __offset_flags_masks {
825 // __virtual_mask = 0x1,
826 // __public_mask = 0x2,
827 // __offset_shift = 8
830 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
831 E = RD->bases_end(); I != E; ++I) {
832 const CXXBaseSpecifier *Base = I;
834 // The __base_type member points to the RTTI for the base type.
835 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
837 const CXXRecordDecl *BaseDecl =
838 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
840 int64_t OffsetFlags = 0;
842 // All but the lower 8 bits of __offset_flags are a signed offset.
843 // For a non-virtual base, this is the offset in the object of the base
844 // subobject. For a virtual base, this is the offset in the virtual table of
845 // the virtual base offset for the virtual base referenced (negative).
847 if (Base->isVirtual())
849 CGM.getItaniumVTableContext().getVirtualBaseOffsetOffset(RD, BaseDecl);
851 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
852 Offset = Layout.getBaseClassOffset(BaseDecl);
855 OffsetFlags = uint64_t(Offset.getQuantity()) << 8;
857 // The low-order byte of __offset_flags contains flags, as given by the
858 // masks from the enumeration __offset_flags_masks.
859 if (Base->isVirtual())
860 OffsetFlags |= BCTI_Virtual;
861 if (Base->getAccessSpecifier() == AS_public)
862 OffsetFlags |= BCTI_Public;
864 Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
868 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
869 /// used for pointer types.
870 void RTTIBuilder::BuildPointerTypeInfo(QualType PointeeTy) {
872 QualType UnqualifiedPointeeTy =
873 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
875 // Itanium C++ ABI 2.9.5p7:
876 // __flags is a flag word describing the cv-qualification and other
877 // attributes of the type pointed to
878 unsigned Flags = ComputeQualifierFlags(Quals);
880 // Itanium C++ ABI 2.9.5p7:
881 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
882 // incomplete class type, the incomplete target type flag is set.
883 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
884 Flags |= PTI_Incomplete;
886 llvm::Type *UnsignedIntLTy =
887 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
888 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
890 // Itanium C++ ABI 2.9.5p7:
891 // __pointee is a pointer to the std::type_info derivation for the
892 // unqualified type being pointed to.
893 llvm::Constant *PointeeTypeInfo =
894 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
895 Fields.push_back(PointeeTypeInfo);
898 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info
899 /// struct, used for member pointer types.
900 void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
901 QualType PointeeTy = Ty->getPointeeType();
904 QualType UnqualifiedPointeeTy =
905 CGM.getContext().getUnqualifiedArrayType(PointeeTy, Quals);
907 // Itanium C++ ABI 2.9.5p7:
908 // __flags is a flag word describing the cv-qualification and other
909 // attributes of the type pointed to.
910 unsigned Flags = ComputeQualifierFlags(Quals);
912 const RecordType *ClassType = cast<RecordType>(Ty->getClass());
914 // Itanium C++ ABI 2.9.5p7:
915 // When the abi::__pbase_type_info is for a direct or indirect pointer to an
916 // incomplete class type, the incomplete target type flag is set.
917 if (ContainsIncompleteClassType(UnqualifiedPointeeTy))
918 Flags |= PTI_Incomplete;
920 if (IsIncompleteClassType(ClassType))
921 Flags |= PTI_ContainingClassIncomplete;
923 llvm::Type *UnsignedIntLTy =
924 CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
925 Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
927 // Itanium C++ ABI 2.9.5p7:
928 // __pointee is a pointer to the std::type_info derivation for the
929 // unqualified type being pointed to.
930 llvm::Constant *PointeeTypeInfo =
931 RTTIBuilder(CGM).BuildTypeInfo(UnqualifiedPointeeTy);
932 Fields.push_back(PointeeTypeInfo);
934 // Itanium C++ ABI 2.9.5p9:
935 // __context is a pointer to an abi::__class_type_info corresponding to the
936 // class type containing the member pointed to
937 // (e.g., the "A" in "int A::*").
938 Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
941 llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty,
943 // Return a bogus pointer if RTTI is disabled, unless it's for EH.
944 // FIXME: should we even be calling this method if RTTI is disabled
945 // and it's not for EH?
946 if (!ForEH && !getLangOpts().RTTI)
947 return llvm::Constant::getNullValue(Int8PtrTy);
949 if (ForEH && Ty->isObjCObjectPointerType() &&
950 LangOpts.ObjCRuntime.isGNUFamily())
951 return ObjCRuntime->GetEHType(Ty);
953 return RTTIBuilder(*this).BuildTypeInfo(Ty);
956 void CodeGenModule::EmitFundamentalRTTIDescriptor(QualType Type) {
957 QualType PointerType = Context.getPointerType(Type);
958 QualType PointerTypeConst = Context.getPointerType(Type.withConst());
959 RTTIBuilder(*this).BuildTypeInfo(Type, true);
960 RTTIBuilder(*this).BuildTypeInfo(PointerType, true);
961 RTTIBuilder(*this).BuildTypeInfo(PointerTypeConst, true);
964 void CodeGenModule::EmitFundamentalRTTIDescriptors() {
965 QualType FundamentalTypes[] = { Context.VoidTy, Context.NullPtrTy,
966 Context.BoolTy, Context.WCharTy,
967 Context.CharTy, Context.UnsignedCharTy,
968 Context.SignedCharTy, Context.ShortTy,
969 Context.UnsignedShortTy, Context.IntTy,
970 Context.UnsignedIntTy, Context.LongTy,
971 Context.UnsignedLongTy, Context.LongLongTy,
972 Context.UnsignedLongLongTy, Context.FloatTy,
973 Context.DoubleTy, Context.LongDoubleTy,
974 Context.Char16Ty, Context.Char32Ty };
975 for (unsigned i = 0; i < llvm::array_lengthof(FundamentalTypes); ++i)
976 EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);