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"
26 static void do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContext *generic_context,
27 const char *ptr, const char **rptr);
29 static gboolean do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only);
30 static gboolean mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only);
31 static gboolean _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2,
32 gboolean signature_only);
34 const static unsigned char AssemblySchema [] = {
35 MONO_MT_UINT32, /* "HashId" }, */
36 MONO_MT_UINT16, /* "Major" }, */
37 MONO_MT_UINT16, /* "Minor" }, */
38 MONO_MT_UINT16, /* "BuildNumber" }, */
39 MONO_MT_UINT16, /* "RevisionNumber" }, */
40 MONO_MT_UINT32, /* "Flags" }, */
41 MONO_MT_BLOB_IDX, /* "PublicKey" }, */
42 MONO_MT_STRING_IDX, /* "Name" }, */
43 MONO_MT_STRING_IDX, /* "Culture" }, */
47 const static unsigned char AssemblyOSSchema [] = {
48 MONO_MT_UINT32, /* "OSPlatformID" }, */
49 MONO_MT_UINT32, /* "OSMajor" }, */
50 MONO_MT_UINT32, /* "OSMinor" }, */
54 const static unsigned char AssemblyProcessorSchema [] = {
55 MONO_MT_UINT32, /* "Processor" }, */
59 const static unsigned char AssemblyRefSchema [] = {
60 MONO_MT_UINT16, /* "Major" }, */
61 MONO_MT_UINT16, /* "Minor" }, */
62 MONO_MT_UINT16, /* "Build" }, */
63 MONO_MT_UINT16, /* "Revision" }, */
64 MONO_MT_UINT32, /* "Flags" }, */
65 MONO_MT_BLOB_IDX, /* "PublicKeyOrToken" }, */
66 MONO_MT_STRING_IDX, /* "Name" }, */
67 MONO_MT_STRING_IDX, /* "Culture" }, */
68 MONO_MT_BLOB_IDX, /* "HashValue" }, */
72 const static unsigned char AssemblyRefOSSchema [] = {
73 MONO_MT_UINT32, /* "OSPlatformID" }, */
74 MONO_MT_UINT32, /* "OSMajorVersion" }, */
75 MONO_MT_UINT32, /* "OSMinorVersion" }, */
76 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
80 const static unsigned char AssemblyRefProcessorSchema [] = {
81 MONO_MT_UINT32, /* "Processor" }, */
82 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
86 const static unsigned char ClassLayoutSchema [] = {
87 MONO_MT_UINT16, /* "PackingSize" }, */
88 MONO_MT_UINT32, /* "ClassSize" }, */
89 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
93 const static unsigned char ConstantSchema [] = {
94 MONO_MT_UINT8, /* "Type" }, */
95 MONO_MT_UINT8, /* "PaddingZero" }, */
96 MONO_MT_CONST_IDX, /* "Parent" }, */
97 MONO_MT_BLOB_IDX, /* "Value" }, */
101 const static unsigned char CustomAttributeSchema [] = {
102 MONO_MT_HASCAT_IDX, /* "Parent" }, */
103 MONO_MT_CAT_IDX, /* "Type" }, */
104 MONO_MT_BLOB_IDX, /* "Value" }, */
108 const static unsigned char DeclSecuritySchema [] = {
109 MONO_MT_UINT16, /* "Action" }, */
110 MONO_MT_HASDEC_IDX, /* "Parent" }, */
111 MONO_MT_BLOB_IDX, /* "PermissionSet" }, */
115 const static unsigned char EventMapSchema [] = {
116 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
117 MONO_MT_TABLE_IDX, /* "EventList:Event" }, */
121 const static unsigned char EventSchema [] = {
122 MONO_MT_UINT16, /* "EventFlags#EventAttribute" }, */
123 MONO_MT_STRING_IDX, /* "Name" }, */
124 MONO_MT_TABLE_IDX, /* "EventType" }, TypeDef or TypeRef */
128 const static unsigned char ExportedTypeSchema [] = {
129 MONO_MT_UINT32, /* "Flags" }, */
130 MONO_MT_TABLE_IDX, /* "TypeDefId" }, */
131 MONO_MT_STRING_IDX, /* "TypeName" }, */
132 MONO_MT_STRING_IDX, /* "TypeNameSpace" }, */
133 MONO_MT_IMPL_IDX, /* "Implementation" }, */
137 const static unsigned char FieldSchema [] = {
138 MONO_MT_UINT16, /* "Flags" }, */
139 MONO_MT_STRING_IDX, /* "Name" }, */
140 MONO_MT_BLOB_IDX, /* "Signature" }, */
143 const static unsigned char FieldLayoutSchema [] = {
144 MONO_MT_UINT32, /* "Offset" }, */
145 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
149 const static unsigned char FieldMarshalSchema [] = {
150 MONO_MT_HFM_IDX, /* "Parent" }, */
151 MONO_MT_BLOB_IDX, /* "NativeType" }, */
154 const static unsigned char FieldRVASchema [] = {
155 MONO_MT_UINT32, /* "RVA" }, */
156 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
160 const static unsigned char FileSchema [] = {
161 MONO_MT_UINT32, /* "Flags" }, */
162 MONO_MT_STRING_IDX, /* "Name" }, */
163 MONO_MT_BLOB_IDX, /* "Value" }, */
167 const static unsigned char ImplMapSchema [] = {
168 MONO_MT_UINT16, /* "MappingFlag" }, */
169 MONO_MT_MF_IDX, /* "MemberForwarded" }, */
170 MONO_MT_STRING_IDX, /* "ImportName" }, */
171 MONO_MT_TABLE_IDX, /* "ImportScope:ModuleRef" }, */
175 const static unsigned char InterfaceImplSchema [] = {
176 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
177 MONO_MT_TDOR_IDX, /* "Interface=TypeDefOrRef" }, */
181 const static unsigned char ManifestResourceSchema [] = {
182 MONO_MT_UINT32, /* "Offset" }, */
183 MONO_MT_UINT32, /* "Flags" }, */
184 MONO_MT_STRING_IDX, /* "Name" }, */
185 MONO_MT_IMPL_IDX, /* "Implementation" }, */
189 const static unsigned char MemberRefSchema [] = {
190 MONO_MT_MRP_IDX, /* "Class" }, */
191 MONO_MT_STRING_IDX, /* "Name" }, */
192 MONO_MT_BLOB_IDX, /* "Signature" }, */
196 const static unsigned char MethodSchema [] = {
197 MONO_MT_UINT32, /* "RVA" }, */
198 MONO_MT_UINT16, /* "ImplFlags#MethodImplAttributes" }, */
199 MONO_MT_UINT16, /* "Flags#MethodAttribute" }, */
200 MONO_MT_STRING_IDX, /* "Name" }, */
201 MONO_MT_BLOB_IDX, /* "Signature" }, */
202 MONO_MT_TABLE_IDX, /* "ParamList:Param" }, */
206 const static unsigned char MethodImplSchema [] = {
207 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
208 MONO_MT_MDOR_IDX, /* "MethodBody" }, */
209 MONO_MT_MDOR_IDX, /* "MethodDeclaration" }, */
213 const static unsigned char MethodSemanticsSchema [] = {
214 MONO_MT_UINT16, /* "MethodSemantic" }, */
215 MONO_MT_TABLE_IDX, /* "Method:Method" }, */
216 MONO_MT_HS_IDX, /* "Association" }, */
220 const static unsigned char ModuleSchema [] = {
221 MONO_MT_UINT16, /* "Generation" }, */
222 MONO_MT_STRING_IDX, /* "Name" }, */
223 MONO_MT_GUID_IDX, /* "MVID" }, */
224 MONO_MT_GUID_IDX, /* "EncID" }, */
225 MONO_MT_GUID_IDX, /* "EncBaseID" }, */
229 const static unsigned char ModuleRefSchema [] = {
230 MONO_MT_STRING_IDX, /* "Name" }, */
234 const static unsigned char NestedClassSchema [] = {
235 MONO_MT_TABLE_IDX, /* "NestedClass:TypeDef" }, */
236 MONO_MT_TABLE_IDX, /* "EnclosingClass:TypeDef" }, */
240 const static unsigned char ParamSchema [] = {
241 MONO_MT_UINT16, /* "Flags" }, */
242 MONO_MT_UINT16, /* "Sequence" }, */
243 MONO_MT_STRING_IDX, /* "Name" }, */
247 const static unsigned char PropertySchema [] = {
248 MONO_MT_UINT16, /* "Flags" }, */
249 MONO_MT_STRING_IDX, /* "Name" }, */
250 MONO_MT_BLOB_IDX, /* "Type" }, */
254 const static unsigned char PropertyMapSchema [] = {
255 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
256 MONO_MT_TABLE_IDX, /* "PropertyList:Property" }, */
260 const static unsigned char StandaloneSigSchema [] = {
261 MONO_MT_BLOB_IDX, /* "Signature" }, */
265 const static unsigned char TypeDefSchema [] = {
266 MONO_MT_UINT32, /* "Flags" }, */
267 MONO_MT_STRING_IDX, /* "Name" }, */
268 MONO_MT_STRING_IDX, /* "Namespace" }, */
269 MONO_MT_TDOR_IDX, /* "Extends" }, */
270 MONO_MT_TABLE_IDX, /* "FieldList:Field" }, */
271 MONO_MT_TABLE_IDX, /* "MethodList:Method" }, */
275 const static unsigned char TypeRefSchema [] = {
276 MONO_MT_RS_IDX, /* "ResolutionScope=ResolutionScope" }, */
277 MONO_MT_STRING_IDX, /* "Name" }, */
278 MONO_MT_STRING_IDX, /* "Namespace" }, */
282 const static unsigned char TypeSpecSchema [] = {
283 MONO_MT_BLOB_IDX, /* "Signature" }, */
287 const static unsigned char GenericParamSchema [] = {
288 MONO_MT_UINT16, /* "Number" }, */
289 MONO_MT_UINT16, /* "Flags" }, */
290 MONO_MT_TABLE_IDX, /* "Owner" }, TypeDef or MethodDef */
291 MONO_MT_STRING_IDX, /* "Name" }, */
293 /* soon to be removed */
294 MONO_MT_TABLE_IDX, /* "Kind" }, */
299 const static unsigned char MethodSpecSchema [] = {
300 MONO_MT_MDOR_IDX, /* "Method" }, */
301 MONO_MT_BLOB_IDX, /* "Signature" }, */
305 const static unsigned char GenericParamConstraintSchema [] = {
306 MONO_MT_TABLE_IDX, /* "GenericParam" }, */
307 MONO_MT_TDOR_IDX, /* "Constraint" }, */
311 const static struct {
312 const unsigned char *description;
315 /* 0 */ { ModuleSchema, "Module" },
316 /* 1 */ { TypeRefSchema, "TypeRef" },
317 /* 2 */ { TypeDefSchema, "TypeDef" },
318 /* 3 */ { NULL, NULL },
319 /* 4 */ { FieldSchema, "Field" },
320 /* 5 */ { NULL, NULL },
321 /* 6 */ { MethodSchema, "Method" },
322 /* 7 */ { NULL, NULL },
323 /* 8 */ { ParamSchema, "Param" },
324 /* 9 */ { InterfaceImplSchema, "InterfaceImpl" },
325 /* A */ { MemberRefSchema, "MemberRef" },
326 /* B */ { ConstantSchema, "Constant" },
327 /* C */ { CustomAttributeSchema, "CustomAttribute" },
328 /* D */ { FieldMarshalSchema, "FieldMarshal" },
329 /* E */ { DeclSecuritySchema, "DeclSecurity" },
330 /* F */ { ClassLayoutSchema, "ClassLayout" },
331 /* 10 */ { FieldLayoutSchema, "FieldLayout" },
332 /* 11 */ { StandaloneSigSchema, "StandaloneSig" },
333 /* 12 */ { EventMapSchema, "EventMap" },
334 /* 13 */ { NULL, NULL },
335 /* 14 */ { EventSchema, "Event" },
336 /* 15 */ { PropertyMapSchema, "PropertyMap" },
337 /* 16 */ { NULL, NULL },
338 /* 17 */ { PropertySchema, "PropertyTable" },
339 /* 18 */ { MethodSemanticsSchema, "MethodSemantics" },
340 /* 19 */ { MethodImplSchema, "MethodImpl" },
341 /* 1A */ { ModuleRefSchema, "ModuleRef" },
342 /* 1B */ { TypeSpecSchema, "TypeSpec" },
343 /* 1C */ { ImplMapSchema, "ImplMap" },
344 /* 1D */ { FieldRVASchema, "FieldRVA" },
345 /* 1E */ { NULL, NULL },
346 /* 1F */ { NULL, NULL },
347 /* 20 */ { AssemblySchema, "Assembly" },
348 /* 21 */ { AssemblyProcessorSchema, "AssemblyProcessor" },
349 /* 22 */ { AssemblyOSSchema, "AssemblyOS" },
350 /* 23 */ { AssemblyRefSchema, "AssemblyRef" },
351 /* 24 */ { AssemblyRefProcessorSchema, "AssemblyRefProcessor" },
352 /* 25 */ { AssemblyRefOSSchema, "AssemblyRefOS" },
353 /* 26 */ { FileSchema, "File" },
354 /* 27 */ { ExportedTypeSchema, "ExportedType" },
355 /* 28 */ { ManifestResourceSchema, "ManifestResource" },
356 /* 29 */ { NestedClassSchema, "NestedClass" },
357 /* 2A */ { GenericParamSchema, "GenericParam" },
358 /* 2B */ { MethodSpecSchema, "MethodSpec" },
359 /* 2C */ { GenericParamConstraintSchema, "GenericParamConstraint" },
363 * mono_meta_table_name:
364 * @table: table index
366 * Returns: the name for the @table index
369 mono_meta_table_name (int table)
371 if ((table < 0) || (table > 0x2c))
374 return tables [table].name;
377 /* The guy who wrote the spec for this should not be allowed near a
380 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
381 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
382 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
383 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
384 inverse of this mapping.
387 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
388 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
390 /* Reference: Partition II - 23.2.6 */
392 * mono_metadata_compute_size:
393 * @meta: metadata context
394 * @tableindex: metadata table number
395 * @result_bitfield: pointer to guint32 where to store additional info
397 * mono_metadata_compute_size() computes the lenght in bytes of a single
398 * row in a metadata table. The size of each column is encoded in the
399 * @result_bitfield return value along with the number of columns in the table.
400 * the resulting bitfield should be handed to the mono_metadata_table_size()
401 * and mono_metadata_table_count() macros.
404 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
406 guint32 bitfield = 0;
407 int size = 0, field_size = 0;
410 const unsigned char *description = tables [tableindex].description;
412 for (i = 0; (code = description [i]) != MONO_MT_END; i++){
415 field_size = 4; break;
418 field_size = 2; break;
421 field_size = 1; break;
423 case MONO_MT_BLOB_IDX:
424 field_size = meta->idx_blob_wide ? 4 : 2; break;
426 case MONO_MT_STRING_IDX:
427 field_size = meta->idx_string_wide ? 4 : 2; break;
429 case MONO_MT_GUID_IDX:
430 field_size = meta->idx_guid_wide ? 4 : 2; break;
432 case MONO_MT_TABLE_IDX:
433 /* Uhm, a table index can point to other tables besides the current one
434 * so, it's not correct to use the rowcount of the current table to
435 * get the size for this column - lupus
437 switch (tableindex) {
438 case MONO_TABLE_ASSEMBLYREFOS:
440 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
441 case MONO_TABLE_ASSEMBLYPROCESSOR:
443 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
444 case MONO_TABLE_CLASSLAYOUT:
446 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
447 case MONO_TABLE_EVENTMAP:
448 g_assert (i == 0 || i == 1);
449 field_size = i ? idx_size (MONO_TABLE_EVENT):
450 idx_size(MONO_TABLE_TYPEDEF);
452 case MONO_TABLE_EVENT:
454 field_size = MAX (idx_size (MONO_TABLE_TYPEDEF), idx_size(MONO_TABLE_TYPEREF));
455 field_size = MAX (field_size, idx_size(MONO_TABLE_TYPESPEC));
457 case MONO_TABLE_EXPORTEDTYPE:
459 /* the index is in another metadata file, so it must be 4 */
460 field_size = 4; break;
461 case MONO_TABLE_FIELDLAYOUT:
463 field_size = idx_size (MONO_TABLE_FIELD); break;
464 case MONO_TABLE_FIELDRVA:
466 field_size = idx_size (MONO_TABLE_FIELD); break;
467 case MONO_TABLE_IMPLMAP:
469 field_size = idx_size (MONO_TABLE_MODULEREF); break;
470 case MONO_TABLE_INTERFACEIMPL:
472 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
473 case MONO_TABLE_METHOD:
475 field_size = idx_size (MONO_TABLE_PARAM); break;
476 case MONO_TABLE_METHODIMPL:
478 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
479 case MONO_TABLE_METHODSEMANTICS:
481 field_size = idx_size (MONO_TABLE_METHOD); break;
482 case MONO_TABLE_NESTEDCLASS:
483 g_assert (i == 0 || i == 1);
484 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
485 case MONO_TABLE_PROPERTYMAP:
486 g_assert (i == 0 || i == 1);
487 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
488 idx_size(MONO_TABLE_TYPEDEF);
490 case MONO_TABLE_TYPEDEF:
491 g_assert (i == 4 || i == 5);
492 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
493 idx_size(MONO_TABLE_METHOD);
494 case MONO_TABLE_GENERICPARAM:
495 g_assert (i == 2 || i == 4 || i == 5);
497 field_size = MAX (idx_size (MONO_TABLE_METHOD), idx_size (MONO_TABLE_TYPEDEF));
499 field_size = idx_size (MONO_TABLE_TYPEDEF);
501 field_size = idx_size (MONO_TABLE_TYPEDEF);
504 case MONO_TABLE_GENERICPARAMCONSTRAINT:
506 field_size = idx_size (MONO_TABLE_GENERICPARAM);
510 g_assert_not_reached ();
512 if (tableindex != MONO_TABLE_EXPORTEDTYPE && field_size != idx_size (tableindex))
513 g_warning ("size changed (%d to %d)", idx_size (tableindex), field_size);
518 * HasConstant: ParamDef, FieldDef, Property
520 case MONO_MT_CONST_IDX:
521 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
522 meta->tables [MONO_TABLE_FIELD].rows);
523 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
525 /* 2 bits to encode tag */
526 field_size = rtsize (n, 16-2);
530 * HasCustomAttribute: points to any table but
533 case MONO_MT_HASCAT_IDX:
535 * We believe that since the signature and
536 * permission are indexing the Blob heap,
537 * we should consider the blob size first
539 /* I'm not a believer - lupus
540 if (meta->idx_blob_wide){
545 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
546 meta->tables [MONO_TABLE_FIELD].rows);
547 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
548 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
549 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
550 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
551 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
552 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
553 n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
554 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
555 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
556 n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
557 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
558 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
559 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
560 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
561 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
562 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
563 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
565 /* 5 bits to encode */
566 field_size = rtsize (n, 16-5);
570 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
571 * MemberRef and String.
573 case MONO_MT_CAT_IDX:
574 /* String is a heap, if it is wide, we know the size */
576 if (meta->idx_string_wide){
581 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
582 meta->tables [MONO_TABLE_TYPEDEF].rows);
583 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
584 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
586 /* 3 bits to encode */
587 field_size = rtsize (n, 16-3);
591 * HasDeclSecurity: Typedef, MethodDef, Assembly
593 case MONO_MT_HASDEC_IDX:
594 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
595 meta->tables [MONO_TABLE_METHOD].rows);
596 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
598 /* 2 bits to encode */
599 field_size = rtsize (n, 16-2);
603 * Implementation: File, AssemblyRef, ExportedType
605 case MONO_MT_IMPL_IDX:
606 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
607 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
608 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
610 /* 2 bits to encode tag */
611 field_size = rtsize (n, 16-2);
615 * HasFieldMarshall: FieldDef, ParamDef
617 case MONO_MT_HFM_IDX:
618 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
619 meta->tables [MONO_TABLE_PARAM].rows);
621 /* 1 bit used to encode tag */
622 field_size = rtsize (n, 16-1);
626 * MemberForwarded: FieldDef, MethodDef
629 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
630 meta->tables [MONO_TABLE_METHOD].rows);
632 /* 1 bit used to encode tag */
633 field_size = rtsize (n, 16-1);
637 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
639 * It is TypeDef, _TypeRef_, TypeSpec, instead.
641 case MONO_MT_TDOR_IDX:
642 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
643 meta->tables [MONO_TABLE_TYPEREF].rows);
644 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
646 /* 2 bits to encode */
647 field_size = rtsize (n, 16-2);
651 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
653 case MONO_MT_MRP_IDX:
654 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
655 meta->tables [MONO_TABLE_TYPEREF].rows);
656 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
657 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
658 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
659 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
661 /* 3 bits to encode */
662 field_size = rtsize (n, 16 - 3);
666 * MethodDefOrRef: MethodDef, MemberRef
668 case MONO_MT_MDOR_IDX:
669 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
670 meta->tables [MONO_TABLE_MEMBERREF].rows);
672 /* 1 bit used to encode tag */
673 field_size = rtsize (n, 16-1);
677 * HasSemantics: Property, Event
680 n = MAX (meta->tables [MONO_TABLE_PROPERTY].rows,
681 meta->tables [MONO_TABLE_EVENT].rows);
683 /* 1 bit used to encode tag */
684 field_size = rtsize (n, 16-1);
688 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
691 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
692 meta->tables [MONO_TABLE_MODULEREF].rows);
693 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
694 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
696 /* 2 bits used to encode tag (ECMA spec claims 3) */
697 field_size = rtsize (n, 16 - 2);
702 * encode field size as follows (we just need to
709 bitfield |= (field_size-1) << shift;
712 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
715 *result_bitfield = (i << 24) | bitfield;
720 * mono_metadata_compute_table_bases:
721 * @meta: metadata context to compute table values
723 * Computes the table bases for the metadata structure.
724 * This is an internal function used by the image loader code.
727 mono_metadata_compute_table_bases (MonoImage *meta)
730 const char *base = meta->tables_base;
732 for (i = 0; i < MONO_TABLE_NUM; i++){
733 if (meta->tables [i].rows == 0)
736 meta->tables [i].row_size = mono_metadata_compute_size (
737 meta, i, &meta->tables [i].size_bitfield);
738 meta->tables [i].base = base;
739 base += meta->tables [i].rows * meta->tables [i].row_size;
744 * mono_metadata_locate:
745 * @meta: metadata context
746 * @table: table code.
747 * @idx: index of element to retrieve from @table.
749 * Returns: a pointer to the @idx element in the metadata table
750 * whose code is @table.
753 mono_metadata_locate (MonoImage *meta, int table, int idx)
755 /* idx == 0 refers always to NULL */
756 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
758 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
762 * mono_metadata_locate_token:
763 * @meta: metadata context
764 * @token: metadata token
766 * Returns: a pointer to the data in the metadata represented by the
770 mono_metadata_locate_token (MonoImage *meta, guint32 token)
772 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
776 * mono_metadata_string_heap:
777 * @meta: metadata context
778 * @index: index into the string heap.
780 * Returns: an in-memory pointer to the @index in the string heap.
783 mono_metadata_string_heap (MonoImage *meta, guint32 index)
785 g_return_val_if_fail (index < meta->heap_strings.size, "");
786 return meta->heap_strings.data + index;
790 * mono_metadata_user_string:
791 * @meta: metadata context
792 * @index: index into the user string heap.
794 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
797 mono_metadata_user_string (MonoImage *meta, guint32 index)
799 g_return_val_if_fail (index < meta->heap_us.size, "");
800 return meta->heap_us.data + index;
804 * mono_metadata_blob_heap:
805 * @meta: metadata context
806 * @index: index into the blob.
808 * Returns: an in-memory pointer to the @index in the Blob heap.
811 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
813 g_return_val_if_fail (index < meta->heap_blob.size, "");
814 return meta->heap_blob.data + index;
818 * mono_metadata_guid_heap:
819 * @meta: metadata context
820 * @index: index into the guid heap.
822 * Returns: an in-memory pointer to the @index in the guid heap.
825 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
828 index *= 16; /* adjust for guid size and 1-based index */
829 g_return_val_if_fail (index < meta->heap_guid.size, "");
830 return meta->heap_guid.data + index;
834 dword_align (const char *ptr)
836 #if SIZEOF_VOID_P == 8
837 return (const char *) (((guint64) (ptr + 3)) & ~3);
839 return (const char *) (((guint32) (ptr + 3)) & ~3);
844 * mono_metadata_decode_row:
845 * @t: table to extract information from.
846 * @idx: index in table.
847 * @res: array of @res_size cols to store the results in
849 * This decompresses the metadata element @idx in table @t
850 * into the guint32 @res array that has res_size elements
853 mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res_size)
855 guint32 bitfield = t->size_bitfield;
856 int i, count = mono_metadata_table_count (bitfield);
857 const char *data = t->base + idx * t->row_size;
859 g_assert (res_size == count);
861 for (i = 0; i < count; i++){
862 int n = mono_metadata_table_size (bitfield, i);
866 res [i] = *data; break;
868 res [i] = read16 (data); break;
871 res [i] = read32 (data); break;
874 g_assert_not_reached ();
881 * mono_metadata_decode_row_col:
882 * @t: table to extract information from.
883 * @idx: index for row in table.
884 * @col: column in the row.
886 * This function returns the value of column @col from the @idx
887 * row in the table @t.
890 mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col)
892 guint32 bitfield = t->size_bitfield;
894 register const char *data = t->base + idx * t->row_size;
897 g_assert (col < mono_metadata_table_count (bitfield));
899 n = mono_metadata_table_size (bitfield, 0);
900 for (i = 0; i < col; ++i) {
902 n = mono_metadata_table_size (bitfield, i + 1);
908 return read16 (data);
910 return read32 (data);
912 g_assert_not_reached ();
917 * mono_metadata_decode_blob_size:
918 * @ptr: pointer to a blob object
919 * @rptr: the new position of the pointer
921 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
923 * Returns: the size of the blob object
926 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
928 const unsigned char *ptr = (const unsigned char *)xptr;
931 if ((*ptr & 0x80) == 0){
932 size = ptr [0] & 0x7f;
934 } else if ((*ptr & 0x40) == 0){
935 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
938 size = ((ptr [0] & 0x1f) << 24) +
951 * mono_metadata_decode_value:
952 * @ptr: pointer to decode from
953 * @rptr: the new position of the pointer
955 * This routine decompresses 32-bit values as specified in the "Blob and
956 * Signature" section (22.2)
958 * Returns: the decoded value
961 mono_metadata_decode_value (const char *_ptr, const char **rptr)
963 const unsigned char *ptr = (const unsigned char *) _ptr;
964 unsigned char b = *ptr;
967 if ((b & 0x80) == 0){
970 } else if ((b & 0x40) == 0){
971 len = ((b & 0x3f) << 8 | ptr [1]);
974 len = ((b & 0x1f) << 24) |
987 * mono_metadata_parse_typedef_or_ref:
988 * @m: a metadata context.
989 * @ptr: a pointer to an encoded TypedefOrRef in @m
990 * @rptr: pointer updated to match the end of the decoded stream
992 * Returns: a token valid in the @m metadata decoded from
993 * the compressed representation.
996 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
999 token = mono_metadata_decode_value (ptr, &ptr);
1002 return mono_metadata_token_from_dor (token);
1006 * mono_metadata_parse_custom_mod:
1007 * @m: a metadata context.
1008 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1009 * @ptr: a pointer to (possibly) the start of a custom modifier list
1010 * @rptr: pointer updated to match the end of the decoded stream
1012 * Checks if @ptr points to a type custom modifier compressed representation.
1014 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1017 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1019 MonoCustomMod local;
1020 if ((*ptr == MONO_TYPE_CMOD_OPT) ||
1021 (*ptr == MONO_TYPE_CMOD_REQD)) {
1024 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1025 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1032 * mono_metadata_parse_array:
1033 * @m: a metadata context.
1034 * @ptr: a pointer to an encoded array description.
1035 * @rptr: pointer updated to match the end of the decoded stream
1037 * Decodes the compressed array description found in the metadata @m at @ptr.
1039 * Returns: a #MonoArrayType structure describing the array type
1043 mono_metadata_parse_array_full (MonoImage *m, MonoGenericContext *generic_context,
1044 const char *ptr, const char **rptr)
1047 MonoArrayType *array = g_new0 (MonoArrayType, 1);
1050 etype = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_TYPE, 0, ptr, &ptr);
1051 array->eklass = mono_class_from_mono_type (etype);
1052 array->rank = mono_metadata_decode_value (ptr, &ptr);
1054 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1055 if (array->numsizes)
1056 array->sizes = g_new0 (int, array->numsizes);
1057 for (i = 0; i < array->numsizes; ++i)
1058 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1060 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1061 if (array->numlobounds)
1062 array->lobounds = g_new0 (int, array->numlobounds);
1063 for (i = 0; i < array->numlobounds; ++i)
1064 array->lobounds [i] = mono_metadata_decode_value (ptr, &ptr);
1072 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1074 return mono_metadata_parse_array_full (m, NULL, ptr, rptr);
1078 * mono_metadata_free_array:
1079 * @array: array description
1081 * Frees the array description returned from mono_metadata_parse_array().
1084 mono_metadata_free_array (MonoArrayType *array)
1086 g_free (array->sizes);
1087 g_free (array->lobounds);
1092 * need to add common field and param attributes combinations:
1095 * public static literal
1098 * private static literal
1100 static const MonoType
1102 /* data, attrs, type, nmods, byref, pinned */
1103 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1104 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1105 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1106 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1107 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1108 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1109 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1110 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1111 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1112 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1113 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1114 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1115 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1116 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1117 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1118 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1119 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1120 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1121 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1122 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1123 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1124 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1125 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1126 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1127 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1128 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1129 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1130 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 0, 0},
1131 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 1, 0},
1132 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1133 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1134 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1135 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1136 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1139 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1141 static GHashTable *type_cache = NULL;
1142 static GHashTable *generic_inst_cache = NULL;
1143 static GHashTable *generic_class_cache = NULL;
1144 static int next_generic_inst_id = 0;
1147 * MonoTypes with modifies are never cached, so we never check or use that field.
1150 mono_type_hash (gconstpointer data)
1152 const MonoType *type = (const MonoType *) data;
1153 return type->type | (type->byref << 8) | (type->attrs << 9);
1157 mono_type_equal (gconstpointer ka, gconstpointer kb)
1159 const MonoType *a = (const MonoType *) ka;
1160 const MonoType *b = (const MonoType *) kb;
1162 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1164 /* need other checks */
1169 mono_generic_inst_hash (gconstpointer data)
1171 const MonoGenericInst *ginst = (const MonoGenericInst *) data;
1172 return ginst->type_argc | (ginst->is_open << 8);
1176 mono_generic_inst_equal (gconstpointer ka, gconstpointer kb)
1178 const MonoGenericInst *a = (const MonoGenericInst *) ka;
1179 const MonoGenericInst *b = (const MonoGenericInst *) kb;
1182 if ((a->is_open != b->is_open) || (a->type_argc != b->type_argc) || (a->is_reference != b->is_reference))
1184 for (i = 0; i < a->type_argc; ++i) {
1185 if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], FALSE))
1192 mono_generic_class_hash (gconstpointer data)
1194 const MonoGenericClass *gclass = (const MonoGenericClass *) data;
1195 return mono_metadata_type_hash (&gclass->container_class->byval_arg);
1199 mono_generic_class_equal (gconstpointer ka, gconstpointer kb)
1201 const MonoGenericClass *a = (const MonoGenericClass *) ka;
1202 const MonoGenericClass *b = (const MonoGenericClass *) kb;
1204 return _mono_metadata_generic_class_equal (a, b, FALSE);
1208 * mono_metadata_init:
1210 * Initialize the global variables of this module.
1213 mono_metadata_init (void)
1217 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1218 generic_inst_cache = g_hash_table_new (mono_generic_inst_hash, mono_generic_inst_equal);
1219 generic_class_cache = g_hash_table_new (mono_generic_class_hash, mono_generic_class_equal);
1221 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1222 g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
1226 * mono_metadata_parse_type:
1227 * @m: metadata context
1228 * @mode: king of type that may be found at @ptr
1229 * @opt_attrs: optional attributes to store in the returned type
1230 * @ptr: pointer to the type representation
1231 * @rptr: pointer updated to match the end of the decoded stream
1233 * Decode a compressed type description found at @ptr in @m.
1234 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1235 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1236 * This function can be used to decode type descriptions in method signatures,
1237 * field signatures, locals signatures etc.
1239 * To parse a generic type, `generic_container' points to the current class'es
1240 * (the `generic_container' field in the MonoClass) or the current generic method's
1241 * (the `generic_container' field in the MonoMethodNormal) generic container.
1242 * When we encounter any MONO_TYPE_VAR or MONO_TYPE_MVAR's, they're looked up in
1243 * this MonoGenericContainer.
1245 * Returns: a #MonoType structure representing the decoded type.
1248 mono_metadata_parse_type_full (MonoImage *m, MonoGenericContext *generic_context, MonoParseTypeMode mode,
1249 short opt_attrs, const char *ptr, const char **rptr)
1251 MonoType *type, *cached;
1253 gboolean byref = FALSE;
1254 gboolean pinned = FALSE;
1255 const char *tmp_ptr;
1260 * According to the spec, custom modifiers should come before the byref
1261 * flag, but the IL produced by ilasm from the following signature:
1262 * object modopt(...) &
1263 * starts with a byref flag, followed by the modifiers. (bug #49802)
1264 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1265 * it would be better to treat byref as real type constructor instead of
1267 * Also, pinned should come before anything else, but some MSV++ produced
1268 * assemblies violate this (#bug 61990).
1271 /* Count the modifiers first */
1276 case MONO_TYPE_PINNED:
1277 case MONO_TYPE_BYREF:
1280 case MONO_TYPE_CMOD_REQD:
1281 case MONO_TYPE_CMOD_OPT:
1283 mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1291 type = g_malloc0 (sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1292 type->num_mods = count;
1294 g_warning ("got more than 64 modifiers in type");
1298 memset (type, 0, sizeof (MonoType));
1301 /* Parse pinned, byref and custom modifiers */
1306 case MONO_TYPE_PINNED:
1310 case MONO_TYPE_BYREF:
1314 case MONO_TYPE_CMOD_REQD:
1315 case MONO_TYPE_CMOD_OPT:
1316 mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1324 type->attrs = opt_attrs;
1325 type->byref = byref;
1326 type->pinned = pinned ? 1 : 0;
1328 do_mono_metadata_parse_type (type, m, generic_context, ptr, &ptr);
1334 /* FIXME: remove the != MONO_PARSE_PARAM condition, this accounts for
1335 * almost 10k (about 2/3rds) of all MonoType's we create.
1337 if (mode != MONO_PARSE_PARAM && !type->num_mods) {
1338 /* no need to free type here, because it is on the stack */
1339 if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
1341 return &type->data.klass->this_arg;
1343 return &type->data.klass->byval_arg;
1345 /* No need to use locking since nobody is modifying the hash table */
1346 if ((cached = g_hash_table_lookup (type_cache, type)))
1350 /*printf ("%x%c %s\n", type->attrs, type->pinned ? 'p' : ' ', mono_type_full_name (type));*/
1353 type = g_memdup (&stype, sizeof (MonoType));
1358 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
1359 const char *ptr, const char **rptr)
1361 return mono_metadata_parse_type_full (m, NULL, mode, opt_attrs, ptr, rptr);
1365 * mono_metadata_parse_signature:
1366 * @image: metadata context
1367 * @toke: metadata token
1369 * Decode a method signature stored in the STANDALONESIG table
1371 * Returns: a MonoMethodSignature describing the signature.
1373 MonoMethodSignature *
1374 mono_metadata_parse_signature_full (MonoImage *image, MonoGenericContext *generic_context, guint32 token)
1376 MonoTableInfo *tables = image->tables;
1377 guint32 idx = mono_metadata_token_index (token);
1382 return mono_lookup_dynamic_token (image, token);
1384 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1386 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1388 ptr = mono_metadata_blob_heap (image, sig);
1389 mono_metadata_decode_blob_size (ptr, &ptr);
1391 return mono_metadata_parse_method_signature_full (image, generic_context, FALSE, ptr, NULL);
1394 MonoMethodSignature *
1395 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1397 return mono_metadata_parse_signature_full (image, NULL, token);
1400 MonoMethodSignature*
1401 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1403 MonoMethodSignature *sig;
1405 /* later we want to allocate signatures with mempools */
1406 sig = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1407 sig->param_count = nparams;
1408 sig->sentinelpos = -1;
1413 MonoMethodSignature*
1414 mono_metadata_signature_dup (MonoMethodSignature *sig)
1418 sigsize = sizeof (MonoMethodSignature) + sig->param_count * sizeof (MonoType *);
1419 return g_memdup (sig, sigsize);
1423 * mono_metadata_parse_method_signature:
1424 * @m: metadata context
1425 * @def: the MethodDef index or 0 for Ref signatures.
1426 * @ptr: pointer to the signature metadata representation
1427 * @rptr: pointer updated to match the end of the decoded stream
1429 * Decode a method signature stored at @ptr.
1431 * Returns: a MonoMethodSignature describing the signature.
1433 MonoMethodSignature *
1434 mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContext *generic_context,
1435 int def, const char *ptr, const char **rptr)
1437 MonoMethodSignature *method;
1438 int i, ret_attrs = 0, *pattrs = NULL;
1439 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1440 guint32 gen_param_count = 0;
1441 gboolean is_open = FALSE;
1442 MonoGenericContext *context = NULL;
1445 gen_param_count = 1;
1450 call_convention = *ptr & 0x0F;
1452 if (gen_param_count)
1453 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1454 param_count = mono_metadata_decode_value (ptr, &ptr);
1455 pattrs = g_new0 (int, param_count);
1458 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1459 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1460 guint32 cols [MONO_PARAM_SIZE];
1461 guint lastp, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1463 if (def < methodt->rows)
1464 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1466 lastp = paramt->rows + 1;
1467 for (i = param_index; i < lastp; ++i) {
1468 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1469 if (!cols [MONO_PARAM_SEQUENCE])
1470 ret_attrs = cols [MONO_PARAM_FLAGS];
1472 pattrs [cols [MONO_PARAM_SEQUENCE] - 1] = cols [MONO_PARAM_FLAGS];
1475 method = mono_metadata_signature_alloc (m, param_count);
1476 method->hasthis = hasthis;
1477 method->explicit_this = explicit_this;
1478 method->call_convention = call_convention;
1479 method->generic_param_count = gen_param_count;
1481 if (gen_param_count)
1482 method->has_type_parameters = 1;
1484 if (gen_param_count && (!generic_context || !generic_context->container->is_method)) {
1485 MonoGenericContainer *container = g_new0 (MonoGenericContainer, 1);
1487 if (generic_context)
1488 container->parent = generic_context->container;
1489 container->is_signature = 1;
1491 context = &container->context;
1492 container->context.container = container;
1494 container->type_argc = gen_param_count;
1495 container->type_params = g_new0 (MonoGenericParam, gen_param_count);
1497 for (i = 0; i < gen_param_count; i++) {
1498 container->type_params [i].owner = container;
1499 container->type_params [i].num = i;
1502 context = generic_context;
1504 if (call_convention != 0xa) {
1505 method->ret = mono_metadata_parse_type_full (m, context, MONO_PARSE_RET, ret_attrs, ptr, &ptr);
1506 is_open = mono_class_is_open_constructed_type (method->ret);
1509 if (method->param_count) {
1510 method->sentinelpos = -1;
1512 for (i = 0; i < method->param_count; ++i) {
1513 if (*ptr == MONO_TYPE_SENTINEL) {
1514 if (method->call_convention != MONO_CALL_VARARG || def)
1515 g_error ("found sentinel for methoddef or no vararg method");
1516 method->sentinelpos = i;
1519 method->params [i] = mono_metadata_parse_type_full (
1520 m, context, MONO_PARSE_PARAM, pattrs [i], ptr, &ptr);
1522 is_open = mono_class_is_open_constructed_type (method->params [i]);
1526 method->has_type_parameters = is_open;
1528 if (def && (method->call_convention == MONO_CALL_VARARG))
1529 method->sentinelpos = method->param_count;
1536 * Add signature to a cache and increase ref count...
1541 MonoMethodSignature *
1542 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1544 return mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr);
1548 * mono_metadata_free_method_signature:
1549 * @sig: signature to destroy
1551 * Free the memory allocated in the signature @sig.
1554 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1557 mono_metadata_free_type (sig->ret);
1558 for (i = 0; i < sig->param_count; ++i)
1559 mono_metadata_free_type (sig->params [i]);
1565 mono_metadata_lookup_generic_inst (MonoGenericInst *ginst)
1567 MonoGenericInst *cached;
1570 cached = g_hash_table_lookup (generic_inst_cache, ginst);
1572 for (i = 0; i < ginst->type_argc; i++)
1573 mono_metadata_free_type (ginst->type_argv [i]);
1574 g_free (ginst->type_argv);
1579 ginst->id = ++next_generic_inst_id;
1580 g_hash_table_insert (generic_inst_cache, ginst, ginst);
1586 mono_metadata_lookup_generic_class (MonoGenericClass *gclass)
1588 MonoGenericClass *cached;
1590 cached = g_hash_table_lookup (generic_class_cache, gclass);
1594 g_hash_table_insert (generic_class_cache, gclass, gclass);
1599 mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context)
1601 MonoGenericInst *nginst;
1604 nginst = g_new0 (MonoGenericInst, 1);
1605 nginst->type_argc = ginst->type_argc;
1606 nginst->type_argv = g_new0 (MonoType*, nginst->type_argc);
1607 nginst->is_reference = 1;
1609 for (i = 0; i < nginst->type_argc; i++) {
1610 MonoType *t = mono_class_inflate_generic_type (ginst->type_argv [i], context);
1612 if (!nginst->is_open)
1613 nginst->is_open = mono_class_is_open_constructed_type (t);
1614 if (nginst->is_reference)
1615 nginst->is_reference = MONO_TYPE_IS_REFERENCE (t);
1617 nginst->type_argv [i] = t;
1620 return mono_metadata_lookup_generic_inst (nginst);
1624 mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContext *generic_context,
1625 int count, const char *ptr, const char **rptr)
1627 MonoGenericInst *ginst;
1630 ginst = g_new0 (MonoGenericInst, 1);
1631 ginst->type_argc = count;
1632 ginst->type_argv = g_new0 (MonoType*, count);
1633 ginst->is_reference = 1;
1635 for (i = 0; i < ginst->type_argc; i++) {
1636 MonoType *t = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_TYPE, 0, ptr, &ptr);
1638 ginst->type_argv [i] = t;
1639 if (!ginst->is_open)
1640 ginst->is_open = mono_class_is_open_constructed_type (t);
1641 if (ginst->is_reference)
1642 ginst->is_reference = MONO_TYPE_IS_REFERENCE (t);
1648 return mono_metadata_lookup_generic_inst (ginst);
1652 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContext *generic_context,
1653 const char *ptr, const char **rptr)
1655 MonoGenericClass *gclass = g_new0 (MonoGenericClass, 1);
1656 MonoGenericClass *cached;
1661 type->data.generic_class = gclass;
1663 gclass->context = g_new0 (MonoGenericContext, 1);
1664 gclass->context->gclass = gclass;
1666 gclass->klass = g_new0 (MonoClass, 1);
1668 gtype = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_TYPE, 0, ptr, &ptr);
1669 gclass->container_class = gklass = mono_class_from_mono_type (gtype);
1671 g_assert ((gclass->context->container = gklass->generic_container) != NULL);
1672 count = mono_metadata_decode_value (ptr, &ptr);
1675 * Create the klass before parsing the type arguments.
1676 * This is required to support "recursive" definitions.
1677 * See mcs/tests/gen-23.cs for an example.
1680 mono_class_create_generic (gclass);
1682 gclass->inst = mono_metadata_parse_generic_inst (m, generic_context, count, ptr, &ptr);
1688 * We may be called multiple times on different metadata to create the same
1689 * instantiated type. This happens for instance if we're part of a method or
1690 * local variable signature.
1692 * It's important to return the same MonoGenericClass * for each particualar
1693 * instantiation of a generic type (ie "Stack<Int32>") to make static fields
1696 * According to the spec ($26.1.5), a static variable in a generic class
1697 * declaration is shared amongst all instances of the same closed constructed
1701 cached = g_hash_table_lookup (generic_class_cache, gclass);
1703 g_free (gclass->klass);
1706 type->data.generic_class = cached;
1709 g_hash_table_insert (generic_class_cache, gclass, gclass);
1711 mono_stats.generic_instance_count++;
1712 mono_stats.generics_metadata_size += sizeof (MonoGenericClass) +
1713 sizeof (MonoGenericContext) +
1714 gclass->inst->type_argc * sizeof (MonoType);
1719 * do_mono_metadata_parse_generic_param:
1720 * @generic_container: Our MonoClass's or MonoMethodNormal's MonoGenericContainer;
1721 * see mono_metadata_parse_type_full() for details.
1722 * Internal routine to parse a generic type parameter.
1724 static MonoGenericParam *
1725 mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContext *generic_context,
1726 gboolean is_mvar, const char *ptr, const char **rptr)
1728 MonoGenericContainer *generic_container;
1731 index = mono_metadata_decode_value (ptr, &ptr);
1735 g_assert (generic_context);
1736 if (generic_context->gmethod)
1737 generic_container = generic_context->gmethod->container;
1738 else if (generic_context->gclass)
1739 generic_container = generic_context->gclass->container_class->generic_container;
1741 generic_container = generic_context->container;
1744 g_assert (generic_container);
1745 if (generic_container->parent) {
1747 * The current MonoGenericContainer is a generic method -> its `parent'
1748 * points to the containing class'es container.
1750 generic_container = generic_container->parent;
1752 g_assert (generic_container && !generic_container->is_method);
1753 g_assert (index < generic_container->type_argc);
1755 return &generic_container->type_params [index];
1757 g_assert (generic_container && (generic_container->is_method || generic_container->is_signature));
1758 g_assert (index < generic_container->type_argc);
1760 return &generic_container->type_params [index];
1765 * do_mono_metadata_parse_type:
1766 * @type: MonoType to be filled in with the return value
1768 * Internal routine used to "fill" the contents of @type from an
1769 * allocated pointer. This is done this way to avoid doing too
1770 * many mini-allocations (particularly for the MonoFieldType which
1771 * most of the time is just a MonoType, but sometimes might be augmented).
1773 * This routine is used by mono_metadata_parse_type and
1774 * mono_metadata_parse_field_type
1776 * This extracts a Type as specified in Partition II (22.2.12)
1779 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContext *generic_context,
1780 const char *ptr, const char **rptr)
1782 type->type = mono_metadata_decode_value (ptr, &ptr);
1784 switch (type->type){
1785 case MONO_TYPE_VOID:
1786 case MONO_TYPE_BOOLEAN:
1787 case MONO_TYPE_CHAR:
1800 case MONO_TYPE_STRING:
1801 case MONO_TYPE_OBJECT:
1802 case MONO_TYPE_TYPEDBYREF:
1804 case MONO_TYPE_VALUETYPE:
1805 case MONO_TYPE_CLASS: {
1807 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
1808 type->data.klass = mono_class_get (m, token);
1811 case MONO_TYPE_SZARRAY: {
1812 MonoType *etype = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1813 type->data.klass = mono_class_from_mono_type (etype);
1814 mono_metadata_free_type (etype);
1818 type->data.type = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1820 case MONO_TYPE_FNPTR:
1821 type->data.method = mono_metadata_parse_method_signature_full (m, generic_context, 0, ptr, &ptr);
1823 case MONO_TYPE_ARRAY:
1824 type->data.array = mono_metadata_parse_array_full (m, generic_context, ptr, &ptr);
1827 case MONO_TYPE_MVAR:
1828 type->data.generic_param = mono_metadata_parse_generic_param (m, generic_context, TRUE, ptr, &ptr);
1832 type->data.generic_param = mono_metadata_parse_generic_param (m, generic_context, FALSE, ptr, &ptr);
1835 case MONO_TYPE_GENERICINST:
1836 do_mono_metadata_parse_generic_class (type, m, generic_context, ptr, &ptr);
1840 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
1848 * mono_metadata_free_type:
1849 * @type: type to free
1851 * Free the memory allocated for type @type.
1854 mono_metadata_free_type (MonoType *type)
1856 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
1859 switch (type->type){
1860 case MONO_TYPE_OBJECT:
1861 case MONO_TYPE_STRING:
1862 if (!type->data.klass)
1865 case MONO_TYPE_CLASS:
1866 case MONO_TYPE_VALUETYPE:
1867 if (type == &type->data.klass->byval_arg || type == &type->data.klass->this_arg)
1871 mono_metadata_free_type (type->data.type);
1873 case MONO_TYPE_FNPTR:
1874 mono_metadata_free_method_signature (type->data.method);
1876 case MONO_TYPE_ARRAY:
1877 mono_metadata_free_array (type->data.array);
1885 hex_dump (const char *buffer, int base, int count)
1887 int show_header = 1;
1895 for (i = 0; i < count; i++){
1898 printf ("\n0x%08x: ", (unsigned char) base + i);
1900 printf ("%02x ", (unsigned char) (buffer [i]));
1907 * @mh: The Method header
1908 * @ptr: Points to the beginning of the Section Data (25.3)
1911 parse_section_data (MonoImage *m, MonoMethodHeader *mh, const unsigned char *ptr)
1913 unsigned char sect_data_flags;
1914 const unsigned char *sptr;
1916 guint32 sect_data_len;
1919 /* align on 32-bit boundary */
1920 /* FIXME: not 64-bit clean code */
1921 sptr = ptr = dword_align (ptr);
1922 sect_data_flags = *ptr;
1925 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
1927 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
1930 sect_data_len = ptr [0];
1934 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
1935 hex_dump (sptr, 0, sect_data_len+8);
1936 g_print ("\nheader: ");
1937 hex_dump (sptr-4, 0, 4);
1941 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
1942 const unsigned char *p = dword_align (ptr);
1944 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
1945 /* we could just store a pointer if we don't need to byteswap */
1946 mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
1947 for (i = 0; i < mh->num_clauses; ++i) {
1948 MonoExceptionClause *ec = &mh->clauses [i];
1951 ec->flags = read32 (p);
1953 ec->try_offset = read32 (p);
1955 ec->try_len = read32 (p);
1957 ec->handler_offset = read32 (p);
1959 ec->handler_len = read32 (p);
1961 tof_value = read32 (p);
1964 ec->flags = read16 (p);
1966 ec->try_offset = read16 (p);
1970 ec->handler_offset = read16 (p);
1972 ec->handler_len = *p;
1974 tof_value = read32 (p);
1977 if (ec->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
1978 ec->data.filter_offset = tof_value;
1979 } else if (ec->flags == MONO_EXCEPTION_CLAUSE_NONE) {
1980 ec->data.catch_class = tof_value? mono_class_get (m, tof_value): 0;
1982 ec->data.catch_class = NULL;
1984 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
1988 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
1989 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
1996 * mono_metadata_parse_mh:
1997 * @m: metadata context
1998 * @ptr: pointer to the method header.
2000 * Decode the method header at @ptr, including pointer to the IL code,
2001 * info about local variables and optional exception tables.
2003 * Returns: a MonoMethodHeader.
2006 mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContext *generic_context, const char *ptr)
2008 MonoMethodHeader *mh;
2009 unsigned char flags = *(const unsigned char *) ptr;
2010 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
2012 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
2013 const unsigned char *code;
2016 g_return_val_if_fail (ptr != NULL, NULL);
2019 case METHOD_HEADER_TINY_FORMAT:
2020 mh = g_new0 (MonoMethodHeader, 1);
2023 local_var_sig_tok = 0;
2024 mh->code_size = flags >> 2;
2027 case METHOD_HEADER_TINY_FORMAT1:
2028 mh = g_new0 (MonoMethodHeader, 1);
2031 local_var_sig_tok = 0;
2034 * The spec claims 3 bits, but the Beta2 is
2037 mh->code_size = flags >> 2;
2040 case METHOD_HEADER_FAT_FORMAT:
2041 fat_flags = read16 (ptr);
2043 hsize = (fat_flags >> 12) & 0xf;
2044 max_stack = read16 (ptr);
2046 code_size = read32 (ptr);
2048 local_var_sig_tok = read32 (ptr);
2051 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
2058 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
2062 * There are more sections
2064 ptr = code + code_size;
2072 if (local_var_sig_tok) {
2073 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
2074 const char *locals_ptr;
2075 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
2076 int len=0, i, bsize;
2078 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
2079 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
2080 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
2081 if (*locals_ptr != 0x07)
2082 g_warning ("wrong signature for locals blob");
2084 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
2085 mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
2086 mh->num_locals = len;
2087 for (i = 0; i < len; ++i)
2088 mh->locals [i] = mono_metadata_parse_type_full (
2089 m, generic_context, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
2091 mh = g_new0 (MonoMethodHeader, 1);
2094 mh->code_size = code_size;
2095 mh->max_stack = max_stack;
2096 mh->init_locals = init_locals;
2097 if (fat_flags & METHOD_HEADER_MORE_SECTS)
2098 parse_section_data (m, mh, (const unsigned char*)ptr);
2103 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
2105 return mono_metadata_parse_mh_full (m, NULL, ptr);
2109 * mono_metadata_free_mh:
2110 * @mh: a method header
2112 * Free the memory allocated for the method header.
2115 mono_metadata_free_mh (MonoMethodHeader *mh)
2118 for (i = 0; i < mh->num_locals; ++i)
2119 mono_metadata_free_type (mh->locals[i]);
2120 g_free (mh->clauses);
2125 * mono_metadata_parse_field_type:
2126 * @m: metadata context to extract information from
2127 * @ptr: pointer to the field signature
2128 * @rptr: pointer updated to match the end of the decoded stream
2130 * Parses the field signature, and returns the type information for it.
2132 * Returns: The MonoType that was extracted from @ptr.
2135 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
2137 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
2141 * mono_metadata_parse_param:
2142 * @m: metadata context to extract information from
2143 * @ptr: pointer to the param signature
2144 * @rptr: pointer updated to match the end of the decoded stream
2146 * Parses the param signature, and returns the type information for it.
2148 * Returns: The MonoType that was extracted from @ptr.
2151 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
2153 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
2157 * mono_metadata_token_from_dor:
2158 * @dor_token: A TypeDefOrRef coded index
2160 * dor_token is a TypeDefOrRef coded index: it contains either
2161 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
2162 * bits contain an index into the table.
2164 * Returns: an expanded token
2167 mono_metadata_token_from_dor (guint32 dor_index)
2171 table = dor_index & 0x03;
2172 idx = dor_index >> 2;
2175 case 0: /* TypeDef */
2176 return MONO_TOKEN_TYPE_DEF | idx;
2178 case 1: /* TypeRef */
2179 return MONO_TOKEN_TYPE_REF | idx;
2181 case 2: /* TypeSpec */
2182 return MONO_TOKEN_TYPE_SPEC | idx;
2185 g_assert_not_reached ();
2192 * We use this to pass context information to the row locator
2195 int idx; /* The index that we are trying to locate */
2196 int col_idx; /* The index in the row where idx may be stored */
2197 MonoTableInfo *t; /* pointer to the table */
2202 * How the row locator works.
2207 * ___|___------> _______
2210 * A column in the rows of table A references an index in table B.
2211 * For example A may be the TYPEDEF table and B the METHODDEF table.
2213 * Given an index in table B we want to get the row in table A
2214 * where the column n references our index in B.
2216 * In the locator_t structure:
2218 * col_idx is the column number
2219 * index is the index in table B
2220 * result will be the index in table A
2223 * Table A Table B column (in table A)
2224 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
2225 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
2226 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
2227 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
2228 * METHODSEM PROPERTY ASSOCIATION (encoded index)
2230 * Note that we still don't support encoded indexes.
2234 typedef_locator (const void *a, const void *b)
2236 locator_t *loc = (locator_t *) a;
2237 const char *bb = (const char *) b;
2238 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
2239 guint32 col, col_next;
2241 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
2247 * Need to check that the next row is valid.
2249 if (typedef_index + 1 < loc->t->rows) {
2250 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
2251 if (loc->idx >= col_next)
2254 if (col == col_next)
2258 loc->result = typedef_index;
2264 table_locator (const void *a, const void *b)
2266 locator_t *loc = (locator_t *) a;
2267 const char *bb = (const char *) b;
2268 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2271 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2273 if (loc->idx == col) {
2274 loc->result = table_index;
2284 declsec_locator (const void *a, const void *b)
2286 locator_t *loc = (locator_t *) a;
2287 const char *bb = (const char *) b;
2288 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2291 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2293 if (loc->idx == col) {
2294 loc->result = table_index;
2304 * mono_metadata_typedef_from_field:
2305 * @meta: metadata context
2306 * @index: FieldDef token
2308 * Returns: the 1-based index into the TypeDef table of the type that
2309 * declared the field described by @index, or 0 if not found.
2312 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
2314 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2320 loc.idx = mono_metadata_token_index (index);
2321 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
2324 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2325 g_assert_not_reached ();
2327 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2328 return loc.result + 1;
2332 * mono_metadata_typedef_from_method:
2333 * @meta: metadata context
2334 * @index: MethodDef token
2336 * Returns: the 1-based index into the TypeDef table of the type that
2337 * declared the method described by @index. 0 if not found.
2340 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
2342 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2348 loc.idx = mono_metadata_token_index (index);
2349 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
2352 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2353 g_assert_not_reached ();
2355 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2356 return loc.result + 1;
2360 * mono_metadata_interfaces_from_typedef:
2361 * @meta: metadata context
2362 * @index: typedef token
2364 * Returns: and array of interfaces that the @index typedef token implements.
2365 * The number of elemnts in the array is returned in @count.
2368 mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, guint *count, MonoGenericContext *context)
2370 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
2373 guint32 cols [MONO_INTERFACEIMPL_SIZE];
2381 loc.idx = mono_metadata_token_index (index);
2382 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
2385 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2390 * We may end up in the middle of the rows...
2393 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
2400 while (start < tdef->rows) {
2401 mono_metadata_decode_row (tdef, start, cols, MONO_INTERFACEIMPL_SIZE);
2402 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
2404 result = g_renew (MonoClass*, result, i + 1);
2405 result [i] = mono_class_get_full (
2406 meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]), context);
2414 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
2416 return mono_metadata_interfaces_from_typedef_full (meta, index, count, NULL);
2420 * mono_metadata_nested_in_typedef:
2421 * @meta: metadata context
2422 * @index: typedef token
2424 * Returns: the 1-based index into the TypeDef table of the type
2425 * where the type described by @index is nested.
2426 * Retruns 0 if @index describes a non-nested type.
2429 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
2431 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2437 loc.idx = mono_metadata_token_index (index);
2438 loc.col_idx = MONO_NESTED_CLASS_NESTED;
2441 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2444 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2445 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
2449 * mono_metadata_nesting_typedef:
2450 * @meta: metadata context
2451 * @index: typedef token
2453 * Returns: the 1-based index into the TypeDef table of the first type
2454 * that is nested inside the type described by @index. The search starts at
2455 * @start_index. returns 0 if no such type is found.
2458 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
2460 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2462 guint32 class_index = mono_metadata_token_index (index);
2467 start = start_index;
2469 while (start <= tdef->rows) {
2470 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
2476 if (start > tdef->rows)
2483 * mono_metadata_packing_from_typedef:
2484 * @meta: metadata context
2485 * @index: token representing a type
2487 * Returns: the info stored in the ClassLAyout table for the given typedef token
2488 * into the @packing and @size pointers.
2489 * Returns 0 if the info is not found.
2492 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
2494 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
2496 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
2501 loc.idx = mono_metadata_token_index (index);
2502 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
2505 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2508 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
2510 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
2512 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
2514 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2515 return loc.result + 1;
2519 * mono_metadata_custom_attrs_from_index:
2520 * @meta: metadata context
2521 * @index: token representing the parent
2523 * Returns: the 1-based index into the CustomAttribute table of the first
2524 * attribute which belongs to the metadata object described by @index.
2525 * Returns 0 if no such attribute is found.
2528 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
2530 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
2537 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
2540 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2543 /* Find the first entry by searching backwards */
2544 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
2547 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2548 return loc.result + 1;
2552 * mono_metadata_declsec_from_index:
2553 * @meta: metadata context
2554 * @index: token representing the parent
2556 * Returns: the 0-based index into the DeclarativeSecurity table of the first
2557 * attribute which belongs to the metadata object described by @index.
2558 * Returns -1 if no such attribute is found.
2561 mono_metadata_declsec_from_index (MonoImage *meta, guint32 index)
2563 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_DECLSECURITY];
2570 loc.col_idx = MONO_DECL_SECURITY_PARENT;
2573 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
2576 /* Find the first entry by searching backwards */
2577 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_DECL_SECURITY_PARENT) == index))
2585 mono_backtrace (int limit)
2590 backtrace (array, limit);
2591 names = backtrace_symbols (array, limit);
2592 for (i =0; i < limit; ++i) {
2593 g_print ("\t%s\n", names [i]);
2600 /*#define __alignof__(a) sizeof(a)*/
2601 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
2606 * @t: the type to return the size of
2608 * Returns: the number of bytes required to hold an instance of this
2612 mono_type_size (MonoType *t, gint *align)
2619 *align = __alignof__(gpointer);
2620 return sizeof (gpointer);
2624 case MONO_TYPE_VOID:
2627 case MONO_TYPE_BOOLEAN:
2628 *align = __alignof__(gint8);
2632 *align = __alignof__(gint8);
2634 case MONO_TYPE_CHAR:
2637 *align = __alignof__(gint16);
2641 *align = __alignof__(gint32);
2644 *align = __alignof__(float);
2648 *align = __alignof__(gint64);
2651 *align = __alignof__(double);
2655 *align = __alignof__(gpointer);
2656 return sizeof (gpointer);
2657 case MONO_TYPE_STRING:
2658 *align = __alignof__(gpointer);
2659 return sizeof (gpointer);
2660 case MONO_TYPE_OBJECT:
2661 *align = __alignof__(gpointer);
2662 return sizeof (gpointer);
2663 case MONO_TYPE_VALUETYPE: {
2664 if (t->data.klass->enumtype)
2665 return mono_type_size (t->data.klass->enum_basetype, align);
2667 return mono_class_value_size (t->data.klass, align);
2669 case MONO_TYPE_CLASS:
2670 case MONO_TYPE_SZARRAY:
2672 case MONO_TYPE_FNPTR:
2673 case MONO_TYPE_ARRAY:
2674 *align = __alignof__(gpointer);
2675 return sizeof (gpointer);
2676 case MONO_TYPE_TYPEDBYREF:
2677 return mono_class_value_size (mono_defaults.typed_reference_class, align);
2678 case MONO_TYPE_GENERICINST: {
2679 MonoGenericClass *gclass = t->data.generic_class;
2681 g_assert (!gclass->inst->is_open && !gclass->klass->generic_container);
2683 if (gclass->container_class->valuetype) {
2684 if (gclass->container_class->enumtype)
2685 return mono_type_size (gclass->container_class->enum_basetype, align);
2687 return mono_class_value_size (gclass->klass, align);
2689 *align = __alignof__(gpointer);
2690 return sizeof (gpointer);
2694 case MONO_TYPE_MVAR:
2695 *align = __alignof__(gpointer);
2696 return sizeof (gpointer);
2698 g_error ("mono_type_size: type 0x%02x unknown", t->type);
2704 * mono_type_stack_size:
2705 * @t: the type to return the size it uses on the stack
2707 * Returns: the number of bytes required to hold an instance of this
2708 * type on the runtime stack
2711 mono_type_stack_size (MonoType *t, gint *align)
2715 g_assert (t != NULL);
2721 *align = __alignof__(gpointer);
2722 return sizeof (gpointer);
2726 case MONO_TYPE_BOOLEAN:
2727 case MONO_TYPE_CHAR:
2736 case MONO_TYPE_STRING:
2737 case MONO_TYPE_OBJECT:
2738 case MONO_TYPE_CLASS:
2739 case MONO_TYPE_SZARRAY:
2741 case MONO_TYPE_FNPTR:
2742 case MONO_TYPE_ARRAY:
2743 *align = __alignof__(gpointer);
2744 return sizeof (gpointer);
2745 case MONO_TYPE_TYPEDBYREF:
2746 *align = __alignof__(gpointer);
2747 return sizeof (gpointer) * 3;
2749 *align = __alignof__(float);
2750 return sizeof (float);
2753 *align = __alignof__(gint64);
2754 return sizeof (gint64);
2756 *align = __alignof__(double);
2757 return sizeof (double);
2758 case MONO_TYPE_VALUETYPE: {
2761 if (t->data.klass->enumtype)
2762 return mono_type_stack_size (t->data.klass->enum_basetype, align);
2764 size = mono_class_value_size (t->data.klass, align);
2766 *align = *align + __alignof__(gpointer) - 1;
2767 *align &= ~(__alignof__(gpointer) - 1);
2769 size += sizeof (gpointer) - 1;
2770 size &= ~(sizeof (gpointer) - 1);
2775 case MONO_TYPE_GENERICINST: {
2776 MonoGenericClass *gclass = t->data.generic_class;
2778 g_assert (!gclass->inst->is_open && !gclass->klass->generic_container);
2780 if (gclass->container_class->valuetype) {
2781 if (gclass->container_class->enumtype)
2782 return mono_type_stack_size (gclass->container_class->enum_basetype, align);
2784 guint32 size = mono_class_value_size (gclass->klass, align);
2786 *align = *align + __alignof__(gpointer) - 1;
2787 *align &= ~(__alignof__(gpointer) - 1);
2789 size += sizeof (gpointer) - 1;
2790 size &= ~(sizeof (gpointer) - 1);
2795 *align = __alignof__(gpointer);
2796 return sizeof (gpointer);
2800 g_error ("type 0x%02x unknown", t->type);
2806 mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass)
2808 return gclass->container_class->valuetype;
2812 _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2, gboolean signature_only)
2816 if ((g1->inst->type_argc != g2->inst->type_argc) || (g1->is_dynamic != g2->is_dynamic) ||
2817 (g1->inst->is_reference != g2->inst->is_reference))
2819 if (!mono_metadata_class_equal (g1->container_class, g2->container_class, signature_only))
2821 for (i = 0; i < g1->inst->type_argc; ++i) {
2822 if (!do_mono_metadata_type_equal (g1->inst->type_argv [i], g2->inst->type_argv [i], signature_only))
2829 mono_metadata_generic_method_hash (MonoGenericMethod *gmethod)
2831 return gmethod->inst->id;
2835 mono_metadata_generic_method_equal (MonoGenericMethod *g1, MonoGenericMethod *g2)
2837 return (g1->container == g2->container) && (g1->inst == g2->inst);
2842 * mono_metadata_type_hash:
2845 * Computes an hash value for @t1 to be used in GHashTable.
2848 mono_metadata_type_hash (MonoType *t1)
2850 guint hash = t1->type;
2852 hash |= t1->byref << 6; /* do not collide with t1->type values */
2854 case MONO_TYPE_VALUETYPE:
2855 case MONO_TYPE_CLASS:
2856 case MONO_TYPE_SZARRAY:
2857 /* check if the distribution is good enough */
2858 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
2860 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
2861 case MONO_TYPE_ARRAY:
2862 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
2863 case MONO_TYPE_GENERICINST:
2864 return ((hash << 5) - hash) ^ mono_generic_class_hash (t1->data.generic_class);
2870 mono_metadata_generic_param_equal (MonoGenericParam *p1, MonoGenericParam *p2, gboolean signature_only)
2874 if (p1->num != p2->num)
2877 g_assert (p1->owner && p2->owner);
2878 if (p1->owner == p2->owner)
2882 * If `signature_only' is true, we're comparing two (method) signatures.
2883 * In this case, the owner of two type parameters doesn't need to match.
2886 return signature_only;
2890 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only)
2894 if (c1->generic_class && c2->generic_class)
2895 return _mono_metadata_generic_class_equal (c1->generic_class, c2->generic_class, signature_only);
2896 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
2897 return mono_metadata_generic_param_equal (
2898 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, FALSE);
2899 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
2900 return mono_metadata_generic_param_equal (
2901 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
2906 * mono_metadata_type_equal:
2910 * Determine if @t1 and @t2 represent the same type.
2911 * Returns: #TRUE if @t1 and @t2 are equal.
2914 do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only)
2916 if (t1->type != t2->type ||
2917 t1->byref != t2->byref)
2921 case MONO_TYPE_VOID:
2922 case MONO_TYPE_BOOLEAN:
2923 case MONO_TYPE_CHAR:
2934 case MONO_TYPE_STRING:
2937 case MONO_TYPE_OBJECT:
2938 case MONO_TYPE_TYPEDBYREF:
2940 case MONO_TYPE_VALUETYPE:
2941 case MONO_TYPE_CLASS:
2942 case MONO_TYPE_SZARRAY:
2943 return mono_metadata_class_equal (t1->data.klass, t2->data.klass, signature_only);
2945 return do_mono_metadata_type_equal (t1->data.type, t2->data.type, signature_only);
2946 case MONO_TYPE_ARRAY:
2947 if (t1->data.array->rank != t2->data.array->rank)
2949 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass, signature_only);
2950 case MONO_TYPE_GENERICINST:
2951 return _mono_metadata_generic_class_equal (
2952 t1->data.generic_class, t2->data.generic_class, signature_only);
2954 return mono_metadata_generic_param_equal (
2955 t1->data.generic_param, t2->data.generic_param, FALSE);
2956 case MONO_TYPE_MVAR:
2957 return mono_metadata_generic_param_equal (
2958 t1->data.generic_param, t2->data.generic_param, signature_only);
2960 g_error ("implement type compare for %0x!", t1->type);
2968 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
2970 return do_mono_metadata_type_equal (t1, t2, FALSE);
2974 * mono_metadata_signature_equal:
2975 * @sig1: a signature
2976 * @sig2: another signature
2978 * Determine if @sig1 and @sig2 represent the same signature, with the
2979 * same number of arguments and the same types.
2980 * Returns: #TRUE if @sig1 and @sig2 are equal.
2983 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
2987 if (sig1->hasthis != sig2->hasthis ||
2988 sig1->param_count != sig2->param_count)
2992 * We're just comparing the signatures of two methods here:
2994 * If we have two generic methods `void Foo<U> (U u)' and `void Bar<V> (V v)',
2995 * U and V are equal here.
2997 * That's what the `signature_only' argument of do_mono_metadata_type_equal() is for.
3000 for (i = 0; i < sig1->param_count; i++) {
3001 MonoType *p1 = sig1->params[i];
3002 MonoType *p2 = sig2->params[i];
3004 /* if (p1->attrs != p2->attrs)
3007 if (!do_mono_metadata_type_equal (p1, p2, TRUE))
3011 if (!do_mono_metadata_type_equal (sig1->ret, sig2->ret, TRUE))
3017 mono_signature_hash (MonoMethodSignature *sig)
3019 guint i, res = sig->ret->type;
3021 for (i = 0; i < sig->param_count; i++)
3022 res = (res << 5) - res + sig->params[i]->type;
3028 * mono_metadata_encode_value:
3029 * @value: value to encode
3030 * @buf: buffer where to write the compressed representation
3031 * @endbuf: pointer updated to point at the end of the encoded output
3033 * Encodes the value @value in the compressed representation used
3034 * in metadata and stores the result in @buf. @buf needs to be big
3035 * enough to hold the data (4 bytes).
3038 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
3044 else if (value < 0x4000) {
3045 p [0] = 0x80 | (value >> 8);
3046 p [1] = value & 0xff;
3049 p [0] = (value >> 24) | 0xc0;
3050 p [1] = (value >> 16) & 0xff;
3051 p [2] = (value >> 8) & 0xff;
3052 p [3] = value & 0xff;
3060 * mono_metadata_field_info:
3061 * @meta: the Image the field is defined in
3062 * @index: the index in the field table representing the field
3063 * @offset: a pointer to an integer where to store the offset that
3064 * may have been specified for the field in a FieldLayout table
3065 * @rva: a pointer to the RVA of the field data in the image that
3066 * may have been defined in a FieldRVA table
3067 * @marshal_spec: a pointer to the marshal spec that may have been
3068 * defined for the field in a FieldMarshal table.
3070 * Gather info for field @index that may have been defined in the FieldLayout,
3071 * FieldRVA and FieldMarshal tables.
3072 * Either of offset, rva and marshal_spec can be NULL if you're not interested
3076 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
3077 MonoMarshalSpec **marshal_spec)
3079 MonoTableInfo *tdef;
3082 loc.idx = index + 1;
3084 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
3086 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
3089 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3090 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
3092 *offset = (guint32)-1;
3096 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
3098 loc.col_idx = MONO_FIELD_RVA_FIELD;
3101 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3103 * LAMESPEC: There is no signature, no nothing, just the raw data.
3105 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
3113 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
3114 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
3121 * mono_metadata_get_constant_index:
3122 * @meta: the Image the field is defined in
3123 * @index: the token that may have a row defined in the constants table
3124 * @hint: possible position for the row
3126 * @token must be a FieldDef, ParamDef or PropertyDef token.
3128 * Returns: the index into the Constants table or 0 if not found.
3131 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
3133 MonoTableInfo *tdef;
3135 guint32 index = mono_metadata_token_index (token);
3137 tdef = &meta->tables [MONO_TABLE_CONSTANT];
3138 index <<= MONO_HASCONSTANT_BITS;
3139 switch (mono_metadata_token_table (token)) {
3140 case MONO_TABLE_FIELD:
3141 index |= MONO_HASCONSTANT_FIEDDEF;
3143 case MONO_TABLE_PARAM:
3144 index |= MONO_HASCONSTANT_PARAM;
3146 case MONO_TABLE_PROPERTY:
3147 index |= MONO_HASCONSTANT_PROPERTY;
3150 g_warning ("Not a valid token for the constant table: 0x%08x", token);
3154 loc.col_idx = MONO_CONSTANT_PARENT;
3157 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
3160 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3161 return loc.result + 1;
3167 * mono_metadata_events_from_typedef:
3168 * @meta: metadata context
3169 * @index: 0-based index (in the TypeDef table) describing a type
3171 * Returns: the 0-based index in the Event table for the events in the
3172 * type. The last event that belongs to the type (plus 1) is stored
3173 * in the @end_idx pointer.
3176 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3180 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
3188 loc.col_idx = MONO_EVENT_MAP_PARENT;
3189 loc.idx = index + 1;
3191 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3194 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
3195 if (loc.result + 1 < tdef->rows) {
3196 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
3198 end = meta->tables [MONO_TABLE_EVENT].rows;
3206 * mono_metadata_methods_from_event:
3207 * @meta: metadata context
3208 * @index: 0-based index (in the Event table) describing a event
3210 * Returns: the 0-based index in the MethodDef table for the methods in the
3211 * event. The last method that belongs to the event (plus 1) is stored
3212 * in the @end_idx pointer.
3215 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
3219 guint32 cols [MONO_METHOD_SEMA_SIZE];
3220 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3227 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3228 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
3230 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3235 * We may end up in the middle of the rows...
3238 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3244 while (end < msemt->rows) {
3245 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3246 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3255 * mono_metadata_properties_from_typedef:
3256 * @meta: metadata context
3257 * @index: 0-based index (in the TypeDef table) describing a type
3259 * Returns: the 0-based index in the Property table for the properties in the
3260 * type. The last property that belongs to the type (plus 1) is stored
3261 * in the @end_idx pointer.
3264 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3268 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
3276 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
3277 loc.idx = index + 1;
3279 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3282 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
3283 if (loc.result + 1 < tdef->rows) {
3284 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
3286 end = meta->tables [MONO_TABLE_PROPERTY].rows;
3294 * mono_metadata_methods_from_property:
3295 * @meta: metadata context
3296 * @index: 0-based index (in the PropertyDef table) describing a property
3298 * Returns: the 0-based index in the MethodDef table for the methods in the
3299 * property. The last method that belongs to the property (plus 1) is stored
3300 * in the @end_idx pointer.
3303 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
3307 guint32 cols [MONO_METHOD_SEMA_SIZE];
3308 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3315 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3316 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
3318 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3323 * We may end up in the middle of the rows...
3326 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3332 while (end < msemt->rows) {
3333 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3334 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3343 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
3346 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
3352 loc.col_idx = MONO_IMPLMAP_MEMBER;
3353 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
3355 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3358 return loc.result + 1;
3362 * @image: context where the image is created
3363 * @type_spec: typespec token
3365 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
3369 mono_type_create_from_typespec_full (MonoImage *image, MonoGenericContext *generic_context, guint32 type_spec)
3371 guint32 idx = mono_metadata_token_index (type_spec);
3373 guint32 cols [MONO_TYPESPEC_SIZE];
3378 mono_loader_lock ();
3380 if ((type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec)))) {
3381 mono_loader_unlock ();
3385 t = &image->tables [MONO_TABLE_TYPESPEC];
3387 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
3388 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
3389 len = mono_metadata_decode_value (ptr, &ptr);
3391 type = g_new0 (MonoType, 1);
3393 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
3395 if (*ptr == MONO_TYPE_BYREF) {
3400 do_mono_metadata_parse_type (type, image, generic_context, ptr, &ptr);
3402 mono_loader_unlock ();
3408 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
3410 return mono_type_create_from_typespec_full (image, NULL, type_spec);
3414 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
3416 MonoMarshalSpec *res;
3418 const char *start = ptr;
3420 /* fixme: this is incomplete, but I cant find more infos in the specs */
3422 res = g_new0 (MonoMarshalSpec, 1);
3424 len = mono_metadata_decode_value (ptr, &ptr);
3425 res->native = *ptr++;
3427 if (res->native == MONO_NATIVE_LPARRAY) {
3428 if (ptr - start <= len)
3429 res->data.array_data.elem_type = *ptr++;
3430 if (ptr - start <= len)
3431 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
3432 if (ptr - start <= len)
3433 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3436 if (res->native == MONO_NATIVE_BYVALTSTR) {
3437 if (ptr - start <= len)
3438 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3441 if (res->native == MONO_NATIVE_BYVALARRAY) {
3442 if (ptr - start <= len)
3443 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3446 if (res->native == MONO_NATIVE_CUSTOM) {
3447 /* skip unused type guid */
3448 len = mono_metadata_decode_value (ptr, &ptr);
3450 /* skip unused native type name */
3451 len = mono_metadata_decode_value (ptr, &ptr);
3453 /* read custom marshaler type name */
3454 len = mono_metadata_decode_value (ptr, &ptr);
3455 res->data.custom_data.custom_name = g_strndup (ptr, len);
3457 /* read cookie string */
3458 len = mono_metadata_decode_value (ptr, &ptr);
3459 res->data.custom_data.cookie = g_strndup (ptr, len);
3466 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
3468 if (spec->native == MONO_NATIVE_CUSTOM) {
3469 g_free (spec->data.custom_data.custom_name);
3470 g_free (spec->data.custom_data.cookie);
3476 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
3477 gboolean unicode, MonoMarshalConv *conv)
3479 MonoMarshalConv dummy_conv;
3485 *conv = MONO_MARSHAL_CONV_NONE;
3488 return MONO_NATIVE_UINT;
3492 case MONO_TYPE_BOOLEAN:
3494 switch (mspec->native) {
3495 case MONO_NATIVE_VARIANTBOOL:
3496 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
3497 return MONO_NATIVE_VARIANTBOOL;
3498 case MONO_NATIVE_BOOLEAN:
3499 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3500 return MONO_NATIVE_BOOLEAN;
3501 case MONO_NATIVE_I1:
3502 case MONO_NATIVE_U1:
3503 return mspec->native;
3505 g_error ("cant marshal bool to native type %02x", mspec->native);
3508 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3509 return MONO_NATIVE_BOOLEAN;
3510 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
3511 case MONO_TYPE_I1: return MONO_NATIVE_I1;
3512 case MONO_TYPE_U1: return MONO_NATIVE_U1;
3513 case MONO_TYPE_I2: return MONO_NATIVE_I2;
3514 case MONO_TYPE_U2: return MONO_NATIVE_U2;
3515 case MONO_TYPE_I4: return MONO_NATIVE_I4;
3516 case MONO_TYPE_U4: return MONO_NATIVE_U4;
3517 case MONO_TYPE_I8: return MONO_NATIVE_I8;
3518 case MONO_TYPE_U8: return MONO_NATIVE_U8;
3519 case MONO_TYPE_R4: return MONO_NATIVE_R4;
3520 case MONO_TYPE_R8: return MONO_NATIVE_R8;
3521 case MONO_TYPE_STRING:
3523 switch (mspec->native) {
3524 case MONO_NATIVE_BSTR:
3525 *conv = MONO_MARSHAL_CONV_STR_BSTR;
3526 return MONO_NATIVE_BSTR;
3527 case MONO_NATIVE_LPSTR:
3528 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
3529 return MONO_NATIVE_LPSTR;
3530 case MONO_NATIVE_LPWSTR:
3531 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
3532 return MONO_NATIVE_LPWSTR;
3533 case MONO_NATIVE_LPTSTR:
3534 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3535 return MONO_NATIVE_LPTSTR;
3536 case MONO_NATIVE_ANSIBSTR:
3537 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
3538 return MONO_NATIVE_ANSIBSTR;
3539 case MONO_NATIVE_TBSTR:
3540 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
3541 return MONO_NATIVE_TBSTR;
3542 case MONO_NATIVE_BYVALTSTR:
3544 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
3546 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
3547 return MONO_NATIVE_BYVALTSTR;
3549 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);
3552 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3553 return MONO_NATIVE_LPTSTR;
3554 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
3555 case MONO_TYPE_VALUETYPE: /*FIXME*/
3556 if (type->data.klass->enumtype) {
3557 t = type->data.klass->enum_basetype->type;
3560 return MONO_NATIVE_STRUCT;
3561 case MONO_TYPE_SZARRAY:
3562 case MONO_TYPE_ARRAY:
3564 switch (mspec->native) {
3565 case MONO_NATIVE_BYVALARRAY:
3566 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
3567 return MONO_NATIVE_BYVALARRAY;
3568 case MONO_NATIVE_SAFEARRAY:
3569 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
3570 return MONO_NATIVE_SAFEARRAY;
3571 case MONO_NATIVE_LPARRAY:
3572 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3573 return MONO_NATIVE_LPARRAY;
3575 g_error ("cant marshal array as native type %02x", mspec->native);
3579 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3580 return MONO_NATIVE_LPARRAY;
3581 case MONO_TYPE_I: return MONO_NATIVE_INT;
3582 case MONO_TYPE_U: return MONO_NATIVE_UINT;
3583 case MONO_TYPE_CLASS:
3584 case MONO_TYPE_OBJECT: {
3585 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
3587 switch (mspec->native) {
3588 case MONO_NATIVE_STRUCT:
3589 return MONO_NATIVE_STRUCT;
3590 case MONO_NATIVE_INTERFACE:
3591 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
3592 return MONO_NATIVE_INTERFACE;
3593 case MONO_NATIVE_IDISPATCH:
3594 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
3595 return MONO_NATIVE_IDISPATCH;
3596 case MONO_NATIVE_IUNKNOWN:
3597 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
3598 return MONO_NATIVE_IUNKNOWN;
3599 case MONO_NATIVE_FUNC:
3600 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3601 type->data.klass == mono_defaults.delegate_class ||
3602 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3603 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3604 return MONO_NATIVE_FUNC;
3610 g_error ("cant marshal object as native type %02x", mspec->native);
3613 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3614 type->data.klass == mono_defaults.delegate_class ||
3615 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3616 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3617 return MONO_NATIVE_FUNC;
3619 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
3620 return MONO_NATIVE_STRUCT;
3622 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
3623 case MONO_TYPE_TYPEDBYREF:
3625 g_error ("type 0x%02x not handled in marshal", t);
3627 return MONO_NATIVE_MAX;
3631 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
3634 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
3640 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
3641 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
3643 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3646 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
3650 method_from_method_def_or_ref (MonoImage *m, guint32 tok)
3652 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
3653 switch (tok & MONO_METHODDEFORREF_MASK) {
3654 case MONO_METHODDEFORREF_METHODDEF:
3655 return mono_get_method (m, MONO_TOKEN_METHOD_DEF | idx, NULL);
3656 case MONO_METHODDEFORREF_METHODREF:
3657 return mono_get_method (m, MONO_TOKEN_MEMBER_REF | idx, NULL);
3659 g_assert_not_reached ();
3664 mono_class_get_overrides (MonoImage *image, guint32 type_token, gint32 *num_overrides)
3667 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
3670 guint32 cols [MONO_METHODIMPL_SIZE];
3671 MonoMethod **result;
3680 loc.col_idx = MONO_METHODIMPL_CLASS;
3681 loc.idx = mono_metadata_token_index (type_token);
3683 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3689 * We may end up in the middle of the rows...
3692 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
3697 while (end < tdef->rows) {
3698 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
3704 result = g_new (MonoMethod*, num * 2);
3705 for (i = 0; i < num; ++i) {
3706 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
3707 result [i * 2] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_DECLARATION]);
3708 result [i * 2 + 1] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_BODY]);
3712 *num_overrides = num;
3717 * mono_guid_to_string:
3719 * Converts a 16 byte Microsoft GUID to the standard string representation.
3722 mono_guid_to_string (const guint8 *guid)
3724 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
3725 guid[3], guid[2], guid[1], guid[0],
3729 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
3733 get_constraints (MonoImage *image, int owner, MonoGenericContext *context)
3735 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
3736 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
3737 guint32 i, token, found;
3738 MonoClass *klass, **res;
3739 GList *cons = NULL, *tmp;
3743 for (i = 0; i < tdef->rows; ++i) {
3744 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
3745 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
3746 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
3747 klass = mono_class_get_full (image, token, context);
3748 cons = g_list_append (cons, klass);
3751 /* contiguous list finished */
3758 res = g_new0 (MonoClass*, found + 1);
3759 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
3760 res [i] = tmp->data;
3767 mono_metadata_has_generic_params (MonoImage *image, guint32 token)
3769 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
3770 guint32 cols [MONO_GENERICPARAM_SIZE];
3771 guint32 i, owner = 0;
3773 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
3774 owner = MONO_TYPEORMETHOD_TYPE;
3775 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3776 owner = MONO_TYPEORMETHOD_METHOD;
3778 g_error ("wrong token %x to load_generics_params", token);
3781 owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
3785 for (i = 0; i < tdef->rows; ++i) {
3786 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3787 if (cols [MONO_GENERICPARAM_OWNER] == owner)
3790 if (i >= tdef->rows)
3796 MonoGenericContainer *
3797 mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericContainer *parent_container)
3799 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
3800 guint32 cols [MONO_GENERICPARAM_SIZE];
3801 guint32 i, owner = 0, last_num, n;
3802 MonoGenericContainer *container;
3803 MonoGenericParam *params;
3805 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
3806 owner = MONO_TYPEORMETHOD_TYPE;
3807 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3808 owner = MONO_TYPEORMETHOD_METHOD;
3810 g_error ("wrong token %x to load_generics_params", token);
3813 owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
3817 for (i = 0; i < tdef->rows; ++i) {
3818 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3819 if (cols [MONO_GENERICPARAM_OWNER] == owner)
3823 if (i >= tdef->rows)
3827 container = g_new0 (MonoGenericContainer, 1);
3830 params = g_realloc (params, sizeof (MonoGenericParam) * n);
3831 params [n - 1].owner = container;
3832 params [n - 1].pklass = NULL;
3833 params [n - 1].method = NULL;
3834 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
3835 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
3836 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
3837 if (++i >= tdef->rows)
3839 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3840 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
3842 container->type_argc = n;
3843 container->type_params = params;
3844 container->parent = parent_container;
3846 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3847 container->is_method = 1;
3849 container->context.container = container;
3851 for (i = 0; i < n; i++)
3852 params [i].constraints = get_constraints (image, last_num + i + 1, &container->context);
3858 mono_type_is_byref (MonoType *type)
3864 mono_type_get_type (MonoType *type)
3869 /* For MONO_TYPE_FNPTR */
3870 MonoMethodSignature*
3871 mono_type_get_signature (MonoType *type)
3873 return type->data.method;
3876 /* For MONO_TYPE_CLASS, VALUETYPE */
3878 mono_type_get_class (MonoType *type)
3880 return type->data.klass;
3883 /* For MONO_TYPE_ARRAY */
3885 mono_type_get_array_type (MonoType *type)
3887 return type->data.array;
3891 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
3893 /* FIXME: implement */
3898 mono_signature_get_return_type (MonoMethodSignature *sig)
3904 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
3910 /* start from the first */
3911 if (sig->param_count) {
3912 *iter = &sig->params [0];
3913 return sig->params [0];
3921 if (type < &sig->params [sig->param_count]) {
3929 mono_signature_get_param_count (MonoMethodSignature *sig)
3931 return sig->param_count;
3935 mono_signature_get_call_conv (MonoMethodSignature *sig)
3937 return sig->call_convention;
3941 mono_signature_vararg_start (MonoMethodSignature *sig)
3943 return sig->sentinelpos;
3947 mono_signature_is_instance (MonoMethodSignature *sig)
3949 return sig->hasthis;
3953 mono_signature_explicit_this (MonoMethodSignature *sig)
3955 return sig->explicit_this;
3958 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
3960 mono_aligned_addr_hash (gconstpointer ptr)
3962 return GPOINTER_TO_UINT (ptr) >> 3;