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 ParameterInfoMirror[] param_info;
18 ParameterInfoMirror ret_param;
19 LocalVariable[] locals;
20 IList<Location> locations;
21 MethodBodyMirror body;
23 TypeMirror[] type_args;
25 internal MethodMirror (VirtualMachine vm, long id) : base (vm, id) {
31 name = vm.conn.Method_GetName (id);
36 public TypeMirror DeclaringType {
38 if (declaring_type == null)
39 declaring_type = vm.GetType (vm.conn.Method_GetDeclaringType (id));
40 return declaring_type;
44 public TypeMirror ReturnType {
46 return ReturnParameter.ParameterType;
51 public string FullName {
53 string type_namespace = DeclaringType.Namespace;
54 string type_name = DeclaringType.Name;
55 StringBuilder sb = new StringBuilder ();
56 sb.Append (ReturnType.Name);
58 if (type_namespace != String.Empty)
59 sb.Append (type_namespace + ".");
65 for (var i = 0; i < param_info.Length; i++) {
66 sb.Append(param_info[i].ParameterType.Name);
67 if (i != param_info.Length - 1)
71 return sb.ToString ();
75 MethodInfo GetInfo () {
77 info = vm.conn.Method_GetInfo (id);
82 public int MetadataToken {
84 return GetInfo ().token;
88 public MethodAttributes Attributes {
90 return (MethodAttributes) GetInfo ().attributes;
94 public bool IsPublic {
96 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
99 public bool IsPrivate {
101 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
104 public bool IsFamily {
106 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family;
109 public bool IsAssembly {
111 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly;
114 public bool IsFamilyAndAssembly {
116 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem;
119 public bool IsFamilyOrAssembly {
121 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem;
124 public bool IsStatic {
126 return (Attributes & MethodAttributes.Static) != 0;
129 public bool IsFinal {
131 return (Attributes & MethodAttributes.Final) != 0;
134 public bool IsVirtual {
136 return (Attributes & MethodAttributes.Virtual) != 0;
139 public bool IsHideBySig {
141 return (Attributes & MethodAttributes.HideBySig) != 0;
144 public bool IsAbstract {
146 return (Attributes & MethodAttributes.Abstract) != 0;
149 public bool IsSpecialName {
151 return (Attributes & MethodAttributes.SpecialName) != 0;
155 public bool IsConstructor {
157 int attr = (int)Attributes;
158 return ((attr & (int)MethodAttributes.RTSpecialName) != 0
159 && (Name == ".ctor"));
163 // Since protocol version 2.12
164 public bool IsGenericMethodDefinition {
166 vm.CheckProtocolVersion (2, 12);
167 return GetInfo ().is_gmd;
171 public bool IsGenericMethod {
173 if (vm.Version.AtLeast (2, 12)) {
174 return GetInfo ().is_generic_method;
176 return Name.IndexOf ('`') != -1;
181 public ParameterInfoMirror[] GetParameters () {
182 if (param_info == null) {
183 var pi = vm.conn.Method_GetParamInfo (id);
184 param_info = new ParameterInfoMirror [pi.param_count];
186 ret_param = new ParameterInfoMirror (this, -1, vm.GetType (pi.ret_type), null, ParameterAttributes.Retval);
189 for (int i = 0; i < pi.param_count; ++i) {
190 param_info [i] = new ParameterInfoMirror (this, i, vm.GetType (pi.param_types [i]), pi.param_names [i], 0);
197 public ParameterInfoMirror ReturnParameter {
199 if (ret_param == null)
205 public LocalVariable[] GetLocals () {
206 if (locals == null) {
207 var li = vm.conn.Method_GetLocalsInfo (id);
208 // Add the arguments as well
209 var pi = vm.conn.Method_GetParamInfo (id);
211 locals = new LocalVariable [pi.param_count + li.names.Length];
213 for (int i = 0; i < pi.param_count; ++i)
214 locals [i] = new LocalVariable (vm, this, i, pi.param_types [i], pi.param_names [i], -1, -1, true);
216 for (int i = 0; i < li.names.Length; ++i)
217 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);
222 public LocalVariable GetLocal (string name) {
224 throw new ArgumentNullException ("name");
228 LocalVariable res = null;
229 for (int i = 0; i < locals.Length; ++i) {
230 if (locals [i].Name == name) {
232 throw new AmbiguousMatchException ("More that one local has the name '" + name + "'.");
240 public MethodBodyMirror GetMethodBody () {
242 MethodBodyInfo info = vm.conn.Method_GetBody (id);
244 body = new MethodBodyMirror (vm, this, info.il);
249 public MethodMirror GetGenericMethodDefinition () {
250 vm.CheckProtocolVersion (2, 12);
253 throw new InvalidOperationException ();
254 gmd = vm.GetMethod (info.gmd);
259 // Since protocol version 2.15
260 public TypeMirror[] GetGenericArguments () {
261 vm.CheckProtocolVersion (2, 15);
262 if (type_args == null)
263 type_args = vm.GetTypes (GetInfo ().type_args);
267 public IList<int> ILOffsets {
269 if (debug_info == null)
270 debug_info = vm.conn.Method_GetDebugInfo (id);
271 return Array.AsReadOnly (debug_info.il_offsets);
275 public IList<int> LineNumbers {
277 if (debug_info == null)
278 debug_info = vm.conn.Method_GetDebugInfo (id);
279 return Array.AsReadOnly (debug_info.line_numbers);
283 public string SourceFile {
285 if (debug_info == null)
286 debug_info = vm.conn.Method_GetDebugInfo (id);
287 return debug_info.source_files.Length > 0 ? debug_info.source_files [0].source_file : null;
291 public IList<Location> Locations {
293 if (locations == null) {
294 var il_offsets = ILOffsets;
295 var line_numbers = LineNumbers;
296 IList<Location> res = new Location [ILOffsets.Count];
297 for (int i = 0; i < il_offsets.Count; ++i)
298 res [i] = new Location (vm, this, -1, il_offsets [i], debug_info.source_files [i].source_file, line_numbers [i], 0, debug_info.source_files [i].hash);
305 internal int il_offset_to_line_number (int il_offset, out string src_file, out byte[] src_hash) {
306 if (debug_info == null)
307 debug_info = vm.conn.Method_GetDebugInfo (id);
309 // FIXME: Optimize this
312 for (int i = debug_info.il_offsets.Length - 1; i >= 0; --i) {
313 if (debug_info.il_offsets [i] <= il_offset) {
314 src_file = debug_info.source_files [i].source_file;
315 src_hash = debug_info.source_files [i].hash;
316 return debug_info.line_numbers [i];
322 public Location LocationAtILOffset (int il_offset) {
323 IList<Location> locs = Locations;
325 // FIXME: Optimize this
326 for (int i = locs.Count - 1; i >= 0; --i) {
327 if (locs [i].ILOffset <= il_offset)
334 public C.MethodDefinition Metadata {
337 meta = (C.MethodDefinition)DeclaringType.Assembly.Metadata.MainModule.LookupToken (MetadataToken);