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. 14
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  8. 123
      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 @@ @@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
namespace Cxxi
{
@ -7,7 +8,7 @@ namespace Cxxi @@ -7,7 +8,7 @@ namespace Cxxi
/// </summary>
public abstract class Type
{
public static ITypeVisitor<string> TypePrinter;
public static Func<Type, string> TypePrinterDelegate;
protected Type()
{
@ -117,7 +118,7 @@ namespace Cxxi @@ -117,7 +118,7 @@ namespace Cxxi
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 @@ -6,13 +6,13 @@ namespace Cxxi.Generators.CLI
{
public class CLIGenerator : Generator
{
private readonly ITypePrinter typePrinter;
private readonly CLITypePrinter typePrinter;
private readonly FileHashes fileHashes;
public CLIGenerator(Driver driver) : base(driver)
{
typePrinter = new CLITypePrinter(driver);
Type.TypePrinter = typePrinter;
Type.TypePrinterDelegate += type => type.Visit(typePrinter);
fileHashes = FileHashes.Load("hashes.ser");
}

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

@ -489,7 +489,7 @@ namespace Cxxi.Generators.CLI @@ -489,7 +489,7 @@ namespace Cxxi.Generators.CLI
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);
WriteStartBraceIndent();

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

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

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

@ -17,7 +17,7 @@ namespace Cxxi.Generators.CLI @@ -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 CLITypePrinterContext Context { get; set; }
@ -307,5 +307,10 @@ namespace Cxxi.Generators.CLI @@ -307,5 +307,10 @@ namespace Cxxi.Generators.CLI
{
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 @@ -6,12 +6,12 @@ namespace Cxxi.Generators.CSharp
{
public class CSharpGenerator : Generator
{
private readonly ITypePrinter typePrinter;
private readonly CSharpTypePrinter typePrinter;
public CSharpGenerator(Driver driver) : base(driver)
{
typePrinter = new CSharpTypePrinter(driver.TypeDatabase, driver.Library);
Type.TypePrinter = typePrinter;
Type.TypePrinterDelegate += type => type.Visit(typePrinter).Type;
}
void WriteTemplate(TextTemplate template)

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

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

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

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

7
src/Generator/Types/CppTypePrinter.cs

@ -3,7 +3,7 @@ using System.Collections.Generic; @@ -3,7 +3,7 @@ using System.Collections.Generic;
namespace Cxxi.Types
{
public class CppTypePrinter : ITypePrinter, IDeclVisitor<string>
public class CppTypePrinter : ITypePrinter<string>, IDeclVisitor<string>
{
public CppTypePrinter(ITypeMapDatabase database)
{
@ -225,5 +225,10 @@ namespace Cxxi.Types @@ -225,5 +225,10 @@ namespace Cxxi.Types
{
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 @@ -49,11 +49,16 @@ namespace Cxxi.Types
public Type Type;
}
public interface ITypePrinter : ITypeVisitor<string>
public interface ITypePrinter
{
string VisitParameters(IEnumerable<Parameter> @params, bool hasNames);
string VisitParameter(Parameter param, bool hasName = true);
string ToString(Type type);
}
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