Browse Source

Type printing fixes for the C# backend.

pull/1/head
triton 12 years ago
parent
commit
bbd94f4983
  1. 53
      src/Generator/Generators/CSharp/CSharpMarshal.cs

53
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -1,5 +1,4 @@
using System.Text; using System.Text;
using Cxxi.Generators.CLI;
using Cxxi.Types; using Cxxi.Types;
namespace Cxxi.Generators.CSharp namespace Cxxi.Generators.CSharp
@ -139,18 +138,18 @@ namespace Cxxi.Generators.CSharp
if (Context.Driver.TypeDatabase.FindTypeMap(decl, out typeMap)) if (Context.Driver.TypeDatabase.FindTypeMap(decl, out typeMap))
{ {
typeMap.Type = typedef; typeMap.Type = typedef;
typeMap.CLIMarshalToManaged(Context); typeMap.CSharpMarshalToManaged(Context);
return typeMap.IsValueType; return typeMap.IsValueType;
} }
FunctionType function; FunctionType function;
if (decl.Type.IsPointerTo(out function)) if (decl.Type.IsPointerTo(out function))
{ {
Context.Return.Write("safe_cast<{0}>(", typedef); Context.SupportBefore.WriteLine("var {0} = new IntPtr({1});",
Context.Return.Write("System::Runtime::InteropServices::Marshal::"); Helpers.GeneratedIdentifier("ptr"), Context.ReturnVarName);
Context.Return.Write("GetDelegateForFunctionPointer(");
Context.Return.Write("IntPtr({0}), {1}::typeid))", Context.ReturnVarName, Context.Return.Write("({1})Marshal.GetDelegateForFunctionPointer({0}, typeof({1}))",
typedef.ToString().TrimEnd('^')); Helpers.GeneratedIdentifier("ptr"), typedef.ToString());
return true; return true;
} }
@ -187,19 +186,15 @@ namespace Cxxi.Generators.CSharp
public string QualifiedIdentifier(Declaration decl) public string QualifiedIdentifier(Declaration decl)
{ {
if (Context.Driver.Options.GenerateLibraryNamespace) if (Context.Driver.Options.GenerateLibraryNamespace)
return string.Format("{0}::{1}", Context.Driver.Options.OutputNamespace, return string.Format("{0}.{1}", Context.Driver.Options.OutputNamespace,
decl.QualifiedName); decl.QualifiedName);
return string.Format("{0}", decl.QualifiedName); return string.Format("{0}", decl.QualifiedName);
} }
public void WriteClassInstance(Class @class, string instance) public void WriteClassInstance(Class @class, string instance)
{ {
if (@class.IsRefType) Context.Return.Write("new {0}({1})", QualifiedIdentifier(@class),
Context.Return.Write("gcnew "); instance);
Context.Return.Write("{0}(", QualifiedIdentifier(@class));
Context.Return.Write("(::{0}*)", @class.QualifiedOriginalName);
Context.Return.Write("{0})", instance);
} }
public override bool VisitFieldDecl(Field field) public override bool VisitFieldDecl(Field field)
@ -214,8 +209,7 @@ namespace Cxxi.Generators.CSharp
public override bool VisitEnumDecl(Enumeration @enum) public override bool VisitEnumDecl(Enumeration @enum)
{ {
Context.Return.Write("({0}){1}", ToCLITypeName(@enum), Context.Return.Write("{0}", Context.ReturnVarName);
Context.ReturnVarName);
return true; return true;
} }
@ -224,12 +218,6 @@ namespace Cxxi.Generators.CSharp
return variable.Type.Visit(this, variable.QualifiedType.Qualifiers); return variable.Type.Visit(this, variable.QualifiedType.Qualifiers);
} }
private string ToCLITypeName(Declaration decl)
{
var typePrinter = new CLITypePrinter(Context.Driver);
return typePrinter.VisitDeclaration(decl);
}
public override bool VisitClassTemplateDecl(ClassTemplate template) public override bool VisitClassTemplateDecl(ClassTemplate template)
{ {
return template.TemplatedClass.Visit(this); return template.TemplatedClass.Visit(this);
@ -260,9 +248,8 @@ namespace Cxxi.Generators.CSharp
// explicit GCHandle if necessary. // explicit GCHandle if necessary.
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.AppendFormat("static_cast<::{0}>(", type); sb.AppendFormat("({0})(", type);
sb.Append("System::Runtime::InteropServices::Marshal::"); sb.Append("Marshal.GetFunctionPointerForDelegate(");
sb.Append("GetFunctionPointerForDelegate(");
sb.AppendFormat("{0}).ToPointer())", Context.Parameter.Name); sb.AppendFormat("{0}).ToPointer())", Context.Parameter.Name);
Context.Return.Write(sb.ToString()); Context.Return.Write(sb.ToString());
@ -306,7 +293,8 @@ namespace Cxxi.Generators.CSharp
if (pointee.IsTagDecl(out @class)) if (pointee.IsTagDecl(out @class))
{ {
if (@class.IsRefType) if (@class.IsRefType)
Context.Return.Write("{0}.Instance", Context.Parameter.Name); Context.Return.Write("{0}.Instance",
Helpers.SafeIdentifier(Context.Parameter.Name));
else else
Context.Return.Write("new IntPtr(&{0})", Context.Parameter.Name); Context.Return.Write("new IntPtr(&{0})", Context.Parameter.Name);
return true; return true;
@ -373,7 +361,7 @@ namespace Cxxi.Generators.CSharp
PrimitiveType primitive; PrimitiveType primitive;
if (decl.Type.Desugar().IsPrimitiveType(out primitive)) if (decl.Type.Desugar().IsPrimitiveType(out primitive))
{ {
Context.Return.Write("({0})", typedef.Declaration.Name); //Context.Return.Write("({0})", typedef.Declaration.Name);
} }
return decl.Type.Visit(this); return decl.Type.Visit(this);
@ -475,13 +463,13 @@ namespace Cxxi.Generators.CSharp
var fieldRef = string.Format("{0}.{1}", Context.Parameter.Name, var fieldRef = string.Format("{0}.{1}", Context.Parameter.Name,
field.Name); field.Name);
var marshalCtx = new MarshalContext(Context.Driver) var marshalCtx = new CSharpMarshalContext(Context.Driver)
{ {
ArgName = fieldRef, ArgName = fieldRef,
ParameterIndex = Context.ParameterIndex++ ParameterIndex = Context.ParameterIndex++
}; };
var marshal = new CLIMarshalManagedToNativePrinter(marshalCtx); var marshal = new CSharpMarshalManagedToNativePrinter(marshalCtx);
field.Visit(marshal); field.Visit(marshal);
Context.ParameterIndex = marshalCtx.ParameterIndex; Context.ParameterIndex = marshalCtx.ParameterIndex;
@ -495,8 +483,8 @@ namespace Cxxi.Generators.CSharp
Context.SupportBefore.PushIndent(); Context.SupportBefore.PushIndent();
} }
Context.SupportBefore.WriteLine("{0}.{1} = {2};", marshalVar, field.OriginalName, Context.SupportBefore.WriteLine("{0}.{1} = {2};", marshalVar,
marshal.Context.Return); field.OriginalName, marshal.Context.Return);
if (field.Type.IsPointer()) if (field.Type.IsPointer())
Context.SupportBefore.PopIndent(); Context.SupportBefore.PopIndent();
@ -540,8 +528,7 @@ namespace Cxxi.Generators.CSharp
public override bool VisitEnumDecl(Enumeration @enum) public override bool VisitEnumDecl(Enumeration @enum)
{ {
Context.Return.Write("(::{0}){1}", @enum.QualifiedOriginalName, Context.Return.Write(Context.Parameter.Name);
Context.Parameter.Name);
return true; return true;
} }

Loading…
Cancel
Save