Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't create copies of indirectly returned values #1241

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Generator/Generators/CSharp/CSharpMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,13 @@ public override bool VisitClassDecl(Class @class)

if (returnType.IsAddress())
Context.Return.Write(HandleReturnedPointer(@class, qualifiedClass.Type));
else
Context.Return.Write($"{qualifiedClass}.{Helpers.CreateInstanceIdentifier}({Context.ReturnVarName})");
else if (Context.MarshalKind == MarshalKind.NativeField ||
Context.Function?.HasIndirectReturnTypeParameter == true ||
Context.Function?.OperatorKind == CXXOperatorKind.Subscript)
Context.Return.Write($@"{qualifiedClass}.{Helpers.CreateInstanceIdentifier}({
Context.ReturnVarName})");
else Context.Return.Write($@"{qualifiedClass}.{Helpers.CreateInstanceIdentifier}(new {
typePrinter.IntPtrType}(&{Context.ReturnVarName}))");

return true;
}
Expand Down
15 changes: 6 additions & 9 deletions src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2808,6 +2808,7 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters

var originalFunction = function.OriginalFunction ?? function;

var names = new List<string>();
if (originalFunction.HasIndirectReturnTypeParameter)
{
var indirectRetType = originalFunction.Parameters.First(
Expand All @@ -2825,8 +2826,8 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters
Class retClass;
type.TryGetClass(out retClass);
var @class = retClass.OriginalClass ?? retClass;
WriteLine($@"var {Helpers.ReturnIdentifier} = new {
TypePrinter.PrintNative(@class)}();");
WriteLine($@"var {Helpers.ReturnIdentifier} = Marshal.AllocHGlobal({
@class.Layout.GetSize()});");
}
else
{
Expand All @@ -2839,13 +2840,15 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters

WriteLine("{0} {1};", typeMap.CSharpSignatureType(typePrinterContext),
Helpers.ReturnIdentifier);
names.Add($"new IntPtr(&{Helpers.ReturnIdentifier})");
}
else
WriteLine("var {0} = {1};", construct);
}
if (names.Count == 0)
names.Add(Helpers.ReturnIdentifier);
}

var names = new List<string>();
foreach (var param in @params)
{
if (param.Param == operatorParam && needsInstance)
Expand All @@ -2862,12 +2865,6 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters

var needsFixedThis = needsInstance && isValueType;

if (originalFunction.HasIndirectReturnTypeParameter)
{
var name = string.Format("new IntPtr(&{0})", Helpers.ReturnIdentifier);
names.Insert(0, name);
}

if (needsInstance)
{
var instanceIndex = GetInstanceParamIndex(method);
Expand Down
3 changes: 1 addition & 2 deletions src/Generator/Types/Std/Stdlib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,7 @@ public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
ctx.MarshalKind == MarshalKind.ReturnVariableArray;
ctx.Before.WriteLine($@"var {varBasicString} = {
basicString.Visit(typePrinter)}.{Helpers.CreateInstanceIdentifier}({
(usePointer ? string.Empty : $"new {typePrinter.IntPtrType}(&")}{
ctx.ReturnVarName}{(usePointer ? string.Empty : ")")});");
ctx.ReturnVarName});");
string @string = $"{qualifiedBasicString}Extensions.{data.Name}({varBasicString})";
if (usePointer)
{
Expand Down