From 33cb87ad65c9b63e16ae1c1531f3cd119a835146 Mon Sep 17 00:00:00 2001 From: Abhinav Tripathi Date: Sun, 21 Jun 2015 14:11:26 +0530 Subject: [PATCH] Added param to interface pass for Operator functions and a test. --- .../Passes/ParamTypeToInterfacePass.cs | 17 ++++--- tests/CSharpTemp/CSharpTemp.Tests.cs | 16 +++++++ tests/CSharpTemp/CSharpTemp.cpp | 46 +++++++++++++++++++ tests/CSharpTemp/CSharpTemp.h | 24 ++++++++++ 4 files changed, 96 insertions(+), 7 deletions(-) diff --git a/src/Generator/Passes/ParamTypeToInterfacePass.cs b/src/Generator/Passes/ParamTypeToInterfacePass.cs index 34f1cea2..e460fc48 100644 --- a/src/Generator/Passes/ParamTypeToInterfacePass.cs +++ b/src/Generator/Passes/ParamTypeToInterfacePass.cs @@ -1,4 +1,5 @@ -using CppSharp.AST; +using System.Linq; +using CppSharp.AST; using CppSharp.AST.Extensions; namespace CppSharp.Passes @@ -7,20 +8,22 @@ namespace CppSharp.Passes { public override bool VisitFunctionDecl(Function function) { - if (!function.IsOperator) + if (!function.IsOperator || function.Parameters.Count > 1) + ChangeToInterfaceType(function.OriginalReturnType); + + if (function.OperatorKind != CXXOperatorKind.Conversion && + function.OperatorKind != CXXOperatorKind.ExplicitConversion) { - ChangeToInterfaceType(function.ReturnType); - foreach (Parameter parameter in function.Parameters) - { + foreach (var parameter in function.Parameters.Where(p => p.Kind != ParameterKind.OperatorParameter)) ChangeToInterfaceType(parameter.QualifiedType); - } } + return base.VisitFunctionDecl(function); } private static void ChangeToInterfaceType(QualifiedType type) { - var tagType = type.Type.GetFinalPointee() as TagType; + var tagType = (type.Type.GetFinalPointee() ?? type.Type) as TagType; if (tagType != null) { var @class = tagType.Declaration as Class; diff --git a/tests/CSharpTemp/CSharpTemp.Tests.cs b/tests/CSharpTemp/CSharpTemp.Tests.cs index a0d07db2..65c17d6b 100644 --- a/tests/CSharpTemp/CSharpTemp.Tests.cs +++ b/tests/CSharpTemp/CSharpTemp.Tests.cs @@ -261,4 +261,20 @@ public class CSharpTempTests : GeneratorTestFixture } } } + + [Test] + public void TestParamTypeToInterfacePass() + { + var baseClass = new TestParamToInterfacePassBaseTwo(); + baseClass++; + Assert.AreEqual(baseClass.M, 1); + ITestParamToInterfacePassBaseTwo baseInterface = new TestParamToInterfacePassBaseTwo(); + var dervClass = new TestParamToInterfacePass(); + dervClass.AddM(baseClass); + Assert.AreEqual(dervClass.M, 1); + dervClass = new TestParamToInterfacePass(dervClass + baseClass); + Assert.AreEqual(dervClass.M, 2); + dervClass = new TestParamToInterfacePass(dervClass + baseInterface); + Assert.AreEqual(dervClass.M, 2); + } } \ No newline at end of file diff --git a/tests/CSharpTemp/CSharpTemp.cpp b/tests/CSharpTemp/CSharpTemp.cpp index be8365f2..ae6f267d 100644 --- a/tests/CSharpTemp/CSharpTemp.cpp +++ b/tests/CSharpTemp/CSharpTemp.cpp @@ -507,3 +507,49 @@ void TestOverrideFromSecondaryBase::VirtualMember() void TestOverrideFromSecondaryBase::setProperty(int value) { } + +int TestParamToInterfacePassBaseTwo::getM() +{ + return m; +} + +void TestParamToInterfacePassBaseTwo::setM(int n) +{ + m = n; +} + +const TestParamToInterfacePassBaseTwo& TestParamToInterfacePassBaseTwo::operator++() +{ + ++m; + return *this; +} + +TestParamToInterfacePassBaseTwo::TestParamToInterfacePassBaseTwo() +{ + m = 0; +} + +TestParamToInterfacePassBaseTwo::TestParamToInterfacePassBaseTwo(int n) +{ + m = n; +} + +TestParamToInterfacePassBaseTwo TestParamToInterfacePass::addM(TestParamToInterfacePassBaseTwo b) +{ + this->setM(this->getM() + b.getM()); + return *this; +} + +TestParamToInterfacePassBaseTwo TestParamToInterfacePass::operator+(TestParamToInterfacePassBaseTwo b) +{ + return TestParamToInterfacePassBaseTwo(this->getM() + b.getM()); +} + +TestParamToInterfacePass::TestParamToInterfacePass(TestParamToInterfacePassBaseTwo b) +{ + this->setM(b.getM()); +} + +TestParamToInterfacePass::TestParamToInterfacePass() : TestParamToInterfacePassBaseOne(), TestParamToInterfacePassBaseTwo() +{ +} diff --git a/tests/CSharpTemp/CSharpTemp.h b/tests/CSharpTemp/CSharpTemp.h index 1057e43b..1ffe08b4 100644 --- a/tests/CSharpTemp/CSharpTemp.h +++ b/tests/CSharpTemp/CSharpTemp.h @@ -509,3 +509,27 @@ public: void VirtualMember(); void setProperty(int value); }; + +class DLL_API TestParamToInterfacePassBaseOne +{ +}; + +class DLL_API TestParamToInterfacePassBaseTwo +{ + int m; +public: + int getM(); + void setM(int n); + const TestParamToInterfacePassBaseTwo& operator++(); + TestParamToInterfacePassBaseTwo(); + TestParamToInterfacePassBaseTwo(int n); +}; + +class DLL_API TestParamToInterfacePass : public TestParamToInterfacePassBaseOne, public TestParamToInterfacePassBaseTwo +{ +public: + TestParamToInterfacePassBaseTwo addM(TestParamToInterfacePassBaseTwo b); + TestParamToInterfacePassBaseTwo operator+(TestParamToInterfacePassBaseTwo b); + TestParamToInterfacePass(TestParamToInterfacePassBaseTwo b); + TestParamToInterfacePass(); +};