@@ -82,43 +82,51 @@ public override bool VisitArrayType(ArrayType array, TypeQualifiers quals)
8282 switch ( array . SizeType )
8383 {
8484 case ArrayType . ArraySize . Constant :
85- var supportBefore = Context . Before ;
86- string value = Generator . GeneratedIdentifier ( "value" ) ;
87- supportBefore . WriteLine ( "{0}[] {1} = null;" , array . Type , value , array . Size ) ;
88- supportBefore . WriteLine ( "if ({0} != null)" , Context . ReturnVarName ) ;
89- supportBefore . WriteStartBraceIndent ( ) ;
90- supportBefore . WriteLine ( "{0} = new {1}[{2}];" , value , array . Type , array . Size ) ;
91- supportBefore . WriteLine ( "for (int i = 0; i < {0}; i++)" , array . Size ) ;
92- if ( array . Type . IsPointerToPrimitiveType ( PrimitiveType . Void ) )
93- supportBefore . WriteLineIndent ( "{0}[i] = new global::System.IntPtr({1}[i]);" ,
94- value , Context . ReturnVarName ) ;
95- else
85+ if ( Context . MarshalKind == MarshalKind . NativeField ||
86+ Context . MarshalKind == MarshalKind . ReturnVariableArray )
9687 {
97- var arrayType = array . Type . Desugar ( ) ;
98- Class @class ;
99- if ( arrayType . TryGetClass ( out @class ) && @class . IsRefType )
100- supportBefore . WriteLineIndent (
101- "{0}[i] = {1}.{2}(*(({1}.{3}*)&({4}[i * sizeof({1}.{3})])));" ,
102- value , array . Type , Helpers . CreateInstanceIdentifier ,
103- Helpers . InternalStruct , Context . ReturnVarName ) ;
88+ var supportBefore = Context . Before ;
89+ string value = Generator . GeneratedIdentifier ( "value" ) ;
90+ supportBefore . WriteLine ( "{0}[] {1} = null;" , array . Type , value , array . Size ) ;
91+ supportBefore . WriteLine ( "if ({0} != null)" , Context . ReturnVarName ) ;
92+ supportBefore . WriteStartBraceIndent ( ) ;
93+ supportBefore . WriteLine ( "{0} = new {1}[{2}];" , value , array . Type , array . Size ) ;
94+ supportBefore . WriteLine ( "for (int i = 0; i < {0}; i++)" , array . Size ) ;
95+ if ( array . Type . IsPointerToPrimitiveType ( PrimitiveType . Void ) )
96+ supportBefore . WriteLineIndent ( "{0}[i] = new global::System.IntPtr({1}[i]);" ,
97+ value , Context . ReturnVarName ) ;
10498 else
10599 {
106- if ( arrayType . IsPrimitiveType ( PrimitiveType . Char ) &&
107- Context . Context . Options . MarshalCharAsManagedChar )
108- {
100+ var arrayType = array . Type . Desugar ( ) ;
101+ Class @class ;
102+ if ( arrayType . TryGetClass ( out @class ) && @class . IsRefType )
109103 supportBefore . WriteLineIndent (
110- "{0}[i] = global::System.Convert.ToChar( {1}[i] );" ,
111- value , Context . ReturnVarName ) ;
112- }
104+ "{0}[i] = {1}.{2}(*(( {1}.{3}*)&({4}[i * sizeof({1}.{3})])) );" ,
105+ value , array . Type , Helpers . CreateInstanceIdentifier ,
106+ Helpers . InternalStruct , Context . ReturnVarName ) ;
113107 else
114108 {
115- supportBefore . WriteLineIndent ( "{0}[i] = {1}[i];" ,
116- value , Context . ReturnVarName ) ;
109+ if ( arrayType . IsPrimitiveType ( PrimitiveType . Char ) &&
110+ Context . Context . Options . MarshalCharAsManagedChar )
111+ {
112+ supportBefore . WriteLineIndent (
113+ "{0}[i] = global::System.Convert.ToChar({1}[i]);" ,
114+ value , Context . ReturnVarName ) ;
115+ }
116+ else
117+ {
118+ supportBefore . WriteLineIndent ( "{0}[i] = {1}[i];" ,
119+ value , Context . ReturnVarName ) ;
120+ }
117121 }
118122 }
123+ supportBefore . WriteCloseBraceIndent ( ) ;
124+ Context . Return . Write ( value ) ;
125+ }
126+ else
127+ {
128+ goto case ArrayType . ArraySize . Incomplete ;
119129 }
120- supportBefore . WriteCloseBraceIndent ( ) ;
121- Context . Return . Write ( value ) ;
122130 break ;
123131 case ArrayType . ArraySize . Incomplete :
124132 // const char* and const char[] are the same so we can use a string
@@ -128,7 +136,7 @@ public override bool VisitArrayType(ArrayType array, TypeQualifiers quals)
128136 {
129137 QualifiedPointee = array . QualifiedType
130138 } , quals ) ;
131- MarshalVariableArray ( array ) ;
139+ MarshalArray ( array ) ;
132140 break ;
133141 case ArrayType . ArraySize . Variable :
134142 Context . Return . Write ( Context . ReturnVarName ) ;
@@ -384,11 +392,11 @@ private string HandleReturnedPointer(Class @class, string qualifiedClass)
384392 return ret ;
385393 }
386394
387- private void MarshalVariableArray ( ArrayType array )
395+ private void MarshalArray ( ArrayType array )
388396 {
389397 Type arrayType = array . Type . Desugar ( ) ;
390398 if ( arrayType . IsPrimitiveType ( ) ||
391- arrayType . IsPointerToPrimitiveType ( PrimitiveType . Char ) ||
399+ arrayType . IsPointerToPrimitiveType ( ) ||
392400 Context . MarshalKind != MarshalKind . GenericDelegate )
393401 {
394402 Context . Return . Write ( Context . ReturnVarName ) ;
0 commit comments