CGVTables.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //===--- CGVTables.h - Emit LLVM Code for C++ vtables -----------*- C++ -*-===//
  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. #ifndef LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H
  13. #define LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H
  14. #include "clang/AST/BaseSubobject.h"
  15. #include "clang/AST/CharUnits.h"
  16. #include "clang/AST/GlobalDecl.h"
  17. #include "clang/AST/VTableBuilder.h"
  18. #include "clang/Basic/ABI.h"
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/IR/GlobalVariable.h"
  21. namespace clang {
  22. class CXXRecordDecl;
  23. namespace CodeGen {
  24. class CodeGenModule;
  25. class ConstantArrayBuilder;
  26. class ConstantStructBuilder;
  27. class CodeGenVTables {
  28. CodeGenModule &CGM;
  29. VTableContextBase *VTContext;
  30. /// VTableAddressPointsMapTy - Address points for a single vtable.
  31. typedef VTableLayout::AddressPointsMapTy VTableAddressPointsMapTy;
  32. typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy;
  33. typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy;
  34. /// SubVTTIndicies - Contains indices into the various sub-VTTs.
  35. SubVTTIndiciesMapTy SubVTTIndicies;
  36. typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t>
  37. SecondaryVirtualPointerIndicesMapTy;
  38. /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer
  39. /// indices.
  40. SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices;
  41. /// Cache for the pure virtual member call function.
  42. llvm::Constant *PureVirtualFn = nullptr;
  43. /// Cache for the deleted virtual member call function.
  44. llvm::Constant *DeletedVirtualFn = nullptr;
  45. /// Get the address of a thunk and emit it if necessary.
  46. llvm::Constant *maybeEmitThunk(GlobalDecl GD,
  47. const ThunkInfo &ThunkAdjustments,
  48. bool ForVTable);
  49. void addVTableComponent(ConstantArrayBuilder &builder,
  50. const VTableLayout &layout, unsigned componentIndex,
  51. llvm::Constant *rtti, unsigned &nextVTableThunkIndex,
  52. unsigned vtableAddressPoint,
  53. bool vtableHasLocalLinkage);
  54. /// Add a 32-bit offset to a component relative to the vtable when using the
  55. /// relative vtables ABI. The array builder points to the start of the vtable.
  56. void addRelativeComponent(ConstantArrayBuilder &builder,
  57. llvm::Constant *component,
  58. unsigned vtableAddressPoint,
  59. bool vtableHasLocalLinkage,
  60. bool isCompleteDtor) const;
  61. /// Create a dso_local stub that will be used for a relative reference in the
  62. /// relative vtable layout. This stub will just be a tail call to the original
  63. /// function and propagate any function attributes from the original. If the
  64. /// original function is already dso_local, the original is returned instead
  65. /// and a stub is not created.
  66. llvm::Function *
  67. getOrCreateRelativeStub(llvm::Function *func,
  68. llvm::GlobalValue::LinkageTypes stubLinkage,
  69. bool isCompleteDtor) const;
  70. bool useRelativeLayout() const;
  71. llvm::Type *getVTableComponentType() const;
  72. public:
  73. /// Add vtable components for the given vtable layout to the given
  74. /// global initializer.
  75. void createVTableInitializer(ConstantStructBuilder &builder,
  76. const VTableLayout &layout, llvm::Constant *rtti,
  77. bool vtableHasLocalLinkage);
  78. CodeGenVTables(CodeGenModule &CGM);
  79. ItaniumVTableContext &getItaniumVTableContext() {
  80. return *cast<ItaniumVTableContext>(VTContext);
  81. }
  82. MicrosoftVTableContext &getMicrosoftVTableContext() {
  83. return *cast<MicrosoftVTableContext>(VTContext);
  84. }
  85. /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
  86. /// given record decl.
  87. uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);
  88. /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the
  89. /// virtual pointer for the given subobject is located.
  90. uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
  91. BaseSubobject Base);
  92. /// GenerateConstructionVTable - Generate a construction vtable for the given
  93. /// base subobject.
  94. llvm::GlobalVariable *
  95. GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base,
  96. bool BaseIsVirtual,
  97. llvm::GlobalVariable::LinkageTypes Linkage,
  98. VTableAddressPointsMapTy& AddressPoints);
  99. /// GetAddrOfVTT - Get the address of the VTT for the given record decl.
  100. llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD);
  101. /// EmitVTTDefinition - Emit the definition of the given vtable.
  102. void EmitVTTDefinition(llvm::GlobalVariable *VTT,
  103. llvm::GlobalVariable::LinkageTypes Linkage,
  104. const CXXRecordDecl *RD);
  105. /// EmitThunks - Emit the associated thunks for the given global decl.
  106. void EmitThunks(GlobalDecl GD);
  107. /// GenerateClassData - Generate all the class data required to be
  108. /// generated upon definition of a KeyFunction. This includes the
  109. /// vtable, the RTTI data structure (if RTTI is enabled) and the VTT
  110. /// (if the class has virtual bases).
  111. void GenerateClassData(const CXXRecordDecl *RD);
  112. bool isVTableExternal(const CXXRecordDecl *RD);
  113. /// Returns the type of a vtable with the given layout. Normally a struct of
  114. /// arrays of pointers, with one struct element for each vtable in the vtable
  115. /// group.
  116. llvm::Type *getVTableType(const VTableLayout &layout);
  117. /// Generate a public facing alias for the vtable and make the vtable either
  118. /// hidden or private. The alias will have the original linkage and visibility
  119. /// of the vtable. This is used for cases under the relative vtables ABI
  120. /// when a vtable may not be dso_local.
  121. void GenerateRelativeVTableAlias(llvm::GlobalVariable *VTable,
  122. llvm::StringRef AliasNameRef);
  123. };
  124. } // end namespace CodeGen
  125. } // end namespace clang
  126. #endif