/// <summary> /// Allocates a metadata (IL) representation along with a source level representation of the body of a method or of a property/event accessor. /// </summary> /// <param name="ilMethodBody">A method body whose IL operations should be decompiled into a block of statements that will be the /// result of the Block property of the resulting source method body.</param> /// <param name="host">An object representing the application that is hosting the converter. It is used to obtain access to some global /// objects and services such as the shared name table and the table for interning references.</param> /// <param name="sourceLocationProvider">An object that can map some kinds of ILocation objects to IPrimarySourceLocation objects. May be null.</param> /// <param name="localScopeProvider">An object that can provide information about the local scopes of a method.</param> /// <param name="options">Set of options that control decompilation.</param> public SourceMethodBody(IMethodBody ilMethodBody, IMetadataHost host, ISourceLocationProvider /*?*/ sourceLocationProvider, ILocalScopeProvider /*?*/ localScopeProvider, DecompilerOptions options = DecompilerOptions.None) : base(host, sourceLocationProvider, localScopeProvider) { Contract.Requires(ilMethodBody != null); Contract.Requires(host != null); this.ilMethodBody = ilMethodBody; this.host = host; this.nameTable = host.NameTable; this.sourceLocationProvider = sourceLocationProvider; this.pdbReader = sourceLocationProvider as PdbReader; this.localScopeProvider = localScopeProvider; this.options = options; this.platformType = ilMethodBody.MethodDefinition.ContainingTypeDefinition.PlatformType; if (IteratorHelper.EnumerableIsNotEmpty(ilMethodBody.LocalVariables)) { this.LocalsAreZeroed = ilMethodBody.LocalsAreZeroed; } else { this.LocalsAreZeroed = true; } this.MethodDefinition = ilMethodBody.MethodDefinition; this.privateHelperFieldsToRemove = null; this.privateHelperMethodsToRemove = null; this.privateHelperTypesToRemove = null; this.cdfg = ControlAndDataFlowGraph <BasicBlock <Instruction>, Instruction> .GetControlAndDataFlowGraphFor(host, ilMethodBody, localScopeProvider); }
internal InstructionParser(SourceMethodBody sourceMethodBody) { Contract.Requires(sourceMethodBody != null); this.sourceMethodBody = sourceMethodBody; this.host = sourceMethodBody.host; Contract.Assume(this.host != null); this.ilMethodBody = sourceMethodBody.ilMethodBody; Contract.Assume(this.ilMethodBody != null); this.MethodDefinition = sourceMethodBody.MethodDefinition; this.nameTable = sourceMethodBody.nameTable; Contract.Assume(this.nameTable != null); this.sourceLocationProvider = sourceMethodBody.sourceLocationProvider; this.localScopeProvider = sourceMethodBody.localScopeProvider; this.options = sourceMethodBody.options; this.platformType = sourceMethodBody.platformType; Contract.Assume(this.platformType != null); this.numberOfAssignmentsToLocal = sourceMethodBody.numberOfAssignmentsToLocal; Contract.Assume(this.numberOfAssignmentsToLocal != null); this.numberOfReferencesToLocal = sourceMethodBody.numberOfReferencesToLocal; Contract.Assume(this.numberOfReferencesToLocal != null); this.gotosThatTarget = sourceMethodBody.gotosThatTarget; Contract.Assume(this.gotosThatTarget != null); this.cdfg = sourceMethodBody.cdfg; Contract.Assume(this.cdfg != null); this.bindingsThatMakeALastUseOfALocalVersion = sourceMethodBody.bindingsThatMakeALastUseOfALocalVersion; Contract.Assume(this.bindingsThatMakeALastUseOfALocalVersion != null); if (this.localScopeProvider != null) { var syncInfo = this.localScopeProvider.GetSynchronizationInformation(sourceMethodBody); if (syncInfo != null) { var syncPointFor = this.synchronizatonPointLocationFor = new Hashtable<SynchronizationPointLocation>(); IDocument doc = Dummy.Document; foreach (var loc in this.MethodDefinition.Locations) { doc = loc.Document; break; } foreach (var syncPoint in syncInfo.SynchronizationPoints) { Contract.Assume(syncPoint != null); var syncLoc = new SynchronizationPointLocation(doc, syncPoint); syncPointFor[syncPoint.SynchronizeOffset] = syncLoc; if (syncPoint.ContinuationMethod == null) syncPointFor[syncPoint.ContinuationOffset] = syncLoc; } } } }
/// <summary> /// Allocates a mutator that copies metadata models into mutable code models by using the base MetadataMutator class to make a mutable copy /// of a given metadata model and also replaces any method bodies with instances of SourceMethodBody, which implements the ISourceMethodBody.Block property /// by decompiling the metadata model information provided by the properties of IMethodBody. /// </summary> /// <param name="host">An object representing the application that is hosting this mutator. It is used to obtain access to some global /// objects and services such as the shared name table and the table for interning references.</param> /// <param name="unit">The unit of metadata that will be mutated.</param> /// <param name="sourceLocationProvider">An object that can map some kinds of ILocation objects to IPrimarySourceLocation objects. May be null.</param> /// <param name="localScopeProvider">An object that can provide information about the local scopes of a method. May be null.</param> /// <param name="options">Set of options that control decompilation.</param> internal ReplaceMetadataMethodBodiesWithDecompiledMethodBodies(IMetadataHost host, IUnit unit, ISourceLocationProvider /*?*/ sourceLocationProvider, ILocalScopeProvider /*?*/ localScopeProvider, DecompilerOptions options) { this.localScopeProvider = localScopeProvider; this.sourceLocationProvider = sourceLocationProvider; this.host = host; this.options = options; }
public static Assembly GetCodeModelFromMetadataModel(IMetadataHost host, IAssembly assembly, PdbReader/*?*/ pdbReader, DecompilerOptions options = DecompilerOptions.None) { Contract.Requires(host != null); Contract.Requires(assembly != null); Contract.Requires(!(assembly is Dummy)); Contract.Ensures(Contract.Result<Assembly>() != null); return (Assembly)GetCodeModelFromMetadataModelHelper(host, assembly, pdbReader, pdbReader, options); }
public static Module GetCodeModelFromMetadataModel(IMetadataHost host, IModule module, PdbReader/*?*/ pdbReader, DecompilerOptions options = DecompilerOptions.None) { Contract.Requires(host != null); Contract.Requires(module != null); Contract.Requires(!(module is Dummy)); Contract.Ensures(Contract.Result<Module>() != null); return GetCodeModelFromMetadataModelHelper(host, module, pdbReader, pdbReader, options); }
/// <summary> /// Allocates a mutator that copies metadata models into mutable code models by using the base MetadataMutator class to make a mutable copy /// of a given metadata model and also replaces any method bodies with instances of SourceMethodBody, which implements the ISourceMethodBody.Block property /// by decompiling the metadata model information provided by the properties of IMethodBody. /// </summary> /// <param name="host">An object representing the application that is hosting this mutator. It is used to obtain access to some global /// objects and services such as the shared name table and the table for interning references.</param> /// <param name="sourceLocationProvider">An object that can map some kinds of ILocation objects to IPrimarySourceLocation objects. May be null.</param> /// <param name="localScopeProvider">An object that can provide information about the local scopes of a method. May be null.</param> /// <param name="options">Set of options that control decompilation.</param> internal ReplaceMetadataMethodBodiesWithDecompiledMethodBodies(IMetadataHost host, ISourceLocationProvider /*?*/ sourceLocationProvider, ILocalScopeProvider /*?*/ localScopeProvider, DecompilerOptions options) { Contract.Requires(host != null); this.localScopeProvider = localScopeProvider; this.sourceLocationProvider = sourceLocationProvider; this.host = host; this.options = options; }
/// <summary> /// Allocates a metadata (IL) representation along with a source level representation of the body of a method or of a property/event accessor. /// </summary> /// <param name="ilMethodBody">A method body whose IL operations should be decompiled into a block of statements that will be the /// result of the Block property of the resulting source method body.</param> /// <param name="host">An object representing the application that is hosting the converter. It is used to obtain access to some global /// objects and services such as the shared name table and the table for interning references.</param> /// <param name="sourceLocationProvider">An object that can map some kinds of ILocation objects to IPrimarySourceLocation objects. May be null.</param> /// <param name="localScopeProvider">An object that can provide information about the local scopes of a method.</param> /// <param name="options">Set of options that control decompilation.</param> public SourceMethodBody(IMethodBody ilMethodBody, IMetadataHost host, ISourceLocationProvider/*?*/ sourceLocationProvider, ILocalScopeProvider/*?*/ localScopeProvider, DecompilerOptions options = DecompilerOptions.None) : base(host, sourceLocationProvider, localScopeProvider) { Contract.Requires(ilMethodBody != null); Contract.Requires(host != null); this.ilMethodBody = ilMethodBody; this.host = host; this.nameTable = host.NameTable; this.sourceLocationProvider = sourceLocationProvider; this.pdbReader = sourceLocationProvider as PdbReader; this.localScopeProvider = localScopeProvider; this.options = options; this.platformType = ilMethodBody.MethodDefinition.ContainingTypeDefinition.PlatformType; if (IteratorHelper.EnumerableIsNotEmpty(ilMethodBody.LocalVariables)) this.LocalsAreZeroed = ilMethodBody.LocalsAreZeroed; else this.LocalsAreZeroed = true; this.MethodDefinition = ilMethodBody.MethodDefinition; this.privateHelperFieldsToRemove = null; this.privateHelperMethodsToRemove = null; this.privateHelperTypesToRemove = null; this.cdfg = ControlAndDataFlowGraph<BasicBlock<Instruction>, Instruction>.GetControlAndDataFlowGraphFor(host, ilMethodBody, localScopeProvider); }
private static Module GetCodeModelFromMetadataModelHelper(IMetadataHost host, IModule module, ISourceLocationProvider /*?*/ sourceLocationProvider, ILocalScopeProvider /*?*/ localScopeProvider, DecompilerOptions options) { Contract.Requires(host != null); Contract.Requires(module != null); Contract.Requires(!(module is Dummy)); Contract.Ensures(Contract.Result <Module>() != null); var result = new MetadataDeepCopier(host).Copy(module); var replacer = new ReplaceMetadataMethodBodiesWithDecompiledMethodBodies(host, sourceLocationProvider, localScopeProvider, options); replacer.Traverse(result); var finder = new HelperTypeFinder(host, sourceLocationProvider); finder.Traverse(result); Contract.Assume(finder.helperTypes != null); Contract.Assume(finder.helperMethods != null); Contract.Assume(finder.helperFields != null); var remover = new RemoveUnnecessaryTypes(finder.helperTypes, finder.helperMethods, finder.helperFields); remover.Traverse(result); result.AllTypes.RemoveAll(td => finder.helperTypes.ContainsKey(td.InternedKey)); // depends on RemoveAll preserving order return(result); }
/// <summary> /// Returns a (mutable) Code Model SourceMethod body that is equivalent to the given Metadata Model method body. /// It does *not* delete any helper types. /// </summary> /// <param name="host">An object representing the application that is hosting this decompiler. It is used to obtain access to some global /// objects and services such as the shared name table and the table for interning references.</param> /// <param name="methodBody">The Metadata Model method body that is to be decompiled.</param> /// <param name="pdbReader">An object that can map offsets in an IL stream to source locations and block scopes. May be null.</param> /// <param name="options">Set of options that control decompilation.</param> public static ISourceMethodBody GetCodeModelFromMetadataModel(IMetadataHost host, IMethodBody methodBody, PdbReader/*?*/ pdbReader, DecompilerOptions options = DecompilerOptions.None) { return new Microsoft.Cci.ILToCodeModel.SourceMethodBody(methodBody, host, pdbReader, pdbReader, options); }
/// <summary> /// Returns a mutable Code Model module that is equivalent to the given Metadata Model module, /// except that in the new module method bodies also implement ISourceMethodBody. /// </summary> /// <param name="host">An object representing the application that is hosting this decompiler. It is used to obtain access to some global /// objects and services such as the shared name table and the table for interning references.</param> /// <param name="module">The root of the Metadata Model to be converted to a Code Model.</param> /// <param name="pdbReader">An object that can map offsets in an IL stream to source locations and block scopes. May be null.</param> /// <param name="options">Set of options that control decompilation.</param> public static Module GetCodeModelFromMetadataModel(IMetadataHost host, IModule module, PdbReader/*?*/ pdbReader, DecompilerOptions options = DecompilerOptions.None) { return GetCodeModelFromMetadataModelHelper(host, module, pdbReader, pdbReader, options); }
/// <summary> /// Returns a mutable Code Model assembly that is equivalent to the given Metadata Model assembly, /// except that in the new assembly method bodies also implement ISourceMethodBody. /// </summary> /// <param name="host">An object representing the application that is hosting this decompiler. It is used to obtain access to some global /// objects and services such as the shared name table and the table for interning references.</param> /// <param name="assembly">The root of the Metadata Model to be converted to a Code Model.</param> /// <param name="pdbReader">An object that can map offsets in an IL stream to source locations and block scopes. May be null.</param> /// <param name="options">Set of options that control decompilation.</param> public static Assembly GetCodeModelFromMetadataModel(IMetadataHost host, IAssembly assembly, PdbReader/*?*/ pdbReader, DecompilerOptions options = DecompilerOptions.None) { return (Assembly)GetCodeModelFromMetadataModelHelper(host, assembly, pdbReader, pdbReader, options); }
/// <summary> /// Allocates a mutator that copies metadata models into mutable code models by using the base MetadataMutator class to make a mutable copy /// of a given metadata model and also replaces any method bodies with instances of SourceMethodBody, which implements the ISourceMethodBody.Block property /// by decompiling the metadata model information provided by the properties of IMethodBody. /// </summary> /// <param name="host">An object representing the application that is hosting this mutator. It is used to obtain access to some global /// objects and services such as the shared name table and the table for interning references.</param> /// <param name="unit">The unit of metadata that will be mutated.</param> /// <param name="sourceLocationProvider">An object that can map some kinds of ILocation objects to IPrimarySourceLocation objects. May be null.</param> /// <param name="localScopeProvider">An object that can provide information about the local scopes of a method. May be null.</param> /// <param name="options">Set of options that control decompilation.</param> internal ReplaceMetadataMethodBodiesWithDecompiledMethodBodies(IMetadataHost host, IUnit unit, ISourceLocationProvider/*?*/ sourceLocationProvider, ILocalScopeProvider/*?*/ localScopeProvider, DecompilerOptions options) { this.localScopeProvider = localScopeProvider; this.sourceLocationProvider = sourceLocationProvider; this.host = host; this.options = options; }
/// <summary> /// Returns a mutable Code Model module that is equivalent to the given Metadata Model module, /// except that in the new module method bodies also implement ISourceMethodBody. /// </summary> /// <param name="host">An object representing the application that is hosting this decompiler. It is used to obtain access to some global /// objects and services such as the shared name table and the table for interning references.</param> /// <param name="module">The root of the Metadata Model to be converted to a Code Model.</param> /// <param name="sourceLocationProvider">An object that can map some kinds of ILocation objects to IPrimarySourceLocation objects. May be null.</param> /// <param name="localScopeProvider">An object that can provide information about the local scopes of a method. May be null.</param> /// <param name="options">Set of options that control decompilation.</param> private static Module GetCodeModelFromMetadataModelHelper(IMetadataHost host, IModule module, ISourceLocationProvider/*?*/ sourceLocationProvider, ILocalScopeProvider/*?*/ localScopeProvider, DecompilerOptions options) { var result = new MetadataDeepCopier(host).Copy(module); var replacer = new ReplaceMetadataMethodBodiesWithDecompiledMethodBodies(host, module, sourceLocationProvider, localScopeProvider, options); replacer.Traverse(result); var finder = new HelperTypeFinder(host, sourceLocationProvider); finder.Traverse(result); var remover = new RemoveUnnecessaryTypes(finder.helperTypes, finder.helperMethods, finder.helperFields); remover.Traverse(result); result.AllTypes.RemoveAll(td => finder.helperTypes.ContainsKey(td.InternedKey)); // depends on RemoveAll preserving order return result; }
public static Module GetCodeModelFromMetadataModel(IMetadataHost host, IModule module, PdbReader /*?*/ pdbReader, DecompilerOptions options = DecompilerOptions.None) { Contract.Requires(host != null); Contract.Requires(module != null); Contract.Requires(!(module is Dummy)); Contract.Ensures(Contract.Result <Module>() != null); return(GetCodeModelFromMetadataModelHelper(host, module, pdbReader, pdbReader, options)); }
public static Assembly GetCodeModelFromMetadataModel(IMetadataHost host, IAssembly assembly, PdbReader /*?*/ pdbReader, DecompilerOptions options = DecompilerOptions.None) { Contract.Requires(host != null); Contract.Requires(assembly != null); Contract.Requires(!(assembly is Dummy)); Contract.Ensures(Contract.Result <Assembly>() != null); return((Assembly)GetCodeModelFromMetadataModelHelper(host, assembly, pdbReader, pdbReader, options)); }
/// <summary> /// Allocates a metadata (IL) representation along with a source level representation of the body of an iterator method/property/event accessor. /// </summary> /// <param name="iteratorMethodBody"> The method body of the iterator method, to which this MoveNextSourceMethodBody corresponds.</param> /// <param name="ilMethodBody">The method body of MoveNext whose IL operations should be decompiled into a block of statements that will be the /// result of the Block property of the resulting source method body. More importantly, the decompiled body for the original iterator method /// is accessed by the TransformedBlock property.</param> /// <param name="host">An object representing the application that is hosting the converter. It is used to obtain access to some global /// objects and services such as the shared name table and the table for interning references.</param> /// <param name="sourceLocationProvider">An object that can map some kinds of ILocation objects to IPrimarySourceLocation objects. May be null.</param> /// <param name="localScopeProvider">An object that can provide information about the local scopes of a method.</param> /// <param name="options">Set of options that control decompilation.</param> public MoveNextSourceMethodBody(IMethodBody iteratorMethodBody, IMethodBody ilMethodBody, IMetadataHost host, ISourceLocationProvider/*?*/ sourceLocationProvider, ILocalScopeProvider/*?*/ localScopeProvider, DecompilerOptions options = DecompilerOptions.None) : base(ilMethodBody, host, sourceLocationProvider, localScopeProvider, options) { this.iteratorMethodBody = iteratorMethodBody; }
/// <summary> /// Returns a (mutable) Code Model SourceMethod body that is equivalent to the given Metadata Model method body. /// It does *not* delete any helper types. /// </summary> /// <param name="host">An object representing the application that is hosting this decompiler. It is used to obtain access to some global /// objects and services such as the shared name table and the table for interning references.</param> /// <param name="methodBody">The Metadata Model method body that is to be decompiled.</param> /// <param name="pdbReader">An object that can map offsets in an IL stream to source locations and block scopes. May be null.</param> /// <param name="options">Set of options that control decompilation.</param> public static ISourceMethodBody GetCodeModelFromMetadataModel(IMetadataHost host, IMethodBody methodBody, PdbReader /*?*/ pdbReader, DecompilerOptions options = DecompilerOptions.None) { Contract.Requires(host != null); Contract.Requires(methodBody != null); return(new Microsoft.Cci.ILToCodeModel.SourceMethodBody(methodBody, host, pdbReader, pdbReader, options)); }
/// <summary> /// Returns a mutable Code Model assembly that is equivalent to the given Metadata Model assembly, /// except that in the new assembly method bodies also implement ISourceMethodBody. /// </summary> /// <param name="host">An object representing the application that is hosting this decompiler. It is used to obtain access to some global /// objects and services such as the shared name table and the table for interning references.</param> /// <param name="assembly">The root of the Metadata Model to be converted to a Code Model.</param> /// <param name="pdbReader">An object that can map offsets in an IL stream to source locations and block scopes. May be null.</param> /// <param name="options">Set of options that control decompilation.</param> public static Assembly GetCodeModelFromMetadataModel(IMetadataHost host, IAssembly assembly, PdbReader /*?*/ pdbReader, DecompilerOptions options = DecompilerOptions.None) { return((Assembly)GetCodeModelFromMetadataModelHelper(host, assembly, pdbReader, pdbReader, options)); }
/// <summary> /// Returns a mutable Code Model module that is equivalent to the given Metadata Model module, /// except that in the new module method bodies also implement ISourceMethodBody. /// </summary> /// <param name="host">An object representing the application that is hosting this decompiler. It is used to obtain access to some global /// objects and services such as the shared name table and the table for interning references.</param> /// <param name="module">The root of the Metadata Model to be converted to a Code Model.</param> /// <param name="pdbReader">An object that can map offsets in an IL stream to source locations and block scopes. May be null.</param> /// <param name="options">Set of options that control decompilation.</param> public static Module GetCodeModelFromMetadataModel(IMetadataHost host, IModule module, PdbReader /*?*/ pdbReader, DecompilerOptions options = DecompilerOptions.None) { return(GetCodeModelFromMetadataModelHelper(host, module, pdbReader, pdbReader, options)); }
/// <summary> /// Allocates a metadata (IL) representation along with a source level representation of the body of a method or of a property/event accessor. /// </summary> /// <param name="ilMethodBody">A method body whose IL operations should be decompiled into a block of statements that will be the /// result of the Block property of the resulting source method body.</param> /// <param name="host">An object representing the application that is hosting the converter. It is used to obtain access to some global /// objects and services such as the shared name table and the table for interning references.</param> /// <param name="sourceLocationProvider">An object that can map some kinds of ILocation objects to IPrimarySourceLocation objects. May be null.</param> /// <param name="localScopeProvider">An object that can provide information about the local scopes of a method.</param> /// <param name="options">Set of options that control decompilation.</param> public SourceMethodBody(IMethodBody ilMethodBody, IMetadataHost host, ISourceLocationProvider/*?*/ sourceLocationProvider, ILocalScopeProvider/*?*/ localScopeProvider, DecompilerOptions options = DecompilerOptions.None) : base(host, sourceLocationProvider) { this.ilMethodBody = ilMethodBody; this.host = host; this.nameTable = host.NameTable; this.sourceLocationProvider = sourceLocationProvider; this.pdbReader = sourceLocationProvider as PdbReader; this.localScopeProvider = localScopeProvider; this.options = options; this.platformType = ilMethodBody.MethodDefinition.ContainingTypeDefinition.PlatformType; this.operationEnumerator = ilMethodBody.Operations.GetEnumerator(); if (IteratorHelper.EnumerableIsNotEmpty(ilMethodBody.LocalVariables)) this.LocalsAreZeroed = ilMethodBody.LocalsAreZeroed; else this.LocalsAreZeroed = true; this.MethodDefinition = ilMethodBody.MethodDefinition; }
/// <summary> /// Allocates a mutator that copies metadata models into mutable code models by using the base MetadataMutator class to make a mutable copy /// of a given metadata model and also replaces any method bodies with instances of SourceMethodBody, which implements the ISourceMethodBody.Block property /// by decompiling the metadata model information provided by the properties of IMethodBody. /// </summary> /// <param name="host">An object representing the application that is hosting this mutator. It is used to obtain access to some global /// objects and services such as the shared name table and the table for interning references.</param> /// <param name="sourceLocationProvider">An object that can map some kinds of ILocation objects to IPrimarySourceLocation objects. May be null.</param> /// <param name="localScopeProvider">An object that can provide information about the local scopes of a method. May be null.</param> /// <param name="options">Set of options that control decompilation.</param> internal ReplaceMetadataMethodBodiesWithDecompiledMethodBodies(IMetadataHost host, ISourceLocationProvider/*?*/ sourceLocationProvider, ILocalScopeProvider/*?*/ localScopeProvider, DecompilerOptions options) { Contract.Requires(host != null); this.localScopeProvider = localScopeProvider; this.sourceLocationProvider = sourceLocationProvider; this.host = host; this.options = options; }