Browse Source

Merge pull request #350 from ddobrev/master

Fixed some problems with arrays of void after mapping void* to IntPtr
pull/352/head
João Matos 11 years ago
parent
commit
0846bbe735
  1. 9
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  2. 17
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  3. 4
      src/Generator/Passes/HandleDefaultParamValuesPass.cs
  4. 2
      tests/CSharpTemp/CSharpTemp.Tests.cs
  5. 6
      tests/CSharpTemp/CSharpTemp.cpp
  6. 8
      tests/CSharpTemp/CSharpTemp.h

9
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -121,6 +121,10 @@ namespace CppSharp.Generators.CSharp
supportBefore.WriteStartBraceIndent(); supportBefore.WriteStartBraceIndent();
supportBefore.WriteLine("{0} = new {1}[{2}];", value, array.Type, array.Size); supportBefore.WriteLine("{0} = new {1}[{2}];", value, array.Type, array.Size);
supportBefore.WriteLine("for (int i = 0; i < {0}; i++)", array.Size); supportBefore.WriteLine("for (int i = 0; i < {0}; i++)", array.Size);
if (array.Type.IsPointerToPrimitiveType(PrimitiveType.Void))
supportBefore.WriteLineIndent("{0}[i] = new global::System.IntPtr({1}[i]);",
value, Context.ReturnVarName);
else
supportBefore.WriteLineIndent("{0}[i] = {1}[i];", value, Context.ReturnVarName); supportBefore.WriteLineIndent("{0}[i] = {1}[i];", value, Context.ReturnVarName);
supportBefore.WriteCloseBraceIndent(); supportBefore.WriteCloseBraceIndent();
Context.Return.Write(value); Context.Return.Write(value);
@ -352,8 +356,9 @@ namespace CppSharp.Generators.CSharp
supportBefore.WriteLine("if ({0} != null)", Context.ArgName); supportBefore.WriteLine("if ({0} != null)", Context.ArgName);
supportBefore.WriteStartBraceIndent(); supportBefore.WriteStartBraceIndent();
supportBefore.WriteLine("for (int i = 0; i < {0}; i++)", array.Size); supportBefore.WriteLine("for (int i = 0; i < {0}; i++)", array.Size);
supportBefore.WriteLineIndent("{0}[i] = {1}[i];", supportBefore.WriteLineIndent("{0}[i] = {1}[i]{2};",
Context.ReturnVarName, Context.ArgName); Context.ReturnVarName, Context.ArgName,
array.Type.IsPointerToPrimitiveType(PrimitiveType.Void) ? ".ToPointer()" : string.Empty);
supportBefore.WriteCloseBraceIndent(); supportBefore.WriteCloseBraceIndent();
break; break;
default: default:

17
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -116,12 +116,15 @@ namespace CppSharp.Generators.CSharp
if (ContextKind == CSharpTypePrinterContextKind.Native && if (ContextKind == CSharpTypePrinterContextKind.Native &&
array.SizeType == ArrayType.ArraySize.Constant) array.SizeType == ArrayType.ArraySize.Constant)
{ {
if (array.Type.Desugar().IsPointerToPrimitiveType()) Type arrayType = array.Type.Desugar();
PrimitiveType primitiveType;
if (arrayType.IsPointerToPrimitiveType(out primitiveType))
{ {
return new CSharpTypePrinterResult if (primitiveType == PrimitiveType.Void)
{ {
Type = string.Format("{0}*", array.Type.Visit(this, quals)) return "void**";
}; }
return string.Format("{0}*", array.Type.Visit(this, quals));
} }
// Do not write the fixed keyword multiple times for nested array types // Do not write the fixed keyword multiple times for nested array types
var fixedKeyword = array.Type is ArrayType ? string.Empty : "fixed "; var fixedKeyword = array.Type is ArrayType ? string.Empty : "fixed ";
@ -217,14 +220,16 @@ namespace CppSharp.Generators.CSharp
{ {
// Skip one indirection if passed by reference // Skip one indirection if passed by reference
var param = Context.Parameter; var param = Context.Parameter;
if (isManagedContext && param != null && (param.IsOut || param.IsInOut)) bool isRefParam = param != null && (param.IsOut || param.IsInOut);
if (isManagedContext && isRefParam)
return pointee.Visit(this, quals); return pointee.Visit(this, quals);
if (ContextKind == CSharpTypePrinterContextKind.GenericDelegate || if (ContextKind == CSharpTypePrinterContextKind.GenericDelegate ||
pointee.IsPrimitiveType(PrimitiveType.Void)) pointee.IsPrimitiveType(PrimitiveType.Void))
return "global::System.IntPtr"; return "global::System.IntPtr";
return pointee.Visit(this, quals) + "*"; var result = pointee.Visit(this, quals);
return !isRefParam && result.Type == "global::System.IntPtr" ? "void**" : result + "*";
} }
Enumeration @enum; Enumeration @enum;

4
src/Generator/Passes/HandleDefaultParamValuesPass.cs

@ -57,7 +57,9 @@ namespace CppSharp.Passes
{ {
if (desugared.IsPointer()) if (desugared.IsPointer())
{ {
parameter.DefaultArgument.String = "null"; // IntPtr.Zero is not a constant
parameter.DefaultArgument.String = desugared.IsPointerToPrimitiveType(PrimitiveType.Void) ?
"new global::System.IntPtr()" : "null";
return true; return true;
} }
return false; return false;

2
tests/CSharpTemp/CSharpTemp.Tests.cs

@ -133,6 +133,8 @@ public class CSharpTempTests : GeneratorTestFixture
methodsWithDefaultValues.DefaultChar(); methodsWithDefaultValues.DefaultChar();
methodsWithDefaultValues.DefaultEmptyChar(); methodsWithDefaultValues.DefaultEmptyChar();
methodsWithDefaultValues.DefaultPointer(); methodsWithDefaultValues.DefaultPointer();
methodsWithDefaultValues.DefaultVoidStar();
methodsWithDefaultValues.DefaultValueType();
methodsWithDefaultValues.DefaultRefTypeAfterOthers(); methodsWithDefaultValues.DefaultRefTypeAfterOthers();
methodsWithDefaultValues.DefaultRefTypeBeforeAndAfterOthers(5, new Foo()); methodsWithDefaultValues.DefaultRefTypeBeforeAndAfterOthers(5, new Foo());
methodsWithDefaultValues.DefaultRefTypeBeforeOthers(); methodsWithDefaultValues.DefaultRefTypeBeforeOthers();

6
tests/CSharpTemp/CSharpTemp.cpp

@ -245,7 +245,11 @@ void MethodsWithDefaultValues::defaultPointer(Foo *ptr)
{ {
} }
void MethodsWithDefaultValues::defaultValueType(ValueType bar) void MethodsWithDefaultValues::defaultVoidStar(void* ptr)
{
}
void MethodsWithDefaultValues::defaultValueType(QGenericArgument valueType)
{ {
} }

8
tests/CSharpTemp/CSharpTemp.h

@ -183,10 +183,6 @@ public:
void Name(); void Name();
}; };
struct DLL_API ValueType
{
};
enum class Flags enum class Flags
{ {
Flag1 = 1, Flag1 = 1,
@ -209,6 +205,7 @@ struct QGenericArgument
{ {
public: public:
QGenericArgument(const char* name = 0); QGenericArgument(const char* name = 0);
void* fixedArrayInValueType[1];
private: private:
const char* _name; const char* _name;
}; };
@ -218,7 +215,8 @@ class DLL_API MethodsWithDefaultValues
public: public:
MethodsWithDefaultValues(Foo foo = Foo()); MethodsWithDefaultValues(Foo foo = Foo());
void defaultPointer(Foo* ptr = 0); void defaultPointer(Foo* ptr = 0);
void defaultValueType(ValueType bar = ValueType()); void defaultVoidStar(void* ptr = 0);
void defaultValueType(QGenericArgument valueType = QGenericArgument());
void defaultChar(char c = 'a'); void defaultChar(char c = 'a');
void defaultEmptyChar(char c = 0); void defaultEmptyChar(char c = 0);
void defaultRefTypeBeforeOthers(Foo foo = Foo(), int i = 5, Bar::Items item = Bar::Item2); void defaultRefTypeBeforeOthers(Foo foo = Foo(), int i = 5, Bar::Items item = Bar::Item2);

Loading…
Cancel
Save