Skip to content

Commit

Permalink
Fix generated code problems with nullable primitive return values.
Browse files Browse the repository at this point in the history
  • Loading branch information
kring committed Dec 6, 2023
1 parent 5c53765 commit c04eebf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Reinterop~/CppType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static CppType FromCSharp(CppGenerationContext context, ITypeSymbol type)
if (named != null && named.Name == "Nullable" && named.TypeArguments.Length == 1)
{
CppType nullabledType = CppType.FromCSharp(context, named.TypeArguments[0]);
if (nullabledType.Kind == InteropTypeKind.BlittableStruct)
if (nullabledType.Kind == InteropTypeKind.BlittableStruct || nullabledType.Kind == InteropTypeKind.Primitive)
return new CppType(InteropTypeKind.Nullable, new[] {"std"}, "optional", new[] { nullabledType }, 0, "<optional>");
}

Expand Down Expand Up @@ -463,6 +463,7 @@ public string GetConversionToInteropType(CppGenerationContext context, string va
return $"::std::uint32_t({variableName})";
case InteropTypeKind.EnumFlags:
return $"{variableName}.underlying_value()";
case InteropTypeKind.Primitive:
case InteropTypeKind.BlittableStruct:
if (this.Flags.HasFlag(CppTypeFlags.Reference))
return $"&{variableName}";
Expand All @@ -473,7 +474,6 @@ public string GetConversionToInteropType(CppGenerationContext context, string va
return $"{variableName}.has_value() ? &{variableName}.value() : nullptr";
else
return variableName;
case InteropTypeKind.Primitive:
case InteropTypeKind.Unknown:
default:
return variableName;
Expand Down
6 changes: 4 additions & 2 deletions Reinterop~/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static (string Name, string Content) CreateCSharpDelegateInit(
CSharpType csReturnType = CSharpType.FromSymbol(context, returnType);
CSharpType csInteropReturnType = csReturnType.AsInteropTypeReturn();

// Rewrite methods that return a blittable struct to instead taking a pointer to one.
// Rewrite methods that return a blittable struct to instead take a pointer to one.
// See Interop.RewriteStructReturn in this file for the C++ side of this and more
// explanation of why it's needed.
bool hasStructRewrite = false;
Expand Down Expand Up @@ -600,7 +600,9 @@ public static bool RewriteStructReturn(ref IEnumerable<(string ParameterName, st

return true;
}
else if (returnType.Kind == InteropTypeKind.Nullable && interopReturnType.Kind == InteropTypeKind.BlittableStruct)
else if (returnType.Kind == InteropTypeKind.Nullable &&
(interopReturnType.Kind == InteropTypeKind.BlittableStruct ||
interopReturnType.Kind == InteropTypeKind.Primitive))
{
CppType originalInteropReturnType = interopReturnType;
interopReturnType = CppType.UInt8;
Expand Down

0 comments on commit c04eebf

Please sign in to comment.