private void SetOverrides(AnalysisNet.Types.MethodDefinition methodDefinition, Cecil.MethodDefinition methodDef) { ISet <AnalysisNet.Types.MethodImplementation> impls = methodDefinition.ContainingType.ExplicitOverrides; IEnumerable <AnalysisNet.Types.MethodImplementation> matchedImpls = impls.Where(impl => methodDefinition.MatchReference(impl.ImplementingMethod)); methodDef.Overrides.AddRange(matchedImpls.Select(impl => ReferenceGenerator.MethodReference(impl.ImplementedMethod))); }
public Cecil.MethodDefinition MethodDefinition(AnalysisNet.Types.MethodDefinition methodDefinition) { Cecil.MethodDefinition cecilMethodDefinition = new Cecil.MethodDefinition(methodDefinition.Name, 0, Context.CurrentModule.TypeSystem.Void); GenerateMethodAttributes(methodDefinition, cecilMethodDefinition); cecilMethodDefinition.CreateGenericParameters(methodDefinition.GenericParameters.Count); Cecil.TypeReference returnType = ReferenceGenerator.TypeReference(methodDefinition.ReturnType); cecilMethodDefinition.ReturnType = returnType; AddConstraintsToGenericParameters(methodDefinition, cecilMethodDefinition); Cecil.TypeReference typeRef = ReferenceGenerator.TypeReference(methodDefinition.ContainingType); Cecil.TypeDefinition containingType = typeRef.Resolve(); cecilMethodDefinition.DeclaringType = containingType as Cecil.TypeDefinition; SetOverrides(methodDefinition, cecilMethodDefinition); SetCustomAttributes(methodDefinition.Attributes, cecilMethodDefinition.CustomAttributes); IDictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.ParameterDefinition> parameterDefinitions = CreateParameters(methodDefinition, cecilMethodDefinition); if (methodDefinition.HasBody) { cecilMethodDefinition.Body.MaxStackSize = methodDefinition.Body.MaxStack; cecilMethodDefinition.Body.InitLocals = methodDefinition.Body.LocalVariables.Count > 0; IDictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.Cil.VariableDefinition> variableDefinitions = CreateLocalVariables(methodDefinition, cecilMethodDefinition); InstructionGenerator instructionGenerator = new InstructionGenerator(ReferenceGenerator); // analysis-net instruction -> [cecil instruction] IDictionary <AnalysisNet.Bytecode.Instruction, IList <Cecil.Cil.Instruction> > mapInstructions = instructionGenerator.CreateInstructions(methodDefinition, cecilMethodDefinition, variableDefinitions, parameterDefinitions); CreateExceptionHandlers(mapInstructions, methodDefinition.Body, cecilMethodDefinition.Body); } return(cecilMethodDefinition); }
private void ExtractMethods(AnalysisNet.Types.TypeDefinition containingType, IList <AnalysisNet.Types.MethodDefinition> dest, IEnumerable <Cecil.MethodDefinition> source) { foreach (Cecil.MethodDefinition methoddef in source) { string name = methoddef.Name; AnalysisNet.Types.MethodDefinition method = new AnalysisNet.Types.MethodDefinition(name, null); ExtractCustomAttributes(method.Attributes, methoddef.CustomAttributes); ExtractGenericMethodParameters(method, methoddef); ExtractParameters(method.Parameters, methoddef.Parameters); method.ReturnType = ExtractType(methoddef.ReturnType); if (methoddef.HasBody) { CodeProvider codeProvider = new CodeProvider(this); method.Body = codeProvider.ExtractBody(methoddef.Body); } method.Visibility = ExtractVisibilityKind(methoddef); method.IsStatic = methoddef.IsStatic; method.IsAbstract = methoddef.IsAbstract; method.IsVirtual = methoddef.IsVirtual; method.IsOverrider = (methoddef.IsAbstract || methoddef.IsVirtual) && !methoddef.IsNewSlot; method.IsFinal = methoddef.IsFinal; method.IsConstructor = methoddef.IsConstructor; method.IsExternal = methoddef.IsPInvokeImpl; method.ContainingType = containingType; dest.Add(method); } }
public BytecodeTranslator(AnalysisNet.Types.MethodDefinition methodDefinition, IDictionary <AnalysisNetTac.Values.IVariable, Cecil.Cil.VariableDefinition> variableDefinitions, IDictionary <AnalysisNetTac.Values.IVariable, Cecil.ParameterDefinition> parameterDefinitions, ReferenceGenerator referenceGenerator, Cecil.Cil.ILProcessor processor) { this.methodDefinition = methodDefinition; this.processor = processor; this.variableDefinitions = variableDefinitions; this.parameterDefinitions = parameterDefinitions; this.referenceGenerator = referenceGenerator; }
private void ExtractGenericMethodParameters(AnalysisNet.Types.MethodDefinition method, Cecil.MethodDefinition methoddef) { foreach (Cecil.GenericParameter cecilParam in methoddef.GenericParameters) { AnalysisNet.Types.GenericParameter analysisNetParam = new AnalysisNet.Types.GenericParameter(AnalysisNet.Types.GenericParameterKind.Method, (ushort)cecilParam.Position, cecilParam.Name, GetGenericParameterTypeKind(cecilParam)) { GenericContainer = method }; method.GenericParameters.Add(analysisNetParam); ExtractCustomAttributes(analysisNetParam.Attributes, cecilParam.CustomAttributes); IEnumerable <AnalysisNet.Types.IType> constraints = cecilParam.Constraints.Select(cecilConst => ExtractType(cecilConst.ConstraintType)); analysisNetParam.Constraints.AddRange(constraints); } }
private void GenerateMethodAttributes(AnalysisNet.Types.MethodDefinition methodDefinition, Cecil.MethodDefinition cecilMethodDefinition) { cecilMethodDefinition.IsPublic = true; if (methodDefinition.IsStatic) { cecilMethodDefinition.IsStatic = true; cecilMethodDefinition.HasThis = false; } else { cecilMethodDefinition.HasThis = true; } if (methodDefinition.IsAbstract) { cecilMethodDefinition.IsAbstract = true; } if (methodDefinition.IsVirtual) { cecilMethodDefinition.IsVirtual = true; cecilMethodDefinition.IsHideBySig = true; if (methodDefinition.IsOverrider) { cecilMethodDefinition.IsReuseSlot = true; } else { cecilMethodDefinition.IsNewSlot = true; } } if (methodDefinition.IsFinal) { cecilMethodDefinition.IsFinal = true; } // analysis-net does not flag static constructors bool isStaticCtor = methodDefinition.Name.Equals(".cctor") && methodDefinition.IsStatic; if (methodDefinition.IsConstructor || isStaticCtor) { cecilMethodDefinition.IsHideBySig = true; cecilMethodDefinition.IsSpecialName = true; cecilMethodDefinition.IsRuntimeSpecialName = true; } if (methodDefinition.ContainingType.Kind == AnalysisNet.Types.TypeDefinitionKind.Delegate) { cecilMethodDefinition.IsRuntime = true; } // hack for properties if (methodDefinition.Name.StartsWith("get_") || methodDefinition.Name.StartsWith("set_")) { cecilMethodDefinition.IsSpecialName = true; cecilMethodDefinition.IsHideBySig = true; } }
private IDictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.Cil.VariableDefinition> CreateLocalVariables(Model.Types.MethodDefinition methodDefinition, Mono.Cecil.MethodDefinition methodDef) { IDictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.Cil.VariableDefinition> variableDefinitions = new Dictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.Cil.VariableDefinition>(); foreach (AnalysisNet.ThreeAddressCode.Values.IVariable localVariable in methodDefinition.Body.LocalVariables) { Cecil.Cil.VariableDefinition varDef = new Cecil.Cil.VariableDefinition(ReferenceGenerator.TypeReference(localVariable.Type)); methodDef.Body.Variables.Add(varDef); variableDefinitions[localVariable] = varDef; } return(variableDefinitions); }
public IDictionary <Model.Bytecode.Instruction, IList <Mono.Cecil.Cil.Instruction> > CreateInstructions(Model.Types.MethodDefinition methodDefinition, Mono.Cecil.MethodDefinition methodDef, IDictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.Cil.VariableDefinition> variableDefinitions, IDictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.ParameterDefinition> parameterDefinitions) { Cecil.Cil.ILProcessor ilProcessor = methodDef.Body.GetILProcessor(); BytecodeTranslator translator = new BytecodeTranslator(methodDefinition, variableDefinitions, parameterDefinitions, ReferenceGenerator, ilProcessor); // analysis net instruction -> [cecil instructions] IDictionary <AnalysisNet.Bytecode.Instruction, IList <Cecil.Cil.Instruction> > mappingTranslatedInstructions = translator.Translate(); IEnumerable <Cecil.Cil.Instruction> instructions = mappingTranslatedInstructions.Values.SelectMany(l => l); foreach (Mono.Cecil.Cil.Instruction ins in instructions) { ilProcessor.Append(ins); } return(mappingTranslatedInstructions); }
private IDictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.ParameterDefinition> CreateParameters(AnalysisNet.Types.MethodDefinition methodDefinition, Mono.Cecil.MethodDefinition methodDef) { IDictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.ParameterDefinition> parameterDefinitions = new Dictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.ParameterDefinition>(); for (int idx = 0; idx < methodDefinition.Parameters.Count; idx++) { AnalysisNet.Types.MethodParameter methodParameter = methodDefinition.Parameters.ElementAt(idx); //if (methodParameter.Name.Equals("this")) // continue; Cecil.ParameterDefinition paramDef = new Cecil.ParameterDefinition(ReferenceGenerator.TypeReference(methodParameter.Type)); if (methodParameter.DefaultValue != null) { paramDef.Constant = methodParameter.DefaultValue.Value; paramDef.HasDefault = true; } if (methodParameter.Kind == AnalysisNet.Types.MethodParameterKind.In) { paramDef.IsIn = true; } else if (methodParameter.Kind == AnalysisNet.Types.MethodParameterKind.Out) { paramDef.IsOut = true; } methodDef.Parameters.Add(paramDef); // map body parameters to cecil parameters if (methodDefinition.HasBody && methodDefinition.Body.Parameters.Count > 0) { // body parameters contain 'this' while analysis-net's parameters do not int localIdx = (methodDefinition.IsStatic ? 0 : 1) + idx; AnalysisNet.ThreeAddressCode.Values.IVariable localVariable = methodDefinition.Body.Parameters.ElementAt(localIdx); parameterDefinitions[localVariable] = paramDef; } } return(parameterDefinitions); }