Beispiel #1
0
        // 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;
        }
Beispiel #2
0
		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);
		}
Beispiel #3
0
 public void same(MethodDef a, MethodDef b)
 {
     merge(get(a), get(b));
 }
Beispiel #4
0
 public void add(MethodDef methodDef)
 {
     get(methodDef);
 }
Beispiel #5
0
 public MethodInst(MethodDef origMethodDef, MethodReference methodReference)
 {
     this.origMethodDef = origMethodDef;
     this.methodReference = methodReference;
 }
Beispiel #6
0
 // 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;
		}
Beispiel #8
0
 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;
 }
Beispiel #9
0
 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;
 }
Beispiel #10
0
 public void renameMethod(MethodDef methodDef, string methodName)
 {
     if (!canRenameMethod(methodDef))
         return;
     var methodInfo = method(methodDef);
     variableNameState.addMethodName(methodName);
     methodInfo.rename(methodName);
 }
Beispiel #11
0
 public MethodInfo method(MethodDef method)
 {
     return memberInfos.method(method);
 }
Beispiel #12
0
        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;
        }
Beispiel #13
0
 MethodDef getOverriddenMethod(MethodDef overrideMethod)
 {
     return modules.resolve(overrideMethod.MethodDefinition.Overrides[0]);
 }
Beispiel #14
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;
        }
Beispiel #15
0
 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;
 }
Beispiel #16
0
		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;
		}
Beispiel #17
0
        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;
		}
Beispiel #19
0
 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;
 }
Beispiel #20
0
		public void RemoveMethodDef(MethodDef md) {
			if (!MethodDefs.Remove(md))
				throw new ApplicationException(string.Format("Could not remove MethodDef: {0}", md));
		}
Beispiel #21
0
        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";
                }
            }
        }
Beispiel #22
0
 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);
 }
Beispiel #23
0
        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));
            }
        }
Beispiel #24
0
 public void add(MethodDef m)
 {
     methods.add(m);
 }
Beispiel #25
0
 void renameSpecialMethod(MethodDef methodDef, string newName)
 {
     if (methodDef == null)
         return;
     if (methodDef.isVirtual())
         return;
     renameMethod(methodDef, newName);
 }
Beispiel #26
0
 public void addMethodIfEmpty(MethodDef ifaceMethod, MethodDef classMethod)
 {
     if (ifaceMethodToClassMethod[ifaceMethod] == null)
         addMethod(ifaceMethod, classMethod);
 }
Beispiel #27
0
 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);
 }
Beispiel #28
0
 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;
 }
Beispiel #29
0
		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;
		}
Beispiel #30
0
 public void add(MethodDef method)
 {
     methods.Add(method);
 }
Beispiel #31
0
 public MMethodDef FindMethod(MethodDef md)
 {
     return(methods.Find(md));
 }