Browse Source

Overall fixes to C++ generation.

pull/1316/head
João Matos 6 years ago committed by João Matos
parent
commit
283ba0ea3b
  1. 33
      src/Generator/Generators/C/CppHeaders.cs
  2. 2
      src/Generator/Generators/C/CppMarshal.cs
  3. 19
      src/Generator/Generators/C/CppSources.cs
  4. 2
      src/Generator/Generators/CodeGenerator.cs
  5. 2
      src/Generator/Types/TypeMapDatabase.cs

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

@ -145,7 +145,7 @@ namespace CppSharp.Generators.Cpp
@namespace.Visit(this); @namespace.Visit(this);
} }
public void GenerateDeclContext(DeclarationContext decl) public override bool VisitDeclContext(DeclarationContext decl)
{ {
// Generate all the type references for the module. // Generate all the type references for the module.
foreach (var typeRef in decl.TypeReferences) foreach (var typeRef in decl.TypeReferences)
@ -176,6 +176,8 @@ namespace CppSharp.Generators.Cpp
foreach (var childNamespace in decl.Namespaces) foreach (var childNamespace in decl.Namespaces)
childNamespace.Visit(this); childNamespace.Visit(this);
return true;
} }
public override bool VisitNamespace(Namespace @namespace) public override bool VisitNamespace(Namespace @namespace)
@ -193,7 +195,7 @@ namespace CppSharp.Generators.Cpp
WriteOpenBraceAndIndent(); WriteOpenBraceAndIndent();
} }
GenerateDeclContext(@namespace); VisitDeclContext(@namespace);
if (generateNamespace) if (generateNamespace)
{ {
@ -258,15 +260,13 @@ namespace CppSharp.Generators.Cpp
// Process the nested types. // Process the nested types.
Indent(); Indent();
GenerateDeclContext(@class); VisitDeclContext(@class);
Unindent(); Unindent();
var nativeType = $"::{@class.QualifiedOriginalName}*";
if (CppGenerator.ShouldGenerateClassNativeField(@class)) if (CppGenerator.ShouldGenerateClassNativeField(@class))
GenerateClassNativeField(@class, nativeType); GenerateClassNativeField(@class);
GenerateClassConstructors(@class, nativeType); GenerateClassConstructors(@class);
GenerateClassProperties(@class); GenerateClassProperties(@class);
GenerateClassEvents(@class); GenerateClassEvents(@class);
GenerateClassMethods(@class.Methods); GenerateClassMethods(@class.Methods);
@ -303,8 +303,10 @@ namespace CppSharp.Generators.Cpp
return true; return true;
} }
public void GenerateClassNativeField(Class @class, string nativeType) public void GenerateClassNativeField(Class @class)
{ {
PushBlock();
var nativeInstanceField = new Field() var nativeInstanceField = new Field()
{ {
Name = Helpers.InstanceIdentifier, Name = Helpers.InstanceIdentifier,
@ -313,9 +315,13 @@ namespace CppSharp.Generators.Cpp
}; };
Indent(); Indent();
CTypePrinter.PushContext(TypePrinterContextKind.Native);
nativeInstanceField.Visit(this); nativeInstanceField.Visit(this);
CTypePrinter.PopContext();
Unindent(); Unindent();
PopBlock(NewLineKind.BeforeNextBlock);
/*var nativeInstanceProperty = new Property() /*var nativeInstanceProperty = new Property()
{ {
Name = Helpers.InstanceIdentifier, Name = Helpers.InstanceIdentifier,
@ -329,20 +335,18 @@ namespace CppSharp.Generators.Cpp
{ {
} }
public void GenerateClassConstructors(Class @class, string nativeType) public void GenerateClassConstructors(Class @class)
{ {
if (@class.IsStatic) if (@class.IsStatic)
return; return;
Indent(); Indent();
var classNativeName = @class.Visit(CTypePrinter);
CTypePrinter.PushContext(TypePrinterContextKind.Native); CTypePrinter.PushContext(TypePrinterContextKind.Native);
var classManagedName = @class.Visit(CTypePrinter); var classNativeName = @class.Visit(CTypePrinter);
CTypePrinter.PopContext(); CTypePrinter.PopContext();
WriteLine($"{@class.Name}({classManagedName}* native);"); WriteLine($"{@class.Name}({classNativeName}* instance);");
NewLine(); NewLine();
foreach (var ctor in @class.Constructors) foreach (var ctor in @class.Constructors)
@ -416,7 +420,8 @@ namespace CppSharp.Generators.Cpp
GenerateDeclarationCommon(field); GenerateDeclarationCommon(field);
var @class = field.Namespace as Class; var @class = field.Namespace as Class;
WriteLine($"{field.Type} {field.Name};"); var fieldType = field.Type.Visit(CTypePrinter);
WriteLine($"{fieldType} {field.Name};");
PopBlock(); PopBlock();

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

@ -236,7 +236,7 @@ namespace CppSharp.Generators.Cpp
if (@class.IsRefType && needsCopy) if (@class.IsRefType && needsCopy)
{ {
var name = Generator.GeneratedIdentifier(Context.ReturnVarName); var name = Generator.GeneratedIdentifier(Context.ReturnVarName);
Context.Before.WriteLine($"auto {name} = {MemoryAllocOperator} ::{0}({1});", Context.Before.WriteLine($"auto {name} = {MemoryAllocOperator} ::{{0}}({{1}});",
@class.QualifiedOriginalName, Context.ReturnVarName); @class.QualifiedOriginalName, Context.ReturnVarName);
instance = name; instance = name;
} }

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

@ -39,7 +39,7 @@ namespace CppSharp.Generators.Cpp
NewLine(); NewLine();
PopBlock(); PopBlock();
VisitDeclContext(TranslationUnit); VisitNamespace(TranslationUnit);
PushBlock(BlockKind.Footer); PushBlock(BlockKind.Footer);
PopBlock(); PopBlock();
@ -114,7 +114,7 @@ namespace CppSharp.Generators.Cpp
return true; return true;
} }
public void GenerateClass(Class @class) public override bool VisitClassDecl(Class @class)
{ {
PushBlock(BlockKind.Class); PushBlock(BlockKind.Class);
@ -144,6 +144,8 @@ namespace CppSharp.Generators.Cpp
} }
PopBlock(); PopBlock();
return true;
} }
public virtual void GenerateClassConstructors(Class @class) public virtual void GenerateClassConstructors(Class @class)
@ -308,13 +310,14 @@ namespace CppSharp.Generators.Cpp
Indent(); Indent();
var baseClass = @class.Bases[0].Class; var baseClass = @class.Bases[0].Class;
Write(": {0}(", QualifiedIdentifier(baseClass)); Write($": {QualifiedIdentifier(baseClass)}(");
// We cast the value to the base clas type since otherwise there // We cast the value to the base class type since otherwise there
// could be ambiguous call to overloaded constructors. // could be ambiguous call to overloaded constructors.
var cppTypePrinter = new CppTypePrinter(); CTypePrinter.PushContext(TypePrinterContextKind.Native);
var nativeTypeName = baseClass.Visit(cppTypePrinter); var nativeTypeName = baseClass.Visit(CTypePrinter);
Write("({0}*)", nativeTypeName); CTypePrinter.PopContext();
Write($"({nativeTypeName}*)");
WriteLine("{0})", method != null ? "nullptr" : ClassCtorInstanceParamIdentifier); WriteLine("{0})", method != null ? "nullptr" : ClassCtorInstanceParamIdentifier);
@ -437,9 +440,9 @@ namespace CppSharp.Generators.Cpp
} }
var method = function as Method; var method = function as Method;
var field = (method?.AssociatedDeclaration as Property)?.Field;
var @class = function.Namespace as Class; var @class = function.Namespace as Class;
var field = (method?.AssociatedDeclaration as Property)?.Field;
if (field != null) if (field != null)
{ {
Write($"((::{@class.QualifiedOriginalName}*){Helpers.InstanceIdentifier})->"); Write($"((::{@class.QualifiedOriginalName}*){Helpers.InstanceIdentifier})->");

2
src/Generator/Generators/CodeGenerator.cs

@ -250,7 +250,7 @@ namespace CppSharp.Generators
public virtual bool VisitTranslationUnit(TranslationUnit unit) public virtual bool VisitTranslationUnit(TranslationUnit unit)
{ {
return VisitDeclContext(unit); return VisitNamespace(unit);
} }
public virtual bool VisitDeclContext(DeclarationContext context) public virtual bool VisitDeclContext(DeclarationContext context)

2
src/Generator/Types/TypeMapDatabase.cs

@ -94,6 +94,8 @@ namespace CppSharp.Types
PrintLogicalNames = true PrintLogicalNames = true
}; };
typePrinter.PushContext(TypePrinterContextKind.Native);
foreach (var resolveTypeDefs in new[] { false, true }) foreach (var resolveTypeDefs in new[] { false, true })
{ {
foreach (var typePrintScopeKind in foreach (var typePrintScopeKind in

Loading…
Cancel
Save