-
Notifications
You must be signed in to change notification settings - Fork 30
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,132 additions
and
8 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/Generation/Generator/Generator/Internal/UntypedRecord.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 UntypedRecord : Generator<GirModel.Record> | ||
{ | ||
private readonly Publisher _publisher; | ||
|
||
public UntypedRecord(Publisher publisher) | ||
{ | ||
_publisher = publisher; | ||
} | ||
|
||
public void Generate(GirModel.Record obj) | ||
{ | ||
if (!Record.IsUntyped(obj)) | ||
return; | ||
|
||
var source = Renderer.Internal.UntypedRecord.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/UntypedRecordData.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 UntypedRecordData : Generator<GirModel.Record> | ||
{ | ||
private readonly Publisher _publisher; | ||
|
||
public UntypedRecordData(Publisher publisher) | ||
{ | ||
_publisher = publisher; | ||
} | ||
|
||
public void Generate(GirModel.Record obj) | ||
{ | ||
if (!Record.IsUntyped(obj)) | ||
return; | ||
|
||
var source = Renderer.Internal.UntypedRecordData.Render(obj); | ||
var codeUnit = new CodeUnit( | ||
Project: Namespace.GetCanonicalName(obj.Namespace), | ||
Name: Model.UntypedRecord.GetDataName(obj), | ||
Source: source, | ||
IsInternal: true | ||
); | ||
|
||
_publisher.Publish(codeUnit); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Generation/Generator/Generator/Internal/UntypedRecordHandle.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 UntypedRecordHandle : Generator<GirModel.Record> | ||
{ | ||
private readonly Publisher _publisher; | ||
|
||
public UntypedRecordHandle(Publisher publisher) | ||
{ | ||
_publisher = publisher; | ||
} | ||
|
||
public void Generate(GirModel.Record obj) | ||
{ | ||
if (!Record.IsUntyped(obj)) | ||
return; | ||
|
||
var source = Renderer.Internal.UntypedRecordHandle.Render(obj); | ||
var codeUnit = new CodeUnit( | ||
Project: Namespace.GetCanonicalName(obj.Namespace), | ||
Name: Model.UntypedRecord.GetInternalHandle(obj), | ||
Source: source, | ||
IsInternal: true | ||
); | ||
|
||
_publisher.Publish(codeUnit); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Generation/Generator/Generator/Public/UntypedRecord.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.Public; | ||
|
||
internal class UntypedRecord : Generator<GirModel.Record> | ||
{ | ||
private readonly Publisher _publisher; | ||
|
||
public UntypedRecord(Publisher publisher) | ||
{ | ||
_publisher = publisher; | ||
} | ||
|
||
public void Generate(GirModel.Record record) | ||
{ | ||
if (!Record.IsUntyped(record)) | ||
return; | ||
|
||
var source = Renderer.Public.UntypedRecord.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,86 @@ | ||
using System.Linq; | ||
|
||
namespace Generator.Model; | ||
|
||
internal static class UntypedRecord | ||
{ | ||
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 GetInternalManagedHandle(GirModel.Record record) | ||
=> $"{Type.GetName(record)}ManagedHandle"; | ||
|
||
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 GetFullyQuallifiedHandle(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"; | ||
|
||
public static string GetFullyQuallifiedDataName(GirModel.Record record) | ||
=> $"{Namespace.GetInternalName(record.Namespace)}.{GetDataName(record)}"; | ||
|
||
public static string GetFullyQuallifiedManagedHandle(GirModel.Record record) | ||
=> $"{Namespace.GetInternalName(record.Namespace)}.{GetInternalManagedHandle(record)}"; | ||
|
||
public static string GetInternalArrayHandle(GirModel.Record record) | ||
{ | ||
var prefix = $"{Type.GetName(record)}Array"; | ||
if (record.Namespace.Records.Select(x => x.Name).Contains(prefix)) | ||
prefix += "2"; | ||
|
||
return $"{prefix}Handle"; | ||
} | ||
|
||
public static string GetFullyQuallifiedArrayHandle(GirModel.Record record) | ||
=> $"{Namespace.GetInternalName(record.Namespace)}.{GetInternalArrayHandle(record)}"; | ||
|
||
public static string GetInternalArrayOwnedHandle(GirModel.Record record) | ||
{ | ||
var prefix = $"{Type.GetName(record)}Array"; | ||
if (record.Namespace.Records.Select(x => x.Name).Contains(prefix)) | ||
prefix += "2"; | ||
|
||
return $"{prefix}OwnedHandle"; | ||
} | ||
|
||
public static string GetFullyQuallifiedArrayOwnedHandle(GirModel.Record record) | ||
=> $"{Namespace.GetInternalName(record.Namespace)}.{GetInternalArrayOwnedHandle(record)}"; | ||
|
||
public static string GetInternalArrayUnownedHandle(GirModel.Record record) | ||
{ | ||
var prefix = $"{Type.GetName(record)}Array"; | ||
if (record.Namespace.Records.Select(x => x.Name).Contains(prefix)) | ||
prefix += "2"; | ||
return $"{prefix}UnownedHandle"; | ||
} | ||
|
||
public static string GetFullyQuallifiedArrayUnownedHandle(GirModel.Record record) | ||
=> $"{Namespace.GetInternalName(record.Namespace)}.{GetInternalArrayUnownedHandle(record)}"; | ||
|
||
public static string GetFullyQuallifiedArrayNullHandle(GirModel.Record record) | ||
=> $"{Namespace.GetInternalName(record.Namespace)}.{GetInternalArrayUnownedHandle(record)}.NullHandle"; | ||
} |
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
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
28 changes: 28 additions & 0 deletions
28
src/Generation/Generator/Renderer/Internal/Field/Converter/UntypedRecord.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,28 @@ | ||
using Generator.Model; | ||
|
||
namespace Generator.Renderer.Internal.Field; | ||
|
||
internal class UntypedRecord : FieldConverter | ||
{ | ||
public bool Supports(GirModel.Field field) | ||
{ | ||
return field.AnyTypeOrCallback.TryPickT0(out var anyType, out _) && anyType.Is<GirModel.Record>(out var record) && Model.Record.IsUntyped(record); | ||
} | ||
|
||
public RenderableField Convert(GirModel.Field field) | ||
{ | ||
return new RenderableField( | ||
Name: Model.Field.GetName(field), | ||
Attribute: null, | ||
NullableTypeName: GetNullableTypeName(field) | ||
); | ||
} | ||
|
||
private static string GetNullableTypeName(GirModel.Field field) | ||
{ | ||
var type = (GirModel.Record) field.AnyTypeOrCallback.AsT0.AsT0; | ||
return field.IsPointer | ||
? Type.Pointer | ||
: Model.Record.GetFullyQualifiedInternalStructName(type); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Generation/Generator/Renderer/Internal/Field/Converter/UntypedRecordArray.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,33 @@ | ||
namespace Generator.Renderer.Internal.Field; | ||
|
||
internal class UntypedRecordArray : FieldConverter | ||
{ | ||
public bool Supports(GirModel.Field field) | ||
{ | ||
return field.AnyTypeOrCallback.TryPickT0(out var anyType, out _) && anyType.IsArray<GirModel.Record>(out var record) && Model.Record.IsUntyped(record); ; | ||
} | ||
|
||
public RenderableField Convert(GirModel.Field field) | ||
{ | ||
return new RenderableField( | ||
Name: Model.Field.GetName(field), | ||
Attribute: GetAttribute(field), | ||
NullableTypeName: GetNullableTypeName(field) | ||
); | ||
} | ||
|
||
private static string? GetAttribute(GirModel.Field field) | ||
{ | ||
var arrayType = field.AnyTypeOrCallback.AsT0.AsT1; | ||
return arrayType.FixedSize is not null | ||
? MarshalAs.UnmanagedByValArray(sizeConst: arrayType.FixedSize.Value) | ||
: null; | ||
} | ||
|
||
private static string GetNullableTypeName(GirModel.Field field) | ||
{ | ||
var arrayType = field.AnyTypeOrCallback.AsT0.AsT1; | ||
var type = (GirModel.Record) arrayType.AnyType.AsT0; | ||
return Model.UntypedRecord.GetFullyQuallifiedDataName(type) + "[]"; | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
src/Generation/Generator/Renderer/Internal/InstanceParameter/Converter/UntypedRecord.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.InstanceParameter; | ||
|
||
internal class UntypedRecord : InstanceParameterConverter | ||
{ | ||
public bool Supports(GirModel.Type type) | ||
{ | ||
return type is GirModel.Record r && Model.Record.IsUntyped(r); | ||
} | ||
|
||
public RenderableInstanceParameter Convert(GirModel.InstanceParameter instanceParameter) | ||
{ | ||
return new RenderableInstanceParameter( | ||
Name: Model.InstanceParameter.GetName(instanceParameter), | ||
NullableTypeName: GetNullableTypeName(instanceParameter) | ||
); | ||
} | ||
|
||
private static string GetNullableTypeName(GirModel.InstanceParameter instanceParameter) | ||
{ | ||
var type = (GirModel.Record) instanceParameter.Type; | ||
return instanceParameter switch | ||
{ | ||
{ Direction: GirModel.Direction.In, Transfer: GirModel.Transfer.None } => Model.UntypedRecord.GetFullyQuallifiedHandle(type), | ||
{ Direction: GirModel.Direction.In, Transfer: GirModel.Transfer.Full } => throw new Exception("Can't transfer ownership of untyped record"), | ||
_ => throw new System.Exception($"Can't detect typed record instance parameter type {instanceParameter.Name}: CallerAllocates={instanceParameter.CallerAllocates} Direction={instanceParameter.Direction} Transfer={instanceParameter.Transfer}") | ||
}; | ||
} | ||
} |
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
Oops, something went wrong.