Skip to content

Commit cbb1e19

Browse files
committed
Add pass to fix up pure C++ methods to C++ generator.
1 parent aca5da8 commit cbb1e19

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/Generator/Generators/C/CppGenerator.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using CppSharp.AST;
33
using CppSharp.Generators.C;
4+
using CppSharp.Passes;
45

56
namespace CppSharp.Generators.Cpp
67
{
@@ -30,7 +31,11 @@ public override List<CodeGenerator> Generate(IEnumerable<TranslationUnit> units)
3031
return outputs;
3132
}
3233

33-
public override bool SetupPasses() => true;
34+
public override bool SetupPasses()
35+
{
36+
new FixupPureMethodsPass().VisitASTContext(Context.ASTContext);
37+
return true;
38+
}
3439

3540
public static bool ShouldGenerateClassNativeField(Class @class)
3641
{
@@ -45,4 +50,21 @@ protected override string TypePrinterDelegate(Type type)
4550
return type.Visit(typePrinter).ToString();
4651
}
4752
}
53+
54+
/// <summary>
55+
/// Removes the pureness of virtual abstract methods in C++ classes since
56+
/// the generated classes cannot have virtual pure methods, as they call
57+
/// the original pure method.
58+
/// This lets user code mark some methods as pure if needed, in that case
59+
/// the generator can generate the necessary pure C++ code annotations safely
60+
/// knowing the only pure functions were user-specified.
61+
/// </summary>
62+
public class FixupPureMethodsPass : TranslationUnitPass
63+
{
64+
public override bool VisitMethodDecl(Method method)
65+
{
66+
method.IsPure = false;
67+
return base.VisitMethodDecl(method);
68+
}
69+
}
4870
}

0 commit comments

Comments
 (0)