2002-04-25 Nick Drochak <ndrochak@gol.com>
[mono.git] / mono / metadata / metadata.h
index 1215556454a88d7857b081fb29743cb0d42f87d8..34e90afd1dc5f39245533e1e7822a54c68e94a5c 100644 (file)
 
-typedef struct {
-       guint32  sh_offset;
-       guint32  sh_size;
-} stream_header_t;
-
-typedef enum {
-       META_TABLE_MODULE,
-       META_TABLE_TYPEREF,
-       META_TABLE_TYPEDEF,
-       META_TABLE_UNUSED1,
-       META_TABLE_FIELD,
-       META_TABLE_UNUSED2,
-       META_TABLE_METHOD,
-       META_TABLE_UNUSED3,
-       META_TABLE_PARAM,
-       META_TABLE_INTERFACEIMPL,
-       META_TABLE_MEMBERREF,
-       META_TABLE_CONSTANT,
-       META_TABLE_CUSTOMATTRIBUTE,
-       META_TABLE_FIELDMARSHAL,
-       META_TABLE_DECLSECURITY,
-       META_TABLE_CLASSLAYOUT,
-       META_TABLE_FIELDLAYOUT,
-       META_TABLE_STANDALONESIG,
-       META_TABLE_EVENTMAP,
-       META_TABLE_UNUSED4,
-       META_TABLE_EVENT,
-       META_TABLE_PROPERTYMAP,
-       META_TABLE_UNUSED5,
-       META_TABLE_PROPERTY,
-       META_TABLE_METHODSEMANTICS,
-       META_TABLE_METHODIMPL,
-       META_TABLE_MODULEREF,
-       META_TABLE_TYPESPEC,
-       META_TABLE_IMPLMAP,
-       META_TABLE_FIELDRVA,
-       META_TABLE_UNUSED6,
-       META_TABLE_UNUSED7,
-       META_TABLE_ASSEMBLY,
-       META_TABLE_ASSEMBLYPROCESSOR,
-       META_TABLE_ASSEMBLYOS,
-       META_TABLE_ASSEMBLYREF,
-       META_TABLE_ASSEMBLYREFPROCESSOR,
-       META_TABLE_ASSEMBLYREFOS,
-       META_TABLE_FILE,
-       META_TABLE_EXPORTEDTYPE,
-       META_TABLE_MANIFESTRESOURCE,
-       META_TABLE_NESTEDCLASS,
-
-#define META_TABLE_LAST META_TABLE_NESTEDCLASS
-} MetaTableEnum;
-
-typedef struct {
-       guint32   rows, row_size;
-       char     *base;
-
-       /*
-        * Tables contain up to 9 rows and the possible sizes of the
-        * fields in the documentation are 1, 2 and 4 bytes.  So we
-        * can encode in 2 bits the size.
-        *
-        * A 32 bit value can encode the resulting size
-        *
-        * The top eight bits encode the number of columns in the table.
-        * we only need 4, but 8 is aligned no shift required. 
-        */
-       guint32   size_bitfield;
-} metadata_tableinfo_t;
-
-/*
- * This macro is used to extract the size of the table encoded in
- * the size_bitfield of metadata_tableinfo_t.
- */
-#define meta_table_size(bitfield,table) ((((bitfield) >> ((table)*2)) & 0x3) + 1)
-#define meta_table_count(bitfield) ((bitfield) >> 24)
-
-typedef struct {
-       char                *raw_metadata;
-                           
-       gboolean             idx_string_wide, idx_guid_wide, idx_blob_wide;
-                           
-       stream_header_t      heap_strings;
-       stream_header_t      heap_us;
-       stream_header_t      heap_blob;
-       stream_header_t      heap_guid;
-       stream_header_t      heap_tables;
-                           
-       char                *tables_base;
-
-       metadata_tableinfo_t tables [64];
-} metadata_t;
-
-/*
- * This enumeration is used to describe the data types in the metadata
- * tables
- */
-enum {
-       MONO_MT_END,
-
-       /* Sized elements */
-       MONO_MT_UINT32,
-       MONO_MT_UINT16,
-       MONO_MT_UINT8,
-
-       /* Index into Blob heap */
-       MONO_MT_BLOB_IDX,
-
-       /* Index into String heap */
-       MONO_MT_STRING_IDX,
-
-       /* GUID index */
-       MONO_MT_GUID_IDX,
-
-       /* Pointer into a table */
-       MONO_MT_TABLE_IDX,
-
-       /* HasConstant:Parent pointer (Param, Field or Property) */
-       MONO_MT_CONST_IDX,
-
-       /* HasCustomAttribute index.  Indexes any table except CustomAttribute */
-       MONO_MT_HASCAT_IDX,
-       
-       /* CustomAttributeType encoded index */
-       MONO_MT_CAT_IDX,
-
-       /* HasDeclSecurity index: TypeDef Method or Assembly */
-       MONO_MT_HASDEC_IDX,
-
-       /* Implementation coded index: File, Export AssemblyRef */
-       MONO_MT_IMPL_IDX,
-
-       /* HasFieldMarshal coded index: Field or Param table */
-       MONO_MT_HFM_IDX,
-
-       /* MemberForwardedIndex: Field or Method */
-       MONO_MT_MF_IDX,
+#ifndef __MONO_METADATA_H__
+#define __MONO_METADATA_H__
 
-       /* TypeDefOrRef coded index: typedef, typeref, typespec */
-       MONO_MT_TDOR_IDX,
+#include <glib.h>
 
-       /* MemberRefParent coded index: typeref, moduleref, method, memberref, typesepc, typedef */
-       MONO_MT_MRP_IDX,
+#include <mono/metadata/blob.h>
+#include <mono/metadata/row-indexes.h>
+#include <mono/metadata/image.h>
 
-       /* MethodDefOrRef coded index: Method or Member Ref table */
-       MONO_MT_MDOR_IDX,
+#ifdef __GNUC__
+#define MONO_ZERO_LEN_ARRAY 0
+#else
+#define MONO_ZERO_LEN_ARRAY 1
+#endif
 
-       /* HasSemantic coded index: Event or Property */
-       MONO_MT_HS_IDX,
+typedef struct _MonoClass MonoClass;
+typedef struct _MonoDomain MonoDomain;
 
-       /* ResolutionScope coded index: Module, ModuleRef, AssemblytRef, TypeRef */
-       MONO_MT_RS_IDX
-};
+typedef enum {
+       MONO_TABLE_MODULE,
+       MONO_TABLE_TYPEREF,
+       MONO_TABLE_TYPEDEF,
+       MONO_TABLE_UNUSED1,
+       MONO_TABLE_FIELD,
+       MONO_TABLE_UNUSED2,
+       MONO_TABLE_METHOD,
+       MONO_TABLE_UNUSED3,
+       MONO_TABLE_PARAM,
+       MONO_TABLE_INTERFACEIMPL,
+       MONO_TABLE_MEMBERREF,
+       MONO_TABLE_CONSTANT,
+       MONO_TABLE_CUSTOMATTRIBUTE,
+       MONO_TABLE_FIELDMARSHAL,
+       MONO_TABLE_DECLSECURITY,
+       MONO_TABLE_CLASSLAYOUT,
+       MONO_TABLE_FIELDLAYOUT,
+       MONO_TABLE_STANDALONESIG,
+       MONO_TABLE_EVENTMAP,
+       MONO_TABLE_UNUSED4,
+       MONO_TABLE_EVENT,
+       MONO_TABLE_PROPERTYMAP,
+       MONO_TABLE_UNUSED5,
+       MONO_TABLE_PROPERTY,
+       MONO_TABLE_METHODSEMANTICS,
+       MONO_TABLE_METHODIMPL,
+       MONO_TABLE_MODULEREF,
+       MONO_TABLE_TYPESPEC,
+       MONO_TABLE_IMPLMAP,
+       MONO_TABLE_FIELDRVA,
+       MONO_TABLE_UNUSED6,
+       MONO_TABLE_UNUSED7,
+       MONO_TABLE_ASSEMBLY,
+       MONO_TABLE_ASSEMBLYPROCESSOR,
+       MONO_TABLE_ASSEMBLYOS,
+       MONO_TABLE_ASSEMBLYREF,
+       MONO_TABLE_ASSEMBLYREFPROCESSOR,
+       MONO_TABLE_ASSEMBLYREFOS,
+       MONO_TABLE_FILE,
+       MONO_TABLE_EXPORTEDTYPE,
+       MONO_TABLE_MANIFESTRESOURCE,
+       MONO_TABLE_NESTEDCLASS
+
+#define MONO_TABLE_LAST MONO_TABLE_NESTEDCLASS
+} MonoMetaTableEnum;
 
-typedef struct {
-       int   code;
-       char *def;
-} MonoMetaTable;
+typedef enum {
+       MONO_EXCEPTION_CLAUSE_NONE,
+       MONO_EXCEPTION_CLAUSE_FILTER,
+       MONO_EXCEPTION_CLAUSE_FINALLY,
+       MONO_EXCEPTION_CLAUSE_FAULT = 4
+} MonoExceptionEnum;
 
-const char *mono_meta_table_name (int table);
+typedef enum {
+       MONO_CALL_DEFAULT,
+       MONO_CALL_C,
+       MONO_CALL_STDCALL,
+       MONO_CALL_THISCALL,
+       MONO_CALL_FASTCALL,
+       MONO_CALL_VARARG
+} MonoCallConvention;
+
+void         mono_metadata_decode_row (MonoTableInfo         *t,
+                                      int                    idx,
+                                      guint32               *res,
+                                      int                    res_size);
+
+guint32      mono_metadata_decode_row_col (MonoTableInfo *t, 
+                                          int            idx, 
+                                          guint          col);
 
-/* Internal functions */
-void           mono_metadata_compute_table_bases (metadata_t *meta);
+/*
+ * This macro is used to extract the size of the table encoded in
+ * the size_bitfield of MonoTableInfo.
+ */
+#define mono_metadata_table_size(bitfield,table) ((((bitfield) >> ((table)*2)) & 0x3) + 1)
+#define mono_metadata_table_count(bitfield) ((bitfield) >> 24)
 
-MonoMetaTable *mono_metadata_get_table    (MetaTableEnum table);
+int mono_metadata_compute_size (MonoImage   *meta,
+                                int             tableindex,
+                                guint32        *result_bitfield);
 
 /*
  *
  */
-char          *mono_metadata_locate       (metadata_t *meta, int table, int idx);
-char          *mono_metadata_locate_token (metadata_t *meta, guint32 token);
+const char    *mono_metadata_locate        (MonoImage *meta, int table, int idx);
+const char    *mono_metadata_locate_token  (MonoImage *meta, guint32 token);
+                                          
+const char    *mono_metadata_string_heap   (MonoImage *meta, guint32 table_index);
+const char    *mono_metadata_blob_heap     (MonoImage *meta, guint32 table_index);
+const char    *mono_metadata_user_string   (MonoImage *meta, guint32 table_index);
+const char    *mono_metadata_guid_heap     (MonoImage *meta, guint32 table_index);
+
+guint32 mono_metadata_typedef_from_field  (MonoImage *meta, guint32 table_index);
+guint32 mono_metadata_typedef_from_method (MonoImage *meta, guint32 table_index);
+guint32 mono_metadata_nested_in_typedef   (MonoImage *meta, guint32 table_index);
+guint32 mono_metadata_nesting_typedef     (MonoImage *meta, guint32 table_index);
+
+MonoClass** mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 table_index, guint *count);
+
+guint32     mono_metadata_events_from_typedef     (MonoImage *meta, guint32 table_index, guint *end_idx);
+guint32     mono_metadata_methods_from_event      (MonoImage *meta, guint32 table_index, guint *end);
+guint32     mono_metadata_properties_from_typedef (MonoImage *meta, guint32 table_index, guint *end);
+guint32     mono_metadata_methods_from_property   (MonoImage *meta, guint32 table_index, guint *end);
+guint32     mono_metadata_packing_from_typedef    (MonoImage *meta, guint32 table_index, guint32 *packing, guint32 *size);
+
+void        mono_metadata_field_info (MonoImage *meta, 
+                                     guint32       table_index,
+                                     guint32      *offset,
+                                     const char  **rva,
+                                     const char  **marshal_info);
+
+guint32     mono_metadata_get_constant_index (MonoImage *meta, guint32 token);
 
-const char    *mono_metadata_string_heap  (metadata_t *meta, guint32 index);
-const char    *mono_metadata_blob_heap    (metadata_t *meta, guint32 index);
-const char    *mono_metadata_user_string  (metadata_t *meta, guint32 index);
+/*
+ * Functions to extract information from the Blobs
+ */
+guint32 mono_metadata_decode_value     (const char            *ptr,
+                                        const char           **rptr);
+guint32 mono_metadata_decode_blob_size (const char            *ptr,
+                                        const char           **rptr);
 
-typedef enum {
-       MONO_META_EXCEPTION_CLAUSE_NONE,
-       MONO_META_EXCEPTION_CLAUSE_FILTER,
-       MONO_META_EXCEPTION_CLAUSE_FINALLY,
-       MONO_META_EXCEPTION_CLAUSE_FAULT
-} MonoMetaExceptionEnum;
+void mono_metadata_encode_value (guint32 value, char *bug, char **endbuf);
 
+#define MONO_OFFSET_IN_CLAUSE(clause,offset) \
+       ((clause)->try_offset <= (offset) && (offset) < ((clause)->try_offset + (clause)->try_len))
+#define MONO_OFFSET_IN_HANDLER(clause,offset) \
+       ((clause)->handler_offset <= (offset) && (offset) < ((clause)->handler_offset + (clause)->handler_len))
 
 typedef struct {
-       MonoMetaExceptionEnum kind;
-       int n_clauses;
-       void **clauses;
-} MonoMetaExceptionHandler;
+       guint32 flags;
+       guint32 try_offset;
+       guint32 try_len;
+       guint32 handler_offset;
+       guint32 handler_len;
+       guint32 token_or_filter;
+} MonoExceptionClause;
+
+typedef struct _MonoType MonoType;
+typedef struct _MonoArrayType MonoArrayType;
+typedef struct _MonoMethodSignature MonoMethodSignature;
 
 typedef struct {
-       guint32     code_size;
-       const char *code;
-       short       max_stack;
-       guint32     local_var_sig_tok;
+       unsigned int required : 1;
+       unsigned int token    : 31;
+} MonoCustomMod;
+
+struct _MonoArrayType {
+       MonoType *type;
+       int rank;
+       int numsizes;
+       int numlobounds;
+       int *sizes;
+       int *lobounds;
+};
 
-       /* if local_var_sig_tok != 0, then the following apply: */
-       unsigned int init_locals : 1;
+struct _MonoType {
+       union {
+               MonoClass *klass; /* for VALUETYPE and CLASS */
+               MonoType *type;   /* for PTR and SZARRAY */
+               MonoArrayType *array; /* for ARRAY */
+               MonoMethodSignature *method;
+       } data;
+       unsigned int attrs    : 16; /* param attributes or field flags */
+       unsigned int type     : 8;  /* ElementTypeEnum */
+       unsigned int num_mods : 6;  /* max 64 modifiers follow at the end */
+       unsigned int byref    : 1;
+       unsigned int pinned   : 1;  /* valid when included in a local var signature */
+       MonoCustomMod modifiers [MONO_ZERO_LEN_ARRAY]; /* this may grow */
+};
+
+struct _MonoMethodSignature {
+       unsigned int  hasthis : 1;
+       unsigned int  explicit_this   : 1;
+       unsigned int  call_convention : 6;
+       unsigned int  ref_count : 24;
+       guint16       param_count;
+       guint16       sentinelpos;
+       MonoType     *ret;
+       MonoType     *params [MONO_ZERO_LEN_ARRAY];
+};
 
-       GList      *exception_handler_list;
-} MonoMetaMethodHeader;
+typedef struct {
+       guint32      code_size;
+       const unsigned char  *code;
+       guint16      max_stack;
+       unsigned int num_clauses : 15;
+       /* if num_locals != 0, then the following apply: */
+       unsigned int init_locals : 1;
+       guint16      num_locals;
+       MonoExceptionClause *clauses;
+       MonoType    *locals [MONO_ZERO_LEN_ARRAY];
+} MonoMethodHeader;
 
-MonoMetaMethodHeader *mono_metadata_parse_mh (const char *ptr);
-void                  mono_metadata_free_mh  (MonoMetaMethodHeader *mh);
+typedef enum {
+       MONO_PARSE_TYPE,
+       MONO_PARSE_MOD_TYPE,
+       MONO_PARSE_LOCAL,
+       MONO_PARSE_PARAM,
+       MONO_PARSE_RET,
+       MONO_PARSE_FIELD
+} MonoParseTypeMode;
+
+guint32     mono_metadata_parse_typedef_or_ref (MonoImage      *m,
+                                                const char      *ptr,
+                                                const char     **rptr);
+int            mono_metadata_parse_custom_mod  (MonoImage      *m,
+                                               MonoCustomMod   *dest,
+                                               const char      *ptr,
+                                               const char     **rptr);
+MonoArrayType *mono_metadata_parse_array       (MonoImage      *m,
+                                               const char      *ptr,
+                                               const char     **rptr);
+void           mono_metadata_free_array        (MonoArrayType     *array);
+MonoType      *mono_metadata_parse_type        (MonoImage      *m,
+                                               MonoParseTypeMode  mode,
+                                               short              opt_attrs,
+                                               const char        *ptr,
+                                               const char       **rptr);
+MonoType      *mono_metadata_parse_param       (MonoImage      *m,
+                                               const char      *ptr,
+                                               const char      **rptr);
+MonoType      *mono_metadata_parse_ret_type    (MonoImage      *m,
+                                               const char      *ptr,
+                                               const char      **rptr);
+MonoType      *mono_metadata_parse_field_type  (MonoImage      *m,
+                                               short            field_flags,
+                                               const char      *ptr,
+                                               const char      **rptr);
+MonoType      *mono_type_create_from_typespec (MonoImage        *image, 
+                                              guint32           type_spec);
+void           mono_metadata_free_type         (MonoType        *type);
+int            mono_type_size                  (MonoType        *type, 
+                                               int             *alignment);
+int            mono_type_stack_size            (MonoType        *type, 
+                                               int             *alignment);
+
+guint          mono_metadata_type_hash         (MonoType *t1);
+gboolean       mono_metadata_type_equal        (MonoType *t1, MonoType *t2);
+
+MonoMethodSignature  *mono_metadata_parse_method_signature (MonoImage            *m,
+                                                            int                    def,
+                                                            const char            *ptr,
+                                                            const char           **rptr);
+void                  mono_metadata_free_method_signature  (MonoMethodSignature   *method);
+
+gboolean          mono_metadata_signature_equal (MonoMethodSignature *sig1, 
+                                                MonoMethodSignature *sig2);
+
+MonoMethodHeader *mono_metadata_parse_mh (MonoImage *m, const char *ptr);
+void              mono_metadata_free_mh  (MonoMethodHeader *mh);
 
 /*
  * Makes a token based on a table and an index
@@ -213,15 +280,14 @@ void                  mono_metadata_free_mh  (MonoMetaMethodHeader *mh);
  */
 #define mono_metadata_token_table(token) ((token) >> 24)
 
-/*
+ /*
  * Returns the index that a token refers to
  */
 #define mono_metadata_token_index(token) ((token & 0xffffff))
 
 
-/*
- * FIXME: put all of the table codes here
- */
-enum {
-       TOKEN_TABLE_XXX = 0
-} MonoMetadataTableCodes;
+#define mono_metadata_token_code(token) ((token & 0xff000000))
+
+guint32 mono_metadata_token_from_dor (guint32 dor_index);
+
+#endif /* __MONO_METADATA_H__ */