2 * metadata.c: Routines for accessing the metadata
5 * Miguel de Icaza (miguel@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * (C) 2001-2002 Ximian, Inc.
17 #include "tabledefs.h"
18 #include "mono-endian.h"
20 #include "tokentype.h"
21 #include "metadata-internals.h"
22 #include "class-internals.h"
25 static void do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContext *generic_context,
26 const char *ptr, const char **rptr);
28 static gboolean do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only);
29 static gboolean mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only);
30 static gboolean _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2,
31 gboolean signature_only);
34 * This enumeration is used to describe the data types in the metadata
45 /* Index into Blob heap */
48 /* Index into String heap */
54 /* Pointer into a table */
57 /* HasConstant:Parent pointer (Param, Field or Property) */
60 /* HasCustomAttribute index. Indexes any table except CustomAttribute */
63 /* CustomAttributeType encoded index */
66 /* HasDeclSecurity index: TypeDef Method or Assembly */
69 /* Implementation coded index: File, Export AssemblyRef */
72 /* HasFieldMarshal coded index: Field or Param table */
75 /* MemberForwardedIndex: Field or Method */
78 /* TypeDefOrRef coded index: typedef, typeref, typespec */
81 /* MemberRefParent coded index: typeref, moduleref, method, memberref, typesepc, typedef */
84 /* MethodDefOrRef coded index: Method or Member Ref table */
87 /* HasSemantic coded index: Event or Property */
90 /* ResolutionScope coded index: Module, ModuleRef, AssemblytRef, TypeRef */
94 const static unsigned char AssemblySchema [] = {
95 MONO_MT_UINT32, /* "HashId" }, */
96 MONO_MT_UINT16, /* "Major" }, */
97 MONO_MT_UINT16, /* "Minor" }, */
98 MONO_MT_UINT16, /* "BuildNumber" }, */
99 MONO_MT_UINT16, /* "RevisionNumber" }, */
100 MONO_MT_UINT32, /* "Flags" }, */
101 MONO_MT_BLOB_IDX, /* "PublicKey" }, */
102 MONO_MT_STRING_IDX, /* "Name" }, */
103 MONO_MT_STRING_IDX, /* "Culture" }, */
107 const static unsigned char AssemblyOSSchema [] = {
108 MONO_MT_UINT32, /* "OSPlatformID" }, */
109 MONO_MT_UINT32, /* "OSMajor" }, */
110 MONO_MT_UINT32, /* "OSMinor" }, */
114 const static unsigned char AssemblyProcessorSchema [] = {
115 MONO_MT_UINT32, /* "Processor" }, */
119 const static unsigned char AssemblyRefSchema [] = {
120 MONO_MT_UINT16, /* "Major" }, */
121 MONO_MT_UINT16, /* "Minor" }, */
122 MONO_MT_UINT16, /* "Build" }, */
123 MONO_MT_UINT16, /* "Revision" }, */
124 MONO_MT_UINT32, /* "Flags" }, */
125 MONO_MT_BLOB_IDX, /* "PublicKeyOrToken" }, */
126 MONO_MT_STRING_IDX, /* "Name" }, */
127 MONO_MT_STRING_IDX, /* "Culture" }, */
128 MONO_MT_BLOB_IDX, /* "HashValue" }, */
132 const static unsigned char AssemblyRefOSSchema [] = {
133 MONO_MT_UINT32, /* "OSPlatformID" }, */
134 MONO_MT_UINT32, /* "OSMajorVersion" }, */
135 MONO_MT_UINT32, /* "OSMinorVersion" }, */
136 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
140 const static unsigned char AssemblyRefProcessorSchema [] = {
141 MONO_MT_UINT32, /* "Processor" }, */
142 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
146 const static unsigned char ClassLayoutSchema [] = {
147 MONO_MT_UINT16, /* "PackingSize" }, */
148 MONO_MT_UINT32, /* "ClassSize" }, */
149 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
153 const static unsigned char ConstantSchema [] = {
154 MONO_MT_UINT8, /* "Type" }, */
155 MONO_MT_UINT8, /* "PaddingZero" }, */
156 MONO_MT_CONST_IDX, /* "Parent" }, */
157 MONO_MT_BLOB_IDX, /* "Value" }, */
161 const static unsigned char CustomAttributeSchema [] = {
162 MONO_MT_HASCAT_IDX, /* "Parent" }, */
163 MONO_MT_CAT_IDX, /* "Type" }, */
164 MONO_MT_BLOB_IDX, /* "Value" }, */
168 const static unsigned char DeclSecuritySchema [] = {
169 MONO_MT_UINT16, /* "Action" }, */
170 MONO_MT_HASDEC_IDX, /* "Parent" }, */
171 MONO_MT_BLOB_IDX, /* "PermissionSet" }, */
175 const static unsigned char EventMapSchema [] = {
176 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
177 MONO_MT_TABLE_IDX, /* "EventList:Event" }, */
181 const static unsigned char EventSchema [] = {
182 MONO_MT_UINT16, /* "EventFlags#EventAttribute" }, */
183 MONO_MT_STRING_IDX, /* "Name" }, */
184 MONO_MT_TABLE_IDX, /* "EventType" }, TypeDef or TypeRef */
188 const static unsigned char ExportedTypeSchema [] = {
189 MONO_MT_UINT32, /* "Flags" }, */
190 MONO_MT_TABLE_IDX, /* "TypeDefId" }, */
191 MONO_MT_STRING_IDX, /* "TypeName" }, */
192 MONO_MT_STRING_IDX, /* "TypeNameSpace" }, */
193 MONO_MT_IMPL_IDX, /* "Implementation" }, */
197 const static unsigned char FieldSchema [] = {
198 MONO_MT_UINT16, /* "Flags" }, */
199 MONO_MT_STRING_IDX, /* "Name" }, */
200 MONO_MT_BLOB_IDX, /* "Signature" }, */
203 const static unsigned char FieldLayoutSchema [] = {
204 MONO_MT_UINT32, /* "Offset" }, */
205 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
209 const static unsigned char FieldMarshalSchema [] = {
210 MONO_MT_HFM_IDX, /* "Parent" }, */
211 MONO_MT_BLOB_IDX, /* "NativeType" }, */
214 const static unsigned char FieldRVASchema [] = {
215 MONO_MT_UINT32, /* "RVA" }, */
216 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
220 const static unsigned char FileSchema [] = {
221 MONO_MT_UINT32, /* "Flags" }, */
222 MONO_MT_STRING_IDX, /* "Name" }, */
223 MONO_MT_BLOB_IDX, /* "Value" }, */
227 const static unsigned char ImplMapSchema [] = {
228 MONO_MT_UINT16, /* "MappingFlag" }, */
229 MONO_MT_MF_IDX, /* "MemberForwarded" }, */
230 MONO_MT_STRING_IDX, /* "ImportName" }, */
231 MONO_MT_TABLE_IDX, /* "ImportScope:ModuleRef" }, */
235 const static unsigned char InterfaceImplSchema [] = {
236 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
237 MONO_MT_TDOR_IDX, /* "Interface=TypeDefOrRef" }, */
241 const static unsigned char ManifestResourceSchema [] = {
242 MONO_MT_UINT32, /* "Offset" }, */
243 MONO_MT_UINT32, /* "Flags" }, */
244 MONO_MT_STRING_IDX, /* "Name" }, */
245 MONO_MT_IMPL_IDX, /* "Implementation" }, */
249 const static unsigned char MemberRefSchema [] = {
250 MONO_MT_MRP_IDX, /* "Class" }, */
251 MONO_MT_STRING_IDX, /* "Name" }, */
252 MONO_MT_BLOB_IDX, /* "Signature" }, */
256 const static unsigned char MethodSchema [] = {
257 MONO_MT_UINT32, /* "RVA" }, */
258 MONO_MT_UINT16, /* "ImplFlags#MethodImplAttributes" }, */
259 MONO_MT_UINT16, /* "Flags#MethodAttribute" }, */
260 MONO_MT_STRING_IDX, /* "Name" }, */
261 MONO_MT_BLOB_IDX, /* "Signature" }, */
262 MONO_MT_TABLE_IDX, /* "ParamList:Param" }, */
266 const static unsigned char MethodImplSchema [] = {
267 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
268 MONO_MT_MDOR_IDX, /* "MethodBody" }, */
269 MONO_MT_MDOR_IDX, /* "MethodDeclaration" }, */
273 const static unsigned char MethodSemanticsSchema [] = {
274 MONO_MT_UINT16, /* "MethodSemantic" }, */
275 MONO_MT_TABLE_IDX, /* "Method:Method" }, */
276 MONO_MT_HS_IDX, /* "Association" }, */
280 const static unsigned char ModuleSchema [] = {
281 MONO_MT_UINT16, /* "Generation" }, */
282 MONO_MT_STRING_IDX, /* "Name" }, */
283 MONO_MT_GUID_IDX, /* "MVID" }, */
284 MONO_MT_GUID_IDX, /* "EncID" }, */
285 MONO_MT_GUID_IDX, /* "EncBaseID" }, */
289 const static unsigned char ModuleRefSchema [] = {
290 MONO_MT_STRING_IDX, /* "Name" }, */
294 const static unsigned char NestedClassSchema [] = {
295 MONO_MT_TABLE_IDX, /* "NestedClass:TypeDef" }, */
296 MONO_MT_TABLE_IDX, /* "EnclosingClass:TypeDef" }, */
300 const static unsigned char ParamSchema [] = {
301 MONO_MT_UINT16, /* "Flags" }, */
302 MONO_MT_UINT16, /* "Sequence" }, */
303 MONO_MT_STRING_IDX, /* "Name" }, */
307 const static unsigned char PropertySchema [] = {
308 MONO_MT_UINT16, /* "Flags" }, */
309 MONO_MT_STRING_IDX, /* "Name" }, */
310 MONO_MT_BLOB_IDX, /* "Type" }, */
314 const static unsigned char PropertyMapSchema [] = {
315 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
316 MONO_MT_TABLE_IDX, /* "PropertyList:Property" }, */
320 const static unsigned char StandaloneSigSchema [] = {
321 MONO_MT_BLOB_IDX, /* "Signature" }, */
325 const static unsigned char TypeDefSchema [] = {
326 MONO_MT_UINT32, /* "Flags" }, */
327 MONO_MT_STRING_IDX, /* "Name" }, */
328 MONO_MT_STRING_IDX, /* "Namespace" }, */
329 MONO_MT_TDOR_IDX, /* "Extends" }, */
330 MONO_MT_TABLE_IDX, /* "FieldList:Field" }, */
331 MONO_MT_TABLE_IDX, /* "MethodList:Method" }, */
335 const static unsigned char TypeRefSchema [] = {
336 MONO_MT_RS_IDX, /* "ResolutionScope=ResolutionScope" }, */
337 MONO_MT_STRING_IDX, /* "Name" }, */
338 MONO_MT_STRING_IDX, /* "Namespace" }, */
342 const static unsigned char TypeSpecSchema [] = {
343 MONO_MT_BLOB_IDX, /* "Signature" }, */
347 const static unsigned char GenericParamSchema [] = {
348 MONO_MT_UINT16, /* "Number" }, */
349 MONO_MT_UINT16, /* "Flags" }, */
350 MONO_MT_TABLE_IDX, /* "Owner" }, TypeDef or MethodDef */
351 MONO_MT_STRING_IDX, /* "Name" }, */
356 const static unsigned char MethodSpecSchema [] = {
357 MONO_MT_MDOR_IDX, /* "Method" }, */
358 MONO_MT_BLOB_IDX, /* "Signature" }, */
362 const static unsigned char GenericParamConstraintSchema [] = {
363 MONO_MT_TABLE_IDX, /* "GenericParam" }, */
364 MONO_MT_TDOR_IDX, /* "Constraint" }, */
368 const static struct {
369 const unsigned char *description;
372 /* 0 */ { ModuleSchema, "Module" },
373 /* 1 */ { TypeRefSchema, "TypeRef" },
374 /* 2 */ { TypeDefSchema, "TypeDef" },
375 /* 3 */ { NULL, NULL },
376 /* 4 */ { FieldSchema, "Field" },
377 /* 5 */ { NULL, NULL },
378 /* 6 */ { MethodSchema, "Method" },
379 /* 7 */ { NULL, NULL },
380 /* 8 */ { ParamSchema, "Param" },
381 /* 9 */ { InterfaceImplSchema, "InterfaceImpl" },
382 /* A */ { MemberRefSchema, "MemberRef" },
383 /* B */ { ConstantSchema, "Constant" },
384 /* C */ { CustomAttributeSchema, "CustomAttribute" },
385 /* D */ { FieldMarshalSchema, "FieldMarshal" },
386 /* E */ { DeclSecuritySchema, "DeclSecurity" },
387 /* F */ { ClassLayoutSchema, "ClassLayout" },
388 /* 10 */ { FieldLayoutSchema, "FieldLayout" },
389 /* 11 */ { StandaloneSigSchema, "StandaloneSig" },
390 /* 12 */ { EventMapSchema, "EventMap" },
391 /* 13 */ { NULL, NULL },
392 /* 14 */ { EventSchema, "Event" },
393 /* 15 */ { PropertyMapSchema, "PropertyMap" },
394 /* 16 */ { NULL, NULL },
395 /* 17 */ { PropertySchema, "PropertyTable" },
396 /* 18 */ { MethodSemanticsSchema, "MethodSemantics" },
397 /* 19 */ { MethodImplSchema, "MethodImpl" },
398 /* 1A */ { ModuleRefSchema, "ModuleRef" },
399 /* 1B */ { TypeSpecSchema, "TypeSpec" },
400 /* 1C */ { ImplMapSchema, "ImplMap" },
401 /* 1D */ { FieldRVASchema, "FieldRVA" },
402 /* 1E */ { NULL, NULL },
403 /* 1F */ { NULL, NULL },
404 /* 20 */ { AssemblySchema, "Assembly" },
405 /* 21 */ { AssemblyProcessorSchema, "AssemblyProcessor" },
406 /* 22 */ { AssemblyOSSchema, "AssemblyOS" },
407 /* 23 */ { AssemblyRefSchema, "AssemblyRef" },
408 /* 24 */ { AssemblyRefProcessorSchema, "AssemblyRefProcessor" },
409 /* 25 */ { AssemblyRefOSSchema, "AssemblyRefOS" },
410 /* 26 */ { FileSchema, "File" },
411 /* 27 */ { ExportedTypeSchema, "ExportedType" },
412 /* 28 */ { ManifestResourceSchema, "ManifestResource" },
413 /* 29 */ { NestedClassSchema, "NestedClass" },
414 /* 2A */ { GenericParamSchema, "GenericParam" },
415 /* 2B */ { MethodSpecSchema, "MethodSpec" },
416 /* 2C */ { GenericParamConstraintSchema, "GenericParamConstraint" },
420 * mono_meta_table_name:
421 * @table: table index
423 * Returns: the name for the @table index
426 mono_meta_table_name (int table)
428 if ((table < 0) || (table > 0x2c))
431 return tables [table].name;
434 /* The guy who wrote the spec for this should not be allowed near a
437 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
438 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
439 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
440 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
441 inverse of this mapping.
444 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
445 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
447 /* Reference: Partition II - 23.2.6 */
449 * mono_metadata_compute_size:
450 * @meta: metadata context
451 * @tableindex: metadata table number
452 * @result_bitfield: pointer to guint32 where to store additional info
454 * mono_metadata_compute_size() computes the lenght in bytes of a single
455 * row in a metadata table. The size of each column is encoded in the
456 * @result_bitfield return value along with the number of columns in the table.
457 * the resulting bitfield should be handed to the mono_metadata_table_size()
458 * and mono_metadata_table_count() macros.
459 * This is a Mono runtime internal only function.
462 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
464 guint32 bitfield = 0;
465 int size = 0, field_size = 0;
468 const unsigned char *description = tables [tableindex].description;
470 for (i = 0; (code = description [i]) != MONO_MT_END; i++){
473 field_size = 4; break;
476 field_size = 2; break;
479 field_size = 1; break;
481 case MONO_MT_BLOB_IDX:
482 field_size = meta->idx_blob_wide ? 4 : 2; break;
484 case MONO_MT_STRING_IDX:
485 field_size = meta->idx_string_wide ? 4 : 2; break;
487 case MONO_MT_GUID_IDX:
488 field_size = meta->idx_guid_wide ? 4 : 2; break;
490 case MONO_MT_TABLE_IDX:
491 /* Uhm, a table index can point to other tables besides the current one
492 * so, it's not correct to use the rowcount of the current table to
493 * get the size for this column - lupus
495 switch (tableindex) {
496 case MONO_TABLE_ASSEMBLYREFOS:
498 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
499 case MONO_TABLE_ASSEMBLYPROCESSOR:
501 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
502 case MONO_TABLE_CLASSLAYOUT:
504 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
505 case MONO_TABLE_EVENTMAP:
506 g_assert (i == 0 || i == 1);
507 field_size = i ? idx_size (MONO_TABLE_EVENT):
508 idx_size(MONO_TABLE_TYPEDEF);
510 case MONO_TABLE_EVENT:
512 field_size = MAX (idx_size (MONO_TABLE_TYPEDEF), idx_size(MONO_TABLE_TYPEREF));
513 field_size = MAX (field_size, idx_size(MONO_TABLE_TYPESPEC));
515 case MONO_TABLE_EXPORTEDTYPE:
517 /* the index is in another metadata file, so it must be 4 */
518 field_size = 4; break;
519 case MONO_TABLE_FIELDLAYOUT:
521 field_size = idx_size (MONO_TABLE_FIELD); break;
522 case MONO_TABLE_FIELDRVA:
524 field_size = idx_size (MONO_TABLE_FIELD); break;
525 case MONO_TABLE_IMPLMAP:
527 field_size = idx_size (MONO_TABLE_MODULEREF); break;
528 case MONO_TABLE_INTERFACEIMPL:
530 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
531 case MONO_TABLE_METHOD:
533 field_size = idx_size (MONO_TABLE_PARAM); break;
534 case MONO_TABLE_METHODIMPL:
536 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
537 case MONO_TABLE_METHODSEMANTICS:
539 field_size = idx_size (MONO_TABLE_METHOD); break;
540 case MONO_TABLE_NESTEDCLASS:
541 g_assert (i == 0 || i == 1);
542 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
543 case MONO_TABLE_PROPERTYMAP:
544 g_assert (i == 0 || i == 1);
545 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
546 idx_size(MONO_TABLE_TYPEDEF);
548 case MONO_TABLE_TYPEDEF:
549 g_assert (i == 4 || i == 5);
550 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
551 idx_size(MONO_TABLE_METHOD);
553 case MONO_TABLE_GENERICPARAM:
554 g_assert (i == 2 || i == 4 || i == 5);
556 field_size = MAX (idx_size (MONO_TABLE_METHOD), idx_size (MONO_TABLE_TYPEDEF));
558 field_size = idx_size (MONO_TABLE_TYPEDEF);
560 field_size = idx_size (MONO_TABLE_TYPEDEF);
563 case MONO_TABLE_GENERICPARAMCONSTRAINT:
565 field_size = idx_size (MONO_TABLE_GENERICPARAM);
569 g_assert_not_reached ();
574 * HasConstant: ParamDef, FieldDef, Property
576 case MONO_MT_CONST_IDX:
577 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
578 meta->tables [MONO_TABLE_FIELD].rows);
579 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
581 /* 2 bits to encode tag */
582 field_size = rtsize (n, 16-2);
586 * HasCustomAttribute: points to any table but
589 case MONO_MT_HASCAT_IDX:
591 * We believe that since the signature and
592 * permission are indexing the Blob heap,
593 * we should consider the blob size first
595 /* I'm not a believer - lupus
596 if (meta->idx_blob_wide){
601 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
602 meta->tables [MONO_TABLE_FIELD].rows);
603 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
604 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
605 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
606 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
607 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
608 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
609 n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
610 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
611 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
612 n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
613 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
614 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
615 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
616 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
617 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
618 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
619 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
621 /* 5 bits to encode */
622 field_size = rtsize (n, 16-5);
626 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
627 * MemberRef and String.
629 case MONO_MT_CAT_IDX:
630 /* String is a heap, if it is wide, we know the size */
632 if (meta->idx_string_wide){
637 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
638 meta->tables [MONO_TABLE_TYPEDEF].rows);
639 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
640 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
642 /* 3 bits to encode */
643 field_size = rtsize (n, 16-3);
647 * HasDeclSecurity: Typedef, MethodDef, Assembly
649 case MONO_MT_HASDEC_IDX:
650 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
651 meta->tables [MONO_TABLE_METHOD].rows);
652 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
654 /* 2 bits to encode */
655 field_size = rtsize (n, 16-2);
659 * Implementation: File, AssemblyRef, ExportedType
661 case MONO_MT_IMPL_IDX:
662 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
663 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
664 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
666 /* 2 bits to encode tag */
667 field_size = rtsize (n, 16-2);
671 * HasFieldMarshall: FieldDef, ParamDef
673 case MONO_MT_HFM_IDX:
674 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
675 meta->tables [MONO_TABLE_PARAM].rows);
677 /* 1 bit used to encode tag */
678 field_size = rtsize (n, 16-1);
682 * MemberForwarded: FieldDef, MethodDef
685 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
686 meta->tables [MONO_TABLE_METHOD].rows);
688 /* 1 bit used to encode tag */
689 field_size = rtsize (n, 16-1);
693 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
695 * It is TypeDef, _TypeRef_, TypeSpec, instead.
697 case MONO_MT_TDOR_IDX:
698 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
699 meta->tables [MONO_TABLE_TYPEREF].rows);
700 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
702 /* 2 bits to encode */
703 field_size = rtsize (n, 16-2);
707 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
709 case MONO_MT_MRP_IDX:
710 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
711 meta->tables [MONO_TABLE_TYPEREF].rows);
712 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
713 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
714 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
715 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
717 /* 3 bits to encode */
718 field_size = rtsize (n, 16 - 3);
722 * MethodDefOrRef: MethodDef, MemberRef
724 case MONO_MT_MDOR_IDX:
725 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
726 meta->tables [MONO_TABLE_MEMBERREF].rows);
728 /* 1 bit used to encode tag */
729 field_size = rtsize (n, 16-1);
733 * HasSemantics: Property, Event
736 n = MAX (meta->tables [MONO_TABLE_PROPERTY].rows,
737 meta->tables [MONO_TABLE_EVENT].rows);
739 /* 1 bit used to encode tag */
740 field_size = rtsize (n, 16-1);
744 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
747 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
748 meta->tables [MONO_TABLE_MODULEREF].rows);
749 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
750 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
752 /* 2 bits used to encode tag (ECMA spec claims 3) */
753 field_size = rtsize (n, 16 - 2);
758 * encode field size as follows (we just need to
765 bitfield |= (field_size-1) << shift;
768 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
771 *result_bitfield = (i << 24) | bitfield;
776 * mono_metadata_compute_table_bases:
777 * @meta: metadata context to compute table values
779 * Computes the table bases for the metadata structure.
780 * This is an internal function used by the image loader code.
783 mono_metadata_compute_table_bases (MonoImage *meta)
786 const char *base = meta->tables_base;
788 for (i = 0; i < MONO_TABLE_NUM; i++) {
789 MonoTableInfo *table = &meta->tables [i];
790 if (table->rows == 0)
793 table->row_size = mono_metadata_compute_size (meta, i, &table->size_bitfield);
795 base += table->rows * table->row_size;
800 * mono_metadata_locate:
801 * @meta: metadata context
802 * @table: table code.
803 * @idx: index of element to retrieve from @table.
805 * Returns: a pointer to the @idx element in the metadata table
806 * whose code is @table.
809 mono_metadata_locate (MonoImage *meta, int table, int idx)
811 /* idx == 0 refers always to NULL */
812 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
814 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
818 * mono_metadata_locate_token:
819 * @meta: metadata context
820 * @token: metadata token
822 * Returns: a pointer to the data in the metadata represented by the
826 mono_metadata_locate_token (MonoImage *meta, guint32 token)
828 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
832 * mono_metadata_string_heap:
833 * @meta: metadata context
834 * @index: index into the string heap.
836 * Returns: an in-memory pointer to the @index in the string heap.
839 mono_metadata_string_heap (MonoImage *meta, guint32 index)
841 g_return_val_if_fail (index < meta->heap_strings.size, "");
842 return meta->heap_strings.data + index;
846 * mono_metadata_user_string:
847 * @meta: metadata context
848 * @index: index into the user string heap.
850 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
853 mono_metadata_user_string (MonoImage *meta, guint32 index)
855 g_return_val_if_fail (index < meta->heap_us.size, "");
856 return meta->heap_us.data + index;
860 * mono_metadata_blob_heap:
861 * @meta: metadata context
862 * @index: index into the blob.
864 * Returns: an in-memory pointer to the @index in the Blob heap.
867 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
869 g_return_val_if_fail (index < meta->heap_blob.size, "");
870 return meta->heap_blob.data + index;
874 * mono_metadata_guid_heap:
875 * @meta: metadata context
876 * @index: index into the guid heap.
878 * Returns: an in-memory pointer to the @index in the guid heap.
881 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
884 index *= 16; /* adjust for guid size and 1-based index */
885 g_return_val_if_fail (index < meta->heap_guid.size, "");
886 return meta->heap_guid.data + index;
890 dword_align (const char *ptr)
892 #if SIZEOF_VOID_P == 8
893 return (const char *) (((guint64) (ptr + 3)) & ~3);
895 return (const char *) (((guint32) (ptr + 3)) & ~3);
900 * mono_metadata_decode_row:
901 * @t: table to extract information from.
902 * @idx: index in table.
903 * @res: array of @res_size cols to store the results in
905 * This decompresses the metadata element @idx in table @t
906 * into the guint32 @res array that has res_size elements
909 mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res_size)
911 guint32 bitfield = t->size_bitfield;
912 int i, count = mono_metadata_table_count (bitfield);
913 const char *data = t->base + idx * t->row_size;
915 g_assert (res_size == count);
917 for (i = 0; i < count; i++) {
918 int n = mono_metadata_table_size (bitfield, i);
922 res [i] = *data; break;
924 res [i] = read16 (data); break;
926 res [i] = read32 (data); break;
928 g_assert_not_reached ();
935 * mono_metadata_decode_row_col:
936 * @t: table to extract information from.
937 * @idx: index for row in table.
938 * @col: column in the row.
940 * This function returns the value of column @col from the @idx
941 * row in the table @t.
944 mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col)
946 guint32 bitfield = t->size_bitfield;
948 register const char *data = t->base + idx * t->row_size;
951 g_assert (col < mono_metadata_table_count (bitfield));
953 n = mono_metadata_table_size (bitfield, 0);
954 for (i = 0; i < col; ++i) {
956 n = mono_metadata_table_size (bitfield, i + 1);
962 return read16 (data);
964 return read32 (data);
966 g_assert_not_reached ();
971 * mono_metadata_decode_blob_size:
972 * @ptr: pointer to a blob object
973 * @rptr: the new position of the pointer
975 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
977 * Returns: the size of the blob object
980 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
982 const unsigned char *ptr = (const unsigned char *)xptr;
985 if ((*ptr & 0x80) == 0){
986 size = ptr [0] & 0x7f;
988 } else if ((*ptr & 0x40) == 0){
989 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
992 size = ((ptr [0] & 0x1f) << 24) +
1005 * mono_metadata_decode_value:
1006 * @ptr: pointer to decode from
1007 * @rptr: the new position of the pointer
1009 * This routine decompresses 32-bit values as specified in the "Blob and
1010 * Signature" section (22.2)
1012 * Returns: the decoded value
1015 mono_metadata_decode_value (const char *_ptr, const char **rptr)
1017 const unsigned char *ptr = (const unsigned char *) _ptr;
1018 unsigned char b = *ptr;
1021 if ((b & 0x80) == 0){
1024 } else if ((b & 0x40) == 0){
1025 len = ((b & 0x3f) << 8 | ptr [1]);
1028 len = ((b & 0x1f) << 24) |
1041 * mono_metadata_parse_typedef_or_ref:
1042 * @m: a metadata context.
1043 * @ptr: a pointer to an encoded TypedefOrRef in @m
1044 * @rptr: pointer updated to match the end of the decoded stream
1046 * Returns: a token valid in the @m metadata decoded from
1047 * the compressed representation.
1050 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1053 token = mono_metadata_decode_value (ptr, &ptr);
1056 return mono_metadata_token_from_dor (token);
1060 * mono_metadata_parse_custom_mod:
1061 * @m: a metadata context.
1062 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1063 * @ptr: a pointer to (possibly) the start of a custom modifier list
1064 * @rptr: pointer updated to match the end of the decoded stream
1066 * Checks if @ptr points to a type custom modifier compressed representation.
1068 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1071 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1073 MonoCustomMod local;
1074 if ((*ptr == MONO_TYPE_CMOD_OPT) || (*ptr == MONO_TYPE_CMOD_REQD)) {
1077 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1078 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1085 * mono_metadata_parse_array:
1086 * @m: a metadata context.
1087 * @ptr: a pointer to an encoded array description.
1088 * @rptr: pointer updated to match the end of the decoded stream
1090 * Decodes the compressed array description found in the metadata @m at @ptr.
1092 * Returns: a #MonoArrayType structure describing the array type
1096 mono_metadata_parse_array_full (MonoImage *m, MonoGenericContext *generic_context,
1097 const char *ptr, const char **rptr)
1100 MonoArrayType *array = g_new0 (MonoArrayType, 1);
1103 etype = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_TYPE, 0, ptr, &ptr);
1104 array->eklass = mono_class_from_mono_type (etype);
1105 array->rank = mono_metadata_decode_value (ptr, &ptr);
1107 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1108 if (array->numsizes)
1109 array->sizes = g_new0 (int, array->numsizes);
1110 for (i = 0; i < array->numsizes; ++i)
1111 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1113 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1114 if (array->numlobounds)
1115 array->lobounds = g_new0 (int, array->numlobounds);
1116 for (i = 0; i < array->numlobounds; ++i)
1117 array->lobounds [i] = mono_metadata_decode_value (ptr, &ptr);
1125 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1127 return mono_metadata_parse_array_full (m, NULL, ptr, rptr);
1131 * mono_metadata_free_array:
1132 * @array: array description
1134 * Frees the array description returned from mono_metadata_parse_array().
1137 mono_metadata_free_array (MonoArrayType *array)
1139 g_free (array->sizes);
1140 g_free (array->lobounds);
1145 * need to add common field and param attributes combinations:
1148 * public static literal
1151 * private static literal
1153 static const MonoType
1155 /* data, attrs, type, nmods, byref, pinned */
1156 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1157 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1158 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1159 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1160 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1161 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1162 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1163 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1164 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1165 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1166 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1167 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1168 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1169 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1170 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1171 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1172 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1173 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1174 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1175 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1176 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1177 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1178 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1179 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1180 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1181 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1182 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1183 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 0, 0},
1184 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 1, 0},
1185 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1186 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1187 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1188 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1189 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1192 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1194 static GHashTable *type_cache = NULL;
1195 static GHashTable *generic_inst_cache = NULL;
1196 static GHashTable *generic_class_cache = NULL;
1197 static int next_generic_inst_id = 0;
1199 static guint mono_generic_class_hash (gconstpointer data);
1202 * MonoTypes with modifies are never cached, so we never check or use that field.
1205 mono_type_hash (gconstpointer data)
1207 const MonoType *type = (const MonoType *) data;
1208 if (type->type == MONO_TYPE_GENERICINST)
1209 return mono_generic_class_hash (type->data.generic_class);
1211 return type->type | (type->byref << 8) | (type->attrs << 9);
1215 mono_type_equal (gconstpointer ka, gconstpointer kb)
1217 const MonoType *a = (const MonoType *) ka;
1218 const MonoType *b = (const MonoType *) kb;
1220 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1222 /* need other checks */
1227 mono_generic_inst_hash (gconstpointer data)
1229 const MonoGenericInst *ginst = (const MonoGenericInst *) data;
1233 for (i = 0; i < ginst->type_argc; ++i) {
1235 hash += mono_metadata_type_hash (ginst->type_argv [i]);
1238 return hash ^ (ginst->is_open << 8);
1242 mono_generic_inst_equal (gconstpointer ka, gconstpointer kb)
1244 const MonoGenericInst *a = (const MonoGenericInst *) ka;
1245 const MonoGenericInst *b = (const MonoGenericInst *) kb;
1248 if ((a->is_open != b->is_open) || (a->type_argc != b->type_argc) || (a->is_reference != b->is_reference))
1250 for (i = 0; i < a->type_argc; ++i) {
1251 if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], FALSE))
1258 mono_generic_class_hash (gconstpointer data)
1260 const MonoGenericClass *gclass = (const MonoGenericClass *) data;
1261 guint hash = mono_metadata_type_hash (&gclass->container_class->byval_arg);
1264 hash += mono_generic_inst_hash (gclass->inst);
1270 mono_generic_class_equal (gconstpointer ka, gconstpointer kb)
1272 const MonoGenericClass *a = (const MonoGenericClass *) ka;
1273 const MonoGenericClass *b = (const MonoGenericClass *) kb;
1275 return _mono_metadata_generic_class_equal (a, b, FALSE);
1279 * mono_metadata_init:
1281 * Initialize the global variables of this module.
1282 * This is a Mono runtime internal function.
1285 mono_metadata_init (void)
1289 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1290 generic_inst_cache = g_hash_table_new (mono_generic_inst_hash, mono_generic_inst_equal);
1291 generic_class_cache = g_hash_table_new (mono_generic_class_hash, mono_generic_class_equal);
1293 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1294 g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
1298 * mono_metadata_parse_type:
1299 * @m: metadata context
1300 * @mode: king of type that may be found at @ptr
1301 * @opt_attrs: optional attributes to store in the returned type
1302 * @ptr: pointer to the type representation
1303 * @rptr: pointer updated to match the end of the decoded stream
1305 * Decode a compressed type description found at @ptr in @m.
1306 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1307 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1308 * This function can be used to decode type descriptions in method signatures,
1309 * field signatures, locals signatures etc.
1311 * To parse a generic type, `generic_container' points to the current class'es
1312 * (the `generic_container' field in the MonoClass) or the current generic method's
1313 * (the `generic_container' field in the MonoMethodNormal) generic container.
1314 * When we encounter any MONO_TYPE_VAR or MONO_TYPE_MVAR's, they're looked up in
1315 * this MonoGenericContainer.
1316 * This is a Mono runtime internal function.
1318 * Returns: a #MonoType structure representing the decoded type.
1321 mono_metadata_parse_type_full (MonoImage *m, MonoGenericContext *generic_context, MonoParseTypeMode mode,
1322 short opt_attrs, const char *ptr, const char **rptr)
1324 MonoType *type, *cached;
1326 gboolean byref = FALSE;
1327 gboolean pinned = FALSE;
1328 const char *tmp_ptr;
1333 * According to the spec, custom modifiers should come before the byref
1334 * flag, but the IL produced by ilasm from the following signature:
1335 * object modopt(...) &
1336 * starts with a byref flag, followed by the modifiers. (bug #49802)
1337 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1338 * it would be better to treat byref as real type constructor instead of
1340 * Also, pinned should come before anything else, but some MSV++ produced
1341 * assemblies violate this (#bug 61990).
1344 /* Count the modifiers first */
1349 case MONO_TYPE_PINNED:
1350 case MONO_TYPE_BYREF:
1353 case MONO_TYPE_CMOD_REQD:
1354 case MONO_TYPE_CMOD_OPT:
1356 mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1364 type = g_malloc0 (sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1365 type->num_mods = count;
1367 g_warning ("got more than 64 modifiers in type");
1371 memset (type, 0, sizeof (MonoType));
1374 /* Parse pinned, byref and custom modifiers */
1379 case MONO_TYPE_PINNED:
1383 case MONO_TYPE_BYREF:
1387 case MONO_TYPE_CMOD_REQD:
1388 case MONO_TYPE_CMOD_OPT:
1389 mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1397 type->attrs = opt_attrs;
1398 type->byref = byref;
1399 type->pinned = pinned ? 1 : 0;
1401 do_mono_metadata_parse_type (type, m, generic_context, ptr, &ptr);
1407 /* FIXME: remove the != MONO_PARSE_PARAM condition, this accounts for
1408 * almost 10k (about 2/3rds) of all MonoType's we create.
1410 if (mode != MONO_PARSE_PARAM && !type->num_mods) {
1411 /* no need to free type here, because it is on the stack */
1412 if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
1414 return &type->data.klass->this_arg;
1416 return &type->data.klass->byval_arg;
1418 /* No need to use locking since nobody is modifying the hash table */
1419 if ((cached = g_hash_table_lookup (type_cache, type)))
1423 /*printf ("%x%c %s\n", type->attrs, type->pinned ? 'p' : ' ', mono_type_full_name (type));*/
1426 type = g_memdup (&stype, sizeof (MonoType));
1431 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
1432 const char *ptr, const char **rptr)
1434 return mono_metadata_parse_type_full (m, NULL, mode, opt_attrs, ptr, rptr);
1438 * mono_metadata_parse_signature_full:
1439 * @image: metadata context
1440 * @generic_container: generic container
1441 * @toke: metadata token
1443 * Decode a method signature stored in the STANDALONESIG table
1445 * Returns: a MonoMethodSignature describing the signature.
1447 MonoMethodSignature*
1448 mono_metadata_parse_signature_full (MonoImage *image, MonoGenericContainer *generic_container, guint32 token)
1450 MonoTableInfo *tables = image->tables;
1451 guint32 idx = mono_metadata_token_index (token);
1456 return mono_lookup_dynamic_token (image, token);
1458 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1460 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1462 ptr = mono_metadata_blob_heap (image, sig);
1463 mono_metadata_decode_blob_size (ptr, &ptr);
1465 return mono_metadata_parse_method_signature_full (image, generic_container, FALSE, ptr, NULL);
1469 * mono_metadata_parse_signature:
1470 * @image: metadata context
1471 * @toke: metadata token
1473 * Decode a method signature stored in the STANDALONESIG table
1475 * Returns: a MonoMethodSignature describing the signature.
1477 MonoMethodSignature*
1478 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1480 return mono_metadata_parse_signature_full (image, NULL, token);
1484 * mono_metadata_signature_alloc:
1485 * @image: metadata context
1486 * @nparmas: number of parameters in the signature
1488 * Allocate a MonoMethodSignature structure with the specified number of params.
1489 * The return type and the params types need to be filled later.
1490 * This is a Mono runtime internal function.
1492 * Returns: the new MonoMethodSignature structure.
1494 MonoMethodSignature*
1495 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1497 MonoMethodSignature *sig;
1499 /* later we want to allocate signatures with mempools */
1500 sig = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1501 sig->param_count = nparams;
1502 sig->sentinelpos = -1;
1508 * mono_metadata_signature_dup:
1509 * @sig: method signature
1511 * Duplicate an existing MonoMethodSignature so it can be modified.
1512 * This is a Mono runtime internal function.
1514 * Returns: the new MonoMethodSignature structure.
1516 MonoMethodSignature*
1517 mono_metadata_signature_dup (MonoMethodSignature *sig)
1521 sigsize = sizeof (MonoMethodSignature) + sig->param_count * sizeof (MonoType *);
1522 return g_memdup (sig, sigsize);
1526 * mono_metadata_parse_method_signature:
1527 * @m: metadata context
1528 * @generic_container: generics container
1529 * @def: the MethodDef index or 0 for Ref signatures.
1530 * @ptr: pointer to the signature metadata representation
1531 * @rptr: pointer updated to match the end of the decoded stream
1533 * Decode a method signature stored at @ptr.
1534 * This is a Mono runtime internal function.
1536 * Returns: a MonoMethodSignature describing the signature.
1538 MonoMethodSignature *
1539 mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContainer *generic_container,
1540 int def, const char *ptr, const char **rptr)
1542 MonoMethodSignature *method;
1543 int i, ret_attrs = 0, *pattrs = NULL;
1544 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1545 guint32 gen_param_count = 0;
1546 gboolean is_open = FALSE;
1547 MonoGenericContainer *container;
1550 gen_param_count = 1;
1555 call_convention = *ptr & 0x0F;
1557 if (gen_param_count)
1558 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1559 param_count = mono_metadata_decode_value (ptr, &ptr);
1560 pattrs = g_new0 (int, param_count);
1563 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1564 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1565 guint32 cols [MONO_PARAM_SIZE];
1566 guint lastp, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1568 if (def < methodt->rows)
1569 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1571 lastp = paramt->rows + 1;
1572 for (i = param_index; i < lastp; ++i) {
1573 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1574 if (!cols [MONO_PARAM_SEQUENCE])
1575 ret_attrs = cols [MONO_PARAM_FLAGS];
1577 pattrs [cols [MONO_PARAM_SEQUENCE] - 1] = cols [MONO_PARAM_FLAGS];
1580 method = mono_metadata_signature_alloc (m, param_count);
1581 method->hasthis = hasthis;
1582 method->explicit_this = explicit_this;
1583 method->call_convention = call_convention;
1584 method->generic_param_count = gen_param_count;
1586 if (gen_param_count)
1587 method->has_type_parameters = 1;
1589 if (gen_param_count && (!generic_container || !generic_container->is_method)) {
1590 container = g_new0 (MonoGenericContainer, 1);
1592 container->parent = generic_container;
1593 container->is_signature = 1;
1595 container->context.container = container;
1597 container->type_argc = gen_param_count;
1598 container->type_params = g_new0 (MonoGenericParam, gen_param_count);
1600 for (i = 0; i < gen_param_count; i++) {
1601 container->type_params [i].owner = container;
1602 container->type_params [i].num = i;
1605 container = generic_container;
1608 if (call_convention != 0xa) {
1609 method->ret = mono_metadata_parse_type_full (m, (MonoGenericContext *) container, MONO_PARSE_RET, ret_attrs, ptr, &ptr);
1610 is_open = mono_class_is_open_constructed_type (method->ret);
1613 if (method->param_count) {
1614 method->sentinelpos = -1;
1616 for (i = 0; i < method->param_count; ++i) {
1617 if (*ptr == MONO_TYPE_SENTINEL) {
1618 if (method->call_convention != MONO_CALL_VARARG || def)
1619 g_error ("found sentinel for methoddef or no vararg method");
1620 method->sentinelpos = i;
1623 method->params [i] = mono_metadata_parse_type_full (
1624 m, (MonoGenericContext *) container, MONO_PARSE_PARAM,
1625 pattrs [i], ptr, &ptr);
1627 is_open = mono_class_is_open_constructed_type (method->params [i]);
1631 method->has_type_parameters = is_open;
1633 if (def && (method->call_convention == MONO_CALL_VARARG))
1634 method->sentinelpos = method->param_count;
1641 * Add signature to a cache and increase ref count...
1648 * mono_metadata_parse_method_signature:
1649 * @m: metadata context
1650 * @def: the MethodDef index or 0 for Ref signatures.
1651 * @ptr: pointer to the signature metadata representation
1652 * @rptr: pointer updated to match the end of the decoded stream
1654 * Decode a method signature stored at @ptr.
1655 * This is a Mono runtime internal function.
1657 * Returns: a MonoMethodSignature describing the signature.
1659 MonoMethodSignature *
1660 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1662 return mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr);
1666 * mono_metadata_free_method_signature:
1667 * @sig: signature to destroy
1669 * Free the memory allocated in the signature @sig.
1672 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1675 mono_metadata_free_type (sig->ret);
1676 for (i = 0; i < sig->param_count; ++i)
1677 mono_metadata_free_type (sig->params [i]);
1683 * mono_metadata_lookup_generic_inst:
1685 * Check whether the newly created generic instantiation @ginst already exists
1686 * in the cache and return the cached value in this case. Otherwise insert
1687 * it into the cache.
1689 * Use this method each time you create a new `MonoGenericInst' to ensure
1690 * proper caching. Only use the returned value as the argument passed to this
1691 * method may be freed.
1695 mono_metadata_lookup_generic_inst (MonoGenericInst *ginst)
1697 MonoGenericInst *cached;
1700 cached = g_hash_table_lookup (generic_inst_cache, ginst);
1702 for (i = 0; i < ginst->type_argc; i++)
1703 mono_metadata_free_type (ginst->type_argv [i]);
1704 g_free (ginst->type_argv);
1709 ginst->id = ++next_generic_inst_id;
1710 g_hash_table_insert (generic_inst_cache, ginst, ginst);
1716 * mono_metadata_lookup_generic_class:
1718 * Check whether the newly created generic class @gclass already exists
1719 * in the cache and return the cached value in this case. Otherwise insert
1720 * it into the cache and return NULL.
1722 * Returns: the previosly cached generic class or NULL if it has been newly
1723 * inserted into the cache.
1727 mono_metadata_lookup_generic_class (MonoGenericClass *gclass)
1729 MonoGenericClass *cached;
1731 cached = g_hash_table_lookup (generic_class_cache, gclass);
1735 g_hash_table_insert (generic_class_cache, gclass, gclass);
1740 * mono_metadata_inflate_generic_inst:
1742 * Instantiate the generic instance @ginst with the context @context.
1746 mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context)
1748 MonoGenericInst *nginst;
1751 nginst = g_new0 (MonoGenericInst, 1);
1752 nginst->type_argc = ginst->type_argc;
1753 nginst->type_argv = g_new0 (MonoType*, nginst->type_argc);
1754 nginst->is_reference = 1;
1756 for (i = 0; i < nginst->type_argc; i++) {
1757 MonoType *t = mono_class_inflate_generic_type (ginst->type_argv [i], context);
1759 if (!nginst->is_open)
1760 nginst->is_open = mono_class_is_open_constructed_type (t);
1761 if (nginst->is_reference)
1762 nginst->is_reference = MONO_TYPE_IS_REFERENCE (t);
1764 nginst->type_argv [i] = t;
1767 return mono_metadata_lookup_generic_inst (nginst);
1771 mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContext *generic_context,
1772 int count, const char *ptr, const char **rptr)
1774 MonoGenericInst *ginst;
1777 ginst = g_new0 (MonoGenericInst, 1);
1778 ginst->type_argc = count;
1779 ginst->type_argv = g_new0 (MonoType*, count);
1780 ginst->is_reference = 1;
1782 for (i = 0; i < ginst->type_argc; i++) {
1783 MonoType *t = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_TYPE, 0, ptr, &ptr);
1785 ginst->type_argv [i] = t;
1786 if (!ginst->is_open)
1787 ginst->is_open = mono_class_is_open_constructed_type (t);
1788 if (ginst->is_reference)
1789 ginst->is_reference = MONO_TYPE_IS_REFERENCE (t);
1795 return mono_metadata_lookup_generic_inst (ginst);
1799 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContext *generic_context,
1800 const char *ptr, const char **rptr)
1802 MonoInflatedGenericClass *igclass;
1803 MonoGenericClass *gclass, *cached;
1808 igclass = g_new0 (MonoInflatedGenericClass, 1);
1809 gclass = &igclass->generic_class;
1810 gclass->is_inflated = TRUE;
1812 type->data.generic_class = gclass;
1814 gclass->context = g_new0 (MonoGenericContext, 1);
1815 gclass->context->gclass = gclass;
1818 * Create the klass before parsing the type arguments.
1819 * This is required to support "recursive" definitions.
1820 * See mcs/tests/gen-23.cs for an example.
1822 igclass->klass = g_new0 (MonoClass, 1);
1824 gtype = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_TYPE, 0, ptr, &ptr);
1825 gclass->container_class = gklass = mono_class_from_mono_type (gtype);
1827 g_assert ((gclass->context->container = gklass->generic_container) != NULL);
1828 count = mono_metadata_decode_value (ptr, &ptr);
1830 gclass->inst = mono_metadata_parse_generic_inst (m, generic_context, count, ptr, &ptr);
1836 * We may be called multiple times on different metadata to create the same
1837 * instantiated type. This happens for instance if we're part of a method or
1838 * local variable signature.
1840 * It's important to return the same MonoGenericClass * for each particualar
1841 * instantiation of a generic type (ie "Stack<Int32>") to make static fields
1844 * According to the spec ($26.1.5), a static variable in a generic class
1845 * declaration is shared amongst all instances of the same closed constructed
1849 cached = g_hash_table_lookup (generic_class_cache, gclass);
1851 g_free (igclass->klass);
1854 type->data.generic_class = cached;
1857 g_hash_table_insert (generic_class_cache, gclass, gclass);
1859 mono_stats.generic_instance_count++;
1860 mono_stats.generics_metadata_size += sizeof (MonoGenericClass) +
1861 sizeof (MonoGenericContext) +
1862 gclass->inst->type_argc * sizeof (MonoType);
1867 * do_mono_metadata_parse_generic_param:
1868 * @generic_container: Our MonoClass's or MonoMethodNormal's MonoGenericContainer;
1869 * see mono_metadata_parse_type_full() for details.
1870 * Internal routine to parse a generic type parameter.
1872 static MonoGenericParam *
1873 mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContext *generic_context,
1874 gboolean is_mvar, const char *ptr, const char **rptr)
1876 MonoGenericContainer *generic_container;
1879 index = mono_metadata_decode_value (ptr, &ptr);
1883 if (!generic_context) {
1884 /* Create dummy MonoGenericParam */
1885 MonoGenericParam *param = g_new0 (MonoGenericParam, 1);
1886 param->name = g_strdup_printf ("%d", index);
1891 generic_container = generic_context->container;
1894 g_assert (generic_container);
1895 if (generic_container->parent) {
1897 * The current MonoGenericContainer is a generic method -> its `parent'
1898 * points to the containing class'es container.
1900 generic_container = generic_container->parent;
1902 g_assert (generic_container && !generic_container->is_method);
1903 g_assert (index < generic_container->type_argc);
1905 return &generic_container->type_params [index];
1907 g_assert (generic_container && (generic_container->is_method || generic_container->is_signature));
1908 g_assert (index < generic_container->type_argc);
1910 return &generic_container->type_params [index];
1915 * do_mono_metadata_parse_type:
1916 * @type: MonoType to be filled in with the return value
1918 * @generic_context: generics_context
1919 * @ptr: pointer to the encoded type
1920 * @rptr: pointer where the end of the encoded type is saved
1922 * Internal routine used to "fill" the contents of @type from an
1923 * allocated pointer. This is done this way to avoid doing too
1924 * many mini-allocations (particularly for the MonoFieldType which
1925 * most of the time is just a MonoType, but sometimes might be augmented).
1927 * This routine is used by mono_metadata_parse_type and
1928 * mono_metadata_parse_field_type
1930 * This extracts a Type as specified in Partition II (22.2.12)
1933 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContext *generic_context,
1934 const char *ptr, const char **rptr)
1936 type->type = mono_metadata_decode_value (ptr, &ptr);
1938 switch (type->type){
1939 case MONO_TYPE_VOID:
1940 case MONO_TYPE_BOOLEAN:
1941 case MONO_TYPE_CHAR:
1954 case MONO_TYPE_STRING:
1955 case MONO_TYPE_OBJECT:
1956 case MONO_TYPE_TYPEDBYREF:
1958 case MONO_TYPE_VALUETYPE:
1959 case MONO_TYPE_CLASS: {
1961 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
1962 type->data.klass = mono_class_get (m, token);
1965 case MONO_TYPE_SZARRAY: {
1966 MonoType *etype = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1967 type->data.klass = mono_class_from_mono_type (etype);
1968 mono_metadata_free_type (etype);
1972 type->data.type = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1974 case MONO_TYPE_FNPTR: {
1975 MonoGenericContainer *container = generic_context ? generic_context->container : NULL;
1976 type->data.method = mono_metadata_parse_method_signature_full (m, container, 0, ptr, &ptr);
1979 case MONO_TYPE_ARRAY:
1980 type->data.array = mono_metadata_parse_array_full (m, generic_context, ptr, &ptr);
1982 case MONO_TYPE_MVAR:
1983 type->data.generic_param = mono_metadata_parse_generic_param (m, generic_context, TRUE, ptr, &ptr);
1986 type->data.generic_param = mono_metadata_parse_generic_param (m, generic_context, FALSE, ptr, &ptr);
1988 case MONO_TYPE_GENERICINST:
1989 do_mono_metadata_parse_generic_class (type, m, generic_context, ptr, &ptr);
1992 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
2000 * mono_metadata_free_type:
2001 * @type: type to free
2003 * Free the memory allocated for type @type.
2006 mono_metadata_free_type (MonoType *type)
2008 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
2011 switch (type->type){
2012 case MONO_TYPE_OBJECT:
2013 case MONO_TYPE_STRING:
2014 if (!type->data.klass)
2017 case MONO_TYPE_CLASS:
2018 case MONO_TYPE_VALUETYPE:
2019 if (type == &type->data.klass->byval_arg || type == &type->data.klass->this_arg)
2023 mono_metadata_free_type (type->data.type);
2025 case MONO_TYPE_FNPTR:
2026 mono_metadata_free_method_signature (type->data.method);
2028 case MONO_TYPE_ARRAY:
2029 mono_metadata_free_array (type->data.array);
2037 hex_dump (const char *buffer, int base, int count)
2039 int show_header = 1;
2047 for (i = 0; i < count; i++){
2050 printf ("\n0x%08x: ", (unsigned char) base + i);
2052 printf ("%02x ", (unsigned char) (buffer [i]));
2059 * @mh: The Method header
2060 * @ptr: Points to the beginning of the Section Data (25.3)
2063 parse_section_data (MonoImage *m, MonoMethodHeader *mh, const unsigned char *ptr)
2065 unsigned char sect_data_flags;
2066 const unsigned char *sptr;
2068 guint32 sect_data_len;
2071 /* align on 32-bit boundary */
2072 /* FIXME: not 64-bit clean code */
2073 sptr = ptr = dword_align (ptr);
2074 sect_data_flags = *ptr;
2077 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
2079 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
2082 sect_data_len = ptr [0];
2086 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
2087 hex_dump (sptr, 0, sect_data_len+8);
2088 g_print ("\nheader: ");
2089 hex_dump (sptr-4, 0, 4);
2093 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
2094 const unsigned char *p = dword_align (ptr);
2096 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
2097 /* we could just store a pointer if we don't need to byteswap */
2098 mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
2099 for (i = 0; i < mh->num_clauses; ++i) {
2100 MonoExceptionClause *ec = &mh->clauses [i];
2103 ec->flags = read32 (p);
2104 ec->try_offset = read32 (p + 4);
2105 ec->try_len = read32 (p + 8);
2106 ec->handler_offset = read32 (p + 12);
2107 ec->handler_len = read32 (p + 16);
2108 tof_value = read32 (p + 20);
2111 ec->flags = read16 (p);
2112 ec->try_offset = read16 (p + 2);
2113 ec->try_len = *(p + 4);
2114 ec->handler_offset = read16 (p + 5);
2115 ec->handler_len = *(p + 7);
2116 tof_value = read32 (p + 8);
2119 if (ec->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2120 ec->data.filter_offset = tof_value;
2121 } else if (ec->flags == MONO_EXCEPTION_CLAUSE_NONE) {
2122 ec->data.catch_class = tof_value? mono_class_get (m, tof_value): 0;
2124 ec->data.catch_class = NULL;
2126 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
2130 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
2131 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
2138 * mono_metadata_parse_mh_full:
2139 * @m: metadata context
2140 * @generic_context: generics context
2141 * @ptr: pointer to the method header.
2143 * Decode the method header at @ptr, including pointer to the IL code,
2144 * info about local variables and optional exception tables.
2145 * This is a Mono runtime internal function.
2147 * Returns: a MonoMethodHeader.
2150 mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContext *generic_context, const char *ptr)
2152 MonoMethodHeader *mh;
2153 unsigned char flags = *(const unsigned char *) ptr;
2154 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
2156 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
2157 const unsigned char *code;
2160 g_return_val_if_fail (ptr != NULL, NULL);
2163 case METHOD_HEADER_TINY_FORMAT:
2164 mh = g_new0 (MonoMethodHeader, 1);
2167 local_var_sig_tok = 0;
2168 mh->code_size = flags >> 2;
2171 case METHOD_HEADER_TINY_FORMAT1:
2172 mh = g_new0 (MonoMethodHeader, 1);
2175 local_var_sig_tok = 0;
2178 * The spec claims 3 bits, but the Beta2 is
2181 mh->code_size = flags >> 2;
2184 case METHOD_HEADER_FAT_FORMAT:
2185 fat_flags = read16 (ptr);
2187 hsize = (fat_flags >> 12) & 0xf;
2188 max_stack = read16 (ptr);
2190 code_size = read32 (ptr);
2192 local_var_sig_tok = read32 (ptr);
2195 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
2202 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
2206 * There are more sections
2208 ptr = code + code_size;
2214 if (local_var_sig_tok) {
2215 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
2216 const char *locals_ptr;
2217 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
2218 int len=0, i, bsize;
2220 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
2221 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
2222 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
2223 if (*locals_ptr != 0x07)
2224 g_warning ("wrong signature for locals blob");
2226 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
2227 mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
2228 mh->num_locals = len;
2229 for (i = 0; i < len; ++i)
2230 mh->locals [i] = mono_metadata_parse_type_full (
2231 m, generic_context, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
2233 mh = g_new0 (MonoMethodHeader, 1);
2236 mh->code_size = code_size;
2237 mh->max_stack = max_stack;
2238 mh->init_locals = init_locals;
2239 if (fat_flags & METHOD_HEADER_MORE_SECTS)
2240 parse_section_data (m, mh, (const unsigned char*)ptr);
2245 * mono_metadata_parse_mh:
2246 * @generic_context: generics context
2247 * @ptr: pointer to the method header.
2249 * Decode the method header at @ptr, including pointer to the IL code,
2250 * info about local variables and optional exception tables.
2251 * This is a Mono runtime internal function.
2253 * Returns: a MonoMethodHeader.
2256 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
2258 return mono_metadata_parse_mh_full (m, NULL, ptr);
2262 * mono_metadata_free_mh:
2263 * @mh: a method header
2265 * Free the memory allocated for the method header.
2266 * This is a Mono runtime internal function.
2269 mono_metadata_free_mh (MonoMethodHeader *mh)
2272 for (i = 0; i < mh->num_locals; ++i)
2273 mono_metadata_free_type (mh->locals[i]);
2274 g_free (mh->clauses);
2279 * mono_method_header_get_code:
2280 * @header: a MonoMethodHeader pointer
2281 * @code_size: memory location for returning the code size
2282 * @max_stack: memory location for returning the max stack
2284 * Method header accessor to retreive info about the IL code properties:
2285 * a pointer to the IL code itself, the size of the code and the max number
2286 * of stack slots used by the code.
2288 * Returns: pointer to the IL code represented by the method header.
2290 const unsigned char*
2291 mono_method_header_get_code (MonoMethodHeader *header, guint32* code_size, guint32* max_stack)
2294 *code_size = header->code_size;
2296 *max_stack = header->max_stack;
2297 return header->code;
2301 * mono_method_header_get_locals:
2302 * @header: a MonoMethodHeader pointer
2303 * @num_locals: memory location for returning the number of local variables
2304 * @init_locals: memory location for returning the init_locals flag
2306 * Method header accessor to retreive info about the local variables:
2307 * an array of local types, the number of locals and whether the locals
2308 * are supposed to be initialized to 0 on method entry
2310 * Returns: pointer to an array of types of the local variables
2313 mono_method_header_get_locals (MonoMethodHeader *header, guint32* num_locals, gboolean *init_locals)
2316 *num_locals = header->num_locals;
2318 *init_locals = header->init_locals;
2319 return header->locals;
2323 * mono_method_header_get_num_clauses:
2324 * @header: a MonoMethodHeader pointer
2326 * Method header accessor to retreive the number of exception clauses.
2328 * Returns: the number of exception clauses present
2331 mono_method_header_get_num_clauses (MonoMethodHeader *header)
2333 return header->num_clauses;
2337 * mono_method_header_get_clauses:
2338 * @header: a MonoMethodHeader pointer
2339 * @method: MonoMethod the header belongs to
2340 * @iter: pointer to a iterator
2341 * @clause: pointer to a MonoExceptionClause structure which will be filled with the info
2343 * Get the info about the exception clauses in the method. Set *iter to NULL to
2344 * initiate the iteration, then call the method repeatedly until it returns FALSE.
2345 * At each iteration, the structure pointed to by clause if filled with the
2346 * exception clause information.
2348 * Returns: TRUE if clause was filled with info, FALSE if there are no more exception
2352 mono_method_header_get_clauses (MonoMethodHeader *header, MonoMethod *method, gpointer *iter, MonoExceptionClause *clause)
2354 MonoExceptionClause *sc;
2355 /* later we'll be able to use this interface to parse the clause info on demand,
2356 * without allocating anything.
2358 if (!iter || !header->num_clauses)
2361 *iter = sc = header->clauses;
2367 if (sc < header->clauses + header->num_clauses) {
2376 * mono_metadata_parse_field_type:
2377 * @m: metadata context to extract information from
2378 * @ptr: pointer to the field signature
2379 * @rptr: pointer updated to match the end of the decoded stream
2381 * Parses the field signature, and returns the type information for it.
2383 * Returns: The MonoType that was extracted from @ptr.
2386 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
2388 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
2392 * mono_metadata_parse_param:
2393 * @m: metadata context to extract information from
2394 * @ptr: pointer to the param signature
2395 * @rptr: pointer updated to match the end of the decoded stream
2397 * Parses the param signature, and returns the type information for it.
2399 * Returns: The MonoType that was extracted from @ptr.
2402 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
2404 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
2408 * mono_metadata_token_from_dor:
2409 * @dor_token: A TypeDefOrRef coded index
2411 * dor_token is a TypeDefOrRef coded index: it contains either
2412 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
2413 * bits contain an index into the table.
2415 * Returns: an expanded token
2418 mono_metadata_token_from_dor (guint32 dor_index)
2422 table = dor_index & 0x03;
2423 idx = dor_index >> 2;
2426 case 0: /* TypeDef */
2427 return MONO_TOKEN_TYPE_DEF | idx;
2428 case 1: /* TypeRef */
2429 return MONO_TOKEN_TYPE_REF | idx;
2430 case 2: /* TypeSpec */
2431 return MONO_TOKEN_TYPE_SPEC | idx;
2433 g_assert_not_reached ();
2440 * We use this to pass context information to the row locator
2443 int idx; /* The index that we are trying to locate */
2444 int col_idx; /* The index in the row where idx may be stored */
2445 MonoTableInfo *t; /* pointer to the table */
2450 * How the row locator works.
2455 * ___|___------> _______
2458 * A column in the rows of table A references an index in table B.
2459 * For example A may be the TYPEDEF table and B the METHODDEF table.
2461 * Given an index in table B we want to get the row in table A
2462 * where the column n references our index in B.
2464 * In the locator_t structure:
2466 * col_idx is the column number
2467 * index is the index in table B
2468 * result will be the index in table A
2471 * Table A Table B column (in table A)
2472 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
2473 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
2474 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
2475 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
2476 * METHODSEM PROPERTY ASSOCIATION (encoded index)
2478 * Note that we still don't support encoded indexes.
2482 typedef_locator (const void *a, const void *b)
2484 locator_t *loc = (locator_t *) a;
2485 const char *bb = (const char *) b;
2486 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
2487 guint32 col, col_next;
2489 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
2495 * Need to check that the next row is valid.
2497 if (typedef_index + 1 < loc->t->rows) {
2498 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
2499 if (loc->idx >= col_next)
2502 if (col == col_next)
2506 loc->result = typedef_index;
2512 table_locator (const void *a, const void *b)
2514 locator_t *loc = (locator_t *) a;
2515 const char *bb = (const char *) b;
2516 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2519 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2521 if (loc->idx == col) {
2522 loc->result = table_index;
2532 declsec_locator (const void *a, const void *b)
2534 locator_t *loc = (locator_t *) a;
2535 const char *bb = (const char *) b;
2536 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2539 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2541 if (loc->idx == col) {
2542 loc->result = table_index;
2552 * mono_metadata_typedef_from_field:
2553 * @meta: metadata context
2554 * @index: FieldDef token
2556 * Returns: the 1-based index into the TypeDef table of the type that
2557 * declared the field described by @index, or 0 if not found.
2560 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
2562 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2568 loc.idx = mono_metadata_token_index (index);
2569 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
2572 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2573 g_assert_not_reached ();
2575 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2576 return loc.result + 1;
2580 * mono_metadata_typedef_from_method:
2581 * @meta: metadata context
2582 * @index: MethodDef token
2584 * Returns: the 1-based index into the TypeDef table of the type that
2585 * declared the method described by @index. 0 if not found.
2588 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
2590 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2596 loc.idx = mono_metadata_token_index (index);
2597 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
2600 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2601 g_assert_not_reached ();
2603 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2604 return loc.result + 1;
2608 * mono_metadata_interfaces_from_typedef_full:
2609 * @meta: metadata context
2610 * @index: typedef token
2612 * The array of interfaces that the @index typedef token implements is returned in
2613 * @interfaces. The number of elemnts in the array is returned in @count.
2615 * Returns: TRUE on success, FALSE on failure.
2618 mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, MonoClass ***interfaces, guint *count, MonoGenericContext *context)
2620 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
2623 guint32 cols [MONO_INTERFACEIMPL_SIZE];
2632 loc.idx = mono_metadata_token_index (index);
2633 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
2636 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2641 * We may end up in the middle of the rows...
2644 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
2651 while (start < tdef->rows) {
2652 mono_metadata_decode_row (tdef, start, cols, MONO_INTERFACEIMPL_SIZE);
2653 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
2655 result = g_renew (MonoClass*, result, i + 1);
2656 result [i] = mono_class_get_full (
2657 meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]), context);
2661 *interfaces = result;
2666 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
2668 MonoClass **interfaces;
2671 rv = mono_metadata_interfaces_from_typedef_full (meta, index, &interfaces, count, NULL);
2679 * mono_metadata_nested_in_typedef:
2680 * @meta: metadata context
2681 * @index: typedef token
2683 * Returns: the 1-based index into the TypeDef table of the type
2684 * where the type described by @index is nested.
2685 * Retruns 0 if @index describes a non-nested type.
2688 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
2690 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2696 loc.idx = mono_metadata_token_index (index);
2697 loc.col_idx = MONO_NESTED_CLASS_NESTED;
2700 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2703 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2704 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
2708 * mono_metadata_nesting_typedef:
2709 * @meta: metadata context
2710 * @index: typedef token
2712 * Returns: the 1-based index into the TypeDef table of the first type
2713 * that is nested inside the type described by @index. The search starts at
2714 * @start_index. returns 0 if no such type is found.
2717 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
2719 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2721 guint32 class_index = mono_metadata_token_index (index);
2726 start = start_index;
2728 while (start <= tdef->rows) {
2729 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
2735 if (start > tdef->rows)
2742 * mono_metadata_packing_from_typedef:
2743 * @meta: metadata context
2744 * @index: token representing a type
2746 * Returns: the info stored in the ClassLAyout table for the given typedef token
2747 * into the @packing and @size pointers.
2748 * Returns 0 if the info is not found.
2751 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
2753 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
2755 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
2760 loc.idx = mono_metadata_token_index (index);
2761 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
2764 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2767 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
2769 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
2771 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
2773 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2774 return loc.result + 1;
2778 * mono_metadata_custom_attrs_from_index:
2779 * @meta: metadata context
2780 * @index: token representing the parent
2782 * Returns: the 1-based index into the CustomAttribute table of the first
2783 * attribute which belongs to the metadata object described by @index.
2784 * Returns 0 if no such attribute is found.
2787 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
2789 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
2796 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
2799 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2802 /* Find the first entry by searching backwards */
2803 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
2806 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2807 return loc.result + 1;
2811 * mono_metadata_declsec_from_index:
2812 * @meta: metadata context
2813 * @index: token representing the parent
2815 * Returns: the 0-based index into the DeclarativeSecurity table of the first
2816 * attribute which belongs to the metadata object described by @index.
2817 * Returns -1 if no such attribute is found.
2820 mono_metadata_declsec_from_index (MonoImage *meta, guint32 index)
2822 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_DECLSECURITY];
2829 loc.col_idx = MONO_DECL_SECURITY_PARENT;
2832 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
2835 /* Find the first entry by searching backwards */
2836 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_DECL_SECURITY_PARENT) == index))
2844 mono_backtrace (int limit)
2849 backtrace (array, limit);
2850 names = backtrace_symbols (array, limit);
2851 for (i =0; i < limit; ++i) {
2852 g_print ("\t%s\n", names [i]);
2859 /*#define __alignof__(a) sizeof(a)*/
2860 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
2865 * @t: the type to return the size of
2867 * Returns: the number of bytes required to hold an instance of this
2871 mono_type_size (MonoType *t, gint *align)
2878 *align = __alignof__(gpointer);
2879 return sizeof (gpointer);
2883 case MONO_TYPE_VOID:
2886 case MONO_TYPE_BOOLEAN:
2887 *align = __alignof__(gint8);
2891 *align = __alignof__(gint8);
2893 case MONO_TYPE_CHAR:
2896 *align = __alignof__(gint16);
2900 *align = __alignof__(gint32);
2903 *align = __alignof__(float);
2907 *align = __alignof__(gint64);
2910 *align = __alignof__(double);
2914 *align = __alignof__(gpointer);
2915 return sizeof (gpointer);
2916 case MONO_TYPE_STRING:
2917 *align = __alignof__(gpointer);
2918 return sizeof (gpointer);
2919 case MONO_TYPE_OBJECT:
2920 *align = __alignof__(gpointer);
2921 return sizeof (gpointer);
2922 case MONO_TYPE_VALUETYPE: {
2923 if (t->data.klass->enumtype)
2924 return mono_type_size (t->data.klass->enum_basetype, align);
2926 return mono_class_value_size (t->data.klass, align);
2928 case MONO_TYPE_CLASS:
2929 case MONO_TYPE_SZARRAY:
2931 case MONO_TYPE_FNPTR:
2932 case MONO_TYPE_ARRAY:
2933 *align = __alignof__(gpointer);
2934 return sizeof (gpointer);
2935 case MONO_TYPE_TYPEDBYREF:
2936 return mono_class_value_size (mono_defaults.typed_reference_class, align);
2937 case MONO_TYPE_GENERICINST: {
2938 MonoInflatedGenericClass *gclass;
2939 MonoClass *container_class;
2941 gclass = mono_get_inflated_generic_class (t->data.generic_class);
2942 g_assert (!gclass->generic_class.inst->is_open);
2943 g_assert (!gclass->klass->generic_container);
2945 container_class = gclass->generic_class.container_class;
2947 if (container_class->valuetype) {
2948 if (container_class->enumtype)
2949 return mono_type_size (container_class->enum_basetype, align);
2951 return mono_class_value_size (gclass->klass, align);
2953 *align = __alignof__(gpointer);
2954 return sizeof (gpointer);
2958 case MONO_TYPE_MVAR:
2959 /* FIXME: Martin, this is wrong. */
2960 *align = __alignof__(gpointer);
2961 return sizeof (gpointer);
2963 g_error ("mono_type_size: type 0x%02x unknown", t->type);
2969 * mono_type_stack_size:
2970 * @t: the type to return the size it uses on the stack
2972 * Returns: the number of bytes required to hold an instance of this
2973 * type on the runtime stack
2976 mono_type_stack_size (MonoType *t, gint *align)
2980 g_assert (t != NULL);
2986 *align = __alignof__(gpointer);
2987 return sizeof (gpointer);
2991 case MONO_TYPE_BOOLEAN:
2992 case MONO_TYPE_CHAR:
3001 case MONO_TYPE_STRING:
3002 case MONO_TYPE_OBJECT:
3003 case MONO_TYPE_CLASS:
3004 case MONO_TYPE_SZARRAY:
3006 case MONO_TYPE_FNPTR:
3007 case MONO_TYPE_ARRAY:
3008 *align = __alignof__(gpointer);
3009 return sizeof (gpointer);
3010 case MONO_TYPE_TYPEDBYREF:
3011 *align = __alignof__(gpointer);
3012 return sizeof (gpointer) * 3;
3014 *align = __alignof__(float);
3015 return sizeof (float);
3018 *align = __alignof__(gint64);
3019 return sizeof (gint64);
3021 *align = __alignof__(double);
3022 return sizeof (double);
3023 case MONO_TYPE_VALUETYPE: {
3026 if (t->data.klass->enumtype)
3027 return mono_type_stack_size (t->data.klass->enum_basetype, align);
3029 size = mono_class_value_size (t->data.klass, align);
3031 *align = *align + __alignof__(gpointer) - 1;
3032 *align &= ~(__alignof__(gpointer) - 1);
3034 size += sizeof (gpointer) - 1;
3035 size &= ~(sizeof (gpointer) - 1);
3040 case MONO_TYPE_GENERICINST: {
3041 MonoInflatedGenericClass *gclass;
3042 MonoClass *container_class;
3044 gclass = mono_get_inflated_generic_class (t->data.generic_class);
3045 container_class = gclass->generic_class.container_class;
3047 g_assert (!gclass->generic_class.inst->is_open);
3048 g_assert (!gclass->klass->generic_container);
3050 if (container_class->valuetype) {
3051 if (container_class->enumtype)
3052 return mono_type_stack_size (container_class->enum_basetype, align);
3054 guint32 size = mono_class_value_size (gclass->klass, align);
3056 *align = *align + __alignof__(gpointer) - 1;
3057 *align &= ~(__alignof__(gpointer) - 1);
3059 size += sizeof (gpointer) - 1;
3060 size &= ~(sizeof (gpointer) - 1);
3065 *align = __alignof__(gpointer);
3066 return sizeof (gpointer);
3070 g_error ("type 0x%02x unknown", t->type);
3076 mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass)
3078 return gclass->container_class->valuetype;
3082 _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2, gboolean signature_only)
3086 if ((g1->inst->type_argc != g2->inst->type_argc) || (g1->is_dynamic != g2->is_dynamic) ||
3087 (g1->inst->is_reference != g2->inst->is_reference))
3089 if (!mono_metadata_class_equal (g1->container_class, g2->container_class, signature_only))
3091 for (i = 0; i < g1->inst->type_argc; ++i) {
3092 if (!do_mono_metadata_type_equal (g1->inst->type_argv [i], g2->inst->type_argv [i], signature_only))
3099 mono_metadata_generic_method_hash (MonoGenericMethod *gmethod)
3101 return gmethod->inst->id;
3105 mono_metadata_generic_method_equal (MonoGenericMethod *g1, MonoGenericMethod *g2)
3107 return (g1->container == g2->container) && (g1->generic_class == g2->generic_class) &&
3108 (g1->inst == g2->inst);
3113 * mono_metadata_type_hash:
3116 * Computes an hash value for @t1 to be used in GHashTable.
3119 mono_metadata_type_hash (MonoType *t1)
3121 guint hash = t1->type;
3123 hash |= t1->byref << 6; /* do not collide with t1->type values */
3125 case MONO_TYPE_VALUETYPE:
3126 case MONO_TYPE_CLASS:
3127 case MONO_TYPE_SZARRAY:
3128 /* check if the distribution is good enough */
3129 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
3131 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
3132 case MONO_TYPE_ARRAY:
3133 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
3134 case MONO_TYPE_GENERICINST:
3135 return ((hash << 5) - hash) ^ mono_generic_class_hash (t1->data.generic_class);
3141 mono_metadata_generic_param_equal (MonoGenericParam *p1, MonoGenericParam *p2, gboolean signature_only)
3145 if (p1->num != p2->num)
3148 if (p1->owner == p2->owner)
3152 * If `signature_only' is true, we're comparing two (method) signatures.
3153 * In this case, the owner of two type parameters doesn't need to match.
3156 return signature_only;
3160 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only)
3164 if (c1->generic_class && c2->generic_class)
3165 return _mono_metadata_generic_class_equal (c1->generic_class, c2->generic_class, signature_only);
3166 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
3167 return mono_metadata_generic_param_equal (
3168 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, FALSE);
3169 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
3170 return mono_metadata_generic_param_equal (
3171 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
3172 if (signature_only &&
3173 (c1->byval_arg.type == MONO_TYPE_SZARRAY) && (c2->byval_arg.type == MONO_TYPE_SZARRAY))
3174 return mono_metadata_class_equal (c1->byval_arg.data.klass, c2->byval_arg.data.klass, signature_only);
3179 * mono_metadata_type_equal:
3183 * Determine if @t1 and @t2 represent the same type.
3184 * Returns: #TRUE if @t1 and @t2 are equal.
3187 do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only)
3189 if (t1->type != t2->type || t1->byref != t2->byref)
3193 case MONO_TYPE_VOID:
3194 case MONO_TYPE_BOOLEAN:
3195 case MONO_TYPE_CHAR:
3206 case MONO_TYPE_STRING:
3209 case MONO_TYPE_OBJECT:
3210 case MONO_TYPE_TYPEDBYREF:
3212 case MONO_TYPE_VALUETYPE:
3213 case MONO_TYPE_CLASS:
3214 case MONO_TYPE_SZARRAY:
3215 return mono_metadata_class_equal (t1->data.klass, t2->data.klass, signature_only);
3217 return do_mono_metadata_type_equal (t1->data.type, t2->data.type, signature_only);
3218 case MONO_TYPE_ARRAY:
3219 if (t1->data.array->rank != t2->data.array->rank)
3221 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass, signature_only);
3222 case MONO_TYPE_GENERICINST:
3223 return _mono_metadata_generic_class_equal (
3224 t1->data.generic_class, t2->data.generic_class, signature_only);
3226 return mono_metadata_generic_param_equal (
3227 t1->data.generic_param, t2->data.generic_param, signature_only);
3228 case MONO_TYPE_MVAR:
3229 return mono_metadata_generic_param_equal (
3230 t1->data.generic_param, t2->data.generic_param, signature_only);
3232 g_error ("implement type compare for %0x!", t1->type);
3240 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
3242 return do_mono_metadata_type_equal (t1, t2, FALSE);
3246 * mono_metadata_signature_equal:
3247 * @sig1: a signature
3248 * @sig2: another signature
3250 * Determine if @sig1 and @sig2 represent the same signature, with the
3251 * same number of arguments and the same types.
3252 * Returns: #TRUE if @sig1 and @sig2 are equal.
3255 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
3259 if (sig1->hasthis != sig2->hasthis || sig1->param_count != sig2->param_count)
3263 * We're just comparing the signatures of two methods here:
3265 * If we have two generic methods `void Foo<U> (U u)' and `void Bar<V> (V v)',
3266 * U and V are equal here.
3268 * That's what the `signature_only' argument of do_mono_metadata_type_equal() is for.
3271 for (i = 0; i < sig1->param_count; i++) {
3272 MonoType *p1 = sig1->params[i];
3273 MonoType *p2 = sig2->params[i];
3275 /* if (p1->attrs != p2->attrs)
3278 if (!do_mono_metadata_type_equal (p1, p2, TRUE))
3282 if (!do_mono_metadata_type_equal (sig1->ret, sig2->ret, TRUE))
3288 mono_signature_hash (MonoMethodSignature *sig)
3290 guint i, res = sig->ret->type;
3292 for (i = 0; i < sig->param_count; i++)
3293 res = (res << 5) - res + mono_type_hash (sig->params[i]);
3299 * mono_metadata_encode_value:
3300 * @value: value to encode
3301 * @buf: buffer where to write the compressed representation
3302 * @endbuf: pointer updated to point at the end of the encoded output
3304 * Encodes the value @value in the compressed representation used
3305 * in metadata and stores the result in @buf. @buf needs to be big
3306 * enough to hold the data (4 bytes).
3309 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
3315 else if (value < 0x4000) {
3316 p [0] = 0x80 | (value >> 8);
3317 p [1] = value & 0xff;
3320 p [0] = (value >> 24) | 0xc0;
3321 p [1] = (value >> 16) & 0xff;
3322 p [2] = (value >> 8) & 0xff;
3323 p [3] = value & 0xff;
3331 * mono_metadata_field_info:
3332 * @meta: the Image the field is defined in
3333 * @index: the index in the field table representing the field
3334 * @offset: a pointer to an integer where to store the offset that
3335 * may have been specified for the field in a FieldLayout table
3336 * @rva: a pointer to the RVA of the field data in the image that
3337 * may have been defined in a FieldRVA table
3338 * @marshal_spec: a pointer to the marshal spec that may have been
3339 * defined for the field in a FieldMarshal table.
3341 * Gather info for field @index that may have been defined in the FieldLayout,
3342 * FieldRVA and FieldMarshal tables.
3343 * Either of offset, rva and marshal_spec can be NULL if you're not interested
3347 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
3348 MonoMarshalSpec **marshal_spec)
3350 MonoTableInfo *tdef;
3353 loc.idx = index + 1;
3355 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
3357 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
3360 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3361 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
3363 *offset = (guint32)-1;
3367 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
3369 loc.col_idx = MONO_FIELD_RVA_FIELD;
3372 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3374 * LAMESPEC: There is no signature, no nothing, just the raw data.
3376 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
3384 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
3385 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
3392 * mono_metadata_get_constant_index:
3393 * @meta: the Image the field is defined in
3394 * @index: the token that may have a row defined in the constants table
3395 * @hint: possible position for the row
3397 * @token must be a FieldDef, ParamDef or PropertyDef token.
3399 * Returns: the index into the Constants table or 0 if not found.
3402 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
3404 MonoTableInfo *tdef;
3406 guint32 index = mono_metadata_token_index (token);
3408 tdef = &meta->tables [MONO_TABLE_CONSTANT];
3409 index <<= MONO_HASCONSTANT_BITS;
3410 switch (mono_metadata_token_table (token)) {
3411 case MONO_TABLE_FIELD:
3412 index |= MONO_HASCONSTANT_FIEDDEF;
3414 case MONO_TABLE_PARAM:
3415 index |= MONO_HASCONSTANT_PARAM;
3417 case MONO_TABLE_PROPERTY:
3418 index |= MONO_HASCONSTANT_PROPERTY;
3421 g_warning ("Not a valid token for the constant table: 0x%08x", token);
3425 loc.col_idx = MONO_CONSTANT_PARENT;
3428 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
3431 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3432 return loc.result + 1;
3438 * mono_metadata_events_from_typedef:
3439 * @meta: metadata context
3440 * @index: 0-based index (in the TypeDef table) describing a type
3442 * Returns: the 0-based index in the Event table for the events in the
3443 * type. The last event that belongs to the type (plus 1) is stored
3444 * in the @end_idx pointer.
3447 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3451 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
3459 loc.col_idx = MONO_EVENT_MAP_PARENT;
3460 loc.idx = index + 1;
3462 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3465 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
3466 if (loc.result + 1 < tdef->rows) {
3467 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
3469 end = meta->tables [MONO_TABLE_EVENT].rows;
3477 * mono_metadata_methods_from_event:
3478 * @meta: metadata context
3479 * @index: 0-based index (in the Event table) describing a event
3481 * Returns: the 0-based index in the MethodDef table for the methods in the
3482 * event. The last method that belongs to the event (plus 1) is stored
3483 * in the @end_idx pointer.
3486 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
3490 guint32 cols [MONO_METHOD_SEMA_SIZE];
3491 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3498 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3499 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
3501 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3506 * We may end up in the middle of the rows...
3509 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3515 while (end < msemt->rows) {
3516 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3517 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3526 * mono_metadata_properties_from_typedef:
3527 * @meta: metadata context
3528 * @index: 0-based index (in the TypeDef table) describing a type
3530 * Returns: the 0-based index in the Property table for the properties in the
3531 * type. The last property that belongs to the type (plus 1) is stored
3532 * in the @end_idx pointer.
3535 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3539 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
3547 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
3548 loc.idx = index + 1;
3550 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3553 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
3554 if (loc.result + 1 < tdef->rows) {
3555 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
3557 end = meta->tables [MONO_TABLE_PROPERTY].rows;
3565 * mono_metadata_methods_from_property:
3566 * @meta: metadata context
3567 * @index: 0-based index (in the PropertyDef table) describing a property
3569 * Returns: the 0-based index in the MethodDef table for the methods in the
3570 * property. The last method that belongs to the property (plus 1) is stored
3571 * in the @end_idx pointer.
3574 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
3578 guint32 cols [MONO_METHOD_SEMA_SIZE];
3579 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3586 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3587 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
3589 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3594 * We may end up in the middle of the rows...
3597 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3603 while (end < msemt->rows) {
3604 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3605 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3614 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
3617 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
3623 loc.col_idx = MONO_IMPLMAP_MEMBER;
3624 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
3626 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3629 return loc.result + 1;
3633 * @ptr: MonoType to unwrap
3635 * Recurses in array/szarray to get the element type.
3636 * Returns the element MonoType or the @ptr itself if its not an array/szarray.
3639 unwrap_arrays (MonoType *ptr)
3641 while (ptr->type == MONO_TYPE_ARRAY || ptr->type == MONO_TYPE_SZARRAY)
3642 /* Found an array/szarray, recurse to get the element type */
3643 if (ptr->type == MONO_TYPE_ARRAY)
3644 ptr = &ptr->data.array->eklass->byval_arg;
3646 ptr = &ptr->data.klass->byval_arg;
3651 * @inst: GENERIC_INST
3652 * @context: context to compare against
3654 * Check if the VARs or MVARs in the GENERIC_INST @inst refer to the context @context.
3657 has_same_context (MonoGenericInst *inst, MonoGenericContext *context)
3659 int i = 0, count = inst->type_argc;
3660 MonoType **ptr = inst->type_argv;
3666 for (i = 0; i < count; i++) {
3669 ctype = unwrap_arrays (ptr [i]);
3671 if (ctype->type == MONO_TYPE_MVAR || ctype->type == MONO_TYPE_VAR) {
3672 MonoGenericContainer *owner = ctype->data.generic_param->owner;
3673 MonoGenericContainer *gc = (MonoGenericContainer *) context;
3674 return owner == gc || (ctype->type == MONO_TYPE_VAR && owner == gc->parent);
3677 if (ctype->type == MONO_TYPE_GENERICINST && ctype->data.generic_class->inst->is_open) {
3678 /* Found an open GenericInst recurse into it. We care about
3679 finding the first argument that is a generic parameter. */
3680 count = ctype->data.generic_class->inst->type_argc;
3681 ptr = ctype->data.generic_class->inst->type_argv;
3685 g_assert_not_reached ();
3690 * @image: context where the image is created
3691 * @type_spec: typespec token
3693 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
3697 mono_type_create_from_typespec_full (MonoImage *image, MonoGenericContext *generic_context, guint32 type_spec)
3699 guint32 idx = mono_metadata_token_index (type_spec);
3701 guint32 cols [MONO_TYPESPEC_SIZE];
3706 mono_loader_lock ();
3708 type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec));
3710 if (type && type->type == MONO_TYPE_GENERICINST && type->data.generic_class->inst->is_open &&
3711 !has_same_context (type->data.generic_class->inst, generic_context))
3712 /* is_open AND does not have the same context as generic_context,
3713 so don't use cached MonoType */
3716 if (type && (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR)) {
3717 g_assert (generic_context);
3718 if (((MonoGenericContext *) type->data.generic_param->owner) != generic_context) {
3719 /* cached MonoType's context differs from generic_context */
3720 MonoGenericContainer *gc = generic_context->container;
3721 if (type->type == MONO_TYPE_VAR && gc->parent)
3724 g_assert (type->type == MONO_TYPE_VAR || gc->is_method);
3726 type = gc->types ? gc->types [type->data.generic_param->num] : NULL;
3731 mono_loader_unlock ();
3735 t = &image->tables [MONO_TABLE_TYPESPEC];
3737 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
3738 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
3739 len = mono_metadata_decode_value (ptr, &ptr);
3741 type = g_new0 (MonoType, 1);
3743 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
3745 if (*ptr == MONO_TYPE_BYREF) {
3750 do_mono_metadata_parse_type (type, image, generic_context, ptr, &ptr);
3752 if ((type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR) && type->data.generic_param->owner) {
3753 MonoGenericContainer *container;
3755 g_assert (type->data.generic_param->owner);
3756 container = type->data.generic_param->owner;
3757 if (!container->types)
3758 container->types = g_new0 (MonoType*, container->type_argc);
3760 container->types [type->data.generic_param->num] = type;
3764 mono_loader_unlock ();
3770 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
3772 return mono_type_create_from_typespec_full (image, NULL, type_spec);
3776 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
3778 MonoMarshalSpec *res;
3780 const char *start = ptr;
3782 /* fixme: this is incomplete, but I cant find more infos in the specs */
3784 res = g_new0 (MonoMarshalSpec, 1);
3786 len = mono_metadata_decode_value (ptr, &ptr);
3787 res->native = *ptr++;
3789 if (res->native == MONO_NATIVE_LPARRAY) {
3790 res->data.array_data.param_num = -1;
3791 res->data.array_data.num_elem = -1;
3792 res->data.array_data.elem_mult = -1;
3794 if (ptr - start <= len)
3795 res->data.array_data.elem_type = *ptr++;
3796 if (ptr - start <= len)
3797 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
3798 if (ptr - start <= len)
3799 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3800 if (ptr - start <= len) {
3802 * LAMESPEC: Older spec versions say this parameter comes before
3803 * num_elem. Never spec versions don't talk about elem_mult at
3804 * all, but csc still emits it, and it is used to distinguish
3805 * between param_num being 0, and param_num being omitted.
3806 * So if (param_num == 0) && (num_elem > 0), then
3807 * elem_mult == 0 -> the array size is num_elem
3808 * elem_mult == 1 -> the array size is @param_num + num_elem
3810 res->data.array_data.elem_mult = mono_metadata_decode_value (ptr, &ptr);
3814 if (res->native == MONO_NATIVE_BYVALTSTR) {
3815 if (ptr - start <= len)
3816 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3819 if (res->native == MONO_NATIVE_BYVALARRAY) {
3820 if (ptr - start <= len)
3821 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3824 if (res->native == MONO_NATIVE_CUSTOM) {
3825 /* skip unused type guid */
3826 len = mono_metadata_decode_value (ptr, &ptr);
3828 /* skip unused native type name */
3829 len = mono_metadata_decode_value (ptr, &ptr);
3831 /* read custom marshaler type name */
3832 len = mono_metadata_decode_value (ptr, &ptr);
3833 res->data.custom_data.custom_name = g_strndup (ptr, len);
3835 /* read cookie string */
3836 len = mono_metadata_decode_value (ptr, &ptr);
3837 res->data.custom_data.cookie = g_strndup (ptr, len);
3840 if (res->native == MONO_NATIVE_SAFEARRAY) {
3841 res->data.safearray_data.elem_type = 0;
3842 res->data.safearray_data.num_elem = 0;
3843 if (ptr - start <= len)
3844 res->data.safearray_data.elem_type = *ptr++;
3845 if (ptr - start <= len)
3846 res->data.safearray_data.num_elem = *ptr++;
3852 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
3854 if (spec->native == MONO_NATIVE_CUSTOM) {
3855 g_free (spec->data.custom_data.custom_name);
3856 g_free (spec->data.custom_data.cookie);
3862 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
3863 gboolean unicode, MonoMarshalConv *conv)
3865 MonoMarshalConv dummy_conv;
3871 *conv = MONO_MARSHAL_CONV_NONE;
3874 return MONO_NATIVE_UINT;
3878 case MONO_TYPE_BOOLEAN:
3880 switch (mspec->native) {
3881 case MONO_NATIVE_VARIANTBOOL:
3882 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
3883 return MONO_NATIVE_VARIANTBOOL;
3884 case MONO_NATIVE_BOOLEAN:
3885 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3886 return MONO_NATIVE_BOOLEAN;
3887 case MONO_NATIVE_I1:
3888 case MONO_NATIVE_U1:
3889 return mspec->native;
3891 g_error ("cant marshal bool to native type %02x", mspec->native);
3894 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3895 return MONO_NATIVE_BOOLEAN;
3896 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
3897 case MONO_TYPE_I1: return MONO_NATIVE_I1;
3898 case MONO_TYPE_U1: return MONO_NATIVE_U1;
3899 case MONO_TYPE_I2: return MONO_NATIVE_I2;
3900 case MONO_TYPE_U2: return MONO_NATIVE_U2;
3901 case MONO_TYPE_I4: return MONO_NATIVE_I4;
3902 case MONO_TYPE_U4: return MONO_NATIVE_U4;
3903 case MONO_TYPE_I8: return MONO_NATIVE_I8;
3904 case MONO_TYPE_U8: return MONO_NATIVE_U8;
3905 case MONO_TYPE_R4: return MONO_NATIVE_R4;
3906 case MONO_TYPE_R8: return MONO_NATIVE_R8;
3907 case MONO_TYPE_STRING:
3909 switch (mspec->native) {
3910 case MONO_NATIVE_BSTR:
3911 *conv = MONO_MARSHAL_CONV_STR_BSTR;
3912 return MONO_NATIVE_BSTR;
3913 case MONO_NATIVE_LPSTR:
3914 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
3915 return MONO_NATIVE_LPSTR;
3916 case MONO_NATIVE_LPWSTR:
3917 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
3918 return MONO_NATIVE_LPWSTR;
3919 case MONO_NATIVE_LPTSTR:
3920 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3921 return MONO_NATIVE_LPTSTR;
3922 case MONO_NATIVE_ANSIBSTR:
3923 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
3924 return MONO_NATIVE_ANSIBSTR;
3925 case MONO_NATIVE_TBSTR:
3926 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
3927 return MONO_NATIVE_TBSTR;
3928 case MONO_NATIVE_BYVALTSTR:
3930 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
3932 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
3933 return MONO_NATIVE_BYVALTSTR;
3935 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);
3938 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3939 return MONO_NATIVE_LPTSTR;
3940 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
3941 case MONO_TYPE_VALUETYPE: /*FIXME*/
3942 if (type->data.klass->enumtype) {
3943 t = type->data.klass->enum_basetype->type;
3946 return MONO_NATIVE_STRUCT;
3947 case MONO_TYPE_SZARRAY:
3948 case MONO_TYPE_ARRAY:
3950 switch (mspec->native) {
3951 case MONO_NATIVE_BYVALARRAY:
3952 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
3953 return MONO_NATIVE_BYVALARRAY;
3954 case MONO_NATIVE_SAFEARRAY:
3955 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
3956 return MONO_NATIVE_SAFEARRAY;
3957 case MONO_NATIVE_LPARRAY:
3958 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3959 return MONO_NATIVE_LPARRAY;
3961 g_error ("cant marshal array as native type %02x", mspec->native);
3965 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3966 return MONO_NATIVE_LPARRAY;
3967 case MONO_TYPE_I: return MONO_NATIVE_INT;
3968 case MONO_TYPE_U: return MONO_NATIVE_UINT;
3969 case MONO_TYPE_CLASS:
3970 case MONO_TYPE_OBJECT: {
3971 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
3973 switch (mspec->native) {
3974 case MONO_NATIVE_STRUCT:
3975 return MONO_NATIVE_STRUCT;
3976 case MONO_NATIVE_INTERFACE:
3977 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
3978 return MONO_NATIVE_INTERFACE;
3979 case MONO_NATIVE_IDISPATCH:
3980 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
3981 return MONO_NATIVE_IDISPATCH;
3982 case MONO_NATIVE_IUNKNOWN:
3983 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
3984 return MONO_NATIVE_IUNKNOWN;
3985 case MONO_NATIVE_FUNC:
3986 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3987 type->data.klass == mono_defaults.delegate_class ||
3988 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3989 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3990 return MONO_NATIVE_FUNC;
3994 g_error ("cant marshal object as native type %02x", mspec->native);
3997 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3998 type->data.klass == mono_defaults.delegate_class ||
3999 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
4000 *conv = MONO_MARSHAL_CONV_DEL_FTN;
4001 return MONO_NATIVE_FUNC;
4003 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
4004 return MONO_NATIVE_STRUCT;
4006 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
4007 case MONO_TYPE_GENERICINST:
4008 type = &type->data.generic_class->container_class->byval_arg;
4011 case MONO_TYPE_TYPEDBYREF:
4013 g_error ("type 0x%02x not handled in marshal", t);
4015 return MONO_NATIVE_MAX;
4019 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
4022 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
4028 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
4029 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
4031 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4034 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
4038 method_from_method_def_or_ref (MonoImage *m, guint32 tok, MonoGenericContext *context)
4040 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
4041 switch (tok & MONO_METHODDEFORREF_MASK) {
4042 case MONO_METHODDEFORREF_METHODDEF:
4043 return mono_get_method_full (m, MONO_TOKEN_METHOD_DEF | idx, NULL, context);
4044 case MONO_METHODDEFORREF_METHODREF:
4045 return mono_get_method_full (m, MONO_TOKEN_MEMBER_REF | idx, NULL, context);
4047 g_assert_not_reached ();
4052 * mono_class_get_overrides_full:
4054 * Return the method overrides belonging to class @type_token in @overrides, and
4055 * the number of overrides in @num_overrides.
4057 * Returns: TRUE on success, FALSE on failure.
4060 mono_class_get_overrides_full (MonoImage *image, guint32 type_token, MonoMethod ***overrides, gint32 *num_overrides,
4061 MonoGenericContext *generic_context)
4064 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
4067 guint32 cols [MONO_METHODIMPL_SIZE];
4068 MonoMethod **result;
4078 loc.col_idx = MONO_METHODIMPL_CLASS;
4079 loc.idx = mono_metadata_token_index (type_token);
4081 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4087 * We may end up in the middle of the rows...
4090 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
4095 while (end < tdef->rows) {
4096 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
4102 result = g_new (MonoMethod*, num * 2);
4103 for (i = 0; i < num; ++i) {
4104 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
4105 result [i * 2] = method_from_method_def_or_ref (
4106 image, cols [MONO_METHODIMPL_DECLARATION], generic_context);
4107 result [i * 2 + 1] = method_from_method_def_or_ref (
4108 image, cols [MONO_METHODIMPL_BODY], generic_context);
4111 *overrides = result;
4113 *num_overrides = num;
4118 * mono_guid_to_string:
4120 * Converts a 16 byte Microsoft GUID to the standard string representation.
4123 mono_guid_to_string (const guint8 *guid)
4125 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
4126 guid[3], guid[2], guid[1], guid[0],
4130 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
4134 get_constraints (MonoImage *image, int owner, MonoClass ***constraints, MonoGenericContext *context)
4136 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
4137 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
4138 guint32 i, token, found;
4139 MonoClass *klass, **res;
4140 GList *cons = NULL, *tmp;
4142 *constraints = NULL;
4144 for (i = 0; i < tdef->rows; ++i) {
4145 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
4146 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
4147 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
4148 klass = mono_class_get_full (image, token, context);
4149 cons = g_list_append (cons, klass);
4152 /* contiguous list finished */
4159 res = g_new0 (MonoClass*, found + 1);
4160 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
4161 res [i] = tmp->data;
4169 mono_metadata_has_generic_params (MonoImage *image, guint32 token)
4171 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
4172 guint32 cols [MONO_GENERICPARAM_SIZE];
4173 guint32 i, owner = 0;
4175 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
4176 owner = MONO_TYPEORMETHOD_TYPE;
4177 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
4178 owner = MONO_TYPEORMETHOD_METHOD;
4180 g_error ("wrong token %x to load_generics_params", token);
4183 owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
4187 for (i = 0; i < tdef->rows; ++i) {
4188 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
4189 if (cols [MONO_GENERICPARAM_OWNER] == owner)
4192 if (i >= tdef->rows)
4199 * mono_metadata_load_generic_param_constraints:
4201 * Load the generic parameter constraints for the newly created generic type or method
4202 * represented by @token and @container. The @container is the new container which has
4203 * been returned by a call to mono_metadata_load_generic_params() with this @token.
4206 mono_metadata_load_generic_param_constraints (MonoImage *image, guint32 token,
4207 MonoGenericContainer *container)
4209 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
4210 guint32 cols [MONO_GENERICPARAM_SIZE];
4211 guint32 i, owner = 0, last_num, n;
4213 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
4214 owner = MONO_TYPEORMETHOD_TYPE;
4215 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
4216 owner = MONO_TYPEORMETHOD_METHOD;
4218 g_error ("wrong token %x to load_generics_param_constraints", token);
4221 owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
4225 for (i = 0; i < tdef->rows; ++i) {
4226 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
4227 if (cols [MONO_GENERICPARAM_OWNER] == owner)
4231 if (i >= tdef->rows)
4234 for (i = 0; i < container->type_argc; i++)
4235 get_constraints (image, last_num + i + 1, &container->type_params [i].constraints,
4236 &container->context);
4240 * mono_metadata_load_generic_params:
4242 * Load the type parameters from the type or method definition @token.
4244 * Use this method after parsing a type or method definition to figure out whether it's a generic
4245 * type / method. When parsing a method definition, @parent_container points to the generic container
4246 * of the current class, if any.
4248 * Note: This method does not load the constraints: for typedefs, this has to be done after fully
4249 * creating the type.
4251 * Returns: NULL if @token is not a generic type or method definition or the new generic container.
4254 MonoGenericContainer *
4255 mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericContainer *parent_container)
4257 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
4258 guint32 cols [MONO_GENERICPARAM_SIZE];
4259 guint32 i, owner = 0, last_num, n;
4260 MonoGenericContainer *container;
4261 MonoGenericParam *params;
4263 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
4264 owner = MONO_TYPEORMETHOD_TYPE;
4265 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
4266 owner = MONO_TYPEORMETHOD_METHOD;
4268 g_error ("wrong token %x to load_generics_params", token);
4271 owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
4275 for (i = 0; i < tdef->rows; ++i) {
4276 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
4277 if (cols [MONO_GENERICPARAM_OWNER] == owner)
4281 if (i >= tdef->rows)
4285 container = g_new0 (MonoGenericContainer, 1);
4288 params = g_realloc (params, sizeof (MonoGenericParam) * n);
4289 params [n - 1].owner = container;
4290 params [n - 1].pklass = NULL;
4291 params [n - 1].method = NULL;
4292 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
4293 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
4294 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
4295 params [n - 1].constraints = NULL;
4296 if (++i >= tdef->rows)
4298 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
4299 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
4301 container->type_argc = n;
4302 container->type_params = params;
4303 container->parent = parent_container;
4305 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
4306 container->is_method = 1;
4308 container->context.container = container;
4314 mono_type_is_byref (MonoType *type)
4320 mono_type_get_type (MonoType *type)
4325 /* For MONO_TYPE_FNPTR */
4326 MonoMethodSignature*
4327 mono_type_get_signature (MonoType *type)
4329 return type->data.method;
4332 /* For MONO_TYPE_CLASS, VALUETYPE */
4334 mono_type_get_class (MonoType *type)
4336 return type->data.klass;
4339 /* For MONO_TYPE_ARRAY */
4341 mono_type_get_array_type (MonoType *type)
4343 return type->data.array;
4347 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
4349 /* FIXME: implement */
4354 mono_signature_get_return_type (MonoMethodSignature *sig)
4360 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
4366 /* start from the first */
4367 if (sig->param_count) {
4368 *iter = &sig->params [0];
4369 return sig->params [0];
4377 if (type < &sig->params [sig->param_count]) {
4385 mono_signature_get_param_count (MonoMethodSignature *sig)
4387 return sig->param_count;
4391 mono_signature_get_call_conv (MonoMethodSignature *sig)
4393 return sig->call_convention;
4397 mono_signature_vararg_start (MonoMethodSignature *sig)
4399 return sig->sentinelpos;
4403 mono_signature_is_instance (MonoMethodSignature *sig)
4405 return sig->hasthis;
4409 mono_signature_explicit_this (MonoMethodSignature *sig)
4411 return sig->explicit_this;
4414 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
4416 mono_aligned_addr_hash (gconstpointer ptr)
4418 return GPOINTER_TO_UINT (ptr) >> 3;