Browse Source

Changed the generated C# for const references to primitives as just primitives.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1089/head
Dimitar Dobrev 7 years ago
parent
commit
9ef1967955
  1. 9
      src/AST/TypeExtensions.cs
  2. 16
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  3. 4
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  4. 2
      src/Generator/Passes/CheckDuplicatedNamesPass.cs
  5. 3
      src/Generator/Passes/MarshalPrimitivePointersAsRefTypePass.cs
  6. 12
      tests/CSharp/CSharp.Tests.cs

9
src/AST/TypeExtensions.cs

@ -346,7 +346,14 @@ @@ -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());

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

@ -186,7 +186,8 @@ namespace CppSharp.Generators.CSharp @@ -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 @@ -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 @@ -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 @@ -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 @@ -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;

4
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -206,6 +206,10 @@ namespace CppSharp.Generators.CSharp @@ -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,

2
src/Generator/Passes/CheckDuplicatedNamesPass.cs

@ -19,7 +19,7 @@ namespace CppSharp.Passes @@ -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);
}

3
src/Generator/Passes/MarshalPrimitivePointersAsRefTypePass.cs

@ -15,7 +15,8 @@ namespace CppSharp.Passes @@ -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;

12
tests/CSharp/CSharp.Tests.cs

@ -681,8 +681,7 @@ public unsafe class CSharpTests : GeneratorTestFixture @@ -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 @@ -702,14 +701,12 @@ public unsafe class CSharpTests : GeneratorTestFixture
{
using (var independentFields = new IndependentFields<int>())
{
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<bool>())
{
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 @@ -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));
}
}

Loading…
Cancel
Save