Browse Source

Add support for generation of default argument expressions in C generator.

For now this only uses the C# code, needs some future work.
pull/1580/head
Joao Matos 4 years ago
parent
commit
b659e5cc06
  1. 21
      src/Generator/Generators/C/CppTypePrinter.cs
  2. 4
      src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs

21
src/Generator/Generators/C/CppTypePrinter.cs

@ -3,6 +3,7 @@ using System.Collections.Generic; @@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using CppSharp.AST;
using CppSharp.AST.Extensions;
using CppSharp.Generators.CSharp;
using CppSharp.Types;
namespace CppSharp.Generators.C
@ -21,6 +22,7 @@ namespace CppSharp.Generators.C @@ -21,6 +22,7 @@ namespace CppSharp.Generators.C
public bool PrintTypeQualifiers { get; set; }
public bool PrintTypeModifiers { get; set; }
public bool PrintVariableArrayAsPointers { get; set; }
public TypePrintScopeKind MethodScopeKind = TypePrintScopeKind.Qualified;
public CppTypePrinter(BindingContext context) : base(TypePrinterContextKind.Native)
@ -421,7 +423,24 @@ namespace CppSharp.Generators.C @@ -421,7 +423,24 @@ namespace CppSharp.Generators.C
return result;
result.Name = param.Name;
return result.ToString();
if (param.DefaultArgument != null && Options.GenerateDefaultValuesForArguments)
{
try
{
var expressionPrinter = new CSharpExpressionPrinter(this);
var defaultValue = expressionPrinter.VisitParameter(param);
return $"{result} = {defaultValue}";
}
catch (Exception)
{
var function = param.Namespace as Function;
Diagnostics.Warning($"Error printing default argument expression: " +
$"{function.QualifiedOriginalName}({param.OriginalName})");
}
}
return $"{result}";
}
public override TypePrinterResult VisitDelegate(FunctionType function)

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

@ -16,7 +16,7 @@ namespace CppSharp.Generators.CSharp @@ -16,7 +16,7 @@ namespace CppSharp.Generators.CSharp
public class CSharpExpressionPrinter : IExpressionPrinter<string>
{
public CSharpExpressionPrinter(CSharpTypePrinter typePrinter)
public CSharpExpressionPrinter(TypePrinter typePrinter)
{
this.typePrinter = typePrinter;
}
@ -87,6 +87,6 @@ namespace CppSharp.Generators.CSharp @@ -87,6 +87,6 @@ namespace CppSharp.Generators.CSharp
throw new System.NotImplementedException();
}
private readonly CSharpTypePrinter typePrinter;
private readonly TypePrinter typePrinter;
}
}
Loading…
Cancel
Save