Browse Source

Rework constructor generation in C++ code generator.

pull/1543/head
Joao Matos 5 years ago committed by João Matos
parent
commit
eba1b83324
  1. 2
      src/Generator/Generators/C/CCodeGenerator.cs
  2. 2
      src/Generator/Generators/C/CppHeaders.cs
  3. 56
      src/Generator/Generators/C/CppSources.cs

2
src/Generator/Generators/C/CCodeGenerator.cs

@ -37,6 +37,8 @@ namespace CppSharp.Generators.C @@ -37,6 +37,8 @@ namespace CppSharp.Generators.C
public override string FileExtension { get; } = "h";
public virtual string ClassCtorInstanceParamIdentifier => "instance";
public override void Process()
{

2
src/Generator/Generators/C/CppHeaders.cs

@ -409,7 +409,7 @@ namespace CppSharp.Generators.Cpp @@ -409,7 +409,7 @@ namespace CppSharp.Generators.Cpp
var classNativeName = @class.Visit(CTypePrinter);
CTypePrinter.PopContext();
WriteLine($"{@class.Name}({classNativeName}* instance);");
WriteLine($"{@class.Name}({classNativeName}* {ClassCtorInstanceParamIdentifier}, bool ownNativeInstance = false);");
NewLine();
foreach (var ctor in @class.Constructors)

56
src/Generator/Generators/C/CppSources.cs

@ -162,7 +162,7 @@ namespace CppSharp.Generators.Cpp @@ -162,7 +162,7 @@ namespace CppSharp.Generators.Cpp
return;
// Output a default constructor that takes the native instance.
GenerateClassConstructor(@class);
GenerateClassConstructor(@class, withOwnNativeInstanceParam: true);
if (@class.IsRefType)
{
@ -290,15 +290,26 @@ namespace CppSharp.Generators.Cpp @@ -290,15 +290,26 @@ namespace CppSharp.Generators.Cpp
return true;
}
public virtual string ClassCtorInstanceParamIdentifier => "instance";
public virtual void GenerateClassConstructor(Class @class)
public virtual void GenerateClassConstructor(Class @class, bool withOwnNativeInstanceParam = false)
{
Write($"{QualifiedIdentifier(@class)}::{@class.Name}(");
var nativeType = $"::{@class.QualifiedOriginalName}*";
WriteLine($"{nativeType} {ClassCtorInstanceParamIdentifier})");
GenerateClassConstructorBase(@class);
//WriteLine($"{nativeType} {ClassCtorInstanceParamIdentifier})");
WriteLine(!withOwnNativeInstanceParam ? $"{nativeType} {ClassCtorInstanceParamIdentifier})" :
$"{nativeType} {ClassCtorInstanceParamIdentifier}, bool ownNativeInstance)");
var hasBase = GenerateClassConstructorBase(@class, null, withOwnNativeInstanceParam);
if (CLIGenerator.ShouldGenerateClassNativeField(@class))
{
Indent();
Write(hasBase ? "," : ":");
Unindent();
WriteLine(!withOwnNativeInstanceParam ? " {0}(false)" : " {0}(ownNativeInstance)",
Helpers.OwnsNativeInstanceIdentifier);
}
WriteOpenBraceAndIndent();
@ -312,30 +323,29 @@ namespace CppSharp.Generators.Cpp @@ -312,30 +323,29 @@ namespace CppSharp.Generators.Cpp
NewLine();
}
private bool GenerateClassConstructorBase(Class @class, Method method = null)
private bool GenerateClassConstructorBase(Class @class, Method method = null,
bool withOwnNativeInstanceParam = false)
{
var hasBase = @class.HasBase && @class.Bases[0].IsClass && @class.Bases[0].Class.IsGenerated;
if (!hasBase)
if (@class.IsValueType)
return true;
if (!@class.NeedsBase)
return false;
if (!@class.IsValueType)
{
Indent();
Indent();
var baseClass = @class.Bases[0].Class;
Write($": {QualifiedIdentifier(baseClass)}(");
Write($": {QualifiedIdentifier(@class.BaseClass)}(");
// We cast the value to the base class type since otherwise there
// could be ambiguous call to overloaded constructors.
CTypePrinter.PushContext(TypePrinterContextKind.Native);
var nativeTypeName = baseClass.Visit(CTypePrinter);
CTypePrinter.PopContext();
Write($"({nativeTypeName}*)");
// We cast the value to the base class type since otherwise there
// could be ambiguous call to overloaded constructors.
var cppTypePrinter = new CppTypePrinter(Context);
var nativeTypeName = @class.BaseClass.Visit(cppTypePrinter);
WriteLine("{0})", method != null ? "nullptr" : ClassCtorInstanceParamIdentifier);
Write($"({nativeTypeName}*)");
WriteLine("{0}{1})", method != null ? "nullptr" : ClassCtorInstanceParamIdentifier,
!withOwnNativeInstanceParam ? "" : ", ownNativeInstance");
Unindent();
}
Unindent();
return true;
}

Loading…
Cancel
Save