From eb2bbba8836cd8b21a616e4a9544a64da43561c5 Mon Sep 17 00:00:00 2001 From: triton Date: Fri, 9 May 2014 14:49:52 +0100 Subject: [PATCH] Added better support for nullptr_t types. --- src/Generator/Generators/CLI/CLIMarshal.cs | 5 +++-- .../Generators/CLI/CLITypePrinter.cs | 1 + .../Generators/CSharp/CSharpMarshal.cs | 1 + .../Generators/CSharp/CSharpTypePrinter.cs | 1 + src/Generator/Types/CppTypePrinter.cs | 1 + src/Generator/Types/Std/Stdlib.cs | 21 +++++++++++++++++++ src/Generator/Types/Types.cs | 15 +++++++++++-- tests/Basic/Basic.Tests.cs | 7 +++++++ tests/Basic/Basic.h | 9 ++++++++ 9 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/Generator/Generators/CLI/CLIMarshal.cs b/src/Generator/Generators/CLI/CLIMarshal.cs index c4b63d47..720b317c 100644 --- a/src/Generator/Generators/CLI/CLIMarshal.cs +++ b/src/Generator/Generators/CLI/CLIMarshal.cs @@ -145,12 +145,13 @@ namespace CppSharp.Generators.CLI case PrimitiveType.Int64: case PrimitiveType.UInt64: case PrimitiveType.Float: - case PrimitiveType.Double: + case PrimitiveType.Double: + case PrimitiveType.Null: Context.Return.Write(Context.ReturnVarName); return true; } - return false; + throw new NotSupportedException(); } public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) diff --git a/src/Generator/Generators/CLI/CLITypePrinter.cs b/src/Generator/Generators/CLI/CLITypePrinter.cs index dbabd825..f1dee991 100644 --- a/src/Generator/Generators/CLI/CLITypePrinter.cs +++ b/src/Generator/Generators/CLI/CLITypePrinter.cs @@ -201,6 +201,7 @@ namespace CppSharp.Generators.CLI case PrimitiveType.Double: return "double"; case PrimitiveType.IntPtr: return "IntPtr"; case PrimitiveType.UIntPtr: return "UIntPtr"; + case PrimitiveType.Null: return "void*"; } throw new NotSupportedException(); diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 1b833e5c..7d0b7e94 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -198,6 +198,7 @@ namespace CppSharp.Generators.CSharp case PrimitiveType.Float: case PrimitiveType.Double: case PrimitiveType.WideChar: + case PrimitiveType.Null: Context.Return.Write(Context.ReturnVarName); return true; case PrimitiveType.Char16: diff --git a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs index dc82cf00..a94eab5e 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -399,6 +399,7 @@ namespace CppSharp.Generators.CSharp case PrimitiveType.Double: return "double"; case PrimitiveType.IntPtr: return "global::System.IntPtr"; case PrimitiveType.UIntPtr: return "global::System.UIntPtr"; + case PrimitiveType.Null: return "void*"; } throw new NotSupportedException(); diff --git a/src/Generator/Types/CppTypePrinter.cs b/src/Generator/Types/CppTypePrinter.cs index 9e084752..c59fdb13 100644 --- a/src/Generator/Types/CppTypePrinter.cs +++ b/src/Generator/Types/CppTypePrinter.cs @@ -114,6 +114,7 @@ namespace CppSharp.Types case PrimitiveType.Double: return "double"; case PrimitiveType.IntPtr: return "void*"; case PrimitiveType.UIntPtr: return "uintptr_t"; + case PrimitiveType.Null: return "std::nullptr_t"; } throw new NotSupportedException(); diff --git a/src/Generator/Types/Std/Stdlib.cs b/src/Generator/Types/Std/Stdlib.cs index f06cfcd4..6b2373f7 100644 --- a/src/Generator/Types/Std/Stdlib.cs +++ b/src/Generator/Types/Std/Stdlib.cs @@ -1,6 +1,7 @@ using CppSharp.AST; using CppSharp.AST.Extensions; using CppSharp.Generators; +using CppSharp.Generators.AST; using CppSharp.Generators.CLI; using CppSharp.Generators.CSharp; @@ -312,4 +313,24 @@ namespace CppSharp.Types.Std marshalCtxName, ctx.Parameter.Name); } } + + [TypeMap("std::nullptr_t")] + public class NullPtr : TypeMap + { + public override bool DoesMarshalling { get { return false; } } + + public override void CLITypeReference(CLITypeReferenceCollector collector, + ASTRecord loc) + { + var typeRef = collector.GetTypeReference(loc.Value); + + var include = new Include + { + File = "cstddef", + Kind = Include.IncludeKind.Angled, + }; + + typeRef.Include = include; + } + } } diff --git a/src/Generator/Types/Types.cs b/src/Generator/Types/Types.cs index 56056b1c..9adabf9f 100644 --- a/src/Generator/Types/Types.cs +++ b/src/Generator/Types/Types.cs @@ -112,8 +112,19 @@ namespace CppSharp } return base.VisitMemberPointerType(member, quals); - } - + } + + public override bool VisitParameterDecl(Parameter parameter) + { + if (parameter.Type.IsPrimitiveType(PrimitiveType.Null)) + { + Ignore(); + return false; + } + + return base.VisitParameterDecl(parameter); + } + public override bool VisitTemplateSpecializationType( TemplateSpecializationType template, TypeQualifiers quals) { diff --git a/tests/Basic/Basic.Tests.cs b/tests/Basic/Basic.Tests.cs index 9ff8f769..493181de 100644 --- a/tests/Basic/Basic.Tests.cs +++ b/tests/Basic/Basic.Tests.cs @@ -308,6 +308,13 @@ public class BasicTests : GeneratorTestFixture { var ret = basic.TestDecltype(); Assert.AreEqual(0, ret); + } + + [Test] + public unsafe void TestNullPtrType() + { + var ret = basic.TestNullPtrTypeRet(); + Assert.AreEqual(IntPtr.Zero, new IntPtr(ret)); } } \ No newline at end of file diff --git a/tests/Basic/Basic.h b/tests/Basic/Basic.h index 68073c04..ebc77227 100644 --- a/tests/Basic/Basic.h +++ b/tests/Basic/Basic.h @@ -453,4 +453,13 @@ int Expr = 0; DLL_API decltype(Expr) TestDecltype() { return Expr; +} + +DLL_API void TestNullPtrType(decltype(nullptr)) +{ +} + +DLL_API decltype(nullptr) TestNullPtrTypeRet() +{ + return nullptr; } \ No newline at end of file