CGVTables.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 idx,
  51. llvm::Constant *rtti,
  52. unsigned &nextVTableThunkIndex);
  53. public:
  54. /// Add vtable components for the given vtable layout to the given
  55. /// global initializer.
  56. void createVTableInitializer(ConstantStructBuilder &builder,
  57. const VTableLayout &layout,
  58. llvm::Constant *rtti);
  59. CodeGenVTables(CodeGenModule &CGM);
  60. ItaniumVTableContext &getItaniumVTableContext() {
  61. return *cast<ItaniumVTableContext>(VTContext);
  62. }
  63. MicrosoftVTableContext &getMicrosoftVTableContext() {
  64. return *cast<MicrosoftVTableContext>(VTContext);
  65. }
  66. /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
  67. /// given record decl.
  68. uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);
  69. /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the
  70. /// virtual pointer for the given subobject is located.
  71. uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
  72. BaseSubobject Base);
  73. /// GenerateConstructionVTable - Generate a construction vtable for the given
  74. /// base subobject.
  75. llvm::GlobalVariable *
  76. GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base,
  77. bool BaseIsVirtual,
  78. llvm::GlobalVariable::LinkageTypes Linkage,
  79. VTableAddressPointsMapTy& AddressPoints);
  80. /// GetAddrOfVTT - Get the address of the VTT for the given record decl.
  81. llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD);
  82. /// EmitVTTDefinition - Emit the definition of the given vtable.
  83. void EmitVTTDefinition(llvm::GlobalVariable *VTT,
  84. llvm::GlobalVariable::LinkageTypes Linkage,
  85. const CXXRecordDecl *RD);
  86. /// EmitThunks - Emit the associated thunks for the given global decl.
  87. void EmitThunks(GlobalDecl GD);
  88. /// GenerateClassData - Generate all the class data required to be
  89. /// generated upon definition of a KeyFunction. This includes the
  90. /// vtable, the RTTI data structure (if RTTI is enabled) and the VTT
  91. /// (if the class has virtual bases).
  92. void GenerateClassData(const CXXRecordDecl *RD);
  93. bool isVTableExternal(const CXXRecordDecl *RD);
  94. /// Returns the type of a vtable with the given layout. Normally a struct of
  95. /// arrays of pointers, with one struct element for each vtable in the vtable
  96. /// group.
  97. llvm::Type *getVTableType(const VTableLayout &layout);
  98. };
  99. } // end namespace CodeGen
  100. } // end namespace clang
  101. #endif