Browse Source

Add support for string constant patterns.

pull/3049/head
Siegfried Pammer 2 years ago
parent
commit
3218a06396
  1. 11
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/PatternMatching.cs
  2. 2
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  3. 11
      ICSharpCode.Decompiler/IL/Instructions/MatchInstruction.cs

11
ICSharpCode.Decompiler.Tests/TestCases/Pretty/PatternMatching.cs

@ -335,6 +335,17 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -335,6 +335,17 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}
}
public void RecursivePattern_StringConstant(object obj)
{
if (obj is X { B: "Hello" } x)
{
Console.WriteLine("Test " + x);
}
else
{
Console.WriteLine("not Test");
}
}
#endif
private bool F()
{

2
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -4641,6 +4641,8 @@ namespace ICSharpCode.Decompiler.CSharp @@ -4641,6 +4641,8 @@ namespace ICSharpCode.Decompiler.CSharp
default:
throw new InvalidOperationException("Unexpected comparison kind: " + comp.Kind);
}
case Call call when MatchInstruction.IsCallToString_op_Equality(call):
return Translate(call.Arguments[1]);
default:
throw new NotImplementedException();
}

11
ICSharpCode.Decompiler/IL/Instructions/MatchInstruction.cs

@ -135,12 +135,22 @@ namespace ICSharpCode.Decompiler.IL @@ -135,12 +135,22 @@ namespace ICSharpCode.Decompiler.IL
testedOperand = comp.Left;
return IsConstant(comp.Right);
}
case Call call when IsCallToString_op_Equality(call):
testedOperand = call.Arguments[0];
return call.Arguments[1].OpCode == OpCode.LdStr;
default:
testedOperand = null;
return false;
}
}
internal static bool IsCallToString_op_Equality(Call call)
{
return call.Method.IsOperator && call.Method.Name == "op_Equality"
&& call.Method.DeclaringType.IsKnownType(KnownTypeCode.String)
&& call.Arguments.Count == 2;
}
private static bool IsConstant(ILInstruction inst)
{
return inst.OpCode switch {
@ -150,7 +160,6 @@ namespace ICSharpCode.Decompiler.IL @@ -150,7 +160,6 @@ namespace ICSharpCode.Decompiler.IL
OpCode.LdcI4 => true,
OpCode.LdcI8 => true,
OpCode.LdNull => true,
OpCode.LdStr => true,
_ => false
};
}

Loading…
Cancel
Save