2 * metadata.c: Routines for accessing the metadata
5 * Miguel de Icaza (miguel@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * (C) 2001-2002 Ximian, Inc.
17 #include "tabledefs.h"
18 #include "mono-endian.h"
20 #include "tokentype.h"
21 #include "metadata-internals.h"
22 #include "class-internals.h"
25 static gboolean do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container,
26 const char *ptr, const char **rptr);
28 static gboolean do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only);
29 static gboolean mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only);
30 static gboolean _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2,
31 gboolean signature_only);
34 * This enumeration is used to describe the data types in the metadata
45 /* Index into Blob heap */
48 /* Index into String heap */
54 /* Pointer into a table */
57 /* HasConstant:Parent pointer (Param, Field or Property) */
60 /* HasCustomAttribute index. Indexes any table except CustomAttribute */
63 /* CustomAttributeType encoded index */
66 /* HasDeclSecurity index: TypeDef Method or Assembly */
69 /* Implementation coded index: File, Export AssemblyRef */
72 /* HasFieldMarshal coded index: Field or Param table */
75 /* MemberForwardedIndex: Field or Method */
78 /* TypeDefOrRef coded index: typedef, typeref, typespec */
81 /* MemberRefParent coded index: typeref, moduleref, method, memberref, typesepc, typedef */
84 /* MethodDefOrRef coded index: Method or Member Ref table */
87 /* HasSemantic coded index: Event or Property */
90 /* ResolutionScope coded index: Module, ModuleRef, AssemblytRef, TypeRef */
94 const static unsigned char TableSchemas [] = {
95 #define ASSEMBLY_SCHEMA_OFFSET 0
96 MONO_MT_UINT32, /* "HashId" }, */
97 MONO_MT_UINT16, /* "Major" }, */
98 MONO_MT_UINT16, /* "Minor" }, */
99 MONO_MT_UINT16, /* "BuildNumber" }, */
100 MONO_MT_UINT16, /* "RevisionNumber" }, */
101 MONO_MT_UINT32, /* "Flags" }, */
102 MONO_MT_BLOB_IDX, /* "PublicKey" }, */
103 MONO_MT_STRING_IDX, /* "Name" }, */
104 MONO_MT_STRING_IDX, /* "Culture" }, */
107 #define ASSEMBLYOS_SCHEMA_OFFSET ASSEMBLY_SCHEMA_OFFSET + 10
108 MONO_MT_UINT32, /* "OSPlatformID" }, */
109 MONO_MT_UINT32, /* "OSMajor" }, */
110 MONO_MT_UINT32, /* "OSMinor" }, */
113 #define ASSEMBLYPROC_SCHEMA_OFFSET ASSEMBLYOS_SCHEMA_OFFSET + 4
114 MONO_MT_UINT32, /* "Processor" }, */
117 #define ASSEMBLYREF_SCHEMA_OFFSET ASSEMBLYPROC_SCHEMA_OFFSET + 2
118 MONO_MT_UINT16, /* "Major" }, */
119 MONO_MT_UINT16, /* "Minor" }, */
120 MONO_MT_UINT16, /* "Build" }, */
121 MONO_MT_UINT16, /* "Revision" }, */
122 MONO_MT_UINT32, /* "Flags" }, */
123 MONO_MT_BLOB_IDX, /* "PublicKeyOrToken" }, */
124 MONO_MT_STRING_IDX, /* "Name" }, */
125 MONO_MT_STRING_IDX, /* "Culture" }, */
126 MONO_MT_BLOB_IDX, /* "HashValue" }, */
129 #define ASSEMBLYREFOS_SCHEMA_OFFSET ASSEMBLYREF_SCHEMA_OFFSET + 10
130 MONO_MT_UINT32, /* "OSPlatformID" }, */
131 MONO_MT_UINT32, /* "OSMajorVersion" }, */
132 MONO_MT_UINT32, /* "OSMinorVersion" }, */
133 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
136 #define ASSEMBLYREFPROC_SCHEMA_OFFSET ASSEMBLYREFOS_SCHEMA_OFFSET + 5
137 MONO_MT_UINT32, /* "Processor" }, */
138 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
141 #define CLASS_LAYOUT_SCHEMA_OFFSET ASSEMBLYREFPROC_SCHEMA_OFFSET + 3
142 MONO_MT_UINT16, /* "PackingSize" }, */
143 MONO_MT_UINT32, /* "ClassSize" }, */
144 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
147 #define CONSTANT_SCHEMA_OFFSET CLASS_LAYOUT_SCHEMA_OFFSET + 4
148 MONO_MT_UINT8, /* "Type" }, */
149 MONO_MT_UINT8, /* "PaddingZero" }, */
150 MONO_MT_CONST_IDX, /* "Parent" }, */
151 MONO_MT_BLOB_IDX, /* "Value" }, */
154 #define CUSTOM_ATTR_SCHEMA_OFFSET CONSTANT_SCHEMA_OFFSET + 5
155 MONO_MT_HASCAT_IDX, /* "Parent" }, */
156 MONO_MT_CAT_IDX, /* "Type" }, */
157 MONO_MT_BLOB_IDX, /* "Value" }, */
160 #define DECL_SEC_SCHEMA_OFFSET CUSTOM_ATTR_SCHEMA_OFFSET + 4
161 MONO_MT_UINT16, /* "Action" }, */
162 MONO_MT_HASDEC_IDX, /* "Parent" }, */
163 MONO_MT_BLOB_IDX, /* "PermissionSet" }, */
166 #define EVENTMAP_SCHEMA_OFFSET DECL_SEC_SCHEMA_OFFSET + 4
167 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
168 MONO_MT_TABLE_IDX, /* "EventList:Event" }, */
171 #define EVENT_SCHEMA_OFFSET EVENTMAP_SCHEMA_OFFSET + 3
172 MONO_MT_UINT16, /* "EventFlags#EventAttribute" }, */
173 MONO_MT_STRING_IDX, /* "Name" }, */
174 MONO_MT_TABLE_IDX, /* "EventType" }, TypeDef or TypeRef */
177 #define EXPORTED_TYPE_SCHEMA_OFFSET EVENT_SCHEMA_OFFSET + 4
178 MONO_MT_UINT32, /* "Flags" }, */
179 MONO_MT_TABLE_IDX, /* "TypeDefId" }, */
180 MONO_MT_STRING_IDX, /* "TypeName" }, */
181 MONO_MT_STRING_IDX, /* "TypeNameSpace" }, */
182 MONO_MT_IMPL_IDX, /* "Implementation" }, */
185 #define FIELD_SCHEMA_OFFSET EXPORTED_TYPE_SCHEMA_OFFSET + 6
186 MONO_MT_UINT16, /* "Flags" }, */
187 MONO_MT_STRING_IDX, /* "Name" }, */
188 MONO_MT_BLOB_IDX, /* "Signature" }, */
191 #define FIELD_LAYOUT_SCHEMA_OFFSET FIELD_SCHEMA_OFFSET + 4
192 MONO_MT_UINT32, /* "Offset" }, */
193 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
196 #define FIELD_MARSHAL_SCHEMA_OFFSET FIELD_LAYOUT_SCHEMA_OFFSET + 3
197 MONO_MT_HFM_IDX, /* "Parent" }, */
198 MONO_MT_BLOB_IDX, /* "NativeType" }, */
201 #define FIELD_RVA_SCHEMA_OFFSET FIELD_MARSHAL_SCHEMA_OFFSET + 3
202 MONO_MT_UINT32, /* "RVA" }, */
203 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
206 #define FILE_SCHEMA_OFFSET FIELD_RVA_SCHEMA_OFFSET + 3
207 MONO_MT_UINT32, /* "Flags" }, */
208 MONO_MT_STRING_IDX, /* "Name" }, */
209 MONO_MT_BLOB_IDX, /* "Value" }, */
212 #define IMPLMAP_SCHEMA_OFFSET FILE_SCHEMA_OFFSET + 4
213 MONO_MT_UINT16, /* "MappingFlag" }, */
214 MONO_MT_MF_IDX, /* "MemberForwarded" }, */
215 MONO_MT_STRING_IDX, /* "ImportName" }, */
216 MONO_MT_TABLE_IDX, /* "ImportScope:ModuleRef" }, */
219 #define IFACEMAP_SCHEMA_OFFSET IMPLMAP_SCHEMA_OFFSET + 5
220 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
221 MONO_MT_TDOR_IDX, /* "Interface=TypeDefOrRef" }, */
224 #define MANIFEST_SCHEMA_OFFSET IFACEMAP_SCHEMA_OFFSET + 3
225 MONO_MT_UINT32, /* "Offset" }, */
226 MONO_MT_UINT32, /* "Flags" }, */
227 MONO_MT_STRING_IDX, /* "Name" }, */
228 MONO_MT_IMPL_IDX, /* "Implementation" }, */
231 #define MEMBERREF_SCHEMA_OFFSET MANIFEST_SCHEMA_OFFSET + 5
232 MONO_MT_MRP_IDX, /* "Class" }, */
233 MONO_MT_STRING_IDX, /* "Name" }, */
234 MONO_MT_BLOB_IDX, /* "Signature" }, */
237 #define METHOD_SCHEMA_OFFSET MEMBERREF_SCHEMA_OFFSET + 4
238 MONO_MT_UINT32, /* "RVA" }, */
239 MONO_MT_UINT16, /* "ImplFlags#MethodImplAttributes" }, */
240 MONO_MT_UINT16, /* "Flags#MethodAttribute" }, */
241 MONO_MT_STRING_IDX, /* "Name" }, */
242 MONO_MT_BLOB_IDX, /* "Signature" }, */
243 MONO_MT_TABLE_IDX, /* "ParamList:Param" }, */
246 #define METHOD_IMPL_SCHEMA_OFFSET METHOD_SCHEMA_OFFSET + 7
247 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
248 MONO_MT_MDOR_IDX, /* "MethodBody" }, */
249 MONO_MT_MDOR_IDX, /* "MethodDeclaration" }, */
252 #define METHOD_SEMA_SCHEMA_OFFSET METHOD_IMPL_SCHEMA_OFFSET + 4
253 MONO_MT_UINT16, /* "MethodSemantic" }, */
254 MONO_MT_TABLE_IDX, /* "Method:Method" }, */
255 MONO_MT_HS_IDX, /* "Association" }, */
258 #define MODULE_SCHEMA_OFFSET METHOD_SEMA_SCHEMA_OFFSET + 4
259 MONO_MT_UINT16, /* "Generation" }, */
260 MONO_MT_STRING_IDX, /* "Name" }, */
261 MONO_MT_GUID_IDX, /* "MVID" }, */
262 MONO_MT_GUID_IDX, /* "EncID" }, */
263 MONO_MT_GUID_IDX, /* "EncBaseID" }, */
266 #define MODULEREF_SCHEMA_OFFSET MODULE_SCHEMA_OFFSET + 6
267 MONO_MT_STRING_IDX, /* "Name" }, */
270 #define NESTED_CLASS_SCHEMA_OFFSET MODULEREF_SCHEMA_OFFSET + 2
271 MONO_MT_TABLE_IDX, /* "NestedClass:TypeDef" }, */
272 MONO_MT_TABLE_IDX, /* "EnclosingClass:TypeDef" }, */
275 #define PARAM_SCHEMA_OFFSET NESTED_CLASS_SCHEMA_OFFSET + 3
276 MONO_MT_UINT16, /* "Flags" }, */
277 MONO_MT_UINT16, /* "Sequence" }, */
278 MONO_MT_STRING_IDX, /* "Name" }, */
281 #define PROPERTY_SCHEMA_OFFSET PARAM_SCHEMA_OFFSET + 4
282 MONO_MT_UINT16, /* "Flags" }, */
283 MONO_MT_STRING_IDX, /* "Name" }, */
284 MONO_MT_BLOB_IDX, /* "Type" }, */
287 #define PROPERTY_MAP_SCHEMA_OFFSET PROPERTY_SCHEMA_OFFSET + 4
288 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
289 MONO_MT_TABLE_IDX, /* "PropertyList:Property" }, */
292 #define STDALON_SIG_SCHEMA_OFFSET PROPERTY_MAP_SCHEMA_OFFSET + 3
293 MONO_MT_BLOB_IDX, /* "Signature" }, */
296 #define TYPEDEF_SCHEMA_OFFSET STDALON_SIG_SCHEMA_OFFSET + 2
297 MONO_MT_UINT32, /* "Flags" }, */
298 MONO_MT_STRING_IDX, /* "Name" }, */
299 MONO_MT_STRING_IDX, /* "Namespace" }, */
300 MONO_MT_TDOR_IDX, /* "Extends" }, */
301 MONO_MT_TABLE_IDX, /* "FieldList:Field" }, */
302 MONO_MT_TABLE_IDX, /* "MethodList:Method" }, */
305 #define TYPEREF_SCHEMA_OFFSET TYPEDEF_SCHEMA_OFFSET + 7
306 MONO_MT_RS_IDX, /* "ResolutionScope=ResolutionScope" }, */
307 MONO_MT_STRING_IDX, /* "Name" }, */
308 MONO_MT_STRING_IDX, /* "Namespace" }, */
311 #define TYPESPEC_SCHEMA_OFFSET TYPEREF_SCHEMA_OFFSET + 4
312 MONO_MT_BLOB_IDX, /* "Signature" }, */
315 #define GENPARAM_SCHEMA_OFFSET TYPESPEC_SCHEMA_OFFSET + 2
316 MONO_MT_UINT16, /* "Number" }, */
317 MONO_MT_UINT16, /* "Flags" }, */
318 MONO_MT_TABLE_IDX, /* "Owner" }, TypeDef or MethodDef */
319 MONO_MT_STRING_IDX, /* "Name" }, */
322 #define METHOD_SPEC_SCHEMA_OFFSET GENPARAM_SCHEMA_OFFSET + 5
323 MONO_MT_MDOR_IDX, /* "Method" }, */
324 MONO_MT_BLOB_IDX, /* "Signature" }, */
327 #define GEN_CONSTRAINT_SCHEMA_OFFSET METHOD_SPEC_SCHEMA_OFFSET + 3
328 MONO_MT_TABLE_IDX, /* "GenericParam" }, */
329 MONO_MT_TDOR_IDX, /* "Constraint" }, */
332 #define NULL_SCHEMA_OFFSET GEN_CONSTRAINT_SCHEMA_OFFSET + 3
336 /* Must be the same order as MONO_TABLE_* */
337 const static unsigned char
338 table_description [] = {
339 MODULE_SCHEMA_OFFSET,
340 TYPEREF_SCHEMA_OFFSET,
341 TYPEDEF_SCHEMA_OFFSET,
345 METHOD_SCHEMA_OFFSET,
348 IFACEMAP_SCHEMA_OFFSET,
349 MEMBERREF_SCHEMA_OFFSET, /* 0xa */
350 CONSTANT_SCHEMA_OFFSET,
351 CUSTOM_ATTR_SCHEMA_OFFSET,
352 FIELD_MARSHAL_SCHEMA_OFFSET,
353 DECL_SEC_SCHEMA_OFFSET,
354 CLASS_LAYOUT_SCHEMA_OFFSET,
355 FIELD_LAYOUT_SCHEMA_OFFSET, /* 0x10 */
356 STDALON_SIG_SCHEMA_OFFSET,
357 EVENTMAP_SCHEMA_OFFSET,
360 PROPERTY_MAP_SCHEMA_OFFSET,
362 PROPERTY_SCHEMA_OFFSET,
363 METHOD_SEMA_SCHEMA_OFFSET,
364 METHOD_IMPL_SCHEMA_OFFSET,
365 MODULEREF_SCHEMA_OFFSET, /* 0x1a */
366 TYPESPEC_SCHEMA_OFFSET,
367 IMPLMAP_SCHEMA_OFFSET,
368 FIELD_RVA_SCHEMA_OFFSET,
371 ASSEMBLY_SCHEMA_OFFSET, /* 0x20 */
372 ASSEMBLYPROC_SCHEMA_OFFSET,
373 ASSEMBLYOS_SCHEMA_OFFSET,
374 ASSEMBLYREF_SCHEMA_OFFSET,
375 ASSEMBLYREFPROC_SCHEMA_OFFSET,
376 ASSEMBLYREFOS_SCHEMA_OFFSET,
378 EXPORTED_TYPE_SCHEMA_OFFSET,
379 MANIFEST_SCHEMA_OFFSET,
380 NESTED_CLASS_SCHEMA_OFFSET,
381 GENPARAM_SCHEMA_OFFSET, /* 0x2a */
382 METHOD_SPEC_SCHEMA_OFFSET,
383 GEN_CONSTRAINT_SCHEMA_OFFSET
386 #ifdef HAVE_ARRAY_ELEM_INIT
387 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
388 #define MSGSTRFIELD1(line) str##line
389 static const struct msgstr_t {
390 #define TABLEDEF(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
391 #include "mono/cil/tables.def"
394 #define TABLEDEF(a,b) b,
395 #include "mono/cil/tables.def"
398 static const gint16 tableidx [] = {
399 #define TABLEDEF(a,b) [a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
400 #include "mono/cil/tables.def"
405 #define TABLEDEF(a,b) b,
406 static const char* const
407 mono_tables_names [] = {
408 #include "mono/cil/tables.def"
415 * mono_meta_table_name:
416 * @table: table index
418 * Returns: the name for the @table index
421 mono_meta_table_name (int table)
423 if ((table < 0) || (table > MONO_TABLE_LAST))
426 #ifdef HAVE_ARRAY_ELEM_INIT
427 return (const char*)&tablestr + tableidx [table];
429 return mono_tables_names [table];
433 /* The guy who wrote the spec for this should not be allowed near a
436 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
437 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
438 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
439 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
440 inverse of this mapping.
443 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
444 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
446 /* Reference: Partition II - 23.2.6 */
448 * mono_metadata_compute_size:
449 * @meta: metadata context
450 * @tableindex: metadata table number
451 * @result_bitfield: pointer to guint32 where to store additional info
453 * mono_metadata_compute_size() computes the lenght in bytes of a single
454 * row in a metadata table. The size of each column is encoded in the
455 * @result_bitfield return value along with the number of columns in the table.
456 * the resulting bitfield should be handed to the mono_metadata_table_size()
457 * and mono_metadata_table_count() macros.
458 * This is a Mono runtime internal only function.
461 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
463 guint32 bitfield = 0;
464 int size = 0, field_size = 0;
467 const unsigned char *description = TableSchemas + table_description [tableindex];
469 for (i = 0; (code = description [i]) != MONO_MT_END; i++){
472 field_size = 4; break;
475 field_size = 2; break;
478 field_size = 1; break;
480 case MONO_MT_BLOB_IDX:
481 field_size = meta->idx_blob_wide ? 4 : 2; break;
483 case MONO_MT_STRING_IDX:
484 field_size = meta->idx_string_wide ? 4 : 2; break;
486 case MONO_MT_GUID_IDX:
487 field_size = meta->idx_guid_wide ? 4 : 2; break;
489 case MONO_MT_TABLE_IDX:
490 /* Uhm, a table index can point to other tables besides the current one
491 * so, it's not correct to use the rowcount of the current table to
492 * get the size for this column - lupus
494 switch (tableindex) {
495 case MONO_TABLE_ASSEMBLYREFOS:
497 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
498 case MONO_TABLE_ASSEMBLYPROCESSOR:
500 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
501 case MONO_TABLE_CLASSLAYOUT:
503 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
504 case MONO_TABLE_EVENTMAP:
505 g_assert (i == 0 || i == 1);
506 field_size = i ? idx_size (MONO_TABLE_EVENT):
507 idx_size(MONO_TABLE_TYPEDEF);
509 case MONO_TABLE_EVENT:
511 field_size = MAX (idx_size (MONO_TABLE_TYPEDEF), idx_size(MONO_TABLE_TYPEREF));
512 field_size = MAX (field_size, idx_size(MONO_TABLE_TYPESPEC));
514 case MONO_TABLE_EXPORTEDTYPE:
516 /* the index is in another metadata file, so it must be 4 */
517 field_size = 4; break;
518 case MONO_TABLE_FIELDLAYOUT:
520 field_size = idx_size (MONO_TABLE_FIELD); break;
521 case MONO_TABLE_FIELDRVA:
523 field_size = idx_size (MONO_TABLE_FIELD); break;
524 case MONO_TABLE_IMPLMAP:
526 field_size = idx_size (MONO_TABLE_MODULEREF); break;
527 case MONO_TABLE_INTERFACEIMPL:
529 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
530 case MONO_TABLE_METHOD:
532 field_size = idx_size (MONO_TABLE_PARAM); break;
533 case MONO_TABLE_METHODIMPL:
535 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
536 case MONO_TABLE_METHODSEMANTICS:
538 field_size = idx_size (MONO_TABLE_METHOD); break;
539 case MONO_TABLE_NESTEDCLASS:
540 g_assert (i == 0 || i == 1);
541 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
542 case MONO_TABLE_PROPERTYMAP:
543 g_assert (i == 0 || i == 1);
544 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
545 idx_size(MONO_TABLE_TYPEDEF);
547 case MONO_TABLE_TYPEDEF:
548 g_assert (i == 4 || i == 5);
549 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
550 idx_size(MONO_TABLE_METHOD);
552 case MONO_TABLE_GENERICPARAM:
553 g_assert (i == 2 || i == 4 || i == 5);
555 field_size = MAX (idx_size (MONO_TABLE_METHOD), idx_size (MONO_TABLE_TYPEDEF));
557 field_size = idx_size (MONO_TABLE_TYPEDEF);
559 field_size = idx_size (MONO_TABLE_TYPEDEF);
562 case MONO_TABLE_GENERICPARAMCONSTRAINT:
564 field_size = idx_size (MONO_TABLE_GENERICPARAM);
568 g_assert_not_reached ();
573 * HasConstant: ParamDef, FieldDef, Property
575 case MONO_MT_CONST_IDX:
576 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
577 meta->tables [MONO_TABLE_FIELD].rows);
578 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
580 /* 2 bits to encode tag */
581 field_size = rtsize (n, 16-2);
585 * HasCustomAttribute: points to any table but
588 case MONO_MT_HASCAT_IDX:
590 * We believe that since the signature and
591 * permission are indexing the Blob heap,
592 * we should consider the blob size first
594 /* I'm not a believer - lupus
595 if (meta->idx_blob_wide){
600 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
601 meta->tables [MONO_TABLE_FIELD].rows);
602 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
603 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
604 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
605 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
606 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
607 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
608 n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
609 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
610 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
611 n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
612 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
613 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
614 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
615 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
616 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
617 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
618 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
620 /* 5 bits to encode */
621 field_size = rtsize (n, 16-5);
625 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
626 * MemberRef and String.
628 case MONO_MT_CAT_IDX:
629 /* String is a heap, if it is wide, we know the size */
631 if (meta->idx_string_wide){
636 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
637 meta->tables [MONO_TABLE_TYPEDEF].rows);
638 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
639 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
641 /* 3 bits to encode */
642 field_size = rtsize (n, 16-3);
646 * HasDeclSecurity: Typedef, MethodDef, Assembly
648 case MONO_MT_HASDEC_IDX:
649 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
650 meta->tables [MONO_TABLE_METHOD].rows);
651 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
653 /* 2 bits to encode */
654 field_size = rtsize (n, 16-2);
658 * Implementation: File, AssemblyRef, ExportedType
660 case MONO_MT_IMPL_IDX:
661 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
662 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
663 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
665 /* 2 bits to encode tag */
666 field_size = rtsize (n, 16-2);
670 * HasFieldMarshall: FieldDef, ParamDef
672 case MONO_MT_HFM_IDX:
673 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
674 meta->tables [MONO_TABLE_PARAM].rows);
676 /* 1 bit used to encode tag */
677 field_size = rtsize (n, 16-1);
681 * MemberForwarded: FieldDef, MethodDef
684 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
685 meta->tables [MONO_TABLE_METHOD].rows);
687 /* 1 bit used to encode tag */
688 field_size = rtsize (n, 16-1);
692 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
694 * It is TypeDef, _TypeRef_, TypeSpec, instead.
696 case MONO_MT_TDOR_IDX:
697 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
698 meta->tables [MONO_TABLE_TYPEREF].rows);
699 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
701 /* 2 bits to encode */
702 field_size = rtsize (n, 16-2);
706 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
708 case MONO_MT_MRP_IDX:
709 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
710 meta->tables [MONO_TABLE_TYPEREF].rows);
711 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
712 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
713 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
714 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
716 /* 3 bits to encode */
717 field_size = rtsize (n, 16 - 3);
721 * MethodDefOrRef: MethodDef, MemberRef
723 case MONO_MT_MDOR_IDX:
724 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
725 meta->tables [MONO_TABLE_MEMBERREF].rows);
727 /* 1 bit used to encode tag */
728 field_size = rtsize (n, 16-1);
732 * HasSemantics: Property, Event
735 n = MAX (meta->tables [MONO_TABLE_PROPERTY].rows,
736 meta->tables [MONO_TABLE_EVENT].rows);
738 /* 1 bit used to encode tag */
739 field_size = rtsize (n, 16-1);
743 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
746 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
747 meta->tables [MONO_TABLE_MODULEREF].rows);
748 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
749 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
751 /* 2 bits used to encode tag (ECMA spec claims 3) */
752 field_size = rtsize (n, 16 - 2);
757 * encode field size as follows (we just need to
764 bitfield |= (field_size-1) << shift;
767 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
770 *result_bitfield = (i << 24) | bitfield;
775 * mono_metadata_compute_table_bases:
776 * @meta: metadata context to compute table values
778 * Computes the table bases for the metadata structure.
779 * This is an internal function used by the image loader code.
782 mono_metadata_compute_table_bases (MonoImage *meta)
785 const char *base = meta->tables_base;
787 for (i = 0; i < MONO_TABLE_NUM; i++) {
788 MonoTableInfo *table = &meta->tables [i];
789 if (table->rows == 0)
792 table->row_size = mono_metadata_compute_size (meta, i, &table->size_bitfield);
794 base += table->rows * table->row_size;
799 * mono_metadata_locate:
800 * @meta: metadata context
801 * @table: table code.
802 * @idx: index of element to retrieve from @table.
804 * Returns: a pointer to the @idx element in the metadata table
805 * whose code is @table.
808 mono_metadata_locate (MonoImage *meta, int table, int idx)
810 /* idx == 0 refers always to NULL */
811 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
813 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
817 * mono_metadata_locate_token:
818 * @meta: metadata context
819 * @token: metadata token
821 * Returns: a pointer to the data in the metadata represented by the
825 mono_metadata_locate_token (MonoImage *meta, guint32 token)
827 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
831 * mono_metadata_string_heap:
832 * @meta: metadata context
833 * @index: index into the string heap.
835 * Returns: an in-memory pointer to the @index in the string heap.
838 mono_metadata_string_heap (MonoImage *meta, guint32 index)
840 g_return_val_if_fail (index < meta->heap_strings.size, "");
841 return meta->heap_strings.data + index;
845 * mono_metadata_user_string:
846 * @meta: metadata context
847 * @index: index into the user string heap.
849 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
852 mono_metadata_user_string (MonoImage *meta, guint32 index)
854 g_return_val_if_fail (index < meta->heap_us.size, "");
855 return meta->heap_us.data + index;
859 * mono_metadata_blob_heap:
860 * @meta: metadata context
861 * @index: index into the blob.
863 * Returns: an in-memory pointer to the @index in the Blob heap.
866 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
868 g_return_val_if_fail (index < meta->heap_blob.size, "");
869 return meta->heap_blob.data + index;
873 * mono_metadata_guid_heap:
874 * @meta: metadata context
875 * @index: index into the guid heap.
877 * Returns: an in-memory pointer to the @index in the guid heap.
880 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
883 index *= 16; /* adjust for guid size and 1-based index */
884 g_return_val_if_fail (index < meta->heap_guid.size, "");
885 return meta->heap_guid.data + index;
889 dword_align (const char *ptr)
891 #if SIZEOF_VOID_P == 8
892 return (const char *) (((guint64) (ptr + 3)) & ~3);
894 return (const char *) (((guint32) (ptr + 3)) & ~3);
899 * mono_metadata_decode_row:
900 * @t: table to extract information from.
901 * @idx: index in table.
902 * @res: array of @res_size cols to store the results in
904 * This decompresses the metadata element @idx in table @t
905 * into the guint32 @res array that has res_size elements
908 mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res_size)
910 guint32 bitfield = t->size_bitfield;
911 int i, count = mono_metadata_table_count (bitfield);
912 const char *data = t->base + idx * t->row_size;
914 g_assert (res_size == count);
916 for (i = 0; i < count; i++) {
917 int n = mono_metadata_table_size (bitfield, i);
921 res [i] = *data; break;
923 res [i] = read16 (data); break;
925 res [i] = read32 (data); break;
927 g_assert_not_reached ();
934 * mono_metadata_decode_row_col:
935 * @t: table to extract information from.
936 * @idx: index for row in table.
937 * @col: column in the row.
939 * This function returns the value of column @col from the @idx
940 * row in the table @t.
943 mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col)
945 guint32 bitfield = t->size_bitfield;
947 register const char *data = t->base + idx * t->row_size;
950 g_assert (col < mono_metadata_table_count (bitfield));
952 n = mono_metadata_table_size (bitfield, 0);
953 for (i = 0; i < col; ++i) {
955 n = mono_metadata_table_size (bitfield, i + 1);
961 return read16 (data);
963 return read32 (data);
965 g_assert_not_reached ();
970 * mono_metadata_decode_blob_size:
971 * @ptr: pointer to a blob object
972 * @rptr: the new position of the pointer
974 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
976 * Returns: the size of the blob object
979 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
981 const unsigned char *ptr = (const unsigned char *)xptr;
984 if ((*ptr & 0x80) == 0){
985 size = ptr [0] & 0x7f;
987 } else if ((*ptr & 0x40) == 0){
988 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
991 size = ((ptr [0] & 0x1f) << 24) +
1003 * mono_metadata_decode_value:
1004 * @ptr: pointer to decode from
1005 * @rptr: the new position of the pointer
1007 * This routine decompresses 32-bit values as specified in the "Blob and
1008 * Signature" section (22.2)
1010 * Returns: the decoded value
1013 mono_metadata_decode_value (const char *_ptr, const char **rptr)
1015 const unsigned char *ptr = (const unsigned char *) _ptr;
1016 unsigned char b = *ptr;
1019 if ((b & 0x80) == 0){
1022 } else if ((b & 0x40) == 0){
1023 len = ((b & 0x3f) << 8 | ptr [1]);
1026 len = ((b & 0x1f) << 24) |
1039 * mono_metadata_decode_signed_value:
1040 * @ptr: pointer to decode from
1041 * @rptr: the new position of the pointer
1043 * This routine decompresses 32-bit signed values
1044 * (not specified in the spec)
1046 * Returns: the decoded value
1049 mono_metadata_decode_signed_value (const char *ptr, const char **rptr)
1051 guint32 uval = mono_metadata_decode_value (ptr, rptr);
1052 gint32 ival = uval >> 1;
1055 /* ival is a truncated 2's complement negative number. */
1057 /* 6 bits = 7 bits for compressed representation (top bit is '0') - 1 sign bit */
1060 /* 13 bits = 14 bits for compressed representation (top bits are '10') - 1 sign bit */
1061 return ival - 0x2000;
1062 if (ival < 0x10000000)
1063 /* 28 bits = 29 bits for compressed representation (top bits are '110') - 1 sign bit */
1064 return ival - 0x10000000;
1065 g_assert (ival < 0x20000000);
1066 g_warning ("compressed signed value appears to use 29 bits for compressed representation: %x (raw: %8x)", ival, uval);
1067 return ival - 0x20000000;
1071 * mono_metadata_parse_typedef_or_ref:
1072 * @m: a metadata context.
1073 * @ptr: a pointer to an encoded TypedefOrRef in @m
1074 * @rptr: pointer updated to match the end of the decoded stream
1076 * Returns: a token valid in the @m metadata decoded from
1077 * the compressed representation.
1080 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1083 token = mono_metadata_decode_value (ptr, &ptr);
1086 return mono_metadata_token_from_dor (token);
1090 * mono_metadata_parse_custom_mod:
1091 * @m: a metadata context.
1092 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1093 * @ptr: a pointer to (possibly) the start of a custom modifier list
1094 * @rptr: pointer updated to match the end of the decoded stream
1096 * Checks if @ptr points to a type custom modifier compressed representation.
1098 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1101 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1103 MonoCustomMod local;
1104 if ((*ptr == MONO_TYPE_CMOD_OPT) || (*ptr == MONO_TYPE_CMOD_REQD)) {
1107 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1108 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1115 * mono_metadata_parse_array:
1116 * @m: a metadata context.
1117 * @ptr: a pointer to an encoded array description.
1118 * @rptr: pointer updated to match the end of the decoded stream
1120 * Decodes the compressed array description found in the metadata @m at @ptr.
1122 * Returns: a #MonoArrayType structure describing the array type
1126 mono_metadata_parse_array_full (MonoImage *m, MonoGenericContainer *container,
1127 const char *ptr, const char **rptr)
1130 MonoArrayType *array = g_new0 (MonoArrayType, 1);
1133 etype = mono_metadata_parse_type_full (m, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
1136 array->eklass = mono_class_from_mono_type (etype);
1137 array->rank = mono_metadata_decode_value (ptr, &ptr);
1139 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1140 if (array->numsizes)
1141 array->sizes = g_new0 (int, array->numsizes);
1142 for (i = 0; i < array->numsizes; ++i)
1143 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1145 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1146 if (array->numlobounds)
1147 array->lobounds = g_new0 (int, array->numlobounds);
1148 for (i = 0; i < array->numlobounds; ++i)
1149 array->lobounds [i] = mono_metadata_decode_signed_value (ptr, &ptr);
1157 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1159 return mono_metadata_parse_array_full (m, NULL, ptr, rptr);
1163 * mono_metadata_free_array:
1164 * @array: array description
1166 * Frees the array description returned from mono_metadata_parse_array().
1169 mono_metadata_free_array (MonoArrayType *array)
1171 g_free (array->sizes);
1172 g_free (array->lobounds);
1177 * need to add common field and param attributes combinations:
1180 * public static literal
1183 * private static literal
1185 static const MonoType
1187 /* data, attrs, type, nmods, byref, pinned */
1188 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1189 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1190 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1191 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1192 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1193 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1194 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1195 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1196 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1197 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1198 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1199 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1200 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1201 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1202 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1203 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1204 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1205 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1206 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1207 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1208 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1209 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1210 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1211 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1212 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1213 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1214 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1215 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 0, 0},
1216 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 1, 0},
1217 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1218 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1219 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1220 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1221 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1224 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1226 static GHashTable *type_cache = NULL;
1227 static GHashTable *generic_inst_cache = NULL;
1228 static GHashTable *generic_class_cache = NULL;
1229 static int next_generic_inst_id = 0;
1231 static guint mono_generic_class_hash (gconstpointer data);
1234 * MonoTypes with modifies are never cached, so we never check or use that field.
1237 mono_type_hash (gconstpointer data)
1239 const MonoType *type = (const MonoType *) data;
1240 if (type->type == MONO_TYPE_GENERICINST)
1241 return mono_generic_class_hash (type->data.generic_class);
1243 return type->type | (type->byref << 8) | (type->attrs << 9);
1247 mono_type_equal (gconstpointer ka, gconstpointer kb)
1249 const MonoType *a = (const MonoType *) ka;
1250 const MonoType *b = (const MonoType *) kb;
1252 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1254 /* need other checks */
1259 mono_generic_inst_hash (gconstpointer data)
1261 const MonoGenericInst *ginst = (const MonoGenericInst *) data;
1265 for (i = 0; i < ginst->type_argc; ++i) {
1267 hash += mono_metadata_type_hash (ginst->type_argv [i]);
1270 return hash ^ (ginst->is_open << 8);
1274 mono_generic_inst_equal (gconstpointer ka, gconstpointer kb)
1276 const MonoGenericInst *a = (const MonoGenericInst *) ka;
1277 const MonoGenericInst *b = (const MonoGenericInst *) kb;
1280 if ((a->is_open != b->is_open) || (a->type_argc != b->type_argc) || (a->is_reference != b->is_reference))
1282 for (i = 0; i < a->type_argc; ++i) {
1283 if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], FALSE))
1290 mono_generic_class_hash (gconstpointer data)
1292 const MonoGenericClass *gclass = (const MonoGenericClass *) data;
1293 guint hash = mono_metadata_type_hash (&gclass->container_class->byval_arg);
1296 hash += mono_generic_inst_hash (gclass->inst);
1302 mono_generic_class_equal (gconstpointer ka, gconstpointer kb)
1304 const MonoGenericClass *a = (const MonoGenericClass *) ka;
1305 const MonoGenericClass *b = (const MonoGenericClass *) kb;
1307 return _mono_metadata_generic_class_equal (a, b, FALSE);
1311 * mono_metadata_init:
1313 * Initialize the global variables of this module.
1314 * This is a Mono runtime internal function.
1317 mono_metadata_init (void)
1321 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1322 generic_inst_cache = g_hash_table_new (mono_generic_inst_hash, mono_generic_inst_equal);
1323 generic_class_cache = g_hash_table_new (mono_generic_class_hash, mono_generic_class_equal);
1325 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1326 g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
1330 * mono_metadata_parse_type:
1331 * @m: metadata context
1332 * @mode: king of type that may be found at @ptr
1333 * @opt_attrs: optional attributes to store in the returned type
1334 * @ptr: pointer to the type representation
1335 * @rptr: pointer updated to match the end of the decoded stream
1337 * Decode a compressed type description found at @ptr in @m.
1338 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1339 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1340 * This function can be used to decode type descriptions in method signatures,
1341 * field signatures, locals signatures etc.
1343 * To parse a generic type, `generic_container' points to the current class'es
1344 * (the `generic_container' field in the MonoClass) or the current generic method's
1345 * (the `generic_container' field in the MonoMethodNormal) generic container.
1346 * When we encounter any MONO_TYPE_VAR or MONO_TYPE_MVAR's, they're looked up in
1347 * this MonoGenericContainer.
1348 * This is a Mono runtime internal function.
1350 * Returns: a #MonoType structure representing the decoded type.
1353 mono_metadata_parse_type_full (MonoImage *m, MonoGenericContainer *container, MonoParseTypeMode mode,
1354 short opt_attrs, const char *ptr, const char **rptr)
1356 MonoType *type, *cached;
1358 gboolean byref = FALSE;
1359 gboolean pinned = FALSE;
1360 const char *tmp_ptr;
1365 * According to the spec, custom modifiers should come before the byref
1366 * flag, but the IL produced by ilasm from the following signature:
1367 * object modopt(...) &
1368 * starts with a byref flag, followed by the modifiers. (bug #49802)
1369 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1370 * it would be better to treat byref as real type constructor instead of
1372 * Also, pinned should come before anything else, but some MSV++ produced
1373 * assemblies violate this (#bug 61990).
1376 /* Count the modifiers first */
1381 case MONO_TYPE_PINNED:
1382 case MONO_TYPE_BYREF:
1385 case MONO_TYPE_CMOD_REQD:
1386 case MONO_TYPE_CMOD_OPT:
1388 mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1396 type = g_malloc0 (sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1397 type->num_mods = count;
1399 g_warning ("got more than 64 modifiers in type");
1402 memset (type, 0, sizeof (MonoType));
1405 /* Parse pinned, byref and custom modifiers */
1410 case MONO_TYPE_PINNED:
1414 case MONO_TYPE_BYREF:
1418 case MONO_TYPE_CMOD_REQD:
1419 case MONO_TYPE_CMOD_OPT:
1420 mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1428 type->attrs = opt_attrs;
1429 type->byref = byref;
1430 type->pinned = pinned ? 1 : 0;
1432 if (!do_mono_metadata_parse_type (type, m, container, ptr, &ptr)) {
1442 /* FIXME: remove the != MONO_PARSE_PARAM condition, this accounts for
1443 * almost 10k (about 2/3rds) of all MonoType's we create.
1445 if (mode != MONO_PARSE_PARAM && !type->num_mods) {
1446 /* no need to free type here, because it is on the stack */
1447 if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
1449 return &type->data.klass->this_arg;
1451 return &type->data.klass->byval_arg;
1453 /* No need to use locking since nobody is modifying the hash table */
1454 if ((cached = g_hash_table_lookup (type_cache, type)))
1458 /*printf ("%x%c %s\n", type->attrs, type->pinned ? 'p' : ' ', mono_type_full_name (type));*/
1461 type = g_memdup (&stype, sizeof (MonoType));
1466 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
1467 const char *ptr, const char **rptr)
1469 return mono_metadata_parse_type_full (m, NULL, mode, opt_attrs, ptr, rptr);
1473 * mono_metadata_parse_signature_full:
1474 * @image: metadata context
1475 * @generic_container: generic container
1476 * @toke: metadata token
1478 * Decode a method signature stored in the STANDALONESIG table
1480 * Returns: a MonoMethodSignature describing the signature.
1482 MonoMethodSignature*
1483 mono_metadata_parse_signature_full (MonoImage *image, MonoGenericContainer *generic_container, guint32 token)
1485 MonoTableInfo *tables = image->tables;
1486 guint32 idx = mono_metadata_token_index (token);
1491 return mono_lookup_dynamic_token (image, token);
1493 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1495 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1497 ptr = mono_metadata_blob_heap (image, sig);
1498 mono_metadata_decode_blob_size (ptr, &ptr);
1500 return mono_metadata_parse_method_signature_full (image, generic_container, FALSE, ptr, NULL);
1504 * mono_metadata_parse_signature:
1505 * @image: metadata context
1506 * @toke: metadata token
1508 * Decode a method signature stored in the STANDALONESIG table
1510 * Returns: a MonoMethodSignature describing the signature.
1512 MonoMethodSignature*
1513 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1515 return mono_metadata_parse_signature_full (image, NULL, token);
1519 * mono_metadata_signature_alloc:
1520 * @image: metadata context
1521 * @nparmas: number of parameters in the signature
1523 * Allocate a MonoMethodSignature structure with the specified number of params.
1524 * The return type and the params types need to be filled later.
1525 * This is a Mono runtime internal function.
1527 * Returns: the new MonoMethodSignature structure.
1529 MonoMethodSignature*
1530 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1532 MonoMethodSignature *sig;
1534 /* later we want to allocate signatures with mempools */
1535 sig = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1536 sig->param_count = nparams;
1537 sig->sentinelpos = -1;
1543 * mono_metadata_signature_dup:
1544 * @sig: method signature
1546 * Duplicate an existing MonoMethodSignature so it can be modified.
1547 * This is a Mono runtime internal function.
1549 * Returns: the new MonoMethodSignature structure.
1551 MonoMethodSignature*
1552 mono_metadata_signature_dup (MonoMethodSignature *sig)
1556 sigsize = sizeof (MonoMethodSignature) + (sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *);
1557 return g_memdup (sig, sigsize);
1561 * mono_metadata_parse_method_signature:
1562 * @m: metadata context
1563 * @generic_container: generics container
1564 * @def: the MethodDef index or 0 for Ref signatures.
1565 * @ptr: pointer to the signature metadata representation
1566 * @rptr: pointer updated to match the end of the decoded stream
1568 * Decode a method signature stored at @ptr.
1569 * This is a Mono runtime internal function.
1571 * Returns: a MonoMethodSignature describing the signature.
1573 MonoMethodSignature *
1574 mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContainer *container,
1575 int def, const char *ptr, const char **rptr)
1577 MonoMethodSignature *method;
1578 int i, *pattrs = NULL;
1579 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1580 guint32 gen_param_count = 0;
1581 gboolean is_open = FALSE;
1584 gen_param_count = 1;
1589 call_convention = *ptr & 0x0F;
1591 if (gen_param_count)
1592 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1593 param_count = mono_metadata_decode_value (ptr, &ptr);
1596 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1597 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1598 guint32 cols [MONO_PARAM_SIZE];
1599 guint lastp, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1601 if (def < methodt->rows)
1602 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1604 lastp = paramt->rows + 1;
1606 pattrs = g_new0 (int, 1 + param_count);
1607 for (i = param_index; i < lastp; ++i) {
1608 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1609 pattrs [cols [MONO_PARAM_SEQUENCE]] = cols [MONO_PARAM_FLAGS];
1612 method = mono_metadata_signature_alloc (m, param_count);
1613 method->hasthis = hasthis;
1614 method->explicit_this = explicit_this;
1615 method->call_convention = call_convention;
1616 method->generic_param_count = gen_param_count;
1618 if (call_convention != 0xa) {
1619 method->ret = mono_metadata_parse_type_full (m, container, MONO_PARSE_RET, pattrs ? pattrs [0] : 0, ptr, &ptr);
1621 mono_metadata_free_method_signature (method);
1625 is_open = mono_class_is_open_constructed_type (method->ret);
1628 for (i = 0; i < method->param_count; ++i) {
1629 if (*ptr == MONO_TYPE_SENTINEL) {
1630 if (method->call_convention != MONO_CALL_VARARG || def)
1631 g_error ("found sentinel for methoddef or no vararg method");
1632 method->sentinelpos = i;
1635 method->params [i] = mono_metadata_parse_type_full (m, container, MONO_PARSE_PARAM, pattrs ? pattrs [i+1] : 0, ptr, &ptr);
1636 if (!method->params [i]) {
1637 mono_metadata_free_method_signature (method);
1642 is_open = mono_class_is_open_constructed_type (method->params [i]);
1645 method->has_type_parameters = is_open;
1647 if (def && (method->call_convention == MONO_CALL_VARARG))
1648 method->sentinelpos = method->param_count;
1655 * Add signature to a cache and increase ref count...
1662 * mono_metadata_parse_method_signature:
1663 * @m: metadata context
1664 * @def: the MethodDef index or 0 for Ref signatures.
1665 * @ptr: pointer to the signature metadata representation
1666 * @rptr: pointer updated to match the end of the decoded stream
1668 * Decode a method signature stored at @ptr.
1669 * This is a Mono runtime internal function.
1671 * Returns: a MonoMethodSignature describing the signature.
1673 MonoMethodSignature *
1674 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1676 return mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr);
1680 * mono_metadata_free_method_signature:
1681 * @sig: signature to destroy
1683 * Free the memory allocated in the signature @sig.
1684 * This method needs to be robust and work also on partially-built
1685 * signatures, so it does extra checks.
1688 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1692 mono_metadata_free_type (sig->ret);
1693 for (i = 0; i < sig->param_count; ++i) {
1694 if (sig->params [i])
1695 mono_metadata_free_type (sig->params [i]);
1702 * mono_metadata_lookup_generic_inst:
1704 * Check whether the newly created generic instantiation @ginst already exists
1705 * in the cache and return the cached value in this case. Otherwise insert
1706 * it into the cache.
1708 * Use this method each time you create a new `MonoGenericInst' to ensure
1709 * proper caching. Only use the returned value as the argument passed to this
1710 * method may be freed.
1714 mono_metadata_lookup_generic_inst (MonoGenericInst *ginst)
1716 MonoGenericInst *cached;
1719 cached = g_hash_table_lookup (generic_inst_cache, ginst);
1721 for (i = 0; i < ginst->type_argc; i++)
1722 mono_metadata_free_type (ginst->type_argv [i]);
1723 g_free (ginst->type_argv);
1728 ginst->id = ++next_generic_inst_id;
1729 g_hash_table_insert (generic_inst_cache, ginst, ginst);
1735 * mono_metadata_lookup_generic_class:
1737 * Check whether the newly created generic class @gclass already exists
1738 * in the cache and return the cached value in this case. Otherwise insert
1739 * it into the cache and return NULL.
1741 * Returns: the previosly cached generic class or NULL if it has been newly
1742 * inserted into the cache.
1746 mono_metadata_lookup_generic_class (MonoGenericClass *gclass)
1748 MonoGenericClass *cached;
1750 cached = g_hash_table_lookup (generic_class_cache, gclass);
1754 g_hash_table_insert (generic_class_cache, gclass, gclass);
1759 * mono_metadata_inflate_generic_inst:
1761 * Instantiate the generic instance @ginst with the context @context.
1765 mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context)
1767 MonoGenericInst *nginst;
1770 if (!ginst->is_open)
1773 nginst = g_new0 (MonoGenericInst, 1);
1774 nginst->type_argc = ginst->type_argc;
1775 nginst->type_argv = g_new0 (MonoType*, nginst->type_argc);
1776 nginst->is_reference = 1;
1778 for (i = 0; i < nginst->type_argc; i++) {
1779 MonoType *t = mono_class_inflate_generic_type (ginst->type_argv [i], context);
1781 if (!nginst->is_open)
1782 nginst->is_open = mono_class_is_open_constructed_type (t);
1783 if (nginst->is_reference)
1784 nginst->is_reference = MONO_TYPE_IS_REFERENCE (t);
1786 nginst->type_argv [i] = t;
1789 return mono_metadata_lookup_generic_inst (nginst);
1793 mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContainer *container,
1794 int count, const char *ptr, const char **rptr)
1796 MonoGenericInst *ginst;
1799 ginst = g_new0 (MonoGenericInst, 1);
1800 ginst->type_argc = count;
1801 ginst->type_argv = g_new0 (MonoType*, count);
1802 ginst->is_reference = 1;
1804 for (i = 0; i < ginst->type_argc; i++) {
1805 MonoType *t = mono_metadata_parse_type_full (m, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
1808 g_free (ginst->type_argv);
1812 ginst->type_argv [i] = t;
1813 if (!ginst->is_open)
1814 ginst->is_open = mono_class_is_open_constructed_type (t);
1815 if (ginst->is_reference)
1816 ginst->is_reference = MONO_TYPE_IS_REFERENCE (t);
1822 return mono_metadata_lookup_generic_inst (ginst);
1826 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContainer *container,
1827 const char *ptr, const char **rptr)
1829 MonoInflatedGenericClass *igclass;
1830 MonoGenericClass *gclass, *cached;
1835 igclass = g_new0 (MonoInflatedGenericClass, 1);
1836 gclass = &igclass->generic_class;
1837 gclass->is_inflated = TRUE;
1839 type->data.generic_class = gclass;
1841 gclass->context = g_new0 (MonoGenericContext, 1);
1842 gclass->context->gclass = gclass;
1845 * Create the klass before parsing the type arguments.
1846 * This is required to support "recursive" definitions.
1847 * See mcs/tests/gen-23.cs for an example.
1849 igclass->klass = g_new0 (MonoClass, 1);
1851 gtype = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1852 gclass->container_class = gklass = mono_class_from_mono_type (gtype);
1854 g_assert (gklass->generic_container);
1855 gclass->context->container = gklass->generic_container;
1857 count = mono_metadata_decode_value (ptr, &ptr);
1859 gclass->inst = mono_metadata_parse_generic_inst (m, container, count, ptr, &ptr);
1865 * We may be called multiple times on different metadata to create the same
1866 * instantiated type. This happens for instance if we're part of a method or
1867 * local variable signature.
1869 * It's important to return the same MonoGenericClass * for each particualar
1870 * instantiation of a generic type (ie "Stack<Int32>") to make static fields
1873 * According to the spec ($26.1.5), a static variable in a generic class
1874 * declaration is shared amongst all instances of the same closed constructed
1878 cached = g_hash_table_lookup (generic_class_cache, gclass);
1880 g_free (igclass->klass);
1883 type->data.generic_class = cached;
1886 g_hash_table_insert (generic_class_cache, gclass, gclass);
1888 mono_stats.generic_instance_count++;
1889 mono_stats.generics_metadata_size += sizeof (MonoGenericClass) +
1890 sizeof (MonoGenericContext) +
1891 gclass->inst->type_argc * sizeof (MonoType);
1897 * @gc: The generic container to normalize
1898 * @type: The kind of generic parameters the resulting generic-container should contain
1901 static MonoGenericContainer *
1902 select_container (MonoGenericContainer *gc, MonoTypeEnum type)
1904 gboolean is_var = (type == MONO_TYPE_VAR);
1908 g_assert (is_var || type == MONO_TYPE_MVAR);
1910 if (is_var && gc->parent)
1912 * The current MonoGenericContainer is a generic method -> its `parent'
1913 * points to the containing class'es container.
1918 * Ensure that we have the correct type of GenericContainer.
1920 g_assert (is_var == !gc->is_method);
1926 * mono_metadata_parse_generic_param:
1927 * @generic_container: Our MonoClass's or MonoMethodNormal's MonoGenericContainer;
1928 * see mono_metadata_parse_type_full() for details.
1929 * Internal routine to parse a generic type parameter.
1931 static MonoGenericParam *
1932 mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContainer *generic_container,
1933 MonoTypeEnum type, const char *ptr, const char **rptr)
1935 int index = mono_metadata_decode_value (ptr, &ptr);
1939 generic_container = select_container (generic_container, type);
1940 if (!generic_container) {
1941 /* Create dummy MonoGenericParam */
1942 MonoGenericParam *param = g_new0 (MonoGenericParam, 1);
1943 param->name = g_strdup_printf ("%d", index);
1949 g_assert (index < generic_container->type_argc);
1950 return &generic_container->type_params [index];
1954 * do_mono_metadata_parse_type:
1955 * @type: MonoType to be filled in with the return value
1957 * @generic_context: generics_context
1958 * @ptr: pointer to the encoded type
1959 * @rptr: pointer where the end of the encoded type is saved
1961 * Internal routine used to "fill" the contents of @type from an
1962 * allocated pointer. This is done this way to avoid doing too
1963 * many mini-allocations (particularly for the MonoFieldType which
1964 * most of the time is just a MonoType, but sometimes might be augmented).
1966 * This routine is used by mono_metadata_parse_type and
1967 * mono_metadata_parse_field_type
1969 * This extracts a Type as specified in Partition II (22.2.12)
1971 * Returns: FALSE if the type could not be loaded
1974 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container,
1975 const char *ptr, const char **rptr)
1977 type->type = mono_metadata_decode_value (ptr, &ptr);
1979 switch (type->type){
1980 case MONO_TYPE_VOID:
1981 case MONO_TYPE_BOOLEAN:
1982 case MONO_TYPE_CHAR:
1995 case MONO_TYPE_STRING:
1996 case MONO_TYPE_OBJECT:
1997 case MONO_TYPE_TYPEDBYREF:
1999 case MONO_TYPE_VALUETYPE:
2000 case MONO_TYPE_CLASS: {
2002 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
2003 type->data.klass = mono_class_get (m, token);
2004 if (!type->data.klass)
2008 case MONO_TYPE_SZARRAY: {
2009 MonoType *etype = mono_metadata_parse_type_full (m, container, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
2012 type->data.klass = mono_class_from_mono_type (etype);
2013 mono_metadata_free_type (etype);
2017 type->data.type = mono_metadata_parse_type_full (m, container, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
2018 if (!type->data.type)
2021 case MONO_TYPE_FNPTR:
2022 type->data.method = mono_metadata_parse_method_signature_full (m, container, 0, ptr, &ptr);
2024 case MONO_TYPE_ARRAY:
2025 type->data.array = mono_metadata_parse_array_full (m, container, ptr, &ptr);
2027 case MONO_TYPE_MVAR:
2029 type->data.generic_param = mono_metadata_parse_generic_param (m, container, type->type, ptr, &ptr);
2031 case MONO_TYPE_GENERICINST:
2032 do_mono_metadata_parse_generic_class (type, m, container, ptr, &ptr);
2035 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
2044 * mono_metadata_free_type:
2045 * @type: type to free
2047 * Free the memory allocated for type @type.
2050 mono_metadata_free_type (MonoType *type)
2052 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
2055 switch (type->type){
2056 case MONO_TYPE_OBJECT:
2057 case MONO_TYPE_STRING:
2058 if (!type->data.klass)
2061 case MONO_TYPE_CLASS:
2062 case MONO_TYPE_VALUETYPE:
2063 if (type == &type->data.klass->byval_arg || type == &type->data.klass->this_arg)
2067 mono_metadata_free_type (type->data.type);
2069 case MONO_TYPE_FNPTR:
2070 mono_metadata_free_method_signature (type->data.method);
2072 case MONO_TYPE_ARRAY:
2073 mono_metadata_free_array (type->data.array);
2081 hex_dump (const char *buffer, int base, int count)
2083 int show_header = 1;
2091 for (i = 0; i < count; i++){
2094 printf ("\n0x%08x: ", (unsigned char) base + i);
2096 printf ("%02x ", (unsigned char) (buffer [i]));
2103 * @mh: The Method header
2104 * @ptr: Points to the beginning of the Section Data (25.3)
2107 parse_section_data (MonoImage *m, MonoMethodHeader *mh, const unsigned char *ptr)
2109 unsigned char sect_data_flags;
2110 const unsigned char *sptr;
2112 guint32 sect_data_len;
2115 /* align on 32-bit boundary */
2116 /* FIXME: not 64-bit clean code */
2117 sptr = ptr = dword_align (ptr);
2118 sect_data_flags = *ptr;
2121 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
2123 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
2126 sect_data_len = ptr [0];
2130 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
2131 hex_dump (sptr, 0, sect_data_len+8);
2132 g_print ("\nheader: ");
2133 hex_dump (sptr-4, 0, 4);
2137 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
2138 const unsigned char *p = dword_align (ptr);
2140 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
2141 /* we could just store a pointer if we don't need to byteswap */
2142 mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
2143 for (i = 0; i < mh->num_clauses; ++i) {
2144 MonoExceptionClause *ec = &mh->clauses [i];
2147 ec->flags = read32 (p);
2148 ec->try_offset = read32 (p + 4);
2149 ec->try_len = read32 (p + 8);
2150 ec->handler_offset = read32 (p + 12);
2151 ec->handler_len = read32 (p + 16);
2152 tof_value = read32 (p + 20);
2155 ec->flags = read16 (p);
2156 ec->try_offset = read16 (p + 2);
2157 ec->try_len = *(p + 4);
2158 ec->handler_offset = read16 (p + 5);
2159 ec->handler_len = *(p + 7);
2160 tof_value = read32 (p + 8);
2163 if (ec->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2164 ec->data.filter_offset = tof_value;
2165 } else if (ec->flags == MONO_EXCEPTION_CLAUSE_NONE) {
2166 ec->data.catch_class = tof_value? mono_class_get (m, tof_value): 0;
2168 ec->data.catch_class = NULL;
2170 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
2174 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
2175 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
2182 * mono_metadata_parse_mh_full:
2183 * @m: metadata context
2184 * @generic_context: generics context
2185 * @ptr: pointer to the method header.
2187 * Decode the method header at @ptr, including pointer to the IL code,
2188 * info about local variables and optional exception tables.
2189 * This is a Mono runtime internal function.
2191 * Returns: a MonoMethodHeader.
2194 mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, const char *ptr)
2196 MonoMethodHeader *mh;
2197 unsigned char flags = *(const unsigned char *) ptr;
2198 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
2200 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
2201 const unsigned char *code;
2204 g_return_val_if_fail (ptr != NULL, NULL);
2207 case METHOD_HEADER_TINY_FORMAT:
2208 mh = g_new0 (MonoMethodHeader, 1);
2211 local_var_sig_tok = 0;
2212 mh->code_size = flags >> 2;
2215 case METHOD_HEADER_TINY_FORMAT1:
2216 mh = g_new0 (MonoMethodHeader, 1);
2219 local_var_sig_tok = 0;
2222 * The spec claims 3 bits, but the Beta2 is
2225 mh->code_size = flags >> 2;
2228 case METHOD_HEADER_FAT_FORMAT:
2229 fat_flags = read16 (ptr);
2231 hsize = (fat_flags >> 12) & 0xf;
2232 max_stack = read16 (ptr);
2234 code_size = read32 (ptr);
2236 local_var_sig_tok = read32 (ptr);
2239 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
2246 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
2250 * There are more sections
2252 ptr = code + code_size;
2258 if (local_var_sig_tok) {
2259 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
2260 const char *locals_ptr;
2261 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
2262 int len=0, i, bsize;
2264 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
2265 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
2266 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
2267 if (*locals_ptr != 0x07)
2268 g_warning ("wrong signature for locals blob");
2270 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
2271 mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
2272 mh->num_locals = len;
2273 for (i = 0; i < len; ++i) {
2274 mh->locals [i] = mono_metadata_parse_type_full (
2275 m, container, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
2276 if (!mh->locals [i]) {
2282 mh = g_new0 (MonoMethodHeader, 1);
2285 mh->code_size = code_size;
2286 mh->max_stack = max_stack;
2287 mh->init_locals = init_locals;
2288 if (fat_flags & METHOD_HEADER_MORE_SECTS)
2289 parse_section_data (m, mh, (const unsigned char*)ptr);
2294 * mono_metadata_parse_mh:
2295 * @generic_context: generics context
2296 * @ptr: pointer to the method header.
2298 * Decode the method header at @ptr, including pointer to the IL code,
2299 * info about local variables and optional exception tables.
2300 * This is a Mono runtime internal function.
2302 * Returns: a MonoMethodHeader.
2305 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
2307 return mono_metadata_parse_mh_full (m, NULL, ptr);
2311 * mono_metadata_free_mh:
2312 * @mh: a method header
2314 * Free the memory allocated for the method header.
2315 * This is a Mono runtime internal function.
2318 mono_metadata_free_mh (MonoMethodHeader *mh)
2321 for (i = 0; i < mh->num_locals; ++i)
2322 mono_metadata_free_type (mh->locals[i]);
2323 g_free (mh->clauses);
2328 * mono_method_header_get_code:
2329 * @header: a MonoMethodHeader pointer
2330 * @code_size: memory location for returning the code size
2331 * @max_stack: memory location for returning the max stack
2333 * Method header accessor to retreive info about the IL code properties:
2334 * a pointer to the IL code itself, the size of the code and the max number
2335 * of stack slots used by the code.
2337 * Returns: pointer to the IL code represented by the method header.
2339 const unsigned char*
2340 mono_method_header_get_code (MonoMethodHeader *header, guint32* code_size, guint32* max_stack)
2343 *code_size = header->code_size;
2345 *max_stack = header->max_stack;
2346 return header->code;
2350 * mono_method_header_get_locals:
2351 * @header: a MonoMethodHeader pointer
2352 * @num_locals: memory location for returning the number of local variables
2353 * @init_locals: memory location for returning the init_locals flag
2355 * Method header accessor to retreive info about the local variables:
2356 * an array of local types, the number of locals and whether the locals
2357 * are supposed to be initialized to 0 on method entry
2359 * Returns: pointer to an array of types of the local variables
2362 mono_method_header_get_locals (MonoMethodHeader *header, guint32* num_locals, gboolean *init_locals)
2365 *num_locals = header->num_locals;
2367 *init_locals = header->init_locals;
2368 return header->locals;
2372 * mono_method_header_get_num_clauses:
2373 * @header: a MonoMethodHeader pointer
2375 * Method header accessor to retreive the number of exception clauses.
2377 * Returns: the number of exception clauses present
2380 mono_method_header_get_num_clauses (MonoMethodHeader *header)
2382 return header->num_clauses;
2386 * mono_method_header_get_clauses:
2387 * @header: a MonoMethodHeader pointer
2388 * @method: MonoMethod the header belongs to
2389 * @iter: pointer to a iterator
2390 * @clause: pointer to a MonoExceptionClause structure which will be filled with the info
2392 * Get the info about the exception clauses in the method. Set *iter to NULL to
2393 * initiate the iteration, then call the method repeatedly until it returns FALSE.
2394 * At each iteration, the structure pointed to by clause if filled with the
2395 * exception clause information.
2397 * Returns: TRUE if clause was filled with info, FALSE if there are no more exception
2401 mono_method_header_get_clauses (MonoMethodHeader *header, MonoMethod *method, gpointer *iter, MonoExceptionClause *clause)
2403 MonoExceptionClause *sc;
2404 /* later we'll be able to use this interface to parse the clause info on demand,
2405 * without allocating anything.
2407 if (!iter || !header->num_clauses)
2410 *iter = sc = header->clauses;
2416 if (sc < header->clauses + header->num_clauses) {
2425 * mono_metadata_parse_field_type:
2426 * @m: metadata context to extract information from
2427 * @ptr: pointer to the field signature
2428 * @rptr: pointer updated to match the end of the decoded stream
2430 * Parses the field signature, and returns the type information for it.
2432 * Returns: The MonoType that was extracted from @ptr.
2435 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
2437 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
2441 * mono_metadata_parse_param:
2442 * @m: metadata context to extract information from
2443 * @ptr: pointer to the param signature
2444 * @rptr: pointer updated to match the end of the decoded stream
2446 * Parses the param signature, and returns the type information for it.
2448 * Returns: The MonoType that was extracted from @ptr.
2451 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
2453 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
2457 * mono_metadata_token_from_dor:
2458 * @dor_token: A TypeDefOrRef coded index
2460 * dor_token is a TypeDefOrRef coded index: it contains either
2461 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
2462 * bits contain an index into the table.
2464 * Returns: an expanded token
2467 mono_metadata_token_from_dor (guint32 dor_index)
2471 table = dor_index & 0x03;
2472 idx = dor_index >> 2;
2475 case 0: /* TypeDef */
2476 return MONO_TOKEN_TYPE_DEF | idx;
2477 case 1: /* TypeRef */
2478 return MONO_TOKEN_TYPE_REF | idx;
2479 case 2: /* TypeSpec */
2480 return MONO_TOKEN_TYPE_SPEC | idx;
2482 g_assert_not_reached ();
2489 * We use this to pass context information to the row locator
2492 int idx; /* The index that we are trying to locate */
2493 int col_idx; /* The index in the row where idx may be stored */
2494 MonoTableInfo *t; /* pointer to the table */
2499 * How the row locator works.
2504 * ___|___------> _______
2507 * A column in the rows of table A references an index in table B.
2508 * For example A may be the TYPEDEF table and B the METHODDEF table.
2510 * Given an index in table B we want to get the row in table A
2511 * where the column n references our index in B.
2513 * In the locator_t structure:
2515 * col_idx is the column number
2516 * index is the index in table B
2517 * result will be the index in table A
2520 * Table A Table B column (in table A)
2521 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
2522 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
2523 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
2524 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
2525 * METHODSEM PROPERTY ASSOCIATION (encoded index)
2527 * Note that we still don't support encoded indexes.
2531 typedef_locator (const void *a, const void *b)
2533 locator_t *loc = (locator_t *) a;
2534 const char *bb = (const char *) b;
2535 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
2536 guint32 col, col_next;
2538 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
2544 * Need to check that the next row is valid.
2546 if (typedef_index + 1 < loc->t->rows) {
2547 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
2548 if (loc->idx >= col_next)
2551 if (col == col_next)
2555 loc->result = typedef_index;
2561 table_locator (const void *a, const void *b)
2563 locator_t *loc = (locator_t *) a;
2564 const char *bb = (const char *) b;
2565 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2568 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2570 if (loc->idx == col) {
2571 loc->result = table_index;
2581 declsec_locator (const void *a, const void *b)
2583 locator_t *loc = (locator_t *) a;
2584 const char *bb = (const char *) b;
2585 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2588 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2590 if (loc->idx == col) {
2591 loc->result = table_index;
2601 * mono_metadata_typedef_from_field:
2602 * @meta: metadata context
2603 * @index: FieldDef token
2605 * Returns: the 1-based index into the TypeDef table of the type that
2606 * declared the field described by @index, or 0 if not found.
2609 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
2611 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2617 loc.idx = mono_metadata_token_index (index);
2618 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
2621 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2622 g_assert_not_reached ();
2624 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2625 return loc.result + 1;
2629 * mono_metadata_typedef_from_method:
2630 * @meta: metadata context
2631 * @index: MethodDef token
2633 * Returns: the 1-based index into the TypeDef table of the type that
2634 * declared the method described by @index. 0 if not found.
2637 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
2639 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2645 loc.idx = mono_metadata_token_index (index);
2646 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
2649 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2650 g_assert_not_reached ();
2652 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2653 return loc.result + 1;
2657 * mono_metadata_interfaces_from_typedef_full:
2658 * @meta: metadata context
2659 * @index: typedef token
2661 * The array of interfaces that the @index typedef token implements is returned in
2662 * @interfaces. The number of elemnts in the array is returned in @count.
2664 * Returns: TRUE on success, FALSE on failure.
2667 mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, MonoClass ***interfaces, guint *count, MonoGenericContext *context)
2669 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
2672 guint32 cols [MONO_INTERFACEIMPL_SIZE];
2681 loc.idx = mono_metadata_token_index (index);
2682 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
2685 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2690 * We may end up in the middle of the rows...
2693 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
2700 while (start < tdef->rows) {
2701 mono_metadata_decode_row (tdef, start, cols, MONO_INTERFACEIMPL_SIZE);
2702 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
2704 result = g_renew (MonoClass*, result, i + 1);
2705 result [i] = mono_class_get_full (
2706 meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]), context);
2710 *interfaces = result;
2715 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
2717 MonoClass **interfaces;
2720 rv = mono_metadata_interfaces_from_typedef_full (meta, index, &interfaces, count, NULL);
2728 * mono_metadata_nested_in_typedef:
2729 * @meta: metadata context
2730 * @index: typedef token
2732 * Returns: the 1-based index into the TypeDef table of the type
2733 * where the type described by @index is nested.
2734 * Retruns 0 if @index describes a non-nested type.
2737 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
2739 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2745 loc.idx = mono_metadata_token_index (index);
2746 loc.col_idx = MONO_NESTED_CLASS_NESTED;
2749 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2752 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2753 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
2757 * mono_metadata_nesting_typedef:
2758 * @meta: metadata context
2759 * @index: typedef token
2761 * Returns: the 1-based index into the TypeDef table of the first type
2762 * that is nested inside the type described by @index. The search starts at
2763 * @start_index. returns 0 if no such type is found.
2766 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
2768 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2770 guint32 class_index = mono_metadata_token_index (index);
2775 start = start_index;
2777 while (start <= tdef->rows) {
2778 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
2784 if (start > tdef->rows)
2791 * mono_metadata_packing_from_typedef:
2792 * @meta: metadata context
2793 * @index: token representing a type
2795 * Returns: the info stored in the ClassLAyout table for the given typedef token
2796 * into the @packing and @size pointers.
2797 * Returns 0 if the info is not found.
2800 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
2802 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
2804 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
2809 loc.idx = mono_metadata_token_index (index);
2810 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
2813 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2816 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
2818 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
2820 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
2822 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2823 return loc.result + 1;
2827 * mono_metadata_custom_attrs_from_index:
2828 * @meta: metadata context
2829 * @index: token representing the parent
2831 * Returns: the 1-based index into the CustomAttribute table of the first
2832 * attribute which belongs to the metadata object described by @index.
2833 * Returns 0 if no such attribute is found.
2836 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
2838 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
2845 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
2848 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2851 /* Find the first entry by searching backwards */
2852 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
2855 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2856 return loc.result + 1;
2860 * mono_metadata_declsec_from_index:
2861 * @meta: metadata context
2862 * @index: token representing the parent
2864 * Returns: the 0-based index into the DeclarativeSecurity table of the first
2865 * attribute which belongs to the metadata object described by @index.
2866 * Returns -1 if no such attribute is found.
2869 mono_metadata_declsec_from_index (MonoImage *meta, guint32 index)
2871 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_DECLSECURITY];
2878 loc.col_idx = MONO_DECL_SECURITY_PARENT;
2881 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
2884 /* Find the first entry by searching backwards */
2885 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_DECL_SECURITY_PARENT) == index))
2893 mono_backtrace (int limit)
2898 backtrace (array, limit);
2899 names = backtrace_symbols (array, limit);
2900 for (i =0; i < limit; ++i) {
2901 g_print ("\t%s\n", names [i]);
2908 /*#define __alignof__(a) sizeof(a)*/
2909 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
2914 * @t: the type to return the size of
2916 * Returns: the number of bytes required to hold an instance of this
2920 mono_type_size (MonoType *t, gint *align)
2927 *align = __alignof__(gpointer);
2928 return sizeof (gpointer);
2932 case MONO_TYPE_VOID:
2935 case MONO_TYPE_BOOLEAN:
2936 *align = __alignof__(gint8);
2940 *align = __alignof__(gint8);
2942 case MONO_TYPE_CHAR:
2945 *align = __alignof__(gint16);
2949 *align = __alignof__(gint32);
2952 *align = __alignof__(float);
2956 *align = __alignof__(gint64);
2959 *align = __alignof__(double);
2963 *align = __alignof__(gpointer);
2964 return sizeof (gpointer);
2965 case MONO_TYPE_STRING:
2966 *align = __alignof__(gpointer);
2967 return sizeof (gpointer);
2968 case MONO_TYPE_OBJECT:
2969 *align = __alignof__(gpointer);
2970 return sizeof (gpointer);
2971 case MONO_TYPE_VALUETYPE: {
2972 if (t->data.klass->enumtype)
2973 return mono_type_size (t->data.klass->enum_basetype, align);
2975 return mono_class_value_size (t->data.klass, align);
2977 case MONO_TYPE_CLASS:
2978 case MONO_TYPE_SZARRAY:
2980 case MONO_TYPE_FNPTR:
2981 case MONO_TYPE_ARRAY:
2982 *align = __alignof__(gpointer);
2983 return sizeof (gpointer);
2984 case MONO_TYPE_TYPEDBYREF:
2985 return mono_class_value_size (mono_defaults.typed_reference_class, align);
2986 case MONO_TYPE_GENERICINST: {
2987 MonoInflatedGenericClass *gclass;
2988 MonoClass *container_class;
2990 gclass = mono_get_inflated_generic_class (t->data.generic_class);
2991 g_assert (!gclass->generic_class.inst->is_open);
2992 g_assert (!gclass->klass->generic_container);
2994 container_class = gclass->generic_class.container_class;
2996 if (container_class->valuetype) {
2997 if (container_class->enumtype)
2998 return mono_type_size (container_class->enum_basetype, align);
3000 return mono_class_value_size (gclass->klass, align);
3002 *align = __alignof__(gpointer);
3003 return sizeof (gpointer);
3007 case MONO_TYPE_MVAR:
3008 /* FIXME: Martin, this is wrong. */
3009 *align = __alignof__(gpointer);
3010 return sizeof (gpointer);
3012 g_error ("mono_type_size: type 0x%02x unknown", t->type);
3018 * mono_type_stack_size:
3019 * @t: the type to return the size it uses on the stack
3021 * Returns: the number of bytes required to hold an instance of this
3022 * type on the runtime stack
3025 mono_type_stack_size (MonoType *t, gint *align)
3029 g_assert (t != NULL);
3035 *align = __alignof__(gpointer);
3036 return sizeof (gpointer);
3040 case MONO_TYPE_BOOLEAN:
3041 case MONO_TYPE_CHAR:
3050 case MONO_TYPE_STRING:
3051 case MONO_TYPE_OBJECT:
3052 case MONO_TYPE_CLASS:
3053 case MONO_TYPE_SZARRAY:
3055 case MONO_TYPE_FNPTR:
3056 case MONO_TYPE_ARRAY:
3057 *align = __alignof__(gpointer);
3058 return sizeof (gpointer);
3059 case MONO_TYPE_TYPEDBYREF:
3060 *align = __alignof__(gpointer);
3061 return sizeof (gpointer) * 3;
3063 *align = __alignof__(float);
3064 return sizeof (float);
3067 *align = __alignof__(gint64);
3068 return sizeof (gint64);
3070 *align = __alignof__(double);
3071 return sizeof (double);
3072 case MONO_TYPE_VALUETYPE: {
3075 if (t->data.klass->enumtype)
3076 return mono_type_stack_size (t->data.klass->enum_basetype, align);
3078 size = mono_class_value_size (t->data.klass, align);
3080 *align = *align + __alignof__(gpointer) - 1;
3081 *align &= ~(__alignof__(gpointer) - 1);
3083 size += sizeof (gpointer) - 1;
3084 size &= ~(sizeof (gpointer) - 1);
3089 case MONO_TYPE_GENERICINST: {
3090 MonoInflatedGenericClass *gclass;
3091 MonoClass *container_class;
3093 gclass = mono_get_inflated_generic_class (t->data.generic_class);
3094 container_class = gclass->generic_class.container_class;
3096 g_assert (!gclass->generic_class.inst->is_open);
3097 g_assert (!gclass->klass->generic_container);
3099 if (container_class->valuetype) {
3100 if (container_class->enumtype)
3101 return mono_type_stack_size (container_class->enum_basetype, align);
3103 guint32 size = mono_class_value_size (gclass->klass, align);
3105 *align = *align + __alignof__(gpointer) - 1;
3106 *align &= ~(__alignof__(gpointer) - 1);
3108 size += sizeof (gpointer) - 1;
3109 size &= ~(sizeof (gpointer) - 1);
3114 *align = __alignof__(gpointer);
3115 return sizeof (gpointer);
3119 g_error ("type 0x%02x unknown", t->type);
3125 mono_type_generic_inst_is_valuetype (MonoType *type)
3127 g_assert (type->type == MONO_TYPE_GENERICINST);
3128 return type->data.generic_class->container_class->valuetype;
3132 mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass)
3134 return gclass->container_class->valuetype;
3138 _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2, gboolean signature_only)
3142 if ((g1->inst->type_argc != g2->inst->type_argc) || (g1->is_dynamic != g2->is_dynamic) ||
3143 (g1->inst->is_reference != g2->inst->is_reference))
3145 if (!mono_metadata_class_equal (g1->container_class, g2->container_class, signature_only))
3147 for (i = 0; i < g1->inst->type_argc; ++i) {
3148 if (!do_mono_metadata_type_equal (g1->inst->type_argv [i], g2->inst->type_argv [i], signature_only))
3155 mono_metadata_generic_method_hash (MonoGenericMethod *gmethod)
3157 return gmethod->inst->id;
3161 mono_metadata_generic_method_equal (MonoGenericMethod *g1, MonoGenericMethod *g2)
3163 return (g1->container == g2->container) && (g1->generic_class == g2->generic_class) &&
3164 (g1->inst == g2->inst);
3169 * mono_metadata_type_hash:
3172 * Computes an hash value for @t1 to be used in GHashTable.
3175 mono_metadata_type_hash (MonoType *t1)
3177 guint hash = t1->type;
3179 hash |= t1->byref << 6; /* do not collide with t1->type values */
3181 case MONO_TYPE_VALUETYPE:
3182 case MONO_TYPE_CLASS:
3183 case MONO_TYPE_SZARRAY:
3184 /* check if the distribution is good enough */
3185 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
3187 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
3188 case MONO_TYPE_ARRAY:
3189 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
3190 case MONO_TYPE_GENERICINST:
3191 return ((hash << 5) - hash) ^ mono_generic_class_hash (t1->data.generic_class);
3197 mono_metadata_generic_param_equal (MonoGenericParam *p1, MonoGenericParam *p2, gboolean signature_only)
3201 if (p1->num != p2->num)
3204 if (p1->owner == p2->owner)
3208 * If `signature_only' is true, we're comparing two (method) signatures.
3209 * In this case, the owner of two type parameters doesn't need to match.
3212 return signature_only;
3216 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only)
3220 if (c1->generic_class && c2->generic_class)
3221 return _mono_metadata_generic_class_equal (c1->generic_class, c2->generic_class, signature_only);
3222 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
3223 return mono_metadata_generic_param_equal (
3224 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
3225 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
3226 return mono_metadata_generic_param_equal (
3227 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
3228 if (signature_only &&
3229 (c1->byval_arg.type == MONO_TYPE_SZARRAY) && (c2->byval_arg.type == MONO_TYPE_SZARRAY))
3230 return mono_metadata_class_equal (c1->byval_arg.data.klass, c2->byval_arg.data.klass, signature_only);
3235 * mono_metadata_type_equal:
3239 * Determine if @t1 and @t2 represent the same type.
3240 * Returns: #TRUE if @t1 and @t2 are equal.
3243 do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only)
3245 if (t1->type != t2->type || t1->byref != t2->byref)
3249 case MONO_TYPE_VOID:
3250 case MONO_TYPE_BOOLEAN:
3251 case MONO_TYPE_CHAR:
3262 case MONO_TYPE_STRING:
3265 case MONO_TYPE_OBJECT:
3266 case MONO_TYPE_TYPEDBYREF:
3268 case MONO_TYPE_VALUETYPE:
3269 case MONO_TYPE_CLASS:
3270 case MONO_TYPE_SZARRAY:
3271 return mono_metadata_class_equal (t1->data.klass, t2->data.klass, signature_only);
3273 return do_mono_metadata_type_equal (t1->data.type, t2->data.type, signature_only);
3274 case MONO_TYPE_ARRAY:
3275 if (t1->data.array->rank != t2->data.array->rank)
3277 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass, signature_only);
3278 case MONO_TYPE_GENERICINST:
3279 return _mono_metadata_generic_class_equal (
3280 t1->data.generic_class, t2->data.generic_class, signature_only);
3282 return mono_metadata_generic_param_equal (
3283 t1->data.generic_param, t2->data.generic_param, signature_only);
3284 case MONO_TYPE_MVAR:
3285 return mono_metadata_generic_param_equal (
3286 t1->data.generic_param, t2->data.generic_param, signature_only);
3288 g_error ("implement type compare for %0x!", t1->type);
3296 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
3298 return do_mono_metadata_type_equal (t1, t2, FALSE);
3302 * mono_metadata_signature_equal:
3303 * @sig1: a signature
3304 * @sig2: another signature
3306 * Determine if @sig1 and @sig2 represent the same signature, with the
3307 * same number of arguments and the same types.
3308 * Returns: #TRUE if @sig1 and @sig2 are equal.
3311 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
3315 if (sig1->hasthis != sig2->hasthis || sig1->param_count != sig2->param_count)
3319 * We're just comparing the signatures of two methods here:
3321 * If we have two generic methods `void Foo<U> (U u)' and `void Bar<V> (V v)',
3322 * U and V are equal here.
3324 * That's what the `signature_only' argument of do_mono_metadata_type_equal() is for.
3327 for (i = 0; i < sig1->param_count; i++) {
3328 MonoType *p1 = sig1->params[i];
3329 MonoType *p2 = sig2->params[i];
3331 /* if (p1->attrs != p2->attrs)
3334 if (!do_mono_metadata_type_equal (p1, p2, TRUE))
3338 if (!do_mono_metadata_type_equal (sig1->ret, sig2->ret, TRUE))
3344 mono_signature_hash (MonoMethodSignature *sig)
3346 guint i, res = sig->ret->type;
3348 for (i = 0; i < sig->param_count; i++)
3349 res = (res << 5) - res + mono_type_hash (sig->params[i]);
3355 * mono_metadata_encode_value:
3356 * @value: value to encode
3357 * @buf: buffer where to write the compressed representation
3358 * @endbuf: pointer updated to point at the end of the encoded output
3360 * Encodes the value @value in the compressed representation used
3361 * in metadata and stores the result in @buf. @buf needs to be big
3362 * enough to hold the data (4 bytes).
3365 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
3371 else if (value < 0x4000) {
3372 p [0] = 0x80 | (value >> 8);
3373 p [1] = value & 0xff;
3376 p [0] = (value >> 24) | 0xc0;
3377 p [1] = (value >> 16) & 0xff;
3378 p [2] = (value >> 8) & 0xff;
3379 p [3] = value & 0xff;
3387 * mono_metadata_field_info:
3388 * @meta: the Image the field is defined in
3389 * @index: the index in the field table representing the field
3390 * @offset: a pointer to an integer where to store the offset that
3391 * may have been specified for the field in a FieldLayout table
3392 * @rva: a pointer to the RVA of the field data in the image that
3393 * may have been defined in a FieldRVA table
3394 * @marshal_spec: a pointer to the marshal spec that may have been
3395 * defined for the field in a FieldMarshal table.
3397 * Gather info for field @index that may have been defined in the FieldLayout,
3398 * FieldRVA and FieldMarshal tables.
3399 * Either of offset, rva and marshal_spec can be NULL if you're not interested
3403 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
3404 MonoMarshalSpec **marshal_spec)
3406 MonoTableInfo *tdef;
3409 loc.idx = index + 1;
3411 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
3413 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
3416 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3417 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
3419 *offset = (guint32)-1;
3423 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
3425 loc.col_idx = MONO_FIELD_RVA_FIELD;
3428 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3430 * LAMESPEC: There is no signature, no nothing, just the raw data.
3432 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
3440 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
3441 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
3448 * mono_metadata_get_constant_index:
3449 * @meta: the Image the field is defined in
3450 * @index: the token that may have a row defined in the constants table
3451 * @hint: possible position for the row
3453 * @token must be a FieldDef, ParamDef or PropertyDef token.
3455 * Returns: the index into the Constants table or 0 if not found.
3458 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
3460 MonoTableInfo *tdef;
3462 guint32 index = mono_metadata_token_index (token);
3464 tdef = &meta->tables [MONO_TABLE_CONSTANT];
3465 index <<= MONO_HASCONSTANT_BITS;
3466 switch (mono_metadata_token_table (token)) {
3467 case MONO_TABLE_FIELD:
3468 index |= MONO_HASCONSTANT_FIEDDEF;
3470 case MONO_TABLE_PARAM:
3471 index |= MONO_HASCONSTANT_PARAM;
3473 case MONO_TABLE_PROPERTY:
3474 index |= MONO_HASCONSTANT_PROPERTY;
3477 g_warning ("Not a valid token for the constant table: 0x%08x", token);
3481 loc.col_idx = MONO_CONSTANT_PARENT;
3484 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
3487 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3488 return loc.result + 1;
3494 * mono_metadata_events_from_typedef:
3495 * @meta: metadata context
3496 * @index: 0-based index (in the TypeDef table) describing a type
3498 * Returns: the 0-based index in the Event table for the events in the
3499 * type. The last event that belongs to the type (plus 1) is stored
3500 * in the @end_idx pointer.
3503 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3507 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
3515 loc.col_idx = MONO_EVENT_MAP_PARENT;
3516 loc.idx = index + 1;
3518 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3521 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
3522 if (loc.result + 1 < tdef->rows) {
3523 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
3525 end = meta->tables [MONO_TABLE_EVENT].rows;
3533 * mono_metadata_methods_from_event:
3534 * @meta: metadata context
3535 * @index: 0-based index (in the Event table) describing a event
3537 * Returns: the 0-based index in the MethodDef table for the methods in the
3538 * event. The last method that belongs to the event (plus 1) is stored
3539 * in the @end_idx pointer.
3542 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
3546 guint32 cols [MONO_METHOD_SEMA_SIZE];
3547 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3554 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3555 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
3557 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3562 * We may end up in the middle of the rows...
3565 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3571 while (end < msemt->rows) {
3572 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3573 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3582 * mono_metadata_properties_from_typedef:
3583 * @meta: metadata context
3584 * @index: 0-based index (in the TypeDef table) describing a type
3586 * Returns: the 0-based index in the Property table for the properties in the
3587 * type. The last property that belongs to the type (plus 1) is stored
3588 * in the @end_idx pointer.
3591 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3595 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
3603 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
3604 loc.idx = index + 1;
3606 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3609 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
3610 if (loc.result + 1 < tdef->rows) {
3611 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
3613 end = meta->tables [MONO_TABLE_PROPERTY].rows;
3621 * mono_metadata_methods_from_property:
3622 * @meta: metadata context
3623 * @index: 0-based index (in the PropertyDef table) describing a property
3625 * Returns: the 0-based index in the MethodDef table for the methods in the
3626 * property. The last method that belongs to the property (plus 1) is stored
3627 * in the @end_idx pointer.
3630 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
3634 guint32 cols [MONO_METHOD_SEMA_SIZE];
3635 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3642 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3643 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
3645 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3650 * We may end up in the middle of the rows...
3653 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3659 while (end < msemt->rows) {
3660 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3661 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3670 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
3673 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
3679 loc.col_idx = MONO_IMPLMAP_MEMBER;
3680 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
3682 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3685 return loc.result + 1;
3689 * @image: context where the image is created
3690 * @type_spec: typespec token
3692 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
3696 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
3698 guint32 idx = mono_metadata_token_index (type_spec);
3700 guint32 cols [MONO_TYPESPEC_SIZE];
3705 mono_loader_lock ();
3707 type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec));
3709 mono_loader_unlock ();
3713 t = &image->tables [MONO_TABLE_TYPESPEC];
3715 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
3716 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
3717 len = mono_metadata_decode_value (ptr, &ptr);
3719 type = g_new0 (MonoType, 1);
3721 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
3723 if (*ptr == MONO_TYPE_BYREF) {
3728 if (!do_mono_metadata_parse_type (type, image, NULL, ptr, &ptr)) {
3729 g_hash_table_remove (image->typespec_cache, GUINT_TO_POINTER (type_spec));
3731 mono_loader_unlock ();
3735 mono_loader_unlock ();
3741 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
3743 MonoMarshalSpec *res;
3745 const char *start = ptr;
3747 /* fixme: this is incomplete, but I cant find more infos in the specs */
3749 res = g_new0 (MonoMarshalSpec, 1);
3751 len = mono_metadata_decode_value (ptr, &ptr);
3752 res->native = *ptr++;
3754 if (res->native == MONO_NATIVE_LPARRAY) {
3755 res->data.array_data.param_num = -1;
3756 res->data.array_data.num_elem = -1;
3757 res->data.array_data.elem_mult = -1;
3759 if (ptr - start <= len)
3760 res->data.array_data.elem_type = *ptr++;
3761 if (ptr - start <= len)
3762 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
3763 if (ptr - start <= len)
3764 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3765 if (ptr - start <= len) {
3767 * LAMESPEC: Older spec versions say this parameter comes before
3768 * num_elem. Never spec versions don't talk about elem_mult at
3769 * all, but csc still emits it, and it is used to distinguish
3770 * between param_num being 0, and param_num being omitted.
3771 * So if (param_num == 0) && (num_elem > 0), then
3772 * elem_mult == 0 -> the array size is num_elem
3773 * elem_mult == 1 -> the array size is @param_num + num_elem
3775 res->data.array_data.elem_mult = mono_metadata_decode_value (ptr, &ptr);
3779 if (res->native == MONO_NATIVE_BYVALTSTR) {
3780 if (ptr - start <= len)
3781 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3784 if (res->native == MONO_NATIVE_BYVALARRAY) {
3785 if (ptr - start <= len)
3786 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3789 if (res->native == MONO_NATIVE_CUSTOM) {
3790 /* skip unused type guid */
3791 len = mono_metadata_decode_value (ptr, &ptr);
3793 /* skip unused native type name */
3794 len = mono_metadata_decode_value (ptr, &ptr);
3796 /* read custom marshaler type name */
3797 len = mono_metadata_decode_value (ptr, &ptr);
3798 res->data.custom_data.custom_name = g_strndup (ptr, len);
3800 /* read cookie string */
3801 len = mono_metadata_decode_value (ptr, &ptr);
3802 res->data.custom_data.cookie = g_strndup (ptr, len);
3805 if (res->native == MONO_NATIVE_SAFEARRAY) {
3806 res->data.safearray_data.elem_type = 0;
3807 res->data.safearray_data.num_elem = 0;
3808 if (ptr - start <= len)
3809 res->data.safearray_data.elem_type = *ptr++;
3810 if (ptr - start <= len)
3811 res->data.safearray_data.num_elem = *ptr++;
3817 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
3819 if (spec->native == MONO_NATIVE_CUSTOM) {
3820 g_free (spec->data.custom_data.custom_name);
3821 g_free (spec->data.custom_data.cookie);
3827 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
3828 gboolean unicode, MonoMarshalConv *conv)
3830 MonoMarshalConv dummy_conv;
3836 *conv = MONO_MARSHAL_CONV_NONE;
3839 return MONO_NATIVE_UINT;
3843 case MONO_TYPE_BOOLEAN:
3845 switch (mspec->native) {
3846 case MONO_NATIVE_VARIANTBOOL:
3847 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
3848 return MONO_NATIVE_VARIANTBOOL;
3849 case MONO_NATIVE_BOOLEAN:
3850 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3851 return MONO_NATIVE_BOOLEAN;
3852 case MONO_NATIVE_I1:
3853 case MONO_NATIVE_U1:
3854 return mspec->native;
3856 g_error ("cant marshal bool to native type %02x", mspec->native);
3859 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3860 return MONO_NATIVE_BOOLEAN;
3861 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
3862 case MONO_TYPE_I1: return MONO_NATIVE_I1;
3863 case MONO_TYPE_U1: return MONO_NATIVE_U1;
3864 case MONO_TYPE_I2: return MONO_NATIVE_I2;
3865 case MONO_TYPE_U2: return MONO_NATIVE_U2;
3866 case MONO_TYPE_I4: return MONO_NATIVE_I4;
3867 case MONO_TYPE_U4: return MONO_NATIVE_U4;
3868 case MONO_TYPE_I8: return MONO_NATIVE_I8;
3869 case MONO_TYPE_U8: return MONO_NATIVE_U8;
3870 case MONO_TYPE_R4: return MONO_NATIVE_R4;
3871 case MONO_TYPE_R8: return MONO_NATIVE_R8;
3872 case MONO_TYPE_STRING:
3874 switch (mspec->native) {
3875 case MONO_NATIVE_BSTR:
3876 *conv = MONO_MARSHAL_CONV_STR_BSTR;
3877 return MONO_NATIVE_BSTR;
3878 case MONO_NATIVE_LPSTR:
3879 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
3880 return MONO_NATIVE_LPSTR;
3881 case MONO_NATIVE_LPWSTR:
3882 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
3883 return MONO_NATIVE_LPWSTR;
3884 case MONO_NATIVE_LPTSTR:
3885 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3886 return MONO_NATIVE_LPTSTR;
3887 case MONO_NATIVE_ANSIBSTR:
3888 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
3889 return MONO_NATIVE_ANSIBSTR;
3890 case MONO_NATIVE_TBSTR:
3891 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
3892 return MONO_NATIVE_TBSTR;
3893 case MONO_NATIVE_BYVALTSTR:
3895 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
3897 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
3898 return MONO_NATIVE_BYVALTSTR;
3900 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);
3903 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3904 return MONO_NATIVE_LPTSTR;
3905 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
3906 case MONO_TYPE_VALUETYPE: /*FIXME*/
3907 if (type->data.klass->enumtype) {
3908 t = type->data.klass->enum_basetype->type;
3911 return MONO_NATIVE_STRUCT;
3912 case MONO_TYPE_SZARRAY:
3913 case MONO_TYPE_ARRAY:
3915 switch (mspec->native) {
3916 case MONO_NATIVE_BYVALARRAY:
3917 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
3918 return MONO_NATIVE_BYVALARRAY;
3919 case MONO_NATIVE_SAFEARRAY:
3920 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
3921 return MONO_NATIVE_SAFEARRAY;
3922 case MONO_NATIVE_LPARRAY:
3923 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3924 return MONO_NATIVE_LPARRAY;
3926 g_error ("cant marshal array as native type %02x", mspec->native);
3930 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3931 return MONO_NATIVE_LPARRAY;
3932 case MONO_TYPE_I: return MONO_NATIVE_INT;
3933 case MONO_TYPE_U: return MONO_NATIVE_UINT;
3934 case MONO_TYPE_CLASS:
3935 case MONO_TYPE_OBJECT: {
3936 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
3938 switch (mspec->native) {
3939 case MONO_NATIVE_STRUCT:
3940 return MONO_NATIVE_STRUCT;
3941 case MONO_NATIVE_INTERFACE:
3942 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
3943 return MONO_NATIVE_INTERFACE;
3944 case MONO_NATIVE_IDISPATCH:
3945 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
3946 return MONO_NATIVE_IDISPATCH;
3947 case MONO_NATIVE_IUNKNOWN:
3948 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
3949 return MONO_NATIVE_IUNKNOWN;
3950 case MONO_NATIVE_FUNC:
3951 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3952 type->data.klass == mono_defaults.delegate_class ||
3953 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3954 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3955 return MONO_NATIVE_FUNC;
3959 g_error ("cant marshal object as native type %02x", mspec->native);
3962 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3963 type->data.klass == mono_defaults.delegate_class ||
3964 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3965 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3966 return MONO_NATIVE_FUNC;
3968 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
3969 return MONO_NATIVE_STRUCT;
3971 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
3972 case MONO_TYPE_GENERICINST:
3973 type = &type->data.generic_class->container_class->byval_arg;
3976 case MONO_TYPE_TYPEDBYREF:
3978 g_error ("type 0x%02x not handled in marshal", t);
3980 return MONO_NATIVE_MAX;
3984 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
3987 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
3993 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
3994 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
3996 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3999 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
4003 method_from_method_def_or_ref (MonoImage *m, guint32 tok, MonoGenericContext *context)
4005 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
4006 switch (tok & MONO_METHODDEFORREF_MASK) {
4007 case MONO_METHODDEFORREF_METHODDEF:
4008 return mono_get_method_full (m, MONO_TOKEN_METHOD_DEF | idx, NULL, context);
4009 case MONO_METHODDEFORREF_METHODREF:
4010 return mono_get_method_full (m, MONO_TOKEN_MEMBER_REF | idx, NULL, context);
4012 g_assert_not_reached ();
4017 * mono_class_get_overrides_full:
4019 * Return the method overrides belonging to class @type_token in @overrides, and
4020 * the number of overrides in @num_overrides.
4022 * Returns: TRUE on success, FALSE on failure.
4025 mono_class_get_overrides_full (MonoImage *image, guint32 type_token, MonoMethod ***overrides, gint32 *num_overrides,
4026 MonoGenericContext *generic_context)
4029 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
4032 guint32 cols [MONO_METHODIMPL_SIZE];
4033 MonoMethod **result;
4043 loc.col_idx = MONO_METHODIMPL_CLASS;
4044 loc.idx = mono_metadata_token_index (type_token);
4046 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4052 * We may end up in the middle of the rows...
4055 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
4060 while (end < tdef->rows) {
4061 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
4067 result = g_new (MonoMethod*, num * 2);
4068 for (i = 0; i < num; ++i) {
4069 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
4070 result [i * 2] = method_from_method_def_or_ref (
4071 image, cols [MONO_METHODIMPL_DECLARATION], generic_context);
4072 result [i * 2 + 1] = method_from_method_def_or_ref (
4073 image, cols [MONO_METHODIMPL_BODY], generic_context);
4076 *overrides = result;
4078 *num_overrides = num;
4083 * mono_guid_to_string:
4085 * Converts a 16 byte Microsoft GUID to the standard string representation.
4088 mono_guid_to_string (const guint8 *guid)
4090 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
4091 guid[3], guid[2], guid[1], guid[0],
4095 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
4099 get_constraints (MonoImage *image, int owner, MonoClass ***constraints, MonoGenericContainer *container)
4101 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
4102 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
4103 guint32 i, token, found;
4104 MonoClass *klass, **res;
4105 GList *cons = NULL, *tmp;
4106 MonoGenericContext *context = &container->context;
4108 /* FIXME: !container->klass => this is probably monodis */
4109 g_assert (!container->klass || context->gclass || context->gmethod);
4111 *constraints = NULL;
4113 for (i = 0; i < tdef->rows; ++i) {
4114 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
4115 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
4116 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
4117 klass = mono_class_get_full (image, token, context);
4118 cons = g_list_append (cons, klass);
4121 /* contiguous list finished */
4128 res = g_new0 (MonoClass*, found + 1);
4129 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
4130 res [i] = tmp->data;
4138 * mono_metadata_get_generic_param_row:
4141 * @token: TypeOrMethodDef token, owner for GenericParam
4142 * @owner: coded token, set on return
4144 * Returns: 1-based row-id in the GenericParam table whose
4145 * owner is @token. 0 if not found.
4148 mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner)
4150 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
4151 guint32 cols [MONO_GENERICPARAM_SIZE];
4158 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
4159 *owner = MONO_TYPEORMETHOD_TYPE;
4160 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
4161 *owner = MONO_TYPEORMETHOD_METHOD;
4163 g_error ("wrong token %x to get_generic_param_row", token);
4166 *owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
4168 for (i = 0; i < tdef->rows; ++i) {
4169 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
4170 if (cols [MONO_GENERICPARAM_OWNER] == *owner)
4178 mono_metadata_has_generic_params (MonoImage *image, guint32 token)
4181 return mono_metadata_get_generic_param_row (image, token, &owner);
4185 * mono_metadata_load_generic_param_constraints:
4187 * Load the generic parameter constraints for the newly created generic type or method
4188 * represented by @token and @container. The @container is the new container which has
4189 * been returned by a call to mono_metadata_load_generic_params() with this @token.
4192 mono_metadata_load_generic_param_constraints (MonoImage *image, guint32 token,
4193 MonoGenericContainer *container)
4195 guint32 start_row, i, owner;
4196 if (! (start_row = mono_metadata_get_generic_param_row (image, token, &owner)))
4198 for (i = 0; i < container->type_argc; i++)
4199 get_constraints (image, start_row + i, &container->type_params [i].constraints, container);
4203 * mono_metadata_load_generic_params:
4205 * Load the type parameters from the type or method definition @token.
4207 * Use this method after parsing a type or method definition to figure out whether it's a generic
4208 * type / method. When parsing a method definition, @parent_container points to the generic container
4209 * of the current class, if any.
4211 * Note: This method does not load the constraints: for typedefs, this has to be done after fully
4212 * creating the type.
4214 * Returns: NULL if @token is not a generic type or method definition or the new generic container.
4217 MonoGenericContainer *
4218 mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericContainer *parent_container)
4220 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
4221 guint32 cols [MONO_GENERICPARAM_SIZE];
4222 guint32 i, owner = 0, n;
4223 MonoGenericContainer *container;
4224 MonoGenericParam *params;
4226 if (!(i = mono_metadata_get_generic_param_row (image, token, &owner)))
4228 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
4231 container = g_new0 (MonoGenericContainer, 1);
4234 params = g_realloc (params, sizeof (MonoGenericParam) * n);
4235 params [n - 1].owner = container;
4236 params [n - 1].pklass = NULL;
4237 params [n - 1].method = NULL;
4238 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
4239 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
4240 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
4241 params [n - 1].constraints = NULL;
4242 if (++i > tdef->rows)
4244 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
4245 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
4247 container->type_argc = n;
4248 container->type_params = params;
4249 container->parent = parent_container;
4251 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
4252 container->is_method = 1;
4254 container->context.container = container;
4260 mono_type_is_byref (MonoType *type)
4266 mono_type_get_type (MonoType *type)
4271 /* For MONO_TYPE_FNPTR */
4272 MonoMethodSignature*
4273 mono_type_get_signature (MonoType *type)
4275 return type->data.method;
4278 /* For MONO_TYPE_CLASS, VALUETYPE */
4280 mono_type_get_class (MonoType *type)
4282 return type->data.klass;
4285 /* For MONO_TYPE_ARRAY */
4287 mono_type_get_array_type (MonoType *type)
4289 return type->data.array;
4293 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
4295 /* FIXME: implement */
4300 mono_signature_get_return_type (MonoMethodSignature *sig)
4306 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
4312 /* start from the first */
4313 if (sig->param_count) {
4314 *iter = &sig->params [0];
4315 return sig->params [0];
4323 if (type < &sig->params [sig->param_count]) {
4331 mono_signature_get_param_count (MonoMethodSignature *sig)
4333 return sig->param_count;
4337 mono_signature_get_call_conv (MonoMethodSignature *sig)
4339 return sig->call_convention;
4343 mono_signature_vararg_start (MonoMethodSignature *sig)
4345 return sig->sentinelpos;
4349 mono_signature_is_instance (MonoMethodSignature *sig)
4351 return sig->hasthis;
4355 mono_signature_explicit_this (MonoMethodSignature *sig)
4357 return sig->explicit_this;
4360 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
4362 mono_aligned_addr_hash (gconstpointer ptr)
4364 return GPOINTER_TO_UINT (ptr) >> 3;