Skip to content

Commit

Permalink
Simplify IsTemplateParameterType
Browse files Browse the repository at this point in the history
  • Loading branch information
Saalvage committed Oct 19, 2023
1 parent 19aa51b commit a2782dd
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
11 changes: 3 additions & 8 deletions src/AST/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/Generators/CSharp/CSharpMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/Types/Std/Stdlib.CSharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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});");

Expand Down

0 comments on commit a2782dd

Please sign in to comment.