From fee1809ca572abbdcac91c8ee493bc71883c15b6 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Mon, 3 Feb 2014 18:47:49 +0200 Subject: [PATCH 1/4] Wrapped copy constructors. Signed-off-by: Dimitar Dobrev --- src/Generator/AST/Utils.cs | 2 +- .../Generators/CSharp/CSharpTextTemplate.cs | 15 +++++++++++++-- tests/Basic/Basic.Tests.cs | 14 ++++++++++++++ tests/Basic/Basic.cpp | 6 ++++++ tests/Basic/Basic.h | 1 + tests/CSharpTemp/CSharpTemp.Tests.cs | 4 ++-- 6 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/Generator/AST/Utils.cs b/src/Generator/AST/Utils.cs index 1abab2f6..8490afb8 100644 --- a/src/Generator/AST/Utils.cs +++ b/src/Generator/AST/Utils.cs @@ -25,7 +25,7 @@ namespace CppSharp.AST if (@class != null && @class.IsValueType && isEmptyCtor) return true; - if (method.IsCopyConstructor || method.IsMoveConstructor) + if (method.IsMoveConstructor) return true; if (method.IsDestructor) diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index 0f6e2fd8..0008a0d8 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -2159,8 +2159,19 @@ namespace CppSharp.Generators.CSharp WriteLine("{0} = Marshal.AllocHGlobal({1});", Helpers.InstanceIdentifier, @class.Layout.Size); - if (!method.IsDefaultConstructor || @class.HasNonTrivialDefaultConstructor) - GenerateInternalFunctionCall(method); + if (method.IsCopyConstructor) + { + if (@class.HasNonTrivialCopyConstructor) + GenerateInternalFunctionCall(method); + else + WriteLine("*(({0}.Internal*) __Instance) = *(({0}.Internal*) {1}.__Instance);", + @class.Name, method.Parameters[0].Name); + } + else + { + if (!method.IsDefaultConstructor || @class.HasNonTrivialDefaultConstructor) + GenerateInternalFunctionCall(method); + } GenerateVTableClassSetupCall(@class); } diff --git a/tests/Basic/Basic.Tests.cs b/tests/Basic/Basic.Tests.cs index 983cac43..5b52a624 100644 --- a/tests/Basic/Basic.Tests.cs +++ b/tests/Basic/Basic.Tests.cs @@ -193,5 +193,19 @@ public class BasicTests : GeneratorTestFixture public void TestChar16() { } + + [Test] + public void TestCopyConstructor() + { + Foo foo = new Foo { A = 5, B = 5.5f }; + var copyFoo = new Foo(foo); + Assert.That(foo.A, Is.EqualTo(copyFoo.A)); + Assert.That(foo.B, Is.EqualTo(copyFoo.B)); + + Bar bar = new Bar { A = 10, B = 5 }; + var copyBar = new Bar(bar); + Assert.That(bar.A, Is.EqualTo(copyBar.A)); + Assert.That(bar.B, Is.EqualTo(copyBar.B)); + } } \ No newline at end of file diff --git a/tests/Basic/Basic.cpp b/tests/Basic/Basic.cpp index 17f6850a..ed366f29 100644 --- a/tests/Basic/Basic.cpp +++ b/tests/Basic/Basic.cpp @@ -27,6 +27,12 @@ Bar::Bar() { } +Bar::Bar(const Bar& bar) +{ + A = bar.A; + B = bar.B; +} + Bar::Item Bar::RetItem1() { return Bar::Item1; diff --git a/tests/Basic/Basic.h b/tests/Basic/Basic.h index 3dcddc76..289db21a 100644 --- a/tests/Basic/Basic.h +++ b/tests/Basic/Basic.h @@ -28,6 +28,7 @@ struct DLL_API Bar }; Bar(); + Bar(const Bar& bar); Item RetItem1(); int A; float B; diff --git a/tests/CSharpTemp/CSharpTemp.Tests.cs b/tests/CSharpTemp/CSharpTemp.Tests.cs index b342413a..50de4321 100644 --- a/tests/CSharpTemp/CSharpTemp.Tests.cs +++ b/tests/CSharpTemp/CSharpTemp.Tests.cs @@ -34,7 +34,7 @@ public class CSharpTempTests : GeneratorTestFixture [Test] public void TestFixedArrays() { - Qux qux = new Qux(null); + Qux qux = new Qux((Foo) null); var array = new[] { 1, 2, 3 }; qux.Array = array; for (int i = 0; i < qux.Array.Length; i++) @@ -64,7 +64,7 @@ public class CSharpTempTests : GeneratorTestFixture Assert.That(proprietor.Value, Is.EqualTo(20)); proprietor.Prop = 50; Assert.That(proprietor.Prop, Is.EqualTo(50)); - var p = new P(null); + var p = new P((IQux) null); p.Value = 20; Assert.That(p.Value, Is.EqualTo(30)); p.Prop = 50; From 8e9d4f9c31e7b8516dfbae4edea5fab157072068 Mon Sep 17 00:00:00 2001 From: triton Date: Sat, 1 Mar 2014 16:41:16 +0000 Subject: [PATCH 2/4] Fixed generation of copy constructors for CLI value types. --- src/Generator/Generators/CLI/CLIHeadersTemplate.cs | 4 ++++ src/Generator/Generators/CLI/CLISourcesTemplate.cs | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs index c9c3e2e6..b6a50ba2 100644 --- a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs +++ b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs @@ -374,6 +374,10 @@ namespace CppSharp.Generators.CLI if (ASTUtils.CheckIgnoreMethod(ctor)) continue; + // C++/CLI does not allow special member funtions for value types. + if (@class.IsValueType && ctor.IsCopyConstructor) + continue; + GenerateMethod(ctor); } diff --git a/src/Generator/Generators/CLI/CLISourcesTemplate.cs b/src/Generator/Generators/CLI/CLISourcesTemplate.cs index 978fc56e..122fdd80 100644 --- a/src/Generator/Generators/CLI/CLISourcesTemplate.cs +++ b/src/Generator/Generators/CLI/CLISourcesTemplate.cs @@ -142,6 +142,10 @@ namespace CppSharp.Generators.CLI if (ASTUtils.CheckIgnoreMethod(method)) continue; + // C++/CLI does not allow special member funtions for value types. + if (@class.IsValueType && method.IsCopyConstructor) + continue; + GenerateMethod(method, @class); } From b761b9b66a9f4fd8d4cbd0978bffbee39a321f0e Mon Sep 17 00:00:00 2001 From: triton Date: Sat, 1 Mar 2014 16:42:49 +0000 Subject: [PATCH 3/4] Ignore constructors and destructors for static classes. --- src/Generator/Passes/CheckStaticClass.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Generator/Passes/CheckStaticClass.cs b/src/Generator/Passes/CheckStaticClass.cs index 762950fc..9dcbd298 100644 --- a/src/Generator/Passes/CheckStaticClass.cs +++ b/src/Generator/Passes/CheckStaticClass.cs @@ -1,4 +1,5 @@ using System.Linq; +using System.Threading; using CppSharp.AST; namespace CppSharp.Passes @@ -59,6 +60,14 @@ namespace CppSharp.Passes // If all the above constraints hold, then we assume the class can be // static. @class.IsStatic = true; + + // Ignore the special methods for static classes. + foreach (var ctor in @class.Constructors) + ctor.IsGenerated = false; + + foreach (var dtor in @class.Destructors) + dtor.IsGenerated = false; + return true; } } From 7477b90023625a046b49baf70ec419e75a179556 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Sat, 1 Mar 2014 19:58:35 +0200 Subject: [PATCH 4/4] Moved the tests for copy ctors to separate classes. Signed-off-by: Dimitar Dobrev --- src/Generator/Passes/CheckStaticClass.cs | 1 - tests/Basic/Basic.Tests.cs | 8 ++++---- tests/Basic/Basic.cpp | 16 ++++++++++------ tests/Basic/Basic.h | 10 +++++++++- tests/CSharpTemp/CSharpTemp.Tests.cs | 9 +++++++++ tests/CSharpTemp/CSharpTemp.cpp | 12 +++++++++++- tests/CSharpTemp/CSharpTemp.cs | 5 +++++ tests/CSharpTemp/CSharpTemp.h | 9 +++++++++ 8 files changed, 57 insertions(+), 13 deletions(-) diff --git a/src/Generator/Passes/CheckStaticClass.cs b/src/Generator/Passes/CheckStaticClass.cs index 9dcbd298..92485a08 100644 --- a/src/Generator/Passes/CheckStaticClass.cs +++ b/src/Generator/Passes/CheckStaticClass.cs @@ -1,5 +1,4 @@ using System.Linq; -using System.Threading; using CppSharp.AST; namespace CppSharp.Passes diff --git a/tests/Basic/Basic.Tests.cs b/tests/Basic/Basic.Tests.cs index 5b52a624..f55d0d90 100644 --- a/tests/Basic/Basic.Tests.cs +++ b/tests/Basic/Basic.Tests.cs @@ -202,10 +202,10 @@ public class BasicTests : GeneratorTestFixture Assert.That(foo.A, Is.EqualTo(copyFoo.A)); Assert.That(foo.B, Is.EqualTo(copyFoo.B)); - Bar bar = new Bar { A = 10, B = 5 }; - var copyBar = new Bar(bar); - Assert.That(bar.A, Is.EqualTo(copyBar.A)); - Assert.That(bar.B, Is.EqualTo(copyBar.B)); + var testCopyConstructorRef = new TestCopyConstructorRef { A = 10, B = 5 }; + var copyBar = new TestCopyConstructorRef(testCopyConstructorRef); + Assert.That(testCopyConstructorRef.A, Is.EqualTo(copyBar.A)); + Assert.That(testCopyConstructorRef.B, Is.EqualTo(copyBar.B)); } } \ No newline at end of file diff --git a/tests/Basic/Basic.cpp b/tests/Basic/Basic.cpp index ed366f29..2e266f8f 100644 --- a/tests/Basic/Basic.cpp +++ b/tests/Basic/Basic.cpp @@ -27,12 +27,6 @@ Bar::Bar() { } -Bar::Bar(const Bar& bar) -{ - A = bar.A; - B = bar.B; -} - Bar::Item Bar::RetItem1() { return Bar::Item1; @@ -223,3 +217,13 @@ Bar::Item operator |(Bar::Item left, Bar::Item right) { return left | right; } + +TestCopyConstructorRef::TestCopyConstructorRef() +{ +} + +TestCopyConstructorRef::TestCopyConstructorRef(const TestCopyConstructorRef& other) +{ + A = other.A; + B = other.B; +} diff --git a/tests/Basic/Basic.h b/tests/Basic/Basic.h index 289db21a..cdcad981 100644 --- a/tests/Basic/Basic.h +++ b/tests/Basic/Basic.h @@ -28,7 +28,6 @@ struct DLL_API Bar }; Bar(); - Bar(const Bar& bar); Item RetItem1(); int A; float B; @@ -279,3 +278,12 @@ class DependentTypeWithNestedIndependent long l; }; }; + +class DLL_API TestCopyConstructorRef +{ +public: + TestCopyConstructorRef(); + TestCopyConstructorRef(const TestCopyConstructorRef& other); + int A; + float B; +}; diff --git a/tests/CSharpTemp/CSharpTemp.Tests.cs b/tests/CSharpTemp/CSharpTemp.Tests.cs index 50de4321..5d30205f 100644 --- a/tests/CSharpTemp/CSharpTemp.Tests.cs +++ b/tests/CSharpTemp/CSharpTemp.Tests.cs @@ -107,4 +107,13 @@ public class CSharpTempTests : GeneratorTestFixture bar.ArrayOfPrimitivePointers = array; Assert.That(i, Is.EqualTo(*(int*) bar.ArrayOfPrimitivePointers[0])); } + + [Test] + public void TestCopyConstructorValue() + { + var testCopyConstructorVal = new TestCopyConstructorVal { A = 10, B = 5 }; + var copyBar = new TestCopyConstructorVal(testCopyConstructorVal); + Assert.That(testCopyConstructorVal.A, Is.EqualTo(copyBar.A)); + Assert.That(testCopyConstructorVal.B, Is.EqualTo(copyBar.B)); + } } \ No newline at end of file diff --git a/tests/CSharpTemp/CSharpTemp.cpp b/tests/CSharpTemp/CSharpTemp.cpp index b1f0cf4c..acad1852 100644 --- a/tests/CSharpTemp/CSharpTemp.cpp +++ b/tests/CSharpTemp/CSharpTemp.cpp @@ -190,4 +190,14 @@ void P::setIsBool(bool value) } -int TestDestructors::Marker = 0; \ No newline at end of file +int TestDestructors::Marker = 0; + +TestCopyConstructorVal::TestCopyConstructorVal() +{ +} + +TestCopyConstructorVal::TestCopyConstructorVal(const TestCopyConstructorVal& other) +{ + A = other.A; + B = other.B; +} diff --git a/tests/CSharpTemp/CSharpTemp.cs b/tests/CSharpTemp/CSharpTemp.cs index ea18a316..b4a662a0 100644 --- a/tests/CSharpTemp/CSharpTemp.cs +++ b/tests/CSharpTemp/CSharpTemp.cs @@ -64,6 +64,11 @@ namespace CppSharp.Tests driver.TranslationUnitPasses.AddPass(new TestAttributesPass()); } + public override void Preprocess(Driver driver, ASTContext ctx) + { + ctx.SetClassAsValueType("TestCopyConstructorVal"); + } + public override void Postprocess(Driver driver, ASTContext lib) { new CaseRenamePass( diff --git a/tests/CSharpTemp/CSharpTemp.h b/tests/CSharpTemp/CSharpTemp.h index 60b6b484..1f967826 100644 --- a/tests/CSharpTemp/CSharpTemp.h +++ b/tests/CSharpTemp/CSharpTemp.h @@ -138,3 +138,12 @@ struct DLL_API TestDestructors TestDestructors() { Marker = 0xf00d; } ~TestDestructors() { Marker = 0xcafe; } }; + +class DLL_API TestCopyConstructorVal +{ +public: + TestCopyConstructorVal(); + TestCopyConstructorVal(const TestCopyConstructorVal& other); + int A; + float B; +};