// Returns the previous method, or null if none public MethodDef addMethod(MethodDef ifaceMethod, MethodDef classMethod) { if (!ifaceMethodToClassMethod.ContainsKey(ifaceMethod)) throw new ApplicationException("Could not find interface method"); MethodDef oldMethod; ifaceMethodToClassMethod.TryGetValue(ifaceMethod, out oldMethod); ifaceMethodToClassMethod[ifaceMethod] = classMethod; return oldMethod; }
public MMethodDef(MethodDef methodDef, MTypeDef owner, int index) : base(methodDef, owner, index) { genericParams = MGenericParamDef.CreateGenericParamDefList(MethodDef.GenericParameters); visibleBaseIndex = methodDef.MethodSig != null && methodDef.MethodSig.HasThis ? 1 : 0; for (int i = 0; i < methodDef.Parameters.Count; i++) { var param = methodDef.Parameters[i]; if (param.IsNormalMethodParameter) visibleParamCount++; paramDefs.Add(new MParamDef(param, i)); } returnParamDef = new MParamDef(methodDef.Parameters.ReturnParameter, -1); }
public void same(MethodDef a, MethodDef b) { merge(get(a), get(b)); }
public void add(MethodDef methodDef) { get(methodDef); }
public MethodInst(MethodDef origMethodDef, MethodReference methodReference) { this.origMethodDef = origMethodDef; this.methodReference = methodReference; }
// Returns the previous classMethod, or null if none public MethodDef addMethod(TypeInfo iface, MethodDef ifaceMethod, MethodDef classMethod) { return addMethod(iface.typeReference, ifaceMethod, classMethod); }
static PropertySig CreatePropertySig(MethodDef method, TypeSig propType, bool isGetter) { if (method == null) return null; var sig = method.MethodSig; if (sig == null) return null; var newSig = new PropertySig(sig.HasThis, propType); newSig.GenParamCount = sig.GenParamCount; int count = sig.Params.Count; if (!isGetter) count--; for (int i = 0; i < count; i++) newSig.Params.Add(sig.Params[i]); return newSig; }
public MethodNameGroup get(MethodDef method) { if (!method.isVirtual()) throw new ApplicationException("Not a virtual method"); MethodNameGroup group; if (!methodGroups.TryGetValue(method, out group)) { methodGroups[method] = group = new MethodNameGroup(); group.add(method); } return group; }
static bool isEventHandler(MethodDef methodDef) { if (methodDef.MethodDefinition.Parameters.Count != 2) return false; if (methodDef.MethodDefinition.MethodReturnType.ReturnType.FullName != "System.Void") return false; if (methodDef.MethodDefinition.Parameters[0].ParameterType.FullName != "System.Object") return false; if (!methodDef.MethodDefinition.Parameters[1].ParameterType.FullName.Contains("EventArgs")) return false; return true; }
public void renameMethod(MethodDef methodDef, string methodName) { if (!canRenameMethod(methodDef)) return; var methodInfo = method(methodDef); variableNameState.addMethodName(methodName); methodInfo.rename(methodName); }
public MethodInfo method(MethodDef method) { return memberInfos.method(method); }
PropertyDef getOverriddenProperty(MethodDef overrideMethod) { var theMethod = overrideMethod.MethodDefinition.Overrides[0]; var overriddenMethod = modules.resolve(theMethod); if (overriddenMethod != null) return overriddenMethod.Property; var extType = theMethod.DeclaringType; if (extType == null) return null; var extTypeDef = modules.resolveOther(extType); if (extTypeDef == null) return null; var theMethodDef = extTypeDef.find(theMethod); if (theMethodDef != null) return theMethodDef.Property; return null; }
MethodDef getOverriddenMethod(MethodDef overrideMethod) { return modules.resolve(overrideMethod.MethodDefinition.Overrides[0]); }
EventDef getOverriddenEvent(MethodDef overrideMethod, out MethodDef overriddenMethod) { var theMethod = overrideMethod.MethodDefinition.Overrides[0]; overriddenMethod = modules.resolve(theMethod); if (overriddenMethod != null) return overriddenMethod.Event; var extType = theMethod.DeclaringType; if (extType == null) return null; var extTypeDef = modules.resolveOther(extType); if (extTypeDef == null) return null; overriddenMethod = extTypeDef.find(theMethod); if (overriddenMethod != null) return overriddenMethod.Event; return null; }
bool canRenameMethod(MethodDef methodDef) { var methodInfo = method(methodDef); if (methodDef.isStatic()) { if (methodInfo.oldName == ".cctor") return false; } else if (methodDef.isVirtual()) { if (DotNetUtils.derivesFromDelegate(type.TypeDefinition)) { switch (methodInfo.oldName) { case "BeginInvoke": case "EndInvoke": case "Invoke": return false; } } } else { if (methodInfo.oldName == ".ctor") return false; } return true; }
static IMethod GetVbHandler(MethodDef method, out string eventName) { eventName = null; if (method.Body == null) return null; var sig = method.MethodSig; if (sig == null) return null; if (sig.RetType.ElementType != ElementType.Void) return null; if (sig.Params.Count != 1) return null; if (method.Body.Variables.Count != 1) return null; if (!IsEventHandlerType(method.Body.Variables[0].Type)) return null; var instructions = method.Body.Instructions; int index = 0; int newobjIndex = FindInstruction(instructions, index, Code.Newobj); if (newobjIndex == -1 || FindInstruction(instructions, newobjIndex + 1, Code.Newobj) != -1) return null; if (!IsEventHandlerCtor(instructions[newobjIndex].Operand as IMethod)) return null; if (newobjIndex < 1) return null; var ldvirtftn = instructions[newobjIndex - 1]; if (ldvirtftn.OpCode.Code != Code.Ldvirtftn && ldvirtftn.OpCode.Code != Code.Ldftn) return null; var handlerMethod = ldvirtftn.Operand as IMethod; if (handlerMethod == null) return null; if (!new SigComparer().Equals(method.DeclaringType, handlerMethod.DeclaringType)) return null; index = newobjIndex; IField addField, removeField; IMethod addMethod, removeMethod; if (!FindEventCall(instructions, ref index, out removeField, out removeMethod)) return null; if (!FindEventCall(instructions, ref index, out addField, out addMethod)) return null; if (FindInstruction(instructions, index, Code.Callvirt) != -1) return null; if (!new SigComparer().Equals(addField, removeField)) return null; if (!new SigComparer().Equals(method.DeclaringType, addField.DeclaringType)) return null; if (!new SigComparer().Equals(addMethod.DeclaringType, removeMethod.DeclaringType)) return null; if (!Utils.StartsWith(addMethod.Name.String, "add_", StringComparison.Ordinal)) return null; if (!Utils.StartsWith(removeMethod.Name.String, "remove_", StringComparison.Ordinal)) return null; eventName = addMethod.Name.String.Substring(4); if (eventName != removeMethod.Name.String.Substring(7)) return null; if (eventName == "") return null; return handlerMethod; }
void findInitializeComponentMethod(TypeDef type, MethodDef possibleInitMethod) { foreach (var methodDef in type.AllMethods) { if (methodDef.MethodDefinition.Name != ".ctor") continue; if (methodDef.MethodDefinition.Body == null) continue; foreach (var instr in methodDef.MethodDefinition.Body.Instructions) { if (instr.OpCode.Code != Code.Call && instr.OpCode.Code != Code.Callvirt) continue; if (!MemberReferenceHelper.compareMethodReferenceAndDeclaringType(possibleInitMethod.MethodDefinition, instr.Operand as MethodReference)) continue; memberInfos.method(possibleInitMethod).suggestedName = "InitializeComponent"; return; } } }
MPropertyDef CreateProperty(MTypeDef ownerType, string name, TypeSig propType, MethodDef getter, MethodDef setter) { if (string.IsNullOrEmpty(name) || propType.ElementType == ElementType.Void) return null; var newSig = CreatePropertySig(getter, propType, true) ?? CreatePropertySig(setter, propType, false); if (newSig == null) return null; var newProp = ownerType.Module.ModuleDefMD.UpdateRowId(new PropertyDefUser(name, newSig, 0)); newProp.GetMethod = getter; newProp.SetMethod = setter; var propDef = ownerType.FindAny(newProp); if (propDef != null) return propDef; propDef = ownerType.Create(newProp); memberInfos.Add(propDef); if (isVerbose) Logger.v("Restoring property: {0}", Utils.RemoveNewlines(newProp)); return propDef; }
string getPinvokeName(MethodDef methodDef) { var entryPoint = methodDef.MethodDefinition.PInvokeInfo.EntryPoint; if (Regex.IsMatch(entryPoint, @"^#\d+$")) entryPoint = DotNetUtils.getDllName(methodDef.MethodDefinition.PInvokeInfo.Module.Name) + "_" + entryPoint.Substring(1); return entryPoint; }
public void RemoveMethodDef(MethodDef md) { if (!MethodDefs.Remove(md)) throw new ApplicationException(string.Format("Could not remove MethodDef: {0}", md)); }
void prepareRenameMethodArgs(MethodDef methodDef) { if (methodDef.ParamDefs.Count > 0) { if (isEventHandler(methodDef)) { ParamInfo info; info = param(methodDef.ParamDefs[0]); if (!info.gotNewName()) info.newName = "sender"; info = param(methodDef.ParamDefs[1]); if (!info.gotNewName()) info.newName = "e"; } else { var newVariableNameState = variableNameState.cloneParamsOnly(); var checker = NameChecker; foreach (var paramDef in methodDef.ParamDefs) { var info = param(paramDef); if (info.gotNewName()) continue; if (!checker.isValidMethodArgName(info.oldName)) info.newName = newVariableNameState.getNewParamName(info.oldName, paramDef.ParameterDefinition); } } } if ((methodDef.Property != null && methodDef == methodDef.Property.SetMethod) || (methodDef.Event != null && (methodDef == methodDef.Event.AddMethod || methodDef == methodDef.Event.RemoveMethod))) { if (methodDef.ParamDefs.Count > 0) { var paramDef = methodDef.ParamDefs[methodDef.ParamDefs.Count - 1]; param(paramDef).newName = "value"; } } }
public void addMethodIfEmpty(TypeInfo iface, MethodDef ifaceMethod, MethodDef classMethod) { InterfaceMethodInfo info; var key = new TypeReferenceKey(iface.typeReference); if (!interfaceMethods.TryGetValue(key, out info)) throw new ApplicationException("Could not find interface"); info.addMethodIfEmpty(ifaceMethod, classMethod); }
void renameMethod(MethodDef methodDef) { if (methodDef.isVirtual()) throw new ApplicationException("Can't rename virtual methods here"); if (!canRenameMethod(methodDef)) return; var info = method(methodDef); if (info.renamed) return; info.renamed = true; var checker = NameChecker; // PInvoke methods' EntryPoint is always valid. It has to, so always rename. if (!NameChecker.isValidMethodName(info.oldName) || methodDef.MethodDefinition.PInvokeInfo != null) { INameCreator nameCreator = null; string newName = info.suggestedName; if (methodDef.MethodDefinition.PInvokeInfo != null) newName = getPinvokeName(methodDef); else if (methodDef.isStatic()) nameCreator = variableNameState.staticMethodNameCreator; else nameCreator = variableNameState.instanceMethodNameCreator; if (newName != null) nameCreator = new NameCreator2(newName); renameMethod(methodDef, variableNameState.getNewMethodName(info.oldName, nameCreator)); } }
public void add(MethodDef m) { methods.add(m); }
void renameSpecialMethod(MethodDef methodDef, string newName) { if (methodDef == null) return; if (methodDef.isVirtual()) return; renameMethod(methodDef, newName); }
public void addMethodIfEmpty(MethodDef ifaceMethod, MethodDef classMethod) { if (ifaceMethodToClassMethod[ifaceMethod] == null) addMethod(ifaceMethod, classMethod); }
void Add(MethodDef md) { if (md == null || methodDefs.ContainsKey(md)) return; if (md.DeclaringType != null && md.DeclaringType.Module != validModule) return; methodDefs[md] = true; Add(md.Signature); Add(md.ParamDefs); Add(md.GenericParameters); Add(md.DeclSecurities); Add(md.MethodBody); Add(md.CustomAttributes); Add(md.Overrides); Add(md.DeclaringType); }
public MethodNameScope get(MethodDef method) { if (!method.isVirtual()) throw new ApplicationException("Not a virtual method"); MethodNameScope scope; if (!methodScopes.TryGetValue(method, out scope)) { methodScopes[method] = scope = new MethodNameScope(); scope.add(method); } return scope; }
static IField GetFieldRef(MethodDef method) { if (method == null || method.Body == null) return null; var instructions = method.Body.Instructions; int index = 0; var ldarg0 = DotNetUtils.GetInstruction(instructions, ref index); if (ldarg0 == null || ldarg0.GetParameterIndex() != 0) return null; var ldfld = DotNetUtils.GetInstruction(instructions, ref index); if (ldfld == null || ldfld.OpCode.Code != Code.Ldfld) return null; var ret = DotNetUtils.GetInstruction(instructions, ref index); if (ret == null) return null; if (ret.IsStloc()) { var local = ret.GetLocal(method.Body.Variables); ret = DotNetUtils.GetInstruction(instructions, ref index); if (ret == null || !ret.IsLdloc()) return null; if (ret.GetLocal(method.Body.Variables) != local) return null; ret = DotNetUtils.GetInstruction(instructions, ref index); } if (ret == null || ret.OpCode.Code != Code.Ret) return null; return ldfld.Operand as IField; }
public void add(MethodDef method) { methods.Add(method); }
public MMethodDef FindMethod(MethodDef md) { return(methods.Find(md)); }