Browse Source

Add Type property for GenerationKind + cleanup

pull/1794/head
Deadlocklogic 2 years ago
parent
commit
0c92a5c1d6
  1. 36
      src/Generator/Driver.cs
  2. 65
      src/Generator/GeneratorKind.cs

36
src/Generator/Driver.cs

@ -4,12 +4,6 @@ using System.IO; @@ -4,12 +4,6 @@ using System.IO;
using System.Linq;
using CppSharp.AST;
using CppSharp.Generators;
using CppSharp.Generators.C;
using CppSharp.Generators.CLI;
using CppSharp.Generators.Cpp;
using CppSharp.Generators.CSharp;
using CppSharp.Generators.Emscripten;
using CppSharp.Generators.TS;
using CppSharp.Parser;
using CppSharp.Passes;
using CppSharp.Utils;
@ -32,31 +26,6 @@ namespace CppSharp @@ -32,31 +26,6 @@ namespace CppSharp
ParserOptions = new ParserOptions();
}
Generator CreateGeneratorFromKind(GeneratorKind kind)
{
switch (kind)
{
case var _ when ReferenceEquals(kind, GeneratorKind.C):
return new CGenerator(Context);
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
return new CppGenerator(Context);
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
return new CLIGenerator(Context);
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
return new CSharpGenerator(Context);
case var _ when ReferenceEquals(kind, GeneratorKind.Emscripten):
return new EmscriptenGenerator(Context);
case var _ when ReferenceEquals(kind, GeneratorKind.QuickJS):
return new QuickJSGenerator(Context);
case var _ when ReferenceEquals(kind, GeneratorKind.NAPI):
return new NAPIGenerator(Context);
case var _ when ReferenceEquals(kind, GeneratorKind.TypeScript):
return new TSGenerator(Context);
}
throw new NotImplementedException();
}
void ValidateOptions()
{
if (!Options.Compilation.Platform.HasValue)
@ -87,7 +56,7 @@ namespace CppSharp @@ -87,7 +56,7 @@ namespace CppSharp
ValidateOptions();
ParserOptions.Setup(Platform.Host);
Context = new BindingContext(Options, ParserOptions);
Generator = CreateGeneratorFromKind(Options.GeneratorKind);
Generator = Options.GeneratorKind.CreateGenerator(Context);
}
public void SetupTypeMaps() =>
@ -387,8 +356,7 @@ namespace CppSharp @@ -387,8 +356,7 @@ namespace CppSharp
out int error, out string errorMessage);
if (error == 0)
{
Diagnostics.Message($@"Compilation succeeded: {
LibraryMappings[module] = Path.Combine(
Diagnostics.Message($@"Compilation succeeded: {LibraryMappings[module] = Path.Combine(
Options.OutputDir, $"{module.LibraryName}.dll")}.");
return true;
}

65
src/Generator/GeneratorKind.cs

@ -1,7 +1,12 @@ @@ -1,7 +1,12 @@
using CppSharp.AST;
using CppSharp.Generators.C;
using CppSharp.Generators.CLI;
using CppSharp.Generators.Cpp;
using CppSharp.Generators.CSharp;
using CppSharp.Generators.Emscripten;
using CppSharp.Generators.TS;
using System;
using System.Collections.Generic;
using System.Linq;
using CppSharp.AST;
namespace CppSharp.Generators
{
@ -13,15 +18,22 @@ namespace CppSharp.Generators @@ -13,15 +18,22 @@ namespace CppSharp.Generators
private static readonly HashSet<string> s_registeredIDSet = new();
public string ID { get; }
public System.Type Type { get; }
public GeneratorKind(string id)
public GeneratorKind(string id, System.Type type)
{
if (s_registeredIDSet.Contains(id))
{
throw new Exception($"GeneratorKind has an already registered ID: {ID}");
}
ID = id;
s_registeredIDSet.Add(id);
ID = id;
Type = type;
}
public Generator CreateGenerator(BindingContext context)
{
return (Generator)Activator.CreateInstance(Type, context);
}
public static bool operator ==(GeneratorKind obj1, GeneratorKind obj2)
@ -67,36 +79,59 @@ namespace CppSharp.Generators @@ -67,36 +79,59 @@ namespace CppSharp.Generators
}
public const string CLI_ID = "CLI";
public static readonly GeneratorKind CLI = new(CLI_ID);
public static readonly GeneratorKind CLI = new(CLI_ID, typeof(CLIGenerator));
public const string CSharp_ID = "CSharp";
public static readonly GeneratorKind CSharp = new(CSharp_ID);
public static readonly GeneratorKind CSharp = new(CSharp_ID, typeof(CSharpGenerator));
public const string C_ID = "C";
public static readonly GeneratorKind C = new(C_ID);
public static readonly GeneratorKind C = new(C_ID, typeof(CGenerator));
public const string CPlusPlus_ID = "CPlusPlus";
public static readonly GeneratorKind CPlusPlus = new(CPlusPlus_ID);
public static readonly GeneratorKind CPlusPlus = new(CPlusPlus_ID, typeof(CppGenerator));
public const string Emscripten_ID = "Emscripten";
public static readonly GeneratorKind Emscripten = new(Emscripten_ID);
public static readonly GeneratorKind Emscripten = new(Emscripten_ID, typeof(EmscriptenGenerator));
public const string ObjectiveC_ID = "ObjectiveC";
public static readonly GeneratorKind ObjectiveC = new(ObjectiveC_ID);
public static readonly GeneratorKind ObjectiveC = new(ObjectiveC_ID, typeof(NotImplementedGenerator));
public const string Java_ID = "Java";
public static readonly GeneratorKind Java = new(Java_ID);
public static readonly GeneratorKind Java = new(Java_ID, typeof(NotImplementedGenerator));
public const string Swift_ID = "Swift";
public static readonly GeneratorKind Swift = new(Swift_ID);
public static readonly GeneratorKind Swift = new(Swift_ID, typeof(NotImplementedGenerator));
public const string QuickJS_ID = "QuickJS";
public static readonly GeneratorKind QuickJS = new(QuickJS_ID);
public static readonly GeneratorKind QuickJS = new(QuickJS_ID, typeof(QuickJSGenerator));
public const string NAPI_ID = "NAPI";
public static readonly GeneratorKind NAPI = new(NAPI_ID);
public static readonly GeneratorKind NAPI = new(NAPI_ID, typeof(NAPIGenerator));
public const string TypeScript_ID = "TypeScript";
public static readonly GeneratorKind TypeScript = new(TypeScript_ID);
public static readonly GeneratorKind TypeScript = new(TypeScript_ID, typeof(TSGenerator));
}
public class NotImplementedGenerator : Generator
{
public NotImplementedGenerator(BindingContext context) : base(context)
{
throw new NotImplementedException();
}
public override List<CodeGenerator> Generate(IEnumerable<TranslationUnit> units)
{
throw new NotImplementedException();
}
public override bool SetupPasses()
{
throw new NotImplementedException();
}
protected override string TypePrinterDelegate(CppSharp.AST.Type type)
{
throw new NotImplementedException();
}
}
}
Loading…
Cancel
Save