Skip to content

Commit 3346be0

Browse files
committed
Resolved ambiguity between char-like types in the generated C#.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent eec0504 commit 3346be0

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/Generator/Passes/CheckDuplicatedNamesPass.cs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
using System.Linq;
44
using CppSharp.AST;
55
using CppSharp.AST.Extensions;
6+
using CppSharp.Generators;
7+
using CppSharp.Generators.CLI;
8+
using CppSharp.Generators.CSharp;
69

710
namespace CppSharp.Passes
811
{
@@ -100,32 +103,35 @@ public static string FixSignatureForConversions(Function function, string signat
100103
return signature;
101104
}
102105

103-
private class ParameterTypeComparer : IEqualityComparer<Parameter>
106+
public class ParameterTypeComparer : IEqualityComparer<Parameter>
104107
{
105108
public static readonly ParameterTypeComparer Instance = new ParameterTypeComparer();
106109

107-
private ParameterTypeComparer()
108-
{
109-
}
110-
111110
public bool Equals(Parameter x, Parameter y)
112111
{
113112
Type left = x.Type.Desugar(resolveTemplateSubstitution: false);
114113
Type right = y.Type.Desugar(resolveTemplateSubstitution: false);
115114
if (left.Equals(right))
116115
return true;
117116

118-
// TODO: some target languages might maek a difference between values and pointers
117+
// TODO: some target languages might make a difference between values and pointers
119118
Type leftPointee = left.GetPointee();
120119
Type rightPointee = right.GetPointee();
121-
return (leftPointee != null && leftPointee.Desugar(false).Equals(right)) ||
122-
(rightPointee != null && rightPointee.Desugar(false).Equals(left));
120+
if ((leftPointee != null && leftPointee.Desugar(false).Equals(right)) ||
121+
(rightPointee != null && rightPointee.Desugar(false).Equals(left)))
122+
return true;
123+
124+
return TypePrinter != null &&
125+
left.IsPrimitiveType() && right.IsPrimitiveType() &&
126+
left.Visit(TypePrinter).Type == right.Visit(TypePrinter).Type;
123127
}
124128

125129
public int GetHashCode(Parameter obj)
126130
{
127131
return obj.Type.GetHashCode();
128132
}
133+
134+
public static TypePrinter TypePrinter { get; set; }
129135
}
130136
}
131137

@@ -139,6 +145,22 @@ public CheckDuplicatedNamesPass()
139145
names = new Dictionary<string, DeclarationName>();
140146
}
141147

148+
public override bool VisitASTContext(ASTContext context)
149+
{
150+
TypePrinter typePrinter = null;
151+
switch (Options.GeneratorKind)
152+
{
153+
case GeneratorKind.CLI:
154+
typePrinter = new CLITypePrinter(Context);
155+
break;
156+
case GeneratorKind.CSharp:
157+
typePrinter = new CSharpTypePrinter(Context);
158+
break;
159+
}
160+
DeclarationName.ParameterTypeComparer.TypePrinter = typePrinter;
161+
return base.VisitASTContext(context);
162+
}
163+
142164
public override bool VisitProperty(Property decl)
143165
{
144166
if (!VisitDeclaration(decl))

tests/CSharp/CSharp.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ Foo::Foo(int a, int p) : publicFieldMappedToEnum(TestFlag::Flag2)
1414
P = p;
1515
}
1616

17+
Foo::Foo(char16_t ch)
18+
{
19+
}
20+
21+
Foo::Foo(wchar_t ch)
22+
{
23+
}
24+
1725
int Foo::method()
1826
{
1927
return 1;

tests/CSharp/CSharp.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class DLL_API Foo
1313
public:
1414
Foo(const char* name = 0);
1515
Foo(int a, int p = 0);
16+
Foo(char16_t ch);
17+
Foo(wchar_t ch);
1618
int method();
1719
int operator[](int i) const;
1820
int operator[](unsigned int i);

0 commit comments

Comments
 (0)