2 * get.c: Functions to get stringified values from the metadata tables.
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
18 #include <mono/metadata/class.h>
20 extern gboolean substitute_with_mscorlib_p;
23 get_typedef (MonoImage *m, int idx)
25 guint32 cols [MONO_TYPEDEF_SIZE];
27 mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPEDEF], idx - 1, cols, MONO_TYPEDEF_SIZE);
29 return g_strdup_printf (
31 mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAMESPACE]),
32 mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAME]));
36 get_module (MonoImage *m, int idx)
38 guint32 cols [MONO_MODULE_SIZE];
41 * There MUST BE only one module in the Module table
45 mono_metadata_decode_row (&m->tables [MONO_TABLE_MODULEREF], idx - 1, cols, MONO_MODULE_SIZE);
47 return g_strdup (mono_metadata_string_heap (m, cols [MONO_MODULE_NAME]));
51 get_assemblyref (MonoImage *m, int idx)
53 guint32 cols [MONO_ASSEMBLYREF_SIZE];
55 mono_metadata_decode_row (&m->tables [MONO_TABLE_ASSEMBLYREF], idx - 1, cols, MONO_ASSEMBLYREF_SIZE);
57 return g_strdup (mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_NAME]));
62 * Returns a string representing the ArrayShape (22.2.16).
65 get_array_shape (MonoImage *m, const char *ptr, char **result)
67 GString *res = g_string_new ("[");
68 guint32 rank, num_sizes, num_lo_bounds;
69 guint32 *sizes = NULL, *lo_bounds = NULL;
73 rank = mono_metadata_decode_value (ptr, &ptr);
74 num_sizes = mono_metadata_decode_value (ptr, &ptr);
77 sizes = g_new (guint32, num_sizes);
79 for (i = 0; i < num_sizes; i++)
80 sizes [i] = mono_metadata_decode_value (ptr, &ptr);
82 num_lo_bounds = mono_metadata_decode_value (ptr, &ptr);
83 if (num_lo_bounds > 0)
84 lo_bounds = g_new (guint32, num_lo_bounds);
86 for (i = 0; i < num_lo_bounds; i++)
87 lo_bounds [i] = mono_metadata_decode_value (ptr, &ptr);
89 for (r = 0; r < rank; r++){
91 if (r < num_lo_bounds){
92 sprintf (buffer, "%d..%d", lo_bounds [r], lo_bounds [r] + sizes [r] - 1);
94 sprintf (buffer, "0..%d", sizes [r] - 1);
99 g_string_append (res, buffer);
101 g_string_append (res, ", ");
103 g_string_append (res, "]");
112 g_string_free (res, FALSE);
119 * @m: metadata context
120 * @blob_idx: index into the blob heap
122 * Returns the stringified representation of a TypeSpec signature (22.2.17)
125 get_typespec (MonoImage *m, guint32 idx)
127 guint32 cols [MONO_TYPESPEC_SIZE];
130 GString *res = g_string_new ("");
133 mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPESPEC], idx-1, cols, MONO_TYPESPEC_SIZE);
134 ptr = mono_metadata_blob_heap (m, cols [MONO_TYPESPEC_SIGNATURE]);
135 len = mono_metadata_decode_value (ptr, &ptr);
139 ptr = get_custom_mod (m, ptr, &s);
141 g_string_append (res, s);
142 g_string_append_c (res, ' ');
146 if (*ptr == MONO_TYPE_VOID)
147 g_string_append (res, "void");
149 ptr = get_type (m, ptr, &s);
151 g_string_append (res, s);
155 case MONO_TYPE_FNPTR:
156 g_string_append (res, "FNPTR ");
158 * we assume MethodRefSig, as we do not know
159 * whether it is a MethodDefSig or a MethodRefSig.
161 printf ("\n FNPTR:\n");
163 hex_dump (ptr, 0, 40);
166 case MONO_TYPE_ARRAY:
167 ptr = get_type (m, ptr, &s);
168 g_string_append (res, s);
170 g_string_append_c (res, ' ');
171 ptr = get_array_shape (m, ptr, &s);
172 g_string_append (res, s);
176 case MONO_TYPE_SZARRAY:
177 ptr = get_custom_mod (m, ptr, &s);
179 g_string_append (res, s);
180 g_string_append_c (res, ' ');
183 ptr = get_type (m, ptr, &s);
184 g_string_append (res, s);
185 g_string_append (res, "[]");
190 g_string_free (res, FALSE);
196 get_typeref (MonoImage *m, int idx)
198 guint32 cols [MONO_TYPEREF_SIZE];
201 guint32 rs_idx, table;
203 mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPEREF], idx - 1, cols, MONO_TYPEREF_SIZE);
205 t = mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAME]);
206 s = mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAMESPACE]);
208 rs_idx = cols [MONO_TYPEREF_SCOPE] >> 2;
211 * ECMA spec claims 3 bits
213 table = cols [MONO_TYPEREF_SCOPE] & 3;
217 x = get_module (m, rs_idx);
218 ret = g_strdup_printf ("TODO:TypeRef-Module [%s] %s.%s", x, s, t);
222 case 1: /* ModuleRef */
223 ret = g_strdup_printf ("TODO:TypeRef-ModuleRef (%s.%s)", s, t);
227 * AssemblyRef (ECMA docs claim it is 3, but it looks to
228 * me like it is 2 (tokens are prefixed with 0x23)
230 x = get_assemblyref (m, rs_idx);
231 ret = g_strdup_printf ("[%s]%s.%s", x, s, t);
235 case 4: /* TypeRef */
236 ret = g_strdup_printf ("TODO:TypeRef-TypeRef: TYPEREF! (%s.%s)", s, t);
240 ret = g_strdup_printf ("Unknown table in TypeRef %d", table);
247 * get_typedef_or_ref:
248 * @m: metadata context
249 * @dor_token: def or ref encoded index
251 * Low two bits contain table to lookup from
252 * high bits contain the index into the def or ref table
254 * Returns: a stringified version of the MethodDef or MethodRef
255 * at (dor_token >> 2)
258 get_typedef_or_ref (MonoImage *m, guint32 dor_token)
260 char *temp = NULL, *s;
264 * low 2 bits contain encoding
266 table = dor_token & 0x03;
267 idx = dor_token >> 2;
270 case 0: /* TypeDef */
271 temp = get_typedef (m, idx);
272 s = g_strdup_printf ("%s", temp);
275 case 1: /* TypeRef */
276 temp = get_typeref (m, idx);
277 s = g_strdup_printf ("%s", temp);
280 case 2: /* TypeSpec */
281 s = get_typespec (m, idx);
285 g_error ("Unhandled encoding for typedef-or-ref coded index");
296 * get_encoded_typedef_or_ref:
297 * @m: metadata context
298 * @ptr: location to decode from.
299 * @result: pointer to string where resulting decoded string is stored
301 * result will point to a g_malloc()ed string.
303 * Returns: the new ptr to continue decoding
306 get_encoded_typedef_or_ref (MonoImage *m, const char *ptr, char **result)
310 token = mono_metadata_decode_value (ptr, &ptr);
312 *result = get_typedef_or_ref (m, token);
320 * Decodes a CustomMod (22.2.7)
322 * Returns: updated pointer location
325 get_custom_mod (MonoImage *m, const char *ptr, char **return_value)
329 if ((*ptr == MONO_TYPE_CMOD_OPT) ||
330 (*ptr == MONO_TYPE_CMOD_REQD)){
332 ptr = get_encoded_typedef_or_ref (m, ptr, &s);
334 *return_value = g_strconcat ("CMOD ", s, NULL);
337 *return_value = NULL;
342 static map_t element_type_map [] = {
343 { MONO_TYPE_END , "end" },
344 { MONO_TYPE_VOID , "void" },
345 { MONO_TYPE_BOOLEAN , "bool" },
346 { MONO_TYPE_CHAR , "char" },
347 { MONO_TYPE_I1 , "int8" },
348 { MONO_TYPE_U1 , "unsigned int8" },
349 { MONO_TYPE_I2 , "int16" },
350 { MONO_TYPE_U2 , "uint16" },
351 { MONO_TYPE_I4 , "int32" },
352 { MONO_TYPE_U4 , "uint32" },
353 { MONO_TYPE_I8 , "int64" },
354 { MONO_TYPE_U8 , "uint64" },
355 { MONO_TYPE_R4 , "float32" },
356 { MONO_TYPE_R8 , "float64" },
357 { MONO_TYPE_STRING , "string" },
358 { MONO_TYPE_TYPEDBYREF , "TypedByRef" },
359 { MONO_TYPE_I , "native int" },
360 { MONO_TYPE_U , "native unsigned int" },
361 { MONO_TYPE_OBJECT , "object" },
365 static map_t call_conv_type_map [] = {
366 { MONO_CALL_DEFAULT , "default" },
367 { MONO_CALL_C , "c" },
368 { MONO_CALL_STDCALL , "stdcall" },
369 { MONO_CALL_THISCALL , "thiscall" },
370 { MONO_CALL_FASTCALL , "fastcall" },
371 { MONO_CALL_VARARG , "vararg" },
376 dis_stringify_token (MonoImage *m, guint32 token)
378 guint idx = token & 0xffffff;
379 switch (token >> 24) {
380 case MONO_TABLE_TYPEDEF: return get_typedef (m, idx);
381 case MONO_TABLE_TYPEREF: return get_typeref (m, idx);
382 case MONO_TABLE_TYPESPEC: return get_typespec (m, idx);
386 return g_strdup_printf("0x%08x", token);
390 dis_stringify_array (MonoImage *m, MonoArrayType *array)
393 GString *s = g_string_new("");
396 type = dis_stringify_type (m, array->type);
397 g_string_append (s, type);
399 g_string_append_c (s, '[');
400 for (i = 0; i < array->rank; ++i) {
402 g_string_append_c (s, ',');
403 if (i < array->numsizes) {
404 if (i < array->numlobounds && array->lobounds[i] != 0)
405 g_string_sprintfa (s, "%d..%d", array->lobounds[i], array->sizes[i]);
407 g_string_sprintfa (s, "%d", array->sizes[i]);
410 g_string_append_c (s, ']');
412 g_string_free (s, FALSE);
417 dis_stringify_modifiers (MonoImage *m, int n, MonoCustomMod *mod)
419 GString *s = g_string_new("");
422 for (i = 0; i < n; ++i) {
423 char *tok = dis_stringify_token (m, mod[i].token);
424 g_string_sprintfa (s, "%s %s", mod[i].required ? "opt": "reqd", tok);
427 g_string_append_c (s, ' ');
429 g_string_free (s, FALSE);
434 dis_stringify_param (MonoImage *m, MonoType *param)
438 const char *out = param->attrs & 2 ? "[out] ": "";
439 t = dis_stringify_type (m, param);
440 result = g_strconcat (out, t, NULL);
446 dis_stringify_method_signature (MonoImage *m, MonoMethodSignature *method, int methoddef_row)
448 guint32 cols [MONO_METHOD_SIZE];
449 guint32 pcols [MONO_PARAM_SIZE];
450 guint32 param_index = 0;
451 const char *name = "";
454 GString *result = g_string_new ("");
457 g_assert (method || methoddef_row);
460 mono_metadata_decode_row (&m->tables [MONO_TABLE_METHOD], methoddef_row -1, cols, MONO_METHOD_SIZE);
461 name = mono_metadata_string_heap (m, cols [MONO_METHOD_NAME]);
462 param_index = cols [MONO_METHOD_PARAMLIST];
464 const char *sig = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
465 mono_metadata_decode_blob_size (sig, &sig);
466 method = mono_metadata_parse_method_signature (m, 1, sig, &sig);
471 retval = dis_stringify_param (m, method->ret);
473 g_string_append (result, "instance ");
474 g_string_append (result, map (method->call_convention, call_conv_type_map));
475 g_string_sprintfa (result, " %s %s(", retval, name);
477 for (i = 0; i < method->param_count; ++i) {
478 if (param_index && param_index <= m->tables [MONO_TABLE_PARAM].rows) {
479 mono_metadata_decode_row (&m->tables [MONO_TABLE_PARAM], param_index - 1, pcols, MONO_PARAM_SIZE);
480 name = mono_metadata_string_heap (m, pcols [MONO_PARAM_NAME]);
481 method->params [i]->attrs = pcols [MONO_PARAM_FLAGS];
487 g_string_append (result, ", ");
488 retval = dis_stringify_param (m, method->params [i]);
489 g_string_sprintfa (result, "%s %s", retval, name);
492 g_string_append (result, ") ");
495 mono_metadata_free_method_signature (method);
496 retval = result->str;
497 g_string_free (result, FALSE);
503 dis_stringify_object (MonoImage *m, MonoType *type)
505 const char *otype = type->type == MONO_TYPE_CLASS? "class" : "valuetype";
506 char *assemblyref = NULL, *result;
507 MonoClass *c = type->data.klass;
509 return g_strdup ("Unknown");
512 if (substitute_with_mscorlib_p && !strcmp ("corlib", c->image->assembly_name))
513 assemblyref = g_strdup_printf ("[%s]", "mscorlib");
515 assemblyref = g_strdup_printf ("[%s]", c->image->assembly_name);
517 result = g_strdup_printf ("%s %s%s%s%s", otype, assemblyref?assemblyref:"", c->name_space,
518 *c->name_space?".":"", c->name);
519 g_free (assemblyref);
524 dis_stringify_type (MonoImage *m, MonoType *type)
526 const char *pinned = "", *byref = "";
527 char *bare = NULL, *mods = NULL;
531 mods = dis_stringify_modifiers (m, type->num_mods, type->modifiers);
534 case MONO_TYPE_BOOLEAN:
548 case MONO_TYPE_STRING:
549 case MONO_TYPE_OBJECT:
550 case MONO_TYPE_TYPEDBYREF:
551 bare = g_strdup (map (type->type, element_type_map));
554 case MONO_TYPE_VALUETYPE:
555 case MONO_TYPE_CLASS:
556 bare = dis_stringify_object (m, type);
559 case MONO_TYPE_FNPTR:
560 bare = dis_stringify_method_signature (m, type->data.method, 0);
563 case MONO_TYPE_SZARRAY: {
565 child_type = dis_stringify_type (m, type->data.type);
567 bare = g_strdup_printf (type->type == MONO_TYPE_PTR ? "%s*" : "%s[]", child_type);
571 case MONO_TYPE_ARRAY:
572 bare = dis_stringify_array (m, type->data.array);
575 bare = g_strdup ("void");
578 g_error ("Do not know how to stringify type 0x%x", type->type);
587 result = g_strconcat (mods ? mods : "", bare, byref, pinned, NULL);
596 * @m: metadata context
597 * @ptr: location to decode from.
598 * @result: pointer to string where resulting decoded string is stored
600 * This routine returs in @result the stringified type pointed by @ptr.
603 * Returns: the new ptr to continue decoding
606 get_type (MonoImage *m, const char *ptr, char **result)
608 MonoType *type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
609 *result = dis_stringify_type (m, type);
610 mono_metadata_free_type (type);
616 * Returns a stringified representation of a FieldSig (22.2.4)
619 get_field_signature (MonoImage *m, guint32 blob_signature)
621 char *allocated_modifier_string, *allocated_type_string;
622 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
627 len = mono_metadata_decode_value (ptr, &ptr);
630 g_assert (*ptr == 0x06);
631 /* hex_dump (ptr, 0, len); */
634 ptr = get_custom_mod (m, ptr, &allocated_modifier_string);
635 ptr = get_type (m, ptr, &allocated_type_string);
637 res = g_strdup_printf (
639 allocated_modifier_string ? allocated_modifier_string : "",
640 allocated_type_string);
642 if (allocated_modifier_string)
643 g_free (allocated_modifier_string);
644 if (allocated_type_string)
645 g_free (allocated_modifier_string);
651 get_field_literal_type (MonoImage *m, guint32 blob_signature)
653 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
655 char *allocated_modifier_string;
657 len = mono_metadata_decode_value (ptr, &ptr);
660 g_assert (*ptr == 0x06);
663 ptr = get_custom_mod (m, ptr, &allocated_modifier_string);
664 if (allocated_modifier_string)
665 g_free (allocated_modifier_string);
667 return (MonoTypeEnum) *ptr;
673 * @m: metadata context
674 * @token: token to decode
676 * decodes the literal indexed by @token.
679 decode_literal (MonoImage *m, guint32 token)
681 return g_strdup ("LITERAL_VALUE");
686 * @m: metadata context
687 * @ptr: location to decode from.
688 * @result: pointer to string where resulting decoded string is stored
690 * This routine returns in @result the stringified RetType (22.2.11)
692 * Returns: the new ptr to continue decoding.
695 get_ret_type (MonoImage *m, const char *ptr, char **ret_type)
697 GString *str = g_string_new ("");
699 char *allocated_type_string;
701 ptr = get_custom_mod (m, ptr, &mod);
703 g_string_append (str, mod);
704 g_string_append_c (str, ' ');
708 if (*ptr == MONO_TYPE_TYPEDBYREF){
709 /* TODO: what does `typedbyref' mean? */
710 g_string_append (str, "/* FIXME: What does this mean? */ typedbyref ");
712 } else if (*ptr == MONO_TYPE_VOID){
713 g_string_append (str, "void");
716 if (*ptr == MONO_TYPE_BYREF){
717 g_string_append (str, "[out] ");
721 ptr = get_type (m, ptr, &allocated_type_string);
722 g_string_append (str, allocated_type_string);
723 g_free (allocated_type_string);
726 *ret_type = str->str;
727 g_string_free (str, FALSE);
734 * @m: metadata context
735 * @ptr: location to decode from.
736 * @result: pointer to string where resulting decoded string is stored
738 * This routine returns in @result the stringified Param (22.2.10)
740 * Returns: the new ptr to continue decoding.
743 get_param (MonoImage *m, const char *ptr, char **retval)
745 GString *str = g_string_new ("");
746 char *allocated_mod_string, *allocated_type_string;
748 ptr = get_custom_mod (m, ptr, &allocated_mod_string);
749 if (allocated_mod_string){
750 g_string_append (str, allocated_mod_string);
751 g_string_append_c (str, ' ');
752 g_free (allocated_mod_string);
755 if (*ptr == MONO_TYPE_TYPEDBYREF){
756 g_string_append (str, "/*FIXME: what does typedbyref mean? */ typedbyref ");
759 if (*ptr == MONO_TYPE_BYREF){
760 g_string_append (str, "[out] ");
763 ptr = get_type (m, ptr, &allocated_type_string);
764 g_string_append (str, allocated_type_string);
765 g_free (allocated_type_string);
769 g_string_free (str, FALSE);
773 static map_t param_map [] = {
774 { PARAM_ATTRIBUTE_IN, "[in] " },
775 { PARAM_ATTRIBUTE_OUT, "[out] " },
776 { PARAM_ATTRIBUTE_OPTIONAL, "optional " },
777 { PARAM_ATTRIBUTE_HAS_DEFAULT, "hasdefault " },
778 { PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL, "fieldmarshal " },
783 param_flags (guint32 f)
785 return g_strdup (flags (f, param_map));
788 static map_t field_access_map [] = {
789 { FIELD_ATTRIBUTE_COMPILER_CONTROLLED, "compilercontrolled " },
790 { FIELD_ATTRIBUTE_PRIVATE, "private " },
791 { FIELD_ATTRIBUTE_FAM_AND_ASSEM, "famandassem " },
792 { FIELD_ATTRIBUTE_ASSEMBLY, "assembly " },
793 { FIELD_ATTRIBUTE_FAMILY, "family " },
794 { FIELD_ATTRIBUTE_FAM_OR_ASSEM, "famorassem " },
795 { FIELD_ATTRIBUTE_PUBLIC, "public " },
799 static map_t field_flags_map [] = {
800 { FIELD_ATTRIBUTE_STATIC, "static " },
801 { FIELD_ATTRIBUTE_INIT_ONLY, "initonly " },
802 { FIELD_ATTRIBUTE_LITERAL, "literal " },
803 { FIELD_ATTRIBUTE_NOT_SERIALIZED, "notserialized " },
804 { FIELD_ATTRIBUTE_SPECIAL_NAME, "specialname " },
805 { FIELD_ATTRIBUTE_PINVOKE_IMPL, "FIXME:pinvokeimpl " },
806 { FIELD_ATTRIBUTE_RT_SPECIAL_NAME, "rtspecialname " },
807 { FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL, "hasfieldmarshal " },
808 { FIELD_ATTRIBUTE_HAS_DEFAULT, "hasdefault " },
809 { FIELD_ATTRIBUTE_HAS_FIELD_RVA, "hasfieldrva " },
816 * Returns a stringified version of a Field's flags
819 field_flags (guint32 f)
822 int access = f & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK;
826 strcat (buffer, map (access, field_access_map));
827 strcat (buffer, flags (f, field_flags_map));
828 return g_strdup (buffer);
832 * Returns a stringifed representation of a MethodRefSig (22.2.2)
835 get_methodref_signature (MonoImage *m, guint32 blob_signature, const char *fancy_name)
837 GString *res = g_string_new ("");
838 const char *ptr = mono_metadata_blob_heap (m, blob_signature);
839 char *allocated_ret_type, *s;
840 gboolean seen_vararg = 0;
841 int param_count, signature_len;
844 signature_len = mono_metadata_decode_value (ptr, &ptr);
848 g_string_append (res, "explicit-this ");
850 g_string_append (res, "instance "); /* has-this */
857 param_count = mono_metadata_decode_value (ptr, &ptr);
858 ptr = get_ret_type (m, ptr, &allocated_ret_type);
860 g_string_append (res, allocated_ret_type);
863 g_string_append_c (res, ' ');
864 g_string_append (res, fancy_name);
867 g_string_append (res, "(");
870 * param_count describes parameters *before* and *after*
871 * the vararg sentinel
873 for (i = 0; i < param_count; i++){
877 * If ptr is a SENTINEL
880 g_string_append (res, " varargs ");
884 ptr = get_param (m, ptr, ¶m);
885 g_string_append (res, param);
886 if (i+1 != param_count)
887 g_string_append (res, ", ");
890 g_string_append (res, ")");
895 g_free (allocated_ret_type);
897 g_string_free (res, FALSE);
903 * @m: metadata context
904 * @token: a FIELD_DEF token
906 * This routine has to locate the TypeDef that "owns" this Field.
907 * Since there is no backpointer in the Field table, we have to scan
908 * the TypeDef table and locate the actual "owner" of the field
911 get_field (MonoImage *m, guint32 token)
913 int idx = mono_metadata_token_index (token);
914 guint32 cols [MONO_FIELD_SIZE];
915 char *sig, *res, *type;
919 * We can get here also with a MenberRef token (for a field
920 * defined in another module/assembly, just like in get_method ()
922 if (mono_metadata_token_code (token) == MONO_TOKEN_MEMBER_REF) {
923 return g_strdup_printf ("fieldref-0x%08x", token);
925 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_FIELD_DEF);
927 mono_metadata_decode_row (&m->tables [MONO_TABLE_FIELD], idx - 1, cols, MONO_FIELD_SIZE);
928 sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE]);
931 * To locate the actual "container" for this field, we have to scan
932 * the TypeDef table. LAME!
934 type_idx = mono_metadata_typedef_from_field (m, idx);
936 type = get_typedef (m, type_idx);
937 res = g_strdup_printf ("%s %s::%s",
939 mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]));
947 get_memberref_parent (MonoImage *m, guint32 mrp_token)
950 * mrp_index is a MemberRefParent coded index
952 guint32 table = mrp_token & 7;
953 guint32 idx = mrp_token >> 3;
956 case 0: /* TypeDef */
957 return get_typedef (m, idx);
959 case 1: /* TypeRef */
960 return get_typeref (m, idx);
962 case 2: /* ModuleRef */
963 return g_strdup_printf ("TODO:MemberRefParent-ModuleRef");
965 case 3: /* MethodDef */
966 return g_strdup ("TODO:MethodDef");
968 case 4: /* TypeSpec */
969 return get_typespec (m, idx);
971 g_assert_not_reached ();
977 * @m: metadata context
978 * @token: a METHOD_DEF or MEMBER_REF token
980 * This routine has to locate the TypeDef that "owns" this Field.
981 * Since there is no backpointer in the Field table, we have to scan
982 * the TypeDef table and locate the actual "owner" of the field
985 get_method (MonoImage *m, guint32 token)
987 int idx = mono_metadata_token_index (token);
988 guint32 member_cols [MONO_MEMBERREF_SIZE], method_cols [MONO_METHOD_SIZE];
994 mh = mono_get_method (m, token, NULL);
996 name = g_strdup_printf ("%s.%s::%s", mh->klass->name_space,
997 mh->klass->name, mh->name);
1001 switch (mono_metadata_token_code (token)){
1002 case MONO_TOKEN_METHOD_DEF:
1003 mono_metadata_decode_row (&m->tables [MONO_TABLE_METHOD],
1004 idx - 1, method_cols, MONO_METHOD_SIZE);
1006 sig = get_methodref_signature (m, method_cols [MONO_METHOD_SIGNATURE], name);
1009 case MONO_TOKEN_MEMBER_REF: {
1011 mono_metadata_decode_row (&m->tables [MONO_TABLE_MEMBERREF],
1012 idx - 1, member_cols, MONO_MEMBERREF_SIZE);
1014 name = g_strdup_printf ("%s::%s",
1015 get_memberref_parent (m, member_cols [MONO_MEMBERREF_CLASS]),
1016 mono_metadata_string_heap (m, member_cols [MONO_MEMBERREF_NAME]));
1017 sig = get_methodref_signature (
1018 m, member_cols [MONO_MEMBERREF_SIGNATURE], name);
1023 g_assert_not_reached ();
1025 g_assert_not_reached ();
1031 * @m: metadata context
1032 * @blob_index: index into the blob where the constant is stored
1034 * Returns: An allocated value representing a stringified version of the
1038 get_constant (MonoImage *m, MonoTypeEnum t, guint32 blob_index)
1040 const char *ptr = mono_metadata_blob_heap (m, blob_index);
1043 len = mono_metadata_decode_value (ptr, &ptr);
1046 case MONO_TYPE_BOOLEAN:
1047 return g_strdup_printf ("%s", *ptr ? "true" : "false");
1049 case MONO_TYPE_CHAR:
1050 return g_strdup_printf ("%c", *ptr); /* FIXME: unicode char */
1054 return g_strdup_printf ("int8(0x%02x)", (int) (*ptr));
1059 return g_strdup_printf ("int16(0x%08x)", (int) read16 (ptr));
1063 return g_strdup_printf ("int32(%d)", read32 (ptr));
1065 case MONO_TYPE_I8: {
1068 high = read32 (ptr + 4);
1069 return g_strdup_printf ("0x%08x%08x", high, low);
1071 case MONO_TYPE_U8: {
1074 high = read32 (ptr + 4);
1075 return g_strdup_printf ("0x%08x%08x", high, low);
1077 case MONO_TYPE_R4: {
1080 return g_strdup_printf ("%g", (double) r);
1082 case MONO_TYPE_R8: {
1085 return g_strdup_printf ("%g", r);
1087 case MONO_TYPE_STRING: {
1091 for (i = 0; !ptr [i+1]; i += 2){
1096 case '\n': /* add more */
1100 res = g_malloc (len + e + 3);
1104 for (i = 0; i < len; i += 2){
1117 res[j++] = isprint (ptr [i]) ? ptr [i] : '.';
1126 case MONO_TYPE_CLASS:
1127 return g_strdup ("CLASS CONSTANT. MUST BE ZERO");
1130 g_error ("Unknown MONO_TYPE (%d) on constant at Blob index (0x%08x)\n",
1131 (int) *ptr, blob_index);
1132 return g_strdup_printf ("Unknown");
1139 * @m: metadata context
1140 * @token: token that we want to decode.
1142 * Returns: An allocated value representing a stringified version of the
1146 get_token (MonoImage *m, guint32 token)
1148 guint32 idx = mono_metadata_token_index (token);
1150 switch (mono_metadata_token_code (token)){
1151 case MONO_TOKEN_FIELD_DEF:
1152 return (get_field (m, token));
1153 case MONO_TOKEN_TYPE_DEF:
1154 return get_typedef (m, idx);
1155 case MONO_TOKEN_TYPE_REF:
1156 return get_typeref (m, idx);
1157 case MONO_TOKEN_TYPE_SPEC:
1158 return get_typespec (m, idx);
1160 g_error ("Do not know how to decode tokens of type 0x%08x", token);
1163 g_assert_not_reached ();
1164 return g_strdup ("ERROR");
1169 * @m: metadata context
1170 * @token: the token can belong to any of the following tables:
1171 * MONO_TOKEN_TYPE_REF, MONO_TOKEN_TYPE_DEF, MONO_TOKEN_TYPE_SPEC
1173 * Returns: a stringified version of the MethodDef or MethodRef or TypeSpecn
1174 * at (token & 0xffffff)
1177 get_token_type (MonoImage *m, guint32 token)
1179 char *temp = NULL, *s;
1182 idx = mono_metadata_token_index (token);
1184 switch (mono_metadata_token_code (token)){
1185 case MONO_TOKEN_TYPE_DEF:
1186 temp = get_typedef (m, idx);
1187 s = g_strdup_printf ("%s", temp);
1190 case MONO_TOKEN_TYPE_REF:
1191 temp = get_typeref (m, idx);
1192 s = g_strdup_printf ("%s", temp);
1195 case MONO_TOKEN_TYPE_SPEC:
1196 s = get_typespec (m, idx);
1200 g_error ("Unhandled encoding for typedef-or-ref coded index");
1211 get_guid (MonoImage *m, guint32 guid_index)
1213 const unsigned char *guid;
1216 guid = mono_metadata_guid_heap (m, guid_index);
1218 result = g_strdup_printf ("{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
1219 guid [3], guid [2], guid [1], guid [0], guid [5], guid [4], guid [7], guid [6],
1220 guid [8], guid [9], guid [10], guid [11], guid [12], guid [13], guid [14], guid [15]);