Skip to content

Commit

Permalink
call by value: handle cctors with default parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Anderegg committed Nov 22, 2022
1 parent 57f6046 commit f8b518c
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Generator/Generators/CSharp/CSharpMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -776,10 +776,18 @@ private void MarshalRefClass(Class @class)
Context.Parameter.Name}"", ""Cannot be null because it is passed by value."");");

var nativeClass = typePrinter.PrintNative(@class);

var cctorName = CSharpSources.GetFunctionNativeIdentifier(Context.Context, cctor);

var defaultValue = "";
var TypePrinter = new CSharpTypePrinter(Context.Context);
var ExpressionPrinter = new CSharpExpressionPrinter(TypePrinter);
if (cctor.Parameters.Count > 1)
defaultValue = $", {ExpressionPrinter.VisitParameter(cctor.Parameters.Last())}";

Context.Before.WriteLine($"byte* __{Context.Parameter.Name}Memory = stackalloc byte[sizeof({nativeClass})];");
Context.Before.WriteLine($"__IntPtr __{Context.Parameter.Name}Ptr = (__IntPtr)__{Context.Parameter.Name}Memory;");
Context.Before.WriteLine($"{nativeClass}.{cctorName}(__{Context.Parameter.Name}Ptr, {Context.Parameter.Name}.__Instance);");
Context.Before.WriteLine($"{nativeClass}.{cctorName}(__{Context.Parameter.Name}Ptr, {Context.Parameter.Name}.__Instance{defaultValue});");
Context.Return.Write($"__{Context.Parameter.Name}Ptr");

if (Context.Context.ParserOptions.IsItaniumLikeAbi && @class.HasNonTrivialDestructor)
Expand Down

0 comments on commit f8b518c

Please sign in to comment.