GenericClass.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "il2cpp-config.h"
  2. #include "metadata/GenericMetadata.h"
  3. #include "os/Atomic.h"
  4. #include "os/Mutex.h"
  5. #include "utils/Memory.h"
  6. #include "vm/Class.h"
  7. #include "vm/GenericClass.h"
  8. #include "vm/Exception.h"
  9. #include "vm/MetadataAlloc.h"
  10. #include "vm/MetadataCache.h"
  11. #include "vm/MetadataLock.h"
  12. #include "il2cpp-class-internals.h"
  13. #include "il2cpp-runtime-metadata.h"
  14. #include "il2cpp-runtime-stats.h"
  15. namespace il2cpp
  16. {
  17. namespace vm
  18. {
  19. void GenericClass::SetupMethods(Il2CppClass* genericInstanceType)
  20. {
  21. Il2CppClass* genericTypeDefinition = GenericClass::GetTypeDefinition(genericInstanceType->generic_class);
  22. uint16_t methodCount = genericTypeDefinition->method_count;
  23. IL2CPP_ASSERT(genericTypeDefinition->method_count == genericInstanceType->method_count);
  24. if (methodCount == 0)
  25. {
  26. genericInstanceType->methods = NULL;
  27. return;
  28. }
  29. const MethodInfo** methods = (const MethodInfo**)MetadataCalloc(methodCount, sizeof(MethodInfo*));
  30. for (uint16_t methodIndex = 0; methodIndex < methodCount; ++methodIndex)
  31. {
  32. const MethodInfo* methodDefinition = genericTypeDefinition->methods[methodIndex];
  33. methods[methodIndex] = metadata::GenericMetadata::Inflate(methodDefinition, GenericClass::GetContext(genericInstanceType->generic_class));
  34. }
  35. genericInstanceType->methods = methods;
  36. il2cpp_runtime_stats.method_count += methodCount;
  37. }
  38. static void InflatePropertyDefinition(const PropertyInfo* propertyDefinition, PropertyInfo* newProperty, Il2CppClass* declaringClass, Il2CppGenericContext* context)
  39. {
  40. newProperty->attrs = propertyDefinition->attrs;
  41. newProperty->parent = declaringClass;
  42. newProperty->name = propertyDefinition->name;
  43. newProperty->token = propertyDefinition->token;
  44. if (propertyDefinition->get)
  45. newProperty->get = metadata::GenericMetadata::Inflate(propertyDefinition->get, context);
  46. if (propertyDefinition->set)
  47. newProperty->set = metadata::GenericMetadata::Inflate(propertyDefinition->set, context);
  48. }
  49. void GenericClass::SetupProperties(Il2CppClass* genericInstanceType)
  50. {
  51. Il2CppClass* genericTypeDefinition = GenericClass::GetTypeDefinition(genericInstanceType->generic_class);
  52. uint16_t propertyCount = genericTypeDefinition->property_count;
  53. IL2CPP_ASSERT(genericTypeDefinition->property_count == genericInstanceType->property_count);
  54. if (propertyCount == 0)
  55. {
  56. genericInstanceType->properties = NULL;
  57. return;
  58. }
  59. PropertyInfo* properties = (PropertyInfo*)MetadataCalloc(propertyCount, sizeof(PropertyInfo));
  60. PropertyInfo* property = properties;
  61. for (uint16_t propertyIndex = 0; propertyIndex < propertyCount; ++propertyIndex)
  62. {
  63. InflatePropertyDefinition(genericTypeDefinition->properties + propertyIndex, property, genericInstanceType, GenericClass::GetContext(genericInstanceType->generic_class));
  64. property++;
  65. }
  66. genericInstanceType->properties = properties;
  67. }
  68. static void InflateEventDefinition(const EventInfo* eventDefinition, EventInfo* newEvent, Il2CppClass* declaringClass, Il2CppGenericContext* context)
  69. {
  70. newEvent->eventType = metadata::GenericMetadata::InflateIfNeeded(eventDefinition->eventType, context, false);
  71. newEvent->name = eventDefinition->name;
  72. newEvent->parent = declaringClass;
  73. newEvent->token = eventDefinition->token;
  74. if (eventDefinition->add)
  75. newEvent->add = metadata::GenericMetadata::Inflate(eventDefinition->add, context);
  76. if (eventDefinition->raise)
  77. newEvent->raise = metadata::GenericMetadata::Inflate(eventDefinition->raise, context);
  78. if (eventDefinition->remove)
  79. newEvent->remove = metadata::GenericMetadata::Inflate(eventDefinition->remove, context);
  80. }
  81. void GenericClass::SetupEvents(Il2CppClass* genericInstanceType)
  82. {
  83. Il2CppClass* genericTypeDefinition = GenericClass::GetTypeDefinition(genericInstanceType->generic_class);
  84. uint16_t eventCount = genericTypeDefinition->event_count;
  85. IL2CPP_ASSERT(genericTypeDefinition->event_count == genericInstanceType->event_count);
  86. if (eventCount == 0)
  87. {
  88. genericInstanceType->events = NULL;
  89. return;
  90. }
  91. EventInfo* events = (EventInfo*)MetadataCalloc(eventCount, sizeof(EventInfo));
  92. EventInfo* event = events;
  93. for (uint16_t eventIndex = 0; eventIndex < eventCount; ++eventIndex)
  94. {
  95. InflateEventDefinition(genericTypeDefinition->events + eventIndex, event, genericInstanceType, GenericClass::GetContext(genericInstanceType->generic_class));
  96. event++;
  97. }
  98. genericInstanceType->events = events;
  99. }
  100. static FieldInfo* InflateFieldDefinition(const FieldInfo* fieldDefinition, FieldInfo* newField, Il2CppClass* declaringClass, Il2CppGenericContext* context)
  101. {
  102. newField->type = metadata::GenericMetadata::InflateIfNeeded(fieldDefinition->type, context, false);
  103. newField->name = fieldDefinition->name;
  104. newField->parent = declaringClass;
  105. newField->offset = fieldDefinition->offset;
  106. newField->token = fieldDefinition->token;
  107. return newField;
  108. }
  109. void GenericClass::SetupFields(Il2CppClass* genericInstanceType)
  110. {
  111. Il2CppClass* genericTypeDefinition = GenericClass::GetTypeDefinition(genericInstanceType->generic_class);
  112. uint16_t fieldCount = genericTypeDefinition->field_count;
  113. IL2CPP_ASSERT(genericTypeDefinition->field_count == genericInstanceType->field_count);
  114. if (fieldCount == 0)
  115. {
  116. genericInstanceType->fields = NULL;
  117. return;
  118. }
  119. FieldInfo* fields = (FieldInfo*)MetadataCalloc(fieldCount, sizeof(FieldInfo));
  120. FieldInfo* field = fields;
  121. for (uint16_t fieldIndex = 0; fieldIndex < fieldCount; ++fieldIndex)
  122. {
  123. InflateFieldDefinition(genericTypeDefinition->fields + fieldIndex, field, genericInstanceType, GenericClass::GetContext(genericInstanceType->generic_class));
  124. field++;
  125. }
  126. genericInstanceType->fields = fields;
  127. }
  128. Il2CppClass* GenericClass::GetClass(Il2CppGenericClass* gclass, bool throwOnError)
  129. {
  130. Il2CppClass* cachedClass = os::Atomic::LoadPointerRelaxed(&gclass->cached_class);
  131. if (cachedClass)
  132. return cachedClass;
  133. return CreateClass(gclass, throwOnError);
  134. }
  135. Il2CppClass* GenericClass::CreateClass(Il2CppGenericClass *gclass, bool throwOnError)
  136. {
  137. Il2CppClass* definition = GetTypeDefinition(gclass);
  138. if (definition == NULL)
  139. {
  140. if (throwOnError)
  141. vm::Exception::Raise(vm::Exception::GetMaximumNestedGenericsException());
  142. return NULL;
  143. }
  144. os::FastAutoLock lock(&g_MetadataLock);
  145. if (!gclass->cached_class)
  146. {
  147. Il2CppClass* klass = (Il2CppClass*)MetadataCalloc(1, sizeof(Il2CppClass) + (sizeof(VirtualInvokeData) * definition->vtable_count));
  148. klass->klass = klass;
  149. klass->name = definition->name;
  150. klass->namespaze = definition->namespaze;
  151. klass->image = definition->image;
  152. klass->flags = definition->flags;
  153. //klass->type_token = definition->type_token;
  154. klass->generic_class = gclass;
  155. Il2CppClass* genericTypeDefinition = GenericClass::GetTypeDefinition(klass->generic_class);
  156. Il2CppGenericContext* context = &klass->generic_class->context;
  157. if (genericTypeDefinition->parent)
  158. klass->parent = Class::FromIl2CppType(metadata::GenericMetadata::InflateIfNeeded(&genericTypeDefinition->parent->byval_arg, context, false));
  159. if (genericTypeDefinition->declaringType)
  160. klass->declaringType = Class::FromIl2CppType(metadata::GenericMetadata::InflateIfNeeded(&genericTypeDefinition->declaringType->byval_arg, context, false));
  161. klass->this_arg.type = klass->byval_arg.type = IL2CPP_TYPE_GENERICINST;
  162. klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = gclass;
  163. klass->this_arg.byref = true;
  164. klass->byval_arg.valuetype = genericTypeDefinition->byval_arg.valuetype;
  165. klass->event_count = definition->event_count;
  166. klass->field_count = definition->field_count;
  167. klass->interfaces_count = definition->interfaces_count;
  168. klass->method_count = definition->method_count;
  169. klass->property_count = definition->property_count;
  170. klass->enumtype = definition->enumtype;
  171. klass->element_class = klass->castClass = klass;
  172. klass->has_cctor = definition->has_cctor;
  173. klass->cctor_finished_or_no_cctor = !definition->has_cctor;
  174. klass->has_finalize = definition->has_finalize;
  175. klass->native_size = klass->thread_static_fields_offset = -1;
  176. klass->token = definition->token;
  177. klass->interopData = MetadataCache::GetInteropDataForType(&klass->byval_arg);
  178. if (GenericClass::GetTypeDefinition(klass->generic_class) == il2cpp_defaults.generic_nullable_class)
  179. {
  180. klass->element_class = klass->castClass = Class::FromIl2CppType(klass->generic_class->context.class_inst->type_argv[0]);
  181. klass->nullabletype = true;
  182. }
  183. if (klass->enumtype)
  184. klass->element_class = klass->castClass = definition->element_class;
  185. klass->is_import_or_windows_runtime = definition->is_import_or_windows_runtime;
  186. // Do not update gclass->cached_class until `klass` is fully initialized
  187. // And do so with an atomic barrier so no threads observer the writes out of order
  188. il2cpp::os::Atomic::ExchangePointer(&gclass->cached_class, klass);
  189. }
  190. return gclass->cached_class;
  191. }
  192. Il2CppGenericContext* GenericClass::GetContext(Il2CppGenericClass *gclass)
  193. {
  194. return &gclass->context;
  195. }
  196. Il2CppClass* GenericClass::GetTypeDefinition(Il2CppGenericClass *gclass)
  197. {
  198. return MetadataCache::GetTypeInfoFromType(gclass->type);
  199. }
  200. bool GenericClass::IsEnum(Il2CppGenericClass *gclass)
  201. {
  202. return IsValueType(gclass) && GetTypeDefinition(gclass)->enumtype;
  203. }
  204. bool GenericClass::IsValueType(Il2CppGenericClass *gclass)
  205. {
  206. return GetTypeDefinition(gclass)->byval_arg.valuetype;
  207. }
  208. } /* namespace vm */
  209. } /* namespace il2cpp */