Skip to content

Commit 0edd48c

Browse files
Major refactoring: refactor GeneratorKind enum into a class (#1794)
* Major refactoring: refactor GeneratorKind enum into a class * Minor fix: add readonly * Add Type property for GenerationKind + cleanup * GeneratorKind: add Name property + refactor hardcoded names * GeneratorKind: add CLIOptions property + refactor hardcoded options * CppSharp.CLI: minor fix: use generator.ToLower() * GeneratorKind: fix warning
1 parent 40f3a09 commit 0edd48c

16 files changed

+231
-157
lines changed

src/CLI/CLI.cs

+5-25
Original file line numberDiff line numberDiff line change
@@ -206,33 +206,13 @@ static void GetFilesFromPath(string path, List<string> errorMessages)
206206

207207
static void GetGeneratorKind(string generator, List<string> errorMessages)
208208
{
209-
switch (generator.ToLower())
209+
foreach (GeneratorKind generatorKind in GeneratorKind.Registered)
210210
{
211-
case "csharp":
212-
options.Kind = GeneratorKind.CSharp;
213-
return;
214-
case "cli":
215-
options.Kind = GeneratorKind.CLI;
216-
return;
217-
case "c":
218-
options.Kind = GeneratorKind.C;
219-
return;
220-
case "cpp":
221-
options.Kind = GeneratorKind.CPlusPlus;
222-
return;
223-
case "napi":
224-
options.Kind = GeneratorKind.NAPI;
225-
return;
226-
case "qjs":
227-
options.Kind = GeneratorKind.QuickJS;
228-
return;
229-
case "ts":
230-
case "typescript":
231-
options.Kind = GeneratorKind.TypeScript;
232-
return;
233-
case "emscripten":
234-
options.Kind = GeneratorKind.Emscripten;
211+
if (generatorKind.IsCLIOptionMatch(generator.ToLower()))
212+
{
213+
options.Kind = generatorKind;
235214
return;
215+
}
236216
}
237217

238218
errorMessages.Add($"Unknown generator kind: {generator}.");

src/CLI/Generator.cs

+1-16
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public void Postprocess(Driver driver, ASTContext ctx)
198198
public void Run()
199199
{
200200
var messageBuilder = new StringBuilder();
201-
messageBuilder.Append($"Generating {GetGeneratorKindName(options.Kind)}");
201+
messageBuilder.Append($"Generating {options.Kind.Name}");
202202
messageBuilder.Append($" bindings for {GetPlatformName(options.Platform)} {options.Architecture}");
203203

204204
if (options.Cpp11ABI)
@@ -225,20 +225,5 @@ private static string GetPlatformName(TargetPlatform? platform)
225225
return platform.ToString();
226226
}
227227
}
228-
229-
private static string GetGeneratorKindName(GeneratorKind kind)
230-
{
231-
switch (kind)
232-
{
233-
case GeneratorKind.CLI:
234-
return "C++/CLI";
235-
case GeneratorKind.CSharp:
236-
return "C#";
237-
case GeneratorKind.NAPI:
238-
return "N-API";
239-
default:
240-
return kind.ToString();
241-
}
242-
}
243228
}
244229
}

src/CppParser/Bootstrap/Bootstrap.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1778,8 +1778,7 @@ private static string CleanClangNamespaceFromName(string qualifiedName)
17781778
return qualifiedName;
17791779
}
17801780

1781-
public static string GetDeclName(Declaration decl,
1782-
GeneratorKind kind = GeneratorKind.CPlusPlus)
1781+
public static string GetDeclName(Declaration decl, GeneratorKind kind)
17831782
{
17841783
string name = decl.Name;
17851784

@@ -1811,6 +1810,11 @@ public static string GetDeclName(Declaration decl,
18111810
return name;
18121811
}
18131812

1813+
public static string GetDeclName(Declaration decl)
1814+
{
1815+
return GetDeclName(decl, GeneratorKind.CPlusPlus);
1816+
}
1817+
18141818
public static AST.Type GetDeclType(AST.Type type,
18151819
TypePrinter typePrinter)
18161820
{

src/Generator/Driver.cs

+2-34
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
using System.Linq;
55
using CppSharp.AST;
66
using CppSharp.Generators;
7-
using CppSharp.Generators.C;
8-
using CppSharp.Generators.CLI;
9-
using CppSharp.Generators.Cpp;
10-
using CppSharp.Generators.CSharp;
11-
using CppSharp.Generators.Emscripten;
12-
using CppSharp.Generators.TS;
137
using CppSharp.Parser;
148
using CppSharp.Passes;
159
using CppSharp.Utils;
@@ -32,31 +26,6 @@ public Driver(DriverOptions options)
3226
ParserOptions = new ParserOptions();
3327
}
3428

35-
Generator CreateGeneratorFromKind(GeneratorKind kind)
36-
{
37-
switch (kind)
38-
{
39-
case GeneratorKind.C:
40-
return new CGenerator(Context);
41-
case GeneratorKind.CPlusPlus:
42-
return new CppGenerator(Context);
43-
case GeneratorKind.CLI:
44-
return new CLIGenerator(Context);
45-
case GeneratorKind.CSharp:
46-
return new CSharpGenerator(Context);
47-
case GeneratorKind.Emscripten:
48-
return new EmscriptenGenerator(Context);
49-
case GeneratorKind.QuickJS:
50-
return new QuickJSGenerator(Context);
51-
case GeneratorKind.NAPI:
52-
return new NAPIGenerator(Context);
53-
case GeneratorKind.TypeScript:
54-
return new TSGenerator(Context);
55-
}
56-
57-
throw new NotImplementedException();
58-
}
59-
6029
void ValidateOptions()
6130
{
6231
if (!Options.Compilation.Platform.HasValue)
@@ -87,7 +56,7 @@ public void Setup()
8756
ValidateOptions();
8857
ParserOptions.Setup(Platform.Host);
8958
Context = new BindingContext(Options, ParserOptions);
90-
Generator = CreateGeneratorFromKind(Options.GeneratorKind);
59+
Generator = Options.GeneratorKind.CreateGenerator(Context);
9160
}
9261

9362
public void SetupTypeMaps() =>
@@ -387,8 +356,7 @@ public bool CompileCode(Module module)
387356
out int error, out string errorMessage);
388357
if (error == 0)
389358
{
390-
Diagnostics.Message($@"Compilation succeeded: {
391-
LibraryMappings[module] = Path.Combine(
359+
Diagnostics.Message($@"Compilation succeeded: {LibraryMappings[module] = Path.Combine(
392360
Options.OutputDir, $"{module.LibraryName}.dll")}.");
393361
return true;
394362
}

src/Generator/Generator.cs

-18
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,6 @@
55

66
namespace CppSharp.Generators
77
{
8-
/// <summary>
9-
/// Kinds of language generators.
10-
/// </summary>
11-
public enum GeneratorKind
12-
{
13-
CLI = 1,
14-
CSharp = 2,
15-
C,
16-
CPlusPlus,
17-
Emscripten,
18-
ObjectiveC,
19-
Java,
20-
Swift,
21-
QuickJS,
22-
NAPI,
23-
TypeScript
24-
}
25-
268
/// <summary>
279
/// Output generated by each backend generator.
2810
/// </summary>

src/Generator/GeneratorKind.cs

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using CppSharp.AST;
2+
using CppSharp.Generators.C;
3+
using CppSharp.Generators.CLI;
4+
using CppSharp.Generators.Cpp;
5+
using CppSharp.Generators.CSharp;
6+
using CppSharp.Generators.Emscripten;
7+
using CppSharp.Generators.TS;
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
12+
namespace CppSharp.Generators
13+
{
14+
/// <summary>
15+
/// Kinds of language generators.
16+
/// </summary>
17+
public class GeneratorKind : IEquatable<GeneratorKind>
18+
{
19+
public static readonly HashSet<GeneratorKind> Registered = new();
20+
21+
public string ID { get; }
22+
public string Name { get; }
23+
public System.Type Type { get; }
24+
public string[] CLIOptions { get; }
25+
26+
public GeneratorKind(string id, string name, System.Type type, string[] cLIOptions = null)
27+
{
28+
if (Registered.Any(kind => kind.ID == id))
29+
{
30+
throw new Exception($"GeneratorKind has an already registered ID: {ID}");
31+
}
32+
ID = id;
33+
Name = name;
34+
Type = type;
35+
CLIOptions = cLIOptions;
36+
Registered.Add(this);
37+
}
38+
39+
public Generator CreateGenerator(BindingContext context)
40+
{
41+
return (Generator)Activator.CreateInstance(Type, context);
42+
}
43+
44+
public bool IsCLIOptionMatch(string cliOption)
45+
{
46+
if (CLIOptions == null)
47+
{
48+
return false;
49+
}
50+
return CLIOptions.Any(cliOption.Contains);
51+
}
52+
53+
public static bool operator ==(GeneratorKind obj1, GeneratorKind obj2)
54+
{
55+
if (ReferenceEquals(obj1, obj2))
56+
{
57+
return true;
58+
}
59+
if (obj1 is null)
60+
{
61+
return false;
62+
}
63+
if (obj2 is null)
64+
{
65+
return false;
66+
}
67+
return obj1.Equals(obj2);
68+
}
69+
70+
public static bool operator !=(GeneratorKind obj1, GeneratorKind obj2) => !(obj1 == obj2);
71+
72+
public bool Equals(GeneratorKind other)
73+
{
74+
if (other is null)
75+
{
76+
return false;
77+
}
78+
if (ReferenceEquals(this, other))
79+
{
80+
return true;
81+
}
82+
return ID.Equals(other.ID);
83+
}
84+
85+
public override bool Equals(object obj) => Equals(obj as GeneratorKind);
86+
87+
public override int GetHashCode()
88+
{
89+
unchecked
90+
{
91+
return ID.GetHashCode();
92+
}
93+
}
94+
95+
public const string CLI_ID = "CLI";
96+
public static readonly GeneratorKind CLI = new(CLI_ID, "C++/CLI", typeof(CLIGenerator), new[] { "cli" });
97+
98+
public const string CSharp_ID = "CSharp";
99+
public static readonly GeneratorKind CSharp = new(CSharp_ID, "C#", typeof(CSharpGenerator), new[] { "csharp" });
100+
101+
public const string C_ID = "C";
102+
public static readonly GeneratorKind C = new(C_ID, "C", typeof(CGenerator), new[] { "c" });
103+
104+
public const string CPlusPlus_ID = "CPlusPlus";
105+
public static readonly GeneratorKind CPlusPlus = new(CPlusPlus_ID, "CPlusPlus", typeof(CppGenerator), new[] { "cpp" });
106+
107+
public const string Emscripten_ID = "Emscripten";
108+
public static readonly GeneratorKind Emscripten = new(Emscripten_ID, "Emscripten", typeof(EmscriptenGenerator), new[] { "emscripten" });
109+
110+
public const string ObjectiveC_ID = "ObjectiveC";
111+
public static readonly GeneratorKind ObjectiveC = new(ObjectiveC_ID, "ObjectiveC", typeof(NotImplementedGenerator));
112+
113+
public const string Java_ID = "Java";
114+
public static readonly GeneratorKind Java = new(Java_ID, "Java", typeof(NotImplementedGenerator));
115+
116+
public const string Swift_ID = "Swift";
117+
public static readonly GeneratorKind Swift = new(Swift_ID, "Swift", typeof(NotImplementedGenerator));
118+
119+
public const string QuickJS_ID = "QuickJS";
120+
public static readonly GeneratorKind QuickJS = new(QuickJS_ID, "QuickJS", typeof(QuickJSGenerator), new[] { "qjs" });
121+
122+
public const string NAPI_ID = "NAPI";
123+
public static readonly GeneratorKind NAPI = new(NAPI_ID, "N-API", typeof(NAPIGenerator), new[] { "napi" });
124+
125+
public const string TypeScript_ID = "TypeScript";
126+
public static readonly GeneratorKind TypeScript = new(TypeScript_ID, "TypeScript", typeof(TSGenerator), new[] { "ts", "typescript" });
127+
}
128+
129+
public class NotImplementedGenerator : Generator
130+
{
131+
public NotImplementedGenerator(BindingContext context) : base(context)
132+
{
133+
throw new NotImplementedException();
134+
}
135+
136+
public override List<CodeGenerator> Generate(IEnumerable<TranslationUnit> units)
137+
{
138+
throw new NotImplementedException();
139+
}
140+
141+
public override bool SetupPasses()
142+
{
143+
throw new NotImplementedException();
144+
}
145+
146+
protected override string TypePrinterDelegate(CppSharp.AST.Type type)
147+
{
148+
throw new NotImplementedException();
149+
}
150+
}
151+
}

src/Generator/Generators/ExtensionMethods.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ public static Type GetMappedType(this Type type, TypeMapDatabase typeMaps,
6565

6666
switch (generatorKind)
6767
{
68-
case GeneratorKind.CLI:
68+
case var _ when ReferenceEquals(generatorKind, GeneratorKind.CLI):
6969
return typeMap.CLISignatureType(typePrinterContext).Desugar();
70-
case GeneratorKind.CSharp:
70+
case var _ when ReferenceEquals(generatorKind, GeneratorKind.CSharp):
7171
return typeMap.CSharpSignatureType(typePrinterContext).Desugar();
7272
}
7373
}

src/Generator/Passes/CheckDuplicatedNamesPass.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,22 @@ private TypePrinter GetTypePrinter(GeneratorKind kind, BindingContext context)
202202
TypePrinter typePrinter;
203203
switch (kind)
204204
{
205-
case GeneratorKind.C:
205+
case var _ when ReferenceEquals(kind, GeneratorKind.C):
206206
typePrinter = new CppTypePrinter(Context) { PrintFlavorKind = CppTypePrintFlavorKind.C };
207207
break;
208-
case GeneratorKind.Emscripten:
208+
case var _ when ReferenceEquals(kind, GeneratorKind.Emscripten):
209209
typePrinter = new EmscriptenTypePrinter(Context);
210210
break;;
211-
case GeneratorKind.CPlusPlus:
212-
case GeneratorKind.QuickJS:
213-
case GeneratorKind.NAPI:
214-
case GeneratorKind.TypeScript:
211+
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
212+
case var _ when ReferenceEquals(kind, GeneratorKind.QuickJS):
213+
case var _ when ReferenceEquals(kind, GeneratorKind.NAPI):
214+
case var _ when ReferenceEquals(kind, GeneratorKind.TypeScript):
215215
typePrinter = new CppTypePrinter(Context);
216216
break;
217-
case GeneratorKind.CLI:
217+
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
218218
typePrinter = new CLITypePrinter(Context);
219219
break;
220-
case GeneratorKind.CSharp:
220+
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
221221
typePrinter = new CSharpTypePrinter(Context);
222222
break;
223223
default:

src/Generator/Passes/ValidateOperatorsPass.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,12 @@ private bool IsValidOperatorOverload(Method @operator)
8383
{
8484
Parameter parameter = @operator.Parameters.Last();
8585
Type type = parameter.Type.Desugar();
86-
switch (Options.GeneratorKind)
86+
var kind = Options.GeneratorKind;
87+
switch (kind)
8788
{
88-
case GeneratorKind.CLI:
89+
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
8990
return type.IsPrimitiveType(PrimitiveType.Int);
90-
case GeneratorKind.CSharp:
91+
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
9192
Types.TypeMap typeMap;
9293
if (Context.TypeMaps.FindTypeMap(type, out typeMap))
9394
{

src/Generator/Types/DeclMap.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class DeclMapAttribute : Attribute
3535
{
3636
public GeneratorKind GeneratorKind { get; set; }
3737

38-
public DeclMapAttribute() : this(0)
38+
public DeclMapAttribute()
3939
{
4040
}
4141

0 commit comments

Comments
 (0)