2 using System.Collections.Generic;
4 using System.Reflection;
6 using Mono.Cecil.Metadata;
8 namespace Mono.Debugger.Soft
10 public class MethodMirror : Mirror
14 TypeMirror declaring_type;
16 C.MethodDefinition meta;
17 CustomAttributeDataMirror[] cattrs;
18 ParameterInfoMirror[] param_info;
19 ParameterInfoMirror ret_param;
20 LocalVariable[] locals;
21 IList<Location> locations;
22 MethodBodyMirror body;
24 TypeMirror[] type_args;
26 internal MethodMirror (VirtualMachine vm, long id) : base (vm, id) {
32 name = vm.conn.Method_GetName (id);
37 public TypeMirror DeclaringType {
39 if (declaring_type == null)
40 declaring_type = vm.GetType (vm.conn.Method_GetDeclaringType (id));
41 return declaring_type;
45 public TypeMirror ReturnType {
47 return ReturnParameter.ParameterType;
52 public string FullName {
54 string type_namespace = DeclaringType.Namespace;
55 string type_name = DeclaringType.Name;
56 StringBuilder sb = new StringBuilder ();
57 sb.Append (ReturnType.Name);
59 if (type_namespace != String.Empty)
60 sb.Append (type_namespace + ".");
66 for (var i = 0; i < param_info.Length; i++) {
67 sb.Append(param_info[i].ParameterType.Name);
68 if (i != param_info.Length - 1)
72 return sb.ToString ();
77 * Creating the custom attributes themselves could modify the behavior of the
78 * debuggee, so we return objects similar to the CustomAttributeData objects
79 * used by the reflection-only functionality on .net.
81 public CustomAttributeDataMirror[] GetCustomAttributes (bool inherit) {
82 return GetCAttrs (null, inherit);
85 public CustomAttributeDataMirror[] GetCustomAttributes (TypeMirror attributeType, bool inherit) {
86 if (attributeType == null)
87 throw new ArgumentNullException ("attributeType");
88 return GetCAttrs (attributeType, inherit);
91 CustomAttributeDataMirror[] GetCAttrs (TypeMirror type, bool inherit) {
92 if (cattrs == null && Metadata != null && !Metadata.HasCustomAttributes)
93 cattrs = new CustomAttributeDataMirror [0];
95 // FIXME: Handle inherit
97 CattrInfo[] info = vm.conn.Type_GetCustomAttributes (id, 0, false);
98 cattrs = CustomAttributeDataMirror.Create (vm, info);
100 var res = new List<CustomAttributeDataMirror> ();
101 foreach (var attr in cattrs)
102 if (type == null || attr.Constructor.DeclaringType == type)
104 return res.ToArray ();
107 MethodInfo GetInfo () {
109 info = vm.conn.Method_GetInfo (id);
114 public int MetadataToken {
116 return GetInfo ().token;
120 public MethodAttributes Attributes {
122 return (MethodAttributes) GetInfo ().attributes;
126 public bool IsPublic {
128 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
131 public bool IsPrivate {
133 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
136 public bool IsFamily {
138 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family;
141 public bool IsAssembly {
143 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly;
146 public bool IsFamilyAndAssembly {
148 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem;
151 public bool IsFamilyOrAssembly {
153 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem;
156 public bool IsStatic {
158 return (Attributes & MethodAttributes.Static) != 0;
161 public bool IsFinal {
163 return (Attributes & MethodAttributes.Final) != 0;
166 public bool IsVirtual {
168 return (Attributes & MethodAttributes.Virtual) != 0;
171 public bool IsHideBySig {
173 return (Attributes & MethodAttributes.HideBySig) != 0;
176 public bool IsAbstract {
178 return (Attributes & MethodAttributes.Abstract) != 0;
181 public bool IsSpecialName {
183 return (Attributes & MethodAttributes.SpecialName) != 0;
187 public bool IsConstructor {
189 int attr = (int)Attributes;
190 return ((attr & (int)MethodAttributes.RTSpecialName) != 0
191 && (Name == ".ctor"));
195 // Since protocol version 2.12
196 public bool IsGenericMethodDefinition {
198 vm.CheckProtocolVersion (2, 12);
199 return GetInfo ().is_gmd;
203 // Since protocol version 2.12
204 public bool IsGenericMethod {
206 vm.CheckProtocolVersion (2, 12);
207 return GetInfo ().is_generic_method;
211 public MethodImplAttributes GetMethodImplementationFlags() {
212 return (MethodImplAttributes)GetInfo ().iattributes;
215 public ParameterInfoMirror[] GetParameters () {
216 if (param_info == null) {
217 var pi = vm.conn.Method_GetParamInfo (id);
218 param_info = new ParameterInfoMirror [pi.param_count];
220 ret_param = new ParameterInfoMirror (this, -1, vm.GetType (pi.ret_type), null, ParameterAttributes.Retval);
223 for (int i = 0; i < pi.param_count; ++i) {
224 param_info [i] = new ParameterInfoMirror (this, i, vm.GetType (pi.param_types [i]), pi.param_names [i], 0);
231 public ParameterInfoMirror ReturnParameter {
233 if (ret_param == null)
239 public LocalVariable[] GetLocals () {
240 if (locals == null) {
242 LocalsInfo li = new LocalsInfo ();
244 li = vm.conn.Method_GetLocalsInfo (id);
245 } catch (CommandException) {
246 throw new ArgumentException ("Method doesn't have a body.");
248 // Add the arguments as well
249 var pi = vm.conn.Method_GetParamInfo (id);
251 locals = new LocalVariable [pi.param_count + li.names.Length];
253 for (int i = 0; i < pi.param_count; ++i)
254 locals [i] = new LocalVariable (vm, this, i, pi.param_types [i], pi.param_names [i], -1, -1, true);
256 for (int i = 0; i < li.names.Length; ++i)
257 locals [i + pi.param_count] = new LocalVariable (vm, this, i, li.types [i], li.names [i], li.live_range_start [i], li.live_range_end [i], false);
262 public LocalVariable GetLocal (string name) {
264 throw new ArgumentNullException ("name");
268 LocalVariable res = null;
269 for (int i = 0; i < locals.Length; ++i) {
270 if (locals [i].Name == name) {
272 throw new AmbiguousMatchException ("More that one local has the name '" + name + "'.");
280 public MethodBodyMirror GetMethodBody () {
282 MethodBodyInfo info = vm.conn.Method_GetBody (id);
284 body = new MethodBodyMirror (vm, this, info);
289 public MethodMirror GetGenericMethodDefinition () {
290 vm.CheckProtocolVersion (2, 12);
293 throw new InvalidOperationException ();
294 gmd = vm.GetMethod (info.gmd);
299 // Since protocol version 2.15
300 public TypeMirror[] GetGenericArguments () {
301 vm.CheckProtocolVersion (2, 15);
302 if (type_args == null)
303 type_args = vm.GetTypes (GetInfo ().type_args);
307 public IList<int> ILOffsets {
309 if (debug_info == null)
310 debug_info = vm.conn.Method_GetDebugInfo (id);
311 return Array.AsReadOnly (debug_info.il_offsets);
315 public IList<int> LineNumbers {
317 if (debug_info == null)
318 debug_info = vm.conn.Method_GetDebugInfo (id);
319 return Array.AsReadOnly (debug_info.line_numbers);
323 public string SourceFile {
325 if (debug_info == null)
326 debug_info = vm.conn.Method_GetDebugInfo (id);
327 return debug_info.source_files.Length > 0 ? debug_info.source_files [0].source_file : null;
331 public IList<Location> Locations {
333 if (locations == null) {
334 var il_offsets = ILOffsets;
335 var line_numbers = LineNumbers;
336 IList<Location> res = new Location [ILOffsets.Count];
337 for (int i = 0; i < il_offsets.Count; ++i)
338 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.source_files [i].hash);
345 internal int il_offset_to_line_number (int il_offset, out string src_file, out byte[] src_hash, out int column_number) {
346 if (debug_info == null)
347 debug_info = vm.conn.Method_GetDebugInfo (id);
349 // FIXME: Optimize this
353 for (int i = debug_info.il_offsets.Length - 1; i >= 0; --i) {
354 if (debug_info.il_offsets [i] <= il_offset) {
355 src_file = debug_info.source_files [i].source_file;
356 src_hash = debug_info.source_files [i].hash;
357 column_number = debug_info.column_numbers [i];
358 return debug_info.line_numbers [i];
364 public Location LocationAtILOffset (int il_offset) {
365 IList<Location> locs = Locations;
367 // FIXME: Optimize this
368 for (int i = locs.Count - 1; i >= 0; --i) {
369 if (locs [i].ILOffset <= il_offset)
376 public C.MethodDefinition Metadata {
379 meta = (C.MethodDefinition)DeclaringType.Assembly.Metadata.MainModule.LookupToken (MetadataToken);