Browse Source

Included ignored fields in the wrappers for better marshalling.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/313/head
Dimitar Dobrev 11 years ago
parent
commit
65cac93259
  1. 5
      src/AST/Declaration.cs
  2. 19
      src/AST/TypeExtensions.cs
  3. 2
      src/Generator/AST/Utils.cs
  4. 12
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  5. 2
      src/Generator/Library.cs
  6. 12
      src/Generator/Passes/CheckIgnoredDecls.cs
  7. 1
      tests/Basic/Basic.cs
  8. 12
      tests/Basic/Basic.h

5
src/AST/Declaration.cs

@ -31,7 +31,7 @@ namespace CppSharp.AST
public enum GenerationKind public enum GenerationKind
{ {
/// <summary> /// <summary>
// Declaration is not generated. /// Declaration is not generated.
/// </summary> /// </summary>
None, None,
/// <summary> /// <summary>
@ -197,7 +197,8 @@ namespace CppSharp.AST
return generationKind.Value; return generationKind.Value;
if (Namespace != null) 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; return GenerationKind.Generate;
} }

19
src/AST/TypeExtensions.cs

@ -112,27 +112,32 @@
} }
public static bool TryGetClass(this Type t, out Class @class) public static bool TryGetClass(this Type t, out Class @class)
{
return TryGetDeclaration(t, out @class);
}
public static bool TryGetDeclaration<T>(this Type t, out T decl) where T : Declaration
{ {
t = t.Desugar(); t = t.Desugar();
var tag = t as TagType; var tag = t as TagType;
if (tag != null) if (tag != null)
{ {
@class = tag.Declaration as Class; decl = tag.Declaration as T;
return @class != null; return decl != null;
} }
var type = t as TemplateSpecializationType; var type = t as TemplateSpecializationType;
if (type != null) if (type != null)
{ {
var templatedClass = ((ClassTemplate)type.Template).TemplatedClass; var templatedClass = ((ClassTemplate)type.Template).TemplatedClass;
@class = templatedClass.CompleteDeclaration == null decl = templatedClass.CompleteDeclaration == null
? templatedClass ? templatedClass as T
: (Class)templatedClass.CompleteDeclaration; : (T) templatedClass.CompleteDeclaration;
return @class != null; return decl != null;
} }
@class = null; decl = null;
return false; return false;
} }

2
src/Generator/AST/Utils.cs

@ -62,7 +62,7 @@ namespace CppSharp.AST
public static bool CheckIgnoreField(Field field, bool useInternals = false) public static bool CheckIgnoreField(Field field, bool useInternals = false)
{ {
if (field.Access == AccessSpecifier.Private) if (field.Access == AccessSpecifier.Private && !useInternals)
return true; return true;
if (field.Class.IsValueType && field.IsDeclared) if (field.Class.IsValueType && field.IsDeclared)

12
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -316,7 +316,7 @@ namespace CppSharp.Generators.CSharp
public void GenerateClass(Class @class) public void GenerateClass(Class @class)
{ {
if (!@class.IsGenerated || @class.IsIncomplete) if (@class.IsIncomplete)
return; return;
PushBlock(CSharpBlockKind.Class); PushBlock(CSharpBlockKind.Class);
@ -332,7 +332,7 @@ namespace CppSharp.Generators.CSharp
GenerateClassInternals(@class); GenerateClassInternals(@class);
GenerateDeclContext(@class); GenerateDeclContext(@class);
if (@class.IsDependent) if (@class.IsDependent || !@class.IsGenerated)
goto exit; goto exit;
if (ShouldGenerateClassNativeField(@class)) 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 // we do not support dependent fields yet, see https://github.com/mono/CppSharp/issues/197
Class @class; Class @class;
field.Type.TryGetClass(out @class);
if (field.Type.IsDependent && !field.Type.IsPointer() && if (field.Type.IsDependent && !field.Type.IsPointer() &&
!(field.Type.TryGetClass(out @class) && @class.IsUnion)) !(@class != null && @class.IsUnion))
return; return;
var safeIdentifier = Helpers.SafeIdentifier(field.OriginalName); var safeIdentifier = Helpers.SafeIdentifier(field.OriginalName);
@ -804,14 +805,15 @@ namespace CppSharp.Generators.CSharp
if (!string.IsNullOrWhiteSpace(fieldTypePrinted.NameSuffix)) if (!string.IsNullOrWhiteSpace(fieldTypePrinted.NameSuffix))
safeIdentifier += fieldTypePrinted.NameSuffix; safeIdentifier += fieldTypePrinted.NameSuffix;
var access = @class != null && !@class.IsGenerated ? "internal" : "public";
if (field.Expression != null) if (field.Expression != null)
{ {
var fieldValuePrinted = field.Expression.CSharpValue(ExpressionPrinter); 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 else
{ {
Write("public {0} {1};", fieldTypePrinted.Type, safeIdentifier); Write("{0} {1} {2};", access, fieldTypePrinted.Type, safeIdentifier);
} }
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);

2
src/Generator/Library.cs

@ -184,7 +184,7 @@ namespace CppSharp
public static void IgnoreClassWithName(this ASTContext context, string name) public static void IgnoreClassWithName(this ASTContext context, string name)
{ {
foreach (var @class in context.FindClass(name)) foreach (var @class in context.FindClass(name))
@class.ExplicitlyIgnore(); @class.GenerationKind = GenerationKind.Internal;
} }
public static void SetClassAsOpaque(this ASTContext context, string name) public static void SetClassAsOpaque(this ASTContext context, string name)

12
src/Generator/Passes/CheckIgnoredDecls.cs

@ -41,13 +41,13 @@ namespace CppSharp.Passes
{ {
Log.Debug("Decl '{0}' was ignored due to invalid access", Log.Debug("Decl '{0}' was ignored due to invalid access",
decl.Name); decl.Name);
decl.ExplicitlyIgnore(); decl.GenerationKind = decl is Field ? GenerationKind.Internal : GenerationKind.None;
return true; return true;
} }
if (decl.IsDependent) if (decl.IsDependent)
{ {
decl.ExplicitlyIgnore(); decl.GenerationKind = decl is Field ? GenerationKind.Internal : GenerationKind.None;
Log.Debug("Decl '{0}' was ignored due to dependent context", Log.Debug("Decl '{0}' was ignored due to dependent context",
decl.Name); decl.Name);
return true; return true;
@ -63,11 +63,13 @@ namespace CppSharp.Passes
var type = field.Type; var type = field.Type;
string msg; Declaration decl;
if (!HasInvalidType(type, out msg)) type.TryGetDeclaration(out decl);
string msg = "internal";
if (decl == null || (decl.GenerationKind != GenerationKind.Internal && !HasInvalidType(type, out msg)))
return false; return false;
field.ExplicitlyIgnore(); field.GenerationKind = GenerationKind.Internal;
var @class = (Class)field.Namespace; var @class = (Class)field.Namespace;

1
tests/Basic/Basic.cs

@ -30,6 +30,7 @@ namespace CppSharp.Tests
driver.AddTranslationUnitPass(new CheckMacroPass()); driver.AddTranslationUnitPass(new CheckMacroPass());
ctx.SetClassAsValueType("Bar"); ctx.SetClassAsValueType("Bar");
ctx.SetClassAsValueType("Bar2"); ctx.SetClassAsValueType("Bar2");
ctx.IgnoreClassWithName("IgnoredType");
} }
public static void Main(string[] args) public static void Main(string[] args)

12
tests/Basic/Basic.h

@ -1,5 +1,16 @@
#include "../Tests.h" #include "../Tests.h"
class DLL_API IgnoredType
{
class IgnoredNested
{
private:
int i;
};
private:
int i;
};
class DLL_API Foo class DLL_API Foo
{ {
public: public:
@ -7,6 +18,7 @@ public:
Foo(); Foo();
int A; int A;
float B; float B;
IgnoredType ignoredType;
const char* GetANSI(); const char* GetANSI();
// TODO: VC++ does not support char16 // TODO: VC++ does not support char16

Loading…
Cancel
Save