Skip to content

Commit 03aecf0

Browse files
Add VariableScope and rework AssignVariableNames step to support renaming parameters of nested ILFunctions in the future.
1 parent a599aae commit 03aecf0

File tree

9 files changed

+438
-282
lines changed

9 files changed

+438
-282
lines changed

ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,10 @@ public static void NameConflict()
377377
public static void NameConflict2(int j)
378378
{
379379
List<Action<int>> list = new List<Action<int>>();
380-
for (int k = 0; k < 10; k++)
380+
for (int i = 0; i < 10; i++)
381381
{
382-
list.Add(delegate (int i) {
383-
Console.WriteLine(i);
382+
list.Add(delegate (int k) {
383+
Console.WriteLine(k);
384384
});
385385
}
386386
}

ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,15 +2547,18 @@ IEnumerable<ParameterDeclaration> MakeParameters(IReadOnlyList<IParameter> param
25472547
foreach (var parameter in parameters)
25482548
{
25492549
var pd = astBuilder.ConvertParameter(parameter);
2550+
if (variables.TryGetValue(i, out var v))
2551+
{
2552+
pd.AddAnnotation(new ILVariableResolveResult(v, parameters[i].Type));
2553+
pd.Name = v.Name;
2554+
}
25502555
if (string.IsNullOrEmpty(pd.Name) && !pd.Type.IsArgList())
25512556
{
25522557
// needs to be consistent with logic in ILReader.CreateILVarable(ParameterDefinition)
25532558
pd.Name = "P_" + i;
25542559
}
25552560
if (settings.AnonymousTypes && parameter.Type.ContainsAnonymousType())
25562561
pd.Type = null;
2557-
if (variables.TryGetValue(i, out var v))
2558-
pd.AddAnnotation(new ILVariableResolveResult(v, parameters[i].Type));
25592562
yield return pd;
25602563
i++;
25612564
}

ICSharpCode.Decompiler/CSharp/StatementBuilder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,16 @@ LocalFunctionDeclarationStatement TranslateFunction(ILFunction function)
13891389
var astBuilder = exprBuilder.astBuilder;
13901390
var method = (MethodDeclaration)astBuilder.ConvertEntity(function.ReducedMethod);
13911391

1392+
var variables = function.Variables.Where(v => v.Kind == VariableKind.Parameter).ToDictionary(v => v.Index);
1393+
1394+
foreach (var (i, p) in method.Parameters.WithIndex())
1395+
{
1396+
if (variables.TryGetValue(i, out var v))
1397+
{
1398+
p.Name = v.Name;
1399+
}
1400+
}
1401+
13921402
if (function.Method.HasBody)
13931403
{
13941404
var nestedBuilder = new StatementBuilder(

ICSharpCode.Decompiler/IL/ControlFlow/AsyncAwaitDecompiler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,7 @@ void InlineBodyOfMoveNext(ILFunction function)
11651165
function.MoveNextMethod = moveNextFunction.Method;
11661166
function.SequencePointCandidates = moveNextFunction.SequencePointCandidates;
11671167
function.CodeSize = moveNextFunction.CodeSize;
1168+
function.LocalVariableSignatureLength = moveNextFunction.LocalVariableSignatureLength;
11681169
function.IsIterator = IsAsyncEnumerator;
11691170
moveNextFunction.Variables.Clear();
11701171
moveNextFunction.ReleaseRef();

ICSharpCode.Decompiler/IL/ControlFlow/YieldReturnDecompiler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ BlockContainer AnalyzeMoveNext(ILFunction function)
656656
function.MoveNextMethod = moveNextFunction.Method;
657657
function.SequencePointCandidates = moveNextFunction.SequencePointCandidates;
658658
function.CodeSize = moveNextFunction.CodeSize;
659+
function.LocalVariableSignatureLength = moveNextFunction.LocalVariableSignatureLength;
659660

660661
// Copy-propagate temporaries holding a copy of 'this'.
661662
// This is necessary because the old (pre-Roslyn) C# compiler likes to store 'this' in temporary variables.

ICSharpCode.Decompiler/IL/ILReader.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,10 @@ ILVariable CreateILVariable(int index, IType parameterType, string name)
342342
if (index < 0)
343343
ilVar.Name = "this";
344344
else if (string.IsNullOrWhiteSpace(name))
345+
{
345346
ilVar.Name = "P_" + index;
347+
ilVar.HasGeneratedName = true;
348+
}
346349
else
347350
ilVar.Name = name;
348351
return ilVar;
@@ -706,6 +709,7 @@ public ILFunction ReadIL(MethodDefinitionHandle method, MethodBodyBlock body, Ge
706709
var function = new ILFunction(this.method, body.GetCodeSize(), this.genericContext, mainContainer, kind);
707710
function.Variables.AddRange(parameterVariables);
708711
function.Variables.AddRange(localVariables);
712+
function.LocalVariableSignatureLength = localVariables.Length;
709713
Debug.Assert(stackVariables != null);
710714
function.Variables.AddRange(stackVariables);
711715
function.Variables.AddRange(variableByExceptionHandler.Values);

ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ partial class ILFunction
6767
/// </summary>
6868
public readonly ILVariableCollection Variables;
6969

70+
/// <summary>
71+
/// Gets
72+
/// </summary>
73+
public int LocalVariableSignatureLength;
74+
7075
/// <summary>
7176
/// Gets the scope in which the local function is declared.
7277
/// Returns null, if this is not a local function.

0 commit comments

Comments
 (0)