Skip to content

Commit b632051

Browse files
committed
Add initial C base generator.
1 parent 423b9a7 commit b632051

File tree

7 files changed

+76
-11
lines changed

7 files changed

+76
-11
lines changed

src/CppParser/Bootstrap/Bootstrap.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,7 @@ public override void GenerateIncludes()
14271427
"AST::Expr* Parser::WalkExpression(const clang::Expr* Expr)";
14281428
}
14291429

1430-
class NativeParserCodeGenerator : CCodeGenerator
1430+
class NativeParserCodeGenerator : Generators.C.CCodeGenerator
14311431
{
14321432
internal readonly IEnumerable<Declaration> Declarations;
14331433

@@ -1795,7 +1795,7 @@ public static string GetDeclName(Declaration decl,
17951795

17961796
if (kind == GeneratorKind.CPlusPlus)
17971797
{
1798-
if (CCodeGenerator.IsReservedKeyword(name))
1798+
if (Generators.C.CCodeGenerator.IsReservedKeyword(name))
17991799
name = $"_{name}";
18001800
}
18011801
else if (kind == GeneratorKind.CSharp)

src/Generator/Driver.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Microsoft.CSharp;
1515
using CppSharp.Types;
1616
using CppSharp.Generators.Cpp;
17+
using CppSharp.Generators.C;
1718

1819
namespace CppSharp
1920
{
@@ -36,6 +37,8 @@ Generator CreateGeneratorFromKind(GeneratorKind kind)
3637
{
3738
switch (kind)
3839
{
40+
case GeneratorKind.C:
41+
return new CGenerator(Context);
3942
case GeneratorKind.CPlusPlus:
4043
return new CppGenerator(Context);
4144
case GeneratorKind.CLI:

src/Generator/Generators/C/CCodeGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using CppSharp.AST;
2+
using CppSharp.AST.Extensions;
23
using System.Collections.Generic;
34
using System.Linq;
45

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Collections.Generic;
2+
using CppSharp.AST;
3+
using CppSharp.Generators.Cpp;
4+
5+
namespace CppSharp.Generators.C
6+
{
7+
/// <summary>
8+
/// C generator responsible for driving the generation of source and
9+
/// header files.
10+
/// </summary>
11+
public class CGenerator : Generator
12+
{
13+
private readonly CppTypePrinter typePrinter;
14+
15+
public CGenerator(BindingContext context) : base(context)
16+
{
17+
typePrinter = new CppTypePrinter(Context);
18+
}
19+
20+
public override List<CodeGenerator> Generate(IEnumerable<TranslationUnit> units)
21+
{
22+
var outputs = new List<CodeGenerator>();
23+
24+
var header = new CppHeaders(Context, units);
25+
outputs.Add(header);
26+
27+
var source = new CppSources(Context, units);
28+
outputs.Add(source);
29+
30+
return outputs;
31+
}
32+
33+
public override bool SetupPasses() => true;
34+
35+
protected override string TypePrinterDelegate(Type type)
36+
{
37+
return type.Visit(typePrinter).ToString();
38+
}
39+
}
40+
}

src/Generator/Generators/C/CppGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
namespace CppSharp.Generators.Cpp
66
{
77
/// <summary>
8-
/// C/C++ generator responsible for driving the generation of source
9-
/// and header files.
8+
/// C++ generator responsible for driving the generation of source and
9+
/// header files.
1010
/// </summary>
11-
public class CppGenerator : Generator
11+
public class CppGenerator : CGenerator
1212
{
1313
private readonly CppTypePrinter typePrinter;
1414

src/Generator/Passes/CheckDuplicatedNamesPass.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using CppSharp.AST;
55
using CppSharp.AST.Extensions;
66
using CppSharp.Generators;
7+
using CppSharp.Generators.C;
78
using CppSharp.Generators.CLI;
89
using CppSharp.Generators.CSharp;
910
using CppSharp.Types;
@@ -198,20 +199,36 @@ public CheckDuplicatedNamesPass()
198199

199200
public override bool VisitASTContext(ASTContext context)
200201
{
201-
TypePrinter typePrinter = null;
202-
switch (Options.GeneratorKind)
202+
var typePrinter = GetTypePrinter(Options.GeneratorKind, Context);
203+
DeclarationName.ParameterTypeComparer.TypePrinter = typePrinter;
204+
DeclarationName.ParameterTypeComparer.TypeMaps = Context.TypeMaps;
205+
DeclarationName.ParameterTypeComparer.GeneratorKind = Options.GeneratorKind;
206+
return base.VisitASTContext(context);
207+
}
208+
209+
private TypePrinter GetTypePrinter(GeneratorKind kind, BindingContext context)
210+
{
211+
TypePrinter typePrinter;
212+
switch (kind)
203213
{
214+
case GeneratorKind.C:
215+
typePrinter = new CppTypePrinter(Context)
216+
{
217+
PrintFlavorKind = CppTypePrintFlavorKind.C
218+
};
219+
break;
220+
case GeneratorKind.CPlusPlus:
204221
case GeneratorKind.CLI:
205222
typePrinter = new CLITypePrinter(Context);
206223
break;
207224
case GeneratorKind.CSharp:
208225
typePrinter = new CSharpTypePrinter(Context);
209226
break;
227+
default:
228+
throw new System.NotImplementedException();
210229
}
211-
DeclarationName.ParameterTypeComparer.TypePrinter = typePrinter;
212-
DeclarationName.ParameterTypeComparer.TypeMaps = Context.TypeMaps;
213-
DeclarationName.ParameterTypeComparer.GeneratorKind = Options.GeneratorKind;
214-
return base.VisitASTContext(context);
230+
231+
return typePrinter;
215232
}
216233

217234
public override bool VisitProperty(Property decl)

src/Generator/Types/TypeMap.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using CppSharp.AST;
33
using CppSharp.Generators;
44
using CppSharp.Generators.AST;
5+
using CppSharp.Generators.C;
56
using CppSharp.Generators.CLI;
67
using CppSharp.Generators.Cpp;
78
using CppSharp.Generators.CSharp;
@@ -54,6 +55,7 @@ public virtual Type SignatureType(GeneratorKind kind, TypePrinterContext ctx)
5455
{
5556
switch (kind)
5657
{
58+
case GeneratorKind.C:
5759
case GeneratorKind.CPlusPlus:
5860
return CppSignatureType(ctx);
5961
case GeneratorKind.CLI:
@@ -69,6 +71,7 @@ public virtual void MarshalToNative(GeneratorKind kind, MarshalContext ctx)
6971
{
7072
switch (kind)
7173
{
74+
case GeneratorKind.C:
7275
case GeneratorKind.CPlusPlus:
7376
CppMarshalToNative(ctx);
7477
return;
@@ -87,6 +90,7 @@ public virtual void MarshalToManaged(GeneratorKind kind, MarshalContext ctx)
8790
{
8891
switch (kind)
8992
{
93+
case GeneratorKind.C:
9094
case GeneratorKind.CPlusPlus:
9195
CppMarshalToManaged(ctx);
9296
return;

0 commit comments

Comments
 (0)