2 * metadata.c: Routines for accessing the metadata
5 * Miguel de Icaza (miguel@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * (C) 2001-2002 Ximian, Inc.
17 #include "tabledefs.h"
18 #include "mono-endian.h"
20 #include "tokentype.h"
21 #include "metadata-internals.h"
22 #include "class-internals.h"
25 static gboolean do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container,
26 const char *ptr, const char **rptr);
28 static gboolean do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only);
29 static gboolean mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only);
30 static gboolean mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only);
31 static gboolean _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2,
32 gboolean signature_only);
33 static void free_generic_inst (MonoGenericInst *ginst);
34 static void free_generic_class (MonoGenericClass *ginst);
35 static void free_inflated_method (MonoMethodInflated *method);
38 * This enumeration is used to describe the data types in the metadata
49 /* Index into Blob heap */
52 /* Index into String heap */
58 /* Pointer into a table */
61 /* HasConstant:Parent pointer (Param, Field or Property) */
64 /* HasCustomAttribute index. Indexes any table except CustomAttribute */
67 /* CustomAttributeType encoded index */
70 /* HasDeclSecurity index: TypeDef Method or Assembly */
73 /* Implementation coded index: File, Export AssemblyRef */
76 /* HasFieldMarshal coded index: Field or Param table */
79 /* MemberForwardedIndex: Field or Method */
82 /* TypeDefOrRef coded index: typedef, typeref, typespec */
85 /* MemberRefParent coded index: typeref, moduleref, method, memberref, typesepc, typedef */
88 /* MethodDefOrRef coded index: Method or Member Ref table */
91 /* HasSemantic coded index: Event or Property */
94 /* ResolutionScope coded index: Module, ModuleRef, AssemblytRef, TypeRef */
98 const static unsigned char TableSchemas [] = {
99 #define ASSEMBLY_SCHEMA_OFFSET 0
100 MONO_MT_UINT32, /* "HashId" }, */
101 MONO_MT_UINT16, /* "Major" }, */
102 MONO_MT_UINT16, /* "Minor" }, */
103 MONO_MT_UINT16, /* "BuildNumber" }, */
104 MONO_MT_UINT16, /* "RevisionNumber" }, */
105 MONO_MT_UINT32, /* "Flags" }, */
106 MONO_MT_BLOB_IDX, /* "PublicKey" }, */
107 MONO_MT_STRING_IDX, /* "Name" }, */
108 MONO_MT_STRING_IDX, /* "Culture" }, */
111 #define ASSEMBLYOS_SCHEMA_OFFSET ASSEMBLY_SCHEMA_OFFSET + 10
112 MONO_MT_UINT32, /* "OSPlatformID" }, */
113 MONO_MT_UINT32, /* "OSMajor" }, */
114 MONO_MT_UINT32, /* "OSMinor" }, */
117 #define ASSEMBLYPROC_SCHEMA_OFFSET ASSEMBLYOS_SCHEMA_OFFSET + 4
118 MONO_MT_UINT32, /* "Processor" }, */
121 #define ASSEMBLYREF_SCHEMA_OFFSET ASSEMBLYPROC_SCHEMA_OFFSET + 2
122 MONO_MT_UINT16, /* "Major" }, */
123 MONO_MT_UINT16, /* "Minor" }, */
124 MONO_MT_UINT16, /* "Build" }, */
125 MONO_MT_UINT16, /* "Revision" }, */
126 MONO_MT_UINT32, /* "Flags" }, */
127 MONO_MT_BLOB_IDX, /* "PublicKeyOrToken" }, */
128 MONO_MT_STRING_IDX, /* "Name" }, */
129 MONO_MT_STRING_IDX, /* "Culture" }, */
130 MONO_MT_BLOB_IDX, /* "HashValue" }, */
133 #define ASSEMBLYREFOS_SCHEMA_OFFSET ASSEMBLYREF_SCHEMA_OFFSET + 10
134 MONO_MT_UINT32, /* "OSPlatformID" }, */
135 MONO_MT_UINT32, /* "OSMajorVersion" }, */
136 MONO_MT_UINT32, /* "OSMinorVersion" }, */
137 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
140 #define ASSEMBLYREFPROC_SCHEMA_OFFSET ASSEMBLYREFOS_SCHEMA_OFFSET + 5
141 MONO_MT_UINT32, /* "Processor" }, */
142 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
145 #define CLASS_LAYOUT_SCHEMA_OFFSET ASSEMBLYREFPROC_SCHEMA_OFFSET + 3
146 MONO_MT_UINT16, /* "PackingSize" }, */
147 MONO_MT_UINT32, /* "ClassSize" }, */
148 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
151 #define CONSTANT_SCHEMA_OFFSET CLASS_LAYOUT_SCHEMA_OFFSET + 4
152 MONO_MT_UINT8, /* "Type" }, */
153 MONO_MT_UINT8, /* "PaddingZero" }, */
154 MONO_MT_CONST_IDX, /* "Parent" }, */
155 MONO_MT_BLOB_IDX, /* "Value" }, */
158 #define CUSTOM_ATTR_SCHEMA_OFFSET CONSTANT_SCHEMA_OFFSET + 5
159 MONO_MT_HASCAT_IDX, /* "Parent" }, */
160 MONO_MT_CAT_IDX, /* "Type" }, */
161 MONO_MT_BLOB_IDX, /* "Value" }, */
164 #define DECL_SEC_SCHEMA_OFFSET CUSTOM_ATTR_SCHEMA_OFFSET + 4
165 MONO_MT_UINT16, /* "Action" }, */
166 MONO_MT_HASDEC_IDX, /* "Parent" }, */
167 MONO_MT_BLOB_IDX, /* "PermissionSet" }, */
170 #define EVENTMAP_SCHEMA_OFFSET DECL_SEC_SCHEMA_OFFSET + 4
171 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
172 MONO_MT_TABLE_IDX, /* "EventList:Event" }, */
175 #define EVENT_SCHEMA_OFFSET EVENTMAP_SCHEMA_OFFSET + 3
176 MONO_MT_UINT16, /* "EventFlags#EventAttribute" }, */
177 MONO_MT_STRING_IDX, /* "Name" }, */
178 MONO_MT_TABLE_IDX, /* "EventType" }, TypeDef or TypeRef */
181 #define EVENT_POINTER_SCHEMA_OFFSET EVENT_SCHEMA_OFFSET + 4
182 MONO_MT_TABLE_IDX, /* "Event" }, */
185 #define EXPORTED_TYPE_SCHEMA_OFFSET EVENT_POINTER_SCHEMA_OFFSET + 2
186 MONO_MT_UINT32, /* "Flags" }, */
187 MONO_MT_TABLE_IDX, /* "TypeDefId" }, */
188 MONO_MT_STRING_IDX, /* "TypeName" }, */
189 MONO_MT_STRING_IDX, /* "TypeNameSpace" }, */
190 MONO_MT_IMPL_IDX, /* "Implementation" }, */
193 #define FIELD_SCHEMA_OFFSET EXPORTED_TYPE_SCHEMA_OFFSET + 6
194 MONO_MT_UINT16, /* "Flags" }, */
195 MONO_MT_STRING_IDX, /* "Name" }, */
196 MONO_MT_BLOB_IDX, /* "Signature" }, */
199 #define FIELD_LAYOUT_SCHEMA_OFFSET FIELD_SCHEMA_OFFSET + 4
200 MONO_MT_UINT32, /* "Offset" }, */
201 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
204 #define FIELD_MARSHAL_SCHEMA_OFFSET FIELD_LAYOUT_SCHEMA_OFFSET + 3
205 MONO_MT_HFM_IDX, /* "Parent" }, */
206 MONO_MT_BLOB_IDX, /* "NativeType" }, */
209 #define FIELD_RVA_SCHEMA_OFFSET FIELD_MARSHAL_SCHEMA_OFFSET + 3
210 MONO_MT_UINT32, /* "RVA" }, */
211 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
214 #define FIELD_POINTER_SCHEMA_OFFSET FIELD_RVA_SCHEMA_OFFSET + 3
215 MONO_MT_TABLE_IDX, /* "Field" }, */
218 #define FILE_SCHEMA_OFFSET FIELD_POINTER_SCHEMA_OFFSET + 2
219 MONO_MT_UINT32, /* "Flags" }, */
220 MONO_MT_STRING_IDX, /* "Name" }, */
221 MONO_MT_BLOB_IDX, /* "Value" }, */
224 #define IMPLMAP_SCHEMA_OFFSET FILE_SCHEMA_OFFSET + 4
225 MONO_MT_UINT16, /* "MappingFlag" }, */
226 MONO_MT_MF_IDX, /* "MemberForwarded" }, */
227 MONO_MT_STRING_IDX, /* "ImportName" }, */
228 MONO_MT_TABLE_IDX, /* "ImportScope:ModuleRef" }, */
231 #define IFACEMAP_SCHEMA_OFFSET IMPLMAP_SCHEMA_OFFSET + 5
232 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
233 MONO_MT_TDOR_IDX, /* "Interface=TypeDefOrRef" }, */
236 #define MANIFEST_SCHEMA_OFFSET IFACEMAP_SCHEMA_OFFSET + 3
237 MONO_MT_UINT32, /* "Offset" }, */
238 MONO_MT_UINT32, /* "Flags" }, */
239 MONO_MT_STRING_IDX, /* "Name" }, */
240 MONO_MT_IMPL_IDX, /* "Implementation" }, */
243 #define MEMBERREF_SCHEMA_OFFSET MANIFEST_SCHEMA_OFFSET + 5
244 MONO_MT_MRP_IDX, /* "Class" }, */
245 MONO_MT_STRING_IDX, /* "Name" }, */
246 MONO_MT_BLOB_IDX, /* "Signature" }, */
249 #define METHOD_SCHEMA_OFFSET MEMBERREF_SCHEMA_OFFSET + 4
250 MONO_MT_UINT32, /* "RVA" }, */
251 MONO_MT_UINT16, /* "ImplFlags#MethodImplAttributes" }, */
252 MONO_MT_UINT16, /* "Flags#MethodAttribute" }, */
253 MONO_MT_STRING_IDX, /* "Name" }, */
254 MONO_MT_BLOB_IDX, /* "Signature" }, */
255 MONO_MT_TABLE_IDX, /* "ParamList:Param" }, */
258 #define METHOD_IMPL_SCHEMA_OFFSET METHOD_SCHEMA_OFFSET + 7
259 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
260 MONO_MT_MDOR_IDX, /* "MethodBody" }, */
261 MONO_MT_MDOR_IDX, /* "MethodDeclaration" }, */
264 #define METHOD_SEMA_SCHEMA_OFFSET METHOD_IMPL_SCHEMA_OFFSET + 4
265 MONO_MT_UINT16, /* "MethodSemantic" }, */
266 MONO_MT_TABLE_IDX, /* "Method:Method" }, */
267 MONO_MT_HS_IDX, /* "Association" }, */
270 #define METHOD_POINTER_SCHEMA_OFFSET METHOD_SEMA_SCHEMA_OFFSET + 4
271 MONO_MT_TABLE_IDX, /* "Method" }, */
274 #define MODULE_SCHEMA_OFFSET METHOD_POINTER_SCHEMA_OFFSET + 2
275 MONO_MT_UINT16, /* "Generation" }, */
276 MONO_MT_STRING_IDX, /* "Name" }, */
277 MONO_MT_GUID_IDX, /* "MVID" }, */
278 MONO_MT_GUID_IDX, /* "EncID" }, */
279 MONO_MT_GUID_IDX, /* "EncBaseID" }, */
282 #define MODULEREF_SCHEMA_OFFSET MODULE_SCHEMA_OFFSET + 6
283 MONO_MT_STRING_IDX, /* "Name" }, */
286 #define NESTED_CLASS_SCHEMA_OFFSET MODULEREF_SCHEMA_OFFSET + 2
287 MONO_MT_TABLE_IDX, /* "NestedClass:TypeDef" }, */
288 MONO_MT_TABLE_IDX, /* "EnclosingClass:TypeDef" }, */
291 #define PARAM_SCHEMA_OFFSET NESTED_CLASS_SCHEMA_OFFSET + 3
292 MONO_MT_UINT16, /* "Flags" }, */
293 MONO_MT_UINT16, /* "Sequence" }, */
294 MONO_MT_STRING_IDX, /* "Name" }, */
297 #define PARAM_POINTER_SCHEMA_OFFSET PARAM_SCHEMA_OFFSET + 4
298 MONO_MT_TABLE_IDX, /* "Param" }, */
301 #define PROPERTY_SCHEMA_OFFSET PARAM_POINTER_SCHEMA_OFFSET + 2
302 MONO_MT_UINT16, /* "Flags" }, */
303 MONO_MT_STRING_IDX, /* "Name" }, */
304 MONO_MT_BLOB_IDX, /* "Type" }, */
307 #define PROPERTY_POINTER_SCHEMA_OFFSET PROPERTY_SCHEMA_OFFSET + 4
308 MONO_MT_TABLE_IDX, /* "Property" }, */
311 #define PROPERTY_MAP_SCHEMA_OFFSET PROPERTY_POINTER_SCHEMA_OFFSET + 2
312 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
313 MONO_MT_TABLE_IDX, /* "PropertyList:Property" }, */
316 #define STDALON_SIG_SCHEMA_OFFSET PROPERTY_MAP_SCHEMA_OFFSET + 3
317 MONO_MT_BLOB_IDX, /* "Signature" }, */
320 #define TYPEDEF_SCHEMA_OFFSET STDALON_SIG_SCHEMA_OFFSET + 2
321 MONO_MT_UINT32, /* "Flags" }, */
322 MONO_MT_STRING_IDX, /* "Name" }, */
323 MONO_MT_STRING_IDX, /* "Namespace" }, */
324 MONO_MT_TDOR_IDX, /* "Extends" }, */
325 MONO_MT_TABLE_IDX, /* "FieldList:Field" }, */
326 MONO_MT_TABLE_IDX, /* "MethodList:Method" }, */
329 #define TYPEREF_SCHEMA_OFFSET TYPEDEF_SCHEMA_OFFSET + 7
330 MONO_MT_RS_IDX, /* "ResolutionScope=ResolutionScope" }, */
331 MONO_MT_STRING_IDX, /* "Name" }, */
332 MONO_MT_STRING_IDX, /* "Namespace" }, */
335 #define TYPESPEC_SCHEMA_OFFSET TYPEREF_SCHEMA_OFFSET + 4
336 MONO_MT_BLOB_IDX, /* "Signature" }, */
339 #define GENPARAM_SCHEMA_OFFSET TYPESPEC_SCHEMA_OFFSET + 2
340 MONO_MT_UINT16, /* "Number" }, */
341 MONO_MT_UINT16, /* "Flags" }, */
342 MONO_MT_TABLE_IDX, /* "Owner" }, TypeDef or MethodDef */
343 MONO_MT_STRING_IDX, /* "Name" }, */
346 #define METHOD_SPEC_SCHEMA_OFFSET GENPARAM_SCHEMA_OFFSET + 5
347 MONO_MT_MDOR_IDX, /* "Method" }, */
348 MONO_MT_BLOB_IDX, /* "Signature" }, */
351 #define GEN_CONSTRAINT_SCHEMA_OFFSET METHOD_SPEC_SCHEMA_OFFSET + 3
352 MONO_MT_TABLE_IDX, /* "GenericParam" }, */
353 MONO_MT_TDOR_IDX, /* "Constraint" }, */
356 #define NULL_SCHEMA_OFFSET GEN_CONSTRAINT_SCHEMA_OFFSET + 3
360 /* Must be the same order as MONO_TABLE_* */
361 const static unsigned char
362 table_description [] = {
363 MODULE_SCHEMA_OFFSET,
364 TYPEREF_SCHEMA_OFFSET,
365 TYPEDEF_SCHEMA_OFFSET,
366 FIELD_POINTER_SCHEMA_OFFSET,
368 METHOD_POINTER_SCHEMA_OFFSET,
369 METHOD_SCHEMA_OFFSET,
370 PARAM_POINTER_SCHEMA_OFFSET,
372 IFACEMAP_SCHEMA_OFFSET,
373 MEMBERREF_SCHEMA_OFFSET, /* 0xa */
374 CONSTANT_SCHEMA_OFFSET,
375 CUSTOM_ATTR_SCHEMA_OFFSET,
376 FIELD_MARSHAL_SCHEMA_OFFSET,
377 DECL_SEC_SCHEMA_OFFSET,
378 CLASS_LAYOUT_SCHEMA_OFFSET,
379 FIELD_LAYOUT_SCHEMA_OFFSET, /* 0x10 */
380 STDALON_SIG_SCHEMA_OFFSET,
381 EVENTMAP_SCHEMA_OFFSET,
382 EVENT_POINTER_SCHEMA_OFFSET,
384 PROPERTY_MAP_SCHEMA_OFFSET,
385 PROPERTY_POINTER_SCHEMA_OFFSET,
386 PROPERTY_SCHEMA_OFFSET,
387 METHOD_SEMA_SCHEMA_OFFSET,
388 METHOD_IMPL_SCHEMA_OFFSET,
389 MODULEREF_SCHEMA_OFFSET, /* 0x1a */
390 TYPESPEC_SCHEMA_OFFSET,
391 IMPLMAP_SCHEMA_OFFSET,
392 FIELD_RVA_SCHEMA_OFFSET,
395 ASSEMBLY_SCHEMA_OFFSET, /* 0x20 */
396 ASSEMBLYPROC_SCHEMA_OFFSET,
397 ASSEMBLYOS_SCHEMA_OFFSET,
398 ASSEMBLYREF_SCHEMA_OFFSET,
399 ASSEMBLYREFPROC_SCHEMA_OFFSET,
400 ASSEMBLYREFOS_SCHEMA_OFFSET,
402 EXPORTED_TYPE_SCHEMA_OFFSET,
403 MANIFEST_SCHEMA_OFFSET,
404 NESTED_CLASS_SCHEMA_OFFSET,
405 GENPARAM_SCHEMA_OFFSET, /* 0x2a */
406 METHOD_SPEC_SCHEMA_OFFSET,
407 GEN_CONSTRAINT_SCHEMA_OFFSET
410 #ifdef HAVE_ARRAY_ELEM_INIT
411 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
412 #define MSGSTRFIELD1(line) str##line
413 static const struct msgstr_t {
414 #define TABLEDEF(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
415 #include "mono/cil/tables.def"
418 #define TABLEDEF(a,b) b,
419 #include "mono/cil/tables.def"
422 static const gint16 tableidx [] = {
423 #define TABLEDEF(a,b) [a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
424 #include "mono/cil/tables.def"
429 #define TABLEDEF(a,b) b,
430 static const char* const
431 mono_tables_names [] = {
432 #include "mono/cil/tables.def"
438 /* Auxiliary structure used for caching inflated signatures */
440 MonoMethodSignature *sig;
441 MonoGenericContext context;
442 } MonoInflatedMethodSignature;
445 * mono_meta_table_name:
446 * @table: table index
448 * Returns: the name for the @table index
451 mono_meta_table_name (int table)
453 if ((table < 0) || (table > MONO_TABLE_LAST))
456 #ifdef HAVE_ARRAY_ELEM_INIT
457 return (const char*)&tablestr + tableidx [table];
459 return mono_tables_names [table];
463 /* The guy who wrote the spec for this should not be allowed near a
466 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
467 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
468 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
469 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
470 inverse of this mapping.
473 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
474 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
476 /* Reference: Partition II - 23.2.6 */
478 * mono_metadata_compute_size:
479 * @meta: metadata context
480 * @tableindex: metadata table number
481 * @result_bitfield: pointer to guint32 where to store additional info
483 * mono_metadata_compute_size() computes the lenght in bytes of a single
484 * row in a metadata table. The size of each column is encoded in the
485 * @result_bitfield return value along with the number of columns in the table.
486 * the resulting bitfield should be handed to the mono_metadata_table_size()
487 * and mono_metadata_table_count() macros.
488 * This is a Mono runtime internal only function.
491 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
493 guint32 bitfield = 0;
494 int size = 0, field_size = 0;
497 const unsigned char *description = TableSchemas + table_description [tableindex];
499 for (i = 0; (code = description [i]) != MONO_MT_END; i++){
502 field_size = 4; break;
505 field_size = 2; break;
508 field_size = 1; break;
510 case MONO_MT_BLOB_IDX:
511 field_size = meta->idx_blob_wide ? 4 : 2; break;
513 case MONO_MT_STRING_IDX:
514 field_size = meta->idx_string_wide ? 4 : 2; break;
516 case MONO_MT_GUID_IDX:
517 field_size = meta->idx_guid_wide ? 4 : 2; break;
519 case MONO_MT_TABLE_IDX:
520 /* Uhm, a table index can point to other tables besides the current one
521 * so, it's not correct to use the rowcount of the current table to
522 * get the size for this column - lupus
524 switch (tableindex) {
525 case MONO_TABLE_ASSEMBLYREFOS:
527 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
528 case MONO_TABLE_ASSEMBLYPROCESSOR:
530 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
531 case MONO_TABLE_CLASSLAYOUT:
533 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
534 case MONO_TABLE_EVENTMAP:
535 g_assert (i == 0 || i == 1);
536 field_size = i ? idx_size (MONO_TABLE_EVENT):
537 idx_size(MONO_TABLE_TYPEDEF);
539 case MONO_TABLE_EVENT:
541 field_size = MAX (idx_size (MONO_TABLE_TYPEDEF), idx_size(MONO_TABLE_TYPEREF));
542 field_size = MAX (field_size, idx_size(MONO_TABLE_TYPESPEC));
544 case MONO_TABLE_EVENT_POINTER:
546 field_size = idx_size (MONO_TABLE_EVENT); break;
547 case MONO_TABLE_EXPORTEDTYPE:
549 /* the index is in another metadata file, so it must be 4 */
550 field_size = 4; break;
551 case MONO_TABLE_FIELDLAYOUT:
553 field_size = idx_size (MONO_TABLE_FIELD); break;
554 case MONO_TABLE_FIELDRVA:
556 field_size = idx_size (MONO_TABLE_FIELD); break;
557 case MONO_TABLE_FIELD_POINTER:
559 field_size = idx_size (MONO_TABLE_FIELD); break;
560 case MONO_TABLE_IMPLMAP:
562 field_size = idx_size (MONO_TABLE_MODULEREF); break;
563 case MONO_TABLE_INTERFACEIMPL:
565 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
566 case MONO_TABLE_METHOD:
568 field_size = idx_size (MONO_TABLE_PARAM); break;
569 case MONO_TABLE_METHODIMPL:
571 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
572 case MONO_TABLE_METHODSEMANTICS:
574 field_size = idx_size (MONO_TABLE_METHOD); break;
575 case MONO_TABLE_METHOD_POINTER:
577 field_size = idx_size (MONO_TABLE_METHOD); break;
578 case MONO_TABLE_NESTEDCLASS:
579 g_assert (i == 0 || i == 1);
580 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
581 case MONO_TABLE_PARAM_POINTER:
583 field_size = idx_size (MONO_TABLE_PARAM); break;
584 case MONO_TABLE_PROPERTYMAP:
585 g_assert (i == 0 || i == 1);
586 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
587 idx_size(MONO_TABLE_TYPEDEF);
589 case MONO_TABLE_PROPERTY_POINTER:
591 field_size = idx_size (MONO_TABLE_PROPERTY); break;
592 case MONO_TABLE_TYPEDEF:
593 g_assert (i == 4 || i == 5);
594 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
595 idx_size(MONO_TABLE_METHOD);
597 case MONO_TABLE_GENERICPARAM:
598 g_assert (i == 2 || i == 4 || i == 5);
600 field_size = MAX (idx_size (MONO_TABLE_METHOD), idx_size (MONO_TABLE_TYPEDEF));
602 field_size = idx_size (MONO_TABLE_TYPEDEF);
604 field_size = idx_size (MONO_TABLE_TYPEDEF);
607 case MONO_TABLE_GENERICPARAMCONSTRAINT:
609 field_size = idx_size (MONO_TABLE_GENERICPARAM);
613 g_assert_not_reached ();
618 * HasConstant: ParamDef, FieldDef, Property
620 case MONO_MT_CONST_IDX:
621 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
622 meta->tables [MONO_TABLE_FIELD].rows);
623 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
625 /* 2 bits to encode tag */
626 field_size = rtsize (n, 16-2);
630 * HasCustomAttribute: points to any table but
633 case MONO_MT_HASCAT_IDX:
635 * We believe that since the signature and
636 * permission are indexing the Blob heap,
637 * we should consider the blob size first
639 /* I'm not a believer - lupus
640 if (meta->idx_blob_wide){
645 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
646 meta->tables [MONO_TABLE_FIELD].rows);
647 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
648 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
649 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
650 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
651 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
652 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
653 n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
654 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
655 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
656 n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
657 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
658 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
659 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
660 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
661 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
662 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
663 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
665 /* 5 bits to encode */
666 field_size = rtsize (n, 16-5);
670 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
671 * MemberRef and String.
673 case MONO_MT_CAT_IDX:
674 /* String is a heap, if it is wide, we know the size */
676 if (meta->idx_string_wide){
681 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
682 meta->tables [MONO_TABLE_TYPEDEF].rows);
683 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
684 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
686 /* 3 bits to encode */
687 field_size = rtsize (n, 16-3);
691 * HasDeclSecurity: Typedef, MethodDef, Assembly
693 case MONO_MT_HASDEC_IDX:
694 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
695 meta->tables [MONO_TABLE_METHOD].rows);
696 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
698 /* 2 bits to encode */
699 field_size = rtsize (n, 16-2);
703 * Implementation: File, AssemblyRef, ExportedType
705 case MONO_MT_IMPL_IDX:
706 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
707 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
708 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
710 /* 2 bits to encode tag */
711 field_size = rtsize (n, 16-2);
715 * HasFieldMarshall: FieldDef, ParamDef
717 case MONO_MT_HFM_IDX:
718 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
719 meta->tables [MONO_TABLE_PARAM].rows);
721 /* 1 bit used to encode tag */
722 field_size = rtsize (n, 16-1);
726 * MemberForwarded: FieldDef, MethodDef
729 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
730 meta->tables [MONO_TABLE_METHOD].rows);
732 /* 1 bit used to encode tag */
733 field_size = rtsize (n, 16-1);
737 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
739 * It is TypeDef, _TypeRef_, TypeSpec, instead.
741 case MONO_MT_TDOR_IDX:
742 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
743 meta->tables [MONO_TABLE_TYPEREF].rows);
744 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
746 /* 2 bits to encode */
747 field_size = rtsize (n, 16-2);
751 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
753 case MONO_MT_MRP_IDX:
754 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
755 meta->tables [MONO_TABLE_TYPEREF].rows);
756 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
757 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
758 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
759 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
761 /* 3 bits to encode */
762 field_size = rtsize (n, 16 - 3);
766 * MethodDefOrRef: MethodDef, MemberRef
768 case MONO_MT_MDOR_IDX:
769 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
770 meta->tables [MONO_TABLE_MEMBERREF].rows);
772 /* 1 bit used to encode tag */
773 field_size = rtsize (n, 16-1);
777 * HasSemantics: Property, Event
780 n = MAX (meta->tables [MONO_TABLE_PROPERTY].rows,
781 meta->tables [MONO_TABLE_EVENT].rows);
783 /* 1 bit used to encode tag */
784 field_size = rtsize (n, 16-1);
788 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
791 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
792 meta->tables [MONO_TABLE_MODULEREF].rows);
793 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
794 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
796 /* 2 bits used to encode tag (ECMA spec claims 3) */
797 field_size = rtsize (n, 16 - 2);
802 * encode field size as follows (we just need to
809 bitfield |= (field_size-1) << shift;
812 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
815 *result_bitfield = (i << 24) | bitfield;
820 * mono_metadata_compute_table_bases:
821 * @meta: metadata context to compute table values
823 * Computes the table bases for the metadata structure.
824 * This is an internal function used by the image loader code.
827 mono_metadata_compute_table_bases (MonoImage *meta)
830 const char *base = meta->tables_base;
832 for (i = 0; i < MONO_TABLE_NUM; i++) {
833 MonoTableInfo *table = &meta->tables [i];
834 if (table->rows == 0)
837 table->row_size = mono_metadata_compute_size (meta, i, &table->size_bitfield);
839 base += table->rows * table->row_size;
844 * mono_metadata_locate:
845 * @meta: metadata context
846 * @table: table code.
847 * @idx: index of element to retrieve from @table.
849 * Returns: a pointer to the @idx element in the metadata table
850 * whose code is @table.
853 mono_metadata_locate (MonoImage *meta, int table, int idx)
855 /* idx == 0 refers always to NULL */
856 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
858 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
862 * mono_metadata_locate_token:
863 * @meta: metadata context
864 * @token: metadata token
866 * Returns: a pointer to the data in the metadata represented by the
870 mono_metadata_locate_token (MonoImage *meta, guint32 token)
872 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
876 * mono_metadata_string_heap:
877 * @meta: metadata context
878 * @index: index into the string heap.
880 * Returns: an in-memory pointer to the @index in the string heap.
883 mono_metadata_string_heap (MonoImage *meta, guint32 index)
885 g_return_val_if_fail (index < meta->heap_strings.size, "");
886 return meta->heap_strings.data + index;
890 * mono_metadata_user_string:
891 * @meta: metadata context
892 * @index: index into the user string heap.
894 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
897 mono_metadata_user_string (MonoImage *meta, guint32 index)
899 g_return_val_if_fail (index < meta->heap_us.size, "");
900 return meta->heap_us.data + index;
904 * mono_metadata_blob_heap:
905 * @meta: metadata context
906 * @index: index into the blob.
908 * Returns: an in-memory pointer to the @index in the Blob heap.
911 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
913 g_return_val_if_fail (index < meta->heap_blob.size, "");
914 return meta->heap_blob.data + index;
918 * mono_metadata_guid_heap:
919 * @meta: metadata context
920 * @index: index into the guid heap.
922 * Returns: an in-memory pointer to the @index in the guid heap.
925 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
928 index *= 16; /* adjust for guid size and 1-based index */
929 g_return_val_if_fail (index < meta->heap_guid.size, "");
930 return meta->heap_guid.data + index;
933 static const unsigned char *
934 dword_align (const unsigned char *ptr)
936 #if SIZEOF_VOID_P == 8
937 return (const unsigned char *) (((guint64) (ptr + 3)) & ~3);
939 return (const unsigned char *) (((guint32) (ptr + 3)) & ~3);
944 * mono_metadata_decode_row:
945 * @t: table to extract information from.
946 * @idx: index in table.
947 * @res: array of @res_size cols to store the results in
949 * This decompresses the metadata element @idx in table @t
950 * into the guint32 @res array that has res_size elements
953 mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res_size)
955 guint32 bitfield = t->size_bitfield;
956 int i, count = mono_metadata_table_count (bitfield);
957 const char *data = t->base + idx * t->row_size;
959 g_assert (res_size == count);
961 for (i = 0; i < count; i++) {
962 int n = mono_metadata_table_size (bitfield, i);
966 res [i] = *data; break;
968 res [i] = read16 (data); break;
970 res [i] = read32 (data); break;
972 g_assert_not_reached ();
979 * mono_metadata_decode_row_col:
980 * @t: table to extract information from.
981 * @idx: index for row in table.
982 * @col: column in the row.
984 * This function returns the value of column @col from the @idx
985 * row in the table @t.
988 mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col)
990 guint32 bitfield = t->size_bitfield;
992 register const char *data = t->base + idx * t->row_size;
995 g_assert (col < mono_metadata_table_count (bitfield));
997 n = mono_metadata_table_size (bitfield, 0);
998 for (i = 0; i < col; ++i) {
1000 n = mono_metadata_table_size (bitfield, i + 1);
1006 return read16 (data);
1008 return read32 (data);
1010 g_assert_not_reached ();
1016 * mono_metadata_decode_blob_size:
1017 * @ptr: pointer to a blob object
1018 * @rptr: the new position of the pointer
1020 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
1022 * Returns: the size of the blob object
1025 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
1027 const unsigned char *ptr = (const unsigned char *)xptr;
1030 if ((*ptr & 0x80) == 0){
1031 size = ptr [0] & 0x7f;
1033 } else if ((*ptr & 0x40) == 0){
1034 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
1037 size = ((ptr [0] & 0x1f) << 24) +
1049 * mono_metadata_decode_value:
1050 * @ptr: pointer to decode from
1051 * @rptr: the new position of the pointer
1053 * This routine decompresses 32-bit values as specified in the "Blob and
1054 * Signature" section (22.2)
1056 * Returns: the decoded value
1059 mono_metadata_decode_value (const char *_ptr, const char **rptr)
1061 const unsigned char *ptr = (const unsigned char *) _ptr;
1062 unsigned char b = *ptr;
1065 if ((b & 0x80) == 0){
1068 } else if ((b & 0x40) == 0){
1069 len = ((b & 0x3f) << 8 | ptr [1]);
1072 len = ((b & 0x1f) << 24) |
1085 * mono_metadata_decode_signed_value:
1086 * @ptr: pointer to decode from
1087 * @rptr: the new position of the pointer
1089 * This routine decompresses 32-bit signed values
1090 * (not specified in the spec)
1092 * Returns: the decoded value
1095 mono_metadata_decode_signed_value (const char *ptr, const char **rptr)
1097 guint32 uval = mono_metadata_decode_value (ptr, rptr);
1098 gint32 ival = uval >> 1;
1101 /* ival is a truncated 2's complement negative number. */
1103 /* 6 bits = 7 bits for compressed representation (top bit is '0') - 1 sign bit */
1106 /* 13 bits = 14 bits for compressed representation (top bits are '10') - 1 sign bit */
1107 return ival - 0x2000;
1108 if (ival < 0x10000000)
1109 /* 28 bits = 29 bits for compressed representation (top bits are '110') - 1 sign bit */
1110 return ival - 0x10000000;
1111 g_assert (ival < 0x20000000);
1112 g_warning ("compressed signed value appears to use 29 bits for compressed representation: %x (raw: %8x)", ival, uval);
1113 return ival - 0x20000000;
1117 * Translates the given 1-based index into the Method, Field, Event, or Param tables
1118 * using the *Ptr tables in uncompressed metadata, if they are available.
1120 * FIXME: The caller is not forced to call this function, which is error-prone, since
1121 * forgetting to call it would only show up as a bug on uncompressed metadata.
1124 mono_metadata_translate_token_index (MonoImage *image, int table, guint32 idx)
1126 if (!image->uncompressed_metadata)
1130 case MONO_TABLE_METHOD:
1131 if (image->tables [MONO_TABLE_METHOD_POINTER].rows)
1132 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_METHOD_POINTER], idx - 1, MONO_METHOD_POINTER_METHOD);
1135 case MONO_TABLE_FIELD:
1136 if (image->tables [MONO_TABLE_FIELD_POINTER].rows)
1137 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_FIELD_POINTER], idx - 1, MONO_FIELD_POINTER_FIELD);
1140 case MONO_TABLE_EVENT:
1141 if (image->tables [MONO_TABLE_EVENT_POINTER].rows)
1142 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_EVENT_POINTER], idx - 1, MONO_EVENT_POINTER_EVENT);
1145 case MONO_TABLE_PROPERTY:
1146 if (image->tables [MONO_TABLE_PROPERTY_POINTER].rows)
1147 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_PROPERTY_POINTER], idx - 1, MONO_PROPERTY_POINTER_PROPERTY);
1150 case MONO_TABLE_PARAM:
1151 if (image->tables [MONO_TABLE_PARAM_POINTER].rows)
1152 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_PARAM_POINTER], idx - 1, MONO_PARAM_POINTER_PARAM);
1161 * mono_metadata_decode_table_row:
1163 * Same as mono_metadata_decode_row, but takes an IMAGE+TABLE ID pair, and takes
1164 * uncompressed metadata into account, so it should be used to access the
1165 * Method, Field, Param and Event tables when the access is made from metadata, i.e.
1166 * IDX is retrieved from a metadata table, like MONO_TYPEDEF_FIELD_LIST.
1169 mono_metadata_decode_table_row (MonoImage *image, int table, int idx, guint32 *res, int res_size)
1171 if (image->uncompressed_metadata)
1172 idx = mono_metadata_translate_token_index (image, table, idx + 1) - 1;
1174 mono_metadata_decode_row (&image->tables [table], idx, res, res_size);
1178 * mono_metadata_decode_table_row_col:
1180 * Same as mono_metadata_decode_row_col, but takes an IMAGE+TABLE ID pair, and takes
1181 * uncompressed metadata into account, so it should be used to access the
1182 * Method, Field, Param and Event tables.
1184 guint32 mono_metadata_decode_table_row_col (MonoImage *image, int table, int idx, guint col)
1186 if (image->uncompressed_metadata)
1187 idx = mono_metadata_translate_token_index (image, table, idx + 1) - 1;
1189 return mono_metadata_decode_row_col (&image->tables [table], idx, col);
1193 * mono_metadata_parse_typedef_or_ref:
1194 * @m: a metadata context.
1195 * @ptr: a pointer to an encoded TypedefOrRef in @m
1196 * @rptr: pointer updated to match the end of the decoded stream
1198 * Returns: a token valid in the @m metadata decoded from
1199 * the compressed representation.
1202 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1205 token = mono_metadata_decode_value (ptr, &ptr);
1208 return mono_metadata_token_from_dor (token);
1212 * mono_metadata_parse_custom_mod:
1213 * @m: a metadata context.
1214 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1215 * @ptr: a pointer to (possibly) the start of a custom modifier list
1216 * @rptr: pointer updated to match the end of the decoded stream
1218 * Checks if @ptr points to a type custom modifier compressed representation.
1220 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1223 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1225 MonoCustomMod local;
1226 if ((*ptr == MONO_TYPE_CMOD_OPT) || (*ptr == MONO_TYPE_CMOD_REQD)) {
1229 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1230 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1237 * mono_metadata_parse_array_full:
1238 * @m: a metadata context.
1239 * @ptr: a pointer to an encoded array description.
1240 * @rptr: pointer updated to match the end of the decoded stream
1242 * Decodes the compressed array description found in the metadata @m at @ptr.
1244 * Returns: a #MonoArrayType structure describing the array type
1245 * and dimensions. Memory is allocated from the image mempool.
1247 * LOCKING: Assumes the loader lock is held.
1250 mono_metadata_parse_array_full (MonoImage *m, MonoGenericContainer *container,
1251 const char *ptr, const char **rptr)
1254 MonoArrayType *array = mono_mempool_alloc0 (m->mempool, sizeof (MonoArrayType));
1257 etype = mono_metadata_parse_type_full (m, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
1260 array->eklass = mono_class_from_mono_type (etype);
1261 array->rank = mono_metadata_decode_value (ptr, &ptr);
1263 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1264 if (array->numsizes)
1265 array->sizes = g_new0 (int, array->numsizes);
1266 for (i = 0; i < array->numsizes; ++i)
1267 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1269 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1270 if (array->numlobounds)
1271 array->lobounds = g_new0 (int, array->numlobounds);
1272 for (i = 0; i < array->numlobounds; ++i)
1273 array->lobounds [i] = mono_metadata_decode_signed_value (ptr, &ptr);
1281 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1283 return mono_metadata_parse_array_full (m, NULL, ptr, rptr);
1287 * mono_metadata_free_array:
1288 * @array: array description
1290 * Frees the array description returned from mono_metadata_parse_array().
1293 mono_metadata_free_array (MonoArrayType *array)
1295 g_free (array->sizes);
1296 g_free (array->lobounds);
1301 * need to add common field and param attributes combinations:
1304 * public static literal
1307 * private static literal
1309 static const MonoType
1311 /* data, attrs, type, nmods, byref, pinned */
1312 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1313 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1314 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1315 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1316 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1317 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1318 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1319 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1320 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1321 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1322 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1323 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1324 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1325 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1326 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1327 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1328 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1329 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1330 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1331 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1332 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1333 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1334 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1335 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1336 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1337 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1338 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1339 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 0, 0},
1340 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 1, 0},
1341 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1342 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1343 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1344 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1345 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1348 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1350 static GHashTable *type_cache = NULL;
1351 static GHashTable *generic_inst_cache = NULL;
1352 static GHashTable *generic_class_cache = NULL;
1353 static int next_generic_inst_id = 0;
1356 * Protected by the loader lock.
1357 * It has a MonoMethodInflated* as key and value.
1358 * The key lookup will just access the declaring and context fields
1360 static GHashTable *generic_method_cache = NULL;
1363 * Protected by the loader lock.
1364 * It has a MonoInflatedMethodSignature* as key and value.
1366 static GHashTable *generic_signature_cache = NULL;
1368 static guint mono_generic_class_hash (gconstpointer data);
1371 * MonoTypes with modifies are never cached, so we never check or use that field.
1374 mono_type_hash (gconstpointer data)
1376 const MonoType *type = (const MonoType *) data;
1377 if (type->type == MONO_TYPE_GENERICINST)
1378 return mono_generic_class_hash (type->data.generic_class);
1380 return type->type | (type->byref << 8) | (type->attrs << 9);
1384 mono_type_equal (gconstpointer ka, gconstpointer kb)
1386 const MonoType *a = (const MonoType *) ka;
1387 const MonoType *b = (const MonoType *) kb;
1389 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1391 /* need other checks */
1396 mono_generic_inst_hash (gconstpointer data)
1398 const MonoGenericInst *ginst = (const MonoGenericInst *) data;
1402 for (i = 0; i < ginst->type_argc; ++i) {
1404 hash += mono_metadata_type_hash (ginst->type_argv [i]);
1407 return hash ^ (ginst->is_open << 8);
1411 mono_generic_inst_equal (gconstpointer ka, gconstpointer kb)
1413 const MonoGenericInst *a = (const MonoGenericInst *) ka;
1414 const MonoGenericInst *b = (const MonoGenericInst *) kb;
1417 if (a->is_open != b->is_open || a->type_argc != b->type_argc)
1419 for (i = 0; i < a->type_argc; ++i) {
1420 if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], FALSE))
1427 mono_generic_class_hash (gconstpointer data)
1429 const MonoGenericClass *gclass = (const MonoGenericClass *) data;
1430 guint hash = mono_metadata_type_hash (&gclass->container_class->byval_arg);
1433 hash += mono_metadata_generic_context_hash (&gclass->context);
1439 mono_generic_class_equal (gconstpointer ka, gconstpointer kb)
1441 const MonoGenericClass *a = (const MonoGenericClass *) ka;
1442 const MonoGenericClass *b = (const MonoGenericClass *) kb;
1444 return _mono_metadata_generic_class_equal (a, b, FALSE);
1448 * mono_metadata_init:
1450 * Initialize the global variables of this module.
1451 * This is a Mono runtime internal function.
1454 mono_metadata_init (void)
1458 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1459 generic_inst_cache = g_hash_table_new_full (mono_generic_inst_hash, mono_generic_inst_equal, NULL, (GDestroyNotify)free_generic_inst);
1460 generic_class_cache = g_hash_table_new_full (mono_generic_class_hash, mono_generic_class_equal, NULL, (GDestroyNotify)free_generic_class);
1462 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1463 g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
1467 * mono_metadata_cleanup:
1469 * Free all resources used by this module.
1470 * This is a Mono runtime internal function.
1473 mono_metadata_cleanup (void)
1475 g_hash_table_destroy (type_cache);
1476 g_hash_table_destroy (generic_inst_cache);
1477 g_hash_table_destroy (generic_class_cache);
1478 if (generic_method_cache)
1479 g_hash_table_destroy (generic_method_cache);
1480 if (generic_signature_cache)
1481 g_hash_table_destroy (generic_signature_cache);
1483 generic_inst_cache = NULL;
1484 generic_class_cache = NULL;
1485 generic_method_cache = NULL;
1486 generic_signature_cache = NULL;
1490 * mono_metadata_parse_type:
1491 * @m: metadata context
1492 * @mode: king of type that may be found at @ptr
1493 * @opt_attrs: optional attributes to store in the returned type
1494 * @ptr: pointer to the type representation
1495 * @rptr: pointer updated to match the end of the decoded stream
1497 * Decode a compressed type description found at @ptr in @m.
1498 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1499 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1500 * This function can be used to decode type descriptions in method signatures,
1501 * field signatures, locals signatures etc.
1503 * To parse a generic type, `generic_container' points to the current class'es
1504 * (the `generic_container' field in the MonoClass) or the current generic method's
1505 * (the `generic_container' field in the MonoMethodNormal) generic container.
1506 * When we encounter any MONO_TYPE_VAR or MONO_TYPE_MVAR's, they're looked up in
1507 * this MonoGenericContainer.
1508 * This is a Mono runtime internal function.
1510 * LOCKING: Acquires the loader lock.
1512 * Returns: a #MonoType structure representing the decoded type.
1515 mono_metadata_parse_type_full (MonoImage *m, MonoGenericContainer *container, MonoParseTypeMode mode,
1516 short opt_attrs, const char *ptr, const char **rptr)
1518 MonoType *type, *cached;
1520 gboolean byref = FALSE;
1521 gboolean pinned = FALSE;
1522 const char *tmp_ptr;
1526 mono_loader_lock ();
1528 * According to the spec, custom modifiers should come before the byref
1529 * flag, but the IL produced by ilasm from the following signature:
1530 * object modopt(...) &
1531 * starts with a byref flag, followed by the modifiers. (bug #49802)
1532 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1533 * it would be better to treat byref as real type constructor instead of
1535 * Also, pinned should come before anything else, but some MSV++ produced
1536 * assemblies violate this (#bug 61990).
1539 /* Count the modifiers first */
1544 case MONO_TYPE_PINNED:
1545 case MONO_TYPE_BYREF:
1548 case MONO_TYPE_CMOD_REQD:
1549 case MONO_TYPE_CMOD_OPT:
1551 mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1559 type = mono_mempool_alloc0 (m->mempool, sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1560 type->num_mods = count;
1562 g_warning ("got more than 64 modifiers in type");
1565 memset (type, 0, sizeof (MonoType));
1568 /* Parse pinned, byref and custom modifiers */
1573 case MONO_TYPE_PINNED:
1577 case MONO_TYPE_BYREF:
1581 case MONO_TYPE_CMOD_REQD:
1582 case MONO_TYPE_CMOD_OPT:
1583 mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1591 type->attrs = opt_attrs;
1592 type->byref = byref;
1593 type->pinned = pinned ? 1 : 0;
1595 if (!do_mono_metadata_parse_type (type, m, container, ptr, &ptr)) {
1596 mono_loader_unlock ();
1603 if (!type->num_mods) {
1604 /* no need to free type here, because it is on the stack */
1605 if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
1606 MonoType *ret = type->byref ? &type->data.klass->this_arg : &type->data.klass->byval_arg;
1608 /* Consider the case:
1610 class Foo<T> { class Bar {} }
1611 class Test : Foo<Test>.Bar {}
1613 When Foo<Test> is being expanded, 'Test' isn't yet initialized. It's actually in
1614 a really pristine state: it doesn't even know whether 'Test' is a reference or a value type.
1616 We ensure that the MonoClass is in a state that we can canonicalize to:
1618 klass->byval_arg.data.klass == klass
1619 klass->this_arg.data.klass == klass
1621 If we can't canonicalize 'type', it doesn't matter, since later users of 'type' will do it.
1623 LOCKING: even though we don't explicitly hold a lock, in the problematic case 'ret' is a field
1624 of a MonoClass which currently holds the loader lock. 'type' is local.
1626 if (ret->data.klass == type->data.klass) {
1627 mono_loader_unlock ();
1631 /* No need to use locking since nobody is modifying the hash table */
1632 if ((cached = g_hash_table_lookup (type_cache, type))) {
1633 mono_loader_unlock ();
1638 /* printf ("%x %x %c %s\n", type->attrs, type->num_mods, type->pinned ? 'p' : ' ', mono_type_full_name (type)); */
1640 if (type == &stype) {
1641 type = mono_mempool_alloc (m->mempool, sizeof (MonoType));
1642 memcpy (type, &stype, sizeof (MonoType));
1644 mono_loader_unlock ();
1649 * LOCKING: Assumes the loader lock is held.
1652 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
1653 const char *ptr, const char **rptr)
1655 return mono_metadata_parse_type_full (m, NULL, mode, opt_attrs, ptr, rptr);
1659 * mono_metadata_get_param_attrs:
1661 * Return the parameter attributes for the method whose MethodDef index is DEF. The
1662 * returned memory needs to be freed by the caller. If all the param attributes are
1663 * 0, then NULL is returned.
1666 mono_metadata_get_param_attrs (MonoImage *m, int def)
1668 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1669 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1670 guint32 cols [MONO_PARAM_SIZE];
1671 guint lastp, i, param_index = mono_metadata_decode_row_col (&m->tables [MONO_TABLE_METHOD], def - 1, MONO_METHOD_PARAMLIST);
1674 if (def < methodt->rows)
1675 lastp = mono_metadata_decode_row_col (&m->tables [MONO_TABLE_METHOD], def, MONO_METHOD_PARAMLIST);
1677 lastp = paramt->rows + 1;
1679 for (i = param_index; i < lastp; ++i) {
1680 mono_metadata_decode_row (&m->tables [MONO_TABLE_PARAM], i - 1, cols, MONO_PARAM_SIZE);
1681 if (cols [MONO_PARAM_FLAGS]) {
1683 pattrs = g_new0 (int, 1 + (lastp - param_index));
1684 pattrs [cols [MONO_PARAM_SEQUENCE]] = cols [MONO_PARAM_FLAGS];
1692 * mono_metadata_parse_signature_full:
1693 * @image: metadata context
1694 * @generic_container: generic container
1695 * @toke: metadata token
1697 * Decode a method signature stored in the STANDALONESIG table
1699 * LOCKING: Assumes the loader lock is held.
1701 * Returns: a MonoMethodSignature describing the signature.
1703 MonoMethodSignature*
1704 mono_metadata_parse_signature_full (MonoImage *image, MonoGenericContainer *generic_container, guint32 token)
1706 MonoTableInfo *tables = image->tables;
1707 guint32 idx = mono_metadata_token_index (token);
1712 return mono_lookup_dynamic_token (image, token, NULL);
1714 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1716 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1718 ptr = mono_metadata_blob_heap (image, sig);
1719 mono_metadata_decode_blob_size (ptr, &ptr);
1721 return mono_metadata_parse_method_signature_full (image, generic_container, 0, ptr, NULL);
1725 * mono_metadata_parse_signature:
1726 * @image: metadata context
1727 * @toke: metadata token
1729 * Decode a method signature stored in the STANDALONESIG table
1731 * Returns: a MonoMethodSignature describing the signature.
1733 MonoMethodSignature*
1734 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1736 return mono_metadata_parse_signature_full (image, NULL, token);
1740 * mono_metadata_signature_alloc:
1741 * @image: metadata context
1742 * @nparmas: number of parameters in the signature
1744 * Allocate a MonoMethodSignature structure with the specified number of params.
1745 * The return type and the params types need to be filled later.
1746 * This is a Mono runtime internal function.
1748 * LOCKING: Assumes the loader lock is held.
1750 * Returns: the new MonoMethodSignature structure.
1752 MonoMethodSignature*
1753 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1755 MonoMethodSignature *sig;
1757 mono_loader_lock ();
1758 sig = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1759 sig->param_count = nparams;
1760 sig->sentinelpos = -1;
1761 mono_loader_unlock ();
1767 * mono_metadata_signature_dup:
1768 * @sig: method signature
1770 * Duplicate an existing MonoMethodSignature so it can be modified.
1771 * This is a Mono runtime internal function.
1773 * Returns: the new MonoMethodSignature structure.
1775 MonoMethodSignature*
1776 mono_metadata_signature_dup (MonoMethodSignature *sig)
1780 sigsize = sizeof (MonoMethodSignature) + (sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *);
1781 return g_memdup (sig, sigsize);
1785 * mono_metadata_parse_method_signature:
1786 * @m: metadata context
1787 * @generic_container: generics container
1788 * @def: the MethodDef index or 0 for Ref signatures.
1789 * @ptr: pointer to the signature metadata representation
1790 * @rptr: pointer updated to match the end of the decoded stream
1792 * Decode a method signature stored at @ptr.
1793 * This is a Mono runtime internal function.
1795 * LOCKING: Assumes the loader lock is held.
1797 * Returns: a MonoMethodSignature describing the signature.
1799 MonoMethodSignature *
1800 mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContainer *container,
1801 int def, const char *ptr, const char **rptr)
1803 MonoMethodSignature *method;
1804 int i, *pattrs = NULL;
1805 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1806 guint32 gen_param_count = 0;
1807 gboolean is_open = FALSE;
1810 gen_param_count = 1;
1815 call_convention = *ptr & 0x0F;
1817 if (gen_param_count)
1818 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1819 param_count = mono_metadata_decode_value (ptr, &ptr);
1822 pattrs = mono_metadata_get_param_attrs (m, def);
1823 method = mono_metadata_signature_alloc (m, param_count);
1824 method->hasthis = hasthis;
1825 method->explicit_this = explicit_this;
1826 method->call_convention = call_convention;
1827 method->generic_param_count = gen_param_count;
1829 if (call_convention != 0xa) {
1830 method->ret = mono_metadata_parse_type_full (m, container, MONO_PARSE_RET, pattrs ? pattrs [0] : 0, ptr, &ptr);
1832 mono_metadata_free_method_signature (method);
1836 is_open = mono_class_is_open_constructed_type (method->ret);
1839 for (i = 0; i < method->param_count; ++i) {
1840 if (*ptr == MONO_TYPE_SENTINEL) {
1841 if (method->call_convention != MONO_CALL_VARARG || def)
1842 g_error ("found sentinel for methoddef or no vararg method");
1843 if (method->sentinelpos >= 0)
1844 g_error ("found sentinel twice in the same signature");
1845 method->sentinelpos = i;
1848 method->params [i] = mono_metadata_parse_type_full (m, container, MONO_PARSE_PARAM, pattrs ? pattrs [i+1] : 0, ptr, &ptr);
1849 if (!method->params [i]) {
1850 mono_metadata_free_method_signature (method);
1855 is_open = mono_class_is_open_constructed_type (method->params [i]);
1858 /* The sentinel could be missing if the caller does not pass any additional arguments */
1859 if (!def && method->call_convention == MONO_CALL_VARARG && method->sentinelpos < 0)
1860 method->sentinelpos = method->param_count;
1862 method->has_type_parameters = is_open;
1864 if (def && (method->call_convention == MONO_CALL_VARARG))
1865 method->sentinelpos = method->param_count;
1872 * Add signature to a cache and increase ref count...
1879 * mono_metadata_parse_method_signature:
1880 * @m: metadata context
1881 * @def: the MethodDef index or 0 for Ref signatures.
1882 * @ptr: pointer to the signature metadata representation
1883 * @rptr: pointer updated to match the end of the decoded stream
1885 * Decode a method signature stored at @ptr.
1886 * This is a Mono runtime internal function.
1888 * LOCKING: Assumes the loader lock is held.
1890 * Returns: a MonoMethodSignature describing the signature.
1892 MonoMethodSignature *
1893 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1895 return mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr);
1899 * mono_metadata_free_method_signature:
1900 * @sig: signature to destroy
1902 * Free the memory allocated in the signature @sig.
1903 * This method needs to be robust and work also on partially-built
1904 * signatures, so it does extra checks.
1907 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1909 /* Everything is allocated from mempools */
1913 mono_metadata_free_type (sig->ret);
1914 for (i = 0; i < sig->param_count; ++i) {
1915 if (sig->params [i])
1916 mono_metadata_free_type (sig->params [i]);
1922 mono_metadata_free_inflated_signature (MonoMethodSignature *sig)
1926 /* Allocated in inflate_generic_signature () */
1928 mono_metadata_free_type (sig->ret);
1929 for (i = 0; i < sig->param_count; ++i) {
1930 if (sig->params [i])
1931 mono_metadata_free_type (sig->params [i]);
1933 /* FIXME: The signature is allocated from a mempool */
1938 inflated_method_equal (gconstpointer a, gconstpointer b)
1940 const MonoMethodInflated *ma = a;
1941 const MonoMethodInflated *mb = b;
1942 if (ma->declaring != mb->declaring)
1944 return mono_metadata_generic_context_equal (&ma->context, &mb->context);
1948 inflated_method_hash (gconstpointer a)
1950 const MonoMethodInflated *ma = a;
1951 return mono_metadata_generic_context_hash (&ma->context) ^ mono_aligned_addr_hash (ma->declaring);
1955 inflated_signature_equal (gconstpointer a, gconstpointer b)
1957 const MonoInflatedMethodSignature *sig1 = a;
1958 const MonoInflatedMethodSignature *sig2 = b;
1960 /* sig->sig is assumed to be canonized */
1961 if (sig1->sig != sig2->sig)
1963 /* The generic instances are canonized */
1964 return mono_metadata_generic_context_equal (&sig1->context, &sig2->context);
1968 inflated_signature_hash (gconstpointer a)
1970 const MonoInflatedMethodSignature *sig = a;
1972 /* sig->sig is assumed to be canonized */
1973 return mono_metadata_generic_context_hash (&sig->context) ^ mono_aligned_addr_hash (sig->sig);
1977 dump_ginst (MonoGenericInst *ginst)
1982 g_print ("Ginst: <");
1983 for (i = 0; i < ginst->type_argc; ++i) {
1986 name = mono_type_get_name (ginst->type_argv [i]);
1987 g_print ("%s", name);
1993 static gboolean type_in_image (MonoType *type, MonoImage *image);
1996 signature_in_image (MonoMethodSignature *sig, MonoImage *image)
1998 gpointer iter = NULL;
2001 while ((p = mono_signature_get_params (sig, &iter)) != NULL)
2002 if (type_in_image (p, image))
2005 return type_in_image (mono_signature_get_return_type (sig), image);
2009 ginst_in_image (MonoGenericInst *ginst, MonoImage *image)
2013 for (i = 0; i < ginst->type_argc; ++i) {
2014 if (type_in_image (ginst->type_argv [i], image))
2022 gclass_in_image (MonoGenericClass *gclass, MonoImage *image)
2024 return gclass->container_class->image == image ||
2025 ginst_in_image (gclass->context.class_inst, image);
2029 type_in_image (MonoType *type, MonoImage *image)
2032 switch (type->type) {
2033 case MONO_TYPE_GENERICINST:
2034 return gclass_in_image (type->data.generic_class, image);
2036 type = type->data.type;
2038 case MONO_TYPE_SZARRAY:
2039 type = &type->data.klass->byval_arg;
2041 case MONO_TYPE_ARRAY:
2042 type = &type->data.array->eklass->byval_arg;
2044 case MONO_TYPE_FNPTR:
2045 return signature_in_image (type->data.method, image);
2047 /* At this point, we should've avoided all potential allocations in mono_class_from_mono_type () */
2048 return image == mono_class_from_mono_type (type)->image;
2055 } CleanForImageUserData;
2058 steal_gclass_in_image (gpointer key, gpointer value, gpointer data)
2060 MonoGenericClass *gclass = key;
2061 CleanForImageUserData *user_data = data;
2063 if (!gclass_in_image (gclass, user_data->image))
2066 user_data->list = g_slist_prepend (user_data->list, gclass);
2071 steal_ginst_in_image (gpointer key, gpointer value, gpointer data)
2073 MonoGenericInst *ginst = key;
2074 CleanForImageUserData *user_data = data;
2076 if (!ginst_in_image (ginst, user_data->image))
2079 user_data->list = g_slist_prepend (user_data->list, ginst);
2084 inflated_method_in_image (gpointer key, gpointer value, gpointer data)
2086 MonoImage *image = data;
2087 MonoMethodInflated *method = key;
2089 return method->declaring->klass->image == image ||
2090 (method->context.class_inst && ginst_in_image (method->context.class_inst, image)) ||
2091 (method->context.method_inst && ginst_in_image (method->context.method_inst, image));
2095 inflated_signature_in_image (gpointer key, gpointer value, gpointer data)
2097 MonoImage *image = data;
2098 MonoInflatedMethodSignature *sig = key;
2100 return /* signature_in_image (sig->sig, image) || */
2101 (sig->context.class_inst && ginst_in_image (sig->context.class_inst, image)) ||
2102 (sig->context.method_inst && ginst_in_image (sig->context.method_inst, image));
2106 mono_metadata_clean_for_image (MonoImage *image)
2108 CleanForImageUserData ginst_data, gclass_data;
2111 /* The data structures could reference each other so we delete them in two phases */
2112 ginst_data.image = gclass_data.image = image;
2113 ginst_data.list = gclass_data.list = NULL;
2115 mono_loader_lock ();
2116 /* Collect the items to delete and remove them from the hash table */
2117 g_hash_table_foreach_steal (generic_inst_cache, steal_ginst_in_image, &ginst_data);
2118 g_hash_table_foreach_steal (generic_class_cache, steal_gclass_in_image, &gclass_data);
2119 if (generic_method_cache)
2120 g_hash_table_foreach_remove (generic_method_cache, inflated_method_in_image, image);
2121 if (generic_signature_cache)
2122 g_hash_table_foreach_remove (generic_signature_cache, inflated_signature_in_image, image);
2123 /* Delete the removed items */
2124 for (l = ginst_data.list; l; l = l->next)
2125 free_generic_inst (l->data);
2126 for (l = gclass_data.list; l; l = l->next)
2127 free_generic_class (l->data);
2128 g_slist_free (ginst_data.list);
2129 g_slist_free (gclass_data.list);
2130 mono_loader_unlock ();
2134 free_inflated_method (MonoMethodInflated *imethod)
2137 MonoMethod *method = (MonoMethod*)imethod;
2139 if (method->signature)
2140 mono_metadata_free_inflated_signature (method->signature);
2142 if (!((method->flags & METHOD_ATTRIBUTE_ABSTRACT) || (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))) {
2143 MonoMethodNormal* mn = (MonoMethodNormal*) method;
2144 MonoMethodHeader *header = mn->header;
2147 /* Allocated in inflate_generic_header () */
2148 for (i = 0; i < header->num_locals; ++i)
2149 mono_metadata_free_type (header->locals [i]);
2150 g_free (header->clauses);
2159 free_generic_inst (MonoGenericInst *ginst)
2163 for (i = 0; i < ginst->type_argc; ++i)
2164 mono_metadata_free_type (ginst->type_argv [i]);
2165 g_free (ginst->type_argv);
2171 free_generic_class (MonoGenericClass *gclass)
2175 /* FIXME: The dynamic case */
2176 if (gclass->cached_class && !gclass->cached_class->image->dynamic) {
2177 MonoClass *class = gclass->cached_class;
2179 /* Allocated in mono_class_init () */
2180 g_free (class->methods);
2181 g_free (class->properties);
2182 /* Allocated in mono_class_setup_fields () */
2183 if (class->fields) {
2184 for (i = 0; i < class->field.count; ++i) {
2185 g_free (class->fields [i].generic_info);
2186 mono_metadata_free_type (class->fields [i].type);
2189 /* Allocated in mono_generic_class_get_class () */
2190 g_free (class->interfaces);
2197 free_inflated_signature (MonoInflatedMethodSignature *sig)
2199 mono_metadata_free_inflated_signature (sig->sig);
2204 * LOCKING: assumes the loader lock is held.
2207 mono_method_inflated_lookup (MonoMethodInflated* method, gboolean cache)
2210 if (!generic_method_cache)
2211 generic_method_cache = g_hash_table_new_full (inflated_method_hash, inflated_method_equal, NULL, (GDestroyNotify)free_inflated_method);
2212 g_hash_table_insert (generic_method_cache, method, method);
2215 if (generic_method_cache)
2216 return g_hash_table_lookup (generic_method_cache, method);
2222 * mono_metadata_get_inflated_signature:
2224 * Given an inflated signature and a generic context, return a canonical copy of the
2225 * signature. The returned signature might be equal to SIG or it might be a cached copy.
2227 MonoMethodSignature *
2228 mono_metadata_get_inflated_signature (MonoMethodSignature *sig, MonoGenericContext *context)
2230 MonoInflatedMethodSignature helper;
2231 MonoInflatedMethodSignature *res;
2233 mono_loader_lock ();
2234 if (!generic_signature_cache)
2235 generic_signature_cache = g_hash_table_new_full (inflated_signature_hash, inflated_signature_equal, NULL, (GDestroyNotify)free_inflated_signature);
2238 helper.context.class_inst = context->class_inst;
2239 helper.context.method_inst = context->method_inst;
2240 res = g_hash_table_lookup (generic_signature_cache, &helper);
2242 res = g_new0 (MonoInflatedMethodSignature, 1);
2244 res->context.class_inst = context->class_inst;
2245 res->context.method_inst = context->method_inst;
2246 g_hash_table_insert (generic_signature_cache, res, res);
2249 mono_loader_unlock ();
2254 * mono_metadata_get_generic_inst:
2256 * Given a list of types, return a MonoGenericInst that represents that list.
2257 * The returned MonoGenericInst has its own copy of the list of types. The list
2258 * passed in the argument can be freed, modified or disposed of.
2262 mono_metadata_get_generic_inst (int type_argc, MonoType **type_argv)
2264 MonoGenericInst *ginst;
2265 MonoGenericInst helper;
2268 helper.type_argc = type_argc;
2269 helper.type_argv = type_argv;
2272 for (i = 0; i < type_argc; ++i)
2273 if (mono_class_is_open_constructed_type (type_argv [i]))
2275 helper.is_open = (i < type_argc);
2277 /*dump_ginst (&helper);*/
2278 mono_loader_lock ();
2279 ginst = g_hash_table_lookup (generic_inst_cache, &helper);
2281 mono_loader_unlock ();
2282 /*g_print (" found cached\n");*/
2286 ginst = g_new0 (MonoGenericInst, 1);
2287 ginst->type_argc = type_argc;
2288 ginst->type_argv = g_new (MonoType*, type_argc);
2289 ginst->id = ++next_generic_inst_id;
2290 ginst->is_open = helper.is_open;
2292 for (i = 0; i < type_argc; ++i)
2293 ginst->type_argv [i] = mono_metadata_type_dup (NULL, type_argv [i]);
2295 g_hash_table_insert (generic_inst_cache, ginst, ginst);
2297 mono_loader_unlock ();
2298 /*g_print (" inserted\n");*/
2303 * mono_metadata_lookup_generic_class:
2305 * Returns a MonoGenericClass with the given properties.
2309 mono_metadata_lookup_generic_class (MonoClass *container_class, MonoGenericInst *inst, gboolean is_dynamic)
2311 MonoGenericClass *gclass;
2313 MonoGenericClass helper;
2314 helper.container_class = container_class;
2315 helper.context.class_inst = inst;
2316 helper.context.method_inst = NULL;
2317 helper.is_dynamic = is_dynamic; /* We use this in a hash lookup, which does not attempt to downcast the pointer */
2318 helper.cached_class = NULL;
2320 mono_loader_lock ();
2322 gclass = g_hash_table_lookup (generic_class_cache, &helper);
2324 /* A tripwire just to keep us honest */
2325 g_assert (!helper.cached_class);
2328 mono_loader_unlock ();
2333 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
2334 gclass = &dgclass->generic_class;
2335 gclass->is_dynamic = 1;
2337 gclass = g_new0 (MonoGenericClass, 1);
2340 gclass->container_class = container_class;
2341 gclass->context.class_inst = inst;
2342 gclass->context.method_inst = NULL;
2344 g_hash_table_insert (generic_class_cache, gclass, gclass);
2346 mono_loader_unlock ();
2352 * mono_metadata_inflate_generic_inst:
2354 * Instantiate the generic instance @ginst with the context @context.
2358 mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context)
2360 MonoType **type_argv;
2361 MonoGenericInst *nginst;
2364 if (!ginst->is_open)
2367 type_argv = g_new0 (MonoType*, ginst->type_argc);
2369 for (i = 0; i < ginst->type_argc; i++)
2370 type_argv [i] = mono_class_inflate_generic_type (ginst->type_argv [i], context);
2372 nginst = mono_metadata_get_generic_inst (ginst->type_argc, type_argv);
2374 for (i = 0; i < ginst->type_argc; i++)
2375 mono_metadata_free_type (type_argv [i]);
2382 mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContainer *container,
2383 int count, const char *ptr, const char **rptr)
2385 MonoType **type_argv;
2386 MonoGenericInst *ginst;
2389 type_argv = g_new0 (MonoType*, count);
2391 for (i = 0; i < count; i++) {
2392 MonoType *t = mono_metadata_parse_type_full (m, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
2403 ginst = mono_metadata_get_generic_inst (count, type_argv);
2411 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContainer *container,
2412 const char *ptr, const char **rptr)
2414 MonoGenericInst *inst;
2419 gtype = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
2423 gklass = mono_class_from_mono_type (gtype);
2424 if (!gklass->generic_container)
2427 count = mono_metadata_decode_value (ptr, &ptr);
2428 inst = mono_metadata_parse_generic_inst (m, container, count, ptr, &ptr);
2435 type->data.generic_class = mono_metadata_lookup_generic_class (gklass, inst, FALSE);
2441 * @gc: The generic container to normalize
2442 * @type: The kind of generic parameters the resulting generic-container should contain
2445 static MonoGenericContainer *
2446 select_container (MonoGenericContainer *gc, MonoTypeEnum type)
2448 gboolean is_var = (type == MONO_TYPE_VAR);
2452 g_assert (is_var || type == MONO_TYPE_MVAR);
2455 if (gc->is_method || gc->parent)
2457 * The current MonoGenericContainer is a generic method -> its `parent'
2458 * points to the containing class'es container.
2467 * mono_metadata_parse_generic_param:
2468 * @generic_container: Our MonoClass's or MonoMethodNormal's MonoGenericContainer;
2469 * see mono_metadata_parse_type_full() for details.
2470 * Internal routine to parse a generic type parameter.
2471 * LOCKING: Assumes the loader lock is held.
2473 static MonoGenericParam *
2474 mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContainer *generic_container,
2475 MonoTypeEnum type, const char *ptr, const char **rptr)
2477 int index = mono_metadata_decode_value (ptr, &ptr);
2481 generic_container = select_container (generic_container, type);
2482 if (!generic_container) {
2483 /* Create dummy MonoGenericParam */
2484 MonoGenericParam *param = mono_mempool_alloc0 (m->mempool, sizeof (MonoGenericParam));
2485 param->name = mono_mempool_alloc0 (m->mempool, 8);
2486 sprintf ((char*)param->name, "%d", index);
2492 g_assert (index < generic_container->type_argc);
2493 return &generic_container->type_params [index];
2497 * do_mono_metadata_parse_type:
2498 * @type: MonoType to be filled in with the return value
2500 * @generic_context: generics_context
2501 * @ptr: pointer to the encoded type
2502 * @rptr: pointer where the end of the encoded type is saved
2504 * Internal routine used to "fill" the contents of @type from an
2505 * allocated pointer. This is done this way to avoid doing too
2506 * many mini-allocations (particularly for the MonoFieldType which
2507 * most of the time is just a MonoType, but sometimes might be augmented).
2509 * This routine is used by mono_metadata_parse_type and
2510 * mono_metadata_parse_field_type
2512 * This extracts a Type as specified in Partition II (22.2.12)
2514 * Returns: FALSE if the type could not be loaded
2517 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container,
2518 const char *ptr, const char **rptr)
2521 type->type = mono_metadata_decode_value (ptr, &ptr);
2523 switch (type->type){
2524 case MONO_TYPE_VOID:
2525 case MONO_TYPE_BOOLEAN:
2526 case MONO_TYPE_CHAR:
2539 case MONO_TYPE_STRING:
2540 case MONO_TYPE_OBJECT:
2541 case MONO_TYPE_TYPEDBYREF:
2543 case MONO_TYPE_VALUETYPE:
2544 case MONO_TYPE_CLASS: {
2546 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
2547 type->data.klass = mono_class_get (m, token);
2548 if (!type->data.klass)
2552 case MONO_TYPE_SZARRAY: {
2553 MonoType *etype = mono_metadata_parse_type_full (m, container, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
2556 type->data.klass = mono_class_from_mono_type (etype);
2560 type->data.type = mono_metadata_parse_type_full (m, container, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
2561 if (!type->data.type)
2564 case MONO_TYPE_FNPTR:
2565 type->data.method = mono_metadata_parse_method_signature_full (m, container, 0, ptr, &ptr);
2567 case MONO_TYPE_ARRAY:
2568 type->data.array = mono_metadata_parse_array_full (m, container, ptr, &ptr);
2570 case MONO_TYPE_MVAR:
2572 type->data.generic_param = mono_metadata_parse_generic_param (m, container, type->type, ptr, &ptr);
2574 case MONO_TYPE_GENERICINST:
2575 ok = do_mono_metadata_parse_generic_class (type, m, container, ptr, &ptr);
2578 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
2587 * mono_metadata_free_type:
2588 * @type: type to free
2590 * Free the memory allocated for type @type which is allocated on the heap.
2593 mono_metadata_free_type (MonoType *type)
2595 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
2598 switch (type->type){
2599 case MONO_TYPE_OBJECT:
2600 case MONO_TYPE_STRING:
2601 if (!type->data.klass)
2604 case MONO_TYPE_CLASS:
2605 case MONO_TYPE_VALUETYPE:
2606 if (type == &type->data.klass->byval_arg || type == &type->data.klass->this_arg)
2610 mono_metadata_free_type (type->data.type);
2612 case MONO_TYPE_FNPTR:
2613 mono_metadata_free_method_signature (type->data.method);
2615 case MONO_TYPE_ARRAY:
2616 mono_metadata_free_array (type->data.array);
2625 hex_dump (const char *buffer, int base, int count)
2627 int show_header = 1;
2635 for (i = 0; i < count; i++){
2638 printf ("\n0x%08x: ", (unsigned char) base + i);
2640 printf ("%02x ", (unsigned char) (buffer [i]));
2647 * @mh: The Method header
2648 * @ptr: Points to the beginning of the Section Data (25.3)
2651 parse_section_data (MonoImage *m, MonoMethodHeader *mh, const unsigned char *ptr)
2653 unsigned char sect_data_flags;
2654 const unsigned char *sptr;
2656 guint32 sect_data_len;
2659 /* align on 32-bit boundary */
2660 sptr = ptr = dword_align (ptr);
2661 sect_data_flags = *ptr;
2664 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
2666 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
2669 sect_data_len = ptr [0];
2673 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
2674 hex_dump (sptr, 0, sect_data_len+8);
2675 g_print ("\nheader: ");
2676 hex_dump (sptr-4, 0, 4);
2680 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
2681 const unsigned char *p = dword_align (ptr);
2683 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
2684 /* we could just store a pointer if we don't need to byteswap */
2685 mh->clauses = mono_mempool_alloc0 (m->mempool, sizeof (MonoExceptionClause) * mh->num_clauses);
2686 for (i = 0; i < mh->num_clauses; ++i) {
2687 MonoExceptionClause *ec = &mh->clauses [i];
2690 ec->flags = read32 (p);
2691 ec->try_offset = read32 (p + 4);
2692 ec->try_len = read32 (p + 8);
2693 ec->handler_offset = read32 (p + 12);
2694 ec->handler_len = read32 (p + 16);
2695 tof_value = read32 (p + 20);
2698 ec->flags = read16 (p);
2699 ec->try_offset = read16 (p + 2);
2700 ec->try_len = *(p + 4);
2701 ec->handler_offset = read16 (p + 5);
2702 ec->handler_len = *(p + 7);
2703 tof_value = read32 (p + 8);
2706 if (ec->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2707 ec->data.filter_offset = tof_value;
2708 } else if (ec->flags == MONO_EXCEPTION_CLAUSE_NONE) {
2709 ec->data.catch_class = tof_value? mono_class_get (m, tof_value): 0;
2711 ec->data.catch_class = NULL;
2713 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
2717 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
2718 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
2725 * mono_metadata_parse_mh_full:
2726 * @m: metadata context
2727 * @generic_context: generics context
2728 * @ptr: pointer to the method header.
2730 * Decode the method header at @ptr, including pointer to the IL code,
2731 * info about local variables and optional exception tables.
2732 * This is a Mono runtime internal function.
2734 * LOCKING: Assumes the loader lock is held.
2736 * Returns: a MonoMethodHeader allocated from the image mempool.
2739 mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, const char *ptr)
2741 MonoMethodHeader *mh;
2742 unsigned char flags = *(const unsigned char *) ptr;
2743 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
2745 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
2746 const unsigned char *code;
2749 g_return_val_if_fail (ptr != NULL, NULL);
2751 mono_loader_lock ();
2753 case METHOD_HEADER_TINY_FORMAT:
2754 mh = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodHeader));
2757 local_var_sig_tok = 0;
2758 mh->code_size = flags >> 2;
2759 mh->code = (unsigned char*)ptr;
2760 mono_loader_unlock ();
2762 case METHOD_HEADER_TINY_FORMAT1:
2763 mh = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodHeader));
2766 local_var_sig_tok = 0;
2769 * The spec claims 3 bits, but the Beta2 is
2772 mh->code_size = flags >> 2;
2773 mh->code = (unsigned char*)ptr;
2774 mono_loader_unlock ();
2776 case METHOD_HEADER_FAT_FORMAT:
2777 fat_flags = read16 (ptr);
2779 hsize = (fat_flags >> 12) & 0xf;
2780 max_stack = read16 (ptr);
2782 code_size = read32 (ptr);
2784 local_var_sig_tok = read32 (ptr);
2787 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
2792 code = (unsigned char*)ptr;
2794 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
2798 * There are more sections
2800 ptr = (char*)code + code_size;
2803 mono_loader_unlock ();
2807 if (local_var_sig_tok) {
2808 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
2809 const char *locals_ptr;
2810 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
2811 int len=0, i, bsize;
2813 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
2814 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
2815 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
2816 if (*locals_ptr != 0x07)
2817 g_warning ("wrong signature for locals blob");
2819 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
2820 mh = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
2821 mh->num_locals = len;
2822 for (i = 0; i < len; ++i) {
2823 mh->locals [i] = mono_metadata_parse_type_full (
2824 m, container, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
2825 if (!mh->locals [i]) {
2826 mono_loader_unlock ();
2831 mh = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodHeader));
2834 mh->code_size = code_size;
2835 mh->max_stack = max_stack;
2836 mh->init_locals = init_locals;
2837 if (fat_flags & METHOD_HEADER_MORE_SECTS)
2838 parse_section_data (m, mh, (const unsigned char*)ptr);
2839 mono_loader_unlock ();
2844 * mono_metadata_parse_mh:
2845 * @generic_context: generics context
2846 * @ptr: pointer to the method header.
2848 * Decode the method header at @ptr, including pointer to the IL code,
2849 * info about local variables and optional exception tables.
2850 * This is a Mono runtime internal function.
2852 * Returns: a MonoMethodHeader.
2855 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
2857 MonoMethodHeader *res;
2859 mono_loader_lock ();
2861 res = mono_metadata_parse_mh_full (m, NULL, ptr);
2863 mono_loader_unlock ();
2869 * mono_metadata_free_mh:
2870 * @mh: a method header
2872 * Free the memory allocated for the method header.
2873 * This is a Mono runtime internal function.
2876 mono_metadata_free_mh (MonoMethodHeader *mh)
2878 /* Allocated from the mempool */
2882 * mono_method_header_get_code:
2883 * @header: a MonoMethodHeader pointer
2884 * @code_size: memory location for returning the code size
2885 * @max_stack: memory location for returning the max stack
2887 * Method header accessor to retreive info about the IL code properties:
2888 * a pointer to the IL code itself, the size of the code and the max number
2889 * of stack slots used by the code.
2891 * Returns: pointer to the IL code represented by the method header.
2893 const unsigned char*
2894 mono_method_header_get_code (MonoMethodHeader *header, guint32* code_size, guint32* max_stack)
2897 *code_size = header->code_size;
2899 *max_stack = header->max_stack;
2900 return header->code;
2904 * mono_method_header_get_locals:
2905 * @header: a MonoMethodHeader pointer
2906 * @num_locals: memory location for returning the number of local variables
2907 * @init_locals: memory location for returning the init_locals flag
2909 * Method header accessor to retreive info about the local variables:
2910 * an array of local types, the number of locals and whether the locals
2911 * are supposed to be initialized to 0 on method entry
2913 * Returns: pointer to an array of types of the local variables
2916 mono_method_header_get_locals (MonoMethodHeader *header, guint32* num_locals, gboolean *init_locals)
2919 *num_locals = header->num_locals;
2921 *init_locals = header->init_locals;
2922 return header->locals;
2926 * mono_method_header_get_num_clauses:
2927 * @header: a MonoMethodHeader pointer
2929 * Method header accessor to retreive the number of exception clauses.
2931 * Returns: the number of exception clauses present
2934 mono_method_header_get_num_clauses (MonoMethodHeader *header)
2936 return header->num_clauses;
2940 * mono_method_header_get_clauses:
2941 * @header: a MonoMethodHeader pointer
2942 * @method: MonoMethod the header belongs to
2943 * @iter: pointer to a iterator
2944 * @clause: pointer to a MonoExceptionClause structure which will be filled with the info
2946 * Get the info about the exception clauses in the method. Set *iter to NULL to
2947 * initiate the iteration, then call the method repeatedly until it returns FALSE.
2948 * At each iteration, the structure pointed to by clause if filled with the
2949 * exception clause information.
2951 * Returns: TRUE if clause was filled with info, FALSE if there are no more exception
2955 mono_method_header_get_clauses (MonoMethodHeader *header, MonoMethod *method, gpointer *iter, MonoExceptionClause *clause)
2957 MonoExceptionClause *sc;
2958 /* later we'll be able to use this interface to parse the clause info on demand,
2959 * without allocating anything.
2961 if (!iter || !header->num_clauses)
2964 *iter = sc = header->clauses;
2970 if (sc < header->clauses + header->num_clauses) {
2979 * mono_metadata_parse_field_type:
2980 * @m: metadata context to extract information from
2981 * @ptr: pointer to the field signature
2982 * @rptr: pointer updated to match the end of the decoded stream
2984 * Parses the field signature, and returns the type information for it.
2986 * Returns: The MonoType that was extracted from @ptr.
2989 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
2991 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
2995 * mono_metadata_parse_param:
2996 * @m: metadata context to extract information from
2997 * @ptr: pointer to the param signature
2998 * @rptr: pointer updated to match the end of the decoded stream
3000 * Parses the param signature, and returns the type information for it.
3002 * Returns: The MonoType that was extracted from @ptr.
3005 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
3007 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
3011 * mono_metadata_token_from_dor:
3012 * @dor_token: A TypeDefOrRef coded index
3014 * dor_token is a TypeDefOrRef coded index: it contains either
3015 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
3016 * bits contain an index into the table.
3018 * Returns: an expanded token
3021 mono_metadata_token_from_dor (guint32 dor_index)
3025 table = dor_index & 0x03;
3026 idx = dor_index >> 2;
3029 case 0: /* TypeDef */
3030 return MONO_TOKEN_TYPE_DEF | idx;
3031 case 1: /* TypeRef */
3032 return MONO_TOKEN_TYPE_REF | idx;
3033 case 2: /* TypeSpec */
3034 return MONO_TOKEN_TYPE_SPEC | idx;
3036 g_assert_not_reached ();
3043 * We use this to pass context information to the row locator
3046 int idx; /* The index that we are trying to locate */
3047 int col_idx; /* The index in the row where idx may be stored */
3048 MonoTableInfo *t; /* pointer to the table */
3053 * How the row locator works.
3058 * ___|___------> _______
3061 * A column in the rows of table A references an index in table B.
3062 * For example A may be the TYPEDEF table and B the METHODDEF table.
3064 * Given an index in table B we want to get the row in table A
3065 * where the column n references our index in B.
3067 * In the locator_t structure:
3069 * col_idx is the column number
3070 * index is the index in table B
3071 * result will be the index in table A
3074 * Table A Table B column (in table A)
3075 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
3076 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
3077 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
3078 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
3079 * METHODSEM PROPERTY ASSOCIATION (encoded index)
3081 * Note that we still don't support encoded indexes.
3085 typedef_locator (const void *a, const void *b)
3087 locator_t *loc = (locator_t *) a;
3088 const char *bb = (const char *) b;
3089 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
3090 guint32 col, col_next;
3092 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
3098 * Need to check that the next row is valid.
3100 if (typedef_index + 1 < loc->t->rows) {
3101 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
3102 if (loc->idx >= col_next)
3105 if (col == col_next)
3109 loc->result = typedef_index;
3115 table_locator (const void *a, const void *b)
3117 locator_t *loc = (locator_t *) a;
3118 const char *bb = (const char *) b;
3119 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
3122 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
3124 if (loc->idx == col) {
3125 loc->result = table_index;
3135 declsec_locator (const void *a, const void *b)
3137 locator_t *loc = (locator_t *) a;
3138 const char *bb = (const char *) b;
3139 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
3142 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
3144 if (loc->idx == col) {
3145 loc->result = table_index;
3157 * Return the 1-based row index in TABLE, which must be one of the *Ptr tables,
3158 * which contains IDX.
3161 search_ptr_table (MonoImage *image, int table, int idx)
3163 MonoTableInfo *ptrdef = &image->tables [table];
3166 /* Use a linear search to find our index in the table */
3167 for (i = 0; i < ptrdef->rows; i ++)
3168 /* All the Ptr tables have the same structure */
3169 if (mono_metadata_decode_row_col (ptrdef, i, 0) == idx)
3172 if (i < ptrdef->rows)
3179 * mono_metadata_typedef_from_field:
3180 * @meta: metadata context
3181 * @index: FieldDef token
3183 * Returns: the 1-based index into the TypeDef table of the type that
3184 * declared the field described by @index, or 0 if not found.
3187 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
3189 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
3195 loc.idx = mono_metadata_token_index (index);
3196 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
3199 if (meta->uncompressed_metadata)
3200 loc.idx = search_ptr_table (meta, MONO_TABLE_FIELD_POINTER, loc.idx);
3202 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
3203 g_assert_not_reached ();
3205 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3206 return loc.result + 1;
3210 * mono_metadata_typedef_from_method:
3211 * @meta: metadata context
3212 * @index: MethodDef token
3214 * Returns: the 1-based index into the TypeDef table of the type that
3215 * declared the method described by @index. 0 if not found.
3218 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
3220 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
3226 loc.idx = mono_metadata_token_index (index);
3227 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
3230 if (meta->uncompressed_metadata)
3231 loc.idx = search_ptr_table (meta, MONO_TABLE_METHOD_POINTER, loc.idx);
3233 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
3234 g_assert_not_reached ();
3236 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3237 return loc.result + 1;
3241 * mono_metadata_interfaces_from_typedef_full:
3242 * @meta: metadata context
3243 * @index: typedef token
3245 * The array of interfaces that the @index typedef token implements is returned in
3246 * @interfaces. The number of elemnts in the array is returned in @count.
3248 * LOCKING: Assumes the loader lock is held.
3250 * Returns: TRUE on success, FALSE on failure.
3253 mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, MonoClass ***interfaces, guint *count, MonoGenericContext *context)
3255 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
3258 guint32 cols [MONO_INTERFACEIMPL_SIZE];
3267 loc.idx = mono_metadata_token_index (index);
3268 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
3271 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3276 * We may end up in the middle of the rows...
3279 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
3285 while (pos < tdef->rows) {
3286 mono_metadata_decode_row (tdef, pos, cols, MONO_INTERFACEIMPL_SIZE);
3287 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
3292 mono_loader_lock ();
3293 result = mono_mempool_alloc0 (meta->mempool, sizeof (MonoClass*) * (pos - start));
3294 mono_loader_unlock ();
3297 while (pos < tdef->rows) {
3300 mono_metadata_decode_row (tdef, pos, cols, MONO_INTERFACEIMPL_SIZE);
3301 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
3303 iface = mono_class_get_full (
3304 meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]), context);
3307 result [pos - start] = iface;
3310 *count = pos - start;
3311 *interfaces = result;
3316 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
3318 MonoClass **interfaces;
3321 mono_loader_lock ();
3322 rv = mono_metadata_interfaces_from_typedef_full (meta, index, &interfaces, count, NULL);
3323 mono_loader_unlock ();
3331 * mono_metadata_nested_in_typedef:
3332 * @meta: metadata context
3333 * @index: typedef token
3335 * Returns: the 1-based index into the TypeDef table of the type
3336 * where the type described by @index is nested.
3337 * Retruns 0 if @index describes a non-nested type.
3340 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
3342 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
3348 loc.idx = mono_metadata_token_index (index);
3349 loc.col_idx = MONO_NESTED_CLASS_NESTED;
3352 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3355 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3356 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
3360 * mono_metadata_nesting_typedef:
3361 * @meta: metadata context
3362 * @index: typedef token
3364 * Returns: the 1-based index into the TypeDef table of the first type
3365 * that is nested inside the type described by @index. The search starts at
3366 * @start_index. returns 0 if no such type is found.
3369 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
3371 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
3373 guint32 class_index = mono_metadata_token_index (index);
3378 start = start_index;
3380 while (start <= tdef->rows) {
3381 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
3387 if (start > tdef->rows)
3394 * mono_metadata_packing_from_typedef:
3395 * @meta: metadata context
3396 * @index: token representing a type
3398 * Returns: the info stored in the ClassLAyout table for the given typedef token
3399 * into the @packing and @size pointers.
3400 * Returns 0 if the info is not found.
3403 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
3405 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
3407 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
3412 loc.idx = mono_metadata_token_index (index);
3413 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
3416 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3419 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
3421 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
3423 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
3425 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3426 return loc.result + 1;
3430 * mono_metadata_custom_attrs_from_index:
3431 * @meta: metadata context
3432 * @index: token representing the parent
3434 * Returns: the 1-based index into the CustomAttribute table of the first
3435 * attribute which belongs to the metadata object described by @index.
3436 * Returns 0 if no such attribute is found.
3439 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
3441 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
3448 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
3451 /* FIXME: Index translation */
3453 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3456 /* Find the first entry by searching backwards */
3457 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
3460 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3461 return loc.result + 1;
3465 * mono_metadata_declsec_from_index:
3466 * @meta: metadata context
3467 * @index: token representing the parent
3469 * Returns: the 0-based index into the DeclarativeSecurity table of the first
3470 * attribute which belongs to the metadata object described by @index.
3471 * Returns -1 if no such attribute is found.
3474 mono_metadata_declsec_from_index (MonoImage *meta, guint32 index)
3476 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_DECLSECURITY];
3483 loc.col_idx = MONO_DECL_SECURITY_PARENT;
3486 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
3489 /* Find the first entry by searching backwards */
3490 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_DECL_SECURITY_PARENT) == index))
3498 mono_backtrace (int limit)
3503 backtrace (array, limit);
3504 names = backtrace_symbols (array, limit);
3505 for (i =0; i < limit; ++i) {
3506 g_print ("\t%s\n", names [i]);
3513 /*#define __alignof__(a) sizeof(a)*/
3514 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
3519 * @t: the type to return the size of
3521 * Returns: the number of bytes required to hold an instance of this
3525 mono_type_size (MonoType *t, int *align)
3532 *align = __alignof__(gpointer);
3533 return sizeof (gpointer);
3537 case MONO_TYPE_VOID:
3540 case MONO_TYPE_BOOLEAN:
3541 *align = __alignof__(gint8);
3545 *align = __alignof__(gint8);
3547 case MONO_TYPE_CHAR:
3550 *align = __alignof__(gint16);
3554 *align = __alignof__(gint32);
3557 *align = __alignof__(float);
3561 *align = __alignof__(gint64);
3564 *align = __alignof__(double);
3568 *align = __alignof__(gpointer);
3569 return sizeof (gpointer);
3570 case MONO_TYPE_STRING:
3571 *align = __alignof__(gpointer);
3572 return sizeof (gpointer);
3573 case MONO_TYPE_OBJECT:
3574 *align = __alignof__(gpointer);
3575 return sizeof (gpointer);
3576 case MONO_TYPE_VALUETYPE: {
3577 if (t->data.klass->enumtype)
3578 return mono_type_size (t->data.klass->enum_basetype, align);
3580 return mono_class_value_size (t->data.klass, (guint32*)align);
3582 case MONO_TYPE_CLASS:
3583 case MONO_TYPE_SZARRAY:
3585 case MONO_TYPE_FNPTR:
3586 case MONO_TYPE_ARRAY:
3587 *align = __alignof__(gpointer);
3588 return sizeof (gpointer);
3589 case MONO_TYPE_TYPEDBYREF:
3590 return mono_class_value_size (mono_defaults.typed_reference_class, (guint32*)align);
3591 case MONO_TYPE_GENERICINST: {
3592 MonoGenericClass *gclass = t->data.generic_class;
3593 MonoClass *container_class = gclass->container_class;
3595 // g_assert (!gclass->inst->is_open);
3597 if (container_class->valuetype) {
3598 if (container_class->enumtype)
3599 return mono_type_size (container_class->enum_basetype, align);
3601 return mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align);
3603 *align = __alignof__(gpointer);
3604 return sizeof (gpointer);
3608 case MONO_TYPE_MVAR:
3609 /* FIXME: Martin, this is wrong. */
3610 *align = __alignof__(gpointer);
3611 return sizeof (gpointer);
3613 g_error ("mono_type_size: type 0x%02x unknown", t->type);
3619 * mono_type_stack_size:
3620 * @t: the type to return the size it uses on the stack
3622 * Returns: the number of bytes required to hold an instance of this
3623 * type on the runtime stack
3626 mono_type_stack_size (MonoType *t, int *align)
3630 g_assert (t != NULL);
3636 *align = __alignof__(gpointer);
3637 return sizeof (gpointer);
3641 case MONO_TYPE_BOOLEAN:
3642 case MONO_TYPE_CHAR:
3651 case MONO_TYPE_STRING:
3652 case MONO_TYPE_OBJECT:
3653 case MONO_TYPE_CLASS:
3654 case MONO_TYPE_SZARRAY:
3656 case MONO_TYPE_FNPTR:
3657 case MONO_TYPE_ARRAY:
3658 *align = __alignof__(gpointer);
3659 return sizeof (gpointer);
3660 case MONO_TYPE_TYPEDBYREF:
3661 *align = __alignof__(gpointer);
3662 return sizeof (gpointer) * 3;
3664 *align = __alignof__(float);
3665 return sizeof (float);
3668 *align = __alignof__(gint64);
3669 return sizeof (gint64);
3671 *align = __alignof__(double);
3672 return sizeof (double);
3673 case MONO_TYPE_VALUETYPE: {
3676 if (t->data.klass->enumtype)
3677 return mono_type_stack_size (t->data.klass->enum_basetype, align);
3679 size = mono_class_value_size (t->data.klass, (guint32*)align);
3681 *align = *align + __alignof__(gpointer) - 1;
3682 *align &= ~(__alignof__(gpointer) - 1);
3684 size += sizeof (gpointer) - 1;
3685 size &= ~(sizeof (gpointer) - 1);
3690 case MONO_TYPE_GENERICINST: {
3691 MonoGenericClass *gclass = t->data.generic_class;
3692 MonoClass *container_class = gclass->container_class;
3694 g_assert (!gclass->context.class_inst->is_open);
3696 if (container_class->valuetype) {
3697 if (container_class->enumtype)
3698 return mono_type_stack_size (container_class->enum_basetype, align);
3700 guint32 size = mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align);
3702 *align = *align + __alignof__(gpointer) - 1;
3703 *align &= ~(__alignof__(gpointer) - 1);
3705 size += sizeof (gpointer) - 1;
3706 size &= ~(sizeof (gpointer) - 1);
3711 *align = __alignof__(gpointer);
3712 return sizeof (gpointer);
3716 g_error ("type 0x%02x unknown", t->type);
3722 mono_type_generic_inst_is_valuetype (MonoType *type)
3724 g_assert (type->type == MONO_TYPE_GENERICINST);
3725 return type->data.generic_class->container_class->valuetype;
3729 mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass)
3731 return gclass->container_class->valuetype;
3735 _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2, gboolean signature_only)
3738 MonoGenericInst *i1 = g1->context.class_inst;
3739 MonoGenericInst *i2 = g2->context.class_inst;
3741 if (i1->type_argc != i2->type_argc || g1->is_dynamic != g2->is_dynamic)
3743 if (!mono_metadata_class_equal (g1->container_class, g2->container_class, signature_only))
3745 for (i = 0; i < i1->type_argc; ++i) {
3746 if (!do_mono_metadata_type_equal (i1->type_argv [i], i2->type_argv [i], signature_only))
3753 mono_metadata_generic_context_hash (const MonoGenericContext *context)
3755 /* FIXME: check if this seed is good enough */
3756 guint hash = 0xc01dfee7;
3757 if (context->class_inst)
3758 hash = ((hash << 5) - hash) ^ context->class_inst->id;
3759 if (context->method_inst)
3760 hash = ((hash << 5) - hash) ^ context->method_inst->id;
3765 mono_metadata_generic_context_equal (const MonoGenericContext *g1, const MonoGenericContext *g2)
3767 return g1->class_inst == g2->class_inst && g1->method_inst == g2->method_inst;
3771 * mono_metadata_type_hash:
3774 * Computes an hash value for @t1 to be used in GHashTable.
3777 mono_metadata_type_hash (MonoType *t1)
3779 guint hash = t1->type;
3781 hash |= t1->byref << 6; /* do not collide with t1->type values */
3783 case MONO_TYPE_VALUETYPE:
3784 case MONO_TYPE_CLASS:
3785 case MONO_TYPE_SZARRAY:
3786 /* check if the distribution is good enough */
3787 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
3789 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
3790 case MONO_TYPE_ARRAY:
3791 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
3792 case MONO_TYPE_GENERICINST:
3793 return ((hash << 5) - hash) ^ mono_generic_class_hash (t1->data.generic_class);
3799 mono_metadata_generic_param_equal (MonoGenericParam *p1, MonoGenericParam *p2, gboolean signature_only)
3803 if (p1->num != p2->num)
3806 if (p1->owner == p2->owner)
3810 * If `signature_only' is true, we're comparing two (method) signatures.
3811 * In this case, the owner of two type parameters doesn't need to match.
3814 return signature_only;
3818 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only)
3822 if (c1->generic_class && c2->generic_class)
3823 return _mono_metadata_generic_class_equal (c1->generic_class, c2->generic_class, signature_only);
3824 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
3825 return mono_metadata_generic_param_equal (
3826 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
3827 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
3828 return mono_metadata_generic_param_equal (
3829 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
3830 if (signature_only &&
3831 (c1->byval_arg.type == MONO_TYPE_SZARRAY) && (c2->byval_arg.type == MONO_TYPE_SZARRAY))
3832 return mono_metadata_class_equal (c1->byval_arg.data.klass, c2->byval_arg.data.klass, signature_only);
3837 mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only)
3839 gpointer iter1 = 0, iter2 = 0;
3843 if (s1->call_convention != s2->call_convention)
3845 if (s1->sentinelpos != s2->sentinelpos)
3847 if (s1->hasthis != s2->hasthis)
3849 if (s1->explicit_this != s2->explicit_this)
3851 if (! do_mono_metadata_type_equal (s1->ret, s2->ret, signature_only))
3853 if (s1->param_count != s2->param_count)
3857 MonoType *t1 = mono_signature_get_params (s1, &iter1);
3858 MonoType *t2 = mono_signature_get_params (s2, &iter2);
3860 if (t1 == NULL || t2 == NULL)
3862 if (! do_mono_metadata_type_equal (t1, t2, signature_only))
3868 * mono_metadata_type_equal:
3872 * Determine if @t1 and @t2 represent the same type.
3873 * Returns: #TRUE if @t1 and @t2 are equal.
3876 do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only)
3878 if (t1->type != t2->type || t1->byref != t2->byref)
3882 case MONO_TYPE_VOID:
3883 case MONO_TYPE_BOOLEAN:
3884 case MONO_TYPE_CHAR:
3895 case MONO_TYPE_STRING:
3898 case MONO_TYPE_OBJECT:
3899 case MONO_TYPE_TYPEDBYREF:
3901 case MONO_TYPE_VALUETYPE:
3902 case MONO_TYPE_CLASS:
3903 case MONO_TYPE_SZARRAY:
3904 return mono_metadata_class_equal (t1->data.klass, t2->data.klass, signature_only);
3906 return do_mono_metadata_type_equal (t1->data.type, t2->data.type, signature_only);
3907 case MONO_TYPE_ARRAY:
3908 if (t1->data.array->rank != t2->data.array->rank)
3910 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass, signature_only);
3911 case MONO_TYPE_GENERICINST:
3912 return _mono_metadata_generic_class_equal (
3913 t1->data.generic_class, t2->data.generic_class, signature_only);
3915 return mono_metadata_generic_param_equal (
3916 t1->data.generic_param, t2->data.generic_param, signature_only);
3917 case MONO_TYPE_MVAR:
3918 return mono_metadata_generic_param_equal (
3919 t1->data.generic_param, t2->data.generic_param, signature_only);
3920 case MONO_TYPE_FNPTR:
3921 return mono_metadata_fnptr_equal (t1->data.method, t2->data.method, signature_only);
3923 g_error ("implement type compare for %0x!", t1->type);
3931 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
3933 return do_mono_metadata_type_equal (t1, t2, FALSE);
3937 * mono_metadata_signature_equal:
3938 * @sig1: a signature
3939 * @sig2: another signature
3941 * Determine if @sig1 and @sig2 represent the same signature, with the
3942 * same number of arguments and the same types.
3943 * Returns: #TRUE if @sig1 and @sig2 are equal.
3946 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
3950 if (sig1->hasthis != sig2->hasthis || sig1->param_count != sig2->param_count)
3953 if (sig1->generic_param_count != sig2->generic_param_count)
3957 * We're just comparing the signatures of two methods here:
3959 * If we have two generic methods `void Foo<U> (U u)' and `void Bar<V> (V v)',
3960 * U and V are equal here.
3962 * That's what the `signature_only' argument of do_mono_metadata_type_equal() is for.
3965 for (i = 0; i < sig1->param_count; i++) {
3966 MonoType *p1 = sig1->params[i];
3967 MonoType *p2 = sig2->params[i];
3969 /* if (p1->attrs != p2->attrs)
3972 if (!do_mono_metadata_type_equal (p1, p2, TRUE))
3976 if (!do_mono_metadata_type_equal (sig1->ret, sig2->ret, TRUE))
3982 * mono_metadata_type_dup:
3983 * @mp: mempool to use
3984 * @original: type to duplicate
3986 * Returns: copy of type allocated from mempool (or from the heap, if @mp is null).
3989 mono_metadata_type_dup (MonoMemPool *mp, const MonoType *o)
3992 int sizeof_o = sizeof (MonoType);
3994 sizeof_o += (o->num_mods - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod);
3996 mono_loader_lock ();
3997 r = mp ? mono_mempool_alloc0 (mp, sizeof_o) : g_malloc (sizeof_o);
3998 mono_loader_unlock ();
4000 memcpy (r, o, sizeof_o);
4002 if (o->type == MONO_TYPE_PTR) {
4003 r->data.type = mono_metadata_type_dup (mp, o->data.type);
4004 } else if (o->type == MONO_TYPE_ARRAY) {
4005 /* FIXME: should mono_dup_array_type() use mempools? */
4007 r->data.array = mono_dup_array_type (o->data.array);
4008 } else if (o->type == MONO_TYPE_FNPTR) {
4009 /* FIXME: should mono_metadata_signature_deep_dup() use mempools? */
4011 r->data.method = mono_metadata_signature_deep_dup (o->data.method);
4017 mono_signature_hash (MonoMethodSignature *sig)
4019 guint i, res = sig->ret->type;
4021 for (i = 0; i < sig->param_count; i++)
4022 res = (res << 5) - res + mono_type_hash (sig->params[i]);
4028 * mono_metadata_encode_value:
4029 * @value: value to encode
4030 * @buf: buffer where to write the compressed representation
4031 * @endbuf: pointer updated to point at the end of the encoded output
4033 * Encodes the value @value in the compressed representation used
4034 * in metadata and stores the result in @buf. @buf needs to be big
4035 * enough to hold the data (4 bytes).
4038 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
4044 else if (value < 0x4000) {
4045 p [0] = 0x80 | (value >> 8);
4046 p [1] = value & 0xff;
4049 p [0] = (value >> 24) | 0xc0;
4050 p [1] = (value >> 16) & 0xff;
4051 p [2] = (value >> 8) & 0xff;
4052 p [3] = value & 0xff;
4060 * mono_metadata_field_info:
4061 * @meta: the Image the field is defined in
4062 * @index: the index in the field table representing the field
4063 * @offset: a pointer to an integer where to store the offset that
4064 * may have been specified for the field in a FieldLayout table
4065 * @rva: a pointer to the RVA of the field data in the image that
4066 * may have been defined in a FieldRVA table
4067 * @marshal_spec: a pointer to the marshal spec that may have been
4068 * defined for the field in a FieldMarshal table.
4070 * Gather info for field @index that may have been defined in the FieldLayout,
4071 * FieldRVA and FieldMarshal tables.
4072 * Either of offset, rva and marshal_spec can be NULL if you're not interested
4076 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
4077 MonoMarshalSpec **marshal_spec)
4079 MonoTableInfo *tdef;
4082 loc.idx = index + 1;
4083 if (meta->uncompressed_metadata)
4084 loc.idx = search_ptr_table (meta, MONO_TABLE_FIELD_POINTER, loc.idx);
4087 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
4089 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
4092 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
4093 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
4095 *offset = (guint32)-1;
4099 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
4101 loc.col_idx = MONO_FIELD_RVA_FIELD;
4104 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
4106 * LAMESPEC: There is no signature, no nothing, just the raw data.
4108 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
4116 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
4117 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
4124 * mono_metadata_get_constant_index:
4125 * @meta: the Image the field is defined in
4126 * @index: the token that may have a row defined in the constants table
4127 * @hint: possible position for the row
4129 * @token must be a FieldDef, ParamDef or PropertyDef token.
4131 * Returns: the index into the Constants table or 0 if not found.
4134 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
4136 MonoTableInfo *tdef;
4138 guint32 index = mono_metadata_token_index (token);
4140 tdef = &meta->tables [MONO_TABLE_CONSTANT];
4141 index <<= MONO_HASCONSTANT_BITS;
4142 switch (mono_metadata_token_table (token)) {
4143 case MONO_TABLE_FIELD:
4144 index |= MONO_HASCONSTANT_FIEDDEF;
4146 case MONO_TABLE_PARAM:
4147 index |= MONO_HASCONSTANT_PARAM;
4149 case MONO_TABLE_PROPERTY:
4150 index |= MONO_HASCONSTANT_PROPERTY;
4153 g_warning ("Not a valid token for the constant table: 0x%08x", token);
4157 loc.col_idx = MONO_CONSTANT_PARENT;
4160 /* FIXME: Index translation */
4162 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
4165 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
4166 return loc.result + 1;
4172 * mono_metadata_events_from_typedef:
4173 * @meta: metadata context
4174 * @index: 0-based index (in the TypeDef table) describing a type
4176 * Returns: the 0-based index in the Event table for the events in the
4177 * type. The last event that belongs to the type (plus 1) is stored
4178 * in the @end_idx pointer.
4181 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
4185 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
4193 loc.col_idx = MONO_EVENT_MAP_PARENT;
4194 loc.idx = index + 1;
4196 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4199 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
4200 if (loc.result + 1 < tdef->rows) {
4201 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
4203 end = meta->tables [MONO_TABLE_EVENT].rows;
4211 * mono_metadata_methods_from_event:
4212 * @meta: metadata context
4213 * @index: 0-based index (in the Event table) describing a event
4215 * Returns: the 0-based index in the MethodDef table for the methods in the
4216 * event. The last method that belongs to the event (plus 1) is stored
4217 * in the @end_idx pointer.
4220 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
4224 guint32 cols [MONO_METHOD_SEMA_SIZE];
4225 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
4231 if (meta->uncompressed_metadata)
4232 index = search_ptr_table (meta, MONO_TABLE_EVENT_POINTER, index + 1) - 1;
4235 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
4236 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
4238 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
4243 * We may end up in the middle of the rows...
4246 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
4252 while (end < msemt->rows) {
4253 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
4254 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
4263 * mono_metadata_properties_from_typedef:
4264 * @meta: metadata context
4265 * @index: 0-based index (in the TypeDef table) describing a type
4267 * Returns: the 0-based index in the Property table for the properties in the
4268 * type. The last property that belongs to the type (plus 1) is stored
4269 * in the @end_idx pointer.
4272 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
4276 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
4284 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
4285 loc.idx = index + 1;
4287 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4290 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
4291 if (loc.result + 1 < tdef->rows) {
4292 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
4294 end = meta->tables [MONO_TABLE_PROPERTY].rows;
4302 * mono_metadata_methods_from_property:
4303 * @meta: metadata context
4304 * @index: 0-based index (in the PropertyDef table) describing a property
4306 * Returns: the 0-based index in the MethodDef table for the methods in the
4307 * property. The last method that belongs to the property (plus 1) is stored
4308 * in the @end_idx pointer.
4311 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
4315 guint32 cols [MONO_METHOD_SEMA_SIZE];
4316 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
4322 if (meta->uncompressed_metadata)
4323 index = search_ptr_table (meta, MONO_TABLE_PROPERTY_POINTER, index + 1) - 1;
4326 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
4327 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
4329 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
4334 * We may end up in the middle of the rows...
4337 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
4343 while (end < msemt->rows) {
4344 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
4345 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
4354 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
4357 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
4362 /* No index translation seems to be needed */
4365 loc.col_idx = MONO_IMPLMAP_MEMBER;
4366 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
4368 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4371 return loc.result + 1;
4375 * @image: context where the image is created
4376 * @type_spec: typespec token
4378 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
4382 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
4384 guint32 idx = mono_metadata_token_index (type_spec);
4386 guint32 cols [MONO_TYPESPEC_SIZE];
4389 MonoType *type, *type2;
4391 mono_loader_lock ();
4393 type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec));
4395 mono_loader_unlock ();
4399 t = &image->tables [MONO_TABLE_TYPESPEC];
4401 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
4402 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
4403 len = mono_metadata_decode_value (ptr, &ptr);
4405 type = mono_mempool_alloc0 (image->mempool, sizeof (MonoType));
4407 if (*ptr == MONO_TYPE_BYREF) {
4412 if (!do_mono_metadata_parse_type (type, image, NULL, ptr, &ptr)) {
4413 mono_loader_unlock ();
4417 type2 = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec));
4420 mono_loader_unlock ();
4424 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
4426 mono_loader_unlock ();
4432 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
4434 MonoMarshalSpec *res;
4436 const char *start = ptr;
4438 /* fixme: this is incomplete, but I cant find more infos in the specs */
4440 res = g_new0 (MonoMarshalSpec, 1);
4442 len = mono_metadata_decode_value (ptr, &ptr);
4443 res->native = *ptr++;
4445 if (res->native == MONO_NATIVE_LPARRAY) {
4446 res->data.array_data.param_num = -1;
4447 res->data.array_data.num_elem = -1;
4448 res->data.array_data.elem_mult = -1;
4450 if (ptr - start <= len)
4451 res->data.array_data.elem_type = *ptr++;
4452 if (ptr - start <= len)
4453 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
4454 if (ptr - start <= len)
4455 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
4456 if (ptr - start <= len) {
4458 * LAMESPEC: Older spec versions say this parameter comes before
4459 * num_elem. Never spec versions don't talk about elem_mult at
4460 * all, but csc still emits it, and it is used to distinguish
4461 * between param_num being 0, and param_num being omitted.
4462 * So if (param_num == 0) && (num_elem > 0), then
4463 * elem_mult == 0 -> the array size is num_elem
4464 * elem_mult == 1 -> the array size is @param_num + num_elem
4466 res->data.array_data.elem_mult = mono_metadata_decode_value (ptr, &ptr);
4470 if (res->native == MONO_NATIVE_BYVALTSTR) {
4471 if (ptr - start <= len)
4472 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
4475 if (res->native == MONO_NATIVE_BYVALARRAY) {
4476 if (ptr - start <= len)
4477 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
4480 if (res->native == MONO_NATIVE_CUSTOM) {
4481 /* skip unused type guid */
4482 len = mono_metadata_decode_value (ptr, &ptr);
4484 /* skip unused native type name */
4485 len = mono_metadata_decode_value (ptr, &ptr);
4487 /* read custom marshaler type name */
4488 len = mono_metadata_decode_value (ptr, &ptr);
4489 res->data.custom_data.custom_name = g_strndup (ptr, len);
4491 /* read cookie string */
4492 len = mono_metadata_decode_value (ptr, &ptr);
4493 res->data.custom_data.cookie = g_strndup (ptr, len);
4496 if (res->native == MONO_NATIVE_SAFEARRAY) {
4497 res->data.safearray_data.elem_type = 0;
4498 res->data.safearray_data.num_elem = 0;
4499 if (ptr - start <= len)
4500 res->data.safearray_data.elem_type = *ptr++;
4501 if (ptr - start <= len)
4502 res->data.safearray_data.num_elem = *ptr++;
4508 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
4510 if (spec->native == MONO_NATIVE_CUSTOM) {
4511 g_free (spec->data.custom_data.custom_name);
4512 g_free (spec->data.custom_data.cookie);
4518 * mono_type_to_unmanaged:
4520 * Returns: A MonoMarshalNative enumeration value (MONO_NATIVE_) value
4521 * describing the underlying native reprensetation of the type.
4523 * In addition the value pointed by
4524 * "conv" will contain the kind of marshalling required for this
4525 * particular type one of the MONO_MARSHAL_CONV_ enumeration values.
4528 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
4529 gboolean unicode, MonoMarshalConv *conv)
4531 MonoMarshalConv dummy_conv;
4537 *conv = MONO_MARSHAL_CONV_NONE;
4540 return MONO_NATIVE_UINT;
4544 case MONO_TYPE_BOOLEAN:
4546 switch (mspec->native) {
4547 case MONO_NATIVE_VARIANTBOOL:
4548 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
4549 return MONO_NATIVE_VARIANTBOOL;
4550 case MONO_NATIVE_BOOLEAN:
4551 *conv = MONO_MARSHAL_CONV_BOOL_I4;
4552 return MONO_NATIVE_BOOLEAN;
4553 case MONO_NATIVE_I1:
4554 case MONO_NATIVE_U1:
4555 return mspec->native;
4557 g_error ("cant marshal bool to native type %02x", mspec->native);
4560 *conv = MONO_MARSHAL_CONV_BOOL_I4;
4561 return MONO_NATIVE_BOOLEAN;
4562 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
4563 case MONO_TYPE_I1: return MONO_NATIVE_I1;
4564 case MONO_TYPE_U1: return MONO_NATIVE_U1;
4565 case MONO_TYPE_I2: return MONO_NATIVE_I2;
4566 case MONO_TYPE_U2: return MONO_NATIVE_U2;
4567 case MONO_TYPE_I4: return MONO_NATIVE_I4;
4568 case MONO_TYPE_U4: return MONO_NATIVE_U4;
4569 case MONO_TYPE_I8: return MONO_NATIVE_I8;
4570 case MONO_TYPE_U8: return MONO_NATIVE_U8;
4571 case MONO_TYPE_R4: return MONO_NATIVE_R4;
4572 case MONO_TYPE_R8: return MONO_NATIVE_R8;
4573 case MONO_TYPE_STRING:
4575 switch (mspec->native) {
4576 case MONO_NATIVE_BSTR:
4577 *conv = MONO_MARSHAL_CONV_STR_BSTR;
4578 return MONO_NATIVE_BSTR;
4579 case MONO_NATIVE_LPSTR:
4580 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
4581 return MONO_NATIVE_LPSTR;
4582 case MONO_NATIVE_LPWSTR:
4583 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
4584 return MONO_NATIVE_LPWSTR;
4585 case MONO_NATIVE_LPTSTR:
4586 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
4587 return MONO_NATIVE_LPTSTR;
4588 case MONO_NATIVE_ANSIBSTR:
4589 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
4590 return MONO_NATIVE_ANSIBSTR;
4591 case MONO_NATIVE_TBSTR:
4592 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
4593 return MONO_NATIVE_TBSTR;
4594 case MONO_NATIVE_BYVALTSTR:
4596 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
4598 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
4599 return MONO_NATIVE_BYVALTSTR;
4601 g_error ("Can not marshal string to native type '%02x': Invalid managed/unmanaged type combination (String fields must be paired with LPStr, LPWStr, BStr or ByValTStr).", mspec->native);
4605 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
4606 return MONO_NATIVE_LPWSTR;
4609 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
4610 return MONO_NATIVE_LPSTR;
4612 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
4613 case MONO_TYPE_VALUETYPE: /*FIXME*/
4614 if (type->data.klass->enumtype) {
4615 t = type->data.klass->enum_basetype->type;
4618 if (type->data.klass == mono_defaults.handleref_class){
4619 *conv = MONO_MARSHAL_CONV_HANDLEREF;
4620 return MONO_NATIVE_INT;
4622 return MONO_NATIVE_STRUCT;
4623 case MONO_TYPE_SZARRAY:
4624 case MONO_TYPE_ARRAY:
4626 switch (mspec->native) {
4627 case MONO_NATIVE_BYVALARRAY:
4628 if ((type->data.klass->element_class == mono_defaults.char_class) && !unicode)
4629 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALCHARARRAY;
4631 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
4632 return MONO_NATIVE_BYVALARRAY;
4633 case MONO_NATIVE_SAFEARRAY:
4634 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
4635 return MONO_NATIVE_SAFEARRAY;
4636 case MONO_NATIVE_LPARRAY:
4637 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
4638 return MONO_NATIVE_LPARRAY;
4640 g_error ("cant marshal array as native type %02x", mspec->native);
4644 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
4645 return MONO_NATIVE_LPARRAY;
4646 case MONO_TYPE_I: return MONO_NATIVE_INT;
4647 case MONO_TYPE_U: return MONO_NATIVE_UINT;
4648 case MONO_TYPE_CLASS:
4649 case MONO_TYPE_OBJECT: {
4650 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
4652 switch (mspec->native) {
4653 case MONO_NATIVE_STRUCT:
4654 return MONO_NATIVE_STRUCT;
4655 case MONO_NATIVE_CUSTOM:
4656 return MONO_NATIVE_CUSTOM;
4657 case MONO_NATIVE_INTERFACE:
4658 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
4659 return MONO_NATIVE_INTERFACE;
4660 case MONO_NATIVE_IDISPATCH:
4661 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
4662 return MONO_NATIVE_IDISPATCH;
4663 case MONO_NATIVE_IUNKNOWN:
4664 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
4665 return MONO_NATIVE_IUNKNOWN;
4666 case MONO_NATIVE_FUNC:
4667 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
4668 type->data.klass == mono_defaults.delegate_class ||
4669 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
4670 *conv = MONO_MARSHAL_CONV_DEL_FTN;
4671 return MONO_NATIVE_FUNC;
4675 g_error ("cant marshal object as native type %02x", mspec->native);
4678 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
4679 type->data.klass == mono_defaults.delegate_class ||
4680 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
4681 *conv = MONO_MARSHAL_CONV_DEL_FTN;
4682 return MONO_NATIVE_FUNC;
4684 if (mono_defaults.safehandle_class && type->data.klass == mono_defaults.safehandle_class){
4685 *conv = MONO_MARSHAL_CONV_SAFEHANDLE;
4686 return MONO_NATIVE_INT;
4688 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
4689 return MONO_NATIVE_STRUCT;
4691 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
4692 case MONO_TYPE_GENERICINST:
4693 type = &type->data.generic_class->container_class->byval_arg;
4696 case MONO_TYPE_TYPEDBYREF:
4698 g_error ("type 0x%02x not handled in marshal", t);
4700 return MONO_NATIVE_MAX;
4704 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
4707 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
4713 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
4714 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
4716 /* FIXME: Index translation */
4718 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4721 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
4725 method_from_method_def_or_ref (MonoImage *m, guint32 tok, MonoGenericContext *context)
4727 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
4729 switch (tok & MONO_METHODDEFORREF_MASK) {
4730 case MONO_METHODDEFORREF_METHODDEF:
4731 return mono_get_method_full (m, MONO_TOKEN_METHOD_DEF | idx, NULL, context);
4732 case MONO_METHODDEFORREF_METHODREF:
4733 return mono_get_method_full (m, MONO_TOKEN_MEMBER_REF | idx, NULL, context);
4735 g_assert_not_reached ();
4740 * mono_class_get_overrides_full:
4742 * Return the method overrides belonging to class @type_token in @overrides, and
4743 * the number of overrides in @num_overrides.
4745 * Returns: TRUE on success, FALSE on failure.
4748 mono_class_get_overrides_full (MonoImage *image, guint32 type_token, MonoMethod ***overrides, gint32 *num_overrides,
4749 MonoGenericContext *generic_context)
4752 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
4755 guint32 cols [MONO_METHODIMPL_SIZE];
4756 MonoMethod **result;
4767 loc.col_idx = MONO_METHODIMPL_CLASS;
4768 loc.idx = mono_metadata_token_index (type_token);
4770 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4776 * We may end up in the middle of the rows...
4779 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
4784 while (end < tdef->rows) {
4785 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
4791 result = g_new (MonoMethod*, num * 2);
4792 for (i = 0; i < num; ++i) {
4795 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
4796 method = method_from_method_def_or_ref (
4797 image, cols [MONO_METHODIMPL_DECLARATION], generic_context);
4800 result [i * 2] = method;
4801 method = method_from_method_def_or_ref (
4802 image, cols [MONO_METHODIMPL_BODY], generic_context);
4805 result [i * 2 + 1] = method;
4808 *overrides = result;
4810 *num_overrides = num;
4815 * mono_guid_to_string:
4817 * Converts a 16 byte Microsoft GUID to the standard string representation.
4820 mono_guid_to_string (const guint8 *guid)
4822 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
4823 guid[3], guid[2], guid[1], guid[0],
4827 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
4831 get_constraints (MonoImage *image, int owner, MonoClass ***constraints, MonoGenericContainer *container)
4833 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
4834 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
4835 guint32 i, token, found;
4836 MonoClass *klass, **res;
4837 GList *cons = NULL, *tmp;
4838 MonoGenericContext *context = &container->context;
4840 *constraints = NULL;
4842 for (i = 0; i < tdef->rows; ++i) {
4843 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
4844 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
4845 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
4846 klass = mono_class_get_full (image, token, context);
4847 cons = g_list_append (cons, klass);
4850 /* contiguous list finished */
4857 res = g_new0 (MonoClass*, found + 1);
4858 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
4859 res [i] = tmp->data;
4867 * mono_metadata_get_generic_param_row:
4870 * @token: TypeOrMethodDef token, owner for GenericParam
4871 * @owner: coded token, set on return
4873 * Returns: 1-based row-id in the GenericParam table whose
4874 * owner is @token. 0 if not found.
4877 mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner)
4879 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
4880 guint32 cols [MONO_GENERICPARAM_SIZE];
4887 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
4888 *owner = MONO_TYPEORMETHOD_TYPE;
4889 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
4890 *owner = MONO_TYPEORMETHOD_METHOD;
4892 g_error ("wrong token %x to get_generic_param_row", token);
4895 *owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
4897 for (i = 0; i < tdef->rows; ++i) {
4898 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
4899 if (cols [MONO_GENERICPARAM_OWNER] == *owner)
4907 mono_metadata_has_generic_params (MonoImage *image, guint32 token)
4910 return mono_metadata_get_generic_param_row (image, token, &owner);
4914 * mono_metadata_load_generic_param_constraints:
4916 * Load the generic parameter constraints for the newly created generic type or method
4917 * represented by @token and @container. The @container is the new container which has
4918 * been returned by a call to mono_metadata_load_generic_params() with this @token.
4921 mono_metadata_load_generic_param_constraints (MonoImage *image, guint32 token,
4922 MonoGenericContainer *container)
4924 guint32 start_row, i, owner;
4925 if (! (start_row = mono_metadata_get_generic_param_row (image, token, &owner)))
4927 for (i = 0; i < container->type_argc; i++)
4928 get_constraints (image, start_row + i, &container->type_params [i].constraints, container);
4932 * mono_metadata_load_generic_params:
4934 * Load the type parameters from the type or method definition @token.
4936 * Use this method after parsing a type or method definition to figure out whether it's a generic
4937 * type / method. When parsing a method definition, @parent_container points to the generic container
4938 * of the current class, if any.
4940 * Note: This method does not load the constraints: for typedefs, this has to be done after fully
4941 * creating the type.
4943 * Returns: NULL if @token is not a generic type or method definition or the new generic container.
4945 * LOCKING: Assumes the loader lock is held.
4948 MonoGenericContainer *
4949 mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericContainer *parent_container)
4951 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
4952 guint32 cols [MONO_GENERICPARAM_SIZE];
4953 guint32 i, owner = 0, n;
4954 MonoGenericContainer *container;
4955 MonoGenericParam *params;
4956 MonoGenericContext *context;
4958 if (!(i = mono_metadata_get_generic_param_row (image, token, &owner)))
4960 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
4963 container = mono_mempool_alloc0 (image->mempool, sizeof (MonoGenericContainer));
4966 params = g_realloc (params, sizeof (MonoGenericParam) * n);
4967 params [n - 1].owner = container;
4968 params [n - 1].pklass = NULL;
4969 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
4970 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
4971 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
4972 params [n - 1].constraints = NULL;
4973 if (++i > tdef->rows)
4975 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
4976 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
4978 container->type_argc = n;
4979 container->type_params = mono_mempool_alloc0 (image->mempool, sizeof (MonoGenericParam) * n);
4980 memcpy (container->type_params, params, sizeof (MonoGenericParam) * n);
4982 container->parent = parent_container;
4984 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
4985 container->is_method = 1;
4987 g_assert (container->parent == NULL || container->is_method);
4989 context = &container->context;
4990 if (container->is_method) {
4991 context->class_inst = container->parent ? container->parent->context.class_inst : NULL;
4992 context->method_inst = mono_get_shared_generic_inst (container);
4994 context->class_inst = mono_get_shared_generic_inst (container);
5001 mono_get_shared_generic_inst (MonoGenericContainer *container)
5003 MonoType **type_argv;
5005 MonoGenericInst *nginst;
5008 type_argv = g_new0 (MonoType *, container->type_argc);
5009 helper = g_new0 (MonoType, container->type_argc);
5011 for (i = 0; i < container->type_argc; i++) {
5012 MonoType *t = &helper [i];
5014 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
5015 t->data.generic_param = &container->type_params [i];
5020 nginst = mono_metadata_get_generic_inst (container->type_argc, type_argv);
5029 mono_type_is_byref (MonoType *type)
5035 mono_type_get_type (MonoType *type)
5040 /* For MONO_TYPE_FNPTR */
5041 MonoMethodSignature*
5042 mono_type_get_signature (MonoType *type)
5044 return type->data.method;
5047 /* For MONO_TYPE_CLASS, VALUETYPE */
5049 mono_type_get_class (MonoType *type)
5051 return type->data.klass;
5054 /* For MONO_TYPE_ARRAY */
5056 mono_type_get_array_type (MonoType *type)
5058 return type->data.array;
5061 /* For MONO_TYPE_PTR */
5063 mono_type_get_ptr_type (MonoType *type)
5065 return type->data.type;
5069 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
5071 /* FIXME: implement */
5076 mono_signature_get_return_type (MonoMethodSignature *sig)
5082 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
5088 /* start from the first */
5089 if (sig->param_count) {
5090 *iter = &sig->params [0];
5091 return sig->params [0];
5099 if (type < &sig->params [sig->param_count]) {
5107 mono_signature_get_param_count (MonoMethodSignature *sig)
5109 return sig->param_count;
5113 mono_signature_get_call_conv (MonoMethodSignature *sig)
5115 return sig->call_convention;
5119 mono_signature_vararg_start (MonoMethodSignature *sig)
5121 return sig->sentinelpos;
5125 mono_signature_is_instance (MonoMethodSignature *sig)
5127 return sig->hasthis;
5131 mono_signature_explicit_this (MonoMethodSignature *sig)
5133 return sig->explicit_this;
5136 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
5138 mono_aligned_addr_hash (gconstpointer ptr)
5140 return GPOINTER_TO_UINT (ptr) >> 3;