From 9ef19679555f9ba8a879c6f3b0bacdd1d9cfe5e9 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Sun, 22 Apr 2018 14:42:26 +0300 Subject: [PATCH] Changed the generated C# for const references to primitives as just primitives. Signed-off-by: Dimitar Dobrev --- src/AST/TypeExtensions.cs | 9 ++++++++- src/Generator/Generators/CSharp/CSharpMarshal.cs | 16 +++++++++++++--- .../Generators/CSharp/CSharpTypePrinter.cs | 4 ++++ src/Generator/Passes/CheckDuplicatedNamesPass.cs | 2 +- .../MarshalPrimitivePointersAsRefTypePass.cs | 3 ++- tests/CSharp/CSharp.Tests.cs | 12 ++++-------- 6 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/AST/TypeExtensions.cs b/src/AST/TypeExtensions.cs index c5e0a678..3db0bbf1 100644 --- a/src/AST/TypeExtensions.cs +++ b/src/AST/TypeExtensions.cs @@ -346,7 +346,14 @@ return left.Equals(right); } - public static bool IsConst(this QualifiedType type) + public static bool IsConstRefToPrimitive(this QualifiedType type) + { + Type desugared = type.Type.Desugar(); + return desugared.IsReference() && + desugared.GetFinalPointee().Desugar().IsPrimitiveType() && type.IsConst(); + } + + private static bool IsConst(this QualifiedType type) { return type.Type != null && (type.Qualifiers.IsConst || type.Type.GetQualifiedPointee().IsConst()); diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index ac837807..2906d071 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -186,7 +186,8 @@ namespace CppSharp.Generators.CSharp if (Context.Function != null && Context.Function.OperatorKind == CXXOperatorKind.Subscript) { - if (type.IsPrimitiveType(primitive)) + if (type.IsPrimitiveType(primitive) || + new QualifiedType(pointer, quals).IsConstRefToPrimitive()) { Context.Return.Write("*"); } @@ -194,7 +195,7 @@ namespace CppSharp.Generators.CSharp { var templateParameter = type as TemplateParameterType; if (templateParameter != null) - Context.Return.Write($@"({templateParameter.Parameter.Name}) (object) *"); + Context.Return.Write($"({templateParameter.Parameter.Name}) (object) *"); } } @@ -557,6 +558,8 @@ namespace CppSharp.Generators.CSharp if (!VisitType(pointer, quals)) return false; + var qualifiedPointer = new QualifiedType(pointer, quals); + var templateSubstitution = pointer.Pointee as TemplateParameterSubstitutionType; PointerType realPointer = null; if (templateSubstitution != null) @@ -585,7 +588,8 @@ namespace CppSharp.Generators.CSharp } if (Context.Function.OperatorKind != CXXOperatorKind.Subscript) { - if (Context.Parameter.Kind == ParameterKind.PropertyValue) + if (Context.Parameter.Kind == ParameterKind.PropertyValue || + qualifiedPointer.IsConstRefToPrimitive()) { Context.Return.Write($"&{Context.Parameter.Name}"); } @@ -685,9 +689,15 @@ namespace CppSharp.Generators.CSharp if (marshalAsString && (Context.MarshalKind == MarshalKind.NativeField || Context.MarshalKind == MarshalKind.VTableReturnValue || Context.MarshalKind == MarshalKind.Variable)) + { Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name)); + } else + { + if (qualifiedPointer.IsConstRefToPrimitive()) + Context.Return.Write("&"); Context.Return.Write(Context.Parameter.Name); + } } return true; diff --git a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs index f570976c..3173d46e 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -206,6 +206,10 @@ namespace CppSharp.Generators.CSharp var pointee = pointer.Pointee.Desugar(); + if (isManagedContext && + new QualifiedType(pointer, quals).IsConstRefToPrimitive()) + return pointee.Visit(this); + // From http://msdn.microsoft.com/en-us/library/y31yhkeb.aspx // Any of the following types may be a pointer type: // * sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, diff --git a/src/Generator/Passes/CheckDuplicatedNamesPass.cs b/src/Generator/Passes/CheckDuplicatedNamesPass.cs index 5264ef09..39576fcb 100644 --- a/src/Generator/Passes/CheckDuplicatedNamesPass.cs +++ b/src/Generator/Passes/CheckDuplicatedNamesPass.cs @@ -19,7 +19,7 @@ namespace CppSharp.Passes public bool UpdateName(Declaration decl) { var function = decl as Function; - if (function != null) + if (function != null && !(function.Namespace is ClassTemplateSpecialization)) { return UpdateName(function); } diff --git a/src/Generator/Passes/MarshalPrimitivePointersAsRefTypePass.cs b/src/Generator/Passes/MarshalPrimitivePointersAsRefTypePass.cs index 27f45a46..0026f6a6 100644 --- a/src/Generator/Passes/MarshalPrimitivePointersAsRefTypePass.cs +++ b/src/Generator/Passes/MarshalPrimitivePointersAsRefTypePass.cs @@ -15,7 +15,8 @@ namespace CppSharp.Passes return false; foreach (var param in function.Parameters.Where( - p => !p.IsOut && p.Type.Desugar().IsPrimitiveTypeConvertibleToRef())) + p => !p.IsOut && !p.QualifiedType.IsConstRefToPrimitive() && + p.Type.Desugar().IsPrimitiveTypeConvertibleToRef())) param.Usage = ParameterUsage.InOut; return true; diff --git a/tests/CSharp/CSharp.Tests.cs b/tests/CSharp/CSharp.Tests.cs index 96191cdc..9288bc31 100644 --- a/tests/CSharp/CSharp.Tests.cs +++ b/tests/CSharp/CSharp.Tests.cs @@ -681,8 +681,7 @@ public unsafe class CSharpTests : GeneratorTestFixture [Test] public void TestTemplateWithPointerToTypeParameter() { - int staticT = 5; - Assert.That(IndependentFieldsExtensions.StaticDependent(ref staticT), Is.EqualTo(5)); + Assert.That(IndependentFieldsExtensions.StaticDependent(5), Is.EqualTo(5)); } [Test] @@ -702,14 +701,12 @@ public unsafe class CSharpTests : GeneratorTestFixture { using (var independentFields = new IndependentFields()) { - var t = 5; - Assert.That(independentFields.GetDependent(ref t), Is.EqualTo(5)); + Assert.That(independentFields.GetDependent(5), Is.EqualTo(5)); Assert.That(independentFields.Independent, Is.EqualTo(1)); } using (var independentFields = new IndependentFields()) { - var t = true; - Assert.That(independentFields.GetDependent(ref t), Is.EqualTo(true)); + Assert.That(independentFields.GetDependent(true), Is.EqualTo(true)); Assert.That(independentFields.Independent, Is.EqualTo(1)); } } @@ -1113,8 +1110,7 @@ public unsafe class CSharpTests : GeneratorTestFixture { using (var indexproperty = new TestIndexedProperties()) { - int a = 2; - Assert.That(indexproperty[&a], Is.EqualTo(2)); + Assert.That(indexproperty[2], Is.EqualTo(2)); } }