From efdec41f136301d246c82fbf8287c5c3793945d4 Mon Sep 17 00:00:00 2001 From: josetr <37419832+josetr@users.noreply.github.com> Date: Fri, 7 Oct 2022 12:05:19 +0100 Subject: [PATCH] Add missing GenerateNativeToManagedFor checks --- src/AST/TypeExtensions.cs | 9 +++++++-- src/Generator/Generators/CSharp/CSharpMarshal.cs | 5 +++-- src/Generator/Generators/CSharp/CSharpSources.cs | 6 +++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/AST/TypeExtensions.cs b/src/AST/TypeExtensions.cs index d929563d..eb447d09 100644 --- a/src/AST/TypeExtensions.cs +++ b/src/AST/TypeExtensions.cs @@ -443,8 +443,9 @@ return array.Size * array.ElementSize; } - internal static bool IsReferenceToPtrToClass(this Type type) + internal static bool TryGetReferenceToPtrToClass(this Type type, out Type classType) { + classType = null; var @ref = type.Desugar().AsLvReference(); if (@ref == null) return false; @@ -454,7 +455,11 @@ return false; var @class = @ptr.Pointee; - return @class != null && @class.IsClass(); + if (!@class.IsClass()) + return false; + + classType = @class; + return true; } internal static PointerType AsLvReference(this Type type) diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 6b6e6534..f2d11426 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -393,8 +393,9 @@ namespace CppSharp.Generators.CSharp var dtorVirtual = (dtor != null && dtor.IsVirtual); var cache = dtorVirtual && Context.Parameter == null; var skipVTables = dtorVirtual && Context.Parameter != null; - Context.Before.WriteLine("var {0} = {1}.__GetOrCreateInstance({2}, {3}{4});", - ret, qualifiedClass, Context.ReturnVarName, cache ? "true" : "false", skipVTables ? ", skipVTables: true" : string.Empty); + var get = Context.Context.Options.GenerateNativeToManagedFor(@class) ? "GetOr" : ""; + Context.Before.WriteLine("var {0} = {1}.__{5}CreateInstance({2}, {3}{4});", + ret, qualifiedClass, Context.ReturnVarName, cache ? "true" : "false", skipVTables ? ", skipVTables: true" : string.Empty, get); } else { diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index fc4aa9c6..bdbb18e9 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -3141,12 +3141,12 @@ internal static{(@new ? " new" : string.Empty)} {printedClass} __GetInstance({Ty foreach (var param in @params) { - if (param.Param.IsInOut && param.Param.Type.IsReferenceToPtrToClass()) + if (param.Param.IsInOut && param.Param.Type.TryGetReferenceToPtrToClass(out var classType) && classType.TryGetClass(out var @class)) { var qualifiedClass = param.Param.Type.Visit(TypePrinter); - + var get = Options.GenerateNativeToManagedFor(@class) ? "GetOr" : ""; WriteLine($"if ({param.Name} != {param.Param.Name}.__Instance)"); - WriteLine($"{param.Param.Name} = {qualifiedClass}.__GetOrCreateInstance(__{param.Name}, false);"); + WriteLine($"{param.Param.Name} = {qualifiedClass}.__{get}CreateInstance(__{param.Name}, false);"); } }