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

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

@ -236,7 +236,7 @@ namespace CppSharp.Generators.Cpp @@ -236,7 +236,7 @@ namespace CppSharp.Generators.Cpp
if (@class.IsRefType && needsCopy)
{
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);
instance = name;
}

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

@ -39,7 +39,7 @@ namespace CppSharp.Generators.Cpp @@ -39,7 +39,7 @@ namespace CppSharp.Generators.Cpp
NewLine();
PopBlock();
VisitDeclContext(TranslationUnit);
VisitNamespace(TranslationUnit);
PushBlock(BlockKind.Footer);
PopBlock();
@ -114,7 +114,7 @@ namespace CppSharp.Generators.Cpp @@ -114,7 +114,7 @@ namespace CppSharp.Generators.Cpp
return true;
}
public void GenerateClass(Class @class)
public override bool VisitClassDecl(Class @class)
{
PushBlock(BlockKind.Class);
@ -144,6 +144,8 @@ namespace CppSharp.Generators.Cpp @@ -144,6 +144,8 @@ namespace CppSharp.Generators.Cpp
}
PopBlock();
return true;
}
public virtual void GenerateClassConstructors(Class @class)
@ -308,13 +310,14 @@ namespace CppSharp.Generators.Cpp @@ -308,13 +310,14 @@ namespace CppSharp.Generators.Cpp
Indent();
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.
var cppTypePrinter = new CppTypePrinter();
var nativeTypeName = baseClass.Visit(cppTypePrinter);
Write("({0}*)", nativeTypeName);
CTypePrinter.PushContext(TypePrinterContextKind.Native);
var nativeTypeName = baseClass.Visit(CTypePrinter);
CTypePrinter.PopContext();
Write($"({nativeTypeName}*)");
WriteLine("{0})", method != null ? "nullptr" : ClassCtorInstanceParamIdentifier);
@ -437,9 +440,9 @@ namespace CppSharp.Generators.Cpp @@ -437,9 +440,9 @@ namespace CppSharp.Generators.Cpp
}
var method = function as Method;
var field = (method?.AssociatedDeclaration as Property)?.Field;
var @class = function.Namespace as Class;
var field = (method?.AssociatedDeclaration as Property)?.Field;
if (field != null)
{
Write($"((::{@class.QualifiedOriginalName}*){Helpers.InstanceIdentifier})->");

2
src/Generator/Generators/CodeGenerator.cs

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

2
src/Generator/Types/TypeMapDatabase.cs

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

Loading…
Cancel
Save