3 * Copyright 2016 Microsoft
4 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
6 #include <mono/metadata/class-internals.h>
7 #include <mono/metadata/tabledefs.h>
11 PROP_MARSHAL_INFO = 1, /* MonoMarshalType */
12 PROP_REF_INFO_HANDLE = 2, /* gchandle */
13 PROP_EXCEPTION_DATA = 3, /* MonoErrorBoxed* */
14 PROP_NESTED_CLASSES = 4, /* GList* */
15 PROP_PROPERTY_INFO = 5, /* MonoClassPropertyInfo* */
16 PROP_EVENT_INFO = 6, /* MonoClassEventInfo* */
17 PROP_FIELD_DEF_VALUES = 7, /* MonoFieldDefaultValue* */
18 PROP_DECLSEC_FLAGS = 8 /* guint32 */
21 /* Accessors based on class kind*/
24 * mono_class_get_generic_class:
26 * Return the MonoGenericClass of @klass, which MUST be a generic instance.
29 mono_class_get_generic_class (MonoClass *klass)
31 g_assert (mono_class_is_ginst (klass));
32 return ((MonoClassGenericInst*)klass)->generic_class;
36 * mono_class_try_get_generic_class:
38 * Return the MonoGenericClass if @klass is a ginst, NULL otherwise
41 mono_class_try_get_generic_class (MonoClass *klass)
43 if (mono_class_is_ginst (klass))
44 return ((MonoClassGenericInst*)klass)->generic_class;
49 * mono_class_get_flags:
50 * \param klass the MonoClass to act on
51 * \returns the \c TypeAttributes flags of \p klass.
52 * See the \c TYPE_ATTRIBUTE_* definitions in \c tabledefs.h for the different values.
55 mono_class_get_flags (MonoClass *klass)
57 switch (klass->class_kind) {
60 return ((MonoClassDef*)klass)->flags;
61 case MONO_CLASS_GINST:
62 return mono_class_get_flags (((MonoClassGenericInst*)klass)->generic_class->container_class);
63 case MONO_CLASS_GPARAM:
64 return TYPE_ATTRIBUTE_PUBLIC;
65 case MONO_CLASS_ARRAY:
66 /* all arrays are marked serializable and sealed, bug #42779 */
67 return TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED | TYPE_ATTRIBUTE_PUBLIC;
68 case MONO_CLASS_POINTER:
69 return TYPE_ATTRIBUTE_CLASS | (mono_class_get_flags (klass->element_class) & TYPE_ATTRIBUTE_VISIBILITY_MASK);
71 g_assert_not_reached ();
75 mono_class_set_flags (MonoClass *klass, guint32 flags)
77 g_assert (klass->class_kind == MONO_CLASS_DEF || klass->class_kind == MONO_CLASS_GTD);
78 ((MonoClassDef*)klass)->flags = flags;
82 * mono_class_get_generic_container:
84 * Return the generic container of KLASS which should be a generic type definition.
87 mono_class_get_generic_container (MonoClass *klass)
89 g_assert (mono_class_is_gtd (klass));
91 return ((MonoClassGtd*)klass)->generic_container;
95 mono_class_try_get_generic_container (MonoClass *klass)
97 if (mono_class_is_gtd (klass))
98 return ((MonoClassGtd*)klass)->generic_container;
104 mono_class_set_generic_container (MonoClass *klass, MonoGenericContainer *container)
106 g_assert (mono_class_is_gtd (klass));
108 ((MonoClassGtd*)klass)->generic_container = container;
112 * mono_class_get_first_method_idx:
114 * Return the table index of the first method for metadata classes.
117 mono_class_get_first_method_idx (MonoClass *klass)
119 g_assert (mono_class_has_static_metadata (klass));
121 return ((MonoClassDef*)klass)->first_method_idx;
125 mono_class_set_first_method_idx (MonoClass *klass, guint32 idx)
127 g_assert (mono_class_has_static_metadata (klass));
129 ((MonoClassDef*)klass)->first_method_idx = idx;
133 mono_class_get_first_field_idx (MonoClass *klass)
135 if (mono_class_is_ginst (klass))
136 return mono_class_get_first_field_idx (mono_class_get_generic_class (klass)->container_class);
138 g_assert (mono_class_has_static_metadata (klass));
140 return ((MonoClassDef*)klass)->first_field_idx;
144 mono_class_set_first_field_idx (MonoClass *klass, guint32 idx)
146 g_assert (mono_class_has_static_metadata (klass));
148 ((MonoClassDef*)klass)->first_field_idx = idx;
152 mono_class_get_method_count (MonoClass *klass)
154 switch (klass->class_kind) {
157 return ((MonoClassDef*)klass)->method_count;
158 case MONO_CLASS_GINST:
159 return mono_class_get_method_count (((MonoClassGenericInst*)klass)->generic_class->container_class);
160 case MONO_CLASS_GPARAM:
162 case MONO_CLASS_ARRAY:
163 return ((MonoClassArray*)klass)->method_count;
164 case MONO_CLASS_POINTER:
167 g_assert_not_reached ();
173 mono_class_set_method_count (MonoClass *klass, guint32 count)
175 switch (klass->class_kind) {
178 ((MonoClassDef*)klass)->method_count = count;
180 case MONO_CLASS_GINST:
182 case MONO_CLASS_GPARAM:
183 case MONO_CLASS_POINTER:
184 g_assert (count == 0);
186 case MONO_CLASS_ARRAY:
187 ((MonoClassArray*)klass)->method_count = count;
190 g_assert_not_reached ();
196 mono_class_get_field_count (MonoClass *klass)
198 switch (klass->class_kind) {
201 return ((MonoClassDef*)klass)->field_count;
202 case MONO_CLASS_GINST:
203 return mono_class_get_field_count (((MonoClassGenericInst*)klass)->generic_class->container_class);
204 case MONO_CLASS_GPARAM:
205 case MONO_CLASS_ARRAY:
206 case MONO_CLASS_POINTER:
209 g_assert_not_reached ();
215 mono_class_set_field_count (MonoClass *klass, guint32 count)
217 switch (klass->class_kind) {
220 ((MonoClassDef*)klass)->field_count = count;
222 case MONO_CLASS_GINST:
224 case MONO_CLASS_GPARAM:
225 case MONO_CLASS_ARRAY:
226 case MONO_CLASS_POINTER:
227 g_assert (count == 0);
230 g_assert_not_reached ();
236 mono_class_get_marshal_info (MonoClass *class)
238 return mono_property_bag_get (&class->infrequent_data, PROP_MARSHAL_INFO);
242 mono_class_set_marshal_info (MonoClass *class, MonoMarshalType *marshal_info)
244 marshal_info->head.tag = PROP_MARSHAL_INFO;
245 mono_property_bag_add (&class->infrequent_data, marshal_info);
249 MonoPropertyBagItem head;
254 mono_class_get_ref_info_handle (MonoClass *class)
256 Uint32Property *prop = mono_property_bag_get (&class->infrequent_data, PROP_REF_INFO_HANDLE);
257 return prop ? prop->value : 0;
261 mono_class_set_ref_info_handle (MonoClass *class, guint32 value)
264 Uint32Property *prop = mono_property_bag_get (&class->infrequent_data, PROP_REF_INFO_HANDLE);
270 Uint32Property *prop = mono_class_alloc (class, sizeof (Uint32Property));
271 prop->head.tag = PROP_REF_INFO_HANDLE;
273 prop = mono_property_bag_add (&class->infrequent_data, prop);
278 MonoPropertyBagItem head;
283 set_pointer_property (MonoClass *klass, InfrequentDataKind property, gpointer value)
285 PointerProperty *prop = mono_class_alloc (klass, sizeof (PointerProperty));
286 prop->head.tag = property;
288 mono_property_bag_add (&klass->infrequent_data, prop);
292 get_pointer_property (MonoClass *klass, InfrequentDataKind property)
294 PointerProperty *prop = (PointerProperty*)mono_property_bag_get (&klass->infrequent_data, property);
295 return prop ? prop->value : NULL;
299 mono_class_get_exception_data (MonoClass *klass)
301 return (MonoErrorBoxed*)get_pointer_property (klass, PROP_EXCEPTION_DATA);
305 mono_class_set_exception_data (MonoClass *klass, MonoErrorBoxed *value)
307 set_pointer_property (klass, PROP_EXCEPTION_DATA, value);
311 mono_class_get_nested_classes_property (MonoClass *klass)
313 return (GList*)get_pointer_property (klass, PROP_NESTED_CLASSES);
317 mono_class_set_nested_classes_property (MonoClass *klass, GList *value)
319 set_pointer_property (klass, PROP_NESTED_CLASSES, value);
322 MonoClassPropertyInfo*
323 mono_class_get_property_info (MonoClass *klass)
325 return mono_property_bag_get (&klass->infrequent_data, PROP_PROPERTY_INFO);
329 mono_class_set_property_info (MonoClass *klass, MonoClassPropertyInfo *info)
331 info->head.tag = PROP_PROPERTY_INFO;
332 mono_property_bag_add (&klass->infrequent_data, info);
336 mono_class_get_event_info (MonoClass *klass)
338 return mono_property_bag_get (&klass->infrequent_data, PROP_EVENT_INFO);
342 mono_class_set_event_info (MonoClass *klass, MonoClassEventInfo *info)
344 info->head.tag = PROP_EVENT_INFO;
345 mono_property_bag_add (&klass->infrequent_data, info);
348 MonoFieldDefaultValue*
349 mono_class_get_field_def_values (MonoClass *klass)
351 return (MonoFieldDefaultValue*)get_pointer_property (klass, PROP_FIELD_DEF_VALUES);
355 mono_class_set_field_def_values (MonoClass *klass, MonoFieldDefaultValue *values)
357 set_pointer_property (klass, PROP_FIELD_DEF_VALUES, values);
361 mono_class_get_declsec_flags (MonoClass *class)
363 Uint32Property *prop = mono_property_bag_get (&class->infrequent_data, PROP_DECLSEC_FLAGS);
364 return prop ? prop->value : 0;
368 mono_class_set_declsec_flags (MonoClass *class, guint32 value)
370 Uint32Property *prop = mono_class_alloc (class, sizeof (Uint32Property));
371 prop->head.tag = PROP_DECLSEC_FLAGS;
373 mono_property_bag_add (&class->infrequent_data, prop);
377 mono_class_set_is_com_object (MonoClass *klass)
381 klass->is_com_object = 1;
382 mono_loader_unlock ();
387 mono_class_gtd_get_canonical_inst (MonoClass *klass)
389 g_assert (mono_class_is_gtd (klass));
390 return &((MonoClassGtd*)klass)->canonical_inst;