Skip to content

Commit

Permalink
Fixed the generated C# for increment and decrement operators.
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
  • Loading branch information
ddobrev committed Aug 11, 2017
1 parent 910304b commit e0816d9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/AST/ClassExtensions.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static Method GetBaseMethod(this Class @class, Method @override)
if (baseMethod != null) if (baseMethod != null)
return baseMethod; return baseMethod;


return baseClass.Methods.FirstOrDefault(m => @override.CanOverride(m)); return baseClass.Methods.FirstOrDefault(@override.CanOverride);
} }


public static Property GetBaseProperty(this Class @class, Property @override, public static Property GetBaseProperty(this Class @class, Property @override,
Expand Down
8 changes: 7 additions & 1 deletion src/Generator/Passes/CheckOperatorsOverloads.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -222,7 +222,13 @@ static bool IsValidOperatorOverload(Method @operator)
// Only prefix operators can be overloaded // Only prefix operators can be overloaded
case CXXOperatorKind.PlusPlus: case CXXOperatorKind.PlusPlus:
case CXXOperatorKind.MinusMinus: case CXXOperatorKind.MinusMinus:
return @operator.Parameters.Count == 0; Class @class;
var returnType = @operator.OriginalReturnType.Type.Desugar();
returnType = (returnType.GetFinalPointee() ?? returnType).Desugar();
return returnType.TryGetClass(out @class) &&
@class.GetNonIgnoredRootBase() ==
((Class) @operator.Namespace).GetNonIgnoredRootBase() &&
@operator.Parameters.Count == 0;


// Bitwise shift operators can only be overloaded if the second parameter is int // Bitwise shift operators can only be overloaded if the second parameter is int
case CXXOperatorKind.LessLess: case CXXOperatorKind.LessLess:
Expand Down
10 changes: 10 additions & 0 deletions tests/CSharp/CSharp.Tests.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1023,6 +1023,16 @@ public void TestFuncWithTypedefedFuncPtrAsParam()
Assert.That(CSharp.CSharp.FuncWithTypedefedFuncPtrAsParam(function), Is.EqualTo(5)); Assert.That(CSharp.CSharp.FuncWithTypedefedFuncPtrAsParam(function), Is.EqualTo(5));
} }


[Test]
public void TestIncrement()
{
var bar = new Bar();
bar.Index = 5;
bar++;
Assert.That(bar.Index, Is.EqualTo(6));
bar.Dispose();
}

private class OverrideVirtualTemplate : VirtualTemplate<int> private class OverrideVirtualTemplate : VirtualTemplate<int>
{ {
public override int Function public override int Function
Expand Down
22 changes: 21 additions & 1 deletion tests/CSharp/CSharp.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ int Foo::getGetPropertyCall()
return 1; return 1;
} }


int Foo::operator ++()
{
return 5;
}

int Foo::operator --()
{
return 4;
}

const Foo& Bar::operator[](int i) const const Foo& Bar::operator[](int i) const
{ {
return m_foo; return m_foo;
Expand Down Expand Up @@ -194,10 +204,20 @@ const Bar& Bar::operator ++()
Bar Bar::operator ++(int i) Bar Bar::operator ++(int i)
{ {
Bar bar = *this; Bar bar = *this;
index++; bar.index++;
return bar; return bar;
} }


int Bar::getIndex()
{
return index;
}

void Bar::setIndex(int value)
{
index = value;
}

ForceCreationOfInterface::ForceCreationOfInterface() ForceCreationOfInterface::ForceCreationOfInterface()
{ {
} }
Expand Down
7 changes: 6 additions & 1 deletion tests/CSharp/CSharp.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class DLL_API Foo
static int propertyCall(); static int propertyCall();
static int getGetPropertyCall(); static int getGetPropertyCall();


int operator ++();
int operator --();

protected: protected:
int P; int P;
TemplateInAnotherUnit<int> templateInAnotherUnit; TemplateInAnotherUnit<int> templateInAnotherUnit;
Expand Down Expand Up @@ -82,13 +85,15 @@ class DLL_API Bar : public Qux
Bar operator++(int i); Bar operator++(int i);
void* arrayOfPrimitivePointers[1]; void* arrayOfPrimitivePointers[1];
Foo foos[4]; Foo foos[4];
int getIndex();
void setIndex(int value);


private: private:
int index; int index;
Foo m_foo; Foo m_foo;
}; };


Bar::Bar() {} Bar::Bar() : index(0) {}


class DLL_API ForceCreationOfInterface : public Foo, public Bar class DLL_API ForceCreationOfInterface : public Foo, public Bar
{ {
Expand Down

0 comments on commit e0816d9

Please sign in to comment.