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

3
src/Generator/Driver.cs

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

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

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

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

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

29
src/Generator/Passes/CheckDuplicatedNamesPass.cs

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

4
src/Generator/Types/TypeMap.cs

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

Loading…
Cancel
Save