From 991c6ffb256cdcfa7556cb252ddfb0c2741620c0 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Tue, 25 Jun 2019 03:49:05 +0300 Subject: [PATCH] Generate valid C# for typedef-ed type parameters Signed-off-by: Dimitar Dobrev --- .../Generators/CSharp/CSharpMarshal.cs | 47 ++++++++++--------- tests/CSharp/CSharpTemplates.h | 8 ++++ 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 845893e4..d25db12c 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -217,32 +217,37 @@ namespace CppSharp.Generators.CSharp public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) { - if (!VisitType(typedef, quals)) + if (!(typedef.Declaration.Type.Desugar(false) is TemplateParameterSubstitutionType) && + !VisitType(typedef, quals)) return false; var decl = typedef.Declaration; - var functionType = decl.Type as FunctionType; - if (functionType != null || decl.Type.IsPointerTo(out functionType)) - { - var ptrName = $"{Generator.GeneratedIdentifier("ptr")}{Context.ParameterIndex}"; - - Context.Before.WriteLine($"var {ptrName} = {Context.ReturnVarName};"); - - var specialization = decl.Namespace as ClassTemplateSpecialization; - Type returnType = Context.ReturnType.Type.Desugar(); - var finalType = (returnType.GetFinalPointee() ?? returnType).Desugar(); - var res = string.Format( - "{0} == IntPtr.Zero? null : {1}({2}) Marshal.GetDelegateForFunctionPointer({0}, typeof({2}))", - ptrName, - finalType.IsDependent ? $@"({specialization.TemplatedDecl.TemplatedClass.Typedefs.First( - t => t.Name == decl.Name).Visit(this.typePrinter)}) (object) " : string.Empty, - typedef); - Context.Return.Write(res); - return true; - } + Type type = decl.Type.Desugar(); + var functionType = type as FunctionType; + if (functionType == null && !type.IsPointerTo(out functionType)) + return decl.Type.Visit(this); + + var ptrName = $@"{Generator.GeneratedIdentifier("ptr")}{ + Context.ParameterIndex}"; - return decl.Type.Visit(this); + Context.Before.WriteLine($"var {ptrName} = {Context.ReturnVarName};"); + + var substitution = decl.Type.Desugar(false) + as TemplateParameterSubstitutionType; + + if (substitution != null) + Context.Return.Write($@"({ + substitution.ReplacedParameter.Parameter.Name}) (object) ("); + + Context.Return.Write($@"{ptrName} == IntPtr.Zero? null : { + (substitution == null ? $"({Context.ReturnType}) " : + string.Empty)}Marshal.GetDelegateForFunctionPointer({ + ptrName}, typeof({typedef}))"); + + if (substitution != null) + Context.Return.Write(")"); + return true; } public override bool VisitFunctionType(FunctionType function, TypeQualifiers quals) diff --git a/tests/CSharp/CSharpTemplates.h b/tests/CSharp/CSharpTemplates.h index 2cf4d8fb..fd31b366 100644 --- a/tests/CSharp/CSharpTemplates.h +++ b/tests/CSharp/CSharpTemplates.h @@ -32,6 +32,7 @@ class DLL_API Ignored template class DLL_API IndependentFields : public T1 { + typedef T Type; public: IndependentFields(); IndependentFields(const IndependentFields& other); @@ -44,6 +45,7 @@ public: int getIndependent(); const T* returnTakeDependentPointer(const T* p); T getDependent(const T& t); + Type property(); const T* propertyReturnDependentPointer(); static T staticDependent(const T& t); void hasDefaultDependentParam(T* ptr, const T& refT = T()); @@ -111,6 +113,12 @@ T IndependentFields::getDependent(const T& t) return t; } +template +T IndependentFields::property() +{ + return T(); +} + template const T* IndependentFields::propertyReturnDependentPointer() {