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;
1206 gboolean pinned = FALSE;
1207 const char *tmp_ptr;
1212 * According to the spec, custom modifiers should come before the byref
1213 * flag, but the IL produced by ilasm from the following signature:
1214 * object modopt(...) &
1215 * starts with a byref flag, followed by the modifiers. (bug #49802)
1216 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1217 * it would be better to treat byref as real type constructor instead of
1219 * Also, pinned should come before anything else, but some MSV++ produced
1220 * assemblies violate this (#bug 61990).
1223 /* Count the modifiers first */
1228 case MONO_TYPE_PINNED:
1229 case MONO_TYPE_BYREF:
1232 case MONO_TYPE_CMOD_REQD:
1233 case MONO_TYPE_CMOD_OPT:
1235 mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1243 type = g_malloc0 (sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1244 type->num_mods = count;
1246 g_warning ("got more than 64 modifiers in type");
1250 * Later we can avoid doing this allocation.
1252 type = g_new0 (MonoType, 1);
1254 /* Parse pinned, byref and custom modifiers */
1259 case MONO_TYPE_PINNED:
1263 case MONO_TYPE_BYREF:
1267 case MONO_TYPE_CMOD_REQD:
1268 case MONO_TYPE_CMOD_OPT:
1270 mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1277 type->attrs = opt_attrs;
1278 type->byref = byref;
1279 type->pinned = pinned ? 1 : 0;
1281 do_mono_metadata_parse_type (type, m, ptr, &ptr);
1286 /* No need to use locking since nobody is modifying the hash table */
1287 if (mode != MONO_PARSE_PARAM && !type->num_mods && (cached = g_hash_table_lookup (type_cache, type))) {
1288 mono_metadata_free_type (type);
1296 * mono_metadata_parse_signature:
1297 * @image: metadata context
1298 * @toke: metadata token
1300 * Decode a method signature stored in the STANDALONESIG table
1302 * Returns: a MonoMethodSignature describing the signature.
1304 MonoMethodSignature *
1305 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1307 MonoTableInfo *tables = image->tables;
1308 guint32 idx = mono_metadata_token_index (token);
1313 return mono_lookup_dynamic_token (image, token);
1315 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1317 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1319 ptr = mono_metadata_blob_heap (image, sig);
1320 mono_metadata_decode_blob_size (ptr, &ptr);
1322 return mono_metadata_parse_method_signature (image, FALSE, ptr, NULL);
1325 MonoMethodSignature*
1326 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1328 MonoMethodSignature *sig;
1330 /* later we want to allocate signatures with mempools */
1331 sig = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1332 sig->param_count = nparams;
1333 sig->sentinelpos = -1;
1338 MonoMethodSignature*
1339 mono_metadata_signature_dup (MonoMethodSignature *sig)
1343 sigsize = sizeof (MonoMethodSignature) + sig->param_count * sizeof (MonoType *);
1344 return g_memdup (sig, sigsize);
1348 * mono_metadata_parse_method_signature:
1349 * @m: metadata context
1350 * @def: the MethodDef index or 0 for Ref signatures.
1351 * @ptr: pointer to the signature metadata representation
1352 * @rptr: pointer updated to match the end of the decoded stream
1354 * Decode a method signature stored at @ptr.
1356 * Returns: a MonoMethodSignature describing the signature.
1358 MonoMethodSignature *
1359 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1361 MonoMethodSignature *method;
1362 int i, ret_attrs = 0, *pattrs = NULL;
1363 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1364 guint32 gen_param_count = 0;
1367 gen_param_count = 1;
1372 call_convention = *ptr & 0x0F;
1374 if (gen_param_count)
1375 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1376 param_count = mono_metadata_decode_value (ptr, &ptr);
1377 pattrs = g_new0 (int, param_count);
1380 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1381 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1382 guint32 cols [MONO_PARAM_SIZE];
1383 guint lastp, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1385 if (def < methodt->rows)
1386 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1388 lastp = paramt->rows + 1;
1389 for (i = param_index; i < lastp; ++i) {
1390 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1391 if (!cols [MONO_PARAM_SEQUENCE])
1392 ret_attrs = cols [MONO_PARAM_FLAGS];
1394 pattrs [cols [MONO_PARAM_SEQUENCE] - 1] = cols [MONO_PARAM_FLAGS];
1397 method = mono_metadata_signature_alloc (m, param_count);
1398 method->hasthis = hasthis;
1399 method->explicit_this = explicit_this;
1400 method->call_convention = call_convention;
1401 method->generic_param_count = gen_param_count;
1402 if (call_convention != 0xa)
1403 method->ret = mono_metadata_parse_type (m, MONO_PARSE_RET, ret_attrs, ptr, &ptr);
1405 if (method->param_count) {
1406 method->sentinelpos = -1;
1408 for (i = 0; i < method->param_count; ++i) {
1409 if (*ptr == MONO_TYPE_SENTINEL) {
1410 if (method->call_convention != MONO_CALL_VARARG || def)
1411 g_error ("found sentinel for methoddef or no vararg method");
1412 method->sentinelpos = i;
1415 method->params [i] = mono_metadata_parse_type (m, MONO_PARSE_PARAM, pattrs [i], ptr, &ptr);
1419 if (def && (method->call_convention == MONO_CALL_VARARG))
1420 method->sentinelpos = method->param_count;
1427 * Add signature to a cache and increase ref count...
1433 * mono_metadata_free_method_signature:
1434 * @sig: signature to destroy
1436 * Free the memory allocated in the signature @sig.
1439 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1442 mono_metadata_free_type (sig->ret);
1443 for (i = 0; i < sig->param_count; ++i)
1444 mono_metadata_free_type (sig->params [i]);
1450 do_mono_metadata_parse_generic_inst (MonoType *type, MonoImage *m, const char *ptr, const char **rptr)
1452 MonoGenericInst *ginst = g_new0 (MonoGenericInst, 1);
1453 MonoGenericInst *cached;
1456 type->data.generic_inst = ginst;
1458 ginst->context = g_new0 (MonoGenericContext, 1);
1459 ginst->context->ginst = ginst;
1461 ginst->klass = g_new0 (MonoClass, 1);
1463 ginst->generic_type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1464 ginst->type_argc = count = mono_metadata_decode_value (ptr, &ptr);
1465 ginst->type_argv = g_new0 (MonoType*, count);
1468 * Create the klass before parsing the type arguments.
1469 * This is required to support "recursive" definitions.
1470 * See mcs/tests/gen-23.cs for an example.
1473 ginst->init_pending = TRUE;
1475 mono_class_create_generic (ginst);
1477 for (i = 0; i < ginst->type_argc; i++) {
1478 MonoType *t = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
1480 ginst->type_argv [i] = t;
1481 if (!ginst->is_open)
1482 ginst->is_open = mono_class_is_open_constructed_type (t);
1490 * We may be called multiple times on different metadata to create the same
1491 * instantiated type. This happens for instance if we're part of a method or
1492 * local variable signature.
1494 * It's important to return the same MonoGenericInst * for each particualar
1495 * instantiation of a generic type (ie "Stack<Int32>") to make static fields
1498 * According to the spec ($26.1.5), a static variable in a generic class
1499 * declaration is shared amongst all instances of the same closed constructed
1503 cached = g_hash_table_lookup (m->generic_inst_cache, ginst);
1505 g_free (ginst->klass);
1506 g_free (ginst->type_argv);
1509 type->data.generic_inst = cached;
1512 g_hash_table_insert (m->generic_inst_cache, ginst, ginst);
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;
2369 g_assert (!ginst->is_open && !ginst->klass->gen_params);
2371 if (MONO_TYPE_ISSTRUCT (ginst->generic_type)) {
2372 MonoClass *gklass = mono_class_from_mono_type (ginst->generic_type);
2374 if (gklass->enumtype)
2375 return mono_type_size (gklass->enum_basetype, align);
2377 return mono_class_value_size (ginst->klass, align);
2379 *align = __alignof__(gpointer);
2380 return sizeof (gpointer);
2384 case MONO_TYPE_MVAR:
2385 *align = __alignof__(gpointer);
2386 return sizeof (gpointer);
2388 g_error ("mono_type_size: type 0x%02x unknown", t->type);
2394 * mono_type_stack_size:
2395 * @t: the type to return the size it uses on the stack
2397 * Returns: the number of bytes required to hold an instance of this
2398 * type on the runtime stack
2401 mono_type_stack_size (MonoType *t, gint *align)
2405 g_assert (t != NULL);
2411 *align = __alignof__(gpointer);
2412 return sizeof (gpointer);
2416 case MONO_TYPE_BOOLEAN:
2417 case MONO_TYPE_CHAR:
2426 case MONO_TYPE_STRING:
2427 case MONO_TYPE_OBJECT:
2428 case MONO_TYPE_CLASS:
2429 case MONO_TYPE_SZARRAY:
2431 case MONO_TYPE_FNPTR:
2432 case MONO_TYPE_ARRAY:
2433 *align = __alignof__(gpointer);
2434 return sizeof (gpointer);
2435 case MONO_TYPE_TYPEDBYREF:
2436 *align = __alignof__(gpointer);
2437 return sizeof (gpointer) * 3;
2439 *align = __alignof__(float);
2440 return sizeof (float);
2443 *align = __alignof__(gint64);
2444 return sizeof (gint64);
2446 *align = __alignof__(double);
2447 return sizeof (double);
2448 case MONO_TYPE_VALUETYPE: {
2451 if (t->data.klass->enumtype)
2452 return mono_type_stack_size (t->data.klass->enum_basetype, align);
2454 size = mono_class_value_size (t->data.klass, align);
2456 *align = *align + __alignof__(gpointer) - 1;
2457 *align &= ~(__alignof__(gpointer) - 1);
2459 size += sizeof (gpointer) - 1;
2460 size &= ~(sizeof (gpointer) - 1);
2465 case MONO_TYPE_GENERICINST: {
2466 MonoGenericInst *ginst = t->data.generic_inst;
2468 g_assert (!ginst->is_open && !ginst->klass->gen_params);
2470 if (MONO_TYPE_ISSTRUCT (ginst->generic_type)) {
2471 MonoClass *gklass = mono_class_from_mono_type (ginst->generic_type);
2473 if (gklass->enumtype)
2474 return mono_type_stack_size (gklass->enum_basetype, align);
2476 guint32 size = mono_class_value_size (ginst->klass, align);
2478 *align = *align + __alignof__(gpointer) - 1;
2479 *align &= ~(__alignof__(gpointer) - 1);
2481 size += sizeof (gpointer) - 1;
2482 size &= ~(sizeof (gpointer) - 1);
2487 *align = __alignof__(gpointer);
2488 return sizeof (gpointer);
2492 g_error ("type 0x%02x unknown", t->type);
2498 mono_metadata_generic_inst_is_valuetype (MonoGenericInst *ginst)
2500 return MONO_TYPE_ISSTRUCT (ginst->generic_type);
2504 mono_metadata_generic_inst_hash (MonoGenericInst *ginst)
2506 return mono_metadata_type_hash (ginst->generic_type);
2510 mono_metadata_generic_inst_equal (MonoGenericInst *g1, MonoGenericInst *g2)
2514 if (g1->type_argc != g2->type_argc)
2516 if (!mono_metadata_type_equal (g1->generic_type, g2->generic_type))
2518 for (i = 0; i < g1->type_argc; ++i) {
2519 if (!mono_metadata_type_equal (g1->type_argv [i], g2->type_argv [i]))
2526 * mono_metadata_type_hash:
2529 * Computes an hash value for @t1 to be used in GHashTable.
2532 mono_metadata_type_hash (MonoType *t1)
2534 guint hash = t1->type;
2536 hash |= t1->byref << 6; /* do not collide with t1->type values */
2538 case MONO_TYPE_VALUETYPE:
2539 case MONO_TYPE_CLASS:
2540 case MONO_TYPE_SZARRAY:
2541 /* check if the distribution is good enough */
2542 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
2544 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
2545 case MONO_TYPE_ARRAY:
2546 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
2547 case MONO_TYPE_GENERICINST:
2548 return ((hash << 5) - hash) ^ mono_metadata_generic_inst_hash (t1->data.generic_inst);
2554 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2)
2558 if (c1->generic_inst && c2->generic_inst)
2559 return mono_metadata_generic_inst_equal (c1->generic_inst, c2->generic_inst);
2560 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
2561 return c1->byval_arg.data.generic_param->num == c2->byval_arg.data.generic_param->num;
2562 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
2563 return c1->byval_arg.data.generic_param->num == c2->byval_arg.data.generic_param->num;
2568 * mono_metadata_type_equal:
2572 * Determine if @t1 and @t2 represent the same type.
2573 * Returns: #TRUE if @t1 and @t2 are equal.
2576 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
2578 if (t1->type != t2->type ||
2579 t1->byref != t2->byref)
2583 case MONO_TYPE_VOID:
2584 case MONO_TYPE_BOOLEAN:
2585 case MONO_TYPE_CHAR:
2596 case MONO_TYPE_STRING:
2599 case MONO_TYPE_OBJECT:
2600 case MONO_TYPE_TYPEDBYREF:
2602 case MONO_TYPE_VALUETYPE:
2603 case MONO_TYPE_CLASS:
2604 case MONO_TYPE_SZARRAY:
2605 return mono_metadata_class_equal (t1->data.klass, t2->data.klass);
2607 return mono_metadata_type_equal (t1->data.type, t2->data.type);
2608 case MONO_TYPE_ARRAY:
2609 if (t1->data.array->rank != t2->data.array->rank)
2611 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass);
2612 case MONO_TYPE_GENERICINST:
2613 return mono_metadata_generic_inst_equal (t1->data.generic_inst,
2614 t2->data.generic_inst);
2616 case MONO_TYPE_MVAR:
2617 return t1->data.generic_param->num == t2->data.generic_param->num;
2619 g_error ("implement type compare for %0x!", t1->type);
2627 * mono_metadata_signature_equal:
2628 * @sig1: a signature
2629 * @sig2: another signature
2631 * Determine if @sig1 and @sig2 represent the same signature, with the
2632 * same number of arguments and the same types.
2633 * Returns: #TRUE if @sig1 and @sig2 are equal.
2636 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
2640 if (sig1->hasthis != sig2->hasthis ||
2641 sig1->param_count != sig2->param_count)
2644 for (i = 0; i < sig1->param_count; i++) {
2645 MonoType *p1 = sig1->params[i];
2646 MonoType *p2 = sig2->params[i];
2648 /* if (p1->attrs != p2->attrs)
2651 if (!mono_metadata_type_equal (p1, p2))
2655 if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
2661 mono_signature_hash (MonoMethodSignature *sig)
2663 guint i, res = sig->ret->type;
2665 for (i = 0; i < sig->param_count; i++)
2666 res = (res << 5) - res + sig->params[i]->type;
2672 * mono_metadata_encode_value:
2673 * @value: value to encode
2674 * @buf: buffer where to write the compressed representation
2675 * @endbuf: pointer updated to point at the end of the encoded output
2677 * Encodes the value @value in the compressed representation used
2678 * in metadata and stores the result in @buf. @buf needs to be big
2679 * enough to hold the data (4 bytes).
2682 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
2688 else if (value < 0x4000) {
2689 p [0] = 0x80 | (value >> 8);
2690 p [1] = value & 0xff;
2693 p [0] = (value >> 24) | 0xc0;
2694 p [1] = (value >> 16) & 0xff;
2695 p [2] = (value >> 8) & 0xff;
2696 p [3] = value & 0xff;
2704 * mono_metadata_field_info:
2705 * @meta: the Image the field is defined in
2706 * @index: the index in the field table representing the field
2707 * @offset: a pointer to an integer where to store the offset that
2708 * may have been specified for the field in a FieldLayout table
2709 * @rva: a pointer to the RVA of the field data in the image that
2710 * may have been defined in a FieldRVA table
2711 * @marshal_spec: a pointer to the marshal spec that may have been
2712 * defined for the field in a FieldMarshal table.
2714 * Gather info for field @index that may have been defined in the FieldLayout,
2715 * FieldRVA and FieldMarshal tables.
2716 * Either of offset, rva and marshal_spec can be NULL if you're not interested
2720 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
2721 MonoMarshalSpec **marshal_spec)
2723 MonoTableInfo *tdef;
2726 loc.idx = index + 1;
2728 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
2730 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
2733 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2734 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
2736 *offset = (guint32)-1;
2740 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
2742 loc.col_idx = MONO_FIELD_RVA_FIELD;
2745 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2747 * LAMESPEC: There is no signature, no nothing, just the raw data.
2749 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
2757 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
2758 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
2765 * mono_metadata_get_constant_index:
2766 * @meta: the Image the field is defined in
2767 * @index: the token that may have a row defined in the constants table
2768 * @hint: possible position for the row
2770 * @token must be a FieldDef, ParamDef or PropertyDef token.
2772 * Returns: the index into the Constants table or 0 if not found.
2775 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
2777 MonoTableInfo *tdef;
2779 guint32 index = mono_metadata_token_index (token);
2781 tdef = &meta->tables [MONO_TABLE_CONSTANT];
2782 index <<= MONO_HASCONSTANT_BITS;
2783 switch (mono_metadata_token_table (token)) {
2784 case MONO_TABLE_FIELD:
2785 index |= MONO_HASCONSTANT_FIEDDEF;
2787 case MONO_TABLE_PARAM:
2788 index |= MONO_HASCONSTANT_PARAM;
2790 case MONO_TABLE_PROPERTY:
2791 index |= MONO_HASCONSTANT_PROPERTY;
2794 g_warning ("Not a valid token for the constant table: 0x%08x", token);
2798 loc.col_idx = MONO_CONSTANT_PARENT;
2801 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
2804 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
2805 return loc.result + 1;
2811 * mono_metadata_events_from_typedef:
2812 * @meta: metadata context
2813 * @index: 0-based index (in the TypeDef table) describing a type
2815 * Returns: the 0-based index in the Event table for the events in the
2816 * type. The last event that belongs to the type (plus 1) is stored
2817 * in the @end_idx pointer.
2820 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
2824 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
2832 loc.col_idx = MONO_EVENT_MAP_PARENT;
2833 loc.idx = index + 1;
2835 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2838 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
2839 if (loc.result + 1 < tdef->rows) {
2840 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
2842 end = meta->tables [MONO_TABLE_EVENT].rows;
2850 * mono_metadata_methods_from_event:
2851 * @meta: metadata context
2852 * @index: 0-based index (in the Event table) describing a event
2854 * Returns: the 0-based index in the MethodDef table for the methods in the
2855 * event. The last method that belongs to the event (plus 1) is stored
2856 * in the @end_idx pointer.
2859 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
2863 guint32 cols [MONO_METHOD_SEMA_SIZE];
2864 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
2871 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
2872 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
2874 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
2879 * We may end up in the middle of the rows...
2882 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
2888 while (end < msemt->rows) {
2889 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
2890 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
2899 * mono_metadata_properties_from_typedef:
2900 * @meta: metadata context
2901 * @index: 0-based index (in the TypeDef table) describing a type
2903 * Returns: the 0-based index in the Property table for the properties in the
2904 * type. The last property that belongs to the type (plus 1) is stored
2905 * in the @end_idx pointer.
2908 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
2912 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
2920 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
2921 loc.idx = index + 1;
2923 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2926 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
2927 if (loc.result + 1 < tdef->rows) {
2928 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
2930 end = meta->tables [MONO_TABLE_PROPERTY].rows;
2938 * mono_metadata_methods_from_property:
2939 * @meta: metadata context
2940 * @index: 0-based index (in the PropertyDef table) describing a property
2942 * Returns: the 0-based index in the MethodDef table for the methods in the
2943 * property. The last method that belongs to the property (plus 1) is stored
2944 * in the @end_idx pointer.
2947 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
2951 guint32 cols [MONO_METHOD_SEMA_SIZE];
2952 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
2959 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
2960 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
2962 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
2967 * We may end up in the middle of the rows...
2970 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
2976 while (end < msemt->rows) {
2977 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
2978 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
2987 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
2990 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
2996 loc.col_idx = MONO_IMPLMAP_MEMBER;
2997 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
2999 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3002 return loc.result + 1;
3006 * @image: context where the image is created
3007 * @type_spec: typespec token
3009 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
3013 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
3015 guint32 idx = mono_metadata_token_index (type_spec);
3017 guint32 cols [MONO_TYPESPEC_SIZE];
3022 mono_loader_lock ();
3024 if ((type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec)))) {
3025 mono_loader_unlock ();
3029 t = &image->tables [MONO_TABLE_TYPESPEC];
3031 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
3032 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
3033 len = mono_metadata_decode_value (ptr, &ptr);
3035 type = g_new0 (MonoType, 1);
3037 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
3039 if (*ptr == MONO_TYPE_BYREF) {
3044 do_mono_metadata_parse_type (type, image, ptr, &ptr);
3046 mono_loader_unlock ();
3052 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
3054 MonoMarshalSpec *res;
3056 const char *start = ptr;
3058 /* fixme: this is incomplete, but I cant find more infos in the specs */
3060 res = g_new0 (MonoMarshalSpec, 1);
3062 len = mono_metadata_decode_value (ptr, &ptr);
3063 res->native = *ptr++;
3065 if (res->native == MONO_NATIVE_LPARRAY) {
3066 if (ptr - start <= len)
3067 res->data.array_data.elem_type = *ptr++;
3068 if (ptr - start <= len)
3069 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
3070 if (ptr - start <= len)
3071 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3074 if (res->native == MONO_NATIVE_BYVALTSTR) {
3075 if (ptr - start <= len)
3076 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3079 if (res->native == MONO_NATIVE_BYVALARRAY) {
3080 if (ptr - start <= len)
3081 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3084 if (res->native == MONO_NATIVE_CUSTOM) {
3085 /* skip unused type guid */
3086 len = mono_metadata_decode_value (ptr, &ptr);
3088 /* skip unused native type name */
3089 len = mono_metadata_decode_value (ptr, &ptr);
3091 /* read custom marshaler type name */
3092 len = mono_metadata_decode_value (ptr, &ptr);
3093 res->data.custom_data.custom_name = g_strndup (ptr, len);
3095 /* read cookie string */
3096 len = mono_metadata_decode_value (ptr, &ptr);
3097 res->data.custom_data.cookie = g_strndup (ptr, len);
3104 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
3106 if (spec->native == MONO_NATIVE_CUSTOM) {
3107 g_free (spec->data.custom_data.custom_name);
3108 g_free (spec->data.custom_data.cookie);
3114 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
3115 gboolean unicode, MonoMarshalConv *conv)
3117 MonoMarshalConv dummy_conv;
3123 *conv = MONO_MARSHAL_CONV_NONE;
3126 return MONO_NATIVE_UINT;
3130 case MONO_TYPE_BOOLEAN:
3132 switch (mspec->native) {
3133 case MONO_NATIVE_VARIANTBOOL:
3134 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
3135 return MONO_NATIVE_VARIANTBOOL;
3136 case MONO_NATIVE_BOOLEAN:
3137 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3138 return MONO_NATIVE_BOOLEAN;
3139 case MONO_NATIVE_I1:
3140 case MONO_NATIVE_U1:
3141 return mspec->native;
3143 g_error ("cant marshal bool to native type %02x", mspec->native);
3146 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3147 return MONO_NATIVE_BOOLEAN;
3148 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
3149 case MONO_TYPE_I1: return MONO_NATIVE_I1;
3150 case MONO_TYPE_U1: return MONO_NATIVE_U1;
3151 case MONO_TYPE_I2: return MONO_NATIVE_I2;
3152 case MONO_TYPE_U2: return MONO_NATIVE_U2;
3153 case MONO_TYPE_I4: return MONO_NATIVE_I4;
3154 case MONO_TYPE_U4: return MONO_NATIVE_U4;
3155 case MONO_TYPE_I8: return MONO_NATIVE_I8;
3156 case MONO_TYPE_U8: return MONO_NATIVE_U8;
3157 case MONO_TYPE_R4: return MONO_NATIVE_R4;
3158 case MONO_TYPE_R8: return MONO_NATIVE_R8;
3159 case MONO_TYPE_STRING:
3161 switch (mspec->native) {
3162 case MONO_NATIVE_BSTR:
3163 *conv = MONO_MARSHAL_CONV_STR_BSTR;
3164 return MONO_NATIVE_BSTR;
3165 case MONO_NATIVE_LPSTR:
3166 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
3167 return MONO_NATIVE_LPSTR;
3168 case MONO_NATIVE_LPWSTR:
3169 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
3170 return MONO_NATIVE_LPWSTR;
3171 case MONO_NATIVE_LPTSTR:
3172 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3173 return MONO_NATIVE_LPTSTR;
3174 case MONO_NATIVE_ANSIBSTR:
3175 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
3176 return MONO_NATIVE_ANSIBSTR;
3177 case MONO_NATIVE_TBSTR:
3178 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
3179 return MONO_NATIVE_TBSTR;
3180 case MONO_NATIVE_BYVALTSTR:
3182 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
3184 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
3185 return MONO_NATIVE_BYVALTSTR;
3187 g_error ("cant marshal string to native type %02x", mspec->native);
3190 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3191 return MONO_NATIVE_LPTSTR;
3192 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
3193 case MONO_TYPE_VALUETYPE: /*FIXME*/
3194 if (type->data.klass->enumtype) {
3195 t = type->data.klass->enum_basetype->type;
3198 return MONO_NATIVE_STRUCT;
3199 case MONO_TYPE_SZARRAY:
3200 case MONO_TYPE_ARRAY:
3202 switch (mspec->native) {
3203 case MONO_NATIVE_BYVALARRAY:
3204 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
3205 return MONO_NATIVE_BYVALARRAY;
3206 case MONO_NATIVE_SAFEARRAY:
3207 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
3208 return MONO_NATIVE_SAFEARRAY;
3209 case MONO_NATIVE_LPARRAY:
3210 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3211 return MONO_NATIVE_LPARRAY;
3213 g_error ("cant marshal array as native type %02x", mspec->native);
3217 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3218 return MONO_NATIVE_LPARRAY;
3219 case MONO_TYPE_I: return MONO_NATIVE_INT;
3220 case MONO_TYPE_U: return MONO_NATIVE_UINT;
3221 case MONO_TYPE_CLASS:
3222 case MONO_TYPE_OBJECT: {
3223 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
3225 switch (mspec->native) {
3226 case MONO_NATIVE_STRUCT:
3227 return MONO_NATIVE_STRUCT;
3228 case MONO_NATIVE_INTERFACE:
3229 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
3230 return MONO_NATIVE_INTERFACE;
3231 case MONO_NATIVE_IDISPATCH:
3232 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
3233 return MONO_NATIVE_IDISPATCH;
3234 case MONO_NATIVE_IUNKNOWN:
3235 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
3236 return MONO_NATIVE_IUNKNOWN;
3237 case MONO_NATIVE_FUNC:
3238 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3239 type->data.klass == mono_defaults.delegate_class ||
3240 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3241 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3242 return MONO_NATIVE_FUNC;
3248 g_error ("cant marshal object as native type %02x", mspec->native);
3251 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3252 type->data.klass == mono_defaults.delegate_class ||
3253 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3254 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3255 return MONO_NATIVE_FUNC;
3257 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
3258 return MONO_NATIVE_STRUCT;
3260 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
3261 case MONO_TYPE_TYPEDBYREF:
3263 g_error ("type 0x%02x not handled in marshal", t);
3265 return MONO_NATIVE_MAX;
3269 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
3272 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
3278 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
3279 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
3281 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3284 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
3288 method_from_method_def_or_ref (MonoImage *m, guint32 tok)
3290 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
3291 switch (tok & MONO_METHODDEFORREF_MASK) {
3292 case MONO_METHODDEFORREF_METHODDEF:
3293 return mono_get_method (m, MONO_TOKEN_METHOD_DEF | idx, NULL);
3294 case MONO_METHODDEFORREF_METHODREF:
3295 return mono_get_method (m, MONO_TOKEN_MEMBER_REF | idx, NULL);
3297 g_assert_not_reached ();
3302 mono_class_get_overrides (MonoImage *image, guint32 type_token, gint32 *num_overrides)
3305 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
3308 guint32 cols [MONO_METHODIMPL_SIZE];
3309 MonoMethod **result;
3318 loc.col_idx = MONO_METHODIMPL_CLASS;
3319 loc.idx = mono_metadata_token_index (type_token);
3321 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3327 * We may end up in the middle of the rows...
3330 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
3335 while (end < tdef->rows) {
3336 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
3342 result = g_new (MonoMethod*, num * 2);
3343 for (i = 0; i < num; ++i) {
3344 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
3345 result [i * 2] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_DECLARATION]);
3346 result [i * 2 + 1] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_BODY]);
3350 *num_overrides = num;
3355 * mono_guid_to_string:
3357 * Converts a 16 byte Microsoft GUID to the standard string representation.
3360 mono_guid_to_string (const guint8 *guid)
3362 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
3363 guid[3], guid[2], guid[1], guid[0],
3367 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
3371 get_constraints (MonoImage *image, int owner)
3373 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
3374 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
3375 guint32 i, token, found;
3376 MonoClass *klass, **res;
3377 GList *cons = NULL, *tmp;
3381 for (i = 0; i < tdef->rows; ++i) {
3382 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
3383 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
3384 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
3385 klass = mono_class_get (image, token);
3386 cons = g_list_append (cons, klass);
3389 /* contiguous list finished */
3396 res = g_new0 (MonoClass*, found + 1);
3397 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
3398 res [i] = tmp->data;
3405 mono_metadata_load_generic_params (MonoImage *image, guint32 token, guint32 *num)
3407 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
3408 guint32 cols [MONO_GENERICPARAM_SIZE];
3409 guint32 i, owner, last_num, n;
3410 MonoGenericParam *params;
3412 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
3413 owner = MONO_TYPEORMETHOD_TYPE;
3414 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3415 owner = MONO_TYPEORMETHOD_METHOD;
3417 g_error ("wrong token %x to load_generics_params", token);
3419 owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
3425 for (i = 0; i < tdef->rows; ++i) {
3426 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3427 if (cols [MONO_GENERICPARAM_OWNER] == owner)
3431 if (i >= tdef->rows)
3437 params = g_realloc (params, sizeof (MonoGenericParam) * n);
3438 params [n - 1].pklass = NULL;
3439 params [n - 1].method = NULL;
3440 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
3441 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
3442 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
3443 params [n - 1].constraints = get_constraints (image, i + 1);
3444 if (++i >= tdef->rows)
3446 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3447 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
3455 mono_type_is_byref (MonoType *type)
3461 mono_type_get_type (MonoType *type)
3466 /* For MONO_TYPE_FNPTR */
3467 MonoMethodSignature*
3468 mono_type_get_signature (MonoType *type)
3470 return type->data.method;
3473 /* For MONO_TYPE_CLASS, VALUETYPE */
3475 mono_type_get_class (MonoType *type)
3477 return type->data.klass;
3480 /* For MONO_TYPE_ARRAY */
3482 mono_type_get_array_type (MonoType *type)
3484 return type->data.array;
3488 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
3490 /* FIXME: implement */
3495 mono_signature_get_return_type (MonoMethodSignature *sig)
3501 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
3507 /* start from the first */
3508 if (sig->param_count) {
3509 *iter = &sig->params [0];
3510 return sig->params [0];
3518 if (type < &sig->params [sig->param_count]) {
3526 mono_signature_get_param_count (MonoMethodSignature *sig)
3528 return sig->param_count;
3532 mono_signature_get_call_conv (MonoMethodSignature *sig)
3534 return sig->call_convention;
3538 mono_signature_vararg_start (MonoMethodSignature *sig)
3540 return sig->sentinelpos;
3544 mono_signature_is_instance (MonoMethodSignature *sig)
3546 return sig->hasthis;
3550 mono_signature_explicit_this (MonoMethodSignature *sig)
3552 return sig->explicit_this;