[mono-error] Replace mono_metadata_parse_type_full with mono_metadata_parse_type_checked.
[mono.git] / mono / metadata / class.c
index 132c3a745cd4b70c880f867e1c2945890d26b301..983f1ca4149d90e78ab62646b4c9983b3d2a415d 100644 (file)
@@ -43,6 +43,7 @@
 #include <mono/utils/mono-memory-model.h>
 #include <mono/utils/atomic.h>
 #include <mono/utils/bsearch.h>
+#include <mono/utils/checked-build.h>
 
 MonoStats mono_stats;
 
@@ -149,14 +150,14 @@ disable_gclass_recording (gclass_record_func func, void *user_data)
        }
 }
 
-/*
+/**
  * mono_class_from_typeref:
  * @image: a MonoImage
  * @type_token: a TypeRef token
  *
  * Creates the MonoClass* structure representing the type defined by
  * the typeref token valid inside @image.
- * Returns: the MonoClass* representing the typeref token, NULL ifcould
+ * Returns: The MonoClass* representing the typeref token, NULL ifcould
  * not be loaded.
  */
 MonoClass *
@@ -168,6 +169,19 @@ mono_class_from_typeref (MonoImage *image, guint32 type_token)
        return klass;
 }
 
+/**
+ * mono_class_from_typeref_checked:
+ * @image: a MonoImage
+ * @type_token: a TypeRef token
+ * @error: error return code, if any.
+ *
+ * Creates the MonoClass* structure representing the type defined by
+ * the typeref token valid inside @image.
+ *
+ * Returns: The MonoClass* representing the typeref token, NULL if it could
+ * not be loaded with the @error value filled with the information about the
+ * error.
+ */
 MonoClass *
 mono_class_from_typeref_checked (MonoImage *image, guint32 type_token, MonoError *error)
 {
@@ -216,13 +230,12 @@ mono_class_from_typeref_checked (MonoImage *image, guint32 type_token, MonoError
                }
 
                enclosing = mono_class_from_typeref_checked (image, MONO_TOKEN_TYPE_REF | idx, error); 
-               if (!mono_error_ok (error))
-                       return NULL;
+               return_val_if_nok (error, NULL);
 
                if (enclosing->nested_classes_inited && enclosing->ext) {
                        /* Micro-optimization: don't scan the metadata tables if enclosing is already inited */
                        for (tmp = enclosing->ext->nested_classes; tmp; tmp = tmp->next) {
-                               res = tmp->data;
+                               res = (MonoClass *)tmp->data;
                                if (strcmp (res->name, name) == 0)
                                        return res;
                        }
@@ -298,17 +311,17 @@ MonoArrayType *
 mono_dup_array_type (MonoImage *image, MonoArrayType *a)
 {
        if (image) {
-               a = mono_image_memdup (image, a, sizeof (MonoArrayType));
+               a = (MonoArrayType *)mono_image_memdup (image, a, sizeof (MonoArrayType));
                if (a->sizes)
-                       a->sizes = mono_image_memdup (image, a->sizes, a->numsizes * sizeof (int));
+                       a->sizes = (int *)mono_image_memdup (image, a->sizes, a->numsizes * sizeof (int));
                if (a->lobounds)
-                       a->lobounds = mono_image_memdup (image, a->lobounds, a->numlobounds * sizeof (int));
+                       a->lobounds = (int *)mono_image_memdup (image, a->lobounds, a->numlobounds * sizeof (int));
        } else {
-               a = g_memdup (a, sizeof (MonoArrayType));
+               a = (MonoArrayType *)g_memdup (a, sizeof (MonoArrayType));
                if (a->sizes)
-                       a->sizes = g_memdup (a->sizes, a->numsizes * sizeof (int));
+                       a->sizes = (int *)g_memdup (a->sizes, a->numsizes * sizeof (int));
                if (a->lobounds)
-                       a->lobounds = g_memdup (a->lobounds, a->numlobounds * sizeof (int));
+                       a->lobounds = (int *)g_memdup (a->lobounds, a->numlobounds * sizeof (int));
        }
        return a;
 }
@@ -551,7 +564,7 @@ mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
  * @format: the format for the return string.
  *
  * 
- * Returns: the string representation in a number of formats:
+ * Returns: The string representation in a number of formats:
  *
  * if format is MONO_TYPE_NAME_FORMAT_REFLECTION, the return string is
  * returned in the formatrequired by System.Reflection, this is the
@@ -580,7 +593,7 @@ mono_type_get_name_full (MonoType *type, MonoTypeNameFormat format)
  * mono_type_get_full_name:
  * @class: a class
  *
- * Returns: the string representation for type as required by System.Reflection.
+ * Returns: The string representation for type as required by System.Reflection.
  * The inverse of mono_reflection_parse_type ().
  */
 char *
@@ -593,7 +606,7 @@ mono_type_get_full_name (MonoClass *klass)
  * mono_type_get_name:
  * @type: a type
  *
- * Returns: the string representation for type as it would be represented in IL code.
+ * Returns: The string representation for type as it would be represented in IL code.
  */
 char*
 mono_type_get_name (MonoType *type)
@@ -605,7 +618,7 @@ mono_type_get_name (MonoType *type)
  * mono_type_get_underlying_type:
  * @type: a type
  *
- * Returns: the MonoType for the underlying integer type if @type
+ * Returns: The MonoType for the underlying integer type if @type
  * is an enum and byref is false, otherwise the type itself.
  */
 MonoType*
@@ -618,11 +631,11 @@ mono_type_get_underlying_type (MonoType *type)
        return type;
 }
 
-/*
+/**
  * mono_class_is_open_constructed_type:
  * @type: a type
  *
- * Returns TRUE if type represents a generics open constructed type.
+ * Returns: TRUE if type represents a generics open constructed type.
  * IOW, not all type parameters required for the instantiation have
  * been provided or it's a generic type definition.
  *
@@ -755,8 +768,8 @@ inflate_generic_type (MonoImage *image, MonoType *type, MonoGenericContext *cont
                        return NULL;
 
                inst = mono_metadata_inflate_generic_inst (gclass->context.class_inst, context, error);
-               if (!mono_error_ok (error))
-                       return NULL;
+               return_val_if_nok (error, NULL);
+
                if (inst != gclass->context.class_inst)
                        gclass = mono_metadata_lookup_generic_class (gclass->container_class, inst, gclass->is_dynamic);
 
@@ -780,8 +793,8 @@ inflate_generic_type (MonoImage *image, MonoType *type, MonoGenericContext *cont
 
                /* We can't use context->class_inst directly, since it can have more elements */
                inst = mono_metadata_inflate_generic_inst (container->context.class_inst, context, error);
-               if (!mono_error_ok (error))
-                       return NULL;
+               return_val_if_nok (error, NULL);
+
                if (inst == container->context.class_inst)
                        return NULL;
 
@@ -857,8 +870,7 @@ mono_class_inflate_generic_type_with_mempool (MonoImage *image, MonoType *type,
 
        if (context)
                inflated = inflate_generic_type (image, type, context, error);
-       if (!mono_error_ok (error))
-               return NULL;
+       return_val_if_nok (error, NULL);
 
        if (!inflated) {
                MonoType *shared = mono_metadata_get_shared_type (type);
@@ -882,7 +894,7 @@ mono_class_inflate_generic_type_with_mempool (MonoImage *image, MonoType *type,
  * If @type is a generic type and @context is not NULL, instantiate it using the 
  * generics context @context.
  *
- * Returns: the instantiated type or a copy of @type. The returned MonoType is allocated
+ * Returns: The instantiated type or a copy of @type. The returned MonoType is allocated
  * on the heap and is owned by the caller. Returns NULL on error.
  *
  * @deprecated Please use mono_class_inflate_generic_type_checked instead
@@ -910,7 +922,7 @@ mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
  * If @type is a generic type and @context is not NULL, instantiate it using the 
  * generics context @context.
  *
- * Returns: the instantiated type or a copy of @type. The returned MonoType is allocated
+ * Returns: The instantiated type or a copy of @type. The returned MonoType is allocated
  * on the heap and is owned by the caller.
  */
 MonoType*
@@ -933,8 +945,7 @@ mono_class_inflate_generic_type_no_copy (MonoImage *image, MonoType *type, MonoG
        mono_error_init (error);
        if (context) {
                inflated = inflate_generic_type (image, type, context, error);
-               if (!mono_error_ok (error))
-                       return NULL;
+               return_val_if_nok (error, NULL);
        }
 
        if (!inflated)
@@ -951,8 +962,7 @@ mono_class_inflate_generic_class_checked (MonoClass *gklass, MonoGenericContext
        MonoType *inflated;
 
        inflated = mono_class_inflate_generic_type_checked (&gklass->byval_arg, context, error);
-       if (!mono_error_ok (error))
-               return NULL;
+       return_val_if_nok (error, NULL);
 
        res = mono_class_from_mono_type (inflated);
        mono_metadata_free_type (inflated);
@@ -1012,7 +1022,7 @@ fail:
  *
  * Instantiate the generic method @method using the generics context @context.
  *
- * Returns: the new instantiated method
+ * Returns: The new instantiated method
  */
 MonoMethod *
 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context)
@@ -1065,8 +1075,8 @@ mono_class_inflate_generic_method_full_checked (MonoMethod *method, MonoClass *k
                MonoMethodInflated *imethod = (MonoMethodInflated *) method;
 
                tmp_context = inflate_generic_context (method_context, context, error);
-               if (!mono_error_ok (error))
-                       return NULL;
+               return_val_if_nok (error, NULL);
+
                context = &tmp_context;
 
                if (mono_metadata_generic_context_equal (method_context, context))
@@ -1109,7 +1119,7 @@ mono_class_inflate_generic_method_full_checked (MonoMethod *method, MonoClass *k
 
        // check cache
        mono_image_set_lock (set);
-       cached = g_hash_table_lookup (set->gmethod_cache, iresult);
+       cached = (MonoMethodInflated *)g_hash_table_lookup (set->gmethod_cache, iresult);
        mono_image_set_unlock (set);
 
        if (cached) {
@@ -1184,7 +1194,7 @@ mono_class_inflate_generic_method_full_checked (MonoMethod *method, MonoClass *k
 
        // check cache
        mono_image_set_lock (set);
-       cached = g_hash_table_lookup (set->gmethod_cache, iresult);
+       cached = (MonoMethodInflated *)g_hash_table_lookup (set->gmethod_cache, iresult);
        if (!cached) {
                g_hash_table_insert (set->gmethod_cache, iresult, iresult);
                iresult->owner = set;
@@ -1264,7 +1274,7 @@ mono_method_get_generic_container (MonoMethod *method)
        if (!method->is_generic)
                return NULL;
 
-       container = mono_image_property_lookup (method->klass->image, method, MONO_METHOD_PROP_GENERIC_CONTAINER);
+       container = (MonoGenericContainer *)mono_image_property_lookup (method->klass->image, method, MONO_METHOD_PROP_GENERIC_CONTAINER);
        g_assert (container);
 
        return container;
@@ -1340,14 +1350,10 @@ mono_class_find_enum_basetype (MonoClass *klass, MonoError *error)
                        goto fail;
                }
 
-               ftype = mono_metadata_parse_type_full (m, container, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
-               if (!ftype) {
-                       if (mono_loader_get_last_error ()) /*FIXME plug the above to not leak errors*/
-                               mono_error_set_from_loader_error (error);
-                       else
-                               mono_error_set_bad_image (error, klass->image, "Could not parse type for field signature %x", cols [MONO_FIELD_SIGNATURE]);
+               ftype = mono_metadata_parse_type_checked (m, container, cols [MONO_FIELD_FLAGS], FALSE, sig + 1, &sig, error);
+               if (!ftype)
                        goto fail;
-               }
+
                if (klass->generic_class) {
                        //FIXME do we leak here?
                        ftype = mono_class_inflate_generic_type_checked (ftype, mono_class_get_context (klass), error);
@@ -1453,7 +1459,7 @@ mono_class_setup_basic_field_info (MonoClass *klass)
                klass->field.count = gtd->field.count;
        }
 
-       klass->fields = mono_class_alloc0 (klass, sizeof (MonoClassField) * top);
+       klass->fields = (MonoClassField *)mono_class_alloc0 (klass, sizeof (MonoClassField) * top);
 
        /*
         * Fetch all the field information.
@@ -2191,7 +2197,7 @@ mono_class_setup_methods (MonoClass *klass)
 
                /* The + 1 makes this always non-NULL to pass the check in mono_class_setup_methods () */
                count = gklass->method.count;
-               methods = mono_class_alloc0 (klass, sizeof (MonoMethod*) * (count + 1));
+               methods = (MonoMethod **)mono_class_alloc0 (klass, sizeof (MonoMethod*) * (count + 1));
 
                for (i = 0; i < count; i++) {
                        methods [i] = mono_class_inflate_generic_method_full_checked (
@@ -2229,7 +2235,7 @@ mono_class_setup_methods (MonoClass *klass)
                        count += klass->interface_count * count_generic;
                }
 
-               methods = mono_class_alloc0 (klass, sizeof (MonoMethod*) * count);
+               methods = (MonoMethod **)mono_class_alloc0 (klass, sizeof (MonoMethod*) * count);
 
                sig = mono_metadata_signature_alloc (klass->image, klass->rank);
                sig->ret = &mono_defaults.void_class->byval_arg;
@@ -2299,7 +2305,7 @@ mono_class_setup_methods (MonoClass *klass)
                MonoError error;
 
                count = klass->method.count;
-               methods = mono_class_alloc (klass, sizeof (MonoMethod*) * count);
+               methods = (MonoMethod **)mono_class_alloc (klass, sizeof (MonoMethod*) * count);
                for (i = 0; i < count; ++i) {
                        int idx = mono_metadata_translate_token_index (klass->image, MONO_TABLE_METHOD, klass->method.first + i + 1);
                        methods [i] = mono_get_method_checked (klass->image, MONO_TOKEN_METHOD_DEF | idx, klass, NULL, &error);
@@ -2517,7 +2523,7 @@ mono_class_setup_properties (MonoClass *klass)
                                return;
                }
 
-               properties = mono_class_alloc0 (klass, sizeof (MonoProperty) * count);
+               properties = (MonoProperty *)mono_class_alloc0 (klass, sizeof (MonoProperty) * count);
                for (i = first; i < last; ++i) {
                        mono_metadata_decode_table_row (klass->image, MONO_TABLE_PROPERTY, i, cols, MONO_PROPERTY_SIZE);
                        properties [i - first].parent = klass;
@@ -2660,7 +2666,7 @@ mono_class_setup_events (MonoClass *klass)
                        }
                }
 
-               events = mono_class_alloc0 (klass, sizeof (MonoEvent) * count);
+               events = (MonoEvent *)mono_class_alloc0 (klass, sizeof (MonoEvent) * count);
                for (i = first; i < last; ++i) {
                        MonoEvent *event = &events [i - first];
 
@@ -2703,7 +2709,7 @@ mono_class_setup_events (MonoClass *klass)
                                        } else {
                                                while (event->other [n])
                                                        n++;
-                                               event->other = g_realloc (event->other, (n + 2) * sizeof (MonoMethod*));
+                                               event->other = (MonoMethod **)g_realloc (event->other, (n + 2) * sizeof (MonoMethod*));
                                        }
                                        event->other [n] = method;
                                        /* NULL terminated */
@@ -2771,14 +2777,14 @@ mono_unload_interface_id (MonoClass *klass)
        }
 }
 
-/*
+/**
  * mono_get_unique_iid:
  * @class: interface
  *
  * Assign a unique integer ID to the interface represented by @class.
  * The ID will positive and as small as possible.
  * LOCKING: Acquires the classes lock.
- * Returns: the new ID.
+ * Returns: The new ID.
  */
 static guint
 mono_get_unique_iid (MonoClass *klass)
@@ -2844,8 +2850,7 @@ collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res, MonoError
        MonoClass *ic;
 
        mono_class_setup_interfaces (klass, error);
-       if (!mono_error_ok (error))
-               return;
+       return_if_nok (error);
 
        for (i = 0; i < klass->interface_count; i++) {
                ic = klass->interfaces [i];
@@ -2860,8 +2865,7 @@ collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res, MonoError
                }
 
                collect_implemented_interfaces_aux (ic, res, error);
-               if (!mono_error_ok (error))
-                       return;
+               return_if_nok (error);
        }
 }
 
@@ -2881,8 +2885,8 @@ mono_class_get_implemented_interfaces (MonoClass *klass, MonoError *error)
 
 static int
 compare_interface_ids (const void *p_key, const void *p_element) {
-       const MonoClass *key = p_key;
-       const MonoClass *element = *(MonoClass**) p_element;
+       const MonoClass *key = (const MonoClass *)p_key;
+       const MonoClass *element = *(const MonoClass **)p_element;
        
        return (key->interface_id - element->interface_id);
 }
@@ -2890,7 +2894,7 @@ compare_interface_ids (const void *p_key, const void *p_element) {
 /*FIXME verify all callers if they should switch to mono_class_interface_offset_with_variance*/
 int
 mono_class_interface_offset (MonoClass *klass, MonoClass *itf) {
-       MonoClass **result = mono_binary_search (
+       MonoClass **result = (MonoClass **)mono_binary_search (
                        itf,
                        klass->interfaces_packed,
                        klass->interface_offsets_count,
@@ -2903,7 +2907,7 @@ mono_class_interface_offset (MonoClass *klass, MonoClass *itf) {
        }
 }
 
-/*
+/**
  * mono_class_interface_offset_with_variance:
  * 
  * Return the interface offset of @itf in @klass. Sets @non_exact_match to TRUE if the match required variance check
@@ -2983,7 +2987,7 @@ print_implemented_interfaces (MonoClass *klass) {
                        mono_error_cleanup (&error);
                } else if (ifaces) {
                        for (i = 0; i < ifaces->len; i++) {
-                               MonoClass *ic = g_ptr_array_index (ifaces, i);
+                               MonoClass *ic = (MonoClass *)g_ptr_array_index (ifaces, i);
                                printf ("  [UIID %d] interface %s\n", ic->interface_id, ic->name);
                                printf ("  [%03d][UUID %03d][SLOT %03d][SIZE  %03d] interface %s.%s\n", i,
                                                ic->interface_id,
@@ -3135,7 +3139,7 @@ get_implicit_generic_array_interfaces (MonoClass *klass, int *num, int *is_enume
                                ++real_count;
                }
 
-               interfaces = g_malloc0 (sizeof (MonoClass*) * real_count);
+               interfaces = (MonoClass **)g_malloc0 (sizeof (MonoClass*) * real_count);
                interfaces [0] = valuetype_types [0];
                if (valuetype_types [1])
                        interfaces [nifaces] = valuetype_types [1];
@@ -3175,7 +3179,7 @@ get_implicit_generic_array_interfaces (MonoClass *klass, int *num, int *is_enume
                        if (valuetype_types [1])
                                ++real_count;
                }
-               interfaces = g_malloc0 (sizeof (MonoClass*) * real_count);
+               interfaces = (MonoClass **)g_malloc0 (sizeof (MonoClass*) * real_count);
                if (MONO_CLASS_IS_INTERFACE (eclass)) {
                        interfaces [0] = mono_defaults.object_class;
                        j = nifaces;
@@ -3412,7 +3416,7 @@ set_interface_and_offset (int num_ifaces, MonoClass **interfaces_full, int *inte
  * the size of the buffer.
  * This compression algorithm assumes the bits set in the bitmap are
  * few and far between, like in interface bitmaps.
- * Returns: the size of the compressed bitmap in bytes.
+ * Returns: The size of the compressed bitmap in bytes.
  */
 int
 mono_compress_bitmap (uint8_t *dest, const uint8_t *bitmap, int size)
@@ -3454,7 +3458,7 @@ mono_compress_bitmap (uint8_t *dest, const uint8_t *bitmap, int size)
  * be already checked for being smaller than the maximum id encoded in the
  * bitmap.
  *
- * Returns: a non-zero value if bit @id is set in the bitmap @bitmap,
+ * Returns: A non-zero value if bit @id is set in the bitmap @bitmap,
  * #FALSE otherwise.
  */
 int
@@ -3528,7 +3532,7 @@ setup_interface_offsets (MonoClass *klass, int cur_slot, gboolean overwrite)
                if (ifaces) {
                        num_ifaces += ifaces->len;
                        for (i = 0; i < ifaces->len; ++i) {
-                               ic = g_ptr_array_index (ifaces, i);
+                               ic = (MonoClass *)g_ptr_array_index (ifaces, i);
                                if (max_iid < ic->interface_id)
                                        max_iid = ic->interface_id;
                        }
@@ -3550,8 +3554,8 @@ setup_interface_offsets (MonoClass *klass, int cur_slot, gboolean overwrite)
        }
        klass->max_interface_id = max_iid;
        /* compute vtable offset for interfaces */
-       interfaces_full = g_malloc0 (sizeof (MonoClass*) * num_ifaces);
-       interface_offsets_full = g_malloc (sizeof (int) * num_ifaces);
+       interfaces_full = (MonoClass **)g_malloc0 (sizeof (MonoClass*) * num_ifaces);
+       interface_offsets_full = (int *)g_malloc (sizeof (int) * num_ifaces);
 
        for (i = 0; i < num_ifaces; i++) {
                interface_offsets_full [i] = -1;
@@ -3565,7 +3569,7 @@ setup_interface_offsets (MonoClass *klass, int cur_slot, gboolean overwrite)
                if (ifaces) {
                        for (i = 0; i < ifaces->len; ++i) {
                                int io;
-                               ic = g_ptr_array_index (ifaces, i);
+                               ic = (MonoClass *)g_ptr_array_index (ifaces, i);
                                
                                /*Force the sharing of interface offsets between parent and subtypes.*/
                                io = mono_class_interface_offset (k, ic);
@@ -3580,7 +3584,7 @@ setup_interface_offsets (MonoClass *klass, int cur_slot, gboolean overwrite)
        if (ifaces) {
                for (i = 0; i < ifaces->len; ++i) {
                        int count;
-                       ic = g_ptr_array_index (ifaces, i);
+                       ic = (MonoClass *)g_ptr_array_index (ifaces, i);
                        if (set_interface_and_offset (num_ifaces, interfaces_full, interface_offsets_full, ic, cur_slot, FALSE))
                                continue;
                        count = count_virtual_methods (ic);
@@ -3668,13 +3672,13 @@ setup_interface_offsets (MonoClass *klass, int cur_slot, gboolean overwrite)
                uint8_t *bitmap;
                int bsize;
                klass->interface_offsets_count = interface_offsets_count;
-               klass->interfaces_packed = mono_class_alloc (klass, sizeof (MonoClass*) * interface_offsets_count);
-               klass->interface_offsets_packed = mono_class_alloc (klass, sizeof (guint16) * interface_offsets_count);
+               klass->interfaces_packed = (MonoClass **)mono_class_alloc (klass, sizeof (MonoClass*) * interface_offsets_count);
+               klass->interface_offsets_packed = (guint16 *)mono_class_alloc (klass, sizeof (guint16) * interface_offsets_count);
                bsize = (sizeof (guint8) * ((max_iid + 1) >> 3)) + (((max_iid + 1) & 7)? 1 :0);
 #ifdef COMPRESSED_INTERFACE_BITMAP
                bitmap = g_malloc0 (bsize);
 #else
-               bitmap = mono_class_alloc0 (klass, bsize);
+               bitmap = (uint8_t *)mono_class_alloc0 (klass, bsize);
 #endif
                for (i = 0; i < interface_offsets_count; i++) {
                        int id = interfaces_full [i]->interface_id;
@@ -4268,7 +4272,7 @@ verify_class_overrides (MonoClass *klass, MonoMethod **overrides, int onum)
                }
 
                if (!mono_class_is_assignable_from_slow (decl->klass, klass)) {
-                       mono_class_set_failure (klass, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Method overrides a class or interface that extended or implemented by this type"));
+                       mono_class_set_failure (klass, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Method overrides a class or interface that is not extended or implemented by this type"));
                        return FALSE;
                }
 
@@ -4327,7 +4331,7 @@ mono_class_setup_vtable_general (MonoClass *klass, MonoMethod **overrides, int o
                return;
        } else if (ifaces) {
                for (i = 0; i < ifaces->len; i++) {
-                       MonoClass *ic = g_ptr_array_index (ifaces, i);
+                       MonoClass *ic = (MonoClass *)g_ptr_array_index (ifaces, i);
                        max_vtsize += ic->method.count;
                }
                g_ptr_array_free (ifaces, TRUE);
@@ -4358,7 +4362,7 @@ mono_class_setup_vtable_general (MonoClass *klass, MonoMethod **overrides, int o
                ++cur_slot;
        }
 
-       vtable = alloca (sizeof (gpointer) * max_vtsize);
+       vtable = (MonoMethod **)alloca (sizeof (gpointer) * max_vtsize);
        memset (vtable, 0, sizeof (gpointer) * max_vtsize);
 
        /* printf ("METAINIT %s.%s\n", klass->name_space, klass->name); */
@@ -4382,7 +4386,7 @@ mono_class_setup_vtable_general (MonoClass *klass, MonoMethod **overrides, int o
                        return;
                }
 
-               tmp = mono_class_alloc0 (klass, sizeof (gpointer) * gklass->vtable_size);
+               tmp = (MonoMethod **)mono_class_alloc0 (klass, sizeof (gpointer) * gklass->vtable_size);
                klass->vtable_size = gklass->vtable_size;
                for (i = 0; i < gklass->vtable_size; ++i)
                        if (gklass->vtable [i]) {
@@ -4531,7 +4535,7 @@ mono_class_setup_vtable_general (MonoClass *klass, MonoMethod **overrides, int o
                for (im_index = 0; im_index < ic->method.count; im_index++) {
                        MonoMethod *im = ic->methods [im_index];
                        int im_slot = ic_offset + im->slot;
-                       MonoMethod *override_im = (override_map != NULL) ? g_hash_table_lookup (override_map, im) : NULL;
+                       MonoMethod *override_im = (override_map != NULL) ? (MonoMethod *)g_hash_table_lookup (override_map, im) : NULL;
                        
                        if (im->flags & METHOD_ATTRIBUTE_STATIC)
                                continue;
@@ -4546,7 +4550,7 @@ mono_class_setup_vtable_general (MonoClass *klass, MonoMethod **overrides, int o
 
                                // First look for a suitable method among the class methods
                                for (l = virt_methods; l; l = l->next) {
-                                       cm = l->data;
+                                       cm = (MonoMethod *)l->data;
                                        TRACE_INTERFACE_VTABLE (printf ("    For slot %d ('%s'.'%s':'%s'), trying method '%s'.'%s':'%s'... [EXPLICIT IMPLEMENTATION = %d][SLOT IS NULL = %d]", im_slot, ic->name_space, ic->name, im->name, cm->klass->name_space, cm->klass->name, cm->name, interface_is_explicitly_implemented_by_class, (vtable [im_slot] == NULL)));
                                        if (check_interface_method_override (klass, im, cm, TRUE, interface_is_explicitly_implemented_by_class, (vtable [im_slot] == NULL))) {
                                                TRACE_INTERFACE_VTABLE (printf ("[check ok]: ASSIGNING"));
@@ -4621,7 +4625,7 @@ mono_class_setup_vtable_general (MonoClass *klass, MonoMethod **overrides, int o
 
        TRACE_INTERFACE_VTABLE (print_vtable_full (klass, vtable, cur_slot, first_non_interface_slot, "AFTER SETTING UP INTERFACE METHODS", FALSE));
        for (l = virt_methods; l; l = l->next) {
-               cm = l->data;
+               cm = (MonoMethod *)l->data;
                /*
                 * If the method is REUSE_SLOT, we must check in the
                 * base class for a method to override.
@@ -4724,7 +4728,7 @@ mono_class_setup_vtable_general (MonoClass *klass, MonoMethod **overrides, int o
                        if (vtable [i]) {
                                TRACE_INTERFACE_VTABLE (printf ("checking slot %d method %s[%p] for overrides\n", i, mono_method_full_name (vtable [i], 1), vtable [i]));
 
-                               cm = g_hash_table_lookup (override_map, vtable [i]);
+                               cm = (MonoMethod *)g_hash_table_lookup (override_map, vtable [i]);
                                if (cm)
                                        vtable [i] = cm;
                        }
@@ -4768,7 +4772,7 @@ mono_class_setup_vtable_general (MonoClass *klass, MonoMethod **overrides, int o
                mono_memory_barrier ();
                klass->vtable = klass->parent->vtable;
        } else {
-               MonoMethod **tmp = mono_class_alloc0 (klass, sizeof (gpointer) * klass->vtable_size);
+               MonoMethod **tmp = (MonoMethod **)mono_class_alloc0 (klass, sizeof (gpointer) * klass->vtable_size);
                memcpy (tmp, vtable,  sizeof (gpointer) * klass->vtable_size);
                mono_memory_barrier ();
                klass->vtable = tmp;
@@ -4953,12 +4957,12 @@ generic_array_methods (MonoClass *klass)
                }
        }
        list = g_list_reverse (list);
-       generic_array_method_info = mono_image_alloc (mono_defaults.corlib, sizeof (GenericArrayMethodInfo) * count_generic);
+       generic_array_method_info = (GenericArrayMethodInfo *)mono_image_alloc (mono_defaults.corlib, sizeof (GenericArrayMethodInfo) * count_generic);
        i = 0;
        for (tmp = list; tmp; tmp = tmp->next) {
                const char *mname, *iname;
                gchar *name;
-               MonoMethod *m = tmp->data;
+               MonoMethod *m = (MonoMethod *)tmp->data;
                const char *ireadonlylist_prefix = "InternalArray__IReadOnlyList_";
                const char *ireadonlycollection_prefix = "InternalArray__IReadOnlyCollection_";
 
@@ -4982,7 +4986,7 @@ generic_array_methods (MonoClass *klass)
                        g_assert_not_reached ();
                }
 
-               name = mono_image_alloc (mono_defaults.corlib, strlen (iname) + strlen (mname) + 1);
+               name = (gchar *)mono_image_alloc (mono_defaults.corlib, strlen (iname) + strlen (mname) + 1);
                strcpy (name, iname);
                strcpy (name + strlen (iname), mname);
                generic_array_method_info [i].name = name;
@@ -5021,7 +5025,7 @@ concat_two_strings_with_zero (MonoImage *image, const char *s1, const char *s2)
 {
        int null_length = strlen ("(null)");
        int len = (s1 ? strlen (s1) : null_length) + (s2 ? strlen (s2) : null_length) + 2;
-       char *s = mono_image_alloc (image, len);
+       char *s = (char *)mono_image_alloc (image, len);
        int result;
 
        result = g_snprintf (s, len, "%s%c%s", s1 ? s1 : "(null)", '\0', s2 ? s2 : "(null)");
@@ -5449,11 +5453,14 @@ mono_class_setup_mono_type (MonoClass *klass)
                        klass->valuetype = 0;
                        klass->enumtype = 0;
                } else if (!strcmp (name, "Object")) {
-                       klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_OBJECT;
+                       klass->byval_arg.type = MONO_TYPE_OBJECT;
+                       klass->this_arg.type = MONO_TYPE_OBJECT;
                } else if (!strcmp (name, "String")) {
-                       klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_STRING;
+                       klass->byval_arg.type = MONO_TYPE_STRING;
+                       klass->this_arg.type = MONO_TYPE_STRING;
                } else if (!strcmp (name, "TypedReference")) {
-                       klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_TYPEDBYREF;
+                       klass->byval_arg.type = MONO_TYPE_TYPEDBYREF;
+                       klass->this_arg.type = MONO_TYPE_TYPEDBYREF;
                }
        }
 
@@ -5535,7 +5542,8 @@ mono_class_setup_mono_type (MonoClass *klass)
                                break;
                        }
                }
-               klass->this_arg.type = klass->byval_arg.type = t;
+               klass->byval_arg.type = (MonoTypeEnum)t;
+               klass->this_arg.type = (MonoTypeEnum)t;
        }
 
        if (MONO_CLASS_IS_INTERFACE (klass))
@@ -5677,7 +5685,7 @@ mono_class_setup_supertypes (MonoClass *klass)
        int ms;
        MonoClass **supertypes;
 
-       mono_atomic_load_acquire (supertypes, void*, &klass->supertypes);
+       mono_atomic_load_acquire (supertypes, MonoClass **, &klass->supertypes);
        if (supertypes)
                return;
 
@@ -5689,16 +5697,19 @@ mono_class_setup_supertypes (MonoClass *klass)
                klass->idepth = 1;
 
        ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, klass->idepth);
-       supertypes = mono_class_alloc0 (klass, sizeof (MonoClass *) * ms);
+       supertypes = (MonoClass **)mono_class_alloc0 (klass, sizeof (MonoClass *) * ms);
 
        if (klass->parent) {
-               supertypes [klass->idepth - 1] = klass;
-               memcpy (supertypes, klass->parent->supertypes, klass->parent->idepth * sizeof (gpointer));
+               CHECKED_METADATA_WRITE_PTR ( supertypes [klass->idepth - 1] , klass );
+
+               int supertype_idx;
+               for (supertype_idx = 0; supertype_idx < klass->parent->idepth; supertype_idx++)
+                       CHECKED_METADATA_WRITE_PTR ( supertypes [supertype_idx] , klass->parent->supertypes [supertype_idx] );
        } else {
-               supertypes [0] = klass;
+               CHECKED_METADATA_WRITE_PTR ( supertypes [0] , klass );
        }
 
-       mono_atomic_store_release (&klass->supertypes, supertypes);
+       CHECKED_METADATA_WRITE_PTR_ATOMIC ( klass->supertypes , supertypes );
 }
 
 static gboolean
@@ -5775,7 +5786,7 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token, MonoError
 
        mono_loader_lock ();
 
-       if ((klass = mono_internal_hash_table_lookup (&image->class_cache, GUINT_TO_POINTER (type_token)))) {
+       if ((klass = (MonoClass *)mono_internal_hash_table_lookup (&image->class_cache, GUINT_TO_POINTER (type_token)))) {
                mono_loader_unlock ();
                mono_loader_assert_no_error ();
                return klass;
@@ -5786,7 +5797,7 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token, MonoError
        name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
        nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
 
-       klass = mono_image_alloc0 (image, sizeof (MonoClass));
+       klass = (MonoClass *)mono_image_alloc0 (image, sizeof (MonoClass));
 
        klass->name = name;
        klass->name_space = nspace;
@@ -5808,6 +5819,7 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token, MonoError
        if (klass->generic_container) {
                klass->is_generic = 1;
                klass->generic_container->owner.klass = klass;
+               klass->generic_container->is_anonymous = FALSE; // Owner class is now known, container is no longer anonymous
                context = &klass->generic_container->context;
        }
 
@@ -5977,7 +5989,7 @@ parent_failure:
        return NULL;
 }
 
-/** is klass Nullable<T>? */
+/** Is klass a Nullable<T> ginst? */
 gboolean
 mono_class_is_nullable (MonoClass *klass)
 {
@@ -6037,7 +6049,7 @@ mono_generic_class_get_class (MonoGenericClass *gclass)
                return gclass->cached_class;
        }
 
-       klass = mono_image_set_alloc0 (gclass->owner, sizeof (MonoClass));
+       klass = (MonoClass *)mono_image_set_alloc0 (gclass->owner, sizeof (MonoClass));
 
        gklass = gclass->container_class;
 
@@ -6062,7 +6074,8 @@ mono_generic_class_get_class (MonoGenericClass *gclass)
        klass->is_inflated = 1;
        klass->generic_class = gclass;
 
-       klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
+       klass->byval_arg.type = MONO_TYPE_GENERICINST;
+       klass->this_arg.type = klass->byval_arg.type;
        klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = gclass;
        klass->this_arg.byref = TRUE;
        klass->enumtype = gklass->enumtype;
@@ -6117,42 +6130,83 @@ mono_generic_class_get_class (MonoGenericClass *gclass)
        return klass;
 }
 
+static MonoImage *
+get_image_for_container (MonoGenericContainer *container)
+{
+       MonoImage *result;
+       if (container->is_anonymous) {
+               result = container->owner.image;
+       } else {
+               MonoClass *klass;
+               if (container->is_method) {
+                       MonoMethod *method = container->owner.method;
+                       g_assert_checked (method);
+                       klass = method->klass;
+               } else {
+                       klass = container->owner.klass;
+               }
+               g_assert_checked (klass);
+               result = klass->image;
+       }
+       g_assert (result);
+       return result;
+}
+
+MonoImage *
+get_image_for_generic_param (MonoGenericParam *param)
+{
+       MonoGenericContainer *container = mono_generic_param_owner (param);
+       g_assert_checked (container);
+       return get_image_for_container (container);
+}
+
+// Make a string in the designated image consisting of a single integer.
+#define INT_STRING_SIZE 16
+char *
+make_generic_name_string (MonoImage *image, int num)
+{
+       char *name = (char *)mono_image_alloc0 (image, INT_STRING_SIZE);
+       g_snprintf (name, INT_STRING_SIZE, "%d", num);
+       return name;
+}
+
+// This is called by mono_class_from_generic_parameter_internal when a new class must be created.
+// pinfo is derived from param by the caller for us.
 static MonoClass*
-make_generic_param_class (MonoGenericParam *param, MonoImage *image, gboolean is_mvar, MonoGenericParamInfo *pinfo)
+make_generic_param_class (MonoGenericParam *param, MonoGenericParamInfo *pinfo)
 {
        MonoClass *klass, **ptr;
        int count, pos, i;
        MonoGenericContainer *container = mono_generic_param_owner (param);
+       g_assert_checked (container);
 
-       if (!image)
-               /* FIXME: */
-               image = mono_defaults.corlib;
+       MonoImage *image = get_image_for_container (container);
+       gboolean is_mvar = container->is_method;
+       gboolean is_anonymous = container->is_anonymous;
 
-       klass = mono_image_alloc0 (image, sizeof (MonoClass));
+       klass = (MonoClass *)mono_image_alloc0 (image, sizeof (MonoClass));
        classes_size += sizeof (MonoClass);
 
        if (pinfo) {
-               klass->name = pinfo->name;
+               CHECKED_METADATA_WRITE_PTR_EXEMPT ( klass->name , pinfo->name );
        } else {
                int n = mono_generic_param_num (param);
-               klass->name = mono_image_alloc0 (image, 16);
-               sprintf ((char*)klass->name, "%d", n);
+               CHECKED_METADATA_WRITE_PTR_LOCAL ( klass->name , make_generic_name_string (image, n) );
        }
 
-       if (container) {
-               if (is_mvar) {
-                       MonoMethod *omethod = container->owner.method;
-                       klass->name_space = (omethod && omethod->klass) ? omethod->klass->name_space : "";
-               } else {
-                       MonoClass *oklass = container->owner.klass;
-                       klass->name_space = oklass ? oklass->name_space : "";
-               }
+       if (is_anonymous) {
+               CHECKED_METADATA_WRITE_PTR_EXEMPT ( klass->name_space ,  "" );
+       } else if (is_mvar) {
+               MonoMethod *omethod = container->owner.method;
+               CHECKED_METADATA_WRITE_PTR_EXEMPT ( klass->name_space , (omethod && omethod->klass) ? omethod->klass->name_space : "" );
        } else {
-               klass->name_space = "";
+               MonoClass *oklass = container->owner.klass;
+               CHECKED_METADATA_WRITE_PTR_EXEMPT ( klass->name_space , oklass ? oklass->name_space : "" );
        }
 
        mono_profiler_class_event (klass, MONO_PROFILE_START_LOAD);
 
+       // Count non-NULL items in pinfo->constraints
        count = 0;
        if (pinfo)
                for (ptr = pinfo->constraints; ptr && *ptr; ptr++, count++)
@@ -6160,30 +6214,33 @@ make_generic_param_class (MonoGenericParam *param, MonoImage *image, gboolean is
 
        pos = 0;
        if ((count > 0) && !MONO_CLASS_IS_INTERFACE (pinfo->constraints [0])) {
-               klass->parent = pinfo->constraints [0];
+               CHECKED_METADATA_WRITE_PTR ( klass->parent , pinfo->constraints [0] );
                pos++;
-       } else if (pinfo && pinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT)
-               klass->parent = mono_class_from_name (mono_defaults.corlib, "System", "ValueType");
-       else
-               klass->parent = mono_defaults.object_class;
-
+       } else if (pinfo && pinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) {
+               CHECKED_METADATA_WRITE_PTR ( klass->parent , mono_class_from_name (mono_defaults.corlib, "System", "ValueType") );
+       } else {
+               CHECKED_METADATA_WRITE_PTR ( klass->parent , mono_defaults.object_class );
+       }
 
        if (count - pos > 0) {
                klass->interface_count = count - pos;
-               klass->interfaces = mono_image_alloc0 (image, sizeof (MonoClass *) * (count - pos));
+               CHECKED_METADATA_WRITE_PTR_LOCAL ( klass->interfaces , (MonoClass **)mono_image_alloc0 (image, sizeof (MonoClass *) * (count - pos)) );
                klass->interfaces_inited = TRUE;
                for (i = pos; i < count; i++)
-                       klass->interfaces [i - pos] = pinfo->constraints [i];
+                       CHECKED_METADATA_WRITE_PTR ( klass->interfaces [i - pos] , pinfo->constraints [i] );
        }
 
-       klass->image = image;
+       CHECKED_METADATA_WRITE_PTR_EXEMPT ( klass->image , image );
 
        klass->inited = TRUE;
-       klass->cast_class = klass->element_class = klass;
+       CHECKED_METADATA_WRITE_PTR_LOCAL ( klass->cast_class ,    klass );
+       CHECKED_METADATA_WRITE_PTR_LOCAL ( klass->element_class , klass );
        klass->flags = TYPE_ATTRIBUTE_PUBLIC;
 
-       klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
-       klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
+       klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
+       klass->this_arg.type = klass->byval_arg.type;
+       CHECKED_METADATA_WRITE_PTR ( klass->this_arg.data.generic_param ,  param );
+       CHECKED_METADATA_WRITE_PTR ( klass->byval_arg.data.generic_param , param );
        klass->this_arg.byref = TRUE;
 
        /* We don't use type_token for VAR since only classes can use it (not arrays, pointer, VARs, etc) */
@@ -6216,24 +6273,31 @@ make_generic_param_class (MonoGenericParam *param, MonoImage *image, gboolean is
 #define FAST_CACHE_SIZE 16
 
 /*
+ * get_anon_gparam_class and set_anon_gparam_class are helpers for mono_class_from_generic_parameter_internal.
+ * The latter will sometimes create MonoClasses for anonymous generic params. To prevent this being wasteful,
+ * we cache the MonoClasses.
+ * FIXME: It would be better to instead cache anonymous MonoGenericParams, and allow anonymous params to point directly to classes using the pklass field.
  * LOCKING: Takes the image lock depending on @take_lock.
  */
 static MonoClass *
-get_anon_gparam_class (MonoGenericParam *param, gboolean is_mvar, gboolean take_lock)
+get_anon_gparam_class (MonoGenericParam *param, gboolean take_lock)
 {
        int n = mono_generic_param_num (param);
-       MonoImage *image = param->image;
+       MonoImage *image = get_image_for_generic_param (param);
+       gboolean is_mvar = mono_generic_param_owner (param)->is_method;
        MonoClass *klass = NULL;
        GHashTable *ht;
 
        g_assert (image);
 
+       // For params with a small num and no constraints, we use a "fast" cache which does simple num lookup in an array.
+       // For high numbers or constraints we have to use pointer hashes.
        if (param->gshared_constraint) {
                ht = is_mvar ? image->mvar_cache_constrained : image->var_cache_constrained;
                if (ht) {
                        if (take_lock)
                                mono_image_lock (image);
-                       klass = g_hash_table_lookup (ht, param);
+                       klass = (MonoClass *)g_hash_table_lookup (ht, param);
                        if (take_lock)
                                mono_image_unlock (image);
                }
@@ -6250,7 +6314,7 @@ get_anon_gparam_class (MonoGenericParam *param, gboolean is_mvar, gboolean take_
                if (ht) {
                        if (take_lock)
                                mono_image_lock (image);
-                       klass = g_hash_table_lookup (ht, GINT_TO_POINTER (n));
+                       klass = (MonoClass *)g_hash_table_lookup (ht, GINT_TO_POINTER (n));
                        if (take_lock)
                                mono_image_unlock (image);
                }
@@ -6262,10 +6326,11 @@ get_anon_gparam_class (MonoGenericParam *param, gboolean is_mvar, gboolean take_
  * LOCKING: Image lock (param->image) must be held
  */
 static void
-set_anon_gparam_class (MonoGenericParam *param, gboolean is_mvar, MonoClass *klass)
+set_anon_gparam_class (MonoGenericParam *param, MonoClass *klass)
 {
        int n = mono_generic_param_num (param);
-       MonoImage *image = param->image;
+       MonoImage *image = get_image_for_generic_param (param);
+       gboolean is_mvar = mono_generic_param_owner (param)->is_method;
 
        g_assert (image);
 
@@ -6284,11 +6349,11 @@ set_anon_gparam_class (MonoGenericParam *param, gboolean is_mvar, MonoClass *kla
                if (is_mvar) {
                        /* Requires locking to avoid droping an already published class */
                        if (!image->mvar_cache_fast)
-                               image->mvar_cache_fast = mono_image_alloc0 (image, sizeof (MonoClass*) * FAST_CACHE_SIZE);
+                               image->mvar_cache_fast = (MonoClass **)mono_image_alloc0 (image, sizeof (MonoClass*) * FAST_CACHE_SIZE);
                        image->mvar_cache_fast [n] = klass;
                } else {
                        if (!image->var_cache_fast)
-                               image->var_cache_fast = mono_image_alloc0 (image, sizeof (MonoClass*) * FAST_CACHE_SIZE);
+                               image->var_cache_fast = (MonoClass **)mono_image_alloc0 (image, sizeof (MonoClass*) * FAST_CACHE_SIZE);
                        image->var_cache_fast [n] = klass;
                }
        } else {
@@ -6312,66 +6377,71 @@ set_anon_gparam_class (MonoGenericParam *param, gboolean is_mvar, MonoClass *kla
  * LOCKING: Acquires the image lock (@image).
  */
 MonoClass *
-mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
+mono_class_from_generic_parameter_internal (MonoGenericParam *param)
 {
-       MonoGenericContainer *container = mono_generic_param_owner (param);
-       MonoGenericParamInfo *pinfo = NULL;
+       MonoImage *image = get_image_for_generic_param (param);
+       MonoGenericParamInfo *pinfo = mono_generic_param_info (param);
        MonoClass *klass, *klass2;
 
-       if (container) {
-               pinfo = mono_generic_param_info (param);
+       // If a klass already exists for this object and is cached, return it.
+       if (pinfo) // Non-anonymous
                klass = pinfo->pklass;
-       } else {
-               image = NULL;
-               klass = get_anon_gparam_class (param, is_mvar, TRUE);
-       }
+       else     // Anonymous
+               klass = get_anon_gparam_class (param, TRUE);
+
        if (klass)
                return klass;
 
-       if (!image && container) {
-               if (is_mvar) {
-                       MonoMethod *method = container->owner.method;
-                       image = (method && method->klass) ? method->klass->image : NULL;
-               } else {
-                       MonoClass *klass = container->owner.klass;
-                       // FIXME: 'klass' should not be null
-                       //        But, monodis creates GenericContainers without associating a owner to it
-                       image = klass ? klass->image : NULL;
-               }
-       }
-
-       klass = make_generic_param_class (param, image, is_mvar, pinfo);
+       // Create a new klass
+       klass = make_generic_param_class (param, pinfo);
 
+       // Now we need to cache the klass we created.
+       // But since we wait to grab the lock until after creating the klass, we need to check to make sure
+       // another thread did not get in and cache a klass ahead of us. In that case, return their klass
+       // and allow our newly-created klass object to just leak.
        mono_memory_barrier ();
 
-       if (!image) //FIXME is this only needed by monodis? Can't we fix monodis instead of having this hack?
-               image = mono_defaults.corlib;
-
        mono_image_lock (image);
-       if (container)
+
+    // Here "klass2" refers to the klass potentially created by the other thread.
+       if (pinfo) // Repeat check from above
                klass2 = pinfo->pklass;
        else
-               klass2 = get_anon_gparam_class (param, is_mvar, FALSE);
+               klass2 = get_anon_gparam_class (param, FALSE);
 
        if (klass2) {
                klass = klass2;
        } else {
-               if (container)
+               // Cache here
+               if (pinfo)
                        pinfo->pklass = klass;
                else
-                       set_anon_gparam_class (param, is_mvar, klass);
+                       set_anon_gparam_class (param, klass);
        }
        mono_image_unlock (image);
 
        /* FIXME: Should this go inside 'make_generic_param_klass'? */
        if (klass2)
-               mono_profiler_class_loaded (klass2, MONO_PROFILE_FAILED);
+               mono_profiler_class_loaded (klass2, MONO_PROFILE_FAILED); // Alert profiler about botched class create
        else
                mono_profiler_class_loaded (klass, MONO_PROFILE_OK);
 
        return klass;
 }
 
+/**
+ * mono_class_from_generic_parameter:
+ * @param: Parameter to find/construct a class for.
+ * @arg2: Is ignored.
+ * @arg3: Is ignored.
+ */
+MonoClass *
+mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *arg2 G_GNUC_UNUSED, gboolean arg3 G_GNUC_UNUSED)
+{
+       return mono_class_from_generic_parameter_internal (param);
+}
+
+
 MonoClass *
 mono_ptr_class_get (MonoType *type)
 {
@@ -6385,14 +6455,14 @@ mono_ptr_class_get (MonoType *type)
 
        mono_image_lock (image);
        if (image->ptr_cache) {
-               if ((result = g_hash_table_lookup (image->ptr_cache, el_class))) {
+               if ((result = (MonoClass *)g_hash_table_lookup (image->ptr_cache, el_class))) {
                        mono_image_unlock (image);
                        return result;
                }
        }
        mono_image_unlock (image);
        
-       result = mono_image_alloc0 (image, sizeof (MonoClass));
+       result = (MonoClass *)mono_image_alloc0 (image, sizeof (MonoClass));
 
        classes_size += sizeof (MonoClass);
 
@@ -6412,7 +6482,8 @@ mono_ptr_class_get (MonoType *type)
        result->cast_class = result->element_class = el_class;
        result->blittable = TRUE;
 
-       result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
+       result->byval_arg.type = MONO_TYPE_PTR;
+       result->this_arg.type = result->byval_arg.type;
        result->this_arg.data.type = result->byval_arg.data.type = &result->element_class->byval_arg;
        result->this_arg.byref = TRUE;
 
@@ -6421,7 +6492,7 @@ mono_ptr_class_get (MonoType *type)
        mono_image_lock (image);
        if (image->ptr_cache) {
                MonoClass *result2;
-               if ((result2 = g_hash_table_lookup (image->ptr_cache, el_class))) {
+               if ((result2 = (MonoClass *)g_hash_table_lookup (image->ptr_cache, el_class))) {
                        mono_image_unlock (image);
                        mono_profiler_class_loaded (result, MONO_PROFILE_FAILED);
                        return result2;
@@ -6450,7 +6521,7 @@ mono_fnptr_class_get (MonoMethodSignature *sig)
        if (!ptr_hash)
                ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
        
-       if ((result = g_hash_table_lookup (ptr_hash, sig))) {
+       if ((result = (MonoClass *)g_hash_table_lookup (ptr_hash, sig))) {
                mono_loader_unlock ();
                return result;
        }
@@ -6470,7 +6541,8 @@ mono_fnptr_class_get (MonoMethodSignature *sig)
        result->cast_class = result->element_class = result;
        result->blittable = TRUE;
 
-       result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
+       result->byval_arg.type = MONO_TYPE_FNPTR;
+       result->this_arg.type = result->byval_arg.type;
        result->this_arg.data.method = result->byval_arg.data.method = sig;
        result->this_arg.byref = TRUE;
        result->blittable = TRUE;
@@ -6486,6 +6558,12 @@ mono_fnptr_class_get (MonoMethodSignature *sig)
        return result;
 }
 
+/**
+ * mono_class_from_mono_type:
+ * @type: describes the type to return
+ *
+ * This returns a MonoClass for the specified MonoType, the value is never NULL.
+ */
 MonoClass *
 mono_class_from_mono_type (MonoType *type)
 {
@@ -6539,15 +6617,16 @@ mono_class_from_mono_type (MonoType *type)
                return type->data.klass;
        case MONO_TYPE_GENERICINST:
                return mono_generic_class_get_class (type->data.generic_class);
-       case MONO_TYPE_VAR:
-               return mono_class_from_generic_parameter (type->data.generic_param, NULL, FALSE);
        case MONO_TYPE_MVAR:
-               return mono_class_from_generic_parameter (type->data.generic_param, NULL, TRUE);
+       case MONO_TYPE_VAR:
+               return mono_class_from_generic_parameter_internal (type->data.generic_param);
        default:
                g_warning ("mono_class_from_mono_type: implement me 0x%02x\n", type->type);
                g_assert_not_reached ();
        }
-       
+
+       // Yes, this returns NULL, even if it is documented as not doing so, but there
+       // is no way for the code to make it this far, due to the assert above.
        return NULL;
 }
 
@@ -6595,8 +6674,7 @@ mono_class_create_from_typespec (MonoImage *image, guint32 type_spec, MonoGeneri
        MonoClass *ret;
        gboolean inflated = FALSE;
        MonoType *t = mono_type_retrieve_from_typespec (image, type_spec, context, &inflated, error);
-       if (!mono_error_ok (error))
-               return NULL;
+       return_val_if_nok (error, NULL);
        ret = mono_class_from_mono_type (t);
        if (inflated)
                mono_metadata_free_type (t);
@@ -6609,7 +6687,7 @@ mono_class_create_from_typespec (MonoImage *image, guint32 type_spec, MonoGeneri
  * @rank: the dimension of the array class
  * @bounded: whenever the array has non-zero bounds
  *
- * Returns: a class object describing the array with element type @element_type and 
+ * Returns: A class object describing the array with element type @element_type and 
  * dimension @rank. 
  */
 MonoClass *
@@ -6640,7 +6718,7 @@ mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
                mono_os_mutex_lock (&image->szarray_cache_lock);
                if (!image->szarray_cache)
                        image->szarray_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
-               klass = g_hash_table_lookup (image->szarray_cache, eclass);
+               klass = (MonoClass *)g_hash_table_lookup (image->szarray_cache, eclass);
                mono_os_mutex_unlock (&image->szarray_cache_lock);
                if (klass)
                        return klass;
@@ -6652,9 +6730,9 @@ mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
                if (!image->array_cache)
                        image->array_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
 
-               if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
+               if ((rootlist = list = (GSList *)g_hash_table_lookup (image->array_cache, eclass))) {
                        for (; list; list = list->next) {
-                               klass = list->data;
+                               klass = (MonoClass *)list->data;
                                if ((klass->rank == rank) && (klass->byval_arg.type == (((rank > 1) || bounded) ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
                                        mono_loader_unlock ();
                                        return klass;
@@ -6673,12 +6751,12 @@ mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
                        mono_class_init (parent);
        }
 
-       klass = mono_image_alloc0 (image, sizeof (MonoClass));
+       klass = (MonoClass *)mono_image_alloc0 (image, sizeof (MonoClass));
 
        klass->image = image;
        klass->name_space = eclass->name_space;
        nsize = strlen (eclass->name);
-       name = g_malloc (nsize + 2 + rank + 1);
+       name = (char *)g_malloc (nsize + 2 + rank + 1);
        memcpy (name, eclass->name, nsize);
        name [nsize] = '[';
        if (rank > 1)
@@ -6759,7 +6837,7 @@ mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
        klass->element_class = eclass;
 
        if ((rank > 1) || bounded) {
-               MonoArrayType *at = mono_image_alloc0 (image, sizeof (MonoArrayType));
+               MonoArrayType *at = (MonoArrayType *)mono_image_alloc0 (image, sizeof (MonoArrayType));
                klass->byval_arg.type = MONO_TYPE_ARRAY;
                klass->byval_arg.data.array = at;
                at->eklass = eclass;
@@ -6781,7 +6859,7 @@ mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
                MonoClass *prev_class;
 
                mono_os_mutex_lock (&image->szarray_cache_lock);
-               prev_class = g_hash_table_lookup (image->szarray_cache, eclass);
+               prev_class = (MonoClass *)g_hash_table_lookup (image->szarray_cache, eclass);
                if (prev_class)
                        /* Someone got in before us */
                        klass = prev_class;
@@ -6805,7 +6883,7 @@ mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
  * @element_class: element class 
  * @rank: the dimension of the array class
  *
- * Returns: a class object describing the array with element type @element_type and 
+ * Returns: A class object describing the array with element type @element_type and 
  * dimension @rank. 
  */
 MonoClass *
@@ -6817,8 +6895,10 @@ mono_array_class_get (MonoClass *eclass, guint32 rank)
 /**
  * mono_class_instance_size:
  * @klass: a class 
- * 
- * Returns: the size of an object instance
+ *
+ * Use to get the size of a class in bytes.
+ *
+ * Returns: The size of an object instance
  */
 gint32
 mono_class_instance_size (MonoClass *klass)
@@ -6832,7 +6912,9 @@ mono_class_instance_size (MonoClass *klass)
 /**
  * mono_class_min_align:
  * @klass: a class 
- * 
+ *
+ * Use to get the computed minimum alignment requirements for the specified class.
+ *
  * Returns: minimm alignment requirements 
  */
 gint32
@@ -6875,7 +6957,7 @@ mono_class_value_size      (MonoClass *klass, guint32 *align)
  * mono_class_data_size:
  * @klass: a class 
  * 
- * Returns: the size of the static class data
+ * Returns: The size of the static class data
  */
 gint32
 mono_class_data_size (MonoClass *klass)
@@ -6958,7 +7040,7 @@ mono_class_get_field (MonoClass *klass, guint32 field_token)
  *
  * Search the class @klass and it's parents for a field with the name @name.
  * 
- * Returns: the MonoClassField pointer of the named field or NULL
+ * Returns: The MonoClassField pointer of the named field or NULL
  */
 MonoClassField *
 mono_class_get_field_from_name (MonoClass *klass, const char *name)
@@ -6977,7 +7059,7 @@ mono_class_get_field_from_name (MonoClass *klass, const char *name)
  * If @klass is an inflated generic type, the type comparison is done with the equivalent field
  * of its generic type definition.
  *
- * Returns: the MonoClassField pointer of the named field or NULL
+ * Returns: The MonoClassField pointer of the named field or NULL
  */
 MonoClassField *
 mono_class_get_field_from_name_full (MonoClass *klass, const char *name, MonoType *type)
@@ -7014,7 +7096,7 @@ mono_class_get_field_from_name_full (MonoClass *klass, const char *name, MonoTyp
  * Get the token of a field. Note that the tokesn is only valid for the image
  * the field was loaded from. Don't use this function for fields in dynamic types.
  * 
- * Returns: the token representing the field in the image it was loaded from.
+ * Returns: The token representing the field in the image it was loaded from.
  */
 guint32
 mono_class_get_field_token (MonoClassField *field)
@@ -7073,7 +7155,7 @@ mono_class_get_field_default_value (MonoClassField *field, MonoTypeEnum *def_typ
 
                mono_class_alloc_ext (klass);
 
-               def_values = mono_class_alloc0 (klass, sizeof (MonoFieldDefaultValue) * klass->field.count);
+               def_values = (MonoFieldDefaultValue *)mono_class_alloc0 (klass, sizeof (MonoFieldDefaultValue) * klass->field.count);
 
                mono_image_lock (klass->image);
                mono_memory_barrier ();
@@ -7092,8 +7174,8 @@ mono_class_get_field_default_value (MonoClassField *field, MonoTypeEnum *def_typ
                g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA));
 
                mono_metadata_decode_row (&field->parent->image->tables [MONO_TABLE_CONSTANT], cindex - 1, constant_cols, MONO_CONSTANT_SIZE);
-               klass->ext->field_def_values [field_index].def_type = constant_cols [MONO_CONSTANT_TYPE];
-               klass->ext->field_def_values [field_index].data = (gpointer)mono_metadata_blob_heap (field->parent->image, constant_cols [MONO_CONSTANT_VALUE]);
+               klass->ext->field_def_values [field_index].def_type = (MonoTypeEnum)constant_cols [MONO_CONSTANT_TYPE];
+               klass->ext->field_def_values [field_index].data = (const char *)mono_metadata_blob_heap (field->parent->image, constant_cols [MONO_CONSTANT_VALUE]);
        }
 
        *def_type = klass->ext->field_def_values [field_index].def_type;
@@ -7142,8 +7224,8 @@ mono_class_get_property_default_value (MonoProperty *property, MonoTypeEnum *def
                return NULL;
 
        mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_CONSTANT], cindex - 1, constant_cols, MONO_CONSTANT_SIZE);
-       *def_type = constant_cols [MONO_CONSTANT_TYPE];
-       return (gpointer)mono_metadata_blob_heap (klass->image, constant_cols [MONO_CONSTANT_VALUE]);
+       *def_type = (MonoTypeEnum)constant_cols [MONO_CONSTANT_TYPE];
+       return (const char *)mono_metadata_blob_heap (klass->image, constant_cols [MONO_CONSTANT_VALUE]);
 }
 
 guint32
@@ -7166,6 +7248,15 @@ mono_class_get_event_token (MonoEvent *event)
        return 0;
 }
 
+/**
+ * mono_class_get_property_from_name:
+ * @klass: a class
+ * @name: name of the property to lookup in the specified class
+ *
+ * Use this method to lookup a property in a class
+ * Returns: the MonoProperty with the given name, or NULL if the property
+ * does not exist on the @klass.
+ */
 MonoProperty*
 mono_class_get_property_from_name (MonoClass *klass, const char *name)
 {
@@ -7181,6 +7272,12 @@ mono_class_get_property_from_name (MonoClass *klass, const char *name)
        return NULL;
 }
 
+/**
+ * mono_class_get_property_token:
+ * @prop: MonoProperty to query
+ *
+ * Returns: The ECMA token for the specified property.
+ */
 guint32
 mono_class_get_property_token (MonoProperty *prop)
 {
@@ -7324,7 +7421,7 @@ mono_assembly_name_from_token (MonoImage *image, guint32 type_token)
  * @context: the generic context used to evaluate generic instantiations in
  * @deprecated: Functions that expose MonoGenericContext are going away in mono 4.0
  *
- * Returns: the MonoClass that represents @type_token in @image
+ * Returns: The MonoClass that represents @type_token in @image
  */
 MonoClass *
 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
@@ -7360,7 +7457,7 @@ mono_class_get_and_inflate_typespec_checked (MonoImage *image, guint32 type_toke
  * @type_token: the token for the class
  * @error: error object to return any error
  *
- * Returns: the MonoClass that represents @type_token in @image
+ * Returns: The MonoClass that represents @type_token in @image, or NULL on error.
  */
 MonoClass *
 mono_class_get_checked (MonoImage *image, guint32 type_token, MonoError *error)
@@ -7376,7 +7473,7 @@ mono_class_get_checked (MonoImage *image, guint32 type_token, MonoError *error)
                        mono_error_set_bad_image (error, image,"Bad token table for dynamic image: %x", table);
                        return NULL;
                }
-               klass = mono_lookup_dynamic_token (image, type_token, NULL); /*FIXME proper error handling*/
+               klass = (MonoClass *)mono_lookup_dynamic_token (image, type_token, NULL); /*FIXME proper error handling*/
                goto done;
        }
 
@@ -7415,7 +7512,7 @@ done:
  *
  * This functions exists to fullfill the fact that sometimes it's desirable to have access to the 
  * 
- * Returns: the MonoType that represents @type_token in @image
+ * Returns: The MonoType that represents @type_token in @image
  */
 MonoType *
 mono_type_get_checked (MonoImage *image, guint32 type_token, MonoGenericContext *context, MonoError *error)
@@ -7427,7 +7524,7 @@ mono_type_get_checked (MonoImage *image, guint32 type_token, MonoGenericContext
 
        //FIXME: this will not fix the very issue for which mono_type_get_full exists -but how to do it then?
        if (image_is_dynamic (image))
-               return mono_class_get_type (mono_lookup_dynamic_token (image, type_token, context));
+               return mono_class_get_type ((MonoClass *)mono_lookup_dynamic_token (image, type_token, context));
 
        if ((type_token & 0xff000000) != MONO_TOKEN_TYPE_SPEC) {
                MonoClass *klass = mono_class_get_checked (image, type_token, error);
@@ -7465,7 +7562,13 @@ mono_type_get_checked (MonoImage *image, guint32 type_token, MonoGenericContext
        return type;
 }
 
-
+/**
+ * mono_class_get:
+ * @image: image where the class token will be looked up.
+ * @type_token: a type token from the image
+ *
+ * Returns the MonoClass with the given @type_token on the @image
+ */
 MonoClass *
 mono_class_get (MonoImage *image, guint32 type_token)
 {
@@ -7522,7 +7625,7 @@ mono_image_init_name_cache (MonoImage *image)
                nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
 
                nspace_index = cols [MONO_TYPEDEF_NAMESPACE];
-               nspace_table = g_hash_table_lookup (name_cache2, GUINT_TO_POINTER (nspace_index));
+               nspace_table = (GHashTable *)g_hash_table_lookup (name_cache2, GUINT_TO_POINTER (nspace_index));
                if (!nspace_table) {
                        nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
                        g_hash_table_insert (the_name_cache, (char*)nspace, nspace_table);
@@ -7550,7 +7653,7 @@ mono_image_init_name_cache (MonoImage *image)
                        nspace = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAMESPACE]);
 
                        nspace_index = cols [MONO_EXP_TYPE_NAMESPACE];
-                       nspace_table = g_hash_table_lookup (name_cache2, GUINT_TO_POINTER (nspace_index));
+                       nspace_table = (GHashTable *)g_hash_table_lookup (name_cache2, GUINT_TO_POINTER (nspace_index));
                        if (!nspace_table) {
                                nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
                                g_hash_table_insert (the_name_cache, (char*)nspace, nspace_table);
@@ -7586,7 +7689,7 @@ mono_image_add_to_name_cache (MonoImage *image, const char *nspace,
        mono_image_lock (image);
 
        name_cache = image->name_cache;
-       if (!(nspace_table = g_hash_table_lookup (name_cache, nspace))) {
+       if (!(nspace_table = (GHashTable *)g_hash_table_lookup (name_cache, nspace))) {
                nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
                g_hash_table_insert (name_cache, (char *)nspace, (char *)nspace_table);
        }
@@ -7619,7 +7722,7 @@ find_nocase (gpointer key, gpointer value, gpointer user_data)
  * @image: The MonoImage where the type is looked up in
  * @name_space: the type namespace
  * @name: the type short name.
- * @deprecated: use the _checked variant
+ * @deprecated: use the mono_class_from_name_case_checked variant instead.
  *
  * Obtains a MonoClass with a given namespace and a given name which
  * is located in the given MonoImage.   The namespace and name
@@ -7634,8 +7737,23 @@ mono_class_from_name_case (MonoImage *image, const char* name_space, const char
        return res;
 }
 
+/**
+ * mono_class_from_name_case:
+ * @image: The MonoImage where the type is looked up in
+ * @name_space: the type namespace
+ * @name: the type short name.
+ * @error: if 
+ *
+ * Obtains a MonoClass with a given namespace and a given name which
+ * is located in the given MonoImage.   The namespace and name
+ * lookups are case insensitive.
+ *
+ * Returns: The MonoClass if the given namespace and name were found, or NULL if it
+ * was not found.   The @error object will contain information about the problem
+ * in that case.
+ */
 MonoClass *
-mono_class_from_name_case_checked (MonoImage *image, const charname_space, const char *name, MonoError *error)
+mono_class_from_name_case_checked (MonoImage *image, const char *name_space, const char *name, MonoError *error)
 {
        MonoTableInfo  *t = &image->tables [MONO_TABLE_TYPEDEF];
        guint32 cols [MONO_TYPEDEF_SIZE];
@@ -7794,7 +7912,7 @@ mono_class_from_name_checked_aux (MonoImage *image, const char* name_space, cons
        mono_image_init_name_cache (image);
        mono_image_lock (image);
 
-       nspace_table = g_hash_table_lookup (image->name_cache, name_space);
+       nspace_table = (GHashTable *)g_hash_table_lookup (image->name_cache, name_space);
 
        if (nspace_table)
                token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
@@ -7916,11 +8034,11 @@ mono_class_from_name (MonoImage *image, const char* name_space, const char *name
  * This method determines whether @klass is a subclass of @klassc.
  *
  * If the @check_interfaces flag is set, then if @klassc is an interface
- * this method return true if the @klass implements the interface or
+ * this method return TRUE if the @klass implements the interface or
  * if @klass is an interface, if one of its base classes is @klass.
  *
  * If @check_interfaces is false then, then if @klass is not an interface
- * then it returns true if the @klass is a subclass of @klassc.
+ * then it returns TRUE if the @klass is a subclass of @klassc.
  *
  * if @klass is an interface and @klassc is System.Object, then this function
  * return true.
@@ -8007,8 +8125,9 @@ mono_gparam_is_reference_conversible (MonoClass *target, MonoClass *candidate, g
  * @klass: the class to be assigned to
  * @oklass: the source class
  * 
- * Both klass and oklass must be instances of the same generic interface.
- * Return true if @klass can be assigned to a @klass variable
+ * Both @klass and @oklass must be instances of the same generic interface.
+ *
+ * Returns: TRUE if @klass can be assigned to a @klass variable
  */
 gboolean
 mono_class_is_variant_compatible (MonoClass *klass, MonoClass *oklass, gboolean check_for_reference_conv)
@@ -8159,7 +8278,7 @@ mono_gparam_is_assignable_from (MonoClass *target, MonoClass *candidate)
  * @klass: the class to be assigned to
  * @oklass: the source class
  *
- * Return: true if an instance of object oklass can be assigned to an
+ * Returns: TRUE if an instance of object oklass can be assigned to an
  * instance of object @klass
  */
 gboolean
@@ -8327,7 +8446,7 @@ mono_class_implement_interface_slow (MonoClass *target, MonoClass *candidate)
 
                /*A TypeBuilder can have more interfaces on tb->interfaces than on candidate->interfaces*/
                if (image_is_dynamic (candidate->image) && !candidate->wastypebuilder) {
-                       MonoReflectionTypeBuilder *tb = mono_class_get_ref_info (candidate);
+                       MonoReflectionTypeBuilder *tb = (MonoReflectionTypeBuilder *)mono_class_get_ref_info (candidate);
                        int j;
                        if (tb && tb->interfaces) {
                                for (j = mono_array_length (tb->interfaces) - 1; j >= 0; --j) {
@@ -8433,7 +8552,7 @@ mono_class_is_assignable_from_slow (MonoClass *target, MonoClass *candidate)
  * mono_class_get_cctor:
  * @klass: A MonoClass pointer
  *
- * Returns: the static constructor of @klass if it exists, NULL otherwise.
+ * Returns: The static constructor of @klass if it exists, NULL otherwise.
  */
 MonoMethod*
 mono_class_get_cctor (MonoClass *klass)
@@ -8469,7 +8588,7 @@ mono_class_get_cctor (MonoClass *klass)
  * mono_class_get_finalizer:
  * @klass: The MonoClass pointer
  *
- * Returns: the finalizer method of @klass if it exists, NULL otherwise.
+ * Returns: The finalizer method of @klass if it exists, NULL otherwise.
  */
 MonoMethod*
 mono_class_get_finalizer (MonoClass *klass)
@@ -8517,7 +8636,7 @@ mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
  * mono_class_array_element_size:
  * @klass: 
  *
- * Returns: the number of bytes an element of type @klass
+ * Returns: The number of bytes an element of type @klass
  * uses when stored into an array.
  */
 gint32
@@ -8581,7 +8700,7 @@ handle_enum:
  * mono_array_element_size:
  * @ac: pointer to a #MonoArrayClass
  *
- * Returns: the size of single array element.
+ * Returns: The size of single array element.
  */
 gint32
 mono_array_element_size (MonoClass *ac)
@@ -8738,6 +8857,13 @@ mono_install_get_class_from_name (MonoGetClassFromName func)
        get_class_from_name = func;
 }
 
+/**
+ * mono_class_get_image:
+ *
+ * Use this method to get the `MonoImage*` where this class came from.
+ *
+ * Returns: The image where this class is defined.
+ */
 MonoImage*
 mono_class_get_image (MonoClass *klass)
 {
@@ -8748,7 +8874,9 @@ mono_class_get_image (MonoClass *klass)
  * mono_class_get_element_class:
  * @klass: the MonoClass to act on
  *
- * Returns: the element class of an array or an enumeration.
+ * Use this function to get the element class of an array.
+ *
+ * Returns: The element class of an array.
  */
 MonoClass*
 mono_class_get_element_class (MonoClass *klass)
@@ -8760,7 +8888,10 @@ mono_class_get_element_class (MonoClass *klass)
  * mono_class_is_valuetype:
  * @klass: the MonoClass to act on
  *
- * Returns: true if the MonoClass represents a ValueType.
+ * Use this method to determine if the provided `MonoClass*` represents a value type,
+ * or a reference type.
+ *
+ * Returns: TRUE if the MonoClass represents a ValueType, FALSE if it represents a reference type.
  */
 gboolean
 mono_class_is_valuetype (MonoClass *klass)
@@ -8772,7 +8903,9 @@ mono_class_is_valuetype (MonoClass *klass)
  * mono_class_is_enum:
  * @klass: the MonoClass to act on
  *
- * Returns: true if the MonoClass represents an enumeration.
+ * Use this function to determine if the provided `MonoClass*` represents an enumeration.
+ *
+ * Returns: TRUE if the MonoClass represents an enumeration.
  */
 gboolean
 mono_class_is_enum (MonoClass *klass)
@@ -8784,7 +8917,9 @@ mono_class_is_enum (MonoClass *klass)
  * mono_class_enum_basetype:
  * @klass: the MonoClass to act on
  *
- * Returns: the underlying type representation for an enumeration.
+ * Use this function to get the underlying type for an enumeration value.
+ * 
+ * Returns: The underlying type representation for an enumeration.
  */
 MonoType*
 mono_class_enum_basetype (MonoClass *klass)
@@ -8800,7 +8935,7 @@ mono_class_enum_basetype (MonoClass *klass)
  * mono_class_get_parent
  * @klass: the MonoClass to act on
  *
- * Returns: the parent class for this class.
+ * Returns: The parent class for this class.
  */
 MonoClass*
 mono_class_get_parent (MonoClass *klass)
@@ -8809,10 +8944,14 @@ mono_class_get_parent (MonoClass *klass)
 }
 
 /**
- * mono_class_get_nesting_type;
+ * mono_class_get_nesting_type:
  * @klass: the MonoClass to act on
  *
- * Returns: the container type where this type is nested or NULL if this type is not a nested type.
+ * Use this function to obtain the class that the provided `MonoClass*` is nested on.
+ *
+ * If the return is NULL, this indicates that this class is not nested.
+ *
+ * Returns: The container type where this type is nested or NULL if this type is not a nested type.
  */
 MonoClass*
 mono_class_get_nesting_type (MonoClass *klass)
@@ -8824,7 +8963,7 @@ mono_class_get_nesting_type (MonoClass *klass)
  * mono_class_get_rank:
  * @klass: the MonoClass to act on
  *
- * Returns: the rank for the array (the number of dimensions).
+ * Returns: The rank for the array (the number of dimensions).
  */
 int
 mono_class_get_rank (MonoClass *klass)
@@ -8840,7 +8979,7 @@ mono_class_get_rank (MonoClass *klass)
  * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
  * different values.
  *
- * Returns: the flags from the TypeDef table.
+ * Returns: The flags from the TypeDef table.
  */
 guint32
 mono_class_get_flags (MonoClass *klass)
@@ -8852,7 +8991,7 @@ mono_class_get_flags (MonoClass *klass)
  * mono_class_get_name
  * @klass: the MonoClass to act on
  *
- * Returns: the name of the class.
+ * Returns: The name of the class.
  */
 const char*
 mono_class_get_name (MonoClass *klass)
@@ -8864,7 +9003,7 @@ mono_class_get_name (MonoClass *klass)
  * mono_class_get_namespace:
  * @klass: the MonoClass to act on
  *
- * Returns: the namespace of the class.
+ * Returns: The namespace of the class.
  */
 const char*
 mono_class_get_namespace (MonoClass *klass)
@@ -8878,7 +9017,7 @@ mono_class_get_namespace (MonoClass *klass)
  *
  * This method returns the internal Type representation for the class.
  *
- * Returns: the MonoType from the class.
+ * Returns: The MonoType from the class.
  */
 MonoType*
 mono_class_get_type (MonoClass *klass)
@@ -8887,12 +9026,12 @@ mono_class_get_type (MonoClass *klass)
 }
 
 /**
- * mono_class_get_type_token
+ * mono_class_get_type_token:
  * @klass: the MonoClass to act on
  *
  * This method returns type token for the class.
  *
- * Returns: the type token for the class.
+ * Returns: The type token for the class.
  */
 guint32
 mono_class_get_type_token (MonoClass *klass)
@@ -8916,7 +9055,7 @@ mono_class_get_byref_type (MonoClass *klass)
  * mono_class_num_fields:
  * @klass: the MonoClass to act on
  *
- * Returns: the number of static and instance fields in the class.
+ * Returns: The number of static and instance fields in the class.
  */
 int
 mono_class_num_fields (MonoClass *klass)
@@ -8928,7 +9067,7 @@ mono_class_num_fields (MonoClass *klass)
  * mono_class_num_methods:
  * @klass: the MonoClass to act on
  *
- * Returns: the number of methods in the class.
+ * Returns: The number of methods in the class.
  */
 int
 mono_class_num_methods (MonoClass *klass)
@@ -8940,7 +9079,7 @@ mono_class_num_methods (MonoClass *klass)
  * mono_class_num_properties
  * @klass: the MonoClass to act on
  *
- * Returns: the number of properties in the class.
+ * Returns: The number of properties in the class.
  */
 int
 mono_class_num_properties (MonoClass *klass)
@@ -8954,7 +9093,7 @@ mono_class_num_properties (MonoClass *klass)
  * mono_class_num_events:
  * @klass: the MonoClass to act on
  *
- * Returns: the number of events in the class.
+ * Returns: The number of events in the class.
  */
 int
 mono_class_num_events (MonoClass *klass)
@@ -8988,16 +9127,18 @@ mono_class_get_fields (MonoClass* klass, gpointer *iter)
                        return NULL;
                /* start from the first */
                if (klass->field.count) {
-                       return *iter = &klass->fields [0];
+                       *iter = &klass->fields [0];
+                       return &klass->fields [0];
                } else {
                        /* no fields */
                        return NULL;
                }
        }
-       field = *iter;
+       field = (MonoClassField *)*iter;
        field++;
        if (field < &klass->fields [klass->field.count]) {
-               return *iter = field;
+               *iter = field;
+               return field;
        }
        return NULL;
 }
@@ -9038,7 +9179,7 @@ mono_class_get_methods (MonoClass* klass, gpointer *iter)
                        return NULL;
                }
        }
-       method = *iter;
+       method = (MonoMethod **)*iter;
        method++;
        if (method < &klass->methods [klass->method.count]) {
                *iter = method;
@@ -9072,7 +9213,7 @@ mono_class_get_virtual_methods (MonoClass* klass, gpointer *iter)
                        /* start from the first */
                        method = &klass->methods [0];
                } else {
-                       method = *iter;
+                       method = (MonoMethod **)*iter;
                        method++;
                }
                while (method < &klass->methods [klass->method.count]) {
@@ -9143,16 +9284,18 @@ mono_class_get_properties (MonoClass* klass, gpointer *iter)
                mono_class_setup_properties (klass);
                /* start from the first */
                if (klass->ext->property.count) {
-                       return *iter = &klass->ext->properties [0];
+                       *iter = &klass->ext->properties [0];
+                       return (MonoProperty *)*iter;
                } else {
                        /* no fields */
                        return NULL;
                }
        }
-       property = *iter;
+       property = (MonoProperty *)*iter;
        property++;
        if (property < &klass->ext->properties [klass->ext->property.count]) {
-               return *iter = property;
+               *iter = property;
+               return (MonoProperty *)*iter;
        }
        return NULL;
 }
@@ -9179,16 +9322,18 @@ mono_class_get_events (MonoClass* klass, gpointer *iter)
                mono_class_setup_events (klass);
                /* start from the first */
                if (klass->ext->event.count) {
-                       return *iter = &klass->ext->events [0];
+                       *iter = &klass->ext->events [0];
+                       return (MonoEvent *)*iter;
                } else {
                        /* no fields */
                        return NULL;
                }
        }
-       event = *iter;
+       event = (MonoEvent *)*iter;
        event++;
        if (event < &klass->ext->events [klass->ext->event.count]) {
-               return *iter = event;
+               *iter = event;
+               return (MonoEvent *)*iter;
        }
        return NULL;
 }
@@ -9231,7 +9376,7 @@ mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
                        return NULL;
                }
        }
-       iface = *iter;
+       iface = (MonoClass **)*iter;
        iface++;
        if (iface < &klass->interfaces [klass->interface_count]) {
                *iter = iface;
@@ -9319,17 +9464,17 @@ mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
                /* start from the first */
                if (klass->ext && klass->ext->nested_classes) {
                        *iter = klass->ext->nested_classes;
-                       return klass->ext->nested_classes->data;
+                       return (MonoClass *)klass->ext->nested_classes->data;
                } else {
                        /* no nested types */
                        return NULL;
                }
        }
-       item = *iter;
+       item = (GList *)*iter;
        item = item->next;
        if (item) {
                *iter = item;
-               return item->data;
+               return (MonoClass *)item->data;
        }
        return NULL;
 }
@@ -9339,7 +9484,7 @@ mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
  * mono_class_is_delegate
  * @klass: the MonoClass to act on
  *
- * Returns: true if the MonoClass represents a System.Delegate.
+ * Returns: TRUE if the MonoClass represents a System.Delegate.
  */
 mono_bool
 mono_class_is_delegate (MonoClass *klass)
@@ -9352,7 +9497,7 @@ mono_class_is_delegate (MonoClass *klass)
  * @klass: The MonoClass to act on
  * @interface: The interface to check if @klass implements.
  *
- * Returns: true if @klass implements @interface.
+ * Returns: TRUE if @klass implements @interface.
  */
 mono_bool
 mono_class_implements_interface (MonoClass* klass, MonoClass* iface)
@@ -9364,7 +9509,7 @@ mono_class_implements_interface (MonoClass* klass, MonoClass* iface)
  * mono_field_get_name:
  * @field: the MonoClassField to act on
  *
- * Returns: the name of the field.
+ * Returns: The name of the field.
  */
 const char*
 mono_field_get_name (MonoClassField *field)
@@ -9426,7 +9571,7 @@ mono_field_get_parent (MonoClassField *field)
  * The metadata flags for a field are encoded using the
  * FIELD_ATTRIBUTE_* constants.  See the tabledefs.h file for details.
  *
- * Returns: the flags for the field.
+ * Returns: The flags for the field.
  */
 guint32
 mono_field_get_flags (MonoClassField *field)
@@ -9437,10 +9582,10 @@ mono_field_get_flags (MonoClassField *field)
 }
 
 /**
- * mono_field_get_offset;
+ * mono_field_get_offset:
  * @field: the MonoClassField to act on
  *
- * Returns: the field offset.
+ * Returns: The field offset.
  */
 guint32
 mono_field_get_offset (MonoClassField *field)
@@ -9461,7 +9606,7 @@ mono_field_get_rva (MonoClassField *field)
        if (!klass->ext || !klass->ext->field_def_values) {
                mono_class_alloc_ext (klass);
 
-               field_def_values = mono_class_alloc0 (klass, sizeof (MonoFieldDefaultValue) * klass->field.count);
+               field_def_values = (MonoFieldDefaultValue *)mono_class_alloc0 (klass, sizeof (MonoFieldDefaultValue) * klass->field.count);
 
                mono_image_lock (klass->image);
                if (!klass->ext->field_def_values)
@@ -9482,10 +9627,10 @@ mono_field_get_rva (MonoClassField *field)
 }
 
 /**
- * mono_field_get_data;
+ * mono_field_get_data:
  * @field: the MonoClassField to act on
  *
- * Returns: pointer to the metadata constant value or to the field
+ * Returns: pointer to the metadata constant value or to the field
  * data if it has an RVA flag.
  */
 const char *
@@ -9506,7 +9651,7 @@ mono_field_get_data (MonoClassField *field)
  * mono_property_get_name: 
  * @prop: the MonoProperty to act on
  *
- * Returns: the name of the property
+ * Returns: The name of the property
  */
 const char*
 mono_property_get_name (MonoProperty *prop)
@@ -9518,7 +9663,7 @@ mono_property_get_name (MonoProperty *prop)
  * mono_property_get_set_method
  * @prop: the MonoProperty to act on.
  *
- * Returns: the setter method of the property (A MonoMethod)
+ * Returns: The setter method of the property (A MonoMethod)
  */
 MonoMethod*
 mono_property_get_set_method (MonoProperty *prop)
@@ -9530,7 +9675,7 @@ mono_property_get_set_method (MonoProperty *prop)
  * mono_property_get_get_method
  * @prop: the MonoProperty to act on.
  *
- * Returns: the setter method of the property (A MonoMethod)
+ * Returns: The setter method of the property (A MonoMethod)
  */
 MonoMethod*
 mono_property_get_get_method (MonoProperty *prop)
@@ -9542,7 +9687,7 @@ mono_property_get_get_method (MonoProperty *prop)
  * mono_property_get_parent:
  * @prop: the MonoProperty to act on.
  *
- * Returns: the MonoClass where the property was defined.
+ * Returns: The MonoClass where the property was defined.
  */
 MonoClass*
 mono_property_get_parent (MonoProperty *prop)
@@ -9557,7 +9702,7 @@ mono_property_get_parent (MonoProperty *prop)
  * The metadata flags for a property are encoded using the
  * PROPERTY_ATTRIBUTE_* constants.  See the tabledefs.h file for details.
  *
- * Returns: the flags for the property.
+ * Returns: The flags for the property.
  */
 guint32
 mono_property_get_flags (MonoProperty *prop)
@@ -9569,7 +9714,7 @@ mono_property_get_flags (MonoProperty *prop)
  * mono_event_get_name:
  * @event: the MonoEvent to act on
  *
- * Returns: the name of the event.
+ * Returns: The name of the event.
  */
 const char*
 mono_event_get_name (MonoEvent *event)
@@ -9581,7 +9726,7 @@ mono_event_get_name (MonoEvent *event)
  * mono_event_get_add_method:
  * @event: The MonoEvent to act on.
  *
- * Returns: the @add' method for the event (a MonoMethod).
+ * Returns: The @add' method for the event (a MonoMethod).
  */
 MonoMethod*
 mono_event_get_add_method (MonoEvent *event)
@@ -9593,7 +9738,7 @@ mono_event_get_add_method (MonoEvent *event)
  * mono_event_get_remove_method:
  * @event: The MonoEvent to act on.
  *
- * Returns: the @remove method for the event (a MonoMethod).
+ * Returns: The @remove method for the event (a MonoMethod).
  */
 MonoMethod*
 mono_event_get_remove_method (MonoEvent *event)
@@ -9605,7 +9750,7 @@ mono_event_get_remove_method (MonoEvent *event)
  * mono_event_get_raise_method:
  * @event: The MonoEvent to act on.
  *
- * Returns: the @raise method for the event (a MonoMethod).
+ * Returns: The @raise method for the event (a MonoMethod).
  */
 MonoMethod*
 mono_event_get_raise_method (MonoEvent *event)
@@ -9617,7 +9762,7 @@ mono_event_get_raise_method (MonoEvent *event)
  * mono_event_get_parent:
  * @event: the MonoEvent to act on.
  *
- * Returns: the MonoClass where the event is defined.
+ * Returns: The MonoClass where the event is defined.
  */
 MonoClass*
 mono_event_get_parent (MonoEvent *event)
@@ -9632,7 +9777,7 @@ mono_event_get_parent (MonoEvent *event)
  * The metadata flags for an event are encoded using the
  * EVENT_* constants.  See the tabledefs.h file for details.
  *
- * Returns: the flags for the event.
+ * Returns: The flags for the event.
  */
 guint32
 mono_event_get_flags (MonoEvent *event)
@@ -9854,19 +9999,19 @@ mono_class_get_exception_for_failure (MonoClass *klass)
                return ex;
        }
        case MONO_EXCEPTION_MISSING_METHOD: {
-               char *class_name = exception_data;
+               char *class_name = (char *)exception_data;
                char *assembly_name = class_name + strlen (class_name) + 1;
 
                return mono_get_exception_missing_method (class_name, assembly_name);
        }
        case MONO_EXCEPTION_MISSING_FIELD: {
-               char *class_name = exception_data;
+               char *class_name = (char *)exception_data;
                char *member_name = class_name + strlen (class_name) + 1;
 
                return mono_get_exception_missing_field (class_name, member_name);
        }
        case MONO_EXCEPTION_FILE_NOT_FOUND: {
-               char *msg_format = exception_data;
+               char *msg_format = (char *)exception_data;
                char *assembly_name = msg_format + strlen (msg_format) + 1;
                char *msg = g_strdup_printf (msg_format, assembly_name);
                MonoException *ex;
@@ -9878,7 +10023,7 @@ mono_class_get_exception_for_failure (MonoClass *klass)
                return ex;
        }
        case MONO_EXCEPTION_BAD_IMAGE: {
-               return mono_get_exception_bad_image_format (exception_data);
+               return mono_get_exception_bad_image_format ((const char *)exception_data);
        }
        default: {
                MonoLoaderError *error;
@@ -9983,16 +10128,16 @@ can_access_internals (MonoAssembly *accessing, MonoAssembly* accessed)
 
        mono_assembly_load_friends (accessed);
        for (tmp = accessed->friend_assembly_names; tmp; tmp = tmp->next) {
-               MonoAssemblyName *friend = tmp->data;
+               MonoAssemblyName *friend_ = (MonoAssemblyName *)tmp->data;
                /* Be conservative with checks */
-               if (!friend->name)
+               if (!friend_->name)
                        continue;
-               if (strcmp (accessing->aname.name, friend->name))
+               if (strcmp (accessing->aname.name, friend_->name))
                        continue;
-               if (friend->public_key_token [0]) {
+               if (friend_->public_key_token [0]) {
                        if (!accessing->aname.public_key_token [0])
                                continue;
-                       if (!mono_public_tokens_are_equal (friend->public_key_token, accessing->aname.public_key_token))
+                       if (!mono_public_tokens_are_equal (friend_->public_key_token, accessing->aname.public_key_token))
                                continue;
                }
                return TRUE;
@@ -10160,6 +10305,16 @@ can_access_member (MonoClass *access_klass, MonoClass *member_klass, MonoClass*
        return FALSE;
 }
 
+/**
+ * mono_method_can_access_field:
+ * @method: Method that will attempt to access the field
+ * @field: the field to access
+ *
+ * Used to determine if a method is allowed to access the specified field.
+ *
+ * Returns: TRUE if the given @method is allowed to access the @field while following
+ * the accessibility rules of the CLI.
+ */
 gboolean
 mono_method_can_access_field (MonoMethod *method, MonoClassField *field)
 {
@@ -10177,6 +10332,16 @@ mono_method_can_access_field (MonoMethod *method, MonoClassField *field)
        return can;
 }
 
+/**
+ * mono_method_can_access_method:
+ * @method: Method that will attempt to access the other method
+ * @called: the method that we want to probe for accessibility.
+ *
+ * Used to determine if the @method is allowed to access the specified @called method.
+ *
+ * Returns: TRUE if the given @method is allowed to invoke the @called while following
+ * the accessibility rules of the CLI.
+ */
 gboolean
 mono_method_can_access_method (MonoMethod *method, MonoMethod *called)
 {
@@ -10424,7 +10589,7 @@ mono_class_alloc_ext (MonoClass *klass)
        if (klass->ext)
                return;
 
-       ext = mono_class_alloc0 (klass, sizeof (MonoClassExt));
+       ext = (MonoClassExt *)mono_class_alloc0 (klass, sizeof (MonoClassExt));
        mono_image_lock (klass->image);
        mono_memory_barrier ();
        if (!klass->ext)
@@ -10456,7 +10621,7 @@ mono_class_setup_interfaces (MonoClass *klass, MonoError *error)
 
                /* generic IList, ICollection, IEnumerable */
                interface_count = mono_defaults.generic_ireadonlylist_class ? 2 : 1;
-               interfaces = mono_image_alloc0 (klass->image, sizeof (MonoClass*) * interface_count);
+               interfaces = (MonoClass **)mono_image_alloc0 (klass->image, sizeof (MonoClass*) * interface_count);
 
                args [0] = &klass->element_class->byval_arg;
                interfaces [0] = mono_class_bind_generic_parameters (
@@ -10517,14 +10682,12 @@ mono_field_resolve_type (MonoClassField *field, MonoError *error)
                if (!mono_error_ok (error)) {
                        char *err_msg = g_strdup_printf ("Could not load field %d type due to: %s", field_idx, mono_error_get_message (error));
                        mono_class_set_failure (klass, MONO_EXCEPTION_TYPE_LOAD, err_msg);
-                       g_free (err_msg);
                }
 
                field->type = mono_class_inflate_generic_type_no_copy (image, gtype, mono_class_get_context (klass), error);
                if (!mono_error_ok (error)) {
                        char *err_msg = g_strdup_printf ("Could not load field %d type due to: %s", field_idx, mono_error_get_message (error));
                        mono_class_set_failure (klass, MONO_EXCEPTION_TYPE_LOAD, err_msg);
-                       g_free (err_msg);
                }
        } else {
                const char *sig;
@@ -10546,8 +10709,8 @@ mono_field_resolve_type (MonoClassField *field, MonoError *error)
                mono_metadata_decode_table_row (image, MONO_TABLE_FIELD, idx, cols, MONO_FIELD_SIZE);
 
                if (!mono_verifier_verify_field_signature (image, cols [MONO_FIELD_SIGNATURE], NULL)) {
-                       mono_error_set_type_load_class (error, klass, "Could not verify field %s signature", field->name);
-                       mono_class_set_failure (klass, MONO_EXCEPTION_TYPE_LOAD, NULL);
+                       mono_error_set_type_load_class (error, klass, "Could not verify field %s signature", field->name);;
+                       mono_class_set_failure (klass, MONO_EXCEPTION_TYPE_LOAD, g_strdup (mono_error_get_message (error)));
                        return;
                }
 
@@ -10556,9 +10719,12 @@ mono_field_resolve_type (MonoClassField *field, MonoError *error)
                mono_metadata_decode_value (sig, &sig);
                /* FIELD signature == 0x06 */
                g_assert (*sig == 0x06);
-               field->type = mono_metadata_parse_type_full (image, container, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
-               if (!field->type)
-                       mono_class_set_failure_from_loader_error (klass, error, g_strdup_printf ("Could not load field %s type", field->name));
+
+               field->type = mono_metadata_parse_type_checked (image, container, cols [MONO_FIELD_FLAGS], FALSE, sig + 1, &sig, error);
+               if (!field->type) {
+                       char *err_msg = g_strdup_printf ("Could not load field %d type due to: %s", field_idx, mono_error_get_message (error));
+                       mono_class_set_failure (klass, MONO_EXCEPTION_TYPE_LOAD, err_msg);
+               }
        }
 }
 
@@ -10625,16 +10791,18 @@ mono_class_get_fields_lazy (MonoClass* klass, gpointer *iter)
                        return NULL;
                /* start from the first */
                if (klass->field.count) {
-                       return *iter = &klass->fields [0];
+                       *iter = &klass->fields [0];
+                       return (MonoClassField *)*iter;
                } else {
                        /* no fields */
                        return NULL;
                }
        }
-       field = *iter;
+       field = (MonoClassField *)*iter;
        field++;
        if (field < &klass->fields [klass->field.count]) {
-               return *iter = field;
+               *iter = field;
+               return (MonoClassField *)*iter;
        }
        return NULL;
 }