2 * metadata.c: Routines for accessing the metadata
5 * Miguel de Icaza (miguel@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * (C) 2001-2002 Ximian, Inc.
17 #include "tabledefs.h"
18 #include "mono-endian.h"
20 #include "tokentype.h"
21 #include "metadata-internals.h"
22 #include "class-internals.h"
26 static void do_mono_metadata_parse_type (MonoType *type, MonoImage *m, const char *ptr, const char **rptr);
29 * Encoding of the "description" argument:
31 * identifier [CODE ARG]
33 * If CODE is ':', then a lookup on table ARG is performed
34 * If CODE is '=', then a lookup in the aliased-table ARG is performed
35 * If CODE is '#', then this encodes a flag, ARG is the flag name.
37 * Aliased table for example is `CustomAttributeType' which depending on the
38 * information might refer to different tables.
41 const static MonoMetaTable AssemblySchema [] = {
42 { MONO_MT_UINT32, "HashId" },
43 { MONO_MT_UINT16, "Major" },
44 { MONO_MT_UINT16, "Minor" },
45 { MONO_MT_UINT16, "BuildNumber" },
46 { MONO_MT_UINT16, "RevisionNumber" },
47 { MONO_MT_UINT32, "Flags" },
48 { MONO_MT_BLOB_IDX, "PublicKey" },
49 { MONO_MT_STRING_IDX, "Name" },
50 { MONO_MT_STRING_IDX, "Culture" },
54 const static MonoMetaTable AssemblyOSSchema [] = {
55 { MONO_MT_UINT32, "OSPlatformID" },
56 { MONO_MT_UINT32, "OSMajor" },
57 { MONO_MT_UINT32, "OSMinor" },
61 const static MonoMetaTable AssemblyProcessorSchema [] = {
62 { MONO_MT_UINT32, "Processor" },
66 const static MonoMetaTable AssemblyRefSchema [] = {
67 { MONO_MT_UINT16, "Major" },
68 { MONO_MT_UINT16, "Minor" },
69 { MONO_MT_UINT16, "Build" },
70 { MONO_MT_UINT16, "Revision" },
71 { MONO_MT_UINT32, "Flags" },
72 { MONO_MT_BLOB_IDX, "PublicKeyOrToken" },
73 { MONO_MT_STRING_IDX, "Name" },
74 { MONO_MT_STRING_IDX, "Culture" },
75 { MONO_MT_BLOB_IDX, "HashValue" },
79 const static MonoMetaTable AssemblyRefOSSchema [] = {
80 { MONO_MT_UINT32, "OSPlatformID" },
81 { MONO_MT_UINT32, "OSMajorVersion" },
82 { MONO_MT_UINT32, "OSMinorVersion" },
83 { MONO_MT_TABLE_IDX, "AssemblyRef:AssemblyRef" },
87 const static MonoMetaTable AssemblyRefProcessorSchema [] = {
88 { MONO_MT_UINT32, "Processor" },
89 { MONO_MT_TABLE_IDX, "AssemblyRef:AssemblyRef" },
93 const static MonoMetaTable ClassLayoutSchema [] = {
94 { MONO_MT_UINT16, "PackingSize" },
95 { MONO_MT_UINT32, "ClassSize" },
96 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
100 const static MonoMetaTable ConstantSchema [] = {
101 { MONO_MT_UINT8, "Type" },
102 { MONO_MT_UINT8, "PaddingZero" },
103 { MONO_MT_CONST_IDX, "Parent" },
104 { MONO_MT_BLOB_IDX, "Value" },
105 { MONO_MT_END, NULL }
108 const static MonoMetaTable CustomAttributeSchema [] = {
109 { MONO_MT_HASCAT_IDX, "Parent" },
110 { MONO_MT_CAT_IDX, "Type" },
111 { MONO_MT_BLOB_IDX, "Value" },
112 { MONO_MT_END, NULL }
115 const static MonoMetaTable DeclSecuritySchema [] = {
116 { MONO_MT_UINT16, "Action" },
117 { MONO_MT_HASDEC_IDX, "Parent" },
118 { MONO_MT_BLOB_IDX, "PermissionSet" },
119 { MONO_MT_END, NULL }
122 const static MonoMetaTable EventMapSchema [] = {
123 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
124 { MONO_MT_TABLE_IDX, "EventList:Event" },
125 { MONO_MT_END, NULL }
128 const static MonoMetaTable EventSchema [] = {
129 { MONO_MT_UINT16, "EventFlags#EventAttribute" },
130 { MONO_MT_STRING_IDX, "Name" },
131 { MONO_MT_TABLE_IDX, "EventType" }, /* TypeDef or TypeRef */
132 { MONO_MT_END, NULL }
135 const static MonoMetaTable ExportedTypeSchema [] = {
136 { MONO_MT_UINT32, "Flags" },
137 { MONO_MT_TABLE_IDX, "TypeDefId" },
138 { MONO_MT_STRING_IDX, "TypeName" },
139 { MONO_MT_STRING_IDX, "TypeNameSpace" },
140 { MONO_MT_IMPL_IDX, "Implementation" },
141 { MONO_MT_END, NULL }
144 const static MonoMetaTable FieldSchema [] = {
145 { MONO_MT_UINT16, "Flags" },
146 { MONO_MT_STRING_IDX, "Name" },
147 { MONO_MT_BLOB_IDX, "Signature" },
148 { MONO_MT_END, NULL }
150 const static MonoMetaTable FieldLayoutSchema [] = {
151 { MONO_MT_UINT32, "Offset" },
152 { MONO_MT_TABLE_IDX, "Field:Field" },
153 { MONO_MT_END, NULL }
156 const static MonoMetaTable FieldMarshalSchema [] = {
157 { MONO_MT_HFM_IDX, "Parent" },
158 { MONO_MT_BLOB_IDX, "NativeType" },
159 { MONO_MT_END, NULL }
161 const static MonoMetaTable FieldRVASchema [] = {
162 { MONO_MT_UINT32, "RVA" },
163 { MONO_MT_TABLE_IDX, "Field:Field" },
164 { MONO_MT_END, NULL }
167 const static MonoMetaTable FileSchema [] = {
168 { MONO_MT_UINT32, "Flags" },
169 { MONO_MT_STRING_IDX, "Name" },
170 { MONO_MT_BLOB_IDX, "Value" },
171 { MONO_MT_END, NULL }
174 const static MonoMetaTable ImplMapSchema [] = {
175 { MONO_MT_UINT16, "MappingFlag" },
176 { MONO_MT_MF_IDX, "MemberForwarded" },
177 { MONO_MT_STRING_IDX, "ImportName" },
178 { MONO_MT_TABLE_IDX, "ImportScope:ModuleRef" },
179 { MONO_MT_END, NULL }
182 const static MonoMetaTable InterfaceImplSchema [] = {
183 { MONO_MT_TABLE_IDX, "Class:TypeDef" },
184 { MONO_MT_TDOR_IDX, "Interface=TypeDefOrRef" },
185 { MONO_MT_END, NULL }
188 const static MonoMetaTable ManifestResourceSchema [] = {
189 { MONO_MT_UINT32, "Offset" },
190 { MONO_MT_UINT32, "Flags" },
191 { MONO_MT_STRING_IDX, "Name" },
192 { MONO_MT_IMPL_IDX, "Implementation" },
193 { MONO_MT_END, NULL }
196 const static MonoMetaTable MemberRefSchema [] = {
197 { MONO_MT_MRP_IDX, "Class" },
198 { MONO_MT_STRING_IDX, "Name" },
199 { MONO_MT_BLOB_IDX, "Signature" },
200 { MONO_MT_END, NULL }
203 const static MonoMetaTable MethodSchema [] = {
204 { MONO_MT_UINT32, "RVA" },
205 { MONO_MT_UINT16, "ImplFlags#MethodImplAttributes" },
206 { MONO_MT_UINT16, "Flags#MethodAttribute" },
207 { MONO_MT_STRING_IDX, "Name" },
208 { MONO_MT_BLOB_IDX, "Signature" },
209 { MONO_MT_TABLE_IDX, "ParamList:Param" },
210 { MONO_MT_END, NULL }
213 const static MonoMetaTable MethodImplSchema [] = {
214 { MONO_MT_TABLE_IDX, "Class:TypeDef" },
215 { MONO_MT_MDOR_IDX, "MethodBody" },
216 { MONO_MT_MDOR_IDX, "MethodDeclaration" },
217 { MONO_MT_END, NULL }
220 const static MonoMetaTable MethodSemanticsSchema [] = {
221 { MONO_MT_UINT16, "MethodSemantic" },
222 { MONO_MT_TABLE_IDX, "Method:Method" },
223 { MONO_MT_HS_IDX, "Association" },
224 { MONO_MT_END, NULL }
227 const static MonoMetaTable ModuleSchema [] = {
228 { MONO_MT_UINT16, "Generation" },
229 { MONO_MT_STRING_IDX, "Name" },
230 { MONO_MT_GUID_IDX, "MVID" },
231 { MONO_MT_GUID_IDX, "EncID" },
232 { MONO_MT_GUID_IDX, "EncBaseID" },
233 { MONO_MT_END, NULL }
236 const static MonoMetaTable ModuleRefSchema [] = {
237 { MONO_MT_STRING_IDX, "Name" },
238 { MONO_MT_END, NULL }
241 const static MonoMetaTable NestedClassSchema [] = {
242 { MONO_MT_TABLE_IDX, "NestedClass:TypeDef" },
243 { MONO_MT_TABLE_IDX, "EnclosingClass:TypeDef" },
244 { MONO_MT_END, NULL }
247 const static MonoMetaTable ParamSchema [] = {
248 { MONO_MT_UINT16, "Flags" },
249 { MONO_MT_UINT16, "Sequence" },
250 { MONO_MT_STRING_IDX, "Name" },
251 { MONO_MT_END, NULL }
254 const static MonoMetaTable PropertySchema [] = {
255 { MONO_MT_UINT16, "Flags" },
256 { MONO_MT_STRING_IDX, "Name" },
257 { MONO_MT_BLOB_IDX, "Type" },
258 { MONO_MT_END, NULL }
261 const static MonoMetaTable PropertyMapSchema [] = {
262 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
263 { MONO_MT_TABLE_IDX, "PropertyList:Property" },
264 { MONO_MT_END, NULL }
267 const static MonoMetaTable StandaloneSigSchema [] = {
268 { MONO_MT_BLOB_IDX, "Signature" },
269 { MONO_MT_END, NULL }
272 const static MonoMetaTable TypeDefSchema [] = {
273 { MONO_MT_UINT32, "Flags" },
274 { MONO_MT_STRING_IDX, "Name" },
275 { MONO_MT_STRING_IDX, "Namespace" },
276 { MONO_MT_TDOR_IDX, "Extends" },
277 { MONO_MT_TABLE_IDX, "FieldList:Field" },
278 { MONO_MT_TABLE_IDX, "MethodList:Method" },
279 { MONO_MT_END, NULL }
282 const static MonoMetaTable TypeRefSchema [] = {
283 { MONO_MT_RS_IDX, "ResolutionScope=ResolutionScope" },
284 { MONO_MT_STRING_IDX, "Name" },
285 { MONO_MT_STRING_IDX, "Namespace" },
286 { MONO_MT_END, NULL }
289 const static MonoMetaTable TypeSpecSchema [] = {
290 { MONO_MT_BLOB_IDX, "Signature" },
291 { MONO_MT_END, NULL }
294 const static MonoMetaTable GenericParamSchema [] = {
295 { MONO_MT_UINT16, "Number" },
296 { MONO_MT_UINT16, "Flags" },
297 { MONO_MT_TABLE_IDX, "Owner" }, /* TypeDef or MethodDef */
298 { MONO_MT_STRING_IDX, "Name" },
300 /* soon to be removed */
301 { MONO_MT_TABLE_IDX, "Kind" },
303 { MONO_MT_END, NULL }
306 const static MonoMetaTable MethodSpecSchema [] = {
307 { MONO_MT_MDOR_IDX, "Method" },
308 { MONO_MT_BLOB_IDX, "Signature" },
309 { MONO_MT_END, NULL }
312 const static MonoMetaTable GenericParamConstraintSchema [] = {
313 { MONO_MT_TABLE_IDX, "GenericParam" },
314 { MONO_MT_TDOR_IDX, "Constraint" },
315 { MONO_MT_END, NULL }
318 const static struct {
319 const MonoMetaTable *table;
322 /* 0 */ { ModuleSchema, "Module" },
323 /* 1 */ { TypeRefSchema, "TypeRef" },
324 /* 2 */ { TypeDefSchema, "TypeDef" },
325 /* 3 */ { NULL, NULL },
326 /* 4 */ { FieldSchema, "Field" },
327 /* 5 */ { NULL, NULL },
328 /* 6 */ { MethodSchema, "Method" },
329 /* 7 */ { NULL, NULL },
330 /* 8 */ { ParamSchema, "Param" },
331 /* 9 */ { InterfaceImplSchema, "InterfaceImpl" },
332 /* A */ { MemberRefSchema, "MemberRef" },
333 /* B */ { ConstantSchema, "Constant" },
334 /* C */ { CustomAttributeSchema, "CustomAttribute" },
335 /* D */ { FieldMarshalSchema, "FieldMarshal" },
336 /* E */ { DeclSecuritySchema, "DeclSecurity" },
337 /* F */ { ClassLayoutSchema, "ClassLayout" },
338 /* 10 */ { FieldLayoutSchema, "FieldLayout" },
339 /* 11 */ { StandaloneSigSchema, "StandaloneSig" },
340 /* 12 */ { EventMapSchema, "EventMap" },
341 /* 13 */ { NULL, NULL },
342 /* 14 */ { EventSchema, "Event" },
343 /* 15 */ { PropertyMapSchema, "PropertyMap" },
344 /* 16 */ { NULL, NULL },
345 /* 17 */ { PropertySchema, "PropertyTable" },
346 /* 18 */ { MethodSemanticsSchema, "MethodSemantics" },
347 /* 19 */ { MethodImplSchema, "MethodImpl" },
348 /* 1A */ { ModuleRefSchema, "ModuleRef" },
349 /* 1B */ { TypeSpecSchema, "TypeSpec" },
350 /* 1C */ { ImplMapSchema, "ImplMap" },
351 /* 1D */ { FieldRVASchema, "FieldRVA" },
352 /* 1E */ { NULL, NULL },
353 /* 1F */ { NULL, NULL },
354 /* 20 */ { AssemblySchema, "Assembly" },
355 /* 21 */ { AssemblyProcessorSchema, "AssemblyProcessor" },
356 /* 22 */ { AssemblyOSSchema, "AssemblyOS" },
357 /* 23 */ { AssemblyRefSchema, "AssemblyRef" },
358 /* 24 */ { AssemblyRefProcessorSchema, "AssemblyRefProcessor" },
359 /* 25 */ { AssemblyRefOSSchema, "AssemblyRefOS" },
360 /* 26 */ { FileSchema, "File" },
361 /* 27 */ { ExportedTypeSchema, "ExportedType" },
362 /* 28 */ { ManifestResourceSchema, "ManifestResource" },
363 /* 29 */ { NestedClassSchema, "NestedClass" },
364 /* 2A */ { GenericParamSchema, "GenericParam" },
365 /* 2B */ { MethodSpecSchema, "MethodSpec" },
366 /* 2C */ { GenericParamConstraintSchema, "GenericParamConstraint" },
370 * mono_meta_table_name:
371 * @table: table index
373 * Returns the name for the @table index
376 mono_meta_table_name (int table)
378 if ((table < 0) || (table > 0x2c))
381 return tables [table].name;
384 /* The guy who wrote the spec for this should not be allowed near a
387 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
388 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
389 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
390 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
391 inverse of this mapping.
394 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
395 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
397 /* Reference: Partition II - 23.2.6 */
399 * mono_metadata_compute_size:
400 * @meta: metadata context
401 * @tableindex: metadata table number
402 * @result_bitfield: pointer to guint32 where to store additional info
404 * mono_metadata_compute_size() computes the lenght in bytes of a single
405 * row in a metadata table. The size of each column is encoded in the
406 * @result_bitfield return value along with the number of columns in the table.
407 * the resulting bitfield should be handed to the mono_metadata_table_size()
408 * and mono_metadata_table_count() macros.
411 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
413 guint32 bitfield = 0;
414 int size = 0, field_size;
417 const MonoMetaTable *table = tables [tableindex].table;
419 for (i = 0; (code = table [i].code) != MONO_MT_END; i++){
422 field_size = 4; break;
425 field_size = 2; break;
428 field_size = 1; break;
430 case MONO_MT_BLOB_IDX:
431 field_size = meta->idx_blob_wide ? 4 : 2; break;
433 case MONO_MT_STRING_IDX:
434 field_size = meta->idx_string_wide ? 4 : 2; break;
436 case MONO_MT_GUID_IDX:
437 field_size = meta->idx_guid_wide ? 4 : 2; break;
439 case MONO_MT_TABLE_IDX:
440 /* Uhm, a table index can point to other tables besides the current one
441 * so, it's not correct to use the rowcount of the current table to
442 * get the size for this column - lupus
444 switch (tableindex) {
445 case MONO_TABLE_ASSEMBLYREFOS:
447 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
448 case MONO_TABLE_ASSEMBLYPROCESSOR:
450 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
451 case MONO_TABLE_CLASSLAYOUT:
453 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
454 case MONO_TABLE_EVENTMAP:
455 g_assert (i == 0 || i == 1);
456 field_size = i ? idx_size (MONO_TABLE_EVENT):
457 idx_size(MONO_TABLE_TYPEDEF);
459 case MONO_TABLE_EVENT:
461 field_size = MAX (idx_size (MONO_TABLE_TYPEDEF), idx_size(MONO_TABLE_TYPEREF));
462 field_size = MAX (field_size, idx_size(MONO_TABLE_TYPESPEC));
464 case MONO_TABLE_EXPORTEDTYPE:
466 /* the index is in another metadata file, so it must be 4 */
467 field_size = 4; break;
468 case MONO_TABLE_FIELDLAYOUT:
470 field_size = idx_size (MONO_TABLE_FIELD); break;
471 case MONO_TABLE_FIELDRVA:
473 field_size = idx_size (MONO_TABLE_FIELD); break;
474 case MONO_TABLE_IMPLMAP:
476 field_size = idx_size (MONO_TABLE_MODULEREF); break;
477 case MONO_TABLE_INTERFACEIMPL:
479 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
480 case MONO_TABLE_METHOD:
482 field_size = idx_size (MONO_TABLE_PARAM); break;
483 case MONO_TABLE_METHODIMPL:
485 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
486 case MONO_TABLE_METHODSEMANTICS:
488 field_size = idx_size (MONO_TABLE_METHOD); break;
489 case MONO_TABLE_NESTEDCLASS:
490 g_assert (i == 0 || i == 1);
491 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
492 case MONO_TABLE_PROPERTYMAP:
493 g_assert (i == 0 || i == 1);
494 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
495 idx_size(MONO_TABLE_TYPEDEF);
497 case MONO_TABLE_TYPEDEF:
498 g_assert (i == 4 || i == 5);
499 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
500 idx_size(MONO_TABLE_METHOD);
501 case MONO_TABLE_GENERICPARAM:
502 g_assert (i == 2 || i == 4 || i == 5);
504 field_size = MAX (idx_size (MONO_TABLE_METHOD), idx_size (MONO_TABLE_TYPEDEF));
506 field_size = idx_size (MONO_TABLE_TYPEDEF);
508 field_size = idx_size (MONO_TABLE_TYPEDEF);
511 case MONO_TABLE_GENERICPARAMCONSTRAINT:
513 field_size = idx_size (MONO_TABLE_GENERICPARAM);
517 g_assert_not_reached ();
519 if (tableindex != MONO_TABLE_EXPORTEDTYPE && field_size != idx_size (tableindex))
520 g_warning ("size changed (%d to %d)", idx_size (tableindex), field_size);
525 * HasConstant: ParamDef, FieldDef, Property
527 case MONO_MT_CONST_IDX:
528 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
529 meta->tables [MONO_TABLE_FIELD].rows);
530 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
532 /* 2 bits to encode tag */
533 field_size = rtsize (n, 16-2);
537 * HasCustomAttribute: points to any table but
540 case MONO_MT_HASCAT_IDX:
542 * We believe that since the signature and
543 * permission are indexing the Blob heap,
544 * we should consider the blob size first
546 /* I'm not a believer - lupus
547 if (meta->idx_blob_wide){
552 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
553 meta->tables [MONO_TABLE_FIELD].rows);
554 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
555 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
556 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
557 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
558 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
559 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
560 n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
561 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
562 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
563 n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
564 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
565 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
566 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
567 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
568 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
569 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
570 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
572 /* 5 bits to encode */
573 field_size = rtsize (n, 16-5);
577 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
578 * MemberRef and String.
580 case MONO_MT_CAT_IDX:
581 /* String is a heap, if it is wide, we know the size */
583 if (meta->idx_string_wide){
588 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
589 meta->tables [MONO_TABLE_TYPEDEF].rows);
590 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
591 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
593 /* 3 bits to encode */
594 field_size = rtsize (n, 16-3);
598 * HasDeclSecurity: Typedef, MethodDef, Assembly
600 case MONO_MT_HASDEC_IDX:
601 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
602 meta->tables [MONO_TABLE_METHOD].rows);
603 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
605 /* 2 bits to encode */
606 field_size = rtsize (n, 16-2);
610 * Implementation: File, AssemblyRef, ExportedType
612 case MONO_MT_IMPL_IDX:
613 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
614 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
615 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
617 /* 2 bits to encode tag */
618 field_size = rtsize (n, 16-2);
622 * HasFieldMarshall: FieldDef, ParamDef
624 case MONO_MT_HFM_IDX:
625 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
626 meta->tables [MONO_TABLE_PARAM].rows);
628 /* 1 bit used to encode tag */
629 field_size = rtsize (n, 16-1);
633 * MemberForwarded: FieldDef, MethodDef
636 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
637 meta->tables [MONO_TABLE_METHOD].rows);
639 /* 1 bit used to encode tag */
640 field_size = rtsize (n, 16-1);
644 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
646 * It is TypeDef, _TypeRef_, TypeSpec, instead.
648 case MONO_MT_TDOR_IDX:
649 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
650 meta->tables [MONO_TABLE_TYPEREF].rows);
651 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
653 /* 2 bits to encode */
654 field_size = rtsize (n, 16-2);
658 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
660 case MONO_MT_MRP_IDX:
661 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
662 meta->tables [MONO_TABLE_TYPEREF].rows);
663 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
664 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
665 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
666 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
668 /* 3 bits to encode */
669 field_size = rtsize (n, 16 - 3);
672 case MONO_MT_MDOR_IDX:
675 * MethodDefOrRef: MethodDef, MemberRef
678 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
679 meta->tables [MONO_TABLE_MEMBERREF].rows);
681 /* 1 bit used to encode tag */
682 field_size = rtsize (n, 16-1);
686 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
689 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
690 meta->tables [MONO_TABLE_MODULEREF].rows);
691 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
692 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
694 /* 2 bits used to encode tag (ECMA spec claims 3) */
695 field_size = rtsize (n, 16 - 2);
700 * encode field size as follows (we just need to
707 bitfield |= (field_size-1) << shift;
710 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
713 *result_bitfield = (i << 24) | bitfield;
718 * mono_metadata_compute_table_bases:
719 * @meta: metadata context to compute table values
721 * Computes the table bases for the metadata structure.
722 * This is an internal function used by the image loader code.
725 mono_metadata_compute_table_bases (MonoImage *meta)
728 const char *base = meta->tables_base;
730 for (i = 0; i < 64; i++){
731 if (meta->tables [i].rows == 0)
734 meta->tables [i].row_size = mono_metadata_compute_size (
735 meta, i, &meta->tables [i].size_bitfield);
736 meta->tables [i].base = base;
737 base += meta->tables [i].rows * meta->tables [i].row_size;
742 * mono_metadata_locate:
743 * @meta: metadata context
744 * @table: table code.
745 * @idx: index of element to retrieve from @table.
747 * Returns a pointer to the @idx element in the metadata table
748 * whose code is @table.
751 mono_metadata_locate (MonoImage *meta, int table, int idx)
753 /* idx == 0 refers always to NULL */
754 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
756 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
760 * mono_metadata_locate_token:
761 * @meta: metadata context
762 * @token: metadata token
764 * Returns a pointer to the data in the metadata represented by the
768 mono_metadata_locate_token (MonoImage *meta, guint32 token)
770 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
774 * mono_metadata_get_table:
775 * @table: table to retrieve
777 * Returns the MonoMetaTable structure for table @table
779 const MonoMetaTable *
780 mono_metadata_get_table (MonoMetaTableEnum table)
784 g_return_val_if_fail ((x > 0) && (x <= MONO_TABLE_LAST), NULL);
786 return tables [table].table;
790 * mono_metadata_string_heap:
791 * @meta: metadata context
792 * @index: index into the string heap.
794 * Returns: an in-memory pointer to the @index in the string heap.
797 mono_metadata_string_heap (MonoImage *meta, guint32 index)
799 g_return_val_if_fail (index < meta->heap_strings.size, "");
800 return meta->heap_strings.data + index;
804 * mono_metadata_user_string:
805 * @meta: metadata context
806 * @index: index into the user string heap.
808 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
811 mono_metadata_user_string (MonoImage *meta, guint32 index)
813 g_return_val_if_fail (index < meta->heap_us.size, "");
814 return meta->heap_us.data + index;
818 * mono_metadata_blob_heap:
819 * @meta: metadata context
820 * @index: index into the blob.
822 * Returns: an in-memory pointer to the @index in the Blob heap.
825 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
827 g_return_val_if_fail (index < meta->heap_blob.size, "");
828 return meta->heap_blob.data + index;
832 * mono_metadata_guid_heap:
833 * @meta: metadata context
834 * @index: index into the guid heap.
836 * Returns: an in-memory pointer to the @index in the guid heap.
839 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
842 index *= 16; /* adjust for guid size and 1-based index */
843 g_return_val_if_fail (index < meta->heap_guid.size, "");
844 return meta->heap_guid.data + index;
848 dword_align (const char *ptr)
850 #if SIZEOF_VOID_P == 8
851 return (const char *) (((guint64) (ptr + 3)) & ~3);
853 return (const char *) (((guint32) (ptr + 3)) & ~3);
858 * mono_metadata_decode_row:
859 * @t: table to extract information from.
860 * @idx: index in table.
861 * @res: array of @res_size cols to store the results in
863 * This decompresses the metadata element @idx in table @t
864 * into the guint32 @res array that has res_size elements
867 mono_metadata_decode_row (MonoTableInfo *t, int idx, guint32 *res, int res_size)
869 guint32 bitfield = t->size_bitfield;
870 int i, count = mono_metadata_table_count (bitfield);
871 const char *data = t->base + idx * t->row_size;
873 g_assert (res_size == count);
875 for (i = 0; i < count; i++){
876 int n = mono_metadata_table_size (bitfield, i);
880 res [i] = *data; break;
882 res [i] = read16 (data); break;
885 res [i] = read32 (data); break;
888 g_assert_not_reached ();
895 * mono_metadata_decode_row_col:
896 * @t: table to extract information from.
897 * @idx: index for row in table.
898 * @col: column in the row.
900 * This function returns the value of column @col from the @idx
901 * row in the table @t.
904 mono_metadata_decode_row_col (MonoTableInfo *t, int idx, guint col)
906 guint32 bitfield = t->size_bitfield;
908 register const char *data = t->base + idx * t->row_size;
911 g_assert (col < mono_metadata_table_count (bitfield));
913 n = mono_metadata_table_size (bitfield, 0);
914 for (i = 0; i < col; ++i) {
916 n = mono_metadata_table_size (bitfield, i + 1);
922 return read16 (data);
924 return read32 (data);
926 g_assert_not_reached ();
931 * mono_metadata_decode_blob_size:
932 * @ptr: pointer to a blob object
933 * @rptr: the new position of the pointer
935 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
937 * Returns: the size of the blob object
940 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
942 const unsigned char *ptr = (const unsigned char *)xptr;
945 if ((*ptr & 0x80) == 0){
946 size = ptr [0] & 0x7f;
948 } else if ((*ptr & 0x40) == 0){
949 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
952 size = ((ptr [0] & 0x1f) << 24) +
965 * mono_metadata_decode_value:
966 * @ptr: pointer to decode from
967 * @rptr: the new position of the pointer
969 * This routine decompresses 32-bit values as specified in the "Blob and
970 * Signature" section (22.2)
972 * Returns: the decoded value
975 mono_metadata_decode_value (const char *_ptr, const char **rptr)
977 const unsigned char *ptr = (const unsigned char *) _ptr;
978 unsigned char b = *ptr;
981 if ((b & 0x80) == 0){
984 } else if ((b & 0x40) == 0){
985 len = ((b & 0x3f) << 8 | ptr [1]);
988 len = ((b & 0x1f) << 24) |
1001 * mono_metadata_parse_typedef_or_ref:
1002 * @m: a metadata context.
1003 * @ptr: a pointer to an encoded TypedefOrRef in @m
1004 * @rptr: pointer updated to match the end of the decoded stream
1006 * Returns: a token valid in the @m metadata decoded from
1007 * the compressed representation.
1010 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1013 token = mono_metadata_decode_value (ptr, &ptr);
1016 return mono_metadata_token_from_dor (token);
1020 * mono_metadata_parse_custom_mod:
1021 * @m: a metadata context.
1022 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1023 * @ptr: a pointer to (possibly) the start of a custom modifier list
1024 * @rptr: pointer updated to match the end of the decoded stream
1026 * Checks if @ptr points to a type custom modifier compressed representation.
1028 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1031 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1033 MonoCustomMod local;
1034 if ((*ptr == MONO_TYPE_CMOD_OPT) ||
1035 (*ptr == MONO_TYPE_CMOD_REQD)) {
1038 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1039 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1046 * mono_metadata_parse_array:
1047 * @m: a metadata context.
1048 * @ptr: a pointer to an encoded array description.
1049 * @rptr: pointer updated to match the end of the decoded stream
1051 * Decodes the compressed array description found in the metadata @m at @ptr.
1053 * Returns: a #MonoArrayType structure describing the array type
1057 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1060 MonoArrayType *array = g_new0 (MonoArrayType, 1);
1063 etype = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1064 array->eklass = mono_class_from_mono_type (etype);
1065 array->rank = mono_metadata_decode_value (ptr, &ptr);
1067 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1068 if (array->numsizes)
1069 array->sizes = g_new0 (int, array->numsizes);
1070 for (i = 0; i < array->numsizes; ++i)
1071 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1073 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1074 if (array->numlobounds)
1075 array->lobounds = g_new0 (int, array->numlobounds);
1076 for (i = 0; i < array->numlobounds; ++i)
1077 array->lobounds [i] = mono_metadata_decode_value (ptr, &ptr);
1085 * mono_metadata_free_array:
1086 * @array: array description
1088 * Frees the array description returned from mono_metadata_parse_array().
1091 mono_metadata_free_array (MonoArrayType *array)
1093 g_free (array->sizes);
1094 g_free (array->lobounds);
1099 * need to add common field and param attributes combinations:
1102 * public static literal
1105 * private static literal
1109 /* data, attrs, type, nmods, byref, pinned */
1110 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1111 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1112 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1113 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1114 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1115 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1116 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1117 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1118 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1119 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1120 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1121 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1122 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1123 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1124 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1125 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1126 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1127 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1128 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1129 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1130 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1131 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1132 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1133 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1134 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1135 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1136 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1137 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1138 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1139 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1140 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1141 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1144 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1146 static GHashTable *type_cache = NULL;
1149 * MonoTypes with modifies are never cached, so we never check or use that field.
1152 mono_type_hash (gconstpointer data)
1154 const MonoType *type = (const MonoType *) data;
1155 return type->type | (type->byref << 8) | (type->attrs << 9);
1159 mono_type_equal (gconstpointer ka, gconstpointer kb)
1161 const MonoType *a = (const MonoType *) ka;
1162 const MonoType *b = (const MonoType *) kb;
1164 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1166 /* need other checks */
1171 * mono_metadata_init:
1173 * Initialize the global variables of this module.
1176 mono_metadata_init (void)
1180 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1182 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1183 g_hash_table_insert (type_cache, &builtin_types [i], &builtin_types [i]);
1187 * mono_metadata_parse_type:
1188 * @m: metadata context
1189 * @mode: king of type that may be found at @ptr
1190 * @opt_attrs: optional attributes to store in the returned type
1191 * @ptr: pointer to the type representation
1192 * @rptr: pointer updated to match the end of the decoded stream
1194 * Decode a compressed type description found at @ptr in @m.
1195 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1196 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1197 * This function can be used to decode type descriptions in method signatures,
1198 * field signatures, locals signatures etc.
1200 * Returns: a #MonoType structure representing the decoded type.
1203 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs, const char *ptr, const char **rptr)
1205 MonoType *type, *cached;
1207 gboolean byref = FALSE;
1208 gboolean pinned = FALSE;
1209 const char *tmp_ptr;
1214 * According to the spec, custom modifiers should come before the byref
1215 * flag, but the IL produced by ilasm from the following signature:
1216 * object modopt(...) &
1217 * starts with a byref flag, followed by the modifiers. (bug #49802)
1218 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1219 * it would be better to treat byref as real type constructor instead of
1221 * Also, pinned should come before anything else, but some MSV++ produced
1222 * assemblies violate this (#bug 61990).
1225 /* Count the modifiers first */
1230 case MONO_TYPE_PINNED:
1231 case MONO_TYPE_BYREF:
1234 case MONO_TYPE_CMOD_REQD:
1235 case MONO_TYPE_CMOD_OPT:
1237 mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1245 type = g_malloc0 (sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1246 type->num_mods = count;
1248 g_warning ("got more than 64 modifiers in type");
1252 memset (type, 0, sizeof (MonoType));
1255 /* Parse pinned, byref and custom modifiers */
1260 case MONO_TYPE_PINNED:
1264 case MONO_TYPE_BYREF:
1268 case MONO_TYPE_CMOD_REQD:
1269 case MONO_TYPE_CMOD_OPT:
1271 mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1278 type->attrs = opt_attrs;
1279 type->byref = byref;
1280 type->pinned = pinned ? 1 : 0;
1282 do_mono_metadata_parse_type (type, m, ptr, &ptr);
1287 /* No need to use locking since nobody is modifying the hash table */
1288 if (mode != MONO_PARSE_PARAM && !type->num_mods && (cached = g_hash_table_lookup (type_cache, type))) {
1289 /* No need to free the contents of stype */
1293 type = g_memdup (&stype, sizeof (MonoType));
1299 * mono_metadata_parse_signature:
1300 * @image: metadata context
1301 * @toke: metadata token
1303 * Decode a method signature stored in the STANDALONESIG table
1305 * Returns: a MonoMethodSignature describing the signature.
1307 MonoMethodSignature *
1308 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1310 MonoTableInfo *tables = image->tables;
1311 guint32 idx = mono_metadata_token_index (token);
1316 return mono_lookup_dynamic_token (image, token);
1318 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1320 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1322 ptr = mono_metadata_blob_heap (image, sig);
1323 mono_metadata_decode_blob_size (ptr, &ptr);
1325 return mono_metadata_parse_method_signature (image, FALSE, ptr, NULL);
1328 MonoMethodSignature*
1329 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1331 MonoMethodSignature *sig;
1333 /* later we want to allocate signatures with mempools */
1334 sig = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1335 sig->param_count = nparams;
1336 sig->sentinelpos = -1;
1341 MonoMethodSignature*
1342 mono_metadata_signature_dup (MonoMethodSignature *sig)
1346 sigsize = sizeof (MonoMethodSignature) + sig->param_count * sizeof (MonoType *);
1347 return g_memdup (sig, sigsize);
1351 * mono_metadata_parse_method_signature:
1352 * @m: metadata context
1353 * @def: the MethodDef index or 0 for Ref signatures.
1354 * @ptr: pointer to the signature metadata representation
1355 * @rptr: pointer updated to match the end of the decoded stream
1357 * Decode a method signature stored at @ptr.
1359 * Returns: a MonoMethodSignature describing the signature.
1361 MonoMethodSignature *
1362 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1364 MonoMethodSignature *method;
1365 int i, ret_attrs = 0, *pattrs = NULL;
1366 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1367 guint32 gen_param_count = 0;
1370 gen_param_count = 1;
1375 call_convention = *ptr & 0x0F;
1377 if (gen_param_count)
1378 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1379 param_count = mono_metadata_decode_value (ptr, &ptr);
1380 pattrs = g_new0 (int, param_count);
1383 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1384 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1385 guint32 cols [MONO_PARAM_SIZE];
1386 guint lastp, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1388 if (def < methodt->rows)
1389 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1391 lastp = paramt->rows + 1;
1392 for (i = param_index; i < lastp; ++i) {
1393 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1394 if (!cols [MONO_PARAM_SEQUENCE])
1395 ret_attrs = cols [MONO_PARAM_FLAGS];
1397 pattrs [cols [MONO_PARAM_SEQUENCE] - 1] = cols [MONO_PARAM_FLAGS];
1400 method = mono_metadata_signature_alloc (m, param_count);
1401 method->hasthis = hasthis;
1402 method->explicit_this = explicit_this;
1403 method->call_convention = call_convention;
1404 method->generic_param_count = gen_param_count;
1405 if (call_convention != 0xa)
1406 method->ret = mono_metadata_parse_type (m, MONO_PARSE_RET, ret_attrs, ptr, &ptr);
1408 if (method->param_count) {
1409 method->sentinelpos = -1;
1411 for (i = 0; i < method->param_count; ++i) {
1412 if (*ptr == MONO_TYPE_SENTINEL) {
1413 if (method->call_convention != MONO_CALL_VARARG || def)
1414 g_error ("found sentinel for methoddef or no vararg method");
1415 method->sentinelpos = i;
1418 method->params [i] = mono_metadata_parse_type (m, MONO_PARSE_PARAM, pattrs [i], ptr, &ptr);
1422 if (def && (method->call_convention == MONO_CALL_VARARG))
1423 method->sentinelpos = method->param_count;
1430 * Add signature to a cache and increase ref count...
1436 * mono_metadata_free_method_signature:
1437 * @sig: signature to destroy
1439 * Free the memory allocated in the signature @sig.
1442 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1445 mono_metadata_free_type (sig->ret);
1446 for (i = 0; i < sig->param_count; ++i)
1447 mono_metadata_free_type (sig->params [i]);
1453 do_mono_metadata_parse_generic_inst (MonoType *type, MonoImage *m, const char *ptr, const char **rptr)
1455 MonoGenericInst *ginst = g_new0 (MonoGenericInst, 1);
1456 MonoGenericInst *cached;
1459 type->data.generic_inst = ginst;
1461 ginst->context = g_new0 (MonoGenericContext, 1);
1462 ginst->context->ginst = ginst;
1464 ginst->klass = g_new0 (MonoClass, 1);
1466 ginst->generic_type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1467 ginst->type_argc = count = mono_metadata_decode_value (ptr, &ptr);
1468 ginst->type_argv = g_new0 (MonoType*, count);
1471 * Create the klass before parsing the type arguments.
1472 * This is required to support "recursive" definitions.
1473 * See mcs/tests/gen-23.cs for an example.
1476 ginst->init_pending = TRUE;
1478 mono_class_create_generic (ginst);
1480 for (i = 0; i < ginst->type_argc; i++) {
1481 MonoType *t = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1483 ginst->type_argv [i] = t;
1484 if (!ginst->is_open)
1485 ginst->is_open = mono_class_is_open_constructed_type (t);
1493 * We may be called multiple times on different metadata to create the same
1494 * instantiated type. This happens for instance if we're part of a method or
1495 * local variable signature.
1497 * It's important to return the same MonoGenericInst * for each particualar
1498 * instantiation of a generic type (ie "Stack<Int32>") to make static fields
1501 * According to the spec ($26.1.5), a static variable in a generic class
1502 * declaration is shared amongst all instances of the same closed constructed
1506 cached = g_hash_table_lookup (m->generic_inst_cache, ginst);
1508 g_free (ginst->klass);
1509 g_free (ginst->type_argv);
1512 type->data.generic_inst = cached;
1515 g_hash_table_insert (m->generic_inst_cache, ginst, ginst);
1517 mono_stats.generic_instance_count++;
1518 mono_stats.generics_metadata_size += sizeof (MonoGenericInst) +
1519 sizeof (MonoGenericContext) +
1520 ginst->type_argc * sizeof (MonoType);
1523 ginst->init_pending = FALSE;
1526 static MonoGenericParam *
1527 mono_metadata_parse_generic_param (MonoImage *m, const char *ptr, const char **rptr)
1529 MonoGenericParam *generic_param = g_new0 (MonoGenericParam, 1);
1531 generic_param->num = mono_metadata_decode_value (ptr, &ptr);
1536 return generic_param;
1540 * do_mono_metadata_parse_type:
1541 * @type: MonoType to be filled in with the return value
1543 * Internal routine used to "fill" the contents of @type from an
1544 * allocated pointer. This is done this way to avoid doing too
1545 * many mini-allocations (particularly for the MonoFieldType which
1546 * most of the time is just a MonoType, but sometimes might be augmented).
1548 * This routine is used by mono_metadata_parse_type and
1549 * mono_metadata_parse_field_type
1551 * This extracts a Type as specified in Partition II (22.2.12)
1554 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, const char *ptr, const char **rptr)
1556 type->type = mono_metadata_decode_value (ptr, &ptr);
1558 switch (type->type){
1559 case MONO_TYPE_VOID:
1560 case MONO_TYPE_BOOLEAN:
1561 case MONO_TYPE_CHAR:
1574 case MONO_TYPE_STRING:
1575 case MONO_TYPE_OBJECT:
1576 case MONO_TYPE_TYPEDBYREF:
1578 case MONO_TYPE_VALUETYPE:
1579 case MONO_TYPE_CLASS: {
1581 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
1582 type->data.klass = mono_class_get (m, token);
1585 case MONO_TYPE_SZARRAY: {
1586 MonoType *etype = mono_metadata_parse_type (m, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1587 type->data.klass = mono_class_from_mono_type (etype);
1588 mono_metadata_free_type (etype);
1592 type->data.type = mono_metadata_parse_type (m, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1594 case MONO_TYPE_FNPTR:
1595 type->data.method = mono_metadata_parse_method_signature (m, 0, ptr, &ptr);
1597 case MONO_TYPE_ARRAY:
1598 type->data.array = mono_metadata_parse_array (m, ptr, &ptr);
1601 case MONO_TYPE_MVAR:
1603 type->data.generic_param = mono_metadata_parse_generic_param (m, ptr, &ptr);
1606 case MONO_TYPE_GENERICINST:
1607 do_mono_metadata_parse_generic_inst (type, m, ptr, &ptr);
1611 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
1619 * mono_metadata_free_type:
1620 * @type: type to free
1622 * Free the memory allocated for type @type.
1625 mono_metadata_free_type (MonoType *type)
1627 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
1629 switch (type->type){
1631 mono_metadata_free_type (type->data.type);
1633 case MONO_TYPE_FNPTR:
1634 mono_metadata_free_method_signature (type->data.method);
1636 case MONO_TYPE_ARRAY:
1637 mono_metadata_free_array (type->data.array);
1645 hex_dump (const char *buffer, int base, int count)
1647 int show_header = 1;
1655 for (i = 0; i < count; i++){
1658 printf ("\n0x%08x: ", (unsigned char) base + i);
1660 printf ("%02x ", (unsigned char) (buffer [i]));
1667 * @mh: The Method header
1668 * @ptr: Points to the beginning of the Section Data (25.3)
1671 parse_section_data (MonoMethodHeader *mh, const unsigned char *ptr)
1673 unsigned char sect_data_flags;
1674 const unsigned char *sptr;
1676 guint32 sect_data_len;
1679 /* align on 32-bit boundary */
1680 /* FIXME: not 64-bit clean code */
1681 sptr = ptr = dword_align (ptr);
1682 sect_data_flags = *ptr;
1685 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
1687 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
1690 sect_data_len = ptr [0];
1694 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
1695 hex_dump (sptr, 0, sect_data_len+8);
1696 g_print ("\nheader: ");
1697 hex_dump (sptr-4, 0, 4);
1701 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
1702 const unsigned char *p = dword_align (ptr);
1704 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
1705 /* we could just store a pointer if we don't need to byteswap */
1706 mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
1707 for (i = 0; i < mh->num_clauses; ++i) {
1708 MonoExceptionClause *ec = &mh->clauses [i];
1711 /* we could memcpy and byteswap */
1712 ec->flags = read32 (p);
1714 ec->try_offset = read32 (p);
1716 ec->try_len = read32 (p);
1718 ec->handler_offset = read32 (p);
1720 ec->handler_len = read32 (p);
1722 ec->token_or_filter = read32 (p);
1725 ec->flags = read16 (p);
1727 ec->try_offset = read16 (p);
1731 ec->handler_offset = read16 (p);
1733 ec->handler_len = *p;
1735 ec->token_or_filter = read32 (p);
1738 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
1742 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
1743 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
1750 * mono_metadata_parse_mh:
1751 * @m: metadata context
1752 * @ptr: pointer to the method header.
1754 * Decode the method header at @ptr, including pointer to the IL code,
1755 * info about local variables and optional exception tables.
1757 * Returns: a MonoMethodHeader.
1760 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
1762 MonoMethodHeader *mh;
1763 unsigned char flags = *(const unsigned char *) ptr;
1764 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
1766 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
1767 const unsigned char *code;
1770 g_return_val_if_fail (ptr != NULL, NULL);
1773 case METHOD_HEADER_TINY_FORMAT:
1774 mh = g_new0 (MonoMethodHeader, 1);
1777 local_var_sig_tok = 0;
1778 mh->code_size = flags >> 2;
1781 case METHOD_HEADER_TINY_FORMAT1:
1782 mh = g_new0 (MonoMethodHeader, 1);
1785 local_var_sig_tok = 0;
1788 * The spec claims 3 bits, but the Beta2 is
1791 mh->code_size = flags >> 2;
1794 case METHOD_HEADER_FAT_FORMAT:
1795 fat_flags = read16 (ptr);
1797 hsize = (fat_flags >> 12) & 0xf;
1798 max_stack = read16 (ptr);
1800 code_size = read32 (ptr);
1802 local_var_sig_tok = read32 (ptr);
1805 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
1812 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
1816 * There are more sections
1818 ptr = code + code_size;
1826 if (local_var_sig_tok) {
1827 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
1828 const char *locals_ptr;
1829 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
1830 int len=0, i, bsize;
1832 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
1833 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
1834 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
1835 if (*locals_ptr != 0x07)
1836 g_warning ("wrong signature for locals blob");
1838 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
1839 mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1840 mh->num_locals = len;
1841 for (i = 0; i < len; ++i)
1842 mh->locals [i] = mono_metadata_parse_type (m, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
1844 mh = g_new0 (MonoMethodHeader, 1);
1847 mh->code_size = code_size;
1848 mh->max_stack = max_stack;
1849 mh->init_locals = init_locals;
1850 if (fat_flags & METHOD_HEADER_MORE_SECTS)
1851 parse_section_data (mh, (const unsigned char*)ptr);
1856 * mono_metadata_free_mh:
1857 * @mh: a method header
1859 * Free the memory allocated for the method header.
1862 mono_metadata_free_mh (MonoMethodHeader *mh)
1865 for (i = 0; i < mh->num_locals; ++i)
1866 mono_metadata_free_type (mh->locals[i]);
1867 g_free (mh->clauses);
1872 * mono_metadata_parse_field_type:
1873 * @m: metadata context to extract information from
1874 * @ptr: pointer to the field signature
1875 * @rptr: pointer updated to match the end of the decoded stream
1877 * Parses the field signature, and returns the type information for it.
1879 * Returns: The MonoType that was extracted from @ptr.
1882 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
1884 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
1888 * mono_metadata_parse_param:
1889 * @m: metadata context to extract information from
1890 * @ptr: pointer to the param signature
1891 * @rptr: pointer updated to match the end of the decoded stream
1893 * Parses the param signature, and returns the type information for it.
1895 * Returns: The MonoType that was extracted from @ptr.
1898 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
1900 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
1904 * mono_metadata_token_from_dor:
1905 * @dor_token: A TypeDefOrRef coded index
1907 * dor_token is a TypeDefOrRef coded index: it contains either
1908 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
1909 * bits contain an index into the table.
1911 * Returns: an expanded token
1914 mono_metadata_token_from_dor (guint32 dor_index)
1918 table = dor_index & 0x03;
1919 idx = dor_index >> 2;
1922 case 0: /* TypeDef */
1923 return MONO_TOKEN_TYPE_DEF | idx;
1925 case 1: /* TypeRef */
1926 return MONO_TOKEN_TYPE_REF | idx;
1928 case 2: /* TypeSpec */
1929 return MONO_TOKEN_TYPE_SPEC | idx;
1932 g_assert_not_reached ();
1939 * We use this to pass context information to the row locator
1942 int idx; /* The index that we are trying to locate */
1943 int col_idx; /* The index in the row where idx may be stored */
1944 MonoTableInfo *t; /* pointer to the table */
1949 * How the row locator works.
1954 * ___|___------> _______
1957 * A column in the rows of table A references an index in table B.
1958 * For example A may be the TYPEDEF table and B the METHODDEF table.
1960 * Given an index in table B we want to get the row in table A
1961 * where the column n references our index in B.
1963 * In the locator_t structure:
1965 * col_idx is the column number
1966 * index is the index in table B
1967 * result will be the index in table A
1970 * Table A Table B column (in table A)
1971 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
1972 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
1973 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
1974 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
1975 * METHODSEM PROPERTY ASSOCIATION (encoded index)
1977 * Note that we still don't support encoded indexes.
1981 typedef_locator (const void *a, const void *b)
1983 locator_t *loc = (locator_t *) a;
1984 const char *bb = (const char *) b;
1985 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
1986 guint32 col, col_next;
1988 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
1994 * Need to check that the next row is valid.
1996 if (typedef_index + 1 < loc->t->rows) {
1997 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
1998 if (loc->idx >= col_next)
2001 if (col == col_next)
2005 loc->result = typedef_index;
2011 table_locator (const void *a, const void *b)
2013 locator_t *loc = (locator_t *) a;
2014 const char *bb = (const char *) b;
2015 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2018 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2020 if (loc->idx == col) {
2021 loc->result = table_index;
2031 * mono_metadata_typedef_from_field:
2032 * @meta: metadata context
2033 * @index: FieldDef token
2035 * Returns the 1-based index into the TypeDef table of the type that
2036 * declared the field described by @index.
2037 * Returns 0 if not found.
2040 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
2042 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2048 loc.idx = mono_metadata_token_index (index);
2049 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
2052 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2053 g_assert_not_reached ();
2055 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2056 return loc.result + 1;
2060 * mono_metadata_typedef_from_method:
2061 * @meta: metadata context
2062 * @index: MethodDef token
2064 * Returns the 1-based index into the TypeDef table of the type that
2065 * declared the method described by @index.
2066 * Returns 0 if not found.
2069 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
2071 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2077 loc.idx = mono_metadata_token_index (index);
2078 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
2081 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2082 g_assert_not_reached ();
2084 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2085 return loc.result + 1;
2089 * mono_metadata_interfaces_from_typedef:
2090 * @meta: metadata context
2091 * @index: typedef token
2093 * Returns and array of interfaces that the @index typedef token implements.
2094 * The number of elemnts in the array is returned in @count.
2097 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
2099 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
2102 guint32 cols [MONO_INTERFACEIMPL_SIZE];
2110 loc.idx = mono_metadata_token_index (index);
2111 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
2114 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2119 * We may end up in the middle of the rows...
2122 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
2129 while (start < tdef->rows) {
2130 mono_metadata_decode_row (tdef, start, cols, MONO_INTERFACEIMPL_SIZE);
2131 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
2133 result = g_renew (MonoClass*, result, i + 1);
2134 result [i] = mono_class_get (meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]));
2142 * mono_metadata_nested_in_typedef:
2143 * @meta: metadata context
2144 * @index: typedef token
2146 * Returns: the 1-based index into the TypeDef table of the type
2147 * where the type described by @index is nested.
2148 * Retruns 0 if @index describes a non-nested type.
2151 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
2153 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2159 loc.idx = mono_metadata_token_index (index);
2160 loc.col_idx = MONO_NESTED_CLASS_NESTED;
2163 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2166 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2167 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
2171 * mono_metadata_nesting_typedef:
2172 * @meta: metadata context
2173 * @index: typedef token
2175 * Returns: the 1-based index into the TypeDef table of the first type
2176 * that is nested inside the type described by @index. The search starts at
2178 * Returns 0 if no such type is found.
2181 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
2183 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2185 guint32 class_index = mono_metadata_token_index (index);
2190 start = start_index;
2192 while (start <= tdef->rows) {
2193 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
2199 if (start > tdef->rows)
2206 * mono_metadata_packing_from_typedef:
2207 * @meta: metadata context
2208 * @index: token representing a type
2210 * Returns the info stored in the ClassLAyout table for the given typedef token
2211 * into the @packing and @size pointers.
2212 * Returns 0 if the info is not found.
2215 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
2217 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
2219 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
2224 loc.idx = mono_metadata_token_index (index);
2225 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
2228 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2231 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
2233 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
2235 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
2237 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2238 return loc.result + 1;
2242 * mono_metadata_custom_attrs_from_index:
2243 * @meta: metadata context
2244 * @index: token representing the parent
2246 * Returns: the 1-based index into the CustomAttribute table of the first
2247 * attribute which belongs to the metadata object described by @index.
2248 * Returns 0 if no such attribute is found.
2251 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
2253 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
2260 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
2263 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2266 /* Find the first entry by searching backwards */
2267 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
2270 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2271 return loc.result + 1;
2276 mono_backtrace (int limit)
2281 backtrace (array, limit);
2282 names = backtrace_symbols (array, limit);
2283 for (i =0; i < limit; ++i) {
2284 g_print ("\t%s\n", names [i]);
2291 /*#define __alignof__(a) sizeof(a)*/
2292 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
2297 * @t: the type to return the size of
2299 * Returns: the number of bytes required to hold an instance of this
2303 mono_type_size (MonoType *t, gint *align)
2310 *align = __alignof__(gpointer);
2311 return sizeof (gpointer);
2315 case MONO_TYPE_VOID:
2318 case MONO_TYPE_BOOLEAN:
2319 *align = __alignof__(gint8);
2323 *align = __alignof__(gint8);
2325 case MONO_TYPE_CHAR:
2328 *align = __alignof__(gint16);
2332 *align = __alignof__(gint32);
2335 *align = __alignof__(float);
2339 *align = __alignof__(gint64);
2342 *align = __alignof__(double);
2346 *align = __alignof__(gpointer);
2347 return sizeof (gpointer);
2348 case MONO_TYPE_STRING:
2349 *align = __alignof__(gpointer);
2350 return sizeof (gpointer);
2351 case MONO_TYPE_OBJECT:
2352 *align = __alignof__(gpointer);
2353 return sizeof (gpointer);
2354 case MONO_TYPE_VALUETYPE: {
2355 if (t->data.klass->enumtype)
2356 return mono_type_size (t->data.klass->enum_basetype, align);
2358 return mono_class_value_size (t->data.klass, align);
2360 case MONO_TYPE_CLASS:
2361 case MONO_TYPE_SZARRAY:
2363 case MONO_TYPE_FNPTR:
2364 case MONO_TYPE_ARRAY:
2365 *align = __alignof__(gpointer);
2366 return sizeof (gpointer);
2367 case MONO_TYPE_TYPEDBYREF:
2368 return mono_class_value_size (mono_defaults.typed_reference_class, align);
2369 case MONO_TYPE_GENERICINST: {
2370 MonoGenericInst *ginst = t->data.generic_inst;
2372 g_assert (!ginst->is_open && !ginst->klass->gen_params);
2374 if (MONO_TYPE_ISSTRUCT (ginst->generic_type)) {
2375 MonoClass *gklass = mono_class_from_mono_type (ginst->generic_type);
2377 if (gklass->enumtype)
2378 return mono_type_size (gklass->enum_basetype, align);
2380 return mono_class_value_size (ginst->klass, align);
2382 *align = __alignof__(gpointer);
2383 return sizeof (gpointer);
2387 case MONO_TYPE_MVAR:
2388 *align = __alignof__(gpointer);
2389 return sizeof (gpointer);
2391 g_error ("mono_type_size: type 0x%02x unknown", t->type);
2397 * mono_type_stack_size:
2398 * @t: the type to return the size it uses on the stack
2400 * Returns: the number of bytes required to hold an instance of this
2401 * type on the runtime stack
2404 mono_type_stack_size (MonoType *t, gint *align)
2408 g_assert (t != NULL);
2414 *align = __alignof__(gpointer);
2415 return sizeof (gpointer);
2419 case MONO_TYPE_BOOLEAN:
2420 case MONO_TYPE_CHAR:
2429 case MONO_TYPE_STRING:
2430 case MONO_TYPE_OBJECT:
2431 case MONO_TYPE_CLASS:
2432 case MONO_TYPE_SZARRAY:
2434 case MONO_TYPE_FNPTR:
2435 case MONO_TYPE_ARRAY:
2436 *align = __alignof__(gpointer);
2437 return sizeof (gpointer);
2438 case MONO_TYPE_TYPEDBYREF:
2439 *align = __alignof__(gpointer);
2440 return sizeof (gpointer) * 3;
2442 *align = __alignof__(float);
2443 return sizeof (float);
2446 *align = __alignof__(gint64);
2447 return sizeof (gint64);
2449 *align = __alignof__(double);
2450 return sizeof (double);
2451 case MONO_TYPE_VALUETYPE: {
2454 if (t->data.klass->enumtype)
2455 return mono_type_stack_size (t->data.klass->enum_basetype, align);
2457 size = mono_class_value_size (t->data.klass, align);
2459 *align = *align + __alignof__(gpointer) - 1;
2460 *align &= ~(__alignof__(gpointer) - 1);
2462 size += sizeof (gpointer) - 1;
2463 size &= ~(sizeof (gpointer) - 1);
2468 case MONO_TYPE_GENERICINST: {
2469 MonoGenericInst *ginst = t->data.generic_inst;
2471 g_assert (!ginst->is_open && !ginst->klass->gen_params);
2473 if (MONO_TYPE_ISSTRUCT (ginst->generic_type)) {
2474 MonoClass *gklass = mono_class_from_mono_type (ginst->generic_type);
2476 if (gklass->enumtype)
2477 return mono_type_stack_size (gklass->enum_basetype, align);
2479 guint32 size = mono_class_value_size (ginst->klass, align);
2481 *align = *align + __alignof__(gpointer) - 1;
2482 *align &= ~(__alignof__(gpointer) - 1);
2484 size += sizeof (gpointer) - 1;
2485 size &= ~(sizeof (gpointer) - 1);
2490 *align = __alignof__(gpointer);
2491 return sizeof (gpointer);
2495 g_error ("type 0x%02x unknown", t->type);
2501 mono_metadata_generic_inst_is_valuetype (MonoGenericInst *ginst)
2503 return MONO_TYPE_ISSTRUCT (ginst->generic_type);
2507 mono_metadata_generic_inst_hash (MonoGenericInst *ginst)
2509 return mono_metadata_type_hash (ginst->generic_type);
2513 mono_metadata_generic_inst_equal (MonoGenericInst *g1, MonoGenericInst *g2)
2517 if (g1->type_argc != g2->type_argc)
2519 if (!mono_metadata_type_equal (g1->generic_type, g2->generic_type))
2521 for (i = 0; i < g1->type_argc; ++i) {
2522 if (!mono_metadata_type_equal (g1->type_argv [i], g2->type_argv [i]))
2529 * mono_metadata_type_hash:
2532 * Computes an hash value for @t1 to be used in GHashTable.
2535 mono_metadata_type_hash (MonoType *t1)
2537 guint hash = t1->type;
2539 hash |= t1->byref << 6; /* do not collide with t1->type values */
2541 case MONO_TYPE_VALUETYPE:
2542 case MONO_TYPE_CLASS:
2543 case MONO_TYPE_SZARRAY:
2544 /* check if the distribution is good enough */
2545 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
2547 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
2548 case MONO_TYPE_ARRAY:
2549 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
2550 case MONO_TYPE_GENERICINST:
2551 return ((hash << 5) - hash) ^ mono_metadata_generic_inst_hash (t1->data.generic_inst);
2557 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2)
2561 if (c1->generic_inst && c2->generic_inst)
2562 return mono_metadata_generic_inst_equal (c1->generic_inst, c2->generic_inst);
2563 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
2564 return c1->byval_arg.data.generic_param->num == c2->byval_arg.data.generic_param->num;
2565 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
2566 return c1->byval_arg.data.generic_param->num == c2->byval_arg.data.generic_param->num;
2571 * mono_metadata_type_equal:
2575 * Determine if @t1 and @t2 represent the same type.
2576 * Returns: #TRUE if @t1 and @t2 are equal.
2579 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
2581 if (t1->type != t2->type ||
2582 t1->byref != t2->byref)
2586 case MONO_TYPE_VOID:
2587 case MONO_TYPE_BOOLEAN:
2588 case MONO_TYPE_CHAR:
2599 case MONO_TYPE_STRING:
2602 case MONO_TYPE_OBJECT:
2603 case MONO_TYPE_TYPEDBYREF:
2605 case MONO_TYPE_VALUETYPE:
2606 case MONO_TYPE_CLASS:
2607 case MONO_TYPE_SZARRAY:
2608 return mono_metadata_class_equal (t1->data.klass, t2->data.klass);
2610 return mono_metadata_type_equal (t1->data.type, t2->data.type);
2611 case MONO_TYPE_ARRAY:
2612 if (t1->data.array->rank != t2->data.array->rank)
2614 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass);
2615 case MONO_TYPE_GENERICINST:
2616 return mono_metadata_generic_inst_equal (t1->data.generic_inst,
2617 t2->data.generic_inst);
2619 case MONO_TYPE_MVAR:
2620 return t1->data.generic_param->num == t2->data.generic_param->num;
2622 g_error ("implement type compare for %0x!", t1->type);
2630 * mono_metadata_signature_equal:
2631 * @sig1: a signature
2632 * @sig2: another signature
2634 * Determine if @sig1 and @sig2 represent the same signature, with the
2635 * same number of arguments and the same types.
2636 * Returns: #TRUE if @sig1 and @sig2 are equal.
2639 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
2643 if (sig1->hasthis != sig2->hasthis ||
2644 sig1->param_count != sig2->param_count)
2647 for (i = 0; i < sig1->param_count; i++) {
2648 MonoType *p1 = sig1->params[i];
2649 MonoType *p2 = sig2->params[i];
2651 /* if (p1->attrs != p2->attrs)
2654 if (!mono_metadata_type_equal (p1, p2))
2658 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
2664 mono_signature_hash (MonoMethodSignature *sig)
2666 guint i, res = sig->ret->type;
2668 for (i = 0; i < sig->param_count; i++)
2669 res = (res << 5) - res + sig->params[i]->type;
2675 * mono_metadata_encode_value:
2676 * @value: value to encode
2677 * @buf: buffer where to write the compressed representation
2678 * @endbuf: pointer updated to point at the end of the encoded output
2680 * Encodes the value @value in the compressed representation used
2681 * in metadata and stores the result in @buf. @buf needs to be big
2682 * enough to hold the data (4 bytes).
2685 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
2691 else if (value < 0x4000) {
2692 p [0] = 0x80 | (value >> 8);
2693 p [1] = value & 0xff;
2696 p [0] = (value >> 24) | 0xc0;
2697 p [1] = (value >> 16) & 0xff;
2698 p [2] = (value >> 8) & 0xff;
2699 p [3] = value & 0xff;
2707 * mono_metadata_field_info:
2708 * @meta: the Image the field is defined in
2709 * @index: the index in the field table representing the field
2710 * @offset: a pointer to an integer where to store the offset that
2711 * may have been specified for the field in a FieldLayout table
2712 * @rva: a pointer to the RVA of the field data in the image that
2713 * may have been defined in a FieldRVA table
2714 * @marshal_spec: a pointer to the marshal spec that may have been
2715 * defined for the field in a FieldMarshal table.
2717 * Gather info for field @index that may have been defined in the FieldLayout,
2718 * FieldRVA and FieldMarshal tables.
2719 * Either of offset, rva and marshal_spec can be NULL if you're not interested
2723 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
2724 MonoMarshalSpec **marshal_spec)
2726 MonoTableInfo *tdef;
2729 loc.idx = index + 1;
2731 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
2733 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
2736 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2737 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
2739 *offset = (guint32)-1;
2743 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
2745 loc.col_idx = MONO_FIELD_RVA_FIELD;
2748 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2750 * LAMESPEC: There is no signature, no nothing, just the raw data.
2752 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
2760 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
2761 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
2768 * mono_metadata_get_constant_index:
2769 * @meta: the Image the field is defined in
2770 * @index: the token that may have a row defined in the constants table
2771 * @hint: possible position for the row
2773 * @token must be a FieldDef, ParamDef or PropertyDef token.
2775 * Returns: the index into the Constants table or 0 if not found.
2778 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
2780 MonoTableInfo *tdef;
2782 guint32 index = mono_metadata_token_index (token);
2784 tdef = &meta->tables [MONO_TABLE_CONSTANT];
2785 index <<= MONO_HASCONSTANT_BITS;
2786 switch (mono_metadata_token_table (token)) {
2787 case MONO_TABLE_FIELD:
2788 index |= MONO_HASCONSTANT_FIEDDEF;
2790 case MONO_TABLE_PARAM:
2791 index |= MONO_HASCONSTANT_PARAM;
2793 case MONO_TABLE_PROPERTY:
2794 index |= MONO_HASCONSTANT_PROPERTY;
2797 g_warning ("Not a valid token for the constant table: 0x%08x", token);
2801 loc.col_idx = MONO_CONSTANT_PARENT;
2804 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
2807 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2808 return loc.result + 1;
2814 * mono_metadata_events_from_typedef:
2815 * @meta: metadata context
2816 * @index: 0-based index (in the TypeDef table) describing a type
2818 * Returns: the 0-based index in the Event table for the events in the
2819 * type. The last event that belongs to the type (plus 1) is stored
2820 * in the @end_idx pointer.
2823 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
2827 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
2835 loc.col_idx = MONO_EVENT_MAP_PARENT;
2836 loc.idx = index + 1;
2838 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2841 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
2842 if (loc.result + 1 < tdef->rows) {
2843 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
2845 end = meta->tables [MONO_TABLE_EVENT].rows;
2853 * mono_metadata_methods_from_event:
2854 * @meta: metadata context
2855 * @index: 0-based index (in the Event table) describing a event
2857 * Returns: the 0-based index in the MethodDef table for the methods in the
2858 * event. The last method that belongs to the event (plus 1) is stored
2859 * in the @end_idx pointer.
2862 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
2866 guint32 cols [MONO_METHOD_SEMA_SIZE];
2867 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
2874 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
2875 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
2877 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
2882 * We may end up in the middle of the rows...
2885 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
2891 while (end < msemt->rows) {
2892 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
2893 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
2902 * mono_metadata_properties_from_typedef:
2903 * @meta: metadata context
2904 * @index: 0-based index (in the TypeDef table) describing a type
2906 * Returns: the 0-based index in the Property table for the properties in the
2907 * type. The last property that belongs to the type (plus 1) is stored
2908 * in the @end_idx pointer.
2911 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
2915 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
2923 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
2924 loc.idx = index + 1;
2926 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2929 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
2930 if (loc.result + 1 < tdef->rows) {
2931 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
2933 end = meta->tables [MONO_TABLE_PROPERTY].rows;
2941 * mono_metadata_methods_from_property:
2942 * @meta: metadata context
2943 * @index: 0-based index (in the PropertyDef table) describing a property
2945 * Returns: the 0-based index in the MethodDef table for the methods in the
2946 * property. The last method that belongs to the property (plus 1) is stored
2947 * in the @end_idx pointer.
2950 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
2954 guint32 cols [MONO_METHOD_SEMA_SIZE];
2955 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
2962 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
2963 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
2965 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
2970 * We may end up in the middle of the rows...
2973 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
2979 while (end < msemt->rows) {
2980 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
2981 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
2990 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
2993 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
2999 loc.col_idx = MONO_IMPLMAP_MEMBER;
3000 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
3002 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3005 return loc.result + 1;
3009 * @image: context where the image is created
3010 * @type_spec: typespec token
3012 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
3016 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
3018 guint32 idx = mono_metadata_token_index (type_spec);
3020 guint32 cols [MONO_TYPESPEC_SIZE];
3025 mono_loader_lock ();
3027 if ((type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec)))) {
3028 mono_loader_unlock ();
3032 t = &image->tables [MONO_TABLE_TYPESPEC];
3034 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
3035 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
3036 len = mono_metadata_decode_value (ptr, &ptr);
3038 type = g_new0 (MonoType, 1);
3040 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
3042 if (*ptr == MONO_TYPE_BYREF) {
3047 do_mono_metadata_parse_type (type, image, ptr, &ptr);
3049 mono_loader_unlock ();
3055 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
3057 MonoMarshalSpec *res;
3059 const char *start = ptr;
3061 /* fixme: this is incomplete, but I cant find more infos in the specs */
3063 res = g_new0 (MonoMarshalSpec, 1);
3065 len = mono_metadata_decode_value (ptr, &ptr);
3066 res->native = *ptr++;
3068 if (res->native == MONO_NATIVE_LPARRAY) {
3069 if (ptr - start <= len)
3070 res->data.array_data.elem_type = *ptr++;
3071 if (ptr - start <= len)
3072 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
3073 if (ptr - start <= len)
3074 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3077 if (res->native == MONO_NATIVE_BYVALTSTR) {
3078 if (ptr - start <= len)
3079 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3082 if (res->native == MONO_NATIVE_BYVALARRAY) {
3083 if (ptr - start <= len)
3084 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3087 if (res->native == MONO_NATIVE_CUSTOM) {
3088 /* skip unused type guid */
3089 len = mono_metadata_decode_value (ptr, &ptr);
3091 /* skip unused native type name */
3092 len = mono_metadata_decode_value (ptr, &ptr);
3094 /* read custom marshaler type name */
3095 len = mono_metadata_decode_value (ptr, &ptr);
3096 res->data.custom_data.custom_name = g_strndup (ptr, len);
3098 /* read cookie string */
3099 len = mono_metadata_decode_value (ptr, &ptr);
3100 res->data.custom_data.cookie = g_strndup (ptr, len);
3107 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
3109 if (spec->native == MONO_NATIVE_CUSTOM) {
3110 g_free (spec->data.custom_data.custom_name);
3111 g_free (spec->data.custom_data.cookie);
3117 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
3118 gboolean unicode, MonoMarshalConv *conv)
3120 MonoMarshalConv dummy_conv;
3126 *conv = MONO_MARSHAL_CONV_NONE;
3129 return MONO_NATIVE_UINT;
3133 case MONO_TYPE_BOOLEAN:
3135 switch (mspec->native) {
3136 case MONO_NATIVE_VARIANTBOOL:
3137 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
3138 return MONO_NATIVE_VARIANTBOOL;
3139 case MONO_NATIVE_BOOLEAN:
3140 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3141 return MONO_NATIVE_BOOLEAN;
3142 case MONO_NATIVE_I1:
3143 case MONO_NATIVE_U1:
3144 return mspec->native;
3146 g_error ("cant marshal bool to native type %02x", mspec->native);
3149 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3150 return MONO_NATIVE_BOOLEAN;
3151 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
3152 case MONO_TYPE_I1: return MONO_NATIVE_I1;
3153 case MONO_TYPE_U1: return MONO_NATIVE_U1;
3154 case MONO_TYPE_I2: return MONO_NATIVE_I2;
3155 case MONO_TYPE_U2: return MONO_NATIVE_U2;
3156 case MONO_TYPE_I4: return MONO_NATIVE_I4;
3157 case MONO_TYPE_U4: return MONO_NATIVE_U4;
3158 case MONO_TYPE_I8: return MONO_NATIVE_I8;
3159 case MONO_TYPE_U8: return MONO_NATIVE_U8;
3160 case MONO_TYPE_R4: return MONO_NATIVE_R4;
3161 case MONO_TYPE_R8: return MONO_NATIVE_R8;
3162 case MONO_TYPE_STRING:
3164 switch (mspec->native) {
3165 case MONO_NATIVE_BSTR:
3166 *conv = MONO_MARSHAL_CONV_STR_BSTR;
3167 return MONO_NATIVE_BSTR;
3168 case MONO_NATIVE_LPSTR:
3169 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
3170 return MONO_NATIVE_LPSTR;
3171 case MONO_NATIVE_LPWSTR:
3172 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
3173 return MONO_NATIVE_LPWSTR;
3174 case MONO_NATIVE_LPTSTR:
3175 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3176 return MONO_NATIVE_LPTSTR;
3177 case MONO_NATIVE_ANSIBSTR:
3178 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
3179 return MONO_NATIVE_ANSIBSTR;
3180 case MONO_NATIVE_TBSTR:
3181 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
3182 return MONO_NATIVE_TBSTR;
3183 case MONO_NATIVE_BYVALTSTR:
3185 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
3187 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
3188 return MONO_NATIVE_BYVALTSTR;
3190 g_error ("cant marshal string to native type %02x", mspec->native);
3193 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3194 return MONO_NATIVE_LPTSTR;
3195 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
3196 case MONO_TYPE_VALUETYPE: /*FIXME*/
3197 if (type->data.klass->enumtype) {
3198 t = type->data.klass->enum_basetype->type;
3201 return MONO_NATIVE_STRUCT;
3202 case MONO_TYPE_SZARRAY:
3203 case MONO_TYPE_ARRAY:
3205 switch (mspec->native) {
3206 case MONO_NATIVE_BYVALARRAY:
3207 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
3208 return MONO_NATIVE_BYVALARRAY;
3209 case MONO_NATIVE_SAFEARRAY:
3210 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
3211 return MONO_NATIVE_SAFEARRAY;
3212 case MONO_NATIVE_LPARRAY:
3213 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3214 return MONO_NATIVE_LPARRAY;
3216 g_error ("cant marshal array as native type %02x", mspec->native);
3220 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3221 return MONO_NATIVE_LPARRAY;
3222 case MONO_TYPE_I: return MONO_NATIVE_INT;
3223 case MONO_TYPE_U: return MONO_NATIVE_UINT;
3224 case MONO_TYPE_CLASS:
3225 case MONO_TYPE_OBJECT: {
3226 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
3228 switch (mspec->native) {
3229 case MONO_NATIVE_STRUCT:
3230 return MONO_NATIVE_STRUCT;
3231 case MONO_NATIVE_INTERFACE:
3232 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
3233 return MONO_NATIVE_INTERFACE;
3234 case MONO_NATIVE_IDISPATCH:
3235 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
3236 return MONO_NATIVE_IDISPATCH;
3237 case MONO_NATIVE_IUNKNOWN:
3238 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
3239 return MONO_NATIVE_IUNKNOWN;
3240 case MONO_NATIVE_FUNC:
3241 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3242 type->data.klass == mono_defaults.delegate_class ||
3243 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3244 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3245 return MONO_NATIVE_FUNC;
3251 g_error ("cant marshal object as native type %02x", mspec->native);
3254 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3255 type->data.klass == mono_defaults.delegate_class ||
3256 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3257 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3258 return MONO_NATIVE_FUNC;
3260 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
3261 return MONO_NATIVE_STRUCT;
3263 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
3264 case MONO_TYPE_TYPEDBYREF:
3266 g_error ("type 0x%02x not handled in marshal", t);
3268 return MONO_NATIVE_MAX;
3272 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
3275 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
3281 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
3282 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
3284 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3287 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
3291 method_from_method_def_or_ref (MonoImage *m, guint32 tok)
3293 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
3294 switch (tok & MONO_METHODDEFORREF_MASK) {
3295 case MONO_METHODDEFORREF_METHODDEF:
3296 return mono_get_method (m, MONO_TOKEN_METHOD_DEF | idx, NULL);
3297 case MONO_METHODDEFORREF_METHODREF:
3298 return mono_get_method (m, MONO_TOKEN_MEMBER_REF | idx, NULL);
3300 g_assert_not_reached ();
3305 mono_class_get_overrides (MonoImage *image, guint32 type_token, gint32 *num_overrides)
3308 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
3311 guint32 cols [MONO_METHODIMPL_SIZE];
3312 MonoMethod **result;
3321 loc.col_idx = MONO_METHODIMPL_CLASS;
3322 loc.idx = mono_metadata_token_index (type_token);
3324 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3330 * We may end up in the middle of the rows...
3333 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
3338 while (end < tdef->rows) {
3339 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
3345 result = g_new (MonoMethod*, num * 2);
3346 for (i = 0; i < num; ++i) {
3347 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
3348 result [i * 2] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_DECLARATION]);
3349 result [i * 2 + 1] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_BODY]);
3353 *num_overrides = num;
3358 * mono_guid_to_string:
3360 * Converts a 16 byte Microsoft GUID to the standard string representation.
3363 mono_guid_to_string (const guint8 *guid)
3365 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
3366 guid[3], guid[2], guid[1], guid[0],
3370 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
3374 get_constraints (MonoImage *image, int owner)
3376 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
3377 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
3378 guint32 i, token, found;
3379 MonoClass *klass, **res;
3380 GList *cons = NULL, *tmp;
3384 for (i = 0; i < tdef->rows; ++i) {
3385 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
3386 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
3387 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
3388 klass = mono_class_get (image, token);
3389 cons = g_list_append (cons, klass);
3392 /* contiguous list finished */
3399 res = g_new0 (MonoClass*, found + 1);
3400 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
3401 res [i] = tmp->data;
3408 mono_metadata_load_generic_params (MonoImage *image, guint32 token, guint32 *num)
3410 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
3411 guint32 cols [MONO_GENERICPARAM_SIZE];
3412 guint32 i, owner, last_num, n;
3413 MonoGenericParam *params;
3415 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
3416 owner = MONO_TYPEORMETHOD_TYPE;
3417 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3418 owner = MONO_TYPEORMETHOD_METHOD;
3420 g_error ("wrong token %x to load_generics_params", token);
3422 owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
3428 for (i = 0; i < tdef->rows; ++i) {
3429 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3430 if (cols [MONO_GENERICPARAM_OWNER] == owner)
3434 if (i >= tdef->rows)
3440 params = g_realloc (params, sizeof (MonoGenericParam) * n);
3441 params [n - 1].pklass = NULL;
3442 params [n - 1].method = NULL;
3443 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
3444 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
3445 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
3446 params [n - 1].constraints = get_constraints (image, i + 1);
3447 if (++i >= tdef->rows)
3449 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3450 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
3458 mono_type_is_byref (MonoType *type)
3464 mono_type_get_type (MonoType *type)
3469 /* For MONO_TYPE_FNPTR */
3470 MonoMethodSignature*
3471 mono_type_get_signature (MonoType *type)
3473 return type->data.method;
3476 /* For MONO_TYPE_CLASS, VALUETYPE */
3478 mono_type_get_class (MonoType *type)
3480 return type->data.klass;
3483 /* For MONO_TYPE_ARRAY */
3485 mono_type_get_array_type (MonoType *type)
3487 return type->data.array;
3491 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
3493 /* FIXME: implement */
3498 mono_signature_get_return_type (MonoMethodSignature *sig)
3504 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
3510 /* start from the first */
3511 if (sig->param_count) {
3512 *iter = &sig->params [0];
3513 return sig->params [0];
3521 if (type < &sig->params [sig->param_count]) {
3529 mono_signature_get_param_count (MonoMethodSignature *sig)
3531 return sig->param_count;
3535 mono_signature_get_call_conv (MonoMethodSignature *sig)
3537 return sig->call_convention;
3541 mono_signature_vararg_start (MonoMethodSignature *sig)
3543 return sig->sentinelpos;
3547 mono_signature_is_instance (MonoMethodSignature *sig)
3549 return sig->hasthis;
3553 mono_signature_explicit_this (MonoMethodSignature *sig)
3555 return sig->explicit_this;