Browse Source

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
pull/1796/head
deadlocklogic 2 years ago committed by GitHub
parent
commit
0edd48c6a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      src/CLI/CLI.cs
  2. 17
      src/CLI/Generator.cs
  3. 8
      src/CppParser/Bootstrap/Bootstrap.cs
  4. 36
      src/Generator/Driver.cs
  5. 18
      src/Generator/Generator.cs
  6. 151
      src/Generator/GeneratorKind.cs
  7. 4
      src/Generator/Generators/ExtensionMethods.cs
  8. 16
      src/Generator/Passes/CheckDuplicatedNamesPass.cs
  9. 7
      src/Generator/Passes/ValidateOperatorsPass.cs
  10. 2
      src/Generator/Types/DeclMap.cs
  11. 2
      src/Generator/Types/DeclMapDatabase.cs
  12. 26
      src/Generator/Types/Std/Stdlib.CLI.cs
  13. 28
      src/Generator/Types/Std/Stdlib.CSharp.cs
  14. 7
      src/Generator/Types/TypeIgnoreChecker.cs
  15. 32
      src/Generator/Types/TypeMap.cs
  16. 4
      src/Generator/Types/TypeMapDatabase.cs

30
src/CLI/CLI.cs

@ -206,33 +206,13 @@ namespace CppSharp @@ -206,33 +206,13 @@ namespace CppSharp
static void GetGeneratorKind(string generator, List<string> errorMessages)
{
switch (generator.ToLower())
foreach (GeneratorKind generatorKind in GeneratorKind.Registered)
{
case "csharp":
options.Kind = GeneratorKind.CSharp;
return;
case "cli":
options.Kind = GeneratorKind.CLI;
return;
case "c":
options.Kind = GeneratorKind.C;
return;
case "cpp":
options.Kind = GeneratorKind.CPlusPlus;
return;
case "napi":
options.Kind = GeneratorKind.NAPI;
return;
case "qjs":
options.Kind = GeneratorKind.QuickJS;
return;
case "ts":
case "typescript":
options.Kind = GeneratorKind.TypeScript;
return;
case "emscripten":
options.Kind = GeneratorKind.Emscripten;
if (generatorKind.IsCLIOptionMatch(generator.ToLower()))
{
options.Kind = generatorKind;
return;
}
}
errorMessages.Add($"Unknown generator kind: {generator}.");

17
src/CLI/Generator.cs

@ -198,7 +198,7 @@ namespace CppSharp @@ -198,7 +198,7 @@ namespace CppSharp
public void Run()
{
var messageBuilder = new StringBuilder();
messageBuilder.Append($"Generating {GetGeneratorKindName(options.Kind)}");
messageBuilder.Append($"Generating {options.Kind.Name}");
messageBuilder.Append($" bindings for {GetPlatformName(options.Platform)} {options.Architecture}");
if (options.Cpp11ABI)
@ -225,20 +225,5 @@ namespace CppSharp @@ -225,20 +225,5 @@ namespace CppSharp
return platform.ToString();
}
}
private static string GetGeneratorKindName(GeneratorKind kind)
{
switch (kind)
{
case GeneratorKind.CLI:
return "C++/CLI";
case GeneratorKind.CSharp:
return "C#";
case GeneratorKind.NAPI:
return "N-API";
default:
return kind.ToString();
}
}
}
}

8
src/CppParser/Bootstrap/Bootstrap.cs

@ -1778,8 +1778,7 @@ namespace CppSharp @@ -1778,8 +1778,7 @@ namespace CppSharp
return qualifiedName;
}
public static string GetDeclName(Declaration decl,
GeneratorKind kind = GeneratorKind.CPlusPlus)
public static string GetDeclName(Declaration decl, GeneratorKind kind)
{
string name = decl.Name;
@ -1811,6 +1810,11 @@ namespace CppSharp @@ -1811,6 +1810,11 @@ namespace CppSharp
return name;
}
public static string GetDeclName(Declaration decl)
{
return GetDeclName(decl, GeneratorKind.CPlusPlus);
}
public static AST.Type GetDeclType(AST.Type type,
TypePrinter typePrinter)
{

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 GeneratorKind.C:
return new CGenerator(Context);
case GeneratorKind.CPlusPlus:
return new CppGenerator(Context);
case GeneratorKind.CLI:
return new CLIGenerator(Context);
case GeneratorKind.CSharp:
return new CSharpGenerator(Context);
case GeneratorKind.Emscripten:
return new EmscriptenGenerator(Context);
case GeneratorKind.QuickJS:
return new QuickJSGenerator(Context);
case GeneratorKind.NAPI:
return new NAPIGenerator(Context);
case 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;
}

18
src/Generator/Generator.cs

@ -5,24 +5,6 @@ using CppSharp.AST; @@ -5,24 +5,6 @@ using CppSharp.AST;
namespace CppSharp.Generators
{
/// <summary>
/// Kinds of language generators.
/// </summary>
public enum GeneratorKind
{
CLI = 1,
CSharp = 2,
C,
CPlusPlus,
Emscripten,
ObjectiveC,
Java,
Swift,
QuickJS,
NAPI,
TypeScript
}
/// <summary>
/// Output generated by each backend generator.
/// </summary>

151
src/Generator/GeneratorKind.cs

@ -0,0 +1,151 @@ @@ -0,0 +1,151 @@
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;
namespace CppSharp.Generators
{
/// <summary>
/// Kinds of language generators.
/// </summary>
public class GeneratorKind : IEquatable<GeneratorKind>
{
public static readonly HashSet<GeneratorKind> Registered = new();
public string ID { get; }
public string Name { get; }
public System.Type Type { get; }
public string[] CLIOptions { get; }
public GeneratorKind(string id, string name, System.Type type, string[] cLIOptions = null)
{
if (Registered.Any(kind => kind.ID == id))
{
throw new Exception($"GeneratorKind has an already registered ID: {ID}");
}
ID = id;
Name = name;
Type = type;
CLIOptions = cLIOptions;
Registered.Add(this);
}
public Generator CreateGenerator(BindingContext context)
{
return (Generator)Activator.CreateInstance(Type, context);
}
public bool IsCLIOptionMatch(string cliOption)
{
if (CLIOptions == null)
{
return false;
}
return CLIOptions.Any(cliOption.Contains);
}
public static bool operator ==(GeneratorKind obj1, GeneratorKind obj2)
{
if (ReferenceEquals(obj1, obj2))
{
return true;
}
if (obj1 is null)
{
return false;
}
if (obj2 is null)
{
return false;
}
return obj1.Equals(obj2);
}
public static bool operator !=(GeneratorKind obj1, GeneratorKind obj2) => !(obj1 == obj2);
public bool Equals(GeneratorKind other)
{
if (other is null)
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return ID.Equals(other.ID);
}
public override bool Equals(object obj) => Equals(obj as GeneratorKind);
public override int GetHashCode()
{
unchecked
{
return ID.GetHashCode();
}
}
public const string CLI_ID = "CLI";
public static readonly GeneratorKind CLI = new(CLI_ID, "C++/CLI", typeof(CLIGenerator), new[] { "cli" });
public const string CSharp_ID = "CSharp";
public static readonly GeneratorKind CSharp = new(CSharp_ID, "C#", typeof(CSharpGenerator), new[] { "csharp" });
public const string C_ID = "C";
public static readonly GeneratorKind C = new(C_ID, "C", typeof(CGenerator), new[] { "c" });
public const string CPlusPlus_ID = "CPlusPlus";
public static readonly GeneratorKind CPlusPlus = new(CPlusPlus_ID, "CPlusPlus", typeof(CppGenerator), new[] { "cpp" });
public const string Emscripten_ID = "Emscripten";
public static readonly GeneratorKind Emscripten = new(Emscripten_ID, "Emscripten", typeof(EmscriptenGenerator), new[] { "emscripten" });
public const string ObjectiveC_ID = "ObjectiveC";
public static readonly GeneratorKind ObjectiveC = new(ObjectiveC_ID, "ObjectiveC", typeof(NotImplementedGenerator));
public const string Java_ID = "Java";
public static readonly GeneratorKind Java = new(Java_ID, "Java", typeof(NotImplementedGenerator));
public const string Swift_ID = "Swift";
public static readonly GeneratorKind Swift = new(Swift_ID, "Swift", typeof(NotImplementedGenerator));
public const string QuickJS_ID = "QuickJS";
public static readonly GeneratorKind QuickJS = new(QuickJS_ID, "QuickJS", typeof(QuickJSGenerator), new[] { "qjs" });
public const string NAPI_ID = "NAPI";
public static readonly GeneratorKind NAPI = new(NAPI_ID, "N-API", typeof(NAPIGenerator), new[] { "napi" });
public const string TypeScript_ID = "TypeScript";
public static readonly GeneratorKind TypeScript = new(TypeScript_ID, "TypeScript", typeof(TSGenerator), new[] { "ts", "typescript" });
}
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();
}
}
}

4
src/Generator/Generators/ExtensionMethods.cs

@ -65,9 +65,9 @@ namespace CppSharp.Generators @@ -65,9 +65,9 @@ namespace CppSharp.Generators
switch (generatorKind)
{
case GeneratorKind.CLI:
case var _ when ReferenceEquals(generatorKind, GeneratorKind.CLI):
return typeMap.CLISignatureType(typePrinterContext).Desugar();
case GeneratorKind.CSharp:
case var _ when ReferenceEquals(generatorKind, GeneratorKind.CSharp):
return typeMap.CSharpSignatureType(typePrinterContext).Desugar();
}
}

16
src/Generator/Passes/CheckDuplicatedNamesPass.cs

@ -202,22 +202,22 @@ namespace CppSharp.Passes @@ -202,22 +202,22 @@ namespace CppSharp.Passes
TypePrinter typePrinter;
switch (kind)
{
case GeneratorKind.C:
case var _ when ReferenceEquals(kind, GeneratorKind.C):
typePrinter = new CppTypePrinter(Context) { PrintFlavorKind = CppTypePrintFlavorKind.C };
break;
case GeneratorKind.Emscripten:
case var _ when ReferenceEquals(kind, GeneratorKind.Emscripten):
typePrinter = new EmscriptenTypePrinter(Context);
break;;
case GeneratorKind.CPlusPlus:
case GeneratorKind.QuickJS:
case GeneratorKind.NAPI:
case GeneratorKind.TypeScript:
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 GeneratorKind.CLI:
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
typePrinter = new CLITypePrinter(Context);
break;
case GeneratorKind.CSharp:
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
typePrinter = new CSharpTypePrinter(Context);
break;
default:

7
src/Generator/Passes/ValidateOperatorsPass.cs

@ -83,11 +83,12 @@ namespace CppSharp.Passes @@ -83,11 +83,12 @@ namespace CppSharp.Passes
{
Parameter parameter = @operator.Parameters.Last();
Type type = parameter.Type.Desugar();
switch (Options.GeneratorKind)
var kind = Options.GeneratorKind;
switch (kind)
{
case GeneratorKind.CLI:
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
return type.IsPrimitiveType(PrimitiveType.Int);
case GeneratorKind.CSharp:
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
Types.TypeMap typeMap;
if (Context.TypeMaps.FindTypeMap(type, out typeMap))
{

2
src/Generator/Types/DeclMap.cs

@ -35,7 +35,7 @@ namespace CppSharp.Types @@ -35,7 +35,7 @@ namespace CppSharp.Types
{
public GeneratorKind GeneratorKind { get; set; }
public DeclMapAttribute() : this(0)
public DeclMapAttribute()
{
}

2
src/Generator/Types/DeclMapDatabase.cs

@ -41,7 +41,7 @@ namespace CppSharp.Types @@ -41,7 +41,7 @@ namespace CppSharp.Types
var attrs = type.GetCustomAttributes(typeof(DeclMapAttribute), true);
foreach (DeclMapAttribute attr in attrs)
{
if (attr.GeneratorKind == 0 ||
if (attr.GeneratorKind == null ||
attr.GeneratorKind == bindingContext.Options.GeneratorKind)
{
var declMap = (DeclMap)Activator.CreateInstance(type);

26
src/Generator/Types/Std/Stdlib.CLI.cs

@ -9,7 +9,7 @@ using CppSharp.Generators.CSharp; @@ -9,7 +9,7 @@ using CppSharp.Generators.CSharp;
namespace CppSharp.Types.Std
{
[TypeMap("const char*", GeneratorKind = GeneratorKind.CLI)]
[TypeMap("const char*", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class ConstCharPointer : TypeMap
{
public override Type CLISignatureType(TypePrinterContext ctx)
@ -62,27 +62,27 @@ namespace CppSharp.Types.Std @@ -62,27 +62,27 @@ namespace CppSharp.Types.Std
}
}
[TypeMap("const char[]", GeneratorKind = GeneratorKind.CLI)]
[TypeMap("const char[]", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class ConstCharArray : ConstCharPointer
{
}
[TypeMap("const wchar_t*", GeneratorKind = GeneratorKind.CLI)]
[TypeMap("const wchar_t*", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class ConstWCharTPointer : ConstCharPointer
{
}
[TypeMap("const char16_t*", GeneratorKind = GeneratorKind.CLI)]
[TypeMap("const char16_t*", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class ConstChar16TPointer : ConstCharPointer
{
}
[TypeMap("const char32_t*", GeneratorKind = GeneratorKind.CLI)]
[TypeMap("const char32_t*", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class ConstChar32TPointer : ConstCharPointer
{
}
[TypeMap("basic_string<char, char_traits<char>, allocator<char>>", GeneratorKind = GeneratorKind.CLI)]
[TypeMap("basic_string<char, char_traits<char>, allocator<char>>", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class String : TypeMap
{
public override Type CLISignatureType(TypePrinterContext ctx)
@ -103,7 +103,7 @@ namespace CppSharp.Types.Std @@ -103,7 +103,7 @@ namespace CppSharp.Types.Std
}
}
[TypeMap("std::wstring", GeneratorKind = GeneratorKind.CLI)]
[TypeMap("std::wstring", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class WString : TypeMap
{
public override Type CLISignatureType(TypePrinterContext ctx)
@ -124,7 +124,7 @@ namespace CppSharp.Types.Std @@ -124,7 +124,7 @@ namespace CppSharp.Types.Std
}
}
[TypeMap("std::vector", GeneratorKind = GeneratorKind.CLI)]
[TypeMap("std::vector", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class Vector : TypeMap
{
public override bool IsIgnored
@ -258,7 +258,7 @@ namespace CppSharp.Types.Std @@ -258,7 +258,7 @@ namespace CppSharp.Types.Std
}
}
[TypeMap("std::map", GeneratorKind = GeneratorKind.CLI)]
[TypeMap("std::map", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class Map : TypeMap
{
public override bool IsIgnored { get { return true; } }
@ -293,19 +293,19 @@ namespace CppSharp.Types.Std @@ -293,19 +293,19 @@ namespace CppSharp.Types.Std
}
}
[TypeMap("std::list", GeneratorKind = GeneratorKind.CLI)]
[TypeMap("std::list", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class List : TypeMap
{
public override bool IsIgnored { get { return true; } }
}
[TypeMap("std::shared_ptr", GeneratorKind = GeneratorKind.CLI)]
[TypeMap("std::shared_ptr", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class SharedPtr : TypeMap
{
public override bool IsIgnored { get { return true; } }
}
[TypeMap("basic_ostream<char, char_traits<char>>", GeneratorKind.CLI)]
[TypeMap("basic_ostream<char, char_traits<char>>", GeneratorKind.CLI_ID)]
public partial class OStream : TypeMap
{
public override Type CLISignatureType(TypePrinterContext ctx)
@ -325,7 +325,7 @@ namespace CppSharp.Types.Std @@ -325,7 +325,7 @@ namespace CppSharp.Types.Std
}
}
[TypeMap("std::nullptr_t", GeneratorKind = GeneratorKind.CLI)]
[TypeMap("std::nullptr_t", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class NullPtr : TypeMap
{
public override bool DoesMarshalling { get { return false; } }

28
src/Generator/Types/Std/Stdlib.CSharp.cs

@ -11,35 +11,35 @@ using Type = CppSharp.AST.Type; @@ -11,35 +11,35 @@ using Type = CppSharp.AST.Type;
namespace CppSharp.Types.Std
{
[TypeMap("int", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("int", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class Int : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx) =>
CSharpTypePrinter.GetSignedType(Context.TargetInfo.IntWidth);
}
[TypeMap("unsigned int", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("unsigned int", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class UnsignedInt : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx) =>
CSharpTypePrinter.GetUnsignedType(Context.TargetInfo.IntWidth);
}
[TypeMap("long", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("long", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class Long : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx) =>
CSharpTypePrinter.GetSignedType(Context.TargetInfo.LongWidth);
}
[TypeMap("unsigned long", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("unsigned long", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class UnsignedLong : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx) =>
CSharpTypePrinter.GetUnsignedType(Context.TargetInfo.LongWidth);
}
[TypeMap("char", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("char", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class Char : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx)
@ -67,7 +67,7 @@ namespace CppSharp.Types.Std @@ -67,7 +67,7 @@ namespace CppSharp.Types.Std
}
}
[TypeMap("char16_t", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("char16_t", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class Char16T : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx)
@ -76,7 +76,7 @@ namespace CppSharp.Types.Std @@ -76,7 +76,7 @@ namespace CppSharp.Types.Std
}
}
[TypeMap("wchar_t", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("wchar_t", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class WCharT : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx)
@ -85,7 +85,7 @@ namespace CppSharp.Types.Std @@ -85,7 +85,7 @@ namespace CppSharp.Types.Std
}
}
[TypeMap("const char*", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("const char*", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class ConstCharPointer : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx)
@ -281,27 +281,27 @@ namespace CppSharp.Types.Std @@ -281,27 +281,27 @@ namespace CppSharp.Types.Std
}
}
[TypeMap("const char[]", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("const char[]", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class ConstCharArray : ConstCharPointer
{
}
[TypeMap("const wchar_t*", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("const wchar_t*", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class ConstWCharTPointer : ConstCharPointer
{
}
[TypeMap("const char16_t*", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("const char16_t*", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class ConstChar16TPointer : ConstCharPointer
{
}
[TypeMap("const char32_t*", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("const char32_t*", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class ConstChar32TPointer : ConstCharPointer
{
}
[TypeMap("basic_string<char, char_traits<char>, allocator<char>>", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("basic_string<char, char_traits<char>, allocator<char>>", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class String : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx)
@ -420,7 +420,7 @@ namespace CppSharp.Types.Std @@ -420,7 +420,7 @@ namespace CppSharp.Types.Std
}
}
[TypeMap("FILE", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("FILE", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class FILE : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx)

7
src/Generator/Types/TypeIgnoreChecker.cs

@ -13,14 +13,17 @@ namespace CppSharp @@ -13,14 +13,17 @@ namespace CppSharp
ITypeMapDatabase TypeMapDatabase { get; }
public bool IsIgnored;
public TypeIgnoreChecker(ITypeMapDatabase database,
GeneratorKind generatorKind = GeneratorKind.CSharp)
public TypeIgnoreChecker(ITypeMapDatabase database, GeneratorKind generatorKind)
{
TypeMapDatabase = database;
VisitOptions.ClearFlags(VisitFlags.ClassBases | VisitFlags.TemplateArguments);
this.generatorKind = generatorKind;
}
public TypeIgnoreChecker(ITypeMapDatabase database) : this(database, GeneratorKind.CSharp)
{
}
void Ignore()
{
IsIgnored = true;

32
src/Generator/Types/TypeMap.cs

@ -15,17 +15,17 @@ namespace CppSharp.Types @@ -15,17 +15,17 @@ namespace CppSharp.Types
public class TypeMapAttribute : Attribute
{
public string Type { get; }
public GeneratorKind GeneratorKind { get; set; }
public string GeneratorKindID { get; set; }
public TypeMapAttribute(string type) : this(type, 0)
public TypeMapAttribute(string type) : this(type, null)
{
Type = type;
}
public TypeMapAttribute(string type, GeneratorKind generatorKind)
public TypeMapAttribute(string type, string generatorKindID)
{
Type = type;
GeneratorKind = generatorKind;
GeneratorKindID = generatorKindID;
}
}
@ -55,12 +55,12 @@ namespace CppSharp.Types @@ -55,12 +55,12 @@ namespace CppSharp.Types
{
switch (kind)
{
case GeneratorKind.C:
case GeneratorKind.CPlusPlus:
case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
return CppSignatureType(ctx);
case GeneratorKind.CLI:
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
return CLISignatureType(ctx);
case GeneratorKind.CSharp:
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
return CSharpSignatureType(ctx);
default:
throw new System.NotImplementedException();
@ -71,14 +71,14 @@ namespace CppSharp.Types @@ -71,14 +71,14 @@ namespace CppSharp.Types
{
switch (kind)
{
case GeneratorKind.C:
case GeneratorKind.CPlusPlus:
case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
CppMarshalToNative(ctx);
return;
case GeneratorKind.CLI:
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
CLIMarshalToNative(ctx);
return;
case GeneratorKind.CSharp:
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
CSharpMarshalToNative(ctx as CSharpMarshalContext);
return;
default:
@ -90,14 +90,14 @@ namespace CppSharp.Types @@ -90,14 +90,14 @@ namespace CppSharp.Types
{
switch (kind)
{
case GeneratorKind.C:
case GeneratorKind.CPlusPlus:
case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
CppMarshalToManaged(ctx);
return;
case GeneratorKind.CLI:
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
CLIMarshalToManaged(ctx);
return;
case GeneratorKind.CSharp:
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
CSharpMarshalToManaged(ctx as CSharpMarshalContext);
return;
default:

4
src/Generator/Types/TypeMapDatabase.cs

@ -40,8 +40,8 @@ namespace CppSharp.Types @@ -40,8 +40,8 @@ namespace CppSharp.Types
var attrs = type.GetCustomAttributes(typeof(TypeMapAttribute), true);
foreach (TypeMapAttribute attr in attrs)
{
if (attr.GeneratorKind == 0 ||
attr.GeneratorKind == bindingContext.Options.GeneratorKind)
if (string.IsNullOrEmpty(attr.GeneratorKindID) ||
attr.GeneratorKindID == bindingContext.Options.GeneratorKind.ID)
{
var typeMap = (TypeMap)Activator.CreateInstance(type);
typeMap.Context = bindingContext;

Loading…
Cancel
Save