From f33637e30c8a4e26a09ae52565f898f2d0a41d40 Mon Sep 17 00:00:00 2001 From: Tom Spilman Date: Fri, 8 Aug 2014 17:54:49 -0500 Subject: [PATCH] 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); } } }