| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This contains code dealing with C++ code generation of virtual tables. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef CLANG_LIB_CIR_CODEGEN_CIRGENVTABLES_H |
| 14 | #define CLANG_LIB_CIR_CODEGEN_CIRGENVTABLES_H |
| 15 | |
| 16 | #include "mlir/IR/Types.h" |
| 17 | #include "clang/AST/GlobalDecl.h" |
| 18 | #include "clang/AST/VTableBuilder.h" |
| 19 | #include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 20 | |
| 21 | namespace clang { |
| 22 | class CXXRecordDecl; |
| 23 | } |
| 24 | |
| 25 | namespace clang::CIRGen { |
| 26 | class CIRGenModule; |
| 27 | |
| 28 | class CIRGenVTables { |
| 29 | CIRGenModule &cgm; |
| 30 | |
| 31 | clang::VTableContextBase *vtContext; |
| 32 | |
| 33 | /// Address points for a single vtable. |
| 34 | using VTableAddressPointsMapTy = clang::VTableLayout::AddressPointsMapTy; |
| 35 | using BaseSubobjectPairTy = |
| 36 | std::pair<const clang::CXXRecordDecl *, clang::BaseSubobject>; |
| 37 | using SubVTTIndiciesMapTy = llvm::DenseMap<BaseSubobjectPairTy, uint64_t>; |
| 38 | |
| 39 | /// Contains indices into the various sub-VTTs. |
| 40 | SubVTTIndiciesMapTy subVTTIndicies; |
| 41 | |
| 42 | using SecondaryVirtualPointerIndicesMapTy = |
| 43 | llvm::DenseMap<BaseSubobjectPairTy, uint64_t>; |
| 44 | |
| 45 | /// Contains the secondary virtual pointer |
| 46 | /// indices. |
| 47 | SecondaryVirtualPointerIndicesMapTy secondaryVirtualPointerIndices; |
| 48 | |
| 49 | /// Cache for the pure virtual member call function. |
| 50 | cir::FuncOp pureVirtualFn = nullptr; |
| 51 | /// Cache for the deleted virtual member call function. |
| 52 | cir::FuncOp deletedVirtualFn = nullptr; |
| 53 | |
| 54 | mlir::Attribute |
| 55 | getVTableComponent(const VTableLayout &layout, unsigned componentIndex, |
| 56 | mlir::Attribute rtti, unsigned &nextVTableThunkIndex, |
| 57 | unsigned vtableAddressPoint, bool vtableHasLocalLinkage); |
| 58 | |
| 59 | mlir::Type getVTableComponentType(); |
| 60 | |
| 61 | public: |
| 62 | CIRGenVTables(CIRGenModule &cgm); |
| 63 | |
| 64 | /// Add vtable components for the given vtable layout to the given |
| 65 | /// global initializer. |
| 66 | void createVTableInitializer(cir::GlobalOp &vtable, |
| 67 | const clang::VTableLayout &layout, |
| 68 | mlir::Attribute rtti, |
| 69 | bool vtableHasLocalLinkage); |
| 70 | |
| 71 | clang::ItaniumVTableContext &getItaniumVTableContext() { |
| 72 | return *llvm::cast<clang::ItaniumVTableContext>(Val: vtContext); |
| 73 | } |
| 74 | |
| 75 | const clang::ItaniumVTableContext &getItaniumVTableContext() const { |
| 76 | return *llvm::cast<clang::ItaniumVTableContext>(Val: vtContext); |
| 77 | } |
| 78 | |
| 79 | /// Generate a construction vtable for the given base subobject. |
| 80 | cir::GlobalOp |
| 81 | generateConstructionVTable(const CXXRecordDecl *rd, const BaseSubobject &base, |
| 82 | bool baseIsVirtual, cir::GlobalLinkageKind linkage, |
| 83 | VTableAddressPointsMapTy &addressPoints); |
| 84 | |
| 85 | /// Get the address of the VTT for the given record decl. |
| 86 | cir::GlobalOp getAddrOfVTT(const CXXRecordDecl *rd); |
| 87 | |
| 88 | /// Emit the definition of the given vtable. |
| 89 | void emitVTTDefinition(cir::GlobalOp vttOp, cir::GlobalLinkageKind linkage, |
| 90 | const CXXRecordDecl *rd); |
| 91 | /// Return the index of the sub-VTT for the base class of the given record |
| 92 | /// decl. |
| 93 | uint64_t getSubVTTIndex(const CXXRecordDecl *rd, BaseSubobject base); |
| 94 | |
| 95 | /// Return the index in the VTT where the virtual pointer for the given |
| 96 | /// subobject is located. |
| 97 | uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *rd, |
| 98 | BaseSubobject base); |
| 99 | |
| 100 | /// Emit the associated thunks for the given global decl. |
| 101 | void emitThunks(GlobalDecl gd); |
| 102 | |
| 103 | /// Emit a thunk for the given global decl if needed, or return an existing |
| 104 | /// thunk. |
| 105 | cir::FuncOp maybeEmitThunk(GlobalDecl gd, const ThunkInfo &thunkAdjustments, |
| 106 | bool forVTable); |
| 107 | |
| 108 | /// Generate all the class data required to be generated upon definition of a |
| 109 | /// KeyFunction. This includes the vtable, the RTTI data structure (if RTTI |
| 110 | /// is enabled) and the VTT (if the class has virtual bases). |
| 111 | void generateClassData(const CXXRecordDecl *rd); |
| 112 | |
| 113 | bool isVTableExternal(const clang::CXXRecordDecl *rd); |
| 114 | |
| 115 | /// Returns the type of a vtable with the given layout. Normally a struct of |
| 116 | /// arrays of pointers, with one struct element for each vtable in the vtable |
| 117 | /// group. |
| 118 | cir::RecordType getVTableType(const clang::VTableLayout &layout); |
| 119 | }; |
| 120 | |
| 121 | } // namespace clang::CIRGen |
| 122 | |
| 123 | #endif // CLANG_LIB_CIR_CODEGEN_CIRGENVTABLES_H |
| 124 | |