@@ -146,13 +146,25 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
146146
147147 var pointee = pointer . Pointee . Desugar ( ) ;
148148 var finalPointee = pointer . GetFinalPointee ( ) . Desugar ( ) ;
149- var type = Context . ReturnType . Type . Desugar (
149+ var returnType = Context . ReturnType . Type . Desugar (
150150 resolveTemplateSubstitution : false ) ;
151- PrimitiveType primitive ;
152- if ( ( pointee . IsConstCharString ( ) && ( isRefParam || type . IsReference ( ) ) ) ||
153- ( ! finalPointee . IsPrimitiveType ( out primitive ) &&
151+ if ( ( pointee . IsConstCharString ( ) && ( isRefParam || returnType . IsReference ( ) ) ) ||
152+ ( ! finalPointee . IsPrimitiveType ( out PrimitiveType primitive ) &&
154153 ! finalPointee . IsEnumType ( ) ) )
154+ {
155+ if ( Context . MarshalKind != MarshalKind . NativeField &&
156+ pointee . IsPointerTo ( out Type type ) &&
157+ type . Desugar ( ) . TryGetClass ( out Class c ) )
158+ {
159+ string ret = Generator . GeneratedIdentifier ( Context . ReturnVarName ) ;
160+ Context . Before . WriteLine ( $@ "{ typePrinter . IntPtrType } { ret } = {
161+ Context . ReturnVarName } == { typePrinter . IntPtrType } .Zero ? {
162+ typePrinter . IntPtrType } .Zero : new {
163+ typePrinter . IntPtrType } (*(void**) { Context . ReturnVarName } );" ) ;
164+ Context . ReturnVarName = ret ;
165+ }
155166 return pointer . QualifiedPointee . Visit ( this ) ;
167+ }
156168
157169 if ( isRefParam )
158170 {
@@ -167,7 +179,7 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
167179 if ( Context . Function != null &&
168180 Context . Function . OperatorKind == CXXOperatorKind . Subscript )
169181 {
170- if ( type . IsPrimitiveType ( primitive ) )
182+ if ( returnType . IsPrimitiveType ( primitive ) )
171183 {
172184 Context . Return . Write ( "*" ) ;
173185 }
@@ -517,7 +529,7 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
517529 if ( param . IsOut )
518530 {
519531 MarshalString ( pointee ) ;
520- Context . Return . Write ( "IntPtr .Zero") ;
532+ Context . Return . Write ( $ " { typePrinter . IntPtrType } .Zero") ;
521533 Context . ArgumentPrefix . Write ( "&" ) ;
522534 return true ;
523535 }
@@ -604,7 +616,7 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
604616 arg , Context . Parameter . Name , Helpers . InstanceIdentifier ) ;
605617 }
606618
607- Context . Return . Write ( $ "new global::System.IntPtr (&{ arg } )") ;
619+ Context . Return . Write ( $ "new { typePrinter . IntPtrType } (&{ arg } )") ;
608620 return true ;
609621 }
610622
@@ -614,7 +626,7 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
614626 pointer . QualifiedPointee . Visit ( this ) ;
615627 Context . Before . WriteLine ( $ "var { arg } = { Context . Return } ;") ;
616628 Context . Return . StringBuilder . Clear ( ) ;
617- Context . Return . Write ( $ "new global::System.IntPtr (&{ arg } )") ;
629+ Context . Return . Write ( $ "new { typePrinter . IntPtrType } (&{ arg } )") ;
618630 return true ;
619631 }
620632
0 commit comments