Skip to content

Commit 8feac37

Browse files
committed
Fixed ambiguity when the type of a parameter is mapped to a type in an overload.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 8c394af commit 8feac37

File tree

5 files changed

+98
-42
lines changed

5 files changed

+98
-42
lines changed

src/Generator/Passes/CheckAmbiguousFunctions.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using CppSharp.AST;
44
using CppSharp.AST.Extensions;
5+
using CppSharp.Types;
56

67
namespace CppSharp.Passes
78
{
@@ -65,17 +66,17 @@ public override bool VisitFunctionDecl(AST.Function function)
6566
return true;
6667
}
6768

68-
private static bool CheckDefaultParametersForAmbiguity(Function function, Function overload)
69+
private bool CheckDefaultParametersForAmbiguity(Function function, Function overload)
6970
{
7071
var commonParameters = Math.Min(function.Parameters.Count, overload.Parameters.Count);
7172

7273
var i = 0;
7374
for (; i < commonParameters; ++i)
7475
{
75-
var funcParam = function.Parameters[i];
76-
var overloadParam = overload.Parameters[i];
76+
AST.Type funcType = GetFinalType(function.Parameters[i]);
77+
AST.Type overloadType = GetFinalType(overload.Parameters[i]);
7778

78-
if (!funcParam.QualifiedType.Equals(overloadParam.QualifiedType))
79+
if (!funcType.Equals(overloadType))
7980
return false;
8081
}
8182

@@ -101,6 +102,30 @@ private static bool CheckDefaultParametersForAmbiguity(Function function, Functi
101102
return true;
102103
}
103104

105+
private AST.Type GetFinalType(Parameter parameter)
106+
{
107+
TypeMap typeMap;
108+
if (Context.TypeMaps.FindTypeMap(parameter.Type, out typeMap))
109+
{
110+
var typePrinterContext = new TypePrinterContext
111+
{
112+
Kind = TypePrinterContextKind.Managed,
113+
Parameter = parameter,
114+
Type = typeMap.Type
115+
};
116+
117+
switch (Options.GeneratorKind)
118+
{
119+
case Generators.GeneratorKind.CLI:
120+
return typeMap.CLISignatureType(typePrinterContext).Desugar();
121+
case Generators.GeneratorKind.CSharp:
122+
return typeMap.CSharpSignatureType(typePrinterContext).Desugar();
123+
}
124+
}
125+
126+
return parameter.Type.Desugar();
127+
}
128+
104129
private static bool CheckConstnessForAmbiguity(Function function, Function overload)
105130
{
106131
var method1 = function as Method;

tests/CSharp/CSharp.Tests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public void TestUncompilableCode()
3535
new InheritanceBuffer().Dispose();
3636
new HasProtectedVirtual().Dispose();
3737
new Proprietor(5).Dispose();
38+
new HasCtorWithMappedToEnum<TestFlag>(TestFlag.Flag1).Dispose();
3839
using (var testOverrideFromSecondaryBase = new TestOverrideFromSecondaryBase())
3940
{
4041
testOverrideFromSecondaryBase.function();

tests/CSharp/CSharp.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#include "CSharp.h"
1+
#pragma once
2+
3+
#include "CSharp.h"
24

35
Foo::Foo(const char* name)
46
{
@@ -322,9 +324,12 @@ ComplexType::ComplexType() : qFlags(QFlags<TestFlag>(TestFlag::Flag2))
322324
{
323325
}
324326

325-
ComplexType::ComplexType(const QFlags<TestFlag> f) : qFlags(QFlags<TestFlag>(TestFlag::Flag2))
327+
ComplexType::ComplexType(const QFlags<TestFlag> f) : qFlags(f)
328+
{
329+
}
330+
331+
ComplexType::ComplexType(const HasCtorWithMappedToEnum<TestFlag> f)
326332
{
327-
qFlags = f;
328333
}
329334

330335
int ComplexType::check()

tests/CSharp/CSharp.h

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
#pragma once
2+
13
#include "../Tests.h"
24
#include <cstdint>
35
#include <vector>
46
#include <limits>
57
#include <string>
68
#include "AnotherUnit.h"
9+
#include "CSharpTemplates.h"
710

811
class DLL_API Foo
912
{
@@ -184,46 +187,12 @@ class DLL_API Proprietor : public AbstractProprietor
184187

185188
Proprietor::Proprietor() : _items(Bar::Items::Item1), _itemsByValue(Bar::Items::Item1) {}
186189

187-
template <typename T>
188-
class DLL_API QFlags
189-
{
190-
typedef int Int;
191-
typedef int (*Zero);
192-
public:
193-
QFlags(T t);
194-
QFlags(Zero = 0);
195-
operator Int();
196-
private:
197-
int flag;
198-
};
199-
200-
template <typename T>
201-
QFlags<T>::QFlags(T t) : flag(Int(t))
202-
{
203-
}
204-
205-
template <typename T>
206-
QFlags<T>::QFlags(Zero) : flag(Int(0))
207-
{
208-
}
209-
210-
template <typename T>
211-
QFlags<T>::operator Int()
212-
{
213-
return flag;
214-
}
215-
216-
enum class TestFlag
217-
{
218-
Flag1,
219-
Flag2
220-
};
221-
222190
class DLL_API ComplexType
223191
{
224192
public:
225193
ComplexType();
226194
ComplexType(const QFlags<TestFlag> f);
195+
ComplexType(const HasCtorWithMappedToEnum<TestFlag> f);
227196
int check();
228197
QFlags<TestFlag> returnsQFlags();
229198
void takesQFlags(const QFlags<int> f);

tests/CSharp/CSharpTemplates.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include "../Tests.h"
24
#include "AnotherUnit.h"
35

@@ -606,6 +608,59 @@ enum class UsedInTemplatedIndexer
606608
Item2
607609
};
608610

611+
template <typename T>
612+
class DLL_API QFlags
613+
{
614+
typedef int Int;
615+
typedef int (*Zero);
616+
public:
617+
QFlags(T t);
618+
QFlags(Zero = 0);
619+
operator Int();
620+
private:
621+
int flag;
622+
};
623+
624+
template <typename T>
625+
QFlags<T>::QFlags(T t) : flag(Int(t))
626+
{
627+
}
628+
629+
template <typename T>
630+
QFlags<T>::QFlags(Zero) : flag(Int(0))
631+
{
632+
}
633+
634+
template <typename T>
635+
QFlags<T>::operator Int()
636+
{
637+
return flag;
638+
}
639+
640+
template <typename T>
641+
class HasCtorWithMappedToEnum
642+
{
643+
public:
644+
HasCtorWithMappedToEnum(QFlags<T> t);
645+
HasCtorWithMappedToEnum(T t);
646+
};
647+
648+
template <typename T>
649+
HasCtorWithMappedToEnum<T>::HasCtorWithMappedToEnum(QFlags<T> t)
650+
{
651+
}
652+
653+
template <typename T>
654+
HasCtorWithMappedToEnum<T>::HasCtorWithMappedToEnum(T t)
655+
{
656+
}
657+
658+
enum class TestFlag
659+
{
660+
Flag1,
661+
Flag2
662+
};
663+
609664
// we optimise specialisations so that only actually used ones are wrapped
610665
void forceUseSpecializations(IndependentFields<int> _1, IndependentFields<bool> _2,
611666
IndependentFields<T1> _3, IndependentFields<std::string> _4,
@@ -643,6 +698,7 @@ template class DLL_API TemplateWithIndexer<T1>;
643698
template class DLL_API TemplateWithIndexer<T2*>;
644699
template class DLL_API TemplateWithIndexer<float>;
645700
template class DLL_API TemplateDerivedFromRegularDynamic<RegularDynamic>;
701+
template class DLL_API HasCtorWithMappedToEnum<TestFlag>;
646702

647703
class TestForwardedClassInAnotherUnit;
648704

0 commit comments

Comments
 (0)