public void AddMethodIfEmpty(MMethodDef ifaceMethod, MMethodDef classMethod) { if (ifaceMethodToClassMethod[new MethodDefKey(ifaceMethod)] == null) { AddMethod(ifaceMethod, classMethod); } }
public void AddMethodIfEmpty(TypeInfo iface, MMethodDef ifaceMethod, MMethodDef classMethod) { InterfaceMethodInfo info; if (!interfaceMethods.TryGetValue(iface.typeRef, out info)) { throw new ApplicationException("Could not find interface"); } info.AddMethodIfEmpty(ifaceMethod, classMethod); }
// Returns the previous classMethod, or null if none public MMethodDef AddMethod(ITypeDefOrRef iface, MMethodDef ifaceMethod, MMethodDef classMethod) { InterfaceMethodInfo info; if (!interfaceMethods.TryGetValue(iface, out info)) { throw new ApplicationException("Could not find interface"); } return(info.AddMethod(ifaceMethod, classMethod)); }
// Returns the previous method, or null if none public MMethodDef addMethod(MMethodDef ifaceMethod, MMethodDef classMethod) { var ifaceKey = new MethodDefKey(ifaceMethod); if (!ifaceMethodToClassMethod.ContainsKey(ifaceKey)) throw new ApplicationException("Could not find interface method"); MMethodDef oldMethod; ifaceMethodToClassMethod.TryGetValue(ifaceKey, out oldMethod); ifaceMethodToClassMethod[ifaceKey] = classMethod; return oldMethod; }
public MethodNameGroup Get(MMethodDef method) { if (!method.IsVirtual()) { throw new ApplicationException("Not a virtual method"); } if (!methodGroups.TryGetValue(method, out var group)) { methodGroups[method] = group = new MethodNameGroup(); group.Add(method); } return(group); }
// Returns the previous method, or null if none public MMethodDef AddMethod(MMethodDef ifaceMethod, MMethodDef classMethod) { var ifaceKey = new MethodDefKey(ifaceMethod); if (!ifaceMethodToClassMethod.ContainsKey(ifaceKey)) { throw new ApplicationException("Could not find interface method"); } MMethodDef oldMethod; ifaceMethodToClassMethod.TryGetValue(ifaceKey, out oldMethod); ifaceMethodToClassMethod[ifaceKey] = classMethod; return(oldMethod); }
bool CanRenameMethod(MMethodDef methodDef) { var methodInfo = Method(methodDef); if (methodDef.IsStatic()) { if (methodInfo.oldName == ".cctor") return false; } else if (methodDef.IsVirtual()) { if (DotNetUtils.DerivesFromDelegate(type.TypeDef)) { switch (methodInfo.oldName) { case "BeginInvoke": case "EndInvoke": case "Invoke": return false; } } } else { if (methodInfo.oldName == ".ctor") return false; } return true; }
MEventDef CreateEventAdder(string name, MMethodDef eventMethod) { if (string.IsNullOrEmpty(name)) return null; var ownerType = eventMethod.Owner; if (!ownerType.HasModule) return null; if (eventMethod.Event != null) return null; var method = eventMethod.MethodDef; var eventDef = CreateEvent(ownerType, name, GetEventType(method)); if (eventDef == null) return null; if (eventDef.AddMethod != null) return null; if (isVerbose) Logger.v("Restoring event adder {0} ({1:X8}), Event: {2} ({3:X8})", Utils.RemoveNewlines(eventMethod), eventMethod.MethodDef.MDToken.ToInt32(), Utils.RemoveNewlines(eventDef.EventDef), eventDef.EventDef.MDToken.ToInt32()); eventDef.EventDef.AddMethod = eventMethod.MethodDef; eventDef.AddMethod = eventMethod; eventMethod.Event = eventDef; return eventDef; }
public void Add(MMethodDef methodDef) { Get(methodDef); }
void CreateProperty(MPropertyDef propDef, MMethodDef methodDef, string overridePrefix) { if (!methodDef.Owner.HasModule) return; var newPropertyName = overridePrefix + propDef.PropertyDef.Name; if (!DotNetUtils.HasReturnValue(methodDef.MethodDef)) CreatePropertySetter(newPropertyName, methodDef); else CreatePropertyGetter(newPropertyName, methodDef); }
void CreateEvent(MEventDef eventDef, MMethodDef methodDef, EventMethodType methodType, string overridePrefix) { if (!methodDef.Owner.HasModule) return; var newEventName = overridePrefix + eventDef.EventDef.Name; switch (methodType) { case EventMethodType.Adder: CreateEventAdder(newEventName, methodDef); break; case EventMethodType.Remover: CreateEventRemover(newEventName, methodDef); break; } }
MEventDef GetOverriddenEvent(MMethodDef overrideMethod, out MMethodDef overriddenMethod) { var theMethod = overrideMethod.MethodDef.Overrides[0].MethodDeclaration; overriddenMethod = modules.ResolveMethod(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.FindMethod(theMethod); if (overriddenMethod != null) return overriddenMethod.Event; return null; }
static PropertyMethodType GetPropertyMethodType(MMethodDef method) { if (DotNetUtils.HasReturnValue(method.MethodDef)) return PropertyMethodType.Getter; if (method.VisibleParameterCount > 0) return PropertyMethodType.Setter; return PropertyMethodType.Other; }
// Returns the previous classMethod, or null if none public MMethodDef AddMethod(TypeInfo iface, MMethodDef ifaceMethod, MMethodDef classMethod) { return(AddMethod(iface.typeRef, ifaceMethod, classMethod)); }
static string GetOverridePrefix(MethodNameGroup group, MMethodDef method) { if (method == null || method.MethodDef.Overrides.Count == 0) return ""; if (group.Methods.Count > 1) { // Don't use an override prefix if the group has an iface method. foreach (var m in group.Methods) { if (m.Owner.TypeDef.IsInterface) return ""; } } var overrideMethod = method.MethodDef.Overrides[0].MethodDeclaration; if (overrideMethod.DeclaringType == null) return ""; var name = overrideMethod.DeclaringType.FullName.Replace('/', '.'); name = removeGenericsArityRegex.Replace(name, ""); return name + "."; }
void RenameSpecialMethod(MMethodDef methodDef, string newName) { if (methodDef == null) return; if (methodDef.IsVirtual()) return; RenameMethod(methodDef, newName); }
void PrepareRenameMethodArgs(MMethodDef methodDef) { VariableNameState newVariableNameState = null; ParamInfo info; if (methodDef.VisibleParameterCount > 0) { if (IsEventHandler(methodDef)) { info = Param(methodDef.ParamDefs[methodDef.VisibleParameterBaseIndex]); if (!info.GotNewName()) info.newName = "sender"; info = Param(methodDef.ParamDefs[methodDef.VisibleParameterBaseIndex + 1]); if (!info.GotNewName()) info.newName = "e"; } else { newVariableNameState = variableNameState.CloneParamsOnly(); var checker = NameChecker; foreach (var paramDef in methodDef.ParamDefs) { if (paramDef.IsHiddenThisParameter) continue; info = Param(paramDef); if (info.GotNewName()) continue; if (!checker.IsValidMethodArgName(info.oldName)) info.newName = newVariableNameState.GetNewParamName(info.oldName, paramDef.ParameterDef); } } } info = Param(methodDef.ReturnParamDef); if (!info.GotNewName()) { if (!NameChecker.IsValidMethodReturnArgName(info.oldName)) { if (newVariableNameState == null) newVariableNameState = variableNameState.CloneParamsOnly(); info.newName = newVariableNameState.GetNewParamName(info.oldName, methodDef.ReturnParamDef.ParameterDef); } } if ((methodDef.Property != null && methodDef == methodDef.Property.SetMethod) || (methodDef.Event != null && (methodDef == methodDef.Event.AddMethod || methodDef == methodDef.Event.RemoveMethod))) { if (methodDef.VisibleParameterCount > 0) { var paramDef = methodDef.ParamDefs[methodDef.ParamDefs.Count - 1]; Param(paramDef).newName = "value"; } } }
public MethodInfo(MMethodDef methodDef) : base(methodDef) { }
public MethodInfo Method(MMethodDef method) { return allMethodInfos[method]; }
public void add(MMethodDef m) { methods.add(m); }
public MethodInst(MMethodDef origMethodDef, IMethodDefOrRef methodRef) { this.origMethodDef = origMethodDef; this.methodRef = methodRef; }
public MethodDefKey(MMethodDef methodDef) { this.methodDef = methodDef; }
public void Add(MMethodDef m) { methods.Add(m); }
public void RenameMethod(MMethodDef methodDef, string methodName) { if (!CanRenameMethod(methodDef)) return; var methodInfo = Method(methodDef); variableNameState.AddMethodName(methodName); methodInfo.Rename(methodName); }
MEventDef GetOverriddenEvent(MMethodDef overrideMethod) { MMethodDef overriddenMethod; return GetOverriddenEvent(overrideMethod, out overriddenMethod); }
void RenameMethod(MMethodDef 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. bool isValidName = NameChecker.IsValidMethodName(info.oldName); bool isExternPInvoke = methodDef.MethodDef.ImplMap != null && methodDef.MethodDef.RVA == 0; if (!isValidName || isExternPInvoke) { INameCreator nameCreator = null; string newName = info.suggestedName; string newName2; if (methodDef.MethodDef.ImplMap != null && !string.IsNullOrEmpty(newName2 = GetPinvokeName(methodDef))) newName = newName2; else if (methodDef.IsStatic()) nameCreator = variableNameState.staticMethodNameCreator; else nameCreator = variableNameState.instanceMethodNameCreator; if (!string.IsNullOrEmpty(newName)) nameCreator = new NameCreator2(newName); RenameMethod(methodDef, variableNameState.GetNewMethodName(info.oldName, nameCreator)); } }
MPropertyDef GetOverriddenProperty(MMethodDef overrideMethod) { var theMethod = overrideMethod.MethodDef.Overrides[0].MethodDeclaration; var overriddenMethod = modules.ResolveMethod(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.FindMethod(theMethod); if (theMethodDef != null) return theMethodDef.Property; return null; }
string GetPinvokeName(MMethodDef methodDef) { var entryPoint = methodDef.MethodDef.ImplMap.Name.String; if (Regex.IsMatch(entryPoint, @"^#\d+$")) entryPoint = DotNetUtils.GetDllName(methodDef.MethodDef.ImplMap.Module.Name.String) + "_" + entryPoint.Substring(1); return entryPoint; }
MMethodDef GetOverriddenMethod(MMethodDef overrideMethod) { return modules.ResolveMethod(overrideMethod.MethodDef.Overrides[0].MethodDeclaration); }
static bool IsEventHandler(MMethodDef methodDef) { var sig = methodDef.MethodDef.MethodSig; if (sig == null || sig.Params.Count != 2) return false; if (sig.RetType.ElementType != ElementType.Void) return false; if (sig.Params[0].ElementType != ElementType.Object) return false; if (!sig.Params[1].FullName.Contains("EventArgs")) return false; return true; }
MPropertyDef CreatePropertySetter(string name, MMethodDef propMethod) { if (string.IsNullOrEmpty(name)) return null; var ownerType = propMethod.Owner; if (!ownerType.HasModule) return null; if (propMethod.Property != null) return null; var sig = propMethod.MethodDef.MethodSig; if (sig == null || sig.Params.Count == 0) return null; var propType = sig.Params[sig.Params.Count - 1]; var propDef = CreateProperty(ownerType, name, propType, null, propMethod.MethodDef); if (propDef == null) return null; if (propDef.SetMethod != null) return null; if (isVerbose) Logger.v("Restoring property setter {0} ({1:X8}), Property: {2} ({3:X8})", Utils.RemoveNewlines(propMethod), propMethod.MethodDef.MDToken.ToInt32(), Utils.RemoveNewlines(propDef.PropertyDef), propDef.PropertyDef.MDToken.ToInt32()); propDef.PropertyDef.SetMethod = propMethod.MethodDef; propDef.SetMethod = propMethod; propMethod.Property = propDef; return propDef; }
public MethodInfo Method(MMethodDef method) { return memberInfos.Method(method); }
static EventMethodType GetEventMethodType(MMethodDef method) { var evt = method.Event; if (evt == null) return EventMethodType.None; if (evt.AddMethod == method) return EventMethodType.Adder; if (evt.RemoveMethod == method) return EventMethodType.Remover; if (evt.RaiseMethod == method) return EventMethodType.Raiser; return EventMethodType.Other; }
void FindInitializeComponentMethod(MTypeDef type, MMethodDef possibleInitMethod) { foreach (var methodDef in type.AllMethods) { if (methodDef.MethodDef.Name != ".ctor") continue; if (methodDef.MethodDef.Body == null) continue; foreach (var instr in methodDef.MethodDef.Body.Instructions) { if (instr.OpCode.Code != Code.Call && instr.OpCode.Code != Code.Callvirt) continue; if (!MethodEqualityComparer.CompareDeclaringTypes.Equals(possibleInitMethod.MethodDef, instr.Operand as IMethod)) continue; memberInfos.Method(possibleInitMethod).suggestedName = "InitializeComponent"; return; } } }
public void Same(MMethodDef a, MMethodDef b) { Merge(Get(a), Get(b)); }
public void Add(MMethodDef method) { methods.Add(method); }
public void Add(MMethodDef methodDef) => Get(methodDef);
public void add(MMethodDef methodDef) { get(methodDef); }
public void same(MMethodDef a, MMethodDef b) { merge(get(a), get(b)); }
public MethodNameGroup Get(MMethodDef 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; }