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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

14
src/Generator/Generators/Marshal.cs

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
using CppSharp.AST;
using CppSharp.Generators.C;
namespace CppSharp.Generators
{
@ -15,9 +16,7 @@ namespace CppSharp.Generators @@ -15,9 +16,7 @@ namespace CppSharp.Generators
Indentation = indentation;
}
public BindingContext Context { get; }
public MarshalPrinter<MarshalContext> MarshalToNative;
public MarshalPrinter<MarshalContext, CppTypePrinter> MarshalToNative;
public TextGenerator Before { get; }
public TextGenerator Return { get; }
@ -35,13 +34,16 @@ namespace CppSharp.Generators @@ -35,13 +34,16 @@ namespace CppSharp.Generators
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;
typePrinter.Context = ctx.Context;
}
protected P typePrinter = new P();
}
}

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

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

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

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

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

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

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

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

3
src/Generator/Generators/TypePrinter.cs

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

2
src/Generator/Passes/ExpressionHelper.cs

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

Loading…
Cancel
Save