-
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
16 changed files
with
336 additions
and
9 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/Generation/Generator/Fixer/Record/PublicMethodsColldingWithPropertiesFixer.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,31 @@ | ||
using Generator.Model; | ||
|
||
namespace Generator.Fixer.Record; | ||
|
||
internal class PublicMethodsColldingWithFieldFixer : Fixer<GirModel.Record> | ||
{ | ||
public void Fixup(GirModel.Record record) | ||
{ | ||
foreach (var field in record.Fields) | ||
{ | ||
foreach (var method in record.Methods) | ||
{ | ||
if (Field.GetName(field) == Method.GetPublicName(method)) | ||
{ | ||
if (method.ReturnType.AnyType.Is<GirModel.Void>()) | ||
{ | ||
Log.Warning($"{record.Namespace.Name}.{record.Name}: Method {Method.GetPublicName(method)} is named like a field but returns no value. It makes no sense to prefix the method with 'Get'. Thus it will be ignored."); | ||
Method.Disable(method); | ||
} | ||
else | ||
{ | ||
var newName = $"Get{Method.GetPublicName(method)}"; | ||
Log.Warning($"{record.Namespace.Name}.{record.Name}: Method {Method.GetPublicName(method)} collides with field {Field.GetName(field)} and is renamed to {newName}."); | ||
|
||
Method.SetPublicName(method, newName); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
src/Generation/Generator/Renderer/Public/Field/Converter/Bitfield.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,39 @@ | ||
namespace Generator.Renderer.Public.Field; | ||
|
||
internal class Bitfield : FieldConverter | ||
{ | ||
public bool Supports(GirModel.Field field) | ||
{ | ||
return field.AnyTypeOrCallback.TryPickT0(out var anyType, out _) && anyType.Is<GirModel.Bitfield>(); | ||
} | ||
|
||
public RenderableField Convert(GirModel.Field field) | ||
{ | ||
var name = Model.Field.GetName(field); | ||
return new RenderableField( | ||
Name: name, | ||
NullableTypeName: GetNullableTypeName(field), | ||
SetExpression: SetExpression, | ||
GetExpression: GetExpression | ||
); | ||
} | ||
|
||
private static string GetNullableTypeName(GirModel.Field field) | ||
{ | ||
var type = (GirModel.Bitfield) field.AnyTypeOrCallback.AsT0.AsT0; | ||
return field.IsPointer | ||
? Model.Type.Pointer | ||
: Model.ComplexType.GetFullyQualified(type); | ||
} | ||
|
||
private static string SetExpression(GirModel.Record record, GirModel.Field field) | ||
{ | ||
return $"Handle.Set{Model.Field.GetName(field)}(value)"; | ||
} | ||
|
||
private static string GetExpression(GirModel.Record record, GirModel.Field field) | ||
{ | ||
return $"Handle.Get{Model.Field.GetName(field)}()"; | ||
} | ||
} | ||
|
37 changes: 37 additions & 0 deletions
37
src/Generation/Generator/Renderer/Public/Field/Converter/Enumeration.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,37 @@ | ||
namespace Generator.Renderer.Public.Field; | ||
|
||
internal class Enumeration : FieldConverter | ||
{ | ||
public bool Supports(GirModel.Field field) | ||
{ | ||
return field.AnyTypeOrCallback.TryPickT0(out var anyType, out _) && anyType.Is<GirModel.Enumeration>(); | ||
} | ||
|
||
public RenderableField Convert(GirModel.Field field) | ||
{ | ||
return new RenderableField( | ||
Name: Model.Field.GetName(field), | ||
NullableTypeName: GetNullableTypeName(field), | ||
SetExpression: SetExpression, | ||
GetExpression: GetExpression | ||
); | ||
} | ||
|
||
private static string GetNullableTypeName(GirModel.Field field) | ||
{ | ||
var type = (GirModel.Enumeration) field.AnyTypeOrCallback.AsT0.AsT0; | ||
return field.IsPointer | ||
? Model.Type.Pointer | ||
: Model.ComplexType.GetFullyQualified(type); | ||
} | ||
|
||
private static string SetExpression(GirModel.Record record, GirModel.Field field) | ||
{ | ||
return $"Handle.Set{Model.Field.GetName(field)}(value)"; | ||
} | ||
|
||
private static string GetExpression(GirModel.Record record, GirModel.Field field) | ||
{ | ||
return $"Handle.Get{Model.Field.GetName(field)}()"; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Generation/Generator/Renderer/Public/Field/Converter/PrimitiveValueType.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 @@ | ||
namespace Generator.Renderer.Public.Field; | ||
|
||
internal class PrimitiveValueType : FieldConverter | ||
{ | ||
public bool Supports(GirModel.Field field) | ||
{ | ||
return field.AnyTypeOrCallback.TryPickT0(out var anyType, out _) && anyType.Is<GirModel.PrimitiveValueType>(); | ||
} | ||
|
||
public RenderableField Convert(GirModel.Field field) | ||
{ | ||
return new RenderableField( | ||
Name: Model.Field.GetName(field), | ||
NullableTypeName: GetNullableTypeName(field), | ||
SetExpression: SetExpression, | ||
GetExpression: GetExpression | ||
); | ||
} | ||
|
||
private static string GetNullableTypeName(GirModel.Field field) | ||
{ | ||
return field.IsPointer | ||
? Model.Type.Pointer | ||
: Model.Type.GetName(field.AnyTypeOrCallback.AsT0.AsT0); | ||
} | ||
|
||
private static string SetExpression(GirModel.Record record, GirModel.Field field) | ||
{ | ||
return $"Handle.Set{Model.Field.GetName(field)}(value)"; | ||
} | ||
|
||
private static string GetExpression(GirModel.Record record, GirModel.Field field) | ||
{ | ||
return $"Handle.Get{Model.Field.GetName(field)}()"; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Generation/Generator/Renderer/Public/Field/Converter/String.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 @@ | ||
using Generator.Model; | ||
|
||
namespace Generator.Renderer.Public.Field; | ||
|
||
internal class String : FieldConverter | ||
{ | ||
public bool Supports(GirModel.Field field) | ||
{ | ||
return field.AnyTypeOrCallback.TryPickT0(out var anyType, out _) && anyType.Is<GirModel.String>(); | ||
} | ||
|
||
public RenderableField Convert(GirModel.Field field) | ||
{ | ||
//Struct fields are always nullable as there are no information on nullability | ||
|
||
return new RenderableField( | ||
Name: Model.Field.GetName(field), | ||
NullableTypeName: $"{Type.GetName(field.AnyTypeOrCallback.AsT0.AsT0)}?", | ||
SetExpression: SetExpression, | ||
GetExpression: GetExpression | ||
); | ||
} | ||
|
||
private static string SetExpression(GirModel.Record record, GirModel.Field field) | ||
{ | ||
return $"Handle.Set{Model.Field.GetName(field)}(GLib.Internal.StringHelper.StringToPtrUtf8(value))"; | ||
} | ||
|
||
private static string GetExpression(GirModel.Record record, GirModel.Field field) | ||
{ | ||
return $"Marshal.PtrToStringUTF8(Handle.Get{Model.Field.GetName(field)}())"; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Generation/Generator/Renderer/Public/Field/FieldConverter.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,7 @@ | ||
namespace Generator.Renderer.Public.Field; | ||
|
||
public interface FieldConverter | ||
{ | ||
bool Supports(GirModel.Field field); | ||
RenderableField Convert(GirModel.Field field); | ||
} |
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,23 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Generator.Renderer.Public; | ||
|
||
internal static class Fields | ||
{ | ||
private static readonly List<Field.FieldConverter> Converters = new() | ||
{ | ||
new Field.Bitfield(), | ||
new Field.Enumeration(), | ||
new Field.PrimitiveValueType(), | ||
new Field.String(), | ||
}; | ||
|
||
public static Field.RenderableField GetRenderableField(GirModel.Field field) | ||
{ | ||
foreach (var converter in Converters) | ||
if (converter.Supports(field)) | ||
return converter.Convert(field); | ||
|
||
throw new System.Exception($"Internal field \"{field.Name}\" of type {field.AnyTypeOrCallback} can not be rendered"); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Generation/Generator/Renderer/Public/Field/RenderableField.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,7 @@ | ||
using System; | ||
|
||
namespace Generator.Renderer.Public.Field; | ||
|
||
public delegate string Expression(GirModel.Record record, GirModel.Field field); | ||
|
||
public record RenderableField(string Name, string NullableTypeName, Expression SetExpression, Expression GetExpression); |
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
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.