Browse Source

Rework the C# type printer to return more information than just a string (CSharpTypePrinterResult).

pull/1/head
triton 12 years ago
parent
commit
128750df4c
  1. 7
      src/Bridge/Type.cs
  2. 4
      src/Generator/Generators/CLI/CLIGenerator.cs
  3. 2
      src/Generator/Generators/CLI/CLIHeadersTemplate.cs
  4. 2
      src/Generator/Generators/CLI/CLITextTemplate.cs
  5. 7
      src/Generator/Generators/CLI/CLITypePrinter.cs
  6. 4
      src/Generator/Generators/CSharp/CSharpGenerator.cs
  7. 12
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  8. 121
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  9. 7
      src/Generator/Passes/CheckAmbiguousOverloads.cs
  10. 7
      src/Generator/Types/CppTypePrinter.cs
  11. 13
      src/Generator/Types/ITypePrinter.cs

7
src/Bridge/Type.cs

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
namespace Cxxi namespace Cxxi
{ {
@ -7,7 +8,7 @@ namespace Cxxi
/// </summary> /// </summary>
public abstract class Type public abstract class Type
{ {
public static ITypeVisitor<string> TypePrinter; public static Func<Type, string> TypePrinterDelegate;
protected Type() protected Type()
{ {
@ -117,7 +118,7 @@ namespace Cxxi
public override string ToString() public override string ToString()
{ {
return Visit(TypePrinter); return TypePrinterDelegate(this);
} }
} }

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

@ -6,13 +6,13 @@ namespace Cxxi.Generators.CLI
{ {
public class CLIGenerator : Generator public class CLIGenerator : Generator
{ {
private readonly ITypePrinter typePrinter; private readonly CLITypePrinter typePrinter;
private readonly FileHashes fileHashes; private readonly FileHashes fileHashes;
public CLIGenerator(Driver driver) : base(driver) public CLIGenerator(Driver driver) : base(driver)
{ {
typePrinter = new CLITypePrinter(driver); typePrinter = new CLITypePrinter(driver);
Type.TypePrinter = typePrinter; Type.TypePrinterDelegate += type => type.Visit(typePrinter);
fileHashes = FileHashes.Load("hashes.ser"); fileHashes = FileHashes.Load("hashes.ser");
} }

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

@ -489,7 +489,7 @@ namespace Cxxi.Generators.CLI
public void GenerateFieldProperty(Field field) public void GenerateFieldProperty(Field field)
{ {
var type = field.Type.Visit(Type.TypePrinter, field.QualifiedType.Qualifiers); var type = field.Type.Visit(TypePrinter, field.QualifiedType.Qualifiers);
WriteLine("property {0} {1}", type, field.Name); WriteLine("property {0} {1}", type, field.Name);
WriteStartBraceIndent(); WriteStartBraceIndent();

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

@ -27,7 +27,7 @@ namespace Cxxi.Generators.CLI
protected const string DefaultIndent = " "; protected const string DefaultIndent = " ";
protected const uint MaxIndent = 80; protected const uint MaxIndent = 80;
public ITypePrinter TypePrinter { get; set; } public CLITypePrinter TypePrinter { get; set; }
public ISet<Include> Includes; public ISet<Include> Includes;

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

@ -17,7 +17,7 @@ namespace Cxxi.Generators.CLI
} }
} }
public class CLITypePrinter : ITypePrinter, IDeclVisitor<string> public class CLITypePrinter : ITypePrinter<string>, IDeclVisitor<string>
{ {
public Library Library { get; set; } public Library Library { get; set; }
public CLITypePrinterContext Context { get; set; } public CLITypePrinterContext Context { get; set; }
@ -307,5 +307,10 @@ namespace Cxxi.Generators.CLI
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public string ToString(Type type)
{
return type.Visit(this);
}
} }
} }

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

@ -6,12 +6,12 @@ namespace Cxxi.Generators.CSharp
{ {
public class CSharpGenerator : Generator public class CSharpGenerator : Generator
{ {
private readonly ITypePrinter typePrinter; private readonly CSharpTypePrinter typePrinter;
public CSharpGenerator(Driver driver) : base(driver) public CSharpGenerator(Driver driver) : base(driver)
{ {
typePrinter = new CSharpTypePrinter(driver.TypeDatabase, driver.Library); typePrinter = new CSharpTypePrinter(driver.TypeDatabase, driver.Library);
Type.TypePrinter = typePrinter; Type.TypePrinterDelegate += type => type.Visit(typePrinter).Type;
} }
void WriteTemplate(TextTemplate template) void WriteTemplate(TextTemplate template)

12
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -58,7 +58,7 @@ namespace Cxxi.Generators.CSharp
public class CSharpTextTemplate : TextTemplate public class CSharpTextTemplate : TextTemplate
{ {
public ITypePrinter TypePrinter { get; set; } public CSharpTypePrinter TypePrinter { get; set; }
public override string FileExtension public override string FileExtension
{ {
@ -262,7 +262,7 @@ namespace Cxxi.Generators.CSharp
public void GenerateClassInternals(Class @class) public void GenerateClassInternals(Class @class)
{ {
var typePrinter = Type.TypePrinter as CSharpTypePrinter; var typePrinter = TypePrinter as CSharpTypePrinter;
typePrinter.PushContext(CSharpTypePrinterContextKind.Native); typePrinter.PushContext(CSharpTypePrinterContextKind.Native);
WriteLine("[StructLayout(LayoutKind.Explicit, Size = {0})]", WriteLine("[StructLayout(LayoutKind.Explicit, Size = {0})]",
@ -1167,7 +1167,7 @@ namespace Cxxi.Generators.CSharp
else if (typedef.Type.IsPointerTo<FunctionType>(out function)) else if (typedef.Type.IsPointerTo<FunctionType>(out function))
{ {
WriteLine("public {0};", WriteLine("public {0};",
string.Format(TypePrinter.VisitDelegate(function), string.Format(TypePrinter.VisitDelegate(function).Type,
SafeIdentifier(typedef.Name))); SafeIdentifier(typedef.Name)));
NeedNewLine(); NeedNewLine();
} }
@ -1291,7 +1291,7 @@ namespace Cxxi.Generators.CSharp
{ {
var identifier = SafeIdentifier(function.Name); var identifier = SafeIdentifier(function.Name);
var printer = Type.TypePrinter as CSharpTypePrinter; var printer = TypePrinter as CSharpTypePrinter;
var isNativeContext = printer.ContextKind == CSharpTypePrinterContextKind.Native; var isNativeContext = printer.ContextKind == CSharpTypePrinterContextKind.Native;
var method = function as Method; var method = function as Method;
@ -1314,7 +1314,7 @@ namespace Cxxi.Generators.CSharp
public string GetFunctionNativeIdentifier(Function function, Class @class = null) public string GetFunctionNativeIdentifier(Function function, Class @class = null)
{ {
var typePrinter = Type.TypePrinter as CSharpTypePrinter; var typePrinter = TypePrinter as CSharpTypePrinter;
typePrinter.PushContext(CSharpTypePrinterContextKind.Native); typePrinter.PushContext(CSharpTypePrinterContextKind.Native);
var name = GetFunctionIdentifier(function, @class); var name = GetFunctionIdentifier(function, @class);
@ -1341,7 +1341,7 @@ namespace Cxxi.Generators.CSharp
var @params = new List<string>(); var @params = new List<string>();
var typePrinter = Type.TypePrinter as CSharpTypePrinter; var typePrinter = TypePrinter as CSharpTypePrinter;
var retType = typePrinter.VisitParameterDecl(new Parameter() var retType = typePrinter.VisitParameterDecl(new Parameter()
{ {
QualifiedType = new QualifiedType() { Type = function.ReturnType } QualifiedType = new QualifiedType() { Type = function.ReturnType }

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

@ -12,19 +12,38 @@ namespace Cxxi.Generators.CSharp
public class CSharpTypePrinterContext : TypePrinterContext public class CSharpTypePrinterContext : TypePrinterContext
{ {
public CSharpTypePrinterContextKind CSharpKind;
public CSharpTypePrinterContext() public CSharpTypePrinterContext()
{ {
} }
public CSharpTypePrinterContext(TypePrinterContextKind kind) public CSharpTypePrinterContext(TypePrinterContextKind kind,
: base(kind) CSharpTypePrinterContextKind csharpKind) : base(kind)
{ {
CSharpKind = csharpKind;
}
}
public class CSharpTypePrinterResult
{
public string Type;
public TypeMap TypeMap;
public static implicit operator CSharpTypePrinterResult(string type)
{
return new CSharpTypePrinterResult() {Type = type};
}
public override string ToString()
{
return Type;
} }
} }
public class CSharpTypePrinter : ITypePrinter, IDeclVisitor<string> public class CSharpTypePrinter : ITypePrinter<CSharpTypePrinterResult>,
IDeclVisitor<CSharpTypePrinterResult>
{ {
public Library Library { get; set; } public Library Library { get; set; }
private readonly ITypeMapDatabase TypeMapDatabase; private readonly ITypeMapDatabase TypeMapDatabase;
@ -59,7 +78,7 @@ namespace Cxxi.Generators.CSharp
return contexts.Pop(); return contexts.Pop();
} }
public string VisitTagType(TagType tag, TypeQualifiers quals) public CSharpTypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals)
{ {
if (tag.Declaration == null) if (tag.Declaration == null)
return string.Empty; return string.Empty;
@ -67,7 +86,8 @@ namespace Cxxi.Generators.CSharp
return VisitDeclaration(tag.Declaration, quals); return VisitDeclaration(tag.Declaration, quals);
} }
public string VisitArrayType(ArrayType array, TypeQualifiers quals) public CSharpTypePrinterResult VisitArrayType(ArrayType array,
TypeQualifiers quals)
{ {
return string.Format("{0}[]", array.Type.Visit(this)); return string.Format("{0}[]", array.Type.Visit(this));
@ -75,14 +95,15 @@ namespace Cxxi.Generators.CSharp
// and they are constrained to a set of built-in types. // and they are constrained to a set of built-in types.
} }
public string VisitFunctionType(FunctionType function, TypeQualifiers quals) public CSharpTypePrinterResult VisitFunctionType(FunctionType function,
TypeQualifiers quals)
{ {
var arguments = function.Parameters; var arguments = function.Parameters;
var returnType = function.ReturnType; var returnType = function.ReturnType;
var args = string.Empty; var args = string.Empty;
if (arguments.Count > 0) if (arguments.Count > 0)
args = VisitParameters(function.Parameters, hasNames: false); args = VisitParameters(function.Parameters, hasNames: false).Type;
if (returnType.IsPrimitiveType(PrimitiveType.Void)) if (returnType.IsPrimitiveType(PrimitiveType.Void))
{ {
@ -123,7 +144,8 @@ namespace Cxxi.Generators.CSharp
return IsConstCharString(pointer); return IsConstCharString(pointer);
} }
public string VisitPointerType(PointerType pointer, TypeQualifiers quals) public CSharpTypePrinterResult VisitPointerType(PointerType pointer,
TypeQualifiers quals)
{ {
var pointee = pointer.Pointee; var pointee = pointer.Pointee;
@ -148,18 +170,20 @@ namespace Cxxi.Generators.CSharp
return pointee.Visit(this, quals); return pointee.Visit(this, quals);
} }
public string VisitMemberPointerType(MemberPointerType member, public CSharpTypePrinterResult VisitMemberPointerType(
TypeQualifiers quals) MemberPointerType member, TypeQualifiers quals)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public string VisitBuiltinType(BuiltinType builtin, TypeQualifiers quals) public CSharpTypePrinterResult VisitBuiltinType(BuiltinType builtin,
TypeQualifiers quals)
{ {
return VisitPrimitiveType(builtin.Type, quals); return VisitPrimitiveType(builtin.Type, quals);
} }
public string VisitTypedefType(TypedefType typedef, TypeQualifiers quals) public CSharpTypePrinterResult VisitTypedefType(TypedefType typedef,
TypeQualifiers quals)
{ {
var decl = typedef.Declaration; var decl = typedef.Declaration;
@ -167,8 +191,16 @@ namespace Cxxi.Generators.CSharp
if (TypeMapDatabase.FindTypeMap(decl, out typeMap)) if (TypeMapDatabase.FindTypeMap(decl, out typeMap))
{ {
typeMap.Type = typedef; typeMap.Type = typedef;
var ctx = new CSharpTypePrinterContext {Type = typedef}; var ctx = new CSharpTypePrinterContext
return typeMap.CSharpSignature(ctx); {
CSharpKind = ContextKind,
Type = typedef
};
return new CSharpTypePrinterResult()
{
Type = typeMap.CSharpSignature(ctx),
TypeMap = typeMap
};
} }
FunctionType func; FunctionType func;
@ -181,8 +213,8 @@ namespace Cxxi.Generators.CSharp
return decl.Type.Visit(this); return decl.Type.Visit(this);
} }
public string VisitTemplateSpecializationType(TemplateSpecializationType template, public CSharpTypePrinterResult VisitTemplateSpecializationType(
TypeQualifiers quals) TemplateSpecializationType template, TypeQualifiers quals)
{ {
var decl = template.Template.TemplatedDecl; var decl = template.Template.TemplatedDecl;
@ -192,19 +224,24 @@ namespace Cxxi.Generators.CSharp
typeMap.Declaration = decl; typeMap.Declaration = decl;
typeMap.Type = template; typeMap.Type = template;
Context.Type = template; Context.Type = template;
return typeMap.CSharpSignature(Context); return new CSharpTypePrinterResult()
{
Type = typeMap.CSharpSignature(Context),
TypeMap = typeMap
};
} }
return decl.Name; return decl.Name;
} }
public string VisitTemplateParameterType(TemplateParameterType param, public CSharpTypePrinterResult VisitTemplateParameterType(
TypeQualifiers quals) TemplateParameterType param, TypeQualifiers quals)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public string VisitPrimitiveType(PrimitiveType primitive, TypeQualifiers quals) public CSharpTypePrinterResult VisitPrimitiveType(PrimitiveType primitive,
TypeQualifiers quals)
{ {
switch (primitive) switch (primitive)
{ {
@ -227,38 +264,39 @@ namespace Cxxi.Generators.CSharp
throw new NotSupportedException(); throw new NotSupportedException();
} }
public string VisitDeclaration(Declaration decl, TypeQualifiers quals) public CSharpTypePrinterResult VisitDeclaration(Declaration decl,
TypeQualifiers quals)
{ {
return VisitDeclaration(decl); return VisitDeclaration(decl);
} }
public string VisitDeclaration(Declaration decl) public CSharpTypePrinterResult VisitDeclaration(Declaration decl)
{ {
var name = decl.Visit(this); var name = decl.Visit(this);
return string.Format("{0}", name); return string.Format("{0}", name);
} }
public string VisitClassDecl(Class @class) public CSharpTypePrinterResult VisitClassDecl(Class @class)
{ {
return @class.Name; return @class.Name;
} }
public string VisitFieldDecl(Field field) public CSharpTypePrinterResult VisitFieldDecl(Field field)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public string VisitFunctionDecl(Function function) public CSharpTypePrinterResult VisitFunctionDecl(Function function)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public string VisitMethodDecl(Method method) public CSharpTypePrinterResult VisitMethodDecl(Method method)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public string VisitParameterDecl(Parameter parameter) public CSharpTypePrinterResult VisitParameterDecl(Parameter parameter)
{ {
var paramType = parameter.Type; var paramType = parameter.Type;
@ -272,58 +310,58 @@ namespace Cxxi.Generators.CSharp
return paramType.Visit(this); return paramType.Visit(this);
} }
public string VisitTypedefDecl(TypedefDecl typedef) public CSharpTypePrinterResult VisitTypedefDecl(TypedefDecl typedef)
{ {
return typedef.Name; return typedef.Name;
} }
public string VisitEnumDecl(Enumeration @enum) public CSharpTypePrinterResult VisitEnumDecl(Enumeration @enum)
{ {
return @enum.Name; return @enum.Name;
} }
public string VisitVariableDecl(Variable variable) public CSharpTypePrinterResult VisitVariableDecl(Variable variable)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public string VisitClassTemplateDecl(ClassTemplate template) public CSharpTypePrinterResult VisitClassTemplateDecl(ClassTemplate template)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public string VisitFunctionTemplateDecl(FunctionTemplate template) public CSharpTypePrinterResult VisitFunctionTemplateDecl(FunctionTemplate template)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public string VisitMacroDefinition(MacroDefinition macro) public CSharpTypePrinterResult VisitMacroDefinition(MacroDefinition macro)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public string VisitNamespace(Namespace @namespace) public CSharpTypePrinterResult VisitNamespace(Namespace @namespace)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public string VisitEvent(Event @event) public CSharpTypePrinterResult VisitEvent(Event @event)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public string VisitParameters(IEnumerable<Parameter> @params, public CSharpTypePrinterResult VisitParameters(IEnumerable<Parameter> @params,
bool hasNames) bool hasNames)
{ {
var args = new List<string>(); var args = new List<string>();
foreach (var param in @params) foreach (var param in @params)
args.Add(VisitParameter(param, hasNames)); args.Add(VisitParameter(param, hasNames).Type);
return string.Join(", ", args); return string.Join(", ", args);
} }
public string VisitParameter(Parameter arg, bool hasName) public CSharpTypePrinterResult 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 = Helpers.SafeIdentifier(arg.Name); var name = Helpers.SafeIdentifier(arg.Name);
@ -334,11 +372,16 @@ namespace Cxxi.Generators.CSharp
return type; return type;
} }
public string VisitDelegate(FunctionType function) public CSharpTypePrinterResult 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),
VisitParameters(function.Parameters, hasNames: true)); VisitParameters(function.Parameters, hasNames: true));
} }
public string ToString(Type type)
{
return type.Visit(this).Type;
}
} }
} }

7
src/Generator/Passes/CheckAmbiguousOverloads.cs

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Cxxi.Generators; using Cxxi.Generators;
using Cxxi.Types;
namespace Cxxi.Passes namespace Cxxi.Passes
{ {
@ -14,14 +15,12 @@ namespace Cxxi.Passes
{ {
Function = function; Function = function;
var typePrinter = Type.TypePrinter; Return = function.ReturnType.ToString();
Return = function.ReturnType.Visit(typePrinter);
Parameters = new List<string>(); Parameters = new List<string>();
foreach (var param in function.Parameters) foreach (var param in function.Parameters)
{ {
var paramType = param.Type.Visit(typePrinter); var paramType = param.Type.ToString();
Parameters.Add(paramType); Parameters.Add(paramType);
} }
} }

7
src/Generator/Types/CppTypePrinter.cs

@ -3,7 +3,7 @@ using System.Collections.Generic;
namespace Cxxi.Types namespace Cxxi.Types
{ {
public class CppTypePrinter : ITypePrinter, IDeclVisitor<string> public class CppTypePrinter : ITypePrinter<string>, IDeclVisitor<string>
{ {
public CppTypePrinter(ITypeMapDatabase database) public CppTypePrinter(ITypeMapDatabase database)
{ {
@ -225,5 +225,10 @@ namespace Cxxi.Types
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public string ToString(Type type)
{
return type.Visit(this);
}
} }
} }

13
src/Generator/Types/ITypePrinter.cs

@ -49,11 +49,16 @@ namespace Cxxi.Types
public Type Type; public Type Type;
} }
public interface ITypePrinter : ITypeVisitor<string> public interface ITypePrinter
{ {
string VisitParameters(IEnumerable<Parameter> @params, bool hasNames); string ToString(Type type);
string VisitParameter(Parameter param, bool hasName = true); }
public interface ITypePrinter<out T> : ITypePrinter, ITypeVisitor<T>
{
T VisitParameters(IEnumerable<Parameter> @params, bool hasNames);
T VisitParameter(Parameter param, bool hasName = true);
string VisitDelegate(FunctionType function); T VisitDelegate(FunctionType function);
} }
} }

Loading…
Cancel
Save