From 385c5e938703438b9b3707c52d150bf81546fafe Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Tue, 28 Sep 2021 23:57:47 +0300 Subject: [PATCH] Ensure valid C# for default args assigned secondary bases Signed-off-by: Dimitar Dobrev --- .../Generators/CSharp/CSharpExpressionPrinter.cs | 15 ++------------- tests/CSharp/CSharp.Tests.cs | 1 + tests/CSharp/CSharp.cpp | 8 ++++++++ tests/CSharp/CSharp.h | 2 ++ 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs b/src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs index 97c42d64..074922fc 100644 --- a/src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs @@ -31,19 +31,8 @@ namespace CppSharp.Generators.CSharp return $"({desugared.Visit(typePrinter)}) {expression}"; var finalType = (desugared.GetFinalPointee() ?? desugared).Desugar(); if (finalType.TryGetClass(out var @class) && @class.IsInterface) - { - string cast; - if (parameter.DefaultArgument.Declaration is Method method && - method.IsConstructor && method.Namespace == @class.OriginalClass) - { - cast = string.Empty; - } - else - { - cast = $"({@class.OriginalClass.Visit(typePrinter)}) "; - } - return cast + expression; - } + return $@"({@class.Visit(typePrinter)}) ({ + @class.OriginalClass.Visit(typePrinter)}) {expression}"; return expression; } diff --git a/tests/CSharp/CSharp.Tests.cs b/tests/CSharp/CSharp.Tests.cs index aa93d980..7d0de367 100644 --- a/tests/CSharp/CSharp.Tests.cs +++ b/tests/CSharp/CSharp.Tests.cs @@ -342,6 +342,7 @@ public unsafe class CSharpTests methodsWithDefaultValues.DefaultWithFreeConstantInNameSpace(); methodsWithDefaultValues.DefaultWithStdNumericLimits(10, 5); methodsWithDefaultValues.DefaultWithSpecialization(); + methodsWithDefaultValues.DefaultOverloadedImplicitCtor(); methodsWithDefaultValues.DefaultWithParamNamedSameAsMethod(5); } } diff --git a/tests/CSharp/CSharp.cpp b/tests/CSharp/CSharp.cpp index b73e68e9..d4e2d1af 100644 --- a/tests/CSharp/CSharp.cpp +++ b/tests/CSharp/CSharp.cpp @@ -822,6 +822,14 @@ void MethodsWithDefaultValues::defaultWithSpecialization(IndependentFields { } +void MethodsWithDefaultValues::defaultOverloadedImplicitCtor(P p) +{ +} + +void MethodsWithDefaultValues::defaultOverloadedImplicitCtor(Qux q) +{ +} + int MethodsWithDefaultValues::DefaultWithParamNamedSameAsMethod(int DefaultWithParamNamedSameAsMethod, const Foo& defaultArg) { return 1; diff --git a/tests/CSharp/CSharp.h b/tests/CSharp/CSharp.h index be164fc1..c1ee5f94 100644 --- a/tests/CSharp/CSharp.h +++ b/tests/CSharp/CSharp.h @@ -483,6 +483,8 @@ public: void defaultWithStdNumericLimits(double d = 1.0, int i = std::numeric_limits::infinity()); void defaultWithParamRequiringRename(_ClassWithLeadingUnderscore* ptr = nullptr); void defaultWithSpecialization(IndependentFields specialization = IndependentFields()); + void defaultOverloadedImplicitCtor(P p); + void defaultOverloadedImplicitCtor(Qux q = Qux()); int DefaultWithParamNamedSameAsMethod(int DefaultWithParamNamedSameAsMethod, const Foo& defaultArg = Foo()); int getA(); private: