diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 3dfbd1e8..fc9f65f7 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 ae60637d..090c6f0b 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(); @@ -1001,7 +1002,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; @@ -1048,7 +1051,8 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat { Parameter = param, ArgName = param.Name, - ReturnVarName = returnVar + ReturnVarName = returnVar, + Field = field, }; ctx.PushMarshalKind(MarshalKind.NativeField); @@ -1147,7 +1151,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); @@ -1287,7 +1292,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); @@ -1338,7 +1343,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; @@ -1410,7 +1417,8 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat { ArgName = field.Name, ReturnVarName = returnVar, - ReturnType = returnType + ReturnType = returnType, + Field = field, }; ctx.PushMarshalKind(MarshalKind.NativeField); @@ -1555,7 +1563,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)); @@ -1931,7 +1941,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); @@ -2076,7 +2087,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); } @@ -2772,12 +2785,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; } @@ -2987,6 +3004,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) { @@ -2995,6 +3013,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 ba100fc7..2ad41a90 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -267,7 +267,7 @@ namespace CppSharp.Generators.CSharp Kind = ContextKind, MarshalKind = MarshalKind, Type = typedef, - Parameter = Parameter + Parameter = Parameter, }; return typeMap.SignatureType(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 1dcd9dce..96fb18da 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 807b81b4..70d7d417 100644 --- a/src/Generator/Generators/TypePrinter.cs +++ b/src/Generator/Generators/TypePrinter.cs @@ -86,6 +86,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 690bb092..b4d0d82f 100644 --- a/src/Generator/Passes/ValidateOperatorsPass.cs +++ b/src/Generator/Passes/ValidateOperatorsPass.cs @@ -96,7 +96,8 @@ namespace CppSharp.Passes new TypePrinterContext { Parameter = parameter, - Type = type + Type = type, + Method = @operator, }); var cilType = mappedTo as CILType; if (cilType?.Type == typeof(int))