diff --git a/src/AST/Declaration.cs b/src/AST/Declaration.cs index af63c65d..48c47270 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 the default generated state. + /// + public virtual bool IsExplicitlyGenerated + { + get + { + return generationKind.HasValue && generationKind.Value == GenerationKind.Generate; + } + } + /// /// Whether the declaration internal bindings should be generated. /// @@ -245,7 +257,6 @@ namespace CppSharp.AST GenerationKind = GenerationKind.None; } - [Obsolete("Replace set by ExplicitlyIgnore(). Replace get by GenerationKind == GenerationKind.None.")] public bool ExplicityIgnored { 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; 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); } } } 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(); };