2 * metadata.c: Routines for accessing the metadata
5 * Miguel de Icaza (miguel@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * (C) 2001-2002 Ximian, Inc.
17 #include "tabledefs.h"
18 #include "mono-endian.h"
20 #include "tokentype.h"
21 #include "metadata-internals.h"
22 #include "class-internals.h"
26 static void do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *generic_container,
27 const char *ptr, const char **rptr);
29 static gboolean do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only);
32 * Encoding of the "description" argument:
34 * identifier [CODE ARG]
36 * If CODE is ':', then a lookup on table ARG is performed
37 * If CODE is '=', then a lookup in the aliased-table ARG is performed
38 * If CODE is '#', then this encodes a flag, ARG is the flag name.
40 * Aliased table for example is `CustomAttributeType' which depending on the
41 * information might refer to different tables.
44 const static MonoMetaTable AssemblySchema [] = {
45 { MONO_MT_UINT32, "HashId" },
46 { MONO_MT_UINT16, "Major" },
47 { MONO_MT_UINT16, "Minor" },
48 { MONO_MT_UINT16, "BuildNumber" },
49 { MONO_MT_UINT16, "RevisionNumber" },
50 { MONO_MT_UINT32, "Flags" },
51 { MONO_MT_BLOB_IDX, "PublicKey" },
52 { MONO_MT_STRING_IDX, "Name" },
53 { MONO_MT_STRING_IDX, "Culture" },
57 const static MonoMetaTable AssemblyOSSchema [] = {
58 { MONO_MT_UINT32, "OSPlatformID" },
59 { MONO_MT_UINT32, "OSMajor" },
60 { MONO_MT_UINT32, "OSMinor" },
64 const static MonoMetaTable AssemblyProcessorSchema [] = {
65 { MONO_MT_UINT32, "Processor" },
69 const static MonoMetaTable AssemblyRefSchema [] = {
70 { MONO_MT_UINT16, "Major" },
71 { MONO_MT_UINT16, "Minor" },
72 { MONO_MT_UINT16, "Build" },
73 { MONO_MT_UINT16, "Revision" },
74 { MONO_MT_UINT32, "Flags" },
75 { MONO_MT_BLOB_IDX, "PublicKeyOrToken" },
76 { MONO_MT_STRING_IDX, "Name" },
77 { MONO_MT_STRING_IDX, "Culture" },
78 { MONO_MT_BLOB_IDX, "HashValue" },
82 const static MonoMetaTable AssemblyRefOSSchema [] = {
83 { MONO_MT_UINT32, "OSPlatformID" },
84 { MONO_MT_UINT32, "OSMajorVersion" },
85 { MONO_MT_UINT32, "OSMinorVersion" },
86 { MONO_MT_TABLE_IDX, "AssemblyRef:AssemblyRef" },
90 const static MonoMetaTable AssemblyRefProcessorSchema [] = {
91 { MONO_MT_UINT32, "Processor" },
92 { MONO_MT_TABLE_IDX, "AssemblyRef:AssemblyRef" },
96 const static MonoMetaTable ClassLayoutSchema [] = {
97 { MONO_MT_UINT16, "PackingSize" },
98 { MONO_MT_UINT32, "ClassSize" },
99 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
100 { MONO_MT_END, NULL }
103 const static MonoMetaTable ConstantSchema [] = {
104 { MONO_MT_UINT8, "Type" },
105 { MONO_MT_UINT8, "PaddingZero" },
106 { MONO_MT_CONST_IDX, "Parent" },
107 { MONO_MT_BLOB_IDX, "Value" },
108 { MONO_MT_END, NULL }
111 const static MonoMetaTable CustomAttributeSchema [] = {
112 { MONO_MT_HASCAT_IDX, "Parent" },
113 { MONO_MT_CAT_IDX, "Type" },
114 { MONO_MT_BLOB_IDX, "Value" },
115 { MONO_MT_END, NULL }
118 const static MonoMetaTable DeclSecuritySchema [] = {
119 { MONO_MT_UINT16, "Action" },
120 { MONO_MT_HASDEC_IDX, "Parent" },
121 { MONO_MT_BLOB_IDX, "PermissionSet" },
122 { MONO_MT_END, NULL }
125 const static MonoMetaTable EventMapSchema [] = {
126 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
127 { MONO_MT_TABLE_IDX, "EventList:Event" },
128 { MONO_MT_END, NULL }
131 const static MonoMetaTable EventSchema [] = {
132 { MONO_MT_UINT16, "EventFlags#EventAttribute" },
133 { MONO_MT_STRING_IDX, "Name" },
134 { MONO_MT_TABLE_IDX, "EventType" }, /* TypeDef or TypeRef */
135 { MONO_MT_END, NULL }
138 const static MonoMetaTable ExportedTypeSchema [] = {
139 { MONO_MT_UINT32, "Flags" },
140 { MONO_MT_TABLE_IDX, "TypeDefId" },
141 { MONO_MT_STRING_IDX, "TypeName" },
142 { MONO_MT_STRING_IDX, "TypeNameSpace" },
143 { MONO_MT_IMPL_IDX, "Implementation" },
144 { MONO_MT_END, NULL }
147 const static MonoMetaTable FieldSchema [] = {
148 { MONO_MT_UINT16, "Flags" },
149 { MONO_MT_STRING_IDX, "Name" },
150 { MONO_MT_BLOB_IDX, "Signature" },
151 { MONO_MT_END, NULL }
153 const static MonoMetaTable FieldLayoutSchema [] = {
154 { MONO_MT_UINT32, "Offset" },
155 { MONO_MT_TABLE_IDX, "Field:Field" },
156 { MONO_MT_END, NULL }
159 const static MonoMetaTable FieldMarshalSchema [] = {
160 { MONO_MT_HFM_IDX, "Parent" },
161 { MONO_MT_BLOB_IDX, "NativeType" },
162 { MONO_MT_END, NULL }
164 const static MonoMetaTable FieldRVASchema [] = {
165 { MONO_MT_UINT32, "RVA" },
166 { MONO_MT_TABLE_IDX, "Field:Field" },
167 { MONO_MT_END, NULL }
170 const static MonoMetaTable FileSchema [] = {
171 { MONO_MT_UINT32, "Flags" },
172 { MONO_MT_STRING_IDX, "Name" },
173 { MONO_MT_BLOB_IDX, "Value" },
174 { MONO_MT_END, NULL }
177 const static MonoMetaTable ImplMapSchema [] = {
178 { MONO_MT_UINT16, "MappingFlag" },
179 { MONO_MT_MF_IDX, "MemberForwarded" },
180 { MONO_MT_STRING_IDX, "ImportName" },
181 { MONO_MT_TABLE_IDX, "ImportScope:ModuleRef" },
182 { MONO_MT_END, NULL }
185 const static MonoMetaTable InterfaceImplSchema [] = {
186 { MONO_MT_TABLE_IDX, "Class:TypeDef" },
187 { MONO_MT_TDOR_IDX, "Interface=TypeDefOrRef" },
188 { MONO_MT_END, NULL }
191 const static MonoMetaTable ManifestResourceSchema [] = {
192 { MONO_MT_UINT32, "Offset" },
193 { MONO_MT_UINT32, "Flags" },
194 { MONO_MT_STRING_IDX, "Name" },
195 { MONO_MT_IMPL_IDX, "Implementation" },
196 { MONO_MT_END, NULL }
199 const static MonoMetaTable MemberRefSchema [] = {
200 { MONO_MT_MRP_IDX, "Class" },
201 { MONO_MT_STRING_IDX, "Name" },
202 { MONO_MT_BLOB_IDX, "Signature" },
203 { MONO_MT_END, NULL }
206 const static MonoMetaTable MethodSchema [] = {
207 { MONO_MT_UINT32, "RVA" },
208 { MONO_MT_UINT16, "ImplFlags#MethodImplAttributes" },
209 { MONO_MT_UINT16, "Flags#MethodAttribute" },
210 { MONO_MT_STRING_IDX, "Name" },
211 { MONO_MT_BLOB_IDX, "Signature" },
212 { MONO_MT_TABLE_IDX, "ParamList:Param" },
213 { MONO_MT_END, NULL }
216 const static MonoMetaTable MethodImplSchema [] = {
217 { MONO_MT_TABLE_IDX, "Class:TypeDef" },
218 { MONO_MT_MDOR_IDX, "MethodBody" },
219 { MONO_MT_MDOR_IDX, "MethodDeclaration" },
220 { MONO_MT_END, NULL }
223 const static MonoMetaTable MethodSemanticsSchema [] = {
224 { MONO_MT_UINT16, "MethodSemantic" },
225 { MONO_MT_TABLE_IDX, "Method:Method" },
226 { MONO_MT_HS_IDX, "Association" },
227 { MONO_MT_END, NULL }
230 const static MonoMetaTable ModuleSchema [] = {
231 { MONO_MT_UINT16, "Generation" },
232 { MONO_MT_STRING_IDX, "Name" },
233 { MONO_MT_GUID_IDX, "MVID" },
234 { MONO_MT_GUID_IDX, "EncID" },
235 { MONO_MT_GUID_IDX, "EncBaseID" },
236 { MONO_MT_END, NULL }
239 const static MonoMetaTable ModuleRefSchema [] = {
240 { MONO_MT_STRING_IDX, "Name" },
241 { MONO_MT_END, NULL }
244 const static MonoMetaTable NestedClassSchema [] = {
245 { MONO_MT_TABLE_IDX, "NestedClass:TypeDef" },
246 { MONO_MT_TABLE_IDX, "EnclosingClass:TypeDef" },
247 { MONO_MT_END, NULL }
250 const static MonoMetaTable ParamSchema [] = {
251 { MONO_MT_UINT16, "Flags" },
252 { MONO_MT_UINT16, "Sequence" },
253 { MONO_MT_STRING_IDX, "Name" },
254 { MONO_MT_END, NULL }
257 const static MonoMetaTable PropertySchema [] = {
258 { MONO_MT_UINT16, "Flags" },
259 { MONO_MT_STRING_IDX, "Name" },
260 { MONO_MT_BLOB_IDX, "Type" },
261 { MONO_MT_END, NULL }
264 const static MonoMetaTable PropertyMapSchema [] = {
265 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
266 { MONO_MT_TABLE_IDX, "PropertyList:Property" },
267 { MONO_MT_END, NULL }
270 const static MonoMetaTable StandaloneSigSchema [] = {
271 { MONO_MT_BLOB_IDX, "Signature" },
272 { MONO_MT_END, NULL }
275 const static MonoMetaTable TypeDefSchema [] = {
276 { MONO_MT_UINT32, "Flags" },
277 { MONO_MT_STRING_IDX, "Name" },
278 { MONO_MT_STRING_IDX, "Namespace" },
279 { MONO_MT_TDOR_IDX, "Extends" },
280 { MONO_MT_TABLE_IDX, "FieldList:Field" },
281 { MONO_MT_TABLE_IDX, "MethodList:Method" },
282 { MONO_MT_END, NULL }
285 const static MonoMetaTable TypeRefSchema [] = {
286 { MONO_MT_RS_IDX, "ResolutionScope=ResolutionScope" },
287 { MONO_MT_STRING_IDX, "Name" },
288 { MONO_MT_STRING_IDX, "Namespace" },
289 { MONO_MT_END, NULL }
292 const static MonoMetaTable TypeSpecSchema [] = {
293 { MONO_MT_BLOB_IDX, "Signature" },
294 { MONO_MT_END, NULL }
297 const static MonoMetaTable GenericParamSchema [] = {
298 { MONO_MT_UINT16, "Number" },
299 { MONO_MT_UINT16, "Flags" },
300 { MONO_MT_TABLE_IDX, "Owner" }, /* TypeDef or MethodDef */
301 { MONO_MT_STRING_IDX, "Name" },
303 /* soon to be removed */
304 { MONO_MT_TABLE_IDX, "Kind" },
306 { MONO_MT_END, NULL }
309 const static MonoMetaTable MethodSpecSchema [] = {
310 { MONO_MT_MDOR_IDX, "Method" },
311 { MONO_MT_BLOB_IDX, "Signature" },
312 { MONO_MT_END, NULL }
315 const static MonoMetaTable GenericParamConstraintSchema [] = {
316 { MONO_MT_TABLE_IDX, "GenericParam" },
317 { MONO_MT_TDOR_IDX, "Constraint" },
318 { MONO_MT_END, NULL }
321 const static struct {
322 const MonoMetaTable *table;
325 /* 0 */ { ModuleSchema, "Module" },
326 /* 1 */ { TypeRefSchema, "TypeRef" },
327 /* 2 */ { TypeDefSchema, "TypeDef" },
328 /* 3 */ { NULL, NULL },
329 /* 4 */ { FieldSchema, "Field" },
330 /* 5 */ { NULL, NULL },
331 /* 6 */ { MethodSchema, "Method" },
332 /* 7 */ { NULL, NULL },
333 /* 8 */ { ParamSchema, "Param" },
334 /* 9 */ { InterfaceImplSchema, "InterfaceImpl" },
335 /* A */ { MemberRefSchema, "MemberRef" },
336 /* B */ { ConstantSchema, "Constant" },
337 /* C */ { CustomAttributeSchema, "CustomAttribute" },
338 /* D */ { FieldMarshalSchema, "FieldMarshal" },
339 /* E */ { DeclSecuritySchema, "DeclSecurity" },
340 /* F */ { ClassLayoutSchema, "ClassLayout" },
341 /* 10 */ { FieldLayoutSchema, "FieldLayout" },
342 /* 11 */ { StandaloneSigSchema, "StandaloneSig" },
343 /* 12 */ { EventMapSchema, "EventMap" },
344 /* 13 */ { NULL, NULL },
345 /* 14 */ { EventSchema, "Event" },
346 /* 15 */ { PropertyMapSchema, "PropertyMap" },
347 /* 16 */ { NULL, NULL },
348 /* 17 */ { PropertySchema, "PropertyTable" },
349 /* 18 */ { MethodSemanticsSchema, "MethodSemantics" },
350 /* 19 */ { MethodImplSchema, "MethodImpl" },
351 /* 1A */ { ModuleRefSchema, "ModuleRef" },
352 /* 1B */ { TypeSpecSchema, "TypeSpec" },
353 /* 1C */ { ImplMapSchema, "ImplMap" },
354 /* 1D */ { FieldRVASchema, "FieldRVA" },
355 /* 1E */ { NULL, NULL },
356 /* 1F */ { NULL, NULL },
357 /* 20 */ { AssemblySchema, "Assembly" },
358 /* 21 */ { AssemblyProcessorSchema, "AssemblyProcessor" },
359 /* 22 */ { AssemblyOSSchema, "AssemblyOS" },
360 /* 23 */ { AssemblyRefSchema, "AssemblyRef" },
361 /* 24 */ { AssemblyRefProcessorSchema, "AssemblyRefProcessor" },
362 /* 25 */ { AssemblyRefOSSchema, "AssemblyRefOS" },
363 /* 26 */ { FileSchema, "File" },
364 /* 27 */ { ExportedTypeSchema, "ExportedType" },
365 /* 28 */ { ManifestResourceSchema, "ManifestResource" },
366 /* 29 */ { NestedClassSchema, "NestedClass" },
367 /* 2A */ { GenericParamSchema, "GenericParam" },
368 /* 2B */ { MethodSpecSchema, "MethodSpec" },
369 /* 2C */ { GenericParamConstraintSchema, "GenericParamConstraint" },
373 * mono_meta_table_name:
374 * @table: table index
376 * Returns: the name for the @table index
379 mono_meta_table_name (int table)
381 if ((table < 0) || (table > 0x2c))
384 return tables [table].name;
387 /* The guy who wrote the spec for this should not be allowed near a
390 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
391 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
392 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
393 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
394 inverse of this mapping.
397 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
398 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
400 /* Reference: Partition II - 23.2.6 */
402 * mono_metadata_compute_size:
403 * @meta: metadata context
404 * @tableindex: metadata table number
405 * @result_bitfield: pointer to guint32 where to store additional info
407 * mono_metadata_compute_size() computes the lenght in bytes of a single
408 * row in a metadata table. The size of each column is encoded in the
409 * @result_bitfield return value along with the number of columns in the table.
410 * the resulting bitfield should be handed to the mono_metadata_table_size()
411 * and mono_metadata_table_count() macros.
414 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
416 guint32 bitfield = 0;
417 int size = 0, field_size = 0;
420 const MonoMetaTable *table = tables [tableindex].table;
422 for (i = 0; (code = table [i].code) != MONO_MT_END; i++){
425 field_size = 4; break;
428 field_size = 2; break;
431 field_size = 1; break;
433 case MONO_MT_BLOB_IDX:
434 field_size = meta->idx_blob_wide ? 4 : 2; break;
436 case MONO_MT_STRING_IDX:
437 field_size = meta->idx_string_wide ? 4 : 2; break;
439 case MONO_MT_GUID_IDX:
440 field_size = meta->idx_guid_wide ? 4 : 2; break;
442 case MONO_MT_TABLE_IDX:
443 /* Uhm, a table index can point to other tables besides the current one
444 * so, it's not correct to use the rowcount of the current table to
445 * get the size for this column - lupus
447 switch (tableindex) {
448 case MONO_TABLE_ASSEMBLYREFOS:
450 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
451 case MONO_TABLE_ASSEMBLYPROCESSOR:
453 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
454 case MONO_TABLE_CLASSLAYOUT:
456 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
457 case MONO_TABLE_EVENTMAP:
458 g_assert (i == 0 || i == 1);
459 field_size = i ? idx_size (MONO_TABLE_EVENT):
460 idx_size(MONO_TABLE_TYPEDEF);
462 case MONO_TABLE_EVENT:
464 field_size = MAX (idx_size (MONO_TABLE_TYPEDEF), idx_size(MONO_TABLE_TYPEREF));
465 field_size = MAX (field_size, idx_size(MONO_TABLE_TYPESPEC));
467 case MONO_TABLE_EXPORTEDTYPE:
469 /* the index is in another metadata file, so it must be 4 */
470 field_size = 4; break;
471 case MONO_TABLE_FIELDLAYOUT:
473 field_size = idx_size (MONO_TABLE_FIELD); break;
474 case MONO_TABLE_FIELDRVA:
476 field_size = idx_size (MONO_TABLE_FIELD); break;
477 case MONO_TABLE_IMPLMAP:
479 field_size = idx_size (MONO_TABLE_MODULEREF); break;
480 case MONO_TABLE_INTERFACEIMPL:
482 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
483 case MONO_TABLE_METHOD:
485 field_size = idx_size (MONO_TABLE_PARAM); break;
486 case MONO_TABLE_METHODIMPL:
488 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
489 case MONO_TABLE_METHODSEMANTICS:
491 field_size = idx_size (MONO_TABLE_METHOD); break;
492 case MONO_TABLE_NESTEDCLASS:
493 g_assert (i == 0 || i == 1);
494 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
495 case MONO_TABLE_PROPERTYMAP:
496 g_assert (i == 0 || i == 1);
497 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
498 idx_size(MONO_TABLE_TYPEDEF);
500 case MONO_TABLE_TYPEDEF:
501 g_assert (i == 4 || i == 5);
502 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
503 idx_size(MONO_TABLE_METHOD);
504 case MONO_TABLE_GENERICPARAM:
505 g_assert (i == 2 || i == 4 || i == 5);
507 field_size = MAX (idx_size (MONO_TABLE_METHOD), idx_size (MONO_TABLE_TYPEDEF));
509 field_size = idx_size (MONO_TABLE_TYPEDEF);
511 field_size = idx_size (MONO_TABLE_TYPEDEF);
514 case MONO_TABLE_GENERICPARAMCONSTRAINT:
516 field_size = idx_size (MONO_TABLE_GENERICPARAM);
520 g_assert_not_reached ();
522 if (tableindex != MONO_TABLE_EXPORTEDTYPE && field_size != idx_size (tableindex))
523 g_warning ("size changed (%d to %d)", idx_size (tableindex), field_size);
528 * HasConstant: ParamDef, FieldDef, Property
530 case MONO_MT_CONST_IDX:
531 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
532 meta->tables [MONO_TABLE_FIELD].rows);
533 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
535 /* 2 bits to encode tag */
536 field_size = rtsize (n, 16-2);
540 * HasCustomAttribute: points to any table but
543 case MONO_MT_HASCAT_IDX:
545 * We believe that since the signature and
546 * permission are indexing the Blob heap,
547 * we should consider the blob size first
549 /* I'm not a believer - lupus
550 if (meta->idx_blob_wide){
555 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
556 meta->tables [MONO_TABLE_FIELD].rows);
557 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
558 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
559 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
560 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
561 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
562 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
563 n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
564 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
565 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
566 n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
567 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
568 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
569 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
570 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
571 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
572 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
573 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
575 /* 5 bits to encode */
576 field_size = rtsize (n, 16-5);
580 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
581 * MemberRef and String.
583 case MONO_MT_CAT_IDX:
584 /* String is a heap, if it is wide, we know the size */
586 if (meta->idx_string_wide){
591 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
592 meta->tables [MONO_TABLE_TYPEDEF].rows);
593 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
594 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
596 /* 3 bits to encode */
597 field_size = rtsize (n, 16-3);
601 * HasDeclSecurity: Typedef, MethodDef, Assembly
603 case MONO_MT_HASDEC_IDX:
604 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
605 meta->tables [MONO_TABLE_METHOD].rows);
606 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
608 /* 2 bits to encode */
609 field_size = rtsize (n, 16-2);
613 * Implementation: File, AssemblyRef, ExportedType
615 case MONO_MT_IMPL_IDX:
616 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
617 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
618 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
620 /* 2 bits to encode tag */
621 field_size = rtsize (n, 16-2);
625 * HasFieldMarshall: FieldDef, ParamDef
627 case MONO_MT_HFM_IDX:
628 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
629 meta->tables [MONO_TABLE_PARAM].rows);
631 /* 1 bit used to encode tag */
632 field_size = rtsize (n, 16-1);
636 * MemberForwarded: FieldDef, MethodDef
639 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
640 meta->tables [MONO_TABLE_METHOD].rows);
642 /* 1 bit used to encode tag */
643 field_size = rtsize (n, 16-1);
647 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
649 * It is TypeDef, _TypeRef_, TypeSpec, instead.
651 case MONO_MT_TDOR_IDX:
652 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
653 meta->tables [MONO_TABLE_TYPEREF].rows);
654 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
656 /* 2 bits to encode */
657 field_size = rtsize (n, 16-2);
661 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
663 case MONO_MT_MRP_IDX:
664 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
665 meta->tables [MONO_TABLE_TYPEREF].rows);
666 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
667 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
668 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
669 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
671 /* 3 bits to encode */
672 field_size = rtsize (n, 16 - 3);
675 case MONO_MT_MDOR_IDX:
678 * MethodDefOrRef: MethodDef, MemberRef
681 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
682 meta->tables [MONO_TABLE_MEMBERREF].rows);
684 /* 1 bit used to encode tag */
685 field_size = rtsize (n, 16-1);
689 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
692 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
693 meta->tables [MONO_TABLE_MODULEREF].rows);
694 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
695 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
697 /* 2 bits used to encode tag (ECMA spec claims 3) */
698 field_size = rtsize (n, 16 - 2);
703 * encode field size as follows (we just need to
710 bitfield |= (field_size-1) << shift;
713 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
716 *result_bitfield = (i << 24) | bitfield;
721 * mono_metadata_compute_table_bases:
722 * @meta: metadata context to compute table values
724 * Computes the table bases for the metadata structure.
725 * This is an internal function used by the image loader code.
728 mono_metadata_compute_table_bases (MonoImage *meta)
731 const char *base = meta->tables_base;
733 for (i = 0; i < 64; i++){
734 if (meta->tables [i].rows == 0)
737 meta->tables [i].row_size = mono_metadata_compute_size (
738 meta, i, &meta->tables [i].size_bitfield);
739 meta->tables [i].base = base;
740 base += meta->tables [i].rows * meta->tables [i].row_size;
745 * mono_metadata_locate:
746 * @meta: metadata context
747 * @table: table code.
748 * @idx: index of element to retrieve from @table.
750 * Returns: a pointer to the @idx element in the metadata table
751 * whose code is @table.
754 mono_metadata_locate (MonoImage *meta, int table, int idx)
756 /* idx == 0 refers always to NULL */
757 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
759 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
763 * mono_metadata_locate_token:
764 * @meta: metadata context
765 * @token: metadata token
767 * Returns: a pointer to the data in the metadata represented by the
771 mono_metadata_locate_token (MonoImage *meta, guint32 token)
773 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
777 * mono_metadata_get_table:
778 * @table: table to retrieve
780 * Returns: the MonoMetaTable structure for table @table
782 const MonoMetaTable *
783 mono_metadata_get_table (MonoMetaTableEnum table)
787 g_return_val_if_fail ((x > 0) && (x <= MONO_TABLE_LAST), NULL);
789 return tables [table].table;
793 * mono_metadata_string_heap:
794 * @meta: metadata context
795 * @index: index into the string heap.
797 * Returns: an in-memory pointer to the @index in the string heap.
800 mono_metadata_string_heap (MonoImage *meta, guint32 index)
802 g_return_val_if_fail (index < meta->heap_strings.size, "");
803 return meta->heap_strings.data + index;
807 * mono_metadata_user_string:
808 * @meta: metadata context
809 * @index: index into the user string heap.
811 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
814 mono_metadata_user_string (MonoImage *meta, guint32 index)
816 g_return_val_if_fail (index < meta->heap_us.size, "");
817 return meta->heap_us.data + index;
821 * mono_metadata_blob_heap:
822 * @meta: metadata context
823 * @index: index into the blob.
825 * Returns: an in-memory pointer to the @index in the Blob heap.
828 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
830 g_return_val_if_fail (index < meta->heap_blob.size, "");
831 return meta->heap_blob.data + index;
835 * mono_metadata_guid_heap:
836 * @meta: metadata context
837 * @index: index into the guid heap.
839 * Returns: an in-memory pointer to the @index in the guid heap.
842 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
845 index *= 16; /* adjust for guid size and 1-based index */
846 g_return_val_if_fail (index < meta->heap_guid.size, "");
847 return meta->heap_guid.data + index;
851 dword_align (const char *ptr)
853 #if SIZEOF_VOID_P == 8
854 return (const char *) (((guint64) (ptr + 3)) & ~3);
856 return (const char *) (((guint32) (ptr + 3)) & ~3);
861 * mono_metadata_decode_row:
862 * @t: table to extract information from.
863 * @idx: index in table.
864 * @res: array of @res_size cols to store the results in
866 * This decompresses the metadata element @idx in table @t
867 * into the guint32 @res array that has res_size elements
870 mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res_size)
872 guint32 bitfield = t->size_bitfield;
873 int i, count = mono_metadata_table_count (bitfield);
874 const char *data = t->base + idx * t->row_size;
876 g_assert (res_size == count);
878 for (i = 0; i < count; i++){
879 int n = mono_metadata_table_size (bitfield, i);
883 res [i] = *data; break;
885 res [i] = read16 (data); break;
888 res [i] = read32 (data); break;
891 g_assert_not_reached ();
898 * mono_metadata_decode_row_col:
899 * @t: table to extract information from.
900 * @idx: index for row in table.
901 * @col: column in the row.
903 * This function returns the value of column @col from the @idx
904 * row in the table @t.
907 mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col)
909 guint32 bitfield = t->size_bitfield;
911 register const char *data = t->base + idx * t->row_size;
914 g_assert (col < mono_metadata_table_count (bitfield));
916 n = mono_metadata_table_size (bitfield, 0);
917 for (i = 0; i < col; ++i) {
919 n = mono_metadata_table_size (bitfield, i + 1);
925 return read16 (data);
927 return read32 (data);
929 g_assert_not_reached ();
934 * mono_metadata_decode_blob_size:
935 * @ptr: pointer to a blob object
936 * @rptr: the new position of the pointer
938 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
940 * Returns: the size of the blob object
943 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
945 const unsigned char *ptr = (const unsigned char *)xptr;
948 if ((*ptr & 0x80) == 0){
949 size = ptr [0] & 0x7f;
951 } else if ((*ptr & 0x40) == 0){
952 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
955 size = ((ptr [0] & 0x1f) << 24) +
968 * mono_metadata_decode_value:
969 * @ptr: pointer to decode from
970 * @rptr: the new position of the pointer
972 * This routine decompresses 32-bit values as specified in the "Blob and
973 * Signature" section (22.2)
975 * Returns: the decoded value
978 mono_metadata_decode_value (const char *_ptr, const char **rptr)
980 const unsigned char *ptr = (const unsigned char *) _ptr;
981 unsigned char b = *ptr;
984 if ((b & 0x80) == 0){
987 } else if ((b & 0x40) == 0){
988 len = ((b & 0x3f) << 8 | ptr [1]);
991 len = ((b & 0x1f) << 24) |
1004 * mono_metadata_parse_typedef_or_ref:
1005 * @m: a metadata context.
1006 * @ptr: a pointer to an encoded TypedefOrRef in @m
1007 * @rptr: pointer updated to match the end of the decoded stream
1009 * Returns: a token valid in the @m metadata decoded from
1010 * the compressed representation.
1013 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1016 token = mono_metadata_decode_value (ptr, &ptr);
1019 return mono_metadata_token_from_dor (token);
1023 * mono_metadata_parse_custom_mod:
1024 * @m: a metadata context.
1025 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1026 * @ptr: a pointer to (possibly) the start of a custom modifier list
1027 * @rptr: pointer updated to match the end of the decoded stream
1029 * Checks if @ptr points to a type custom modifier compressed representation.
1031 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1034 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1036 MonoCustomMod local;
1037 if ((*ptr == MONO_TYPE_CMOD_OPT) ||
1038 (*ptr == MONO_TYPE_CMOD_REQD)) {
1041 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1042 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1049 * mono_metadata_parse_array:
1050 * @m: a metadata context.
1051 * @ptr: a pointer to an encoded array description.
1052 * @rptr: pointer updated to match the end of the decoded stream
1054 * Decodes the compressed array description found in the metadata @m at @ptr.
1056 * Returns: a #MonoArrayType structure describing the array type
1060 mono_metadata_parse_array_full (MonoImage *m, MonoGenericContainer *generic_container,
1061 const char *ptr, const char **rptr)
1064 MonoArrayType *array = g_new0 (MonoArrayType, 1);
1067 etype = mono_metadata_parse_type_full (m, generic_container, MONO_PARSE_TYPE, 0, ptr, &ptr);
1068 array->eklass = mono_class_from_mono_type (etype);
1069 array->rank = mono_metadata_decode_value (ptr, &ptr);
1071 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1072 if (array->numsizes)
1073 array->sizes = g_new0 (int, array->numsizes);
1074 for (i = 0; i < array->numsizes; ++i)
1075 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1077 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1078 if (array->numlobounds)
1079 array->lobounds = g_new0 (int, array->numlobounds);
1080 for (i = 0; i < array->numlobounds; ++i)
1081 array->lobounds [i] = mono_metadata_decode_value (ptr, &ptr);
1089 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1091 return mono_metadata_parse_array_full (m, NULL, ptr, rptr);
1095 * mono_metadata_free_array:
1096 * @array: array description
1098 * Frees the array description returned from mono_metadata_parse_array().
1101 mono_metadata_free_array (MonoArrayType *array)
1103 g_free (array->sizes);
1104 g_free (array->lobounds);
1109 * need to add common field and param attributes combinations:
1112 * public static literal
1115 * private static literal
1119 /* data, attrs, type, nmods, byref, pinned */
1120 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1121 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1122 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1123 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1124 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1125 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1126 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1127 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1128 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1129 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1130 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1131 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1132 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1133 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1134 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1135 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1136 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1137 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1138 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1139 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1140 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1141 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1142 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1143 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1144 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1145 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1146 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1147 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 0, 0},
1148 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 1, 0},
1149 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1150 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1151 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1152 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1153 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1156 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1158 static GHashTable *type_cache = NULL;
1161 * MonoTypes with modifies are never cached, so we never check or use that field.
1164 mono_type_hash (gconstpointer data)
1166 const MonoType *type = (const MonoType *) data;
1167 return type->type | (type->byref << 8) | (type->attrs << 9);
1171 mono_type_equal (gconstpointer ka, gconstpointer kb)
1173 const MonoType *a = (const MonoType *) ka;
1174 const MonoType *b = (const MonoType *) kb;
1176 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1178 /* need other checks */
1183 * mono_metadata_init:
1185 * Initialize the global variables of this module.
1188 mono_metadata_init (void)
1192 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1194 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1195 g_hash_table_insert (type_cache, &builtin_types [i], &builtin_types [i]);
1199 * mono_metadata_parse_type:
1200 * @m: metadata context
1201 * @mode: king of type that may be found at @ptr
1202 * @opt_attrs: optional attributes to store in the returned type
1203 * @ptr: pointer to the type representation
1204 * @rptr: pointer updated to match the end of the decoded stream
1206 * Decode a compressed type description found at @ptr in @m.
1207 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1208 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1209 * This function can be used to decode type descriptions in method signatures,
1210 * field signatures, locals signatures etc.
1212 * To parse a generic type, `generic_container' points to the current class'es
1213 * (the `generic_container' field in the MonoClass) or the current generic method's
1214 * (the `generic_container' field in the MonoMethodNormal) generic container.
1215 * When we encounter any MONO_TYPE_VAR or MONO_TYPE_MVAR's, they're looked up in
1216 * this MonoGenericContainer.
1218 * Returns: a #MonoType structure representing the decoded type.
1221 mono_metadata_parse_type_full (MonoImage *m, MonoGenericContainer *generic_container, MonoParseTypeMode mode,
1222 short opt_attrs, const char *ptr, const char **rptr)
1224 MonoType *type, *cached;
1226 gboolean byref = FALSE;
1227 gboolean pinned = FALSE;
1228 const char *tmp_ptr;
1233 * According to the spec, custom modifiers should come before the byref
1234 * flag, but the IL produced by ilasm from the following signature:
1235 * object modopt(...) &
1236 * starts with a byref flag, followed by the modifiers. (bug #49802)
1237 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1238 * it would be better to treat byref as real type constructor instead of
1240 * Also, pinned should come before anything else, but some MSV++ produced
1241 * assemblies violate this (#bug 61990).
1244 /* Count the modifiers first */
1249 case MONO_TYPE_PINNED:
1250 case MONO_TYPE_BYREF:
1253 case MONO_TYPE_CMOD_REQD:
1254 case MONO_TYPE_CMOD_OPT:
1256 mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1264 type = g_malloc0 (sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1265 type->num_mods = count;
1267 g_warning ("got more than 64 modifiers in type");
1271 memset (type, 0, sizeof (MonoType));
1274 /* Parse pinned, byref and custom modifiers */
1279 case MONO_TYPE_PINNED:
1283 case MONO_TYPE_BYREF:
1287 case MONO_TYPE_CMOD_REQD:
1288 case MONO_TYPE_CMOD_OPT:
1289 mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1297 type->attrs = opt_attrs;
1298 type->byref = byref;
1299 type->pinned = pinned ? 1 : 0;
1301 do_mono_metadata_parse_type (type, m, generic_container, ptr, &ptr);
1307 /* FIXME: remove the != MONO_PARSE_PARAM condition, this accounts for
1308 * almost 10k (about 2/3rds) of all MonoType's we create.
1310 if (mode != MONO_PARSE_PARAM && !type->num_mods) {
1311 /* no need to free type here, because it is on the stack */
1312 if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
1314 return &type->data.klass->this_arg;
1316 return &type->data.klass->byval_arg;
1318 /* No need to use locking since nobody is modifying the hash table */
1319 if ((cached = g_hash_table_lookup (type_cache, type)))
1323 /*printf ("%x%c %s\n", type->attrs, type->pinned ? 'p' : ' ', mono_type_full_name (type));*/
1326 type = g_memdup (&stype, sizeof (MonoType));
1331 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
1332 const char *ptr, const char **rptr)
1334 return mono_metadata_parse_type_full (m, NULL, mode, opt_attrs, ptr, rptr);
1338 * mono_metadata_parse_signature:
1339 * @image: metadata context
1340 * @toke: metadata token
1342 * Decode a method signature stored in the STANDALONESIG table
1344 * Returns: a MonoMethodSignature describing the signature.
1346 MonoMethodSignature *
1347 mono_metadata_parse_signature_full (MonoImage *image, MonoGenericContainer *generic_container, guint32 token)
1349 MonoTableInfo *tables = image->tables;
1350 guint32 idx = mono_metadata_token_index (token);
1355 return mono_lookup_dynamic_token (image, token);
1357 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1359 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1361 ptr = mono_metadata_blob_heap (image, sig);
1362 mono_metadata_decode_blob_size (ptr, &ptr);
1364 return mono_metadata_parse_method_signature_full (image, generic_container, FALSE, ptr, NULL);
1367 MonoMethodSignature *
1368 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1370 return mono_metadata_parse_signature_full (image, NULL, token);
1373 MonoMethodSignature*
1374 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1376 MonoMethodSignature *sig;
1378 /* later we want to allocate signatures with mempools */
1379 sig = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1380 sig->param_count = nparams;
1381 sig->sentinelpos = -1;
1386 MonoMethodSignature*
1387 mono_metadata_signature_dup (MonoMethodSignature *sig)
1391 sigsize = sizeof (MonoMethodSignature) + sig->param_count * sizeof (MonoType *);
1392 return g_memdup (sig, sigsize);
1396 * mono_metadata_parse_method_signature:
1397 * @m: metadata context
1398 * @def: the MethodDef index or 0 for Ref signatures.
1399 * @ptr: pointer to the signature metadata representation
1400 * @rptr: pointer updated to match the end of the decoded stream
1402 * Decode a method signature stored at @ptr.
1404 * Returns: a MonoMethodSignature describing the signature.
1406 MonoMethodSignature *
1407 mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContainer *generic_container,
1408 int def, const char *ptr, const char **rptr)
1410 MonoMethodSignature *method;
1411 int i, ret_attrs = 0, *pattrs = NULL;
1412 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1413 guint32 gen_param_count = 0;
1414 gboolean is_open = FALSE;
1415 MonoGenericContainer *container = NULL;
1418 gen_param_count = 1;
1423 call_convention = *ptr & 0x0F;
1425 if (gen_param_count)
1426 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1427 param_count = mono_metadata_decode_value (ptr, &ptr);
1428 pattrs = g_new0 (int, param_count);
1431 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1432 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1433 guint32 cols [MONO_PARAM_SIZE];
1434 guint lastp, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1436 if (def < methodt->rows)
1437 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1439 lastp = paramt->rows + 1;
1440 for (i = param_index; i < lastp; ++i) {
1441 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1442 if (!cols [MONO_PARAM_SEQUENCE])
1443 ret_attrs = cols [MONO_PARAM_FLAGS];
1445 pattrs [cols [MONO_PARAM_SEQUENCE] - 1] = cols [MONO_PARAM_FLAGS];
1448 method = mono_metadata_signature_alloc (m, param_count);
1449 method->hasthis = hasthis;
1450 method->explicit_this = explicit_this;
1451 method->call_convention = call_convention;
1452 method->generic_param_count = gen_param_count;
1454 if (gen_param_count)
1455 method->has_type_parameters = 1;
1457 if (gen_param_count && (!generic_container || !generic_container->is_method)) {
1458 container = g_new0 (MonoGenericContainer, 1);
1459 container->parent = generic_container;
1460 container->is_signature = 1;
1462 container->context = g_new0 (MonoGenericContext, 1);
1463 container->context->container = container;
1465 container->type_argc = gen_param_count;
1466 container->type_params = g_new0 (MonoGenericParam, gen_param_count);
1468 for (i = 0; i < gen_param_count; i++) {
1469 container->type_params [i].owner = container;
1470 container->type_params [i].num = i;
1473 container = generic_container;
1475 if (call_convention != 0xa) {
1476 method->ret = mono_metadata_parse_type_full (m, container, MONO_PARSE_RET, ret_attrs, ptr, &ptr);
1477 is_open = mono_class_is_open_constructed_type (method->ret);
1480 if (method->param_count) {
1481 method->sentinelpos = -1;
1483 for (i = 0; i < method->param_count; ++i) {
1484 if (*ptr == MONO_TYPE_SENTINEL) {
1485 if (method->call_convention != MONO_CALL_VARARG || def)
1486 g_error ("found sentinel for methoddef or no vararg method");
1487 method->sentinelpos = i;
1490 method->params [i] = mono_metadata_parse_type_full (
1491 m, container, MONO_PARSE_PARAM, pattrs [i], ptr, &ptr);
1493 is_open = mono_class_is_open_constructed_type (method->params [i]);
1497 method->has_type_parameters = is_open;
1499 if (def && (method->call_convention == MONO_CALL_VARARG))
1500 method->sentinelpos = method->param_count;
1507 * Add signature to a cache and increase ref count...
1512 MonoMethodSignature *
1513 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1515 return mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr);
1519 * mono_metadata_free_method_signature:
1520 * @sig: signature to destroy
1522 * Free the memory allocated in the signature @sig.
1525 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1528 mono_metadata_free_type (sig->ret);
1529 for (i = 0; i < sig->param_count; ++i)
1530 mono_metadata_free_type (sig->params [i]);
1536 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContainer *generic_container,
1537 const char *ptr, const char **rptr)
1539 MonoGenericClass *gclass = g_new0 (MonoGenericClass, 1);
1540 MonoGenericClass *cached;
1544 type->data.generic_class = gclass;
1546 gclass->context = g_new0 (MonoGenericContext, 1);
1547 gclass->context->gclass = gclass;
1549 gclass->klass = g_new0 (MonoClass, 1);
1551 gclass->generic_type = mono_metadata_parse_type_full (m, generic_container, MONO_PARSE_TYPE, 0, ptr, &ptr);
1552 gclass->inst = g_new0 (MonoGenericInst, 1);
1553 gclass->inst->type_argc = count = mono_metadata_decode_value (ptr, &ptr);
1554 gclass->inst->type_argv = g_new0 (MonoType*, count);
1556 gklass = mono_class_from_mono_type (gclass->generic_type);
1557 g_assert ((gclass->container = gklass->generic_container) != NULL);
1560 * Create the klass before parsing the type arguments.
1561 * This is required to support "recursive" definitions.
1562 * See mcs/tests/gen-23.cs for an example.
1565 gclass->init_pending = TRUE;
1567 mono_class_create_generic (gclass);
1569 for (i = 0; i < gclass->inst->type_argc; i++) {
1570 MonoType *t = mono_metadata_parse_type_full (m, generic_container, MONO_PARSE_TYPE, 0, ptr, &ptr);
1572 gclass->inst->type_argv [i] = t;
1573 if (!gclass->inst->is_open)
1574 gclass->inst->is_open = mono_class_is_open_constructed_type (t);
1577 mono_class_create_generic_2 (gclass);
1583 * We may be called multiple times on different metadata to create the same
1584 * instantiated type. This happens for instance if we're part of a method or
1585 * local variable signature.
1587 * It's important to return the same MonoGenericClass * for each particualar
1588 * instantiation of a generic type (ie "Stack<Int32>") to make static fields
1591 * According to the spec ($26.1.5), a static variable in a generic class
1592 * declaration is shared amongst all instances of the same closed constructed
1596 cached = g_hash_table_lookup (m->generic_class_cache, gclass);
1598 g_free (gclass->klass);
1599 g_free (gclass->inst->type_argv);
1600 g_free (gclass->inst);
1603 type->data.generic_class = cached;
1606 g_hash_table_insert (m->generic_class_cache, gclass, gclass);
1608 mono_stats.generic_instance_count++;
1609 mono_stats.generics_metadata_size += sizeof (MonoGenericClass) +
1610 sizeof (MonoGenericContext) +
1611 gclass->inst->type_argc * sizeof (MonoType);
1614 gclass->init_pending = FALSE;
1618 * do_mono_metadata_parse_generic_param:
1619 * @generic_container: Our MonoClass's or MonoMethodNormal's MonoGenericContainer;
1620 * see mono_metadata_parse_type_full() for details.
1621 * Internal routine to parse a generic type parameter.
1623 static MonoGenericParam *
1624 mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContainer *generic_container,
1625 gboolean is_mvar, const char *ptr, const char **rptr)
1629 index = mono_metadata_decode_value (ptr, &ptr);
1634 g_assert (generic_container);
1635 if (generic_container->parent) {
1637 * The current MonoGenericContainer is a generic method -> its `parent'
1638 * points to the containing class'es container.
1640 generic_container = generic_container->parent;
1642 g_assert (generic_container && !generic_container->is_method);
1643 g_assert (index < generic_container->type_argc);
1645 return &generic_container->type_params [index];
1647 g_assert (generic_container && (generic_container->is_method || generic_container->is_signature));
1648 g_assert (index < generic_container->type_argc);
1650 return &generic_container->type_params [index];
1655 * do_mono_metadata_parse_type:
1656 * @type: MonoType to be filled in with the return value
1658 * Internal routine used to "fill" the contents of @type from an
1659 * allocated pointer. This is done this way to avoid doing too
1660 * many mini-allocations (particularly for the MonoFieldType which
1661 * most of the time is just a MonoType, but sometimes might be augmented).
1663 * This routine is used by mono_metadata_parse_type and
1664 * mono_metadata_parse_field_type
1666 * This extracts a Type as specified in Partition II (22.2.12)
1669 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *generic_container,
1670 const char *ptr, const char **rptr)
1672 type->type = mono_metadata_decode_value (ptr, &ptr);
1674 switch (type->type){
1675 case MONO_TYPE_VOID:
1676 case MONO_TYPE_BOOLEAN:
1677 case MONO_TYPE_CHAR:
1690 case MONO_TYPE_STRING:
1691 case MONO_TYPE_OBJECT:
1692 case MONO_TYPE_TYPEDBYREF:
1694 case MONO_TYPE_VALUETYPE:
1695 case MONO_TYPE_CLASS: {
1697 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
1698 type->data.klass = mono_class_get (m, token);
1701 case MONO_TYPE_SZARRAY: {
1702 MonoType *etype = mono_metadata_parse_type_full (m, generic_container, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1703 type->data.klass = mono_class_from_mono_type (etype);
1704 mono_metadata_free_type (etype);
1708 type->data.type = mono_metadata_parse_type_full (m, generic_container, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1710 case MONO_TYPE_FNPTR:
1711 type->data.method = mono_metadata_parse_method_signature_full (m, generic_container, 0, ptr, &ptr);
1713 case MONO_TYPE_ARRAY:
1714 type->data.array = mono_metadata_parse_array_full (m, generic_container, ptr, &ptr);
1717 case MONO_TYPE_MVAR:
1718 type->data.generic_param = mono_metadata_parse_generic_param (m, generic_container, TRUE, ptr, &ptr);
1722 type->data.generic_param = mono_metadata_parse_generic_param (m, generic_container, FALSE, ptr, &ptr);
1725 case MONO_TYPE_GENERICINST:
1726 do_mono_metadata_parse_generic_class (type, m, generic_container, ptr, &ptr);
1730 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
1738 * mono_metadata_free_type:
1739 * @type: type to free
1741 * Free the memory allocated for type @type.
1744 mono_metadata_free_type (MonoType *type)
1746 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
1749 switch (type->type){
1750 case MONO_TYPE_OBJECT:
1751 case MONO_TYPE_STRING:
1752 if (!type->data.klass)
1755 case MONO_TYPE_CLASS:
1756 case MONO_TYPE_VALUETYPE:
1757 if (type == &type->data.klass->byval_arg || type == &type->data.klass->this_arg)
1761 mono_metadata_free_type (type->data.type);
1763 case MONO_TYPE_FNPTR:
1764 mono_metadata_free_method_signature (type->data.method);
1766 case MONO_TYPE_ARRAY:
1767 mono_metadata_free_array (type->data.array);
1775 hex_dump (const char *buffer, int base, int count)
1777 int show_header = 1;
1785 for (i = 0; i < count; i++){
1788 printf ("\n0x%08x: ", (unsigned char) base + i);
1790 printf ("%02x ", (unsigned char) (buffer [i]));
1797 * @mh: The Method header
1798 * @ptr: Points to the beginning of the Section Data (25.3)
1801 parse_section_data (MonoImage *m, MonoMethodHeader *mh, const unsigned char *ptr)
1803 unsigned char sect_data_flags;
1804 const unsigned char *sptr;
1806 guint32 sect_data_len;
1809 /* align on 32-bit boundary */
1810 /* FIXME: not 64-bit clean code */
1811 sptr = ptr = dword_align (ptr);
1812 sect_data_flags = *ptr;
1815 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
1817 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
1820 sect_data_len = ptr [0];
1824 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
1825 hex_dump (sptr, 0, sect_data_len+8);
1826 g_print ("\nheader: ");
1827 hex_dump (sptr-4, 0, 4);
1831 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
1832 const unsigned char *p = dword_align (ptr);
1834 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
1835 /* we could just store a pointer if we don't need to byteswap */
1836 mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
1837 for (i = 0; i < mh->num_clauses; ++i) {
1838 MonoExceptionClause *ec = &mh->clauses [i];
1841 ec->flags = read32 (p);
1843 ec->try_offset = read32 (p);
1845 ec->try_len = read32 (p);
1847 ec->handler_offset = read32 (p);
1849 ec->handler_len = read32 (p);
1851 tof_value = read32 (p);
1854 ec->flags = read16 (p);
1856 ec->try_offset = read16 (p);
1860 ec->handler_offset = read16 (p);
1862 ec->handler_len = *p;
1864 tof_value = read32 (p);
1867 if (ec->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
1868 ec->data.filter_offset = tof_value;
1869 } else if (ec->flags == MONO_EXCEPTION_CLAUSE_NONE) {
1870 ec->data.catch_class = tof_value? mono_class_get (m, tof_value): 0;
1872 ec->data.catch_class = NULL;
1874 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
1878 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
1879 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
1886 * mono_metadata_parse_mh:
1887 * @m: metadata context
1888 * @ptr: pointer to the method header.
1890 * Decode the method header at @ptr, including pointer to the IL code,
1891 * info about local variables and optional exception tables.
1893 * Returns: a MonoMethodHeader.
1896 mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *generic_container, const char *ptr)
1898 MonoMethodHeader *mh;
1899 unsigned char flags = *(const unsigned char *) ptr;
1900 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
1902 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
1903 const unsigned char *code;
1906 g_return_val_if_fail (ptr != NULL, NULL);
1909 case METHOD_HEADER_TINY_FORMAT:
1910 mh = g_new0 (MonoMethodHeader, 1);
1913 local_var_sig_tok = 0;
1914 mh->code_size = flags >> 2;
1917 case METHOD_HEADER_TINY_FORMAT1:
1918 mh = g_new0 (MonoMethodHeader, 1);
1921 local_var_sig_tok = 0;
1924 * The spec claims 3 bits, but the Beta2 is
1927 mh->code_size = flags >> 2;
1930 case METHOD_HEADER_FAT_FORMAT:
1931 fat_flags = read16 (ptr);
1933 hsize = (fat_flags >> 12) & 0xf;
1934 max_stack = read16 (ptr);
1936 code_size = read32 (ptr);
1938 local_var_sig_tok = read32 (ptr);
1941 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
1948 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
1952 * There are more sections
1954 ptr = code + code_size;
1962 if (local_var_sig_tok) {
1963 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
1964 const char *locals_ptr;
1965 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
1966 int len=0, i, bsize;
1968 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
1969 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
1970 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
1971 if (*locals_ptr != 0x07)
1972 g_warning ("wrong signature for locals blob");
1974 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
1975 mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1976 mh->num_locals = len;
1977 for (i = 0; i < len; ++i)
1978 mh->locals [i] = mono_metadata_parse_type_full (
1979 m, generic_container, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
1981 mh = g_new0 (MonoMethodHeader, 1);
1984 mh->code_size = code_size;
1985 mh->max_stack = max_stack;
1986 mh->init_locals = init_locals;
1987 if (fat_flags & METHOD_HEADER_MORE_SECTS)
1988 parse_section_data (m, mh, (const unsigned char*)ptr);
1993 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
1995 return mono_metadata_parse_mh_full (m, NULL, ptr);
1999 * mono_metadata_free_mh:
2000 * @mh: a method header
2002 * Free the memory allocated for the method header.
2005 mono_metadata_free_mh (MonoMethodHeader *mh)
2008 for (i = 0; i < mh->num_locals; ++i)
2009 mono_metadata_free_type (mh->locals[i]);
2010 g_free (mh->clauses);
2015 * mono_metadata_parse_field_type:
2016 * @m: metadata context to extract information from
2017 * @ptr: pointer to the field signature
2018 * @rptr: pointer updated to match the end of the decoded stream
2020 * Parses the field signature, and returns the type information for it.
2022 * Returns: The MonoType that was extracted from @ptr.
2025 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
2027 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
2031 * mono_metadata_parse_param:
2032 * @m: metadata context to extract information from
2033 * @ptr: pointer to the param signature
2034 * @rptr: pointer updated to match the end of the decoded stream
2036 * Parses the param signature, and returns the type information for it.
2038 * Returns: The MonoType that was extracted from @ptr.
2041 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
2043 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
2047 * mono_metadata_token_from_dor:
2048 * @dor_token: A TypeDefOrRef coded index
2050 * dor_token is a TypeDefOrRef coded index: it contains either
2051 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
2052 * bits contain an index into the table.
2054 * Returns: an expanded token
2057 mono_metadata_token_from_dor (guint32 dor_index)
2061 table = dor_index & 0x03;
2062 idx = dor_index >> 2;
2065 case 0: /* TypeDef */
2066 return MONO_TOKEN_TYPE_DEF | idx;
2068 case 1: /* TypeRef */
2069 return MONO_TOKEN_TYPE_REF | idx;
2071 case 2: /* TypeSpec */
2072 return MONO_TOKEN_TYPE_SPEC | idx;
2075 g_assert_not_reached ();
2082 * We use this to pass context information to the row locator
2085 int idx; /* The index that we are trying to locate */
2086 int col_idx; /* The index in the row where idx may be stored */
2087 MonoTableInfo *t; /* pointer to the table */
2092 * How the row locator works.
2097 * ___|___------> _______
2100 * A column in the rows of table A references an index in table B.
2101 * For example A may be the TYPEDEF table and B the METHODDEF table.
2103 * Given an index in table B we want to get the row in table A
2104 * where the column n references our index in B.
2106 * In the locator_t structure:
2108 * col_idx is the column number
2109 * index is the index in table B
2110 * result will be the index in table A
2113 * Table A Table B column (in table A)
2114 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
2115 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
2116 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
2117 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
2118 * METHODSEM PROPERTY ASSOCIATION (encoded index)
2120 * Note that we still don't support encoded indexes.
2124 typedef_locator (const void *a, const void *b)
2126 locator_t *loc = (locator_t *) a;
2127 const char *bb = (const char *) b;
2128 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
2129 guint32 col, col_next;
2131 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
2137 * Need to check that the next row is valid.
2139 if (typedef_index + 1 < loc->t->rows) {
2140 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
2141 if (loc->idx >= col_next)
2144 if (col == col_next)
2148 loc->result = typedef_index;
2154 table_locator (const void *a, const void *b)
2156 locator_t *loc = (locator_t *) a;
2157 const char *bb = (const char *) b;
2158 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2161 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2163 if (loc->idx == col) {
2164 loc->result = table_index;
2174 declsec_locator (const void *a, const void *b)
2176 locator_t *loc = (locator_t *) a;
2177 const char *bb = (const char *) b;
2178 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2181 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2183 if (loc->idx == col) {
2184 loc->result = table_index;
2194 * mono_metadata_typedef_from_field:
2195 * @meta: metadata context
2196 * @index: FieldDef token
2198 * Returns: the 1-based index into the TypeDef table of the type that
2199 * declared the field described by @index, or 0 if not found.
2202 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
2204 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2210 loc.idx = mono_metadata_token_index (index);
2211 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
2214 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2215 g_assert_not_reached ();
2217 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2218 return loc.result + 1;
2222 * mono_metadata_typedef_from_method:
2223 * @meta: metadata context
2224 * @index: MethodDef token
2226 * Returns: the 1-based index into the TypeDef table of the type that
2227 * declared the method described by @index. 0 if not found.
2230 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
2232 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2238 loc.idx = mono_metadata_token_index (index);
2239 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
2242 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2243 g_assert_not_reached ();
2245 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2246 return loc.result + 1;
2250 * mono_metadata_interfaces_from_typedef:
2251 * @meta: metadata context
2252 * @index: typedef token
2254 * Returns: and array of interfaces that the @index typedef token implements.
2255 * The number of elemnts in the array is returned in @count.
2258 mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, guint *count, MonoGenericContext *context)
2260 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
2263 guint32 cols [MONO_INTERFACEIMPL_SIZE];
2271 loc.idx = mono_metadata_token_index (index);
2272 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
2275 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2280 * We may end up in the middle of the rows...
2283 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
2290 while (start < tdef->rows) {
2291 mono_metadata_decode_row (tdef, start, cols, MONO_INTERFACEIMPL_SIZE);
2292 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
2294 result = g_renew (MonoClass*, result, i + 1);
2295 result [i] = mono_class_get_full (
2296 meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]), context);
2304 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
2306 return mono_metadata_interfaces_from_typedef_full (meta, index, count, NULL);
2310 * mono_metadata_nested_in_typedef:
2311 * @meta: metadata context
2312 * @index: typedef token
2314 * Returns: the 1-based index into the TypeDef table of the type
2315 * where the type described by @index is nested.
2316 * Retruns 0 if @index describes a non-nested type.
2319 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
2321 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2327 loc.idx = mono_metadata_token_index (index);
2328 loc.col_idx = MONO_NESTED_CLASS_NESTED;
2331 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2334 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2335 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
2339 * mono_metadata_nesting_typedef:
2340 * @meta: metadata context
2341 * @index: typedef token
2343 * Returns: the 1-based index into the TypeDef table of the first type
2344 * that is nested inside the type described by @index. The search starts at
2345 * @start_index. returns 0 if no such type is found.
2348 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
2350 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2352 guint32 class_index = mono_metadata_token_index (index);
2357 start = start_index;
2359 while (start <= tdef->rows) {
2360 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
2366 if (start > tdef->rows)
2373 * mono_metadata_packing_from_typedef:
2374 * @meta: metadata context
2375 * @index: token representing a type
2377 * Returns: the info stored in the ClassLAyout table for the given typedef token
2378 * into the @packing and @size pointers.
2379 * Returns 0 if the info is not found.
2382 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
2384 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
2386 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
2391 loc.idx = mono_metadata_token_index (index);
2392 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
2395 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2398 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
2400 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
2402 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
2404 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2405 return loc.result + 1;
2409 * mono_metadata_custom_attrs_from_index:
2410 * @meta: metadata context
2411 * @index: token representing the parent
2413 * Returns: the 1-based index into the CustomAttribute table of the first
2414 * attribute which belongs to the metadata object described by @index.
2415 * Returns 0 if no such attribute is found.
2418 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
2420 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
2427 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
2430 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2433 /* Find the first entry by searching backwards */
2434 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
2437 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2438 return loc.result + 1;
2442 * mono_metadata_declsec_from_index:
2443 * @meta: metadata context
2444 * @index: token representing the parent
2446 * Returns: the 0-based index into the DeclarativeSecurity table of the first
2447 * attribute which belongs to the metadata object described by @index.
2448 * Returns -1 if no such attribute is found.
2451 mono_metadata_declsec_from_index (MonoImage *meta, guint32 index)
2453 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_DECLSECURITY];
2460 loc.col_idx = MONO_DECL_SECURITY_PARENT;
2463 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
2466 /* Find the first entry by searching backwards */
2467 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_DECL_SECURITY_PARENT) == index))
2475 mono_backtrace (int limit)
2480 backtrace (array, limit);
2481 names = backtrace_symbols (array, limit);
2482 for (i =0; i < limit; ++i) {
2483 g_print ("\t%s\n", names [i]);
2490 /*#define __alignof__(a) sizeof(a)*/
2491 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
2496 * @t: the type to return the size of
2498 * Returns: the number of bytes required to hold an instance of this
2502 mono_type_size (MonoType *t, gint *align)
2509 *align = __alignof__(gpointer);
2510 return sizeof (gpointer);
2514 case MONO_TYPE_VOID:
2517 case MONO_TYPE_BOOLEAN:
2518 *align = __alignof__(gint8);
2522 *align = __alignof__(gint8);
2524 case MONO_TYPE_CHAR:
2527 *align = __alignof__(gint16);
2531 *align = __alignof__(gint32);
2534 *align = __alignof__(float);
2538 *align = __alignof__(gint64);
2541 *align = __alignof__(double);
2545 *align = __alignof__(gpointer);
2546 return sizeof (gpointer);
2547 case MONO_TYPE_STRING:
2548 *align = __alignof__(gpointer);
2549 return sizeof (gpointer);
2550 case MONO_TYPE_OBJECT:
2551 *align = __alignof__(gpointer);
2552 return sizeof (gpointer);
2553 case MONO_TYPE_VALUETYPE: {
2554 if (t->data.klass->enumtype)
2555 return mono_type_size (t->data.klass->enum_basetype, align);
2557 return mono_class_value_size (t->data.klass, align);
2559 case MONO_TYPE_CLASS:
2560 case MONO_TYPE_SZARRAY:
2562 case MONO_TYPE_FNPTR:
2563 case MONO_TYPE_ARRAY:
2564 *align = __alignof__(gpointer);
2565 return sizeof (gpointer);
2566 case MONO_TYPE_TYPEDBYREF:
2567 return mono_class_value_size (mono_defaults.typed_reference_class, align);
2568 case MONO_TYPE_GENERICINST: {
2569 MonoGenericClass *gclass = t->data.generic_class;
2571 g_assert (!gclass->inst->is_open && !gclass->klass->generic_container);
2573 if (MONO_TYPE_ISSTRUCT (gclass->generic_type)) {
2574 MonoClass *gklass = mono_class_from_mono_type (gclass->generic_type);
2576 if (gklass->enumtype)
2577 return mono_type_size (gklass->enum_basetype, align);
2579 return mono_class_value_size (gclass->klass, align);
2581 *align = __alignof__(gpointer);
2582 return sizeof (gpointer);
2586 case MONO_TYPE_MVAR:
2587 *align = __alignof__(gpointer);
2588 return sizeof (gpointer);
2590 g_error ("mono_type_size: type 0x%02x unknown", t->type);
2596 * mono_type_stack_size:
2597 * @t: the type to return the size it uses on the stack
2599 * Returns: the number of bytes required to hold an instance of this
2600 * type on the runtime stack
2603 mono_type_stack_size (MonoType *t, gint *align)
2607 g_assert (t != NULL);
2613 *align = __alignof__(gpointer);
2614 return sizeof (gpointer);
2618 case MONO_TYPE_BOOLEAN:
2619 case MONO_TYPE_CHAR:
2628 case MONO_TYPE_STRING:
2629 case MONO_TYPE_OBJECT:
2630 case MONO_TYPE_CLASS:
2631 case MONO_TYPE_SZARRAY:
2633 case MONO_TYPE_FNPTR:
2634 case MONO_TYPE_ARRAY:
2635 *align = __alignof__(gpointer);
2636 return sizeof (gpointer);
2637 case MONO_TYPE_TYPEDBYREF:
2638 *align = __alignof__(gpointer);
2639 return sizeof (gpointer) * 3;
2641 *align = __alignof__(float);
2642 return sizeof (float);
2645 *align = __alignof__(gint64);
2646 return sizeof (gint64);
2648 *align = __alignof__(double);
2649 return sizeof (double);
2650 case MONO_TYPE_VALUETYPE: {
2653 if (t->data.klass->enumtype)
2654 return mono_type_stack_size (t->data.klass->enum_basetype, align);
2656 size = mono_class_value_size (t->data.klass, align);
2658 *align = *align + __alignof__(gpointer) - 1;
2659 *align &= ~(__alignof__(gpointer) - 1);
2661 size += sizeof (gpointer) - 1;
2662 size &= ~(sizeof (gpointer) - 1);
2667 case MONO_TYPE_GENERICINST: {
2668 MonoGenericClass *gclass = t->data.generic_class;
2670 g_assert (!gclass->inst->is_open && !gclass->klass->generic_container);
2672 if (MONO_TYPE_ISSTRUCT (gclass->generic_type)) {
2673 MonoClass *gklass = mono_class_from_mono_type (gclass->generic_type);
2675 if (gklass->enumtype)
2676 return mono_type_stack_size (gklass->enum_basetype, align);
2678 guint32 size = mono_class_value_size (gclass->klass, align);
2680 *align = *align + __alignof__(gpointer) - 1;
2681 *align &= ~(__alignof__(gpointer) - 1);
2683 size += sizeof (gpointer) - 1;
2684 size &= ~(sizeof (gpointer) - 1);
2689 *align = __alignof__(gpointer);
2690 return sizeof (gpointer);
2694 g_error ("type 0x%02x unknown", t->type);
2700 mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass)
2702 return MONO_TYPE_ISSTRUCT (gclass->generic_type);
2706 mono_metadata_generic_class_hash (MonoGenericClass *gclass)
2708 return mono_metadata_type_hash (gclass->generic_type);
2712 _mono_metadata_generic_class_equal (MonoGenericClass *g1, MonoGenericClass *g2, gboolean signature_only)
2716 if (g1->inst->type_argc != g2->inst->type_argc)
2718 if (!do_mono_metadata_type_equal (g1->generic_type, g2->generic_type, signature_only))
2720 for (i = 0; i < g1->inst->type_argc; ++i) {
2721 if (!do_mono_metadata_type_equal (g1->inst->type_argv [i], g2->inst->type_argv [i], signature_only))
2728 mono_metadata_generic_class_equal (MonoGenericClass *g1, MonoGenericClass *g2)
2730 return _mono_metadata_generic_class_equal (g1, g2, FALSE);
2734 mono_metadata_generic_method_hash (MonoGenericMethod *gmethod)
2736 return gmethod->inst->type_argc;
2740 mono_metadata_generic_method_equal (MonoGenericMethod *g1, MonoGenericMethod *g2)
2744 if (g1->inst->type_argc != g2->inst->type_argc)
2746 for (i = 0; i < g1->inst->type_argc; ++i) {
2747 if (!mono_metadata_type_equal (g1->inst->type_argv [i], g2->inst->type_argv [i]))
2755 * mono_metadata_type_hash:
2758 * Computes an hash value for @t1 to be used in GHashTable.
2761 mono_metadata_type_hash (MonoType *t1)
2763 guint hash = t1->type;
2765 hash |= t1->byref << 6; /* do not collide with t1->type values */
2767 case MONO_TYPE_VALUETYPE:
2768 case MONO_TYPE_CLASS:
2769 case MONO_TYPE_SZARRAY:
2770 /* check if the distribution is good enough */
2771 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
2773 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
2774 case MONO_TYPE_ARRAY:
2775 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
2776 case MONO_TYPE_GENERICINST:
2777 return ((hash << 5) - hash) ^ mono_metadata_generic_class_hash (t1->data.generic_class);
2783 mono_metadata_generic_param_equal (MonoGenericParam *p1, MonoGenericParam *p2, gboolean signature_only)
2787 if (p1->num != p2->num)
2790 g_assert (p1->owner && p2->owner);
2791 if (p1->owner == p2->owner)
2795 * If `signature_only' is true, we're comparing two (method) signatures.
2796 * In this case, the owner of two type parameters doesn't need to match.
2799 return signature_only;
2803 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only)
2807 if (c1->generic_class && c2->generic_class)
2808 return _mono_metadata_generic_class_equal (c1->generic_class, c2->generic_class, signature_only);
2809 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
2810 return mono_metadata_generic_param_equal (
2811 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, FALSE);
2812 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
2813 return mono_metadata_generic_param_equal (
2814 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
2819 * mono_metadata_type_equal:
2823 * Determine if @t1 and @t2 represent the same type.
2824 * Returns: #TRUE if @t1 and @t2 are equal.
2827 do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only)
2829 if (t1->type != t2->type ||
2830 t1->byref != t2->byref)
2834 case MONO_TYPE_VOID:
2835 case MONO_TYPE_BOOLEAN:
2836 case MONO_TYPE_CHAR:
2847 case MONO_TYPE_STRING:
2850 case MONO_TYPE_OBJECT:
2851 case MONO_TYPE_TYPEDBYREF:
2853 case MONO_TYPE_VALUETYPE:
2854 case MONO_TYPE_CLASS:
2855 case MONO_TYPE_SZARRAY:
2856 return mono_metadata_class_equal (t1->data.klass, t2->data.klass, signature_only);
2858 return do_mono_metadata_type_equal (t1->data.type, t2->data.type, signature_only);
2859 case MONO_TYPE_ARRAY:
2860 if (t1->data.array->rank != t2->data.array->rank)
2862 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass, signature_only);
2863 case MONO_TYPE_GENERICINST:
2864 return _mono_metadata_generic_class_equal (
2865 t1->data.generic_class, t2->data.generic_class, signature_only);
2867 return mono_metadata_generic_param_equal (
2868 t1->data.generic_param, t2->data.generic_param, FALSE);
2869 case MONO_TYPE_MVAR:
2870 return mono_metadata_generic_param_equal (
2871 t1->data.generic_param, t2->data.generic_param, signature_only);
2873 g_error ("implement type compare for %0x!", t1->type);
2881 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
2883 return do_mono_metadata_type_equal (t1, t2, FALSE);
2887 * mono_metadata_signature_equal:
2888 * @sig1: a signature
2889 * @sig2: another signature
2891 * Determine if @sig1 and @sig2 represent the same signature, with the
2892 * same number of arguments and the same types.
2893 * Returns: #TRUE if @sig1 and @sig2 are equal.
2896 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
2900 if (sig1->hasthis != sig2->hasthis ||
2901 sig1->param_count != sig2->param_count)
2905 * We're just comparing the signatures of two methods here:
2907 * If we have two generic methods `void Foo<U> (U u)' and `void Bar<V> (V v)',
2908 * U and V are equal here.
2910 * That's what the `signature_only' argument of do_mono_metadata_type_equal() is for.
2913 for (i = 0; i < sig1->param_count; i++) {
2914 MonoType *p1 = sig1->params[i];
2915 MonoType *p2 = sig2->params[i];
2917 /* if (p1->attrs != p2->attrs)
2920 if (!do_mono_metadata_type_equal (p1, p2, TRUE))
2924 if (!do_mono_metadata_type_equal (sig1->ret, sig2->ret, TRUE))
2930 mono_signature_hash (MonoMethodSignature *sig)
2932 guint i, res = sig->ret->type;
2934 for (i = 0; i < sig->param_count; i++)
2935 res = (res << 5) - res + sig->params[i]->type;
2941 * mono_metadata_encode_value:
2942 * @value: value to encode
2943 * @buf: buffer where to write the compressed representation
2944 * @endbuf: pointer updated to point at the end of the encoded output
2946 * Encodes the value @value in the compressed representation used
2947 * in metadata and stores the result in @buf. @buf needs to be big
2948 * enough to hold the data (4 bytes).
2951 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
2957 else if (value < 0x4000) {
2958 p [0] = 0x80 | (value >> 8);
2959 p [1] = value & 0xff;
2962 p [0] = (value >> 24) | 0xc0;
2963 p [1] = (value >> 16) & 0xff;
2964 p [2] = (value >> 8) & 0xff;
2965 p [3] = value & 0xff;
2973 * mono_metadata_field_info:
2974 * @meta: the Image the field is defined in
2975 * @index: the index in the field table representing the field
2976 * @offset: a pointer to an integer where to store the offset that
2977 * may have been specified for the field in a FieldLayout table
2978 * @rva: a pointer to the RVA of the field data in the image that
2979 * may have been defined in a FieldRVA table
2980 * @marshal_spec: a pointer to the marshal spec that may have been
2981 * defined for the field in a FieldMarshal table.
2983 * Gather info for field @index that may have been defined in the FieldLayout,
2984 * FieldRVA and FieldMarshal tables.
2985 * Either of offset, rva and marshal_spec can be NULL if you're not interested
2989 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
2990 MonoMarshalSpec **marshal_spec)
2992 MonoTableInfo *tdef;
2995 loc.idx = index + 1;
2997 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
2999 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
3002 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3003 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
3005 *offset = (guint32)-1;
3009 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
3011 loc.col_idx = MONO_FIELD_RVA_FIELD;
3014 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3016 * LAMESPEC: There is no signature, no nothing, just the raw data.
3018 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
3026 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
3027 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
3034 * mono_metadata_get_constant_index:
3035 * @meta: the Image the field is defined in
3036 * @index: the token that may have a row defined in the constants table
3037 * @hint: possible position for the row
3039 * @token must be a FieldDef, ParamDef or PropertyDef token.
3041 * Returns: the index into the Constants table or 0 if not found.
3044 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
3046 MonoTableInfo *tdef;
3048 guint32 index = mono_metadata_token_index (token);
3050 tdef = &meta->tables [MONO_TABLE_CONSTANT];
3051 index <<= MONO_HASCONSTANT_BITS;
3052 switch (mono_metadata_token_table (token)) {
3053 case MONO_TABLE_FIELD:
3054 index |= MONO_HASCONSTANT_FIEDDEF;
3056 case MONO_TABLE_PARAM:
3057 index |= MONO_HASCONSTANT_PARAM;
3059 case MONO_TABLE_PROPERTY:
3060 index |= MONO_HASCONSTANT_PROPERTY;
3063 g_warning ("Not a valid token for the constant table: 0x%08x", token);
3067 loc.col_idx = MONO_CONSTANT_PARENT;
3070 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
3073 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3074 return loc.result + 1;
3080 * mono_metadata_events_from_typedef:
3081 * @meta: metadata context
3082 * @index: 0-based index (in the TypeDef table) describing a type
3084 * Returns: the 0-based index in the Event table for the events in the
3085 * type. The last event that belongs to the type (plus 1) is stored
3086 * in the @end_idx pointer.
3089 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3093 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
3101 loc.col_idx = MONO_EVENT_MAP_PARENT;
3102 loc.idx = index + 1;
3104 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3107 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
3108 if (loc.result + 1 < tdef->rows) {
3109 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
3111 end = meta->tables [MONO_TABLE_EVENT].rows;
3119 * mono_metadata_methods_from_event:
3120 * @meta: metadata context
3121 * @index: 0-based index (in the Event table) describing a event
3123 * Returns: the 0-based index in the MethodDef table for the methods in the
3124 * event. The last method that belongs to the event (plus 1) is stored
3125 * in the @end_idx pointer.
3128 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
3132 guint32 cols [MONO_METHOD_SEMA_SIZE];
3133 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3140 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3141 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
3143 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3148 * We may end up in the middle of the rows...
3151 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3157 while (end < msemt->rows) {
3158 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3159 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3168 * mono_metadata_properties_from_typedef:
3169 * @meta: metadata context
3170 * @index: 0-based index (in the TypeDef table) describing a type
3172 * Returns: the 0-based index in the Property table for the properties in the
3173 * type. The last property that belongs to the type (plus 1) is stored
3174 * in the @end_idx pointer.
3177 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3181 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
3189 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
3190 loc.idx = index + 1;
3192 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3195 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
3196 if (loc.result + 1 < tdef->rows) {
3197 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
3199 end = meta->tables [MONO_TABLE_PROPERTY].rows;
3207 * mono_metadata_methods_from_property:
3208 * @meta: metadata context
3209 * @index: 0-based index (in the PropertyDef table) describing a property
3211 * Returns: the 0-based index in the MethodDef table for the methods in the
3212 * property. The last method that belongs to the property (plus 1) is stored
3213 * in the @end_idx pointer.
3216 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
3220 guint32 cols [MONO_METHOD_SEMA_SIZE];
3221 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3228 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3229 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
3231 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3236 * We may end up in the middle of the rows...
3239 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3245 while (end < msemt->rows) {
3246 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3247 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3256 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
3259 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
3265 loc.col_idx = MONO_IMPLMAP_MEMBER;
3266 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
3268 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3271 return loc.result + 1;
3275 * @image: context where the image is created
3276 * @type_spec: typespec token
3278 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
3282 mono_type_create_from_typespec_full (MonoImage *image, MonoGenericContainer *generic_container, guint32 type_spec)
3284 guint32 idx = mono_metadata_token_index (type_spec);
3286 guint32 cols [MONO_TYPESPEC_SIZE];
3291 mono_loader_lock ();
3293 if ((type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec)))) {
3294 mono_loader_unlock ();
3298 t = &image->tables [MONO_TABLE_TYPESPEC];
3300 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
3301 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
3302 len = mono_metadata_decode_value (ptr, &ptr);
3304 type = g_new0 (MonoType, 1);
3306 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
3308 if (*ptr == MONO_TYPE_BYREF) {
3313 do_mono_metadata_parse_type (type, image, generic_container, ptr, &ptr);
3315 mono_loader_unlock ();
3321 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
3323 return mono_type_create_from_typespec_full (image, NULL, type_spec);
3327 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
3329 MonoMarshalSpec *res;
3331 const char *start = ptr;
3333 /* fixme: this is incomplete, but I cant find more infos in the specs */
3335 res = g_new0 (MonoMarshalSpec, 1);
3337 len = mono_metadata_decode_value (ptr, &ptr);
3338 res->native = *ptr++;
3340 if (res->native == MONO_NATIVE_LPARRAY) {
3341 if (ptr - start <= len)
3342 res->data.array_data.elem_type = *ptr++;
3343 if (ptr - start <= len)
3344 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
3345 if (ptr - start <= len)
3346 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3349 if (res->native == MONO_NATIVE_BYVALTSTR) {
3350 if (ptr - start <= len)
3351 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3354 if (res->native == MONO_NATIVE_BYVALARRAY) {
3355 if (ptr - start <= len)
3356 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3359 if (res->native == MONO_NATIVE_CUSTOM) {
3360 /* skip unused type guid */
3361 len = mono_metadata_decode_value (ptr, &ptr);
3363 /* skip unused native type name */
3364 len = mono_metadata_decode_value (ptr, &ptr);
3366 /* read custom marshaler type name */
3367 len = mono_metadata_decode_value (ptr, &ptr);
3368 res->data.custom_data.custom_name = g_strndup (ptr, len);
3370 /* read cookie string */
3371 len = mono_metadata_decode_value (ptr, &ptr);
3372 res->data.custom_data.cookie = g_strndup (ptr, len);
3379 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
3381 if (spec->native == MONO_NATIVE_CUSTOM) {
3382 g_free (spec->data.custom_data.custom_name);
3383 g_free (spec->data.custom_data.cookie);
3389 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
3390 gboolean unicode, MonoMarshalConv *conv)
3392 MonoMarshalConv dummy_conv;
3398 *conv = MONO_MARSHAL_CONV_NONE;
3401 return MONO_NATIVE_UINT;
3405 case MONO_TYPE_BOOLEAN:
3407 switch (mspec->native) {
3408 case MONO_NATIVE_VARIANTBOOL:
3409 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
3410 return MONO_NATIVE_VARIANTBOOL;
3411 case MONO_NATIVE_BOOLEAN:
3412 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3413 return MONO_NATIVE_BOOLEAN;
3414 case MONO_NATIVE_I1:
3415 case MONO_NATIVE_U1:
3416 return mspec->native;
3418 g_error ("cant marshal bool to native type %02x", mspec->native);
3421 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3422 return MONO_NATIVE_BOOLEAN;
3423 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
3424 case MONO_TYPE_I1: return MONO_NATIVE_I1;
3425 case MONO_TYPE_U1: return MONO_NATIVE_U1;
3426 case MONO_TYPE_I2: return MONO_NATIVE_I2;
3427 case MONO_TYPE_U2: return MONO_NATIVE_U2;
3428 case MONO_TYPE_I4: return MONO_NATIVE_I4;
3429 case MONO_TYPE_U4: return MONO_NATIVE_U4;
3430 case MONO_TYPE_I8: return MONO_NATIVE_I8;
3431 case MONO_TYPE_U8: return MONO_NATIVE_U8;
3432 case MONO_TYPE_R4: return MONO_NATIVE_R4;
3433 case MONO_TYPE_R8: return MONO_NATIVE_R8;
3434 case MONO_TYPE_STRING:
3436 switch (mspec->native) {
3437 case MONO_NATIVE_BSTR:
3438 *conv = MONO_MARSHAL_CONV_STR_BSTR;
3439 return MONO_NATIVE_BSTR;
3440 case MONO_NATIVE_LPSTR:
3441 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
3442 return MONO_NATIVE_LPSTR;
3443 case MONO_NATIVE_LPWSTR:
3444 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
3445 return MONO_NATIVE_LPWSTR;
3446 case MONO_NATIVE_LPTSTR:
3447 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3448 return MONO_NATIVE_LPTSTR;
3449 case MONO_NATIVE_ANSIBSTR:
3450 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
3451 return MONO_NATIVE_ANSIBSTR;
3452 case MONO_NATIVE_TBSTR:
3453 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
3454 return MONO_NATIVE_TBSTR;
3455 case MONO_NATIVE_BYVALTSTR:
3457 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
3459 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
3460 return MONO_NATIVE_BYVALTSTR;
3462 g_error ("cant marshal string to native type %02x", mspec->native);
3465 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3466 return MONO_NATIVE_LPTSTR;
3467 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
3468 case MONO_TYPE_VALUETYPE: /*FIXME*/
3469 if (type->data.klass->enumtype) {
3470 t = type->data.klass->enum_basetype->type;
3473 return MONO_NATIVE_STRUCT;
3474 case MONO_TYPE_SZARRAY:
3475 case MONO_TYPE_ARRAY:
3477 switch (mspec->native) {
3478 case MONO_NATIVE_BYVALARRAY:
3479 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
3480 return MONO_NATIVE_BYVALARRAY;
3481 case MONO_NATIVE_SAFEARRAY:
3482 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
3483 return MONO_NATIVE_SAFEARRAY;
3484 case MONO_NATIVE_LPARRAY:
3485 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3486 return MONO_NATIVE_LPARRAY;
3488 g_error ("cant marshal array as native type %02x", mspec->native);
3492 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3493 return MONO_NATIVE_LPARRAY;
3494 case MONO_TYPE_I: return MONO_NATIVE_INT;
3495 case MONO_TYPE_U: return MONO_NATIVE_UINT;
3496 case MONO_TYPE_CLASS:
3497 case MONO_TYPE_OBJECT: {
3498 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
3500 switch (mspec->native) {
3501 case MONO_NATIVE_STRUCT:
3502 return MONO_NATIVE_STRUCT;
3503 case MONO_NATIVE_INTERFACE:
3504 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
3505 return MONO_NATIVE_INTERFACE;
3506 case MONO_NATIVE_IDISPATCH:
3507 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
3508 return MONO_NATIVE_IDISPATCH;
3509 case MONO_NATIVE_IUNKNOWN:
3510 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
3511 return MONO_NATIVE_IUNKNOWN;
3512 case MONO_NATIVE_FUNC:
3513 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3514 type->data.klass == mono_defaults.delegate_class ||
3515 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3516 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3517 return MONO_NATIVE_FUNC;
3523 g_error ("cant marshal object as native type %02x", mspec->native);
3526 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3527 type->data.klass == mono_defaults.delegate_class ||
3528 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3529 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3530 return MONO_NATIVE_FUNC;
3532 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
3533 return MONO_NATIVE_STRUCT;
3535 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
3536 case MONO_TYPE_TYPEDBYREF:
3538 g_error ("type 0x%02x not handled in marshal", t);
3540 return MONO_NATIVE_MAX;
3544 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
3547 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
3553 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
3554 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
3556 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3559 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
3563 method_from_method_def_or_ref (MonoImage *m, guint32 tok)
3565 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
3566 switch (tok & MONO_METHODDEFORREF_MASK) {
3567 case MONO_METHODDEFORREF_METHODDEF:
3568 return mono_get_method (m, MONO_TOKEN_METHOD_DEF | idx, NULL);
3569 case MONO_METHODDEFORREF_METHODREF:
3570 return mono_get_method (m, MONO_TOKEN_MEMBER_REF | idx, NULL);
3572 g_assert_not_reached ();
3577 mono_class_get_overrides (MonoImage *image, guint32 type_token, gint32 *num_overrides)
3580 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
3583 guint32 cols [MONO_METHODIMPL_SIZE];
3584 MonoMethod **result;
3593 loc.col_idx = MONO_METHODIMPL_CLASS;
3594 loc.idx = mono_metadata_token_index (type_token);
3596 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3602 * We may end up in the middle of the rows...
3605 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
3610 while (end < tdef->rows) {
3611 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
3617 result = g_new (MonoMethod*, num * 2);
3618 for (i = 0; i < num; ++i) {
3619 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
3620 result [i * 2] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_DECLARATION]);
3621 result [i * 2 + 1] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_BODY]);
3625 *num_overrides = num;
3630 * mono_guid_to_string:
3632 * Converts a 16 byte Microsoft GUID to the standard string representation.
3635 mono_guid_to_string (const guint8 *guid)
3637 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
3638 guid[3], guid[2], guid[1], guid[0],
3642 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
3646 get_constraints (MonoImage *image, int owner, MonoGenericContext *context)
3648 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
3649 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
3650 guint32 i, token, found;
3651 MonoClass *klass, **res;
3652 GList *cons = NULL, *tmp;
3656 for (i = 0; i < tdef->rows; ++i) {
3657 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
3658 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
3659 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
3660 klass = mono_class_get_full (image, token, context);
3661 cons = g_list_append (cons, klass);
3664 /* contiguous list finished */
3671 res = g_new0 (MonoClass*, found + 1);
3672 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
3673 res [i] = tmp->data;
3679 MonoGenericContainer *
3680 mono_metadata_load_generic_params (MonoImage *image, guint32 token)
3682 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
3683 guint32 cols [MONO_GENERICPARAM_SIZE];
3684 guint32 i, owner = 0, last_num, n;
3685 MonoGenericContainer *container;
3686 MonoGenericParam *params;
3688 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
3689 owner = MONO_TYPEORMETHOD_TYPE;
3690 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3691 owner = MONO_TYPEORMETHOD_METHOD;
3693 g_error ("wrong token %x to load_generics_params", token);
3696 owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
3700 for (i = 0; i < tdef->rows; ++i) {
3701 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3702 if (cols [MONO_GENERICPARAM_OWNER] == owner)
3706 if (i >= tdef->rows)
3710 container = g_new0 (MonoGenericContainer, 1);
3713 params = g_realloc (params, sizeof (MonoGenericParam) * n);
3714 params [n - 1].owner = container;
3715 params [n - 1].pklass = NULL;
3716 params [n - 1].method = NULL;
3717 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
3718 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
3719 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
3720 if (++i >= tdef->rows)
3722 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3723 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
3725 container->type_argc = n;
3726 container->type_params = params;
3728 container->context = g_new0 (MonoGenericContext, 1);
3729 container->context->container = container;
3731 for (i = 0; i < n; i++)
3732 params [i].constraints = get_constraints (image, i + 1, container->context);
3738 mono_type_is_byref (MonoType *type)
3744 mono_type_get_type (MonoType *type)
3749 /* For MONO_TYPE_FNPTR */
3750 MonoMethodSignature*
3751 mono_type_get_signature (MonoType *type)
3753 return type->data.method;
3756 /* For MONO_TYPE_CLASS, VALUETYPE */
3758 mono_type_get_class (MonoType *type)
3760 return type->data.klass;
3763 /* For MONO_TYPE_ARRAY */
3765 mono_type_get_array_type (MonoType *type)
3767 return type->data.array;
3771 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
3773 /* FIXME: implement */
3778 mono_signature_get_return_type (MonoMethodSignature *sig)
3784 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
3790 /* start from the first */
3791 if (sig->param_count) {
3792 *iter = &sig->params [0];
3793 return sig->params [0];
3801 if (type < &sig->params [sig->param_count]) {
3809 mono_signature_get_param_count (MonoMethodSignature *sig)
3811 return sig->param_count;
3815 mono_signature_get_call_conv (MonoMethodSignature *sig)
3817 return sig->call_convention;
3821 mono_signature_vararg_start (MonoMethodSignature *sig)
3823 return sig->sentinelpos;
3827 mono_signature_is_instance (MonoMethodSignature *sig)
3829 return sig->hasthis;
3833 mono_signature_explicit_this (MonoMethodSignature *sig)
3835 return sig->explicit_this;