示例#1
0
 public void AddMethodIfEmpty(MMethodDef ifaceMethod, MMethodDef classMethod)
 {
     if (ifaceMethodToClassMethod[new MethodDefKey(ifaceMethod)] == null)
     {
         AddMethod(ifaceMethod, classMethod);
     }
 }
示例#2
0
        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);
        }
示例#3
0
        // 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));
        }
示例#4
0
        // 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;
        }
示例#5
0
 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);
 }
示例#6
0
        // 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);
        }
示例#7
0
		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;
		}
示例#8
0
		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;
		}
示例#9
0
 public void Add(MMethodDef methodDef)
 {
     Get(methodDef);
 }
示例#10
0
		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);
		}
示例#11
0
		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;
			}
		}
示例#12
0
		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;
		}
示例#13
0
		static PropertyMethodType GetPropertyMethodType(MMethodDef method) {
			if (DotNetUtils.HasReturnValue(method.MethodDef))
				return PropertyMethodType.Getter;
			if (method.VisibleParameterCount > 0)
				return PropertyMethodType.Setter;
			return PropertyMethodType.Other;
		}
示例#14
0
 // Returns the previous classMethod, or null if none
 public MMethodDef AddMethod(TypeInfo iface, MMethodDef ifaceMethod, MMethodDef classMethod)
 {
     return(AddMethod(iface.typeRef, ifaceMethod, classMethod));
 }
示例#15
0
		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 + ".";
		}
示例#16
0
		void RenameSpecialMethod(MMethodDef methodDef, string newName) {
			if (methodDef == null)
				return;
			if (methodDef.IsVirtual())
				return;
			RenameMethod(methodDef, newName);
		}
示例#17
0
		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";
				}
			}
		}
示例#18
0
		public MethodInfo(MMethodDef methodDef)
			: base(methodDef) {
		}
示例#19
0
		public MethodInfo Method(MMethodDef method) {
			return allMethodInfos[method];
		}
示例#20
0
 public void add(MMethodDef m)
 {
     methods.add(m);
 }
示例#21
0
		public MethodInst(MMethodDef origMethodDef, IMethodDefOrRef methodRef) {
			this.origMethodDef = origMethodDef;
			this.methodRef = methodRef;
		}
示例#22
0
		public MethodDefKey(MMethodDef methodDef) {
			this.methodDef = methodDef;
		}
示例#23
0
		public void Add(MMethodDef m) {
			methods.Add(m);
		}
示例#24
0
		public void RenameMethod(MMethodDef methodDef, string methodName) {
			if (!CanRenameMethod(methodDef))
				return;
			var methodInfo = Method(methodDef);
			variableNameState.AddMethodName(methodName);
			methodInfo.Rename(methodName);
		}
示例#25
0
		MEventDef GetOverriddenEvent(MMethodDef overrideMethod) {
			MMethodDef overriddenMethod;
			return GetOverriddenEvent(overrideMethod, out overriddenMethod);
		}
示例#26
0
		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));
			}
		}
示例#27
0
		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;
		}
示例#28
0
		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;
		}
示例#29
0
		MMethodDef GetOverriddenMethod(MMethodDef overrideMethod) {
			return modules.ResolveMethod(overrideMethod.MethodDef.Overrides[0].MethodDeclaration);
		}
示例#30
0
		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;
		}
示例#31
0
		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;
		}
示例#32
0
		public MethodInfo Method(MMethodDef method) {
			return memberInfos.Method(method);
		}
示例#33
0
		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;
		}
示例#34
0
		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;
				}
			}
		}
示例#35
0
 public void Same(MMethodDef a, MMethodDef b)
 {
     Merge(Get(a), Get(b));
 }
		public void Same(MMethodDef a, MMethodDef b) {
			Merge(Get(a), Get(b));
		}
示例#37
0
 public void Add(MMethodDef method)
 {
     methods.Add(method);
 }
示例#38
0
 public void Add(MMethodDef methodDef) => Get(methodDef);
 public void add(MMethodDef methodDef)
 {
     get(methodDef);
 }
示例#40
0
 public MethodDefKey(MMethodDef methodDef)
 {
     this.methodDef = methodDef;
 }
		public void Add(MMethodDef methodDef) {
			Get(methodDef);
		}
 public void same(MMethodDef a, MMethodDef b)
 {
     merge(get(a), get(b));
 }
示例#43
0
 public void Add(MMethodDef m)
 {
     methods.Add(m);
 }
		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;
		}
示例#45
0
 public MethodInst(MMethodDef origMethodDef, IMethodDefOrRef methodRef)
 {
     this.origMethodDef = origMethodDef;
     this.methodRef     = methodRef;
 }
		public void Add(MMethodDef method) {
			methods.Add(method);
		}