Skip to content

Commit 991c6ff

Browse files
committed
Generate valid C# for typedef-ed type parameters
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent a991dbd commit 991c6ff

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

src/Generator/Generators/CSharp/CSharpMarshal.cs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -217,32 +217,37 @@ public override bool VisitPrimitiveType(PrimitiveType primitive, TypeQualifiers
217217

218218
public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
219219
{
220-
if (!VisitType(typedef, quals))
220+
if (!(typedef.Declaration.Type.Desugar(false) is TemplateParameterSubstitutionType) &&
221+
!VisitType(typedef, quals))
221222
return false;
222223

223224
var decl = typedef.Declaration;
224225

225-
var functionType = decl.Type as FunctionType;
226-
if (functionType != null || decl.Type.IsPointerTo(out functionType))
227-
{
228-
var ptrName = $"{Generator.GeneratedIdentifier("ptr")}{Context.ParameterIndex}";
229-
230-
Context.Before.WriteLine($"var {ptrName} = {Context.ReturnVarName};");
231-
232-
var specialization = decl.Namespace as ClassTemplateSpecialization;
233-
Type returnType = Context.ReturnType.Type.Desugar();
234-
var finalType = (returnType.GetFinalPointee() ?? returnType).Desugar();
235-
var res = string.Format(
236-
"{0} == IntPtr.Zero? null : {1}({2}) Marshal.GetDelegateForFunctionPointer({0}, typeof({2}))",
237-
ptrName,
238-
finalType.IsDependent ? $@"({specialization.TemplatedDecl.TemplatedClass.Typedefs.First(
239-
t => t.Name == decl.Name).Visit(this.typePrinter)}) (object) " : string.Empty,
240-
typedef);
241-
Context.Return.Write(res);
242-
return true;
243-
}
226+
Type type = decl.Type.Desugar();
227+
var functionType = type as FunctionType;
228+
if (functionType == null && !type.IsPointerTo(out functionType))
229+
return decl.Type.Visit(this);
230+
231+
var ptrName = $@"{Generator.GeneratedIdentifier("ptr")}{
232+
Context.ParameterIndex}";
244233

245-
return decl.Type.Visit(this);
234+
Context.Before.WriteLine($"var {ptrName} = {Context.ReturnVarName};");
235+
236+
var substitution = decl.Type.Desugar(false)
237+
as TemplateParameterSubstitutionType;
238+
239+
if (substitution != null)
240+
Context.Return.Write($@"({
241+
substitution.ReplacedParameter.Parameter.Name}) (object) (");
242+
243+
Context.Return.Write($@"{ptrName} == IntPtr.Zero? null : {
244+
(substitution == null ? $"({Context.ReturnType}) " :
245+
string.Empty)}Marshal.GetDelegateForFunctionPointer({
246+
ptrName}, typeof({typedef}))");
247+
248+
if (substitution != null)
249+
Context.Return.Write(")");
250+
return true;
246251
}
247252

248253
public override bool VisitFunctionType(FunctionType function, TypeQualifiers quals)

tests/CSharp/CSharpTemplates.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class DLL_API Ignored
3232
template <typename T>
3333
class DLL_API IndependentFields : public T1
3434
{
35+
typedef T Type;
3536
public:
3637
IndependentFields();
3738
IndependentFields(const IndependentFields<T>& other);
@@ -44,6 +45,7 @@ class DLL_API IndependentFields : public T1
4445
int getIndependent();
4546
const T* returnTakeDependentPointer(const T* p);
4647
T getDependent(const T& t);
48+
Type property();
4749
const T* propertyReturnDependentPointer();
4850
static T staticDependent(const T& t);
4951
void hasDefaultDependentParam(T* ptr, const T& refT = T());
@@ -111,6 +113,12 @@ T IndependentFields<T>::getDependent(const T& t)
111113
return t;
112114
}
113115

116+
template <typename T>
117+
T IndependentFields<T>::property()
118+
{
119+
return T();
120+
}
121+
114122
template <typename T>
115123
const T* IndependentFields<T>::propertyReturnDependentPointer()
116124
{

0 commit comments

Comments
 (0)