Browse Source

Refactored the parameter type printing code to be more re-usable.

pull/1/head
triton 13 years ago
parent
commit
3d1612f747
  1. 4
      src/Generator/Generators/CLI/CLIHeadersTemplate.cs
  2. 2
      src/Generator/Generators/CLI/CLISourcesTemplate.cs
  3. 2
      src/Generator/Generators/CLI/CLITextTemplate.cs
  4. 25
      src/Generator/Generators/CLI/CLITypePrinter.cs
  5. 25
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  6. 11
      src/Generator/Types/ITypePrinter.cs

4
src/Generator/Generators/CLI/CLIHeadersTemplate.cs

@ -394,7 +394,7 @@ namespace Cxxi.Generators.CLI @@ -394,7 +394,7 @@ namespace Cxxi.Generators.CLI
if (typedef.Type.IsPointerTo<FunctionType>(out function))
{
WriteLine("public {0};",
string.Format(TypePrinter.ToDelegateString(function),
string.Format(TypePrinter.VisitDelegate(function),
SafeIdentifier(typedef.Name)));
return true;
}
@ -421,7 +421,7 @@ namespace Cxxi.Generators.CLI @@ -421,7 +421,7 @@ namespace Cxxi.Generators.CLI
for (int i = 0; i < function.Parameters.Count; ++i)
{
var param = function.Parameters[i];
Write("{0}", TypePrinter.GetArgumentString(param));
Write("{0}", TypePrinter.VisitParameter(param));
if (i < function.Parameters.Count - 1)
Write(", ");
}

2
src/Generator/Generators/CLI/CLISourcesTemplate.cs

@ -268,7 +268,7 @@ namespace Cxxi.Generators.CLI @@ -268,7 +268,7 @@ namespace Cxxi.Generators.CLI
for (var i = 0; i < function.Parameters.Count; ++i)
{
var param = function.Parameters[i];
Write("{0}", TypePrinter.GetArgumentString(param));
Write("{0}", TypePrinter.VisitParameter(param));
if (i < function.Parameters.Count - 1)
Write(", ");
}

2
src/Generator/Generators/CLI/CLITextTemplate.cs

@ -81,7 +81,7 @@ namespace Cxxi.Generators.CLI @@ -81,7 +81,7 @@ namespace Cxxi.Generators.CLI
continue;
var param = method.Parameters[i];
Write("{0}", TypePrinter.GetArgumentString(param));
Write("{0}", TypePrinter.VisitParameter(param));
if (i < method.Parameters.Count - 1)
Write(", ");
}

25
src/Generator/Generators/CLI/CLITypePrinter.cs

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Cxxi.Types;
namespace Cxxi.Generators.CLI
@ -36,7 +37,7 @@ namespace Cxxi.Generators.CLI @@ -36,7 +37,7 @@ namespace Cxxi.Generators.CLI
var args = string.Empty;
if (arguments.Count > 0)
args = GetArgumentsString(function, hasNames: false);
args = VisitParameters(function.Arguments, hasNames: false);
if (returnType.IsPrimitiveType(PrimitiveType.Void))
{
@ -51,22 +52,18 @@ namespace Cxxi.Generators.CLI @@ -51,22 +52,18 @@ namespace Cxxi.Generators.CLI
return string.Format("System::Func<{0}{1}>", returnType.Visit(this), args);
}
public string GetArgumentsString(FunctionType function, bool hasNames)
public string VisitParameters(IEnumerable<Parameter> @params,
bool hasNames)
{
var arguments = function.Arguments;
var s = string.Empty;
var args = new List<string>();
for (var i = 0; i < arguments.Count; ++i)
{
s += GetArgumentString(arguments[i], hasNames);
if (i < arguments.Count - 1)
s += ", ";
}
foreach (var param in @params)
args.Add(VisitParameter(param, hasNames));
return s;
return string.Join(" ,", args);
}
public string GetArgumentString(Parameter param, bool hasName = true)
public string VisitParameter(Parameter param, bool hasName = true)
{
var type = param.Type.Visit(this, param.QualifiedType.Qualifiers);
var name = param.Name;
@ -77,11 +74,11 @@ namespace Cxxi.Generators.CLI @@ -77,11 +74,11 @@ namespace Cxxi.Generators.CLI
return type;
}
public string ToDelegateString(FunctionType function)
public string VisitDelegate(FunctionType function)
{
return string.Format("delegate {0} {{0}}({1})",
function.ReturnType.Visit(this),
GetArgumentsString(function, hasNames: true));
VisitParameters(function.Arguments, hasNames: true));
}
public string VisitPointerType(PointerType pointer, TypeQualifiers quals)

25
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Cxxi.Types;
namespace Cxxi.Generators.CSharp
@ -37,7 +38,7 @@ namespace Cxxi.Generators.CSharp @@ -37,7 +38,7 @@ namespace Cxxi.Generators.CSharp
var args = string.Empty;
if (arguments.Count > 0)
args = GetArgumentsString(function, hasNames: false);
args = VisitParameters(function.Arguments, hasNames: false);
if (returnType.IsPrimitiveType(PrimitiveType.Void))
{
@ -210,22 +211,18 @@ namespace Cxxi.Generators.CSharp @@ -210,22 +211,18 @@ namespace Cxxi.Generators.CSharp
throw new NotImplementedException();
}
public string GetArgumentsString(FunctionType function, bool hasNames)
public string VisitParameters(IEnumerable<Parameter> @params,
bool hasNames)
{
var arguments = function.Arguments;
var s = string.Empty;
var args = new List<string>();
for (var i = 0; i < arguments.Count; ++i)
{
s += GetArgumentString(arguments[i], hasNames);
if (i < arguments.Count - 1)
s += ", ";
}
foreach (var param in @params)
args.Add(VisitParameter(param, hasNames));
return s;
return string.Join(" ,", args);
}
public string GetArgumentString(Parameter arg, bool hasName)
public string VisitParameter(Parameter arg, bool hasName)
{
var type = arg.Type.Visit(this, arg.QualifiedType.Qualifiers);
var name = arg.Name;
@ -236,11 +233,11 @@ namespace Cxxi.Generators.CSharp @@ -236,11 +233,11 @@ namespace Cxxi.Generators.CSharp
return type;
}
public string ToDelegateString(FunctionType function)
public string VisitDelegate(FunctionType function)
{
return string.Format("delegate {0} {{0}}({1})",
function.ReturnType.Visit(this),
GetArgumentsString(function, hasNames: true));
VisitParameters(function.Arguments, hasNames: true));
}
}
}

11
src/Generator/Types/ITypePrinter.cs

@ -1,11 +1,14 @@ @@ -1,11 +1,14 @@
namespace Cxxi.Types
using System.Collections.Generic;
namespace Cxxi.Types
{
public interface ITypePrinter : ITypeVisitor<string>
{
Library Library { get; set; }
string GetArgumentsString(FunctionType function, bool hasNames);
string GetArgumentString(Parameter arg, bool hasName = true);
string ToDelegateString(FunctionType function);
string VisitParameters(IEnumerable<Parameter> @params, bool hasNames);
string VisitParameter(Parameter param, bool hasName = true);
string VisitDelegate(FunctionType function);
}
}

Loading…
Cancel
Save