@@ -82,59 +82,50 @@ public override bool VisitArrayType(ArrayType array, TypeQualifiers quals)
8282 switch ( array . SizeType )
8383 {
8484 case ArrayType . ArraySize . Constant :
85- if ( Context . MarshalKind == MarshalKind . NativeField ||
86- Context . MarshalKind == MarshalKind . ReturnVariableArray )
85+ if ( Context . MarshalKind != MarshalKind . NativeField &&
86+ Context . MarshalKind != MarshalKind . ReturnVariableArray )
87+ goto case ArrayType . ArraySize . Incomplete ;
88+
89+ var supportBefore = Context . Before ;
90+ string value = Generator . GeneratedIdentifier ( "value" ) ;
91+ var arrayType = array . Type . Desugar ( ) ;
92+ supportBefore . WriteLine ( $ "{ arrayType } [] { value } = null;") ;
93+ supportBefore . WriteLine ( $ "if ({ Context . ReturnVarName } != null)") ;
94+ supportBefore . WriteStartBraceIndent ( ) ;
95+ supportBefore . WriteLine ( $ "{ value } = new { arrayType } [{ array . Size } ];") ;
96+ supportBefore . WriteLine ( $ "for (int i = 0; i < { array . Size } ; i++)") ;
97+ if ( array . Type . IsPointerToPrimitiveType ( PrimitiveType . Void ) )
98+ supportBefore . WriteLineIndent ( $@ "{ value } [i] = new global::System.IntPtr({
99+ Context . ReturnVarName } [i]);" ) ;
100+ else
87101 {
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 ) ;
102+ var finalArrayType = arrayType . GetPointee ( ) ?? arrayType ;
103+ Class @class ;
104+ if ( ( finalArrayType . TryGetClass ( out @class ) ) && @class . IsRefType )
105+ {
106+ if ( arrayType == finalArrayType )
107+ supportBefore . WriteLineIndent (
108+ "{0}[i] = {1}.{2}(*(({1}.{3}*)&({4}[i * sizeof({1}.{3})])));" ,
109+ value , array . Type , Helpers . CreateInstanceIdentifier ,
110+ Helpers . InternalStruct , Context . ReturnVarName ) ;
111+ else
112+ supportBefore . WriteLineIndent (
113+ $@ "{ value } [i] = { finalArrayType } .{ Helpers . CreateInstanceIdentifier } (({
114+ CSharpTypePrinter . IntPtrType } ) { Context . ReturnVarName } [i]);" ) ;
115+ }
98116 else
99117 {
100- var arrayType = array . Type . Desugar ( ) ;
101- var finalArrayType = arrayType . GetPointee ( ) ?? arrayType ;
102- Class @class ;
103- if ( ( finalArrayType . TryGetClass ( out @class ) ) && @class . IsRefType )
104- {
105- if ( arrayType == finalArrayType )
106- supportBefore . WriteLineIndent (
107- "{0}[i] = {1}.{2}(*(({1}.{3}*)&({4}[i * sizeof({1}.{3})])));" ,
108- value , array . Type , Helpers . CreateInstanceIdentifier ,
109- Helpers . InternalStruct , Context . ReturnVarName ) ;
110- else
111- supportBefore . WriteLineIndent (
112- $@ "{ value } [i] = { finalArrayType } .{ Helpers . CreateInstanceIdentifier } (({
113- CSharpTypePrinter . IntPtrType } ) { Context . ReturnVarName } [i]);" ) ;
114- }
118+ if ( arrayType . IsPrimitiveType ( PrimitiveType . Char ) &&
119+ Context . Context . Options . MarshalCharAsManagedChar )
120+ supportBefore . WriteLineIndent ( $@ "{ value } [i] = global::System.Convert.ToChar({
121+ Context . ReturnVarName } [i]);" ) ;
115122 else
116- {
117- if ( arrayType . IsPrimitiveType ( PrimitiveType . Char ) &&
118- Context . Context . Options . MarshalCharAsManagedChar )
119- {
120- supportBefore . WriteLineIndent (
121- "{0}[i] = global::System.Convert.ToChar({1}[i]);" ,
122- value , Context . ReturnVarName ) ;
123- }
124- else
125- {
126- supportBefore . WriteLineIndent ( "{0}[i] = {1}[i];" ,
127- value , Context . ReturnVarName ) ;
128- }
129- }
123+ supportBefore . WriteLineIndent ( $@ "{ value } [i] = {
124+ Context . ReturnVarName } [i];" ) ;
130125 }
131- supportBefore . WriteCloseBraceIndent ( ) ;
132- Context . Return . Write ( value ) ;
133- }
134- else
135- {
136- goto case ArrayType . ArraySize . Incomplete ;
137126 }
127+ supportBefore . WriteCloseBraceIndent ( ) ;
128+ Context . Return . Write ( value ) ;
138129 break ;
139130 case ArrayType . ArraySize . Incomplete :
140131 // const char* and const char[] are the same so we can use a string
@@ -495,56 +486,46 @@ public override bool VisitArrayType(ArrayType array, TypeQualifiers quals)
495486 {
496487 case ArrayType . ArraySize . Constant :
497488 if ( string . IsNullOrEmpty ( Context . ReturnVarName ) )
498- {
499489 goto case ArrayType . ArraySize . Incomplete ;
490+
491+ var supportBefore = Context . Before ;
492+ supportBefore . WriteLine ( $ "if ({ Context . ArgName } != null)") ;
493+ supportBefore . WriteStartBraceIndent ( ) ;
494+ Class @class ;
495+ var arrayType = array . Type . Desugar ( ) ;
496+ var finalArrayType = arrayType . GetPointee ( ) ?? arrayType ;
497+ if ( finalArrayType . TryGetClass ( out @class ) && @class . IsRefType )
498+ {
499+ supportBefore . WriteLine ( $ "if (value.Length != { array . Size } )") ;
500+ ThrowArgumentOutOfRangeException ( ) ;
501+ }
502+ supportBefore . WriteLine ( $ "for (int i = 0; i < { array . Size } ; i++)") ;
503+ if ( @class != null && @class . IsRefType )
504+ {
505+ if ( finalArrayType == arrayType )
506+ supportBefore . WriteLineIndent (
507+ "*({1}.{2}*) &{0}[i * sizeof({1}.{2})] = *({1}.{2}*){3}[i].{4};" ,
508+ Context . ReturnVarName , arrayType , Helpers . InternalStruct ,
509+ Context . ArgName , Helpers . InstanceIdentifier ) ;
510+ else
511+ supportBefore . WriteLineIndent ( $@ "{ Context . ReturnVarName } [i] = ({
512+ ( Context . Context . TargetInfo . PointerWidth == 64 ? "long" : "int" ) } ) {
513+ Context . ArgName } [i].{ Helpers . InstanceIdentifier } ;" ) ;
500514 }
501515 else
502516 {
503- var supportBefore = Context . Before ;
504- supportBefore . WriteLine ( "if ({0} != null)" , Context . ArgName ) ;
505- supportBefore . WriteStartBraceIndent ( ) ;
506- Class @class ;
507- var arrayType = array . Type . Desugar ( ) ;
508- var finalArrayType = arrayType . GetPointee ( ) ?? arrayType ;
509- if ( finalArrayType . TryGetClass ( out @class ) && @class . IsRefType )
510- {
511- supportBefore . WriteLine ( "if (value.Length != {0})" , array . Size ) ;
512- ThrowArgumentOutOfRangeException ( ) ;
513- }
514- supportBefore . WriteLine ( "for (int i = 0; i < {0}; i++)" , array . Size ) ;
515- if ( @class != null && @class . IsRefType )
516- {
517- if ( finalArrayType == arrayType )
518- supportBefore . WriteLineIndent (
519- "*({1}.{2}*) &{0}[i * sizeof({1}.{2})] = *({1}.{2}*){3}[i].{4};" ,
520- Context . ReturnVarName , arrayType , Helpers . InternalStruct ,
521- Context . ArgName , Helpers . InstanceIdentifier ) ;
522- else
523- supportBefore . WriteLineIndent ( $@ "{ Context . ReturnVarName } [i] = ({
524- ( Context . Context . TargetInfo . PointerWidth == 64 ? "long" : "int" ) } ) {
525- Context . ArgName } [i].{ Helpers . InstanceIdentifier } ;" ) ;
526- }
517+ if ( arrayType . IsPrimitiveType ( PrimitiveType . Char ) &&
518+ Context . Context . Options . MarshalCharAsManagedChar )
519+ supportBefore . WriteLineIndent ( $@ "{
520+ Context . ReturnVarName } [i] = global::System.Convert.ToSByte({
521+ Context . ArgName } [i]);" ) ;
527522 else
528- {
529- if ( arrayType . IsPrimitiveType ( PrimitiveType . Char ) &&
530- Context . Context . Options . MarshalCharAsManagedChar )
531- {
532- supportBefore . WriteLineIndent (
533- "{0}[i] = global::System.Convert.ToSByte({1}[i]);" ,
534- Context . ReturnVarName , Context . ArgName ) ;
535- }
536- else
537- {
538- supportBefore . WriteLineIndent ( "{0}[i] = {1}[i]{2};" ,
539- Context . ReturnVarName ,
540- Context . ArgName ,
541- arrayType . IsPointerToPrimitiveType ( PrimitiveType . Void )
542- ? ".ToPointer()"
543- : string . Empty ) ;
544- }
545- }
546- supportBefore . WriteCloseBraceIndent ( ) ;
523+ supportBefore . WriteLineIndent ( $@ "{ Context . ReturnVarName } [i] = {
524+ Context . ArgName } [i]{
525+ ( arrayType . IsPointerToPrimitiveType ( PrimitiveType . Void ) ?
526+ ".ToPointer()" : string . Empty ) } ;" ) ;
547527 }
528+ supportBefore . WriteCloseBraceIndent ( ) ;
548529 break ;
549530 case ArrayType . ArraySize . Incomplete :
550531 MarshalArray ( array ) ;
0 commit comments