Browse Source

Factor VarPrefix and ArgumentPrefix helpers into base marshaler.

pull/1547/head
Joao Matos 5 years ago committed by João Matos
parent
commit
c4f96ab1b9
  1. 10
      src/Generator/Generators/C/CppMarshal.cs
  2. 4
      src/Generator/Generators/C/CppSources.cs
  3. 6
      src/Generator/Generators/CLI/CLIMarshal.cs
  4. 2
      src/Generator/Generators/CLI/CLISources.cs
  5. 4
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  6. 8
      src/Generator/Generators/Marshal.cs

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

@ -333,15 +333,9 @@ namespace CppSharp.Generators.Cpp
public class CppMarshalManagedToNativePrinter : MarshalPrinter<MarshalContext> public class CppMarshalManagedToNativePrinter : MarshalPrinter<MarshalContext>
{ {
public readonly TextGenerator VarPrefix;
public readonly TextGenerator ArgumentPrefix;
public CppMarshalManagedToNativePrinter(MarshalContext ctx) public CppMarshalManagedToNativePrinter(MarshalContext ctx)
: base(ctx) : base(ctx)
{ {
VarPrefix = new TextGenerator();
ArgumentPrefix = new TextGenerator();
Context.MarshalToNative = this; Context.MarshalToNative = this;
} }
@ -414,7 +408,7 @@ namespace CppSharp.Generators.Cpp
var isRef = Context.Parameter.Usage == ParameterUsage.Out || var isRef = Context.Parameter.Usage == ParameterUsage.Out ||
Context.Parameter.Usage == ParameterUsage.InOut; Context.Parameter.Usage == ParameterUsage.InOut;
ArgumentPrefix.Write("&"); Context.ArgumentPrefix.Write("&");
Context.Return.Write($"(::{@enum.QualifiedOriginalName}){0}{Context.Parameter.Name}", Context.Return.Write($"(::{@enum.QualifiedOriginalName}){0}{Context.Parameter.Name}",
isRef ? string.Empty : "*"); isRef ? string.Empty : "*");
return true; return true;
@ -586,7 +580,7 @@ namespace CppSharp.Generators.Cpp
Context.Return.Write("*"); Context.Return.Write("*");
if (Context.Parameter.Type.IsReference()) if (Context.Parameter.Type.IsReference())
VarPrefix.Write("&"); Context.VarPrefix.Write("&");
} }
var method = Context.Function as Method; var method = Context.Function as Method;

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

@ -658,8 +658,8 @@ namespace CppSharp.Generators.Cpp
if (!string.IsNullOrWhiteSpace(marshal.Context.Before)) if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before); Write(marshal.Context.Before);
WriteLine($"auto {marshal.VarPrefix}{argName} = {marshal.Context.Return};"); WriteLine($"auto {marshal.Context.VarPrefix}{argName} = {marshal.Context.Return};");
paramMarshal.Prefix = marshal.ArgumentPrefix; paramMarshal.Prefix = marshal.Context.ArgumentPrefix;
} }
paramMarshal.Name = argName; paramMarshal.Name = argName;

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

@ -738,7 +738,7 @@ namespace CppSharp.Generators.CLI
public void MarshalValueClass(Class @class) public void MarshalValueClass(Class @class)
{ {
var marshalVar = Context.MarshalVarPrefix + "_marshal" + var marshalVar = Context.VarPrefix + "_marshal" +
Context.ParameterIndex++; Context.ParameterIndex++;
Context.Before.WriteLine("auto {0} = ::{1}();", marshalVar, Context.Before.WriteLine("auto {0} = ::{1}();", marshalVar,
@ -774,14 +774,12 @@ namespace CppSharp.Generators.CLI
private void MarshalValueClassProperty(Property property, string marshalVar) private void MarshalValueClassProperty(Property property, string marshalVar)
{ {
var fieldRef = string.Format("{0}.{1}", Context.Parameter.Name, var fieldRef = $"{Context.Parameter.Name}.{property.Name}";
property.Name);
var marshalCtx = new MarshalContext(Context.Context, Context.Indentation) var marshalCtx = new MarshalContext(Context.Context, Context.Indentation)
{ {
ArgName = fieldRef, ArgName = fieldRef,
ParameterIndex = Context.ParameterIndex++, ParameterIndex = Context.ParameterIndex++,
MarshalVarPrefix = Context.MarshalVarPrefix,
ReturnVarName = $"{marshalVar}.{property.Field.OriginalName}" ReturnVarName = $"{marshalVar}.{property.Field.OriginalName}"
}; };

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

@ -968,9 +968,9 @@ namespace CppSharp.Generators.CLI
var param = new Parameter { Name = "(*this)" , Namespace = function.Namespace }; var param = new Parameter { Name = "(*this)" , Namespace = function.Namespace };
var ctx = new MarshalContext(Context, CurrentIndentation) var ctx = new MarshalContext(Context, CurrentIndentation)
{ {
MarshalVarPrefix = valueMarshalName,
Parameter = param Parameter = param
}; };
ctx.VarPrefix.Write(valueMarshalName);
var marshal = new CLIMarshalManagedToNativePrinter(ctx); var marshal = new CLIMarshalManagedToNativePrinter(ctx);
marshal.MarshalValueClassProperties(@class, valueMarshalName); marshal.MarshalValueClassProperties(@class, valueMarshalName);

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

@ -12,12 +12,8 @@ namespace CppSharp.Generators.CSharp
public CSharpMarshalContext(BindingContext context, uint indentation) public CSharpMarshalContext(BindingContext context, uint indentation)
: base(context, indentation) : base(context, indentation)
{ {
ArgumentPrefix = new TextGenerator { CurrentIndentation = indentation };
Cleanup = new TextGenerator { CurrentIndentation = indentation };
} }
public TextGenerator ArgumentPrefix { get; }
public TextGenerator Cleanup { get; }
public bool HasCodeBlock { get; set; } public bool HasCodeBlock { get; set; }
} }

8
src/Generator/Generators/Marshal.cs

@ -9,7 +9,9 @@ namespace CppSharp.Generators
Context = context; Context = context;
Before = new TextGenerator { CurrentIndentation = indentation }; Before = new TextGenerator { CurrentIndentation = indentation };
Return = new TextGenerator { CurrentIndentation = indentation }; Return = new TextGenerator { CurrentIndentation = indentation };
MarshalVarPrefix = string.Empty; Cleanup = new TextGenerator { CurrentIndentation = indentation };
VarPrefix = new TextGenerator();
ArgumentPrefix = new TextGenerator();
this.Indentation = indentation; this.Indentation = indentation;
} }
@ -19,6 +21,9 @@ namespace CppSharp.Generators
public TextGenerator Before { get; } public TextGenerator Before { get; }
public TextGenerator Return { get; } public TextGenerator Return { get; }
public TextGenerator Cleanup { get; }
public TextGenerator VarPrefix;
public TextGenerator ArgumentPrefix;
public string ReturnVarName { get; set; } public string ReturnVarName { get; set; }
public QualifiedType ReturnType { get; set; } public QualifiedType ReturnType { get; set; }
@ -27,7 +32,6 @@ namespace CppSharp.Generators
public int ParameterIndex { get; set; } public int ParameterIndex { get; set; }
public Function Function { get; set; } public Function Function { get; set; }
public string MarshalVarPrefix { get; set; }
public uint Indentation { get; } public uint Indentation { get; }
} }

Loading…
Cancel
Save