466c7c5dc21b9c97eba6b31abaddc590daaa2bc7
[mono.git] / mono / metadata / metadata.h
1
2 #ifndef __MONO_METADATA_H__
3 #define __MONO_METADATA_H__
4
5 #include <glib.h>
6
7 #include <mono/metadata/blob.h>
8 #include <mono/metadata/row-indexes.h>
9
10 #ifdef __GNUC__
11 #define MONO_ZERO_LEN_ARRAY 0
12 #else
13 #define MONO_ZERO_LEN_ARRAY 1
14 #endif
15
16 typedef enum {
17         MONO_TABLE_MODULE,
18         MONO_TABLE_TYPEREF,
19         MONO_TABLE_TYPEDEF,
20         MONO_TABLE_UNUSED1,
21         MONO_TABLE_FIELD,
22         MONO_TABLE_UNUSED2,
23         MONO_TABLE_METHOD,
24         MONO_TABLE_UNUSED3,
25         MONO_TABLE_PARAM,
26         MONO_TABLE_INTERFACEIMPL,
27         MONO_TABLE_MEMBERREF,
28         MONO_TABLE_CONSTANT,
29         MONO_TABLE_CUSTOMATTRIBUTE,
30         MONO_TABLE_FIELDMARSHAL,
31         MONO_TABLE_DECLSECURITY,
32         MONO_TABLE_CLASSLAYOUT,
33         MONO_TABLE_FIELDLAYOUT,
34         MONO_TABLE_STANDALONESIG,
35         MONO_TABLE_EVENTMAP,
36         MONO_TABLE_UNUSED4,
37         MONO_TABLE_EVENT,
38         MONO_TABLE_PROPERTYMAP,
39         MONO_TABLE_UNUSED5,
40         MONO_TABLE_PROPERTY,
41         MONO_TABLE_METHODSEMANTICS,
42         MONO_TABLE_METHODIMPL,
43         MONO_TABLE_MODULEREF,
44         MONO_TABLE_TYPESPEC,
45         MONO_TABLE_IMPLMAP,
46         MONO_TABLE_FIELDRVA,
47         MONO_TABLE_UNUSED6,
48         MONO_TABLE_UNUSED7,
49         MONO_TABLE_ASSEMBLY,
50         MONO_TABLE_ASSEMBLYPROCESSOR,
51         MONO_TABLE_ASSEMBLYOS,
52         MONO_TABLE_ASSEMBLYREF,
53         MONO_TABLE_ASSEMBLYREFPROCESSOR,
54         MONO_TABLE_ASSEMBLYREFOS,
55         MONO_TABLE_FILE,
56         MONO_TABLE_EXPORTEDTYPE,
57         MONO_TABLE_MANIFESTRESOURCE,
58         MONO_TABLE_NESTEDCLASS,
59
60 #define MONO_TABLE_LAST MONO_TABLE_NESTEDCLASS
61 } MonoMetaTableEnum;
62
63 typedef enum {
64         MONO_EXCEPTION_CLAUSE_NONE,
65         MONO_EXCEPTION_CLAUSE_FILTER,
66         MONO_EXCEPTION_CLAUSE_FINALLY,
67         MONO_EXCEPTION_CLAUSE_FAULT = 4
68 } MonoExceptionEnum;
69
70 typedef enum {
71         MONO_CALL_DEFAULT,
72         MONO_CALL_C,
73         MONO_CALL_STDCALL,
74         MONO_CALL_THISCALL,
75         MONO_CALL_FASTCALL,
76         MONO_CALL_VARARG
77 } MonoCallConvention;
78
79 typedef struct {
80         guint32  offset;
81         guint32  size;
82 } MonoStreamHeader;
83
84 typedef struct {
85         guint32   rows, row_size;
86         char     *base;
87
88         /*
89          * Tables contain up to 9 rows and the possible sizes of the
90          * fields in the documentation are 1, 2 and 4 bytes.  So we
91          * can encode in 2 bits the size.
92          *
93          * A 32 bit value can encode the resulting size
94          *
95          * The top eight bits encode the number of columns in the table.
96          * we only need 4, but 8 is aligned no shift required. 
97          */
98         guint32   size_bitfield;
99 } MonoTableInfo;
100
101 void         mono_metadata_decode_row (MonoTableInfo         *t,
102                                        int                    idx,
103                                        guint32               *res,
104                                        int                    res_size);
105
106 guint32      mono_metadata_decode_row_col (MonoTableInfo *t, 
107                                            int            idx, 
108                                            guint          col);
109
110 /*
111  * This macro is used to extract the size of the table encoded in
112  * the size_bitfield of MonoTableInfo.
113  */
114 #define mono_metadata_table_size(bitfield,table) ((((bitfield) >> ((table)*2)) & 0x3) + 1)
115 #define mono_metadata_table_count(bitfield) ((bitfield) >> 24)
116
117 typedef struct {
118         char                *raw_metadata;
119                             
120         gboolean             idx_string_wide, idx_guid_wide, idx_blob_wide;
121                             
122         MonoStreamHeader     heap_strings;
123         MonoStreamHeader     heap_us;
124         MonoStreamHeader     heap_blob;
125         MonoStreamHeader     heap_guid;
126         MonoStreamHeader     heap_tables;
127                             
128         char                *tables_base;
129
130         MonoTableInfo        tables [64];
131 } MonoMetadata;
132
133 /*
134  *
135  */
136 char          *mono_metadata_locate        (MonoMetadata *meta, int table, int idx);
137 char          *mono_metadata_locate_token  (MonoMetadata *meta, guint32 token);
138                                            
139 const char    *mono_metadata_string_heap   (MonoMetadata *meta, guint32 index);
140 const char    *mono_metadata_blob_heap     (MonoMetadata *meta, guint32 index);
141 const char    *mono_metadata_user_string   (MonoMetadata *meta, guint32 index);
142
143 guint32 mono_metadata_typedef_from_field  (MonoMetadata *meta, guint32 index);
144 guint32 mono_metadata_typedef_from_method (MonoMetadata *meta, guint32 index);
145
146 /*
147  * Functions to extract information from the Blobs
148  */
149 guint32 mono_metadata_decode_value     (const char            *ptr,
150                                         const char           **rptr);
151 guint32 mono_metadata_decode_blob_size (const char            *ptr,
152                                         const char           **rptr);
153
154 typedef struct {
155         guint32 flags;
156         guint32 try_offset;
157         guint32 try_len;
158         guint32 handler_offset;
159         guint32 handler_len;
160         guint32 token_or_filter;
161 } MonoExceptionClause;
162
163 typedef struct _MonoType MonoType;
164 typedef struct _MonoArray MonoArray;
165 typedef struct _MonoMethodSignature MonoMethodSignature;
166
167 typedef struct {
168         guchar mod;
169         guint32 token;
170 } MonoCustomMod;
171
172 typedef struct {
173         MonoType *type;
174         int num_modifiers;
175         MonoCustomMod modifiers [MONO_ZERO_LEN_ARRAY]; /* this may grow */
176 } MonoModifiedType;
177
178 struct _MonoArray {
179         MonoType *type;
180         int rank;
181         int numsizes;
182         int numlobounds;
183         int *sizes;
184         int *lobounds;
185 };
186
187 struct _MonoType {
188         guchar type; /* ElementTypeEnum */
189         guchar custom_mod; /* for PTR and SZARRAY: use data.mtype instead of data.type */
190         guchar byref; /* when included in a MonoRetType */
191         guchar constraint; /* valid when included in a local var signature */
192         union {
193                 guint32 token; /* for VALUETYPE and CLASS */
194                 MonoType *type;
195                 MonoModifiedType *mtype;
196                 MonoArray *array; /* for ARRAY */
197                 MonoMethodSignature *method;
198         } data;
199 };
200
201 typedef struct {
202         /* maybe use a union here: saves 4 bytes */
203         MonoType *type; /* NULL for VOID */
204         short param_attrs; /* 22.1.11 */
205         char typedbyref;
206         char num_modifiers;
207         MonoCustomMod modifiers [MONO_ZERO_LEN_ARRAY]; /* this may grow */
208 } MonoRetType;
209
210 /* MonoRetType is used also for params and fields */
211 typedef MonoRetType MonoParam;
212 typedef MonoRetType MonoFieldType;
213
214 struct _MonoMethodSignature {
215         char          hasthis;
216         char          explicit_this;
217         char          call_convention;
218         guint16       param_count;
219         guint16       sentinelpos;
220         MonoRetType  *ret;
221         MonoParam   **params;
222         guint32      *param_offsets;
223 };
224
225 typedef struct {
226         guint32      code_size;
227         const char  *code;
228         guint16      max_stack;
229         unsigned int num_clauses : 15;
230         /* if num_locals != 0, then the following apply: */
231         unsigned int init_locals : 1;
232         guint16      num_locals;
233         MonoType   **locals;
234         guint32     *locals_offsets;
235         MonoExceptionClause *clauses;
236 } MonoMethodHeader;
237
238 guint32     mono_metadata_parse_typedef_or_ref (MonoMetadata      *m,
239                                                 const char      *ptr,
240                                                 const char     **rptr);
241 int            mono_metadata_parse_custom_mod  (MonoMetadata      *m,
242                                                 MonoCustomMod   *dest,
243                                                 const char      *ptr,
244                                                 const char     **rptr);
245 MonoArray     *mono_metadata_parse_array       (MonoMetadata      *m,
246                                                 const char      *ptr,
247                                                 const char     **rptr);
248 void           mono_metadata_free_array        (MonoArray       *array);
249 MonoParam     *mono_metadata_parse_param       (MonoMetadata      *m,
250                                                 int              rettype,
251                                                 const char      *ptr,
252                                                 const char     **rptr);
253 void           mono_metadata_free_param        (MonoParam       *param);
254 MonoType      *mono_metadata_parse_type        (MonoMetadata      *m,
255                                                 const char      *ptr,
256                                                 const char     **rptr);
257 void           mono_metadata_free_type         (MonoType        *type);
258 int            mono_type_size                  (MonoType        *type, 
259                                                                 int             *alignment);
260
261 MonoFieldType *mono_metadata_parse_field_type  (MonoMetadata      *m,
262                                                 const char      *ptr,
263                                                 const char      **rptr);
264                                                         
265
266 MonoMethodSignature  *mono_metadata_parse_method_signature (MonoMetadata            *m,
267                                                             int                    def,
268                                                             const char            *ptr,
269                                                             const char           **rptr);
270 void                  mono_metadata_free_method_signature  (MonoMethodSignature   *method);
271
272 MonoMethodHeader *mono_metadata_parse_mh (MonoMetadata *m, const char *ptr);
273 void              mono_metadata_free_mh  (MonoMethodHeader *mh);
274
275 /*
276  * Makes a token based on a table and an index
277  */
278 #define mono_metadata_make_token(table,idx) (((table) << 24)| idx)
279
280 /*
281  * Returns the table index that this token encodes.
282  */
283 #define mono_metadata_token_table(token) ((token) >> 24)
284
285  /*
286  * Returns the index that a token refers to
287  */
288 #define mono_metadata_token_index(token) ((token & 0xffffff))
289
290
291 #define mono_metadata_token_code(token) ((token & 0xff000000))
292
293 guint32 mono_metadata_token_from_dor (guint32 dor_index);
294
295 #endif /* __MONO_METADATA_H__ */