-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
44 changed files
with
2,048 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Generator.Model; | ||
|
||
internal static class PlatformStringArray | ||
{ | ||
public static string GetInternalOwnedHandleName() => "GLib.Internal.PlatformStringArrayNullTerminatedOwnedHandle"; | ||
public static string GetInternalUnownedHandleName() => "GLib.Internal.PlatformStringArrayNullTerminatedUnownedHandle"; | ||
public static string GetInternalContainerHandleName() => "GLib.Internal.PlatformStringArrayNullTerminatedContainerHandle"; | ||
public static string GetInternalHandleName() => "GLib.Internal.PlatformStringArrayNullTerminatedHandle"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Generator.Model; | ||
|
||
internal static class Utf8StringArray | ||
{ | ||
public static string GetInternalOwnedHandleName() => "GLib.Internal.Utf8StringArrayNullTerminatedOwnedHandle"; | ||
public static string GetInternalUnownedHandleName() => "GLib.Internal.Utf8StringArrayNullTerminatedUnownedHandle"; | ||
public static string GetInternalContainerHandleName() => "GLib.Internal.Utf8StringArrayNullTerminatedContainerHandle"; | ||
public static string GetInternalHandleName() => "GLib.Internal.Utf8StringArrayNullTerminatedHandle"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/Generation/Generator/Renderer/Internal/Parameter/Converter/PlatformStringArray.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
|
||
namespace Generator.Renderer.Internal.Parameter; | ||
|
||
internal class PlatformStringArray : ParameterConverter | ||
{ | ||
public bool Supports(GirModel.AnyType anyType) | ||
{ | ||
return anyType.IsArray<GirModel.PlatformString>(); | ||
} | ||
|
||
public RenderableParameter Convert(GirModel.Parameter parameter) | ||
{ | ||
return parameter.AnyTypeOrVarArgs.AsT0.AsT1.Length is null | ||
? ParameterWithoutLength(parameter) | ||
: ParameterWithLength(parameter); | ||
} | ||
|
||
private static RenderableParameter ParameterWithLength(GirModel.Parameter parameter) | ||
{ | ||
var length = parameter.AnyTypeOrVarArgs.AsT0.AsT1.Length ?? throw new Exception("Length must not be null"); | ||
|
||
return new RenderableParameter( | ||
Attribute: MarshalAs.UnmanagedLpArray(sizeParamIndex: length), | ||
Direction: string.Empty, | ||
NullableTypeName: Model.ArrayType.GetName(parameter.AnyTypeOrVarArgs.AsT0.AsT1), | ||
Name: Model.Parameter.GetName(parameter) | ||
); | ||
} | ||
|
||
private static RenderableParameter ParameterWithoutLength(GirModel.Parameter parameter) | ||
{ | ||
return parameter.Direction switch | ||
{ | ||
GirModel.Direction.In => ParameterWithoutLengthIn(parameter), | ||
GirModel.Direction.Out => ParameterWithoutLengthOut(parameter), | ||
_ => throw new Exception("Unknown direction for parameter") | ||
}; | ||
} | ||
|
||
private static RenderableParameter ParameterWithoutLengthIn(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"); | ||
|
||
var nullableTypeName = parameter switch | ||
{ | ||
{ Transfer: GirModel.Transfer.None } => Model.PlatformStringArray.GetInternalHandleName(), | ||
{ Transfer: GirModel.Transfer.Full } => Model.PlatformStringArray.GetInternalUnownedHandleName(), | ||
{ Transfer: GirModel.Transfer.Container } => throw new Exception("Transfer container not supported for platform string arrays"), | ||
_ => throw new Exception("Can't detect typename for platform string array") | ||
}; | ||
|
||
return new RenderableParameter( | ||
Attribute: string.Empty, | ||
Direction: ParameterDirection.In(), | ||
NullableTypeName: nullableTypeName, | ||
Name: Model.Parameter.GetName(parameter) | ||
); | ||
} | ||
|
||
private static RenderableParameter ParameterWithoutLengthOut(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"); | ||
|
||
var nullableTypeName = parameter switch | ||
{ | ||
{ Transfer: GirModel.Transfer.None } => Model.PlatformStringArray.GetInternalUnownedHandleName(), | ||
{ Transfer: GirModel.Transfer.Full } => Model.PlatformStringArray.GetInternalOwnedHandleName(), | ||
{ Transfer: GirModel.Transfer.Container } => Model.PlatformStringArray.GetInternalContainerHandleName(), | ||
_ => throw new Exception("Can't detect typename for platform string array") | ||
}; | ||
|
||
return new RenderableParameter( | ||
Attribute: string.Empty, | ||
Direction: ParameterDirection.Out(), | ||
NullableTypeName: nullableTypeName, | ||
Name: Model.Parameter.GetName(parameter) | ||
); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...Generation/Generator/Renderer/Internal/Parameter/Converter/PlatformStringArrayCallback.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
namespace Generator.Renderer.Internal.Parameter; | ||
|
||
internal class PlatformStringArrayCallback : ParameterConverter | ||
{ | ||
public bool Supports(GirModel.AnyType anyType) | ||
{ | ||
return anyType.IsArray<GirModel.PlatformString>(); | ||
} | ||
|
||
public RenderableParameter Convert(GirModel.Parameter parameter) | ||
{ | ||
return new RenderableParameter( | ||
Attribute: GetAttribute(parameter), | ||
Direction: string.Empty, | ||
NullableTypeName: GetNullableTypeName(parameter), | ||
Name: Model.Parameter.GetName(parameter) | ||
); | ||
} | ||
|
||
private static string GetNullableTypeName(GirModel.Parameter parameter) | ||
{ | ||
return parameter switch | ||
{ | ||
// Arrays of string which do not transfer ownership and have no length index can not be marshalled automatically | ||
{ Transfer: GirModel.Transfer.None, AnyTypeOrVarArgs.AsT0.AsT1.Length: null } => Model.Type.Pointer, | ||
_ => Model.ArrayType.GetName(parameter.AnyTypeOrVarArgs.AsT0.AsT1) | ||
}; | ||
} | ||
|
||
private static string GetAttribute(GirModel.Parameter parameter) | ||
{ | ||
return parameter.AnyTypeOrVarArgs.AsT0.AsT1.Length switch | ||
{ | ||
null => string.Empty, | ||
{ } l => MarshalAs.UnmanagedLpArray(sizeParamIndex: l) | ||
}; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/Generation/Generator/Renderer/Internal/Parameter/Converter/Utf8StringArray.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
|
||
namespace Generator.Renderer.Internal.Parameter; | ||
|
||
internal class Utf8StringArray : ParameterConverter | ||
{ | ||
public bool Supports(GirModel.AnyType anyType) | ||
{ | ||
return anyType.IsArray<GirModel.Utf8String>(); | ||
} | ||
|
||
public RenderableParameter Convert(GirModel.Parameter parameter) | ||
{ | ||
return parameter.AnyTypeOrVarArgs.AsT0.AsT1.Length is null | ||
? ParameterWithoutLength(parameter) | ||
: ParameterWithLength(parameter); | ||
} | ||
|
||
private static RenderableParameter ParameterWithLength(GirModel.Parameter parameter) | ||
{ | ||
var length = parameter.AnyTypeOrVarArgs.AsT0.AsT1.Length ?? throw new Exception("Length must not be null"); | ||
|
||
return new RenderableParameter( | ||
Attribute: MarshalAs.UnmanagedLpArray(sizeParamIndex: length), | ||
Direction: string.Empty, | ||
NullableTypeName: Model.ArrayType.GetName(parameter.AnyTypeOrVarArgs.AsT0.AsT1), | ||
Name: Model.Parameter.GetName(parameter) | ||
); | ||
} | ||
|
||
private static RenderableParameter ParameterWithoutLength(GirModel.Parameter parameter) | ||
{ | ||
return parameter.Direction switch | ||
{ | ||
GirModel.Direction.In => ParameterWithoutLengthIn(parameter), | ||
GirModel.Direction.Out => ParameterWithoutLengthOut(parameter), | ||
_ => throw new Exception("Unknown direction for parameter") | ||
}; | ||
} | ||
|
||
private static RenderableParameter ParameterWithoutLengthIn(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"); | ||
|
||
var nullableTypeName = parameter switch | ||
{ | ||
{ Transfer: GirModel.Transfer.None } => Model.Utf8StringArray.GetInternalHandleName(), | ||
{ Transfer: GirModel.Transfer.Full } => Model.Utf8StringArray.GetInternalUnownedHandleName(), | ||
{ Transfer: GirModel.Transfer.Container } => throw new Exception("Transfer container not supported for utf8 string arrays"), | ||
_ => throw new Exception("Can't detect typename for utf8 string array") | ||
}; | ||
|
||
return new RenderableParameter( | ||
Attribute: string.Empty, | ||
Direction: ParameterDirection.In(), | ||
NullableTypeName: nullableTypeName, | ||
Name: Model.Parameter.GetName(parameter) | ||
); | ||
} | ||
|
||
private static RenderableParameter ParameterWithoutLengthOut(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"); | ||
|
||
var nullableTypeName = parameter switch | ||
{ | ||
{ Transfer: GirModel.Transfer.None } => Model.Utf8StringArray.GetInternalUnownedHandleName(), | ||
{ Transfer: GirModel.Transfer.Full } => Model.Utf8StringArray.GetInternalOwnedHandleName(), | ||
{ Transfer: GirModel.Transfer.Container } => Model.Utf8StringArray.GetInternalContainerHandleName(), | ||
_ => throw new Exception("Can't detect typename for utf8 string array") | ||
}; | ||
|
||
return new RenderableParameter( | ||
Attribute: string.Empty, | ||
Direction: ParameterDirection.Out(), | ||
NullableTypeName: nullableTypeName, | ||
Name: Model.Parameter.GetName(parameter) | ||
); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...ternal/Parameter/Converter/StringArray.cs → ...eter/Converter/Utf8StringArrayCallback.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...Generator/Renderer/Internal/ParameterToManagedExpression/Converter/PlatformStringArray.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Generator.Renderer.Internal.ParameterToManagedExpressions; | ||
|
||
internal class PlatformStringArray : ToManagedParameterConverter | ||
{ | ||
public bool Supports(GirModel.AnyType type) | ||
=> type.IsArray<GirModel.PlatformString>(); | ||
|
||
public void Initialize(ParameterToManagedData parameterData, IEnumerable<ParameterToManagedData> parameters) | ||
{ | ||
var arrayType = parameterData.Parameter.AnyTypeOrVarArgs.AsT0.AsT1; | ||
if (arrayType.Length is null) | ||
NullTerminatedArray(parameterData); | ||
else | ||
SizeBasedArray(parameterData); | ||
} | ||
|
||
private static void NullTerminatedArray(ParameterToManagedData parameterData) | ||
{ | ||
var signatureName = Model.Parameter.GetName(parameterData.Parameter); | ||
var callName = Model.Parameter.GetConvertedName(parameterData.Parameter); | ||
|
||
var handleExpression = parameterData.Parameter switch | ||
{ | ||
{ Transfer: GirModel.Transfer.None } => $"new {Model.PlatformStringArray.GetInternalUnownedHandleName()}({signatureName}).ConvertToStringArray()", | ||
{ Transfer: GirModel.Transfer.Full } => $"new {Model.PlatformStringArray.GetInternalOwnedHandleName()}({signatureName}).ConvertToStringArray()", | ||
_ => throw new Exception("Unknown transfer type for platform string array to managed expression") | ||
}; | ||
|
||
parameterData.SetSignatureName(signatureName); | ||
parameterData.SetExpression($"var {callName} = {handleExpression};"); | ||
parameterData.SetCallName(callName); | ||
} | ||
|
||
private static void SizeBasedArray(ParameterToManagedData parameterData) | ||
{ | ||
var variableName = Model.Parameter.GetName(parameterData.Parameter); | ||
|
||
parameterData.SetSignatureName(variableName); | ||
parameterData.SetCallName(variableName); | ||
} | ||
} |
30 changes: 0 additions & 30 deletions
30
...eration/Generator/Renderer/Internal/ParameterToManagedExpression/Converter/StringArray.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.