Browse Source

Renamed MarshalContext.SupportBefore to Before.

pull/829/head
Joao Matos 8 years ago
parent
commit
73374065f6
  1. 47
      src/Generator/Generators/CLI/CLIMarshal.cs
  2. 42
      src/Generator/Generators/CLI/CLISources.cs
  3. 58
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  4. 38
      src/Generator/Generators/CSharp/CSharpSources.cs
  5. 4
      src/Generator/Generators/Marshal.cs
  6. 48
      src/Generator/Types/Std/Stdlib.cs

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

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using System.Text;
using CppSharp.AST;
@ -44,19 +44,18 @@ namespace CppSharp.Generators.CLI @@ -44,19 +44,18 @@ namespace CppSharp.Generators.CLI
switch (array.SizeType)
{
case ArrayType.ArraySize.Constant:
var supportBefore = Context.SupportBefore;
string value = Generator.GeneratedIdentifier("array") + Context.ParameterIndex;
supportBefore.WriteLine("cli::array<{0}>^ {1} = nullptr;", array.Type, value, array.Size);
supportBefore.WriteLine("if ({0} != 0)", Context.ReturnVarName);
supportBefore.WriteStartBraceIndent();
supportBefore.WriteLine("{0} = gcnew cli::array<{1}>({2});", value, array.Type, array.Size);
supportBefore.WriteLine("for (int i = 0; i < {0}; i++)", array.Size);
Context.Before.WriteLine("cli::array<{0}>^ {1} = nullptr;", array.Type, value, array.Size);
Context.Before.WriteLine("if ({0} != 0)", Context.ReturnVarName);
Context.Before.WriteStartBraceIndent();
Context.Before.WriteLine("{0} = gcnew cli::array<{1}>({2});", value, array.Type, array.Size);
Context.Before.WriteLine("for (int i = 0; i < {0}; i++)", array.Size);
if (array.Type.IsPointerToPrimitiveType(PrimitiveType.Void))
supportBefore.WriteLineIndent("{0}[i] = new ::System::IntPtr({1}[i]);",
Context.Before.WriteLineIndent("{0}[i] = new ::System::IntPtr({1}[i]);",
value, Context.ReturnVarName);
else
supportBefore.WriteLineIndent("{0}[i] = {1}[i];", value, Context.ReturnVarName);
supportBefore.WriteCloseBraceIndent();
Context.Before.WriteLineIndent("{0}[i] = {1}[i];", value, Context.ReturnVarName);
Context.Before.WriteCloseBraceIndent();
Context.Return.Write(value);
break;
case ArrayType.ArraySize.Incomplete:
@ -305,7 +304,7 @@ namespace CppSharp.Generators.CLI @@ -305,7 +304,7 @@ namespace CppSharp.Generators.CLI
if (@class.IsRefType && needsCopy)
{
var name = Generator.GeneratedIdentifier(Context.ReturnVarName);
Context.SupportBefore.WriteLine("auto {0} = new ::{1}({2});", name,
Context.Before.WriteLine("auto {0} = new ::{1}({2});", name,
@class.QualifiedOriginalName, Context.ReturnVarName);
instance = name;
}
@ -458,15 +457,15 @@ namespace CppSharp.Generators.CLI @@ -458,15 +457,15 @@ namespace CppSharp.Generators.CLI
if (string.IsNullOrEmpty(Context.ReturnVarName))
{
const string pinnedPtr = "__pinnedPtr";
Context.SupportBefore.WriteLine("cli::pin_ptr<{0}> {1} = &{2}[0];",
Context.Before.WriteLine("cli::pin_ptr<{0}> {1} = &{2}[0];",
array.Type, pinnedPtr, Context.Parameter.Name);
const string arrayPtr = "__arrayPtr";
Context.SupportBefore.WriteLine("{0}* {1} = {2};", array.Type, arrayPtr, pinnedPtr);
Context.Before.WriteLine("{0}* {1} = {2};", array.Type, arrayPtr, pinnedPtr);
Context.Return.Write("({0} (&)[{1}]) {2}", array.Type, array.Size, arrayPtr);
}
else
{
var supportBefore = Context.SupportBefore;
var supportBefore = Context.Before;
supportBefore.WriteLine("if ({0} != nullptr)", Context.ArgName);
supportBefore.WriteStartBraceIndent();
supportBefore.WriteLine("for (int i = 0; i < {0}; i++)", array.Size);
@ -519,7 +518,7 @@ namespace CppSharp.Generators.CLI @@ -519,7 +518,7 @@ namespace CppSharp.Generators.CLI
pointee.IsPrimitiveType(PrimitiveType.WideChar)) &&
pointer.QualifiedPointee.Qualifiers.IsConst)
{
Context.SupportBefore.WriteLine(
Context.Before.WriteLine(
"auto _{0} = clix::marshalString<clix::E_UTF8>({1});",
Context.ArgName, Context.Parameter.Name);
@ -708,8 +707,8 @@ namespace CppSharp.Generators.CLI @@ -708,8 +707,8 @@ namespace CppSharp.Generators.CLI
(method.OperatorKind != CXXOperatorKind.EqualEqual &&
method.OperatorKind != CXXOperatorKind.ExclaimEqual)))
{
Context.SupportBefore.WriteLine("if (ReferenceEquals({0}, nullptr))", Context.Parameter.Name);
Context.SupportBefore.WriteLineIndent(
Context.Before.WriteLine("if (ReferenceEquals({0}, nullptr))", Context.Parameter.Name);
Context.Before.WriteLineIndent(
"throw gcnew ::System::ArgumentNullException(\"{0}\", " +
"\"Cannot be null because it is a C++ reference (&).\");",
Context.Parameter.Name);
@ -741,7 +740,7 @@ namespace CppSharp.Generators.CLI @@ -741,7 +740,7 @@ namespace CppSharp.Generators.CLI
var marshalVar = Context.MarshalVarPrefix + "_marshal" +
Context.ParameterIndex++;
Context.SupportBefore.WriteLine("auto {0} = ::{1}();", marshalVar,
Context.Before.WriteLine("auto {0} = ::{1}();", marshalVar,
@class.QualifiedOriginalName);
MarshalValueClassProperties(@class, marshalVar);
@ -789,8 +788,8 @@ namespace CppSharp.Generators.CLI @@ -789,8 +788,8 @@ namespace CppSharp.Generators.CLI
Context.ParameterIndex = marshalCtx.ParameterIndex;
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Context.SupportBefore.Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Context.Before.Write(marshal.Context.Before);
Type type;
Class @class;
@ -800,15 +799,15 @@ namespace CppSharp.Generators.CLI @@ -800,15 +799,15 @@ namespace CppSharp.Generators.CLI
if (isRef)
{
Context.SupportBefore.WriteLine("if ({0} != nullptr)", fieldRef);
Context.SupportBefore.PushIndent();
Context.Before.WriteLine("if ({0} != nullptr)", fieldRef);
Context.Before.PushIndent();
}
Context.SupportBefore.WriteLine("{0}.{1} = {2};", marshalVar,
Context.Before.WriteLine("{0}.{1} = {2};", marshalVar,
property.Field.OriginalName, marshal.Context.Return);
if (isRef)
Context.SupportBefore.PopIndent();
Context.Before.PopIndent();
}
public override bool VisitFieldDecl(Field field)

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

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
@ -421,8 +421,8 @@ namespace CppSharp.Generators.CLI @@ -421,8 +421,8 @@ namespace CppSharp.Generators.CLI
variable += string.Format("({0})", marshal2.Context.Return);
}
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
if (marshal.Context.Return.StringBuilder.Length > 0)
{
@ -495,8 +495,8 @@ namespace CppSharp.Generators.CLI @@ -495,8 +495,8 @@ namespace CppSharp.Generators.CLI
var marshal = new CLIMarshalNativeToManagedPrinter(ctx);
decl.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
WriteLine("return {0};", marshal.Context.Return);
}
@ -699,8 +699,8 @@ namespace CppSharp.Generators.CLI @@ -699,8 +699,8 @@ namespace CppSharp.Generators.CLI
var marshal = new CLIMarshalNativeToManagedPrinter(ctx);
property.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
WriteLine("{0} = {1};", property.Field.Name, marshal.Context.Return);
}
@ -846,8 +846,8 @@ namespace CppSharp.Generators.CLI @@ -846,8 +846,8 @@ namespace CppSharp.Generators.CLI
var marshal = new CLIMarshalManagedToNativePrinter(ctx);
param.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
names.Add(marshal.Context.Return);
}
@ -880,8 +880,8 @@ namespace CppSharp.Generators.CLI @@ -880,8 +880,8 @@ namespace CppSharp.Generators.CLI
var marshal = new CLIMarshalNativeToManagedPrinter(ctx);
property.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
WriteLine("this->{0} = {1};", property.Name, marshal.Context.Return);
}
@ -956,8 +956,8 @@ namespace CppSharp.Generators.CLI @@ -956,8 +956,8 @@ namespace CppSharp.Generators.CLI
var marshal = new CLIMarshalManagedToNativePrinter(ctx);
marshal.MarshalValueClassProperties(@class, valueMarshalName);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
}
var @params = GenerateFunctionParamsMarshal(function.Parameters, function);
@ -1032,8 +1032,8 @@ namespace CppSharp.Generators.CLI @@ -1032,8 +1032,8 @@ namespace CppSharp.Generators.CLI
var marshal = new CLIMarshalNativeToManagedPrinter(ctx);
param.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
WriteLine("{0} = {1};",param.Name,marshal.Context.Return);
}
@ -1065,8 +1065,8 @@ namespace CppSharp.Generators.CLI @@ -1065,8 +1065,8 @@ namespace CppSharp.Generators.CLI
var marshal = new CLIMarshalNativeToManagedPrinter(ctx);
retType.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
// Special case for indexer - needs to dereference if the internal
// function is a pointer type and the property is not.
@ -1190,8 +1190,8 @@ namespace CppSharp.Generators.CLI @@ -1190,8 +1190,8 @@ namespace CppSharp.Generators.CLI
if (param.IsInOut)
{
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
WriteLine("{0} {1} = {2};", type, argName, marshal.Context.Return);
}
@ -1200,8 +1200,8 @@ namespace CppSharp.Generators.CLI @@ -1200,8 +1200,8 @@ namespace CppSharp.Generators.CLI
}
else
{
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
WriteLine("auto {0}{1} = {2};", marshal.VarPrefix, argName,
marshal.Context.Return);

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

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using System.Text;
using CppSharp.AST;
@ -82,7 +82,7 @@ namespace CppSharp.Generators.CSharp @@ -82,7 +82,7 @@ namespace CppSharp.Generators.CSharp
switch (array.SizeType)
{
case ArrayType.ArraySize.Constant:
var supportBefore = Context.SupportBefore;
var supportBefore = Context.Before;
string value = Generator.GeneratedIdentifier("value");
supportBefore.WriteLine("{0}[] {1} = null;", array.Type, value, array.Size);
supportBefore.WriteLine("if ({0} != null)", Context.ReturnVarName);
@ -243,7 +243,7 @@ namespace CppSharp.Generators.CSharp @@ -243,7 +243,7 @@ namespace CppSharp.Generators.CSharp
var ptrName = Generator.GeneratedIdentifier("ptr") +
Context.ParameterIndex;
Context.SupportBefore.WriteLine("var {0} = {1};", ptrName,
Context.Before.WriteLine("var {0} = {1};", ptrName,
Context.ReturnVarName);
var res = $"{ptrName} == IntPtr.Zero? null : ({typedef})Marshal.GetDelegateForFunctionPointer({ptrName}, typeof({typedef}))";
@ -258,7 +258,7 @@ namespace CppSharp.Generators.CSharp @@ -258,7 +258,7 @@ namespace CppSharp.Generators.CSharp
{
var ptrName = Generator.GeneratedIdentifier("ptr") + Context.ParameterIndex;
Context.SupportBefore.WriteLine("var {0} = {1};", ptrName,
Context.Before.WriteLine("var {0} = {1};", ptrName,
Context.ReturnVarName);
Context.Return.Write("({1})Marshal.GetDelegateForFunctionPointer({0}, typeof({1}))",
@ -302,13 +302,13 @@ namespace CppSharp.Generators.CSharp @@ -302,13 +302,13 @@ namespace CppSharp.Generators.CSharp
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx);
parameter.Type.Visit(marshal);
if (!string.IsNullOrWhiteSpace(ctx.SupportBefore))
Context.SupportBefore.WriteLine(ctx.SupportBefore);
if (!string.IsNullOrWhiteSpace(ctx.Before))
Context.Before.WriteLine(ctx.Before);
if (!string.IsNullOrWhiteSpace(ctx.Return) &&
!parameter.Type.IsPrimitiveTypeConvertibleToRef())
{
Context.SupportBefore.WriteLine("var _{0} = {1};", parameter.Name,
Context.Before.WriteLine("var _{0} = {1};", parameter.Name,
ctx.Return);
}
@ -328,19 +328,19 @@ namespace CppSharp.Generators.CSharp @@ -328,19 +328,19 @@ namespace CppSharp.Generators.CSharp
var originalClass = @class.OriginalClass ?? @class;
var ret = Generator.GeneratedIdentifier("result") + Context.ParameterIndex;
var qualifiedIdentifier = @class.Visit(typePrinter);
Context.SupportBefore.WriteLine("{0} {1};", qualifiedIdentifier, ret);
Context.SupportBefore.WriteLine("if ({0} == IntPtr.Zero) {1} = {2};", Context.ReturnVarName, ret,
Context.Before.WriteLine("{0} {1};", qualifiedIdentifier, ret);
Context.Before.WriteLine("if ({0} == IntPtr.Zero) {1} = {2};", Context.ReturnVarName, ret,
originalClass.IsRefType ? "null" : string.Format("new {0}()", qualifiedClass));
if (originalClass.IsRefType)
{
Context.SupportBefore.WriteLine(
Context.Before.WriteLine(
"else if ({0}.NativeToManagedMap.ContainsKey({1}))", qualifiedClass, Context.ReturnVarName);
Context.SupportBefore.WriteLineIndent("{0} = ({1}) {2}.NativeToManagedMap[{3}];",
Context.Before.WriteLineIndent("{0} = ({1}) {2}.NativeToManagedMap[{3}];",
ret, qualifiedIdentifier, qualifiedClass, Context.ReturnVarName);
var dtor = originalClass.Destructors.FirstOrDefault();
if (dtor != null && dtor.IsVirtual)
{
Context.SupportBefore.WriteLine("else {0}{1} = ({2}) {3}.{4}({5}{6});",
Context.Before.WriteLine("else {0}{1} = ({2}) {3}.{4}({5}{6});",
MarshalsParameter
? string.Empty
: string.Format("{0}.NativeToManagedMap[{1}] = ", qualifiedClass, Context.ReturnVarName),
@ -349,13 +349,13 @@ namespace CppSharp.Generators.CSharp @@ -349,13 +349,13 @@ namespace CppSharp.Generators.CSharp
}
else
{
Context.SupportBefore.WriteLine("else {0} = {1}.{2}({3});", ret, qualifiedClass,
Context.Before.WriteLine("else {0} = {1}.{2}({3});", ret, qualifiedClass,
Helpers.CreateInstanceIdentifier, Context.ReturnVarName);
}
}
else
{
Context.SupportBefore.WriteLine("else {0} = {1}.{2}({3});", ret, qualifiedClass,
Context.Before.WriteLine("else {0} = {1}.{2}({3});", ret, qualifiedClass,
Helpers.CreateInstanceIdentifier, Context.ReturnVarName);
}
return ret;
@ -408,19 +408,19 @@ namespace CppSharp.Generators.CSharp @@ -408,19 +408,19 @@ namespace CppSharp.Generators.CSharp
case ArrayType.ArraySize.Constant:
if (string.IsNullOrEmpty(Context.ReturnVarName))
{
Context.SupportBefore.WriteLine("if ({0} == null || {0}.Length != {1})",
Context.Before.WriteLine("if ({0} == null || {0}.Length != {1})",
Context.Parameter.Name, array.Size);
ThrowArgumentOutOfRangeException();
var ptr = "__ptr" + Context.ParameterIndex;
Context.SupportBefore.WriteLine("fixed ({0}* {1} = {2})",
Context.Before.WriteLine("fixed ({0}* {1} = {2})",
array.Type, ptr, Context.Parameter.Name);
Context.SupportBefore.WriteStartBraceIndent();
Context.Before.WriteStartBraceIndent();
Context.Return.Write("new global::System.IntPtr({0})", ptr);
Context.HasCodeBlock = true;
}
else
{
var supportBefore = Context.SupportBefore;
var supportBefore = Context.Before;
supportBefore.WriteLine("if ({0} != null)", Context.ArgName);
supportBefore.WriteStartBraceIndent();
Class @class;
@ -472,7 +472,7 @@ namespace CppSharp.Generators.CSharp @@ -472,7 +472,7 @@ namespace CppSharp.Generators.CSharp
private void ThrowArgumentOutOfRangeException()
{
Context.SupportBefore.WriteLineIndent(
Context.Before.WriteLineIndent(
"throw new ArgumentOutOfRangeException(\"{0}\", " +
"\"The dimensions of the provided array don't match the required size.\");",
Context.Parameter.Name);
@ -499,17 +499,17 @@ namespace CppSharp.Generators.CSharp @@ -499,17 +499,17 @@ namespace CppSharp.Generators.CSharp
if (templateSubstitution != null)
{
var castParam = $"__{Context.Parameter.Name}{Context.ParameterIndex}";
Context.SupportBefore.WriteLine(
Context.Before.WriteLine(
$"var {castParam} = ({templateSubstitution}) (object) {Context.Parameter.Name};");
Context.SupportBefore.WriteLine($"{pointer} {refParamPtr} = &{castParam};");
Context.Before.WriteLine($"{pointer} {refParamPtr} = &{castParam};");
Context.Return.Write(refParamPtr);
}
else
{
Context.SupportBefore.WriteLine(
Context.Before.WriteLine(
$"fixed ({pointer} {refParamPtr} = &{Context.Parameter.Name})");
Context.HasCodeBlock = true;
Context.SupportBefore.WriteStartBraceIndent();
Context.Before.WriteStartBraceIndent();
Context.Return.Write(refParamPtr);
}
return true;
@ -550,13 +550,13 @@ namespace CppSharp.Generators.CSharp @@ -550,13 +550,13 @@ namespace CppSharp.Generators.CSharp
if (Context.Parameter.Usage == ParameterUsage.Out)
{
var qualifiedIdentifier = (@class.OriginalClass ?? @class).Visit(typePrinter);
Context.SupportBefore.WriteLine("var {0} = new {1}.{2}();",
Context.Before.WriteLine("var {0} = new {1}.{2}();",
Generator.GeneratedIdentifier(Context.ArgName), qualifiedIdentifier,
Helpers.InternalStruct);
}
else
{
Context.SupportBefore.WriteLine("var {0} = {1}.{2};",
Context.Before.WriteLine("var {0} = {1}.{2};",
Generator.GeneratedIdentifier(Context.ArgName),
Context.Parameter.Name,
Helpers.InstanceIdentifier);
@ -582,9 +582,9 @@ namespace CppSharp.Generators.CSharp @@ -582,9 +582,9 @@ namespace CppSharp.Generators.CSharp
var typeName = Type.TypePrinterDelegate(finalPointee);
if (param.IsInOut)
Context.SupportBefore.WriteLine("{0} _{1} = {1};", typeName, param.Name);
Context.Before.WriteLine("{0} _{1} = {1};", typeName, param.Name);
else
Context.SupportBefore.WriteLine("{0} _{1};", typeName, param.Name);
Context.Before.WriteLine("{0} _{1};", typeName, param.Name);
Context.Return.Write("&_{0}", param.Name);
}
@ -755,8 +755,8 @@ namespace CppSharp.Generators.CSharp @@ -755,8 +755,8 @@ namespace CppSharp.Generators.CSharp
(method.OperatorKind != CXXOperatorKind.EqualEqual &&
method.OperatorKind != CXXOperatorKind.ExclaimEqual))
{
Context.SupportBefore.WriteLine("if (ReferenceEquals({0}, null))", param);
Context.SupportBefore.WriteLineIndent(
Context.Before.WriteLine("if (ReferenceEquals({0}, null))", param);
Context.Before.WriteLineIndent(
"throw new global::System.ArgumentNullException(\"{0}\", " +
"\"Cannot be null because it is a C++ reference (&).\");",
param);

38
src/Generator/Generators/CSharp/CSharpSources.cs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
@ -773,8 +773,8 @@ namespace CppSharp.Generators.CSharp @@ -773,8 +773,8 @@ namespace CppSharp.Generators.CSharp
var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
decl.CSharpMarshalToNative(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
if (ctx.HasCodeBlock)
PushIndent();
@ -854,8 +854,8 @@ namespace CppSharp.Generators.CSharp @@ -854,8 +854,8 @@ namespace CppSharp.Generators.CSharp
}
param.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
if (ctx.HasCodeBlock)
PushIndent();
@ -1038,8 +1038,8 @@ namespace CppSharp.Generators.CSharp @@ -1038,8 +1038,8 @@ namespace CppSharp.Generators.CSharp
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx);
decl.CSharpMarshalToManaged(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
if (ctx.HasCodeBlock)
PushIndent();
@ -1086,8 +1086,8 @@ namespace CppSharp.Generators.CSharp @@ -1086,8 +1086,8 @@ namespace CppSharp.Generators.CSharp
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx);
field.CSharpMarshalToManaged(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
if (ctx.HasCodeBlock)
PushIndent();
@ -1552,8 +1552,8 @@ namespace CppSharp.Generators.CSharp @@ -1552,8 +1552,8 @@ namespace CppSharp.Generators.CSharp
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx) { MarshalsParameter = true };
param.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
marshals.Add(marshal.Context.Return);
}
@ -1593,8 +1593,8 @@ namespace CppSharp.Generators.CSharp @@ -1593,8 +1593,8 @@ namespace CppSharp.Generators.CSharp
var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
method.OriginalReturnType.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
if (method.HasIndirectReturnTypeParameter)
{
@ -2712,8 +2712,8 @@ namespace CppSharp.Generators.CSharp @@ -2712,8 +2712,8 @@ namespace CppSharp.Generators.CSharp
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx);
retType.CSharpMarshalToManaged(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
if (ctx.HasCodeBlock)
PushIndent();
@ -2809,8 +2809,8 @@ namespace CppSharp.Generators.CSharp @@ -2809,8 +2809,8 @@ namespace CppSharp.Generators.CSharp
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx);
param.CSharpMarshalToManaged(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
WriteLine("{0} = {1};", param.Name, marshal.Context.Return);
@ -2883,8 +2883,8 @@ namespace CppSharp.Generators.CSharp @@ -2883,8 +2883,8 @@ namespace CppSharp.Generators.CSharp
if (string.IsNullOrEmpty(marshal.Context.Return))
throw new Exception("Cannot marshal argument of function");
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
if (paramMarshal.HasUsingBlock)
PushIndent();

4
src/Generator/Generators/Marshal.cs

@ -7,7 +7,7 @@ namespace CppSharp.Generators @@ -7,7 +7,7 @@ namespace CppSharp.Generators
public MarshalContext(BindingContext context)
{
Context = context;
SupportBefore = new TextGenerator();
Before = new TextGenerator();
Return = new TextGenerator();
MarshalVarPrefix = string.Empty;
}
@ -16,7 +16,7 @@ namespace CppSharp.Generators @@ -16,7 +16,7 @@ namespace CppSharp.Generators
public MarshalPrinter<MarshalContext> MarshalToNative;
public TextGenerator SupportBefore { get; private set; }
public TextGenerator Before { get; private set; }
public TextGenerator Return { get; private set; }
public string ReturnVarName { get; set; }

48
src/Generator/Types/Std/Stdlib.cs

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using CppSharp.AST;
using CppSharp.AST.Extensions;
using CppSharp.Generators;
@ -79,9 +79,9 @@ namespace CppSharp.Types.Std @@ -79,9 +79,9 @@ namespace CppSharp.Types.Std
{
var varAllocator = $"__allocator{ctx.ParameterIndex}";
var varBasicString = $"__basicString{ctx.ParameterIndex}";
ctx.SupportBefore.WriteLine($@"var {varAllocator} = new {
ctx.Before.WriteLine($@"var {varAllocator} = new {
allocatorChar.Visit(typePrinter)}();");
ctx.SupportBefore.WriteLine($@"var {varBasicString} = new {
ctx.Before.WriteLine($@"var {varBasicString} = new {
basicString.Visit(typePrinter)}({ctx.Parameter.Name}, {varAllocator});");
ctx.Return.Write($"{varBasicString}.{Helpers.InstanceIdentifier}");
ctx.Cleanup.WriteLine($@"{varBasicString}.Dispose({
@ -107,10 +107,10 @@ namespace CppSharp.Types.Std @@ -107,10 +107,10 @@ namespace CppSharp.Types.Std
else
{
const string varBasicString = "__basicStringRet";
ctx.SupportBefore.WriteLine("using (var {0} = {1}.{2}({3}))",
ctx.Before.WriteLine("using (var {0} = {1}.{2}({3}))",
varBasicString, basicString.Visit(typePrinter),
Helpers.CreateInstanceIdentifier, ctx.ReturnVarName);
ctx.SupportBefore.WriteStartBraceIndent();
ctx.Before.WriteStartBraceIndent();
ctx.Return.Write("{0}.{1}{2}", varBasicString, c_str.Name,
c_str is Method ? "()" : string.Empty);
ctx.HasCodeBlock = true;
@ -208,11 +208,11 @@ namespace CppSharp.Types.Std @@ -208,11 +208,11 @@ namespace CppSharp.Types.Std
var cppTypePrinter = new CppTypePrinter();
var nativeType = type.Type.Visit(cppTypePrinter);
ctx.SupportBefore.WriteLine("auto {0} = std::vector<{1}>();",
ctx.Before.WriteLine("auto {0} = std::vector<{1}>();",
tmpVarName, nativeType);
ctx.SupportBefore.WriteLine("for each({0} _element in {1})",
ctx.Before.WriteLine("for each({0} _element in {1})",
managedType, entryString);
ctx.SupportBefore.WriteStartBraceIndent();
ctx.Before.WriteStartBraceIndent();
{
var param = new Parameter
{
@ -229,21 +229,21 @@ namespace CppSharp.Types.Std @@ -229,21 +229,21 @@ namespace CppSharp.Types.Std
var marshal = new CLIMarshalManagedToNativePrinter(elementCtx);
type.Type.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
ctx.SupportBefore.Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
ctx.Before.Write(marshal.Context.Before);
if (isPointerToPrimitive)
ctx.SupportBefore.WriteLine("auto _marshalElement = {0}.ToPointer();",
ctx.Before.WriteLine("auto _marshalElement = {0}.ToPointer();",
marshal.Context.Return);
else
ctx.SupportBefore.WriteLine("auto _marshalElement = {0};",
ctx.Before.WriteLine("auto _marshalElement = {0};",
marshal.Context.Return);
ctx.SupportBefore.WriteLine("{0}.push_back(_marshalElement);",
ctx.Before.WriteLine("{0}.push_back(_marshalElement);",
tmpVarName);
}
ctx.SupportBefore.WriteCloseBraceIndent();
ctx.Before.WriteCloseBraceIndent();
ctx.Return.Write(tmpVarName);
}
@ -258,12 +258,12 @@ namespace CppSharp.Types.Std @@ -258,12 +258,12 @@ namespace CppSharp.Types.Std
: type.Type;
var tmpVarName = "_tmp" + ctx.ArgName;
ctx.SupportBefore.WriteLine(
ctx.Before.WriteLine(
"auto {0} = gcnew System::Collections::Generic::List<{1}>();",
tmpVarName, managedType);
ctx.SupportBefore.WriteLine("for(auto _element : {0})",
ctx.Before.WriteLine("for(auto _element : {0})",
ctx.ReturnVarName);
ctx.SupportBefore.WriteStartBraceIndent();
ctx.Before.WriteStartBraceIndent();
{
var elementCtx = new MarshalContext(ctx.Context)
{
@ -274,20 +274,20 @@ namespace CppSharp.Types.Std @@ -274,20 +274,20 @@ namespace CppSharp.Types.Std
var marshal = new CLIMarshalNativeToManagedPrinter(elementCtx);
type.Type.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
ctx.SupportBefore.Write(marshal.Context.SupportBefore);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
ctx.Before.Write(marshal.Context.Before);
ctx.SupportBefore.WriteLine("auto _marshalElement = {0};",
ctx.Before.WriteLine("auto _marshalElement = {0};",
marshal.Context.Return);
if (isPointerToPrimitive)
ctx.SupportBefore.WriteLine("{0}->Add({1}(_marshalElement));",
ctx.Before.WriteLine("{0}->Add({1}(_marshalElement));",
tmpVarName, managedType);
else
ctx.SupportBefore.WriteLine("{0}->Add(_marshalElement);",
ctx.Before.WriteLine("{0}->Add(_marshalElement);",
tmpVarName);
}
ctx.SupportBefore.WriteCloseBraceIndent();
ctx.Before.WriteCloseBraceIndent();
ctx.Return.Write(tmpVarName);
}
@ -391,7 +391,7 @@ namespace CppSharp.Types.Std @@ -391,7 +391,7 @@ namespace CppSharp.Types.Std
if (!ctx.Parameter.Type.Desugar().IsPointer())
marshal.ArgumentPrefix.Write("*");
var marshalCtxName = string.Format("ctx_{0}", ctx.Parameter.Name);
ctx.SupportBefore.WriteLine("msclr::interop::marshal_context {0};", marshalCtxName);
ctx.Before.WriteLine("msclr::interop::marshal_context {0};", marshalCtxName);
ctx.Return.Write("{0}.marshal_as<std::ostream*>({1})",
marshalCtxName, ctx.Parameter.Name);
}

Loading…
Cancel
Save