Browse Source

Introduce BlockContainer.FindClosestSwitchContainer

pull/887/head
Siegfried Pammer 8 years ago
parent
commit
403a79099a
  1. 4
      ICSharpCode.Decompiler/CSharp/StatementBuilder.cs
  2. 10
      ICSharpCode.Decompiler/IL/Instructions/BlockContainer.cs

4
ICSharpCode.Decompiler/CSharp/StatementBuilder.cs

@ -153,7 +153,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -153,7 +153,7 @@ namespace ICSharpCode.Decompiler.CSharp
switch (section.Body) {
case Branch br:
// we can only inline the block, if all branches are in the switchContainer.
if (br.TargetBlock.Parent == switchContainer && switchContainer.Descendants.OfType<Branch>().Where(b => b.TargetBlock == br.TargetBlock).All(b => BlockContainer.FindClosestContainer(b) == switchContainer))
if (br.TargetBlock.Parent == switchContainer && switchContainer.Descendants.OfType<Branch>().Where(b => b.TargetBlock == br.TargetBlock).All(b => BlockContainer.FindClosestSwitchContainer(b) == switchContainer))
caseLabelMapping.Add(br.TargetBlock, firstValueResolveResult);
break;
default:
@ -167,7 +167,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -167,7 +167,7 @@ namespace ICSharpCode.Decompiler.CSharp
switch (section.Body) {
case Branch br:
// we can only inline the block, if all branches are in the switchContainer.
if (br.TargetBlock.Parent == switchContainer && switchContainer.Descendants.OfType<Branch>().Where(b => b.TargetBlock == br.TargetBlock).All(b => BlockContainer.FindClosestContainer(b) == switchContainer))
if (br.TargetBlock.Parent == switchContainer && switchContainer.Descendants.OfType<Branch>().Where(b => b.TargetBlock == br.TargetBlock).All(b => BlockContainer.FindClosestSwitchContainer(b) == switchContainer))
ConvertSwitchSectionBody(astSection, br.TargetBlock);
else
ConvertSwitchSectionBody(astSection, section.Body);

10
ICSharpCode.Decompiler/IL/Instructions/BlockContainer.cs

@ -226,5 +226,15 @@ namespace ICSharpCode.Decompiler.IL @@ -226,5 +226,15 @@ namespace ICSharpCode.Decompiler.IL
}
return null;
}
public static BlockContainer FindClosestSwitchContainer(ILInstruction inst)
{
while (inst != null) {
if (inst is BlockContainer bc && bc.entryPoint.Instructions.FirstOrDefault() is SwitchInstruction)
return bc;
inst = inst.Parent;
}
return null;
}
}
}

Loading…
Cancel
Save