Browse Source

Remove the repetitive field for a type printer

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1626/head
Dimitar Dobrev 4 years ago
parent
commit
065ed82d2e
  1. 28
      src/Generator/Generators/C/CppMarshal.cs
  2. 3
      src/Generator/Generators/C/CppSources.cs
  3. 21
      src/Generator/Generators/C/CppTypePrinter.cs
  4. 6
      src/Generator/Generators/CLI/CLIHeaders.cs
  5. 16
      src/Generator/Generators/CLI/CLIMarshal.cs
  6. 5
      src/Generator/Generators/CLI/CLISources.cs
  7. 14
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  8. 12
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  9. 14
      src/Generator/Generators/Marshal.cs
  10. 8
      src/Generator/Generators/NAPI/NAPIHelpers.cs
  11. 28
      src/Generator/Generators/NAPI/NAPIMarshal.cs
  12. 21
      src/Generator/Generators/QuickJS/QuickJSMarshal.cs
  13. 14
      src/Generator/Generators/QuickJS/QuickJSSources.cs
  14. 3
      src/Generator/Generators/TypePrinter.cs
  15. 2
      src/Generator/Passes/ExpressionHelper.cs

28
src/Generator/Generators/C/CppMarshal.cs

@ -9,7 +9,7 @@ using Type = CppSharp.AST.Type;
namespace CppSharp.Generators.Cpp namespace CppSharp.Generators.Cpp
{ {
public class CppMarshalNativeToManagedPrinter : MarshalPrinter<MarshalContext> public class CppMarshalNativeToManagedPrinter : MarshalPrinter<MarshalContext, CppTypePrinter>
{ {
public CppMarshalNativeToManagedPrinter(MarshalContext marshalContext) public CppMarshalNativeToManagedPrinter(MarshalContext marshalContext)
: base(marshalContext) : base(marshalContext)
@ -60,17 +60,15 @@ namespace CppSharp.Generators.Cpp
return false; return false;
var pointee = pointer.Pointee.Desugar(); var pointee = pointer.Pointee.Desugar();
PrimitiveType primitive;
var param = Context.Parameter; var param = Context.Parameter;
if (param != null && (param.IsOut || param.IsInOut) && if (param != null && (param.IsOut || param.IsInOut) &&
pointee.IsPrimitiveType(out primitive)) pointee.IsPrimitiveType())
{ {
Context.Return.Write(Context.ReturnVarName); Context.Return.Write(Context.ReturnVarName);
return true; return true;
} }
if (pointee.IsPrimitiveType(out primitive)) if (pointee.IsPrimitiveType())
{ {
var returnVarName = Context.ReturnVarName; var returnVarName = Context.ReturnVarName;
@ -99,8 +97,7 @@ namespace CppSharp.Generators.Cpp
Modifier = pointer.Modifier, Modifier = pointer.Modifier,
QualifiedPointee = new QualifiedType(pointee) QualifiedPointee = new QualifiedType(pointee)
}; };
var nativeTypePrinter = new CppTypePrinter(Context.Context); var nativeTypeName = desugaredPointer.Visit(typePrinter, quals);
var nativeTypeName = desugaredPointer.Visit(nativeTypePrinter, quals);
Context.Return.Write("reinterpret_cast<{0}>({1})", nativeTypeName, Context.Return.Write("reinterpret_cast<{0}>({1})", nativeTypeName,
returnVarName); returnVarName);
} }
@ -307,9 +304,9 @@ namespace CppSharp.Generators.Cpp
public override bool VisitEnumDecl(Enumeration @enum) public override bool VisitEnumDecl(Enumeration @enum)
{ {
var typePrinter = new CppTypePrinter(Context.Context);
typePrinter.PushContext(TypePrinterContextKind.Managed); typePrinter.PushContext(TypePrinterContextKind.Managed);
var typeName = typePrinter.VisitDeclaration(@enum); var typeName = typePrinter.VisitDeclaration(@enum);
typePrinter.PopContext();
Context.Return.Write($"({typeName}){Context.ReturnVarName}"); Context.Return.Write($"({typeName}){Context.ReturnVarName}");
return true; return true;
@ -331,7 +328,7 @@ namespace CppSharp.Generators.Cpp
} }
} }
public class CppMarshalManagedToNativePrinter : MarshalPrinter<MarshalContext> public class CppMarshalManagedToNativePrinter : MarshalPrinter<MarshalContext, CppTypePrinter>
{ {
public CppMarshalManagedToNativePrinter(MarshalContext ctx) public CppMarshalManagedToNativePrinter(MarshalContext ctx)
: base(ctx) : base(ctx)
@ -395,9 +392,9 @@ namespace CppSharp.Generators.Cpp
if (pointee is FunctionType) if (pointee is FunctionType)
{ {
var cppTypePrinter = new CppTypePrinter(Context.Context); typePrinter.PushContext(TypePrinterContextKind.Managed);
cppTypePrinter.PushContext(TypePrinterContextKind.Managed); var cppTypeName = pointer.Visit(typePrinter, quals);
var cppTypeName = pointer.Visit(cppTypePrinter, quals); typePrinter.PopContext();
return VisitDelegateType(cppTypeName); return VisitDelegateType(cppTypeName);
} }
@ -425,8 +422,7 @@ namespace CppSharp.Generators.Cpp
var finalPointee = pointer.GetFinalPointee(); var finalPointee = pointer.GetFinalPointee();
if (finalPointee.IsPrimitiveType()) if (finalPointee.IsPrimitiveType())
{ {
var cppTypePrinter = new CppTypePrinter(Context.Context); var cppTypeName = pointer.Visit(typePrinter, quals);
var cppTypeName = pointer.Visit(cppTypePrinter, quals);
Context.Return.Write($"({cppTypeName})"); Context.Return.Write($"({cppTypeName})");
Context.Return.Write(Context.Parameter.Name); Context.Return.Write(Context.Parameter.Name);
@ -489,10 +485,6 @@ namespace CppSharp.Generators.Cpp
FunctionType func; FunctionType func;
if (decl.Type.IsPointerTo(out func)) if (decl.Type.IsPointerTo(out func))
{ {
var typePrinter = new CppTypePrinter(Context.Context);
typePrinter.PushContext(TypePrinterContextKind.Native);
var declName = decl.Visit(typePrinter);
typePrinter.PopContext();
// Use the original typedef name if available, otherwise just use the function pointer type // Use the original typedef name if available, otherwise just use the function pointer type
string cppTypeName; string cppTypeName;
if (!decl.IsSynthetized) if (!decl.IsSynthetized)

3
src/Generator/Generators/C/CppSources.cs

@ -338,8 +338,7 @@ namespace CppSharp.Generators.Cpp
// We cast the value to the base class type since otherwise there // We cast the value to the base class type since otherwise there
// could be ambiguous call to overloaded constructors. // could be ambiguous call to overloaded constructors.
var cppTypePrinter = new CppTypePrinter(Context); var nativeTypeName = @class.BaseClass.Visit(typePrinter);
var nativeTypeName = @class.BaseClass.Visit(cppTypePrinter);
Write($"({nativeTypeName}*)"); Write($"({nativeTypeName}*)");
WriteLine("{0}{1})", method != null ? "nullptr" : ClassCtorInstanceParamIdentifier, WriteLine("{0}{1})", method != null ? "nullptr" : ClassCtorInstanceParamIdentifier,

21
src/Generator/Generators/C/CppTypePrinter.cs

@ -25,15 +25,18 @@ namespace CppSharp.Generators.C
public TypePrintScopeKind MethodScopeKind = TypePrintScopeKind.Qualified; public TypePrintScopeKind MethodScopeKind = TypePrintScopeKind.Qualified;
public CppTypePrinter(BindingContext context) : base(TypePrinterContextKind.Native) public CppTypePrinter() : base(TypePrinterContextKind.Native)
{ {
Context = context;
PrintFlavorKind = CppTypePrintFlavorKind.Cpp; PrintFlavorKind = CppTypePrintFlavorKind.Cpp;
PrintTypeQualifiers = true; PrintTypeQualifiers = true;
PrintTypeModifiers = true; PrintTypeModifiers = true;
} }
public BindingContext Context { get; private set; } public CppTypePrinter(BindingContext context) : this()
{
Context = context;
}
public TypeMapDatabase TypeMapDatabase => Context.TypeMaps; public TypeMapDatabase TypeMapDatabase => Context.TypeMaps;
public DriverOptions Options => Context.Options; public DriverOptions Options => Context.Options;
@ -53,7 +56,7 @@ namespace CppSharp.Generators.C
var typePrinterContext = new TypePrinterContext var typePrinterContext = new TypePrinterContext
{ {
Type = type, Type = type,
Kind = Kind, Kind = ContextKind,
MarshalKind = MarshalKind MarshalKind = MarshalKind
}; };
@ -101,13 +104,11 @@ namespace CppSharp.Generators.C
throw new NotImplementedException(); throw new NotImplementedException();
} }
var result = new TypePrinterResult return new TypePrinterResult
{ {
Type = array.Type.Visit(this), Type = array.Type.Visit(this),
NameSuffix = new System.Text.StringBuilder(arraySuffix) NameSuffix = new System.Text.StringBuilder(arraySuffix)
}; };
return result;
} }
private static string ConvertModifierToString(PointerType.TypeModifier modifier) private static string ConvertModifierToString(PointerType.TypeModifier modifier)
@ -601,7 +602,7 @@ namespace CppSharp.Generators.C
$"operator {method.OriginalReturnType.Visit(this)}" : $"operator {method.OriginalReturnType.Visit(this)}" :
method.OriginalName; method.OriginalName;
string exceptionType = GetExceptionType(functionType); string exceptionType = Print(functionType.ExceptionSpecType);
if (!string.IsNullOrEmpty(@return.Type)) if (!string.IsNullOrEmpty(@return.Type))
@return.NamePrefix.Append(' '); @return.NamePrefix.Append(' ');
@ -753,9 +754,9 @@ namespace CppSharp.Generators.C
return string.Join(" ", stringQuals) + (appendSpace ? " " : string.Empty); return string.Join(" ", stringQuals) + (appendSpace ? " " : string.Empty);
} }
private static string GetExceptionType(FunctionType functionType) private static string Print(ExceptionSpecType exceptionSpecType)
{ {
switch (functionType.ExceptionSpecType) switch (exceptionSpecType)
{ {
case ExceptionSpecType.BasicNoexcept: case ExceptionSpecType.BasicNoexcept:
return " noexcept"; return " noexcept";

6
src/Generator/Generators/CLI/CLIHeaders.cs

@ -467,9 +467,9 @@ namespace CppSharp.Generators.CLI
{ {
if (!@event.IsGenerated) continue; if (!@event.IsGenerated) continue;
var cppTypePrinter = new CppTypePrinter(Context); typePrinter.PushContext(TypePrinterContextKind.Native);
cppTypePrinter.PushContext(TypePrinterContextKind.Native); var cppArgs = typePrinter.VisitParameters(@event.Parameters, hasNames: true);
var cppArgs = cppTypePrinter.VisitParameters(@event.Parameters, hasNames: true); typePrinter.PopContext();
WriteLine("private:"); WriteLine("private:");
Indent(); Indent();

16
src/Generator/Generators/CLI/CLIMarshal.cs

@ -10,7 +10,7 @@ using Type = CppSharp.AST.Type;
namespace CppSharp.Generators.CLI namespace CppSharp.Generators.CLI
{ {
public class CLIMarshalNativeToManagedPrinter : MarshalPrinter<MarshalContext> public class CLIMarshalNativeToManagedPrinter : MarshalPrinter<MarshalContext, CppTypePrinter>
{ {
public CLIMarshalNativeToManagedPrinter(MarshalContext marshalContext) public CLIMarshalNativeToManagedPrinter(MarshalContext marshalContext)
: base(marshalContext) : base(marshalContext)
@ -139,8 +139,7 @@ namespace CppSharp.Generators.CLI
Modifier = pointer.Modifier, Modifier = pointer.Modifier,
QualifiedPointee = new QualifiedType(pointee) QualifiedPointee = new QualifiedType(pointee)
}; };
var nativeTypePrinter = new CppTypePrinter(Context.Context); var nativeTypeName = desugaredPointer.Visit(typePrinter, quals);
var nativeTypeName = desugaredPointer.Visit(nativeTypePrinter, quals);
Context.Return.Write("reinterpret_cast<{0}>({1})", nativeTypeName, Context.Return.Write("reinterpret_cast<{0}>({1})", nativeTypeName,
returnVarName); returnVarName);
} }
@ -387,7 +386,7 @@ namespace CppSharp.Generators.CLI
} }
} }
public class CLIMarshalManagedToNativePrinter : MarshalPrinter<MarshalContext> public class CLIMarshalManagedToNativePrinter : MarshalPrinter<MarshalContext, CppTypePrinter>
{ {
public readonly TextGenerator VarPrefix; public readonly TextGenerator VarPrefix;
public readonly TextGenerator ArgumentPrefix; public readonly TextGenerator ArgumentPrefix;
@ -510,8 +509,7 @@ namespace CppSharp.Generators.CLI
if (pointee is FunctionType) if (pointee is FunctionType)
{ {
var cppTypePrinter = new CppTypePrinter(Context.Context); var cppTypeName = pointer.Visit(typePrinter, quals);
var cppTypeName = pointer.Visit(cppTypePrinter, quals);
return VisitDelegateType(cppTypeName); return VisitDelegateType(cppTypeName);
} }
@ -544,8 +542,7 @@ namespace CppSharp.Generators.CLI
var finalPointee = pointer.GetFinalPointee(); var finalPointee = pointer.GetFinalPointee();
if (finalPointee.IsPrimitiveType()) if (finalPointee.IsPrimitiveType())
{ {
var cppTypePrinter = new CppTypePrinter(Context.Context); var cppTypeName = pointer.Visit(typePrinter, quals);
var cppTypeName = pointer.Visit(cppTypePrinter, quals);
Context.Return.Write("({0})", cppTypeName); Context.Return.Write("({0})", cppTypeName);
Context.Return.Write(Context.Parameter.Name); Context.Return.Write(Context.Parameter.Name);
@ -620,8 +617,7 @@ namespace CppSharp.Generators.CLI
cppTypeName = "::" + typedef.Declaration.QualifiedOriginalName; cppTypeName = "::" + typedef.Declaration.QualifiedOriginalName;
else else
{ {
var cppTypePrinter = new CppTypePrinter(Context.Context); cppTypeName = decl.Type.Visit(typePrinter, quals);
cppTypeName = decl.Type.Visit(cppTypePrinter, quals);
} }
VisitDelegateType(cppTypeName); VisitDelegateType(cppTypeName);

5
src/Generator/Generators/CLI/CLISources.cs

@ -534,7 +534,6 @@ namespace CppSharp.Generators.CLI
WriteLine("if (!{0}Instance)", delegateName); WriteLine("if (!{0}Instance)", delegateName);
WriteOpenBraceAndIndent(); WriteOpenBraceAndIndent();
var typePrinter = new CppTypePrinter(Context);
var args = typePrinter.VisitParameters(@event.Parameters, hasNames: false); var args = typePrinter.VisitParameters(@event.Parameters, hasNames: false);
WriteLine("{0}Instance = gcnew {0}(this, &{1}::_{2}Raise);", WriteLine("{0}Instance = gcnew {0}(this, &{1}::_{2}Raise);",
@ -583,7 +582,6 @@ namespace CppSharp.Generators.CLI
private void GenerateEventRaiseWrapper(Event @event, Class @class) private void GenerateEventRaiseWrapper(Event @event, Class @class)
{ {
var typePrinter = new CppTypePrinter(Context);
var args = typePrinter.VisitParameters(@event.Parameters, hasNames: true); var args = typePrinter.VisitParameters(@event.Parameters, hasNames: true);
WriteLine("void {0}::_{1}Raise({2})", QualifiedIdentifier(@class), WriteLine("void {0}::_{1}Raise({2})", QualifiedIdentifier(@class),
@ -988,8 +986,7 @@ namespace CppSharp.Generators.CLI
function.OperatorKind == CXXOperatorKind.ExplicitConversion) function.OperatorKind == CXXOperatorKind.ExplicitConversion)
{ {
var method = function as Method; var method = function as Method;
var typePrinter = new CppTypePrinter(Context); var typeName = method.ConversionType.Visit(new CppTypePrinter(Context));
var typeName = method.ConversionType.Visit(typePrinter);
WriteLine("({0}) {1};", typeName, @params[0].Name); WriteLine("({0}) {1};", typeName, @params[0].Name);
} }
else if (function.IsOperator && else if (function.IsOperator &&

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

@ -17,7 +17,7 @@ namespace CppSharp.Generators.CSharp
public bool HasCodeBlock { get; set; } public bool HasCodeBlock { get; set; }
} }
public abstract class CSharpMarshalPrinter : MarshalPrinter<CSharpMarshalContext> public abstract class CSharpMarshalPrinter : MarshalPrinter<CSharpMarshalContext, CSharpTypePrinter>
{ {
protected CSharpMarshalPrinter(CSharpMarshalContext context) protected CSharpMarshalPrinter(CSharpMarshalContext context)
: base(context) : base(context)
@ -38,7 +38,6 @@ namespace CppSharp.Generators.CSharp
public CSharpMarshalNativeToManagedPrinter(CSharpMarshalContext context) public CSharpMarshalNativeToManagedPrinter(CSharpMarshalContext context)
: base(context) : base(context)
{ {
typePrinter = new CSharpTypePrinter(context.Context);
} }
public override bool VisitType(Type type, TypeQualifiers quals) public override bool VisitType(Type type, TypeQualifiers quals)
@ -87,7 +86,7 @@ namespace CppSharp.Generators.CSharp
supportBefore.WriteLine($"for (int i = 0; i < {array.Size}; i++)"); supportBefore.WriteLine($"for (int i = 0; i < {array.Size}; i++)");
var finalArrayType = arrayType.GetPointee() ?? arrayType; var finalArrayType = arrayType.GetPointee() ?? arrayType;
Class @class; Class @class;
if ((finalArrayType.TryGetClass(out @class)) && @class.IsRefType) if (finalArrayType.TryGetClass(out @class) && @class.IsRefType)
{ {
if (arrayType == finalArrayType) if (arrayType == finalArrayType)
supportBefore.WriteLineIndent( supportBefore.WriteLineIndent(
@ -101,11 +100,11 @@ namespace CppSharp.Generators.CSharp
} }
else else
{ {
supportBefore.WriteLineIndent($@"{value}[i] = {Context.ReturnVarName}[i];"); supportBefore.WriteLineIndent($"{value}[i] = {Context.ReturnVarName}[i];");
} }
supportBefore.UnindentAndWriteCloseBrace(); supportBefore.UnindentAndWriteCloseBrace();
Context.Return.Write(value); Context.Return.Write(value);
} }
break; break;
case ArrayType.ArraySize.Incomplete: case ArrayType.ArraySize.Incomplete:
// const char* and const char[] are the same so we can use a string // const char* and const char[] are the same so we can use a string
@ -448,8 +447,6 @@ namespace CppSharp.Generators.CSharp
return array.Type.IsPrimitiveType(out var primitive) && return array.Type.IsPrimitiveType(out var primitive) &&
(!Context.Context.Options.MarshalCharAsManagedChar || primitive != PrimitiveType.Char); (!Context.Context.Options.MarshalCharAsManagedChar || primitive != PrimitiveType.Char);
} }
private readonly CSharpTypePrinter typePrinter;
} }
public class CSharpMarshalManagedToNativePrinter : CSharpMarshalPrinter public class CSharpMarshalManagedToNativePrinter : CSharpMarshalPrinter
@ -457,7 +454,6 @@ namespace CppSharp.Generators.CSharp
public CSharpMarshalManagedToNativePrinter(CSharpMarshalContext context) public CSharpMarshalManagedToNativePrinter(CSharpMarshalContext context)
: base(context) : base(context)
{ {
typePrinter = new CSharpTypePrinter(context.Context);
} }
public override bool VisitType(Type type, TypeQualifiers quals) public override bool VisitType(Type type, TypeQualifiers quals)
@ -930,7 +926,5 @@ namespace CppSharp.Generators.CSharp
marshal.Context.Return};"); marshal.Context.Return};");
Context.Return.StringBuilder.Clear(); Context.Return.StringBuilder.Clear();
} }
private readonly CSharpTypePrinter typePrinter;
} }
} }

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

@ -16,13 +16,15 @@ namespace CppSharp.Generators.CSharp
{ {
public string IntPtrType => "__IntPtr"; public string IntPtrType => "__IntPtr";
public BindingContext Context { get; set; }
public DriverOptions Options => Context.Options; public DriverOptions Options => Context.Options;
public TypeMapDatabase TypeMapDatabase => Context.TypeMaps; public TypeMapDatabase TypeMapDatabase => Context.TypeMaps;
public bool PrintModuleOutputNamespace = true; public bool PrintModuleOutputNamespace = true;
public CSharpTypePrinter()
{
}
public CSharpTypePrinter(BindingContext context) public CSharpTypePrinter(BindingContext context)
{ {
Context = context; Context = context;
@ -45,7 +47,7 @@ namespace CppSharp.Generators.CSharp
var typePrinterContext = new TypePrinterContext() var typePrinterContext = new TypePrinterContext()
{ {
Kind = Kind, Kind = ContextKind,
MarshalKind = MarshalKind, MarshalKind = MarshalKind,
Type = tag Type = tag
}; };
@ -135,7 +137,7 @@ namespace CppSharp.Generators.CSharp
{ {
var typePrinterContext = new TypePrinterContext() var typePrinterContext = new TypePrinterContext()
{ {
Kind = Kind, Kind = ContextKind,
MarshalKind = MarshalKind, MarshalKind = MarshalKind,
Type = builtin, Type = builtin,
Parameter = Parameter Parameter = Parameter
@ -167,7 +169,7 @@ namespace CppSharp.Generators.CSharp
TypeMapDatabase.FindTypeMap(pointer, out typeMap); TypeMapDatabase.FindTypeMap(pointer, out typeMap);
var typePrinterContext = new TypePrinterContext() var typePrinterContext = new TypePrinterContext()
{ {
Kind = Kind, Kind = ContextKind,
MarshalKind = MarshalKind, MarshalKind = MarshalKind,
Type = pointer.Pointee, Type = pointer.Pointee,
Parameter = Parameter Parameter = Parameter

14
src/Generator/Generators/Marshal.cs

@ -1,4 +1,5 @@
using CppSharp.AST; using CppSharp.AST;
using CppSharp.Generators.C;
namespace CppSharp.Generators namespace CppSharp.Generators
{ {
@ -15,9 +16,7 @@ namespace CppSharp.Generators
Indentation = indentation; Indentation = indentation;
} }
public BindingContext Context { get; } public MarshalPrinter<MarshalContext, CppTypePrinter> MarshalToNative;
public MarshalPrinter<MarshalContext> MarshalToNative;
public TextGenerator Before { get; } public TextGenerator Before { get; }
public TextGenerator Return { get; } public TextGenerator Return { get; }
@ -35,13 +34,16 @@ namespace CppSharp.Generators
public uint Indentation { get; } public uint Indentation { get; }
} }
public abstract class MarshalPrinter<T> : AstVisitor where T : MarshalContext public abstract class MarshalPrinter<C, P> : AstVisitor where C : MarshalContext where P : TypePrinter, new()
{ {
public T Context { get; } public C Context { get; }
protected MarshalPrinter(T ctx) protected MarshalPrinter(C ctx)
{ {
Context = ctx; Context = ctx;
typePrinter.Context = ctx.Context;
} }
protected P typePrinter = new P();
} }
} }

8
src/Generator/Generators/NAPI/NAPIHelpers.cs

@ -1,13 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO;
using System.Linq; using System.Linq;
using CppSharp.AST; using CppSharp.AST;
using CppSharp.AST.Extensions; using CppSharp.AST.Extensions;
using CppSharp.Generators.C; using CppSharp.Generators.C;
using CppSharp.Generators.NAPI; using CppSharp.Generators.NAPI;
using CppSharp.Passes;
namespace CppSharp.Generators.Cpp namespace CppSharp.Generators.Cpp
{ {
@ -94,12 +92,12 @@ namespace CppSharp.Generators.Cpp
} }
} }
public virtual MarshalPrinter<MarshalContext> GetMarshalManagedToNativePrinter(MarshalContext ctx) public virtual MarshalPrinter<MarshalContext, CppTypePrinter> GetMarshalManagedToNativePrinter(MarshalContext ctx)
{ {
return new NAPIMarshalManagedToNativePrinter(ctx); return new NAPIMarshalManagedToNativePrinter(ctx);
} }
public virtual MarshalPrinter<MarshalContext> GetMarshalNativeToManagedPrinter(MarshalContext ctx) public virtual MarshalPrinter<MarshalContext, CppTypePrinter> GetMarshalNativeToManagedPrinter(MarshalContext ctx)
{ {
return new NAPIMarshalNativeToManagedPrinter(ctx); return new NAPIMarshalNativeToManagedPrinter(ctx);
} }
@ -220,7 +218,7 @@ namespace CppSharp.Generators.Cpp
public virtual void GenerateFunctionParamsMarshalCleanups(List<ParamMarshal> @params) public virtual void GenerateFunctionParamsMarshalCleanups(List<ParamMarshal> @params)
{ {
var marshalers = new List<MarshalPrinter<MarshalContext>>(); var marshalers = new List<MarshalPrinter<MarshalContext, CppTypePrinter>>();
PushBlock(); PushBlock();
{ {

28
src/Generator/Generators/NAPI/NAPIMarshal.cs

@ -10,7 +10,7 @@ using Type = CppSharp.AST.Type;
namespace CppSharp.Generators.NAPI namespace CppSharp.Generators.NAPI
{ {
public class NAPIMarshalNativeToManagedPrinter : MarshalPrinter<MarshalContext> public class NAPIMarshalNativeToManagedPrinter : MarshalPrinter<MarshalContext, CppTypePrinter>
{ {
public NAPIMarshalNativeToManagedPrinter(MarshalContext marshalContext) public NAPIMarshalNativeToManagedPrinter(MarshalContext marshalContext)
: base(marshalContext) : base(marshalContext)
@ -198,15 +198,10 @@ namespace CppSharp.Generators.NAPI
return typeMap.IsValueType; return typeMap.IsValueType;
} }
FunctionType function; if (decl.Type.IsPointerTo(out FunctionType _))
if (decl.Type.IsPointerTo(out function))
{ {
var typePrinter = new CppTypePrinter(Context.Context);
var typeName = typePrinter.VisitDeclaration(decl); var typeName = typePrinter.VisitDeclaration(decl);
var typeName2 = decl.Type.Visit(typePrinter);
Context.Return.Write(typeName); Context.Return.Write(typeName);
//return typeName;
//throw new System.NotImplementedException();
} }
return decl.Type.Visit(this); return decl.Type.Visit(this);
@ -335,7 +330,7 @@ namespace CppSharp.Generators.NAPI
} }
} }
public class NAPIMarshalManagedToNativePrinter : MarshalPrinter<MarshalContext> public class NAPIMarshalManagedToNativePrinter : MarshalPrinter<MarshalContext, CppTypePrinter>
{ {
public NAPIMarshalManagedToNativePrinter(MarshalContext ctx) public NAPIMarshalManagedToNativePrinter(MarshalContext ctx)
: base(ctx) : base(ctx)
@ -402,9 +397,9 @@ namespace CppSharp.Generators.NAPI
if (pointee is FunctionType) if (pointee is FunctionType)
{ {
var cppTypePrinter = new CppTypePrinter(Context.Context); typePrinter.PushContext(TypePrinterContextKind.Managed);
cppTypePrinter.PushContext(TypePrinterContextKind.Managed); var cppTypeName = pointer.Visit(typePrinter, quals);
var cppTypeName = pointer.Visit(cppTypePrinter, quals); typePrinter.PopContext();
return VisitDelegateType(cppTypeName); return VisitDelegateType(cppTypeName);
} }
@ -432,8 +427,7 @@ namespace CppSharp.Generators.NAPI
var finalPointee = pointer.GetFinalPointee(); var finalPointee = pointer.GetFinalPointee();
if (finalPointee.IsPrimitiveType()) if (finalPointee.IsPrimitiveType())
{ {
var cppTypePrinter = new CppTypePrinter(Context.Context); var cppTypeName = pointer.Visit(typePrinter, quals);
var cppTypeName = pointer.Visit(cppTypePrinter, quals);
Context.Return.Write($"({cppTypeName})"); Context.Return.Write($"({cppTypeName})");
Context.Return.Write(Context.Parameter.Name); Context.Return.Write(Context.Parameter.Name);
@ -601,14 +595,8 @@ namespace CppSharp.Generators.NAPI
return typeMap.IsValueType; return typeMap.IsValueType;
} }
FunctionType func; if (decl.Type.IsPointerTo(out FunctionType _))
if (decl.Type.IsPointerTo(out func))
{ {
var typePrinter = new CppTypePrinter(Context.Context);
typePrinter.PushContext(TypePrinterContextKind.Native);
var declName = decl.Visit(typePrinter);
typePrinter.PopContext();
// Use the original typedef name if available, otherwise just use the function pointer type // Use the original typedef name if available, otherwise just use the function pointer type
string cppTypeName; string cppTypeName;
if (!decl.IsSynthetized) if (!decl.IsSynthetized)

21
src/Generator/Generators/QuickJS/QuickJSMarshal.cs

@ -9,7 +9,7 @@ using Type = CppSharp.AST.Type;
namespace CppSharp.Generators.Cpp namespace CppSharp.Generators.Cpp
{ {
public class QuickJSMarshalNativeToManagedPrinter : MarshalPrinter<MarshalContext> public class QuickJSMarshalNativeToManagedPrinter : MarshalPrinter<MarshalContext, CppTypePrinter>
{ {
public QuickJSMarshalNativeToManagedPrinter(MarshalContext marshalContext) public QuickJSMarshalNativeToManagedPrinter(MarshalContext marshalContext)
: base(marshalContext) : base(marshalContext)
@ -99,8 +99,7 @@ namespace CppSharp.Generators.Cpp
Modifier = pointer.Modifier, Modifier = pointer.Modifier,
QualifiedPointee = new QualifiedType(pointee) QualifiedPointee = new QualifiedType(pointee)
}; };
var nativeTypePrinter = new CppTypePrinter(Context.Context); var nativeTypeName = desugaredPointer.Visit(typePrinter, quals);
var nativeTypeName = desugaredPointer.Visit(nativeTypePrinter, quals);
Context.Return.Write("reinterpret_cast<{0}>({1})", nativeTypeName, Context.Return.Write("reinterpret_cast<{0}>({1})", nativeTypeName,
returnVarName); returnVarName);
} }
@ -218,12 +217,9 @@ namespace CppSharp.Generators.Cpp
FunctionType function; FunctionType function;
if (decl.Type.IsPointerTo(out function)) if (decl.Type.IsPointerTo(out function))
{ {
var typePrinter = new CppTypePrinter(Context.Context);
var typeName = typePrinter.VisitDeclaration(decl); var typeName = typePrinter.VisitDeclaration(decl);
var typeName2 = decl.Type.Visit(typePrinter); var typeName2 = decl.Type.Visit(typePrinter);
Context.Return.Write(typeName); Context.Return.Write(typeName);
//return typeName;
//throw new System.NotImplementedException();
} }
return decl.Type.Visit(this); return decl.Type.Visit(this);
@ -370,7 +366,7 @@ namespace CppSharp.Generators.Cpp
} }
} }
public class QuickJSMarshalManagedToNativePrinter : MarshalPrinter<MarshalContext> public class QuickJSMarshalManagedToNativePrinter : MarshalPrinter<MarshalContext, CppTypePrinter>
{ {
public readonly TextGenerator VarPrefix; public readonly TextGenerator VarPrefix;
public readonly TextGenerator ArgumentPrefix; public readonly TextGenerator ArgumentPrefix;
@ -440,9 +436,9 @@ namespace CppSharp.Generators.Cpp
if (pointee is FunctionType) if (pointee is FunctionType)
{ {
var cppTypePrinter = new CppTypePrinter(Context.Context); typePrinter.PushContext(TypePrinterContextKind.Managed);
cppTypePrinter.PushContext(TypePrinterContextKind.Managed); var cppTypeName = pointer.Visit(typePrinter, quals);
var cppTypeName = pointer.Visit(cppTypePrinter, quals); typePrinter.PopContext();
return VisitDelegateType(cppTypeName); return VisitDelegateType(cppTypeName);
} }
@ -470,8 +466,7 @@ namespace CppSharp.Generators.Cpp
var finalPointee = pointer.GetFinalPointee(); var finalPointee = pointer.GetFinalPointee();
if (finalPointee.IsPrimitiveType()) if (finalPointee.IsPrimitiveType())
{ {
var cppTypePrinter = new CppTypePrinter(Context.Context); var cppTypeName = pointer.Visit(typePrinter, quals);
var cppTypeName = pointer.Visit(cppTypePrinter, quals);
Context.Return.Write($"({cppTypeName})"); Context.Return.Write($"({cppTypeName})");
Context.Return.Write(Context.Parameter.Name); Context.Return.Write(Context.Parameter.Name);
@ -494,7 +489,6 @@ namespace CppSharp.Generators.Cpp
public bool VisitPrimitiveType(PrimitiveType primitive) public bool VisitPrimitiveType(PrimitiveType primitive)
{ {
var typePrinter = new CppTypePrinter(Context.Context);
var type = typePrinter.VisitPrimitiveType(primitive); var type = typePrinter.VisitPrimitiveType(primitive);
var argName = Context.Parameter.Name; var argName = Context.Parameter.Name;
@ -596,7 +590,6 @@ namespace CppSharp.Generators.Cpp
FunctionType func; FunctionType func;
if (decl.Type.IsPointerTo(out func)) if (decl.Type.IsPointerTo(out func))
{ {
var typePrinter = new CppTypePrinter(Context.Context);
typePrinter.PushContext(TypePrinterContextKind.Native); typePrinter.PushContext(TypePrinterContextKind.Native);
var declName = decl.Visit(typePrinter); var declName = decl.Visit(typePrinter);
typePrinter.PopContext(); typePrinter.PopContext();

14
src/Generator/Generators/QuickJS/QuickJSSources.cs

@ -17,12 +17,12 @@ namespace CppSharp.Generators.Cpp
{ {
} }
public override MarshalPrinter<MarshalContext> GetMarshalManagedToNativePrinter(MarshalContext ctx) public override MarshalPrinter<MarshalContext, CppTypePrinter> GetMarshalManagedToNativePrinter(MarshalContext ctx)
{ {
return new QuickJSMarshalManagedToNativePrinter(ctx); return new QuickJSMarshalManagedToNativePrinter(ctx);
} }
public override MarshalPrinter<MarshalContext> GetMarshalNativeToManagedPrinter(MarshalContext ctx) public override MarshalPrinter<MarshalContext, CppTypePrinter> GetMarshalNativeToManagedPrinter(MarshalContext ctx)
{ {
return new QuickJSMarshalNativeToManagedPrinter(ctx); return new QuickJSMarshalNativeToManagedPrinter(ctx);
} }
@ -247,12 +247,12 @@ namespace CppSharp.Generators.Cpp
{ {
} }
public override MarshalPrinter<MarshalContext> GetMarshalManagedToNativePrinter(MarshalContext ctx) public override MarshalPrinter<MarshalContext, CppTypePrinter> GetMarshalManagedToNativePrinter(MarshalContext ctx)
{ {
return new QuickJSMarshalManagedToNativePrinter(ctx); return new QuickJSMarshalManagedToNativePrinter(ctx);
} }
public override MarshalPrinter<MarshalContext> GetMarshalNativeToManagedPrinter(MarshalContext ctx) public override MarshalPrinter<MarshalContext, CppTypePrinter> GetMarshalNativeToManagedPrinter(MarshalContext ctx)
{ {
return new QuickJSMarshalNativeToManagedPrinter(ctx); return new QuickJSMarshalNativeToManagedPrinter(ctx);
} }
@ -426,7 +426,7 @@ namespace CppSharp.Generators.Cpp
NewLine(); NewLine();
// Marshal the arguments. // Marshal the arguments.
var marshalers = new List<MarshalPrinter<MarshalContext>>(); var marshalers = new List<MarshalPrinter<MarshalContext, CppTypePrinter>>();
foreach (var param in @event.Parameters) foreach (var param in @event.Parameters)
{ {
var ctx = new MarshalContext(Context, CurrentIndentation) var ctx = new MarshalContext(Context, CurrentIndentation)
@ -721,12 +721,12 @@ namespace CppSharp.Generators.Cpp
{ {
} }
public override MarshalPrinter<MarshalContext> GetMarshalManagedToNativePrinter(MarshalContext ctx) public override MarshalPrinter<MarshalContext, CppTypePrinter> GetMarshalManagedToNativePrinter(MarshalContext ctx)
{ {
return new QuickJSMarshalManagedToNativePrinter(ctx); return new QuickJSMarshalManagedToNativePrinter(ctx);
} }
public override MarshalPrinter<MarshalContext> GetMarshalNativeToManagedPrinter(MarshalContext ctx) public override MarshalPrinter<MarshalContext, CppTypePrinter> GetMarshalNativeToManagedPrinter(MarshalContext ctx)
{ {
return new QuickJSMarshalNativeToManagedPrinter(ctx); return new QuickJSMarshalNativeToManagedPrinter(ctx);
} }

3
src/Generator/Generators/TypePrinter.cs

@ -55,10 +55,9 @@ namespace CppSharp.Generators
private readonly Stack<MarshalKind> marshalKinds; private readonly Stack<MarshalKind> marshalKinds;
private readonly Stack<TypePrintScopeKind> scopeKinds; private readonly Stack<TypePrintScopeKind> scopeKinds;
public BindingContext Context { get; set; }
public TypePrinterContextKind ContextKind => contexts.Peek(); public TypePrinterContextKind ContextKind => contexts.Peek();
public TypePrinterContextKind Kind => ContextKind;
public MarshalKind MarshalKind => marshalKinds.Peek(); public MarshalKind MarshalKind => marshalKinds.Peek();
public TypePrintScopeKind ScopeKind => scopeKinds.Peek(); public TypePrintScopeKind ScopeKind => scopeKinds.Peek();

2
src/Generator/Passes/ExpressionHelper.cs

@ -244,7 +244,7 @@ namespace CppSharp.Internal
{ {
var typePrinterContext = new TypePrinterContext() var typePrinterContext = new TypePrinterContext()
{ {
Kind = typePrinter.Kind, Kind = typePrinter.ContextKind,
MarshalKind = typePrinter.MarshalKind, MarshalKind = typePrinter.MarshalKind,
Type = type Type = type
}; };

Loading…
Cancel
Save