Browse Source

Merge pull request #323 from ddobrev/master

Moved the code for creating a ref object by value from the marshalling printer to a valid ctor by value
pull/324/head
João Matos 12 years ago
parent
commit
513c58e970
  1. 607
      src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/AST.cs
  2. 40
      src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/CppParser.cs
  3. 9
      src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/Target.cs
  4. 607
      src/CppParser/Bindings/CSharp/i686-pc-win32-msvc/AST.cs
  5. 40
      src/CppParser/Bindings/CSharp/i686-pc-win32-msvc/CppParser.cs
  6. 9
      src/CppParser/Bindings/CSharp/i686-pc-win32-msvc/Target.cs
  7. 607
      src/CppParser/Bindings/CSharp/x86_64-linux-gnu/AST.cs
  8. 40
      src/CppParser/Bindings/CSharp/x86_64-linux-gnu/CppParser.cs
  9. 9
      src/CppParser/Bindings/CSharp/x86_64-linux-gnu/Target.cs
  10. 51
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  11. 67
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs

607
src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/AST.cs

File diff suppressed because it is too large Load Diff

40
src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/CppParser.cs

@ -217,8 +217,15 @@ namespace CppSharp @@ -217,8 +217,15 @@ namespace CppSharp
{
}
private static global::System.IntPtr __CopyValue(ParserOptions.Internal native)
{
var ret = Marshal.AllocHGlobal(104);
CppSharp.Parser.ParserOptions.Internal.cctor_2(ret, new global::System.IntPtr(&native));
return ret;
}
internal ParserOptions(ParserOptions.Internal native)
: this(&native)
: this(__CopyValue(native))
{
}
@ -597,8 +604,15 @@ namespace CppSharp @@ -597,8 +604,15 @@ namespace CppSharp
{
}
private static global::System.IntPtr __CopyValue(ParserDiagnostic.Internal native)
{
var ret = Marshal.AllocHGlobal(36);
CppSharp.Parser.ParserDiagnostic.Internal.cctor_1(ret, new global::System.IntPtr(&native));
return ret;
}
internal ParserDiagnostic(ParserDiagnostic.Internal native)
: this(&native)
: this(__CopyValue(native))
{
}
@ -765,8 +779,15 @@ namespace CppSharp @@ -765,8 +779,15 @@ namespace CppSharp
{
}
private static global::System.IntPtr __CopyValue(ParserResult.Internal native)
{
var ret = Marshal.AllocHGlobal(28);
CppSharp.Parser.ParserResult.Internal.cctor_1(ret, new global::System.IntPtr(&native));
return ret;
}
internal ParserResult(ParserResult.Internal native)
: this(&native)
: this(__CopyValue(native))
{
}
@ -797,9 +818,7 @@ namespace CppSharp @@ -797,9 +818,7 @@ namespace CppSharp
{
var __ret = new CppSharp.Parser.ParserDiagnostic.Internal();
Internal.getDiagnostics_0(new IntPtr(&__ret), __Instance, i);
var __instance = Marshal.AllocHGlobal(36);
CppSharp.Parser.ParserDiagnostic.Internal.cctor_1(__instance, new global::System.IntPtr(&__ret));
return new CppSharp.Parser.ParserDiagnostic(__instance);
return new CppSharp.Parser.ParserDiagnostic(__ret);
}
public void addDiagnostics(CppSharp.Parser.ParserDiagnostic s)
@ -901,8 +920,15 @@ namespace CppSharp @@ -901,8 +920,15 @@ namespace CppSharp
{
}
private static global::System.IntPtr __CopyValue(ClangParser.Internal native)
{
global::System.IntPtr ret = Marshal.AllocHGlobal(1);
CppSharp.Runtime.Helpers.memcpy(ret, new IntPtr(&native), new UIntPtr(1));
return ret;
}
internal ClangParser(ClangParser.Internal native)
: this(&native)
: this(__CopyValue(native))
{
}

9
src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/Target.cs

@ -177,8 +177,15 @@ namespace CppSharp @@ -177,8 +177,15 @@ namespace CppSharp
{
}
private static global::System.IntPtr __CopyValue(ParserTargetInfo.Internal native)
{
var ret = Marshal.AllocHGlobal(164);
CppSharp.Parser.ParserTargetInfo.Internal.cctor_2(ret, new global::System.IntPtr(&native));
return ret;
}
internal ParserTargetInfo(ParserTargetInfo.Internal native)
: this(&native)
: this(__CopyValue(native))
{
}

607
src/CppParser/Bindings/CSharp/i686-pc-win32-msvc/AST.cs

File diff suppressed because it is too large Load Diff

40
src/CppParser/Bindings/CSharp/i686-pc-win32-msvc/CppParser.cs

@ -217,8 +217,15 @@ namespace CppSharp @@ -217,8 +217,15 @@ namespace CppSharp
{
}
private static global::System.IntPtr __CopyValue(ParserOptions.Internal native)
{
var ret = Marshal.AllocHGlobal(128);
CppSharp.Parser.ParserOptions.Internal.cctor_2(ret, new global::System.IntPtr(&native));
return ret;
}
internal ParserOptions(ParserOptions.Internal native)
: this(&native)
: this(__CopyValue(native))
{
}
@ -597,8 +604,15 @@ namespace CppSharp @@ -597,8 +604,15 @@ namespace CppSharp
{
}
private static global::System.IntPtr __CopyValue(ParserDiagnostic.Internal native)
{
var ret = Marshal.AllocHGlobal(60);
CppSharp.Parser.ParserDiagnostic.Internal.cctor_1(ret, new global::System.IntPtr(&native));
return ret;
}
internal ParserDiagnostic(ParserDiagnostic.Internal native)
: this(&native)
: this(__CopyValue(native))
{
}
@ -765,8 +779,15 @@ namespace CppSharp @@ -765,8 +779,15 @@ namespace CppSharp
{
}
private static global::System.IntPtr __CopyValue(ParserResult.Internal native)
{
var ret = Marshal.AllocHGlobal(28);
CppSharp.Parser.ParserResult.Internal.cctor_1(ret, new global::System.IntPtr(&native));
return ret;
}
internal ParserResult(ParserResult.Internal native)
: this(&native)
: this(__CopyValue(native))
{
}
@ -797,9 +818,7 @@ namespace CppSharp @@ -797,9 +818,7 @@ namespace CppSharp
{
var __ret = new CppSharp.Parser.ParserDiagnostic.Internal();
Internal.getDiagnostics_0(__Instance, new IntPtr(&__ret), i);
var __instance = Marshal.AllocHGlobal(60);
CppSharp.Parser.ParserDiagnostic.Internal.cctor_1(__instance, new global::System.IntPtr(&__ret));
return new CppSharp.Parser.ParserDiagnostic(__instance);
return new CppSharp.Parser.ParserDiagnostic(__ret);
}
public void addDiagnostics(CppSharp.Parser.ParserDiagnostic s)
@ -901,8 +920,15 @@ namespace CppSharp @@ -901,8 +920,15 @@ namespace CppSharp
{
}
private static global::System.IntPtr __CopyValue(ClangParser.Internal native)
{
global::System.IntPtr ret = Marshal.AllocHGlobal(1);
CppSharp.Runtime.Helpers.memcpy(ret, new IntPtr(&native), new UIntPtr(1));
return ret;
}
internal ClangParser(ClangParser.Internal native)
: this(&native)
: this(__CopyValue(native))
{
}

9
src/CppParser/Bindings/CSharp/i686-pc-win32-msvc/Target.cs

@ -177,8 +177,15 @@ namespace CppSharp @@ -177,8 +177,15 @@ namespace CppSharp
{
}
private static global::System.IntPtr __CopyValue(ParserTargetInfo.Internal native)
{
var ret = Marshal.AllocHGlobal(176);
CppSharp.Parser.ParserTargetInfo.Internal.cctor_2(ret, new global::System.IntPtr(&native));
return ret;
}
internal ParserTargetInfo(ParserTargetInfo.Internal native)
: this(&native)
: this(__CopyValue(native))
{
}

607
src/CppParser/Bindings/CSharp/x86_64-linux-gnu/AST.cs

File diff suppressed because it is too large Load Diff

40
src/CppParser/Bindings/CSharp/x86_64-linux-gnu/CppParser.cs

@ -217,8 +217,15 @@ namespace CppSharp @@ -217,8 +217,15 @@ namespace CppSharp
{
}
private static global::System.IntPtr __CopyValue(ParserOptions.Internal native)
{
var ret = Marshal.AllocHGlobal(168);
CppSharp.Parser.ParserOptions.Internal.cctor_1(ret, new global::System.IntPtr(&native));
return ret;
}
internal ParserOptions(ParserOptions.Internal native)
: this(&native)
: this(__CopyValue(native))
{
}
@ -597,8 +604,15 @@ namespace CppSharp @@ -597,8 +604,15 @@ namespace CppSharp
{
}
private static global::System.IntPtr __CopyValue(ParserDiagnostic.Internal native)
{
var ret = Marshal.AllocHGlobal(32);
CppSharp.Parser.ParserDiagnostic.Internal.cctor_1(ret, new global::System.IntPtr(&native));
return ret;
}
internal ParserDiagnostic(ParserDiagnostic.Internal native)
: this(&native)
: this(__CopyValue(native))
{
}
@ -765,8 +779,15 @@ namespace CppSharp @@ -765,8 +779,15 @@ namespace CppSharp
{
}
private static global::System.IntPtr __CopyValue(ParserResult.Internal native)
{
var ret = Marshal.AllocHGlobal(56);
CppSharp.Parser.ParserResult.Internal.cctor_1(ret, new global::System.IntPtr(&native));
return ret;
}
internal ParserResult(ParserResult.Internal native)
: this(&native)
: this(__CopyValue(native))
{
}
@ -797,9 +818,7 @@ namespace CppSharp @@ -797,9 +818,7 @@ namespace CppSharp
{
var __ret = new CppSharp.Parser.ParserDiagnostic.Internal();
Internal.getDiagnostics_0(new IntPtr(&__ret), __Instance, i);
var __instance = Marshal.AllocHGlobal(32);
CppSharp.Parser.ParserDiagnostic.Internal.cctor_1(__instance, new global::System.IntPtr(&__ret));
return new CppSharp.Parser.ParserDiagnostic(__instance);
return new CppSharp.Parser.ParserDiagnostic(__ret);
}
public void addDiagnostics(CppSharp.Parser.ParserDiagnostic s)
@ -901,8 +920,15 @@ namespace CppSharp @@ -901,8 +920,15 @@ namespace CppSharp
{
}
private static global::System.IntPtr __CopyValue(ClangParser.Internal native)
{
global::System.IntPtr ret = Marshal.AllocHGlobal(1);
CppSharp.Runtime.Helpers.memcpy(ret, new IntPtr(&native), new UIntPtr(1));
return ret;
}
internal ClangParser(ClangParser.Internal native)
: this(&native)
: this(__CopyValue(native))
{
}

9
src/CppParser/Bindings/CSharp/x86_64-linux-gnu/Target.cs

@ -177,8 +177,15 @@ namespace CppSharp @@ -177,8 +177,15 @@ namespace CppSharp
{
}
private static global::System.IntPtr __CopyValue(ParserTargetInfo.Internal native)
{
var ret = Marshal.AllocHGlobal(160);
CppSharp.Parser.ParserTargetInfo.Internal.cctor_0(ret, new global::System.IntPtr(&native));
return ret;
}
internal ParserTargetInfo(ParserTargetInfo.Internal native)
: this(&native)
: this(__CopyValue(native))
{
}

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

@ -61,8 +61,6 @@ namespace CppSharp.Generators.CSharp @@ -61,8 +61,6 @@ namespace CppSharp.Generators.CSharp
Context.MarshalToManaged = this;
}
public int VarSuffix { get; set; }
public static string QualifiedIdentifier(Declaration decl)
{
var names = new List<string> { decl.Name };
@ -248,59 +246,10 @@ namespace CppSharp.Generators.CSharp @@ -248,59 +246,10 @@ namespace CppSharp.Generators.CSharp
public override bool VisitClassDecl(Class @class)
{
var ctx = Context as CSharpMarshalContext;
var instance = Context.ReturnVarName;
@class = @class.OriginalClass ?? @class;
Type returnType = Context.ReturnType.Type.Desugar();
if (@class.IsRefType &&
(Context.ReturnType.Qualifiers.IsConst || !returnType.IsAddress()) &&
(!Context.Driver.Options.GenerateAbstractImpls || !@class.IsAbstract))
{
var instanceName = Generator.GeneratedIdentifier("instance");
if (VarSuffix > 0)
instanceName += VarSuffix;
if (@class.HasNonTrivialCopyConstructor)
{
// Find a valid copy constructor overload.
var copyCtorMethod = @class.Methods.FirstOrDefault(method =>
method.IsCopyConstructor);
if (copyCtorMethod == null)
throw new NotSupportedException("Expected a valid copy constructor");
// Call the copy constructor.
TypeMap typeMap;
if (!copyCtorMethod.IsGenerated && FindTypeMap(ctx.Driver.TypeDatabase, @class, out typeMap))
{
typeMap.CSharpMarshalCopyCtorToManaged(Context);
}
else
{
// Allocate memory for a new native object and call the ctor.
Context.SupportBefore.WriteLine("var {0} = Marshal.AllocHGlobal({1});",
instanceName, @class.Layout.Size);
Context.SupportBefore.WriteLine("{0}.Internal.{1}({2}, new global::System.IntPtr(&{3}));",
QualifiedIdentifier(@class),
CSharpTextTemplate.GetFunctionNativeIdentifier(copyCtorMethod),
instanceName, instance);
}
}
else
{
// Allocate memory for a new native object and call the ctor.
Context.SupportBefore.WriteLine("var {0} = Marshal.AllocHGlobal({1});",
instanceName, @class.Layout.Size);
instance = instance.Trim('*');
Context.SupportBefore.WriteLine(
"CppSharp.Runtime.Helpers.memcpy({0}, new IntPtr(&{1}), new UIntPtr({2}));",
instanceName, instance, @class.Layout.Size);
}
instance = instanceName;
}
var type = QualifiedIdentifier(@class) +
(Context.Driver.Options.GenerateAbstractImpls && @class.IsAbstract ?

67
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -613,7 +613,7 @@ namespace CppSharp.Generators.CSharp @@ -613,7 +613,7 @@ namespace CppSharp.Generators.CSharp
ReturnType = property.QualifiedType
};
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx) { VarSuffix = i };
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx);
property.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
@ -1513,7 +1513,7 @@ namespace CppSharp.Generators.CSharp @@ -1513,7 +1513,7 @@ namespace CppSharp.Generators.CSharp
ReturnVarName = param.Name
};
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx) { VarSuffix = i };
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx);
param.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
@ -1889,12 +1889,7 @@ namespace CppSharp.Generators.CSharp @@ -1889,12 +1889,7 @@ namespace CppSharp.Generators.CSharp
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
PushBlock(CSharpBlockKind.Method);
WriteLine("internal {0}({1}.Internal native)", safeIdentifier, className);
WriteLineIndent(": this(&native)");
WriteStartBraceIndent();
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
GenerateNativeConstructorByValue(@class, className, safeIdentifier);
PushBlock(CSharpBlockKind.Method);
WriteLine("public {0}(global::System.IntPtr native, bool isInternalImpl = false){1}",
@ -1945,6 +1940,62 @@ namespace CppSharp.Generators.CSharp @@ -1945,6 +1940,62 @@ namespace CppSharp.Generators.CSharp
}
}
private void GenerateNativeConstructorByValue(Class @class, string className, string safeIdentifier)
{
if (@class.IsRefType)
{
PushBlock(CSharpBlockKind.Method);
WriteLine("private static global::System.IntPtr __CopyValue({0}.Internal native)", className);
WriteStartBraceIndent();
if (@class.HasNonTrivialCopyConstructor)
{
// Find a valid copy constructor overload.
var copyCtorMethod = @class.Methods.FirstOrDefault(method =>
method.IsCopyConstructor);
if (copyCtorMethod == null)
throw new NotSupportedException("Expected a valid copy constructor");
// Call the copy constructor.
TypeMap typeMap;
if (!copyCtorMethod.IsGenerated && Driver.TypeDatabase.FindTypeMap(@class, out typeMap))
{
var context = new CSharpMarshalContext(Driver)
{
ArgName = "native",
ReturnVarName = "ret",
ReturnType = new QualifiedType(new TagType(@class))
};
typeMap.CSharpMarshalCopyCtorToManaged(context);
WriteLine(context.SupportBefore);
}
else
{
// Allocate memory for a new native object and call the ctor.
WriteLine("var ret = Marshal.AllocHGlobal({0});", @class.Layout.Size);
WriteLine("{0}.Internal.{1}(ret, new global::System.IntPtr(&native));",
QualifiedIdentifier(@class), GetFunctionNativeIdentifier(copyCtorMethod));
WriteLine("return ret;");
}
}
else
{
WriteLine("global::System.IntPtr ret = Marshal.AllocHGlobal({0});", @class.Layout.Size);
WriteLine("CppSharp.Runtime.Helpers.memcpy(ret, new IntPtr(&native), new UIntPtr({0}));",
@class.Layout.Size);
WriteLine("return ret;");
}
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
}
PushBlock(CSharpBlockKind.Method);
WriteLine("internal {0}({1}.Internal native)", safeIdentifier, className);
WriteLineIndent(@class.IsRefType ? ": this(__CopyValue(native))" : ": this(&native)", className);
WriteStartBraceIndent();
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
}
private bool GenerateClassConstructorBase(Class @class, Method method)
{
var hasBase = @class.HasBaseClass;

Loading…
Cancel
Save