diff options
Diffstat (limited to 'contrib/llvm/tools/clang/lib/CodeGen/MicrosoftCXXABI.cpp')
-rw-r--r-- | contrib/llvm/tools/clang/lib/CodeGen/MicrosoftCXXABI.cpp | 1872 |
1 files changed, 1404 insertions, 468 deletions
diff --git a/contrib/llvm/tools/clang/lib/CodeGen/MicrosoftCXXABI.cpp b/contrib/llvm/tools/clang/lib/CodeGen/MicrosoftCXXABI.cpp index 7318fe7..a69d4dd 100644 --- a/contrib/llvm/tools/clang/lib/CodeGen/MicrosoftCXXABI.cpp +++ b/contrib/llvm/tools/clang/lib/CodeGen/MicrosoftCXXABI.cpp @@ -15,64 +15,92 @@ //===----------------------------------------------------------------------===// #include "CGCXXABI.h" -#include "CodeGenModule.h" #include "CGVTables.h" -#include "MicrosoftVBTables.h" +#include "CodeGenModule.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/VTableBuilder.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringSet.h" +#include "llvm/IR/CallSite.h" using namespace clang; using namespace CodeGen; namespace { +/// Holds all the vbtable globals for a given class. +struct VBTableGlobals { + const VPtrInfoVector *VBTables; + SmallVector<llvm::GlobalVariable *, 2> Globals; +}; + class MicrosoftCXXABI : public CGCXXABI { public: - MicrosoftCXXABI(CodeGenModule &CGM) : CGCXXABI(CGM) {} + MicrosoftCXXABI(CodeGenModule &CGM) + : CGCXXABI(CGM), BaseClassDescriptorType(nullptr), + ClassHierarchyDescriptorType(nullptr), + CompleteObjectLocatorType(nullptr) {} - bool HasThisReturn(GlobalDecl GD) const; + bool HasThisReturn(GlobalDecl GD) const override; - bool isReturnTypeIndirect(const CXXRecordDecl *RD) const { - // Structures that are not C++03 PODs are always indirect. - return !RD->isPOD(); - } + bool classifyReturnType(CGFunctionInfo &FI) const override; - RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const { - if (RD->hasNonTrivialCopyConstructor() || RD->hasNonTrivialDestructor()) - return RAA_DirectInMemory; - return RAA_Default; - } + RecordArgABI getRecordArgABI(const CXXRecordDecl *RD) const override; - StringRef GetPureVirtualCallName() { return "_purecall"; } + bool isSRetParameterAfterThis() const override { return true; } + + StringRef GetPureVirtualCallName() override { return "_purecall"; } // No known support for deleted functions in MSVC yet, so this choice is // arbitrary. - StringRef GetDeletedVirtualCallName() { return "_purecall"; } - - bool isInlineInitializedStaticDataMemberLinkOnce() { return true; } + StringRef GetDeletedVirtualCallName() override { return "_purecall"; } llvm::Value *adjustToCompleteObject(CodeGenFunction &CGF, llvm::Value *ptr, - QualType type); + QualType type) override; - llvm::Value *GetVirtualBaseClassOffset(CodeGenFunction &CGF, - llvm::Value *This, - const CXXRecordDecl *ClassDecl, - const CXXRecordDecl *BaseClassDecl); + llvm::GlobalVariable *getMSCompleteObjectLocator(const CXXRecordDecl *RD, + const VPtrInfo *Info); + + llvm::Constant *getAddrOfRTTIDescriptor(QualType Ty) override; + + bool shouldTypeidBeNullChecked(bool IsDeref, QualType SrcRecordTy) override; + void EmitBadTypeidCall(CodeGenFunction &CGF) override; + llvm::Value *EmitTypeid(CodeGenFunction &CGF, QualType SrcRecordTy, + llvm::Value *ThisPtr, + llvm::Type *StdTypeInfoPtrTy) override; + + bool shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, + QualType SrcRecordTy) override; + + llvm::Value *EmitDynamicCastCall(CodeGenFunction &CGF, llvm::Value *Value, + QualType SrcRecordTy, QualType DestTy, + QualType DestRecordTy, + llvm::BasicBlock *CastEnd) override; + + llvm::Value *EmitDynamicCastToVoid(CodeGenFunction &CGF, llvm::Value *Value, + QualType SrcRecordTy, + QualType DestTy) override; + + bool EmitBadCastCall(CodeGenFunction &CGF) override; + + llvm::Value * + GetVirtualBaseClassOffset(CodeGenFunction &CGF, llvm::Value *This, + const CXXRecordDecl *ClassDecl, + const CXXRecordDecl *BaseClassDecl) override; void BuildConstructorSignature(const CXXConstructorDecl *Ctor, - CXXCtorType Type, - CanQualType &ResTy, - SmallVectorImpl<CanQualType> &ArgTys); + CXXCtorType Type, CanQualType &ResTy, + SmallVectorImpl<CanQualType> &ArgTys) override; - llvm::BasicBlock *EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, - const CXXRecordDecl *RD); + llvm::BasicBlock * + EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, + const CXXRecordDecl *RD) override; void initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF, - const CXXRecordDecl *RD); + const CXXRecordDecl *RD) override; - void EmitCXXConstructors(const CXXConstructorDecl *D); + void EmitCXXConstructors(const CXXConstructorDecl *D) override; // Background on MSVC destructors // ============================== @@ -109,17 +137,18 @@ public: void BuildDestructorSignature(const CXXDestructorDecl *Dtor, CXXDtorType Type, CanQualType &ResTy, - SmallVectorImpl<CanQualType> &ArgTys); + SmallVectorImpl<CanQualType> &ArgTys) override; /// Non-base dtors should be emitted as delegating thunks in this ABI. bool useThunkForDtorVariant(const CXXDestructorDecl *Dtor, - CXXDtorType DT) const { + CXXDtorType DT) const override { return DT != Dtor_Base; } - void EmitCXXDestructors(const CXXDestructorDecl *D); + void EmitCXXDestructors(const CXXDestructorDecl *D) override; - const CXXRecordDecl *getThisArgumentTypeForMethod(const CXXMethodDecl *MD) { + const CXXRecordDecl * + getThisArgumentTypeForMethod(const CXXMethodDecl *MD) override { MD = MD->getCanonicalDecl(); if (MD->isVirtual() && !isa<CXXDestructorDecl>(MD)) { MicrosoftVTableContext::MethodVFTableLocation ML = @@ -131,76 +160,100 @@ public: // FIXME: might want to have a more precise type in the non-virtual // multiple inheritance case. if (ML.VBase || !ML.VFPtrOffset.isZero()) - return 0; + return nullptr; } return MD->getParent(); } - llvm::Value *adjustThisArgumentForVirtualCall(CodeGenFunction &CGF, - GlobalDecl GD, - llvm::Value *This); + llvm::Value * + adjustThisArgumentForVirtualFunctionCall(CodeGenFunction &CGF, GlobalDecl GD, + llvm::Value *This, + bool VirtualCall) override; - void BuildInstanceFunctionParams(CodeGenFunction &CGF, - QualType &ResTy, - FunctionArgList &Params); + void addImplicitStructorParams(CodeGenFunction &CGF, QualType &ResTy, + FunctionArgList &Params) override; llvm::Value *adjustThisParameterInVirtualFunctionPrologue( - CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This); + CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) override; - void EmitInstanceFunctionProlog(CodeGenFunction &CGF); + void EmitInstanceFunctionProlog(CodeGenFunction &CGF) override; - void EmitConstructorCall(CodeGenFunction &CGF, - const CXXConstructorDecl *D, CXXCtorType Type, - bool ForVirtualBase, bool Delegating, - llvm::Value *This, - CallExpr::const_arg_iterator ArgBeg, - CallExpr::const_arg_iterator ArgEnd); + unsigned addImplicitConstructorArgs(CodeGenFunction &CGF, + const CXXConstructorDecl *D, + CXXCtorType Type, bool ForVirtualBase, + bool Delegating, + CallArgList &Args) override; - void emitVTableDefinitions(CodeGenVTables &CGVT, const CXXRecordDecl *RD); + void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD, + CXXDtorType Type, bool ForVirtualBase, + bool Delegating, llvm::Value *This) override; + + void emitVTableDefinitions(CodeGenVTables &CGVT, + const CXXRecordDecl *RD) override; llvm::Value *getVTableAddressPointInStructor( CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, const CXXRecordDecl *NearestVBase, - bool &NeedsVirtualOffset); + bool &NeedsVirtualOffset) override; llvm::Constant * getVTableAddressPointForConstExpr(BaseSubobject Base, - const CXXRecordDecl *VTableClass); + const CXXRecordDecl *VTableClass) override; llvm::GlobalVariable *getAddrOfVTable(const CXXRecordDecl *RD, - CharUnits VPtrOffset); + CharUnits VPtrOffset) override; llvm::Value *getVirtualFunctionPointer(CodeGenFunction &CGF, GlobalDecl GD, - llvm::Value *This, llvm::Type *Ty); + llvm::Value *This, + llvm::Type *Ty) override; void EmitVirtualDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *Dtor, CXXDtorType DtorType, SourceLocation CallLoc, - llvm::Value *This); + llvm::Value *This) override; void adjustCallArgsForDestructorThunk(CodeGenFunction &CGF, GlobalDecl GD, - CallArgList &CallArgs) { + CallArgList &CallArgs) override { assert(GD.getDtorType() == Dtor_Deleting && "Only deleting destructor thunks are available in this ABI"); CallArgs.add(RValue::get(getStructorImplicitParamValue(CGF)), CGM.getContext().IntTy); } - void emitVirtualInheritanceTables(const CXXRecordDecl *RD); + void emitVirtualInheritanceTables(const CXXRecordDecl *RD) override; + + llvm::GlobalVariable * + getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD, + llvm::GlobalVariable::LinkageTypes Linkage); + + void emitVBTableDefinition(const VPtrInfo &VBT, const CXXRecordDecl *RD, + llvm::GlobalVariable *GV) const; + + void setThunkLinkage(llvm::Function *Thunk, bool ForVTable, + GlobalDecl GD, bool ReturnAdjustment) override { + // Never dllimport/dllexport thunks. + Thunk->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass); - void setThunkLinkage(llvm::Function *Thunk, bool ForVTable) { - Thunk->setLinkage(llvm::GlobalValue::WeakAnyLinkage); + GVALinkage Linkage = + getContext().GetGVALinkageForFunction(cast<FunctionDecl>(GD.getDecl())); + + if (Linkage == GVA_Internal) + Thunk->setLinkage(llvm::GlobalValue::InternalLinkage); + else if (ReturnAdjustment) + Thunk->setLinkage(llvm::GlobalValue::WeakODRLinkage); + else + Thunk->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage); } llvm::Value *performThisAdjustment(CodeGenFunction &CGF, llvm::Value *This, - const ThisAdjustment &TA); + const ThisAdjustment &TA) override; llvm::Value *performReturnAdjustment(CodeGenFunction &CGF, llvm::Value *Ret, - const ReturnAdjustment &RA); + const ReturnAdjustment &RA) override; void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, llvm::GlobalVariable *DeclPtr, - bool PerformInit); + bool PerformInit) override; // ==== Notes on array cookies ========= // @@ -225,17 +278,126 @@ public: // } // Whereas it prints "104" and "104" if you give A a destructor. - bool requiresArrayCookie(const CXXDeleteExpr *expr, QualType elementType); - bool requiresArrayCookie(const CXXNewExpr *expr); - CharUnits getArrayCookieSizeImpl(QualType type); + bool requiresArrayCookie(const CXXDeleteExpr *expr, + QualType elementType) override; + bool requiresArrayCookie(const CXXNewExpr *expr) override; + CharUnits getArrayCookieSizeImpl(QualType type) override; llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, llvm::Value *NewPtr, llvm::Value *NumElements, const CXXNewExpr *expr, - QualType ElementType); + QualType ElementType) override; llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, llvm::Value *allocPtr, - CharUnits cookieSize); + CharUnits cookieSize) override; + + friend struct MSRTTIBuilder; + + bool isImageRelative() const { + return CGM.getTarget().getPointerWidth(/*AddressSpace=*/0) == 64; + } + + // 5 routines for constructing the llvm types for MS RTTI structs. + llvm::StructType *getTypeDescriptorType(StringRef TypeInfoString) { + llvm::SmallString<32> TDTypeName("rtti.TypeDescriptor"); + TDTypeName += llvm::utostr(TypeInfoString.size()); + llvm::StructType *&TypeDescriptorType = + TypeDescriptorTypeMap[TypeInfoString.size()]; + if (TypeDescriptorType) + return TypeDescriptorType; + llvm::Type *FieldTypes[] = { + CGM.Int8PtrPtrTy, + CGM.Int8PtrTy, + llvm::ArrayType::get(CGM.Int8Ty, TypeInfoString.size() + 1)}; + TypeDescriptorType = + llvm::StructType::create(CGM.getLLVMContext(), FieldTypes, TDTypeName); + return TypeDescriptorType; + } + + llvm::Type *getImageRelativeType(llvm::Type *PtrType) { + if (!isImageRelative()) + return PtrType; + return CGM.IntTy; + } + + llvm::StructType *getBaseClassDescriptorType() { + if (BaseClassDescriptorType) + return BaseClassDescriptorType; + llvm::Type *FieldTypes[] = { + getImageRelativeType(CGM.Int8PtrTy), + CGM.IntTy, + CGM.IntTy, + CGM.IntTy, + CGM.IntTy, + CGM.IntTy, + getImageRelativeType(getClassHierarchyDescriptorType()->getPointerTo()), + }; + BaseClassDescriptorType = llvm::StructType::create( + CGM.getLLVMContext(), FieldTypes, "rtti.BaseClassDescriptor"); + return BaseClassDescriptorType; + } + + llvm::StructType *getClassHierarchyDescriptorType() { + if (ClassHierarchyDescriptorType) + return ClassHierarchyDescriptorType; + // Forward-declare RTTIClassHierarchyDescriptor to break a cycle. + ClassHierarchyDescriptorType = llvm::StructType::create( + CGM.getLLVMContext(), "rtti.ClassHierarchyDescriptor"); + llvm::Type *FieldTypes[] = { + CGM.IntTy, + CGM.IntTy, + CGM.IntTy, + getImageRelativeType( + getBaseClassDescriptorType()->getPointerTo()->getPointerTo()), + }; + ClassHierarchyDescriptorType->setBody(FieldTypes); + return ClassHierarchyDescriptorType; + } + + llvm::StructType *getCompleteObjectLocatorType() { + if (CompleteObjectLocatorType) + return CompleteObjectLocatorType; + CompleteObjectLocatorType = llvm::StructType::create( + CGM.getLLVMContext(), "rtti.CompleteObjectLocator"); + llvm::Type *FieldTypes[] = { + CGM.IntTy, + CGM.IntTy, + CGM.IntTy, + getImageRelativeType(CGM.Int8PtrTy), + getImageRelativeType(getClassHierarchyDescriptorType()->getPointerTo()), + getImageRelativeType(CompleteObjectLocatorType), + }; + llvm::ArrayRef<llvm::Type *> FieldTypesRef(FieldTypes); + if (!isImageRelative()) + FieldTypesRef = FieldTypesRef.drop_back(); + CompleteObjectLocatorType->setBody(FieldTypesRef); + return CompleteObjectLocatorType; + } + + llvm::GlobalVariable *getImageBase() { + StringRef Name = "__ImageBase"; + if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(Name)) + return GV; + + return new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty, + /*isConstant=*/true, + llvm::GlobalValue::ExternalLinkage, + /*Initializer=*/nullptr, Name); + } + + llvm::Constant *getImageRelativeConstant(llvm::Constant *PtrVal) { + if (!isImageRelative()) + return PtrVal; + + llvm::Constant *ImageBaseAsInt = + llvm::ConstantExpr::getPtrToInt(getImageBase(), CGM.IntPtrTy); + llvm::Constant *PtrValAsInt = + llvm::ConstantExpr::getPtrToInt(PtrVal, CGM.IntPtrTy); + llvm::Constant *Diff = + llvm::ConstantExpr::getSub(PtrValAsInt, ImageBaseAsInt, + /*HasNUW=*/true, /*HasNSW=*/true); + return llvm::ConstantExpr::getTrunc(Diff, CGM.IntTy); + } private: MicrosoftMangleContext &getMangleContext() { @@ -258,15 +420,12 @@ private: return C ? C : getZeroInt(); } + CharUnits getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD); + void GetNullMemberPointerFields(const MemberPointerType *MPT, llvm::SmallVectorImpl<llvm::Constant *> &fields); - /// \brief Finds the offset from the base of RD to the vbptr it uses, even if - /// it is reusing a vbptr from a non-virtual base. RD must have morally - /// virtual bases. - CharUnits GetVBPtrOffsetFromBases(const CXXRecordDecl *RD); - /// \brief Shared code for virtual base adjustment. Returns the offset from /// the vbptr to the virtual base. Optionally returns the address of the /// vbptr itself. @@ -274,13 +433,13 @@ private: llvm::Value *Base, llvm::Value *VBPtrOffset, llvm::Value *VBTableOffset, - llvm::Value **VBPtr = 0); + llvm::Value **VBPtr = nullptr); llvm::Value *GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, llvm::Value *Base, int32_t VBPtrOffset, int32_t VBTableOffset, - llvm::Value **VBPtr = 0) { + llvm::Value **VBPtr = nullptr) { llvm::Value *VBPOffset = llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset), *VBTOffset = llvm::ConstantInt::get(CGM.IntTy, VBTableOffset); return GetVBaseOffsetFromVBPtr(CGF, Base, VBPOffset, VBTOffset, VBPtr); @@ -288,8 +447,8 @@ private: /// \brief Performs a full virtual base adjustment. Used to dereference /// pointers to members of virtual bases. - llvm::Value *AdjustVirtualBase(CodeGenFunction &CGF, const CXXRecordDecl *RD, - llvm::Value *Base, + llvm::Value *AdjustVirtualBase(CodeGenFunction &CGF, const Expr *E, + const CXXRecordDecl *RD, llvm::Value *Base, llvm::Value *VirtualBaseAdjustmentOffset, llvm::Value *VBPtrOffset /* optional */); @@ -311,69 +470,71 @@ private: void EmitVBPtrStores(CodeGenFunction &CGF, const CXXRecordDecl *RD); /// \brief Caching wrapper around VBTableBuilder::enumerateVBTables(). - const VBTableVector &EnumerateVBTables(const CXXRecordDecl *RD); + const VBTableGlobals &enumerateVBTables(const CXXRecordDecl *RD); /// \brief Generate a thunk for calling a virtual member function MD. - llvm::Function *EmitVirtualMemPtrThunk(const CXXMethodDecl *MD, - StringRef ThunkName); + llvm::Function *EmitVirtualMemPtrThunk( + const CXXMethodDecl *MD, + const MicrosoftVTableContext::MethodVFTableLocation &ML); public: - virtual llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT); + llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT) override; - virtual bool isZeroInitializable(const MemberPointerType *MPT); + bool isZeroInitializable(const MemberPointerType *MPT) override; - virtual llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT); + llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT) override; - virtual llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, - CharUnits offset); - virtual llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD); - virtual llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT); + llvm::Constant *EmitMemberDataPointer(const MemberPointerType *MPT, + CharUnits offset) override; + llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD) override; + llvm::Constant *EmitMemberPointer(const APValue &MP, QualType MPT) override; - virtual llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, - llvm::Value *L, - llvm::Value *R, - const MemberPointerType *MPT, - bool Inequality); + llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF, + llvm::Value *L, + llvm::Value *R, + const MemberPointerType *MPT, + bool Inequality) override; - virtual llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, - llvm::Value *MemPtr, - const MemberPointerType *MPT); + llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF, + llvm::Value *MemPtr, + const MemberPointerType *MPT) override; - virtual llvm::Value *EmitMemberDataPointerAddress(CodeGenFunction &CGF, - llvm::Value *Base, - llvm::Value *MemPtr, - const MemberPointerType *MPT); + llvm::Value * + EmitMemberDataPointerAddress(CodeGenFunction &CGF, const Expr *E, + llvm::Value *Base, llvm::Value *MemPtr, + const MemberPointerType *MPT) override; - virtual llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, - const CastExpr *E, - llvm::Value *Src); + llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF, + const CastExpr *E, + llvm::Value *Src) override; - virtual llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, - llvm::Constant *Src); + llvm::Constant *EmitMemberPointerConversion(const CastExpr *E, + llvm::Constant *Src) override; - virtual llvm::Value * - EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, - llvm::Value *&This, - llvm::Value *MemPtr, - const MemberPointerType *MPT); + llvm::Value * + EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, const Expr *E, + llvm::Value *&This, llvm::Value *MemPtr, + const MemberPointerType *MPT) override; private: typedef std::pair<const CXXRecordDecl *, CharUnits> VFTableIdTy; - typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalVariable *> VFTablesMapTy; + typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalVariable *> VTablesMapTy; + typedef llvm::DenseMap<VFTableIdTy, llvm::GlobalValue *> VFTablesMapTy; /// \brief All the vftables that have been referenced. VFTablesMapTy VFTablesMap; + VTablesMapTy VTablesMap; /// \brief This set holds the record decls we've deferred vtable emission for. llvm::SmallPtrSet<const CXXRecordDecl *, 4> DeferredVFTables; /// \brief All the vbtables which have been referenced. - llvm::DenseMap<const CXXRecordDecl *, VBTableVector> VBTablesMap; + llvm::DenseMap<const CXXRecordDecl *, VBTableGlobals> VBTablesMap; /// Info on the global variable used to guard initialization of static locals. /// The BitIndex field is only used for externally invisible declarations. struct GuardInfo { - GuardInfo() : Guard(0), BitIndex(0) {} + GuardInfo() : Guard(nullptr), BitIndex(0) {} llvm::GlobalVariable *Guard; unsigned BitIndex; }; @@ -381,10 +542,69 @@ private: /// Map from DeclContext to the current guard variable. We assume that the /// AST is visited in source code order. llvm::DenseMap<const DeclContext *, GuardInfo> GuardVariableMap; + + llvm::DenseMap<size_t, llvm::StructType *> TypeDescriptorTypeMap; + llvm::StructType *BaseClassDescriptorType; + llvm::StructType *ClassHierarchyDescriptorType; + llvm::StructType *CompleteObjectLocatorType; }; } +CGCXXABI::RecordArgABI +MicrosoftCXXABI::getRecordArgABI(const CXXRecordDecl *RD) const { + switch (CGM.getTarget().getTriple().getArch()) { + default: + // FIXME: Implement for other architectures. + return RAA_Default; + + case llvm::Triple::x86: + // All record arguments are passed in memory on x86. Decide whether to + // construct the object directly in argument memory, or to construct the + // argument elsewhere and copy the bytes during the call. + + // If C++ prohibits us from making a copy, construct the arguments directly + // into argument memory. + if (!canCopyArgument(RD)) + return RAA_DirectInMemory; + + // Otherwise, construct the argument into a temporary and copy the bytes + // into the outgoing argument memory. + return RAA_Default; + + case llvm::Triple::x86_64: + // Win64 passes objects with non-trivial copy ctors indirectly. + if (RD->hasNonTrivialCopyConstructor()) + return RAA_Indirect; + + // Win64 passes objects larger than 8 bytes indirectly. + if (getContext().getTypeSize(RD->getTypeForDecl()) > 64) + return RAA_Indirect; + + // We have a trivial copy constructor or no copy constructors, but we have + // to make sure it isn't deleted. + bool CopyDeleted = false; + for (const CXXConstructorDecl *CD : RD->ctors()) { + if (CD->isCopyConstructor()) { + assert(CD->isTrivial()); + // We had at least one undeleted trivial copy ctor. Return directly. + if (!CD->isDeleted()) + return RAA_Default; + CopyDeleted = true; + } + } + + // The trivial copy constructor was deleted. Return indirectly. + if (CopyDeleted) + return RAA_Indirect; + + // There were no copy ctors. Return in RAX. + return RAA_Default; + } + + llvm_unreachable("invalid enum"); +} + llvm::Value *MicrosoftCXXABI::adjustToCompleteObject(CodeGenFunction &CGF, llvm::Value *ptr, QualType type) { @@ -392,33 +612,127 @@ llvm::Value *MicrosoftCXXABI::adjustToCompleteObject(CodeGenFunction &CGF, return ptr; } -/// \brief Finds the first non-virtual base of RD that has virtual bases. If RD -/// doesn't have a vbptr, it will reuse the vbptr of the returned class. -static const CXXRecordDecl *FindFirstNVBaseWithVBases(const CXXRecordDecl *RD) { - for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), - E = RD->bases_end(); I != E; ++I) { - const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); - if (!I->isVirtual() && Base->getNumVBases() > 0) - return Base; - } - llvm_unreachable("RD must have an nv base with vbases"); +/// \brief Gets the offset to the virtual base that contains the vfptr for +/// MS-ABI polymorphic types. +static llvm::Value *getPolymorphicOffset(CodeGenFunction &CGF, + const CXXRecordDecl *RD, + llvm::Value *Value) { + const ASTContext &Context = RD->getASTContext(); + for (const CXXBaseSpecifier &Base : RD->vbases()) + if (Context.getASTRecordLayout(Base.getType()->getAsCXXRecordDecl()) + .hasExtendableVFPtr()) + return CGF.CGM.getCXXABI().GetVirtualBaseClassOffset( + CGF, Value, RD, Base.getType()->getAsCXXRecordDecl()); + llvm_unreachable("One of our vbases should be polymorphic."); } -CharUnits MicrosoftCXXABI::GetVBPtrOffsetFromBases(const CXXRecordDecl *RD) { - assert(RD->getNumVBases()); - CharUnits Total = CharUnits::Zero(); - while (RD) { - const ASTRecordLayout &RDLayout = getContext().getASTRecordLayout(RD); - CharUnits VBPtrOffset = RDLayout.getVBPtrOffset(); - // -1 is the sentinel for no vbptr. - if (VBPtrOffset != CharUnits::fromQuantity(-1)) { - Total += VBPtrOffset; - break; - } - RD = FindFirstNVBaseWithVBases(RD); - Total += RDLayout.getBaseClassOffset(RD); - } - return Total; +static std::pair<llvm::Value *, llvm::Value *> +performBaseAdjustment(CodeGenFunction &CGF, llvm::Value *Value, + QualType SrcRecordTy) { + Value = CGF.Builder.CreateBitCast(Value, CGF.Int8PtrTy); + const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); + + if (CGF.getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr()) + return std::make_pair(Value, llvm::ConstantInt::get(CGF.Int32Ty, 0)); + + // Perform a base adjustment. + llvm::Value *Offset = getPolymorphicOffset(CGF, SrcDecl, Value); + Value = CGF.Builder.CreateInBoundsGEP(Value, Offset); + Offset = CGF.Builder.CreateTrunc(Offset, CGF.Int32Ty); + return std::make_pair(Value, Offset); +} + +bool MicrosoftCXXABI::shouldTypeidBeNullChecked(bool IsDeref, + QualType SrcRecordTy) { + const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); + return IsDeref && + !CGM.getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr(); +} + +static llvm::CallSite emitRTtypeidCall(CodeGenFunction &CGF, + llvm::Value *Argument) { + llvm::Type *ArgTypes[] = {CGF.Int8PtrTy}; + llvm::FunctionType *FTy = + llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false); + llvm::Value *Args[] = {Argument}; + llvm::Constant *Fn = CGF.CGM.CreateRuntimeFunction(FTy, "__RTtypeid"); + return CGF.EmitRuntimeCallOrInvoke(Fn, Args); +} + +void MicrosoftCXXABI::EmitBadTypeidCall(CodeGenFunction &CGF) { + llvm::CallSite Call = + emitRTtypeidCall(CGF, llvm::Constant::getNullValue(CGM.VoidPtrTy)); + Call.setDoesNotReturn(); + CGF.Builder.CreateUnreachable(); +} + +llvm::Value *MicrosoftCXXABI::EmitTypeid(CodeGenFunction &CGF, + QualType SrcRecordTy, + llvm::Value *ThisPtr, + llvm::Type *StdTypeInfoPtrTy) { + llvm::Value *Offset; + std::tie(ThisPtr, Offset) = performBaseAdjustment(CGF, ThisPtr, SrcRecordTy); + return CGF.Builder.CreateBitCast( + emitRTtypeidCall(CGF, ThisPtr).getInstruction(), StdTypeInfoPtrTy); +} + +bool MicrosoftCXXABI::shouldDynamicCastCallBeNullChecked(bool SrcIsPtr, + QualType SrcRecordTy) { + const CXXRecordDecl *SrcDecl = SrcRecordTy->getAsCXXRecordDecl(); + return SrcIsPtr && + !CGM.getContext().getASTRecordLayout(SrcDecl).hasExtendableVFPtr(); +} + +llvm::Value *MicrosoftCXXABI::EmitDynamicCastCall( + CodeGenFunction &CGF, llvm::Value *Value, QualType SrcRecordTy, + QualType DestTy, QualType DestRecordTy, llvm::BasicBlock *CastEnd) { + llvm::Type *DestLTy = CGF.ConvertType(DestTy); + + llvm::Value *SrcRTTI = + CGF.CGM.GetAddrOfRTTIDescriptor(SrcRecordTy.getUnqualifiedType()); + llvm::Value *DestRTTI = + CGF.CGM.GetAddrOfRTTIDescriptor(DestRecordTy.getUnqualifiedType()); + + llvm::Value *Offset; + std::tie(Value, Offset) = performBaseAdjustment(CGF, Value, SrcRecordTy); + + // PVOID __RTDynamicCast( + // PVOID inptr, + // LONG VfDelta, + // PVOID SrcType, + // PVOID TargetType, + // BOOL isReference) + llvm::Type *ArgTypes[] = {CGF.Int8PtrTy, CGF.Int32Ty, CGF.Int8PtrTy, + CGF.Int8PtrTy, CGF.Int32Ty}; + llvm::Constant *Function = CGF.CGM.CreateRuntimeFunction( + llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false), + "__RTDynamicCast"); + llvm::Value *Args[] = { + Value, Offset, SrcRTTI, DestRTTI, + llvm::ConstantInt::get(CGF.Int32Ty, DestTy->isReferenceType())}; + Value = CGF.EmitRuntimeCallOrInvoke(Function, Args).getInstruction(); + return CGF.Builder.CreateBitCast(Value, DestLTy); +} + +llvm::Value * +MicrosoftCXXABI::EmitDynamicCastToVoid(CodeGenFunction &CGF, llvm::Value *Value, + QualType SrcRecordTy, + QualType DestTy) { + llvm::Value *Offset; + std::tie(Value, Offset) = performBaseAdjustment(CGF, Value, SrcRecordTy); + + // PVOID __RTCastToVoid( + // PVOID inptr) + llvm::Type *ArgTypes[] = {CGF.Int8PtrTy}; + llvm::Constant *Function = CGF.CGM.CreateRuntimeFunction( + llvm::FunctionType::get(CGF.Int8PtrTy, ArgTypes, false), + "__RTCastToVoid"); + llvm::Value *Args[] = {Value}; + return CGF.EmitRuntimeCall(Function, Args); +} + +bool MicrosoftCXXABI::EmitBadCastCall(CodeGenFunction &CGF) { + return false; } llvm::Value * @@ -426,7 +740,8 @@ MicrosoftCXXABI::GetVirtualBaseClassOffset(CodeGenFunction &CGF, llvm::Value *This, const CXXRecordDecl *ClassDecl, const CXXRecordDecl *BaseClassDecl) { - int64_t VBPtrChars = GetVBPtrOffsetFromBases(ClassDecl).getQuantity(); + int64_t VBPtrChars = + getContext().getASTRecordLayout(ClassDecl).getVBPtrOffset().getQuantity(); llvm::Value *VBPtrOffset = llvm::ConstantInt::get(CGM.PtrDiffTy, VBPtrChars); CharUnits IntSize = getContext().getTypeSizeInChars(getContext().IntTy); CharUnits VBTableChars = @@ -446,16 +761,41 @@ bool MicrosoftCXXABI::HasThisReturn(GlobalDecl GD) const { return isa<CXXConstructorDecl>(GD.getDecl()); } -void MicrosoftCXXABI::BuildConstructorSignature(const CXXConstructorDecl *Ctor, - CXXCtorType Type, - CanQualType &ResTy, - SmallVectorImpl<CanQualType> &ArgTys) { - // 'this' parameter and 'this' return are already in place +bool MicrosoftCXXABI::classifyReturnType(CGFunctionInfo &FI) const { + const CXXRecordDecl *RD = FI.getReturnType()->getAsCXXRecordDecl(); + if (!RD) + return false; + + if (FI.isInstanceMethod()) { + // If it's an instance method, aggregates are always returned indirectly via + // the second parameter. + FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false); + FI.getReturnInfo().setSRetAfterThis(FI.isInstanceMethod()); + return true; + } else if (!RD->isPOD()) { + // If it's a free function, non-POD types are returned indirectly. + FI.getReturnInfo() = ABIArgInfo::getIndirect(0, /*ByVal=*/false); + return true; + } + + // Otherwise, use the C ABI rules. + return false; +} + +void MicrosoftCXXABI::BuildConstructorSignature( + const CXXConstructorDecl *Ctor, CXXCtorType Type, CanQualType &ResTy, + SmallVectorImpl<CanQualType> &ArgTys) { + + // All parameters are already in place except is_most_derived, which goes + // after 'this' if it's variadic and last if it's not. const CXXRecordDecl *Class = Ctor->getParent(); + const FunctionProtoType *FPT = Ctor->getType()->castAs<FunctionProtoType>(); if (Class->getNumVBases()) { - // Constructors of classes with virtual bases take an implicit parameter. - ArgTys.push_back(CGM.getContext().IntTy); + if (FPT->isVariadic()) + ArgTys.insert(ArgTys.begin() + 1, CGM.getContext().IntTy); + else + ArgTys.push_back(CGM.getContext().IntTy); } } @@ -505,7 +845,7 @@ void MicrosoftCXXABI::initializeHiddenVirtualInheritanceMembers( unsigned AS = cast<llvm::PointerType>(getThisValue(CGF)->getType())->getAddressSpace(); - llvm::Value *Int8This = 0; // Initialize lazily. + llvm::Value *Int8This = nullptr; // Initialize lazily. for (VBOffsets::const_iterator I = VBaseMap.begin(), E = VBaseMap.end(); I != E; ++I) { @@ -547,19 +887,23 @@ void MicrosoftCXXABI::EmitVBPtrStores(CodeGenFunction &CGF, const CXXRecordDecl *RD) { llvm::Value *ThisInt8Ptr = CGF.Builder.CreateBitCast(getThisValue(CGF), CGM.Int8PtrTy, "this.int8"); + const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); - const VBTableVector &VBTables = EnumerateVBTables(RD); - for (VBTableVector::const_iterator I = VBTables.begin(), E = VBTables.end(); - I != E; ++I) { + const VBTableGlobals &VBGlobals = enumerateVBTables(RD); + for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) { + const VPtrInfo *VBT = (*VBGlobals.VBTables)[I]; + llvm::GlobalVariable *GV = VBGlobals.Globals[I]; const ASTRecordLayout &SubobjectLayout = - CGM.getContext().getASTRecordLayout(I->VBPtrSubobject.getBase()); - uint64_t Offs = (I->VBPtrSubobject.getBaseOffset() + - SubobjectLayout.getVBPtrOffset()).getQuantity(); + CGM.getContext().getASTRecordLayout(VBT->BaseWithVPtr); + CharUnits Offs = VBT->NonVirtualOffset; + Offs += SubobjectLayout.getVBPtrOffset(); + if (VBT->getVBaseWithVPtr()) + Offs += Layout.getVBaseClassOffset(VBT->getVBaseWithVPtr()); llvm::Value *VBPtr = - CGF.Builder.CreateConstInBoundsGEP1_64(ThisInt8Ptr, Offs); - VBPtr = CGF.Builder.CreateBitCast(VBPtr, I->GV->getType()->getPointerTo(0), - "vbptr." + I->ReusingBase->getName()); - CGF.Builder.CreateStore(I->GV, VBPtr); + CGF.Builder.CreateConstInBoundsGEP1_64(ThisInt8Ptr, Offs.getQuantity()); + VBPtr = CGF.Builder.CreateBitCast(VBPtr, GV->getType()->getPointerTo(0), + "vbptr." + VBT->ReusingBase->getName()); + CGF.Builder.CreateStore(GV, VBPtr); } } @@ -583,12 +927,61 @@ void MicrosoftCXXABI::EmitCXXDestructors(const CXXDestructorDecl *D) { CGM.EmitGlobal(GlobalDecl(D, Dtor_Base)); } -llvm::Value *MicrosoftCXXABI::adjustThisArgumentForVirtualCall( - CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) { +CharUnits +MicrosoftCXXABI::getVirtualFunctionPrologueThisAdjustment(GlobalDecl GD) { + GD = GD.getCanonicalDecl(); + const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); + + GlobalDecl LookupGD = GD; + if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { + // Complete destructors take a pointer to the complete object as a + // parameter, thus don't need this adjustment. + if (GD.getDtorType() == Dtor_Complete) + return CharUnits(); + + // There's no Dtor_Base in vftable but it shares the this adjustment with + // the deleting one, so look it up instead. + LookupGD = GlobalDecl(DD, Dtor_Deleting); + } + + MicrosoftVTableContext::MethodVFTableLocation ML = + CGM.getMicrosoftVTableContext().getMethodVFTableLocation(LookupGD); + CharUnits Adjustment = ML.VFPtrOffset; + + // Normal virtual instance methods need to adjust from the vfptr that first + // defined the virtual method to the virtual base subobject, but destructors + // do not. The vector deleting destructor thunk applies this adjustment for + // us if necessary. + if (isa<CXXDestructorDecl>(MD)) + Adjustment = CharUnits::Zero(); + + if (ML.VBase) { + const ASTRecordLayout &DerivedLayout = + CGM.getContext().getASTRecordLayout(MD->getParent()); + Adjustment += DerivedLayout.getVBaseClassOffset(ML.VBase); + } + + return Adjustment; +} + +llvm::Value *MicrosoftCXXABI::adjustThisArgumentForVirtualFunctionCall( + CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This, bool VirtualCall) { + if (!VirtualCall) { + // If the call of a virtual function is not virtual, we just have to + // compensate for the adjustment the virtual function does in its prologue. + CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD); + if (Adjustment.isZero()) + return This; + + unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace(); + llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS); + This = CGF.Builder.CreateBitCast(This, charPtrTy); + assert(Adjustment.isPositive()); + return CGF.Builder.CreateConstGEP1_32(This, Adjustment.getQuantity()); + } + GD = GD.getCanonicalDecl(); const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); - // FIXME: consider splitting the vdtor vs regular method code into two - // functions. GlobalDecl LookupGD = GD; if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { @@ -607,51 +1000,18 @@ llvm::Value *MicrosoftCXXABI::adjustThisArgumentForVirtualCall( unsigned AS = cast<llvm::PointerType>(This->getType())->getAddressSpace(); llvm::Type *charPtrTy = CGF.Int8Ty->getPointerTo(AS); CharUnits StaticOffset = ML.VFPtrOffset; - if (ML.VBase) { - bool AvoidVirtualOffset = false; - if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) { - // A base destructor can only be called from a complete destructor of the - // same record type or another destructor of a more derived type; - // or a constructor of the same record type if an exception is thrown. - assert(isa<CXXDestructorDecl>(CGF.CurGD.getDecl()) || - isa<CXXConstructorDecl>(CGF.CurGD.getDecl())); - const CXXRecordDecl *CurRD = - cast<CXXMethodDecl>(CGF.CurGD.getDecl())->getParent(); - - if (MD->getParent() == CurRD) { - if (isa<CXXDestructorDecl>(CGF.CurGD.getDecl())) - assert(CGF.CurGD.getDtorType() == Dtor_Complete); - if (isa<CXXConstructorDecl>(CGF.CurGD.getDecl())) - assert(CGF.CurGD.getCtorType() == Ctor_Complete); - // We're calling the main base dtor from a complete structor, - // so we know the "this" offset statically. - AvoidVirtualOffset = true; - } else { - // Let's see if we try to call a destructor of a non-virtual base. - for (CXXRecordDecl::base_class_const_iterator I = CurRD->bases_begin(), - E = CurRD->bases_end(); I != E; ++I) { - if (I->getType()->getAsCXXRecordDecl() != MD->getParent()) - continue; - // If we call a base destructor for a non-virtual base, we statically - // know where it expects the vfptr and "this" to be. - // The total offset should reflect the adjustment done by - // adjustThisParameterInVirtualFunctionPrologue(). - AvoidVirtualOffset = true; - break; - } - } - } - if (AvoidVirtualOffset) { - const ASTRecordLayout &Layout = - CGF.getContext().getASTRecordLayout(MD->getParent()); - StaticOffset += Layout.getVBaseClassOffset(ML.VBase); - } else { - This = CGF.Builder.CreateBitCast(This, charPtrTy); - llvm::Value *VBaseOffset = - GetVirtualBaseClassOffset(CGF, This, MD->getParent(), ML.VBase); - This = CGF.Builder.CreateInBoundsGEP(This, VBaseOffset); - } + // Base destructors expect 'this' to point to the beginning of the base + // subobject, not the first vfptr that happens to contain the virtual dtor. + // However, we still need to apply the virtual base adjustment. + if (isa<CXXDestructorDecl>(MD) && GD.getDtorType() == Dtor_Base) + StaticOffset = CharUnits::Zero(); + + if (ML.VBase) { + This = CGF.Builder.CreateBitCast(This, charPtrTy); + llvm::Value *VBaseOffset = + GetVirtualBaseClassOffset(CGF, This, MD->getParent(), ML.VBase); + This = CGF.Builder.CreateInBoundsGEP(This, VBaseOffset); } if (!StaticOffset.isZero()) { assert(StaticOffset.isPositive()); @@ -678,24 +1038,29 @@ static bool IsDeletingDtor(GlobalDecl GD) { return false; } -void MicrosoftCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF, - QualType &ResTy, - FunctionArgList &Params) { - BuildThisParam(CGF, Params); - +void MicrosoftCXXABI::addImplicitStructorParams(CodeGenFunction &CGF, + QualType &ResTy, + FunctionArgList &Params) { ASTContext &Context = getContext(); const CXXMethodDecl *MD = cast<CXXMethodDecl>(CGF.CurGD.getDecl()); + assert(isa<CXXConstructorDecl>(MD) || isa<CXXDestructorDecl>(MD)); if (isa<CXXConstructorDecl>(MD) && MD->getParent()->getNumVBases()) { ImplicitParamDecl *IsMostDerived - = ImplicitParamDecl::Create(Context, 0, + = ImplicitParamDecl::Create(Context, nullptr, CGF.CurGD.getDecl()->getLocation(), &Context.Idents.get("is_most_derived"), Context.IntTy); - Params.push_back(IsMostDerived); + // The 'most_derived' parameter goes second if the ctor is variadic and last + // if it's not. Dtors can't be variadic. + const FunctionProtoType *FPT = MD->getType()->castAs<FunctionProtoType>(); + if (FPT->isVariadic()) + Params.insert(Params.begin() + 1, IsMostDerived); + else + Params.push_back(IsMostDerived); getStructorImplicitParamDecl(CGF) = IsMostDerived; } else if (IsDeletingDtor(CGF.CurGD)) { ImplicitParamDecl *ShouldDelete - = ImplicitParamDecl::Create(Context, 0, + = ImplicitParamDecl::Create(Context, nullptr, CGF.CurGD.getDecl()->getLocation(), &Context.Idents.get("should_call_delete"), Context.IntTy); @@ -706,36 +1071,12 @@ void MicrosoftCXXABI::BuildInstanceFunctionParams(CodeGenFunction &CGF, llvm::Value *MicrosoftCXXABI::adjustThisParameterInVirtualFunctionPrologue( CodeGenFunction &CGF, GlobalDecl GD, llvm::Value *This) { - GD = GD.getCanonicalDecl(); - const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); - - GlobalDecl LookupGD = GD; - if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) { - // Complete destructors take a pointer to the complete object as a - // parameter, thus don't need this adjustment. - if (GD.getDtorType() == Dtor_Complete) - return This; - - // There's no Dtor_Base in vftable but it shares the this adjustment with - // the deleting one, so look it up instead. - LookupGD = GlobalDecl(DD, Dtor_Deleting); - } - // In this ABI, every virtual function takes a pointer to one of the // subobjects that first defines it as the 'this' parameter, rather than a - // pointer to ther final overrider subobject. Thus, we need to adjust it back + // pointer to the final overrider subobject. Thus, we need to adjust it back // to the final overrider subobject before use. // See comments in the MicrosoftVFTableContext implementation for the details. - - MicrosoftVTableContext::MethodVFTableLocation ML = - CGM.getMicrosoftVTableContext().getMethodVFTableLocation(LookupGD); - CharUnits Adjustment = ML.VFPtrOffset; - if (ML.VBase) { - const ASTRecordLayout &DerivedLayout = - CGF.getContext().getASTRecordLayout(MD->getParent()); - Adjustment += DerivedLayout.getVBaseClassOffset(ML.VBase); - } - + CharUnits Adjustment = getVirtualFunctionPrologueThisAdjustment(GD); if (Adjustment.isZero()) return This; @@ -784,61 +1125,81 @@ void MicrosoftCXXABI::EmitInstanceFunctionProlog(CodeGenFunction &CGF) { } } -void MicrosoftCXXABI::EmitConstructorCall(CodeGenFunction &CGF, - const CXXConstructorDecl *D, - CXXCtorType Type, - bool ForVirtualBase, - bool Delegating, - llvm::Value *This, - CallExpr::const_arg_iterator ArgBeg, - CallExpr::const_arg_iterator ArgEnd) { +unsigned MicrosoftCXXABI::addImplicitConstructorArgs( + CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type, + bool ForVirtualBase, bool Delegating, CallArgList &Args) { assert(Type == Ctor_Complete || Type == Ctor_Base); - llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Ctor_Complete); - llvm::Value *ImplicitParam = 0; - QualType ImplicitParamTy; - if (D->getParent()->getNumVBases()) { - ImplicitParam = llvm::ConstantInt::get(CGM.Int32Ty, Type == Ctor_Complete); - ImplicitParamTy = getContext().IntTy; + // Check if we need a 'most_derived' parameter. + if (!D->getParent()->getNumVBases()) + return 0; + + // Add the 'most_derived' argument second if we are variadic or last if not. + const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>(); + llvm::Value *MostDerivedArg = + llvm::ConstantInt::get(CGM.Int32Ty, Type == Ctor_Complete); + RValue RV = RValue::get(MostDerivedArg); + if (MostDerivedArg) { + if (FPT->isVariadic()) + Args.insert(Args.begin() + 1, + CallArg(RV, getContext().IntTy, /*needscopy=*/false)); + else + Args.add(RV, getContext().IntTy); + } + + return 1; // Added one arg. +} + +void MicrosoftCXXABI::EmitDestructorCall(CodeGenFunction &CGF, + const CXXDestructorDecl *DD, + CXXDtorType Type, bool ForVirtualBase, + bool Delegating, llvm::Value *This) { + llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(DD, Type); + + if (DD->isVirtual()) { + assert(Type != CXXDtorType::Dtor_Deleting && + "The deleting destructor should only be called via a virtual call"); + This = adjustThisArgumentForVirtualFunctionCall(CGF, GlobalDecl(DD, Type), + This, false); } // FIXME: Provide a source location here. - CGF.EmitCXXMemberCall(D, SourceLocation(), Callee, ReturnValueSlot(), This, - ImplicitParam, ImplicitParamTy, ArgBeg, ArgEnd); + CGF.EmitCXXMemberCall(DD, SourceLocation(), Callee, ReturnValueSlot(), This, + /*ImplicitParam=*/nullptr, + /*ImplicitParamTy=*/QualType(), nullptr, nullptr); } void MicrosoftCXXABI::emitVTableDefinitions(CodeGenVTables &CGVT, const CXXRecordDecl *RD) { MicrosoftVTableContext &VFTContext = CGM.getMicrosoftVTableContext(); - MicrosoftVTableContext::VFPtrListTy VFPtrs = VFTContext.getVFPtrOffsets(RD); - llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD); + VPtrInfoVector VFPtrs = VFTContext.getVFPtrOffsets(RD); - for (MicrosoftVTableContext::VFPtrListTy::iterator I = VFPtrs.begin(), - E = VFPtrs.end(); I != E; ++I) { - llvm::GlobalVariable *VTable = getAddrOfVTable(RD, I->VFPtrFullOffset); + for (VPtrInfo *Info : VFPtrs) { + llvm::GlobalVariable *VTable = getAddrOfVTable(RD, Info->FullOffsetInMDC); if (VTable->hasInitializer()) continue; + llvm::Constant *RTTI = getMSCompleteObjectLocator(RD, Info); + const VTableLayout &VTLayout = - VFTContext.getVFTableLayout(RD, I->VFPtrFullOffset); + VFTContext.getVFTableLayout(RD, Info->FullOffsetInMDC); llvm::Constant *Init = CGVT.CreateVTableInitializer( RD, VTLayout.vtable_component_begin(), VTLayout.getNumVTableComponents(), VTLayout.vtable_thunk_begin(), - VTLayout.getNumVTableThunks()); - VTable->setInitializer(Init); + VTLayout.getNumVTableThunks(), RTTI); - VTable->setLinkage(Linkage); - CGM.setTypeVisibility(VTable, RD, CodeGenModule::TVK_ForVTable); + VTable->setInitializer(Init); } } llvm::Value *MicrosoftCXXABI::getVTableAddressPointInStructor( CodeGenFunction &CGF, const CXXRecordDecl *VTableClass, BaseSubobject Base, const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset) { - NeedsVirtualOffset = (NearestVBase != 0); + NeedsVirtualOffset = (NearestVBase != nullptr); - llvm::Value *VTableAddressPoint = - getAddrOfVTable(VTableClass, Base.getBaseOffset()); + (void)getAddrOfVTable(VTableClass, Base.getBaseOffset()); + VFTableIdTy ID(VTableClass, Base.getBaseOffset()); + llvm::GlobalValue *VTableAddressPoint = VFTablesMap[ID]; if (!VTableAddressPoint) { assert(Base.getBase()->getNumVBases() && !CGM.getContext().getASTRecordLayout(Base.getBase()).hasOwnVFPtr()); @@ -847,17 +1208,19 @@ llvm::Value *MicrosoftCXXABI::getVTableAddressPointInStructor( } static void mangleVFTableName(MicrosoftMangleContext &MangleContext, - const CXXRecordDecl *RD, const VFPtrInfo &VFPtr, + const CXXRecordDecl *RD, const VPtrInfo *VFPtr, SmallString<256> &Name) { llvm::raw_svector_ostream Out(Name); - MangleContext.mangleCXXVFTable(RD, VFPtr.PathToMangle, Out); + MangleContext.mangleCXXVFTable(RD, VFPtr->MangledPath, Out); } llvm::Constant *MicrosoftCXXABI::getVTableAddressPointForConstExpr( BaseSubobject Base, const CXXRecordDecl *VTableClass) { - llvm::Constant *VTable = getAddrOfVTable(VTableClass, Base.getBaseOffset()); - assert(VTable && "Couldn't find a vftable for the given base?"); - return VTable; + (void)getAddrOfVTable(VTableClass, Base.getBaseOffset()); + VFTableIdTy ID(VTableClass, Base.getBaseOffset()); + llvm::GlobalValue *VFTable = VFTablesMap[ID]; + assert(VFTable && "Couldn't find a vftable for the given base?"); + return VFTable; } llvm::GlobalVariable *MicrosoftCXXABI::getAddrOfVTable(const CXXRecordDecl *RD, @@ -866,18 +1229,16 @@ llvm::GlobalVariable *MicrosoftCXXABI::getAddrOfVTable(const CXXRecordDecl *RD, // shouldn't be used in the given record type. We want to cache this result in // VFTablesMap, thus a simple zero check is not sufficient. VFTableIdTy ID(RD, VPtrOffset); - VFTablesMapTy::iterator I; + VTablesMapTy::iterator I; bool Inserted; - llvm::tie(I, Inserted) = VFTablesMap.insert( - std::make_pair(ID, static_cast<llvm::GlobalVariable *>(0))); + std::tie(I, Inserted) = VTablesMap.insert(std::make_pair(ID, nullptr)); if (!Inserted) return I->second; llvm::GlobalVariable *&VTable = I->second; MicrosoftVTableContext &VTContext = CGM.getMicrosoftVTableContext(); - const MicrosoftVTableContext::VFPtrListTy &VFPtrs = - VTContext.getVFPtrOffsets(RD); + const VPtrInfoVector &VFPtrs = VTContext.getVFPtrOffsets(RD); if (DeferredVFTables.insert(RD)) { // We haven't processed this record type before. @@ -898,19 +1259,94 @@ llvm::GlobalVariable *MicrosoftCXXABI::getAddrOfVTable(const CXXRecordDecl *RD, } for (size_t J = 0, F = VFPtrs.size(); J != F; ++J) { - if (VFPtrs[J].VFPtrFullOffset != VPtrOffset) + if (VFPtrs[J]->FullOffsetInMDC != VPtrOffset) continue; + SmallString<256> VFTableName; + mangleVFTableName(getMangleContext(), RD, VFPtrs[J], VFTableName); + StringRef VTableName = VFTableName; + + uint64_t NumVTableSlots = + VTContext.getVFTableLayout(RD, VFPtrs[J]->FullOffsetInMDC) + .getNumVTableComponents(); + llvm::GlobalValue::LinkageTypes VTableLinkage = + llvm::GlobalValue::ExternalLinkage; + llvm::ArrayType *VTableType = + llvm::ArrayType::get(CGM.Int8PtrTy, NumVTableSlots); + if (getContext().getLangOpts().RTTIData) { + VTableLinkage = llvm::GlobalValue::PrivateLinkage; + VTableName = ""; + } - llvm::ArrayType *ArrayType = llvm::ArrayType::get( - CGM.Int8PtrTy, - VTContext.getVFTableLayout(RD, VFPtrs[J].VFPtrFullOffset) - .getNumVTableComponents()); - - SmallString<256> Name; - mangleVFTableName(getMangleContext(), RD, VFPtrs[J], Name); - VTable = CGM.CreateOrReplaceCXXRuntimeVariable( - Name.str(), ArrayType, llvm::GlobalValue::ExternalLinkage); - VTable->setUnnamedAddr(true); + VTable = CGM.getModule().getNamedGlobal(VFTableName); + if (!VTable) { + // Create a backing variable for the contents of VTable. The VTable may + // or may not include space for a pointer to RTTI data. + llvm::GlobalValue *VFTable = VTable = new llvm::GlobalVariable( + CGM.getModule(), VTableType, /*isConstant=*/true, VTableLinkage, + /*Initializer=*/nullptr, VTableName); + VTable->setUnnamedAddr(true); + + // Only insert a pointer into the VFTable for RTTI data if we are not + // importing it. We never reference the RTTI data directly so there is no + // need to make room for it. + if (getContext().getLangOpts().RTTIData && + !RD->hasAttr<DLLImportAttr>()) { + llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.IntTy, 0), + llvm::ConstantInt::get(CGM.IntTy, 1)}; + // Create a GEP which points just after the first entry in the VFTable, + // this should be the location of the first virtual method. + llvm::Constant *VTableGEP = + llvm::ConstantExpr::getInBoundsGetElementPtr(VTable, GEPIndices); + // The symbol for the VFTable is an alias to the GEP. It is + // transparent, to other modules, what the nature of this symbol is; all + // that matters is that the alias be the address of the first virtual + // method. + VFTable = llvm::GlobalAlias::create( + cast<llvm::SequentialType>(VTableGEP->getType())->getElementType(), + /*AddressSpace=*/0, llvm::GlobalValue::ExternalLinkage, + VFTableName.str(), VTableGEP, &CGM.getModule()); + } else { + // We don't need a GlobalAlias to be a symbol for the VTable if we won't + // be referencing any RTTI data. The GlobalVariable will end up being + // an appropriate definition of the VFTable. + VTable->setName(VFTableName.str()); + } + + VFTable->setUnnamedAddr(true); + if (RD->hasAttr<DLLImportAttr>()) + VFTable->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); + else if (RD->hasAttr<DLLExportAttr>()) + VFTable->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); + + llvm::GlobalValue::LinkageTypes VFTableLinkage = CGM.getVTableLinkage(RD); + if (VFTable != VTable) { + if (llvm::GlobalValue::isAvailableExternallyLinkage(VFTableLinkage)) { + // AvailableExternally implies that we grabbed the data from another + // executable. No need to stick the alias in a Comdat. + } else if (llvm::GlobalValue::isInternalLinkage(VFTableLinkage) || + llvm::GlobalValue::isWeakODRLinkage(VFTableLinkage) || + llvm::GlobalValue::isLinkOnceODRLinkage(VFTableLinkage)) { + // The alias is going to be dropped into a Comdat, no need to make it + // weak. + if (!llvm::GlobalValue::isInternalLinkage(VFTableLinkage)) + VFTableLinkage = llvm::GlobalValue::ExternalLinkage; + llvm::Comdat *C = + CGM.getModule().getOrInsertComdat(VFTable->getName()); + // We must indicate which VFTable is larger to support linking between + // translation units which do and do not have RTTI data. The largest + // VFTable contains the RTTI data; translation units which reference + // the smaller VFTable always reference it relative to the first + // virtual method. + C->setSelectionKind(llvm::Comdat::Largest); + VTable->setComdat(C); + } else { + llvm_unreachable("unexpected linkage for vftable!"); + } + } + VFTable->setLinkage(VFTableLinkage); + CGM.setGlobalVisibility(VFTable, RD); + VFTablesMap[ID] = VFTable; + } break; } @@ -925,7 +1361,8 @@ llvm::Value *MicrosoftCXXABI::getVirtualFunctionPointer(CodeGenFunction &CGF, CGBuilderTy &Builder = CGF.Builder; Ty = Ty->getPointerTo()->getPointerTo(); - llvm::Value *VPtr = adjustThisArgumentForVirtualCall(CGF, GD, This); + llvm::Value *VPtr = + adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true); llvm::Value *VTable = CGF.GetVTablePtr(VPtr, Ty); MicrosoftVTableContext::MethodVFTableLocation ML = @@ -955,30 +1392,47 @@ void MicrosoftCXXABI::EmitVirtualDestructorCall(CodeGenFunction &CGF, llvm::ConstantInt::get(llvm::IntegerType::getInt32Ty(CGF.getLLVMContext()), DtorType == Dtor_Deleting); - This = adjustThisArgumentForVirtualCall(CGF, GD, This); + This = adjustThisArgumentForVirtualFunctionCall(CGF, GD, This, true); CGF.EmitCXXMemberCall(Dtor, CallLoc, Callee, ReturnValueSlot(), This, - ImplicitParam, Context.IntTy, 0, 0); + ImplicitParam, Context.IntTy, nullptr, nullptr); } -const VBTableVector & -MicrosoftCXXABI::EnumerateVBTables(const CXXRecordDecl *RD) { +const VBTableGlobals & +MicrosoftCXXABI::enumerateVBTables(const CXXRecordDecl *RD) { // At this layer, we can key the cache off of a single class, which is much - // easier than caching at the GlobalVariable layer. - llvm::DenseMap<const CXXRecordDecl*, VBTableVector>::iterator I; - bool added; - llvm::tie(I, added) = VBTablesMap.insert(std::make_pair(RD, VBTableVector())); - VBTableVector &VBTables = I->second; - if (!added) - return VBTables; - - VBTableBuilder(CGM, RD).enumerateVBTables(VBTables); + // easier than caching each vbtable individually. + llvm::DenseMap<const CXXRecordDecl*, VBTableGlobals>::iterator Entry; + bool Added; + std::tie(Entry, Added) = + VBTablesMap.insert(std::make_pair(RD, VBTableGlobals())); + VBTableGlobals &VBGlobals = Entry->second; + if (!Added) + return VBGlobals; + + MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext(); + VBGlobals.VBTables = &Context.enumerateVBTables(RD); + + // Cache the globals for all vbtables so we don't have to recompute the + // mangled names. + llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD); + for (VPtrInfoVector::const_iterator I = VBGlobals.VBTables->begin(), + E = VBGlobals.VBTables->end(); + I != E; ++I) { + VBGlobals.Globals.push_back(getAddrOfVBTable(**I, RD, Linkage)); + } - return VBTables; + return VBGlobals; } -llvm::Function * -MicrosoftCXXABI::EmitVirtualMemPtrThunk(const CXXMethodDecl *MD, - StringRef ThunkName) { +llvm::Function *MicrosoftCXXABI::EmitVirtualMemPtrThunk( + const CXXMethodDecl *MD, + const MicrosoftVTableContext::MethodVFTableLocation &ML) { + // Calculate the mangled name. + SmallString<256> ThunkName; + llvm::raw_svector_ostream Out(ThunkName); + getMangleContext().mangleVirtualMemPtrThunk(MD, Out); + Out.flush(); + // If the thunk has been generated previously, just return it. if (llvm::GlobalValue *GV = CGM.getModule().getNamedValue(ThunkName)) return cast<llvm::Function>(GV); @@ -1002,24 +1456,127 @@ MicrosoftCXXABI::EmitVirtualMemPtrThunk(const CXXMethodDecl *MD, CodeGenFunction CGF(CGM); CGF.StartThunk(ThunkFn, MD, FnInfo); - // Get to the Callee. + // Load the vfptr and then callee from the vftable. The callee should have + // adjusted 'this' so that the vfptr is at offset zero. llvm::Value *This = CGF.LoadCXXThis(); - llvm::Value *Callee = getVirtualFunctionPointer(CGF, MD, This, ThunkTy); + llvm::Value *VTable = + CGF.GetVTablePtr(This, ThunkTy->getPointerTo()->getPointerTo()); + llvm::Value *VFuncPtr = + CGF.Builder.CreateConstInBoundsGEP1_64(VTable, ML.Index, "vfn"); + llvm::Value *Callee = CGF.Builder.CreateLoad(VFuncPtr); + + unsigned CallingConv; + CodeGen::AttributeListType AttributeList; + CGM.ConstructAttributeList(FnInfo, MD, AttributeList, CallingConv, true); + llvm::AttributeSet Attrs = + llvm::AttributeSet::get(CGF.getLLVMContext(), AttributeList); + + // Do a musttail call with perfect argument forwarding. Any inalloca argument + // will be forwarded in place without any copy. + SmallVector<llvm::Value *, 8> Args; + for (llvm::Argument &A : ThunkFn->args()) + Args.push_back(&A); + llvm::CallInst *Call = CGF.Builder.CreateCall(Callee, Args); + Call->setTailCallKind(llvm::CallInst::TCK_MustTail); + Call->setAttributes(Attrs); + Call->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv)); + + if (Call->getType()->isVoidTy()) + CGF.Builder.CreateRetVoid(); + else + CGF.Builder.CreateRet(Call); - // Make the call and return the result. - CGF.EmitCallAndReturnForThunk(MD, Callee, 0); + // Finish the function to maintain CodeGenFunction invariants. + // FIXME: Don't emit unreachable code. + CGF.EmitBlock(CGF.createBasicBlock()); + CGF.FinishFunction(); return ThunkFn; } void MicrosoftCXXABI::emitVirtualInheritanceTables(const CXXRecordDecl *RD) { - const VBTableVector &VBTables = EnumerateVBTables(RD); - llvm::GlobalVariable::LinkageTypes Linkage = CGM.getVTableLinkage(RD); + const VBTableGlobals &VBGlobals = enumerateVBTables(RD); + for (unsigned I = 0, E = VBGlobals.VBTables->size(); I != E; ++I) { + const VPtrInfo *VBT = (*VBGlobals.VBTables)[I]; + llvm::GlobalVariable *GV = VBGlobals.Globals[I]; + emitVBTableDefinition(*VBT, RD, GV); + } +} - for (VBTableVector::const_iterator I = VBTables.begin(), E = VBTables.end(); - I != E; ++I) { - I->EmitVBTableDefinition(CGM, RD, Linkage); +llvm::GlobalVariable * +MicrosoftCXXABI::getAddrOfVBTable(const VPtrInfo &VBT, const CXXRecordDecl *RD, + llvm::GlobalVariable::LinkageTypes Linkage) { + SmallString<256> OutName; + llvm::raw_svector_ostream Out(OutName); + getMangleContext().mangleCXXVBTable(RD, VBT.MangledPath, Out); + Out.flush(); + StringRef Name = OutName.str(); + + llvm::ArrayType *VBTableType = + llvm::ArrayType::get(CGM.IntTy, 1 + VBT.ReusingBase->getNumVBases()); + + assert(!CGM.getModule().getNamedGlobal(Name) && + "vbtable with this name already exists: mangling bug?"); + llvm::GlobalVariable *GV = + CGM.CreateOrReplaceCXXRuntimeVariable(Name, VBTableType, Linkage); + GV->setUnnamedAddr(true); + + if (RD->hasAttr<DLLImportAttr>()) + GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); + else if (RD->hasAttr<DLLExportAttr>()) + GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); + + return GV; +} + +void MicrosoftCXXABI::emitVBTableDefinition(const VPtrInfo &VBT, + const CXXRecordDecl *RD, + llvm::GlobalVariable *GV) const { + const CXXRecordDecl *ReusingBase = VBT.ReusingBase; + + assert(RD->getNumVBases() && ReusingBase->getNumVBases() && + "should only emit vbtables for classes with vbtables"); + + const ASTRecordLayout &BaseLayout = + CGM.getContext().getASTRecordLayout(VBT.BaseWithVPtr); + const ASTRecordLayout &DerivedLayout = + CGM.getContext().getASTRecordLayout(RD); + + SmallVector<llvm::Constant *, 4> Offsets(1 + ReusingBase->getNumVBases(), + nullptr); + + // The offset from ReusingBase's vbptr to itself always leads. + CharUnits VBPtrOffset = BaseLayout.getVBPtrOffset(); + Offsets[0] = llvm::ConstantInt::get(CGM.IntTy, -VBPtrOffset.getQuantity()); + + MicrosoftVTableContext &Context = CGM.getMicrosoftVTableContext(); + for (const auto &I : ReusingBase->vbases()) { + const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl(); + CharUnits Offset = DerivedLayout.getVBaseClassOffset(VBase); + assert(!Offset.isNegative()); + + // Make it relative to the subobject vbptr. + CharUnits CompleteVBPtrOffset = VBT.NonVirtualOffset + VBPtrOffset; + if (VBT.getVBaseWithVPtr()) + CompleteVBPtrOffset += + DerivedLayout.getVBaseClassOffset(VBT.getVBaseWithVPtr()); + Offset -= CompleteVBPtrOffset; + + unsigned VBIndex = Context.getVBTableIndex(ReusingBase, VBase); + assert(Offsets[VBIndex] == nullptr && "The same vbindex seen twice?"); + Offsets[VBIndex] = llvm::ConstantInt::get(CGM.IntTy, Offset.getQuantity()); } + + assert(Offsets.size() == + cast<llvm::ArrayType>(cast<llvm::PointerType>(GV->getType()) + ->getElementType())->getNumElements()); + llvm::ArrayType *VBTableType = + llvm::ArrayType::get(CGM.IntTy, Offsets.size()); + llvm::Constant *Init = llvm::ConstantArray::get(VBTableType, Offsets); + GV->setInitializer(Init); + + // Set the right visibility. + CGM.setGlobalVisibility(GV, RD); } llvm::Value *MicrosoftCXXABI::performThisAdjustment(CodeGenFunction &CGF, @@ -1149,6 +1706,15 @@ llvm::Value* MicrosoftCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, llvm::GlobalVariable *GV, bool PerformInit) { + // MSVC only uses guards for static locals. + if (!D.isStaticLocal()) { + assert(GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()); + // GlobalOpt is allowed to discard the initializer, so use linkonce_odr. + CGF.CurFn->setLinkage(llvm::GlobalValue::LinkOnceODRLinkage); + CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit); + return; + } + // MSVC always uses an i32 bitfield to guard initialization, which is *not* // threadsafe. Since the user may be linking in inline functions compiled by // cl.exe, there's no reason to provide a false sense of security by using @@ -1162,29 +1728,29 @@ void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, llvm::ConstantInt *Zero = llvm::ConstantInt::get(GuardTy, 0); // Get the guard variable for this function if we have one already. - GuardInfo &GI = GuardVariableMap[D.getDeclContext()]; + GuardInfo *GI = &GuardVariableMap[D.getDeclContext()]; unsigned BitIndex; - if (D.isExternallyVisible()) { + if (D.isStaticLocal() && D.isExternallyVisible()) { // Externally visible variables have to be numbered in Sema to properly // handle unreachable VarDecls. - BitIndex = getContext().getManglingNumber(&D); + BitIndex = getContext().getStaticLocalNumber(&D); assert(BitIndex > 0); BitIndex--; } else { // Non-externally visible variables are numbered here in CodeGen. - BitIndex = GI.BitIndex++; + BitIndex = GI->BitIndex++; } if (BitIndex >= 32) { if (D.isExternallyVisible()) ErrorUnsupportedABI(CGF, "more than 32 guarded initializations"); BitIndex %= 32; - GI.Guard = 0; + GI->Guard = nullptr; } // Lazily create the i32 bitfield for this function. - if (!GI.Guard) { + if (!GI->Guard) { // Mangle the name for the guard. SmallString<256> GuardName; { @@ -1193,13 +1759,15 @@ void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, Out.flush(); } - // Create the guard variable with a zero-initializer. Just absorb linkage - // and visibility from the guarded variable. - GI.Guard = new llvm::GlobalVariable(CGM.getModule(), GuardTy, false, - GV->getLinkage(), Zero, GuardName.str()); - GI.Guard->setVisibility(GV->getVisibility()); + // Create the guard variable with a zero-initializer. Just absorb linkage, + // visibility and dll storage class from the guarded variable. + GI->Guard = + new llvm::GlobalVariable(CGM.getModule(), GuardTy, false, + GV->getLinkage(), Zero, GuardName.str()); + GI->Guard->setVisibility(GV->getVisibility()); + GI->Guard->setDLLStorageClass(GV->getDLLStorageClass()); } else { - assert(GI.Guard->getLinkage() == GV->getLinkage() && + assert(GI->Guard->getLinkage() == GV->getLinkage() && "static local from the same function had different linkage"); } @@ -1211,7 +1779,7 @@ void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, // Test our bit from the guard variable. llvm::ConstantInt *Bit = llvm::ConstantInt::get(GuardTy, 1U << BitIndex); - llvm::LoadInst *LI = Builder.CreateLoad(GI.Guard); + llvm::LoadInst *LI = Builder.CreateLoad(GI->Guard); llvm::Value *IsInitialized = Builder.CreateICmpNE(Builder.CreateAnd(LI, Bit), Zero); llvm::BasicBlock *InitBlock = CGF.createBasicBlock("init"); @@ -1221,7 +1789,7 @@ void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, // Set our bit in the guard variable and emit the initializer and add a global // destructor if appropriate. CGF.EmitBlock(InitBlock); - Builder.CreateStore(Builder.CreateOr(LI, Bit), GI.Guard); + Builder.CreateStore(Builder.CreateOr(LI, Bit), GI->Guard); CGF.EmitCXXGlobalVarDeclInit(D, GV, PerformInit); Builder.CreateBr(EndBlock); @@ -1229,38 +1797,6 @@ void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, CGF.EmitBlock(EndBlock); } -// Member pointer helpers. -static bool hasVBPtrOffsetField(MSInheritanceModel Inheritance) { - return Inheritance == MSIM_Unspecified; -} - -static bool hasOnlyOneField(bool IsMemberFunction, - MSInheritanceModel Inheritance) { - return Inheritance <= MSIM_SinglePolymorphic || - (!IsMemberFunction && Inheritance <= MSIM_MultiplePolymorphic); -} - -// Only member pointers to functions need a this adjustment, since it can be -// combined with the field offset for data pointers. -static bool hasNonVirtualBaseAdjustmentField(bool IsMemberFunction, - MSInheritanceModel Inheritance) { - return (IsMemberFunction && Inheritance >= MSIM_Multiple); -} - -static bool hasVirtualBaseAdjustmentField(MSInheritanceModel Inheritance) { - return Inheritance >= MSIM_Virtual; -} - -// Use zero for the field offset of a null data member pointer if we can -// guarantee that zero is not a valid field offset, or if the member pointer has -// multiple fields. Polymorphic classes have a vfptr at offset zero, so we can -// use zero for null. If there are multiple fields, we can use zero even if it -// is a valid field offset because null-ness testing will check the other -// fields. -static bool nullFieldOffsetIsZero(MSInheritanceModel Inheritance) { - return Inheritance != MSIM_Multiple && Inheritance != MSIM_Single; -} - bool MicrosoftCXXABI::isZeroInitializable(const MemberPointerType *MPT) { // Null-ness for function memptrs only depends on the first field, which is // the function pointer. The rest don't matter, so we can zero initialize. @@ -1270,28 +1806,28 @@ bool MicrosoftCXXABI::isZeroInitializable(const MemberPointerType *MPT) { // The virtual base adjustment field is always -1 for null, so if we have one // we can't zero initialize. The field offset is sometimes also -1 if 0 is a // valid field offset. - const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); - MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); - return (!hasVirtualBaseAdjustmentField(Inheritance) && - nullFieldOffsetIsZero(Inheritance)); + const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); + MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); + return (!MSInheritanceAttr::hasVBTableOffsetField(Inheritance) && + RD->nullFieldOffsetIsZero()); } llvm::Type * MicrosoftCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) { - const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); - MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); + const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); + MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); llvm::SmallVector<llvm::Type *, 4> fields; if (MPT->isMemberFunctionPointer()) fields.push_back(CGM.VoidPtrTy); // FunctionPointerOrVirtualThunk else fields.push_back(CGM.IntTy); // FieldOffset - if (hasNonVirtualBaseAdjustmentField(MPT->isMemberFunctionPointer(), - Inheritance)) + if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(), + Inheritance)) fields.push_back(CGM.IntTy); - if (hasVBPtrOffsetField(Inheritance)) + if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) fields.push_back(CGM.IntTy); - if (hasVirtualBaseAdjustmentField(Inheritance)) + if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) fields.push_back(CGM.IntTy); // VirtualBaseAdjustmentOffset if (fields.size() == 1) @@ -1303,24 +1839,24 @@ void MicrosoftCXXABI:: GetNullMemberPointerFields(const MemberPointerType *MPT, llvm::SmallVectorImpl<llvm::Constant *> &fields) { assert(fields.empty()); - const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); - MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); + const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); + MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); if (MPT->isMemberFunctionPointer()) { // FunctionPointerOrVirtualThunk fields.push_back(llvm::Constant::getNullValue(CGM.VoidPtrTy)); } else { - if (nullFieldOffsetIsZero(Inheritance)) + if (RD->nullFieldOffsetIsZero()) fields.push_back(getZeroInt()); // FieldOffset else fields.push_back(getAllOnesInt()); // FieldOffset } - if (hasNonVirtualBaseAdjustmentField(MPT->isMemberFunctionPointer(), - Inheritance)) + if (MSInheritanceAttr::hasNVOffsetField(MPT->isMemberFunctionPointer(), + Inheritance)) fields.push_back(getZeroInt()); - if (hasVBPtrOffsetField(Inheritance)) + if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) fields.push_back(getZeroInt()); - if (hasVirtualBaseAdjustmentField(Inheritance)) + if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) fields.push_back(getAllOnesInt()); } @@ -1341,29 +1877,29 @@ MicrosoftCXXABI::EmitFullMemberPointer(llvm::Constant *FirstField, const CXXRecordDecl *RD, CharUnits NonVirtualBaseAdjustment) { - MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); + MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); // Single inheritance class member pointer are represented as scalars instead // of aggregates. - if (hasOnlyOneField(IsMemberFunction, Inheritance)) + if (MSInheritanceAttr::hasOnlyOneField(IsMemberFunction, Inheritance)) return FirstField; llvm::SmallVector<llvm::Constant *, 4> fields; fields.push_back(FirstField); - if (hasNonVirtualBaseAdjustmentField(IsMemberFunction, Inheritance)) + if (MSInheritanceAttr::hasNVOffsetField(IsMemberFunction, Inheritance)) fields.push_back(llvm::ConstantInt::get( CGM.IntTy, NonVirtualBaseAdjustment.getQuantity())); - if (hasVBPtrOffsetField(Inheritance)) { + if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) { CharUnits Offs = CharUnits::Zero(); if (RD->getNumVBases()) - Offs = GetVBPtrOffsetFromBases(RD); + Offs = getContext().getASTRecordLayout(RD).getVBPtrOffset(); fields.push_back(llvm::ConstantInt::get(CGM.IntTy, Offs.getQuantity())); } // The rest of the fields are adjusted by conversions to a more derived class. - if (hasVirtualBaseAdjustmentField(Inheritance)) + if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) fields.push_back(getZeroInt()); return llvm::ConstantStruct::getAnon(fields); @@ -1372,7 +1908,7 @@ MicrosoftCXXABI::EmitFullMemberPointer(llvm::Constant *FirstField, llvm::Constant * MicrosoftCXXABI::EmitMemberDataPointer(const MemberPointerType *MPT, CharUnits offset) { - const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); + const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); llvm::Constant *FirstField = llvm::ConstantInt::get(CGM.IntTy, offset.getQuantity()); return EmitFullMemberPointer(FirstField, /*IsMemberFunction=*/false, RD, @@ -1395,8 +1931,8 @@ llvm::Constant *MicrosoftCXXABI::EmitMemberPointer(const APValue &MP, // FIXME PR15713: Support virtual inheritance paths. if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MPD)) - return BuildMemberPointer(MPT->getClass()->getAsCXXRecordDecl(), - MD, ThisAdjustment); + return BuildMemberPointer(MPT->getMostRecentCXXRecordDecl(), MD, + ThisAdjustment); CharUnits FieldOffset = getContext().toCharUnitsFromBits(getContext().getFieldOffset(MPD)); @@ -1409,6 +1945,7 @@ MicrosoftCXXABI::BuildMemberPointer(const CXXRecordDecl *RD, CharUnits NonVirtualBaseAdjustment) { assert(MD->isInstance() && "Member function must not be static!"); MD = MD->getCanonicalDecl(); + RD = RD->getMostRecentDecl(); CodeGenTypes &Types = CGM.getTypes(); llvm::Constant *FirstField; @@ -1442,16 +1979,10 @@ MicrosoftCXXABI::BuildMemberPointer(const CXXRecordDecl *RD, "member function in virtual base class"); FirstField = llvm::Constant::getNullValue(CGM.VoidPtrTy); } else { - SmallString<256> ThunkName; - CharUnits PointerWidth = getContext().toCharUnitsFromBits( - getContext().getTargetInfo().getPointerWidth(0)); - uint64_t OffsetInVFTable = ML.Index * PointerWidth.getQuantity(); - llvm::raw_svector_ostream Out(ThunkName); - getMangleContext().mangleVirtualMemPtrThunk(MD, OffsetInVFTable, Out); - Out.flush(); - - llvm::Function *Thunk = EmitVirtualMemPtrThunk(MD, ThunkName.str()); + llvm::Function *Thunk = EmitVirtualMemPtrThunk(MD, ML); FirstField = llvm::ConstantExpr::getBitCast(Thunk, CGM.VoidPtrTy); + // Include the vfptr adjustment if the method is in a non-primary vftable. + NonVirtualBaseAdjustment += ML.VFPtrOffset; } } @@ -1486,9 +2017,10 @@ MicrosoftCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, // If this is a single field member pointer (single inheritance), this is a // single icmp. - const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); - MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); - if (hasOnlyOneField(MPT->isMemberFunctionPointer(), Inheritance)) + const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); + MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); + if (MSInheritanceAttr::hasOnlyOneField(MPT->isMemberFunctionPointer(), + Inheritance)) return Builder.CreateICmp(Eq, L, R); // Compare the first field. @@ -1497,7 +2029,7 @@ MicrosoftCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF, llvm::Value *Cmp0 = Builder.CreateICmp(Eq, L0, R0, "memptr.cmp.first"); // Compare everything other than the first field. - llvm::Value *Res = 0; + llvm::Value *Res = nullptr; llvm::StructType *LType = cast<llvm::StructType>(L->getType()); for (unsigned I = 1, E = LType->getNumElements(); I != E; ++I) { llvm::Value *LF = Builder.CreateExtractValue(L, I); @@ -1548,7 +2080,7 @@ MicrosoftCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF, for (int I = 1, E = fields.size(); I < E; ++I) { llvm::Value *Field = Builder.CreateExtractValue(MemPtr, I); llvm::Value *Next = Builder.CreateICmpNE(Field, fields[I], "memptr.cmp"); - Res = Builder.CreateAnd(Res, Next, "memptr.tobool"); + Res = Builder.CreateOr(Res, Next, "memptr.tobool"); } return Res; } @@ -1607,16 +2139,14 @@ MicrosoftCXXABI::GetVBaseOffsetFromVBPtr(CodeGenFunction &CGF, // Returns an adjusted base cast to i8*, since we do more address arithmetic on // it. -llvm::Value * -MicrosoftCXXABI::AdjustVirtualBase(CodeGenFunction &CGF, - const CXXRecordDecl *RD, llvm::Value *Base, - llvm::Value *VBTableOffset, - llvm::Value *VBPtrOffset) { +llvm::Value *MicrosoftCXXABI::AdjustVirtualBase( + CodeGenFunction &CGF, const Expr *E, const CXXRecordDecl *RD, + llvm::Value *Base, llvm::Value *VBTableOffset, llvm::Value *VBPtrOffset) { CGBuilderTy &Builder = CGF.Builder; Base = Builder.CreateBitCast(Base, CGM.Int8PtrTy); - llvm::BasicBlock *OriginalBB = 0; - llvm::BasicBlock *SkipAdjustBB = 0; - llvm::BasicBlock *VBaseAdjustBB = 0; + llvm::BasicBlock *OriginalBB = nullptr; + llvm::BasicBlock *SkipAdjustBB = nullptr; + llvm::BasicBlock *VBaseAdjustBB = nullptr; // In the unspecified inheritance model, there might not be a vbtable at all, // in which case we need to skip the virtual base lookup. If there is a @@ -1637,12 +2167,18 @@ MicrosoftCXXABI::AdjustVirtualBase(CodeGenFunction &CGF, // know the vbptr offset. if (!VBPtrOffset) { CharUnits offs = CharUnits::Zero(); - if (RD->getNumVBases()) { - offs = GetVBPtrOffsetFromBases(RD); - } + if (!RD->hasDefinition()) { + DiagnosticsEngine &Diags = CGF.CGM.getDiags(); + unsigned DiagID = Diags.getCustomDiagID( + DiagnosticsEngine::Error, + "member pointer representation requires a " + "complete class type for %0 to perform this expression"); + Diags.Report(E->getExprLoc(), DiagID) << RD << E->getSourceRange(); + } else if (RD->getNumVBases()) + offs = getContext().getASTRecordLayout(RD).getVBPtrOffset(); VBPtrOffset = llvm::ConstantInt::get(CGM.IntTy, offs.getQuantity()); } - llvm::Value *VBPtr = 0; + llvm::Value *VBPtr = nullptr; llvm::Value *VBaseOffs = GetVBaseOffsetFromVBPtr(CGF, Base, VBPtrOffset, VBTableOffset, &VBPtr); llvm::Value *AdjustedBase = Builder.CreateInBoundsGEP(VBPtr, VBaseOffs); @@ -1659,38 +2195,41 @@ MicrosoftCXXABI::AdjustVirtualBase(CodeGenFunction &CGF, return AdjustedBase; } -llvm::Value * -MicrosoftCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF, - llvm::Value *Base, - llvm::Value *MemPtr, - const MemberPointerType *MPT) { +llvm::Value *MicrosoftCXXABI::EmitMemberDataPointerAddress( + CodeGenFunction &CGF, const Expr *E, llvm::Value *Base, llvm::Value *MemPtr, + const MemberPointerType *MPT) { assert(MPT->isMemberDataPointer()); unsigned AS = Base->getType()->getPointerAddressSpace(); llvm::Type *PType = CGF.ConvertTypeForMem(MPT->getPointeeType())->getPointerTo(AS); CGBuilderTy &Builder = CGF.Builder; - const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); - MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); + const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); + MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); // Extract the fields we need, regardless of model. We'll apply them if we // have them. llvm::Value *FieldOffset = MemPtr; - llvm::Value *VirtualBaseAdjustmentOffset = 0; - llvm::Value *VBPtrOffset = 0; + llvm::Value *VirtualBaseAdjustmentOffset = nullptr; + llvm::Value *VBPtrOffset = nullptr; if (MemPtr->getType()->isStructTy()) { // We need to extract values. unsigned I = 0; FieldOffset = Builder.CreateExtractValue(MemPtr, I++); - if (hasVBPtrOffsetField(Inheritance)) + if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++); - if (hasVirtualBaseAdjustmentField(Inheritance)) + if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++); } if (VirtualBaseAdjustmentOffset) { - Base = AdjustVirtualBase(CGF, RD, Base, VirtualBaseAdjustmentOffset, + Base = AdjustVirtualBase(CGF, E, RD, Base, VirtualBaseAdjustmentOffset, VBPtrOffset); } + + // Cast to char*. + Base = Builder.CreateBitCast(Base, Builder.getInt8Ty()->getPointerTo(AS)); + + // Apply the offset, which we assume is non-null. llvm::Value *Addr = Builder.CreateInBoundsGEP(Base, FieldOffset, "memptr.offset"); @@ -1699,9 +2238,9 @@ MicrosoftCXXABI::EmitMemberDataPointerAddress(CodeGenFunction &CGF, return Builder.CreateBitCast(Addr, PType); } -static MSInheritanceModel +static MSInheritanceAttr::Spelling getInheritanceFromMemptr(const MemberPointerType *MPT) { - return MPT->getClass()->getAsCXXRecordDecl()->getMSInheritanceModel(); + return MPT->getMostRecentCXXRecordDecl()->getMSInheritanceModel(); } llvm::Value * @@ -1721,15 +2260,17 @@ MicrosoftCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, const MemberPointerType *SrcTy = E->getSubExpr()->getType()->castAs<MemberPointerType>(); const MemberPointerType *DstTy = E->getType()->castAs<MemberPointerType>(); - MSInheritanceModel SrcInheritance = getInheritanceFromMemptr(SrcTy); - MSInheritanceModel DstInheritance = getInheritanceFromMemptr(DstTy); bool IsFunc = SrcTy->isMemberFunctionPointer(); // If the classes use the same null representation, reinterpret_cast is a nop. bool IsReinterpret = E->getCastKind() == CK_ReinterpretMemberPointer; - if (IsReinterpret && (IsFunc || - nullFieldOffsetIsZero(SrcInheritance) == - nullFieldOffsetIsZero(DstInheritance))) + if (IsReinterpret && IsFunc) + return Src; + + CXXRecordDecl *SrcRD = SrcTy->getMostRecentCXXRecordDecl(); + CXXRecordDecl *DstRD = DstTy->getMostRecentCXXRecordDecl(); + if (IsReinterpret && + SrcRD->nullFieldOffsetIsZero() == DstRD->nullFieldOffsetIsZero()) return Src; CGBuilderTy &Builder = CGF.Builder; @@ -1755,18 +2296,19 @@ MicrosoftCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, // Decompose src. llvm::Value *FirstField = Src; - llvm::Value *NonVirtualBaseAdjustment = 0; - llvm::Value *VirtualBaseAdjustmentOffset = 0; - llvm::Value *VBPtrOffset = 0; - if (!hasOnlyOneField(IsFunc, SrcInheritance)) { + llvm::Value *NonVirtualBaseAdjustment = nullptr; + llvm::Value *VirtualBaseAdjustmentOffset = nullptr; + llvm::Value *VBPtrOffset = nullptr; + MSInheritanceAttr::Spelling SrcInheritance = SrcRD->getMSInheritanceModel(); + if (!MSInheritanceAttr::hasOnlyOneField(IsFunc, SrcInheritance)) { // We need to extract values. unsigned I = 0; FirstField = Builder.CreateExtractValue(Src, I++); - if (hasNonVirtualBaseAdjustmentField(IsFunc, SrcInheritance)) + if (MSInheritanceAttr::hasNVOffsetField(IsFunc, SrcInheritance)) NonVirtualBaseAdjustment = Builder.CreateExtractValue(Src, I++); - if (hasVBPtrOffsetField(SrcInheritance)) + if (MSInheritanceAttr::hasVBPtrOffsetField(SrcInheritance)) VBPtrOffset = Builder.CreateExtractValue(Src, I++); - if (hasVirtualBaseAdjustmentField(SrcInheritance)) + if (MSInheritanceAttr::hasVBTableOffsetField(SrcInheritance)) VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(Src, I++); } @@ -1788,20 +2330,21 @@ MicrosoftCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF, // FIXME PR15713: Support conversions through virtually derived classes. // Recompose dst from the null struct and the adjusted fields from src. + MSInheritanceAttr::Spelling DstInheritance = DstRD->getMSInheritanceModel(); llvm::Value *Dst; - if (hasOnlyOneField(IsFunc, DstInheritance)) { + if (MSInheritanceAttr::hasOnlyOneField(IsFunc, DstInheritance)) { Dst = FirstField; } else { Dst = llvm::UndefValue::get(DstNull->getType()); unsigned Idx = 0; Dst = Builder.CreateInsertValue(Dst, FirstField, Idx++); - if (hasNonVirtualBaseAdjustmentField(IsFunc, DstInheritance)) + if (MSInheritanceAttr::hasNVOffsetField(IsFunc, DstInheritance)) Dst = Builder.CreateInsertValue( Dst, getValueOrZeroInt(NonVirtualBaseAdjustment), Idx++); - if (hasVBPtrOffsetField(DstInheritance)) + if (MSInheritanceAttr::hasVBPtrOffsetField(DstInheritance)) Dst = Builder.CreateInsertValue( Dst, getValueOrZeroInt(VBPtrOffset), Idx++); - if (hasVirtualBaseAdjustmentField(DstInheritance)) + if (MSInheritanceAttr::hasVBTableOffsetField(DstInheritance)) Dst = Builder.CreateInsertValue( Dst, getValueOrZeroInt(VirtualBaseAdjustmentOffset), Idx++); } @@ -1833,24 +2376,24 @@ MicrosoftCXXABI::EmitMemberPointerConversion(const CastExpr *E, if (E->getCastKind() == CK_ReinterpretMemberPointer) return Src; - MSInheritanceModel SrcInheritance = getInheritanceFromMemptr(SrcTy); - MSInheritanceModel DstInheritance = getInheritanceFromMemptr(DstTy); + MSInheritanceAttr::Spelling SrcInheritance = getInheritanceFromMemptr(SrcTy); + MSInheritanceAttr::Spelling DstInheritance = getInheritanceFromMemptr(DstTy); // Decompose src. llvm::Constant *FirstField = Src; - llvm::Constant *NonVirtualBaseAdjustment = 0; - llvm::Constant *VirtualBaseAdjustmentOffset = 0; - llvm::Constant *VBPtrOffset = 0; + llvm::Constant *NonVirtualBaseAdjustment = nullptr; + llvm::Constant *VirtualBaseAdjustmentOffset = nullptr; + llvm::Constant *VBPtrOffset = nullptr; bool IsFunc = SrcTy->isMemberFunctionPointer(); - if (!hasOnlyOneField(IsFunc, SrcInheritance)) { + if (!MSInheritanceAttr::hasOnlyOneField(IsFunc, SrcInheritance)) { // We need to extract values. unsigned I = 0; FirstField = Src->getAggregateElement(I++); - if (hasNonVirtualBaseAdjustmentField(IsFunc, SrcInheritance)) + if (MSInheritanceAttr::hasNVOffsetField(IsFunc, SrcInheritance)) NonVirtualBaseAdjustment = Src->getAggregateElement(I++); - if (hasVBPtrOffsetField(SrcInheritance)) + if (MSInheritanceAttr::hasVBPtrOffsetField(SrcInheritance)) VBPtrOffset = Src->getAggregateElement(I++); - if (hasVirtualBaseAdjustmentField(SrcInheritance)) + if (MSInheritanceAttr::hasVBTableOffsetField(SrcInheritance)) VirtualBaseAdjustmentOffset = Src->getAggregateElement(I++); } @@ -1873,56 +2416,54 @@ MicrosoftCXXABI::EmitMemberPointerConversion(const CastExpr *E, // FIXME PR15713: Support conversions through virtually derived classes. // Recompose dst from the null struct and the adjusted fields from src. - if (hasOnlyOneField(IsFunc, DstInheritance)) + if (MSInheritanceAttr::hasOnlyOneField(IsFunc, DstInheritance)) return FirstField; llvm::SmallVector<llvm::Constant *, 4> Fields; Fields.push_back(FirstField); - if (hasNonVirtualBaseAdjustmentField(IsFunc, DstInheritance)) + if (MSInheritanceAttr::hasNVOffsetField(IsFunc, DstInheritance)) Fields.push_back(getConstantOrZeroInt(NonVirtualBaseAdjustment)); - if (hasVBPtrOffsetField(DstInheritance)) + if (MSInheritanceAttr::hasVBPtrOffsetField(DstInheritance)) Fields.push_back(getConstantOrZeroInt(VBPtrOffset)); - if (hasVirtualBaseAdjustmentField(DstInheritance)) + if (MSInheritanceAttr::hasVBTableOffsetField(DstInheritance)) Fields.push_back(getConstantOrZeroInt(VirtualBaseAdjustmentOffset)); return llvm::ConstantStruct::getAnon(Fields); } -llvm::Value * -MicrosoftCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, - llvm::Value *&This, - llvm::Value *MemPtr, - const MemberPointerType *MPT) { +llvm::Value *MicrosoftCXXABI::EmitLoadOfMemberFunctionPointer( + CodeGenFunction &CGF, const Expr *E, llvm::Value *&This, + llvm::Value *MemPtr, const MemberPointerType *MPT) { assert(MPT->isMemberFunctionPointer()); const FunctionProtoType *FPT = MPT->getPointeeType()->castAs<FunctionProtoType>(); - const CXXRecordDecl *RD = MPT->getClass()->getAsCXXRecordDecl(); + const CXXRecordDecl *RD = MPT->getMostRecentCXXRecordDecl(); llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType( CGM.getTypes().arrangeCXXMethodType(RD, FPT)); CGBuilderTy &Builder = CGF.Builder; - MSInheritanceModel Inheritance = RD->getMSInheritanceModel(); + MSInheritanceAttr::Spelling Inheritance = RD->getMSInheritanceModel(); // Extract the fields we need, regardless of model. We'll apply them if we // have them. llvm::Value *FunctionPointer = MemPtr; - llvm::Value *NonVirtualBaseAdjustment = NULL; - llvm::Value *VirtualBaseAdjustmentOffset = NULL; - llvm::Value *VBPtrOffset = NULL; + llvm::Value *NonVirtualBaseAdjustment = nullptr; + llvm::Value *VirtualBaseAdjustmentOffset = nullptr; + llvm::Value *VBPtrOffset = nullptr; if (MemPtr->getType()->isStructTy()) { // We need to extract values. unsigned I = 0; FunctionPointer = Builder.CreateExtractValue(MemPtr, I++); - if (hasNonVirtualBaseAdjustmentField(MPT, Inheritance)) + if (MSInheritanceAttr::hasNVOffsetField(MPT, Inheritance)) NonVirtualBaseAdjustment = Builder.CreateExtractValue(MemPtr, I++); - if (hasVBPtrOffsetField(Inheritance)) + if (MSInheritanceAttr::hasVBPtrOffsetField(Inheritance)) VBPtrOffset = Builder.CreateExtractValue(MemPtr, I++); - if (hasVirtualBaseAdjustmentField(Inheritance)) + if (MSInheritanceAttr::hasVBTableOffsetField(Inheritance)) VirtualBaseAdjustmentOffset = Builder.CreateExtractValue(MemPtr, I++); } if (VirtualBaseAdjustmentOffset) { - This = AdjustVirtualBase(CGF, RD, This, VirtualBaseAdjustmentOffset, + This = AdjustVirtualBase(CGF, E, RD, This, VirtualBaseAdjustmentOffset, VBPtrOffset); } @@ -1939,3 +2480,398 @@ MicrosoftCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF, CGCXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) { return new MicrosoftCXXABI(CGM); } + +// MS RTTI Overview: +// The run time type information emitted by cl.exe contains 5 distinct types of +// structures. Many of them reference each other. +// +// TypeInfo: Static classes that are returned by typeid. +// +// CompleteObjectLocator: Referenced by vftables. They contain information +// required for dynamic casting, including OffsetFromTop. They also contain +// a reference to the TypeInfo for the type and a reference to the +// CompleteHierarchyDescriptor for the type. +// +// ClassHieararchyDescriptor: Contains information about a class hierarchy. +// Used during dynamic_cast to walk a class hierarchy. References a base +// class array and the size of said array. +// +// BaseClassArray: Contains a list of classes in a hierarchy. BaseClassArray is +// somewhat of a misnomer because the most derived class is also in the list +// as well as multiple copies of virtual bases (if they occur multiple times +// in the hiearchy.) The BaseClassArray contains one BaseClassDescriptor for +// every path in the hierarchy, in pre-order depth first order. Note, we do +// not declare a specific llvm type for BaseClassArray, it's merely an array +// of BaseClassDescriptor pointers. +// +// BaseClassDescriptor: Contains information about a class in a class hierarchy. +// BaseClassDescriptor is also somewhat of a misnomer for the same reason that +// BaseClassArray is. It contains information about a class within a +// hierarchy such as: is this base is ambiguous and what is its offset in the +// vbtable. The names of the BaseClassDescriptors have all of their fields +// mangled into them so they can be aggressively deduplicated by the linker. + +static llvm::GlobalVariable *getTypeInfoVTable(CodeGenModule &CGM) { + StringRef MangledName("\01??_7type_info@@6B@"); + if (auto VTable = CGM.getModule().getNamedGlobal(MangledName)) + return VTable; + return new llvm::GlobalVariable(CGM.getModule(), CGM.Int8PtrTy, + /*Constant=*/true, + llvm::GlobalVariable::ExternalLinkage, + /*Initializer=*/nullptr, MangledName); +} + +namespace { + +/// \brief A Helper struct that stores information about a class in a class +/// hierarchy. The information stored in these structs struct is used during +/// the generation of ClassHierarchyDescriptors and BaseClassDescriptors. +// During RTTI creation, MSRTTIClasses are stored in a contiguous array with +// implicit depth first pre-order tree connectivity. getFirstChild and +// getNextSibling allow us to walk the tree efficiently. +struct MSRTTIClass { + enum { + IsPrivateOnPath = 1 | 8, + IsAmbiguous = 2, + IsPrivate = 4, + IsVirtual = 16, + HasHierarchyDescriptor = 64 + }; + MSRTTIClass(const CXXRecordDecl *RD) : RD(RD) {} + uint32_t initialize(const MSRTTIClass *Parent, + const CXXBaseSpecifier *Specifier); + + MSRTTIClass *getFirstChild() { return this + 1; } + static MSRTTIClass *getNextChild(MSRTTIClass *Child) { + return Child + 1 + Child->NumBases; + } + + const CXXRecordDecl *RD, *VirtualRoot; + uint32_t Flags, NumBases, OffsetInVBase; +}; + +/// \brief Recursively initialize the base class array. +uint32_t MSRTTIClass::initialize(const MSRTTIClass *Parent, + const CXXBaseSpecifier *Specifier) { + Flags = HasHierarchyDescriptor; + if (!Parent) { + VirtualRoot = nullptr; + OffsetInVBase = 0; + } else { + if (Specifier->getAccessSpecifier() != AS_public) + Flags |= IsPrivate | IsPrivateOnPath; + if (Specifier->isVirtual()) { + Flags |= IsVirtual; + VirtualRoot = RD; + OffsetInVBase = 0; + } else { + if (Parent->Flags & IsPrivateOnPath) + Flags |= IsPrivateOnPath; + VirtualRoot = Parent->VirtualRoot; + OffsetInVBase = Parent->OffsetInVBase + RD->getASTContext() + .getASTRecordLayout(Parent->RD).getBaseClassOffset(RD).getQuantity(); + } + } + NumBases = 0; + MSRTTIClass *Child = getFirstChild(); + for (const CXXBaseSpecifier &Base : RD->bases()) { + NumBases += Child->initialize(this, &Base) + 1; + Child = getNextChild(Child); + } + return NumBases; +} + +static llvm::GlobalValue::LinkageTypes getLinkageForRTTI(QualType Ty) { + switch (Ty->getLinkage()) { + case NoLinkage: + case InternalLinkage: + case UniqueExternalLinkage: + return llvm::GlobalValue::InternalLinkage; + + case VisibleNoLinkage: + case ExternalLinkage: + return llvm::GlobalValue::LinkOnceODRLinkage; + } + llvm_unreachable("Invalid linkage!"); +} + +/// \brief An ephemeral helper class for building MS RTTI types. It caches some +/// calls to the module and information about the most derived class in a +/// hierarchy. +struct MSRTTIBuilder { + enum { + HasBranchingHierarchy = 1, + HasVirtualBranchingHierarchy = 2, + HasAmbiguousBases = 4 + }; + + MSRTTIBuilder(MicrosoftCXXABI &ABI, const CXXRecordDecl *RD) + : CGM(ABI.CGM), Context(CGM.getContext()), + VMContext(CGM.getLLVMContext()), Module(CGM.getModule()), RD(RD), + Linkage(getLinkageForRTTI(CGM.getContext().getTagDeclType(RD))), + ABI(ABI) {} + + llvm::GlobalVariable *getBaseClassDescriptor(const MSRTTIClass &Classes); + llvm::GlobalVariable * + getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes); + llvm::GlobalVariable *getClassHierarchyDescriptor(); + llvm::GlobalVariable *getCompleteObjectLocator(const VPtrInfo *Info); + + CodeGenModule &CGM; + ASTContext &Context; + llvm::LLVMContext &VMContext; + llvm::Module &Module; + const CXXRecordDecl *RD; + llvm::GlobalVariable::LinkageTypes Linkage; + MicrosoftCXXABI &ABI; +}; + +} // namespace + +/// \brief Recursively serializes a class hierarchy in pre-order depth first +/// order. +static void serializeClassHierarchy(SmallVectorImpl<MSRTTIClass> &Classes, + const CXXRecordDecl *RD) { + Classes.push_back(MSRTTIClass(RD)); + for (const CXXBaseSpecifier &Base : RD->bases()) + serializeClassHierarchy(Classes, Base.getType()->getAsCXXRecordDecl()); +} + +/// \brief Find ambiguity among base classes. +static void +detectAmbiguousBases(SmallVectorImpl<MSRTTIClass> &Classes) { + llvm::SmallPtrSet<const CXXRecordDecl *, 8> VirtualBases; + llvm::SmallPtrSet<const CXXRecordDecl *, 8> UniqueBases; + llvm::SmallPtrSet<const CXXRecordDecl *, 8> AmbiguousBases; + for (MSRTTIClass *Class = &Classes.front(); Class <= &Classes.back();) { + if ((Class->Flags & MSRTTIClass::IsVirtual) && + !VirtualBases.insert(Class->RD)) { + Class = MSRTTIClass::getNextChild(Class); + continue; + } + if (!UniqueBases.insert(Class->RD)) + AmbiguousBases.insert(Class->RD); + Class++; + } + if (AmbiguousBases.empty()) + return; + for (MSRTTIClass &Class : Classes) + if (AmbiguousBases.count(Class.RD)) + Class.Flags |= MSRTTIClass::IsAmbiguous; +} + +llvm::GlobalVariable *MSRTTIBuilder::getClassHierarchyDescriptor() { + SmallString<256> MangledName; + { + llvm::raw_svector_ostream Out(MangledName); + ABI.getMangleContext().mangleCXXRTTIClassHierarchyDescriptor(RD, Out); + } + + // Check to see if we've already declared this ClassHierarchyDescriptor. + if (auto CHD = Module.getNamedGlobal(MangledName)) + return CHD; + + // Serialize the class hierarchy and initialize the CHD Fields. + SmallVector<MSRTTIClass, 8> Classes; + serializeClassHierarchy(Classes, RD); + Classes.front().initialize(/*Parent=*/nullptr, /*Specifier=*/nullptr); + detectAmbiguousBases(Classes); + int Flags = 0; + for (auto Class : Classes) { + if (Class.RD->getNumBases() > 1) + Flags |= HasBranchingHierarchy; + // Note: cl.exe does not calculate "HasAmbiguousBases" correctly. We + // believe the field isn't actually used. + if (Class.Flags & MSRTTIClass::IsAmbiguous) + Flags |= HasAmbiguousBases; + } + if ((Flags & HasBranchingHierarchy) && RD->getNumVBases() != 0) + Flags |= HasVirtualBranchingHierarchy; + // These gep indices are used to get the address of the first element of the + // base class array. + llvm::Value *GEPIndices[] = {llvm::ConstantInt::get(CGM.IntTy, 0), + llvm::ConstantInt::get(CGM.IntTy, 0)}; + + // Forward-declare the class hierarchy descriptor + auto Type = ABI.getClassHierarchyDescriptorType(); + auto CHD = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage, + /*Initializer=*/nullptr, + MangledName.c_str()); + + // Initialize the base class ClassHierarchyDescriptor. + llvm::Constant *Fields[] = { + llvm::ConstantInt::get(CGM.IntTy, 0), // Unknown + llvm::ConstantInt::get(CGM.IntTy, Flags), + llvm::ConstantInt::get(CGM.IntTy, Classes.size()), + ABI.getImageRelativeConstant(llvm::ConstantExpr::getInBoundsGetElementPtr( + getBaseClassArray(Classes), + llvm::ArrayRef<llvm::Value *>(GEPIndices))), + }; + CHD->setInitializer(llvm::ConstantStruct::get(Type, Fields)); + return CHD; +} + +llvm::GlobalVariable * +MSRTTIBuilder::getBaseClassArray(SmallVectorImpl<MSRTTIClass> &Classes) { + SmallString<256> MangledName; + { + llvm::raw_svector_ostream Out(MangledName); + ABI.getMangleContext().mangleCXXRTTIBaseClassArray(RD, Out); + } + + // Forward-declare the base class array. + // cl.exe pads the base class array with 1 (in 32 bit mode) or 4 (in 64 bit + // mode) bytes of padding. We provide a pointer sized amount of padding by + // adding +1 to Classes.size(). The sections have pointer alignment and are + // marked pick-any so it shouldn't matter. + llvm::Type *PtrType = ABI.getImageRelativeType( + ABI.getBaseClassDescriptorType()->getPointerTo()); + auto *ArrType = llvm::ArrayType::get(PtrType, Classes.size() + 1); + auto *BCA = new llvm::GlobalVariable( + Module, ArrType, + /*Constant=*/true, Linkage, /*Initializer=*/nullptr, MangledName.c_str()); + + // Initialize the BaseClassArray. + SmallVector<llvm::Constant *, 8> BaseClassArrayData; + for (MSRTTIClass &Class : Classes) + BaseClassArrayData.push_back( + ABI.getImageRelativeConstant(getBaseClassDescriptor(Class))); + BaseClassArrayData.push_back(llvm::Constant::getNullValue(PtrType)); + BCA->setInitializer(llvm::ConstantArray::get(ArrType, BaseClassArrayData)); + return BCA; +} + +llvm::GlobalVariable * +MSRTTIBuilder::getBaseClassDescriptor(const MSRTTIClass &Class) { + // Compute the fields for the BaseClassDescriptor. They are computed up front + // because they are mangled into the name of the object. + uint32_t OffsetInVBTable = 0; + int32_t VBPtrOffset = -1; + if (Class.VirtualRoot) { + auto &VTableContext = CGM.getMicrosoftVTableContext(); + OffsetInVBTable = VTableContext.getVBTableIndex(RD, Class.VirtualRoot) * 4; + VBPtrOffset = Context.getASTRecordLayout(RD).getVBPtrOffset().getQuantity(); + } + + SmallString<256> MangledName; + { + llvm::raw_svector_ostream Out(MangledName); + ABI.getMangleContext().mangleCXXRTTIBaseClassDescriptor( + Class.RD, Class.OffsetInVBase, VBPtrOffset, OffsetInVBTable, + Class.Flags, Out); + } + + // Check to see if we've already declared this object. + if (auto BCD = Module.getNamedGlobal(MangledName)) + return BCD; + + // Forward-declare the base class descriptor. + auto Type = ABI.getBaseClassDescriptorType(); + auto BCD = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage, + /*Initializer=*/nullptr, + MangledName.c_str()); + + // Initialize the BaseClassDescriptor. + llvm::Constant *Fields[] = { + ABI.getImageRelativeConstant( + ABI.getAddrOfRTTIDescriptor(Context.getTypeDeclType(Class.RD))), + llvm::ConstantInt::get(CGM.IntTy, Class.NumBases), + llvm::ConstantInt::get(CGM.IntTy, Class.OffsetInVBase), + llvm::ConstantInt::get(CGM.IntTy, VBPtrOffset), + llvm::ConstantInt::get(CGM.IntTy, OffsetInVBTable), + llvm::ConstantInt::get(CGM.IntTy, Class.Flags), + ABI.getImageRelativeConstant( + MSRTTIBuilder(ABI, Class.RD).getClassHierarchyDescriptor()), + }; + BCD->setInitializer(llvm::ConstantStruct::get(Type, Fields)); + return BCD; +} + +llvm::GlobalVariable * +MSRTTIBuilder::getCompleteObjectLocator(const VPtrInfo *Info) { + SmallString<256> MangledName; + { + llvm::raw_svector_ostream Out(MangledName); + ABI.getMangleContext().mangleCXXRTTICompleteObjectLocator(RD, Info->MangledPath, Out); + } + + // Check to see if we've already computed this complete object locator. + if (auto COL = Module.getNamedGlobal(MangledName)) + return COL; + + // Compute the fields of the complete object locator. + int OffsetToTop = Info->FullOffsetInMDC.getQuantity(); + int VFPtrOffset = 0; + // The offset includes the vtordisp if one exists. + if (const CXXRecordDecl *VBase = Info->getVBaseWithVPtr()) + if (Context.getASTRecordLayout(RD) + .getVBaseOffsetsMap() + .find(VBase) + ->second.hasVtorDisp()) + VFPtrOffset = Info->NonVirtualOffset.getQuantity() + 4; + + // Forward-declare the complete object locator. + llvm::StructType *Type = ABI.getCompleteObjectLocatorType(); + auto COL = new llvm::GlobalVariable(Module, Type, /*Constant=*/true, Linkage, + /*Initializer=*/nullptr, MangledName.c_str()); + + // Initialize the CompleteObjectLocator. + llvm::Constant *Fields[] = { + llvm::ConstantInt::get(CGM.IntTy, ABI.isImageRelative()), + llvm::ConstantInt::get(CGM.IntTy, OffsetToTop), + llvm::ConstantInt::get(CGM.IntTy, VFPtrOffset), + ABI.getImageRelativeConstant( + CGM.GetAddrOfRTTIDescriptor(Context.getTypeDeclType(RD))), + ABI.getImageRelativeConstant(getClassHierarchyDescriptor()), + ABI.getImageRelativeConstant(COL), + }; + llvm::ArrayRef<llvm::Constant *> FieldsRef(Fields); + if (!ABI.isImageRelative()) + FieldsRef = FieldsRef.drop_back(); + COL->setInitializer(llvm::ConstantStruct::get(Type, FieldsRef)); + return COL; +} + +/// \brief Gets a TypeDescriptor. Returns a llvm::Constant * rather than a +/// llvm::GlobalVariable * because different type descriptors have different +/// types, and need to be abstracted. They are abstracting by casting the +/// address to an Int8PtrTy. +llvm::Constant *MicrosoftCXXABI::getAddrOfRTTIDescriptor(QualType Type) { + SmallString<256> MangledName, TypeInfoString; + { + llvm::raw_svector_ostream Out(MangledName); + getMangleContext().mangleCXXRTTI(Type, Out); + } + + // Check to see if we've already declared this TypeDescriptor. + if (llvm::GlobalVariable *GV = CGM.getModule().getNamedGlobal(MangledName)) + return llvm::ConstantExpr::getBitCast(GV, CGM.Int8PtrTy); + + // Compute the fields for the TypeDescriptor. + { + llvm::raw_svector_ostream Out(TypeInfoString); + getMangleContext().mangleCXXRTTIName(Type, Out); + } + + // Declare and initialize the TypeDescriptor. + llvm::Constant *Fields[] = { + getTypeInfoVTable(CGM), // VFPtr + llvm::ConstantPointerNull::get(CGM.Int8PtrTy), // Runtime data + llvm::ConstantDataArray::getString(CGM.getLLVMContext(), TypeInfoString)}; + llvm::StructType *TypeDescriptorType = + getTypeDescriptorType(TypeInfoString); + return llvm::ConstantExpr::getBitCast( + new llvm::GlobalVariable( + CGM.getModule(), TypeDescriptorType, /*Constant=*/false, + getLinkageForRTTI(Type), + llvm::ConstantStruct::get(TypeDescriptorType, Fields), + MangledName.c_str()), + CGM.Int8PtrTy); +} + +/// \brief Gets or a creates a Microsoft CompleteObjectLocator. +llvm::GlobalVariable * +MicrosoftCXXABI::getMSCompleteObjectLocator(const CXXRecordDecl *RD, + const VPtrInfo *Info) { + return MSRTTIBuilder(*this, RD).getCompleteObjectLocator(Info); +} |