2 using System.Collections.Generic;
3 using System.Reflection;
5 using Mono.Cecil.Metadata;
7 namespace Mono.Debugger.Soft
10 * Represents a type in the remote virtual machine.
11 * It might be better to make this a subclass of Type, but that could be
12 * difficult as some of our methods like GetMethods () return Mirror objects.
14 public class TypeMirror : Mirror
16 MethodMirror[] methods;
19 C.TypeDefinition meta;
20 FieldInfoMirror[] fields;
21 PropertyInfoMirror[] properties;
23 TypeMirror base_type, element_type, gtd;
25 CustomAttributeDataMirror[] cattrs;
27 Dictionary<TypeMirror, InterfaceMappingMirror> iface_map;
28 TypeMirror[] type_args;
30 internal const BindingFlags DefaultBindingFlags =
31 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
33 internal TypeMirror (VirtualMachine vm, long id) : base (vm, id) {
38 return GetInfo ().name;
42 public string Namespace {
48 public AssemblyMirror Assembly {
51 ass = vm.GetAssembly (GetInfo ().assembly);
57 public ModuleMirror Module {
60 module = vm.GetModule (GetInfo ().module);
66 public int MetadataToken {
68 return GetInfo ().token;
72 public TypeAttributes Attributes {
74 return (TypeAttributes)GetInfo ().attributes;
78 public TypeMirror BaseType {
80 // FIXME: base_type could be null for object/interfaces
81 if (base_type == null) {
82 base_type = vm.GetType (GetInfo ().base_type);
88 public int GetArrayRank () {
91 throw new ArgumentException ("Type must be an array type.");
96 public bool IsAbstract {
98 return (Attributes & TypeAttributes.Abstract) != 0;
102 public bool IsAnsiClass {
104 return (Attributes & TypeAttributes.StringFormatMask)
105 == TypeAttributes.AnsiClass;
109 public bool IsArray {
111 return IsArrayImpl ();
115 public bool IsAutoClass {
117 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
121 public bool IsAutoLayout {
123 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
127 public bool IsByRef {
129 return IsByRefImpl ();
133 public bool IsClass {
142 public bool IsCOMObject {
144 return IsCOMObjectImpl ();
148 public bool IsContextful {
150 return IsContextfulImpl ();
156 return GetInfo ().is_enum;
160 public bool IsExplicitLayout {
162 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
166 public bool IsImport {
168 return (Attributes & TypeAttributes.Import) != 0;
172 public bool IsInterface {
174 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
178 public bool IsLayoutSequential {
180 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
184 public bool IsMarshalByRef {
186 return IsMarshalByRefImpl ();
190 public bool IsNestedAssembly {
192 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
196 public bool IsNestedFamANDAssem {
198 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
202 public bool IsNestedFamily {
204 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
208 public bool IsNestedFamORAssem {
210 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
214 public bool IsNestedPrivate {
216 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
220 public bool IsNestedPublic {
222 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
226 public bool IsNotPublic {
228 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
232 public bool IsPointer {
234 return IsPointerImpl ();
238 public bool IsPrimitive {
240 return IsPrimitiveImpl ();
244 public bool IsPublic {
246 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
250 public bool IsSealed {
252 return (Attributes & TypeAttributes.Sealed) != 0;
256 public bool IsSerializable {
258 if ((Attributes & TypeAttributes.Serializable) != 0)
266 public bool IsSpecialName {
268 return (Attributes & TypeAttributes.SpecialName) != 0;
272 public bool IsUnicodeClass {
274 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
278 public bool IsValueType {
280 return IsValueTypeImpl ();
284 public bool HasElementType {
286 return HasElementTypeImpl ();
290 // Since protocol version 2.12
291 public bool IsGenericTypeDefinition {
293 vm.CheckProtocolVersion (2, 12);
299 public bool IsGenericType {
301 if (vm.Version.AtLeast (2, 12)) {
302 return GetInfo ().is_generic_type;
304 return Name.IndexOf ('`') != -1;
309 public TypeMirror GetElementType () {
311 if (element_type == null && info.element_type != 0)
312 element_type = vm.GetType (info.element_type);
316 public TypeMirror GetGenericTypeDefinition () {
317 vm.CheckProtocolVersion (2, 12);
321 throw new InvalidOperationException ();
322 gtd = vm.GetType (info.gtd);
327 // Since protocol version 2.15
328 public TypeMirror[] GetGenericArguments () {
329 vm.CheckProtocolVersion (2, 15);
330 if (type_args == null)
331 type_args = vm.GetTypes (GetInfo ().type_args);
335 public string FullName {
337 return GetInfo ().full_name;
341 public string CSharpName {
344 if (GetArrayRank () == 1)
345 return GetElementType ().CSharpName + "[]";
348 for (int i = 0; i < GetArrayRank (); ++i)
350 return GetElementType ().CSharpName + "[" + ranks + "]";
383 // FIXME: Only do this for real corlib types
384 if (Namespace == "System") {
402 public MethodMirror[] GetMethods () {
403 if (methods == null) {
404 long[] ids = vm.conn.Type_GetMethods (id);
405 MethodMirror[] m = new MethodMirror [ids.Length];
406 for (int i = 0; i < ids.Length; ++i) {
407 m [i] = vm.GetMethod (ids [i]);
414 // FIXME: Sync this with Type
415 public MethodMirror GetMethod (string name) {
416 foreach (var m in GetMethods ())
422 public FieldInfoMirror[] GetFields () {
429 long[] ids = vm.conn.Type_GetFields (id, out names, out types, out attrs);
431 FieldInfoMirror[] res = new FieldInfoMirror [ids.Length];
432 for (int i = 0; i < res.Length; ++i)
433 res [i] = new FieldInfoMirror (this, ids [i], names [i], vm.GetType (types [i]), (FieldAttributes)attrs [i]);
439 public FieldInfoMirror GetField (string name) {
441 throw new ArgumentNullException ("name");
442 foreach (var f in GetFields ())
448 public TypeMirror[] GetNestedTypes ()
450 return GetNestedTypes (DefaultBindingFlags);
453 public TypeMirror[] GetNestedTypes (BindingFlags bindingAttr) {
457 // FIXME: bindingAttr
459 var arr = new TypeMirror [info.nested.Length];
460 for (int i = 0; i < arr.Length; ++i)
461 arr [i] = vm.GetType (info.nested [i]);
467 public PropertyInfoMirror[] GetProperties () {
468 return GetProperties (DefaultBindingFlags);
471 public PropertyInfoMirror[] GetProperties (BindingFlags bindingAttr) {
472 if (properties != null)
475 PropInfo[] info = vm.conn.Type_GetProperties (id);
477 PropertyInfoMirror[] res = new PropertyInfoMirror [info.Length];
478 for (int i = 0; i < res.Length; ++i)
479 res [i] = new PropertyInfoMirror (this, info [i].id, info [i].name, vm.GetMethod (info [i].get_method), vm.GetMethod (info [i].set_method), (PropertyAttributes)info [i].attrs);
485 public PropertyInfoMirror GetProperty (string name) {
487 throw new ArgumentNullException ("name");
488 foreach (var p in GetProperties ())
494 public virtual bool IsAssignableFrom (TypeMirror c) {
496 throw new ArgumentNullException ("c");
500 // This is complex so do it in the debuggee
501 return vm.conn.Type_IsAssignableFrom (id, c.Id);
504 public Value GetValue (FieldInfoMirror field) {
505 return GetValues (new FieldInfoMirror [] { field }) [0];
508 public Value[] GetValues (IList<FieldInfoMirror> fields, ThreadMirror thread) {
510 throw new ArgumentNullException ("fields");
511 foreach (FieldInfoMirror f in fields) {
513 throw new ArgumentNullException ("field");
516 long[] ids = new long [fields.Count];
517 for (int i = 0; i < fields.Count; ++i)
518 ids [i] = fields [i].Id;
520 return vm.DecodeValues (vm.conn.Type_GetValues (id, ids, thread != null ? thread.Id : 0));
521 } catch (CommandException ex) {
522 if (ex.ErrorCode == ErrorCode.INVALID_FIELDID)
523 throw new ArgumentException ("One of the fields is not valid for this type.", "fields");
529 public Value[] GetValues (IList<FieldInfoMirror> fields) {
530 return GetValues (fields, null);
534 * Return the value of the [ThreadStatic] field FIELD on the thread THREAD.
536 public Value GetValue (FieldInfoMirror field, ThreadMirror thread) {
538 throw new ArgumentNullException ("thread");
539 CheckMirror (thread);
540 return GetValues (new FieldInfoMirror [] { field }, thread) [0];
543 public void SetValues (IList<FieldInfoMirror> fields, Value[] values) {
545 throw new ArgumentNullException ("fields");
547 throw new ArgumentNullException ("values");
548 foreach (FieldInfoMirror f in fields) {
550 throw new ArgumentNullException ("field");
553 foreach (Value v in values) {
555 throw new ArgumentNullException ("values");
558 long[] ids = new long [fields.Count];
559 for (int i = 0; i < fields.Count; ++i)
560 ids [i] = fields [i].Id;
562 vm.conn.Type_SetValues (id, ids, vm.EncodeValues (values));
563 } catch (CommandException ex) {
564 if (ex.ErrorCode == ErrorCode.INVALID_FIELDID)
565 throw new ArgumentException ("One of the fields is not valid for this type.", "fields");
571 public void SetValue (FieldInfoMirror field, Value value) {
572 SetValues (new FieldInfoMirror [] { field }, new Value [] { value });
575 public ObjectMirror GetTypeObject () {
576 return vm.GetObject (vm.conn.Type_GetObject (id));
580 * Return a list of source files without path info, where methods of
581 * this type are defined. Return an empty list if the information is not
583 * This can be used by a debugger to find out which types occur in a
584 * given source file, to filter the list of methods whose locations
585 * have to be checked when placing breakpoints.
587 public string[] GetSourceFiles () {
588 return GetSourceFiles (false);
591 string[] source_files;
592 string[] source_files_full_path;
593 public string[] GetSourceFiles (bool return_full_paths) {
594 string[] res = return_full_paths ? source_files_full_path : source_files;
596 res = vm.conn.Type_GetSourceFiles (id, return_full_paths);
597 if (return_full_paths)
598 source_files_full_path = res;
605 public C.TypeDefinition Metadata {
608 if (Assembly.Metadata == null || MetadataToken == 0)
610 meta = (C.TypeDefinition)Assembly.Metadata.MainModule.LookupToken (MetadataToken);
616 TypeInfo GetInfo () {
618 info = vm.conn.Type_GetInfo (id);
622 protected virtual TypeAttributes GetAttributeFlagsImpl () {
623 return (TypeAttributes)GetInfo ().attributes;
626 protected virtual bool HasElementTypeImpl () {
627 return IsArray || IsByRef || IsPointer;
630 protected virtual bool IsArrayImpl () {
631 return GetInfo ().rank > 0;
634 protected virtual bool IsByRefImpl () {
635 return GetInfo ().is_byref;
638 protected virtual bool IsCOMObjectImpl () {
642 protected virtual bool IsPointerImpl () {
643 return GetInfo ().is_pointer;
646 protected virtual bool IsPrimitiveImpl () {
647 return GetInfo ().is_primitive;
650 protected virtual bool IsValueTypeImpl ()
652 return GetInfo ().is_valuetype;
655 protected virtual bool IsContextfulImpl ()
661 protected virtual bool IsMarshalByRefImpl ()
667 // Same as Enum.GetUnderlyingType ()
668 public TypeMirror EnumUnderlyingType {
671 throw new ArgumentException ("Type is not an enum type.");
672 foreach (FieldInfoMirror f in GetFields ()) {
676 throw new NotImplementedException ();
681 * Creating the custom attributes themselves could modify the behavior of the
682 * debuggee, so we return objects similar to the CustomAttributeData objects
683 * used by the reflection-only functionality on .net.
685 public CustomAttributeDataMirror[] GetCustomAttributes (bool inherit) {
686 return GetCAttrs (null, inherit);
689 public CustomAttributeDataMirror[] GetCustomAttributes (TypeMirror attributeType, bool inherit) {
690 if (attributeType == null)
691 throw new ArgumentNullException ("attributeType");
692 return GetCAttrs (attributeType, inherit);
695 CustomAttributeDataMirror[] GetCAttrs (TypeMirror type, bool inherit) {
696 if (cattrs == null && Metadata != null && !Metadata.HasCustomAttributes)
697 cattrs = new CustomAttributeDataMirror [0];
699 // FIXME: Handle inherit
700 if (cattrs == null) {
701 CattrInfo[] info = vm.conn.Type_GetCustomAttributes (id, 0, false);
702 cattrs = CustomAttributeDataMirror.Create (vm, info);
704 var res = new List<CustomAttributeDataMirror> ();
705 foreach (var attr in cattrs)
706 if (type == null || attr.Constructor.DeclaringType == type)
708 return res.ToArray ();
711 public MethodMirror[] GetMethodsByNameFlags (string name, BindingFlags flags, bool ignoreCase) {
712 if (vm.conn.Version.AtLeast (2, 6)) {
713 long[] ids = vm.conn.Type_GetMethodsByNameFlags (id, name, (int)flags, ignoreCase);
714 MethodMirror[] m = new MethodMirror [ids.Length];
715 for (int i = 0; i < ids.Length; ++i)
716 m [i] = vm.GetMethod (ids [i]);
719 if ((flags & BindingFlags.IgnoreCase) != 0) {
720 flags &= ~BindingFlags.IgnoreCase;
724 if (flags == BindingFlags.Default)
725 flags = BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.Static;
727 MethodAttributes access = (MethodAttributes) 0;
728 bool matchInstance = false;
729 bool matchStatic = false;
731 if ((flags & BindingFlags.NonPublic) != 0) {
732 access |= MethodAttributes.Private;
733 flags &= ~BindingFlags.NonPublic;
735 if ((flags & BindingFlags.Public) != 0) {
736 access |= MethodAttributes.Public;
737 flags &= ~BindingFlags.Public;
739 if ((flags & BindingFlags.Instance) != 0) {
740 flags &= ~BindingFlags.Instance;
741 matchInstance = true;
743 if ((flags & BindingFlags.Static) != 0) {
744 flags &= ~BindingFlags.Static;
748 if ((int) flags != 0)
749 throw new NotImplementedException ();
751 var res = new List<MethodMirror> ();
752 foreach (MethodMirror m in GetMethods ()) {
753 if ((m.Attributes & access) == (MethodAttributes) 0)
756 if (!((matchStatic && m.IsStatic) || (matchInstance && !m.IsStatic)))
759 if ((!ignoreCase && m.Name == name) || (ignoreCase && m.Name.Equals (name, StringComparison.CurrentCultureIgnoreCase)))
762 return res.ToArray ();
766 public Value InvokeMethod (ThreadMirror thread, MethodMirror method, IList<Value> arguments) {
767 return ObjectMirror.InvokeMethod (vm, thread, method, null, arguments, InvokeOptions.None);
770 public Value InvokeMethod (ThreadMirror thread, MethodMirror method, IList<Value> arguments, InvokeOptions options) {
771 return ObjectMirror.InvokeMethod (vm, thread, method, null, arguments, options);
774 [Obsolete ("Use the overload without the 'vm' argument")]
775 public IAsyncResult BeginInvokeMethod (VirtualMachine vm, ThreadMirror thread, MethodMirror method, IList<Value> arguments, InvokeOptions options, AsyncCallback callback, object state) {
776 return ObjectMirror.BeginInvokeMethod (vm, thread, method, null, arguments, options, callback, state);
779 public IAsyncResult BeginInvokeMethod (ThreadMirror thread, MethodMirror method, IList<Value> arguments, InvokeOptions options, AsyncCallback callback, object state) {
780 return ObjectMirror.BeginInvokeMethod (vm, thread, method, null, arguments, options, callback, state);
783 public Value EndInvokeMethod (IAsyncResult asyncResult) {
784 return ObjectMirror.EndInvokeMethodInternal (asyncResult);
787 public Value NewInstance (ThreadMirror thread, MethodMirror method, IList<Value> arguments) {
788 return ObjectMirror.InvokeMethod (vm, thread, method, null, arguments, InvokeOptions.None);
791 public Value NewInstance (ThreadMirror thread, MethodMirror method, IList<Value> arguments, InvokeOptions options) {
792 return ObjectMirror.InvokeMethod (vm, thread, method, null, arguments, options);
795 // Since protocol version 2.11
796 public TypeMirror[] GetInterfaces () {
798 ifaces = vm.GetTypes (vm.conn.Type_GetInterfaces (id));
802 // Since protocol version 2.11
803 public InterfaceMappingMirror GetInterfaceMap (TypeMirror interfaceType) {
804 if (interfaceType == null)
805 throw new ArgumentNullException ("interfaceType");
806 if (!interfaceType.IsInterface)
807 throw new ArgumentException ("Argument must be an interface.", "interfaceType");
809 throw new ArgumentException ("'this' type cannot be an interface itself");
811 if (iface_map == null) {
812 // Query the info in bulk
814 var ids = new long [ifaces.Length];
815 for (int i = 0; i < ifaces.Length; ++i)
816 ids [i] = ifaces [i].Id;
818 var ifacemap = vm.conn.Type_GetInterfaceMap (id, ids);
820 var imap = new Dictionary<TypeMirror, InterfaceMappingMirror> ();
821 for (int i = 0; i < ifacemap.Length; ++i) {
822 IfaceMapInfo info = ifacemap [i];
824 MethodMirror[] imethods = new MethodMirror [info.iface_methods.Length];
825 for (int j = 0; j < info.iface_methods.Length; ++j)
826 imethods [j] = vm.GetMethod (info.iface_methods [j]);
828 MethodMirror[] tmethods = new MethodMirror [info.iface_methods.Length];
829 for (int j = 0; j < info.target_methods.Length; ++j)
830 tmethods [j] = vm.GetMethod (info.target_methods [j]);
832 InterfaceMappingMirror map = new InterfaceMappingMirror (vm, this, vm.GetType (info.iface_id), imethods, tmethods);
834 imap [map.InterfaceType] = map;
840 InterfaceMappingMirror res;
841 if (!iface_map.TryGetValue (interfaceType, out res))
842 throw new ArgumentException ("Interface not found", "interfaceType");