2 using System.Collections.Generic;
5 using System.Reflection;
7 using Mono.Cecil.Metadata;
9 namespace Mono.Debugger.Soft
11 public class MethodMirror : Mirror
15 TypeMirror declaring_type;
17 C.MethodDefinition meta;
18 CustomAttributeDataMirror[] cattrs;
19 ParameterInfoMirror[] param_info;
20 ParameterInfoMirror ret_param;
21 LocalVariable[] locals;
22 IList<Location> locations;
23 MethodBodyMirror body;
25 TypeMirror[] type_args;
27 internal MethodMirror (VirtualMachine vm, long id) : base (vm, id) {
33 name = vm.conn.Method_GetName (id);
38 public TypeMirror DeclaringType {
40 if (declaring_type == null)
41 declaring_type = vm.GetType (vm.conn.Method_GetDeclaringType (id));
42 return declaring_type;
46 public TypeMirror ReturnType {
48 return ReturnParameter.ParameterType;
53 public string FullName {
55 string type_namespace = DeclaringType.Namespace;
56 string type_name = DeclaringType.Name;
57 StringBuilder sb = new StringBuilder ();
58 sb.Append (ReturnType.Name);
60 if (type_namespace != String.Empty)
61 sb.Append (type_namespace + ".");
67 for (var i = 0; i < param_info.Length; i++) {
68 sb.Append(param_info[i].ParameterType.Name);
69 if (i != param_info.Length - 1)
73 return sb.ToString ();
78 * Creating the custom attributes themselves could modify the behavior of the
79 * debuggee, so we return objects similar to the CustomAttributeData objects
80 * used by the reflection-only functionality on .net.
81 * Since protocol version 2.21
83 public CustomAttributeDataMirror[] GetCustomAttributes (bool inherit) {
84 return GetCAttrs (null, inherit);
87 /* Since protocol version 2.21 */
88 public CustomAttributeDataMirror[] GetCustomAttributes (TypeMirror attributeType, bool inherit) {
89 if (attributeType == null)
90 throw new ArgumentNullException ("attributeType");
91 return GetCAttrs (attributeType, inherit);
94 CustomAttributeDataMirror[] GetCAttrs (TypeMirror type, bool inherit) {
95 if (cattrs == null && meta != null && !Metadata.HasCustomAttributes)
96 cattrs = new CustomAttributeDataMirror [0];
98 // FIXME: Handle inherit
100 CattrInfo[] info = vm.conn.Method_GetCustomAttributes (id, 0, false);
101 cattrs = CustomAttributeDataMirror.Create (vm, info);
103 var res = new List<CustomAttributeDataMirror> ();
104 foreach (var attr in cattrs)
105 if (type == null || attr.Constructor.DeclaringType == type)
107 return res.ToArray ();
110 MethodInfo GetInfo () {
112 info = vm.conn.Method_GetInfo (id);
117 public int MetadataToken {
119 return GetInfo ().token;
123 public MethodAttributes Attributes {
125 return (MethodAttributes) GetInfo ().attributes;
129 public bool IsPublic {
131 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
134 public bool IsPrivate {
136 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
139 public bool IsFamily {
141 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family;
144 public bool IsAssembly {
146 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly;
149 public bool IsFamilyAndAssembly {
151 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem;
154 public bool IsFamilyOrAssembly {
156 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem;
159 public bool IsStatic {
161 return (Attributes & MethodAttributes.Static) != 0;
164 public bool IsFinal {
166 return (Attributes & MethodAttributes.Final) != 0;
169 public bool IsVirtual {
171 return (Attributes & MethodAttributes.Virtual) != 0;
174 public bool IsHideBySig {
176 return (Attributes & MethodAttributes.HideBySig) != 0;
179 public bool IsAbstract {
181 return (Attributes & MethodAttributes.Abstract) != 0;
184 public bool IsSpecialName {
186 return (Attributes & MethodAttributes.SpecialName) != 0;
190 public bool IsConstructor {
192 int attr = (int)Attributes;
193 return ((attr & (int)MethodAttributes.RTSpecialName) != 0
194 && (Name == ".ctor"));
198 // Since protocol version 2.12
199 public bool IsGenericMethodDefinition {
201 vm.CheckProtocolVersion (2, 12);
202 return GetInfo ().is_gmd;
206 // Since protocol version 2.12
207 public bool IsGenericMethod {
209 vm.CheckProtocolVersion (2, 12);
210 return GetInfo ().is_generic_method;
214 public MethodImplAttributes GetMethodImplementationFlags() {
215 return (MethodImplAttributes)GetInfo ().iattributes;
218 public ParameterInfoMirror[] GetParameters () {
219 if (param_info == null) {
220 var pi = vm.conn.Method_GetParamInfo (id);
221 param_info = new ParameterInfoMirror [pi.param_count];
223 ret_param = new ParameterInfoMirror (this, -1, vm.GetType (pi.ret_type), null, ParameterAttributes.Retval);
226 for (int i = 0; i < pi.param_count; ++i) {
227 param_info [i] = new ParameterInfoMirror (this, i, vm.GetType (pi.param_types [i]), pi.param_names [i], 0);
234 public ParameterInfoMirror ReturnParameter {
236 if (ret_param == null)
242 public LocalVariable[] GetLocals () {
243 if (locals == null) {
244 LocalsInfo li = new LocalsInfo ();
246 li = vm.conn.Method_GetLocalsInfo (id);
247 } catch (CommandException) {
248 throw new AbsentInformationException ();
251 // Add the arguments as well
252 var pi = GetParameters ();
254 locals = new LocalVariable [pi.Length + li.names.Length];
256 for (int i = 0; i < pi.Length; ++i)
257 locals [i] = new LocalVariable (vm, this, i, pi[i].ParameterType.Id, pi[i].Name, -1, -1, true);
259 for (int i = 0; i < li.names.Length; ++i)
260 locals [i + pi.Length] = new LocalVariable (vm, this, i, li.types [i], li.names [i], li.live_range_start [i], li.live_range_end [i], false);
265 public LocalVariable GetLocal (string name) {
267 throw new ArgumentNullException ("name");
271 LocalVariable res = null;
272 for (int i = 0; i < locals.Length; ++i) {
273 if (locals [i].Name == name) {
275 throw new AmbiguousMatchException ("More that one local has the name '" + name + "'.");
283 public MethodBodyMirror GetMethodBody () {
285 MethodBodyInfo info = vm.conn.Method_GetBody (id);
287 body = new MethodBodyMirror (vm, this, info);
292 public MethodMirror GetGenericMethodDefinition () {
293 vm.CheckProtocolVersion (2, 12);
296 throw new InvalidOperationException ();
297 gmd = vm.GetMethod (info.gmd);
302 // Since protocol version 2.15
303 public TypeMirror[] GetGenericArguments () {
304 vm.CheckProtocolVersion (2, 15);
305 if (type_args == null)
306 type_args = vm.GetTypes (GetInfo ().type_args);
310 // Since protocol version 2.24
311 public MethodMirror MakeGenericMethod (TypeMirror[] args) {
313 throw new ArgumentNullException ("args");
314 foreach (var a in args)
316 throw new ArgumentNullException ("args");
318 if (!IsGenericMethodDefinition)
319 throw new InvalidOperationException ("not a generic method definition");
321 if (GetGenericArguments ().Length != args.Length)
322 throw new ArgumentException ("Incorrect length");
324 vm.CheckProtocolVersion (2, 24);
327 id = vm.conn.Method_MakeGenericMethod (Id, args.Select (t => t.Id).ToArray ());
328 } catch (CommandException) {
329 throw new InvalidOperationException ();
331 return vm.GetMethod (id);
334 public IList<int> ILOffsets {
336 if (debug_info == null)
337 debug_info = vm.conn.Method_GetDebugInfo (id);
338 return Array.AsReadOnly (debug_info.il_offsets);
342 public IList<int> LineNumbers {
344 if (debug_info == null)
345 debug_info = vm.conn.Method_GetDebugInfo (id);
346 return Array.AsReadOnly (debug_info.line_numbers);
350 public string SourceFile {
352 if (debug_info == null)
353 debug_info = vm.conn.Method_GetDebugInfo (id);
354 return debug_info.source_files.Length > 0 ? debug_info.source_files [0].source_file : null;
358 public IList<Location> Locations {
360 if (locations == null) {
361 var il_offsets = ILOffsets;
362 var line_numbers = LineNumbers;
363 IList<Location> res = new Location [ILOffsets.Count];
364 for (int i = 0; i < il_offsets.Count; ++i)
365 res [i] = new Location (vm, this, -1, il_offsets [i], debug_info.source_files [i].source_file, line_numbers [i], debug_info.column_numbers [i], debug_info.end_line_numbers [i], debug_info.end_column_numbers [i], debug_info.source_files [i].hash);
372 internal int il_offset_to_line_number (int il_offset, out string src_file, out byte[] src_hash, out int column_number, out int end_line_number, out int end_column_number) {
373 if (debug_info == null)
374 debug_info = vm.conn.Method_GetDebugInfo (id);
376 // FIXME: Optimize this
380 end_line_number = -1;
381 end_column_number = -1;
382 for (int i = debug_info.il_offsets.Length - 1; i >= 0; --i) {
383 if (debug_info.il_offsets [i] <= il_offset) {
384 src_file = debug_info.source_files [i].source_file;
385 src_hash = debug_info.source_files [i].hash;
386 column_number = debug_info.column_numbers [i];
387 end_line_number = debug_info.end_line_numbers [i];
388 end_column_number = debug_info.end_column_numbers [i];
389 return debug_info.line_numbers [i];
395 public Location LocationAtILOffset (int il_offset) {
396 IList<Location> locs = Locations;
398 // FIXME: Optimize this
399 for (int i = locs.Count - 1; i >= 0; --i) {
400 if (locs [i].ILOffset <= il_offset)
407 public C.MethodDefinition Metadata {
410 meta = (C.MethodDefinition)DeclaringType.Assembly.Metadata.MainModule.LookupToken (MetadataToken);
416 // Evaluate the method on the client using an IL interpreter.
417 // Only supports a subset of IL instructions. Doesn't change
419 // Returns the result of the evaluation, or null for methods
420 // which return void.
421 // Throws a NotSupportedException if the method body contains
422 // unsupported IL instructions, or if evaluating the method
423 // would change debuggee state.
425 public Value Evaluate (Value this_val, Value[] args) {
426 var interp = new ILInterpreter (this);
428 return interp.Evaluate (this_val, args);