Browse Source

Typemap cleanup (#1802)

* TypeMap: prepare refactoring into a modular design

* TypeMap: refactor C++ backend into common methods

* TypeMap: refactor CLI backend into common methods

* CLI.Gen.cs: fix omitted typemap from previous commit

* Common.Gen.cs: fixed silly modification while testing

* GeneratorKind: add FindGeneratorKindByID method

* TypeMapDatabase: heavy refactor: group typemaps by GeneratorKind

* TypeMap: refactor CSharp backend into common methods + migration

* TypeMap: cleanup patches from previous commits

* TypeMapDatabase: fix passing GeneratorKind to FindTypeMap calls

* Stdlib.CSharp.cs: move std::map typemap from Stdlib.CLI.cs

* TypeMapDatabase: improve parameter name
pull/1811/head
deadlocklogic 2 years ago committed by GitHub
parent
commit
e068f2a14a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/Generator/AST/Utils.cs
  2. 5
      src/Generator/GeneratorKind.cs
  3. 14
      src/Generator/Generators/C/CppMarshal.cs
  4. 2
      src/Generator/Generators/C/CppTypePrinter.cs
  5. 14
      src/Generator/Generators/CLI/CLIMarshal.cs
  6. 8
      src/Generator/Generators/CLI/CLITypePrinter.cs
  7. 4
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  8. 2
      src/Generator/Generators/CSharp/CSharpSources.cs
  9. 20
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  10. 7
      src/Generator/Generators/ExtensionMethods.cs
  11. 12
      src/Generator/Generators/NAPI/NAPIMarshal.cs
  12. 14
      src/Generator/Generators/QuickJS/QuickJSMarshal.cs
  13. 12
      src/Generator/Passes/ExpressionHelper.cs
  14. 4
      src/Generator/Passes/ValidateOperatorsPass.cs
  15. 80
      src/Generator/Types/Std/Stdlib.CLI.cs
  16. 98
      src/Generator/Types/Std/Stdlib.CSharp.cs
  17. 108
      src/Generator/Types/TypeMap.cs
  18. 75
      src/Generator/Types/TypeMapDatabase.cs
  19. 65
      tests/dotnet/CLI/CLI.Gen.cs
  20. 36
      tests/dotnet/CSharp/CSharp.Gen.cs
  21. 62
      tests/dotnet/Common/Common.Gen.cs

2
src/Generator/AST/Utils.cs

@ -207,7 +207,7 @@ namespace CppSharp.AST @@ -207,7 +207,7 @@ namespace CppSharp.AST
return false;
var typePrinterContext = new TypePrinterContext { Type = type };
var mappedTo = typeMap.CSharpSignatureType(typePrinterContext);
var mappedTo = typeMap.SignatureType(typePrinterContext);
mappedTo = mappedTo.Desugar();
mappedTo = (mappedTo.GetFinalPointee() ?? mappedTo).Desugar();
return (mappedTo.IsPrimitiveType() ||

5
src/Generator/GeneratorKind.cs

@ -38,6 +38,11 @@ namespace CppSharp.Generators @@ -38,6 +38,11 @@ namespace CppSharp.Generators
Registered.Add(this);
}
public static GeneratorKind FindGeneratorKindByID(string id)
{
return Registered.Where(kind => kind.ID == id).First();
}
public Generator CreateGenerator(BindingContext context)
{
return (Generator)Activator.CreateInstance(GeneratorType, context);

14
src/Generator/Generators/C/CppMarshal.cs

@ -25,7 +25,7 @@ namespace CppSharp.Generators.Cpp @@ -25,7 +25,7 @@ namespace CppSharp.Generators.Cpp
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return false;
}
@ -173,7 +173,7 @@ namespace CppSharp.Generators.Cpp @@ -173,7 +173,7 @@ namespace CppSharp.Generators.Cpp
typeMap.DoesMarshalling)
{
typeMap.Type = typedef;
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return typeMap.IsValueType;
}
@ -193,7 +193,7 @@ namespace CppSharp.Generators.Cpp @@ -193,7 +193,7 @@ namespace CppSharp.Generators.Cpp
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return true;
}
@ -341,7 +341,7 @@ namespace CppSharp.Generators.Cpp @@ -341,7 +341,7 @@ namespace CppSharp.Generators.Cpp
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return false;
}
@ -478,7 +478,7 @@ namespace CppSharp.Generators.Cpp @@ -478,7 +478,7 @@ namespace CppSharp.Generators.Cpp
if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) &&
typeMap.DoesMarshalling)
{
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return typeMap.IsValueType;
}
@ -516,7 +516,7 @@ namespace CppSharp.Generators.Cpp @@ -516,7 +516,7 @@ namespace CppSharp.Generators.Cpp
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return true;
}
@ -563,7 +563,7 @@ namespace CppSharp.Generators.Cpp @@ -563,7 +563,7 @@ namespace CppSharp.Generators.Cpp
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) &&
typeMap.DoesMarshalling)
{
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return;
}

2
src/Generator/Generators/C/CppTypePrinter.cs

@ -68,7 +68,7 @@ namespace CppSharp.Generators.C @@ -68,7 +68,7 @@ namespace CppSharp.Generators.C
typePrinter.PushContext(ContextKind);
typePrinter.PushScope(ScopeKind);
var typeName = typeMap.CppSignatureType(typePrinterContext).Visit(typePrinter);
var typeName = typeMap.SignatureType(typePrinterContext).Visit(typePrinter);
result = new TypePrinterResult(typeName) { TypeMap = typeMap };
return true;

14
src/Generator/Generators/CLI/CLIMarshal.cs

@ -22,7 +22,7 @@ namespace CppSharp.Generators.CLI @@ -22,7 +22,7 @@ namespace CppSharp.Generators.CLI
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CLIMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return false;
}
@ -215,7 +215,7 @@ namespace CppSharp.Generators.CLI @@ -215,7 +215,7 @@ namespace CppSharp.Generators.CLI
typeMap.DoesMarshalling)
{
typeMap.Type = typedef;
typeMap.CLIMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return typeMap.IsValueType;
}
@ -240,7 +240,7 @@ namespace CppSharp.Generators.CLI @@ -240,7 +240,7 @@ namespace CppSharp.Generators.CLI
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CLIMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return true;
}
@ -406,7 +406,7 @@ namespace CppSharp.Generators.CLI @@ -406,7 +406,7 @@ namespace CppSharp.Generators.CLI
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CLIMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return false;
}
@ -605,7 +605,7 @@ namespace CppSharp.Generators.CLI @@ -605,7 +605,7 @@ namespace CppSharp.Generators.CLI
if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) &&
typeMap.DoesMarshalling)
{
typeMap.CLIMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return typeMap.IsValueType;
}
@ -641,7 +641,7 @@ namespace CppSharp.Generators.CLI @@ -641,7 +641,7 @@ namespace CppSharp.Generators.CLI
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CLIMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return true;
}
@ -688,7 +688,7 @@ namespace CppSharp.Generators.CLI @@ -688,7 +688,7 @@ namespace CppSharp.Generators.CLI
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) &&
typeMap.DoesMarshalling)
{
typeMap.CLIMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return;
}

8
src/Generator/Generators/CLI/CLITypePrinter.cs

@ -21,7 +21,7 @@ namespace CppSharp.Generators.CLI @@ -21,7 +21,7 @@ namespace CppSharp.Generators.CLI
if (TypeMapDatabase.FindTypeMap(tag, out typeMap))
{
var typePrinterContext = new TypePrinterContext { Type = tag };
return typeMap.CLISignatureType(typePrinterContext).ToString();
return typeMap.SignatureType(typePrinterContext).ToString();
}
Declaration decl = tag.Declaration;
@ -112,7 +112,7 @@ namespace CppSharp.Generators.CLI @@ -112,7 +112,7 @@ namespace CppSharp.Generators.CLI
Type = pointer
};
return typeMap.CLISignatureType(typePrinterContext).Visit(this);
return typeMap.SignatureType(typePrinterContext).Visit(this);
}
var pointee = pointer.Pointee.Desugar();
@ -217,7 +217,7 @@ namespace CppSharp.Generators.CLI @@ -217,7 +217,7 @@ namespace CppSharp.Generators.CLI
{
typeMap.Type = typedef;
var typePrinterContext = new TypePrinterContext { Type = typedef };
return typeMap.CLISignatureType(typePrinterContext).ToString();
return typeMap.SignatureType(typePrinterContext).ToString();
}
FunctionType func;
@ -241,7 +241,7 @@ namespace CppSharp.Generators.CLI @@ -241,7 +241,7 @@ namespace CppSharp.Generators.CLI
if (TypeMapDatabase.FindTypeMap(template, out typeMap) && !typeMap.IsIgnored)
{
var typePrinterContext = new TypePrinterContext { Type = template };
return typeMap.CLISignatureType(typePrinterContext).ToString();
return typeMap.SignatureType(typePrinterContext).ToString();
}
return decl.Name;

4
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -45,7 +45,7 @@ namespace CppSharp.Generators.CSharp @@ -45,7 +45,7 @@ namespace CppSharp.Generators.CSharp
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CSharpMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return false;
}
@ -471,7 +471,7 @@ namespace CppSharp.Generators.CSharp @@ -471,7 +471,7 @@ namespace CppSharp.Generators.CSharp
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CSharpMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return false;
}

2
src/Generator/Generators/CSharp/CSharpSources.cs

@ -3158,7 +3158,7 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty @@ -3158,7 +3158,7 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty
Type = indirectRetType.Type.Desugar()
};
WriteLine("{0} {1};", typeMap.CSharpSignatureType(typePrinterContext),
WriteLine("{0} {1};", typeMap.SignatureType(typePrinterContext),
Helpers.ReturnIdentifier);
}
else

20
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -36,7 +36,7 @@ namespace CppSharp.Generators.CSharp @@ -36,7 +36,7 @@ namespace CppSharp.Generators.CSharp
return string.Empty;
TypeMap typeMap;
if (TypeMapDatabase.FindTypeMap(tag, out typeMap))
if (TypeMapDatabase.FindTypeMap(tag, GeneratorKind.CSharp, out typeMap))
{
typeMap.Type = tag;
@ -47,7 +47,7 @@ namespace CppSharp.Generators.CSharp @@ -47,7 +47,7 @@ namespace CppSharp.Generators.CSharp
Type = tag
};
return typeMap.CSharpSignatureType(typePrinterContext).ToString();
return typeMap.SignatureType(typePrinterContext).ToString();
}
return base.VisitTagType(tag, quals);
@ -150,7 +150,7 @@ namespace CppSharp.Generators.CSharp @@ -150,7 +150,7 @@ namespace CppSharp.Generators.CSharp
public override TypePrinterResult VisitBuiltinType(BuiltinType builtin, TypeQualifiers quals)
{
TypeMap typeMap;
if (TypeMapDatabase.FindTypeMap(builtin, out typeMap))
if (TypeMapDatabase.FindTypeMap(builtin, GeneratorKind.CSharp, out typeMap))
{
var typePrinterContext = new TypePrinterContext()
{
@ -159,7 +159,7 @@ namespace CppSharp.Generators.CSharp @@ -159,7 +159,7 @@ namespace CppSharp.Generators.CSharp
Type = builtin,
Parameter = Parameter
};
return typeMap.CSharpSignatureType(typePrinterContext).Visit(this);
return typeMap.SignatureType(typePrinterContext).Visit(this);
}
return base.VisitBuiltinType(builtin, quals);
}
@ -183,7 +183,7 @@ namespace CppSharp.Generators.CSharp @@ -183,7 +183,7 @@ namespace CppSharp.Generators.CSharp
if (allowStrings && pointer.IsConstCharString())
{
TypeMap typeMap;
TypeMapDatabase.FindTypeMap(pointer, out typeMap);
TypeMapDatabase.FindTypeMap(pointer, GeneratorKind.CSharp, out typeMap);
var typePrinterContext = new TypePrinterContext()
{
Kind = ContextKind,
@ -191,7 +191,7 @@ namespace CppSharp.Generators.CSharp @@ -191,7 +191,7 @@ namespace CppSharp.Generators.CSharp
Type = pointer.Pointee,
Parameter = Parameter
};
return typeMap.CSharpSignatureType(typePrinterContext).Visit(this);
return typeMap.SignatureType(typePrinterContext).Visit(this);
}
var pointee = pointer.Pointee.Desugar();
@ -258,7 +258,7 @@ namespace CppSharp.Generators.CSharp @@ -258,7 +258,7 @@ namespace CppSharp.Generators.CSharp
var decl = typedef.Declaration;
TypeMap typeMap;
if (TypeMapDatabase.FindTypeMap(typedef, out typeMap))
if (TypeMapDatabase.FindTypeMap(typedef, GeneratorKind.CSharp, out typeMap))
{
typeMap.Type = typedef;
@ -270,7 +270,7 @@ namespace CppSharp.Generators.CSharp @@ -270,7 +270,7 @@ namespace CppSharp.Generators.CSharp
Parameter = Parameter
};
return typeMap.CSharpSignatureType(typePrinterContext).ToString();
return typeMap.SignatureType(typePrinterContext).ToString();
}
FunctionType func;
@ -299,7 +299,7 @@ namespace CppSharp.Generators.CSharp @@ -299,7 +299,7 @@ namespace CppSharp.Generators.CSharp
template.Template.TemplatedDecl;
TypeMap typeMap;
if (!TypeMapDatabase.FindTypeMap(template, out typeMap))
if (!TypeMapDatabase.FindTypeMap(template, GeneratorKind.CSharp, out typeMap))
{
if (ContextKind == TypePrinterContextKind.Managed &&
decl == template.Template.TemplatedDecl &&
@ -330,7 +330,7 @@ namespace CppSharp.Generators.CSharp @@ -330,7 +330,7 @@ namespace CppSharp.Generators.CSharp
MarshalKind = MarshalKind
};
return typeMap.CSharpSignatureType(typePrinterContext).ToString();
return typeMap.SignatureType(typePrinterContext).ToString();
}
public override TypePrinterResult VisitDependentTemplateSpecializationType(

7
src/Generator/Generators/ExtensionMethods.cs

@ -63,12 +63,9 @@ namespace CppSharp.Generators @@ -63,12 +63,9 @@ namespace CppSharp.Generators
Type = typeMap.Type
};
switch (generatorKind)
if (generatorKind == GeneratorKind.CLI || generatorKind == GeneratorKind.CSharp)
{
case var _ when ReferenceEquals(generatorKind, GeneratorKind.CLI):
return typeMap.CLISignatureType(typePrinterContext).Desugar();
case var _ when ReferenceEquals(generatorKind, GeneratorKind.CSharp):
return typeMap.CSharpSignatureType(typePrinterContext).Desugar();
return typeMap.SignatureType(typePrinterContext).Desugar();
}
}

12
src/Generator/Generators/NAPI/NAPIMarshal.cs

@ -26,7 +26,7 @@ namespace CppSharp.Generators.NAPI @@ -26,7 +26,7 @@ namespace CppSharp.Generators.NAPI
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return false;
}
@ -194,7 +194,7 @@ namespace CppSharp.Generators.NAPI @@ -194,7 +194,7 @@ namespace CppSharp.Generators.NAPI
typeMap.DoesMarshalling)
{
typeMap.Type = typedef;
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return typeMap.IsValueType;
}
@ -214,7 +214,7 @@ namespace CppSharp.Generators.NAPI @@ -214,7 +214,7 @@ namespace CppSharp.Generators.NAPI
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CppMarshalToManaged(Context);
typeMap.MarshalToManaged(Context);
return true;
}
@ -343,7 +343,7 @@ namespace CppSharp.Generators.NAPI @@ -343,7 +343,7 @@ namespace CppSharp.Generators.NAPI
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return false;
}
@ -591,7 +591,7 @@ namespace CppSharp.Generators.NAPI @@ -591,7 +591,7 @@ namespace CppSharp.Generators.NAPI
if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) &&
typeMap.DoesMarshalling)
{
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return typeMap.IsValueType;
}
@ -628,7 +628,7 @@ namespace CppSharp.Generators.NAPI @@ -628,7 +628,7 @@ namespace CppSharp.Generators.NAPI
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.CppMarshalToNative(Context);
typeMap.MarshalToNative(Context);
return true;
}

14
src/Generator/Generators/QuickJS/QuickJSMarshal.cs

@ -25,7 +25,7 @@ namespace CppSharp.Generators.Cpp @@ -25,7 +25,7 @@ namespace CppSharp.Generators.Cpp
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.MarshalToManaged(GeneratorKind.QuickJS, Context);
typeMap.MarshalToManaged(Context);
return false;
}
@ -210,7 +210,7 @@ namespace CppSharp.Generators.Cpp @@ -210,7 +210,7 @@ namespace CppSharp.Generators.Cpp
typeMap.DoesMarshalling)
{
typeMap.Type = typedef;
typeMap.MarshalToManaged(GeneratorKind.QuickJS, Context);
typeMap.MarshalToManaged(Context);
return typeMap.IsValueType;
}
@ -232,7 +232,7 @@ namespace CppSharp.Generators.Cpp @@ -232,7 +232,7 @@ namespace CppSharp.Generators.Cpp
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.MarshalToManaged(GeneratorKind.QuickJS, Context);
typeMap.MarshalToManaged(Context);
return true;
}
@ -385,7 +385,7 @@ namespace CppSharp.Generators.Cpp @@ -385,7 +385,7 @@ namespace CppSharp.Generators.Cpp
TypeMap typeMap;
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.MarshalToNative(GeneratorKind.QuickJS, Context);
typeMap.MarshalToNative(Context);
return false;
}
@ -583,7 +583,7 @@ namespace CppSharp.Generators.Cpp @@ -583,7 +583,7 @@ namespace CppSharp.Generators.Cpp
if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) &&
typeMap.DoesMarshalling)
{
typeMap.MarshalToNative(GeneratorKind.QuickJS, Context);
typeMap.MarshalToNative(Context);
return typeMap.IsValueType;
}
@ -625,7 +625,7 @@ namespace CppSharp.Generators.Cpp @@ -625,7 +625,7 @@ namespace CppSharp.Generators.Cpp
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
{
typeMap.Type = template;
typeMap.MarshalToNative(GeneratorKind.QuickJS, Context);
typeMap.MarshalToNative(Context);
return true;
}
@ -672,7 +672,7 @@ namespace CppSharp.Generators.Cpp @@ -672,7 +672,7 @@ namespace CppSharp.Generators.Cpp
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) &&
typeMap.DoesMarshalling)
{
typeMap.MarshalToNative(GeneratorKind.QuickJS, Context);
typeMap.MarshalToNative(Context);
return;
}

12
src/Generator/Passes/ExpressionHelper.cs

@ -68,9 +68,9 @@ namespace CppSharp.Internal @@ -68,9 +68,9 @@ namespace CppSharp.Internal
public static System.Type GetSystemType(BindingContext context, Type type)
{
if (context.TypeMaps.FindTypeMap(type, out TypeMap typeMap))
if (context.TypeMaps.FindTypeMap(type, GeneratorKind.CSharp, out TypeMap typeMap))
{
var cilType = typeMap.CSharpSignatureType(new TypePrinterContext { Type = type, Kind = TypePrinterContextKind.Managed }) as CILType;
var cilType = typeMap.SignatureType(new TypePrinterContext { Type = type, Kind = TypePrinterContextKind.Managed }) as CILType;
if (cilType != null)
return cilType.Type;
}
@ -239,7 +239,7 @@ namespace CppSharp.Internal @@ -239,7 +239,7 @@ namespace CppSharp.Internal
var typePrinterResult = type.Visit(typePrinter);
TypeMap typeMap;
if (context.TypeMaps.FindTypeMap(type, out typeMap))
if (context.TypeMaps.FindTypeMap(type, GeneratorKind.CSharp, out typeMap))
{
var typePrinterContext = new TypePrinterContext()
{
@ -248,7 +248,7 @@ namespace CppSharp.Internal @@ -248,7 +248,7 @@ namespace CppSharp.Internal
Type = type
};
var typeInSignature = typeMap.CSharpSignatureType(typePrinterContext)
var typeInSignature = typeMap.SignatureType(typePrinterContext)
.SkipPointerRefs().Desugar();
Enumeration @enum;
@ -413,7 +413,7 @@ namespace CppSharp.Internal @@ -413,7 +413,7 @@ namespace CppSharp.Internal
private static bool CheckForString(BindingContext context, Type desugared, ref string result)
{
if (context.TypeMaps.FindTypeMap(desugared, out TypeMap typeMap))
if (context.TypeMaps.FindTypeMap(desugared, GeneratorKind.CSharp, out TypeMap typeMap))
{
var typePrinterContext = new TypePrinterContext()
{
@ -422,7 +422,7 @@ namespace CppSharp.Internal @@ -422,7 +422,7 @@ namespace CppSharp.Internal
Type = desugared
};
var typeInSignature = typeMap.CSharpSignatureType(typePrinterContext)
var typeInSignature = typeMap.SignatureType(typePrinterContext)
.SkipPointerRefs().Desugar();
if (typeInSignature is CILType managed && managed.Type == typeof(string))

4
src/Generator/Passes/ValidateOperatorsPass.cs

@ -90,9 +90,9 @@ namespace CppSharp.Passes @@ -90,9 +90,9 @@ namespace CppSharp.Passes
return type.IsPrimitiveType(PrimitiveType.Int);
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
Types.TypeMap typeMap;
if (Context.TypeMaps.FindTypeMap(type, out typeMap))
if (Context.TypeMaps.FindTypeMap(type, GeneratorKind.CSharp, out typeMap))
{
var mappedTo = typeMap.CSharpSignatureType(
var mappedTo = typeMap.SignatureType(
new TypePrinterContext
{
Parameter = parameter,

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

@ -5,19 +5,18 @@ using CppSharp.Generators; @@ -5,19 +5,18 @@ using CppSharp.Generators;
using CppSharp.Generators.AST;
using CppSharp.Generators.C;
using CppSharp.Generators.CLI;
using CppSharp.Generators.CSharp;
namespace CppSharp.Types.Std
namespace CppSharp.Types.Std.CLI
{
[TypeMap("const char*", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class ConstCharPointer : TypeMap
public class ConstCharPointer : TypeMap
{
public override Type CLISignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(string));
}
public override void CLIMarshalToNative(MarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx)
{
ctx.Before.WriteLine(
"auto _{0} = clix::marshalString<clix::E_UTF8>({1});",
@ -26,7 +25,7 @@ namespace CppSharp.Types.Std @@ -26,7 +25,7 @@ namespace CppSharp.Types.Std
ctx.Return.Write("_{0}.c_str()", ctx.ArgName);
}
public override void CLIMarshalToManaged(MarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx)
{
if (ctx.Parameter != null && !ctx.Parameter.IsOut &&
!ctx.Parameter.IsInOut)
@ -57,46 +56,45 @@ namespace CppSharp.Types.Std @@ -57,46 +56,45 @@ namespace CppSharp.Types.Std
$"{Context.Options.Encoding.EncodingName} is not supported yet.");
ctx.Return.Write(
$@"({ctx.ReturnVarName} == 0 ? nullptr : clix::marshalString<clix::{
param}>({ctx.ReturnVarName}))");
$@"({ctx.ReturnVarName} == 0 ? nullptr : clix::marshalString<clix::{param}>({ctx.ReturnVarName}))");
}
}
[TypeMap("const char[]", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class ConstCharArray : ConstCharPointer
public class ConstCharArray : ConstCharPointer
{
}
[TypeMap("const wchar_t*", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class ConstWCharTPointer : ConstCharPointer
public class ConstWCharTPointer : ConstCharPointer
{
}
[TypeMap("const char16_t*", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class ConstChar16TPointer : ConstCharPointer
public class ConstChar16TPointer : ConstCharPointer
{
}
[TypeMap("const char32_t*", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class ConstChar32TPointer : ConstCharPointer
public class ConstChar32TPointer : ConstCharPointer
{
}
[TypeMap("basic_string<char, char_traits<char>, allocator<char>>", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class String : TypeMap
public class String : TypeMap
{
public override Type CLISignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(string));
}
public override void CLIMarshalToNative(MarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx)
{
ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0})",
ctx.Parameter.Name);
}
public override void CLIMarshalToManaged(MarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0})",
ctx.ReturnVarName);
@ -104,20 +102,20 @@ namespace CppSharp.Types.Std @@ -104,20 +102,20 @@ namespace CppSharp.Types.Std
}
[TypeMap("std::wstring", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class WString : TypeMap
public class WString : TypeMap
{
public override Type CLISignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(string));
}
public override void CLIMarshalToNative(MarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx)
{
ctx.Return.Write("clix::marshalString<clix::E_UTF16>({0})",
ctx.Parameter.Name);
}
public override void CLIMarshalToManaged(MarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write("clix::marshalString<clix::E_UTF16>({0})",
ctx.ReturnVarName);
@ -125,7 +123,7 @@ namespace CppSharp.Types.Std @@ -125,7 +123,7 @@ namespace CppSharp.Types.Std
}
[TypeMap("std::vector", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class Vector : TypeMap
public class Vector : TypeMap
{
public override bool IsIgnored
{
@ -145,13 +143,13 @@ namespace CppSharp.Types.Std @@ -145,13 +143,13 @@ namespace CppSharp.Types.Std
}
}
public override Type CLISignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CustomType(
$"::System::Collections::Generic::List<{ctx.GetTemplateParameterList()}>^");
}
public override void CLIMarshalToNative(MarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx)
{
var desugared = Type.Desugar();
var templateType = desugared as TemplateSpecializationType;
@ -209,7 +207,7 @@ namespace CppSharp.Types.Std @@ -209,7 +207,7 @@ namespace CppSharp.Types.Std
ctx.Return.Write(tmpVarName);
}
public override void CLIMarshalToManaged(MarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx)
{
var desugared = Type.Desugar();
var templateType = desugared as TemplateSpecializationType;
@ -259,61 +257,49 @@ namespace CppSharp.Types.Std @@ -259,61 +257,49 @@ namespace CppSharp.Types.Std
}
[TypeMap("std::map", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class Map : TypeMap
public class Map : TypeMap
{
public override bool IsIgnored { get { return true; } }
public override Type CLISignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
var type = Type as TemplateSpecializationType;
return new CustomType(
$@"::System::Collections::Generic::Dictionary<{
type.Arguments[0].Type}, {type.Arguments[1].Type}>^");
$@"::System::Collections::Generic::Dictionary<{type.Arguments[0].Type}, {type.Arguments[1].Type}>^");
}
public override void CLIMarshalToNative(MarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx)
{
throw new System.NotImplementedException();
}
public override void CLIMarshalToManaged(MarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx)
{
throw new System.NotImplementedException();
}
public override Type CSharpSignatureType(TypePrinterContext ctx)
{
if (ctx.Kind == TypePrinterContextKind.Native)
return new CustomType("Std.Map");
var type = Type as TemplateSpecializationType;
return new CustomType(
$@"System.Collections.Generic.Dictionary<{
type.Arguments[0].Type}, {type.Arguments[1].Type}>");
}
}
[TypeMap("std::list", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class List : TypeMap
public class List : TypeMap
{
public override bool IsIgnored { get { return true; } }
}
[TypeMap("std::shared_ptr", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class SharedPtr : TypeMap
public class SharedPtr : TypeMap
{
public override bool IsIgnored { get { return true; } }
}
[TypeMap("basic_ostream<char, char_traits<char>>", GeneratorKind.CLI_ID)]
public partial class OStream : TypeMap
public class OStream : TypeMap
{
public override Type CLISignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(System.IO.TextWriter));
}
public override void CLIMarshalToNative(MarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx)
{
var marshal = (CLIMarshalManagedToNativePrinter)ctx.MarshalToNative;
if (!ctx.Parameter.Type.Desugar().IsPointer())
@ -326,7 +312,7 @@ namespace CppSharp.Types.Std @@ -326,7 +312,7 @@ namespace CppSharp.Types.Std
}
[TypeMap("std::nullptr_t", GeneratorKindID = GeneratorKind.CLI_ID)]
public partial class NullPtr : TypeMap
public class NullPtr : TypeMap
{
public override bool DoesMarshalling { get { return false; } }

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

@ -9,46 +9,46 @@ using CppSharp.Generators; @@ -9,46 +9,46 @@ using CppSharp.Generators;
using CppSharp.Generators.CSharp;
using Type = CppSharp.AST.Type;
namespace CppSharp.Types.Std
namespace CppSharp.Types.Std.CSharp
{
[TypeMap("int", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class Int : TypeMap
public class Int : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx) =>
public override Type SignatureType(TypePrinterContext ctx) =>
CSharpTypePrinter.GetSignedType(Context.TargetInfo.IntWidth);
}
[TypeMap("unsigned int", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class UnsignedInt : TypeMap
public class UnsignedInt : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx) =>
public override Type SignatureType(TypePrinterContext ctx) =>
CSharpTypePrinter.GetUnsignedType(Context.TargetInfo.IntWidth);
}
[TypeMap("long", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class Long : TypeMap
public class Long : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx) =>
public override Type SignatureType(TypePrinterContext ctx) =>
CSharpTypePrinter.GetSignedType(Context.TargetInfo.LongWidth);
}
[TypeMap("unsigned long", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class UnsignedLong : TypeMap
public class UnsignedLong : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx) =>
public override Type SignatureType(TypePrinterContext ctx) =>
CSharpTypePrinter.GetUnsignedType(Context.TargetInfo.LongWidth);
}
[TypeMap("char", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class Char : TypeMap
public class Char : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(ctx.Kind == TypePrinterContextKind.Native ||
!Context.Options.MarshalCharAsManagedChar ? typeof(sbyte) : typeof(char));
}
public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx)
{
if (Context.Options.MarshalCharAsManagedChar)
ctx.Return.Write("global::System.Convert.ToSByte({0})",
@ -57,7 +57,7 @@ namespace CppSharp.Types.Std @@ -57,7 +57,7 @@ namespace CppSharp.Types.Std
ctx.Return.Write(ctx.Parameter.Name);
}
public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx)
{
if (Context.Options.MarshalCharAsManagedChar)
ctx.Return.Write("global::System.Convert.ToChar({0})",
@ -68,27 +68,27 @@ namespace CppSharp.Types.Std @@ -68,27 +68,27 @@ namespace CppSharp.Types.Std
}
[TypeMap("char16_t", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class Char16T : TypeMap
public class Char16T : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(char));
}
}
[TypeMap("wchar_t", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class WCharT : TypeMap
public class WCharT : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(char));
}
}
[TypeMap("const char*", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class ConstCharPointer : TypeMap
public class ConstCharPointer : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
if (ctx.Kind == TypePrinterContextKind.Managed)
return new CILType(typeof(string));
@ -117,7 +117,7 @@ namespace CppSharp.Types.Std @@ -117,7 +117,7 @@ namespace CppSharp.Types.Std
$"{Context.Options.Encoding.EncodingName} is not supported yet.");
}
public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx)
{
string param = ctx.Parameter.Name;
if (ctx.Parameter.Usage == ParameterUsage.Unknown &&
@ -194,7 +194,7 @@ namespace CppSharp.Types.Std @@ -194,7 +194,7 @@ namespace CppSharp.Types.Std
ctx.Return.Write($"{bytePtr}");
}
public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx)
{
if (ctx.Parameter != null && !ctx.Parameter.IsOut &&
!ctx.Parameter.IsInOut)
@ -282,29 +282,29 @@ namespace CppSharp.Types.Std @@ -282,29 +282,29 @@ namespace CppSharp.Types.Std
}
[TypeMap("const char[]", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class ConstCharArray : ConstCharPointer
public class ConstCharArray : ConstCharPointer
{
}
[TypeMap("const wchar_t*", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class ConstWCharTPointer : ConstCharPointer
public class ConstWCharTPointer : ConstCharPointer
{
}
[TypeMap("const char16_t*", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class ConstChar16TPointer : ConstCharPointer
public class ConstChar16TPointer : ConstCharPointer
{
}
[TypeMap("const char32_t*", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class ConstChar32TPointer : ConstCharPointer
public class ConstChar32TPointer : ConstCharPointer
{
}
[TypeMap("basic_string<char, char_traits<char>, allocator<char>>", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class String : TypeMap
public class String : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
if (ctx.Kind == TypePrinterContextKind.Managed)
return new CILType(typeof(string));
@ -316,7 +316,7 @@ namespace CppSharp.Types.Std @@ -316,7 +316,7 @@ namespace CppSharp.Types.Std
return new CustomType(basicString.Visit(typePrinter).Type);
}
public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx)
{
Type type = ctx.Parameter.Type.Desugar();
ClassTemplateSpecialization basicString = GetBasicString(type);
@ -334,15 +334,13 @@ namespace CppSharp.Types.Std @@ -334,15 +334,13 @@ namespace CppSharp.Types.Std
var = Generator.GeneratedIdentifier(ctx.ArgName);
ctx.Before.WriteLine($"fixed (void* {var} = &{ctx.ReturnVarName})");
ctx.Before.WriteOpenBraceAndIndent();
ctx.HasCodeBlock = true;
(ctx as CSharpMarshalContext).HasCodeBlock = true;
}
else
{
var = $"&{ctx.ReturnVarName}";
}
ctx.Return.Write($@"{qualifiedBasicString}Extensions.{
Helpers.InternalStruct}.{assign.Name}(new {
typePrinter.IntPtrType}({var}), ");
ctx.Return.Write($@"{qualifiedBasicString}Extensions.{Helpers.InternalStruct}.{assign.Name}(new {typePrinter.IntPtrType}({var}), ");
if (ctx.Parameter.Type.IsTemplateParameterType())
ctx.Return.Write("(string) (object) ");
ctx.Return.Write($"{ctx.Parameter.Name})");
@ -351,22 +349,19 @@ namespace CppSharp.Types.Std @@ -351,22 +349,19 @@ namespace CppSharp.Types.Std
else
{
var varBasicString = $"__basicString{ctx.ParameterIndex}";
ctx.Before.WriteLine($@"var {varBasicString} = new {
basicString.Visit(typePrinter)}();");
ctx.Before.WriteLine($@"var {varBasicString} = new {basicString.Visit(typePrinter)}();");
ctx.Before.Write($@"{qualifiedBasicString}Extensions.{
assign.Name}({varBasicString}, ");
ctx.Before.Write($@"{qualifiedBasicString}Extensions.{assign.Name}({varBasicString}, ");
if (ctx.Parameter.Type.IsTemplateParameterType())
ctx.Before.Write("(string) (object) ");
ctx.Before.WriteLine($"{ctx.Parameter.Name});");
ctx.Return.Write($"{varBasicString}.{Helpers.InstanceIdentifier}");
ctx.Cleanup.WriteLine($@"{varBasicString}.Dispose({
(!Type.IsAddress() || ctx.Parameter?.IsIndirect == true ? "disposing: true, callNativeDtor:false" : string.Empty)});");
ctx.Cleanup.WriteLine($@"{varBasicString}.Dispose({(!Type.IsAddress() || ctx.Parameter?.IsIndirect == true ? "disposing: true, callNativeDtor:false" : string.Empty)});");
}
}
public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx)
{
var type = Type.Desugar(resolveTemplateSubstitution: false);
ClassTemplateSpecialization basicString = GetBasicString(type);
@ -376,10 +371,7 @@ namespace CppSharp.Types.Std @@ -376,10 +371,7 @@ namespace CppSharp.Types.Std
string varBasicString = $"__basicStringRet{ctx.ParameterIndex}";
bool usePointer = type.IsAddress() || ctx.MarshalKind == MarshalKind.NativeField ||
ctx.MarshalKind == MarshalKind.ReturnVariableArray;
ctx.Before.WriteLine($@"var {varBasicString} = {
basicString.Visit(typePrinter)}.{Helpers.CreateInstanceIdentifier}({
(usePointer ? string.Empty : $"new {typePrinter.IntPtrType}(&")}{
ctx.ReturnVarName}{(usePointer ? string.Empty : ")")});");
ctx.Before.WriteLine($@"var {varBasicString} = {basicString.Visit(typePrinter)}.{Helpers.CreateInstanceIdentifier}({(usePointer ? string.Empty : $"new {typePrinter.IntPtrType}(&")}{ctx.ReturnVarName}{(usePointer ? string.Empty : ")")});");
string @string = $"{qualifiedBasicString}Extensions.{data.Name}({varBasicString})";
if (usePointer)
{
@ -420,10 +412,26 @@ namespace CppSharp.Types.Std @@ -420,10 +412,26 @@ namespace CppSharp.Types.Std
}
}
[TypeMap("std::map", GeneratorKindID = GeneratorKind.CSharp_ID)]
public class Map : TypeMap
{
public override bool IsIgnored { get { return true; } }
public override Type SignatureType(TypePrinterContext ctx)
{
if (ctx.Kind == TypePrinterContextKind.Native)
return new CustomType("Std.Map");
var type = Type as TemplateSpecializationType;
return new CustomType(
$@"System.Collections.Generic.Dictionary<{type.Arguments[0].Type}, {type.Arguments[1].Type}>");
}
}
[TypeMap("FILE", GeneratorKindID = GeneratorKind.CSharp_ID)]
public partial class FILE : TypeMap
public class FILE : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(System.IntPtr));
}

108
src/Generator/Types/TypeMap.cs

@ -2,10 +2,7 @@ @@ -2,10 +2,7 @@
using CppSharp.AST;
using CppSharp.Generators;
using CppSharp.Generators.AST;
using CppSharp.Generators.C;
using CppSharp.Generators.CLI;
using CppSharp.Generators.Cpp;
using CppSharp.Generators.CSharp;
using Attribute = System.Attribute;
using Type = CppSharp.AST.Type;
@ -51,77 +48,23 @@ namespace CppSharp.Types @@ -51,77 +48,23 @@ namespace CppSharp.Types
/// </summary>
public virtual bool DoesMarshalling => true;
public virtual Type SignatureType(GeneratorKind kind, TypePrinterContext ctx)
{
switch (kind)
{
case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
return CppSignatureType(ctx);
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
return CLISignatureType(ctx);
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
return CSharpSignatureType(ctx);
default:
throw new System.NotImplementedException();
}
}
public virtual void MarshalToNative(GeneratorKind kind, MarshalContext ctx)
{
switch (kind)
{
case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
CppMarshalToNative(ctx);
return;
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
CLIMarshalToNative(ctx);
return;
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
CSharpMarshalToNative(ctx as CSharpMarshalContext);
return;
default:
throw new System.NotImplementedException();
}
}
public virtual void MarshalToManaged(GeneratorKind kind, MarshalContext ctx)
{
switch (kind)
{
case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
CppMarshalToManaged(ctx);
return;
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
CLIMarshalToManaged(ctx);
return;
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
CSharpMarshalToManaged(ctx as CSharpMarshalContext);
return;
default:
throw new System.NotImplementedException();
}
}
#region C# backend
public virtual Type CSharpSignatureType(TypePrinterContext ctx)
public virtual Type SignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(object));
}
public virtual void CSharpMarshalToNative(CSharpMarshalContext ctx)
public virtual void MarshalToNative(MarshalContext ctx)
{
ctx.Return.Write(ctx.Parameter.Name);
}
public virtual void CSharpMarshalToManaged(CSharpMarshalContext ctx)
public virtual void MarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write(ctx.ReturnVarName);
}
#region C# backend
/// <summary>
/// Used to construct a new instance of the mapped type.
/// </summary>
@ -135,55 +78,18 @@ namespace CppSharp.Types @@ -135,55 +78,18 @@ namespace CppSharp.Types
#region C++/CLI backend
public virtual Type CLISignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(object));
}
public virtual void CLITypeReference(CLITypeReferenceCollector collector, ASTRecord<Declaration> loc)
{
}
public virtual void CLIMarshalToNative(MarshalContext ctx)
{
ctx.Return.Write(ctx.Parameter.Name);
}
public virtual void CLIMarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write(ctx.ReturnVarName);
}
#endregion
#region C++ backend
public virtual Type CppSignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(object));
}
public virtual void CppTypeReference(CLITypeReference collector, ASTRecord<Declaration> record)
{
throw new NotImplementedException();
}
public virtual void CppMarshalToNative(MarshalContext ctx)
{
ctx.Return.Write(ctx.Parameter.Name);
}
public virtual void CppMarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write(ctx.ReturnVarName);
}
#endregion
}
public interface ITypeMapDatabase
{
bool FindTypeMap(Type decl, out TypeMap typeMap);
bool FindTypeMap(Type decl, GeneratorKind kind, out TypeMap typeMap);
bool FindTypeMap(Declaration declaration, out TypeMap typeMap);
bool FindTypeMap(Declaration declaration, GeneratorKind kind, out TypeMap typeMap);
}
}

75
src/Generator/Types/TypeMapDatabase.cs

@ -10,13 +10,16 @@ namespace CppSharp.Types @@ -10,13 +10,16 @@ namespace CppSharp.Types
{
public class TypeMapDatabase : ITypeMapDatabase
{
public IDictionary<string, TypeMap> TypeMaps { get; set; }
private readonly BindingContext Context;
private readonly Dictionary<GeneratorKind, Dictionary<Type, TypeMap>> typeMapsCache = new();
public Dictionary<GeneratorKind, Dictionary<string, TypeMap>> GlobalTypeMaps { get; private set; }
public Dictionary<string, TypeMap> TypeMaps => TypeMapsByKind(GlobalTypeMaps, Context.Options.GeneratorKind);
public TypeMapDatabase(BindingContext bindingContext)
{
Context = bindingContext;
TypeMaps = new Dictionary<string, TypeMap>();
GlobalTypeMaps = new Dictionary<GeneratorKind, Dictionary<string, TypeMap>>();
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
try
@ -32,33 +35,22 @@ namespace CppSharp.Types @@ -32,33 +35,22 @@ namespace CppSharp.Types
}
}
private void SetupTypeMaps(IEnumerable<System.Type> types,
BindingContext bindingContext)
public static Dictionary<T, TypeMap> TypeMapsByKind<T>(Dictionary<GeneratorKind, Dictionary<T, TypeMap>> typeMapsDictionary, GeneratorKind kind)
{
foreach (var type in types)
if (!typeMapsDictionary.TryGetValue(kind, out Dictionary<T, TypeMap> typeMap))
{
var attrs = type.GetCustomAttributes(typeof(TypeMapAttribute), true);
foreach (TypeMapAttribute attr in attrs)
{
if (string.IsNullOrEmpty(attr.GeneratorKindID) ||
attr.GeneratorKindID == bindingContext.Options.GeneratorKind.ID)
{
var typeMap = (TypeMap)Activator.CreateInstance(type);
typeMap.Context = bindingContext;
typeMap.TypeMapDatabase = this;
// Custom types won't be overwritten by CppSharp ones.
if (!TypeMaps.ContainsKey(attr.Type))
{
TypeMaps.Add(attr.Type, typeMap);
}
}
}
typeMap = new Dictionary<T, TypeMap>();
typeMapsDictionary.Add(kind, typeMap);
}
return typeMap;
}
public bool FindTypeMap(Type type, out TypeMap typeMap)
public bool FindTypeMap(Type type, out TypeMap typeMap) =>
FindTypeMap(type, Context.Options.GeneratorKind, out typeMap);
public bool FindTypeMap(Type type, GeneratorKind kind, out TypeMap typeMap)
{
var typeMaps = TypeMapsByKind(typeMapsCache, kind);
// Looks up the type in the cache map.
if (typeMaps.ContainsKey(type))
{
@ -72,13 +64,12 @@ namespace CppSharp.Types @@ -72,13 +64,12 @@ namespace CppSharp.Types
{
var specialization = template.GetClassTemplateSpecialization();
if (specialization != null &&
FindTypeMap(specialization, out typeMap))
FindTypeMap(specialization, kind, out typeMap))
return true;
if (template.Template.TemplatedDecl != null)
{
if (FindTypeMap(template.Template.TemplatedDecl,
out typeMap))
if (FindTypeMap(template.Template.TemplatedDecl, kind, out typeMap))
{
typeMap.Type = type;
return true;
@ -113,7 +104,7 @@ namespace CppSharp.Types @@ -113,7 +104,7 @@ namespace CppSharp.Types
typePrinter.PushScope(typePrintScopeKind);
var typeName = type.Visit(typePrinter);
typePrinter.PopScope();
if (FindTypeMap(typeName, out typeMap))
if (FindTypeMap(typeName, kind, out typeMap))
{
typeMap.Type = type;
typeMaps[type] = typeMap;
@ -127,11 +118,33 @@ namespace CppSharp.Types @@ -127,11 +118,33 @@ namespace CppSharp.Types
}
public bool FindTypeMap(Declaration declaration, out TypeMap typeMap) =>
FindTypeMap(new TagType(declaration), out typeMap);
FindTypeMap(declaration, Context.Options.GeneratorKind, out typeMap);
public bool FindTypeMap(Declaration declaration, GeneratorKind kind, out TypeMap typeMap) =>
FindTypeMap(new TagType(declaration), kind, out typeMap);
public bool FindTypeMap(string name, out TypeMap typeMap) =>
TypeMaps.TryGetValue(name, out typeMap) && typeMap.IsEnabled;
public bool FindTypeMap(string name, GeneratorKind kind, out TypeMap typeMap) =>
TypeMapsByKind(GlobalTypeMaps, kind).TryGetValue(name, out typeMap) && typeMap.IsEnabled;
private Dictionary<Type, TypeMap> typeMaps = new Dictionary<Type, TypeMap>();
private void SetupTypeMaps(IEnumerable<System.Type> types, BindingContext bindingContext)
{
foreach (var type in types)
{
var attrs = type.GetCustomAttributes(typeof(TypeMapAttribute), true);
foreach (TypeMapAttribute attr in attrs)
{
var kind = string.IsNullOrEmpty(attr.GeneratorKindID) ? Context.Options.GeneratorKind : GeneratorKind.FindGeneratorKindByID(attr.GeneratorKindID);
var typeMaps = TypeMapsByKind(GlobalTypeMaps, kind);
// Custom types won't be overwritten by CppSharp ones.
if (!typeMaps.ContainsKey(attr.Type))
{
var typeMap = (TypeMap)Activator.CreateInstance(type);
typeMap.Context = bindingContext;
typeMap.TypeMapDatabase = this;
typeMaps.Add(attr.Type, typeMap);
}
}
}
}
}
}

65
tests/dotnet/CLI/CLI.Gen.cs

@ -1,59 +1,68 @@ @@ -1,59 +1,68 @@
using CppSharp.AST;
using CppSharp.Generators;
using CppSharp.Generators.C;
using CppSharp.Passes;
using CppSharp.Types;
using CppSharp.Utils;
namespace CppSharp.Tests
{
[TypeMap("IgnoredClassTemplateForEmployee")]
[TypeMap("IgnoredClassTemplateForEmployee", GeneratorKindID = GeneratorKind.CLI_ID)]
public class IgnoredClassTemplateForEmployeeMap : TypeMap
{
public override Type CLISignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
return new CustomType("CLI::Employee^");
}
public override void CLIMarshalToManaged(MarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write($"gcnew CLI::Employee({ctx.ReturnVarName}.m_employee)");
}
}
[TypeMap("TestMappedTypeNonConstRefParam")]
public class TestMappedTypeNonConstRefParamTypeMap : TypeMap
namespace CLI
{
public override Type CLISignatureType(TypePrinterContext ctx)
[TypeMap("TestMappedTypeNonConstRefParam", GeneratorKindID = GeneratorKind.CLI_ID)]
public class TestMappedTypeNonConstRefParamTypeMap : TypeMap
{
return new CILType(typeof(string));
}
public override Type SignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(string));
}
public override Type CppSignatureType(TypePrinterContext ctx)
{
var tagType = ctx.Type as TagType;
var typePrinter = new CppTypePrinter(Context);
return new CustomType(tagType.Declaration.Visit(typePrinter));
}
public override void MarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0}.m_str)", ctx.ReturnVarName);
}
public override void CLIMarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0}.m_str)", ctx.ReturnVarName);
public override void MarshalToNative(MarshalContext ctx)
{
if (ctx.Parameter.Usage == ParameterUsage.InOut)
{
ctx.Before.WriteLine($"System::String^ _{ctx.Parameter.Name} = {ctx.Parameter.Name};");
}
string paramName = ctx.Parameter.Usage == ParameterUsage.InOut ? $"_{ctx.Parameter.Name}" : ctx.Parameter.Name;
ctx.Before.WriteLine(
$"::TestMappedTypeNonConstRefParam _{ctx.ArgName} = clix::marshalString<clix::E_UTF8>({paramName});");
ctx.Return.Write("_{0}", ctx.ArgName);
}
}
}
public override void CLIMarshalToNative(MarshalContext ctx)
namespace Cpp
{
[TypeMap("TestMappedTypeNonConstRefParam", GeneratorKindID = GeneratorKind.CPlusPlus_ID)]
public class TestMappedTypeNonConstRefParamTypeMap : TypeMap
{
if (ctx.Parameter.Usage == ParameterUsage.InOut)
public override Type SignatureType(TypePrinterContext ctx)
{
ctx.Before.WriteLine($"System::String^ _{ctx.Parameter.Name} = {ctx.Parameter.Name};");
var tagType = ctx.Type as TagType;
var typePrinter = new CppTypePrinter(Context);
return new CustomType(tagType.Declaration.Visit(typePrinter));
}
string paramName = ctx.Parameter.Usage == ParameterUsage.InOut ? $"_{ctx.Parameter.Name}" : ctx.Parameter.Name;
ctx.Before.WriteLine(
$"::TestMappedTypeNonConstRefParam _{ctx.ArgName} = clix::marshalString<clix::E_UTF8>({paramName});");
ctx.Return.Write("_{0}", ctx.ArgName);
}
}

36
tests/dotnet/CSharp/CSharp.Gen.cs

@ -120,17 +120,17 @@ namespace CppSharp.Tests @@ -120,17 +120,17 @@ namespace CppSharp.Tests
[TypeMap("boolean_t")]
public class BooleanTypeMap : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
return new BuiltinType(PrimitiveType.Bool);
}
public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx)
{
ctx.Return.Write(ctx.Parameter.Name);
}
public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write(ctx.ReturnVarName);
}
@ -144,12 +144,12 @@ namespace CppSharp.Tests @@ -144,12 +144,12 @@ namespace CppSharp.Tests
return string.Empty;
}
public override Type CSharpSignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
return GetEnumType(ctx.Type);
}
public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx)
{
if (ctx.Parameter.Type.Desugar().IsAddress())
ctx.Return.Write("new global::System.IntPtr(&{0})", ctx.Parameter.Name);
@ -157,7 +157,7 @@ namespace CppSharp.Tests @@ -157,7 +157,7 @@ namespace CppSharp.Tests
ctx.Return.Write(ctx.Parameter.Name);
}
public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx)
{
if (ctx.ReturnType.Type.Desugar().IsAddress())
{
@ -196,17 +196,17 @@ namespace CppSharp.Tests @@ -196,17 +196,17 @@ namespace CppSharp.Tests
return string.Empty;
}
public override Type CSharpSignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
return new TagType(flags ?? (flags = Context.ASTContext.FindEnum("Flags").First()));
}
public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx)
{
ctx.Return.Write(ctx.Parameter.Name);
}
public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write(ctx.ReturnVarName);
}
@ -229,7 +229,7 @@ namespace CppSharp.Tests @@ -229,7 +229,7 @@ namespace CppSharp.Tests
}
}
public override Type CSharpSignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
if (ctx.Kind == TypePrinterContextKind.Native)
{
@ -249,7 +249,7 @@ namespace CppSharp.Tests @@ -249,7 +249,7 @@ namespace CppSharp.Tests
ctx.GetTemplateParameterList()}>");
}
public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx)
{
// pointless, put just so that the generated code compiles
var type = (TemplateSpecializationType)ctx.Parameter.Type.Desugar();
@ -259,7 +259,7 @@ namespace CppSharp.Tests @@ -259,7 +259,7 @@ namespace CppSharp.Tests
ctx.Return.Write("new {0}()", specialization.Visit(typePrinter));
}
public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write(ctx.ReturnVarName);
}
@ -268,18 +268,18 @@ namespace CppSharp.Tests @@ -268,18 +268,18 @@ namespace CppSharp.Tests
[TypeMap("TypeMappedWithOperator")]
public class TypeMappedWithOperator : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
// doesn't matter, we just need it to compile
return new BuiltinType(PrimitiveType.Int);
}
public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx)
{
ctx.Return.Write(ctx.Parameter.Name);
}
public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write(ctx.ReturnVarName);
}
@ -288,7 +288,7 @@ namespace CppSharp.Tests @@ -288,7 +288,7 @@ namespace CppSharp.Tests
[TypeMap("QString")]
public class QString : TypeMap
{
public override Type CSharpSignatureType(TypePrinterContext ctx)
public override Type SignatureType(TypePrinterContext ctx)
{
if (ctx.Kind == TypePrinterContextKind.Native)
{
@ -299,13 +299,13 @@ namespace CppSharp.Tests @@ -299,13 +299,13 @@ namespace CppSharp.Tests
return new CILType(typeof(string));
}
public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
public override void MarshalToNative(MarshalContext ctx)
{
ctx.Return.Write(ctx.Parameter.Type.Desugar().IsAddress() ?
"global::System.IntPtr.Zero" : "\"test\"");
}
public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
public override void MarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write("\"test\"");
}

62
tests/dotnet/Common/Common.Gen.cs

@ -7,37 +7,47 @@ using CppSharp.Utils; @@ -7,37 +7,47 @@ using CppSharp.Utils;
namespace CppSharp.Tests
{
[TypeMap("TypeMappedIndex")]
public class TypeMappedIndex : TypeMap
namespace CLI
{
public override Type CLISignatureType(TypePrinterContext ctx)
[TypeMap("TypeMappedIndex", GeneratorKindID = GeneratorKind.CLI_ID)]
public class TypeMappedIndex : TypeMap
{
return new BuiltinType(PrimitiveType.UShort);
}
public override void CLIMarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write(ctx.ReturnVarName);
}
public override void CLIMarshalToNative(MarshalContext ctx)
{
ctx.Return.Write("::TypeMappedIndex()");
}
public override Type CSharpSignatureType(TypePrinterContext ctx)
{
return new BuiltinType(PrimitiveType.UShort);
}
public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
{
ctx.Return.Write(ctx.ReturnVarName);
public override Type SignatureType(TypePrinterContext ctx)
{
return new BuiltinType(PrimitiveType.UShort);
}
public override void MarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write(ctx.ReturnVarName);
}
public override void MarshalToNative(MarshalContext ctx)
{
ctx.Return.Write("::TypeMappedIndex()");
}
}
}
public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
namespace CSharp
{
[TypeMap("TypeMappedIndex", GeneratorKindID = GeneratorKind.CSharp_ID)]
public class TypeMappedIndex : TypeMap
{
ctx.Return.Write("IntPtr.Zero");
public override Type SignatureType(TypePrinterContext ctx)
{
return new BuiltinType(PrimitiveType.UShort);
}
public override void MarshalToManaged(MarshalContext ctx)
{
ctx.Return.Write(ctx.ReturnVarName);
}
public override void MarshalToNative(MarshalContext ctx)
{
ctx.Return.Write("IntPtr.Zero");
}
}
}

Loading…
Cancel
Save