2 * metadata.c: Routines for accessing the metadata
5 * Miguel de Icaza (miguel@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * (C) 2001-2002 Ximian, Inc.
17 #include "tabledefs.h"
18 #include "mono-endian.h"
20 #include "tokentype.h"
21 #include "metadata-internals.h"
22 #include "class-internals.h"
25 static void do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContext *generic_context,
26 const char *ptr, const char **rptr);
28 static gboolean do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only);
29 static gboolean mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only);
30 static gboolean _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2,
31 gboolean signature_only);
34 * This enumeration is used to describe the data types in the metadata
45 /* Index into Blob heap */
48 /* Index into String heap */
54 /* Pointer into a table */
57 /* HasConstant:Parent pointer (Param, Field or Property) */
60 /* HasCustomAttribute index. Indexes any table except CustomAttribute */
63 /* CustomAttributeType encoded index */
66 /* HasDeclSecurity index: TypeDef Method or Assembly */
69 /* Implementation coded index: File, Export AssemblyRef */
72 /* HasFieldMarshal coded index: Field or Param table */
75 /* MemberForwardedIndex: Field or Method */
78 /* TypeDefOrRef coded index: typedef, typeref, typespec */
81 /* MemberRefParent coded index: typeref, moduleref, method, memberref, typesepc, typedef */
84 /* MethodDefOrRef coded index: Method or Member Ref table */
87 /* HasSemantic coded index: Event or Property */
90 /* ResolutionScope coded index: Module, ModuleRef, AssemblytRef, TypeRef */
94 const static unsigned char AssemblySchema [] = {
95 MONO_MT_UINT32, /* "HashId" }, */
96 MONO_MT_UINT16, /* "Major" }, */
97 MONO_MT_UINT16, /* "Minor" }, */
98 MONO_MT_UINT16, /* "BuildNumber" }, */
99 MONO_MT_UINT16, /* "RevisionNumber" }, */
100 MONO_MT_UINT32, /* "Flags" }, */
101 MONO_MT_BLOB_IDX, /* "PublicKey" }, */
102 MONO_MT_STRING_IDX, /* "Name" }, */
103 MONO_MT_STRING_IDX, /* "Culture" }, */
107 const static unsigned char AssemblyOSSchema [] = {
108 MONO_MT_UINT32, /* "OSPlatformID" }, */
109 MONO_MT_UINT32, /* "OSMajor" }, */
110 MONO_MT_UINT32, /* "OSMinor" }, */
114 const static unsigned char AssemblyProcessorSchema [] = {
115 MONO_MT_UINT32, /* "Processor" }, */
119 const static unsigned char AssemblyRefSchema [] = {
120 MONO_MT_UINT16, /* "Major" }, */
121 MONO_MT_UINT16, /* "Minor" }, */
122 MONO_MT_UINT16, /* "Build" }, */
123 MONO_MT_UINT16, /* "Revision" }, */
124 MONO_MT_UINT32, /* "Flags" }, */
125 MONO_MT_BLOB_IDX, /* "PublicKeyOrToken" }, */
126 MONO_MT_STRING_IDX, /* "Name" }, */
127 MONO_MT_STRING_IDX, /* "Culture" }, */
128 MONO_MT_BLOB_IDX, /* "HashValue" }, */
132 const static unsigned char AssemblyRefOSSchema [] = {
133 MONO_MT_UINT32, /* "OSPlatformID" }, */
134 MONO_MT_UINT32, /* "OSMajorVersion" }, */
135 MONO_MT_UINT32, /* "OSMinorVersion" }, */
136 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
140 const static unsigned char AssemblyRefProcessorSchema [] = {
141 MONO_MT_UINT32, /* "Processor" }, */
142 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
146 const static unsigned char ClassLayoutSchema [] = {
147 MONO_MT_UINT16, /* "PackingSize" }, */
148 MONO_MT_UINT32, /* "ClassSize" }, */
149 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
153 const static unsigned char ConstantSchema [] = {
154 MONO_MT_UINT8, /* "Type" }, */
155 MONO_MT_UINT8, /* "PaddingZero" }, */
156 MONO_MT_CONST_IDX, /* "Parent" }, */
157 MONO_MT_BLOB_IDX, /* "Value" }, */
161 const static unsigned char CustomAttributeSchema [] = {
162 MONO_MT_HASCAT_IDX, /* "Parent" }, */
163 MONO_MT_CAT_IDX, /* "Type" }, */
164 MONO_MT_BLOB_IDX, /* "Value" }, */
168 const static unsigned char DeclSecuritySchema [] = {
169 MONO_MT_UINT16, /* "Action" }, */
170 MONO_MT_HASDEC_IDX, /* "Parent" }, */
171 MONO_MT_BLOB_IDX, /* "PermissionSet" }, */
175 const static unsigned char EventMapSchema [] = {
176 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
177 MONO_MT_TABLE_IDX, /* "EventList:Event" }, */
181 const static unsigned char EventSchema [] = {
182 MONO_MT_UINT16, /* "EventFlags#EventAttribute" }, */
183 MONO_MT_STRING_IDX, /* "Name" }, */
184 MONO_MT_TABLE_IDX, /* "EventType" }, TypeDef or TypeRef */
188 const static unsigned char ExportedTypeSchema [] = {
189 MONO_MT_UINT32, /* "Flags" }, */
190 MONO_MT_TABLE_IDX, /* "TypeDefId" }, */
191 MONO_MT_STRING_IDX, /* "TypeName" }, */
192 MONO_MT_STRING_IDX, /* "TypeNameSpace" }, */
193 MONO_MT_IMPL_IDX, /* "Implementation" }, */
197 const static unsigned char FieldSchema [] = {
198 MONO_MT_UINT16, /* "Flags" }, */
199 MONO_MT_STRING_IDX, /* "Name" }, */
200 MONO_MT_BLOB_IDX, /* "Signature" }, */
203 const static unsigned char FieldLayoutSchema [] = {
204 MONO_MT_UINT32, /* "Offset" }, */
205 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
209 const static unsigned char FieldMarshalSchema [] = {
210 MONO_MT_HFM_IDX, /* "Parent" }, */
211 MONO_MT_BLOB_IDX, /* "NativeType" }, */
214 const static unsigned char FieldRVASchema [] = {
215 MONO_MT_UINT32, /* "RVA" }, */
216 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
220 const static unsigned char FileSchema [] = {
221 MONO_MT_UINT32, /* "Flags" }, */
222 MONO_MT_STRING_IDX, /* "Name" }, */
223 MONO_MT_BLOB_IDX, /* "Value" }, */
227 const static unsigned char ImplMapSchema [] = {
228 MONO_MT_UINT16, /* "MappingFlag" }, */
229 MONO_MT_MF_IDX, /* "MemberForwarded" }, */
230 MONO_MT_STRING_IDX, /* "ImportName" }, */
231 MONO_MT_TABLE_IDX, /* "ImportScope:ModuleRef" }, */
235 const static unsigned char InterfaceImplSchema [] = {
236 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
237 MONO_MT_TDOR_IDX, /* "Interface=TypeDefOrRef" }, */
241 const static unsigned char ManifestResourceSchema [] = {
242 MONO_MT_UINT32, /* "Offset" }, */
243 MONO_MT_UINT32, /* "Flags" }, */
244 MONO_MT_STRING_IDX, /* "Name" }, */
245 MONO_MT_IMPL_IDX, /* "Implementation" }, */
249 const static unsigned char MemberRefSchema [] = {
250 MONO_MT_MRP_IDX, /* "Class" }, */
251 MONO_MT_STRING_IDX, /* "Name" }, */
252 MONO_MT_BLOB_IDX, /* "Signature" }, */
256 const static unsigned char MethodSchema [] = {
257 MONO_MT_UINT32, /* "RVA" }, */
258 MONO_MT_UINT16, /* "ImplFlags#MethodImplAttributes" }, */
259 MONO_MT_UINT16, /* "Flags#MethodAttribute" }, */
260 MONO_MT_STRING_IDX, /* "Name" }, */
261 MONO_MT_BLOB_IDX, /* "Signature" }, */
262 MONO_MT_TABLE_IDX, /* "ParamList:Param" }, */
266 const static unsigned char MethodImplSchema [] = {
267 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
268 MONO_MT_MDOR_IDX, /* "MethodBody" }, */
269 MONO_MT_MDOR_IDX, /* "MethodDeclaration" }, */
273 const static unsigned char MethodSemanticsSchema [] = {
274 MONO_MT_UINT16, /* "MethodSemantic" }, */
275 MONO_MT_TABLE_IDX, /* "Method:Method" }, */
276 MONO_MT_HS_IDX, /* "Association" }, */
280 const static unsigned char ModuleSchema [] = {
281 MONO_MT_UINT16, /* "Generation" }, */
282 MONO_MT_STRING_IDX, /* "Name" }, */
283 MONO_MT_GUID_IDX, /* "MVID" }, */
284 MONO_MT_GUID_IDX, /* "EncID" }, */
285 MONO_MT_GUID_IDX, /* "EncBaseID" }, */
289 const static unsigned char ModuleRefSchema [] = {
290 MONO_MT_STRING_IDX, /* "Name" }, */
294 const static unsigned char NestedClassSchema [] = {
295 MONO_MT_TABLE_IDX, /* "NestedClass:TypeDef" }, */
296 MONO_MT_TABLE_IDX, /* "EnclosingClass:TypeDef" }, */
300 const static unsigned char ParamSchema [] = {
301 MONO_MT_UINT16, /* "Flags" }, */
302 MONO_MT_UINT16, /* "Sequence" }, */
303 MONO_MT_STRING_IDX, /* "Name" }, */
307 const static unsigned char PropertySchema [] = {
308 MONO_MT_UINT16, /* "Flags" }, */
309 MONO_MT_STRING_IDX, /* "Name" }, */
310 MONO_MT_BLOB_IDX, /* "Type" }, */
314 const static unsigned char PropertyMapSchema [] = {
315 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
316 MONO_MT_TABLE_IDX, /* "PropertyList:Property" }, */
320 const static unsigned char StandaloneSigSchema [] = {
321 MONO_MT_BLOB_IDX, /* "Signature" }, */
325 const static unsigned char TypeDefSchema [] = {
326 MONO_MT_UINT32, /* "Flags" }, */
327 MONO_MT_STRING_IDX, /* "Name" }, */
328 MONO_MT_STRING_IDX, /* "Namespace" }, */
329 MONO_MT_TDOR_IDX, /* "Extends" }, */
330 MONO_MT_TABLE_IDX, /* "FieldList:Field" }, */
331 MONO_MT_TABLE_IDX, /* "MethodList:Method" }, */
335 const static unsigned char TypeRefSchema [] = {
336 MONO_MT_RS_IDX, /* "ResolutionScope=ResolutionScope" }, */
337 MONO_MT_STRING_IDX, /* "Name" }, */
338 MONO_MT_STRING_IDX, /* "Namespace" }, */
342 const static unsigned char TypeSpecSchema [] = {
343 MONO_MT_BLOB_IDX, /* "Signature" }, */
347 const static unsigned char GenericParamSchema [] = {
348 MONO_MT_UINT16, /* "Number" }, */
349 MONO_MT_UINT16, /* "Flags" }, */
350 MONO_MT_TABLE_IDX, /* "Owner" }, TypeDef or MethodDef */
351 MONO_MT_STRING_IDX, /* "Name" }, */
356 const static unsigned char MethodSpecSchema [] = {
357 MONO_MT_MDOR_IDX, /* "Method" }, */
358 MONO_MT_BLOB_IDX, /* "Signature" }, */
362 const static unsigned char GenericParamConstraintSchema [] = {
363 MONO_MT_TABLE_IDX, /* "GenericParam" }, */
364 MONO_MT_TDOR_IDX, /* "Constraint" }, */
368 const static struct {
369 const unsigned char *description;
372 /* 0 */ { ModuleSchema, "Module" },
373 /* 1 */ { TypeRefSchema, "TypeRef" },
374 /* 2 */ { TypeDefSchema, "TypeDef" },
375 /* 3 */ { NULL, NULL },
376 /* 4 */ { FieldSchema, "Field" },
377 /* 5 */ { NULL, NULL },
378 /* 6 */ { MethodSchema, "Method" },
379 /* 7 */ { NULL, NULL },
380 /* 8 */ { ParamSchema, "Param" },
381 /* 9 */ { InterfaceImplSchema, "InterfaceImpl" },
382 /* A */ { MemberRefSchema, "MemberRef" },
383 /* B */ { ConstantSchema, "Constant" },
384 /* C */ { CustomAttributeSchema, "CustomAttribute" },
385 /* D */ { FieldMarshalSchema, "FieldMarshal" },
386 /* E */ { DeclSecuritySchema, "DeclSecurity" },
387 /* F */ { ClassLayoutSchema, "ClassLayout" },
388 /* 10 */ { FieldLayoutSchema, "FieldLayout" },
389 /* 11 */ { StandaloneSigSchema, "StandaloneSig" },
390 /* 12 */ { EventMapSchema, "EventMap" },
391 /* 13 */ { NULL, NULL },
392 /* 14 */ { EventSchema, "Event" },
393 /* 15 */ { PropertyMapSchema, "PropertyMap" },
394 /* 16 */ { NULL, NULL },
395 /* 17 */ { PropertySchema, "PropertyTable" },
396 /* 18 */ { MethodSemanticsSchema, "MethodSemantics" },
397 /* 19 */ { MethodImplSchema, "MethodImpl" },
398 /* 1A */ { ModuleRefSchema, "ModuleRef" },
399 /* 1B */ { TypeSpecSchema, "TypeSpec" },
400 /* 1C */ { ImplMapSchema, "ImplMap" },
401 /* 1D */ { FieldRVASchema, "FieldRVA" },
402 /* 1E */ { NULL, NULL },
403 /* 1F */ { NULL, NULL },
404 /* 20 */ { AssemblySchema, "Assembly" },
405 /* 21 */ { AssemblyProcessorSchema, "AssemblyProcessor" },
406 /* 22 */ { AssemblyOSSchema, "AssemblyOS" },
407 /* 23 */ { AssemblyRefSchema, "AssemblyRef" },
408 /* 24 */ { AssemblyRefProcessorSchema, "AssemblyRefProcessor" },
409 /* 25 */ { AssemblyRefOSSchema, "AssemblyRefOS" },
410 /* 26 */ { FileSchema, "File" },
411 /* 27 */ { ExportedTypeSchema, "ExportedType" },
412 /* 28 */ { ManifestResourceSchema, "ManifestResource" },
413 /* 29 */ { NestedClassSchema, "NestedClass" },
414 /* 2A */ { GenericParamSchema, "GenericParam" },
415 /* 2B */ { MethodSpecSchema, "MethodSpec" },
416 /* 2C */ { GenericParamConstraintSchema, "GenericParamConstraint" },
420 * mono_meta_table_name:
421 * @table: table index
423 * Returns: the name for the @table index
426 mono_meta_table_name (int table)
428 if ((table < 0) || (table > 0x2c))
431 return tables [table].name;
434 /* The guy who wrote the spec for this should not be allowed near a
437 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
438 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
439 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
440 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
441 inverse of this mapping.
444 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
445 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
447 /* Reference: Partition II - 23.2.6 */
449 * mono_metadata_compute_size:
450 * @meta: metadata context
451 * @tableindex: metadata table number
452 * @result_bitfield: pointer to guint32 where to store additional info
454 * mono_metadata_compute_size() computes the lenght in bytes of a single
455 * row in a metadata table. The size of each column is encoded in the
456 * @result_bitfield return value along with the number of columns in the table.
457 * the resulting bitfield should be handed to the mono_metadata_table_size()
458 * and mono_metadata_table_count() macros.
461 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
463 guint32 bitfield = 0;
464 int size = 0, field_size = 0;
467 const unsigned char *description = tables [tableindex].description;
469 for (i = 0; (code = description [i]) != MONO_MT_END; i++){
472 field_size = 4; break;
475 field_size = 2; break;
478 field_size = 1; break;
480 case MONO_MT_BLOB_IDX:
481 field_size = meta->idx_blob_wide ? 4 : 2; break;
483 case MONO_MT_STRING_IDX:
484 field_size = meta->idx_string_wide ? 4 : 2; break;
486 case MONO_MT_GUID_IDX:
487 field_size = meta->idx_guid_wide ? 4 : 2; break;
489 case MONO_MT_TABLE_IDX:
490 /* Uhm, a table index can point to other tables besides the current one
491 * so, it's not correct to use the rowcount of the current table to
492 * get the size for this column - lupus
494 switch (tableindex) {
495 case MONO_TABLE_ASSEMBLYREFOS:
497 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
498 case MONO_TABLE_ASSEMBLYPROCESSOR:
500 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
501 case MONO_TABLE_CLASSLAYOUT:
503 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
504 case MONO_TABLE_EVENTMAP:
505 g_assert (i == 0 || i == 1);
506 field_size = i ? idx_size (MONO_TABLE_EVENT):
507 idx_size(MONO_TABLE_TYPEDEF);
509 case MONO_TABLE_EVENT:
511 field_size = MAX (idx_size (MONO_TABLE_TYPEDEF), idx_size(MONO_TABLE_TYPEREF));
512 field_size = MAX (field_size, idx_size(MONO_TABLE_TYPESPEC));
514 case MONO_TABLE_EXPORTEDTYPE:
516 /* the index is in another metadata file, so it must be 4 */
517 field_size = 4; break;
518 case MONO_TABLE_FIELDLAYOUT:
520 field_size = idx_size (MONO_TABLE_FIELD); break;
521 case MONO_TABLE_FIELDRVA:
523 field_size = idx_size (MONO_TABLE_FIELD); break;
524 case MONO_TABLE_IMPLMAP:
526 field_size = idx_size (MONO_TABLE_MODULEREF); break;
527 case MONO_TABLE_INTERFACEIMPL:
529 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
530 case MONO_TABLE_METHOD:
532 field_size = idx_size (MONO_TABLE_PARAM); break;
533 case MONO_TABLE_METHODIMPL:
535 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
536 case MONO_TABLE_METHODSEMANTICS:
538 field_size = idx_size (MONO_TABLE_METHOD); break;
539 case MONO_TABLE_NESTEDCLASS:
540 g_assert (i == 0 || i == 1);
541 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
542 case MONO_TABLE_PROPERTYMAP:
543 g_assert (i == 0 || i == 1);
544 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
545 idx_size(MONO_TABLE_TYPEDEF);
547 case MONO_TABLE_TYPEDEF:
548 g_assert (i == 4 || i == 5);
549 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
550 idx_size(MONO_TABLE_METHOD);
552 case MONO_TABLE_GENERICPARAM:
553 g_assert (i == 2 || i == 4 || i == 5);
555 field_size = MAX (idx_size (MONO_TABLE_METHOD), idx_size (MONO_TABLE_TYPEDEF));
557 field_size = idx_size (MONO_TABLE_TYPEDEF);
559 field_size = idx_size (MONO_TABLE_TYPEDEF);
562 case MONO_TABLE_GENERICPARAMCONSTRAINT:
564 field_size = idx_size (MONO_TABLE_GENERICPARAM);
568 g_assert_not_reached ();
573 * HasConstant: ParamDef, FieldDef, Property
575 case MONO_MT_CONST_IDX:
576 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
577 meta->tables [MONO_TABLE_FIELD].rows);
578 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
580 /* 2 bits to encode tag */
581 field_size = rtsize (n, 16-2);
585 * HasCustomAttribute: points to any table but
588 case MONO_MT_HASCAT_IDX:
590 * We believe that since the signature and
591 * permission are indexing the Blob heap,
592 * we should consider the blob size first
594 /* I'm not a believer - lupus
595 if (meta->idx_blob_wide){
600 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
601 meta->tables [MONO_TABLE_FIELD].rows);
602 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
603 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
604 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
605 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
606 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
607 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
608 n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
609 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
610 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
611 n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
612 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
613 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
614 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
615 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
616 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
617 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
618 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
620 /* 5 bits to encode */
621 field_size = rtsize (n, 16-5);
625 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
626 * MemberRef and String.
628 case MONO_MT_CAT_IDX:
629 /* String is a heap, if it is wide, we know the size */
631 if (meta->idx_string_wide){
636 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
637 meta->tables [MONO_TABLE_TYPEDEF].rows);
638 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
639 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
641 /* 3 bits to encode */
642 field_size = rtsize (n, 16-3);
646 * HasDeclSecurity: Typedef, MethodDef, Assembly
648 case MONO_MT_HASDEC_IDX:
649 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
650 meta->tables [MONO_TABLE_METHOD].rows);
651 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
653 /* 2 bits to encode */
654 field_size = rtsize (n, 16-2);
658 * Implementation: File, AssemblyRef, ExportedType
660 case MONO_MT_IMPL_IDX:
661 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
662 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
663 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
665 /* 2 bits to encode tag */
666 field_size = rtsize (n, 16-2);
670 * HasFieldMarshall: FieldDef, ParamDef
672 case MONO_MT_HFM_IDX:
673 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
674 meta->tables [MONO_TABLE_PARAM].rows);
676 /* 1 bit used to encode tag */
677 field_size = rtsize (n, 16-1);
681 * MemberForwarded: FieldDef, MethodDef
684 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
685 meta->tables [MONO_TABLE_METHOD].rows);
687 /* 1 bit used to encode tag */
688 field_size = rtsize (n, 16-1);
692 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
694 * It is TypeDef, _TypeRef_, TypeSpec, instead.
696 case MONO_MT_TDOR_IDX:
697 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
698 meta->tables [MONO_TABLE_TYPEREF].rows);
699 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
701 /* 2 bits to encode */
702 field_size = rtsize (n, 16-2);
706 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
708 case MONO_MT_MRP_IDX:
709 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
710 meta->tables [MONO_TABLE_TYPEREF].rows);
711 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
712 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
713 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
714 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
716 /* 3 bits to encode */
717 field_size = rtsize (n, 16 - 3);
721 * MethodDefOrRef: MethodDef, MemberRef
723 case MONO_MT_MDOR_IDX:
724 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
725 meta->tables [MONO_TABLE_MEMBERREF].rows);
727 /* 1 bit used to encode tag */
728 field_size = rtsize (n, 16-1);
732 * HasSemantics: Property, Event
735 n = MAX (meta->tables [MONO_TABLE_PROPERTY].rows,
736 meta->tables [MONO_TABLE_EVENT].rows);
738 /* 1 bit used to encode tag */
739 field_size = rtsize (n, 16-1);
743 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
746 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
747 meta->tables [MONO_TABLE_MODULEREF].rows);
748 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
749 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
751 /* 2 bits used to encode tag (ECMA spec claims 3) */
752 field_size = rtsize (n, 16 - 2);
757 * encode field size as follows (we just need to
764 bitfield |= (field_size-1) << shift;
767 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
770 *result_bitfield = (i << 24) | bitfield;
775 * mono_metadata_compute_table_bases:
776 * @meta: metadata context to compute table values
778 * Computes the table bases for the metadata structure.
779 * This is an internal function used by the image loader code.
782 mono_metadata_compute_table_bases (MonoImage *meta)
785 const char *base = meta->tables_base;
787 for (i = 0; i < MONO_TABLE_NUM; i++){
788 if (meta->tables [i].rows == 0)
791 meta->tables [i].row_size = mono_metadata_compute_size (
792 meta, i, &meta->tables [i].size_bitfield);
793 meta->tables [i].base = base;
794 base += meta->tables [i].rows * meta->tables [i].row_size;
799 * mono_metadata_locate:
800 * @meta: metadata context
801 * @table: table code.
802 * @idx: index of element to retrieve from @table.
804 * Returns: a pointer to the @idx element in the metadata table
805 * whose code is @table.
808 mono_metadata_locate (MonoImage *meta, int table, int idx)
810 /* idx == 0 refers always to NULL */
811 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
813 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
817 * mono_metadata_locate_token:
818 * @meta: metadata context
819 * @token: metadata token
821 * Returns: a pointer to the data in the metadata represented by the
825 mono_metadata_locate_token (MonoImage *meta, guint32 token)
827 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
831 * mono_metadata_string_heap:
832 * @meta: metadata context
833 * @index: index into the string heap.
835 * Returns: an in-memory pointer to the @index in the string heap.
838 mono_metadata_string_heap (MonoImage *meta, guint32 index)
840 g_return_val_if_fail (index < meta->heap_strings.size, "");
841 return meta->heap_strings.data + index;
845 * mono_metadata_user_string:
846 * @meta: metadata context
847 * @index: index into the user string heap.
849 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
852 mono_metadata_user_string (MonoImage *meta, guint32 index)
854 g_return_val_if_fail (index < meta->heap_us.size, "");
855 return meta->heap_us.data + index;
859 * mono_metadata_blob_heap:
860 * @meta: metadata context
861 * @index: index into the blob.
863 * Returns: an in-memory pointer to the @index in the Blob heap.
866 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
868 g_return_val_if_fail (index < meta->heap_blob.size, "");
869 return meta->heap_blob.data + index;
873 * mono_metadata_guid_heap:
874 * @meta: metadata context
875 * @index: index into the guid heap.
877 * Returns: an in-memory pointer to the @index in the guid heap.
880 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
883 index *= 16; /* adjust for guid size and 1-based index */
884 g_return_val_if_fail (index < meta->heap_guid.size, "");
885 return meta->heap_guid.data + index;
889 dword_align (const char *ptr)
891 #if SIZEOF_VOID_P == 8
892 return (const char *) (((guint64) (ptr + 3)) & ~3);
894 return (const char *) (((guint32) (ptr + 3)) & ~3);
899 * mono_metadata_decode_row:
900 * @t: table to extract information from.
901 * @idx: index in table.
902 * @res: array of @res_size cols to store the results in
904 * This decompresses the metadata element @idx in table @t
905 * into the guint32 @res array that has res_size elements
908 mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res_size)
910 guint32 bitfield = t->size_bitfield;
911 int i, count = mono_metadata_table_count (bitfield);
912 const char *data = t->base + idx * t->row_size;
914 g_assert (res_size == count);
916 for (i = 0; i < count; i++){
917 int n = mono_metadata_table_size (bitfield, i);
921 res [i] = *data; break;
923 res [i] = read16 (data); break;
926 res [i] = read32 (data); break;
929 g_assert_not_reached ();
936 * mono_metadata_decode_row_col:
937 * @t: table to extract information from.
938 * @idx: index for row in table.
939 * @col: column in the row.
941 * This function returns the value of column @col from the @idx
942 * row in the table @t.
945 mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col)
947 guint32 bitfield = t->size_bitfield;
949 register const char *data = t->base + idx * t->row_size;
952 g_assert (col < mono_metadata_table_count (bitfield));
954 n = mono_metadata_table_size (bitfield, 0);
955 for (i = 0; i < col; ++i) {
957 n = mono_metadata_table_size (bitfield, i + 1);
963 return read16 (data);
965 return read32 (data);
967 g_assert_not_reached ();
972 * mono_metadata_decode_blob_size:
973 * @ptr: pointer to a blob object
974 * @rptr: the new position of the pointer
976 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
978 * Returns: the size of the blob object
981 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
983 const unsigned char *ptr = (const unsigned char *)xptr;
986 if ((*ptr & 0x80) == 0){
987 size = ptr [0] & 0x7f;
989 } else if ((*ptr & 0x40) == 0){
990 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
993 size = ((ptr [0] & 0x1f) << 24) +
1006 * mono_metadata_decode_value:
1007 * @ptr: pointer to decode from
1008 * @rptr: the new position of the pointer
1010 * This routine decompresses 32-bit values as specified in the "Blob and
1011 * Signature" section (22.2)
1013 * Returns: the decoded value
1016 mono_metadata_decode_value (const char *_ptr, const char **rptr)
1018 const unsigned char *ptr = (const unsigned char *) _ptr;
1019 unsigned char b = *ptr;
1022 if ((b & 0x80) == 0){
1025 } else if ((b & 0x40) == 0){
1026 len = ((b & 0x3f) << 8 | ptr [1]);
1029 len = ((b & 0x1f) << 24) |
1042 * mono_metadata_parse_typedef_or_ref:
1043 * @m: a metadata context.
1044 * @ptr: a pointer to an encoded TypedefOrRef in @m
1045 * @rptr: pointer updated to match the end of the decoded stream
1047 * Returns: a token valid in the @m metadata decoded from
1048 * the compressed representation.
1051 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1054 token = mono_metadata_decode_value (ptr, &ptr);
1057 return mono_metadata_token_from_dor (token);
1061 * mono_metadata_parse_custom_mod:
1062 * @m: a metadata context.
1063 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1064 * @ptr: a pointer to (possibly) the start of a custom modifier list
1065 * @rptr: pointer updated to match the end of the decoded stream
1067 * Checks if @ptr points to a type custom modifier compressed representation.
1069 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1072 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1074 MonoCustomMod local;
1075 if ((*ptr == MONO_TYPE_CMOD_OPT) ||
1076 (*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.
1286 mono_metadata_init (void)
1290 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1291 generic_inst_cache = g_hash_table_new (mono_generic_inst_hash, mono_generic_inst_equal);
1292 generic_class_cache = g_hash_table_new (mono_generic_class_hash, mono_generic_class_equal);
1294 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1295 g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
1299 * mono_metadata_parse_type:
1300 * @m: metadata context
1301 * @mode: king of type that may be found at @ptr
1302 * @opt_attrs: optional attributes to store in the returned type
1303 * @ptr: pointer to the type representation
1304 * @rptr: pointer updated to match the end of the decoded stream
1306 * Decode a compressed type description found at @ptr in @m.
1307 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1308 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1309 * This function can be used to decode type descriptions in method signatures,
1310 * field signatures, locals signatures etc.
1312 * To parse a generic type, `generic_container' points to the current class'es
1313 * (the `generic_container' field in the MonoClass) or the current generic method's
1314 * (the `generic_container' field in the MonoMethodNormal) generic container.
1315 * When we encounter any MONO_TYPE_VAR or MONO_TYPE_MVAR's, they're looked up in
1316 * this MonoGenericContainer.
1318 * Returns: a #MonoType structure representing the decoded type.
1321 mono_metadata_parse_type_full (MonoImage *m, MonoGenericContext *generic_context, MonoParseTypeMode mode,
1322 short opt_attrs, const char *ptr, const char **rptr)
1324 MonoType *type, *cached;
1326 gboolean byref = FALSE;
1327 gboolean pinned = FALSE;
1328 const char *tmp_ptr;
1333 * According to the spec, custom modifiers should come before the byref
1334 * flag, but the IL produced by ilasm from the following signature:
1335 * object modopt(...) &
1336 * starts with a byref flag, followed by the modifiers. (bug #49802)
1337 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1338 * it would be better to treat byref as real type constructor instead of
1340 * Also, pinned should come before anything else, but some MSV++ produced
1341 * assemblies violate this (#bug 61990).
1344 /* Count the modifiers first */
1349 case MONO_TYPE_PINNED:
1350 case MONO_TYPE_BYREF:
1353 case MONO_TYPE_CMOD_REQD:
1354 case MONO_TYPE_CMOD_OPT:
1356 mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1364 type = g_malloc0 (sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1365 type->num_mods = count;
1367 g_warning ("got more than 64 modifiers in type");
1371 memset (type, 0, sizeof (MonoType));
1374 /* Parse pinned, byref and custom modifiers */
1379 case MONO_TYPE_PINNED:
1383 case MONO_TYPE_BYREF:
1387 case MONO_TYPE_CMOD_REQD:
1388 case MONO_TYPE_CMOD_OPT:
1389 mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1397 type->attrs = opt_attrs;
1398 type->byref = byref;
1399 type->pinned = pinned ? 1 : 0;
1401 do_mono_metadata_parse_type (type, m, generic_context, ptr, &ptr);
1407 /* FIXME: remove the != MONO_PARSE_PARAM condition, this accounts for
1408 * almost 10k (about 2/3rds) of all MonoType's we create.
1410 if (mode != MONO_PARSE_PARAM && !type->num_mods) {
1411 /* no need to free type here, because it is on the stack */
1412 if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
1414 return &type->data.klass->this_arg;
1416 return &type->data.klass->byval_arg;
1418 /* No need to use locking since nobody is modifying the hash table */
1419 if ((cached = g_hash_table_lookup (type_cache, type)))
1423 /*printf ("%x%c %s\n", type->attrs, type->pinned ? 'p' : ' ', mono_type_full_name (type));*/
1426 type = g_memdup (&stype, sizeof (MonoType));
1431 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
1432 const char *ptr, const char **rptr)
1434 return mono_metadata_parse_type_full (m, NULL, mode, opt_attrs, ptr, rptr);
1438 * mono_metadata_parse_signature:
1439 * @image: metadata context
1440 * @toke: metadata token
1442 * Decode a method signature stored in the STANDALONESIG table
1444 * Returns: a MonoMethodSignature describing the signature.
1446 MonoMethodSignature *
1447 mono_metadata_parse_signature_full (MonoImage *image, MonoGenericContext *generic_context, guint32 token)
1449 MonoTableInfo *tables = image->tables;
1450 guint32 idx = mono_metadata_token_index (token);
1455 return mono_lookup_dynamic_token (image, token);
1457 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1459 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1461 ptr = mono_metadata_blob_heap (image, sig);
1462 mono_metadata_decode_blob_size (ptr, &ptr);
1464 return mono_metadata_parse_method_signature_full (image, generic_context, FALSE, ptr, NULL);
1467 MonoMethodSignature *
1468 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1470 return mono_metadata_parse_signature_full (image, NULL, token);
1473 MonoMethodSignature*
1474 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1476 MonoMethodSignature *sig;
1478 /* later we want to allocate signatures with mempools */
1479 sig = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1480 sig->param_count = nparams;
1481 sig->sentinelpos = -1;
1486 MonoMethodSignature*
1487 mono_metadata_signature_dup (MonoMethodSignature *sig)
1491 sigsize = sizeof (MonoMethodSignature) + sig->param_count * sizeof (MonoType *);
1492 return g_memdup (sig, sigsize);
1496 * mono_metadata_parse_method_signature:
1497 * @m: metadata context
1498 * @def: the MethodDef index or 0 for Ref signatures.
1499 * @ptr: pointer to the signature metadata representation
1500 * @rptr: pointer updated to match the end of the decoded stream
1502 * Decode a method signature stored at @ptr.
1504 * Returns: a MonoMethodSignature describing the signature.
1506 MonoMethodSignature *
1507 mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContext *generic_context,
1508 int def, const char *ptr, const char **rptr)
1510 MonoMethodSignature *method;
1511 int i, ret_attrs = 0, *pattrs = NULL;
1512 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1513 guint32 gen_param_count = 0;
1514 gboolean is_open = FALSE;
1515 MonoGenericContext *context = NULL;
1518 gen_param_count = 1;
1523 call_convention = *ptr & 0x0F;
1525 if (gen_param_count)
1526 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1527 param_count = mono_metadata_decode_value (ptr, &ptr);
1528 pattrs = g_new0 (int, param_count);
1531 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1532 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1533 guint32 cols [MONO_PARAM_SIZE];
1534 guint lastp, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1536 if (def < methodt->rows)
1537 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1539 lastp = paramt->rows + 1;
1540 for (i = param_index; i < lastp; ++i) {
1541 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1542 if (!cols [MONO_PARAM_SEQUENCE])
1543 ret_attrs = cols [MONO_PARAM_FLAGS];
1545 pattrs [cols [MONO_PARAM_SEQUENCE] - 1] = cols [MONO_PARAM_FLAGS];
1548 method = mono_metadata_signature_alloc (m, param_count);
1549 method->hasthis = hasthis;
1550 method->explicit_this = explicit_this;
1551 method->call_convention = call_convention;
1552 method->generic_param_count = gen_param_count;
1554 if (gen_param_count)
1555 method->has_type_parameters = 1;
1557 if (gen_param_count && (!generic_context || !generic_context->container->is_method)) {
1558 MonoGenericContainer *container = g_new0 (MonoGenericContainer, 1);
1560 if (generic_context)
1561 container->parent = generic_context->container;
1562 container->is_signature = 1;
1564 context = &container->context;
1565 container->context.container = container;
1567 container->type_argc = gen_param_count;
1568 container->type_params = g_new0 (MonoGenericParam, gen_param_count);
1570 for (i = 0; i < gen_param_count; i++) {
1571 container->type_params [i].owner = container;
1572 container->type_params [i].num = i;
1575 context = generic_context;
1577 if (call_convention != 0xa) {
1578 method->ret = mono_metadata_parse_type_full (m, context, MONO_PARSE_RET, ret_attrs, ptr, &ptr);
1579 is_open = mono_class_is_open_constructed_type (method->ret);
1582 if (method->param_count) {
1583 method->sentinelpos = -1;
1585 for (i = 0; i < method->param_count; ++i) {
1586 if (*ptr == MONO_TYPE_SENTINEL) {
1587 if (method->call_convention != MONO_CALL_VARARG || def)
1588 g_error ("found sentinel for methoddef or no vararg method");
1589 method->sentinelpos = i;
1592 method->params [i] = mono_metadata_parse_type_full (
1593 m, context, MONO_PARSE_PARAM, pattrs [i], ptr, &ptr);
1595 is_open = mono_class_is_open_constructed_type (method->params [i]);
1599 method->has_type_parameters = is_open;
1601 if (def && (method->call_convention == MONO_CALL_VARARG))
1602 method->sentinelpos = method->param_count;
1609 * Add signature to a cache and increase ref count...
1614 MonoMethodSignature *
1615 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1617 return mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr);
1621 * mono_metadata_free_method_signature:
1622 * @sig: signature to destroy
1624 * Free the memory allocated in the signature @sig.
1627 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1630 mono_metadata_free_type (sig->ret);
1631 for (i = 0; i < sig->param_count; ++i)
1632 mono_metadata_free_type (sig->params [i]);
1638 mono_metadata_lookup_generic_inst (MonoGenericInst *ginst)
1640 MonoGenericInst *cached;
1643 cached = g_hash_table_lookup (generic_inst_cache, ginst);
1645 for (i = 0; i < ginst->type_argc; i++)
1646 mono_metadata_free_type (ginst->type_argv [i]);
1647 g_free (ginst->type_argv);
1652 ginst->id = ++next_generic_inst_id;
1653 g_hash_table_insert (generic_inst_cache, ginst, ginst);
1659 mono_metadata_lookup_generic_class (MonoGenericClass *gclass)
1661 MonoGenericClass *cached;
1663 cached = g_hash_table_lookup (generic_class_cache, gclass);
1667 g_hash_table_insert (generic_class_cache, gclass, gclass);
1672 mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context)
1674 MonoGenericInst *nginst;
1677 nginst = g_new0 (MonoGenericInst, 1);
1678 nginst->type_argc = ginst->type_argc;
1679 nginst->type_argv = g_new0 (MonoType*, nginst->type_argc);
1680 nginst->is_reference = 1;
1682 for (i = 0; i < nginst->type_argc; i++) {
1683 MonoType *t = mono_class_inflate_generic_type (ginst->type_argv [i], context);
1685 if (!nginst->is_open)
1686 nginst->is_open = mono_class_is_open_constructed_type (t);
1687 if (nginst->is_reference)
1688 nginst->is_reference = MONO_TYPE_IS_REFERENCE (t);
1690 nginst->type_argv [i] = t;
1693 return mono_metadata_lookup_generic_inst (nginst);
1697 mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContext *generic_context,
1698 int count, const char *ptr, const char **rptr)
1700 MonoGenericInst *ginst;
1703 ginst = g_new0 (MonoGenericInst, 1);
1704 ginst->type_argc = count;
1705 ginst->type_argv = g_new0 (MonoType*, count);
1706 ginst->is_reference = 1;
1708 for (i = 0; i < ginst->type_argc; i++) {
1709 MonoType *t = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_TYPE, 0, ptr, &ptr);
1711 ginst->type_argv [i] = t;
1712 if (!ginst->is_open)
1713 ginst->is_open = mono_class_is_open_constructed_type (t);
1714 if (ginst->is_reference)
1715 ginst->is_reference = MONO_TYPE_IS_REFERENCE (t);
1721 return mono_metadata_lookup_generic_inst (ginst);
1725 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContext *generic_context,
1726 const char *ptr, const char **rptr)
1728 MonoInflatedGenericClass *igclass;
1729 MonoGenericClass *gclass, *cached;
1734 igclass = g_new0 (MonoInflatedGenericClass, 1);
1735 gclass = &igclass->generic_class;
1736 gclass->is_inflated = TRUE;
1738 type->data.generic_class = gclass;
1740 gclass->context = g_new0 (MonoGenericContext, 1);
1741 gclass->context->gclass = gclass;
1743 igclass->klass = g_new0 (MonoClass, 1);
1745 gtype = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_TYPE, 0, ptr, &ptr);
1746 gclass->container_class = gklass = mono_class_from_mono_type (gtype);
1748 g_assert ((gclass->context->container = gklass->generic_container) != NULL);
1749 count = mono_metadata_decode_value (ptr, &ptr);
1752 * Create the klass before parsing the type arguments.
1753 * This is required to support "recursive" definitions.
1754 * See mcs/tests/gen-23.cs for an example.
1757 gclass->inst = mono_metadata_parse_generic_inst (m, generic_context, count, ptr, &ptr);
1763 * We may be called multiple times on different metadata to create the same
1764 * instantiated type. This happens for instance if we're part of a method or
1765 * local variable signature.
1767 * It's important to return the same MonoGenericClass * for each particualar
1768 * instantiation of a generic type (ie "Stack<Int32>") to make static fields
1771 * According to the spec ($26.1.5), a static variable in a generic class
1772 * declaration is shared amongst all instances of the same closed constructed
1776 cached = g_hash_table_lookup (generic_class_cache, gclass);
1778 g_free (igclass->klass);
1781 type->data.generic_class = cached;
1784 g_hash_table_insert (generic_class_cache, gclass, gclass);
1786 mono_stats.generic_instance_count++;
1787 mono_stats.generics_metadata_size += sizeof (MonoGenericClass) +
1788 sizeof (MonoGenericContext) +
1789 gclass->inst->type_argc * sizeof (MonoType);
1794 * do_mono_metadata_parse_generic_param:
1795 * @generic_container: Our MonoClass's or MonoMethodNormal's MonoGenericContainer;
1796 * see mono_metadata_parse_type_full() for details.
1797 * Internal routine to parse a generic type parameter.
1799 static MonoGenericParam *
1800 mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContext *generic_context,
1801 gboolean is_mvar, const char *ptr, const char **rptr)
1803 MonoGenericContainer *generic_container;
1806 index = mono_metadata_decode_value (ptr, &ptr);
1810 g_assert (generic_context);
1811 if (generic_context->gmethod)
1812 generic_container = generic_context->gmethod->container;
1813 else if (generic_context->gclass)
1814 generic_container = generic_context->gclass->container_class->generic_container;
1816 generic_container = generic_context->container;
1819 g_assert (generic_container);
1820 if (generic_container->parent) {
1822 * The current MonoGenericContainer is a generic method -> its `parent'
1823 * points to the containing class'es container.
1825 generic_container = generic_container->parent;
1827 g_assert (generic_container && !generic_container->is_method);
1828 g_assert (index < generic_container->type_argc);
1830 return &generic_container->type_params [index];
1832 g_assert (generic_container && (generic_container->is_method || generic_container->is_signature));
1833 g_assert (index < generic_container->type_argc);
1835 return &generic_container->type_params [index];
1840 * do_mono_metadata_parse_type:
1841 * @type: MonoType to be filled in with the return value
1843 * Internal routine used to "fill" the contents of @type from an
1844 * allocated pointer. This is done this way to avoid doing too
1845 * many mini-allocations (particularly for the MonoFieldType which
1846 * most of the time is just a MonoType, but sometimes might be augmented).
1848 * This routine is used by mono_metadata_parse_type and
1849 * mono_metadata_parse_field_type
1851 * This extracts a Type as specified in Partition II (22.2.12)
1854 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContext *generic_context,
1855 const char *ptr, const char **rptr)
1857 type->type = mono_metadata_decode_value (ptr, &ptr);
1859 switch (type->type){
1860 case MONO_TYPE_VOID:
1861 case MONO_TYPE_BOOLEAN:
1862 case MONO_TYPE_CHAR:
1875 case MONO_TYPE_STRING:
1876 case MONO_TYPE_OBJECT:
1877 case MONO_TYPE_TYPEDBYREF:
1879 case MONO_TYPE_VALUETYPE:
1880 case MONO_TYPE_CLASS: {
1882 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
1883 type->data.klass = mono_class_get (m, token);
1886 case MONO_TYPE_SZARRAY: {
1887 MonoType *etype = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1888 type->data.klass = mono_class_from_mono_type (etype);
1889 mono_metadata_free_type (etype);
1893 type->data.type = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1895 case MONO_TYPE_FNPTR:
1896 type->data.method = mono_metadata_parse_method_signature_full (m, generic_context, 0, ptr, &ptr);
1898 case MONO_TYPE_ARRAY:
1899 type->data.array = mono_metadata_parse_array_full (m, generic_context, ptr, &ptr);
1902 case MONO_TYPE_MVAR:
1903 type->data.generic_param = mono_metadata_parse_generic_param (m, generic_context, TRUE, ptr, &ptr);
1907 type->data.generic_param = mono_metadata_parse_generic_param (m, generic_context, FALSE, ptr, &ptr);
1910 case MONO_TYPE_GENERICINST:
1911 do_mono_metadata_parse_generic_class (type, m, generic_context, ptr, &ptr);
1915 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
1923 * mono_metadata_free_type:
1924 * @type: type to free
1926 * Free the memory allocated for type @type.
1929 mono_metadata_free_type (MonoType *type)
1931 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
1934 switch (type->type){
1935 case MONO_TYPE_OBJECT:
1936 case MONO_TYPE_STRING:
1937 if (!type->data.klass)
1940 case MONO_TYPE_CLASS:
1941 case MONO_TYPE_VALUETYPE:
1942 if (type == &type->data.klass->byval_arg || type == &type->data.klass->this_arg)
1946 mono_metadata_free_type (type->data.type);
1948 case MONO_TYPE_FNPTR:
1949 mono_metadata_free_method_signature (type->data.method);
1951 case MONO_TYPE_ARRAY:
1952 mono_metadata_free_array (type->data.array);
1960 hex_dump (const char *buffer, int base, int count)
1962 int show_header = 1;
1970 for (i = 0; i < count; i++){
1973 printf ("\n0x%08x: ", (unsigned char) base + i);
1975 printf ("%02x ", (unsigned char) (buffer [i]));
1982 * @mh: The Method header
1983 * @ptr: Points to the beginning of the Section Data (25.3)
1986 parse_section_data (MonoImage *m, MonoMethodHeader *mh, const unsigned char *ptr)
1988 unsigned char sect_data_flags;
1989 const unsigned char *sptr;
1991 guint32 sect_data_len;
1994 /* align on 32-bit boundary */
1995 /* FIXME: not 64-bit clean code */
1996 sptr = ptr = dword_align (ptr);
1997 sect_data_flags = *ptr;
2000 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
2002 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
2005 sect_data_len = ptr [0];
2009 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
2010 hex_dump (sptr, 0, sect_data_len+8);
2011 g_print ("\nheader: ");
2012 hex_dump (sptr-4, 0, 4);
2016 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
2017 const unsigned char *p = dword_align (ptr);
2019 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
2020 /* we could just store a pointer if we don't need to byteswap */
2021 mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
2022 for (i = 0; i < mh->num_clauses; ++i) {
2023 MonoExceptionClause *ec = &mh->clauses [i];
2026 ec->flags = read32 (p);
2028 ec->try_offset = read32 (p);
2030 ec->try_len = read32 (p);
2032 ec->handler_offset = read32 (p);
2034 ec->handler_len = read32 (p);
2036 tof_value = read32 (p);
2039 ec->flags = read16 (p);
2041 ec->try_offset = read16 (p);
2045 ec->handler_offset = read16 (p);
2047 ec->handler_len = *p;
2049 tof_value = read32 (p);
2052 if (ec->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2053 ec->data.filter_offset = tof_value;
2054 } else if (ec->flags == MONO_EXCEPTION_CLAUSE_NONE) {
2055 ec->data.catch_class = tof_value? mono_class_get (m, tof_value): 0;
2057 ec->data.catch_class = NULL;
2059 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
2063 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
2064 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
2071 * mono_metadata_parse_mh:
2072 * @m: metadata context
2073 * @ptr: pointer to the method header.
2075 * Decode the method header at @ptr, including pointer to the IL code,
2076 * info about local variables and optional exception tables.
2078 * Returns: a MonoMethodHeader.
2081 mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContext *generic_context, const char *ptr)
2083 MonoMethodHeader *mh;
2084 unsigned char flags = *(const unsigned char *) ptr;
2085 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
2087 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
2088 const unsigned char *code;
2091 g_return_val_if_fail (ptr != NULL, NULL);
2094 case METHOD_HEADER_TINY_FORMAT:
2095 mh = g_new0 (MonoMethodHeader, 1);
2098 local_var_sig_tok = 0;
2099 mh->code_size = flags >> 2;
2102 case METHOD_HEADER_TINY_FORMAT1:
2103 mh = g_new0 (MonoMethodHeader, 1);
2106 local_var_sig_tok = 0;
2109 * The spec claims 3 bits, but the Beta2 is
2112 mh->code_size = flags >> 2;
2115 case METHOD_HEADER_FAT_FORMAT:
2116 fat_flags = read16 (ptr);
2118 hsize = (fat_flags >> 12) & 0xf;
2119 max_stack = read16 (ptr);
2121 code_size = read32 (ptr);
2123 local_var_sig_tok = read32 (ptr);
2126 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
2133 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
2137 * There are more sections
2139 ptr = code + code_size;
2147 if (local_var_sig_tok) {
2148 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
2149 const char *locals_ptr;
2150 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
2151 int len=0, i, bsize;
2153 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
2154 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
2155 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
2156 if (*locals_ptr != 0x07)
2157 g_warning ("wrong signature for locals blob");
2159 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
2160 mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
2161 mh->num_locals = len;
2162 for (i = 0; i < len; ++i)
2163 mh->locals [i] = mono_metadata_parse_type_full (
2164 m, generic_context, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
2166 mh = g_new0 (MonoMethodHeader, 1);
2169 mh->code_size = code_size;
2170 mh->max_stack = max_stack;
2171 mh->init_locals = init_locals;
2172 if (fat_flags & METHOD_HEADER_MORE_SECTS)
2173 parse_section_data (m, mh, (const unsigned char*)ptr);
2178 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
2180 return mono_metadata_parse_mh_full (m, NULL, ptr);
2184 * mono_metadata_free_mh:
2185 * @mh: a method header
2187 * Free the memory allocated for the method header.
2190 mono_metadata_free_mh (MonoMethodHeader *mh)
2193 for (i = 0; i < mh->num_locals; ++i)
2194 mono_metadata_free_type (mh->locals[i]);
2195 g_free (mh->clauses);
2200 * mono_metadata_parse_field_type:
2201 * @m: metadata context to extract information from
2202 * @ptr: pointer to the field signature
2203 * @rptr: pointer updated to match the end of the decoded stream
2205 * Parses the field signature, and returns the type information for it.
2207 * Returns: The MonoType that was extracted from @ptr.
2210 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
2212 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
2216 * mono_metadata_parse_param:
2217 * @m: metadata context to extract information from
2218 * @ptr: pointer to the param signature
2219 * @rptr: pointer updated to match the end of the decoded stream
2221 * Parses the param signature, and returns the type information for it.
2223 * Returns: The MonoType that was extracted from @ptr.
2226 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
2228 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
2232 * mono_metadata_token_from_dor:
2233 * @dor_token: A TypeDefOrRef coded index
2235 * dor_token is a TypeDefOrRef coded index: it contains either
2236 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
2237 * bits contain an index into the table.
2239 * Returns: an expanded token
2242 mono_metadata_token_from_dor (guint32 dor_index)
2246 table = dor_index & 0x03;
2247 idx = dor_index >> 2;
2250 case 0: /* TypeDef */
2251 return MONO_TOKEN_TYPE_DEF | idx;
2253 case 1: /* TypeRef */
2254 return MONO_TOKEN_TYPE_REF | idx;
2256 case 2: /* TypeSpec */
2257 return MONO_TOKEN_TYPE_SPEC | idx;
2260 g_assert_not_reached ();
2267 * We use this to pass context information to the row locator
2270 int idx; /* The index that we are trying to locate */
2271 int col_idx; /* The index in the row where idx may be stored */
2272 MonoTableInfo *t; /* pointer to the table */
2277 * How the row locator works.
2282 * ___|___------> _______
2285 * A column in the rows of table A references an index in table B.
2286 * For example A may be the TYPEDEF table and B the METHODDEF table.
2288 * Given an index in table B we want to get the row in table A
2289 * where the column n references our index in B.
2291 * In the locator_t structure:
2293 * col_idx is the column number
2294 * index is the index in table B
2295 * result will be the index in table A
2298 * Table A Table B column (in table A)
2299 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
2300 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
2301 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
2302 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
2303 * METHODSEM PROPERTY ASSOCIATION (encoded index)
2305 * Note that we still don't support encoded indexes.
2309 typedef_locator (const void *a, const void *b)
2311 locator_t *loc = (locator_t *) a;
2312 const char *bb = (const char *) b;
2313 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
2314 guint32 col, col_next;
2316 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
2322 * Need to check that the next row is valid.
2324 if (typedef_index + 1 < loc->t->rows) {
2325 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
2326 if (loc->idx >= col_next)
2329 if (col == col_next)
2333 loc->result = typedef_index;
2339 table_locator (const void *a, const void *b)
2341 locator_t *loc = (locator_t *) a;
2342 const char *bb = (const char *) b;
2343 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2346 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2348 if (loc->idx == col) {
2349 loc->result = table_index;
2359 declsec_locator (const void *a, const void *b)
2361 locator_t *loc = (locator_t *) a;
2362 const char *bb = (const char *) b;
2363 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2366 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2368 if (loc->idx == col) {
2369 loc->result = table_index;
2379 * mono_metadata_typedef_from_field:
2380 * @meta: metadata context
2381 * @index: FieldDef token
2383 * Returns: the 1-based index into the TypeDef table of the type that
2384 * declared the field described by @index, or 0 if not found.
2387 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
2389 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2395 loc.idx = mono_metadata_token_index (index);
2396 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
2399 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2400 g_assert_not_reached ();
2402 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2403 return loc.result + 1;
2407 * mono_metadata_typedef_from_method:
2408 * @meta: metadata context
2409 * @index: MethodDef token
2411 * Returns: the 1-based index into the TypeDef table of the type that
2412 * declared the method described by @index. 0 if not found.
2415 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
2417 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2423 loc.idx = mono_metadata_token_index (index);
2424 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
2427 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2428 g_assert_not_reached ();
2430 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2431 return loc.result + 1;
2435 * mono_metadata_interfaces_from_typedef:
2436 * @meta: metadata context
2437 * @index: typedef token
2439 * Returns: and array of interfaces that the @index typedef token implements.
2440 * The number of elemnts in the array is returned in @count.
2443 mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, guint *count, MonoGenericContext *context)
2445 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
2448 guint32 cols [MONO_INTERFACEIMPL_SIZE];
2456 loc.idx = mono_metadata_token_index (index);
2457 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
2460 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2465 * We may end up in the middle of the rows...
2468 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
2475 while (start < tdef->rows) {
2476 mono_metadata_decode_row (tdef, start, cols, MONO_INTERFACEIMPL_SIZE);
2477 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
2479 result = g_renew (MonoClass*, result, i + 1);
2480 result [i] = mono_class_get_full (
2481 meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]), context);
2489 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
2491 return mono_metadata_interfaces_from_typedef_full (meta, index, count, NULL);
2495 * mono_metadata_nested_in_typedef:
2496 * @meta: metadata context
2497 * @index: typedef token
2499 * Returns: the 1-based index into the TypeDef table of the type
2500 * where the type described by @index is nested.
2501 * Retruns 0 if @index describes a non-nested type.
2504 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
2506 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2512 loc.idx = mono_metadata_token_index (index);
2513 loc.col_idx = MONO_NESTED_CLASS_NESTED;
2516 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2519 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2520 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
2524 * mono_metadata_nesting_typedef:
2525 * @meta: metadata context
2526 * @index: typedef token
2528 * Returns: the 1-based index into the TypeDef table of the first type
2529 * that is nested inside the type described by @index. The search starts at
2530 * @start_index. returns 0 if no such type is found.
2533 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
2535 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2537 guint32 class_index = mono_metadata_token_index (index);
2542 start = start_index;
2544 while (start <= tdef->rows) {
2545 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
2551 if (start > tdef->rows)
2558 * mono_metadata_packing_from_typedef:
2559 * @meta: metadata context
2560 * @index: token representing a type
2562 * Returns: the info stored in the ClassLAyout table for the given typedef token
2563 * into the @packing and @size pointers.
2564 * Returns 0 if the info is not found.
2567 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
2569 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
2571 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
2576 loc.idx = mono_metadata_token_index (index);
2577 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
2580 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2583 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
2585 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
2587 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
2589 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2590 return loc.result + 1;
2594 * mono_metadata_custom_attrs_from_index:
2595 * @meta: metadata context
2596 * @index: token representing the parent
2598 * Returns: the 1-based index into the CustomAttribute table of the first
2599 * attribute which belongs to the metadata object described by @index.
2600 * Returns 0 if no such attribute is found.
2603 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
2605 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
2612 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
2615 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2618 /* Find the first entry by searching backwards */
2619 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
2622 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2623 return loc.result + 1;
2627 * mono_metadata_declsec_from_index:
2628 * @meta: metadata context
2629 * @index: token representing the parent
2631 * Returns: the 0-based index into the DeclarativeSecurity table of the first
2632 * attribute which belongs to the metadata object described by @index.
2633 * Returns -1 if no such attribute is found.
2636 mono_metadata_declsec_from_index (MonoImage *meta, guint32 index)
2638 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_DECLSECURITY];
2645 loc.col_idx = MONO_DECL_SECURITY_PARENT;
2648 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
2651 /* Find the first entry by searching backwards */
2652 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_DECL_SECURITY_PARENT) == index))
2660 mono_backtrace (int limit)
2665 backtrace (array, limit);
2666 names = backtrace_symbols (array, limit);
2667 for (i =0; i < limit; ++i) {
2668 g_print ("\t%s\n", names [i]);
2675 /*#define __alignof__(a) sizeof(a)*/
2676 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
2681 * @t: the type to return the size of
2683 * Returns: the number of bytes required to hold an instance of this
2687 mono_type_size (MonoType *t, gint *align)
2694 *align = __alignof__(gpointer);
2695 return sizeof (gpointer);
2699 case MONO_TYPE_VOID:
2702 case MONO_TYPE_BOOLEAN:
2703 *align = __alignof__(gint8);
2707 *align = __alignof__(gint8);
2709 case MONO_TYPE_CHAR:
2712 *align = __alignof__(gint16);
2716 *align = __alignof__(gint32);
2719 *align = __alignof__(float);
2723 *align = __alignof__(gint64);
2726 *align = __alignof__(double);
2730 *align = __alignof__(gpointer);
2731 return sizeof (gpointer);
2732 case MONO_TYPE_STRING:
2733 *align = __alignof__(gpointer);
2734 return sizeof (gpointer);
2735 case MONO_TYPE_OBJECT:
2736 *align = __alignof__(gpointer);
2737 return sizeof (gpointer);
2738 case MONO_TYPE_VALUETYPE: {
2739 if (t->data.klass->enumtype)
2740 return mono_type_size (t->data.klass->enum_basetype, align);
2742 return mono_class_value_size (t->data.klass, align);
2744 case MONO_TYPE_CLASS:
2745 case MONO_TYPE_SZARRAY:
2747 case MONO_TYPE_FNPTR:
2748 case MONO_TYPE_ARRAY:
2749 *align = __alignof__(gpointer);
2750 return sizeof (gpointer);
2751 case MONO_TYPE_TYPEDBYREF:
2752 return mono_class_value_size (mono_defaults.typed_reference_class, align);
2753 case MONO_TYPE_GENERICINST: {
2754 MonoInflatedGenericClass *gclass;
2755 MonoClass *container_class;
2757 gclass = mono_get_inflated_generic_class (t->data.generic_class);
2758 g_assert (!gclass->generic_class.inst->is_open);
2759 g_assert (!gclass->klass->generic_container);
2761 container_class = gclass->generic_class.container_class;
2763 if (container_class->valuetype) {
2764 if (container_class->enumtype)
2765 return mono_type_size (container_class->enum_basetype, align);
2767 return mono_class_value_size (gclass->klass, align);
2769 *align = __alignof__(gpointer);
2770 return sizeof (gpointer);
2774 case MONO_TYPE_MVAR:
2775 *align = __alignof__(gpointer);
2776 return sizeof (gpointer);
2778 g_error ("mono_type_size: type 0x%02x unknown", t->type);
2784 * mono_type_stack_size:
2785 * @t: the type to return the size it uses on the stack
2787 * Returns: the number of bytes required to hold an instance of this
2788 * type on the runtime stack
2791 mono_type_stack_size (MonoType *t, gint *align)
2795 g_assert (t != NULL);
2801 *align = __alignof__(gpointer);
2802 return sizeof (gpointer);
2806 case MONO_TYPE_BOOLEAN:
2807 case MONO_TYPE_CHAR:
2816 case MONO_TYPE_STRING:
2817 case MONO_TYPE_OBJECT:
2818 case MONO_TYPE_CLASS:
2819 case MONO_TYPE_SZARRAY:
2821 case MONO_TYPE_FNPTR:
2822 case MONO_TYPE_ARRAY:
2823 *align = __alignof__(gpointer);
2824 return sizeof (gpointer);
2825 case MONO_TYPE_TYPEDBYREF:
2826 *align = __alignof__(gpointer);
2827 return sizeof (gpointer) * 3;
2829 *align = __alignof__(float);
2830 return sizeof (float);
2833 *align = __alignof__(gint64);
2834 return sizeof (gint64);
2836 *align = __alignof__(double);
2837 return sizeof (double);
2838 case MONO_TYPE_VALUETYPE: {
2841 if (t->data.klass->enumtype)
2842 return mono_type_stack_size (t->data.klass->enum_basetype, align);
2844 size = mono_class_value_size (t->data.klass, align);
2846 *align = *align + __alignof__(gpointer) - 1;
2847 *align &= ~(__alignof__(gpointer) - 1);
2849 size += sizeof (gpointer) - 1;
2850 size &= ~(sizeof (gpointer) - 1);
2855 case MONO_TYPE_GENERICINST: {
2856 MonoInflatedGenericClass *gclass;
2857 MonoClass *container_class;
2859 gclass = mono_get_inflated_generic_class (t->data.generic_class);
2860 container_class = gclass->generic_class.container_class;
2862 g_assert (!gclass->generic_class.inst->is_open);
2863 g_assert (!gclass->klass->generic_container);
2865 if (container_class->valuetype) {
2866 if (container_class->enumtype)
2867 return mono_type_stack_size (container_class->enum_basetype, align);
2869 guint32 size = mono_class_value_size (gclass->klass, align);
2871 *align = *align + __alignof__(gpointer) - 1;
2872 *align &= ~(__alignof__(gpointer) - 1);
2874 size += sizeof (gpointer) - 1;
2875 size &= ~(sizeof (gpointer) - 1);
2880 *align = __alignof__(gpointer);
2881 return sizeof (gpointer);
2885 g_error ("type 0x%02x unknown", t->type);
2891 mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass)
2893 return gclass->container_class->valuetype;
2897 _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2, gboolean signature_only)
2901 if ((g1->inst->type_argc != g2->inst->type_argc) || (g1->is_dynamic != g2->is_dynamic) ||
2902 (g1->inst->is_reference != g2->inst->is_reference))
2904 if (!mono_metadata_class_equal (g1->container_class, g2->container_class, signature_only))
2906 for (i = 0; i < g1->inst->type_argc; ++i) {
2907 if (!do_mono_metadata_type_equal (g1->inst->type_argv [i], g2->inst->type_argv [i], signature_only))
2914 mono_metadata_generic_method_hash (MonoGenericMethod *gmethod)
2916 return gmethod->inst->id;
2920 mono_metadata_generic_method_equal (MonoGenericMethod *g1, MonoGenericMethod *g2)
2922 return (g1->container == g2->container) && (g1->generic_class == g2->generic_class) &&
2923 (g1->inst == g2->inst);
2928 * mono_metadata_type_hash:
2931 * Computes an hash value for @t1 to be used in GHashTable.
2934 mono_metadata_type_hash (MonoType *t1)
2936 guint hash = t1->type;
2938 hash |= t1->byref << 6; /* do not collide with t1->type values */
2940 case MONO_TYPE_VALUETYPE:
2941 case MONO_TYPE_CLASS:
2942 case MONO_TYPE_SZARRAY:
2943 /* check if the distribution is good enough */
2944 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
2946 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
2947 case MONO_TYPE_ARRAY:
2948 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
2949 case MONO_TYPE_GENERICINST:
2950 return ((hash << 5) - hash) ^ mono_generic_class_hash (t1->data.generic_class);
2956 mono_metadata_generic_param_equal (MonoGenericParam *p1, MonoGenericParam *p2, gboolean signature_only)
2960 if (p1->num != p2->num)
2963 g_assert (p1->owner && p2->owner);
2964 if (p1->owner == p2->owner)
2968 * If `signature_only' is true, we're comparing two (method) signatures.
2969 * In this case, the owner of two type parameters doesn't need to match.
2972 return signature_only;
2976 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only)
2980 if (c1->generic_class && c2->generic_class)
2981 return _mono_metadata_generic_class_equal (c1->generic_class, c2->generic_class, signature_only);
2982 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
2983 return mono_metadata_generic_param_equal (
2984 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, FALSE);
2985 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
2986 return mono_metadata_generic_param_equal (
2987 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
2988 if (signature_only &&
2989 (c1->byval_arg.type == MONO_TYPE_SZARRAY) && (c2->byval_arg.type == MONO_TYPE_SZARRAY))
2990 return mono_metadata_class_equal (c1->byval_arg.data.klass, c2->byval_arg.data.klass, signature_only);
2995 * mono_metadata_type_equal:
2999 * Determine if @t1 and @t2 represent the same type.
3000 * Returns: #TRUE if @t1 and @t2 are equal.
3003 do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only)
3005 if (t1->type != t2->type ||
3006 t1->byref != t2->byref)
3010 case MONO_TYPE_VOID:
3011 case MONO_TYPE_BOOLEAN:
3012 case MONO_TYPE_CHAR:
3023 case MONO_TYPE_STRING:
3026 case MONO_TYPE_OBJECT:
3027 case MONO_TYPE_TYPEDBYREF:
3029 case MONO_TYPE_VALUETYPE:
3030 case MONO_TYPE_CLASS:
3031 case MONO_TYPE_SZARRAY:
3032 return mono_metadata_class_equal (t1->data.klass, t2->data.klass, signature_only);
3034 return do_mono_metadata_type_equal (t1->data.type, t2->data.type, signature_only);
3035 case MONO_TYPE_ARRAY:
3036 if (t1->data.array->rank != t2->data.array->rank)
3038 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass, signature_only);
3039 case MONO_TYPE_GENERICINST:
3040 return _mono_metadata_generic_class_equal (
3041 t1->data.generic_class, t2->data.generic_class, signature_only);
3043 return mono_metadata_generic_param_equal (
3044 t1->data.generic_param, t2->data.generic_param, FALSE);
3045 case MONO_TYPE_MVAR:
3046 return mono_metadata_generic_param_equal (
3047 t1->data.generic_param, t2->data.generic_param, signature_only);
3049 g_error ("implement type compare for %0x!", t1->type);
3057 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
3059 return do_mono_metadata_type_equal (t1, t2, FALSE);
3063 * mono_metadata_signature_equal:
3064 * @sig1: a signature
3065 * @sig2: another signature
3067 * Determine if @sig1 and @sig2 represent the same signature, with the
3068 * same number of arguments and the same types.
3069 * Returns: #TRUE if @sig1 and @sig2 are equal.
3072 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
3076 if (sig1->hasthis != sig2->hasthis ||
3077 sig1->param_count != sig2->param_count)
3081 * We're just comparing the signatures of two methods here:
3083 * If we have two generic methods `void Foo<U> (U u)' and `void Bar<V> (V v)',
3084 * U and V are equal here.
3086 * That's what the `signature_only' argument of do_mono_metadata_type_equal() is for.
3089 for (i = 0; i < sig1->param_count; i++) {
3090 MonoType *p1 = sig1->params[i];
3091 MonoType *p2 = sig2->params[i];
3093 /* if (p1->attrs != p2->attrs)
3096 if (!do_mono_metadata_type_equal (p1, p2, TRUE))
3100 if (!do_mono_metadata_type_equal (sig1->ret, sig2->ret, TRUE))
3106 mono_signature_hash (MonoMethodSignature *sig)
3108 guint i, res = sig->ret->type;
3110 for (i = 0; i < sig->param_count; i++)
3111 res = (res << 5) - res + mono_type_hash (sig->params[i]);
3117 * mono_metadata_encode_value:
3118 * @value: value to encode
3119 * @buf: buffer where to write the compressed representation
3120 * @endbuf: pointer updated to point at the end of the encoded output
3122 * Encodes the value @value in the compressed representation used
3123 * in metadata and stores the result in @buf. @buf needs to be big
3124 * enough to hold the data (4 bytes).
3127 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
3133 else if (value < 0x4000) {
3134 p [0] = 0x80 | (value >> 8);
3135 p [1] = value & 0xff;
3138 p [0] = (value >> 24) | 0xc0;
3139 p [1] = (value >> 16) & 0xff;
3140 p [2] = (value >> 8) & 0xff;
3141 p [3] = value & 0xff;
3149 * mono_metadata_field_info:
3150 * @meta: the Image the field is defined in
3151 * @index: the index in the field table representing the field
3152 * @offset: a pointer to an integer where to store the offset that
3153 * may have been specified for the field in a FieldLayout table
3154 * @rva: a pointer to the RVA of the field data in the image that
3155 * may have been defined in a FieldRVA table
3156 * @marshal_spec: a pointer to the marshal spec that may have been
3157 * defined for the field in a FieldMarshal table.
3159 * Gather info for field @index that may have been defined in the FieldLayout,
3160 * FieldRVA and FieldMarshal tables.
3161 * Either of offset, rva and marshal_spec can be NULL if you're not interested
3165 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
3166 MonoMarshalSpec **marshal_spec)
3168 MonoTableInfo *tdef;
3171 loc.idx = index + 1;
3173 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
3175 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
3178 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3179 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
3181 *offset = (guint32)-1;
3185 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
3187 loc.col_idx = MONO_FIELD_RVA_FIELD;
3190 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3192 * LAMESPEC: There is no signature, no nothing, just the raw data.
3194 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
3202 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
3203 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
3210 * mono_metadata_get_constant_index:
3211 * @meta: the Image the field is defined in
3212 * @index: the token that may have a row defined in the constants table
3213 * @hint: possible position for the row
3215 * @token must be a FieldDef, ParamDef or PropertyDef token.
3217 * Returns: the index into the Constants table or 0 if not found.
3220 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
3222 MonoTableInfo *tdef;
3224 guint32 index = mono_metadata_token_index (token);
3226 tdef = &meta->tables [MONO_TABLE_CONSTANT];
3227 index <<= MONO_HASCONSTANT_BITS;
3228 switch (mono_metadata_token_table (token)) {
3229 case MONO_TABLE_FIELD:
3230 index |= MONO_HASCONSTANT_FIEDDEF;
3232 case MONO_TABLE_PARAM:
3233 index |= MONO_HASCONSTANT_PARAM;
3235 case MONO_TABLE_PROPERTY:
3236 index |= MONO_HASCONSTANT_PROPERTY;
3239 g_warning ("Not a valid token for the constant table: 0x%08x", token);
3243 loc.col_idx = MONO_CONSTANT_PARENT;
3246 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
3249 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3250 return loc.result + 1;
3256 * mono_metadata_events_from_typedef:
3257 * @meta: metadata context
3258 * @index: 0-based index (in the TypeDef table) describing a type
3260 * Returns: the 0-based index in the Event table for the events in the
3261 * type. The last event that belongs to the type (plus 1) is stored
3262 * in the @end_idx pointer.
3265 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3269 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
3277 loc.col_idx = MONO_EVENT_MAP_PARENT;
3278 loc.idx = index + 1;
3280 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3283 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
3284 if (loc.result + 1 < tdef->rows) {
3285 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
3287 end = meta->tables [MONO_TABLE_EVENT].rows;
3295 * mono_metadata_methods_from_event:
3296 * @meta: metadata context
3297 * @index: 0-based index (in the Event table) describing a event
3299 * Returns: the 0-based index in the MethodDef table for the methods in the
3300 * event. The last method that belongs to the event (plus 1) is stored
3301 * in the @end_idx pointer.
3304 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
3308 guint32 cols [MONO_METHOD_SEMA_SIZE];
3309 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3316 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3317 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
3319 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3324 * We may end up in the middle of the rows...
3327 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3333 while (end < msemt->rows) {
3334 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3335 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3344 * mono_metadata_properties_from_typedef:
3345 * @meta: metadata context
3346 * @index: 0-based index (in the TypeDef table) describing a type
3348 * Returns: the 0-based index in the Property table for the properties in the
3349 * type. The last property that belongs to the type (plus 1) is stored
3350 * in the @end_idx pointer.
3353 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3357 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
3365 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
3366 loc.idx = index + 1;
3368 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3371 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
3372 if (loc.result + 1 < tdef->rows) {
3373 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
3375 end = meta->tables [MONO_TABLE_PROPERTY].rows;
3383 * mono_metadata_methods_from_property:
3384 * @meta: metadata context
3385 * @index: 0-based index (in the PropertyDef table) describing a property
3387 * Returns: the 0-based index in the MethodDef table for the methods in the
3388 * property. The last method that belongs to the property (plus 1) is stored
3389 * in the @end_idx pointer.
3392 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
3396 guint32 cols [MONO_METHOD_SEMA_SIZE];
3397 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3404 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3405 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
3407 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3412 * We may end up in the middle of the rows...
3415 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3421 while (end < msemt->rows) {
3422 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3423 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3432 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
3435 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
3441 loc.col_idx = MONO_IMPLMAP_MEMBER;
3442 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
3444 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3447 return loc.result + 1;
3451 * @image: context where the image is created
3452 * @type_spec: typespec token
3454 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
3458 mono_type_create_from_typespec_full (MonoImage *image, MonoGenericContext *generic_context, guint32 type_spec)
3460 guint32 idx = mono_metadata_token_index (type_spec);
3462 guint32 cols [MONO_TYPESPEC_SIZE];
3467 mono_loader_lock ();
3469 if ((type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec)))) {
3470 mono_loader_unlock ();
3474 t = &image->tables [MONO_TABLE_TYPESPEC];
3476 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
3477 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
3478 len = mono_metadata_decode_value (ptr, &ptr);
3480 type = g_new0 (MonoType, 1);
3482 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
3484 if (*ptr == MONO_TYPE_BYREF) {
3489 do_mono_metadata_parse_type (type, image, generic_context, ptr, &ptr);
3491 mono_loader_unlock ();
3497 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
3499 return mono_type_create_from_typespec_full (image, NULL, type_spec);
3503 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
3505 MonoMarshalSpec *res;
3507 const char *start = ptr;
3509 /* fixme: this is incomplete, but I cant find more infos in the specs */
3511 res = g_new0 (MonoMarshalSpec, 1);
3513 len = mono_metadata_decode_value (ptr, &ptr);
3514 res->native = *ptr++;
3516 if (res->native == MONO_NATIVE_LPARRAY) {
3517 res->data.array_data.param_num = -1;
3518 res->data.array_data.num_elem = -1;
3519 res->data.array_data.elem_mult = -1;
3521 if (ptr - start <= len)
3522 res->data.array_data.elem_type = *ptr++;
3523 if (ptr - start <= len)
3524 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
3525 if (ptr - start <= len)
3526 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3527 if (ptr - start <= len) {
3529 * LAMESPEC: Older spec versions say this parameter comes before
3530 * num_elem. Never spec versions don't talk about elem_mult at
3531 * all, but csc still emits it, and it is used to distinguish
3532 * between param_num being 0, and param_num being omitted.
3533 * So if (param_num == 0) && (num_elem > 0), then
3534 * elem_mult == 0 -> the array size is num_elem
3535 * elem_mult == 1 -> the array size is @param_num + num_elem
3537 res->data.array_data.elem_mult = mono_metadata_decode_value (ptr, &ptr);
3541 if (res->native == MONO_NATIVE_BYVALTSTR) {
3542 if (ptr - start <= len)
3543 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3546 if (res->native == MONO_NATIVE_BYVALARRAY) {
3547 if (ptr - start <= len)
3548 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3551 if (res->native == MONO_NATIVE_CUSTOM) {
3552 /* skip unused type guid */
3553 len = mono_metadata_decode_value (ptr, &ptr);
3555 /* skip unused native type name */
3556 len = mono_metadata_decode_value (ptr, &ptr);
3558 /* read custom marshaler type name */
3559 len = mono_metadata_decode_value (ptr, &ptr);
3560 res->data.custom_data.custom_name = g_strndup (ptr, len);
3562 /* read cookie string */
3563 len = mono_metadata_decode_value (ptr, &ptr);
3564 res->data.custom_data.cookie = g_strndup (ptr, len);
3567 if (res->native == MONO_NATIVE_SAFEARRAY) {
3568 res->data.safearray_data.elem_type = 0;
3569 res->data.safearray_data.num_elem = 0;
3570 if (ptr - start <= len)
3571 res->data.safearray_data.elem_type = *ptr++;
3572 if (ptr - start <= len)
3573 res->data.safearray_data.num_elem = *ptr++;
3579 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
3581 if (spec->native == MONO_NATIVE_CUSTOM) {
3582 g_free (spec->data.custom_data.custom_name);
3583 g_free (spec->data.custom_data.cookie);
3589 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
3590 gboolean unicode, MonoMarshalConv *conv)
3592 MonoMarshalConv dummy_conv;
3598 *conv = MONO_MARSHAL_CONV_NONE;
3601 return MONO_NATIVE_UINT;
3605 case MONO_TYPE_BOOLEAN:
3607 switch (mspec->native) {
3608 case MONO_NATIVE_VARIANTBOOL:
3609 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
3610 return MONO_NATIVE_VARIANTBOOL;
3611 case MONO_NATIVE_BOOLEAN:
3612 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3613 return MONO_NATIVE_BOOLEAN;
3614 case MONO_NATIVE_I1:
3615 case MONO_NATIVE_U1:
3616 return mspec->native;
3618 g_error ("cant marshal bool to native type %02x", mspec->native);
3621 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3622 return MONO_NATIVE_BOOLEAN;
3623 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
3624 case MONO_TYPE_I1: return MONO_NATIVE_I1;
3625 case MONO_TYPE_U1: return MONO_NATIVE_U1;
3626 case MONO_TYPE_I2: return MONO_NATIVE_I2;
3627 case MONO_TYPE_U2: return MONO_NATIVE_U2;
3628 case MONO_TYPE_I4: return MONO_NATIVE_I4;
3629 case MONO_TYPE_U4: return MONO_NATIVE_U4;
3630 case MONO_TYPE_I8: return MONO_NATIVE_I8;
3631 case MONO_TYPE_U8: return MONO_NATIVE_U8;
3632 case MONO_TYPE_R4: return MONO_NATIVE_R4;
3633 case MONO_TYPE_R8: return MONO_NATIVE_R8;
3634 case MONO_TYPE_STRING:
3636 switch (mspec->native) {
3637 case MONO_NATIVE_BSTR:
3638 *conv = MONO_MARSHAL_CONV_STR_BSTR;
3639 return MONO_NATIVE_BSTR;
3640 case MONO_NATIVE_LPSTR:
3641 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
3642 return MONO_NATIVE_LPSTR;
3643 case MONO_NATIVE_LPWSTR:
3644 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
3645 return MONO_NATIVE_LPWSTR;
3646 case MONO_NATIVE_LPTSTR:
3647 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3648 return MONO_NATIVE_LPTSTR;
3649 case MONO_NATIVE_ANSIBSTR:
3650 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
3651 return MONO_NATIVE_ANSIBSTR;
3652 case MONO_NATIVE_TBSTR:
3653 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
3654 return MONO_NATIVE_TBSTR;
3655 case MONO_NATIVE_BYVALTSTR:
3657 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
3659 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
3660 return MONO_NATIVE_BYVALTSTR;
3662 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);
3665 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3666 return MONO_NATIVE_LPTSTR;
3667 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
3668 case MONO_TYPE_VALUETYPE: /*FIXME*/
3669 if (type->data.klass->enumtype) {
3670 t = type->data.klass->enum_basetype->type;
3673 return MONO_NATIVE_STRUCT;
3674 case MONO_TYPE_SZARRAY:
3675 case MONO_TYPE_ARRAY:
3677 switch (mspec->native) {
3678 case MONO_NATIVE_BYVALARRAY:
3679 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
3680 return MONO_NATIVE_BYVALARRAY;
3681 case MONO_NATIVE_SAFEARRAY:
3682 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
3683 return MONO_NATIVE_SAFEARRAY;
3684 case MONO_NATIVE_LPARRAY:
3685 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3686 return MONO_NATIVE_LPARRAY;
3688 g_error ("cant marshal array as native type %02x", mspec->native);
3692 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3693 return MONO_NATIVE_LPARRAY;
3694 case MONO_TYPE_I: return MONO_NATIVE_INT;
3695 case MONO_TYPE_U: return MONO_NATIVE_UINT;
3696 case MONO_TYPE_CLASS:
3697 case MONO_TYPE_OBJECT: {
3698 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
3700 switch (mspec->native) {
3701 case MONO_NATIVE_STRUCT:
3702 return MONO_NATIVE_STRUCT;
3703 case MONO_NATIVE_INTERFACE:
3704 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
3705 return MONO_NATIVE_INTERFACE;
3706 case MONO_NATIVE_IDISPATCH:
3707 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
3708 return MONO_NATIVE_IDISPATCH;
3709 case MONO_NATIVE_IUNKNOWN:
3710 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
3711 return MONO_NATIVE_IUNKNOWN;
3712 case MONO_NATIVE_FUNC:
3713 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3714 type->data.klass == mono_defaults.delegate_class ||
3715 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3716 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3717 return MONO_NATIVE_FUNC;
3723 g_error ("cant marshal object as native type %02x", mspec->native);
3726 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3727 type->data.klass == mono_defaults.delegate_class ||
3728 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3729 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3730 return MONO_NATIVE_FUNC;
3732 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
3733 return MONO_NATIVE_STRUCT;
3735 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
3736 case MONO_TYPE_GENERICINST:
3737 type = &type->data.generic_class->container_class->byval_arg;
3740 case MONO_TYPE_TYPEDBYREF:
3742 g_error ("type 0x%02x not handled in marshal", t);
3744 return MONO_NATIVE_MAX;
3748 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
3751 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
3757 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
3758 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
3760 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3763 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
3767 method_from_method_def_or_ref (MonoImage *m, guint32 tok, MonoGenericContext *context)
3769 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
3770 switch (tok & MONO_METHODDEFORREF_MASK) {
3771 case MONO_METHODDEFORREF_METHODDEF:
3772 return mono_get_method_full (m, MONO_TOKEN_METHOD_DEF | idx, NULL, context);
3773 case MONO_METHODDEFORREF_METHODREF:
3774 return mono_get_method_full (m, MONO_TOKEN_MEMBER_REF | idx, NULL, context);
3776 g_assert_not_reached ();
3781 mono_class_get_overrides_full (MonoImage *image, guint32 type_token, gint32 *num_overrides,
3782 MonoGenericContext *generic_context)
3785 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
3788 guint32 cols [MONO_METHODIMPL_SIZE];
3789 MonoMethod **result;
3798 loc.col_idx = MONO_METHODIMPL_CLASS;
3799 loc.idx = mono_metadata_token_index (type_token);
3801 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3807 * We may end up in the middle of the rows...
3810 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
3815 while (end < tdef->rows) {
3816 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
3822 result = g_new (MonoMethod*, num * 2);
3823 for (i = 0; i < num; ++i) {
3824 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
3825 result [i * 2] = method_from_method_def_or_ref (
3826 image, cols [MONO_METHODIMPL_DECLARATION], generic_context);
3827 result [i * 2 + 1] = method_from_method_def_or_ref (
3828 image, cols [MONO_METHODIMPL_BODY], generic_context);
3832 *num_overrides = num;
3837 * mono_guid_to_string:
3839 * Converts a 16 byte Microsoft GUID to the standard string representation.
3842 mono_guid_to_string (const guint8 *guid)
3844 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
3845 guid[3], guid[2], guid[1], guid[0],
3849 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
3853 get_constraints (MonoImage *image, int owner, MonoGenericContext *context)
3855 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
3856 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
3857 guint32 i, token, found;
3858 MonoClass *klass, **res;
3859 GList *cons = NULL, *tmp;
3863 for (i = 0; i < tdef->rows; ++i) {
3864 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
3865 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
3866 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
3867 klass = mono_class_get_full (image, token, context);
3868 cons = g_list_append (cons, klass);
3871 /* contiguous list finished */
3878 res = g_new0 (MonoClass*, found + 1);
3879 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
3880 res [i] = tmp->data;
3887 mono_metadata_has_generic_params (MonoImage *image, guint32 token)
3889 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
3890 guint32 cols [MONO_GENERICPARAM_SIZE];
3891 guint32 i, owner = 0;
3893 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
3894 owner = MONO_TYPEORMETHOD_TYPE;
3895 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3896 owner = MONO_TYPEORMETHOD_METHOD;
3898 g_error ("wrong token %x to load_generics_params", token);
3901 owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
3905 for (i = 0; i < tdef->rows; ++i) {
3906 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3907 if (cols [MONO_GENERICPARAM_OWNER] == owner)
3910 if (i >= tdef->rows)
3916 MonoGenericContainer *
3917 mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericContainer *parent_container)
3919 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
3920 guint32 cols [MONO_GENERICPARAM_SIZE];
3921 guint32 i, owner = 0, last_num, n;
3922 MonoGenericContainer *container;
3923 MonoGenericParam *params;
3925 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
3926 owner = MONO_TYPEORMETHOD_TYPE;
3927 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3928 owner = MONO_TYPEORMETHOD_METHOD;
3930 g_error ("wrong token %x to load_generics_params", token);
3933 owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
3937 for (i = 0; i < tdef->rows; ++i) {
3938 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3939 if (cols [MONO_GENERICPARAM_OWNER] == owner)
3943 if (i >= tdef->rows)
3947 container = g_new0 (MonoGenericContainer, 1);
3950 params = g_realloc (params, sizeof (MonoGenericParam) * n);
3951 params [n - 1].owner = container;
3952 params [n - 1].pklass = NULL;
3953 params [n - 1].method = NULL;
3954 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
3955 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
3956 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
3957 if (++i >= tdef->rows)
3959 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3960 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
3962 container->type_argc = n;
3963 container->type_params = params;
3964 container->parent = parent_container;
3966 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3967 container->is_method = 1;
3969 container->context.container = container;
3971 for (i = 0; i < n; i++)
3972 params [i].constraints = get_constraints (image, last_num + i + 1, &container->context);
3978 mono_type_is_byref (MonoType *type)
3984 mono_type_get_type (MonoType *type)
3989 /* For MONO_TYPE_FNPTR */
3990 MonoMethodSignature*
3991 mono_type_get_signature (MonoType *type)
3993 return type->data.method;
3996 /* For MONO_TYPE_CLASS, VALUETYPE */
3998 mono_type_get_class (MonoType *type)
4000 return type->data.klass;
4003 /* For MONO_TYPE_ARRAY */
4005 mono_type_get_array_type (MonoType *type)
4007 return type->data.array;
4011 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
4013 /* FIXME: implement */
4018 mono_signature_get_return_type (MonoMethodSignature *sig)
4024 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
4030 /* start from the first */
4031 if (sig->param_count) {
4032 *iter = &sig->params [0];
4033 return sig->params [0];
4041 if (type < &sig->params [sig->param_count]) {
4049 mono_signature_get_param_count (MonoMethodSignature *sig)
4051 return sig->param_count;
4055 mono_signature_get_call_conv (MonoMethodSignature *sig)
4057 return sig->call_convention;
4061 mono_signature_vararg_start (MonoMethodSignature *sig)
4063 return sig->sentinelpos;
4067 mono_signature_is_instance (MonoMethodSignature *sig)
4069 return sig->hasthis;
4073 mono_signature_explicit_this (MonoMethodSignature *sig)
4075 return sig->explicit_this;
4078 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
4080 mono_aligned_addr_hash (gconstpointer ptr)
4082 return GPOINTER_TO_UINT (ptr) >> 3;