Browse Source

Merge pull request #304 from tomspilman/static2

Alternate Static Protected Fix
pull/300/merge
João Matos 11 years ago
parent
commit
8b7a1057d1
  1. 13
      src/AST/Declaration.cs
  2. 2
      src/Generator/AST/Utils.cs
  3. 2
      src/Generator/Passes/CheckIgnoredDecls.cs
  4. 37
      src/Generator/Passes/CheckStaticClass.cs
  5. 1
      tests/Basic/Basic.Tests.cs
  6. 8
      tests/Basic/Basic.h

13
src/AST/Declaration.cs

@ -215,6 +215,18 @@ namespace CppSharp.AST
} }
} }
/// <summary>
/// Whether the declaration was explicitly set to be generated via
/// the GenerationKind propery as opposed to the default generated state.
/// </summary>
public virtual bool IsExplicitlyGenerated
{
get
{
return generationKind.HasValue && generationKind.Value == GenerationKind.Generate;
}
}
/// <summary> /// <summary>
/// Whether the declaration internal bindings should be generated. /// Whether the declaration internal bindings should be generated.
/// </summary> /// </summary>
@ -245,7 +257,6 @@ namespace CppSharp.AST
GenerationKind = GenerationKind.None; GenerationKind = GenerationKind.None;
} }
[Obsolete("Replace set by ExplicitlyIgnore(). Replace get by GenerationKind == GenerationKind.None.")] [Obsolete("Replace set by ExplicitlyIgnore(). Replace get by GenerationKind == GenerationKind.None.")]
public bool ExplicityIgnored public bool ExplicityIgnored
{ {

2
src/Generator/AST/Utils.cs

@ -36,7 +36,7 @@ namespace CppSharp.AST
if (method.OperatorKind == CXXOperatorKind.Equal) if (method.OperatorKind == CXXOperatorKind.Equal)
return true; return true;
if (method.Access == AccessSpecifier.Private && !method.IsOverride) if (method.Access == AccessSpecifier.Private && !method.IsOverride && !method.IsExplicitlyGenerated)
return true; return true;
//Ignore copy constructor if a base class don't has or has a private copy constructor //Ignore copy constructor if a base class don't has or has a private copy constructor

2
src/Generator/Passes/CheckIgnoredDecls.cs

@ -23,7 +23,7 @@ namespace CppSharp.Passes
case AccessSpecifier.Private: case AccessSpecifier.Private:
var method = decl as Method; var method = decl as Method;
var isOverride = method != null && method.IsOverride; var isOverride = method != null && method.IsOverride;
return generateNonPublicDecls && isOverride; return generateNonPublicDecls && (isOverride || decl.IsExplicitlyGenerated);
} }
return true; return true;

37
src/Generator/Passes/CheckStaticClass.cs

@ -9,6 +9,38 @@ namespace CppSharp.Passes
/// </summary> /// </summary>
public class CheckStaticClass : TranslationUnitPass 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) static bool ReturnsClassInstance(Function function)
{ {
var returnType = function.ReturnType.Type.Desugar(); var returnType = function.ReturnType.Type.Desugar();
@ -28,9 +60,6 @@ namespace CppSharp.Passes
public override bool VisitClassDecl(Class @class) public override bool VisitClassDecl(Class @class)
{ {
if (!VisitDeclaration(@class))
return false;
// If the class has any non-private constructors then it cannot // If the class has any non-private constructors then it cannot
// be bound as a static class and we bail out early. // be bound as a static class and we bail out early.
if (@class.Constructors.Any(m => if (@class.Constructors.Any(m =>
@ -68,7 +97,7 @@ namespace CppSharp.Passes
foreach (var dtor in @class.Destructors) foreach (var dtor in @class.Destructors)
dtor.GenerationKind = GenerationKind.Internal; dtor.GenerationKind = GenerationKind.Internal;
return true; return base.VisitClassDecl(@class);
} }
} }
} }

1
tests/Basic/Basic.Tests.cs

@ -264,6 +264,7 @@ public class BasicTests : GeneratorTestFixture
public void TestStaticClasses() public void TestStaticClasses()
{ {
Assert.That(TestStaticClass.Add(1, 2), Is.EqualTo(3)); Assert.That(TestStaticClass.Add(1, 2), Is.EqualTo(3));
Assert.That(TestStaticClass.OneTwoThree, Is.EqualTo(123));
Assert.That(TestStaticClassDerived.Foo(), Is.EqualTo(0)); Assert.That(TestStaticClassDerived.Foo(), Is.EqualTo(0));
} }

8
tests/Basic/Basic.h

@ -294,6 +294,14 @@ struct DLL_API TestStaticClass
{ {
static int Add(int a, int b); 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: private:
TestStaticClass(); TestStaticClass();
}; };

Loading…
Cancel
Save