Browse Source

Generate valid C# for specializations in default args

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1635/head
Dimitar Dobrev 4 years ago
parent
commit
6dfabb6273
  1. 15
      src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs
  2. 2
      src/Generator/Passes/ExpressionHelper.cs
  3. 1
      tests/CSharp/CSharp.Tests.cs
  4. 4
      tests/CSharp/CSharp.cpp
  5. 1
      tests/CSharp/CSharp.h

15
src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs

@ -31,8 +31,19 @@ namespace CppSharp.Generators.CSharp @@ -31,8 +31,19 @@ namespace CppSharp.Generators.CSharp
return $"({desugared.Visit(typePrinter)}) {expression}";
var finalType = (desugared.GetFinalPointee() ?? desugared).Desugar();
if (finalType.TryGetClass(out var @class) && @class.IsInterface)
return $@"({@class.Visit(typePrinter)}) ({
@class.OriginalClass.Visit(typePrinter)}) {expression}";
{
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 expression;
}

2
src/Generator/Passes/ExpressionHelper.cs

@ -237,7 +237,7 @@ namespace CppSharp.Internal @@ -237,7 +237,7 @@ namespace CppSharp.Internal
var typePrinter = new CSharpTypePrinter(context);
typePrinter.PushMarshalKind(MarshalKind.DefaultExpression);
var typePrinterResult = type.Visit(typePrinter).Type;
var typePrinterResult = type.Visit(typePrinter);
TypeMap typeMap;
if (context.TypeMaps.FindTypeMap(type, out typeMap))

1
tests/CSharp/CSharp.Tests.cs

@ -341,6 +341,7 @@ public unsafe class CSharpTests @@ -341,6 +341,7 @@ public unsafe class CSharpTests
methodsWithDefaultValues.DefaultWithCharFromInt();
methodsWithDefaultValues.DefaultWithFreeConstantInNameSpace();
methodsWithDefaultValues.DefaultWithStdNumericLimits(10, 5);
methodsWithDefaultValues.DefaultWithSpecialization();
methodsWithDefaultValues.DefaultWithParamNamedSameAsMethod(5);
}
}

4
tests/CSharp/CSharp.cpp

@ -818,6 +818,10 @@ void MethodsWithDefaultValues::defaultWithParamRequiringRename(_ClassWithLeading @@ -818,6 +818,10 @@ void MethodsWithDefaultValues::defaultWithParamRequiringRename(_ClassWithLeading
{
}
void MethodsWithDefaultValues::defaultWithSpecialization(IndependentFields<int> specialization)
{
}
int MethodsWithDefaultValues::DefaultWithParamNamedSameAsMethod(int DefaultWithParamNamedSameAsMethod, const Foo& defaultArg)
{
return 1;

1
tests/CSharp/CSharp.h

@ -482,6 +482,7 @@ public: @@ -482,6 +482,7 @@ public:
void defaultWithFreeConstantInNameSpace(int c = HasFreeConstant::FREE_CONSTANT_IN_NAMESPACE);
void defaultWithStdNumericLimits(double d = 1.0, int i = std::numeric_limits<double>::infinity());
void defaultWithParamRequiringRename(_ClassWithLeadingUnderscore* ptr = nullptr);
void defaultWithSpecialization(IndependentFields<int> specialization = IndependentFields<int>());
int DefaultWithParamNamedSameAsMethod(int DefaultWithParamNamedSameAsMethod, const Foo& defaultArg = Foo());
int getA();
private:

Loading…
Cancel
Save