diff options
Diffstat (limited to 'include/clang/AST/VTableBuilder.h')
-rw-r--r-- | include/clang/AST/VTableBuilder.h | 301 |
1 files changed, 222 insertions, 79 deletions
diff --git a/include/clang/AST/VTableBuilder.h b/include/clang/AST/VTableBuilder.h index bcbe875..4e45132 100644 --- a/include/clang/AST/VTableBuilder.h +++ b/include/clang/AST/VTableBuilder.h @@ -20,12 +20,13 @@ #include "clang/AST/RecordLayout.h" #include "clang/Basic/ABI.h" #include "llvm/ADT/SetVector.h" +#include "llvm/ADT/DenseSet.h" #include <utility> namespace clang { class CXXRecordDecl; -/// VTableComponent - Represents a single component in a vtable. +/// \brief Represents a single component in a vtable. class VTableComponent { public: enum Kind { @@ -35,15 +36,17 @@ public: CK_RTTI, CK_FunctionPointer, - /// CK_CompleteDtorPointer - A pointer to the complete destructor. + /// \brief A pointer to the complete destructor. CK_CompleteDtorPointer, - /// CK_DeletingDtorPointer - A pointer to the deleting destructor. + /// \brief A pointer to the deleting destructor. CK_DeletingDtorPointer, - /// CK_UnusedFunctionPointer - In some cases, a vtable function pointer - /// will end up never being called. Such vtable function pointers are - /// represented as a CK_UnusedFunctionPointer. + /// \brief An entry that is never used. + /// + /// In some cases, a vtable function pointer will end up never being + /// called. Such vtable function pointers are represented as a + /// CK_UnusedFunctionPointer. CK_UnusedFunctionPointer }; @@ -94,7 +97,7 @@ public: return VTableComponent(I); } - /// getKind - Get the kind of this vtable component. + /// \brief Get the kind of this vtable component. Kind getKind() const { return (Kind)(Value & 0x7); } @@ -189,7 +192,7 @@ private: /// The kind is stored in the lower 3 bits of the value. For offsets, we /// make use of the facts that classes can't be larger than 2^55 bytes, - /// so we store the offset in the lower part of the 61 bytes that remain. + /// so we store the offset in the lower part of the 61 bits that remain. /// (The reason that we're not simply using a PointerIntPair here is that we /// need the offsets to be 64-bit, even when on a 32-bit machine). int64_t Value; @@ -198,7 +201,6 @@ private: class VTableLayout { public: typedef std::pair<uint64_t, ThunkInfo> VTableThunkTy; - typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy; typedef const VTableComponent *vtable_component_iterator; typedef const VTableThunkTy *vtable_thunk_iterator; @@ -208,11 +210,11 @@ private: uint64_t NumVTableComponents; llvm::OwningArrayPtr<VTableComponent> VTableComponents; - /// VTableThunks - Contains thunks needed by vtables. + /// \brief Contains thunks needed by vtables, sorted by indices. uint64_t NumVTableThunks; llvm::OwningArrayPtr<VTableThunkTy> VTableThunks; - /// Address points - Address points for all vtables. + /// \brief Address points for all vtables. AddressPointsMapTy AddressPoints; bool IsMicrosoftABI; @@ -231,23 +233,21 @@ public: } vtable_component_iterator vtable_component_begin() const { - return VTableComponents.get(); + return VTableComponents.get(); } vtable_component_iterator vtable_component_end() const { - return VTableComponents.get()+NumVTableComponents; + return VTableComponents.get() + NumVTableComponents; } - uint64_t getNumVTableThunks() const { - return NumVTableThunks; - } + uint64_t getNumVTableThunks() const { return NumVTableThunks; } vtable_thunk_iterator vtable_thunk_begin() const { - return VTableThunks.get(); + return VTableThunks.get(); } vtable_thunk_iterator vtable_thunk_end() const { - return VTableThunks.get()+NumVTableThunks; + return VTableThunks.get() + NumVTableThunks; } uint64_t getAddressPoint(BaseSubobject Base) const { @@ -266,19 +266,45 @@ public: } }; -class VTableContext { - ASTContext &Context; - +class VTableContextBase { public: - typedef SmallVector<std::pair<uint64_t, ThunkInfo>, 1> - VTableThunksTy; typedef SmallVector<ThunkInfo, 1> ThunkInfoVectorTy; +protected: + typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy; + + /// \brief Contains all thunks that a given method decl will need. + ThunksMapTy Thunks; + + /// Compute and store all vtable related information (vtable layout, vbase + /// offset offsets, thunks etc) for the given record decl. + virtual void computeVTableRelatedInformation(const CXXRecordDecl *RD) = 0; + + virtual ~VTableContextBase() {} + +public: + virtual const ThunkInfoVectorTy *getThunkInfo(GlobalDecl GD) { + const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()->getCanonicalDecl()); + computeVTableRelatedInformation(MD->getParent()); + + // This assumes that all the destructors present in the vtable + // use exactly the same set of thunks. + ThunksMapTy::const_iterator I = Thunks.find(MD); + if (I == Thunks.end()) { + // We did not find a thunk for this method. + return 0; + } + + return &I->second; + } +}; + +class ItaniumVTableContext : public VTableContextBase { private: bool IsMicrosoftABI; - /// MethodVTableIndices - Contains the index (relative to the vtable address - /// point) where the function pointer for a virtual function is stored. + /// \brief Contains the index (relative to the vtable address point) + /// where the function pointer for a virtual function is stored. typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVTableIndicesTy; MethodVTableIndicesTy MethodVTableIndices; @@ -286,49 +312,25 @@ private: VTableLayoutMapTy; VTableLayoutMapTy VTableLayouts; - /// NumVirtualFunctionPointers - Contains the number of virtual function - /// pointers in the vtable for a given record decl. - llvm::DenseMap<const CXXRecordDecl *, uint64_t> NumVirtualFunctionPointers; - typedef std::pair<const CXXRecordDecl *, const CXXRecordDecl *> ClassPairTy; - /// VirtualBaseClassOffsetOffsets - Contains the vtable offset (relative to - /// the address point) in chars where the offsets for virtual bases of a class - /// are stored. + /// \brief vtable offsets for offsets of virtual bases of a class. + /// + /// Contains the vtable offset (relative to the address point) in chars + /// where the offsets for virtual bases of a class are stored. typedef llvm::DenseMap<ClassPairTy, CharUnits> VirtualBaseClassOffsetOffsetsMapTy; VirtualBaseClassOffsetOffsetsMapTy VirtualBaseClassOffsetOffsets; - typedef llvm::DenseMap<const CXXMethodDecl *, ThunkInfoVectorTy> ThunksMapTy; - - /// Thunks - Contains all thunks that a given method decl will need. - ThunksMapTy Thunks; - - void ComputeMethodVTableIndices(const CXXRecordDecl *RD); - - /// ComputeVTableRelatedInformation - Compute and store all vtable related - /// information (vtable layout, vbase offset offsets, thunks etc) for the - /// given record decl. - void ComputeVTableRelatedInformation(const CXXRecordDecl *RD); - - /// ErrorUnsupported - Print out an error that the v-table layout code - /// doesn't support the particular C++ feature yet. - void ErrorUnsupported(StringRef Feature, SourceLocation Location); + void computeVTableRelatedInformation(const CXXRecordDecl *RD); public: - VTableContext(ASTContext &Context); - ~VTableContext(); - - bool isMicrosoftABI() const { - // FIXME: Currently, this method is only used in the VTableContext and - // VTableBuilder code which is ABI-specific. Probably we can remove it - // when we add a layer of abstraction for vtable generation. - return IsMicrosoftABI; - } + ItaniumVTableContext(ASTContext &Context); + ~ItaniumVTableContext(); const VTableLayout &getVTableLayout(const CXXRecordDecl *RD) { - ComputeVTableRelatedInformation(RD); + computeVTableRelatedInformation(RD); assert(VTableLayouts.count(RD) && "No layout for this record decl!"); return *VTableLayouts[RD]; @@ -340,36 +342,177 @@ public: bool MostDerivedClassIsVirtual, const CXXRecordDecl *LayoutClass); - const ThunkInfoVectorTy *getThunkInfo(const CXXMethodDecl *MD) { - ComputeVTableRelatedInformation(MD->getParent()); + /// \brief Locate a virtual function in the vtable. + /// + /// Return the index (relative to the vtable address point) where the + /// function pointer for the given virtual function is stored. + uint64_t getMethodVTableIndex(GlobalDecl GD); - ThunksMapTy::const_iterator I = Thunks.find(MD); - if (I == Thunks.end()) { - // We did not find a thunk for this method. - return 0; - } + /// Return the offset in chars (relative to the vtable address point) where + /// the offset of the virtual base that contains the given base is stored, + /// otherwise, if no virtual base contains the given class, return 0. + /// + /// Base must be a virtual base class or an unambiguous base. + CharUnits getVirtualBaseOffsetOffset(const CXXRecordDecl *RD, + const CXXRecordDecl *VBase); +}; - return &I->second; +struct VFPtrInfo { + typedef SmallVector<const CXXRecordDecl *, 1> BasePath; + + // Don't pass the PathToMangle as it should be calculated later. + VFPtrInfo(CharUnits VFPtrOffset, const BasePath &PathToBaseWithVFPtr) + : VBTableIndex(0), LastVBase(0), VFPtrOffset(VFPtrOffset), + PathToBaseWithVFPtr(PathToBaseWithVFPtr), VFPtrFullOffset(VFPtrOffset) { } - /// getNumVirtualFunctionPointers - Return the number of virtual function - /// pointers in the vtable for a given record decl. - uint64_t getNumVirtualFunctionPointers(const CXXRecordDecl *RD); + // Don't pass the PathToMangle as it should be calculated later. + VFPtrInfo(uint64_t VBTableIndex, const CXXRecordDecl *LastVBase, + CharUnits VFPtrOffset, const BasePath &PathToBaseWithVFPtr, + CharUnits VFPtrFullOffset) + : VBTableIndex(VBTableIndex), LastVBase(LastVBase), + VFPtrOffset(VFPtrOffset), PathToBaseWithVFPtr(PathToBaseWithVFPtr), + VFPtrFullOffset(VFPtrFullOffset) { + assert(VBTableIndex && "The full constructor should only be used " + "for vfptrs in virtual bases"); + assert(LastVBase); + } - /// getMethodVTableIndex - Return the index (relative to the vtable address - /// point) where the function pointer for the given virtual function is - /// stored. - uint64_t getMethodVTableIndex(GlobalDecl GD); + /// If nonzero, holds the vbtable index of the virtual base with the vfptr. + uint64_t VBTableIndex; - /// getVirtualBaseOffsetOffset - Return the offset in chars (relative to the - /// vtable address point) where the offset of the virtual base that contains - /// the given base is stored, otherwise, if no virtual base contains the given - /// class, return 0. Base must be a virtual base class or an unambigious - /// base. - CharUnits getVirtualBaseOffsetOffset(const CXXRecordDecl *RD, - const CXXRecordDecl *VBase); + /// Stores the last vbase on the path from the complete type to the vfptr. + const CXXRecordDecl *LastVBase; + + /// This is the offset of the vfptr from the start of the last vbase, + /// or the complete type if there are no virtual bases. + CharUnits VFPtrOffset; + + /// This holds the base classes path from the complete type to the first base + /// with the given vfptr offset, in the base-to-derived order. + BasePath PathToBaseWithVFPtr; + + /// This holds the subset of records that need to be mangled into the vftable + /// symbol name in order to get a unique name, in the derived-to-base order. + BasePath PathToMangle; + + /// This is the full offset of the vfptr from the start of the complete type. + CharUnits VFPtrFullOffset; }; +class MicrosoftVTableContext : public VTableContextBase { +public: + struct MethodVFTableLocation { + /// If nonzero, holds the vbtable index of the virtual base with the vfptr. + uint64_t VBTableIndex; + + /// If nonnull, holds the last vbase which contains the vfptr that the + /// method definition is adjusted to. + const CXXRecordDecl *VBase; + + /// This is the offset of the vfptr from the start of the last vbase, or the + /// complete type if there are no virtual bases. + CharUnits VFPtrOffset; + + /// Method's index in the vftable. + uint64_t Index; + + MethodVFTableLocation() + : VBTableIndex(0), VBase(0), VFPtrOffset(CharUnits::Zero()), + Index(0) {} + + MethodVFTableLocation(uint64_t VBTableIndex, const CXXRecordDecl *VBase, + CharUnits VFPtrOffset, uint64_t Index) + : VBTableIndex(VBTableIndex), VBase(VBase), + VFPtrOffset(VFPtrOffset), Index(Index) {} + + bool operator<(const MethodVFTableLocation &other) const { + if (VBTableIndex != other.VBTableIndex) { + assert(VBase != other.VBase); + return VBTableIndex < other.VBTableIndex; + } + if (VFPtrOffset != other.VFPtrOffset) + return VFPtrOffset < other.VFPtrOffset; + if (Index != other.Index) + return Index < other.Index; + return false; + } + }; + + typedef SmallVector<VFPtrInfo, 1> VFPtrListTy; + +private: + ASTContext &Context; + + typedef llvm::DenseMap<GlobalDecl, MethodVFTableLocation> + MethodVFTableLocationsTy; + MethodVFTableLocationsTy MethodVFTableLocations; + + typedef llvm::DenseMap<const CXXRecordDecl *, VFPtrListTy> + VFPtrLocationsMapTy; + VFPtrLocationsMapTy VFPtrLocations; + + typedef std::pair<const CXXRecordDecl *, CharUnits> VFTableIdTy; + typedef llvm::DenseMap<VFTableIdTy, const VTableLayout *> VFTableLayoutMapTy; + VFTableLayoutMapTy VFTableLayouts; + + typedef llvm::SmallSetVector<const CXXRecordDecl *, 8> BasesSetVectorTy; + void enumerateVFPtrs(const CXXRecordDecl *MostDerivedClass, + const ASTRecordLayout &MostDerivedClassLayout, + BaseSubobject Base, const CXXRecordDecl *LastVBase, + const VFPtrInfo::BasePath &PathFromCompleteClass, + BasesSetVectorTy &VisitedVBases, + MicrosoftVTableContext::VFPtrListTy &Result); + + void enumerateVFPtrs(const CXXRecordDecl *ForClass, + MicrosoftVTableContext::VFPtrListTy &Result); + + void computeVTableRelatedInformation(const CXXRecordDecl *RD); + + void dumpMethodLocations(const CXXRecordDecl *RD, + const MethodVFTableLocationsTy &NewMethods, + raw_ostream &); + + typedef std::pair<const CXXRecordDecl *, const CXXRecordDecl *> ClassPairTy; + typedef llvm::DenseMap<ClassPairTy, unsigned> VBTableIndicesTy; + VBTableIndicesTy VBTableIndices; + llvm::DenseSet<const CXXRecordDecl *> ComputedVBTableIndices; + + void computeVBTableRelatedInformation(const CXXRecordDecl *RD); + +public: + MicrosoftVTableContext(ASTContext &Context) : Context(Context) {} + + ~MicrosoftVTableContext() { llvm::DeleteContainerSeconds(VFTableLayouts); } + + const VFPtrListTy &getVFPtrOffsets(const CXXRecordDecl *RD); + + const VTableLayout &getVFTableLayout(const CXXRecordDecl *RD, + CharUnits VFPtrOffset); + + const MethodVFTableLocation &getMethodVFTableLocation(GlobalDecl GD); + + const ThunkInfoVectorTy *getThunkInfo(GlobalDecl GD) { + // Complete destructors don't have a slot in a vftable, so no thunks needed. + if (isa<CXXDestructorDecl>(GD.getDecl()) && + GD.getDtorType() == Dtor_Complete) + return 0; + return VTableContextBase::getThunkInfo(GD); + } + + /// \brief Returns the index of VBase in the vbtable of Derived. + /// VBase must be a morally virtual base of Derived. + /// The vbtable is an array of i32 offsets. The first entry is a self entry, + /// and the rest are offsets from the vbptr to virtual bases. + unsigned getVBTableIndex(const CXXRecordDecl *Derived, + const CXXRecordDecl *VBase) { + computeVBTableRelatedInformation(Derived); + ClassPairTy Pair(Derived, VBase); + assert(VBTableIndices.count(Pair) == 1 && + "VBase must be a vbase of Derived"); + return VBTableIndices[Pair]; + } +}; } #endif |