VTableSetup.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #include <vector>
  3. #include "metadata/Il2CppTypeHash.h"
  4. #include "metadata/Il2CppTypeCompare.h"
  5. #include "../CommonDef.h"
  6. #include "MetadataUtil.h"
  7. namespace hybridclr
  8. {
  9. namespace metadata
  10. {
  11. struct GenericClassMethod
  12. {
  13. const Il2CppType* type;
  14. const Il2CppMethodDefinition* method;
  15. const char* name; // TODO remove
  16. };
  17. struct VirtualMethodImpl
  18. {
  19. const Il2CppMethodDefinition* method;
  20. const Il2CppType* type;
  21. uint16_t slot;
  22. //const char* name; // TODO for debug
  23. };
  24. class VTableSetUp;
  25. struct RawInterfaceOffsetInfo
  26. {
  27. const Il2CppType* type;
  28. VTableSetUp* tree;
  29. uint32_t offset;
  30. };
  31. typedef Il2CppHashMap<const Il2CppType*, VTableSetUp*, Il2CppTypeHash, Il2CppTypeEqualTo> Il2CppType2TypeDeclaringTreeMap;
  32. class VTableSetUp
  33. {
  34. public:
  35. typedef Il2CppHashMap<int32_t, uint16_t, il2cpp::utils::PassThroughHash<int32_t>> Int32ToUin16Map;
  36. typedef Il2CppHashSet<uint16_t, il2cpp::utils::PassThroughHash<uint16_t>> Uin16Set;
  37. static VTableSetUp* BuildByType(Il2CppType2TypeDeclaringTreeMap& cache, const Il2CppType* type);
  38. static VTableSetUp* InflateVts(Il2CppType2TypeDeclaringTreeMap& cache, VTableSetUp* genericType, const Il2CppType* type);
  39. VTableSetUp()
  40. {
  41. }
  42. const VTableSetUp* GetParent() const { return _parent; }
  43. const Il2CppType* FindImplType(const Il2CppMethodDefinition* methodDef);
  44. const VTableSetUp* FindAncestorTypeTree(const Il2CppType* implType);
  45. const GenericClassMethod* FindImplMethod(const Il2CppType* containerType, const Il2CppMethodDefinition* methodDef, bool throwExceptionIfNotFind = true);
  46. const std::vector<RawInterfaceOffsetInfo>& GetInterfaceOffsetInfos() const { return _interfaceOffsetInfos; }
  47. const std::vector<VirtualMethodImpl>& GetVirtualMethodImpls() const { return _methodImpls; }
  48. const Il2CppType* GetType() const { return _type; }
  49. uint32_t GetTypeIndex() const { return _typeDef->byvalTypeIndex; }
  50. bool IsInterType() const { return hybridclr::metadata::IsInterpreterType(_typeDef); }
  51. private:
  52. void ComputeVtables(Il2CppType2TypeDeclaringTreeMap& cache);
  53. void ComputAotTypeVtables(Il2CppType2TypeDeclaringTreeMap& cache);
  54. void InitInterfaceVTable(uint16_t& curOffset, std::vector<uint16_t>& implInterfaceOffsetIdxs);
  55. void ComputeExplicitImpls(const std::vector<uint16_t>& implInterfaceOffsetIdxs, Int32ToUin16Map& explicitImplToken2Slots);
  56. void ApplyTypeExplicitImpls(const Il2CppType* type, const VTableSetUp* tree, const std::vector<uint16_t>& implInterfaceOffsetIdxs, Int32ToUin16Map& explicitImplToken2Slots);
  57. void ComputeOverrideParentVirtualMethod(uint16_t& curOffset, const std::vector<uint16_t>& implInterfaceOffsetIdxs, Int32ToUin16Map& explicitImplToken2Slots);
  58. void ComputeInterfaceOverrideByParentVirtualMethod(const std::vector<uint16_t>& implInterfaceOffsetIdxs);
  59. void ComputeInterpTypeVtables(Il2CppType2TypeDeclaringTreeMap& cache);
  60. void ComputeInterfaceVtables(Il2CppType2TypeDeclaringTreeMap& cache);
  61. bool isExplicitImplInterfaceSlot(uint16_t slot) const { return _explicitImplSlots.find(slot) != _explicitImplSlots.end(); }
  62. uint16_t FindDefaultOverrideExplicitInterfaceSlot(GenericClassMethod& gcm, const Uin16Set& explicitImplSlots, const std::vector<uint16_t>& implInterfaceOffsetIdxs);
  63. uint16_t FindExplicitOverrideInterfaceSlot(GenericClassMethod& gcm, const Int32ToUin16Map& explicitImplSlots);
  64. void ApplyOverrideMethod(const GenericClassMethod* beOverrideParentMethod, const Il2CppMethodDefinition* overrideMethodDef, uint16_t checkOverrideMaxIdx);
  65. void ApplyExplicitOverride(const std::vector<uint16_t>& implInterfaceOffsetIdxs, Int32ToUin16Map& explicitImplToken2Slots, const Il2CppType* declaringType,
  66. const Il2CppMethodDefinition* decalringMethod, const Il2CppType* implType, const Il2CppMethodDefinition* implMethod);
  67. void ApplyAOTInterfaceExplicitOverride(const std::vector<uint16_t>& implInterfaceOffsetIdxs, Int32ToUin16Map& explicitImplToken2Slots, const Il2CppType* intfType,
  68. const Il2CppType* implType, const Il2CppMethodDefinition* implMethod);
  69. VTableSetUp* _parent;
  70. std::vector<VTableSetUp*> _interfaces;
  71. std::vector<RawInterfaceOffsetInfo> _interfaceOffsetInfos;
  72. const Il2CppType* _type;
  73. const Il2CppTypeDefinition* _typeDef;
  74. const char* _name; // TODO remove
  75. std::vector<GenericClassMethod> _virtualMethods;
  76. std::vector<VirtualMethodImpl> _methodImpls;
  77. Uin16Set _explicitImplSlots;;
  78. };
  79. }
  80. }