Browse Source

TypeMap: refactor C++ backend into common methods

pull/1802/head
Deadlocklogic 2 years ago
parent
commit
8271415e8a
  1. 14
      src/Generator/Generators/C/CppMarshal.cs
  2. 2
      src/Generator/Generators/C/CppTypePrinter.cs
  3. 12
      src/Generator/Generators/NAPI/NAPIMarshal.cs
  4. 30
      src/Generator/Types/TypeMap.cs
  5. 7
      tests/dotnet/CLI/CLI.Gen.cs

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

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

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

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

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

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

30
src/Generator/Types/TypeMap.cs

@ -58,7 +58,7 @@ namespace CppSharp.Types
{ {
case var _ when ReferenceEquals(kind, GeneratorKind.C): case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
return CppSignatureType(ctx); return new CILType(typeof(object));
case var _ when ReferenceEquals(kind, GeneratorKind.CLI): case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
return CLISignatureType(ctx); return CLISignatureType(ctx);
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp): case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
@ -75,7 +75,7 @@ namespace CppSharp.Types
{ {
case var _ when ReferenceEquals(kind, GeneratorKind.C): case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
CppMarshalToNative(ctx); ctx.Return.Write(ctx.Parameter.Name);
return; return;
case var _ when ReferenceEquals(kind, GeneratorKind.CLI): case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
CLIMarshalToNative(ctx); CLIMarshalToNative(ctx);
@ -95,7 +95,7 @@ namespace CppSharp.Types
{ {
case var _ when ReferenceEquals(kind, GeneratorKind.C): case var _ when ReferenceEquals(kind, GeneratorKind.C):
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus): case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
CppMarshalToManaged(ctx); ctx.Return.Write(ctx.ReturnVarName);
return; return;
case var _ when ReferenceEquals(kind, GeneratorKind.CLI): case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
CLIMarshalToManaged(ctx); CLIMarshalToManaged(ctx);
@ -158,30 +158,6 @@ namespace CppSharp.Types
} }
#endregion #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 public interface ITypeMapDatabase

7
tests/dotnet/CLI/CLI.Gen.cs

@ -29,13 +29,6 @@ namespace CppSharp.Tests
return new CILType(typeof(string)); 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 CLIMarshalToManaged(MarshalContext ctx) public override void CLIMarshalToManaged(MarshalContext ctx)
{ {
ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0}.m_str)", ctx.ReturnVarName); ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0}.m_str)", ctx.ReturnVarName);

Loading…
Cancel
Save