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

Prefer zero terminated string array over length based arrays. #962

Merged
merged 1 commit into from
Oct 13, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ public bool Supports(GirModel.AnyType anyType)

public RenderableParameter Convert(GirModel.Parameter parameter)
{
return parameter.AnyTypeOrVarArgs.AsT0.AsT1.Length is null
? ParameterWithoutLength(parameter)
: ParameterWithLength(parameter);
var arrayType = parameter.AnyTypeOrVarArgs.AsT0.AsT1;

if (arrayType.IsZeroTerminated)
return NullTerminatedArray(parameter);

if (arrayType.Length is not null)
return SizeBasedArray(parameter);

throw new Exception("Unknown kind of array");
}

private static RenderableParameter ParameterWithLength(GirModel.Parameter parameter)
private static RenderableParameter SizeBasedArray(GirModel.Parameter parameter)
{
var length = parameter.AnyTypeOrVarArgs.AsT0.AsT1.Length ?? throw new Exception("Length must not be null");

Expand All @@ -28,21 +34,18 @@ private static RenderableParameter ParameterWithLength(GirModel.Parameter parame
);
}

private static RenderableParameter ParameterWithoutLength(GirModel.Parameter parameter)
private static RenderableParameter NullTerminatedArray(GirModel.Parameter parameter)
{
return parameter.Direction switch
{
GirModel.Direction.In => ParameterWithoutLengthIn(parameter),
GirModel.Direction.Out => ParameterWithoutLengthOut(parameter),
GirModel.Direction.In => NullTerminatedArrayIn(parameter),
GirModel.Direction.Out => NullTerminatedArrayOut(parameter),
_ => throw new Exception("Unknown direction for parameter")
};
}

private static RenderableParameter ParameterWithoutLengthIn(GirModel.Parameter parameter)
private static RenderableParameter NullTerminatedArrayIn(GirModel.Parameter parameter)
{
if (parameter.AnyTypeOrVarArgs.AsT0.AsT1.Length is not null)
throw new Exception("Length must be null");

if (parameter.Direction != GirModel.Direction.In)
throw new Exception("Direction must be in");

Expand All @@ -62,11 +65,8 @@ private static RenderableParameter ParameterWithoutLengthIn(GirModel.Parameter p
);
}

private static RenderableParameter ParameterWithoutLengthOut(GirModel.Parameter parameter)
private static RenderableParameter NullTerminatedArrayOut(GirModel.Parameter parameter)
{
if (parameter.AnyTypeOrVarArgs.AsT0.AsT1.Length is not null)
throw new Exception("Length must be null");

if (parameter.Direction != GirModel.Direction.Out)
throw new Exception("Direction must be out");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ public bool Supports(GirModel.AnyType anyType)

public RenderableParameter Convert(GirModel.Parameter parameter)
{
return parameter.AnyTypeOrVarArgs.AsT0.AsT1.Length is null
? ParameterWithoutLength(parameter)
: ParameterWithLength(parameter);
var arrayType = parameter.AnyTypeOrVarArgs.AsT0.AsT1;

if (arrayType.IsZeroTerminated)
return NullTerminatedArray(parameter);

if (arrayType.Length is not null)
return SizeBasedArray(parameter);

throw new Exception("Unknown kind of array");
}

private static RenderableParameter ParameterWithLength(GirModel.Parameter parameter)
private static RenderableParameter SizeBasedArray(GirModel.Parameter parameter)
{
var length = parameter.AnyTypeOrVarArgs.AsT0.AsT1.Length ?? throw new Exception("Length must not be null");

Expand All @@ -28,21 +34,18 @@ private static RenderableParameter ParameterWithLength(GirModel.Parameter parame
);
}

private static RenderableParameter ParameterWithoutLength(GirModel.Parameter parameter)
private static RenderableParameter NullTerminatedArray(GirModel.Parameter parameter)
{
return parameter.Direction switch
{
GirModel.Direction.In => ParameterWithoutLengthIn(parameter),
GirModel.Direction.Out => ParameterWithoutLengthOut(parameter),
GirModel.Direction.In => NullTerminatedArrayIn(parameter),
GirModel.Direction.Out => NullTerminatedArrayOut(parameter),
_ => throw new Exception("Unknown direction for parameter")
};
}

private static RenderableParameter ParameterWithoutLengthIn(GirModel.Parameter parameter)
private static RenderableParameter NullTerminatedArrayIn(GirModel.Parameter parameter)
{
if (parameter.AnyTypeOrVarArgs.AsT0.AsT1.Length is not null)
throw new Exception("Length must be null");

if (parameter.Direction != GirModel.Direction.In)
throw new Exception("Direction must be in");

Expand All @@ -62,11 +65,8 @@ private static RenderableParameter ParameterWithoutLengthIn(GirModel.Parameter p
);
}

private static RenderableParameter ParameterWithoutLengthOut(GirModel.Parameter parameter)
private static RenderableParameter NullTerminatedArrayOut(GirModel.Parameter parameter)
{
if (parameter.AnyTypeOrVarArgs.AsT0.AsT1.Length is not null)
throw new Exception("Length must be null");

if (parameter.Direction != GirModel.Direction.Out)
throw new Exception("Direction must be out");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ public bool Supports(GirModel.AnyType type)
public void Initialize(ParameterToManagedData parameterData, IEnumerable<ParameterToManagedData> parameters)
{
var arrayType = parameterData.Parameter.AnyTypeOrVarArgs.AsT0.AsT1;
if (arrayType.Length is null)

if (arrayType.IsZeroTerminated)
NullTerminatedArray(parameterData);
else
else if (arrayType.Length is not null)
SizeBasedArray(parameterData);
else
throw new Exception("Unknown kind of array");
}

private static void NullTerminatedArray(ParameterToManagedData parameterData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ public bool Supports(GirModel.AnyType type)
public void Initialize(ParameterToManagedData parameterData, IEnumerable<ParameterToManagedData> parameters)
{
var arrayType = parameterData.Parameter.AnyTypeOrVarArgs.AsT0.AsT1;
if (arrayType.Length is null)

if (arrayType.IsZeroTerminated)
NullTerminatedArray(parameterData);
else
else if (arrayType.Length is not null)
SizeBasedArray(parameterData);
else
throw new Exception("Unknown kind of array");
}

private static void NullTerminatedArray(ParameterToManagedData parameterData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,30 @@ public RenderableReturnType Convert(GirModel.ReturnType returnType)
{
var arrayType = returnType.AnyType.AsT1;

var nullableTypeName = arrayType.Length is null
? NullTerminatedArray(returnType)
: SizeBasedArray();
if (arrayType.IsZeroTerminated)
return NullTerminatedArray(returnType);

return new RenderableReturnType(nullableTypeName);
if (arrayType.Length is not null)
return SizeBasedArray();

throw new Exception("Unknown kind of array");
}

private static string NullTerminatedArray(GirModel.ReturnType returnType)
private static RenderableReturnType NullTerminatedArray(GirModel.ReturnType returnType)
{
return returnType switch
var typeName = returnType switch
{
{ Transfer: GirModel.Transfer.Full } => Model.PlatformStringArray.GetInternalOwnedHandleName(),
{ Transfer: GirModel.Transfer.None } => Model.PlatformStringArray.GetInternalUnownedHandleName(),
{ Transfer: GirModel.Transfer.Container } => Model.PlatformStringArray.GetInternalContainerHandleName(),
_ => throw new Exception("Unknown transfer type for platform string array return value")
};

return new RenderableReturnType(typeName);
}

private static string SizeBasedArray()
private static RenderableReturnType SizeBasedArray()
{
return "string[]";
return new RenderableReturnType("string[]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,30 @@ public RenderableReturnType Convert(GirModel.ReturnType returnType)
{
var arrayType = returnType.AnyType.AsT1;

var nullableTypeName = arrayType.Length is null
? NullTerminatedArray(returnType)
: SizeBasedArray();
if (arrayType.IsZeroTerminated)
return NullTerminatedArray(returnType);

return new RenderableReturnType(nullableTypeName);
if (arrayType.Length is not null)
return SizeBasedArray();

throw new Exception("Unknown kind of array");
}

private static string NullTerminatedArray(GirModel.ReturnType returnType)
private static RenderableReturnType NullTerminatedArray(GirModel.ReturnType returnType)
{
return returnType switch
var typeName = returnType switch
{
{ Transfer: GirModel.Transfer.Full } => Model.Utf8StringArray.GetInternalOwnedHandleName(),
{ Transfer: GirModel.Transfer.None } => Model.Utf8StringArray.GetInternalUnownedHandleName(),
{ Transfer: GirModel.Transfer.Container } => Model.Utf8StringArray.GetInternalContainerHandleName(),
_ => throw new Exception("Unknown transfer type for utf8 string array return value")
};

return new RenderableReturnType(typeName);
}

private static string SizeBasedArray()
private static RenderableReturnType SizeBasedArray()
{
return "string[]";
return new RenderableReturnType("string[]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ public bool Supports(GirModel.AnyType type)
public void Initialize(ParameterToNativeData parameter, IEnumerable<ParameterToNativeData> _)
{
var arrayType = parameter.Parameter.AnyTypeOrVarArgs.AsT0.AsT1;
if (arrayType.Length == null)

if (arrayType.IsZeroTerminated)
NullTerminatedArray(parameter);
else
else if (arrayType.Length is not null)
SizeBasedArray(parameter);
else
throw new Exception("Unknown kind of array");
}

private static void NullTerminatedArray(ParameterToNativeData parameter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ public bool Supports(GirModel.AnyType type)
public void Initialize(ParameterToNativeData parameter, IEnumerable<ParameterToNativeData> _)
{
var arrayType = parameter.Parameter.AnyTypeOrVarArgs.AsT0.AsT1;
if (arrayType.Length is null)

if (arrayType.IsZeroTerminated)
NullTerminatedArray(parameter);
else
else if (arrayType.Length is not null)
SizeBasedArray(parameter);
else
throw new Exception("Unknown kind of array");
}

private static void NullTerminatedArray(ParameterToNativeData parameter)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Generator.Renderer.Public.ReturnTypeToManagedExpressions;
using System;

namespace Generator.Renderer.Public.ReturnTypeToManagedExpressions;

internal class PlatformStringArray : ReturnTypeConverter
{
Expand All @@ -7,10 +9,14 @@ public bool Supports(GirModel.AnyType type)

public string GetString(GirModel.ReturnType returnType, string fromVariableName)
{
if (returnType.AnyType.AsT1.Length == null)
var arrayType = returnType.AnyType.AsT1;
if (arrayType.IsZeroTerminated)
return NullTerminatedArray(returnType, fromVariableName);
else

if (arrayType.Length is not null)
return SizeBasedArray(returnType, fromVariableName);

throw new Exception("Unknown kind of array");
}

private static string NullTerminatedArray(GirModel.ReturnType returnType, string fromVariableName)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Generator.Renderer.Public.ReturnTypeToManagedExpressions;
using System;

namespace Generator.Renderer.Public.ReturnTypeToManagedExpressions;

internal class Utf8StringArray : ReturnTypeConverter
{
Expand All @@ -7,10 +9,14 @@ public bool Supports(GirModel.AnyType type)

public string GetString(GirModel.ReturnType returnType, string fromVariableName)
{
if (returnType.AnyType.AsT1.Length == null)
var arrayType = returnType.AnyType.AsT1;
if (arrayType.IsZeroTerminated)
return NullTerminatedArray(returnType, fromVariableName);
else

if (arrayType.Length is not null)
return SizeBasedArray(returnType, fromVariableName);

throw new Exception("Unknown kind of array");
}

private static string NullTerminatedArray(GirModel.ReturnType returnType, string fromVariableName)
Expand Down
2 changes: 1 addition & 1 deletion src/Generation/GirLoader/Input/ArrayType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ArrayType : AnyType
public string? Length { get; set; }

[XmlAttribute("zero-terminated")]
public string? ZeroTerminated { get; set; }
public bool ZeroTerminated { get; set; }

[XmlAttribute("fixed-size")]
public string? FixedSize { get; set; }
Expand Down
9 changes: 3 additions & 6 deletions src/Generation/GirLoader/Output/TypeReferenceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ private bool TryCreateArrayTypeReference(Input.AnyType anyType, [NotNullWhen(tru

int? length = int.TryParse(anyType.Array.Length, out var l) ? l : null;
int? fixedSize = int.TryParse(anyType.Array.FixedSize, out var f) ? f : null;
bool? zeroTerminated = bool.TryParse(anyType.Array.ZeroTerminated, out var b) ? b : null;

var reference = new ArrayTypeReference(
typeReference: typeReference,
Expand All @@ -61,11 +60,9 @@ private bool TryCreateArrayTypeReference(Input.AnyType anyType, [NotNullWhen(tru
{
Length = length,
FixedSize = fixedSize,
// TODO: The zero-terminated attribute is not consistently present in the gir file.
// If there isn't a length and fixed size treat as zero-terminated anyways. The GLib
// array types are never zero-terminated and always reset the flag.
// This can be removed if the C gir generator changes this behavior.
IsZeroTerminated = zeroTerminated ?? (length is null && fixedSize is null)
//The fallback is required as gobject-introspection expects an array to be zero terminated,
//if neither length nor fixedSize are given.
IsZeroTerminated = anyType.Array.ZeroTerminated || (length is null && fixedSize is null)
};

arrayTypeReference = anyType.Array.Name switch
Expand Down
Loading