2001-07-20 Miguel de Icaza <miguel@ximian.com>
[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/eltype.h>
8
9 typedef struct {
10         guint32  sh_offset;
11         guint32  sh_size;
12 } stream_header_t;
13
14 typedef enum {
15         META_TABLE_MODULE,
16         META_TABLE_TYPEREF,
17         META_TABLE_TYPEDEF,
18         META_TABLE_UNUSED1,
19         META_TABLE_FIELD,
20         META_TABLE_UNUSED2,
21         META_TABLE_METHOD,
22         META_TABLE_UNUSED3,
23         META_TABLE_PARAM,
24         META_TABLE_INTERFACEIMPL,
25         META_TABLE_MEMBERREF,
26         META_TABLE_CONSTANT,
27         META_TABLE_CUSTOMATTRIBUTE,
28         META_TABLE_FIELDMARSHAL,
29         META_TABLE_DECLSECURITY,
30         META_TABLE_CLASSLAYOUT,
31         META_TABLE_FIELDLAYOUT,
32         META_TABLE_STANDALONESIG,
33         META_TABLE_EVENTMAP,
34         META_TABLE_UNUSED4,
35         META_TABLE_EVENT,
36         META_TABLE_PROPERTYMAP,
37         META_TABLE_UNUSED5,
38         META_TABLE_PROPERTY,
39         META_TABLE_METHODSEMANTICS,
40         META_TABLE_METHODIMPL,
41         META_TABLE_MODULEREF,
42         META_TABLE_TYPESPEC,
43         META_TABLE_IMPLMAP,
44         META_TABLE_FIELDRVA,
45         META_TABLE_UNUSED6,
46         META_TABLE_UNUSED7,
47         META_TABLE_ASSEMBLY,
48         META_TABLE_ASSEMBLYPROCESSOR,
49         META_TABLE_ASSEMBLYOS,
50         META_TABLE_ASSEMBLYREF,
51         META_TABLE_ASSEMBLYREFPROCESSOR,
52         META_TABLE_ASSEMBLYREFOS,
53         META_TABLE_FILE,
54         META_TABLE_EXPORTEDTYPE,
55         META_TABLE_MANIFESTRESOURCE,
56         META_TABLE_NESTEDCLASS,
57
58 #define META_TABLE_LAST META_TABLE_NESTEDCLASS
59 } MetaTableEnum;
60
61 typedef struct {
62         guint32   rows, row_size;
63         char     *base;
64
65         /*
66          * Tables contain up to 9 rows and the possible sizes of the
67          * fields in the documentation are 1, 2 and 4 bytes.  So we
68          * can encode in 2 bits the size.
69          *
70          * A 32 bit value can encode the resulting size
71          *
72          * The top eight bits encode the number of columns in the table.
73          * we only need 4, but 8 is aligned no shift required. 
74          */
75         guint32   size_bitfield;
76 } metadata_tableinfo_t;
77
78 void         mono_metadata_decode_row (metadata_tableinfo_t  *t,
79                                        int                    idx,
80                                        guint32               *res,
81                                        int                    res_size);
82
83 /*
84  * This macro is used to extract the size of the table encoded in
85  * the size_bitfield of metadata_tableinfo_t.
86  */
87 #define meta_table_size(bitfield,table) ((((bitfield) >> ((table)*2)) & 0x3) + 1)
88 #define meta_table_count(bitfield) ((bitfield) >> 24)
89
90 typedef struct {
91         char                *raw_metadata;
92                             
93         gboolean             idx_string_wide, idx_guid_wide, idx_blob_wide;
94                             
95         stream_header_t      heap_strings;
96         stream_header_t      heap_us;
97         stream_header_t      heap_blob;
98         stream_header_t      heap_guid;
99         stream_header_t      heap_tables;
100                             
101         char                *tables_base;
102
103         metadata_tableinfo_t tables [64];
104 } metadata_t;
105
106 /*
107  * This enumeration is used to describe the data types in the metadata
108  * tables
109  */
110 enum {
111         MONO_MT_END,
112
113         /* Sized elements */
114         MONO_MT_UINT32,
115         MONO_MT_UINT16,
116         MONO_MT_UINT8,
117
118         /* Index into Blob heap */
119         MONO_MT_BLOB_IDX,
120
121         /* Index into String heap */
122         MONO_MT_STRING_IDX,
123
124         /* GUID index */
125         MONO_MT_GUID_IDX,
126
127         /* Pointer into a table */
128         MONO_MT_TABLE_IDX,
129
130         /* HasConstant:Parent pointer (Param, Field or Property) */
131         MONO_MT_CONST_IDX,
132
133         /* HasCustomAttribute index.  Indexes any table except CustomAttribute */
134         MONO_MT_HASCAT_IDX,
135         
136         /* CustomAttributeType encoded index */
137         MONO_MT_CAT_IDX,
138
139         /* HasDeclSecurity index: TypeDef Method or Assembly */
140         MONO_MT_HASDEC_IDX,
141
142         /* Implementation coded index: File, Export AssemblyRef */
143         MONO_MT_IMPL_IDX,
144
145         /* HasFieldMarshal coded index: Field or Param table */
146         MONO_MT_HFM_IDX,
147
148         /* MemberForwardedIndex: Field or Method */
149         MONO_MT_MF_IDX,
150
151         /* TypeDefOrRef coded index: typedef, typeref, typespec */
152         MONO_MT_TDOR_IDX,
153
154         /* MemberRefParent coded index: typeref, moduleref, method, memberref, typesepc, typedef */
155         MONO_MT_MRP_IDX,
156
157         /* MethodDefOrRef coded index: Method or Member Ref table */
158         MONO_MT_MDOR_IDX,
159
160         /* HasSemantic coded index: Event or Property */
161         MONO_MT_HS_IDX,
162
163         /* ResolutionScope coded index: Module, ModuleRef, AssemblytRef, TypeRef */
164         MONO_MT_RS_IDX
165 };
166
167 typedef struct {
168         int   code;
169         char *def;
170 } MonoMetaTable;
171
172 const char *mono_meta_table_name (int table);
173
174 /* Internal functions */
175 void           mono_metadata_compute_table_bases (metadata_t *meta);
176
177 MonoMetaTable *mono_metadata_get_table    (MetaTableEnum table);
178
179 /*
180  *
181  */
182 char          *mono_metadata_locate        (metadata_t *meta, int table, int idx);
183 char          *mono_metadata_locate_token  (metadata_t *meta, guint32 token);
184                                            
185 const char    *mono_metadata_string_heap   (metadata_t *meta, guint32 index);
186 const char    *mono_metadata_blob_heap     (metadata_t *meta, guint32 index);
187 const char    *mono_metadata_user_string   (metadata_t *meta, guint32 index);
188
189 /*
190  * Functions to extract information from the Blobs
191  */
192 const char  *mono_metadata_decode_value     (const char            *ptr,
193                                              guint32               *len);
194 const char  *mono_metadata_decode_blob_size (const char            *xptr,
195                                              int                   *size);
196
197 typedef enum {
198         MONO_META_EXCEPTION_CLAUSE_NONE,
199         MONO_META_EXCEPTION_CLAUSE_FILTER,
200         MONO_META_EXCEPTION_CLAUSE_FINALLY,
201         MONO_META_EXCEPTION_CLAUSE_FAULT
202 } MonoMetaExceptionEnum;
203
204 typedef enum {
205         MONO_CALL_DEFAULT,
206         MONO_CALL_C,
207         MONO_CALL_STDCALL,
208         MONO_CALL_THISCALL,
209         MONO_CALL_FASTCALL,
210         MONO_CALL_VARARG
211 } MonoCallConvention;
212
213 typedef struct {
214         MonoMetaExceptionEnum kind;
215         int n_clauses;
216         void **clauses;
217 } MonoMetaExceptionHandler;
218
219 typedef struct _MonoType MonoType;
220 typedef struct _MonoArray MonoArray;
221 typedef struct _MonoMethodSignature MonoMethodSignature;
222
223 typedef struct {
224         guchar mod;
225         guint32 token;
226 } MonoCustomMod;
227
228 typedef struct {
229         MonoType *type;
230         int num_modifiers;
231         MonoCustomMod modifiers[1]; /* this may grow */
232 } MonoModifiedType;
233
234 struct _MonoArray {
235         MonoType *type;
236         int rank;
237         int numsizes;
238         int numlobounds;
239         int *sizes;
240         int *lobounds;
241 };
242
243 struct _MonoType {
244         guchar type; /* ElementTypeEnum */
245         guchar custom_mod; /* for PTR and SZARRAY: use data.mtype instead of data.type */
246         guchar byref; /* when included in a MonoRetType */
247         guchar constraint; /* valid when included in a local var signature */
248         union {
249                 guint32 token; /* for VALUETYPE and CLASS */
250                 MonoType *type;
251                 MonoModifiedType *mtype;
252                 MonoArray *array; /* for ARRAY */
253                 MonoMethodSignature *method;
254         } data;
255 };
256
257 typedef struct {
258         /* maybe use a union here: saves 4 bytes */
259         MonoType *type; /* NULL for VOID */
260         short param_attrs; /* 22.1.11 */
261         char typedbyref;
262         int num_modifiers;
263         MonoCustomMod modifiers[1]; /* this may grow */
264 } MonoRetType;
265
266 /* MonoRetType is used also for params */
267 typedef MonoRetType MonoParam;
268
269 struct _MonoMethodSignature {
270         char hasthis;
271         char explicit_this;
272         char call_convention;
273         int param_count;
274         int sentinelpos;
275         MonoRetType *ret;
276         MonoParam **params;
277 };
278
279 typedef struct {
280         guint32     code_size;
281         const char *code;
282         short       max_stack;
283         guint32     local_var_sig_tok;
284
285         /* if local_var_sig_tok != 0, then the following apply: */
286         unsigned int init_locals : 1;
287         int         num_locals;
288         MonoType  **locals;
289
290         GList      *exception_handler_list;
291 } MonoMetaMethodHeader;
292
293 guint32     mono_metadata_parse_typedef_or_ref (metadata_t      *m,
294                                                 const char      *ptr,
295                                                 const char     **rptr);
296 int         mono_metadata_parse_custom_mod     (metadata_t      *m,
297                                                 MonoCustomMod   *dest,
298                                                 const char      *ptr,
299                                                 const char     **rptr);
300 MonoArray  *mono_metadata_parse_array          (metadata_t      *m,
301                                                 const char      *ptr,
302                                                 const char     **rptr);
303 void        mono_metadata_free_array           (MonoArray       *array);
304 MonoParam  *mono_metadata_parse_param          (metadata_t      *m,
305                                                 int              rettype,
306                                                 const char      *ptr,
307                                                 const char     **rptr);
308 void        mono_metadata_free_param           (MonoParam       *param);
309 MonoType   *mono_metadata_parse_type           (metadata_t      *m,
310                                                 const char      *ptr,
311                                                 const char     **rptr);
312 void        mono_metadata_free_type            (MonoType        *type);
313
314 MonoMethodSignature  *mono_metadata_parse_method_signature (metadata_t            *m,
315                                                             int                    def,
316                                                             const char            *ptr,
317                                                             const char           **rptr);
318 void                  mono_metadata_free_method_signature  (MonoMethodSignature   *method);
319
320 MonoMetaMethodHeader *mono_metadata_parse_mh (metadata_t *m, const char *ptr);
321 void                  mono_metadata_free_mh  (MonoMetaMethodHeader *mh);
322
323 /*
324  * Makes a token based on a table and an index
325  */
326 #define mono_metadata_make_token(table,idx) (((table) << 24)| idx)
327
328 /*
329  * Returns the table index that this token encodes.
330  */
331 #define mono_metadata_token_table(token) ((token) >> 24)
332
333 /*
334  * Returns the index that a token refers to
335  */
336 #define mono_metadata_token_index(token) ((token & 0xffffff))
337
338
339 #define mono_metadata_token_code(token) ((token & 0xff000000))
340
341 /*
342  * FIXME: put all of the table codes here
343  */
344 enum {
345         TOKEN_TABLE_XXX = 0
346 } MonoMetadataTableCodes;
347
348 #endif /* __MONO_METADATA_H__ */