+static void
+mono_type_get_name_recurse (MonoType *type, GString *str)
+{
+ MonoClass *klass;
+
+ switch (type->type) {
+ case MONO_TYPE_ARRAY: {
+ int i, rank = type->data.array->rank;
+
+ mono_type_get_name_recurse (type->data.array->type, str);
+ g_string_append_c (str, '[');
+ for (i = 1; i < rank; i++)
+ g_string_append_c (str, ',');
+ g_string_append_c (str, ']');
+ break;
+ }
+ case MONO_TYPE_SZARRAY:
+ mono_type_get_name_recurse (type->data.type, str);
+ g_string_append (str, "[]");
+ break;
+ case MONO_TYPE_PTR:
+ mono_type_get_name_recurse (type->data.type, str);
+ g_string_append_c (str, '*');
+ break;
+ default:
+ klass = mono_class_from_mono_type (type);
+ if (klass->nested_in) {
+ mono_type_get_name_recurse (&klass->nested_in->byval_arg, str);
+ g_string_append_c (str, '+');
+ }
+ if (*klass->name_space) {
+ g_string_append (str, klass->name_space);
+ g_string_append_c (str, '.');
+ }
+ g_string_append (str, klass->name);
+ break;
+ }
+}
+
+/*
+ * mono_type_get_name:
+ * @type: a type
+ *
+ * Returns the string representation for type as required by System.Reflection.
+ * The inverse of mono_reflection_parse_type ().
+ */
+char*
+mono_type_get_name (MonoType *type)
+{
+ GString* result = g_string_new ("");
+ mono_type_get_name_recurse (type, result);
+
+ if (type->byref)
+ g_string_append_c (result, '&');
+
+ return g_string_free (result, FALSE);
+}
+
+/*
+ * mono_reflection_get_type:
+ * @image: a metadata context
+ * @info: type description structure
+ * @ignorecase: flag for case-insensitive string compares
+ *
+ * Build a MonoType from the type description in @info.
+ *
+ */
+MonoType*
+mono_reflection_get_type (MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase)
+{
+ MonoClass *klass;
+ GList *mod;
+ int modval;
+
+ if (ignorecase) {
+ g_warning ("Ignore case not yet supported in GetType()");
+ return NULL;
+ }
+
+ if (!image)
+ image = mono_defaults.corlib;
+
+ if (info->nest_name) {
+ klass = mono_class_from_name (image, info->nest_name_space, info->nest_name);
+ if (klass) {
+ GList *nested;
+ mono_class_init (klass);
+ nested = klass->nested_classes;
+ klass = NULL;
+ while (nested) {
+ klass = nested->data;
+ if (strcmp (klass->name, info->nest_name) == 0 &&
+ strcmp (klass->name_space, info->nest_name_space) == 0)
+ break;
+ klass = NULL;
+ nested = nested->next;
+ }
+ }
+ } else {
+ klass = mono_class_from_name (image, info->name_space, info->name);
+ }
+ if (!klass)
+ return NULL;
+ mono_class_init (klass);
+ for (mod = info->modifiers; mod; mod = mod->next) {
+ modval = GPOINTER_TO_UINT (mod->data);
+ if (!modval) { /* byref: must be last modifier */
+ return &klass->this_arg;
+ } else if (modval == -1) {
+ klass = mono_ptr_class_get (&klass->byval_arg);
+ } else { /* array rank */
+ klass = mono_array_class_get (&klass->byval_arg, modval);
+ }
+ mono_class_init (klass);
+ }
+ return &klass->byval_arg;
+}
+
+/*
+ * Optimization we could avoid mallocing() an little-endian archs that
+ * don't crash with unaligned accesses.
+ */
+static void
+fill_param_data (MonoImage *image, MonoMethodSignature *sig, guint32 blobidx, void **params) {
+ int len, i, slen, type;
+ const char *p = mono_metadata_blob_heap (image, blobidx);
+
+ len = mono_metadata_decode_value (p, &p);
+ if (len < 2 || read16 (p) != 0x0001) /* Prolog */
+ return;
+
+ /* skip prolog */
+ p += 2;
+ for (i = 0; i < sig->param_count; ++i) {
+ type = sig->params [i]->type;
+handle_enum:
+ switch (type) {
+ case MONO_TYPE_U1:
+ case MONO_TYPE_I1:
+ case MONO_TYPE_BOOLEAN: {
+ MonoBoolean *bval = params [i] = g_malloc (sizeof (MonoBoolean));
+ *bval = *p;
+ ++p;
+ break;
+ }
+ case MONO_TYPE_CHAR:
+ case MONO_TYPE_U2:
+ case MONO_TYPE_I2: {
+ guint16 *val = params [i] = g_malloc (sizeof (guint16));
+ *val = read16 (p);
+ p += 2;
+ break;
+ }
+#if SIZEOF_VOID_P == 4
+ case MONO_TYPE_U:
+ case MONO_TYPE_I:
+#endif
+ case MONO_TYPE_R4:
+ case MONO_TYPE_U4:
+ case MONO_TYPE_I4: {
+ guint32 *val = params [i] = g_malloc (sizeof (guint32));
+ *val = read32 (p);
+ p += 4;
+ break;
+ }
+#if SIZEOF_VOID_P == 8
+ case MONO_TYPE_U: /* error out instead? this should probably not happen */
+ case MONO_TYPE_I:
+#endif
+ case MONO_TYPE_R8:
+ case MONO_TYPE_U8:
+ case MONO_TYPE_I8: {
+ guint64 *val = params [i] = g_malloc (sizeof (guint64));
+ *val = read64 (p);
+ p += 8;
+ break;
+ }
+ case MONO_TYPE_VALUETYPE:
+ if (sig->params [i]->data.klass->enumtype) {
+ type = sig->params [i]->data.klass->enum_basetype->type;
+ goto handle_enum;
+ } else {
+ g_warning ("generic valutype %s not handled in custom attr value decoding", sig->params [i]->data.klass->name);
+ }
+ break;
+ case MONO_TYPE_STRING: {
+ slen = mono_metadata_decode_value (p, &p);
+ params [i] = mono_string_new_len (mono_domain_get (), p, slen);
+ p += slen;
+ break;
+ }
+ default:
+ g_warning ("Type %02x not handled in custom attr value decoding", sig->params [i]->type);
+ break;
+ }
+ }
+}
+
+static void
+free_param_data (MonoMethodSignature *sig, void **params) {
+ int i;
+ for (i = 0; i < sig->param_count; ++i) {
+ switch (sig->params [i]->type) {
+ case MONO_TYPE_BOOLEAN:
+ case MONO_TYPE_CHAR:
+ case MONO_TYPE_U:
+ case MONO_TYPE_I:
+ case MONO_TYPE_U1:
+ case MONO_TYPE_I1:
+ case MONO_TYPE_U2:
+ case MONO_TYPE_I2:
+ case MONO_TYPE_U4:
+ case MONO_TYPE_I4:
+ case MONO_TYPE_U8:
+ case MONO_TYPE_I8:
+ case MONO_TYPE_R8:
+ case MONO_TYPE_R4:
+ case MONO_TYPE_VALUETYPE:
+ g_free (params [i]);
+ break;
+ default:
+ break;
+ }
+ }
+}
+
+/*
+ * Find the method index in the metadata methodDef table.
+ * Later put these three helper methods in metadata and export them.
+ */
+static guint32
+find_method_index (MonoMethod *method) {
+ MonoClass *klass = method->klass;
+ int i;
+
+ for (i = 0; i < klass->method.count; ++i) {
+ if (method == klass->methods [i])
+ return klass->method.first + 1 + i;
+ }
+ return 0;
+}
+
+/*
+ * Find the field index in the metadata FieldDef table.
+ */
+static guint32
+find_field_index (MonoClass *klass, MonoClassField *field) {
+ int i;
+
+ for (i = 0; i < klass->field.count; ++i) {
+ if (field == &klass->fields [i])
+ return klass->field.first + 1 + i;
+ }
+ return 0;
+}
+
+/*
+ * Find the property index in the metadata Property table.
+ */
+static guint32
+find_property_index (MonoClass *klass, MonoProperty *property) {
+ int i;
+
+ for (i = 0; i < klass->property.count; ++i) {
+ if (property == &klass->properties [i])
+ return klass->property.first + 1 + i;
+ }
+ return 0;
+}
+
+/*
+ * Find the event index in the metadata Event table.
+ */
+static guint32
+find_event_index (MonoClass *klass, MonoEvent *event) {
+ int i;
+
+ for (i = 0; i < klass->event.count; ++i) {
+ if (event == &klass->events [i])
+ return klass->event.first + 1 + i;
+ }
+ return 0;
+}
+
+/*
+ * mono_reflection_get_custom_attrs:
+ * @obj: a reflection object handle
+ *
+ * Return an array with all the custom attributes defined of the
+ * reflection handle @obj. The objects are fully build.
+ */
+MonoArray*
+mono_reflection_get_custom_attrs (MonoObject *obj)
+{
+ guint32 idx, mtoken, i, len;
+ guint32 cols [MONO_CUSTOM_ATTR_SIZE];
+ MonoClass *klass;
+ MonoImage *image;
+ MonoTableInfo *ca;
+ MonoMethod *method;
+ MonoObject *attr;
+ MonoArray *result;
+ GList *list = NULL;
+ void **params;
+
+ klass = obj->vtable->klass;
+ /* FIXME: need to handle: Module */
+ if (klass == mono_defaults.monotype_class) {
+ MonoReflectionType *rtype = (MonoReflectionType*)obj;
+ klass = mono_class_from_mono_type (rtype->type);
+ idx = mono_metadata_token_index (klass->type_token);
+ idx <<= CUSTOM_ATTR_BITS;
+ idx |= CUSTOM_ATTR_TYPEDEF;
+ image = klass->image;
+ } else if (strcmp ("Assembly", klass->name) == 0) {
+ MonoReflectionAssembly *rassembly = (MonoReflectionAssembly*)obj;
+ idx = 1; /* there is only one assembly */
+ idx <<= CUSTOM_ATTR_BITS;
+ idx |= CUSTOM_ATTR_ASSEMBLY;
+ image = rassembly->assembly->image;
+ } else if (strcmp ("MonoProperty", klass->name) == 0) {
+ MonoReflectionProperty *rprop = (MonoReflectionProperty*)obj;
+ idx = find_property_index (rprop->klass, rprop->property);
+ idx <<= CUSTOM_ATTR_BITS;
+ idx |= CUSTOM_ATTR_PROPERTY;
+ image = rprop->klass->image;
+ } else if (strcmp ("MonoEvent", klass->name) == 0) {
+ MonoReflectionEvent *revent = (MonoReflectionEvent*)obj;
+ idx = find_event_index (revent->klass, revent->event);
+ idx <<= CUSTOM_ATTR_BITS;
+ idx |= CUSTOM_ATTR_EVENT;
+ image = revent->klass->image;
+ } else if (strcmp ("MonoField", klass->name) == 0) {
+ MonoReflectionField *rfield = (MonoReflectionField*)obj;
+ idx = find_field_index (rfield->klass, rfield->field);
+ idx <<= CUSTOM_ATTR_BITS;
+ idx |= CUSTOM_ATTR_FIELDDEF;
+ image = rfield->klass->image;
+ } else if ((strcmp ("MonoMethod", klass->name) == 0) || (strcmp ("MonoCMethod", klass->name) == 0)) {
+ MonoReflectionMethod *rmethod = (MonoReflectionMethod*)obj;
+ idx = find_method_index (rmethod->method);
+ idx <<= CUSTOM_ATTR_BITS;
+ idx |= CUSTOM_ATTR_METHODDEF;
+ image = method->klass->image;
+ } else if (strcmp ("ParameterInfo", klass->name) == 0) {
+ MonoReflectionParameter *param = (MonoReflectionParameter*)obj;
+ MonoReflectionMethod *rmethod = (MonoReflectionMethod*)param->MemberImpl;
+ guint32 method_index = find_method_index (rmethod->method);
+ guint32 param_list, param_last, param_pos, found;
+
+ image = rmethod->method->klass->image;
+ ca = &image->tables [MONO_TABLE_METHOD];
+
+ param_list = mono_metadata_decode_row_col (ca, method_index - 1, MONO_METHOD_PARAMLIST);
+ if (method_index == ca->rows) {
+ ca = &image->tables [MONO_TABLE_PARAM];
+ param_last = ca->rows + 1;
+ } else {
+ param_last = mono_metadata_decode_row_col (ca, method_index, MONO_METHOD_PARAMLIST);
+ ca = &image->tables [MONO_TABLE_PARAM];
+ }
+ found = 0;
+ for (i = param_list; i < param_last; ++i) {
+ param_pos = mono_metadata_decode_row_col (ca, i - 1, MONO_PARAM_SEQUENCE);
+ if (param_pos == param->PositionImpl) {
+ found = 1;
+ break;
+ }
+ }
+ if (!found)
+ return mono_array_new (mono_domain_get (), mono_defaults.object_class, 0);
+ idx = i;
+ idx <<= CUSTOM_ATTR_BITS;
+ idx |= CUSTOM_ATTR_PARAMDEF;
+ } else { /* handle other types here... */
+ g_error ("get custom attrs not yet supported for %s", klass->name);
+ }
+
+ /* at this point image and index are set correctly for searching the custom attr */
+ ca = &image->tables [MONO_TABLE_CUSTOMATTRIBUTE];
+ /* the table is not sorted */
+ for (i = 0; i < ca->rows; ++i) {
+ mono_metadata_decode_row (ca, i, cols, MONO_CUSTOM_ATTR_SIZE);
+ if (cols [MONO_CUSTOM_ATTR_PARENT] != idx)
+ continue;
+ mtoken = cols [MONO_CUSTOM_ATTR_TYPE] >> CUSTOM_ATTR_TYPE_BITS;
+ switch (cols [MONO_CUSTOM_ATTR_TYPE] & CUSTOM_ATTR_TYPE_MASK) {
+ case CUSTOM_ATTR_TYPE_METHODDEF:
+ mtoken |= MONO_TOKEN_METHOD_DEF;
+ break;
+ case CUSTOM_ATTR_TYPE_MEMBERREF:
+ mtoken |= MONO_TOKEN_MEMBER_REF;
+ break;
+ default:
+ g_error ("Unknown table for custom attr type %08x", cols [MONO_CUSTOM_ATTR_TYPE]);
+ break;
+ }
+ method = mono_get_method (image, mtoken, NULL);
+ if (!method)
+ g_error ("Can't find custom attr constructor");
+ mono_class_init (method->klass);
+ /*g_print ("got attr %s\n", method->klass->name);*/
+ params = g_new (void*, method->signature->param_count);
+ fill_param_data (image, method->signature, cols [MONO_CUSTOM_ATTR_VALUE], params);
+ attr = mono_object_new (mono_domain_get (), method->klass);
+ mono_runtime_invoke (method, attr, params);
+ list = g_list_prepend (list, attr);
+ free_param_data (method->signature, params);
+ g_free (params);
+ }
+
+ len = g_list_length (list);
+ /*
+ * The return type is really object[], but System/Attribute.cs does a cast
+ * to (Attribute []) and that is not allowed: I'm lazy for now, but we should
+ * probably fix that.
+ */
+ klass = mono_class_from_name (mono_defaults.corlib, "System", "Attribute");
+ result = mono_array_new (mono_domain_get (), klass, len);
+ for (i = 0; i < len; ++i) {
+ mono_array_set (result, gpointer, i, list->data);
+ list = list->next;
+ }
+ g_list_free (g_list_first (list));
+
+ return result;
+}
+
+/*
+ * mono_reflection_get_custom_attrs_blob:
+ * @ctor: custom attribute constructor
+ * @ctorArgs: arguments o the constructor
+ * @properties:
+ * @propValues:
+ * @fields:
+ * @fieldValues:
+ *
+ * Creates the blob of data that needs to be saved in the metadata and that represents
+ * the custom attributed described by @ctor, @ctorArgs etc.
+ * Returns: a Byte array representing the blob of data.
+ */
+MonoArray*
+mono_reflection_get_custom_attrs_blob (MonoObject *ctor, MonoArray *ctorArgs, MonoArray *properties, MonoArray *propValues, MonoArray *fields, MonoArray* fieldValues) {
+ MonoArray *result;
+ MonoMethodSignature *sig;
+ MonoObject *arg;
+ char *buffer, *p, *argval;
+ guint32 buflen, i, type;
+
+ if (strcmp (ctor->vtable->klass->name, "MonoCMethod")) {
+ g_warning ("ConstructorBuilder Custom attribute not yet supported");
+ /*
+ * maybe we should have a param array to method signature function and
+ * continue with the normal codepath.
+ */
+ result = mono_array_new (mono_domain_get (), mono_defaults.byte_class, 4);
+ mono_array_set (result, char, 0, 1);
+ return result;
+ }
+ buflen = 256;
+ p = buffer = g_malloc (buflen);
+ /* write the prolog */
+ *p++ = 1;
+ *p++ = 0;
+ sig = ((MonoReflectionMethod*)ctor)->method->signature;
+ /* FIXME: ENOENDIAN */
+ for (i = 0; i < sig->param_count; ++i) {
+ if ((p-buffer) + 10 >= buflen) {
+ char *newbuf;
+ buflen *= 2;
+ newbuf = g_realloc (buffer, buflen);
+ p = newbuf + (p-buffer);
+ buffer = newbuf;
+ }
+ arg = (MonoObject*)mono_array_get (ctorArgs, gpointer, i);
+ argval = ((char*)arg + sizeof (MonoObject));
+ type = sig->params [i]->type;
+handle_enum:
+ switch (type) {
+ case MONO_TYPE_BOOLEAN:
+ case MONO_TYPE_U1:
+ case MONO_TYPE_I1:
+ *p++ = *argval;
+ break;
+ case MONO_TYPE_CHAR:
+ case MONO_TYPE_U2:
+ case MONO_TYPE_I2: {
+ guint16 *val = (guint16*)p;
+ *val = *(guint16*)argval;
+ p += 2;
+ break;
+ }
+ case MONO_TYPE_U4:
+ case MONO_TYPE_I4:
+ case MONO_TYPE_R4: {
+ guint32 *val = (guint32*)p;
+ *val = *(guint32*)argval;
+ p += 4;
+ break;
+ }
+ case MONO_TYPE_U8:
+ case MONO_TYPE_I8:
+ case MONO_TYPE_R8: {
+ guint64 *val = (guint64*)p;
+ *val = *(guint64*)argval;
+ p += 8;
+ break;
+ }
+ case MONO_TYPE_VALUETYPE:
+ if (sig->params [i]->data.klass->enumtype) {
+ type = sig->params [i]->data.klass->enum_basetype->type;
+ goto handle_enum;
+ } else {
+ g_warning ("generic valutype %s not handled in custom attr value decoding", sig->params [i]->data.klass->name);
+ }
+ break;
+ case MONO_TYPE_STRING: {
+ char *str = mono_string_to_utf8 ((MonoString*)arg);
+ guint32 slen = strlen (str);
+ if ((p-buffer) + 10 + slen >= buflen) {
+ char *newbuf;
+ buflen *= 2;
+ buflen += slen;
+ newbuf = g_realloc (buffer, buflen);
+ p = newbuf + (p-buffer);
+ buffer = newbuf;
+ }
+ mono_metadata_encode_value (slen, p, &p);
+ memcpy (p, str, slen);
+ p += slen;
+ g_free (str);
+ break;
+ }
+ default:
+ g_error ("type 0x%02x not yet supported in custom attr encoder", type);
+ }
+ }
+ /*
+ * we don't support properties and fields, yet, set to 0.
+ */
+ *p++ = 0;
+ *p++ = 0;
+ buflen = p - buffer;
+ result = mono_array_new (mono_domain_get (), mono_defaults.byte_class, buflen);
+ p = mono_array_addr (result, char, 0);
+ memcpy (p, buffer, buflen);
+ g_free (buffer);
+ return result;
+}
+
+/*
+ * mono_reflection_setup_internal_class:
+ * @tb: a TypeBuilder object
+ *
+ * Creates a MonoClass that represents the TypeBuilder.
+ * This is a trick that lets us simplify a lot of reflection code
+ * (and will allow us to support Build and Run assemblies easier).
+ */
+void
+mono_reflection_setup_internal_class (MonoReflectionTypeBuilder *tb)
+{
+ MonoClass *klass, *parent;
+
+ klass = g_new0 (MonoClass, 1);
+
+ klass->image = tb->module->assemblyb->dynamic_assembly->assembly.image;
+
+ if (tb->parent)
+ parent = mono_class_from_mono_type (tb->parent->type);
+
+ klass->inited = 1; /* we lie to the runtime */
+ klass->name = mono_string_to_utf8 (tb->name);
+ klass->name_space = mono_string_to_utf8 (tb->nspace);
+ klass->type_token = MONO_TOKEN_TYPE_DEF | tb->table_idx;
+ klass->flags = tb->attrs;
+
+ klass->element_class = klass;
+ klass->reflection_info = tb; /* need to pin. */
+
+ mono_class_setup_parent (klass, parent);
+ mono_class_setup_mono_type (klass);
+
+ /*
+ * FIXME: handle interfaces.
+ */
+
+ tb->type.type = &klass->byval_arg;
+
+ /*g_print ("setup %s as %s (%p)\n", klass->name, ((MonoObject*)tb)->vtable->klass->name, tb);*/
+}
+
+MonoArray *
+mono_reflection_sighelper_get_signature_local (MonoReflectionSigHelper *sig)
+{
+ MonoDynamicAssembly *assembly = sig->module->assemblyb->dynamic_assembly;
+ guint32 na = mono_array_length (sig->arguments);
+ guint32 buflen, i;
+ MonoArray *result;
+ char *buf, *p;
+
+ p = buf = g_malloc (10 + na * 10);
+
+ mono_metadata_encode_value (0x07, p, &p);
+ mono_metadata_encode_value (na, p, &p);
+ for (i = 0; i < na; ++i) {
+ MonoReflectionType *type = mono_array_get (sig->arguments, MonoReflectionType *, i);
+ encode_reflection_type (assembly, type, p, &p);
+ }
+
+ buflen = p - buf;
+ result = mono_array_new (mono_domain_get (), mono_defaults.byte_class, buflen);
+ p = mono_array_addr (result, char, 0);
+ memcpy (p, buf, buflen);
+ g_free (buf);
+
+ return result;
+}
+
+MonoArray *
+mono_reflection_sighelper_get_signature_field (MonoReflectionSigHelper *sig)
+{
+ MonoDynamicAssembly *assembly = sig->module->assemblyb->dynamic_assembly;
+ guint32 na = mono_array_length (sig->arguments);
+ guint32 buflen, i;
+ MonoArray *result;
+ char *buf, *p;
+
+ p = buf = g_malloc (10 + na * 10);
+
+ mono_metadata_encode_value (0x06, p, &p);
+ for (i = 0; i < na; ++i) {
+ MonoReflectionType *type = mono_array_get (sig->arguments, MonoReflectionType *, i);
+ encode_reflection_type (assembly, type, p, &p);
+ }
+
+ buflen = p - buf;
+ result = mono_array_new (mono_domain_get (), mono_defaults.byte_class, buflen);
+ p = mono_array_addr (result, char, 0);
+ memcpy (p, buf, buflen);
+ g_free (buf);
+
+ return result;
+}
+