From c5f2acacc6b4443b09f0f90e39fff953e335636b Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Sun, 8 Dec 2013 23:28:23 +0200 Subject: [PATCH] Fixed the wrapping of instance operators to account for fixed instances. Signed-off-by: Dimitar Dobrev --- .../Generators/CSharp/CSharpTextTemplate.cs | 61 ++++++++++++------- tests/Basic/Basic.Tests.cs | 4 +- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index dbd8c65d..557768f9 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -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 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 var names = new List(); 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 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 var cleanups = new List(); 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,8 +2046,15 @@ namespace CppSharp.Generators.CSharp if (needsFixedThis) { - // WriteCloseBraceIndent(); - WriteLine("FromInternal(&{0});", Generator.GeneratedIdentifier("fixedInstance")); + if (operatorParam != null) + { + WriteLine("{0}.FromInternal(&{1});", + operatorParam.Name, Generator.GeneratedIdentifier("fixedInstance")); + } + else + { + WriteLine("FromInternal(&{0});", Generator.GeneratedIdentifier("fixedInstance")); + } } if (needsReturn) diff --git a/tests/Basic/Basic.Tests.cs b/tests/Basic/Basic.Tests.cs index 81ac0426..bb683f3c 100644 --- a/tests/Basic/Basic.Tests.cs +++ b/tests/Basic/Basic.Tests.cs @@ -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 Assert.AreEqual(foo.C, 3); } - [Test, Ignore] + [Test] public void TestConversionOperator() { var bar = new Bar2 {A = 1, B = 2, C = 3};