Browse Source

#nullable enable for ILReader

pull/2993/head
Daniel Grunwald 3 years ago
parent
commit
72a895f64f
  1. 57
      ICSharpCode.Decompiler/IL/ILReader.cs

57
ICSharpCode.Decompiler/IL/ILReader.cs

@ -16,6 +16,8 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
#nullable enable
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
@ -49,12 +51,12 @@ namespace ICSharpCode.Decompiler.IL
readonly MetadataReader metadata; readonly MetadataReader metadata;
public bool UseDebugSymbols { get; set; } public bool UseDebugSymbols { get; set; }
public DebugInfo.IDebugInfoProvider DebugInfo { get; set; } public DebugInfo.IDebugInfoProvider? DebugInfo { get; set; }
public List<string> Warnings { get; } = new List<string>(); public List<string> Warnings { get; } = new List<string>();
// List of candidate locations for sequence points. Includes empty il stack locations, any nop instructions, and the instruction following // List of candidate locations for sequence points. Includes empty il stack locations, any nop instructions, and the instruction following
// a call instruction. // a call instruction.
public List<int> SequencePointCandidates { get; private set; } public List<int> SequencePointCandidates { get; } = new List<int>();
/// <summary> /// <summary>
/// Creates a new ILReader instance. /// Creates a new ILReader instance.
@ -69,29 +71,29 @@ namespace ICSharpCode.Decompiler.IL
this.module = module; this.module = module;
this.compilation = module.Compilation; this.compilation = module.Compilation;
this.metadata = module.metadata; this.metadata = module.metadata;
this.SequencePointCandidates = new List<int>();
} }
// The members initialized with `null!` are initialized in the Init method.
GenericContext genericContext; GenericContext genericContext;
IMethod method; IMethod method = null!;
MethodBodyBlock body; MethodBodyBlock body = null!;
StackType methodReturnStackType; StackType methodReturnStackType;
BlobReader reader; BlobReader reader;
ImmutableStack<ILVariable> currentStack; ImmutableStack<ILVariable> currentStack = ImmutableStack<ILVariable>.Empty;
List<ILInstruction> expressionStack; List<ILInstruction> expressionStack = new List<ILInstruction>();
ILVariable[] parameterVariables; ILVariable[] parameterVariables = null!;
ILVariable[] localVariables; ILVariable[] localVariables = null!;
BitSet isBranchTarget; BitSet isBranchTarget = null!;
BlockContainer mainContainer; BlockContainer mainContainer = null!;
List<ILInstruction> instructionBuilder; List<ILInstruction> instructionBuilder = new List<ILInstruction>();
int currentInstructionStart; int currentInstructionStart;
// Dictionary that stores stacks for each IL instruction // Dictionary that stores stacks for each IL instruction
Dictionary<int, ImmutableStack<ILVariable>> stackByOffset; Dictionary<int, ImmutableStack<ILVariable>> stackByOffset = new Dictionary<int, ImmutableStack<ILVariable>>();
Dictionary<ExceptionRegion, ILVariable> variableByExceptionHandler; Dictionary<ExceptionRegion, ILVariable> variableByExceptionHandler = new Dictionary<ExceptionRegion, ILVariable>();
UnionFind<ILVariable> unionFind; UnionFind<ILVariable> unionFind = null!;
List<(ILVariable, ILVariable)> stackMismatchPairs; List<(ILVariable, ILVariable)> stackMismatchPairs = new List<(ILVariable, ILVariable)>();
IEnumerable<ILVariable> stackVariables; IEnumerable<ILVariable>? stackVariables;
void Init(MethodDefinitionHandle methodDefinitionHandle, MethodBodyBlock body, GenericContext genericContext) void Init(MethodDefinitionHandle methodDefinitionHandle, MethodBodyBlock body, GenericContext genericContext)
{ {
@ -114,9 +116,9 @@ namespace ICSharpCode.Decompiler.IL
this.body = body; this.body = body;
this.reader = body.GetILReader(); this.reader = body.GetILReader();
this.currentStack = ImmutableStack<ILVariable>.Empty; this.currentStack = ImmutableStack<ILVariable>.Empty;
this.expressionStack = new List<ILInstruction>(); this.expressionStack.Clear();
this.unionFind = new UnionFind<ILVariable>(); this.unionFind = new UnionFind<ILVariable>();
this.stackMismatchPairs = new List<(ILVariable, ILVariable)>(); this.stackMismatchPairs.Clear();
this.methodReturnStackType = method.ReturnType.GetStackType(); this.methodReturnStackType = method.ReturnType.GetStackType();
InitParameterVariables(); InitParameterVariables();
localVariables = InitLocalVariables(); localVariables = InitLocalVariables();
@ -126,10 +128,10 @@ namespace ICSharpCode.Decompiler.IL
v.UsesInitialValue = true; v.UsesInitialValue = true;
} }
this.mainContainer = new BlockContainer(expectedResultType: methodReturnStackType); this.mainContainer = new BlockContainer(expectedResultType: methodReturnStackType);
this.instructionBuilder = new List<ILInstruction>(); this.instructionBuilder.Clear();
this.isBranchTarget = new BitSet(reader.Length); this.isBranchTarget = new BitSet(reader.Length);
this.stackByOffset = new Dictionary<int, ImmutableStack<ILVariable>>(); this.stackByOffset.Clear();
this.variableByExceptionHandler = new Dictionary<ExceptionRegion, ILVariable>(); this.variableByExceptionHandler.Clear();
} }
EntityHandle ReadAndDecodeMetadataToken() EntityHandle ReadAndDecodeMetadataToken()
@ -256,7 +258,7 @@ namespace ICSharpCode.Decompiler.IL
ILVariable CreateILVariable(int index, IType parameterType, string name) ILVariable CreateILVariable(int index, IType parameterType, string name)
{ {
Debug.Assert(!parameterType.IsUnbound()); Debug.Assert(!parameterType.IsUnbound());
ITypeDefinition def = parameterType.GetDefinition(); ITypeDefinition? def = parameterType.GetDefinition();
if (def != null && index < 0 && def.IsReferenceType == false) if (def != null && index < 0 && def.IsReferenceType == false)
{ {
parameterType = new ByReferenceType(parameterType); parameterType = new ByReferenceType(parameterType);
@ -443,7 +445,11 @@ namespace ICSharpCode.Decompiler.IL
if (inst.HasDirectFlag(InstructionFlags.EndPointUnreachable)) if (inst.HasDirectFlag(InstructionFlags.EndPointUnreachable))
{ {
FlushExpressionStack(); FlushExpressionStack();
if (!stackByOffset.TryGetValue(end, out currentStack)) if (stackByOffset.TryGetValue(end, out var stackAfterEnd))
{
currentStack = stackAfterEnd;
}
else
{ {
currentStack = ImmutableStack<ILVariable>.Empty; currentStack = ImmutableStack<ILVariable>.Empty;
} }
@ -634,6 +640,7 @@ namespace ICSharpCode.Decompiler.IL
var function = new ILFunction(this.method, body.GetCodeSize(), this.genericContext, mainContainer, kind); var function = new ILFunction(this.method, body.GetCodeSize(), this.genericContext, mainContainer, kind);
function.Variables.AddRange(parameterVariables); function.Variables.AddRange(parameterVariables);
function.Variables.AddRange(localVariables); function.Variables.AddRange(localVariables);
Debug.Assert(stackVariables != null);
function.Variables.AddRange(stackVariables); function.Variables.AddRange(stackVariables);
function.Variables.AddRange(variableByExceptionHandler.Values); function.Variables.AddRange(variableByExceptionHandler.Values);
function.Variables.AddRange(blockBuilder.OnErrorDispatcherVariables); function.Variables.AddRange(blockBuilder.OnErrorDispatcherVariables);
@ -1553,7 +1560,7 @@ namespace ICSharpCode.Decompiler.IL
return new StObj(target, value, type); return new StObj(target, value, type);
} }
IType constrainedPrefix; IType? constrainedPrefix;
private DecodedInstruction DecodeConstrainedCall() private DecodedInstruction DecodeConstrainedCall()
{ {

Loading…
Cancel
Save