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
} }
else else
{ {
var systemType = Internal.ExpressionHelper.GetSystemType(Context, type); if (Internal.ExpressionHelper.PrintExpression(Context, null, type,
if (!Internal.ExpressionHelper.TryParseExactLiteralExpression(ref initializerString, systemType)) variable.Initializer, false, ref initializerString) == null)
Write(type.IsPrimitiveType() || type.IsEnum() ? $"({type}) {initializerString}" : {
$"new {type}({initializerString})");
else
Write(initializerString); Write(initializerString);
}
else
{
var systemType = Internal.ExpressionHelper.GetSystemType(Context, type);
if (!Internal.ExpressionHelper.TryParseExactLiteralExpression(ref initializerString, systemType))
Write($"({type}) {initializerString}");
else
Write(initializerString);
}
} }
WriteLine(";"); WriteLine(";");
} }

12
src/Generator/Passes/ExpressionHelper.cs

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

4
tests/Common/Common.h

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

Loading…
Cancel
Save