Skip to content

Commit d735f39

Browse files
committed
Fix the generated C# for fields of type function pointer
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent d5380fe commit d735f39

File tree

7 files changed

+55
-39
lines changed

7 files changed

+55
-39
lines changed

src/Generator/Generators/CSharp/CSharpTypePrinter.cs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -157,42 +157,6 @@ public override TypePrinterResult VisitBuiltinType(BuiltinType builtin, TypeQual
157157
return base.VisitBuiltinType(builtin, quals);
158158
}
159159

160-
public override TypePrinterResult VisitFunctionType(FunctionType function,
161-
TypeQualifiers quals)
162-
{
163-
var arguments = function.Parameters;
164-
var returnType = function.ReturnType;
165-
var args = string.Empty;
166-
167-
PushMarshalKind(MarshalKind.GenericDelegate);
168-
169-
if (arguments.Count > 0)
170-
args = VisitParameters(function.Parameters, hasNames: false).Type;
171-
172-
PopMarshalKind();
173-
174-
if (ContextKind != TypePrinterContextKind.Managed)
175-
return IntPtrType;
176-
177-
if (returnType.Type.IsPrimitiveType(PrimitiveType.Void))
178-
{
179-
if (!string.IsNullOrEmpty(args))
180-
args = string.Format("<{0}>", args);
181-
return string.Format("Action{0}", args);
182-
}
183-
184-
if (!string.IsNullOrEmpty(args))
185-
args = string.Format(", {0}", args);
186-
187-
PushMarshalKind(MarshalKind.GenericDelegate);
188-
189-
var returnTypePrinterResult = returnType.Visit(this);
190-
191-
PopMarshalKind();
192-
193-
return string.Format("Func<{0}{1}>", returnTypePrinterResult, args);
194-
}
195-
196160
private bool allowStrings = true;
197161

198162
public override TypePrinterResult VisitPointerType(PointerType pointer,

src/Generator/Passes/CheckIgnoredDecls.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public override bool VisitFieldDecl(Field field)
110110
type.TryGetDeclaration(out decl);
111111
string msg = "internal";
112112
TypeMap typeMap;
113-
if (!(type is FunctionType) && (decl == null ||
113+
if ((decl == null ||
114114
((decl.GenerationKind != GenerationKind.Internal ||
115115
Context.TypeMaps.FindTypeMap(type, out typeMap)) &&
116116
!HasInvalidType(field, out msg))))

src/Generator/Passes/DelegatesPass.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ public override bool VisitParameterDecl(Parameter parameter)
7676
return true;
7777
}
7878

79+
public override bool VisitProperty(Property property)
80+
{
81+
if (!base.VisitProperty(property))
82+
return false;
83+
84+
property.QualifiedType = CheckForDelegate(property.QualifiedType, property);
85+
86+
return true;
87+
}
88+
89+
public override bool VisitFieldDecl(Field field)
90+
{
91+
if (!base.VisitFieldDecl(field))
92+
return false;
93+
94+
field.QualifiedType = CheckForDelegate(field.QualifiedType, field);
95+
96+
return true;
97+
}
98+
7999
private QualifiedType CheckForDelegate(QualifiedType type, ITypedDecl decl)
80100
{
81101
if (type.Type is TypedefType)

src/Generator/Passes/RenamePass.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,14 @@ private static bool AreThereConflicts(Declaration decl, string newName)
190190
declarations.RemoveAll(d => specialization.TemplatedDecl.TemplatedDecl == d);
191191

192192
var @class = decl.Namespace as Class;
193-
if (@class != null && @class.IsDependent)
194-
declarations.AddRange(@class.TemplateParameters);
193+
if (@class != null)
194+
{
195+
declarations.AddRange(from typedefDecl in @class.Typedefs
196+
where typedefDecl.Type.Desugar() is FunctionType
197+
select typedefDecl);
198+
if (@class.IsDependent)
199+
declarations.AddRange(@class.TemplateParameters);
200+
}
195201

196202
var result = declarations.Any(d => d != decl && d.Name == newName);
197203
if (result)

tests/CSharp/CSharp.Tests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,16 @@ public void TestConstCharStarRef()
12611261
Assert.That(CSharp.CSharp.TakeConstCharStarRef("Test"), Is.EqualTo("Test"));
12621262
}
12631263

1264+
[Test]
1265+
public void Test()
1266+
{
1267+
using (var hasFunctionPtrField = new HasFunctionPtrField())
1268+
{
1269+
hasFunctionPtrField.FunctionPtrField = @string => @string.Length;
1270+
Assert.That(hasFunctionPtrField.FunctionPtrField("Test"), Is.EqualTo(4));
1271+
}
1272+
}
1273+
12641274
public class Inter : SimpleInterface
12651275
{
12661276
public override int Size => s;

tests/CSharp/CSharp.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,14 @@ void InterfaceTester::setInterface(SimpleInterface* i)
15431543
interface = i;
15441544
}
15451545

1546+
HasFunctionPtrField::HasFunctionPtrField()
1547+
{
1548+
}
1549+
1550+
HasFunctionPtrField::~HasFunctionPtrField()
1551+
{
1552+
}
1553+
15461554
void va_listFunction(va_list v)
15471555
{
15481556
}

tests/CSharp/CSharp.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,14 @@ class DLL_API InterfaceTester
12951295
SimpleInterface* interface;
12961296
};
12971297

1298+
class DLL_API HasFunctionPtrField
1299+
{
1300+
public:
1301+
HasFunctionPtrField();
1302+
~HasFunctionPtrField();
1303+
int (*functionPtrField)(const char*);
1304+
};
1305+
12981306
DLL_API void va_listFunction(va_list v);
12991307
DLL_API char* returnCharPointer();
13001308
DLL_API const char* takeConstCharStarRef(const char*& c);

0 commit comments

Comments
 (0)