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/1581/head
Joao Matos 5 years ago committed by João Matos
parent
commit
494419de7b
  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;
using System.Linq; using System.Linq;
using CppSharp.AST; using CppSharp.AST;
using CppSharp.AST.Extensions; using CppSharp.AST.Extensions;
using CppSharp.Generators.CSharp;
using CppSharp.Types; using CppSharp.Types;
namespace CppSharp.Generators.C namespace CppSharp.Generators.C
@ -21,6 +22,7 @@ namespace CppSharp.Generators.C
public bool PrintTypeQualifiers { get; set; } public bool PrintTypeQualifiers { get; set; }
public bool PrintTypeModifiers { get; set; } public bool PrintTypeModifiers { get; set; }
public bool PrintVariableArrayAsPointers { get; set; } public bool PrintVariableArrayAsPointers { get; set; }
public TypePrintScopeKind MethodScopeKind = TypePrintScopeKind.Qualified; public TypePrintScopeKind MethodScopeKind = TypePrintScopeKind.Qualified;
public CppTypePrinter(BindingContext context) : base(TypePrinterContextKind.Native) public CppTypePrinter(BindingContext context) : base(TypePrinterContextKind.Native)
@ -421,7 +423,24 @@ namespace CppSharp.Generators.C
return result; return result;
result.Name = param.Name; 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) public override TypePrinterResult VisitDelegate(FunctionType function)

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

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