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.
16 #include "tabledefs.h"
17 #include "mono-endian.h"
19 #include "tokentype.h"
20 #include "metadata-internals.h"
21 #include "class-internals.h"
25 static void do_mono_metadata_parse_type (MonoType *type, MonoImage *m, const char *ptr, const char **rptr);
28 * Encoding of the "description" argument:
30 * identifier [CODE ARG]
32 * If CODE is ':', then a lookup on table ARG is performed
33 * If CODE is '=', then a lookup in the aliased-table ARG is performed
34 * If CODE is '#', then this encodes a flag, ARG is the flag name.
36 * Aliased table for example is `CustomAttributeType' which depending on the
37 * information might refer to different tables.
40 const static MonoMetaTable AssemblySchema [] = {
41 { MONO_MT_UINT32, "HashId" },
42 { MONO_MT_UINT16, "Major" },
43 { MONO_MT_UINT16, "Minor" },
44 { MONO_MT_UINT16, "BuildNumber" },
45 { MONO_MT_UINT16, "RevisionNumber" },
46 { MONO_MT_UINT32, "Flags" },
47 { MONO_MT_BLOB_IDX, "PublicKey" },
48 { MONO_MT_STRING_IDX, "Name" },
49 { MONO_MT_STRING_IDX, "Culture" },
53 const static MonoMetaTable AssemblyOSSchema [] = {
54 { MONO_MT_UINT32, "OSPlatformID" },
55 { MONO_MT_UINT32, "OSMajor" },
56 { MONO_MT_UINT32, "OSMinor" },
60 const static MonoMetaTable AssemblyProcessorSchema [] = {
61 { MONO_MT_UINT32, "Processor" },
65 const static MonoMetaTable AssemblyRefSchema [] = {
66 { MONO_MT_UINT16, "Major" },
67 { MONO_MT_UINT16, "Minor" },
68 { MONO_MT_UINT16, "Build" },
69 { MONO_MT_UINT16, "Revision" },
70 { MONO_MT_UINT32, "Flags" },
71 { MONO_MT_BLOB_IDX, "PublicKeyOrToken" },
72 { MONO_MT_STRING_IDX, "Name" },
73 { MONO_MT_STRING_IDX, "Culture" },
74 { MONO_MT_BLOB_IDX, "HashValue" },
78 const static MonoMetaTable AssemblyRefOSSchema [] = {
79 { MONO_MT_UINT32, "OSPlatformID" },
80 { MONO_MT_UINT32, "OSMajorVersion" },
81 { MONO_MT_UINT32, "OSMinorVersion" },
82 { MONO_MT_TABLE_IDX, "AssemblyRef:AssemblyRef" },
86 const static MonoMetaTable AssemblyRefProcessorSchema [] = {
87 { MONO_MT_UINT32, "Processor" },
88 { MONO_MT_TABLE_IDX, "AssemblyRef:AssemblyRef" },
92 const static MonoMetaTable ClassLayoutSchema [] = {
93 { MONO_MT_UINT16, "PackingSize" },
94 { MONO_MT_UINT32, "ClassSize" },
95 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
99 const static MonoMetaTable ConstantSchema [] = {
100 { MONO_MT_UINT8, "Type" },
101 { MONO_MT_UINT8, "PaddingZero" },
102 { MONO_MT_CONST_IDX, "Parent" },
103 { MONO_MT_BLOB_IDX, "Value" },
104 { MONO_MT_END, NULL }
107 const static MonoMetaTable CustomAttributeSchema [] = {
108 { MONO_MT_HASCAT_IDX, "Parent" },
109 { MONO_MT_CAT_IDX, "Type" },
110 { MONO_MT_BLOB_IDX, "Value" },
111 { MONO_MT_END, NULL }
114 const static MonoMetaTable DeclSecuritySchema [] = {
115 { MONO_MT_UINT16, "Action" },
116 { MONO_MT_HASDEC_IDX, "Parent" },
117 { MONO_MT_BLOB_IDX, "PermissionSet" },
118 { MONO_MT_END, NULL }
121 const static MonoMetaTable EventMapSchema [] = {
122 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
123 { MONO_MT_TABLE_IDX, "EventList:Event" },
124 { MONO_MT_END, NULL }
127 const static MonoMetaTable EventSchema [] = {
128 { MONO_MT_UINT16, "EventFlags#EventAttribute" },
129 { MONO_MT_STRING_IDX, "Name" },
130 { MONO_MT_TABLE_IDX, "EventType" }, /* TypeDef or TypeRef */
131 { MONO_MT_END, NULL }
134 const static MonoMetaTable ExportedTypeSchema [] = {
135 { MONO_MT_UINT32, "Flags" },
136 { MONO_MT_TABLE_IDX, "TypeDefId" },
137 { MONO_MT_STRING_IDX, "TypeName" },
138 { MONO_MT_STRING_IDX, "TypeNameSpace" },
139 { MONO_MT_IMPL_IDX, "Implementation" },
140 { MONO_MT_END, NULL }
143 const static MonoMetaTable FieldSchema [] = {
144 { MONO_MT_UINT16, "Flags" },
145 { MONO_MT_STRING_IDX, "Name" },
146 { MONO_MT_BLOB_IDX, "Signature" },
147 { MONO_MT_END, NULL }
149 const static MonoMetaTable FieldLayoutSchema [] = {
150 { MONO_MT_UINT32, "Offset" },
151 { MONO_MT_TABLE_IDX, "Field:Field" },
152 { MONO_MT_END, NULL }
155 const static MonoMetaTable FieldMarshalSchema [] = {
156 { MONO_MT_HFM_IDX, "Parent" },
157 { MONO_MT_BLOB_IDX, "NativeType" },
158 { MONO_MT_END, NULL }
160 const static MonoMetaTable FieldRVASchema [] = {
161 { MONO_MT_UINT32, "RVA" },
162 { MONO_MT_TABLE_IDX, "Field:Field" },
163 { MONO_MT_END, NULL }
166 const static MonoMetaTable FileSchema [] = {
167 { MONO_MT_UINT32, "Flags" },
168 { MONO_MT_STRING_IDX, "Name" },
169 { MONO_MT_BLOB_IDX, "Value" },
170 { MONO_MT_END, NULL }
173 const static MonoMetaTable ImplMapSchema [] = {
174 { MONO_MT_UINT16, "MappingFlag" },
175 { MONO_MT_MF_IDX, "MemberForwarded" },
176 { MONO_MT_STRING_IDX, "ImportName" },
177 { MONO_MT_TABLE_IDX, "ImportScope:ModuleRef" },
178 { MONO_MT_END, NULL }
181 const static MonoMetaTable InterfaceImplSchema [] = {
182 { MONO_MT_TABLE_IDX, "Class:TypeDef" },
183 { MONO_MT_TDOR_IDX, "Interface=TypeDefOrRef" },
184 { MONO_MT_END, NULL }
187 const static MonoMetaTable ManifestResourceSchema [] = {
188 { MONO_MT_UINT32, "Offset" },
189 { MONO_MT_UINT32, "Flags" },
190 { MONO_MT_STRING_IDX, "Name" },
191 { MONO_MT_IMPL_IDX, "Implementation" },
192 { MONO_MT_END, NULL }
195 const static MonoMetaTable MemberRefSchema [] = {
196 { MONO_MT_MRP_IDX, "Class" },
197 { MONO_MT_STRING_IDX, "Name" },
198 { MONO_MT_BLOB_IDX, "Signature" },
199 { MONO_MT_END, NULL }
202 const static MonoMetaTable MethodSchema [] = {
203 { MONO_MT_UINT32, "RVA" },
204 { MONO_MT_UINT16, "ImplFlags#MethodImplAttributes" },
205 { MONO_MT_UINT16, "Flags#MethodAttribute" },
206 { MONO_MT_STRING_IDX, "Name" },
207 { MONO_MT_BLOB_IDX, "Signature" },
208 { MONO_MT_TABLE_IDX, "ParamList:Param" },
209 { MONO_MT_END, NULL }
212 const static MonoMetaTable MethodImplSchema [] = {
213 { MONO_MT_TABLE_IDX, "Class:TypeDef" },
214 { MONO_MT_MDOR_IDX, "MethodBody" },
215 { MONO_MT_MDOR_IDX, "MethodDeclaration" },
216 { MONO_MT_END, NULL }
219 const static MonoMetaTable MethodSemanticsSchema [] = {
220 { MONO_MT_UINT16, "MethodSemantic" },
221 { MONO_MT_TABLE_IDX, "Method:Method" },
222 { MONO_MT_HS_IDX, "Association" },
223 { MONO_MT_END, NULL }
226 const static MonoMetaTable ModuleSchema [] = {
227 { MONO_MT_UINT16, "Generation" },
228 { MONO_MT_STRING_IDX, "Name" },
229 { MONO_MT_GUID_IDX, "MVID" },
230 { MONO_MT_GUID_IDX, "EncID" },
231 { MONO_MT_GUID_IDX, "EncBaseID" },
232 { MONO_MT_END, NULL }
235 const static MonoMetaTable ModuleRefSchema [] = {
236 { MONO_MT_STRING_IDX, "Name" },
237 { MONO_MT_END, NULL }
240 const static MonoMetaTable NestedClassSchema [] = {
241 { MONO_MT_TABLE_IDX, "NestedClass:TypeDef" },
242 { MONO_MT_TABLE_IDX, "EnclosingClass:TypeDef" },
243 { MONO_MT_END, NULL }
246 const static MonoMetaTable ParamSchema [] = {
247 { MONO_MT_UINT16, "Flags" },
248 { MONO_MT_UINT16, "Sequence" },
249 { MONO_MT_STRING_IDX, "Name" },
250 { MONO_MT_END, NULL }
253 const static MonoMetaTable PropertySchema [] = {
254 { MONO_MT_UINT16, "Flags" },
255 { MONO_MT_STRING_IDX, "Name" },
256 { MONO_MT_BLOB_IDX, "Type" },
257 { MONO_MT_END, NULL }
260 const static MonoMetaTable PropertyMapSchema [] = {
261 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
262 { MONO_MT_TABLE_IDX, "PropertyList:Property" },
263 { MONO_MT_END, NULL }
266 const static MonoMetaTable StandaloneSigSchema [] = {
267 { MONO_MT_BLOB_IDX, "Signature" },
268 { MONO_MT_END, NULL }
271 const static MonoMetaTable TypeDefSchema [] = {
272 { MONO_MT_UINT32, "Flags" },
273 { MONO_MT_STRING_IDX, "Name" },
274 { MONO_MT_STRING_IDX, "Namespace" },
275 { MONO_MT_TDOR_IDX, "Extends" },
276 { MONO_MT_TABLE_IDX, "FieldList:Field" },
277 { MONO_MT_TABLE_IDX, "MethodList:Method" },
278 { MONO_MT_END, NULL }
281 const static MonoMetaTable TypeRefSchema [] = {
282 { MONO_MT_RS_IDX, "ResolutionScope=ResolutionScope" },
283 { MONO_MT_STRING_IDX, "Name" },
284 { MONO_MT_STRING_IDX, "Namespace" },
285 { MONO_MT_END, NULL }
288 const static MonoMetaTable TypeSpecSchema [] = {
289 { MONO_MT_BLOB_IDX, "Signature" },
290 { MONO_MT_END, NULL }
293 const static MonoMetaTable GenericParamSchema [] = {
294 { MONO_MT_UINT16, "Number" },
295 { MONO_MT_UINT16, "Flags" },
296 { MONO_MT_TABLE_IDX, "Owner" }, /* TypeDef or MethodDef */
297 { MONO_MT_STRING_IDX, "Name" },
299 /* soon to be removed */
300 { MONO_MT_TABLE_IDX, "Kind" },
302 { MONO_MT_END, NULL }
305 const static MonoMetaTable MethodSpecSchema [] = {
306 { MONO_MT_MDOR_IDX, "Method" },
307 { MONO_MT_BLOB_IDX, "Signature" },
308 { MONO_MT_END, NULL }
311 const static MonoMetaTable GenericParamConstraintSchema [] = {
312 { MONO_MT_TABLE_IDX, "GenericParam" },
313 { MONO_MT_TDOR_IDX, "Constraint" },
314 { MONO_MT_END, NULL }
317 const static struct {
318 const MonoMetaTable *table;
321 /* 0 */ { ModuleSchema, "Module" },
322 /* 1 */ { TypeRefSchema, "TypeRef" },
323 /* 2 */ { TypeDefSchema, "TypeDef" },
324 /* 3 */ { NULL, NULL },
325 /* 4 */ { FieldSchema, "Field" },
326 /* 5 */ { NULL, NULL },
327 /* 6 */ { MethodSchema, "Method" },
328 /* 7 */ { NULL, NULL },
329 /* 8 */ { ParamSchema, "Param" },
330 /* 9 */ { InterfaceImplSchema, "InterfaceImpl" },
331 /* A */ { MemberRefSchema, "MemberRef" },
332 /* B */ { ConstantSchema, "Constant" },
333 /* C */ { CustomAttributeSchema, "CustomAttribute" },
334 /* D */ { FieldMarshalSchema, "FieldMarshal" },
335 /* E */ { DeclSecuritySchema, "DeclSecurity" },
336 /* F */ { ClassLayoutSchema, "ClassLayout" },
337 /* 10 */ { FieldLayoutSchema, "FieldLayout" },
338 /* 11 */ { StandaloneSigSchema, "StandaloneSig" },
339 /* 12 */ { EventMapSchema, "EventMap" },
340 /* 13 */ { NULL, NULL },
341 /* 14 */ { EventSchema, "Event" },
342 /* 15 */ { PropertyMapSchema, "PropertyMap" },
343 /* 16 */ { NULL, NULL },
344 /* 17 */ { PropertySchema, "PropertyTable" },
345 /* 18 */ { MethodSemanticsSchema, "MethodSemantics" },
346 /* 19 */ { MethodImplSchema, "MethodImpl" },
347 /* 1A */ { ModuleRefSchema, "ModuleRef" },
348 /* 1B */ { TypeSpecSchema, "TypeSpec" },
349 /* 1C */ { ImplMapSchema, "ImplMap" },
350 /* 1D */ { FieldRVASchema, "FieldRVA" },
351 /* 1E */ { NULL, NULL },
352 /* 1F */ { NULL, NULL },
353 /* 20 */ { AssemblySchema, "Assembly" },
354 /* 21 */ { AssemblyProcessorSchema, "AssemblyProcessor" },
355 /* 22 */ { AssemblyOSSchema, "AssemblyOS" },
356 /* 23 */ { AssemblyRefSchema, "AssemblyRef" },
357 /* 24 */ { AssemblyRefProcessorSchema, "AssemblyRefProcessor" },
358 /* 25 */ { AssemblyRefOSSchema, "AssemblyRefOS" },
359 /* 26 */ { FileSchema, "File" },
360 /* 27 */ { ExportedTypeSchema, "ExportedType" },
361 /* 28 */ { ManifestResourceSchema, "ManifestResource" },
362 /* 29 */ { NestedClassSchema, "NestedClass" },
363 /* 2A */ { GenericParamSchema, "GenericParam" },
364 /* 2B */ { MethodSpecSchema, "MethodSpec" },
365 /* 2C */ { GenericParamConstraintSchema, "GenericParamConstraint" },
369 * mono_meta_table_name:
370 * @table: table index
372 * Returns the name for the @table index
375 mono_meta_table_name (int table)
377 if ((table < 0) || (table > 0x2c))
380 return tables [table].name;
383 /* The guy who wrote the spec for this should not be allowed near a
386 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
387 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
388 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
389 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
390 inverse of this mapping.
393 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
394 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
396 /* Reference: Partition II - 23.2.6 */
398 * mono_metadata_compute_size:
399 * @meta: metadata context
400 * @tableindex: metadata table number
401 * @result_bitfield: pointer to guint32 where to store additional info
403 * mono_metadata_compute_size() computes the lenght in bytes of a single
404 * row in a metadata table. The size of each column is encoded in the
405 * @result_bitfield return value along with the number of columns in the table.
406 * the resulting bitfield should be handed to the mono_metadata_table_size()
407 * and mono_metadata_table_count() macros.
410 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
412 guint32 bitfield = 0;
413 int size = 0, field_size;
416 const MonoMetaTable *table = tables [tableindex].table;
418 for (i = 0; (code = table [i].code) != MONO_MT_END; i++){
421 field_size = 4; break;
424 field_size = 2; break;
427 field_size = 1; break;
429 case MONO_MT_BLOB_IDX:
430 field_size = meta->idx_blob_wide ? 4 : 2; break;
432 case MONO_MT_STRING_IDX:
433 field_size = meta->idx_string_wide ? 4 : 2; break;
435 case MONO_MT_GUID_IDX:
436 field_size = meta->idx_guid_wide ? 4 : 2; break;
438 case MONO_MT_TABLE_IDX:
439 /* Uhm, a table index can point to other tables besides the current one
440 * so, it's not correct to use the rowcount of the current table to
441 * get the size for this column - lupus
443 switch (tableindex) {
444 case MONO_TABLE_ASSEMBLYREFOS:
446 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
447 case MONO_TABLE_ASSEMBLYPROCESSOR:
449 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
450 case MONO_TABLE_CLASSLAYOUT:
452 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
453 case MONO_TABLE_EVENTMAP:
454 g_assert (i == 0 || i == 1);
455 field_size = i ? idx_size (MONO_TABLE_EVENT):
456 idx_size(MONO_TABLE_TYPEDEF);
458 case MONO_TABLE_EVENT:
460 field_size = MAX (idx_size (MONO_TABLE_TYPEDEF), idx_size(MONO_TABLE_TYPEREF));
461 field_size = MAX (field_size, idx_size(MONO_TABLE_TYPESPEC));
463 case MONO_TABLE_EXPORTEDTYPE:
465 /* the index is in another metadata file, so it must be 4 */
466 field_size = 4; break;
467 case MONO_TABLE_FIELDLAYOUT:
469 field_size = idx_size (MONO_TABLE_FIELD); break;
470 case MONO_TABLE_FIELDRVA:
472 field_size = idx_size (MONO_TABLE_FIELD); break;
473 case MONO_TABLE_IMPLMAP:
475 field_size = idx_size (MONO_TABLE_MODULEREF); break;
476 case MONO_TABLE_INTERFACEIMPL:
478 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
479 case MONO_TABLE_METHOD:
481 field_size = idx_size (MONO_TABLE_PARAM); break;
482 case MONO_TABLE_METHODIMPL:
484 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
485 case MONO_TABLE_METHODSEMANTICS:
487 field_size = idx_size (MONO_TABLE_METHOD); break;
488 case MONO_TABLE_NESTEDCLASS:
489 g_assert (i == 0 || i == 1);
490 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
491 case MONO_TABLE_PROPERTYMAP:
492 g_assert (i == 0 || i == 1);
493 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
494 idx_size(MONO_TABLE_TYPEDEF);
496 case MONO_TABLE_TYPEDEF:
497 g_assert (i == 4 || i == 5);
498 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
499 idx_size(MONO_TABLE_METHOD);
500 case MONO_TABLE_GENERICPARAM:
501 g_assert (i == 2 || i == 4 || i == 5);
503 field_size = MAX (idx_size (MONO_TABLE_METHOD), idx_size (MONO_TABLE_TYPEDEF));
505 field_size = idx_size (MONO_TABLE_TYPEDEF);
507 field_size = idx_size (MONO_TABLE_TYPEDEF);
510 case MONO_TABLE_GENERICPARAMCONSTRAINT:
512 field_size = idx_size (MONO_TABLE_GENERICPARAM);
516 g_assert_not_reached ();
518 if (tableindex != MONO_TABLE_EXPORTEDTYPE && field_size != idx_size (tableindex))
519 g_warning ("size changed (%d to %d)", idx_size (tableindex), field_size);
524 * HasConstant: ParamDef, FieldDef, Property
526 case MONO_MT_CONST_IDX:
527 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
528 meta->tables [MONO_TABLE_FIELD].rows);
529 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
531 /* 2 bits to encode tag */
532 field_size = rtsize (n, 16-2);
536 * HasCustomAttribute: points to any table but
539 case MONO_MT_HASCAT_IDX:
541 * We believe that since the signature and
542 * permission are indexing the Blob heap,
543 * we should consider the blob size first
545 /* I'm not a believer - lupus
546 if (meta->idx_blob_wide){
551 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
552 meta->tables [MONO_TABLE_FIELD].rows);
553 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
554 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
555 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
556 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
557 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
558 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
559 n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
560 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
561 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
562 n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
563 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
564 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
565 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
566 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
567 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
568 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
569 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
571 /* 5 bits to encode */
572 field_size = rtsize (n, 16-5);
576 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
577 * MemberRef and String.
579 case MONO_MT_CAT_IDX:
580 /* String is a heap, if it is wide, we know the size */
582 if (meta->idx_string_wide){
587 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
588 meta->tables [MONO_TABLE_TYPEDEF].rows);
589 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
590 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
592 /* 3 bits to encode */
593 field_size = rtsize (n, 16-3);
597 * HasDeclSecurity: Typedef, MethodDef, Assembly
599 case MONO_MT_HASDEC_IDX:
600 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
601 meta->tables [MONO_TABLE_METHOD].rows);
602 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
604 /* 2 bits to encode */
605 field_size = rtsize (n, 16-2);
609 * Implementation: File, AssemblyRef, ExportedType
611 case MONO_MT_IMPL_IDX:
612 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
613 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
614 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
616 /* 2 bits to encode tag */
617 field_size = rtsize (n, 16-2);
621 * HasFieldMarshall: FieldDef, ParamDef
623 case MONO_MT_HFM_IDX:
624 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
625 meta->tables [MONO_TABLE_PARAM].rows);
627 /* 1 bit used to encode tag */
628 field_size = rtsize (n, 16-1);
632 * MemberForwarded: FieldDef, MethodDef
635 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
636 meta->tables [MONO_TABLE_METHOD].rows);
638 /* 1 bit used to encode tag */
639 field_size = rtsize (n, 16-1);
643 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
645 * It is TypeDef, _TypeRef_, TypeSpec, instead.
647 case MONO_MT_TDOR_IDX:
648 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
649 meta->tables [MONO_TABLE_TYPEREF].rows);
650 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
652 /* 2 bits to encode */
653 field_size = rtsize (n, 16-2);
657 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
659 case MONO_MT_MRP_IDX:
660 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
661 meta->tables [MONO_TABLE_TYPEREF].rows);
662 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
663 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
664 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
665 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
667 /* 3 bits to encode */
668 field_size = rtsize (n, 16 - 3);
671 case MONO_MT_MDOR_IDX:
674 * MethodDefOrRef: MethodDef, MemberRef
677 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
678 meta->tables [MONO_TABLE_MEMBERREF].rows);
680 /* 1 bit used to encode tag */
681 field_size = rtsize (n, 16-1);
685 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
688 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
689 meta->tables [MONO_TABLE_MODULEREF].rows);
690 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
691 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
693 /* 2 bits used to encode tag (ECMA spec claims 3) */
694 field_size = rtsize (n, 16 - 2);
699 * encode field size as follows (we just need to
706 bitfield |= (field_size-1) << shift;
709 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
712 *result_bitfield = (i << 24) | bitfield;
717 * mono_metadata_compute_table_bases:
718 * @meta: metadata context to compute table values
720 * Computes the table bases for the metadata structure.
721 * This is an internal function used by the image loader code.
724 mono_metadata_compute_table_bases (MonoImage *meta)
727 const char *base = meta->tables_base;
729 for (i = 0; i < 64; i++){
730 if (meta->tables [i].rows == 0)
733 meta->tables [i].row_size = mono_metadata_compute_size (
734 meta, i, &meta->tables [i].size_bitfield);
735 meta->tables [i].base = base;
736 base += meta->tables [i].rows * meta->tables [i].row_size;
741 * mono_metadata_locate:
742 * @meta: metadata context
743 * @table: table code.
744 * @idx: index of element to retrieve from @table.
746 * Returns a pointer to the @idx element in the metadata table
747 * whose code is @table.
750 mono_metadata_locate (MonoImage *meta, int table, int idx)
752 /* idx == 0 refers always to NULL */
753 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
755 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
759 * mono_metadata_locate_token:
760 * @meta: metadata context
761 * @token: metadata token
763 * Returns a pointer to the data in the metadata represented by the
767 mono_metadata_locate_token (MonoImage *meta, guint32 token)
769 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
773 * mono_metadata_get_table:
774 * @table: table to retrieve
776 * Returns the MonoMetaTable structure for table @table
778 const MonoMetaTable *
779 mono_metadata_get_table (MonoMetaTableEnum table)
783 g_return_val_if_fail ((x > 0) && (x <= MONO_TABLE_LAST), NULL);
785 return tables [table].table;
789 * mono_metadata_string_heap:
790 * @meta: metadata context
791 * @index: index into the string heap.
793 * Returns: an in-memory pointer to the @index in the string heap.
796 mono_metadata_string_heap (MonoImage *meta, guint32 index)
798 g_return_val_if_fail (index < meta->heap_strings.size, "");
799 return meta->heap_strings.data + index;
803 * mono_metadata_user_string:
804 * @meta: metadata context
805 * @index: index into the user string heap.
807 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
810 mono_metadata_user_string (MonoImage *meta, guint32 index)
812 g_return_val_if_fail (index < meta->heap_us.size, "");
813 return meta->heap_us.data + index;
817 * mono_metadata_blob_heap:
818 * @meta: metadata context
819 * @index: index into the blob.
821 * Returns: an in-memory pointer to the @index in the Blob heap.
824 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
826 g_return_val_if_fail (index < meta->heap_blob.size, "");
827 return meta->heap_blob.data + index;
831 * mono_metadata_guid_heap:
832 * @meta: metadata context
833 * @index: index into the guid heap.
835 * Returns: an in-memory pointer to the @index in the guid heap.
838 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
841 index *= 16; /* adjust for guid size and 1-based index */
842 g_return_val_if_fail (index < meta->heap_guid.size, "");
843 return meta->heap_guid.data + index;
847 dword_align (const char *ptr)
849 #if SIZEOF_VOID_P == 8
850 return (const char *) (((guint64) (ptr + 3)) & ~3);
852 return (const char *) (((guint32) (ptr + 3)) & ~3);
857 * mono_metadata_decode_row:
858 * @t: table to extract information from.
859 * @idx: index in table.
860 * @res: array of @res_size cols to store the results in
862 * This decompresses the metadata element @idx in table @t
863 * into the guint32 @res array that has res_size elements
866 mono_metadata_decode_row (MonoTableInfo *t, int idx, guint32 *res, int res_size)
868 guint32 bitfield = t->size_bitfield;
869 int i, count = mono_metadata_table_count (bitfield);
870 const char *data = t->base + idx * t->row_size;
872 g_assert (res_size == count);
874 for (i = 0; i < count; i++){
875 int n = mono_metadata_table_size (bitfield, i);
879 res [i] = *data; break;
881 res [i] = read16 (data); break;
884 res [i] = read32 (data); break;
887 g_assert_not_reached ();
894 * mono_metadata_decode_row_col:
895 * @t: table to extract information from.
896 * @idx: index for row in table.
897 * @col: column in the row.
899 * This function returns the value of column @col from the @idx
900 * row in the table @t.
903 mono_metadata_decode_row_col (MonoTableInfo *t, int idx, guint col)
905 guint32 bitfield = t->size_bitfield;
907 register const char *data = t->base + idx * t->row_size;
910 g_assert (col < mono_metadata_table_count (bitfield));
912 n = mono_metadata_table_size (bitfield, 0);
913 for (i = 0; i < col; ++i) {
915 n = mono_metadata_table_size (bitfield, i + 1);
921 return read16 (data);
923 return read32 (data);
925 g_assert_not_reached ();
930 * mono_metadata_decode_blob_size:
931 * @ptr: pointer to a blob object
932 * @rptr: the new position of the pointer
934 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
936 * Returns: the size of the blob object
939 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
941 const unsigned char *ptr = (const unsigned char *)xptr;
944 if ((*ptr & 0x80) == 0){
945 size = ptr [0] & 0x7f;
947 } else if ((*ptr & 0x40) == 0){
948 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
951 size = ((ptr [0] & 0x1f) << 24) +
964 * mono_metadata_decode_value:
965 * @ptr: pointer to decode from
966 * @rptr: the new position of the pointer
968 * This routine decompresses 32-bit values as specified in the "Blob and
969 * Signature" section (22.2)
971 * Returns: the decoded value
974 mono_metadata_decode_value (const char *_ptr, const char **rptr)
976 const unsigned char *ptr = (const unsigned char *) _ptr;
977 unsigned char b = *ptr;
980 if ((b & 0x80) == 0){
983 } else if ((b & 0x40) == 0){
984 len = ((b & 0x3f) << 8 | ptr [1]);
987 len = ((b & 0x1f) << 24) |
1000 * mono_metadata_parse_typedef_or_ref:
1001 * @m: a metadata context.
1002 * @ptr: a pointer to an encoded TypedefOrRef in @m
1003 * @rptr: pointer updated to match the end of the decoded stream
1005 * Returns: a token valid in the @m metadata decoded from
1006 * the compressed representation.
1009 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1012 token = mono_metadata_decode_value (ptr, &ptr);
1015 return mono_metadata_token_from_dor (token);
1019 * mono_metadata_parse_custom_mod:
1020 * @m: a metadata context.
1021 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1022 * @ptr: a pointer to (possibly) the start of a custom modifier list
1023 * @rptr: pointer updated to match the end of the decoded stream
1025 * Checks if @ptr points to a type custom modifier compressed representation.
1027 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1030 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1032 MonoCustomMod local;
1033 if ((*ptr == MONO_TYPE_CMOD_OPT) ||
1034 (*ptr == MONO_TYPE_CMOD_REQD)) {
1037 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1038 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1045 * mono_metadata_parse_array:
1046 * @m: a metadata context.
1047 * @ptr: a pointer to an encoded array description.
1048 * @rptr: pointer updated to match the end of the decoded stream
1050 * Decodes the compressed array description found in the metadata @m at @ptr.
1052 * Returns: a #MonoArrayType structure describing the array type
1056 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1059 MonoArrayType *array = g_new0 (MonoArrayType, 1);
1062 etype = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1063 array->eklass = mono_class_from_mono_type (etype);
1064 array->rank = mono_metadata_decode_value (ptr, &ptr);
1066 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1067 if (array->numsizes)
1068 array->sizes = g_new0 (int, array->numsizes);
1069 for (i = 0; i < array->numsizes; ++i)
1070 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1072 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1073 if (array->numlobounds)
1074 array->lobounds = g_new0 (int, array->numlobounds);
1075 for (i = 0; i < array->numlobounds; ++i)
1076 array->lobounds [i] = mono_metadata_decode_value (ptr, &ptr);
1084 * mono_metadata_free_array:
1085 * @array: array description
1087 * Frees the array description returned from mono_metadata_parse_array().
1090 mono_metadata_free_array (MonoArrayType *array)
1092 g_free (array->sizes);
1093 g_free (array->lobounds);
1098 * need to add common field and param attributes combinations:
1101 * public static literal
1104 * private static literal
1108 /* data, attrs, type, nmods, byref, pinned */
1109 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1110 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1111 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1112 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1113 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1114 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1115 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1116 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1117 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1118 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1119 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1120 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1121 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1122 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1123 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1124 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1125 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1126 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1127 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1128 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1129 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1130 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1131 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1132 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1133 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1134 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1135 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1136 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1137 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1138 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1139 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1140 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1143 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1145 static GHashTable *type_cache = NULL;
1148 * MonoTypes with modifies are never cached, so we never check or use that field.
1151 mono_type_hash (gconstpointer data)
1153 const MonoType *type = (const MonoType *) data;
1154 return type->type | (type->byref << 8) | (type->attrs << 9);
1158 mono_type_equal (gconstpointer ka, gconstpointer kb)
1160 const MonoType *a = (const MonoType *) ka;
1161 const MonoType *b = (const MonoType *) kb;
1163 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1165 /* need other checks */
1170 * mono_metadata_init:
1172 * Initialize the global variables of this module.
1175 mono_metadata_init (void)
1179 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1181 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1182 g_hash_table_insert (type_cache, &builtin_types [i], &builtin_types [i]);
1186 * mono_metadata_parse_type:
1187 * @m: metadata context
1188 * @mode: king of type that may be found at @ptr
1189 * @opt_attrs: optional attributes to store in the returned type
1190 * @ptr: pointer to the type representation
1191 * @rptr: pointer updated to match the end of the decoded stream
1193 * Decode a compressed type description found at @ptr in @m.
1194 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1195 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1196 * This function can be used to decode type descriptions in method signatures,
1197 * field signatures, locals signatures etc.
1199 * Returns: a #MonoType structure representing the decoded type.
1202 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs, const char *ptr, const char **rptr)
1204 MonoType *type, *cached;
1205 gboolean byref = FALSE;
1208 * According to the spec, custom modifiers should come before the byref
1209 * flag, but the IL produced by ilasm from the following signature:
1210 * object modopt(...) &
1211 * starts with a byref flag, followed by the modifiers. (bug #49802)
1212 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1213 * it would be better to treat byref as real type constructor instead of
1216 if (*ptr == MONO_TYPE_BYREF) {
1222 case MONO_PARSE_MOD_TYPE:
1223 case MONO_PARSE_PARAM:
1224 case MONO_PARSE_RET:
1225 case MONO_PARSE_LOCAL: /* should not have modifiers according to the spec, but ms tools disagree */
1226 case MONO_PARSE_FIELD: {
1227 /* count the modifiers */
1228 const char *tmp_ptr = ptr;
1230 while (mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr))
1233 type = g_malloc0 (sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1234 type->num_mods = count;
1236 g_warning ("got more than 64 modifiers in type");
1237 /* save them this time */
1239 while (mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr))
1242 } /* fall through */
1244 case MONO_PARSE_TYPE:
1246 * Later we can avoid doing this allocation.
1248 type = g_new0 (MonoType, 1);
1251 g_assert_not_reached ();
1254 type->attrs = opt_attrs;
1255 type->byref = byref;
1256 if (mode == MONO_PARSE_LOCAL) {
1258 * check for pinned flag
1260 if (*ptr == MONO_TYPE_PINNED) {
1267 case MONO_TYPE_BYREF:
1268 if (mode == MONO_PARSE_FIELD)
1269 g_warning ("A field type cannot be byref");
1272 /* follow through */
1274 /*if (*ptr == MONO_TYPE_VOID && mode != MONO_PARSE_RET)
1275 g_error ("void not allowed in param");*/
1276 do_mono_metadata_parse_type (type, m, ptr, &ptr);
1282 /* No need to use locking since nobody is modifying the hash table */
1283 if (mode != MONO_PARSE_PARAM && !type->num_mods && (cached = g_hash_table_lookup (type_cache, type))) {
1284 mono_metadata_free_type (type);
1292 * mono_metadata_parse_signature:
1293 * @image: metadata context
1294 * @toke: metadata token
1296 * Decode a method signature stored in the STANDALONESIG table
1298 * Returns: a MonoMethodSignature describing the signature.
1300 MonoMethodSignature *
1301 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1303 MonoTableInfo *tables = image->tables;
1304 guint32 idx = mono_metadata_token_index (token);
1309 return mono_lookup_dynamic_token (image, token);
1311 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1313 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1315 ptr = mono_metadata_blob_heap (image, sig);
1316 mono_metadata_decode_blob_size (ptr, &ptr);
1318 return mono_metadata_parse_method_signature (image, FALSE, ptr, NULL);
1321 MonoMethodSignature*
1322 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1324 MonoMethodSignature *sig;
1326 /* later we want to allocate signatures with mempools */
1327 sig = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1328 sig->param_count = nparams;
1329 sig->sentinelpos = -1;
1334 MonoMethodSignature*
1335 mono_metadata_signature_dup (MonoMethodSignature *sig)
1339 sigsize = sizeof (MonoMethodSignature) + sig->param_count * sizeof (MonoType *);
1340 return g_memdup (sig, sigsize);
1344 * mono_metadata_parse_method_signature:
1345 * @m: metadata context
1346 * @def: the MethodDef index or 0 for Ref signatures.
1347 * @ptr: pointer to the signature metadata representation
1348 * @rptr: pointer updated to match the end of the decoded stream
1350 * Decode a method signature stored at @ptr.
1352 * Returns: a MonoMethodSignature describing the signature.
1354 MonoMethodSignature *
1355 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1357 MonoMethodSignature *method;
1358 int i, ret_attrs = 0, *pattrs = NULL;
1359 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1360 guint32 gen_param_count = 0;
1363 gen_param_count = 1;
1368 call_convention = *ptr & 0x0F;
1370 if (gen_param_count)
1371 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1372 param_count = mono_metadata_decode_value (ptr, &ptr);
1373 pattrs = g_new0 (int, param_count);
1376 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1377 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1378 guint32 cols [MONO_PARAM_SIZE];
1379 guint lastp, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1381 if (def < methodt->rows)
1382 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1384 lastp = paramt->rows + 1;
1385 for (i = param_index; i < lastp; ++i) {
1386 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1387 if (!cols [MONO_PARAM_SEQUENCE])
1388 ret_attrs = cols [MONO_PARAM_FLAGS];
1390 pattrs [cols [MONO_PARAM_SEQUENCE] - 1] = cols [MONO_PARAM_FLAGS];
1393 method = mono_metadata_signature_alloc (m, param_count);
1394 method->hasthis = hasthis;
1395 method->explicit_this = explicit_this;
1396 method->call_convention = call_convention;
1397 method->generic_param_count = gen_param_count;
1398 if (call_convention != 0xa)
1399 method->ret = mono_metadata_parse_type (m, MONO_PARSE_RET, ret_attrs, ptr, &ptr);
1401 if (method->param_count) {
1402 method->sentinelpos = -1;
1404 for (i = 0; i < method->param_count; ++i) {
1405 if (*ptr == MONO_TYPE_SENTINEL) {
1406 if (method->call_convention != MONO_CALL_VARARG || def)
1407 g_error ("found sentinel for methoddef or no vararg method");
1408 method->sentinelpos = i;
1411 method->params [i] = mono_metadata_parse_type (m, MONO_PARSE_PARAM, pattrs [i], ptr, &ptr);
1415 if (def && (method->call_convention == MONO_CALL_VARARG))
1416 method->sentinelpos = method->param_count;
1423 * Add signature to a cache and increase ref count...
1429 * mono_metadata_free_method_signature:
1430 * @sig: signature to destroy
1432 * Free the memory allocated in the signature @sig.
1435 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1438 mono_metadata_free_type (sig->ret);
1439 for (i = 0; i < sig->param_count; ++i)
1440 mono_metadata_free_type (sig->params [i]);
1446 do_mono_metadata_parse_generic_inst (MonoType *type, MonoImage *m, const char *ptr, const char **rptr)
1448 MonoGenericInst *ginst = g_new0 (MonoGenericInst, 1);
1452 type->data.generic_inst = ginst;
1454 ginst->context = g_new0 (MonoGenericContext, 1);
1455 ginst->context->ginst = ginst;
1457 ginst->klass = g_new0 (MonoClass, 1);
1459 ginst->generic_type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1460 ginst->type_argc = count = mono_metadata_decode_value (ptr, &ptr);
1461 ginst->type_argv = g_new0 (MonoType*, count);
1464 * Create the klass before parsing the type arguments.
1465 * This is required to support "recursive" definitions.
1466 * See mcs/tests/gen-23.cs for an example.
1469 ginst->init_pending = TRUE;
1471 mono_class_create_generic (ginst);
1473 for (i = 0; i < ginst->type_argc; i++) {
1474 MonoType *t = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1476 ginst->type_argv [i] = t;
1477 if (!ginst->is_open)
1478 ginst->is_open = mono_class_is_open_constructed_type (t);
1486 * We may be called multiple times on different metadata to create the same
1487 * instantiated type. This happens for instance if we're part of a method or
1488 * local variable signature.
1490 * It's important to return the same MonoGenericInst * for each particualar
1491 * instantiation of a generic type (ie "Stack<Int32>") to make static fields
1494 * According to the spec ($26.1.5), a static variable in a generic class
1495 * declaration is shared amongst all instances of the same closed constructed
1499 cached = g_hash_table_lookup (m->generic_inst_cache, ginst);
1501 g_free (ginst->klass);
1502 g_free (ginst->type_argv);
1505 type->data.generic_inst = cached->data.generic_inst;
1508 cached = g_new0 (MonoType, 1);
1509 cached->type = MONO_TYPE_GENERICINST;
1510 cached->data.generic_inst = ginst;
1512 g_hash_table_insert (m->generic_inst_cache, ginst, cached);
1514 mono_stats.generic_instance_count++;
1515 mono_stats.generics_metadata_size += sizeof (MonoGenericInst) +
1516 sizeof (MonoGenericContext) +
1517 ginst->type_argc * sizeof (MonoType);
1520 ginst->init_pending = FALSE;
1523 static MonoGenericParam *
1524 mono_metadata_parse_generic_param (MonoImage *m, const char *ptr, const char **rptr)
1526 MonoGenericParam *generic_param = g_new0 (MonoGenericParam, 1);
1528 generic_param->num = mono_metadata_decode_value (ptr, &ptr);
1533 return generic_param;
1537 * do_mono_metadata_parse_type:
1538 * @type: MonoType to be filled in with the return value
1540 * Internal routine used to "fill" the contents of @type from an
1541 * allocated pointer. This is done this way to avoid doing too
1542 * many mini-allocations (particularly for the MonoFieldType which
1543 * most of the time is just a MonoType, but sometimes might be augmented).
1545 * This routine is used by mono_metadata_parse_type and
1546 * mono_metadata_parse_field_type
1548 * This extracts a Type as specified in Partition II (22.2.12)
1551 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, const char *ptr, const char **rptr)
1553 type->type = mono_metadata_decode_value (ptr, &ptr);
1555 switch (type->type){
1556 case MONO_TYPE_VOID:
1557 case MONO_TYPE_BOOLEAN:
1558 case MONO_TYPE_CHAR:
1571 case MONO_TYPE_STRING:
1572 case MONO_TYPE_OBJECT:
1573 case MONO_TYPE_TYPEDBYREF:
1575 case MONO_TYPE_VALUETYPE:
1576 case MONO_TYPE_CLASS: {
1578 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
1579 type->data.klass = mono_class_get (m, token);
1582 case MONO_TYPE_SZARRAY: {
1583 MonoType *etype = mono_metadata_parse_type (m, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1584 type->data.klass = mono_class_from_mono_type (etype);
1585 mono_metadata_free_type (etype);
1589 type->data.type = mono_metadata_parse_type (m, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1591 case MONO_TYPE_FNPTR:
1592 type->data.method = mono_metadata_parse_method_signature (m, 0, ptr, &ptr);
1594 case MONO_TYPE_ARRAY:
1595 type->data.array = mono_metadata_parse_array (m, ptr, &ptr);
1598 case MONO_TYPE_MVAR:
1600 type->data.generic_param = mono_metadata_parse_generic_param (m, ptr, &ptr);
1603 case MONO_TYPE_GENERICINST:
1604 do_mono_metadata_parse_generic_inst (type, m, ptr, &ptr);
1608 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
1616 * mono_metadata_free_type:
1617 * @type: type to free
1619 * Free the memory allocated for type @type.
1622 mono_metadata_free_type (MonoType *type)
1624 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
1626 switch (type->type){
1628 mono_metadata_free_type (type->data.type);
1630 case MONO_TYPE_FNPTR:
1631 mono_metadata_free_method_signature (type->data.method);
1633 case MONO_TYPE_ARRAY:
1634 mono_metadata_free_array (type->data.array);
1642 hex_dump (const char *buffer, int base, int count)
1644 int show_header = 1;
1652 for (i = 0; i < count; i++){
1655 printf ("\n0x%08x: ", (unsigned char) base + i);
1657 printf ("%02x ", (unsigned char) (buffer [i]));
1664 * @mh: The Method header
1665 * @ptr: Points to the beginning of the Section Data (25.3)
1668 parse_section_data (MonoMethodHeader *mh, const unsigned char *ptr)
1670 unsigned char sect_data_flags;
1671 const unsigned char *sptr;
1673 guint32 sect_data_len;
1676 /* align on 32-bit boundary */
1677 /* FIXME: not 64-bit clean code */
1678 sptr = ptr = dword_align (ptr);
1679 sect_data_flags = *ptr;
1682 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
1684 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
1687 sect_data_len = ptr [0];
1691 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
1692 hex_dump (sptr, 0, sect_data_len+8);
1693 g_print ("\nheader: ");
1694 hex_dump (sptr-4, 0, 4);
1698 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
1699 const unsigned char *p = dword_align (ptr);
1701 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
1702 /* we could just store a pointer if we don't need to byteswap */
1703 mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
1704 for (i = 0; i < mh->num_clauses; ++i) {
1705 MonoExceptionClause *ec = &mh->clauses [i];
1708 /* we could memcpy and byteswap */
1709 ec->flags = read32 (p);
1711 ec->try_offset = read32 (p);
1713 ec->try_len = read32 (p);
1715 ec->handler_offset = read32 (p);
1717 ec->handler_len = read32 (p);
1719 ec->token_or_filter = read32 (p);
1722 ec->flags = read16 (p);
1724 ec->try_offset = read16 (p);
1728 ec->handler_offset = read16 (p);
1730 ec->handler_len = *p;
1732 ec->token_or_filter = read32 (p);
1735 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
1739 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
1740 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
1747 * mono_metadata_parse_mh:
1748 * @m: metadata context
1749 * @ptr: pointer to the method header.
1751 * Decode the method header at @ptr, including pointer to the IL code,
1752 * info about local variables and optional exception tables.
1754 * Returns: a MonoMethodHeader.
1757 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
1759 MonoMethodHeader *mh;
1760 unsigned char flags = *(const unsigned char *) ptr;
1761 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
1763 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
1764 const unsigned char *code;
1767 g_return_val_if_fail (ptr != NULL, NULL);
1770 case METHOD_HEADER_TINY_FORMAT:
1771 mh = g_new0 (MonoMethodHeader, 1);
1774 local_var_sig_tok = 0;
1775 mh->code_size = flags >> 2;
1778 case METHOD_HEADER_TINY_FORMAT1:
1779 mh = g_new0 (MonoMethodHeader, 1);
1782 local_var_sig_tok = 0;
1785 * The spec claims 3 bits, but the Beta2 is
1788 mh->code_size = flags >> 2;
1791 case METHOD_HEADER_FAT_FORMAT:
1792 fat_flags = read16 (ptr);
1794 hsize = (fat_flags >> 12) & 0xf;
1795 max_stack = read16 (ptr);
1797 code_size = read32 (ptr);
1799 local_var_sig_tok = read32 (ptr);
1802 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
1809 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
1813 * There are more sections
1815 ptr = code + code_size;
1823 if (local_var_sig_tok) {
1824 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
1825 const char *locals_ptr;
1826 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
1827 int len=0, i, bsize;
1829 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
1830 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
1831 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
1832 if (*locals_ptr != 0x07)
1833 g_warning ("wrong signature for locals blob");
1835 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
1836 mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1837 mh->num_locals = len;
1838 for (i = 0; i < len; ++i)
1839 mh->locals [i] = mono_metadata_parse_type (m, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
1841 mh = g_new0 (MonoMethodHeader, 1);
1844 mh->code_size = code_size;
1845 mh->max_stack = max_stack;
1846 mh->init_locals = init_locals;
1847 if (fat_flags & METHOD_HEADER_MORE_SECTS)
1848 parse_section_data (mh, (const unsigned char*)ptr);
1853 * mono_metadata_free_mh:
1854 * @mh: a method header
1856 * Free the memory allocated for the method header.
1859 mono_metadata_free_mh (MonoMethodHeader *mh)
1862 for (i = 0; i < mh->num_locals; ++i)
1863 mono_metadata_free_type (mh->locals[i]);
1864 g_free (mh->clauses);
1869 * mono_metadata_parse_field_type:
1870 * @m: metadata context to extract information from
1871 * @ptr: pointer to the field signature
1872 * @rptr: pointer updated to match the end of the decoded stream
1874 * Parses the field signature, and returns the type information for it.
1876 * Returns: The MonoType that was extracted from @ptr.
1879 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
1881 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
1885 * mono_metadata_parse_param:
1886 * @m: metadata context to extract information from
1887 * @ptr: pointer to the param signature
1888 * @rptr: pointer updated to match the end of the decoded stream
1890 * Parses the param signature, and returns the type information for it.
1892 * Returns: The MonoType that was extracted from @ptr.
1895 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
1897 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
1901 * mono_metadata_token_from_dor:
1902 * @dor_token: A TypeDefOrRef coded index
1904 * dor_token is a TypeDefOrRef coded index: it contains either
1905 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
1906 * bits contain an index into the table.
1908 * Returns: an expanded token
1911 mono_metadata_token_from_dor (guint32 dor_index)
1915 table = dor_index & 0x03;
1916 idx = dor_index >> 2;
1919 case 0: /* TypeDef */
1920 return MONO_TOKEN_TYPE_DEF | idx;
1922 case 1: /* TypeRef */
1923 return MONO_TOKEN_TYPE_REF | idx;
1925 case 2: /* TypeSpec */
1926 return MONO_TOKEN_TYPE_SPEC | idx;
1929 g_assert_not_reached ();
1936 * We use this to pass context information to the row locator
1939 int idx; /* The index that we are trying to locate */
1940 int col_idx; /* The index in the row where idx may be stored */
1941 MonoTableInfo *t; /* pointer to the table */
1946 * How the row locator works.
1951 * ___|___------> _______
1954 * A column in the rows of table A references an index in table B.
1955 * For example A may be the TYPEDEF table and B the METHODDEF table.
1957 * Given an index in table B we want to get the row in table A
1958 * where the column n references our index in B.
1960 * In the locator_t structure:
1962 * col_idx is the column number
1963 * index is the index in table B
1964 * result will be the index in table A
1967 * Table A Table B column (in table A)
1968 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
1969 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
1970 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
1971 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
1972 * METHODSEM PROPERTY ASSOCIATION (encoded index)
1974 * Note that we still don't support encoded indexes.
1978 typedef_locator (const void *a, const void *b)
1980 locator_t *loc = (locator_t *) a;
1981 const char *bb = (const char *) b;
1982 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
1983 guint32 col, col_next;
1985 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
1991 * Need to check that the next row is valid.
1993 if (typedef_index + 1 < loc->t->rows) {
1994 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
1995 if (loc->idx >= col_next)
1998 if (col == col_next)
2002 loc->result = typedef_index;
2008 table_locator (const void *a, const void *b)
2010 locator_t *loc = (locator_t *) a;
2011 const char *bb = (const char *) b;
2012 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2015 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2017 if (loc->idx == col) {
2018 loc->result = table_index;
2028 * mono_metadata_typedef_from_field:
2029 * @meta: metadata context
2030 * @index: FieldDef token
2032 * Returns the 1-based index into the TypeDef table of the type that
2033 * declared the field described by @index.
2034 * Returns 0 if not found.
2037 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
2039 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2045 loc.idx = mono_metadata_token_index (index);
2046 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
2049 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2050 g_assert_not_reached ();
2052 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2053 return loc.result + 1;
2057 * mono_metadata_typedef_from_method:
2058 * @meta: metadata context
2059 * @index: MethodDef token
2061 * Returns the 1-based index into the TypeDef table of the type that
2062 * declared the method described by @index.
2063 * Returns 0 if not found.
2066 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
2068 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2074 loc.idx = mono_metadata_token_index (index);
2075 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
2078 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2079 g_assert_not_reached ();
2081 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2082 return loc.result + 1;
2086 * mono_metadata_interfaces_from_typedef:
2087 * @meta: metadata context
2088 * @index: typedef token
2090 * Returns and array of interfaces that the @index typedef token implements.
2091 * The number of elemnts in the array is returned in @count.
2094 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
2096 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
2099 guint32 cols [MONO_INTERFACEIMPL_SIZE];
2107 loc.idx = mono_metadata_token_index (index);
2108 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
2111 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2116 * We may end up in the middle of the rows...
2119 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
2126 while (start < tdef->rows) {
2127 mono_metadata_decode_row (tdef, start, cols, MONO_INTERFACEIMPL_SIZE);
2128 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
2130 result = g_renew (MonoClass*, result, i + 1);
2131 result [i] = mono_class_get (meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]));
2139 * mono_metadata_nested_in_typedef:
2140 * @meta: metadata context
2141 * @index: typedef token
2143 * Returns: the 1-based index into the TypeDef table of the type
2144 * where the type described by @index is nested.
2145 * Retruns 0 if @index describes a non-nested type.
2148 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
2150 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2156 loc.idx = mono_metadata_token_index (index);
2157 loc.col_idx = MONO_NESTED_CLASS_NESTED;
2160 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2163 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2164 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
2168 * mono_metadata_nesting_typedef:
2169 * @meta: metadata context
2170 * @index: typedef token
2172 * Returns: the 1-based index into the TypeDef table of the first type
2173 * that is nested inside the type described by @index. The search starts at
2175 * Returns 0 if no such type is found.
2178 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
2180 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2182 guint32 class_index = mono_metadata_token_index (index);
2187 start = start_index;
2189 while (start <= tdef->rows) {
2190 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
2196 if (start > tdef->rows)
2203 * mono_metadata_packing_from_typedef:
2204 * @meta: metadata context
2205 * @index: token representing a type
2207 * Returns the info stored in the ClassLAyout table for the given typedef token
2208 * into the @packing and @size pointers.
2209 * Returns 0 if the info is not found.
2212 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
2214 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
2216 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
2221 loc.idx = mono_metadata_token_index (index);
2222 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
2225 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2228 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
2230 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
2232 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
2234 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2235 return loc.result + 1;
2239 * mono_metadata_custom_attrs_from_index:
2240 * @meta: metadata context
2241 * @index: token representing the parent
2243 * Returns: the 1-based index into the CustomAttribute table of the first
2244 * attribute which belongs to the metadata object described by @index.
2245 * Returns 0 if no such attribute is found.
2248 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
2250 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
2257 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
2260 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2263 /* Find the first entry by searching backwards */
2264 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
2267 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2268 return loc.result + 1;
2273 mono_backtrace (int limit)
2278 backtrace (array, limit);
2279 names = backtrace_symbols (array, limit);
2280 for (i =0; i < limit; ++i) {
2281 g_print ("\t%s\n", names [i]);
2288 /*#define __alignof__(a) sizeof(a)*/
2289 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
2294 * @t: the type to return the size of
2296 * Returns: the number of bytes required to hold an instance of this
2300 mono_type_size (MonoType *t, gint *align)
2307 *align = __alignof__(gpointer);
2308 return sizeof (gpointer);
2312 case MONO_TYPE_VOID:
2315 case MONO_TYPE_BOOLEAN:
2316 *align = __alignof__(gint8);
2320 *align = __alignof__(gint8);
2322 case MONO_TYPE_CHAR:
2325 *align = __alignof__(gint16);
2329 *align = __alignof__(gint32);
2332 *align = __alignof__(float);
2336 *align = __alignof__(gint64);
2339 *align = __alignof__(double);
2343 *align = __alignof__(gpointer);
2344 return sizeof (gpointer);
2345 case MONO_TYPE_STRING:
2346 *align = __alignof__(gpointer);
2347 return sizeof (gpointer);
2348 case MONO_TYPE_OBJECT:
2349 *align = __alignof__(gpointer);
2350 return sizeof (gpointer);
2351 case MONO_TYPE_VALUETYPE: {
2352 if (t->data.klass->enumtype)
2353 return mono_type_size (t->data.klass->enum_basetype, align);
2355 return mono_class_value_size (t->data.klass, align);
2357 case MONO_TYPE_CLASS:
2358 case MONO_TYPE_SZARRAY:
2360 case MONO_TYPE_FNPTR:
2361 case MONO_TYPE_ARRAY:
2362 *align = __alignof__(gpointer);
2363 return sizeof (gpointer);
2364 case MONO_TYPE_TYPEDBYREF:
2365 return mono_class_value_size (mono_defaults.typed_reference_class, align);
2366 case MONO_TYPE_GENERICINST: {
2367 MonoGenericInst *ginst = t->data.generic_inst;
2368 MonoClass *iclass = mono_class_from_mono_type (ginst->generic_type);
2369 return mono_type_size (&iclass->byval_arg, align);
2372 case MONO_TYPE_MVAR:
2373 *align = __alignof__(gpointer);
2374 return sizeof (gpointer);
2376 g_error ("mono_type_size: type 0x%02x unknown", t->type);
2382 * mono_type_stack_size:
2383 * @t: the type to return the size it uses on the stack
2385 * Returns: the number of bytes required to hold an instance of this
2386 * type on the runtime stack
2389 mono_type_stack_size (MonoType *t, gint *align)
2393 g_assert (t != NULL);
2399 *align = __alignof__(gpointer);
2400 return sizeof (gpointer);
2404 case MONO_TYPE_BOOLEAN:
2405 case MONO_TYPE_CHAR:
2414 case MONO_TYPE_STRING:
2415 case MONO_TYPE_OBJECT:
2416 case MONO_TYPE_CLASS:
2417 case MONO_TYPE_SZARRAY:
2419 case MONO_TYPE_FNPTR:
2420 case MONO_TYPE_ARRAY:
2421 *align = __alignof__(gpointer);
2422 return sizeof (gpointer);
2423 case MONO_TYPE_TYPEDBYREF:
2424 *align = __alignof__(gpointer);
2425 return sizeof (gpointer) * 3;
2427 *align = __alignof__(float);
2428 return sizeof (float);
2431 *align = __alignof__(gint64);
2432 return sizeof (gint64);
2434 *align = __alignof__(double);
2435 return sizeof (double);
2436 case MONO_TYPE_VALUETYPE: {
2439 if (t->data.klass->enumtype)
2440 return mono_type_stack_size (t->data.klass->enum_basetype, align);
2442 size = mono_class_value_size (t->data.klass, align);
2444 *align = *align + __alignof__(gpointer) - 1;
2445 *align &= ~(__alignof__(gpointer) - 1);
2447 size += sizeof (gpointer) - 1;
2448 size &= ~(sizeof (gpointer) - 1);
2453 case MONO_TYPE_GENERICINST: {
2454 MonoGenericInst *ginst = t->data.generic_inst;
2455 MonoClass *iclass = mono_class_from_mono_type (ginst->generic_type);
2456 return mono_type_stack_size (&iclass->byval_arg, align);
2459 g_error ("type 0x%02x unknown", t->type);
2465 mono_metadata_generic_inst_hash (MonoGenericInst *ginst)
2467 return mono_metadata_type_hash (ginst->generic_type);
2471 mono_metadata_generic_inst_equal (MonoGenericInst *g1, MonoGenericInst *g2)
2475 if (g1->type_argc != g2->type_argc)
2477 if (!mono_metadata_type_equal (g1->generic_type, g2->generic_type))
2479 for (i = 0; i < g1->type_argc; ++i) {
2480 if (!mono_metadata_type_equal (g1->type_argv [i], g2->type_argv [i]))
2487 * mono_metadata_type_hash:
2490 * Computes an hash value for @t1 to be used in GHashTable.
2493 mono_metadata_type_hash (MonoType *t1)
2495 guint hash = t1->type;
2497 hash |= t1->byref << 6; /* do not collide with t1->type values */
2499 case MONO_TYPE_VALUETYPE:
2500 case MONO_TYPE_CLASS:
2501 case MONO_TYPE_SZARRAY:
2502 /* check if the distribution is good enough */
2503 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
2505 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
2506 case MONO_TYPE_ARRAY:
2507 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
2508 case MONO_TYPE_GENERICINST:
2509 return ((hash << 5) - hash) ^ mono_metadata_generic_inst_hash (t1->data.generic_inst);
2515 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2)
2519 if (c1->generic_inst && c2->generic_inst)
2520 return mono_metadata_generic_inst_equal (c1->generic_inst, c2->generic_inst);
2521 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
2522 return c1->byval_arg.data.generic_param->num == c2->byval_arg.data.generic_param->num;
2523 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
2524 return c1->byval_arg.data.generic_param->num == c2->byval_arg.data.generic_param->num;
2529 * mono_metadata_type_equal:
2533 * Determine if @t1 and @t2 represent the same type.
2534 * Returns: #TRUE if @t1 and @t2 are equal.
2537 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
2539 if (t1->type != t2->type ||
2540 t1->byref != t2->byref)
2544 case MONO_TYPE_VOID:
2545 case MONO_TYPE_BOOLEAN:
2546 case MONO_TYPE_CHAR:
2557 case MONO_TYPE_STRING:
2560 case MONO_TYPE_OBJECT:
2561 case MONO_TYPE_TYPEDBYREF:
2563 case MONO_TYPE_VALUETYPE:
2564 case MONO_TYPE_CLASS:
2565 case MONO_TYPE_SZARRAY:
2566 return mono_metadata_class_equal (t1->data.klass, t2->data.klass);
2568 return mono_metadata_type_equal (t1->data.type, t2->data.type);
2569 case MONO_TYPE_ARRAY:
2570 if (t1->data.array->rank != t2->data.array->rank)
2572 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass);
2573 case MONO_TYPE_GENERICINST:
2574 return mono_metadata_generic_inst_equal (t1->data.generic_inst,
2575 t2->data.generic_inst);
2577 case MONO_TYPE_MVAR:
2578 return t1->data.generic_param->num == t2->data.generic_param->num;
2580 g_error ("implement type compare for %0x!", t1->type);
2588 * mono_metadata_signature_equal:
2589 * @sig1: a signature
2590 * @sig2: another signature
2592 * Determine if @sig1 and @sig2 represent the same signature, with the
2593 * same number of arguments and the same types.
2594 * Returns: #TRUE if @sig1 and @sig2 are equal.
2597 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
2601 if (sig1->hasthis != sig2->hasthis ||
2602 sig1->param_count != sig2->param_count)
2605 for (i = 0; i < sig1->param_count; i++) {
2606 MonoType *p1 = sig1->params[i];
2607 MonoType *p2 = sig2->params[i];
2609 /* if (p1->attrs != p2->attrs)
2612 if (!mono_metadata_type_equal (p1, p2))
2616 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
2622 mono_signature_hash (MonoMethodSignature *sig)
2624 guint i, res = sig->ret->type;
2626 for (i = 0; i < sig->param_count; i++)
2627 res = (res << 5) - res + sig->params[i]->type;
2633 * mono_metadata_encode_value:
2634 * @value: value to encode
2635 * @buf: buffer where to write the compressed representation
2636 * @endbuf: pointer updated to point at the end of the encoded output
2638 * Encodes the value @value in the compressed representation used
2639 * in metadata and stores the result in @buf. @buf needs to be big
2640 * enough to hold the data (4 bytes).
2643 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
2649 else if (value < 0x4000) {
2650 p [0] = 0x80 | (value >> 8);
2651 p [1] = value & 0xff;
2654 p [0] = (value >> 24) | 0xc0;
2655 p [1] = (value >> 16) & 0xff;
2656 p [2] = (value >> 8) & 0xff;
2657 p [3] = value & 0xff;
2665 * mono_metadata_field_info:
2666 * @meta: the Image the field is defined in
2667 * @index: the index in the field table representing the field
2668 * @offset: a pointer to an integer where to store the offset that
2669 * may have been specified for the field in a FieldLayout table
2670 * @rva: a pointer to the RVA of the field data in the image that
2671 * may have been defined in a FieldRVA table
2672 * @marshal_spec: a pointer to the marshal spec that may have been
2673 * defined for the field in a FieldMarshal table.
2675 * Gather info for field @index that may have been defined in the FieldLayout,
2676 * FieldRVA and FieldMarshal tables.
2677 * Either of offset, rva and marshal_spec can be NULL if you're not interested
2681 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
2682 MonoMarshalSpec **marshal_spec)
2684 MonoTableInfo *tdef;
2687 loc.idx = index + 1;
2689 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
2691 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
2694 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2695 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
2697 *offset = (guint32)-1;
2701 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
2703 loc.col_idx = MONO_FIELD_RVA_FIELD;
2706 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2708 * LAMESPEC: There is no signature, no nothing, just the raw data.
2710 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
2718 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
2719 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
2726 * mono_metadata_get_constant_index:
2727 * @meta: the Image the field is defined in
2728 * @index: the token that may have a row defined in the constants table
2729 * @hint: possible position for the row
2731 * @token must be a FieldDef, ParamDef or PropertyDef token.
2733 * Returns: the index into the Constants table or 0 if not found.
2736 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
2738 MonoTableInfo *tdef;
2740 guint32 index = mono_metadata_token_index (token);
2742 tdef = &meta->tables [MONO_TABLE_CONSTANT];
2743 index <<= MONO_HASCONSTANT_BITS;
2744 switch (mono_metadata_token_table (token)) {
2745 case MONO_TABLE_FIELD:
2746 index |= MONO_HASCONSTANT_FIEDDEF;
2748 case MONO_TABLE_PARAM:
2749 index |= MONO_HASCONSTANT_PARAM;
2751 case MONO_TABLE_PROPERTY:
2752 index |= MONO_HASCONSTANT_PROPERTY;
2755 g_warning ("Not a valid token for the constant table: 0x%08x", token);
2759 loc.col_idx = MONO_CONSTANT_PARENT;
2762 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
2765 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2766 return loc.result + 1;
2772 * mono_metadata_events_from_typedef:
2773 * @meta: metadata context
2774 * @index: 0-based index (in the TypeDef table) describing a type
2776 * Returns: the 0-based index in the Event table for the events in the
2777 * type. The last event that belongs to the type (plus 1) is stored
2778 * in the @end_idx pointer.
2781 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
2785 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
2793 loc.col_idx = MONO_EVENT_MAP_PARENT;
2794 loc.idx = index + 1;
2796 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2799 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
2800 if (loc.result + 1 < tdef->rows) {
2801 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
2803 end = meta->tables [MONO_TABLE_EVENT].rows;
2811 * mono_metadata_methods_from_event:
2812 * @meta: metadata context
2813 * @index: 0-based index (in the Event table) describing a event
2815 * Returns: the 0-based index in the MethodDef table for the methods in the
2816 * event. The last method that belongs to the event (plus 1) is stored
2817 * in the @end_idx pointer.
2820 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
2824 guint32 cols [MONO_METHOD_SEMA_SIZE];
2825 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
2832 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
2833 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
2835 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
2840 * We may end up in the middle of the rows...
2843 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
2849 while (end < msemt->rows) {
2850 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
2851 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
2860 * mono_metadata_properties_from_typedef:
2861 * @meta: metadata context
2862 * @index: 0-based index (in the TypeDef table) describing a type
2864 * Returns: the 0-based index in the Property table for the properties in the
2865 * type. The last property that belongs to the type (plus 1) is stored
2866 * in the @end_idx pointer.
2869 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
2873 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
2881 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
2882 loc.idx = index + 1;
2884 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2887 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
2888 if (loc.result + 1 < tdef->rows) {
2889 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
2891 end = meta->tables [MONO_TABLE_PROPERTY].rows;
2899 * mono_metadata_methods_from_property:
2900 * @meta: metadata context
2901 * @index: 0-based index (in the PropertyDef table) describing a property
2903 * Returns: the 0-based index in the MethodDef table for the methods in the
2904 * property. The last method that belongs to the property (plus 1) is stored
2905 * in the @end_idx pointer.
2908 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
2912 guint32 cols [MONO_METHOD_SEMA_SIZE];
2913 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
2920 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
2921 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
2923 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
2928 * We may end up in the middle of the rows...
2931 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
2937 while (end < msemt->rows) {
2938 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
2939 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
2948 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
2951 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
2957 loc.col_idx = MONO_IMPLMAP_MEMBER;
2958 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
2960 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2963 return loc.result + 1;
2967 * @image: context where the image is created
2968 * @type_spec: typespec token
2970 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
2974 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
2976 guint32 idx = mono_metadata_token_index (type_spec);
2978 guint32 cols [MONO_TYPESPEC_SIZE];
2983 mono_loader_lock ();
2985 if ((type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec)))) {
2986 mono_loader_unlock ();
2990 t = &image->tables [MONO_TABLE_TYPESPEC];
2992 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
2993 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
2994 len = mono_metadata_decode_value (ptr, &ptr);
2996 type = g_new0 (MonoType, 1);
2998 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
3000 if (*ptr == MONO_TYPE_BYREF) {
3005 do_mono_metadata_parse_type (type, image, ptr, &ptr);
3007 mono_loader_unlock ();
3013 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
3015 MonoMarshalSpec *res;
3017 const char *start = ptr;
3019 /* fixme: this is incomplete, but I cant find more infos in the specs */
3021 res = g_new0 (MonoMarshalSpec, 1);
3023 len = mono_metadata_decode_value (ptr, &ptr);
3024 res->native = *ptr++;
3026 if (res->native == MONO_NATIVE_LPARRAY) {
3027 if (ptr - start <= len)
3028 res->data.array_data.elem_type = *ptr++;
3029 if (ptr - start <= len)
3030 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
3031 if (ptr - start <= len)
3032 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3035 if (res->native == MONO_NATIVE_BYVALTSTR) {
3036 if (ptr - start <= len)
3037 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3040 if (res->native == MONO_NATIVE_BYVALARRAY) {
3041 if (ptr - start <= len)
3042 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3045 if (res->native == MONO_NATIVE_CUSTOM) {
3046 /* skip unused type guid */
3047 len = mono_metadata_decode_value (ptr, &ptr);
3049 /* skip unused native type name */
3050 len = mono_metadata_decode_value (ptr, &ptr);
3052 /* read custom marshaler type name */
3053 len = mono_metadata_decode_value (ptr, &ptr);
3054 res->data.custom_data.custom_name = g_strndup (ptr, len);
3056 /* read cookie string */
3057 len = mono_metadata_decode_value (ptr, &ptr);
3058 res->data.custom_data.cookie = g_strndup (ptr, len);
3065 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
3066 gboolean unicode, MonoMarshalConv *conv)
3068 MonoMarshalConv dummy_conv;
3074 *conv = MONO_MARSHAL_CONV_NONE;
3077 return MONO_NATIVE_UINT;
3081 case MONO_TYPE_BOOLEAN:
3083 switch (mspec->native) {
3084 case MONO_NATIVE_VARIANTBOOL:
3085 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
3086 return MONO_NATIVE_VARIANTBOOL;
3087 case MONO_NATIVE_BOOLEAN:
3088 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3089 return MONO_NATIVE_BOOLEAN;
3090 case MONO_NATIVE_I1:
3091 case MONO_NATIVE_U1:
3092 return mspec->native;
3094 g_error ("cant marshal bool to native type %02x", mspec->native);
3097 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3098 return MONO_NATIVE_BOOLEAN;
3099 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
3100 case MONO_TYPE_I1: return MONO_NATIVE_I1;
3101 case MONO_TYPE_U1: return MONO_NATIVE_U1;
3102 case MONO_TYPE_I2: return MONO_NATIVE_I2;
3103 case MONO_TYPE_U2: return MONO_NATIVE_U2;
3104 case MONO_TYPE_I4: return MONO_NATIVE_I4;
3105 case MONO_TYPE_U4: return MONO_NATIVE_U4;
3106 case MONO_TYPE_I8: return MONO_NATIVE_I8;
3107 case MONO_TYPE_U8: return MONO_NATIVE_U8;
3108 case MONO_TYPE_R4: return MONO_NATIVE_R4;
3109 case MONO_TYPE_R8: return MONO_NATIVE_R8;
3110 case MONO_TYPE_STRING:
3112 switch (mspec->native) {
3113 case MONO_NATIVE_BSTR:
3114 *conv = MONO_MARSHAL_CONV_STR_BSTR;
3115 return MONO_NATIVE_BSTR;
3116 case MONO_NATIVE_LPSTR:
3117 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
3118 return MONO_NATIVE_LPSTR;
3119 case MONO_NATIVE_LPWSTR:
3120 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
3121 return MONO_NATIVE_LPWSTR;
3122 case MONO_NATIVE_LPTSTR:
3123 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3124 return MONO_NATIVE_LPTSTR;
3125 case MONO_NATIVE_ANSIBSTR:
3126 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
3127 return MONO_NATIVE_ANSIBSTR;
3128 case MONO_NATIVE_TBSTR:
3129 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
3130 return MONO_NATIVE_TBSTR;
3131 case MONO_NATIVE_BYVALTSTR:
3133 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
3135 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
3136 return MONO_NATIVE_BYVALTSTR;
3138 g_error ("cant marshal string to native type %02x", mspec->native);
3141 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3142 return MONO_NATIVE_LPTSTR;
3143 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
3144 case MONO_TYPE_VALUETYPE: /*FIXME*/
3145 if (type->data.klass->enumtype) {
3146 t = type->data.klass->enum_basetype->type;
3149 return MONO_NATIVE_STRUCT;
3150 case MONO_TYPE_SZARRAY:
3151 case MONO_TYPE_ARRAY:
3153 switch (mspec->native) {
3154 case MONO_NATIVE_BYVALARRAY:
3155 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
3156 return MONO_NATIVE_BYVALARRAY;
3157 case MONO_NATIVE_SAFEARRAY:
3158 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
3159 return MONO_NATIVE_SAFEARRAY;
3160 case MONO_NATIVE_LPARRAY:
3161 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3162 return MONO_NATIVE_LPARRAY;
3164 g_error ("cant marshal array as native type %02x", mspec->native);
3168 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3169 return MONO_NATIVE_LPARRAY;
3170 case MONO_TYPE_I: return MONO_NATIVE_INT;
3171 case MONO_TYPE_U: return MONO_NATIVE_UINT;
3172 case MONO_TYPE_CLASS:
3173 case MONO_TYPE_OBJECT: {
3174 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
3176 switch (mspec->native) {
3177 case MONO_NATIVE_STRUCT:
3178 return MONO_NATIVE_STRUCT;
3179 case MONO_NATIVE_INTERFACE:
3180 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
3181 return MONO_NATIVE_INTERFACE;
3182 case MONO_NATIVE_IDISPATCH:
3183 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
3184 return MONO_NATIVE_IDISPATCH;
3185 case MONO_NATIVE_IUNKNOWN:
3186 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
3187 return MONO_NATIVE_IUNKNOWN;
3188 case MONO_NATIVE_FUNC:
3189 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3190 type->data.klass == mono_defaults.delegate_class ||
3191 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3192 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3193 return MONO_NATIVE_FUNC;
3199 g_error ("cant marshal object as native type %02x", mspec->native);
3202 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3203 type->data.klass == mono_defaults.delegate_class ||
3204 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3205 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3206 return MONO_NATIVE_FUNC;
3208 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
3209 return MONO_NATIVE_STRUCT;
3211 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
3212 case MONO_TYPE_TYPEDBYREF:
3214 g_error ("type 0x%02x not handled in marshal", t);
3216 return MONO_NATIVE_MAX;
3220 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
3223 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
3229 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
3230 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
3232 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3235 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
3239 method_from_method_def_or_ref (MonoImage *m, guint32 tok)
3241 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
3242 switch (tok & MONO_METHODDEFORREF_MASK) {
3243 case MONO_METHODDEFORREF_METHODDEF:
3244 return mono_get_method (m, MONO_TOKEN_METHOD_DEF | idx, NULL);
3245 case MONO_METHODDEFORREF_METHODREF:
3246 return mono_get_method (m, MONO_TOKEN_MEMBER_REF | idx, NULL);
3248 g_assert_not_reached ();
3253 mono_class_get_overrides (MonoImage *image, guint32 type_token, gint32 *num_overrides)
3256 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
3259 guint32 cols [MONO_METHODIMPL_SIZE];
3260 MonoMethod **result;
3269 loc.col_idx = MONO_METHODIMPL_CLASS;
3270 loc.idx = mono_metadata_token_index (type_token);
3272 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3278 * We may end up in the middle of the rows...
3281 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
3286 while (end < tdef->rows) {
3287 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
3293 result = g_new (MonoMethod*, num * 2);
3294 for (i = 0; i < num; ++i) {
3295 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
3296 result [i * 2] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_DECLARATION]);
3297 result [i * 2 + 1] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_BODY]);
3301 *num_overrides = num;
3306 * mono_guid_to_string:
3308 * Converts a 16 byte Microsoft GUID to the standard string representation.
3311 mono_guid_to_string (const guint8 *guid)
3313 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
3314 guid[3], guid[2], guid[1], guid[0],
3318 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
3322 get_constraints (MonoImage *image, int owner)
3324 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
3325 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
3326 guint32 i, token, found;
3327 MonoClass *klass, **res;
3328 GList *cons = NULL, *tmp;
3332 for (i = 0; i < tdef->rows; ++i) {
3333 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
3334 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
3335 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
3336 klass = mono_class_get (image, token);
3337 cons = g_list_append (cons, klass);
3340 /* contiguous list finished */
3347 res = g_new0 (MonoClass*, found + 1);
3348 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
3349 res [i] = tmp->data;
3356 mono_metadata_load_generic_params (MonoImage *image, guint32 token, guint32 *num)
3358 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
3359 guint32 cols [MONO_GENERICPARAM_SIZE];
3360 guint32 i, owner, last_num, n;
3361 MonoGenericParam *params;
3363 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
3364 owner = MONO_TYPEORMETHOD_TYPE;
3365 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3366 owner = MONO_TYPEORMETHOD_METHOD;
3368 g_error ("wrong token %x to load_generics_params", token);
3370 owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
3376 for (i = 0; i < tdef->rows; ++i) {
3377 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3378 if (cols [MONO_GENERICPARAM_OWNER] == owner)
3382 if (i >= tdef->rows)
3388 params = g_realloc (params, sizeof (MonoGenericParam) * n);
3389 params [n - 1].pklass = NULL;
3390 params [n - 1].method = NULL;
3391 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
3392 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
3393 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
3394 params [n - 1].constraints = get_constraints (image, i + 1);
3395 if (++i >= tdef->rows)
3397 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3398 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
3406 mono_type_is_byref (MonoType *type)
3412 mono_type_get_type (MonoType *type)
3417 /* For MONO_TYPE_FNPTR */
3418 MonoMethodSignature*
3419 mono_type_get_signature (MonoType *type)
3421 return type->data.method;
3424 /* For MONO_TYPE_CLASS, VALUETYPE */
3426 mono_type_get_class (MonoType *type)
3428 return type->data.klass;
3431 /* For MONO_TYPE_ARRAY */
3433 mono_type_get_array_type (MonoType *type)
3435 return type->data.array;
3439 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
3441 /* FIXME: implement */
3446 mono_signature_get_return_type (MonoMethodSignature *sig)
3452 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
3458 /* start from the first */
3459 if (sig->param_count) {
3460 *iter = &sig->params [0];
3461 return sig->params [0];
3469 if (type < &sig->params [sig->param_count]) {
3477 mono_signature_get_param_count (MonoMethodSignature *sig)
3479 return sig->param_count;
3483 mono_signature_get_call_conv (MonoMethodSignature *sig)
3485 return sig->call_convention;
3489 mono_signature_vararg_start (MonoMethodSignature *sig)
3491 return sig->sentinelpos;
3495 mono_signature_is_instance (MonoMethodSignature *sig)
3497 return sig->hasthis;
3501 mono_signature_explicit_this (MonoMethodSignature *sig)
3503 return sig->explicit_this;