From 38bc0e4d7b3a77688f4e1aea75d53f8ae8f051af Mon Sep 17 00:00:00 2001 From: Salvage <29021710+Saalvage@users.noreply.github.com> Date: Sun, 22 Oct 2023 02:00:10 +0200 Subject: [PATCH] Provide `TypePrinter`s with more context --- .../Generators/CSharp/CSharpMarshal.cs | 14 +++++++-- .../Generators/CSharp/CSharpSources.cs | 31 +++++++++++++++---- .../Generators/CSharp/CSharpTypePrinter.cs | 4 ++- src/Generator/Generators/ITypePrinter.cs | 8 +++++ src/Generator/Generators/Marshal.cs | 1 - src/Generator/Generators/TypePrinter.cs | 11 +++++++ src/Generator/Passes/ValidateOperatorsPass.cs | 3 +- 7 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 09ced7d1..b7248ce2 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -826,10 +826,14 @@ namespace CppSharp.Generators.CSharp Context.Parameter = new Parameter { Name = Context.ArgName, - QualifiedType = field.QualifiedType + QualifiedType = field.QualifiedType, }; - return field.Type.Visit(this, field.QualifiedType.Qualifiers); + Context.Field = field; + var ret = field.Type.Visit(this, field.QualifiedType.Qualifiers); + Context.Field = null; + + return ret; } public override bool VisitProperty(Property property) @@ -843,7 +847,11 @@ namespace CppSharp.Generators.CSharp QualifiedType = property.QualifiedType }; - return base.VisitProperty(property); + Context.Property = property; + var ret = base.VisitProperty(property); + Context.Property = null; + + return ret; } public override bool VisitEnumDecl(Enumeration @enum) diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 1abdb262..074e3fde 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -694,11 +694,12 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat private IEnumerable GatherInternalParams(Function function, out TypePrinterResult retType) { TypePrinter.PushContext(TypePrinterContextKind.Native); - retType = function.ReturnType.Visit(TypePrinter); + TypePrinter.Function = function; var @params = function.GatherInternalParams(Context.ParserOptions.IsItaniumLikeAbi).Select(p => $"{p.Visit(TypePrinter)} {p.Name}").ToList(); + TypePrinter.Function = null; TypePrinter.PopContext(); @@ -995,7 +996,9 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat var actualProperty = GetActualProperty(property, @class); if (actualProperty == null) { + TypePrinter.Property = property; WriteLine($@"throw new MissingMethodException(""Method {property.Name} missing from explicit specialization {@class.Visit(TypePrinter)}."");"); + TypePrinter.Property = null; return false; } property = actualProperty; @@ -1042,7 +1045,8 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat { Parameter = param, ArgName = param.Name, - ReturnVarName = returnVar + ReturnVarName = returnVar, + Field = field, }; ctx.PushMarshalKind(MarshalKind.NativeField); @@ -1141,7 +1145,8 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat ParameterIndex = function.Parameters.Count( p => p.Kind != ParameterKind.IndirectReturnType), ReturnType = new QualifiedType(type), - ArgName = "value" + ArgName = "value", + Function = function, }; var marshal = new CSharpMarshalManagedToNativePrinter(ctx); type.Visit(marshal); @@ -1282,7 +1287,7 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat var ctx = new CSharpMarshalContext(Context, CurrentIndentation) { ArgName = var.Name, - ReturnType = var.QualifiedType + ReturnType = var.QualifiedType, }; ctx.PushMarshalKind(MarshalKind.ReturnVariableArray); @@ -1333,7 +1338,9 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat var actualProperty = GetActualProperty(property, @class); if (actualProperty == null) { + TypePrinter.Property = property; WriteLine($@"throw new MissingMethodException(""Method {property.Name} missing from explicit specialization {@class.Visit(TypePrinter)}."");"); + TypePrinter.Property = null; return false; } QualifiedType type = default; @@ -1405,7 +1412,8 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat { ArgName = field.Name, ReturnVarName = returnVar, - ReturnType = returnType + ReturnType = returnType, + Field = field, }; ctx.PushMarshalKind(MarshalKind.NativeField); @@ -1550,7 +1558,9 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat } GenerateDeclarationCommon(prop); + TypePrinter.Property = prop; var printedType = prop.Type.Visit(TypePrinter); + TypePrinter.Property = null; if (prop.ExplicitInterfaceImpl == null) { Write(Helpers.GetAccess(prop.Access)); @@ -1926,7 +1936,8 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat ReturnType = param.QualifiedType, ReturnVarName = param.Name, ParameterIndex = i, - Parameter = param + Parameter = param, + Function = method, }; ctx.PushMarshalKind(MarshalKind.GenericDelegate); @@ -2071,7 +2082,9 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat using (WriteBlock($"private static {retType} {vTableMethodDelegateName}Hook({string.Join(", ", @params)})")) { + TypePrinter.Function = method; WriteLine($@"var {Helpers.TargetIdentifier} = {@class.Visit(TypePrinter)}.__GetInstance({Helpers.InstanceField});"); + TypePrinter.Function = null; GenerateVTableManagedCall(method); } @@ -2735,12 +2748,16 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty m => m.InstantiatedFrom == (method.OriginalFunction ?? method)); if (specializedMethod == null) { + TypePrinter.Function = method; WriteLine($@"throw new MissingMethodException(""Method {method.Name} missing from explicit specialization {@class.Visit(TypePrinter)}."");"); + TypePrinter.Function = null; return false; } if (specializedMethod.Ignore) { + TypePrinter.Function = method; WriteLine($@"throw new MissingMethodException(""Method {method.Name} ignored in specialization {@class.Visit(TypePrinter)}."");"); + TypePrinter.Function = null; return false; } @@ -2950,6 +2967,7 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty parameter.Type.IsPrimitiveTypeConvertibleToRef() ? "ref *" : string.Empty, parameter.Name); + TypePrinter.Function = method; var printedType = method.ConversionType.Visit(TypePrinter); if (@interface != null) { @@ -2958,6 +2976,7 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty } else WriteLine($"return new {printedType}({paramName});"); + TypePrinter.Function = null; } else { diff --git a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs index 854b4e54..5d1c721e 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -272,7 +272,7 @@ namespace CppSharp.Generators.CSharp Kind = ContextKind, MarshalKind = MarshalKind, Type = typedef, - Parameter = Parameter + Parameter = Parameter, }; return typeMap.CSharpSignatureType(typePrinterContext).ToString(); @@ -698,7 +698,9 @@ $"[{Context.TargetInfo.LongDoubleWidth}]"); public override TypePrinterResult VisitFieldDecl(Field field) { PushMarshalKind(MarshalKind.NativeField); + Field = field; var fieldTypePrinted = field.QualifiedType.Visit(this); + Field = null; PopMarshalKind(); var returnTypePrinter = new TypePrinterResult(); diff --git a/src/Generator/Generators/ITypePrinter.cs b/src/Generator/Generators/ITypePrinter.cs index 20d4bf87..3d9ccb4c 100644 --- a/src/Generator/Generators/ITypePrinter.cs +++ b/src/Generator/Generators/ITypePrinter.cs @@ -36,6 +36,14 @@ namespace CppSharp.AST public MarshalKind MarshalKind; public Declaration Declaration; public Parameter Parameter; + public Property Property; + private Field field; + public Field Field + { + get => field ?? Property.Field; + set => field = value; + } + public Method Method; public Type Type; public TypePrinterContext() : this(TypePrinterContextKind.Normal) diff --git a/src/Generator/Generators/Marshal.cs b/src/Generator/Generators/Marshal.cs index b1798fdb..bb27eac3 100644 --- a/src/Generator/Generators/Marshal.cs +++ b/src/Generator/Generators/Marshal.cs @@ -29,7 +29,6 @@ namespace CppSharp.Generators public string ArgName { get; set; } public int ParameterIndex { get; set; } - public Function Function { get; set; } public uint Indentation { get; } } diff --git a/src/Generator/Generators/TypePrinter.cs b/src/Generator/Generators/TypePrinter.cs index 806a7192..cf051251 100644 --- a/src/Generator/Generators/TypePrinter.cs +++ b/src/Generator/Generators/TypePrinter.cs @@ -85,6 +85,17 @@ namespace CppSharp.Generators public Parameter Parameter; + private Field field; + public Field Field + { + get => field ?? Property.Field; + set => field = value; + } + + public Property Property; + + public Function Function; + #region Dummy implementations public virtual string ToString(CppSharp.AST.Type type) diff --git a/src/Generator/Passes/ValidateOperatorsPass.cs b/src/Generator/Passes/ValidateOperatorsPass.cs index b1f86db1..5448fe0d 100644 --- a/src/Generator/Passes/ValidateOperatorsPass.cs +++ b/src/Generator/Passes/ValidateOperatorsPass.cs @@ -95,7 +95,8 @@ namespace CppSharp.Passes new TypePrinterContext { Parameter = parameter, - Type = type + Type = type, + Method = @operator, }); var cilType = mappedTo as CILType; if (cilType?.Type == typeof(int))