Skip to content

Commit eba1b83

Browse files
committed
Rework constructor generation in C++ code generator.
1 parent 59bd475 commit eba1b83

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

src/Generator/Generators/C/CCodeGenerator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public CCodeGenerator(BindingContext context,
3737

3838
public override string FileExtension { get; } = "h";
3939

40+
public virtual string ClassCtorInstanceParamIdentifier => "instance";
41+
4042
public override void Process()
4143
{
4244

src/Generator/Generators/C/CppHeaders.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ public void GenerateClassConstructors(Class @class)
409409
var classNativeName = @class.Visit(CTypePrinter);
410410
CTypePrinter.PopContext();
411411

412-
WriteLine($"{@class.Name}({classNativeName}* instance);");
412+
WriteLine($"{@class.Name}({classNativeName}* {ClassCtorInstanceParamIdentifier}, bool ownNativeInstance = false);");
413413
NewLine();
414414

415415
foreach (var ctor in @class.Constructors)

src/Generator/Generators/C/CppSources.cs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)