From aa529c6c5af582fc49f4f289dc9e31f22893baf4 Mon Sep 17 00:00:00 2001 From: de4dot Date: Sun, 18 Nov 2012 23:48:57 +0100 Subject: [PATCH] Remvove old code --- blocks/DotNetUtils.cs | 653 ---------------------- blocks/MemberRefInstance.cs | 231 -------- blocks/MemberReferenceHelper.cs | 862 ----------------------------- blocks/TypeReferenceUpdaterBase.cs | 92 --- blocks/blocks.csproj | 3 - 5 files changed, 1841 deletions(-) delete mode 100644 blocks/MemberRefInstance.cs delete mode 100644 blocks/MemberReferenceHelper.cs delete mode 100644 blocks/TypeReferenceUpdaterBase.cs diff --git a/blocks/DotNetUtils.cs b/blocks/DotNetUtils.cs index 7455a82e..b0f71415 100644 --- a/blocks/DotNetUtils.cs +++ b/blocks/DotNetUtils.cs @@ -32,55 +32,6 @@ namespace de4dot.blocks { Zune, } -#if PORT - class TypeCache { - ModuleDefinition module; - de4dot.blocks.OLD_REMOVE.TypeDefinitionDict typeRefToDef = new de4dot.blocks.OLD_REMOVE.TypeDefinitionDict(); - - public TypeCache(ModuleDefinition module) { - this.module = module; - init(); - } - - void init() { - foreach (var type in module.GetTypes()) - typeRefToDef.add(type, type); - } - - public TypeDefinition lookup(TypeReference typeReference) { - return typeRefToDef.find(typeReference); - } - } -#endif - -#if PORT - public class TypeCaches { - Dictionary typeCaches = new Dictionary(); - - // Should be called when the whole module is reloaded or when a lot of types have been - // modified (eg. renamed) - public void invalidate(ModuleDefinition module) { - if (module == null) - return; - typeCaches.Remove(module); - } - - // Call this to invalidate all modules - public List invalidateAll() { - var list = new List(typeCaches.Keys); - typeCaches.Clear(); - return list; - } - - public TypeDefinition lookup(ModuleDefinition module, TypeReference typeReference) { - TypeCache typeCache; - if (!typeCaches.TryGetValue(module, out typeCache)) - typeCaches[module] = typeCache = new TypeCache(module); - return typeCache.lookup(typeReference); - } - } -#endif - public class CallCounter { Dictionary calls = new Dictionary(MethodEqualityComparer.CompareDeclaringTypes); @@ -109,44 +60,7 @@ namespace de4dot.blocks { } } -#if PORT - public class MethodCalls { - Dictionary methodCalls = new Dictionary(StringComparer.Ordinal); - - public void addMethodCalls(MethodDef method) { - if (!method.HasBody) - return; - foreach (var instr in method.Body.Instructions) { - var calledMethod = instr.Operand as MethodReference; - if (calledMethod != null) - add(calledMethod); - } - } - - public void add(MethodReference method) { - string key = method.FullName; - if (!methodCalls.ContainsKey(key)) - methodCalls[key] = 0; - methodCalls[key]++; - } - - public int count(string methodFullName) { - int count; - methodCalls.TryGetValue(methodFullName, out count); - return count; - } - - public bool called(string methodFullName) { - return count(methodFullName) != 0; - } - } -#endif - public static class DotNetUtils { -#if PORT - public static readonly TypeCaches typeCaches = new TypeCaches(); -#endif - public static TypeDef getModuleType(ModuleDef module) { return module.GlobalType; } @@ -222,16 +136,6 @@ namespace de4dot.blocks { return type != null && isDelegate(type.BaseType); } -#if PORT - public static bool isSameAssembly(TypeReference type, string assembly) { - return MemberReferenceHelper.getCanonicalizedScopeName(type.Scope) == assembly.ToLowerInvariant(); - } - - public static bool isMethod(MethodReference method, string returnType, string parameters) { - return method != null && method.FullName == returnType + " " + method.DeclaringType.FullName + "::" + method.Name + parameters; - } -#endif - public static bool isMethod(IMethod method, string returnType, string parameters) { return method != null && method.FullName == returnType + " " + method.DeclaringType.FullName + "::" + method.Name + parameters; } @@ -275,30 +179,6 @@ namespace de4dot.blocks { return getDllName(dll).Equals(getDllName(method.ImplMap.Module.Name.String), StringComparison.OrdinalIgnoreCase); } -#if PORT - public static MethodDef getMethod(TypeDefinition type, string name) { - if (type == null) - return null; - foreach (var method in type.Methods) { - if (method.Name == name) - return method; - } - return null; - } - - public static MethodDef getMethod(TypeDefinition type, MethodReference methodReference) { - if (type == null || methodReference == null) - return null; - if (methodReference is MethodDef) - return (MethodDef)methodReference; - foreach (var method in type.Methods) { - if (MemberReferenceHelper.compareMethodReference(method, methodReference)) - return method; - } - return null; - } -#endif - public static MethodDef getMethod(ModuleDefMD module, IMethod method) { if (method == null) return null; @@ -386,16 +266,6 @@ namespace de4dot.blocks { } } -#if PORT - public static TypeDefinition getType(ModuleDefinition module, TypeReference typeReference) { - if (typeReference == null) - return null; - if (typeReference is TypeDefinition) - return (TypeDefinition)typeReference; - return typeCaches.lookup(module, typeReference); - } -#endif - public static FieldDef getField(ModuleDef module, IField field) { if (field == null) return null; @@ -422,18 +292,6 @@ namespace de4dot.blocks { return null; } -#if PORT - public static FieldDefinition getFieldByName(TypeDefinition type, string name) { - if (type == null) - return null; - foreach (var field in type.Fields) { - if (field.Name == name) - return field; - } - return null; - } -#endif - public static IEnumerable getMethodCalls(MethodDef method) { var list = new List(); if (method.HasBody) { @@ -446,35 +304,6 @@ namespace de4dot.blocks { return list; } -#if PORT - public static MethodCalls getMethodCallCounts(MethodDef method) { - var methodCalls = new MethodCalls(); - methodCalls.addMethodCalls(method); - return methodCalls; - } - - public static bool hasString(MethodDef method, string s) { - if (method == null || method.Body == null) - return false; - foreach (var instr in method.Body.Instructions) { - if (instr.OpCode.Code == Code.Ldstr && (string)instr.Operand == s) - return true; - } - return false; - } - - public static IList getCodeStrings(MethodDef method) { - var strings = new List(); - if (method != null && method.Body != null) { - foreach (var instr in method.Body.Instructions) { - if (instr.OpCode.Code == Code.Ldstr) - strings.Add((string)instr.Operand); - } - } - return strings; - } -#endif - public static IList getCodeStrings(MethodDef method) { var strings = new List(); if (method != null && method.Body != null) { @@ -516,27 +345,6 @@ namespace de4dot.blocks { return s.Substring(0, index); } -#if PORT - // Copies most things but not everything - public static MethodDef clone(MethodDef method) { - var newMethod = new MethodDef(method.Name, method.Attributes, method.MethodReturnType.ReturnType); - newMethod.MetadataToken = method.MetadataToken; - newMethod.Attributes = method.Attributes; - newMethod.ImplAttributes = method.ImplAttributes; - newMethod.HasThis = method.HasThis; - newMethod.ExplicitThis = method.ExplicitThis; - newMethod.CallingConvention = method.CallingConvention; - newMethod.SemanticsAttributes = method.SemanticsAttributes; - newMethod.DeclaringType = method.DeclaringType; - foreach (var arg in method.Parameters) - newMethod.Parameters.Add(new ParameterDefinition(arg.Name, arg.Attributes, arg.ParameterType)); - foreach (var gp in method.GenericParameters) - newMethod.GenericParameters.Add(new GenericParameter(gp.Name, newMethod) { Attributes = gp.Attributes }); - copyBodyFromTo(method, newMethod); - return newMethod; - } -#endif - // Copies most things but not everything public static MethodDef clone(MethodDef method) { var newMethod = new MethodDefUser(method.Name, method.MethodSig, method.ImplAttributes, method.Attributes); @@ -555,65 +363,6 @@ namespace de4dot.blocks { return newMethod; } -#if PORT - public static Instruction clone(Instruction instr) { - return new Instruction { - Offset = instr.Offset, - OpCode = instr.OpCode, - Operand = instr.Operand, - SequencePoint = instr.SequencePoint, - }; - } - - public static void copyBody(MethodDef method, out IList instructions, out IList exceptionHandlers) { - if (method == null || !method.HasBody) { - instructions = new List(); - exceptionHandlers = new List(); - return; - } - - var oldInstrs = method.Body.Instructions; - var oldExHandlers = method.Body.ExceptionHandlers; - instructions = new List(oldInstrs.Count); - exceptionHandlers = new List(oldExHandlers.Count); - var oldToIndex = Utils.createObjectToIndexDictionary(oldInstrs); - - foreach (var oldInstr in oldInstrs) - instructions.Add(clone(oldInstr)); - - foreach (var newInstr in instructions) { - var operand = newInstr.Operand; - if (operand is Instruction) - newInstr.Operand = instructions[oldToIndex[(Instruction)operand]]; - else if (operand is Instruction[]) { - var oldArray = (Instruction[])operand; - var newArray = new Instruction[oldArray.Length]; - for (int i = 0; i < oldArray.Length; i++) - newArray[i] = instructions[oldToIndex[oldArray[i]]]; - newInstr.Operand = newArray; - } - } - - foreach (var oldEx in oldExHandlers) { - var newEx = new ExceptionHandler(oldEx.HandlerType) { - TryStart = getInstruction(instructions, oldToIndex, oldEx.TryStart), - TryEnd = getInstruction(instructions, oldToIndex, oldEx.TryEnd), - FilterStart = getInstruction(instructions, oldToIndex, oldEx.FilterStart), - HandlerStart= getInstruction(instructions, oldToIndex, oldEx.HandlerStart), - HandlerEnd = getInstruction(instructions, oldToIndex, oldEx.HandlerEnd), - CatchType = oldEx.CatchType, - }; - exceptionHandlers.Add(newEx); - } - } - - static Instruction getInstruction(IList instructions, IDictionary instructionToIndex, Instruction instruction) { - if (instruction == null) - return null; - return instructions[instructionToIndex[instruction]]; - } -#endif - public static void copyBody(MethodDef method, out IList instructions, out IList exceptionHandlers) { if (method == null || !method.HasBody) { instructions = new List(); @@ -662,23 +411,6 @@ namespace de4dot.blocks { return instructions[instructionToIndex[instruction]]; } -#if PORT - public static void restoreBody(MethodDef method, IEnumerable instructions, IEnumerable exceptionHandlers) { - if (method == null || !method.HasBody) - return; - - var bodyInstrs = method.Body.Instructions; - bodyInstrs.Clear(); - foreach (var instr in instructions) - bodyInstrs.Add(instr); - - var bodyExceptionHandlers = method.Body.ExceptionHandlers; - bodyExceptionHandlers.Clear(); - foreach (var eh in exceptionHandlers) - bodyExceptionHandlers.Add(eh); - } -#endif - public static void restoreBody(MethodDef method, IEnumerable instructions, IEnumerable exceptionHandlers) { if (method == null || method.Body == null) return; @@ -739,19 +471,6 @@ namespace de4dot.blocks { } } -#if PORT - public static IEnumerable findAttributes(ICustomAttributeProvider custAttrProvider, TypeReference attr) { - var list = new List(); - if (custAttrProvider == null) - return list; - foreach (var cattr in custAttrProvider.CustomAttributes) { - if (MemberReferenceHelper.compareTypes(attr, cattr.AttributeType)) - list.Add(cattr); - } - return list; - } -#endif - public static string getCustomArgAsString(CustomAttribute cattr, int arg) { if (cattr == null || arg >= cattr.ConstructorArguments.Count) return null; @@ -795,234 +514,12 @@ namespace de4dot.blocks { return list; } -#if PORT - public static bool hasReturnValue(IMethodSignature method) { - var type = method.MethodReturnType.ReturnType; - while (type.IsOptionalModifier || type.IsRequiredModifier) - type = ((TypeSpecification)type).ElementType; - return type.EType != ElementType.Void; - } -#endif - public static bool hasReturnValue(IMethod method) { if (method == null || method.MethodSig == null || method.MethodSig.RetType == null) return false; return method.MethodSig.RetType.RemovePinnedAndModifiers().ElementType != ElementType.Void; } -#if PORT - public static void updateStack(Instruction instr, ref int stack, bool methodHasReturnValue) { - int pushes, pops; - calculateStackUsage(instr, methodHasReturnValue, out pushes, out pops); - if (pops == -1) - stack = 0; - else - stack += pushes - pops; - } - - // Sets pops to -1 if the stack is supposed to be cleared - public static void calculateStackUsage(Instruction instr, bool methodHasReturnValue, out int pushes, out int pops) { - if (instr.OpCode.FlowControl == FlowControl.Call) - calculateStackUsage_call(instr, out pushes, out pops); - else - calculateStackUsage_nonCall(instr, methodHasReturnValue, out pushes, out pops); - } - - static void calculateStackUsage_call(Instruction instr, out int pushes, out int pops) { - pushes = 0; - pops = 0; - - var method = (IMethodSignature)instr.Operand; - bool implicitThis = method.HasThis && !method.ExplicitThis; - if (hasReturnValue(method) || (instr.OpCode.Code == Code.Newobj && method.HasThis)) - pushes++; - - if (method.HasParameters) - pops += method.Parameters.Count; - if (implicitThis && instr.OpCode.Code != Code.Newobj) - pops++; - if (instr.OpCode.Code == Code.Calli) - pops++; - } - - // Sets pops to -1 if the stack is supposed to be cleared - static void calculateStackUsage_nonCall(Instruction instr, bool methodHasReturnValue, out int pushes, out int pops) { - StackBehaviour stackBehavior; - - pushes = 0; - pops = 0; - - stackBehavior = instr.OpCode.StackBehaviourPush; - switch (stackBehavior) { - case StackBehaviour.Push0: - break; - - case StackBehaviour.Push1: - case StackBehaviour.Pushi: - case StackBehaviour.Pushi8: - case StackBehaviour.Pushr4: - case StackBehaviour.Pushr8: - case StackBehaviour.Pushref: - pushes++; - break; - - case StackBehaviour.Push1_push1: - pushes += 2; - break; - - case StackBehaviour.Varpush: // only call, calli, callvirt which are handled elsewhere - default: - throw new ApplicationException(string.Format("Unknown push StackBehavior {0}", stackBehavior)); - } - - stackBehavior = instr.OpCode.StackBehaviourPop; - switch (stackBehavior) { - case StackBehaviour.Pop0: - break; - - case StackBehaviour.Pop1: - case StackBehaviour.Popi: - case StackBehaviour.Popref: - pops++; - break; - - case StackBehaviour.Pop1_pop1: - case StackBehaviour.Popi_pop1: - case StackBehaviour.Popi_popi: - case StackBehaviour.Popi_popi8: - case StackBehaviour.Popi_popr4: - case StackBehaviour.Popi_popr8: - case StackBehaviour.Popref_pop1: - case StackBehaviour.Popref_popi: - pops += 2; - break; - - case StackBehaviour.Popi_popi_popi: - case StackBehaviour.Popref_popi_popi: - case StackBehaviour.Popref_popi_popi8: - case StackBehaviour.Popref_popi_popr4: - case StackBehaviour.Popref_popi_popr8: - case StackBehaviour.Popref_popi_popref: - pops += 3; - break; - - case StackBehaviour.PopAll: - pops = -1; - break; - - case StackBehaviour.Varpop: // call, calli, callvirt, newobj (all handled elsewhere), and ret - if (methodHasReturnValue) - pops++; - break; - - default: - throw new ApplicationException(string.Format("Unknown pop StackBehavior {0}", stackBehavior)); - } - } - - public static AssemblyNameReference getAssemblyNameReference(TypeReference type) { - var scope = type.Scope; - if (scope == null) - return null; - - if (scope is ModuleDefinition) { - var moduleDefinition = (ModuleDefinition)scope; - return moduleDefinition.Assembly.Name; - } - - if (scope is AssemblyNameReference) - return (AssemblyNameReference)scope; - - if (scope is ModuleReference && type.Module.Assembly != null) { - foreach (var module in type.Module.Assembly.Modules) { - if (scope.Name == module.Name) - return type.Module.Assembly.Name; - } - } - - throw new ApplicationException(string.Format("Unknown IMetadataScope type: {0}", scope.GetType())); - } - - public static string getFullAssemblyName(TypeReference type) { - var asmRef = getAssemblyNameReference(type); - return asmRef == null ? null : asmRef.FullName; - } - - public static bool isAssembly(IMetadataScope scope, string assemblySimpleName) { - return scope.Name == assemblySimpleName || - scope.Name.StartsWith(assemblySimpleName + ",", StringComparison.Ordinal); - } - - public static bool isReferenceToModule(ModuleReference moduleReference, IMetadataScope scope) { - switch (scope.MetadataScopeType) { - case MetadataScopeType.AssemblyNameReference: - var asmRef = (AssemblyNameReference)scope; - var module = moduleReference as ModuleDefinition; - return module != null && module.Assembly != null && module.Assembly.Name.FullName == asmRef.FullName; - - case MetadataScopeType.ModuleDefinition: - return moduleReference == scope; - - case MetadataScopeType.ModuleReference: - return moduleReference.Name == ((ModuleReference)scope).Name; - - default: - throw new ApplicationException("Unknown MetadataScopeType"); - } - } - - public static int getArgIndex(Instruction instr) { - switch (instr.OpCode.Code) { - case Code.Ldarg_0: return 0; - case Code.Ldarg_1: return 1; - case Code.Ldarg_2: return 2; - case Code.Ldarg_3: return 3; - - case Code.Ldarga: - case Code.Ldarga_S: - case Code.Ldarg: - case Code.Ldarg_S: - return getArgIndex(instr.Operand as ParameterDefinition); - } - - return -1; - } - - public static int getArgIndex(ParameterDefinition arg) { - if (arg == null) - return -1; - return arg.Sequence; - } - - public static List getParameters(MethodReference method) { - var args = new List(method.Parameters.Count + 1); - if (method.HasImplicitThis) { - var methodDef = method as MethodDef; - if (methodDef != null && methodDef.Body != null) - args.Add(methodDef.Body.ThisParameter); - else - args.Add(new ParameterDefinition(method.DeclaringType, method)); - } - foreach (var arg in method.Parameters) - args.Add(arg); - return args; - } - - public static ParameterDefinition getParameter(MethodReference method, Instruction instr) { - return getParameter(getParameters(method), instr); - } - - public static ParameterDefinition getParameter(IList parameters, Instruction instr) { - return getParameter(parameters, getArgIndex(instr)); - } - - public static ParameterDefinition getParameter(IList parameters, int index) { - if (0 <= index && index < parameters.Count) - return parameters[index]; - return null; - } -#endif - public static Parameter getParameter(IList parameters, int index) { if (0 <= index && index < parameters.Count) return parameters[index]; @@ -1035,17 +532,6 @@ namespace de4dot.blocks { return null; } -#if PORT - public static List getArgs(MethodReference method) { - var args = new List(method.Parameters.Count + 1); - if (method.HasImplicitThis) - args.Add(method.DeclaringType); - foreach (var arg in method.Parameters) - args.Add(arg.ParameterType); - return args; - } -#endif - public static List getArgs(IMethod method) { var sig = method.MethodSig; var args = new List(sig.Params.Count + 1); @@ -1056,29 +542,6 @@ namespace de4dot.blocks { return args; } -#if PORT - public static TypeReference getArgType(MethodReference method, Instruction instr) { - return getArgType(getArgs(method), instr); - } - - public static TypeReference getArgType(IList methodArgs, Instruction instr) { - return getArgType(methodArgs, getArgIndex(instr)); - } - - public static TypeReference getArgType(IList methodArgs, int index) { - if (0 <= index && index < methodArgs.Count) - return methodArgs[index]; - return null; - } - - public static int getArgsCount(MethodReference method) { - int count = method.Parameters.Count; - if (method.HasImplicitThis) - count++; - return count; - } -#endif - public static int getArgsCount(IMethod method) { var sig = method.MethodSig; if (sig == null) @@ -1104,7 +567,6 @@ namespace de4dot.blocks { return GenericArgsSubstitutor.create(type, typeArgs, genMethodArgs); } -#if PORT public static Instruction getInstruction(IList instructions, ref int index) { for (int i = 0; i < 10; i++) { if (index < 0 || index >= instructions.Count) @@ -1123,97 +585,6 @@ namespace de4dot.blocks { } return null; } -#endif - - public static Instruction getInstruction(IList instructions, ref int index) { - for (int i = 0; i < 10; i++) { - if (index < 0 || index >= instructions.Count) - return null; - var instr = instructions[index++]; - if (instr.OpCode.Code == Code.Nop) - continue; - if (instr.OpCode.OpCodeType == OpCodeType.Prefix) - continue; - if (instr == null || (instr.OpCode.Code != Code.Br && instr.OpCode.Code != Code.Br_S)) - return instr; - instr = instr.Operand as Instruction; - if (instr == null) - return null; - index = instructions.IndexOf(instr); - } - return null; - } - -#if PORT - public static PropertyDefinition createPropertyDefinition(string name, TypeReference propType, MethodDef getter, MethodDef setter) { - return new PropertyDefinition(name, PropertyAttributes.None, propType) { - MetadataToken = nextPropertyToken(), - GetMethod = getter, - SetMethod = setter, - }; - } - - public static EventDefinition createEventDefinition(string name, TypeReference eventType) { - return new EventDefinition(name, EventAttributes.None, eventType) { - MetadataToken = nextEventToken(), - }; - } - - public static FieldDefinition createFieldDefinition(string name, FieldAttributes attributes, TypeReference fieldType) { - return new FieldDefinition(name, attributes, fieldType) { - MetadataToken = nextFieldToken(), - }; - } - - static int nextTokenRid = 0x00FFFFFF; - public static MetadataToken nextTypeRefToken() { - return new MetadataToken(TokenType.TypeRef, nextTokenRid--); - } - - public static MetadataToken nextTypeDefToken() { - return new MetadataToken(TokenType.TypeDef, nextTokenRid--); - } - - public static MetadataToken nextFieldToken() { - return new MetadataToken(TokenType.Field, nextTokenRid--); - } - - public static MetadataToken nextMethodToken() { - return new MetadataToken(TokenType.Method, nextTokenRid--); - } - - public static MetadataToken nextPropertyToken() { - return new MetadataToken(TokenType.Property, nextTokenRid--); - } - - public static MetadataToken nextEventToken() { - return new MetadataToken(TokenType.Event, nextTokenRid--); - } - - public static TypeReference findTypeReference(ModuleDefinition module, string asmSimpleName, string fullName) { - foreach (var type in module.GetTypeReferences()) { - if (type.FullName != fullName) - continue; - var asmRef = type.Scope as AssemblyNameReference; - if (asmRef == null || asmRef.Name != asmSimpleName) - continue; - - return type; - } - return null; - } - - public static TypeReference findOrCreateTypeReference(ModuleDefinition module, AssemblyNameReference asmRef, string ns, string name, bool isValueType) { - var typeRef = findTypeReference(module, asmRef.Name, ns + "." + name); - if (typeRef != null) - return typeRef; - - typeRef = new TypeReference(ns, name, module, asmRef); - typeRef.MetadataToken = nextTypeRefToken(); - typeRef.IsValueType = isValueType; - return typeRef; - } -#endif public static TypeDefOrRefSig findOrCreateTypeReference(ModuleDef module, AssemblyRef asmRef, string ns, string name, bool isValueType) { var typeRef = module.UpdateRowId(new TypeRefUser(module, ns, name, asmRef)); @@ -1339,29 +710,5 @@ namespace de4dot.blocks { args.Reverse(); return args; } - -#if PORT - public static AssemblyNameReference addAssemblyReference(ModuleDefinition module, AssemblyNameReference asmRef) { - foreach (var modAsmRef in module.AssemblyReferences) { - if (modAsmRef.FullName == asmRef.FullName) - return modAsmRef; - } - - var newAsmRef = AssemblyNameReference.Parse(asmRef.FullName); - module.AssemblyReferences.Add(newAsmRef); - return newAsmRef; - } - - public static ModuleReference addModuleReference(ModuleDefinition module, ModuleReference modRef) { - foreach (var modModRef in module.ModuleReferences) { - if (modModRef.Name == modRef.Name) - return modModRef; - } - - var newModRef = new ModuleReference(modRef.Name); - module.ModuleReferences.Add(newModRef); - return newModRef; - } -#endif } } diff --git a/blocks/MemberRefInstance.cs b/blocks/MemberRefInstance.cs deleted file mode 100644 index add812e4..00000000 --- a/blocks/MemberRefInstance.cs +++ /dev/null @@ -1,231 +0,0 @@ -/* - Copyright (C) 2011-2012 de4dot@gmail.com - - This file is part of de4dot. - - de4dot is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - de4dot is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with de4dot. If not, see . -*/ - -#if PORT - -// Create a new type, method, etc, where all generic parameters have been replaced with the -// corresponding generic argument. - -using System; -using Mono.Cecil; -using de4dot.blocks; - -namespace de4dot.blocks { - public class TypeReferenceInstance : TypeReferenceUpdaterBase { - TypeReference typeReference; - GenericInstanceType git; - IGenericInstance gim; - bool modified; - - public static TypeReference make(TypeReference typeReference, GenericInstanceType git) { - return make(typeReference, git, null); - } - - public static TypeReference make(TypeReference typeReference, GenericInstanceType git, IGenericInstance gim) { - if (git == null && gim == null) - return typeReference; - return new TypeReferenceInstance(typeReference, git, gim).makeInstance(); - } - - TypeReferenceInstance(TypeReference typeReference, GenericInstanceType git, IGenericInstance gim) { - this.typeReference = typeReference; - this.git = git; - this.gim = gim; - } - - void checkModified(object a, object b) { - if (!ReferenceEquals(a, b)) - modified = true; - } - - // Returns same one if nothing was modified - TypeReference makeInstance() { - var rv = update(typeReference); - return modified ? rv : typeReference; - } - - protected override FunctionPointerType updateFunctionPointerType(FunctionPointerType a) { - var rv = new FunctionPointerType(); - rv.function = MethodReferenceInstance.make(a.function, git, gim); - checkModified(a.function, rv.function); - return rv; - } - - protected override TypeReference updateGenericParameter(GenericParameter a) { - switch (a.Type) { - case GenericParameterType.Type: - if (git == null || a.Position >= git.GenericArguments.Count || - !MemberReferenceHelper.compareTypes(git.ElementType, a.Owner as TypeReference)) { - return a; - } - modified = true; - return update(git.GenericArguments[a.Position]); - - case GenericParameterType.Method: - if (gim == null || a.Position >= gim.GenericArguments.Count) - return a; - modified = true; - return update(gim.GenericArguments[a.Position]); - - default: - return a; - } - } - } - - public abstract class MultiTypeRefInstance { - GenericInstanceType git; - IGenericInstance gim; - bool modified; - - public MultiTypeRefInstance(GenericInstanceType git) - : this(git, null) { - } - - public MultiTypeRefInstance(GenericInstanceType git, IGenericInstance gim) { - this.git = git; - this.gim = gim; - } - - void checkModified(object a, object b) { - if (!ReferenceEquals(a, b)) - modified = true; - } - - protected TypeReference makeInstance(TypeReference tr) { - var type = TypeReferenceInstance.make(tr, git, gim); - checkModified(type, tr); - return type; - } - - protected T getResult(T orig, T newOne) { - return modified ? newOne : orig; - } - } - - public class MethodReferenceInstance : MultiTypeRefInstance { - MethodReference methodReference; - - public static MethodReference make(MethodReference methodReference, GenericInstanceType git) { - return make(methodReference, git, null); - } - - public static MethodReference make(MethodReference methodReference, GenericInstanceType git, IGenericInstance gim) { - if (git == null && gim == null) - return methodReference; - return new MethodReferenceInstance(methodReference, git, gim).makeInstance(); - } - - MethodReferenceInstance(MethodReference methodReference, GenericInstanceType git, IGenericInstance gim) - : base(git, gim) { - this.methodReference = methodReference; - } - - MethodReference makeInstance() { - var mr = new MethodReference(methodReference.Name, makeInstance(methodReference.MethodReturnType.ReturnType), methodReference.DeclaringType); - mr.HasThis = methodReference.HasThis; - mr.ExplicitThis = methodReference.ExplicitThis; - mr.CallingConvention = methodReference.CallingConvention; - - if (methodReference.HasParameters) { - foreach (var param in methodReference.Parameters) { - var newParam = new ParameterDefinition(param.Name, param.Attributes, makeInstance(param.ParameterType)); - mr.Parameters.Add(newParam); - } - } - - if (methodReference.HasGenericParameters) { - foreach (var param in methodReference.GenericParameters) { - var newParam = new GenericParameter(param.Name, mr); - mr.GenericParameters.Add(newParam); - } - } - - return getResult(methodReference, mr); - } - } - - public class FieldReferenceInstance : MultiTypeRefInstance { - FieldReference fieldReference; - - public static FieldReference make(FieldReference fieldReference, GenericInstanceType git) { - if (git == null) - return fieldReference; - return new FieldReferenceInstance(fieldReference, git).makeInstance(); - } - - FieldReferenceInstance(FieldReference fieldReference, GenericInstanceType git) - : base(git) { - this.fieldReference = fieldReference; - } - - FieldReference makeInstance() { - var fr = new FieldReference(fieldReference.Name, makeInstance(fieldReference.FieldType)); - return getResult(fieldReference, fr); - } - } - - public class EventReferenceInstance : MultiTypeRefInstance { - EventReference eventReference; - - public static EventReference make(EventReference eventReference, GenericInstanceType git) { - if (git == null) - return eventReference; - return new EventReferenceInstance(eventReference, git).makeInstance(); - } - - EventReferenceInstance(EventReference eventReference, GenericInstanceType git) - : base(git) { - this.eventReference = eventReference; - } - - EventReference makeInstance() { - var er = new EventDefinition(eventReference.Name, (EventAttributes)0, makeInstance(eventReference.EventType)); - return getResult(eventReference, er); - } - } - - public class PropertyReferenceInstance : MultiTypeRefInstance { - PropertyReference propertyReference; - - public static PropertyReference make(PropertyReference propertyReference, GenericInstanceType git) { - if (git == null) - return propertyReference; - return new PropertyReferenceInstance(propertyReference, git).makeInstance(); - } - - PropertyReferenceInstance(PropertyReference propertyReference, GenericInstanceType git) - : base(git) { - this.propertyReference = propertyReference; - } - - PropertyReference makeInstance() { - var pr = new PropertyDefinition(propertyReference.Name, (PropertyAttributes)0, makeInstance(propertyReference.PropertyType)); - if (propertyReference.Parameters != null) { - foreach (var param in propertyReference.Parameters) { - var newParam = new ParameterDefinition(param.Name, param.Attributes, makeInstance(param.ParameterType)); - pr.Parameters.Add(newParam); - } - } - - return getResult(propertyReference, pr); - } - } -} -#endif diff --git a/blocks/MemberReferenceHelper.cs b/blocks/MemberReferenceHelper.cs deleted file mode 100644 index 88e433bf..00000000 --- a/blocks/MemberReferenceHelper.cs +++ /dev/null @@ -1,862 +0,0 @@ -/* - Copyright (C) 2011-2012 de4dot@gmail.com - - This file is part of de4dot. - - de4dot is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - de4dot is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with de4dot. If not, see . -*/ - -#if PORT -using System; -using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Metadata; - -namespace de4dot.blocks.OLD_REMOVE { - public class TypeDefinitionDict { - Dictionary tokenToValue = new Dictionary(); - Dictionary tokenToKey = new Dictionary(); - Dictionary refToValue = new Dictionary(); - Dictionary refToKey = new Dictionary(); - - public int Count { - get { return tokenToValue.Count; } - } - - public IEnumerable getKeys() { - return tokenToKey.Values; - } - - public IEnumerable getValues() { - return tokenToValue.Values; - } - - ScopeAndTokenKey getTokenKey(TypeReference typeReference) { - return new ScopeAndTokenKey(typeReference); - } - - TypeReferenceKey getReferenceKey(TypeReference typeReference) { - return new TypeReferenceKey(typeReference); - } - - public TValue find(TypeReference typeReference) { - TValue value; - if (typeReference is TypeDefinition) - tokenToValue.TryGetValue(getTokenKey(typeReference), out value); - else - refToValue.TryGetValue(getReferenceKey(typeReference), out value); - return value; - } - - public TValue findAny(TypeReference typeReference) { - TValue value; - if (tokenToValue.TryGetValue(getTokenKey(typeReference), out value)) - return value; - - refToValue.TryGetValue(getReferenceKey(typeReference), out value); - return value; - } - - public void add(TypeDefinition typeDefinition, TValue value) { - var tokenKey = getTokenKey(typeDefinition); - tokenToValue[tokenKey] = value; - tokenToKey[tokenKey] = typeDefinition; - - var refKey = getReferenceKey(typeDefinition); - if (!refToValue.ContainsKey(refKey) || - getAccessibilityOrder(typeDefinition) < getAccessibilityOrder(refToKey[refKey])) { - refToKey[refKey] = typeDefinition; - refToValue[refKey] = value; - } - } - - // Order: public, family, assembly, private - static int[] accessibilityOrder = new int[8] { - 40, // NotPublic - 0, // Public - 10, // NestedPublic - 70, // NestedPrivate - 20, // NestedFamily - 50, // NestedAssembly - 60, // NestedFamANDAssem - 30, // NestedFamORAssem - }; - static int getAccessibilityOrder(TypeDefinition typeDefinition) { - return accessibilityOrder[(int)typeDefinition.Attributes & 7]; - } - - public void onTypesRenamed() { - var newTypeRefToValue = new Dictionary(refToValue.Count); - foreach (var kvp in refToValue) - newTypeRefToValue[getReferenceKey((TypeDefinition)kvp.Key.TypeReference)] = kvp.Value; - refToValue = newTypeRefToValue; - } - } - - public abstract class FieldDefinitionDictBase { - Dictionary tokenToValue = new Dictionary(); - Dictionary tokenToKey = new Dictionary(); - Dictionary refToValue = new Dictionary(); - Dictionary refToKey = new Dictionary(); - - public int Count { - get { return tokenToValue.Count; } - } - - public IEnumerable getKeys() { - return tokenToKey.Values; - } - - public IEnumerable getValues() { - return tokenToValue.Values; - } - - ScopeAndTokenKey getTokenKey(FieldReference fieldReference) { - return new ScopeAndTokenKey(fieldReference); - } - - protected abstract IFieldReferenceKey getReferenceKey(FieldReference fieldReference); - - public TValue find(FieldReference fieldReference) { - TValue value; - if (fieldReference is FieldDefinition) - tokenToValue.TryGetValue(getTokenKey(fieldReference), out value); - else - refToValue.TryGetValue(getReferenceKey(fieldReference), out value); - return value; - } - - public TValue findAny(FieldReference fieldReference) { - TValue value; - if (tokenToValue.TryGetValue(getTokenKey(fieldReference), out value)) - return value; - - refToValue.TryGetValue(getReferenceKey(fieldReference), out value); - return value; - } - - public void add(FieldDefinition fieldDefinition, TValue value) { - var tokenKey = getTokenKey(fieldDefinition); - tokenToValue[tokenKey] = value; - tokenToKey[tokenKey] = fieldDefinition; - - var refKey = getReferenceKey(fieldDefinition); - if (!refToValue.ContainsKey(refKey) || - getAccessibilityOrder(fieldDefinition) < getAccessibilityOrder(refToKey[refKey])) { - refToKey[refKey] = fieldDefinition; - refToValue[refKey] = value; - } - } - - // Order: public, family, assembly, private - static int[] accessibilityOrder = new int[8] { - 60, // CompilerControlled - 50, // Private - 40, // FamANDAssem - 30, // Assembly - 10, // Family - 20, // FamORAssem - 0, // Public - 70, // - }; - static int getAccessibilityOrder(FieldDefinition fieldDefinition) { - return accessibilityOrder[(int)fieldDefinition.Attributes & 7]; - } - - public void onTypesRenamed() { - var newFieldRefToDef = new Dictionary(refToValue.Count); - foreach (var kvp in refToValue) - newFieldRefToDef[getReferenceKey((FieldDefinition)kvp.Key.FieldReference)] = kvp.Value; - refToValue = newFieldRefToDef; - } - } - - public class FieldDefinitionDict : FieldDefinitionDictBase { - protected override IFieldReferenceKey getReferenceKey(FieldReference fieldReference) { - return new FieldReferenceKey(fieldReference); - } - } - - public class FieldDefinitionAndDeclaringTypeDict : FieldDefinitionDictBase { - protected override IFieldReferenceKey getReferenceKey(FieldReference fieldReference) { - return new FieldReferenceAndDeclaringTypeKey(fieldReference); - } - } - - public abstract class MethodDefinitionDictBase { - Dictionary tokenToValue = new Dictionary(); - Dictionary tokenToKey = new Dictionary(); - Dictionary refToValue = new Dictionary(); - Dictionary refToKey = new Dictionary(); - - public int Count { - get { return tokenToValue.Count; } - } - - public IEnumerable getKeys() { - return tokenToKey.Values; - } - - public IEnumerable getValues() { - return tokenToValue.Values; - } - - ScopeAndTokenKey getTokenKey(MethodReference methodReference) { - return new ScopeAndTokenKey(methodReference); - } - - protected abstract IMethodReferenceKey getReferenceKey(MethodReference methodReference); - - public TValue find(MethodReference methodReference) { - TValue value; - if (methodReference is MethodDefinition) - tokenToValue.TryGetValue(getTokenKey(methodReference), out value); - else - refToValue.TryGetValue(getReferenceKey(methodReference), out value); - return value; - } - - public TValue findAny(MethodReference methodReference) { - TValue value; - if (tokenToValue.TryGetValue(getTokenKey(methodReference), out value)) - return value; - - refToValue.TryGetValue(getReferenceKey(methodReference), out value); - return value; - } - - public void add(MethodDefinition methodDefinition, TValue value) { - var tokenKey = getTokenKey(methodDefinition); - tokenToValue[tokenKey] = value; - tokenToKey[tokenKey] = methodDefinition; - - var refKey = getReferenceKey(methodDefinition); - if (!refToValue.ContainsKey(refKey) || - getAccessibilityOrder(methodDefinition) < getAccessibilityOrder(refToKey[refKey])) { - refToKey[refKey] = methodDefinition; - refToValue[refKey] = value; - } - } - - // Order: public, family, assembly, private - static int[] accessibilityOrder = new int[8] { - 60, // CompilerControlled - 50, // Private - 40, // FamANDAssem - 30, // Assembly - 10, // Family - 20, // FamORAssem - 0, // Public - 70, // - }; - static int getAccessibilityOrder(MethodDefinition methodDefinition) { - return accessibilityOrder[(int)methodDefinition.Attributes & 7]; - } - - public void onTypesRenamed() { - var newFieldRefToDef = new Dictionary(refToValue.Count); - foreach (var kvp in refToValue) - newFieldRefToDef[getReferenceKey((MethodDefinition)kvp.Key.MethodReference)] = kvp.Value; - refToValue = newFieldRefToDef; - } - } - - public class MethodDefinitionDict : MethodDefinitionDictBase { - protected override IMethodReferenceKey getReferenceKey(MethodReference methodReference) { - return new MethodReferenceKey(methodReference); - } - } - - public class MethodDefinitionAndDeclaringTypeDict : MethodDefinitionDictBase { - protected override IMethodReferenceKey getReferenceKey(MethodReference methodReference) { - return new MethodReferenceAndDeclaringTypeKey(methodReference); - } - } - - public abstract class PropertyDefinitionDictBase { - Dictionary tokenToValue = new Dictionary(); - Dictionary tokenToKey = new Dictionary(); - Dictionary refToValue = new Dictionary(); - - public int Count { - get { return tokenToValue.Count; } - } - - public IEnumerable getKeys() { - return tokenToKey.Values; - } - - public IEnumerable getValues() { - return tokenToValue.Values; - } - - ScopeAndTokenKey getTokenKey(PropertyReference propertyReference) { - return new ScopeAndTokenKey(propertyReference); - } - - protected abstract IPropertyReferenceKey getReferenceKey(PropertyReference propertyReference); - - public TValue find(PropertyReference propertyReference) { - TValue value; - if (propertyReference is PropertyDefinition) - tokenToValue.TryGetValue(getTokenKey(propertyReference), out value); - else - refToValue.TryGetValue(getReferenceKey(propertyReference), out value); - return value; - } - - public TValue findAny(PropertyReference propertyReference) { - TValue value; - if (tokenToValue.TryGetValue(getTokenKey(propertyReference), out value)) - return value; - - refToValue.TryGetValue(getReferenceKey(propertyReference), out value); - return value; - } - - public void add(PropertyDefinition propertyDefinition, TValue value) { - var tokenKey = getTokenKey(propertyDefinition); - tokenToValue[tokenKey] = value; - tokenToKey[tokenKey] = propertyDefinition; - - refToValue[getReferenceKey(propertyDefinition)] = value; - } - - public void onTypesRenamed() { - var newFieldRefToDef = new Dictionary(refToValue.Count); - foreach (var kvp in refToValue) - newFieldRefToDef[getReferenceKey((PropertyDefinition)kvp.Key.PropertyReference)] = kvp.Value; - refToValue = newFieldRefToDef; - } - } - - public class PropertyDefinitionDict : PropertyDefinitionDictBase { - protected override IPropertyReferenceKey getReferenceKey(PropertyReference propertyReference) { - return new PropertyReferenceKey(propertyReference); - } - } - - public class PropertyDefinitionAndDeclaringTypeDict : PropertyDefinitionDictBase { - protected override IPropertyReferenceKey getReferenceKey(PropertyReference propertyReference) { - return new PropertyReferenceAndDeclaringTypeKey(propertyReference); - } - } - - public abstract class EventDefinitionDictBase { - Dictionary tokenToValue = new Dictionary(); - Dictionary tokenToKey = new Dictionary(); - Dictionary refToValue = new Dictionary(); - - public int Count { - get { return tokenToValue.Count; } - } - - public IEnumerable getKeys() { - return tokenToKey.Values; - } - - public IEnumerable getValues() { - return tokenToValue.Values; - } - - ScopeAndTokenKey getTokenKey(EventReference eventReference) { - return new ScopeAndTokenKey(eventReference); - } - - protected abstract IEventReferenceKey getReferenceKey(EventReference eventReference); - - public TValue find(EventReference eventReference) { - TValue value; - if (eventReference is EventDefinition) - tokenToValue.TryGetValue(getTokenKey(eventReference), out value); - else - refToValue.TryGetValue(getReferenceKey(eventReference), out value); - return value; - } - - public TValue findAny(EventReference eventReference) { - TValue value; - if (tokenToValue.TryGetValue(getTokenKey(eventReference), out value)) - return value; - - refToValue.TryGetValue(getReferenceKey(eventReference), out value); - return value; - } - - public void add(EventDefinition eventDefinition, TValue value) { - var tokenKey = getTokenKey(eventDefinition); - tokenToValue[tokenKey] = value; - tokenToKey[tokenKey] = eventDefinition; - - refToValue[getReferenceKey(eventDefinition)] = value; - } - - public void onTypesRenamed() { - var newFieldRefToDef = new Dictionary(refToValue.Count); - foreach (var kvp in refToValue) - newFieldRefToDef[getReferenceKey((EventDefinition)kvp.Key.EventReference)] = kvp.Value; - refToValue = newFieldRefToDef; - } - } - - public class EventDefinitionDict : EventDefinitionDictBase { - protected override IEventReferenceKey getReferenceKey(EventReference eventReference) { - return new EventReferenceKey(eventReference); - } - } - - public class EventDefinitionAndDeclaringTypeDict : EventDefinitionDictBase { - protected override IEventReferenceKey getReferenceKey(EventReference eventReference) { - return new EventReferenceAndDeclaringTypeKey(eventReference); - } - } - - public class ScopeAndTokenKey { - readonly IMetadataScope scope; - readonly int token; - - public ScopeAndTokenKey(TypeReference type) - : this(type.Scope, type.MetadataToken.ToInt32()) { - } - - public ScopeAndTokenKey(FieldReference field) - : this(field.DeclaringType == null ? null : field.DeclaringType.Scope, field.MetadataToken.ToInt32()) { - } - - public ScopeAndTokenKey(MethodReference method) - : this(method.DeclaringType == null ? null : method.DeclaringType.Scope, method.MetadataToken.ToInt32()) { - } - - public ScopeAndTokenKey(PropertyReference prop) - : this(prop.DeclaringType == null ? null : prop.DeclaringType.Scope, prop.MetadataToken.ToInt32()) { - } - - public ScopeAndTokenKey(EventReference evt) - : this(evt.DeclaringType == null ? null : evt.DeclaringType.Scope, evt.MetadataToken.ToInt32()) { - } - - public ScopeAndTokenKey(IMetadataScope scope, int token) { - this.scope = scope; - this.token = token; - } - - public override int GetHashCode() { - return token + MemberReferenceHelper.scopeHashCode(scope); - } - - public override bool Equals(object obj) { - var other = obj as ScopeAndTokenKey; - if (other == null) - return false; - return token == other.token && - MemberReferenceHelper.compareScope(scope, other.scope); - } - - public override string ToString() { - return string.Format("{0:X8} {1}", token, scope); - } - } - - public class TypeReferenceKey { - readonly TypeReference typeRef; - - public TypeReference TypeReference { - get { return typeRef; } - } - - public TypeReferenceKey(TypeReference typeRef) { - this.typeRef = typeRef; - } - - public override int GetHashCode() { - throw new NotImplementedException(); - } - - public override bool Equals(object obj) { - var other = obj as TypeReferenceKey; - if (other == null) - return false; - throw new NotImplementedException(); - } - - public override string ToString() { - return typeRef.ToString(); - } - } - - public class TypeReferenceSameVersionKey { - readonly TypeReference typeRef; - - public TypeReference TypeReference { - get { return typeRef; } - } - - public TypeReferenceSameVersionKey(TypeReference typeRef) { - this.typeRef = typeRef; - } - - public override int GetHashCode() { - throw new NotImplementedException(); - } - - public override bool Equals(object obj) { - var other = obj as TypeReferenceSameVersionKey; - if (other == null) - return false; - throw new NotImplementedException(); - } - - public override string ToString() { - return typeRef.ToString(); - } - } - - public interface IFieldReferenceKey { - FieldReference FieldReference { get; } - } - - public interface IPropertyReferenceKey { - PropertyReference PropertyReference { get; } - } - - public interface IEventReferenceKey { - EventReference EventReference { get; } - } - - public interface IMethodReferenceKey { - MethodReference MethodReference { get; } - } - - public class FieldReferenceKey : IFieldReferenceKey { - readonly FieldReference fieldRef; - - public FieldReference FieldReference { - get { return fieldRef; } - } - - public FieldReferenceKey(FieldReference fieldRef) { - this.fieldRef = fieldRef; - } - - public override int GetHashCode() { - throw new NotImplementedException(); - } - - public override bool Equals(object obj) { - var other = obj as FieldReferenceKey; - if (other == null) - return false; - throw new NotImplementedException(); - } - - public override string ToString() { - return fieldRef.ToString(); - } - } - - public class PropertyReferenceKey : IPropertyReferenceKey { - readonly PropertyReference propRef; - - public PropertyReference PropertyReference { - get { return propRef; } - } - - public PropertyReferenceKey(PropertyReference propRef) { - this.propRef = propRef; - } - - public override int GetHashCode() { - throw new NotImplementedException(); - } - - public override bool Equals(object obj) { - var other = obj as PropertyReferenceKey; - if (other == null) - return false; - throw new NotImplementedException(); - } - - public override string ToString() { - return propRef.ToString(); - } - } - - public class EventReferenceKey : IEventReferenceKey { - readonly EventReference eventRef; - - public EventReference EventReference { - get { return eventRef; } - } - - public EventReferenceKey(EventReference eventRef) { - this.eventRef = eventRef; - } - - public override int GetHashCode() { - throw new NotImplementedException(); - } - - public override bool Equals(object obj) { - var other = obj as EventReferenceKey; - if (other == null) - return false; - throw new NotImplementedException(); - } - - public override string ToString() { - return eventRef.ToString(); - } - } - - public class MethodReferenceKey : IMethodReferenceKey { - readonly MethodReference methodRef; - - public MethodReference MethodReference { - get { return methodRef; } - } - - public MethodReferenceKey(MethodReference methodRef) { - this.methodRef = methodRef; - } - - public override int GetHashCode() { - throw new NotImplementedException(); - } - - public override bool Equals(object obj) { - var other = obj as MethodReferenceKey; - if (other == null) - return false; - throw new NotImplementedException(); - } - - public override string ToString() { - return methodRef.ToString(); - } - } - - public class FieldReferenceAndDeclaringTypeKey : IFieldReferenceKey { - readonly FieldReference fieldRef; - - public FieldReference FieldReference { - get { return fieldRef; } - } - - public FieldReferenceAndDeclaringTypeKey(FieldReference fieldRef) { - this.fieldRef = fieldRef; - } - - public override int GetHashCode() { - throw new NotImplementedException(); - } - - public override bool Equals(object obj) { - var other = obj as FieldReferenceAndDeclaringTypeKey; - if (other == null) - return false; - throw new NotImplementedException(); - } - - public override string ToString() { - return fieldRef.ToString(); - } - } - - public class PropertyReferenceAndDeclaringTypeKey : IPropertyReferenceKey { - readonly PropertyReference propRef; - - public PropertyReference PropertyReference { - get { return propRef; } - } - - public PropertyReferenceAndDeclaringTypeKey(PropertyReference propRef) { - this.propRef = propRef; - } - - public override int GetHashCode() { - throw new NotImplementedException(); - } - - public override bool Equals(object obj) { - var other = obj as PropertyReferenceAndDeclaringTypeKey; - if (other == null) - return false; - throw new NotImplementedException(); - } - - public override string ToString() { - return propRef.ToString(); - } - } - - public class EventReferenceAndDeclaringTypeKey : IEventReferenceKey { - readonly EventReference eventRef; - - public EventReference EventReference { - get { return eventRef; } - } - - public EventReferenceAndDeclaringTypeKey(EventReference eventRef) { - this.eventRef = eventRef; - } - - public override int GetHashCode() { - throw new NotImplementedException(); - } - - public override bool Equals(object obj) { - var other = obj as EventReferenceAndDeclaringTypeKey; - if (other == null) - return false; - throw new NotImplementedException(); - } - - public override string ToString() { - return eventRef.ToString(); - } - } - - public class MethodReferenceAndDeclaringTypeKey : IMethodReferenceKey { - readonly MethodReference methodRef; - - public MethodReference MethodReference { - get { return methodRef; } - } - - public MethodReferenceAndDeclaringTypeKey(MethodReference methodRef) { - this.methodRef = methodRef; - } - - public override int GetHashCode() { - throw new NotImplementedException(); - } - - public override bool Equals(object obj) { - var other = obj as MethodReferenceAndDeclaringTypeKey; - if (other == null) - return false; - throw new NotImplementedException(); - } - - public override string ToString() { - return methodRef.ToString(); - } - } -} - -namespace de4dot.blocks { - public static class MemberReferenceHelper { - public static bool verifyType(TypeReference typeReference, string assembly, string type) { - return verifyType(typeReference, assembly, type, ""); - } - - public static bool verifyType(TypeReference typeReference, string assembly, string type, string extra) { - return typeReference != null && - MemberReferenceHelper.getCanonicalizedTypeRefName(typeReference.GetElementType()) == "[" + assembly + "]" + type && - typeReference.FullName == type + extra; - } - - public static bool isSystemObject(TypeReference typeReference) { - return typeReference != null && typeReference.EType == ElementType.Object; - } - - public static string getCanonicalizedTypeRefName(TypeReference typeRef) { - return getCanonicalizedTypeRefName(typeRef.Scope, typeRef.FullName); - } - - public static string getCanonicalizedTypeRefName(IMetadataScope scope, string fullName) { - return string.Format("[{0}]{1}", getCanonicalizedScopeName(scope), fullName); - } - - public static AssemblyNameReference getAssemblyNameReference(IMetadataScope scope) { - switch (scope.MetadataScopeType) { - case MetadataScopeType.AssemblyNameReference: - return (AssemblyNameReference)scope; - case MetadataScopeType.ModuleDefinition: - var module = (ModuleDefinition)scope; - if (module.Assembly != null) - return module.Assembly.Name; - break; - case MetadataScopeType.ModuleReference: - break; - default: - throw new ApplicationException(string.Format("Invalid scope type: {0}", scope.GetType())); - } - - return null; - } - - public static string getCanonicalizedScopeName(IMetadataScope scope) { - var asmRef = getAssemblyNameReference(scope); - if (asmRef != null) { - // The version number should be ignored. Older code may reference an old version of - // the assembly, but if the newer one has been loaded, that one is used. - return asmRef.Name.ToLowerInvariant(); - } - return string.Format("{0}", scope.ToString().ToLowerInvariant()); - } - - public static string getCanonicalizedScopeAndVersion(IMetadataScope scope) { - var asmRef = getAssemblyNameReference(scope); - if (asmRef != null) - return string.Format("{0}, Version={1}", asmRef.Name.ToLowerInvariant(), asmRef.Version); - return string.Format("{0}, Version=", scope.ToString().ToLowerInvariant()); - } - - public static bool compareScope(IMetadataScope a, IMetadataScope b) { - if (ReferenceEquals(a, b)) - return true; - if (a == null || b == null) - return false; - return getCanonicalizedScopeName(a) == getCanonicalizedScopeName(b); - } - - public static int scopeHashCode(IMetadataScope a) { - if (a == null) - return 0; - return getCanonicalizedScopeName(a).GetHashCode(); - } - - public static bool compareScopeSameVersion(IMetadataScope a, IMetadataScope b) { - if (ReferenceEquals(a, b)) - return true; - if (a == null || b == null) - return false; - return getCanonicalizedScopeAndVersion(a) == getCanonicalizedScopeAndVersion(b); - } - - public static int scopeHashCodeSameVersion(IMetadataScope a) { - if (a == null) - return 0; - return getCanonicalizedScopeAndVersion(a).GetHashCode(); - } - - public static bool compareMethodReference(MethodReference a, MethodReference b) { - throw new NotImplementedException(); - } - - public static bool compareMethodReferenceAndDeclaringType(MethodReference a, MethodReference b) { - throw new NotImplementedException(); - } - - public static bool compareFieldReference(FieldReference a, FieldReference b) { - throw new NotImplementedException(); - } - - public static bool compareTypes(TypeReference a, TypeReference b) { - throw new NotImplementedException(); - } - } -} -#endif diff --git a/blocks/TypeReferenceUpdaterBase.cs b/blocks/TypeReferenceUpdaterBase.cs deleted file mode 100644 index 03e8babd..00000000 --- a/blocks/TypeReferenceUpdaterBase.cs +++ /dev/null @@ -1,92 +0,0 @@ -/* - Copyright (C) 2011-2012 de4dot@gmail.com - - This file is part of de4dot. - - de4dot is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - de4dot is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with de4dot. If not, see . -*/ - -#if PORT -using System; -using Mono.Cecil; - -namespace de4dot.blocks { - public abstract class TypeReferenceUpdaterBase { - public TypeReference update(TypeReference a) { - if (a == null) - return null; - - throw new NotImplementedException(); - } - - protected virtual ArrayType updateArrayType(ArrayType a) { - var rv = new ArrayType(update(a.ElementType)); - if (!a.IsVector) { - foreach (var dim in a.Dimensions) - rv.Dimensions.Add(dim); - } - return rv; - } - - protected virtual ByReferenceType updateByReferenceType(ByReferenceType a) { - return new ByReferenceType(update(a.ElementType)); - } - - protected virtual FunctionPointerType updateFunctionPointerType(FunctionPointerType a) { - var rv = new FunctionPointerType(); - rv.function = a.function; - return rv; - } - - protected virtual GenericInstanceType updateGenericInstanceType(GenericInstanceType a) { - var rv = new GenericInstanceType(update(a.ElementType)); - foreach (var arg in a.GenericArguments) - rv.GenericArguments.Add(update(arg)); - return rv; - } - - protected virtual TypeReference updateGenericParameter(GenericParameter a) { - return a; - } - - protected virtual OptionalModifierType updateOptionalModifierType(OptionalModifierType a) { - return new OptionalModifierType(update(a.ModifierType), update(a.ElementType)); - } - - protected virtual PinnedType updatePinnedType(PinnedType a) { - return new PinnedType(update(a.ElementType)); - } - - protected virtual PointerType updatePointerType(PointerType a) { - return new PointerType(update(a.ElementType)); - } - - protected virtual RequiredModifierType updateRequiredModifierType(RequiredModifierType a) { - return new RequiredModifierType(update(a.ModifierType), update(a.ElementType)); - } - - protected virtual SentinelType updateSentinelType(SentinelType a) { - return new SentinelType(update(a.ElementType)); - } - - protected virtual TypeReference updateTypeDefinition(TypeDefinition a) { - return updateTypeReference(a); - } - - protected virtual TypeReference updateTypeReference(TypeReference a) { - return a; - } - } -} -#endif diff --git a/blocks/blocks.csproj b/blocks/blocks.csproj index 691797a5..1c0f83e4 100644 --- a/blocks/blocks.csproj +++ b/blocks/blocks.csproj @@ -72,8 +72,6 @@ - - @@ -95,7 +93,6 @@ -