Browse Source

Use fixed c# statement when accessing fixed struct members

pull/821/head
Gilad Levi 8 years ago
parent
commit
5302f5870c
  1. 24
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 2
      tests/CSharp/CSharp.cs
  3. 8
      tests/CSharp/CSharp.h

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)
{ {
@ -1077,7 +1077,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);
@ -1284,7 +1284,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();
@ -1423,7 +1423,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(
@ -1565,7 +1565,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
{ {
@ -2156,7 +2156,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
@ -2277,7 +2277,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));
} }
@ -2361,7 +2361,7 @@ namespace CppSharp.Generators.CSharp
} }
else else
{ {
WriteLine("return {0}.GetHashCode();", Helpers.InstanceIdentifier); WriteLine("return {0}.GetHashCode();", Helpers.InstanceIdentifier);
} }
WriteCloseBraceIndent(); WriteCloseBraceIndent();
} }
@ -2729,7 +2729,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
tests/CSharp/CSharp.cs

@ -33,7 +33,7 @@ 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");
} }

8
tests/CSharp/CSharp.h

@ -1169,4 +1169,10 @@ 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;
};
Loading…
Cancel
Save