Class.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/ArrayMetadata.h"
  7. #include "metadata/Il2CppTypeCompare.h"
  8. #include "utils/dynamic_array.h"
  9. #include "il2cpp-class-internals.h"
  10. #include "il2cpp-object-internals.h"
  11. #include "Exception.h"
  12. #include "GenericClass.h"
  13. #include "Type.h"
  14. #include "os/Mutex.h"
  15. #include "vm/MetadataCache.h"
  16. #include "il2cpp-tabledefs.h"
  17. struct Il2CppClass;
  18. struct EventInfo;
  19. struct FieldInfo;
  20. struct PropertyInfo;
  21. struct MethodInfo;
  22. struct Il2CppImage;
  23. struct Il2CppReflectionType;
  24. struct Il2CppType;
  25. struct Il2CppGenericContainer;
  26. struct Il2CppGenericContext;
  27. struct MonoGenericParameterInfo;
  28. namespace il2cpp
  29. {
  30. namespace vm
  31. {
  32. class TypeNameParseInfo;
  33. enum TypeSearchFlags
  34. {
  35. kTypeSearchFlagNone = 0x0,
  36. kTypeSearchFlagIgnoreCase = 0x1,
  37. kTypeSearchFlagThrowOnError = 0x2,
  38. kTypeSearchFlagDontUseExecutingImage = 0x4
  39. };
  40. class LIBIL2CPP_CODEGEN_API Class
  41. {
  42. public:
  43. static Il2CppClass* FromIl2CppType(const Il2CppType* type, bool throwOnError = true);
  44. static Il2CppClass* FromIl2CppTypeEnum(Il2CppTypeEnum type);
  45. static Il2CppClass* FromName(const Il2CppImage* image, const char* namespaze, const char *name);
  46. static Il2CppClass* FromSystemType(Il2CppReflectionType *type);
  47. static Il2CppClass* FromGenericParameter(Il2CppMetadataGenericParameterHandle param);
  48. static Il2CppClass* GetElementClass(Il2CppClass *klass);
  49. static const Il2CppType* GetEnumBaseType(Il2CppClass *klass);
  50. static const EventInfo* GetEvents(Il2CppClass *klass, void* *iter);
  51. static FieldInfo* GetFields(Il2CppClass *klass, void* *iter);
  52. static FieldInfo* GetFieldFromName(Il2CppClass *klass, const char* name);
  53. static const MethodInfo* GetFinalizer(Il2CppClass *klass);
  54. static int32_t GetInstanceSize(const Il2CppClass *klass);
  55. static Il2CppClass* GetInterfaces(Il2CppClass *klass, void* *iter);
  56. static const MethodInfo* GetMethods(Il2CppClass *klass, void* *iter);
  57. static const MethodInfo* GetMethodFromName(Il2CppClass *klass, const char* name, int argsCount);
  58. static const MethodInfo* GetMethodFromNameFlags(Il2CppClass *klass, const char* name, int argsCount, int32_t flags);
  59. static const MethodInfo* GetMethodFromNameFlagsAndSig(Il2CppClass *klass, const char* name, int argsCount, int32_t flags, const Il2CppType** argTypes);
  60. static const MethodInfo* GetGenericInstanceMethodFromDefintion(Il2CppClass* genericInstanceClass, const MethodInfo* methodDefinition);
  61. static const char* GetName(Il2CppClass *klass);
  62. static const char* GetNamespace(Il2CppClass *klass);
  63. static Il2CppClass* GetNestedTypes(Il2CppClass *klass, void* *iter);
  64. static size_t GetNumMethods(const Il2CppClass* klass);
  65. static size_t GetNumProperties(const Il2CppClass* klass);
  66. static size_t GetNumFields(const Il2CppClass* klass);
  67. static Il2CppClass* GetParent(Il2CppClass *klass);
  68. static const PropertyInfo* GetProperties(Il2CppClass *klass, void* *iter);
  69. static const PropertyInfo* GetPropertyFromName(Il2CppClass *klass, const char* name);
  70. static int32_t GetValueSize(Il2CppClass *klass, uint32_t *align);
  71. //for Unsafe, but more performant version of HasParent, see ClassInlines.h
  72. static bool HasParent(Il2CppClass *klass, Il2CppClass *parent);
  73. static bool IsAssignableFrom(Il2CppClass *klass, Il2CppClass *oklass);
  74. static bool IsAssignableFrom(Il2CppReflectionType *klass, Il2CppReflectionType *oklass);
  75. static bool IsGeneric(const Il2CppClass *klass);
  76. static bool IsInflated(const Il2CppClass *klass);
  77. static bool IsGenericTypeDefinition(const Il2CppClass *klass);
  78. static bool IsSubclassOf(Il2CppClass *klass, Il2CppClass *klassc, bool check_interfaces);
  79. inline static bool IsValuetype(const Il2CppClass* klass)
  80. {
  81. return klass->byval_arg.valuetype;
  82. }
  83. inline static bool IsBlittable(const Il2CppClass *klass)
  84. {
  85. return klass->is_blittable;
  86. }
  87. static bool HasDefaultConstructor(Il2CppClass* klass);
  88. inline static int GetFlags(const Il2CppClass* klass)
  89. {
  90. return klass->flags;
  91. }
  92. inline static bool IsAbstract(const Il2CppClass* klass)
  93. {
  94. return (klass->flags & TYPE_ATTRIBUTE_ABSTRACT) != 0;
  95. }
  96. inline static bool IsInterface(const Il2CppClass* klass)
  97. {
  98. return (klass->flags & TYPE_ATTRIBUTE_INTERFACE) || (klass->byval_arg.type == IL2CPP_TYPE_VAR) || (klass->byval_arg.type == IL2CPP_TYPE_MVAR);
  99. }
  100. inline static bool IsNullable(const Il2CppClass* klass)
  101. {
  102. // Based on benchmarking doing the check on `klass->generic_class != NULL` makes this check faster
  103. // Likely since nullabletype is a bitfield and requires some manipulation to check
  104. return klass->generic_class != NULL && klass->nullabletype;
  105. }
  106. inline static Il2CppClass* GetNullableArgument(const Il2CppClass* klass)
  107. {
  108. IL2CPP_ASSERT(IsNullable(klass));
  109. return klass->element_class;
  110. }
  111. static int GetArrayElementSize(const Il2CppClass *klass);
  112. inline static const Il2CppType* GetByrefType(Il2CppClass* klass)
  113. {
  114. return &klass->this_arg;
  115. }
  116. inline static const Il2CppType* GetType(Il2CppClass* klass)
  117. {
  118. return &klass->byval_arg;
  119. }
  120. static const Il2CppType* GetType(Il2CppClass *klass, const TypeNameParseInfo &info);
  121. static bool HasAttribute(Il2CppClass *klass, Il2CppClass *attr_class);
  122. inline static bool IsEnum(const Il2CppClass* klass)
  123. {
  124. return klass->enumtype;
  125. }
  126. static const Il2CppImage* GetImage(Il2CppClass* klass);
  127. static const char *GetAssemblyName(const Il2CppClass *klass);
  128. static const char *GetAssemblyNameNoExtension(const Il2CppClass *klass);
  129. static Il2CppClass* GenericParamGetBaseType(Il2CppClass* klass);
  130. static MonoGenericParameterInfo* GetOrCreateMonoGenericParameterInfo(Il2CppMetadataGenericParameterHandle parameterHandle);
  131. static const int IgnoreNumberOfArguments;
  132. public:
  133. static void Init(Il2CppClass *klass);
  134. static bool InitLocked(Il2CppClass* klass, const il2cpp::os::FastAutoLock& lock);
  135. static bool InitSizeAndFieldLayoutLocked(Il2CppClass* klass, const il2cpp::os::FastAutoLock& lock);
  136. static Il2CppClass* GetArrayClass(Il2CppClass *element_class, uint32_t rank);
  137. static Il2CppClass* GetBoundedArrayClass(Il2CppClass *element_class, uint32_t rank, bool bounded);
  138. static Il2CppClass* GetInflatedGenericInstanceClass(Il2CppClass* klass, const Il2CppType** types, uint32_t typeCount);
  139. static Il2CppClass* GetInflatedGenericInstanceClass(Il2CppClass* klass, const Il2CppGenericInst* genericInst);
  140. static Il2CppClass* InflateGenericClass(Il2CppClass* klass, Il2CppGenericContext *context);
  141. static const Il2CppType* InflateGenericType(const Il2CppType* type, Il2CppGenericContext *context);
  142. static Il2CppMetadataGenericContainerHandle GetGenericContainer(Il2CppClass *klass);
  143. static const MethodInfo* GetCCtor(Il2CppClass *klass);
  144. static const char* GetFieldDefaultValue(const FieldInfo *field, const Il2CppType** type);
  145. static int GetFieldMarshaledSize(const FieldInfo *field);
  146. static int GetFieldMarshaledAlignment(const FieldInfo *field);
  147. static Il2CppClass* GetPtrClass(const Il2CppType* type);
  148. static Il2CppClass* GetPtrClass(Il2CppClass* elementClass);
  149. static bool HasReferences(Il2CppClass *klass);
  150. static void SetupEvents(Il2CppClass *klass);
  151. static void SetupFields(Il2CppClass *klass);
  152. static void SetupMethods(Il2CppClass *klass);
  153. static void SetupNestedTypes(Il2CppClass *klass);
  154. static void SetupProperties(Il2CppClass *klass);
  155. static void SetupTypeHierarchy(Il2CppClass *klass);
  156. static void SetupInterfaces(Il2CppClass *klass);
  157. static const il2cpp::utils::dynamic_array<Il2CppClass*>& GetStaticFieldData();
  158. static size_t GetBitmapSize(const Il2CppClass* klass);
  159. static void GetBitmap(Il2CppClass* klass, size_t* bitmap, size_t& maxSetBit);
  160. static const Il2CppType* il2cpp_type_from_type_info(const TypeNameParseInfo& info, TypeSearchFlags searchFlags);
  161. static Il2CppClass* GetDeclaringType(Il2CppClass* klass);
  162. static const MethodInfo* GetVirtualMethod(Il2CppClass* klass, const MethodInfo* virtualMethod);
  163. static void SetClassInitializationError(Il2CppClass* klass, Il2CppException* error);
  164. static void PublishInitialized(Il2CppClass* klass);
  165. static IL2CPP_FORCE_INLINE bool IsGenericClassAssignableFrom(const Il2CppClass* klass, const Il2CppClass* oklass, const Il2CppClass* implementingClass = il2cpp_defaults.missing_class)
  166. {
  167. return klass == oklass || IsGenericClassAssignableFromVariance(klass, oklass, implementingClass);
  168. }
  169. static IL2CPP_FORCE_INLINE bool IsGenericClassAssignableFromVariance(const Il2CppClass* klass, const Il2CppClass* oklass, const Il2CppClass* implementingClass = il2cpp_defaults.missing_class)
  170. {
  171. IL2CPP_ASSERT(klass != oklass && "IsGenericClassAssignableFromVariance is for generic variance checks - you should call IsGenericClassAssignableFrom instead");
  172. const Il2CppGenericClass* genericClass = klass->generic_class;
  173. const Il2CppGenericClass* oGenericClass = oklass->generic_class;
  174. if (oGenericClass == NULL || !GenericClass::HasSameGenericTypeDefinition(oGenericClass, genericClass))
  175. return false;
  176. const Il2CppGenericInst* genericInst = genericClass->context.class_inst;
  177. const Il2CppGenericInst* oGenericInst = oGenericClass->context.class_inst;
  178. Il2CppMetadataGenericContainerHandle genericContainer = MetadataCache::GetGenericContainerFromGenericClass(klass->image, klass->generic_class);
  179. IL2CPP_ASSERT(oGenericInst->type_argc == genericInst->type_argc);
  180. for (uint32_t i = 0; i < genericInst->type_argc; ++i)
  181. {
  182. uint16_t flags = MetadataCache::GetGenericParameterFlags(MetadataCache::GetGenericParameterFromIndex(genericContainer, i));
  183. const int32_t parameterVariance = flags & IL2CPP_GENERIC_PARAMETER_ATTRIBUTE_VARIANCE_MASK;
  184. Il2CppClass* genericParameterType = Class::FromIl2CppType(genericInst->type_argv[i]);
  185. Il2CppClass* oGenericParameterType = Class::FromIl2CppType(oGenericInst->type_argv[i]);
  186. if (parameterVariance == IL2CPP_GENERIC_PARAMETER_ATTRIBUTE_NON_VARIANT || Class::IsValuetype(genericParameterType) || Class::IsValuetype(oGenericParameterType))
  187. {
  188. if (genericParameterType != oGenericParameterType)
  189. {
  190. if (implementingClass->rank || implementingClass->declaringType == il2cpp_defaults.array_class)
  191. {
  192. // For arrays or System.Array/InternalEnumerator<T> we need to follow the array variance rules when looking for an
  193. // generic interface i.e. Int32[] should be assignable to IEnumerable<UInt32>
  194. IL2CPP_ASSERT(implementingClass->rank || strcmp(implementingClass->name, "InternalEnumerator`1") == 0);
  195. if (metadata::ArrayMetadata::GetArrayVarianceReducedType(genericParameterType) != metadata::ArrayMetadata::GetArrayVarianceReducedType(oGenericParameterType))
  196. return false;
  197. }
  198. else
  199. {
  200. return false;
  201. }
  202. }
  203. }
  204. else if (parameterVariance == IL2CPP_GENERIC_PARAMETER_ATTRIBUTE_COVARIANT)
  205. {
  206. if (!Class::IsAssignableFrom(genericParameterType, oGenericParameterType))
  207. return false;
  208. }
  209. else
  210. {
  211. IL2CPP_ASSERT(parameterVariance == IL2CPP_GENERIC_PARAMETER_ATTRIBUTE_CONTRAVARIANT);
  212. if (!Class::IsAssignableFrom(oGenericParameterType, genericParameterType))
  213. return false;
  214. }
  215. }
  216. return true;
  217. }
  218. };
  219. } /* namespace vm */
  220. } /* namespace il2cpp */