diff --git a/src/Generator/Generator.cs b/src/Generator/Generator.cs index 1593a85509..42a8ba69a5 100644 --- a/src/Generator/Generator.cs +++ b/src/Generator/Generator.cs @@ -28,9 +28,12 @@ public abstract class Generator : IDisposable { public BindingContext Context { get; } + protected readonly TypePrinter typePrinter; + protected Generator(BindingContext context) { Context = context; + typePrinter = Context.Options.GeneratorKind.CreateTypePrinter(context); CppSharp.AST.Type.TypePrinterDelegate += TypePrinterDelegate; } @@ -155,7 +158,10 @@ public virtual GeneratorOutput GenerateModule(Module module) return output; } - protected abstract string TypePrinterDelegate(CppSharp.AST.Type type); + protected virtual string TypePrinterDelegate(CppSharp.AST.Type type) + { + return type.Visit(typePrinter); + } public static string GeneratedIdentifier(string id) => $"__{(id.StartsWith("@") ? id.Substring(1) : id)}"; diff --git a/src/Generator/GeneratorKind.cs b/src/Generator/GeneratorKind.cs index 222420404e..9307452f59 100644 --- a/src/Generator/GeneratorKind.cs +++ b/src/Generator/GeneratorKind.cs @@ -20,10 +20,11 @@ public class GeneratorKind : IEquatable public string ID { get; } public string Name { get; } - public System.Type Type { get; } + public System.Type GeneratorType { get; } + public System.Type TypePrinterType { get; } public string[] CLIOptions { get; } - public GeneratorKind(string id, string name, System.Type type, string[] cLIOptions = null) + public GeneratorKind(string id, string name, System.Type generatorType, System.Type typePrinterType, string[] cLIOptions = null) { if (Registered.Any(kind => kind.ID == id)) { @@ -31,14 +32,20 @@ public GeneratorKind(string id, string name, System.Type type, string[] cLIOptio } ID = id; Name = name; - Type = type; + GeneratorType = generatorType; + TypePrinterType = typePrinterType; CLIOptions = cLIOptions; Registered.Add(this); } public Generator CreateGenerator(BindingContext context) { - return (Generator)Activator.CreateInstance(Type, context); + return (Generator)Activator.CreateInstance(GeneratorType, context); + } + + public TypePrinter CreateTypePrinter(BindingContext context) + { + return (TypePrinter)Activator.CreateInstance(TypePrinterType, context); } public bool IsCLIOptionMatch(string cliOption) @@ -93,37 +100,44 @@ public override int GetHashCode() } public const string CLI_ID = "CLI"; - public static readonly GeneratorKind CLI = new(CLI_ID, "C++/CLI", typeof(CLIGenerator), new[] { "cli" }); + public static readonly GeneratorKind CLI = new(CLI_ID, "C++/CLI", typeof(CLIGenerator), typeof(CLITypePrinter), new[] { "cli" }); public const string CSharp_ID = "CSharp"; - public static readonly GeneratorKind CSharp = new(CSharp_ID, "C#", typeof(CSharpGenerator), new[] { "csharp" }); + public static readonly GeneratorKind CSharp = new(CSharp_ID, "C#", typeof(CSharpGenerator), typeof(CSharpTypePrinter), new[] { "csharp" }); public const string C_ID = "C"; - public static readonly GeneratorKind C = new(C_ID, "C", typeof(CGenerator), new[] { "c" }); + public static readonly GeneratorKind C = new(C_ID, "C", typeof(CGenerator), typeof(CppTypePrinter), new[] { "c" }); public const string CPlusPlus_ID = "CPlusPlus"; - public static readonly GeneratorKind CPlusPlus = new(CPlusPlus_ID, "CPlusPlus", typeof(CppGenerator), new[] { "cpp" }); + public static readonly GeneratorKind CPlusPlus = new(CPlusPlus_ID, "CPlusPlus", typeof(CppGenerator), typeof(CppTypePrinter), new[] { "cpp" }); public const string Emscripten_ID = "Emscripten"; - public static readonly GeneratorKind Emscripten = new(Emscripten_ID, "Emscripten", typeof(EmscriptenGenerator), new[] { "emscripten" }); + public static readonly GeneratorKind Emscripten = new(Emscripten_ID, "Emscripten", typeof(EmscriptenGenerator), typeof(EmscriptenTypePrinter), new[] { "emscripten" }); public const string ObjectiveC_ID = "ObjectiveC"; - public static readonly GeneratorKind ObjectiveC = new(ObjectiveC_ID, "ObjectiveC", typeof(NotImplementedGenerator)); + public static readonly GeneratorKind ObjectiveC = new(ObjectiveC_ID, "ObjectiveC", typeof(NotImplementedGenerator), typeof(NotImplementedTypePrinter)); public const string Java_ID = "Java"; - public static readonly GeneratorKind Java = new(Java_ID, "Java", typeof(NotImplementedGenerator)); + public static readonly GeneratorKind Java = new(Java_ID, "Java", typeof(NotImplementedGenerator), typeof(NotImplementedTypePrinter)); public const string Swift_ID = "Swift"; - public static readonly GeneratorKind Swift = new(Swift_ID, "Swift", typeof(NotImplementedGenerator)); + public static readonly GeneratorKind Swift = new(Swift_ID, "Swift", typeof(NotImplementedGenerator), typeof(NotImplementedTypePrinter)); public const string QuickJS_ID = "QuickJS"; - public static readonly GeneratorKind QuickJS = new(QuickJS_ID, "QuickJS", typeof(QuickJSGenerator), new[] { "qjs" }); + public static readonly GeneratorKind QuickJS = new(QuickJS_ID, "QuickJS", typeof(QuickJSGenerator), typeof(QuickJSTypePrinter), new[] { "qjs" }); public const string NAPI_ID = "NAPI"; - public static readonly GeneratorKind NAPI = new(NAPI_ID, "N-API", typeof(NAPIGenerator), new[] { "napi" }); + public static readonly GeneratorKind NAPI = new(NAPI_ID, "N-API", typeof(NAPIGenerator), typeof(NAPITypePrinter), new[] { "napi" }); public const string TypeScript_ID = "TypeScript"; - public static readonly GeneratorKind TypeScript = new(TypeScript_ID, "TypeScript", typeof(TSGenerator), new[] { "ts", "typescript" }); + public static readonly GeneratorKind TypeScript = new(TypeScript_ID, "TypeScript", typeof(TSGenerator), typeof(TSTypePrinter), new[] { "ts", "typescript" }); + } + + public class NotImplementedTypePrinter : TypePrinter + { + public NotImplementedTypePrinter(BindingContext context) : base(context) + { + } } public class NotImplementedGenerator : Generator @@ -142,10 +156,5 @@ public override bool SetupPasses() { throw new NotImplementedException(); } - - protected override string TypePrinterDelegate(CppSharp.AST.Type type) - { - throw new NotImplementedException(); - } } } diff --git a/src/Generator/Generators/C/CGenerator.cs b/src/Generator/Generators/C/CGenerator.cs index 60c1dc8184..07884929cf 100644 --- a/src/Generator/Generators/C/CGenerator.cs +++ b/src/Generator/Generators/C/CGenerator.cs @@ -10,11 +10,8 @@ namespace CppSharp.Generators.C /// public class CGenerator : Generator { - private readonly CppTypePrinter typePrinter; - public CGenerator(BindingContext context) : base(context) { - typePrinter = new CppTypePrinter(Context); } public override List Generate(IEnumerable units) @@ -31,10 +28,5 @@ public override List Generate(IEnumerable units) } public override bool SetupPasses() => true; - - protected override string TypePrinterDelegate(Type type) - { - return type.Visit(typePrinter).ToString(); - } } } diff --git a/src/Generator/Generators/C/CppGenerator.cs b/src/Generator/Generators/C/CppGenerator.cs index 73a2aa8dc9..f02de85fbc 100644 --- a/src/Generator/Generators/C/CppGenerator.cs +++ b/src/Generator/Generators/C/CppGenerator.cs @@ -11,11 +11,8 @@ namespace CppSharp.Generators.Cpp /// public class CppGenerator : CGenerator { - private readonly CppTypePrinter typePrinter; - public CppGenerator(BindingContext context) : base(context) { - typePrinter = new CppTypePrinter(Context); } public override List Generate(IEnumerable units) @@ -44,11 +41,6 @@ public static bool ShouldGenerateClassNativeInstanceField(Class @class) return @class.IsRefType && (!@class.HasBase || !@class.HasRefBase()); } - - protected override string TypePrinterDelegate(Type type) - { - return type.Visit(typePrinter).ToString(); - } } /// diff --git a/src/Generator/Generators/C/CppTypePrinter.cs b/src/Generator/Generators/C/CppTypePrinter.cs index f289705f76..edfcdcd911 100644 --- a/src/Generator/Generators/C/CppTypePrinter.cs +++ b/src/Generator/Generators/C/CppTypePrinter.cs @@ -28,18 +28,13 @@ public class CppTypePrinter : TypePrinter public TypePrintScopeKind MethodScopeKind = TypePrintScopeKind.Qualified; - public CppTypePrinter() : base(TypePrinterContextKind.Native) + public CppTypePrinter(BindingContext context) : base(context, TypePrinterContextKind.Native) { PrintFlavorKind = CppTypePrintFlavorKind.Cpp; PrintTypeQualifiers = true; PrintTypeModifiers = true; } - public CppTypePrinter(BindingContext context) : this() - { - Context = context; - } - public TypeMapDatabase TypeMapDatabase => Context.TypeMaps; public DriverOptions Options => Context.Options; diff --git a/src/Generator/Generators/CLI/CLIGenerator.cs b/src/Generator/Generators/CLI/CLIGenerator.cs index 716095ac74..b3f93eb827 100644 --- a/src/Generator/Generators/CLI/CLIGenerator.cs +++ b/src/Generator/Generators/CLI/CLIGenerator.cs @@ -10,11 +10,8 @@ namespace CppSharp.Generators.CLI /// public class CLIGenerator : Generator { - private readonly CppTypePrinter typePrinter; - public CLIGenerator(BindingContext context) : base(context) { - typePrinter = new CLITypePrinter(context); } public override List Generate(IEnumerable units) @@ -39,10 +36,5 @@ public static bool ShouldGenerateClassNativeField(Class @class) return @class.IsRefType && (!@class.NeedsBase || !@class.HasRefBase()); } - - protected override string TypePrinterDelegate(Type type) - { - return type.Visit(typePrinter).ToString(); - } } } \ No newline at end of file diff --git a/src/Generator/Generators/CSharp/CSharpGenerator.cs b/src/Generator/Generators/CSharp/CSharpGenerator.cs index 5d6da4749b..611f3c6bdc 100644 --- a/src/Generator/Generators/CSharp/CSharpGenerator.cs +++ b/src/Generator/Generators/CSharp/CSharpGenerator.cs @@ -7,18 +7,15 @@ namespace CppSharp.Generators.CSharp { public class CSharpGenerator : Generator { - private readonly CSharpTypePrinter typePrinter; - public CSharpGenerator(BindingContext context) : base(context) { - typePrinter = new CSharpTypePrinter(context); } public override List Generate(IEnumerable units) { var outputs = new List(); - var gen = new CSharpSources(Context, units) { TypePrinter = typePrinter }; + var gen = new CSharpSources(Context, units) { TypePrinter = (CSharpTypePrinter)typePrinter }; outputs.Add(gen); return outputs; @@ -42,10 +39,5 @@ public override bool SetupPasses() return true; } - - protected override string TypePrinterDelegate(Type type) - { - return type.Visit(typePrinter); - } } } diff --git a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs index 854b4e5434..1453adb1fa 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -21,15 +21,10 @@ public class CSharpTypePrinter : TypePrinter public bool PrintModuleOutputNamespace = true; - public CSharpTypePrinter() + public CSharpTypePrinter(BindingContext context) : base(context) { } - public CSharpTypePrinter(BindingContext context) - { - Context = context; - } - public string QualifiedType(string name) { return IsGlobalQualifiedScope ? $"global::{name}" : name; diff --git a/src/Generator/Generators/Marshal.cs b/src/Generator/Generators/Marshal.cs index b1798fdbe4..1dcd9dced3 100644 --- a/src/Generator/Generators/Marshal.cs +++ b/src/Generator/Generators/Marshal.cs @@ -1,13 +1,13 @@ using CppSharp.AST; using CppSharp.Generators.C; +using System; namespace CppSharp.Generators { public class MarshalContext : TypePrinter { - public MarshalContext(BindingContext context, uint indentation) + public MarshalContext(BindingContext context, uint indentation) : base(context) { - Context = context; Before = new TextGenerator { CurrentIndentation = indentation }; Return = new TextGenerator { CurrentIndentation = indentation }; Cleanup = new TextGenerator { CurrentIndentation = indentation }; @@ -34,16 +34,16 @@ public MarshalContext(BindingContext context, uint indentation) public uint Indentation { get; } } - public abstract class MarshalPrinter : AstVisitor where C : MarshalContext where P : TypePrinter, new() + public abstract class MarshalPrinter : AstVisitor where C : MarshalContext where P : TypePrinter { public C Context { get; } protected MarshalPrinter(C ctx) { Context = ctx; - typePrinter.Context = ctx.Context; + typePrinter = (P)Activator.CreateInstance(typeof(P), ctx.Context); } - protected P typePrinter = new P(); + protected P typePrinter; } } diff --git a/src/Generator/Generators/NAPI/NAPITypePrinter.cs b/src/Generator/Generators/NAPI/NAPITypePrinter.cs new file mode 100644 index 0000000000..55109863b7 --- /dev/null +++ b/src/Generator/Generators/NAPI/NAPITypePrinter.cs @@ -0,0 +1,11 @@ +using CppSharp.Generators.C; + +namespace CppSharp.Generators.C +{ + public class NAPITypePrinter : CppTypePrinter + { + public NAPITypePrinter(BindingContext context) : base(context) + { + } + } +} diff --git a/src/Generator/Generators/TS/TSGenerator.cs b/src/Generator/Generators/TS/TSGenerator.cs index fa45267160..b4e3402c45 100644 --- a/src/Generator/Generators/TS/TSGenerator.cs +++ b/src/Generator/Generators/TS/TSGenerator.cs @@ -11,11 +11,8 @@ namespace CppSharp.Generators.TS /// public class TSGenerator : CGenerator { - private readonly TSTypePrinter typePrinter; - public TSGenerator(BindingContext context) : base(context) { - typePrinter = new TSTypePrinter(Context); } public override List Generate(IEnumerable units) @@ -32,10 +29,5 @@ public override bool SetupPasses() { return true; } - - protected override string TypePrinterDelegate(Type type) - { - return type.Visit(typePrinter).ToString(); - } } } diff --git a/src/Generator/Generators/TypePrinter.cs b/src/Generator/Generators/TypePrinter.cs index 806a719217..136b74d304 100644 --- a/src/Generator/Generators/TypePrinter.cs +++ b/src/Generator/Generators/TypePrinter.cs @@ -64,8 +64,9 @@ public class TypePrinter : ITypePrinter, public TypePrintScopeKind ScopeKind => scopeKinds.Peek(); public bool IsGlobalQualifiedScope => ScopeKind == TypePrintScopeKind.GlobalQualified; - public TypePrinter(TypePrinterContextKind contextKind = TypePrinterContextKind.Managed) + public TypePrinter(BindingContext context, TypePrinterContextKind contextKind = TypePrinterContextKind.Managed) { + Context = context; contexts = new Stack(); marshalKinds = new Stack(); scopeKinds = new Stack(); diff --git a/src/Generator/Passes/CheckDuplicatedNamesPass.cs b/src/Generator/Passes/CheckDuplicatedNamesPass.cs index 0a520be282..13d4bc2f29 100644 --- a/src/Generator/Passes/CheckDuplicatedNamesPass.cs +++ b/src/Generator/Passes/CheckDuplicatedNamesPass.cs @@ -4,10 +4,6 @@ using CppSharp.AST; using CppSharp.AST.Extensions; using CppSharp.Generators; -using CppSharp.Generators.C; -using CppSharp.Generators.CLI; -using CppSharp.Generators.CSharp; -using CppSharp.Generators.Emscripten; using CppSharp.Types; namespace CppSharp.Passes @@ -190,43 +186,12 @@ public class CheckDuplicatedNamesPass : TranslationUnitPass public override bool VisitASTContext(ASTContext context) { - var typePrinter = GetTypePrinter(Options.GeneratorKind, Context); - DeclarationName.ParameterTypeComparer.TypePrinter = typePrinter; + DeclarationName.ParameterTypeComparer.TypePrinter = Options.GeneratorKind.CreateTypePrinter(Context); 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 var _ when ReferenceEquals(kind, GeneratorKind.C): - typePrinter = new CppTypePrinter(Context) { PrintFlavorKind = CppTypePrintFlavorKind.C }; - break; - case var _ when ReferenceEquals(kind, GeneratorKind.Emscripten): - typePrinter = new EmscriptenTypePrinter(Context); - break;; - case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): - case var _ when ReferenceEquals(kind, GeneratorKind.QuickJS): - case var _ when ReferenceEquals(kind, GeneratorKind.NAPI): - case var _ when ReferenceEquals(kind, GeneratorKind.TypeScript): - typePrinter = new CppTypePrinter(Context); - break; - case var _ when ReferenceEquals(kind, GeneratorKind.CLI): - typePrinter = new CLITypePrinter(Context); - break; - case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): - typePrinter = new CSharpTypePrinter(Context); - break; - default: - throw new System.NotImplementedException(); - } - - return typePrinter; - } - public override bool VisitProperty(Property decl) { if (!VisitDeclaration(decl))