Skip to content

Commit 9ef1967

Browse files
committed
Changed the generated C# for const references to primitives as just primitives.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 634892e commit 9ef1967

File tree

6 files changed

+32
-14
lines changed

6 files changed

+32
-14
lines changed

src/AST/TypeExtensions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,14 @@ public static bool ResolvesTo(this QualifiedType type, QualifiedType other)
346346
return left.Equals(right);
347347
}
348348

349-
public static bool IsConst(this QualifiedType type)
349+
public static bool IsConstRefToPrimitive(this QualifiedType type)
350+
{
351+
Type desugared = type.Type.Desugar();
352+
return desugared.IsReference() &&
353+
desugared.GetFinalPointee().Desugar().IsPrimitiveType() && type.IsConst();
354+
}
355+
356+
private static bool IsConst(this QualifiedType type)
350357
{
351358
return type.Type != null && (type.Qualifiers.IsConst ||
352359
type.Type.GetQualifiedPointee().IsConst());

src/Generator/Generators/CSharp/CSharpMarshal.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,16 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
186186
if (Context.Function != null &&
187187
Context.Function.OperatorKind == CXXOperatorKind.Subscript)
188188
{
189-
if (type.IsPrimitiveType(primitive))
189+
if (type.IsPrimitiveType(primitive) ||
190+
new QualifiedType(pointer, quals).IsConstRefToPrimitive())
190191
{
191192
Context.Return.Write("*");
192193
}
193194
else
194195
{
195196
var templateParameter = type as TemplateParameterType;
196197
if (templateParameter != null)
197-
Context.Return.Write($@"({templateParameter.Parameter.Name}) (object) *");
198+
Context.Return.Write($"({templateParameter.Parameter.Name}) (object) *");
198199
}
199200
}
200201

@@ -557,6 +558,8 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
557558
if (!VisitType(pointer, quals))
558559
return false;
559560

561+
var qualifiedPointer = new QualifiedType(pointer, quals);
562+
560563
var templateSubstitution = pointer.Pointee as TemplateParameterSubstitutionType;
561564
PointerType realPointer = null;
562565
if (templateSubstitution != null)
@@ -585,7 +588,8 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
585588
}
586589
if (Context.Function.OperatorKind != CXXOperatorKind.Subscript)
587590
{
588-
if (Context.Parameter.Kind == ParameterKind.PropertyValue)
591+
if (Context.Parameter.Kind == ParameterKind.PropertyValue ||
592+
qualifiedPointer.IsConstRefToPrimitive())
589593
{
590594
Context.Return.Write($"&{Context.Parameter.Name}");
591595
}
@@ -685,9 +689,15 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
685689
if (marshalAsString && (Context.MarshalKind == MarshalKind.NativeField ||
686690
Context.MarshalKind == MarshalKind.VTableReturnValue ||
687691
Context.MarshalKind == MarshalKind.Variable))
692+
{
688693
Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name));
694+
}
689695
else
696+
{
697+
if (qualifiedPointer.IsConstRefToPrimitive())
698+
Context.Return.Write("&");
690699
Context.Return.Write(Context.Parameter.Name);
700+
}
691701
}
692702

693703
return true;

src/Generator/Generators/CSharp/CSharpTypePrinter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ public override TypePrinterResult VisitPointerType(PointerType pointer,
206206

207207
var pointee = pointer.Pointee.Desugar();
208208

209+
if (isManagedContext &&
210+
new QualifiedType(pointer, quals).IsConstRefToPrimitive())
211+
return pointee.Visit(this);
212+
209213
// From http://msdn.microsoft.com/en-us/library/y31yhkeb.aspx
210214
// Any of the following types may be a pointer type:
211215
// * sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double,

src/Generator/Passes/CheckDuplicatedNamesPass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public DeclarationName()
1919
public bool UpdateName(Declaration decl)
2020
{
2121
var function = decl as Function;
22-
if (function != null)
22+
if (function != null && !(function.Namespace is ClassTemplateSpecialization))
2323
{
2424
return UpdateName(function);
2525
}

src/Generator/Passes/MarshalPrimitivePointersAsRefTypePass.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public override bool VisitFunctionDecl(Function function)
1515
return false;
1616

1717
foreach (var param in function.Parameters.Where(
18-
p => !p.IsOut && p.Type.Desugar().IsPrimitiveTypeConvertibleToRef()))
18+
p => !p.IsOut && !p.QualifiedType.IsConstRefToPrimitive() &&
19+
p.Type.Desugar().IsPrimitiveTypeConvertibleToRef()))
1920
param.Usage = ParameterUsage.InOut;
2021

2122
return true;

tests/CSharp/CSharp.Tests.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,7 @@ public void TestStackOverflowOnVirtualCall()
681681
[Test]
682682
public void TestTemplateWithPointerToTypeParameter()
683683
{
684-
int staticT = 5;
685-
Assert.That(IndependentFieldsExtensions.StaticDependent(ref staticT), Is.EqualTo(5));
684+
Assert.That(IndependentFieldsExtensions.StaticDependent(5), Is.EqualTo(5));
686685
}
687686

688687
[Test]
@@ -702,14 +701,12 @@ public void TestTemplateWithIndependentFields()
702701
{
703702
using (var independentFields = new IndependentFields<int>())
704703
{
705-
var t = 5;
706-
Assert.That(independentFields.GetDependent(ref t), Is.EqualTo(5));
704+
Assert.That(independentFields.GetDependent(5), Is.EqualTo(5));
707705
Assert.That(independentFields.Independent, Is.EqualTo(1));
708706
}
709707
using (var independentFields = new IndependentFields<bool>())
710708
{
711-
var t = true;
712-
Assert.That(independentFields.GetDependent(ref t), Is.EqualTo(true));
709+
Assert.That(independentFields.GetDependent(true), Is.EqualTo(true));
713710
Assert.That(independentFields.Independent, Is.EqualTo(1));
714711
}
715712
}
@@ -1113,8 +1110,7 @@ public void TestConstRefIndexer()
11131110
{
11141111
using (var indexproperty = new TestIndexedProperties())
11151112
{
1116-
int a = 2;
1117-
Assert.That(indexproperty[&a], Is.EqualTo(2));
1113+
Assert.That(indexproperty[2], Is.EqualTo(2));
11181114
}
11191115
}
11201116

0 commit comments

Comments
 (0)