Browse Source

Fixed ambiguity when the type of a parameter is mapped to a type in an overload.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1139/head
Dimitar Dobrev 7 years ago
parent
commit
8feac37ae1
  1. 33
      src/Generator/Passes/CheckAmbiguousFunctions.cs
  2. 1
      tests/CSharp/CSharp.Tests.cs
  3. 11
      tests/CSharp/CSharp.cpp
  4. 39
      tests/CSharp/CSharp.h
  5. 56
      tests/CSharp/CSharpTemplates.h

33
src/Generator/Passes/CheckAmbiguousFunctions.cs

@ -2,6 +2,7 @@
using System.Linq; using System.Linq;
using CppSharp.AST; using CppSharp.AST;
using CppSharp.AST.Extensions; using CppSharp.AST.Extensions;
using CppSharp.Types;
namespace CppSharp.Passes namespace CppSharp.Passes
{ {
@ -65,17 +66,17 @@ namespace CppSharp.Passes
return true; return true;
} }
private static bool CheckDefaultParametersForAmbiguity(Function function, Function overload) private bool CheckDefaultParametersForAmbiguity(Function function, Function overload)
{ {
var commonParameters = Math.Min(function.Parameters.Count, overload.Parameters.Count); var commonParameters = Math.Min(function.Parameters.Count, overload.Parameters.Count);
var i = 0; var i = 0;
for (; i < commonParameters; ++i) for (; i < commonParameters; ++i)
{ {
var funcParam = function.Parameters[i]; AST.Type funcType = GetFinalType(function.Parameters[i]);
var overloadParam = overload.Parameters[i]; AST.Type overloadType = GetFinalType(overload.Parameters[i]);
if (!funcParam.QualifiedType.Equals(overloadParam.QualifiedType)) if (!funcType.Equals(overloadType))
return false; return false;
} }
@ -101,6 +102,30 @@ namespace CppSharp.Passes
return true; return true;
} }
private AST.Type GetFinalType(Parameter parameter)
{
TypeMap typeMap;
if (Context.TypeMaps.FindTypeMap(parameter.Type, out typeMap))
{
var typePrinterContext = new TypePrinterContext
{
Kind = TypePrinterContextKind.Managed,
Parameter = parameter,
Type = typeMap.Type
};
switch (Options.GeneratorKind)
{
case Generators.GeneratorKind.CLI:
return typeMap.CLISignatureType(typePrinterContext).Desugar();
case Generators.GeneratorKind.CSharp:
return typeMap.CSharpSignatureType(typePrinterContext).Desugar();
}
}
return parameter.Type.Desugar();
}
private static bool CheckConstnessForAmbiguity(Function function, Function overload) private static bool CheckConstnessForAmbiguity(Function function, Function overload)
{ {
var method1 = function as Method; var method1 = function as Method;

1
tests/CSharp/CSharp.Tests.cs

@ -35,6 +35,7 @@ public unsafe class CSharpTests : GeneratorTestFixture
new InheritanceBuffer().Dispose(); new InheritanceBuffer().Dispose();
new HasProtectedVirtual().Dispose(); new HasProtectedVirtual().Dispose();
new Proprietor(5).Dispose(); new Proprietor(5).Dispose();
new HasCtorWithMappedToEnum<TestFlag>(TestFlag.Flag1).Dispose();
using (var testOverrideFromSecondaryBase = new TestOverrideFromSecondaryBase()) using (var testOverrideFromSecondaryBase = new TestOverrideFromSecondaryBase())
{ {
testOverrideFromSecondaryBase.function(); testOverrideFromSecondaryBase.function();

11
tests/CSharp/CSharp.cpp

@ -1,4 +1,6 @@
#include "CSharp.h" #pragma once
#include "CSharp.h"
Foo::Foo(const char* name) Foo::Foo(const char* name)
{ {
@ -322,9 +324,12 @@ ComplexType::ComplexType() : qFlags(QFlags<TestFlag>(TestFlag::Flag2))
{ {
} }
ComplexType::ComplexType(const QFlags<TestFlag> f) : qFlags(QFlags<TestFlag>(TestFlag::Flag2)) ComplexType::ComplexType(const QFlags<TestFlag> f) : qFlags(f)
{
}
ComplexType::ComplexType(const HasCtorWithMappedToEnum<TestFlag> f)
{ {
qFlags = f;
} }
int ComplexType::check() int ComplexType::check()

39
tests/CSharp/CSharp.h

@ -1,9 +1,12 @@
#pragma once
#include "../Tests.h" #include "../Tests.h"
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include <limits> #include <limits>
#include <string> #include <string>
#include "AnotherUnit.h" #include "AnotherUnit.h"
#include "CSharpTemplates.h"
class DLL_API Foo class DLL_API Foo
{ {
@ -184,46 +187,12 @@ private:
Proprietor::Proprietor() : _items(Bar::Items::Item1), _itemsByValue(Bar::Items::Item1) {} Proprietor::Proprietor() : _items(Bar::Items::Item1), _itemsByValue(Bar::Items::Item1) {}
template <typename T>
class DLL_API QFlags
{
typedef int Int;
typedef int (*Zero);
public:
QFlags(T t);
QFlags(Zero = 0);
operator Int();
private:
int flag;
};
template <typename T>
QFlags<T>::QFlags(T t) : flag(Int(t))
{
}
template <typename T>
QFlags<T>::QFlags(Zero) : flag(Int(0))
{
}
template <typename T>
QFlags<T>::operator Int()
{
return flag;
}
enum class TestFlag
{
Flag1,
Flag2
};
class DLL_API ComplexType class DLL_API ComplexType
{ {
public: public:
ComplexType(); ComplexType();
ComplexType(const QFlags<TestFlag> f); ComplexType(const QFlags<TestFlag> f);
ComplexType(const HasCtorWithMappedToEnum<TestFlag> f);
int check(); int check();
QFlags<TestFlag> returnsQFlags(); QFlags<TestFlag> returnsQFlags();
void takesQFlags(const QFlags<int> f); void takesQFlags(const QFlags<int> f);

56
tests/CSharp/CSharpTemplates.h

@ -1,3 +1,5 @@
#pragma once
#include "../Tests.h" #include "../Tests.h"
#include "AnotherUnit.h" #include "AnotherUnit.h"
@ -606,6 +608,59 @@ enum class UsedInTemplatedIndexer
Item2 Item2
}; };
template <typename T>
class DLL_API QFlags
{
typedef int Int;
typedef int (*Zero);
public:
QFlags(T t);
QFlags(Zero = 0);
operator Int();
private:
int flag;
};
template <typename T>
QFlags<T>::QFlags(T t) : flag(Int(t))
{
}
template <typename T>
QFlags<T>::QFlags(Zero) : flag(Int(0))
{
}
template <typename T>
QFlags<T>::operator Int()
{
return flag;
}
template <typename T>
class HasCtorWithMappedToEnum
{
public:
HasCtorWithMappedToEnum(QFlags<T> t);
HasCtorWithMappedToEnum(T t);
};
template <typename T>
HasCtorWithMappedToEnum<T>::HasCtorWithMappedToEnum(QFlags<T> t)
{
}
template <typename T>
HasCtorWithMappedToEnum<T>::HasCtorWithMappedToEnum(T t)
{
}
enum class TestFlag
{
Flag1,
Flag2
};
// we optimise specialisations so that only actually used ones are wrapped // we optimise specialisations so that only actually used ones are wrapped
void forceUseSpecializations(IndependentFields<int> _1, IndependentFields<bool> _2, void forceUseSpecializations(IndependentFields<int> _1, IndependentFields<bool> _2,
IndependentFields<T1> _3, IndependentFields<std::string> _4, IndependentFields<T1> _3, IndependentFields<std::string> _4,
@ -643,6 +698,7 @@ template class DLL_API TemplateWithIndexer<T1>;
template class DLL_API TemplateWithIndexer<T2*>; template class DLL_API TemplateWithIndexer<T2*>;
template class DLL_API TemplateWithIndexer<float>; template class DLL_API TemplateWithIndexer<float>;
template class DLL_API TemplateDerivedFromRegularDynamic<RegularDynamic>; template class DLL_API TemplateDerivedFromRegularDynamic<RegularDynamic>;
template class DLL_API HasCtorWithMappedToEnum<TestFlag>;
class TestForwardedClassInAnotherUnit; class TestForwardedClassInAnotherUnit;

Loading…
Cancel
Save