Skip to content

Commit

Permalink
Fixed the binding of a copy ctor when a move ctor exists too.
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
  • Loading branch information
ddobrev committed Jun 12, 2017
1 parent 3298bc8 commit c740a34
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 15 deletions.
3 changes: 0 additions & 3 deletions src/Generator/AST/Utils.cs
Expand Up @@ -31,9 +31,6 @@ public static bool CheckIgnoreMethod(Method method)
if (@class != null && @class.IsValueType && isEmptyCtor) if (@class != null && @class.IsValueType && isEmptyCtor)
return true; return true;


if (method.IsMoveConstructor)
return true;

if (method.IsDestructor) if (method.IsDestructor)
return true; return true;


Expand Down
1 change: 1 addition & 0 deletions src/Generator/Driver.cs
Expand Up @@ -302,6 +302,7 @@ public void SetupPasses(ILibrary library)
TranslationUnitPasses.AddPass(new GenerateAnonymousDelegatesPass()); TranslationUnitPasses.AddPass(new GenerateAnonymousDelegatesPass());
TranslationUnitPasses.AddPass(new ConstructorToConversionOperatorPass()); TranslationUnitPasses.AddPass(new ConstructorToConversionOperatorPass());
TranslationUnitPasses.AddPass(new MarshalPrimitivePointersAsRefTypePass()); TranslationUnitPasses.AddPass(new MarshalPrimitivePointersAsRefTypePass());
TranslationUnitPasses.AddPass(new IgnoreMoveConstructorsPass());
TranslationUnitPasses.AddPass(new CheckAmbiguousFunctions()); TranslationUnitPasses.AddPass(new CheckAmbiguousFunctions());
TranslationUnitPasses.AddPass(new CheckOperatorsOverloadsPass()); TranslationUnitPasses.AddPass(new CheckOperatorsOverloadsPass());
TranslationUnitPasses.AddPass(new CheckVirtualOverrideReturnCovariance()); TranslationUnitPasses.AddPass(new CheckVirtualOverrideReturnCovariance());
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Generators/CSharp/CSharpSources.cs
Expand Up @@ -494,7 +494,7 @@ public void GenerateClassInternals(Class @class)
{ {
foreach (var ctor in @class.Constructors) foreach (var ctor in @class.Constructors)
{ {
if (@class.IsStatic || ctor.IsMoveConstructor) if (@class.IsStatic)
continue; continue;


if (!ctor.IsGenerated) if (!ctor.IsGenerated)
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Passes/CheckAmbiguousFunctions.cs
Expand Up @@ -33,7 +33,7 @@ public override bool VisitFunctionDecl(AST.Function function)
if (!VisitDeclaration(function)) if (!VisitDeclaration(function))
return false; return false;


if (function.IsAmbiguous) if (function.IsAmbiguous || !function.IsGenerated)
return false; return false;


var overloads = function.Namespace.GetOverloads(function); var overloads = function.Namespace.GetOverloads(function);
Expand Down
36 changes: 36 additions & 0 deletions src/Generator/Passes/IgnoreMoveConstructorsPass.cs
@@ -0,0 +1,36 @@
using CppSharp.AST;
using CppSharp.Passes;

namespace CppSharp.Passes
{
public class IgnoreMoveConstructorsPass : TranslationUnitPass
{
public IgnoreMoveConstructorsPass()
{
VisitOptions.VisitClassBases = false;
VisitOptions.VisitClassFields = false;
VisitOptions.VisitClassProperties = false;
VisitOptions.VisitEventParameters = false;
VisitOptions.VisitFunctionParameters = false;
VisitOptions.VisitFunctionReturnType = false;
VisitOptions.VisitNamespaceEnums = false;
VisitOptions.VisitNamespaceEvents = false;
VisitOptions.VisitNamespaceVariables = false;
VisitOptions.VisitTemplateArguments = false;
}

public override bool VisitMethodDecl(Method method)
{
if (!base.VisitMethodDecl(method))
return false;

if (method.IsMoveConstructor)
{
method.ExplicitlyIgnore();
return true;
}

return false;
}
}
}
34 changes: 25 additions & 9 deletions tests/Common/Common.Tests.cs
Expand Up @@ -355,15 +355,31 @@ public void TestStaticClasses()
[Test] [Test]
public void TestCopyConstructor() public void TestCopyConstructor()
{ {
Foo foo = new Foo { A = 5, B = 5.5f }; using (Foo foo = new Foo { A = 5, B = 5.5f })
var copyFoo = new Foo(foo); {
Assert.That(foo.A, Is.EqualTo(copyFoo.A)); using (var copyFoo = new Foo(foo))
Assert.That(foo.B, Is.EqualTo(copyFoo.B)); {

Assert.That(foo.A, Is.EqualTo(copyFoo.A));
var testCopyConstructorRef = new TestCopyConstructorRef { A = 10, B = 5 }; Assert.That(foo.B, Is.EqualTo(copyFoo.B));
var copyBar = new TestCopyConstructorRef(testCopyConstructorRef); }
Assert.That(testCopyConstructorRef.A, Is.EqualTo(copyBar.A)); }
Assert.That(testCopyConstructorRef.B, Is.EqualTo(copyBar.B));
using (var testCopyConstructorRef = new TestCopyConstructorRef { A = 10, B = 5 })
{
using (var copyBar = new TestCopyConstructorRef(testCopyConstructorRef))
{
Assert.That(testCopyConstructorRef.A, Is.EqualTo(copyBar.A));
Assert.That(testCopyConstructorRef.B, Is.EqualTo(copyBar.B));
}
}

using (var original = new HasCopyAndMoveConstructor(5))
{
using (var copy = new HasCopyAndMoveConstructor(original))
{
Assert.That(copy.Field, Is.EqualTo(original.Field));
}
}
} }


[Test] [Test]
Expand Down
24 changes: 24 additions & 0 deletions tests/Common/Common.cpp
Expand Up @@ -683,3 +683,27 @@ TestStaticClass& TestStaticClass::operator=(const TestStaticClass& oth)
{ {
return *this; return *this;
} }

HasCopyAndMoveConstructor::HasCopyAndMoveConstructor(int value)
{
field = value;
}

HasCopyAndMoveConstructor::HasCopyAndMoveConstructor(const HasCopyAndMoveConstructor &other)
{
field = other.field;
}

HasCopyAndMoveConstructor::HasCopyAndMoveConstructor(HasCopyAndMoveConstructor&& other)
{
field = other.field;
}

HasCopyAndMoveConstructor::~HasCopyAndMoveConstructor()
{
}

int HasCopyAndMoveConstructor::getField()
{
return field;
}
13 changes: 12 additions & 1 deletion tests/Common/Common.h
Expand Up @@ -763,7 +763,6 @@ struct DLL_API TestNestedTypes


class DLL_API HasStdString class DLL_API HasStdString
{ {
// test if these are ignored with the C# back-end
public: public:
std::string testStdString(std::string s); std::string testStdString(std::string s);
std::string s; std::string s;
Expand Down Expand Up @@ -1223,3 +1222,15 @@ enum EmptyEnum { };
enum __enum_with_underscores { lOWER_BEFORE_CAPITAL, CAPITALS_More, underscore_at_end_, usesDigits1_0 }; enum __enum_with_underscores { lOWER_BEFORE_CAPITAL, CAPITALS_More, underscore_at_end_, usesDigits1_0 };


void DLL_API sMallFollowedByCapital(); void DLL_API sMallFollowedByCapital();

class DLL_API HasCopyAndMoveConstructor
{
public:
HasCopyAndMoveConstructor(int value);
HasCopyAndMoveConstructor(const HasCopyAndMoveConstructor& other);
HasCopyAndMoveConstructor(HasCopyAndMoveConstructor&& other);
~HasCopyAndMoveConstructor();
int getField();
private:
int field;
};

0 comments on commit c740a34

Please sign in to comment.