Skip to content

Commit 8c2da6d

Browse files
Major refactor: TypePrinter: improve modular design + cleanup (#1796)
1 parent 0edd48c commit 8c2da6d

13 files changed

+58
-116
lines changed

src/Generator/Generator.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ public abstract class Generator : IDisposable
2828
{
2929
public BindingContext Context { get; }
3030

31+
protected readonly TypePrinter typePrinter;
32+
3133
protected Generator(BindingContext context)
3234
{
3335
Context = context;
36+
typePrinter = Context.Options.GeneratorKind.CreateTypePrinter(context);
3437
CppSharp.AST.Type.TypePrinterDelegate += TypePrinterDelegate;
3538
}
3639

@@ -155,7 +158,10 @@ public virtual GeneratorOutput GenerateModule(Module module)
155158
return output;
156159
}
157160

158-
protected abstract string TypePrinterDelegate(CppSharp.AST.Type type);
161+
protected virtual string TypePrinterDelegate(CppSharp.AST.Type type)
162+
{
163+
return type.Visit(typePrinter);
164+
}
159165

160166
public static string GeneratedIdentifier(string id) =>
161167
$"__{(id.StartsWith("@") ? id.Substring(1) : id)}";

src/Generator/GeneratorKind.cs

+29-20
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,32 @@ public class GeneratorKind : IEquatable<GeneratorKind>
2020

2121
public string ID { get; }
2222
public string Name { get; }
23-
public System.Type Type { get; }
23+
public System.Type GeneratorType { get; }
24+
public System.Type TypePrinterType { get; }
2425
public string[] CLIOptions { get; }
2526

26-
public GeneratorKind(string id, string name, System.Type type, string[] cLIOptions = null)
27+
public GeneratorKind(string id, string name, System.Type generatorType, System.Type typePrinterType, string[] cLIOptions = null)
2728
{
2829
if (Registered.Any(kind => kind.ID == id))
2930
{
3031
throw new Exception($"GeneratorKind has an already registered ID: {ID}");
3132
}
3233
ID = id;
3334
Name = name;
34-
Type = type;
35+
GeneratorType = generatorType;
36+
TypePrinterType = typePrinterType;
3537
CLIOptions = cLIOptions;
3638
Registered.Add(this);
3739
}
3840

3941
public Generator CreateGenerator(BindingContext context)
4042
{
41-
return (Generator)Activator.CreateInstance(Type, context);
43+
return (Generator)Activator.CreateInstance(GeneratorType, context);
44+
}
45+
46+
public TypePrinter CreateTypePrinter(BindingContext context)
47+
{
48+
return (TypePrinter)Activator.CreateInstance(TypePrinterType, context);
4249
}
4350

4451
public bool IsCLIOptionMatch(string cliOption)
@@ -93,37 +100,44 @@ public override int GetHashCode()
93100
}
94101

95102
public const string CLI_ID = "CLI";
96-
public static readonly GeneratorKind CLI = new(CLI_ID, "C++/CLI", typeof(CLIGenerator), new[] { "cli" });
103+
public static readonly GeneratorKind CLI = new(CLI_ID, "C++/CLI", typeof(CLIGenerator), typeof(CLITypePrinter), new[] { "cli" });
97104

98105
public const string CSharp_ID = "CSharp";
99-
public static readonly GeneratorKind CSharp = new(CSharp_ID, "C#", typeof(CSharpGenerator), new[] { "csharp" });
106+
public static readonly GeneratorKind CSharp = new(CSharp_ID, "C#", typeof(CSharpGenerator), typeof(CSharpTypePrinter), new[] { "csharp" });
100107

101108
public const string C_ID = "C";
102-
public static readonly GeneratorKind C = new(C_ID, "C", typeof(CGenerator), new[] { "c" });
109+
public static readonly GeneratorKind C = new(C_ID, "C", typeof(CGenerator), typeof(CppTypePrinter), new[] { "c" });
103110

104111
public const string CPlusPlus_ID = "CPlusPlus";
105-
public static readonly GeneratorKind CPlusPlus = new(CPlusPlus_ID, "CPlusPlus", typeof(CppGenerator), new[] { "cpp" });
112+
public static readonly GeneratorKind CPlusPlus = new(CPlusPlus_ID, "CPlusPlus", typeof(CppGenerator), typeof(CppTypePrinter), new[] { "cpp" });
106113

107114
public const string Emscripten_ID = "Emscripten";
108-
public static readonly GeneratorKind Emscripten = new(Emscripten_ID, "Emscripten", typeof(EmscriptenGenerator), new[] { "emscripten" });
115+
public static readonly GeneratorKind Emscripten = new(Emscripten_ID, "Emscripten", typeof(EmscriptenGenerator), typeof(EmscriptenTypePrinter), new[] { "emscripten" });
109116

110117
public const string ObjectiveC_ID = "ObjectiveC";
111-
public static readonly GeneratorKind ObjectiveC = new(ObjectiveC_ID, "ObjectiveC", typeof(NotImplementedGenerator));
118+
public static readonly GeneratorKind ObjectiveC = new(ObjectiveC_ID, "ObjectiveC", typeof(NotImplementedGenerator), typeof(NotImplementedTypePrinter));
112119

113120
public const string Java_ID = "Java";
114-
public static readonly GeneratorKind Java = new(Java_ID, "Java", typeof(NotImplementedGenerator));
121+
public static readonly GeneratorKind Java = new(Java_ID, "Java", typeof(NotImplementedGenerator), typeof(NotImplementedTypePrinter));
115122

116123
public const string Swift_ID = "Swift";
117-
public static readonly GeneratorKind Swift = new(Swift_ID, "Swift", typeof(NotImplementedGenerator));
124+
public static readonly GeneratorKind Swift = new(Swift_ID, "Swift", typeof(NotImplementedGenerator), typeof(NotImplementedTypePrinter));
118125

119126
public const string QuickJS_ID = "QuickJS";
120-
public static readonly GeneratorKind QuickJS = new(QuickJS_ID, "QuickJS", typeof(QuickJSGenerator), new[] { "qjs" });
127+
public static readonly GeneratorKind QuickJS = new(QuickJS_ID, "QuickJS", typeof(QuickJSGenerator), typeof(QuickJSTypePrinter), new[] { "qjs" });
121128

122129
public const string NAPI_ID = "NAPI";
123-
public static readonly GeneratorKind NAPI = new(NAPI_ID, "N-API", typeof(NAPIGenerator), new[] { "napi" });
130+
public static readonly GeneratorKind NAPI = new(NAPI_ID, "N-API", typeof(NAPIGenerator), typeof(NAPITypePrinter), new[] { "napi" });
124131

125132
public const string TypeScript_ID = "TypeScript";
126-
public static readonly GeneratorKind TypeScript = new(TypeScript_ID, "TypeScript", typeof(TSGenerator), new[] { "ts", "typescript" });
133+
public static readonly GeneratorKind TypeScript = new(TypeScript_ID, "TypeScript", typeof(TSGenerator), typeof(TSTypePrinter), new[] { "ts", "typescript" });
134+
}
135+
136+
public class NotImplementedTypePrinter : TypePrinter
137+
{
138+
public NotImplementedTypePrinter(BindingContext context) : base(context)
139+
{
140+
}
127141
}
128142

129143
public class NotImplementedGenerator : Generator
@@ -142,10 +156,5 @@ public override bool SetupPasses()
142156
{
143157
throw new NotImplementedException();
144158
}
145-
146-
protected override string TypePrinterDelegate(CppSharp.AST.Type type)
147-
{
148-
throw new NotImplementedException();
149-
}
150159
}
151160
}

src/Generator/Generators/C/CGenerator.cs

-8
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ namespace CppSharp.Generators.C
1010
/// </summary>
1111
public class CGenerator : Generator
1212
{
13-
private readonly CppTypePrinter typePrinter;
14-
1513
public CGenerator(BindingContext context) : base(context)
1614
{
17-
typePrinter = new CppTypePrinter(Context);
1815
}
1916

2017
public override List<CodeGenerator> Generate(IEnumerable<TranslationUnit> units)
@@ -31,10 +28,5 @@ public override List<CodeGenerator> Generate(IEnumerable<TranslationUnit> units)
3128
}
3229

3330
public override bool SetupPasses() => true;
34-
35-
protected override string TypePrinterDelegate(Type type)
36-
{
37-
return type.Visit(typePrinter).ToString();
38-
}
3931
}
4032
}

src/Generator/Generators/C/CppGenerator.cs

-8
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@ namespace CppSharp.Generators.Cpp
1111
/// </summary>
1212
public class CppGenerator : CGenerator
1313
{
14-
private readonly CppTypePrinter typePrinter;
15-
1614
public CppGenerator(BindingContext context) : base(context)
1715
{
18-
typePrinter = new CppTypePrinter(Context);
1916
}
2017

2118
public override List<CodeGenerator> Generate(IEnumerable<TranslationUnit> units)
@@ -44,11 +41,6 @@ public static bool ShouldGenerateClassNativeInstanceField(Class @class)
4441

4542
return @class.IsRefType && (!@class.HasBase || !@class.HasRefBase());
4643
}
47-
48-
protected override string TypePrinterDelegate(Type type)
49-
{
50-
return type.Visit(typePrinter).ToString();
51-
}
5244
}
5345

5446
/// <summary>

src/Generator/Generators/C/CppTypePrinter.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,13 @@ public class CppTypePrinter : TypePrinter
2828

2929
public TypePrintScopeKind MethodScopeKind = TypePrintScopeKind.Qualified;
3030

31-
public CppTypePrinter() : base(TypePrinterContextKind.Native)
31+
public CppTypePrinter(BindingContext context) : base(context, TypePrinterContextKind.Native)
3232
{
3333
PrintFlavorKind = CppTypePrintFlavorKind.Cpp;
3434
PrintTypeQualifiers = true;
3535
PrintTypeModifiers = true;
3636
}
3737

38-
public CppTypePrinter(BindingContext context) : this()
39-
{
40-
Context = context;
41-
}
42-
4338
public TypeMapDatabase TypeMapDatabase => Context.TypeMaps;
4439
public DriverOptions Options => Context.Options;
4540

src/Generator/Generators/CLI/CLIGenerator.cs

-8
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ namespace CppSharp.Generators.CLI
1010
/// </summary>
1111
public class CLIGenerator : Generator
1212
{
13-
private readonly CppTypePrinter typePrinter;
14-
1513
public CLIGenerator(BindingContext context) : base(context)
1614
{
17-
typePrinter = new CLITypePrinter(context);
1815
}
1916

2017
public override List<CodeGenerator> Generate(IEnumerable<TranslationUnit> units)
@@ -39,10 +36,5 @@ public static bool ShouldGenerateClassNativeField(Class @class)
3936

4037
return @class.IsRefType && (!@class.NeedsBase || !@class.HasRefBase());
4138
}
42-
43-
protected override string TypePrinterDelegate(Type type)
44-
{
45-
return type.Visit(typePrinter).ToString();
46-
}
4739
}
4840
}

src/Generator/Generators/CSharp/CSharpGenerator.cs

+1-9
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@ namespace CppSharp.Generators.CSharp
77
{
88
public class CSharpGenerator : Generator
99
{
10-
private readonly CSharpTypePrinter typePrinter;
11-
1210
public CSharpGenerator(BindingContext context) : base(context)
1311
{
14-
typePrinter = new CSharpTypePrinter(context);
1512
}
1613

1714
public override List<CodeGenerator> Generate(IEnumerable<TranslationUnit> units)
1815
{
1916
var outputs = new List<CodeGenerator>();
2017

21-
var gen = new CSharpSources(Context, units) { TypePrinter = typePrinter };
18+
var gen = new CSharpSources(Context, units) { TypePrinter = (CSharpTypePrinter)typePrinter };
2219
outputs.Add(gen);
2320

2421
return outputs;
@@ -42,10 +39,5 @@ public override bool SetupPasses()
4239

4340
return true;
4441
}
45-
46-
protected override string TypePrinterDelegate(Type type)
47-
{
48-
return type.Visit(typePrinter);
49-
}
5042
}
5143
}

src/Generator/Generators/CSharp/CSharpTypePrinter.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,10 @@ public class CSharpTypePrinter : TypePrinter
2121

2222
public bool PrintModuleOutputNamespace = true;
2323

24-
public CSharpTypePrinter()
24+
public CSharpTypePrinter(BindingContext context) : base(context)
2525
{
2626
}
2727

28-
public CSharpTypePrinter(BindingContext context)
29-
{
30-
Context = context;
31-
}
32-
3328
public string QualifiedType(string name)
3429
{
3530
return IsGlobalQualifiedScope ? $"global::{name}" : name;

src/Generator/Generators/Marshal.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using CppSharp.AST;
22
using CppSharp.Generators.C;
3+
using System;
34

45
namespace CppSharp.Generators
56
{
67
public class MarshalContext : TypePrinter
78
{
8-
public MarshalContext(BindingContext context, uint indentation)
9+
public MarshalContext(BindingContext context, uint indentation) : base(context)
910
{
10-
Context = context;
1111
Before = new TextGenerator { CurrentIndentation = indentation };
1212
Return = new TextGenerator { CurrentIndentation = indentation };
1313
Cleanup = new TextGenerator { CurrentIndentation = indentation };
@@ -34,16 +34,16 @@ public MarshalContext(BindingContext context, uint indentation)
3434
public uint Indentation { get; }
3535
}
3636

37-
public abstract class MarshalPrinter<C, P> : AstVisitor where C : MarshalContext where P : TypePrinter, new()
37+
public abstract class MarshalPrinter<C, P> : AstVisitor where C : MarshalContext where P : TypePrinter
3838
{
3939
public C Context { get; }
4040

4141
protected MarshalPrinter(C ctx)
4242
{
4343
Context = ctx;
44-
typePrinter.Context = ctx.Context;
44+
typePrinter = (P)Activator.CreateInstance(typeof(P), ctx.Context);
4545
}
4646

47-
protected P typePrinter = new P();
47+
protected P typePrinter;
4848
}
4949
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using CppSharp.Generators.C;
2+
3+
namespace CppSharp.Generators.C
4+
{
5+
public class NAPITypePrinter : CppTypePrinter
6+
{
7+
public NAPITypePrinter(BindingContext context) : base(context)
8+
{
9+
}
10+
}
11+
}

src/Generator/Generators/TS/TSGenerator.cs

-8
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@ namespace CppSharp.Generators.TS
1111
/// </summary>
1212
public class TSGenerator : CGenerator
1313
{
14-
private readonly TSTypePrinter typePrinter;
15-
1614
public TSGenerator(BindingContext context) : base(context)
1715
{
18-
typePrinter = new TSTypePrinter(Context);
1916
}
2017

2118
public override List<CodeGenerator> Generate(IEnumerable<TranslationUnit> units)
@@ -32,10 +29,5 @@ public override bool SetupPasses()
3229
{
3330
return true;
3431
}
35-
36-
protected override string TypePrinterDelegate(Type type)
37-
{
38-
return type.Visit(typePrinter).ToString();
39-
}
4032
}
4133
}

src/Generator/Generators/TypePrinter.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ public class TypePrinter : ITypePrinter<TypePrinterResult>,
6464
public TypePrintScopeKind ScopeKind => scopeKinds.Peek();
6565
public bool IsGlobalQualifiedScope => ScopeKind == TypePrintScopeKind.GlobalQualified;
6666

67-
public TypePrinter(TypePrinterContextKind contextKind = TypePrinterContextKind.Managed)
67+
public TypePrinter(BindingContext context, TypePrinterContextKind contextKind = TypePrinterContextKind.Managed)
6868
{
69+
Context = context;
6970
contexts = new Stack<TypePrinterContextKind>();
7071
marshalKinds = new Stack<MarshalKind>();
7172
scopeKinds = new Stack<TypePrintScopeKind>();

0 commit comments

Comments
 (0)