Merge pull request #3142 from henricm/fix-for-win-mono_string_to_utf8
[mono.git] / mono / metadata / metadata.c
1 /*
2  * metadata.c: Routines for accessing the metadata
3  *
4  * Authors:
5  *   Miguel de Icaza (miguel@ximian.com)
6  *   Paolo Molaro (lupus@ximian.com)
7  *
8  * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
10  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11  */
12
13 #include <config.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <glib.h>
18 #include "metadata.h"
19 #include "tabledefs.h"
20 #include "mono-endian.h"
21 #include "cil-coff.h"
22 #include "tokentype.h"
23 #include "class-internals.h"
24 #include "metadata-internals.h"
25 #include "verify-internals.h"
26 #include "class.h"
27 #include "marshal.h"
28 #include "debug-helpers.h"
29 #include "abi-details.h"
30 #include <mono/utils/mono-error-internals.h>
31 #include <mono/utils/bsearch.h>
32 #include <mono/utils/atomic.h>
33
34 /* Auxiliary structure used for caching inflated signatures */
35 typedef struct {
36         MonoMethodSignature *sig;
37         MonoGenericContext context;
38 } MonoInflatedMethodSignature;
39
40 static gboolean do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container, gboolean transient,
41                                          const char *ptr, const char **rptr, MonoError *error);
42
43 static gboolean do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only);
44 static gboolean mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only);
45 static gboolean mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only);
46 static gboolean _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2,
47                                                     gboolean signature_only);
48 static void free_generic_inst (MonoGenericInst *ginst);
49 static void free_generic_class (MonoGenericClass *ginst);
50 static void free_inflated_method (MonoMethodInflated *method);
51 static void free_inflated_signature (MonoInflatedMethodSignature *sig);
52 static void mono_metadata_field_info_full (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva, MonoMarshalSpec **marshal_spec, gboolean alloc_from_image);
53
54 /*
55  * This enumeration is used to describe the data types in the metadata
56  * tables
57  */
58 enum {
59         MONO_MT_END,
60
61         /* Sized elements */
62         MONO_MT_UINT32,
63         MONO_MT_UINT16,
64         MONO_MT_UINT8,
65
66         /* Index into Blob heap */
67         MONO_MT_BLOB_IDX,
68
69         /* Index into String heap */
70         MONO_MT_STRING_IDX,
71
72         /* GUID index */
73         MONO_MT_GUID_IDX,
74
75         /* Pointer into a table */
76         MONO_MT_TABLE_IDX,
77
78         /* HasConstant:Parent pointer (Param, Field or Property) */
79         MONO_MT_CONST_IDX,
80
81         /* HasCustomAttribute index.  Indexes any table except CustomAttribute */
82         MONO_MT_HASCAT_IDX,
83         
84         /* CustomAttributeType encoded index */
85         MONO_MT_CAT_IDX,
86
87         /* HasDeclSecurity index: TypeDef Method or Assembly */
88         MONO_MT_HASDEC_IDX,
89
90         /* Implementation coded index: File, Export AssemblyRef */
91         MONO_MT_IMPL_IDX,
92
93         /* HasFieldMarshal coded index: Field or Param table */
94         MONO_MT_HFM_IDX,
95
96         /* MemberForwardedIndex: Field or Method */
97         MONO_MT_MF_IDX,
98
99         /* TypeDefOrRef coded index: typedef, typeref, typespec */
100         MONO_MT_TDOR_IDX,
101
102         /* MemberRefParent coded index: typeref, moduleref, method, memberref, typesepc, typedef */
103         MONO_MT_MRP_IDX,
104
105         /* MethodDefOrRef coded index: Method or Member Ref table */
106         MONO_MT_MDOR_IDX,
107
108         /* HasSemantic coded index: Event or Property */
109         MONO_MT_HS_IDX,
110
111         /* ResolutionScope coded index: Module, ModuleRef, AssemblytRef, TypeRef */
112         MONO_MT_RS_IDX
113 };
114
115 const static unsigned char TableSchemas [] = {
116 #define ASSEMBLY_SCHEMA_OFFSET 0
117         MONO_MT_UINT32,     /* "HashId" }, */
118         MONO_MT_UINT16,     /* "Major" },  */
119         MONO_MT_UINT16,     /* "Minor" }, */
120         MONO_MT_UINT16,     /* "BuildNumber" }, */
121         MONO_MT_UINT16,     /* "RevisionNumber" }, */
122         MONO_MT_UINT32,     /* "Flags" }, */
123         MONO_MT_BLOB_IDX,   /* "PublicKey" }, */
124         MONO_MT_STRING_IDX, /* "Name" }, */
125         MONO_MT_STRING_IDX, /* "Culture" }, */
126         MONO_MT_END,
127
128 #define ASSEMBLYOS_SCHEMA_OFFSET ASSEMBLY_SCHEMA_OFFSET + 10
129         MONO_MT_UINT32,     /* "OSPlatformID" }, */
130         MONO_MT_UINT32,     /* "OSMajor" }, */
131         MONO_MT_UINT32,     /* "OSMinor" }, */
132         MONO_MT_END,
133
134 #define ASSEMBLYPROC_SCHEMA_OFFSET ASSEMBLYOS_SCHEMA_OFFSET + 4
135         MONO_MT_UINT32,     /* "Processor" }, */
136         MONO_MT_END,
137
138 #define ASSEMBLYREF_SCHEMA_OFFSET ASSEMBLYPROC_SCHEMA_OFFSET + 2
139         MONO_MT_UINT16,     /* "Major" }, */
140         MONO_MT_UINT16,     /* "Minor" }, */
141         MONO_MT_UINT16,     /* "Build" }, */
142         MONO_MT_UINT16,     /* "Revision" }, */
143         MONO_MT_UINT32,     /* "Flags" }, */
144         MONO_MT_BLOB_IDX,   /* "PublicKeyOrToken" }, */
145         MONO_MT_STRING_IDX, /* "Name" }, */
146         MONO_MT_STRING_IDX, /* "Culture" }, */
147         MONO_MT_BLOB_IDX,   /* "HashValue" }, */
148         MONO_MT_END,
149
150 #define ASSEMBLYREFOS_SCHEMA_OFFSET ASSEMBLYREF_SCHEMA_OFFSET + 10
151         MONO_MT_UINT32,     /* "OSPlatformID" }, */
152         MONO_MT_UINT32,     /* "OSMajorVersion" }, */
153         MONO_MT_UINT32,     /* "OSMinorVersion" }, */
154         MONO_MT_TABLE_IDX,  /* "AssemblyRef:AssemblyRef" }, */
155         MONO_MT_END,
156
157 #define ASSEMBLYREFPROC_SCHEMA_OFFSET ASSEMBLYREFOS_SCHEMA_OFFSET + 5
158         MONO_MT_UINT32,     /* "Processor" }, */
159         MONO_MT_TABLE_IDX,  /* "AssemblyRef:AssemblyRef" }, */
160         MONO_MT_END,
161
162 #define CLASS_LAYOUT_SCHEMA_OFFSET ASSEMBLYREFPROC_SCHEMA_OFFSET + 3
163         MONO_MT_UINT16,     /* "PackingSize" }, */
164         MONO_MT_UINT32,     /* "ClassSize" }, */
165         MONO_MT_TABLE_IDX,  /* "Parent:TypeDef" }, */
166         MONO_MT_END,
167
168 #define CONSTANT_SCHEMA_OFFSET CLASS_LAYOUT_SCHEMA_OFFSET + 4
169         MONO_MT_UINT8,      /* "Type" }, */
170         MONO_MT_UINT8,      /* "PaddingZero" }, */
171         MONO_MT_CONST_IDX,  /* "Parent" }, */
172         MONO_MT_BLOB_IDX,   /* "Value" }, */
173         MONO_MT_END,
174
175 #define CUSTOM_ATTR_SCHEMA_OFFSET CONSTANT_SCHEMA_OFFSET + 5
176         MONO_MT_HASCAT_IDX, /* "Parent" }, */
177         MONO_MT_CAT_IDX,    /* "Type" }, */
178         MONO_MT_BLOB_IDX,   /* "Value" }, */
179         MONO_MT_END,
180
181 #define DECL_SEC_SCHEMA_OFFSET CUSTOM_ATTR_SCHEMA_OFFSET + 4
182         MONO_MT_UINT16,     /* "Action" }, */
183         MONO_MT_HASDEC_IDX, /* "Parent" }, */
184         MONO_MT_BLOB_IDX,   /* "PermissionSet" }, */
185         MONO_MT_END,
186
187 #define EVENTMAP_SCHEMA_OFFSET DECL_SEC_SCHEMA_OFFSET + 4
188         MONO_MT_TABLE_IDX,  /* "Parent:TypeDef" }, */
189         MONO_MT_TABLE_IDX,  /* "EventList:Event" }, */
190         MONO_MT_END,
191
192 #define EVENT_SCHEMA_OFFSET EVENTMAP_SCHEMA_OFFSET + 3
193         MONO_MT_UINT16,     /* "EventFlags#EventAttribute" }, */
194         MONO_MT_STRING_IDX, /* "Name" }, */
195         MONO_MT_TDOR_IDX,  /* "EventType" }, TypeDef or TypeRef or TypeSpec  */
196         MONO_MT_END,
197
198 #define EVENT_POINTER_SCHEMA_OFFSET EVENT_SCHEMA_OFFSET + 4
199         MONO_MT_TABLE_IDX,  /* "Event" }, */
200         MONO_MT_END,
201
202 #define EXPORTED_TYPE_SCHEMA_OFFSET EVENT_POINTER_SCHEMA_OFFSET + 2
203         MONO_MT_UINT32,     /* "Flags" }, */
204         MONO_MT_TABLE_IDX,  /* "TypeDefId" }, */
205         MONO_MT_STRING_IDX, /* "TypeName" }, */
206         MONO_MT_STRING_IDX, /* "TypeNameSpace" }, */
207         MONO_MT_IMPL_IDX,   /* "Implementation" }, */
208         MONO_MT_END,
209
210 #define FIELD_SCHEMA_OFFSET EXPORTED_TYPE_SCHEMA_OFFSET + 6
211         MONO_MT_UINT16,     /* "Flags" }, */
212         MONO_MT_STRING_IDX, /* "Name" }, */
213         MONO_MT_BLOB_IDX,   /* "Signature" }, */
214         MONO_MT_END,
215
216 #define FIELD_LAYOUT_SCHEMA_OFFSET FIELD_SCHEMA_OFFSET + 4
217         MONO_MT_UINT32,     /* "Offset" }, */
218         MONO_MT_TABLE_IDX,  /* "Field:Field" }, */
219         MONO_MT_END,
220
221 #define FIELD_MARSHAL_SCHEMA_OFFSET FIELD_LAYOUT_SCHEMA_OFFSET + 3
222         MONO_MT_HFM_IDX,    /* "Parent" }, */
223         MONO_MT_BLOB_IDX,   /* "NativeType" }, */
224         MONO_MT_END,
225
226 #define FIELD_RVA_SCHEMA_OFFSET FIELD_MARSHAL_SCHEMA_OFFSET + 3
227         MONO_MT_UINT32,     /* "RVA" }, */
228         MONO_MT_TABLE_IDX,  /* "Field:Field" }, */
229         MONO_MT_END,
230
231 #define FIELD_POINTER_SCHEMA_OFFSET FIELD_RVA_SCHEMA_OFFSET + 3
232         MONO_MT_TABLE_IDX,  /* "Field" }, */
233         MONO_MT_END,
234
235 #define FILE_SCHEMA_OFFSET FIELD_POINTER_SCHEMA_OFFSET + 2
236         MONO_MT_UINT32,     /* "Flags" }, */
237         MONO_MT_STRING_IDX, /* "Name" }, */
238         MONO_MT_BLOB_IDX,   /* "Value" },  */
239         MONO_MT_END,
240
241 #define IMPLMAP_SCHEMA_OFFSET FILE_SCHEMA_OFFSET + 4
242         MONO_MT_UINT16,     /* "MappingFlag" }, */
243         MONO_MT_MF_IDX,     /* "MemberForwarded" }, */
244         MONO_MT_STRING_IDX, /* "ImportName" }, */
245         MONO_MT_TABLE_IDX,  /* "ImportScope:ModuleRef" }, */
246         MONO_MT_END,
247
248 #define IFACEMAP_SCHEMA_OFFSET IMPLMAP_SCHEMA_OFFSET + 5
249         MONO_MT_TABLE_IDX,  /* "Class:TypeDef" },  */
250         MONO_MT_TDOR_IDX,  /* "Interface=TypeDefOrRef" }, */
251         MONO_MT_END,
252
253 #define MANIFEST_SCHEMA_OFFSET IFACEMAP_SCHEMA_OFFSET + 3
254         MONO_MT_UINT32,     /* "Offset" }, */
255         MONO_MT_UINT32,     /* "Flags" }, */
256         MONO_MT_STRING_IDX, /* "Name" }, */
257         MONO_MT_IMPL_IDX,   /* "Implementation" }, */
258         MONO_MT_END,
259
260 #define MEMBERREF_SCHEMA_OFFSET MANIFEST_SCHEMA_OFFSET + 5
261         MONO_MT_MRP_IDX,    /* "Class" }, */
262         MONO_MT_STRING_IDX, /* "Name" }, */
263         MONO_MT_BLOB_IDX,   /* "Signature" }, */
264         MONO_MT_END,
265
266 #define METHOD_SCHEMA_OFFSET MEMBERREF_SCHEMA_OFFSET + 4
267         MONO_MT_UINT32,     /* "RVA" }, */
268         MONO_MT_UINT16,     /* "ImplFlags#MethodImplAttributes" }, */
269         MONO_MT_UINT16,     /* "Flags#MethodAttribute" }, */
270         MONO_MT_STRING_IDX, /* "Name" }, */
271         MONO_MT_BLOB_IDX,   /* "Signature" }, */
272         MONO_MT_TABLE_IDX,  /* "ParamList:Param" }, */
273         MONO_MT_END,
274
275 #define METHOD_IMPL_SCHEMA_OFFSET METHOD_SCHEMA_OFFSET + 7
276         MONO_MT_TABLE_IDX,  /* "Class:TypeDef" }, */
277         MONO_MT_MDOR_IDX,   /* "MethodBody" }, */
278         MONO_MT_MDOR_IDX,   /* "MethodDeclaration" }, */
279         MONO_MT_END,
280
281 #define METHOD_SEMA_SCHEMA_OFFSET METHOD_IMPL_SCHEMA_OFFSET + 4
282         MONO_MT_UINT16,     /* "MethodSemantic" }, */
283         MONO_MT_TABLE_IDX,  /* "Method:Method" }, */
284         MONO_MT_HS_IDX,     /* "Association" }, */
285         MONO_MT_END,
286
287 #define METHOD_POINTER_SCHEMA_OFFSET METHOD_SEMA_SCHEMA_OFFSET + 4
288         MONO_MT_TABLE_IDX,  /* "Method" }, */
289         MONO_MT_END,
290
291 #define MODULE_SCHEMA_OFFSET METHOD_POINTER_SCHEMA_OFFSET + 2
292         MONO_MT_UINT16,     /* "Generation" }, */
293         MONO_MT_STRING_IDX, /* "Name" }, */
294         MONO_MT_GUID_IDX,   /* "MVID" }, */
295         MONO_MT_GUID_IDX,   /* "EncID" }, */
296         MONO_MT_GUID_IDX,   /* "EncBaseID" }, */
297         MONO_MT_END,
298
299 #define MODULEREF_SCHEMA_OFFSET MODULE_SCHEMA_OFFSET + 6
300         MONO_MT_STRING_IDX, /* "Name" }, */
301         MONO_MT_END,
302
303 #define NESTED_CLASS_SCHEMA_OFFSET MODULEREF_SCHEMA_OFFSET + 2
304         MONO_MT_TABLE_IDX,  /* "NestedClass:TypeDef" }, */
305         MONO_MT_TABLE_IDX,  /* "EnclosingClass:TypeDef" }, */
306         MONO_MT_END,
307
308 #define PARAM_SCHEMA_OFFSET NESTED_CLASS_SCHEMA_OFFSET + 3
309         MONO_MT_UINT16,     /* "Flags" }, */
310         MONO_MT_UINT16,     /* "Sequence" }, */
311         MONO_MT_STRING_IDX, /* "Name" }, */
312         MONO_MT_END,
313
314 #define PARAM_POINTER_SCHEMA_OFFSET PARAM_SCHEMA_OFFSET + 4
315         MONO_MT_TABLE_IDX,  /* "Param" }, */
316         MONO_MT_END,
317
318 #define PROPERTY_SCHEMA_OFFSET PARAM_POINTER_SCHEMA_OFFSET + 2
319         MONO_MT_UINT16,     /* "Flags" }, */
320         MONO_MT_STRING_IDX, /* "Name" }, */
321         MONO_MT_BLOB_IDX,   /* "Type" }, */
322         MONO_MT_END,
323
324 #define PROPERTY_POINTER_SCHEMA_OFFSET PROPERTY_SCHEMA_OFFSET + 4
325         MONO_MT_TABLE_IDX, /* "Property" }, */
326         MONO_MT_END,
327
328 #define PROPERTY_MAP_SCHEMA_OFFSET PROPERTY_POINTER_SCHEMA_OFFSET + 2
329         MONO_MT_TABLE_IDX,  /* "Parent:TypeDef" }, */
330         MONO_MT_TABLE_IDX,  /* "PropertyList:Property" }, */
331         MONO_MT_END,
332
333 #define STDALON_SIG_SCHEMA_OFFSET PROPERTY_MAP_SCHEMA_OFFSET + 3
334         MONO_MT_BLOB_IDX,   /* "Signature" }, */
335         MONO_MT_END,
336
337 #define TYPEDEF_SCHEMA_OFFSET STDALON_SIG_SCHEMA_OFFSET + 2
338         MONO_MT_UINT32,     /* "Flags" }, */
339         MONO_MT_STRING_IDX, /* "Name" }, */
340         MONO_MT_STRING_IDX, /* "Namespace" }, */
341         MONO_MT_TDOR_IDX,   /* "Extends" }, */
342         MONO_MT_TABLE_IDX,  /* "FieldList:Field" }, */
343         MONO_MT_TABLE_IDX,  /* "MethodList:Method" }, */
344         MONO_MT_END,
345
346 #define TYPEREF_SCHEMA_OFFSET TYPEDEF_SCHEMA_OFFSET + 7
347         MONO_MT_RS_IDX,     /* "ResolutionScope=ResolutionScope" }, */
348         MONO_MT_STRING_IDX, /* "Name" }, */
349         MONO_MT_STRING_IDX, /* "Namespace" }, */
350         MONO_MT_END,
351
352 #define TYPESPEC_SCHEMA_OFFSET TYPEREF_SCHEMA_OFFSET + 4
353         MONO_MT_BLOB_IDX,   /* "Signature" }, */
354         MONO_MT_END,
355
356 #define GENPARAM_SCHEMA_OFFSET TYPESPEC_SCHEMA_OFFSET + 2
357         MONO_MT_UINT16,     /* "Number" }, */
358         MONO_MT_UINT16,     /* "Flags" }, */
359         MONO_MT_TABLE_IDX,  /* "Owner" },  TypeDef or MethodDef */
360         MONO_MT_STRING_IDX, /* "Name" }, */
361         MONO_MT_END,
362
363 #define METHOD_SPEC_SCHEMA_OFFSET GENPARAM_SCHEMA_OFFSET + 5
364         MONO_MT_MDOR_IDX,   /* "Method" }, */
365         MONO_MT_BLOB_IDX,   /* "Signature" }, */
366         MONO_MT_END,
367
368 #define GEN_CONSTRAINT_SCHEMA_OFFSET METHOD_SPEC_SCHEMA_OFFSET + 3
369         MONO_MT_TABLE_IDX,  /* "GenericParam" }, */
370         MONO_MT_TDOR_IDX,   /* "Constraint" }, */
371         MONO_MT_END,
372
373 #define DOCUMENT_SCHEMA_OFFSET GEN_CONSTRAINT_SCHEMA_OFFSET + 3
374         MONO_MT_BLOB_IDX,   /* Name */
375         MONO_MT_GUID_IDX,   /* HashAlgorithm */
376         MONO_MT_BLOB_IDX,   /* Hash */
377         MONO_MT_GUID_IDX,   /* Language */
378         MONO_MT_END,
379
380 #define METHODBODY_SCHEMA_OFFSET DOCUMENT_SCHEMA_OFFSET + 5
381         MONO_MT_TABLE_IDX,   /* Document */
382         MONO_MT_BLOB_IDX,   /* SequencePoints */
383         MONO_MT_END,
384
385 #define LOCALSCOPE_SCHEMA_OFFSET METHODBODY_SCHEMA_OFFSET + 3
386         MONO_MT_TABLE_IDX,   /* Method */
387         MONO_MT_TABLE_IDX,   /* ImportScope */
388         MONO_MT_TABLE_IDX,   /* VariableList */
389         MONO_MT_TABLE_IDX,   /* ConstantList */
390         MONO_MT_UINT32,      /* StartOffset */
391         MONO_MT_UINT32,      /* Length */
392         MONO_MT_END,
393
394 #define LOCALVARIABLE_SCHEMA_OFFSET LOCALSCOPE_SCHEMA_OFFSET + 7
395         MONO_MT_UINT16,      /* Attributes */
396         MONO_MT_UINT16,      /* Index */
397         MONO_MT_STRING_IDX,  /* Name */
398         MONO_MT_END,
399
400 #define NULL_SCHEMA_OFFSET LOCALVARIABLE_SCHEMA_OFFSET + 4
401         MONO_MT_END
402 };
403
404 /* Must be the same order as MONO_TABLE_* */
405 const static unsigned char
406 table_description [] = {
407         MODULE_SCHEMA_OFFSET,
408         TYPEREF_SCHEMA_OFFSET,
409         TYPEDEF_SCHEMA_OFFSET,
410         FIELD_POINTER_SCHEMA_OFFSET,
411         FIELD_SCHEMA_OFFSET,
412         METHOD_POINTER_SCHEMA_OFFSET,
413         METHOD_SCHEMA_OFFSET,
414         PARAM_POINTER_SCHEMA_OFFSET,
415         PARAM_SCHEMA_OFFSET,
416         IFACEMAP_SCHEMA_OFFSET,
417         MEMBERREF_SCHEMA_OFFSET, /* 0xa */
418         CONSTANT_SCHEMA_OFFSET,
419         CUSTOM_ATTR_SCHEMA_OFFSET,
420         FIELD_MARSHAL_SCHEMA_OFFSET,
421         DECL_SEC_SCHEMA_OFFSET,
422         CLASS_LAYOUT_SCHEMA_OFFSET,
423         FIELD_LAYOUT_SCHEMA_OFFSET, /* 0x10 */
424         STDALON_SIG_SCHEMA_OFFSET,
425         EVENTMAP_SCHEMA_OFFSET,
426         EVENT_POINTER_SCHEMA_OFFSET,
427         EVENT_SCHEMA_OFFSET,
428         PROPERTY_MAP_SCHEMA_OFFSET,
429         PROPERTY_POINTER_SCHEMA_OFFSET,
430         PROPERTY_SCHEMA_OFFSET,
431         METHOD_SEMA_SCHEMA_OFFSET,
432         METHOD_IMPL_SCHEMA_OFFSET,
433         MODULEREF_SCHEMA_OFFSET, /* 0x1a */
434         TYPESPEC_SCHEMA_OFFSET,
435         IMPLMAP_SCHEMA_OFFSET,
436         FIELD_RVA_SCHEMA_OFFSET,
437         NULL_SCHEMA_OFFSET,
438         NULL_SCHEMA_OFFSET,
439         ASSEMBLY_SCHEMA_OFFSET, /* 0x20 */
440         ASSEMBLYPROC_SCHEMA_OFFSET,
441         ASSEMBLYOS_SCHEMA_OFFSET,
442         ASSEMBLYREF_SCHEMA_OFFSET,
443         ASSEMBLYREFPROC_SCHEMA_OFFSET,
444         ASSEMBLYREFOS_SCHEMA_OFFSET,
445         FILE_SCHEMA_OFFSET,
446         EXPORTED_TYPE_SCHEMA_OFFSET,
447         MANIFEST_SCHEMA_OFFSET,
448         NESTED_CLASS_SCHEMA_OFFSET,
449         GENPARAM_SCHEMA_OFFSET, /* 0x2a */
450         METHOD_SPEC_SCHEMA_OFFSET,
451         GEN_CONSTRAINT_SCHEMA_OFFSET,
452         NULL_SCHEMA_OFFSET,
453         NULL_SCHEMA_OFFSET,
454         NULL_SCHEMA_OFFSET,
455         DOCUMENT_SCHEMA_OFFSET, /* 0x30 */
456         METHODBODY_SCHEMA_OFFSET,
457         LOCALSCOPE_SCHEMA_OFFSET,
458         LOCALVARIABLE_SCHEMA_OFFSET
459 };
460
461 #ifdef HAVE_ARRAY_ELEM_INIT
462 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
463 #define MSGSTRFIELD1(line) str##line
464 static const struct msgstr_t {
465 #define TABLEDEF(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
466 #include "mono/cil/tables.def"
467 #undef TABLEDEF
468 } tablestr = {
469 #define TABLEDEF(a,b) b,
470 #include "mono/cil/tables.def"
471 #undef TABLEDEF
472 };
473 static const gint16 tableidx [] = {
474 #define TABLEDEF(a,b) [a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
475 #include "mono/cil/tables.def"
476 #undef TABLEDEF
477 };
478
479 #else
480 #define TABLEDEF(a,b) b,
481 static const char* const
482 mono_tables_names [] = {
483 #include "mono/cil/tables.def"
484         NULL
485 };
486
487 #endif
488
489 // Amount initially reserved in each imageset's mempool.
490 // FIXME: This number is arbitrary, a more practical number should be found
491 #define INITIAL_IMAGE_SET_SIZE    1024
492
493 /**
494  * mono_meta_table_name:
495  * @table: table index
496  *
497  * Returns the name of the given ECMA metadata logical format table
498  * as described in ECMA 335, Partition II, Section 22.
499  * 
500  * Returns: the name for the @table index
501  */
502 const char *
503 mono_meta_table_name (int table)
504 {
505         if ((table < 0) || (table > MONO_TABLE_LAST))
506                 return "";
507
508 #ifdef HAVE_ARRAY_ELEM_INIT
509         return (const char*)&tablestr + tableidx [table];
510 #else
511         return mono_tables_names [table];
512 #endif
513 }
514
515 /* The guy who wrote the spec for this should not be allowed near a
516  * computer again.
517  
518 If  e is a coded token(see clause 23.1.7) that points into table ti out of n possible tables t0, .. tn-1, 
519 then it is stored as e << (log n) & tag{ t0, .. tn-1}[ ti] using 2 bytes if the maximum number of 
520 rows of tables t0, ..tn-1, is less than 2^16 - (log n), and using 4 bytes otherwise. The family of 
521 finite maps tag{ t0, ..tn-1} is defined below. Note that to decode a physical row, you need the 
522 inverse of this mapping.
523
524  */
525 #define rtsize(meta,s,b) (((s) < (1 << (b)) ? 2 : 4))
526 #define idx_size(meta,tableidx) ((meta)->tables [(tableidx)].rows < 65536 ? 2 : 4)
527
528 /* Reference: Partition II - 23.2.6 */
529 /*
530  * mono_metadata_compute_size:
531  * @meta: metadata context
532  * @tableindex: metadata table number
533  * @result_bitfield: pointer to guint32 where to store additional info
534  * 
535  * mono_metadata_compute_size() computes the lenght in bytes of a single
536  * row in a metadata table. The size of each column is encoded in the
537  * @result_bitfield return value along with the number of columns in the table.
538  * the resulting bitfield should be handed to the mono_metadata_table_size()
539  * and mono_metadata_table_count() macros.
540  * This is a Mono runtime internal only function.
541  */
542 int
543 mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bitfield)
544 {
545         guint32 bitfield = 0;
546         int size = 0, field_size = 0;
547         int i, n, code;
548         int shift = 0;
549         const unsigned char *description = TableSchemas + table_description [tableindex];
550
551         for (i = 0; (code = description [i]) != MONO_MT_END; i++){
552                 switch (code){
553                 case MONO_MT_UINT32:
554                         field_size = 4; break;
555                         
556                 case MONO_MT_UINT16:
557                         field_size = 2; break;
558                         
559                 case MONO_MT_UINT8:
560                         field_size = 1; break;
561                         
562                 case MONO_MT_BLOB_IDX:
563                         field_size = meta->idx_blob_wide ? 4 : 2; break;
564                         
565                 case MONO_MT_STRING_IDX:
566                         field_size = meta->idx_string_wide ? 4 : 2; break;
567                         
568                 case MONO_MT_GUID_IDX:
569                         field_size = meta->idx_guid_wide ? 4 : 2; break;
570
571                 case MONO_MT_TABLE_IDX:
572                         /* Uhm, a table index can point to other tables besides the current one
573                          * so, it's not correct to use the rowcount of the current table to
574                          * get the size for this column - lupus 
575                          */
576                         switch (tableindex) {
577                         case MONO_TABLE_ASSEMBLYREFOS:
578                                 g_assert (i == 3);
579                                 field_size = idx_size (meta, MONO_TABLE_ASSEMBLYREF); break;
580                         case MONO_TABLE_ASSEMBLYREFPROCESSOR:
581                                 g_assert (i == 1);
582                                 field_size = idx_size (meta, MONO_TABLE_ASSEMBLYREF); break;
583                         case MONO_TABLE_CLASSLAYOUT:
584                                 g_assert (i == 2);
585                                 field_size = idx_size (meta, MONO_TABLE_TYPEDEF); break;
586                         case MONO_TABLE_EVENTMAP:
587                                 g_assert (i == 0 || i == 1);
588                                 field_size = i ? idx_size (meta, MONO_TABLE_EVENT):
589                                         idx_size (meta, MONO_TABLE_TYPEDEF);
590                                 break;
591                         case MONO_TABLE_EVENT_POINTER:
592                                 g_assert (i == 0);
593                                 field_size = idx_size (meta, MONO_TABLE_EVENT); break;
594                         case MONO_TABLE_EXPORTEDTYPE:
595                                 g_assert (i == 1);
596                                 /* the index is in another metadata file, so it must be 4 */
597                                 field_size = 4; break;
598                         case MONO_TABLE_FIELDLAYOUT:
599                                 g_assert (i == 1);
600                                 field_size = idx_size (meta, MONO_TABLE_FIELD); break;
601                         case MONO_TABLE_FIELDRVA:
602                                 g_assert (i == 1);
603                                 field_size = idx_size (meta, MONO_TABLE_FIELD); break;
604                         case MONO_TABLE_FIELD_POINTER:
605                                 g_assert (i == 0);
606                                 field_size = idx_size (meta, MONO_TABLE_FIELD); break;
607                         case MONO_TABLE_IMPLMAP:
608                                 g_assert (i == 3);
609                                 field_size = idx_size (meta, MONO_TABLE_MODULEREF); break;
610                         case MONO_TABLE_INTERFACEIMPL:
611                                 g_assert (i == 0);
612                                 field_size = idx_size (meta, MONO_TABLE_TYPEDEF); break;
613                         case MONO_TABLE_METHOD:
614                                 g_assert (i == 5);
615                                 field_size = idx_size (meta, MONO_TABLE_PARAM); break;
616                         case MONO_TABLE_METHODIMPL:
617                                 g_assert (i == 0);
618                                 field_size = idx_size (meta, MONO_TABLE_TYPEDEF); break;
619                         case MONO_TABLE_METHODSEMANTICS:
620                                 g_assert (i == 1);
621                                 field_size = idx_size (meta, MONO_TABLE_METHOD); break;
622                         case MONO_TABLE_METHOD_POINTER:
623                                 g_assert (i == 0);
624                                 field_size = idx_size (meta, MONO_TABLE_METHOD); break;
625                         case MONO_TABLE_NESTEDCLASS:
626                                 g_assert (i == 0 || i == 1);
627                                 field_size = idx_size (meta, MONO_TABLE_TYPEDEF); break;
628                         case MONO_TABLE_PARAM_POINTER:
629                                 g_assert (i == 0);
630                                 field_size = idx_size (meta, MONO_TABLE_PARAM); break;
631                         case MONO_TABLE_PROPERTYMAP:
632                                 g_assert (i == 0 || i == 1);
633                                 field_size = i ? idx_size (meta, MONO_TABLE_PROPERTY):
634                                         idx_size (meta, MONO_TABLE_TYPEDEF);
635                                 break;
636                         case MONO_TABLE_PROPERTY_POINTER:
637                                 g_assert (i == 0);
638                                 field_size = idx_size (meta, MONO_TABLE_PROPERTY); break;
639                         case MONO_TABLE_TYPEDEF:
640                                 g_assert (i == 4 || i == 5);
641                                 field_size = i == 4 ? idx_size (meta, MONO_TABLE_FIELD):
642                                         idx_size (meta, MONO_TABLE_METHOD);
643                                 break;
644                         case MONO_TABLE_GENERICPARAM:
645                                 g_assert (i == 2);
646                                 n = MAX (meta->tables [MONO_TABLE_METHOD].rows, meta->tables [MONO_TABLE_TYPEDEF].rows);
647                                 /*This is a coded token for 2 tables, so takes 1 bit */
648                                 field_size = rtsize (meta, n, 16 - MONO_TYPEORMETHOD_BITS);
649                                 break;
650                         case MONO_TABLE_GENERICPARAMCONSTRAINT:
651                                 g_assert (i == 0);
652                                 field_size = idx_size (meta, MONO_TABLE_GENERICPARAM);
653                                 break;
654                         case MONO_TABLE_LOCALSCOPE:
655                                 switch (i) {
656                                 case 0:
657                                         // FIXME: This table is in another file
658                                         field_size = idx_size (meta, MONO_TABLE_METHOD);
659                                         break;
660                                 case 1:
661                                         field_size = idx_size (meta, MONO_TABLE_IMPORTSCOPE);
662                                         break;
663                                 case 2:
664                                         field_size = idx_size (meta, MONO_TABLE_LOCALVARIABLE);
665                                         break;
666                                 case 3:
667                                         field_size = idx_size (meta, MONO_TABLE_LOCALCONSTANT);
668                                         break;
669                                 default:
670                                         g_assert_not_reached ();
671                                         break;
672                                 }
673                                 break;
674                         case MONO_TABLE_METHODBODY:
675                                 g_assert (i == 0);
676                                 field_size = idx_size (meta, MONO_TABLE_DOCUMENT);
677                                 break;
678                         default:
679                                 g_error ("Can't handle MONO_MT_TABLE_IDX for table %d element %d", tableindex, i);
680                         }
681                         break;
682
683                         /*
684                          * HasConstant: ParamDef, FieldDef, Property
685                          */
686                 case MONO_MT_CONST_IDX:
687                         n = MAX (meta->tables [MONO_TABLE_PARAM].rows,
688                                  meta->tables [MONO_TABLE_FIELD].rows);
689                         n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
690
691                         /* 2 bits to encode tag */
692                         field_size = rtsize (meta, n, 16-2);
693                         break;
694
695                         /*
696                          * HasCustomAttribute: points to any table but
697                          * itself.
698                          */
699                 case MONO_MT_HASCAT_IDX:
700                         /*
701                          * We believe that since the signature and
702                          * permission are indexing the Blob heap,
703                          * we should consider the blob size first
704                          */
705                         /* I'm not a believer - lupus
706                         if (meta->idx_blob_wide){
707                                 field_size = 4;
708                                 break;
709                         }*/
710                         
711                         n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
712                                  meta->tables [MONO_TABLE_FIELD].rows);
713                         n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
714                         n = MAX (n, meta->tables [MONO_TABLE_TYPEDEF].rows);
715                         n = MAX (n, meta->tables [MONO_TABLE_PARAM].rows);
716                         n = MAX (n, meta->tables [MONO_TABLE_INTERFACEIMPL].rows);
717                         n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
718                         n = MAX (n, meta->tables [MONO_TABLE_MODULE].rows);
719                         n = MAX (n, meta->tables [MONO_TABLE_DECLSECURITY].rows);
720                         n = MAX (n, meta->tables [MONO_TABLE_PROPERTY].rows);
721                         n = MAX (n, meta->tables [MONO_TABLE_EVENT].rows);
722                         n = MAX (n, meta->tables [MONO_TABLE_STANDALONESIG].rows);
723                         n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
724                         n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
725                         n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
726                         n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
727                         n = MAX (n, meta->tables [MONO_TABLE_FILE].rows);
728                         n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
729                         n = MAX (n, meta->tables [MONO_TABLE_MANIFESTRESOURCE].rows);
730
731                         /* 5 bits to encode */
732                         field_size = rtsize (meta, n, 16-5);
733                         break;
734
735                         /*
736                          * CustomAttributeType: TypeDef, TypeRef, MethodDef, 
737                          * MemberRef and String.  
738                          */
739                 case MONO_MT_CAT_IDX:
740                         /* String is a heap, if it is wide, we know the size */
741                         /* See above, nope. 
742                         if (meta->idx_string_wide){
743                                 field_size = 4;
744                                 break;
745                         }*/
746                         
747                         n = MAX (meta->tables [MONO_TABLE_TYPEREF].rows,
748                                  meta->tables [MONO_TABLE_TYPEDEF].rows);
749                         n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
750                         n = MAX (n, meta->tables [MONO_TABLE_MEMBERREF].rows);
751
752                         /* 3 bits to encode */
753                         field_size = rtsize (meta, n, 16-3);
754                         break;
755
756                         /*
757                          * HasDeclSecurity: Typedef, MethodDef, Assembly
758                          */
759                 case MONO_MT_HASDEC_IDX:
760                         n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
761                                  meta->tables [MONO_TABLE_METHOD].rows);
762                         n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLY].rows);
763
764                         /* 2 bits to encode */
765                         field_size = rtsize (meta, n, 16-2);
766                         break;
767
768                         /*
769                          * Implementation: File, AssemblyRef, ExportedType
770                          */
771                 case MONO_MT_IMPL_IDX:
772                         n = MAX (meta->tables [MONO_TABLE_FILE].rows,
773                                  meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
774                         n = MAX (n, meta->tables [MONO_TABLE_EXPORTEDTYPE].rows);
775
776                         /* 2 bits to encode tag */
777                         field_size = rtsize (meta, n, 16-2);
778                         break;
779
780                         /*
781                          * HasFieldMarshall: FieldDef, ParamDef
782                          */
783                 case MONO_MT_HFM_IDX:
784                         n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
785                                  meta->tables [MONO_TABLE_PARAM].rows);
786
787                         /* 1 bit used to encode tag */
788                         field_size = rtsize (meta, n, 16-1);
789                         break;
790
791                         /*
792                          * MemberForwarded: FieldDef, MethodDef
793                          */
794                 case MONO_MT_MF_IDX:
795                         n = MAX (meta->tables [MONO_TABLE_FIELD].rows,
796                                  meta->tables [MONO_TABLE_METHOD].rows);
797
798                         /* 1 bit used to encode tag */
799                         field_size = rtsize (meta, n, 16-1);
800                         break;
801
802                         /*
803                          * TypeDefOrRef: TypeDef, ParamDef, TypeSpec
804                          * LAMESPEC
805                          * It is TypeDef, _TypeRef_, TypeSpec, instead.
806                          */
807                 case MONO_MT_TDOR_IDX:
808                         n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
809                                  meta->tables [MONO_TABLE_TYPEREF].rows);
810                         n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
811
812                         /* 2 bits to encode */
813                         field_size = rtsize (meta, n, 16-2);
814                         break;
815
816                         /*
817                          * MemberRefParent: TypeDef, TypeRef, MethodDef, ModuleRef, TypeSpec, MemberRef
818                          */
819                 case MONO_MT_MRP_IDX:
820                         n = MAX (meta->tables [MONO_TABLE_TYPEDEF].rows,
821                                  meta->tables [MONO_TABLE_TYPEREF].rows);
822                         n = MAX (n, meta->tables [MONO_TABLE_METHOD].rows);
823                         n = MAX (n, meta->tables [MONO_TABLE_MODULEREF].rows);
824                         n = MAX (n, meta->tables [MONO_TABLE_TYPESPEC].rows);
825
826                         /* 3 bits to encode */
827                         field_size = rtsize (meta, n, 16 - 3);
828                         break;
829                         
830                         /*
831                          * MethodDefOrRef: MethodDef, MemberRef
832                          */
833                 case MONO_MT_MDOR_IDX:
834                         n = MAX (meta->tables [MONO_TABLE_METHOD].rows,
835                                  meta->tables [MONO_TABLE_MEMBERREF].rows);
836
837                         /* 1 bit used to encode tag */
838                         field_size = rtsize (meta, n, 16-1);
839                         break;
840                         
841                         /*
842                          * HasSemantics: Property, Event
843                          */
844                 case MONO_MT_HS_IDX:
845                         n = MAX (meta->tables [MONO_TABLE_PROPERTY].rows,
846                                  meta->tables [MONO_TABLE_EVENT].rows);
847
848                         /* 1 bit used to encode tag */
849                         field_size = rtsize (meta, n, 16-1);
850                         break;
851
852                         /*
853                          * ResolutionScope: Module, ModuleRef, AssemblyRef, TypeRef
854                          */
855                 case MONO_MT_RS_IDX:
856                         n = MAX (meta->tables [MONO_TABLE_MODULE].rows,
857                                  meta->tables [MONO_TABLE_MODULEREF].rows);
858                         n = MAX (n, meta->tables [MONO_TABLE_ASSEMBLYREF].rows);
859                         n = MAX (n, meta->tables [MONO_TABLE_TYPEREF].rows);
860
861                         /* 2 bits used to encode tag (ECMA spec claims 3) */
862                         field_size = rtsize (meta, n, 16 - 2);
863                         break;
864                 }
865
866                 /*
867                  * encode field size as follows (we just need to
868                  * distinguish them).
869                  *
870                  * 4 -> 3
871                  * 2 -> 1
872                  * 1 -> 0
873                  */
874                 bitfield |= (field_size-1) << shift;
875                 shift += 2;
876                 size += field_size;
877                 /*g_print ("table %02x field %d size %d\n", tableindex, i, field_size);*/
878         }
879
880         *result_bitfield = (i << 24) | bitfield;
881         return size;
882 }
883
884 /**
885  * mono_metadata_compute_table_bases:
886  * @meta: metadata context to compute table values
887  *
888  * Computes the table bases for the metadata structure.
889  * This is an internal function used by the image loader code.
890  */
891 void
892 mono_metadata_compute_table_bases (MonoImage *meta)
893 {
894         int i;
895         const char *base = meta->tables_base;
896         
897         for (i = 0; i < MONO_TABLE_NUM; i++) {
898                 MonoTableInfo *table = &meta->tables [i];
899                 if (table->rows == 0)
900                         continue;
901
902                 table->row_size = mono_metadata_compute_size (meta, i, &table->size_bitfield);
903                 table->base = base;
904                 base += table->rows * table->row_size;
905         }
906 }
907
908 /**
909  * mono_metadata_locate:
910  * @meta: metadata context
911  * @table: table code.
912  * @idx: index of element to retrieve from @table.
913  *
914  * Returns: a pointer to the @idx element in the metadata table
915  * whose code is @table.
916  */
917 const char *
918 mono_metadata_locate (MonoImage *meta, int table, int idx)
919 {
920         /* idx == 0 refers always to NULL */
921         g_return_val_if_fail (idx > 0 && idx <= meta->tables [table].rows, ""); /*FIXME shouldn't we return NULL here?*/
922            
923         return meta->tables [table].base + (meta->tables [table].row_size * (idx - 1));
924 }
925
926 /**
927  * mono_metadata_locate_token:
928  * @meta: metadata context
929  * @token: metadata token
930  *
931  * Returns: a pointer to the data in the metadata represented by the
932  * token #token.
933  */
934 const char *
935 mono_metadata_locate_token (MonoImage *meta, guint32 token)
936 {
937         return mono_metadata_locate (meta, token >> 24, token & 0xffffff);
938 }
939
940 /**
941  * mono_metadata_string_heap:
942  * @meta: metadata context
943  * @index: index into the string heap.
944  *
945  * Returns: an in-memory pointer to the @index in the string heap.
946  */
947 const char *
948 mono_metadata_string_heap (MonoImage *meta, guint32 index)
949 {
950         g_assert (index < meta->heap_strings.size);
951         g_return_val_if_fail (index < meta->heap_strings.size, "");
952         return meta->heap_strings.data + index;
953 }
954
955 /**
956  * mono_metadata_user_string:
957  * @meta: metadata context
958  * @index: index into the user string heap.
959  *
960  * Returns: an in-memory pointer to the @index in the user string heap ("#US").
961  */
962 const char *
963 mono_metadata_user_string (MonoImage *meta, guint32 index)
964 {
965         g_assert (index < meta->heap_us.size);
966         g_return_val_if_fail (index < meta->heap_us.size, "");
967         return meta->heap_us.data + index;
968 }
969
970 /**
971  * mono_metadata_blob_heap:
972  * @meta: metadata context
973  * @index: index into the blob.
974  *
975  * Returns: an in-memory pointer to the @index in the Blob heap.
976  */
977 const char *
978 mono_metadata_blob_heap (MonoImage *meta, guint32 index)
979 {
980         g_assert (index < meta->heap_blob.size);
981         g_return_val_if_fail (index < meta->heap_blob.size, "");/*FIXME shouldn't we return NULL and check for index == 0?*/
982         return meta->heap_blob.data + index;
983 }
984
985 /**
986  * mono_metadata_guid_heap:
987  * @meta: metadata context
988  * @index: index into the guid heap.
989  *
990  * Returns: an in-memory pointer to the @index in the guid heap.
991  */
992 const char *
993 mono_metadata_guid_heap (MonoImage *meta, guint32 index)
994 {
995         --index;
996         index *= 16; /* adjust for guid size and 1-based index */
997         g_return_val_if_fail (index < meta->heap_guid.size, "");
998         return meta->heap_guid.data + index;
999 }
1000
1001 static const unsigned char *
1002 dword_align (const unsigned char *ptr)
1003 {
1004 #if SIZEOF_VOID_P == 8
1005         return (const unsigned char *) (((guint64) (ptr + 3)) & ~3);
1006 #else
1007         return (const unsigned char *) (((guint32) (ptr + 3)) & ~3);
1008 #endif
1009 }
1010
1011 /**
1012  * mono_metadata_decode_row:
1013  * @t: table to extract information from.
1014  * @idx: index in table.
1015  * @res: array of @res_size cols to store the results in
1016  *
1017  * This decompresses the metadata element @idx in table @t
1018  * into the guint32 @res array that has res_size elements
1019  */
1020 void
1021 mono_metadata_decode_row (const MonoTableInfo *t, int idx, guint32 *res, int res_size)
1022 {
1023         guint32 bitfield = t->size_bitfield;
1024         int i, count = mono_metadata_table_count (bitfield);
1025         const char *data;
1026
1027         g_assert (idx < t->rows);
1028         g_assert (idx >= 0);
1029         data = t->base + idx * t->row_size;
1030         
1031         g_assert (res_size == count);
1032
1033         for (i = 0; i < count; i++) {
1034                 int n = mono_metadata_table_size (bitfield, i);
1035
1036                 switch (n){
1037                 case 1:
1038                         res [i] = *data; break;
1039                 case 2:
1040                         res [i] = read16 (data); break;
1041                 case 4:
1042                         res [i] = read32 (data); break;
1043                 default:
1044                         g_assert_not_reached ();
1045                 }
1046                 data += n;
1047         }
1048 }
1049
1050 /**
1051  * mono_metadata_decode_row_col:
1052  * @t: table to extract information from.
1053  * @idx: index for row in table.
1054  * @col: column in the row.
1055  *
1056  * This function returns the value of column @col from the @idx
1057  * row in the table @t.
1058  */
1059 guint32
1060 mono_metadata_decode_row_col (const MonoTableInfo *t, int idx, guint col)
1061 {
1062         guint32 bitfield = t->size_bitfield;
1063         int i;
1064         register const char *data; 
1065         register int n;
1066         
1067         g_assert (idx < t->rows);
1068         g_assert (col < mono_metadata_table_count (bitfield));
1069         data = t->base + idx * t->row_size;
1070
1071         n = mono_metadata_table_size (bitfield, 0);
1072         for (i = 0; i < col; ++i) {
1073                 data += n;
1074                 n = mono_metadata_table_size (bitfield, i + 1);
1075         }
1076         switch (n) {
1077         case 1:
1078                 return *data;
1079         case 2:
1080                 return read16 (data);
1081         case 4:
1082                 return read32 (data);
1083         default:
1084                 g_assert_not_reached ();
1085         }
1086         return 0;
1087 }
1088
1089 /**
1090  * mono_metadata_decode_blob_size:
1091  * @ptr: pointer to a blob object
1092  * @rptr: the new position of the pointer
1093  *
1094  * This decodes a compressed size as described by 23.1.4 (a blob or user string object)
1095  *
1096  * Returns: the size of the blob object
1097  */
1098 guint32
1099 mono_metadata_decode_blob_size (const char *xptr, const char **rptr)
1100 {
1101         const unsigned char *ptr = (const unsigned char *)xptr;
1102         guint32 size;
1103         
1104         if ((*ptr & 0x80) == 0){
1105                 size = ptr [0] & 0x7f;
1106                 ptr++;
1107         } else if ((*ptr & 0x40) == 0){
1108                 size = ((ptr [0] & 0x3f) << 8) + ptr [1];
1109                 ptr += 2;
1110         } else {
1111                 size = ((ptr [0] & 0x1f) << 24) +
1112                         (ptr [1] << 16) +
1113                         (ptr [2] << 8) +
1114                         ptr [3];
1115                 ptr += 4;
1116         }
1117         if (rptr)
1118                 *rptr = (char*)ptr;
1119         return size;
1120 }
1121
1122 /**
1123  * mono_metadata_decode_value:
1124  * @ptr: pointer to decode from
1125  * @rptr: the new position of the pointer
1126  *
1127  * This routine decompresses 32-bit values as specified in the "Blob and
1128  * Signature" section (22.2)
1129  *
1130  * Returns: the decoded value
1131  */
1132 guint32
1133 mono_metadata_decode_value (const char *_ptr, const char **rptr)
1134 {
1135         const unsigned char *ptr = (const unsigned char *) _ptr;
1136         unsigned char b = *ptr;
1137         guint32 len;
1138         
1139         if ((b & 0x80) == 0){
1140                 len = b;
1141                 ++ptr;
1142         } else if ((b & 0x40) == 0){
1143                 len = ((b & 0x3f) << 8 | ptr [1]);
1144                 ptr += 2;
1145         } else {
1146                 len = ((b & 0x1f) << 24) |
1147                         (ptr [1] << 16) |
1148                         (ptr [2] << 8) |
1149                         ptr [3];
1150                 ptr += 4;
1151         }
1152         if (rptr)
1153                 *rptr = (char*)ptr;
1154         
1155         return len;
1156 }
1157
1158 /**
1159  * mono_metadata_decode_signed_value:
1160  * @ptr: pointer to decode from
1161  * @rptr: the new position of the pointer
1162  *
1163  * This routine decompresses 32-bit signed values
1164  * (not specified in the spec)
1165  *
1166  * Returns: the decoded value
1167  */
1168 gint32
1169 mono_metadata_decode_signed_value (const char *ptr, const char **rptr)
1170 {
1171         guint32 uval = mono_metadata_decode_value (ptr, rptr);
1172         gint32 ival = uval >> 1;
1173         if (!(uval & 1))
1174                 return ival;
1175         /* ival is a truncated 2's complement negative number.  */
1176         if (ival < 0x40)
1177                 /* 6 bits = 7 bits for compressed representation (top bit is '0') - 1 sign bit */
1178                 return ival - 0x40;
1179         if (ival < 0x2000)
1180                 /* 13 bits = 14 bits for compressed representation (top bits are '10') - 1 sign bit */
1181                 return ival - 0x2000;
1182         if (ival < 0x10000000)
1183                 /* 28 bits = 29 bits for compressed representation (top bits are '110') - 1 sign bit */
1184                 return ival - 0x10000000;
1185         g_assert (ival < 0x20000000);
1186         g_warning ("compressed signed value appears to use 29 bits for compressed representation: %x (raw: %8x)", ival, uval);
1187         return ival - 0x20000000;
1188 }
1189
1190 /* 
1191  * Translates the given 1-based index into the Method, Field, Event, or Param tables
1192  * using the *Ptr tables in uncompressed metadata, if they are available.
1193  *
1194  * FIXME: The caller is not forced to call this function, which is error-prone, since 
1195  * forgetting to call it would only show up as a bug on uncompressed metadata.
1196  */
1197 guint32
1198 mono_metadata_translate_token_index (MonoImage *image, int table, guint32 idx)
1199 {
1200         if (!image->uncompressed_metadata)
1201                 return idx;
1202
1203         switch (table) {
1204         case MONO_TABLE_METHOD:
1205                 if (image->tables [MONO_TABLE_METHOD_POINTER].rows)
1206                         return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_METHOD_POINTER], idx - 1, MONO_METHOD_POINTER_METHOD);
1207                 else
1208                         return idx;
1209         case MONO_TABLE_FIELD:
1210                 if (image->tables [MONO_TABLE_FIELD_POINTER].rows)
1211                         return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_FIELD_POINTER], idx - 1, MONO_FIELD_POINTER_FIELD);
1212                 else
1213                         return idx;
1214         case MONO_TABLE_EVENT:
1215                 if (image->tables [MONO_TABLE_EVENT_POINTER].rows)
1216                         return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_EVENT_POINTER], idx - 1, MONO_EVENT_POINTER_EVENT);
1217                 else
1218                         return idx;
1219         case MONO_TABLE_PROPERTY:
1220                 if (image->tables [MONO_TABLE_PROPERTY_POINTER].rows)
1221                         return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_PROPERTY_POINTER], idx - 1, MONO_PROPERTY_POINTER_PROPERTY);
1222                 else
1223                         return idx;
1224         case MONO_TABLE_PARAM:
1225                 if (image->tables [MONO_TABLE_PARAM_POINTER].rows)
1226                         return mono_metadata_decode_row_col (&image->tables [MONO_TABLE_PARAM_POINTER], idx - 1, MONO_PARAM_POINTER_PARAM);
1227                 else
1228                         return idx;
1229         default:
1230                 return idx;
1231         }
1232 }
1233
1234 /**
1235  * mono_metadata_decode_table_row:
1236  *
1237  *   Same as mono_metadata_decode_row, but takes an IMAGE+TABLE ID pair, and takes
1238  * uncompressed metadata into account, so it should be used to access the
1239  * Method, Field, Param and Event tables when the access is made from metadata, i.e.
1240  * IDX is retrieved from a metadata table, like MONO_TYPEDEF_FIELD_LIST.
1241  */
1242 void
1243 mono_metadata_decode_table_row (MonoImage *image, int table, int idx, guint32 *res, int res_size)
1244 {
1245         if (image->uncompressed_metadata)
1246                 idx = mono_metadata_translate_token_index (image, table, idx + 1) - 1;
1247
1248         mono_metadata_decode_row (&image->tables [table], idx, res, res_size);
1249 }
1250
1251 /**
1252  * mono_metadata_decode_table_row_col:
1253  *
1254  *   Same as mono_metadata_decode_row_col, but takes an IMAGE+TABLE ID pair, and takes
1255  * uncompressed metadata into account, so it should be used to access the
1256  * Method, Field, Param and Event tables.
1257  */
1258 guint32 mono_metadata_decode_table_row_col (MonoImage *image, int table, int idx, guint col)
1259 {
1260         if (image->uncompressed_metadata)
1261                 idx = mono_metadata_translate_token_index (image, table, idx + 1) - 1;
1262
1263         return mono_metadata_decode_row_col (&image->tables [table], idx, col);
1264 }
1265
1266 /*
1267  * mono_metadata_parse_typedef_or_ref:
1268  * @m: a metadata context.
1269  * @ptr: a pointer to an encoded TypedefOrRef in @m
1270  * @rptr: pointer updated to match the end of the decoded stream
1271  *
1272  * Returns: a token valid in the @m metadata decoded from
1273  * the compressed representation.
1274  */
1275 guint32
1276 mono_metadata_parse_typedef_or_ref (MonoImage *m, const char *ptr, const char **rptr)
1277 {
1278         guint32 token;
1279         token = mono_metadata_decode_value (ptr, &ptr);
1280         if (rptr)
1281                 *rptr = ptr;
1282         return mono_metadata_token_from_dor (token);
1283 }
1284
1285 /*
1286  * mono_metadata_parse_custom_mod:
1287  * @m: a metadata context.
1288  * @dest: storage where the info about the custom modifier is stored (may be NULL)
1289  * @ptr: a pointer to (possibly) the start of a custom modifier list
1290  * @rptr: pointer updated to match the end of the decoded stream
1291  *
1292  * Checks if @ptr points to a type custom modifier compressed representation.
1293  *
1294  * Returns: #TRUE if a custom modifier was found, #FALSE if not.
1295  */
1296 int
1297 mono_metadata_parse_custom_mod (MonoImage *m, MonoCustomMod *dest, const char *ptr, const char **rptr)
1298 {
1299         MonoCustomMod local;
1300         if ((*ptr == MONO_TYPE_CMOD_OPT) || (*ptr == MONO_TYPE_CMOD_REQD)) {
1301                 if (!dest)
1302                         dest = &local;
1303                 dest->required = *ptr == MONO_TYPE_CMOD_REQD ? 1 : 0;
1304                 dest->token = mono_metadata_parse_typedef_or_ref (m, ptr + 1, rptr);
1305                 return TRUE;
1306         }
1307         return FALSE;
1308 }
1309
1310 /*
1311  * mono_metadata_parse_array_internal:
1312  * @m: a metadata context.
1313  * @transient: whenever to allocate data from the heap
1314  * @ptr: a pointer to an encoded array description.
1315  * @rptr: pointer updated to match the end of the decoded stream
1316  *
1317  * Decodes the compressed array description found in the metadata @m at @ptr.
1318  *
1319  * Returns: a #MonoArrayType structure describing the array type
1320  * and dimensions. Memory is allocated from the heap or from the image mempool, depending
1321  * on the value of @transient.
1322  *
1323  * LOCKING: Acquires the loader lock
1324  */
1325 static MonoArrayType *
1326 mono_metadata_parse_array_internal (MonoImage *m, MonoGenericContainer *container,
1327                                                                         gboolean transient, const char *ptr, const char **rptr, MonoError *error)
1328 {
1329         int i;
1330         MonoArrayType *array;
1331         MonoType *etype;
1332         
1333         etype = mono_metadata_parse_type_checked (m, container, 0, FALSE, ptr, &ptr, error); //FIXME this doesn't respect @transient
1334         if (!etype)
1335                 return NULL;
1336
1337         array = transient ? (MonoArrayType *)g_malloc0 (sizeof (MonoArrayType)) : (MonoArrayType *)mono_image_alloc0 (m, sizeof (MonoArrayType));
1338         array->eklass = mono_class_from_mono_type (etype);
1339         array->rank = mono_metadata_decode_value (ptr, &ptr);
1340
1341         array->numsizes = mono_metadata_decode_value (ptr, &ptr);
1342         if (array->numsizes)
1343                 array->sizes = transient ? (int *)g_malloc0 (sizeof (int) * array->numsizes) : (int *)mono_image_alloc0 (m, sizeof (int) * array->numsizes);
1344         for (i = 0; i < array->numsizes; ++i)
1345                 array->sizes [i] = mono_metadata_decode_value (ptr, &ptr);
1346
1347         array->numlobounds = mono_metadata_decode_value (ptr, &ptr);
1348         if (array->numlobounds)
1349                 array->lobounds = transient ? (int *)g_malloc0 (sizeof (int) * array->numlobounds) : (int *)mono_image_alloc0 (m, sizeof (int) * array->numlobounds);
1350         for (i = 0; i < array->numlobounds; ++i)
1351                 array->lobounds [i] = mono_metadata_decode_signed_value (ptr, &ptr);
1352
1353         if (rptr)
1354                 *rptr = ptr;
1355         return array;
1356 }
1357
1358 MonoArrayType *
1359 mono_metadata_parse_array (MonoImage *m, const char *ptr, const char **rptr)
1360 {
1361         MonoError error;
1362         MonoArrayType *ret = mono_metadata_parse_array_internal (m, NULL, FALSE, ptr, rptr, &error);
1363         mono_error_cleanup (&error);
1364
1365         return ret;
1366 }
1367
1368 /*
1369  * mono_metadata_free_array:
1370  * @array: array description
1371  *
1372  * Frees the array description returned from mono_metadata_parse_array().
1373  */
1374 void
1375 mono_metadata_free_array (MonoArrayType *array)
1376 {
1377         g_free (array->sizes);
1378         g_free (array->lobounds);
1379         g_free (array);
1380 }
1381
1382 /*
1383  * need to add common field and param attributes combinations:
1384  * [out] param
1385  * public static
1386  * public static literal
1387  * private
1388  * private static
1389  * private static literal
1390  */
1391 static const MonoType
1392 builtin_types[] = {
1393         /* data, attrs, type,              nmods, byref, pinned */
1394         {{NULL}, 0,     MONO_TYPE_VOID,    0,     0,     0},
1395         {{NULL}, 0,     MONO_TYPE_BOOLEAN, 0,     0,     0},
1396         {{NULL}, 0,     MONO_TYPE_BOOLEAN, 0,     1,     0},
1397         {{NULL}, 0,     MONO_TYPE_CHAR,    0,     0,     0},
1398         {{NULL}, 0,     MONO_TYPE_CHAR,    0,     1,     0},
1399         {{NULL}, 0,     MONO_TYPE_I1,      0,     0,     0},
1400         {{NULL}, 0,     MONO_TYPE_I1,      0,     1,     0},
1401         {{NULL}, 0,     MONO_TYPE_U1,      0,     0,     0},
1402         {{NULL}, 0,     MONO_TYPE_U1,      0,     1,     0},
1403         {{NULL}, 0,     MONO_TYPE_I2,      0,     0,     0},
1404         {{NULL}, 0,     MONO_TYPE_I2,      0,     1,     0},
1405         {{NULL}, 0,     MONO_TYPE_U2,      0,     0,     0},
1406         {{NULL}, 0,     MONO_TYPE_U2,      0,     1,     0},
1407         {{NULL}, 0,     MONO_TYPE_I4,      0,     0,     0},
1408         {{NULL}, 0,     MONO_TYPE_I4,      0,     1,     0},
1409         {{NULL}, 0,     MONO_TYPE_U4,      0,     0,     0},
1410         {{NULL}, 0,     MONO_TYPE_U4,      0,     1,     0},
1411         {{NULL}, 0,     MONO_TYPE_I8,      0,     0,     0},
1412         {{NULL}, 0,     MONO_TYPE_I8,      0,     1,     0},
1413         {{NULL}, 0,     MONO_TYPE_U8,      0,     0,     0},
1414         {{NULL}, 0,     MONO_TYPE_U8,      0,     1,     0},
1415         {{NULL}, 0,     MONO_TYPE_R4,      0,     0,     0},
1416         {{NULL}, 0,     MONO_TYPE_R4,      0,     1,     0},
1417         {{NULL}, 0,     MONO_TYPE_R8,      0,     0,     0},
1418         {{NULL}, 0,     MONO_TYPE_R8,      0,     1,     0},
1419         {{NULL}, 0,     MONO_TYPE_STRING,  0,     0,     0},
1420         {{NULL}, 0,     MONO_TYPE_STRING,  0,     1,     0},
1421         {{NULL}, 0,     MONO_TYPE_OBJECT,  0,     0,     0},
1422         {{NULL}, 0,     MONO_TYPE_OBJECT,  0,     1,     0},
1423         {{NULL}, 0,     MONO_TYPE_TYPEDBYREF,  0,     0,     0},
1424         {{NULL}, 0,     MONO_TYPE_I,       0,     0,     0},
1425         {{NULL}, 0,     MONO_TYPE_I,       0,     1,     0},
1426         {{NULL}, 0,     MONO_TYPE_U,       0,     0,     0},
1427         {{NULL}, 0,     MONO_TYPE_U,       0,     1,     0},
1428 };
1429
1430 #define NBUILTIN_TYPES() (sizeof (builtin_types) / sizeof (builtin_types [0]))
1431
1432 static GHashTable *type_cache = NULL;
1433 static int next_generic_inst_id = 0;
1434
1435 /* Protected by image_sets_mutex */
1436 static MonoImageSet *mscorlib_image_set;
1437 /* Protected by image_sets_mutex */
1438 static GPtrArray *image_sets;
1439 static mono_mutex_t image_sets_mutex;
1440
1441 static guint mono_generic_class_hash (gconstpointer data);
1442
1443 /*
1444  * MonoTypes with modifies are never cached, so we never check or use that field.
1445  */
1446 static guint
1447 mono_type_hash (gconstpointer data)
1448 {
1449         const MonoType *type = (const MonoType *) data;
1450         if (type->type == MONO_TYPE_GENERICINST)
1451                 return mono_generic_class_hash (type->data.generic_class);
1452         else
1453                 return type->type | (type->byref << 8) | (type->attrs << 9);
1454 }
1455
1456 static gint
1457 mono_type_equal (gconstpointer ka, gconstpointer kb)
1458 {
1459         const MonoType *a = (const MonoType *) ka;
1460         const MonoType *b = (const MonoType *) kb;
1461         
1462         if (a->type != b->type || a->byref != b->byref || a->attrs != b->attrs || a->pinned != b->pinned)
1463                 return 0;
1464         /* need other checks */
1465         return 1;
1466 }
1467
1468 guint
1469 mono_metadata_generic_inst_hash (gconstpointer data)
1470 {
1471         const MonoGenericInst *ginst = (const MonoGenericInst *) data;
1472         guint hash = 0;
1473         int i;
1474         
1475         for (i = 0; i < ginst->type_argc; ++i) {
1476                 hash *= 13;
1477                 hash += mono_metadata_type_hash (ginst->type_argv [i]);
1478         }
1479
1480         return hash ^ (ginst->is_open << 8);
1481 }
1482
1483 static gboolean
1484 mono_generic_inst_equal_full (const MonoGenericInst *a, const MonoGenericInst *b, gboolean signature_only)
1485 {
1486         int i;
1487
1488         // An optimization: if the ids of two insts are the same, we know they are the same inst and don't check contents.
1489         // Furthermore, because we perform early de-duping, if the ids differ, we know the contents differ.
1490 #ifndef MONO_SMALL_CONFIG // Optimization does not work in MONO_SMALL_CONFIG: There are no IDs
1491         if (a->id && b->id) { // "id 0" means "object has no id"-- de-duping hasn't been performed yet, must check contents.
1492                 if (a->id == b->id)
1493                         return TRUE;
1494                 // In signature-comparison mode id equality implies object equality, but this is not true for inequality.
1495                 // Two separate objects could have signature-equavalent contents.
1496                 if (!signature_only)
1497                         return FALSE;
1498         }
1499 #endif
1500
1501         if (a->is_open != b->is_open || a->type_argc != b->type_argc)
1502                 return FALSE;
1503         for (i = 0; i < a->type_argc; ++i) {
1504                 if (!do_mono_metadata_type_equal (a->type_argv [i], b->type_argv [i], signature_only))
1505                         return FALSE;
1506         }
1507         return TRUE;
1508 }
1509
1510 gboolean
1511 mono_metadata_generic_inst_equal (gconstpointer ka, gconstpointer kb)
1512 {
1513         const MonoGenericInst *a = (const MonoGenericInst *) ka;
1514         const MonoGenericInst *b = (const MonoGenericInst *) kb;
1515
1516         return mono_generic_inst_equal_full (a, b, FALSE);
1517 }
1518
1519 static guint
1520 mono_generic_class_hash (gconstpointer data)
1521 {
1522         const MonoGenericClass *gclass = (const MonoGenericClass *) data;
1523         guint hash = mono_metadata_type_hash (&gclass->container_class->byval_arg);
1524
1525         hash *= 13;
1526         hash += gclass->is_tb_open;
1527         hash += mono_metadata_generic_context_hash (&gclass->context);
1528
1529         return hash;
1530 }
1531
1532 static gboolean
1533 mono_generic_class_equal (gconstpointer ka, gconstpointer kb)
1534 {
1535         const MonoGenericClass *a = (const MonoGenericClass *) ka;
1536         const MonoGenericClass *b = (const MonoGenericClass *) kb;
1537
1538         return _mono_metadata_generic_class_equal (a, b, FALSE);
1539 }
1540
1541 /**
1542  * mono_metadata_init:
1543  *
1544  * Initialize the global variables of this module.
1545  * This is a Mono runtime internal function.
1546  */
1547 void
1548 mono_metadata_init (void)
1549 {
1550         int i;
1551
1552         type_cache = g_hash_table_new (mono_type_hash, mono_type_equal);
1553
1554         for (i = 0; i < NBUILTIN_TYPES (); ++i)
1555                 g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
1556
1557         mono_os_mutex_init_recursive (&image_sets_mutex);
1558 }
1559
1560 /**
1561  * mono_metadata_cleanup:
1562  *
1563  * Free all resources used by this module.
1564  * This is a Mono runtime internal function.
1565  */
1566 void
1567 mono_metadata_cleanup (void)
1568 {
1569         g_hash_table_destroy (type_cache);
1570         type_cache = NULL;
1571         g_ptr_array_free (image_sets, TRUE);
1572         image_sets = NULL;
1573         mono_os_mutex_destroy (&image_sets_mutex);
1574 }
1575
1576 /**
1577  * mono_metadata_parse_type:
1578  * @m: metadata context
1579  * @mode: king of type that may be found at @ptr
1580  * @opt_attrs: optional attributes to store in the returned type
1581  * @ptr: pointer to the type representation
1582  * @rptr: pointer updated to match the end of the decoded stream
1583  * @transient: whenever to allocate the result from the heap or from a mempool
1584  * 
1585  * Decode a compressed type description found at @ptr in @m.
1586  * @mode can be one of MONO_PARSE_MOD_TYPE, MONO_PARSE_PARAM, MONO_PARSE_RET,
1587  * MONO_PARSE_FIELD, MONO_PARSE_LOCAL, MONO_PARSE_TYPE.
1588  * This function can be used to decode type descriptions in method signatures,
1589  * field signatures, locals signatures etc.
1590  *
1591  * To parse a generic type, `generic_container' points to the current class'es
1592  * (the `generic_container' field in the MonoClass) or the current generic method's
1593  * (stored in image->property_hash) generic container.
1594  * When we encounter any MONO_TYPE_VAR or MONO_TYPE_MVAR's, they're looked up in
1595  * this MonoGenericContainer.
1596  *
1597  * LOCKING: Acquires the loader lock.
1598  *
1599  * Returns: a #MonoType structure representing the decoded type.
1600  */
1601 static MonoType*
1602 mono_metadata_parse_type_internal (MonoImage *m, MonoGenericContainer *container,
1603                                                                    short opt_attrs, gboolean transient, const char *ptr, const char **rptr, MonoError *error)
1604 {
1605         MonoType *type, *cached;
1606         MonoType stype;
1607         gboolean byref = FALSE;
1608         gboolean pinned = FALSE;
1609         const char *tmp_ptr;
1610         int count = 0; // Number of mod arguments
1611         gboolean found;
1612
1613         mono_error_init (error);
1614
1615         /*
1616          * According to the spec, custom modifiers should come before the byref
1617          * flag, but the IL produced by ilasm from the following signature:
1618          *   object modopt(...) &
1619          * starts with a byref flag, followed by the modifiers. (bug #49802)
1620          * Also, this type seems to be different from 'object & modopt(...)'. Maybe
1621          * it would be better to treat byref as real type constructor instead of
1622          * a modifier...
1623          * Also, pinned should come before anything else, but some MSV++ produced
1624          * assemblies violate this (#bug 61990).
1625          */
1626
1627         /* Count the modifiers first */
1628         tmp_ptr = ptr;
1629         found = TRUE;
1630         while (found) {
1631                 switch (*tmp_ptr) {
1632                 case MONO_TYPE_PINNED:
1633                 case MONO_TYPE_BYREF:
1634                         ++tmp_ptr;
1635                         break;
1636                 case MONO_TYPE_CMOD_REQD:
1637                 case MONO_TYPE_CMOD_OPT:
1638                         count ++;
1639                         mono_metadata_parse_custom_mod (m, NULL, tmp_ptr, &tmp_ptr);
1640                         break;
1641                 default:
1642                         found = FALSE;
1643                 }
1644         }
1645
1646         if (count) { // There are mods, so the MonoType will be of nonstandard size.
1647                 int size;
1648
1649                 size = MONO_SIZEOF_TYPE + ((gint32)count) * sizeof (MonoCustomMod);
1650                 type = transient ? (MonoType *)g_malloc0 (size) : (MonoType *)mono_image_alloc0 (m, size);
1651                 type->num_mods = count;
1652                 if (count > 64) {
1653                         mono_error_set_bad_image (error, m, "Invalid type with more than 64 modifiers");
1654                         return NULL;
1655                 }
1656         } else {     // The type is of standard size, so we can allocate it on the stack.
1657                 type = &stype;
1658                 memset (type, 0, MONO_SIZEOF_TYPE);
1659         }
1660
1661         /* Iterate again, but now parse pinned, byref and custom modifiers */
1662         found = TRUE;
1663         count = 0;
1664         while (found) {
1665                 switch (*ptr) {
1666                 case MONO_TYPE_PINNED:
1667                         pinned = TRUE;
1668                         ++ptr;
1669                         break;
1670                 case MONO_TYPE_BYREF:
1671                         byref = TRUE;
1672                         ++ptr;
1673                         break;
1674                 case MONO_TYPE_CMOD_REQD:
1675                 case MONO_TYPE_CMOD_OPT:
1676                         mono_metadata_parse_custom_mod (m, &(type->modifiers [count]), ptr, &ptr);
1677                         count ++;
1678                         break;
1679                 default:
1680                         found = FALSE;
1681                 }
1682         }
1683         
1684         type->attrs = opt_attrs;
1685         type->byref = byref;
1686         type->pinned = pinned ? 1 : 0;
1687
1688         if (!do_mono_metadata_parse_type (type, m, container, transient, ptr, &ptr, error))
1689                 return NULL;
1690
1691         if (rptr)
1692                 *rptr = ptr;
1693
1694         // Possibly we can return an already-allocated type instead of the one we decoded
1695         if (!type->num_mods && !transient) {
1696                 /* no need to free type here, because it is on the stack */
1697                 if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
1698                         MonoType *ret = type->byref ? &type->data.klass->this_arg : &type->data.klass->byval_arg;
1699
1700                         /* Consider the case:
1701
1702                              class Foo<T> { class Bar {} }
1703                              class Test : Foo<Test>.Bar {}
1704
1705                            When Foo<Test> is being expanded, 'Test' isn't yet initialized.  It's actually in
1706                            a really pristine state: it doesn't even know whether 'Test' is a reference or a value type.
1707
1708                            We ensure that the MonoClass is in a state that we can canonicalize to:
1709
1710                              klass->byval_arg.data.klass == klass
1711                              klass->this_arg.data.klass == klass
1712
1713                            If we can't canonicalize 'type', it doesn't matter, since later users of 'type' will do it.
1714
1715                            LOCKING: even though we don't explicitly hold a lock, in the problematic case 'ret' is a field
1716                                     of a MonoClass which currently holds the loader lock.  'type' is local.
1717                         */
1718                         if (ret->data.klass == type->data.klass) {
1719                                 return ret;
1720                         }
1721                 }
1722                 /* No need to use locking since nobody is modifying the hash table */
1723                 if ((cached = (MonoType *)g_hash_table_lookup (type_cache, type))) {
1724                         return cached;
1725                 }
1726         }
1727         
1728         /* printf ("%x %x %c %s\n", type->attrs, type->num_mods, type->pinned ? 'p' : ' ', mono_type_full_name (type)); */
1729         
1730         if (type == &stype) { // Type was allocated on the stack, so we need to copy it to safety
1731                 type = transient ? (MonoType *)g_malloc (MONO_SIZEOF_TYPE) : (MonoType *)mono_image_alloc (m, MONO_SIZEOF_TYPE);
1732                 memcpy (type, &stype, MONO_SIZEOF_TYPE);
1733         }
1734         return type;
1735 }
1736
1737
1738 MonoType*
1739 mono_metadata_parse_type_checked (MonoImage *m, MonoGenericContainer *container,
1740                                                            short opt_attrs, gboolean transient, const char *ptr, const char **rptr, MonoError *error)
1741 {
1742         return mono_metadata_parse_type_internal (m, container, opt_attrs, transient, ptr, rptr, error);
1743 }
1744
1745 /*
1746  * LOCKING: Acquires the loader lock.
1747  */
1748 MonoType*
1749 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
1750                           const char *ptr, const char **rptr)
1751 {
1752         MonoError error;
1753         MonoType * type = mono_metadata_parse_type_internal (m, NULL, opt_attrs, FALSE, ptr, rptr, &error);
1754         mono_error_cleanup (&error);
1755         return type;
1756 }
1757
1758 gboolean
1759 mono_metadata_method_has_param_attrs (MonoImage *m, int def)
1760 {
1761         MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1762         MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1763         guint lastp, i, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1764
1765         if (def < methodt->rows)
1766                 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1767         else
1768                 lastp = m->tables [MONO_TABLE_PARAM].rows + 1;
1769
1770         for (i = param_index; i < lastp; ++i) {
1771                 guint32 flags = mono_metadata_decode_row_col (paramt, i - 1, MONO_PARAM_FLAGS);
1772                 if (flags)
1773                         return TRUE;
1774         }
1775
1776         return FALSE;
1777 }
1778
1779 /*
1780  * mono_metadata_get_param_attrs:
1781  *
1782  * @m The image to loader parameter attributes from
1783  * @def method def token (one based)
1784  * @param_count number of params to decode including the return value
1785  *
1786  *   Return the parameter attributes for the method whose MethodDef index is DEF. The 
1787  * returned memory needs to be freed by the caller. If all the param attributes are
1788  * 0, then NULL is returned.
1789  */
1790 int*
1791 mono_metadata_get_param_attrs (MonoImage *m, int def, int param_count)
1792 {
1793         MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
1794         MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
1795         guint32 cols [MONO_PARAM_SIZE];
1796         guint lastp, i, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
1797         int *pattrs = NULL;
1798
1799         if (def < methodt->rows)
1800                 lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
1801         else
1802                 lastp = paramt->rows + 1;
1803
1804         for (i = param_index; i < lastp; ++i) {
1805                 mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
1806                 if (cols [MONO_PARAM_FLAGS]) {
1807                         if (!pattrs)
1808                                 pattrs = g_new0 (int, param_count);
1809                         /* at runtime we just ignore this kind of malformed file:
1810                         * the verifier can signal the error to the user
1811                         */
1812                         if (cols [MONO_PARAM_SEQUENCE] >= param_count)
1813                                 continue;
1814                         pattrs [cols [MONO_PARAM_SEQUENCE]] = cols [MONO_PARAM_FLAGS];
1815                 }
1816         }
1817
1818         return pattrs;
1819 }
1820
1821
1822 /*
1823  * mono_metadata_parse_signature:
1824  * @image: metadata context
1825  * @token: metadata token
1826  *
1827  * Decode a method signature stored in the STANDALONESIG table
1828  *
1829  * Returns: a MonoMethodSignature describing the signature.
1830  */
1831 MonoMethodSignature*
1832 mono_metadata_parse_signature (MonoImage *image, guint32 token)
1833 {
1834         MonoError error;
1835         MonoMethodSignature *ret;
1836         ret = mono_metadata_parse_signature_checked (image, token, &error);
1837         mono_error_cleanup (&error);
1838         return ret;
1839 }
1840
1841 /*
1842  * mono_metadata_parse_signature_checked:
1843  * @image: metadata context
1844  * @token: metadata token
1845  * @error: set on error
1846  *
1847  * Decode a method signature stored in the STANDALONESIG table
1848  *
1849  * Returns: a MonoMethodSignature describing the signature. On failure
1850  * returns NULL and sets @error.
1851  */
1852 MonoMethodSignature*
1853 mono_metadata_parse_signature_checked (MonoImage *image, guint32 token, MonoError *error)
1854 {
1855
1856         mono_error_init (error);
1857         MonoTableInfo *tables = image->tables;
1858         guint32 idx = mono_metadata_token_index (token);
1859         guint32 sig;
1860         const char *ptr;
1861
1862         if (image_is_dynamic (image)) {
1863                 return (MonoMethodSignature *)mono_lookup_dynamic_token (image, token, NULL, error);
1864         }
1865
1866         g_assert (mono_metadata_token_table(token) == MONO_TABLE_STANDALONESIG);
1867                 
1868         sig = mono_metadata_decode_row_col (&tables [MONO_TABLE_STANDALONESIG], idx - 1, 0);
1869
1870         ptr = mono_metadata_blob_heap (image, sig);
1871         mono_metadata_decode_blob_size (ptr, &ptr);
1872
1873         return mono_metadata_parse_method_signature_full (image, NULL, 0, ptr, NULL, error);
1874 }
1875
1876 /*
1877  * mono_metadata_signature_alloc:
1878  * @image: metadata context
1879  * @nparmas: number of parameters in the signature
1880  *
1881  * Allocate a MonoMethodSignature structure with the specified number of params.
1882  * The return type and the params types need to be filled later.
1883  * This is a Mono runtime internal function.
1884  *
1885  * LOCKING: Assumes the loader lock is held.
1886  *
1887  * Returns: the new MonoMethodSignature structure.
1888  */
1889 MonoMethodSignature*
1890 mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
1891 {
1892         MonoMethodSignature *sig;
1893
1894         sig = (MonoMethodSignature *)mono_image_alloc0 (m, MONO_SIZEOF_METHOD_SIGNATURE + ((gint32)nparams) * sizeof (MonoType*));
1895         sig->param_count = nparams;
1896         sig->sentinelpos = -1;
1897
1898         return sig;
1899 }
1900
1901 static MonoMethodSignature*
1902 mono_metadata_signature_dup_internal_with_padding (MonoImage *image, MonoMemPool *mp, MonoMethodSignature *sig, size_t padding)
1903 {
1904         int sigsize, sig_header_size;
1905         MonoMethodSignature *ret;
1906         sigsize = sig_header_size = MONO_SIZEOF_METHOD_SIGNATURE + sig->param_count * sizeof (MonoType *) + padding;
1907         if (sig->ret)
1908                 sigsize += MONO_SIZEOF_TYPE;
1909
1910         if (image) {
1911                 ret = (MonoMethodSignature *)mono_image_alloc (image, sigsize);
1912         } else if (mp) {
1913                 ret = (MonoMethodSignature *)mono_mempool_alloc (mp, sigsize);
1914         } else {
1915                 ret = (MonoMethodSignature *)g_malloc (sigsize);
1916         }
1917
1918         memcpy (ret, sig, sig_header_size - padding);
1919
1920         // Copy return value because of ownership semantics.
1921         if (sig->ret) {
1922                 // Danger! Do not alter padding use without changing the dup_add_this below
1923                 intptr_t end_of_header = (intptr_t)( (char*)(ret) + sig_header_size);
1924                 ret->ret = (MonoType *)end_of_header;
1925                 memcpy (ret->ret, sig->ret, MONO_SIZEOF_TYPE);
1926         }
1927
1928         return ret;
1929 }
1930
1931 static MonoMethodSignature*
1932 mono_metadata_signature_dup_internal (MonoImage *image, MonoMemPool *mp, MonoMethodSignature *sig)
1933 {
1934         return mono_metadata_signature_dup_internal_with_padding (image, mp, sig, 0);
1935 }
1936 /*
1937  * signature_dup_add_this:
1938  *
1939  *  Make a copy of @sig, adding an explicit this argument.
1940  */
1941 MonoMethodSignature*
1942 mono_metadata_signature_dup_add_this (MonoImage *image, MonoMethodSignature *sig, MonoClass *klass)
1943 {
1944         MonoMethodSignature *ret;
1945         ret = mono_metadata_signature_dup_internal_with_padding (image, NULL, sig, sizeof (MonoType *));
1946
1947         ret->param_count = sig->param_count + 1;
1948         ret->hasthis = FALSE;
1949
1950         for (int i = sig->param_count - 1; i >= 0; i --)
1951                 ret->params [i + 1] = sig->params [i];
1952         ret->params [0] = klass->valuetype ? &klass->this_arg : &klass->byval_arg;
1953
1954         for (int i = sig->param_count - 1; i >= 0; i --)
1955                 g_assert(ret->params [i + 1]->type == sig->params [i]->type && ret->params [i+1]->type != MONO_TYPE_END);
1956         g_assert (ret->ret->type == sig->ret->type && ret->ret->type != MONO_TYPE_END);
1957
1958         return ret;
1959 }
1960
1961
1962
1963 MonoMethodSignature*
1964 mono_metadata_signature_dup_full (MonoImage *image, MonoMethodSignature *sig)
1965 {
1966         MonoMethodSignature *ret = mono_metadata_signature_dup_internal (image, NULL, sig);
1967
1968         for (int i = 0 ; i < sig->param_count; i ++)
1969                 g_assert(ret->params [i]->type == sig->params [i]->type);
1970         g_assert (ret->ret->type == sig->ret->type);
1971
1972         return ret;
1973 }
1974
1975 /*The mempool is accessed without synchronization*/
1976 MonoMethodSignature*
1977 mono_metadata_signature_dup_mempool (MonoMemPool *mp, MonoMethodSignature *sig)
1978 {
1979         return mono_metadata_signature_dup_internal (NULL, mp, sig);
1980 }
1981
1982 /*
1983  * mono_metadata_signature_dup:
1984  * @sig: method signature
1985  *
1986  * Duplicate an existing MonoMethodSignature so it can be modified.
1987  * This is a Mono runtime internal function.
1988  *
1989  * Returns: the new MonoMethodSignature structure.
1990  */
1991 MonoMethodSignature*
1992 mono_metadata_signature_dup (MonoMethodSignature *sig)
1993 {
1994         return mono_metadata_signature_dup_full (NULL, sig);
1995 }
1996
1997 /*
1998  * mono_metadata_signature_size:
1999  *
2000  *   Return the amount of memory allocated to SIG.
2001  */
2002 guint32
2003 mono_metadata_signature_size (MonoMethodSignature *sig)
2004 {
2005         return MONO_SIZEOF_METHOD_SIGNATURE + sig->param_count * sizeof (MonoType *);
2006 }
2007
2008 /*
2009  * mono_metadata_parse_method_signature:
2010  * @m: metadata context
2011  * @generic_container: generics container
2012  * @def: the MethodDef index or 0 for Ref signatures.
2013  * @ptr: pointer to the signature metadata representation
2014  * @rptr: pointer updated to match the end of the decoded stream
2015  *
2016  * Decode a method signature stored at @ptr.
2017  * This is a Mono runtime internal function.
2018  *
2019  * LOCKING: Assumes the loader lock is held.
2020  *
2021  * Returns: a MonoMethodSignature describing the signature.
2022  */
2023 MonoMethodSignature *
2024 mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContainer *container,
2025                                            int def, const char *ptr, const char **rptr, MonoError *error)
2026 {
2027         MonoMethodSignature *method;
2028         int i, *pattrs = NULL;
2029         guint32 hasthis = 0, explicit_this = 0, call_convention, param_count;
2030         guint32 gen_param_count = 0;
2031         gboolean is_open = FALSE;
2032
2033         mono_error_init (error);
2034
2035         if (*ptr & 0x10)
2036                 gen_param_count = 1;
2037         if (*ptr & 0x20)
2038                 hasthis = 1;
2039         if (*ptr & 0x40)
2040                 explicit_this = 1;
2041         call_convention = *ptr & 0x0F;
2042         ptr++;
2043         if (gen_param_count)
2044                 gen_param_count = mono_metadata_decode_value (ptr, &ptr);
2045         param_count = mono_metadata_decode_value (ptr, &ptr);
2046
2047         if (def)
2048                 pattrs = mono_metadata_get_param_attrs (m, def, param_count + 1); /*Must be + 1 since signature's param count doesn't account for the return value */
2049
2050         method = mono_metadata_signature_alloc (m, param_count);
2051         method->hasthis = hasthis;
2052         method->explicit_this = explicit_this;
2053         method->call_convention = call_convention;
2054         method->generic_param_count = gen_param_count;
2055
2056         if (call_convention != 0xa) {
2057                 method->ret = mono_metadata_parse_type_checked (m, container, pattrs ? pattrs [0] : 0, FALSE, ptr, &ptr, error);
2058                 if (!method->ret) {
2059                         mono_metadata_free_method_signature (method);
2060                         g_free (pattrs);
2061                         return NULL;
2062                 }
2063                 is_open = mono_class_is_open_constructed_type (method->ret);
2064         }
2065
2066         for (i = 0; i < method->param_count; ++i) {
2067                 if (*ptr == MONO_TYPE_SENTINEL) {
2068                         if (method->call_convention != MONO_CALL_VARARG || def) {
2069                                 mono_error_set_bad_image (error, m, "Found sentinel for methoddef or no vararg");
2070                                 g_free (pattrs);
2071                                 return NULL;
2072                         }
2073                         if (method->sentinelpos >= 0) {
2074                                 mono_error_set_bad_image (error, m, "Found sentinel twice in the same signature.");
2075                                 g_free (pattrs);
2076                                 return NULL;
2077                         }
2078                         method->sentinelpos = i;
2079                         ptr++;
2080                 }
2081                 method->params [i] = mono_metadata_parse_type_checked (m, container, pattrs ? pattrs [i+1] : 0, FALSE, ptr, &ptr, error);
2082                 if (!method->params [i]) {
2083                         mono_metadata_free_method_signature (method);
2084                         g_free (pattrs);
2085                         return NULL;
2086                 }
2087                 if (!is_open)
2088                         is_open = mono_class_is_open_constructed_type (method->params [i]);
2089         }
2090
2091         /* The sentinel could be missing if the caller does not pass any additional arguments */
2092         if (!def && method->call_convention == MONO_CALL_VARARG && method->sentinelpos < 0)
2093                 method->sentinelpos = method->param_count;
2094
2095         method->has_type_parameters = is_open;
2096
2097         if (def && (method->call_convention == MONO_CALL_VARARG))
2098                 method->sentinelpos = method->param_count;
2099
2100         g_free (pattrs);
2101
2102         if (rptr)
2103                 *rptr = ptr;
2104         /*
2105          * Add signature to a cache and increase ref count...
2106          */
2107
2108         return method;
2109 }
2110
2111 /*
2112  * mono_metadata_parse_method_signature:
2113  * @m: metadata context
2114  * @def: the MethodDef index or 0 for Ref signatures.
2115  * @ptr: pointer to the signature metadata representation
2116  * @rptr: pointer updated to match the end of the decoded stream
2117  *
2118  * Decode a method signature stored at @ptr.
2119  * This is a Mono runtime internal function.
2120  *
2121  * LOCKING: Assumes the loader lock is held.
2122  *
2123  * Returns: a MonoMethodSignature describing the signature.
2124  */
2125 MonoMethodSignature *
2126 mono_metadata_parse_method_signature (MonoImage *m, int def, const char *ptr, const char **rptr)
2127 {
2128         /*
2129          * This function MUST NOT be called by runtime code as it does error handling incorrectly.
2130          * Use mono_metadata_parse_method_signature_full instead.
2131          * It's ok to asser on failure as we no longer use it.
2132          */
2133         MonoError error;
2134         MonoMethodSignature *ret;
2135         ret = mono_metadata_parse_method_signature_full (m, NULL, def, ptr, rptr, &error);
2136         g_assert (mono_error_ok (&error));
2137
2138         return ret;
2139 }
2140
2141 /*
2142  * mono_metadata_free_method_signature:
2143  * @sig: signature to destroy
2144  *
2145  * Free the memory allocated in the signature @sig.
2146  * This method needs to be robust and work also on partially-built
2147  * signatures, so it does extra checks.
2148  */
2149 void
2150 mono_metadata_free_method_signature (MonoMethodSignature *sig)
2151 {
2152         /* Everything is allocated from mempools */
2153         /*
2154         int i;
2155         if (sig->ret)
2156                 mono_metadata_free_type (sig->ret);
2157         for (i = 0; i < sig->param_count; ++i) {
2158                 if (sig->params [i])
2159                         mono_metadata_free_type (sig->params [i]);
2160         }
2161         */
2162 }
2163
2164 void
2165 mono_metadata_free_inflated_signature (MonoMethodSignature *sig)
2166 {
2167         int i;
2168
2169         /* Allocated in inflate_generic_signature () */
2170         if (sig->ret)
2171                 mono_metadata_free_type (sig->ret);
2172         for (i = 0; i < sig->param_count; ++i) {
2173                 if (sig->params [i])
2174                         mono_metadata_free_type (sig->params [i]);
2175         }
2176         g_free (sig);
2177 }
2178
2179 static gboolean
2180 inflated_method_equal (gconstpointer a, gconstpointer b)
2181 {
2182         const MonoMethodInflated *ma = (const MonoMethodInflated *)a;
2183         const MonoMethodInflated *mb = (const MonoMethodInflated *)b;
2184         if (ma->declaring != mb->declaring)
2185                 return FALSE;
2186         return mono_metadata_generic_context_equal (&ma->context, &mb->context);
2187 }
2188
2189 static guint
2190 inflated_method_hash (gconstpointer a)
2191 {
2192         const MonoMethodInflated *ma = (const MonoMethodInflated *)a;
2193         return (mono_metadata_generic_context_hash (&ma->context) ^ mono_aligned_addr_hash (ma->declaring));
2194 }
2195
2196 static gboolean
2197 inflated_signature_equal (gconstpointer a, gconstpointer b)
2198 {
2199         const MonoInflatedMethodSignature *sig1 = (const MonoInflatedMethodSignature *)a;
2200         const MonoInflatedMethodSignature *sig2 = (const MonoInflatedMethodSignature *)b;
2201
2202         /* sig->sig is assumed to be canonized */
2203         if (sig1->sig != sig2->sig)
2204                 return FALSE;
2205         /* The generic instances are canonized */
2206         return mono_metadata_generic_context_equal (&sig1->context, &sig2->context);
2207 }
2208
2209 static guint
2210 inflated_signature_hash (gconstpointer a)
2211 {
2212         const MonoInflatedMethodSignature *sig = (const MonoInflatedMethodSignature *)a;
2213
2214         /* sig->sig is assumed to be canonized */
2215         return mono_metadata_generic_context_hash (&sig->context) ^ mono_aligned_addr_hash (sig->sig);
2216 }
2217
2218 /*static void
2219 dump_ginst (MonoGenericInst *ginst)
2220 {
2221         int i;
2222         char *name;
2223
2224         g_print ("Ginst: <");
2225         for (i = 0; i < ginst->type_argc; ++i) {
2226                 if (i != 0)
2227                         g_print (", ");
2228                 name = mono_type_get_name (ginst->type_argv [i]);
2229                 g_print ("%s", name);
2230                 g_free (name);
2231         }
2232         g_print (">");
2233 }*/
2234
2235 static gboolean type_in_image (MonoType *type, MonoImage *image);
2236
2237 static gboolean
2238 signature_in_image (MonoMethodSignature *sig, MonoImage *image)
2239 {
2240         gpointer iter = NULL;
2241         MonoType *p;
2242
2243         while ((p = mono_signature_get_params (sig, &iter)) != NULL)
2244                 if (type_in_image (p, image))
2245                         return TRUE;
2246
2247         return type_in_image (mono_signature_get_return_type (sig), image);
2248 }
2249
2250 static gboolean
2251 ginst_in_image (MonoGenericInst *ginst, MonoImage *image)
2252 {
2253         int i;
2254
2255         for (i = 0; i < ginst->type_argc; ++i) {
2256                 if (type_in_image (ginst->type_argv [i], image))
2257                         return TRUE;
2258         }
2259
2260         return FALSE;
2261 }
2262
2263 static gboolean
2264 gclass_in_image (MonoGenericClass *gclass, MonoImage *image)
2265 {
2266         return gclass->container_class->image == image ||
2267                 ginst_in_image (gclass->context.class_inst, image);
2268 }
2269
2270 static gboolean
2271 type_in_image (MonoType *type, MonoImage *image)
2272 {
2273 retry:
2274         switch (type->type) {
2275         case MONO_TYPE_GENERICINST:
2276                 return gclass_in_image (type->data.generic_class, image);
2277         case MONO_TYPE_PTR:
2278                 type = type->data.type;
2279                 goto retry;
2280         case MONO_TYPE_SZARRAY:
2281                 type = &type->data.klass->byval_arg;
2282                 goto retry;
2283         case MONO_TYPE_ARRAY:
2284                 type = &type->data.array->eklass->byval_arg;
2285                 goto retry;
2286         case MONO_TYPE_FNPTR:
2287                 return signature_in_image (type->data.method, image);
2288         case MONO_TYPE_VAR:
2289         case MONO_TYPE_MVAR:
2290                 return image == get_image_for_generic_param (type->data.generic_param);
2291         default:
2292                 /* At this point, we should've avoided all potential allocations in mono_class_from_mono_type () */
2293                 return image == mono_class_from_mono_type (type)->image;
2294         }
2295 }
2296
2297 static inline void
2298 image_sets_lock (void)
2299 {
2300         mono_os_mutex_lock (&image_sets_mutex);
2301 }
2302
2303 static inline void
2304 image_sets_unlock (void)
2305 {
2306         mono_os_mutex_unlock (&image_sets_mutex);
2307 }
2308
2309 /*
2310  * get_image_set:
2311  *
2312  *   Return a MonoImageSet representing the set of images in IMAGES.
2313  */
2314 static MonoImageSet*
2315 get_image_set (MonoImage **images, int nimages)
2316 {
2317         int i, j, k;
2318         MonoImageSet *set;
2319         GSList *l;
2320
2321         /* Common case: Image set contains corlib only. If we've seen that case before, we cached the set. */
2322         if (nimages == 1 && images [0] == mono_defaults.corlib && mscorlib_image_set)
2323                 return mscorlib_image_set;
2324
2325         /* Happens with empty generic instances */
2326         // FIXME: Is corlib the correct thing to return here? If so, why? This may be an artifact of generic instances previously defaulting to allocating from corlib.
2327         if (nimages == 0)
2328                 return mscorlib_image_set;
2329
2330         image_sets_lock ();
2331
2332         if (!image_sets)
2333                 image_sets = g_ptr_array_new ();
2334
2335         // Before we go on, we should check to see whether a MonoImageSet with these images already exists.
2336         // We can search the referred-by imagesets of any one of our images to do this. Arbitrarily pick one here:
2337         if (images [0] == mono_defaults.corlib && nimages > 1)
2338                 l = images [1]->image_sets; // Prefer not to search the imagesets of corlib-- that will be a long list.
2339         else
2340                 l = images [0]->image_sets;
2341
2342         set = NULL;
2343         while (l) // Iterate over selected list, looking for an imageset with members equal to our target one
2344         {
2345                 set = (MonoImageSet *)l->data;
2346
2347                 if (set->nimages == nimages) { // Member count differs, this can't be it
2348                         // Compare all members to all members-- order might be different
2349                         for (j = 0; j < nimages; ++j) {
2350                                 for (k = 0; k < nimages; ++k)
2351                                         if (set->images [k] == images [j])
2352                                                 break; // Break on match
2353
2354                                 // If we iterated all the way through set->images, images[j] was *not* found.
2355                                 if (k == nimages)
2356                                         break; // Break on "image not found"
2357                         }
2358
2359                         // If we iterated all the way through images without breaking, all items in images were found in set->images
2360                         if (j == nimages)
2361                                 break; // Break on "found a set with equal members"
2362                 }
2363
2364                 l = l->next;
2365         }
2366
2367         // If we iterated all the way through l without breaking, the imageset does not already exist and we shuold create it
2368         if (!l) {
2369                 set = g_new0 (MonoImageSet, 1);
2370                 set->nimages = nimages;
2371                 set->images = g_new0 (MonoImage*, nimages);
2372                 mono_os_mutex_init_recursive (&set->lock);
2373                 for (i = 0; i < nimages; ++i)
2374                         set->images [i] = images [i];
2375                 set->gclass_cache = g_hash_table_new_full (mono_generic_class_hash, mono_generic_class_equal, NULL, (GDestroyNotify)free_generic_class);
2376                 set->ginst_cache = g_hash_table_new_full (mono_metadata_generic_inst_hash, mono_metadata_generic_inst_equal, NULL, (GDestroyNotify)free_generic_inst);
2377                 set->gmethod_cache = g_hash_table_new_full (inflated_method_hash, inflated_method_equal, NULL, (GDestroyNotify)free_inflated_method);
2378                 set->gsignature_cache = g_hash_table_new_full (inflated_signature_hash, inflated_signature_equal, NULL, (GDestroyNotify)free_inflated_signature);
2379
2380                 for (i = 0; i < nimages; ++i)
2381                         set->images [i]->image_sets = g_slist_prepend (set->images [i]->image_sets, set);
2382
2383                 g_ptr_array_add (image_sets, set);
2384         }
2385
2386         if (nimages == 1 && images [0] == mono_defaults.corlib) {
2387                 mono_memory_barrier ();
2388                 mscorlib_image_set = set;
2389         }
2390
2391         image_sets_unlock ();
2392
2393         return set;
2394 }
2395
2396 static void
2397 delete_image_set (MonoImageSet *set)
2398 {
2399         int i;
2400
2401         g_hash_table_destroy (set->gclass_cache);
2402         g_hash_table_destroy (set->ginst_cache);
2403         g_hash_table_destroy (set->gmethod_cache);
2404         g_hash_table_destroy (set->gsignature_cache);
2405
2406         mono_wrapper_caches_free (&set->wrapper_caches);
2407
2408         image_sets_lock ();
2409
2410         for (i = 0; i < set->nimages; ++i)
2411                 set->images [i]->image_sets = g_slist_remove (set->images [i]->image_sets, set);
2412
2413         g_ptr_array_remove (image_sets, set);
2414
2415         image_sets_unlock ();
2416
2417         if (set->mempool)
2418                 mono_mempool_destroy (set->mempool);
2419         g_free (set->images);
2420         mono_os_mutex_destroy (&set->lock);
2421         g_free (set);
2422 }
2423
2424 void
2425 mono_image_set_lock (MonoImageSet *set)
2426 {
2427         mono_os_mutex_lock (&set->lock);
2428 }
2429
2430 void
2431 mono_image_set_unlock (MonoImageSet *set)
2432 {
2433         mono_os_mutex_unlock (&set->lock);
2434 }
2435
2436 gpointer
2437 mono_image_set_alloc (MonoImageSet *set, guint size)
2438 {
2439         gpointer res;
2440
2441         mono_image_set_lock (set);
2442         if (!set->mempool)
2443                 set->mempool = mono_mempool_new_size (INITIAL_IMAGE_SET_SIZE);
2444         res = mono_mempool_alloc (set->mempool, size);
2445         mono_image_set_unlock (set);
2446
2447         return res;
2448 }
2449
2450 gpointer
2451 mono_image_set_alloc0 (MonoImageSet *set, guint size)
2452 {
2453         gpointer res;
2454
2455         mono_image_set_lock (set);
2456         if (!set->mempool)
2457                 set->mempool = mono_mempool_new_size (INITIAL_IMAGE_SET_SIZE);
2458         res = mono_mempool_alloc0 (set->mempool, size);
2459         mono_image_set_unlock (set);
2460
2461         return res;
2462 }
2463
2464 char*
2465 mono_image_set_strdup (MonoImageSet *set, const char *s)
2466 {
2467         char *res;
2468
2469         mono_image_set_lock (set);
2470         if (!set->mempool)
2471                 set->mempool = mono_mempool_new_size (INITIAL_IMAGE_SET_SIZE);
2472         res = mono_mempool_strdup (set->mempool, s);
2473         mono_image_set_unlock (set);
2474
2475         return res;
2476 }
2477
2478 // Get a descriptive string for a MonoImageSet
2479 // Callers are obligated to free buffer with g_free after use
2480 char *
2481 mono_image_set_description (MonoImageSet *set)
2482 {
2483         GString *result = g_string_new (NULL);
2484         int img;
2485         g_string_append (result, "[");
2486         for (img = 0; img < set->nimages; img++)
2487         {
2488                 if (img > 0)
2489                         g_string_append (result, ", ");
2490                 g_string_append (result, set->images[img]->name);
2491         }
2492         g_string_append (result, "]");
2493         return g_string_free (result, FALSE);
2494 }
2495
2496 /* 
2497  * Structure used by the collect_..._images functions to store the image list.
2498  */
2499 typedef struct {
2500         MonoImage *image_buf [64];
2501         MonoImage **images;
2502         int nimages, images_len;
2503 } CollectData;
2504
2505 static void
2506 collect_data_init (CollectData *data)
2507 {
2508         data->images = data->image_buf;
2509         data->images_len = 64;
2510         data->nimages = 0;
2511 }
2512
2513 static void
2514 collect_data_free (CollectData *data)
2515 {
2516         if (data->images != data->image_buf)
2517                 g_free (data->images);
2518 }
2519
2520 static void
2521 enlarge_data (CollectData *data)
2522 {
2523         int new_len = data->images_len < 16 ? 16 : data->images_len * 2;
2524         MonoImage **d = g_new (MonoImage *, new_len);
2525
2526         // FIXME: test this
2527         g_assert_not_reached ();
2528         memcpy (d, data->images, data->images_len);
2529         if (data->images != data->image_buf)
2530                 g_free (data->images);
2531         data->images = d;
2532         data->images_len = new_len;
2533 }
2534
2535 static inline void
2536 add_image (MonoImage *image, CollectData *data)
2537 {
2538         int i;
2539
2540         /* The arrays are small, so use a linear search instead of a hash table */
2541         for (i = 0; i < data->nimages; ++i)
2542                 if (data->images [i] == image)
2543                         return;
2544
2545         if (data->nimages == data->images_len)
2546                 enlarge_data (data);
2547
2548         data->images [data->nimages ++] = image;
2549 }
2550
2551 static void
2552 collect_type_images (MonoType *type, CollectData *data);
2553
2554 static void
2555 collect_ginst_images (MonoGenericInst *ginst, CollectData *data)
2556 {
2557         int i;
2558
2559         for (i = 0; i < ginst->type_argc; ++i) {
2560                 collect_type_images (ginst->type_argv [i], data);
2561         }
2562 }
2563
2564 static void
2565 collect_gclass_images (MonoGenericClass *gclass, CollectData *data)
2566 {
2567         add_image (gclass->container_class->image, data);
2568         if (gclass->context.class_inst)
2569                 collect_ginst_images (gclass->context.class_inst, data);
2570 }
2571
2572 static void
2573 collect_signature_images (MonoMethodSignature *sig, CollectData *data)
2574 {
2575         gpointer iter = NULL;
2576         MonoType *p;
2577
2578         collect_type_images (mono_signature_get_return_type (sig), data);
2579         while ((p = mono_signature_get_params (sig, &iter)) != NULL)
2580                 collect_type_images (p, data);
2581 }
2582
2583 static void
2584 collect_inflated_signature_images (MonoInflatedMethodSignature *sig, CollectData *data)
2585 {
2586         collect_signature_images (sig->sig, data);
2587         if (sig->context.class_inst)
2588                 collect_ginst_images (sig->context.class_inst, data);
2589         if (sig->context.method_inst)
2590                 collect_ginst_images (sig->context.method_inst, data);
2591 }
2592
2593 static void
2594 collect_method_images (MonoMethodInflated *method, CollectData *data)
2595 {
2596         MonoMethod *m = method->declaring;
2597
2598         add_image (method->declaring->klass->image, data);
2599         if (method->context.class_inst)
2600                 collect_ginst_images (method->context.class_inst, data);
2601         if (method->context.method_inst)
2602                 collect_ginst_images (method->context.method_inst, data);
2603         /*
2604          * Dynamic assemblies have no references, so the images they depend on can be unloaded before them.
2605          */
2606         if (image_is_dynamic (m->klass->image))
2607                 collect_signature_images (mono_method_signature (m), data);
2608 }
2609
2610 static void
2611 collect_type_images (MonoType *type, CollectData *data)
2612 {
2613 retry:
2614         switch (type->type) {
2615         case MONO_TYPE_GENERICINST:
2616                 collect_gclass_images (type->data.generic_class, data);
2617                 break;
2618         case MONO_TYPE_PTR:
2619                 type = type->data.type;
2620                 goto retry;
2621         case MONO_TYPE_SZARRAY:
2622                 type = &type->data.klass->byval_arg;
2623                 goto retry;
2624         case MONO_TYPE_ARRAY:
2625                 type = &type->data.array->eklass->byval_arg;
2626                 goto retry;
2627         case MONO_TYPE_FNPTR:
2628                 //return signature_in_image (type->data.method, image);
2629                 g_assert_not_reached ();
2630         case MONO_TYPE_VAR:
2631         case MONO_TYPE_MVAR:
2632         {
2633                 MonoImage *image = get_image_for_generic_param (type->data.generic_param);
2634                 add_image (image, data);
2635                 break;
2636         }
2637         case MONO_TYPE_CLASS:
2638         case MONO_TYPE_VALUETYPE:
2639                 add_image (mono_class_from_mono_type (type)->image, data);
2640                 break;
2641         default:
2642                 add_image (mono_defaults.corlib, data);
2643         }
2644 }
2645
2646 typedef struct {
2647         MonoImage *image;
2648         GSList *list;
2649 } CleanForImageUserData;
2650
2651 static gboolean
2652 steal_gclass_in_image (gpointer key, gpointer value, gpointer data)
2653 {
2654         MonoGenericClass *gclass = (MonoGenericClass *)key;
2655         CleanForImageUserData *user_data = (CleanForImageUserData *)data;
2656
2657         g_assert (gclass_in_image (gclass, user_data->image));
2658
2659         user_data->list = g_slist_prepend (user_data->list, gclass);
2660         return TRUE;
2661 }
2662
2663 static gboolean
2664 steal_ginst_in_image (gpointer key, gpointer value, gpointer data)
2665 {
2666         MonoGenericInst *ginst = (MonoGenericInst *)key;
2667         CleanForImageUserData *user_data = (CleanForImageUserData *)data;
2668
2669         // This doesn't work during corlib compilation
2670         //g_assert (ginst_in_image (ginst, user_data->image));
2671
2672         user_data->list = g_slist_prepend (user_data->list, ginst);
2673         return TRUE;
2674 }
2675
2676 static gboolean
2677 inflated_method_in_image (gpointer key, gpointer value, gpointer data)
2678 {
2679         MonoImage *image = (MonoImage *)data;
2680         MonoMethodInflated *method = (MonoMethodInflated *)key;
2681
2682         // FIXME:
2683         // https://bugzilla.novell.com/show_bug.cgi?id=458168
2684         g_assert (method->declaring->klass->image == image ||
2685                 (method->context.class_inst && ginst_in_image (method->context.class_inst, image)) ||
2686                           (method->context.method_inst && ginst_in_image (method->context.method_inst, image)) || (((MonoMethod*)method)->signature && signature_in_image (mono_method_signature ((MonoMethod*)method), image)));
2687
2688         return TRUE;
2689 }
2690
2691 static gboolean
2692 inflated_signature_in_image (gpointer key, gpointer value, gpointer data)
2693 {
2694         MonoImage *image = (MonoImage *)data;
2695         MonoInflatedMethodSignature *sig = (MonoInflatedMethodSignature *)key;
2696
2697         return signature_in_image (sig->sig, image) ||
2698                 (sig->context.class_inst && ginst_in_image (sig->context.class_inst, image)) ||
2699                 (sig->context.method_inst && ginst_in_image (sig->context.method_inst, image));
2700 }       
2701
2702 static void
2703 check_gmethod (gpointer key, gpointer value, gpointer data)
2704 {
2705         MonoMethodInflated *method = (MonoMethodInflated *)key;
2706         MonoImage *image = (MonoImage *)data;
2707
2708         if (method->context.class_inst)
2709                 g_assert (!ginst_in_image (method->context.class_inst, image));
2710         if (method->context.method_inst)
2711                 g_assert (!ginst_in_image (method->context.method_inst, image));
2712         if (((MonoMethod*)method)->signature)
2713                 g_assert (!signature_in_image (mono_method_signature ((MonoMethod*)method), image));
2714 }
2715
2716 /*
2717  * check_image_sets:
2718  *
2719  *   Run a consistency check on the image set data structures.
2720  */
2721 static G_GNUC_UNUSED void
2722 check_image_sets (MonoImage *image)
2723 {
2724         int i;
2725         GSList *l = image->image_sets;
2726
2727         if (!image_sets)
2728                 return;
2729
2730         for (i = 0; i < image_sets->len; ++i) {
2731                 MonoImageSet *set = (MonoImageSet *)g_ptr_array_index (image_sets, i);
2732
2733                 if (!g_slist_find (l, set)) {
2734                         g_hash_table_foreach (set->gmethod_cache, check_gmethod, image);
2735                 }
2736         }
2737 }
2738
2739 void
2740 mono_metadata_clean_for_image (MonoImage *image)
2741 {
2742         CleanForImageUserData ginst_data, gclass_data;
2743         GSList *l, *set_list;
2744
2745         //check_image_sets (image);
2746
2747         /*
2748          * The data structures could reference each other so we delete them in two phases.
2749          * This is required because of the hashing functions in gclass/ginst_cache.
2750          */
2751         ginst_data.image = gclass_data.image = image;
2752         ginst_data.list = gclass_data.list = NULL;
2753
2754         /* Collect the items to delete */
2755         /* delete_image_set () modifies the lists so make a copy */
2756         for (l = image->image_sets; l; l = l->next) {
2757                 MonoImageSet *set = (MonoImageSet *)l->data;
2758
2759                 mono_image_set_lock (set);
2760                 g_hash_table_foreach_steal (set->gclass_cache, steal_gclass_in_image, &gclass_data);
2761                 g_hash_table_foreach_steal (set->ginst_cache, steal_ginst_in_image, &ginst_data);
2762                 g_hash_table_foreach_remove (set->gmethod_cache, inflated_method_in_image, image);
2763                 g_hash_table_foreach_remove (set->gsignature_cache, inflated_signature_in_image, image);
2764                 mono_image_set_unlock (set);
2765         }
2766
2767         /* Delete the removed items */
2768         for (l = ginst_data.list; l; l = l->next)
2769                 free_generic_inst ((MonoGenericInst *)l->data);
2770         for (l = gclass_data.list; l; l = l->next)
2771                 free_generic_class ((MonoGenericClass *)l->data);
2772         g_slist_free (ginst_data.list);
2773         g_slist_free (gclass_data.list);
2774         /* delete_image_set () modifies the lists so make a copy */
2775         set_list = g_slist_copy (image->image_sets);
2776         for (l = set_list; l; l = l->next) {
2777                 MonoImageSet *set = (MonoImageSet *)l->data;
2778
2779                 delete_image_set (set);
2780         }
2781         g_slist_free (set_list);
2782 }
2783
2784 static void
2785 free_inflated_method (MonoMethodInflated *imethod)
2786 {
2787         MonoMethod *method = (MonoMethod*)imethod;
2788
2789         if (method->signature)
2790                 mono_metadata_free_inflated_signature (method->signature);
2791
2792         g_free (method);
2793 }
2794
2795 static void
2796 free_generic_inst (MonoGenericInst *ginst)
2797 {
2798         int i;
2799
2800         /* The ginst itself is allocated from the image set mempool */
2801         for (i = 0; i < ginst->type_argc; ++i)
2802                 mono_metadata_free_type (ginst->type_argv [i]);
2803 }
2804
2805 static void
2806 free_generic_class (MonoGenericClass *gclass)
2807 {
2808         /* The gclass itself is allocated from the image set mempool */
2809         if (gclass->is_dynamic)
2810                 mono_reflection_free_dynamic_generic_class (gclass);
2811         if (gclass->cached_class && gclass->cached_class->interface_id)
2812                 mono_unload_interface_id (gclass->cached_class);
2813 }
2814
2815 static void
2816 free_inflated_signature (MonoInflatedMethodSignature *sig)
2817 {
2818         mono_metadata_free_inflated_signature (sig->sig);
2819         g_free (sig);
2820 }
2821
2822 /*
2823  * mono_metadata_get_inflated_signature:
2824  *
2825  *   Given an inflated signature and a generic context, return a canonical copy of the 
2826  * signature. The returned signature might be equal to SIG or it might be a cached copy.
2827  */
2828 MonoMethodSignature *
2829 mono_metadata_get_inflated_signature (MonoMethodSignature *sig, MonoGenericContext *context)
2830 {
2831         MonoInflatedMethodSignature helper;
2832         MonoInflatedMethodSignature *res;
2833         CollectData data;
2834         MonoImageSet *set;
2835
2836         helper.sig = sig;
2837         helper.context.class_inst = context->class_inst;
2838         helper.context.method_inst = context->method_inst;
2839
2840         collect_data_init (&data);
2841
2842         collect_inflated_signature_images (&helper, &data);
2843
2844         set = get_image_set (data.images, data.nimages);
2845
2846         collect_data_free (&data);
2847
2848         mono_image_set_lock (set);
2849
2850         res = (MonoInflatedMethodSignature *)g_hash_table_lookup (set->gsignature_cache, &helper);
2851         if (!res) {
2852                 res = g_new0 (MonoInflatedMethodSignature, 1);
2853                 res->sig = sig;
2854                 res->context.class_inst = context->class_inst;
2855                 res->context.method_inst = context->method_inst;
2856                 g_hash_table_insert (set->gsignature_cache, res, res);
2857         }
2858
2859         mono_image_set_unlock (set);
2860
2861         return res->sig;
2862 }
2863
2864 MonoImageSet *
2865 mono_metadata_get_image_set_for_method (MonoMethodInflated *method)
2866 {
2867         MonoImageSet *set;
2868         CollectData image_set_data;
2869
2870         collect_data_init (&image_set_data);
2871         collect_method_images (method, &image_set_data);
2872         set = get_image_set (image_set_data.images, image_set_data.nimages);
2873         collect_data_free (&image_set_data);
2874
2875         return set;
2876 }
2877
2878 /*
2879  * mono_metadata_get_generic_inst:
2880  *
2881  * Given a list of types, return a MonoGenericInst that represents that list.
2882  * The returned MonoGenericInst has its own copy of the list of types.  The list
2883  * passed in the argument can be freed, modified or disposed of.
2884  *
2885  */
2886 MonoGenericInst *
2887 mono_metadata_get_generic_inst (int type_argc, MonoType **type_argv)
2888 {
2889         MonoGenericInst *ginst;
2890         gboolean is_open;
2891         int i;
2892         int size = MONO_SIZEOF_GENERIC_INST + type_argc * sizeof (MonoType *);
2893         CollectData data;
2894         MonoImageSet *set;
2895
2896         for (i = 0; i < type_argc; ++i)
2897                 if (mono_class_is_open_constructed_type (type_argv [i]))
2898                         break;
2899         is_open = (i < type_argc);
2900
2901         ginst = (MonoGenericInst *)g_alloca (size);
2902         memset (ginst, 0, sizeof (MonoGenericInst));
2903         ginst->is_open = is_open;
2904         ginst->type_argc = type_argc;
2905         memcpy (ginst->type_argv, type_argv, type_argc * sizeof (MonoType *));
2906
2907         collect_data_init (&data);
2908
2909         collect_ginst_images (ginst, &data);
2910
2911         set = get_image_set (data.images, data.nimages);
2912
2913         collect_data_free (&data);
2914
2915         mono_image_set_lock (set);
2916
2917         ginst = (MonoGenericInst *)g_hash_table_lookup (set->ginst_cache, ginst);
2918         if (!ginst) {
2919                 ginst = (MonoGenericInst *)mono_image_set_alloc0 (set, size);
2920 #ifndef MONO_SMALL_CONFIG
2921                 ginst->id = ++next_generic_inst_id;
2922 #endif
2923                 ginst->is_open = is_open;
2924                 ginst->type_argc = type_argc;
2925
2926                 for (i = 0; i < type_argc; ++i)
2927                         ginst->type_argv [i] = mono_metadata_type_dup (NULL, type_argv [i]);
2928
2929                 g_hash_table_insert (set->ginst_cache, ginst, ginst);
2930         }
2931
2932         mono_image_set_unlock (set);
2933         return ginst;
2934 }
2935
2936 static gboolean
2937 mono_metadata_is_type_builder_generic_type_definition (MonoClass *container_class, MonoGenericInst *inst, gboolean is_dynamic)
2938 {
2939         MonoGenericContainer *container = container_class->generic_container; 
2940
2941         if (!is_dynamic || container_class->wastypebuilder || container->type_argc != inst->type_argc)
2942                 return FALSE;
2943         return inst == container->context.class_inst;
2944 }
2945
2946 /*
2947  * mono_metadata_lookup_generic_class:
2948  *
2949  * Returns a MonoGenericClass with the given properties.
2950  *
2951  */
2952 MonoGenericClass *
2953 mono_metadata_lookup_generic_class (MonoClass *container_class, MonoGenericInst *inst, gboolean is_dynamic)
2954 {
2955         MonoGenericClass *gclass;
2956         MonoGenericClass helper;
2957         gboolean is_tb_open = mono_metadata_is_type_builder_generic_type_definition (container_class, inst, is_dynamic);
2958         MonoImageSet *set;
2959         CollectData data;
2960
2961         memset (&helper, 0, sizeof(helper)); // act like g_new0
2962         helper.container_class = container_class;
2963         helper.context.class_inst = inst;
2964         helper.is_dynamic = is_dynamic; /* We use this in a hash lookup, which does not attempt to downcast the pointer */
2965         helper.is_tb_open = is_tb_open;
2966
2967         collect_data_init (&data);
2968
2969         collect_gclass_images (&helper, &data);
2970
2971         set = get_image_set (data.images, data.nimages);
2972
2973         collect_data_free (&data);
2974
2975         mono_image_set_lock (set);
2976
2977         gclass = (MonoGenericClass *)g_hash_table_lookup (set->gclass_cache, &helper);
2978
2979         /* A tripwire just to keep us honest */
2980         g_assert (!helper.cached_class);
2981
2982         if (gclass) {
2983                 mono_image_set_unlock (set);
2984                 return gclass;
2985         }
2986
2987         if (is_dynamic) {
2988                 MonoDynamicGenericClass *dgclass = mono_image_set_new0 (set, MonoDynamicGenericClass, 1);
2989                 gclass = &dgclass->generic_class;
2990                 gclass->is_dynamic = 1;
2991         } else {
2992                 gclass = mono_image_set_new0 (set, MonoGenericClass, 1);
2993         }
2994
2995         gclass->is_tb_open = is_tb_open;
2996         gclass->container_class = container_class;
2997         gclass->context.class_inst = inst;
2998         gclass->context.method_inst = NULL;
2999         gclass->owner = set;
3000         if (inst == container_class->generic_container->context.class_inst && !is_tb_open)
3001                 gclass->cached_class = container_class;
3002
3003         g_hash_table_insert (set->gclass_cache, gclass, gclass);
3004
3005         mono_image_set_unlock (set);
3006
3007         return gclass;
3008 }
3009
3010 /*
3011  * mono_metadata_inflate_generic_inst:
3012  *
3013  * Instantiate the generic instance @ginst with the context @context.
3014  * Check @error for success.
3015  *
3016  */
3017 MonoGenericInst *
3018 mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *context, MonoError *error)
3019 {
3020         MonoType **type_argv;
3021         MonoGenericInst *nginst = NULL;
3022         int i, count = 0;
3023
3024         mono_error_init (error);
3025
3026         if (!ginst->is_open)
3027                 return ginst;
3028
3029         type_argv = g_new0 (MonoType*, ginst->type_argc);
3030
3031         for (i = 0; i < ginst->type_argc; i++) {
3032                 type_argv [i] = mono_class_inflate_generic_type_checked (ginst->type_argv [i], context, error);
3033                 if (!mono_error_ok (error))
3034                         goto cleanup;
3035                 ++count;
3036         }
3037
3038         nginst = mono_metadata_get_generic_inst (ginst->type_argc, type_argv);
3039
3040 cleanup:
3041         for (i = 0; i < count; i++)
3042                 mono_metadata_free_type (type_argv [i]);
3043         g_free (type_argv);
3044
3045         return nginst;
3046 }
3047
3048 MonoGenericInst *
3049 mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContainer *container,
3050                                   int count, const char *ptr, const char **rptr, MonoError *error)
3051 {
3052         MonoType **type_argv;
3053         MonoGenericInst *ginst;
3054         int i;
3055
3056         mono_error_init (error);
3057         type_argv = g_new0 (MonoType*, count);
3058
3059         for (i = 0; i < count; i++) {
3060                 MonoType *t = mono_metadata_parse_type_checked (m, container, 0, FALSE, ptr, &ptr, error);
3061                 if (!t) {
3062                         g_free (type_argv);
3063                         return NULL;
3064                 }
3065                 type_argv [i] = t;
3066         }
3067
3068         if (rptr)
3069                 *rptr = ptr;
3070
3071         ginst = mono_metadata_get_generic_inst (count, type_argv);
3072
3073         g_free (type_argv);
3074
3075         return ginst;
3076 }
3077
3078 static gboolean
3079 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContainer *container,
3080                                       const char *ptr, const char **rptr, MonoError *error)
3081 {
3082         MonoGenericInst *inst;
3083         MonoClass *gklass;
3084         MonoType *gtype;
3085         int count;
3086
3087         mono_error_init (error);
3088
3089         // XXX how about transient?
3090         gtype = mono_metadata_parse_type_checked (m, NULL, 0, FALSE, ptr, &ptr, error);
3091         if (gtype == NULL)
3092                 return FALSE;
3093
3094         gklass = mono_class_from_mono_type (gtype);
3095         if (!gklass->generic_container) {
3096                 mono_error_set_bad_image (error, m, "Generic instance with non-generic definition");
3097                 return FALSE;
3098         }
3099
3100         count = mono_metadata_decode_value (ptr, &ptr);
3101         inst = mono_metadata_parse_generic_inst (m, container, count, ptr, &ptr, error);
3102         if (inst == NULL)
3103                 return FALSE;
3104
3105         if (rptr)
3106                 *rptr = ptr;
3107
3108         type->data.generic_class = mono_metadata_lookup_generic_class (gklass, inst, FALSE);
3109         return TRUE;
3110 }
3111
3112 /*
3113  * select_container:
3114  * @gc: The generic container to normalize
3115  * @type: The kind of generic parameters the resulting generic-container should contain
3116  */
3117
3118 static MonoGenericContainer *
3119 select_container (MonoGenericContainer *gc, MonoTypeEnum type)
3120 {
3121         gboolean is_var = (type == MONO_TYPE_VAR);
3122         if (!gc)
3123                 return NULL;
3124
3125         g_assert (is_var || type == MONO_TYPE_MVAR);
3126
3127         if (is_var) {
3128                 if (gc->is_method || gc->parent)
3129                         /*
3130                          * The current MonoGenericContainer is a generic method -> its `parent'
3131                          * points to the containing class'es container.
3132                          */
3133                         return gc->parent;
3134         }
3135
3136         return gc;
3137 }
3138
3139 MonoGenericContainer *
3140 get_anonymous_container_for_image (MonoImage *image, gboolean is_mvar)
3141 {
3142         MonoGenericContainer **container_pointer;
3143         if (is_mvar)
3144                 container_pointer = &image->anonymous_generic_method_container;
3145         else
3146                 container_pointer = &image->anonymous_generic_class_container;
3147         MonoGenericContainer *result = *container_pointer;
3148
3149         // This container has never been created; make it now.
3150         if (!result)
3151         {
3152                 // Note this is never deallocated anywhere-- it exists for the lifetime of the image it's allocated from
3153                 result = (MonoGenericContainer *)mono_image_alloc0 (image, sizeof (MonoGenericContainer));
3154                 result->owner.image = image;
3155                 result->is_anonymous = TRUE;
3156                 result->is_small_param = TRUE;
3157                 result->is_method = is_mvar;
3158
3159                 // If another thread already made a container, use that and leak this new one.
3160                 // (Technically it would currently be safe to just assign instead of CASing.)
3161                 MonoGenericContainer *exchange = (MonoGenericContainer *)InterlockedCompareExchangePointer ((volatile gpointer *)container_pointer, result, NULL);
3162                 if (exchange)
3163                         result = exchange;
3164         }
3165         return result;
3166 }
3167
3168 /*
3169  * mono_metadata_parse_generic_param:
3170  * @generic_container: Our MonoClass's or MonoMethod's MonoGenericContainer;
3171  *                     see mono_metadata_parse_type_checked() for details.
3172  * Internal routine to parse a generic type parameter.
3173  * LOCKING: Acquires the loader lock
3174  */
3175 static MonoGenericParam *
3176 mono_metadata_parse_generic_param (MonoImage *m, MonoGenericContainer *generic_container,
3177                                    MonoTypeEnum type, const char *ptr, const char **rptr, MonoError *error)
3178 {
3179         int index = mono_metadata_decode_value (ptr, &ptr);
3180         if (rptr)
3181                 *rptr = ptr;
3182
3183         mono_error_init (error);
3184
3185         generic_container = select_container (generic_container, type);
3186         if (!generic_container) {
3187                 gboolean is_mvar = FALSE;
3188                 switch (type)
3189                 {
3190                         case MONO_TYPE_VAR:
3191                                 break;
3192                         case MONO_TYPE_MVAR:
3193                                 is_mvar = TRUE;
3194                                 break;
3195                         default:
3196                                 g_error ("Cerating generic param object with invalid MonoType"); // This is not a generic param
3197                 }
3198
3199                 /* Create dummy MonoGenericParam */
3200                 MonoGenericParam *param;
3201
3202                 param = (MonoGenericParam *)mono_image_alloc0 (m, sizeof (MonoGenericParam));
3203                 param->num = index;
3204                 param->owner = get_anonymous_container_for_image (m, is_mvar);
3205
3206                 return param;
3207         }
3208
3209         if (index >= generic_container->type_argc) {
3210                 mono_error_set_bad_image (error, m, "Invalid generic %s parameter index %d, max index is %d",
3211                         generic_container->is_method ? "method" : "type",
3212                         index, generic_container->type_argc);
3213                 return NULL;
3214         }
3215
3216         //This can't return NULL
3217         return mono_generic_container_get_param (generic_container, index);
3218 }
3219
3220 /*
3221  * mono_metadata_get_shared_type:
3222  *
3223  *   Return a shared instance of TYPE, if available, NULL otherwise.
3224  * Shared MonoType instances help save memory. Their contents should not be modified
3225  * by the caller. They do not need to be freed as their lifetime is bound by either
3226  * the lifetime of the runtime (builtin types), or the lifetime of the MonoClass
3227  * instance they are embedded in. If they are freed, they should be freed using
3228  * mono_metadata_free_type () instead of g_free ().
3229  */
3230 MonoType*
3231 mono_metadata_get_shared_type (MonoType *type)
3232 {
3233         MonoType *cached;
3234
3235         /* No need to use locking since nobody is modifying the hash table */
3236         if ((cached = (MonoType *)g_hash_table_lookup (type_cache, type)))
3237                 return cached;
3238
3239         switch (type->type){
3240         case MONO_TYPE_CLASS:
3241         case MONO_TYPE_VALUETYPE:
3242                 if (type == &type->data.klass->byval_arg)
3243                         return type;
3244                 if (type == &type->data.klass->this_arg)
3245                         return type;
3246                 break;
3247         default:
3248                 break;
3249         }
3250
3251         return NULL;
3252 }
3253
3254 static gboolean
3255 compare_type_literals (MonoImage *image, int class_type, int type_type, MonoError *error)
3256 {
3257         mono_error_init (error);
3258
3259         /* byval_arg.type can be zero if we're decoding a type that references a class been loading.
3260          * See mcs/test/gtest-440. and #650936.
3261          * FIXME This better be moved to the metadata verifier as it can catch more cases.
3262          */
3263         if (!class_type)
3264                 return TRUE;
3265         /* NET 1.1 assemblies might encode string and object in a denormalized way.
3266          * See #675464.
3267          */
3268         if (class_type == type_type)
3269                 return TRUE;
3270
3271         if (type_type == MONO_TYPE_CLASS) {
3272                 if (class_type == MONO_TYPE_STRING || class_type == MONO_TYPE_OBJECT)
3273                         return TRUE;
3274                 //XXX stringify this argument
3275                 mono_error_set_bad_image (error, image, "Expected reference type but got type kind %d", class_type);
3276                 return FALSE;
3277         }
3278
3279         g_assert (type_type == MONO_TYPE_VALUETYPE);
3280         switch (class_type) {
3281         case MONO_TYPE_BOOLEAN:
3282         case MONO_TYPE_CHAR:
3283         case MONO_TYPE_I1:
3284         case MONO_TYPE_U1:
3285         case MONO_TYPE_I2:
3286         case MONO_TYPE_U2:
3287         case MONO_TYPE_I4:
3288         case MONO_TYPE_U4:
3289         case MONO_TYPE_I8:
3290         case MONO_TYPE_U8:
3291         case MONO_TYPE_R4:
3292         case MONO_TYPE_R8:
3293         case MONO_TYPE_I:
3294         case MONO_TYPE_U:
3295         case MONO_TYPE_CLASS:
3296                 return TRUE;
3297         default:
3298                 //XXX stringify this argument
3299                 mono_error_set_bad_image (error, image, "Expected value type but got type kind %d", class_type);
3300                 return FALSE;
3301         }
3302 }
3303
3304 static gboolean
3305 verify_var_type_and_container (MonoImage *image, int var_type, MonoGenericContainer *container, MonoError *error)
3306 {
3307         mono_error_init (error);
3308         if (var_type == MONO_TYPE_MVAR) {
3309                 if (!container->is_method) { //MVAR and a method container
3310                         mono_error_set_bad_image (error, image, "MVAR parsed in a context without a method container");
3311                         return FALSE;
3312                 }
3313         } else {
3314                 if (!(!container->is_method || //VAR and class container
3315                         (container->is_method && container->parent))) { //VAR and method container with parent
3316                         mono_error_set_bad_image (error, image, "VAR parsed in a context without a class container");
3317                         return FALSE;
3318                 }
3319         }
3320         return TRUE;
3321 }
3322
3323 /* 
3324  * do_mono_metadata_parse_type:
3325  * @type: MonoType to be filled in with the return value
3326  * @m: image context
3327  * @generic_context: generics_context
3328  * @transient: whenever to allocate data from the heap
3329  * @ptr: pointer to the encoded type
3330  * @rptr: pointer where the end of the encoded type is saved
3331  * 
3332  * Internal routine used to "fill" the contents of @type from an 
3333  * allocated pointer.  This is done this way to avoid doing too
3334  * many mini-allocations (particularly for the MonoFieldType which
3335  * most of the time is just a MonoType, but sometimes might be augmented).
3336  *
3337  * This routine is used by mono_metadata_parse_type and
3338  * mono_metadata_parse_field_type
3339  *
3340  * This extracts a Type as specified in Partition II (22.2.12) 
3341  *
3342  * Returns: FALSE if the type could not be loaded
3343  */
3344 static gboolean
3345 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container,
3346                                                          gboolean transient, const char *ptr, const char **rptr, MonoError *error)
3347 {
3348         mono_error_init (error);
3349
3350         type->type = (MonoTypeEnum)mono_metadata_decode_value (ptr, &ptr);
3351         
3352         switch (type->type){
3353         case MONO_TYPE_VOID:
3354         case MONO_TYPE_BOOLEAN:
3355         case MONO_TYPE_CHAR:
3356         case MONO_TYPE_I1:
3357         case MONO_TYPE_U1:
3358         case MONO_TYPE_I2:
3359         case MONO_TYPE_U2:
3360         case MONO_TYPE_I4:
3361         case MONO_TYPE_U4:
3362         case MONO_TYPE_I8:
3363         case MONO_TYPE_U8:
3364         case MONO_TYPE_R4:
3365         case MONO_TYPE_R8:
3366         case MONO_TYPE_I:
3367         case MONO_TYPE_U:
3368         case MONO_TYPE_STRING:
3369         case MONO_TYPE_OBJECT:
3370         case MONO_TYPE_TYPEDBYREF:
3371                 break;
3372         case MONO_TYPE_VALUETYPE:
3373         case MONO_TYPE_CLASS: {
3374                 guint32 token;
3375                 MonoClass *klass;
3376                 token = mono_metadata_parse_typedef_or_ref (m, ptr, &ptr);
3377                 klass = mono_class_get_checked (m, token, error);
3378                 type->data.klass = klass;
3379                 if (!klass)
3380                         return FALSE;
3381
3382                 if (!compare_type_literals (m, klass->byval_arg.type, type->type, error))
3383                         return FALSE;
3384
3385                 break;
3386         }
3387         case MONO_TYPE_SZARRAY: {
3388                 MonoType *etype = mono_metadata_parse_type_checked (m, container, 0, transient, ptr, &ptr, error);
3389                 if (!etype)
3390                         return FALSE;
3391
3392                 type->data.klass = mono_class_from_mono_type (etype);
3393
3394                 if (transient)
3395                         mono_metadata_free_type (etype);
3396
3397                 g_assert (type->data.klass); //This was previously a check for NULL, but mcfmt should never fail. It can return a borken MonoClass, but should return at least something.
3398                 break;
3399         }
3400         case MONO_TYPE_PTR: {
3401                 type->data.type = mono_metadata_parse_type_checked (m, container, 0, transient, ptr, &ptr, error);
3402                 if (!type->data.type)
3403                         return FALSE;
3404                 break;
3405         }
3406         case MONO_TYPE_FNPTR: {
3407                 type->data.method = mono_metadata_parse_method_signature_full (m, container, 0, ptr, &ptr, error);
3408                 if (!type->data.method)
3409                         return FALSE;
3410                 break;
3411         }
3412         case MONO_TYPE_ARRAY: {
3413                 type->data.array = mono_metadata_parse_array_internal (m, container, transient, ptr, &ptr, error);
3414                 if (!type->data.array)
3415                         return FALSE;
3416                 break;
3417         }
3418         case MONO_TYPE_MVAR:
3419         case MONO_TYPE_VAR: {
3420                 if (container && !verify_var_type_and_container (m, type->type, container, error))
3421                         return FALSE;
3422
3423                 type->data.generic_param = mono_metadata_parse_generic_param (m, container, type->type, ptr, &ptr, error);
3424                 if (!type->data.generic_param)
3425                         return FALSE;
3426
3427                 break;
3428         }
3429         case MONO_TYPE_GENERICINST: {
3430                 if (!do_mono_metadata_parse_generic_class (type, m, container, ptr, &ptr, error))
3431                         return FALSE;
3432                 break;
3433         }
3434         default:
3435                 mono_error_set_bad_image (error, m, "type 0x%02x not handled in do_mono_metadata_parse_type on image %s", type->type, m->name);
3436                 return FALSE;
3437         }
3438         
3439         if (rptr)
3440                 *rptr = ptr;
3441         return TRUE;
3442 }
3443
3444 /*
3445  * mono_metadata_free_type:
3446  * @type: type to free
3447  *
3448  * Free the memory allocated for type @type which is allocated on the heap.
3449  */
3450 void
3451 mono_metadata_free_type (MonoType *type)
3452 {
3453         if (type >= builtin_types && type < builtin_types + NBUILTIN_TYPES ())
3454                 return;
3455         
3456         switch (type->type){
3457         case MONO_TYPE_OBJECT:
3458         case MONO_TYPE_STRING:
3459                 if (!type->data.klass)
3460                         break;
3461                 /* fall through */
3462         case MONO_TYPE_CLASS:
3463         case MONO_TYPE_VALUETYPE:
3464                 if (type == &type->data.klass->byval_arg || type == &type->data.klass->this_arg)
3465                         return;
3466                 break;
3467         case MONO_TYPE_PTR:
3468                 mono_metadata_free_type (type->data.type);
3469                 break;
3470         case MONO_TYPE_FNPTR:
3471                 mono_metadata_free_method_signature (type->data.method);
3472                 break;
3473         case MONO_TYPE_ARRAY:
3474                 mono_metadata_free_array (type->data.array);
3475                 break;
3476         default:
3477                 break;
3478         }
3479
3480         g_free (type);
3481 }
3482
3483 #if 0
3484 static void
3485 hex_dump (const char *buffer, int base, int count)
3486 {
3487         int show_header = 1;
3488         int i;
3489
3490         if (count < 0){
3491                 count = -count;
3492                 show_header = 0;
3493         }
3494         
3495         for (i = 0; i < count; i++){
3496                 if (show_header)
3497                         if ((i % 16) == 0)
3498                                 printf ("\n0x%08x: ", (unsigned char) base + i);
3499
3500                 printf ("%02x ", (unsigned char) (buffer [i]));
3501         }
3502         fflush (stdout);
3503 }
3504 #endif
3505
3506 /** 
3507  * @ptr: Points to the beginning of the Section Data (25.3)
3508  */
3509 static MonoExceptionClause*
3510 parse_section_data (MonoImage *m, int *num_clauses, const unsigned char *ptr, MonoError *error)
3511 {
3512         unsigned char sect_data_flags;
3513         int is_fat;
3514         guint32 sect_data_len;
3515         MonoExceptionClause* clauses = NULL;
3516
3517         mono_error_init (error);
3518         
3519         while (1) {
3520                 /* align on 32-bit boundary */
3521                 ptr = dword_align (ptr); 
3522                 sect_data_flags = *ptr;
3523                 ptr++;
3524                 
3525                 is_fat = sect_data_flags & METHOD_HEADER_SECTION_FAT_FORMAT;
3526                 if (is_fat) {
3527                         sect_data_len = (ptr [2] << 16) | (ptr [1] << 8) | ptr [0];
3528                         ptr += 3;
3529                 } else {
3530                         sect_data_len = ptr [0];
3531                         ++ptr;
3532                 }
3533
3534                 if (sect_data_flags & METHOD_HEADER_SECTION_EHTABLE) {
3535                         const unsigned char *p = dword_align (ptr);
3536                         int i;
3537                         *num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
3538                         /* we could just store a pointer if we don't need to byteswap */
3539                         clauses = (MonoExceptionClause *)g_malloc0 (sizeof (MonoExceptionClause) * (*num_clauses));
3540                         for (i = 0; i < *num_clauses; ++i) {
3541                                 MonoExceptionClause *ec = &clauses [i];
3542                                 guint32 tof_value;
3543                                 if (is_fat) {
3544                                         ec->flags = read32 (p);
3545                                         ec->try_offset = read32 (p + 4);
3546                                         ec->try_len = read32 (p + 8);
3547                                         ec->handler_offset = read32 (p + 12);
3548                                         ec->handler_len = read32 (p + 16);
3549                                         tof_value = read32 (p + 20);
3550                                         p += 24;
3551                                 } else {
3552                                         ec->flags = read16 (p);
3553                                         ec->try_offset = read16 (p + 2);
3554                                         ec->try_len = *(p + 4);
3555                                         ec->handler_offset = read16 (p + 5);
3556                                         ec->handler_len = *(p + 7);
3557                                         tof_value = read32 (p + 8);
3558                                         p += 12;
3559                                 }
3560                                 if (ec->flags == MONO_EXCEPTION_CLAUSE_FILTER) {
3561                                         ec->data.filter_offset = tof_value;
3562                                 } else if (ec->flags == MONO_EXCEPTION_CLAUSE_NONE) {
3563                                         ec->data.catch_class = NULL;
3564                                         if (tof_value) {
3565                                                 ec->data.catch_class = mono_class_get_checked (m, tof_value, error);
3566                                                 if (!is_ok (error)) {
3567                                                         g_free (clauses);
3568                                                         return NULL;
3569                                                 }
3570                                         }
3571                                 } else {
3572                                         ec->data.catch_class = NULL;
3573                                 }
3574                                 /* g_print ("try %d: %x %04x-%04x %04x\n", i, ec->flags, ec->try_offset, ec->try_offset+ec->try_len, ec->try_len); */
3575                         }
3576
3577                 }
3578                 if (sect_data_flags & METHOD_HEADER_SECTION_MORE_SECTS)
3579                         ptr += sect_data_len - 4; /* LAMESPEC: it seems the size includes the header */
3580                 else
3581                         return clauses;
3582         }
3583 }
3584
3585 /*
3586  * mono_method_get_header_summary:
3587  * @method: The method to get the header.
3588  * @summary: Where to store the header
3589  *
3590  *
3591  * Returns: TRUE if the header was properly decoded.
3592  */
3593 gboolean
3594 mono_method_get_header_summary (MonoMethod *method, MonoMethodHeaderSummary *summary)
3595 {
3596         int idx;
3597         guint32 rva;
3598         MonoImage* img;
3599         const char *ptr;
3600         unsigned char flags, format;
3601         guint16 fat_flags;
3602
3603         /*Only the GMD has a pointer to the metadata.*/
3604         while (method->is_inflated)
3605                 method = ((MonoMethodInflated*)method)->declaring;
3606
3607         summary->code_size = 0;
3608         summary->has_clauses = FALSE;
3609
3610         /*FIXME extract this into a MACRO and share it with mono_method_get_header*/
3611         if ((method->flags & METHOD_ATTRIBUTE_ABSTRACT) || (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) || (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) || (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
3612                 return FALSE;
3613
3614         if (method->wrapper_type != MONO_WRAPPER_NONE || method->sre_method) {
3615                 MonoMethodHeader *header =  ((MonoMethodWrapper *)method)->header;
3616                 if (!header)
3617                         return FALSE;
3618                 summary->code_size = header->code_size;
3619                 summary->has_clauses = header->num_clauses > 0;
3620                 return TRUE;
3621         }
3622
3623
3624         idx = mono_metadata_token_index (method->token);
3625         img = method->klass->image;
3626         rva = mono_metadata_decode_row_col (&img->tables [MONO_TABLE_METHOD], idx - 1, MONO_METHOD_RVA);
3627
3628         /*We must run the verifier since we'll be decoding it.*/
3629         if (!mono_verifier_verify_method_header (img, rva, NULL))
3630                 return FALSE;
3631
3632         ptr = mono_image_rva_map (img, rva);
3633         if (!ptr)
3634                 return FALSE;
3635
3636         flags = *(const unsigned char *)ptr;
3637         format = flags & METHOD_HEADER_FORMAT_MASK;
3638
3639         switch (format) {
3640         case METHOD_HEADER_TINY_FORMAT:
3641                 ptr++;
3642                 summary->code_size = flags >> 2;
3643                 break;
3644         case METHOD_HEADER_FAT_FORMAT:
3645                 fat_flags = read16 (ptr);
3646                 ptr += 4;
3647                 summary->code_size = read32 (ptr);
3648                 if (fat_flags & METHOD_HEADER_MORE_SECTS)
3649                         summary->has_clauses = TRUE;
3650                 break;
3651         default:
3652                 return FALSE;
3653         }
3654         return TRUE;
3655 }
3656
3657 /*
3658  * mono_metadata_parse_mh_full:
3659  * @m: metadata context
3660  * @generic_context: generics context
3661  * @ptr: pointer to the method header.
3662  *
3663  * Decode the method header at @ptr, including pointer to the IL code,
3664  * info about local variables and optional exception tables.
3665  * This is a Mono runtime internal function.
3666  *
3667  * LOCKING: Acquires the loader lock.
3668  *
3669  * Returns: a transient MonoMethodHeader allocated from the heap.
3670  */
3671 MonoMethodHeader *
3672 mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, const char *ptr, MonoError *error)
3673 {
3674         MonoMethodHeader *mh = NULL;
3675         unsigned char flags = *(const unsigned char *) ptr;
3676         unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
3677         guint16 fat_flags;
3678         guint32 local_var_sig_tok, max_stack, code_size, init_locals;
3679         const unsigned char *code;
3680         MonoExceptionClause* clauses = NULL;
3681         int num_clauses = 0;
3682         MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
3683         guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
3684
3685         mono_error_init (error);
3686
3687         if (!ptr) {
3688                 mono_error_set_bad_image (error, m, "Method header with null pointer");
3689                 return NULL;
3690         }
3691
3692         switch (format) {
3693         case METHOD_HEADER_TINY_FORMAT:
3694                 mh = (MonoMethodHeader *)g_malloc0 (MONO_SIZEOF_METHOD_HEADER);
3695                 ptr++;
3696                 mh->max_stack = 8;
3697                 mh->is_transient = TRUE;
3698                 local_var_sig_tok = 0;
3699                 mh->code_size = flags >> 2;
3700                 mh->code = (unsigned char*)ptr;
3701                 return mh;
3702         case METHOD_HEADER_FAT_FORMAT:
3703                 fat_flags = read16 (ptr);
3704                 ptr += 2;
3705                 max_stack = read16 (ptr);
3706                 ptr += 2;
3707                 code_size = read32 (ptr);
3708                 ptr += 4;
3709                 local_var_sig_tok = read32 (ptr);
3710                 ptr += 4;
3711
3712                 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
3713                         init_locals = 1;
3714                 else
3715                         init_locals = 0;
3716
3717                 code = (unsigned char*)ptr;
3718
3719                 if (!(fat_flags & METHOD_HEADER_MORE_SECTS))
3720                         break;
3721
3722                 /*
3723                  * There are more sections
3724                  */
3725                 ptr = (char*)code + code_size;
3726                 break;
3727         default:
3728                 mono_error_set_bad_image (error, m, "Invalid method header format %d", format);
3729                 return NULL;
3730         }
3731
3732         if (local_var_sig_tok) {
3733                 int idx = (local_var_sig_tok & 0xffffff)-1;
3734                 if (idx >= t->rows || idx < 0) {
3735                         mono_error_set_bad_image (error, m, "Invalid method header local vars signature token 0x%8x", idx);
3736                         goto fail;
3737                 }
3738                 mono_metadata_decode_row (t, idx, cols, 1);
3739
3740                 if (!mono_verifier_verify_standalone_signature (m, cols [MONO_STAND_ALONE_SIGNATURE], NULL)) {
3741                         mono_error_set_bad_image (error, m, "Method header locals signature 0x%8x verification failed", idx);
3742                         goto fail;
3743                 }
3744         }
3745         if (fat_flags & METHOD_HEADER_MORE_SECTS) {
3746                 clauses = parse_section_data (m, &num_clauses, (const unsigned char*)ptr, error);
3747                 if (!is_ok (error))
3748                         goto fail;
3749         }
3750         if (local_var_sig_tok) {
3751                 const char *locals_ptr;
3752                 int len=0, i;
3753
3754                 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
3755                 mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
3756                 if (*locals_ptr != 0x07)
3757                         g_warning ("wrong signature for locals blob");
3758                 locals_ptr++;
3759                 len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
3760                 mh = (MonoMethodHeader *)g_malloc0 (MONO_SIZEOF_METHOD_HEADER + len * sizeof (MonoType*) + num_clauses * sizeof (MonoExceptionClause));
3761                 mh->num_locals = len;
3762                 for (i = 0; i < len; ++i) {
3763                         mh->locals [i] = mono_metadata_parse_type_internal (m, container, 0, TRUE, locals_ptr, &locals_ptr, error);
3764                         if (!is_ok (error))
3765                                 goto fail;
3766                 }
3767         } else {
3768                 mh = (MonoMethodHeader *)g_malloc0 (MONO_SIZEOF_METHOD_HEADER + num_clauses * sizeof (MonoExceptionClause));
3769         }
3770         mh->code = code;
3771         mh->code_size = code_size;
3772         mh->max_stack = max_stack;
3773         mh->is_transient = TRUE;
3774         mh->init_locals = init_locals;
3775         if (clauses) {
3776                 MonoExceptionClause* clausesp = (MonoExceptionClause*)&mh->locals [mh->num_locals];
3777                 memcpy (clausesp, clauses, num_clauses * sizeof (MonoExceptionClause));
3778                 g_free (clauses);
3779                 mh->clauses = clausesp;
3780                 mh->num_clauses = num_clauses;
3781         }
3782         return mh;
3783 fail:
3784         g_free (clauses);
3785         g_free (mh);
3786         return NULL;
3787
3788 }
3789
3790 /*
3791  * mono_metadata_parse_mh:
3792  * @generic_context: generics context
3793  * @ptr: pointer to the method header.
3794  *
3795  * Decode the method header at @ptr, including pointer to the IL code,
3796  * info about local variables and optional exception tables.
3797  *
3798  * Returns: a transient MonoMethodHeader allocated from the heap.
3799  */
3800 MonoMethodHeader *
3801 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
3802 {
3803         MonoError error;
3804         MonoMethodHeader *header = mono_metadata_parse_mh_full (m, NULL, ptr, &error);
3805         mono_error_cleanup (&error);
3806         return header;
3807 }
3808
3809 /*
3810  * mono_metadata_free_mh:
3811  * @mh: a method header
3812  *
3813  * Free the memory allocated for the method header.
3814  */
3815 void
3816 mono_metadata_free_mh (MonoMethodHeader *mh)
3817 {
3818         int i;
3819
3820         /* If it is not transient it means it's part of a wrapper method,
3821          * or a SRE-generated method, so the lifetime in that case is
3822          * dictated by the method's own lifetime
3823          */
3824         if (mh->is_transient) {
3825                 for (i = 0; i < mh->num_locals; ++i)
3826                         mono_metadata_free_type (mh->locals [i]);
3827                 g_free (mh);
3828         }
3829 }
3830
3831 /*
3832  * mono_method_header_get_code:
3833  * @header: a MonoMethodHeader pointer
3834  * @code_size: memory location for returning the code size
3835  * @max_stack: memory location for returning the max stack
3836  *
3837  * Method header accessor to retreive info about the IL code properties:
3838  * a pointer to the IL code itself, the size of the code and the max number
3839  * of stack slots used by the code.
3840  *
3841  * Returns: pointer to the IL code represented by the method header.
3842  */
3843 const unsigned char*
3844 mono_method_header_get_code (MonoMethodHeader *header, guint32* code_size, guint32* max_stack)
3845 {
3846         if (code_size)
3847                 *code_size = header->code_size;
3848         if (max_stack)
3849                 *max_stack = header->max_stack;
3850         return header->code;
3851 }
3852
3853 /*
3854  * mono_method_header_get_locals:
3855  * @header: a MonoMethodHeader pointer
3856  * @num_locals: memory location for returning the number of local variables
3857  * @init_locals: memory location for returning the init_locals flag
3858  *
3859  * Method header accessor to retreive info about the local variables:
3860  * an array of local types, the number of locals and whether the locals
3861  * are supposed to be initialized to 0 on method entry
3862  *
3863  * Returns: pointer to an array of types of the local variables
3864  */
3865 MonoType**
3866 mono_method_header_get_locals (MonoMethodHeader *header, guint32* num_locals, gboolean *init_locals)
3867 {
3868         if (num_locals)
3869                 *num_locals = header->num_locals;
3870         if (init_locals)
3871                 *init_locals = header->init_locals;
3872         return header->locals;
3873 }
3874
3875 /*
3876  * mono_method_header_get_num_clauses:
3877  * @header: a MonoMethodHeader pointer
3878  *
3879  * Method header accessor to retreive the number of exception clauses.
3880  *
3881  * Returns: the number of exception clauses present
3882  */
3883 int
3884 mono_method_header_get_num_clauses (MonoMethodHeader *header)
3885 {
3886         return header->num_clauses;
3887 }
3888
3889 /*
3890  * mono_method_header_get_clauses:
3891  * @header: a MonoMethodHeader pointer
3892  * @method: MonoMethod the header belongs to
3893  * @iter: pointer to a iterator
3894  * @clause: pointer to a MonoExceptionClause structure which will be filled with the info
3895  *
3896  * Get the info about the exception clauses in the method. Set *iter to NULL to
3897  * initiate the iteration, then call the method repeatedly until it returns FALSE.
3898  * At each iteration, the structure pointed to by clause if filled with the
3899  * exception clause information.
3900  *
3901  * Returns: TRUE if clause was filled with info, FALSE if there are no more exception
3902  * clauses.
3903  */
3904 int
3905 mono_method_header_get_clauses (MonoMethodHeader *header, MonoMethod *method, gpointer *iter, MonoExceptionClause *clause)
3906 {
3907         MonoExceptionClause *sc;
3908         /* later we'll be able to use this interface to parse the clause info on demand,
3909          * without allocating anything.
3910          */
3911         if (!iter || !header->num_clauses)
3912                 return FALSE;
3913         if (!*iter) {
3914                 *iter = sc = header->clauses;
3915                 *clause = *sc;
3916                 return TRUE;
3917         }
3918         sc = (MonoExceptionClause *)*iter;
3919         sc++;
3920         if (sc < header->clauses + header->num_clauses) {
3921                 *iter = sc;
3922                 *clause = *sc;
3923                 return TRUE;
3924         }
3925         return FALSE;
3926 }
3927
3928 /**
3929  * mono_metadata_parse_field_type:
3930  * @m: metadata context to extract information from
3931  * @ptr: pointer to the field signature
3932  * @rptr: pointer updated to match the end of the decoded stream
3933  *
3934  * Parses the field signature, and returns the type information for it. 
3935  *
3936  * Returns: The MonoType that was extracted from @ptr.
3937  */
3938 MonoType *
3939 mono_metadata_parse_field_type (MonoImage *m, short field_flags, const char *ptr, const char **rptr)
3940 {
3941         MonoError error;
3942         MonoType * type = mono_metadata_parse_type_internal (m, NULL, field_flags, FALSE, ptr, rptr, &error);
3943         mono_error_cleanup (&error);
3944         return type;
3945 }
3946
3947 /**
3948  * mono_metadata_parse_param:
3949  * @m: metadata context to extract information from
3950  * @ptr: pointer to the param signature
3951  * @rptr: pointer updated to match the end of the decoded stream
3952  *
3953  * Parses the param signature, and returns the type information for it. 
3954  *
3955  * Returns: The MonoType that was extracted from @ptr.
3956  */
3957 MonoType *
3958 mono_metadata_parse_param (MonoImage *m, const char *ptr, const char **rptr)
3959 {
3960         MonoError error;
3961         MonoType * type = mono_metadata_parse_type_internal (m, NULL, 0, FALSE, ptr, rptr, &error);
3962         mono_error_cleanup (&error);
3963         return type;
3964 }
3965
3966 /*
3967  * mono_metadata_token_from_dor:
3968  * @dor_token: A TypeDefOrRef coded index
3969  *
3970  * dor_token is a TypeDefOrRef coded index: it contains either
3971  * a TypeDef, TypeRef or TypeSpec in the lower bits, and the upper
3972  * bits contain an index into the table.
3973  *
3974  * Returns: an expanded token
3975  */
3976 guint32
3977 mono_metadata_token_from_dor (guint32 dor_index)
3978 {
3979         guint32 table, idx;
3980
3981         table = dor_index & 0x03;
3982         idx = dor_index >> 2;
3983
3984         switch (table){
3985         case 0: /* TypeDef */
3986                 return MONO_TOKEN_TYPE_DEF | idx;
3987         case 1: /* TypeRef */
3988                 return MONO_TOKEN_TYPE_REF | idx;
3989         case 2: /* TypeSpec */
3990                 return MONO_TOKEN_TYPE_SPEC | idx;
3991         default:
3992                 g_assert_not_reached ();
3993         }
3994
3995         return 0;
3996 }
3997
3998 /*
3999  * We use this to pass context information to the row locator
4000  */
4001 typedef struct {
4002         int idx;                        /* The index that we are trying to locate */
4003         int col_idx;            /* The index in the row where idx may be stored */
4004         MonoTableInfo *t;       /* pointer to the table */
4005         guint32 result;
4006 } locator_t;
4007
4008 /*
4009  * How the row locator works.
4010  *
4011  *   Table A
4012  *   ___|___
4013  *   ___|___         Table B
4014  *   ___|___------>  _______
4015  *   ___|___         _______
4016  *   
4017  * A column in the rows of table A references an index in table B.
4018  * For example A may be the TYPEDEF table and B the METHODDEF table.
4019  * 
4020  * Given an index in table B we want to get the row in table A
4021  * where the column n references our index in B.
4022  *
4023  * In the locator_t structure:
4024  *      t is table A
4025  *      col_idx is the column number
4026  *      index is the index in table B
4027  *      result will be the index in table A
4028  *
4029  * Examples:
4030  * Table A              Table B         column (in table A)
4031  * TYPEDEF              METHODDEF   MONO_TYPEDEF_METHOD_LIST
4032  * TYPEDEF              FIELD           MONO_TYPEDEF_FIELD_LIST
4033  * PROPERTYMAP  PROPERTY        MONO_PROPERTY_MAP_PROPERTY_LIST
4034  * INTERFIMPL   TYPEDEF         MONO_INTERFACEIMPL_CLASS
4035  * METHODSEM    PROPERTY        ASSOCIATION (encoded index)
4036  *
4037  * Note that we still don't support encoded indexes.
4038  *
4039  */
4040 static int
4041 typedef_locator (const void *a, const void *b)
4042 {
4043         locator_t *loc = (locator_t *) a;
4044         const char *bb = (const char *) b;
4045         int typedef_index = (bb - loc->t->base) / loc->t->row_size;
4046         guint32 col, col_next;
4047
4048         col = mono_metadata_decode_row_col (loc->t, typedef_index, loc->col_idx);
4049
4050         if (loc->idx < col)
4051                 return -1;
4052
4053         /*
4054          * Need to check that the next row is valid.
4055          */
4056         if (typedef_index + 1 < loc->t->rows) {
4057                 col_next = mono_metadata_decode_row_col (loc->t, typedef_index + 1, loc->col_idx);
4058                 if (loc->idx >= col_next)
4059                         return 1;
4060
4061                 if (col == col_next)
4062                         return 1; 
4063         }
4064
4065         loc->result = typedef_index;
4066         
4067         return 0;
4068 }
4069
4070 static int
4071 table_locator (const void *a, const void *b)
4072 {
4073         locator_t *loc = (locator_t *) a;
4074         const char *bb = (const char *) b;
4075         guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
4076         guint32 col;
4077         
4078         col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
4079
4080         if (loc->idx == col) {
4081                 loc->result = table_index;
4082                 return 0;
4083         }
4084         if (loc->idx < col)
4085                 return -1;
4086         else 
4087                 return 1;
4088 }
4089
4090 static int
4091 declsec_locator (const void *a, const void *b)
4092 {
4093         locator_t *loc = (locator_t *) a;
4094         const char *bb = (const char *) b;
4095         guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
4096         guint32 col;
4097
4098         col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
4099
4100         if (loc->idx == col) {
4101                 loc->result = table_index;
4102                 return 0;
4103         }
4104         if (loc->idx < col)
4105                 return -1;
4106         else
4107                 return 1;
4108 }
4109
4110 /**
4111  * search_ptr_table:
4112  *
4113  *  Return the 1-based row index in TABLE, which must be one of the *Ptr tables, 
4114  * which contains IDX.
4115  */
4116 static guint32
4117 search_ptr_table (MonoImage *image, int table, int idx)
4118 {
4119         MonoTableInfo *ptrdef = &image->tables [table];
4120         int i;
4121
4122         /* Use a linear search to find our index in the table */
4123         for (i = 0; i < ptrdef->rows; i ++)
4124                 /* All the Ptr tables have the same structure */
4125                 if (mono_metadata_decode_row_col (ptrdef, i, 0) == idx)
4126                         break;
4127
4128         if (i < ptrdef->rows)
4129                 return i + 1;
4130         else
4131                 return idx;
4132 }
4133
4134 /**
4135  * mono_metadata_typedef_from_field:
4136  * @meta: metadata context
4137  * @index: FieldDef token
4138  *
4139  * Returns: the 1-based index into the TypeDef table of the type that
4140  * declared the field described by @index, or 0 if not found.
4141  */
4142 guint32
4143 mono_metadata_typedef_from_field (MonoImage *meta, guint32 index)
4144 {
4145         MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
4146         locator_t loc;
4147
4148         if (!tdef->base)
4149                 return 0;
4150
4151         loc.idx = mono_metadata_token_index (index);
4152         loc.col_idx = MONO_TYPEDEF_FIELD_LIST;
4153         loc.t = tdef;
4154
4155         if (meta->uncompressed_metadata)
4156                 loc.idx = search_ptr_table (meta, MONO_TABLE_FIELD_POINTER, loc.idx);
4157
4158         if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
4159                 return 0;
4160
4161         /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
4162         return loc.result + 1;
4163 }
4164
4165 /*
4166  * mono_metadata_typedef_from_method:
4167  * @meta: metadata context
4168  * @index: MethodDef token
4169  *
4170  * Returns: the 1-based index into the TypeDef table of the type that
4171  * declared the method described by @index.  0 if not found.
4172  */
4173 guint32
4174 mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
4175 {
4176         MonoTableInfo *tdef = &meta->tables [MONO_TABLE_TYPEDEF];
4177         locator_t loc;
4178         
4179         if (!tdef->base)
4180                 return 0;
4181
4182         loc.idx = mono_metadata_token_index (index);
4183         loc.col_idx = MONO_TYPEDEF_METHOD_LIST;
4184         loc.t = tdef;
4185
4186         if (meta->uncompressed_metadata)
4187                 loc.idx = search_ptr_table (meta, MONO_TABLE_METHOD_POINTER, loc.idx);
4188
4189         if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, typedef_locator))
4190                 return 0;
4191
4192         /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
4193         return loc.result + 1;
4194 }
4195
4196 /*
4197  * mono_metadata_interfaces_from_typedef_full:
4198  * @meta: metadata context
4199  * @index: typedef token
4200  * @interfaces: Out parameter used to store the interface array
4201  * @count: Out parameter used to store the number of interfaces
4202  * @heap_alloc_result: if TRUE the result array will be g_malloc'd
4203  * @context: The generic context
4204  * 
4205  * The array of interfaces that the @index typedef token implements is returned in
4206  * @interfaces. The number of elements in the array is returned in @count. 
4207  *
4208
4209  * Returns: TRUE on success, FALSE on failure.
4210  */
4211 gboolean
4212 mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, MonoClass ***interfaces, guint *count, gboolean heap_alloc_result, MonoGenericContext *context, MonoError *error)
4213 {
4214         MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
4215         locator_t loc;
4216         guint32 start, pos;
4217         guint32 cols [MONO_INTERFACEIMPL_SIZE];
4218         MonoClass **result;
4219
4220         *interfaces = NULL;
4221         *count = 0;
4222
4223         mono_error_init (error);
4224
4225         if (!tdef->base)
4226                 return TRUE;
4227
4228         loc.idx = mono_metadata_token_index (index);
4229         loc.col_idx = MONO_INTERFACEIMPL_CLASS;
4230         loc.t = tdef;
4231
4232         if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4233                 return TRUE;
4234
4235         start = loc.result;
4236         /*
4237          * We may end up in the middle of the rows... 
4238          */
4239         while (start > 0) {
4240                 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_INTERFACEIMPL_CLASS))
4241                         start--;
4242                 else
4243                         break;
4244         }
4245         pos = start;
4246         while (pos < tdef->rows) {
4247                 mono_metadata_decode_row (tdef, pos, cols, MONO_INTERFACEIMPL_SIZE);
4248                 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
4249                         break;
4250                 ++pos;
4251         }
4252
4253         if (heap_alloc_result)
4254                 result = g_new0 (MonoClass*, pos - start);
4255         else
4256                 result = (MonoClass **)mono_image_alloc0 (meta, sizeof (MonoClass*) * (pos - start));
4257
4258         pos = start;
4259         while (pos < tdef->rows) {
4260                 MonoClass *iface;
4261                 
4262                 mono_metadata_decode_row (tdef, pos, cols, MONO_INTERFACEIMPL_SIZE);
4263                 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
4264                         break;
4265                 iface = mono_class_get_and_inflate_typespec_checked (
4266                         meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]), context, error);
4267                 if (iface == NULL)
4268                         return FALSE;
4269                 result [pos - start] = iface;
4270                 ++pos;
4271         }
4272         *count = pos - start;
4273         *interfaces = result;
4274         return TRUE;
4275 }
4276
4277 /*
4278  * @meta: metadata context
4279  * @index: typedef token
4280  * @count: Out parameter used to store the number of interfaces
4281  * 
4282  * The array of interfaces that the @index typedef token implements is returned in
4283  * @interfaces. The number of elements in the array is returned in @count. The returned
4284  * array is g_malloc'd and the caller must free it.
4285  *
4286  * LOCKING: Acquires the loader lock .
4287  *
4288  * Returns: the interface array on success, NULL on failure.
4289  */
4290
4291 MonoClass**
4292 mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *count)
4293 {
4294         MonoError error;
4295         MonoClass **interfaces = NULL;
4296         gboolean rv;
4297
4298         rv = mono_metadata_interfaces_from_typedef_full (meta, index, &interfaces, count, TRUE, NULL, &error);
4299         g_assert (mono_error_ok (&error)); /* FIXME dont swallow the error */
4300         if (rv)
4301                 return interfaces;
4302         else
4303                 return NULL;
4304 }
4305
4306 /*
4307  * mono_metadata_nested_in_typedef:
4308  * @meta: metadata context
4309  * @index: typedef token
4310  * 
4311  * Returns: the 1-based index into the TypeDef table of the type
4312  * where the type described by @index is nested.
4313  * Returns 0 if @index describes a non-nested type.
4314  */
4315 guint32
4316 mono_metadata_nested_in_typedef (MonoImage *meta, guint32 index)
4317 {
4318         MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
4319         locator_t loc;
4320         
4321         if (!tdef->base)
4322                 return 0;
4323
4324         loc.idx = mono_metadata_token_index (index);
4325         loc.col_idx = MONO_NESTED_CLASS_NESTED;
4326         loc.t = tdef;
4327
4328         if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4329                 return 0;
4330
4331         /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
4332         return mono_metadata_decode_row_col (tdef, loc.result, MONO_NESTED_CLASS_ENCLOSING) | MONO_TOKEN_TYPE_DEF;
4333 }
4334
4335 /*
4336  * mono_metadata_nesting_typedef:
4337  * @meta: metadata context
4338  * @index: typedef token
4339  * 
4340  * Returns: the 1-based index into the TypeDef table of the first type
4341  * that is nested inside the type described by @index. The search starts at
4342  * @start_index.  returns 0 if no such type is found.
4343  */
4344 guint32
4345 mono_metadata_nesting_typedef (MonoImage *meta, guint32 index, guint32 start_index)
4346 {
4347         MonoTableInfo *tdef = &meta->tables [MONO_TABLE_NESTEDCLASS];
4348         guint32 start;
4349         guint32 class_index = mono_metadata_token_index (index);
4350         
4351         if (!tdef->base)
4352                 return 0;
4353
4354         start = start_index;
4355
4356         while (start <= tdef->rows) {
4357                 if (class_index == mono_metadata_decode_row_col (tdef, start - 1, MONO_NESTED_CLASS_ENCLOSING))
4358                         break;
4359                 else
4360                         start++;
4361         }
4362
4363         if (start > tdef->rows)
4364                 return 0;
4365         else
4366                 return start;
4367 }
4368
4369 /*
4370  * mono_metadata_packing_from_typedef:
4371  * @meta: metadata context
4372  * @index: token representing a type
4373  * 
4374  * Returns: the info stored in the ClassLAyout table for the given typedef token
4375  * into the @packing and @size pointers.
4376  * Returns 0 if the info is not found.
4377  */
4378 guint32
4379 mono_metadata_packing_from_typedef (MonoImage *meta, guint32 index, guint32 *packing, guint32 *size)
4380 {
4381         MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CLASSLAYOUT];
4382         locator_t loc;
4383         guint32 cols [MONO_CLASS_LAYOUT_SIZE];
4384         
4385         if (!tdef->base)
4386                 return 0;
4387
4388         loc.idx = mono_metadata_token_index (index);
4389         loc.col_idx = MONO_CLASS_LAYOUT_PARENT;
4390         loc.t = tdef;
4391
4392         if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4393                 return 0;
4394
4395         mono_metadata_decode_row (tdef, loc.result, cols, MONO_CLASS_LAYOUT_SIZE);
4396         if (packing)
4397                 *packing = cols [MONO_CLASS_LAYOUT_PACKING_SIZE];
4398         if (size)
4399                 *size = cols [MONO_CLASS_LAYOUT_CLASS_SIZE];
4400
4401         /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
4402         return loc.result + 1;
4403 }
4404
4405 /*
4406  * mono_metadata_custom_attrs_from_index:
4407  * @meta: metadata context
4408  * @index: token representing the parent
4409  * 
4410  * Returns: the 1-based index into the CustomAttribute table of the first 
4411  * attribute which belongs to the metadata object described by @index.
4412  * Returns 0 if no such attribute is found.
4413  */
4414 guint32
4415 mono_metadata_custom_attrs_from_index (MonoImage *meta, guint32 index)
4416 {
4417         MonoTableInfo *tdef = &meta->tables [MONO_TABLE_CUSTOMATTRIBUTE];
4418         locator_t loc;
4419         
4420         if (!tdef->base)
4421                 return 0;
4422
4423         loc.idx = index;
4424         loc.col_idx = MONO_CUSTOM_ATTR_PARENT;
4425         loc.t = tdef;
4426
4427         /* FIXME: Index translation */
4428
4429         if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4430                 return 0;
4431
4432         /* Find the first entry by searching backwards */
4433         while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_CUSTOM_ATTR_PARENT) == index))
4434                 loc.result --;
4435
4436         /* loc_result is 0..1, needs to be mapped to table index (that is +1) */
4437         return loc.result + 1;
4438 }
4439
4440 /*
4441  * mono_metadata_declsec_from_index:
4442  * @meta: metadata context
4443  * @index: token representing the parent
4444  * 
4445  * Returns: the 0-based index into the DeclarativeSecurity table of the first 
4446  * attribute which belongs to the metadata object described by @index.
4447  * Returns -1 if no such attribute is found.
4448  */
4449 guint32
4450 mono_metadata_declsec_from_index (MonoImage *meta, guint32 index)
4451 {
4452         MonoTableInfo *tdef = &meta->tables [MONO_TABLE_DECLSECURITY];
4453         locator_t loc;
4454
4455         if (!tdef->base)
4456                 return -1;
4457
4458         loc.idx = index;
4459         loc.col_idx = MONO_DECL_SECURITY_PARENT;
4460         loc.t = tdef;
4461
4462         if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, declsec_locator))
4463                 return -1;
4464
4465         /* Find the first entry by searching backwards */
4466         while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_DECL_SECURITY_PARENT) == index))
4467                 loc.result --;
4468
4469         return loc.result;
4470 }
4471
4472 /*
4473  * mono_metadata_localscope_from_methoddef:
4474  * @meta: metadata context
4475  * @index: methoddef index
4476  * 
4477  * Returns: the 1-based index into the LocalScope table of the first
4478  * scope which belongs to the method described by @index.
4479  * Returns 0 if no such row is found.
4480  */
4481 guint32
4482 mono_metadata_localscope_from_methoddef (MonoImage *meta, guint32 index)
4483 {
4484         MonoTableInfo *tdef = &meta->tables [MONO_TABLE_LOCALSCOPE];
4485         locator_t loc;
4486
4487         if (!tdef->base)
4488                 return 0;
4489
4490         loc.idx = index;
4491         loc.col_idx = MONO_LOCALSCOPE_METHOD;
4492         loc.t = tdef;
4493
4494         if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
4495                 return 0;
4496
4497         /* Find the first entry by searching backwards */
4498         while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_LOCALSCOPE_METHOD) == index))
4499                 loc.result --;
4500
4501         return loc.result + 1;
4502 }
4503
4504 #ifdef DEBUG
4505 static void
4506 mono_backtrace (int limit)
4507 {
4508         void *array[limit];
4509         char **names;
4510         int i;
4511         backtrace (array, limit);
4512         names = backtrace_symbols (array, limit);
4513         for (i =0; i < limit; ++i) {
4514                 g_print ("\t%s\n", names [i]);
4515         }
4516         g_free (names);
4517 }
4518 #endif
4519
4520 static int i8_align;
4521
4522 /*
4523  * mono_type_set_alignment:
4524  *
4525  *   Set the alignment used by runtime to layout fields etc. of type TYPE to ALIGN.
4526  * This should only be used in AOT mode since the resulting layout will not match the
4527  * host abi layout.
4528  */
4529 void
4530 mono_type_set_alignment (MonoTypeEnum type, int align)
4531 {
4532         /* Support only a few types whose alignment is abi dependent */
4533         switch (type) {
4534         case MONO_TYPE_I8:
4535                 i8_align = align;
4536                 break;
4537         default:
4538                 g_assert_not_reached ();
4539                 break;
4540         }
4541 }
4542
4543 /*
4544  * mono_type_size:
4545  * @t: the type to return the size of
4546  *
4547  * Returns: the number of bytes required to hold an instance of this
4548  * type in memory
4549  */
4550 int
4551 mono_type_size (MonoType *t, int *align)
4552 {
4553         MonoTypeEnum simple_type;
4554
4555         if (!t) {
4556                 *align = 1;
4557                 return 0;
4558         }
4559         if (t->byref) {
4560                 *align = MONO_ABI_ALIGNOF (gpointer);
4561                 return MONO_ABI_SIZEOF (gpointer);
4562         }
4563
4564         simple_type = t->type;
4565  again:
4566         switch (simple_type) {
4567         case MONO_TYPE_VOID:
4568                 *align = 1;
4569                 return 0;
4570         case MONO_TYPE_BOOLEAN:
4571                 *align = MONO_ABI_ALIGNOF (gint8);
4572                 return 1;
4573         case MONO_TYPE_I1:
4574         case MONO_TYPE_U1:
4575                 *align = MONO_ABI_ALIGNOF (gint8);
4576                 return 1;
4577         case MONO_TYPE_CHAR:
4578         case MONO_TYPE_I2:
4579         case MONO_TYPE_U2:
4580                 *align = MONO_ABI_ALIGNOF (gint16);
4581                 return 2;               
4582         case MONO_TYPE_I4:
4583         case MONO_TYPE_U4:
4584                 *align = MONO_ABI_ALIGNOF (gint32);
4585                 return 4;
4586         case MONO_TYPE_R4:
4587                 *align = MONO_ABI_ALIGNOF (float);
4588                 return 4;
4589         case MONO_TYPE_I8:
4590         case MONO_TYPE_U8:
4591                 *align = MONO_ABI_ALIGNOF (gint64);
4592                 return 8;               
4593         case MONO_TYPE_R8:
4594                 *align = MONO_ABI_ALIGNOF (double);
4595                 return 8;               
4596         case MONO_TYPE_I:
4597         case MONO_TYPE_U:
4598                 *align = MONO_ABI_ALIGNOF (gpointer);
4599                 return MONO_ABI_SIZEOF (gpointer);
4600         case MONO_TYPE_VALUETYPE: {
4601                 if (t->data.klass->enumtype)
4602                         return mono_type_size (mono_class_enum_basetype (t->data.klass), align);
4603                 else
4604                         return mono_class_value_size (t->data.klass, (guint32*)align);
4605         }
4606         case MONO_TYPE_STRING:
4607         case MONO_TYPE_OBJECT:
4608         case MONO_TYPE_CLASS:
4609         case MONO_TYPE_SZARRAY:
4610         case MONO_TYPE_PTR:
4611         case MONO_TYPE_FNPTR:
4612         case MONO_TYPE_ARRAY:
4613                 *align = MONO_ABI_ALIGNOF (gpointer);
4614                 return MONO_ABI_SIZEOF (gpointer);
4615         case MONO_TYPE_TYPEDBYREF:
4616                 return mono_class_value_size (mono_defaults.typed_reference_class, (guint32*)align);
4617         case MONO_TYPE_GENERICINST: {
4618                 MonoGenericClass *gclass = t->data.generic_class;
4619                 MonoClass *container_class = gclass->container_class;
4620
4621                 // g_assert (!gclass->inst->is_open);
4622
4623                 if (container_class->valuetype) {
4624                         if (container_class->enumtype)
4625                                 return mono_type_size (mono_class_enum_basetype (container_class), align);
4626                         else
4627                                 return mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align);
4628                 } else {
4629                         *align = MONO_ABI_ALIGNOF (gpointer);
4630                         return MONO_ABI_SIZEOF (gpointer);
4631                 }
4632         }
4633         case MONO_TYPE_VAR:
4634         case MONO_TYPE_MVAR:
4635                 if (!t->data.generic_param->gshared_constraint || t->data.generic_param->gshared_constraint->type == MONO_TYPE_VALUETYPE) {
4636                         *align = MONO_ABI_ALIGNOF (gpointer);
4637                         return MONO_ABI_SIZEOF (gpointer);
4638                 } else {
4639                         /* The gparam can only match types given by gshared_constraint */
4640                         return mono_type_size (t->data.generic_param->gshared_constraint, align);
4641                         goto again;
4642                 }
4643         default:
4644                 g_error ("mono_type_size: type 0x%02x unknown", t->type);
4645         }
4646         return 0;
4647 }
4648
4649 /*
4650  * mono_type_stack_size:
4651  * @t: the type to return the size it uses on the stack
4652  *
4653  * Returns: the number of bytes required to hold an instance of this
4654  * type on the runtime stack
4655  */
4656 int
4657 mono_type_stack_size (MonoType *t, int *align)
4658 {
4659         return mono_type_stack_size_internal (t, align, FALSE);
4660 }
4661
4662 int
4663 mono_type_stack_size_internal (MonoType *t, int *align, gboolean allow_open)
4664 {
4665         int tmp;
4666         MonoTypeEnum simple_type;
4667 #if SIZEOF_VOID_P == SIZEOF_REGISTER
4668         int stack_slot_size = MONO_ABI_SIZEOF (gpointer);
4669         int stack_slot_align = MONO_ABI_ALIGNOF (gpointer);
4670 #elif SIZEOF_VOID_P < SIZEOF_REGISTER
4671         int stack_slot_size = SIZEOF_REGISTER;
4672         int stack_slot_align = SIZEOF_REGISTER;
4673 #endif
4674
4675         g_assert (t != NULL);
4676
4677         if (!align)
4678                 align = &tmp;
4679
4680         if (t->byref) {
4681                 *align = stack_slot_align;
4682                 return stack_slot_size;
4683         }
4684
4685         simple_type = t->type;
4686         switch (simple_type) {
4687         case MONO_TYPE_BOOLEAN:
4688         case MONO_TYPE_CHAR:
4689         case MONO_TYPE_I1:
4690         case MONO_TYPE_U1:
4691         case MONO_TYPE_I2:
4692         case MONO_TYPE_U2:
4693         case MONO_TYPE_I4:
4694         case MONO_TYPE_U4:
4695         case MONO_TYPE_I:
4696         case MONO_TYPE_U:
4697         case MONO_TYPE_STRING:
4698         case MONO_TYPE_OBJECT:
4699         case MONO_TYPE_CLASS:
4700         case MONO_TYPE_SZARRAY:
4701         case MONO_TYPE_PTR:
4702         case MONO_TYPE_FNPTR:
4703         case MONO_TYPE_ARRAY:
4704                 *align = stack_slot_align;
4705                 return stack_slot_size;
4706         case MONO_TYPE_VAR:
4707         case MONO_TYPE_MVAR:
4708                 g_assert (allow_open);
4709                 if (!t->data.generic_param->gshared_constraint || t->data.generic_param->gshared_constraint->type == MONO_TYPE_VALUETYPE) {
4710                         *align = stack_slot_align;
4711                         return stack_slot_size;
4712                 } else {
4713                         /* The gparam can only match types given by gshared_constraint */
4714                         return mono_type_stack_size_internal (t->data.generic_param->gshared_constraint, align, allow_open);
4715                 }
4716         case MONO_TYPE_TYPEDBYREF:
4717                 *align = stack_slot_align;
4718                 return stack_slot_size * 3;
4719         case MONO_TYPE_R4:
4720                 *align = MONO_ABI_ALIGNOF (float);
4721                 return sizeof (float);          
4722         case MONO_TYPE_I8:
4723         case MONO_TYPE_U8:
4724                 *align = MONO_ABI_ALIGNOF (gint64);
4725                 return sizeof (gint64);         
4726         case MONO_TYPE_R8:
4727                 *align = MONO_ABI_ALIGNOF (double);
4728                 return sizeof (double);
4729         case MONO_TYPE_VALUETYPE: {
4730                 guint32 size;
4731
4732                 if (t->data.klass->enumtype)
4733                         return mono_type_stack_size_internal (mono_class_enum_basetype (t->data.klass), align, allow_open);
4734                 else {
4735                         size = mono_class_value_size (t->data.klass, (guint32*)align);
4736
4737                         *align = *align + stack_slot_align - 1;
4738                         *align &= ~(stack_slot_align - 1);
4739
4740                         size += stack_slot_size - 1;
4741                         size &= ~(stack_slot_size - 1);
4742
4743                         return size;
4744                 }
4745         }
4746         case MONO_TYPE_GENERICINST: {
4747                 MonoGenericClass *gclass = t->data.generic_class;
4748                 MonoClass *container_class = gclass->container_class;
4749
4750                 if (!allow_open)
4751                         g_assert (!gclass->context.class_inst->is_open);
4752
4753                 if (container_class->valuetype) {
4754                         if (container_class->enumtype)
4755                                 return mono_type_stack_size_internal (mono_class_enum_basetype (container_class), align, allow_open);
4756                         else {
4757                                 guint32 size = mono_class_value_size (mono_class_from_mono_type (t), (guint32*)align);
4758
4759                                 *align = *align + stack_slot_align - 1;
4760                                 *align &= ~(stack_slot_align - 1);
4761
4762                                 size += stack_slot_size - 1;
4763                                 size &= ~(stack_slot_size - 1);
4764
4765                                 return size;
4766                         }
4767                 } else {
4768                         *align = stack_slot_align;
4769                         return stack_slot_size;
4770                 }
4771         }
4772         default:
4773                 g_error ("type 0x%02x unknown", t->type);
4774         }
4775         return 0;
4776 }
4777
4778 gboolean
4779 mono_type_generic_inst_is_valuetype (MonoType *type)
4780 {
4781         g_assert (type->type == MONO_TYPE_GENERICINST);
4782         return type->data.generic_class->container_class->valuetype;
4783 }
4784
4785 gboolean
4786 mono_metadata_generic_class_is_valuetype (MonoGenericClass *gclass)
4787 {
4788         return gclass->container_class->valuetype;
4789 }
4790
4791 static gboolean
4792 _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2, gboolean signature_only)
4793 {
4794         MonoGenericInst *i1 = g1->context.class_inst;
4795         MonoGenericInst *i2 = g2->context.class_inst;
4796
4797         if (g1->is_dynamic != g2->is_dynamic)
4798                 return FALSE;
4799         if (!mono_metadata_class_equal (g1->container_class, g2->container_class, signature_only))
4800                 return FALSE;
4801         if (!mono_generic_inst_equal_full (i1, i2, signature_only))
4802                 return FALSE;
4803         return g1->is_tb_open == g2->is_tb_open;
4804 }
4805
4806 static gboolean
4807 _mono_metadata_generic_class_container_equal (const MonoGenericClass *g1, MonoClass *c2, gboolean signature_only)
4808 {
4809         MonoGenericInst *i1 = g1->context.class_inst;
4810         MonoGenericInst *i2 = c2->generic_container->context.class_inst;
4811
4812         if (!mono_metadata_class_equal (g1->container_class, c2, signature_only))
4813                 return FALSE;
4814         if (!mono_generic_inst_equal_full (i1, i2, signature_only))
4815                 return FALSE;
4816         return !g1->is_tb_open;
4817 }
4818
4819 guint
4820 mono_metadata_generic_context_hash (const MonoGenericContext *context)
4821 {
4822         /* FIXME: check if this seed is good enough */
4823         guint hash = 0xc01dfee7;
4824         if (context->class_inst)
4825                 hash = ((hash << 5) - hash) ^ mono_metadata_generic_inst_hash (context->class_inst);
4826         if (context->method_inst)
4827                 hash = ((hash << 5) - hash) ^ mono_metadata_generic_inst_hash (context->method_inst);
4828         return hash;
4829 }
4830
4831 gboolean
4832 mono_metadata_generic_context_equal (const MonoGenericContext *g1, const MonoGenericContext *g2)
4833 {
4834         return g1->class_inst == g2->class_inst && g1->method_inst == g2->method_inst;
4835 }
4836
4837 /*
4838  * mono_metadata_str_hash:
4839  *
4840  *   This should be used instead of g_str_hash for computing hash codes visible
4841  * outside this module, since g_str_hash () is not guaranteed to be stable
4842  * (its not the same in eglib for example).
4843  */
4844 guint
4845 mono_metadata_str_hash (gconstpointer v1)
4846 {
4847         /* Same as g_str_hash () in glib */
4848         char *p = (char *) v1;
4849         guint hash = *p;
4850
4851         while (*p++) {
4852                 if (*p)
4853                         hash = (hash << 5) - hash + *p;
4854         }
4855
4856         return hash;
4857
4858
4859 /*
4860  * mono_metadata_type_hash:
4861  * @t1: a type
4862  *
4863  * Computes an hash value for @t1 to be used in GHashTable.
4864  * The returned hash is guaranteed to be the same across executions.
4865  */
4866 guint
4867 mono_metadata_type_hash (MonoType *t1)
4868 {
4869         guint hash = t1->type;
4870
4871         hash |= t1->byref << 6; /* do not collide with t1->type values */
4872         switch (t1->type) {
4873         case MONO_TYPE_VALUETYPE:
4874         case MONO_TYPE_CLASS:
4875         case MONO_TYPE_SZARRAY: {
4876                 MonoClass *klass = t1->data.klass;
4877                 /*
4878                  * Dynamic classes must not be hashed on their type since it can change
4879                  * during runtime. For example, if we hash a reference type that is
4880                  * later made into a valuetype.
4881                  *
4882                  * This is specially problematic with generic instances since they are
4883                  * inserted in a bunch of hash tables before been finished.
4884                  */
4885                 if (image_is_dynamic (klass->image))
4886                         return (t1->byref << 6) | mono_metadata_str_hash (klass->name);
4887                 return ((hash << 5) - hash) ^ mono_metadata_str_hash (klass->name);
4888         }
4889         case MONO_TYPE_PTR:
4890                 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
4891         case MONO_TYPE_ARRAY:
4892                 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
4893         case MONO_TYPE_GENERICINST:
4894                 return ((hash << 5) - hash) ^ mono_generic_class_hash (t1->data.generic_class);
4895         case MONO_TYPE_VAR:
4896         case MONO_TYPE_MVAR:
4897                 return ((hash << 5) - hash) ^ mono_metadata_generic_param_hash (t1->data.generic_param);
4898         default:
4899                 return hash;
4900         }
4901 }
4902
4903 guint
4904 mono_metadata_generic_param_hash (MonoGenericParam *p)
4905 {
4906         guint hash;
4907         MonoGenericParamInfo *info;
4908
4909         hash = (mono_generic_param_num (p) << 2);
4910         if (p->gshared_constraint)
4911                 hash = ((hash << 5) - hash) ^ mono_metadata_type_hash (p->gshared_constraint);
4912         info = mono_generic_param_info (p);
4913         /* Can't hash on the owner klass/method, since those might not be set when this is called */
4914         if (info)
4915                 hash = ((hash << 5) - hash) ^ info->token;
4916         return hash;
4917 }
4918
4919 static gboolean
4920 mono_metadata_generic_param_equal_internal (MonoGenericParam *p1, MonoGenericParam *p2, gboolean signature_only)
4921 {
4922         if (p1 == p2)
4923                 return TRUE;
4924         if (mono_generic_param_num (p1) != mono_generic_param_num (p2))
4925                 return FALSE;
4926         if (p1->gshared_constraint && p2->gshared_constraint) {
4927                 if (!mono_metadata_type_equal (p1->gshared_constraint, p2->gshared_constraint))
4928                         return FALSE;
4929         } else {
4930                 if (p1->gshared_constraint != p2->gshared_constraint)
4931                         return FALSE;
4932         }
4933
4934         /*
4935          * We have to compare the image as well because if we didn't,
4936          * the generic_inst_cache lookup wouldn't care about the image
4937          * of generic params, so what could happen is that a generic
4938          * inst with params from image A is put into the cache, then
4939          * image B gets that generic inst from the cache, image A is
4940          * unloaded, so the inst is deleted, but image B still retains
4941          * a pointer to it.
4942          */
4943         if (mono_generic_param_owner (p1) == mono_generic_param_owner (p2))
4944                 return TRUE;
4945
4946         /*
4947          * If `signature_only' is true, we're comparing two (method) signatures.
4948          * In this case, the owner of two type parameters doesn't need to match.
4949          */
4950
4951         return signature_only;
4952 }
4953
4954 gboolean
4955 mono_metadata_generic_param_equal (MonoGenericParam *p1, MonoGenericParam *p2)
4956 {
4957         return mono_metadata_generic_param_equal_internal (p1, p2, TRUE);
4958 }
4959
4960 static gboolean
4961 mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only)
4962 {
4963         if (c1 == c2)
4964                 return TRUE;
4965         if (c1->generic_class && c2->generic_class)
4966                 return _mono_metadata_generic_class_equal (c1->generic_class, c2->generic_class, signature_only);
4967         if (c1->generic_class && c2->generic_container)
4968                 return _mono_metadata_generic_class_container_equal (c1->generic_class, c2, signature_only);
4969         if (c1->generic_container && c2->generic_class)
4970                 return _mono_metadata_generic_class_container_equal (c2->generic_class, c1, signature_only);
4971         if ((c1->byval_arg.type == MONO_TYPE_VAR) && (c2->byval_arg.type == MONO_TYPE_VAR))
4972                 return mono_metadata_generic_param_equal_internal (
4973                         c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
4974         if ((c1->byval_arg.type == MONO_TYPE_MVAR) && (c2->byval_arg.type == MONO_TYPE_MVAR))
4975                 return mono_metadata_generic_param_equal_internal (
4976                         c1->byval_arg.data.generic_param, c2->byval_arg.data.generic_param, signature_only);
4977         if (signature_only &&
4978             (c1->byval_arg.type == MONO_TYPE_SZARRAY) && (c2->byval_arg.type == MONO_TYPE_SZARRAY))
4979                 return mono_metadata_class_equal (c1->byval_arg.data.klass, c2->byval_arg.data.klass, signature_only);
4980         if (signature_only &&
4981             (c1->byval_arg.type == MONO_TYPE_ARRAY) && (c2->byval_arg.type == MONO_TYPE_ARRAY))
4982                 return do_mono_metadata_type_equal (&c1->byval_arg, &c2->byval_arg, signature_only);
4983         return FALSE;
4984 }
4985
4986 static gboolean
4987 mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only)
4988 {
4989         gpointer iter1 = 0, iter2 = 0;
4990
4991         if (s1 == s2)
4992                 return TRUE;
4993         if (s1->call_convention != s2->call_convention)
4994                 return FALSE;
4995         if (s1->sentinelpos != s2->sentinelpos)
4996                 return FALSE;
4997         if (s1->hasthis != s2->hasthis)
4998                 return FALSE;
4999         if (s1->explicit_this != s2->explicit_this)
5000                 return FALSE;
5001         if (! do_mono_metadata_type_equal (s1->ret, s2->ret, signature_only))
5002                 return FALSE;
5003         if (s1->param_count != s2->param_count)
5004                 return FALSE;
5005
5006         while (TRUE) {
5007                 MonoType *t1 = mono_signature_get_params (s1, &iter1);
5008                 MonoType *t2 = mono_signature_get_params (s2, &iter2);
5009
5010                 if (t1 == NULL || t2 == NULL)
5011                         return (t1 == t2);
5012                 if (! do_mono_metadata_type_equal (t1, t2, signature_only))
5013                         return FALSE;
5014         }
5015 }
5016
5017 /*
5018  * mono_metadata_type_equal:
5019  * @t1: a type
5020  * @t2: another type
5021  * @signature_only: If true, treat ginsts as equal which are instantiated separately but have equal positional value
5022  *
5023  * Determine if @t1 and @t2 represent the same type.
5024  * Returns: #TRUE if @t1 and @t2 are equal.
5025  */
5026 static gboolean
5027 do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only)
5028 {
5029         if (t1->type != t2->type || t1->byref != t2->byref)
5030                 return FALSE;
5031
5032         switch (t1->type) {
5033         case MONO_TYPE_VOID:
5034         case MONO_TYPE_BOOLEAN:
5035         case MONO_TYPE_CHAR:
5036         case MONO_TYPE_I1:
5037         case MONO_TYPE_U1:
5038         case MONO_TYPE_I2:
5039         case MONO_TYPE_U2:
5040         case MONO_TYPE_I4:
5041         case MONO_TYPE_U4:
5042         case MONO_TYPE_I8:
5043         case MONO_TYPE_U8:
5044         case MONO_TYPE_R4:
5045         case MONO_TYPE_R8:
5046         case MONO_TYPE_STRING:
5047         case MONO_TYPE_I:
5048         case MONO_TYPE_U:
5049         case MONO_TYPE_OBJECT:
5050         case MONO_TYPE_TYPEDBYREF:
5051                 return TRUE;
5052         case MONO_TYPE_VALUETYPE:
5053         case MONO_TYPE_CLASS:
5054         case MONO_TYPE_SZARRAY:
5055                 return mono_metadata_class_equal (t1->data.klass, t2->data.klass, signature_only);
5056         case MONO_TYPE_PTR:
5057                 return do_mono_metadata_type_equal (t1->data.type, t2->data.type, signature_only);
5058         case MONO_TYPE_ARRAY:
5059                 if (t1->data.array->rank != t2->data.array->rank)
5060                         return FALSE;
5061                 return mono_metadata_class_equal (t1->data.array->eklass, t2->data.array->eklass, signature_only);
5062         case MONO_TYPE_GENERICINST:
5063                 return _mono_metadata_generic_class_equal (
5064                         t1->data.generic_class, t2->data.generic_class, signature_only);
5065         case MONO_TYPE_VAR:
5066                 return mono_metadata_generic_param_equal_internal (
5067                         t1->data.generic_param, t2->data.generic_param, signature_only);
5068         case MONO_TYPE_MVAR:
5069                 return mono_metadata_generic_param_equal_internal (
5070                         t1->data.generic_param, t2->data.generic_param, signature_only);
5071         case MONO_TYPE_FNPTR:
5072                 return mono_metadata_fnptr_equal (t1->data.method, t2->data.method, signature_only);
5073         default:
5074                 g_error ("implement type compare for %0x!", t1->type);
5075                 return FALSE;
5076         }
5077
5078         return FALSE;
5079 }
5080
5081 gboolean
5082 mono_metadata_type_equal (MonoType *t1, MonoType *t2)
5083 {
5084         return do_mono_metadata_type_equal (t1, t2, FALSE);
5085 }
5086
5087 /**
5088  * mono_metadata_type_equal_full:
5089  * @t1: a type
5090  * @t2: another type
5091  * @signature_only: if signature only comparison should be made
5092  *
5093  * Determine if @t1 and @t2 are signature compatible if @signature_only is #TRUE, otherwise
5094  * behaves the same way as mono_metadata_type_equal.
5095  * The function mono_metadata_type_equal(a, b) is just a shortcut for mono_metadata_type_equal_full(a, b, FALSE).
5096  * Returns: #TRUE if @t1 and @t2 are equal taking @signature_only into account.
5097  */
5098 gboolean
5099 mono_metadata_type_equal_full (MonoType *t1, MonoType *t2, gboolean signature_only)
5100 {
5101         return do_mono_metadata_type_equal (t1, t2, signature_only);
5102 }
5103
5104 /**
5105  * mono_metadata_signature_equal:
5106  * @sig1: a signature
5107  * @sig2: another signature
5108  *
5109  * Determine if @sig1 and @sig2 represent the same signature, with the
5110  * same number of arguments and the same types.
5111  * Returns: #TRUE if @sig1 and @sig2 are equal.
5112  */
5113 gboolean
5114 mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
5115 {
5116         int i;
5117
5118         if (sig1->hasthis != sig2->hasthis || sig1->param_count != sig2->param_count)
5119                 return FALSE;
5120
5121         if (sig1->generic_param_count != sig2->generic_param_count)
5122                 return FALSE;
5123
5124         /*
5125          * We're just comparing the signatures of two methods here:
5126          *
5127          * If we have two generic methods `void Foo<U> (U u)' and `void Bar<V> (V v)',
5128          * U and V are equal here.
5129          *
5130          * That's what the `signature_only' argument of do_mono_metadata_type_equal() is for.
5131          */
5132
5133         for (i = 0; i < sig1->param_count; i++) { 
5134                 MonoType *p1 = sig1->params[i];
5135                 MonoType *p2 = sig2->params[i];
5136                 
5137                 /* if (p1->attrs != p2->attrs)
5138                         return FALSE;
5139                 */
5140                 if (!do_mono_metadata_type_equal (p1, p2, TRUE))
5141                         return FALSE;
5142         }
5143
5144         if (!do_mono_metadata_type_equal (sig1->ret, sig2->ret, TRUE))
5145                 return FALSE;
5146         return TRUE;
5147 }
5148
5149 /**
5150  * mono_metadata_type_dup:
5151  * @image: image to alloc memory from
5152  * @original: type to duplicate
5153  *
5154  * Returns: copy of type allocated from the image's mempool (or from the heap, if @image is null).
5155  */
5156 MonoType *
5157 mono_metadata_type_dup (MonoImage *image, const MonoType *o)
5158 {
5159         MonoType *r = NULL;
5160         int sizeof_o = MONO_SIZEOF_TYPE;
5161         if (o->num_mods)
5162                 sizeof_o += o->num_mods  * sizeof (MonoCustomMod);
5163
5164         r = image ? (MonoType *)mono_image_alloc0 (image, sizeof_o) : (MonoType *)g_malloc (sizeof_o);
5165
5166         memcpy (r, o, sizeof_o);
5167
5168         if (o->type == MONO_TYPE_PTR) {
5169                 r->data.type = mono_metadata_type_dup (image, o->data.type);
5170         } else if (o->type == MONO_TYPE_ARRAY) {
5171                 r->data.array = mono_dup_array_type (image, o->data.array);
5172         } else if (o->type == MONO_TYPE_FNPTR) {
5173                 /*FIXME the dup'ed signature is leaked mono_metadata_free_type*/
5174                 r->data.method = mono_metadata_signature_deep_dup (image, o->data.method);
5175         }
5176         return r;
5177 }
5178
5179 guint
5180 mono_signature_hash (MonoMethodSignature *sig)
5181 {
5182         guint i, res = sig->ret->type;
5183
5184         for (i = 0; i < sig->param_count; i++)
5185                 res = (res << 5) - res + mono_type_hash (sig->params[i]);
5186
5187         return res;
5188 }
5189
5190 /*
5191  * mono_metadata_encode_value:
5192  * @value: value to encode
5193  * @buf: buffer where to write the compressed representation
5194  * @endbuf: pointer updated to point at the end of the encoded output
5195  *
5196  * Encodes the value @value in the compressed representation used
5197  * in metadata and stores the result in @buf. @buf needs to be big
5198  * enough to hold the data (4 bytes).
5199  */
5200 void
5201 mono_metadata_encode_value (guint32 value, char *buf, char **endbuf)
5202 {
5203         char *p = buf;
5204         
5205         if (value < 0x80)
5206                 *p++ = value;
5207         else if (value < 0x4000) {
5208                 p [0] = 0x80 | (value >> 8);
5209                 p [1] = value & 0xff;
5210                 p += 2;
5211         } else {
5212                 p [0] = (value >> 24) | 0xc0;
5213                 p [1] = (value >> 16) & 0xff;
5214                 p [2] = (value >> 8) & 0xff;
5215                 p [3] = value & 0xff;
5216                 p += 4;
5217         }
5218         if (endbuf)
5219                 *endbuf = p;
5220 }
5221
5222 /*
5223  * mono_metadata_field_info:
5224  * @meta: the Image the field is defined in
5225  * @index: the index in the field table representing the field
5226  * @offset: a pointer to an integer where to store the offset that 
5227  * may have been specified for the field in a FieldLayout table
5228  * @rva: a pointer to the RVA of the field data in the image that
5229  * may have been defined in a FieldRVA table
5230  * @marshal_spec: a pointer to the marshal spec that may have been 
5231  * defined for the field in a FieldMarshal table.
5232  *
5233  * Gather info for field @index that may have been defined in the FieldLayout, 
5234  * FieldRVA and FieldMarshal tables.
5235  * Either of offset, rva and marshal_spec can be NULL if you're not interested 
5236  * in the data.
5237  */
5238 void
5239 mono_metadata_field_info (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva, 
5240                           MonoMarshalSpec **marshal_spec)
5241 {
5242         mono_metadata_field_info_full (meta, index, offset, rva, marshal_spec, FALSE);
5243 }
5244
5245 void
5246 mono_metadata_field_info_with_mempool (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva, 
5247                           MonoMarshalSpec **marshal_spec)
5248 {
5249         mono_metadata_field_info_full (meta, index, offset, rva, marshal_spec, TRUE);
5250 }
5251
5252 static void
5253 mono_metadata_field_info_full (MonoImage *meta, guint32 index, guint32 *offset, guint32 *rva, 
5254                                        MonoMarshalSpec **marshal_spec, gboolean alloc_from_image)
5255 {
5256         MonoTableInfo *tdef;
5257         locator_t loc;
5258
5259         loc.idx = index + 1;
5260         if (meta->uncompressed_metadata)
5261                 loc.idx = search_ptr_table (meta, MONO_TABLE_FIELD_POINTER, loc.idx);
5262
5263         if (offset) {
5264                 tdef = &meta->tables [MONO_TABLE_FIELDLAYOUT];
5265
5266                 loc.col_idx = MONO_FIELD_LAYOUT_FIELD;
5267                 loc.t = tdef;
5268
5269                 if (tdef->base && mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
5270                         *offset = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_LAYOUT_OFFSET);
5271                 } else {
5272                         *offset = (guint32)-1;
5273                 }
5274         }
5275         if (rva) {
5276                 tdef = &meta->tables [MONO_TABLE_FIELDRVA];
5277
5278                 loc.col_idx = MONO_FIELD_RVA_FIELD;
5279                 loc.t = tdef;
5280                 
5281                 if (tdef->base && mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
5282                         /*
5283                          * LAMESPEC: There is no signature, no nothing, just the raw data.
5284                          */
5285                         *rva = mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_RVA_RVA);
5286                 } else {
5287                         *rva = 0;
5288                 }
5289         }
5290         if (marshal_spec) {
5291                 const char *p;
5292                 
5293                 if ((p = mono_metadata_get_marshal_info (meta, index, TRUE))) {
5294                         *marshal_spec = mono_metadata_parse_marshal_spec_full (alloc_from_image ? meta : NULL, meta, p);
5295                 }
5296         }
5297
5298 }
5299
5300 /*
5301  * mono_metadata_get_constant_index:
5302  * @meta: the Image the field is defined in
5303  * @index: the token that may have a row defined in the constants table
5304  * @hint: possible position for the row
5305  *
5306  * @token must be a FieldDef, ParamDef or PropertyDef token.
5307  *
5308  * Returns: the index into the Constants table or 0 if not found.
5309  */
5310 guint32
5311 mono_metadata_get_constant_index (MonoImage *meta, guint32 token, guint32 hint)
5312 {
5313         MonoTableInfo *tdef;
5314         locator_t loc;
5315         guint32 index = mono_metadata_token_index (token);
5316
5317         tdef = &meta->tables [MONO_TABLE_CONSTANT];
5318         index <<= MONO_HASCONSTANT_BITS;
5319         switch (mono_metadata_token_table (token)) {
5320         case MONO_TABLE_FIELD:
5321                 index |= MONO_HASCONSTANT_FIEDDEF;
5322                 break;
5323         case MONO_TABLE_PARAM:
5324                 index |= MONO_HASCONSTANT_PARAM;
5325                 break;
5326         case MONO_TABLE_PROPERTY:
5327                 index |= MONO_HASCONSTANT_PROPERTY;
5328                 break;
5329         default:
5330                 g_warning ("Not a valid token for the constant table: 0x%08x", token);
5331                 return 0;
5332         }
5333         loc.idx = index;
5334         loc.col_idx = MONO_CONSTANT_PARENT;
5335         loc.t = tdef;
5336
5337         /* FIXME: Index translation */
5338
5339         if ((hint > 0) && (hint < tdef->rows) && (mono_metadata_decode_row_col (tdef, hint - 1, MONO_CONSTANT_PARENT) == index))
5340                 return hint;
5341
5342         if (tdef->base && mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator)) {
5343                 return loc.result + 1;
5344         }
5345         return 0;
5346 }
5347
5348 /*
5349  * mono_metadata_events_from_typedef:
5350  * @meta: metadata context
5351  * @index: 0-based index (in the TypeDef table) describing a type
5352  *
5353  * Returns: the 0-based index in the Event table for the events in the
5354  * type. The last event that belongs to the type (plus 1) is stored
5355  * in the @end_idx pointer.
5356  */
5357 guint32
5358 mono_metadata_events_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
5359 {
5360         locator_t loc;
5361         guint32 start, end;
5362         MonoTableInfo *tdef  = &meta->tables [MONO_TABLE_EVENTMAP];
5363
5364         *end_idx = 0;
5365         
5366         if (!tdef->base)
5367                 return 0;
5368
5369         loc.t = tdef;
5370         loc.col_idx = MONO_EVENT_MAP_PARENT;
5371         loc.idx = index + 1;
5372
5373         if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
5374                 return 0;
5375         
5376         start = mono_metadata_decode_row_col (tdef, loc.result, MONO_EVENT_MAP_EVENTLIST);
5377         if (loc.result + 1 < tdef->rows) {
5378                 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_EVENT_MAP_EVENTLIST) - 1;
5379         } else {
5380                 end = meta->tables [MONO_TABLE_EVENT].rows;
5381         }
5382
5383         *end_idx = end;
5384         return start - 1;
5385 }
5386
5387 /*
5388  * mono_metadata_methods_from_event:
5389  * @meta: metadata context
5390  * @index: 0-based index (in the Event table) describing a event
5391  *
5392  * Returns: the 0-based index in the MethodDef table for the methods in the
5393  * event. The last method that belongs to the event (plus 1) is stored
5394  * in the @end_idx pointer.
5395  */
5396 guint32
5397 mono_metadata_methods_from_event   (MonoImage *meta, guint32 index, guint *end_idx)
5398 {
5399         locator_t loc;
5400         guint start, end;
5401         guint32 cols [MONO_METHOD_SEMA_SIZE];
5402         MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
5403
5404         *end_idx = 0;
5405         if (!msemt->base)
5406                 return 0;
5407
5408         if (meta->uncompressed_metadata)
5409             index = search_ptr_table (meta, MONO_TABLE_EVENT_POINTER, index + 1) - 1;
5410
5411         loc.t = msemt;
5412         loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
5413         loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_EVENT; /* Method association coded index */
5414
5415         if (!mono_binary_search (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
5416                 return 0;
5417
5418         start = loc.result;
5419         /*
5420          * We may end up in the middle of the rows... 
5421          */
5422         while (start > 0) {
5423                 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
5424                         start--;
5425                 else
5426                         break;
5427         }
5428         end = start + 1;
5429         while (end < msemt->rows) {
5430                 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
5431                 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
5432                         break;
5433                 ++end;
5434         }
5435         *end_idx = end;
5436         return start;
5437 }
5438
5439 /*
5440  * mono_metadata_properties_from_typedef:
5441  * @meta: metadata context
5442  * @index: 0-based index (in the TypeDef table) describing a type
5443  *
5444  * Returns: the 0-based index in the Property table for the properties in the
5445  * type. The last property that belongs to the type (plus 1) is stored
5446  * in the @end_idx pointer.
5447  */
5448 guint32
5449 mono_metadata_properties_from_typedef (MonoImage *meta, guint32 index, guint *end_idx)
5450 {
5451         locator_t loc;
5452         guint32 start, end;
5453         MonoTableInfo *tdef  = &meta->tables [MONO_TABLE_PROPERTYMAP];
5454
5455         *end_idx = 0;
5456         
5457         if (!tdef->base)
5458                 return 0;
5459
5460         loc.t = tdef;
5461         loc.col_idx = MONO_PROPERTY_MAP_PARENT;
5462         loc.idx = index + 1;
5463
5464         if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
5465                 return 0;
5466         
5467         start = mono_metadata_decode_row_col (tdef, loc.result, MONO_PROPERTY_MAP_PROPERTY_LIST);
5468         if (loc.result + 1 < tdef->rows) {
5469                 end = mono_metadata_decode_row_col (tdef, loc.result + 1, MONO_PROPERTY_MAP_PROPERTY_LIST) - 1;
5470         } else {
5471                 end = meta->tables [MONO_TABLE_PROPERTY].rows;
5472         }
5473
5474         *end_idx = end;
5475         return start - 1;
5476 }
5477
5478 /*
5479  * mono_metadata_methods_from_property:
5480  * @meta: metadata context
5481  * @index: 0-based index (in the PropertyDef table) describing a property
5482  *
5483  * Returns: the 0-based index in the MethodDef table for the methods in the
5484  * property. The last method that belongs to the property (plus 1) is stored
5485  * in the @end_idx pointer.
5486  */
5487 guint32
5488 mono_metadata_methods_from_property   (MonoImage *meta, guint32 index, guint *end_idx)
5489 {
5490         locator_t loc;
5491         guint start, end;
5492         guint32 cols [MONO_METHOD_SEMA_SIZE];
5493         MonoTableInfo *msemt = &meta->tables [MONO_TABLE_METHODSEMANTICS];
5494
5495         *end_idx = 0;
5496         if (!msemt->base)
5497                 return 0;
5498
5499         if (meta->uncompressed_metadata)
5500             index = search_ptr_table (meta, MONO_TABLE_PROPERTY_POINTER, index + 1) - 1;
5501
5502         loc.t = msemt;
5503         loc.col_idx = MONO_METHOD_SEMA_ASSOCIATION;
5504         loc.idx = ((index + 1) << MONO_HAS_SEMANTICS_BITS) | MONO_HAS_SEMANTICS_PROPERTY; /* Method association coded index */
5505
5506         if (!mono_binary_search (&loc, msemt->base, msemt->rows, msemt->row_size, table_locator))
5507                 return 0;
5508
5509         start = loc.result;
5510         /*
5511          * We may end up in the middle of the rows... 
5512          */
5513         while (start > 0) {
5514                 if (loc.idx == mono_metadata_decode_row_col (msemt, start - 1, MONO_METHOD_SEMA_ASSOCIATION))
5515                         start--;
5516                 else
5517                         break;
5518         }
5519         end = start + 1;
5520         while (end < msemt->rows) {
5521                 mono_metadata_decode_row (msemt, end, cols, MONO_METHOD_SEMA_SIZE);
5522                 if (cols [MONO_METHOD_SEMA_ASSOCIATION] != loc.idx)
5523                         break;
5524                 ++end;
5525         }
5526         *end_idx = end;
5527         return start;
5528 }
5529
5530 guint32
5531 mono_metadata_implmap_from_method (MonoImage *meta, guint32 method_idx)
5532 {
5533         locator_t loc;
5534         MonoTableInfo *tdef  = &meta->tables [MONO_TABLE_IMPLMAP];
5535
5536         if (!tdef->base)
5537                 return 0;
5538
5539         /* No index translation seems to be needed */
5540
5541         loc.t = tdef;
5542         loc.col_idx = MONO_IMPLMAP_MEMBER;
5543         loc.idx = ((method_idx + 1) << MONO_MEMBERFORWD_BITS) | MONO_MEMBERFORWD_METHODDEF;
5544
5545         if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
5546                 return 0;
5547
5548         return loc.result + 1;
5549 }
5550
5551 /**
5552  * @image: context where the image is created
5553  * @type_spec:  typespec token
5554  * @deprecated use mono_type_create_from_typespec_checked that has proper error handling
5555  *
5556  * Creates a MonoType representing the TypeSpec indexed by the @type_spec
5557  * token.
5558  */
5559 MonoType *
5560 mono_type_create_from_typespec (MonoImage *image, guint32 type_spec)
5561 {
5562         MonoError error;
5563         MonoType *type = mono_type_create_from_typespec_checked (image, type_spec, &error);
5564         if (!type)
5565                  g_error ("Could not create typespec %x due to %s", type_spec, mono_error_get_message (&error));
5566         return type;
5567 }
5568
5569 MonoType *
5570 mono_type_create_from_typespec_checked (MonoImage *image, guint32 type_spec, MonoError *error)
5571
5572 {
5573         guint32 idx = mono_metadata_token_index (type_spec);
5574         MonoTableInfo *t;
5575         guint32 cols [MONO_TYPESPEC_SIZE];
5576         const char *ptr;
5577         MonoType *type, *type2;
5578
5579         mono_error_init (error);
5580
5581         mono_image_lock (image);
5582         type = (MonoType *)g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec));
5583         mono_image_unlock (image);
5584         if (type)
5585                 return type;
5586
5587         t = &image->tables [MONO_TABLE_TYPESPEC];
5588
5589         mono_metadata_decode_row (t, idx-1, cols, MONO_TYPESPEC_SIZE);
5590         ptr = mono_metadata_blob_heap (image, cols [MONO_TYPESPEC_SIGNATURE]);
5591
5592         if (!mono_verifier_verify_typespec_signature (image, cols [MONO_TYPESPEC_SIGNATURE], type_spec, NULL)) {
5593                 mono_error_set_bad_image (error, image, "Could not verify type spec %08x.", type_spec);
5594                 return NULL;
5595         }
5596
5597         mono_metadata_decode_value (ptr, &ptr);
5598
5599         type = mono_metadata_parse_type_checked (image, NULL, 0, TRUE, ptr, &ptr, error);
5600         if (!type)
5601                 return NULL;
5602
5603         type2 = mono_metadata_type_dup (image, type);
5604         mono_metadata_free_type (type);
5605
5606         mono_image_lock (image);
5607         type = (MonoType *)g_hash_table_lookup (image->typespec_cache, GUINT_TO_POINTER (type_spec));
5608         /* We might leak some data in the image mempool if found */
5609         if (!type) {
5610                 g_hash_table_insert (image->typespec_cache, GUINT_TO_POINTER (type_spec), type2);
5611                 type = type2;
5612         }
5613         mono_image_unlock (image);
5614
5615         return type;
5616 }
5617
5618
5619 static char*
5620 mono_image_strndup (MonoImage *image, const char *data, guint len)
5621 {
5622         char *res;
5623         if (!image)
5624                 return g_strndup (data, len);
5625         res = (char *)mono_image_alloc (image, len + 1);
5626         memcpy (res, data, len);
5627         res [len] = 0;
5628         return res;
5629 }
5630
5631 MonoMarshalSpec *
5632 mono_metadata_parse_marshal_spec (MonoImage *image, const char *ptr)
5633 {
5634         return mono_metadata_parse_marshal_spec_full (NULL, image, ptr);
5635 }
5636
5637 /*
5638  * If IMAGE is non-null, memory will be allocated from its mempool, otherwise it will be allocated using malloc.
5639  * PARENT_IMAGE is the image containing the marshal spec.
5640  */
5641 MonoMarshalSpec *
5642 mono_metadata_parse_marshal_spec_full (MonoImage *image, MonoImage *parent_image, const char *ptr)
5643 {
5644         MonoMarshalSpec *res;
5645         int len;
5646         const char *start = ptr;
5647
5648         /* fixme: this is incomplete, but I cant find more infos in the specs */
5649
5650         if (image)
5651                 res = (MonoMarshalSpec *)mono_image_alloc0 (image, sizeof (MonoMarshalSpec));
5652         else
5653                 res = g_new0 (MonoMarshalSpec, 1);
5654         
5655         len = mono_metadata_decode_value (ptr, &ptr);
5656         res->native = (MonoMarshalNative)*ptr++;
5657
5658         if (res->native == MONO_NATIVE_LPARRAY) {
5659                 res->data.array_data.param_num = -1;
5660                 res->data.array_data.num_elem = -1;
5661                 res->data.array_data.elem_mult = -1;
5662
5663                 if (ptr - start <= len)
5664                         res->data.array_data.elem_type = (MonoMarshalNative)*ptr++;
5665                 if (ptr - start <= len)
5666                         res->data.array_data.param_num = mono_metadata_decode_value (ptr, &ptr);
5667                 if (ptr - start <= len)
5668                         res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
5669                 if (ptr - start <= len) {
5670                         /*
5671                          * LAMESPEC: Older spec versions say this parameter comes before 
5672                          * num_elem. Never spec versions don't talk about elem_mult at
5673                          * all, but csc still emits it, and it is used to distinguish
5674                          * between param_num being 0, and param_num being omitted.
5675                          * So if (param_num == 0) && (num_elem > 0), then
5676                          * elem_mult == 0 -> the array size is num_elem
5677                          * elem_mult == 1 -> the array size is @param_num + num_elem
5678                          */
5679                         res->data.array_data.elem_mult = mono_metadata_decode_value (ptr, &ptr);
5680                 }
5681         } 
5682
5683         if (res->native == MONO_NATIVE_BYVALTSTR) {
5684                 if (ptr - start <= len)
5685                         res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
5686         }
5687
5688         if (res->native == MONO_NATIVE_BYVALARRAY) {
5689                 if (ptr - start <= len)
5690                         res->data.array_data.num_elem = mono_metadata_decode_value (ptr, &ptr);
5691         }
5692         
5693         if (res->native == MONO_NATIVE_CUSTOM) {
5694                 /* skip unused type guid */
5695                 len = mono_metadata_decode_value (ptr, &ptr);
5696                 ptr += len;
5697                 /* skip unused native type name */
5698                 len = mono_metadata_decode_value (ptr, &ptr);
5699                 ptr += len;
5700                 /* read custom marshaler type name */
5701                 len = mono_metadata_decode_value (ptr, &ptr);
5702                 res->data.custom_data.custom_name = mono_image_strndup (image, ptr, len);               
5703                 ptr += len;
5704                 /* read cookie string */
5705                 len = mono_metadata_decode_value (ptr, &ptr);
5706                 res->data.custom_data.cookie = mono_image_strndup (image, ptr, len);
5707                 res->data.custom_data.image = parent_image;
5708         }
5709
5710         if (res->native == MONO_NATIVE_SAFEARRAY) {
5711                 res->data.safearray_data.elem_type = (MonoMarshalVariant)0;
5712                 res->data.safearray_data.num_elem = 0;
5713                 if (ptr - start <= len)
5714                         res->data.safearray_data.elem_type = (MonoMarshalVariant)*ptr++;
5715                 if (ptr - start <= len)
5716                         res->data.safearray_data.num_elem = *ptr++;
5717         }
5718         return res;
5719 }
5720
5721 void 
5722 mono_metadata_free_marshal_spec (MonoMarshalSpec *spec)
5723 {
5724         if (spec->native == MONO_NATIVE_CUSTOM) {
5725                 g_free (spec->data.custom_data.custom_name);
5726                 g_free (spec->data.custom_data.cookie);
5727         }
5728         g_free (spec);
5729 }
5730
5731 /**
5732  * mono_type_to_unmanaged:
5733  *
5734  * Returns: A MonoMarshalNative enumeration value (MONO_NATIVE_) value
5735  * describing the underlying native reprensetation of the type.
5736  * 
5737  * In addition the value pointed by
5738  * "conv" will contain the kind of marshalling required for this
5739  * particular type one of the MONO_MARSHAL_CONV_ enumeration values.
5740  */
5741 guint32
5742 mono_type_to_unmanaged (MonoType *type, MonoMarshalSpec *mspec, gboolean as_field,
5743                         gboolean unicode, MonoMarshalConv *conv) 
5744 {
5745         MonoMarshalConv dummy_conv;
5746         int t = type->type;
5747
5748         if (!conv)
5749                 conv = &dummy_conv;
5750
5751         *conv = MONO_MARSHAL_CONV_NONE;
5752
5753         if (type->byref)
5754                 return MONO_NATIVE_UINT;
5755
5756 handle_enum:
5757         switch (t) {
5758         case MONO_TYPE_BOOLEAN: 
5759                 if (mspec) {
5760                         switch (mspec->native) {
5761                         case MONO_NATIVE_VARIANTBOOL:
5762                                 *conv = MONO_MARSHAL_CONV_BOOL_VARIANTBOOL;
5763                                 return MONO_NATIVE_VARIANTBOOL;
5764                         case MONO_NATIVE_BOOLEAN:
5765                                 *conv = MONO_MARSHAL_CONV_BOOL_I4;
5766                                 return MONO_NATIVE_BOOLEAN;
5767                         case MONO_NATIVE_I1:
5768                         case MONO_NATIVE_U1:
5769                                 return mspec->native;
5770                         default:
5771                                 g_error ("cant marshal bool to native type %02x", mspec->native);
5772                         }
5773                 }
5774                 *conv = MONO_MARSHAL_CONV_BOOL_I4;
5775                 return MONO_NATIVE_BOOLEAN;
5776         case MONO_TYPE_CHAR:
5777                 if (mspec) {
5778                         switch (mspec->native) {
5779                         case MONO_NATIVE_U2:
5780                         case MONO_NATIVE_U1:
5781                                 return mspec->native;
5782                         default:
5783                                 g_error ("cant marshal char to native type %02x", mspec->native);
5784                         }
5785                 }
5786                 return unicode ? MONO_NATIVE_U2 : MONO_NATIVE_U1;
5787         case MONO_TYPE_I1: return MONO_NATIVE_I1;
5788         case MONO_TYPE_U1: return MONO_NATIVE_U1;
5789         case MONO_TYPE_I2: return MONO_NATIVE_I2;
5790         case MONO_TYPE_U2: return MONO_NATIVE_U2;
5791         case MONO_TYPE_I4: return MONO_NATIVE_I4;
5792         case MONO_TYPE_U4: return MONO_NATIVE_U4;
5793         case MONO_TYPE_I8: return MONO_NATIVE_I8;
5794         case MONO_TYPE_U8: return MONO_NATIVE_U8;
5795         case MONO_TYPE_R4: return MONO_NATIVE_R4;
5796         case MONO_TYPE_R8: return MONO_NATIVE_R8;
5797         case MONO_TYPE_STRING:
5798                 if (mspec) {
5799                         switch (mspec->native) {
5800                         case MONO_NATIVE_BSTR:
5801                                 *conv = MONO_MARSHAL_CONV_STR_BSTR;
5802                                 return MONO_NATIVE_BSTR;
5803                         case MONO_NATIVE_LPSTR:
5804                                 *conv = MONO_MARSHAL_CONV_STR_LPSTR;
5805                                 return MONO_NATIVE_LPSTR;
5806                         case MONO_NATIVE_LPWSTR:
5807                                 *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
5808                                 return MONO_NATIVE_LPWSTR;
5809                         case MONO_NATIVE_LPTSTR:
5810                                 *conv = MONO_MARSHAL_CONV_STR_LPTSTR;
5811                                 return MONO_NATIVE_LPTSTR;
5812                         case MONO_NATIVE_ANSIBSTR:
5813                                 *conv = MONO_MARSHAL_CONV_STR_ANSIBSTR;
5814                                 return MONO_NATIVE_ANSIBSTR;
5815                         case MONO_NATIVE_TBSTR:
5816                                 *conv = MONO_MARSHAL_CONV_STR_TBSTR;
5817                                 return MONO_NATIVE_TBSTR;
5818                         case MONO_NATIVE_BYVALTSTR:
5819                                 if (unicode)
5820                                         *conv = MONO_MARSHAL_CONV_STR_BYVALWSTR;
5821                                 else
5822                                         *conv = MONO_MARSHAL_CONV_STR_BYVALSTR;
5823                                 return MONO_NATIVE_BYVALTSTR;
5824                         default:
5825                                 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);
5826                         }
5827                 }       
5828                 if (unicode) {
5829                         *conv = MONO_MARSHAL_CONV_STR_LPWSTR;
5830                         return MONO_NATIVE_LPWSTR; 
5831                 }
5832                 else {
5833                         *conv = MONO_MARSHAL_CONV_STR_LPSTR;
5834                         return MONO_NATIVE_LPSTR; 
5835                 }
5836         case MONO_TYPE_PTR: return MONO_NATIVE_UINT;
5837         case MONO_TYPE_VALUETYPE: /*FIXME*/
5838                 if (type->data.klass->enumtype) {
5839                         t = mono_class_enum_basetype (type->data.klass)->type;
5840                         goto handle_enum;
5841                 }
5842                 if (type->data.klass == mono_defaults.handleref_class){
5843                         *conv = MONO_MARSHAL_CONV_HANDLEREF;
5844                         return MONO_NATIVE_INT;
5845                 }
5846                 return MONO_NATIVE_STRUCT;
5847         case MONO_TYPE_SZARRAY: 
5848         case MONO_TYPE_ARRAY: 
5849                 if (mspec) {
5850                         switch (mspec->native) {
5851                         case MONO_NATIVE_BYVALARRAY:
5852                                 if ((type->data.klass->element_class == mono_defaults.char_class) && !unicode)
5853                                         *conv = MONO_MARSHAL_CONV_ARRAY_BYVALCHARARRAY;
5854                                 else
5855                                         *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
5856                                 return MONO_NATIVE_BYVALARRAY;
5857                         case MONO_NATIVE_SAFEARRAY:
5858                                 *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
5859                                 return MONO_NATIVE_SAFEARRAY;
5860                         case MONO_NATIVE_LPARRAY:                               
5861                                 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
5862                                 return MONO_NATIVE_LPARRAY;
5863                         default:
5864                                 g_error ("cant marshal array as native type %02x", mspec->native);
5865                         }
5866                 }       
5867
5868                 *conv = MONO_MARSHAL_CONV_ARRAY_LPARRAY;
5869                 return MONO_NATIVE_LPARRAY;
5870         case MONO_TYPE_I: return MONO_NATIVE_INT;
5871         case MONO_TYPE_U: return MONO_NATIVE_UINT;
5872         case MONO_TYPE_CLASS: 
5873         case MONO_TYPE_OBJECT: {
5874                 /* FIXME : we need to handle ArrayList and StringBuilder here, probably */
5875                 if (mspec) {
5876                         switch (mspec->native) {
5877                         case MONO_NATIVE_STRUCT:
5878                                 return MONO_NATIVE_STRUCT;
5879                         case MONO_NATIVE_CUSTOM:
5880                                 return MONO_NATIVE_CUSTOM;
5881                         case MONO_NATIVE_INTERFACE:
5882                                 *conv = MONO_MARSHAL_CONV_OBJECT_INTERFACE;
5883                                 return MONO_NATIVE_INTERFACE;
5884                         case MONO_NATIVE_IDISPATCH:
5885                                 *conv = MONO_MARSHAL_CONV_OBJECT_IDISPATCH;
5886                                 return MONO_NATIVE_IDISPATCH;
5887                         case MONO_NATIVE_IUNKNOWN:
5888                                 *conv = MONO_MARSHAL_CONV_OBJECT_IUNKNOWN;
5889                                 return MONO_NATIVE_IUNKNOWN;
5890                         case MONO_NATIVE_FUNC:
5891                                 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
5892                                                                                          type->data.klass == mono_defaults.delegate_class || 
5893                                                                                          type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
5894                                         *conv = MONO_MARSHAL_CONV_DEL_FTN;
5895                                         return MONO_NATIVE_FUNC;
5896                                 }
5897                                 /* Fall through */
5898                         default:
5899                                 g_error ("cant marshal object as native type %02x", mspec->native);
5900                         }
5901                 }
5902                 if (t == MONO_TYPE_CLASS && (type->data.klass == mono_defaults.multicastdelegate_class ||
5903                                              type->data.klass == mono_defaults.delegate_class || 
5904                                              type->data.klass->parent == mono_defaults.multicastdelegate_class)) {
5905                         *conv = MONO_MARSHAL_CONV_DEL_FTN;
5906                         return MONO_NATIVE_FUNC;
5907                 }
5908                 if (mono_class_try_get_safehandle_class () && type->data.klass == mono_class_try_get_safehandle_class ()){
5909                         *conv = MONO_MARSHAL_CONV_SAFEHANDLE;
5910                         return MONO_NATIVE_INT;
5911                 }
5912                 *conv = MONO_MARSHAL_CONV_OBJECT_STRUCT;
5913                 return MONO_NATIVE_STRUCT;
5914         }
5915         case MONO_TYPE_FNPTR: return MONO_NATIVE_FUNC;
5916         case MONO_TYPE_GENERICINST:
5917                 type = &type->data.generic_class->container_class->byval_arg;
5918                 t = type->type;
5919                 goto handle_enum;
5920         case MONO_TYPE_TYPEDBYREF:
5921         default:
5922                 g_error ("type 0x%02x not handled in marshal", t);
5923         }
5924         return MONO_NATIVE_MAX;
5925 }
5926
5927 const char*
5928 mono_metadata_get_marshal_info (MonoImage *meta, guint32 idx, gboolean is_field)
5929 {
5930         locator_t loc;
5931         MonoTableInfo *tdef  = &meta->tables [MONO_TABLE_FIELDMARSHAL];
5932
5933         if (!tdef->base)
5934                 return NULL;
5935
5936         loc.t = tdef;
5937         loc.col_idx = MONO_FIELD_MARSHAL_PARENT;
5938         loc.idx = ((idx + 1) << MONO_HAS_FIELD_MARSHAL_BITS) | (is_field? MONO_HAS_FIELD_MARSHAL_FIELDSREF: MONO_HAS_FIELD_MARSHAL_PARAMDEF);
5939
5940         /* FIXME: Index translation */
5941
5942         if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
5943                 return NULL;
5944
5945         return mono_metadata_blob_heap (meta, mono_metadata_decode_row_col (tdef, loc.result, MONO_FIELD_MARSHAL_NATIVE_TYPE));
5946 }
5947
5948 MonoMethod*
5949 method_from_method_def_or_ref (MonoImage *m, guint32 tok, MonoGenericContext *context, MonoError *error)
5950 {
5951         MonoMethod *result = NULL;
5952         guint32 idx = tok >> MONO_METHODDEFORREF_BITS;
5953
5954         mono_error_init (error);
5955
5956         switch (tok & MONO_METHODDEFORREF_MASK) {
5957         case MONO_METHODDEFORREF_METHODDEF:
5958                 result = mono_get_method_checked (m, MONO_TOKEN_METHOD_DEF | idx, NULL, context, error);
5959                 break;
5960         case MONO_METHODDEFORREF_METHODREF:
5961                 result = mono_get_method_checked (m, MONO_TOKEN_MEMBER_REF | idx, NULL, context, error);
5962                 break;
5963         default:
5964                 mono_error_set_bad_image (error, m, "Invalid MethodDefOfRef token %x", tok);
5965         }
5966
5967         return result;
5968 }
5969
5970 /*
5971  * mono_class_get_overrides_full:
5972  *
5973  *   Return the method overrides belonging to class @type_token in @overrides, and
5974  * the number of overrides in @num_overrides.
5975  *
5976  * Returns: TRUE on success, FALSE on failure.
5977  */
5978 gboolean
5979 mono_class_get_overrides_full (MonoImage *image, guint32 type_token, MonoMethod ***overrides, gint32 *num_overrides,
5980                                MonoGenericContext *generic_context)
5981 {
5982         MonoError error;
5983         locator_t loc;
5984         MonoTableInfo *tdef  = &image->tables [MONO_TABLE_METHODIMPL];
5985         guint32 start, end;
5986         gint32 i, num;
5987         guint32 cols [MONO_METHODIMPL_SIZE];
5988         MonoMethod **result;
5989         gint32 ok = TRUE;
5990         
5991         *overrides = NULL;
5992         if (num_overrides)
5993                 *num_overrides = 0;
5994
5995         if (!tdef->base)
5996                 return TRUE;
5997
5998         loc.t = tdef;
5999         loc.col_idx = MONO_METHODIMPL_CLASS;
6000         loc.idx = mono_metadata_token_index (type_token);
6001
6002         if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
6003                 return TRUE;
6004
6005         start = loc.result;
6006         end = start + 1;
6007         /*
6008          * We may end up in the middle of the rows... 
6009          */
6010         while (start > 0) {
6011                 if (loc.idx == mono_metadata_decode_row_col (tdef, start - 1, MONO_METHODIMPL_CLASS))
6012                         start--;
6013                 else
6014                         break;
6015         }
6016         while (end < tdef->rows) {
6017                 if (loc.idx == mono_metadata_decode_row_col (tdef, end, MONO_METHODIMPL_CLASS))
6018                         end++;
6019                 else
6020                         break;
6021         }
6022         num = end - start;
6023         result = g_new (MonoMethod*, num * 2);
6024         for (i = 0; i < num; ++i) {
6025                 MonoMethod *method;
6026
6027                 if (!mono_verifier_verify_methodimpl_row (image, start + i, &error)) {
6028                         mono_error_cleanup (&error); /* FIXME don't swallow the error */
6029                         ok = FALSE;
6030                         break;
6031                 }
6032
6033                 mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
6034                 method = method_from_method_def_or_ref (
6035                         image, cols [MONO_METHODIMPL_DECLARATION], generic_context, &error);
6036                 if (method == NULL) {
6037                         mono_error_cleanup (&error); /* FIXME don't swallow the error */
6038                         ok = FALSE;
6039                 }
6040                 result [i * 2] = method;
6041                 method = method_from_method_def_or_ref (
6042                         image, cols [MONO_METHODIMPL_BODY], generic_context, &error);
6043                 if (method == NULL) {
6044                         mono_error_cleanup (&error); /* FIXME don't swallow the error */
6045                         ok = FALSE;
6046                 }
6047                 result [i * 2 + 1] = method;
6048         }
6049
6050         *overrides = result;
6051         if (num_overrides)
6052                 *num_overrides = num;
6053         return ok;
6054 }
6055
6056 /**
6057  * mono_guid_to_string:
6058  *
6059  * Converts a 16 byte Microsoft GUID to the standard string representation.
6060  */
6061 char *
6062 mono_guid_to_string (const guint8 *guid)
6063 {
6064         return g_strdup_printf ("%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X", 
6065                                 guid[3], guid[2], guid[1], guid[0],
6066                                 guid[5], guid[4],
6067                                 guid[7], guid[6],
6068                                 guid[8], guid[9],
6069                                 guid[10], guid[11], guid[12], guid[13], guid[14], guid[15]);
6070 }
6071
6072 static gboolean
6073 get_constraints (MonoImage *image, int owner, MonoClass ***constraints, MonoGenericContainer *container, MonoError *error)
6074 {
6075         MonoTableInfo *tdef  = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
6076         guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
6077         guint32 i, token, found;
6078         MonoClass *klass, **res;
6079         GSList *cons = NULL, *tmp;
6080         MonoGenericContext *context = &container->context;
6081
6082         mono_error_init (error);
6083
6084         *constraints = NULL;
6085         found = 0;
6086         for (i = 0; i < tdef->rows; ++i) {
6087                 mono_metadata_decode_row (tdef, i, cols, MONO_GENPARCONSTRAINT_SIZE);
6088                 if (cols [MONO_GENPARCONSTRAINT_GENERICPAR] == owner) {
6089                         token = mono_metadata_token_from_dor (cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
6090                         klass = mono_class_get_and_inflate_typespec_checked (image, token, context, error);
6091                         if (!klass) {
6092                                 g_slist_free (cons);
6093                                 return FALSE;
6094                         }
6095                         cons = g_slist_append (cons, klass);
6096                         ++found;
6097                 } else {
6098                         /* contiguous list finished */
6099                         if (found)
6100                                 break;
6101                 }
6102         }
6103         if (!found)
6104                 return TRUE;
6105         res = (MonoClass **)mono_image_alloc0 (image, sizeof (MonoClass*) * (found + 1));
6106         for (i = 0, tmp = cons; i < found; ++i, tmp = tmp->next) {
6107                 res [i] = (MonoClass *)tmp->data;
6108         }
6109         g_slist_free (cons);
6110         *constraints = res;
6111         return TRUE;
6112 }
6113
6114 /*
6115  * mono_metadata_get_generic_param_row:
6116  *
6117  * @image:
6118  * @token: TypeOrMethodDef token, owner for GenericParam
6119  * @owner: coded token, set on return
6120  * 
6121  * Returns: 1-based row-id in the GenericParam table whose
6122  * owner is @token. 0 if not found.
6123  */
6124 guint32
6125 mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner)
6126 {
6127         MonoTableInfo *tdef  = &image->tables [MONO_TABLE_GENERICPARAM];
6128         locator_t loc;
6129
6130         g_assert (owner);
6131         if (!tdef->base)
6132                 return 0;
6133
6134         if (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF)
6135                 *owner = MONO_TYPEORMETHOD_TYPE;
6136         else if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
6137                 *owner = MONO_TYPEORMETHOD_METHOD;
6138         else {
6139                 g_error ("wrong token %x to get_generic_param_row", token);
6140                 return 0;
6141         }
6142         *owner |= mono_metadata_token_index (token) << MONO_TYPEORMETHOD_BITS;
6143
6144         loc.idx = *owner;
6145         loc.col_idx = MONO_GENERICPARAM_OWNER;
6146         loc.t = tdef;
6147
6148         if (!mono_binary_search (&loc, tdef->base, tdef->rows, tdef->row_size, table_locator))
6149                 return 0;
6150
6151         /* Find the first entry by searching backwards */
6152         while ((loc.result > 0) && (mono_metadata_decode_row_col (tdef, loc.result - 1, MONO_GENERICPARAM_OWNER) == loc.idx))
6153                 loc.result --;
6154
6155         return loc.result + 1;
6156 }
6157
6158 gboolean
6159 mono_metadata_has_generic_params (MonoImage *image, guint32 token)
6160 {
6161         guint32 owner;
6162         return mono_metadata_get_generic_param_row (image, token, &owner);
6163 }
6164
6165 /*
6166  * Memory is allocated from IMAGE's mempool.
6167  */
6168 gboolean
6169 mono_metadata_load_generic_param_constraints_checked (MonoImage *image, guint32 token,
6170                                               MonoGenericContainer *container, MonoError *error)
6171 {
6172
6173         guint32 start_row, i, owner;
6174         mono_error_init (error);
6175
6176         if (! (start_row = mono_metadata_get_generic_param_row (image, token, &owner)))
6177                 return TRUE;
6178         for (i = 0; i < container->type_argc; i++) {
6179                 if (!get_constraints (image, start_row + i, &mono_generic_container_get_param_info (container, i)->constraints, container, error)) {
6180                         return FALSE;
6181                 }
6182         }
6183         return TRUE;
6184 }
6185
6186 /*
6187  * mono_metadata_load_generic_params:
6188  *
6189  * Load the type parameters from the type or method definition @token.
6190  *
6191  * Use this method after parsing a type or method definition to figure out whether it's a generic
6192  * type / method.  When parsing a method definition, @parent_container points to the generic container
6193  * of the current class, if any.
6194  *
6195  * Note: This method does not load the constraints: for typedefs, this has to be done after fully
6196  *       creating the type.
6197  *
6198  * Returns: NULL if @token is not a generic type or method definition or the new generic container.
6199  *
6200  * LOCKING: Acquires the loader lock
6201  *
6202  */
6203 MonoGenericContainer *
6204 mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericContainer *parent_container)
6205 {
6206         MonoTableInfo *tdef  = &image->tables [MONO_TABLE_GENERICPARAM];
6207         guint32 cols [MONO_GENERICPARAM_SIZE];
6208         guint32 i, owner = 0, n;
6209         MonoGenericContainer *container;
6210         MonoGenericParamFull *params;
6211         MonoGenericContext *context;
6212
6213         if (!(i = mono_metadata_get_generic_param_row (image, token, &owner)))
6214                 return NULL;
6215         mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
6216         params = NULL;
6217         n = 0;
6218         container = (MonoGenericContainer *)mono_image_alloc0 (image, sizeof (MonoGenericContainer));
6219         container->owner.image = image; // Temporarily mark as anonymous, but this will be overriden by caller
6220         container->is_anonymous = TRUE;
6221         do {
6222                 n++;
6223                 params = (MonoGenericParamFull *)g_realloc (params, sizeof (MonoGenericParamFull) * n);
6224                 memset (&params [n - 1], 0, sizeof (MonoGenericParamFull));
6225                 params [n - 1].param.owner = container;
6226                 params [n - 1].param.num = cols [MONO_GENERICPARAM_NUMBER];
6227                 params [n - 1].info.token = i | MONO_TOKEN_GENERIC_PARAM;
6228                 params [n - 1].info.flags = cols [MONO_GENERICPARAM_FLAGS];
6229                 params [n - 1].info.name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
6230                 if (params [n - 1].param.num != n - 1)
6231                         g_warning ("GenericParam table unsorted or hole in generic param sequence: token %d", i);
6232                 if (++i > tdef->rows)
6233                         break;
6234                 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
6235         } while (cols [MONO_GENERICPARAM_OWNER] == owner);
6236
6237         container->type_argc = n;
6238         container->type_params = (MonoGenericParamFull *)mono_image_alloc0 (image, sizeof (MonoGenericParamFull) * n);
6239         memcpy (container->type_params, params, sizeof (MonoGenericParamFull) * n);
6240         g_free (params);
6241         container->parent = parent_container;
6242
6243         if (mono_metadata_token_table (token) == MONO_TABLE_METHOD)
6244                 container->is_method = 1;
6245
6246         g_assert (container->parent == NULL || container->is_method);
6247
6248         context = &container->context;
6249         if (container->is_method) {
6250                 context->class_inst = container->parent ? container->parent->context.class_inst : NULL;
6251                 context->method_inst = mono_get_shared_generic_inst (container);
6252         } else {
6253                 context->class_inst = mono_get_shared_generic_inst (container);
6254         }
6255
6256         return container;
6257 }
6258
6259 MonoGenericInst *
6260 mono_get_shared_generic_inst (MonoGenericContainer *container)
6261 {
6262         MonoType **type_argv;
6263         MonoType *helper;
6264         MonoGenericInst *nginst;
6265         int i;
6266
6267         type_argv = g_new0 (MonoType *, container->type_argc);
6268         helper = g_new0 (MonoType, container->type_argc);
6269
6270         for (i = 0; i < container->type_argc; i++) {
6271                 MonoType *t = &helper [i];
6272
6273                 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
6274                 t->data.generic_param = mono_generic_container_get_param (container, i);
6275
6276                 type_argv [i] = t;
6277         }
6278
6279         nginst = mono_metadata_get_generic_inst (container->type_argc, type_argv);
6280
6281         g_free (type_argv);
6282         g_free (helper);
6283
6284         return nginst;
6285 }
6286
6287 /**
6288  * mono_type_is_byref:
6289  * @type: the MonoType operated on
6290  *
6291  * Returns: #TRUE if @type represents a type passed by reference,
6292  * #FALSE otherwise.
6293  */
6294 gboolean
6295 mono_type_is_byref (MonoType *type)
6296 {
6297         return type->byref;
6298 }
6299
6300 /**
6301  * mono_type_get_type:
6302  * @type: the MonoType operated on
6303  *
6304  * Returns: the IL type value for @type. This is one of the MonoTypeEnum
6305  * enum members like MONO_TYPE_I4 or MONO_TYPE_STRING.
6306  */
6307 int
6308 mono_type_get_type (MonoType *type)
6309 {
6310         return type->type;
6311 }
6312
6313 /**
6314  * mono_type_get_signature:
6315  * @type: the MonoType operated on
6316  *
6317  * It is only valid to call this function if @type is a MONO_TYPE_FNPTR.
6318  *
6319  * Returns: the MonoMethodSignature pointer that describes the signature
6320  * of the function pointer @type represents.
6321  */
6322 MonoMethodSignature*
6323 mono_type_get_signature (MonoType *type)
6324 {
6325         g_assert (type->type == MONO_TYPE_FNPTR);
6326         return type->data.method;
6327 }
6328
6329 /**
6330  * mono_type_get_class:
6331  * @type: the MonoType operated on
6332  *
6333  * It is only valid to call this function if @type is a MONO_TYPE_CLASS or a
6334  * MONO_TYPE_VALUETYPE. For more general functionality, use mono_class_from_mono_type (),
6335  * instead
6336  *
6337  * Returns: the MonoClass pointer that describes the class that @type represents.
6338  */
6339 MonoClass*
6340 mono_type_get_class (MonoType *type)
6341 {
6342         /* FIXME: review the runtime users before adding the assert here */
6343         return type->data.klass;
6344 }
6345
6346 /**
6347  * mono_type_get_array_type:
6348  * @type: the MonoType operated on
6349  *
6350  * It is only valid to call this function if @type is a MONO_TYPE_ARRAY.
6351  *
6352  * Returns: a MonoArrayType struct describing the array type that @type
6353  * represents. The info includes details such as rank, array element type
6354  * and the sizes and bounds of multidimensional arrays.
6355  */
6356 MonoArrayType*
6357 mono_type_get_array_type (MonoType *type)
6358 {
6359         return type->data.array;
6360 }
6361
6362 /**
6363  * mono_type_get_ptr_type:
6364  * @type: the MonoType operated on
6365  *
6366  * It is only valid to call this function if @type is a MONO_TYPE_PTR.
6367  * instead
6368  *
6369  * Returns: the MonoType pointer that describes the type that @type
6370  * represents a pointer to.
6371  */
6372 MonoType*
6373 mono_type_get_ptr_type (MonoType *type)
6374 {
6375         g_assert (type->type == MONO_TYPE_PTR);
6376         return type->data.type;
6377 }
6378
6379 MonoClass*
6380 mono_type_get_modifiers (MonoType *type, gboolean *is_required, gpointer *iter)
6381 {
6382         /* FIXME: implement */
6383         return NULL;
6384 }
6385
6386 /**
6387  * mono_type_is_struct:
6388  * @type: the MonoType operated on
6389  *
6390  * Returns: #TRUE is @type is a struct, that is a ValueType but not en enum
6391  * or a basic type like System.Int32. #FALSE otherwise.
6392  */
6393 mono_bool
6394 mono_type_is_struct (MonoType *type)
6395 {
6396         return (!type->byref && ((type->type == MONO_TYPE_VALUETYPE &&
6397                 !type->data.klass->enumtype) || (type->type == MONO_TYPE_TYPEDBYREF) ||
6398                 ((type->type == MONO_TYPE_GENERICINST) && 
6399                 mono_metadata_generic_class_is_valuetype (type->data.generic_class) &&
6400                 !type->data.generic_class->container_class->enumtype)));
6401 }
6402
6403 /**
6404  * mono_type_is_void:
6405  * @type: the MonoType operated on
6406  *
6407  * Returns: #TRUE is @type is System.Void. #FALSE otherwise.
6408  */
6409 mono_bool
6410 mono_type_is_void (MonoType *type)
6411 {
6412         return (type && (type->type == MONO_TYPE_VOID) && !type->byref);
6413 }
6414
6415 /**
6416  * mono_type_is_pointer:
6417  * @type: the MonoType operated on
6418  *
6419  * Returns: #TRUE is @type is a managed or unmanaged pointer type. #FALSE otherwise.
6420  */
6421 mono_bool
6422 mono_type_is_pointer (MonoType *type)
6423 {
6424         return (type && ((type->byref || (type->type == MONO_TYPE_I) || type->type == MONO_TYPE_STRING)
6425                 || (type->type == MONO_TYPE_SZARRAY) || (type->type == MONO_TYPE_CLASS) ||
6426                 (type->type == MONO_TYPE_U) || (type->type == MONO_TYPE_OBJECT) ||
6427                 (type->type == MONO_TYPE_ARRAY) || (type->type == MONO_TYPE_PTR) ||
6428                 (type->type == MONO_TYPE_FNPTR)));
6429 }
6430
6431 /**
6432  * mono_type_is_reference:
6433  * @type: the MonoType operated on
6434  *
6435  * Returns: #TRUE is @type represents an object reference . #FALSE otherwise.
6436  */
6437 mono_bool
6438 mono_type_is_reference (MonoType *type)
6439 {
6440         return (type && (((type->type == MONO_TYPE_STRING) ||
6441                 (type->type == MONO_TYPE_SZARRAY) || (type->type == MONO_TYPE_CLASS) ||
6442                 (type->type == MONO_TYPE_OBJECT) || (type->type == MONO_TYPE_ARRAY)) ||
6443                 ((type->type == MONO_TYPE_GENERICINST) &&
6444                 !mono_metadata_generic_class_is_valuetype (type->data.generic_class))));
6445 }
6446
6447 /**
6448  * mono_signature_get_return_type:
6449  * @sig: the method signature inspected
6450  *
6451  * Returns: the return type of the method signature @sig
6452  */
6453 MonoType*
6454 mono_signature_get_return_type (MonoMethodSignature *sig)
6455 {
6456         return sig->ret;
6457 }
6458
6459 /**
6460  * mono_signature_get_params:
6461  * @sig: the method signature inspected
6462  * #iter: pointer to an iterator
6463  *
6464  * Iterates over the parameters for the method signature @sig.
6465  * A void* pointer must be initualized to #NULL to start the iteration
6466  * and it's address is passed to this function repeteadly until it returns
6467  * #NULL.
6468  *
6469  * Returns: the next parameter type of the method signature @sig,
6470  * #NULL when finished.
6471  */
6472 MonoType*
6473 mono_signature_get_params (MonoMethodSignature *sig, gpointer *iter)
6474 {
6475         MonoType** type;
6476         if (!iter)
6477                 return NULL;
6478         if (!*iter) {
6479                 /* start from the first */
6480                 if (sig->param_count) {
6481                         *iter = &sig->params [0];
6482                         return sig->params [0];
6483                 } else {
6484                         /* no method */
6485                         return NULL;
6486                 }
6487         }
6488         type = (MonoType **)*iter;
6489         type++;
6490         if (type < &sig->params [sig->param_count]) {
6491                 *iter = type;
6492                 return *type;
6493         }
6494         return NULL;
6495 }
6496
6497 /**
6498  * mono_signature_get_param_count:
6499  * @sig: the method signature inspected
6500  *
6501  * Returns: the number of parameters in the method signature @sig.
6502  */
6503 guint32
6504 mono_signature_get_param_count (MonoMethodSignature *sig)
6505 {
6506         return sig->param_count;
6507 }
6508
6509 /**
6510  * mono_signature_get_call_conv:
6511  * @sig: the method signature inspected
6512  *
6513  * Returns: the call convention of the method signature @sig.
6514  */
6515 guint32
6516 mono_signature_get_call_conv (MonoMethodSignature *sig)
6517 {
6518         return sig->call_convention;
6519 }
6520
6521 /**
6522  * mono_signature_vararg_start:
6523  * @sig: the method signature inspected
6524  *
6525  * Returns: the number of the first vararg parameter in the
6526  * method signature @sig. -1 if this is not a vararg signature.
6527  */
6528 int
6529 mono_signature_vararg_start (MonoMethodSignature *sig)
6530 {
6531         return sig->sentinelpos;
6532 }
6533
6534 /**
6535  * mono_signature_is_instance:
6536  * @sig: the method signature inspected
6537  *
6538  * Returns: #TRUE if this the method signature @sig has an implicit
6539  * first instance argument. #FALSE otherwise.
6540  */
6541 gboolean
6542 mono_signature_is_instance (MonoMethodSignature *sig)
6543 {
6544         return sig->hasthis;
6545 }
6546
6547 /**
6548  * mono_signature_param_is_out
6549  * @sig: the method signature inspected
6550  * @param_num: the 0-based index of the inspected parameter
6551  * 
6552  * Returns: #TRUE if the parameter is an out parameter, #FALSE
6553  * otherwise.
6554  */
6555 mono_bool
6556 mono_signature_param_is_out (MonoMethodSignature *sig, int param_num)
6557 {
6558         g_assert (param_num >= 0 && param_num < sig->param_count);
6559         return (sig->params [param_num]->attrs & PARAM_ATTRIBUTE_OUT) != 0;
6560 }
6561
6562 /**
6563  * mono_signature_explicit_this:
6564  * @sig: the method signature inspected
6565  *
6566  * Returns: #TRUE if this the method signature @sig has an explicit
6567  * instance argument. #FALSE otherwise.
6568  */
6569 gboolean
6570 mono_signature_explicit_this (MonoMethodSignature *sig)
6571 {
6572         return sig->explicit_this;
6573 }
6574
6575 /* for use with allocated memory blocks (assumes alignment is to 8 bytes) */
6576 guint
6577 mono_aligned_addr_hash (gconstpointer ptr)
6578 {
6579         return GPOINTER_TO_UINT (ptr) >> 3;
6580 }
6581
6582 /*
6583  * If @field belongs to an inflated generic class, return the corresponding field of the
6584  * generic type definition class.
6585  */
6586 MonoClassField*
6587 mono_metadata_get_corresponding_field_from_generic_type_definition (MonoClassField *field)
6588 {
6589         MonoClass *gtd;
6590         int offset;
6591
6592         if (!field->parent->generic_class)
6593                 return field;
6594
6595         gtd = field->parent->generic_class->container_class;
6596         offset = field - field->parent->fields;
6597         return gtd->fields + offset;
6598 }
6599
6600 /*
6601  * If @event belongs to an inflated generic class, return the corresponding event of the
6602  * generic type definition class.
6603  */
6604 MonoEvent*
6605 mono_metadata_get_corresponding_event_from_generic_type_definition (MonoEvent *event)
6606 {
6607         MonoClass *gtd;
6608         int offset;
6609
6610         if (!event->parent->generic_class)
6611                 return event;
6612
6613         gtd = event->parent->generic_class->container_class;
6614         offset = event - event->parent->ext->events;
6615         return gtd->ext->events + offset;
6616 }
6617
6618 /*
6619  * If @property belongs to an inflated generic class, return the corresponding property of the
6620  * generic type definition class.
6621  */
6622 MonoProperty*
6623 mono_metadata_get_corresponding_property_from_generic_type_definition (MonoProperty *property)
6624 {
6625         MonoClass *gtd;
6626         int offset;
6627
6628         if (!property->parent->generic_class)
6629                 return property;
6630
6631         gtd = property->parent->generic_class->container_class;
6632         offset = property - property->parent->ext->properties;
6633         return gtd->ext->properties + offset;
6634 }
6635
6636 MonoWrapperCaches*
6637 mono_method_get_wrapper_cache (MonoMethod *method)
6638 {
6639         if (method->is_inflated) {
6640                 MonoMethodInflated *imethod = (MonoMethodInflated *)method;
6641                 return &imethod->owner->wrapper_caches;
6642         } else {
6643                 return &method->klass->image->wrapper_caches;
6644         }
6645 }
6646
6647 // This is support for the mempool reference tracking feature in checked-build, but lives in metadata.c due to use of static variables of this file.
6648
6649 /**
6650  * mono_find_image_set_owner:
6651  *
6652  * Find the imageset, if any, which a given pointer is located in the memory of.
6653  */
6654 MonoImageSet *
6655 mono_find_image_set_owner (void *ptr)
6656 {
6657         MonoImageSet *owner = NULL;
6658         int i;
6659
6660         image_sets_lock ();
6661
6662         if (image_sets)
6663         {
6664                 for (i = 0; !owner && i < image_sets->len; ++i) {
6665                         MonoImageSet *set = (MonoImageSet *)g_ptr_array_index (image_sets, i);
6666                         if (mono_mempool_contains_addr (set->mempool, ptr))
6667                                 owner = set;
6668                 }
6669         }
6670
6671         image_sets_unlock ();
6672
6673         return owner;
6674 }