Skip to content

Commit

Permalink
Reimplement DecompilerSettings.StaticLocalFunctions (was lost in the …
Browse files Browse the repository at this point in the history
…refactoring in #2077)
  • Loading branch information
siegfriedpammer committed Jul 26, 2020
1 parent b4aec9a commit 8d72672
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
22 changes: 21 additions & 1 deletion ICSharpCode.Decompiler/IL/Transforms/LocalFunctionDecompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,27 @@ LocalFunctionMethod ReduceToLocalFunction(IMethod method, int typeParametersToRe
break;
parametersToRemove++;
}
return new LocalFunctionMethod(method, method.Name, parametersToRemove, typeParametersToRemove);
return new LocalFunctionMethod(method, method.Name, CanBeStaticLocalFunction(), parametersToRemove, typeParametersToRemove);

bool CanBeStaticLocalFunction()
{
if (!context.Settings.StaticLocalFunctions)
return false;
// Cannot be static because there are closure parameters that will be removed
if (parametersToRemove > 0)
return false;
// no closure parameters, but static:
// we can safely assume, this local function can be declared static
if (method.IsStatic)
return true;
// the local function is used in conjunction with a lambda, which means,
// it is defined inside the display-class type
var declaringType = method.DeclaringTypeDefinition;
if (!declaringType.IsCompilerGenerated())
return false;
// if there are no instance fields, we can make it a static local function
return !declaringType.GetFields(f => !f.IsStatic).Any();
}
}

static void TransformToLocalFunctionReference(ILFunction function, CallInstruction useSite)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ class LocalFunctionMethod : IMethod
{
readonly IMethod baseMethod;

public LocalFunctionMethod(IMethod baseMethod, string name, int numberOfCompilerGeneratedParameters, int numberOfCompilerGeneratedTypeParameters)
public LocalFunctionMethod(IMethod baseMethod, string name, bool isStaticLocalFunction, int numberOfCompilerGeneratedParameters, int numberOfCompilerGeneratedTypeParameters)
{
if (baseMethod == null)
throw new ArgumentNullException(nameof(baseMethod));
this.baseMethod = baseMethod;
this.Name = name;
this.IsStaticLocalFunction = isStaticLocalFunction;
this.NumberOfCompilerGeneratedParameters = numberOfCompilerGeneratedParameters;
this.NumberOfCompilerGeneratedTypeParameters = numberOfCompilerGeneratedTypeParameters;
}
Expand All @@ -47,7 +48,8 @@ public bool Equals(IMember obj, TypeVisitor typeNormalization)
return false;
return baseMethod.Equals(other.baseMethod, typeNormalization)
&& NumberOfCompilerGeneratedParameters == other.NumberOfCompilerGeneratedParameters
&& NumberOfCompilerGeneratedTypeParameters == other.NumberOfCompilerGeneratedTypeParameters;
&& NumberOfCompilerGeneratedTypeParameters == other.NumberOfCompilerGeneratedTypeParameters
&& IsStaticLocalFunction == other.IsStaticLocalFunction;
}

public override bool Equals(object obj)
Expand All @@ -56,7 +58,8 @@ public override bool Equals(object obj)
return false;
return baseMethod.Equals(other.baseMethod)
&& NumberOfCompilerGeneratedParameters == other.NumberOfCompilerGeneratedParameters
&& NumberOfCompilerGeneratedTypeParameters == other.NumberOfCompilerGeneratedTypeParameters;
&& NumberOfCompilerGeneratedTypeParameters == other.NumberOfCompilerGeneratedTypeParameters
&& IsStaticLocalFunction == other.IsStaticLocalFunction;
}

public override int GetHashCode()
Expand All @@ -66,14 +69,14 @@ public override int GetHashCode()

public override string ToString()
{
return string.Format("[LocalFunctionMethod: ReducedFrom={0}, Name={1}, NumberOfGeneratedParameters={2}, NumberOfCompilerGeneratedTypeParameters={3}]", ReducedFrom, Name, NumberOfCompilerGeneratedParameters, NumberOfCompilerGeneratedTypeParameters);
return string.Format("[LocalFunctionMethod: ReducedFrom={0}, Name={1}, NumberOfGeneratedParameters={2}, NumberOfCompilerGeneratedTypeParameters={3}, IsStaticLocalFunction={4}]", ReducedFrom, Name, NumberOfCompilerGeneratedParameters, NumberOfCompilerGeneratedTypeParameters, IsStaticLocalFunction);
}

internal int NumberOfCompilerGeneratedParameters { get; }

internal int NumberOfCompilerGeneratedTypeParameters { get; }

internal bool IsStaticLocalFunction => NumberOfCompilerGeneratedParameters == 0 && (baseMethod.IsStatic || (baseMethod.DeclaringTypeDefinition.IsCompilerGenerated() && !baseMethod.DeclaringType.GetFields(f => !f.IsStatic).Any()));
internal bool IsStaticLocalFunction { get; }

public IMember MemberDefinition => this;

Expand All @@ -89,7 +92,7 @@ public IMethod Specialize(TypeParameterSubstitution substitution)
{
return new LocalFunctionMethod(
baseMethod.Specialize(substitution),
Name, NumberOfCompilerGeneratedParameters, NumberOfCompilerGeneratedTypeParameters);
Name, IsStaticLocalFunction, NumberOfCompilerGeneratedParameters, NumberOfCompilerGeneratedTypeParameters);
}

IMember IMember.Specialize(TypeParameterSubstitution substitution)
Expand Down

0 comments on commit 8d72672

Please sign in to comment.