Browse Source

Renamed MarshalContext.SupportBefore to Before.

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

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

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

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

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

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

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

4
src/Generator/Generators/Marshal.cs

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

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

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

Loading…
Cancel
Save