Browse Source

Use C# 'default literal' to avoid using a type's name that may be renamed (#1432)

* Use C# 'default literal' to avoid using a type's name that may be renamed
pull/1298/head
josetr 5 years ago committed by GitHub
parent
commit
df98fc460a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      build/Helpers.lua
  2. 9
      src/Generator/Passes/HandleDefaultParamValuesPass.cs
  3. 4
      tests/CSharp/CSharp.cpp
  4. 4
      tests/CSharp/CSharp.h

1
build/Helpers.lua

@ -165,6 +165,7 @@ end
function SetupManagedProject() function SetupManagedProject()
language "C#" language "C#"
location ("%{wks.location}/projects") location ("%{wks.location}/projects")
buildoptions {"/langversion:7.2"}
buildoptions {"/platform:".._OPTIONS["arch"]} buildoptions {"/platform:".._OPTIONS["arch"]}
dotnetframework "4.7.2" dotnetframework "4.7.2"

9
src/Generator/Passes/HandleDefaultParamValuesPass.cs

@ -54,7 +54,7 @@ namespace CppSharp.Passes
var result = parameter.DefaultArgument.String; var result = parameter.DefaultArgument.String;
if (PrintExpression(function, parameter.Type, if (PrintExpression(function, parameter.Type,
parameter.OriginalDefaultArgument, ref result) == null) parameter.OriginalDefaultArgument, allowDefaultLiteral: true, ref result) == null)
overloadIndices.Add(function.Parameters.IndexOf(parameter)); overloadIndices.Add(function.Parameters.IndexOf(parameter));
if (string.IsNullOrEmpty(result)) if (string.IsNullOrEmpty(result))
{ {
@ -71,7 +71,7 @@ namespace CppSharp.Passes
return true; return true;
} }
private bool? PrintExpression(Function function, Type type, ExpressionObsolete expression, ref string result) private bool? PrintExpression(Function function, Type type, ExpressionObsolete expression, bool allowDefaultLiteral, ref string result)
{ {
var desugared = type.Desugar(); var desugared = type.Desugar();
@ -79,7 +79,7 @@ namespace CppSharp.Passes
(expression.String == "0" || expression.String == "nullptr")) (expression.String == "0" || expression.String == "nullptr"))
{ {
result = desugared.GetPointee()?.Desugar() is FunctionType ? result = desugared.GetPointee()?.Desugar() is FunctionType ?
"null" : $"default({desugared})"; "null" : (allowDefaultLiteral ? "default" : $"default({desugared})");
return true; return true;
} }
@ -212,8 +212,9 @@ namespace CppSharp.Passes
{ {
var argument = ctor.Arguments[i]; var argument = ctor.Arguments[i];
var argResult = argument.String; var argResult = argument.String;
expressionSupported &= PrintExpression(method, expressionSupported &= PrintExpression(method,
method.Parameters[i].Type.Desugar(), argument, ref argResult) ?? false; method.Parameters[i].Type.Desugar(), argument, allowDefaultLiteral: false, ref argResult) ?? false;
argsBuilder.Append(argResult); argsBuilder.Append(argResult);
if (i < ctor.Arguments.Count - 1) if (i < ctor.Arguments.Count - 1)
argsBuilder.Append(", "); argsBuilder.Append(", ");

4
tests/CSharp/CSharp.cpp

@ -804,6 +804,10 @@ void MethodsWithDefaultValues::defaultWithStdNumericLimits(double d, int i)
{ {
} }
void MethodsWithDefaultValues::defaultWithParamRequiringRename(_ClassWithLeadingUnderscore* ptr)
{
}
int MethodsWithDefaultValues::DefaultWithParamNamedSameAsMethod(int DefaultWithParamNamedSameAsMethod, const Foo& defaultArg) int MethodsWithDefaultValues::DefaultWithParamNamedSameAsMethod(int DefaultWithParamNamedSameAsMethod, const Foo& defaultArg)
{ {
return 1; return 1;

4
tests/CSharp/CSharp.h

@ -407,6 +407,9 @@ enum class Empty : unsigned long long int
{ {
}; };
class _ClassWithLeadingUnderscore {
};
class DLL_API MethodsWithDefaultValues : public Quux class DLL_API MethodsWithDefaultValues : public Quux
{ {
public: public:
@ -475,6 +478,7 @@ public:
void defaultWithCharFromInt(char c = 32); void defaultWithCharFromInt(char c = 32);
void defaultWithFreeConstantInNameSpace(int c = HasFreeConstant::FREE_CONSTANT_IN_NAMESPACE); void defaultWithFreeConstantInNameSpace(int c = HasFreeConstant::FREE_CONSTANT_IN_NAMESPACE);
void defaultWithStdNumericLimits(double d = 1.0, int i = std::numeric_limits<double>::infinity()); void defaultWithStdNumericLimits(double d = 1.0, int i = std::numeric_limits<double>::infinity());
void defaultWithParamRequiringRename(_ClassWithLeadingUnderscore* ptr = nullptr);
int DefaultWithParamNamedSameAsMethod(int DefaultWithParamNamedSameAsMethod, const Foo& defaultArg = Foo()); int DefaultWithParamNamedSameAsMethod(int DefaultWithParamNamedSameAsMethod, const Foo& defaultArg = Foo());
int getA(); int getA();
private: private:

Loading…
Cancel
Save