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);
33 GHashTable *mono_generic_params_with_ambiguous_names;
36 * This enumeration is used to describe the data types in the metadata
47 /* Index into Blob heap */
50 /* Index into String heap */
56 /* Pointer into a table */
59 /* HasConstant:Parent pointer (Param, Field or Property) */
62 /* HasCustomAttribute index. Indexes any table except CustomAttribute */
65 /* CustomAttributeType encoded index */
68 /* HasDeclSecurity index: TypeDef Method or Assembly */
71 /* Implementation coded index: File, Export AssemblyRef */
74 /* HasFieldMarshal coded index: Field or Param table */
77 /* MemberForwardedIndex: Field or Method */
80 /* TypeDefOrRef coded index: typedef, typeref, typespec */
83 /* MemberRefParent coded index: typeref, moduleref, method, memberref, typesepc, typedef */
86 /* MethodDefOrRef coded index: Method or Member Ref table */
89 /* HasSemantic coded index: Event or Property */
92 /* ResolutionScope coded index: Module, ModuleRef, AssemblytRef, TypeRef */
96 const static unsigned char AssemblySchema [] = {
97 MONO_MT_UINT32, /* "HashId" }, */
98 MONO_MT_UINT16, /* "Major" }, */
99 MONO_MT_UINT16, /* "Minor" }, */
100 MONO_MT_UINT16, /* "BuildNumber" }, */
101 MONO_MT_UINT16, /* "RevisionNumber" }, */
102 MONO_MT_UINT32, /* "Flags" }, */
103 MONO_MT_BLOB_IDX, /* "PublicKey" }, */
104 MONO_MT_STRING_IDX, /* "Name" }, */
105 MONO_MT_STRING_IDX, /* "Culture" }, */
109 const static unsigned char AssemblyOSSchema [] = {
110 MONO_MT_UINT32, /* "OSPlatformID" }, */
111 MONO_MT_UINT32, /* "OSMajor" }, */
112 MONO_MT_UINT32, /* "OSMinor" }, */
116 const static unsigned char AssemblyProcessorSchema [] = {
117 MONO_MT_UINT32, /* "Processor" }, */
121 const static unsigned char AssemblyRefSchema [] = {
122 MONO_MT_UINT16, /* "Major" }, */
123 MONO_MT_UINT16, /* "Minor" }, */
124 MONO_MT_UINT16, /* "Build" }, */
125 MONO_MT_UINT16, /* "Revision" }, */
126 MONO_MT_UINT32, /* "Flags" }, */
127 MONO_MT_BLOB_IDX, /* "PublicKeyOrToken" }, */
128 MONO_MT_STRING_IDX, /* "Name" }, */
129 MONO_MT_STRING_IDX, /* "Culture" }, */
130 MONO_MT_BLOB_IDX, /* "HashValue" }, */
134 const static unsigned char AssemblyRefOSSchema [] = {
135 MONO_MT_UINT32, /* "OSPlatformID" }, */
136 MONO_MT_UINT32, /* "OSMajorVersion" }, */
137 MONO_MT_UINT32, /* "OSMinorVersion" }, */
138 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
142 const static unsigned char AssemblyRefProcessorSchema [] = {
143 MONO_MT_UINT32, /* "Processor" }, */
144 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
148 const static unsigned char ClassLayoutSchema [] = {
149 MONO_MT_UINT16, /* "PackingSize" }, */
150 MONO_MT_UINT32, /* "ClassSize" }, */
151 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
155 const static unsigned char ConstantSchema [] = {
156 MONO_MT_UINT8, /* "Type" }, */
157 MONO_MT_UINT8, /* "PaddingZero" }, */
158 MONO_MT_CONST_IDX, /* "Parent" }, */
159 MONO_MT_BLOB_IDX, /* "Value" }, */
163 const static unsigned char CustomAttributeSchema [] = {
164 MONO_MT_HASCAT_IDX, /* "Parent" }, */
165 MONO_MT_CAT_IDX, /* "Type" }, */
166 MONO_MT_BLOB_IDX, /* "Value" }, */
170 const static unsigned char DeclSecuritySchema [] = {
171 MONO_MT_UINT16, /* "Action" }, */
172 MONO_MT_HASDEC_IDX, /* "Parent" }, */
173 MONO_MT_BLOB_IDX, /* "PermissionSet" }, */
177 const static unsigned char EventMapSchema [] = {
178 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
179 MONO_MT_TABLE_IDX, /* "EventList:Event" }, */
183 const static unsigned char EventSchema [] = {
184 MONO_MT_UINT16, /* "EventFlags#EventAttribute" }, */
185 MONO_MT_STRING_IDX, /* "Name" }, */
186 MONO_MT_TABLE_IDX, /* "EventType" }, TypeDef or TypeRef */
190 const static unsigned char ExportedTypeSchema [] = {
191 MONO_MT_UINT32, /* "Flags" }, */
192 MONO_MT_TABLE_IDX, /* "TypeDefId" }, */
193 MONO_MT_STRING_IDX, /* "TypeName" }, */
194 MONO_MT_STRING_IDX, /* "TypeNameSpace" }, */
195 MONO_MT_IMPL_IDX, /* "Implementation" }, */
199 const static unsigned char FieldSchema [] = {
200 MONO_MT_UINT16, /* "Flags" }, */
201 MONO_MT_STRING_IDX, /* "Name" }, */
202 MONO_MT_BLOB_IDX, /* "Signature" }, */
205 const static unsigned char FieldLayoutSchema [] = {
206 MONO_MT_UINT32, /* "Offset" }, */
207 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
211 const static unsigned char FieldMarshalSchema [] = {
212 MONO_MT_HFM_IDX, /* "Parent" }, */
213 MONO_MT_BLOB_IDX, /* "NativeType" }, */
216 const static unsigned char FieldRVASchema [] = {
217 MONO_MT_UINT32, /* "RVA" }, */
218 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
222 const static unsigned char FileSchema [] = {
223 MONO_MT_UINT32, /* "Flags" }, */
224 MONO_MT_STRING_IDX, /* "Name" }, */
225 MONO_MT_BLOB_IDX, /* "Value" }, */
229 const static unsigned char ImplMapSchema [] = {
230 MONO_MT_UINT16, /* "MappingFlag" }, */
231 MONO_MT_MF_IDX, /* "MemberForwarded" }, */
232 MONO_MT_STRING_IDX, /* "ImportName" }, */
233 MONO_MT_TABLE_IDX, /* "ImportScope:ModuleRef" }, */
237 const static unsigned char InterfaceImplSchema [] = {
238 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
239 MONO_MT_TDOR_IDX, /* "Interface=TypeDefOrRef" }, */
243 const static unsigned char ManifestResourceSchema [] = {
244 MONO_MT_UINT32, /* "Offset" }, */
245 MONO_MT_UINT32, /* "Flags" }, */
246 MONO_MT_STRING_IDX, /* "Name" }, */
247 MONO_MT_IMPL_IDX, /* "Implementation" }, */
251 const static unsigned char MemberRefSchema [] = {
252 MONO_MT_MRP_IDX, /* "Class" }, */
253 MONO_MT_STRING_IDX, /* "Name" }, */
254 MONO_MT_BLOB_IDX, /* "Signature" }, */
258 const static unsigned char MethodSchema [] = {
259 MONO_MT_UINT32, /* "RVA" }, */
260 MONO_MT_UINT16, /* "ImplFlags#MethodImplAttributes" }, */
261 MONO_MT_UINT16, /* "Flags#MethodAttribute" }, */
262 MONO_MT_STRING_IDX, /* "Name" }, */
263 MONO_MT_BLOB_IDX, /* "Signature" }, */
264 MONO_MT_TABLE_IDX, /* "ParamList:Param" }, */
268 const static unsigned char MethodImplSchema [] = {
269 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
270 MONO_MT_MDOR_IDX, /* "MethodBody" }, */
271 MONO_MT_MDOR_IDX, /* "MethodDeclaration" }, */
275 const static unsigned char MethodSemanticsSchema [] = {
276 MONO_MT_UINT16, /* "MethodSemantic" }, */
277 MONO_MT_TABLE_IDX, /* "Method:Method" }, */
278 MONO_MT_HS_IDX, /* "Association" }, */
282 const static unsigned char ModuleSchema [] = {
283 MONO_MT_UINT16, /* "Generation" }, */
284 MONO_MT_STRING_IDX, /* "Name" }, */
285 MONO_MT_GUID_IDX, /* "MVID" }, */
286 MONO_MT_GUID_IDX, /* "EncID" }, */
287 MONO_MT_GUID_IDX, /* "EncBaseID" }, */
291 const static unsigned char ModuleRefSchema [] = {
292 MONO_MT_STRING_IDX, /* "Name" }, */
296 const static unsigned char NestedClassSchema [] = {
297 MONO_MT_TABLE_IDX, /* "NestedClass:TypeDef" }, */
298 MONO_MT_TABLE_IDX, /* "EnclosingClass:TypeDef" }, */
302 const static unsigned char ParamSchema [] = {
303 MONO_MT_UINT16, /* "Flags" }, */
304 MONO_MT_UINT16, /* "Sequence" }, */
305 MONO_MT_STRING_IDX, /* "Name" }, */
309 const static unsigned char PropertySchema [] = {
310 MONO_MT_UINT16, /* "Flags" }, */
311 MONO_MT_STRING_IDX, /* "Name" }, */
312 MONO_MT_BLOB_IDX, /* "Type" }, */
316 const static unsigned char PropertyMapSchema [] = {
317 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
318 MONO_MT_TABLE_IDX, /* "PropertyList:Property" }, */
322 const static unsigned char StandaloneSigSchema [] = {
323 MONO_MT_BLOB_IDX, /* "Signature" }, */
327 const static unsigned char TypeDefSchema [] = {
328 MONO_MT_UINT32, /* "Flags" }, */
329 MONO_MT_STRING_IDX, /* "Name" }, */
330 MONO_MT_STRING_IDX, /* "Namespace" }, */
331 MONO_MT_TDOR_IDX, /* "Extends" }, */
332 MONO_MT_TABLE_IDX, /* "FieldList:Field" }, */
333 MONO_MT_TABLE_IDX, /* "MethodList:Method" }, */
337 const static unsigned char TypeRefSchema [] = {
338 MONO_MT_RS_IDX, /* "ResolutionScope=ResolutionScope" }, */
339 MONO_MT_STRING_IDX, /* "Name" }, */
340 MONO_MT_STRING_IDX, /* "Namespace" }, */
344 const static unsigned char TypeSpecSchema [] = {
345 MONO_MT_BLOB_IDX, /* "Signature" }, */
349 const static unsigned char GenericParamSchema [] = {
350 MONO_MT_UINT16, /* "Number" }, */
351 MONO_MT_UINT16, /* "Flags" }, */
352 MONO_MT_TABLE_IDX, /* "Owner" }, TypeDef or MethodDef */
353 MONO_MT_STRING_IDX, /* "Name" }, */
358 const static unsigned char MethodSpecSchema [] = {
359 MONO_MT_MDOR_IDX, /* "Method" }, */
360 MONO_MT_BLOB_IDX, /* "Signature" }, */
364 const static unsigned char GenericParamConstraintSchema [] = {
365 MONO_MT_TABLE_IDX, /* "GenericParam" }, */
366 MONO_MT_TDOR_IDX, /* "Constraint" }, */
370 const static struct {
371 const unsigned char *description;
374 /* 0 */ { ModuleSchema, "Module" },
375 /* 1 */ { TypeRefSchema, "TypeRef" },
376 /* 2 */ { TypeDefSchema, "TypeDef" },
377 /* 3 */ { NULL, NULL },
378 /* 4 */ { FieldSchema, "Field" },
379 /* 5 */ { NULL, NULL },
380 /* 6 */ { MethodSchema, "Method" },
381 /* 7 */ { NULL, NULL },
382 /* 8 */ { ParamSchema, "Param" },
383 /* 9 */ { InterfaceImplSchema, "InterfaceImpl" },
384 /* A */ { MemberRefSchema, "MemberRef" },
385 /* B */ { ConstantSchema, "Constant" },
386 /* C */ { CustomAttributeSchema, "CustomAttribute" },
387 /* D */ { FieldMarshalSchema, "FieldMarshal" },
388 /* E */ { DeclSecuritySchema, "DeclSecurity" },
389 /* F */ { ClassLayoutSchema, "ClassLayout" },
390 /* 10 */ { FieldLayoutSchema, "FieldLayout" },
391 /* 11 */ { StandaloneSigSchema, "StandaloneSig" },
392 /* 12 */ { EventMapSchema, "EventMap" },
393 /* 13 */ { NULL, NULL },
394 /* 14 */ { EventSchema, "Event" },
395 /* 15 */ { PropertyMapSchema, "PropertyMap" },
396 /* 16 */ { NULL, NULL },
397 /* 17 */ { PropertySchema, "PropertyTable" },
398 /* 18 */ { MethodSemanticsSchema, "MethodSemantics" },
399 /* 19 */ { MethodImplSchema, "MethodImpl" },
400 /* 1A */ { ModuleRefSchema, "ModuleRef" },
401 /* 1B */ { TypeSpecSchema, "TypeSpec" },
402 /* 1C */ { ImplMapSchema, "ImplMap" },
403 /* 1D */ { FieldRVASchema, "FieldRVA" },
404 /* 1E */ { NULL, NULL },
405 /* 1F */ { NULL, NULL },
406 /* 20 */ { AssemblySchema, "Assembly" },
407 /* 21 */ { AssemblyProcessorSchema, "AssemblyProcessor" },
408 /* 22 */ { AssemblyOSSchema, "AssemblyOS" },
409 /* 23 */ { AssemblyRefSchema, "AssemblyRef" },
410 /* 24 */ { AssemblyRefProcessorSchema, "AssemblyRefProcessor" },
411 /* 25 */ { AssemblyRefOSSchema, "AssemblyRefOS" },
412 /* 26 */ { FileSchema, "File" },
413 /* 27 */ { ExportedTypeSchema, "ExportedType" },
414 /* 28 */ { ManifestResourceSchema, "ManifestResource" },
415 /* 29 */ { NestedClassSchema, "NestedClass" },
416 /* 2A */ { GenericParamSchema, "GenericParam" },
417 /* 2B */ { MethodSpecSchema, "MethodSpec" },
418 /* 2C */ { GenericParamConstraintSchema, "GenericParamConstraint" },
422 * mono_meta_table_name:
423 * @table: table index
425 * Returns: the name for the @table index
428 mono_meta_table_name (int table)
430 if ((table < 0) || (table > 0x2c))
433 return tables [table].name;
436 /* The guy who wrote the spec for this should not be allowed near a
439 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
440 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
441 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
442 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
443 inverse of this mapping.
446 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
447 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
449 /* Reference: Partition II - 23.2.6 */
451 * mono_metadata_compute_size:
452 * @meta: metadata context
453 * @tableindex: metadata table number
454 * @result_bitfield: pointer to guint32 where to store additional info
456 * mono_metadata_compute_size() computes the lenght in bytes of a single
457 * row in a metadata table. The size of each column is encoded in the
458 * @result_bitfield return value along with the number of columns in the table.
459 * the resulting bitfield should be handed to the mono_metadata_table_size()
460 * and mono_metadata_table_count() macros.
461 * This is a Mono runtime internal only function.
464 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
466 guint32 bitfield = 0;
467 int size = 0, field_size = 0;
470 const unsigned char *description = tables [tableindex].description;
472 for (i = 0; (code = description [i]) != MONO_MT_END; i++){
475 field_size = 4; break;
478 field_size = 2; break;
481 field_size = 1; break;
483 case MONO_MT_BLOB_IDX:
484 field_size = meta->idx_blob_wide ? 4 : 2; break;
486 case MONO_MT_STRING_IDX:
487 field_size = meta->idx_string_wide ? 4 : 2; break;
489 case MONO_MT_GUID_IDX:
490 field_size = meta->idx_guid_wide ? 4 : 2; break;
492 case MONO_MT_TABLE_IDX:
493 /* Uhm, a table index can point to other tables besides the current one
494 * so, it's not correct to use the rowcount of the current table to
495 * get the size for this column - lupus
497 switch (tableindex) {
498 case MONO_TABLE_ASSEMBLYREFOS:
500 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
501 case MONO_TABLE_ASSEMBLYPROCESSOR:
503 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
504 case MONO_TABLE_CLASSLAYOUT:
506 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
507 case MONO_TABLE_EVENTMAP:
508 g_assert (i == 0 || i == 1);
509 field_size = i ? idx_size (MONO_TABLE_EVENT):
510 idx_size(MONO_TABLE_TYPEDEF);
512 case MONO_TABLE_EVENT:
514 field_size = MAX (idx_size (MONO_TABLE_TYPEDEF), idx_size(MONO_TABLE_TYPEREF));
515 field_size = MAX (field_size, idx_size(MONO_TABLE_TYPESPEC));
517 case MONO_TABLE_EXPORTEDTYPE:
519 /* the index is in another metadata file, so it must be 4 */
520 field_size = 4; break;
521 case MONO_TABLE_FIELDLAYOUT:
523 field_size = idx_size (MONO_TABLE_FIELD); break;
524 case MONO_TABLE_FIELDRVA:
526 field_size = idx_size (MONO_TABLE_FIELD); break;
527 case MONO_TABLE_IMPLMAP:
529 field_size = idx_size (MONO_TABLE_MODULEREF); break;
530 case MONO_TABLE_INTERFACEIMPL:
532 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
533 case MONO_TABLE_METHOD:
535 field_size = idx_size (MONO_TABLE_PARAM); break;
536 case MONO_TABLE_METHODIMPL:
538 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
539 case MONO_TABLE_METHODSEMANTICS:
541 field_size = idx_size (MONO_TABLE_METHOD); break;
542 case MONO_TABLE_NESTEDCLASS:
543 g_assert (i == 0 || i == 1);
544 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
545 case MONO_TABLE_PROPERTYMAP:
546 g_assert (i == 0 || i == 1);
547 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
548 idx_size(MONO_TABLE_TYPEDEF);
550 case MONO_TABLE_TYPEDEF:
551 g_assert (i == 4 || i == 5);
552 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
553 idx_size(MONO_TABLE_METHOD);
555 case MONO_TABLE_GENERICPARAM:
556 g_assert (i == 2 || i == 4 || i == 5);
558 field_size = MAX (idx_size (MONO_TABLE_METHOD), idx_size (MONO_TABLE_TYPEDEF));
560 field_size = idx_size (MONO_TABLE_TYPEDEF);
562 field_size = idx_size (MONO_TABLE_TYPEDEF);
565 case MONO_TABLE_GENERICPARAMCONSTRAINT:
567 field_size = idx_size (MONO_TABLE_GENERICPARAM);
571 g_assert_not_reached ();
576 * HasConstant: ParamDef, FieldDef, Property
578 case MONO_MT_CONST_IDX:
579 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
580 meta->tables [MONO_TABLE_FIELD].rows);
581 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
583 /* 2 bits to encode tag */
584 field_size = rtsize (n, 16-2);
588 * HasCustomAttribute: points to any table but
591 case MONO_MT_HASCAT_IDX:
593 * We believe that since the signature and
594 * permission are indexing the Blob heap,
595 * we should consider the blob size first
597 /* I'm not a believer - lupus
598 if (meta->idx_blob_wide){
603 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
604 meta->tables [MONO_TABLE_FIELD].rows);
605 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
606 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
607 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
608 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
609 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
610 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
611 n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
612 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
613 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
614 n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
615 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
616 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
617 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
618 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
619 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
620 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
621 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
623 /* 5 bits to encode */
624 field_size = rtsize (n, 16-5);
628 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
629 * MemberRef and String.
631 case MONO_MT_CAT_IDX:
632 /* String is a heap, if it is wide, we know the size */
634 if (meta->idx_string_wide){
639 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
640 meta->tables [MONO_TABLE_TYPEDEF].rows);
641 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
642 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
644 /* 3 bits to encode */
645 field_size = rtsize (n, 16-3);
649 * HasDeclSecurity: Typedef, MethodDef, Assembly
651 case MONO_MT_HASDEC_IDX:
652 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
653 meta->tables [MONO_TABLE_METHOD].rows);
654 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
656 /* 2 bits to encode */
657 field_size = rtsize (n, 16-2);
661 * Implementation: File, AssemblyRef, ExportedType
663 case MONO_MT_IMPL_IDX:
664 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
665 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
666 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
668 /* 2 bits to encode tag */
669 field_size = rtsize (n, 16-2);
673 * HasFieldMarshall: FieldDef, ParamDef
675 case MONO_MT_HFM_IDX:
676 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
677 meta->tables [MONO_TABLE_PARAM].rows);
679 /* 1 bit used to encode tag */
680 field_size = rtsize (n, 16-1);
684 * MemberForwarded: FieldDef, MethodDef
687 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
688 meta->tables [MONO_TABLE_METHOD].rows);
690 /* 1 bit used to encode tag */
691 field_size = rtsize (n, 16-1);
695 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
697 * It is TypeDef, _TypeRef_, TypeSpec, instead.
699 case MONO_MT_TDOR_IDX:
700 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
701 meta->tables [MONO_TABLE_TYPEREF].rows);
702 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
704 /* 2 bits to encode */
705 field_size = rtsize (n, 16-2);
709 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
711 case MONO_MT_MRP_IDX:
712 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
713 meta->tables [MONO_TABLE_TYPEREF].rows);
714 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
715 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
716 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
717 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
719 /* 3 bits to encode */
720 field_size = rtsize (n, 16 - 3);
724 * MethodDefOrRef: MethodDef, MemberRef
726 case MONO_MT_MDOR_IDX:
727 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
728 meta->tables [MONO_TABLE_MEMBERREF].rows);
730 /* 1 bit used to encode tag */
731 field_size = rtsize (n, 16-1);
735 * HasSemantics: Property, Event
738 n = MAX (meta->tables [MONO_TABLE_PROPERTY].rows,
739 meta->tables [MONO_TABLE_EVENT].rows);
741 /* 1 bit used to encode tag */
742 field_size = rtsize (n, 16-1);
746 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
749 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
750 meta->tables [MONO_TABLE_MODULEREF].rows);
751 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
752 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
754 /* 2 bits used to encode tag (ECMA spec claims 3) */
755 field_size = rtsize (n, 16 - 2);
760 * encode field size as follows (we just need to
767 bitfield |= (field_size-1) << shift;
770 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
773 *result_bitfield = (i << 24) | bitfield;
778 * mono_metadata_compute_table_bases:
779 * @meta: metadata context to compute table values
781 * Computes the table bases for the metadata structure.
782 * This is an internal function used by the image loader code.
785 mono_metadata_compute_table_bases (MonoImage *meta)
788 const char *base = meta->tables_base;
790 for (i = 0; i < MONO_TABLE_NUM; i++) {
791 MonoTableInfo *table = &meta->tables [i];
792 if (table->rows == 0)
795 table->row_size = mono_metadata_compute_size (meta, i, &table->size_bitfield);
797 base += table->rows * table->row_size;
802 * mono_metadata_locate:
803 * @meta: metadata context
804 * @table: table code.
805 * @idx: index of element to retrieve from @table.
807 * Returns: a pointer to the @idx element in the metadata table
808 * whose code is @table.
811 mono_metadata_locate (MonoImage *meta, int table, int idx)
813 /* idx == 0 refers always to NULL */
814 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
816 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
820 * mono_metadata_locate_token:
821 * @meta: metadata context
822 * @token: metadata token
824 * Returns: a pointer to the data in the metadata represented by the
828 mono_metadata_locate_token (MonoImage *meta, guint32 token)
830 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
834 * mono_metadata_string_heap:
835 * @meta: metadata context
836 * @index: index into the string heap.
838 * Returns: an in-memory pointer to the @index in the string heap.
841 mono_metadata_string_heap (MonoImage *meta, guint32 index)
843 g_return_val_if_fail (index < meta->heap_strings.size, "");
844 return meta->heap_strings.data + index;
848 * mono_metadata_user_string:
849 * @meta: metadata context
850 * @index: index into the user string heap.
852 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
855 mono_metadata_user_string (MonoImage *meta, guint32 index)
857 g_return_val_if_fail (index < meta->heap_us.size, "");
858 return meta->heap_us.data + index;
862 * mono_metadata_blob_heap:
863 * @meta: metadata context
864 * @index: index into the blob.
866 * Returns: an in-memory pointer to the @index in the Blob heap.
869 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
871 g_return_val_if_fail (index < meta->heap_blob.size, "");
872 return meta->heap_blob.data + index;
876 * mono_metadata_guid_heap:
877 * @meta: metadata context
878 * @index: index into the guid heap.
880 * Returns: an in-memory pointer to the @index in the guid heap.
883 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
886 index *= 16; /* adjust for guid size and 1-based index */
887 g_return_val_if_fail (index < meta->heap_guid.size, "");
888 return meta->heap_guid.data + index;
892 dword_align (const char *ptr)
894 #if SIZEOF_VOID_P == 8
895 return (const char *) (((guint64) (ptr + 3)) & ~3);
897 return (const char *) (((guint32) (ptr + 3)) & ~3);
902 * mono_metadata_decode_row:
903 * @t: table to extract information from.
904 * @idx: index in table.
905 * @res: array of @res_size cols to store the results in
907 * This decompresses the metadata element @idx in table @t
908 * into the guint32 @res array that has res_size elements
911 mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res_size)
913 guint32 bitfield = t->size_bitfield;
914 int i, count = mono_metadata_table_count (bitfield);
915 const char *data = t->base + idx * t->row_size;
917 g_assert (res_size == count);
919 for (i = 0; i < count; i++) {
920 int n = mono_metadata_table_size (bitfield, i);
924 res [i] = *data; break;
926 res [i] = read16 (data); break;
928 res [i] = read32 (data); break;
930 g_assert_not_reached ();
937 * mono_metadata_decode_row_col:
938 * @t: table to extract information from.
939 * @idx: index for row in table.
940 * @col: column in the row.
942 * This function returns the value of column @col from the @idx
943 * row in the table @t.
946 mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col)
948 guint32 bitfield = t->size_bitfield;
950 register const char *data = t->base + idx * t->row_size;
953 g_assert (col < mono_metadata_table_count (bitfield));
955 n = mono_metadata_table_size (bitfield, 0);
956 for (i = 0; i < col; ++i) {
958 n = mono_metadata_table_size (bitfield, i + 1);
964 return read16 (data);
966 return read32 (data);
968 g_assert_not_reached ();
973 * mono_metadata_decode_blob_size:
974 * @ptr: pointer to a blob object
975 * @rptr: the new position of the pointer
977 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
979 * Returns: the size of the blob object
982 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
984 const unsigned char *ptr = (const unsigned char *)xptr;
987 if ((*ptr & 0x80) == 0){
988 size = ptr [0] & 0x7f;
990 } else if ((*ptr & 0x40) == 0){
991 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
994 size = ((ptr [0] & 0x1f) << 24) +
1007 * mono_metadata_decode_value:
1008 * @ptr: pointer to decode from
1009 * @rptr: the new position of the pointer
1011 * This routine decompresses 32-bit values as specified in the "Blob and
1012 * Signature" section (22.2)
1014 * Returns: the decoded value
1017 mono_metadata_decode_value (const char *_ptr, const char **rptr)
1019 const unsigned char *ptr = (const unsigned char *) _ptr;
1020 unsigned char b = *ptr;
1023 if ((b & 0x80) == 0){
1026 } else if ((b & 0x40) == 0){
1027 len = ((b & 0x3f) << 8 | ptr [1]);
1030 len = ((b & 0x1f) << 24) |
1043 * mono_metadata_parse_typedef_or_ref:
1044 * @m: a metadata context.
1045 * @ptr: a pointer to an encoded TypedefOrRef in @m
1046 * @rptr: pointer updated to match the end of the decoded stream
1048 * Returns: a token valid in the @m metadata decoded from
1049 * the compressed representation.
1052 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1055 token = mono_metadata_decode_value (ptr, &ptr);
1058 return mono_metadata_token_from_dor (token);
1062 * mono_metadata_parse_custom_mod:
1063 * @m: a metadata context.
1064 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1065 * @ptr: a pointer to (possibly) the start of a custom modifier list
1066 * @rptr: pointer updated to match the end of the decoded stream
1068 * Checks if @ptr points to a type custom modifier compressed representation.
1070 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1073 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1075 MonoCustomMod local;
1076 if ((*ptr == MONO_TYPE_CMOD_OPT) || (*ptr == MONO_TYPE_CMOD_REQD)) {
1079 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1080 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1087 * mono_metadata_parse_array:
1088 * @m: a metadata context.
1089 * @ptr: a pointer to an encoded array description.
1090 * @rptr: pointer updated to match the end of the decoded stream
1092 * Decodes the compressed array description found in the metadata @m at @ptr.
1094 * Returns: a #MonoArrayType structure describing the array type
1098 mono_metadata_parse_array_full (MonoImage *m, MonoGenericContext *generic_context,
1099 const char *ptr, const char **rptr)
1102 MonoArrayType *array = g_new0 (MonoArrayType, 1);
1105 etype = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_TYPE, 0, ptr, &ptr);
1106 array->eklass = mono_class_from_mono_type (etype);
1107 array->rank = mono_metadata_decode_value (ptr, &ptr);
1109 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1110 if (array->numsizes)
1111 array->sizes = g_new0 (int, array->numsizes);
1112 for (i = 0; i < array->numsizes; ++i)
1113 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1115 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1116 if (array->numlobounds)
1117 array->lobounds = g_new0 (int, array->numlobounds);
1118 for (i = 0; i < array->numlobounds; ++i)
1119 array->lobounds [i] = mono_metadata_decode_value (ptr, &ptr);
1127 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1129 return mono_metadata_parse_array_full (m, NULL, ptr, rptr);
1133 * mono_metadata_free_array:
1134 * @array: array description
1136 * Frees the array description returned from mono_metadata_parse_array().
1139 mono_metadata_free_array (MonoArrayType *array)
1141 g_free (array->sizes);
1142 g_free (array->lobounds);
1147 * need to add common field and param attributes combinations:
1150 * public static literal
1153 * private static literal
1155 static const MonoType
1157 /* data, attrs, type, nmods, byref, pinned */
1158 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1159 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1160 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1161 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1162 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1163 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1164 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1165 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1166 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1167 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1168 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1169 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1170 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1171 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1172 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1173 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1174 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1175 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1176 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1177 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1178 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1179 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1180 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1181 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1182 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1183 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1184 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1185 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 0, 0},
1186 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 1, 0},
1187 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1188 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1189 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1190 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1191 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1194 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1196 static GHashTable *type_cache = NULL;
1197 static GHashTable *generic_inst_cache = NULL;
1198 static GHashTable *generic_class_cache = NULL;
1199 static int next_generic_inst_id = 0;
1201 static guint mono_generic_class_hash (gconstpointer data);
1204 * MonoTypes with modifies are never cached, so we never check or use that field.
1207 mono_type_hash (gconstpointer data)
1209 const MonoType *type = (const MonoType *) data;
1210 if (type->type == MONO_TYPE_GENERICINST)
1211 return mono_generic_class_hash (type->data.generic_class);
1213 return type->type | (type->byref << 8) | (type->attrs << 9);
1217 mono_type_equal (gconstpointer ka, gconstpointer kb)
1219 const MonoType *a = (const MonoType *) ka;
1220 const MonoType *b = (const MonoType *) kb;
1222 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1224 /* need other checks */
1229 mono_generic_inst_hash (gconstpointer data)
1231 const MonoGenericInst *ginst = (const MonoGenericInst *) data;
1235 for (i = 0; i < ginst->type_argc; ++i) {
1237 hash += mono_metadata_type_hash (ginst->type_argv [i]);
1240 return hash ^ (ginst->is_open << 8);
1244 mono_generic_inst_equal (gconstpointer ka, gconstpointer kb)
1246 const MonoGenericInst *a = (const MonoGenericInst *) ka;
1247 const MonoGenericInst *b = (const MonoGenericInst *) kb;
1250 if ((a->is_open != b->is_open) || (a->type_argc != b->type_argc) || (a->is_reference != b->is_reference))
1252 for (i = 0; i < a->type_argc; ++i) {
1253 if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], FALSE))
1260 mono_generic_class_hash (gconstpointer data)
1262 const MonoGenericClass *gclass = (const MonoGenericClass *) data;
1263 guint hash = mono_metadata_type_hash (&gclass->container_class->byval_arg);
1266 hash += mono_generic_inst_hash (gclass->inst);
1272 mono_generic_class_equal (gconstpointer ka, gconstpointer kb)
1274 const MonoGenericClass *a = (const MonoGenericClass *) ka;
1275 const MonoGenericClass *b = (const MonoGenericClass *) kb;
1277 return _mono_metadata_generic_class_equal (a, b, FALSE);
1281 * mono_metadata_init:
1283 * Initialize the global variables of this module.
1284 * This is a Mono runtime internal function.
1287 mono_metadata_init (void)
1291 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1292 generic_inst_cache = g_hash_table_new (mono_generic_inst_hash, mono_generic_inst_equal);
1293 generic_class_cache = g_hash_table_new (mono_generic_class_hash, mono_generic_class_equal);
1295 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1296 g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
1300 * mono_metadata_parse_type:
1301 * @m: metadata context
1302 * @mode: king of type that may be found at @ptr
1303 * @opt_attrs: optional attributes to store in the returned type
1304 * @ptr: pointer to the type representation
1305 * @rptr: pointer updated to match the end of the decoded stream
1307 * Decode a compressed type description found at @ptr in @m.
1308 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1309 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1310 * This function can be used to decode type descriptions in method signatures,
1311 * field signatures, locals signatures etc.
1313 * To parse a generic type, `generic_container' points to the current class'es
1314 * (the `generic_container' field in the MonoClass) or the current generic method's
1315 * (the `generic_container' field in the MonoMethodNormal) generic container.
1316 * When we encounter any MONO_TYPE_VAR or MONO_TYPE_MVAR's, they're looked up in
1317 * this MonoGenericContainer.
1318 * This is a Mono runtime internal function.
1320 * Returns: a #MonoType structure representing the decoded type.
1323 mono_metadata_parse_type_full (MonoImage *m, MonoGenericContext *generic_context, MonoParseTypeMode mode,
1324 short opt_attrs, const char *ptr, const char **rptr)
1326 MonoType *type, *cached;
1328 gboolean byref = FALSE;
1329 gboolean pinned = FALSE;
1330 const char *tmp_ptr;
1335 * According to the spec, custom modifiers should come before the byref
1336 * flag, but the IL produced by ilasm from the following signature:
1337 * object modopt(...) &
1338 * starts with a byref flag, followed by the modifiers. (bug #49802)
1339 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1340 * it would be better to treat byref as real type constructor instead of
1342 * Also, pinned should come before anything else, but some MSV++ produced
1343 * assemblies violate this (#bug 61990).
1346 /* Count the modifiers first */
1351 case MONO_TYPE_PINNED:
1352 case MONO_TYPE_BYREF:
1355 case MONO_TYPE_CMOD_REQD:
1356 case MONO_TYPE_CMOD_OPT:
1358 mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1366 type = g_malloc0 (sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1367 type->num_mods = count;
1369 g_warning ("got more than 64 modifiers in type");
1373 memset (type, 0, sizeof (MonoType));
1376 /* Parse pinned, byref and custom modifiers */
1381 case MONO_TYPE_PINNED:
1385 case MONO_TYPE_BYREF:
1389 case MONO_TYPE_CMOD_REQD:
1390 case MONO_TYPE_CMOD_OPT:
1391 mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1399 type->attrs = opt_attrs;
1400 type->byref = byref;
1401 type->pinned = pinned ? 1 : 0;
1403 do_mono_metadata_parse_type (type, m, generic_context, ptr, &ptr);
1409 /* FIXME: remove the != MONO_PARSE_PARAM condition, this accounts for
1410 * almost 10k (about 2/3rds) of all MonoType's we create.
1412 if (mode != MONO_PARSE_PARAM && !type->num_mods) {
1413 /* no need to free type here, because it is on the stack */
1414 if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
1416 return &type->data.klass->this_arg;
1418 return &type->data.klass->byval_arg;
1420 /* No need to use locking since nobody is modifying the hash table */
1421 if ((cached = g_hash_table_lookup (type_cache, type)))
1425 /*printf ("%x%c %s\n", type->attrs, type->pinned ? 'p' : ' ', mono_type_full_name (type));*/
1428 type = g_memdup (&stype, sizeof (MonoType));
1433 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
1434 const char *ptr, const char **rptr)
1436 return mono_metadata_parse_type_full (m, NULL, mode, opt_attrs, ptr, rptr);
1440 * mono_metadata_parse_signature_full:
1441 * @image: metadata context
1442 * @generic_container: generic container
1443 * @toke: metadata token
1445 * Decode a method signature stored in the STANDALONESIG table
1447 * Returns: a MonoMethodSignature describing the signature.
1449 MonoMethodSignature*
1450 mono_metadata_parse_signature_full (MonoImage *image, MonoGenericContainer *generic_container, guint32 token)
1452 MonoTableInfo *tables = image->tables;
1453 guint32 idx = mono_metadata_token_index (token);
1458 return mono_lookup_dynamic_token (image, token);
1460 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1462 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1464 ptr = mono_metadata_blob_heap (image, sig);
1465 mono_metadata_decode_blob_size (ptr, &ptr);
1467 return mono_metadata_parse_method_signature_full (image, generic_container, FALSE, ptr, NULL);
1471 * mono_metadata_parse_signature:
1472 * @image: metadata context
1473 * @toke: metadata token
1475 * Decode a method signature stored in the STANDALONESIG table
1477 * Returns: a MonoMethodSignature describing the signature.
1479 MonoMethodSignature*
1480 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1482 return mono_metadata_parse_signature_full (image, NULL, token);
1486 * mono_metadata_signature_alloc:
1487 * @image: metadata context
1488 * @nparmas: number of parameters in the signature
1490 * Allocate a MonoMethodSignature structure with the specified number of params.
1491 * The return type and the params types need to be filled later.
1492 * This is a Mono runtime internal function.
1494 * Returns: the new MonoMethodSignature structure.
1496 MonoMethodSignature*
1497 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1499 MonoMethodSignature *sig;
1501 /* later we want to allocate signatures with mempools */
1502 sig = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1503 sig->param_count = nparams;
1504 sig->sentinelpos = -1;
1510 * mono_metadata_signature_dup:
1511 * @sig: method signature
1513 * Duplicate an existing MonoMethodSignature so it can be modified.
1514 * This is a Mono runtime internal function.
1516 * Returns: the new MonoMethodSignature structure.
1518 MonoMethodSignature*
1519 mono_metadata_signature_dup (MonoMethodSignature *sig)
1523 sigsize = sizeof (MonoMethodSignature) + (sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *);
1524 return g_memdup (sig, sigsize);
1528 * mono_metadata_parse_method_signature:
1529 * @m: metadata context
1530 * @generic_container: generics container
1531 * @def: the MethodDef index or 0 for Ref signatures.
1532 * @ptr: pointer to the signature metadata representation
1533 * @rptr: pointer updated to match the end of the decoded stream
1535 * Decode a method signature stored at @ptr.
1536 * This is a Mono runtime internal function.
1538 * Returns: a MonoMethodSignature describing the signature.
1540 MonoMethodSignature *
1541 mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContainer *container,
1542 int def, const char *ptr, const char **rptr)
1544 MonoMethodSignature *method;
1545 int i, *pattrs = NULL;
1546 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1547 guint32 gen_param_count = 0;
1548 gboolean is_open = FALSE;
1551 gen_param_count = 1;
1556 call_convention = *ptr & 0x0F;
1558 if (gen_param_count)
1559 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1560 param_count = mono_metadata_decode_value (ptr, &ptr);
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;
1573 pattrs = g_new0 (int, 1 + param_count);
1574 for (i = param_index; i < lastp; ++i) {
1575 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1576 pattrs [cols [MONO_PARAM_SEQUENCE]] = cols [MONO_PARAM_FLAGS];
1579 method = mono_metadata_signature_alloc (m, param_count);
1580 method->hasthis = hasthis;
1581 method->explicit_this = explicit_this;
1582 method->call_convention = call_convention;
1583 method->generic_param_count = gen_param_count;
1585 if (call_convention != 0xa) {
1586 method->ret = mono_metadata_parse_type_full (
1587 m, (MonoGenericContext *) container, MONO_PARSE_RET,
1588 pattrs ? pattrs [0] : 0, ptr, &ptr);
1589 is_open = mono_class_is_open_constructed_type (method->ret);
1592 for (i = 0; i < method->param_count; ++i) {
1593 if (*ptr == MONO_TYPE_SENTINEL) {
1594 if (method->call_convention != MONO_CALL_VARARG || def)
1595 g_error ("found sentinel for methoddef or no vararg method");
1596 method->sentinelpos = i;
1599 method->params [i] = mono_metadata_parse_type_full (
1600 m, (MonoGenericContext *) container, MONO_PARSE_PARAM,
1601 pattrs ? pattrs [i+1] : 0, ptr, &ptr);
1603 is_open = mono_class_is_open_constructed_type (method->params [i]);
1606 method->has_type_parameters = is_open;
1608 if (def && (method->call_convention == MONO_CALL_VARARG))
1609 method->sentinelpos = method->param_count;
1616 * Add signature to a cache and increase ref count...
1623 * mono_metadata_parse_method_signature:
1624 * @m: metadata context
1625 * @def: the MethodDef index or 0 for Ref signatures.
1626 * @ptr: pointer to the signature metadata representation
1627 * @rptr: pointer updated to match the end of the decoded stream
1629 * Decode a method signature stored at @ptr.
1630 * This is a Mono runtime internal function.
1632 * Returns: a MonoMethodSignature describing the signature.
1634 MonoMethodSignature *
1635 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1637 return mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr);
1641 * mono_metadata_free_method_signature:
1642 * @sig: signature to destroy
1644 * Free the memory allocated in the signature @sig.
1647 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1650 mono_metadata_free_type (sig->ret);
1651 for (i = 0; i < sig->param_count; ++i)
1652 mono_metadata_free_type (sig->params [i]);
1658 * mono_metadata_lookup_generic_inst:
1660 * Check whether the newly created generic instantiation @ginst already exists
1661 * in the cache and return the cached value in this case. Otherwise insert
1662 * it into the cache.
1664 * Use this method each time you create a new `MonoGenericInst' to ensure
1665 * proper caching. Only use the returned value as the argument passed to this
1666 * method may be freed.
1670 mono_metadata_lookup_generic_inst (MonoGenericInst *ginst)
1672 MonoGenericInst *cached;
1675 cached = g_hash_table_lookup (generic_inst_cache, ginst);
1677 for (i = 0; i < ginst->type_argc; i++)
1678 mono_metadata_free_type (ginst->type_argv [i]);
1679 g_free (ginst->type_argv);
1684 ginst->id = ++next_generic_inst_id;
1685 g_hash_table_insert (generic_inst_cache, ginst, ginst);
1691 * mono_metadata_lookup_generic_class:
1693 * Check whether the newly created generic class @gclass already exists
1694 * in the cache and return the cached value in this case. Otherwise insert
1695 * it into the cache and return NULL.
1697 * Returns: the previosly cached generic class or NULL if it has been newly
1698 * inserted into the cache.
1702 mono_metadata_lookup_generic_class (MonoGenericClass *gclass)
1704 MonoGenericClass *cached;
1706 cached = g_hash_table_lookup (generic_class_cache, gclass);
1710 g_hash_table_insert (generic_class_cache, gclass, gclass);
1715 * mono_metadata_inflate_generic_inst:
1717 * Instantiate the generic instance @ginst with the context @context.
1721 mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context)
1723 MonoGenericInst *nginst;
1726 nginst = g_new0 (MonoGenericInst, 1);
1727 nginst->type_argc = ginst->type_argc;
1728 nginst->type_argv = g_new0 (MonoType*, nginst->type_argc);
1729 nginst->is_reference = 1;
1731 for (i = 0; i < nginst->type_argc; i++) {
1732 MonoType *t = mono_class_inflate_generic_type (ginst->type_argv [i], context);
1734 if (!nginst->is_open)
1735 nginst->is_open = mono_class_is_open_constructed_type (t);
1736 if (nginst->is_reference)
1737 nginst->is_reference = MONO_TYPE_IS_REFERENCE (t);
1739 nginst->type_argv [i] = t;
1742 return mono_metadata_lookup_generic_inst (nginst);
1746 mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContext *generic_context,
1747 int count, const char *ptr, const char **rptr)
1749 MonoGenericInst *ginst;
1752 ginst = g_new0 (MonoGenericInst, 1);
1753 ginst->type_argc = count;
1754 ginst->type_argv = g_new0 (MonoType*, count);
1755 ginst->is_reference = 1;
1757 for (i = 0; i < ginst->type_argc; i++) {
1758 MonoType *t = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_TYPE, 0, ptr, &ptr);
1760 ginst->type_argv [i] = t;
1761 if (!ginst->is_open)
1762 ginst->is_open = mono_class_is_open_constructed_type (t);
1763 if (ginst->is_reference)
1764 ginst->is_reference = MONO_TYPE_IS_REFERENCE (t);
1770 return mono_metadata_lookup_generic_inst (ginst);
1774 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContext *generic_context,
1775 const char *ptr, const char **rptr)
1777 MonoInflatedGenericClass *igclass;
1778 MonoGenericClass *gclass, *cached;
1783 igclass = g_new0 (MonoInflatedGenericClass, 1);
1784 gclass = &igclass->generic_class;
1785 gclass->is_inflated = TRUE;
1787 type->data.generic_class = gclass;
1789 gclass->context = g_new0 (MonoGenericContext, 1);
1790 gclass->context->gclass = gclass;
1793 * Create the klass before parsing the type arguments.
1794 * This is required to support "recursive" definitions.
1795 * See mcs/tests/gen-23.cs for an example.
1797 igclass->klass = g_new0 (MonoClass, 1);
1799 gtype = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1800 gclass->container_class = gklass = mono_class_from_mono_type (gtype);
1802 g_assert (gklass->generic_container);
1803 gclass->context->container = gklass->generic_container;
1805 count = mono_metadata_decode_value (ptr, &ptr);
1807 gclass->inst = mono_metadata_parse_generic_inst (m, generic_context, count, ptr, &ptr);
1813 * We may be called multiple times on different metadata to create the same
1814 * instantiated type. This happens for instance if we're part of a method or
1815 * local variable signature.
1817 * It's important to return the same MonoGenericClass * for each particualar
1818 * instantiation of a generic type (ie "Stack<Int32>") to make static fields
1821 * According to the spec ($26.1.5), a static variable in a generic class
1822 * declaration is shared amongst all instances of the same closed constructed
1826 cached = g_hash_table_lookup (generic_class_cache, gclass);
1828 g_free (igclass->klass);
1831 type->data.generic_class = cached;
1834 g_hash_table_insert (generic_class_cache, gclass, gclass);
1836 mono_stats.generic_instance_count++;
1837 mono_stats.generics_metadata_size += sizeof (MonoGenericClass) +
1838 sizeof (MonoGenericContext) +
1839 gclass->inst->type_argc * sizeof (MonoType);
1845 * @gc: The generic container to normalize
1846 * @type: The kind of generic parameters the resulting generic-container should contain
1849 static MonoGenericContainer *
1850 select_container (MonoGenericContainer *gc, MonoTypeEnum type)
1852 gboolean is_var = (type == MONO_TYPE_VAR);
1856 g_assert (is_var || type == MONO_TYPE_MVAR);
1858 if (is_var && gc->parent)
1860 * The current MonoGenericContainer is a generic method -> its `parent'
1861 * points to the containing class'es container.
1866 * Ensure that we have the correct type of GenericContainer.
1868 g_assert (is_var == !gc->is_method);
1874 * mono_metadata_parse_generic_param:
1875 * @generic_container: Our MonoClass's or MonoMethodNormal's MonoGenericContainer;
1876 * see mono_metadata_parse_type_full() for details.
1877 * Internal routine to parse a generic type parameter.
1879 static MonoGenericParam *
1880 mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContainer *generic_container,
1881 MonoTypeEnum type, const char *ptr, const char **rptr)
1883 int index = mono_metadata_decode_value (ptr, &ptr);
1887 generic_container = select_container (generic_container, type);
1888 if (!generic_container) {
1889 /* Create dummy MonoGenericParam */
1890 MonoGenericParam *param = g_new0 (MonoGenericParam, 1);
1891 param->name = g_strdup_printf ("%d", index);
1897 g_assert (index < generic_container->type_argc);
1898 return &generic_container->type_params [index];
1902 * do_mono_metadata_parse_type:
1903 * @type: MonoType to be filled in with the return value
1905 * @generic_context: generics_context
1906 * @ptr: pointer to the encoded type
1907 * @rptr: pointer where the end of the encoded type is saved
1909 * Internal routine used to "fill" the contents of @type from an
1910 * allocated pointer. This is done this way to avoid doing too
1911 * many mini-allocations (particularly for the MonoFieldType which
1912 * most of the time is just a MonoType, but sometimes might be augmented).
1914 * This routine is used by mono_metadata_parse_type and
1915 * mono_metadata_parse_field_type
1917 * This extracts a Type as specified in Partition II (22.2.12)
1920 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContext *generic_context,
1921 const char *ptr, const char **rptr)
1923 MonoGenericContainer *container = generic_context ? generic_context->container : NULL;
1924 type->type = mono_metadata_decode_value (ptr, &ptr);
1926 switch (type->type){
1927 case MONO_TYPE_VOID:
1928 case MONO_TYPE_BOOLEAN:
1929 case MONO_TYPE_CHAR:
1942 case MONO_TYPE_STRING:
1943 case MONO_TYPE_OBJECT:
1944 case MONO_TYPE_TYPEDBYREF:
1946 case MONO_TYPE_VALUETYPE:
1947 case MONO_TYPE_CLASS: {
1949 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
1950 type->data.klass = mono_class_get (m, token);
1953 case MONO_TYPE_SZARRAY: {
1954 MonoType *etype = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1955 type->data.klass = mono_class_from_mono_type (etype);
1956 mono_metadata_free_type (etype);
1960 type->data.type = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1962 case MONO_TYPE_FNPTR:
1963 type->data.method = mono_metadata_parse_method_signature_full (m, container, 0, ptr, &ptr);
1965 case MONO_TYPE_ARRAY:
1966 type->data.array = mono_metadata_parse_array_full (m, generic_context, ptr, &ptr);
1968 case MONO_TYPE_MVAR:
1970 type->data.generic_param = mono_metadata_parse_generic_param (m, container, type->type, ptr, &ptr);
1972 case MONO_TYPE_GENERICINST:
1973 do_mono_metadata_parse_generic_class (type, m, generic_context, ptr, &ptr);
1976 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
1984 * mono_metadata_free_type:
1985 * @type: type to free
1987 * Free the memory allocated for type @type.
1990 mono_metadata_free_type (MonoType *type)
1992 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
1995 switch (type->type){
1996 case MONO_TYPE_OBJECT:
1997 case MONO_TYPE_STRING:
1998 if (!type->data.klass)
2001 case MONO_TYPE_CLASS:
2002 case MONO_TYPE_VALUETYPE:
2003 if (type == &type->data.klass->byval_arg || type == &type->data.klass->this_arg)
2007 mono_metadata_free_type (type->data.type);
2009 case MONO_TYPE_FNPTR:
2010 mono_metadata_free_method_signature (type->data.method);
2012 case MONO_TYPE_ARRAY:
2013 mono_metadata_free_array (type->data.array);
2021 hex_dump (const char *buffer, int base, int count)
2023 int show_header = 1;
2031 for (i = 0; i < count; i++){
2034 printf ("\n0x%08x: ", (unsigned char) base + i);
2036 printf ("%02x ", (unsigned char) (buffer [i]));
2043 * @mh: The Method header
2044 * @ptr: Points to the beginning of the Section Data (25.3)
2047 parse_section_data (MonoImage *m, MonoMethodHeader *mh, const unsigned char *ptr)
2049 unsigned char sect_data_flags;
2050 const unsigned char *sptr;
2052 guint32 sect_data_len;
2055 /* align on 32-bit boundary */
2056 /* FIXME: not 64-bit clean code */
2057 sptr = ptr = dword_align (ptr);
2058 sect_data_flags = *ptr;
2061 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
2063 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
2066 sect_data_len = ptr [0];
2070 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
2071 hex_dump (sptr, 0, sect_data_len+8);
2072 g_print ("\nheader: ");
2073 hex_dump (sptr-4, 0, 4);
2077 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
2078 const unsigned char *p = dword_align (ptr);
2080 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
2081 /* we could just store a pointer if we don't need to byteswap */
2082 mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
2083 for (i = 0; i < mh->num_clauses; ++i) {
2084 MonoExceptionClause *ec = &mh->clauses [i];
2087 ec->flags = read32 (p);
2088 ec->try_offset = read32 (p + 4);
2089 ec->try_len = read32 (p + 8);
2090 ec->handler_offset = read32 (p + 12);
2091 ec->handler_len = read32 (p + 16);
2092 tof_value = read32 (p + 20);
2095 ec->flags = read16 (p);
2096 ec->try_offset = read16 (p + 2);
2097 ec->try_len = *(p + 4);
2098 ec->handler_offset = read16 (p + 5);
2099 ec->handler_len = *(p + 7);
2100 tof_value = read32 (p + 8);
2103 if (ec->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2104 ec->data.filter_offset = tof_value;
2105 } else if (ec->flags == MONO_EXCEPTION_CLAUSE_NONE) {
2106 ec->data.catch_class = tof_value? mono_class_get (m, tof_value): 0;
2108 ec->data.catch_class = NULL;
2110 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
2114 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
2115 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
2122 * mono_metadata_parse_mh_full:
2123 * @m: metadata context
2124 * @generic_context: generics context
2125 * @ptr: pointer to the method header.
2127 * Decode the method header at @ptr, including pointer to the IL code,
2128 * info about local variables and optional exception tables.
2129 * This is a Mono runtime internal function.
2131 * Returns: a MonoMethodHeader.
2134 mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContext *generic_context, const char *ptr)
2136 MonoMethodHeader *mh;
2137 unsigned char flags = *(const unsigned char *) ptr;
2138 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
2140 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
2141 const unsigned char *code;
2144 g_return_val_if_fail (ptr != NULL, NULL);
2147 case METHOD_HEADER_TINY_FORMAT:
2148 mh = g_new0 (MonoMethodHeader, 1);
2151 local_var_sig_tok = 0;
2152 mh->code_size = flags >> 2;
2155 case METHOD_HEADER_TINY_FORMAT1:
2156 mh = g_new0 (MonoMethodHeader, 1);
2159 local_var_sig_tok = 0;
2162 * The spec claims 3 bits, but the Beta2 is
2165 mh->code_size = flags >> 2;
2168 case METHOD_HEADER_FAT_FORMAT:
2169 fat_flags = read16 (ptr);
2171 hsize = (fat_flags >> 12) & 0xf;
2172 max_stack = read16 (ptr);
2174 code_size = read32 (ptr);
2176 local_var_sig_tok = read32 (ptr);
2179 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
2186 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
2190 * There are more sections
2192 ptr = code + code_size;
2198 if (local_var_sig_tok) {
2199 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
2200 const char *locals_ptr;
2201 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
2202 int len=0, i, bsize;
2204 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
2205 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
2206 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
2207 if (*locals_ptr != 0x07)
2208 g_warning ("wrong signature for locals blob");
2210 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
2211 mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
2212 mh->num_locals = len;
2213 for (i = 0; i < len; ++i)
2214 mh->locals [i] = mono_metadata_parse_type_full (
2215 m, generic_context, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
2217 mh = g_new0 (MonoMethodHeader, 1);
2220 mh->code_size = code_size;
2221 mh->max_stack = max_stack;
2222 mh->init_locals = init_locals;
2223 if (fat_flags & METHOD_HEADER_MORE_SECTS)
2224 parse_section_data (m, mh, (const unsigned char*)ptr);
2229 * mono_metadata_parse_mh:
2230 * @generic_context: generics context
2231 * @ptr: pointer to the method header.
2233 * Decode the method header at @ptr, including pointer to the IL code,
2234 * info about local variables and optional exception tables.
2235 * This is a Mono runtime internal function.
2237 * Returns: a MonoMethodHeader.
2240 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
2242 return mono_metadata_parse_mh_full (m, NULL, ptr);
2246 * mono_metadata_free_mh:
2247 * @mh: a method header
2249 * Free the memory allocated for the method header.
2250 * This is a Mono runtime internal function.
2253 mono_metadata_free_mh (MonoMethodHeader *mh)
2256 for (i = 0; i < mh->num_locals; ++i)
2257 mono_metadata_free_type (mh->locals[i]);
2258 g_free (mh->clauses);
2263 * mono_method_header_get_code:
2264 * @header: a MonoMethodHeader pointer
2265 * @code_size: memory location for returning the code size
2266 * @max_stack: memory location for returning the max stack
2268 * Method header accessor to retreive info about the IL code properties:
2269 * a pointer to the IL code itself, the size of the code and the max number
2270 * of stack slots used by the code.
2272 * Returns: pointer to the IL code represented by the method header.
2274 const unsigned char*
2275 mono_method_header_get_code (MonoMethodHeader *header, guint32* code_size, guint32* max_stack)
2278 *code_size = header->code_size;
2280 *max_stack = header->max_stack;
2281 return header->code;
2285 * mono_method_header_get_locals:
2286 * @header: a MonoMethodHeader pointer
2287 * @num_locals: memory location for returning the number of local variables
2288 * @init_locals: memory location for returning the init_locals flag
2290 * Method header accessor to retreive info about the local variables:
2291 * an array of local types, the number of locals and whether the locals
2292 * are supposed to be initialized to 0 on method entry
2294 * Returns: pointer to an array of types of the local variables
2297 mono_method_header_get_locals (MonoMethodHeader *header, guint32* num_locals, gboolean *init_locals)
2300 *num_locals = header->num_locals;
2302 *init_locals = header->init_locals;
2303 return header->locals;
2307 * mono_method_header_get_num_clauses:
2308 * @header: a MonoMethodHeader pointer
2310 * Method header accessor to retreive the number of exception clauses.
2312 * Returns: the number of exception clauses present
2315 mono_method_header_get_num_clauses (MonoMethodHeader *header)
2317 return header->num_clauses;
2321 * mono_method_header_get_clauses:
2322 * @header: a MonoMethodHeader pointer
2323 * @method: MonoMethod the header belongs to
2324 * @iter: pointer to a iterator
2325 * @clause: pointer to a MonoExceptionClause structure which will be filled with the info
2327 * Get the info about the exception clauses in the method. Set *iter to NULL to
2328 * initiate the iteration, then call the method repeatedly until it returns FALSE.
2329 * At each iteration, the structure pointed to by clause if filled with the
2330 * exception clause information.
2332 * Returns: TRUE if clause was filled with info, FALSE if there are no more exception
2336 mono_method_header_get_clauses (MonoMethodHeader *header, MonoMethod *method, gpointer *iter, MonoExceptionClause *clause)
2338 MonoExceptionClause *sc;
2339 /* later we'll be able to use this interface to parse the clause info on demand,
2340 * without allocating anything.
2342 if (!iter || !header->num_clauses)
2345 *iter = sc = header->clauses;
2351 if (sc < header->clauses + header->num_clauses) {
2360 * mono_metadata_parse_field_type:
2361 * @m: metadata context to extract information from
2362 * @ptr: pointer to the field signature
2363 * @rptr: pointer updated to match the end of the decoded stream
2365 * Parses the field signature, and returns the type information for it.
2367 * Returns: The MonoType that was extracted from @ptr.
2370 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
2372 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
2376 * mono_metadata_parse_param:
2377 * @m: metadata context to extract information from
2378 * @ptr: pointer to the param signature
2379 * @rptr: pointer updated to match the end of the decoded stream
2381 * Parses the param signature, and returns the type information for it.
2383 * Returns: The MonoType that was extracted from @ptr.
2386 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
2388 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
2392 * mono_metadata_token_from_dor:
2393 * @dor_token: A TypeDefOrRef coded index
2395 * dor_token is a TypeDefOrRef coded index: it contains either
2396 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
2397 * bits contain an index into the table.
2399 * Returns: an expanded token
2402 mono_metadata_token_from_dor (guint32 dor_index)
2406 table = dor_index & 0x03;
2407 idx = dor_index >> 2;
2410 case 0: /* TypeDef */
2411 return MONO_TOKEN_TYPE_DEF | idx;
2412 case 1: /* TypeRef */
2413 return MONO_TOKEN_TYPE_REF | idx;
2414 case 2: /* TypeSpec */
2415 return MONO_TOKEN_TYPE_SPEC | idx;
2417 g_assert_not_reached ();
2424 * We use this to pass context information to the row locator
2427 int idx; /* The index that we are trying to locate */
2428 int col_idx; /* The index in the row where idx may be stored */
2429 MonoTableInfo *t; /* pointer to the table */
2434 * How the row locator works.
2439 * ___|___------> _______
2442 * A column in the rows of table A references an index in table B.
2443 * For example A may be the TYPEDEF table and B the METHODDEF table.
2445 * Given an index in table B we want to get the row in table A
2446 * where the column n references our index in B.
2448 * In the locator_t structure:
2450 * col_idx is the column number
2451 * index is the index in table B
2452 * result will be the index in table A
2455 * Table A Table B column (in table A)
2456 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
2457 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
2458 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
2459 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
2460 * METHODSEM PROPERTY ASSOCIATION (encoded index)
2462 * Note that we still don't support encoded indexes.
2466 typedef_locator (const void *a, const void *b)
2468 locator_t *loc = (locator_t *) a;
2469 const char *bb = (const char *) b;
2470 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
2471 guint32 col, col_next;
2473 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
2479 * Need to check that the next row is valid.
2481 if (typedef_index + 1 < loc->t->rows) {
2482 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
2483 if (loc->idx >= col_next)
2486 if (col == col_next)
2490 loc->result = typedef_index;
2496 table_locator (const void *a, const void *b)
2498 locator_t *loc = (locator_t *) a;
2499 const char *bb = (const char *) b;
2500 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2503 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2505 if (loc->idx == col) {
2506 loc->result = table_index;
2516 declsec_locator (const void *a, const void *b)
2518 locator_t *loc = (locator_t *) a;
2519 const char *bb = (const char *) b;
2520 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2523 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2525 if (loc->idx == col) {
2526 loc->result = table_index;
2536 * mono_metadata_typedef_from_field:
2537 * @meta: metadata context
2538 * @index: FieldDef token
2540 * Returns: the 1-based index into the TypeDef table of the type that
2541 * declared the field described by @index, or 0 if not found.
2544 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
2546 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2552 loc.idx = mono_metadata_token_index (index);
2553 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
2556 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2557 g_assert_not_reached ();
2559 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2560 return loc.result + 1;
2564 * mono_metadata_typedef_from_method:
2565 * @meta: metadata context
2566 * @index: MethodDef token
2568 * Returns: the 1-based index into the TypeDef table of the type that
2569 * declared the method described by @index. 0 if not found.
2572 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
2574 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2580 loc.idx = mono_metadata_token_index (index);
2581 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
2584 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2585 g_assert_not_reached ();
2587 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2588 return loc.result + 1;
2592 * mono_metadata_interfaces_from_typedef_full:
2593 * @meta: metadata context
2594 * @index: typedef token
2596 * The array of interfaces that the @index typedef token implements is returned in
2597 * @interfaces. The number of elemnts in the array is returned in @count.
2599 * Returns: TRUE on success, FALSE on failure.
2602 mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, MonoClass ***interfaces, guint *count, MonoGenericContext *context)
2604 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
2607 guint32 cols [MONO_INTERFACEIMPL_SIZE];
2616 loc.idx = mono_metadata_token_index (index);
2617 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
2620 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2625 * We may end up in the middle of the rows...
2628 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
2635 while (start < tdef->rows) {
2636 mono_metadata_decode_row (tdef, start, cols, MONO_INTERFACEIMPL_SIZE);
2637 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
2639 result = g_renew (MonoClass*, result, i + 1);
2640 result [i] = mono_class_get_full (
2641 meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]), context);
2645 *interfaces = result;
2650 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
2652 MonoClass **interfaces;
2655 rv = mono_metadata_interfaces_from_typedef_full (meta, index, &interfaces, count, NULL);
2663 * mono_metadata_nested_in_typedef:
2664 * @meta: metadata context
2665 * @index: typedef token
2667 * Returns: the 1-based index into the TypeDef table of the type
2668 * where the type described by @index is nested.
2669 * Retruns 0 if @index describes a non-nested type.
2672 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
2674 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2680 loc.idx = mono_metadata_token_index (index);
2681 loc.col_idx = MONO_NESTED_CLASS_NESTED;
2684 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2687 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2688 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
2692 * mono_metadata_nesting_typedef:
2693 * @meta: metadata context
2694 * @index: typedef token
2696 * Returns: the 1-based index into the TypeDef table of the first type
2697 * that is nested inside the type described by @index. The search starts at
2698 * @start_index. returns 0 if no such type is found.
2701 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
2703 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2705 guint32 class_index = mono_metadata_token_index (index);
2710 start = start_index;
2712 while (start <= tdef->rows) {
2713 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
2719 if (start > tdef->rows)
2726 * mono_metadata_packing_from_typedef:
2727 * @meta: metadata context
2728 * @index: token representing a type
2730 * Returns: the info stored in the ClassLAyout table for the given typedef token
2731 * into the @packing and @size pointers.
2732 * Returns 0 if the info is not found.
2735 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
2737 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
2739 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
2744 loc.idx = mono_metadata_token_index (index);
2745 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
2748 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2751 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
2753 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
2755 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
2757 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2758 return loc.result + 1;
2762 * mono_metadata_custom_attrs_from_index:
2763 * @meta: metadata context
2764 * @index: token representing the parent
2766 * Returns: the 1-based index into the CustomAttribute table of the first
2767 * attribute which belongs to the metadata object described by @index.
2768 * Returns 0 if no such attribute is found.
2771 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
2773 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
2780 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
2783 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2786 /* Find the first entry by searching backwards */
2787 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
2790 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2791 return loc.result + 1;
2795 * mono_metadata_declsec_from_index:
2796 * @meta: metadata context
2797 * @index: token representing the parent
2799 * Returns: the 0-based index into the DeclarativeSecurity table of the first
2800 * attribute which belongs to the metadata object described by @index.
2801 * Returns -1 if no such attribute is found.
2804 mono_metadata_declsec_from_index (MonoImage *meta, guint32 index)
2806 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_DECLSECURITY];
2813 loc.col_idx = MONO_DECL_SECURITY_PARENT;
2816 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
2819 /* Find the first entry by searching backwards */
2820 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_DECL_SECURITY_PARENT) == index))
2828 mono_backtrace (int limit)
2833 backtrace (array, limit);
2834 names = backtrace_symbols (array, limit);
2835 for (i =0; i < limit; ++i) {
2836 g_print ("\t%s\n", names [i]);
2843 /*#define __alignof__(a) sizeof(a)*/
2844 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
2849 * @t: the type to return the size of
2851 * Returns: the number of bytes required to hold an instance of this
2855 mono_type_size (MonoType *t, gint *align)
2862 *align = __alignof__(gpointer);
2863 return sizeof (gpointer);
2867 case MONO_TYPE_VOID:
2870 case MONO_TYPE_BOOLEAN:
2871 *align = __alignof__(gint8);
2875 *align = __alignof__(gint8);
2877 case MONO_TYPE_CHAR:
2880 *align = __alignof__(gint16);
2884 *align = __alignof__(gint32);
2887 *align = __alignof__(float);
2891 *align = __alignof__(gint64);
2894 *align = __alignof__(double);
2898 *align = __alignof__(gpointer);
2899 return sizeof (gpointer);
2900 case MONO_TYPE_STRING:
2901 *align = __alignof__(gpointer);
2902 return sizeof (gpointer);
2903 case MONO_TYPE_OBJECT:
2904 *align = __alignof__(gpointer);
2905 return sizeof (gpointer);
2906 case MONO_TYPE_VALUETYPE: {
2907 if (t->data.klass->enumtype)
2908 return mono_type_size (t->data.klass->enum_basetype, align);
2910 return mono_class_value_size (t->data.klass, align);
2912 case MONO_TYPE_CLASS:
2913 case MONO_TYPE_SZARRAY:
2915 case MONO_TYPE_FNPTR:
2916 case MONO_TYPE_ARRAY:
2917 *align = __alignof__(gpointer);
2918 return sizeof (gpointer);
2919 case MONO_TYPE_TYPEDBYREF:
2920 return mono_class_value_size (mono_defaults.typed_reference_class, align);
2921 case MONO_TYPE_GENERICINST: {
2922 MonoInflatedGenericClass *gclass;
2923 MonoClass *container_class;
2925 gclass = mono_get_inflated_generic_class (t->data.generic_class);
2926 g_assert (!gclass->generic_class.inst->is_open);
2927 g_assert (!gclass->klass->generic_container);
2929 container_class = gclass->generic_class.container_class;
2931 if (container_class->valuetype) {
2932 if (container_class->enumtype)
2933 return mono_type_size (container_class->enum_basetype, align);
2935 return mono_class_value_size (gclass->klass, align);
2937 *align = __alignof__(gpointer);
2938 return sizeof (gpointer);
2942 case MONO_TYPE_MVAR:
2943 /* FIXME: Martin, this is wrong. */
2944 *align = __alignof__(gpointer);
2945 return sizeof (gpointer);
2947 g_error ("mono_type_size: type 0x%02x unknown", t->type);
2953 * mono_type_stack_size:
2954 * @t: the type to return the size it uses on the stack
2956 * Returns: the number of bytes required to hold an instance of this
2957 * type on the runtime stack
2960 mono_type_stack_size (MonoType *t, gint *align)
2964 g_assert (t != NULL);
2970 *align = __alignof__(gpointer);
2971 return sizeof (gpointer);
2975 case MONO_TYPE_BOOLEAN:
2976 case MONO_TYPE_CHAR:
2985 case MONO_TYPE_STRING:
2986 case MONO_TYPE_OBJECT:
2987 case MONO_TYPE_CLASS:
2988 case MONO_TYPE_SZARRAY:
2990 case MONO_TYPE_FNPTR:
2991 case MONO_TYPE_ARRAY:
2992 *align = __alignof__(gpointer);
2993 return sizeof (gpointer);
2994 case MONO_TYPE_TYPEDBYREF:
2995 *align = __alignof__(gpointer);
2996 return sizeof (gpointer) * 3;
2998 *align = __alignof__(float);
2999 return sizeof (float);
3002 *align = __alignof__(gint64);
3003 return sizeof (gint64);
3005 *align = __alignof__(double);
3006 return sizeof (double);
3007 case MONO_TYPE_VALUETYPE: {
3010 if (t->data.klass->enumtype)
3011 return mono_type_stack_size (t->data.klass->enum_basetype, align);
3013 size = mono_class_value_size (t->data.klass, align);
3015 *align = *align + __alignof__(gpointer) - 1;
3016 *align &= ~(__alignof__(gpointer) - 1);
3018 size += sizeof (gpointer) - 1;
3019 size &= ~(sizeof (gpointer) - 1);
3024 case MONO_TYPE_GENERICINST: {
3025 MonoInflatedGenericClass *gclass;
3026 MonoClass *container_class;
3028 gclass = mono_get_inflated_generic_class (t->data.generic_class);
3029 container_class = gclass->generic_class.container_class;
3031 g_assert (!gclass->generic_class.inst->is_open);
3032 g_assert (!gclass->klass->generic_container);
3034 if (container_class->valuetype) {
3035 if (container_class->enumtype)
3036 return mono_type_stack_size (container_class->enum_basetype, align);
3038 guint32 size = mono_class_value_size (gclass->klass, align);
3040 *align = *align + __alignof__(gpointer) - 1;
3041 *align &= ~(__alignof__(gpointer) - 1);
3043 size += sizeof (gpointer) - 1;
3044 size &= ~(sizeof (gpointer) - 1);
3049 *align = __alignof__(gpointer);
3050 return sizeof (gpointer);
3054 g_error ("type 0x%02x unknown", t->type);
3060 mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass)
3062 return gclass->container_class->valuetype;
3066 _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2, gboolean signature_only)
3070 if ((g1->inst->type_argc != g2->inst->type_argc) || (g1->is_dynamic != g2->is_dynamic) ||
3071 (g1->inst->is_reference != g2->inst->is_reference))
3073 if (!mono_metadata_class_equal (g1->container_class, g2->container_class, signature_only))
3075 for (i = 0; i < g1->inst->type_argc; ++i) {
3076 if (!do_mono_metadata_type_equal (g1->inst->type_argv [i], g2->inst->type_argv [i], signature_only))
3083 mono_metadata_generic_method_hash (MonoGenericMethod *gmethod)
3085 return gmethod->inst->id;
3089 mono_metadata_generic_method_equal (MonoGenericMethod *g1, MonoGenericMethod *g2)
3091 return (g1->container == g2->container) && (g1->generic_class == g2->generic_class) &&
3092 (g1->inst == g2->inst);
3097 * mono_metadata_type_hash:
3100 * Computes an hash value for @t1 to be used in GHashTable.
3103 mono_metadata_type_hash (MonoType *t1)
3105 guint hash = t1->type;
3107 hash |= t1->byref << 6; /* do not collide with t1->type values */
3109 case MONO_TYPE_VALUETYPE:
3110 case MONO_TYPE_CLASS:
3111 case MONO_TYPE_SZARRAY:
3112 /* check if the distribution is good enough */
3113 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
3115 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
3116 case MONO_TYPE_ARRAY:
3117 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
3118 case MONO_TYPE_GENERICINST:
3119 return ((hash << 5) - hash) ^ mono_generic_class_hash (t1->data.generic_class);
3125 mono_metadata_generic_param_equal (MonoGenericParam *p1, MonoGenericParam *p2, gboolean signature_only)
3129 if (p1->num != p2->num)
3132 if (p1->owner == p2->owner)
3136 * If `signature_only' is true, we're comparing two (method) signatures.
3137 * In this case, the owner of two type parameters doesn't need to match.
3140 return signature_only;
3144 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only)
3148 if (c1->generic_class && c2->generic_class)
3149 return _mono_metadata_generic_class_equal (c1->generic_class, c2->generic_class, signature_only);
3150 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
3151 return mono_metadata_generic_param_equal (
3152 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
3153 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
3154 return mono_metadata_generic_param_equal (
3155 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
3156 if (signature_only &&
3157 (c1->byval_arg.type == MONO_TYPE_SZARRAY) && (c2->byval_arg.type == MONO_TYPE_SZARRAY))
3158 return mono_metadata_class_equal (c1->byval_arg.data.klass, c2->byval_arg.data.klass, signature_only);
3163 * mono_metadata_type_equal:
3167 * Determine if @t1 and @t2 represent the same type.
3168 * Returns: #TRUE if @t1 and @t2 are equal.
3171 do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only)
3173 if (t1->type != t2->type || t1->byref != t2->byref)
3177 case MONO_TYPE_VOID:
3178 case MONO_TYPE_BOOLEAN:
3179 case MONO_TYPE_CHAR:
3190 case MONO_TYPE_STRING:
3193 case MONO_TYPE_OBJECT:
3194 case MONO_TYPE_TYPEDBYREF:
3196 case MONO_TYPE_VALUETYPE:
3197 case MONO_TYPE_CLASS:
3198 case MONO_TYPE_SZARRAY:
3199 return mono_metadata_class_equal (t1->data.klass, t2->data.klass, signature_only);
3201 return do_mono_metadata_type_equal (t1->data.type, t2->data.type, signature_only);
3202 case MONO_TYPE_ARRAY:
3203 if (t1->data.array->rank != t2->data.array->rank)
3205 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass, signature_only);
3206 case MONO_TYPE_GENERICINST:
3207 return _mono_metadata_generic_class_equal (
3208 t1->data.generic_class, t2->data.generic_class, signature_only);
3210 return mono_metadata_generic_param_equal (
3211 t1->data.generic_param, t2->data.generic_param, signature_only);
3212 case MONO_TYPE_MVAR:
3213 return mono_metadata_generic_param_equal (
3214 t1->data.generic_param, t2->data.generic_param, signature_only);
3216 g_error ("implement type compare for %0x!", t1->type);
3224 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
3226 return do_mono_metadata_type_equal (t1, t2, FALSE);
3230 * mono_metadata_signature_equal:
3231 * @sig1: a signature
3232 * @sig2: another signature
3234 * Determine if @sig1 and @sig2 represent the same signature, with the
3235 * same number of arguments and the same types.
3236 * Returns: #TRUE if @sig1 and @sig2 are equal.
3239 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
3243 if (sig1->hasthis != sig2->hasthis || sig1->param_count != sig2->param_count)
3247 * We're just comparing the signatures of two methods here:
3249 * If we have two generic methods `void Foo<U> (U u)' and `void Bar<V> (V v)',
3250 * U and V are equal here.
3252 * That's what the `signature_only' argument of do_mono_metadata_type_equal() is for.
3255 for (i = 0; i < sig1->param_count; i++) {
3256 MonoType *p1 = sig1->params[i];
3257 MonoType *p2 = sig2->params[i];
3259 /* if (p1->attrs != p2->attrs)
3262 if (!do_mono_metadata_type_equal (p1, p2, TRUE))
3266 if (!do_mono_metadata_type_equal (sig1->ret, sig2->ret, TRUE))
3272 mono_signature_hash (MonoMethodSignature *sig)
3274 guint i, res = sig->ret->type;
3276 for (i = 0; i < sig->param_count; i++)
3277 res = (res << 5) - res + mono_type_hash (sig->params[i]);
3283 * mono_metadata_encode_value:
3284 * @value: value to encode
3285 * @buf: buffer where to write the compressed representation
3286 * @endbuf: pointer updated to point at the end of the encoded output
3288 * Encodes the value @value in the compressed representation used
3289 * in metadata and stores the result in @buf. @buf needs to be big
3290 * enough to hold the data (4 bytes).
3293 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
3299 else if (value < 0x4000) {
3300 p [0] = 0x80 | (value >> 8);
3301 p [1] = value & 0xff;
3304 p [0] = (value >> 24) | 0xc0;
3305 p [1] = (value >> 16) & 0xff;
3306 p [2] = (value >> 8) & 0xff;
3307 p [3] = value & 0xff;
3315 * mono_metadata_field_info:
3316 * @meta: the Image the field is defined in
3317 * @index: the index in the field table representing the field
3318 * @offset: a pointer to an integer where to store the offset that
3319 * may have been specified for the field in a FieldLayout table
3320 * @rva: a pointer to the RVA of the field data in the image that
3321 * may have been defined in a FieldRVA table
3322 * @marshal_spec: a pointer to the marshal spec that may have been
3323 * defined for the field in a FieldMarshal table.
3325 * Gather info for field @index that may have been defined in the FieldLayout,
3326 * FieldRVA and FieldMarshal tables.
3327 * Either of offset, rva and marshal_spec can be NULL if you're not interested
3331 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
3332 MonoMarshalSpec **marshal_spec)
3334 MonoTableInfo *tdef;
3337 loc.idx = index + 1;
3339 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
3341 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
3344 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3345 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
3347 *offset = (guint32)-1;
3351 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
3353 loc.col_idx = MONO_FIELD_RVA_FIELD;
3356 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3358 * LAMESPEC: There is no signature, no nothing, just the raw data.
3360 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
3368 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
3369 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
3376 * mono_metadata_get_constant_index:
3377 * @meta: the Image the field is defined in
3378 * @index: the token that may have a row defined in the constants table
3379 * @hint: possible position for the row
3381 * @token must be a FieldDef, ParamDef or PropertyDef token.
3383 * Returns: the index into the Constants table or 0 if not found.
3386 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
3388 MonoTableInfo *tdef;
3390 guint32 index = mono_metadata_token_index (token);
3392 tdef = &meta->tables [MONO_TABLE_CONSTANT];
3393 index <<= MONO_HASCONSTANT_BITS;
3394 switch (mono_metadata_token_table (token)) {
3395 case MONO_TABLE_FIELD:
3396 index |= MONO_HASCONSTANT_FIEDDEF;
3398 case MONO_TABLE_PARAM:
3399 index |= MONO_HASCONSTANT_PARAM;
3401 case MONO_TABLE_PROPERTY:
3402 index |= MONO_HASCONSTANT_PROPERTY;
3405 g_warning ("Not a valid token for the constant table: 0x%08x", token);
3409 loc.col_idx = MONO_CONSTANT_PARENT;
3412 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
3415 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3416 return loc.result + 1;
3422 * mono_metadata_events_from_typedef:
3423 * @meta: metadata context
3424 * @index: 0-based index (in the TypeDef table) describing a type
3426 * Returns: the 0-based index in the Event table for the events in the
3427 * type. The last event that belongs to the type (plus 1) is stored
3428 * in the @end_idx pointer.
3431 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3435 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
3443 loc.col_idx = MONO_EVENT_MAP_PARENT;
3444 loc.idx = index + 1;
3446 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3449 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
3450 if (loc.result + 1 < tdef->rows) {
3451 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
3453 end = meta->tables [MONO_TABLE_EVENT].rows;
3461 * mono_metadata_methods_from_event:
3462 * @meta: metadata context
3463 * @index: 0-based index (in the Event table) describing a event
3465 * Returns: the 0-based index in the MethodDef table for the methods in the
3466 * event. The last method that belongs to the event (plus 1) is stored
3467 * in the @end_idx pointer.
3470 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
3474 guint32 cols [MONO_METHOD_SEMA_SIZE];
3475 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3482 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3483 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
3485 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3490 * We may end up in the middle of the rows...
3493 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3499 while (end < msemt->rows) {
3500 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3501 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3510 * mono_metadata_properties_from_typedef:
3511 * @meta: metadata context
3512 * @index: 0-based index (in the TypeDef table) describing a type
3514 * Returns: the 0-based index in the Property table for the properties in the
3515 * type. The last property that belongs to the type (plus 1) is stored
3516 * in the @end_idx pointer.
3519 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3523 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
3531 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
3532 loc.idx = index + 1;
3534 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3537 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
3538 if (loc.result + 1 < tdef->rows) {
3539 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
3541 end = meta->tables [MONO_TABLE_PROPERTY].rows;
3549 * mono_metadata_methods_from_property:
3550 * @meta: metadata context
3551 * @index: 0-based index (in the PropertyDef table) describing a property
3553 * Returns: the 0-based index in the MethodDef table for the methods in the
3554 * property. The last method that belongs to the property (plus 1) is stored
3555 * in the @end_idx pointer.
3558 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
3562 guint32 cols [MONO_METHOD_SEMA_SIZE];
3563 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3570 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3571 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
3573 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3578 * We may end up in the middle of the rows...
3581 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3587 while (end < msemt->rows) {
3588 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3589 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3598 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
3601 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
3607 loc.col_idx = MONO_IMPLMAP_MEMBER;
3608 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
3610 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3613 return loc.result + 1;
3617 * @ptr: MonoType to unwrap
3619 * Recurses in array/szarray to get the element type.
3620 * Returns the element MonoType or the @ptr itself if its not an array/szarray.
3623 unwrap_arrays (MonoType *ptr)
3626 if (ptr->type == MONO_TYPE_ARRAY)
3627 ptr = &ptr->data.array->eklass->byval_arg;
3628 else if (ptr->type == MONO_TYPE_SZARRAY)
3629 ptr = &ptr->data.klass->byval_arg;
3636 * @inst: MonoGenericClass to search in
3637 * @prefer_mvar: Whether to try harder to find an MVAR
3639 * Return some occurance of a generic parameter in this instantiation, if any. Return NULL otherwise.
3640 * If @prefer_mvar is set, it tries to find if there's an MVAR generic parameter, and returns that
3641 * in preference to any VAR generic parameter.
3644 find_generic_param (MonoGenericClass *gclass, gboolean prefer_mvar)
3646 int i = 0, count = gclass->inst->type_argc;
3647 MonoType **ptr = gclass->inst->type_argv;
3648 MonoType *fallback = NULL;
3650 g_assert (gclass->inst->is_open);
3652 for (i = 0; i < count; i++) {
3653 MonoType *ctype = unwrap_arrays (ptr [i]);
3655 if (ctype->type == MONO_TYPE_GENERICINST && ctype->data.generic_class->inst->is_open)
3656 ctype = find_generic_param (ctype->data.generic_class, prefer_mvar);
3658 if (ctype->type == MONO_TYPE_MVAR)
3661 if (ctype->type == MONO_TYPE_VAR) {
3673 * @image: context where the image is created
3674 * @type_spec: typespec token
3676 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
3680 mono_type_create_from_typespec_full (MonoImage *image, MonoGenericContext *generic_context, guint32 type_spec)
3682 guint32 idx = mono_metadata_token_index (type_spec);
3684 guint32 cols [MONO_TYPESPEC_SIZE];
3688 gboolean cache_type = TRUE;
3689 MonoGenericContainer *gc = generic_context ? generic_context->container : NULL;
3691 mono_loader_lock ();
3693 type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec));
3695 if (type && type->type == MONO_TYPE_GENERICINST && type->data.generic_class->inst->is_open) {
3696 MonoType *gtype = find_generic_param (type->data.generic_class, gc ? gc->is_method : FALSE);
3697 gc = select_container (gc, gtype->type);
3698 if (gtype->data.generic_param->owner != gc)
3702 if (type && (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR)) {
3703 gc = select_container (gc, type->type);
3705 /* Use the one already cached in the container, if it exists. Otherwise, ensure that it's created */
3706 type = gc->types ? gc->types [type->data.generic_param->num] : NULL;
3708 /* Either way, some other variant of this generic-parameter is already in the typespec cache. */
3710 } else if (type->data.generic_param->owner) {
3712 * We need a null-owner generic-parameter, but the one in the cache has an owner.
3713 * Ensure that the null-owner generic-parameter goes into the cache.
3715 * Together with the 'cache_type = FALSE;' line in the other arm of this condition,
3716 * this ensures that a null-owner generic-parameter, once created, doesn't need to be re-created.
3717 * The generic-parameters with owners are cached in their respective owners, and thus don't
3718 * need to be re-created either.
3725 mono_loader_unlock ();
3729 t = &image->tables [MONO_TABLE_TYPESPEC];
3731 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
3732 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
3733 len = mono_metadata_decode_value (ptr, &ptr);
3735 type = g_new0 (MonoType, 1);
3738 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
3740 if (*ptr == MONO_TYPE_BYREF) {
3745 do_mono_metadata_parse_type (type, image, generic_context, ptr, &ptr);
3747 if ((type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR) && type->data.generic_param->owner) {
3748 MonoGenericContainer *container = type->data.generic_param->owner;
3750 if (!container->types)
3751 container->types = g_new0 (MonoType*, container->type_argc);
3753 container->types [type->data.generic_param->num] = type;
3756 mono_loader_unlock ();
3762 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
3764 return mono_type_create_from_typespec_full (image, NULL, type_spec);
3768 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
3770 MonoMarshalSpec *res;
3772 const char *start = ptr;
3774 /* fixme: this is incomplete, but I cant find more infos in the specs */
3776 res = g_new0 (MonoMarshalSpec, 1);
3778 len = mono_metadata_decode_value (ptr, &ptr);
3779 res->native = *ptr++;
3781 if (res->native == MONO_NATIVE_LPARRAY) {
3782 res->data.array_data.param_num = -1;
3783 res->data.array_data.num_elem = -1;
3784 res->data.array_data.elem_mult = -1;
3786 if (ptr - start <= len)
3787 res->data.array_data.elem_type = *ptr++;
3788 if (ptr - start <= len)
3789 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
3790 if (ptr - start <= len)
3791 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3792 if (ptr - start <= len) {
3794 * LAMESPEC: Older spec versions say this parameter comes before
3795 * num_elem. Never spec versions don't talk about elem_mult at
3796 * all, but csc still emits it, and it is used to distinguish
3797 * between param_num being 0, and param_num being omitted.
3798 * So if (param_num == 0) && (num_elem > 0), then
3799 * elem_mult == 0 -> the array size is num_elem
3800 * elem_mult == 1 -> the array size is @param_num + num_elem
3802 res->data.array_data.elem_mult = mono_metadata_decode_value (ptr, &ptr);
3806 if (res->native == MONO_NATIVE_BYVALTSTR) {
3807 if (ptr - start <= len)
3808 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3811 if (res->native == MONO_NATIVE_BYVALARRAY) {
3812 if (ptr - start <= len)
3813 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3816 if (res->native == MONO_NATIVE_CUSTOM) {
3817 /* skip unused type guid */
3818 len = mono_metadata_decode_value (ptr, &ptr);
3820 /* skip unused native type name */
3821 len = mono_metadata_decode_value (ptr, &ptr);
3823 /* read custom marshaler type name */
3824 len = mono_metadata_decode_value (ptr, &ptr);
3825 res->data.custom_data.custom_name = g_strndup (ptr, len);
3827 /* read cookie string */
3828 len = mono_metadata_decode_value (ptr, &ptr);
3829 res->data.custom_data.cookie = g_strndup (ptr, len);
3832 if (res->native == MONO_NATIVE_SAFEARRAY) {
3833 res->data.safearray_data.elem_type = 0;
3834 res->data.safearray_data.num_elem = 0;
3835 if (ptr - start <= len)
3836 res->data.safearray_data.elem_type = *ptr++;
3837 if (ptr - start <= len)
3838 res->data.safearray_data.num_elem = *ptr++;
3844 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
3846 if (spec->native == MONO_NATIVE_CUSTOM) {
3847 g_free (spec->data.custom_data.custom_name);
3848 g_free (spec->data.custom_data.cookie);
3854 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
3855 gboolean unicode, MonoMarshalConv *conv)
3857 MonoMarshalConv dummy_conv;
3863 *conv = MONO_MARSHAL_CONV_NONE;
3866 return MONO_NATIVE_UINT;
3870 case MONO_TYPE_BOOLEAN:
3872 switch (mspec->native) {
3873 case MONO_NATIVE_VARIANTBOOL:
3874 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
3875 return MONO_NATIVE_VARIANTBOOL;
3876 case MONO_NATIVE_BOOLEAN:
3877 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3878 return MONO_NATIVE_BOOLEAN;
3879 case MONO_NATIVE_I1:
3880 case MONO_NATIVE_U1:
3881 return mspec->native;
3883 g_error ("cant marshal bool to native type %02x", mspec->native);
3886 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3887 return MONO_NATIVE_BOOLEAN;
3888 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
3889 case MONO_TYPE_I1: return MONO_NATIVE_I1;
3890 case MONO_TYPE_U1: return MONO_NATIVE_U1;
3891 case MONO_TYPE_I2: return MONO_NATIVE_I2;
3892 case MONO_TYPE_U2: return MONO_NATIVE_U2;
3893 case MONO_TYPE_I4: return MONO_NATIVE_I4;
3894 case MONO_TYPE_U4: return MONO_NATIVE_U4;
3895 case MONO_TYPE_I8: return MONO_NATIVE_I8;
3896 case MONO_TYPE_U8: return MONO_NATIVE_U8;
3897 case MONO_TYPE_R4: return MONO_NATIVE_R4;
3898 case MONO_TYPE_R8: return MONO_NATIVE_R8;
3899 case MONO_TYPE_STRING:
3901 switch (mspec->native) {
3902 case MONO_NATIVE_BSTR:
3903 *conv = MONO_MARSHAL_CONV_STR_BSTR;
3904 return MONO_NATIVE_BSTR;
3905 case MONO_NATIVE_LPSTR:
3906 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
3907 return MONO_NATIVE_LPSTR;
3908 case MONO_NATIVE_LPWSTR:
3909 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
3910 return MONO_NATIVE_LPWSTR;
3911 case MONO_NATIVE_LPTSTR:
3912 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3913 return MONO_NATIVE_LPTSTR;
3914 case MONO_NATIVE_ANSIBSTR:
3915 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
3916 return MONO_NATIVE_ANSIBSTR;
3917 case MONO_NATIVE_TBSTR:
3918 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
3919 return MONO_NATIVE_TBSTR;
3920 case MONO_NATIVE_BYVALTSTR:
3922 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
3924 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
3925 return MONO_NATIVE_BYVALTSTR;
3927 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);
3930 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3931 return MONO_NATIVE_LPTSTR;
3932 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
3933 case MONO_TYPE_VALUETYPE: /*FIXME*/
3934 if (type->data.klass->enumtype) {
3935 t = type->data.klass->enum_basetype->type;
3938 return MONO_NATIVE_STRUCT;
3939 case MONO_TYPE_SZARRAY:
3940 case MONO_TYPE_ARRAY:
3942 switch (mspec->native) {
3943 case MONO_NATIVE_BYVALARRAY:
3944 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
3945 return MONO_NATIVE_BYVALARRAY;
3946 case MONO_NATIVE_SAFEARRAY:
3947 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
3948 return MONO_NATIVE_SAFEARRAY;
3949 case MONO_NATIVE_LPARRAY:
3950 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3951 return MONO_NATIVE_LPARRAY;
3953 g_error ("cant marshal array as native type %02x", mspec->native);
3957 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3958 return MONO_NATIVE_LPARRAY;
3959 case MONO_TYPE_I: return MONO_NATIVE_INT;
3960 case MONO_TYPE_U: return MONO_NATIVE_UINT;
3961 case MONO_TYPE_CLASS:
3962 case MONO_TYPE_OBJECT: {
3963 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
3965 switch (mspec->native) {
3966 case MONO_NATIVE_STRUCT:
3967 return MONO_NATIVE_STRUCT;
3968 case MONO_NATIVE_INTERFACE:
3969 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
3970 return MONO_NATIVE_INTERFACE;
3971 case MONO_NATIVE_IDISPATCH:
3972 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
3973 return MONO_NATIVE_IDISPATCH;
3974 case MONO_NATIVE_IUNKNOWN:
3975 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
3976 return MONO_NATIVE_IUNKNOWN;
3977 case MONO_NATIVE_FUNC:
3978 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3979 type->data.klass == mono_defaults.delegate_class ||
3980 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3981 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3982 return MONO_NATIVE_FUNC;
3986 g_error ("cant marshal object as native type %02x", mspec->native);
3989 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3990 type->data.klass == mono_defaults.delegate_class ||
3991 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3992 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3993 return MONO_NATIVE_FUNC;
3995 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
3996 return MONO_NATIVE_STRUCT;
3998 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
3999 case MONO_TYPE_GENERICINST:
4000 type = &type->data.generic_class->container_class->byval_arg;
4003 case MONO_TYPE_TYPEDBYREF:
4005 g_error ("type 0x%02x not handled in marshal", t);
4007 return MONO_NATIVE_MAX;
4011 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
4014 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
4020 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
4021 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
4023 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4026 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
4030 method_from_method_def_or_ref (MonoImage *m, guint32 tok, MonoGenericContext *context)
4032 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
4033 switch (tok & MONO_METHODDEFORREF_MASK) {
4034 case MONO_METHODDEFORREF_METHODDEF:
4035 return mono_get_method_full (m, MONO_TOKEN_METHOD_DEF | idx, NULL, context);
4036 case MONO_METHODDEFORREF_METHODREF:
4037 return mono_get_method_full (m, MONO_TOKEN_MEMBER_REF | idx, NULL, context);
4039 g_assert_not_reached ();
4044 * mono_class_get_overrides_full:
4046 * Return the method overrides belonging to class @type_token in @overrides, and
4047 * the number of overrides in @num_overrides.
4049 * Returns: TRUE on success, FALSE on failure.
4052 mono_class_get_overrides_full (MonoImage *image, guint32 type_token, MonoMethod ***overrides, gint32 *num_overrides,
4053 MonoGenericContext *generic_context)
4056 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
4059 guint32 cols [MONO_METHODIMPL_SIZE];
4060 MonoMethod **result;
4070 loc.col_idx = MONO_METHODIMPL_CLASS;
4071 loc.idx = mono_metadata_token_index (type_token);
4073 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4079 * We may end up in the middle of the rows...
4082 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
4087 while (end < tdef->rows) {
4088 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
4094 result = g_new (MonoMethod*, num * 2);
4095 for (i = 0; i < num; ++i) {
4096 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
4097 result [i * 2] = method_from_method_def_or_ref (
4098 image, cols [MONO_METHODIMPL_DECLARATION], generic_context);
4099 result [i * 2 + 1] = method_from_method_def_or_ref (
4100 image, cols [MONO_METHODIMPL_BODY], generic_context);
4103 *overrides = result;
4105 *num_overrides = num;
4110 * mono_guid_to_string:
4112 * Converts a 16 byte Microsoft GUID to the standard string representation.
4115 mono_guid_to_string (const guint8 *guid)
4117 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
4118 guid[3], guid[2], guid[1], guid[0],
4122 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
4126 get_constraints (MonoImage *image, int owner, MonoClass ***constraints, MonoGenericContext *context)
4128 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
4129 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
4130 guint32 i, token, found;
4131 MonoClass *klass, **res;
4132 GList *cons = NULL, *tmp;
4134 *constraints = NULL;
4136 for (i = 0; i < tdef->rows; ++i) {
4137 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
4138 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
4139 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
4140 klass = mono_class_get_full (image, token, context);
4141 cons = g_list_append (cons, klass);
4144 /* contiguous list finished */
4151 res = g_new0 (MonoClass*, found + 1);
4152 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
4153 res [i] = tmp->data;
4161 * mono_metadata_get_generic_param_row:
4164 * @token: TypeOrMethodDef token, owner for GenericParam
4165 * @owner: coded token, set on return
4167 * Returns: 1-based row-id in the GenericParam table whose
4168 * owner is @token. 0 if not found.
4171 mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner)
4173 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
4174 guint32 cols [MONO_GENERICPARAM_SIZE];
4181 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
4182 *owner = MONO_TYPEORMETHOD_TYPE;
4183 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
4184 *owner = MONO_TYPEORMETHOD_METHOD;
4186 g_error ("wrong token %x to get_generic_param_row", token);
4189 *owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
4191 for (i = 0; i < tdef->rows; ++i) {
4192 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
4193 if (cols [MONO_GENERICPARAM_OWNER] == *owner)
4201 mono_metadata_has_generic_params (MonoImage *image, guint32 token)
4204 return mono_metadata_get_generic_param_row (image, token, &owner);
4208 * mono_metadata_load_generic_param_constraints:
4210 * Load the generic parameter constraints for the newly created generic type or method
4211 * represented by @token and @container. The @container is the new container which has
4212 * been returned by a call to mono_metadata_load_generic_params() with this @token.
4215 mono_metadata_load_generic_param_constraints (MonoImage *image, guint32 token,
4216 MonoGenericContainer *container)
4218 guint32 start_row, i, owner;
4219 if (! (start_row = mono_metadata_get_generic_param_row (image, token, &owner)))
4221 for (i = 0; i < container->type_argc; i++)
4222 get_constraints (image, start_row + i, &container->type_params [i].constraints,
4223 &container->context);
4227 * mono_metadata_load_generic_params:
4229 * Load the type parameters from the type or method definition @token.
4231 * Use this method after parsing a type or method definition to figure out whether it's a generic
4232 * type / method. When parsing a method definition, @parent_container points to the generic container
4233 * of the current class, if any.
4235 * Note: This method does not load the constraints: for typedefs, this has to be done after fully
4236 * creating the type.
4238 * Returns: NULL if @token is not a generic type or method definition or the new generic container.
4241 MonoGenericContainer *
4242 mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericContainer *parent_container)
4244 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
4245 guint32 cols [MONO_GENERICPARAM_SIZE];
4246 guint32 i, owner = 0, n;
4247 MonoGenericContainer *container;
4248 MonoGenericParam *params;
4250 GSList *dup_list = NULL, *l;
4251 GHashTable *table = NULL;
4253 if (!(i = mono_metadata_get_generic_param_row (image, token, &owner)))
4255 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
4258 table = g_hash_table_new (g_str_hash, g_str_equal);
4259 container = g_new0 (MonoGenericContainer, 1);
4262 params = g_realloc (params, sizeof (MonoGenericParam) * n);
4263 params [n - 1].owner = container;
4264 params [n - 1].pklass = NULL;
4265 params [n - 1].method = NULL;
4266 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
4267 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
4268 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
4269 if ((p = g_hash_table_lookup (table, params [n - 1].name)))
4270 dup_list = g_slist_prepend (g_slist_prepend (dup_list, GUINT_TO_POINTER (n)), p);
4272 g_hash_table_insert (table, params [n - 1].name, GUINT_TO_POINTER (n));
4273 params [n - 1].constraints = NULL;
4274 if (++i > tdef->rows)
4276 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
4277 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
4280 if (!mono_generic_params_with_ambiguous_names)
4281 mono_generic_params_with_ambiguous_names = g_hash_table_new (NULL, NULL);
4282 for (l = dup_list; l; l = l->next) {
4283 int param = GPOINTER_TO_UINT (l->data);
4284 g_hash_table_insert (mono_generic_params_with_ambiguous_names, ¶ms [param-1], ¶ms [param-1]);
4286 g_slist_free (dup_list);
4288 g_hash_table_destroy (table);
4291 container->type_argc = n;
4292 container->type_params = params;
4293 container->parent = parent_container;
4295 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
4296 container->is_method = 1;
4298 container->context.container = container;
4304 mono_type_is_byref (MonoType *type)
4310 mono_type_get_type (MonoType *type)
4315 /* For MONO_TYPE_FNPTR */
4316 MonoMethodSignature*
4317 mono_type_get_signature (MonoType *type)
4319 return type->data.method;
4322 /* For MONO_TYPE_CLASS, VALUETYPE */
4324 mono_type_get_class (MonoType *type)
4326 return type->data.klass;
4329 /* For MONO_TYPE_ARRAY */
4331 mono_type_get_array_type (MonoType *type)
4333 return type->data.array;
4337 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
4339 /* FIXME: implement */
4344 mono_signature_get_return_type (MonoMethodSignature *sig)
4350 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
4356 /* start from the first */
4357 if (sig->param_count) {
4358 *iter = &sig->params [0];
4359 return sig->params [0];
4367 if (type < &sig->params [sig->param_count]) {
4375 mono_signature_get_param_count (MonoMethodSignature *sig)
4377 return sig->param_count;
4381 mono_signature_get_call_conv (MonoMethodSignature *sig)
4383 return sig->call_convention;
4387 mono_signature_vararg_start (MonoMethodSignature *sig)
4389 return sig->sentinelpos;
4393 mono_signature_is_instance (MonoMethodSignature *sig)
4395 return sig->hasthis;
4399 mono_signature_explicit_this (MonoMethodSignature *sig)
4401 return sig->explicit_this;
4404 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
4406 mono_aligned_addr_hash (gconstpointer ptr)
4408 return GPOINTER_TO_UINT (ptr) >> 3;