mirror of https://github.com/icsharpcode/ILSpy.git
148 changed files with 2993 additions and 1659 deletions
@ -0,0 +1,19 @@
@@ -0,0 +1,19 @@
|
||||
using System; |
||||
|
||||
namespace TestEnum |
||||
{ |
||||
public enum BooleanEnum : bool |
||||
{ |
||||
Min = false, |
||||
Zero = false, |
||||
One = true, |
||||
Max = byte.MaxValue |
||||
} |
||||
|
||||
public enum NativeIntEnum : IntPtr |
||||
{ |
||||
Zero = 0L, |
||||
One = 1L, |
||||
FortyTwo = 42L |
||||
} |
||||
} |
||||
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
// 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 BoolEnum |
||||
{ |
||||
.ver 1:0:0:0 |
||||
} |
||||
.module BoolEnum.exe |
||||
.imagebase 0x00400000 |
||||
.file alignment 0x00000200 |
||||
.stackreserve 0x00100000 |
||||
.subsystem 0x0003 // WINDOWS_CUI |
||||
.corflags 0x00020003 // ILONLY 32BITPREFERRED |
||||
// Image base: 0x000001C4B6C90000 |
||||
|
||||
.class public auto ansi sealed TestEnum.BooleanEnum |
||||
extends [mscorlib]System.Enum |
||||
{ |
||||
.field public specialname rtspecialname bool value__ |
||||
.field public static literal valuetype TestEnum.BooleanEnum Min = bool(false) |
||||
.field public static literal valuetype TestEnum.BooleanEnum Zero = bool(false) |
||||
.field public static literal valuetype TestEnum.BooleanEnum One = bool(true) |
||||
.field public static literal valuetype TestEnum.BooleanEnum Max = uint8(255) |
||||
} |
||||
|
||||
.class public auto ansi sealed TestEnum.NativeIntEnum |
||||
extends [mscorlib]System.Enum |
||||
{ |
||||
.field public specialname rtspecialname native int value__ |
||||
.field public static literal valuetype TestEnum.NativeIntEnum Zero = int64(0) |
||||
.field public static literal valuetype TestEnum.NativeIntEnum One = int64(1) |
||||
.field public static literal valuetype TestEnum.NativeIntEnum FortyTwo = int64(42) |
||||
} |
||||
@ -0,0 +1,65 @@
@@ -0,0 +1,65 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Threading.Tasks; |
||||
|
||||
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty |
||||
{ |
||||
public class AsyncStreams |
||||
{ |
||||
public static async IAsyncEnumerable<int> CountTo(int until) |
||||
{ |
||||
for (int i = 0; i < until; i++) { |
||||
yield return i; |
||||
await Task.Delay(10); |
||||
} |
||||
} |
||||
|
||||
public static async IAsyncEnumerable<int> AlwaysThrow() |
||||
{ |
||||
throw null; |
||||
yield break; |
||||
} |
||||
|
||||
public static async IAsyncEnumerator<int> InfiniteLoop() |
||||
{ |
||||
while (true) { |
||||
} |
||||
yield break; |
||||
} |
||||
|
||||
public static async IAsyncEnumerable<int> InfiniteLoopWithAwait() |
||||
{ |
||||
while (true) { |
||||
await Task.Delay(10); |
||||
} |
||||
yield break; |
||||
} |
||||
|
||||
public async IAsyncEnumerable<int> AwaitInFinally() |
||||
{ |
||||
try { |
||||
Console.WriteLine("try"); |
||||
yield return 1; |
||||
Console.WriteLine("end try"); |
||||
} finally { |
||||
Console.WriteLine("finally"); |
||||
await Task.Yield(); |
||||
Console.WriteLine("end finally"); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public struct TestStruct |
||||
{ |
||||
private int i; |
||||
|
||||
public async IAsyncEnumerable<int> AwaitInStruct(TestStruct xx) |
||||
{ |
||||
xx.i++; |
||||
i++; |
||||
await Task.Yield(); |
||||
yield return i; |
||||
yield return xx.i; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,60 @@
@@ -0,0 +1,60 @@
|
||||
using System; |
||||
using System.Runtime.InteropServices; |
||||
using System.Threading.Tasks; |
||||
|
||||
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty |
||||
{ |
||||
internal class AsyncUsing |
||||
{ |
||||
internal class AsyncDisposableClass : IAsyncDisposable |
||||
{ |
||||
public ValueTask DisposeAsync() |
||||
{ |
||||
throw new NotImplementedException(); |
||||
} |
||||
} |
||||
|
||||
[StructLayout(LayoutKind.Sequential, Size = 1)] |
||||
internal struct AsyncDisposableStruct : IAsyncDisposable |
||||
{ |
||||
public ValueTask DisposeAsync() |
||||
{ |
||||
throw new NotImplementedException(); |
||||
} |
||||
} |
||||
|
||||
public static async void TestAsyncUsing(IAsyncDisposable disposable) |
||||
{ |
||||
await using (disposable) { |
||||
Console.WriteLine("Hello"); |
||||
} |
||||
} |
||||
|
||||
public static async void TestAsyncUsingClass() |
||||
{ |
||||
await using (AsyncDisposableClass test = new AsyncDisposableClass()) { |
||||
Use(test); |
||||
} |
||||
} |
||||
|
||||
public static async void TestAsyncUsingStruct() |
||||
{ |
||||
await using (AsyncDisposableStruct asyncDisposableStruct = default(AsyncDisposableStruct)) { |
||||
Use(asyncDisposableStruct); |
||||
} |
||||
} |
||||
|
||||
public static async void TestAsyncUsingNullableStruct() |
||||
{ |
||||
await using (AsyncDisposableStruct? asyncDisposableStruct = new AsyncDisposableStruct?(default(AsyncDisposableStruct))) { |
||||
Use(asyncDisposableStruct); |
||||
} |
||||
} |
||||
|
||||
private static void Use(IAsyncDisposable test) |
||||
{ |
||||
throw new NotImplementedException(); |
||||
} |
||||
|
||||
} |
||||
} |
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,242 @@
@@ -0,0 +1,242 @@
|
||||
// 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.
|
||||
|
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using ICSharpCode.Decompiler.FlowAnalysis; |
||||
using ICSharpCode.Decompiler.IL.Transforms; |
||||
using ICSharpCode.Decompiler.TypeSystem; |
||||
|
||||
namespace ICSharpCode.Decompiler.IL.ControlFlow |
||||
{ |
||||
class AwaitInFinallyTransform |
||||
{ |
||||
public static void Run(ILFunction function, ILTransformContext context) |
||||
{ |
||||
if (!context.Settings.AwaitInCatchFinally) |
||||
return; |
||||
HashSet<BlockContainer> changedContainers = new HashSet<BlockContainer>(); |
||||
|
||||
// analyze all try-catch statements in the function
|
||||
foreach (var tryCatch in function.Descendants.OfType<TryCatch>().ToArray()) { |
||||
if (!(tryCatch.Parent?.Parent is BlockContainer container)) |
||||
continue; |
||||
// await in finally uses a single catch block with catch-type object
|
||||
if (tryCatch.Handlers.Count != 1 || !(tryCatch.Handlers[0].Body is BlockContainer catchBlockContainer) || !tryCatch.Handlers[0].Variable.Type.IsKnownType(KnownTypeCode.Object)) |
||||
continue; |
||||
// and consists of an assignment to a temporary that is used outside the catch block
|
||||
// and a jump to the finally block
|
||||
var block = catchBlockContainer.EntryPoint; |
||||
if (block.Instructions.Count < 2 || !block.Instructions[0].MatchStLoc(out var globalCopyVar, out var value) || !value.MatchLdLoc(tryCatch.Handlers[0].Variable)) |
||||
continue; |
||||
if (block.Instructions.Count == 3) { |
||||
if (!block.Instructions[1].MatchStLoc(out var globalCopyVarTemp, out value) || !value.MatchLdLoc(globalCopyVar)) |
||||
continue; |
||||
globalCopyVar = globalCopyVarTemp; |
||||
} |
||||
if (!block.Instructions[block.Instructions.Count - 1].MatchBranch(out var entryPointOfFinally)) |
||||
continue; |
||||
// globalCopyVar should only be used once, at the end of the finally-block
|
||||
if (globalCopyVar.LoadCount != 1 || globalCopyVar.StoreCount > 2) |
||||
continue; |
||||
var tempStore = globalCopyVar.LoadInstructions[0].Parent as StLoc; |
||||
if (tempStore == null || !MatchExceptionCaptureBlock(tempStore, out var exitOfFinally, out var afterFinally, out var blocksToRemove)) |
||||
continue; |
||||
if (!MatchAfterFinallyBlock(ref afterFinally, blocksToRemove, out bool removeFirstInstructionInAfterFinally)) |
||||
continue; |
||||
var cfg = new ControlFlowGraph(container, context.CancellationToken); |
||||
var exitOfFinallyNode = cfg.GetNode(exitOfFinally); |
||||
var entryPointOfFinallyNode = cfg.GetNode(entryPointOfFinally); |
||||
|
||||
var additionalBlocksInFinally = new HashSet<Block>(); |
||||
var invalidExits = new List<ControlFlowNode>(); |
||||
|
||||
TraverseDominatorTree(entryPointOfFinallyNode); |
||||
|
||||
void TraverseDominatorTree(ControlFlowNode node) |
||||
{ |
||||
if (entryPointOfFinallyNode != node) { |
||||
if (entryPointOfFinallyNode.Dominates(node)) |
||||
additionalBlocksInFinally.Add((Block)node.UserData); |
||||
else |
||||
invalidExits.Add(node); |
||||
} |
||||
|
||||
if (node == exitOfFinallyNode) |
||||
return; |
||||
|
||||
foreach (var child in node.DominatorTreeChildren) { |
||||
TraverseDominatorTree(child); |
||||
} |
||||
} |
||||
|
||||
if (invalidExits.Any()) |
||||
continue; |
||||
|
||||
context.Step("Inline finally block with await", tryCatch.Handlers[0]); |
||||
|
||||
foreach (var blockToRemove in blocksToRemove) { |
||||
blockToRemove.Remove(); |
||||
} |
||||
var finallyContainer = new BlockContainer(); |
||||
entryPointOfFinally.Remove(); |
||||
if (removeFirstInstructionInAfterFinally) |
||||
afterFinally.Instructions.RemoveAt(0); |
||||
changedContainers.Add(container); |
||||
var outer = BlockContainer.FindClosestContainer(container.Parent); |
||||
if (outer != null) changedContainers.Add(outer); |
||||
finallyContainer.Blocks.Add(entryPointOfFinally); |
||||
finallyContainer.AddILRange(entryPointOfFinally); |
||||
exitOfFinally.Instructions.RemoveRange(tempStore.ChildIndex, 3); |
||||
exitOfFinally.Instructions.Add(new Leave(finallyContainer)); |
||||
foreach (var branchToFinally in container.Descendants.OfType<Branch>()) { |
||||
if (branchToFinally.TargetBlock == entryPointOfFinally) |
||||
branchToFinally.ReplaceWith(new Branch(afterFinally)); |
||||
} |
||||
foreach (var newBlock in additionalBlocksInFinally) { |
||||
newBlock.Remove(); |
||||
finallyContainer.Blocks.Add(newBlock); |
||||
finallyContainer.AddILRange(newBlock); |
||||
} |
||||
tryCatch.ReplaceWith(new TryFinally(tryCatch.TryBlock, finallyContainer).WithILRange(tryCatch.TryBlock)); |
||||
} |
||||
|
||||
// clean up all modified containers
|
||||
foreach (var container in changedContainers) |
||||
container.SortBlocks(deleteUnreachableBlocks: true); |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Block finallyHead (incoming: 2) {
|
||||
/// [body of finally]
|
||||
/// stloc V_4(ldloc V_1)
|
||||
/// if (comp(ldloc V_4 == ldnull)) br afterFinally
|
||||
/// br typeCheckBlock
|
||||
/// }
|
||||
///
|
||||
/// Block typeCheckBlock (incoming: 1) {
|
||||
/// stloc S_110(isinst System.Exception(ldloc V_4))
|
||||
/// if (comp(ldloc S_110 != ldnull)) br captureBlock
|
||||
/// br throwBlock
|
||||
/// }
|
||||
///
|
||||
/// Block throwBlock (incoming: 1) {
|
||||
/// throw(ldloc V_4)
|
||||
/// }
|
||||
///
|
||||
/// Block captureBlock (incoming: 1) {
|
||||
/// callvirt Throw(call Capture(ldloc S_110))
|
||||
/// br afterFinally
|
||||
/// }
|
||||
///
|
||||
/// Block afterFinally (incoming: 2) {
|
||||
/// stloc V_1(ldnull)
|
||||
/// [after finally]
|
||||
/// }
|
||||
/// </summary>
|
||||
static bool MatchExceptionCaptureBlock(StLoc tempStore, out Block endOfFinally, out Block afterFinally, out List<Block> blocksToRemove) |
||||
{ |
||||
afterFinally = null; |
||||
endOfFinally = (Block)tempStore.Parent; |
||||
blocksToRemove = new List<Block>(); |
||||
int count = endOfFinally.Instructions.Count; |
||||
if (tempStore.ChildIndex != count - 3) |
||||
return false; |
||||
if (!(endOfFinally.Instructions[count - 2] is IfInstruction ifInst)) |
||||
return false; |
||||
if (!endOfFinally.Instructions.Last().MatchBranch(out var typeCheckBlock)) |
||||
return false; |
||||
if (!ifInst.TrueInst.MatchBranch(out afterFinally)) |
||||
return false; |
||||
// match typeCheckBlock
|
||||
if (typeCheckBlock.Instructions.Count != 3) |
||||
return false; |
||||
if (!typeCheckBlock.Instructions[0].MatchStLoc(out var castStore, out var cast) |
||||
|| !cast.MatchIsInst(out var arg, out var type) || !type.IsKnownType(KnownTypeCode.Exception) || !arg.MatchLdLoc(tempStore.Variable)) |
||||
return false; |
||||
if (!typeCheckBlock.Instructions[1].MatchIfInstruction(out var cond, out var jumpToCaptureBlock)) |
||||
return false; |
||||
if (!cond.MatchCompNotEqualsNull(out arg) || !arg.MatchLdLoc(castStore)) |
||||
return false; |
||||
if (!typeCheckBlock.Instructions[2].MatchBranch(out var throwBlock)) |
||||
return false; |
||||
if (!jumpToCaptureBlock.MatchBranch(out var captureBlock)) |
||||
return false; |
||||
// match throwBlock
|
||||
if (throwBlock.Instructions.Count != 1 || !throwBlock.Instructions[0].MatchThrow(out arg) || !arg.MatchLdLoc(tempStore.Variable)) |
||||
return false; |
||||
// match captureBlock
|
||||
if (captureBlock.Instructions.Count != 2) |
||||
return false; |
||||
if (!captureBlock.Instructions[1].MatchBranch(afterFinally)) |
||||
return false; |
||||
if (!(captureBlock.Instructions[0] is CallVirt callVirt) || callVirt.Method.FullName != "System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw" || callVirt.Arguments.Count != 1) |
||||
return false; |
||||
if (!(callVirt.Arguments[0] is Call call) || call.Method.FullName != "System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture" || call.Arguments.Count != 1) |
||||
return false; |
||||
if (!call.Arguments[0].MatchLdLoc(castStore)) |
||||
return false; |
||||
blocksToRemove.Add(typeCheckBlock); |
||||
blocksToRemove.Add(throwBlock); |
||||
blocksToRemove.Add(captureBlock); |
||||
return true; |
||||
} |
||||
|
||||
static bool MatchAfterFinallyBlock(ref Block afterFinally, List<Block> blocksToRemove, out bool removeFirstInstructionInAfterFinally) |
||||
{ |
||||
removeFirstInstructionInAfterFinally = false; |
||||
if (afterFinally.Instructions.Count < 2) |
||||
return false; |
||||
ILVariable globalCopyVarSplitted; |
||||
switch (afterFinally.Instructions[0]) { |
||||
case IfInstruction ifInst: |
||||
if (ifInst.Condition.MatchCompEquals(out var load, out var ldone) && ldone.MatchLdcI4(1) && load.MatchLdLoc(out var variable)) { |
||||
if (!ifInst.TrueInst.MatchBranch(out var targetBlock)) |
||||
return false; |
||||
blocksToRemove.Add(afterFinally); |
||||
afterFinally = targetBlock; |
||||
return true; |
||||
} else if (ifInst.Condition.MatchCompNotEquals(out load, out ldone) && ldone.MatchLdcI4(1) && load.MatchLdLoc(out variable)) { |
||||
if (!afterFinally.Instructions[1].MatchBranch(out var targetBlock)) |
||||
return false; |
||||
blocksToRemove.Add(afterFinally); |
||||
afterFinally = targetBlock; |
||||
return true; |
||||
} |
||||
return false; |
||||
case LdLoc ldLoc: |
||||
if (ldLoc.Variable.LoadCount != 1 || ldLoc.Variable.StoreCount != 1) |
||||
return false; |
||||
if (!afterFinally.Instructions[1].MatchStLoc(out globalCopyVarSplitted, out var ldnull) || !ldnull.MatchLdNull()) |
||||
return false; |
||||
removeFirstInstructionInAfterFinally = true; |
||||
break; |
||||
case StLoc stloc: |
||||
globalCopyVarSplitted = stloc.Variable; |
||||
if (!stloc.Value.MatchLdNull()) |
||||
return false; |
||||
break; |
||||
default: |
||||
return false; |
||||
} |
||||
if (globalCopyVarSplitted.StoreCount != 1 || globalCopyVarSplitted.LoadCount != 0) |
||||
return false; |
||||
return true; |
||||
} |
||||
} |
||||
} |
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue