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, MonoGenericContext *generic_context,
27 const char *ptr, const char **rptr);
29 static gboolean do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only);
30 static gboolean mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only);
31 static gboolean _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2,
32 gboolean signature_only);
35 * Encoding of the "description" argument:
37 * identifier [CODE ARG]
39 * If CODE is ':', then a lookup on table ARG is performed
40 * If CODE is '=', then a lookup in the aliased-table ARG is performed
41 * If CODE is '#', then this encodes a flag, ARG is the flag name.
43 * Aliased table for example is `CustomAttributeType' which depending on the
44 * information might refer to different tables.
47 const static MonoMetaTable AssemblySchema [] = {
48 { MONO_MT_UINT32, "HashId" },
49 { MONO_MT_UINT16, "Major" },
50 { MONO_MT_UINT16, "Minor" },
51 { MONO_MT_UINT16, "BuildNumber" },
52 { MONO_MT_UINT16, "RevisionNumber" },
53 { MONO_MT_UINT32, "Flags" },
54 { MONO_MT_BLOB_IDX, "PublicKey" },
55 { MONO_MT_STRING_IDX, "Name" },
56 { MONO_MT_STRING_IDX, "Culture" },
60 const static MonoMetaTable AssemblyOSSchema [] = {
61 { MONO_MT_UINT32, "OSPlatformID" },
62 { MONO_MT_UINT32, "OSMajor" },
63 { MONO_MT_UINT32, "OSMinor" },
67 const static MonoMetaTable AssemblyProcessorSchema [] = {
68 { MONO_MT_UINT32, "Processor" },
72 const static MonoMetaTable AssemblyRefSchema [] = {
73 { MONO_MT_UINT16, "Major" },
74 { MONO_MT_UINT16, "Minor" },
75 { MONO_MT_UINT16, "Build" },
76 { MONO_MT_UINT16, "Revision" },
77 { MONO_MT_UINT32, "Flags" },
78 { MONO_MT_BLOB_IDX, "PublicKeyOrToken" },
79 { MONO_MT_STRING_IDX, "Name" },
80 { MONO_MT_STRING_IDX, "Culture" },
81 { MONO_MT_BLOB_IDX, "HashValue" },
85 const static MonoMetaTable AssemblyRefOSSchema [] = {
86 { MONO_MT_UINT32, "OSPlatformID" },
87 { MONO_MT_UINT32, "OSMajorVersion" },
88 { MONO_MT_UINT32, "OSMinorVersion" },
89 { MONO_MT_TABLE_IDX, "AssemblyRef:AssemblyRef" },
93 const static MonoMetaTable AssemblyRefProcessorSchema [] = {
94 { MONO_MT_UINT32, "Processor" },
95 { MONO_MT_TABLE_IDX, "AssemblyRef:AssemblyRef" },
99 const static MonoMetaTable ClassLayoutSchema [] = {
100 { MONO_MT_UINT16, "PackingSize" },
101 { MONO_MT_UINT32, "ClassSize" },
102 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
103 { MONO_MT_END, NULL }
106 const static MonoMetaTable ConstantSchema [] = {
107 { MONO_MT_UINT8, "Type" },
108 { MONO_MT_UINT8, "PaddingZero" },
109 { MONO_MT_CONST_IDX, "Parent" },
110 { MONO_MT_BLOB_IDX, "Value" },
111 { MONO_MT_END, NULL }
114 const static MonoMetaTable CustomAttributeSchema [] = {
115 { MONO_MT_HASCAT_IDX, "Parent" },
116 { MONO_MT_CAT_IDX, "Type" },
117 { MONO_MT_BLOB_IDX, "Value" },
118 { MONO_MT_END, NULL }
121 const static MonoMetaTable DeclSecuritySchema [] = {
122 { MONO_MT_UINT16, "Action" },
123 { MONO_MT_HASDEC_IDX, "Parent" },
124 { MONO_MT_BLOB_IDX, "PermissionSet" },
125 { MONO_MT_END, NULL }
128 const static MonoMetaTable EventMapSchema [] = {
129 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
130 { MONO_MT_TABLE_IDX, "EventList:Event" },
131 { MONO_MT_END, NULL }
134 const static MonoMetaTable EventSchema [] = {
135 { MONO_MT_UINT16, "EventFlags#EventAttribute" },
136 { MONO_MT_STRING_IDX, "Name" },
137 { MONO_MT_TABLE_IDX, "EventType" }, /* TypeDef or TypeRef */
138 { MONO_MT_END, NULL }
141 const static MonoMetaTable ExportedTypeSchema [] = {
142 { MONO_MT_UINT32, "Flags" },
143 { MONO_MT_TABLE_IDX, "TypeDefId" },
144 { MONO_MT_STRING_IDX, "TypeName" },
145 { MONO_MT_STRING_IDX, "TypeNameSpace" },
146 { MONO_MT_IMPL_IDX, "Implementation" },
147 { MONO_MT_END, NULL }
150 const static MonoMetaTable FieldSchema [] = {
151 { MONO_MT_UINT16, "Flags" },
152 { MONO_MT_STRING_IDX, "Name" },
153 { MONO_MT_BLOB_IDX, "Signature" },
154 { MONO_MT_END, NULL }
156 const static MonoMetaTable FieldLayoutSchema [] = {
157 { MONO_MT_UINT32, "Offset" },
158 { MONO_MT_TABLE_IDX, "Field:Field" },
159 { MONO_MT_END, NULL }
162 const static MonoMetaTable FieldMarshalSchema [] = {
163 { MONO_MT_HFM_IDX, "Parent" },
164 { MONO_MT_BLOB_IDX, "NativeType" },
165 { MONO_MT_END, NULL }
167 const static MonoMetaTable FieldRVASchema [] = {
168 { MONO_MT_UINT32, "RVA" },
169 { MONO_MT_TABLE_IDX, "Field:Field" },
170 { MONO_MT_END, NULL }
173 const static MonoMetaTable FileSchema [] = {
174 { MONO_MT_UINT32, "Flags" },
175 { MONO_MT_STRING_IDX, "Name" },
176 { MONO_MT_BLOB_IDX, "Value" },
177 { MONO_MT_END, NULL }
180 const static MonoMetaTable ImplMapSchema [] = {
181 { MONO_MT_UINT16, "MappingFlag" },
182 { MONO_MT_MF_IDX, "MemberForwarded" },
183 { MONO_MT_STRING_IDX, "ImportName" },
184 { MONO_MT_TABLE_IDX, "ImportScope:ModuleRef" },
185 { MONO_MT_END, NULL }
188 const static MonoMetaTable InterfaceImplSchema [] = {
189 { MONO_MT_TABLE_IDX, "Class:TypeDef" },
190 { MONO_MT_TDOR_IDX, "Interface=TypeDefOrRef" },
191 { MONO_MT_END, NULL }
194 const static MonoMetaTable ManifestResourceSchema [] = {
195 { MONO_MT_UINT32, "Offset" },
196 { MONO_MT_UINT32, "Flags" },
197 { MONO_MT_STRING_IDX, "Name" },
198 { MONO_MT_IMPL_IDX, "Implementation" },
199 { MONO_MT_END, NULL }
202 const static MonoMetaTable MemberRefSchema [] = {
203 { MONO_MT_MRP_IDX, "Class" },
204 { MONO_MT_STRING_IDX, "Name" },
205 { MONO_MT_BLOB_IDX, "Signature" },
206 { MONO_MT_END, NULL }
209 const static MonoMetaTable MethodSchema [] = {
210 { MONO_MT_UINT32, "RVA" },
211 { MONO_MT_UINT16, "ImplFlags#MethodImplAttributes" },
212 { MONO_MT_UINT16, "Flags#MethodAttribute" },
213 { MONO_MT_STRING_IDX, "Name" },
214 { MONO_MT_BLOB_IDX, "Signature" },
215 { MONO_MT_TABLE_IDX, "ParamList:Param" },
216 { MONO_MT_END, NULL }
219 const static MonoMetaTable MethodImplSchema [] = {
220 { MONO_MT_TABLE_IDX, "Class:TypeDef" },
221 { MONO_MT_MDOR_IDX, "MethodBody" },
222 { MONO_MT_MDOR_IDX, "MethodDeclaration" },
223 { MONO_MT_END, NULL }
226 const static MonoMetaTable MethodSemanticsSchema [] = {
227 { MONO_MT_UINT16, "MethodSemantic" },
228 { MONO_MT_TABLE_IDX, "Method:Method" },
229 { MONO_MT_HS_IDX, "Association" },
230 { MONO_MT_END, NULL }
233 const static MonoMetaTable ModuleSchema [] = {
234 { MONO_MT_UINT16, "Generation" },
235 { MONO_MT_STRING_IDX, "Name" },
236 { MONO_MT_GUID_IDX, "MVID" },
237 { MONO_MT_GUID_IDX, "EncID" },
238 { MONO_MT_GUID_IDX, "EncBaseID" },
239 { MONO_MT_END, NULL }
242 const static MonoMetaTable ModuleRefSchema [] = {
243 { MONO_MT_STRING_IDX, "Name" },
244 { MONO_MT_END, NULL }
247 const static MonoMetaTable NestedClassSchema [] = {
248 { MONO_MT_TABLE_IDX, "NestedClass:TypeDef" },
249 { MONO_MT_TABLE_IDX, "EnclosingClass:TypeDef" },
250 { MONO_MT_END, NULL }
253 const static MonoMetaTable ParamSchema [] = {
254 { MONO_MT_UINT16, "Flags" },
255 { MONO_MT_UINT16, "Sequence" },
256 { MONO_MT_STRING_IDX, "Name" },
257 { MONO_MT_END, NULL }
260 const static MonoMetaTable PropertySchema [] = {
261 { MONO_MT_UINT16, "Flags" },
262 { MONO_MT_STRING_IDX, "Name" },
263 { MONO_MT_BLOB_IDX, "Type" },
264 { MONO_MT_END, NULL }
267 const static MonoMetaTable PropertyMapSchema [] = {
268 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
269 { MONO_MT_TABLE_IDX, "PropertyList:Property" },
270 { MONO_MT_END, NULL }
273 const static MonoMetaTable StandaloneSigSchema [] = {
274 { MONO_MT_BLOB_IDX, "Signature" },
275 { MONO_MT_END, NULL }
278 const static MonoMetaTable TypeDefSchema [] = {
279 { MONO_MT_UINT32, "Flags" },
280 { MONO_MT_STRING_IDX, "Name" },
281 { MONO_MT_STRING_IDX, "Namespace" },
282 { MONO_MT_TDOR_IDX, "Extends" },
283 { MONO_MT_TABLE_IDX, "FieldList:Field" },
284 { MONO_MT_TABLE_IDX, "MethodList:Method" },
285 { MONO_MT_END, NULL }
288 const static MonoMetaTable TypeRefSchema [] = {
289 { MONO_MT_RS_IDX, "ResolutionScope=ResolutionScope" },
290 { MONO_MT_STRING_IDX, "Name" },
291 { MONO_MT_STRING_IDX, "Namespace" },
292 { MONO_MT_END, NULL }
295 const static MonoMetaTable TypeSpecSchema [] = {
296 { MONO_MT_BLOB_IDX, "Signature" },
297 { MONO_MT_END, NULL }
300 const static MonoMetaTable GenericParamSchema [] = {
301 { MONO_MT_UINT16, "Number" },
302 { MONO_MT_UINT16, "Flags" },
303 { MONO_MT_TABLE_IDX, "Owner" }, /* TypeDef or MethodDef */
304 { MONO_MT_STRING_IDX, "Name" },
306 /* soon to be removed */
307 { MONO_MT_TABLE_IDX, "Kind" },
309 { MONO_MT_END, NULL }
312 const static MonoMetaTable MethodSpecSchema [] = {
313 { MONO_MT_MDOR_IDX, "Method" },
314 { MONO_MT_BLOB_IDX, "Signature" },
315 { MONO_MT_END, NULL }
318 const static MonoMetaTable GenericParamConstraintSchema [] = {
319 { MONO_MT_TABLE_IDX, "GenericParam" },
320 { MONO_MT_TDOR_IDX, "Constraint" },
321 { MONO_MT_END, NULL }
324 const static struct {
325 const MonoMetaTable *table;
328 /* 0 */ { ModuleSchema, "Module" },
329 /* 1 */ { TypeRefSchema, "TypeRef" },
330 /* 2 */ { TypeDefSchema, "TypeDef" },
331 /* 3 */ { NULL, NULL },
332 /* 4 */ { FieldSchema, "Field" },
333 /* 5 */ { NULL, NULL },
334 /* 6 */ { MethodSchema, "Method" },
335 /* 7 */ { NULL, NULL },
336 /* 8 */ { ParamSchema, "Param" },
337 /* 9 */ { InterfaceImplSchema, "InterfaceImpl" },
338 /* A */ { MemberRefSchema, "MemberRef" },
339 /* B */ { ConstantSchema, "Constant" },
340 /* C */ { CustomAttributeSchema, "CustomAttribute" },
341 /* D */ { FieldMarshalSchema, "FieldMarshal" },
342 /* E */ { DeclSecuritySchema, "DeclSecurity" },
343 /* F */ { ClassLayoutSchema, "ClassLayout" },
344 /* 10 */ { FieldLayoutSchema, "FieldLayout" },
345 /* 11 */ { StandaloneSigSchema, "StandaloneSig" },
346 /* 12 */ { EventMapSchema, "EventMap" },
347 /* 13 */ { NULL, NULL },
348 /* 14 */ { EventSchema, "Event" },
349 /* 15 */ { PropertyMapSchema, "PropertyMap" },
350 /* 16 */ { NULL, NULL },
351 /* 17 */ { PropertySchema, "PropertyTable" },
352 /* 18 */ { MethodSemanticsSchema, "MethodSemantics" },
353 /* 19 */ { MethodImplSchema, "MethodImpl" },
354 /* 1A */ { ModuleRefSchema, "ModuleRef" },
355 /* 1B */ { TypeSpecSchema, "TypeSpec" },
356 /* 1C */ { ImplMapSchema, "ImplMap" },
357 /* 1D */ { FieldRVASchema, "FieldRVA" },
358 /* 1E */ { NULL, NULL },
359 /* 1F */ { NULL, NULL },
360 /* 20 */ { AssemblySchema, "Assembly" },
361 /* 21 */ { AssemblyProcessorSchema, "AssemblyProcessor" },
362 /* 22 */ { AssemblyOSSchema, "AssemblyOS" },
363 /* 23 */ { AssemblyRefSchema, "AssemblyRef" },
364 /* 24 */ { AssemblyRefProcessorSchema, "AssemblyRefProcessor" },
365 /* 25 */ { AssemblyRefOSSchema, "AssemblyRefOS" },
366 /* 26 */ { FileSchema, "File" },
367 /* 27 */ { ExportedTypeSchema, "ExportedType" },
368 /* 28 */ { ManifestResourceSchema, "ManifestResource" },
369 /* 29 */ { NestedClassSchema, "NestedClass" },
370 /* 2A */ { GenericParamSchema, "GenericParam" },
371 /* 2B */ { MethodSpecSchema, "MethodSpec" },
372 /* 2C */ { GenericParamConstraintSchema, "GenericParamConstraint" },
376 * mono_meta_table_name:
377 * @table: table index
379 * Returns: the name for the @table index
382 mono_meta_table_name (int table)
384 if ((table < 0) || (table > 0x2c))
387 return tables [table].name;
390 /* The guy who wrote the spec for this should not be allowed near a
393 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
394 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
395 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
396 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
397 inverse of this mapping.
400 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
401 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
403 /* Reference: Partition II - 23.2.6 */
405 * mono_metadata_compute_size:
406 * @meta: metadata context
407 * @tableindex: metadata table number
408 * @result_bitfield: pointer to guint32 where to store additional info
410 * mono_metadata_compute_size() computes the lenght in bytes of a single
411 * row in a metadata table. The size of each column is encoded in the
412 * @result_bitfield return value along with the number of columns in the table.
413 * the resulting bitfield should be handed to the mono_metadata_table_size()
414 * and mono_metadata_table_count() macros.
417 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
419 guint32 bitfield = 0;
420 int size = 0, field_size = 0;
423 const MonoMetaTable *table = tables [tableindex].table;
425 for (i = 0; (code = table [i].code) != MONO_MT_END; i++){
428 field_size = 4; break;
431 field_size = 2; break;
434 field_size = 1; break;
436 case MONO_MT_BLOB_IDX:
437 field_size = meta->idx_blob_wide ? 4 : 2; break;
439 case MONO_MT_STRING_IDX:
440 field_size = meta->idx_string_wide ? 4 : 2; break;
442 case MONO_MT_GUID_IDX:
443 field_size = meta->idx_guid_wide ? 4 : 2; break;
445 case MONO_MT_TABLE_IDX:
446 /* Uhm, a table index can point to other tables besides the current one
447 * so, it's not correct to use the rowcount of the current table to
448 * get the size for this column - lupus
450 switch (tableindex) {
451 case MONO_TABLE_ASSEMBLYREFOS:
453 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
454 case MONO_TABLE_ASSEMBLYPROCESSOR:
456 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
457 case MONO_TABLE_CLASSLAYOUT:
459 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
460 case MONO_TABLE_EVENTMAP:
461 g_assert (i == 0 || i == 1);
462 field_size = i ? idx_size (MONO_TABLE_EVENT):
463 idx_size(MONO_TABLE_TYPEDEF);
465 case MONO_TABLE_EVENT:
467 field_size = MAX (idx_size (MONO_TABLE_TYPEDEF), idx_size(MONO_TABLE_TYPEREF));
468 field_size = MAX (field_size, idx_size(MONO_TABLE_TYPESPEC));
470 case MONO_TABLE_EXPORTEDTYPE:
472 /* the index is in another metadata file, so it must be 4 */
473 field_size = 4; break;
474 case MONO_TABLE_FIELDLAYOUT:
476 field_size = idx_size (MONO_TABLE_FIELD); break;
477 case MONO_TABLE_FIELDRVA:
479 field_size = idx_size (MONO_TABLE_FIELD); break;
480 case MONO_TABLE_IMPLMAP:
482 field_size = idx_size (MONO_TABLE_MODULEREF); break;
483 case MONO_TABLE_INTERFACEIMPL:
485 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
486 case MONO_TABLE_METHOD:
488 field_size = idx_size (MONO_TABLE_PARAM); break;
489 case MONO_TABLE_METHODIMPL:
491 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
492 case MONO_TABLE_METHODSEMANTICS:
494 field_size = idx_size (MONO_TABLE_METHOD); break;
495 case MONO_TABLE_NESTEDCLASS:
496 g_assert (i == 0 || i == 1);
497 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
498 case MONO_TABLE_PROPERTYMAP:
499 g_assert (i == 0 || i == 1);
500 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
501 idx_size(MONO_TABLE_TYPEDEF);
503 case MONO_TABLE_TYPEDEF:
504 g_assert (i == 4 || i == 5);
505 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
506 idx_size(MONO_TABLE_METHOD);
507 case MONO_TABLE_GENERICPARAM:
508 g_assert (i == 2 || i == 4 || i == 5);
510 field_size = MAX (idx_size (MONO_TABLE_METHOD), idx_size (MONO_TABLE_TYPEDEF));
512 field_size = idx_size (MONO_TABLE_TYPEDEF);
514 field_size = idx_size (MONO_TABLE_TYPEDEF);
517 case MONO_TABLE_GENERICPARAMCONSTRAINT:
519 field_size = idx_size (MONO_TABLE_GENERICPARAM);
523 g_assert_not_reached ();
525 if (tableindex != MONO_TABLE_EXPORTEDTYPE && field_size != idx_size (tableindex))
526 g_warning ("size changed (%d to %d)", idx_size (tableindex), field_size);
531 * HasConstant: ParamDef, FieldDef, Property
533 case MONO_MT_CONST_IDX:
534 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
535 meta->tables [MONO_TABLE_FIELD].rows);
536 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
538 /* 2 bits to encode tag */
539 field_size = rtsize (n, 16-2);
543 * HasCustomAttribute: points to any table but
546 case MONO_MT_HASCAT_IDX:
548 * We believe that since the signature and
549 * permission are indexing the Blob heap,
550 * we should consider the blob size first
552 /* I'm not a believer - lupus
553 if (meta->idx_blob_wide){
558 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
559 meta->tables [MONO_TABLE_FIELD].rows);
560 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
561 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
562 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
563 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
564 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
565 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
566 n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
567 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
568 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
569 n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
570 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
571 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
572 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
573 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
574 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
575 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
576 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
578 /* 5 bits to encode */
579 field_size = rtsize (n, 16-5);
583 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
584 * MemberRef and String.
586 case MONO_MT_CAT_IDX:
587 /* String is a heap, if it is wide, we know the size */
589 if (meta->idx_string_wide){
594 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
595 meta->tables [MONO_TABLE_TYPEDEF].rows);
596 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
597 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
599 /* 3 bits to encode */
600 field_size = rtsize (n, 16-3);
604 * HasDeclSecurity: Typedef, MethodDef, Assembly
606 case MONO_MT_HASDEC_IDX:
607 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
608 meta->tables [MONO_TABLE_METHOD].rows);
609 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
611 /* 2 bits to encode */
612 field_size = rtsize (n, 16-2);
616 * Implementation: File, AssemblyRef, ExportedType
618 case MONO_MT_IMPL_IDX:
619 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
620 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
621 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
623 /* 2 bits to encode tag */
624 field_size = rtsize (n, 16-2);
628 * HasFieldMarshall: FieldDef, ParamDef
630 case MONO_MT_HFM_IDX:
631 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
632 meta->tables [MONO_TABLE_PARAM].rows);
634 /* 1 bit used to encode tag */
635 field_size = rtsize (n, 16-1);
639 * MemberForwarded: FieldDef, MethodDef
642 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
643 meta->tables [MONO_TABLE_METHOD].rows);
645 /* 1 bit used to encode tag */
646 field_size = rtsize (n, 16-1);
650 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
652 * It is TypeDef, _TypeRef_, TypeSpec, instead.
654 case MONO_MT_TDOR_IDX:
655 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
656 meta->tables [MONO_TABLE_TYPEREF].rows);
657 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
659 /* 2 bits to encode */
660 field_size = rtsize (n, 16-2);
664 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
666 case MONO_MT_MRP_IDX:
667 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
668 meta->tables [MONO_TABLE_TYPEREF].rows);
669 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
670 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
671 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
672 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
674 /* 3 bits to encode */
675 field_size = rtsize (n, 16 - 3);
678 case MONO_MT_MDOR_IDX:
681 * MethodDefOrRef: MethodDef, MemberRef
684 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
685 meta->tables [MONO_TABLE_MEMBERREF].rows);
687 /* 1 bit used to encode tag */
688 field_size = rtsize (n, 16-1);
692 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
695 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
696 meta->tables [MONO_TABLE_MODULEREF].rows);
697 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
698 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
700 /* 2 bits used to encode tag (ECMA spec claims 3) */
701 field_size = rtsize (n, 16 - 2);
706 * encode field size as follows (we just need to
713 bitfield |= (field_size-1) << shift;
716 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
719 *result_bitfield = (i << 24) | bitfield;
724 * mono_metadata_compute_table_bases:
725 * @meta: metadata context to compute table values
727 * Computes the table bases for the metadata structure.
728 * This is an internal function used by the image loader code.
731 mono_metadata_compute_table_bases (MonoImage *meta)
734 const char *base = meta->tables_base;
736 for (i = 0; i < 64; i++){
737 if (meta->tables [i].rows == 0)
740 meta->tables [i].row_size = mono_metadata_compute_size (
741 meta, i, &meta->tables [i].size_bitfield);
742 meta->tables [i].base = base;
743 base += meta->tables [i].rows * meta->tables [i].row_size;
748 * mono_metadata_locate:
749 * @meta: metadata context
750 * @table: table code.
751 * @idx: index of element to retrieve from @table.
753 * Returns: a pointer to the @idx element in the metadata table
754 * whose code is @table.
757 mono_metadata_locate (MonoImage *meta, int table, int idx)
759 /* idx == 0 refers always to NULL */
760 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
762 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
766 * mono_metadata_locate_token:
767 * @meta: metadata context
768 * @token: metadata token
770 * Returns: a pointer to the data in the metadata represented by the
774 mono_metadata_locate_token (MonoImage *meta, guint32 token)
776 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
780 * mono_metadata_get_table:
781 * @table: table to retrieve
783 * Returns: the MonoMetaTable structure for table @table
785 const MonoMetaTable *
786 mono_metadata_get_table (MonoMetaTableEnum table)
790 g_return_val_if_fail ((x > 0) && (x <= MONO_TABLE_LAST), NULL);
792 return tables [table].table;
796 * mono_metadata_string_heap:
797 * @meta: metadata context
798 * @index: index into the string heap.
800 * Returns: an in-memory pointer to the @index in the string heap.
803 mono_metadata_string_heap (MonoImage *meta, guint32 index)
805 g_return_val_if_fail (index < meta->heap_strings.size, "");
806 return meta->heap_strings.data + index;
810 * mono_metadata_user_string:
811 * @meta: metadata context
812 * @index: index into the user string heap.
814 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
817 mono_metadata_user_string (MonoImage *meta, guint32 index)
819 g_return_val_if_fail (index < meta->heap_us.size, "");
820 return meta->heap_us.data + index;
824 * mono_metadata_blob_heap:
825 * @meta: metadata context
826 * @index: index into the blob.
828 * Returns: an in-memory pointer to the @index in the Blob heap.
831 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
833 g_return_val_if_fail (index < meta->heap_blob.size, "");
834 return meta->heap_blob.data + index;
838 * mono_metadata_guid_heap:
839 * @meta: metadata context
840 * @index: index into the guid heap.
842 * Returns: an in-memory pointer to the @index in the guid heap.
845 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
848 index *= 16; /* adjust for guid size and 1-based index */
849 g_return_val_if_fail (index < meta->heap_guid.size, "");
850 return meta->heap_guid.data + index;
854 dword_align (const char *ptr)
856 #if SIZEOF_VOID_P == 8
857 return (const char *) (((guint64) (ptr + 3)) & ~3);
859 return (const char *) (((guint32) (ptr + 3)) & ~3);
864 * mono_metadata_decode_row:
865 * @t: table to extract information from.
866 * @idx: index in table.
867 * @res: array of @res_size cols to store the results in
869 * This decompresses the metadata element @idx in table @t
870 * into the guint32 @res array that has res_size elements
873 mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res_size)
875 guint32 bitfield = t->size_bitfield;
876 int i, count = mono_metadata_table_count (bitfield);
877 const char *data = t->base + idx * t->row_size;
879 g_assert (res_size == count);
881 for (i = 0; i < count; i++){
882 int n = mono_metadata_table_size (bitfield, i);
886 res [i] = *data; break;
888 res [i] = read16 (data); break;
891 res [i] = read32 (data); break;
894 g_assert_not_reached ();
901 * mono_metadata_decode_row_col:
902 * @t: table to extract information from.
903 * @idx: index for row in table.
904 * @col: column in the row.
906 * This function returns the value of column @col from the @idx
907 * row in the table @t.
910 mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col)
912 guint32 bitfield = t->size_bitfield;
914 register const char *data = t->base + idx * t->row_size;
917 g_assert (col < mono_metadata_table_count (bitfield));
919 n = mono_metadata_table_size (bitfield, 0);
920 for (i = 0; i < col; ++i) {
922 n = mono_metadata_table_size (bitfield, i + 1);
928 return read16 (data);
930 return read32 (data);
932 g_assert_not_reached ();
937 * mono_metadata_decode_blob_size:
938 * @ptr: pointer to a blob object
939 * @rptr: the new position of the pointer
941 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
943 * Returns: the size of the blob object
946 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
948 const unsigned char *ptr = (const unsigned char *)xptr;
951 if ((*ptr & 0x80) == 0){
952 size = ptr [0] & 0x7f;
954 } else if ((*ptr & 0x40) == 0){
955 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
958 size = ((ptr [0] & 0x1f) << 24) +
971 * mono_metadata_decode_value:
972 * @ptr: pointer to decode from
973 * @rptr: the new position of the pointer
975 * This routine decompresses 32-bit values as specified in the "Blob and
976 * Signature" section (22.2)
978 * Returns: the decoded value
981 mono_metadata_decode_value (const char *_ptr, const char **rptr)
983 const unsigned char *ptr = (const unsigned char *) _ptr;
984 unsigned char b = *ptr;
987 if ((b & 0x80) == 0){
990 } else if ((b & 0x40) == 0){
991 len = ((b & 0x3f) << 8 | ptr [1]);
994 len = ((b & 0x1f) << 24) |
1007 * mono_metadata_parse_typedef_or_ref:
1008 * @m: a metadata context.
1009 * @ptr: a pointer to an encoded TypedefOrRef in @m
1010 * @rptr: pointer updated to match the end of the decoded stream
1012 * Returns: a token valid in the @m metadata decoded from
1013 * the compressed representation.
1016 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1019 token = mono_metadata_decode_value (ptr, &ptr);
1022 return mono_metadata_token_from_dor (token);
1026 * mono_metadata_parse_custom_mod:
1027 * @m: a metadata context.
1028 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1029 * @ptr: a pointer to (possibly) the start of a custom modifier list
1030 * @rptr: pointer updated to match the end of the decoded stream
1032 * Checks if @ptr points to a type custom modifier compressed representation.
1034 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1037 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1039 MonoCustomMod local;
1040 if ((*ptr == MONO_TYPE_CMOD_OPT) ||
1041 (*ptr == MONO_TYPE_CMOD_REQD)) {
1044 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1045 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1052 * mono_metadata_parse_array:
1053 * @m: a metadata context.
1054 * @ptr: a pointer to an encoded array description.
1055 * @rptr: pointer updated to match the end of the decoded stream
1057 * Decodes the compressed array description found in the metadata @m at @ptr.
1059 * Returns: a #MonoArrayType structure describing the array type
1063 mono_metadata_parse_array_full (MonoImage *m, MonoGenericContext *generic_context,
1064 const char *ptr, const char **rptr)
1067 MonoArrayType *array = g_new0 (MonoArrayType, 1);
1070 etype = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_TYPE, 0, ptr, &ptr);
1071 array->eklass = mono_class_from_mono_type (etype);
1072 array->rank = mono_metadata_decode_value (ptr, &ptr);
1074 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1075 if (array->numsizes)
1076 array->sizes = g_new0 (int, array->numsizes);
1077 for (i = 0; i < array->numsizes; ++i)
1078 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1080 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1081 if (array->numlobounds)
1082 array->lobounds = g_new0 (int, array->numlobounds);
1083 for (i = 0; i < array->numlobounds; ++i)
1084 array->lobounds [i] = mono_metadata_decode_value (ptr, &ptr);
1092 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1094 return mono_metadata_parse_array_full (m, NULL, ptr, rptr);
1098 * mono_metadata_free_array:
1099 * @array: array description
1101 * Frees the array description returned from mono_metadata_parse_array().
1104 mono_metadata_free_array (MonoArrayType *array)
1106 g_free (array->sizes);
1107 g_free (array->lobounds);
1112 * need to add common field and param attributes combinations:
1115 * public static literal
1118 * private static literal
1120 static const MonoType
1122 /* data, attrs, type, nmods, byref, pinned */
1123 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1124 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1125 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1126 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1127 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1128 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1129 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1130 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1131 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1132 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1133 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1134 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1135 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1136 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1137 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1138 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1139 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1140 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1141 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1142 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1143 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1144 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1145 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1146 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1147 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1148 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1149 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1150 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 0, 0},
1151 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 1, 0},
1152 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1153 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1154 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1155 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1156 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1159 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1161 static GHashTable *type_cache = NULL;
1162 static GHashTable *generic_inst_cache = NULL;
1163 static GHashTable *generic_class_cache = NULL;
1164 static int next_generic_inst_id = 0;
1167 * MonoTypes with modifies are never cached, so we never check or use that field.
1170 mono_type_hash (gconstpointer data)
1172 const MonoType *type = (const MonoType *) data;
1173 return type->type | (type->byref << 8) | (type->attrs << 9);
1177 mono_type_equal (gconstpointer ka, gconstpointer kb)
1179 const MonoType *a = (const MonoType *) ka;
1180 const MonoType *b = (const MonoType *) kb;
1182 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1184 /* need other checks */
1189 mono_generic_inst_hash (gconstpointer data)
1191 const MonoGenericInst *ginst = (const MonoGenericInst *) data;
1192 return ginst->type_argc | (ginst->is_open << 8);
1196 mono_generic_inst_equal (gconstpointer ka, gconstpointer kb)
1198 const MonoGenericInst *a = (const MonoGenericInst *) ka;
1199 const MonoGenericInst *b = (const MonoGenericInst *) kb;
1202 if ((a->is_open != b->is_open) || (a->type_argc != b->type_argc))
1204 for (i = 0; i < a->type_argc; ++i) {
1205 if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], FALSE))
1212 mono_generic_class_hash (gconstpointer data)
1214 const MonoGenericClass *gclass = (const MonoGenericClass *) data;
1215 return mono_metadata_type_hash (&gclass->container_class->byval_arg);
1219 mono_generic_class_equal (gconstpointer ka, gconstpointer kb)
1221 const MonoGenericClass *a = (const MonoGenericClass *) ka;
1222 const MonoGenericClass *b = (const MonoGenericClass *) kb;
1224 return _mono_metadata_generic_class_equal (a, b, FALSE);
1228 * mono_metadata_init:
1230 * Initialize the global variables of this module.
1233 mono_metadata_init (void)
1237 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1238 generic_inst_cache = g_hash_table_new (mono_generic_inst_hash, mono_generic_inst_equal);
1239 generic_class_cache = g_hash_table_new (mono_generic_class_hash, mono_generic_class_equal);
1241 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1242 g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
1246 * mono_metadata_parse_type:
1247 * @m: metadata context
1248 * @mode: king of type that may be found at @ptr
1249 * @opt_attrs: optional attributes to store in the returned type
1250 * @ptr: pointer to the type representation
1251 * @rptr: pointer updated to match the end of the decoded stream
1253 * Decode a compressed type description found at @ptr in @m.
1254 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1255 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1256 * This function can be used to decode type descriptions in method signatures,
1257 * field signatures, locals signatures etc.
1259 * To parse a generic type, `generic_container' points to the current class'es
1260 * (the `generic_container' field in the MonoClass) or the current generic method's
1261 * (the `generic_container' field in the MonoMethodNormal) generic container.
1262 * When we encounter any MONO_TYPE_VAR or MONO_TYPE_MVAR's, they're looked up in
1263 * this MonoGenericContainer.
1265 * Returns: a #MonoType structure representing the decoded type.
1268 mono_metadata_parse_type_full (MonoImage *m, MonoGenericContext *generic_context, MonoParseTypeMode mode,
1269 short opt_attrs, const char *ptr, const char **rptr)
1271 MonoType *type, *cached;
1273 gboolean byref = FALSE;
1274 gboolean pinned = FALSE;
1275 const char *tmp_ptr;
1280 * According to the spec, custom modifiers should come before the byref
1281 * flag, but the IL produced by ilasm from the following signature:
1282 * object modopt(...) &
1283 * starts with a byref flag, followed by the modifiers. (bug #49802)
1284 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1285 * it would be better to treat byref as real type constructor instead of
1287 * Also, pinned should come before anything else, but some MSV++ produced
1288 * assemblies violate this (#bug 61990).
1291 /* Count the modifiers first */
1296 case MONO_TYPE_PINNED:
1297 case MONO_TYPE_BYREF:
1300 case MONO_TYPE_CMOD_REQD:
1301 case MONO_TYPE_CMOD_OPT:
1303 mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1311 type = g_malloc0 (sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1312 type->num_mods = count;
1314 g_warning ("got more than 64 modifiers in type");
1318 memset (type, 0, sizeof (MonoType));
1321 /* Parse pinned, byref and custom modifiers */
1326 case MONO_TYPE_PINNED:
1330 case MONO_TYPE_BYREF:
1334 case MONO_TYPE_CMOD_REQD:
1335 case MONO_TYPE_CMOD_OPT:
1336 mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1344 type->attrs = opt_attrs;
1345 type->byref = byref;
1346 type->pinned = pinned ? 1 : 0;
1348 do_mono_metadata_parse_type (type, m, generic_context, ptr, &ptr);
1354 /* FIXME: remove the != MONO_PARSE_PARAM condition, this accounts for
1355 * almost 10k (about 2/3rds) of all MonoType's we create.
1357 if (mode != MONO_PARSE_PARAM && !type->num_mods) {
1358 /* no need to free type here, because it is on the stack */
1359 if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
1361 return &type->data.klass->this_arg;
1363 return &type->data.klass->byval_arg;
1365 /* No need to use locking since nobody is modifying the hash table */
1366 if ((cached = g_hash_table_lookup (type_cache, type)))
1370 /*printf ("%x%c %s\n", type->attrs, type->pinned ? 'p' : ' ', mono_type_full_name (type));*/
1373 type = g_memdup (&stype, sizeof (MonoType));
1378 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
1379 const char *ptr, const char **rptr)
1381 return mono_metadata_parse_type_full (m, NULL, mode, opt_attrs, ptr, rptr);
1385 * mono_metadata_parse_signature:
1386 * @image: metadata context
1387 * @toke: metadata token
1389 * Decode a method signature stored in the STANDALONESIG table
1391 * Returns: a MonoMethodSignature describing the signature.
1393 MonoMethodSignature *
1394 mono_metadata_parse_signature_full (MonoImage *image, MonoGenericContext *generic_context, guint32 token)
1396 MonoTableInfo *tables = image->tables;
1397 guint32 idx = mono_metadata_token_index (token);
1402 return mono_lookup_dynamic_token (image, token);
1404 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1406 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1408 ptr = mono_metadata_blob_heap (image, sig);
1409 mono_metadata_decode_blob_size (ptr, &ptr);
1411 return mono_metadata_parse_method_signature_full (image, generic_context, FALSE, ptr, NULL);
1414 MonoMethodSignature *
1415 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1417 return mono_metadata_parse_signature_full (image, NULL, token);
1420 MonoMethodSignature*
1421 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1423 MonoMethodSignature *sig;
1425 /* later we want to allocate signatures with mempools */
1426 sig = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1427 sig->param_count = nparams;
1428 sig->sentinelpos = -1;
1433 MonoMethodSignature*
1434 mono_metadata_signature_dup (MonoMethodSignature *sig)
1438 sigsize = sizeof (MonoMethodSignature) + sig->param_count * sizeof (MonoType *);
1439 return g_memdup (sig, sigsize);
1443 * mono_metadata_parse_method_signature:
1444 * @m: metadata context
1445 * @def: the MethodDef index or 0 for Ref signatures.
1446 * @ptr: pointer to the signature metadata representation
1447 * @rptr: pointer updated to match the end of the decoded stream
1449 * Decode a method signature stored at @ptr.
1451 * Returns: a MonoMethodSignature describing the signature.
1453 MonoMethodSignature *
1454 mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContext *generic_context,
1455 int def, const char *ptr, const char **rptr)
1457 MonoMethodSignature *method;
1458 int i, ret_attrs = 0, *pattrs = NULL;
1459 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1460 guint32 gen_param_count = 0;
1461 gboolean is_open = FALSE;
1462 MonoGenericContext *context = NULL;
1465 gen_param_count = 1;
1470 call_convention = *ptr & 0x0F;
1472 if (gen_param_count)
1473 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1474 param_count = mono_metadata_decode_value (ptr, &ptr);
1475 pattrs = g_new0 (int, param_count);
1478 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1479 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1480 guint32 cols [MONO_PARAM_SIZE];
1481 guint lastp, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1483 if (def < methodt->rows)
1484 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1486 lastp = paramt->rows + 1;
1487 for (i = param_index; i < lastp; ++i) {
1488 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1489 if (!cols [MONO_PARAM_SEQUENCE])
1490 ret_attrs = cols [MONO_PARAM_FLAGS];
1492 pattrs [cols [MONO_PARAM_SEQUENCE] - 1] = cols [MONO_PARAM_FLAGS];
1495 method = mono_metadata_signature_alloc (m, param_count);
1496 method->hasthis = hasthis;
1497 method->explicit_this = explicit_this;
1498 method->call_convention = call_convention;
1499 method->generic_param_count = gen_param_count;
1501 if (gen_param_count)
1502 method->has_type_parameters = 1;
1504 if (gen_param_count && (!generic_context || !generic_context->container->is_method)) {
1505 MonoGenericContainer *container = g_new0 (MonoGenericContainer, 1);
1507 if (generic_context)
1508 container->parent = generic_context->container;
1509 container->is_signature = 1;
1511 context = &container->context;
1512 container->context.container = container;
1514 container->type_argc = gen_param_count;
1515 container->type_params = g_new0 (MonoGenericParam, gen_param_count);
1517 for (i = 0; i < gen_param_count; i++) {
1518 container->type_params [i].owner = container;
1519 container->type_params [i].num = i;
1522 context = generic_context;
1524 if (call_convention != 0xa) {
1525 method->ret = mono_metadata_parse_type_full (m, context, MONO_PARSE_RET, ret_attrs, ptr, &ptr);
1526 is_open = mono_class_is_open_constructed_type (method->ret);
1529 if (method->param_count) {
1530 method->sentinelpos = -1;
1532 for (i = 0; i < method->param_count; ++i) {
1533 if (*ptr == MONO_TYPE_SENTINEL) {
1534 if (method->call_convention != MONO_CALL_VARARG || def)
1535 g_error ("found sentinel for methoddef or no vararg method");
1536 method->sentinelpos = i;
1539 method->params [i] = mono_metadata_parse_type_full (
1540 m, context, MONO_PARSE_PARAM, pattrs [i], ptr, &ptr);
1542 is_open = mono_class_is_open_constructed_type (method->params [i]);
1546 method->has_type_parameters = is_open;
1548 if (def && (method->call_convention == MONO_CALL_VARARG))
1549 method->sentinelpos = method->param_count;
1556 * Add signature to a cache and increase ref count...
1561 MonoMethodSignature *
1562 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1564 return mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr);
1568 * mono_metadata_free_method_signature:
1569 * @sig: signature to destroy
1571 * Free the memory allocated in the signature @sig.
1574 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1577 mono_metadata_free_type (sig->ret);
1578 for (i = 0; i < sig->param_count; ++i)
1579 mono_metadata_free_type (sig->params [i]);
1585 mono_metadata_lookup_generic_inst (MonoGenericInst *ginst)
1587 MonoGenericInst *cached;
1590 cached = g_hash_table_lookup (generic_inst_cache, ginst);
1592 for (i = 0; i < ginst->type_argc; i++)
1593 mono_metadata_free_type (ginst->type_argv [i]);
1594 g_free (ginst->type_argv);
1599 ginst->id = ++next_generic_inst_id;
1600 g_hash_table_insert (generic_inst_cache, ginst, ginst);
1606 mono_metadata_lookup_generic_class (MonoGenericClass *gclass)
1608 MonoGenericClass *cached;
1611 cached = g_hash_table_lookup (generic_class_cache, gclass);
1615 g_hash_table_insert (generic_class_cache, gclass, gclass);
1620 mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context)
1622 MonoGenericInst *nginst, *cached;
1625 nginst = g_new0 (MonoGenericInst, 1);
1626 nginst->type_argc = ginst->type_argc;
1627 nginst->type_argv = g_new0 (MonoType*, nginst->type_argc);
1629 for (i = 0; i < nginst->type_argc; i++) {
1630 MonoType *t = ginst->type_argv [i];
1632 nginst->type_argv [i] = mono_class_inflate_generic_type (t, context);
1634 if (!nginst->is_open)
1635 nginst->is_open = mono_class_is_open_constructed_type (nginst->type_argv [i]);
1638 return mono_metadata_lookup_generic_inst (nginst);
1642 mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContext *generic_context,
1643 int count, const char *ptr, const char **rptr)
1645 MonoGenericInst *ginst;
1648 ginst = g_new0 (MonoGenericInst, 1);
1649 ginst->type_argc = count;
1650 ginst->type_argv = g_new0 (MonoType*, count);
1652 for (i = 0; i < ginst->type_argc; i++) {
1653 MonoType *t = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_TYPE, 0, ptr, &ptr);
1655 ginst->type_argv [i] = t;
1656 if (!ginst->is_open)
1657 ginst->is_open = mono_class_is_open_constructed_type (t);
1663 return mono_metadata_lookup_generic_inst (ginst);
1667 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContext *generic_context,
1668 const char *ptr, const char **rptr)
1670 MonoGenericClass *gclass = g_new0 (MonoGenericClass, 1);
1671 MonoGenericContainer *container;
1672 MonoGenericClass *cached;
1673 MonoGenericInst *ginst;
1678 type->data.generic_class = gclass;
1680 gclass->context = g_new0 (MonoGenericContext, 1);
1681 gclass->context->gclass = gclass;
1683 gclass->klass = g_new0 (MonoClass, 1);
1685 gtype = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_TYPE, 0, ptr, &ptr);
1686 gclass->container_class = gklass = mono_class_from_mono_type (gtype);
1688 g_assert ((gclass->context->container = gklass->generic_container) != NULL);
1689 count = mono_metadata_decode_value (ptr, &ptr);
1692 * Create the klass before parsing the type arguments.
1693 * This is required to support "recursive" definitions.
1694 * See mcs/tests/gen-23.cs for an example.
1697 gclass->init_pending = TRUE;
1699 mono_class_create_generic (gclass);
1701 gclass->inst = mono_metadata_parse_generic_inst (m, generic_context, count, ptr, &ptr);
1703 mono_class_create_generic_2 (gclass);
1709 * We may be called multiple times on different metadata to create the same
1710 * instantiated type. This happens for instance if we're part of a method or
1711 * local variable signature.
1713 * It's important to return the same MonoGenericClass * for each particualar
1714 * instantiation of a generic type (ie "Stack<Int32>") to make static fields
1717 * According to the spec ($26.1.5), a static variable in a generic class
1718 * declaration is shared amongst all instances of the same closed constructed
1722 cached = g_hash_table_lookup (generic_class_cache, gclass);
1724 g_free (gclass->klass);
1727 type->data.generic_class = cached;
1730 g_hash_table_insert (generic_class_cache, gclass, gclass);
1732 mono_stats.generic_instance_count++;
1733 mono_stats.generics_metadata_size += sizeof (MonoGenericClass) +
1734 sizeof (MonoGenericContext) +
1735 gclass->inst->type_argc * sizeof (MonoType);
1738 gclass->init_pending = FALSE;
1742 * do_mono_metadata_parse_generic_param:
1743 * @generic_container: Our MonoClass's or MonoMethodNormal's MonoGenericContainer;
1744 * see mono_metadata_parse_type_full() for details.
1745 * Internal routine to parse a generic type parameter.
1747 static MonoGenericParam *
1748 mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContext *generic_context,
1749 gboolean is_mvar, const char *ptr, const char **rptr)
1751 MonoGenericContainer *generic_container;
1754 index = mono_metadata_decode_value (ptr, &ptr);
1758 g_assert (generic_context);
1759 if (generic_context->gmethod)
1760 generic_container = generic_context->gmethod->container;
1761 else if (generic_context->gclass)
1762 generic_container = generic_context->gclass->container_class->generic_container;
1764 generic_container = generic_context->container;
1767 g_assert (generic_container);
1768 if (generic_container->parent) {
1770 * The current MonoGenericContainer is a generic method -> its `parent'
1771 * points to the containing class'es container.
1773 generic_container = generic_container->parent;
1775 g_assert (generic_container && !generic_container->is_method);
1776 g_assert (index < generic_container->type_argc);
1778 return &generic_container->type_params [index];
1780 g_assert (generic_container && (generic_container->is_method || generic_container->is_signature));
1781 g_assert (index < generic_container->type_argc);
1783 return &generic_container->type_params [index];
1788 * do_mono_metadata_parse_type:
1789 * @type: MonoType to be filled in with the return value
1791 * Internal routine used to "fill" the contents of @type from an
1792 * allocated pointer. This is done this way to avoid doing too
1793 * many mini-allocations (particularly for the MonoFieldType which
1794 * most of the time is just a MonoType, but sometimes might be augmented).
1796 * This routine is used by mono_metadata_parse_type and
1797 * mono_metadata_parse_field_type
1799 * This extracts a Type as specified in Partition II (22.2.12)
1802 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContext *generic_context,
1803 const char *ptr, const char **rptr)
1805 type->type = mono_metadata_decode_value (ptr, &ptr);
1807 switch (type->type){
1808 case MONO_TYPE_VOID:
1809 case MONO_TYPE_BOOLEAN:
1810 case MONO_TYPE_CHAR:
1823 case MONO_TYPE_STRING:
1824 case MONO_TYPE_OBJECT:
1825 case MONO_TYPE_TYPEDBYREF:
1827 case MONO_TYPE_VALUETYPE:
1828 case MONO_TYPE_CLASS: {
1830 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
1831 type->data.klass = mono_class_get (m, token);
1834 case MONO_TYPE_SZARRAY: {
1835 MonoType *etype = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1836 type->data.klass = mono_class_from_mono_type (etype);
1837 mono_metadata_free_type (etype);
1841 type->data.type = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1843 case MONO_TYPE_FNPTR:
1844 type->data.method = mono_metadata_parse_method_signature_full (m, generic_context, 0, ptr, &ptr);
1846 case MONO_TYPE_ARRAY:
1847 type->data.array = mono_metadata_parse_array_full (m, generic_context, ptr, &ptr);
1850 case MONO_TYPE_MVAR:
1851 type->data.generic_param = mono_metadata_parse_generic_param (m, generic_context, TRUE, ptr, &ptr);
1855 type->data.generic_param = mono_metadata_parse_generic_param (m, generic_context, FALSE, ptr, &ptr);
1858 case MONO_TYPE_GENERICINST:
1859 do_mono_metadata_parse_generic_class (type, m, generic_context, ptr, &ptr);
1863 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
1871 * mono_metadata_free_type:
1872 * @type: type to free
1874 * Free the memory allocated for type @type.
1877 mono_metadata_free_type (MonoType *type)
1879 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
1882 switch (type->type){
1883 case MONO_TYPE_OBJECT:
1884 case MONO_TYPE_STRING:
1885 if (!type->data.klass)
1888 case MONO_TYPE_CLASS:
1889 case MONO_TYPE_VALUETYPE:
1890 if (type == &type->data.klass->byval_arg || type == &type->data.klass->this_arg)
1894 mono_metadata_free_type (type->data.type);
1896 case MONO_TYPE_FNPTR:
1897 mono_metadata_free_method_signature (type->data.method);
1899 case MONO_TYPE_ARRAY:
1900 mono_metadata_free_array (type->data.array);
1908 hex_dump (const char *buffer, int base, int count)
1910 int show_header = 1;
1918 for (i = 0; i < count; i++){
1921 printf ("\n0x%08x: ", (unsigned char) base + i);
1923 printf ("%02x ", (unsigned char) (buffer [i]));
1930 * @mh: The Method header
1931 * @ptr: Points to the beginning of the Section Data (25.3)
1934 parse_section_data (MonoImage *m, MonoMethodHeader *mh, const unsigned char *ptr)
1936 unsigned char sect_data_flags;
1937 const unsigned char *sptr;
1939 guint32 sect_data_len;
1942 /* align on 32-bit boundary */
1943 /* FIXME: not 64-bit clean code */
1944 sptr = ptr = dword_align (ptr);
1945 sect_data_flags = *ptr;
1948 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
1950 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
1953 sect_data_len = ptr [0];
1957 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
1958 hex_dump (sptr, 0, sect_data_len+8);
1959 g_print ("\nheader: ");
1960 hex_dump (sptr-4, 0, 4);
1964 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
1965 const unsigned char *p = dword_align (ptr);
1967 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
1968 /* we could just store a pointer if we don't need to byteswap */
1969 mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
1970 for (i = 0; i < mh->num_clauses; ++i) {
1971 MonoExceptionClause *ec = &mh->clauses [i];
1974 ec->flags = read32 (p);
1976 ec->try_offset = read32 (p);
1978 ec->try_len = read32 (p);
1980 ec->handler_offset = read32 (p);
1982 ec->handler_len = read32 (p);
1984 tof_value = read32 (p);
1987 ec->flags = read16 (p);
1989 ec->try_offset = read16 (p);
1993 ec->handler_offset = read16 (p);
1995 ec->handler_len = *p;
1997 tof_value = read32 (p);
2000 if (ec->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2001 ec->data.filter_offset = tof_value;
2002 } else if (ec->flags == MONO_EXCEPTION_CLAUSE_NONE) {
2003 ec->data.catch_class = tof_value? mono_class_get (m, tof_value): 0;
2005 ec->data.catch_class = NULL;
2007 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
2011 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
2012 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
2019 * mono_metadata_parse_mh:
2020 * @m: metadata context
2021 * @ptr: pointer to the method header.
2023 * Decode the method header at @ptr, including pointer to the IL code,
2024 * info about local variables and optional exception tables.
2026 * Returns: a MonoMethodHeader.
2029 mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContext *generic_context, const char *ptr)
2031 MonoMethodHeader *mh;
2032 unsigned char flags = *(const unsigned char *) ptr;
2033 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
2035 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
2036 const unsigned char *code;
2039 g_return_val_if_fail (ptr != NULL, NULL);
2042 case METHOD_HEADER_TINY_FORMAT:
2043 mh = g_new0 (MonoMethodHeader, 1);
2046 local_var_sig_tok = 0;
2047 mh->code_size = flags >> 2;
2050 case METHOD_HEADER_TINY_FORMAT1:
2051 mh = g_new0 (MonoMethodHeader, 1);
2054 local_var_sig_tok = 0;
2057 * The spec claims 3 bits, but the Beta2 is
2060 mh->code_size = flags >> 2;
2063 case METHOD_HEADER_FAT_FORMAT:
2064 fat_flags = read16 (ptr);
2066 hsize = (fat_flags >> 12) & 0xf;
2067 max_stack = read16 (ptr);
2069 code_size = read32 (ptr);
2071 local_var_sig_tok = read32 (ptr);
2074 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
2081 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
2085 * There are more sections
2087 ptr = code + code_size;
2095 if (local_var_sig_tok) {
2096 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
2097 const char *locals_ptr;
2098 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
2099 int len=0, i, bsize;
2101 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
2102 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
2103 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
2104 if (*locals_ptr != 0x07)
2105 g_warning ("wrong signature for locals blob");
2107 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
2108 mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
2109 mh->num_locals = len;
2110 for (i = 0; i < len; ++i)
2111 mh->locals [i] = mono_metadata_parse_type_full (
2112 m, generic_context, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
2114 mh = g_new0 (MonoMethodHeader, 1);
2117 mh->code_size = code_size;
2118 mh->max_stack = max_stack;
2119 mh->init_locals = init_locals;
2120 if (fat_flags & METHOD_HEADER_MORE_SECTS)
2121 parse_section_data (m, mh, (const unsigned char*)ptr);
2126 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
2128 return mono_metadata_parse_mh_full (m, NULL, ptr);
2132 * mono_metadata_free_mh:
2133 * @mh: a method header
2135 * Free the memory allocated for the method header.
2138 mono_metadata_free_mh (MonoMethodHeader *mh)
2141 for (i = 0; i < mh->num_locals; ++i)
2142 mono_metadata_free_type (mh->locals[i]);
2143 g_free (mh->clauses);
2148 * mono_metadata_parse_field_type:
2149 * @m: metadata context to extract information from
2150 * @ptr: pointer to the field signature
2151 * @rptr: pointer updated to match the end of the decoded stream
2153 * Parses the field signature, and returns the type information for it.
2155 * Returns: The MonoType that was extracted from @ptr.
2158 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
2160 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
2164 * mono_metadata_parse_param:
2165 * @m: metadata context to extract information from
2166 * @ptr: pointer to the param signature
2167 * @rptr: pointer updated to match the end of the decoded stream
2169 * Parses the param signature, and returns the type information for it.
2171 * Returns: The MonoType that was extracted from @ptr.
2174 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
2176 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
2180 * mono_metadata_token_from_dor:
2181 * @dor_token: A TypeDefOrRef coded index
2183 * dor_token is a TypeDefOrRef coded index: it contains either
2184 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
2185 * bits contain an index into the table.
2187 * Returns: an expanded token
2190 mono_metadata_token_from_dor (guint32 dor_index)
2194 table = dor_index & 0x03;
2195 idx = dor_index >> 2;
2198 case 0: /* TypeDef */
2199 return MONO_TOKEN_TYPE_DEF | idx;
2201 case 1: /* TypeRef */
2202 return MONO_TOKEN_TYPE_REF | idx;
2204 case 2: /* TypeSpec */
2205 return MONO_TOKEN_TYPE_SPEC | idx;
2208 g_assert_not_reached ();
2215 * We use this to pass context information to the row locator
2218 int idx; /* The index that we are trying to locate */
2219 int col_idx; /* The index in the row where idx may be stored */
2220 MonoTableInfo *t; /* pointer to the table */
2225 * How the row locator works.
2230 * ___|___------> _______
2233 * A column in the rows of table A references an index in table B.
2234 * For example A may be the TYPEDEF table and B the METHODDEF table.
2236 * Given an index in table B we want to get the row in table A
2237 * where the column n references our index in B.
2239 * In the locator_t structure:
2241 * col_idx is the column number
2242 * index is the index in table B
2243 * result will be the index in table A
2246 * Table A Table B column (in table A)
2247 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
2248 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
2249 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
2250 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
2251 * METHODSEM PROPERTY ASSOCIATION (encoded index)
2253 * Note that we still don't support encoded indexes.
2257 typedef_locator (const void *a, const void *b)
2259 locator_t *loc = (locator_t *) a;
2260 const char *bb = (const char *) b;
2261 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
2262 guint32 col, col_next;
2264 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
2270 * Need to check that the next row is valid.
2272 if (typedef_index + 1 < loc->t->rows) {
2273 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
2274 if (loc->idx >= col_next)
2277 if (col == col_next)
2281 loc->result = typedef_index;
2287 table_locator (const void *a, const void *b)
2289 locator_t *loc = (locator_t *) a;
2290 const char *bb = (const char *) b;
2291 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2294 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2296 if (loc->idx == col) {
2297 loc->result = table_index;
2307 declsec_locator (const void *a, const void *b)
2309 locator_t *loc = (locator_t *) a;
2310 const char *bb = (const char *) b;
2311 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2314 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2316 if (loc->idx == col) {
2317 loc->result = table_index;
2327 * mono_metadata_typedef_from_field:
2328 * @meta: metadata context
2329 * @index: FieldDef token
2331 * Returns: the 1-based index into the TypeDef table of the type that
2332 * declared the field described by @index, or 0 if not found.
2335 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
2337 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2343 loc.idx = mono_metadata_token_index (index);
2344 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
2347 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2348 g_assert_not_reached ();
2350 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2351 return loc.result + 1;
2355 * mono_metadata_typedef_from_method:
2356 * @meta: metadata context
2357 * @index: MethodDef token
2359 * Returns: the 1-based index into the TypeDef table of the type that
2360 * declared the method described by @index. 0 if not found.
2363 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
2365 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2371 loc.idx = mono_metadata_token_index (index);
2372 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
2375 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2376 g_assert_not_reached ();
2378 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2379 return loc.result + 1;
2383 * mono_metadata_interfaces_from_typedef:
2384 * @meta: metadata context
2385 * @index: typedef token
2387 * Returns: and array of interfaces that the @index typedef token implements.
2388 * The number of elemnts in the array is returned in @count.
2391 mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, guint *count, MonoGenericContext *context)
2393 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
2396 guint32 cols [MONO_INTERFACEIMPL_SIZE];
2404 loc.idx = mono_metadata_token_index (index);
2405 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
2408 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2413 * We may end up in the middle of the rows...
2416 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
2423 while (start < tdef->rows) {
2424 mono_metadata_decode_row (tdef, start, cols, MONO_INTERFACEIMPL_SIZE);
2425 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
2427 result = g_renew (MonoClass*, result, i + 1);
2428 result [i] = mono_class_get_full (
2429 meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]), context);
2437 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
2439 return mono_metadata_interfaces_from_typedef_full (meta, index, count, NULL);
2443 * mono_metadata_nested_in_typedef:
2444 * @meta: metadata context
2445 * @index: typedef token
2447 * Returns: the 1-based index into the TypeDef table of the type
2448 * where the type described by @index is nested.
2449 * Retruns 0 if @index describes a non-nested type.
2452 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
2454 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2460 loc.idx = mono_metadata_token_index (index);
2461 loc.col_idx = MONO_NESTED_CLASS_NESTED;
2464 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2467 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2468 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
2472 * mono_metadata_nesting_typedef:
2473 * @meta: metadata context
2474 * @index: typedef token
2476 * Returns: the 1-based index into the TypeDef table of the first type
2477 * that is nested inside the type described by @index. The search starts at
2478 * @start_index. returns 0 if no such type is found.
2481 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
2483 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2485 guint32 class_index = mono_metadata_token_index (index);
2490 start = start_index;
2492 while (start <= tdef->rows) {
2493 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
2499 if (start > tdef->rows)
2506 * mono_metadata_packing_from_typedef:
2507 * @meta: metadata context
2508 * @index: token representing a type
2510 * Returns: the info stored in the ClassLAyout table for the given typedef token
2511 * into the @packing and @size pointers.
2512 * Returns 0 if the info is not found.
2515 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
2517 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
2519 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
2524 loc.idx = mono_metadata_token_index (index);
2525 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
2528 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2531 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
2533 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
2535 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
2537 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2538 return loc.result + 1;
2542 * mono_metadata_custom_attrs_from_index:
2543 * @meta: metadata context
2544 * @index: token representing the parent
2546 * Returns: the 1-based index into the CustomAttribute table of the first
2547 * attribute which belongs to the metadata object described by @index.
2548 * Returns 0 if no such attribute is found.
2551 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
2553 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
2560 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
2563 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2566 /* Find the first entry by searching backwards */
2567 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
2570 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2571 return loc.result + 1;
2575 * mono_metadata_declsec_from_index:
2576 * @meta: metadata context
2577 * @index: token representing the parent
2579 * Returns: the 0-based index into the DeclarativeSecurity table of the first
2580 * attribute which belongs to the metadata object described by @index.
2581 * Returns -1 if no such attribute is found.
2584 mono_metadata_declsec_from_index (MonoImage *meta, guint32 index)
2586 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_DECLSECURITY];
2593 loc.col_idx = MONO_DECL_SECURITY_PARENT;
2596 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
2599 /* Find the first entry by searching backwards */
2600 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_DECL_SECURITY_PARENT) == index))
2608 mono_backtrace (int limit)
2613 backtrace (array, limit);
2614 names = backtrace_symbols (array, limit);
2615 for (i =0; i < limit; ++i) {
2616 g_print ("\t%s\n", names [i]);
2623 /*#define __alignof__(a) sizeof(a)*/
2624 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
2629 * @t: the type to return the size of
2631 * Returns: the number of bytes required to hold an instance of this
2635 mono_type_size (MonoType *t, gint *align)
2642 *align = __alignof__(gpointer);
2643 return sizeof (gpointer);
2647 case MONO_TYPE_VOID:
2650 case MONO_TYPE_BOOLEAN:
2651 *align = __alignof__(gint8);
2655 *align = __alignof__(gint8);
2657 case MONO_TYPE_CHAR:
2660 *align = __alignof__(gint16);
2664 *align = __alignof__(gint32);
2667 *align = __alignof__(float);
2671 *align = __alignof__(gint64);
2674 *align = __alignof__(double);
2678 *align = __alignof__(gpointer);
2679 return sizeof (gpointer);
2680 case MONO_TYPE_STRING:
2681 *align = __alignof__(gpointer);
2682 return sizeof (gpointer);
2683 case MONO_TYPE_OBJECT:
2684 *align = __alignof__(gpointer);
2685 return sizeof (gpointer);
2686 case MONO_TYPE_VALUETYPE: {
2687 if (t->data.klass->enumtype)
2688 return mono_type_size (t->data.klass->enum_basetype, align);
2690 return mono_class_value_size (t->data.klass, align);
2692 case MONO_TYPE_CLASS:
2693 case MONO_TYPE_SZARRAY:
2695 case MONO_TYPE_FNPTR:
2696 case MONO_TYPE_ARRAY:
2697 *align = __alignof__(gpointer);
2698 return sizeof (gpointer);
2699 case MONO_TYPE_TYPEDBYREF:
2700 return mono_class_value_size (mono_defaults.typed_reference_class, align);
2701 case MONO_TYPE_GENERICINST: {
2702 MonoGenericClass *gclass = t->data.generic_class;
2704 g_assert (!gclass->inst->is_open && !gclass->klass->generic_container);
2706 if (gclass->container_class->valuetype) {
2707 if (gclass->container_class->enumtype)
2708 return mono_type_size (gclass->container_class->enum_basetype, align);
2710 return mono_class_value_size (gclass->klass, align);
2712 *align = __alignof__(gpointer);
2713 return sizeof (gpointer);
2717 case MONO_TYPE_MVAR:
2718 *align = __alignof__(gpointer);
2719 return sizeof (gpointer);
2721 g_error ("mono_type_size: type 0x%02x unknown", t->type);
2727 * mono_type_stack_size:
2728 * @t: the type to return the size it uses on the stack
2730 * Returns: the number of bytes required to hold an instance of this
2731 * type on the runtime stack
2734 mono_type_stack_size (MonoType *t, gint *align)
2738 g_assert (t != NULL);
2744 *align = __alignof__(gpointer);
2745 return sizeof (gpointer);
2749 case MONO_TYPE_BOOLEAN:
2750 case MONO_TYPE_CHAR:
2759 case MONO_TYPE_STRING:
2760 case MONO_TYPE_OBJECT:
2761 case MONO_TYPE_CLASS:
2762 case MONO_TYPE_SZARRAY:
2764 case MONO_TYPE_FNPTR:
2765 case MONO_TYPE_ARRAY:
2766 *align = __alignof__(gpointer);
2767 return sizeof (gpointer);
2768 case MONO_TYPE_TYPEDBYREF:
2769 *align = __alignof__(gpointer);
2770 return sizeof (gpointer) * 3;
2772 *align = __alignof__(float);
2773 return sizeof (float);
2776 *align = __alignof__(gint64);
2777 return sizeof (gint64);
2779 *align = __alignof__(double);
2780 return sizeof (double);
2781 case MONO_TYPE_VALUETYPE: {
2784 if (t->data.klass->enumtype)
2785 return mono_type_stack_size (t->data.klass->enum_basetype, align);
2787 size = mono_class_value_size (t->data.klass, align);
2789 *align = *align + __alignof__(gpointer) - 1;
2790 *align &= ~(__alignof__(gpointer) - 1);
2792 size += sizeof (gpointer) - 1;
2793 size &= ~(sizeof (gpointer) - 1);
2798 case MONO_TYPE_GENERICINST: {
2799 MonoGenericClass *gclass = t->data.generic_class;
2801 g_assert (!gclass->inst->is_open && !gclass->klass->generic_container);
2803 if (gclass->container_class->valuetype) {
2804 if (gclass->container_class->enumtype)
2805 return mono_type_stack_size (gclass->container_class->enum_basetype, align);
2807 guint32 size = mono_class_value_size (gclass->klass, align);
2809 *align = *align + __alignof__(gpointer) - 1;
2810 *align &= ~(__alignof__(gpointer) - 1);
2812 size += sizeof (gpointer) - 1;
2813 size &= ~(sizeof (gpointer) - 1);
2818 *align = __alignof__(gpointer);
2819 return sizeof (gpointer);
2823 g_error ("type 0x%02x unknown", t->type);
2829 mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass)
2831 return gclass->container_class->valuetype;
2835 _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2, gboolean signature_only)
2839 if ((g1->inst->type_argc != g2->inst->type_argc) || (g1->is_dynamic != g2->is_dynamic))
2841 if (!mono_metadata_class_equal (g1->container_class, g2->container_class, signature_only))
2843 for (i = 0; i < g1->inst->type_argc; ++i) {
2844 if (!do_mono_metadata_type_equal (g1->inst->type_argv [i], g2->inst->type_argv [i], signature_only))
2851 mono_metadata_generic_method_hash (MonoGenericMethod *gmethod)
2853 return gmethod->inst->id;
2857 mono_metadata_generic_method_equal (MonoGenericMethod *g1, MonoGenericMethod *g2)
2859 return (g1->container == g2->container) && (g1->inst == g2->inst);
2864 * mono_metadata_type_hash:
2867 * Computes an hash value for @t1 to be used in GHashTable.
2870 mono_metadata_type_hash (MonoType *t1)
2872 guint hash = t1->type;
2874 hash |= t1->byref << 6; /* do not collide with t1->type values */
2876 case MONO_TYPE_VALUETYPE:
2877 case MONO_TYPE_CLASS:
2878 case MONO_TYPE_SZARRAY:
2879 /* check if the distribution is good enough */
2880 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
2882 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
2883 case MONO_TYPE_ARRAY:
2884 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
2885 case MONO_TYPE_GENERICINST:
2886 return ((hash << 5) - hash) ^ mono_generic_class_hash (t1->data.generic_class);
2892 mono_metadata_generic_param_equal (MonoGenericParam *p1, MonoGenericParam *p2, gboolean signature_only)
2896 if (p1->num != p2->num)
2899 g_assert (p1->owner && p2->owner);
2900 if (p1->owner == p2->owner)
2904 * If `signature_only' is true, we're comparing two (method) signatures.
2905 * In this case, the owner of two type parameters doesn't need to match.
2908 return signature_only;
2912 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only)
2916 if (c1->generic_class && c2->generic_class)
2917 return _mono_metadata_generic_class_equal (c1->generic_class, c2->generic_class, signature_only);
2918 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
2919 return mono_metadata_generic_param_equal (
2920 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, FALSE);
2921 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
2922 return mono_metadata_generic_param_equal (
2923 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
2928 * mono_metadata_type_equal:
2932 * Determine if @t1 and @t2 represent the same type.
2933 * Returns: #TRUE if @t1 and @t2 are equal.
2936 do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only)
2938 if (t1->type != t2->type ||
2939 t1->byref != t2->byref)
2943 case MONO_TYPE_VOID:
2944 case MONO_TYPE_BOOLEAN:
2945 case MONO_TYPE_CHAR:
2956 case MONO_TYPE_STRING:
2959 case MONO_TYPE_OBJECT:
2960 case MONO_TYPE_TYPEDBYREF:
2962 case MONO_TYPE_VALUETYPE:
2963 case MONO_TYPE_CLASS:
2964 case MONO_TYPE_SZARRAY:
2965 return mono_metadata_class_equal (t1->data.klass, t2->data.klass, signature_only);
2967 return do_mono_metadata_type_equal (t1->data.type, t2->data.type, signature_only);
2968 case MONO_TYPE_ARRAY:
2969 if (t1->data.array->rank != t2->data.array->rank)
2971 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass, signature_only);
2972 case MONO_TYPE_GENERICINST:
2973 return _mono_metadata_generic_class_equal (
2974 t1->data.generic_class, t2->data.generic_class, signature_only);
2976 return mono_metadata_generic_param_equal (
2977 t1->data.generic_param, t2->data.generic_param, FALSE);
2978 case MONO_TYPE_MVAR:
2979 return mono_metadata_generic_param_equal (
2980 t1->data.generic_param, t2->data.generic_param, signature_only);
2982 g_error ("implement type compare for %0x!", t1->type);
2990 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
2992 return do_mono_metadata_type_equal (t1, t2, FALSE);
2996 * mono_metadata_signature_equal:
2997 * @sig1: a signature
2998 * @sig2: another signature
3000 * Determine if @sig1 and @sig2 represent the same signature, with the
3001 * same number of arguments and the same types.
3002 * Returns: #TRUE if @sig1 and @sig2 are equal.
3005 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
3009 if (sig1->hasthis != sig2->hasthis ||
3010 sig1->param_count != sig2->param_count)
3014 * We're just comparing the signatures of two methods here:
3016 * If we have two generic methods `void Foo<U> (U u)' and `void Bar<V> (V v)',
3017 * U and V are equal here.
3019 * That's what the `signature_only' argument of do_mono_metadata_type_equal() is for.
3022 for (i = 0; i < sig1->param_count; i++) {
3023 MonoType *p1 = sig1->params[i];
3024 MonoType *p2 = sig2->params[i];
3026 /* if (p1->attrs != p2->attrs)
3029 if (!do_mono_metadata_type_equal (p1, p2, TRUE))
3033 if (!do_mono_metadata_type_equal (sig1->ret, sig2->ret, TRUE))
3039 mono_signature_hash (MonoMethodSignature *sig)
3041 guint i, res = sig->ret->type;
3043 for (i = 0; i < sig->param_count; i++)
3044 res = (res << 5) - res + sig->params[i]->type;
3050 * mono_metadata_encode_value:
3051 * @value: value to encode
3052 * @buf: buffer where to write the compressed representation
3053 * @endbuf: pointer updated to point at the end of the encoded output
3055 * Encodes the value @value in the compressed representation used
3056 * in metadata and stores the result in @buf. @buf needs to be big
3057 * enough to hold the data (4 bytes).
3060 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
3066 else if (value < 0x4000) {
3067 p [0] = 0x80 | (value >> 8);
3068 p [1] = value & 0xff;
3071 p [0] = (value >> 24) | 0xc0;
3072 p [1] = (value >> 16) & 0xff;
3073 p [2] = (value >> 8) & 0xff;
3074 p [3] = value & 0xff;
3082 * mono_metadata_field_info:
3083 * @meta: the Image the field is defined in
3084 * @index: the index in the field table representing the field
3085 * @offset: a pointer to an integer where to store the offset that
3086 * may have been specified for the field in a FieldLayout table
3087 * @rva: a pointer to the RVA of the field data in the image that
3088 * may have been defined in a FieldRVA table
3089 * @marshal_spec: a pointer to the marshal spec that may have been
3090 * defined for the field in a FieldMarshal table.
3092 * Gather info for field @index that may have been defined in the FieldLayout,
3093 * FieldRVA and FieldMarshal tables.
3094 * Either of offset, rva and marshal_spec can be NULL if you're not interested
3098 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
3099 MonoMarshalSpec **marshal_spec)
3101 MonoTableInfo *tdef;
3104 loc.idx = index + 1;
3106 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
3108 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
3111 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3112 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
3114 *offset = (guint32)-1;
3118 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
3120 loc.col_idx = MONO_FIELD_RVA_FIELD;
3123 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3125 * LAMESPEC: There is no signature, no nothing, just the raw data.
3127 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
3135 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
3136 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
3143 * mono_metadata_get_constant_index:
3144 * @meta: the Image the field is defined in
3145 * @index: the token that may have a row defined in the constants table
3146 * @hint: possible position for the row
3148 * @token must be a FieldDef, ParamDef or PropertyDef token.
3150 * Returns: the index into the Constants table or 0 if not found.
3153 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
3155 MonoTableInfo *tdef;
3157 guint32 index = mono_metadata_token_index (token);
3159 tdef = &meta->tables [MONO_TABLE_CONSTANT];
3160 index <<= MONO_HASCONSTANT_BITS;
3161 switch (mono_metadata_token_table (token)) {
3162 case MONO_TABLE_FIELD:
3163 index |= MONO_HASCONSTANT_FIEDDEF;
3165 case MONO_TABLE_PARAM:
3166 index |= MONO_HASCONSTANT_PARAM;
3168 case MONO_TABLE_PROPERTY:
3169 index |= MONO_HASCONSTANT_PROPERTY;
3172 g_warning ("Not a valid token for the constant table: 0x%08x", token);
3176 loc.col_idx = MONO_CONSTANT_PARENT;
3179 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
3182 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3183 return loc.result + 1;
3189 * mono_metadata_events_from_typedef:
3190 * @meta: metadata context
3191 * @index: 0-based index (in the TypeDef table) describing a type
3193 * Returns: the 0-based index in the Event table for the events in the
3194 * type. The last event that belongs to the type (plus 1) is stored
3195 * in the @end_idx pointer.
3198 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3202 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
3210 loc.col_idx = MONO_EVENT_MAP_PARENT;
3211 loc.idx = index + 1;
3213 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3216 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
3217 if (loc.result + 1 < tdef->rows) {
3218 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
3220 end = meta->tables [MONO_TABLE_EVENT].rows;
3228 * mono_metadata_methods_from_event:
3229 * @meta: metadata context
3230 * @index: 0-based index (in the Event table) describing a event
3232 * Returns: the 0-based index in the MethodDef table for the methods in the
3233 * event. The last method that belongs to the event (plus 1) is stored
3234 * in the @end_idx pointer.
3237 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
3241 guint32 cols [MONO_METHOD_SEMA_SIZE];
3242 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3249 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3250 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
3252 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3257 * We may end up in the middle of the rows...
3260 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3266 while (end < msemt->rows) {
3267 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3268 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3277 * mono_metadata_properties_from_typedef:
3278 * @meta: metadata context
3279 * @index: 0-based index (in the TypeDef table) describing a type
3281 * Returns: the 0-based index in the Property table for the properties in the
3282 * type. The last property that belongs to the type (plus 1) is stored
3283 * in the @end_idx pointer.
3286 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3290 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
3298 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
3299 loc.idx = index + 1;
3301 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3304 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
3305 if (loc.result + 1 < tdef->rows) {
3306 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
3308 end = meta->tables [MONO_TABLE_PROPERTY].rows;
3316 * mono_metadata_methods_from_property:
3317 * @meta: metadata context
3318 * @index: 0-based index (in the PropertyDef table) describing a property
3320 * Returns: the 0-based index in the MethodDef table for the methods in the
3321 * property. The last method that belongs to the property (plus 1) is stored
3322 * in the @end_idx pointer.
3325 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
3329 guint32 cols [MONO_METHOD_SEMA_SIZE];
3330 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3337 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3338 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
3340 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3345 * We may end up in the middle of the rows...
3348 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3354 while (end < msemt->rows) {
3355 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3356 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3365 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
3368 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
3374 loc.col_idx = MONO_IMPLMAP_MEMBER;
3375 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
3377 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3380 return loc.result + 1;
3384 * @image: context where the image is created
3385 * @type_spec: typespec token
3387 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
3391 mono_type_create_from_typespec_full (MonoImage *image, MonoGenericContext *generic_context, guint32 type_spec)
3393 guint32 idx = mono_metadata_token_index (type_spec);
3395 guint32 cols [MONO_TYPESPEC_SIZE];
3400 mono_loader_lock ();
3402 if ((type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec)))) {
3403 mono_loader_unlock ();
3407 t = &image->tables [MONO_TABLE_TYPESPEC];
3409 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
3410 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
3411 len = mono_metadata_decode_value (ptr, &ptr);
3413 type = g_new0 (MonoType, 1);
3415 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
3417 if (*ptr == MONO_TYPE_BYREF) {
3422 do_mono_metadata_parse_type (type, image, generic_context, ptr, &ptr);
3424 mono_loader_unlock ();
3430 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
3432 return mono_type_create_from_typespec_full (image, NULL, type_spec);
3436 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
3438 MonoMarshalSpec *res;
3440 const char *start = ptr;
3442 /* fixme: this is incomplete, but I cant find more infos in the specs */
3444 res = g_new0 (MonoMarshalSpec, 1);
3446 len = mono_metadata_decode_value (ptr, &ptr);
3447 res->native = *ptr++;
3449 if (res->native == MONO_NATIVE_LPARRAY) {
3450 if (ptr - start <= len)
3451 res->data.array_data.elem_type = *ptr++;
3452 if (ptr - start <= len)
3453 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
3454 if (ptr - start <= len)
3455 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3458 if (res->native == MONO_NATIVE_BYVALTSTR) {
3459 if (ptr - start <= len)
3460 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3463 if (res->native == MONO_NATIVE_BYVALARRAY) {
3464 if (ptr - start <= len)
3465 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3468 if (res->native == MONO_NATIVE_CUSTOM) {
3469 /* skip unused type guid */
3470 len = mono_metadata_decode_value (ptr, &ptr);
3472 /* skip unused native type name */
3473 len = mono_metadata_decode_value (ptr, &ptr);
3475 /* read custom marshaler type name */
3476 len = mono_metadata_decode_value (ptr, &ptr);
3477 res->data.custom_data.custom_name = g_strndup (ptr, len);
3479 /* read cookie string */
3480 len = mono_metadata_decode_value (ptr, &ptr);
3481 res->data.custom_data.cookie = g_strndup (ptr, len);
3488 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
3490 if (spec->native == MONO_NATIVE_CUSTOM) {
3491 g_free (spec->data.custom_data.custom_name);
3492 g_free (spec->data.custom_data.cookie);
3498 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
3499 gboolean unicode, MonoMarshalConv *conv)
3501 MonoMarshalConv dummy_conv;
3507 *conv = MONO_MARSHAL_CONV_NONE;
3510 return MONO_NATIVE_UINT;
3514 case MONO_TYPE_BOOLEAN:
3516 switch (mspec->native) {
3517 case MONO_NATIVE_VARIANTBOOL:
3518 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
3519 return MONO_NATIVE_VARIANTBOOL;
3520 case MONO_NATIVE_BOOLEAN:
3521 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3522 return MONO_NATIVE_BOOLEAN;
3523 case MONO_NATIVE_I1:
3524 case MONO_NATIVE_U1:
3525 return mspec->native;
3527 g_error ("cant marshal bool to native type %02x", mspec->native);
3530 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3531 return MONO_NATIVE_BOOLEAN;
3532 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
3533 case MONO_TYPE_I1: return MONO_NATIVE_I1;
3534 case MONO_TYPE_U1: return MONO_NATIVE_U1;
3535 case MONO_TYPE_I2: return MONO_NATIVE_I2;
3536 case MONO_TYPE_U2: return MONO_NATIVE_U2;
3537 case MONO_TYPE_I4: return MONO_NATIVE_I4;
3538 case MONO_TYPE_U4: return MONO_NATIVE_U4;
3539 case MONO_TYPE_I8: return MONO_NATIVE_I8;
3540 case MONO_TYPE_U8: return MONO_NATIVE_U8;
3541 case MONO_TYPE_R4: return MONO_NATIVE_R4;
3542 case MONO_TYPE_R8: return MONO_NATIVE_R8;
3543 case MONO_TYPE_STRING:
3545 switch (mspec->native) {
3546 case MONO_NATIVE_BSTR:
3547 *conv = MONO_MARSHAL_CONV_STR_BSTR;
3548 return MONO_NATIVE_BSTR;
3549 case MONO_NATIVE_LPSTR:
3550 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
3551 return MONO_NATIVE_LPSTR;
3552 case MONO_NATIVE_LPWSTR:
3553 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
3554 return MONO_NATIVE_LPWSTR;
3555 case MONO_NATIVE_LPTSTR:
3556 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3557 return MONO_NATIVE_LPTSTR;
3558 case MONO_NATIVE_ANSIBSTR:
3559 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
3560 return MONO_NATIVE_ANSIBSTR;
3561 case MONO_NATIVE_TBSTR:
3562 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
3563 return MONO_NATIVE_TBSTR;
3564 case MONO_NATIVE_BYVALTSTR:
3566 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
3568 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
3569 return MONO_NATIVE_BYVALTSTR;
3571 g_error ("cant marshal string to native type %02x", mspec->native);
3574 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3575 return MONO_NATIVE_LPTSTR;
3576 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
3577 case MONO_TYPE_VALUETYPE: /*FIXME*/
3578 if (type->data.klass->enumtype) {
3579 t = type->data.klass->enum_basetype->type;
3582 return MONO_NATIVE_STRUCT;
3583 case MONO_TYPE_SZARRAY:
3584 case MONO_TYPE_ARRAY:
3586 switch (mspec->native) {
3587 case MONO_NATIVE_BYVALARRAY:
3588 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
3589 return MONO_NATIVE_BYVALARRAY;
3590 case MONO_NATIVE_SAFEARRAY:
3591 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
3592 return MONO_NATIVE_SAFEARRAY;
3593 case MONO_NATIVE_LPARRAY:
3594 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3595 return MONO_NATIVE_LPARRAY;
3597 g_error ("cant marshal array as native type %02x", mspec->native);
3601 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3602 return MONO_NATIVE_LPARRAY;
3603 case MONO_TYPE_I: return MONO_NATIVE_INT;
3604 case MONO_TYPE_U: return MONO_NATIVE_UINT;
3605 case MONO_TYPE_CLASS:
3606 case MONO_TYPE_OBJECT: {
3607 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
3609 switch (mspec->native) {
3610 case MONO_NATIVE_STRUCT:
3611 return MONO_NATIVE_STRUCT;
3612 case MONO_NATIVE_INTERFACE:
3613 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
3614 return MONO_NATIVE_INTERFACE;
3615 case MONO_NATIVE_IDISPATCH:
3616 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
3617 return MONO_NATIVE_IDISPATCH;
3618 case MONO_NATIVE_IUNKNOWN:
3619 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
3620 return MONO_NATIVE_IUNKNOWN;
3621 case MONO_NATIVE_FUNC:
3622 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3623 type->data.klass == mono_defaults.delegate_class ||
3624 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3625 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3626 return MONO_NATIVE_FUNC;
3632 g_error ("cant marshal object as native type %02x", mspec->native);
3635 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3636 type->data.klass == mono_defaults.delegate_class ||
3637 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3638 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3639 return MONO_NATIVE_FUNC;
3641 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
3642 return MONO_NATIVE_STRUCT;
3644 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
3645 case MONO_TYPE_TYPEDBYREF:
3647 g_error ("type 0x%02x not handled in marshal", t);
3649 return MONO_NATIVE_MAX;
3653 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
3656 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
3662 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
3663 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
3665 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3668 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
3672 method_from_method_def_or_ref (MonoImage *m, guint32 tok)
3674 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
3675 switch (tok & MONO_METHODDEFORREF_MASK) {
3676 case MONO_METHODDEFORREF_METHODDEF:
3677 return mono_get_method (m, MONO_TOKEN_METHOD_DEF | idx, NULL);
3678 case MONO_METHODDEFORREF_METHODREF:
3679 return mono_get_method (m, MONO_TOKEN_MEMBER_REF | idx, NULL);
3681 g_assert_not_reached ();
3686 mono_class_get_overrides (MonoImage *image, guint32 type_token, gint32 *num_overrides)
3689 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
3692 guint32 cols [MONO_METHODIMPL_SIZE];
3693 MonoMethod **result;
3702 loc.col_idx = MONO_METHODIMPL_CLASS;
3703 loc.idx = mono_metadata_token_index (type_token);
3705 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3711 * We may end up in the middle of the rows...
3714 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
3719 while (end < tdef->rows) {
3720 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
3726 result = g_new (MonoMethod*, num * 2);
3727 for (i = 0; i < num; ++i) {
3728 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
3729 result [i * 2] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_DECLARATION]);
3730 result [i * 2 + 1] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_BODY]);
3734 *num_overrides = num;
3739 * mono_guid_to_string:
3741 * Converts a 16 byte Microsoft GUID to the standard string representation.
3744 mono_guid_to_string (const guint8 *guid)
3746 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
3747 guid[3], guid[2], guid[1], guid[0],
3751 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
3755 get_constraints (MonoImage *image, int owner, MonoGenericContext *context)
3757 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
3758 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
3759 guint32 i, token, found;
3760 MonoClass *klass, **res;
3761 GList *cons = NULL, *tmp;
3765 for (i = 0; i < tdef->rows; ++i) {
3766 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
3767 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
3768 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
3769 klass = mono_class_get_full (image, token, context);
3770 cons = g_list_append (cons, klass);
3773 /* contiguous list finished */
3780 res = g_new0 (MonoClass*, found + 1);
3781 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
3782 res [i] = tmp->data;
3788 MonoGenericContainer *
3789 mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericContainer *parent_container)
3791 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
3792 guint32 cols [MONO_GENERICPARAM_SIZE];
3793 guint32 i, owner = 0, last_num, n;
3794 MonoGenericContainer *container;
3795 MonoGenericClass *gclass;
3796 MonoGenericParam *params;
3798 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
3799 owner = MONO_TYPEORMETHOD_TYPE;
3800 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3801 owner = MONO_TYPEORMETHOD_METHOD;
3803 g_error ("wrong token %x to load_generics_params", token);
3806 owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
3810 for (i = 0; i < tdef->rows; ++i) {
3811 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3812 if (cols [MONO_GENERICPARAM_OWNER] == owner)
3816 if (i >= tdef->rows)
3820 container = g_new0 (MonoGenericContainer, 1);
3823 params = g_realloc (params, sizeof (MonoGenericParam) * n);
3824 params [n - 1].owner = container;
3825 params [n - 1].pklass = NULL;
3826 params [n - 1].method = NULL;
3827 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
3828 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
3829 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
3830 if (++i >= tdef->rows)
3832 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3833 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
3835 container->type_argc = n;
3836 container->type_params = params;
3837 container->parent = parent_container;
3839 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3840 container->is_method = 1;
3842 container->context.container = container;
3844 for (i = 0; i < n; i++)
3845 params [i].constraints = get_constraints (image, last_num + i + 1, &container->context);
3851 mono_type_is_byref (MonoType *type)
3857 mono_type_get_type (MonoType *type)
3862 /* For MONO_TYPE_FNPTR */
3863 MonoMethodSignature*
3864 mono_type_get_signature (MonoType *type)
3866 return type->data.method;
3869 /* For MONO_TYPE_CLASS, VALUETYPE */
3871 mono_type_get_class (MonoType *type)
3873 return type->data.klass;
3876 /* For MONO_TYPE_ARRAY */
3878 mono_type_get_array_type (MonoType *type)
3880 return type->data.array;
3884 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
3886 /* FIXME: implement */
3891 mono_signature_get_return_type (MonoMethodSignature *sig)
3897 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
3903 /* start from the first */
3904 if (sig->param_count) {
3905 *iter = &sig->params [0];
3906 return sig->params [0];
3914 if (type < &sig->params [sig->param_count]) {
3922 mono_signature_get_param_count (MonoMethodSignature *sig)
3924 return sig->param_count;
3928 mono_signature_get_call_conv (MonoMethodSignature *sig)
3930 return sig->call_convention;
3934 mono_signature_vararg_start (MonoMethodSignature *sig)
3936 return sig->sentinelpos;
3940 mono_signature_is_instance (MonoMethodSignature *sig)
3942 return sig->hasthis;
3946 mono_signature_explicit_this (MonoMethodSignature *sig)
3948 return sig->explicit_this;