Browse Source

Add initial C base generator.

pull/1514/head
Joao Matos 5 years ago committed by João Matos
parent
commit
b632051f3e
  1. 4
      src/CppParser/Bootstrap/Bootstrap.cs
  2. 3
      src/Generator/Driver.cs
  3. 1
      src/Generator/Generators/C/CCodeGenerator.cs
  4. 40
      src/Generator/Generators/C/CGenerator.cs
  5. 6
      src/Generator/Generators/C/CppGenerator.cs
  6. 29
      src/Generator/Passes/CheckDuplicatedNamesPass.cs
  7. 4
      src/Generator/Types/TypeMap.cs

4
src/CppParser/Bootstrap/Bootstrap.cs

@ -1427,7 +1427,7 @@ namespace CppSharp @@ -1427,7 +1427,7 @@ namespace CppSharp
"AST::Expr* Parser::WalkExpression(const clang::Expr* Expr)";
}
class NativeParserCodeGenerator : CCodeGenerator
class NativeParserCodeGenerator : Generators.C.CCodeGenerator
{
internal readonly IEnumerable<Declaration> Declarations;
@ -1795,7 +1795,7 @@ namespace CppSharp @@ -1795,7 +1795,7 @@ namespace CppSharp
if (kind == GeneratorKind.CPlusPlus)
{
if (CCodeGenerator.IsReservedKeyword(name))
if (Generators.C.CCodeGenerator.IsReservedKeyword(name))
name = $"_{name}";
}
else if (kind == GeneratorKind.CSharp)

3
src/Generator/Driver.cs

@ -14,6 +14,7 @@ using CppSharp.Utils; @@ -14,6 +14,7 @@ using CppSharp.Utils;
using Microsoft.CSharp;
using CppSharp.Types;
using CppSharp.Generators.Cpp;
using CppSharp.Generators.C;
namespace CppSharp
{
@ -36,6 +37,8 @@ namespace CppSharp @@ -36,6 +37,8 @@ namespace CppSharp
{
switch (kind)
{
case GeneratorKind.C:
return new CGenerator(Context);
case GeneratorKind.CPlusPlus:
return new CppGenerator(Context);
case GeneratorKind.CLI:

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

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
using CppSharp.AST;
using CppSharp.AST.Extensions;
using System.Collections.Generic;
using System.Linq;

40
src/Generator/Generators/C/CGenerator.cs

@ -0,0 +1,40 @@ @@ -0,0 +1,40 @@
using System.Collections.Generic;
using CppSharp.AST;
using CppSharp.Generators.Cpp;
namespace CppSharp.Generators.C
{
/// <summary>
/// C generator responsible for driving the generation of source and
/// header files.
/// </summary>
public class CGenerator : Generator
{
private readonly CppTypePrinter typePrinter;
public CGenerator(BindingContext context) : base(context)
{
typePrinter = new CppTypePrinter(Context);
}
public override List<CodeGenerator> Generate(IEnumerable<TranslationUnit> units)
{
var outputs = new List<CodeGenerator>();
var header = new CppHeaders(Context, units);
outputs.Add(header);
var source = new CppSources(Context, units);
outputs.Add(source);
return outputs;
}
public override bool SetupPasses() => true;
protected override string TypePrinterDelegate(Type type)
{
return type.Visit(typePrinter).ToString();
}
}
}

6
src/Generator/Generators/C/CppGenerator.cs

@ -5,10 +5,10 @@ using CppSharp.Generators.C; @@ -5,10 +5,10 @@ using CppSharp.Generators.C;
namespace CppSharp.Generators.Cpp
{
/// <summary>
/// C/C++ generator responsible for driving the generation of source
/// and header files.
/// C++ generator responsible for driving the generation of source and
/// header files.
/// </summary>
public class CppGenerator : Generator
public class CppGenerator : CGenerator
{
private readonly CppTypePrinter typePrinter;

29
src/Generator/Passes/CheckDuplicatedNamesPass.cs

@ -4,6 +4,7 @@ using System.Linq; @@ -4,6 +4,7 @@ using System.Linq;
using CppSharp.AST;
using CppSharp.AST.Extensions;
using CppSharp.Generators;
using CppSharp.Generators.C;
using CppSharp.Generators.CLI;
using CppSharp.Generators.CSharp;
using CppSharp.Types;
@ -198,20 +199,36 @@ namespace CppSharp.Passes @@ -198,20 +199,36 @@ namespace CppSharp.Passes
public override bool VisitASTContext(ASTContext context)
{
TypePrinter typePrinter = null;
switch (Options.GeneratorKind)
var typePrinter = GetTypePrinter(Options.GeneratorKind, Context);
DeclarationName.ParameterTypeComparer.TypePrinter = typePrinter;
DeclarationName.ParameterTypeComparer.TypeMaps = Context.TypeMaps;
DeclarationName.ParameterTypeComparer.GeneratorKind = Options.GeneratorKind;
return base.VisitASTContext(context);
}
private TypePrinter GetTypePrinter(GeneratorKind kind, BindingContext context)
{
TypePrinter typePrinter;
switch (kind)
{
case GeneratorKind.C:
typePrinter = new CppTypePrinter(Context)
{
PrintFlavorKind = CppTypePrintFlavorKind.C
};
break;
case GeneratorKind.CPlusPlus:
case GeneratorKind.CLI:
typePrinter = new CLITypePrinter(Context);
break;
case GeneratorKind.CSharp:
typePrinter = new CSharpTypePrinter(Context);
break;
default:
throw new System.NotImplementedException();
}
DeclarationName.ParameterTypeComparer.TypePrinter = typePrinter;
DeclarationName.ParameterTypeComparer.TypeMaps = Context.TypeMaps;
DeclarationName.ParameterTypeComparer.GeneratorKind = Options.GeneratorKind;
return base.VisitASTContext(context);
return typePrinter;
}
public override bool VisitProperty(Property decl)

4
src/Generator/Types/TypeMap.cs

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
using CppSharp.AST;
using CppSharp.Generators;
using CppSharp.Generators.AST;
using CppSharp.Generators.C;
using CppSharp.Generators.CLI;
using CppSharp.Generators.Cpp;
using CppSharp.Generators.CSharp;
@ -54,6 +55,7 @@ namespace CppSharp.Types @@ -54,6 +55,7 @@ namespace CppSharp.Types
{
switch (kind)
{
case GeneratorKind.C:
case GeneratorKind.CPlusPlus:
return CppSignatureType(ctx);
case GeneratorKind.CLI:
@ -69,6 +71,7 @@ namespace CppSharp.Types @@ -69,6 +71,7 @@ namespace CppSharp.Types
{
switch (kind)
{
case GeneratorKind.C:
case GeneratorKind.CPlusPlus:
CppMarshalToNative(ctx);
return;
@ -87,6 +90,7 @@ namespace CppSharp.Types @@ -87,6 +90,7 @@ namespace CppSharp.Types
{
switch (kind)
{
case GeneratorKind.C:
case GeneratorKind.CPlusPlus:
CppMarshalToManaged(ctx);
return;

Loading…
Cancel
Save