-
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
40 changed files
with
1,238 additions
and
5 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/Generation/Generator/Generator/Internal/TypedRecord.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,29 @@ | ||
using Generator.Model; | ||
|
||
namespace Generator.Generator.Internal; | ||
|
||
internal class TypedRecord : Generator<GirModel.Record> | ||
{ | ||
private readonly Publisher _publisher; | ||
|
||
public TypedRecord(Publisher publisher) | ||
{ | ||
_publisher = publisher; | ||
} | ||
|
||
public void Generate(GirModel.Record obj) | ||
{ | ||
if (!Record.IsTyped(obj)) | ||
return; | ||
|
||
var source = Renderer.Internal.TypedRecord.Render(obj); | ||
var codeUnit = new CodeUnit( | ||
Project: Namespace.GetCanonicalName(obj.Namespace), | ||
Name: obj.Name, | ||
Source: source, | ||
IsInternal: true | ||
); | ||
|
||
_publisher.Publish(codeUnit); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Generation/Generator/Generator/Internal/TypedRecordData.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,29 @@ | ||
using Generator.Model; | ||
|
||
namespace Generator.Generator.Internal; | ||
|
||
internal class TypedRecordData : Generator<GirModel.Record> | ||
{ | ||
private readonly Publisher _publisher; | ||
|
||
public TypedRecordData(Publisher publisher) | ||
{ | ||
_publisher = publisher; | ||
} | ||
|
||
public void Generate(GirModel.Record obj) | ||
{ | ||
if (!Record.IsTyped(obj)) | ||
return; | ||
|
||
var source = Renderer.Internal.TypedRecordData.Render(obj); | ||
var codeUnit = new CodeUnit( | ||
Project: Namespace.GetCanonicalName(obj.Namespace), | ||
Name: Model.TypedRecord.GetDataName(obj), | ||
Source: source, | ||
IsInternal: true | ||
); | ||
|
||
_publisher.Publish(codeUnit); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Generation/Generator/Generator/Internal/TypedRecordHandle.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,29 @@ | ||
using Generator.Model; | ||
|
||
namespace Generator.Generator.Internal; | ||
|
||
internal class TypedRecordHandle : Generator<GirModel.Record> | ||
{ | ||
private readonly Publisher _publisher; | ||
|
||
public TypedRecordHandle(Publisher publisher) | ||
{ | ||
_publisher = publisher; | ||
} | ||
|
||
public void Generate(GirModel.Record obj) | ||
{ | ||
if (!Record.IsTyped(obj)) | ||
return; | ||
|
||
var source = Renderer.Internal.TypedRecordHandle.Render(obj); | ||
var codeUnit = new CodeUnit( | ||
Project: Namespace.GetCanonicalName(obj.Namespace), | ||
Name: Model.TypedRecord.GetInternalHandle(obj), | ||
Source: source, | ||
IsInternal: true | ||
); | ||
|
||
_publisher.Publish(codeUnit); | ||
} | ||
} |
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,29 @@ | ||
using Generator.Model; | ||
|
||
namespace Generator.Generator.Public; | ||
|
||
internal class TypedRecord : Generator<GirModel.Record> | ||
{ | ||
private readonly Publisher _publisher; | ||
|
||
public TypedRecord(Publisher publisher) | ||
{ | ||
_publisher = publisher; | ||
} | ||
|
||
public void Generate(GirModel.Record record) | ||
{ | ||
if (!Record.IsTyped(record)) | ||
return; | ||
|
||
var source = Renderer.Public.TypedRecord.Render(record); | ||
var codeUnit = new CodeUnit( | ||
Project: Namespace.GetCanonicalName(record.Namespace), | ||
Name: Record.GetPublicClassName(record), | ||
Source: source, | ||
IsInternal: false | ||
); | ||
|
||
_publisher.Publish(codeUnit); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
namespace Generator.Model; | ||
|
||
internal static class TypedRecord | ||
{ | ||
public static string GetPublicClassName(GirModel.Record record) | ||
=> record.Name; | ||
|
||
public static string GetFullyQualifiedPublicClassName(GirModel.Record record) | ||
=> Namespace.GetPublicName(record.Namespace) + "." + GetPublicClassName(record); | ||
|
||
public static string GetFullyQualifiedInternalClassName(GirModel.Record record) | ||
=> Namespace.GetInternalName(record.Namespace) + "." + record.Name; | ||
|
||
public static string GetInternalHandle(GirModel.Record record) | ||
=> $"{Type.GetName(record)}Handle"; | ||
|
||
public static string GetInternalOwnedHandle(GirModel.Record record) | ||
=> $"{Type.GetName(record)}OwnedHandle"; | ||
|
||
public static string GetInternalUnownedHandle(GirModel.Record record) | ||
=> $"{Type.GetName(record)}UnownedHandle"; | ||
|
||
public static string GetFullyQuallifiedInternalHandle(GirModel.Record record) | ||
=> $"{Namespace.GetInternalName(record.Namespace)}.{GetInternalHandle(record)}"; | ||
|
||
public static string GetFullyQuallifiedOwnedHandle(GirModel.Record record) | ||
=> $"{Namespace.GetInternalName(record.Namespace)}.{GetInternalOwnedHandle(record)}"; | ||
|
||
public static string GetFullyQuallifiedUnownedHandle(GirModel.Record record) | ||
=> $"{Namespace.GetInternalName(record.Namespace)}.{GetInternalUnownedHandle(record)}"; | ||
|
||
public static string GetFullyQuallifiedNullHandle(GirModel.Record record) | ||
=> $"{Namespace.GetInternalName(record.Namespace)}.{GetInternalUnownedHandle(record)}.NullHandle"; | ||
|
||
public static string GetDataName(GirModel.Record record) | ||
=> $"{Type.GetName(record)}Data"; | ||
} |
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
41 changes: 41 additions & 0 deletions
41
src/Generation/Generator/Renderer/Internal/Parameter/Converter/TypedRecord.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,41 @@ | ||
using System; | ||
|
||
namespace Generator.Renderer.Internal.Parameter; | ||
|
||
internal class TypedRecord : ParameterConverter | ||
{ | ||
public bool Supports(GirModel.AnyType anyType) | ||
{ | ||
return anyType.Is<GirModel.Record>(out var record) && Model.Record.IsTyped(record); | ||
} | ||
|
||
public RenderableParameter Convert(GirModel.Parameter parameter) | ||
{ | ||
return new RenderableParameter( | ||
Attribute: string.Empty, | ||
Direction: GetDirection(parameter), | ||
NullableTypeName: GetNullableTypeName(parameter), | ||
Name: Model.Parameter.GetName(parameter) | ||
); | ||
} | ||
|
||
private static string GetNullableTypeName(GirModel.Parameter parameter) | ||
{ | ||
//Native records are represented as SafeHandles and are not nullable | ||
|
||
var type = (GirModel.Record) parameter.AnyTypeOrVarArgs.AsT0.AsT0; | ||
return parameter switch | ||
{ | ||
{ Direction: GirModel.Direction.In, Transfer: GirModel.Transfer.None } => Model.TypedRecord.GetFullyQuallifiedInternalHandle(type), | ||
{ Direction: GirModel.Direction.In, Transfer: GirModel.Transfer.Full } => Model.TypedRecord.GetFullyQuallifiedUnownedHandle(type), | ||
_ => throw new Exception($"Can't detect record parameter type {parameter.Name}: CallerAllocates={parameter.CallerAllocates} Direction={parameter.Direction} Transfer={parameter.Transfer}") | ||
}; | ||
} | ||
|
||
private static string GetDirection(GirModel.Parameter parameter) => parameter switch | ||
{ | ||
{ Direction: GirModel.Direction.In } => ParameterDirection.In(), | ||
{ Direction: GirModel.Direction.InOut } => ParameterDirection.In(), | ||
_ => throw new Exception($"Unknown parameter direction for opaque typed record parameter {parameter.Name}") | ||
}; | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Generation/Generator/Renderer/Internal/Parameter/Converter/TypedRecordArray.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,27 @@ | ||
using System; | ||
|
||
namespace Generator.Renderer.Internal.Parameter; | ||
|
||
internal class TypedRecordArray : ParameterConverter | ||
{ | ||
public bool Supports(GirModel.AnyType anyType) | ||
{ | ||
return anyType.IsArray<GirModel.Record>(out var record) && Model.Record.IsTyped(record); | ||
} | ||
|
||
public RenderableParameter Convert(GirModel.Parameter parameter) | ||
{ | ||
if (!parameter.AnyTypeOrVarArgs.AsT0.AsT1.IsPointer) | ||
{ | ||
var record = (GirModel.Record) parameter.AnyTypeOrVarArgs.AsT0.AsT1.AnyType.AsT0; | ||
throw new Exception($"Unpointed record array of type {record.Name} not yet supported"); | ||
} | ||
|
||
return new RenderableParameter( | ||
Attribute: string.Empty, | ||
Direction: string.Empty, | ||
NullableTypeName: $"ref {Model.Type.Pointer}", | ||
Name: Model.Parameter.GetName(parameter) | ||
); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Generation/Generator/Renderer/Internal/Parameter/Converter/TypedRecordCallback.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,30 @@ | ||
using System; | ||
|
||
namespace Generator.Renderer.Internal.Parameter; | ||
|
||
internal class TypedRecordCallback : ParameterConverter | ||
{ | ||
public bool Supports(GirModel.AnyType anyType) | ||
{ | ||
return anyType.Is<GirModel.Record>(out var record) && Model.Record.IsTyped(record); | ||
} | ||
|
||
public RenderableParameter Convert(GirModel.Parameter parameter) | ||
{ | ||
return new RenderableParameter( | ||
Attribute: string.Empty, | ||
Direction: GetDirection(parameter), | ||
NullableTypeName: Model.Type.Pointer, | ||
Name: Model.Parameter.GetName(parameter) | ||
); | ||
} | ||
|
||
private static string GetDirection(GirModel.Parameter parameter) => parameter switch | ||
{ | ||
{ Direction: GirModel.Direction.In } => ParameterDirection.In(), | ||
{ Direction: GirModel.Direction.InOut } => ParameterDirection.In(), | ||
{ Direction: GirModel.Direction.Out, CallerAllocates: true } => ParameterDirection.In(), | ||
{ Direction: GirModel.Direction.Out } => ParameterDirection.Out(), | ||
_ => throw new Exception("Unknown direction for record parameter in callback") | ||
}; | ||
} |
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
36 changes: 36 additions & 0 deletions
36
...eration/Generator/Renderer/Internal/ParameterToManagedExpression/Converter/TypedRecord.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,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Generator.Renderer.Internal.ParameterToManagedExpressions; | ||
|
||
internal class TypedRecord : ToManagedParameterConverter | ||
{ | ||
public bool Supports(GirModel.AnyType type) | ||
=> type.Is<GirModel.Record>(out var record) && Model.Record.IsTyped(record); | ||
|
||
public void Initialize(ParameterToManagedData parameterData, IEnumerable<ParameterToManagedData> parameters) | ||
{ | ||
if (parameterData.Parameter.Direction != GirModel.Direction.In) | ||
throw new NotImplementedException($"{parameterData.Parameter.AnyTypeOrVarArgs}: typed record with direction != in not yet supported"); | ||
|
||
var record = (GirModel.Record) parameterData.Parameter.AnyTypeOrVarArgs.AsT0.AsT0; | ||
var variableName = Model.Parameter.GetConvertedName(parameterData.Parameter); | ||
|
||
var signatureName = Model.Parameter.GetName(parameterData.Parameter); | ||
|
||
var ownedHandle = parameterData.Parameter switch | ||
{ | ||
{ Transfer: GirModel.Transfer.Full } => $"new {Model.TypedRecord.GetFullyQuallifiedOwnedHandle(record)}({signatureName})", | ||
{ Transfer: GirModel.Transfer.None } => $"{Model.TypedRecord.GetFullyQuallifiedOwnedHandle(record)}.FromUnowned({signatureName})", | ||
_ => throw new Exception($"Unknown transfer type for typed record parameter {parameterData.Parameter.Name}") | ||
}; | ||
|
||
var nullable = parameterData.Parameter.Nullable | ||
? $" {signatureName} == IntPtr.Zero ? null :" | ||
: string.Empty; | ||
|
||
parameterData.SetSignatureName(signatureName); | ||
parameterData.SetExpression($"var {variableName} ={nullable} new {Model.TypedRecord.GetFullyQualifiedPublicClassName(record)}({ownedHandle});"); | ||
parameterData.SetCallName(variableName); | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
src/Generation/Generator/Renderer/Internal/ReturnType/Converter/TypedRecord.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,25 @@ | ||
using GirModel; | ||
|
||
namespace Generator.Renderer.Internal.ReturnType; | ||
|
||
internal class TypedRecord : ReturnTypeConverter | ||
{ | ||
public bool Supports(GirModel.ReturnType returnType) | ||
{ | ||
return returnType.AnyType.Is<GirModel.Record>(out var record) && Model.Record.IsTyped(record); | ||
} | ||
|
||
public RenderableReturnType Convert(GirModel.ReturnType returnType) | ||
{ | ||
var type = (GirModel.Record) returnType.AnyType.AsT0; | ||
|
||
var typeName = returnType switch | ||
{ | ||
{ Transfer: Transfer.Full } => Model.TypedRecord.GetFullyQuallifiedOwnedHandle(type), | ||
_ => Model.TypedRecord.GetFullyQuallifiedUnownedHandle(type) | ||
}; | ||
|
||
//Returned SafeHandles are never "null" but "invalid" in case of C NULL. | ||
return new RenderableReturnType(typeName); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Generation/Generator/Renderer/Internal/ReturnType/Converter/TypedRecordCallback.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,14 @@ | ||
namespace Generator.Renderer.Internal.ReturnType; | ||
|
||
internal class TypedRecordCallback : ReturnTypeConverter | ||
{ | ||
public bool Supports(GirModel.ReturnType returnType) | ||
{ | ||
return returnType.AnyType.Is<GirModel.Record>(out var record) && Model.Record.IsTyped(record); | ||
} | ||
|
||
public RenderableReturnType Convert(GirModel.ReturnType returnType) | ||
{ | ||
return new RenderableReturnType(Model.Type.Pointer); | ||
} | ||
} |
Oops, something went wrong.