From a2782dd83c9ebe528691d1e7c1da0647c1e42561 Mon Sep 17 00:00:00 2001 From: Salvage <29021710+Saalvage@users.noreply.github.com> Date: Thu, 19 Oct 2023 14:26:00 +0200 Subject: [PATCH] Simplify `IsTemplateParameterType` --- src/AST/TypeExtensions.cs | 11 +++-------- src/Generator/Generators/CSharp/CSharpMarshal.cs | 4 ++-- src/Generator/Types/Std/Stdlib.CSharp.cs | 4 ++-- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/AST/TypeExtensions.cs b/src/AST/TypeExtensions.cs index af22e2982..d94551cf7 100644 --- a/src/AST/TypeExtensions.cs +++ b/src/AST/TypeExtensions.cs @@ -439,18 +439,13 @@ public static bool IsDependentPointer(this Type type) return false; } - public static bool IsTemplate(this Type type) + public static bool IsTemplateParameterType(this Type type) { if (type is TemplateParameterType or TemplateParameterSubstitutionType) return true; - var ptr = type; - while (ptr is PointerType pType) - { - ptr = pType.Pointee; - if (ptr is TemplateParameterType or TemplateParameterSubstitutionType) - return true; - } + if (type is PointerType pt) + return pt.GetFinalPointee() is TemplateParameterType or TemplateParameterSubstitutionType; return false; } diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index dea8aa5da..eec484e05 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -644,7 +644,7 @@ public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals) else { Context.Before.Write($"var {arg} = "); - if (pointer.Pointee.IsTemplate()) + if (pointer.Pointee.IsTemplateParameterType()) Context.Before.Write($"(({Context.Parameter.Type}) (object) {Context.Parameter.Name})"); else Context.Before.WriteLine(Context.Parameter.Name); @@ -810,7 +810,7 @@ private void MarshalRefClass(Class @class) private void MarshalValueClass() { - if (Context.Parameter.Type.IsTemplate()) + if (Context.Parameter.Type.IsTemplateParameterType()) Context.Return.Write($"(({Context.Parameter.Type}) (object) {Context.Parameter.Name})"); else Context.Return.Write(Context.Parameter.Name); diff --git a/src/Generator/Types/Std/Stdlib.CSharp.cs b/src/Generator/Types/Std/Stdlib.CSharp.cs index c197a6655..a2db882d7 100644 --- a/src/Generator/Types/Std/Stdlib.CSharp.cs +++ b/src/Generator/Types/Std/Stdlib.CSharp.cs @@ -330,7 +330,7 @@ public override void CSharpMarshalToNative(CSharpMarshalContext ctx) Helpers.InternalStruct}.{assign.Name}(new { typePrinter.IntPtrType}(&{ ctx.ReturnVarName}), "); - if (ctx.Parameter.Type.IsTemplate()) + if (ctx.Parameter.Type.IsTemplateParameterType()) ctx.Return.Write("(string) (object) "); ctx.Return.Write($"{ctx.Parameter.Name})"); ctx.ReturnVarName = string.Empty; @@ -343,7 +343,7 @@ public override void CSharpMarshalToNative(CSharpMarshalContext ctx) ctx.Before.Write($@"{qualifiedBasicString}Extensions.{ assign.Name}({varBasicString}, "); - if (ctx.Parameter.Type.IsTemplate()) + if (ctx.Parameter.Type.IsTemplateParameterType()) ctx.Before.Write("(string) (object) "); ctx.Before.WriteLine($"{ctx.Parameter.Name});");