diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.cs index d7566dced..8b96d4a6f 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/DelegateConstruction.cs @@ -183,7 +183,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction public interface IM3 { - void M3(); + void M(); } public class BaseClass : IM3 @@ -194,7 +194,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction protected virtual void M2() { } - public virtual void M3() + public virtual void M() { } @@ -209,7 +209,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction protected override void M2() { } - public new void M3() + public new void M() { } @@ -219,30 +219,30 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction Noop("M1", M1); Noop("M2.base", base.M2); Noop("M2", M2); - Noop("M3.base", base.M3); - Noop("M3.base_virt", ((BaseClass)this).M3); - Noop("M3.base_interface", ((IM3)this).M3); + Noop("M.base", base.M); + Noop("M.base_virt", ((BaseClass)this).M); + Noop("M.base_interface", ((IM3)this).M); #if CS70 - Noop("M3", this.M3); - Noop("M3", M3); + Noop("M", this.M); + Noop("M", M); #if CS80 - static void M3() + static void M() #else - void M3() + void M() #endif { } #else - Noop("M3", M3); + Noop("M", M); #endif } public void Test2() { - Noop("M3.new", new BaseClass().M3); - Noop("M3.new", new SubClass().M3); + Noop("M.new", new BaseClass().M); + Noop("M.new", new SubClass().M); } private void Noop(string name, Action _) @@ -525,7 +525,7 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction public static void LocalFunctionDelegateReference() { Use(LocalFunction); - Use2(LocalFunction1); + Use2(LocalFunction2); #if CS80 static void LocalFunction() #else @@ -534,9 +534,9 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.DelegateConstruction { } #if CS80 - static void LocalFunction1() + static void LocalFunction2() #else - void LocalFunction1() + void LocalFunction2() #endif { } diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/LocalFunctions.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/LocalFunctions.cs index ff29a4351..36c2d340a 100644 --- a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/LocalFunctions.cs +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/LocalFunctions.cs @@ -42,50 +42,50 @@ namespace LocalFunctions for (int i = 0; i < 10; i++) { int i2 = 0; - i2 += NonStaticMethod6(0); + i2 += NonStaticMethod(0); #if CS90 [My] [return: My] - int NonStaticMethod6<[My] T3>([My] int unused) + int NonStaticMethod<[My] T3>([My] int unused) #else - int NonStaticMethod6(int unused) + int NonStaticMethod(int unused) #endif { t2 = default(T2); int l = 0; - return NonStaticMethod6_1() + NonStaticMethod6_1() + z.GetHashCode(); - int NonStaticMethod6_1() + return NonStaticMethod2() + NonStaticMethod2() + z.GetHashCode(); + int NonStaticMethod2() { - return i2 + l + NonStaticMethod6(0) + StaticMethod1(); + return i2 + l + NonStaticMethod(0) + StaticMethod(); } } } - return MixedLocalFunction() + MixedLocalFunction() + StaticMethod1() + StaticMethod1() + NonStaticMethod3() + StaticMethod4(null) + StaticMethod5(); + return MixedLocalFunction() + MixedLocalFunction() + StaticMethod() + StaticMethod() + NonStaticMethod3() + StaticMethod4(null) + StaticMethod5(); int NonStaticMethod3() { return GetHashCode(); } #if CS80 - static int StaticMethod1() where T3 : struct + static int StaticMethod() where T3 : struct #else - int StaticMethod1() where T3 : struct + int StaticMethod() where T3 : struct #endif { - return typeof(T1).Name.Length + typeof(T2).Name.Length + typeof(T3).Name.Length + StaticMethod1() + StaticMethod1_1() + StaticMethod2_RepeatT2(); + return typeof(T1).Name.Length + typeof(T2).Name.Length + typeof(T3).Name.Length + StaticMethod() + StaticMethod2() + StaticMethod3(); } #if CS80 - static int StaticMethod1_1() where T3 : struct where T4 : Enum + static int StaticMethod2() where T3 : struct where T4 : Enum #else - int StaticMethod1_1() where T3 : struct where T4 : Enum + int StaticMethod2() where T3 : struct where T4 : Enum #endif { - return typeof(T1).Name.Length + typeof(T2).Name.Length + typeof(T3).Name.Length + typeof(T4).Name.Length + StaticMethod1() + StaticMethod1_1(); + return typeof(T1).Name.Length + typeof(T2).Name.Length + typeof(T3).Name.Length + typeof(T4).Name.Length + StaticMethod() + StaticMethod2(); } #pragma warning disable CS8387 #if CS80 - static int StaticMethod2_RepeatT2() where T2 : IConvertible where T3 : struct where T4 : Enum + static int StaticMethod3() where T2 : IConvertible where T3 : struct where T4 : Enum #else - int StaticMethod2_RepeatT2() where T2 : IConvertible where T3 : struct where T4 : Enum + int StaticMethod3() where T2 : IConvertible where T3 : struct where T4 : Enum #endif #pragma warning restore CS8387 { @@ -106,8 +106,8 @@ namespace LocalFunctions #endif { int k = 0; - return k + NonStaticMethod5_1(); - int NonStaticMethod5_1() + return k + NonStaticMethod4(); + int NonStaticMethod4() { return k; } @@ -122,20 +122,20 @@ namespace LocalFunctions for (int i = 0; i < 10; i++) { int i2 = 0; - i2 += StaticInvokeAsFunc(NonStaticMethod6); - int NonStaticMethod6() + i2 += StaticInvokeAsFunc(NonStaticMethod); + int NonStaticMethod() { t2 = default(T2); int l = 0; - return StaticInvokeAsFunc(NonStaticMethod6_1) + StaticInvokeAsFunc(NonStaticMethod6_1) + z.GetHashCode(); - int NonStaticMethod6_1() + return StaticInvokeAsFunc(NonStaticMethod2) + StaticInvokeAsFunc(NonStaticMethod2) + z.GetHashCode(); + int NonStaticMethod2() { - return i2 + l + StaticInvokeAsFunc(NonStaticMethod6) + StaticInvokeAsFunc(StaticMethod1); + return i2 + l + StaticInvokeAsFunc(NonStaticMethod) + StaticInvokeAsFunc(StaticMethod); } } } Console.WriteLine(t2); - return StaticInvokeAsFunc(MixedLocalFunction2Delegate) + StaticInvokeAsFunc(MixedLocalFunction2Delegate) + StaticInvokeAsFunc(StaticMethod1) + StaticInvokeAsFunc(StaticMethod1) + StaticInvokeAsFunc(NonStaticMethod3) + StaticInvokeAsFunc(StaticMethod5) + new Func(StaticMethod4)(null) + StaticInvokeAsFunc2(StaticMethod4) + new Func, int>(StaticInvokeAsFunc2)(StaticMethod4); + return StaticInvokeAsFunc(MixedLocalFunction2Delegate) + StaticInvokeAsFunc(MixedLocalFunction2Delegate) + StaticInvokeAsFunc(StaticMethod) + StaticInvokeAsFunc(StaticMethod) + StaticInvokeAsFunc(NonStaticMethod3) + StaticInvokeAsFunc(StaticMethod4) + new Func(StaticMethod5)(null) + StaticInvokeAsFunc2(StaticMethod5) + new Func, int>(StaticInvokeAsFunc2)(StaticMethod5); int NonStaticMethod3() { return GetHashCode(); @@ -157,51 +157,51 @@ namespace LocalFunctions return func(default(T)); } #if CS80 - static int StaticMethod1() where T3 : struct + static int StaticMethod() where T3 : struct #else - int StaticMethod1() where T3 : struct + int StaticMethod() where T3 : struct #endif { - return typeof(T1).Name.Length + typeof(T2).Name.Length + typeof(T3).Name.Length + StaticInvokeAsFunc(StaticMethod1) + StaticInvokeAsFunc(StaticMethod1_1) + StaticInvokeAsFunc(StaticMethod2_RepeatT2); + return typeof(T1).Name.Length + typeof(T2).Name.Length + typeof(T3).Name.Length + StaticInvokeAsFunc(StaticMethod) + StaticInvokeAsFunc(StaticMethod2) + StaticInvokeAsFunc(StaticMethod3); } #if CS80 - static int StaticMethod1_1() where T3 : struct where T4 : Enum + static int StaticMethod2() where T3 : struct where T4 : Enum #else - int StaticMethod1_1() where T3 : struct where T4 : Enum + int StaticMethod2() where T3 : struct where T4 : Enum #endif { - return typeof(T1).Name.Length + typeof(T2).Name.Length + typeof(T3).Name.Length + typeof(T4).Name.Length + StaticInvokeAsFunc(StaticMethod1) + StaticInvokeAsFunc(StaticMethod1_1); + return typeof(T1).Name.Length + typeof(T2).Name.Length + typeof(T3).Name.Length + typeof(T4).Name.Length + StaticInvokeAsFunc(StaticMethod) + StaticInvokeAsFunc(StaticMethod2); } #pragma warning disable CS8387 #if CS80 - static int StaticMethod2_RepeatT2() where T2 : IConvertible where T3 : struct where T4 : Enum + static int StaticMethod3() where T2 : IConvertible where T3 : struct where T4 : Enum #else - int StaticMethod2_RepeatT2() where T2 : IConvertible where T3 : struct where T4 : Enum + int StaticMethod3() where T2 : IConvertible where T3 : struct where T4 : Enum #endif #pragma warning restore CS8387 { return typeof(T2).Name.Length; } #if CS80 - static int StaticMethod4(T dd) + static int StaticMethod4() #else - int StaticMethod4(T dd) + int StaticMethod4() #endif { - return 0; + int k = 0; + return k + StaticInvokeAsFunc(NonStaticMethod4); + int NonStaticMethod4() + { + return k; + } } #if CS80 - static int StaticMethod5() + static int StaticMethod5(T dd) #else - int StaticMethod5() + int StaticMethod5(T dd) #endif { - int k = 0; - return k + StaticInvokeAsFunc(NonStaticMethod5_1); - int NonStaticMethod5_1() - { - return k; - } + return 0; } } @@ -209,14 +209,14 @@ namespace LocalFunctions { #pragma warning disable CS0219 T2 t2 = default(T2); - Method1(); - void Method1() + Method(); + void Method() { t2 = default(T2); T2 t2x = t2; T3 t3 = default(T3); - Method1_1(); - void Method1_1() + Method2(); + void Method2() { t2 = default(T2); t2x = t2; @@ -229,7 +229,7 @@ namespace LocalFunctions public void TestGenericArgs() where T2 : List { ZZ(null); - ZZ2(null); + ZZ3(null); #if CS80 static void Nop(T data) #else @@ -244,25 +244,25 @@ namespace LocalFunctions #endif { Nop>(t3); - ZZ1(t3); - ZZ3(); - void ZZ3() + ZZ2(t3); + ZZ4(); + void ZZ4() { Nop>(t3); } } #if CS80 - static void ZZ1(T3 t3) + static void ZZ2(T3 t3) #else - void ZZ1(T3 t3) + void ZZ2(T3 t3) #endif { Nop>((List)(object)t3); } #if CS80 - static void ZZ2(T3 t3) + static void ZZ3(T3 t3) #else - void ZZ2(T3 t3) + void ZZ3(T3 t3) #endif { Nop>((List)(object)t3); @@ -695,22 +695,22 @@ namespace LocalFunctions public void NestedCapture1() { - Method1(null); + Method(null); #if CS80 - static Action Method1(Action action) + static Action Method(Action action) #else - Action Method1(Action action) + Action Method(Action action) #endif { - return Method1_1; + return Method2; - void Method1_1(object containerBuilder) + void Method2(object containerBuilder) { - Method1_2(containerBuilder); + Method3(containerBuilder); } - void Method1_2(object containerBuilder) + void Method3(object containerBuilder) { action(containerBuilder); } @@ -727,25 +727,25 @@ namespace LocalFunctions #endif { int t0 = 0; - return ZZZ_0(); - int ZZZ_0() + return ZZZ(); + int ZZZ() { t0 = 0; int t2 = t0; - return new Func(ZZZ_0_0)(); - int ZZZ_0_0() + return new Func(ZZZ2)(); + int ZZZ2() { t0 = 0; t2 = 0; - return ZZZ_1(); + return ZZZ3(); } } - int ZZZ_1() + int ZZZ3() { t0 = 0; int t3 = t0; - return new Func(ZZZ_1_0)(); - int ZZZ_1_0() + return new Func(ZZZ4)(); + int ZZZ4() { t0 = 0; t3 = 0; @@ -765,18 +765,18 @@ namespace LocalFunctions #endif { int t0 = 0; - return ZZZ_0(); - int ZZZ_0() + return ZZZ(); + int ZZZ() { t0 = 0; int t2 = t0; return ((Func)delegate { t0 = 0; t2 = 0; - return ZZZ_1(); + return ZZZ2(); })(); } - int ZZZ_1() + int ZZZ2() { t0 = 0; int t3 = t0; @@ -808,18 +808,18 @@ namespace LocalFunctions #endif { int t0 = 0; - return ZZZ_0() + ZZZ_1(); - int ZZZ_0() + return ZZZ() + ZZZ2(); + int ZZZ() { t0 = 0; int t2 = t0; return ((Func)delegate { t0 = 0; t2 = 0; - return ZZZ_1(); + return ZZZ2(); })(); } - int ZZZ_1() + int ZZZ2() { t0 = 0; int t3 = t0; diff --git a/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs b/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs index dbe8c5dd4..81dbef08e 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/AssignVariableNames.cs @@ -512,23 +512,37 @@ namespace ICSharpCode.Decompiler.IL.Transforms // assign names to local functions if (!LocalFunctionDecompiler.ParseLocalFunctionName(function.Name, out _, out var newName) || !IsValidName(newName)) newName = null; - if (newName == null) + string nameWithoutNumber; + int number; + if (!string.IsNullOrEmpty(newName)) { - 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(); - } + nameWithoutNumber = SplitName(newName, out number); + } + else + { + nameWithoutNumber = "f"; + number = 1; + } + int count; + if (!context.IsReservedVariableName(nameWithoutNumber, out int currentIndex)) + { + count = 1; + } + else + { + if (currentIndex < number) + count = number; else - { - newName = nameWithoutNumber; - } + count = Math.Max(number, currentIndex) + 1; + } + context.ReserveVariableName(nameWithoutNumber, count); + if (count > 1) + { + newName = nameWithoutNumber + count.ToString(); + } + else + { + newName = nameWithoutNumber; } function.Name = newName; function.ReducedMethod.Name = newName; diff --git a/ILSpy/Properties/Resources.resx b/ILSpy/Properties/Resources.resx index 8b6e4ce23..3c23e3ed5 100644 --- a/ILSpy/Properties/Resources.resx +++ b/ILSpy/Properties/Resources.resx @@ -381,6 +381,9 @@ Are you sure you want to continue? Introduce local functions + + Introduce 'private protected' accessibility + Introduce static local functions @@ -1108,7 +1111,4 @@ Do you want to continue? _Window - - Introduce 'private protected' accessibility - \ No newline at end of file