From 17506c57d1575df3800066c798afb3b85fe0720e Mon Sep 17 00:00:00 2001 From: Tom Spilman Date: Fri, 8 Aug 2014 17:51:38 -0500 Subject: [PATCH 1/5] Added Declaration.IsExplicitlyGenerated. --- src/AST/Declaration.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/AST/Declaration.cs b/src/AST/Declaration.cs index af63c65d..fdb37cf8 100644 --- a/src/AST/Declaration.cs +++ b/src/AST/Declaration.cs @@ -215,6 +215,18 @@ namespace CppSharp.AST } } + /// + /// Whether the declaration was explicitly set to be generated via + /// the GenerationKind propery as opposed to its default generated state. + /// + public virtual bool IsExplicitlyGenerated + { + get + { + return generationKind.HasValue && generationKind.Value == GenerationKind.Generate; + } + } + /// /// Whether the declaration internal bindings should be generated. /// From f33637e30c8a4e26a09ae52565f898f2d0a41d40 Mon Sep 17 00:00:00 2001 From: Tom Spilman Date: Fri, 8 Aug 2014 17:54:49 -0500 Subject: [PATCH 2/5] Fixed CheckStaticClass to promote protected members to private (as protected is not allowed in static classes). --- src/Generator/Passes/CheckStaticClass.cs | 37 +++++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/Generator/Passes/CheckStaticClass.cs b/src/Generator/Passes/CheckStaticClass.cs index 586520ac..ed49f31a 100644 --- a/src/Generator/Passes/CheckStaticClass.cs +++ b/src/Generator/Passes/CheckStaticClass.cs @@ -9,6 +9,38 @@ namespace CppSharp.Passes /// public class CheckStaticClass : TranslationUnitPass { + public CheckStaticClass() + { + Options.VisitClassBases = false; + } + + public override bool VisitDeclaration(Declaration decl) + { + if (!base.VisitDeclaration(decl)) + return false; + + if (Driver.Options.IsCSharpGenerator) + { + // C# cannot have protected members in static classes. + var @class = decl.Namespace as Class; + if ( decl.Access == AccessSpecifier.Protected && + decl.GenerationKind == GenerationKind.Generate && + @class != null && + @class.IsStatic) + { + // By setting it to private it will appear + // as an internal in the final C# wrapper. + decl.Access = AccessSpecifier.Private; + + // We need to explicity set the generation else the + // now private declaration will get filtered out later. + decl.GenerationKind = GenerationKind.Generate; + } + } + + return true; + } + static bool ReturnsClassInstance(Function function) { var returnType = function.ReturnType.Type.Desugar(); @@ -28,9 +60,6 @@ namespace CppSharp.Passes public override bool VisitClassDecl(Class @class) { - if (!VisitDeclaration(@class)) - return false; - // If the class has any non-private constructors then it cannot // be bound as a static class and we bail out early. if (@class.Constructors.Any(m => @@ -68,7 +97,7 @@ namespace CppSharp.Passes foreach (var dtor in @class.Destructors) dtor.GenerationKind = GenerationKind.Internal; - return true; + return base.VisitClassDecl(@class); } } } From 0f8977ec912d31c85828d81f4a959790cce6b965 Mon Sep 17 00:00:00 2001 From: Tom Spilman Date: Fri, 8 Aug 2014 17:55:58 -0500 Subject: [PATCH 3/5] Fixed a couple of places where privates that need to be generated were being filtered. --- src/Generator/AST/Utils.cs | 2 +- src/Generator/Passes/CheckIgnoredDecls.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Generator/AST/Utils.cs b/src/Generator/AST/Utils.cs index 12fffbe4..285fa708 100644 --- a/src/Generator/AST/Utils.cs +++ b/src/Generator/AST/Utils.cs @@ -36,7 +36,7 @@ namespace CppSharp.AST if (method.OperatorKind == CXXOperatorKind.Equal) return true; - if (method.Access == AccessSpecifier.Private && !method.IsOverride) + if (method.Access == AccessSpecifier.Private && !method.IsOverride && !method.IsExplicitlyGenerated) return true; //Ignore copy constructor if a base class don't has or has a private copy constructor diff --git a/src/Generator/Passes/CheckIgnoredDecls.cs b/src/Generator/Passes/CheckIgnoredDecls.cs index c5642840..a23a70be 100644 --- a/src/Generator/Passes/CheckIgnoredDecls.cs +++ b/src/Generator/Passes/CheckIgnoredDecls.cs @@ -23,7 +23,7 @@ namespace CppSharp.Passes case AccessSpecifier.Private: var method = decl as Method; var isOverride = method != null && method.IsOverride; - return generateNonPublicDecls && isOverride; + return generateNonPublicDecls && (isOverride || decl.IsExplicitlyGenerated); } return true; From 997de0e836f4f5f80a5f98a983b4f013ed38bdc4 Mon Sep 17 00:00:00 2001 From: Tom Spilman Date: Fri, 8 Aug 2014 17:56:21 -0500 Subject: [PATCH 4/5] Added test for static protected methods/properties. --- tests/Basic/Basic.Tests.cs | 1 + tests/Basic/Basic.h | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/tests/Basic/Basic.Tests.cs b/tests/Basic/Basic.Tests.cs index 43d9022c..013c08d7 100644 --- a/tests/Basic/Basic.Tests.cs +++ b/tests/Basic/Basic.Tests.cs @@ -264,6 +264,7 @@ public class BasicTests : GeneratorTestFixture public void TestStaticClasses() { Assert.That(TestStaticClass.Add(1, 2), Is.EqualTo(3)); + Assert.That(TestStaticClass.OneTwoThree, Is.EqualTo(123)); Assert.That(TestStaticClassDerived.Foo(), Is.EqualTo(0)); } diff --git a/tests/Basic/Basic.h b/tests/Basic/Basic.h index 8736b05f..160c129b 100644 --- a/tests/Basic/Basic.h +++ b/tests/Basic/Basic.h @@ -294,6 +294,14 @@ struct DLL_API TestStaticClass { static int Add(int a, int b); + static int GetOneTwoThree() { return 123; } + +protected: + + static int _Mult(int a, int b) { return a * b; } + + static int GetFourFiveSix() { return 456; } + private: TestStaticClass(); }; From 6b791ed75206ad1f6f16ae17efb20e8b1f972862 Mon Sep 17 00:00:00 2001 From: Tom Spilman Date: Fri, 8 Aug 2014 18:02:27 -0500 Subject: [PATCH 5/5] Fixed comment. --- src/AST/Declaration.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/AST/Declaration.cs b/src/AST/Declaration.cs index fdb37cf8..48c47270 100644 --- a/src/AST/Declaration.cs +++ b/src/AST/Declaration.cs @@ -217,7 +217,7 @@ namespace CppSharp.AST /// /// Whether the declaration was explicitly set to be generated via - /// the GenerationKind propery as opposed to its default generated state. + /// the GenerationKind propery as opposed to the default generated state. /// public virtual bool IsExplicitlyGenerated { @@ -257,7 +257,6 @@ namespace CppSharp.AST GenerationKind = GenerationKind.None; } - [Obsolete("Replace set by ExplicitlyIgnore(). Replace get by GenerationKind == GenerationKind.None.")] public bool ExplicityIgnored {