/
class.c
5776 lines (4986 loc) · 158 KB
/
class.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* class.c: Class management for the Mono runtime
*
* Author:
* Miguel de Icaza (miguel@ximian.com)
*
* (C) 2001-2006 Novell, Inc.
*
*/
#include <config.h>
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#if !PLATFORM_WIN32
#include <mono/io-layer/atomic.h>
#endif
#include <mono/metadata/image.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/cil-coff.h>
#include <mono/metadata/metadata.h>
#include <mono/metadata/metadata-internals.h>
#include <mono/metadata/tabledefs.h>
#include <mono/metadata/tokentype.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/object.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/mono-endian.h>
#include <mono/metadata/debug-helpers.h>
#include <mono/metadata/reflection.h>
#include <mono/metadata/exception.h>
#include <mono/metadata/security-manager.h>
#include <mono/os/gc_wrapper.h>
#include <mono/utils/mono-counters.h>
MonoStats mono_stats;
gboolean mono_print_vtable = FALSE;
/* Function supplied by the runtime to find classes by name using information from the AOT file */
static MonoGetClassFromName get_class_from_name = NULL;
static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
static gboolean mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res);
void (*mono_debugger_start_class_init_func) (MonoClass *klass) = NULL;
void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
/*
* mono_class_from_typeref:
* @image: a MonoImage
* @type_token: a TypeRef token
*
* Creates the MonoClass* structure representing the type defined by
* the typeref token valid inside @image.
* Returns: the MonoClass* representing the typeref token, NULL ifcould
* not be loaded.
*/
MonoClass *
mono_class_from_typeref (MonoImage *image, guint32 type_token)
{
guint32 cols [MONO_TYPEREF_SIZE];
MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
guint32 idx;
const char *name, *nspace;
MonoClass *res;
MonoImage *module;
mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
case MONO_RESOLTION_SCOPE_MODULE:
if (!idx)
g_error ("null ResolutionScope not yet handled");
/* a typedef in disguise */
return mono_class_from_name (image, nspace, name);
case MONO_RESOLTION_SCOPE_MODULEREF:
module = mono_image_load_module (image, idx);
if (module)
return mono_class_from_name (module, nspace, name);
else {
char *msg = g_strdup_printf ("%s%s%s", nspace, nspace [0] ? "." : "", name);
char *human_name;
human_name = mono_stringify_assembly_name (&image->assembly->aname);
mono_loader_set_error_type_load (msg, human_name);
g_free (msg);
g_free (human_name);
return NULL;
}
case MONO_RESOLTION_SCOPE_TYPEREF: {
MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
GList *tmp;
if (enclosing->inited) {
/* Micro-optimization: don't scan the metadata tables if enclosing is already inited */
for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
res = tmp->data;
if (strcmp (res->name, name) == 0)
return res;
}
} else {
/* Don't call mono_class_init as we might've been called by it recursively */
int i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, 1);
while (i) {
guint32 class_nested = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, MONO_NESTED_CLASS_NESTED);
guint32 string_offset = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_TYPEDEF], class_nested - 1, MONO_TYPEDEF_NAME);
const char *nname = mono_metadata_string_heap (enclosing->image, string_offset);
if (strcmp (nname, name) == 0)
return mono_class_create_from_typedef (enclosing->image, MONO_TOKEN_TYPE_DEF | class_nested);
i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, i + 1);
}
}
g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
return NULL;
}
case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
break;
}
if (!image->references || !image->references [idx - 1])
mono_assembly_load_reference (image, idx - 1);
g_assert (image->references [idx - 1]);
/* If the assembly did not load, register this as a type load exception */
if (image->references [idx - 1] == REFERENCE_MISSING){
MonoAssemblyName aname;
char *human_name;
mono_assembly_get_assemblyref (image, idx - 1, &aname);
human_name = mono_stringify_assembly_name (&aname);
mono_loader_set_error_assembly_load (human_name, image->assembly->ref_only);
g_free (human_name);
return NULL;
}
return mono_class_from_name (image->references [idx - 1]->image, nspace, name);
}
static inline MonoType*
dup_type (MonoType* t, const MonoType *original)
{
MonoType *r = g_new0 (MonoType, 1);
*r = *t;
r->attrs = original->attrs;
r->byref = original->byref;
if (t->type == MONO_TYPE_PTR)
t->data.type = dup_type (t->data.type, original->data.type);
else if (t->type == MONO_TYPE_ARRAY)
t->data.array = mono_dup_array_type (t->data.array);
else if (t->type == MONO_TYPE_FNPTR)
t->data.method = mono_metadata_signature_deep_dup (t->data.method);
mono_stats.generics_metadata_size += sizeof (MonoType);
return r;
}
/* Copy everything mono_metadata_free_array free. */
MonoArrayType *
mono_dup_array_type (MonoArrayType *a)
{
a = g_memdup (a, sizeof (MonoArrayType));
if (a->sizes)
a->sizes = g_memdup (a->sizes, a->numsizes * sizeof (int));
if (a->lobounds)
a->lobounds = g_memdup (a->lobounds, a->numlobounds * sizeof (int));
return a;
}
/* Copy everything mono_metadata_free_method_signature free. */
MonoMethodSignature*
mono_metadata_signature_deep_dup (MonoMethodSignature *sig)
{
int i;
sig = mono_metadata_signature_dup (sig);
sig->ret = dup_type (sig->ret, sig->ret);
for (i = 0; i < sig->param_count; ++i)
sig->params [i] = dup_type (sig->params [i], sig->params [i]);
return sig;
}
static void
_mono_type_get_assembly_name (MonoClass *klass, GString *str)
{
MonoAssembly *ta = klass->image->assembly;
g_string_append_printf (
str, ", %s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s",
ta->aname.name,
ta->aname.major, ta->aname.minor, ta->aname.build, ta->aname.revision,
ta->aname.culture && *ta->aname.culture? ta->aname.culture: "neutral",
ta->aname.public_key_token [0] ? (char *)ta->aname.public_key_token : "null");
}
static void
mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
MonoTypeNameFormat format)
{
MonoClass *klass;
switch (type->type) {
case MONO_TYPE_ARRAY: {
int i, rank = type->data.array->rank;
MonoTypeNameFormat nested_format;
nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
mono_type_get_name_recurse (
&type->data.array->eklass->byval_arg, str, FALSE, nested_format);
g_string_append_c (str, '[');
if (rank == 1)
g_string_append_c (str, '*');
for (i = 1; i < rank; i++)
g_string_append_c (str, ',');
g_string_append_c (str, ']');
if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
_mono_type_get_assembly_name (type->data.array->eklass, str);
break;
}
case MONO_TYPE_SZARRAY: {
MonoTypeNameFormat nested_format;
nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
mono_type_get_name_recurse (
&type->data.klass->byval_arg, str, FALSE, nested_format);
g_string_append (str, "[]");
if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
_mono_type_get_assembly_name (type->data.klass, str);
break;
}
case MONO_TYPE_PTR: {
MonoTypeNameFormat nested_format;
nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
mono_type_get_name_recurse (
type->data.type, str, FALSE, nested_format);
g_string_append_c (str, '*');
if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
_mono_type_get_assembly_name (mono_class_from_mono_type (type->data.type), str);
break;
}
case MONO_TYPE_VAR:
case MONO_TYPE_MVAR:
g_assert (type->data.generic_param->name);
g_string_append (str, type->data.generic_param->name);
break;
default:
klass = mono_class_from_mono_type (type);
if (klass->nested_in) {
mono_type_get_name_recurse (
&klass->nested_in->byval_arg, str, TRUE, format);
if (format == MONO_TYPE_NAME_FORMAT_IL)
g_string_append_c (str, '.');
else
g_string_append_c (str, '+');
} else if (*klass->name_space) {
g_string_append (str, klass->name_space);
g_string_append_c (str, '.');
}
if (format == MONO_TYPE_NAME_FORMAT_IL) {
char *s = strchr (klass->name, '`');
int len = s ? s - klass->name : strlen (klass->name);
g_string_append_len (str, klass->name, len);
} else
g_string_append (str, klass->name);
if (is_recursed)
break;
if (klass->generic_class) {
MonoGenericClass *gclass = klass->generic_class;
MonoGenericInst *inst = gclass->context.class_inst;
MonoTypeNameFormat nested_format;
int i;
nested_format = format == MONO_TYPE_NAME_FORMAT_FULL_NAME ?
MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED : format;
if (format == MONO_TYPE_NAME_FORMAT_IL)
g_string_append_c (str, '<');
else
g_string_append_c (str, '[');
for (i = 0; i < inst->type_argc; i++) {
MonoType *t = inst->type_argv [i];
if (i)
g_string_append_c (str, ',');
if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
(t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
g_string_append_c (str, '[');
mono_type_get_name_recurse (inst->type_argv [i], str, FALSE, nested_format);
if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
(t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
g_string_append_c (str, ']');
}
if (format == MONO_TYPE_NAME_FORMAT_IL)
g_string_append_c (str, '>');
else
g_string_append_c (str, ']');
} else if (klass->generic_container &&
(format != MONO_TYPE_NAME_FORMAT_FULL_NAME) &&
(format != MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)) {
int i;
if (format == MONO_TYPE_NAME_FORMAT_IL)
g_string_append_c (str, '<');
else
g_string_append_c (str, '[');
for (i = 0; i < klass->generic_container->type_argc; i++) {
if (i)
g_string_append_c (str, ',');
g_string_append (str, klass->generic_container->type_params [i].name);
}
if (format == MONO_TYPE_NAME_FORMAT_IL)
g_string_append_c (str, '>');
else
g_string_append_c (str, ']');
}
if ((format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
(type->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
_mono_type_get_assembly_name (klass, str);
break;
}
}
/**
* mono_type_get_name:
* @type: a type
* @format: the format for the return string.
*
*
* Returns: the string representation in a number of formats:
*
* if format is MONO_TYPE_NAME_FORMAT_REFLECTION, the return string is
* returned in the formatrequired by System.Reflection, this is the
* inverse of mono_reflection_parse_type ().
*
* if format is MONO_TYPE_NAME_FORMAT_IL, it returns a syntax that can
* be used by the IL assembler.
*
* if format is MONO_TYPE_NAME_FORMAT_FULL_NAME
*
* if format is MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
*/
char*
mono_type_get_name_full (MonoType *type, MonoTypeNameFormat format)
{
GString* result;
result = g_string_new ("");
mono_type_get_name_recurse (type, result, FALSE, format);
if (type->byref)
g_string_append_c (result, '&');
return g_string_free (result, FALSE);
}
/**
* mono_type_get_full_name:
* @class: a class
*
* Returns: the string representation for type as required by System.Reflection.
* The inverse of mono_reflection_parse_type ().
*/
char *
mono_type_get_full_name (MonoClass *class)
{
return mono_type_get_name_full (mono_class_get_type (class), MONO_TYPE_NAME_FORMAT_REFLECTION);
}
/**
* mono_type_get_name:
* @type: a type
*
* Returns: the string representation for type as it would be represented in IL code.
*/
char*
mono_type_get_name (MonoType *type)
{
return mono_type_get_name_full (type, MONO_TYPE_NAME_FORMAT_IL);
}
/*
* mono_type_get_underlying_type:
* @type: a type
*
* Returns: the MonoType for the underlying integer type if @type
* is an enum and byref is false, otherwise the type itself.
*/
MonoType*
mono_type_get_underlying_type (MonoType *type)
{
if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype && !type->byref)
return type->data.klass->enum_basetype;
if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype && !type->byref)
return type->data.generic_class->container_class->enum_basetype;
return type;
}
/*
* mono_class_is_open_constructed_type:
* @type: a type
*
* Returns TRUE if type represents a generics open constructed type
* (not all the type parameters required for the instantiation have
* been provided).
*/
gboolean
mono_class_is_open_constructed_type (MonoType *t)
{
switch (t->type) {
case MONO_TYPE_VAR:
case MONO_TYPE_MVAR:
return TRUE;
case MONO_TYPE_SZARRAY:
return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
case MONO_TYPE_ARRAY:
return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
case MONO_TYPE_PTR:
return mono_class_is_open_constructed_type (t->data.type);
case MONO_TYPE_GENERICINST:
return t->data.generic_class->context.class_inst->is_open;
default:
return FALSE;
}
}
static MonoType*
inflate_generic_type (MonoType *type, MonoGenericContext *context)
{
switch (type->type) {
case MONO_TYPE_MVAR: {
int num = type->data.generic_param->num;
MonoGenericInst *inst = context->method_inst;
if (!inst || !inst->type_argv)
return NULL;
if (num >= inst->type_argc)
g_error ("MVAR %d (%s) cannot be expanded in this context with %d instantiations", num, type->data.generic_param->name, inst->type_argc);
return dup_type (inst->type_argv [num], type);
}
case MONO_TYPE_VAR: {
int num = type->data.generic_param->num;
MonoGenericInst *inst = context->class_inst;
if (!inst)
return NULL;
if (num >= inst->type_argc)
g_error ("VAR %d (%s) cannot be expanded in this context with %d instantiations", num, type->data.generic_param->name, inst->type_argc);
return dup_type (inst->type_argv [num], type);
}
case MONO_TYPE_SZARRAY: {
MonoClass *eclass = type->data.klass;
MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
if (!inflated)
return NULL;
nt = dup_type (type, type);
nt->data.klass = mono_class_from_mono_type (inflated);
return nt;
}
case MONO_TYPE_ARRAY: {
MonoClass *eclass = type->data.array->eklass;
MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
if (!inflated)
return NULL;
nt = dup_type (type, type);
nt->data.array = g_memdup (nt->data.array, sizeof (MonoArrayType));
nt->data.array->eklass = mono_class_from_mono_type (inflated);
return nt;
}
case MONO_TYPE_GENERICINST: {
MonoGenericClass *gclass = type->data.generic_class;
MonoGenericInst *inst;
MonoType *nt;
if (!gclass->context.class_inst->is_open)
return NULL;
inst = mono_metadata_inflate_generic_inst (gclass->context.class_inst, context);
if (inst != gclass->context.class_inst)
gclass = mono_metadata_lookup_generic_class (gclass->container_class, inst, gclass->is_dynamic);
if (gclass == type->data.generic_class)
return NULL;
nt = dup_type (type, type);
nt->data.generic_class = gclass;
return nt;
}
case MONO_TYPE_CLASS:
case MONO_TYPE_VALUETYPE: {
MonoClass *klass = type->data.klass;
MonoGenericContainer *container = klass->generic_container;
MonoGenericInst *inst;
MonoGenericClass *gclass = NULL;
MonoType *nt;
if (!container)
return NULL;
/* We can't use context->class_inst directly, since it can have more elements */
inst = mono_metadata_inflate_generic_inst (container->context.class_inst, context);
gclass = mono_metadata_lookup_generic_class (klass, inst, klass->image->dynamic);
nt = dup_type (type, type);
nt->type = MONO_TYPE_GENERICINST;
nt->data.generic_class = gclass;
return nt;
}
default:
return NULL;
}
return NULL;
}
MonoGenericContext *
mono_generic_class_get_context (MonoGenericClass *gclass)
{
return &gclass->context;
}
MonoGenericContext *
mono_class_get_context (MonoClass *class)
{
return class->generic_class ? mono_generic_class_get_context (class->generic_class) : NULL;
}
/*
* mono_class_inflate_generic_type:
* @type: a type
* @context: a generics context
*
* Instantiate the generic type @type, using the generics context @context.
*
* Returns: the instantiated type
*/
MonoType*
mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
{
MonoType *inflated = inflate_generic_type (type, context);
if (!inflated)
return dup_type (type, type);
mono_stats.inflated_type_count++;
return inflated;
}
static MonoGenericContext
inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflate_with)
{
MonoGenericInst *class_inst = NULL;
MonoGenericInst *method_inst = NULL;
MonoGenericContext res;
if (context->class_inst)
class_inst = mono_metadata_inflate_generic_inst (context->class_inst, inflate_with);
if (context->method_inst)
method_inst = mono_metadata_inflate_generic_inst (context->method_inst, inflate_with);
res.class_inst = class_inst;
res.method_inst = method_inst;
return res;
}
/*
* mono_class_inflate_generic_method:
* @method: a generic method
* @context: a generics context
*
* Instantiate the generic method @method using the generics context @context.
*
* Returns: the new instantiated method
*/
MonoMethod *
mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context)
{
return mono_class_inflate_generic_method_full (method, NULL, context);
}
/**
* mono_class_inflate_generic_method:
*
* Instantiate method @method with the generic context @context.
* BEWARE: All non-trivial fields are invalid, including klass, signature, and header.
* Use mono_method_signature () and mono_method_get_header () to get the correct values.
*/
MonoMethod*
mono_class_inflate_generic_method_full (MonoMethod *method, MonoClass *klass_hint, MonoGenericContext *context)
{
MonoMethod *result;
MonoMethodInflated *iresult, *cached;
MonoMethodSignature *sig;
MonoGenericContext tmp_context;
/* The `method' has already been instantiated before => we need to peel out the instantiation and create a new context */
while (method->is_inflated) {
MonoGenericContext *method_context = mono_method_get_context (method);
MonoMethodInflated *imethod = (MonoMethodInflated *) method;
tmp_context = inflate_generic_context (method_context, context);
context = &tmp_context;
if (mono_metadata_generic_context_equal (method_context, context))
return method;
method = imethod->declaring;
}
if (!method->generic_container && !method->klass->generic_container)
return method;
mono_stats.inflated_method_count++;
iresult = g_new0 (MonoMethodInflated, 1);
iresult->context = *context;
iresult->declaring = method;
mono_loader_lock ();
cached = mono_method_inflated_lookup (iresult, FALSE);
if (cached) {
mono_loader_unlock ();
g_free (iresult);
return (MonoMethod*)cached;
}
sig = mono_method_signature (method);
if (sig->pinvoke) {
iresult->method.pinvoke = *(MonoMethodPInvoke*)method;
} else {
iresult->method.normal = *(MonoMethodNormal*)method;
iresult->method.normal.header = NULL;
}
result = (MonoMethod *) iresult;
result->is_inflated = 1;
/* result->generic_container = NULL; */
result->signature = NULL;
if (!klass_hint || !klass_hint->generic_class ||
klass_hint->generic_class->container_class != method->klass ||
klass_hint->generic_class->context.class_inst != context->class_inst)
klass_hint = NULL;
if (method->klass->generic_container)
result->klass = klass_hint;
if (!result->klass) {
MonoType *inflated = inflate_generic_type (&method->klass->byval_arg, context);
result->klass = inflated ? mono_class_from_mono_type (inflated) : method->klass;
}
if (context->method_inst)
result->generic_container = NULL;
else if (method->generic_container)
iresult->context.method_inst = method->generic_container->context.method_inst;
mono_method_inflated_lookup (iresult, TRUE);
mono_loader_unlock ();
return result;
}
/**
* mono_get_inflated_method:
*
* Obsolete. We keep it around since it's mentioned in the public API.
*/
MonoMethod*
mono_get_inflated_method (MonoMethod *method)
{
return method;
}
MonoGenericContext*
mono_method_get_context (MonoMethod *method)
{
MonoMethodInflated *imethod;
if (!method->is_inflated)
return NULL;
imethod = (MonoMethodInflated *) method;
return &imethod->context;
}
/**
* mono_class_find_enum_basetype:
* @class: The enum class
*
* Determine the basetype of an enum by iterating through its fields. We do this
* in a separate function since it is cheaper than calling mono_class_setup_fields.
*/
static MonoType*
mono_class_find_enum_basetype (MonoClass *class)
{
MonoImage *m = class->image;
const int top = class->field.count;
int i;
g_assert (class->enumtype);
/*
* Fetch all the field information.
*/
for (i = 0; i < top; i++){
const char *sig;
guint32 cols [MONO_FIELD_SIZE];
int idx = class->field.first + i;
MonoGenericContainer *container = NULL;
MonoType *ftype;
/* class->field.first and idx points into the fieldptr table */
mono_metadata_decode_table_row (m, MONO_TABLE_FIELD, idx, cols, MONO_FIELD_SIZE);
sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
mono_metadata_decode_value (sig, &sig);
/* FIELD signature == 0x06 */
g_assert (*sig == 0x06);
if (class->generic_container)
container = class->generic_container;
else if (class->generic_class) {
MonoClass *gklass = class->generic_class->container_class;
container = gklass->generic_container;
g_assert (container);
}
ftype = mono_metadata_parse_type_full (m, container, MONO_PARSE_FIELD, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
if (!ftype)
return NULL;
if (class->generic_class) {
ftype = mono_class_inflate_generic_type (ftype, mono_class_get_context (class));
ftype->attrs = cols [MONO_FIELD_FLAGS];
}
if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC))
return ftype;
}
return NULL;
}
/**
* mono_class_setup_fields:
* @class: The class to initialize
*
* Initializes the class->fields.
* LOCKING: Assumes the loader lock is held.
*/
static void
mono_class_setup_fields (MonoClass *class)
{
MonoImage *m = class->image;
int top = class->field.count;
guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
int i, blittable = TRUE;
guint32 real_size = 0;
guint32 packing_size = 0;
gboolean explicit_size;
MonoClassField *field;
MonoGenericContainer *container = NULL;
MonoClass *gklass = NULL;
if (class->size_inited)
return;
if (class->generic_class) {
MonoClass *gklass = class->generic_class->container_class;
mono_class_setup_fields (gklass);
top = gklass->field.count;
}
class->instance_size = 0;
if (!class->rank)
class->sizes.class_size = 0;
if (class->parent) {
/* For generic instances, class->parent might not have been initialized */
mono_class_init (class->parent);
if (!class->parent->size_inited)
mono_class_setup_fields (class->parent);
class->instance_size += class->parent->instance_size;
class->min_align = class->parent->min_align;
/* we use |= since it may have been set already */
class->has_references |= class->parent->has_references;
blittable = class->parent->blittable;
} else {
class->instance_size = sizeof (MonoObject);
class->min_align = 1;
}
/* Get the real size */
explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
if (explicit_size) {
g_assert ((packing_size & 0xfffffff0) == 0);
class->packing_size = packing_size;
real_size += class->instance_size;
}
if (!top) {
if (explicit_size && real_size) {
class->instance_size = MAX (real_size, class->instance_size);
}
class->size_inited = 1;
class->blittable = blittable;
return;
}
if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
blittable = FALSE;
/* Prevent infinite loops if the class references itself */
class->size_inited = 1;
class->fields = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoClassField) * top);
if (class->generic_container) {
container = class->generic_container;
} else if (class->generic_class) {
gklass = class->generic_class->container_class;
container = gklass->generic_container;
g_assert (container);
mono_class_setup_fields (gklass);
}
/*
* Fetch all the field information.
*/
for (i = 0; i < top; i++){
int idx = class->field.first + i;
field = &class->fields [i];
field->parent = class;
if (class->generic_class) {
MonoClassField *gfield = &gklass->fields [i];
MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
ifield->generic_type = gfield->type;
field->name = gfield->name;
field->generic_info = ifield;
field->type = mono_class_inflate_generic_type (gfield->type, mono_class_get_context (class));
field->type->attrs = gfield->type->attrs;
if (mono_field_is_deleted (field))
continue;
field->offset = gfield->offset;
field->data = gfield->data;
} else {
guint32 rva;
const char *sig;
guint32 cols [MONO_FIELD_SIZE];
/* class->field.first and idx points into the fieldptr table */
mono_metadata_decode_table_row (m, MONO_TABLE_FIELD, idx, cols, MONO_FIELD_SIZE);
/* The name is needed for fieldrefs */
field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
mono_metadata_decode_value (sig, &sig);
/* FIELD signature == 0x06 */
g_assert (*sig == 0x06);
field->type = mono_metadata_parse_type_full (m, container, MONO_PARSE_FIELD, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
if (!field->type) {
mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
break;
}
if (mono_field_is_deleted (field))
continue;
if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
guint32 offset;
mono_metadata_field_info (m, idx, &offset, NULL, NULL);
field->offset = offset;
if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
g_warning ("%s not initialized correctly (missing field layout info for %s)",
class->name, field->name);
}
if (field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
mono_metadata_field_info (m, idx, NULL, &rva, NULL);
if (!rva)
g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
field->data = mono_image_rva_map (class->image, rva);
}
}
/* Only do these checks if we still think this type is blittable */
if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
blittable = FALSE;
} else {
MonoClass *field_class = mono_class_from_mono_type (field->type);
if (!field_class || !field_class->blittable)
blittable = FALSE;
}
}
if (class->enumtype && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
class->enum_basetype = field->type;
class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
blittable = class->element_class->blittable;
}
/* The def_value of fields is compute lazily during vtable creation */
}
if (class == mono_defaults.string_class)
blittable = FALSE;
class->blittable = blittable;
if (class->enumtype && !class->enum_basetype) {
if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
G_BREAKPOINT ();
}
if (explicit_size && real_size) {
class->instance_size = MAX (real_size, class->instance_size);
}
if (class->exception_type)
return;
mono_class_layout_fields (class);
}
/**
* mono_class_setup_fields_locking:
* @class: The class to initialize
*
* Initializes the class->fields array of fields.
* Aquires the loader lock.
*/
static void
mono_class_setup_fields_locking (MonoClass *class)
{
mono_loader_lock ();
mono_class_setup_fields (class);
mono_loader_unlock ();
}
/*
* mono_class_has_references:
*
* Returns whenever @klass->has_references is set, initializing it if needed.
* Aquires the loader lock.
*/
static gboolean
mono_class_has_references (MonoClass *klass)
{
if (klass->init_pending) {
/* Be conservative */
return TRUE;
} else {
mono_class_init (klass);
return klass->has_references;
}
}
/* useful until we keep track of gc-references in corlib etc. */
#ifdef HAVE_SGEN_GC
#define IS_GC_REFERENCE(t) FALSE
#else
#define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U && class->image == mono_defaults.corlib)
#endif
/*
* mono_class_layout_fields:
* @class: a class
*
* Compute the placement of fields inside an object or struct, according to
* the layout rules and set the following fields in @class:
* - has_references (if the class contains instance references firled or structs that contain references)
* - has_static_refs (same, but for static fields)
* - instance_size (size of the object in memory)
* - class_size (size needed for the static fields)
* - size_inited (flag set when the instance_size is set)
*
* LOCKING: this is supposed to be called with the loader lock held.
*/
void
mono_class_layout_fields (MonoClass *class)
{
int i;
const int top = class->field.count;
guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
guint32 pass, passes, real_size;
gboolean gc_aware_layout = FALSE;
MonoClassField *field;