Browse Source

Add VariableScope and rework AssignVariableNames step to support renaming parameters of nested ILFunctions in the future.

pull/3416/head
Siegfried Pammer 4 months ago
parent
commit
03aecf047d
  1. 6
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.cs
  2. 7
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  3. 10
      ICSharpCode.Decompiler/CSharp/StatementBuilder.cs
  4. 1
      ICSharpCode.Decompiler/IL/ControlFlow/AsyncAwaitDecompiler.cs
  5. 1
      ICSharpCode.Decompiler/IL/ControlFlow/YieldReturnDecompiler.cs
  6. 4
      ICSharpCode.Decompiler/IL/ILReader.cs
  7. 5
      ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs
  8. 480
      ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs
  9. 2
      ICSharpCode.Decompiler/Util/CollectionExtensions.cs

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

@ -377,10 +377,10 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction
public static void NameConflict2(int j) public static void NameConflict2(int j)
{ {
List<Action<int>> list = new List<Action<int>>(); List<Action<int>> list = new List<Action<int>>();
for (int k = 0; k < 10; k++) for (int i = 0; i < 10; i++)
{ {
list.Add(delegate (int i) { list.Add(delegate (int k) {
Console.WriteLine(i); Console.WriteLine(k);
}); });
} }
} }

7
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -2547,6 +2547,11 @@ namespace ICSharpCode.Decompiler.CSharp
foreach (var parameter in parameters) foreach (var parameter in parameters)
{ {
var pd = astBuilder.ConvertParameter(parameter); var pd = astBuilder.ConvertParameter(parameter);
if (variables.TryGetValue(i, out var v))
{
pd.AddAnnotation(new ILVariableResolveResult(v, parameters[i].Type));
pd.Name = v.Name;
}
if (string.IsNullOrEmpty(pd.Name) && !pd.Type.IsArgList()) if (string.IsNullOrEmpty(pd.Name) && !pd.Type.IsArgList())
{ {
// needs to be consistent with logic in ILReader.CreateILVarable(ParameterDefinition) // needs to be consistent with logic in ILReader.CreateILVarable(ParameterDefinition)
@ -2554,8 +2559,6 @@ namespace ICSharpCode.Decompiler.CSharp
} }
if (settings.AnonymousTypes && parameter.Type.ContainsAnonymousType()) if (settings.AnonymousTypes && parameter.Type.ContainsAnonymousType())
pd.Type = null; pd.Type = null;
if (variables.TryGetValue(i, out var v))
pd.AddAnnotation(new ILVariableResolveResult(v, parameters[i].Type));
yield return pd; yield return pd;
i++; i++;
} }

10
ICSharpCode.Decompiler/CSharp/StatementBuilder.cs

@ -1389,6 +1389,16 @@ namespace ICSharpCode.Decompiler.CSharp
var astBuilder = exprBuilder.astBuilder; var astBuilder = exprBuilder.astBuilder;
var method = (MethodDeclaration)astBuilder.ConvertEntity(function.ReducedMethod); var method = (MethodDeclaration)astBuilder.ConvertEntity(function.ReducedMethod);
var variables = function.Variables.Where(v => v.Kind == VariableKind.Parameter).ToDictionary(v => v.Index);
foreach (var (i, p) in method.Parameters.WithIndex())
{
if (variables.TryGetValue(i, out var v))
{
p.Name = v.Name;
}
}
if (function.Method.HasBody) if (function.Method.HasBody)
{ {
var nestedBuilder = new StatementBuilder( var nestedBuilder = new StatementBuilder(

1
ICSharpCode.Decompiler/IL/ControlFlow/AsyncAwaitDecompiler.cs

@ -1165,6 +1165,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
function.MoveNextMethod = moveNextFunction.Method; function.MoveNextMethod = moveNextFunction.Method;
function.SequencePointCandidates = moveNextFunction.SequencePointCandidates; function.SequencePointCandidates = moveNextFunction.SequencePointCandidates;
function.CodeSize = moveNextFunction.CodeSize; function.CodeSize = moveNextFunction.CodeSize;
function.LocalVariableSignatureLength = moveNextFunction.LocalVariableSignatureLength;
function.IsIterator = IsAsyncEnumerator; function.IsIterator = IsAsyncEnumerator;
moveNextFunction.Variables.Clear(); moveNextFunction.Variables.Clear();
moveNextFunction.ReleaseRef(); moveNextFunction.ReleaseRef();

1
ICSharpCode.Decompiler/IL/ControlFlow/YieldReturnDecompiler.cs

@ -656,6 +656,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
function.MoveNextMethod = moveNextFunction.Method; function.MoveNextMethod = moveNextFunction.Method;
function.SequencePointCandidates = moveNextFunction.SequencePointCandidates; function.SequencePointCandidates = moveNextFunction.SequencePointCandidates;
function.CodeSize = moveNextFunction.CodeSize; function.CodeSize = moveNextFunction.CodeSize;
function.LocalVariableSignatureLength = moveNextFunction.LocalVariableSignatureLength;
// Copy-propagate temporaries holding a copy of 'this'. // Copy-propagate temporaries holding a copy of 'this'.
// This is necessary because the old (pre-Roslyn) C# compiler likes to store 'this' in temporary variables. // This is necessary because the old (pre-Roslyn) C# compiler likes to store 'this' in temporary variables.

4
ICSharpCode.Decompiler/IL/ILReader.cs

@ -342,7 +342,10 @@ namespace ICSharpCode.Decompiler.IL
if (index < 0) if (index < 0)
ilVar.Name = "this"; ilVar.Name = "this";
else if (string.IsNullOrWhiteSpace(name)) else if (string.IsNullOrWhiteSpace(name))
{
ilVar.Name = "P_" + index; ilVar.Name = "P_" + index;
ilVar.HasGeneratedName = true;
}
else else
ilVar.Name = name; ilVar.Name = name;
return ilVar; return ilVar;
@ -706,6 +709,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);
function.LocalVariableSignatureLength = localVariables.Length;
Debug.Assert(stackVariables != null); Debug.Assert(stackVariables != null);
function.Variables.AddRange(stackVariables); function.Variables.AddRange(stackVariables);
function.Variables.AddRange(variableByExceptionHandler.Values); function.Variables.AddRange(variableByExceptionHandler.Values);

5
ICSharpCode.Decompiler/IL/Instructions/ILFunction.cs

@ -67,6 +67,11 @@ namespace ICSharpCode.Decompiler.IL
/// </summary> /// </summary>
public readonly ILVariableCollection Variables; public readonly ILVariableCollection Variables;
/// <summary>
/// Gets
/// </summary>
public int LocalVariableSignatureLength;
/// <summary> /// <summary>
/// Gets the scope in which the local function is declared. /// Gets the scope in which the local function is declared.
/// Returns null, if this is not a local function. /// Returns null, if this is not a local function.

480
ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs

@ -18,6 +18,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
@ -25,6 +26,7 @@ using System.Reflection.Metadata;
using Humanizer.Inflections; using Humanizer.Inflections;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.CSharp.OutputVisitor; using ICSharpCode.Decompiler.CSharp.OutputVisitor;
using ICSharpCode.Decompiler.TypeSystem; using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.TypeSystem.Implementation; using ICSharpCode.Decompiler.TypeSystem.Implementation;
@ -32,7 +34,7 @@ using ICSharpCode.Decompiler.Util;
namespace ICSharpCode.Decompiler.IL.Transforms namespace ICSharpCode.Decompiler.IL.Transforms
{ {
public class AssignVariableNames : IILTransform public class AssignVariableNames : ILVisitor<AssignVariableNames.VariableScope, Unit>, IILTransform
{ {
static readonly Dictionary<string, string> typeNameToVariableNameDict = new Dictionary<string, string> { static readonly Dictionary<string, string> typeNameToVariableNameDict = new Dictionary<string, string> {
{ "System.Boolean", "flag" }, { "System.Boolean", "flag" },
@ -53,52 +55,83 @@ namespace ICSharpCode.Decompiler.IL.Transforms
}; };
ILTransformContext context; ILTransformContext context;
List<string> currentLowerCaseTypeOrMemberNames; const char maxLoopVariableName = 'n';
public class VariableScope
{
readonly ILTransformContext context;
readonly VariableScope parentScope;
readonly ILFunction function;
readonly Dictionary<MethodDefinitionHandle, string> localFunctions = new();
readonly Dictionary<ILVariable, string> variableMapping = new(ILVariableEqualityComparer.Instance);
readonly string[] assignedLocalSignatureIndices;
IImmutableSet<string> currentLowerCaseTypeOrMemberNames;
Dictionary<string, int> reservedVariableNames; Dictionary<string, int> reservedVariableNames;
HashSet<ILVariable> loopCounters; HashSet<ILVariable> loopCounters;
const char maxLoopVariableName = 'n';
int numDisplayClassLocals; int numDisplayClassLocals;
public void Run(ILFunction function, ILTransformContext context) public VariableScope(ILFunction function, ILTransformContext context, VariableScope parentScope = null)
{ {
this.function = function;
this.context = context; this.context = context;
this.parentScope = parentScope;
numDisplayClassLocals = 0;
assignedLocalSignatureIndices = new string[function.LocalVariableSignatureLength];
reservedVariableNames = new Dictionary<string, int>(); reservedVariableNames = new Dictionary<string, int>();
currentLowerCaseTypeOrMemberNames = new List<string>();
var currentLowerCaseMemberNames = CollectAllLowerCaseMemberNames(function.Method.DeclaringTypeDefinition); // find all loop counters in the current function
foreach (var name in currentLowerCaseMemberNames) loopCounters = new HashSet<ILVariable>();
foreach (var inst in TreeTraversal.PreOrder((ILInstruction)function, i => i.Children))
{
if (inst is ILFunction && inst != function)
break;
if (inst is BlockContainer { Kind: ContainerKind.For, Blocks: [.., var incrementBlock] })
{
foreach (var i in incrementBlock.Instructions)
{
if (HighLevelLoopTransform.MatchIncrement(i, out var variable))
loopCounters.Add(variable);
}
}
}
// if this is the root scope, we also collect all lower-case type and member names
// and fixed parameter names to avoid conflicts when naming local variables.
if (parentScope == null)
{
var currentLowerCaseTypeOrMemberNames = new HashSet<string>(StringComparer.Ordinal);
foreach (var name in CollectAllLowerCaseMemberNames(function.Method.DeclaringTypeDefinition))
currentLowerCaseTypeOrMemberNames.Add(name); currentLowerCaseTypeOrMemberNames.Add(name);
var currentLowerCaseTypeNames = CollectAllLowerCaseTypeNames(function.Method.DeclaringTypeDefinition); foreach (var name in CollectAllLowerCaseTypeNames(function.Method.DeclaringTypeDefinition))
foreach (var name in currentLowerCaseTypeNames)
{ {
currentLowerCaseTypeOrMemberNames.Add(name); currentLowerCaseTypeOrMemberNames.Add(name);
AddExistingName(reservedVariableNames, name); AddExistingName(reservedVariableNames, name);
} }
loopCounters = CollectLoopCounters(function); this.currentLowerCaseTypeOrMemberNames = currentLowerCaseTypeOrMemberNames.ToImmutableHashSet();
foreach (var f in function.Descendants.OfType<ILFunction>())
{ // handle implicit parameters of set or event accessors
if (f.Method != null) if (function.Method != null && IsSetOrEventAccessor(function.Method) && function.Parameters.Count > 0)
{
if (IsSetOrEventAccessor(f.Method) && f.Method.Parameters.Count > 0)
{ {
for (int i = 0; i < f.Method.Parameters.Count - 1; i++) for (int i = 0; i < function.Method.Parameters.Count - 1; i++)
{ {
AddExistingName(reservedVariableNames, f.Method.Parameters[i].Name); AddExistingName(reservedVariableNames, function.Method.Parameters[i].Name);
} }
var lastParameter = f.Method.Parameters.Last(); var lastParameter = function.Method.Parameters.Last();
switch (f.Method.AccessorOwner) switch (function.Method.AccessorOwner)
{ {
case IProperty prop: case IProperty prop:
if (f.Method.AccessorKind == MethodSemanticsAttributes.Setter) if (function.Method.AccessorKind == MethodSemanticsAttributes.Setter)
{ {
if (prop.Parameters.Any(p => p.Name == "value")) if (prop.Parameters.Any(p => p.Name == "value"))
{ {
f.Warnings.Add("Parameter named \"value\" already present in property signature!"); function.Warnings.Add("Parameter named \"value\" already present in property signature!");
break; break;
} }
var variableForLastParameter = f.Variables.FirstOrDefault(v => v.Function == f var variableForLastParameter = function.Variables.FirstOrDefault(v => v.Function == function
&& v.Kind == VariableKind.Parameter && v.Kind == VariableKind.Parameter
&& v.Index == f.Method.Parameters.Count - 1); && v.Index == function.Method.Parameters.Count - 1);
if (variableForLastParameter == null) if (variableForLastParameter == null)
{ {
AddExistingName(reservedVariableNames, lastParameter.Name); AddExistingName(reservedVariableNames, lastParameter.Name);
@ -114,11 +147,11 @@ namespace ICSharpCode.Decompiler.IL.Transforms
} }
break; break;
case IEvent ev: case IEvent ev:
if (f.Method.AccessorKind != MethodSemanticsAttributes.Raiser) if (function.Method.AccessorKind != MethodSemanticsAttributes.Raiser)
{ {
var variableForLastParameter = f.Variables.FirstOrDefault(v => v.Function == f var variableForLastParameter = function.Variables.FirstOrDefault(v => v.Function == function
&& v.Kind == VariableKind.Parameter && v.Kind == VariableKind.Parameter
&& v.Index == f.Method.Parameters.Count - 1); && v.Index == function.Method.Parameters.Count - 1);
if (variableForLastParameter == null) if (variableForLastParameter == null)
{ {
AddExistingName(reservedVariableNames, lastParameter.Name); AddExistingName(reservedVariableNames, lastParameter.Name);
@ -140,42 +173,22 @@ namespace ICSharpCode.Decompiler.IL.Transforms
} }
else else
{ {
foreach (var p in f.Method.Parameters) var variables = function.Variables.Where(v => v.Kind == VariableKind.Parameter && v.Index >= 0).ToDictionary(v => v.Index);
AddExistingName(reservedVariableNames, p.Name); foreach (var (i, p) in function.Parameters.WithIndex())
}
}
else
{ {
foreach (var p in f.Variables.Where(v => v.Kind == VariableKind.Parameter)) string name = p.Name;
AddExistingName(reservedVariableNames, p.Name); if (string.IsNullOrWhiteSpace(name) && p.Type != SpecialType.ArgList)
}
}
numDisplayClassLocals = 0;
PerformAssignment(function);
}
static IEnumerable<string> CollectAllLowerCaseMemberNames(ITypeDefinition type)
{ {
foreach (var item in type.GetMembers(m => IsLowerCase(m.Name))) // needs to be consistent with logic in ILReader.CreateILVarable
yield return item.Name; name = "P_" + i;
} }
if (variables.TryGetValue(i, out var v))
static IEnumerable<string> CollectAllLowerCaseTypeNames(ITypeDefinition type) variableMapping[v] = name;
{ AddExistingName(reservedVariableNames, name);
var ns = type.ParentModule.Compilation.GetNamespaceByFullName(type.Namespace);
foreach (var item in ns.Types)
{
if (IsLowerCase(item.Name))
yield return item.Name;
} }
} }
static bool IsLowerCase(string name) static bool IsSetOrEventAccessor(IMethod method)
{
return name.Length > 0 && char.ToLower(name[0]) == name[0];
}
bool IsSetOrEventAccessor(IMethod method)
{ {
switch (method.AccessorKind) switch (method.AccessorKind)
{ {
@ -187,95 +200,94 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return false; return false;
} }
} }
}
void PerformAssignment(ILFunction function) else
{ {
var localFunctionMapping = new Dictionary<MethodDefinitionHandle, string>(); this.currentLowerCaseTypeOrMemberNames = parentScope.currentLowerCaseTypeOrMemberNames;
var variableMapping = new Dictionary<ILVariable, string>(ILVariableEqualityComparer.Instance); var variables = function.Variables.Where(v => v.Kind == VariableKind.Parameter).ToDictionary(v => v.Index);
var assignedLocalSignatureIndices = new Dictionary<(ILFunction, int), string>();
foreach (var inst in function.Descendants) foreach (var (i, p) in function.Parameters.WithIndex())
{
if (inst is ILFunction { Kind: ILFunctionKind.LocalFunction } localFunction)
{ {
// assign names to local functions if (function.Kind is ILFunctionKind.Delegate or ILFunctionKind.ExpressionTree
if (!LocalFunctionDecompiler.ParseLocalFunctionName(localFunction.Name, out _, out var newName) || !IsValidName(newName)) && CSharpDecompiler.IsTransparentIdentifier(p.Name))
newName = null;
if (newName == null)
{
string nameWithoutNumber = "f";
if (!reservedVariableNames.TryGetValue(nameWithoutNumber, out int currentIndex))
{ {
currentIndex = 1; AddExistingName(reservedVariableNames, p.Name);
if (variables.TryGetValue(i, out var v))
variableMapping[v] = p.Name;
} }
int count = Math.Max(1, currentIndex) + 1; if (!parentScope.IsReservedVariableName(p.Name, out _))
reservedVariableNames[nameWithoutNumber] = count;
if (count > 1)
{ {
newName = nameWithoutNumber + count.ToString(); AddExistingName(reservedVariableNames, p.Name);
if (variables.TryGetValue(i, out var v))
variableMapping[v] = p.Name;
} }
else
{
newName = nameWithoutNumber;
} }
} }
localFunction.Name = newName;
localFunction.ReducedMethod.Name = newName;
localFunctionMapping[(MethodDefinitionHandle)localFunction.ReducedMethod.MetadataToken] = newName;
} }
else if (inst is IInstructionWithVariableOperand i)
public void Add(MethodDefinitionHandle localFunction, string name)
{ {
var v = i.Variable; this.localFunctions[localFunction] = name;
// if there is already a valid name for the variable slot, just use it }
if (variableMapping.TryGetValue(v, out string name))
public string TryGetExistingName(MethodDefinitionHandle localFunction)
{ {
v.Name = name; if (localFunctions.TryGetValue(localFunction, out var name))
continue; return name;
return parentScope?.TryGetExistingName(localFunction);
} }
switch (v.Kind)
public string TryGetExistingName(ILVariable v)
{ {
case VariableKind.Parameter: if (variableMapping.TryGetValue(v, out var name))
// Parameter names are handled in ILReader.CreateILVariable return name;
// and CSharpDecompiler.FixParameterNames return parentScope?.TryGetExistingName(v);
break; }
case VariableKind.InitializerTarget: // keep generated names
AddExistingName(reservedVariableNames, v.Name); public string TryGetExistingName(ILFunction function, int index)
break;
case VariableKind.DisplayClassLocal:
v.Name = "CS$<>8__locals" + (numDisplayClassLocals++);
break;
case VariableKind.Local when v.Index != null:
if (assignedLocalSignatureIndices.TryGetValue((v.Function, v.Index.Value), out name))
{ {
// make sure all local ILVariables that refer to the same slot in the locals signature if (this.function == function)
// are assigned the same name. {
v.Name = name; return this.assignedLocalSignatureIndices[index];
} }
else else
{ {
v.Name = AssignName(v, variableMapping); return parentScope?.TryGetExistingName(function, index);
// Remember the newly assigned name:
assignedLocalSignatureIndices.Add((v.Function, v.Index.Value), v.Name);
} }
break;
default:
v.Name = AssignName(v, variableMapping);
break;
} }
}
else if (inst is (Call or LdFtn) and IInstructionWithMethodOperand m) public void AssignNameToLocalSignatureIndex(ILFunction function, int index, string name)
{ {
// update references to local functions var scope = this;
if (m.Method is LocalFunctionMethod lf while (scope != null && scope.function != function)
&& localFunctionMapping.TryGetValue((MethodDefinitionHandle)lf.MetadataToken, out var name)) scope = scope.parentScope;
Debug.Assert(scope != null);
scope.assignedLocalSignatureIndices[index] = name;
}
public bool IsReservedVariableName(string name, out int index)
{ {
lf.Name = name; if (reservedVariableNames.TryGetValue(name, out index))
return true;
return parentScope?.IsReservedVariableName(name, out index) ?? false;
} }
public void ReserveVariableName(string name, int index = 1)
{
reservedVariableNames[name] = index;
} }
public string NextDisplayClassLocal()
{
return parentScope?.NextDisplayClassLocal() ?? "CS$<>8__locals" + (numDisplayClassLocals++);
} }
public bool IsLoopCounter(ILVariable v)
{
return loopCounters.Contains(v) || (parentScope?.IsLoopCounter(v) == true);
} }
string AssignName(ILVariable v, Dictionary<ILVariable, string> variableMapping) public string AssignName(ILVariable v)
{ {
// variable has no valid name // variable has no valid name
string newName = v.Name; string newName = v.Name;
@ -287,9 +299,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms
} }
// use the existing name and update index appended to future conflicts // use the existing name and update index appended to future conflicts
string nameWithoutNumber = SplitName(newName, out int newIndex); string nameWithoutNumber = SplitName(newName, out int newIndex);
if (reservedVariableNames.TryGetValue(nameWithoutNumber, out int lastUsedIndex)) if (IsReservedVariableName(nameWithoutNumber, out int lastUsedIndex))
{ {
if (v.Type.IsKnownType(KnownTypeCode.Int32) && loopCounters.Contains(v)) if (v.Type.IsKnownType(KnownTypeCode.Int32) && IsLoopCounter(v))
{ {
// special case for loop counters, // special case for loop counters,
// we don't want them to be named i, i2, ..., but i, j, ... // we don't want them to be named i, i2, ..., but i, j, ...
@ -298,7 +310,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
newIndex = 1; newIndex = 1;
} }
} }
if (reservedVariableNames.TryGetValue(nameWithoutNumber, out lastUsedIndex)) if (IsReservedVariableName(nameWithoutNumber, out lastUsedIndex))
{ {
// name without number was already used // name without number was already used
if (newIndex > lastUsedIndex) if (newIndex > lastUsedIndex)
@ -314,61 +326,11 @@ namespace ICSharpCode.Decompiler.IL.Transforms
newName = nameWithoutNumber + newIndex.ToString(); newName = nameWithoutNumber + newIndex.ToString();
} }
// update the last used index // update the last used index
reservedVariableNames[nameWithoutNumber] = newIndex; ReserveVariableName(nameWithoutNumber, newIndex);
variableMapping.Add(v, newName); variableMapping.Add(v, newName);
return newName; return newName;
} }
/// <remarks>
/// Must be in sync with <see cref="GetNameFromInstruction" />.
/// </remarks>
internal static bool IsSupportedInstruction(object arg)
{
switch (arg)
{
case GetPinnableReference _:
case LdObj _:
case LdFlda _:
case LdsFlda _:
case CallInstruction _:
return true;
default:
return false;
}
}
internal static bool IsValidName(string varName)
{
if (string.IsNullOrWhiteSpace(varName))
return false;
if (!(char.IsLetter(varName[0]) || varName[0] == '_'))
return false;
for (int i = 1; i < varName.Length; i++)
{
if (!(char.IsLetterOrDigit(varName[i]) || varName[i] == '_'))
return false;
}
return true;
}
HashSet<ILVariable> CollectLoopCounters(ILFunction function)
{
var loopCounters = new HashSet<ILVariable>();
foreach (BlockContainer possibleLoop in function.Descendants.OfType<BlockContainer>())
{
if (possibleLoop.Kind != ContainerKind.For)
continue;
foreach (var inst in possibleLoop.Blocks.Last().Instructions)
{
if (HighLevelLoopTransform.MatchIncrement(inst, out var variable))
loopCounters.Add(variable);
}
}
return loopCounters;
}
string GenerateNameForVariable(ILVariable variable) string GenerateNameForVariable(ILVariable variable)
{ {
string proposedName = null; string proposedName = null;
@ -380,7 +342,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
// For loop variables, use i,j,k,l,m,n // For loop variables, use i,j,k,l,m,n
for (char c = 'i'; c <= maxLoopVariableName; c++) for (char c = 'i'; c <= maxLoopVariableName; c++)
{ {
if (!reservedVariableNames.ContainsKey(c.ToString())) if (!IsReservedVariableName(c.ToString(), out _))
{ {
proposedName = c.ToString(); proposedName = c.ToString();
break; break;
@ -462,6 +424,176 @@ namespace ICSharpCode.Decompiler.IL.Transforms
// for generated names remove number-suffixes // for generated names remove number-suffixes
return SplitName(proposedName, out _); return SplitName(proposedName, out _);
} }
}
public void Run(ILFunction function, ILTransformContext context)
{
this.context = context;
function.AcceptVisitor(this, null);
}
protected override Unit Default(ILInstruction inst, VariableScope context)
{
foreach (var child in inst.Children)
{
child.AcceptVisitor(this, context);
}
if (inst is not IInstructionWithVariableOperand { Variable: var v })
return default;
// if there is already a valid name for the variable slot, just use it
string name = context.TryGetExistingName(v);
if (!string.IsNullOrEmpty(name))
{
v.Name = name;
return default;
}
switch (v.Kind)
{
case VariableKind.Parameter when !v.HasGeneratedName && v.Function.Kind == ILFunctionKind.TopLevelFunction:
// Parameter names of top-level functions are handled in ILReader.CreateILVariable
// and CSharpDecompiler.FixParameterNames
break;
case VariableKind.InitializerTarget: // keep generated names
case VariableKind.NamedArgument:
context.ReserveVariableName(v.Name);
break;
case VariableKind.DisplayClassLocal:
v.Name = context.NextDisplayClassLocal();
break;
case VariableKind.Local when v.Index != null:
name = context.TryGetExistingName(v.Function, v.Index.Value);
if (name != null)
{
// make sure all local ILVariables that refer to the same slot in the locals signature
// are assigned the same name.
v.Name = name;
}
else
{
v.Name = context.AssignName(v);
context.AssignNameToLocalSignatureIndex(v.Function, v.Index.Value, v.Name);
}
break;
default:
v.Name = context.AssignName(v);
break;
}
return default;
}
protected internal override Unit VisitILFunction(ILFunction function, VariableScope context)
{
if (function.Kind == ILFunctionKind.LocalFunction)
{
// assign names to local functions
if (!LocalFunctionDecompiler.ParseLocalFunctionName(function.Name, out _, out var newName) || !IsValidName(newName))
newName = null;
if (newName == null)
{
string nameWithoutNumber = "f";
if (!context.IsReservedVariableName(nameWithoutNumber, out int currentIndex))
{
currentIndex = 1;
}
int count = Math.Max(1, currentIndex) + 1;
context.ReserveVariableName(nameWithoutNumber, count);
if (count > 1)
{
newName = nameWithoutNumber + count.ToString();
}
else
{
newName = nameWithoutNumber;
}
}
function.Name = newName;
function.ReducedMethod.Name = newName;
context.Add((MethodDefinitionHandle)function.ReducedMethod.MetadataToken, newName);
}
return base.VisitILFunction(function, new VariableScope(function, this.context, context));
}
protected internal override Unit VisitCall(Call inst, VariableScope context)
{
if (inst.Method is LocalFunctionMethod m)
{
string name = context.TryGetExistingName((MethodDefinitionHandle)m.MetadataToken);
if (!string.IsNullOrEmpty(name))
m.Name = name;
}
return base.VisitCall(inst, context);
}
protected internal override Unit VisitLdFtn(LdFtn inst, VariableScope context)
{
if (inst.Method is LocalFunctionMethod m)
{
string name = context.TryGetExistingName((MethodDefinitionHandle)m.MetadataToken);
if (!string.IsNullOrEmpty(name))
m.Name = name;
}
return base.VisitLdFtn(inst, context);
}
static IEnumerable<string> CollectAllLowerCaseMemberNames(ITypeDefinition type)
{
foreach (var item in type.GetMembers(m => IsLowerCase(m.Name)))
yield return item.Name;
}
static IEnumerable<string> CollectAllLowerCaseTypeNames(ITypeDefinition type)
{
var ns = type.ParentModule.Compilation.GetNamespaceByFullName(type.Namespace);
foreach (var item in ns.Types)
{
if (IsLowerCase(item.Name))
yield return item.Name;
}
}
static bool IsLowerCase(string name)
{
return name.Length > 0 && char.ToLower(name[0]) == name[0];
}
/// <remarks>
/// Must be in sync with <see cref="GetNameFromInstruction" />.
/// </remarks>
internal static bool IsSupportedInstruction(object arg)
{
switch (arg)
{
case GetPinnableReference _:
case LdObj _:
case LdFlda _:
case LdsFlda _:
case CallInstruction _:
return true;
default:
return false;
}
}
internal static bool IsValidName(string varName)
{
if (string.IsNullOrWhiteSpace(varName))
return false;
if (!(char.IsLetter(varName[0]) || varName[0] == '_'))
return false;
for (int i = 1; i < varName.Length; i++)
{
if (!(char.IsLetterOrDigit(varName[i]) || varName[i] == '_'))
return false;
}
return true;
}
static string GetNameFromInstruction(ILInstruction inst) static string GetNameFromInstruction(ILInstruction inst)
{ {
@ -663,7 +795,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return lowerCaseName; return lowerCaseName;
} }
internal static IType GuessType(IType variableType, ILInstruction inst, ILTransformContext context) static IType GuessType(IType variableType, ILInstruction inst, ILTransformContext context)
{ {
if (!variableType.IsKnownType(KnownTypeCode.Object)) if (!variableType.IsKnownType(KnownTypeCode.Object))
return variableType; return variableType;

2
ICSharpCode.Decompiler/Util/CollectionExtensions.cs

@ -225,7 +225,7 @@ namespace ICSharpCode.Decompiler.Util
yield return func(index++, element); yield return func(index++, element);
} }
public static IEnumerable<(int, T)> WithIndex<T>(this ICollection<T> source) public static IEnumerable<(int, T)> WithIndex<T>(this IEnumerable<T> source)
{ {
int index = 0; int index = 0;
foreach (var item in source) foreach (var item in source)

Loading…
Cancel
Save