Skip to content

Commit

Permalink
Support overloaded operator &&/||.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrunwald committed Jul 23, 2018
1 parent aa10f00 commit 9479e8a
Show file tree
Hide file tree
Showing 23 changed files with 3,009 additions and 140 deletions.
88 changes: 0 additions & 88 deletions ICSharpCode.Decompiler.Tests/CustomShortCircuitOperators.cs

This file was deleted.

Expand Up @@ -69,6 +69,7 @@
<Compile Include="DataFlowTest.cs" />
<Compile Include="TestCases\Correctness\RefLocalsAndReturns.cs" />
<Compile Include="TestCases\Pretty\OptionalArguments.cs" />
<Compile Include="TestCases\Pretty\CustomShortCircuitOperators.cs" />
<Compile Include="TestCases\Pretty\TypeTests.cs" />
<None Include="TestCases\Disassembler\Pretty\SecurityDeclarations.il" />
<Compile Include="TestCases\Pretty\CustomAttributeConflicts.cs" />
Expand Down
6 changes: 6 additions & 0 deletions ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
Expand Up @@ -102,6 +102,12 @@ public void ShortCircuit([ValueSource("defaultOptions")] CSharpCompilerOptions c
RunForLibrary(cscOptions: cscOptions);
}

[Test]
public void CustomShortCircuitOperators([ValueSource("defaultOptions")] CSharpCompilerOptions cscOptions)
{
RunForLibrary(cscOptions: cscOptions);
}

[Test]
public void ExceptionHandling([ValueSource("defaultOptions")] CSharpCompilerOptions cscOptions)
{
Expand Down
@@ -0,0 +1,171 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// 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;

namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty.CustomShortCircuitOperators
{
internal class BaseClass
{
public static bool operator true(BaseClass x)
{
return true;
}

public static bool operator false(BaseClass x)
{
return false;
}
}

internal class C : BaseClass
{
public static C operator &(C x, C y)
{
return null;
}

public static C operator |(C x, C y)
{
return null;
}

public static C operator !(C x)
{
return x;
}

public static C GetC(int a)
{
return new C();
}

public static C LogicAnd()
{
return GetC(1) && GetC(2);
}

public static C LogicOr()
{
return GetC(1) || GetC(2);
}

public static C Complex()
{
return (GetC(1) || GetC(2)) && GetC(3) && !(GetC(4) || GetC(5));
}

private static void Main()
{
C c = new C();
C c2 = new C();
C c3 = c && c2;
C c4 = c || c2;
Console.WriteLine(c3.ToString());
Console.WriteLine(c4.ToString());
}

private static void Test2()
{
C c = new C();
if (c && c) {
Console.WriteLine(c.ToString());
}

if (!(c && c)) {
Console.WriteLine(c.ToString());
}
}

private static void Test3()
{
C c = new C();
if (c) {
Console.WriteLine(c.ToString());
}
if (!c) {
Console.WriteLine(c.ToString());
}
}
}

internal struct S
{
private readonly bool val;

public S(bool val)
{
this.val = val;
}

public static bool operator true(S x)
{
return x.val;
}

public static bool operator false(S x)
{
return x.val;
}

public static S operator &(S x, S y)
{
return new S(x.val & y.val);
}

public static S operator |(S x, S y)
{
return new S(x.val | y.val);
}

public static S operator !(S x)
{
return new S(!x.val);
}

public static S Get(int i)
{
return new S(i > 0);
}

public static S LogicAnd()
{
return Get(1) && Get(2);
}

public static S LogicOr()
{
return Get(1) || Get(2);
}

public void InConditionDetection()
{
Console.WriteLine("a");
if (Get(1) && Get(2)) {
Console.WriteLine("b");
} else {
Console.WriteLine("c");
}
if (Get(1) || Get(2)) {
Console.WriteLine("d");
} else {
Console.WriteLine("e");
}
}
}
}

0 comments on commit 9479e8a

Please sign in to comment.