@@ -98,12 +98,20 @@ public override bool VisitArrayType(ArrayType array, TypeQualifiers quals)
9898 else
9999 {
100100 var arrayType = array . Type . Desugar ( ) ;
101+ var finalArrayType = arrayType . GetPointee ( ) ?? arrayType ;
101102 Class @class ;
102- if ( arrayType . TryGetClass ( out @class ) && @class . IsRefType )
103- supportBefore . WriteLineIndent (
104- "{0}[i] = {1}.{2}(*(({1}.{3}*)&({4}[i * sizeof({1}.{3})])));" ,
105- value , array . Type , Helpers . CreateInstanceIdentifier ,
106- Helpers . InternalStruct , Context . ReturnVarName ) ;
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+ }
107115 else
108116 {
109117 if ( arrayType . IsPrimitiveType ( PrimitiveType . Char ) &&
@@ -483,7 +491,6 @@ public override bool VisitArrayType(ArrayType array, TypeQualifiers quals)
483491 if ( ! VisitType ( array , quals ) )
484492 return false ;
485493
486- var arrayType = array . Type . Desugar ( ) ;
487494 switch ( array . SizeType )
488495 {
489496 case ArrayType . ArraySize . Constant :
@@ -497,18 +504,25 @@ public override bool VisitArrayType(ArrayType array, TypeQualifiers quals)
497504 supportBefore . WriteLine ( "if ({0} != null)" , Context . ArgName ) ;
498505 supportBefore . WriteStartBraceIndent ( ) ;
499506 Class @class ;
500- if ( arrayType . TryGetClass ( out @class ) && @class . IsRefType )
507+ var arrayType = array . Type . Desugar ( ) ;
508+ var finalArrayType = arrayType . GetPointee ( ) ?? arrayType ;
509+ if ( finalArrayType . TryGetClass ( out @class ) && @class . IsRefType )
501510 {
502511 supportBefore . WriteLine ( "if (value.Length != {0})" , array . Size ) ;
503512 ThrowArgumentOutOfRangeException ( ) ;
504513 }
505514 supportBefore . WriteLine ( "for (int i = 0; i < {0}; i++)" , array . Size ) ;
506515 if ( @class != null && @class . IsRefType )
507516 {
508- supportBefore . WriteLineIndent (
509- "*({1}.{2}*) &{0}[i * sizeof({1}.{2})] = *({1}.{2}*){3}[i].{4};" ,
510- Context . ReturnVarName , arrayType , Helpers . InternalStruct ,
511- Context . ArgName , Helpers . InstanceIdentifier ) ;
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 } ;" ) ;
512526 }
513527 else
514528 {
0 commit comments