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;
25 CustomAttributeDataMirror[] cattrs;
27 internal const BindingFlags DefaultBindingFlags =
28 BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
30 internal TypeMirror (VirtualMachine vm, long id) : base (vm, id) {
35 return GetInfo ().name;
39 public string Namespace {
45 public AssemblyMirror Assembly {
48 ass = vm.GetAssembly (GetInfo ().assembly);
54 public ModuleMirror Module {
57 module = vm.GetModule (GetInfo ().module);
63 public int MetadataToken {
65 return GetInfo ().token;
69 public TypeAttributes Attributes {
71 return (TypeAttributes)GetInfo ().attributes;
75 public TypeMirror BaseType {
77 // FIXME: base_type could be null for object/interfaces
78 if (base_type == null) {
79 base_type = vm.GetType (GetInfo ().base_type);
85 public int GetArrayRank () {
88 throw new ArgumentException ("Type must be an array type.");
93 public bool IsAbstract {
95 return (Attributes & TypeAttributes.Abstract) != 0;
99 public bool IsAnsiClass {
101 return (Attributes & TypeAttributes.StringFormatMask)
102 == TypeAttributes.AnsiClass;
106 public bool IsArray {
108 return IsArrayImpl ();
112 public bool IsAutoClass {
114 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.AutoClass;
118 public bool IsAutoLayout {
120 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.AutoLayout;
124 public bool IsByRef {
126 return IsByRefImpl ();
130 public bool IsClass {
139 public bool IsCOMObject {
141 return IsCOMObjectImpl ();
145 public bool IsContextful {
147 return IsContextfulImpl ();
153 return GetInfo ().is_enum;
157 public bool IsExplicitLayout {
159 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.ExplicitLayout;
163 public bool IsImport {
165 return (Attributes & TypeAttributes.Import) != 0;
169 public bool IsInterface {
171 return (Attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface;
175 public bool IsLayoutSequential {
177 return (Attributes & TypeAttributes.LayoutMask) == TypeAttributes.SequentialLayout;
181 public bool IsMarshalByRef {
183 return IsMarshalByRefImpl ();
187 public bool IsNestedAssembly {
189 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedAssembly;
193 public bool IsNestedFamANDAssem {
195 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamANDAssem;
199 public bool IsNestedFamily {
201 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamily;
205 public bool IsNestedFamORAssem {
207 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedFamORAssem;
211 public bool IsNestedPrivate {
213 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPrivate;
217 public bool IsNestedPublic {
219 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic;
223 public bool IsNotPublic {
225 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.NotPublic;
229 public bool IsPointer {
231 return IsPointerImpl ();
235 public bool IsPrimitive {
237 return IsPrimitiveImpl ();
241 public bool IsPublic {
243 return (Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public;
247 public bool IsSealed {
249 return (Attributes & TypeAttributes.Sealed) != 0;
253 public bool IsSerializable {
255 if ((Attributes & TypeAttributes.Serializable) != 0)
263 public bool IsSpecialName {
265 return (Attributes & TypeAttributes.SpecialName) != 0;
269 public bool IsUnicodeClass {
271 return (Attributes & TypeAttributes.StringFormatMask) == TypeAttributes.UnicodeClass;
275 public bool IsValueType {
277 return IsValueTypeImpl ();
281 public bool HasElementType {
283 return HasElementTypeImpl ();
287 public TypeMirror GetElementType () {
289 if (element_type == null && info.element_type != 0)
290 element_type = vm.GetType (info.element_type);
294 public string FullName {
296 return GetInfo ().full_name;
300 public string CSharpName {
303 if (GetArrayRank () == 1)
304 return GetElementType ().CSharpName + "[]";
307 for (int i = 0; i < GetArrayRank (); ++i)
309 return GetElementType ().CSharpName + "[" + ranks + "]";
324 // FIXME: Only do this for real corlib types
325 if (Namespace == "System") {
339 public MethodMirror[] GetMethods () {
340 if (methods == null) {
341 long[] ids = vm.conn.Type_GetMethods (id);
342 MethodMirror[] m = new MethodMirror [ids.Length];
343 for (int i = 0; i < ids.Length; ++i) {
344 m [i] = vm.GetMethod (ids [i]);
351 // FIXME: Sync this with Type
352 public MethodMirror GetMethod (string name) {
353 foreach (var m in GetMethods ())
359 public FieldInfoMirror[] GetFields () {
366 long[] ids = vm.conn.Type_GetFields (id, out names, out types, out attrs);
368 FieldInfoMirror[] res = new FieldInfoMirror [ids.Length];
369 for (int i = 0; i < res.Length; ++i)
370 res [i] = new FieldInfoMirror (this, ids [i], names [i], vm.GetType (types [i]), (FieldAttributes)attrs [i]);
376 public FieldInfoMirror GetField (string name) {
378 throw new ArgumentNullException ("name");
379 foreach (var f in GetFields ())
385 public TypeMirror[] GetNestedTypes ()
387 return GetNestedTypes (DefaultBindingFlags);
390 public TypeMirror[] GetNestedTypes (BindingFlags bindingAttr) {
394 // FIXME: bindingAttr
396 var arr = new TypeMirror [info.nested.Length];
397 for (int i = 0; i < arr.Length; ++i)
398 arr [i] = vm.GetType (info.nested [i]);
404 public PropertyInfoMirror[] GetProperties () {
405 return GetProperties (DefaultBindingFlags);
408 public PropertyInfoMirror[] GetProperties (BindingFlags bindingAttr) {
409 if (properties != null)
412 PropInfo[] info = vm.conn.Type_GetProperties (id);
414 PropertyInfoMirror[] res = new PropertyInfoMirror [info.Length];
415 for (int i = 0; i < res.Length; ++i)
416 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);
422 public PropertyInfoMirror GetProperty (string name) {
424 throw new ArgumentNullException ("name");
425 foreach (var p in GetProperties ())
431 public virtual bool IsAssignableFrom (TypeMirror c) {
433 throw new ArgumentNullException ("c");
437 // This is complex so do it in the debuggee
438 return vm.conn.Type_IsAssignableFrom (id, c.Id);
441 public Value GetValue (FieldInfoMirror field) {
442 return GetValues (new FieldInfoMirror [] { field }) [0];
445 public Value[] GetValues (IList<FieldInfoMirror> fields) {
447 throw new ArgumentNullException ("fields");
448 foreach (FieldInfoMirror f in fields) {
450 throw new ArgumentNullException ("field");
453 long[] ids = new long [fields.Count];
454 for (int i = 0; i < fields.Count; ++i)
455 ids [i] = fields [i].Id;
457 return vm.DecodeValues (vm.conn.Type_GetValues (id, ids));
458 } catch (CommandException ex) {
459 if (ex.ErrorCode == ErrorCode.INVALID_FIELDID)
460 throw new ArgumentException ("One of the fields is not valid for this type.", "fields");
466 public void SetValues (IList<FieldInfoMirror> fields, Value[] values) {
468 throw new ArgumentNullException ("fields");
470 throw new ArgumentNullException ("values");
471 foreach (FieldInfoMirror f in fields) {
473 throw new ArgumentNullException ("field");
476 foreach (Value v in values) {
478 throw new ArgumentNullException ("values");
481 long[] ids = new long [fields.Count];
482 for (int i = 0; i < fields.Count; ++i)
483 ids [i] = fields [i].Id;
485 vm.conn.Type_SetValues (id, ids, vm.EncodeValues (values));
486 } catch (CommandException ex) {
487 if (ex.ErrorCode == ErrorCode.INVALID_FIELDID)
488 throw new ArgumentException ("One of the fields is not valid for this type.", "fields");
494 public void SetValue (FieldInfoMirror field, Value value) {
495 SetValues (new FieldInfoMirror [] { field }, new Value [] { value });
498 public ObjectMirror GetTypeObject () {
499 return vm.GetObject (vm.conn.Type_GetObject (id));
503 * Return a list of source files without path info, where methods of
504 * this type are defined. Return an empty list if the information is not
506 * This can be used by a debugger to find out which types occur in a
507 * given source file, to filter the list of methods whose locations
508 * have to be checked when placing breakpoints.
510 public string[] GetSourceFiles () {
511 return GetSourceFiles (false);
514 public string[] GetSourceFiles (bool return_full_paths) {
515 return vm.conn.Type_GetSourceFiles (id, return_full_paths);
518 public C.TypeDefinition Metadata {
521 if (Assembly.Metadata == null || MetadataToken == 0)
523 meta = (C.TypeDefinition)Assembly.Metadata.MainModule.LookupToken (MetadataToken);
529 TypeInfo GetInfo () {
531 info = vm.conn.Type_GetInfo (id);
535 protected virtual TypeAttributes GetAttributeFlagsImpl () {
536 return (TypeAttributes)GetInfo ().attributes;
539 protected virtual bool HasElementTypeImpl () {
540 return IsArray || IsByRef || IsPointer;
543 protected virtual bool IsArrayImpl () {
544 return GetInfo ().rank > 0;
547 protected virtual bool IsByRefImpl () {
548 return GetInfo ().is_byref;
551 protected virtual bool IsCOMObjectImpl () {
555 protected virtual bool IsPointerImpl () {
556 return GetInfo ().is_pointer;
559 protected virtual bool IsPrimitiveImpl () {
560 return GetInfo ().is_primitive;
563 protected virtual bool IsValueTypeImpl ()
565 return GetInfo ().is_valuetype;
568 protected virtual bool IsContextfulImpl ()
574 protected virtual bool IsMarshalByRefImpl ()
580 // Same as Enum.GetUnderlyingType ()
581 public TypeMirror EnumUnderlyingType {
584 throw new ArgumentException ("Type is not an enum type.");
585 foreach (FieldInfoMirror f in GetFields ()) {
589 throw new NotImplementedException ();
594 * Creating the custom attributes themselves could modify the behavior of the
595 * debuggee, so we return objects similar to the CustomAttributeData objects
596 * used by the reflection-only functionality on .net.
598 public CustomAttributeDataMirror[] GetCustomAttributes (bool inherit) {
599 return GetCAttrs (null, inherit);
602 public CustomAttributeDataMirror[] GetCustomAttributes (TypeMirror attributeType, bool inherit) {
603 if (attributeType == null)
604 throw new ArgumentNullException ("attributeType");
605 return GetCAttrs (attributeType, inherit);
608 CustomAttributeDataMirror[] GetCAttrs (TypeMirror type, bool inherit) {
609 // FIXME: Handle inherit
610 if (cattrs == null) {
611 CattrInfo[] info = vm.conn.Type_GetCustomAttributes (id, 0, false);
612 cattrs = CustomAttributeDataMirror.Create (vm, info);
614 var res = new List<CustomAttributeDataMirror> ();
615 foreach (var attr in cattrs)
616 if (type == null || attr.Constructor.DeclaringType == type)
618 return res.ToArray ();
621 public Value InvokeMethod (ThreadMirror thread, MethodMirror method, IList<Value> arguments) {
622 return ObjectMirror.InvokeMethod (vm, thread, method, null, arguments, InvokeOptions.None);
625 public Value InvokeMethod (ThreadMirror thread, MethodMirror method, IList<Value> arguments, InvokeOptions options) {
626 return ObjectMirror.InvokeMethod (vm, thread, method, null, arguments, options);
629 [Obsolete ("Use the overload without the 'vm' argument")]
630 public IAsyncResult BeginInvokeMethod (VirtualMachine vm, ThreadMirror thread, MethodMirror method, IList<Value> arguments, InvokeOptions options, AsyncCallback callback, object state) {
631 return ObjectMirror.BeginInvokeMethod (vm, thread, method, null, arguments, options, callback, state);
634 public IAsyncResult BeginInvokeMethod (ThreadMirror thread, MethodMirror method, IList<Value> arguments, InvokeOptions options, AsyncCallback callback, object state) {
635 return ObjectMirror.BeginInvokeMethod (vm, thread, method, null, arguments, options, callback, state);
638 public Value EndInvokeMethod (IAsyncResult asyncResult) {
639 return ObjectMirror.EndInvokeMethodInternal (asyncResult);
642 public Value NewInstance (ThreadMirror thread, MethodMirror method, IList<Value> arguments) {
643 return ObjectMirror.InvokeMethod (vm, thread, method, null, arguments, InvokeOptions.None);
646 public Value NewInstance (ThreadMirror thread, MethodMirror method, IList<Value> arguments, InvokeOptions options) {
647 return ObjectMirror.InvokeMethod (vm, thread, method, null, arguments, options);