Skip to content

Commit 6e78b4d

Browse files
ddobrevtritao
authored andcommitted
Fix the generated C# when type arguments are mapped the same
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 45b05d7 commit 6e78b4d

File tree

7 files changed

+99
-33
lines changed

7 files changed

+99
-33
lines changed

src/Generator/Generators/ExtensionMethods.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using CppSharp.AST;
44
using CppSharp.AST.Extensions;
5+
using CppSharp.Types;
56
using Interop = System.Runtime.InteropServices;
67

78
namespace CppSharp.Generators
@@ -46,5 +47,29 @@ public static bool IsPrimitiveTypeConvertibleToRef(this Type type)
4647
return type.IsPointerToPrimitiveType() &&
4748
allowedToHaveDefaultPtrVals.Any(type.IsPointerToPrimitiveType);
4849
}
50+
51+
public static Type GetMappedType(this Type type, TypeMapDatabase typeMaps,
52+
GeneratorKind generatorKind)
53+
{
54+
TypeMap typeMap;
55+
if (typeMaps.FindTypeMap(type, out typeMap))
56+
{
57+
var typePrinterContext = new TypePrinterContext
58+
{
59+
Kind = TypePrinterContextKind.Managed,
60+
Type = typeMap.Type
61+
};
62+
63+
switch (generatorKind)
64+
{
65+
case GeneratorKind.CLI:
66+
return typeMap.CLISignatureType(typePrinterContext).Desugar();
67+
case GeneratorKind.CSharp:
68+
return typeMap.CSharpSignatureType(typePrinterContext).Desugar();
69+
}
70+
}
71+
72+
return type.Desugar();
73+
}
4974
}
5075
}

src/Generator/Passes/CheckAmbiguousFunctions.cs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ private bool CheckDefaultParametersForAmbiguity(Function function, Function over
7878
var i = 0;
7979
for (; i < commonParameters; ++i)
8080
{
81-
AST.Type funcType = GetFinalType(functionParams[i]);
82-
AST.Type overloadType = GetFinalType(overloadParams[i]);
81+
AST.Type funcType = functionParams[i].Type.GetMappedType(
82+
TypeMaps, Options.GeneratorKind);
83+
AST.Type overloadType = overloadParams[i].Type.GetMappedType(
84+
TypeMaps, Options.GeneratorKind);
8385

8486
AST.Type funcPointee = funcType.GetFinalPointee() ?? funcType;
8587
AST.Type overloadPointee = overloadType.GetFinalPointee() ?? overloadType;
@@ -137,30 +139,6 @@ private List<Parameter> RemoveOperatorParams(Function function)
137139
return functionParams;
138140
}
139141

140-
private AST.Type GetFinalType(Parameter parameter)
141-
{
142-
TypeMap typeMap;
143-
if (Context.TypeMaps.FindTypeMap(parameter.Type, out typeMap))
144-
{
145-
var typePrinterContext = new TypePrinterContext
146-
{
147-
Kind = TypePrinterContextKind.Managed,
148-
Parameter = parameter,
149-
Type = typeMap.Type
150-
};
151-
152-
switch (Options.GeneratorKind)
153-
{
154-
case Generators.GeneratorKind.CLI:
155-
return typeMap.CLISignatureType(typePrinterContext).Desugar();
156-
case Generators.GeneratorKind.CSharp:
157-
return typeMap.CSharpSignatureType(typePrinterContext).Desugar();
158-
}
159-
}
160-
161-
return parameter.Type.Desugar();
162-
}
163-
164142
private static bool CheckConstnessForAmbiguity(Function function, Function overload)
165143
{
166144
var method1 = function as Method;

src/Generator/Passes/CheckDuplicatedNamesPass.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using CppSharp.Generators;
77
using CppSharp.Generators.CLI;
88
using CppSharp.Generators.CSharp;
9+
using CppSharp.Types;
910

1011
namespace CppSharp.Passes
1112
{
@@ -95,7 +96,11 @@ public static string FixSignatureForConversions(Function function, string signat
9596

9697
public class ParameterTypeComparer : IEqualityComparer<Parameter>
9798
{
98-
public static readonly ParameterTypeComparer Instance = new ParameterTypeComparer();
99+
public static readonly ParameterTypeComparer Instance =
100+
new ParameterTypeComparer();
101+
102+
public static TypeMapDatabase TypeMaps { get; set; }
103+
public static GeneratorKind GeneratorKind { get; set; }
99104

100105
public bool Equals(Parameter x, Parameter y)
101106
{
@@ -107,17 +112,60 @@ public bool Equals(Parameter x, Parameter y)
107112
// TODO: some target languages might make a difference between values and pointers
108113
Type leftPointee = left.GetPointee();
109114
Type rightPointee = right.GetPointee();
115+
116+
if (CheckForSpecializations(leftPointee, rightPointee))
117+
return true;
118+
119+
if (leftPointee != null && rightPointee != null &&
120+
leftPointee.GetMappedType(TypeMaps, GeneratorKind).Equals(
121+
rightPointee.GetMappedType(TypeMaps, GeneratorKind)))
122+
return true;
123+
110124
return (leftPointee != null && leftPointee.Desugar(false).Equals(right)) ||
111125
(rightPointee != null && rightPointee.Desugar(false).Equals(left));
112126
}
113127

128+
private static bool CheckForSpecializations(Type leftPointee, Type rightPointee)
129+
{
130+
ClassTemplateSpecialization leftSpecialization;
131+
ClassTemplateSpecialization rightSpecialization;
132+
return leftPointee.TryGetDeclaration(out leftSpecialization) &&
133+
rightPointee.TryGetDeclaration(out rightSpecialization) &&
134+
leftSpecialization.TemplatedDecl.TemplatedDecl.Equals(
135+
rightSpecialization.TemplatedDecl.TemplatedDecl) &&
136+
leftSpecialization.Arguments.SequenceEqual(
137+
rightSpecialization.Arguments, TemplateArgumentComparer.Instance);
138+
}
139+
114140
public int GetHashCode(Parameter obj)
115141
{
116142
return obj.Type.GetHashCode();
117143
}
118144

119145
public static TypePrinter TypePrinter { get; set; }
120146
}
147+
148+
public class TemplateArgumentComparer : IEqualityComparer<TemplateArgument>
149+
{
150+
public static readonly TemplateArgumentComparer Instance =
151+
new TemplateArgumentComparer();
152+
153+
public bool Equals(TemplateArgument x, TemplateArgument y)
154+
{
155+
if (x.Kind != TemplateArgument.ArgumentKind.Type ||
156+
y.Kind != TemplateArgument.ArgumentKind.Type)
157+
return x.Equals(y);
158+
return x.Type.Type.GetMappedType(ParameterTypeComparer.TypeMaps,
159+
ParameterTypeComparer.GeneratorKind).Equals(
160+
y.Type.Type.GetMappedType(ParameterTypeComparer.TypeMaps,
161+
ParameterTypeComparer.GeneratorKind));
162+
}
163+
164+
public int GetHashCode(TemplateArgument obj)
165+
{
166+
return obj.GetHashCode();
167+
}
168+
}
121169
}
122170

123171
public class CheckDuplicatedNamesPass : TranslationUnitPass
@@ -143,6 +191,8 @@ public override bool VisitASTContext(ASTContext context)
143191
break;
144192
}
145193
DeclarationName.ParameterTypeComparer.TypePrinter = typePrinter;
194+
DeclarationName.ParameterTypeComparer.TypeMaps = Context.TypeMaps;
195+
DeclarationName.ParameterTypeComparer.GeneratorKind = Options.GeneratorKind;
146196
return base.VisitASTContext(context);
147197
}
148198

tests/CSharp/CSharp.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,12 @@ public class QString : TypeMap
248248
{
249249
public override Type CSharpSignatureType(TypePrinterContext ctx)
250250
{
251+
if (ctx.Kind == TypePrinterContextKind.Native)
252+
{
253+
return new CustomType($@"global::CSharp.QString.{
254+
Helpers.InternalStruct}{
255+
(ctx.Type.IsAddress() ? "*" : string.Empty)}");
256+
}
251257
return new CILType(typeof(string));
252258
}
253259

tests/CSharp/CSharp.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
#include "AnotherUnit.h"
99
#include "CSharpTemplates.h"
1010

11-
class DLL_API QString
12-
{
13-
};
14-
1511
class DLL_API Foo
1612
{
1713
public:

tests/CSharp/CSharpTemplates.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ void TemplateSpecializer::completeSpecializationInParameter(DependentValueFields
121121
void TemplateSpecializer::completeSpecializationInParameter(TwoTemplateArgs<int *, int *> p1,
122122
TwoTemplateArgs<int *, int> p2,
123123
TwoTemplateArgs<int *, float> p3,
124-
TwoTemplateArgs<const char *, int> p4)
124+
TwoTemplateArgs<const char *, int> p4,
125+
TwoTemplateArgs<QString, int> p5)
125126
{
126127
}
127128

tests/CSharp/CSharpTemplates.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
#include <string>
77
#include <map>
88

9+
class DLL_API QString
10+
{
11+
};
12+
913
class DLL_API T1
1014
{
1115
public:
@@ -275,6 +279,11 @@ class TwoTemplateArgs
275279
V value;
276280
};
277281

282+
template <typename K, typename V>
283+
void TwoTemplateArgs<K, V>::takeDependentPtrToSecondTemplateArg(const V& v)
284+
{
285+
}
286+
278287
template <typename T, typename D = IndependentFields<T>>
279288
class DLL_API HasDefaultTemplateArgument
280289
{
@@ -537,7 +546,8 @@ class DLL_API TemplateSpecializer
537546
void completeSpecializationInParameter(TwoTemplateArgs<int*, int*> p1,
538547
TwoTemplateArgs<int*, int> p2,
539548
TwoTemplateArgs<int*, float> p3,
540-
TwoTemplateArgs<const char*, int> p4);
549+
TwoTemplateArgs<const char*, int> p4,
550+
TwoTemplateArgs<QString, int> p5);
541551
VirtualTemplate<void> returnSpecializedWithVoid();
542552
private:
543553
IndependentFields<int> independentFields;

0 commit comments

Comments
 (0)