Browse Source

Add support for string constant patterns.

pull/3049/head
Siegfried Pammer 3 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
} }
} }
public void RecursivePattern_StringConstant(object obj)
{
if (obj is X { B: "Hello" } x)
{
Console.WriteLine("Test " + x);
}
else
{
Console.WriteLine("not Test");
}
}
#endif #endif
private bool F() private bool F()
{ {

2
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

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

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

@ -135,12 +135,22 @@ namespace ICSharpCode.Decompiler.IL
testedOperand = comp.Left; testedOperand = comp.Left;
return IsConstant(comp.Right); return IsConstant(comp.Right);
} }
case Call call when IsCallToString_op_Equality(call):
testedOperand = call.Arguments[0];
return call.Arguments[1].OpCode == OpCode.LdStr;
default: default:
testedOperand = null; testedOperand = null;
return false; 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) private static bool IsConstant(ILInstruction inst)
{ {
return inst.OpCode switch { return inst.OpCode switch {
@ -150,7 +160,6 @@ namespace ICSharpCode.Decompiler.IL
OpCode.LdcI4 => true, OpCode.LdcI4 => true,
OpCode.LdcI8 => true, OpCode.LdcI8 => true,
OpCode.LdNull => true, OpCode.LdNull => true,
OpCode.LdStr => true,
_ => false _ => false
}; };
} }

Loading…
Cancel
Save