Skip to content

Commit 94de3a9

Browse files
Move LocalFunctionDecompiler.GetStatement to Block.GetContainingStatement
1 parent 8cb9469 commit 94de3a9

File tree

6 files changed

+22
-33
lines changed

6 files changed

+22
-33
lines changed

ICSharpCode.Decompiler/IL/ControlFlow/AwaitInFinallyTransform.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@
1616
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1717
// DEALINGS IN THE SOFTWARE.
1818

19-
using System;
2019
using System.Collections.Generic;
2120
using System.Diagnostics;
2221
using System.Linq;
2322

24-
using ICSharpCode.Decompiler.FlowAnalysis;
2523
using ICSharpCode.Decompiler.IL.Transforms;
2624
using ICSharpCode.Decompiler.TypeSystem;
2725

@@ -109,7 +107,7 @@ public static void Run(ILFunction function, ILTransformContext context)
109107
if (objectVariable.LoadCount != 1 || objectVariable.StoreCount > 2)
110108
continue;
111109

112-
var beforeExceptionCaptureBlock = (Block)LocalFunctionDecompiler.GetStatement(objectVariable.LoadInstructions[0])?.Parent;
110+
var beforeExceptionCaptureBlock = Block.FindClosestBlock(objectVariable.LoadInstructions[0]);
113111
if (beforeExceptionCaptureBlock == null)
114112
continue;
115113

ICSharpCode.Decompiler/IL/Instructions/Block.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,22 @@ public void RunTransforms(IEnumerable<IBlockTransform> transforms, BlockTransfor
383383
return null;
384384
}
385385

386+
/// <summary>
387+
/// Gets the closest ancestor that is child of a control-flow (top-level) Block.
388+
/// Returns null, if the instruction is not a descendant of a Block.
389+
/// </summary>
390+
public static ILInstruction? GetContainingStatement(ILInstruction inst)
391+
{
392+
var curr = inst;
393+
while (curr != null)
394+
{
395+
if (curr.Parent is Block { Kind: BlockKind.ControlFlow })
396+
return curr;
397+
curr = curr.Parent;
398+
}
399+
return null;
400+
}
401+
386402
public bool MatchInlineAssignBlock([NotNullWhen(true)] out CallInstruction? call, [NotNullWhen(true)] out ILInstruction? value)
387403
{
388404
call = null;

ICSharpCode.Decompiler/IL/Transforms/DetectCatchWhenConditionBlocks.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1717
// DEALINGS IN THE SOFTWARE.
1818

19-
using System;
20-
using System.Collections.Generic;
21-
using System.Diagnostics;
2219
using System.Linq;
2320

2421
using ICSharpCode.Decompiler.TypeSystem;
@@ -107,7 +104,7 @@ static void PropagateExceptionVariable(ILTransformContext context, TryCatchHandl
107104

108105
// We are only interested in store "statements" copying the exception variable
109106
// without modifying it.
110-
var statement = LocalFunctionDecompiler.GetStatement(load);
107+
var statement = Block.GetContainingStatement(load);
111108
if (!(statement is StLoc stloc))
112109
{
113110
i++;

ICSharpCode.Decompiler/IL/Transforms/ExpressionTransforms.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -312,25 +312,14 @@ protected internal override void VisitCallVirt(CallVirt inst)
312312

313313
protected internal override void VisitNewObj(NewObj inst)
314314
{
315-
Block block;
316315
if (TransformSpanTCtorContainingStackAlloc(inst, out ILInstruction locallocSpan))
317316
{
318317
context.Step("new Span<T>(stackalloc) -> stackalloc Span<T>", inst);
319318
inst.ReplaceWith(locallocSpan);
320-
block = null;
321-
ILInstruction stmt = locallocSpan;
322-
while (stmt.Parent != null)
323-
{
324-
if (stmt.Parent is Block b)
325-
{
326-
block = b;
327-
break;
328-
}
329-
stmt = stmt.Parent;
330-
}
319+
ILInstruction stmt = Block.GetContainingStatement(locallocSpan);
331320
// Special case to eliminate extra store
332321
if (stmt.GetNextSibling() is StLoc storeStmt && storeStmt.Value is LdLoc)
333-
ILInlining.InlineIfPossible(block, stmt.ChildIndex, context);
322+
ILInlining.InlineIfPossible((Block)stmt.Parent, stmt.ChildIndex, context);
334323
return;
335324
}
336325
if (TransformArrayInitializers.TransformSpanTArrayInitialization(inst, context, out var replacement))

ICSharpCode.Decompiler/IL/Transforms/LocalFunctionDecompiler.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -575,17 +575,6 @@ internal static bool IsClosureParameter(IParameter parameter, ITypeResolveContex
575575
&& TransformDisplayClassUsage.IsPotentialClosure(context.CurrentTypeDefinition, type);
576576
}
577577

578-
internal static ILInstruction GetStatement(ILInstruction inst)
579-
{
580-
while (inst.Parent != null)
581-
{
582-
if (inst.Parent is Block b && b.Kind == BlockKind.ControlFlow)
583-
return inst;
584-
inst = inst.Parent;
585-
}
586-
return inst;
587-
}
588-
589578
LocalFunctionMethod ReduceToLocalFunction(IMethod method, int typeParametersToRemove)
590579
{
591580
int parametersToRemove = 0;
@@ -747,7 +736,7 @@ ILInstruction GetClosureInitializer(ILVariable variable)
747736
if (variable.Kind == VariableKind.Parameter)
748737
return null;
749738
if (type.Kind == TypeKind.Struct)
750-
return GetStatement(variable.AddressInstructions.OrderBy(i => i.StartILOffset).First());
739+
return Block.GetContainingStatement(variable.AddressInstructions.OrderBy(i => i.StartILOffset).First());
751740
else
752741
return (StLoc)variable.StoreInstructions[0];
753742
}

ICSharpCode.Decompiler/IL/Transforms/TransformDisplayClassUsage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ internal static bool IsClosure(ILTransformContext context, ILVariable variable,
604604
if (context.Settings.LocalFunctions && closureType?.Kind == TypeKind.Struct
605605
&& variable.UsesInitialValue && IsPotentialClosure(context, closureType))
606606
{
607-
initializer = LocalFunctionDecompiler.GetStatement(variable.AddressInstructions.OrderBy(i => i.StartILOffset).First());
607+
initializer = Block.GetContainingStatement(variable.AddressInstructions.OrderBy(i => i.StartILOffset).First());
608608
return true;
609609
}
610610
return false;

0 commit comments

Comments
 (0)