From f956e16f58778eb8285ebe16bdb7640d325f150a Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Mon, 23 Jul 2018 21:12:02 +0200 Subject: [PATCH] Add basic support for C# 4 optional arguments. --- .../ICSharpCode.Decompiler.Tests.csproj | 1 + .../PrettyTestRunner.cs | 6 + .../TestCases/Pretty/OptionalArguments.cs | 95 +++++ .../TestCases/Pretty/OptionalArguments.il | 333 +++++++++++++++++ .../TestCases/Pretty/OptionalArguments.opt.il | 304 ++++++++++++++++ .../Pretty/OptionalArguments.opt.roslyn.il | 302 ++++++++++++++++ .../Pretty/OptionalArguments.roslyn.il | 334 ++++++++++++++++++ ICSharpCode.Decompiler/CSharp/CallBuilder.cs | 98 ++++- ILSpy/Properties/AssemblyInfo.template.cs | 2 +- 9 files changed, 1457 insertions(+), 18 deletions(-) create mode 100644 ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.cs create mode 100644 ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.il create mode 100644 ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.opt.il create mode 100644 ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.opt.roslyn.il create mode 100644 ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.roslyn.il diff --git a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj index bf4a8ba3a..8946c9ecb 100644 --- a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj +++ b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj @@ -68,6 +68,7 @@ + diff --git a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs index ac6081054..8b64d0573 100644 --- a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs +++ b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs @@ -302,6 +302,12 @@ namespace ICSharpCode.Decompiler.Tests RunForLibrary(cscOptions: cscOptions); } + [Test] + public void OptionalArguments([ValueSource("defaultOptions")] CSharpCompilerOptions cscOptions) + { + RunForLibrary(cscOptions: cscOptions); + } + [Test] public void Issue1080([ValueSource(nameof(roslynOnlyOptions))] CSharpCompilerOptions cscOptions) { diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.cs new file mode 100644 index 000000000..acb2db36a --- /dev/null +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.cs @@ -0,0 +1,95 @@ +// Copyright (c) 2018 Siegfried Pammer +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this +// software and associated documentation files (the "Software"), to deal in the Software +// without restriction, including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons +// to whom the Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or +// substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty +{ + internal class OptionalArguments + { + private void SimpleTests() + { + Test(); + Test(5); + Test(10, "Hello World!"); + } + + private void Conflicts() + { + OnlyDifferenceIsLastArgument(5, 3, "Hello"); + OnlyDifferenceIsLastArgument(5, 3, 3.141); + OnlyDifferenceIsLastArgument(5, 3, null); + OnlyDifferenceIsLastArgument(5, 3, double.NegativeInfinity); + + OnlyDifferenceIsLastArgumentCastNecessary(10, "World", (string)null); + OnlyDifferenceIsLastArgumentCastNecessary(10, "Hello", (OptionalArguments)null); + + DifferenceInArgumentCount(); + DifferenceInArgumentCount("Hello"); + DifferenceInArgumentCount("World"); + } + + private void ParamsTests() + { + ParamsMethod(5, 10, 9, 8); + ParamsMethod(null); + ParamsMethod(5); + ParamsMethod(10); + ParamsMethod(null, 1, 2, 3); + } + + private void ParamsMethod(int a = 5, params int[] values) + { + } + + private void ParamsMethod(string a = null, params int[] values) + { + } + + private void DifferenceInArgumentCount() + { + } + + private void DifferenceInArgumentCount(string a = "Hello") + { + } + + private void Test(int a = 10, string b = "Test") + { + } + + private void OnlyDifferenceIsLastArgument(int a, int b, string c = null) + { + } + + private void OnlyDifferenceIsLastArgument(int a, int b, double d = double.NegativeInfinity) + { + } + + private void OnlyDifferenceIsLastArgumentCastNecessary(int a, string b, string c = null) + { + } + + private void OnlyDifferenceIsLastArgumentCastNecessary(int a, string b, OptionalArguments args = null) + { + } + + private string Get(out int a) + { + throw null; + } + } +} diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.il b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.il new file mode 100644 index 000000000..1ec7b9b6e --- /dev/null +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.il @@ -0,0 +1,333 @@ + + + + +// Metadata version: v4.0.30319 +.assembly extern mscorlib +{ + .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. + .ver 4:0:0:0 +} +.assembly OptionalArguments +{ + .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx + 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows. + .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) + .permissionset reqmin + = {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}} + .hash algorithm 0x00008004 + .ver 0:0:0:0 +} +.module OptionalArguments.dll +.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 ) +.imagebase 0x10000000 +.file alignment 0x00000200 +.stackreserve 0x00100000 +.subsystem 0x0003 // WINDOWS_CUI +.corflags 0x00000001 // ILONLY + + +// =============== CLASS MEMBERS DECLARATION =================== + +.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments + extends [mscorlib]System.Object +{ + .method private hidebysig instance void + SimpleTests() cil managed + { + // Code size 43 (0x2b) + .maxstack 8 + IL_0000: nop + IL_0001: ldarg.0 + IL_0002: ldc.i4.s 10 + IL_0004: ldstr "Test" + IL_0009: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::Test(int32, + string) + IL_000e: nop + IL_000f: ldarg.0 + IL_0010: ldc.i4.5 + IL_0011: ldstr "Test" + IL_0016: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::Test(int32, + string) + IL_001b: nop + IL_001c: ldarg.0 + IL_001d: ldc.i4.s 10 + IL_001f: ldstr "Hello World!" + IL_0024: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::Test(int32, + string) + IL_0029: nop + IL_002a: ret + } // end of method OptionalArguments::SimpleTests + + .method private hidebysig instance void + Conflicts() cil managed + { + // Code size 123 (0x7b) + .maxstack 4 + IL_0000: nop + IL_0001: ldarg.0 + IL_0002: ldc.i4.5 + IL_0003: ldc.i4.3 + IL_0004: ldstr "Hello" + IL_0009: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgument(int32, + int32, + string) + IL_000e: nop + IL_000f: ldarg.0 + IL_0010: ldc.i4.5 + IL_0011: ldc.i4.3 + IL_0012: ldc.r8 3.141 + IL_001b: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgument(int32, + int32, + float64) + IL_0020: nop + IL_0021: ldarg.0 + IL_0022: ldc.i4.5 + IL_0023: ldc.i4.3 + IL_0024: ldnull + IL_0025: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgument(int32, + int32, + string) + IL_002a: nop + IL_002b: ldarg.0 + IL_002c: ldc.i4.5 + IL_002d: ldc.i4.3 + IL_002e: ldc.r8 (00 00 00 00 00 00 F0 FF) + IL_0037: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgument(int32, + int32, + float64) + IL_003c: nop + IL_003d: ldarg.0 + IL_003e: ldc.i4.s 10 + IL_0040: ldstr "World" + IL_0045: ldnull + IL_0046: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgumentCastNecessary(int32, + string, + string) + IL_004b: nop + IL_004c: ldarg.0 + IL_004d: ldc.i4.s 10 + IL_004f: ldstr "Hello" + IL_0054: ldnull + IL_0055: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgumentCastNecessary(int32, + string, + class ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments) + IL_005a: nop + IL_005b: ldarg.0 + IL_005c: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::DifferenceInArgumentCount() + IL_0061: nop + IL_0062: ldarg.0 + IL_0063: ldstr "Hello" + IL_0068: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::DifferenceInArgumentCount(string) + IL_006d: nop + IL_006e: ldarg.0 + IL_006f: ldstr "World" + IL_0074: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::DifferenceInArgumentCount(string) + IL_0079: nop + IL_007a: ret + } // end of method OptionalArguments::Conflicts + + .method private hidebysig instance void + ParamsTests() cil managed + { + // Code size 103 (0x67) + .maxstack 5 + .locals init (int32[] V_0) + IL_0000: nop + IL_0001: ldarg.0 + IL_0002: ldc.i4.5 + IL_0003: ldc.i4.3 + IL_0004: newarr [mscorlib]System.Int32 + IL_0009: stloc.0 + IL_000a: ldloc.0 + IL_000b: ldc.i4.0 + IL_000c: ldc.i4.s 10 + IL_000e: stelem.i4 + IL_000f: ldloc.0 + IL_0010: ldc.i4.1 + IL_0011: ldc.i4.s 9 + IL_0013: stelem.i4 + IL_0014: ldloc.0 + IL_0015: ldc.i4.2 + IL_0016: ldc.i4.8 + IL_0017: stelem.i4 + IL_0018: ldloc.0 + IL_0019: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(int32, + int32[]) + IL_001e: nop + IL_001f: ldarg.0 + IL_0020: ldnull + IL_0021: ldc.i4.0 + IL_0022: newarr [mscorlib]System.Int32 + IL_0027: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(string, + int32[]) + IL_002c: nop + IL_002d: ldarg.0 + IL_002e: ldc.i4.5 + IL_002f: ldc.i4.0 + IL_0030: newarr [mscorlib]System.Int32 + IL_0035: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(int32, + int32[]) + IL_003a: nop + IL_003b: ldarg.0 + IL_003c: ldc.i4.s 10 + IL_003e: ldc.i4.0 + IL_003f: newarr [mscorlib]System.Int32 + IL_0044: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(int32, + int32[]) + IL_0049: nop + IL_004a: ldarg.0 + IL_004b: ldnull + IL_004c: ldc.i4.3 + IL_004d: newarr [mscorlib]System.Int32 + IL_0052: stloc.0 + IL_0053: ldloc.0 + IL_0054: ldc.i4.0 + IL_0055: ldc.i4.1 + IL_0056: stelem.i4 + IL_0057: ldloc.0 + IL_0058: ldc.i4.1 + IL_0059: ldc.i4.2 + IL_005a: stelem.i4 + IL_005b: ldloc.0 + IL_005c: ldc.i4.2 + IL_005d: ldc.i4.3 + IL_005e: stelem.i4 + IL_005f: ldloc.0 + IL_0060: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(string, + int32[]) + IL_0065: nop + IL_0066: ret + } // end of method OptionalArguments::ParamsTests + + .method private hidebysig instance void + ParamsMethod([opt] int32 a, + int32[] values) cil managed + { + .param [1] = int32(0x00000005) + .param [2] + .custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() = ( 01 00 00 00 ) + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::ParamsMethod + + .method private hidebysig instance void + ParamsMethod([opt] string a, + int32[] values) cil managed + { + .param [1] = nullref + .param [2] + .custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() = ( 01 00 00 00 ) + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::ParamsMethod + + .method private hidebysig instance void + DifferenceInArgumentCount() cil managed + { + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::DifferenceInArgumentCount + + .method private hidebysig instance void + DifferenceInArgumentCount([opt] string a) cil managed + { + .param [1] = "Hello" + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::DifferenceInArgumentCount + + .method private hidebysig instance void + Test([opt] int32 a, + [opt] string b) cil managed + { + .param [1] = int32(0x0000000A) + .param [2] = "Test" + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::Test + + .method private hidebysig instance void + OnlyDifferenceIsLastArgument(int32 a, + int32 b, + [opt] string c) cil managed + { + .param [3] = nullref + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::OnlyDifferenceIsLastArgument + + .method private hidebysig instance void + OnlyDifferenceIsLastArgument(int32 a, + int32 b, + [opt] float64 d) cil managed + { + .param [3] = float64(0xFFF0000000000000) // -1.#INF + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::OnlyDifferenceIsLastArgument + + .method private hidebysig instance void + OnlyDifferenceIsLastArgumentCastNecessary(int32 a, + string b, + [opt] string c) cil managed + { + .param [3] = nullref + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::OnlyDifferenceIsLastArgumentCastNecessary + + .method private hidebysig instance void + OnlyDifferenceIsLastArgumentCastNecessary(int32 a, + string b, + [opt] class ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments args) cil managed + { + .param [3] = nullref + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::OnlyDifferenceIsLastArgumentCastNecessary + + .method private hidebysig instance string + Get([out] int32& a) cil managed + { + // Code size 3 (0x3) + .maxstack 8 + IL_0000: nop + IL_0001: ldnull + IL_0002: throw + } // end of method OptionalArguments::Get + + .method public hidebysig specialname rtspecialname + instance void .ctor() cil managed + { + // Code size 7 (0x7) + .maxstack 8 + IL_0000: ldarg.0 + IL_0001: call instance void [mscorlib]System.Object::.ctor() + IL_0006: ret + } // end of method OptionalArguments::.ctor + +} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments + + +// ============================================================= + +// *********** DISASSEMBLY COMPLETE *********************** diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.opt.il b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.opt.il new file mode 100644 index 000000000..5194dff00 --- /dev/null +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.opt.il @@ -0,0 +1,304 @@ + + + + +// Metadata version: v4.0.30319 +.assembly extern mscorlib +{ + .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. + .ver 4:0:0:0 +} +.assembly OptionalArguments.opt +{ + .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx + 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows. + .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) + .permissionset reqmin + = {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}} + .hash algorithm 0x00008004 + .ver 0:0:0:0 +} +.module OptionalArguments.opt.dll +.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 ) +.imagebase 0x10000000 +.file alignment 0x00000200 +.stackreserve 0x00100000 +.subsystem 0x0003 // WINDOWS_CUI +.corflags 0x00000001 // ILONLY + + +// =============== CLASS MEMBERS DECLARATION =================== + +.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments + extends [mscorlib]System.Object +{ + .method private hidebysig instance void + SimpleTests() cil managed + { + // Code size 39 (0x27) + .maxstack 8 + IL_0000: ldarg.0 + IL_0001: ldc.i4.s 10 + IL_0003: ldstr "Test" + IL_0008: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::Test(int32, + string) + IL_000d: ldarg.0 + IL_000e: ldc.i4.5 + IL_000f: ldstr "Test" + IL_0014: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::Test(int32, + string) + IL_0019: ldarg.0 + IL_001a: ldc.i4.s 10 + IL_001c: ldstr "Hello World!" + IL_0021: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::Test(int32, + string) + IL_0026: ret + } // end of method OptionalArguments::SimpleTests + + .method private hidebysig instance void + Conflicts() cil managed + { + // Code size 113 (0x71) + .maxstack 4 + IL_0000: ldarg.0 + IL_0001: ldc.i4.5 + IL_0002: ldc.i4.3 + IL_0003: ldstr "Hello" + IL_0008: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgument(int32, + int32, + string) + IL_000d: ldarg.0 + IL_000e: ldc.i4.5 + IL_000f: ldc.i4.3 + IL_0010: ldc.r8 3.141 + IL_0019: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgument(int32, + int32, + float64) + IL_001e: ldarg.0 + IL_001f: ldc.i4.5 + IL_0020: ldc.i4.3 + IL_0021: ldnull + IL_0022: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgument(int32, + int32, + string) + IL_0027: ldarg.0 + IL_0028: ldc.i4.5 + IL_0029: ldc.i4.3 + IL_002a: ldc.r8 (00 00 00 00 00 00 F0 FF) + IL_0033: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgument(int32, + int32, + float64) + IL_0038: ldarg.0 + IL_0039: ldc.i4.s 10 + IL_003b: ldstr "World" + IL_0040: ldnull + IL_0041: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgumentCastNecessary(int32, + string, + string) + IL_0046: ldarg.0 + IL_0047: ldc.i4.s 10 + IL_0049: ldstr "Hello" + IL_004e: ldnull + IL_004f: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgumentCastNecessary(int32, + string, + class ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments) + IL_0054: ldarg.0 + IL_0055: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::DifferenceInArgumentCount() + IL_005a: ldarg.0 + IL_005b: ldstr "Hello" + IL_0060: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::DifferenceInArgumentCount(string) + IL_0065: ldarg.0 + IL_0066: ldstr "World" + IL_006b: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::DifferenceInArgumentCount(string) + IL_0070: ret + } // end of method OptionalArguments::Conflicts + + .method private hidebysig instance void + ParamsTests() cil managed + { + // Code size 97 (0x61) + .maxstack 5 + .locals init (int32[] V_0, + int32[] V_1) + IL_0000: ldarg.0 + IL_0001: ldc.i4.5 + IL_0002: ldc.i4.3 + IL_0003: newarr [mscorlib]System.Int32 + IL_0008: stloc.0 + IL_0009: ldloc.0 + IL_000a: ldc.i4.0 + IL_000b: ldc.i4.s 10 + IL_000d: stelem.i4 + IL_000e: ldloc.0 + IL_000f: ldc.i4.1 + IL_0010: ldc.i4.s 9 + IL_0012: stelem.i4 + IL_0013: ldloc.0 + IL_0014: ldc.i4.2 + IL_0015: ldc.i4.8 + IL_0016: stelem.i4 + IL_0017: ldloc.0 + IL_0018: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(int32, + int32[]) + IL_001d: ldarg.0 + IL_001e: ldnull + IL_001f: ldc.i4.0 + IL_0020: newarr [mscorlib]System.Int32 + IL_0025: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(string, + int32[]) + IL_002a: ldarg.0 + IL_002b: ldc.i4.5 + IL_002c: ldc.i4.0 + IL_002d: newarr [mscorlib]System.Int32 + IL_0032: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(int32, + int32[]) + IL_0037: ldarg.0 + IL_0038: ldc.i4.s 10 + IL_003a: ldc.i4.0 + IL_003b: newarr [mscorlib]System.Int32 + IL_0040: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(int32, + int32[]) + IL_0045: ldarg.0 + IL_0046: ldnull + IL_0047: ldc.i4.3 + IL_0048: newarr [mscorlib]System.Int32 + IL_004d: stloc.1 + IL_004e: ldloc.1 + IL_004f: ldc.i4.0 + IL_0050: ldc.i4.1 + IL_0051: stelem.i4 + IL_0052: ldloc.1 + IL_0053: ldc.i4.1 + IL_0054: ldc.i4.2 + IL_0055: stelem.i4 + IL_0056: ldloc.1 + IL_0057: ldc.i4.2 + IL_0058: ldc.i4.3 + IL_0059: stelem.i4 + IL_005a: ldloc.1 + IL_005b: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(string, + int32[]) + IL_0060: ret + } // end of method OptionalArguments::ParamsTests + + .method private hidebysig instance void + ParamsMethod([opt] int32 a, + int32[] values) cil managed + { + .param [1] = int32(0x00000005) + .param [2] + .custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() = ( 01 00 00 00 ) + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::ParamsMethod + + .method private hidebysig instance void + ParamsMethod([opt] string a, + int32[] values) cil managed + { + .param [1] = nullref + .param [2] + .custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() = ( 01 00 00 00 ) + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::ParamsMethod + + .method private hidebysig instance void + DifferenceInArgumentCount() cil managed + { + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::DifferenceInArgumentCount + + .method private hidebysig instance void + DifferenceInArgumentCount([opt] string a) cil managed + { + .param [1] = "Hello" + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::DifferenceInArgumentCount + + .method private hidebysig instance void + Test([opt] int32 a, + [opt] string b) cil managed + { + .param [1] = int32(0x0000000A) + .param [2] = "Test" + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::Test + + .method private hidebysig instance void + OnlyDifferenceIsLastArgument(int32 a, + int32 b, + [opt] string c) cil managed + { + .param [3] = nullref + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::OnlyDifferenceIsLastArgument + + .method private hidebysig instance void + OnlyDifferenceIsLastArgument(int32 a, + int32 b, + [opt] float64 d) cil managed + { + .param [3] = float64(0xFFF0000000000000) // -1.#INF + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::OnlyDifferenceIsLastArgument + + .method private hidebysig instance void + OnlyDifferenceIsLastArgumentCastNecessary(int32 a, + string b, + [opt] string c) cil managed + { + .param [3] = nullref + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::OnlyDifferenceIsLastArgumentCastNecessary + + .method private hidebysig instance void + OnlyDifferenceIsLastArgumentCastNecessary(int32 a, + string b, + [opt] class ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments args) cil managed + { + .param [3] = nullref + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::OnlyDifferenceIsLastArgumentCastNecessary + + .method private hidebysig instance string + Get([out] int32& a) cil managed + { + // Code size 2 (0x2) + .maxstack 8 + IL_0000: ldnull + IL_0001: throw + } // end of method OptionalArguments::Get + + .method public hidebysig specialname rtspecialname + instance void .ctor() cil managed + { + // Code size 7 (0x7) + .maxstack 8 + IL_0000: ldarg.0 + IL_0001: call instance void [mscorlib]System.Object::.ctor() + IL_0006: ret + } // end of method OptionalArguments::.ctor + +} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments + + +// ============================================================= + +// *********** DISASSEMBLY COMPLETE *********************** diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.opt.roslyn.il b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.opt.roslyn.il new file mode 100644 index 000000000..e70c79387 --- /dev/null +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.opt.roslyn.il @@ -0,0 +1,302 @@ + + + + +// Metadata version: v4.0.30319 +.assembly extern mscorlib +{ + .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. + .ver 4:0:0:0 +} +.assembly OptionalArguments +{ + .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) + .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx + 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows. + + // --- The following custom attribute is added automatically, do not uncomment ------- + // .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 02 00 00 00 00 00 ) + + .permissionset reqmin + = {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}} + .hash algorithm 0x00008004 + .ver 0:0:0:0 +} +.module OptionalArguments.dll +.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 ) +.imagebase 0x10000000 +.file alignment 0x00000200 +.stackreserve 0x00100000 +.subsystem 0x0003 // WINDOWS_CUI +.corflags 0x00000001 // ILONLY + + +// =============== CLASS MEMBERS DECLARATION =================== + +.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments + extends [mscorlib]System.Object +{ + .method private hidebysig instance void + SimpleTests() cil managed + { + // Code size 39 (0x27) + .maxstack 8 + IL_0000: ldarg.0 + IL_0001: ldc.i4.s 10 + IL_0003: ldstr "Test" + IL_0008: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::Test(int32, + string) + IL_000d: ldarg.0 + IL_000e: ldc.i4.5 + IL_000f: ldstr "Test" + IL_0014: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::Test(int32, + string) + IL_0019: ldarg.0 + IL_001a: ldc.i4.s 10 + IL_001c: ldstr "Hello World!" + IL_0021: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::Test(int32, + string) + IL_0026: ret + } // end of method OptionalArguments::SimpleTests + + .method private hidebysig instance void + Conflicts() cil managed + { + // Code size 113 (0x71) + .maxstack 4 + IL_0000: ldarg.0 + IL_0001: ldc.i4.5 + IL_0002: ldc.i4.3 + IL_0003: ldstr "Hello" + IL_0008: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgument(int32, + int32, + string) + IL_000d: ldarg.0 + IL_000e: ldc.i4.5 + IL_000f: ldc.i4.3 + IL_0010: ldc.r8 3.141 + IL_0019: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgument(int32, + int32, + float64) + IL_001e: ldarg.0 + IL_001f: ldc.i4.5 + IL_0020: ldc.i4.3 + IL_0021: ldnull + IL_0022: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgument(int32, + int32, + string) + IL_0027: ldarg.0 + IL_0028: ldc.i4.5 + IL_0029: ldc.i4.3 + IL_002a: ldc.r8 (00 00 00 00 00 00 F0 FF) + IL_0033: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgument(int32, + int32, + float64) + IL_0038: ldarg.0 + IL_0039: ldc.i4.s 10 + IL_003b: ldstr "World" + IL_0040: ldnull + IL_0041: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgumentCastNecessary(int32, + string, + string) + IL_0046: ldarg.0 + IL_0047: ldc.i4.s 10 + IL_0049: ldstr "Hello" + IL_004e: ldnull + IL_004f: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgumentCastNecessary(int32, + string, + class ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments) + IL_0054: ldarg.0 + IL_0055: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::DifferenceInArgumentCount() + IL_005a: ldarg.0 + IL_005b: ldstr "Hello" + IL_0060: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::DifferenceInArgumentCount(string) + IL_0065: ldarg.0 + IL_0066: ldstr "World" + IL_006b: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::DifferenceInArgumentCount(string) + IL_0070: ret + } // end of method OptionalArguments::Conflicts + + .method private hidebysig instance void + ParamsTests() cil managed + { + // Code size 86 (0x56) + .maxstack 5 + IL_0000: ldarg.0 + IL_0001: ldc.i4.5 + IL_0002: ldc.i4.3 + IL_0003: newarr [mscorlib]System.Int32 + IL_0008: dup + IL_0009: ldtoken field valuetype ''/'__StaticArrayInitTypeSize=12' ''::'0F3DD643C5167ACFC541F72809FFF828A6E41494' + IL_000e: call void [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, + valuetype [mscorlib]System.RuntimeFieldHandle) + IL_0013: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(int32, + int32[]) + IL_0018: ldarg.0 + IL_0019: ldnull + IL_001a: call !!0[] [mscorlib]System.Array::Empty() + IL_001f: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(string, + int32[]) + IL_0024: ldarg.0 + IL_0025: ldc.i4.5 + IL_0026: call !!0[] [mscorlib]System.Array::Empty() + IL_002b: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(int32, + int32[]) + IL_0030: ldarg.0 + IL_0031: ldc.i4.s 10 + IL_0033: call !!0[] [mscorlib]System.Array::Empty() + IL_0038: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(int32, + int32[]) + IL_003d: ldarg.0 + IL_003e: ldnull + IL_003f: ldc.i4.3 + IL_0040: newarr [mscorlib]System.Int32 + IL_0045: dup + IL_0046: ldtoken field valuetype ''/'__StaticArrayInitTypeSize=12' ''::E429CCA3F703A39CC5954A6572FEC9086135B34E + IL_004b: call void [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, + valuetype [mscorlib]System.RuntimeFieldHandle) + IL_0050: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(string, + int32[]) + IL_0055: ret + } // end of method OptionalArguments::ParamsTests + + .method private hidebysig instance void + ParamsMethod([opt] int32 a, + int32[] values) cil managed + { + .param [1] = int32(0x00000005) + .param [2] + .custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() = ( 01 00 00 00 ) + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::ParamsMethod + + .method private hidebysig instance void + ParamsMethod([opt] string a, + int32[] values) cil managed + { + .param [1] = nullref + .param [2] + .custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() = ( 01 00 00 00 ) + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::ParamsMethod + + .method private hidebysig instance void + DifferenceInArgumentCount() cil managed + { + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::DifferenceInArgumentCount + + .method private hidebysig instance void + DifferenceInArgumentCount([opt] string a) cil managed + { + .param [1] = "Hello" + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::DifferenceInArgumentCount + + .method private hidebysig instance void + Test([opt] int32 a, + [opt] string b) cil managed + { + .param [1] = int32(0x0000000A) + .param [2] = "Test" + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::Test + + .method private hidebysig instance void + OnlyDifferenceIsLastArgument(int32 a, + int32 b, + [opt] string c) cil managed + { + .param [3] = nullref + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::OnlyDifferenceIsLastArgument + + .method private hidebysig instance void + OnlyDifferenceIsLastArgument(int32 a, + int32 b, + [opt] float64 d) cil managed + { + .param [3] = float64(0xFFF0000000000000) // -1.#INF + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::OnlyDifferenceIsLastArgument + + .method private hidebysig instance void + OnlyDifferenceIsLastArgumentCastNecessary(int32 a, + string b, + [opt] string c) cil managed + { + .param [3] = nullref + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::OnlyDifferenceIsLastArgumentCastNecessary + + .method private hidebysig instance void + OnlyDifferenceIsLastArgumentCastNecessary(int32 a, + string b, + [opt] class ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments args) cil managed + { + .param [3] = nullref + // Code size 1 (0x1) + .maxstack 8 + IL_0000: ret + } // end of method OptionalArguments::OnlyDifferenceIsLastArgumentCastNecessary + + .method private hidebysig instance string + Get([out] int32& a) cil managed + { + // Code size 2 (0x2) + .maxstack 8 + IL_0000: ldnull + IL_0001: throw + } // end of method OptionalArguments::Get + + .method public hidebysig specialname rtspecialname + instance void .ctor() cil managed + { + // Code size 7 (0x7) + .maxstack 8 + IL_0000: ldarg.0 + IL_0001: call instance void [mscorlib]System.Object::.ctor() + IL_0006: ret + } // end of method OptionalArguments::.ctor + +} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments + +.class private auto ansi sealed '' + extends [mscorlib]System.Object +{ + .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 ) + .class explicit ansi sealed nested private '__StaticArrayInitTypeSize=12' + extends [mscorlib]System.ValueType + { + .pack 1 + .size 12 + } // end of class '__StaticArrayInitTypeSize=12' + + .field static assembly initonly valuetype ''/'__StaticArrayInitTypeSize=12' '0F3DD643C5167ACFC541F72809FFF828A6E41494' at I_00002A74 + .field static assembly initonly valuetype ''/'__StaticArrayInitTypeSize=12' E429CCA3F703A39CC5954A6572FEC9086135B34E at I_00002A84 +} // end of class '' + + +// ============================================================= + +.data cil I_00002A74 = bytearray ( + 0A 00 00 00 09 00 00 00 08 00 00 00) +.data cil I_00002A84 = bytearray ( + 01 00 00 00 02 00 00 00 03 00 00 00) +// *********** DISASSEMBLY COMPLETE *********************** diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.roslyn.il b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.roslyn.il new file mode 100644 index 000000000..37ec71a9f --- /dev/null +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArguments.roslyn.il @@ -0,0 +1,334 @@ + + + + +// Metadata version: v4.0.30319 +.assembly extern mscorlib +{ + .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. + .ver 4:0:0:0 +} +.assembly OptionalArguments +{ + .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) + .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx + 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows. + + // --- The following custom attribute is added automatically, do not uncomment ------- + // .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 07 01 00 00 00 00 ) + + .permissionset reqmin + = {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}} + .hash algorithm 0x00008004 + .ver 0:0:0:0 +} +.module OptionalArguments.dll +.custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 ) +.imagebase 0x10000000 +.file alignment 0x00000200 +.stackreserve 0x00100000 +.subsystem 0x0003 // WINDOWS_CUI +.corflags 0x00000001 // ILONLY + + +// =============== CLASS MEMBERS DECLARATION =================== + +.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments + extends [mscorlib]System.Object +{ + .method private hidebysig instance void + SimpleTests() cil managed + { + // Code size 43 (0x2b) + .maxstack 8 + IL_0000: nop + IL_0001: ldarg.0 + IL_0002: ldc.i4.s 10 + IL_0004: ldstr "Test" + IL_0009: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::Test(int32, + string) + IL_000e: nop + IL_000f: ldarg.0 + IL_0010: ldc.i4.5 + IL_0011: ldstr "Test" + IL_0016: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::Test(int32, + string) + IL_001b: nop + IL_001c: ldarg.0 + IL_001d: ldc.i4.s 10 + IL_001f: ldstr "Hello World!" + IL_0024: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::Test(int32, + string) + IL_0029: nop + IL_002a: ret + } // end of method OptionalArguments::SimpleTests + + .method private hidebysig instance void + Conflicts() cil managed + { + // Code size 123 (0x7b) + .maxstack 4 + IL_0000: nop + IL_0001: ldarg.0 + IL_0002: ldc.i4.5 + IL_0003: ldc.i4.3 + IL_0004: ldstr "Hello" + IL_0009: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgument(int32, + int32, + string) + IL_000e: nop + IL_000f: ldarg.0 + IL_0010: ldc.i4.5 + IL_0011: ldc.i4.3 + IL_0012: ldc.r8 3.141 + IL_001b: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgument(int32, + int32, + float64) + IL_0020: nop + IL_0021: ldarg.0 + IL_0022: ldc.i4.5 + IL_0023: ldc.i4.3 + IL_0024: ldnull + IL_0025: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgument(int32, + int32, + string) + IL_002a: nop + IL_002b: ldarg.0 + IL_002c: ldc.i4.5 + IL_002d: ldc.i4.3 + IL_002e: ldc.r8 (00 00 00 00 00 00 F0 FF) + IL_0037: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgument(int32, + int32, + float64) + IL_003c: nop + IL_003d: ldarg.0 + IL_003e: ldc.i4.s 10 + IL_0040: ldstr "World" + IL_0045: ldnull + IL_0046: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgumentCastNecessary(int32, + string, + string) + IL_004b: nop + IL_004c: ldarg.0 + IL_004d: ldc.i4.s 10 + IL_004f: ldstr "Hello" + IL_0054: ldnull + IL_0055: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::OnlyDifferenceIsLastArgumentCastNecessary(int32, + string, + class ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments) + IL_005a: nop + IL_005b: ldarg.0 + IL_005c: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::DifferenceInArgumentCount() + IL_0061: nop + IL_0062: ldarg.0 + IL_0063: ldstr "Hello" + IL_0068: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::DifferenceInArgumentCount(string) + IL_006d: nop + IL_006e: ldarg.0 + IL_006f: ldstr "World" + IL_0074: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::DifferenceInArgumentCount(string) + IL_0079: nop + IL_007a: ret + } // end of method OptionalArguments::Conflicts + + .method private hidebysig instance void + ParamsTests() cil managed + { + // Code size 92 (0x5c) + .maxstack 5 + IL_0000: nop + IL_0001: ldarg.0 + IL_0002: ldc.i4.5 + IL_0003: ldc.i4.3 + IL_0004: newarr [mscorlib]System.Int32 + IL_0009: dup + IL_000a: ldtoken field valuetype ''/'__StaticArrayInitTypeSize=12' ''::'0F3DD643C5167ACFC541F72809FFF828A6E41494' + IL_000f: call void [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, + valuetype [mscorlib]System.RuntimeFieldHandle) + IL_0014: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(int32, + int32[]) + IL_0019: nop + IL_001a: ldarg.0 + IL_001b: ldnull + IL_001c: call !!0[] [mscorlib]System.Array::Empty() + IL_0021: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(string, + int32[]) + IL_0026: nop + IL_0027: ldarg.0 + IL_0028: ldc.i4.5 + IL_0029: call !!0[] [mscorlib]System.Array::Empty() + IL_002e: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(int32, + int32[]) + IL_0033: nop + IL_0034: ldarg.0 + IL_0035: ldc.i4.s 10 + IL_0037: call !!0[] [mscorlib]System.Array::Empty() + IL_003c: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(int32, + int32[]) + IL_0041: nop + IL_0042: ldarg.0 + IL_0043: ldnull + IL_0044: ldc.i4.3 + IL_0045: newarr [mscorlib]System.Int32 + IL_004a: dup + IL_004b: ldtoken field valuetype ''/'__StaticArrayInitTypeSize=12' ''::E429CCA3F703A39CC5954A6572FEC9086135B34E + IL_0050: call void [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array, + valuetype [mscorlib]System.RuntimeFieldHandle) + IL_0055: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments::ParamsMethod(string, + int32[]) + IL_005a: nop + IL_005b: ret + } // end of method OptionalArguments::ParamsTests + + .method private hidebysig instance void + ParamsMethod([opt] int32 a, + int32[] values) cil managed + { + .param [1] = int32(0x00000005) + .param [2] + .custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() = ( 01 00 00 00 ) + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::ParamsMethod + + .method private hidebysig instance void + ParamsMethod([opt] string a, + int32[] values) cil managed + { + .param [1] = nullref + .param [2] + .custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() = ( 01 00 00 00 ) + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::ParamsMethod + + .method private hidebysig instance void + DifferenceInArgumentCount() cil managed + { + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::DifferenceInArgumentCount + + .method private hidebysig instance void + DifferenceInArgumentCount([opt] string a) cil managed + { + .param [1] = "Hello" + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::DifferenceInArgumentCount + + .method private hidebysig instance void + Test([opt] int32 a, + [opt] string b) cil managed + { + .param [1] = int32(0x0000000A) + .param [2] = "Test" + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::Test + + .method private hidebysig instance void + OnlyDifferenceIsLastArgument(int32 a, + int32 b, + [opt] string c) cil managed + { + .param [3] = nullref + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::OnlyDifferenceIsLastArgument + + .method private hidebysig instance void + OnlyDifferenceIsLastArgument(int32 a, + int32 b, + [opt] float64 d) cil managed + { + .param [3] = float64(0xFFF0000000000000) // -1.#INF + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::OnlyDifferenceIsLastArgument + + .method private hidebysig instance void + OnlyDifferenceIsLastArgumentCastNecessary(int32 a, + string b, + [opt] string c) cil managed + { + .param [3] = nullref + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::OnlyDifferenceIsLastArgumentCastNecessary + + .method private hidebysig instance void + OnlyDifferenceIsLastArgumentCastNecessary(int32 a, + string b, + [opt] class ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments args) cil managed + { + .param [3] = nullref + // Code size 2 (0x2) + .maxstack 8 + IL_0000: nop + IL_0001: ret + } // end of method OptionalArguments::OnlyDifferenceIsLastArgumentCastNecessary + + .method private hidebysig instance string + Get([out] int32& a) cil managed + { + // Code size 3 (0x3) + .maxstack 8 + IL_0000: nop + IL_0001: ldnull + IL_0002: throw + } // end of method OptionalArguments::Get + + .method public hidebysig specialname rtspecialname + instance void .ctor() cil managed + { + // Code size 8 (0x8) + .maxstack 8 + IL_0000: ldarg.0 + IL_0001: call instance void [mscorlib]System.Object::.ctor() + IL_0006: nop + IL_0007: ret + } // end of method OptionalArguments::.ctor + +} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.OptionalArguments + +.class private auto ansi sealed '' + extends [mscorlib]System.Object +{ + .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 ) + .class explicit ansi sealed nested private '__StaticArrayInitTypeSize=12' + extends [mscorlib]System.ValueType + { + .pack 1 + .size 12 + } // end of class '__StaticArrayInitTypeSize=12' + + .field static assembly initonly valuetype ''/'__StaticArrayInitTypeSize=12' '0F3DD643C5167ACFC541F72809FFF828A6E41494' at I_00002A90 + .field static assembly initonly valuetype ''/'__StaticArrayInitTypeSize=12' E429CCA3F703A39CC5954A6572FEC9086135B34E at I_00002AA0 +} // end of class '' + + +// ============================================================= + +.data cil I_00002A90 = bytearray ( + 0A 00 00 00 09 00 00 00 08 00 00 00) +.data cil I_00002A9C = int8[4] +.data cil I_00002AA0 = bytearray ( + 01 00 00 00 02 00 00 00 03 00 00 00) +// *********** DISASSEMBLY COMPLETE *********************** diff --git a/ICSharpCode.Decompiler/CSharp/CallBuilder.cs b/ICSharpCode.Decompiler/CSharp/CallBuilder.cs index 72a92f9bd..00d397d33 100644 --- a/ICSharpCode.Decompiler/CSharp/CallBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/CallBuilder.cs @@ -120,9 +120,22 @@ namespace ICSharpCode.Decompiler.CSharp Debug.Assert(callArguments.Count == firstParamIndex + method.Parameters.Count); var expectedParameters = new List(arguments.Count); // parameters, but in argument order bool isExpandedForm = false; + + // Optional arguments: + // We only allow removing optional arguments in the following cases: + // - call arguments are not in expanded form + // - there are no named arguments + // This value has the following following values: + // -2 - there are no optional arguments + // -1 - optional arguments are forbidden + // >= 0 - the index of the first argument that can be removed, because it is optional + // and is the default value of the parameter. + int firstOptionalArgumentIndex = -2; for (int i = firstParamIndex; i < callArguments.Count; i++) { IParameter parameter; if (argumentToParameterMap != null) { + // Don't use optional arguments, if we have to use named arguments. + firstOptionalArgumentIndex = -1; if (argumentNames == null && argumentToParameterMap[i] != i - firstParamIndex) { // Starting at the first argument that is out-of-place, // assign names to that argument and all following arguments: @@ -136,6 +149,12 @@ namespace ICSharpCode.Decompiler.CSharp parameter = method.Parameters[i - firstParamIndex]; } var arg = expressionBuilder.Translate(callArguments[i], parameter.Type); + if (IsOptionalArgument(parameter, arg)) { + if (firstOptionalArgumentIndex == -2) + firstOptionalArgumentIndex = i - firstParamIndex; + } else { + firstOptionalArgumentIndex = -2; + } if (parameter.IsParams && i + 1 == callArguments.Count && argumentToParameterMap == null) { // Parameter is marked params // If the argument is an array creation, inline all elements into the call and add missing default values. @@ -143,6 +162,7 @@ namespace ICSharpCode.Decompiler.CSharp if (TransformParamsArgument(expectedTargetDetails, target.ResolveResult, method, parameter, arg, ref expectedParameters, ref arguments)) { Debug.Assert(argumentNames == null); + firstOptionalArgumentIndex = -1; isExpandedForm = true; continue; } @@ -166,6 +186,7 @@ namespace ICSharpCode.Decompiler.CSharp } if (method is VarArgInstanceMethod) { + firstOptionalArgumentIndex = -1; int regularParameterCount = ((VarArgInstanceMethod)method).RegularParameterCount; var argListArg = new UndocumentedExpression(); argListArg.UndocumentedExpressionType = UndocumentedExpressionType.ArgList; @@ -203,7 +224,7 @@ namespace ICSharpCode.Decompiler.CSharp return atce .WithRR(rr); } else { - if (IsUnambiguousCall(expectedTargetDetails, method, null, Empty.Array, arguments, argumentNames, out _) != OverloadResolutionErrors.None) { + if (IsUnambiguousCall(expectedTargetDetails, method, null, Empty.Array, arguments, argumentNames, firstOptionalArgumentIndex, out _) != OverloadResolutionErrors.None) { CastArguments(arguments, expectedParameters); } return new ObjectCreateExpression( @@ -227,7 +248,7 @@ namespace ICSharpCode.Decompiler.CSharp return HandleImplicitConversion(method, arguments[0]); } else { var transform = GetRequiredTransformationsForCall(expectedTargetDetails, method, ref target, - arguments, argumentNames, expectedParameters, CallTransformation.All, out IParameterizedMember foundMethod); + arguments, argumentNames, firstOptionalArgumentIndex, expectedParameters, CallTransformation.All, out IParameterizedMember foundMethod); // Note: after this, 'method' and 'foundMethod' may differ, // but as far as allowed by IsAppropriateCallTarget(). @@ -235,6 +256,9 @@ namespace ICSharpCode.Decompiler.CSharp Expression targetExpr; string methodName = method.Name; AstNodeCollection typeArgumentList; + if ((transform & CallTransformation.NoOptionalArgumentAllowed) != 0) { + firstOptionalArgumentIndex = -1; + } if ((transform & CallTransformation.RequireTarget) != 0) { targetExpr = new MemberReferenceExpression(target.Expression, methodName); typeArgumentList = ((MemberReferenceExpression)targetExpr).TypeArguments; @@ -256,8 +280,8 @@ namespace ICSharpCode.Decompiler.CSharp if ((transform & CallTransformation.RequireTypeArguments) != 0 && (!settings.AnonymousTypes || !method.TypeArguments.Any(a => a.ContainsAnonymousType()))) typeArgumentList.AddRange(method.TypeArguments.Select(expressionBuilder.ConvertType)); var argumentExpressions = GetArgumentExpressions(arguments, argumentNames); - return new InvocationExpression(targetExpr, argumentExpressions) - .WithRR(new CSharpInvocationResolveResult(target.ResolveResult, foundMethod, argumentResolveResults, isExpandedForm: isExpandedForm)); + return new InvocationExpression(targetExpr, firstOptionalArgumentIndex < 0 ? argumentExpressions : argumentExpressions.Take(firstOptionalArgumentIndex).ToArray()) + .WithRR(new CSharpInvocationResolveResult(target.ResolveResult, foundMethod, firstOptionalArgumentIndex < 0 ? argumentResolveResults : argumentResolveResults.Take(firstOptionalArgumentIndex).ToList(), isExpandedForm: isExpandedForm)); } } } @@ -327,7 +351,7 @@ namespace ICSharpCode.Decompiler.CSharp var unused = new IdentifierExpression("initializedObject").WithRR(target).WithoutILInstruction(); var transform = GetRequiredTransformationsForCall(expectedTargetDetails, method, ref unused, - arguments, null, expectedParameters, CallTransformation.None, out IParameterizedMember foundMethod); + arguments, null, -1, expectedParameters, CallTransformation.None, out IParameterizedMember foundMethod); Debug.Assert(transform == CallTransformation.None); // Calls with only one argument do not need an array initializer expression to wrap them. @@ -344,15 +368,11 @@ namespace ICSharpCode.Decompiler.CSharp IMethod method, IParameter parameter, TranslatedExpression arg, ref List expectedParameters, ref List arguments) { - if (arg.ResolveResult is ArrayCreateResolveResult acrr && - acrr.SizeArguments.Count == 1 && - acrr.SizeArguments[0].IsCompileTimeConstant && - acrr.SizeArguments[0].ConstantValue is int length) { + if (CheckArgument(out int length, out IType elementType)) { var expandedParameters = new List(expectedParameters); var expandedArguments = new List(arguments); if (length > 0) { var arrayElements = ((ArrayCreateExpression)arg.Expression).Initializer.Elements.ToArray(); - var elementType = ((ArrayType)acrr.Type).ElementType; for (int j = 0; j < length; j++) { expandedParameters.Add(new DefaultParameter(elementType, parameter.Name + j)); if (j < arrayElements.Length) @@ -361,13 +381,47 @@ namespace ICSharpCode.Decompiler.CSharp expandedArguments.Add(expressionBuilder.GetDefaultValueExpression(elementType).WithoutILInstruction()); } } - if (IsUnambiguousCall(expectedTargetDetails, method, targetResolveResult, Empty.Array, expandedArguments, null, out _) == OverloadResolutionErrors.None) { + if (IsUnambiguousCall(expectedTargetDetails, method, targetResolveResult, Empty.Array, expandedArguments, null, -1, out _) == OverloadResolutionErrors.None) { expectedParameters = expandedParameters; arguments = expandedArguments.SelectList(a => new TranslatedExpression(a.Expression.Detach())); return true; } } return false; + + bool CheckArgument(out int len, out IType t) + { + len = 0; + t = null; + if (arg.ResolveResult is CSharpInvocationResolveResult csirr && + csirr.Arguments.Count == 0 && csirr.Member is IMethod emptyMethod && + emptyMethod.IsStatic && + "System.Array.Empty" == emptyMethod.FullName && + emptyMethod.TypeArguments.Count == 1) + { + t = emptyMethod.TypeArguments[0]; + return true; + } + + if (arg.ResolveResult is ArrayCreateResolveResult acrr && + acrr.SizeArguments.Count == 1 && + acrr.SizeArguments[0].IsCompileTimeConstant && + acrr.SizeArguments[0].ConstantValue is int l) + { + len = l; + t = ((ArrayType)acrr.Type).ElementType; + return true; + } + return false; + } + } + + bool IsOptionalArgument(IParameter parameter, TranslatedExpression arg) + { + if (!parameter.IsOptional || !arg.ResolveResult.IsCompileTimeConstant) + return false; + return (parameter.ConstantValue == null && arg.ResolveResult.ConstantValue == null) + || (parameter.ConstantValue != null && parameter.ConstantValue.Equals(arg.ResolveResult.ConstantValue)); } [Flags] @@ -376,11 +430,12 @@ namespace ICSharpCode.Decompiler.CSharp None = 0, RequireTarget = 1, RequireTypeArguments = 2, - All = 3 + NoOptionalArgumentAllowed = 4, + All = 7 } private CallTransformation GetRequiredTransformationsForCall(ExpectedTargetDetails expectedTargetDetails, IMethod method, - ref TranslatedExpression target, List arguments, string[] argumentNames, + ref TranslatedExpression target, List arguments, string[] argumentNames, int firstOptionalArgumentIndex, List expectedParameters, CallTransformation allowedTransforms, out IParameterizedMember foundMethod) { CallTransformation transform = CallTransformation.None; @@ -437,7 +492,7 @@ namespace ICSharpCode.Decompiler.CSharp bool targetCasted = false; bool argumentsCasted = false; OverloadResolutionErrors errors; - while ((errors = IsUnambiguousCall(expectedTargetDetails, method, targetResolveResult, typeArguments, arguments, argumentNames, out foundMethod)) != OverloadResolutionErrors.None) { + while ((errors = IsUnambiguousCall(expectedTargetDetails, method, targetResolveResult, typeArguments, arguments, argumentNames, firstOptionalArgumentIndex, out foundMethod)) != OverloadResolutionErrors.None) { switch (errors) { case OverloadResolutionErrors.TypeInferenceFailed: if ((allowedTransforms & CallTransformation.RequireTypeArguments) != 0) { @@ -450,10 +505,16 @@ namespace ICSharpCode.Decompiler.CSharp requireTypeArguments = true; typeArguments = method.TypeArguments.ToArray(); continue; + case OverloadResolutionErrors.MissingArgumentForRequiredParameter: + if (firstOptionalArgumentIndex == -1) goto default; + firstOptionalArgumentIndex = -1; + continue; default: // TODO : implement some more intelligent algorithm that decides which of these fixes (cast args, add target, cast target, add type args) // is best in this case. Additionally we should not cast all arguments at once, but step-by-step try to add only a minimal number of casts. - if (!argumentsCasted) { + if (firstOptionalArgumentIndex >= 0) { + firstOptionalArgumentIndex = -1; + } else if (!argumentsCasted) { // If we added type arguments beforehand, but that didn't make the code any better, // undo that decision and add casts first. if (appliedRequireTypeArgumentsShortcut) { @@ -486,6 +547,8 @@ namespace ICSharpCode.Decompiler.CSharp transform |= CallTransformation.RequireTarget; if ((allowedTransforms & CallTransformation.RequireTypeArguments) != 0 && requireTypeArguments) transform |= CallTransformation.RequireTypeArguments; + if (firstOptionalArgumentIndex < 0) + transform |= CallTransformation.NoOptionalArgumentAllowed; return transform; } @@ -607,13 +670,14 @@ namespace ICSharpCode.Decompiler.CSharp OverloadResolutionErrors IsUnambiguousCall(ExpectedTargetDetails expectedTargetDetails, IMethod method, ResolveResult target, IType[] typeArguments, IList arguments, - string[] argumentNames, + string[] argumentNames, int firstOptionalArgumentIndex, out IParameterizedMember foundMember) { + Debug.Assert(firstOptionalArgumentIndex < 0 || argumentNames == null); foundMember = null; var lookup = new MemberLookup(resolver.CurrentTypeDefinition, resolver.CurrentTypeDefinition.ParentModule); var or = new OverloadResolution(resolver.Compilation, - arguments.SelectArray(a => a.ResolveResult), + firstOptionalArgumentIndex < 0 ? arguments.SelectArray(a => a.ResolveResult) : arguments.Take(firstOptionalArgumentIndex).Select(a => a.ResolveResult).ToArray(), argumentNames: argumentNames, typeArguments: typeArguments, conversions: expressionBuilder.resolver.conversions); diff --git a/ILSpy/Properties/AssemblyInfo.template.cs b/ILSpy/Properties/AssemblyInfo.template.cs index 0e431fc75..d5a2a0b61 100644 --- a/ILSpy/Properties/AssemblyInfo.template.cs +++ b/ILSpy/Properties/AssemblyInfo.template.cs @@ -42,7 +42,7 @@ internal static class RevisionClass public const string Minor = "0"; public const string Build = "0"; public const string Revision = "$INSERTREVISION$"; - public const string VersionName = "alpha1"; + public const string VersionName = "beta1"; public const string FullVersion = Major + "." + Minor + "." + Build + ".$INSERTREVISION$$INSERTBRANCHPOSTFIX$$INSERTVERSIONNAMEPOSTFIX$"; }