Browse Source

Generate valid C# for variables which use float

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1641/head
Dimitar Dobrev 4 years ago
parent
commit
132ed7e25b
  1. 17
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 12
      src/Generator/Passes/ExpressionHelper.cs
  3. 4
      tests/Common/Common.h

17
src/Generator/Generators/CSharp/CSharpSources.cs

@ -1218,12 +1218,19 @@ namespace CppSharp.Generators.CSharp @@ -1218,12 +1218,19 @@ namespace CppSharp.Generators.CSharp
}
else
{
var systemType = Internal.ExpressionHelper.GetSystemType(Context, type);
if (!Internal.ExpressionHelper.TryParseExactLiteralExpression(ref initializerString, systemType))
Write(type.IsPrimitiveType() || type.IsEnum() ? $"({type}) {initializerString}" :
$"new {type}({initializerString})");
else
if (Internal.ExpressionHelper.PrintExpression(Context, null, type,
variable.Initializer, false, ref initializerString) == null)
{
Write(initializerString);
}
else
{
var systemType = Internal.ExpressionHelper.GetSystemType(Context, type);
if (!Internal.ExpressionHelper.TryParseExactLiteralExpression(ref initializerString, systemType))
Write($"({type}) {initializerString}");
else
Write(initializerString);
}
}
WriteLine(";");
}

12
src/Generator/Passes/ExpressionHelper.cs

@ -1,12 +1,10 @@ @@ -1,12 +1,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using CppSharp.AST;
using CppSharp.AST.Extensions;
using CppSharp.Generators;
using CppSharp.Generators.CSharp;
using CppSharp.Parser;
using CppSharp.Types;
using TypeCode = System.TypeCode;
@ -40,8 +38,7 @@ namespace CppSharp.Internal @@ -40,8 +38,7 @@ namespace CppSharp.Internal
if (desugared.IsPrimitiveTypeConvertibleToRef())
{
var method = function as Method;
if (method != null && method.IsConstructor)
if (function is Method method && method.IsConstructor)
{
result = string.Empty;
return false;
@ -282,7 +279,8 @@ namespace CppSharp.Internal @@ -282,7 +279,8 @@ namespace CppSharp.Internal
var method = (Method)expression.Declaration;
var expressionSupported = decl.IsValueType && method.Parameters.Count == 0;
if (expression.String.Contains('(') || expression.String.StartsWith("{"))
if (expression.String.Contains('(') || expression.String.StartsWith("{") ||
ctor.Arguments.Count > 1)
{
var argsBuilder = new StringBuilder("new ");
argsBuilder.Append(typePrinterResult);
@ -312,14 +310,14 @@ namespace CppSharp.Internal @@ -312,14 +310,14 @@ namespace CppSharp.Internal
switch (builtin.Type)
{
case PrimitiveType.Float:
if (statement.String.EndsWith(".F", System.StringComparison.Ordinal))
if (statement.String.EndsWith(".F", System.StringComparison.OrdinalIgnoreCase))
{
result = statement.String.Replace(".F", ".0F");
return true;
}
break;
case PrimitiveType.Double:
if (statement.String.EndsWith(".", System.StringComparison.Ordinal))
if (statement.String.EndsWith(".", System.StringComparison.OrdinalIgnoreCase))
{
result = statement.String + '0';
return true;

4
tests/Common/Common.h

@ -491,13 +491,13 @@ namespace SomeNamespace @@ -491,13 +491,13 @@ namespace SomeNamespace
class Inlines
{
public:
constexpr Inlines(int param, const char* name) {}
constexpr Inlines(float param, const char* name) {}
inline operator NamespacedAbstractImpl () const { return NamespacedAbstractImpl(); }
protected:
void protectedInlined() {}
};
constexpr Inlines constWithParams(5, "test");
constexpr Inlines constWithParams(5.f, "test");
class AbstractInlines
{

Loading…
Cancel
Save