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 09ced7d119..b7248ce284 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -826,10 +826,14 @@ public override bool VisitFieldDecl(Field field) 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 @@ public override bool VisitProperty(Property property) 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 1abdb262e5..074e3fde8a 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -694,11 +694,12 @@ private void GatherClassInternalFunctions(Class @class, bool includeCtors, 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 @@ private bool GenerateFunctionSetter(Class @class, Property property) 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 @@ private void GenerateFieldSetter(Field field, Class @class, QualifiedType fieldT { Parameter = param, ArgName = param.Name, - ReturnVarName = returnVar + ReturnVarName = returnVar, + Field = field, }; ctx.PushMarshalKind(MarshalKind.NativeField); @@ -1141,7 +1145,8 @@ private void GenerateIndexerSetter(Function function) 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 @@ private bool GenerateVariableGetter(Variable var) var ctx = new CSharpMarshalContext(Context, CurrentIndentation) { ArgName = var.Name, - ReturnType = var.QualifiedType + ReturnType = var.QualifiedType, }; ctx.PushMarshalKind(MarshalKind.ReturnVariableArray); @@ -1333,7 +1338,9 @@ private bool GenerateFunctionGetter(Class @class, Property property) 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 @@ private void GenerateFieldGetter(Field field, Class @class, QualifiedType return { ArgName = field.Name, ReturnVarName = returnVar, - ReturnType = returnType + ReturnType = returnType, + Field = field, }; ctx.PushMarshalKind(MarshalKind.NativeField); @@ -1550,7 +1558,9 @@ private void GenerateProperties(Class @class) } 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 @@ private void GenerateVTableManagedCall(Method method) ReturnType = param.QualifiedType, ReturnVarName = param.Name, ParameterIndex = i, - Parameter = param + Parameter = param, + Function = method, }; ctx.PushMarshalKind(MarshalKind.GenericDelegate); @@ -2071,7 +2082,9 @@ private void GenerateVTableMethodDelegates(Class @class, Method method) 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 @@ private bool GenerateMethodBody(Class @class, Method method, 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 @@ private void GenerateOperator(Method method, QualifiedType returnType) parameter.Type.IsPrimitiveTypeConvertibleToRef() ? "ref *" : string.Empty, parameter.Name); + TypePrinter.Function = method; var printedType = method.ConversionType.Visit(TypePrinter); if (@interface != null) { @@ -2958,6 +2976,7 @@ private void GenerateOperator(Method method, QualifiedType returnType) } 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 854b4e5434..5d1c721eea 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -272,7 +272,7 @@ public override TypePrinterResult VisitTypedefType(TypedefType typedef, Kind = ContextKind, MarshalKind = MarshalKind, Type = typedef, - Parameter = Parameter + Parameter = Parameter, }; return typeMap.CSharpSignatureType(typePrinterContext).ToString(); @@ -698,7 +698,9 @@ private static string GetParameterUsage(ParameterUsage usage) 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 20d4bf87bc..3d9ccb4cf0 100644 --- a/src/Generator/Generators/ITypePrinter.cs +++ b/src/Generator/Generators/ITypePrinter.cs @@ -36,6 +36,14 @@ public class TypePrinterContext 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 b1798fdbe4..bb27eac3f4 100644 --- a/src/Generator/Generators/Marshal.cs +++ b/src/Generator/Generators/Marshal.cs @@ -29,7 +29,6 @@ public MarshalContext(BindingContext context, uint indentation) 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 806a719217..cf051251c9 100644 --- a/src/Generator/Generators/TypePrinter.cs +++ b/src/Generator/Generators/TypePrinter.cs @@ -85,6 +85,17 @@ public TypePrinter(TypePrinterContextKind contextKind = TypePrinterContextKind.M 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 b1f86db1e6..5448fe0d9a 100644 --- a/src/Generator/Passes/ValidateOperatorsPass.cs +++ b/src/Generator/Passes/ValidateOperatorsPass.cs @@ -95,7 +95,8 @@ private bool IsValidOperatorOverload(Method @operator) new TypePrinterContext { Parameter = parameter, - Type = type + Type = type, + Method = @operator, }); var cilType = mappedTo as CILType; if (cilType?.Type == typeof(int))