2 * metadata.c: Routines for accessing the metadata
5 * Miguel de Icaza (miguel@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * (C) 2001-2002 Ximian, Inc.
17 #include "tabledefs.h"
18 #include "mono-endian.h"
20 #include "tokentype.h"
21 #include "metadata-internals.h"
22 #include "class-internals.h"
25 static gboolean do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container,
26 const char *ptr, const char **rptr);
28 static gboolean do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only);
29 static gboolean mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only);
30 static gboolean mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only);
31 static gboolean _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2,
32 gboolean signature_only);
35 * This enumeration is used to describe the data types in the metadata
46 /* Index into Blob heap */
49 /* Index into String heap */
55 /* Pointer into a table */
58 /* HasConstant:Parent pointer (Param, Field or Property) */
61 /* HasCustomAttribute index. Indexes any table except CustomAttribute */
64 /* CustomAttributeType encoded index */
67 /* HasDeclSecurity index: TypeDef Method or Assembly */
70 /* Implementation coded index: File, Export AssemblyRef */
73 /* HasFieldMarshal coded index: Field or Param table */
76 /* MemberForwardedIndex: Field or Method */
79 /* TypeDefOrRef coded index: typedef, typeref, typespec */
82 /* MemberRefParent coded index: typeref, moduleref, method, memberref, typesepc, typedef */
85 /* MethodDefOrRef coded index: Method or Member Ref table */
88 /* HasSemantic coded index: Event or Property */
91 /* ResolutionScope coded index: Module, ModuleRef, AssemblytRef, TypeRef */
95 const static unsigned char TableSchemas [] = {
96 #define ASSEMBLY_SCHEMA_OFFSET 0
97 MONO_MT_UINT32, /* "HashId" }, */
98 MONO_MT_UINT16, /* "Major" }, */
99 MONO_MT_UINT16, /* "Minor" }, */
100 MONO_MT_UINT16, /* "BuildNumber" }, */
101 MONO_MT_UINT16, /* "RevisionNumber" }, */
102 MONO_MT_UINT32, /* "Flags" }, */
103 MONO_MT_BLOB_IDX, /* "PublicKey" }, */
104 MONO_MT_STRING_IDX, /* "Name" }, */
105 MONO_MT_STRING_IDX, /* "Culture" }, */
108 #define ASSEMBLYOS_SCHEMA_OFFSET ASSEMBLY_SCHEMA_OFFSET + 10
109 MONO_MT_UINT32, /* "OSPlatformID" }, */
110 MONO_MT_UINT32, /* "OSMajor" }, */
111 MONO_MT_UINT32, /* "OSMinor" }, */
114 #define ASSEMBLYPROC_SCHEMA_OFFSET ASSEMBLYOS_SCHEMA_OFFSET + 4
115 MONO_MT_UINT32, /* "Processor" }, */
118 #define ASSEMBLYREF_SCHEMA_OFFSET ASSEMBLYPROC_SCHEMA_OFFSET + 2
119 MONO_MT_UINT16, /* "Major" }, */
120 MONO_MT_UINT16, /* "Minor" }, */
121 MONO_MT_UINT16, /* "Build" }, */
122 MONO_MT_UINT16, /* "Revision" }, */
123 MONO_MT_UINT32, /* "Flags" }, */
124 MONO_MT_BLOB_IDX, /* "PublicKeyOrToken" }, */
125 MONO_MT_STRING_IDX, /* "Name" }, */
126 MONO_MT_STRING_IDX, /* "Culture" }, */
127 MONO_MT_BLOB_IDX, /* "HashValue" }, */
130 #define ASSEMBLYREFOS_SCHEMA_OFFSET ASSEMBLYREF_SCHEMA_OFFSET + 10
131 MONO_MT_UINT32, /* "OSPlatformID" }, */
132 MONO_MT_UINT32, /* "OSMajorVersion" }, */
133 MONO_MT_UINT32, /* "OSMinorVersion" }, */
134 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
137 #define ASSEMBLYREFPROC_SCHEMA_OFFSET ASSEMBLYREFOS_SCHEMA_OFFSET + 5
138 MONO_MT_UINT32, /* "Processor" }, */
139 MONO_MT_TABLE_IDX, /* "AssemblyRef:AssemblyRef" }, */
142 #define CLASS_LAYOUT_SCHEMA_OFFSET ASSEMBLYREFPROC_SCHEMA_OFFSET + 3
143 MONO_MT_UINT16, /* "PackingSize" }, */
144 MONO_MT_UINT32, /* "ClassSize" }, */
145 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
148 #define CONSTANT_SCHEMA_OFFSET CLASS_LAYOUT_SCHEMA_OFFSET + 4
149 MONO_MT_UINT8, /* "Type" }, */
150 MONO_MT_UINT8, /* "PaddingZero" }, */
151 MONO_MT_CONST_IDX, /* "Parent" }, */
152 MONO_MT_BLOB_IDX, /* "Value" }, */
155 #define CUSTOM_ATTR_SCHEMA_OFFSET CONSTANT_SCHEMA_OFFSET + 5
156 MONO_MT_HASCAT_IDX, /* "Parent" }, */
157 MONO_MT_CAT_IDX, /* "Type" }, */
158 MONO_MT_BLOB_IDX, /* "Value" }, */
161 #define DECL_SEC_SCHEMA_OFFSET CUSTOM_ATTR_SCHEMA_OFFSET + 4
162 MONO_MT_UINT16, /* "Action" }, */
163 MONO_MT_HASDEC_IDX, /* "Parent" }, */
164 MONO_MT_BLOB_IDX, /* "PermissionSet" }, */
167 #define EVENTMAP_SCHEMA_OFFSET DECL_SEC_SCHEMA_OFFSET + 4
168 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
169 MONO_MT_TABLE_IDX, /* "EventList:Event" }, */
172 #define EVENT_SCHEMA_OFFSET EVENTMAP_SCHEMA_OFFSET + 3
173 MONO_MT_UINT16, /* "EventFlags#EventAttribute" }, */
174 MONO_MT_STRING_IDX, /* "Name" }, */
175 MONO_MT_TABLE_IDX, /* "EventType" }, TypeDef or TypeRef */
178 #define EVENT_POINTER_SCHEMA_OFFSET EVENT_SCHEMA_OFFSET + 4
179 MONO_MT_TABLE_IDX, /* "Event" }, */
182 #define EXPORTED_TYPE_SCHEMA_OFFSET EVENT_POINTER_SCHEMA_OFFSET + 2
183 MONO_MT_UINT32, /* "Flags" }, */
184 MONO_MT_TABLE_IDX, /* "TypeDefId" }, */
185 MONO_MT_STRING_IDX, /* "TypeName" }, */
186 MONO_MT_STRING_IDX, /* "TypeNameSpace" }, */
187 MONO_MT_IMPL_IDX, /* "Implementation" }, */
190 #define FIELD_SCHEMA_OFFSET EXPORTED_TYPE_SCHEMA_OFFSET + 6
191 MONO_MT_UINT16, /* "Flags" }, */
192 MONO_MT_STRING_IDX, /* "Name" }, */
193 MONO_MT_BLOB_IDX, /* "Signature" }, */
196 #define FIELD_LAYOUT_SCHEMA_OFFSET FIELD_SCHEMA_OFFSET + 4
197 MONO_MT_UINT32, /* "Offset" }, */
198 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
201 #define FIELD_MARSHAL_SCHEMA_OFFSET FIELD_LAYOUT_SCHEMA_OFFSET + 3
202 MONO_MT_HFM_IDX, /* "Parent" }, */
203 MONO_MT_BLOB_IDX, /* "NativeType" }, */
206 #define FIELD_RVA_SCHEMA_OFFSET FIELD_MARSHAL_SCHEMA_OFFSET + 3
207 MONO_MT_UINT32, /* "RVA" }, */
208 MONO_MT_TABLE_IDX, /* "Field:Field" }, */
211 #define FIELD_POINTER_SCHEMA_OFFSET FIELD_RVA_SCHEMA_OFFSET + 3
212 MONO_MT_TABLE_IDX, /* "Field" }, */
215 #define FILE_SCHEMA_OFFSET FIELD_POINTER_SCHEMA_OFFSET + 2
216 MONO_MT_UINT32, /* "Flags" }, */
217 MONO_MT_STRING_IDX, /* "Name" }, */
218 MONO_MT_BLOB_IDX, /* "Value" }, */
221 #define IMPLMAP_SCHEMA_OFFSET FILE_SCHEMA_OFFSET + 4
222 MONO_MT_UINT16, /* "MappingFlag" }, */
223 MONO_MT_MF_IDX, /* "MemberForwarded" }, */
224 MONO_MT_STRING_IDX, /* "ImportName" }, */
225 MONO_MT_TABLE_IDX, /* "ImportScope:ModuleRef" }, */
228 #define IFACEMAP_SCHEMA_OFFSET IMPLMAP_SCHEMA_OFFSET + 5
229 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
230 MONO_MT_TDOR_IDX, /* "Interface=TypeDefOrRef" }, */
233 #define MANIFEST_SCHEMA_OFFSET IFACEMAP_SCHEMA_OFFSET + 3
234 MONO_MT_UINT32, /* "Offset" }, */
235 MONO_MT_UINT32, /* "Flags" }, */
236 MONO_MT_STRING_IDX, /* "Name" }, */
237 MONO_MT_IMPL_IDX, /* "Implementation" }, */
240 #define MEMBERREF_SCHEMA_OFFSET MANIFEST_SCHEMA_OFFSET + 5
241 MONO_MT_MRP_IDX, /* "Class" }, */
242 MONO_MT_STRING_IDX, /* "Name" }, */
243 MONO_MT_BLOB_IDX, /* "Signature" }, */
246 #define METHOD_SCHEMA_OFFSET MEMBERREF_SCHEMA_OFFSET + 4
247 MONO_MT_UINT32, /* "RVA" }, */
248 MONO_MT_UINT16, /* "ImplFlags#MethodImplAttributes" }, */
249 MONO_MT_UINT16, /* "Flags#MethodAttribute" }, */
250 MONO_MT_STRING_IDX, /* "Name" }, */
251 MONO_MT_BLOB_IDX, /* "Signature" }, */
252 MONO_MT_TABLE_IDX, /* "ParamList:Param" }, */
255 #define METHOD_IMPL_SCHEMA_OFFSET METHOD_SCHEMA_OFFSET + 7
256 MONO_MT_TABLE_IDX, /* "Class:TypeDef" }, */
257 MONO_MT_MDOR_IDX, /* "MethodBody" }, */
258 MONO_MT_MDOR_IDX, /* "MethodDeclaration" }, */
261 #define METHOD_SEMA_SCHEMA_OFFSET METHOD_IMPL_SCHEMA_OFFSET + 4
262 MONO_MT_UINT16, /* "MethodSemantic" }, */
263 MONO_MT_TABLE_IDX, /* "Method:Method" }, */
264 MONO_MT_HS_IDX, /* "Association" }, */
267 #define METHOD_POINTER_SCHEMA_OFFSET METHOD_SEMA_SCHEMA_OFFSET + 4
268 MONO_MT_TABLE_IDX, /* "Method" }, */
271 #define MODULE_SCHEMA_OFFSET METHOD_POINTER_SCHEMA_OFFSET + 2
272 MONO_MT_UINT16, /* "Generation" }, */
273 MONO_MT_STRING_IDX, /* "Name" }, */
274 MONO_MT_GUID_IDX, /* "MVID" }, */
275 MONO_MT_GUID_IDX, /* "EncID" }, */
276 MONO_MT_GUID_IDX, /* "EncBaseID" }, */
279 #define MODULEREF_SCHEMA_OFFSET MODULE_SCHEMA_OFFSET + 6
280 MONO_MT_STRING_IDX, /* "Name" }, */
283 #define NESTED_CLASS_SCHEMA_OFFSET MODULEREF_SCHEMA_OFFSET + 2
284 MONO_MT_TABLE_IDX, /* "NestedClass:TypeDef" }, */
285 MONO_MT_TABLE_IDX, /* "EnclosingClass:TypeDef" }, */
288 #define PARAM_SCHEMA_OFFSET NESTED_CLASS_SCHEMA_OFFSET + 3
289 MONO_MT_UINT16, /* "Flags" }, */
290 MONO_MT_UINT16, /* "Sequence" }, */
291 MONO_MT_STRING_IDX, /* "Name" }, */
294 #define PARAM_POINTER_SCHEMA_OFFSET PARAM_SCHEMA_OFFSET + 4
295 MONO_MT_TABLE_IDX, /* "Param" }, */
298 #define PROPERTY_SCHEMA_OFFSET PARAM_POINTER_SCHEMA_OFFSET + 2
299 MONO_MT_UINT16, /* "Flags" }, */
300 MONO_MT_STRING_IDX, /* "Name" }, */
301 MONO_MT_BLOB_IDX, /* "Type" }, */
304 #define PROPERTY_POINTER_SCHEMA_OFFSET PROPERTY_SCHEMA_OFFSET + 4
305 MONO_MT_TABLE_IDX, /* "Property" }, */
308 #define PROPERTY_MAP_SCHEMA_OFFSET PROPERTY_POINTER_SCHEMA_OFFSET + 2
309 MONO_MT_TABLE_IDX, /* "Parent:TypeDef" }, */
310 MONO_MT_TABLE_IDX, /* "PropertyList:Property" }, */
313 #define STDALON_SIG_SCHEMA_OFFSET PROPERTY_MAP_SCHEMA_OFFSET + 3
314 MONO_MT_BLOB_IDX, /* "Signature" }, */
317 #define TYPEDEF_SCHEMA_OFFSET STDALON_SIG_SCHEMA_OFFSET + 2
318 MONO_MT_UINT32, /* "Flags" }, */
319 MONO_MT_STRING_IDX, /* "Name" }, */
320 MONO_MT_STRING_IDX, /* "Namespace" }, */
321 MONO_MT_TDOR_IDX, /* "Extends" }, */
322 MONO_MT_TABLE_IDX, /* "FieldList:Field" }, */
323 MONO_MT_TABLE_IDX, /* "MethodList:Method" }, */
326 #define TYPEREF_SCHEMA_OFFSET TYPEDEF_SCHEMA_OFFSET + 7
327 MONO_MT_RS_IDX, /* "ResolutionScope=ResolutionScope" }, */
328 MONO_MT_STRING_IDX, /* "Name" }, */
329 MONO_MT_STRING_IDX, /* "Namespace" }, */
332 #define TYPESPEC_SCHEMA_OFFSET TYPEREF_SCHEMA_OFFSET + 4
333 MONO_MT_BLOB_IDX, /* "Signature" }, */
336 #define GENPARAM_SCHEMA_OFFSET TYPESPEC_SCHEMA_OFFSET + 2
337 MONO_MT_UINT16, /* "Number" }, */
338 MONO_MT_UINT16, /* "Flags" }, */
339 MONO_MT_TABLE_IDX, /* "Owner" }, TypeDef or MethodDef */
340 MONO_MT_STRING_IDX, /* "Name" }, */
343 #define METHOD_SPEC_SCHEMA_OFFSET GENPARAM_SCHEMA_OFFSET + 5
344 MONO_MT_MDOR_IDX, /* "Method" }, */
345 MONO_MT_BLOB_IDX, /* "Signature" }, */
348 #define GEN_CONSTRAINT_SCHEMA_OFFSET METHOD_SPEC_SCHEMA_OFFSET + 3
349 MONO_MT_TABLE_IDX, /* "GenericParam" }, */
350 MONO_MT_TDOR_IDX, /* "Constraint" }, */
353 #define NULL_SCHEMA_OFFSET GEN_CONSTRAINT_SCHEMA_OFFSET + 3
357 /* Must be the same order as MONO_TABLE_* */
358 const static unsigned char
359 table_description [] = {
360 MODULE_SCHEMA_OFFSET,
361 TYPEREF_SCHEMA_OFFSET,
362 TYPEDEF_SCHEMA_OFFSET,
363 FIELD_POINTER_SCHEMA_OFFSET,
365 METHOD_POINTER_SCHEMA_OFFSET,
366 METHOD_SCHEMA_OFFSET,
367 PARAM_POINTER_SCHEMA_OFFSET,
369 IFACEMAP_SCHEMA_OFFSET,
370 MEMBERREF_SCHEMA_OFFSET, /* 0xa */
371 CONSTANT_SCHEMA_OFFSET,
372 CUSTOM_ATTR_SCHEMA_OFFSET,
373 FIELD_MARSHAL_SCHEMA_OFFSET,
374 DECL_SEC_SCHEMA_OFFSET,
375 CLASS_LAYOUT_SCHEMA_OFFSET,
376 FIELD_LAYOUT_SCHEMA_OFFSET, /* 0x10 */
377 STDALON_SIG_SCHEMA_OFFSET,
378 EVENTMAP_SCHEMA_OFFSET,
379 EVENT_POINTER_SCHEMA_OFFSET,
381 PROPERTY_MAP_SCHEMA_OFFSET,
382 PROPERTY_POINTER_SCHEMA_OFFSET,
383 PROPERTY_SCHEMA_OFFSET,
384 METHOD_SEMA_SCHEMA_OFFSET,
385 METHOD_IMPL_SCHEMA_OFFSET,
386 MODULEREF_SCHEMA_OFFSET, /* 0x1a */
387 TYPESPEC_SCHEMA_OFFSET,
388 IMPLMAP_SCHEMA_OFFSET,
389 FIELD_RVA_SCHEMA_OFFSET,
392 ASSEMBLY_SCHEMA_OFFSET, /* 0x20 */
393 ASSEMBLYPROC_SCHEMA_OFFSET,
394 ASSEMBLYOS_SCHEMA_OFFSET,
395 ASSEMBLYREF_SCHEMA_OFFSET,
396 ASSEMBLYREFPROC_SCHEMA_OFFSET,
397 ASSEMBLYREFOS_SCHEMA_OFFSET,
399 EXPORTED_TYPE_SCHEMA_OFFSET,
400 MANIFEST_SCHEMA_OFFSET,
401 NESTED_CLASS_SCHEMA_OFFSET,
402 GENPARAM_SCHEMA_OFFSET, /* 0x2a */
403 METHOD_SPEC_SCHEMA_OFFSET,
404 GEN_CONSTRAINT_SCHEMA_OFFSET
407 #ifdef HAVE_ARRAY_ELEM_INIT
408 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
409 #define MSGSTRFIELD1(line) str##line
410 static const struct msgstr_t {
411 #define TABLEDEF(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
412 #include "mono/cil/tables.def"
415 #define TABLEDEF(a,b) b,
416 #include "mono/cil/tables.def"
419 static const gint16 tableidx [] = {
420 #define TABLEDEF(a,b) [a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
421 #include "mono/cil/tables.def"
426 #define TABLEDEF(a,b) b,
427 static const char* const
428 mono_tables_names [] = {
429 #include "mono/cil/tables.def"
436 * mono_meta_table_name:
437 * @table: table index
439 * Returns: the name for the @table index
442 mono_meta_table_name (int table)
444 if ((table < 0) || (table > MONO_TABLE_LAST))
447 #ifdef HAVE_ARRAY_ELEM_INIT
448 return (const char*)&tablestr + tableidx [table];
450 return mono_tables_names [table];
454 /* The guy who wrote the spec for this should not be allowed near a
457 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
458 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
459 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
460 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
461 inverse of this mapping.
464 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
465 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
467 /* Reference: Partition II - 23.2.6 */
469 * mono_metadata_compute_size:
470 * @meta: metadata context
471 * @tableindex: metadata table number
472 * @result_bitfield: pointer to guint32 where to store additional info
474 * mono_metadata_compute_size() computes the lenght in bytes of a single
475 * row in a metadata table. The size of each column is encoded in the
476 * @result_bitfield return value along with the number of columns in the table.
477 * the resulting bitfield should be handed to the mono_metadata_table_size()
478 * and mono_metadata_table_count() macros.
479 * This is a Mono runtime internal only function.
482 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
484 guint32 bitfield = 0;
485 int size = 0, field_size = 0;
488 const unsigned char *description = TableSchemas + table_description [tableindex];
490 for (i = 0; (code = description [i]) != MONO_MT_END; i++){
493 field_size = 4; break;
496 field_size = 2; break;
499 field_size = 1; break;
501 case MONO_MT_BLOB_IDX:
502 field_size = meta->idx_blob_wide ? 4 : 2; break;
504 case MONO_MT_STRING_IDX:
505 field_size = meta->idx_string_wide ? 4 : 2; break;
507 case MONO_MT_GUID_IDX:
508 field_size = meta->idx_guid_wide ? 4 : 2; break;
510 case MONO_MT_TABLE_IDX:
511 /* Uhm, a table index can point to other tables besides the current one
512 * so, it's not correct to use the rowcount of the current table to
513 * get the size for this column - lupus
515 switch (tableindex) {
516 case MONO_TABLE_ASSEMBLYREFOS:
518 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
519 case MONO_TABLE_ASSEMBLYPROCESSOR:
521 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
522 case MONO_TABLE_CLASSLAYOUT:
524 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
525 case MONO_TABLE_EVENTMAP:
526 g_assert (i == 0 || i == 1);
527 field_size = i ? idx_size (MONO_TABLE_EVENT):
528 idx_size(MONO_TABLE_TYPEDEF);
530 case MONO_TABLE_EVENT:
532 field_size = MAX (idx_size (MONO_TABLE_TYPEDEF), idx_size(MONO_TABLE_TYPEREF));
533 field_size = MAX (field_size, idx_size(MONO_TABLE_TYPESPEC));
535 case MONO_TABLE_EVENT_POINTER:
537 field_size = idx_size (MONO_TABLE_EVENT); break;
538 case MONO_TABLE_EXPORTEDTYPE:
540 /* the index is in another metadata file, so it must be 4 */
541 field_size = 4; break;
542 case MONO_TABLE_FIELDLAYOUT:
544 field_size = idx_size (MONO_TABLE_FIELD); break;
545 case MONO_TABLE_FIELDRVA:
547 field_size = idx_size (MONO_TABLE_FIELD); break;
548 case MONO_TABLE_FIELD_POINTER:
550 field_size = idx_size (MONO_TABLE_FIELD); break;
551 case MONO_TABLE_IMPLMAP:
553 field_size = idx_size (MONO_TABLE_MODULEREF); break;
554 case MONO_TABLE_INTERFACEIMPL:
556 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
557 case MONO_TABLE_METHOD:
559 field_size = idx_size (MONO_TABLE_PARAM); break;
560 case MONO_TABLE_METHODIMPL:
562 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
563 case MONO_TABLE_METHODSEMANTICS:
565 field_size = idx_size (MONO_TABLE_METHOD); break;
566 case MONO_TABLE_METHOD_POINTER:
568 field_size = idx_size (MONO_TABLE_METHOD); break;
569 case MONO_TABLE_NESTEDCLASS:
570 g_assert (i == 0 || i == 1);
571 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
572 case MONO_TABLE_PARAM_POINTER:
574 field_size = idx_size (MONO_TABLE_PARAM); break;
575 case MONO_TABLE_PROPERTYMAP:
576 g_assert (i == 0 || i == 1);
577 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
578 idx_size(MONO_TABLE_TYPEDEF);
580 case MONO_TABLE_PROPERTY_POINTER:
582 field_size = idx_size (MONO_TABLE_PROPERTY); break;
583 case MONO_TABLE_TYPEDEF:
584 g_assert (i == 4 || i == 5);
585 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
586 idx_size(MONO_TABLE_METHOD);
588 case MONO_TABLE_GENERICPARAM:
589 g_assert (i == 2 || i == 4 || i == 5);
591 field_size = MAX (idx_size (MONO_TABLE_METHOD), idx_size (MONO_TABLE_TYPEDEF));
593 field_size = idx_size (MONO_TABLE_TYPEDEF);
595 field_size = idx_size (MONO_TABLE_TYPEDEF);
598 case MONO_TABLE_GENERICPARAMCONSTRAINT:
600 field_size = idx_size (MONO_TABLE_GENERICPARAM);
604 g_assert_not_reached ();
609 * HasConstant: ParamDef, FieldDef, Property
611 case MONO_MT_CONST_IDX:
612 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
613 meta->tables [MONO_TABLE_FIELD].rows);
614 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
616 /* 2 bits to encode tag */
617 field_size = rtsize (n, 16-2);
621 * HasCustomAttribute: points to any table but
624 case MONO_MT_HASCAT_IDX:
626 * We believe that since the signature and
627 * permission are indexing the Blob heap,
628 * we should consider the blob size first
630 /* I'm not a believer - lupus
631 if (meta->idx_blob_wide){
636 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
637 meta->tables [MONO_TABLE_FIELD].rows);
638 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
639 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
640 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
641 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
642 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
643 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
644 n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
645 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
646 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
647 n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
648 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
649 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
650 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
651 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
652 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
653 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
654 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
656 /* 5 bits to encode */
657 field_size = rtsize (n, 16-5);
661 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
662 * MemberRef and String.
664 case MONO_MT_CAT_IDX:
665 /* String is a heap, if it is wide, we know the size */
667 if (meta->idx_string_wide){
672 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
673 meta->tables [MONO_TABLE_TYPEDEF].rows);
674 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
675 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
677 /* 3 bits to encode */
678 field_size = rtsize (n, 16-3);
682 * HasDeclSecurity: Typedef, MethodDef, Assembly
684 case MONO_MT_HASDEC_IDX:
685 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
686 meta->tables [MONO_TABLE_METHOD].rows);
687 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
689 /* 2 bits to encode */
690 field_size = rtsize (n, 16-2);
694 * Implementation: File, AssemblyRef, ExportedType
696 case MONO_MT_IMPL_IDX:
697 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
698 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
699 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
701 /* 2 bits to encode tag */
702 field_size = rtsize (n, 16-2);
706 * HasFieldMarshall: FieldDef, ParamDef
708 case MONO_MT_HFM_IDX:
709 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
710 meta->tables [MONO_TABLE_PARAM].rows);
712 /* 1 bit used to encode tag */
713 field_size = rtsize (n, 16-1);
717 * MemberForwarded: FieldDef, MethodDef
720 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
721 meta->tables [MONO_TABLE_METHOD].rows);
723 /* 1 bit used to encode tag */
724 field_size = rtsize (n, 16-1);
728 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
730 * It is TypeDef, _TypeRef_, TypeSpec, instead.
732 case MONO_MT_TDOR_IDX:
733 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
734 meta->tables [MONO_TABLE_TYPEREF].rows);
735 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
737 /* 2 bits to encode */
738 field_size = rtsize (n, 16-2);
742 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
744 case MONO_MT_MRP_IDX:
745 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
746 meta->tables [MONO_TABLE_TYPEREF].rows);
747 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
748 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
749 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
750 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
752 /* 3 bits to encode */
753 field_size = rtsize (n, 16 - 3);
757 * MethodDefOrRef: MethodDef, MemberRef
759 case MONO_MT_MDOR_IDX:
760 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
761 meta->tables [MONO_TABLE_MEMBERREF].rows);
763 /* 1 bit used to encode tag */
764 field_size = rtsize (n, 16-1);
768 * HasSemantics: Property, Event
771 n = MAX (meta->tables [MONO_TABLE_PROPERTY].rows,
772 meta->tables [MONO_TABLE_EVENT].rows);
774 /* 1 bit used to encode tag */
775 field_size = rtsize (n, 16-1);
779 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
782 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
783 meta->tables [MONO_TABLE_MODULEREF].rows);
784 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
785 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
787 /* 2 bits used to encode tag (ECMA spec claims 3) */
788 field_size = rtsize (n, 16 - 2);
793 * encode field size as follows (we just need to
800 bitfield |= (field_size-1) << shift;
803 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
806 *result_bitfield = (i << 24) | bitfield;
811 * mono_metadata_compute_table_bases:
812 * @meta: metadata context to compute table values
814 * Computes the table bases for the metadata structure.
815 * This is an internal function used by the image loader code.
818 mono_metadata_compute_table_bases (MonoImage *meta)
821 const char *base = meta->tables_base;
823 for (i = 0; i < MONO_TABLE_NUM; i++) {
824 MonoTableInfo *table = &meta->tables [i];
825 if (table->rows == 0)
828 table->row_size = mono_metadata_compute_size (meta, i, &table->size_bitfield);
830 base += table->rows * table->row_size;
835 * mono_metadata_locate:
836 * @meta: metadata context
837 * @table: table code.
838 * @idx: index of element to retrieve from @table.
840 * Returns: a pointer to the @idx element in the metadata table
841 * whose code is @table.
844 mono_metadata_locate (MonoImage *meta, int table, int idx)
846 /* idx == 0 refers always to NULL */
847 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
849 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
853 * mono_metadata_locate_token:
854 * @meta: metadata context
855 * @token: metadata token
857 * Returns: a pointer to the data in the metadata represented by the
861 mono_metadata_locate_token (MonoImage *meta, guint32 token)
863 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
867 * mono_metadata_string_heap:
868 * @meta: metadata context
869 * @index: index into the string heap.
871 * Returns: an in-memory pointer to the @index in the string heap.
874 mono_metadata_string_heap (MonoImage *meta, guint32 index)
876 g_return_val_if_fail (index < meta->heap_strings.size, "");
877 return meta->heap_strings.data + index;
881 * mono_metadata_user_string:
882 * @meta: metadata context
883 * @index: index into the user string heap.
885 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
888 mono_metadata_user_string (MonoImage *meta, guint32 index)
890 g_return_val_if_fail (index < meta->heap_us.size, "");
891 return meta->heap_us.data + index;
895 * mono_metadata_blob_heap:
896 * @meta: metadata context
897 * @index: index into the blob.
899 * Returns: an in-memory pointer to the @index in the Blob heap.
902 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
904 g_return_val_if_fail (index < meta->heap_blob.size, "");
905 return meta->heap_blob.data + index;
909 * mono_metadata_guid_heap:
910 * @meta: metadata context
911 * @index: index into the guid heap.
913 * Returns: an in-memory pointer to the @index in the guid heap.
916 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
919 index *= 16; /* adjust for guid size and 1-based index */
920 g_return_val_if_fail (index < meta->heap_guid.size, "");
921 return meta->heap_guid.data + index;
924 static const unsigned char *
925 dword_align (const unsigned char *ptr)
927 #if SIZEOF_VOID_P == 8
928 return (const unsigned char *) (((guint64) (ptr + 3)) & ~3);
930 return (const unsigned char *) (((guint32) (ptr + 3)) & ~3);
935 * mono_metadata_decode_row:
936 * @t: table to extract information from.
937 * @idx: index in table.
938 * @res: array of @res_size cols to store the results in
940 * This decompresses the metadata element @idx in table @t
941 * into the guint32 @res array that has res_size elements
944 mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res_size)
946 guint32 bitfield = t->size_bitfield;
947 int i, count = mono_metadata_table_count (bitfield);
948 const char *data = t->base + idx * t->row_size;
950 g_assert (res_size == count);
952 for (i = 0; i < count; i++) {
953 int n = mono_metadata_table_size (bitfield, i);
957 res [i] = *data; break;
959 res [i] = read16 (data); break;
961 res [i] = read32 (data); break;
963 g_assert_not_reached ();
970 * mono_metadata_decode_row_col:
971 * @t: table to extract information from.
972 * @idx: index for row in table.
973 * @col: column in the row.
975 * This function returns the value of column @col from the @idx
976 * row in the table @t.
979 mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col)
981 guint32 bitfield = t->size_bitfield;
983 register const char *data = t->base + idx * t->row_size;
986 g_assert (col < mono_metadata_table_count (bitfield));
988 n = mono_metadata_table_size (bitfield, 0);
989 for (i = 0; i < col; ++i) {
991 n = mono_metadata_table_size (bitfield, i + 1);
997 return read16 (data);
999 return read32 (data);
1001 g_assert_not_reached ();
1007 * mono_metadata_decode_blob_size:
1008 * @ptr: pointer to a blob object
1009 * @rptr: the new position of the pointer
1011 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
1013 * Returns: the size of the blob object
1016 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
1018 const unsigned char *ptr = (const unsigned char *)xptr;
1021 if ((*ptr & 0x80) == 0){
1022 size = ptr [0] & 0x7f;
1024 } else if ((*ptr & 0x40) == 0){
1025 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
1028 size = ((ptr [0] & 0x1f) << 24) +
1040 * mono_metadata_decode_value:
1041 * @ptr: pointer to decode from
1042 * @rptr: the new position of the pointer
1044 * This routine decompresses 32-bit values as specified in the "Blob and
1045 * Signature" section (22.2)
1047 * Returns: the decoded value
1050 mono_metadata_decode_value (const char *_ptr, const char **rptr)
1052 const unsigned char *ptr = (const unsigned char *) _ptr;
1053 unsigned char b = *ptr;
1056 if ((b & 0x80) == 0){
1059 } else if ((b & 0x40) == 0){
1060 len = ((b & 0x3f) << 8 | ptr [1]);
1063 len = ((b & 0x1f) << 24) |
1076 * mono_metadata_decode_signed_value:
1077 * @ptr: pointer to decode from
1078 * @rptr: the new position of the pointer
1080 * This routine decompresses 32-bit signed values
1081 * (not specified in the spec)
1083 * Returns: the decoded value
1086 mono_metadata_decode_signed_value (const char *ptr, const char **rptr)
1088 guint32 uval = mono_metadata_decode_value (ptr, rptr);
1089 gint32 ival = uval >> 1;
1092 /* ival is a truncated 2's complement negative number. */
1094 /* 6 bits = 7 bits for compressed representation (top bit is '0') - 1 sign bit */
1097 /* 13 bits = 14 bits for compressed representation (top bits are '10') - 1 sign bit */
1098 return ival - 0x2000;
1099 if (ival < 0x10000000)
1100 /* 28 bits = 29 bits for compressed representation (top bits are '110') - 1 sign bit */
1101 return ival - 0x10000000;
1102 g_assert (ival < 0x20000000);
1103 g_warning ("compressed signed value appears to use 29 bits for compressed representation: %x (raw: %8x)", ival, uval);
1104 return ival - 0x20000000;
1108 * Translates the given 1-based index into the Method, Field, Event, or Param tables
1109 * using the *Ptr tables in uncompressed metadata, if they are available.
1111 * FIXME: The caller is not forced to call this function, which is error-prone, since
1112 * forgetting to call it would only show up as a bug on uncompressed metadata.
1115 mono_metadata_translate_token_index (MonoImage *image, int table, guint32 idx)
1117 if (!image->uncompressed_metadata)
1121 case MONO_TABLE_METHOD:
1122 if (image->tables [MONO_TABLE_METHOD_POINTER].rows)
1123 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_METHOD_POINTER], idx - 1, MONO_METHOD_POINTER_METHOD);
1126 case MONO_TABLE_FIELD:
1127 if (image->tables [MONO_TABLE_FIELD_POINTER].rows)
1128 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_FIELD_POINTER], idx - 1, MONO_FIELD_POINTER_FIELD);
1131 case MONO_TABLE_EVENT:
1132 if (image->tables [MONO_TABLE_EVENT_POINTER].rows)
1133 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_EVENT_POINTER], idx - 1, MONO_EVENT_POINTER_EVENT);
1136 case MONO_TABLE_PROPERTY:
1137 if (image->tables [MONO_TABLE_PROPERTY_POINTER].rows)
1138 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_PROPERTY_POINTER], idx - 1, MONO_PROPERTY_POINTER_PROPERTY);
1141 case MONO_TABLE_PARAM:
1142 if (image->tables [MONO_TABLE_PARAM_POINTER].rows)
1143 return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_PARAM_POINTER], idx - 1, MONO_PARAM_POINTER_PARAM);
1152 * mono_metadata_decode_table_row:
1154 * Same as mono_metadata_decode_row, but takes an IMAGE+TABLE ID pair, and takes
1155 * uncompressed metadata into account, so it should be used to access the
1156 * Method, Field, Param and Event tables when the access is made from metadata, i.e.
1157 * IDX is retrieved from a metadata table, like MONO_TYPEDEF_FIELD_LIST.
1160 mono_metadata_decode_table_row (MonoImage *image, int table, int idx, guint32 *res, int res_size)
1162 if (image->uncompressed_metadata)
1163 idx = mono_metadata_translate_token_index (image, table, idx + 1) - 1;
1165 mono_metadata_decode_row (&image->tables [table], idx, res, res_size);
1169 * mono_metadata_decode_table_row_col:
1171 * Same as mono_metadata_decode_row_col, but takes an IMAGE+TABLE ID pair, and takes
1172 * uncompressed metadata into account, so it should be used to access the
1173 * Method, Field, Param and Event tables.
1175 guint32 mono_metadata_decode_table_row_col (MonoImage *image, int table, int idx, guint col)
1177 if (image->uncompressed_metadata)
1178 idx = mono_metadata_translate_token_index (image, table, idx + 1) - 1;
1180 return mono_metadata_decode_row_col (&image->tables [table], idx, col);
1184 * mono_metadata_parse_typedef_or_ref:
1185 * @m: a metadata context.
1186 * @ptr: a pointer to an encoded TypedefOrRef in @m
1187 * @rptr: pointer updated to match the end of the decoded stream
1189 * Returns: a token valid in the @m metadata decoded from
1190 * the compressed representation.
1193 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1196 token = mono_metadata_decode_value (ptr, &ptr);
1199 return mono_metadata_token_from_dor (token);
1203 * mono_metadata_parse_custom_mod:
1204 * @m: a metadata context.
1205 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1206 * @ptr: a pointer to (possibly) the start of a custom modifier list
1207 * @rptr: pointer updated to match the end of the decoded stream
1209 * Checks if @ptr points to a type custom modifier compressed representation.
1211 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1214 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1216 MonoCustomMod local;
1217 if ((*ptr == MONO_TYPE_CMOD_OPT) || (*ptr == MONO_TYPE_CMOD_REQD)) {
1220 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1221 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1228 * mono_metadata_parse_array:
1229 * @m: a metadata context.
1230 * @ptr: a pointer to an encoded array description.
1231 * @rptr: pointer updated to match the end of the decoded stream
1233 * Decodes the compressed array description found in the metadata @m at @ptr.
1235 * Returns: a #MonoArrayType structure describing the array type
1239 mono_metadata_parse_array_full (MonoImage *m, MonoGenericContainer *container,
1240 const char *ptr, const char **rptr)
1243 MonoArrayType *array = g_new0 (MonoArrayType, 1);
1246 etype = mono_metadata_parse_type_full (m, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
1249 array->eklass = mono_class_from_mono_type (etype);
1250 array->rank = mono_metadata_decode_value (ptr, &ptr);
1252 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1253 if (array->numsizes)
1254 array->sizes = g_new0 (int, array->numsizes);
1255 for (i = 0; i < array->numsizes; ++i)
1256 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1258 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1259 if (array->numlobounds)
1260 array->lobounds = g_new0 (int, array->numlobounds);
1261 for (i = 0; i < array->numlobounds; ++i)
1262 array->lobounds [i] = mono_metadata_decode_signed_value (ptr, &ptr);
1270 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1272 return mono_metadata_parse_array_full (m, NULL, ptr, rptr);
1276 * mono_metadata_free_array:
1277 * @array: array description
1279 * Frees the array description returned from mono_metadata_parse_array().
1282 mono_metadata_free_array (MonoArrayType *array)
1284 g_free (array->sizes);
1285 g_free (array->lobounds);
1290 * need to add common field and param attributes combinations:
1293 * public static literal
1296 * private static literal
1298 static const MonoType
1300 /* data, attrs, type, nmods, byref, pinned */
1301 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1302 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1303 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1304 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1305 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1306 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1307 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1308 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1309 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1310 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1311 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1312 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1313 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1314 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1315 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1316 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1317 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1318 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1319 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1320 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1321 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1322 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1323 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1324 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1325 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1326 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1327 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1328 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 0, 0},
1329 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 1, 0},
1330 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1331 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1332 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1333 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1334 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1337 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1339 static GHashTable *type_cache = NULL;
1340 static GHashTable *generic_inst_cache = NULL;
1341 static GHashTable *generic_class_cache = NULL;
1342 static int next_generic_inst_id = 0;
1345 * Protected by the loader lock.
1346 * It has a MonoMethodInflated* as key and value.
1347 * The key lookup will just access the declaring and context fields
1349 static GHashTable *generic_method_cache = NULL;
1351 static guint mono_generic_class_hash (gconstpointer data);
1354 * MonoTypes with modifies are never cached, so we never check or use that field.
1357 mono_type_hash (gconstpointer data)
1359 const MonoType *type = (const MonoType *) data;
1360 if (type->type == MONO_TYPE_GENERICINST)
1361 return mono_generic_class_hash (type->data.generic_class);
1363 return type->type | (type->byref << 8) | (type->attrs << 9);
1367 mono_type_equal (gconstpointer ka, gconstpointer kb)
1369 const MonoType *a = (const MonoType *) ka;
1370 const MonoType *b = (const MonoType *) kb;
1372 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1374 /* need other checks */
1379 mono_generic_inst_hash (gconstpointer data)
1381 const MonoGenericInst *ginst = (const MonoGenericInst *) data;
1385 for (i = 0; i < ginst->type_argc; ++i) {
1387 hash += mono_metadata_type_hash (ginst->type_argv [i]);
1390 return hash ^ (ginst->is_open << 8);
1394 mono_generic_inst_equal (gconstpointer ka, gconstpointer kb)
1396 const MonoGenericInst *a = (const MonoGenericInst *) ka;
1397 const MonoGenericInst *b = (const MonoGenericInst *) kb;
1400 if (a->is_open != b->is_open || a->type_argc != b->type_argc)
1402 for (i = 0; i < a->type_argc; ++i) {
1403 if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], FALSE))
1410 mono_generic_class_hash (gconstpointer data)
1412 const MonoGenericClass *gclass = (const MonoGenericClass *) data;
1413 guint hash = mono_metadata_type_hash (&gclass->container_class->byval_arg);
1416 hash += mono_metadata_generic_context_hash (&gclass->context);
1422 mono_generic_class_equal (gconstpointer ka, gconstpointer kb)
1424 const MonoGenericClass *a = (const MonoGenericClass *) ka;
1425 const MonoGenericClass *b = (const MonoGenericClass *) kb;
1427 return _mono_metadata_generic_class_equal (a, b, FALSE);
1431 * mono_metadata_init:
1433 * Initialize the global variables of this module.
1434 * This is a Mono runtime internal function.
1437 mono_metadata_init (void)
1441 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1442 generic_inst_cache = g_hash_table_new (mono_generic_inst_hash, mono_generic_inst_equal);
1443 generic_class_cache = g_hash_table_new (mono_generic_class_hash, mono_generic_class_equal);
1445 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1446 g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
1450 * mono_metadata_cleanup:
1452 * Free all resources used by this module.
1453 * This is a Mono runtime internal function.
1456 mono_metadata_cleanup (void)
1458 g_hash_table_destroy (type_cache);
1459 g_hash_table_destroy (generic_inst_cache);
1460 g_hash_table_destroy (generic_class_cache);
1461 if (generic_method_cache)
1462 g_hash_table_destroy (generic_method_cache);
1464 generic_inst_cache = NULL;
1465 generic_class_cache = NULL;
1466 generic_method_cache = NULL;
1470 * mono_metadata_parse_type:
1471 * @m: metadata context
1472 * @mode: king of type that may be found at @ptr
1473 * @opt_attrs: optional attributes to store in the returned type
1474 * @ptr: pointer to the type representation
1475 * @rptr: pointer updated to match the end of the decoded stream
1477 * Decode a compressed type description found at @ptr in @m.
1478 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1479 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1480 * This function can be used to decode type descriptions in method signatures,
1481 * field signatures, locals signatures etc.
1483 * To parse a generic type, `generic_container' points to the current class'es
1484 * (the `generic_container' field in the MonoClass) or the current generic method's
1485 * (the `generic_container' field in the MonoMethodNormal) generic container.
1486 * When we encounter any MONO_TYPE_VAR or MONO_TYPE_MVAR's, they're looked up in
1487 * this MonoGenericContainer.
1488 * This is a Mono runtime internal function.
1490 * LOCKING: Assumes the loader lock is held.
1492 * Returns: a #MonoType structure representing the decoded type.
1495 mono_metadata_parse_type_full (MonoImage *m, MonoGenericContainer *container, MonoParseTypeMode mode,
1496 short opt_attrs, const char *ptr, const char **rptr)
1498 MonoType *type, *cached;
1500 gboolean byref = FALSE;
1501 gboolean pinned = FALSE;
1502 const char *tmp_ptr;
1506 mono_loader_lock ();
1508 * According to the spec, custom modifiers should come before the byref
1509 * flag, but the IL produced by ilasm from the following signature:
1510 * object modopt(...) &
1511 * starts with a byref flag, followed by the modifiers. (bug #49802)
1512 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1513 * it would be better to treat byref as real type constructor instead of
1515 * Also, pinned should come before anything else, but some MSV++ produced
1516 * assemblies violate this (#bug 61990).
1519 /* Count the modifiers first */
1524 case MONO_TYPE_PINNED:
1525 case MONO_TYPE_BYREF:
1528 case MONO_TYPE_CMOD_REQD:
1529 case MONO_TYPE_CMOD_OPT:
1531 mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1539 type = mono_mempool_alloc0 (m->mempool, sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1540 type->num_mods = count;
1542 g_warning ("got more than 64 modifiers in type");
1545 memset (type, 0, sizeof (MonoType));
1548 /* Parse pinned, byref and custom modifiers */
1553 case MONO_TYPE_PINNED:
1557 case MONO_TYPE_BYREF:
1561 case MONO_TYPE_CMOD_REQD:
1562 case MONO_TYPE_CMOD_OPT:
1563 mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1571 type->attrs = opt_attrs;
1572 type->byref = byref;
1573 type->pinned = pinned ? 1 : 0;
1575 if (!do_mono_metadata_parse_type (type, m, container, ptr, &ptr)) {
1576 mono_loader_unlock ();
1583 if (!type->num_mods) {
1584 /* no need to free type here, because it is on the stack */
1585 if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
1586 MonoType *ret = type->byref ? &type->data.klass->this_arg : &type->data.klass->byval_arg;
1588 /* Consider the case:
1590 class Foo<T> { class Bar {} }
1591 class Test : Foo<Test>.Bar {}
1593 When Foo<Test> is being expanded, 'Test' isn't yet initialized. It's actually in
1594 a really pristine state: it doesn't even know whether 'Test' is a reference or a value type.
1596 We ensure that the MonoClass is in a state that we can canonicalize to:
1598 klass->byval_arg.data.klass == klass
1599 klass->this_arg.data.klass == klass
1601 If we can't canonicalize 'type', it doesn't matter, since later users of 'type' will do it.
1603 LOCKING: even though we don't explicitly hold a lock, in the problematic case 'ret' is a field
1604 of a MonoClass which currently holds the loader lock. 'type' is local.
1606 if (ret->data.klass == type->data.klass) {
1607 mono_loader_unlock ();
1611 /* No need to use locking since nobody is modifying the hash table */
1612 if ((cached = g_hash_table_lookup (type_cache, type))) {
1613 mono_loader_unlock ();
1618 /* printf ("%x %x %c %s\n", type->attrs, type->num_mods, type->pinned ? 'p' : ' ', mono_type_full_name (type)); */
1620 if (type == &stype) {
1621 type = mono_mempool_alloc (m->mempool, sizeof (MonoType));
1622 memcpy (type, &stype, sizeof (MonoType));
1624 mono_loader_unlock ();
1629 * LOCKING: Assumes the loader lock is held.
1632 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
1633 const char *ptr, const char **rptr)
1635 return mono_metadata_parse_type_full (m, NULL, mode, opt_attrs, ptr, rptr);
1639 * mono_metadata_get_param_attrs:
1641 * Return the parameter attributes for the method whose MethodDef index is DEF. The
1642 * returned memory needs to be freed by the caller. If all the param attributes are
1643 * 0, then NULL is returned.
1646 mono_metadata_get_param_attrs (MonoImage *m, int def)
1648 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1649 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1650 guint32 cols [MONO_PARAM_SIZE];
1651 guint lastp, i, param_index = mono_metadata_decode_row_col (&m->tables [MONO_TABLE_METHOD], def - 1, MONO_METHOD_PARAMLIST);
1654 if (def < methodt->rows)
1655 lastp = mono_metadata_decode_row_col (&m->tables [MONO_TABLE_METHOD], def, MONO_METHOD_PARAMLIST);
1657 lastp = paramt->rows + 1;
1659 for (i = param_index; i < lastp; ++i) {
1660 mono_metadata_decode_row (&m->tables [MONO_TABLE_PARAM], i - 1, cols, MONO_PARAM_SIZE);
1661 if (cols [MONO_PARAM_FLAGS]) {
1663 pattrs = g_new0 (int, 1 + (lastp - param_index));
1664 pattrs [cols [MONO_PARAM_SEQUENCE]] = cols [MONO_PARAM_FLAGS];
1672 * mono_metadata_parse_signature_full:
1673 * @image: metadata context
1674 * @generic_container: generic container
1675 * @toke: metadata token
1677 * Decode a method signature stored in the STANDALONESIG table
1679 * LOCKING: Assumes the loader lock is held.
1681 * Returns: a MonoMethodSignature describing the signature.
1683 MonoMethodSignature*
1684 mono_metadata_parse_signature_full (MonoImage *image, MonoGenericContainer *generic_container, guint32 token)
1686 MonoTableInfo *tables = image->tables;
1687 guint32 idx = mono_metadata_token_index (token);
1692 return mono_lookup_dynamic_token (image, token);
1694 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1696 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1698 ptr = mono_metadata_blob_heap (image, sig);
1699 mono_metadata_decode_blob_size (ptr, &ptr);
1701 return mono_metadata_parse_method_signature_full (image, generic_container, 0, ptr, NULL);
1705 * mono_metadata_parse_signature:
1706 * @image: metadata context
1707 * @toke: metadata token
1709 * Decode a method signature stored in the STANDALONESIG table
1711 * Returns: a MonoMethodSignature describing the signature.
1713 MonoMethodSignature*
1714 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1716 return mono_metadata_parse_signature_full (image, NULL, token);
1720 * mono_metadata_signature_alloc:
1721 * @image: metadata context
1722 * @nparmas: number of parameters in the signature
1724 * Allocate a MonoMethodSignature structure with the specified number of params.
1725 * The return type and the params types need to be filled later.
1726 * This is a Mono runtime internal function.
1728 * LOCKING: Assumes the loader lock is held.
1730 * Returns: the new MonoMethodSignature structure.
1732 MonoMethodSignature*
1733 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1735 MonoMethodSignature *sig;
1737 mono_loader_lock ();
1738 sig = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1739 sig->param_count = nparams;
1740 sig->sentinelpos = -1;
1741 mono_loader_unlock ();
1747 * mono_metadata_signature_dup:
1748 * @sig: method signature
1750 * Duplicate an existing MonoMethodSignature so it can be modified.
1751 * This is a Mono runtime internal function.
1753 * Returns: the new MonoMethodSignature structure.
1755 MonoMethodSignature*
1756 mono_metadata_signature_dup (MonoMethodSignature *sig)
1760 sigsize = sizeof (MonoMethodSignature) + (sig->param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType *);
1761 return g_memdup (sig, sigsize);
1765 * mono_metadata_parse_method_signature:
1766 * @m: metadata context
1767 * @generic_container: generics container
1768 * @def: the MethodDef index or 0 for Ref signatures.
1769 * @ptr: pointer to the signature metadata representation
1770 * @rptr: pointer updated to match the end of the decoded stream
1772 * Decode a method signature stored at @ptr.
1773 * This is a Mono runtime internal function.
1775 * LOCKING: Assumes the loader lock is held.
1777 * Returns: a MonoMethodSignature describing the signature.
1779 MonoMethodSignature *
1780 mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContainer *container,
1781 int def, const char *ptr, const char **rptr)
1783 MonoMethodSignature *method;
1784 int i, *pattrs = NULL;
1785 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1786 guint32 gen_param_count = 0;
1787 gboolean is_open = FALSE;
1790 gen_param_count = 1;
1795 call_convention = *ptr & 0x0F;
1797 if (gen_param_count)
1798 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1799 param_count = mono_metadata_decode_value (ptr, &ptr);
1802 pattrs = mono_metadata_get_param_attrs (m, def);
1803 method = mono_metadata_signature_alloc (m, param_count);
1804 method->hasthis = hasthis;
1805 method->explicit_this = explicit_this;
1806 method->call_convention = call_convention;
1807 method->generic_param_count = gen_param_count;
1809 if (call_convention != 0xa) {
1810 method->ret = mono_metadata_parse_type_full (m, container, MONO_PARSE_RET, pattrs ? pattrs [0] : 0, ptr, &ptr);
1812 mono_metadata_free_method_signature (method);
1816 is_open = mono_class_is_open_constructed_type (method->ret);
1819 for (i = 0; i < method->param_count; ++i) {
1820 if (*ptr == MONO_TYPE_SENTINEL) {
1821 if (method->call_convention != MONO_CALL_VARARG || def)
1822 g_error ("found sentinel for methoddef or no vararg method");
1823 if (method->sentinelpos >= 0)
1824 g_error ("found sentinel twice in the same signature");
1825 method->sentinelpos = i;
1828 method->params [i] = mono_metadata_parse_type_full (m, container, MONO_PARSE_PARAM, pattrs ? pattrs [i+1] : 0, ptr, &ptr);
1829 if (!method->params [i]) {
1830 mono_metadata_free_method_signature (method);
1835 is_open = mono_class_is_open_constructed_type (method->params [i]);
1838 /* The sentinel could be missing if the caller does not pass any additional arguments */
1839 if (!def && method->call_convention == MONO_CALL_VARARG && method->sentinelpos < 0)
1840 method->sentinelpos = method->param_count;
1842 method->has_type_parameters = is_open;
1844 if (def && (method->call_convention == MONO_CALL_VARARG))
1845 method->sentinelpos = method->param_count;
1852 * Add signature to a cache and increase ref count...
1859 * mono_metadata_parse_method_signature:
1860 * @m: metadata context
1861 * @def: the MethodDef index or 0 for Ref signatures.
1862 * @ptr: pointer to the signature metadata representation
1863 * @rptr: pointer updated to match the end of the decoded stream
1865 * Decode a method signature stored at @ptr.
1866 * This is a Mono runtime internal function.
1868 * LOCKING: Assumes the loader lock is held.
1870 * Returns: a MonoMethodSignature describing the signature.
1872 MonoMethodSignature *
1873 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1875 return mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr);
1879 * mono_metadata_free_method_signature:
1880 * @sig: signature to destroy
1882 * Free the memory allocated in the signature @sig.
1883 * This method needs to be robust and work also on partially-built
1884 * signatures, so it does extra checks.
1887 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1891 mono_metadata_free_type (sig->ret);
1892 for (i = 0; i < sig->param_count; ++i) {
1893 if (sig->params [i])
1894 mono_metadata_free_type (sig->params [i]);
1899 inflated_method_equal (gconstpointer a, gconstpointer b)
1901 const MonoMethodInflated *ma = a;
1902 const MonoMethodInflated *mb = b;
1903 if (ma->declaring != mb->declaring)
1905 return mono_metadata_generic_context_equal (&ma->context, &mb->context);
1909 inflated_method_hash (gconstpointer a)
1911 const MonoMethodInflated *ma = a;
1912 return mono_metadata_generic_context_hash (&ma->context) ^ mono_aligned_addr_hash (ma->declaring);
1916 dump_ginst (MonoGenericInst *ginst)
1921 g_print ("Ginst: <");
1922 for (i = 0; i < ginst->type_argc; ++i) {
1925 name = mono_type_get_name (ginst->type_argv [i]);
1926 g_print ("%s", name);
1932 static gboolean gclass_in_image (gpointer key, gpointer value, gpointer data);
1935 ginst_in_image (gpointer key, gpointer value, gpointer data)
1937 MonoImage *image = data;
1938 MonoGenericInst *ginst = key;
1941 for (i = 0; i < ginst->type_argc; ++i) {
1942 MonoType *type = ginst->type_argv [i];
1944 /* FIXME: Avoid a possible mono_class_inst inside mono_class_from_mono_type */
1945 if (type->type == MONO_TYPE_GENERICINST) {
1946 if (gclass_in_image (type->data.generic_class, NULL, image))
1951 klass = mono_class_from_mono_type (ginst->type_argv [i]);
1952 if (klass->image == image) {
1953 /*dump_ginst (ginst);
1954 g_print (" removed\n");*/
1962 gclass_in_image (gpointer key, gpointer value, gpointer data)
1964 MonoImage *image = data;
1965 MonoGenericClass *gclass = key;
1967 if (ginst_in_image (gclass->context.class_inst, NULL, image))
1969 if (gclass->container_class->image == image)
1975 inflated_method_in_image (gpointer key, gpointer value, gpointer data)
1977 MonoImage *image = data;
1978 MonoMethodInflated *method = key;
1980 if (method->declaring->klass->image == image)
1982 if (method->context.class_inst && ginst_in_image (method->context.class_inst, NULL, image))
1984 if (method->context.method_inst && ginst_in_image (method->context.method_inst, NULL, image))
1990 * LOCKING: assumes the loader lock is held.
1993 mono_method_inflated_lookup (MonoMethodInflated* method, gboolean cache)
1996 if (!generic_method_cache)
1997 generic_method_cache = g_hash_table_new (inflated_method_hash, inflated_method_equal);
1998 g_hash_table_insert (generic_method_cache, method, method);
2001 if (generic_method_cache)
2002 return g_hash_table_lookup (generic_method_cache, method);
2008 mono_metadata_clean_for_image (MonoImage *image)
2010 mono_loader_lock ();
2011 g_hash_table_foreach_remove (generic_inst_cache, ginst_in_image, image);
2012 g_hash_table_foreach_remove (generic_class_cache, gclass_in_image, image);
2013 if (generic_method_cache)
2014 g_hash_table_foreach_remove (generic_method_cache, inflated_method_in_image, image);
2015 mono_loader_unlock ();
2019 * mono_metadata_get_generic_inst:
2021 * Given a list of types, return a MonoGenericInst that represents that list.
2022 * The returned MonoGenericInst has its own copy of the list of types. The list
2023 * passed in the argument can be freed, modified or disposed of.
2027 mono_metadata_get_generic_inst (int type_argc, MonoType **type_argv)
2029 MonoGenericInst *ginst;
2030 MonoGenericInst helper;
2033 helper.type_argc = type_argc;
2034 helper.type_argv = type_argv;
2037 for (i = 0; i < type_argc; ++i)
2038 if (mono_class_is_open_constructed_type (type_argv [i]))
2040 helper.is_open = (i < type_argc);
2042 /*dump_ginst (&helper);*/
2043 mono_loader_lock ();
2044 ginst = g_hash_table_lookup (generic_inst_cache, &helper);
2046 mono_loader_unlock ();
2047 /*g_print (" found cached\n");*/
2051 ginst = g_new0 (MonoGenericInst, 1);
2052 ginst->type_argc = type_argc;
2053 ginst->type_argv = g_new (MonoType*, type_argc);
2054 ginst->id = ++next_generic_inst_id;
2055 ginst->is_open = helper.is_open;
2057 for (i = 0; i < type_argc; ++i)
2058 ginst->type_argv [i] = mono_metadata_type_dup (NULL, type_argv [i]);
2060 g_hash_table_insert (generic_inst_cache, ginst, ginst);
2062 mono_loader_unlock ();
2063 /*g_print (" inserted\n");*/
2068 * mono_metadata_lookup_generic_class:
2070 * Returns a MonoGenericClass with the given properties.
2074 mono_metadata_lookup_generic_class (MonoClass *container_class, MonoGenericInst *inst, gboolean is_dynamic)
2076 MonoGenericClass *gclass;
2078 MonoGenericClass helper;
2079 helper.container_class = container_class;
2080 helper.context.class_inst = inst;
2081 helper.context.method_inst = NULL;
2082 helper.is_dynamic = is_dynamic; /* We use this in a hash lookup, which does not attempt to downcast the pointer */
2083 helper.cached_class = NULL;
2085 mono_loader_lock ();
2087 gclass = g_hash_table_lookup (generic_class_cache, &helper);
2089 /* A tripwire just to keep us honest */
2090 g_assert (!helper.cached_class);
2093 mono_loader_unlock ();
2098 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
2099 gclass = &dgclass->generic_class;
2100 gclass->is_dynamic = 1;
2102 gclass = g_new0 (MonoGenericClass, 1);
2105 gclass->container_class = container_class;
2106 gclass->context.class_inst = inst;
2107 gclass->context.method_inst = NULL;
2109 g_hash_table_insert (generic_class_cache, gclass, gclass);
2111 mono_loader_unlock ();
2117 * mono_metadata_inflate_generic_inst:
2119 * Instantiate the generic instance @ginst with the context @context.
2123 mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context)
2125 MonoType **type_argv;
2126 MonoGenericInst *nginst;
2129 if (!ginst->is_open)
2132 type_argv = g_new0 (MonoType*, ginst->type_argc);
2134 for (i = 0; i < ginst->type_argc; i++)
2135 type_argv [i] = mono_class_inflate_generic_type (ginst->type_argv [i], context);
2137 nginst = mono_metadata_get_generic_inst (ginst->type_argc, type_argv);
2145 mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContainer *container,
2146 int count, const char *ptr, const char **rptr)
2148 MonoType **type_argv;
2149 MonoGenericInst *ginst;
2152 type_argv = g_new0 (MonoType*, count);
2154 for (i = 0; i < count; i++) {
2155 MonoType *t = mono_metadata_parse_type_full (m, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
2166 ginst = mono_metadata_get_generic_inst (count, type_argv);
2174 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContainer *container,
2175 const char *ptr, const char **rptr)
2177 MonoGenericInst *inst;
2182 gtype = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
2186 gklass = mono_class_from_mono_type (gtype);
2187 if (!gklass->generic_container)
2190 count = mono_metadata_decode_value (ptr, &ptr);
2191 inst = mono_metadata_parse_generic_inst (m, container, count, ptr, &ptr);
2198 type->data.generic_class = mono_metadata_lookup_generic_class (gklass, inst, FALSE);
2204 * @gc: The generic container to normalize
2205 * @type: The kind of generic parameters the resulting generic-container should contain
2208 static MonoGenericContainer *
2209 select_container (MonoGenericContainer *gc, MonoTypeEnum type)
2211 gboolean is_var = (type == MONO_TYPE_VAR);
2215 g_assert (is_var || type == MONO_TYPE_MVAR);
2218 if (gc->is_method || gc->parent)
2220 * The current MonoGenericContainer is a generic method -> its `parent'
2221 * points to the containing class'es container.
2230 * mono_metadata_parse_generic_param:
2231 * @generic_container: Our MonoClass's or MonoMethodNormal's MonoGenericContainer;
2232 * see mono_metadata_parse_type_full() for details.
2233 * Internal routine to parse a generic type parameter.
2235 static MonoGenericParam *
2236 mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContainer *generic_container,
2237 MonoTypeEnum type, const char *ptr, const char **rptr)
2239 int index = mono_metadata_decode_value (ptr, &ptr);
2243 generic_container = select_container (generic_container, type);
2244 if (!generic_container) {
2245 /* Create dummy MonoGenericParam */
2246 MonoGenericParam *param = g_new0 (MonoGenericParam, 1);
2247 param->name = g_strdup_printf ("%d", index);
2253 g_assert (index < generic_container->type_argc);
2254 return &generic_container->type_params [index];
2258 * do_mono_metadata_parse_type:
2259 * @type: MonoType to be filled in with the return value
2261 * @generic_context: generics_context
2262 * @ptr: pointer to the encoded type
2263 * @rptr: pointer where the end of the encoded type is saved
2265 * Internal routine used to "fill" the contents of @type from an
2266 * allocated pointer. This is done this way to avoid doing too
2267 * many mini-allocations (particularly for the MonoFieldType which
2268 * most of the time is just a MonoType, but sometimes might be augmented).
2270 * This routine is used by mono_metadata_parse_type and
2271 * mono_metadata_parse_field_type
2273 * This extracts a Type as specified in Partition II (22.2.12)
2275 * Returns: FALSE if the type could not be loaded
2278 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container,
2279 const char *ptr, const char **rptr)
2282 type->type = mono_metadata_decode_value (ptr, &ptr);
2284 switch (type->type){
2285 case MONO_TYPE_VOID:
2286 case MONO_TYPE_BOOLEAN:
2287 case MONO_TYPE_CHAR:
2300 case MONO_TYPE_STRING:
2301 case MONO_TYPE_OBJECT:
2302 case MONO_TYPE_TYPEDBYREF:
2304 case MONO_TYPE_VALUETYPE:
2305 case MONO_TYPE_CLASS: {
2307 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
2308 type->data.klass = mono_class_get (m, token);
2309 if (!type->data.klass)
2313 case MONO_TYPE_SZARRAY: {
2314 MonoType *etype = mono_metadata_parse_type_full (m, container, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
2317 type->data.klass = mono_class_from_mono_type (etype);
2318 mono_metadata_free_type (etype);
2322 type->data.type = mono_metadata_parse_type_full (m, container, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
2323 if (!type->data.type)
2326 case MONO_TYPE_FNPTR:
2327 type->data.method = mono_metadata_parse_method_signature_full (m, container, 0, ptr, &ptr);
2329 case MONO_TYPE_ARRAY:
2330 type->data.array = mono_metadata_parse_array_full (m, container, ptr, &ptr);
2332 case MONO_TYPE_MVAR:
2334 type->data.generic_param = mono_metadata_parse_generic_param (m, container, type->type, ptr, &ptr);
2336 case MONO_TYPE_GENERICINST:
2337 ok = do_mono_metadata_parse_generic_class (type, m, container, ptr, &ptr);
2340 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
2349 * mono_metadata_free_type:
2350 * @type: type to free
2352 * Free the memory allocated for type @type which is assumed to be created by
2353 * mono_metadata_parse_type ().
2356 mono_metadata_free_type (MonoType *type)
2358 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
2361 switch (type->type){
2362 case MONO_TYPE_OBJECT:
2363 case MONO_TYPE_STRING:
2364 if (!type->data.klass)
2367 case MONO_TYPE_CLASS:
2368 case MONO_TYPE_VALUETYPE:
2369 if (type == &type->data.klass->byval_arg || type == &type->data.klass->this_arg)
2373 mono_metadata_free_type (type->data.type);
2375 case MONO_TYPE_FNPTR:
2376 mono_metadata_free_method_signature (type->data.method);
2378 case MONO_TYPE_ARRAY:
2379 mono_metadata_free_array (type->data.array);
2383 /* Allocated from a mempool, no need to free it */
2388 hex_dump (const char *buffer, int base, int count)
2390 int show_header = 1;
2398 for (i = 0; i < count; i++){
2401 printf ("\n0x%08x: ", (unsigned char) base + i);
2403 printf ("%02x ", (unsigned char) (buffer [i]));
2410 * @mh: The Method header
2411 * @ptr: Points to the beginning of the Section Data (25.3)
2414 parse_section_data (MonoImage *m, MonoMethodHeader *mh, const unsigned char *ptr)
2416 unsigned char sect_data_flags;
2417 const unsigned char *sptr;
2419 guint32 sect_data_len;
2422 /* align on 32-bit boundary */
2423 sptr = ptr = dword_align (ptr);
2424 sect_data_flags = *ptr;
2427 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
2429 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
2432 sect_data_len = ptr [0];
2436 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
2437 hex_dump (sptr, 0, sect_data_len+8);
2438 g_print ("\nheader: ");
2439 hex_dump (sptr-4, 0, 4);
2443 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
2444 const unsigned char *p = dword_align (ptr);
2446 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
2447 /* we could just store a pointer if we don't need to byteswap */
2448 mh->clauses = mono_mempool_alloc0 (m->mempool, sizeof (MonoExceptionClause) * mh->num_clauses);
2449 for (i = 0; i < mh->num_clauses; ++i) {
2450 MonoExceptionClause *ec = &mh->clauses [i];
2453 ec->flags = read32 (p);
2454 ec->try_offset = read32 (p + 4);
2455 ec->try_len = read32 (p + 8);
2456 ec->handler_offset = read32 (p + 12);
2457 ec->handler_len = read32 (p + 16);
2458 tof_value = read32 (p + 20);
2461 ec->flags = read16 (p);
2462 ec->try_offset = read16 (p + 2);
2463 ec->try_len = *(p + 4);
2464 ec->handler_offset = read16 (p + 5);
2465 ec->handler_len = *(p + 7);
2466 tof_value = read32 (p + 8);
2469 if (ec->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2470 ec->data.filter_offset = tof_value;
2471 } else if (ec->flags == MONO_EXCEPTION_CLAUSE_NONE) {
2472 ec->data.catch_class = tof_value? mono_class_get (m, tof_value): 0;
2474 ec->data.catch_class = NULL;
2476 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
2480 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
2481 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
2488 * mono_metadata_parse_mh_full:
2489 * @m: metadata context
2490 * @generic_context: generics context
2491 * @ptr: pointer to the method header.
2493 * Decode the method header at @ptr, including pointer to the IL code,
2494 * info about local variables and optional exception tables.
2495 * This is a Mono runtime internal function.
2497 * LOCKING: Assumes the loader lock is held.
2499 * Returns: a MonoMethodHeader allocated from the image mempool.
2502 mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, const char *ptr)
2504 MonoMethodHeader *mh;
2505 unsigned char flags = *(const unsigned char *) ptr;
2506 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
2508 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
2509 const unsigned char *code;
2512 g_return_val_if_fail (ptr != NULL, NULL);
2514 mono_loader_lock ();
2516 case METHOD_HEADER_TINY_FORMAT:
2517 mh = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodHeader));
2520 local_var_sig_tok = 0;
2521 mh->code_size = flags >> 2;
2522 mh->code = (unsigned char*)ptr;
2523 mono_loader_unlock ();
2525 case METHOD_HEADER_TINY_FORMAT1:
2526 mh = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodHeader));
2529 local_var_sig_tok = 0;
2532 * The spec claims 3 bits, but the Beta2 is
2535 mh->code_size = flags >> 2;
2536 mh->code = (unsigned char*)ptr;
2537 mono_loader_unlock ();
2539 case METHOD_HEADER_FAT_FORMAT:
2540 fat_flags = read16 (ptr);
2542 hsize = (fat_flags >> 12) & 0xf;
2543 max_stack = read16 (ptr);
2545 code_size = read32 (ptr);
2547 local_var_sig_tok = read32 (ptr);
2550 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
2555 code = (unsigned char*)ptr;
2557 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
2561 * There are more sections
2563 ptr = (char*)code + code_size;
2566 mono_loader_unlock ();
2570 if (local_var_sig_tok) {
2571 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
2572 const char *locals_ptr;
2573 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
2574 int len=0, i, bsize;
2576 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
2577 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
2578 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
2579 if (*locals_ptr != 0x07)
2580 g_warning ("wrong signature for locals blob");
2582 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
2583 mh = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
2584 mh->num_locals = len;
2585 for (i = 0; i < len; ++i) {
2586 mh->locals [i] = mono_metadata_parse_type_full (
2587 m, container, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
2588 if (!mh->locals [i]) {
2589 mono_loader_unlock ();
2594 mh = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodHeader));
2597 mh->code_size = code_size;
2598 mh->max_stack = max_stack;
2599 mh->init_locals = init_locals;
2600 if (fat_flags & METHOD_HEADER_MORE_SECTS)
2601 parse_section_data (m, mh, (const unsigned char*)ptr);
2602 mono_loader_unlock ();
2607 * mono_metadata_parse_mh:
2608 * @generic_context: generics context
2609 * @ptr: pointer to the method header.
2611 * Decode the method header at @ptr, including pointer to the IL code,
2612 * info about local variables and optional exception tables.
2613 * This is a Mono runtime internal function.
2615 * Returns: a MonoMethodHeader.
2618 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
2620 MonoMethodHeader *res;
2622 mono_loader_lock ();
2624 res = mono_metadata_parse_mh_full (m, NULL, ptr);
2626 mono_loader_unlock ();
2632 * mono_metadata_free_mh:
2633 * @mh: a method header
2635 * Free the memory allocated for the method header.
2636 * This is a Mono runtime internal function.
2639 mono_metadata_free_mh (MonoMethodHeader *mh)
2641 /* Allocated from the mempool */
2645 * mono_method_header_get_code:
2646 * @header: a MonoMethodHeader pointer
2647 * @code_size: memory location for returning the code size
2648 * @max_stack: memory location for returning the max stack
2650 * Method header accessor to retreive info about the IL code properties:
2651 * a pointer to the IL code itself, the size of the code and the max number
2652 * of stack slots used by the code.
2654 * Returns: pointer to the IL code represented by the method header.
2656 const unsigned char*
2657 mono_method_header_get_code (MonoMethodHeader *header, guint32* code_size, guint32* max_stack)
2660 *code_size = header->code_size;
2662 *max_stack = header->max_stack;
2663 return header->code;
2667 * mono_method_header_get_locals:
2668 * @header: a MonoMethodHeader pointer
2669 * @num_locals: memory location for returning the number of local variables
2670 * @init_locals: memory location for returning the init_locals flag
2672 * Method header accessor to retreive info about the local variables:
2673 * an array of local types, the number of locals and whether the locals
2674 * are supposed to be initialized to 0 on method entry
2676 * Returns: pointer to an array of types of the local variables
2679 mono_method_header_get_locals (MonoMethodHeader *header, guint32* num_locals, gboolean *init_locals)
2682 *num_locals = header->num_locals;
2684 *init_locals = header->init_locals;
2685 return header->locals;
2689 * mono_method_header_get_num_clauses:
2690 * @header: a MonoMethodHeader pointer
2692 * Method header accessor to retreive the number of exception clauses.
2694 * Returns: the number of exception clauses present
2697 mono_method_header_get_num_clauses (MonoMethodHeader *header)
2699 return header->num_clauses;
2703 * mono_method_header_get_clauses:
2704 * @header: a MonoMethodHeader pointer
2705 * @method: MonoMethod the header belongs to
2706 * @iter: pointer to a iterator
2707 * @clause: pointer to a MonoExceptionClause structure which will be filled with the info
2709 * Get the info about the exception clauses in the method. Set *iter to NULL to
2710 * initiate the iteration, then call the method repeatedly until it returns FALSE.
2711 * At each iteration, the structure pointed to by clause if filled with the
2712 * exception clause information.
2714 * Returns: TRUE if clause was filled with info, FALSE if there are no more exception
2718 mono_method_header_get_clauses (MonoMethodHeader *header, MonoMethod *method, gpointer *iter, MonoExceptionClause *clause)
2720 MonoExceptionClause *sc;
2721 /* later we'll be able to use this interface to parse the clause info on demand,
2722 * without allocating anything.
2724 if (!iter || !header->num_clauses)
2727 *iter = sc = header->clauses;
2733 if (sc < header->clauses + header->num_clauses) {
2742 * mono_metadata_parse_field_type:
2743 * @m: metadata context to extract information from
2744 * @ptr: pointer to the field signature
2745 * @rptr: pointer updated to match the end of the decoded stream
2747 * Parses the field signature, and returns the type information for it.
2749 * Returns: The MonoType that was extracted from @ptr.
2752 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
2754 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
2758 * mono_metadata_parse_param:
2759 * @m: metadata context to extract information from
2760 * @ptr: pointer to the param signature
2761 * @rptr: pointer updated to match the end of the decoded stream
2763 * Parses the param signature, and returns the type information for it.
2765 * Returns: The MonoType that was extracted from @ptr.
2768 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
2770 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
2774 * mono_metadata_token_from_dor:
2775 * @dor_token: A TypeDefOrRef coded index
2777 * dor_token is a TypeDefOrRef coded index: it contains either
2778 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
2779 * bits contain an index into the table.
2781 * Returns: an expanded token
2784 mono_metadata_token_from_dor (guint32 dor_index)
2788 table = dor_index & 0x03;
2789 idx = dor_index >> 2;
2792 case 0: /* TypeDef */
2793 return MONO_TOKEN_TYPE_DEF | idx;
2794 case 1: /* TypeRef */
2795 return MONO_TOKEN_TYPE_REF | idx;
2796 case 2: /* TypeSpec */
2797 return MONO_TOKEN_TYPE_SPEC | idx;
2799 g_assert_not_reached ();
2806 * We use this to pass context information to the row locator
2809 int idx; /* The index that we are trying to locate */
2810 int col_idx; /* The index in the row where idx may be stored */
2811 MonoTableInfo *t; /* pointer to the table */
2816 * How the row locator works.
2821 * ___|___------> _______
2824 * A column in the rows of table A references an index in table B.
2825 * For example A may be the TYPEDEF table and B the METHODDEF table.
2827 * Given an index in table B we want to get the row in table A
2828 * where the column n references our index in B.
2830 * In the locator_t structure:
2832 * col_idx is the column number
2833 * index is the index in table B
2834 * result will be the index in table A
2837 * Table A Table B column (in table A)
2838 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
2839 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
2840 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
2841 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
2842 * METHODSEM PROPERTY ASSOCIATION (encoded index)
2844 * Note that we still don't support encoded indexes.
2848 typedef_locator (const void *a, const void *b)
2850 locator_t *loc = (locator_t *) a;
2851 const char *bb = (const char *) b;
2852 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
2853 guint32 col, col_next;
2855 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
2861 * Need to check that the next row is valid.
2863 if (typedef_index + 1 < loc->t->rows) {
2864 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
2865 if (loc->idx >= col_next)
2868 if (col == col_next)
2872 loc->result = typedef_index;
2878 table_locator (const void *a, const void *b)
2880 locator_t *loc = (locator_t *) a;
2881 const char *bb = (const char *) b;
2882 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2885 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2887 if (loc->idx == col) {
2888 loc->result = table_index;
2898 declsec_locator (const void *a, const void *b)
2900 locator_t *loc = (locator_t *) a;
2901 const char *bb = (const char *) b;
2902 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2905 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2907 if (loc->idx == col) {
2908 loc->result = table_index;
2920 * Return the 1-based row index in TABLE, which must be one of the *Ptr tables,
2921 * which contains IDX.
2924 search_ptr_table (MonoImage *image, int table, int idx)
2926 MonoTableInfo *ptrdef = &image->tables [table];
2929 /* Use a linear search to find our index in the table */
2930 for (i = 0; i < ptrdef->rows; i ++)
2931 /* All the Ptr tables have the same structure */
2932 if (mono_metadata_decode_row_col (ptrdef, i, 0) == idx)
2935 if (i < ptrdef->rows)
2942 * mono_metadata_typedef_from_field:
2943 * @meta: metadata context
2944 * @index: FieldDef token
2946 * Returns: the 1-based index into the TypeDef table of the type that
2947 * declared the field described by @index, or 0 if not found.
2950 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
2952 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2958 loc.idx = mono_metadata_token_index (index);
2959 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
2962 if (meta->uncompressed_metadata)
2963 loc.idx = search_ptr_table (meta, MONO_TABLE_FIELD_POINTER, loc.idx);
2965 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2966 g_assert_not_reached ();
2968 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2969 return loc.result + 1;
2973 * mono_metadata_typedef_from_method:
2974 * @meta: metadata context
2975 * @index: MethodDef token
2977 * Returns: the 1-based index into the TypeDef table of the type that
2978 * declared the method described by @index. 0 if not found.
2981 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
2983 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2989 loc.idx = mono_metadata_token_index (index);
2990 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
2993 if (meta->uncompressed_metadata)
2994 loc.idx = search_ptr_table (meta, MONO_TABLE_METHOD_POINTER, loc.idx);
2996 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2997 g_assert_not_reached ();
2999 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3000 return loc.result + 1;
3004 * mono_metadata_interfaces_from_typedef_full:
3005 * @meta: metadata context
3006 * @index: typedef token
3008 * The array of interfaces that the @index typedef token implements is returned in
3009 * @interfaces. The number of elemnts in the array is returned in @count.
3011 * LOCKING: Assumes the loader lock is held.
3013 * Returns: TRUE on success, FALSE on failure.
3016 mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, MonoClass ***interfaces, guint *count, MonoGenericContext *context)
3018 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
3021 guint32 cols [MONO_INTERFACEIMPL_SIZE];
3030 loc.idx = mono_metadata_token_index (index);
3031 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
3034 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3039 * We may end up in the middle of the rows...
3042 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
3048 while (pos < tdef->rows) {
3049 mono_metadata_decode_row (tdef, pos, cols, MONO_INTERFACEIMPL_SIZE);
3050 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
3055 mono_loader_lock ();
3056 result = mono_mempool_alloc0 (meta->mempool, sizeof (MonoClass*) * (pos - start));
3057 mono_loader_unlock ();
3060 while (pos < tdef->rows) {
3063 mono_metadata_decode_row (tdef, pos, cols, MONO_INTERFACEIMPL_SIZE);
3064 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
3066 iface = mono_class_get_full (
3067 meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]), context);
3070 result [pos - start] = iface;
3073 *count = pos - start;
3074 *interfaces = result;
3079 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
3081 MonoClass **interfaces;
3084 mono_loader_lock ();
3085 rv = mono_metadata_interfaces_from_typedef_full (meta, index, &interfaces, count, NULL);
3086 mono_loader_unlock ();
3094 * mono_metadata_nested_in_typedef:
3095 * @meta: metadata context
3096 * @index: typedef token
3098 * Returns: the 1-based index into the TypeDef table of the type
3099 * where the type described by @index is nested.
3100 * Retruns 0 if @index describes a non-nested type.
3103 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
3105 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
3111 loc.idx = mono_metadata_token_index (index);
3112 loc.col_idx = MONO_NESTED_CLASS_NESTED;
3115 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3118 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3119 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
3123 * mono_metadata_nesting_typedef:
3124 * @meta: metadata context
3125 * @index: typedef token
3127 * Returns: the 1-based index into the TypeDef table of the first type
3128 * that is nested inside the type described by @index. The search starts at
3129 * @start_index. returns 0 if no such type is found.
3132 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
3134 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
3136 guint32 class_index = mono_metadata_token_index (index);
3141 start = start_index;
3143 while (start <= tdef->rows) {
3144 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
3150 if (start > tdef->rows)
3157 * mono_metadata_packing_from_typedef:
3158 * @meta: metadata context
3159 * @index: token representing a type
3161 * Returns: the info stored in the ClassLAyout table for the given typedef token
3162 * into the @packing and @size pointers.
3163 * Returns 0 if the info is not found.
3166 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
3168 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
3170 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
3175 loc.idx = mono_metadata_token_index (index);
3176 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
3179 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3182 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
3184 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
3186 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
3188 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3189 return loc.result + 1;
3193 * mono_metadata_custom_attrs_from_index:
3194 * @meta: metadata context
3195 * @index: token representing the parent
3197 * Returns: the 1-based index into the CustomAttribute table of the first
3198 * attribute which belongs to the metadata object described by @index.
3199 * Returns 0 if no such attribute is found.
3202 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
3204 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
3211 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
3214 /* FIXME: Index translation */
3216 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3219 /* Find the first entry by searching backwards */
3220 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
3223 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
3224 return loc.result + 1;
3228 * mono_metadata_declsec_from_index:
3229 * @meta: metadata context
3230 * @index: token representing the parent
3232 * Returns: the 0-based index into the DeclarativeSecurity table of the first
3233 * attribute which belongs to the metadata object described by @index.
3234 * Returns -1 if no such attribute is found.
3237 mono_metadata_declsec_from_index (MonoImage *meta, guint32 index)
3239 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_DECLSECURITY];
3246 loc.col_idx = MONO_DECL_SECURITY_PARENT;
3249 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
3252 /* Find the first entry by searching backwards */
3253 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_DECL_SECURITY_PARENT) == index))
3261 mono_backtrace (int limit)
3266 backtrace (array, limit);
3267 names = backtrace_symbols (array, limit);
3268 for (i =0; i < limit; ++i) {
3269 g_print ("\t%s\n", names [i]);
3276 /*#define __alignof__(a) sizeof(a)*/
3277 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
3282 * @t: the type to return the size of
3284 * Returns: the number of bytes required to hold an instance of this
3288 mono_type_size (MonoType *t, int *align)
3295 *align = __alignof__(gpointer);
3296 return sizeof (gpointer);
3300 case MONO_TYPE_VOID:
3303 case MONO_TYPE_BOOLEAN:
3304 *align = __alignof__(gint8);
3308 *align = __alignof__(gint8);
3310 case MONO_TYPE_CHAR:
3313 *align = __alignof__(gint16);
3317 *align = __alignof__(gint32);
3320 *align = __alignof__(float);
3324 *align = __alignof__(gint64);
3327 *align = __alignof__(double);
3331 *align = __alignof__(gpointer);
3332 return sizeof (gpointer);
3333 case MONO_TYPE_STRING:
3334 *align = __alignof__(gpointer);
3335 return sizeof (gpointer);
3336 case MONO_TYPE_OBJECT:
3337 *align = __alignof__(gpointer);
3338 return sizeof (gpointer);
3339 case MONO_TYPE_VALUETYPE: {
3340 if (t->data.klass->enumtype)
3341 return mono_type_size (t->data.klass->enum_basetype, align);
3343 return mono_class_value_size (t->data.klass, (guint32*)align);
3345 case MONO_TYPE_CLASS:
3346 case MONO_TYPE_SZARRAY:
3348 case MONO_TYPE_FNPTR:
3349 case MONO_TYPE_ARRAY:
3350 *align = __alignof__(gpointer);
3351 return sizeof (gpointer);
3352 case MONO_TYPE_TYPEDBYREF:
3353 return mono_class_value_size (mono_defaults.typed_reference_class, (guint32*)align);
3354 case MONO_TYPE_GENERICINST: {
3355 MonoGenericClass *gclass = t->data.generic_class;
3356 MonoClass *container_class = gclass->container_class;
3358 // g_assert (!gclass->inst->is_open);
3360 if (container_class->valuetype) {
3361 if (container_class->enumtype)
3362 return mono_type_size (container_class->enum_basetype, align);
3364 return mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align);
3366 *align = __alignof__(gpointer);
3367 return sizeof (gpointer);
3371 case MONO_TYPE_MVAR:
3372 /* FIXME: Martin, this is wrong. */
3373 *align = __alignof__(gpointer);
3374 return sizeof (gpointer);
3376 g_error ("mono_type_size: type 0x%02x unknown", t->type);
3382 * mono_type_stack_size:
3383 * @t: the type to return the size it uses on the stack
3385 * Returns: the number of bytes required to hold an instance of this
3386 * type on the runtime stack
3389 mono_type_stack_size (MonoType *t, int *align)
3393 g_assert (t != NULL);
3399 *align = __alignof__(gpointer);
3400 return sizeof (gpointer);
3404 case MONO_TYPE_BOOLEAN:
3405 case MONO_TYPE_CHAR:
3414 case MONO_TYPE_STRING:
3415 case MONO_TYPE_OBJECT:
3416 case MONO_TYPE_CLASS:
3417 case MONO_TYPE_SZARRAY:
3419 case MONO_TYPE_FNPTR:
3420 case MONO_TYPE_ARRAY:
3421 *align = __alignof__(gpointer);
3422 return sizeof (gpointer);
3423 case MONO_TYPE_TYPEDBYREF:
3424 *align = __alignof__(gpointer);
3425 return sizeof (gpointer) * 3;
3427 *align = __alignof__(float);
3428 return sizeof (float);
3431 *align = __alignof__(gint64);
3432 return sizeof (gint64);
3434 *align = __alignof__(double);
3435 return sizeof (double);
3436 case MONO_TYPE_VALUETYPE: {
3439 if (t->data.klass->enumtype)
3440 return mono_type_stack_size (t->data.klass->enum_basetype, align);
3442 size = mono_class_value_size (t->data.klass, (guint32*)align);
3444 *align = *align + __alignof__(gpointer) - 1;
3445 *align &= ~(__alignof__(gpointer) - 1);
3447 size += sizeof (gpointer) - 1;
3448 size &= ~(sizeof (gpointer) - 1);
3453 case MONO_TYPE_GENERICINST: {
3454 MonoGenericClass *gclass = t->data.generic_class;
3455 MonoClass *container_class = gclass->container_class;
3457 g_assert (!gclass->context.class_inst->is_open);
3459 if (container_class->valuetype) {
3460 if (container_class->enumtype)
3461 return mono_type_stack_size (container_class->enum_basetype, align);
3463 guint32 size = mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align);
3465 *align = *align + __alignof__(gpointer) - 1;
3466 *align &= ~(__alignof__(gpointer) - 1);
3468 size += sizeof (gpointer) - 1;
3469 size &= ~(sizeof (gpointer) - 1);
3474 *align = __alignof__(gpointer);
3475 return sizeof (gpointer);
3479 g_error ("type 0x%02x unknown", t->type);
3485 mono_type_generic_inst_is_valuetype (MonoType *type)
3487 g_assert (type->type == MONO_TYPE_GENERICINST);
3488 return type->data.generic_class->container_class->valuetype;
3492 mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass)
3494 return gclass->container_class->valuetype;
3498 _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2, gboolean signature_only)
3501 MonoGenericInst *i1 = g1->context.class_inst;
3502 MonoGenericInst *i2 = g2->context.class_inst;
3504 if (i1->type_argc != i2->type_argc || g1->is_dynamic != g2->is_dynamic)
3506 if (!mono_metadata_class_equal (g1->container_class, g2->container_class, signature_only))
3508 for (i = 0; i < i1->type_argc; ++i) {
3509 if (!do_mono_metadata_type_equal (i1->type_argv [i], i2->type_argv [i], signature_only))
3516 mono_metadata_generic_context_hash (const MonoGenericContext *context)
3518 /* FIXME: check if this seed is good enough */
3519 guint hash = 0xc01dfee7;
3520 if (context->class_inst)
3521 hash = ((hash << 5) - hash) ^ context->class_inst->id;
3522 if (context->method_inst)
3523 hash = ((hash << 5) - hash) ^ context->method_inst->id;
3528 mono_metadata_generic_context_equal (const MonoGenericContext *g1, const MonoGenericContext *g2)
3530 return g1->class_inst == g2->class_inst && g1->method_inst == g2->method_inst;
3534 * mono_metadata_type_hash:
3537 * Computes an hash value for @t1 to be used in GHashTable.
3540 mono_metadata_type_hash (MonoType *t1)
3542 guint hash = t1->type;
3544 hash |= t1->byref << 6; /* do not collide with t1->type values */
3546 case MONO_TYPE_VALUETYPE:
3547 case MONO_TYPE_CLASS:
3548 case MONO_TYPE_SZARRAY:
3549 /* check if the distribution is good enough */
3550 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
3552 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
3553 case MONO_TYPE_ARRAY:
3554 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
3555 case MONO_TYPE_GENERICINST:
3556 return ((hash << 5) - hash) ^ mono_generic_class_hash (t1->data.generic_class);
3562 mono_metadata_generic_param_equal (MonoGenericParam *p1, MonoGenericParam *p2, gboolean signature_only)
3566 if (p1->num != p2->num)
3569 if (p1->owner == p2->owner)
3573 * If `signature_only' is true, we're comparing two (method) signatures.
3574 * In this case, the owner of two type parameters doesn't need to match.
3577 return signature_only;
3581 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only)
3585 if (c1->generic_class && c2->generic_class)
3586 return _mono_metadata_generic_class_equal (c1->generic_class, c2->generic_class, signature_only);
3587 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
3588 return mono_metadata_generic_param_equal (
3589 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
3590 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
3591 return mono_metadata_generic_param_equal (
3592 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
3593 if (signature_only &&
3594 (c1->byval_arg.type == MONO_TYPE_SZARRAY) && (c2->byval_arg.type == MONO_TYPE_SZARRAY))
3595 return mono_metadata_class_equal (c1->byval_arg.data.klass, c2->byval_arg.data.klass, signature_only);
3600 mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only)
3602 gpointer iter1 = 0, iter2 = 0;
3606 if (s1->call_convention != s2->call_convention)
3608 if (s1->sentinelpos != s2->sentinelpos)
3610 if (s1->hasthis != s2->hasthis)
3612 if (s1->explicit_this != s2->explicit_this)
3614 if (! do_mono_metadata_type_equal (s1->ret, s2->ret, signature_only))
3616 if (s1->param_count != s2->param_count)
3620 MonoType *t1 = mono_signature_get_params (s1, &iter1);
3621 MonoType *t2 = mono_signature_get_params (s2, &iter2);
3623 if (t1 == NULL || t2 == NULL)
3625 if (! do_mono_metadata_type_equal (t1, t2, signature_only))
3631 * mono_metadata_type_equal:
3635 * Determine if @t1 and @t2 represent the same type.
3636 * Returns: #TRUE if @t1 and @t2 are equal.
3639 do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only)
3641 if (t1->type != t2->type || t1->byref != t2->byref)
3645 case MONO_TYPE_VOID:
3646 case MONO_TYPE_BOOLEAN:
3647 case MONO_TYPE_CHAR:
3658 case MONO_TYPE_STRING:
3661 case MONO_TYPE_OBJECT:
3662 case MONO_TYPE_TYPEDBYREF:
3664 case MONO_TYPE_VALUETYPE:
3665 case MONO_TYPE_CLASS:
3666 case MONO_TYPE_SZARRAY:
3667 return mono_metadata_class_equal (t1->data.klass, t2->data.klass, signature_only);
3669 return do_mono_metadata_type_equal (t1->data.type, t2->data.type, signature_only);
3670 case MONO_TYPE_ARRAY:
3671 if (t1->data.array->rank != t2->data.array->rank)
3673 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass, signature_only);
3674 case MONO_TYPE_GENERICINST:
3675 return _mono_metadata_generic_class_equal (
3676 t1->data.generic_class, t2->data.generic_class, signature_only);
3678 return mono_metadata_generic_param_equal (
3679 t1->data.generic_param, t2->data.generic_param, signature_only);
3680 case MONO_TYPE_MVAR:
3681 return mono_metadata_generic_param_equal (
3682 t1->data.generic_param, t2->data.generic_param, signature_only);
3683 case MONO_TYPE_FNPTR:
3684 return mono_metadata_fnptr_equal (t1->data.method, t2->data.method, signature_only);
3686 g_error ("implement type compare for %0x!", t1->type);
3694 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
3696 return do_mono_metadata_type_equal (t1, t2, FALSE);
3700 * mono_metadata_signature_equal:
3701 * @sig1: a signature
3702 * @sig2: another signature
3704 * Determine if @sig1 and @sig2 represent the same signature, with the
3705 * same number of arguments and the same types.
3706 * Returns: #TRUE if @sig1 and @sig2 are equal.
3709 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
3713 if (sig1->hasthis != sig2->hasthis || sig1->param_count != sig2->param_count)
3717 * We're just comparing the signatures of two methods here:
3719 * If we have two generic methods `void Foo<U> (U u)' and `void Bar<V> (V v)',
3720 * U and V are equal here.
3722 * That's what the `signature_only' argument of do_mono_metadata_type_equal() is for.
3725 for (i = 0; i < sig1->param_count; i++) {
3726 MonoType *p1 = sig1->params[i];
3727 MonoType *p2 = sig2->params[i];
3729 /* if (p1->attrs != p2->attrs)
3732 if (!do_mono_metadata_type_equal (p1, p2, TRUE))
3736 if (!do_mono_metadata_type_equal (sig1->ret, sig2->ret, TRUE))
3742 * mono_metadata_type_dup:
3743 * @image: mempool to use
3744 * @original: type to duplicate
3746 * Returns: copy of type allocated from mempool.
3749 mono_metadata_type_dup (MonoMemPool *mp, const MonoType *o)
3752 int sizeof_o = sizeof (MonoType);
3754 sizeof_o += (o->num_mods - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod);
3756 mono_loader_lock ();
3757 r = mp ? mono_mempool_alloc0 (mp, sizeof_o) : g_malloc (sizeof_o);
3758 mono_loader_unlock ();
3760 memcpy (r, o, sizeof_o);
3762 if (o->type == MONO_TYPE_PTR) {
3763 r->data.type = mono_metadata_type_dup (mp, o->data.type);
3764 } else if (o->type == MONO_TYPE_ARRAY) {
3765 /* FIXME: should mono_dup_array_type() use mempools? */
3767 r->data.array = mono_dup_array_type (o->data.array);
3768 } else if (o->type == MONO_TYPE_FNPTR) {
3769 /* FIXME: should mono_metadata_signature_deep_dup() use mempools? */
3771 r->data.method = mono_metadata_signature_deep_dup (o->data.method);
3777 mono_signature_hash (MonoMethodSignature *sig)
3779 guint i, res = sig->ret->type;
3781 for (i = 0; i < sig->param_count; i++)
3782 res = (res << 5) - res + mono_type_hash (sig->params[i]);
3788 * mono_metadata_encode_value:
3789 * @value: value to encode
3790 * @buf: buffer where to write the compressed representation
3791 * @endbuf: pointer updated to point at the end of the encoded output
3793 * Encodes the value @value in the compressed representation used
3794 * in metadata and stores the result in @buf. @buf needs to be big
3795 * enough to hold the data (4 bytes).
3798 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
3804 else if (value < 0x4000) {
3805 p [0] = 0x80 | (value >> 8);
3806 p [1] = value & 0xff;
3809 p [0] = (value >> 24) | 0xc0;
3810 p [1] = (value >> 16) & 0xff;
3811 p [2] = (value >> 8) & 0xff;
3812 p [3] = value & 0xff;
3820 * mono_metadata_field_info:
3821 * @meta: the Image the field is defined in
3822 * @index: the index in the field table representing the field
3823 * @offset: a pointer to an integer where to store the offset that
3824 * may have been specified for the field in a FieldLayout table
3825 * @rva: a pointer to the RVA of the field data in the image that
3826 * may have been defined in a FieldRVA table
3827 * @marshal_spec: a pointer to the marshal spec that may have been
3828 * defined for the field in a FieldMarshal table.
3830 * Gather info for field @index that may have been defined in the FieldLayout,
3831 * FieldRVA and FieldMarshal tables.
3832 * Either of offset, rva and marshal_spec can be NULL if you're not interested
3836 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
3837 MonoMarshalSpec **marshal_spec)
3839 MonoTableInfo *tdef;
3842 loc.idx = index + 1;
3843 if (meta->uncompressed_metadata)
3844 loc.idx = search_ptr_table (meta, MONO_TABLE_FIELD_POINTER, loc.idx);
3847 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
3849 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
3852 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3853 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
3855 *offset = (guint32)-1;
3859 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
3861 loc.col_idx = MONO_FIELD_RVA_FIELD;
3864 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3866 * LAMESPEC: There is no signature, no nothing, just the raw data.
3868 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
3876 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
3877 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
3884 * mono_metadata_get_constant_index:
3885 * @meta: the Image the field is defined in
3886 * @index: the token that may have a row defined in the constants table
3887 * @hint: possible position for the row
3889 * @token must be a FieldDef, ParamDef or PropertyDef token.
3891 * Returns: the index into the Constants table or 0 if not found.
3894 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
3896 MonoTableInfo *tdef;
3898 guint32 index = mono_metadata_token_index (token);
3900 tdef = &meta->tables [MONO_TABLE_CONSTANT];
3901 index <<= MONO_HASCONSTANT_BITS;
3902 switch (mono_metadata_token_table (token)) {
3903 case MONO_TABLE_FIELD:
3904 index |= MONO_HASCONSTANT_FIEDDEF;
3906 case MONO_TABLE_PARAM:
3907 index |= MONO_HASCONSTANT_PARAM;
3909 case MONO_TABLE_PROPERTY:
3910 index |= MONO_HASCONSTANT_PROPERTY;
3913 g_warning ("Not a valid token for the constant table: 0x%08x", token);
3917 loc.col_idx = MONO_CONSTANT_PARENT;
3920 /* FIXME: Index translation */
3922 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
3925 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3926 return loc.result + 1;
3932 * mono_metadata_events_from_typedef:
3933 * @meta: metadata context
3934 * @index: 0-based index (in the TypeDef table) describing a type
3936 * Returns: the 0-based index in the Event table for the events in the
3937 * type. The last event that belongs to the type (plus 1) is stored
3938 * in the @end_idx pointer.
3941 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3945 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
3953 loc.col_idx = MONO_EVENT_MAP_PARENT;
3954 loc.idx = index + 1;
3956 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3959 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
3960 if (loc.result + 1 < tdef->rows) {
3961 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
3963 end = meta->tables [MONO_TABLE_EVENT].rows;
3971 * mono_metadata_methods_from_event:
3972 * @meta: metadata context
3973 * @index: 0-based index (in the Event table) describing a event
3975 * Returns: the 0-based index in the MethodDef table for the methods in the
3976 * event. The last method that belongs to the event (plus 1) is stored
3977 * in the @end_idx pointer.
3980 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
3984 guint32 cols [MONO_METHOD_SEMA_SIZE];
3985 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3991 if (meta->uncompressed_metadata)
3992 index = search_ptr_table (meta, MONO_TABLE_EVENT_POINTER, index + 1) - 1;
3995 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3996 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
3998 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
4003 * We may end up in the middle of the rows...
4006 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
4012 while (end < msemt->rows) {
4013 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
4014 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
4023 * mono_metadata_properties_from_typedef:
4024 * @meta: metadata context
4025 * @index: 0-based index (in the TypeDef table) describing a type
4027 * Returns: the 0-based index in the Property table for the properties in the
4028 * type. The last property that belongs to the type (plus 1) is stored
4029 * in the @end_idx pointer.
4032 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
4036 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
4044 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
4045 loc.idx = index + 1;
4047 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4050 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
4051 if (loc.result + 1 < tdef->rows) {
4052 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
4054 end = meta->tables [MONO_TABLE_PROPERTY].rows;
4062 * mono_metadata_methods_from_property:
4063 * @meta: metadata context
4064 * @index: 0-based index (in the PropertyDef table) describing a property
4066 * Returns: the 0-based index in the MethodDef table for the methods in the
4067 * property. The last method that belongs to the property (plus 1) is stored
4068 * in the @end_idx pointer.
4071 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
4075 guint32 cols [MONO_METHOD_SEMA_SIZE];
4076 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
4082 if (meta->uncompressed_metadata)
4083 index = search_ptr_table (meta, MONO_TABLE_PROPERTY_POINTER, index + 1) - 1;
4086 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
4087 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
4089 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
4094 * We may end up in the middle of the rows...
4097 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
4103 while (end < msemt->rows) {
4104 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
4105 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
4114 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
4117 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
4122 /* No index translation seems to be needed */
4125 loc.col_idx = MONO_IMPLMAP_MEMBER;
4126 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
4128 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4131 return loc.result + 1;
4135 * @image: context where the image is created
4136 * @type_spec: typespec token
4138 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
4142 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
4144 guint32 idx = mono_metadata_token_index (type_spec);
4146 guint32 cols [MONO_TYPESPEC_SIZE];
4151 mono_loader_lock ();
4153 type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec));
4155 mono_loader_unlock ();
4159 t = &image->tables [MONO_TABLE_TYPESPEC];
4161 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
4162 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
4163 len = mono_metadata_decode_value (ptr, &ptr);
4165 type = g_new0 (MonoType, 1);
4167 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
4169 if (*ptr == MONO_TYPE_BYREF) {
4174 if (!do_mono_metadata_parse_type (type, image, NULL, ptr, &ptr)) {
4175 g_hash_table_remove (image->typespec_cache, GUINT_TO_POINTER (type_spec));
4177 mono_loader_unlock ();
4181 mono_loader_unlock ();
4187 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
4189 MonoMarshalSpec *res;
4191 const char *start = ptr;
4193 /* fixme: this is incomplete, but I cant find more infos in the specs */
4195 res = g_new0 (MonoMarshalSpec, 1);
4197 len = mono_metadata_decode_value (ptr, &ptr);
4198 res->native = *ptr++;
4200 if (res->native == MONO_NATIVE_LPARRAY) {
4201 res->data.array_data.param_num = -1;
4202 res->data.array_data.num_elem = -1;
4203 res->data.array_data.elem_mult = -1;
4205 if (ptr - start <= len)
4206 res->data.array_data.elem_type = *ptr++;
4207 if (ptr - start <= len)
4208 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
4209 if (ptr - start <= len)
4210 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
4211 if (ptr - start <= len) {
4213 * LAMESPEC: Older spec versions say this parameter comes before
4214 * num_elem. Never spec versions don't talk about elem_mult at
4215 * all, but csc still emits it, and it is used to distinguish
4216 * between param_num being 0, and param_num being omitted.
4217 * So if (param_num == 0) && (num_elem > 0), then
4218 * elem_mult == 0 -> the array size is num_elem
4219 * elem_mult == 1 -> the array size is @param_num + num_elem
4221 res->data.array_data.elem_mult = mono_metadata_decode_value (ptr, &ptr);
4225 if (res->native == MONO_NATIVE_BYVALTSTR) {
4226 if (ptr - start <= len)
4227 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
4230 if (res->native == MONO_NATIVE_BYVALARRAY) {
4231 if (ptr - start <= len)
4232 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
4235 if (res->native == MONO_NATIVE_CUSTOM) {
4236 /* skip unused type guid */
4237 len = mono_metadata_decode_value (ptr, &ptr);
4239 /* skip unused native type name */
4240 len = mono_metadata_decode_value (ptr, &ptr);
4242 /* read custom marshaler type name */
4243 len = mono_metadata_decode_value (ptr, &ptr);
4244 res->data.custom_data.custom_name = g_strndup (ptr, len);
4246 /* read cookie string */
4247 len = mono_metadata_decode_value (ptr, &ptr);
4248 res->data.custom_data.cookie = g_strndup (ptr, len);
4251 if (res->native == MONO_NATIVE_SAFEARRAY) {
4252 res->data.safearray_data.elem_type = 0;
4253 res->data.safearray_data.num_elem = 0;
4254 if (ptr - start <= len)
4255 res->data.safearray_data.elem_type = *ptr++;
4256 if (ptr - start <= len)
4257 res->data.safearray_data.num_elem = *ptr++;
4263 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
4265 if (spec->native == MONO_NATIVE_CUSTOM) {
4266 g_free (spec->data.custom_data.custom_name);
4267 g_free (spec->data.custom_data.cookie);
4273 * mono_type_to_unmanaged:
4275 * Returns: A MonoMarshalNative enumeration value (MONO_NATIVE_) value
4276 * describing the underlying native reprensetation of the type.
4278 * In addition the value pointed by
4279 * "conv" will contain the kind of marshalling required for this
4280 * particular type one of the MONO_MARSHAL_CONV_ enumeration values.
4283 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
4284 gboolean unicode, MonoMarshalConv *conv)
4286 MonoMarshalConv dummy_conv;
4292 *conv = MONO_MARSHAL_CONV_NONE;
4295 return MONO_NATIVE_UINT;
4299 case MONO_TYPE_BOOLEAN:
4301 switch (mspec->native) {
4302 case MONO_NATIVE_VARIANTBOOL:
4303 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
4304 return MONO_NATIVE_VARIANTBOOL;
4305 case MONO_NATIVE_BOOLEAN:
4306 *conv = MONO_MARSHAL_CONV_BOOL_I4;
4307 return MONO_NATIVE_BOOLEAN;
4308 case MONO_NATIVE_I1:
4309 case MONO_NATIVE_U1:
4310 return mspec->native;
4312 g_error ("cant marshal bool to native type %02x", mspec->native);
4315 *conv = MONO_MARSHAL_CONV_BOOL_I4;
4316 return MONO_NATIVE_BOOLEAN;
4317 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
4318 case MONO_TYPE_I1: return MONO_NATIVE_I1;
4319 case MONO_TYPE_U1: return MONO_NATIVE_U1;
4320 case MONO_TYPE_I2: return MONO_NATIVE_I2;
4321 case MONO_TYPE_U2: return MONO_NATIVE_U2;
4322 case MONO_TYPE_I4: return MONO_NATIVE_I4;
4323 case MONO_TYPE_U4: return MONO_NATIVE_U4;
4324 case MONO_TYPE_I8: return MONO_NATIVE_I8;
4325 case MONO_TYPE_U8: return MONO_NATIVE_U8;
4326 case MONO_TYPE_R4: return MONO_NATIVE_R4;
4327 case MONO_TYPE_R8: return MONO_NATIVE_R8;
4328 case MONO_TYPE_STRING:
4330 switch (mspec->native) {
4331 case MONO_NATIVE_BSTR:
4332 *conv = MONO_MARSHAL_CONV_STR_BSTR;
4333 return MONO_NATIVE_BSTR;
4334 case MONO_NATIVE_LPSTR:
4335 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
4336 return MONO_NATIVE_LPSTR;
4337 case MONO_NATIVE_LPWSTR:
4338 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
4339 return MONO_NATIVE_LPWSTR;
4340 case MONO_NATIVE_LPTSTR:
4341 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
4342 return MONO_NATIVE_LPTSTR;
4343 case MONO_NATIVE_ANSIBSTR:
4344 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
4345 return MONO_NATIVE_ANSIBSTR;
4346 case MONO_NATIVE_TBSTR:
4347 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
4348 return MONO_NATIVE_TBSTR;
4349 case MONO_NATIVE_BYVALTSTR:
4351 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
4353 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
4354 return MONO_NATIVE_BYVALTSTR;
4356 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);
4360 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
4361 return MONO_NATIVE_LPWSTR;
4364 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
4365 return MONO_NATIVE_LPSTR;
4367 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
4368 case MONO_TYPE_VALUETYPE: /*FIXME*/
4369 if (type->data.klass->enumtype) {
4370 t = type->data.klass->enum_basetype->type;
4373 if (type->data.klass == mono_defaults.handleref_class){
4374 *conv = MONO_MARSHAL_CONV_HANDLEREF;
4375 return MONO_NATIVE_INT;
4377 return MONO_NATIVE_STRUCT;
4378 case MONO_TYPE_SZARRAY:
4379 case MONO_TYPE_ARRAY:
4381 switch (mspec->native) {
4382 case MONO_NATIVE_BYVALARRAY:
4383 if ((type->data.klass->element_class == mono_defaults.char_class) && !unicode)
4384 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALCHARARRAY;
4386 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
4387 return MONO_NATIVE_BYVALARRAY;
4388 case MONO_NATIVE_SAFEARRAY:
4389 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
4390 return MONO_NATIVE_SAFEARRAY;
4391 case MONO_NATIVE_LPARRAY:
4392 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
4393 return MONO_NATIVE_LPARRAY;
4395 g_error ("cant marshal array as native type %02x", mspec->native);
4399 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
4400 return MONO_NATIVE_LPARRAY;
4401 case MONO_TYPE_I: return MONO_NATIVE_INT;
4402 case MONO_TYPE_U: return MONO_NATIVE_UINT;
4403 case MONO_TYPE_CLASS:
4404 case MONO_TYPE_OBJECT: {
4405 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
4407 switch (mspec->native) {
4408 case MONO_NATIVE_STRUCT:
4409 return MONO_NATIVE_STRUCT;
4410 case MONO_NATIVE_INTERFACE:
4411 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
4412 return MONO_NATIVE_INTERFACE;
4413 case MONO_NATIVE_IDISPATCH:
4414 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
4415 return MONO_NATIVE_IDISPATCH;
4416 case MONO_NATIVE_IUNKNOWN:
4417 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
4418 return MONO_NATIVE_IUNKNOWN;
4419 case MONO_NATIVE_FUNC:
4420 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
4421 type->data.klass == mono_defaults.delegate_class ||
4422 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
4423 *conv = MONO_MARSHAL_CONV_DEL_FTN;
4424 return MONO_NATIVE_FUNC;
4428 g_error ("cant marshal object as native type %02x", mspec->native);
4431 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
4432 type->data.klass == mono_defaults.delegate_class ||
4433 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
4434 *conv = MONO_MARSHAL_CONV_DEL_FTN;
4435 return MONO_NATIVE_FUNC;
4437 if (mono_defaults.safehandle_class && type->data.klass == mono_defaults.safehandle_class){
4438 *conv = MONO_MARSHAL_CONV_SAFEHANDLE;
4439 return MONO_NATIVE_INT;
4441 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
4442 return MONO_NATIVE_STRUCT;
4444 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
4445 case MONO_TYPE_GENERICINST:
4446 type = &type->data.generic_class->container_class->byval_arg;
4449 case MONO_TYPE_TYPEDBYREF:
4451 g_error ("type 0x%02x not handled in marshal", t);
4453 return MONO_NATIVE_MAX;
4457 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
4460 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
4466 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
4467 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
4469 /* FIXME: Index translation */
4471 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4474 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
4478 method_from_method_def_or_ref (MonoImage *m, guint32 tok, MonoGenericContext *context)
4480 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
4482 switch (tok & MONO_METHODDEFORREF_MASK) {
4483 case MONO_METHODDEFORREF_METHODDEF:
4484 return mono_get_method_full (m, MONO_TOKEN_METHOD_DEF | idx, NULL, context);
4485 case MONO_METHODDEFORREF_METHODREF:
4486 return mono_get_method_full (m, MONO_TOKEN_MEMBER_REF | idx, NULL, context);
4488 g_assert_not_reached ();
4493 * mono_class_get_overrides_full:
4495 * Return the method overrides belonging to class @type_token in @overrides, and
4496 * the number of overrides in @num_overrides.
4498 * Returns: TRUE on success, FALSE on failure.
4501 mono_class_get_overrides_full (MonoImage *image, guint32 type_token, MonoMethod ***overrides, gint32 *num_overrides,
4502 MonoGenericContext *generic_context)
4505 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
4508 guint32 cols [MONO_METHODIMPL_SIZE];
4509 MonoMethod **result;
4520 loc.col_idx = MONO_METHODIMPL_CLASS;
4521 loc.idx = mono_metadata_token_index (type_token);
4523 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4529 * We may end up in the middle of the rows...
4532 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
4537 while (end < tdef->rows) {
4538 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
4544 result = g_new (MonoMethod*, num * 2);
4545 for (i = 0; i < num; ++i) {
4548 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
4549 method = method_from_method_def_or_ref (
4550 image, cols [MONO_METHODIMPL_DECLARATION], generic_context);
4553 result [i * 2] = method;
4554 method = method_from_method_def_or_ref (
4555 image, cols [MONO_METHODIMPL_BODY], generic_context);
4558 result [i * 2 + 1] = method;
4561 *overrides = result;
4563 *num_overrides = num;
4568 * mono_guid_to_string:
4570 * Converts a 16 byte Microsoft GUID to the standard string representation.
4573 mono_guid_to_string (const guint8 *guid)
4575 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
4576 guid[3], guid[2], guid[1], guid[0],
4580 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
4584 get_constraints (MonoImage *image, int owner, MonoClass ***constraints, MonoGenericContainer *container)
4586 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
4587 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
4588 guint32 i, token, found;
4589 MonoClass *klass, **res;
4590 GList *cons = NULL, *tmp;
4591 MonoGenericContext *context = &container->context;
4593 *constraints = NULL;
4595 for (i = 0; i < tdef->rows; ++i) {
4596 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
4597 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
4598 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
4599 klass = mono_class_get_full (image, token, context);
4600 cons = g_list_append (cons, klass);
4603 /* contiguous list finished */
4610 res = g_new0 (MonoClass*, found + 1);
4611 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
4612 res [i] = tmp->data;
4620 * mono_metadata_get_generic_param_row:
4623 * @token: TypeOrMethodDef token, owner for GenericParam
4624 * @owner: coded token, set on return
4626 * Returns: 1-based row-id in the GenericParam table whose
4627 * owner is @token. 0 if not found.
4630 mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner)
4632 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
4633 guint32 cols [MONO_GENERICPARAM_SIZE];
4640 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
4641 *owner = MONO_TYPEORMETHOD_TYPE;
4642 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
4643 *owner = MONO_TYPEORMETHOD_METHOD;
4645 g_error ("wrong token %x to get_generic_param_row", token);
4648 *owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
4650 for (i = 0; i < tdef->rows; ++i) {
4651 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
4652 if (cols [MONO_GENERICPARAM_OWNER] == *owner)
4660 mono_metadata_has_generic_params (MonoImage *image, guint32 token)
4663 return mono_metadata_get_generic_param_row (image, token, &owner);
4667 * mono_metadata_load_generic_param_constraints:
4669 * Load the generic parameter constraints for the newly created generic type or method
4670 * represented by @token and @container. The @container is the new container which has
4671 * been returned by a call to mono_metadata_load_generic_params() with this @token.
4674 mono_metadata_load_generic_param_constraints (MonoImage *image, guint32 token,
4675 MonoGenericContainer *container)
4677 guint32 start_row, i, owner;
4678 if (! (start_row = mono_metadata_get_generic_param_row (image, token, &owner)))
4680 for (i = 0; i < container->type_argc; i++)
4681 get_constraints (image, start_row + i, &container->type_params [i].constraints, container);
4685 * mono_metadata_load_generic_params:
4687 * Load the type parameters from the type or method definition @token.
4689 * Use this method after parsing a type or method definition to figure out whether it's a generic
4690 * type / method. When parsing a method definition, @parent_container points to the generic container
4691 * of the current class, if any.
4693 * Note: This method does not load the constraints: for typedefs, this has to be done after fully
4694 * creating the type.
4696 * Returns: NULL if @token is not a generic type or method definition or the new generic container.
4699 MonoGenericContainer *
4700 mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericContainer *parent_container)
4702 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
4703 guint32 cols [MONO_GENERICPARAM_SIZE];
4704 guint32 i, owner = 0, n;
4705 MonoGenericContainer *container;
4706 MonoGenericParam *params;
4707 MonoGenericContext *context;
4709 if (!(i = mono_metadata_get_generic_param_row (image, token, &owner)))
4711 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
4714 container = g_new0 (MonoGenericContainer, 1);
4717 params = g_realloc (params, sizeof (MonoGenericParam) * n);
4718 params [n - 1].owner = container;
4719 params [n - 1].pklass = NULL;
4720 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
4721 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
4722 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
4723 params [n - 1].constraints = NULL;
4724 if (++i > tdef->rows)
4726 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
4727 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
4729 container->type_argc = n;
4730 container->type_params = params;
4731 container->parent = parent_container;
4733 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
4734 container->is_method = 1;
4736 g_assert (container->parent == NULL || container->is_method);
4738 context = &container->context;
4739 if (container->is_method) {
4740 context->class_inst = container->parent ? container->parent->context.class_inst : NULL;
4741 context->method_inst = mono_get_shared_generic_inst (container);
4743 context->class_inst = mono_get_shared_generic_inst (container);
4750 mono_get_shared_generic_inst (MonoGenericContainer *container)
4752 MonoType **type_argv;
4754 MonoGenericInst *nginst;
4757 type_argv = g_new0 (MonoType *, container->type_argc);
4758 helper = g_new0 (MonoType, container->type_argc);
4760 for (i = 0; i < container->type_argc; i++) {
4761 MonoType *t = &helper [i];
4763 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
4764 t->data.generic_param = &container->type_params [i];
4769 nginst = mono_metadata_get_generic_inst (container->type_argc, type_argv);
4778 mono_type_is_byref (MonoType *type)
4784 mono_type_get_type (MonoType *type)
4789 /* For MONO_TYPE_FNPTR */
4790 MonoMethodSignature*
4791 mono_type_get_signature (MonoType *type)
4793 return type->data.method;
4796 /* For MONO_TYPE_CLASS, VALUETYPE */
4798 mono_type_get_class (MonoType *type)
4800 return type->data.klass;
4803 /* For MONO_TYPE_ARRAY */
4805 mono_type_get_array_type (MonoType *type)
4807 return type->data.array;
4810 /* For MONO_TYPE_PTR */
4812 mono_type_get_ptr_type (MonoType *type)
4814 return type->data.type;
4818 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
4820 /* FIXME: implement */
4825 mono_signature_get_return_type (MonoMethodSignature *sig)
4831 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
4837 /* start from the first */
4838 if (sig->param_count) {
4839 *iter = &sig->params [0];
4840 return sig->params [0];
4848 if (type < &sig->params [sig->param_count]) {
4856 mono_signature_get_param_count (MonoMethodSignature *sig)
4858 return sig->param_count;
4862 mono_signature_get_call_conv (MonoMethodSignature *sig)
4864 return sig->call_convention;
4868 mono_signature_vararg_start (MonoMethodSignature *sig)
4870 return sig->sentinelpos;
4874 mono_signature_is_instance (MonoMethodSignature *sig)
4876 return sig->hasthis;
4880 mono_signature_explicit_this (MonoMethodSignature *sig)
4882 return sig->explicit_this;
4885 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
4887 mono_aligned_addr_hash (gconstpointer ptr)
4889 return GPOINTER_TO_UINT (ptr) >> 3;