Browse Source

Implement UsingTransform in ILAst.

pull/870/merge
Siegfried Pammer 8 years ago
parent
commit
31469c8ef1
  1. 1
      ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj
  2. 6
      ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
  3. 89
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Using.cs
  4. 321
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Using.il
  5. 260
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Using.opt.il
  6. 236
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Using.opt.roslyn.il
  7. 266
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/Using.roslyn.il
  8. 1
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
  9. 8
      ICSharpCode.Decompiler/CSharp/StatementBuilder.cs
  10. 4
      ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs
  11. 161
      ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs
  12. 1
      ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj
  13. 136
      ICSharpCode.Decompiler/IL/Transforms/UsingTransform.cs

1
ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj

@ -99,6 +99,7 @@ @@ -99,6 +99,7 @@
<Compile Include="TestCases\Pretty\QueryExpressions.cs" />
<Compile Include="TestCases\Pretty\ShortCircuit.cs" />
<Compile Include="TestCases\Pretty\TypeAnalysisTests.cs" />
<Compile Include="TestCases\Pretty\Using.cs" />
<Compile Include="TestTraceListener.cs" />
<Compile Include="Util\BitSetTests.cs" />
<Compile Include="Util\IntervalTests.cs" />

6
ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

@ -115,6 +115,12 @@ namespace ICSharpCode.Decompiler.Tests @@ -115,6 +115,12 @@ namespace ICSharpCode.Decompiler.Tests
Run(cscOptions: cscOptions);
}
[Test]
public void Using([ValueSource("defaultOptions")] CompilerOptions cscOptions)
{
Run(cscOptions: cscOptions);
}
[Test, Ignore("Not implemented")]
public void LiftedOperators([ValueSource("defaultOptions")] CompilerOptions cscOptions)
{

89
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Using.cs

@ -0,0 +1,89 @@ @@ -0,0 +1,89 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// 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.
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
internal class Using
{
[StructLayout(LayoutKind.Sequential, Size = 1)]
private struct UsingStruct : IDisposable
{
public UsingStruct(int i)
{
Console.WriteLine(i);
}
void IDisposable.Dispose()
{
throw new NotImplementedException();
}
}
#if LEGACY_CSC
public void SimpleUsingNullStatement()
{
using (null) {
Console.WriteLine("using (null)");
}
}
#endif
public void SimpleUsingExpressionStatement()
{
using (new MemoryStream()) {
Console.WriteLine("using-body");
}
}
public void SimpleUsingExpressionStatementWithDeclaration()
{
using (MemoryStream memoryStream = new MemoryStream()) {
memoryStream.WriteByte((byte)42);
Console.WriteLine("using-body: " + memoryStream.Position);
}
}
public void UsingStatementThatChangesTheVariable()
{
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
using (cancellationTokenSource) {
cancellationTokenSource = new CancellationTokenSource();
}
cancellationTokenSource.Cancel();
}
public void UsingStatementOnStruct()
{
using (new UsingStruct(1)) {
Console.WriteLine("using-body");
}
}
public void UsingStatementOnStructWithVariable()
{
using (UsingStruct usingStruct = new UsingStruct(2)) {
Console.WriteLine("using-body: " + usingStruct);
}
}
}
}

321
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Using.il

@ -0,0 +1,321 @@ @@ -0,0 +1,321 @@
// Microsoft (R) .NET Framework IL Disassembler. Version 4.0.30319.17929
// Copyright (c) Microsoft Corporation. All rights reserved.
// 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 '3l4pawob'
{
.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.
.permissionset reqmin
= {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}}
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module '3l4pawob.dll'
// MVID: {2C12BC13-2CF2-4EA9-AC06-49690D29B45C}
.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
// Image base: 0x02530000
// =============== CLASS MEMBERS DECLARATION ===================
.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using
extends [mscorlib]System.Object
{
.class sequential ansi sealed nested private beforefieldinit UsingStruct
extends [mscorlib]System.ValueType
implements [mscorlib]System.IDisposable
{
.pack 0
.size 1
.method public hidebysig specialname rtspecialname
instance void .ctor(int32 i) cil managed
{
// Code size 9 (0x9)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.1
IL_0002: call void [mscorlib]System.Console::WriteLine(int32)
IL_0007: nop
IL_0008: ret
} // end of method UsingStruct::.ctor
.method private hidebysig newslot virtual final
instance void System.IDisposable.Dispose() cil managed
{
.override [mscorlib]System.IDisposable::Dispose
// Code size 7 (0x7)
.maxstack 8
IL_0000: nop
IL_0001: newobj instance void [mscorlib]System.NotImplementedException::.ctor()
IL_0006: throw
} // end of method UsingStruct::System.IDisposable.Dispose
} // end of class UsingStruct
.method public hidebysig instance void
SimpleUsingNullStatement() cil managed
{
// Code size 36 (0x24)
.maxstack 2
.locals init (object V_0,
bool V_1)
IL_0000: nop
IL_0001: ldnull
IL_0002: stloc.0
.try
{
IL_0003: nop
IL_0004: ldstr "using (null)"
IL_0009: call void [mscorlib]System.Console::WriteLine(string)
IL_000e: nop
IL_000f: nop
IL_0010: leave.s IL_0022
} // end .try
finally
{
IL_0012: ldloc.0
IL_0013: ldnull
IL_0014: ceq
IL_0016: stloc.1
IL_0017: ldloc.1
IL_0018: brtrue.s IL_0021
IL_001a: ldnull
IL_001b: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0020: nop
IL_0021: endfinally
} // end handler
IL_0022: nop
IL_0023: ret
} // end of method Using::SimpleUsingNullStatement
.method public hidebysig instance void
SimpleUsingExpressionStatement() cil managed
{
// Code size 40 (0x28)
.maxstack 2
.locals init (class [mscorlib]System.IO.MemoryStream V_0,
bool V_1)
IL_0000: nop
IL_0001: newobj instance void [mscorlib]System.IO.MemoryStream::.ctor()
IL_0006: stloc.0
.try
{
IL_0007: nop
IL_0008: ldstr "using-body"
IL_000d: call void [mscorlib]System.Console::WriteLine(string)
IL_0012: nop
IL_0013: nop
IL_0014: leave.s IL_0026
} // end .try
finally
{
IL_0016: ldloc.0
IL_0017: ldnull
IL_0018: ceq
IL_001a: stloc.1
IL_001b: ldloc.1
IL_001c: brtrue.s IL_0025
IL_001e: ldloc.0
IL_001f: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0024: nop
IL_0025: endfinally
} // end handler
IL_0026: nop
IL_0027: ret
} // end of method Using::SimpleUsingExpressionStatement
.method public hidebysig instance void
SimpleUsingExpressionStatementWithDeclaration() cil managed
{
// Code size 65 (0x41)
.maxstack 2
.locals init (class [mscorlib]System.IO.MemoryStream V_0,
bool V_1)
IL_0000: nop
IL_0001: newobj instance void [mscorlib]System.IO.MemoryStream::.ctor()
IL_0006: stloc.0
.try
{
IL_0007: nop
IL_0008: ldloc.0
IL_0009: ldc.i4.s 42
IL_000b: callvirt instance void [mscorlib]System.IO.Stream::WriteByte(uint8)
IL_0010: nop
IL_0011: ldstr "using-body: "
IL_0016: ldloc.0
IL_0017: callvirt instance int64 [mscorlib]System.IO.Stream::get_Position()
IL_001c: box [mscorlib]System.Int64
IL_0021: call string [mscorlib]System.String::Concat(object,
object)
IL_0026: call void [mscorlib]System.Console::WriteLine(string)
IL_002b: nop
IL_002c: nop
IL_002d: leave.s IL_003f
} // end .try
finally
{
IL_002f: ldloc.0
IL_0030: ldnull
IL_0031: ceq
IL_0033: stloc.1
IL_0034: ldloc.1
IL_0035: brtrue.s IL_003e
IL_0037: ldloc.0
IL_0038: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_003d: nop
IL_003e: endfinally
} // end handler
IL_003f: nop
IL_0040: ret
} // end of method Using::SimpleUsingExpressionStatementWithDeclaration
.method public hidebysig instance void
UsingStatementThatChangesTheVariable() cil managed
{
// Code size 44 (0x2c)
.maxstack 2
.locals init (class [mscorlib]System.Threading.CancellationTokenSource V_0,
class [mscorlib]System.Threading.CancellationTokenSource V_1,
bool V_2)
IL_0000: nop
IL_0001: newobj instance void [mscorlib]System.Threading.CancellationTokenSource::.ctor()
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: stloc.1
.try
{
IL_0009: nop
IL_000a: newobj instance void [mscorlib]System.Threading.CancellationTokenSource::.ctor()
IL_000f: stloc.0
IL_0010: nop
IL_0011: leave.s IL_0023
} // end .try
finally
{
IL_0013: ldloc.1
IL_0014: ldnull
IL_0015: ceq
IL_0017: stloc.2
IL_0018: ldloc.2
IL_0019: brtrue.s IL_0022
IL_001b: ldloc.1
IL_001c: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0021: nop
IL_0022: endfinally
} // end handler
IL_0023: nop
IL_0024: ldloc.0
IL_0025: callvirt instance void [mscorlib]System.Threading.CancellationTokenSource::Cancel()
IL_002a: nop
IL_002b: ret
} // end of method Using::UsingStatementThatChangesTheVariable
.method public hidebysig instance void
UsingStatementOnStruct() cil managed
{
// Code size 40 (0x28)
.maxstack 1
.locals init (valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct V_0)
IL_0000: nop
IL_0001: ldc.i4.1
IL_0002: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct::.ctor(int32)
IL_0007: stloc.0
.try
{
IL_0008: nop
IL_0009: ldstr "using-body"
IL_000e: call void [mscorlib]System.Console::WriteLine(string)
IL_0013: nop
IL_0014: nop
IL_0015: leave.s IL_0026
} // end .try
finally
{
IL_0017: ldloca.s V_0
IL_0019: constrained. ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct
IL_001f: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0024: nop
IL_0025: endfinally
} // end handler
IL_0026: nop
IL_0027: ret
} // end of method Using::UsingStatementOnStruct
.method public hidebysig instance void
UsingStatementOnStructWithVariable() cil managed
{
// Code size 53 (0x35)
.maxstack 2
.locals init (valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct V_0)
IL_0000: nop
IL_0001: ldloca.s V_0
IL_0003: ldc.i4.2
IL_0004: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct::.ctor(int32)
IL_0009: nop
.try
{
IL_000a: nop
IL_000b: ldstr "using-body: "
IL_0010: ldloc.0
IL_0011: box ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct
IL_0016: call string [mscorlib]System.String::Concat(object,
object)
IL_001b: call void [mscorlib]System.Console::WriteLine(string)
IL_0020: nop
IL_0021: nop
IL_0022: leave.s IL_0033
} // end .try
finally
{
IL_0024: ldloca.s V_0
IL_0026: constrained. ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct
IL_002c: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0031: nop
IL_0032: endfinally
} // end handler
IL_0033: nop
IL_0034: ret
} // end of method Using::UsingStatementOnStructWithVariable
.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 Using::.ctor
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using
// =============================================================
// *********** DISASSEMBLY COMPLETE ***********************
// WARNING: Created Win32 resource file ../../../TestCases/Pretty\Using.res

260
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Using.opt.il

@ -0,0 +1,260 @@ @@ -0,0 +1,260 @@
// Microsoft (R) .NET Framework IL Disassembler. Version 4.0.30319.17929
// Copyright (c) Microsoft Corporation. All rights reserved.
// 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 uo2xsddx
{
.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.
.permissionset reqmin
= {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'SkipVerification' = bool(true)}}
.hash algorithm 0x00008004
.ver 0:0:0:0
}
.module uo2xsddx.dll
// MVID: {27F758E4-41DD-43CE-A9B2-39385F597AAF}
.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
// Image base: 0x029E0000
// =============== CLASS MEMBERS DECLARATION ===================
.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using
extends [mscorlib]System.Object
{
.class sequential ansi sealed nested private beforefieldinit UsingStruct
extends [mscorlib]System.ValueType
implements [mscorlib]System.IDisposable
{
.pack 0
.size 1
.method public hidebysig specialname rtspecialname
instance void .ctor(int32 i) cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.1
IL_0001: call void [mscorlib]System.Console::WriteLine(int32)
IL_0006: ret
} // end of method UsingStruct::.ctor
.method private hidebysig newslot virtual final
instance void System.IDisposable.Dispose() cil managed
{
.override [mscorlib]System.IDisposable::Dispose
// Code size 6 (0x6)
.maxstack 8
IL_0000: newobj instance void [mscorlib]System.NotImplementedException::.ctor()
IL_0005: throw
} // end of method UsingStruct::System.IDisposable.Dispose
} // end of class UsingStruct
.method public hidebysig instance void
SimpleUsingNullStatement() cil managed
{
// Code size 25 (0x19)
.maxstack 1
.locals init (object V_0)
IL_0000: ldnull
IL_0001: stloc.0
.try
{
IL_0002: ldstr "using (null)"
IL_0007: call void [mscorlib]System.Console::WriteLine(string)
IL_000c: leave.s IL_0018
} // end .try
finally
{
IL_000e: ldloc.0
IL_000f: brfalse.s IL_0017
IL_0011: ldnull
IL_0012: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0017: endfinally
} // end handler
IL_0018: ret
} // end of method Using::SimpleUsingNullStatement
.method public hidebysig instance void
SimpleUsingExpressionStatement() cil managed
{
// Code size 29 (0x1d)
.maxstack 1
.locals init (class [mscorlib]System.IO.MemoryStream V_0)
IL_0000: newobj instance void [mscorlib]System.IO.MemoryStream::.ctor()
IL_0005: stloc.0
.try
{
IL_0006: ldstr "using-body"
IL_000b: call void [mscorlib]System.Console::WriteLine(string)
IL_0010: leave.s IL_001c
} // end .try
finally
{
IL_0012: ldloc.0
IL_0013: brfalse.s IL_001b
IL_0015: ldloc.0
IL_0016: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_001b: endfinally
} // end handler
IL_001c: ret
} // end of method Using::SimpleUsingExpressionStatement
.method public hidebysig instance void
SimpleUsingExpressionStatementWithDeclaration() cil managed
{
// Code size 53 (0x35)
.maxstack 2
.locals init (class [mscorlib]System.IO.MemoryStream V_0)
IL_0000: newobj instance void [mscorlib]System.IO.MemoryStream::.ctor()
IL_0005: stloc.0
.try
{
IL_0006: ldloc.0
IL_0007: ldc.i4.s 42
IL_0009: callvirt instance void [mscorlib]System.IO.Stream::WriteByte(uint8)
IL_000e: ldstr "using-body: "
IL_0013: ldloc.0
IL_0014: callvirt instance int64 [mscorlib]System.IO.Stream::get_Position()
IL_0019: box [mscorlib]System.Int64
IL_001e: call string [mscorlib]System.String::Concat(object,
object)
IL_0023: call void [mscorlib]System.Console::WriteLine(string)
IL_0028: leave.s IL_0034
} // end .try
finally
{
IL_002a: ldloc.0
IL_002b: brfalse.s IL_0033
IL_002d: ldloc.0
IL_002e: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0033: endfinally
} // end handler
IL_0034: ret
} // end of method Using::SimpleUsingExpressionStatementWithDeclaration
.method public hidebysig instance void
UsingStatementThatChangesTheVariable() cil managed
{
// Code size 33 (0x21)
.maxstack 1
.locals init (class [mscorlib]System.Threading.CancellationTokenSource V_0,
class [mscorlib]System.Threading.CancellationTokenSource V_1)
IL_0000: newobj instance void [mscorlib]System.Threading.CancellationTokenSource::.ctor()
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: stloc.1
.try
{
IL_0008: newobj instance void [mscorlib]System.Threading.CancellationTokenSource::.ctor()
IL_000d: stloc.0
IL_000e: leave.s IL_001a
} // end .try
finally
{
IL_0010: ldloc.1
IL_0011: brfalse.s IL_0019
IL_0013: ldloc.1
IL_0014: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0019: endfinally
} // end handler
IL_001a: ldloc.0
IL_001b: callvirt instance void [mscorlib]System.Threading.CancellationTokenSource::Cancel()
IL_0020: ret
} // end of method Using::UsingStatementThatChangesTheVariable
.method public hidebysig instance void
UsingStatementOnStruct() cil managed
{
// Code size 34 (0x22)
.maxstack 1
.locals init (valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct V_0)
IL_0000: ldc.i4.1
IL_0001: newobj instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct::.ctor(int32)
IL_0006: stloc.0
.try
{
IL_0007: ldstr "using-body"
IL_000c: call void [mscorlib]System.Console::WriteLine(string)
IL_0011: leave.s IL_0021
} // end .try
finally
{
IL_0013: ldloca.s V_0
IL_0015: constrained. ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct
IL_001b: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0020: endfinally
} // end handler
IL_0021: ret
} // end of method Using::UsingStatementOnStruct
.method public hidebysig instance void
UsingStatementOnStructWithVariable() cil managed
{
// Code size 46 (0x2e)
.maxstack 2
.locals init (valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct V_0)
IL_0000: ldloca.s V_0
IL_0002: ldc.i4.2
IL_0003: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct::.ctor(int32)
.try
{
IL_0008: ldstr "using-body: "
IL_000d: ldloc.0
IL_000e: box ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct
IL_0013: call string [mscorlib]System.String::Concat(object,
object)
IL_0018: call void [mscorlib]System.Console::WriteLine(string)
IL_001d: leave.s IL_002d
} // end .try
finally
{
IL_001f: ldloca.s V_0
IL_0021: constrained. ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct
IL_0027: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_002c: endfinally
} // end handler
IL_002d: ret
} // end of method Using::UsingStatementOnStructWithVariable
.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 Using::.ctor
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using
// =============================================================
// *********** DISASSEMBLY COMPLETE ***********************
// WARNING: Created Win32 resource file ../../../TestCases/Pretty\Using.opt.res

236
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Using.opt.roslyn.il

@ -0,0 +1,236 @@ @@ -0,0 +1,236 @@
// Microsoft (R) .NET Framework IL Disassembler. Version 4.0.30319.17929
// Copyright (c) Microsoft Corporation. All rights reserved.
// 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 Using
{
.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 Using.dll
// MVID: {6F6E9DB6-A82C-4001-BDFB-56864E8E1A98}
.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
// Image base: 0x02870000
// =============== CLASS MEMBERS DECLARATION ===================
.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using
extends [mscorlib]System.Object
{
.class sequential ansi sealed nested private beforefieldinit UsingStruct
extends [mscorlib]System.ValueType
implements [mscorlib]System.IDisposable
{
.pack 0
.size 1
.method public hidebysig specialname rtspecialname
instance void .ctor(int32 i) cil managed
{
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.1
IL_0001: call void [mscorlib]System.Console::WriteLine(int32)
IL_0006: ret
} // end of method UsingStruct::.ctor
.method private hidebysig newslot virtual final
instance void System.IDisposable.Dispose() cil managed
{
.override [mscorlib]System.IDisposable::Dispose
// Code size 6 (0x6)
.maxstack 8
IL_0000: newobj instance void [mscorlib]System.NotImplementedException::.ctor()
IL_0005: throw
} // end of method UsingStruct::System.IDisposable.Dispose
} // end of class UsingStruct
.method public hidebysig instance void
SimpleUsingExpressionStatement() cil managed
{
// Code size 29 (0x1d)
.maxstack 1
.locals init (class [mscorlib]System.IO.MemoryStream V_0)
IL_0000: newobj instance void [mscorlib]System.IO.MemoryStream::.ctor()
IL_0005: stloc.0
.try
{
IL_0006: ldstr "using-body"
IL_000b: call void [mscorlib]System.Console::WriteLine(string)
IL_0010: leave.s IL_001c
} // end .try
finally
{
IL_0012: ldloc.0
IL_0013: brfalse.s IL_001b
IL_0015: ldloc.0
IL_0016: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_001b: endfinally
} // end handler
IL_001c: ret
} // end of method Using::SimpleUsingExpressionStatement
.method public hidebysig instance void
SimpleUsingExpressionStatementWithDeclaration() cil managed
{
// Code size 53 (0x35)
.maxstack 2
.locals init (class [mscorlib]System.IO.MemoryStream V_0)
IL_0000: newobj instance void [mscorlib]System.IO.MemoryStream::.ctor()
IL_0005: stloc.0
.try
{
IL_0006: ldloc.0
IL_0007: ldc.i4.s 42
IL_0009: callvirt instance void [mscorlib]System.IO.Stream::WriteByte(uint8)
IL_000e: ldstr "using-body: "
IL_0013: ldloc.0
IL_0014: callvirt instance int64 [mscorlib]System.IO.Stream::get_Position()
IL_0019: box [mscorlib]System.Int64
IL_001e: call string [mscorlib]System.String::Concat(object,
object)
IL_0023: call void [mscorlib]System.Console::WriteLine(string)
IL_0028: leave.s IL_0034
} // end .try
finally
{
IL_002a: ldloc.0
IL_002b: brfalse.s IL_0033
IL_002d: ldloc.0
IL_002e: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0033: endfinally
} // end handler
IL_0034: ret
} // end of method Using::SimpleUsingExpressionStatementWithDeclaration
.method public hidebysig instance void
UsingStatementThatChangesTheVariable() cil managed
{
// Code size 33 (0x21)
.maxstack 1
.locals init (class [mscorlib]System.Threading.CancellationTokenSource V_0,
class [mscorlib]System.Threading.CancellationTokenSource V_1)
IL_0000: newobj instance void [mscorlib]System.Threading.CancellationTokenSource::.ctor()
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: stloc.1
.try
{
IL_0008: newobj instance void [mscorlib]System.Threading.CancellationTokenSource::.ctor()
IL_000d: stloc.0
IL_000e: leave.s IL_001a
} // end .try
finally
{
IL_0010: ldloc.1
IL_0011: brfalse.s IL_0019
IL_0013: ldloc.1
IL_0014: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0019: endfinally
} // end handler
IL_001a: ldloc.0
IL_001b: callvirt instance void [mscorlib]System.Threading.CancellationTokenSource::Cancel()
IL_0020: ret
} // end of method Using::UsingStatementThatChangesTheVariable
.method public hidebysig instance void
UsingStatementOnStruct() cil managed
{
// Code size 35 (0x23)
.maxstack 2
.locals init (valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct V_0)
IL_0000: ldloca.s V_0
IL_0002: ldc.i4.1
IL_0003: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct::.ctor(int32)
.try
{
IL_0008: ldstr "using-body"
IL_000d: call void [mscorlib]System.Console::WriteLine(string)
IL_0012: leave.s IL_0022
} // end .try
finally
{
IL_0014: ldloca.s V_0
IL_0016: constrained. ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct
IL_001c: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0021: endfinally
} // end handler
IL_0022: ret
} // end of method Using::UsingStatementOnStruct
.method public hidebysig instance void
UsingStatementOnStructWithVariable() cil managed
{
// Code size 46 (0x2e)
.maxstack 2
.locals init (valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct V_0)
IL_0000: ldloca.s V_0
IL_0002: ldc.i4.2
IL_0003: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct::.ctor(int32)
.try
{
IL_0008: ldstr "using-body: "
IL_000d: ldloc.0
IL_000e: box ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct
IL_0013: call string [mscorlib]System.String::Concat(object,
object)
IL_0018: call void [mscorlib]System.Console::WriteLine(string)
IL_001d: leave.s IL_002d
} // end .try
finally
{
IL_001f: ldloca.s V_0
IL_0021: constrained. ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct
IL_0027: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_002c: endfinally
} // end handler
IL_002d: ret
} // end of method Using::UsingStatementOnStructWithVariable
.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 Using::.ctor
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using
// =============================================================
// *********** DISASSEMBLY COMPLETE ***********************

266
ICSharpCode.Decompiler.Tests/TestCases/Pretty/Using.roslyn.il

@ -0,0 +1,266 @@ @@ -0,0 +1,266 @@
// Microsoft (R) .NET Framework IL Disassembler. Version 4.0.30319.17929
// Copyright (c) Microsoft Corporation. All rights reserved.
// 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 Using
{
.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 Using.dll
// MVID: {1EB77740-4391-47E8-B1E6-5F7B79BDFBA6}
.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
// Image base: 0x009A0000
// =============== CLASS MEMBERS DECLARATION ===================
.class private auto ansi beforefieldinit ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using
extends [mscorlib]System.Object
{
.class sequential ansi sealed nested private beforefieldinit UsingStruct
extends [mscorlib]System.ValueType
implements [mscorlib]System.IDisposable
{
.pack 0
.size 1
.method public hidebysig specialname rtspecialname
instance void .ctor(int32 i) cil managed
{
// Code size 9 (0x9)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.1
IL_0002: call void [mscorlib]System.Console::WriteLine(int32)
IL_0007: nop
IL_0008: ret
} // end of method UsingStruct::.ctor
.method private hidebysig newslot virtual final
instance void System.IDisposable.Dispose() cil managed
{
.override [mscorlib]System.IDisposable::Dispose
// Code size 7 (0x7)
.maxstack 8
IL_0000: nop
IL_0001: newobj instance void [mscorlib]System.NotImplementedException::.ctor()
IL_0006: throw
} // end of method UsingStruct::System.IDisposable.Dispose
} // end of class UsingStruct
.method public hidebysig instance void
SimpleUsingExpressionStatement() cil managed
{
// Code size 34 (0x22)
.maxstack 1
.locals init (class [mscorlib]System.IO.MemoryStream V_0)
IL_0000: nop
IL_0001: newobj instance void [mscorlib]System.IO.MemoryStream::.ctor()
IL_0006: stloc.0
.try
{
IL_0007: nop
IL_0008: ldstr "using-body"
IL_000d: call void [mscorlib]System.Console::WriteLine(string)
IL_0012: nop
IL_0013: nop
IL_0014: leave.s IL_0021
} // end .try
finally
{
IL_0016: ldloc.0
IL_0017: brfalse.s IL_0020
IL_0019: ldloc.0
IL_001a: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_001f: nop
IL_0020: endfinally
} // end handler
IL_0021: ret
} // end of method Using::SimpleUsingExpressionStatement
.method public hidebysig instance void
SimpleUsingExpressionStatementWithDeclaration() cil managed
{
// Code size 59 (0x3b)
.maxstack 2
.locals init (class [mscorlib]System.IO.MemoryStream V_0)
IL_0000: nop
IL_0001: newobj instance void [mscorlib]System.IO.MemoryStream::.ctor()
IL_0006: stloc.0
.try
{
IL_0007: nop
IL_0008: ldloc.0
IL_0009: ldc.i4.s 42
IL_000b: callvirt instance void [mscorlib]System.IO.Stream::WriteByte(uint8)
IL_0010: nop
IL_0011: ldstr "using-body: "
IL_0016: ldloc.0
IL_0017: callvirt instance int64 [mscorlib]System.IO.Stream::get_Position()
IL_001c: box [mscorlib]System.Int64
IL_0021: call string [mscorlib]System.String::Concat(object,
object)
IL_0026: call void [mscorlib]System.Console::WriteLine(string)
IL_002b: nop
IL_002c: nop
IL_002d: leave.s IL_003a
} // end .try
finally
{
IL_002f: ldloc.0
IL_0030: brfalse.s IL_0039
IL_0032: ldloc.0
IL_0033: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0038: nop
IL_0039: endfinally
} // end handler
IL_003a: ret
} // end of method Using::SimpleUsingExpressionStatementWithDeclaration
.method public hidebysig instance void
UsingStatementThatChangesTheVariable() cil managed
{
// Code size 38 (0x26)
.maxstack 1
.locals init (class [mscorlib]System.Threading.CancellationTokenSource V_0,
class [mscorlib]System.Threading.CancellationTokenSource V_1)
IL_0000: nop
IL_0001: newobj instance void [mscorlib]System.Threading.CancellationTokenSource::.ctor()
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: stloc.1
.try
{
IL_0009: nop
IL_000a: newobj instance void [mscorlib]System.Threading.CancellationTokenSource::.ctor()
IL_000f: stloc.0
IL_0010: nop
IL_0011: leave.s IL_001e
} // end .try
finally
{
IL_0013: ldloc.1
IL_0014: brfalse.s IL_001d
IL_0016: ldloc.1
IL_0017: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_001c: nop
IL_001d: endfinally
} // end handler
IL_001e: ldloc.0
IL_001f: callvirt instance void [mscorlib]System.Threading.CancellationTokenSource::Cancel()
IL_0024: nop
IL_0025: ret
} // end of method Using::UsingStatementThatChangesTheVariable
.method public hidebysig instance void
UsingStatementOnStruct() cil managed
{
// Code size 40 (0x28)
.maxstack 2
.locals init (valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct V_0)
IL_0000: nop
IL_0001: ldloca.s V_0
IL_0003: ldc.i4.1
IL_0004: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct::.ctor(int32)
.try
{
IL_0009: nop
IL_000a: ldstr "using-body"
IL_000f: call void [mscorlib]System.Console::WriteLine(string)
IL_0014: nop
IL_0015: nop
IL_0016: leave.s IL_0027
} // end .try
finally
{
IL_0018: ldloca.s V_0
IL_001a: constrained. ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct
IL_0020: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0025: nop
IL_0026: endfinally
} // end handler
IL_0027: ret
} // end of method Using::UsingStatementOnStruct
.method public hidebysig instance void
UsingStatementOnStructWithVariable() cil managed
{
// Code size 51 (0x33)
.maxstack 2
.locals init (valuetype ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct V_0)
IL_0000: nop
IL_0001: ldloca.s V_0
IL_0003: ldc.i4.2
IL_0004: call instance void ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct::.ctor(int32)
.try
{
IL_0009: nop
IL_000a: ldstr "using-body: "
IL_000f: ldloc.0
IL_0010: box ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct
IL_0015: call string [mscorlib]System.String::Concat(object,
object)
IL_001a: call void [mscorlib]System.Console::WriteLine(string)
IL_001f: nop
IL_0020: nop
IL_0021: leave.s IL_0032
} // end .try
finally
{
IL_0023: ldloca.s V_0
IL_0025: constrained. ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using/UsingStruct
IL_002b: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_0030: nop
IL_0031: endfinally
} // end handler
IL_0032: ret
} // end of method Using::UsingStatementOnStructWithVariable
.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 Using::.ctor
} // end of class ICSharpCode.Decompiler.Tests.TestCases.Pretty.Using
// =============================================================
// *********** DISASSEMBLY COMPLETE ***********************

1
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -102,6 +102,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -102,6 +102,7 @@ namespace ICSharpCode.Decompiler.CSharp
//new UseExitPoints(),
new ConditionDetection(),
new LockTransform(),
new UsingTransform(),
// CachedDelegateInitialization must run after ConditionDetection and before/in LoopingBlockTransform
// and must run before NullCoalescingTransform
new CachedDelegateInitialization(),

8
ICSharpCode.Decompiler/CSharp/StatementBuilder.cs

@ -254,6 +254,14 @@ namespace ICSharpCode.Decompiler.CSharp @@ -254,6 +254,14 @@ namespace ICSharpCode.Decompiler.CSharp
};
}
protected internal override Statement VisitUsingInstruction(UsingInstruction inst)
{
return new UsingStatement {
ResourceAcquisition = exprBuilder.Translate(inst.ResourceExpression),
EmbeddedStatement = ConvertAsBlock(inst.Body)
};
}
protected internal override Statement VisitPinnedRegion(PinnedRegion inst)
{
var fixedStmt = new FixedStatement();

4
ICSharpCode.Decompiler/CSharp/Transforms/DeclareVariables.cs

@ -314,6 +314,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -314,6 +314,8 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
while (!(v.InsertionPoint.nextNode.Parent is BlockStatement)) {
if (v.InsertionPoint.nextNode.Parent is ForStatement f && v.InsertionPoint.nextNode == f.Initializers.FirstOrDefault() && IsMatchingAssignment(v, out _))
break;
if (v.InsertionPoint.nextNode.Parent is UsingStatement u && v.InsertionPoint.nextNode == u.ResourceAcquisition && IsMatchingAssignment(v, out _))
break;
v.InsertionPoint = v.InsertionPoint.Up();
}
@ -350,7 +352,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -350,7 +352,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
bool IsMatchingAssignment(VariableToDeclare v, out AssignmentExpression assignment)
{
assignment = (v.InsertionPoint.nextNode as ExpressionStatement)?.Expression as AssignmentExpression;
assignment = v.InsertionPoint.nextNode as AssignmentExpression ?? (v.InsertionPoint.nextNode as ExpressionStatement)?.Expression as AssignmentExpression;
Expression expectedExpr = new IdentifierExpression(v.Name);
if (v.Type.Kind == TypeKind.ByReference) {
expectedExpr = new DirectionExpression(FieldDirection.Ref, expectedExpr);

161
ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs

@ -78,9 +78,6 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -78,9 +78,6 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
result = TransformNonGenericForEach(expressionStatement);
if (result != null)
return result;
result = TransformUsings(expressionStatement);
if (result != null)
return result;
}
result = TransformFor(expressionStatement);
if (result != null)
@ -167,165 +164,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -167,165 +164,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
new NamedNode("variable", new IdentifierExpression(Pattern.AnyString)),
new AnyNode("initializer")
));
#region using
static Expression InvokeDispose(Expression identifier)
{
return new Choice {
new InvocationExpression(new MemberReferenceExpression(identifier, "Dispose")),
new InvocationExpression(new MemberReferenceExpression(new CastExpression(new TypePattern(typeof(IDisposable)), identifier.Clone()), "Dispose"))
};
}
static readonly AstNode usingTryCatchPattern = new Choice {
{ "c#/vb",
new TryCatchStatement {
TryBlock = new AnyNode(),
FinallyBlock = new BlockStatement {
new Choice {
{ "valueType",
new ExpressionStatement(InvokeDispose(new NamedNode("ident", new IdentifierExpression(Pattern.AnyString))))
},
{ "referenceType",
new IfElseStatement {
Condition = new BinaryOperatorExpression(
new NamedNode("ident", new IdentifierExpression(Pattern.AnyString)),
BinaryOperatorType.InEquality,
new NullReferenceExpression()
),
TrueStatement = new BlockStatement {
new ExpressionStatement(InvokeDispose(new Backreference("ident")))
}
}
}
}.ToStatement()
}
}
},
{ "f#",
new TryCatchStatement {
TryBlock = new AnyNode(),
FinallyBlock =
new BlockStatement {
new ExpressionStatement(
new AssignmentExpression(left: new NamedNode("disposable", new IdentifierExpression(Pattern.AnyString)),
right: new AsExpression(expression: new NamedNode("ident", new IdentifierExpression(Pattern.AnyString)),
type: new TypePattern(typeof(IDisposable))
)
)
),
new IfElseStatement {
Condition = new BinaryOperatorExpression(
new Backreference("disposable"),
BinaryOperatorType.InEquality,
new NullReferenceExpression()
),
TrueStatement = new BlockStatement {
new ExpressionStatement(InvokeDispose(new Backreference("disposable")))
}
}
}
}
}
};
public UsingStatement TransformUsings(ExpressionStatement node)
{
// Conditions:
// 1. CaptureScope of the resource-variable must be either null or the BlockContainer of the using block.
// 2. The variable must not be used outside of the block.
// 3. The variable is only assigned once, right before the try-block.
Match m1 = variableAssignPattern.Match(node);
if (!m1.Success) return null;
TryCatchStatement tryCatch = node.NextSibling as TryCatchStatement;
Match m2 = usingTryCatchPattern.Match(tryCatch);
if (!m2.Success) return null;
IL.ILVariable variable = m1.Get<IdentifierExpression>("variable").Single().GetILVariable();
string variableName = m1.Get<IdentifierExpression>("variable").Single().Identifier;
if (variable == null || variableName != m2.Get<IdentifierExpression>("ident").Single().Identifier)
return null;
if (m2.Has("valueType")) {
// if there's no if(x!=null), then it must be a value type
if (variable.Type.IsReferenceType != false)
return null;
}
if (variable.StoreCount > 1 || !variable.Type.GetAllBaseTypes().Any(t => t.IsKnownType(KnownTypeCode.IDisposable)))
return null;
//if (m2.Has("f#")) {
// string variableNameDisposable = m2.Get<IdentifierExpression>("disposable").Single().Identifier;
// VariableDeclarationStatement varDeclDisposable = FindVariableDeclaration(node, variableNameDisposable);
// if (varDeclDisposable == null || !(varDeclDisposable.Parent is BlockStatement))
// return null;
// // Validate that the variable is not used after the using statement:
// if (!IsVariableValueUnused(varDeclDisposable, tryCatch))
// return null;
//}
node.Remove();
UsingStatement usingStatement = new UsingStatement();
var tryBlock = tryCatch.TryBlock.Detach();
tryCatch.ReplaceWith(usingStatement);
// If possible, we'll eliminate the variable completely:
if (tryBlock.Descendants.OfType<IdentifierExpression>().Any(ident => ident.Identifier == variableName)) {
// variable is used, so we'll create a variable declaration
variable.Kind = IL.VariableKind.UsingLocal;
usingStatement.ResourceAcquisition = new VariableDeclarationStatement {
Type = context.Settings.AnonymousTypes && variable.Type.ContainsAnonymousType() ? new SimpleType("var") : context.TypeSystemAstBuilder.ConvertType(variable.Type),
Variables = {
new VariableInitializer {
Name = variableName,
Initializer = m1.Get<Expression>("initializer").Single().Detach()
}.CopyAnnotationsFrom(node.Expression)
.WithILVariable(variable)
}
}.CopyAnnotationsFrom(node);
} else {
// the variable is never used; eliminate it:
usingStatement.ResourceAcquisition = m1.Get<Expression>("initializer").Single().Detach();
}
usingStatement.EmbeddedStatement = tryBlock;
return usingStatement;
}
internal static VariableDeclarationStatement FindVariableDeclaration(AstNode node, string identifier)
{
while (node != null) {
while (node.PrevSibling != null) {
node = node.PrevSibling;
VariableDeclarationStatement varDecl = node as VariableDeclarationStatement;
if (varDecl != null && varDecl.Variables.Count == 1 && varDecl.Variables.Single().Name == identifier) {
return varDecl;
}
}
node = node.Parent;
}
return null;
}
/// <summary>
/// Gets whether there is an assignment to 'variableName' anywhere within the given node.
/// </summary>
bool HasAssignment(AstNode root, string variableName)
{
foreach (AstNode node in root.DescendantsAndSelf) {
IdentifierExpression ident = node as IdentifierExpression;
if (ident != null && ident.Identifier == variableName) {
if (ident.Parent is AssignmentExpression && ident.Role == AssignmentExpression.LeftRole
|| ident.Parent is DirectionExpression)
{
return true;
}
}
}
return false;
}
#endregion
#region foreach (generic)
static readonly UsingStatement genericForeachPattern = new UsingStatement {
ResourceAcquisition = new VariableDeclarationStatement {

1
ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

@ -268,6 +268,7 @@ @@ -268,6 +268,7 @@
<Compile Include="DotNetCore\DotNetCorePathFinder.cs" />
<Compile Include="DotNetCore\DotNetCorePathFinderExtensions.cs" />
<Compile Include="DotNetCore\UnresolvedAssemblyNameReference.cs" />
<Compile Include="IL\Transforms\UsingTransform.cs" />
<Compile Include="IL\ControlFlow\AsyncAwaitDecompiler.cs" />
<Compile Include="IL\ControlFlow\ControlFlowGraph.cs" />
<Compile Include="IL\ControlFlow\StateRangeAnalysis.cs" />

136
ICSharpCode.Decompiler/IL/Transforms/UsingTransform.cs

@ -0,0 +1,136 @@ @@ -0,0 +1,136 @@
// Copyright (c) 2017 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.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ICSharpCode.Decompiler.TypeSystem;
namespace ICSharpCode.Decompiler.IL.Transforms
{
class UsingTransform : IBlockTransform
{
BlockTransformContext context;
void IBlockTransform.Run(Block block, BlockTransformContext context)
{
if (!context.Settings.UsingStatement) return;
this.context = context;
for (int i = block.Instructions.Count - 1; i >= 0; i--) {
if (!TransformUsing(block, i))
continue;
// This happens in some cases:
// Use correct index after transformation.
if (i >= block.Instructions.Count)
i = block.Instructions.Count;
}
}
/// <summary>
/// stloc obj(resourceExpression)
/// .try BlockContainer {
/// Block IL_0003(incoming: 1) {
/// call WriteLine(ldstr "using (null)")
/// leave IL_0003(nop)
/// }
/// } finally BlockContainer {
/// Block IL_0012(incoming: 1) {
/// if (comp(ldloc obj != ldnull)) Block IL_001a {
/// callvirt Dispose(ldnull)
/// }
/// leave IL_0012(nop)
/// }
/// }
/// leave IL_0000(nop)
/// =>
/// using (resourceExpression) {
/// BlockContainer {
/// Block IL_0003(incoming: 1) {
/// call WriteLine(ldstr "using (null)")
/// leave IL_0003(nop)
/// }
/// }
/// }
/// </summary>
bool TransformUsing(Block block, int i)
{
if (i < 1) return false;
if (!(block.Instructions[i] is TryFinally body) || !(block.Instructions[i - 1] is StLoc storeInst))
return false;
if (!(body.FinallyBlock is BlockContainer container) || !MatchDisposeBlock(container, storeInst.Variable, storeInst.Value.MatchLdNull()))
return false;
ILInstruction resourceExpression;
if (storeInst.Variable.Type.IsReferenceType != false) {
if (storeInst.Variable.IsSingleDefinition && storeInst.Variable.LoadCount <= 2) {
resourceExpression = storeInst.Value;
} else {
resourceExpression = storeInst;
}
} else {
if (storeInst.Variable.StoreCount == 1 && storeInst.Variable.LoadCount == 0 && storeInst.Variable.AddressCount == 1) {
resourceExpression = storeInst.Value;
} else {
resourceExpression = storeInst;
}
}
context.Step("UsingTransform", body);
block.Instructions.RemoveAt(i);
block.Instructions[i - 1] = new UsingInstruction(resourceExpression, body.TryBlock);
return true;
}
bool MatchDisposeBlock(BlockContainer container, ILVariable objVar, bool usingNull)
{
var entryPoint = container.EntryPoint;
if (entryPoint.Instructions.Count != 2 || entryPoint.IncomingEdgeCount != 1)
return false;
if (!entryPoint.Instructions[1].MatchLeave(container, out var returnValue) || !returnValue.MatchNop())
return false;
CallVirt callVirt;
// reference types have a null check.
if (objVar.Type.IsReferenceType != false) {
if (!entryPoint.Instructions[0].MatchIfInstruction(out var condition, out var disposeInst))
return false;
if (!condition.MatchCompNotEquals(out var left, out var right) || !left.MatchLdLoc(objVar) || !right.MatchLdNull())
return false;
if (!(disposeInst is Block disposeBlock) || disposeBlock.Instructions.Count != 1)
return false;
if (!(disposeBlock.Instructions[0] is CallVirt cv))
return false;
callVirt = cv;
} else {
if (!(entryPoint.Instructions[0] is CallVirt cv))
return false;
callVirt = cv;
}
if (callVirt.Method.Name != "Dispose" || callVirt.Method.DeclaringType.FullName != "System.IDisposable")
return false;
if (callVirt.Method.Parameters.Count > 0)
return false;
if (callVirt.Arguments.Count != 1)
return false;
if (objVar.Type.IsReferenceType != false) {
return callVirt.Arguments[0].MatchLdLoc(objVar) || (usingNull && callVirt.Arguments[0].MatchLdNull());
} else {
return callVirt.Arguments[0].MatchLdLoca(objVar);
}
}
}
}
Loading…
Cancel
Save