From 53816286bc951f1102a39b0963c38aedee4567d1 Mon Sep 17 00:00:00 2001 From: Joao Matos Date: Wed, 1 May 2019 22:42:28 +0100 Subject: [PATCH] Fixed type map support for typedef types. Fixes https://github.com/mono/CppSharp/issues/1205. --- src/AST/Type.cs | 6 +++++- .../Generators/CSharp/CSharpTypePrinter.cs | 2 +- src/Generator/Types/TypeMapDatabase.cs | 2 +- tests/CSharp/CSharp.Tests.cs | 6 ++++++ tests/CSharp/CSharp.cpp | 5 +++++ tests/CSharp/CSharp.cs | 18 ++++++++++++++++++ tests/CSharp/CSharp.h | 4 ++++ 7 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/AST/Type.cs b/src/AST/Type.cs index 311c9ec5..c6933f2f 100644 --- a/src/AST/Type.cs +++ b/src/AST/Type.cs @@ -438,7 +438,11 @@ namespace CppSharp.AST public override bool Equals(object obj) { var typedef = obj as TypedefType; - return Declaration.Type.Equals(typedef == null ? obj : typedef.Declaration.Type); + if (typedef == null) + return false; + + return Declaration.OriginalName == typedef.Declaration.OriginalName && + Declaration.Type.Equals(typedef.Declaration.Type); } public override int GetHashCode() => diff --git a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs index 0a7167a9..7ca2642e 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -253,7 +253,7 @@ namespace CppSharp.Generators.CSharp var decl = typedef.Declaration; TypeMap typeMap; - if (TypeMapDatabase.FindTypeMap(decl.Type, out typeMap)) + if (TypeMapDatabase.FindTypeMap(typedef, out typeMap)) { typeMap.Type = typedef; diff --git a/src/Generator/Types/TypeMapDatabase.cs b/src/Generator/Types/TypeMapDatabase.cs index 38b8b622..da4fecf5 100644 --- a/src/Generator/Types/TypeMapDatabase.cs +++ b/src/Generator/Types/TypeMapDatabase.cs @@ -93,7 +93,7 @@ namespace CppSharp.Types PrintLogicalNames = true }; - foreach (var resolveTypeDefs in new[] { true, false }) + foreach (var resolveTypeDefs in new[] { false, true }) { foreach (var typePrintScopeKind in new[] { TypePrintScopeKind.Local, TypePrintScopeKind.Qualified }) diff --git a/tests/CSharp/CSharp.Tests.cs b/tests/CSharp/CSharp.Tests.cs index 3be409a5..1e35c29a 100644 --- a/tests/CSharp/CSharp.Tests.cs +++ b/tests/CSharp/CSharp.Tests.cs @@ -1285,4 +1285,10 @@ public unsafe class CSharpTests : GeneratorTestFixture { public override int Function() => 10; } + + [Test] + public void TestTypemapTypedefParam() + { + Assert.That(CSharp.CSharp.TakeTypemapTypedefParam(false), Is.False); + } } diff --git a/tests/CSharp/CSharp.cpp b/tests/CSharp/CSharp.cpp index 1f5a6de6..afb073d6 100644 --- a/tests/CSharp/CSharp.cpp +++ b/tests/CSharp/CSharp.cpp @@ -1564,3 +1564,8 @@ const char*& takeConstCharStarRef(const char*& c) { return c; } + +boolean_t takeTypemapTypedefParam(boolean_t b) +{ + return b; +} diff --git a/tests/CSharp/CSharp.cs b/tests/CSharp/CSharp.cs index bfca2e7b..1dcf5a67 100644 --- a/tests/CSharp/CSharp.cs +++ b/tests/CSharp/CSharp.cs @@ -95,6 +95,24 @@ namespace CppSharp.Tests } #region Type Maps + [TypeMap("boolean_t")] + public class BooleanTypeMap : TypeMap + { + public override Type CSharpSignatureType(TypePrinterContext ctx) + { + return new BuiltinType(PrimitiveType.Bool); + } + + public override void CSharpMarshalToNative(CSharpMarshalContext ctx) + { + ctx.Return.Write(ctx.Parameter.Name); + } + + public override void CSharpMarshalToManaged(CSharpMarshalContext ctx) + { + ctx.Return.Write(ctx.ReturnVarName); + } + } [TypeMap("QFlags")] public class QFlags : TypeMap diff --git a/tests/CSharp/CSharp.h b/tests/CSharp/CSharp.h index 9c283fd5..acde268c 100644 --- a/tests/CSharp/CSharp.h +++ b/tests/CSharp/CSharp.h @@ -1321,3 +1321,7 @@ struct { } example; } root; } kotlin; + +typedef int boolean_t; +DLL_API boolean_t takeTypemapTypedefParam(boolean_t b); +