Class.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #pragma once
  2. #include <stdint.h>
  3. #include "il2cpp-config.h"
  4. #include "il2cpp-blob.h"
  5. #include "il2cpp-class-internals.h"
  6. #include "metadata/Il2CppTypeVector.h"
  7. #include "utils/dynamic_array.h"
  8. #include "il2cpp-class-internals.h"
  9. #include "il2cpp-object-internals.h"
  10. #include "Exception.h"
  11. #include "Type.h"
  12. #include "vm/MetadataCache.h"
  13. #include "il2cpp-tabledefs.h"
  14. struct Il2CppClass;
  15. struct EventInfo;
  16. struct FieldInfo;
  17. struct PropertyInfo;
  18. struct MethodInfo;
  19. struct Il2CppImage;
  20. struct Il2CppReflectionType;
  21. struct Il2CppType;
  22. struct Il2CppGenericContainer;
  23. struct Il2CppGenericContext;
  24. struct Il2CppGenericParameter;
  25. namespace il2cpp
  26. {
  27. namespace vm
  28. {
  29. class TypeNameParseInfo;
  30. enum TypeSearchFlags
  31. {
  32. kTypeSearchFlagNone = 0x0,
  33. kTypeSearchFlagIgnoreCase = 0x1,
  34. kTypeSearchFlagThrowOnError = 0x2,
  35. kTypeSearchFlagDontUseExecutingImage = 0x4
  36. };
  37. class LIBIL2CPP_CODEGEN_API Class
  38. {
  39. public:
  40. static Il2CppClass* FromIl2CppType(const Il2CppType* type);
  41. static Il2CppClass* FromName(const Il2CppImage* image, const char* namespaze, const char *name);
  42. static Il2CppClass* FromSystemType(Il2CppReflectionType *type);
  43. static Il2CppClass* FromGenericParameter(const Il2CppGenericParameter *param);
  44. static Il2CppClass* GetElementClass(Il2CppClass *klass);
  45. static const Il2CppType* GetEnumBaseType(Il2CppClass *klass);
  46. static const EventInfo* GetEvents(Il2CppClass *klass, void* *iter);
  47. static FieldInfo* GetFields(Il2CppClass *klass, void* *iter);
  48. static FieldInfo* GetFieldFromName(Il2CppClass *klass, const char* name);
  49. static const MethodInfo* GetFinalizer(Il2CppClass *klass);
  50. static int32_t GetInstanceSize(const Il2CppClass *klass);
  51. static Il2CppClass* GetInterfaces(Il2CppClass *klass, void* *iter);
  52. static const MethodInfo* GetMethods(Il2CppClass *klass, void* *iter);
  53. static const MethodInfo* GetMethodFromName(Il2CppClass *klass, const char* name, int argsCount);
  54. static const MethodInfo* GetMethodFromNameFlags(Il2CppClass *klass, const char* name, int argsCount, int32_t flags);
  55. static const char* GetName(Il2CppClass *klass);
  56. static const char* GetNamespace(Il2CppClass *klass);
  57. static Il2CppClass* GetNestedTypes(Il2CppClass *klass, void* *iter);
  58. static size_t GetNumMethods(const Il2CppClass* klass);
  59. static size_t GetNumProperties(const Il2CppClass* klass);
  60. static size_t GetNumFields(const Il2CppClass* klass);
  61. static Il2CppClass* GetParent(Il2CppClass *klass);
  62. static const PropertyInfo* GetProperties(Il2CppClass *klass, void* *iter);
  63. static const PropertyInfo* GetPropertyFromName(Il2CppClass *klass, const char* name);
  64. static int32_t GetValueSize(Il2CppClass *klass, uint32_t *align);
  65. //for Unsafe, but more performant version of HasParent, see ClassInlines.h
  66. static bool HasParent(Il2CppClass *klass, Il2CppClass *parent);
  67. static bool IsAssignableFrom(Il2CppClass *klass, Il2CppClass *oklass);
  68. static bool IsAssignableFrom(Il2CppReflectionType *klass, Il2CppReflectionType *oklass);
  69. static bool IsGeneric(const Il2CppClass *klass);
  70. static bool IsInflated(const Il2CppClass *klass);
  71. static bool IsSubclassOf(Il2CppClass *klass, Il2CppClass *klassc, bool check_interfaces);
  72. static bool IsValuetype(const Il2CppClass *klass);
  73. static bool IsBlittable(const Il2CppClass *klass);
  74. static bool HasDefaultConstructor(Il2CppClass* klass);
  75. static int GetFlags(const Il2CppClass *klass);
  76. static bool IsAbstract(const Il2CppClass *klass);
  77. static bool IsInterface(const Il2CppClass *klass);
  78. static bool IsNullable(const Il2CppClass *klass);
  79. static Il2CppClass* GetNullableArgument(const Il2CppClass* klass);
  80. static int GetArrayElementSize(const Il2CppClass *klass);
  81. static const Il2CppType* GetByrefType(Il2CppClass *klass);
  82. static const Il2CppType* GetType(Il2CppClass *klass);
  83. static const Il2CppType* GetType(Il2CppClass *klass, const TypeNameParseInfo &info);
  84. static bool HasAttribute(Il2CppClass *klass, Il2CppClass *attr_class);
  85. static bool IsEnum(const Il2CppClass *klass);
  86. static const Il2CppImage* GetImage(Il2CppClass* klass);
  87. static const char *GetAssemblyName(const Il2CppClass *klass);
  88. static const char *GetAssemblyNameNoExtension(const Il2CppClass *klass);
  89. static const int IgnoreNumberOfArguments;
  90. public:
  91. static bool Init(Il2CppClass *klass);
  92. static Il2CppClass* GetArrayClass(Il2CppClass *element_class, uint32_t rank);
  93. static Il2CppClass* GetBoundedArrayClass(Il2CppClass *element_class, uint32_t rank, bool bounded);
  94. static Il2CppClass* GetInflatedGenericInstanceClass(Il2CppClass* klass, const metadata::Il2CppTypeVector& types);
  95. static Il2CppClass* GetInflatedGenericInstanceClass(Il2CppClass* klass, const Il2CppGenericInst* genericInst);
  96. static Il2CppClass* InflateGenericClass(Il2CppClass* klass, Il2CppGenericContext *context);
  97. static const Il2CppType* InflateGenericType(const Il2CppType* type, Il2CppGenericContext *context);
  98. static Il2CppClass* GetArrayClassCached(Il2CppClass *element_class, uint32_t rank, bool bounded)
  99. {
  100. return GetBoundedArrayClass(element_class, rank, bounded);
  101. }
  102. static const Il2CppGenericContainer* GetGenericContainer(Il2CppClass *klass);
  103. static const MethodInfo* GetCCtor(Il2CppClass *klass);
  104. static const char* GetFieldDefaultValue(const FieldInfo *field, const Il2CppType** type);
  105. static int GetFieldMarshaledSize(const FieldInfo *field);
  106. static int GetFieldMarshaledAlignment(const FieldInfo *field);
  107. static Il2CppClass* GetPtrClass(const Il2CppType* type);
  108. static Il2CppClass* GetPtrClass(Il2CppClass* elementClass);
  109. static bool HasReferences(Il2CppClass *klass);
  110. static void SetupEvents(Il2CppClass *klass);
  111. static void SetupFields(Il2CppClass *klass);
  112. static void SetupMethods(Il2CppClass *klass);
  113. static void SetupNestedTypes(Il2CppClass *klass);
  114. static void SetupProperties(Il2CppClass *klass);
  115. static void SetupTypeHierarchy(Il2CppClass *klass);
  116. static void SetupInterfaces(Il2CppClass *klass);
  117. static const il2cpp::utils::dynamic_array<Il2CppClass*>& GetStaticFieldData();
  118. static size_t GetBitmapSize(const Il2CppClass* klass);
  119. static void GetBitmap(Il2CppClass* klass, size_t* bitmap, size_t& maxSetBit);
  120. static const Il2CppType* il2cpp_type_from_type_info(const TypeNameParseInfo& info, TypeSearchFlags searchFlags);
  121. static Il2CppClass* GetDeclaringType(Il2CppClass* klass);
  122. static void UpdateInitializedAndNoError(Il2CppClass *klass);
  123. static IL2CPP_FORCE_INLINE bool IsGenericClassAssignableFrom(const Il2CppClass* klass, const Il2CppClass* oklass, const Il2CppGenericContainer* genericContainer)
  124. {
  125. const Il2CppGenericClass* genericClass = klass->generic_class;
  126. const Il2CppGenericClass* oGenericClass = oklass->generic_class;
  127. if (oGenericClass == NULL || oGenericClass->typeDefinitionIndex != genericClass->typeDefinitionIndex)
  128. return false;
  129. const int32_t genericParameterCount = genericContainer->type_argc;
  130. const Il2CppGenericInst* genericInst = genericClass->context.class_inst;
  131. IL2CPP_ASSERT(genericInst->type_argc == genericParameterCount);
  132. const Il2CppGenericInst* oGenericInst = oGenericClass->context.class_inst;
  133. IL2CPP_ASSERT(oGenericInst->type_argc == genericParameterCount);
  134. for (int32_t i = 0; i < genericParameterCount; ++i)
  135. {
  136. const Il2CppGenericParameter* genericParameter = MetadataCache::GetGenericParameterFromIndex(genericContainer->genericParameterStart + i);
  137. const int32_t parameterVariance = genericParameter->flags & IL2CPP_GENERIC_PARAMETER_ATTRIBUTE_VARIANCE_MASK;
  138. Il2CppClass* genericParameterType = Class::FromIl2CppType(genericInst->type_argv[i]);
  139. Il2CppClass* oGenericParameterType = Class::FromIl2CppType(oGenericInst->type_argv[i]);
  140. if (parameterVariance == IL2CPP_GENERIC_PARAMETER_ATTRIBUTE_NON_VARIANT || Class::IsValuetype(genericParameterType) || Class::IsValuetype(oGenericParameterType))
  141. {
  142. if (genericParameterType != oGenericParameterType)
  143. return false;
  144. }
  145. else if (parameterVariance == IL2CPP_GENERIC_PARAMETER_ATTRIBUTE_COVARIANT)
  146. {
  147. if (!Class::IsAssignableFrom(genericParameterType, oGenericParameterType))
  148. return false;
  149. }
  150. else
  151. {
  152. IL2CPP_ASSERT(parameterVariance == IL2CPP_GENERIC_PARAMETER_ATTRIBUTE_CONTRAVARIANT);
  153. if (!Class::IsAssignableFrom(oGenericParameterType, genericParameterType))
  154. return false;
  155. }
  156. }
  157. return true;
  158. }
  159. };
  160. } /* namespace vm */
  161. } /* namespace il2cpp */