2 * metadata.c: Routines for accessing the metadata
5 * Miguel de Icaza (miguel@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
21 #include "tabledefs.h"
22 #include "mono-endian.h"
24 #include "tokentype.h"
25 #include "metadata-internals.h"
26 #include "class-internals.h"
27 #include "verify-internals.h"
31 static gboolean do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container,
32 const char *ptr, const char **rptr);
34 static gboolean do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only);
35 static gboolean mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only);
36 static gboolean mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only);
37 static gboolean _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2,
38 gboolean signature_only);
39 static void free_generic_inst (MonoGenericInst *ginst);
40 static void free_generic_class (MonoGenericClass *ginst);
41 static void free_inflated_method (MonoMethodInflated *method);
42 static void mono_metadata_field_info_full (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva, MonoMarshalSpec **marshal_spec, gboolean alloc_from_image);
45 * This enumeration is used to describe the data types in the metadata
56 /* Index into Blob heap */
59 /* Index into String heap */
65 /* Pointer into a table */
68 /* HasConstant:Parent pointer (Param, Field or Property) */
71 /* HasCustomAttribute index. Indexes any table except CustomAttribute */
74 /* CustomAttributeType encoded index */
77 /* HasDeclSecurity index: TypeDef Method or Assembly */
80 /* Implementation coded index: File, Export AssemblyRef */
83 /* HasFieldMarshal coded index: Field or Param table */
86 /* MemberForwardedIndex: Field or Method */
89 /* TypeDefOrRef coded index: typedef, typeref, typespec */
92 /* MemberRefParent coded index: typeref, moduleref, method, memberref, typesepc, typedef */
95 /* MethodDefOrRef coded index: Method or Member Ref table */
98 /* HasSemantic coded index: Event or Property */
101 /* ResolutionScope coded index: Module, ModuleRef, AssemblytRef, TypeRef */
105 const static unsigned char TableSchemas [] = {
106 #define ASSEMBLY_SCHEMA_OFFSET 0
107 MONO_MT_UINT32, /* "HashId" }, */
108 MONO_MT_UINT16, /* "Major" }, */
109 MONO_MT_UINT16, /* "Minor" }, */
110 MONO_MT_UINT16, /* "BuildNumber" }, */
111 MONO_MT_UINT16, /* "RevisionNumber" }, */
112 MONO_MT_UINT32, /* "Flags" }, */
113 MONO_MT_BLOB_IDX, /* "PublicKey" }, */
114 MONO_MT_STRING_IDX, /* "Name" }, */
115 MONO_MT_STRING_IDX, /* "Culture" }, */
118 #define ASSEMBLYOS_SCHEMA_OFFSET ASSEMBLY_SCHEMA_OFFSET + 10
119 MONO_MT_UINT32, /* "OSPlatformID" }, */
120 MONO_MT_UINT32, /* "OSMajor" }, */
121 MONO_MT_UINT32, /* "OSMinor" }, */
124 #define ASSEMBLYPROC_SCHEMA_OFFSET ASSEMBLYOS_SCHEMA_OFFSET + 4
125 MONO_MT_UINT32, /* "Processor" }, */
128 #define ASSEMBLYREF_SCHEMA_OFFSET ASSEMBLYPROC_SCHEMA_OFFSET + 2
129 MONO_MT_UINT16, /* "Major" }, */
130 MONO_MT_UINT16, /* "Minor" }, */
131 MONO_MT_UINT16, /* "Build" }, */
132 MONO_MT_UINT16, /* "Revision" }, */
133 MONO_MT_UINT32, /* "Flags" }, */
134 MONO_MT_BLOB_IDX, /* "PublicKeyOrToken" }, */
135 MONO_MT_STRING_IDX, /* "Name" }, */
136 MONO_MT_STRING_IDX, /* "Culture" }, */
137 MONO_MT_BLOB_IDX, /* "HashValue" }, */
140 #define ASSEMBLYREFOS_SCHEMA_OFFSET ASSEMBLYREF_SCHEMA_OFFSET + 10
141 MONO_MT_UINT32, /* "OSPlatformID" }, */
142 MONO_MT_UINT32, /* "OSMajorVersion" }, */
143 MONO_MT_UINT32, /* "OSMinorVersion" }, */
144 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
147 #define ASSEMBLYREFPROC_SCHEMA_OFFSET ASSEMBLYREFOS_SCHEMA_OFFSET + 5
148 MONO_MT_UINT32, /* "Processor" }, */
149 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
152 #define CLASS_LAYOUT_SCHEMA_OFFSET ASSEMBLYREFPROC_SCHEMA_OFFSET + 3
153 MONO_MT_UINT16, /* "PackingSize" }, */
154 MONO_MT_UINT32, /* "ClassSize" }, */
155 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
158 #define CONSTANT_SCHEMA_OFFSET CLASS_LAYOUT_SCHEMA_OFFSET + 4
159 MONO_MT_UINT8, /* "Type" }, */
160 MONO_MT_UINT8, /* "PaddingZero" }, */
161 MONO_MT_CONST_IDX, /* "Parent" }, */
162 MONO_MT_BLOB_IDX, /* "Value" }, */
165 #define CUSTOM_ATTR_SCHEMA_OFFSET CONSTANT_SCHEMA_OFFSET + 5
166 MONO_MT_HASCAT_IDX, /* "Parent" }, */
167 MONO_MT_CAT_IDX, /* "Type" }, */
168 MONO_MT_BLOB_IDX, /* "Value" }, */
171 #define DECL_SEC_SCHEMA_OFFSET CUSTOM_ATTR_SCHEMA_OFFSET + 4
172 MONO_MT_UINT16, /* "Action" }, */
173 MONO_MT_HASDEC_IDX, /* "Parent" }, */
174 MONO_MT_BLOB_IDX, /* "PermissionSet" }, */
177 #define EVENTMAP_SCHEMA_OFFSET DECL_SEC_SCHEMA_OFFSET + 4
178 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
179 MONO_MT_TABLE_IDX, /* "EventList:Event" }, */
182 #define EVENT_SCHEMA_OFFSET EVENTMAP_SCHEMA_OFFSET + 3
183 MONO_MT_UINT16, /* "EventFlags#EventAttribute" }, */
184 MONO_MT_STRING_IDX, /* "Name" }, */
185 MONO_MT_TABLE_IDX, /* "EventType" }, TypeDef or TypeRef */
188 #define EVENT_POINTER_SCHEMA_OFFSET EVENT_SCHEMA_OFFSET + 4
189 MONO_MT_TABLE_IDX, /* "Event" }, */
192 #define EXPORTED_TYPE_SCHEMA_OFFSET EVENT_POINTER_SCHEMA_OFFSET + 2
193 MONO_MT_UINT32, /* "Flags" }, */
194 MONO_MT_TABLE_IDX, /* "TypeDefId" }, */
195 MONO_MT_STRING_IDX, /* "TypeName" }, */
196 MONO_MT_STRING_IDX, /* "TypeNameSpace" }, */
197 MONO_MT_IMPL_IDX, /* "Implementation" }, */
200 #define FIELD_SCHEMA_OFFSET EXPORTED_TYPE_SCHEMA_OFFSET + 6
201 MONO_MT_UINT16, /* "Flags" }, */
202 MONO_MT_STRING_IDX, /* "Name" }, */
203 MONO_MT_BLOB_IDX, /* "Signature" }, */
206 #define FIELD_LAYOUT_SCHEMA_OFFSET FIELD_SCHEMA_OFFSET + 4
207 MONO_MT_UINT32, /* "Offset" }, */
208 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
211 #define FIELD_MARSHAL_SCHEMA_OFFSET FIELD_LAYOUT_SCHEMA_OFFSET + 3
212 MONO_MT_HFM_IDX, /* "Parent" }, */
213 MONO_MT_BLOB_IDX, /* "NativeType" }, */
216 #define FIELD_RVA_SCHEMA_OFFSET FIELD_MARSHAL_SCHEMA_OFFSET + 3
217 MONO_MT_UINT32, /* "RVA" }, */
218 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
221 #define FIELD_POINTER_SCHEMA_OFFSET FIELD_RVA_SCHEMA_OFFSET + 3
222 MONO_MT_TABLE_IDX, /* "Field" }, */
225 #define FILE_SCHEMA_OFFSET FIELD_POINTER_SCHEMA_OFFSET + 2
226 MONO_MT_UINT32, /* "Flags" }, */
227 MONO_MT_STRING_IDX, /* "Name" }, */
228 MONO_MT_BLOB_IDX, /* "Value" }, */
231 #define IMPLMAP_SCHEMA_OFFSET FILE_SCHEMA_OFFSET + 4
232 MONO_MT_UINT16, /* "MappingFlag" }, */
233 MONO_MT_MF_IDX, /* "MemberForwarded" }, */
234 MONO_MT_STRING_IDX, /* "ImportName" }, */
235 MONO_MT_TABLE_IDX, /* "ImportScope:ModuleRef" }, */
238 #define IFACEMAP_SCHEMA_OFFSET IMPLMAP_SCHEMA_OFFSET + 5
239 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
240 MONO_MT_TDOR_IDX, /* "Interface=TypeDefOrRef" }, */
243 #define MANIFEST_SCHEMA_OFFSET IFACEMAP_SCHEMA_OFFSET + 3
244 MONO_MT_UINT32, /* "Offset" }, */
245 MONO_MT_UINT32, /* "Flags" }, */
246 MONO_MT_STRING_IDX, /* "Name" }, */
247 MONO_MT_IMPL_IDX, /* "Implementation" }, */
250 #define MEMBERREF_SCHEMA_OFFSET MANIFEST_SCHEMA_OFFSET + 5
251 MONO_MT_MRP_IDX, /* "Class" }, */
252 MONO_MT_STRING_IDX, /* "Name" }, */
253 MONO_MT_BLOB_IDX, /* "Signature" }, */
256 #define METHOD_SCHEMA_OFFSET MEMBERREF_SCHEMA_OFFSET + 4
257 MONO_MT_UINT32, /* "RVA" }, */
258 MONO_MT_UINT16, /* "ImplFlags#MethodImplAttributes" }, */
259 MONO_MT_UINT16, /* "Flags#MethodAttribute" }, */
260 MONO_MT_STRING_IDX, /* "Name" }, */
261 MONO_MT_BLOB_IDX, /* "Signature" }, */
262 MONO_MT_TABLE_IDX, /* "ParamList:Param" }, */
265 #define METHOD_IMPL_SCHEMA_OFFSET METHOD_SCHEMA_OFFSET + 7
266 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
267 MONO_MT_MDOR_IDX, /* "MethodBody" }, */
268 MONO_MT_MDOR_IDX, /* "MethodDeclaration" }, */
271 #define METHOD_SEMA_SCHEMA_OFFSET METHOD_IMPL_SCHEMA_OFFSET + 4
272 MONO_MT_UINT16, /* "MethodSemantic" }, */
273 MONO_MT_TABLE_IDX, /* "Method:Method" }, */
274 MONO_MT_HS_IDX, /* "Association" }, */
277 #define METHOD_POINTER_SCHEMA_OFFSET METHOD_SEMA_SCHEMA_OFFSET + 4
278 MONO_MT_TABLE_IDX, /* "Method" }, */
281 #define MODULE_SCHEMA_OFFSET METHOD_POINTER_SCHEMA_OFFSET + 2
282 MONO_MT_UINT16, /* "Generation" }, */
283 MONO_MT_STRING_IDX, /* "Name" }, */
284 MONO_MT_GUID_IDX, /* "MVID" }, */
285 MONO_MT_GUID_IDX, /* "EncID" }, */
286 MONO_MT_GUID_IDX, /* "EncBaseID" }, */
289 #define MODULEREF_SCHEMA_OFFSET MODULE_SCHEMA_OFFSET + 6
290 MONO_MT_STRING_IDX, /* "Name" }, */
293 #define NESTED_CLASS_SCHEMA_OFFSET MODULEREF_SCHEMA_OFFSET + 2
294 MONO_MT_TABLE_IDX, /* "NestedClass:TypeDef" }, */
295 MONO_MT_TABLE_IDX, /* "EnclosingClass:TypeDef" }, */
298 #define PARAM_SCHEMA_OFFSET NESTED_CLASS_SCHEMA_OFFSET + 3
299 MONO_MT_UINT16, /* "Flags" }, */
300 MONO_MT_UINT16, /* "Sequence" }, */
301 MONO_MT_STRING_IDX, /* "Name" }, */
304 #define PARAM_POINTER_SCHEMA_OFFSET PARAM_SCHEMA_OFFSET + 4
305 MONO_MT_TABLE_IDX, /* "Param" }, */
308 #define PROPERTY_SCHEMA_OFFSET PARAM_POINTER_SCHEMA_OFFSET + 2
309 MONO_MT_UINT16, /* "Flags" }, */
310 MONO_MT_STRING_IDX, /* "Name" }, */
311 MONO_MT_BLOB_IDX, /* "Type" }, */
314 #define PROPERTY_POINTER_SCHEMA_OFFSET PROPERTY_SCHEMA_OFFSET + 4
315 MONO_MT_TABLE_IDX, /* "Property" }, */
318 #define PROPERTY_MAP_SCHEMA_OFFSET PROPERTY_POINTER_SCHEMA_OFFSET + 2
319 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
320 MONO_MT_TABLE_IDX, /* "PropertyList:Property" }, */
323 #define STDALON_SIG_SCHEMA_OFFSET PROPERTY_MAP_SCHEMA_OFFSET + 3
324 MONO_MT_BLOB_IDX, /* "Signature" }, */
327 #define TYPEDEF_SCHEMA_OFFSET STDALON_SIG_SCHEMA_OFFSET + 2
328 MONO_MT_UINT32, /* "Flags" }, */
329 MONO_MT_STRING_IDX, /* "Name" }, */
330 MONO_MT_STRING_IDX, /* "Namespace" }, */
331 MONO_MT_TDOR_IDX, /* "Extends" }, */
332 MONO_MT_TABLE_IDX, /* "FieldList:Field" }, */
333 MONO_MT_TABLE_IDX, /* "MethodList:Method" }, */
336 #define TYPEREF_SCHEMA_OFFSET TYPEDEF_SCHEMA_OFFSET + 7
337 MONO_MT_RS_IDX, /* "ResolutionScope=ResolutionScope" }, */
338 MONO_MT_STRING_IDX, /* "Name" }, */
339 MONO_MT_STRING_IDX, /* "Namespace" }, */
342 #define TYPESPEC_SCHEMA_OFFSET TYPEREF_SCHEMA_OFFSET + 4
343 MONO_MT_BLOB_IDX, /* "Signature" }, */
346 #define GENPARAM_SCHEMA_OFFSET TYPESPEC_SCHEMA_OFFSET + 2
347 MONO_MT_UINT16, /* "Number" }, */
348 MONO_MT_UINT16, /* "Flags" }, */
349 MONO_MT_TABLE_IDX, /* "Owner" }, TypeDef or MethodDef */
350 MONO_MT_STRING_IDX, /* "Name" }, */
353 #define METHOD_SPEC_SCHEMA_OFFSET GENPARAM_SCHEMA_OFFSET + 5
354 MONO_MT_MDOR_IDX, /* "Method" }, */
355 MONO_MT_BLOB_IDX, /* "Signature" }, */
358 #define GEN_CONSTRAINT_SCHEMA_OFFSET METHOD_SPEC_SCHEMA_OFFSET + 3
359 MONO_MT_TABLE_IDX, /* "GenericParam" }, */
360 MONO_MT_TDOR_IDX, /* "Constraint" }, */
363 #define NULL_SCHEMA_OFFSET GEN_CONSTRAINT_SCHEMA_OFFSET + 3
367 /* Must be the same order as MONO_TABLE_* */
368 const static unsigned char
369 table_description [] = {
370 MODULE_SCHEMA_OFFSET,
371 TYPEREF_SCHEMA_OFFSET,
372 TYPEDEF_SCHEMA_OFFSET,
373 FIELD_POINTER_SCHEMA_OFFSET,
375 METHOD_POINTER_SCHEMA_OFFSET,
376 METHOD_SCHEMA_OFFSET,
377 PARAM_POINTER_SCHEMA_OFFSET,
379 IFACEMAP_SCHEMA_OFFSET,
380 MEMBERREF_SCHEMA_OFFSET, /* 0xa */
381 CONSTANT_SCHEMA_OFFSET,
382 CUSTOM_ATTR_SCHEMA_OFFSET,
383 FIELD_MARSHAL_SCHEMA_OFFSET,
384 DECL_SEC_SCHEMA_OFFSET,
385 CLASS_LAYOUT_SCHEMA_OFFSET,
386 FIELD_LAYOUT_SCHEMA_OFFSET, /* 0x10 */
387 STDALON_SIG_SCHEMA_OFFSET,
388 EVENTMAP_SCHEMA_OFFSET,
389 EVENT_POINTER_SCHEMA_OFFSET,
391 PROPERTY_MAP_SCHEMA_OFFSET,
392 PROPERTY_POINTER_SCHEMA_OFFSET,
393 PROPERTY_SCHEMA_OFFSET,
394 METHOD_SEMA_SCHEMA_OFFSET,
395 METHOD_IMPL_SCHEMA_OFFSET,
396 MODULEREF_SCHEMA_OFFSET, /* 0x1a */
397 TYPESPEC_SCHEMA_OFFSET,
398 IMPLMAP_SCHEMA_OFFSET,
399 FIELD_RVA_SCHEMA_OFFSET,
402 ASSEMBLY_SCHEMA_OFFSET, /* 0x20 */
403 ASSEMBLYPROC_SCHEMA_OFFSET,
404 ASSEMBLYOS_SCHEMA_OFFSET,
405 ASSEMBLYREF_SCHEMA_OFFSET,
406 ASSEMBLYREFPROC_SCHEMA_OFFSET,
407 ASSEMBLYREFOS_SCHEMA_OFFSET,
409 EXPORTED_TYPE_SCHEMA_OFFSET,
410 MANIFEST_SCHEMA_OFFSET,
411 NESTED_CLASS_SCHEMA_OFFSET,
412 GENPARAM_SCHEMA_OFFSET, /* 0x2a */
413 METHOD_SPEC_SCHEMA_OFFSET,
414 GEN_CONSTRAINT_SCHEMA_OFFSET
417 #ifdef HAVE_ARRAY_ELEM_INIT
418 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
419 #define MSGSTRFIELD1(line) str##line
420 static const struct msgstr_t {
421 #define TABLEDEF(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
422 #include "mono/cil/tables.def"
425 #define TABLEDEF(a,b) b,
426 #include "mono/cil/tables.def"
429 static const gint16 tableidx [] = {
430 #define TABLEDEF(a,b) [a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
431 #include "mono/cil/tables.def"
436 #define TABLEDEF(a,b) b,
437 static const char* const
438 mono_tables_names [] = {
439 #include "mono/cil/tables.def"
445 /* Auxiliary structure used for caching inflated signatures */
447 MonoMethodSignature *sig;
448 MonoGenericContext context;
449 } MonoInflatedMethodSignature;
452 * mono_meta_table_name:
453 * @table: table index
455 * Returns the name of the given ECMA metadata logical format table
456 * as described in ECMA 335, Partition II, Section 22.
458 * Returns: the name for the @table index
461 mono_meta_table_name (int table)
463 if ((table < 0) || (table > MONO_TABLE_LAST))
466 #ifdef HAVE_ARRAY_ELEM_INIT
467 return (const char*)&tablestr + tableidx [table];
469 return mono_tables_names [table];
473 /* The guy who wrote the spec for this should not be allowed near a
476 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
477 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
478 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
479 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
480 inverse of this mapping.
483 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
484 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
486 /* Reference: Partition II - 23.2.6 */
488 * mono_metadata_compute_size:
489 * @meta: metadata context
490 * @tableindex: metadata table number
491 * @result_bitfield: pointer to guint32 where to store additional info
493 * mono_metadata_compute_size() computes the lenght in bytes of a single
494 * row in a metadata table. The size of each column is encoded in the
495 * @result_bitfield return value along with the number of columns in the table.
496 * the resulting bitfield should be handed to the mono_metadata_table_size()
497 * and mono_metadata_table_count() macros.
498 * This is a Mono runtime internal only function.
501 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
503 guint32 bitfield = 0;
504 int size = 0, field_size = 0;
507 const unsigned char *description = TableSchemas + table_description [tableindex];
509 for (i = 0; (code = description [i]) != MONO_MT_END; i++){
512 field_size = 4; break;
515 field_size = 2; break;
518 field_size = 1; break;
520 case MONO_MT_BLOB_IDX:
521 field_size = meta->idx_blob_wide ? 4 : 2; break;
523 case MONO_MT_STRING_IDX:
524 field_size = meta->idx_string_wide ? 4 : 2; break;
526 case MONO_MT_GUID_IDX:
527 field_size = meta->idx_guid_wide ? 4 : 2; break;
529 case MONO_MT_TABLE_IDX:
530 /* Uhm, a table index can point to other tables besides the current one
531 * so, it's not correct to use the rowcount of the current table to
532 * get the size for this column - lupus
534 switch (tableindex) {
535 case MONO_TABLE_ASSEMBLYREFOS:
537 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
538 case MONO_TABLE_ASSEMBLYPROCESSOR:
540 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
541 case MONO_TABLE_CLASSLAYOUT:
543 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
544 case MONO_TABLE_EVENTMAP:
545 g_assert (i == 0 || i == 1);
546 field_size = i ? idx_size (MONO_TABLE_EVENT):
547 idx_size(MONO_TABLE_TYPEDEF);
549 case MONO_TABLE_EVENT:
551 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows, meta->tables [MONO_TABLE_TYPEREF].rows);
552 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
553 /*This is a coded token for 3 tables, so takes 2 bits */
554 field_size = rtsize (n, 16 - MONO_TYPEDEFORREF_BITS);
556 case MONO_TABLE_EVENT_POINTER:
558 field_size = idx_size (MONO_TABLE_EVENT); break;
559 case MONO_TABLE_EXPORTEDTYPE:
561 /* the index is in another metadata file, so it must be 4 */
562 field_size = 4; break;
563 case MONO_TABLE_FIELDLAYOUT:
565 field_size = idx_size (MONO_TABLE_FIELD); break;
566 case MONO_TABLE_FIELDRVA:
568 field_size = idx_size (MONO_TABLE_FIELD); break;
569 case MONO_TABLE_FIELD_POINTER:
571 field_size = idx_size (MONO_TABLE_FIELD); break;
572 case MONO_TABLE_IMPLMAP:
574 field_size = idx_size (MONO_TABLE_MODULEREF); break;
575 case MONO_TABLE_INTERFACEIMPL:
577 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
578 case MONO_TABLE_METHOD:
580 field_size = idx_size (MONO_TABLE_PARAM); break;
581 case MONO_TABLE_METHODIMPL:
583 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
584 case MONO_TABLE_METHODSEMANTICS:
586 field_size = idx_size (MONO_TABLE_METHOD); break;
587 case MONO_TABLE_METHOD_POINTER:
589 field_size = idx_size (MONO_TABLE_METHOD); break;
590 case MONO_TABLE_NESTEDCLASS:
591 g_assert (i == 0 || i == 1);
592 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
593 case MONO_TABLE_PARAM_POINTER:
595 field_size = idx_size (MONO_TABLE_PARAM); break;
596 case MONO_TABLE_PROPERTYMAP:
597 g_assert (i == 0 || i == 1);
598 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
599 idx_size(MONO_TABLE_TYPEDEF);
601 case MONO_TABLE_PROPERTY_POINTER:
603 field_size = idx_size (MONO_TABLE_PROPERTY); break;
604 case MONO_TABLE_TYPEDEF:
605 g_assert (i == 4 || i == 5);
606 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
607 idx_size(MONO_TABLE_METHOD);
609 case MONO_TABLE_GENERICPARAM:
610 g_assert (i == 2 || i == 4 || i == 5);
612 n = MAX (meta->tables [MONO_TABLE_METHOD].rows, meta->tables [MONO_TABLE_TYPEDEF].rows);
613 /*This is a coded token for 2 tables, so takes 1 bit */
614 field_size = rtsize (n, 16 - MONO_TYPEORMETHOD_BITS);
616 field_size = idx_size (MONO_TABLE_TYPEDEF);
618 field_size = idx_size (MONO_TABLE_TYPEDEF);
621 case MONO_TABLE_GENERICPARAMCONSTRAINT:
623 field_size = idx_size (MONO_TABLE_GENERICPARAM);
627 g_assert_not_reached ();
632 * HasConstant: ParamDef, FieldDef, Property
634 case MONO_MT_CONST_IDX:
635 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
636 meta->tables [MONO_TABLE_FIELD].rows);
637 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
639 /* 2 bits to encode tag */
640 field_size = rtsize (n, 16-2);
644 * HasCustomAttribute: points to any table but
647 case MONO_MT_HASCAT_IDX:
649 * We believe that since the signature and
650 * permission are indexing the Blob heap,
651 * we should consider the blob size first
653 /* I'm not a believer - lupus
654 if (meta->idx_blob_wide){
659 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
660 meta->tables [MONO_TABLE_FIELD].rows);
661 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
662 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
663 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
664 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
665 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
666 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
667 n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
668 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
669 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
670 n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
671 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
672 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
673 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
674 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
675 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
676 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
677 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
679 /* 5 bits to encode */
680 field_size = rtsize (n, 16-5);
684 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
685 * MemberRef and String.
687 case MONO_MT_CAT_IDX:
688 /* String is a heap, if it is wide, we know the size */
690 if (meta->idx_string_wide){
695 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
696 meta->tables [MONO_TABLE_TYPEDEF].rows);
697 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
698 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
700 /* 3 bits to encode */
701 field_size = rtsize (n, 16-3);
705 * HasDeclSecurity: Typedef, MethodDef, Assembly
707 case MONO_MT_HASDEC_IDX:
708 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
709 meta->tables [MONO_TABLE_METHOD].rows);
710 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
712 /* 2 bits to encode */
713 field_size = rtsize (n, 16-2);
717 * Implementation: File, AssemblyRef, ExportedType
719 case MONO_MT_IMPL_IDX:
720 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
721 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
722 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
724 /* 2 bits to encode tag */
725 field_size = rtsize (n, 16-2);
729 * HasFieldMarshall: FieldDef, ParamDef
731 case MONO_MT_HFM_IDX:
732 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
733 meta->tables [MONO_TABLE_PARAM].rows);
735 /* 1 bit used to encode tag */
736 field_size = rtsize (n, 16-1);
740 * MemberForwarded: FieldDef, MethodDef
743 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
744 meta->tables [MONO_TABLE_METHOD].rows);
746 /* 1 bit used to encode tag */
747 field_size = rtsize (n, 16-1);
751 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
753 * It is TypeDef, _TypeRef_, TypeSpec, instead.
755 case MONO_MT_TDOR_IDX:
756 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
757 meta->tables [MONO_TABLE_TYPEREF].rows);
758 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
760 /* 2 bits to encode */
761 field_size = rtsize (n, 16-2);
765 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
767 case MONO_MT_MRP_IDX:
768 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
769 meta->tables [MONO_TABLE_TYPEREF].rows);
770 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
771 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
772 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
773 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
775 /* 3 bits to encode */
776 field_size = rtsize (n, 16 - 3);
780 * MethodDefOrRef: MethodDef, MemberRef
782 case MONO_MT_MDOR_IDX:
783 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
784 meta->tables [MONO_TABLE_MEMBERREF].rows);
786 /* 1 bit used to encode tag */
787 field_size = rtsize (n, 16-1);
791 * HasSemantics: Property, Event
794 n = MAX (meta->tables [MONO_TABLE_PROPERTY].rows,
795 meta->tables [MONO_TABLE_EVENT].rows);
797 /* 1 bit used to encode tag */
798 field_size = rtsize (n, 16-1);
802 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
805 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
806 meta->tables [MONO_TABLE_MODULEREF].rows);
807 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
808 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
810 /* 2 bits used to encode tag (ECMA spec claims 3) */
811 field_size = rtsize (n, 16 - 2);
816 * encode field size as follows (we just need to
823 bitfield |= (field_size-1) << shift;
826 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
829 *result_bitfield = (i << 24) | bitfield;
834 * mono_metadata_compute_table_bases:
835 * @meta: metadata context to compute table values
837 * Computes the table bases for the metadata structure.
838 * This is an internal function used by the image loader code.
841 mono_metadata_compute_table_bases (MonoImage *meta)
844 const char *base = meta->tables_base;
846 for (i = 0; i < MONO_TABLE_NUM; i++) {
847 MonoTableInfo *table = &meta->tables [i];
848 if (table->rows == 0)
851 table->row_size = mono_metadata_compute_size (meta, i, &table->size_bitfield);
853 base += table->rows * table->row_size;
858 * mono_metadata_locate:
859 * @meta: metadata context
860 * @table: table code.
861 * @idx: index of element to retrieve from @table.
863 * Returns: a pointer to the @idx element in the metadata table
864 * whose code is @table.
867 mono_metadata_locate (MonoImage *meta, int table, int idx)
869 /* idx == 0 refers always to NULL */
870 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, ""); /*FIXME shouldn't we return NULL here?*/
872 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
876 * mono_metadata_locate_token:
877 * @meta: metadata context
878 * @token: metadata token
880 * Returns: a pointer to the data in the metadata represented by the
884 mono_metadata_locate_token (MonoImage *meta, guint32 token)
886 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
890 * mono_metadata_string_heap:
891 * @meta: metadata context
892 * @index: index into the string heap.
894 * Returns: an in-memory pointer to the @index in the string heap.
897 mono_metadata_string_heap (MonoImage *meta, guint32 index)
899 g_return_val_if_fail (index < meta->heap_strings.size, "");
900 return meta->heap_strings.data + index;
904 * mono_metadata_user_string:
905 * @meta: metadata context
906 * @index: index into the user string heap.
908 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
911 mono_metadata_user_string (MonoImage *meta, guint32 index)
913 g_return_val_if_fail (index < meta->heap_us.size, "");
914 return meta->heap_us.data + index;
918 * mono_metadata_blob_heap:
919 * @meta: metadata context
920 * @index: index into the blob.
922 * Returns: an in-memory pointer to the @index in the Blob heap.
925 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
927 g_return_val_if_fail (index < meta->heap_blob.size, "");/*FIXME shouldn't we return NULL and check for index == 0?*/
928 return meta->heap_blob.data + index;
932 * mono_metadata_guid_heap:
933 * @meta: metadata context
934 * @index: index into the guid heap.
936 * Returns: an in-memory pointer to the @index in the guid heap.
939 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
942 index *= 16; /* adjust for guid size and 1-based index */
943 g_return_val_if_fail (index < meta->heap_guid.size, "");
944 return meta->heap_guid.data + index;
947 static const unsigned char *
948 dword_align (const unsigned char *ptr)
950 #if SIZEOF_VOID_P == 8
951 return (const unsigned char *) (((guint64) (ptr + 3)) & ~3);
953 return (const unsigned char *) (((guint32) (ptr + 3)) & ~3);
958 * mono_metadata_decode_row:
959 * @t: table to extract information from.
960 * @idx: index in table.
961 * @res: array of @res_size cols to store the results in
963 * This decompresses the metadata element @idx in table @t
964 * into the guint32 @res array that has res_size elements
967 mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res_size)
969 guint32 bitfield = t->size_bitfield;
970 int i, count = mono_metadata_table_count (bitfield);
973 g_assert (idx < t->rows);
974 data = t->base + idx * t->row_size;
976 g_assert (res_size == count);
978 for (i = 0; i < count; i++) {
979 int n = mono_metadata_table_size (bitfield, i);
983 res [i] = *data; break;
985 res [i] = read16 (data); break;
987 res [i] = read32 (data); break;
989 g_assert_not_reached ();
996 * mono_metadata_decode_row_col:
997 * @t: table to extract information from.
998 * @idx: index for row in table.
999 * @col: column in the row.
1001 * This function returns the value of column @col from the @idx
1002 * row in the table @t.
1005 mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col)
1007 guint32 bitfield = t->size_bitfield;
1009 register const char *data;
1012 g_assert (idx < t->rows);
1013 g_assert (col < mono_metadata_table_count (bitfield));
1014 data = t->base + idx * t->row_size;
1016 n = mono_metadata_table_size (bitfield, 0);
1017 for (i = 0; i < col; ++i) {
1019 n = mono_metadata_table_size (bitfield, i + 1);
1025 return read16 (data);
1027 return read32 (data);
1029 g_assert_not_reached ();
1035 * mono_metadata_decode_blob_size:
1036 * @ptr: pointer to a blob object
1037 * @rptr: the new position of the pointer
1039 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
1041 * Returns: the size of the blob object
1044 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
1046 const unsigned char *ptr = (const unsigned char *)xptr;
1049 if ((*ptr & 0x80) == 0){
1050 size = ptr [0] & 0x7f;
1052 } else if ((*ptr & 0x40) == 0){
1053 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
1056 size = ((ptr [0] & 0x1f) << 24) +
1068 * mono_metadata_decode_value:
1069 * @ptr: pointer to decode from
1070 * @rptr: the new position of the pointer
1072 * This routine decompresses 32-bit values as specified in the "Blob and
1073 * Signature" section (22.2)
1075 * Returns: the decoded value
1078 mono_metadata_decode_value (const char *_ptr, const char **rptr)
1080 const unsigned char *ptr = (const unsigned char *) _ptr;
1081 unsigned char b = *ptr;
1084 if ((b & 0x80) == 0){
1087 } else if ((b & 0x40) == 0){
1088 len = ((b & 0x3f) << 8 | ptr [1]);
1091 len = ((b & 0x1f) << 24) |
1104 * mono_metadata_decode_signed_value:
1105 * @ptr: pointer to decode from
1106 * @rptr: the new position of the pointer
1108 * This routine decompresses 32-bit signed values
1109 * (not specified in the spec)
1111 * Returns: the decoded value
1114 mono_metadata_decode_signed_value (const char *ptr, const char **rptr)
1116 guint32 uval = mono_metadata_decode_value (ptr, rptr);
1117 gint32 ival = uval >> 1;
1120 /* ival is a truncated 2's complement negative number. */
1122 /* 6 bits = 7 bits for compressed representation (top bit is '0') - 1 sign bit */
1125 /* 13 bits = 14 bits for compressed representation (top bits are '10') - 1 sign bit */
1126 return ival - 0x2000;
1127 if (ival < 0x10000000)
1128 /* 28 bits = 29 bits for compressed representation (top bits are '110') - 1 sign bit */
1129 return ival - 0x10000000;
1130 g_assert (ival < 0x20000000);
1131 g_warning ("compressed signed value appears to use 29 bits for compressed representation: %x (raw: %8x)", ival, uval);
1132 return ival - 0x20000000;
1136 * Translates the given 1-based index into the Method, Field, Event, or Param tables
1137 * using the *Ptr tables in uncompressed metadata, if they are available.
1139 * FIXME: The caller is not forced to call this function, which is error-prone, since
1140 * forgetting to call it would only show up as a bug on uncompressed metadata.
1143 mono_metadata_translate_token_index (MonoImage *image, int table, guint32 idx)
1145 if (!image->uncompressed_metadata)
1149 case MONO_TABLE_METHOD:
1150 if (image->tables [MONO_TABLE_METHOD_POINTER].rows)
1151 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_METHOD_POINTER], idx - 1, MONO_METHOD_POINTER_METHOD);
1154 case MONO_TABLE_FIELD:
1155 if (image->tables [MONO_TABLE_FIELD_POINTER].rows)
1156 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_FIELD_POINTER], idx - 1, MONO_FIELD_POINTER_FIELD);
1159 case MONO_TABLE_EVENT:
1160 if (image->tables [MONO_TABLE_EVENT_POINTER].rows)
1161 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_EVENT_POINTER], idx - 1, MONO_EVENT_POINTER_EVENT);
1164 case MONO_TABLE_PROPERTY:
1165 if (image->tables [MONO_TABLE_PROPERTY_POINTER].rows)
1166 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_PROPERTY_POINTER], idx - 1, MONO_PROPERTY_POINTER_PROPERTY);
1169 case MONO_TABLE_PARAM:
1170 if (image->tables [MONO_TABLE_PARAM_POINTER].rows)
1171 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_PARAM_POINTER], idx - 1, MONO_PARAM_POINTER_PARAM);
1180 * mono_metadata_decode_table_row:
1182 * Same as mono_metadata_decode_row, but takes an IMAGE+TABLE ID pair, and takes
1183 * uncompressed metadata into account, so it should be used to access the
1184 * Method, Field, Param and Event tables when the access is made from metadata, i.e.
1185 * IDX is retrieved from a metadata table, like MONO_TYPEDEF_FIELD_LIST.
1188 mono_metadata_decode_table_row (MonoImage *image, int table, int idx, guint32 *res, int res_size)
1190 if (image->uncompressed_metadata)
1191 idx = mono_metadata_translate_token_index (image, table, idx + 1) - 1;
1193 mono_metadata_decode_row (&image->tables [table], idx, res, res_size);
1197 * mono_metadata_decode_table_row_col:
1199 * Same as mono_metadata_decode_row_col, but takes an IMAGE+TABLE ID pair, and takes
1200 * uncompressed metadata into account, so it should be used to access the
1201 * Method, Field, Param and Event tables.
1203 guint32 mono_metadata_decode_table_row_col (MonoImage *image, int table, int idx, guint col)
1205 if (image->uncompressed_metadata)
1206 idx = mono_metadata_translate_token_index (image, table, idx + 1) - 1;
1208 return mono_metadata_decode_row_col (&image->tables [table], idx, col);
1212 * mono_metadata_parse_typedef_or_ref:
1213 * @m: a metadata context.
1214 * @ptr: a pointer to an encoded TypedefOrRef in @m
1215 * @rptr: pointer updated to match the end of the decoded stream
1217 * Returns: a token valid in the @m metadata decoded from
1218 * the compressed representation.
1221 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1224 token = mono_metadata_decode_value (ptr, &ptr);
1227 return mono_metadata_token_from_dor (token);
1231 * mono_metadata_parse_custom_mod:
1232 * @m: a metadata context.
1233 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1234 * @ptr: a pointer to (possibly) the start of a custom modifier list
1235 * @rptr: pointer updated to match the end of the decoded stream
1237 * Checks if @ptr points to a type custom modifier compressed representation.
1239 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1242 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1244 MonoCustomMod local;
1245 if ((*ptr == MONO_TYPE_CMOD_OPT) || (*ptr == MONO_TYPE_CMOD_REQD)) {
1248 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1249 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1256 * mono_metadata_parse_array_full:
1257 * @m: a metadata context.
1258 * @ptr: a pointer to an encoded array description.
1259 * @rptr: pointer updated to match the end of the decoded stream
1261 * Decodes the compressed array description found in the metadata @m at @ptr.
1263 * Returns: a #MonoArrayType structure describing the array type
1264 * and dimensions. Memory is allocated from the image mempool.
1266 * LOCKING: Acquires the loader lock
1269 mono_metadata_parse_array_full (MonoImage *m, MonoGenericContainer *container,
1270 const char *ptr, const char **rptr)
1273 MonoArrayType *array;
1276 array = mono_image_alloc0 (m, sizeof (MonoArrayType));
1277 etype = mono_metadata_parse_type_full (m, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
1280 array->eklass = mono_class_from_mono_type (etype);
1281 array->rank = mono_metadata_decode_value (ptr, &ptr);
1283 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1284 if (array->numsizes)
1285 array->sizes = mono_image_alloc0 (m, sizeof (int) * array->numsizes);
1286 for (i = 0; i < array->numsizes; ++i)
1287 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1289 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1290 if (array->numlobounds)
1291 array->lobounds = mono_image_alloc0 (m, sizeof (int) * array->numlobounds);
1292 for (i = 0; i < array->numlobounds; ++i)
1293 array->lobounds [i] = mono_metadata_decode_signed_value (ptr, &ptr);
1301 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1303 return mono_metadata_parse_array_full (m, NULL, ptr, rptr);
1307 * mono_metadata_free_array:
1308 * @array: array description
1310 * Frees the array description returned from mono_metadata_parse_array().
1313 mono_metadata_free_array (MonoArrayType *array)
1315 g_free (array->sizes);
1316 g_free (array->lobounds);
1321 * need to add common field and param attributes combinations:
1324 * public static literal
1327 * private static literal
1329 static const MonoType
1331 /* data, attrs, type, nmods, byref, pinned */
1332 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1333 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1334 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1335 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1336 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1337 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1338 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1339 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1340 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1341 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1342 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1343 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1344 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1345 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1346 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1347 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1348 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1349 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1350 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1351 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1352 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1353 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1354 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1355 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1356 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1357 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1358 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1359 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 0, 0},
1360 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 1, 0},
1361 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1362 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1363 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1364 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1365 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1368 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1370 static GHashTable *type_cache = NULL;
1371 static GHashTable *generic_inst_cache = NULL;
1372 static GHashTable *generic_class_cache = NULL;
1373 static int next_generic_inst_id = 0;
1376 * Protected by the loader lock.
1377 * It has a MonoMethodInflated* as key and value.
1378 * The key lookup will just access the declaring and context fields
1380 static GHashTable *generic_method_cache = NULL;
1383 * Protected by the loader lock.
1384 * It has a MonoInflatedMethodSignature* as key and value.
1386 static GHashTable *generic_signature_cache = NULL;
1388 static guint mono_generic_class_hash (gconstpointer data);
1391 * MonoTypes with modifies are never cached, so we never check or use that field.
1394 mono_type_hash (gconstpointer data)
1396 const MonoType *type = (const MonoType *) data;
1397 if (type->type == MONO_TYPE_GENERICINST)
1398 return mono_generic_class_hash (type->data.generic_class);
1400 return type->type | (type->byref << 8) | (type->attrs << 9);
1404 mono_type_equal (gconstpointer ka, gconstpointer kb)
1406 const MonoType *a = (const MonoType *) ka;
1407 const MonoType *b = (const MonoType *) kb;
1409 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1411 /* need other checks */
1416 mono_metadata_generic_inst_hash (gconstpointer data)
1418 const MonoGenericInst *ginst = (const MonoGenericInst *) data;
1422 for (i = 0; i < ginst->type_argc; ++i) {
1424 hash += mono_metadata_type_hash (ginst->type_argv [i]);
1427 return hash ^ (ginst->is_open << 8);
1431 mono_generic_inst_equal_full (const MonoGenericInst *a, const MonoGenericInst *b, gboolean signature_only)
1435 if (a->id && b->id) {
1438 if (!signature_only)
1442 if (a->is_open != b->is_open || a->type_argc != b->type_argc)
1444 for (i = 0; i < a->type_argc; ++i) {
1445 if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], signature_only))
1452 mono_metadata_generic_inst_equal (gconstpointer ka, gconstpointer kb)
1454 const MonoGenericInst *a = (const MonoGenericInst *) ka;
1455 const MonoGenericInst *b = (const MonoGenericInst *) kb;
1457 return mono_generic_inst_equal_full (a, b, FALSE);
1461 mono_generic_class_hash (gconstpointer data)
1463 const MonoGenericClass *gclass = (const MonoGenericClass *) data;
1464 guint hash = mono_metadata_type_hash (&gclass->container_class->byval_arg);
1467 hash += gclass->is_tb_open;
1468 hash += mono_metadata_generic_context_hash (&gclass->context);
1474 mono_generic_class_equal (gconstpointer ka, gconstpointer kb)
1476 const MonoGenericClass *a = (const MonoGenericClass *) ka;
1477 const MonoGenericClass *b = (const MonoGenericClass *) kb;
1479 return _mono_metadata_generic_class_equal (a, b, FALSE);
1483 * mono_metadata_init:
1485 * Initialize the global variables of this module.
1486 * This is a Mono runtime internal function.
1489 mono_metadata_init (void)
1493 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1494 generic_inst_cache = g_hash_table_new_full (mono_metadata_generic_inst_hash, mono_metadata_generic_inst_equal, NULL, (GDestroyNotify)free_generic_inst);
1495 generic_class_cache = g_hash_table_new_full (mono_generic_class_hash, mono_generic_class_equal, NULL, (GDestroyNotify)free_generic_class);
1497 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1498 g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
1502 * mono_metadata_cleanup:
1504 * Free all resources used by this module.
1505 * This is a Mono runtime internal function.
1508 mono_metadata_cleanup (void)
1510 /* Has to free this first, as it could reference data in the other caches */
1511 if (generic_method_cache)
1512 g_hash_table_destroy (generic_method_cache);
1513 g_hash_table_destroy (type_cache);
1514 g_hash_table_destroy (generic_inst_cache);
1515 g_hash_table_destroy (generic_class_cache);
1516 if (generic_signature_cache)
1517 g_hash_table_destroy (generic_signature_cache);
1519 generic_inst_cache = NULL;
1520 generic_class_cache = NULL;
1521 generic_method_cache = NULL;
1522 generic_signature_cache = NULL;
1526 * mono_metadata_parse_type:
1527 * @m: metadata context
1528 * @mode: king of type that may be found at @ptr
1529 * @opt_attrs: optional attributes to store in the returned type
1530 * @ptr: pointer to the type representation
1531 * @rptr: pointer updated to match the end of the decoded stream
1533 * Decode a compressed type description found at @ptr in @m.
1534 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1535 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1536 * This function can be used to decode type descriptions in method signatures,
1537 * field signatures, locals signatures etc.
1539 * To parse a generic type, `generic_container' points to the current class'es
1540 * (the `generic_container' field in the MonoClass) or the current generic method's
1541 * (the `generic_container' field in the MonoMethodNormal) generic container.
1542 * When we encounter any MONO_TYPE_VAR or MONO_TYPE_MVAR's, they're looked up in
1543 * this MonoGenericContainer.
1544 * This is a Mono runtime internal function.
1546 * LOCKING: Acquires the loader lock.
1548 * Returns: a #MonoType structure representing the decoded type.
1551 mono_metadata_parse_type_full (MonoImage *m, MonoGenericContainer *container, MonoParseTypeMode mode,
1552 short opt_attrs, const char *ptr, const char **rptr)
1554 MonoType *type, *cached;
1556 gboolean byref = FALSE;
1557 gboolean pinned = FALSE;
1558 const char *tmp_ptr;
1563 * According to the spec, custom modifiers should come before the byref
1564 * flag, but the IL produced by ilasm from the following signature:
1565 * object modopt(...) &
1566 * starts with a byref flag, followed by the modifiers. (bug #49802)
1567 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1568 * it would be better to treat byref as real type constructor instead of
1570 * Also, pinned should come before anything else, but some MSV++ produced
1571 * assemblies violate this (#bug 61990).
1574 /* Count the modifiers first */
1579 case MONO_TYPE_PINNED:
1580 case MONO_TYPE_BYREF:
1583 case MONO_TYPE_CMOD_REQD:
1584 case MONO_TYPE_CMOD_OPT:
1586 mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1594 type = mono_image_alloc0 (m, MONO_SIZEOF_TYPE + ((gint32)count) * sizeof (MonoCustomMod));
1595 type->num_mods = count;
1597 g_warning ("got more than 64 modifiers in type");
1600 memset (type, 0, MONO_SIZEOF_TYPE);
1603 /* Parse pinned, byref and custom modifiers */
1608 case MONO_TYPE_PINNED:
1612 case MONO_TYPE_BYREF:
1616 case MONO_TYPE_CMOD_REQD:
1617 case MONO_TYPE_CMOD_OPT:
1618 mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1626 type->attrs = opt_attrs;
1627 type->byref = byref;
1628 type->pinned = pinned ? 1 : 0;
1630 if (!do_mono_metadata_parse_type (type, m, container, ptr, &ptr)) {
1637 if (!type->num_mods) {
1638 /* no need to free type here, because it is on the stack */
1639 if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
1640 MonoType *ret = type->byref ? &type->data.klass->this_arg : &type->data.klass->byval_arg;
1642 /* Consider the case:
1644 class Foo<T> { class Bar {} }
1645 class Test : Foo<Test>.Bar {}
1647 When Foo<Test> is being expanded, 'Test' isn't yet initialized. It's actually in
1648 a really pristine state: it doesn't even know whether 'Test' is a reference or a value type.
1650 We ensure that the MonoClass is in a state that we can canonicalize to:
1652 klass->byval_arg.data.klass == klass
1653 klass->this_arg.data.klass == klass
1655 If we can't canonicalize 'type', it doesn't matter, since later users of 'type' will do it.
1657 LOCKING: even though we don't explicitly hold a lock, in the problematic case 'ret' is a field
1658 of a MonoClass which currently holds the loader lock. 'type' is local.
1660 if (ret->data.klass == type->data.klass) {
1664 /* No need to use locking since nobody is modifying the hash table */
1665 if ((cached = g_hash_table_lookup (type_cache, type))) {
1670 /* printf ("%x %x %c %s\n", type->attrs, type->num_mods, type->pinned ? 'p' : ' ', mono_type_full_name (type)); */
1672 if (type == &stype) {
1673 type = mono_image_alloc (m, MONO_SIZEOF_TYPE);
1674 memcpy (type, &stype, MONO_SIZEOF_TYPE);
1680 * LOCKING: Acquires the loader lock.
1683 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
1684 const char *ptr, const char **rptr)
1686 return mono_metadata_parse_type_full (m, NULL, mode, opt_attrs, ptr, rptr);
1690 * mono_metadata_get_param_attrs:
1692 * Return the parameter attributes for the method whose MethodDef index is DEF. The
1693 * returned memory needs to be freed by the caller. If all the param attributes are
1694 * 0, then NULL is returned.
1697 mono_metadata_get_param_attrs (MonoImage *m, int def)
1699 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1700 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1701 guint32 cols [MONO_PARAM_SIZE];
1702 guint lastp, i, param_index = mono_metadata_decode_row_col (&m->tables [MONO_TABLE_METHOD], def - 1, MONO_METHOD_PARAMLIST);
1705 if (def < methodt->rows)
1706 lastp = mono_metadata_decode_row_col (&m->tables [MONO_TABLE_METHOD], def, MONO_METHOD_PARAMLIST);
1708 lastp = paramt->rows + 1;
1710 for (i = param_index; i < lastp; ++i) {
1711 mono_metadata_decode_row (&m->tables [MONO_TABLE_PARAM], i - 1, cols, MONO_PARAM_SIZE);
1712 if (cols [MONO_PARAM_FLAGS]) {
1714 pattrs = g_new0 (int, 1 + (lastp - param_index));
1715 pattrs [cols [MONO_PARAM_SEQUENCE]] = cols [MONO_PARAM_FLAGS];
1723 * mono_metadata_parse_signature_full:
1724 * @image: metadata context
1725 * @generic_container: generic container
1726 * @toke: metadata token
1728 * Decode a method signature stored in the STANDALONESIG table
1730 * LOCKING: Assumes the loader lock is held.
1732 * Returns: a MonoMethodSignature describing the signature.
1734 MonoMethodSignature*
1735 mono_metadata_parse_signature_full (MonoImage *image, MonoGenericContainer *generic_container, guint32 token)
1737 MonoTableInfo *tables = image->tables;
1738 guint32 idx = mono_metadata_token_index (token);
1743 return mono_lookup_dynamic_token (image, token, NULL);
1745 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1747 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1749 ptr = mono_metadata_blob_heap (image, sig);
1750 mono_metadata_decode_blob_size (ptr, &ptr);
1752 return mono_metadata_parse_method_signature_full (image, generic_container, 0, ptr, NULL);
1756 * mono_metadata_parse_signature:
1757 * @image: metadata context
1758 * @toke: metadata token
1760 * Decode a method signature stored in the STANDALONESIG table
1762 * Returns: a MonoMethodSignature describing the signature.
1764 MonoMethodSignature*
1765 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1767 return mono_metadata_parse_signature_full (image, NULL, token);
1771 * mono_metadata_signature_alloc:
1772 * @image: metadata context
1773 * @nparmas: number of parameters in the signature
1775 * Allocate a MonoMethodSignature structure with the specified number of params.
1776 * The return type and the params types need to be filled later.
1777 * This is a Mono runtime internal function.
1779 * LOCKING: Assumes the loader lock is held.
1781 * Returns: the new MonoMethodSignature structure.
1783 MonoMethodSignature*
1784 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1786 MonoMethodSignature *sig;
1788 sig = mono_image_alloc0 (m, MONO_SIZEOF_METHOD_SIGNATURE + ((gint32)nparams) * sizeof (MonoType*));
1789 sig->param_count = nparams;
1790 sig->sentinelpos = -1;
1795 static MonoMethodSignature*
1796 mono_metadata_signature_dup_internal (MonoImage *image, MonoMemPool *mp, MonoMethodSignature *sig)
1799 MonoMethodSignature *ret;
1800 sigsize = MONO_SIZEOF_METHOD_SIGNATURE + sig->param_count * sizeof (MonoType *);
1803 ret = mono_image_alloc (image, sigsize);
1805 ret = mono_mempool_alloc (mp, sigsize);
1807 ret = g_malloc (sigsize);
1809 memcpy (ret, sig, sigsize);
1813 MonoMethodSignature*
1814 mono_metadata_signature_dup_full (MonoImage *image, MonoMethodSignature *sig)
1816 return mono_metadata_signature_dup_internal (image, NULL, sig);
1819 /*The mempool is accessed without synchronization*/
1820 MonoMethodSignature*
1821 mono_metadata_signature_dup_mempool (MonoMemPool *mp, MonoMethodSignature *sig)
1823 return mono_metadata_signature_dup_internal (NULL, mp, sig);
1827 * mono_metadata_signature_dup:
1828 * @sig: method signature
1830 * Duplicate an existing MonoMethodSignature so it can be modified.
1831 * This is a Mono runtime internal function.
1833 * Returns: the new MonoMethodSignature structure.
1835 MonoMethodSignature*
1836 mono_metadata_signature_dup (MonoMethodSignature *sig)
1838 return mono_metadata_signature_dup_full (NULL, sig);
1842 * mono_metadata_signature_size:
1844 * Return the amount of memory allocated to SIG.
1847 mono_metadata_signature_size (MonoMethodSignature *sig)
1849 return MONO_SIZEOF_METHOD_SIGNATURE + sig->param_count * sizeof (MonoType *);
1853 * mono_metadata_parse_method_signature:
1854 * @m: metadata context
1855 * @generic_container: generics container
1856 * @def: the MethodDef index or 0 for Ref signatures.
1857 * @ptr: pointer to the signature metadata representation
1858 * @rptr: pointer updated to match the end of the decoded stream
1860 * Decode a method signature stored at @ptr.
1861 * This is a Mono runtime internal function.
1863 * LOCKING: Assumes the loader lock is held.
1865 * Returns: a MonoMethodSignature describing the signature.
1867 MonoMethodSignature *
1868 mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContainer *container,
1869 int def, const char *ptr, const char **rptr)
1871 MonoMethodSignature *method;
1872 int i, *pattrs = NULL;
1873 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1874 guint32 gen_param_count = 0;
1875 gboolean is_open = FALSE;
1878 gen_param_count = 1;
1883 call_convention = *ptr & 0x0F;
1885 if (gen_param_count)
1886 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1887 param_count = mono_metadata_decode_value (ptr, &ptr);
1890 pattrs = mono_metadata_get_param_attrs (m, def);
1891 method = mono_metadata_signature_alloc (m, param_count);
1892 method->hasthis = hasthis;
1893 method->explicit_this = explicit_this;
1894 method->call_convention = call_convention;
1895 method->generic_param_count = gen_param_count;
1897 if (call_convention != 0xa) {
1898 method->ret = mono_metadata_parse_type_full (m, container, MONO_PARSE_RET, pattrs ? pattrs [0] : 0, ptr, &ptr);
1900 mono_metadata_free_method_signature (method);
1904 is_open = mono_class_is_open_constructed_type (method->ret);
1907 for (i = 0; i < method->param_count; ++i) {
1908 if (*ptr == MONO_TYPE_SENTINEL) {
1909 if (method->call_convention != MONO_CALL_VARARG || def) {
1910 g_warning ("found sentinel for methoddef or no vararg method 0x%08x on image %s", def, m->name);
1914 if (method->sentinelpos >= 0) {
1915 g_warning ("found sentinel twice in the same signature for method 0x%08x on image %s", def, m->name);
1919 method->sentinelpos = i;
1922 method->params [i] = mono_metadata_parse_type_full (m, container, MONO_PARSE_PARAM, pattrs ? pattrs [i+1] : 0, ptr, &ptr);
1923 if (!method->params [i]) {
1924 mono_metadata_free_method_signature (method);
1929 is_open = mono_class_is_open_constructed_type (method->params [i]);
1932 /* The sentinel could be missing if the caller does not pass any additional arguments */
1933 if (!def && method->call_convention == MONO_CALL_VARARG && method->sentinelpos < 0)
1934 method->sentinelpos = method->param_count;
1936 method->has_type_parameters = is_open;
1938 if (def && (method->call_convention == MONO_CALL_VARARG))
1939 method->sentinelpos = method->param_count;
1946 * Add signature to a cache and increase ref count...
1953 * mono_metadata_parse_method_signature:
1954 * @m: metadata context
1955 * @def: the MethodDef index or 0 for Ref signatures.
1956 * @ptr: pointer to the signature metadata representation
1957 * @rptr: pointer updated to match the end of the decoded stream
1959 * Decode a method signature stored at @ptr.
1960 * This is a Mono runtime internal function.
1962 * LOCKING: Assumes the loader lock is held.
1964 * Returns: a MonoMethodSignature describing the signature.
1966 MonoMethodSignature *
1967 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1969 return mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr);
1973 * mono_metadata_free_method_signature:
1974 * @sig: signature to destroy
1976 * Free the memory allocated in the signature @sig.
1977 * This method needs to be robust and work also on partially-built
1978 * signatures, so it does extra checks.
1981 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1983 /* Everything is allocated from mempools */
1987 mono_metadata_free_type (sig->ret);
1988 for (i = 0; i < sig->param_count; ++i) {
1989 if (sig->params [i])
1990 mono_metadata_free_type (sig->params [i]);
1996 mono_metadata_free_inflated_signature (MonoMethodSignature *sig)
2000 /* Allocated in inflate_generic_signature () */
2002 mono_metadata_free_type (sig->ret);
2003 for (i = 0; i < sig->param_count; ++i) {
2004 if (sig->params [i])
2005 mono_metadata_free_type (sig->params [i]);
2011 inflated_method_equal (gconstpointer a, gconstpointer b)
2013 const MonoMethodInflated *ma = a;
2014 const MonoMethodInflated *mb = b;
2015 if (ma->declaring != mb->declaring)
2017 if (ma->method.method.is_mb_open != mb->method.method.is_mb_open)
2019 return mono_metadata_generic_context_equal (&ma->context, &mb->context);
2023 inflated_method_hash (gconstpointer a)
2025 const MonoMethodInflated *ma = a;
2026 return (mono_metadata_generic_context_hash (&ma->context) ^ mono_aligned_addr_hash (ma->declaring)) + ma->method.method.is_mb_open;
2030 inflated_signature_equal (gconstpointer a, gconstpointer b)
2032 const MonoInflatedMethodSignature *sig1 = a;
2033 const MonoInflatedMethodSignature *sig2 = b;
2035 /* sig->sig is assumed to be canonized */
2036 if (sig1->sig != sig2->sig)
2038 /* The generic instances are canonized */
2039 return mono_metadata_generic_context_equal (&sig1->context, &sig2->context);
2043 inflated_signature_hash (gconstpointer a)
2045 const MonoInflatedMethodSignature *sig = a;
2047 /* sig->sig is assumed to be canonized */
2048 return mono_metadata_generic_context_hash (&sig->context) ^ mono_aligned_addr_hash (sig->sig);
2052 dump_ginst (MonoGenericInst *ginst)
2057 g_print ("Ginst: <");
2058 for (i = 0; i < ginst->type_argc; ++i) {
2061 name = mono_type_get_name (ginst->type_argv [i]);
2062 g_print ("%s", name);
2068 static gboolean type_in_image (MonoType *type, MonoImage *image);
2071 signature_in_image (MonoMethodSignature *sig, MonoImage *image)
2073 gpointer iter = NULL;
2076 while ((p = mono_signature_get_params (sig, &iter)) != NULL)
2077 if (type_in_image (p, image))
2080 return type_in_image (mono_signature_get_return_type (sig), image);
2084 ginst_in_image (MonoGenericInst *ginst, MonoImage *image)
2088 for (i = 0; i < ginst->type_argc; ++i) {
2089 if (type_in_image (ginst->type_argv [i], image))
2097 gclass_in_image (MonoGenericClass *gclass, MonoImage *image)
2099 return gclass->container_class->image == image ||
2100 ginst_in_image (gclass->context.class_inst, image);
2104 type_in_image (MonoType *type, MonoImage *image)
2107 switch (type->type) {
2108 case MONO_TYPE_GENERICINST:
2109 return gclass_in_image (type->data.generic_class, image);
2111 type = type->data.type;
2113 case MONO_TYPE_SZARRAY:
2114 type = &type->data.klass->byval_arg;
2116 case MONO_TYPE_ARRAY:
2117 type = &type->data.array->eklass->byval_arg;
2119 case MONO_TYPE_FNPTR:
2120 return signature_in_image (type->data.method, image);
2121 case MONO_TYPE_VAR: {
2122 MonoGenericContainer *container = mono_type_get_generic_param_owner (type);
2124 g_assert (!container->is_method);
2126 * FIXME: The following check is here solely
2127 * for monodis, which uses the internal
2129 * mono_metadata_load_generic_params(). The
2130 * caller of that function needs to fill in
2131 * owner->klass or owner->method of the
2132 * returned struct, but monodis doesn't do
2133 * that. The image unloading depends on that,
2134 * however, so a crash results without this
2137 if (!container->owner.klass)
2138 return container->image == image;
2139 return container->owner.klass->image == image;
2141 return type->data.generic_param->image == image;
2144 case MONO_TYPE_MVAR: {
2145 MonoGenericContainer *container = mono_type_get_generic_param_owner (type);
2146 if (type->data.generic_param->image == image)
2149 g_assert (container->is_method);
2150 if (!container->owner.method)
2151 /* RefEmit created generic param whose method is not finished */
2152 return container->image == image;
2153 return container->owner.method->klass->image == image;
2155 return type->data.generic_param->image == image;
2159 /* At this point, we should've avoided all potential allocations in mono_class_from_mono_type () */
2160 return image == mono_class_from_mono_type (type)->image;
2167 } CleanForImageUserData;
2170 steal_gclass_in_image (gpointer key, gpointer value, gpointer data)
2172 MonoGenericClass *gclass = key;
2173 CleanForImageUserData *user_data = data;
2175 if (!gclass_in_image (gclass, user_data->image))
2178 user_data->list = g_slist_prepend (user_data->list, gclass);
2183 steal_ginst_in_image (gpointer key, gpointer value, gpointer data)
2185 MonoGenericInst *ginst = key;
2186 CleanForImageUserData *user_data = data;
2188 if (!ginst_in_image (ginst, user_data->image))
2191 user_data->list = g_slist_prepend (user_data->list, ginst);
2196 inflated_method_in_image (gpointer key, gpointer value, gpointer data)
2198 MonoImage *image = data;
2199 MonoMethodInflated *method = key;
2202 // https://bugzilla.novell.com/show_bug.cgi?id=458168
2203 return method->declaring->klass->image == image ||
2204 (method->context.class_inst && ginst_in_image (method->context.class_inst, image)) ||
2205 (method->context.method_inst && ginst_in_image (method->context.method_inst, image)) || (((MonoMethod*)method)->signature && signature_in_image (mono_method_signature ((MonoMethod*)method), image));
2209 inflated_signature_in_image (gpointer key, gpointer value, gpointer data)
2211 MonoImage *image = data;
2212 MonoInflatedMethodSignature *sig = key;
2214 return signature_in_image (sig->sig, image) ||
2215 (sig->context.class_inst && ginst_in_image (sig->context.class_inst, image)) ||
2216 (sig->context.method_inst && ginst_in_image (sig->context.method_inst, image));
2220 mono_metadata_clean_for_image (MonoImage *image)
2222 CleanForImageUserData ginst_data, gclass_data;
2225 /* The data structures could reference each other so we delete them in two phases */
2226 ginst_data.image = gclass_data.image = image;
2227 ginst_data.list = gclass_data.list = NULL;
2229 mono_loader_lock ();
2230 /* Collect the items to delete and remove them from the hash table */
2231 g_hash_table_foreach_steal (generic_inst_cache, steal_ginst_in_image, &ginst_data);
2232 g_hash_table_foreach_steal (generic_class_cache, steal_gclass_in_image, &gclass_data);
2233 if (generic_method_cache)
2234 g_hash_table_foreach_remove (generic_method_cache, inflated_method_in_image, image);
2235 if (generic_signature_cache)
2236 g_hash_table_foreach_remove (generic_signature_cache, inflated_signature_in_image, image);
2237 /* Delete the removed items */
2238 for (l = ginst_data.list; l; l = l->next)
2239 free_generic_inst (l->data);
2240 for (l = gclass_data.list; l; l = l->next)
2241 free_generic_class (l->data);
2242 g_slist_free (ginst_data.list);
2243 g_slist_free (gclass_data.list);
2244 mono_class_unregister_image_generic_subclasses (image);
2245 mono_loader_unlock ();
2249 free_inflated_method (MonoMethodInflated *imethod)
2252 MonoMethod *method = (MonoMethod*)imethod;
2254 mono_marshal_free_inflated_wrappers (method);
2256 if (method->signature)
2257 mono_metadata_free_inflated_signature (method->signature);
2259 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))) {
2260 MonoMethodNormal* mn = (MonoMethodNormal*) method;
2261 MonoMethodHeader *header = mn->header;
2264 /* Allocated in inflate_generic_header () */
2265 for (i = 0; i < header->num_locals; ++i)
2266 mono_metadata_free_type (header->locals [i]);
2267 g_free (header->clauses);
2276 free_generic_inst (MonoGenericInst *ginst)
2280 for (i = 0; i < ginst->type_argc; ++i)
2281 mono_metadata_free_type (ginst->type_argv [i]);
2287 free_generic_class (MonoGenericClass *gclass)
2291 /* FIXME: The dynamic case */
2292 if (gclass->cached_class && !gclass->cached_class->image->dynamic && !mono_generic_class_is_generic_type_definition (gclass)) {
2293 MonoClass *class = gclass->cached_class;
2295 /* Allocated in mono_class_init () */
2296 g_free (class->methods);
2298 g_free (class->ext->properties);
2299 /* Allocated in mono_generic_class_get_class () */
2300 g_free (class->interfaces);
2302 } else if (gclass->is_dynamic) {
2303 MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *)gclass;
2305 for (i = 0; i < dgclass->count_fields; ++i) {
2306 MonoClassField *field = dgclass->fields + i;
2307 mono_metadata_free_type (field->type);
2308 g_free ((char*)field->name);
2310 for (i = 0; i < dgclass->count_properties; ++i) {
2311 MonoProperty *property = dgclass->properties + i;
2312 g_free ((char*)property->name);
2314 for (i = 0; i < dgclass->count_events; ++i) {
2315 MonoEvent *event = dgclass->events + i;
2316 g_free ((char*)event->name);
2319 g_free (dgclass->methods);
2320 g_free (dgclass->ctors);
2321 g_free (dgclass->fields);
2322 g_free (dgclass->properties);
2323 g_free (dgclass->events);
2324 g_free (dgclass->field_objects);
2325 g_free (dgclass->field_generic_types);
2326 if (!mono_generic_class_is_generic_type_definition (gclass))
2327 g_free (gclass->cached_class);
2333 free_inflated_signature (MonoInflatedMethodSignature *sig)
2335 mono_metadata_free_inflated_signature (sig->sig);
2340 * LOCKING: assumes the loader lock is held.
2343 mono_method_inflated_lookup (MonoMethodInflated* method, gboolean cache)
2346 if (!generic_method_cache)
2347 generic_method_cache = g_hash_table_new_full (inflated_method_hash, inflated_method_equal, NULL, (GDestroyNotify)free_inflated_method);
2348 g_hash_table_insert (generic_method_cache, method, method);
2351 if (generic_method_cache)
2352 return g_hash_table_lookup (generic_method_cache, method);
2358 * mono_metadata_get_inflated_signature:
2360 * Given an inflated signature and a generic context, return a canonical copy of the
2361 * signature. The returned signature might be equal to SIG or it might be a cached copy.
2363 MonoMethodSignature *
2364 mono_metadata_get_inflated_signature (MonoMethodSignature *sig, MonoGenericContext *context)
2366 MonoInflatedMethodSignature helper;
2367 MonoInflatedMethodSignature *res;
2369 mono_loader_lock ();
2370 if (!generic_signature_cache)
2371 generic_signature_cache = g_hash_table_new_full (inflated_signature_hash, inflated_signature_equal, NULL, (GDestroyNotify)free_inflated_signature);
2374 helper.context.class_inst = context->class_inst;
2375 helper.context.method_inst = context->method_inst;
2376 res = g_hash_table_lookup (generic_signature_cache, &helper);
2378 res = g_new0 (MonoInflatedMethodSignature, 1);
2380 res->context.class_inst = context->class_inst;
2381 res->context.method_inst = context->method_inst;
2382 g_hash_table_insert (generic_signature_cache, res, res);
2385 mono_loader_unlock ();
2390 * mono_metadata_get_generic_inst:
2392 * Given a list of types, return a MonoGenericInst that represents that list.
2393 * The returned MonoGenericInst has its own copy of the list of types. The list
2394 * passed in the argument can be freed, modified or disposed of.
2398 mono_metadata_get_generic_inst (int type_argc, MonoType **type_argv)
2400 MonoGenericInst *ginst;
2403 int size = MONO_SIZEOF_GENERIC_INST + type_argc * sizeof (MonoType *);
2405 for (i = 0; i < type_argc; ++i)
2406 if (mono_class_is_open_constructed_type (type_argv [i]))
2408 is_open = (i < type_argc);
2410 ginst = alloca (size);
2412 ginst->is_open = is_open;
2413 ginst->type_argc = type_argc;
2414 memcpy (ginst->type_argv, type_argv, type_argc * sizeof (MonoType *));
2416 mono_loader_lock ();
2418 ginst = g_hash_table_lookup (generic_inst_cache, ginst);
2420 ginst = g_malloc (size);
2421 ginst->id = ++next_generic_inst_id;
2422 ginst->is_open = is_open;
2423 ginst->type_argc = type_argc;
2425 for (i = 0; i < type_argc; ++i)
2426 ginst->type_argv [i] = mono_metadata_type_dup (NULL, type_argv [i]);
2428 g_hash_table_insert (generic_inst_cache, ginst, ginst);
2431 mono_loader_unlock ();
2436 mono_metadata_is_type_builder_generic_type_definition (MonoClass *container_class, MonoGenericInst *inst, gboolean is_dynamic)
2438 MonoGenericContainer *container = container_class->generic_container;
2440 if (!is_dynamic || container_class->wastypebuilder || container->type_argc != inst->type_argc)
2442 return inst == container->context.class_inst;
2446 * mono_metadata_lookup_generic_class:
2448 * Returns a MonoGenericClass with the given properties.
2452 mono_metadata_lookup_generic_class (MonoClass *container_class, MonoGenericInst *inst, gboolean is_dynamic)
2454 MonoGenericClass *gclass;
2455 MonoGenericClass helper;
2456 gboolean is_tb_open = mono_metadata_is_type_builder_generic_type_definition (container_class, inst, is_dynamic);
2458 helper.container_class = container_class;
2459 helper.context.class_inst = inst;
2460 helper.context.method_inst = NULL;
2461 helper.is_dynamic = is_dynamic; /* We use this in a hash lookup, which does not attempt to downcast the pointer */
2462 helper.is_tb_open = is_tb_open;
2463 helper.cached_class = NULL;
2465 mono_loader_lock ();
2467 gclass = g_hash_table_lookup (generic_class_cache, &helper);
2469 /* A tripwire just to keep us honest */
2470 g_assert (!helper.cached_class);
2473 mono_loader_unlock ();
2478 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
2479 gclass = &dgclass->generic_class;
2480 gclass->is_dynamic = 1;
2482 gclass = g_new0 (MonoGenericClass, 1);
2485 gclass->is_tb_open = is_tb_open;
2486 gclass->container_class = container_class;
2487 gclass->context.class_inst = inst;
2488 gclass->context.method_inst = NULL;
2489 if (inst == container_class->generic_container->context.class_inst && !is_tb_open)
2490 gclass->cached_class = container_class;
2492 g_hash_table_insert (generic_class_cache, gclass, gclass);
2494 mono_loader_unlock ();
2500 * mono_metadata_inflate_generic_inst:
2502 * Instantiate the generic instance @ginst with the context @context.
2506 mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context)
2508 MonoType **type_argv;
2509 MonoGenericInst *nginst;
2512 if (!ginst->is_open)
2515 type_argv = g_new0 (MonoType*, ginst->type_argc);
2517 for (i = 0; i < ginst->type_argc; i++)
2518 type_argv [i] = mono_class_inflate_generic_type (ginst->type_argv [i], context);
2520 nginst = mono_metadata_get_generic_inst (ginst->type_argc, type_argv);
2522 for (i = 0; i < ginst->type_argc; i++)
2523 mono_metadata_free_type (type_argv [i]);
2530 mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContainer *container,
2531 int count, const char *ptr, const char **rptr)
2533 MonoType **type_argv;
2534 MonoGenericInst *ginst;
2537 type_argv = g_new0 (MonoType*, count);
2539 for (i = 0; i < count; i++) {
2540 MonoType *t = mono_metadata_parse_type_full (m, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
2551 ginst = mono_metadata_get_generic_inst (count, type_argv);
2559 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContainer *container,
2560 const char *ptr, const char **rptr)
2562 MonoGenericInst *inst;
2567 gtype = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
2571 gklass = mono_class_from_mono_type (gtype);
2572 if (!gklass->generic_container)
2575 count = mono_metadata_decode_value (ptr, &ptr);
2576 inst = mono_metadata_parse_generic_inst (m, container, count, ptr, &ptr);
2583 type->data.generic_class = mono_metadata_lookup_generic_class (gklass, inst, FALSE);
2589 * @gc: The generic container to normalize
2590 * @type: The kind of generic parameters the resulting generic-container should contain
2593 static MonoGenericContainer *
2594 select_container (MonoGenericContainer *gc, MonoTypeEnum type)
2596 gboolean is_var = (type == MONO_TYPE_VAR);
2600 g_assert (is_var || type == MONO_TYPE_MVAR);
2603 if (gc->is_method || gc->parent)
2605 * The current MonoGenericContainer is a generic method -> its `parent'
2606 * points to the containing class'es container.
2615 * mono_metadata_parse_generic_param:
2616 * @generic_container: Our MonoClass's or MonoMethodNormal's MonoGenericContainer;
2617 * see mono_metadata_parse_type_full() for details.
2618 * Internal routine to parse a generic type parameter.
2619 * LOCKING: Acquires the loader lock
2621 static MonoGenericParam *
2622 mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContainer *generic_container,
2623 MonoTypeEnum type, const char *ptr, const char **rptr)
2625 int index = mono_metadata_decode_value (ptr, &ptr);
2629 generic_container = select_container (generic_container, type);
2630 if (!generic_container) {
2631 /* Create dummy MonoGenericParam */
2632 MonoGenericParam *param;
2634 param = mono_image_alloc0 (m, sizeof (MonoGenericParam));
2641 g_assert (index < generic_container->type_argc);
2642 return mono_generic_container_get_param (generic_container, index);
2646 * mono_metadata_get_shared_type:
2648 * Return a shared instance of TYPE, if available, NULL otherwise.
2649 * Shared MonoType instances help save memory. Their contents should not be modified
2650 * by the caller. They do not need to be freed as their lifetime is bound by either
2651 * the lifetime of the runtime (builtin types), or the lifetime of the MonoClass
2652 * instance they are embedded in. If they are freed, they should be freed using
2653 * mono_metadata_free_type () instead of g_free ().
2656 mono_metadata_get_shared_type (MonoType *type)
2660 /* No need to use locking since nobody is modifying the hash table */
2661 if ((cached = g_hash_table_lookup (type_cache, type)))
2664 switch (type->type){
2665 case MONO_TYPE_CLASS:
2666 case MONO_TYPE_VALUETYPE:
2667 if (type == &type->data.klass->byval_arg)
2669 if (type == &type->data.klass->this_arg)
2678 * do_mono_metadata_parse_type:
2679 * @type: MonoType to be filled in with the return value
2681 * @generic_context: generics_context
2682 * @ptr: pointer to the encoded type
2683 * @rptr: pointer where the end of the encoded type is saved
2685 * Internal routine used to "fill" the contents of @type from an
2686 * allocated pointer. This is done this way to avoid doing too
2687 * many mini-allocations (particularly for the MonoFieldType which
2688 * most of the time is just a MonoType, but sometimes might be augmented).
2690 * This routine is used by mono_metadata_parse_type and
2691 * mono_metadata_parse_field_type
2693 * This extracts a Type as specified in Partition II (22.2.12)
2695 * Returns: FALSE if the type could not be loaded
2698 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container,
2699 const char *ptr, const char **rptr)
2702 type->type = mono_metadata_decode_value (ptr, &ptr);
2704 switch (type->type){
2705 case MONO_TYPE_VOID:
2706 case MONO_TYPE_BOOLEAN:
2707 case MONO_TYPE_CHAR:
2720 case MONO_TYPE_STRING:
2721 case MONO_TYPE_OBJECT:
2722 case MONO_TYPE_TYPEDBYREF:
2724 case MONO_TYPE_VALUETYPE:
2725 case MONO_TYPE_CLASS: {
2727 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
2728 type->data.klass = mono_class_get (m, token);
2729 if (!type->data.klass)
2733 case MONO_TYPE_SZARRAY: {
2734 MonoType *etype = mono_metadata_parse_type_full (m, container, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
2737 type->data.klass = mono_class_from_mono_type (etype);
2738 if (!type->data.klass)
2743 type->data.type = mono_metadata_parse_type_full (m, container, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
2744 if (!type->data.type)
2747 case MONO_TYPE_FNPTR:
2748 type->data.method = mono_metadata_parse_method_signature_full (m, container, 0, ptr, &ptr);
2749 if (!type->data.method)
2752 case MONO_TYPE_ARRAY:
2753 type->data.array = mono_metadata_parse_array_full (m, container, ptr, &ptr);
2754 if (!type->data.array)
2757 case MONO_TYPE_MVAR:
2758 if (container && !container->is_method)
2761 type->data.generic_param = mono_metadata_parse_generic_param (m, container, type->type, ptr, &ptr);
2762 if (!type->data.generic_param)
2765 case MONO_TYPE_GENERICINST:
2766 ok = do_mono_metadata_parse_generic_class (type, m, container, ptr, &ptr);
2769 g_warning ("type 0x%02x not handled in do_mono_metadata_parse_type on image %s", type->type, m->name);
2779 * mono_metadata_free_type:
2780 * @type: type to free
2782 * Free the memory allocated for type @type which is allocated on the heap.
2785 mono_metadata_free_type (MonoType *type)
2787 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
2790 switch (type->type){
2791 case MONO_TYPE_OBJECT:
2792 case MONO_TYPE_STRING:
2793 if (!type->data.klass)
2796 case MONO_TYPE_CLASS:
2797 case MONO_TYPE_VALUETYPE:
2798 if (type == &type->data.klass->byval_arg || type == &type->data.klass->this_arg)
2802 mono_metadata_free_type (type->data.type);
2804 case MONO_TYPE_FNPTR:
2805 mono_metadata_free_method_signature (type->data.method);
2807 case MONO_TYPE_ARRAY:
2808 mono_metadata_free_array (type->data.array);
2817 hex_dump (const char *buffer, int base, int count)
2819 int show_header = 1;
2827 for (i = 0; i < count; i++){
2830 printf ("\n0x%08x: ", (unsigned char) base + i);
2832 printf ("%02x ", (unsigned char) (buffer [i]));
2839 * @mh: The Method header
2840 * @ptr: Points to the beginning of the Section Data (25.3)
2843 parse_section_data (MonoImage *m, MonoMethodHeader *mh, const unsigned char *ptr)
2845 unsigned char sect_data_flags;
2846 const unsigned char *sptr;
2848 guint32 sect_data_len;
2851 /* align on 32-bit boundary */
2852 sptr = ptr = dword_align (ptr);
2853 sect_data_flags = *ptr;
2856 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
2858 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
2861 sect_data_len = ptr [0];
2865 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
2866 hex_dump (sptr, 0, sect_data_len+8);
2867 g_print ("\nheader: ");
2868 hex_dump (sptr-4, 0, 4);
2872 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
2873 const unsigned char *p = dword_align (ptr);
2875 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
2876 /* we could just store a pointer if we don't need to byteswap */
2877 mh->clauses = mono_image_alloc0 (m, sizeof (MonoExceptionClause) * mh->num_clauses);
2878 for (i = 0; i < mh->num_clauses; ++i) {
2879 MonoExceptionClause *ec = &mh->clauses [i];
2882 ec->flags = read32 (p);
2883 ec->try_offset = read32 (p + 4);
2884 ec->try_len = read32 (p + 8);
2885 ec->handler_offset = read32 (p + 12);
2886 ec->handler_len = read32 (p + 16);
2887 tof_value = read32 (p + 20);
2890 ec->flags = read16 (p);
2891 ec->try_offset = read16 (p + 2);
2892 ec->try_len = *(p + 4);
2893 ec->handler_offset = read16 (p + 5);
2894 ec->handler_len = *(p + 7);
2895 tof_value = read32 (p + 8);
2898 if (ec->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2899 ec->data.filter_offset = tof_value;
2900 } else if (ec->flags == MONO_EXCEPTION_CLAUSE_NONE) {
2901 ec->data.catch_class = tof_value? mono_class_get (m, tof_value): 0;
2903 ec->data.catch_class = NULL;
2905 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
2909 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
2910 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
2917 * mono_metadata_parse_mh_full:
2918 * @m: metadata context
2919 * @generic_context: generics context
2920 * @ptr: pointer to the method header.
2922 * Decode the method header at @ptr, including pointer to the IL code,
2923 * info about local variables and optional exception tables.
2924 * This is a Mono runtime internal function.
2926 * LOCKING: Acquires the loader lock.
2928 * Returns: a MonoMethodHeader allocated from the image mempool.
2931 mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, const char *ptr)
2933 MonoMethodHeader *mh;
2934 unsigned char flags = *(const unsigned char *) ptr;
2935 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
2937 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
2938 const unsigned char *code;
2941 g_return_val_if_fail (ptr != NULL, NULL);
2944 case METHOD_HEADER_TINY_FORMAT:
2945 mh = mono_image_alloc0 (m, MONO_SIZEOF_METHOD_HEADER);
2948 local_var_sig_tok = 0;
2949 mh->code_size = flags >> 2;
2950 mh->code = (unsigned char*)ptr;
2952 case METHOD_HEADER_FAT_FORMAT:
2953 fat_flags = read16 (ptr);
2955 hsize = (fat_flags >> 12) & 0xf;
2956 max_stack = read16 (ptr);
2958 code_size = read32 (ptr);
2960 local_var_sig_tok = read32 (ptr);
2963 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
2968 code = (unsigned char*)ptr;
2970 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
2974 * There are more sections
2976 ptr = (char*)code + code_size;
2982 if (local_var_sig_tok) {
2983 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
2984 const char *locals_ptr;
2985 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
2986 int len=0, i, bsize;
2988 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
2990 if (!mono_verifier_verify_standalone_signature (m, cols [MONO_STAND_ALONE_SIGNATURE], NULL))
2993 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
2994 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
2995 if (*locals_ptr != 0x07)
2996 g_warning ("wrong signature for locals blob");
2998 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
2999 mh = mono_image_alloc0 (m, MONO_SIZEOF_METHOD_HEADER + len * sizeof (MonoType*));
3000 mh->num_locals = len;
3001 for (i = 0; i < len; ++i) {
3002 mh->locals [i] = mono_metadata_parse_type_full (
3003 m, container, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
3004 if (!mh->locals [i]) {
3009 mh = mono_image_alloc0 (m, MONO_SIZEOF_METHOD_HEADER);
3012 mh->code_size = code_size;
3013 mh->max_stack = max_stack;
3014 mh->init_locals = init_locals;
3015 if (fat_flags & METHOD_HEADER_MORE_SECTS)
3016 parse_section_data (m, mh, (const unsigned char*)ptr);
3021 * mono_metadata_parse_mh:
3022 * @generic_context: generics context
3023 * @ptr: pointer to the method header.
3025 * Decode the method header at @ptr, including pointer to the IL code,
3026 * info about local variables and optional exception tables.
3027 * This is a Mono runtime internal function.
3029 * Returns: a MonoMethodHeader.
3032 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
3034 MonoMethodHeader *res;
3036 mono_loader_lock ();
3038 res = mono_metadata_parse_mh_full (m, NULL, ptr);
3040 mono_loader_unlock ();
3046 * mono_metadata_free_mh:
3047 * @mh: a method header
3049 * Free the memory allocated for the method header.
3050 * This is a Mono runtime internal function.
3053 mono_metadata_free_mh (MonoMethodHeader *mh)
3055 /* Allocated from the mempool */
3059 * mono_method_header_get_code:
3060 * @header: a MonoMethodHeader pointer
3061 * @code_size: memory location for returning the code size
3062 * @max_stack: memory location for returning the max stack
3064 * Method header accessor to retreive info about the IL code properties:
3065 * a pointer to the IL code itself, the size of the code and the max number
3066 * of stack slots used by the code.
3068 * Returns: pointer to the IL code represented by the method header.
3070 const unsigned char*
3071 mono_method_header_get_code (MonoMethodHeader *header, guint32* code_size, guint32* max_stack)
3074 *code_size = header->code_size;
3076 *max_stack = header->max_stack;
3077 return header->code;
3081 * mono_method_header_get_locals:
3082 * @header: a MonoMethodHeader pointer
3083 * @num_locals: memory location for returning the number of local variables
3084 * @init_locals: memory location for returning the init_locals flag
3086 * Method header accessor to retreive info about the local variables:
3087 * an array of local types, the number of locals and whether the locals
3088 * are supposed to be initialized to 0 on method entry
3090 * Returns: pointer to an array of types of the local variables
3093 mono_method_header_get_locals (MonoMethodHeader *header, guint32* num_locals, gboolean *init_locals)
3096 *num_locals = header->num_locals;
3098 *init_locals = header->init_locals;
3099 return header->locals;
3103 * mono_method_header_get_num_clauses:
3104 * @header: a MonoMethodHeader pointer
3106 * Method header accessor to retreive the number of exception clauses.
3108 * Returns: the number of exception clauses present
3111 mono_method_header_get_num_clauses (MonoMethodHeader *header)
3113 return header->num_clauses;
3117 * mono_method_header_get_clauses:
3118 * @header: a MonoMethodHeader pointer
3119 * @method: MonoMethod the header belongs to
3120 * @iter: pointer to a iterator
3121 * @clause: pointer to a MonoExceptionClause structure which will be filled with the info
3123 * Get the info about the exception clauses in the method. Set *iter to NULL to
3124 * initiate the iteration, then call the method repeatedly until it returns FALSE.
3125 * At each iteration, the structure pointed to by clause if filled with the
3126 * exception clause information.
3128 * Returns: TRUE if clause was filled with info, FALSE if there are no more exception
3132 mono_method_header_get_clauses (MonoMethodHeader *header, MonoMethod *method, gpointer *iter, MonoExceptionClause *clause)
3134 MonoExceptionClause *sc;
3135 /* later we'll be able to use this interface to parse the clause info on demand,
3136 * without allocating anything.
3138 if (!iter || !header->num_clauses)
3141 *iter = sc = header->clauses;
3147 if (sc < header->clauses + header->num_clauses) {
3156 * mono_metadata_parse_field_type:
3157 * @m: metadata context to extract information from
3158 * @ptr: pointer to the field signature
3159 * @rptr: pointer updated to match the end of the decoded stream
3161 * Parses the field signature, and returns the type information for it.
3163 * Returns: The MonoType that was extracted from @ptr.
3166 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
3168 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
3172 * mono_metadata_parse_param:
3173 * @m: metadata context to extract information from
3174 * @ptr: pointer to the param signature
3175 * @rptr: pointer updated to match the end of the decoded stream
3177 * Parses the param signature, and returns the type information for it.
3179 * Returns: The MonoType that was extracted from @ptr.
3182 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
3184 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
3188 * mono_metadata_token_from_dor:
3189 * @dor_token: A TypeDefOrRef coded index
3191 * dor_token is a TypeDefOrRef coded index: it contains either
3192 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
3193 * bits contain an index into the table.
3195 * Returns: an expanded token
3198 mono_metadata_token_from_dor (guint32 dor_index)
3202 table = dor_index & 0x03;
3203 idx = dor_index >> 2;
3206 case 0: /* TypeDef */
3207 return MONO_TOKEN_TYPE_DEF | idx;
3208 case 1: /* TypeRef */
3209 return MONO_TOKEN_TYPE_REF | idx;
3210 case 2: /* TypeSpec */
3211 return MONO_TOKEN_TYPE_SPEC | idx;
3213 g_assert_not_reached ();
3220 * We use this to pass context information to the row locator
3223 int idx; /* The index that we are trying to locate */
3224 int col_idx; /* The index in the row where idx may be stored */
3225 MonoTableInfo *t; /* pointer to the table */
3230 * How the row locator works.
3235 * ___|___------> _______
3238 * A column in the rows of table A references an index in table B.
3239 * For example A may be the TYPEDEF table and B the METHODDEF table.
3241 * Given an index in table B we want to get the row in table A
3242 * where the column n references our index in B.
3244 * In the locator_t structure:
3246 * col_idx is the column number
3247 * index is the index in table B
3248 * result will be the index in table A
3251 * Table A Table B column (in table A)
3252 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
3253 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
3254 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
3255 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
3256 * METHODSEM PROPERTY ASSOCIATION (encoded index)
3258 * Note that we still don't support encoded indexes.
3262 typedef_locator (const void *a, const void *b)
3264 locator_t *loc = (locator_t *) a;
3265 const char *bb = (const char *) b;
3266 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
3267 guint32 col, col_next;
3269 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
3275 * Need to check that the next row is valid.
3277 if (typedef_index + 1 < loc->t->rows) {
3278 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
3279 if (loc->idx >= col_next)
3282 if (col == col_next)
3286 loc->result = typedef_index;
3292 table_locator (const void *a, const void *b)
3294 locator_t *loc = (locator_t *) a;
3295 const char *bb = (const char *) b;
3296 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
3299 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
3301 if (loc->idx == col) {
3302 loc->result = table_index;
3312 declsec_locator (const void *a, const void *b)
3314 locator_t *loc = (locator_t *) a;
3315 const char *bb = (const char *) b;
3316 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
3319 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
3321 if (loc->idx == col) {
3322 loc->result = table_index;
3334 * Return the 1-based row index in TABLE, which must be one of the *Ptr tables,
3335 * which contains IDX.
3338 search_ptr_table (MonoImage *image, int table, int idx)
3340 MonoTableInfo *ptrdef = &image->tables [table];
3343 /* Use a linear search to find our index in the table */
3344 for (i = 0; i < ptrdef->rows; i ++)
3345 /* All the Ptr tables have the same structure */
3346 if (mono_metadata_decode_row_col (ptrdef, i, 0) == idx)
3349 if (i < ptrdef->rows)
3356 * mono_metadata_typedef_from_field:
3357 * @meta: metadata context
3358 * @index: FieldDef token
3360 * Returns: the 1-based index into the TypeDef table of the type that
3361 * declared the field described by @index, or 0 if not found.
3364 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
3366 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
3372 loc.idx = mono_metadata_token_index (index);
3373 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
3376 if (meta->uncompressed_metadata)
3377 loc.idx = search_ptr_table (meta, MONO_TABLE_FIELD_POINTER, loc.idx);
3379 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
3380 g_assert_not_reached ();
3382 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3383 return loc.result + 1;
3387 * mono_metadata_typedef_from_method:
3388 * @meta: metadata context
3389 * @index: MethodDef token
3391 * Returns: the 1-based index into the TypeDef table of the type that
3392 * declared the method described by @index. 0 if not found.
3395 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
3397 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
3403 loc.idx = mono_metadata_token_index (index);
3404 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
3407 if (meta->uncompressed_metadata)
3408 loc.idx = search_ptr_table (meta, MONO_TABLE_METHOD_POINTER, loc.idx);
3410 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
3411 g_assert_not_reached ();
3413 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3414 return loc.result + 1;
3418 * mono_metadata_interfaces_from_typedef_full:
3419 * @meta: metadata context
3420 * @index: typedef token
3421 * @interfaces: Out parameter used to store the interface array
3422 * @count: Out parameter used to store the number of interfaces
3423 * @heap_alloc_result: if TRUE the result array will be g_malloc'd
3424 * @context: The generic context
3426 * The array of interfaces that the @index typedef token implements is returned in
3427 * @interfaces. The number of elements in the array is returned in @count.
3429 * LOCKING: Assumes the loader lock is held.
3431 * Returns: TRUE on success, FALSE on failure.
3434 mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, MonoClass ***interfaces, guint *count, gboolean heap_alloc_result, MonoGenericContext *context)
3436 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
3439 guint32 cols [MONO_INTERFACEIMPL_SIZE];
3448 loc.idx = mono_metadata_token_index (index);
3449 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
3452 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3457 * We may end up in the middle of the rows...
3460 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
3466 while (pos < tdef->rows) {
3467 mono_metadata_decode_row (tdef, pos, cols, MONO_INTERFACEIMPL_SIZE);
3468 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
3473 if (heap_alloc_result)
3474 result = g_new0 (MonoClass*, pos - start);
3476 result = mono_image_alloc0 (meta, sizeof (MonoClass*) * (pos - start));
3479 while (pos < tdef->rows) {
3482 mono_metadata_decode_row (tdef, pos, cols, MONO_INTERFACEIMPL_SIZE);
3483 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
3485 iface = mono_class_get_full (
3486 meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]), context);
3489 result [pos - start] = iface;
3492 *count = pos - start;
3493 *interfaces = result;
3498 * @meta: metadata context
3499 * @index: typedef token
3500 * @count: Out parameter used to store the number of interfaces
3502 * The array of interfaces that the @index typedef token implements is returned in
3503 * @interfaces. The number of elements in the array is returned in @count. The returned
3504 * array is g_malloc'd and the caller must free it.
3506 * LOCKING: Acquires the loader lock .
3508 * Returns: the interface array on success, NULL on failure.
3512 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
3514 MonoClass **interfaces;
3517 mono_loader_lock ();
3518 rv = mono_metadata_interfaces_from_typedef_full (meta, index, &interfaces, count, TRUE, NULL);
3519 mono_loader_unlock ();
3527 * mono_metadata_nested_in_typedef:
3528 * @meta: metadata context
3529 * @index: typedef token
3531 * Returns: the 1-based index into the TypeDef table of the type
3532 * where the type described by @index is nested.
3533 * Retruns 0 if @index describes a non-nested type.
3536 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
3538 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
3544 loc.idx = mono_metadata_token_index (index);
3545 loc.col_idx = MONO_NESTED_CLASS_NESTED;
3548 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3551 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3552 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
3556 * mono_metadata_nesting_typedef:
3557 * @meta: metadata context
3558 * @index: typedef token
3560 * Returns: the 1-based index into the TypeDef table of the first type
3561 * that is nested inside the type described by @index. The search starts at
3562 * @start_index. returns 0 if no such type is found.
3565 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
3567 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
3569 guint32 class_index = mono_metadata_token_index (index);
3574 start = start_index;
3576 while (start <= tdef->rows) {
3577 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
3583 if (start > tdef->rows)
3590 * mono_metadata_packing_from_typedef:
3591 * @meta: metadata context
3592 * @index: token representing a type
3594 * Returns: the info stored in the ClassLAyout table for the given typedef token
3595 * into the @packing and @size pointers.
3596 * Returns 0 if the info is not found.
3599 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
3601 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
3603 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
3608 loc.idx = mono_metadata_token_index (index);
3609 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
3612 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3615 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
3617 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
3619 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
3621 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3622 return loc.result + 1;
3626 * mono_metadata_custom_attrs_from_index:
3627 * @meta: metadata context
3628 * @index: token representing the parent
3630 * Returns: the 1-based index into the CustomAttribute table of the first
3631 * attribute which belongs to the metadata object described by @index.
3632 * Returns 0 if no such attribute is found.
3635 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
3637 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
3644 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
3647 /* FIXME: Index translation */
3649 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3652 /* Find the first entry by searching backwards */
3653 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
3656 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3657 return loc.result + 1;
3661 * mono_metadata_declsec_from_index:
3662 * @meta: metadata context
3663 * @index: token representing the parent
3665 * Returns: the 0-based index into the DeclarativeSecurity table of the first
3666 * attribute which belongs to the metadata object described by @index.
3667 * Returns -1 if no such attribute is found.
3670 mono_metadata_declsec_from_index (MonoImage *meta, guint32 index)
3672 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_DECLSECURITY];
3679 loc.col_idx = MONO_DECL_SECURITY_PARENT;
3682 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
3685 /* Find the first entry by searching backwards */
3686 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_DECL_SECURITY_PARENT) == index))
3694 mono_backtrace (int limit)
3699 backtrace (array, limit);
3700 names = backtrace_symbols (array, limit);
3701 for (i =0; i < limit; ++i) {
3702 g_print ("\t%s\n", names [i]);
3709 /*#define __alignof__(a) sizeof(a)*/
3710 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
3715 * @t: the type to return the size of
3717 * Returns: the number of bytes required to hold an instance of this
3721 mono_type_size (MonoType *t, int *align)
3728 *align = __alignof__(gpointer);
3729 return sizeof (gpointer);
3733 case MONO_TYPE_VOID:
3736 case MONO_TYPE_BOOLEAN:
3737 *align = __alignof__(gint8);
3741 *align = __alignof__(gint8);
3743 case MONO_TYPE_CHAR:
3746 *align = __alignof__(gint16);
3750 *align = __alignof__(gint32);
3753 *align = __alignof__(float);
3757 *align = __alignof__(gint64);
3760 *align = __alignof__(double);
3764 *align = __alignof__(gpointer);
3765 return sizeof (gpointer);
3766 case MONO_TYPE_STRING:
3767 *align = __alignof__(gpointer);
3768 return sizeof (gpointer);
3769 case MONO_TYPE_OBJECT:
3770 *align = __alignof__(gpointer);
3771 return sizeof (gpointer);
3772 case MONO_TYPE_VALUETYPE: {
3773 if (t->data.klass->enumtype)
3774 return mono_type_size (mono_class_enum_basetype (t->data.klass), align);
3776 return mono_class_value_size (t->data.klass, (guint32*)align);
3778 case MONO_TYPE_CLASS:
3779 case MONO_TYPE_SZARRAY:
3781 case MONO_TYPE_FNPTR:
3782 case MONO_TYPE_ARRAY:
3783 *align = __alignof__(gpointer);
3784 return sizeof (gpointer);
3785 case MONO_TYPE_TYPEDBYREF:
3786 return mono_class_value_size (mono_defaults.typed_reference_class, (guint32*)align);
3787 case MONO_TYPE_GENERICINST: {
3788 MonoGenericClass *gclass = t->data.generic_class;
3789 MonoClass *container_class = gclass->container_class;
3791 // g_assert (!gclass->inst->is_open);
3793 if (container_class->valuetype) {
3794 if (container_class->enumtype)
3795 return mono_type_size (mono_class_enum_basetype (container_class), align);
3797 return mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align);
3799 *align = __alignof__(gpointer);
3800 return sizeof (gpointer);
3804 case MONO_TYPE_MVAR:
3805 /* FIXME: Martin, this is wrong. */
3806 *align = __alignof__(gpointer);
3807 return sizeof (gpointer);
3809 g_error ("mono_type_size: type 0x%02x unknown", t->type);
3815 * mono_type_stack_size:
3816 * @t: the type to return the size it uses on the stack
3818 * Returns: the number of bytes required to hold an instance of this
3819 * type on the runtime stack
3822 mono_type_stack_size (MonoType *t, int *align)
3824 return mono_type_stack_size_internal (t, align, FALSE);
3828 mono_type_stack_size_internal (MonoType *t, int *align, gboolean allow_open)
3831 #if SIZEOF_VOID_P == SIZEOF_REGISTER
3832 int stack_slot_size = sizeof (gpointer);
3833 int stack_slot_align = __alignof__ (gpointer);
3834 #elif SIZEOF_VOID_P < SIZEOF_REGISTER
3835 int stack_slot_size = SIZEOF_REGISTER;
3836 int stack_slot_align = SIZEOF_REGISTER;
3839 g_assert (t != NULL);
3845 *align = stack_slot_align;
3846 return stack_slot_size;
3850 case MONO_TYPE_BOOLEAN:
3851 case MONO_TYPE_CHAR:
3860 case MONO_TYPE_STRING:
3861 case MONO_TYPE_OBJECT:
3862 case MONO_TYPE_CLASS:
3863 case MONO_TYPE_SZARRAY:
3865 case MONO_TYPE_FNPTR:
3866 case MONO_TYPE_ARRAY:
3867 *align = stack_slot_align;
3868 return stack_slot_size;
3870 case MONO_TYPE_MVAR:
3871 g_assert (allow_open);
3872 *align = stack_slot_align;
3873 return stack_slot_size;
3874 case MONO_TYPE_TYPEDBYREF:
3875 *align = stack_slot_align;
3876 return stack_slot_size * 3;
3878 *align = __alignof__(float);
3879 return sizeof (float);
3882 *align = __alignof__(gint64);
3883 return sizeof (gint64);
3885 *align = __alignof__(double);
3886 return sizeof (double);
3887 case MONO_TYPE_VALUETYPE: {
3890 if (t->data.klass->enumtype)
3891 return mono_type_stack_size_internal (mono_class_enum_basetype (t->data.klass), align, allow_open);
3893 size = mono_class_value_size (t->data.klass, (guint32*)align);
3895 *align = *align + stack_slot_align - 1;
3896 *align &= ~(stack_slot_align - 1);
3898 size += stack_slot_size - 1;
3899 size &= ~(stack_slot_size - 1);
3904 case MONO_TYPE_GENERICINST: {
3905 MonoGenericClass *gclass = t->data.generic_class;
3906 MonoClass *container_class = gclass->container_class;
3909 g_assert (!gclass->context.class_inst->is_open);
3911 if (container_class->valuetype) {
3912 if (container_class->enumtype)
3913 return mono_type_stack_size_internal (mono_class_enum_basetype (container_class), align, allow_open);
3915 guint32 size = mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align);
3917 *align = *align + stack_slot_align - 1;
3918 *align &= ~(stack_slot_align - 1);
3920 size += stack_slot_size - 1;
3921 size &= ~(stack_slot_size - 1);
3926 *align = stack_slot_align;
3927 return stack_slot_size;
3931 g_error ("type 0x%02x unknown", t->type);
3937 mono_type_generic_inst_is_valuetype (MonoType *type)
3939 g_assert (type->type == MONO_TYPE_GENERICINST);
3940 return type->data.generic_class->container_class->valuetype;
3944 mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass)
3946 return gclass->container_class->valuetype;
3950 _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2, gboolean signature_only)
3952 MonoGenericInst *i1 = g1->context.class_inst;
3953 MonoGenericInst *i2 = g2->context.class_inst;
3955 if (g1->is_dynamic != g2->is_dynamic)
3957 if (!mono_metadata_class_equal (g1->container_class, g2->container_class, signature_only))
3959 if (!mono_generic_inst_equal_full (i1, i2, signature_only))
3961 return g1->is_tb_open == g2->is_tb_open;
3965 _mono_metadata_generic_class_container_equal (const MonoGenericClass *g1, MonoClass *c2, gboolean signature_only)
3967 MonoGenericInst *i1 = g1->context.class_inst;
3968 MonoGenericInst *i2 = c2->generic_container->context.class_inst;
3970 if (!mono_metadata_class_equal (g1->container_class, c2, signature_only))
3972 if (!mono_generic_inst_equal_full (i1, i2, signature_only))
3974 return !g1->is_tb_open;
3978 mono_metadata_generic_context_hash (const MonoGenericContext *context)
3980 /* FIXME: check if this seed is good enough */
3981 guint hash = 0xc01dfee7;
3982 if (context->class_inst)
3983 hash = ((hash << 5) - hash) ^ mono_metadata_generic_inst_hash (context->class_inst);
3984 if (context->method_inst)
3985 hash = ((hash << 5) - hash) ^ mono_metadata_generic_inst_hash (context->method_inst);
3990 mono_metadata_generic_context_equal (const MonoGenericContext *g1, const MonoGenericContext *g2)
3992 return g1->class_inst == g2->class_inst && g1->method_inst == g2->method_inst;
3996 * mono_metadata_type_hash:
3999 * Computes an hash value for @t1 to be used in GHashTable.
4002 mono_metadata_type_hash (MonoType *t1)
4004 guint hash = t1->type;
4006 hash |= t1->byref << 6; /* do not collide with t1->type values */
4008 case MONO_TYPE_VALUETYPE:
4009 case MONO_TYPE_CLASS:
4010 case MONO_TYPE_SZARRAY:
4011 /* check if the distribution is good enough */
4012 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
4014 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
4015 case MONO_TYPE_ARRAY:
4016 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
4017 case MONO_TYPE_GENERICINST:
4018 return ((hash << 5) - hash) ^ mono_generic_class_hash (t1->data.generic_class);
4024 mono_metadata_generic_param_equal (MonoGenericParam *p1, MonoGenericParam *p2, gboolean signature_only)
4028 if (mono_generic_param_num (p1) != mono_generic_param_num (p2))
4032 * We have to compare the image as well because if we didn't,
4033 * the generic_inst_cache lookup wouldn't care about the image
4034 * of generic params, so what could happen is that a generic
4035 * inst with params from image A is put into the cache, then
4036 * image B gets that generic inst from the cache, image A is
4037 * unloaded, so the inst is deleted, but image B still retains
4040 * The AOT runtime doesn't set the image when it's decoding
4041 * types, so we only compare it when the owner is NULL.
4043 if (mono_generic_param_owner (p1) == mono_generic_param_owner (p2) &&
4044 (mono_generic_param_owner (p1) || p1->image == p2->image))
4048 * If `signature_only' is true, we're comparing two (method) signatures.
4049 * In this case, the owner of two type parameters doesn't need to match.
4052 return signature_only;
4056 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only)
4060 if (c1->generic_class && c2->generic_class)
4061 return _mono_metadata_generic_class_equal (c1->generic_class, c2->generic_class, signature_only);
4062 if (c1->generic_class && c2->generic_container)
4063 return _mono_metadata_generic_class_container_equal (c1->generic_class, c2, signature_only);
4064 if (c1->generic_container && c2->generic_class)
4065 return _mono_metadata_generic_class_container_equal (c2->generic_class, c1, signature_only);
4066 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
4067 return mono_metadata_generic_param_equal (
4068 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
4069 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
4070 return mono_metadata_generic_param_equal (
4071 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
4072 if (signature_only &&
4073 (c1->byval_arg.type == MONO_TYPE_SZARRAY) && (c2->byval_arg.type == MONO_TYPE_SZARRAY))
4074 return mono_metadata_class_equal (c1->byval_arg.data.klass, c2->byval_arg.data.klass, signature_only);
4079 mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only)
4081 gpointer iter1 = 0, iter2 = 0;
4085 if (s1->call_convention != s2->call_convention)
4087 if (s1->sentinelpos != s2->sentinelpos)
4089 if (s1->hasthis != s2->hasthis)
4091 if (s1->explicit_this != s2->explicit_this)
4093 if (! do_mono_metadata_type_equal (s1->ret, s2->ret, signature_only))
4095 if (s1->param_count != s2->param_count)
4099 MonoType *t1 = mono_signature_get_params (s1, &iter1);
4100 MonoType *t2 = mono_signature_get_params (s2, &iter2);
4102 if (t1 == NULL || t2 == NULL)
4104 if (! do_mono_metadata_type_equal (t1, t2, signature_only))
4110 * mono_metadata_type_equal:
4114 * Determine if @t1 and @t2 represent the same type.
4115 * Returns: #TRUE if @t1 and @t2 are equal.
4118 do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only)
4120 if (t1->type != t2->type || t1->byref != t2->byref)
4124 case MONO_TYPE_VOID:
4125 case MONO_TYPE_BOOLEAN:
4126 case MONO_TYPE_CHAR:
4137 case MONO_TYPE_STRING:
4140 case MONO_TYPE_OBJECT:
4141 case MONO_TYPE_TYPEDBYREF:
4143 case MONO_TYPE_VALUETYPE:
4144 case MONO_TYPE_CLASS:
4145 case MONO_TYPE_SZARRAY:
4146 return mono_metadata_class_equal (t1->data.klass, t2->data.klass, signature_only);
4148 return do_mono_metadata_type_equal (t1->data.type, t2->data.type, signature_only);
4149 case MONO_TYPE_ARRAY:
4150 if (t1->data.array->rank != t2->data.array->rank)
4152 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass, signature_only);
4153 case MONO_TYPE_GENERICINST:
4154 return _mono_metadata_generic_class_equal (
4155 t1->data.generic_class, t2->data.generic_class, signature_only);
4157 return mono_metadata_generic_param_equal (
4158 t1->data.generic_param, t2->data.generic_param, signature_only);
4159 case MONO_TYPE_MVAR:
4160 return mono_metadata_generic_param_equal (
4161 t1->data.generic_param, t2->data.generic_param, signature_only);
4162 case MONO_TYPE_FNPTR:
4163 return mono_metadata_fnptr_equal (t1->data.method, t2->data.method, signature_only);
4165 g_error ("implement type compare for %0x!", t1->type);
4173 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
4175 return do_mono_metadata_type_equal (t1, t2, FALSE);
4179 * mono_metadata_type_equal_full:
4182 * @signature_only: if signature only comparison should be made
4184 * Determine if @t1 and @t2 are signature compatible if @signature_only is #TRUE, otherwise
4185 * behaves the same way as mono_metadata_type_equal.
4186 * The function mono_metadata_type_equal(a, b) is just a shortcut for mono_metadata_type_equal_full(a, b, FALSE).
4187 * Returns: #TRUE if @t1 and @t2 are equal taking @signature_only into account.
4190 mono_metadata_type_equal_full (MonoType *t1, MonoType *t2, gboolean signature_only)
4192 return do_mono_metadata_type_equal (t1, t2, signature_only);
4196 * mono_metadata_signature_equal:
4197 * @sig1: a signature
4198 * @sig2: another signature
4200 * Determine if @sig1 and @sig2 represent the same signature, with the
4201 * same number of arguments and the same types.
4202 * Returns: #TRUE if @sig1 and @sig2 are equal.
4205 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
4209 if (sig1->hasthis != sig2->hasthis || sig1->param_count != sig2->param_count)
4212 if (sig1->generic_param_count != sig2->generic_param_count)
4216 * We're just comparing the signatures of two methods here:
4218 * If we have two generic methods `void Foo<U> (U u)' and `void Bar<V> (V v)',
4219 * U and V are equal here.
4221 * That's what the `signature_only' argument of do_mono_metadata_type_equal() is for.
4224 for (i = 0; i < sig1->param_count; i++) {
4225 MonoType *p1 = sig1->params[i];
4226 MonoType *p2 = sig2->params[i];
4228 /* if (p1->attrs != p2->attrs)
4231 if (!do_mono_metadata_type_equal (p1, p2, TRUE))
4235 if (!do_mono_metadata_type_equal (sig1->ret, sig2->ret, TRUE))
4241 * mono_metadata_type_dup:
4242 * @image: image to alloc memory from
4243 * @original: type to duplicate
4245 * Returns: copy of type allocated from the image's mempool (or from the heap, if @image is null).
4248 mono_metadata_type_dup (MonoImage *image, const MonoType *o)
4251 int sizeof_o = MONO_SIZEOF_TYPE;
4253 sizeof_o += o->num_mods * sizeof (MonoCustomMod);
4255 r = image ? mono_image_alloc0 (image, sizeof_o) : g_malloc (sizeof_o);
4257 memcpy (r, o, sizeof_o);
4259 if (o->type == MONO_TYPE_PTR) {
4260 r->data.type = mono_metadata_type_dup (image, o->data.type);
4261 } else if (o->type == MONO_TYPE_ARRAY) {
4262 r->data.array = mono_dup_array_type (image, o->data.array);
4263 } else if (o->type == MONO_TYPE_FNPTR) {
4264 /*FIXME the dup'ed signature is leaked mono_metadata_free_type*/
4265 r->data.method = mono_metadata_signature_deep_dup (image, o->data.method);
4271 mono_signature_hash (MonoMethodSignature *sig)
4273 guint i, res = sig->ret->type;
4275 for (i = 0; i < sig->param_count; i++)
4276 res = (res << 5) - res + mono_type_hash (sig->params[i]);
4282 * mono_metadata_encode_value:
4283 * @value: value to encode
4284 * @buf: buffer where to write the compressed representation
4285 * @endbuf: pointer updated to point at the end of the encoded output
4287 * Encodes the value @value in the compressed representation used
4288 * in metadata and stores the result in @buf. @buf needs to be big
4289 * enough to hold the data (4 bytes).
4292 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
4298 else if (value < 0x4000) {
4299 p [0] = 0x80 | (value >> 8);
4300 p [1] = value & 0xff;
4303 p [0] = (value >> 24) | 0xc0;
4304 p [1] = (value >> 16) & 0xff;
4305 p [2] = (value >> 8) & 0xff;
4306 p [3] = value & 0xff;
4314 * mono_metadata_field_info:
4315 * @meta: the Image the field is defined in
4316 * @index: the index in the field table representing the field
4317 * @offset: a pointer to an integer where to store the offset that
4318 * may have been specified for the field in a FieldLayout table
4319 * @rva: a pointer to the RVA of the field data in the image that
4320 * may have been defined in a FieldRVA table
4321 * @marshal_spec: a pointer to the marshal spec that may have been
4322 * defined for the field in a FieldMarshal table.
4324 * Gather info for field @index that may have been defined in the FieldLayout,
4325 * FieldRVA and FieldMarshal tables.
4326 * Either of offset, rva and marshal_spec can be NULL if you're not interested
4330 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
4331 MonoMarshalSpec **marshal_spec)
4333 mono_metadata_field_info_full (meta, index, offset, rva, marshal_spec, FALSE);
4337 mono_metadata_field_info_with_mempool (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
4338 MonoMarshalSpec **marshal_spec)
4340 mono_metadata_field_info_full (meta, index, offset, rva, marshal_spec, TRUE);
4344 mono_metadata_field_info_full (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
4345 MonoMarshalSpec **marshal_spec, gboolean alloc_from_image)
4347 MonoTableInfo *tdef;
4350 loc.idx = index + 1;
4351 if (meta->uncompressed_metadata)
4352 loc.idx = search_ptr_table (meta, MONO_TABLE_FIELD_POINTER, loc.idx);
4355 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
4357 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
4360 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
4361 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
4363 *offset = (guint32)-1;
4367 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
4369 loc.col_idx = MONO_FIELD_RVA_FIELD;
4372 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
4374 * LAMESPEC: There is no signature, no nothing, just the raw data.
4376 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
4384 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
4385 *marshal_spec = mono_metadata_parse_marshal_spec_full (alloc_from_image ? meta : NULL, p);
4392 * mono_metadata_get_constant_index:
4393 * @meta: the Image the field is defined in
4394 * @index: the token that may have a row defined in the constants table
4395 * @hint: possible position for the row
4397 * @token must be a FieldDef, ParamDef or PropertyDef token.
4399 * Returns: the index into the Constants table or 0 if not found.
4402 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
4404 MonoTableInfo *tdef;
4406 guint32 index = mono_metadata_token_index (token);
4408 tdef = &meta->tables [MONO_TABLE_CONSTANT];
4409 index <<= MONO_HASCONSTANT_BITS;
4410 switch (mono_metadata_token_table (token)) {
4411 case MONO_TABLE_FIELD:
4412 index |= MONO_HASCONSTANT_FIEDDEF;
4414 case MONO_TABLE_PARAM:
4415 index |= MONO_HASCONSTANT_PARAM;
4417 case MONO_TABLE_PROPERTY:
4418 index |= MONO_HASCONSTANT_PROPERTY;
4421 g_warning ("Not a valid token for the constant table: 0x%08x", token);
4425 loc.col_idx = MONO_CONSTANT_PARENT;
4428 /* FIXME: Index translation */
4430 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
4433 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
4434 return loc.result + 1;
4440 * mono_metadata_events_from_typedef:
4441 * @meta: metadata context
4442 * @index: 0-based index (in the TypeDef table) describing a type
4444 * Returns: the 0-based index in the Event table for the events in the
4445 * type. The last event that belongs to the type (plus 1) is stored
4446 * in the @end_idx pointer.
4449 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
4453 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
4461 loc.col_idx = MONO_EVENT_MAP_PARENT;
4462 loc.idx = index + 1;
4464 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4467 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
4468 if (loc.result + 1 < tdef->rows) {
4469 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
4471 end = meta->tables [MONO_TABLE_EVENT].rows;
4479 * mono_metadata_methods_from_event:
4480 * @meta: metadata context
4481 * @index: 0-based index (in the Event table) describing a event
4483 * Returns: the 0-based index in the MethodDef table for the methods in the
4484 * event. The last method that belongs to the event (plus 1) is stored
4485 * in the @end_idx pointer.
4488 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
4492 guint32 cols [MONO_METHOD_SEMA_SIZE];
4493 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
4499 if (meta->uncompressed_metadata)
4500 index = search_ptr_table (meta, MONO_TABLE_EVENT_POINTER, index + 1) - 1;
4503 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
4504 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
4506 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
4511 * We may end up in the middle of the rows...
4514 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
4520 while (end < msemt->rows) {
4521 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
4522 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
4531 * mono_metadata_properties_from_typedef:
4532 * @meta: metadata context
4533 * @index: 0-based index (in the TypeDef table) describing a type
4535 * Returns: the 0-based index in the Property table for the properties in the
4536 * type. The last property that belongs to the type (plus 1) is stored
4537 * in the @end_idx pointer.
4540 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
4544 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
4552 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
4553 loc.idx = index + 1;
4555 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4558 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
4559 if (loc.result + 1 < tdef->rows) {
4560 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
4562 end = meta->tables [MONO_TABLE_PROPERTY].rows;
4570 * mono_metadata_methods_from_property:
4571 * @meta: metadata context
4572 * @index: 0-based index (in the PropertyDef table) describing a property
4574 * Returns: the 0-based index in the MethodDef table for the methods in the
4575 * property. The last method that belongs to the property (plus 1) is stored
4576 * in the @end_idx pointer.
4579 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
4583 guint32 cols [MONO_METHOD_SEMA_SIZE];
4584 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
4590 if (meta->uncompressed_metadata)
4591 index = search_ptr_table (meta, MONO_TABLE_PROPERTY_POINTER, index + 1) - 1;
4594 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
4595 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
4597 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
4602 * We may end up in the middle of the rows...
4605 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
4611 while (end < msemt->rows) {
4612 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
4613 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
4622 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
4625 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
4630 /* No index translation seems to be needed */
4633 loc.col_idx = MONO_IMPLMAP_MEMBER;
4634 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
4636 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4639 return loc.result + 1;
4643 * @image: context where the image is created
4644 * @type_spec: typespec token
4646 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
4650 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
4652 guint32 idx = mono_metadata_token_index (type_spec);
4654 guint32 cols [MONO_TYPESPEC_SIZE];
4657 MonoType *type, *type2;
4659 mono_loader_lock ();
4661 type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec));
4663 mono_loader_unlock ();
4667 t = &image->tables [MONO_TABLE_TYPESPEC];
4669 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
4670 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
4672 if (!mono_verifier_verify_typespec_signature (image, cols [MONO_TYPESPEC_SIGNATURE], NULL)) {
4673 mono_loader_unlock ();
4677 len = mono_metadata_decode_value (ptr, &ptr);
4679 type = mono_image_alloc0 (image, MONO_SIZEOF_TYPE);
4681 if (*ptr == MONO_TYPE_BYREF) {
4686 if (!do_mono_metadata_parse_type (type, image, NULL, ptr, &ptr)) {
4687 mono_loader_unlock ();
4691 type2 = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec));
4694 mono_loader_unlock ();
4698 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
4700 mono_loader_unlock ();
4707 mono_image_strndup (MonoImage *image, const char *data, guint len)
4711 return g_strndup (data, len);
4712 res = mono_image_alloc (image, len + 1);
4713 memcpy (res, data, len);
4719 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
4721 return mono_metadata_parse_marshal_spec_full (NULL, ptr);
4725 mono_metadata_parse_marshal_spec_full (MonoImage *image, const char *ptr)
4727 MonoMarshalSpec *res;
4729 const char *start = ptr;
4731 /* fixme: this is incomplete, but I cant find more infos in the specs */
4734 res = mono_image_alloc0 (image, sizeof (MonoMarshalSpec));
4736 res = g_new0 (MonoMarshalSpec, 1);
4738 len = mono_metadata_decode_value (ptr, &ptr);
4739 res->native = *ptr++;
4741 if (res->native == MONO_NATIVE_LPARRAY) {
4742 res->data.array_data.param_num = -1;
4743 res->data.array_data.num_elem = -1;
4744 res->data.array_data.elem_mult = -1;
4746 if (ptr - start <= len)
4747 res->data.array_data.elem_type = *ptr++;
4748 if (ptr - start <= len)
4749 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
4750 if (ptr - start <= len)
4751 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
4752 if (ptr - start <= len) {
4754 * LAMESPEC: Older spec versions say this parameter comes before
4755 * num_elem. Never spec versions don't talk about elem_mult at
4756 * all, but csc still emits it, and it is used to distinguish
4757 * between param_num being 0, and param_num being omitted.
4758 * So if (param_num == 0) && (num_elem > 0), then
4759 * elem_mult == 0 -> the array size is num_elem
4760 * elem_mult == 1 -> the array size is @param_num + num_elem
4762 res->data.array_data.elem_mult = mono_metadata_decode_value (ptr, &ptr);
4766 if (res->native == MONO_NATIVE_BYVALTSTR) {
4767 if (ptr - start <= len)
4768 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
4771 if (res->native == MONO_NATIVE_BYVALARRAY) {
4772 if (ptr - start <= len)
4773 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
4776 if (res->native == MONO_NATIVE_CUSTOM) {
4777 /* skip unused type guid */
4778 len = mono_metadata_decode_value (ptr, &ptr);
4780 /* skip unused native type name */
4781 len = mono_metadata_decode_value (ptr, &ptr);
4783 /* read custom marshaler type name */
4784 len = mono_metadata_decode_value (ptr, &ptr);
4785 res->data.custom_data.custom_name = mono_image_strndup (image, ptr, len);
4787 /* read cookie string */
4788 len = mono_metadata_decode_value (ptr, &ptr);
4789 res->data.custom_data.cookie = mono_image_strndup (image, ptr, len);
4792 if (res->native == MONO_NATIVE_SAFEARRAY) {
4793 res->data.safearray_data.elem_type = 0;
4794 res->data.safearray_data.num_elem = 0;
4795 if (ptr - start <= len)
4796 res->data.safearray_data.elem_type = *ptr++;
4797 if (ptr - start <= len)
4798 res->data.safearray_data.num_elem = *ptr++;
4804 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
4806 if (spec->native == MONO_NATIVE_CUSTOM) {
4807 g_free (spec->data.custom_data.custom_name);
4808 g_free (spec->data.custom_data.cookie);
4814 * mono_type_to_unmanaged:
4816 * Returns: A MonoMarshalNative enumeration value (MONO_NATIVE_) value
4817 * describing the underlying native reprensetation of the type.
4819 * In addition the value pointed by
4820 * "conv" will contain the kind of marshalling required for this
4821 * particular type one of the MONO_MARSHAL_CONV_ enumeration values.
4824 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
4825 gboolean unicode, MonoMarshalConv *conv)
4827 MonoMarshalConv dummy_conv;
4833 *conv = MONO_MARSHAL_CONV_NONE;
4836 return MONO_NATIVE_UINT;
4840 case MONO_TYPE_BOOLEAN:
4842 switch (mspec->native) {
4843 case MONO_NATIVE_VARIANTBOOL:
4844 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
4845 return MONO_NATIVE_VARIANTBOOL;
4846 case MONO_NATIVE_BOOLEAN:
4847 *conv = MONO_MARSHAL_CONV_BOOL_I4;
4848 return MONO_NATIVE_BOOLEAN;
4849 case MONO_NATIVE_I1:
4850 case MONO_NATIVE_U1:
4851 return mspec->native;
4853 g_error ("cant marshal bool to native type %02x", mspec->native);
4856 *conv = MONO_MARSHAL_CONV_BOOL_I4;
4857 return MONO_NATIVE_BOOLEAN;
4858 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
4859 case MONO_TYPE_I1: return MONO_NATIVE_I1;
4860 case MONO_TYPE_U1: return MONO_NATIVE_U1;
4861 case MONO_TYPE_I2: return MONO_NATIVE_I2;
4862 case MONO_TYPE_U2: return MONO_NATIVE_U2;
4863 case MONO_TYPE_I4: return MONO_NATIVE_I4;
4864 case MONO_TYPE_U4: return MONO_NATIVE_U4;
4865 case MONO_TYPE_I8: return MONO_NATIVE_I8;
4866 case MONO_TYPE_U8: return MONO_NATIVE_U8;
4867 case MONO_TYPE_R4: return MONO_NATIVE_R4;
4868 case MONO_TYPE_R8: return MONO_NATIVE_R8;
4869 case MONO_TYPE_STRING:
4871 switch (mspec->native) {
4872 case MONO_NATIVE_BSTR:
4873 *conv = MONO_MARSHAL_CONV_STR_BSTR;
4874 return MONO_NATIVE_BSTR;
4875 case MONO_NATIVE_LPSTR:
4876 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
4877 return MONO_NATIVE_LPSTR;
4878 case MONO_NATIVE_LPWSTR:
4879 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
4880 return MONO_NATIVE_LPWSTR;
4881 case MONO_NATIVE_LPTSTR:
4882 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
4883 return MONO_NATIVE_LPTSTR;
4884 case MONO_NATIVE_ANSIBSTR:
4885 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
4886 return MONO_NATIVE_ANSIBSTR;
4887 case MONO_NATIVE_TBSTR:
4888 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
4889 return MONO_NATIVE_TBSTR;
4890 case MONO_NATIVE_BYVALTSTR:
4892 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
4894 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
4895 return MONO_NATIVE_BYVALTSTR;
4897 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);
4901 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
4902 return MONO_NATIVE_LPWSTR;
4905 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
4906 return MONO_NATIVE_LPSTR;
4908 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
4909 case MONO_TYPE_VALUETYPE: /*FIXME*/
4910 if (type->data.klass->enumtype) {
4911 t = mono_class_enum_basetype (type->data.klass)->type;
4914 if (type->data.klass == mono_defaults.handleref_class){
4915 *conv = MONO_MARSHAL_CONV_HANDLEREF;
4916 return MONO_NATIVE_INT;
4918 return MONO_NATIVE_STRUCT;
4919 case MONO_TYPE_SZARRAY:
4920 case MONO_TYPE_ARRAY:
4922 switch (mspec->native) {
4923 case MONO_NATIVE_BYVALARRAY:
4924 if ((type->data.klass->element_class == mono_defaults.char_class) && !unicode)
4925 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALCHARARRAY;
4927 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
4928 return MONO_NATIVE_BYVALARRAY;
4929 case MONO_NATIVE_SAFEARRAY:
4930 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
4931 return MONO_NATIVE_SAFEARRAY;
4932 case MONO_NATIVE_LPARRAY:
4933 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
4934 return MONO_NATIVE_LPARRAY;
4936 g_error ("cant marshal array as native type %02x", mspec->native);
4940 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
4941 return MONO_NATIVE_LPARRAY;
4942 case MONO_TYPE_I: return MONO_NATIVE_INT;
4943 case MONO_TYPE_U: return MONO_NATIVE_UINT;
4944 case MONO_TYPE_CLASS:
4945 case MONO_TYPE_OBJECT: {
4946 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
4948 switch (mspec->native) {
4949 case MONO_NATIVE_STRUCT:
4950 return MONO_NATIVE_STRUCT;
4951 case MONO_NATIVE_CUSTOM:
4952 return MONO_NATIVE_CUSTOM;
4953 case MONO_NATIVE_INTERFACE:
4954 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
4955 return MONO_NATIVE_INTERFACE;
4956 case MONO_NATIVE_IDISPATCH:
4957 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
4958 return MONO_NATIVE_IDISPATCH;
4959 case MONO_NATIVE_IUNKNOWN:
4960 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
4961 return MONO_NATIVE_IUNKNOWN;
4962 case MONO_NATIVE_FUNC:
4963 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
4964 type->data.klass == mono_defaults.delegate_class ||
4965 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
4966 *conv = MONO_MARSHAL_CONV_DEL_FTN;
4967 return MONO_NATIVE_FUNC;
4971 g_error ("cant marshal object as native type %02x", mspec->native);
4974 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
4975 type->data.klass == mono_defaults.delegate_class ||
4976 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
4977 *conv = MONO_MARSHAL_CONV_DEL_FTN;
4978 return MONO_NATIVE_FUNC;
4980 if (mono_defaults.safehandle_class && type->data.klass == mono_defaults.safehandle_class){
4981 *conv = MONO_MARSHAL_CONV_SAFEHANDLE;
4982 return MONO_NATIVE_INT;
4984 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
4985 return MONO_NATIVE_STRUCT;
4987 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
4988 case MONO_TYPE_GENERICINST:
4989 type = &type->data.generic_class->container_class->byval_arg;
4992 case MONO_TYPE_TYPEDBYREF:
4994 g_error ("type 0x%02x not handled in marshal", t);
4996 return MONO_NATIVE_MAX;
5000 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
5003 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
5009 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
5010 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
5012 /* FIXME: Index translation */
5014 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
5017 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
5021 method_from_method_def_or_ref (MonoImage *m, guint32 tok, MonoGenericContext *context)
5023 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
5025 switch (tok & MONO_METHODDEFORREF_MASK) {
5026 case MONO_METHODDEFORREF_METHODDEF:
5027 return mono_get_method_full (m, MONO_TOKEN_METHOD_DEF | idx, NULL, context);
5028 case MONO_METHODDEFORREF_METHODREF:
5029 return mono_get_method_full (m, MONO_TOKEN_MEMBER_REF | idx, NULL, context);
5031 g_assert_not_reached ();
5036 * mono_class_get_overrides_full:
5038 * Return the method overrides belonging to class @type_token in @overrides, and
5039 * the number of overrides in @num_overrides.
5041 * Returns: TRUE on success, FALSE on failure.
5044 mono_class_get_overrides_full (MonoImage *image, guint32 type_token, MonoMethod ***overrides, gint32 *num_overrides,
5045 MonoGenericContext *generic_context)
5048 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
5051 guint32 cols [MONO_METHODIMPL_SIZE];
5052 MonoMethod **result;
5063 loc.col_idx = MONO_METHODIMPL_CLASS;
5064 loc.idx = mono_metadata_token_index (type_token);
5066 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
5072 * We may end up in the middle of the rows...
5075 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
5080 while (end < tdef->rows) {
5081 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
5087 result = g_new (MonoMethod*, num * 2);
5088 for (i = 0; i < num; ++i) {
5091 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
5092 method = method_from_method_def_or_ref (
5093 image, cols [MONO_METHODIMPL_DECLARATION], generic_context);
5096 result [i * 2] = method;
5097 method = method_from_method_def_or_ref (
5098 image, cols [MONO_METHODIMPL_BODY], generic_context);
5101 result [i * 2 + 1] = method;
5104 *overrides = result;
5106 *num_overrides = num;
5111 * mono_guid_to_string:
5113 * Converts a 16 byte Microsoft GUID to the standard string representation.
5116 mono_guid_to_string (const guint8 *guid)
5118 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
5119 guid[3], guid[2], guid[1], guid[0],
5123 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
5127 get_constraints (MonoImage *image, int owner, MonoClass ***constraints, MonoGenericContainer *container)
5129 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
5130 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
5131 guint32 i, token, found;
5132 MonoClass *klass, **res;
5133 GList *cons = NULL, *tmp;
5134 MonoGenericContext *context = &container->context;
5136 *constraints = NULL;
5138 for (i = 0; i < tdef->rows; ++i) {
5139 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
5140 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
5141 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
5142 klass = mono_class_get_full (image, token, context);
5143 cons = g_list_append (cons, klass);
5146 /* contiguous list finished */
5153 res = g_new0 (MonoClass*, found + 1);
5154 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
5155 res [i] = tmp->data;
5163 * mono_metadata_get_generic_param_row:
5166 * @token: TypeOrMethodDef token, owner for GenericParam
5167 * @owner: coded token, set on return
5169 * Returns: 1-based row-id in the GenericParam table whose
5170 * owner is @token. 0 if not found.
5173 mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner)
5175 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
5182 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
5183 *owner = MONO_TYPEORMETHOD_TYPE;
5184 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
5185 *owner = MONO_TYPEORMETHOD_METHOD;
5187 g_error ("wrong token %x to get_generic_param_row", token);
5190 *owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
5193 loc.col_idx = MONO_GENERICPARAM_OWNER;
5196 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
5199 /* Find the first entry by searching backwards */
5200 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_GENERICPARAM_OWNER) == loc.idx))
5203 return loc.result + 1;
5207 mono_metadata_has_generic_params (MonoImage *image, guint32 token)
5210 return mono_metadata_get_generic_param_row (image, token, &owner);
5214 * mono_metadata_load_generic_param_constraints:
5216 * Load the generic parameter constraints for the newly created generic type or method
5217 * represented by @token and @container. The @container is the new container which has
5218 * been returned by a call to mono_metadata_load_generic_params() with this @token.
5221 mono_metadata_load_generic_param_constraints (MonoImage *image, guint32 token,
5222 MonoGenericContainer *container)
5224 guint32 start_row, i, owner;
5225 if (! (start_row = mono_metadata_get_generic_param_row (image, token, &owner)))
5227 for (i = 0; i < container->type_argc; i++)
5228 get_constraints (image, start_row + i, &mono_generic_container_get_param_info (container, i)->constraints, container);
5232 * mono_metadata_load_generic_params:
5234 * Load the type parameters from the type or method definition @token.
5236 * Use this method after parsing a type or method definition to figure out whether it's a generic
5237 * type / method. When parsing a method definition, @parent_container points to the generic container
5238 * of the current class, if any.
5240 * Note: This method does not load the constraints: for typedefs, this has to be done after fully
5241 * creating the type.
5243 * Returns: NULL if @token is not a generic type or method definition or the new generic container.
5245 * LOCKING: Acquires the loader lock
5248 MonoGenericContainer *
5249 mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericContainer *parent_container)
5251 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
5252 guint32 cols [MONO_GENERICPARAM_SIZE];
5253 guint32 i, owner = 0, n;
5254 MonoGenericContainer *container;
5255 MonoGenericParamFull *params;
5256 MonoGenericContext *context;
5258 if (!(i = mono_metadata_get_generic_param_row (image, token, &owner)))
5260 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
5263 container = mono_image_alloc0 (image, sizeof (MonoGenericContainer));
5264 container->image = image;
5267 params = g_realloc (params, sizeof (MonoGenericParamFull) * n);
5268 memset (¶ms [n - 1], 0, sizeof (MonoGenericParamFull));
5269 params [n - 1].param.owner = container;
5270 params [n - 1].param.num = cols [MONO_GENERICPARAM_NUMBER];
5271 params [n - 1].info.token = i | MONO_TOKEN_GENERIC_PARAM;
5272 params [n - 1].info.flags = cols [MONO_GENERICPARAM_FLAGS];
5273 params [n - 1].info.name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
5274 if (params [n - 1].param.num != n - 1)
5275 g_warning ("GenericParam table unsorted or hole in generic param sequence: token %d", i);
5276 if (++i > tdef->rows)
5278 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
5279 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
5281 container->type_argc = n;
5282 container->type_params = mono_image_alloc0 (image, sizeof (MonoGenericParamFull) * n);
5283 memcpy (container->type_params, params, sizeof (MonoGenericParamFull) * n);
5285 container->parent = parent_container;
5287 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
5288 container->is_method = 1;
5290 g_assert (container->parent == NULL || container->is_method);
5292 context = &container->context;
5293 if (container->is_method) {
5294 context->class_inst = container->parent ? container->parent->context.class_inst : NULL;
5295 context->method_inst = mono_get_shared_generic_inst (container);
5297 context->class_inst = mono_get_shared_generic_inst (container);
5304 mono_get_shared_generic_inst (MonoGenericContainer *container)
5306 MonoType **type_argv;
5308 MonoGenericInst *nginst;
5311 type_argv = g_new0 (MonoType *, container->type_argc);
5312 helper = g_new0 (MonoType, container->type_argc);
5314 for (i = 0; i < container->type_argc; i++) {
5315 MonoType *t = &helper [i];
5317 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
5318 t->data.generic_param = mono_generic_container_get_param (container, i);
5323 nginst = mono_metadata_get_generic_inst (container->type_argc, type_argv);
5332 * mono_type_is_byref:
5333 * @type: the MonoType operated on
5335 * Returns: #TRUE if @type represents a type passed by reference,
5339 mono_type_is_byref (MonoType *type)
5345 * mono_type_get_type:
5346 * @type: the MonoType operated on
5348 * Returns: the IL type value for @type. This is one of the MonoTypeEnum
5349 * enum members like MONO_TYPE_I4 or MONO_TYPE_STRING.
5352 mono_type_get_type (MonoType *type)
5358 * mono_type_get_signature:
5359 * @type: the MonoType operated on
5361 * It is only valid to call this function if @type is a MONO_TYPE_FNPTR.
5363 * Returns: the MonoMethodSignature pointer that describes the signature
5364 * of the function pointer @type represents.
5366 MonoMethodSignature*
5367 mono_type_get_signature (MonoType *type)
5369 g_assert (type->type == MONO_TYPE_FNPTR);
5370 return type->data.method;
5374 * mono_type_get_class:
5375 * @type: the MonoType operated on
5377 * It is only valid to call this function if @type is a MONO_TYPE_CLASS or a
5378 * MONO_TYPE_VALUETYPE. For more general functionality, use mono_class_from_mono_type (),
5381 * Returns: the MonoClass pointer that describes the class that @type represents.
5384 mono_type_get_class (MonoType *type)
5386 /* FIXME: review the runtime users before adding the assert here */
5387 return type->data.klass;
5391 * mono_type_get_array_type:
5392 * @type: the MonoType operated on
5394 * It is only valid to call this function if @type is a MONO_TYPE_ARRAY.
5396 * Returns: a MonoArrayType struct describing the array type that @type
5397 * represents. The info includes details such as rank, array element type
5398 * and the sizes and bounds of multidimensional arrays.
5401 mono_type_get_array_type (MonoType *type)
5403 return type->data.array;
5407 * mono_type_get_ptr_type:
5408 * @type: the MonoType operated on
5410 * It is only valid to call this function if @type is a MONO_TYPE_PTR.
5413 * Returns: the MonoType pointer that describes the type that @type
5414 * represents a pointer to.
5417 mono_type_get_ptr_type (MonoType *type)
5419 g_assert (type->type == MONO_TYPE_PTR);
5420 return type->data.type;
5424 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
5426 /* FIXME: implement */
5431 mono_signature_get_return_type (MonoMethodSignature *sig)
5437 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
5443 /* start from the first */
5444 if (sig->param_count) {
5445 *iter = &sig->params [0];
5446 return sig->params [0];
5454 if (type < &sig->params [sig->param_count]) {
5462 mono_signature_get_param_count (MonoMethodSignature *sig)
5464 return sig->param_count;
5468 mono_signature_get_call_conv (MonoMethodSignature *sig)
5470 return sig->call_convention;
5474 mono_signature_vararg_start (MonoMethodSignature *sig)
5476 return sig->sentinelpos;
5480 mono_signature_is_instance (MonoMethodSignature *sig)
5482 return sig->hasthis;
5486 mono_signature_explicit_this (MonoMethodSignature *sig)
5488 return sig->explicit_this;
5491 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
5493 mono_aligned_addr_hash (gconstpointer ptr)
5495 return GPOINTER_TO_UINT (ptr) >> 3;
5499 * If @field belongs to an inflated generic class, return the corresponding field of the
5500 * generic type definition class.
5503 mono_metadata_get_corresponding_field_from_generic_type_definition (MonoClassField *field)
5508 if (!field->parent->generic_class)
5511 gtd = field->parent->generic_class->container_class;
5512 offset = field - field->parent->fields;
5513 return gtd->fields + offset;
5517 * If @event belongs to an inflated generic class, return the corresponding event of the
5518 * generic type definition class.
5521 mono_metadata_get_corresponding_event_from_generic_type_definition (MonoEvent *event)
5526 if (!event->parent->generic_class)
5529 gtd = event->parent->generic_class->container_class;
5530 offset = event - event->parent->ext->events;
5531 return gtd->ext->events + offset;
5535 * If @property belongs to an inflated generic class, return the corresponding property of the
5536 * generic type definition class.
5539 mono_metadata_get_corresponding_property_from_generic_type_definition (MonoProperty *property)
5544 if (!property->parent->generic_class)
5547 gtd = property->parent->generic_class->container_class;
5548 offset = property - property->parent->ext->properties;
5549 return gtd->ext->properties + offset;