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 @@ -31,7 +31,7 @@ namespace CppSharp.AST
public enum GenerationKind
{
/// <summary>
// Declaration is not generated.
/// Declaration is not generated.
/// </summary>
None,
/// <summary>
@ -197,7 +197,8 @@ namespace CppSharp.AST @@ -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;
}

19
src/AST/TypeExtensions.cs

@ -112,27 +112,32 @@ @@ -112,27 +112,32 @@
}
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();
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;
}

2
src/Generator/AST/Utils.cs

@ -62,7 +62,7 @@ namespace CppSharp.AST @@ -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)

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

@ -316,7 +316,7 @@ namespace CppSharp.Generators.CSharp @@ -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 @@ -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 @@ -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 @@ -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);

2
src/Generator/Library.cs

@ -184,7 +184,7 @@ namespace CppSharp @@ -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)

12
src/Generator/Passes/CheckIgnoredDecls.cs

@ -41,13 +41,13 @@ namespace CppSharp.Passes @@ -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 @@ -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;

1
tests/Basic/Basic.cs

@ -30,6 +30,7 @@ namespace CppSharp.Tests @@ -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)

12
tests/Basic/Basic.h

@ -1,5 +1,16 @@ @@ -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: @@ -7,6 +18,7 @@ public:
Foo();
int A;
float B;
IgnoredType ignoredType;
const char* GetANSI();
// TODO: VC++ does not support char16

Loading…
Cancel
Save