Fri Mar 29 18:09:08 CET 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / metadata / class.c
index 9c92743d73951709f9afb7751dd98733a85031ba..363f1c11877ffd058fb9ed8f7ae14cd21c53a847 100644 (file)
@@ -28,6 +28,9 @@
 #include <mono/metadata/object.h>
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/mono-endian.h>
+#if HAVE_BOEHM_GC
+#include <gc/gc.h>
+#endif
 
 #define CSIZE(x) (sizeof (x) / 4)
 
@@ -41,6 +44,13 @@ default_trampoline (MonoMethod *method)
        return method;
 }
 
+static gpointer
+default_remoting_trampoline (MonoMethod *method)
+{
+       g_error ("remoting not installed");
+       return NULL;
+}
+
 static void
 default_runtime_class_init (MonoClass *klass)
 {
@@ -48,6 +58,7 @@ default_runtime_class_init (MonoClass *klass)
 }
 
 static MonoTrampoline arch_create_jit_trampoline = default_trampoline;
+static MonoTrampoline arch_create_remoting_trampoline = default_remoting_trampoline;
 static MonoRuntimeClassInit mono_runtime_class_init = default_runtime_class_init;
 
 void
@@ -56,6 +67,12 @@ mono_install_trampoline (MonoTrampoline func)
        arch_create_jit_trampoline = func? func: default_trampoline;
 }
 
+void
+mono_install_remoting_trampoline (MonoTrampoline func) 
+{
+       arch_create_remoting_trampoline = func? func: default_remoting_trampoline;
+}
+
 void
 mono_install_runtime_class_init (MonoRuntimeClassInit func)
 {
@@ -150,35 +167,33 @@ class_compute_field_layout (MonoClass *class)
                        G_BREAKPOINT ();
        }
        /*
-        * Compute field layout and total size.
+        * Compute field layout and total size (not considering static fields)
         */
-       switch (layout){
+       switch (layout) {
        case TYPE_ATTRIBUTE_AUTO_LAYOUT:
        case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
                for (i = 0; i < top; i++){
                        int size, align;
                        
+                       if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
+                               continue;
+
                        size = mono_type_size (class->fields [i].type, &align);
-                       if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC) {
-                               class->fields [i].offset = class->class_size;
-                               class->fields [i].offset += align - 1;
-                               class->fields [i].offset &= ~(align - 1);
-                               class->class_size = class->fields [i].offset + size;
-                       } else {
-                               class->min_align = MAX (align, class->min_align);
-                               class->fields [i].offset = class->instance_size;
-                               class->fields [i].offset += align - 1;
-                               class->fields [i].offset &= ~(align - 1);
-                               class->instance_size = class->fields [i].offset + size;
-                       }
+
+                       class->min_align = MAX (align, class->min_align);
+                       class->fields [i].offset = class->instance_size;
+                       class->fields [i].offset += align - 1;
+                       class->fields [i].offset &= ~(align - 1);
+                       class->instance_size = class->fields [i].offset + size;
                }
+       
                if (class->instance_size & (class->min_align - 1)) {
                        class->instance_size += class->min_align - 1;
                        class->instance_size &= ~(class->min_align - 1);
                }
                break;
        case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
-               for (i = 0; i < top; i++){
+               for (i = 0; i < top; i++) {
                        int size, align;
                        int idx = class->field.first + i;
 
@@ -187,27 +202,67 @@ class_compute_field_layout (MonoClass *class)
                         * uses explicit layout.
                         */
 
+                       if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC)
+                               continue;
+
                        size = mono_type_size (class->fields [i].type, &align);
-                       if (class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC) {
-                               class->fields [i].offset = class->class_size;
-                               class->fields [i].offset += align - 1;
-                               class->fields [i].offset &= ~(align - 1);
-                               class->class_size = class->fields [i].offset + size;
-                       } else {
-                               mono_metadata_field_info (m, idx, &class->fields [i].offset, NULL, NULL);
-                               if (class->fields [i].offset == (guint32)-1)
-                                               g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, class->fields [i].name);
-                               /*
-                                * The offset is from the start of the object: this works for both
-                                * classes and valuetypes.
-                                */
-                               class->fields [i].offset += sizeof (MonoObject);
-                               /*
-                                * Calc max size.
-                                */
-                               size += class->fields [i].offset;
-                               class->instance_size = MAX (class->instance_size, size);
-                       }
+                       
+                       mono_metadata_field_info (m, idx, &class->fields [i].offset, NULL, NULL);
+                       if (class->fields [i].offset == (guint32)-1)
+                               g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, class->fields [i].name);
+                       /*
+                        * The offset is from the start of the object: this works for both
+                        * classes and valuetypes.
+                        */
+                       class->fields [i].offset += sizeof (MonoObject);
+                       /*
+                        * Calc max size.
+                        */
+                       size += class->fields [i].offset;
+                       class->instance_size = MAX (class->instance_size, size);
+               }
+               break;
+       }
+
+       class->size_inited = 1;
+
+       /*
+        * Compute static field layout and size
+        */
+       switch (layout) {
+       case TYPE_ATTRIBUTE_AUTO_LAYOUT:
+       case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
+               for (i = 0; i < top; i++){
+                       int size, align;
+                       
+                       if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
+                               continue;
+                       
+                       size = mono_type_size (class->fields [i].type, &align);
+                       class->fields [i].offset = class->class_size;
+                       class->fields [i].offset += align - 1;
+                       class->fields [i].offset &= ~(align - 1);
+                       class->class_size = class->fields [i].offset + size;
+               }
+               break;
+       case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
+               for (i = 0; i < top; i++){
+                       int size, align;
+
+                       /*
+                        * There must be info about all the fields in a type if it
+                        * uses explicit layout.
+                        */
+
+                       
+                       if (!(class->fields [i].type->attrs & FIELD_ATTRIBUTE_STATIC))
+                               continue;
+
+                       size = mono_type_size (class->fields [i].type, &align);
+                       class->fields [i].offset = class->class_size;
+                       class->fields [i].offset += align - 1;
+                       class->fields [i].offset &= ~(align - 1);
+                       class->class_size = class->fields [i].offset + size;
                }
                break;
        }
@@ -234,10 +289,10 @@ init_properties (MonoClass *class)
                for (j = startm; j < endm; ++j) {
                        mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
                        switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
-                       case 1: /* set */
+                       case METHOD_SEMANTIC_SETTER:
                                class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
                                break;
-                       case 2: /* get */
+                       case METHOD_SEMANTIC_GETTER:
                                class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
                                break;
                        default:
@@ -247,6 +302,44 @@ init_properties (MonoClass *class)
        }
 }
 
+static void
+init_events (MonoClass *class)
+{
+       guint startm, endm, i, j;
+       guint32 cols [MONO_EVENT_SIZE];
+       MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
+       MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
+
+       class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
+       class->event.count = class->event.last - class->event.first;
+
+       class->events = g_new0 (MonoEvent, class->event.count);
+       for (i = class->event.first; i < class->event.last; ++i) {
+               mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
+               class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
+               class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
+
+               startm = mono_metadata_methods_from_event (class->image, i, &endm);
+               for (j = startm; j < endm; ++j) {
+                       mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
+                       switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
+                       case METHOD_SEMANTIC_ADD_ON:
+                               class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
+                               break;
+                       case METHOD_SEMANTIC_REMOVE_ON:
+                               class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
+                               break;
+                       case METHOD_SEMANTIC_FIRE:
+                               class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
+                               break;
+                       case METHOD_SEMANTIC_OTHER: /* don't care for now */
+                       default:
+                               break;
+                       }
+               }
+       }
+}
+
 static guint
 mono_get_unique_iid (MonoClass *class)
 {
@@ -271,7 +364,6 @@ mono_get_unique_iid (MonoClass *class)
                ++iid;
        }
 
-       g_free (str);
        return iid - 1;
 }
 
@@ -290,12 +382,20 @@ mono_class_init (MonoClass *class)
        MonoClass *k, *ic;
        MonoMethod **vtable = class->vtable;
        int i, max_iid, cur_slot = 0;
+       static MonoMethod *default_ghc = NULL;
+       static int ghc_slot = -1;
 
        g_assert (class);
 
        if (class->inited)
                return;
-       class->inited = 1;
+
+       if (class->init_pending) {
+               /* this indicates a cyclic dependency */
+               g_error ("pending init %s.%s\n", class->name_space, class->name);
+       }
+
+       class->init_pending = 1;
 
        if (class->parent) {
                if (!class->parent->inited)
@@ -310,7 +410,7 @@ mono_class_init (MonoClass *class)
        /*
         * Computes the size used by the fields, and their locations
         */
-       if (class->field.count > 0){
+       if (!class->size_inited && class->field.count > 0){
                class->fields = g_new0 (MonoClassField, class->field.count);
                class_compute_field_layout (class);
        }
@@ -324,6 +424,8 @@ mono_class_init (MonoClass *class)
        if (class->flags & TYPE_ATTRIBUTE_INTERFACE) {
                for (i = 0; i < class->method.count; ++i)
                        class->methods [i]->slot = i;
+               class->init_pending = 0;
+               class->inited = 1;
                return;
        }
 
@@ -438,11 +540,13 @@ mono_class_init (MonoClass *class)
 
                        for (l = 0; l < ic->method.count; l++) {
                                MonoMethod *im = ic->methods [l];                                               
-                               char *qname;
+                               char *qname, *fqname;
+                               
+                               qname = g_strconcat (ic->name, ".", im->name, NULL); 
                                if (ic->name_space && ic->name_space [0])
-                                       qname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
+                                       fqname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
                                else
-                                       qname = g_strconcat (ic->name, ".", im->name, NULL); 
+                                       fqname = NULL;
 
                                for (j = 0; j < class->method.count; ++j) {
                                        MonoMethod *cm = class->methods [j];
@@ -450,7 +554,7 @@ mono_class_init (MonoClass *class)
                                        if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
                                                continue;
                                        
-                                       if (!strcmp (cm->name, qname) &&
+                                       if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
                                            mono_metadata_signature_equal (cm->signature, im->signature)) {
                                                g_assert (io + l <= class->vtable_size);
                                                vtable [io + l] = cm;
@@ -458,6 +562,7 @@ mono_class_init (MonoClass *class)
                                        }
                                }
                                g_free (qname);
+                               g_free (fqname);
                        }
 
                        
@@ -530,6 +635,7 @@ mono_class_init (MonoClass *class)
        }
 
        init_properties (class);
+       init_events (class);
 
        i = mono_metadata_nesting_typedef (class->image, class->type_token);
        while (i) {
@@ -586,8 +692,45 @@ mono_class_init (MonoClass *class)
                        }
                }
        }
+
+       class->inited = 1;
+       class->init_pending = 0;
+
+       if (!default_ghc) {
+               if (class == mono_defaults.object_class) { 
+                      
+                       for (i = 0; i < class->vtable_size; ++i) {
+                               MonoMethod *cm = vtable [i];
+              
+                               if (!strcmp (cm->name, "GetHashCode")) {
+                                       ghc_slot = i;
+                                       break;
+                               }
+                       }
+
+                       g_assert (ghc_slot > 0);
+
+                       default_ghc = vtable [ghc_slot];
+               }
+       }
+       
+       class->ghcimpl = 1;
+       if (class != mono_defaults.object_class) { 
+
+               if (vtable [ghc_slot] == default_ghc) {
+                       class->ghcimpl = 0;
+               }
+
+       }
 }
 
+#if HAVE_BOEHM_GC
+static void
+vtable_finalizer (void *obj, void *data) {
+       g_print ("%s finalized (%p)\n", (char*)data, obj);
+}
+#endif
+
 /**
  * mono_class_vtable:
  * @domain: the application domain
@@ -606,7 +749,7 @@ mono_class_vtable (MonoDomain *domain, MonoClass *class)
        guint32 cols [MONO_CONSTANT_SIZE];
        const char *p;
        char *t;
-       int i;
+       int i, len;
 
        g_assert (class);
 
@@ -614,21 +757,33 @@ mono_class_vtable (MonoDomain *domain, MonoClass *class)
        if (class->flags & TYPE_ATTRIBUTE_INTERFACE)
                g_assert_not_reached ();
 
-       if ((vt = g_hash_table_lookup (domain->class_vtable_hash, class)))
+       if ((vt = mono_g_hash_table_lookup (domain->class_vtable_hash, class)))
                return vt;
        
        if (!class->inited)
                mono_class_init (class);
                
+#if HAVE_BOEHM_GC
+       vt = GC_malloc (sizeof (MonoVTable) + class->vtable_size * sizeof (gpointer));
+       /*vt = GC_debug_malloc (sizeof (MonoVTable) + class->vtable_size * sizeof (gpointer), class->name, 1);*/
+       GC_register_finalizer (vt, vtable_finalizer, "vtable", NULL, NULL);
+#else
        vt = g_malloc0 (sizeof (MonoVTable) + class->vtable_size * sizeof (gpointer));
+#endif
        vt->klass = class;
        vt->domain = domain;
 
        if (class->class_size) {
+#if HAVE_BOEHM_GC
+               vt->data = GC_malloc (class->class_size + 8);
+               /*vt->data = GC_debug_malloc (class->class_size + 8, class->name, 2);*/
+               GC_register_finalizer (vt->data, vtable_finalizer, class->name, NULL, NULL);
+#else
                vt->data = g_malloc0 (class->class_size + 8);
+#endif
                /* align: fixme not 64 bit clean */
                if (((guint32)vt->data) & 0x7)
-                       vt->data += 8 - (((guint32)vt->data) & 0x7);
+                       vt->data = (char*)vt->data + 8 - (((guint32)vt->data) & 0x7);
        }
 
        for (i = class->field.first; i < class->field.last; ++i) {
@@ -644,7 +799,7 @@ mono_class_vtable (MonoDomain *domain, MonoClass *class)
                }
                mono_metadata_decode_row (&class->image->tables [MONO_TABLE_CONSTANT], cindex - 1, cols, MONO_CONSTANT_SIZE);
                p = mono_metadata_blob_heap (class->image, cols [MONO_CONSTANT_VALUE]);
-               mono_metadata_decode_blob_size (p, &p);
+               len = mono_metadata_decode_blob_size (p, &p);
                t = (char*)vt->data + field->offset;
                /* should we check that the type matches? */
                switch (cols [MONO_CONSTANT_TYPE]) {
@@ -683,7 +838,8 @@ mono_class_vtable (MonoDomain *domain, MonoClass *class)
                        break;
                }
                case MONO_TYPE_STRING: {
-                       g_warning ("we don't handle strings in constant table");
+                       gpointer *val = (gpointer*)t;
+                       *val = mono_string_new_utf16 (domain, (const guint16*)p, len/2);
                        break;
                }
                case MONO_TYPE_CLASS:
@@ -714,13 +870,60 @@ mono_class_vtable (MonoDomain *domain, MonoClass *class)
                        vt->vtable [i] = arch_create_jit_trampoline (cm);
        }
 
-       g_hash_table_insert (domain->class_vtable_hash, class, vt);
+       mono_g_hash_table_insert (domain->class_vtable_hash, class, vt);
 
        mono_runtime_class_init (class);
 
        return vt;
 }
 
+/**
+ * mono_class_proxy_vtable:
+ * @domain: the application domain
+ * @class: the class to proxy
+ *
+ * Creates a vtable for transparent proxies. It is basically
+ * a copy of the real vtable of @class, but all function pointers invoke
+ * the remoting functions, and vtable->klass points to the 
+ * transparent proxy class, and not to @class.
+ */
+MonoVTable *
+mono_class_proxy_vtable (MonoDomain *domain, MonoClass *class)
+{
+       MonoVTable *vt, *pvt;
+       int i, vtsize;
+
+       if ((pvt = mono_g_hash_table_lookup (domain->proxy_vtable_hash, class)))
+               return pvt;
+
+       vt = mono_class_vtable (domain, class);
+       vtsize = sizeof (MonoVTable) + class->vtable_size * sizeof (gpointer);
+
+#if HAVE_BOEHM_GC
+       pvt = GC_malloc (vtsize);
+       GC_register_finalizer (vt, vtable_finalizer, "vtable", NULL, NULL);
+#else
+       pvt = g_malloc0 (vtsize);
+#endif
+       
+       memcpy (pvt, vt, vtsize);
+
+       pvt->klass = mono_defaults.transparent_proxy_class;
+
+       /* initialize vtable */
+       for (i = 0; i < class->vtable_size; ++i) {
+               MonoMethod *cm;
+              
+               if ((cm = class->vtable [i]))
+                       pvt->vtable [i] = arch_create_remoting_trampoline (cm);
+       }
+
+       mono_g_hash_table_insert (domain->proxy_vtable_hash, class, pvt);
+
+       return pvt;
+}
+
+
 /*
  * Compute a relative numbering of the class hierarchy as described in
  * "Java for Large-Scale Scientific Computations?"
@@ -740,63 +943,11 @@ mono_compute_relative_numbering (MonoClass *class, int *c)
        class->diffval = *c -  class->baseval;
 }
 
-/**
- * @image: context where the image is created
- * @type_token:  typedef token
- */
-static MonoClass *
-mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
+void
+mono_class_setup_mono_type (MonoClass *class)
 {
-       MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
-       MonoClass *class, *parent = NULL;
-       guint32 cols [MONO_TYPEDEF_SIZE];
-       guint32 cols_next [MONO_TYPEDEF_SIZE];
-       guint tidx = mono_metadata_token_index (type_token);
-       const char *name, *nspace;
-       guint vtsize = 0, icount = 0; 
-       MonoClass **interfaces;
-       int i;
-
-       if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token))))
-               return class;
-
-       g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
-       
-       mono_metadata_decode_row (tt, tidx - 1, cols, CSIZE (cols));
-       
-       if (tt->rows > tidx) {          
-               mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
-               vtsize += cols_next [MONO_TYPEDEF_METHOD_LIST] - cols [MONO_TYPEDEF_METHOD_LIST];
-       } else {
-               vtsize += image->tables [MONO_TABLE_METHOD].rows - cols [MONO_TYPEDEF_METHOD_LIST] + 1;
-       }
-
-       name = mono_metadata_string_heap (image, cols[1]);
-       nspace = mono_metadata_string_heap (image, cols[2]);
-
-       if (!(!strcmp (nspace, "System") && !strcmp (name, "Object")) &&
-           !(cols [0] & TYPE_ATTRIBUTE_INTERFACE)) {
-               parent = mono_class_get (image, mono_metadata_token_from_dor (cols [3]));
-       }
-       interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
-
-       for (i = 0; i < icount; i++) 
-               vtsize += interfaces [i]->method.count;
-       
-       if (parent)
-               vtsize += parent->vtable_size;
-
-       if (cols [0] & TYPE_ATTRIBUTE_INTERFACE)
-               vtsize = 0;
-
-       class = g_malloc0 (sizeof (MonoClass) + vtsize * sizeof (gpointer));
-       
-       g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
-
-       class->parent = parent;
-       class->interfaces = interfaces;
-       class->interface_count = icount;
-       class->vtable_size = vtsize;
+       const char *name = class->name;
+       const char *nspace = class->name_space;
 
        class->this_arg.byref = 1;
        class->this_arg.data.klass = class;
@@ -804,34 +955,6 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
        class->byval_arg.data.klass = class;
        class->byval_arg.type = MONO_TYPE_CLASS;
 
-       class->name = name;
-       class->name_space = nspace;
-
-       class->image = image;
-       class->type_token = type_token;
-       class->flags = cols [0];
-
-       class->element_class = class;
-
-       /*g_print ("Init class %s\n", name);*/
-
-       /* if root of the hierarchy */
-       if (!strcmp (nspace, "System") && !strcmp (name, "Object")) {
-               class->parent = NULL;
-               class->instance_size = sizeof (MonoObject);
-       } else if (!(cols [0] & TYPE_ATTRIBUTE_INTERFACE)) {
-               int rnum = 0;
-               class->parent = mono_class_get (image,  mono_metadata_token_from_dor (cols [3]));
-               if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) && (strcmp (class->parent->name_space, "System") == 0)))
-                       class->valuetype = 1;
-               if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
-                       class->valuetype = class->enumtype = 1;
-               }
-               //class->enumtype = class->parent->enumtype;
-               class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
-               mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
-       }
-
        if (!strcmp (nspace, "System")) {
                if (!strcmp (name, "ValueType")) {
                        /*
@@ -913,6 +1036,127 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
                }
                class->this_arg.type = class->byval_arg.type = t;
        }
+}
+
+void
+mono_class_setup_parent (MonoClass *class, MonoClass *parent)
+{
+       gboolean system_namespace;
+
+       system_namespace = !strcmp (class->name_space, "System");
+
+       /* if root of the hierarchy */
+       if (system_namespace && !strcmp (class->name, "Object")) {
+               class->parent = NULL;
+               class->instance_size = sizeof (MonoObject);
+               return;
+       }
+
+       if (!(class->flags & TYPE_ATTRIBUTE_INTERFACE)) {
+               int rnum = 0;
+               class->parent = parent;
+
+               class->marshalbyref = parent->marshalbyref;
+               class->contextbound  = parent->contextbound;
+               class->delegate  = parent->delegate;
+               
+               if (system_namespace) {
+                       if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
+                               class->marshalbyref = 1;
+
+                       if (*class->name == 'M' && !strcmp (class->name, "ContextBoundObject")) 
+                               class->contextbound  = 1;
+
+                       if (*class->name == 'D' && !strcmp (class->name, "Delegate")) 
+                               class->delegate  = 1;
+               }
+
+               if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) && 
+                                               (strcmp (class->parent->name_space, "System") == 0)))
+                       class->valuetype = 1;
+               if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
+                       class->valuetype = class->enumtype = 1;
+               }
+               //class->enumtype = class->parent->enumtype;
+               class->parent->subclasses = g_list_prepend (class->parent->subclasses, class);
+               mono_compute_relative_numbering (mono_defaults.object_class, &rnum);
+       } else {
+               class->parent = NULL;
+       }
+
+}
+
+/**
+ * @image: context where the image is created
+ * @type_token:  typedef token
+ */
+static MonoClass *
+mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
+{
+       MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
+       MonoClass *class, *parent = NULL;
+       guint32 cols [MONO_TYPEDEF_SIZE];
+       guint32 cols_next [MONO_TYPEDEF_SIZE];
+       guint tidx = mono_metadata_token_index (type_token);
+       const char *name, *nspace;
+       guint vtsize = 0, icount = 0; 
+       MonoClass **interfaces;
+       int i;
+
+       if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token))))
+               return class;
+
+       g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
+       
+       mono_metadata_decode_row (tt, tidx - 1, cols, CSIZE (cols));
+       
+       if (tt->rows > tidx) {          
+               mono_metadata_decode_row (tt, tidx, cols_next, CSIZE (cols_next));
+               vtsize += cols_next [MONO_TYPEDEF_METHOD_LIST] - cols [MONO_TYPEDEF_METHOD_LIST];
+       } else {
+               vtsize += image->tables [MONO_TABLE_METHOD].rows - cols [MONO_TYPEDEF_METHOD_LIST] + 1;
+       }
+
+       name = mono_metadata_string_heap (image, cols[1]);
+       nspace = mono_metadata_string_heap (image, cols[2]);
+
+       if (!(!strcmp (nspace, "System") && !strcmp (name, "Object")) &&
+           !(cols [0] & TYPE_ATTRIBUTE_INTERFACE)) {
+               parent = mono_class_get (image, mono_metadata_token_from_dor (cols [3]));
+       }
+       interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
+
+       for (i = 0; i < icount; i++) 
+               vtsize += interfaces [i]->method.count;
+       
+       if (parent)
+               vtsize += parent->vtable_size;
+
+       if (cols [0] & TYPE_ATTRIBUTE_INTERFACE)
+               vtsize = 0;
+
+       class = g_malloc0 (sizeof (MonoClass) + vtsize * sizeof (gpointer));
+       
+       g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
+
+       class->interfaces = interfaces;
+       class->interface_count = icount;
+       class->vtable_size = vtsize;
+
+       class->name = name;
+       class->name_space = nspace;
+
+       class->image = image;
+       class->type_token = type_token;
+       class->flags = cols [MONO_TYPEDEF_FLAGS];
+
+       class->element_class = class;
+
+       /*g_print ("Init class %s\n", name);*/
+
+       mono_class_setup_parent (class, parent);
+
+       mono_class_setup_mono_type (class);
 
        /*
         * Compute the field and method lists
@@ -951,16 +1195,18 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
 
        //class->interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &class->interface_count);
 
-       if (class->enumtype)
-               mono_class_init (class);
+       if (class->enumtype) {
+               class->fields = g_new0 (MonoClassField, class->field.count);
+               class_compute_field_layout (class);
+       } 
 
        if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
                class->nested_in = mono_class_create_from_typedef (image, type_token);
        return class;
 }
 
-static MonoClass *
-mono_create_ptr_class (MonoType *type)
+MonoClass *
+mono_ptr_class_get (MonoType *type)
 {
        MonoClass *result;
        MonoClass *el_class;
@@ -976,7 +1222,7 @@ mono_create_ptr_class (MonoType *type)
        result->parent = NULL; /* no parent for PTR types */
        result->name = "System";
        result->name_space = "MonoPtrFakeClass";
-       result->image = NULL;
+       result->image = el_class->image;
        result->inited = TRUE;
        /* Can pointers get boxed? */
        result->instance_size = sizeof (gpointer);
@@ -1034,11 +1280,11 @@ mono_class_from_mono_type (MonoType *type)
        case MONO_TYPE_STRING:
                return mono_defaults.string_class;
        case MONO_TYPE_ARRAY:
-               return mono_array_class_get (mono_class_from_mono_type (type->data.array->type), type->data.array->rank);
+               return mono_array_class_get (type->data.array->type, type->data.array->rank);
        case MONO_TYPE_PTR:
-               return mono_create_ptr_class (type->data.type);
+               return mono_ptr_class_get (type->data.type);
        case MONO_TYPE_SZARRAY:
-               return mono_array_class_get (mono_class_from_mono_type (type->data.type), 1);
+               return mono_array_class_get (type->data.type, 1);
        case MONO_TYPE_CLASS:
        case MONO_TYPE_VALUETYPE:
                return type->data.klass;
@@ -1058,18 +1304,16 @@ static MonoClass *
 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
 {
        MonoType *type;
-       MonoClass *class, *eclass;
+       MonoClass *class;
 
        type = mono_type_create_from_typespec (image, type_spec);
 
        switch (type->type) {
        case MONO_TYPE_ARRAY:
-               eclass = mono_class_from_mono_type (type->data.array->type);
-               class = mono_array_class_get (eclass, type->data.array->rank);
+               class = mono_array_class_get (type->data.array->type, type->data.array->rank);
                break;
        case MONO_TYPE_SZARRAY:
-               eclass = mono_class_from_mono_type (type->data.type);
-               class = mono_array_class_get (eclass, 1);
+               class = mono_array_class_get (type->data.type, 1);
                break;
        case MONO_TYPE_PTR:
                class = mono_class_from_mono_type (type->data.type);
@@ -1086,21 +1330,23 @@ mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
 
 /**
  * mono_array_class_get:
- * @eclass: element type class
+ * @element_type: element type 
  * @rank: the dimension of the array class
  *
- * Returns: a class object describing the array with element type @etype and 
+ * Returns: a class object describing the array with element type @element_type and 
  * dimension @rank. 
  */
 MonoClass *
-mono_array_class_get (MonoClass *eclass, guint32 rank)
+mono_array_class_get (MonoType *element_type, guint32 rank)
 {
+       MonoClass *eclass;
        MonoImage *image;
        MonoClass *class;
        static MonoClass *parent = NULL;
        guint32 key;
        int rnum = 0;
 
+       eclass = mono_class_from_mono_type (element_type);
        g_assert (rank <= 255);
 
        if (!parent)
@@ -1135,7 +1381,11 @@ mono_array_class_get (MonoClass *eclass, guint32 rank)
        class->rank = rank;
        class->element_class = eclass;
        if (rank > 1) {
+               MonoArrayType *at = g_new0 (MonoArrayType, 1);
                class->byval_arg.type = MONO_TYPE_ARRAY;
+               class->byval_arg.data.array = at;
+               at->type = &eclass->byval_arg;
+               at->rank = rank;
                /* FIXME: complete.... */
        } else {
                class->byval_arg.type = MONO_TYPE_SZARRAY;
@@ -1158,7 +1408,7 @@ gint32
 mono_class_instance_size (MonoClass *klass)
 {
        
-       if (!klass->inited)
+       if (!klass->size_inited)
                mono_class_init (klass);
 
        return klass->instance_size;
@@ -1252,7 +1502,7 @@ mono_class_get_field_from_name (MonoClass *klass, const char *name)
        int i;
 
        for (i = 0; i < klass->field.count; ++i) {
-               if (strcmp (name, klass->fields->name) == 0)
+               if (strcmp (name, klass->fields [i].name) == 0)
                        return &klass->fields [i];
        }
        return NULL;