Browse Source

Generate valid C# for typedef-ed type parameters

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1159/merge
Dimitar Dobrev 6 years ago
parent
commit
991c6ffb25
  1. 47
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  2. 8
      tests/CSharp/CSharpTemplates.h

47
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -217,32 +217,37 @@ namespace CppSharp.Generators.CSharp @@ -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)

8
tests/CSharp/CSharpTemplates.h

@ -32,6 +32,7 @@ class DLL_API Ignored @@ -32,6 +32,7 @@ class DLL_API Ignored
template <typename T>
class DLL_API IndependentFields : public T1
{
typedef T Type;
public:
IndependentFields();
IndependentFields(const IndependentFields<T>& other);
@ -44,6 +45,7 @@ public: @@ -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<T>::getDependent(const T& t) @@ -111,6 +113,12 @@ T IndependentFields<T>::getDependent(const T& t)
return t;
}
template <typename T>
T IndependentFields<T>::property()
{
return T();
}
template <typename T>
const T* IndependentFields<T>::propertyReturnDependentPointer()
{

Loading…
Cancel
Save