Browse Source

Merge branch 'master' of https://github.com/giladlevi/CppSharp

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/829/head
Dimitar Dobrev 9 years ago
parent
commit
0b858668d2
  1. 1
      build/Tests.lua
  2. 24
      src/Generator/Generators/CSharp/CSharpSources.cs
  3. 2
      src/Generator/Library.cs
  4. 16
      tests/CSharp/CSharp.Tests.cs
  5. 19
      tests/CSharp/CSharp.cs
  6. 11
      tests/CSharp/CSharp.h

1
build/Tests.lua

@ -55,6 +55,7 @@ function SetupTestGeneratorProject(name, depends)
dependson { name .. ".Native" } dependson { name .. ".Native" }
linktable = { linktable = {
"System",
"System.Core", "System.Core",
"CppSharp", "CppSharp",
"CppSharp.AST", "CppSharp.AST",

24
src/Generator/Generators/CSharp/CSharpSources.cs

@ -308,12 +308,12 @@ namespace CppSharp.Generators.CSharp
} }
PushBlock(BlockKind.Class); PushBlock(BlockKind.Class);
GenerateDeclarationCommon(@class); GenerateDeclarationCommon(@class);
GenerateClassSpecifier(@class); GenerateClassSpecifier(@class);
NewLine(); NewLine();
WriteStartBraceIndent(); WriteStartBraceIndent();
if (!@class.IsAbstractImpl) if (!@class.IsAbstractImpl)
GenerateClassInternals(@class); GenerateClassInternals(@class);
@ -569,7 +569,7 @@ namespace CppSharp.Generators.CSharp
// private classes must be visible to because the internal structs can be used in dependencies // private classes must be visible to because the internal structs can be used in dependencies
// the proper fix is InternalsVisibleTo // the proper fix is InternalsVisibleTo
var keywords = new List<string>(); var keywords = new List<string>();
keywords.Add(@class.Access == AccessSpecifier.Protected ? "protected internal" : "public"); keywords.Add(@class.Access == AccessSpecifier.Protected ? "protected internal" : "public");
keywords.Add("unsafe"); keywords.Add("unsafe");
@ -833,7 +833,7 @@ namespace CppSharp.Generators.CSharp
var marshal = new CSharpMarshalManagedToNativePrinter(ctx); var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
ctx.Declaration = field; ctx.Declaration = field;
var arrayType = field.Type as ArrayType; var arrayType = field.Type as ArrayType ?? field.QualifiedType.Type.Desugar() as ArrayType;
if (arrayType != null && @class.IsValueType) if (arrayType != null && @class.IsValueType)
{ {
@ -1078,7 +1078,7 @@ namespace CppSharp.Generators.CSharp
TypePrinter.PopContext(); TypePrinter.PopContext();
var arrayType = field.Type as ArrayType; var arrayType = field.Type as ArrayType ?? field.QualifiedType.Type.Desugar() as ArrayType;
if (arrayType != null && @class.IsValueType) if (arrayType != null && @class.IsValueType)
ctx.ReturnVarName = HandleValueArray(arrayType, field); ctx.ReturnVarName = HandleValueArray(arrayType, field);
@ -1285,7 +1285,7 @@ namespace CppSharp.Generators.CSharp
private void GenerateVariable(Class @class, Variable variable) private void GenerateVariable(Class @class, Variable variable)
{ {
PushBlock(BlockKind.Variable); PushBlock(BlockKind.Variable);
GenerateDeclarationCommon(variable); GenerateDeclarationCommon(variable);
WriteLine("public static {0} {1}", variable.Type, variable.Name); WriteLine("public static {0} {1}", variable.Type, variable.Name);
WriteStartBraceIndent(); WriteStartBraceIndent();
@ -1424,7 +1424,7 @@ namespace CppSharp.Generators.CSharp
if (Context.ParserOptions.IsMicrosoftAbi) if (Context.ParserOptions.IsMicrosoftAbi)
WriteLine("__OriginalVTables = new void*[] {{ {0} }};", WriteLine("__OriginalVTables = new void*[] {{ {0} }};",
string.Join(", ", string.Join(", ",
@class.Layout.VTablePointers.Select(v => @class.Layout.VTablePointers.Select(v =>
$"*(void**) ({Helpers.InstanceIdentifier} + {v.Offset})"))); $"*(void**) ({Helpers.InstanceIdentifier} + {v.Offset})")));
else else
WriteLine( WriteLine(
@ -1566,7 +1566,7 @@ namespace CppSharp.Generators.CSharp
if (method.IsGenerated) if (method.IsGenerated)
{ {
WriteLine("{0}.{1}({2});", Helpers.TargetIdentifier, WriteLine("{0}.{1}({2});", Helpers.TargetIdentifier,
method.Name, string.Join(", ", marshals)); method.Name, string.Join(", ", marshals));
} }
else else
{ {
@ -2157,7 +2157,7 @@ namespace CppSharp.Generators.CSharp
else if (method.ExplicitInterfaceImpl != null) else if (method.ExplicitInterfaceImpl != null)
Write("{0} {1}.{2}(", method.OriginalReturnType, Write("{0} {1}.{2}(", method.OriginalReturnType,
method.ExplicitInterfaceImpl.Name, functionName); method.ExplicitInterfaceImpl.Name, functionName);
else if (method.OperatorKind == CXXOperatorKind.Conversion || else if (method.OperatorKind == CXXOperatorKind.Conversion ||
method.OperatorKind == CXXOperatorKind.ExplicitConversion) method.OperatorKind == CXXOperatorKind.ExplicitConversion)
Write("{0} {1}(", functionName, method.OriginalReturnType); Write("{0} {1}(", functionName, method.OriginalReturnType);
else else
@ -2278,7 +2278,7 @@ namespace CppSharp.Generators.CSharp
Class @class; Class @class;
return p.Type.IsPointerToPrimitiveType() && p.Usage == ParameterUsage.InOut && p.HasDefaultValue return p.Type.IsPointerToPrimitiveType() && p.Usage == ParameterUsage.InOut && p.HasDefaultValue
? "ref param" + index++ ? "ref param" + index++
: (( p.Type.TryGetClass(out @class) && @class.IsInterface) ? "param" + index++ : (( p.Type.TryGetClass(out @class) && @class.IsInterface) ? "param" + index++
: ExpressionPrinter.VisitParameter(p)); : ExpressionPrinter.VisitParameter(p));
} }
@ -2362,7 +2362,7 @@ namespace CppSharp.Generators.CSharp
} }
else else
{ {
WriteLine("return {0}.GetHashCode();", Helpers.InstanceIdentifier); WriteLine("return {0}.GetHashCode();", Helpers.InstanceIdentifier);
} }
WriteCloseBraceIndent(); WriteCloseBraceIndent();
} }
@ -2733,7 +2733,7 @@ namespace CppSharp.Generators.CSharp
if (needsFixedThis && operatorParam == null) if (needsFixedThis && operatorParam == null)
WriteCloseBraceIndent(); WriteCloseBraceIndent();
var numFixedBlocks = @params.Count(param => param.HasUsingBlock); var numFixedBlocks = @params.Count(param => param.HasUsingBlock);
for(var i = 0; i < numFixedBlocks; ++i) for(var i = 0; i < numFixedBlocks; ++i)
WriteCloseBraceIndent(); WriteCloseBraceIndent();

2
src/Generator/Library.cs

@ -117,6 +117,8 @@ namespace CppSharp
static ulong ParseMacroExpression(string expression) static ulong ParseMacroExpression(string expression)
{ {
// TODO: Handle string expressions // TODO: Handle string expressions
if (expression.Length == 3 && expression[0] == '\'' && expression[2] == '\'') // '0' || 'A'
return expression[1]; // return the ASCI code of this character
long val; long val;
return ParseToNumber(expression, out val) ? (ulong)val : 0; return ParseToNumber(expression, out val) ? (ulong)val : 0;

16
tests/CSharp/CSharp.Tests.cs

@ -68,7 +68,7 @@ public unsafe class CSharpTests : GeneratorTestFixture
} }
#pragma warning restore 0168 #pragma warning restore 0168
#pragma warning restore 0219 #pragma warning restore 0219
} }
[Test] [Test]
@ -81,7 +81,7 @@ public unsafe class CSharpTests : GeneratorTestFixture
Assert.That(foo[0], Is.EqualTo(250)); Assert.That(foo[0], Is.EqualTo(250));
Assert.That(foo[(uint) 0], Is.EqualTo(15)); Assert.That(foo[(uint) 0], Is.EqualTo(15));
var bar = new Bar(); var bar = new Bar();
Assert.That(bar[0].A, Is.EqualTo(10)); Assert.That(bar[0].A, Is.EqualTo(10));
bar[0] = new Foo { A = 25 }; bar[0] = new Foo { A = 25 };
@ -399,7 +399,7 @@ public unsafe class CSharpTests : GeneratorTestFixture
Assert.AreEqual(100, p[0]); Assert.AreEqual(100, p[0]);
Assert.AreEqual(200, p[1]); Assert.AreEqual(200, p[1]);
Assert.AreEqual(300, p[2]); Assert.AreEqual(300, p[2]);
int[] array = { 1, 2, 3 }; int[] array = { 1, 2, 3 };
fixed (int* p1 = array) fixed (int* p1 = array)
{ {
@ -532,7 +532,7 @@ public unsafe class CSharpTests : GeneratorTestFixture
} }
Assert.IsTrue(VirtualDtorAddedInDerived.DtorCalled); Assert.IsTrue(VirtualDtorAddedInDerived.DtorCalled);
} }
[Test] [Test]
public void TestGetEnumFromNativePointer() public void TestGetEnumFromNativePointer()
{ {
@ -722,4 +722,12 @@ public unsafe class CSharpTests : GeneratorTestFixture
var i = CSharp.CSharp.UseDuplicateDeclaredStruct(duplicateDeclaredIncompleteStruct); var i = CSharp.CSharp.UseDuplicateDeclaredStruct(duplicateDeclaredIncompleteStruct);
Assert.AreEqual(10, i); Assert.AreEqual(10, i);
} }
[Test]
public void TestMyMacroTestEnum()
{
var a = (MyMacroTestEnum)'1';
var b = (MyMacroTestEnum)'2';
Assert.IsTrue(a == MyMacroTestEnum.MY_MACRO_TEST_1 && b == MyMacroTestEnum.MY_MACRO_TEST_2);
}
} }

19
tests/CSharp/CSharp.cs

@ -1,4 +1,7 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using CppSharp.AST; using CppSharp.AST;
using CppSharp.AST.Extensions; using CppSharp.AST.Extensions;
using CppSharp.Generators; using CppSharp.Generators;
@ -33,8 +36,22 @@ namespace CppSharp.Tests
ctx.SetClassAsValueType("QPoint"); ctx.SetClassAsValueType("QPoint");
ctx.SetClassAsValueType("QSize"); ctx.SetClassAsValueType("QSize");
ctx.SetClassAsValueType("QRect"); ctx.SetClassAsValueType("QRect");
ctx.SetClassAsValueType("StructTestArrayTypeFromTypedef");
ctx.IgnoreClassWithName("IgnoredTypeInheritingNonIgnoredWithNoEmptyCtor"); ctx.IgnoreClassWithName("IgnoredTypeInheritingNonIgnoredWithNoEmptyCtor");
var macroRegex = new Regex(@"(MY_MACRO_TEST_.*)");
List<string> list = new List<string>();
foreach (TranslationUnit unit in ctx.TranslationUnits)
{
if (unit.FilePath == "<invalid>" || unit.FileName == "CSharp.h")
foreach (var macro in unit.PreprocessedEntities.OfType<MacroDefinition>())
{
Match match = macroRegex.Match(macro.Name);
if (match.Success) list.Add(macro.Name);
}
}
var enumTest = ctx.GenerateEnumFromMacros("MyMacroTestEnum", list.ToArray());
enumTest.Namespace = new Namespace() {Name = "MacroTest"};
} }
public override void Postprocess(Driver driver, ASTContext ctx) public override void Postprocess(Driver driver, ASTContext ctx)

11
tests/CSharp/CSharp.h

@ -1169,4 +1169,13 @@ struct DLL_API ForwardDeclaredStruct {
}; };
DLL_API ForwardDeclaredStruct* createForwardDeclaredStruct(int i); DLL_API ForwardDeclaredStruct* createForwardDeclaredStruct(int i);
DLL_API int useForwardDeclaredStruct(ForwardDeclaredStruct* s); DLL_API int useForwardDeclaredStruct(ForwardDeclaredStruct* s);
typedef char charsArrayType[13];
struct StructTestArrayTypeFromTypedef
{
charsArrayType arr;
};
#define MY_MACRO_TEST_1 '1'
#define MY_MACRO_TEST_2 '2'

Loading…
Cancel
Save