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

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

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

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

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

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

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

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

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

11
src/Generator/Types/ITypePrinter.cs

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

Loading…
Cancel
Save