From 65cac93259d14c7bf38aab11b62f075ef215159d Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Tue, 19 Aug 2014 17:57:25 +0300 Subject: [PATCH] Included ignored fields in the wrappers for better marshalling. Signed-off-by: Dimitar Dobrev --- src/AST/Declaration.cs | 5 +++-- src/AST/TypeExtensions.cs | 19 ++++++++++++------- src/Generator/AST/Utils.cs | 2 +- .../Generators/CSharp/CSharpTextTemplate.cs | 12 +++++++----- src/Generator/Library.cs | 2 +- src/Generator/Passes/CheckIgnoredDecls.cs | 12 +++++++----- tests/Basic/Basic.cs | 1 + tests/Basic/Basic.h | 12 ++++++++++++ 8 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/AST/Declaration.cs b/src/AST/Declaration.cs index 48c47270..722b1abe 100644 --- a/src/AST/Declaration.cs +++ b/src/AST/Declaration.cs @@ -31,7 +31,7 @@ namespace CppSharp.AST public enum GenerationKind { /// - // Declaration is not generated. + /// Declaration is not generated. /// None, /// @@ -197,7 +197,8 @@ namespace CppSharp.AST return generationKind.Value; if (Namespace != null) - return Namespace.GenerationKind; + // fields in nested classes have to always be generated + return !Namespace.IsGenerated && this is Field ? GenerationKind.Internal : Namespace.GenerationKind; return GenerationKind.Generate; } diff --git a/src/AST/TypeExtensions.cs b/src/AST/TypeExtensions.cs index 6406bd22..f90dfa0f 100644 --- a/src/AST/TypeExtensions.cs +++ b/src/AST/TypeExtensions.cs @@ -112,27 +112,32 @@ } public static bool TryGetClass(this Type t, out Class @class) + { + return TryGetDeclaration(t, out @class); + } + + public static bool TryGetDeclaration(this Type t, out T decl) where T : Declaration { t = t.Desugar(); var tag = t as TagType; if (tag != null) { - @class = tag.Declaration as Class; - return @class != null; + decl = tag.Declaration as T; + return decl != null; } var type = t as TemplateSpecializationType; if (type != null) { var templatedClass = ((ClassTemplate)type.Template).TemplatedClass; - @class = templatedClass.CompleteDeclaration == null - ? templatedClass - : (Class)templatedClass.CompleteDeclaration; - return @class != null; + decl = templatedClass.CompleteDeclaration == null + ? templatedClass as T + : (T) templatedClass.CompleteDeclaration; + return decl != null; } - @class = null; + decl = null; return false; } diff --git a/src/Generator/AST/Utils.cs b/src/Generator/AST/Utils.cs index 285fa708..cabd7f36 100644 --- a/src/Generator/AST/Utils.cs +++ b/src/Generator/AST/Utils.cs @@ -62,7 +62,7 @@ namespace CppSharp.AST public static bool CheckIgnoreField(Field field, bool useInternals = false) { - if (field.Access == AccessSpecifier.Private) + if (field.Access == AccessSpecifier.Private && !useInternals) return true; if (field.Class.IsValueType && field.IsDeclared) diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index 01f1d708..e9c41ed6 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -316,7 +316,7 @@ namespace CppSharp.Generators.CSharp public void GenerateClass(Class @class) { - if (!@class.IsGenerated || @class.IsIncomplete) + if (@class.IsIncomplete) return; PushBlock(CSharpBlockKind.Class); @@ -332,7 +332,7 @@ namespace CppSharp.Generators.CSharp GenerateClassInternals(@class); GenerateDeclContext(@class); - if (@class.IsDependent) + if (@class.IsDependent || !@class.IsGenerated) goto exit; if (ShouldGenerateClassNativeField(@class)) @@ -789,8 +789,9 @@ namespace CppSharp.Generators.CSharp { // we do not support dependent fields yet, see https://github.com/mono/CppSharp/issues/197 Class @class; + field.Type.TryGetClass(out @class); if (field.Type.IsDependent && !field.Type.IsPointer() && - !(field.Type.TryGetClass(out @class) && @class.IsUnion)) + !(@class != null && @class.IsUnion)) return; var safeIdentifier = Helpers.SafeIdentifier(field.OriginalName); @@ -804,14 +805,15 @@ namespace CppSharp.Generators.CSharp if (!string.IsNullOrWhiteSpace(fieldTypePrinted.NameSuffix)) safeIdentifier += fieldTypePrinted.NameSuffix; + var access = @class != null && !@class.IsGenerated ? "internal" : "public"; if (field.Expression != null) { var fieldValuePrinted = field.Expression.CSharpValue(ExpressionPrinter); - Write("public {0} {1} = {2};", fieldTypePrinted.Type, safeIdentifier, fieldValuePrinted); + Write("{0} {1} {2} = {3};", access, fieldTypePrinted.Type, safeIdentifier, fieldValuePrinted); } else { - Write("public {0} {1};", fieldTypePrinted.Type, safeIdentifier); + Write("{0} {1} {2};", access, fieldTypePrinted.Type, safeIdentifier); } PopBlock(NewLineKind.BeforeNextBlock); diff --git a/src/Generator/Library.cs b/src/Generator/Library.cs index bd32595e..026e8d06 100644 --- a/src/Generator/Library.cs +++ b/src/Generator/Library.cs @@ -184,7 +184,7 @@ namespace CppSharp public static void IgnoreClassWithName(this ASTContext context, string name) { foreach (var @class in context.FindClass(name)) - @class.ExplicitlyIgnore(); + @class.GenerationKind = GenerationKind.Internal; } public static void SetClassAsOpaque(this ASTContext context, string name) diff --git a/src/Generator/Passes/CheckIgnoredDecls.cs b/src/Generator/Passes/CheckIgnoredDecls.cs index a23a70be..f6a6f4c0 100644 --- a/src/Generator/Passes/CheckIgnoredDecls.cs +++ b/src/Generator/Passes/CheckIgnoredDecls.cs @@ -41,13 +41,13 @@ namespace CppSharp.Passes { Log.Debug("Decl '{0}' was ignored due to invalid access", decl.Name); - decl.ExplicitlyIgnore(); + decl.GenerationKind = decl is Field ? GenerationKind.Internal : GenerationKind.None; return true; } if (decl.IsDependent) { - decl.ExplicitlyIgnore(); + decl.GenerationKind = decl is Field ? GenerationKind.Internal : GenerationKind.None; Log.Debug("Decl '{0}' was ignored due to dependent context", decl.Name); return true; @@ -63,11 +63,13 @@ namespace CppSharp.Passes var type = field.Type; - string msg; - if (!HasInvalidType(type, out msg)) + Declaration decl; + type.TryGetDeclaration(out decl); + string msg = "internal"; + if (decl == null || (decl.GenerationKind != GenerationKind.Internal && !HasInvalidType(type, out msg))) return false; - field.ExplicitlyIgnore(); + field.GenerationKind = GenerationKind.Internal; var @class = (Class)field.Namespace; diff --git a/tests/Basic/Basic.cs b/tests/Basic/Basic.cs index 4bd4ffb2..f1d9b0af 100644 --- a/tests/Basic/Basic.cs +++ b/tests/Basic/Basic.cs @@ -30,6 +30,7 @@ namespace CppSharp.Tests driver.AddTranslationUnitPass(new CheckMacroPass()); ctx.SetClassAsValueType("Bar"); ctx.SetClassAsValueType("Bar2"); + ctx.IgnoreClassWithName("IgnoredType"); } public static void Main(string[] args) diff --git a/tests/Basic/Basic.h b/tests/Basic/Basic.h index 43f1e6d8..5f70e28f 100644 --- a/tests/Basic/Basic.h +++ b/tests/Basic/Basic.h @@ -1,5 +1,16 @@ #include "../Tests.h" +class DLL_API IgnoredType +{ + class IgnoredNested + { + private: + int i; + }; +private: + int i; +}; + class DLL_API Foo { public: @@ -7,6 +18,7 @@ public: Foo(); int A; float B; + IgnoredType ignoredType; const char* GetANSI(); // TODO: VC++ does not support char16