2 * metadata.c: Routines for accessing the metadata
5 * Miguel de Icaza (miguel@ximian.com)
6 * Paolo Molaro (lupus@ximian.com)
8 * (C) 2001-2002 Ximian, Inc.
17 #include "tabledefs.h"
18 #include "mono-endian.h"
20 #include "tokentype.h"
21 #include "metadata-internals.h"
22 #include "class-internals.h"
26 static void do_mono_metadata_parse_type (MonoType
*type
, MonoImage
*m
, MonoGenericContext
*generic_context
,
27 const char *ptr
, const char **rptr
);
29 static gboolean
do_mono_metadata_type_equal (MonoType
*t1
, MonoType
*t2
, gboolean signature_only
);
30 static gboolean
mono_metadata_class_equal (MonoClass
*c1
, MonoClass
*c2
, gboolean signature_only
);
31 static gboolean
_mono_metadata_generic_class_equal (const MonoGenericClass
*g1
, const MonoGenericClass
*g2
,
32 gboolean signature_only
);
35 * Encoding of the "description" argument:
37 * identifier [CODE ARG]
39 * If CODE is ':', then a lookup on table ARG is performed
40 * If CODE is '=', then a lookup in the aliased-table ARG is performed
41 * If CODE is '#', then this encodes a flag, ARG is the flag name.
43 * Aliased table for example is `CustomAttributeType' which depending on the
44 * information might refer to different tables.
47 const static MonoMetaTable AssemblySchema
[] = {
48 { MONO_MT_UINT32
, "HashId" },
49 { MONO_MT_UINT16
, "Major" },
50 { MONO_MT_UINT16
, "Minor" },
51 { MONO_MT_UINT16
, "BuildNumber" },
52 { MONO_MT_UINT16
, "RevisionNumber" },
53 { MONO_MT_UINT32
, "Flags" },
54 { MONO_MT_BLOB_IDX
, "PublicKey" },
55 { MONO_MT_STRING_IDX
, "Name" },
56 { MONO_MT_STRING_IDX
, "Culture" },
60 const static MonoMetaTable AssemblyOSSchema
[] = {
61 { MONO_MT_UINT32
, "OSPlatformID" },
62 { MONO_MT_UINT32
, "OSMajor" },
63 { MONO_MT_UINT32
, "OSMinor" },
67 const static MonoMetaTable AssemblyProcessorSchema
[] = {
68 { MONO_MT_UINT32
, "Processor" },
72 const static MonoMetaTable AssemblyRefSchema
[] = {
73 { MONO_MT_UINT16
, "Major" },
74 { MONO_MT_UINT16
, "Minor" },
75 { MONO_MT_UINT16
, "Build" },
76 { MONO_MT_UINT16
, "Revision" },
77 { MONO_MT_UINT32
, "Flags" },
78 { MONO_MT_BLOB_IDX
, "PublicKeyOrToken" },
79 { MONO_MT_STRING_IDX
, "Name" },
80 { MONO_MT_STRING_IDX
, "Culture" },
81 { MONO_MT_BLOB_IDX
, "HashValue" },
85 const static MonoMetaTable AssemblyRefOSSchema
[] = {
86 { MONO_MT_UINT32
, "OSPlatformID" },
87 { MONO_MT_UINT32
, "OSMajorVersion" },
88 { MONO_MT_UINT32
, "OSMinorVersion" },
89 { MONO_MT_TABLE_IDX
, "AssemblyRef:AssemblyRef" },
93 const static MonoMetaTable AssemblyRefProcessorSchema
[] = {
94 { MONO_MT_UINT32
, "Processor" },
95 { MONO_MT_TABLE_IDX
, "AssemblyRef:AssemblyRef" },
99 const static MonoMetaTable ClassLayoutSchema
[] = {
100 { MONO_MT_UINT16
, "PackingSize" },
101 { MONO_MT_UINT32
, "ClassSize" },
102 { MONO_MT_TABLE_IDX
, "Parent:TypeDef" },
103 { MONO_MT_END
, NULL
}
106 const static MonoMetaTable ConstantSchema
[] = {
107 { MONO_MT_UINT8
, "Type" },
108 { MONO_MT_UINT8
, "PaddingZero" },
109 { MONO_MT_CONST_IDX
, "Parent" },
110 { MONO_MT_BLOB_IDX
, "Value" },
111 { MONO_MT_END
, NULL
}
114 const static MonoMetaTable CustomAttributeSchema
[] = {
115 { MONO_MT_HASCAT_IDX
, "Parent" },
116 { MONO_MT_CAT_IDX
, "Type" },
117 { MONO_MT_BLOB_IDX
, "Value" },
118 { MONO_MT_END
, NULL
}
121 const static MonoMetaTable DeclSecuritySchema
[] = {
122 { MONO_MT_UINT16
, "Action" },
123 { MONO_MT_HASDEC_IDX
, "Parent" },
124 { MONO_MT_BLOB_IDX
, "PermissionSet" },
125 { MONO_MT_END
, NULL
}
128 const static MonoMetaTable EventMapSchema
[] = {
129 { MONO_MT_TABLE_IDX
, "Parent:TypeDef" },
130 { MONO_MT_TABLE_IDX
, "EventList:Event" },
131 { MONO_MT_END
, NULL
}
134 const static MonoMetaTable EventSchema
[] = {
135 { MONO_MT_UINT16
, "EventFlags#EventAttribute" },
136 { MONO_MT_STRING_IDX
, "Name" },
137 { MONO_MT_TABLE_IDX
, "EventType" }, /* TypeDef or TypeRef */
138 { MONO_MT_END
, NULL
}
141 const static MonoMetaTable ExportedTypeSchema
[] = {
142 { MONO_MT_UINT32
, "Flags" },
143 { MONO_MT_TABLE_IDX
, "TypeDefId" },
144 { MONO_MT_STRING_IDX
, "TypeName" },
145 { MONO_MT_STRING_IDX
, "TypeNameSpace" },
146 { MONO_MT_IMPL_IDX
, "Implementation" },
147 { MONO_MT_END
, NULL
}
150 const static MonoMetaTable FieldSchema
[] = {
151 { MONO_MT_UINT16
, "Flags" },
152 { MONO_MT_STRING_IDX
, "Name" },
153 { MONO_MT_BLOB_IDX
, "Signature" },
154 { MONO_MT_END
, NULL
}
156 const static MonoMetaTable FieldLayoutSchema
[] = {
157 { MONO_MT_UINT32
, "Offset" },
158 { MONO_MT_TABLE_IDX
, "Field:Field" },
159 { MONO_MT_END
, NULL
}
162 const static MonoMetaTable FieldMarshalSchema
[] = {
163 { MONO_MT_HFM_IDX
, "Parent" },
164 { MONO_MT_BLOB_IDX
, "NativeType" },
165 { MONO_MT_END
, NULL
}
167 const static MonoMetaTable FieldRVASchema
[] = {
168 { MONO_MT_UINT32
, "RVA" },
169 { MONO_MT_TABLE_IDX
, "Field:Field" },
170 { MONO_MT_END
, NULL
}
173 const static MonoMetaTable FileSchema
[] = {
174 { MONO_MT_UINT32
, "Flags" },
175 { MONO_MT_STRING_IDX
, "Name" },
176 { MONO_MT_BLOB_IDX
, "Value" },
177 { MONO_MT_END
, NULL
}
180 const static MonoMetaTable ImplMapSchema
[] = {
181 { MONO_MT_UINT16
, "MappingFlag" },
182 { MONO_MT_MF_IDX
, "MemberForwarded" },
183 { MONO_MT_STRING_IDX
, "ImportName" },
184 { MONO_MT_TABLE_IDX
, "ImportScope:ModuleRef" },
185 { MONO_MT_END
, NULL
}
188 const static MonoMetaTable InterfaceImplSchema
[] = {
189 { MONO_MT_TABLE_IDX
, "Class:TypeDef" },
190 { MONO_MT_TDOR_IDX
, "Interface=TypeDefOrRef" },
191 { MONO_MT_END
, NULL
}
194 const static MonoMetaTable ManifestResourceSchema
[] = {
195 { MONO_MT_UINT32
, "Offset" },
196 { MONO_MT_UINT32
, "Flags" },
197 { MONO_MT_STRING_IDX
, "Name" },
198 { MONO_MT_IMPL_IDX
, "Implementation" },
199 { MONO_MT_END
, NULL
}
202 const static MonoMetaTable MemberRefSchema
[] = {
203 { MONO_MT_MRP_IDX
, "Class" },
204 { MONO_MT_STRING_IDX
, "Name" },
205 { MONO_MT_BLOB_IDX
, "Signature" },
206 { MONO_MT_END
, NULL
}
209 const static MonoMetaTable MethodSchema
[] = {
210 { MONO_MT_UINT32
, "RVA" },
211 { MONO_MT_UINT16
, "ImplFlags#MethodImplAttributes" },
212 { MONO_MT_UINT16
, "Flags#MethodAttribute" },
213 { MONO_MT_STRING_IDX
, "Name" },
214 { MONO_MT_BLOB_IDX
, "Signature" },
215 { MONO_MT_TABLE_IDX
, "ParamList:Param" },
216 { MONO_MT_END
, NULL
}
219 const static MonoMetaTable MethodImplSchema
[] = {
220 { MONO_MT_TABLE_IDX
, "Class:TypeDef" },
221 { MONO_MT_MDOR_IDX
, "MethodBody" },
222 { MONO_MT_MDOR_IDX
, "MethodDeclaration" },
223 { MONO_MT_END
, NULL
}
226 const static MonoMetaTable MethodSemanticsSchema
[] = {
227 { MONO_MT_UINT16
, "MethodSemantic" },
228 { MONO_MT_TABLE_IDX
, "Method:Method" },
229 { MONO_MT_HS_IDX
, "Association" },
230 { MONO_MT_END
, NULL
}
233 const static MonoMetaTable ModuleSchema
[] = {
234 { MONO_MT_UINT16
, "Generation" },
235 { MONO_MT_STRING_IDX
, "Name" },
236 { MONO_MT_GUID_IDX
, "MVID" },
237 { MONO_MT_GUID_IDX
, "EncID" },
238 { MONO_MT_GUID_IDX
, "EncBaseID" },
239 { MONO_MT_END
, NULL
}
242 const static MonoMetaTable ModuleRefSchema
[] = {
243 { MONO_MT_STRING_IDX
, "Name" },
244 { MONO_MT_END
, NULL
}
247 const static MonoMetaTable NestedClassSchema
[] = {
248 { MONO_MT_TABLE_IDX
, "NestedClass:TypeDef" },
249 { MONO_MT_TABLE_IDX
, "EnclosingClass:TypeDef" },
250 { MONO_MT_END
, NULL
}
253 const static MonoMetaTable ParamSchema
[] = {
254 { MONO_MT_UINT16
, "Flags" },
255 { MONO_MT_UINT16
, "Sequence" },
256 { MONO_MT_STRING_IDX
, "Name" },
257 { MONO_MT_END
, NULL
}
260 const static MonoMetaTable PropertySchema
[] = {
261 { MONO_MT_UINT16
, "Flags" },
262 { MONO_MT_STRING_IDX
, "Name" },
263 { MONO_MT_BLOB_IDX
, "Type" },
264 { MONO_MT_END
, NULL
}
267 const static MonoMetaTable PropertyMapSchema
[] = {
268 { MONO_MT_TABLE_IDX
, "Parent:TypeDef" },
269 { MONO_MT_TABLE_IDX
, "PropertyList:Property" },
270 { MONO_MT_END
, NULL
}
273 const static MonoMetaTable StandaloneSigSchema
[] = {
274 { MONO_MT_BLOB_IDX
, "Signature" },
275 { MONO_MT_END
, NULL
}
278 const static MonoMetaTable TypeDefSchema
[] = {
279 { MONO_MT_UINT32
, "Flags" },
280 { MONO_MT_STRING_IDX
, "Name" },
281 { MONO_MT_STRING_IDX
, "Namespace" },
282 { MONO_MT_TDOR_IDX
, "Extends" },
283 { MONO_MT_TABLE_IDX
, "FieldList:Field" },
284 { MONO_MT_TABLE_IDX
, "MethodList:Method" },
285 { MONO_MT_END
, NULL
}
288 const static MonoMetaTable TypeRefSchema
[] = {
289 { MONO_MT_RS_IDX
, "ResolutionScope=ResolutionScope" },
290 { MONO_MT_STRING_IDX
, "Name" },
291 { MONO_MT_STRING_IDX
, "Namespace" },
292 { MONO_MT_END
, NULL
}
295 const static MonoMetaTable TypeSpecSchema
[] = {
296 { MONO_MT_BLOB_IDX
, "Signature" },
297 { MONO_MT_END
, NULL
}
300 const static MonoMetaTable GenericParamSchema
[] = {
301 { MONO_MT_UINT16
, "Number" },
302 { MONO_MT_UINT16
, "Flags" },
303 { MONO_MT_TABLE_IDX
, "Owner" }, /* TypeDef or MethodDef */
304 { MONO_MT_STRING_IDX
, "Name" },
306 /* soon to be removed */
307 { MONO_MT_TABLE_IDX
, "Kind" },
309 { MONO_MT_END
, NULL
}
312 const static MonoMetaTable MethodSpecSchema
[] = {
313 { MONO_MT_MDOR_IDX
, "Method" },
314 { MONO_MT_BLOB_IDX
, "Signature" },
315 { MONO_MT_END
, NULL
}
318 const static MonoMetaTable GenericParamConstraintSchema
[] = {
319 { MONO_MT_TABLE_IDX
, "GenericParam" },
320 { MONO_MT_TDOR_IDX
, "Constraint" },
321 { MONO_MT_END
, NULL
}
324 const static struct {
325 const MonoMetaTable
*table
;
328 /* 0 */ { ModuleSchema
, "Module" },
329 /* 1 */ { TypeRefSchema
, "TypeRef" },
330 /* 2 */ { TypeDefSchema
, "TypeDef" },
331 /* 3 */ { NULL
, NULL
},
332 /* 4 */ { FieldSchema
, "Field" },
333 /* 5 */ { NULL
, NULL
},
334 /* 6 */ { MethodSchema
, "Method" },
335 /* 7 */ { NULL
, NULL
},
336 /* 8 */ { ParamSchema
, "Param" },
337 /* 9 */ { InterfaceImplSchema
, "InterfaceImpl" },
338 /* A */ { MemberRefSchema
, "MemberRef" },
339 /* B */ { ConstantSchema
, "Constant" },
340 /* C */ { CustomAttributeSchema
, "CustomAttribute" },
341 /* D */ { FieldMarshalSchema
, "FieldMarshal" },
342 /* E */ { DeclSecuritySchema
, "DeclSecurity" },
343 /* F */ { ClassLayoutSchema
, "ClassLayout" },
344 /* 10 */ { FieldLayoutSchema
, "FieldLayout" },
345 /* 11 */ { StandaloneSigSchema
, "StandaloneSig" },
346 /* 12 */ { EventMapSchema
, "EventMap" },
347 /* 13 */ { NULL
, NULL
},
348 /* 14 */ { EventSchema
, "Event" },
349 /* 15 */ { PropertyMapSchema
, "PropertyMap" },
350 /* 16 */ { NULL
, NULL
},
351 /* 17 */ { PropertySchema
, "PropertyTable" },
352 /* 18 */ { MethodSemanticsSchema
, "MethodSemantics" },
353 /* 19 */ { MethodImplSchema
, "MethodImpl" },
354 /* 1A */ { ModuleRefSchema
, "ModuleRef" },
355 /* 1B */ { TypeSpecSchema
, "TypeSpec" },
356 /* 1C */ { ImplMapSchema
, "ImplMap" },
357 /* 1D */ { FieldRVASchema
, "FieldRVA" },
358 /* 1E */ { NULL
, NULL
},
359 /* 1F */ { NULL
, NULL
},
360 /* 20 */ { AssemblySchema
, "Assembly" },
361 /* 21 */ { AssemblyProcessorSchema
, "AssemblyProcessor" },
362 /* 22 */ { AssemblyOSSchema
, "AssemblyOS" },
363 /* 23 */ { AssemblyRefSchema
, "AssemblyRef" },
364 /* 24 */ { AssemblyRefProcessorSchema
, "AssemblyRefProcessor" },
365 /* 25 */ { AssemblyRefOSSchema
, "AssemblyRefOS" },
366 /* 26 */ { FileSchema
, "File" },
367 /* 27 */ { ExportedTypeSchema
, "ExportedType" },
368 /* 28 */ { ManifestResourceSchema
, "ManifestResource" },
369 /* 29 */ { NestedClassSchema
, "NestedClass" },
370 /* 2A */ { GenericParamSchema
, "GenericParam" },
371 /* 2B */ { MethodSpecSchema
, "MethodSpec" },
372 /* 2C */ { GenericParamConstraintSchema
, "GenericParamConstraint" },
376 * mono_meta_table_name:
377 * @table: table index
379 * Returns: the name for the @table index
382 mono_meta_table_name (int table
)
384 if ((table
< 0) || (table
> 0x2c))
387 return tables
[table
].name
;
390 /* The guy who wrote the spec for this should not be allowed near a
393 If e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1,
394 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of
395 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of
396 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the
397 inverse of this mapping.
400 #define rtsize(s,b) (((s) < (1 << (b)) ? 2 : 4))
401 #define idx_size(tableidx) (meta->tables [(tableidx)].rows < 65536 ? 2 : 4)
403 /* Reference: Partition II - 23.2.6 */
405 * mono_metadata_compute_size:
406 * @meta: metadata context
407 * @tableindex: metadata table number
408 * @result_bitfield: pointer to guint32 where to store additional info
410 * mono_metadata_compute_size() computes the lenght in bytes of a single
411 * row in a metadata table. The size of each column is encoded in the
412 * @result_bitfield return value along with the number of columns in the table.
413 * the resulting bitfield should be handed to the mono_metadata_table_size()
414 * and mono_metadata_table_count() macros.
417 mono_metadata_compute_size (MonoImage
*meta
, int tableindex
, guint32
*result_bitfield
)
419 guint32 bitfield
= 0;
420 int size
= 0, field_size
= 0;
423 const MonoMetaTable
*table
= tables
[tableindex
].table
;
425 for (i
= 0; (code
= table
[i
].code
) != MONO_MT_END
; i
++){
428 field_size
= 4; break;
431 field_size
= 2; break;
434 field_size
= 1; break;
436 case MONO_MT_BLOB_IDX
:
437 field_size
= meta
->idx_blob_wide
? 4 : 2; break;
439 case MONO_MT_STRING_IDX
:
440 field_size
= meta
->idx_string_wide
? 4 : 2; break;
442 case MONO_MT_GUID_IDX
:
443 field_size
= meta
->idx_guid_wide
? 4 : 2; break;
445 case MONO_MT_TABLE_IDX
:
446 /* Uhm, a table index can point to other tables besides the current one
447 * so, it's not correct to use the rowcount of the current table to
448 * get the size for this column - lupus
450 switch (tableindex
) {
451 case MONO_TABLE_ASSEMBLYREFOS
:
453 field_size
= idx_size (MONO_TABLE_ASSEMBLYREF
); break;
454 case MONO_TABLE_ASSEMBLYPROCESSOR
:
456 field_size
= idx_size (MONO_TABLE_ASSEMBLYREF
); break;
457 case MONO_TABLE_CLASSLAYOUT
:
459 field_size
= idx_size (MONO_TABLE_TYPEDEF
); break;
460 case MONO_TABLE_EVENTMAP
:
461 g_assert (i
== 0 || i
== 1);
462 field_size
= i
? idx_size (MONO_TABLE_EVENT
):
463 idx_size(MONO_TABLE_TYPEDEF
);
465 case MONO_TABLE_EVENT
:
467 field_size
= MAX (idx_size (MONO_TABLE_TYPEDEF
), idx_size(MONO_TABLE_TYPEREF
));
468 field_size
= MAX (field_size
, idx_size(MONO_TABLE_TYPESPEC
));
470 case MONO_TABLE_EXPORTEDTYPE
:
472 /* the index is in another metadata file, so it must be 4 */
473 field_size
= 4; break;
474 case MONO_TABLE_FIELDLAYOUT
:
476 field_size
= idx_size (MONO_TABLE_FIELD
); break;
477 case MONO_TABLE_FIELDRVA
:
479 field_size
= idx_size (MONO_TABLE_FIELD
); break;
480 case MONO_TABLE_IMPLMAP
:
482 field_size
= idx_size (MONO_TABLE_MODULEREF
); break;
483 case MONO_TABLE_INTERFACEIMPL
:
485 field_size
= idx_size (MONO_TABLE_TYPEDEF
); break;
486 case MONO_TABLE_METHOD
:
488 field_size
= idx_size (MONO_TABLE_PARAM
); break;
489 case MONO_TABLE_METHODIMPL
:
491 field_size
= idx_size (MONO_TABLE_TYPEDEF
); break;
492 case MONO_TABLE_METHODSEMANTICS
:
494 field_size
= idx_size (MONO_TABLE_METHOD
); break;
495 case MONO_TABLE_NESTEDCLASS
:
496 g_assert (i
== 0 || i
== 1);
497 field_size
= idx_size (MONO_TABLE_TYPEDEF
); break;
498 case MONO_TABLE_PROPERTYMAP
:
499 g_assert (i
== 0 || i
== 1);
500 field_size
= i
? idx_size (MONO_TABLE_PROPERTY
):
501 idx_size(MONO_TABLE_TYPEDEF
);
503 case MONO_TABLE_TYPEDEF
:
504 g_assert (i
== 4 || i
== 5);
505 field_size
= i
== 4 ? idx_size (MONO_TABLE_FIELD
):
506 idx_size(MONO_TABLE_METHOD
);
507 case MONO_TABLE_GENERICPARAM
:
508 g_assert (i
== 2 || i
== 4 || i
== 5);
510 field_size
= MAX (idx_size (MONO_TABLE_METHOD
), idx_size (MONO_TABLE_TYPEDEF
));
512 field_size
= idx_size (MONO_TABLE_TYPEDEF
);
514 field_size
= idx_size (MONO_TABLE_TYPEDEF
);
517 case MONO_TABLE_GENERICPARAMCONSTRAINT
:
519 field_size
= idx_size (MONO_TABLE_GENERICPARAM
);
523 g_assert_not_reached ();
525 if (tableindex
!= MONO_TABLE_EXPORTEDTYPE
&& field_size
!= idx_size (tableindex
))
526 g_warning ("size changed (%d to %d)", idx_size (tableindex
), field_size
);
531 * HasConstant: ParamDef, FieldDef, Property
533 case MONO_MT_CONST_IDX
:
534 n
= MAX (meta
->tables
[MONO_TABLE_PARAM
].rows
,
535 meta
->tables
[MONO_TABLE_FIELD
].rows
);
536 n
= MAX (n
, meta
->tables
[MONO_TABLE_PROPERTY
].rows
);
538 /* 2 bits to encode tag */
539 field_size
= rtsize (n
, 16-2);
543 * HasCustomAttribute: points to any table but
546 case MONO_MT_HASCAT_IDX
:
548 * We believe that since the signature and
549 * permission are indexing the Blob heap,
550 * we should consider the blob size first
552 /* I'm not a believer - lupus
553 if (meta->idx_blob_wide){
558 n
= MAX (meta
->tables
[MONO_TABLE_METHOD
].rows
,
559 meta
->tables
[MONO_TABLE_FIELD
].rows
);
560 n
= MAX (n
, meta
->tables
[MONO_TABLE_TYPEREF
].rows
);
561 n
= MAX (n
, meta
->tables
[MONO_TABLE_TYPEDEF
].rows
);
562 n
= MAX (n
, meta
->tables
[MONO_TABLE_PARAM
].rows
);
563 n
= MAX (n
, meta
->tables
[MONO_TABLE_INTERFACEIMPL
].rows
);
564 n
= MAX (n
, meta
->tables
[MONO_TABLE_MEMBERREF
].rows
);
565 n
= MAX (n
, meta
->tables
[MONO_TABLE_MODULE
].rows
);
566 n
= MAX (n
, meta
->tables
[MONO_TABLE_DECLSECURITY
].rows
);
567 n
= MAX (n
, meta
->tables
[MONO_TABLE_PROPERTY
].rows
);
568 n
= MAX (n
, meta
->tables
[MONO_TABLE_EVENT
].rows
);
569 n
= MAX (n
, meta
->tables
[MONO_TABLE_STANDALONESIG
].rows
);
570 n
= MAX (n
, meta
->tables
[MONO_TABLE_MODULEREF
].rows
);
571 n
= MAX (n
, meta
->tables
[MONO_TABLE_TYPESPEC
].rows
);
572 n
= MAX (n
, meta
->tables
[MONO_TABLE_ASSEMBLY
].rows
);
573 n
= MAX (n
, meta
->tables
[MONO_TABLE_ASSEMBLYREF
].rows
);
574 n
= MAX (n
, meta
->tables
[MONO_TABLE_FILE
].rows
);
575 n
= MAX (n
, meta
->tables
[MONO_TABLE_EXPORTEDTYPE
].rows
);
576 n
= MAX (n
, meta
->tables
[MONO_TABLE_MANIFESTRESOURCE
].rows
);
578 /* 5 bits to encode */
579 field_size
= rtsize (n
, 16-5);
583 * CustomAttributeType: TypeDef, TypeRef, MethodDef,
584 * MemberRef and String.
586 case MONO_MT_CAT_IDX
:
587 /* String is a heap, if it is wide, we know the size */
589 if (meta->idx_string_wide){
594 n
= MAX (meta
->tables
[MONO_TABLE_TYPEREF
].rows
,
595 meta
->tables
[MONO_TABLE_TYPEDEF
].rows
);
596 n
= MAX (n
, meta
->tables
[MONO_TABLE_METHOD
].rows
);
597 n
= MAX (n
, meta
->tables
[MONO_TABLE_MEMBERREF
].rows
);
599 /* 3 bits to encode */
600 field_size
= rtsize (n
, 16-3);
604 * HasDeclSecurity: Typedef, MethodDef, Assembly
606 case MONO_MT_HASDEC_IDX
:
607 n
= MAX (meta
->tables
[MONO_TABLE_TYPEDEF
].rows
,
608 meta
->tables
[MONO_TABLE_METHOD
].rows
);
609 n
= MAX (n
, meta
->tables
[MONO_TABLE_ASSEMBLY
].rows
);
611 /* 2 bits to encode */
612 field_size
= rtsize (n
, 16-2);
616 * Implementation: File, AssemblyRef, ExportedType
618 case MONO_MT_IMPL_IDX
:
619 n
= MAX (meta
->tables
[MONO_TABLE_FILE
].rows
,
620 meta
->tables
[MONO_TABLE_ASSEMBLYREF
].rows
);
621 n
= MAX (n
, meta
->tables
[MONO_TABLE_EXPORTEDTYPE
].rows
);
623 /* 2 bits to encode tag */
624 field_size
= rtsize (n
, 16-2);
628 * HasFieldMarshall: FieldDef, ParamDef
630 case MONO_MT_HFM_IDX
:
631 n
= MAX (meta
->tables
[MONO_TABLE_FIELD
].rows
,
632 meta
->tables
[MONO_TABLE_PARAM
].rows
);
634 /* 1 bit used to encode tag */
635 field_size
= rtsize (n
, 16-1);
639 * MemberForwarded: FieldDef, MethodDef
642 n
= MAX (meta
->tables
[MONO_TABLE_FIELD
].rows
,
643 meta
->tables
[MONO_TABLE_METHOD
].rows
);
645 /* 1 bit used to encode tag */
646 field_size
= rtsize (n
, 16-1);
650 * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
652 * It is TypeDef, _TypeRef_, TypeSpec, instead.
654 case MONO_MT_TDOR_IDX
:
655 n
= MAX (meta
->tables
[MONO_TABLE_TYPEDEF
].rows
,
656 meta
->tables
[MONO_TABLE_TYPEREF
].rows
);
657 n
= MAX (n
, meta
->tables
[MONO_TABLE_TYPESPEC
].rows
);
659 /* 2 bits to encode */
660 field_size
= rtsize (n
, 16-2);
664 * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
666 case MONO_MT_MRP_IDX
:
667 n
= MAX (meta
->tables
[MONO_TABLE_TYPEDEF
].rows
,
668 meta
->tables
[MONO_TABLE_TYPEREF
].rows
);
669 n
= MAX (n
, meta
->tables
[MONO_TABLE_METHOD
].rows
);
670 n
= MAX (n
, meta
->tables
[MONO_TABLE_MODULEREF
].rows
);
671 n
= MAX (n
, meta
->tables
[MONO_TABLE_TYPESPEC
].rows
);
672 n
= MAX (n
, meta
->tables
[MONO_TABLE_MEMBERREF
].rows
);
674 /* 3 bits to encode */
675 field_size
= rtsize (n
, 16 - 3);
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);
690 * HasSemantics: Property, Event
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);
701 * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
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);
715 * encode field size as follows (we just need to
722 bitfield
|= (field_size
-1) << shift
;
725 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
728 *result_bitfield
= (i
<< 24) | bitfield
;
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.
740 mono_metadata_compute_table_bases (MonoImage
*meta
)
743 const char *base
= meta
->tables_base
;
745 for (i
= 0; i
< MONO_TABLE_NUM
; i
++){
746 if (meta
->tables
[i
].rows
== 0)
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.
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
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
)
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.
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").
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.
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.
854 mono_metadata_guid_heap (MonoImage
*meta
, guint32 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
;
863 dword_align (const char *ptr
)
865 #if SIZEOF_VOID_P == 8
866 return (const char *) (((guint64
) (ptr
+ 3)) & ~3);
868 return (const char *) (((guint32
) (ptr
+ 3)) & ~3);
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
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
);
895 res
[i
] = *data
; break;
897 res
[i
] = read16 (data
); break;
900 res
[i
] = read32 (data
); break;
903 g_assert_not_reached ();
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.
919 mono_metadata_decode_row_col (const MonoTableInfo
*t
, int idx
, guint col
)
921 guint32 bitfield
= t
->size_bitfield
;
923 register const char *data
= t
->base
+ idx
* t
->row_size
;
926 g_assert (col
< mono_metadata_table_count (bitfield
));
928 n
= mono_metadata_table_size (bitfield
, 0);
929 for (i
= 0; i
< col
; ++i
) {
931 n
= mono_metadata_table_size (bitfield
, i
+ 1);
937 return read16 (data
);
939 return read32 (data
);
941 g_assert_not_reached ();
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
955 mono_metadata_decode_blob_size (const char *xptr
, const char **rptr
)
957 const unsigned char *ptr
= (const unsigned char *)xptr
;
960 if ((*ptr
& 0x80) == 0){
961 size
= ptr
[0] & 0x7f;
963 } else if ((*ptr
& 0x40) == 0){
964 size
= ((ptr
[0] & 0x3f) << 8) + ptr
[1];
967 size
= ((ptr
[0] & 0x1f) << 24) +
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
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
;
996 if ((b
& 0x80) == 0){
999 } else if ((b
& 0x40) == 0){
1000 len
= ((b
& 0x3f) << 8 | ptr
[1]);
1003 len
= ((b
& 0x1f) << 24) |
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.
1025 mono_metadata_parse_typedef_or_ref (MonoImage
*m
, const char *ptr
, const char **rptr
)
1028 token
= mono_metadata_decode_value (ptr
, &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
)) {
1053 dest
->required
= *ptr
== MONO_TYPE_CMOD_REQD
? 1 : 0;
1054 dest
->token
= mono_metadata_parse_typedef_or_ref (m
, ptr
+ 1, rptr
);
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
1072 mono_metadata_parse_array_full (MonoImage
*m
, MonoGenericContext
*generic_context
,
1073 const char *ptr
, const char **rptr
)
1076 MonoArrayType
*array
= g_new0 (MonoArrayType
, 1);
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
);
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().
1113 mono_metadata_free_array (MonoArrayType
*array
)
1115 g_free (array
->sizes
);
1116 g_free (array
->lobounds
);
1121 * need to add common field and param attributes combinations:
1124 * public static literal
1127 * private static literal
1129 static const MonoType
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.
1179 mono_type_hash (gconstpointer data
)
1181 const MonoType
*type
= (const MonoType
*) data
;
1182 return type
->type
| (type
->byref
<< 8) | (type
->attrs
<< 9);
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
)
1193 /* need other checks */
1198 mono_generic_inst_hash (gconstpointer data
)
1200 const MonoGenericInst
*ginst
= (const MonoGenericInst
*) data
;
1201 return ginst
->type_argc
| (ginst
->is_open
<< 8);
1205 mono_generic_inst_equal (gconstpointer ka
, gconstpointer kb
)
1207 const MonoGenericInst
*a
= (const MonoGenericInst
*) ka
;
1208 const MonoGenericInst
*b
= (const MonoGenericInst
*) kb
;
1211 if ((a
->is_open
!= b
->is_open
) || (a
->type_argc
!= b
->type_argc
) || (a
->is_reference
!= b
->is_reference
))
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
))
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
);
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.
1242 mono_metadata_init (void)
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.
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
;
1282 gboolean byref
= FALSE
;
1283 gboolean pinned
= FALSE
;
1284 const char *tmp_ptr
;
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
1296 * Also, pinned should come before anything else, but some MSV++ produced
1297 * assemblies violate this (#bug 61990).
1300 /* Count the modifiers first */
1305 case MONO_TYPE_PINNED
:
1306 case MONO_TYPE_BYREF
:
1309 case MONO_TYPE_CMOD_REQD
:
1310 case MONO_TYPE_CMOD_OPT
:
1312 mono_metadata_parse_custom_mod (m
, NULL
, tmp_ptr
, &tmp_ptr
);
1320 type
= g_malloc0 (sizeof (MonoType
) + ((gint32
)count
- MONO_ZERO_LEN_ARRAY
) * sizeof (MonoCustomMod
));
1321 type
->num_mods
= count
;
1323 g_warning ("got more than 64 modifiers in type");
1327 memset (type
, 0, sizeof (MonoType
));
1330 /* Parse pinned, byref and custom modifiers */
1335 case MONO_TYPE_PINNED
:
1339 case MONO_TYPE_BYREF
:
1343 case MONO_TYPE_CMOD_REQD
:
1344 case MONO_TYPE_CMOD_OPT
:
1345 mono_metadata_parse_custom_mod (m
, &(type
->modifiers
[count
]), ptr
, &ptr
);
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
);
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
) {
1370 return &type
->data
.klass
->this_arg
;
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
)))
1379 /*printf ("%x%c %s\n", type->attrs, type->pinned ? 'p' : ' ', mono_type_full_name (type));*/
1382 type
= g_memdup (&stype
, sizeof (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
);
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;
1442 MonoMethodSignature
*
1443 mono_metadata_signature_dup (MonoMethodSignature
*sig
)
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
;
1474 gen_param_count
= 1;
1479 call_convention
= *ptr
& 0x0F;
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
);
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
);
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
];
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
;
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
;
1548 method
->params
[i
] = mono_metadata_parse_type_full (
1549 m
, context
, MONO_PARSE_PARAM
, pattrs
[i
], ptr
, &ptr
);
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
;
1565 * Add signature to a cache and increase ref count...
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.
1583 mono_metadata_free_method_signature (MonoMethodSignature
*sig
)
1586 mono_metadata_free_type (sig
->ret
);
1587 for (i
= 0; i
< sig
->param_count
; ++i
)
1588 mono_metadata_free_type (sig
->params
[i
]);
1594 mono_metadata_lookup_generic_inst (MonoGenericInst
*ginst
)
1596 MonoGenericInst
*cached
;
1599 cached
= g_hash_table_lookup (generic_inst_cache
, ginst
);
1601 for (i
= 0; i
< ginst
->type_argc
; i
++)
1602 mono_metadata_free_type (ginst
->type_argv
[i
]);
1603 g_free (ginst
->type_argv
);
1608 ginst
->id
= ++next_generic_inst_id
;
1609 g_hash_table_insert (generic_inst_cache
, ginst
, ginst
);
1615 mono_metadata_lookup_generic_class (MonoGenericClass
*gclass
)
1617 MonoGenericClass
*cached
;
1619 cached
= g_hash_table_lookup (generic_class_cache
, gclass
);
1623 g_hash_table_insert (generic_class_cache
, gclass
, gclass
);
1628 mono_metadata_inflate_generic_inst (MonoGenericInst
*ginst
, MonoGenericContext
*context
)
1630 MonoGenericInst
*nginst
;
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
);
1653 mono_metadata_parse_generic_inst (MonoImage
*m
, MonoGenericContext
*generic_context
,
1654 int count
, const char *ptr
, const char **rptr
)
1656 MonoGenericInst
*ginst
;
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
);
1677 return mono_metadata_lookup_generic_inst (ginst
);
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
;
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
);
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
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
1730 cached
= g_hash_table_lookup (generic_class_cache
, gclass
);
1732 g_free (gclass
->klass
);
1735 type
->data
.generic_class
= cached
;
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
;
1760 index
= mono_metadata_decode_value (ptr
, &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
;
1770 generic_container
= generic_context
->container
;
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
];
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)
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
:
1829 case MONO_TYPE_STRING
:
1830 case MONO_TYPE_OBJECT
:
1831 case MONO_TYPE_TYPEDBYREF
:
1833 case MONO_TYPE_VALUETYPE
:
1834 case MONO_TYPE_CLASS
: {
1836 token
= mono_metadata_parse_typedef_or_ref (m
, ptr
, &ptr
);
1837 type
->data
.klass
= mono_class_get (m
, token
);
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
);
1847 type
->data
.type
= mono_metadata_parse_type_full (m
, generic_context
, MONO_PARSE_MOD_TYPE
, 0, ptr
, &ptr
);
1849 case MONO_TYPE_FNPTR
:
1850 type
->data
.method
= mono_metadata_parse_method_signature_full (m
, generic_context
, 0, ptr
, &ptr
);
1852 case MONO_TYPE_ARRAY
:
1853 type
->data
.array
= mono_metadata_parse_array_full (m
, generic_context
, ptr
, &ptr
);
1856 case MONO_TYPE_MVAR
:
1857 type
->data
.generic_param
= mono_metadata_parse_generic_param (m
, generic_context
, TRUE
, ptr
, &ptr
);
1861 type
->data
.generic_param
= mono_metadata_parse_generic_param (m
, generic_context
, FALSE
, ptr
, &ptr
);
1864 case MONO_TYPE_GENERICINST
:
1865 do_mono_metadata_parse_generic_class (type
, m
, generic_context
, ptr
, &ptr
);
1869 g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type
->type
);
1877 * mono_metadata_free_type:
1878 * @type: type to free
1880 * Free the memory allocated for type @type.
1883 mono_metadata_free_type (MonoType
*type
)
1885 if (type
>= builtin_types
&& type
< builtin_types
+ NBUILTIN_TYPES ())
1888 switch (type
->type
){
1889 case MONO_TYPE_OBJECT
:
1890 case MONO_TYPE_STRING
:
1891 if (!type
->data
.klass
)
1894 case MONO_TYPE_CLASS
:
1895 case MONO_TYPE_VALUETYPE
:
1896 if (type
== &type
->data
.klass
->byval_arg
|| type
== &type
->data
.klass
->this_arg
)
1900 mono_metadata_free_type (type
->data
.type
);
1902 case MONO_TYPE_FNPTR
:
1903 mono_metadata_free_method_signature (type
->data
.method
);
1905 case MONO_TYPE_ARRAY
:
1906 mono_metadata_free_array (type
->data
.array
);
1914 hex_dump (const char *buffer
, int base
, int count
)
1916 int show_header
= 1;
1924 for (i
= 0; i
< count
; i
++){
1927 printf ("\n0x%08x: ", (unsigned char) base
+ i
);
1929 printf ("%02x ", (unsigned char) (buffer
[i
]));
1936 * @mh: The Method header
1937 * @ptr: Points to the beginning of the Section Data (25.3)
1940 parse_section_data (MonoImage
*m
, MonoMethodHeader
*mh
, const unsigned char *ptr
)
1942 unsigned char sect_data_flags
;
1943 const unsigned char *sptr
;
1945 guint32 sect_data_len
;
1948 /* align on 32-bit boundary */
1949 /* FIXME: not 64-bit clean code */
1950 sptr
= ptr
= dword_align (ptr
);
1951 sect_data_flags
= *ptr
;
1954 is_fat
= sect_data_flags
& METHOD_HEADER_SECTION_FAT_FORMAT
;
1956 sect_data_len
= (ptr
[2] << 16) | (ptr
[1] << 8) | ptr
[0];
1959 sect_data_len
= ptr
[0];
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);
1970 if (sect_data_flags
& METHOD_HEADER_SECTION_EHTABLE
) {
1971 const unsigned char *p
= dword_align (ptr
);
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
];
1980 ec
->flags
= read32 (p
);
1982 ec
->try_offset
= read32 (p
);
1984 ec
->try_len
= read32 (p
);
1986 ec
->handler_offset
= read32 (p
);
1988 ec
->handler_len
= read32 (p
);
1990 tof_value
= read32 (p
);
1993 ec
->flags
= read16 (p
);
1995 ec
->try_offset
= read16 (p
);
1999 ec
->handler_offset
= read16 (p
);
2001 ec
->handler_len
= *p
;
2003 tof_value
= read32 (p
);
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;
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 */
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.
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
;
2041 guint32 local_var_sig_tok
, max_stack
, code_size
, init_locals
;
2042 const unsigned char *code
;
2045 g_return_val_if_fail (ptr
!= NULL
, NULL
);
2048 case METHOD_HEADER_TINY_FORMAT
:
2049 mh
= g_new0 (MonoMethodHeader
, 1);
2052 local_var_sig_tok
= 0;
2053 mh
->code_size
= flags
>> 2;
2056 case METHOD_HEADER_TINY_FORMAT1
:
2057 mh
= g_new0 (MonoMethodHeader
, 1);
2060 local_var_sig_tok
= 0;
2063 * The spec claims 3 bits, but the Beta2 is
2066 mh
->code_size
= flags
>> 2;
2069 case METHOD_HEADER_FAT_FORMAT
:
2070 fat_flags
= read16 (ptr
);
2072 hsize
= (fat_flags
>> 12) & 0xf;
2073 max_stack
= read16 (ptr
);
2075 code_size
= read32 (ptr
);
2077 local_var_sig_tok
= read32 (ptr
);
2080 if (fat_flags
& METHOD_HEADER_INIT_LOCALS
)
2087 if (!(fat_flags
& METHOD_HEADER_MORE_SECTS
))
2091 * There are more sections
2093 ptr
= code
+ code_size
;
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");
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
);
2120 mh
= g_new0 (MonoMethodHeader
, 1);
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
);
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.
2144 mono_metadata_free_mh (MonoMethodHeader
*mh
)
2147 for (i
= 0; i
< mh
->num_locals
; ++i
)
2148 mono_metadata_free_type (mh
->locals
[i
]);
2149 g_free (mh
->clauses
);
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.
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.
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
2196 mono_metadata_token_from_dor (guint32 dor_index
)
2200 table
= dor_index
& 0x03;
2201 idx
= dor_index
>> 2;
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
;
2214 g_assert_not_reached ();
2221 * We use this to pass context information to the row locator
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 */
2231 * How the row locator works.
2236 * ___|___------> _______
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:
2247 * col_idx is the column number
2248 * index is the index in table B
2249 * result will be the index in table A
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.
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
);
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
)
2283 if (col
== col_next
)
2287 loc
->result
= typedef_index
;
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
;
2300 col
= mono_metadata_decode_row_col (loc
->t
, table_index
, loc
->col_idx
);
2302 if (loc
->idx
== col
) {
2303 loc
->result
= table_index
;
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
;
2320 col
= mono_metadata_decode_row_col (loc
->t
, table_index
, loc
->col_idx
);
2322 if (loc
->idx
== col
) {
2323 loc
->result
= table_index
;
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.
2341 mono_metadata_typedef_from_field (MonoImage
*meta
, guint32 index
)
2343 MonoTableInfo
*tdef
= &meta
->tables
[MONO_TABLE_TYPEDEF
];
2349 loc
.idx
= mono_metadata_token_index (index
);
2350 loc
.col_idx
= MONO_TYPEDEF_FIELD_LIST
;
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.
2369 mono_metadata_typedef_from_method (MonoImage
*meta
, guint32 index
)
2371 MonoTableInfo
*tdef
= &meta
->tables
[MONO_TABLE_TYPEDEF
];
2377 loc
.idx
= mono_metadata_token_index (index
);
2378 loc
.col_idx
= MONO_TYPEDEF_METHOD_LIST
;
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.
2397 mono_metadata_interfaces_from_typedef_full (MonoImage
*meta
, guint32 index
, guint
*count
, MonoGenericContext
*context
)
2399 MonoTableInfo
*tdef
= &meta
->tables
[MONO_TABLE_INTERFACEIMPL
];
2402 guint32 cols
[MONO_INTERFACEIMPL_SIZE
];
2410 loc
.idx
= mono_metadata_token_index (index
);
2411 loc
.col_idx
= MONO_INTERFACEIMPL_CLASS
;
2414 if (!bsearch (&loc
, tdef
->base
, tdef
->rows
, tdef
->row_size
, table_locator
))
2419 * We may end up in the middle of the rows...
2422 if (loc
.idx
== mono_metadata_decode_row_col (tdef
, start
- 1, MONO_INTERFACEIMPL_CLASS
))
2429 while (start
< tdef
->rows
) {
2430 mono_metadata_decode_row (tdef
, start
, cols
, MONO_INTERFACEIMPL_SIZE
);
2431 if (cols
[MONO_INTERFACEIMPL_CLASS
] != loc
.idx
)
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
);
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.
2458 mono_metadata_nested_in_typedef (MonoImage
*meta
, guint32 index
)
2460 MonoTableInfo
*tdef
= &meta
->tables
[MONO_TABLE_NESTEDCLASS
];
2466 loc
.idx
= mono_metadata_token_index (index
);
2467 loc
.col_idx
= MONO_NESTED_CLASS_NESTED
;
2470 if (!bsearch (&loc
, tdef
->base
, tdef
->rows
, tdef
->row_size
, table_locator
))
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.
2487 mono_metadata_nesting_typedef (MonoImage
*meta
, guint32 index
, guint32 start_index
)
2489 MonoTableInfo
*tdef
= &meta
->tables
[MONO_TABLE_NESTEDCLASS
];
2491 guint32 class_index
= mono_metadata_token_index (index
);
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
))
2505 if (start
> tdef
->rows
)
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.
2521 mono_metadata_packing_from_typedef (MonoImage
*meta
, guint32 index
, guint32
*packing
, guint32
*size
)
2523 MonoTableInfo
*tdef
= &meta
->tables
[MONO_TABLE_CLASSLAYOUT
];
2525 guint32 cols
[MONO_CLASS_LAYOUT_SIZE
];
2530 loc
.idx
= mono_metadata_token_index (index
);
2531 loc
.col_idx
= MONO_CLASS_LAYOUT_PARENT
;
2534 if (!bsearch (&loc
, tdef
->base
, tdef
->rows
, tdef
->row_size
, table_locator
))
2537 mono_metadata_decode_row (tdef
, loc
.result
, cols
, MONO_CLASS_LAYOUT_SIZE
);
2539 *packing
= cols
[MONO_CLASS_LAYOUT_PACKING_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.
2557 mono_metadata_custom_attrs_from_index (MonoImage
*meta
, guint32 index
)
2559 MonoTableInfo
*tdef
= &meta
->tables
[MONO_TABLE_CUSTOMATTRIBUTE
];
2566 loc
.col_idx
= MONO_CUSTOM_ATTR_PARENT
;
2569 if (!bsearch (&loc
, tdef
->base
, tdef
->rows
, tdef
->row_size
, table_locator
))
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
))
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.
2590 mono_metadata_declsec_from_index (MonoImage
*meta
, guint32 index
)
2592 MonoTableInfo
*tdef
= &meta
->tables
[MONO_TABLE_DECLSECURITY
];
2599 loc
.col_idx
= MONO_DECL_SECURITY_PARENT
;
2602 if (!bsearch (&loc
, tdef
->base
, tdef
->rows
, tdef
->row_size
, declsec_locator
))
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
))
2614 mono_backtrace (int limit
)
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
]);
2629 /*#define __alignof__(a) sizeof(a)*/
2630 #define __alignof__(type) G_STRUCT_OFFSET(struct { char c; type x; }, x)
2635 * @t: the type to return the size of
2637 * Returns: the number of bytes required to hold an instance of this
2641 mono_type_size (MonoType
*t
, gint
*align
)
2648 *align
= __alignof__(gpointer
);
2649 return sizeof (gpointer
);
2653 case MONO_TYPE_VOID
:
2656 case MONO_TYPE_BOOLEAN
:
2657 *align
= __alignof__(gint8
);
2661 *align
= __alignof__(gint8
);
2663 case MONO_TYPE_CHAR
:
2666 *align
= __alignof__(gint16
);
2670 *align
= __alignof__(gint32
);
2673 *align
= __alignof__(float);
2677 *align
= __alignof__(gint64
);
2680 *align
= __alignof__(double);
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
);
2696 return mono_class_value_size (t
->data
.klass
, align
);
2698 case MONO_TYPE_CLASS
:
2699 case MONO_TYPE_SZARRAY
:
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
);
2716 return mono_class_value_size (gclass
->klass
, align
);
2718 *align
= __alignof__(gpointer
);
2719 return sizeof (gpointer
);
2723 case MONO_TYPE_MVAR
:
2724 *align
= __alignof__(gpointer
);
2725 return sizeof (gpointer
);
2727 g_error ("mono_type_size: type 0x%02x unknown", t
->type
);
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
)
2744 g_assert (t
!= NULL
);
2750 *align
= __alignof__(gpointer
);
2751 return sizeof (gpointer
);
2755 case MONO_TYPE_BOOLEAN
:
2756 case MONO_TYPE_CHAR
:
2765 case MONO_TYPE_STRING
:
2766 case MONO_TYPE_OBJECT
:
2767 case MONO_TYPE_CLASS
:
2768 case MONO_TYPE_SZARRAY
:
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;
2778 *align
= __alignof__(float);
2779 return sizeof (float);
2782 *align
= __alignof__(gint64
);
2783 return sizeof (gint64
);
2785 *align
= __alignof__(double);
2786 return sizeof (double);
2787 case MONO_TYPE_VALUETYPE
: {
2790 if (t
->data
.klass
->enumtype
)
2791 return mono_type_stack_size (t
->data
.klass
->enum_basetype
, align
);
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);
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
);
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);
2824 *align
= __alignof__(gpointer
);
2825 return sizeof (gpointer
);
2829 g_error ("type 0x%02x unknown", t
->type
);
2835 mono_metadata_generic_class_is_valuetype (MonoGenericClass
*gclass
)
2837 return gclass
->container_class
->valuetype
;
2841 _mono_metadata_generic_class_equal (const MonoGenericClass
*g1
, const MonoGenericClass
*g2
, gboolean signature_only
)
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
))
2848 if (!mono_metadata_class_equal (g1
->container_class
, g2
->container_class
, signature_only
))
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
))
2858 mono_metadata_generic_method_hash (MonoGenericMethod
*gmethod
)
2860 return gmethod
->inst
->id
;
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:
2874 * Computes an hash value for @t1 to be used in GHashTable.
2877 mono_metadata_type_hash (MonoType
*t1
)
2879 guint hash
= t1
->type
;
2881 hash
|= t1
->byref
<< 6; /* do not collide with t1->type values */
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
);
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
);
2899 mono_metadata_generic_param_equal (MonoGenericParam
*p1
, MonoGenericParam
*p2
, gboolean signature_only
)
2903 if (p1
->num
!= p2
->num
)
2906 g_assert (p1
->owner
&& p2
->owner
);
2907 if (p1
->owner
== p2
->owner
)
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
;
2919 mono_metadata_class_equal (MonoClass
*c1
, MonoClass
*c2
, gboolean signature_only
)
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
);
2935 * mono_metadata_type_equal:
2939 * Determine if @t1 and @t2 represent the same type.
2940 * Returns: #TRUE if @t1 and @t2 are equal.
2943 do_mono_metadata_type_equal (MonoType
*t1
, MonoType
*t2
, gboolean signature_only
)
2945 if (t1
->type
!= t2
->type
||
2946 t1
->byref
!= t2
->byref
)
2950 case MONO_TYPE_VOID
:
2951 case MONO_TYPE_BOOLEAN
:
2952 case MONO_TYPE_CHAR
:
2963 case MONO_TYPE_STRING
:
2966 case MONO_TYPE_OBJECT
:
2967 case MONO_TYPE_TYPEDBYREF
:
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
);
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
)
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
);
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
);
2989 g_error ("implement type compare for %0x!", t1
->type
);
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.
3012 mono_metadata_signature_equal (MonoMethodSignature
*sig1
, MonoMethodSignature
*sig2
)
3016 if (sig1
->hasthis
!= sig2
->hasthis
||
3017 sig1
->param_count
!= sig2
->param_count
)
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)
3036 if (!do_mono_metadata_type_equal (p1
, p2
, TRUE
))
3040 if (!do_mono_metadata_type_equal (sig1
->ret
, sig2
->ret
, TRUE
))
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
;
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).
3067 mono_metadata_encode_value (guint32 value
, char *buf
, char **endbuf
)
3073 else if (value
< 0x4000) {
3074 p
[0] = 0x80 | (value
>> 8);
3075 p
[1] = value
& 0xff;
3078 p
[0] = (value
>> 24) | 0xc0;
3079 p
[1] = (value
>> 16) & 0xff;
3080 p
[2] = (value
>> 8) & 0xff;
3081 p
[3] = value
& 0xff;
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
3105 mono_metadata_field_info (MonoImage
*meta
, guint32 index
, guint32
*offset
, guint32
*rva
,
3106 MonoMarshalSpec
**marshal_spec
)
3108 MonoTableInfo
*tdef
;
3111 loc
.idx
= index
+ 1;
3113 tdef
= &meta
->tables
[MONO_TABLE_FIELDLAYOUT
];
3115 loc
.col_idx
= MONO_FIELD_LAYOUT_FIELD
;
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
);
3121 *offset
= (guint32
)-1;
3125 tdef
= &meta
->tables
[MONO_TABLE_FIELDRVA
];
3127 loc
.col_idx
= MONO_FIELD_RVA_FIELD
;
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
);
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.
3160 mono_metadata_get_constant_index (MonoImage
*meta
, guint32 token
, guint32 hint
)
3162 MonoTableInfo
*tdef
;
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
;
3172 case MONO_TABLE_PARAM
:
3173 index
|= MONO_HASCONSTANT_PARAM
;
3175 case MONO_TABLE_PROPERTY
:
3176 index
|= MONO_HASCONSTANT_PROPERTY
;
3179 g_warning ("Not a valid token for the constant table: 0x%08x", token
);
3183 loc
.col_idx
= MONO_CONSTANT_PARENT
;
3186 if ((hint
> 0) && (hint
< tdef
->rows
) && (mono_metadata_decode_row_col (tdef
, hint
- 1, MONO_CONSTANT_PARENT
) == index
))
3189 if (tdef
->base
&& bsearch (&loc
, tdef
->base
, tdef
->rows
, tdef
->row_size
, table_locator
)) {
3190 return loc
.result
+ 1;
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.
3205 mono_metadata_events_from_typedef (MonoImage
*meta
, guint32 index
, guint
*end_idx
)
3209 MonoTableInfo
*tdef
= &meta
->tables
[MONO_TABLE_EVENTMAP
];
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
))
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;
3227 end
= meta
->tables
[MONO_TABLE_EVENT
].rows
;
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.
3244 mono_metadata_methods_from_event (MonoImage
*meta
, guint32 index
, guint
*end_idx
)
3248 guint32 cols
[MONO_METHOD_SEMA_SIZE
];
3249 MonoTableInfo
*msemt
= &meta
->tables
[MONO_TABLE_METHODSEMANTICS
];
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
))
3264 * We may end up in the middle of the rows...
3267 if (loc
.idx
== mono_metadata_decode_row_col (msemt
, start
- 1, MONO_METHOD_SEMA_ASSOCIATION
))
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
)
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.
3293 mono_metadata_properties_from_typedef (MonoImage
*meta
, guint32 index
, guint
*end_idx
)
3297 MonoTableInfo
*tdef
= &meta
->tables
[MONO_TABLE_PROPERTYMAP
];
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
))
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;
3315 end
= meta
->tables
[MONO_TABLE_PROPERTY
].rows
;
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.
3332 mono_metadata_methods_from_property (MonoImage
*meta
, guint32 index
, guint
*end_idx
)
3336 guint32 cols
[MONO_METHOD_SEMA_SIZE
];
3337 MonoTableInfo
*msemt
= &meta
->tables
[MONO_TABLE_METHODSEMANTICS
];
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
))
3352 * We may end up in the middle of the rows...
3355 if (loc
.idx
== mono_metadata_decode_row_col (msemt
, start
- 1, MONO_METHOD_SEMA_ASSOCIATION
))
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
)
3372 mono_metadata_implmap_from_method (MonoImage
*meta
, guint32 method_idx
)
3375 MonoTableInfo
*tdef
= &meta
->tables
[MONO_TABLE_IMPLMAP
];
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
))
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
3398 mono_type_create_from_typespec_full (MonoImage
*image
, MonoGenericContext
*generic_context
, guint32 type_spec
)
3400 guint32 idx
= mono_metadata_token_index (type_spec
);
3402 guint32 cols
[MONO_TYPESPEC_SIZE
];
3407 mono_loader_lock ();
3409 if ((type
= g_hash_table_lookup (image
->typespec_cache
, GUINT_TO_POINTER (type_spec
)))) {
3410 mono_loader_unlock ();
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
) {
3429 do_mono_metadata_parse_type (type
, image
, generic_context
, ptr
, &ptr
);
3431 mono_loader_unlock ();
3437 mono_type_create_from_typespec (MonoImage
*image
, guint32 type_spec
)
3439 return mono_type_create_from_typespec_full (image
, NULL
, type_spec
);
3443 mono_metadata_parse_marshal_spec (MonoImage
*image
, const char *ptr
)
3445 MonoMarshalSpec
*res
;
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
);
3479 /* skip unused native type name */
3480 len
= mono_metadata_decode_value (ptr
, &ptr
);
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
);
3486 /* read cookie string */
3487 len
= mono_metadata_decode_value (ptr
, &ptr
);
3488 res
->data
.custom_data
.cookie
= g_strndup (ptr
, len
);
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
);
3505 mono_type_to_unmanaged (MonoType
*type
, MonoMarshalSpec
*mspec
, gboolean as_field
,
3506 gboolean unicode
, MonoMarshalConv
*conv
)
3508 MonoMarshalConv dummy_conv
;
3514 *conv
= MONO_MARSHAL_CONV_NONE
;
3517 return MONO_NATIVE_UINT
;
3521 case MONO_TYPE_BOOLEAN
:
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
;
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
:
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
:
3573 *conv
= MONO_MARSHAL_CONV_STR_BYVALWSTR
;
3575 *conv
= MONO_MARSHAL_CONV_STR_BYVALSTR
;
3576 return MONO_NATIVE_BYVALTSTR
;
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
;
3589 return MONO_NATIVE_STRUCT
;
3590 case MONO_TYPE_SZARRAY
:
3591 case MONO_TYPE_ARRAY
:
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
;
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 */
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
;
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
:
3654 g_error ("type 0x%02x not handled in marshal", t
);
3656 return MONO_NATIVE_MAX
;
3660 mono_metadata_get_marshal_info (MonoImage
*meta
, guint32 idx
, gboolean is_field
)
3663 MonoTableInfo
*tdef
= &meta
->tables
[MONO_TABLE_FIELDMARSHAL
];
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
))
3675 return mono_metadata_blob_heap (meta
, mono_metadata_decode_row_col (tdef
, loc
.result
, MONO_FIELD_MARSHAL_NATIVE_TYPE
));
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 ();
3693 mono_class_get_overrides (MonoImage
*image
, guint32 type_token
, gint32
*num_overrides
)
3696 MonoTableInfo
*tdef
= &image
->tables
[MONO_TABLE_METHODIMPL
];
3699 guint32 cols
[MONO_METHODIMPL_SIZE
];
3700 MonoMethod
**result
;
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
))
3718 * We may end up in the middle of the rows...
3721 if (loc
.idx
== mono_metadata_decode_row_col (tdef
, start
- 1, MONO_METHODIMPL_CLASS
))
3726 while (end
< tdef
->rows
) {
3727 if (loc
.idx
== mono_metadata_decode_row_col (tdef
, end
, MONO_METHODIMPL_CLASS
))
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
]);
3741 *num_overrides
= num
;
3746 * mono_guid_to_string:
3748 * Converts a 16 byte Microsoft GUID to the standard string representation.
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],
3758 guid
[10], guid
[11], guid
[12], guid
[13], guid
[14], guid
[15]);
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
;
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
);
3780 /* contiguous list finished */
3787 res
= g_new0 (MonoClass
*, found
+ 1);
3788 for (i
= 0, tmp
= cons
; i
< found
; ++i
, tmp
= tmp
->next
) {
3789 res
[i
] = tmp
->data
;
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
;
3809 g_error ("wrong token %x to load_generics_params", token
);
3812 owner
|= mono_metadata_token_index (token
) << MONO_TYPEORMETHOD_BITS
;
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
)
3822 if (i
>= tdef
->rows
)
3826 container
= g_new0 (MonoGenericContainer
, 1);
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
)
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
);
3857 mono_type_is_byref (MonoType
*type
)
3863 mono_type_get_type (MonoType
*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 */
3877 mono_type_get_class (MonoType
*type
)
3879 return type
->data
.klass
;
3882 /* For MONO_TYPE_ARRAY */
3884 mono_type_get_array_type (MonoType
*type
)
3886 return type
->data
.array
;
3890 mono_type_get_modifiers (MonoType
*type
, gboolean
*is_required
, gpointer
*iter
)
3892 /* FIXME: implement */
3897 mono_signature_get_return_type (MonoMethodSignature
*sig
)
3903 mono_signature_get_params (MonoMethodSignature
*sig
, gpointer
*iter
)
3909 /* start from the first */
3910 if (sig
->param_count
) {
3911 *iter
= &sig
->params
[0];
3912 return sig
->params
[0];
3920 if (type
< &sig
->params
[sig
->param_count
]) {
3928 mono_signature_get_param_count (MonoMethodSignature
*sig
)
3930 return sig
->param_count
;
3934 mono_signature_get_call_conv (MonoMethodSignature
*sig
)
3936 return sig
->call_convention
;
3940 mono_signature_vararg_start (MonoMethodSignature
*sig
)
3942 return sig
->sentinelpos
;
3946 mono_signature_is_instance (MonoMethodSignature
*sig
)
3948 return sig
->hasthis
;
3952 mono_signature_explicit_this (MonoMethodSignature
*sig
)
3954 return sig
->explicit_this
;