Skip to content

Commit 82e41d3

Browse files
AleXr64ddobrev
authored andcommitted
Fix a crash when a function pointer takes a function pointer
Fixes #1144
1 parent 0e56bc6 commit 82e41d3

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

src/Generator/Generators/CSharp/CSharpTypePrinter.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,14 @@ public override TypePrinterResult VisitTypedefType(TypedefType typedef,
276276
return typeMap.CSharpSignatureType(typePrinterContext).ToString();
277277
}
278278

279-
FunctionType func = decl.Type as FunctionType;
279+
FunctionType func;
280+
if (decl.Type.IsPointerTo(out func))
281+
{
282+
if (MarshalKind == MarshalKind.GenericDelegate && ContextKind == TypePrinterContextKind.Native)
283+
return VisitDeclaration(decl);
284+
}
285+
286+
func = decl.Type as FunctionType;
280287
if (func != null || decl.Type.IsPointerTo(out func))
281288
{
282289
if (ContextKind == TypePrinterContextKind.Native)

src/Generator/Passes/DelegatesPass.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ public override bool VisitFunctionDecl(Function function)
9494

9595
public override bool VisitParameterDecl(Parameter parameter)
9696
{
97+
if(parameter.Namespace?.TranslationUnit?.Module == null && namespaces.Count > 0)
98+
parameter.Namespace = namespaces.Peek();
99+
97100
if (!base.VisitDeclaration(parameter) || parameter.Namespace == null ||
98101
parameter.Namespace.Ignore)
99102
return false;
@@ -117,12 +120,17 @@ public override bool VisitProperty(Property property)
117120

118121
public override bool VisitFieldDecl(Field field)
119122
{
123+
if (field.Namespace?.TranslationUnit?.Module != null)
124+
namespaces.Push(field.Namespace);
125+
120126
if (!base.VisitFieldDecl(field))
121127
return false;
122128

123129
field.QualifiedType = CheckForDelegate(field.QualifiedType,
124130
field.Namespace);
125131

132+
namespaces.Clear();
133+
126134
return true;
127135
}
128136

@@ -297,5 +305,6 @@ private CSharpTypePrinter TypePrinter
297305
/// iterating over it, so we collect all the typedefs and add them at the end.
298306
/// </summary>
299307
private readonly List<TypedefDecl> delegates = new List<TypedefDecl>();
308+
private readonly Stack<DeclarationContext> namespaces = new Stack<DeclarationContext>();
300309
}
301310
}

tests/CSharp/CSharp.Tests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,8 @@ public void Test()
12681268
{
12691269
hasFunctionPtrField.FunctionPtrField = @string => @string.Length;
12701270
Assert.That(hasFunctionPtrField.FunctionPtrField("Test"), Is.EqualTo(4));
1271+
hasFunctionPtrField.FunctionPtrTakeFunctionPtrField = field => field();
1272+
Assert.That(hasFunctionPtrField.FunctionPtrTakeFunctionPtrField(() => 42), Is.EqualTo(42));
12711273
}
12721274
}
12731275

tests/CSharp/CSharp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,7 @@ class DLL_API HasFunctionPtrField
13011301
HasFunctionPtrField();
13021302
~HasFunctionPtrField();
13031303
int (*functionPtrField)(const char*);
1304+
int (*functionPtrTakeFunctionPtrField)(int(*TakenInFuncPtrField)());
13041305
};
13051306

13061307
DLL_API void va_listFunction(va_list v);

0 commit comments

Comments
 (0)