From 3c5f1355cb570b3546dc65d76d98dcf8f0e720fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Srbeck=C3=BD?= Date: Tue, 12 Apr 2011 01:59:46 +0100 Subject: [PATCH] Handle uninitialized variables. Closes #102. Closes #116. --- ICSharpCode.Decompiler/ILAst/ILAstBuilder.cs | 21 +++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/ICSharpCode.Decompiler/ILAst/ILAstBuilder.cs b/ICSharpCode.Decompiler/ILAst/ILAstBuilder.cs index aa702ccc2..6d0dafab5 100644 --- a/ICSharpCode.Decompiler/ILAst/ILAstBuilder.cs +++ b/ICSharpCode.Decompiler/ILAst/ILAstBuilder.cs @@ -534,11 +534,30 @@ namespace ICSharpCode.Decompiler.ILAst Loads = new List() }).ToList(); + // VB.NET uses the 'init' to allow use of uninitialized variables. + // We do not really care about them too much - if the original variable + // was uninitialized at that point it means that no store was called and + // thus all our new variables must be uninitialized as well. + // So it does not matter which one we load. + + // TODO: We should add explicit initialization so that C# code compiles. + // Remember to handle cases where one path inits the variable, but other does not. + // Add loads to the data structure; merge variables if necessary foreach(ByteCode load in loads) { ByteCode[] storedBy = load.VariablesBefore[variableIndex].StoredBy; if (storedBy.Length == 0) { - throw new Exception("Load of uninitialized variable"); + // Load which always loads the default ('uninitialized') value + // Create a dummy variable just for this load + newVars.Add(new VariableInfo() { + Variable = new ILVariable() { + Name = "var_" + variableIndex + "_" + load.Offset.ToString("X2") + "_default", + Type = varType, + OriginalVariable = methodDef.Body.Variables[variableIndex] + }, + Stores = new List(), + Loads = new List() { load } + }); } else if (storedBy.Length == 1) { VariableInfo newVar = newVars.Where(v => v.Stores.Contains(storedBy[0])).Single(); newVar.Loads.Add(load);