@@ -162,7 +162,7 @@ public virtual void GenerateClassConstructors(Class @class)
162162 return ;
163163
164164 // Output a default constructor that takes the native instance.
165- GenerateClassConstructor ( @class ) ;
165+ GenerateClassConstructor ( @class , withOwnNativeInstanceParam : true ) ;
166166
167167 if ( @class . IsRefType )
168168 {
@@ -290,15 +290,26 @@ public override bool VisitVariableDecl(Variable variable)
290290 return true ;
291291 }
292292
293- public virtual string ClassCtorInstanceParamIdentifier => "instance" ;
294-
295- public virtual void GenerateClassConstructor ( Class @class )
293+ public virtual void GenerateClassConstructor ( Class @class , bool withOwnNativeInstanceParam = false )
296294 {
297295 Write ( $ "{ QualifiedIdentifier ( @class ) } ::{ @class . Name } (") ;
298296
299297 var nativeType = $ "::{ @class . QualifiedOriginalName } *";
300- WriteLine ( $ "{ nativeType } { ClassCtorInstanceParamIdentifier } )") ;
301- GenerateClassConstructorBase ( @class ) ;
298+ //WriteLine($"{nativeType} {ClassCtorInstanceParamIdentifier})");
299+ WriteLine ( ! withOwnNativeInstanceParam ? $ "{ nativeType } { ClassCtorInstanceParamIdentifier } )" :
300+ $ "{ nativeType } { ClassCtorInstanceParamIdentifier } , bool ownNativeInstance)") ;
301+
302+ var hasBase = GenerateClassConstructorBase ( @class , null , withOwnNativeInstanceParam ) ;
303+
304+ if ( CLIGenerator . ShouldGenerateClassNativeField ( @class ) )
305+ {
306+ Indent ( ) ;
307+ Write ( hasBase ? "," : ":" ) ;
308+ Unindent ( ) ;
309+
310+ WriteLine ( ! withOwnNativeInstanceParam ? " {0}(false)" : " {0}(ownNativeInstance)" ,
311+ Helpers . OwnsNativeInstanceIdentifier ) ;
312+ }
302313
303314 WriteOpenBraceAndIndent ( ) ;
304315
@@ -312,30 +323,29 @@ public virtual void GenerateClassConstructor(Class @class)
312323 NewLine ( ) ;
313324 }
314325
315- private bool GenerateClassConstructorBase ( Class @class , Method method = null )
326+ private bool GenerateClassConstructorBase ( Class @class , Method method = null ,
327+ bool withOwnNativeInstanceParam = false )
316328 {
317- var hasBase = @class . HasBase && @class . Bases [ 0 ] . IsClass && @class . Bases [ 0 ] . Class . IsGenerated ;
318- if ( ! hasBase )
329+ if ( @class . IsValueType )
330+ return true ;
331+
332+ if ( ! @class . NeedsBase )
319333 return false ;
320334
321- if ( ! @class . IsValueType )
322- {
323- Indent ( ) ;
335+ Indent ( ) ;
324336
325- var baseClass = @class . Bases [ 0 ] . Class ;
326- Write ( $ ": { QualifiedIdentifier ( baseClass ) } (") ;
337+ Write ( $ ": { QualifiedIdentifier ( @class . BaseClass ) } (") ;
327338
328- // We cast the value to the base class type since otherwise there
329- // could be ambiguous call to overloaded constructors.
330- CTypePrinter . PushContext ( TypePrinterContextKind . Native ) ;
331- var nativeTypeName = baseClass . Visit ( CTypePrinter ) ;
332- CTypePrinter . PopContext ( ) ;
333- Write ( $ "({ nativeTypeName } *)") ;
339+ // We cast the value to the base class type since otherwise there
340+ // could be ambiguous call to overloaded constructors.
341+ var cppTypePrinter = new CppTypePrinter ( Context ) ;
342+ var nativeTypeName = @class . BaseClass . Visit ( cppTypePrinter ) ;
334343
335- WriteLine ( "{0})" , method != null ? "nullptr" : ClassCtorInstanceParamIdentifier ) ;
344+ Write ( $ "({ nativeTypeName } *)") ;
345+ WriteLine ( "{0}{1})" , method != null ? "nullptr" : ClassCtorInstanceParamIdentifier ,
346+ ! withOwnNativeInstanceParam ? "" : ", ownNativeInstance" ) ;
336347
337- Unindent ( ) ;
338- }
348+ Unindent ( ) ;
339349
340350 return true ;
341351 }
0 commit comments