2005-01-24 Ben Maurer <bmaurer@ximian.com>
[mono-project.git] / mono / metadata / metadata.c
blob0fc9d3da94af5945b6ac5d28d006b25791339bd7
1 /*
2 * metadata.c: Routines for accessing the metadata
4 * Authors:
5 * Miguel de Icaza (miguel@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * (C) 2001-2002 Ximian, Inc.
9 */
11 #include <config.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <glib.h>
16 #include "metadata.h"
17 #include "tabledefs.h"
18 #include "mono-endian.h"
19 #include "cil-coff.h"
20 #include "tokentype.h"
21 #include "metadata-internals.h"
22 #include "class-internals.h"
23 #include "private.h"
24 #include "class.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" },
57 { MONO_MT_END, NULL }
60 const static MonoMetaTable AssemblyOSSchema [] = {
61 { MONO_MT_UINT32, "OSPlatformID" },
62 { MONO_MT_UINT32, "OSMajor" },
63 { MONO_MT_UINT32, "OSMinor" },
64 { MONO_MT_END, NULL }
67 const static MonoMetaTable AssemblyProcessorSchema [] = {
68 { MONO_MT_UINT32, "Processor" },
69 { MONO_MT_END, NULL }
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" },
82 { MONO_MT_END, NULL }
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" },
90 { MONO_MT_END, NULL }
93 const static MonoMetaTable AssemblyRefProcessorSchema [] = {
94 { MONO_MT_UINT32, "Processor" },
95 { MONO_MT_TABLE_IDX, "AssemblyRef:AssemblyRef" },
96 { MONO_MT_END, NULL }
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;
326 const char *name;
327 } tables [] = {
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
381 const char *
382 mono_meta_table_name (int table)
384 if ((table < 0) || (table > 0x2c))
385 return "";
387 return tables [table].name;
390 /* The guy who wrote the spec for this should not be allowed near a
391 * computer again.
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;
421 int i, n, code;
422 int shift = 0;
423 const MonoMetaTable *table = tables [tableindex].table;
425 for (i = 0; (code = table [i].code) != MONO_MT_END; i++){
426 switch (code){
427 case MONO_MT_UINT32:
428 field_size = 4; break;
430 case MONO_MT_UINT16:
431 field_size = 2; break;
433 case MONO_MT_UINT8:
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:
452 g_assert (i == 3);
453 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
454 case MONO_TABLE_ASSEMBLYPROCESSOR:
455 g_assert (i == 1);
456 field_size = idx_size (MONO_TABLE_ASSEMBLYREF); break;
457 case MONO_TABLE_CLASSLAYOUT:
458 g_assert (i == 2);
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);
464 break;
465 case MONO_TABLE_EVENT:
466 g_assert (i == 2);
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));
469 break;
470 case MONO_TABLE_EXPORTEDTYPE:
471 g_assert (i == 1);
472 /* the index is in another metadata file, so it must be 4 */
473 field_size = 4; break;
474 case MONO_TABLE_FIELDLAYOUT:
475 g_assert (i == 1);
476 field_size = idx_size (MONO_TABLE_FIELD); break;
477 case MONO_TABLE_FIELDRVA:
478 g_assert (i == 1);
479 field_size = idx_size (MONO_TABLE_FIELD); break;
480 case MONO_TABLE_IMPLMAP:
481 g_assert (i == 3);
482 field_size = idx_size (MONO_TABLE_MODULEREF); break;
483 case MONO_TABLE_INTERFACEIMPL:
484 g_assert (i == 0);
485 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
486 case MONO_TABLE_METHOD:
487 g_assert (i == 5);
488 field_size = idx_size (MONO_TABLE_PARAM); break;
489 case MONO_TABLE_METHODIMPL:
490 g_assert (i == 0);
491 field_size = idx_size (MONO_TABLE_TYPEDEF); break;
492 case MONO_TABLE_METHODSEMANTICS:
493 g_assert (i == 1);
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);
502 break;
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);
509 if (i == 2)
510 field_size = MAX (idx_size (MONO_TABLE_METHOD), idx_size (MONO_TABLE_TYPEDEF));
511 else if (i == 4)
512 field_size = idx_size (MONO_TABLE_TYPEDEF);
513 else if (i == 5)
514 field_size = idx_size (MONO_TABLE_TYPEDEF);
515 break;
517 case MONO_TABLE_GENERICPARAMCONSTRAINT:
518 g_assert (i == 0);
519 field_size = idx_size (MONO_TABLE_GENERICPARAM);
520 break;
522 default:
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);
528 break;
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);
540 break;
543 * HasCustomAttribute: points to any table but
544 * itself.
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){
554 field_size = 4;
555 break;
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);
580 break;
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 */
588 /* See above, nope.
589 if (meta->idx_string_wide){
590 field_size = 4;
591 break;
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);
601 break;
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);
613 break;
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);
625 break;
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);
636 break;
639 * MemberForwarded: FieldDef, MethodDef
641 case MONO_MT_MF_IDX:
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);
647 break;
650 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
651 * LAMESPEC
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);
661 break;
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);
676 break;
679 * MethodDefOrRef: MethodDef, MemberRef
681 case MONO_MT_MDOR_IDX:
682 n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
683 meta->tables [MONO_TABLE_MEMBERREF].rows);
685 /* 1 bit used to encode tag */
686 field_size = rtsize (n, 16-1);
687 break;
690 * HasSemantics: Property, Event
692 case MONO_MT_HS_IDX:
693 n = MAX (meta->tables [MONO_TABLE_PROPERTY].rows,
694 meta->tables [MONO_TABLE_EVENT].rows);
696 /* 1 bit used to encode tag */
697 field_size = rtsize (n, 16-1);
698 break;
701 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
703 case MONO_MT_RS_IDX:
704 n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
705 meta->tables [MONO_TABLE_MODULEREF].rows);
706 n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
707 n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
709 /* 2 bits used to encode tag (ECMA spec claims 3) */
710 field_size = rtsize (n, 16 - 2);
711 break;
715 * encode field size as follows (we just need to
716 * distinguish them).
718 * 4 -> 3
719 * 2 -> 1
720 * 1 -> 0
722 bitfield |= (field_size-1) << shift;
723 shift += 2;
724 size += field_size;
725 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
728 *result_bitfield = (i << 24) | bitfield;
729 return size;
733 * mono_metadata_compute_table_bases:
734 * @meta: metadata context to compute table values
736 * Computes the table bases for the metadata structure.
737 * This is an internal function used by the image loader code.
739 void
740 mono_metadata_compute_table_bases (MonoImage *meta)
742 int i;
743 const char *base = meta->tables_base;
745 for (i = 0; i < MONO_TABLE_NUM; i++){
746 if (meta->tables [i].rows == 0)
747 continue;
749 meta->tables [i].row_size = mono_metadata_compute_size (
750 meta, i, &meta->tables [i].size_bitfield);
751 meta->tables [i].base = base;
752 base += meta->tables [i].rows * meta->tables [i].row_size;
757 * mono_metadata_locate:
758 * @meta: metadata context
759 * @table: table code.
760 * @idx: index of element to retrieve from @table.
762 * Returns: a pointer to the @idx element in the metadata table
763 * whose code is @table.
765 const char *
766 mono_metadata_locate (MonoImage *meta, int table, int idx)
768 /* idx == 0 refers always to NULL */
769 g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, "");
771 return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
775 * mono_metadata_locate_token:
776 * @meta: metadata context
777 * @token: metadata token
779 * Returns: a pointer to the data in the metadata represented by the
780 * token #token.
782 const char *
783 mono_metadata_locate_token (MonoImage *meta, guint32 token)
785 return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
789 * mono_metadata_get_table:
790 * @table: table to retrieve
792 * Returns: the MonoMetaTable structure for table @table
794 const MonoMetaTable *
795 mono_metadata_get_table (MonoMetaTableEnum table)
797 int x = (int) table;
799 g_return_val_if_fail ((x > 0) && (x <= MONO_TABLE_LAST), NULL);
801 return tables [table].table;
805 * mono_metadata_string_heap:
806 * @meta: metadata context
807 * @index: index into the string heap.
809 * Returns: an in-memory pointer to the @index in the string heap.
811 const char *
812 mono_metadata_string_heap (MonoImage *meta, guint32 index)
814 g_return_val_if_fail (index < meta->heap_strings.size, "");
815 return meta->heap_strings.data + index;
819 * mono_metadata_user_string:
820 * @meta: metadata context
821 * @index: index into the user string heap.
823 * Returns: an in-memory pointer to the @index in the user string heap ("#US").
825 const char *
826 mono_metadata_user_string (MonoImage *meta, guint32 index)
828 g_return_val_if_fail (index < meta->heap_us.size, "");
829 return meta->heap_us.data + index;
833 * mono_metadata_blob_heap:
834 * @meta: metadata context
835 * @index: index into the blob.
837 * Returns: an in-memory pointer to the @index in the Blob heap.
839 const char *
840 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
842 g_return_val_if_fail (index < meta->heap_blob.size, "");
843 return meta->heap_blob.data + index;
847 * mono_metadata_guid_heap:
848 * @meta: metadata context
849 * @index: index into the guid heap.
851 * Returns: an in-memory pointer to the @index in the guid heap.
853 const char *
854 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
856 --index;
857 index *= 16; /* adjust for guid size and 1-based index */
858 g_return_val_if_fail (index < meta->heap_guid.size, "");
859 return meta->heap_guid.data + index;
862 static const char *
863 dword_align (const char *ptr)
865 #if SIZEOF_VOID_P == 8
866 return (const char *) (((guint64) (ptr + 3)) & ~3);
867 #else
868 return (const char *) (((guint32) (ptr + 3)) & ~3);
869 #endif
873 * mono_metadata_decode_row:
874 * @t: table to extract information from.
875 * @idx: index in table.
876 * @res: array of @res_size cols to store the results in
878 * This decompresses the metadata element @idx in table @t
879 * into the guint32 @res array that has res_size elements
881 void
882 mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res_size)
884 guint32 bitfield = t->size_bitfield;
885 int i, count = mono_metadata_table_count (bitfield);
886 const char *data = t->base + idx * t->row_size;
888 g_assert (res_size == count);
890 for (i = 0; i < count; i++){
891 int n = mono_metadata_table_size (bitfield, i);
893 switch (n){
894 case 1:
895 res [i] = *data; break;
896 case 2:
897 res [i] = read16 (data); break;
899 case 4:
900 res [i] = read32 (data); break;
902 default:
903 g_assert_not_reached ();
905 data += n;
910 * mono_metadata_decode_row_col:
911 * @t: table to extract information from.
912 * @idx: index for row in table.
913 * @col: column in the row.
915 * This function returns the value of column @col from the @idx
916 * row in the table @t.
918 guint32
919 mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col)
921 guint32 bitfield = t->size_bitfield;
922 int i;
923 register const char *data = t->base + idx * t->row_size;
924 register int n;
926 g_assert (col < mono_metadata_table_count (bitfield));
928 n = mono_metadata_table_size (bitfield, 0);
929 for (i = 0; i < col; ++i) {
930 data += n;
931 n = mono_metadata_table_size (bitfield, i + 1);
933 switch (n){
934 case 1:
935 return *data;
936 case 2:
937 return read16 (data);
938 case 4:
939 return read32 (data);
940 default:
941 g_assert_not_reached ();
943 return 0;
946 * mono_metadata_decode_blob_size:
947 * @ptr: pointer to a blob object
948 * @rptr: the new position of the pointer
950 * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
952 * Returns: the size of the blob object
954 guint32
955 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
957 const unsigned char *ptr = (const unsigned char *)xptr;
958 guint32 size;
960 if ((*ptr & 0x80) == 0){
961 size = ptr [0] & 0x7f;
962 ptr++;
963 } else if ((*ptr & 0x40) == 0){
964 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
965 ptr += 2;
966 } else {
967 size = ((ptr [0] & 0x1f) << 24) +
968 (ptr [1] << 16) +
969 (ptr [2] << 8) +
970 ptr [3];
971 ptr += 4;
973 if (rptr)
974 *rptr = ptr;
975 return size;
980 * mono_metadata_decode_value:
981 * @ptr: pointer to decode from
982 * @rptr: the new position of the pointer
984 * This routine decompresses 32-bit values as specified in the "Blob and
985 * Signature" section (22.2)
987 * Returns: the decoded value
989 guint32
990 mono_metadata_decode_value (const char *_ptr, const char **rptr)
992 const unsigned char *ptr = (const unsigned char *) _ptr;
993 unsigned char b = *ptr;
994 guint32 len;
996 if ((b & 0x80) == 0){
997 len = b;
998 ++ptr;
999 } else if ((b & 0x40) == 0){
1000 len = ((b & 0x3f) << 8 | ptr [1]);
1001 ptr += 2;
1002 } else {
1003 len = ((b & 0x1f) << 24) |
1004 (ptr [1] << 16) |
1005 (ptr [2] << 8) |
1006 ptr [3];
1007 ptr += 4;
1009 if (rptr)
1010 *rptr = ptr;
1012 return len;
1016 * mono_metadata_parse_typedef_or_ref:
1017 * @m: a metadata context.
1018 * @ptr: a pointer to an encoded TypedefOrRef in @m
1019 * @rptr: pointer updated to match the end of the decoded stream
1021 * Returns: a token valid in the @m metadata decoded from
1022 * the compressed representation.
1024 guint32
1025 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1027 guint32 token;
1028 token = mono_metadata_decode_value (ptr, &ptr);
1029 if (rptr)
1030 *rptr = ptr;
1031 return mono_metadata_token_from_dor (token);
1035 * mono_metadata_parse_custom_mod:
1036 * @m: a metadata context.
1037 * @dest: storage where the info about the custom modifier is stored (may be NULL)
1038 * @ptr: a pointer to (possibly) the start of a custom modifier list
1039 * @rptr: pointer updated to match the end of the decoded stream
1041 * Checks if @ptr points to a type custom modifier compressed representation.
1043 * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1046 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1048 MonoCustomMod local;
1049 if ((*ptr == MONO_TYPE_CMOD_OPT) ||
1050 (*ptr == MONO_TYPE_CMOD_REQD)) {
1051 if (!dest)
1052 dest = &local;
1053 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1054 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1055 return TRUE;
1057 return FALSE;
1061 * mono_metadata_parse_array:
1062 * @m: a metadata context.
1063 * @ptr: a pointer to an encoded array description.
1064 * @rptr: pointer updated to match the end of the decoded stream
1066 * Decodes the compressed array description found in the metadata @m at @ptr.
1068 * Returns: a #MonoArrayType structure describing the array type
1069 * and dimensions.
1071 MonoArrayType *
1072 mono_metadata_parse_array_full (MonoImage *m, MonoGenericContext *generic_context,
1073 const char *ptr, const char **rptr)
1075 int i;
1076 MonoArrayType *array = g_new0 (MonoArrayType, 1);
1077 MonoType *etype;
1079 etype = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_TYPE, 0, ptr, &ptr);
1080 array->eklass = mono_class_from_mono_type (etype);
1081 array->rank = mono_metadata_decode_value (ptr, &ptr);
1083 array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1084 if (array->numsizes)
1085 array->sizes = g_new0 (int, array->numsizes);
1086 for (i = 0; i < array->numsizes; ++i)
1087 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1089 array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1090 if (array->numlobounds)
1091 array->lobounds = g_new0 (int, array->numlobounds);
1092 for (i = 0; i < array->numlobounds; ++i)
1093 array->lobounds [i] = mono_metadata_decode_value (ptr, &ptr);
1095 if (rptr)
1096 *rptr = ptr;
1097 return array;
1100 MonoArrayType *
1101 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1103 return mono_metadata_parse_array_full (m, NULL, ptr, rptr);
1107 * mono_metadata_free_array:
1108 * @array: array description
1110 * Frees the array description returned from mono_metadata_parse_array().
1112 void
1113 mono_metadata_free_array (MonoArrayType *array)
1115 g_free (array->sizes);
1116 g_free (array->lobounds);
1117 g_free (array);
1121 * need to add common field and param attributes combinations:
1122 * [out] param
1123 * public static
1124 * public static literal
1125 * private
1126 * private static
1127 * private static literal
1129 static const MonoType
1130 builtin_types[] = {
1131 /* data, attrs, type, nmods, byref, pinned */
1132 {{NULL}, 0, MONO_TYPE_VOID, 0, 0, 0},
1133 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 0, 0},
1134 {{NULL}, 0, MONO_TYPE_BOOLEAN, 0, 1, 0},
1135 {{NULL}, 0, MONO_TYPE_CHAR, 0, 0, 0},
1136 {{NULL}, 0, MONO_TYPE_CHAR, 0, 1, 0},
1137 {{NULL}, 0, MONO_TYPE_I1, 0, 0, 0},
1138 {{NULL}, 0, MONO_TYPE_I1, 0, 1, 0},
1139 {{NULL}, 0, MONO_TYPE_U1, 0, 0, 0},
1140 {{NULL}, 0, MONO_TYPE_U1, 0, 1, 0},
1141 {{NULL}, 0, MONO_TYPE_I2, 0, 0, 0},
1142 {{NULL}, 0, MONO_TYPE_I2, 0, 1, 0},
1143 {{NULL}, 0, MONO_TYPE_U2, 0, 0, 0},
1144 {{NULL}, 0, MONO_TYPE_U2, 0, 1, 0},
1145 {{NULL}, 0, MONO_TYPE_I4, 0, 0, 0},
1146 {{NULL}, 0, MONO_TYPE_I4, 0, 1, 0},
1147 {{NULL}, 0, MONO_TYPE_U4, 0, 0, 0},
1148 {{NULL}, 0, MONO_TYPE_U4, 0, 1, 0},
1149 {{NULL}, 0, MONO_TYPE_I8, 0, 0, 0},
1150 {{NULL}, 0, MONO_TYPE_I8, 0, 1, 0},
1151 {{NULL}, 0, MONO_TYPE_U8, 0, 0, 0},
1152 {{NULL}, 0, MONO_TYPE_U8, 0, 1, 0},
1153 {{NULL}, 0, MONO_TYPE_R4, 0, 0, 0},
1154 {{NULL}, 0, MONO_TYPE_R4, 0, 1, 0},
1155 {{NULL}, 0, MONO_TYPE_R8, 0, 0, 0},
1156 {{NULL}, 0, MONO_TYPE_R8, 0, 1, 0},
1157 {{NULL}, 0, MONO_TYPE_STRING, 0, 0, 0},
1158 {{NULL}, 0, MONO_TYPE_STRING, 0, 1, 0},
1159 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 0, 0},
1160 {{NULL}, 0, MONO_TYPE_OBJECT, 0, 1, 0},
1161 {{NULL}, 0, MONO_TYPE_TYPEDBYREF, 0, 0, 0},
1162 {{NULL}, 0, MONO_TYPE_I, 0, 0, 0},
1163 {{NULL}, 0, MONO_TYPE_I, 0, 1, 0},
1164 {{NULL}, 0, MONO_TYPE_U, 0, 0, 0},
1165 {{NULL}, 0, MONO_TYPE_U, 0, 1, 0},
1168 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1170 static GHashTable *type_cache = NULL;
1171 static GHashTable *generic_inst_cache = NULL;
1172 static GHashTable *generic_class_cache = NULL;
1173 static int next_generic_inst_id = 0;
1176 * MonoTypes with modifies are never cached, so we never check or use that field.
1178 static guint
1179 mono_type_hash (gconstpointer data)
1181 const MonoType *type = (const MonoType *) data;
1182 return type->type | (type->byref << 8) | (type->attrs << 9);
1185 static gint
1186 mono_type_equal (gconstpointer ka, gconstpointer kb)
1188 const MonoType *a = (const MonoType *) ka;
1189 const MonoType *b = (const MonoType *) kb;
1191 if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1192 return 0;
1193 /* need other checks */
1194 return 1;
1197 static guint
1198 mono_generic_inst_hash (gconstpointer data)
1200 const MonoGenericInst *ginst = (const MonoGenericInst *) data;
1201 return ginst->type_argc | (ginst->is_open << 8);
1204 static gboolean
1205 mono_generic_inst_equal (gconstpointer ka, gconstpointer kb)
1207 const MonoGenericInst *a = (const MonoGenericInst *) ka;
1208 const MonoGenericInst *b = (const MonoGenericInst *) kb;
1209 int i;
1211 if ((a->is_open != b->is_open) || (a->type_argc != b->type_argc) || (a->is_reference != b->is_reference))
1212 return FALSE;
1213 for (i = 0; i < a->type_argc; ++i) {
1214 if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], FALSE))
1215 return FALSE;
1217 return TRUE;
1220 static guint
1221 mono_generic_class_hash (gconstpointer data)
1223 const MonoGenericClass *gclass = (const MonoGenericClass *) data;
1224 return mono_metadata_type_hash (&gclass->container_class->byval_arg);
1227 static gboolean
1228 mono_generic_class_equal (gconstpointer ka, gconstpointer kb)
1230 const MonoGenericClass *a = (const MonoGenericClass *) ka;
1231 const MonoGenericClass *b = (const MonoGenericClass *) kb;
1233 return _mono_metadata_generic_class_equal (a, b, FALSE);
1237 * mono_metadata_init:
1239 * Initialize the global variables of this module.
1241 void
1242 mono_metadata_init (void)
1244 int i;
1246 type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1247 generic_inst_cache = g_hash_table_new (mono_generic_inst_hash, mono_generic_inst_equal);
1248 generic_class_cache = g_hash_table_new (mono_generic_class_hash, mono_generic_class_equal);
1250 for (i = 0; i < NBUILTIN_TYPES (); ++i)
1251 g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
1255 * mono_metadata_parse_type:
1256 * @m: metadata context
1257 * @mode: king of type that may be found at @ptr
1258 * @opt_attrs: optional attributes to store in the returned type
1259 * @ptr: pointer to the type representation
1260 * @rptr: pointer updated to match the end of the decoded stream
1262 * Decode a compressed type description found at @ptr in @m.
1263 * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1264 * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1265 * This function can be used to decode type descriptions in method signatures,
1266 * field signatures, locals signatures etc.
1268 * To parse a generic type, `generic_container' points to the current class'es
1269 * (the `generic_container' field in the MonoClass) or the current generic method's
1270 * (the `generic_container' field in the MonoMethodNormal) generic container.
1271 * When we encounter any MONO_TYPE_VAR or MONO_TYPE_MVAR's, they're looked up in
1272 * this MonoGenericContainer.
1274 * Returns: a #MonoType structure representing the decoded type.
1276 MonoType*
1277 mono_metadata_parse_type_full (MonoImage *m, MonoGenericContext *generic_context, MonoParseTypeMode mode,
1278 short opt_attrs, const char *ptr, const char **rptr)
1280 MonoType *type, *cached;
1281 MonoType stype;
1282 gboolean byref = FALSE;
1283 gboolean pinned = FALSE;
1284 const char *tmp_ptr;
1285 int count = 0;
1286 gboolean found;
1289 * According to the spec, custom modifiers should come before the byref
1290 * flag, but the IL produced by ilasm from the following signature:
1291 * object modopt(...) &
1292 * starts with a byref flag, followed by the modifiers. (bug #49802)
1293 * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1294 * it would be better to treat byref as real type constructor instead of
1295 * a modifier...
1296 * Also, pinned should come before anything else, but some MSV++ produced
1297 * assemblies violate this (#bug 61990).
1300 /* Count the modifiers first */
1301 tmp_ptr = ptr;
1302 found = TRUE;
1303 while (found) {
1304 switch (*tmp_ptr) {
1305 case MONO_TYPE_PINNED:
1306 case MONO_TYPE_BYREF:
1307 ++tmp_ptr;
1308 break;
1309 case MONO_TYPE_CMOD_REQD:
1310 case MONO_TYPE_CMOD_OPT:
1311 count ++;
1312 mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1313 break;
1314 default:
1315 found = FALSE;
1319 if (count) {
1320 type = g_malloc0 (sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
1321 type->num_mods = count;
1322 if (count > 64)
1323 g_warning ("got more than 64 modifiers in type");
1325 else {
1326 type = &stype;
1327 memset (type, 0, sizeof (MonoType));
1330 /* Parse pinned, byref and custom modifiers */
1331 found = TRUE;
1332 count = 0;
1333 while (found) {
1334 switch (*ptr) {
1335 case MONO_TYPE_PINNED:
1336 pinned = TRUE;
1337 ++ptr;
1338 break;
1339 case MONO_TYPE_BYREF:
1340 byref = TRUE;
1341 ++ptr;
1342 break;
1343 case MONO_TYPE_CMOD_REQD:
1344 case MONO_TYPE_CMOD_OPT:
1345 mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1346 count ++;
1347 break;
1348 default:
1349 found = FALSE;
1353 type->attrs = opt_attrs;
1354 type->byref = byref;
1355 type->pinned = pinned ? 1 : 0;
1357 do_mono_metadata_parse_type (type, m, generic_context, ptr, &ptr);
1359 if (rptr)
1360 *rptr = ptr;
1363 /* FIXME: remove the != MONO_PARSE_PARAM condition, this accounts for
1364 * almost 10k (about 2/3rds) of all MonoType's we create.
1366 if (mode != MONO_PARSE_PARAM && !type->num_mods) {
1367 /* no need to free type here, because it is on the stack */
1368 if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
1369 if (type->byref)
1370 return &type->data.klass->this_arg;
1371 else
1372 return &type->data.klass->byval_arg;
1374 /* No need to use locking since nobody is modifying the hash table */
1375 if ((cached = g_hash_table_lookup (type_cache, type)))
1376 return cached;
1379 /*printf ("%x%c %s\n", type->attrs, type->pinned ? 'p' : ' ', mono_type_full_name (type));*/
1381 if (type == &stype)
1382 type = g_memdup (&stype, sizeof (MonoType));
1383 return type;
1386 MonoType*
1387 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
1388 const char *ptr, const char **rptr)
1390 return mono_metadata_parse_type_full (m, NULL, mode, opt_attrs, ptr, rptr);
1394 * mono_metadata_parse_signature:
1395 * @image: metadata context
1396 * @toke: metadata token
1398 * Decode a method signature stored in the STANDALONESIG table
1400 * Returns: a MonoMethodSignature describing the signature.
1402 MonoMethodSignature *
1403 mono_metadata_parse_signature_full (MonoImage *image, MonoGenericContext *generic_context, guint32 token)
1405 MonoTableInfo *tables = image->tables;
1406 guint32 idx = mono_metadata_token_index (token);
1407 guint32 sig;
1408 const char *ptr;
1410 if (image->dynamic)
1411 return mono_lookup_dynamic_token (image, token);
1413 g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1415 sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1417 ptr = mono_metadata_blob_heap (image, sig);
1418 mono_metadata_decode_blob_size (ptr, &ptr);
1420 return mono_metadata_parse_method_signature_full (image, generic_context, FALSE, ptr, NULL);
1423 MonoMethodSignature *
1424 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1426 return mono_metadata_parse_signature_full (image, NULL, token);
1429 MonoMethodSignature*
1430 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1432 MonoMethodSignature *sig;
1434 /* later we want to allocate signatures with mempools */
1435 sig = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
1436 sig->param_count = nparams;
1437 sig->sentinelpos = -1;
1439 return sig;
1442 MonoMethodSignature*
1443 mono_metadata_signature_dup (MonoMethodSignature *sig)
1445 int sigsize;
1447 sigsize = sizeof (MonoMethodSignature) + sig->param_count * sizeof (MonoType *);
1448 return g_memdup (sig, sigsize);
1452 * mono_metadata_parse_method_signature:
1453 * @m: metadata context
1454 * @def: the MethodDef index or 0 for Ref signatures.
1455 * @ptr: pointer to the signature metadata representation
1456 * @rptr: pointer updated to match the end of the decoded stream
1458 * Decode a method signature stored at @ptr.
1460 * Returns: a MonoMethodSignature describing the signature.
1462 MonoMethodSignature *
1463 mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContext *generic_context,
1464 int def, const char *ptr, const char **rptr)
1466 MonoMethodSignature *method;
1467 int i, ret_attrs = 0, *pattrs = NULL;
1468 guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
1469 guint32 gen_param_count = 0;
1470 gboolean is_open = FALSE;
1471 MonoGenericContext *context = NULL;
1473 if (*ptr & 0x10)
1474 gen_param_count = 1;
1475 if (*ptr & 0x20)
1476 hasthis = 1;
1477 if (*ptr & 0x40)
1478 explicit_this = 1;
1479 call_convention = *ptr & 0x0F;
1480 ptr++;
1481 if (gen_param_count)
1482 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
1483 param_count = mono_metadata_decode_value (ptr, &ptr);
1484 pattrs = g_new0 (int, param_count);
1486 if (def) {
1487 MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1488 MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1489 guint32 cols [MONO_PARAM_SIZE];
1490 guint lastp, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1492 if (def < methodt->rows)
1493 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1494 else
1495 lastp = paramt->rows + 1;
1496 for (i = param_index; i < lastp; ++i) {
1497 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1498 if (!cols [MONO_PARAM_SEQUENCE])
1499 ret_attrs = cols [MONO_PARAM_FLAGS];
1500 else
1501 pattrs [cols [MONO_PARAM_SEQUENCE] - 1] = cols [MONO_PARAM_FLAGS];
1504 method = mono_metadata_signature_alloc (m, param_count);
1505 method->hasthis = hasthis;
1506 method->explicit_this = explicit_this;
1507 method->call_convention = call_convention;
1508 method->generic_param_count = gen_param_count;
1510 if (gen_param_count)
1511 method->has_type_parameters = 1;
1513 if (gen_param_count && (!generic_context || !generic_context->container->is_method)) {
1514 MonoGenericContainer *container = g_new0 (MonoGenericContainer, 1);
1516 if (generic_context)
1517 container->parent = generic_context->container;
1518 container->is_signature = 1;
1520 context = &container->context;
1521 container->context.container = container;
1523 container->type_argc = gen_param_count;
1524 container->type_params = g_new0 (MonoGenericParam, gen_param_count);
1526 for (i = 0; i < gen_param_count; i++) {
1527 container->type_params [i].owner = container;
1528 container->type_params [i].num = i;
1530 } else
1531 context = generic_context;
1533 if (call_convention != 0xa) {
1534 method->ret = mono_metadata_parse_type_full (m, context, MONO_PARSE_RET, ret_attrs, ptr, &ptr);
1535 is_open = mono_class_is_open_constructed_type (method->ret);
1538 if (method->param_count) {
1539 method->sentinelpos = -1;
1541 for (i = 0; i < method->param_count; ++i) {
1542 if (*ptr == MONO_TYPE_SENTINEL) {
1543 if (method->call_convention != MONO_CALL_VARARG || def)
1544 g_error ("found sentinel for methoddef or no vararg method");
1545 method->sentinelpos = i;
1546 ptr++;
1548 method->params [i] = mono_metadata_parse_type_full (
1549 m, context, MONO_PARSE_PARAM, pattrs [i], ptr, &ptr);
1550 if (!is_open)
1551 is_open = mono_class_is_open_constructed_type (method->params [i]);
1555 method->has_type_parameters = is_open;
1557 if (def && (method->call_convention == MONO_CALL_VARARG))
1558 method->sentinelpos = method->param_count;
1560 g_free (pattrs);
1562 if (rptr)
1563 *rptr = ptr;
1565 * Add signature to a cache and increase ref count...
1567 return method;
1570 MonoMethodSignature *
1571 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
1573 return mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr);
1577 * mono_metadata_free_method_signature:
1578 * @sig: signature to destroy
1580 * Free the memory allocated in the signature @sig.
1582 void
1583 mono_metadata_free_method_signature (MonoMethodSignature *sig)
1585 int i;
1586 mono_metadata_free_type (sig->ret);
1587 for (i = 0; i < sig->param_count; ++i)
1588 mono_metadata_free_type (sig->params [i]);
1590 g_free (sig);
1593 MonoGenericInst *
1594 mono_metadata_lookup_generic_inst (MonoGenericInst *ginst)
1596 MonoGenericInst *cached;
1597 int i;
1599 cached = g_hash_table_lookup (generic_inst_cache, ginst);
1600 if (cached) {
1601 for (i = 0; i < ginst->type_argc; i++)
1602 mono_metadata_free_type (ginst->type_argv [i]);
1603 g_free (ginst->type_argv);
1604 g_free (ginst);
1605 return cached;
1608 ginst->id = ++next_generic_inst_id;
1609 g_hash_table_insert (generic_inst_cache, ginst, ginst);
1611 return ginst;
1614 MonoGenericClass *
1615 mono_metadata_lookup_generic_class (MonoGenericClass *gclass)
1617 MonoGenericClass *cached;
1619 cached = g_hash_table_lookup (generic_class_cache, gclass);
1620 if (cached)
1621 return cached;
1623 g_hash_table_insert (generic_class_cache, gclass, gclass);
1624 return NULL;
1627 MonoGenericInst *
1628 mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context)
1630 MonoGenericInst *nginst;
1631 int i;
1633 nginst = g_new0 (MonoGenericInst, 1);
1634 nginst->type_argc = ginst->type_argc;
1635 nginst->type_argv = g_new0 (MonoType*, nginst->type_argc);
1636 nginst->is_reference = 1;
1638 for (i = 0; i < nginst->type_argc; i++) {
1639 MonoType *t = mono_class_inflate_generic_type (ginst->type_argv [i], context);
1641 if (!nginst->is_open)
1642 nginst->is_open = mono_class_is_open_constructed_type (t);
1643 if (nginst->is_reference)
1644 nginst->is_reference = MONO_TYPE_IS_REFERENCE (t);
1646 nginst->type_argv [i] = t;
1649 return mono_metadata_lookup_generic_inst (nginst);
1652 MonoGenericInst *
1653 mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContext *generic_context,
1654 int count, const char *ptr, const char **rptr)
1656 MonoGenericInst *ginst;
1657 int i;
1659 ginst = g_new0 (MonoGenericInst, 1);
1660 ginst->type_argc = count;
1661 ginst->type_argv = g_new0 (MonoType*, count);
1662 ginst->is_reference = 1;
1664 for (i = 0; i < ginst->type_argc; i++) {
1665 MonoType *t = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_TYPE, 0, ptr, &ptr);
1667 ginst->type_argv [i] = t;
1668 if (!ginst->is_open)
1669 ginst->is_open = mono_class_is_open_constructed_type (t);
1670 if (ginst->is_reference)
1671 ginst->is_reference = MONO_TYPE_IS_REFERENCE (t);
1674 if (rptr)
1675 *rptr = ptr;
1677 return mono_metadata_lookup_generic_inst (ginst);
1680 static void
1681 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContext *generic_context,
1682 const char *ptr, const char **rptr)
1684 MonoGenericClass *gclass = g_new0 (MonoGenericClass, 1);
1685 MonoGenericClass *cached;
1686 MonoClass *gklass;
1687 MonoType *gtype;
1688 int count;
1690 type->data.generic_class = gclass;
1692 gclass->context = g_new0 (MonoGenericContext, 1);
1693 gclass->context->gclass = gclass;
1695 gclass->klass = g_new0 (MonoClass, 1);
1697 gtype = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_TYPE, 0, ptr, &ptr);
1698 gclass->container_class = gklass = mono_class_from_mono_type (gtype);
1700 g_assert ((gclass->context->container = gklass->generic_container) != NULL);
1701 count = mono_metadata_decode_value (ptr, &ptr);
1704 * Create the klass before parsing the type arguments.
1705 * This is required to support "recursive" definitions.
1706 * See mcs/tests/gen-23.cs for an example.
1709 mono_class_create_generic (gclass);
1711 gclass->inst = mono_metadata_parse_generic_inst (m, generic_context, count, ptr, &ptr);
1713 if (rptr)
1714 *rptr = ptr;
1717 * We may be called multiple times on different metadata to create the same
1718 * instantiated type. This happens for instance if we're part of a method or
1719 * local variable signature.
1721 * It's important to return the same MonoGenericClass * for each particualar
1722 * instantiation of a generic type (ie "Stack<Int32>") to make static fields
1723 * work.
1725 * According to the spec ($26.1.5), a static variable in a generic class
1726 * declaration is shared amongst all instances of the same closed constructed
1727 * type.
1730 cached = g_hash_table_lookup (generic_class_cache, gclass);
1731 if (cached) {
1732 g_free (gclass->klass);
1733 g_free (gclass);
1735 type->data.generic_class = cached;
1736 return;
1737 } else {
1738 g_hash_table_insert (generic_class_cache, gclass, gclass);
1740 mono_stats.generic_instance_count++;
1741 mono_stats.generics_metadata_size += sizeof (MonoGenericClass) +
1742 sizeof (MonoGenericContext) +
1743 gclass->inst->type_argc * sizeof (MonoType);
1748 * do_mono_metadata_parse_generic_param:
1749 * @generic_container: Our MonoClass's or MonoMethodNormal's MonoGenericContainer;
1750 * see mono_metadata_parse_type_full() for details.
1751 * Internal routine to parse a generic type parameter.
1753 static MonoGenericParam *
1754 mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContext *generic_context,
1755 gboolean is_mvar, const char *ptr, const char **rptr)
1757 MonoGenericContainer *generic_container;
1758 int index;
1760 index = mono_metadata_decode_value (ptr, &ptr);
1761 if (rptr)
1762 *rptr = ptr;
1764 g_assert (generic_context);
1765 if (generic_context->gmethod)
1766 generic_container = generic_context->gmethod->container;
1767 else if (generic_context->gclass)
1768 generic_container = generic_context->gclass->container_class->generic_container;
1769 else
1770 generic_container = generic_context->container;
1772 if (!is_mvar) {
1773 g_assert (generic_container);
1774 if (generic_container->parent) {
1776 * The current MonoGenericContainer is a generic method -> its `parent'
1777 * points to the containing class'es container.
1779 generic_container = generic_container->parent;
1781 g_assert (generic_container && !generic_container->is_method);
1782 g_assert (index < generic_container->type_argc);
1784 return &generic_container->type_params [index];
1785 } else {
1786 g_assert (generic_container && (generic_container->is_method || generic_container->is_signature));
1787 g_assert (index < generic_container->type_argc);
1789 return &generic_container->type_params [index];
1794 * do_mono_metadata_parse_type:
1795 * @type: MonoType to be filled in with the return value
1797 * Internal routine used to "fill" the contents of @type from an
1798 * allocated pointer. This is done this way to avoid doing too
1799 * many mini-allocations (particularly for the MonoFieldType which
1800 * most of the time is just a MonoType, but sometimes might be augmented).
1802 * This routine is used by mono_metadata_parse_type and
1803 * mono_metadata_parse_field_type
1805 * This extracts a Type as specified in Partition II (22.2.12)
1807 static void
1808 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContext *generic_context,
1809 const char *ptr, const char **rptr)
1811 type->type = mono_metadata_decode_value (ptr, &ptr);
1813 switch (type->type){
1814 case MONO_TYPE_VOID:
1815 case MONO_TYPE_BOOLEAN:
1816 case MONO_TYPE_CHAR:
1817 case MONO_TYPE_I1:
1818 case MONO_TYPE_U1:
1819 case MONO_TYPE_I2:
1820 case MONO_TYPE_U2:
1821 case MONO_TYPE_I4:
1822 case MONO_TYPE_U4:
1823 case MONO_TYPE_I8:
1824 case MONO_TYPE_U8:
1825 case MONO_TYPE_R4:
1826 case MONO_TYPE_R8:
1827 case MONO_TYPE_I:
1828 case MONO_TYPE_U:
1829 case MONO_TYPE_STRING:
1830 case MONO_TYPE_OBJECT:
1831 case MONO_TYPE_TYPEDBYREF:
1832 break;
1833 case MONO_TYPE_VALUETYPE:
1834 case MONO_TYPE_CLASS: {
1835 guint32 token;
1836 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
1837 type->data.klass = mono_class_get (m, token);
1838 break;
1840 case MONO_TYPE_SZARRAY: {
1841 MonoType *etype = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1842 type->data.klass = mono_class_from_mono_type (etype);
1843 mono_metadata_free_type (etype);
1844 break;
1846 case MONO_TYPE_PTR:
1847 type->data.type = mono_metadata_parse_type_full (m, generic_context, MONO_PARSE_MOD_TYPE, 0, ptr, &ptr);
1848 break;
1849 case MONO_TYPE_FNPTR:
1850 type->data.method = mono_metadata_parse_method_signature_full (m, generic_context, 0, ptr, &ptr);
1851 break;
1852 case MONO_TYPE_ARRAY:
1853 type->data.array = mono_metadata_parse_array_full (m, generic_context, ptr, &ptr);
1854 break;
1856 case MONO_TYPE_MVAR:
1857 type->data.generic_param = mono_metadata_parse_generic_param (m, generic_context, TRUE, ptr, &ptr);
1858 break;
1860 case MONO_TYPE_VAR:
1861 type->data.generic_param = mono_metadata_parse_generic_param (m, generic_context, FALSE, ptr, &ptr);
1862 break;
1864 case MONO_TYPE_GENERICINST:
1865 do_mono_metadata_parse_generic_class (type, m, generic_context, ptr, &ptr);
1866 break;
1868 default:
1869 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
1872 if (rptr)
1873 *rptr = ptr;
1877 * mono_metadata_free_type:
1878 * @type: type to free
1880 * Free the memory allocated for type @type.
1882 void
1883 mono_metadata_free_type (MonoType *type)
1885 if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
1886 return;
1888 switch (type->type){
1889 case MONO_TYPE_OBJECT:
1890 case MONO_TYPE_STRING:
1891 if (!type->data.klass)
1892 break;
1893 /* fall through */
1894 case MONO_TYPE_CLASS:
1895 case MONO_TYPE_VALUETYPE:
1896 if (type == &type->data.klass->byval_arg || type == &type->data.klass->this_arg)
1897 return;
1898 break;
1899 case MONO_TYPE_PTR:
1900 mono_metadata_free_type (type->data.type);
1901 break;
1902 case MONO_TYPE_FNPTR:
1903 mono_metadata_free_method_signature (type->data.method);
1904 break;
1905 case MONO_TYPE_ARRAY:
1906 mono_metadata_free_array (type->data.array);
1907 break;
1909 g_free (type);
1912 #if 0
1913 static void
1914 hex_dump (const char *buffer, int base, int count)
1916 int show_header = 1;
1917 int i;
1919 if (count < 0){
1920 count = -count;
1921 show_header = 0;
1924 for (i = 0; i < count; i++){
1925 if (show_header)
1926 if ((i % 16) == 0)
1927 printf ("\n0x%08x: ", (unsigned char) base + i);
1929 printf ("%02x ", (unsigned char) (buffer [i]));
1931 fflush (stdout);
1933 #endif
1935 /**
1936 * @mh: The Method header
1937 * @ptr: Points to the beginning of the Section Data (25.3)
1939 static void
1940 parse_section_data (MonoImage *m, MonoMethodHeader *mh, const unsigned char *ptr)
1942 unsigned char sect_data_flags;
1943 const unsigned char *sptr;
1944 int is_fat;
1945 guint32 sect_data_len;
1947 while (1) {
1948 /* align on 32-bit boundary */
1949 /* FIXME: not 64-bit clean code */
1950 sptr = ptr = dword_align (ptr);
1951 sect_data_flags = *ptr;
1952 ptr++;
1954 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
1955 if (is_fat) {
1956 sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
1957 ptr += 3;
1958 } else {
1959 sect_data_len = ptr [0];
1960 ++ptr;
1963 g_print ("flags: %02x, len: %d\n", sect_data_flags, sect_data_len);
1964 hex_dump (sptr, 0, sect_data_len+8);
1965 g_print ("\nheader: ");
1966 hex_dump (sptr-4, 0, 4);
1967 g_print ("\n");
1970 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
1971 const unsigned char *p = dword_align (ptr);
1972 int i;
1973 mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
1974 /* we could just store a pointer if we don't need to byteswap */
1975 mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
1976 for (i = 0; i < mh->num_clauses; ++i) {
1977 MonoExceptionClause *ec = &mh->clauses [i];
1978 guint32 tof_value;
1979 if (is_fat) {
1980 ec->flags = read32 (p);
1981 p += 4;
1982 ec->try_offset = read32 (p);
1983 p += 4;
1984 ec->try_len = read32 (p);
1985 p += 4;
1986 ec->handler_offset = read32 (p);
1987 p += 4;
1988 ec->handler_len = read32 (p);
1989 p += 4;
1990 tof_value = read32 (p);
1991 p += 4;
1992 } else {
1993 ec->flags = read16 (p);
1994 p += 2;
1995 ec->try_offset = read16 (p);
1996 p += 2;
1997 ec->try_len = *p;
1998 ++p;
1999 ec->handler_offset = read16 (p);
2000 p += 2;
2001 ec->handler_len = *p;
2002 ++p;
2003 tof_value = read32 (p);
2004 p += 4;
2006 if (ec->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
2007 ec->data.filter_offset = tof_value;
2008 } else if (ec->flags == MONO_EXCEPTION_CLAUSE_NONE) {
2009 ec->data.catch_class = tof_value? mono_class_get (m, tof_value): 0;
2010 } else {
2011 ec->data.catch_class = NULL;
2013 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
2017 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
2018 ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
2019 else
2020 return;
2025 * mono_metadata_parse_mh:
2026 * @m: metadata context
2027 * @ptr: pointer to the method header.
2029 * Decode the method header at @ptr, including pointer to the IL code,
2030 * info about local variables and optional exception tables.
2032 * Returns: a MonoMethodHeader.
2034 MonoMethodHeader *
2035 mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContext *generic_context, const char *ptr)
2037 MonoMethodHeader *mh;
2038 unsigned char flags = *(const unsigned char *) ptr;
2039 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
2040 guint16 fat_flags;
2041 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
2042 const unsigned char *code;
2043 int hsize;
2045 g_return_val_if_fail (ptr != NULL, NULL);
2047 switch (format){
2048 case METHOD_HEADER_TINY_FORMAT:
2049 mh = g_new0 (MonoMethodHeader, 1);
2050 ptr++;
2051 mh->max_stack = 8;
2052 local_var_sig_tok = 0;
2053 mh->code_size = flags >> 2;
2054 mh->code = ptr;
2055 return mh;
2056 case METHOD_HEADER_TINY_FORMAT1:
2057 mh = g_new0 (MonoMethodHeader, 1);
2058 ptr++;
2059 mh->max_stack = 8;
2060 local_var_sig_tok = 0;
2063 * The spec claims 3 bits, but the Beta2 is
2064 * incorrect
2066 mh->code_size = flags >> 2;
2067 mh->code = ptr;
2068 return mh;
2069 case METHOD_HEADER_FAT_FORMAT:
2070 fat_flags = read16 (ptr);
2071 ptr += 2;
2072 hsize = (fat_flags >> 12) & 0xf;
2073 max_stack = read16 (ptr);
2074 ptr += 2;
2075 code_size = read32 (ptr);
2076 ptr += 4;
2077 local_var_sig_tok = read32 (ptr);
2078 ptr += 4;
2080 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
2081 init_locals = 1;
2082 else
2083 init_locals = 0;
2085 code = ptr;
2087 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
2088 break;
2091 * There are more sections
2093 ptr = code + code_size;
2095 break;
2097 default:
2098 return NULL;
2101 if (local_var_sig_tok) {
2102 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
2103 const char *locals_ptr;
2104 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
2105 int len=0, i, bsize;
2107 mono_metadata_decode_row (t, (local_var_sig_tok & 0xffffff)-1, cols, 1);
2108 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
2109 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
2110 if (*locals_ptr != 0x07)
2111 g_warning ("wrong signature for locals blob");
2112 locals_ptr++;
2113 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
2114 mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
2115 mh->num_locals = len;
2116 for (i = 0; i < len; ++i)
2117 mh->locals [i] = mono_metadata_parse_type_full (
2118 m, generic_context, MONO_PARSE_LOCAL, 0, locals_ptr, &locals_ptr);
2119 } else {
2120 mh = g_new0 (MonoMethodHeader, 1);
2122 mh->code = code;
2123 mh->code_size = code_size;
2124 mh->max_stack = max_stack;
2125 mh->init_locals = init_locals;
2126 if (fat_flags & METHOD_HEADER_MORE_SECTS)
2127 parse_section_data (m, mh, (const unsigned char*)ptr);
2128 return mh;
2131 MonoMethodHeader *
2132 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
2134 return mono_metadata_parse_mh_full (m, NULL, ptr);
2138 * mono_metadata_free_mh:
2139 * @mh: a method header
2141 * Free the memory allocated for the method header.
2143 void
2144 mono_metadata_free_mh (MonoMethodHeader *mh)
2146 int i;
2147 for (i = 0; i < mh->num_locals; ++i)
2148 mono_metadata_free_type (mh->locals[i]);
2149 g_free (mh->clauses);
2150 g_free (mh);
2154 * mono_metadata_parse_field_type:
2155 * @m: metadata context to extract information from
2156 * @ptr: pointer to the field signature
2157 * @rptr: pointer updated to match the end of the decoded stream
2159 * Parses the field signature, and returns the type information for it.
2161 * Returns: The MonoType that was extracted from @ptr.
2163 MonoType *
2164 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
2166 return mono_metadata_parse_type (m, MONO_PARSE_FIELD, field_flags, ptr, rptr);
2170 * mono_metadata_parse_param:
2171 * @m: metadata context to extract information from
2172 * @ptr: pointer to the param signature
2173 * @rptr: pointer updated to match the end of the decoded stream
2175 * Parses the param signature, and returns the type information for it.
2177 * Returns: The MonoType that was extracted from @ptr.
2179 MonoType *
2180 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
2182 return mono_metadata_parse_type (m, MONO_PARSE_PARAM, 0, ptr, rptr);
2186 * mono_metadata_token_from_dor:
2187 * @dor_token: A TypeDefOrRef coded index
2189 * dor_token is a TypeDefOrRef coded index: it contains either
2190 * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
2191 * bits contain an index into the table.
2193 * Returns: an expanded token
2195 guint32
2196 mono_metadata_token_from_dor (guint32 dor_index)
2198 guint32 table, idx;
2200 table = dor_index & 0x03;
2201 idx = dor_index >> 2;
2203 switch (table){
2204 case 0: /* TypeDef */
2205 return MONO_TOKEN_TYPE_DEF | idx;
2207 case 1: /* TypeRef */
2208 return MONO_TOKEN_TYPE_REF | idx;
2210 case 2: /* TypeSpec */
2211 return MONO_TOKEN_TYPE_SPEC | idx;
2213 default:
2214 g_assert_not_reached ();
2217 return 0;
2221 * We use this to pass context information to the row locator
2223 typedef struct {
2224 int idx; /* The index that we are trying to locate */
2225 int col_idx; /* The index in the row where idx may be stored */
2226 MonoTableInfo *t; /* pointer to the table */
2227 guint32 result;
2228 } locator_t;
2231 * How the row locator works.
2233 * Table A
2234 * ___|___
2235 * ___|___ Table B
2236 * ___|___------> _______
2237 * ___|___ _______
2239 * A column in the rows of table A references an index in table B.
2240 * For example A may be the TYPEDEF table and B the METHODDEF table.
2242 * Given an index in table B we want to get the row in table A
2243 * where the column n references our index in B.
2245 * In the locator_t structure:
2246 * t is table A
2247 * col_idx is the column number
2248 * index is the index in table B
2249 * result will be the index in table A
2251 * Examples:
2252 * Table A Table B column (in table A)
2253 * TYPEDEF METHODDEF MONO_TYPEDEF_METHOD_LIST
2254 * TYPEDEF FIELD MONO_TYPEDEF_FIELD_LIST
2255 * PROPERTYMAP PROPERTY MONO_PROPERTY_MAP_PROPERTY_LIST
2256 * INTERFIMPL TYPEDEF MONO_INTERFACEIMPL_CLASS
2257 * METHODSEM PROPERTY ASSOCIATION (encoded index)
2259 * Note that we still don't support encoded indexes.
2262 static int
2263 typedef_locator (const void *a, const void *b)
2265 locator_t *loc = (locator_t *) a;
2266 const char *bb = (const char *) b;
2267 int typedef_index = (bb - loc->t->base) / loc->t->row_size;
2268 guint32 col, col_next;
2270 col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
2272 if (loc->idx < col)
2273 return -1;
2276 * Need to check that the next row is valid.
2278 if (typedef_index + 1 < loc->t->rows) {
2279 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
2280 if (loc->idx >= col_next)
2281 return 1;
2283 if (col == col_next)
2284 return 1;
2287 loc->result = typedef_index;
2289 return 0;
2292 static int
2293 table_locator (const void *a, const void *b)
2295 locator_t *loc = (locator_t *) a;
2296 const char *bb = (const char *) b;
2297 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2298 guint32 col;
2300 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2302 if (loc->idx == col) {
2303 loc->result = table_index;
2304 return 0;
2306 if (loc->idx < col)
2307 return -1;
2308 else
2309 return 1;
2312 static int
2313 declsec_locator (const void *a, const void *b)
2315 locator_t *loc = (locator_t *) a;
2316 const char *bb = (const char *) b;
2317 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
2318 guint32 col;
2320 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
2322 if (loc->idx == col) {
2323 loc->result = table_index;
2324 return 0;
2326 if (loc->idx < col)
2327 return -1;
2328 else
2329 return 1;
2333 * mono_metadata_typedef_from_field:
2334 * @meta: metadata context
2335 * @index: FieldDef token
2337 * Returns: the 1-based index into the TypeDef table of the type that
2338 * declared the field described by @index, or 0 if not found.
2340 guint32
2341 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
2343 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2344 locator_t loc;
2346 if (!tdef->base)
2347 return 0;
2349 loc.idx = mono_metadata_token_index (index);
2350 loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
2351 loc.t = tdef;
2353 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2354 g_assert_not_reached ();
2356 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2357 return loc.result + 1;
2361 * mono_metadata_typedef_from_method:
2362 * @meta: metadata context
2363 * @index: MethodDef token
2365 * Returns: the 1-based index into the TypeDef table of the type that
2366 * declared the method described by @index. 0 if not found.
2368 guint32
2369 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
2371 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
2372 locator_t loc;
2374 if (!tdef->base)
2375 return 0;
2377 loc.idx = mono_metadata_token_index (index);
2378 loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
2379 loc.t = tdef;
2381 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
2382 g_assert_not_reached ();
2384 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2385 return loc.result + 1;
2389 * mono_metadata_interfaces_from_typedef:
2390 * @meta: metadata context
2391 * @index: typedef token
2393 * Returns: and array of interfaces that the @index typedef token implements.
2394 * The number of elemnts in the array is returned in @count.
2396 MonoClass**
2397 mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, guint *count, MonoGenericContext *context)
2399 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
2400 locator_t loc;
2401 guint32 start, i;
2402 guint32 cols [MONO_INTERFACEIMPL_SIZE];
2403 MonoClass **result;
2405 *count = 0;
2407 if (!tdef->base)
2408 return NULL;
2410 loc.idx = mono_metadata_token_index (index);
2411 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
2412 loc.t = tdef;
2414 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2415 return NULL;
2417 start = loc.result;
2419 * We may end up in the middle of the rows...
2421 while (start > 0) {
2422 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
2423 start--;
2424 else
2425 break;
2427 result = NULL;
2428 i = 0;
2429 while (start < tdef->rows) {
2430 mono_metadata_decode_row (tdef, start, cols, MONO_INTERFACEIMPL_SIZE);
2431 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
2432 break;
2433 result = g_renew (MonoClass*, result, i + 1);
2434 result [i] = mono_class_get_full (
2435 meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]), context);
2436 *count = ++i;
2437 ++start;
2439 return result;
2442 MonoClass**
2443 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
2445 return mono_metadata_interfaces_from_typedef_full (meta, index, count, NULL);
2449 * mono_metadata_nested_in_typedef:
2450 * @meta: metadata context
2451 * @index: typedef token
2453 * Returns: the 1-based index into the TypeDef table of the type
2454 * where the type described by @index is nested.
2455 * Retruns 0 if @index describes a non-nested type.
2457 guint32
2458 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
2460 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2461 locator_t loc;
2463 if (!tdef->base)
2464 return 0;
2466 loc.idx = mono_metadata_token_index (index);
2467 loc.col_idx = MONO_NESTED_CLASS_NESTED;
2468 loc.t = tdef;
2470 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2471 return 0;
2473 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2474 return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
2478 * mono_metadata_nesting_typedef:
2479 * @meta: metadata context
2480 * @index: typedef token
2482 * Returns: the 1-based index into the TypeDef table of the first type
2483 * that is nested inside the type described by @index. The search starts at
2484 * @start_index. returns 0 if no such type is found.
2486 guint32
2487 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
2489 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
2490 guint32 start;
2491 guint32 class_index = mono_metadata_token_index (index);
2493 if (!tdef->base)
2494 return 0;
2496 start = start_index;
2498 while (start <= tdef->rows) {
2499 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
2500 break;
2501 else
2502 start++;
2505 if (start > tdef->rows)
2506 return 0;
2507 else
2508 return start;
2512 * mono_metadata_packing_from_typedef:
2513 * @meta: metadata context
2514 * @index: token representing a type
2516 * Returns: the info stored in the ClassLAyout table for the given typedef token
2517 * into the @packing and @size pointers.
2518 * Returns 0 if the info is not found.
2520 guint32
2521 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
2523 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
2524 locator_t loc;
2525 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
2527 if (!tdef->base)
2528 return 0;
2530 loc.idx = mono_metadata_token_index (index);
2531 loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
2532 loc.t = tdef;
2534 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2535 return 0;
2537 mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
2538 if (packing)
2539 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
2540 if (size)
2541 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
2543 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2544 return loc.result + 1;
2548 * mono_metadata_custom_attrs_from_index:
2549 * @meta: metadata context
2550 * @index: token representing the parent
2552 * Returns: the 1-based index into the CustomAttribute table of the first
2553 * attribute which belongs to the metadata object described by @index.
2554 * Returns 0 if no such attribute is found.
2556 guint32
2557 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
2559 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
2560 locator_t loc;
2562 if (!tdef->base)
2563 return 0;
2565 loc.idx = index;
2566 loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
2567 loc.t = tdef;
2569 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
2570 return 0;
2572 /* Find the first entry by searching backwards */
2573 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
2574 loc.result --;
2576 /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
2577 return loc.result + 1;
2581 * mono_metadata_declsec_from_index:
2582 * @meta: metadata context
2583 * @index: token representing the parent
2585 * Returns: the 0-based index into the DeclarativeSecurity table of the first
2586 * attribute which belongs to the metadata object described by @index.
2587 * Returns -1 if no such attribute is found.
2589 guint32
2590 mono_metadata_declsec_from_index (MonoImage *meta, guint32 index)
2592 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_DECLSECURITY];
2593 locator_t loc;
2595 if (!tdef->base)
2596 return -1;
2598 loc.idx = index;
2599 loc.col_idx = MONO_DECL_SECURITY_PARENT;
2600 loc.t = tdef;
2602 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
2603 return -1;
2605 /* Find the first entry by searching backwards */
2606 while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_DECL_SECURITY_PARENT) == index))
2607 loc.result --;
2609 return loc.result;
2612 #ifdef DEBUG
2613 static void
2614 mono_backtrace (int limit)
2616 void *array[limit];
2617 char **names;
2618 int i;
2619 backtrace (array, limit);
2620 names = backtrace_symbols (array, limit);
2621 for (i =0; i < limit; ++i) {
2622 g_print ("\t%s\n", names [i]);
2624 g_free (names);
2626 #endif
2628 #ifndef __GNUC__
2629 /*#define __alignof__(a) sizeof(a)*/
2630 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
2631 #endif
2634 * mono_type_size:
2635 * @t: the type to return the size of
2637 * Returns: the number of bytes required to hold an instance of this
2638 * type in memory
2641 mono_type_size (MonoType *t, gint *align)
2643 if (!t) {
2644 *align = 1;
2645 return 0;
2647 if (t->byref) {
2648 *align = __alignof__(gpointer);
2649 return sizeof (gpointer);
2652 switch (t->type){
2653 case MONO_TYPE_VOID:
2654 *align = 1;
2655 return 0;
2656 case MONO_TYPE_BOOLEAN:
2657 *align = __alignof__(gint8);
2658 return 1;
2659 case MONO_TYPE_I1:
2660 case MONO_TYPE_U1:
2661 *align = __alignof__(gint8);
2662 return 1;
2663 case MONO_TYPE_CHAR:
2664 case MONO_TYPE_I2:
2665 case MONO_TYPE_U2:
2666 *align = __alignof__(gint16);
2667 return 2;
2668 case MONO_TYPE_I4:
2669 case MONO_TYPE_U4:
2670 *align = __alignof__(gint32);
2671 return 4;
2672 case MONO_TYPE_R4:
2673 *align = __alignof__(float);
2674 return 4;
2675 case MONO_TYPE_I8:
2676 case MONO_TYPE_U8:
2677 *align = __alignof__(gint64);
2678 return 8;
2679 case MONO_TYPE_R8:
2680 *align = __alignof__(double);
2681 return 8;
2682 case MONO_TYPE_I:
2683 case MONO_TYPE_U:
2684 *align = __alignof__(gpointer);
2685 return sizeof (gpointer);
2686 case MONO_TYPE_STRING:
2687 *align = __alignof__(gpointer);
2688 return sizeof (gpointer);
2689 case MONO_TYPE_OBJECT:
2690 *align = __alignof__(gpointer);
2691 return sizeof (gpointer);
2692 case MONO_TYPE_VALUETYPE: {
2693 if (t->data.klass->enumtype)
2694 return mono_type_size (t->data.klass->enum_basetype, align);
2695 else
2696 return mono_class_value_size (t->data.klass, align);
2698 case MONO_TYPE_CLASS:
2699 case MONO_TYPE_SZARRAY:
2700 case MONO_TYPE_PTR:
2701 case MONO_TYPE_FNPTR:
2702 case MONO_TYPE_ARRAY:
2703 *align = __alignof__(gpointer);
2704 return sizeof (gpointer);
2705 case MONO_TYPE_TYPEDBYREF:
2706 return mono_class_value_size (mono_defaults.typed_reference_class, align);
2707 case MONO_TYPE_GENERICINST: {
2708 MonoGenericClass *gclass = t->data.generic_class;
2710 g_assert (!gclass->inst->is_open && !gclass->klass->generic_container);
2712 if (gclass->container_class->valuetype) {
2713 if (gclass->container_class->enumtype)
2714 return mono_type_size (gclass->container_class->enum_basetype, align);
2715 else
2716 return mono_class_value_size (gclass->klass, align);
2717 } else {
2718 *align = __alignof__(gpointer);
2719 return sizeof (gpointer);
2722 case MONO_TYPE_VAR:
2723 case MONO_TYPE_MVAR:
2724 *align = __alignof__(gpointer);
2725 return sizeof (gpointer);
2726 default:
2727 g_error ("mono_type_size: type 0x%02x unknown", t->type);
2729 return 0;
2733 * mono_type_stack_size:
2734 * @t: the type to return the size it uses on the stack
2736 * Returns: the number of bytes required to hold an instance of this
2737 * type on the runtime stack
2740 mono_type_stack_size (MonoType *t, gint *align)
2742 int tmp;
2744 g_assert (t != NULL);
2746 if (!align)
2747 align = &tmp;
2749 if (t->byref) {
2750 *align = __alignof__(gpointer);
2751 return sizeof (gpointer);
2754 switch (t->type){
2755 case MONO_TYPE_BOOLEAN:
2756 case MONO_TYPE_CHAR:
2757 case MONO_TYPE_I1:
2758 case MONO_TYPE_U1:
2759 case MONO_TYPE_I2:
2760 case MONO_TYPE_U2:
2761 case MONO_TYPE_I4:
2762 case MONO_TYPE_U4:
2763 case MONO_TYPE_I:
2764 case MONO_TYPE_U:
2765 case MONO_TYPE_STRING:
2766 case MONO_TYPE_OBJECT:
2767 case MONO_TYPE_CLASS:
2768 case MONO_TYPE_SZARRAY:
2769 case MONO_TYPE_PTR:
2770 case MONO_TYPE_FNPTR:
2771 case MONO_TYPE_ARRAY:
2772 *align = __alignof__(gpointer);
2773 return sizeof (gpointer);
2774 case MONO_TYPE_TYPEDBYREF:
2775 *align = __alignof__(gpointer);
2776 return sizeof (gpointer) * 3;
2777 case MONO_TYPE_R4:
2778 *align = __alignof__(float);
2779 return sizeof (float);
2780 case MONO_TYPE_I8:
2781 case MONO_TYPE_U8:
2782 *align = __alignof__(gint64);
2783 return sizeof (gint64);
2784 case MONO_TYPE_R8:
2785 *align = __alignof__(double);
2786 return sizeof (double);
2787 case MONO_TYPE_VALUETYPE: {
2788 guint32 size;
2790 if (t->data.klass->enumtype)
2791 return mono_type_stack_size (t->data.klass->enum_basetype, align);
2792 else {
2793 size = mono_class_value_size (t->data.klass, align);
2795 *align = *align + __alignof__(gpointer) - 1;
2796 *align &= ~(__alignof__(gpointer) - 1);
2798 size += sizeof (gpointer) - 1;
2799 size &= ~(sizeof (gpointer) - 1);
2801 return size;
2804 case MONO_TYPE_GENERICINST: {
2805 MonoGenericClass *gclass = t->data.generic_class;
2807 g_assert (!gclass->inst->is_open && !gclass->klass->generic_container);
2809 if (gclass->container_class->valuetype) {
2810 if (gclass->container_class->enumtype)
2811 return mono_type_stack_size (gclass->container_class->enum_basetype, align);
2812 else {
2813 guint32 size = mono_class_value_size (gclass->klass, align);
2815 *align = *align + __alignof__(gpointer) - 1;
2816 *align &= ~(__alignof__(gpointer) - 1);
2818 size += sizeof (gpointer) - 1;
2819 size &= ~(sizeof (gpointer) - 1);
2821 return size;
2823 } else {
2824 *align = __alignof__(gpointer);
2825 return sizeof (gpointer);
2828 default:
2829 g_error ("type 0x%02x unknown", t->type);
2831 return 0;
2834 gboolean
2835 mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass)
2837 return gclass->container_class->valuetype;
2840 static gboolean
2841 _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2, gboolean signature_only)
2843 int i;
2845 if ((g1->inst->type_argc != g2->inst->type_argc) || (g1->is_dynamic != g2->is_dynamic) ||
2846 (g1->inst->is_reference != g2->inst->is_reference))
2847 return FALSE;
2848 if (!mono_metadata_class_equal (g1->container_class, g2->container_class, signature_only))
2849 return FALSE;
2850 for (i = 0; i < g1->inst->type_argc; ++i) {
2851 if (!do_mono_metadata_type_equal (g1->inst->type_argv [i], g2->inst->type_argv [i], signature_only))
2852 return FALSE;
2854 return TRUE;
2857 guint
2858 mono_metadata_generic_method_hash (MonoGenericMethod *gmethod)
2860 return gmethod->inst->id;
2863 gboolean
2864 mono_metadata_generic_method_equal (MonoGenericMethod *g1, MonoGenericMethod *g2)
2866 return (g1->container == g2->container) && (g1->inst == g2->inst);
2871 * mono_metadata_type_hash:
2872 * @t1: a type
2874 * Computes an hash value for @t1 to be used in GHashTable.
2876 guint
2877 mono_metadata_type_hash (MonoType *t1)
2879 guint hash = t1->type;
2881 hash |= t1->byref << 6; /* do not collide with t1->type values */
2882 switch (t1->type) {
2883 case MONO_TYPE_VALUETYPE:
2884 case MONO_TYPE_CLASS:
2885 case MONO_TYPE_SZARRAY:
2886 /* check if the distribution is good enough */
2887 return ((hash << 5) - hash) ^ g_str_hash (t1->data.klass->name);
2888 case MONO_TYPE_PTR:
2889 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
2890 case MONO_TYPE_ARRAY:
2891 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
2892 case MONO_TYPE_GENERICINST:
2893 return ((hash << 5) - hash) ^ mono_generic_class_hash (t1->data.generic_class);
2895 return hash;
2898 static gboolean
2899 mono_metadata_generic_param_equal (MonoGenericParam *p1, MonoGenericParam *p2, gboolean signature_only)
2901 if (p1 == p2)
2902 return TRUE;
2903 if (p1->num != p2->num)
2904 return FALSE;
2906 g_assert (p1->owner && p2->owner);
2907 if (p1->owner == p2->owner)
2908 return TRUE;
2911 * If `signature_only' is true, we're comparing two (method) signatures.
2912 * In this case, the owner of two type parameters doesn't need to match.
2915 return signature_only;
2918 static gboolean
2919 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only)
2921 if (c1 == c2)
2922 return TRUE;
2923 if (c1->generic_class && c2->generic_class)
2924 return _mono_metadata_generic_class_equal (c1->generic_class, c2->generic_class, signature_only);
2925 if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
2926 return mono_metadata_generic_param_equal (
2927 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, FALSE);
2928 if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
2929 return mono_metadata_generic_param_equal (
2930 c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
2931 return FALSE;
2935 * mono_metadata_type_equal:
2936 * @t1: a type
2937 * @t2: another type
2939 * Determine if @t1 and @t2 represent the same type.
2940 * Returns: #TRUE if @t1 and @t2 are equal.
2942 static gboolean
2943 do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only)
2945 if (t1->type != t2->type ||
2946 t1->byref != t2->byref)
2947 return FALSE;
2949 switch (t1->type) {
2950 case MONO_TYPE_VOID:
2951 case MONO_TYPE_BOOLEAN:
2952 case MONO_TYPE_CHAR:
2953 case MONO_TYPE_I1:
2954 case MONO_TYPE_U1:
2955 case MONO_TYPE_I2:
2956 case MONO_TYPE_U2:
2957 case MONO_TYPE_I4:
2958 case MONO_TYPE_U4:
2959 case MONO_TYPE_I8:
2960 case MONO_TYPE_U8:
2961 case MONO_TYPE_R4:
2962 case MONO_TYPE_R8:
2963 case MONO_TYPE_STRING:
2964 case MONO_TYPE_I:
2965 case MONO_TYPE_U:
2966 case MONO_TYPE_OBJECT:
2967 case MONO_TYPE_TYPEDBYREF:
2968 return TRUE;
2969 case MONO_TYPE_VALUETYPE:
2970 case MONO_TYPE_CLASS:
2971 case MONO_TYPE_SZARRAY:
2972 return mono_metadata_class_equal (t1->data.klass, t2->data.klass, signature_only);
2973 case MONO_TYPE_PTR:
2974 return do_mono_metadata_type_equal (t1->data.type, t2->data.type, signature_only);
2975 case MONO_TYPE_ARRAY:
2976 if (t1->data.array->rank != t2->data.array->rank)
2977 return FALSE;
2978 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass, signature_only);
2979 case MONO_TYPE_GENERICINST:
2980 return _mono_metadata_generic_class_equal (
2981 t1->data.generic_class, t2->data.generic_class, signature_only);
2982 case MONO_TYPE_VAR:
2983 return mono_metadata_generic_param_equal (
2984 t1->data.generic_param, t2->data.generic_param, FALSE);
2985 case MONO_TYPE_MVAR:
2986 return mono_metadata_generic_param_equal (
2987 t1->data.generic_param, t2->data.generic_param, signature_only);
2988 default:
2989 g_error ("implement type compare for %0x!", t1->type);
2990 return FALSE;
2993 return FALSE;
2996 gboolean
2997 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
2999 return do_mono_metadata_type_equal (t1, t2, FALSE);
3003 * mono_metadata_signature_equal:
3004 * @sig1: a signature
3005 * @sig2: another signature
3007 * Determine if @sig1 and @sig2 represent the same signature, with the
3008 * same number of arguments and the same types.
3009 * Returns: #TRUE if @sig1 and @sig2 are equal.
3011 gboolean
3012 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
3014 int i;
3016 if (sig1->hasthis != sig2->hasthis ||
3017 sig1->param_count != sig2->param_count)
3018 return FALSE;
3021 * We're just comparing the signatures of two methods here:
3023 * If we have two generic methods `void Foo<U> (U u)' and `void Bar<V> (V v)',
3024 * U and V are equal here.
3026 * That's what the `signature_only' argument of do_mono_metadata_type_equal() is for.
3029 for (i = 0; i < sig1->param_count; i++) {
3030 MonoType *p1 = sig1->params[i];
3031 MonoType *p2 = sig2->params[i];
3033 /* if (p1->attrs != p2->attrs)
3034 return FALSE;
3036 if (!do_mono_metadata_type_equal (p1, p2, TRUE))
3037 return FALSE;
3040 if (!do_mono_metadata_type_equal (sig1->ret, sig2->ret, TRUE))
3041 return FALSE;
3042 return TRUE;
3045 guint
3046 mono_signature_hash (MonoMethodSignature *sig)
3048 guint i, res = sig->ret->type;
3050 for (i = 0; i < sig->param_count; i++)
3051 res = (res << 5) - res + sig->params[i]->type;
3053 return res;
3057 * mono_metadata_encode_value:
3058 * @value: value to encode
3059 * @buf: buffer where to write the compressed representation
3060 * @endbuf: pointer updated to point at the end of the encoded output
3062 * Encodes the value @value in the compressed representation used
3063 * in metadata and stores the result in @buf. @buf needs to be big
3064 * enough to hold the data (4 bytes).
3066 void
3067 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
3069 char *p = buf;
3071 if (value < 0x80)
3072 *p++ = value;
3073 else if (value < 0x4000) {
3074 p [0] = 0x80 | (value >> 8);
3075 p [1] = value & 0xff;
3076 p += 2;
3077 } else {
3078 p [0] = (value >> 24) | 0xc0;
3079 p [1] = (value >> 16) & 0xff;
3080 p [2] = (value >> 8) & 0xff;
3081 p [3] = value & 0xff;
3082 p += 4;
3084 if (endbuf)
3085 *endbuf = p;
3089 * mono_metadata_field_info:
3090 * @meta: the Image the field is defined in
3091 * @index: the index in the field table representing the field
3092 * @offset: a pointer to an integer where to store the offset that
3093 * may have been specified for the field in a FieldLayout table
3094 * @rva: a pointer to the RVA of the field data in the image that
3095 * may have been defined in a FieldRVA table
3096 * @marshal_spec: a pointer to the marshal spec that may have been
3097 * defined for the field in a FieldMarshal table.
3099 * Gather info for field @index that may have been defined in the FieldLayout,
3100 * FieldRVA and FieldMarshal tables.
3101 * Either of offset, rva and marshal_spec can be NULL if you're not interested
3102 * in the data.
3104 void
3105 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva,
3106 MonoMarshalSpec **marshal_spec)
3108 MonoTableInfo *tdef;
3109 locator_t loc;
3111 loc.idx = index + 1;
3112 if (offset) {
3113 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
3115 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
3116 loc.t = tdef;
3118 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3119 *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
3120 } else {
3121 *offset = (guint32)-1;
3124 if (rva) {
3125 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
3127 loc.col_idx = MONO_FIELD_RVA_FIELD;
3128 loc.t = tdef;
3130 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3132 * LAMESPEC: There is no signature, no nothing, just the raw data.
3134 *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
3135 } else {
3136 *rva = 0;
3139 if (marshal_spec) {
3140 const char *p;
3142 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
3143 *marshal_spec = mono_metadata_parse_marshal_spec (meta, p);
3150 * mono_metadata_get_constant_index:
3151 * @meta: the Image the field is defined in
3152 * @index: the token that may have a row defined in the constants table
3153 * @hint: possible position for the row
3155 * @token must be a FieldDef, ParamDef or PropertyDef token.
3157 * Returns: the index into the Constants table or 0 if not found.
3159 guint32
3160 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
3162 MonoTableInfo *tdef;
3163 locator_t loc;
3164 guint32 index = mono_metadata_token_index (token);
3166 tdef = &meta->tables [MONO_TABLE_CONSTANT];
3167 index <<= MONO_HASCONSTANT_BITS;
3168 switch (mono_metadata_token_table (token)) {
3169 case MONO_TABLE_FIELD:
3170 index |= MONO_HASCONSTANT_FIEDDEF;
3171 break;
3172 case MONO_TABLE_PARAM:
3173 index |= MONO_HASCONSTANT_PARAM;
3174 break;
3175 case MONO_TABLE_PROPERTY:
3176 index |= MONO_HASCONSTANT_PROPERTY;
3177 break;
3178 default:
3179 g_warning ("Not a valid token for the constant table: 0x%08x", token);
3180 return 0;
3182 loc.idx = index;
3183 loc.col_idx = MONO_CONSTANT_PARENT;
3184 loc.t = tdef;
3186 if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
3187 return hint;
3189 if (tdef->base && bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
3190 return loc.result + 1;
3192 return 0;
3196 * mono_metadata_events_from_typedef:
3197 * @meta: metadata context
3198 * @index: 0-based index (in the TypeDef table) describing a type
3200 * Returns: the 0-based index in the Event table for the events in the
3201 * type. The last event that belongs to the type (plus 1) is stored
3202 * in the @end_idx pointer.
3204 guint32
3205 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3207 locator_t loc;
3208 guint32 start, end;
3209 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_EVENTMAP];
3211 *end_idx = 0;
3213 if (!tdef->base)
3214 return 0;
3216 loc.t = tdef;
3217 loc.col_idx = MONO_EVENT_MAP_PARENT;
3218 loc.idx = index + 1;
3220 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3221 return 0;
3223 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
3224 if (loc.result + 1 < tdef->rows) {
3225 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
3226 } else {
3227 end = meta->tables [MONO_TABLE_EVENT].rows;
3230 *end_idx = end;
3231 return start - 1;
3235 * mono_metadata_methods_from_event:
3236 * @meta: metadata context
3237 * @index: 0-based index (in the Event table) describing a event
3239 * Returns: the 0-based index in the MethodDef table for the methods in the
3240 * event. The last method that belongs to the event (plus 1) is stored
3241 * in the @end_idx pointer.
3243 guint32
3244 mono_metadata_methods_from_event (MonoImage *meta, guint32 index, guint *end_idx)
3246 locator_t loc;
3247 guint start, end;
3248 guint32 cols [MONO_METHOD_SEMA_SIZE];
3249 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3251 *end_idx = 0;
3252 if (!msemt->base)
3253 return 0;
3255 loc.t = msemt;
3256 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3257 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
3259 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3260 return 0;
3262 start = loc.result;
3264 * We may end up in the middle of the rows...
3266 while (start > 0) {
3267 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3268 start--;
3269 else
3270 break;
3272 end = start + 1;
3273 while (end < msemt->rows) {
3274 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3275 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3276 break;
3277 ++end;
3279 *end_idx = end;
3280 return start;
3284 * mono_metadata_properties_from_typedef:
3285 * @meta: metadata context
3286 * @index: 0-based index (in the TypeDef table) describing a type
3288 * Returns: the 0-based index in the Property table for the properties in the
3289 * type. The last property that belongs to the type (plus 1) is stored
3290 * in the @end_idx pointer.
3292 guint32
3293 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
3295 locator_t loc;
3296 guint32 start, end;
3297 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_PROPERTYMAP];
3299 *end_idx = 0;
3301 if (!tdef->base)
3302 return 0;
3304 loc.t = tdef;
3305 loc.col_idx = MONO_PROPERTY_MAP_PARENT;
3306 loc.idx = index + 1;
3308 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3309 return 0;
3311 start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
3312 if (loc.result + 1 < tdef->rows) {
3313 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
3314 } else {
3315 end = meta->tables [MONO_TABLE_PROPERTY].rows;
3318 *end_idx = end;
3319 return start - 1;
3323 * mono_metadata_methods_from_property:
3324 * @meta: metadata context
3325 * @index: 0-based index (in the PropertyDef table) describing a property
3327 * Returns: the 0-based index in the MethodDef table for the methods in the
3328 * property. The last method that belongs to the property (plus 1) is stored
3329 * in the @end_idx pointer.
3331 guint32
3332 mono_metadata_methods_from_property (MonoImage *meta, guint32 index, guint *end_idx)
3334 locator_t loc;
3335 guint start, end;
3336 guint32 cols [MONO_METHOD_SEMA_SIZE];
3337 MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
3339 *end_idx = 0;
3340 if (!msemt->base)
3341 return 0;
3343 loc.t = msemt;
3344 loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
3345 loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
3347 if (!bsearch (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
3348 return 0;
3350 start = loc.result;
3352 * We may end up in the middle of the rows...
3354 while (start > 0) {
3355 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
3356 start--;
3357 else
3358 break;
3360 end = start + 1;
3361 while (end < msemt->rows) {
3362 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
3363 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
3364 break;
3365 ++end;
3367 *end_idx = end;
3368 return start;
3371 guint32
3372 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
3374 locator_t loc;
3375 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_IMPLMAP];
3377 if (!tdef->base)
3378 return 0;
3380 loc.t = tdef;
3381 loc.col_idx = MONO_IMPLMAP_MEMBER;
3382 loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
3384 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3385 return 0;
3387 return loc.result + 1;
3391 * @image: context where the image is created
3392 * @type_spec: typespec token
3394 * Creates a MonoType representing the TypeSpec indexed by the @type_spec
3395 * token.
3397 MonoType *
3398 mono_type_create_from_typespec_full (MonoImage *image, MonoGenericContext *generic_context, guint32 type_spec)
3400 guint32 idx = mono_metadata_token_index (type_spec);
3401 MonoTableInfo *t;
3402 guint32 cols [MONO_TYPESPEC_SIZE];
3403 const char *ptr;
3404 guint32 len;
3405 MonoType *type;
3407 mono_loader_lock ();
3409 if ((type = g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec)))) {
3410 mono_loader_unlock ();
3411 return type;
3414 t = &image->tables [MONO_TABLE_TYPESPEC];
3416 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
3417 ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
3418 len = mono_metadata_decode_value (ptr, &ptr);
3420 type = g_new0 (MonoType, 1);
3422 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type);
3424 if (*ptr == MONO_TYPE_BYREF) {
3425 type->byref = 1;
3426 ptr++;
3429 do_mono_metadata_parse_type (type, image, generic_context, ptr, &ptr);
3431 mono_loader_unlock ();
3433 return type;
3436 MonoType *
3437 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
3439 return mono_type_create_from_typespec_full (image, NULL, type_spec);
3442 MonoMarshalSpec *
3443 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
3445 MonoMarshalSpec *res;
3446 int len;
3447 const char *start = ptr;
3449 /* fixme: this is incomplete, but I cant find more infos in the specs */
3451 res = g_new0 (MonoMarshalSpec, 1);
3453 len = mono_metadata_decode_value (ptr, &ptr);
3454 res->native = *ptr++;
3456 if (res->native == MONO_NATIVE_LPARRAY) {
3457 if (ptr - start <= len)
3458 res->data.array_data.elem_type = *ptr++;
3459 if (ptr - start <= len)
3460 res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
3461 if (ptr - start <= len)
3462 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3465 if (res->native == MONO_NATIVE_BYVALTSTR) {
3466 if (ptr - start <= len)
3467 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3470 if (res->native == MONO_NATIVE_BYVALARRAY) {
3471 if (ptr - start <= len)
3472 res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
3475 if (res->native == MONO_NATIVE_CUSTOM) {
3476 /* skip unused type guid */
3477 len = mono_metadata_decode_value (ptr, &ptr);
3478 ptr += len;
3479 /* skip unused native type name */
3480 len = mono_metadata_decode_value (ptr, &ptr);
3481 ptr += len;
3482 /* read custom marshaler type name */
3483 len = mono_metadata_decode_value (ptr, &ptr);
3484 res->data.custom_data.custom_name = g_strndup (ptr, len);
3485 ptr += len;
3486 /* read cookie string */
3487 len = mono_metadata_decode_value (ptr, &ptr);
3488 res->data.custom_data.cookie = g_strndup (ptr, len);
3491 return res;
3494 void
3495 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
3497 if (spec->native == MONO_NATIVE_CUSTOM) {
3498 g_free (spec->data.custom_data.custom_name);
3499 g_free (spec->data.custom_data.cookie);
3501 g_free (spec);
3504 guint32
3505 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
3506 gboolean unicode, MonoMarshalConv *conv)
3508 MonoMarshalConv dummy_conv;
3509 int t = type->type;
3511 if (!conv)
3512 conv = &dummy_conv;
3514 *conv = MONO_MARSHAL_CONV_NONE;
3516 if (type->byref)
3517 return MONO_NATIVE_UINT;
3519 handle_enum:
3520 switch (t) {
3521 case MONO_TYPE_BOOLEAN:
3522 if (mspec) {
3523 switch (mspec->native) {
3524 case MONO_NATIVE_VARIANTBOOL:
3525 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
3526 return MONO_NATIVE_VARIANTBOOL;
3527 case MONO_NATIVE_BOOLEAN:
3528 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3529 return MONO_NATIVE_BOOLEAN;
3530 case MONO_NATIVE_I1:
3531 case MONO_NATIVE_U1:
3532 return mspec->native;
3533 default:
3534 g_error ("cant marshal bool to native type %02x", mspec->native);
3537 *conv = MONO_MARSHAL_CONV_BOOL_I4;
3538 return MONO_NATIVE_BOOLEAN;
3539 case MONO_TYPE_CHAR: return MONO_NATIVE_U2;
3540 case MONO_TYPE_I1: return MONO_NATIVE_I1;
3541 case MONO_TYPE_U1: return MONO_NATIVE_U1;
3542 case MONO_TYPE_I2: return MONO_NATIVE_I2;
3543 case MONO_TYPE_U2: return MONO_NATIVE_U2;
3544 case MONO_TYPE_I4: return MONO_NATIVE_I4;
3545 case MONO_TYPE_U4: return MONO_NATIVE_U4;
3546 case MONO_TYPE_I8: return MONO_NATIVE_I8;
3547 case MONO_TYPE_U8: return MONO_NATIVE_U8;
3548 case MONO_TYPE_R4: return MONO_NATIVE_R4;
3549 case MONO_TYPE_R8: return MONO_NATIVE_R8;
3550 case MONO_TYPE_STRING:
3551 if (mspec) {
3552 switch (mspec->native) {
3553 case MONO_NATIVE_BSTR:
3554 *conv = MONO_MARSHAL_CONV_STR_BSTR;
3555 return MONO_NATIVE_BSTR;
3556 case MONO_NATIVE_LPSTR:
3557 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
3558 return MONO_NATIVE_LPSTR;
3559 case MONO_NATIVE_LPWSTR:
3560 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
3561 return MONO_NATIVE_LPWSTR;
3562 case MONO_NATIVE_LPTSTR:
3563 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3564 return MONO_NATIVE_LPTSTR;
3565 case MONO_NATIVE_ANSIBSTR:
3566 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
3567 return MONO_NATIVE_ANSIBSTR;
3568 case MONO_NATIVE_TBSTR:
3569 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
3570 return MONO_NATIVE_TBSTR;
3571 case MONO_NATIVE_BYVALTSTR:
3572 if (unicode)
3573 *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
3574 else
3575 *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
3576 return MONO_NATIVE_BYVALTSTR;
3577 default:
3578 g_error ("Can not marshal string to native type '%02x': Invalid managed/unmanaged type combination (String fields must be paired with LPStr, LPWStr, BStr or ByValTStr).", mspec->native);
3581 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
3582 return MONO_NATIVE_LPTSTR;
3583 case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
3584 case MONO_TYPE_VALUETYPE: /*FIXME*/
3585 if (type->data.klass->enumtype) {
3586 t = type->data.klass->enum_basetype->type;
3587 goto handle_enum;
3589 return MONO_NATIVE_STRUCT;
3590 case MONO_TYPE_SZARRAY:
3591 case MONO_TYPE_ARRAY:
3592 if (mspec) {
3593 switch (mspec->native) {
3594 case MONO_NATIVE_BYVALARRAY:
3595 *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
3596 return MONO_NATIVE_BYVALARRAY;
3597 case MONO_NATIVE_SAFEARRAY:
3598 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
3599 return MONO_NATIVE_SAFEARRAY;
3600 case MONO_NATIVE_LPARRAY:
3601 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3602 return MONO_NATIVE_LPARRAY;
3603 default:
3604 g_error ("cant marshal array as native type %02x", mspec->native);
3608 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
3609 return MONO_NATIVE_LPARRAY;
3610 case MONO_TYPE_I: return MONO_NATIVE_INT;
3611 case MONO_TYPE_U: return MONO_NATIVE_UINT;
3612 case MONO_TYPE_CLASS:
3613 case MONO_TYPE_OBJECT: {
3614 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
3615 if (mspec) {
3616 switch (mspec->native) {
3617 case MONO_NATIVE_STRUCT:
3618 return MONO_NATIVE_STRUCT;
3619 case MONO_NATIVE_INTERFACE:
3620 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
3621 return MONO_NATIVE_INTERFACE;
3622 case MONO_NATIVE_IDISPATCH:
3623 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
3624 return MONO_NATIVE_IDISPATCH;
3625 case MONO_NATIVE_IUNKNOWN:
3626 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
3627 return MONO_NATIVE_IUNKNOWN;
3628 case MONO_NATIVE_FUNC:
3629 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3630 type->data.klass == mono_defaults.delegate_class ||
3631 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3632 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3633 return MONO_NATIVE_FUNC;
3635 else
3636 /* Fall through */
3638 default:
3639 g_error ("cant marshal object as native type %02x", mspec->native);
3642 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
3643 type->data.klass == mono_defaults.delegate_class ||
3644 type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
3645 *conv = MONO_MARSHAL_CONV_DEL_FTN;
3646 return MONO_NATIVE_FUNC;
3648 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
3649 return MONO_NATIVE_STRUCT;
3651 case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
3652 case MONO_TYPE_TYPEDBYREF:
3653 default:
3654 g_error ("type 0x%02x not handled in marshal", t);
3656 return MONO_NATIVE_MAX;
3659 const char*
3660 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
3662 locator_t loc;
3663 MonoTableInfo *tdef = &meta->tables [MONO_TABLE_FIELDMARSHAL];
3665 if (!tdef->base)
3666 return NULL;
3668 loc.t = tdef;
3669 loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
3670 loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
3672 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3673 return NULL;
3675 return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
3678 static MonoMethod*
3679 method_from_method_def_or_ref (MonoImage *m, guint32 tok)
3681 guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
3682 switch (tok & MONO_METHODDEFORREF_MASK) {
3683 case MONO_METHODDEFORREF_METHODDEF:
3684 return mono_get_method (m, MONO_TOKEN_METHOD_DEF | idx, NULL);
3685 case MONO_METHODDEFORREF_METHODREF:
3686 return mono_get_method (m, MONO_TOKEN_MEMBER_REF | idx, NULL);
3688 g_assert_not_reached ();
3689 return NULL;
3692 MonoMethod**
3693 mono_class_get_overrides (MonoImage *image, guint32 type_token, gint32 *num_overrides)
3695 locator_t loc;
3696 MonoTableInfo *tdef = &image->tables [MONO_TABLE_METHODIMPL];
3697 guint32 start, end;
3698 gint32 i, num;
3699 guint32 cols [MONO_METHODIMPL_SIZE];
3700 MonoMethod **result;
3702 if (num_overrides)
3703 *num_overrides = 0;
3705 if (!tdef->base)
3706 return NULL;
3708 loc.t = tdef;
3709 loc.col_idx = MONO_METHODIMPL_CLASS;
3710 loc.idx = mono_metadata_token_index (type_token);
3712 if (!bsearch (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
3713 return NULL;
3715 start = loc.result;
3716 end = start + 1;
3718 * We may end up in the middle of the rows...
3720 while (start > 0) {
3721 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
3722 start--;
3723 else
3724 break;
3726 while (end < tdef->rows) {
3727 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
3728 end++;
3729 else
3730 break;
3732 num = end - start;
3733 result = g_new (MonoMethod*, num * 2);
3734 for (i = 0; i < num; ++i) {
3735 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
3736 result [i * 2] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_DECLARATION]);
3737 result [i * 2 + 1] = method_from_method_def_or_ref (image, cols [MONO_METHODIMPL_BODY]);
3740 if (num_overrides)
3741 *num_overrides = num;
3742 return result;
3746 * mono_guid_to_string:
3748 * Converts a 16 byte Microsoft GUID to the standard string representation.
3750 char *
3751 mono_guid_to_string (const guint8 *guid)
3753 return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
3754 guid[3], guid[2], guid[1], guid[0],
3755 guid[5], guid[4],
3756 guid[7], guid[6],
3757 guid[8], guid[9],
3758 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
3761 static MonoClass**
3762 get_constraints (MonoImage *image, int owner, MonoGenericContext *context)
3764 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
3765 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
3766 guint32 i, token, found;
3767 MonoClass *klass, **res;
3768 GList *cons = NULL, *tmp;
3771 found = 0;
3772 for (i = 0; i < tdef->rows; ++i) {
3773 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
3774 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
3775 token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
3776 klass = mono_class_get_full (image, token, context);
3777 cons = g_list_append (cons, klass);
3778 ++found;
3779 } else {
3780 /* contiguous list finished */
3781 if (found)
3782 break;
3785 if (!found)
3786 return NULL;
3787 res = g_new0 (MonoClass*, found + 1);
3788 for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
3789 res [i] = tmp->data;
3791 g_list_free (cons);
3792 return res;
3795 MonoGenericContainer *
3796 mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericContainer *parent_container)
3798 MonoTableInfo *tdef = &image->tables [MONO_TABLE_GENERICPARAM];
3799 guint32 cols [MONO_GENERICPARAM_SIZE];
3800 guint32 i, owner = 0, last_num, n;
3801 MonoGenericContainer *container;
3802 MonoGenericParam *params;
3804 if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
3805 owner = MONO_TYPEORMETHOD_TYPE;
3806 else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3807 owner = MONO_TYPEORMETHOD_METHOD;
3808 else {
3809 g_error ("wrong token %x to load_generics_params", token);
3810 return NULL;
3812 owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
3813 if (!tdef->base)
3814 return NULL;
3816 for (i = 0; i < tdef->rows; ++i) {
3817 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3818 if (cols [MONO_GENERICPARAM_OWNER] == owner)
3819 break;
3821 last_num = i;
3822 if (i >= tdef->rows)
3823 return NULL;
3824 params = NULL;
3825 n = 0;
3826 container = g_new0 (MonoGenericContainer, 1);
3827 do {
3828 n++;
3829 params = g_realloc (params, sizeof (MonoGenericParam) * n);
3830 params [n - 1].owner = container;
3831 params [n - 1].pklass = NULL;
3832 params [n - 1].method = NULL;
3833 params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
3834 params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
3835 params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
3836 if (++i >= tdef->rows)
3837 break;
3838 mono_metadata_decode_row (tdef, i, cols, MONO_GENERICPARAM_SIZE);
3839 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
3841 container->type_argc = n;
3842 container->type_params = params;
3843 container->parent = parent_container;
3845 if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
3846 container->is_method = 1;
3848 container->context.container = container;
3850 for (i = 0; i < n; i++)
3851 params [i].constraints = get_constraints (image, last_num + i + 1, &container->context);
3853 return container;
3856 gboolean
3857 mono_type_is_byref (MonoType *type)
3859 return type->byref;
3863 mono_type_get_type (MonoType *type)
3865 return type->type;
3868 /* For MONO_TYPE_FNPTR */
3869 MonoMethodSignature*
3870 mono_type_get_signature (MonoType *type)
3872 return type->data.method;
3875 /* For MONO_TYPE_CLASS, VALUETYPE */
3876 MonoClass*
3877 mono_type_get_class (MonoType *type)
3879 return type->data.klass;
3882 /* For MONO_TYPE_ARRAY */
3883 MonoArrayType*
3884 mono_type_get_array_type (MonoType *type)
3886 return type->data.array;
3889 MonoClass*
3890 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
3892 /* FIXME: implement */
3893 return NULL;
3896 MonoType*
3897 mono_signature_get_return_type (MonoMethodSignature *sig)
3899 return sig->ret;
3902 MonoType*
3903 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
3905 MonoType** type;
3906 if (!iter)
3907 return NULL;
3908 if (!*iter) {
3909 /* start from the first */
3910 if (sig->param_count) {
3911 *iter = &sig->params [0];
3912 return sig->params [0];
3913 } else {
3914 /* no method */
3915 return NULL;
3918 type = *iter;
3919 type++;
3920 if (type < &sig->params [sig->param_count]) {
3921 *iter = type;
3922 return *type;
3924 return NULL;
3927 guint32
3928 mono_signature_get_param_count (MonoMethodSignature *sig)
3930 return sig->param_count;
3933 guint32
3934 mono_signature_get_call_conv (MonoMethodSignature *sig)
3936 return sig->call_convention;
3940 mono_signature_vararg_start (MonoMethodSignature *sig)
3942 return sig->sentinelpos;
3945 gboolean
3946 mono_signature_is_instance (MonoMethodSignature *sig)
3948 return sig->hasthis;
3951 gboolean
3952 mono_signature_explicit_this (MonoMethodSignature *sig)
3954 return sig->explicit_this;