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;
29 internal const BindingFlags DefaultBindingFlags =
30 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
32 internal TypeMirror (VirtualMachine vm, long id) : base (vm, id) {
37 return GetInfo ().name;
41 public string Namespace {
47 public AssemblyMirror Assembly {
50 ass = vm.GetAssembly (GetInfo ().assembly);
56 public ModuleMirror Module {
59 module = vm.GetModule (GetInfo ().module);
65 public int MetadataToken {
67 return GetInfo ().token;
71 public TypeAttributes Attributes {
73 return (TypeAttributes)GetInfo ().attributes;
77 public TypeMirror BaseType {
79 // FIXME: base_type could be null for object/interfaces
80 if (base_type == null) {
81 base_type = vm.GetType (GetInfo ().base_type);
87 public int GetArrayRank () {
90 throw new ArgumentException ("Type must be an array type.");
95 public bool IsAbstract {
97 return (Attributes & TypeAttributes.Abstract) != 0;
101 public bool IsAnsiClass {
103 return (Attributes & TypeAttributes.StringFormatMask)
104 == TypeAttributes.AnsiClass;
108 public bool IsArray {
110 return IsArrayImpl ();
114 public bool IsAutoClass {
116 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
120 public bool IsAutoLayout {
122 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
126 public bool IsByRef {
128 return IsByRefImpl ();
132 public bool IsClass {
141 public bool IsCOMObject {
143 return IsCOMObjectImpl ();
147 public bool IsContextful {
149 return IsContextfulImpl ();
155 return GetInfo ().is_enum;
159 public bool IsExplicitLayout {
161 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
165 public bool IsImport {
167 return (Attributes & TypeAttributes.Import) != 0;
171 public bool IsInterface {
173 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
177 public bool IsLayoutSequential {
179 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
183 public bool IsMarshalByRef {
185 return IsMarshalByRefImpl ();
189 public bool IsNestedAssembly {
191 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
195 public bool IsNestedFamANDAssem {
197 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
201 public bool IsNestedFamily {
203 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
207 public bool IsNestedFamORAssem {
209 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
213 public bool IsNestedPrivate {
215 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
219 public bool IsNestedPublic {
221 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
225 public bool IsNotPublic {
227 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
231 public bool IsPointer {
233 return IsPointerImpl ();
237 public bool IsPrimitive {
239 return IsPrimitiveImpl ();
243 public bool IsPublic {
245 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
249 public bool IsSealed {
251 return (Attributes & TypeAttributes.Sealed) != 0;
255 public bool IsSerializable {
257 if ((Attributes & TypeAttributes.Serializable) != 0)
265 public bool IsSpecialName {
267 return (Attributes & TypeAttributes.SpecialName) != 0;
271 public bool IsUnicodeClass {
273 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
277 public bool IsValueType {
279 return IsValueTypeImpl ();
283 public bool HasElementType {
285 return HasElementTypeImpl ();
289 // Since protocol version 2.12
290 public bool IsGenericTypeDefinition {
292 vm.CheckProtocolVersion (2, 12);
298 // Since protocol version 2.12
299 public bool IsGenericType {
301 vm.CheckProtocolVersion (2, 12);
303 return info.is_generic_type;
307 public TypeMirror GetElementType () {
309 if (element_type == null && info.element_type != 0)
310 element_type = vm.GetType (info.element_type);
314 public TypeMirror GetGenericTypeDefinition () {
315 vm.CheckProtocolVersion (2, 12);
319 throw new InvalidOperationException ();
320 gtd = vm.GetType (info.gtd);
325 public string FullName {
327 return GetInfo ().full_name;
331 public string CSharpName {
334 if (GetArrayRank () == 1)
335 return GetElementType ().CSharpName + "[]";
338 for (int i = 0; i < GetArrayRank (); ++i)
340 return GetElementType ().CSharpName + "[" + ranks + "]";
355 // FIXME: Only do this for real corlib types
356 if (Namespace == "System") {
370 public MethodMirror[] GetMethods () {
371 if (methods == null) {
372 long[] ids = vm.conn.Type_GetMethods (id);
373 MethodMirror[] m = new MethodMirror [ids.Length];
374 for (int i = 0; i < ids.Length; ++i) {
375 m [i] = vm.GetMethod (ids [i]);
382 // FIXME: Sync this with Type
383 public MethodMirror GetMethod (string name) {
384 foreach (var m in GetMethods ())
390 public FieldInfoMirror[] GetFields () {
397 long[] ids = vm.conn.Type_GetFields (id, out names, out types, out attrs);
399 FieldInfoMirror[] res = new FieldInfoMirror [ids.Length];
400 for (int i = 0; i < res.Length; ++i)
401 res [i] = new FieldInfoMirror (this, ids [i], names [i], vm.GetType (types [i]), (FieldAttributes)attrs [i]);
407 public FieldInfoMirror GetField (string name) {
409 throw new ArgumentNullException ("name");
410 foreach (var f in GetFields ())
416 public TypeMirror[] GetNestedTypes ()
418 return GetNestedTypes (DefaultBindingFlags);
421 public TypeMirror[] GetNestedTypes (BindingFlags bindingAttr) {
425 // FIXME: bindingAttr
427 var arr = new TypeMirror [info.nested.Length];
428 for (int i = 0; i < arr.Length; ++i)
429 arr [i] = vm.GetType (info.nested [i]);
435 public PropertyInfoMirror[] GetProperties () {
436 return GetProperties (DefaultBindingFlags);
439 public PropertyInfoMirror[] GetProperties (BindingFlags bindingAttr) {
440 if (properties != null)
443 PropInfo[] info = vm.conn.Type_GetProperties (id);
445 PropertyInfoMirror[] res = new PropertyInfoMirror [info.Length];
446 for (int i = 0; i < res.Length; ++i)
447 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);
453 public PropertyInfoMirror GetProperty (string name) {
455 throw new ArgumentNullException ("name");
456 foreach (var p in GetProperties ())
462 public virtual bool IsAssignableFrom (TypeMirror c) {
464 throw new ArgumentNullException ("c");
468 // This is complex so do it in the debuggee
469 return vm.conn.Type_IsAssignableFrom (id, c.Id);
472 public Value GetValue (FieldInfoMirror field) {
473 return GetValues (new FieldInfoMirror [] { field }) [0];
476 public Value[] GetValues (IList<FieldInfoMirror> fields, ThreadMirror thread) {
478 throw new ArgumentNullException ("fields");
479 foreach (FieldInfoMirror f in fields) {
481 throw new ArgumentNullException ("field");
484 long[] ids = new long [fields.Count];
485 for (int i = 0; i < fields.Count; ++i)
486 ids [i] = fields [i].Id;
488 return vm.DecodeValues (vm.conn.Type_GetValues (id, ids, thread != null ? thread.Id : 0));
489 } catch (CommandException ex) {
490 if (ex.ErrorCode == ErrorCode.INVALID_FIELDID)
491 throw new ArgumentException ("One of the fields is not valid for this type.", "fields");
497 public Value[] GetValues (IList<FieldInfoMirror> fields) {
498 return GetValues (fields, null);
502 * Return the value of the [ThreadStatic] field FIELD on the thread THREAD.
504 public Value GetValue (FieldInfoMirror field, ThreadMirror thread) {
506 throw new ArgumentNullException ("thread");
507 CheckMirror (thread);
508 return GetValues (new FieldInfoMirror [] { field }, thread) [0];
511 public void SetValues (IList<FieldInfoMirror> fields, Value[] values) {
513 throw new ArgumentNullException ("fields");
515 throw new ArgumentNullException ("values");
516 foreach (FieldInfoMirror f in fields) {
518 throw new ArgumentNullException ("field");
521 foreach (Value v in values) {
523 throw new ArgumentNullException ("values");
526 long[] ids = new long [fields.Count];
527 for (int i = 0; i < fields.Count; ++i)
528 ids [i] = fields [i].Id;
530 vm.conn.Type_SetValues (id, ids, vm.EncodeValues (values));
531 } catch (CommandException ex) {
532 if (ex.ErrorCode == ErrorCode.INVALID_FIELDID)
533 throw new ArgumentException ("One of the fields is not valid for this type.", "fields");
539 public void SetValue (FieldInfoMirror field, Value value) {
540 SetValues (new FieldInfoMirror [] { field }, new Value [] { value });
543 public ObjectMirror GetTypeObject () {
544 return vm.GetObject (vm.conn.Type_GetObject (id));
548 * Return a list of source files without path info, where methods of
549 * this type are defined. Return an empty list if the information is not
551 * This can be used by a debugger to find out which types occur in a
552 * given source file, to filter the list of methods whose locations
553 * have to be checked when placing breakpoints.
555 public string[] GetSourceFiles () {
556 return GetSourceFiles (false);
559 string[] source_files;
560 string[] source_files_full_path;
561 public string[] GetSourceFiles (bool return_full_paths) {
562 string[] res = return_full_paths ? source_files_full_path : source_files;
564 res = vm.conn.Type_GetSourceFiles (id, return_full_paths);
565 if (return_full_paths)
566 source_files_full_path = res;
573 public C.TypeDefinition Metadata {
576 if (Assembly.Metadata == null || MetadataToken == 0)
578 meta = (C.TypeDefinition)Assembly.Metadata.MainModule.LookupToken (MetadataToken);
584 TypeInfo GetInfo () {
586 info = vm.conn.Type_GetInfo (id);
590 protected virtual TypeAttributes GetAttributeFlagsImpl () {
591 return (TypeAttributes)GetInfo ().attributes;
594 protected virtual bool HasElementTypeImpl () {
595 return IsArray || IsByRef || IsPointer;
598 protected virtual bool IsArrayImpl () {
599 return GetInfo ().rank > 0;
602 protected virtual bool IsByRefImpl () {
603 return GetInfo ().is_byref;
606 protected virtual bool IsCOMObjectImpl () {
610 protected virtual bool IsPointerImpl () {
611 return GetInfo ().is_pointer;
614 protected virtual bool IsPrimitiveImpl () {
615 return GetInfo ().is_primitive;
618 protected virtual bool IsValueTypeImpl ()
620 return GetInfo ().is_valuetype;
623 protected virtual bool IsContextfulImpl ()
629 protected virtual bool IsMarshalByRefImpl ()
635 // Same as Enum.GetUnderlyingType ()
636 public TypeMirror EnumUnderlyingType {
639 throw new ArgumentException ("Type is not an enum type.");
640 foreach (FieldInfoMirror f in GetFields ()) {
644 throw new NotImplementedException ();
649 * Creating the custom attributes themselves could modify the behavior of the
650 * debuggee, so we return objects similar to the CustomAttributeData objects
651 * used by the reflection-only functionality on .net.
653 public CustomAttributeDataMirror[] GetCustomAttributes (bool inherit) {
654 return GetCAttrs (null, inherit);
657 public CustomAttributeDataMirror[] GetCustomAttributes (TypeMirror attributeType, bool inherit) {
658 if (attributeType == null)
659 throw new ArgumentNullException ("attributeType");
660 return GetCAttrs (attributeType, inherit);
663 CustomAttributeDataMirror[] GetCAttrs (TypeMirror type, bool inherit) {
664 if (cattrs == null && Metadata != null && !Metadata.HasCustomAttributes)
665 cattrs = new CustomAttributeDataMirror [0];
667 // FIXME: Handle inherit
668 if (cattrs == null) {
669 CattrInfo[] info = vm.conn.Type_GetCustomAttributes (id, 0, false);
670 cattrs = CustomAttributeDataMirror.Create (vm, info);
672 var res = new List<CustomAttributeDataMirror> ();
673 foreach (var attr in cattrs)
674 if (type == null || attr.Constructor.DeclaringType == type)
676 return res.ToArray ();
679 public MethodMirror[] GetMethodsByNameFlags (string name, BindingFlags flags, bool ignoreCase) {
680 if (vm.conn.Version.AtLeast (2, 6)) {
681 long[] ids = vm.conn.Type_GetMethodsByNameFlags (id, name, (int)flags, ignoreCase);
682 MethodMirror[] m = new MethodMirror [ids.Length];
683 for (int i = 0; i < ids.Length; ++i)
684 m [i] = vm.GetMethod (ids [i]);
687 if (flags != (BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.NonPublic))
688 throw new NotImplementedException ();
689 var res = new List<MethodMirror> ();
690 foreach (MethodMirror m in GetMethods ()) {
691 if ((!ignoreCase && m.Name == name) || (ignoreCase && m.Name.Equals (name, StringComparison.CurrentCultureIgnoreCase)))
694 return res.ToArray ();
698 public Value InvokeMethod (ThreadMirror thread, MethodMirror method, IList<Value> arguments) {
699 return ObjectMirror.InvokeMethod (vm, thread, method, null, arguments, InvokeOptions.None);
702 public Value InvokeMethod (ThreadMirror thread, MethodMirror method, IList<Value> arguments, InvokeOptions options) {
703 return ObjectMirror.InvokeMethod (vm, thread, method, null, arguments, options);
706 [Obsolete ("Use the overload without the 'vm' argument")]
707 public IAsyncResult BeginInvokeMethod (VirtualMachine vm, ThreadMirror thread, MethodMirror method, IList<Value> arguments, InvokeOptions options, AsyncCallback callback, object state) {
708 return ObjectMirror.BeginInvokeMethod (vm, thread, method, null, arguments, options, callback, state);
711 public IAsyncResult BeginInvokeMethod (ThreadMirror thread, MethodMirror method, IList<Value> arguments, InvokeOptions options, AsyncCallback callback, object state) {
712 return ObjectMirror.BeginInvokeMethod (vm, thread, method, null, arguments, options, callback, state);
715 public Value EndInvokeMethod (IAsyncResult asyncResult) {
716 return ObjectMirror.EndInvokeMethodInternal (asyncResult);
719 public Value NewInstance (ThreadMirror thread, MethodMirror method, IList<Value> arguments) {
720 return ObjectMirror.InvokeMethod (vm, thread, method, null, arguments, InvokeOptions.None);
723 public Value NewInstance (ThreadMirror thread, MethodMirror method, IList<Value> arguments, InvokeOptions options) {
724 return ObjectMirror.InvokeMethod (vm, thread, method, null, arguments, options);
727 // Since protocol version 2.11
728 public TypeMirror[] GetInterfaces () {
730 ifaces = vm.GetTypes (vm.conn.Type_GetInterfaces (id));
734 // Since protocol version 2.11
735 public InterfaceMappingMirror GetInterfaceMap (TypeMirror interfaceType) {
736 if (interfaceType == null)
737 throw new ArgumentNullException ("interfaceType");
738 if (!interfaceType.IsInterface)
739 throw new ArgumentException ("Argument must be an interface.", "interfaceType");
741 throw new ArgumentException ("'this' type cannot be an interface itself");
743 if (iface_map == null) {
744 // Query the info in bulk
746 var ids = new long [ifaces.Length];
747 for (int i = 0; i < ifaces.Length; ++i)
748 ids [i] = ifaces [i].Id;
750 var ifacemap = vm.conn.Type_GetInterfaceMap (id, ids);
752 var imap = new Dictionary<TypeMirror, InterfaceMappingMirror> ();
753 for (int i = 0; i < ifacemap.Length; ++i) {
754 IfaceMapInfo info = ifacemap [i];
756 MethodMirror[] imethods = new MethodMirror [info.iface_methods.Length];
757 for (int j = 0; j < info.iface_methods.Length; ++j)
758 imethods [j] = vm.GetMethod (info.iface_methods [j]);
760 MethodMirror[] tmethods = new MethodMirror [info.iface_methods.Length];
761 for (int j = 0; j < info.target_methods.Length; ++j)
762 tmethods [j] = vm.GetMethod (info.target_methods [j]);
764 InterfaceMappingMirror map = new InterfaceMappingMirror (vm, this, vm.GetType (info.iface_id), imethods, tmethods);
766 imap [map.InterfaceType] = map;
772 InterfaceMappingMirror res;
773 if (!iface_map.TryGetValue (interfaceType, out res))
774 throw new ArgumentException ("Interface not found", "interfaceType");