diff --git a/ICSharpCode.Decompiler/IL/ILReader.cs b/ICSharpCode.Decompiler/IL/ILReader.cs index 4aac9ecb4..4a5f9c589 100644 --- a/ICSharpCode.Decompiler/IL/ILReader.cs +++ b/ICSharpCode.Decompiler/IL/ILReader.cs @@ -50,7 +50,6 @@ namespace ICSharpCode.Decompiler.IL } MetadataReader metadata; - Metadata.MethodDefinition methodDefinition; IMethod method; MethodBodyBlock body; Metadata.IDebugInfoProvider debugInfo; @@ -73,14 +72,13 @@ namespace ICSharpCode.Decompiler.IL List<(ILVariable, ILVariable)> stackMismatchPairs; IEnumerable stackVariables; - void Init(Metadata.MethodDefinition method, MethodBodyBlock body) + void Init(Metadata.PEFile module, MethodDefinitionHandle methodDefinitionHandle, MethodBodyBlock body) { if (body == null) throw new ArgumentNullException(nameof(body)); - this.metadata = method.Module.GetMetadataReader(); - this.methodDefinition = method; - this.method = typeSystem.ResolveAsMethod(method.Handle); - this.methodSignature = metadata.GetMethodDefinition(method.Handle).DecodeSignature(new TypeSystem.Implementation.TypeReferenceSignatureDecoder(), default); + this.metadata = module.GetMetadataReader(); + this.method = typeSystem.ResolveAsMethod(methodDefinitionHandle); + this.methodSignature = metadata.GetMethodDefinition(methodDefinitionHandle).DecodeSignature(new TypeSystem.Implementation.TypeReferenceSignatureDecoder(), default); this.body = body; this.reader = body.GetILReader(); //this.debugInfo = metadata.GetMethodDebugInformation(method.Handle.ToDebugInformationHandle()); @@ -157,7 +155,7 @@ namespace ICSharpCode.Decompiler.IL { VariableKind kind = IsPinned(type) ? VariableKind.PinnedLocal : VariableKind.Local; ILVariable ilVar = new ILVariable(kind, type.Resolve(resolveContext), index); - if (!UseDebugSymbols || debugInfo == null || !debugInfo.TryGetName(methodDefinition, index, out string name)) { + if (!UseDebugSymbols || debugInfo == null || !debugInfo.TryGetName((MethodDefinitionHandle)method.MetadataToken, index, out string name)) { ilVar.Name = "V_" + index; ilVar.HasGeneratedName = true; } else if (string.IsNullOrWhiteSpace(name)) { @@ -387,9 +385,9 @@ namespace ICSharpCode.Decompiler.IL /// /// Debugging helper: writes the decoded instruction stream interleaved with the inferred evaluation stack layout. /// - public void WriteTypedIL(Metadata.MethodDefinition method, MethodBodyBlock body, ITextOutput output, CancellationToken cancellationToken = default(CancellationToken)) + public void WriteTypedIL(Metadata.PEFile module, MethodDefinitionHandle method, MethodBodyBlock body, ITextOutput output, CancellationToken cancellationToken = default(CancellationToken)) { - Init(body); + Init(module, method, body); ReadInstructions(cancellationToken); foreach (var inst in instructionBuilder) { if (inst is StLoc stloc && stloc.IsStackAdjustment) { @@ -421,21 +419,21 @@ namespace ICSharpCode.Decompiler.IL output.WriteLine(); } new Disassembler.MethodBodyDisassembler(output, cancellationToken) { DetectControlStructure = false } - .WriteExceptionHandlers(method, body); + .WriteExceptionHandlers(new Metadata.MethodDefinition(module, method), body); } /// /// Decodes the specified method body and returns an ILFunction. /// - public ILFunction ReadIL(Metadata.MethodDefinition method, MethodBodyBlock body, CancellationToken cancellationToken = default(CancellationToken)) + public ILFunction ReadIL(Metadata.PEFile module, MethodDefinitionHandle method, MethodBodyBlock body, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); - Init(method, body); + Init(module, method, body); ReadInstructions(cancellationToken); var blockBuilder = new BlockBuilder(body, typeSystem, variableByExceptionHandler); blockBuilder.CreateBlocks(mainContainer, instructionBuilder, isBranchTarget, cancellationToken); - var resolvedMethod = typeSystem.Resolve(method); - var function = new ILFunction(resolvedMethod, method, mainContainer); + var resolvedMethod = typeSystem.ResolveAsMethod(method); + var function = new ILFunction(resolvedMethod, body.GetCodeSize(), mainContainer); CollectionExtensions.AddRange(function.Variables, parameterVariables); CollectionExtensions.AddRange(function.Variables, localVariables); CollectionExtensions.AddRange(function.Variables, stackVariables); diff --git a/ICSharpCode.Decompiler/TypeSystem/DecompilerTypeSystem.cs b/ICSharpCode.Decompiler/TypeSystem/DecompilerTypeSystem.cs index 2a8467a7e..10c8ef5fb 100644 --- a/ICSharpCode.Decompiler/TypeSystem/DecompilerTypeSystem.cs +++ b/ICSharpCode.Decompiler/TypeSystem/DecompilerTypeSystem.cs @@ -89,6 +89,8 @@ namespace ICSharpCode.Decompiler.TypeSystem get { return moduleDefinition; } } + public SRM.MetadataReader GetMetadata() => moduleDefinition.GetMetadataReader(); + void StoreMemberReference(IUnresolvedEntity entity, SRM.EntityHandle mr) { // This is a callback from the type system, which is multi-threaded and may be accessed externally diff --git a/ICSharpCode.Decompiler/TypeSystem/IDecompilerTypeSystem.cs b/ICSharpCode.Decompiler/TypeSystem/IDecompilerTypeSystem.cs index cb1415a48..5eb330c72 100644 --- a/ICSharpCode.Decompiler/TypeSystem/IDecompilerTypeSystem.cs +++ b/ICSharpCode.Decompiler/TypeSystem/IDecompilerTypeSystem.cs @@ -32,6 +32,8 @@ namespace ICSharpCode.Decompiler.TypeSystem IMethod ResolveAsMethod(EntityHandle methodReference); IMember ResolveAsMember(EntityHandle memberReference); + MetadataReader GetMetadata(); + /// /// Gets a type system instance that automatically specializes the results /// of each Resolve() call with the provided substitution. diff --git a/ICSharpCode.Decompiler/TypeSystem/MetadataLoader.cs b/ICSharpCode.Decompiler/TypeSystem/MetadataLoader.cs index 70fc97b2f..e32a4dac7 100644 --- a/ICSharpCode.Decompiler/TypeSystem/MetadataLoader.cs +++ b/ICSharpCode.Decompiler/TypeSystem/MetadataLoader.cs @@ -279,7 +279,7 @@ namespace ICSharpCode.Decompiler.TypeSystem /// a type system type reference. /// Attributes associated with the Cecil type reference. /// This is used to support the 'dynamic' type. - public ITypeReference ReadTypeReference(EntityHandle type, SRM.CustomAttributeHandleCollection typeAttributes = default(CustomAttributeHandleCollection)) + public ITypeReference ReadTypeReference(EntityHandle type, CustomAttributeHandleCollection typeAttributes = default(CustomAttributeHandleCollection)) { ITypeReference CreateTypeReference(TypeReferenceHandle handle) { @@ -1689,7 +1689,6 @@ namespace ICSharpCode.Decompiler.TypeSystem { OnEntityLoaded?.Invoke(typeSystemObject, cecilObject); } - } #endregion } }