2 * metadata.c: Routines for accessing the metadata
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
15 #include "tabledefs.h"
18 #include "tokentype.h"
22 static void do_mono_metadata_parse_type (MonoType *type, MonoMetadata *m, const char *ptr, const char **rptr);
25 * Encoding of the "description" argument:
27 * identifier [CODE ARG]
29 * If CODE is ':', then a lookup on table ARG is performed
30 * If CODE is '=', then a lookup in the aliased-table ARG is performed
31 * If CODE is '#', then this encodes a flag, ARG is the flag name.
33 * Aliased table for example is `CustomAttributeType' which depending on the
34 * information might refer to different tables.
37 static MonoMetaTable AssemblySchema [] = {
38 { MONO_MT_UINT32, "HashId" },
39 { MONO_MT_UINT16, "Major" },
40 { MONO_MT_UINT16, "Minor" },
41 { MONO_MT_UINT16, "BuildNumber" },
42 { MONO_MT_UINT16, "RevisionNumber" },
43 { MONO_MT_UINT32, "Flags" },
44 { MONO_MT_BLOB_IDX, "PublicKey" },
45 { MONO_MT_STRING_IDX, "Name" },
46 { MONO_MT_STRING_IDX, "Culture" },
50 static MonoMetaTable AssemblyOSSchema [] = {
51 { MONO_MT_UINT32, "OSPlatformID" },
52 { MONO_MT_UINT32, "OSMajor" },
53 { MONO_MT_UINT32, "OSMinor" },
57 static MonoMetaTable AssemblyProcessorSchema [] = {
58 { MONO_MT_UINT32, "Processor" },
62 static MonoMetaTable AssemblyRefSchema [] = {
63 { MONO_MT_UINT16, "Major" },
64 { MONO_MT_UINT16, "Minor" },
65 { MONO_MT_UINT16, "Build" },
66 { MONO_MT_UINT16, "Revision" },
67 { MONO_MT_UINT32, "Flags" },
68 { MONO_MT_BLOB_IDX, "PublicKeyOrToken" },
69 { MONO_MT_STRING_IDX, "Name" },
70 { MONO_MT_STRING_IDX, "Culture" },
71 { MONO_MT_BLOB_IDX, "HashValue" },
75 static MonoMetaTable AssemblyRefOSSchema [] = {
76 { MONO_MT_UINT32, "OSPlatformID" },
77 { MONO_MT_UINT32, "OSMajorVersion" },
78 { MONO_MT_UINT32, "OSMinorVersion" },
79 { MONO_MT_TABLE_IDX, "AssemblyRef:AssemblyRef" },
83 static MonoMetaTable AssemblyRefProcessorSchema [] = {
84 { MONO_MT_UINT32, "Processor" },
85 { MONO_MT_TABLE_IDX, "AssemblyRef:AssemblyRef" },
89 static MonoMetaTable ClassLayoutSchema [] = {
90 { MONO_MT_UINT16, "PackingSize" },
91 { MONO_MT_UINT32, "ClassSize" },
92 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
96 static MonoMetaTable ConstantSchema [] = {
97 { MONO_MT_UINT8, "Type" },
98 { MONO_MT_UINT8, "PaddingZero" },
99 { MONO_MT_CONST_IDX, "Parent" },
100 { MONO_MT_BLOB_IDX, "Value" },
101 { MONO_MT_END, NULL }
104 static MonoMetaTable CustomAttributeSchema [] = {
105 { MONO_MT_HASCAT_IDX, "Parent" },
106 { MONO_MT_CAT_IDX, "Type" },
107 { MONO_MT_BLOB_IDX, "Value" },
108 { MONO_MT_END, NULL }
111 static MonoMetaTable DeclSecuritySchema [] = {
112 { MONO_MT_UINT16, "Action" },
113 { MONO_MT_HASDEC_IDX, "Parent" },
114 { MONO_MT_BLOB_IDX, "PermissionSet" },
115 { MONO_MT_END, NULL }
118 static MonoMetaTable EventMapSchema [] = {
119 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
120 { MONO_MT_TABLE_IDX, "EventList:Event" },
121 { MONO_MT_END, NULL }
124 static MonoMetaTable EventSchema [] = {
125 { MONO_MT_UINT16, "EventFlags#EventAttribute" },
126 { MONO_MT_STRING_IDX, "Name" },
127 { MONO_MT_TABLE_IDX, "EventType" }, /* TypeDef or TypeRef */
128 { MONO_MT_END, NULL }
131 static MonoMetaTable ExportedTypeSchema [] = {
132 { MONO_MT_UINT32, "Flags" },
133 { MONO_MT_TABLE_IDX, "TypeDefId" },
134 { MONO_MT_STRING_IDX, "TypeName" },
135 { MONO_MT_STRING_IDX, "TypeNameSpace" },
136 { MONO_MT_IMPL_IDX, "Implementation" },
137 { MONO_MT_END, NULL }
140 static MonoMetaTable FieldSchema [] = {
141 { MONO_MT_UINT16, "Flags" },
142 { MONO_MT_STRING_IDX, "Name" },
143 { MONO_MT_BLOB_IDX, "Signature" },
144 { MONO_MT_END, NULL }
146 static MonoMetaTable FieldLayoutSchema [] = {
147 { MONO_MT_UINT32, "Offset" },
148 { MONO_MT_TABLE_IDX, "Field:Field" },
149 { MONO_MT_END, NULL }
152 static MonoMetaTable FieldMarshalSchema [] = {
153 { MONO_MT_HFM_IDX, "Parent" },
154 { MONO_MT_BLOB_IDX, "NativeType" },
155 { MONO_MT_END, NULL }
157 static MonoMetaTable FieldRVASchema [] = {
158 { MONO_MT_UINT32, "RVA" },
159 { MONO_MT_TABLE_IDX, "Field:Field" },
160 { MONO_MT_END, NULL }
163 static MonoMetaTable FileSchema [] = {
164 { MONO_MT_UINT32, "Flags" },
165 { MONO_MT_STRING_IDX, "Name" },
166 { MONO_MT_BLOB_IDX, "Value" },
167 { MONO_MT_END, NULL }
170 static MonoMetaTable ImplMapSchema [] = {
171 { MONO_MT_UINT16, "MappingFlag" },
172 { MONO_MT_MF_IDX, "MemberForwarded" },
173 { MONO_MT_STRING_IDX, "ImportName" },
174 { MONO_MT_TABLE_IDX, "ImportScope:ModuleRef" },
175 { MONO_MT_END, NULL }
178 static MonoMetaTable InterfaceImplSchema [] = {
179 { MONO_MT_TABLE_IDX, "Class:TypeDef" },
180 { MONO_MT_TDOR_IDX, "Interface=TypeDefOrRef" },
181 { MONO_MT_END, NULL }
184 static MonoMetaTable ManifestResourceSchema [] = {
185 { MONO_MT_UINT32, "Offset" },
186 { MONO_MT_UINT32, "Flags" },
187 { MONO_MT_STRING_IDX, "Name" },
188 { MONO_MT_IMPL_IDX, "Implementation" },
189 { MONO_MT_END, NULL }
192 static MonoMetaTable MemberRefSchema [] = {
193 { MONO_MT_MRP_IDX, "Class" },
194 { MONO_MT_STRING_IDX, "Name" },
195 { MONO_MT_BLOB_IDX, "Signature" },
196 { MONO_MT_END, NULL }
199 static MonoMetaTable MethodSchema [] = {
200 { MONO_MT_UINT32, "RVA" },
201 { MONO_MT_UINT16, "ImplFlags#MethodImplAttributes" },
202 { MONO_MT_UINT16, "Flags#MethodAttribute" },
203 { MONO_MT_STRING_IDX, "Name" },
204 { MONO_MT_BLOB_IDX, "Signature" },
205 { MONO_MT_TABLE_IDX, "ParamList:Param" },
206 { MONO_MT_END, NULL }
209 static MonoMetaTable MethodImplSchema [] = {
210 { MONO_MT_TABLE_IDX, "Class:TypeDef" },
211 { MONO_MT_MDOR_IDX, "MethodBody" },
212 { MONO_MT_MDOR_IDX, "MethodDeclaration" },
213 { MONO_MT_END, NULL }
216 static MonoMetaTable MethodSemanticsSchema [] = {
217 { MONO_MT_UINT16, "MethodSemantic" },
218 { MONO_MT_TABLE_IDX, "Method:Method" },
219 { MONO_MT_HS_IDX, "Association" },
220 { MONO_MT_END, NULL }
223 static MonoMetaTable ModuleSchema [] = {
224 { MONO_MT_UINT16, "Generation" },
225 { MONO_MT_STRING_IDX, "Name" },
226 { MONO_MT_GUID_IDX, "MVID" },
227 { MONO_MT_GUID_IDX, "EncID" },
228 { MONO_MT_GUID_IDX, "EncBaseID" },
229 { MONO_MT_END, NULL }
232 static MonoMetaTable ModuleRefSchema [] = {
233 { MONO_MT_STRING_IDX, "Name" },
234 { MONO_MT_END, NULL }
237 static MonoMetaTable NestedClassSchema [] = {
238 { MONO_MT_TABLE_IDX, "NestedClass:TypeDef" },
239 { MONO_MT_TABLE_IDX, "EnclosingClass:TypeDef" },
240 { MONO_MT_END, NULL }
243 static MonoMetaTable ParamSchema [] = {
244 { MONO_MT_UINT16, "Flags" },
245 { MONO_MT_UINT16, "Sequence" },
246 { MONO_MT_STRING_IDX, "Name" },
247 { MONO_MT_END, NULL }
250 static MonoMetaTable PropertySchema [] = {
251 { MONO_MT_UINT16, "Flags" },
252 { MONO_MT_STRING_IDX, "Name" },
253 { MONO_MT_BLOB_IDX, "Type" },
254 { MONO_MT_END, NULL }
257 static MonoMetaTable PropertyMapSchema [] = {
258 { MONO_MT_TABLE_IDX, "Parent:TypeDef" },
259 { MONO_MT_TABLE_IDX, "PropertyList:Property" },
260 { MONO_MT_END, NULL }
263 static MonoMetaTable StandaloneSigSchema [] = {
264 { MONO_MT_BLOB_IDX, "Signature" },
265 { MONO_MT_END, NULL }
268 static MonoMetaTable TypeDefSchema [] = {
269 { MONO_MT_UINT32, "Flags" },
270 { MONO_MT_STRING_IDX, "Name" },
271 { MONO_MT_STRING_IDX, "Namespace" },
272 { MONO_MT_TDOR_IDX, "Extends" },
273 { MONO_MT_TABLE_IDX, "FieldList:Field" },
274 { MONO_MT_TABLE_IDX, "MethodList:Method" },
275 { MONO_MT_END, NULL }
278 static MonoMetaTable TypeRefSchema [] = {
279 { MONO_MT_RS_IDX, "ResolutionScope=ResolutionScope" },
280 { MONO_MT_STRING_IDX, "Name" },
281 { MONO_MT_STRING_IDX, "Namespace" },
282 { MONO_MT_END, NULL }
285 static MonoMetaTable TypeSpecSchema [] = {
286 { MONO_MT_BLOB_IDX, "Signature" },
287 { MONO_MT_END, NULL }
291 MonoMetaTable *table;
294 /* 0 */ { ModuleSchema, "Module" },
295 /* 1 */ { TypeRefSchema, "TypeRef" },
296 /* 2 */ { TypeDefSchema, "TypeDef" },
297 /* 3 */ { NULL, NULL },
298 /* 4 */ { FieldSchema, "Field" },
299 /* 5 */ { NULL, NULL },
300 /* 6 */ { MethodSchema, "Method" },
301 /* 7 */ { NULL, NULL },
302 /* 8 */ { ParamSchema, "Param" },
303 /* 9 */ { InterfaceImplSchema, "InterfaceImpl" },
304 /* A */ { MemberRefSchema, "MemberRef" },
305 /* B */ { ConstantSchema, "Constant" },
306 /* C */ { CustomAttributeSchema, "CustomAttribute" },
307 /* D */ { FieldMarshalSchema, "FieldMarshal" },
308 /* E */ { DeclSecuritySchema, "DeclSecurity" },
309 /* F */ { ClassLayoutSchema, "ClassLayout" },
310 /* 10 */ { FieldLayoutSchema, "FieldLayout" },
311 /* 11 */ { StandaloneSigSchema, "StandaloneSig" },
312 /* 12 */ { EventMapSchema, "EventMap" },
313 /* 13 */ { NULL, NULL },
314 /* 14 */ { EventSchema, "Event" },
315 /* 15 */ { PropertyMapSchema, "PropertyMap" },
316 /* 16 */ { NULL, NULL },
317 /* 17 */ { PropertySchema, "PropertyTable" },
318 /* 18 */ { MethodSemanticsSchema, "MethodSemantics" },
319 /* 19 */ { MethodImplSchema, "MethodImpl" },
320 /* 1A */ { ModuleRefSchema, "ModuleRef" },
321 /* 1B */ { TypeSpecSchema, "TypeSpec" },
322 /* 1C */ { ImplMapSchema, "ImplMap" },
323 /* 1D */ { FieldRVASchema, "FieldRVA" },
324 /* 1E */ { NULL, NULL },
325 /* 1F */ { NULL, NULL },
326 /* 20 */ { AssemblySchema, "Assembly" },
327 /* 21 */ { AssemblyProcessorSchema, "AssemblyProcessor" },
328 /* 22 */ { AssemblyOSSchema, "AssemblyOS" },
329 /* 23 */ { AssemblyRefSchema, "AssemblyRef" },
330 /* 24 */ { AssemblyRefProcessorSchema, "AssemblyRefProcessor" },
331 /* 25 */ { AssemblyRefOSSchema, "AssemblyRefOS" },
332 /* 26 */ { FileSchema, "File" },
333 /* 27 */ { ExportedTypeSchema, "ExportedType" },
334 /* 28 */ { ManifestResourceSchema, "ManifestResource" },
335 /* 29 */ { NestedClassSchema, "NestedClass" },
336 /* 2A */ { NULL, NULL },
337 /* 2B */ { NULL, NULL },
341 * mono_meta_table_name:
342 * @table: table index
344 * Returns the name for the @table index
347 mono_meta_table_name (int table)
349 if ((table < 0) || (table > 0x29))
352 return tables [table].name;
355 /* The guy who wrote the spec for this should not be allowed near a
358 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
359 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
360 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
361 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
362 inverse of this mapping.
365 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
366 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
368 /* Reference: Partition II - 23.2.6 */
370 compute_size (MonoMetadata *meta, MonoMetaTable *table, int tableindex, guint32 *result_bitfield)
372 guint32 bitfield = 0;
373 int size = 0, field_size;
377 for (i = 0; (code = table [i].code) != MONO_MT_END; i++){
380 field_size = 4; break;
383 field_size = 2; break;
386 field_size = 1; break;
388 case MONO_MT_BLOB_IDX:
389 field_size = meta->idx_blob_wide ? 4 : 2; break;
391 case MONO_MT_STRING_IDX:
392 field_size = meta->idx_string_wide ? 4 : 2; break;
394 case MONO_MT_GUID_IDX:
395 field_size = meta->idx_guid_wide ? 4 : 2; break;
397 case MONO_MT_TABLE_IDX:
398 /* Uhm, a table index can point to other tables besides the current one
399 * so, it's not correct to use the rowcount of the current table to
400 * get the size for this column - lupus
402 switch (tableindex) {
403 case MONO_TABLE_ASSEMBLYREFOS:
405 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
406 case MONO_TABLE_ASSEMBLYPROCESSOR:
408 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
409 case MONO_TABLE_CLASSLAYOUT:
411 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
412 case MONO_TABLE_EVENTMAP:
413 g_assert (i == 0 || i == 1);
414 field_size = i ? idx_size (MONO_TABLE_EVENT):
415 idx_size(MONO_TABLE_TYPEDEF);
417 case MONO_TABLE_EVENT:
419 field_size = MAX (idx_size (MONO_TABLE_TYPEDEF), idx_size(MONO_TABLE_TYPEREF));
420 field_size = MAX (field_size, idx_size(MONO_TABLE_TYPESPEC));
422 case MONO_TABLE_EXPORTEDTYPE:
424 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
425 case MONO_TABLE_FIELDLAYOUT:
427 field_size = idx_size (MONO_TABLE_FIELD); break;
428 case MONO_TABLE_FIELDRVA:
430 field_size = idx_size (MONO_TABLE_FIELD); break;
431 case MONO_TABLE_IMPLMAP:
433 field_size = idx_size (MONO_TABLE_MODULEREF); break;
434 case MONO_TABLE_INTERFACEIMPL:
436 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
437 case MONO_TABLE_METHOD:
439 field_size = idx_size (MONO_TABLE_PARAM); break;
440 case MONO_TABLE_METHODIMPL:
442 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
443 case MONO_TABLE_METHODSEMANTICS:
445 field_size = idx_size (MONO_TABLE_METHOD); break;
446 case MONO_TABLE_NESTEDCLASS:
447 g_assert (i == 0 || i == 1);
448 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
449 case MONO_TABLE_PROPERTYMAP:
450 g_assert (i == 0 || i == 1);
451 field_size = i ? idx_size (MONO_TABLE_PROPERTY):
452 idx_size(MONO_TABLE_TYPEDEF);
454 case MONO_TABLE_TYPEDEF:
455 g_assert (i == 4 || i == 5);
456 field_size = i == 4 ? idx_size (MONO_TABLE_FIELD):
457 idx_size(MONO_TABLE_METHOD);
460 g_assert_not_reached ();
462 if (field_size != idx_size (tableindex))
463 g_warning ("size changed (%d to %d)", idx_size (tableindex), field_size);
468 * HasConstant: ParamDef, FieldDef, Property
470 case MONO_MT_CONST_IDX:
471 n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
472 meta->tables [MONO_TABLE_FIELD].rows);
473 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
475 /* 2 bits to encode tag */
476 field_size = rtsize (n, 16-2);
480 * HasCustomAttribute: points to any table but
483 case MONO_MT_HASCAT_IDX:
485 * We believe that since the signature and
486 * permission are indexing the Blob heap,
487 * we should consider the blob size first
489 /* I'm not a believer - lupus
490 if (meta->idx_blob_wide){
495 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
496 meta->tables [MONO_TABLE_FIELD].rows);
497 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
498 n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
499 n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
500 n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
501 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
502 n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
503 /* Permission seems to be a blob heap pointer */
504 n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
505 n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
506 /* Signature seems to be a blob heap pointer */
507 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
508 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
509 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
510 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
511 n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
512 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
513 n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
515 /* 5 bits to encode */
516 field_size = rtsize (n, 16-5);
520 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
521 * MemberRef and String.
523 case MONO_MT_CAT_IDX:
524 /* String is a heap, if it is wide, we know the size */
526 if (meta->idx_string_wide){
531 n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
532 meta->tables [MONO_TABLE_TYPEDEF].rows);
533 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
534 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
536 /* 3 bits to encode */
537 field_size = rtsize (n, 16-3);
541 * HasDeclSecurity: Typedef, MethodDef, Assembly
543 case MONO_MT_HASDEC_IDX:
544 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
545 meta->tables [MONO_TABLE_METHOD].rows);
546 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
548 /* 2 bits to encode */
549 field_size = rtsize (n, 16-2);
553 * Implementation: File, AssemblyRef, ExportedType
555 case MONO_MT_IMPL_IDX:
556 n = MAX (meta->tables [MONO_TABLE_FILE].rows,
557 meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
558 n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
560 /* 2 bits to encode tag */
561 field_size = rtsize (n, 16-2);
565 * HasFieldMarshall: FieldDef, ParamDef
567 case MONO_MT_HFM_IDX:
568 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
569 meta->tables [MONO_TABLE_PARAM].rows);
571 /* 1 bit used to encode tag */
572 field_size = rtsize (n, 16-1);
576 * MemberForwarded: FieldDef, MethodDef
579 n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
580 meta->tables [MONO_TABLE_METHOD].rows);
582 /* 1 bit used to encode tag */
583 field_size = rtsize (n, 16-1);
587 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
589 * It is TypeDef, _TypeRef_, TypeSpec, instead.
591 case MONO_MT_TDOR_IDX:
592 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
593 meta->tables [MONO_TABLE_TYPEREF].rows);
594 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
596 /* 2 bits to encode */
597 field_size = rtsize (n, 16-2);
601 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
603 case MONO_MT_MRP_IDX:
604 n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
605 meta->tables [MONO_TABLE_TYPEREF].rows);
606 n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
607 n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
608 n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
609 n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
611 /* 3 bits to encode */
612 field_size = rtsize (n, 16 - 3);
615 case MONO_MT_MDOR_IDX:
618 * MethodDefOrRef: MethodDef, MemberRef
621 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
622 meta->tables [MONO_TABLE_MEMBERREF].rows);
624 /* 1 bit used to encode tag */
625 field_size = rtsize (n, 16-1);
629 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
632 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
633 meta->tables [MONO_TABLE_MODULEREF].rows);
634 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
635 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
637 /* 2 bits used to encode tag (ECMA spec claims 3) */
638 field_size = rtsize (n, 16 - 2);
643 * encode field size as follows (we just need to
650 bitfield |= (field_size-1) << shift;
653 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
656 *result_bitfield = (i << 24) | bitfield;
661 * mono_metadata_compute_table_bases:
662 * @meta: metadata context to compute table values
664 * Computes the table bases for the metadata structure.
665 * This is an internal function used by the image loader code.
668 mono_metadata_compute_table_bases (MonoMetadata *meta)
671 char *base = meta->tables_base;
673 for (i = 0; i < 64; i++){
674 if (meta->tables [i].rows == 0)
677 meta->tables [i].row_size = compute_size (
678 meta, tables [i].table, i,
679 &meta->tables [i].size_bitfield);
680 meta->tables [i].base = base;
681 base += meta->tables [i].rows * meta->tables [i].row_size;
686 * mono_metadata_locate:
687 * @meta: metadata context
688 * @table: table code.
689 * @idx: index of element to retrieve from @table.
691 * Returns a pointer to the @idx element in the metadata table
692 * whose code is @table.
695 mono_metadata_locate (MonoMetadata *meta, int table, int idx)
697 /* idx == 0 refers always to NULL */
698 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
700 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
704 mono_metadata_locate_token (MonoMetadata *meta, guint32 token)
706 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
710 * mono_metadata_get_table:
711 * @table: table to retrieve
713 * Returns the MonoMetaTable structure for table @table
716 mono_metadata_get_table (MonoMetaTableEnum table)
720 g_return_val_if_fail ((x > 0) && (x <= MONO_TABLE_LAST), NULL);
722 return tables [table].table;
726 * mono_metadata_string_heap:
727 * @meta: metadata context
728 * @index: index into the string heap.
730 * Returns: an in-memory pointer to the @index in the string heap.
733 mono_metadata_string_heap (MonoMetadata *meta, guint32 index)
735 g_return_val_if_fail (index < meta->heap_strings.size, "");
736 return meta->raw_metadata + meta->heap_strings.offset + index;
740 mono_metadata_user_string (MonoMetadata *meta, guint32 index)
742 g_return_val_if_fail (index < meta->heap_us.size, "");
743 return meta->raw_metadata + meta->heap_us.offset + index;
747 * mono_metadata_blob_heap:
748 * @meta: metadata context
749 * @index: index into the blob.
751 * Returns: an in-memory pointer to the @index in the Blob heap.
754 mono_metadata_blob_heap (MonoMetadata *meta, guint32 index)
756 g_return_val_if_fail (index < meta->heap_blob.size, "");
757 return meta->raw_metadata + meta->heap_blob.offset + index;
761 dword_align (const char *ptr)
763 return (const char *) (((guint32) (ptr + 3)) & ~3);
767 * mono_metadata_decode_row:
768 * @t: table to extract information from.
769 * @idx: index in table.
770 * @res: array of @res_size cols to store the results in
772 * This decompresses the metadata element @idx in table @t
773 * into the guint32 @res array that has res_size elements
776 mono_metadata_decode_row (MonoTableInfo *t, int idx, guint32 *res, int res_size)
778 guint32 bitfield = t->size_bitfield;
779 int i, count = mono_metadata_table_count (bitfield);
780 char *data = t->base + idx * t->row_size;
782 g_assert (res_size == count);
784 for (i = 0; i < count; i++){
785 int n = mono_metadata_table_size (bitfield, i);
789 res [i] = *data; break;
791 res [i] = read16 (data); break;
794 res [i] = read32 (data); break;
797 g_assert_not_reached ();
804 * mono_metadata_decode_row_col:
805 * @t: table to extract information from.
806 * @idx: index for row in table.
807 * @col: column in the row.
809 * This function returns the value of column @col from the @idx
810 * row in the table @t.
813 mono_metadata_decode_row_col (MonoTableInfo *t, int idx, guint col)
815 guint32 bitfield = t->size_bitfield;
817 register char *data = t->base + idx * t->row_size;
820 g_assert (col < mono_metadata_table_count (bitfield));
822 n = mono_metadata_table_size (bitfield, 0);
823 for (i = 0; i < col; ++i) {
825 n = mono_metadata_table_size (bitfield, i + 1);
831 return read16 (data);
833 return read32 (data);
835 g_assert_not_reached ();
840 * mono_metadata_decode_blob_size:
841 * @ptr: pointer to a blob object
842 * @rptr: the new position of the pointer
844 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
846 * Returns: the size of the blob object
849 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
851 const unsigned char *ptr = xptr;
854 if ((*ptr & 0x80) == 0){
855 size = ptr [0] & 0x7f;
857 } else if ((*ptr & 0x40) == 0){
858 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
861 size = ((ptr [0] & 0x1f) << 24) +
874 * mono_metadata_decode_value:
875 * @ptr: pointer to decode from
876 * @rptr: the new position of the pointer
878 * This routine decompresses 32-bit values as specified in the "Blob and
879 * Signature" section (22.2)
881 * Returns: the decoded value
884 mono_metadata_decode_value (const char *_ptr, const char **rptr)
886 const unsigned char *ptr = (unsigned char *) _ptr;
887 unsigned char b = *ptr;
890 if ((b & 0x80) == 0){
893 } else if ((b & 0x40) == 0){
894 len = ((b & 0x3f) << 8 | ptr [1]);
897 len = ((b & 0x1f) << 24) |
910 mono_metadata_parse_typedef_or_ref (MonoMetadata *m, const char *ptr, const char **rptr)
914 token = mono_metadata_decode_value (ptr, &ptr);
916 switch (token & 0x03) {
917 case 0: table = MONO_TABLE_TYPEDEF; break;
918 case 1: table = MONO_TABLE_TYPEREF; break;
919 case 2: table = MONO_TABLE_TYPESPEC; break;
920 default: g_error ("Unhandled encoding for typedef-or-ref coded index");
924 return (token >> 2) | table << 24;
928 mono_metadata_parse_custom_mod (MonoMetadata *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
931 if ((*ptr == MONO_TYPE_CMOD_OPT) ||
932 (*ptr == MONO_TYPE_CMOD_REQD)) {
935 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
936 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, &ptr);
943 mono_metadata_parse_array (MonoMetadata *m, const char *ptr, const char **rptr)
946 MonoArray *array = g_new0 (MonoArray, 1);
948 array->type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
949 array->rank = mono_metadata_decode_value (ptr, &ptr);
951 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
953 array->sizes = g_new0 (int, array->numsizes);
954 for (i = 0; i < array->numsizes; ++i)
955 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
957 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
958 if (array->numlobounds)
959 array->lobounds = g_new0 (int, array->numlobounds);
960 for (i = 0; i < array->numlobounds; ++i)
961 array->lobounds [i] = mono_metadata_decode_value (ptr, &ptr);
969 mono_metadata_free_array (MonoArray *array)
971 mono_metadata_free_type (array->type);
972 g_free (array->sizes);
973 g_free (array->lobounds);
978 * need to add common field and param attributes combinations:
981 * public static literal
984 * private static literal
988 /* data, attrs, type, nmods, byref, pinned */
989 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
990 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
991 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
992 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
993 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
994 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
995 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
996 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
997 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
998 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
999 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1000 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1001 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1002 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1003 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1004 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1005 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1006 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1007 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1008 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1009 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1010 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1011 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1012 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1013 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1014 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1015 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1016 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1017 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1018 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1019 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1020 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1021 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 0, 0},
1022 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 1, 0}
1025 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1027 static GHashTable *type_cache = NULL;
1030 * MonoTypes with modifies are never cached, so we never check or use that field.
1033 mono_type_hash (MonoType *type)
1035 return type->type | (type->byref << 8) | (type->attrs << 9);
1039 mono_type_equal (MonoType *a, MonoType *b)
1041 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1043 /* need other checks */
1048 mono_metadata_parse_type (MonoMetadata *m, MonoParseTypeMode mode, short opt_attrs, const char *ptr, const char **rptr)
1050 MonoType *type, *cached;
1054 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1055 for (i=0; i < NBUILTIN_TYPES (); ++i)
1056 g_hash_table_insert (type_cache, &builtin_types [i], &builtin_types [i]);
1060 case MONO_PARSE_MOD_TYPE:
1061 case MONO_PARSE_PARAM:
1062 case MONO_PARSE_RET:
1063 case MONO_PARSE_FIELD: {
1064 /* count the modifiers */
1065 const char *tmp_ptr = ptr;
1067 while (mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr))
1070 type = g_malloc0 (sizeof (MonoType) + (count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1071 type->num_mods = count;
1073 g_warning ("got more than 64 modifiers in type");
1074 /* save them this time */
1076 while (mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr))
1079 } /* fall through */
1081 case MONO_PARSE_LOCAL:
1082 case MONO_PARSE_TYPE:
1084 * Later we can avoid doing this allocation.
1086 type = g_new0 (MonoType, 1);
1089 g_assert_not_reached ();
1092 type->attrs = opt_attrs;
1093 if (mode == MONO_PARSE_LOCAL) {
1095 * check for pinned flag
1097 if (*ptr == MONO_TYPE_PINNED) {
1104 case MONO_TYPE_BYREF:
1105 if (mode == MONO_PARSE_FIELD)
1106 g_warning ("A field type cannot be byref");
1109 /* follow through */
1111 /*if (*ptr == MONO_TYPE_VOID && mode != MONO_PARSE_RET)
1112 g_error ("void not allowed in param");*/
1113 do_mono_metadata_parse_type (type, m, ptr, &ptr);
1118 if (mode != MONO_PARSE_PARAM && !type->num_mods && (cached = g_hash_table_lookup (type_cache, type))) {
1119 mono_metadata_free_type (type);
1126 MonoMethodSignature *
1127 mono_metadata_parse_method_signature (MonoMetadata *m, int def, const char *ptr, const char **rptr)
1129 MonoMethodSignature *method;
1130 int i, align, offset = 0;
1131 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1137 call_convention = *ptr & 0x0F;
1139 param_count = mono_metadata_decode_value (ptr, &ptr);
1141 method = g_malloc0 (sizeof (MonoMethodSignature) + (param_count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1142 method->param_count = param_count;
1143 method->hasthis = hasthis;
1144 method->explicit_this = explicit_this;
1145 method->call_convention = call_convention;
1146 method->ret = mono_metadata_parse_type (m, MONO_PARSE_RET, 0, ptr, &ptr);
1148 if (method->hasthis)
1149 offset += sizeof(gpointer);
1150 if (method->param_count) {
1153 method->sentinelpos = -1;
1155 for (i = 0; i < method->param_count; ++i) {
1156 if (*ptr == MONO_TYPE_SENTINEL) {
1157 if (method->call_convention != MONO_CALL_VARARG || def)
1158 g_error ("found sentinel for methoddef or no vararg method");
1159 method->sentinelpos = i;
1162 method->params [i] = mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, &ptr);
1163 size = mono_type_size (method->params [i], &align);
1164 offset += (offset % align);
1168 method->params_size = offset;
1173 * Add signature to a cache and increase ref count...
1179 mono_metadata_free_method_signature (MonoMethodSignature *method)
1182 mono_metadata_free_type (method->ret);
1183 for (i = 0; i < method->param_count; ++i)
1184 mono_metadata_free_type (method->params [i]);
1190 * do_mono_metadata_parse_type:
1191 * @type: MonoType to be filled in with the return value
1193 * Internal routine used to "fill" the contents of @type from an
1194 * allocated pointer. This is done this way to avoid doing too
1195 * many mini-allocations (particularly for the MonoFieldType which
1196 * most of the time is just a MonoType, but sometimes might be augmented).
1198 * This routine is used by mono_metadata_parse_type and
1199 * mono_metadata_parse_field_type
1201 * This extracts a Type as specified in Partition II (22.2.12)
1204 do_mono_metadata_parse_type (MonoType *type, MonoMetadata *m, const char *ptr, const char **rptr)
1206 type->type = mono_metadata_decode_value (ptr, &ptr);
1208 switch (type->type){
1209 case MONO_TYPE_VOID:
1210 case MONO_TYPE_BOOLEAN:
1211 case MONO_TYPE_CHAR:
1224 case MONO_TYPE_STRING:
1225 case MONO_TYPE_OBJECT:
1226 case MONO_TYPE_TYPEDBYREF:
1228 case MONO_TYPE_VALUETYPE:
1229 case MONO_TYPE_CLASS: {
1231 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
1232 type->data.klass = mono_class_get (m, token);
1235 case MONO_TYPE_SZARRAY:
1237 type->data.type = mono_metadata_parse_type (m, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1239 case MONO_TYPE_FNPTR:
1240 type->data.method = mono_metadata_parse_method_signature (m, 0, ptr, &ptr);
1242 case MONO_TYPE_ARRAY:
1243 type->data.array = mono_metadata_parse_array (m, ptr, &ptr);
1246 g_error ("type 0x%02x not handled in mono_metadata_parse_type", type->type);
1255 * mono_metadata_parse_type:
1256 * @m: metadata context to scan
1257 * @ptr: pointer to encoded Type stream.
1258 * @rptr: the new position in the stream after parsing the type
1260 * Returns: A MonoType structure that has the parsed information
1261 * from the type stored at @ptr in the metadata table @m.
1264 mono_metadata_parse_type (MonoMetadata *m, const char *ptr, const char **rptr)
1266 /* should probably be allocated in a memchunk */
1267 MonoType *type = g_new0(MonoType, 1);
1269 do_mono_metadata_parse_type (type, m, ptr, rptr);
1276 mono_metadata_free_type (MonoType *type)
1278 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
1280 switch (type->type){
1281 case MONO_TYPE_SZARRAY:
1283 mono_metadata_free_type (type->data.type);
1285 case MONO_TYPE_FNPTR:
1286 mono_metadata_free_method_signature (type->data.method);
1288 case MONO_TYPE_ARRAY:
1289 mono_metadata_free_array (type->data.array);
1296 hex_dump (const char *buffer, int base, int count)
1298 int show_header = 1;
1306 for (i = 0; i < count; i++){
1309 printf ("\n0x%08x: ", (unsigned char) base + i);
1311 printf ("%02x ", (unsigned char) (buffer [i]));
1317 * @mh: The Method header
1318 * @ptr: Points to the beginning of the Section Data (25.3)
1321 parse_section_data (MonoMethodHeader *mh, const unsigned char *ptr)
1323 unsigned char sect_data_flags;
1324 const unsigned char *sptr;
1326 guint32 sect_data_len;
1329 /* align on 32-bit boundary */
1330 /* FIXME: not 64-bit clean code */
1331 sptr = ptr = dword_align (ptr);
1332 sect_data_flags = *ptr;
1335 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
1337 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
1340 sect_data_len = ptr [0];
1344 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
1345 hex_dump (sptr, 0, sect_data_len+8);
1346 g_print ("\nheader: ");
1347 hex_dump (sptr-4, 0, 4);
1351 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
1352 const unsigned char *p = dword_align (ptr);
1354 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
1355 /* we could just store a pointer if we don't need to byteswap */
1356 mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
1357 for (i = 0; i < mh->num_clauses; ++i) {
1358 MonoExceptionClause *ec = &mh->clauses [i];
1360 /* we could memcpy and byteswap */
1361 ec->flags = read32 (p);
1363 ec->try_offset = read32 (p);
1365 ec->try_len = read32 (p);
1367 ec->handler_offset = read32 (p);
1369 ec->handler_len = read32 (p);
1371 ec->token_or_filter = read32 (p);
1374 ec->flags = read16 (p);
1376 ec->try_offset = read16 (p);
1380 ec->handler_offset = read16 (p);
1382 ec->handler_len = *p;
1384 ec->token_or_filter = read32 (p);
1387 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
1390 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
1391 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
1398 mono_metadata_parse_mh (MonoMetadata *m, const char *ptr)
1400 MonoMethodHeader *mh;
1401 unsigned char flags = *(unsigned char *) ptr;
1402 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
1404 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
1405 const unsigned char *code;
1408 g_return_val_if_fail (ptr != NULL, NULL);
1411 case METHOD_HEADER_TINY_FORMAT:
1412 mh = g_new0 (MonoMethodHeader, 1);
1415 local_var_sig_tok = 0;
1416 mh->code_size = flags >> 2;
1419 case METHOD_HEADER_TINY_FORMAT1:
1420 mh = g_new0 (MonoMethodHeader, 1);
1423 local_var_sig_tok = 0;
1426 // The spec claims 3 bits, but the Beta2 is
1429 mh->code_size = flags >> 2;
1432 case METHOD_HEADER_FAT_FORMAT:
1433 fat_flags = read16 (ptr);
1435 hsize = (fat_flags >> 12) & 0xf;
1436 max_stack = read16 (ptr);
1438 code_size = read32 (ptr);
1440 local_var_sig_tok = read32 (ptr);
1443 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
1450 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
1454 * There are more sections
1456 ptr = code + code_size;
1464 if (local_var_sig_tok) {
1465 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
1467 guint32 cols [MONO_STAND_ALONG_SIGNATURE_SIZE];
1468 int len=0, i, bsize;
1471 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
1472 ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONG_SIGNATURE]);
1473 bsize = mono_metadata_decode_blob_size (ptr, &ptr);
1475 g_warning ("wrong signature for locals blob");
1477 len = mono_metadata_decode_value (ptr, &ptr);
1478 mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1479 mh->num_locals = len;
1480 for (i = 0; i < len; ++i) {
1483 mh->locals [i] = mono_metadata_parse_type (m, MONO_PARSE_LOCAL, 0, ptr, &ptr);
1485 val = mono_type_size (mh->locals [i], &align);
1486 offset += (offset % align);
1489 mh->locals_size = offset;
1491 mh = g_new0 (MonoMethodHeader, 1);
1494 mh->code_size = code_size;
1495 mh->max_stack = max_stack;
1496 mh->init_locals = init_locals;
1497 if (fat_flags & METHOD_HEADER_MORE_SECTS)
1498 parse_section_data (mh, (const unsigned char*)ptr);
1503 mono_metadata_free_mh (MonoMethodHeader *mh)
1506 for (i = 0; i < mh->num_locals; ++i)
1507 mono_metadata_free_type (mh->locals[i]);
1508 g_free (mh->clauses);
1513 * mono_metadata_parse_field_type:
1514 * @m: metadata context to extract information from
1515 * @ptr: pointer to the field signature
1517 * Parses the field signature, and returns the type information for it.
1519 * Returns: The MonoType that was extracted from @ptr.
1522 mono_metadata_parse_field_type (MonoMetadata *m, short field_flags, const char *ptr, const char **rptr)
1524 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
1528 mono_metadata_parse_param (MonoMetadata *m, const char *ptr, const char **rptr)
1530 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
1534 * mono_metadata_token_from_dor:
1535 * @dor_token: A TypeDefOrRef coded index
1537 * dor_token is a TypeDefOrRef coded index: it contains either
1538 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
1539 * bits contain an index into the table.
1541 * Returns: an expanded token
1544 mono_metadata_token_from_dor (guint32 dor_index)
1548 table = dor_index & 0x03;
1549 idx = dor_index >> 2;
1552 case 0: /* TypeDef */
1553 return MONO_TOKEN_TYPE_DEF | idx;
1555 case 1: /* TypeRef */
1556 return MONO_TOKEN_TYPE_REF | idx;
1558 case 2: /* TypeSpec */
1559 return MONO_TOKEN_TYPE_SPEC | idx;
1562 g_assert_not_reached ();
1569 * We use this to pass context information to the row locator
1572 int idx; /* The index that we are trying to locate */
1573 int col_idx; /* The index in the row where idx may be stored */
1574 MonoTableInfo *t; /* pointer to the table */
1578 #define CSIZE(x) (sizeof (x) / 4)
1581 * How the row locator works.
1586 * ___|___------> _______
1589 * A column in the rows of table A references an index in table B.
1590 * For example A may be the TYPEDEF table and B the METHODDEF table.
1592 * Given an index in table B we want to get the row in table A
1593 * where the column n references our index in B.
1595 * In the locator_t structure:
1597 * col_idx is the column number
1598 * index is the index in table B
1599 * result will be the index in table A
1602 * Table A Table B column (in table A)
1603 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
1604 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
1605 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
1606 * METHODSEM PROPERTY ASSOCIATION (encoded index)
1608 * Note that we still don't support encoded indexes.
1612 typedef_locator (const void *a, const void *b)
1614 locator_t *loc = (locator_t *) a;
1615 char *bb = (char *) b;
1616 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
1617 guint32 col, col_next;
1619 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
1625 * Need to check that the next row is valid.
1627 if (typedef_index + 1 < loc->t->rows) {
1628 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
1629 if (loc->idx >= col_next)
1632 if (col == col_next)
1636 loc->result = typedef_index;
1642 mono_metadata_typedef_from_field (MonoMetadata *meta, guint32 index)
1644 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
1647 loc.idx = mono_metadata_token_index (index);
1648 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
1651 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
1652 g_assert_not_reached ();
1654 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
1655 return loc.result + 1;
1659 mono_metadata_typedef_from_method (MonoMetadata *meta, guint32 index)
1661 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
1664 loc.idx = mono_metadata_token_index (index);
1665 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
1668 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
1669 g_assert_not_reached ();
1671 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
1672 return loc.result + 1;
1676 #define __alignof__(a) sizeof(a)
1681 * @t: the type to return the size of
1683 * Returns: the number of bytes required to hold an instance of this
1687 mono_type_size (MonoType *t, gint *align)
1694 *align = __alignof__(gpointer);
1695 return sizeof (gpointer);
1699 case MONO_TYPE_VOID:
1702 case MONO_TYPE_BOOLEAN:
1703 *align = __alignof__(char);
1704 return sizeof (char);
1706 case MONO_TYPE_CHAR:
1707 *align = __alignof__(short);
1708 return sizeof (short);
1712 *align = __alignof__(char);
1717 *align = __alignof__(gint16);
1722 *align = __alignof__(gint32);
1725 *align = __alignof__(float);
1730 *align = __alignof__(gint64);
1732 *align = __alignof__(double);
1737 *align = __alignof__(gpointer);
1738 return sizeof (gpointer);
1740 case MONO_TYPE_STRING:
1741 *align = __alignof__(gpointer);
1742 return sizeof (gpointer);
1744 case MONO_TYPE_OBJECT:
1745 *align = __alignof__(gpointer);
1746 return sizeof (gpointer);
1748 case MONO_TYPE_VALUETYPE: {
1751 size = mono_class_value_size (t->data.klass, align);
1754 case MONO_TYPE_CLASS:
1755 case MONO_TYPE_SZARRAY:
1757 case MONO_TYPE_FNPTR:
1758 case MONO_TYPE_ARRAY:
1759 case MONO_TYPE_TYPEDBYREF: /* we may want to use a struct {MonoType* type, void *data } instead ...*/
1760 *align = __alignof__(gpointer);
1761 return sizeof (gpointer);
1763 g_error ("type 0x%02x unknown", t->type);
1769 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
1771 if (t1->type != t2->type ||
1772 t1->byref != t2->byref)
1776 case MONO_TYPE_VOID:
1777 case MONO_TYPE_BOOLEAN:
1778 case MONO_TYPE_CHAR:
1789 case MONO_TYPE_STRING:
1792 case MONO_TYPE_OBJECT:
1794 case MONO_TYPE_VALUETYPE:
1795 case MONO_TYPE_CLASS:
1796 case MONO_TYPE_SZARRAY:
1797 return t1->data.klass == t2->data.klass;
1799 g_error ("implement type compare for %0x!", t1->type);
1807 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
1811 if (sig1->hasthis != sig2->hasthis ||
1812 sig1->param_count != sig2->param_count)
1815 for (i = 0; i < sig1->param_count; i++) {
1816 MonoType *p1 = sig1->params[i];
1817 MonoType *p2 = sig2->params[i];
1819 if (p1->attrs != p2->attrs)
1822 if (!mono_metadata_type_equal (p1, p2))