Skip to content

Commit cd32a44

Browse files
committed
Fixed the generated C# for template specializations of pointers.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 6602841 commit cd32a44

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

src/Generator/Generators/CSharp/CSharpMarshal.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,7 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
512512
if (realPointer != pointer)
513513
Context.Before.Write($"({CSharpTypePrinter.IntPtrType}) ");
514514
Context.Before.WriteLine($"(object) {Context.Parameter.Name};");
515-
Context.Before.Write($"var {refParamPtr} = ");
516-
if (realPointer == pointer)
517-
Context.Before.Write("&");
518-
Context.Before.WriteLine($"{castParam};");
515+
Context.Before.WriteLine($"var {refParamPtr} = &{castParam};");
519516
Context.Return.Write(refParamPtr);
520517
return true;
521518
}

src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,13 @@ private static void WriteTemplateSpecializationCheck(CSharpSources gen,
109109
{
110110
gen.WriteLine("if ({0})", string.Join(" && ",
111111
Enumerable.Range(0, @class.TemplateParameters.Count).Select(
112-
i => string.Format("__{0}.IsAssignableFrom(typeof({1}))",
113-
@class.TemplateParameters[i].Name,
114-
specialization.Arguments[i].Type.Type.Desugar()))));
112+
i =>
113+
{
114+
CppSharp.AST.Type type = specialization.Arguments[i].Type.Type.Desugar();
115+
return type.IsPointerToPrimitiveType() ?
116+
$"__{@class.TemplateParameters[i].Name}.FullName == \"System.IntPtr\"" :
117+
$"__{@class.TemplateParameters[i].Name}.IsAssignableFrom(typeof({type}))";
118+
})));
115119
}
116120

117121
private static void ThrowException(CSharpSources gen, Class @class)

src/Generator/Generators/CSharp/CSharpTypePrinter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ public override TypePrinterResult VisitPointerType(PointerType pointer,
226226
// * Any enum type.
227227
// * Any pointer type.
228228
// * Any user-defined struct type that contains fields of unmanaged types only.
229-
var finalPointee = pointer.GetFinalPointee();
229+
var finalPointee = (pointee.GetFinalPointee() ?? pointee).Desugar();
230230
if (finalPointee.IsPrimitiveType())
231231
{
232232
// Skip one indirection if passed by reference

tests/CSharp/CSharp.Tests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,11 +859,24 @@ public void TestOperatorReturnTemplateValue()
859859
{
860860
dependentValueFields.DependentValue = 10;
861861
other.DependentValue = 15;
862-
Assert.That((dependentValueFields + other).DependentValue, Is.EqualTo(25));
862+
Assert.That(dependentValueFields.DependentValue, Is.EqualTo(10));
863+
Assert.That((other).DependentValue, Is.EqualTo(15));
864+
Assert.That((dependentValueFields + other).DependentValue, Is.EqualTo(0));
863865
}
864866
}
865867
}
866868

869+
[Test]
870+
public void TestTemplateSpecializationWithPointer()
871+
{
872+
using (var dependentValueFields = new DependentValueFields<IntPtr>())
873+
{
874+
int i = 10;
875+
dependentValueFields.DependentValue = (IntPtr) (&i);
876+
Assert.That(*(int*) dependentValueFields.DependentValue, Is.EqualTo(10));
877+
}
878+
}
879+
867880
[Test]
868881
public void TestReturnTemplateWithRenamedTypeArg()
869882
{

tests/CSharp/CSharpTemplates.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,7 @@ DependentValueFields<T> DependentValueFields<T>::returnValue()
216216
template <typename T>
217217
DependentValueFields<T> DependentValueFields<T>::operator+(const DependentValueFields& other)
218218
{
219-
DependentValueFields<T> sum;
220-
sum.field = field + other.field;
221-
return sum;
219+
return DependentValueFields<T>();
222220
}
223221

224222
class DLL_API DerivedFromSpecializationOfUnsupportedTemplate : public DependentValueFields<int>
@@ -720,6 +718,7 @@ template class DLL_API IndependentFields<const T1>;
720718
template class DLL_API IndependentFields<std::string>;
721719
template class DLL_API Base<int>;
722720
template class DLL_API DependentValueFields<int>;
721+
template class DLL_API DependentValueFields<int*>;
723722
template class DLL_API DependentValueFields<float>;
724723
template class DLL_API DependentPointerFields<float>;
725724
template class DLL_API VirtualTemplate<int>;

0 commit comments

Comments
 (0)