Class.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #if NET_4_0
  13. #include "vm/MetadataCache.h"
  14. #include "il2cpp-tabledefs.h"
  15. #endif
  16. struct Il2CppClass;
  17. struct EventInfo;
  18. struct FieldInfo;
  19. struct PropertyInfo;
  20. struct MethodInfo;
  21. struct Il2CppImage;
  22. struct Il2CppReflectionType;
  23. struct Il2CppType;
  24. struct Il2CppGenericContainer;
  25. struct Il2CppGenericContext;
  26. struct Il2CppGenericParameter;
  27. namespace il2cpp
  28. {
  29. namespace vm
  30. {
  31. class TypeNameParseInfo;
  32. class LIBIL2CPP_CODEGEN_API Class
  33. {
  34. public:
  35. static Il2CppClass* FromIl2CppType(const Il2CppType* type);
  36. static Il2CppClass* FromName(const Il2CppImage* image, const char* namespaze, const char *name);
  37. static Il2CppClass* FromSystemType(Il2CppReflectionType *type);
  38. static Il2CppClass* FromGenericParameter(const Il2CppGenericParameter *param);
  39. static Il2CppClass* GetElementClass(Il2CppClass *klass);
  40. static const Il2CppType* GetEnumBaseType(Il2CppClass *klass);
  41. static const EventInfo* GetEvents(Il2CppClass *klass, void* *iter);
  42. static FieldInfo* GetFields(Il2CppClass *klass, void* *iter);
  43. static FieldInfo* GetFieldFromName(Il2CppClass *klass, const char* name);
  44. static const MethodInfo* GetFinalizer(Il2CppClass *klass);
  45. static int32_t GetInstanceSize(const Il2CppClass *klass);
  46. static Il2CppClass* GetInterfaces(Il2CppClass *klass, void* *iter);
  47. static const MethodInfo* GetMethods(Il2CppClass *klass, void* *iter);
  48. static const MethodInfo* GetMethodFromName(Il2CppClass *klass, const char* name, int argsCount);
  49. static const MethodInfo* GetMethodFromNameFlags(Il2CppClass *klass, const char* name, int argsCount, int32_t flags);
  50. static const char* GetName(Il2CppClass *klass);
  51. static const char* GetNamespace(Il2CppClass *klass);
  52. static Il2CppClass* GetNestedTypes(Il2CppClass *klass, void* *iter);
  53. static size_t GetNumMethods(const Il2CppClass* klass);
  54. static size_t GetNumProperties(const Il2CppClass* klass);
  55. static size_t GetNumFields(const Il2CppClass* klass);
  56. static Il2CppClass* GetParent(Il2CppClass *klass);
  57. static const PropertyInfo* GetProperties(Il2CppClass *klass, void* *iter);
  58. static const PropertyInfo* GetPropertyFromName(Il2CppClass *klass, const char* name);
  59. static int32_t GetValueSize(Il2CppClass *klass, uint32_t *align);
  60. static bool HasParent(Il2CppClass *klass, Il2CppClass *parent);
  61. // we assume that the Il2CppClass's have already been initialized in this case, like in code generation
  62. static bool HasParentUnsafe(const Il2CppClass* klass, const Il2CppClass* parent) { return klass->typeHierarchyDepth >= parent->typeHierarchyDepth && klass->typeHierarchy[parent->typeHierarchyDepth - 1] == parent; }
  63. static bool IsAssignableFrom(Il2CppClass *klass, Il2CppClass *oklass);
  64. static bool IsGeneric(const Il2CppClass *klass);
  65. static bool IsInflated(const Il2CppClass *klass);
  66. static bool IsSubclassOf(Il2CppClass *klass, Il2CppClass *klassc, bool check_interfaces);
  67. static bool IsValuetype(const Il2CppClass *klass);
  68. static bool IsBlittable(const Il2CppClass *klass);
  69. static bool HasDefaultConstructor(Il2CppClass* klass);
  70. static int GetFlags(const Il2CppClass *klass);
  71. static bool IsAbstract(const Il2CppClass *klass);
  72. static bool IsInterface(const Il2CppClass *klass);
  73. static bool IsNullable(const Il2CppClass *klass);
  74. static Il2CppClass* GetNullableArgument(const Il2CppClass* klass);
  75. static int GetArrayElementSize(const Il2CppClass *klass);
  76. static const Il2CppType* GetType(Il2CppClass *klass);
  77. static const Il2CppType* GetType(Il2CppClass *klass, const TypeNameParseInfo &info);
  78. static bool HasAttribute(Il2CppClass *klass, Il2CppClass *attr_class);
  79. static bool IsEnum(const Il2CppClass *klass);
  80. static const Il2CppImage* GetImage(Il2CppClass* klass);
  81. static const char *GetAssemblyName(const Il2CppClass *klass);
  82. static const char *GetAssemblyNameNoExtension(const Il2CppClass *klass);
  83. public:
  84. //internal
  85. static FORCE_INLINE const VirtualInvokeData& GetInterfaceInvokeDataFromVTable(const Il2CppObject* obj, const Il2CppClass* itf, Il2CppMethodSlot slot)
  86. {
  87. const Il2CppClass* klass = obj->klass;
  88. IL2CPP_ASSERT(klass->initialized);
  89. IL2CPP_ASSERT(slot < itf->method_count);
  90. for (uint16_t i = 0; i < klass->interface_offsets_count; i++)
  91. {
  92. if (klass->interfaceOffsets[i].interfaceType == itf)
  93. {
  94. int32_t offset = klass->interfaceOffsets[i].offset;
  95. IL2CPP_ASSERT(offset != -1);
  96. IL2CPP_ASSERT(offset + slot < klass->vtable_count);
  97. return klass->vtable[offset + slot];
  98. }
  99. }
  100. return GetInterfaceInvokeDataFromVTableSlowPath(obj, itf, slot);
  101. }
  102. static bool Init(Il2CppClass *klass);
  103. static Il2CppClass* GetArrayClass(Il2CppClass *element_class, uint32_t rank);
  104. static Il2CppClass* GetBoundedArrayClass(Il2CppClass *element_class, uint32_t rank, bool bounded);
  105. static Il2CppClass* GetInflatedGenericInstanceClass(Il2CppClass* klass, const metadata::Il2CppTypeVector& types);
  106. static Il2CppClass* GetInflatedGenericInstanceClass(Il2CppClass* klass, const Il2CppGenericInst* genericInst);
  107. static Il2CppClass* InflateGenericClass(Il2CppClass* klass, Il2CppGenericContext *context);
  108. static const Il2CppType* InflateGenericType(const Il2CppType* type, Il2CppGenericContext *context);
  109. static Il2CppClass* GetArrayClassCached(Il2CppClass *element_class, uint32_t rank)
  110. {
  111. return GetArrayClass(element_class, rank);
  112. }
  113. static const Il2CppGenericContainer* GetGenericContainer(Il2CppClass *klass);
  114. static const MethodInfo* GetCCtor(Il2CppClass *klass);
  115. static const char* GetFieldDefaultValue(const FieldInfo *field, const Il2CppType** type);
  116. static int GetFieldMarshaledSize(const FieldInfo *field);
  117. static int GetFieldMarshaledAlignment(const FieldInfo *field);
  118. static Il2CppClass* GetPtrClass(const Il2CppType* type);
  119. static Il2CppClass* GetPtrClass(Il2CppClass* elementClass);
  120. static bool HasReferences(Il2CppClass *klass);
  121. static void SetupEvents(Il2CppClass *klass);
  122. static void SetupFields(Il2CppClass *klass);
  123. static void SetupMethods(Il2CppClass *klass);
  124. static void SetupNestedTypes(Il2CppClass *klass);
  125. static void SetupProperties(Il2CppClass *klass);
  126. static void SetupTypeHierarchy(Il2CppClass *klass);
  127. static void SetupInterfaces(Il2CppClass *klass);
  128. static const il2cpp::utils::dynamic_array<Il2CppClass*>& GetStaticFieldData();
  129. static size_t GetBitmapSize(const Il2CppClass* klass);
  130. static void GetBitmap(Il2CppClass* klass, size_t* bitmap, size_t& maxSetBit);
  131. static const Il2CppType* il2cpp_type_from_type_info(const TypeNameParseInfo& info, bool throwOnError, bool ignoreCase);
  132. static Il2CppClass* GetDeclaringType(Il2CppClass* klass);
  133. private:
  134. #if NET_4_0
  135. static FORCE_INLINE bool IsGenericClassAssignableFrom(const Il2CppClass* klass, const Il2CppClass* oklass, const Il2CppGenericContainer* genericContainer)
  136. {
  137. const Il2CppGenericClass* genericClass = klass->generic_class;
  138. const Il2CppGenericClass* oGenericClass = oklass->generic_class;
  139. if (oGenericClass == NULL || oGenericClass->typeDefinitionIndex != genericClass->typeDefinitionIndex)
  140. return false;
  141. const int32_t genericParameterCount = genericContainer->type_argc;
  142. const Il2CppGenericInst* genericInst = genericClass->context.class_inst;
  143. IL2CPP_ASSERT(genericInst->type_argc == genericParameterCount);
  144. const Il2CppGenericInst* oGenericInst = oGenericClass->context.class_inst;
  145. IL2CPP_ASSERT(oGenericInst->type_argc == genericParameterCount);
  146. for (int32_t i = 0; i < genericParameterCount; ++i)
  147. {
  148. const Il2CppGenericParameter* genericParameter = MetadataCache::GetGenericParameterFromIndex(genericContainer->genericParameterStart + i);
  149. const int32_t parameterVariance = genericParameter->flags & GENERIC_PARAMETER_ATTRIBUTE_VARIANCE_MASK;
  150. Il2CppClass* genericParameterType = Class::FromIl2CppType(genericInst->type_argv[i]);
  151. Il2CppClass* oGenericParameterType = Class::FromIl2CppType(oGenericInst->type_argv[i]);
  152. if (parameterVariance == GENERIC_PARAMETER_ATTRIBUTE_NON_VARIANT || Class::IsValuetype(genericParameterType) || Class::IsValuetype(oGenericParameterType))
  153. {
  154. if (genericParameterType != oGenericParameterType)
  155. return false;
  156. }
  157. else if (parameterVariance == GENERIC_PARAMETER_ATTRIBUTE_COVARIANT)
  158. {
  159. if (!Class::IsAssignableFrom(genericParameterType, oGenericParameterType))
  160. return false;
  161. }
  162. else
  163. {
  164. IL2CPP_ASSERT(parameterVariance == GENERIC_PARAMETER_ATTRIBUTE_CONTRAVARIANT);
  165. if (!Class::IsAssignableFrom(oGenericParameterType, genericParameterType))
  166. return false;
  167. }
  168. }
  169. return true;
  170. }
  171. #endif
  172. // we don't want this method to get inlined because that makes GetInterfaceInvokeDataFromVTable method itself very large and performance suffers
  173. static IL2CPP_NO_INLINE const VirtualInvokeData& GetInterfaceInvokeDataFromVTableSlowPath(const Il2CppObject* obj, const Il2CppClass* itf, Il2CppMethodSlot slot);
  174. };
  175. } /* namespace vm */
  176. } /* namespace il2cpp */