Skip to content

Commit 6e0a7be

Browse files
committed
refactor CLI backend into common methods
1 parent 8271415 commit 6e0a7be

File tree

7 files changed

+67
-81
lines changed

7 files changed

+67
-81
lines changed

src/Generator/Generators/CLI/CLIMarshal.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public override bool VisitType(Type type, TypeQualifiers quals)
2222
TypeMap typeMap;
2323
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
2424
{
25-
typeMap.CLIMarshalToManaged(Context);
25+
typeMap.MarshalToManaged(Context);
2626
return false;
2727
}
2828

@@ -215,7 +215,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
215215
typeMap.DoesMarshalling)
216216
{
217217
typeMap.Type = typedef;
218-
typeMap.CLIMarshalToManaged(Context);
218+
typeMap.MarshalToManaged(Context);
219219
return typeMap.IsValueType;
220220
}
221221

@@ -240,7 +240,7 @@ public override bool VisitTemplateSpecializationType(TemplateSpecializationType
240240
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
241241
{
242242
typeMap.Type = template;
243-
typeMap.CLIMarshalToManaged(Context);
243+
typeMap.MarshalToManaged(Context);
244244
return true;
245245
}
246246

@@ -406,7 +406,7 @@ public override bool VisitType(Type type, TypeQualifiers quals)
406406
TypeMap typeMap;
407407
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling)
408408
{
409-
typeMap.CLIMarshalToNative(Context);
409+
typeMap.MarshalToNative(Context);
410410
return false;
411411
}
412412

@@ -605,7 +605,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
605605
if (Context.Context.TypeMaps.FindTypeMap(decl.Type, out typeMap) &&
606606
typeMap.DoesMarshalling)
607607
{
608-
typeMap.CLIMarshalToNative(Context);
608+
typeMap.MarshalToNative(Context);
609609
return typeMap.IsValueType;
610610
}
611611

@@ -641,7 +641,7 @@ public override bool VisitTemplateSpecializationType(TemplateSpecializationType
641641
if (Context.Context.TypeMaps.FindTypeMap(template, out typeMap) && typeMap.DoesMarshalling)
642642
{
643643
typeMap.Type = template;
644-
typeMap.CLIMarshalToNative(Context);
644+
typeMap.MarshalToNative(Context);
645645
return true;
646646
}
647647

@@ -688,7 +688,7 @@ private void MarshalRefClass(Class @class)
688688
if (Context.Context.TypeMaps.FindTypeMap(type, out typeMap) &&
689689
typeMap.DoesMarshalling)
690690
{
691-
typeMap.CLIMarshalToNative(Context);
691+
typeMap.MarshalToNative(Context);
692692
return;
693693
}
694694

src/Generator/Generators/CLI/CLITypePrinter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public override TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals
2121
if (TypeMapDatabase.FindTypeMap(tag, out typeMap))
2222
{
2323
var typePrinterContext = new TypePrinterContext { Type = tag };
24-
return typeMap.CLISignatureType(typePrinterContext).ToString();
24+
return typeMap.SignatureType(typePrinterContext).ToString();
2525
}
2626

2727
Declaration decl = tag.Declaration;
@@ -112,7 +112,7 @@ public override TypePrinterResult VisitPointerType(PointerType pointer,
112112
Type = pointer
113113
};
114114

115-
return typeMap.CLISignatureType(typePrinterContext).Visit(this);
115+
return typeMap.SignatureType(typePrinterContext).Visit(this);
116116
}
117117

118118
var pointee = pointer.Pointee.Desugar();
@@ -217,7 +217,7 @@ public override TypePrinterResult VisitTypedefType(TypedefType typedef,
217217
{
218218
typeMap.Type = typedef;
219219
var typePrinterContext = new TypePrinterContext { Type = typedef };
220-
return typeMap.CLISignatureType(typePrinterContext).ToString();
220+
return typeMap.SignatureType(typePrinterContext).ToString();
221221
}
222222

223223
FunctionType func;
@@ -241,7 +241,7 @@ public override TypePrinterResult VisitTemplateSpecializationType(
241241
if (TypeMapDatabase.FindTypeMap(template, out typeMap) && !typeMap.IsIgnored)
242242
{
243243
var typePrinterContext = new TypePrinterContext { Type = template };
244-
return typeMap.CLISignatureType(typePrinterContext).ToString();
244+
return typeMap.SignatureType(typePrinterContext).ToString();
245245
}
246246

247247
return decl.Name;

src/Generator/Generators/ExtensionMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static Type GetMappedType(this Type type, TypeMapDatabase typeMaps,
6666
switch (generatorKind)
6767
{
6868
case var _ when ReferenceEquals(generatorKind, GeneratorKind.CLI):
69-
return typeMap.CLISignatureType(typePrinterContext).Desugar();
69+
return typeMap.SignatureType(typePrinterContext).Desugar();
7070
case var _ when ReferenceEquals(generatorKind, GeneratorKind.CSharp):
7171
return typeMap.CSharpSignatureType(typePrinterContext).Desugar();
7272
}

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

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ namespace CppSharp.Types.Std
1212
[TypeMap("const char*", GeneratorKindID = GeneratorKind.CLI_ID)]
1313
public partial class ConstCharPointer : TypeMap
1414
{
15-
public override Type CLISignatureType(TypePrinterContext ctx)
15+
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
1616
{
1717
return new CILType(typeof(string));
1818
}
1919

20-
public override void CLIMarshalToNative(MarshalContext ctx)
20+
public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
2121
{
2222
ctx.Before.WriteLine(
2323
"auto _{0} = clix::marshalString<clix::E_UTF8>({1});",
@@ -26,7 +26,7 @@ public override void CLIMarshalToNative(MarshalContext ctx)
2626
ctx.Return.Write("_{0}.c_str()", ctx.ArgName);
2727
}
2828

29-
public override void CLIMarshalToManaged(MarshalContext ctx)
29+
public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
3030
{
3131
if (ctx.Parameter != null && !ctx.Parameter.IsOut &&
3232
!ctx.Parameter.IsInOut)
@@ -85,18 +85,18 @@ public partial class ConstChar32TPointer : ConstCharPointer
8585
[TypeMap("basic_string<char, char_traits<char>, allocator<char>>", GeneratorKindID = GeneratorKind.CLI_ID)]
8686
public partial class String : TypeMap
8787
{
88-
public override Type CLISignatureType(TypePrinterContext ctx)
88+
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
8989
{
9090
return new CILType(typeof(string));
9191
}
9292

93-
public override void CLIMarshalToNative(MarshalContext ctx)
93+
public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
9494
{
9595
ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0})",
9696
ctx.Parameter.Name);
9797
}
9898

99-
public override void CLIMarshalToManaged(MarshalContext ctx)
99+
public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
100100
{
101101
ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0})",
102102
ctx.ReturnVarName);
@@ -106,18 +106,18 @@ public override void CLIMarshalToManaged(MarshalContext ctx)
106106
[TypeMap("std::wstring", GeneratorKindID = GeneratorKind.CLI_ID)]
107107
public partial class WString : TypeMap
108108
{
109-
public override Type CLISignatureType(TypePrinterContext ctx)
109+
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
110110
{
111111
return new CILType(typeof(string));
112112
}
113113

114-
public override void CLIMarshalToNative(MarshalContext ctx)
114+
public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
115115
{
116116
ctx.Return.Write("clix::marshalString<clix::E_UTF16>({0})",
117117
ctx.Parameter.Name);
118118
}
119119

120-
public override void CLIMarshalToManaged(MarshalContext ctx)
120+
public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
121121
{
122122
ctx.Return.Write("clix::marshalString<clix::E_UTF16>({0})",
123123
ctx.ReturnVarName);
@@ -145,13 +145,13 @@ public override bool IsIgnored
145145
}
146146
}
147147

148-
public override Type CLISignatureType(TypePrinterContext ctx)
148+
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
149149
{
150150
return new CustomType(
151151
$"::System::Collections::Generic::List<{ctx.GetTemplateParameterList()}>^");
152152
}
153153

154-
public override void CLIMarshalToNative(MarshalContext ctx)
154+
public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
155155
{
156156
var desugared = Type.Desugar();
157157
var templateType = desugared as TemplateSpecializationType;
@@ -209,7 +209,7 @@ public override void CLIMarshalToNative(MarshalContext ctx)
209209
ctx.Return.Write(tmpVarName);
210210
}
211211

212-
public override void CLIMarshalToManaged(MarshalContext ctx)
212+
public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
213213
{
214214
var desugared = Type.Desugar();
215215
var templateType = desugared as TemplateSpecializationType;
@@ -263,34 +263,23 @@ public partial class Map : TypeMap
263263
{
264264
public override bool IsIgnored { get { return true; } }
265265

266-
public override Type CLISignatureType(TypePrinterContext ctx)
266+
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
267267
{
268268
var type = Type as TemplateSpecializationType;
269269
return new CustomType(
270270
$@"::System::Collections::Generic::Dictionary<{
271271
type.Arguments[0].Type}, {type.Arguments[1].Type}>^");
272272
}
273273

274-
public override void CLIMarshalToNative(MarshalContext ctx)
274+
public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
275275
{
276276
throw new System.NotImplementedException();
277277
}
278278

279-
public override void CLIMarshalToManaged(MarshalContext ctx)
279+
public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
280280
{
281281
throw new System.NotImplementedException();
282282
}
283-
284-
public override Type CSharpSignatureType(TypePrinterContext ctx)
285-
{
286-
if (ctx.Kind == TypePrinterContextKind.Native)
287-
return new CustomType("Std.Map");
288-
289-
var type = Type as TemplateSpecializationType;
290-
return new CustomType(
291-
$@"System.Collections.Generic.Dictionary<{
292-
type.Arguments[0].Type}, {type.Arguments[1].Type}>");
293-
}
294283
}
295284

296285
[TypeMap("std::list", GeneratorKindID = GeneratorKind.CLI_ID)]
@@ -308,12 +297,12 @@ public partial class SharedPtr : TypeMap
308297
[TypeMap("basic_ostream<char, char_traits<char>>", GeneratorKind.CLI_ID)]
309298
public partial class OStream : TypeMap
310299
{
311-
public override Type CLISignatureType(TypePrinterContext ctx)
300+
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
312301
{
313302
return new CILType(typeof(System.IO.TextWriter));
314303
}
315304

316-
public override void CLIMarshalToNative(MarshalContext ctx)
305+
public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
317306
{
318307
var marshal = (CLIMarshalManagedToNativePrinter)ctx.MarshalToNative;
319308
if (!ctx.Parameter.Type.Desugar().IsPointer())

src/Generator/Types/TypeMap.cs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,40 @@ public class TypeMap
5151
/// </summary>
5252
public virtual bool DoesMarshalling => true;
5353

54-
public virtual Type SignatureType(TypePrinterContext ctx, GeneratorKind kind = null)
54+
public Type SignatureType(TypePrinterContext ctx)
55+
{
56+
return SignatureType(ctx, Context.Options.GeneratorKind);
57+
}
58+
59+
public virtual Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
5560
{
56-
kind ??= Context.Options.GeneratorKind;
5761
switch (kind)
5862
{
5963
case var _ when ReferenceEquals(kind, GeneratorKind.C):
6064
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
61-
return new CILType(typeof(object));
6265
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
63-
return CLISignatureType(ctx);
66+
return new CILType(typeof(object));
6467
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
6568
return CSharpSignatureType(ctx);
6669
default:
6770
throw new System.NotImplementedException();
6871
}
6972
}
7073

71-
public virtual void MarshalToNative(MarshalContext ctx, GeneratorKind kind = null)
74+
public void MarshalToNative(MarshalContext ctx)
75+
{
76+
MarshalToNative(ctx, Context.Options.GeneratorKind);
77+
}
78+
79+
public virtual void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
7280
{
7381
kind ??= Context.Options.GeneratorKind;
7482
switch (kind)
7583
{
7684
case var _ when ReferenceEquals(kind, GeneratorKind.C):
7785
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
78-
ctx.Return.Write(ctx.Parameter.Name);
79-
return;
8086
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
81-
CLIMarshalToNative(ctx);
87+
ctx.Return.Write(ctx.Parameter.Name);
8288
return;
8389
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
8490
CSharpMarshalToNative(ctx as CSharpMarshalContext);
@@ -88,17 +94,19 @@ public virtual void MarshalToNative(MarshalContext ctx, GeneratorKind kind = nul
8894
}
8995
}
9096

91-
public virtual void MarshalToManaged(MarshalContext ctx, GeneratorKind kind = null)
97+
public void MarshalToManaged(MarshalContext ctx)
98+
{
99+
MarshalToManaged(ctx, Context.Options.GeneratorKind);
100+
}
101+
102+
public virtual void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
92103
{
93-
kind ??= Context.Options.GeneratorKind;
94104
switch (kind)
95105
{
96106
case var _ when ReferenceEquals(kind, GeneratorKind.C):
97107
case var _ when ReferenceEquals(kind, GeneratorKind.CPlusPlus):
98-
ctx.Return.Write(ctx.ReturnVarName);
99-
return;
100108
case var _ when ReferenceEquals(kind, GeneratorKind.CLI):
101-
CLIMarshalToManaged(ctx);
109+
ctx.Return.Write(ctx.ReturnVarName);
102110
return;
103111
case var _ when ReferenceEquals(kind, GeneratorKind.CSharp):
104112
CSharpMarshalToManaged(ctx as CSharpMarshalContext);
@@ -138,25 +146,10 @@ public virtual string CSharpConstruct()
138146

139147
#region C++/CLI backend
140148

141-
public virtual Type CLISignatureType(TypePrinterContext ctx)
142-
{
143-
return new CILType(typeof(object));
144-
}
145-
146149
public virtual void CLITypeReference(CLITypeReferenceCollector collector, ASTRecord<Declaration> loc)
147150
{
148151
}
149152

150-
public virtual void CLIMarshalToNative(MarshalContext ctx)
151-
{
152-
ctx.Return.Write(ctx.Parameter.Name);
153-
}
154-
155-
public virtual void CLIMarshalToManaged(MarshalContext ctx)
156-
{
157-
ctx.Return.Write(ctx.ReturnVarName);
158-
}
159-
160153
#endregion
161154
}
162155

tests/dotnet/CLI/CLI.Gen.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,34 @@
77

88
namespace CppSharp.Tests
99
{
10-
[TypeMap("IgnoredClassTemplateForEmployee")]
10+
[TypeMap("IgnoredClassTemplateForEmployee", GeneratorKindID = GeneratorKind.CLI_ID)]
1111
public class IgnoredClassTemplateForEmployeeMap : TypeMap
1212
{
13-
public override Type CLISignatureType(TypePrinterContext ctx)
13+
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
1414
{
1515
return new CustomType("CLI::Employee^");
1616
}
1717

18-
public override void CLIMarshalToManaged(MarshalContext ctx)
18+
public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
1919
{
2020
ctx.Return.Write($"gcnew CLI::Employee({ctx.ReturnVarName}.m_employee)");
2121
}
2222
}
2323

24-
[TypeMap("TestMappedTypeNonConstRefParam")]
24+
[TypeMap("TestMappedTypeNonConstRefParam", GeneratorKindID = GeneratorKind.CLI_ID)]
2525
public class TestMappedTypeNonConstRefParamTypeMap : TypeMap
2626
{
27-
public override Type CLISignatureType(TypePrinterContext ctx)
27+
public override Type SignatureType(TypePrinterContext ctx, GeneratorKind kind)
2828
{
2929
return new CILType(typeof(string));
3030
}
3131

32-
public override void CLIMarshalToManaged(MarshalContext ctx)
32+
public override void MarshalToManaged(MarshalContext ctx, GeneratorKind kind)
3333
{
3434
ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0}.m_str)", ctx.ReturnVarName);
3535
}
3636

37-
public override void CLIMarshalToNative(MarshalContext ctx)
37+
public override void MarshalToNative(MarshalContext ctx, GeneratorKind kind)
3838
{
3939
if (ctx.Parameter.Usage == ParameterUsage.InOut)
4040
{

0 commit comments

Comments
 (0)