clang API Documentation

CGRTTI.cpp

Go to the documentation of this file.
00001 //===--- CGCXXRTTI.cpp - Emit LLVM Code for C++ RTTI descriptors ----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This contains code dealing with C++ code generation of RTTI descriptors.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/AST/Type.h"
00015 #include "clang/AST/RecordLayout.h"
00016 #include "CodeGenModule.h"
00017 using namespace clang;
00018 using namespace CodeGen;
00019 
00020 namespace {
00021 class RTTIBuilder {
00022   CodeGenModule &CGM;  // Per-module state.
00023   llvm::LLVMContext &VMContext;
00024   
00025   const llvm::Type *Int8PtrTy;
00026   
00027   /// Fields - The fields of the RTTI descriptor currently being built.
00028   llvm::SmallVector<llvm::Constant *, 16> Fields;
00029 
00030   /// GetAddrOfExternalRTTIDescriptor - Returns the constant for the RTTI 
00031   /// descriptor of the given type.
00032   llvm::Constant *GetAddrOfExternalRTTIDescriptor(QualType Ty);
00033   
00034   /// BuildVtablePointer - Build the vtable pointer for the given type.
00035   void BuildVtablePointer(const Type *Ty);
00036   
00037   /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
00038   /// inheritance, according to the Itanium C++ ABI, 2.9.5p6b.
00039   void BuildSIClassTypeInfo(const CXXRecordDecl *RD);
00040   
00041   /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
00042   /// classes with bases that do not satisfy the abi::__si_class_type_info 
00043   /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
00044   void BuildVMIClassTypeInfo(const CXXRecordDecl *RD);
00045   
00046   /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct, used
00047   /// for pointer types.
00048   void BuildPointerTypeInfo(const PointerType *Ty);
00049   
00050   /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info 
00051   /// struct, used for member pointer types.
00052   void BuildPointerToMemberTypeInfo(const MemberPointerType *Ty);
00053   
00054 public:
00055   RTTIBuilder(CodeGenModule &cgm)
00056     : CGM(cgm), VMContext(cgm.getModule().getContext()),
00057       Int8PtrTy(llvm::Type::getInt8PtrTy(VMContext)) { }
00058 
00059   llvm::Constant *BuildName(QualType Ty, bool Hidden, 
00060                             llvm::GlobalVariable::LinkageTypes Linkage) {
00061     llvm::SmallString<256> OutName;
00062     CGM.getMangleContext().mangleCXXRTTIName(Ty, OutName);
00063     llvm::StringRef Name = OutName.str();
00064 
00065     llvm::GlobalVariable *OGV = CGM.getModule().getNamedGlobal(Name);
00066     if (OGV && !OGV->isDeclaration())
00067       return llvm::ConstantExpr::getBitCast(OGV, Int8PtrTy);
00068 
00069     llvm::Constant *C = llvm::ConstantArray::get(VMContext, Name.substr(4));
00070 
00071     llvm::GlobalVariable *GV = 
00072       new llvm::GlobalVariable(CGM.getModule(), C->getType(), true, Linkage,
00073                                C, Name);
00074     if (OGV) {
00075       GV->takeName(OGV);
00076       llvm::Constant *NewPtr = llvm::ConstantExpr::getBitCast(GV,
00077                                                               OGV->getType());
00078       OGV->replaceAllUsesWith(NewPtr);
00079       OGV->eraseFromParent();
00080     }
00081     if (Hidden)
00082       GV->setVisibility(llvm::GlobalVariable::HiddenVisibility);
00083     return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
00084   }
00085 
00086   // FIXME: unify with DecideExtern
00087   bool DecideHidden(QualType Ty) {
00088     // For this type, see if all components are never hidden.
00089     if (const MemberPointerType *MPT = Ty->getAs<MemberPointerType>())
00090       return (DecideHidden(MPT->getPointeeType())
00091               && DecideHidden(QualType(MPT->getClass(), 0)));
00092     if (const PointerType *PT = Ty->getAs<PointerType>())
00093       return DecideHidden(PT->getPointeeType());
00094     if (const FunctionType *FT = Ty->getAs<FunctionType>()) {
00095       if (DecideHidden(FT->getResultType()) == false)
00096         return false;
00097       if (const FunctionProtoType *FPT = Ty->getAs<FunctionProtoType>()) {
00098         for (unsigned i = 0; i <FPT->getNumArgs(); ++i)
00099           if (DecideHidden(FPT->getArgType(i)) == false)
00100             return false;
00101         for (unsigned i = 0; i <FPT->getNumExceptions(); ++i)
00102           if (DecideHidden(FPT->getExceptionType(i)) == false)
00103             return false;
00104         return true;
00105       }
00106     }
00107     if (const RecordType *RT = Ty->getAs<RecordType>())
00108       if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
00109         return CGM.getDeclVisibilityMode(RD) == LangOptions::Hidden;
00110     return false;
00111   }
00112   
00113   // Pointer type info flags.
00114   enum {
00115     /// PTI_Const - Type has const qualifier.
00116     PTI_Const = 0x1,
00117     
00118     /// PTI_Volatile - Type has volatile qualifier.
00119     PTI_Volatile = 0x2,
00120     
00121     /// PTI_Restrict - Type has restrict qualifier.
00122     PTI_Restrict = 0x4,
00123     
00124     /// PTI_Incomplete - Type is incomplete.
00125     PTI_Incomplete = 0x8,
00126     
00127     /// PTI_ContainingClassIncomplete - Containing class is incomplete.
00128     /// (in pointer to member).
00129     PTI_ContainingClassIncomplete = 0x10
00130   };
00131   
00132   // VMI type info flags.
00133   enum {
00134     /// VMI_NonDiamondRepeat - Class has non-diamond repeated inheritance.
00135     VMI_NonDiamondRepeat = 0x1,
00136     
00137     /// VMI_DiamondShaped - Class is diamond shaped.
00138     VMI_DiamondShaped = 0x2
00139   };
00140   
00141   // Base class type info flags.
00142   enum {
00143     /// BCTI_Virtual - Base class is virtual.
00144     BCTI_Virtual = 0x1,
00145     
00146     /// BCTI_Public - Base class is public.
00147     BCTI_Public = 0x2
00148   };
00149   
00150   /// BuildTypeInfo - Build the RTTI type info struct for the given type.
00151   llvm::Constant *BuildTypeInfo(QualType Ty);
00152 };
00153 }
00154 
00155 llvm::Constant *RTTIBuilder::GetAddrOfExternalRTTIDescriptor(QualType Ty) {
00156   // Mangle the RTTI name.
00157   llvm::SmallString<256> OutName;
00158   CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
00159   llvm::StringRef Name = OutName.str();
00160 
00161   // Look for an existing global.
00162   llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name);
00163   
00164   if (!GV) {
00165     // Create a new global variable.
00166     GV = new llvm::GlobalVariable(CGM.getModule(), Int8PtrTy, /*Constant=*/true,
00167                                   llvm::GlobalValue::ExternalLinkage, 0, Name);
00168   }
00169   
00170   return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
00171 }
00172 
00173 /// TypeInfoIsInStandardLibrary - Given a builtin type, returns whether the type
00174 /// info for that type is defined in the standard library.
00175 static bool TypeInfoIsInStandardLibrary(const BuiltinType *Ty) {
00176   // Itanium C++ ABI 2.9.2:
00177   //   Basic type information (e.g. for "int", "bool", etc.) will be kept in
00178   //   the run-time support library. Specifically, the run-time support
00179   //   library should contain type_info objects for the types X, X* and 
00180   //   X const*, for every X in: void, bool, wchar_t, char, unsigned char, 
00181   //   signed char, short, unsigned short, int, unsigned int, long, 
00182   //   unsigned long, long long, unsigned long long, float, double, long double, 
00183   //   char16_t, char32_t, and the IEEE 754r decimal and half-precision 
00184   //   floating point types.
00185   switch (Ty->getKind()) {
00186     case BuiltinType::Void:
00187     case BuiltinType::Bool:
00188     case BuiltinType::WChar:
00189     case BuiltinType::Char_U:
00190     case BuiltinType::Char_S:
00191     case BuiltinType::UChar:
00192     case BuiltinType::SChar:
00193     case BuiltinType::Short:
00194     case BuiltinType::UShort:
00195     case BuiltinType::Int:
00196     case BuiltinType::UInt:
00197     case BuiltinType::Long:
00198     case BuiltinType::ULong:
00199     case BuiltinType::LongLong:
00200     case BuiltinType::ULongLong:
00201     case BuiltinType::Float:
00202     case BuiltinType::Double:
00203     case BuiltinType::LongDouble:
00204     case BuiltinType::Char16:
00205     case BuiltinType::Char32:
00206     case BuiltinType::Int128:
00207     case BuiltinType::UInt128:
00208       return true;
00209       
00210     case BuiltinType::Overload:
00211     case BuiltinType::Dependent:
00212     case BuiltinType::UndeducedAuto:
00213       assert(false && "Should not see this type here!");
00214       
00215     case BuiltinType::NullPtr:
00216       assert(false && "FIXME: nullptr_t is not handled!");
00217 
00218     case BuiltinType::ObjCId:
00219     case BuiltinType::ObjCClass:
00220     case BuiltinType::ObjCSel:
00221       assert(false && "FIXME: Objective-C types are unsupported!");
00222   }
00223   
00224   // Silent gcc.
00225   return false;
00226 }
00227 
00228 static bool TypeInfoIsInStandardLibrary(const PointerType *PointerTy) {
00229   QualType PointeeTy = PointerTy->getPointeeType();
00230   const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(PointeeTy);
00231   if (!BuiltinTy)
00232     return false;
00233     
00234   // Check the qualifiers.
00235   Qualifiers Quals = PointeeTy.getQualifiers();
00236   Quals.removeConst();
00237     
00238   if (!Quals.empty())
00239     return false;
00240     
00241   return TypeInfoIsInStandardLibrary(BuiltinTy);
00242 }
00243 
00244 /// ShouldUseExternalRTTIDescriptor - Returns whether the type information for
00245 /// the given type exists somewhere else, and that we should not emit the typ
00246 /// information in this translation unit.
00247 bool ShouldUseExternalRTTIDescriptor(QualType Ty) {
00248   // Type info for builtin types is defined in the standard library.
00249   if (const BuiltinType *BuiltinTy = dyn_cast<BuiltinType>(Ty))
00250     return TypeInfoIsInStandardLibrary(BuiltinTy);
00251   
00252   // Type info for some pointer types to builtin types is defined in the
00253   // standard library.
00254   if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
00255     return TypeInfoIsInStandardLibrary(PointerTy);
00256 
00257   if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
00258     const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
00259     if (!RD->hasDefinition())
00260       return false;
00261 
00262     if (!RD->isDynamicClass())
00263       return false;
00264 
00265     // Get the key function.
00266     const CXXMethodDecl *KeyFunction = RD->getASTContext().getKeyFunction(RD);
00267     if (KeyFunction && !KeyFunction->getBody()) {
00268       // The class has a key function, but it is not defined in this translation
00269       // unit, so we should use the external descriptor for it.
00270       return true;
00271     }
00272   }
00273   
00274   return false;
00275 }
00276 
00277 /// IsIncompleteClassType - Returns whether the given record type is incomplete.
00278 static bool IsIncompleteClassType(const RecordType *RecordTy) {
00279   return !RecordTy->getDecl()->isDefinition();
00280 }  
00281 
00282 /// ContainsIncompleteClassType - Returns whether the given type contains an
00283 /// incomplete class type. This is true if
00284 ///
00285 ///   * The given type is an incomplete class type.
00286 ///   * The given type is a pointer type whose pointee type contains an 
00287 ///     incomplete class type.
00288 ///   * The given type is a member pointer type whose class is an incomplete
00289 ///     class type.
00290 ///   * The given type is a member pointer type whoise pointee type contains an
00291 ///     incomplete class type.
00292 /// is an indirect or direct pointer to an incomplete class type.
00293 static bool ContainsIncompleteClassType(QualType Ty) {
00294   if (const RecordType *RecordTy = dyn_cast<RecordType>(Ty)) {
00295     if (IsIncompleteClassType(RecordTy))
00296       return true;
00297   }
00298   
00299   if (const PointerType *PointerTy = dyn_cast<PointerType>(Ty))
00300     return ContainsIncompleteClassType(PointerTy->getPointeeType());
00301   
00302   if (const MemberPointerType *MemberPointerTy = 
00303       dyn_cast<MemberPointerType>(Ty)) {
00304     // Check if the class type is incomplete.
00305     const RecordType *ClassType = cast<RecordType>(MemberPointerTy->getClass());
00306     if (IsIncompleteClassType(ClassType))
00307       return true;
00308     
00309     return ContainsIncompleteClassType(MemberPointerTy->getPointeeType());
00310   }
00311   
00312   return false;
00313 }
00314 
00315 /// getTypeInfoLinkage - Return the linkage that the type info and type info
00316 /// name constants should have for the given type.
00317 static llvm::GlobalVariable::LinkageTypes getTypeInfoLinkage(QualType Ty) {
00318   // Itanium C++ ABI 2.9.5p7:
00319   //   In addition, it and all of the intermediate abi::__pointer_type_info 
00320   //   structs in the chain down to the abi::__class_type_info for the
00321   //   incomplete class type must be prevented from resolving to the 
00322   //   corresponding type_info structs for the complete class type, possibly
00323   //   by making them local static objects. Finally, a dummy class RTTI is
00324   //   generated for the incomplete type that will not resolve to the final 
00325   //   complete class RTTI (because the latter need not exist), possibly by 
00326   //   making it a local static object.
00327   if (ContainsIncompleteClassType(Ty))
00328     return llvm::GlobalValue::InternalLinkage;
00329   
00330   switch (Ty->getTypeClass()) {
00331   default:   
00332     // FIXME: We need to add code to handle all types.
00333     assert(false && "Unhandled type!");
00334     break;
00335 
00336   case Type::Pointer: {
00337     const PointerType *PointerTy = cast<PointerType>(Ty);
00338  
00339     // If the pointee type has internal linkage, then the pointer type needs to
00340     // have it as well.
00341     if (getTypeInfoLinkage(PointerTy->getPointeeType()) == 
00342         llvm::GlobalVariable::InternalLinkage)
00343       return llvm::GlobalVariable::InternalLinkage;
00344     
00345     return llvm::GlobalVariable::WeakODRLinkage;
00346   }
00347 
00348   case Type::Enum: {
00349     const EnumType *EnumTy = cast<EnumType>(Ty);
00350     const EnumDecl *ED = EnumTy->getDecl();
00351     
00352     // If we're in an anonymous namespace, then we always want internal linkage.
00353     if (ED->isInAnonymousNamespace() || !ED->hasLinkage())
00354       return llvm::GlobalVariable::InternalLinkage;
00355     
00356     return llvm::GlobalValue::WeakODRLinkage;
00357   }
00358 
00359   case Type::Record: {
00360     const RecordType *RecordTy = cast<RecordType>(Ty);
00361     const CXXRecordDecl *RD = cast<CXXRecordDecl>(RecordTy->getDecl());
00362 
00363     // If we're in an anonymous namespace, then we always want internal linkage.
00364     if (RD->isInAnonymousNamespace() || !RD->hasLinkage())
00365       return llvm::GlobalVariable::InternalLinkage;
00366 
00367     // If this class does not have a vtable, we want weak linkage.
00368     if (!RD->isDynamicClass())
00369       return llvm::GlobalValue::WeakODRLinkage;
00370     
00371     return CodeGenModule::getVtableLinkage(RD);
00372   }
00373 
00374   case Type::Vector:
00375   case Type::ExtVector:
00376   case Type::Builtin:
00377     return llvm::GlobalValue::WeakODRLinkage;
00378 
00379   case Type::FunctionProto: {
00380     const FunctionProtoType *FPT = cast<FunctionProtoType>(Ty);
00381 
00382     // Check the return type.
00383     if (getTypeInfoLinkage(FPT->getResultType()) == 
00384         llvm::GlobalValue::InternalLinkage)
00385       return llvm::GlobalValue::InternalLinkage;
00386     
00387     // Check the parameter types.
00388     for (unsigned i = 0; i != FPT->getNumArgs(); ++i) {
00389       if (getTypeInfoLinkage(FPT->getArgType(i)) == 
00390           llvm::GlobalValue::InternalLinkage)
00391         return llvm::GlobalValue::InternalLinkage;
00392     }
00393     
00394     return llvm::GlobalValue::WeakODRLinkage;
00395   }
00396   
00397   case Type::ConstantArray: 
00398   case Type::IncompleteArray: {
00399     const ArrayType *AT = cast<ArrayType>(Ty);
00400 
00401     // Check the element type.
00402     if (getTypeInfoLinkage(AT->getElementType()) ==
00403         llvm::GlobalValue::InternalLinkage)
00404       return llvm::GlobalValue::InternalLinkage;
00405   }
00406 
00407   }
00408 
00409   return llvm::GlobalValue::WeakODRLinkage;
00410 }
00411 
00412 // CanUseSingleInheritance - Return whether the given record decl has a "single, 
00413 // public, non-virtual base at offset zero (i.e. the derived class is dynamic 
00414 // iff the base is)", according to Itanium C++ ABI, 2.95p6b.
00415 static bool CanUseSingleInheritance(const CXXRecordDecl *RD) {
00416   // Check the number of bases.
00417   if (RD->getNumBases() != 1)
00418     return false;
00419   
00420   // Get the base.
00421   CXXRecordDecl::base_class_const_iterator Base = RD->bases_begin();
00422   
00423   // Check that the base is not virtual.
00424   if (Base->isVirtual())
00425     return false;
00426   
00427   // Check that the base is public.
00428   if (Base->getAccessSpecifier() != AS_public)
00429     return false;
00430   
00431   // Check that the class is dynamic iff the base is.
00432   const CXXRecordDecl *BaseDecl = 
00433     cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
00434   if (!BaseDecl->isEmpty() && 
00435       BaseDecl->isDynamicClass() != RD->isDynamicClass())
00436     return false;
00437   
00438   return true;
00439 }
00440 
00441 void RTTIBuilder::BuildVtablePointer(const Type *Ty) {
00442   const char *VtableName;
00443 
00444   switch (Ty->getTypeClass()) {
00445   default: assert(0 && "Unhandled type!");
00446 
00447   // GCC treats vector types as fundamental types.
00448   case Type::Vector:
00449   case Type::ExtVector:
00450     // abi::__fundamental_type_info.
00451     VtableName = "_ZTVN10__cxxabiv123__fundamental_type_infoE";
00452     break;
00453 
00454   case Type::ConstantArray:
00455   case Type::IncompleteArray:
00456     // abi::__array_type_info.
00457     VtableName = "_ZTVN10__cxxabiv117__array_type_infoE";
00458     break;
00459 
00460   case Type::FunctionNoProto:
00461   case Type::FunctionProto:
00462     // abi::__function_type_info.
00463     VtableName = "_ZTVN10__cxxabiv120__function_type_infoE";
00464     break;
00465 
00466   case Type::Enum:
00467     // abi::__enum_type_info.
00468     VtableName = "_ZTVN10__cxxabiv116__enum_type_infoE";
00469     break;
00470       
00471   case Type::Record: {
00472     const CXXRecordDecl *RD = 
00473       cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
00474     
00475     if (!RD->hasDefinition() || !RD->getNumBases()) {
00476       // abi::__class_type_info.
00477       VtableName = "_ZTVN10__cxxabiv117__class_type_infoE";
00478     } else if (CanUseSingleInheritance(RD)) {
00479       // abi::__si_class_type_info.
00480       VtableName = "_ZTVN10__cxxabiv120__si_class_type_infoE";
00481     } else {
00482       // abi::__vmi_class_type_info.
00483       VtableName = "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
00484     }
00485     
00486     break;
00487   }
00488 
00489   case Type::Pointer:
00490     // abi::__pointer_type_info.
00491     VtableName = "_ZTVN10__cxxabiv119__pointer_type_infoE";
00492     break;
00493 
00494   case Type::MemberPointer:
00495     // abi::__pointer_to_member_type_info.
00496     VtableName = "_ZTVN10__cxxabiv129__pointer_to_member_type_infoE";
00497     break;
00498   }
00499 
00500   llvm::Constant *Vtable = 
00501     CGM.getModule().getOrInsertGlobal(VtableName, Int8PtrTy);
00502     
00503   const llvm::Type *PtrDiffTy = 
00504     CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
00505 
00506   // The vtable address point is 2.
00507   llvm::Constant *Two = llvm::ConstantInt::get(PtrDiffTy, 2);
00508   Vtable = llvm::ConstantExpr::getInBoundsGetElementPtr(Vtable, &Two, 1);
00509   Vtable = llvm::ConstantExpr::getBitCast(Vtable, Int8PtrTy);
00510 
00511   Fields.push_back(Vtable);
00512 }
00513 
00514 llvm::Constant *RTTIBuilder::BuildTypeInfo(QualType Ty) {
00515   // We want to operate on the canonical type.
00516   Ty = CGM.getContext().getCanonicalType(Ty);
00517 
00518   // Check if we've already emitted an RTTI descriptor for this type.
00519   llvm::SmallString<256> OutName;
00520   CGM.getMangleContext().mangleCXXRTTI(Ty, OutName);
00521   llvm::StringRef Name = OutName.str();
00522   
00523   llvm::GlobalVariable *OldGV = CGM.getModule().getNamedGlobal(Name);
00524   if (OldGV && !OldGV->isDeclaration())
00525     return llvm::ConstantExpr::getBitCast(OldGV, Int8PtrTy);
00526   
00527   // Check if there is already an external RTTI descriptor for this type.
00528   if (ShouldUseExternalRTTIDescriptor(Ty))
00529     return GetAddrOfExternalRTTIDescriptor(Ty);
00530 
00531   llvm::GlobalVariable::LinkageTypes Linkage = getTypeInfoLinkage(Ty);
00532 
00533   // Add the vtable pointer.
00534   BuildVtablePointer(cast<Type>(Ty));
00535   
00536   // And the name.
00537   Fields.push_back(BuildName(Ty, DecideHidden(Ty), Linkage));
00538   
00539   switch (Ty->getTypeClass()) {
00540   default: assert(false && "Unhandled type class!");
00541   case Type::Builtin:
00542     assert(false && "Builtin type info must be in the standard library!");
00543     break;
00544 
00545   // GCC treats vector types as fundamental types.
00546   case Type::Vector:
00547   case Type::ExtVector:
00548     // Itanium C++ ABI 2.9.5p4:
00549     // abi::__fundamental_type_info adds no data members to std::type_info.
00550     break;
00551       
00552   case Type::ConstantArray:
00553   case Type::IncompleteArray:
00554     // Itanium C++ ABI 2.9.5p5:
00555     // abi::__array_type_info adds no data members to std::type_info.
00556     break;
00557 
00558   case Type::FunctionNoProto:
00559   case Type::FunctionProto:
00560     // Itanium C++ ABI 2.9.5p5:
00561     // abi::__function_type_info adds no data members to std::type_info.
00562     break;
00563 
00564   case Type::Enum:
00565     // Itanium C++ ABI 2.9.5p5:
00566     // abi::__enum_type_info adds no data members to std::type_info.
00567     break;
00568 
00569   case Type::Record: {
00570     const CXXRecordDecl *RD = 
00571       cast<CXXRecordDecl>(cast<RecordType>(Ty)->getDecl());
00572     if (!RD->hasDefinition() || !RD->getNumBases()) {
00573       // We don't need to emit any fields.
00574       break;
00575     }
00576     
00577     if (CanUseSingleInheritance(RD))
00578       BuildSIClassTypeInfo(RD);
00579     else 
00580       BuildVMIClassTypeInfo(RD);
00581 
00582     break;
00583   }
00584       
00585   case Type::Pointer:
00586     BuildPointerTypeInfo(cast<PointerType>(Ty));
00587     break;
00588   
00589   case Type::MemberPointer:
00590     BuildPointerToMemberTypeInfo(cast<MemberPointerType>(Ty));
00591     break;
00592   }
00593 
00594   llvm::Constant *Init = 
00595     llvm::ConstantStruct::get(VMContext, &Fields[0], Fields.size(), 
00596                               /*Packed=*/false);
00597 
00598   llvm::GlobalVariable *GV = 
00599     new llvm::GlobalVariable(CGM.getModule(), Init->getType(), 
00600                              /*Constant=*/true, Linkage, Init, Name);
00601   
00602   // If there's already an old global variable, replace it with the new one.
00603   if (OldGV) {
00604     GV->takeName(OldGV);
00605     llvm::Constant *NewPtr = 
00606       llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
00607     OldGV->replaceAllUsesWith(NewPtr);
00608     OldGV->eraseFromParent();
00609   }
00610     
00611   return llvm::ConstantExpr::getBitCast(GV, Int8PtrTy);
00612 }
00613 
00614 /// ComputeQualifierFlags - Compute the pointer type info flags from the
00615 /// given qualifier.
00616 static unsigned ComputeQualifierFlags(Qualifiers Quals) {
00617   unsigned Flags = 0;
00618 
00619   if (Quals.hasConst())
00620     Flags |= RTTIBuilder::PTI_Const;
00621   if (Quals.hasVolatile())
00622     Flags |= RTTIBuilder::PTI_Volatile;
00623   if (Quals.hasRestrict())
00624     Flags |= RTTIBuilder::PTI_Restrict;
00625 
00626   return Flags;
00627 }
00628 
00629 /// BuildSIClassTypeInfo - Build an abi::__si_class_type_info, used for single
00630 /// inheritance, according to the Itanium C++ ABI, 2.95p6b.
00631 void RTTIBuilder::BuildSIClassTypeInfo(const CXXRecordDecl *RD) {
00632   // Itanium C++ ABI 2.9.5p6b:
00633   // It adds to abi::__class_type_info a single member pointing to the 
00634   // type_info structure for the base type,
00635   llvm::Constant *BaseTypeInfo = 
00636     RTTIBuilder(CGM).BuildTypeInfo(RD->bases_begin()->getType());
00637   Fields.push_back(BaseTypeInfo);
00638 }
00639 
00640 /// SeenBases - Contains virtual and non-virtual bases seen when traversing
00641 /// a class hierarchy.
00642 struct SeenBases {
00643   llvm::SmallPtrSet<const CXXRecordDecl *, 16> NonVirtualBases;
00644   llvm::SmallPtrSet<const CXXRecordDecl *, 16> VirtualBases;
00645 };
00646 
00647 /// ComputeVMIClassTypeInfoFlags - Compute the value of the flags member in
00648 /// abi::__vmi_class_type_info.
00649 ///
00650 static unsigned ComputeVMIClassTypeInfoFlags(const CXXBaseSpecifier *Base, 
00651                                              SeenBases &Bases) {
00652   
00653   unsigned Flags = 0;
00654   
00655   const CXXRecordDecl *BaseDecl = 
00656     cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
00657   
00658   if (Base->isVirtual()) {
00659     if (Bases.VirtualBases.count(BaseDecl)) {
00660       // If this virtual base has been seen before, then the class is diamond
00661       // shaped.
00662       Flags |= RTTIBuilder::VMI_DiamondShaped;
00663     } else {
00664       if (Bases.NonVirtualBases.count(BaseDecl))
00665         Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
00666 
00667       // Mark the virtual base as seen.
00668       Bases.VirtualBases.insert(BaseDecl);
00669     }
00670   } else {
00671     if (Bases.NonVirtualBases.count(BaseDecl)) {
00672       // If this non-virtual base has been seen before, then the class has non-
00673       // diamond shaped repeated inheritance.
00674       Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
00675     } else {
00676       if (Bases.VirtualBases.count(BaseDecl))
00677         Flags |= RTTIBuilder::VMI_NonDiamondRepeat;
00678         
00679       // Mark the non-virtual base as seen.
00680       Bases.NonVirtualBases.insert(BaseDecl);
00681     }
00682   }
00683 
00684   // Walk all bases.
00685   for (CXXRecordDecl::base_class_const_iterator I = BaseDecl->bases_begin(),
00686        E = BaseDecl->bases_end(); I != E; ++I) 
00687     Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
00688   
00689   return Flags;
00690 }
00691 
00692 static unsigned ComputeVMIClassTypeInfoFlags(const CXXRecordDecl *RD) {
00693   unsigned Flags = 0;
00694   SeenBases Bases;
00695   
00696   // Walk all bases.
00697   for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
00698        E = RD->bases_end(); I != E; ++I) 
00699     Flags |= ComputeVMIClassTypeInfoFlags(I, Bases);
00700   
00701   return Flags;
00702 }
00703 
00704 /// BuildVMIClassTypeInfo - Build an abi::__vmi_class_type_info, used for
00705 /// classes with bases that do not satisfy the abi::__si_class_type_info 
00706 /// constraints, according ti the Itanium C++ ABI, 2.9.5p5c.
00707 void RTTIBuilder::BuildVMIClassTypeInfo(const CXXRecordDecl *RD) {
00708   const llvm::Type *UnsignedIntLTy = 
00709     CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
00710   
00711   // Itanium C++ ABI 2.9.5p6c:
00712   //   __flags is a word with flags describing details about the class 
00713   //   structure, which may be referenced by using the __flags_masks 
00714   //   enumeration. These flags refer to both direct and indirect bases. 
00715   unsigned Flags = ComputeVMIClassTypeInfoFlags(RD);
00716   Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
00717 
00718   // Itanium C++ ABI 2.9.5p6c:
00719   //   __base_count is a word with the number of direct proper base class 
00720   //   descriptions that follow.
00721   Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, RD->getNumBases()));
00722   
00723   if (!RD->getNumBases())
00724     return;
00725   
00726   const llvm::Type *LongLTy = 
00727     CGM.getTypes().ConvertType(CGM.getContext().LongTy);
00728 
00729   // Now add the base class descriptions.
00730   
00731   // Itanium C++ ABI 2.9.5p6c:
00732   //   __base_info[] is an array of base class descriptions -- one for every 
00733   //   direct proper base. Each description is of the type:
00734   //
00735   //   struct abi::__base_class_type_info {
00736   //   public:
00737   //     const __class_type_info *__base_type;
00738   //     long __offset_flags;
00739   //
00740   //     enum __offset_flags_masks {
00741   //       __virtual_mask = 0x1,
00742   //       __public_mask = 0x2,
00743   //       __offset_shift = 8
00744   //     };
00745   //   };
00746   for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
00747        E = RD->bases_end(); I != E; ++I) {
00748     const CXXBaseSpecifier *Base = I;
00749 
00750     // The __base_type member points to the RTTI for the base type.
00751     Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(Base->getType()));
00752 
00753     const CXXRecordDecl *BaseDecl = 
00754       cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
00755 
00756     int64_t OffsetFlags = 0;
00757     
00758     // All but the lower 8 bits of __offset_flags are a signed offset. 
00759     // For a non-virtual base, this is the offset in the object of the base
00760     // subobject. For a virtual base, this is the offset in the virtual table of
00761     // the virtual base offset for the virtual base referenced (negative).
00762     if (Base->isVirtual())
00763       OffsetFlags = CGM.getVtableInfo().getVirtualBaseOffsetOffset(RD, BaseDecl);
00764     else {
00765       const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
00766       OffsetFlags = Layout.getBaseClassOffset(BaseDecl) / 8;
00767     };
00768     
00769     OffsetFlags <<= 8;
00770     
00771     // The low-order byte of __offset_flags contains flags, as given by the 
00772     // masks from the enumeration __offset_flags_masks.
00773     if (Base->isVirtual())
00774       OffsetFlags |= BCTI_Virtual;
00775     if (Base->getAccessSpecifier() == AS_public)
00776       OffsetFlags |= BCTI_Public;
00777 
00778     Fields.push_back(llvm::ConstantInt::get(LongLTy, OffsetFlags));
00779   }
00780 }
00781 
00782 /// BuildPointerTypeInfo - Build an abi::__pointer_type_info struct,
00783 /// used for pointer types.
00784 void RTTIBuilder::BuildPointerTypeInfo(const PointerType *Ty) {
00785   QualType PointeeTy = Ty->getPointeeType();
00786   
00787   // Itanium C++ ABI 2.9.5p7:
00788   //   __flags is a flag word describing the cv-qualification and other 
00789   //   attributes of the type pointed to
00790   unsigned Flags = ComputeQualifierFlags(PointeeTy.getQualifiers());
00791 
00792   // Itanium C++ ABI 2.9.5p7:
00793   //   When the abi::__pbase_type_info is for a direct or indirect pointer to an
00794   //   incomplete class type, the incomplete target type flag is set. 
00795   if (ContainsIncompleteClassType(PointeeTy))
00796     Flags |= PTI_Incomplete;
00797 
00798   const llvm::Type *UnsignedIntLTy = 
00799     CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
00800   Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
00801   
00802   // Itanium C++ ABI 2.9.5p7:
00803   //  __pointee is a pointer to the std::type_info derivation for the 
00804   //  unqualified type being pointed to.
00805   llvm::Constant *PointeeTypeInfo = 
00806     RTTIBuilder(CGM).BuildTypeInfo(PointeeTy.getUnqualifiedType());
00807   Fields.push_back(PointeeTypeInfo);
00808 }
00809 
00810 /// BuildPointerToMemberTypeInfo - Build an abi::__pointer_to_member_type_info 
00811 /// struct, used for member pointer types.
00812 void RTTIBuilder::BuildPointerToMemberTypeInfo(const MemberPointerType *Ty) {
00813   QualType PointeeTy = Ty->getPointeeType();
00814   
00815   // Itanium C++ ABI 2.9.5p7:
00816   //   __flags is a flag word describing the cv-qualification and other 
00817   //   attributes of the type pointed to.
00818   unsigned Flags = ComputeQualifierFlags(PointeeTy.getQualifiers());
00819 
00820   const RecordType *ClassType = cast<RecordType>(Ty->getClass());
00821 
00822   // Itanium C++ ABI 2.9.5p7:
00823   //   When the abi::__pbase_type_info is for a direct or indirect pointer to an
00824   //   incomplete class type, the incomplete target type flag is set. 
00825   if (ContainsIncompleteClassType(PointeeTy))
00826     Flags |= PTI_Incomplete;
00827 
00828   if (IsIncompleteClassType(ClassType))
00829     Flags |= PTI_ContainingClassIncomplete;
00830   
00831   const llvm::Type *UnsignedIntLTy = 
00832     CGM.getTypes().ConvertType(CGM.getContext().UnsignedIntTy);
00833   Fields.push_back(llvm::ConstantInt::get(UnsignedIntLTy, Flags));
00834   
00835   // Itanium C++ ABI 2.9.5p7:
00836   //   __pointee is a pointer to the std::type_info derivation for the 
00837   //   unqualified type being pointed to.
00838   llvm::Constant *PointeeTypeInfo = 
00839     RTTIBuilder(CGM).BuildTypeInfo(PointeeTy.getUnqualifiedType());
00840   Fields.push_back(PointeeTypeInfo);
00841 
00842   // Itanium C++ ABI 2.9.5p9:
00843   //   __context is a pointer to an abi::__class_type_info corresponding to the
00844   //   class type containing the member pointed to 
00845   //   (e.g., the "A" in "int A::*").
00846   Fields.push_back(RTTIBuilder(CGM).BuildTypeInfo(QualType(ClassType, 0)));
00847 }
00848 
00849 llvm::Constant *CodeGenModule::GetAddrOfRTTIDescriptor(QualType Ty) {
00850   if (!getContext().getLangOptions().RTTI) {
00851     const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
00852     return llvm::Constant::getNullValue(Int8PtrTy);
00853   }
00854   
00855   return RTTIBuilder(*this).BuildTypeInfo(Ty);
00856 }