Skip to content

Commit 065ed82

Browse files
committed
Remove the repetitive field for a type printer
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent c6768bc commit 065ed82

15 files changed

+78
-117
lines changed

src/Generator/Generators/C/CppMarshal.cs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace CppSharp.Generators.Cpp
1111
{
12-
public class CppMarshalNativeToManagedPrinter : MarshalPrinter<MarshalContext>
12+
public class CppMarshalNativeToManagedPrinter : MarshalPrinter<MarshalContext, CppTypePrinter>
1313
{
1414
public CppMarshalNativeToManagedPrinter(MarshalContext marshalContext)
1515
: base(marshalContext)
@@ -60,17 +60,15 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
6060
return false;
6161

6262
var pointee = pointer.Pointee.Desugar();
63-
64-
PrimitiveType primitive;
6563
var param = Context.Parameter;
6664
if (param != null && (param.IsOut || param.IsInOut) &&
67-
pointee.IsPrimitiveType(out primitive))
65+
pointee.IsPrimitiveType())
6866
{
6967
Context.Return.Write(Context.ReturnVarName);
7068
return true;
7169
}
7270

73-
if (pointee.IsPrimitiveType(out primitive))
71+
if (pointee.IsPrimitiveType())
7472
{
7573
var returnVarName = Context.ReturnVarName;
7674

@@ -99,8 +97,7 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
9997
Modifier = pointer.Modifier,
10098
QualifiedPointee = new QualifiedType(pointee)
10199
};
102-
var nativeTypePrinter = new CppTypePrinter(Context.Context);
103-
var nativeTypeName = desugaredPointer.Visit(nativeTypePrinter, quals);
100+
var nativeTypeName = desugaredPointer.Visit(typePrinter, quals);
104101
Context.Return.Write("reinterpret_cast<{0}>({1})", nativeTypeName,
105102
returnVarName);
106103
}
@@ -307,9 +304,9 @@ public override bool VisitTypedefDecl(TypedefDecl typedef)
307304

308305
public override bool VisitEnumDecl(Enumeration @enum)
309306
{
310-
var typePrinter = new CppTypePrinter(Context.Context);
311307
typePrinter.PushContext(TypePrinterContextKind.Managed);
312308
var typeName = typePrinter.VisitDeclaration(@enum);
309+
typePrinter.PopContext();
313310
Context.Return.Write($"({typeName}){Context.ReturnVarName}");
314311

315312
return true;
@@ -331,7 +328,7 @@ public override bool VisitFunctionTemplateDecl(FunctionTemplate template)
331328
}
332329
}
333330

334-
public class CppMarshalManagedToNativePrinter : MarshalPrinter<MarshalContext>
331+
public class CppMarshalManagedToNativePrinter : MarshalPrinter<MarshalContext, CppTypePrinter>
335332
{
336333
public CppMarshalManagedToNativePrinter(MarshalContext ctx)
337334
: base(ctx)
@@ -395,9 +392,9 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
395392

396393
if (pointee is FunctionType)
397394
{
398-
var cppTypePrinter = new CppTypePrinter(Context.Context);
399-
cppTypePrinter.PushContext(TypePrinterContextKind.Managed);
400-
var cppTypeName = pointer.Visit(cppTypePrinter, quals);
395+
typePrinter.PushContext(TypePrinterContextKind.Managed);
396+
var cppTypeName = pointer.Visit(typePrinter, quals);
397+
typePrinter.PopContext();
401398

402399
return VisitDelegateType(cppTypeName);
403400
}
@@ -425,8 +422,7 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
425422
var finalPointee = pointer.GetFinalPointee();
426423
if (finalPointee.IsPrimitiveType())
427424
{
428-
var cppTypePrinter = new CppTypePrinter(Context.Context);
429-
var cppTypeName = pointer.Visit(cppTypePrinter, quals);
425+
var cppTypeName = pointer.Visit(typePrinter, quals);
430426

431427
Context.Return.Write($"({cppTypeName})");
432428
Context.Return.Write(Context.Parameter.Name);
@@ -489,10 +485,6 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
489485
FunctionType func;
490486
if (decl.Type.IsPointerTo(out func))
491487
{
492-
var typePrinter = new CppTypePrinter(Context.Context);
493-
typePrinter.PushContext(TypePrinterContextKind.Native);
494-
var declName = decl.Visit(typePrinter);
495-
typePrinter.PopContext();
496488
// Use the original typedef name if available, otherwise just use the function pointer type
497489
string cppTypeName;
498490
if (!decl.IsSynthetized)

src/Generator/Generators/C/CppSources.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,7 @@ private bool GenerateClassConstructorBase(Class @class, Method method = null,
338338

339339
// We cast the value to the base class type since otherwise there
340340
// could be ambiguous call to overloaded constructors.
341-
var cppTypePrinter = new CppTypePrinter(Context);
342-
var nativeTypeName = @class.BaseClass.Visit(cppTypePrinter);
341+
var nativeTypeName = @class.BaseClass.Visit(typePrinter);
343342

344343
Write($"({nativeTypeName}*)");
345344
WriteLine("{0}{1})", method != null ? "nullptr" : ClassCtorInstanceParamIdentifier,

src/Generator/Generators/C/CppTypePrinter.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ public class CppTypePrinter : TypePrinter
2525

2626
public TypePrintScopeKind MethodScopeKind = TypePrintScopeKind.Qualified;
2727

28-
public CppTypePrinter(BindingContext context) : base(TypePrinterContextKind.Native)
28+
public CppTypePrinter() : base(TypePrinterContextKind.Native)
2929
{
30-
Context = context;
3130
PrintFlavorKind = CppTypePrintFlavorKind.Cpp;
3231
PrintTypeQualifiers = true;
3332
PrintTypeModifiers = true;
3433
}
3534

36-
public BindingContext Context { get; private set; }
35+
public CppTypePrinter(BindingContext context) : this()
36+
{
37+
Context = context;
38+
}
39+
3740
public TypeMapDatabase TypeMapDatabase => Context.TypeMaps;
3841
public DriverOptions Options => Context.Options;
3942

@@ -53,7 +56,7 @@ public virtual bool FindTypeMap(CppSharp.AST.Type type, out TypePrinterResult re
5356
var typePrinterContext = new TypePrinterContext
5457
{
5558
Type = type,
56-
Kind = Kind,
59+
Kind = ContextKind,
5760
MarshalKind = MarshalKind
5861
};
5962

@@ -101,13 +104,11 @@ public override TypePrinterResult VisitArrayType(ArrayType array,
101104
throw new NotImplementedException();
102105
}
103106

104-
var result = new TypePrinterResult
107+
return new TypePrinterResult
105108
{
106109
Type = array.Type.Visit(this),
107110
NameSuffix = new System.Text.StringBuilder(arraySuffix)
108111
};
109-
110-
return result;
111112
}
112113

113114
private static string ConvertModifierToString(PointerType.TypeModifier modifier)
@@ -601,7 +602,7 @@ public override TypePrinterResult VisitMethodDecl(Method method)
601602
$"operator {method.OriginalReturnType.Visit(this)}" :
602603
method.OriginalName;
603604

604-
string exceptionType = GetExceptionType(functionType);
605+
string exceptionType = Print(functionType.ExceptionSpecType);
605606

606607
if (!string.IsNullOrEmpty(@return.Type))
607608
@return.NamePrefix.Append(' ');
@@ -753,9 +754,9 @@ private string GetStringQuals(TypeQualifiers quals, bool appendSpace = true)
753754
return string.Join(" ", stringQuals) + (appendSpace ? " " : string.Empty);
754755
}
755756

756-
private static string GetExceptionType(FunctionType functionType)
757+
private static string Print(ExceptionSpecType exceptionSpecType)
757758
{
758-
switch (functionType.ExceptionSpecType)
759+
switch (exceptionSpecType)
759760
{
760761
case ExceptionSpecType.BasicNoexcept:
761762
return " noexcept";

src/Generator/Generators/CLI/CLIHeaders.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,9 @@ public override void GenerateClassEvents(Class @class)
467467
{
468468
if (!@event.IsGenerated) continue;
469469

470-
var cppTypePrinter = new CppTypePrinter(Context);
471-
cppTypePrinter.PushContext(TypePrinterContextKind.Native);
472-
var cppArgs = cppTypePrinter.VisitParameters(@event.Parameters, hasNames: true);
470+
typePrinter.PushContext(TypePrinterContextKind.Native);
471+
var cppArgs = typePrinter.VisitParameters(@event.Parameters, hasNames: true);
472+
typePrinter.PopContext();
473473

474474
WriteLine("private:");
475475
Indent();

src/Generator/Generators/CLI/CLIMarshal.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace CppSharp.Generators.CLI
1212
{
13-
public class CLIMarshalNativeToManagedPrinter : MarshalPrinter<MarshalContext>
13+
public class CLIMarshalNativeToManagedPrinter : MarshalPrinter<MarshalContext, CppTypePrinter>
1414
{
1515
public CLIMarshalNativeToManagedPrinter(MarshalContext marshalContext)
1616
: base(marshalContext)
@@ -139,8 +139,7 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
139139
Modifier = pointer.Modifier,
140140
QualifiedPointee = new QualifiedType(pointee)
141141
};
142-
var nativeTypePrinter = new CppTypePrinter(Context.Context);
143-
var nativeTypeName = desugaredPointer.Visit(nativeTypePrinter, quals);
142+
var nativeTypeName = desugaredPointer.Visit(typePrinter, quals);
144143
Context.Return.Write("reinterpret_cast<{0}>({1})", nativeTypeName,
145144
returnVarName);
146145
}
@@ -387,7 +386,7 @@ public bool VisitDelegate(Delegate @delegate)
387386
}
388387
}
389388

390-
public class CLIMarshalManagedToNativePrinter : MarshalPrinter<MarshalContext>
389+
public class CLIMarshalManagedToNativePrinter : MarshalPrinter<MarshalContext, CppTypePrinter>
391390
{
392391
public readonly TextGenerator VarPrefix;
393392
public readonly TextGenerator ArgumentPrefix;
@@ -510,8 +509,7 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
510509

511510
if (pointee is FunctionType)
512511
{
513-
var cppTypePrinter = new CppTypePrinter(Context.Context);
514-
var cppTypeName = pointer.Visit(cppTypePrinter, quals);
512+
var cppTypeName = pointer.Visit(typePrinter, quals);
515513

516514
return VisitDelegateType(cppTypeName);
517515
}
@@ -544,8 +542,7 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
544542
var finalPointee = pointer.GetFinalPointee();
545543
if (finalPointee.IsPrimitiveType())
546544
{
547-
var cppTypePrinter = new CppTypePrinter(Context.Context);
548-
var cppTypeName = pointer.Visit(cppTypePrinter, quals);
545+
var cppTypeName = pointer.Visit(typePrinter, quals);
549546

550547
Context.Return.Write("({0})", cppTypeName);
551548
Context.Return.Write(Context.Parameter.Name);
@@ -620,8 +617,7 @@ public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals)
620617
cppTypeName = "::" + typedef.Declaration.QualifiedOriginalName;
621618
else
622619
{
623-
var cppTypePrinter = new CppTypePrinter(Context.Context);
624-
cppTypeName = decl.Type.Visit(cppTypePrinter, quals);
620+
cppTypeName = decl.Type.Visit(typePrinter, quals);
625621
}
626622

627623
VisitDelegateType(cppTypeName);

src/Generator/Generators/CLI/CLISources.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,6 @@ private void GenerateEventAdd(Event @event, Class @class)
534534
WriteLine("if (!{0}Instance)", delegateName);
535535
WriteOpenBraceAndIndent();
536536

537-
var typePrinter = new CppTypePrinter(Context);
538537
var args = typePrinter.VisitParameters(@event.Parameters, hasNames: false);
539538

540539
WriteLine("{0}Instance = gcnew {0}(this, &{1}::_{2}Raise);",
@@ -583,7 +582,6 @@ private void GenerateEventRaise(Event @event, Class @class)
583582

584583
private void GenerateEventRaiseWrapper(Event @event, Class @class)
585584
{
586-
var typePrinter = new CppTypePrinter(Context);
587585
var args = typePrinter.VisitParameters(@event.Parameters, hasNames: true);
588586

589587
WriteLine("void {0}::_{1}Raise({2})", QualifiedIdentifier(@class),
@@ -988,8 +986,7 @@ public void GenerateFunctionCall(Function function, Class @class = null, Type pu
988986
function.OperatorKind == CXXOperatorKind.ExplicitConversion)
989987
{
990988
var method = function as Method;
991-
var typePrinter = new CppTypePrinter(Context);
992-
var typeName = method.ConversionType.Visit(typePrinter);
989+
var typeName = method.ConversionType.Visit(new CppTypePrinter(Context));
993990
WriteLine("({0}) {1};", typeName, @params[0].Name);
994991
}
995992
else if (function.IsOperator &&

src/Generator/Generators/CSharp/CSharpMarshal.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public CSharpMarshalContext(BindingContext context, uint indentation)
1717
public bool HasCodeBlock { get; set; }
1818
}
1919

20-
public abstract class CSharpMarshalPrinter : MarshalPrinter<CSharpMarshalContext>
20+
public abstract class CSharpMarshalPrinter : MarshalPrinter<CSharpMarshalContext, CSharpTypePrinter>
2121
{
2222
protected CSharpMarshalPrinter(CSharpMarshalContext context)
2323
: base(context)
@@ -38,7 +38,6 @@ public class CSharpMarshalNativeToManagedPrinter : CSharpMarshalPrinter
3838
public CSharpMarshalNativeToManagedPrinter(CSharpMarshalContext context)
3939
: base(context)
4040
{
41-
typePrinter = new CSharpTypePrinter(context.Context);
4241
}
4342

4443
public override bool VisitType(Type type, TypeQualifiers quals)
@@ -87,7 +86,7 @@ public override bool VisitArrayType(ArrayType array, TypeQualifiers quals)
8786
supportBefore.WriteLine($"for (int i = 0; i < {array.Size}; i++)");
8887
var finalArrayType = arrayType.GetPointee() ?? arrayType;
8988
Class @class;
90-
if ((finalArrayType.TryGetClass(out @class)) && @class.IsRefType)
89+
if (finalArrayType.TryGetClass(out @class) && @class.IsRefType)
9190
{
9291
if (arrayType == finalArrayType)
9392
supportBefore.WriteLineIndent(
@@ -101,11 +100,11 @@ public override bool VisitArrayType(ArrayType array, TypeQualifiers quals)
101100
}
102101
else
103102
{
104-
supportBefore.WriteLineIndent($@"{value}[i] = {Context.ReturnVarName}[i];");
103+
supportBefore.WriteLineIndent($"{value}[i] = {Context.ReturnVarName}[i];");
105104
}
106105
supportBefore.UnindentAndWriteCloseBrace();
107106
Context.Return.Write(value);
108-
}
107+
}
109108
break;
110109
case ArrayType.ArraySize.Incomplete:
111110
// const char* and const char[] are the same so we can use a string
@@ -448,16 +447,13 @@ public bool CheckIfArrayCanBeCopiedUsingMemoryCopy(ArrayType array)
448447
return array.Type.IsPrimitiveType(out var primitive) &&
449448
(!Context.Context.Options.MarshalCharAsManagedChar || primitive != PrimitiveType.Char);
450449
}
451-
452-
private readonly CSharpTypePrinter typePrinter;
453450
}
454451

455452
public class CSharpMarshalManagedToNativePrinter : CSharpMarshalPrinter
456453
{
457454
public CSharpMarshalManagedToNativePrinter(CSharpMarshalContext context)
458455
: base(context)
459456
{
460-
typePrinter = new CSharpTypePrinter(context.Context);
461457
}
462458

463459
public override bool VisitType(Type type, TypeQualifiers quals)
@@ -930,7 +926,5 @@ private void MarshalString(Type pointee)
930926
marshal.Context.Return};");
931927
Context.Return.StringBuilder.Clear();
932928
}
933-
934-
private readonly CSharpTypePrinter typePrinter;
935929
}
936930
}

src/Generator/Generators/CSharp/CSharpTypePrinter.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ public class CSharpTypePrinter : TypePrinter
1616
{
1717
public string IntPtrType => "__IntPtr";
1818

19-
public BindingContext Context { get; set; }
20-
2119
public DriverOptions Options => Context.Options;
2220
public TypeMapDatabase TypeMapDatabase => Context.TypeMaps;
2321

2422
public bool PrintModuleOutputNamespace = true;
2523

24+
public CSharpTypePrinter()
25+
{
26+
}
27+
2628
public CSharpTypePrinter(BindingContext context)
2729
{
2830
Context = context;
@@ -45,7 +47,7 @@ public override TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals
4547

4648
var typePrinterContext = new TypePrinterContext()
4749
{
48-
Kind = Kind,
50+
Kind = ContextKind,
4951
MarshalKind = MarshalKind,
5052
Type = tag
5153
};
@@ -135,7 +137,7 @@ public override TypePrinterResult VisitBuiltinType(BuiltinType builtin, TypeQual
135137
{
136138
var typePrinterContext = new TypePrinterContext()
137139
{
138-
Kind = Kind,
140+
Kind = ContextKind,
139141
MarshalKind = MarshalKind,
140142
Type = builtin,
141143
Parameter = Parameter
@@ -167,7 +169,7 @@ public override TypePrinterResult VisitPointerType(PointerType pointer,
167169
TypeMapDatabase.FindTypeMap(pointer, out typeMap);
168170
var typePrinterContext = new TypePrinterContext()
169171
{
170-
Kind = Kind,
172+
Kind = ContextKind,
171173
MarshalKind = MarshalKind,
172174
Type = pointer.Pointee,
173175
Parameter = Parameter

0 commit comments

Comments
 (0)