Browse Source

Merge pull request #133 from ddobrev/master

Fixed the wrapping of instance operators to account for fixed instances
pull/134/head
João Matos 12 years ago
parent
commit
430e99a914
  1. 59
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  2. 4
      tests/Basic/Basic.Tests.cs

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

@ -1950,15 +1950,15 @@ namespace CppSharp.Generators.CSharp @@ -1950,15 +1950,15 @@ namespace CppSharp.Generators.CSharp
var needsInstance = false;
var method = function as Method;
Parameter operatorParam = null;
if (method != null)
{
var @class = (Class) method.Namespace;
isValueType = @class.IsValueType;
needsInstance = !method.IsStatic;
if (method.IsOperator)
needsInstance &= !Operators.IsBuiltinOperator(method.OperatorKind);
operatorParam = method.Parameters.FirstOrDefault(
p => p.Kind == ParameterKind.OperatorParameter);
needsInstance = !method.IsStatic || operatorParam != null;
}
var needsFixedThis = needsInstance && isValueType;
@ -1972,7 +1972,7 @@ namespace CppSharp.Generators.CSharp @@ -1972,7 +1972,7 @@ namespace CppSharp.Generators.CSharp
if (hiddenParam.Kind != ParameterKind.IndirectReturnType)
throw new NotSupportedException("Expected hidden structure parameter kind");
Class retClass = null;
Class retClass;
hiddenParam.Type.Desugar().IsTagDecl(out retClass);
WriteLine("var {0} = new {1}.Internal();", GeneratedIdentifier("ret"),
QualifiedIdentifier(retClass.OriginalClass ?? retClass));
@ -1981,6 +1981,9 @@ namespace CppSharp.Generators.CSharp @@ -1981,6 +1981,9 @@ namespace CppSharp.Generators.CSharp
var names = new List<string>();
foreach (var param in @params)
{
if (param.Param == operatorParam && needsInstance)
continue;
var name = string.Empty;
if (param.Context != null
&& !string.IsNullOrWhiteSpace(param.Context.ArgumentPrefix))
@ -1998,16 +2001,28 @@ namespace CppSharp.Generators.CSharp @@ -1998,16 +2001,28 @@ namespace CppSharp.Generators.CSharp
if (needsInstance)
{
names.Insert(0, needsFixedThis ? string.Format("new global::System.IntPtr(&{0})",
GeneratedIdentifier("fixedInstance")) : Helpers.InstanceIdentifier);
if (needsFixedThis)
{
names.Insert(0, string.Format("new global::System.IntPtr(&{0})",
GeneratedIdentifier("fixedInstance")));
}
else
{
if (operatorParam != null)
{
names.Insert(0, operatorParam.Name + "." + Helpers.InstanceIdentifier);
}
else
{
names.Insert(0, Helpers.InstanceIdentifier);
}
}
}
if (needsFixedThis)
{
//WriteLine("fixed({0}* {1} = &this)", @class.QualifiedName,
// GeneratedIdentifier("instance"));
//WriteStartBraceIndent();
WriteLine("var {0} = ToInternal();", Generator.GeneratedIdentifier("fixedInstance"));
WriteLine("var {0} = {1};", Generator.GeneratedIdentifier("fixedInstance"),
method.IsOperator ? "__arg0" : "ToInternal()");
}
if (needsReturn && !originalFunction.HasIndirectReturnTypeParameter)
@ -2018,14 +2033,11 @@ namespace CppSharp.Generators.CSharp @@ -2018,14 +2033,11 @@ namespace CppSharp.Generators.CSharp
var cleanups = new List<TextGenerator>();
GenerateFunctionCallOutParams(@params, cleanups);
foreach (var param in @params)
{
var context = param.Context;
if (context == null) continue;
if (!string.IsNullOrWhiteSpace(context.Cleanup))
cleanups.Add(context.Cleanup);
}
cleanups.AddRange(
from param in @params
select param.Context into context
where context != null && !string.IsNullOrWhiteSpace(context.Cleanup)
select context.Cleanup);
foreach (var cleanup in cleanups)
{
@ -2034,9 +2046,16 @@ namespace CppSharp.Generators.CSharp @@ -2034,9 +2046,16 @@ namespace CppSharp.Generators.CSharp
if (needsFixedThis)
{
// WriteCloseBraceIndent();
if (operatorParam != null)
{
WriteLine("{0}.FromInternal(&{1});",
operatorParam.Name, Generator.GeneratedIdentifier("fixedInstance"));
}
else
{
WriteLine("FromInternal(&{0});", Generator.GeneratedIdentifier("fixedInstance"));
}
}
if (needsReturn)
{

4
tests/Basic/Basic.Tests.cs

@ -98,7 +98,7 @@ public class BasicTests @@ -98,7 +98,7 @@ public class BasicTests
def.Bar();
}
[Test, Ignore]
[Test]
public void TestLeftShiftOperator()
{
var foo2 = new Foo2 {C = 2};
@ -139,7 +139,7 @@ public class BasicTests @@ -139,7 +139,7 @@ public class BasicTests
Assert.AreEqual(foo.C, 3);
}
[Test, Ignore]
[Test]
public void TestConversionOperator()
{
var bar = new Bar2 {A = 1, B = 2, C = 3};

Loading…
Cancel
Save