-
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.
Merge pull request #391 from gircore/fixes
Prefer symbol name for primitive value types over their ctype to detect the correct type as the symbol sometimes is more expressive (e.g. symbolname = "double" ctype = "gpointer") Differentiate between Bitfields and Enumerations. Bitfields are transferred as ulong and Enumerations as long. They are represented as different types in the gir.rnc file, too. Additionally Bitfields can contain negative values (despite them being of type ulong). Those values must be filtered out as they are convenience functions of C which do not apply for us. Bitfields/Enumerations need to respect the type references namespace name to avoid false positive matches.
- Loading branch information
Showing
17 changed files
with
153 additions
and
25 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
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,13 @@ | ||
{{~ #Bitfields are passed as ulong via the ffi ~}} | ||
using System; | ||
|
||
namespace {{ namespace.name }} | ||
{ | ||
[Flags] | ||
public enum {{ name }} : ulong | ||
{ | ||
{{~ for member in members }} | ||
{{ include 'member.sbntxt' member ~}} | ||
{{ end }} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,14 +1,12 @@ | ||
{{~ #Enumerations are passed as long via the ffi ~}} | ||
using System; | ||
|
||
namespace {{ namespace.name }} | ||
{ | ||
{{~ if has_flags ~}} | ||
[Flags] | ||
{{~ end ~}} | ||
public enum {{ name }} : long | ||
{ | ||
{{~ for member in members }} | ||
{{ include 'enum.member.sbntxt' member ~}} | ||
{{ include 'member.sbntxt' member ~}} | ||
{{ end }} | ||
} | ||
} |
File renamed without changes.
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; | ||
using System.Xml.Serialization; | ||
|
||
namespace GirLoader.Input.Model | ||
{ | ||
public class Bitfield | ||
{ | ||
[XmlAttribute("name")] | ||
public string? Name { get; set; } | ||
|
||
[XmlAttribute("type", Namespace = "http://www.gtk.org/introspection/c/1.0")] | ||
public string? Type { get; set; } | ||
|
||
[XmlAttribute("type-name", Namespace = "http://www.gtk.org/introspection/glib/1.0")] | ||
public string? TypeName { get; set; } | ||
|
||
[XmlElement("doc")] | ||
public Doc? Doc { get; set; } | ||
|
||
[XmlElement("member")] | ||
public List<Member> Members { get; set; } = default!; | ||
} | ||
} |
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,38 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using GirLoader.Helper; | ||
|
||
namespace GirLoader.Output.Model | ||
{ | ||
public class Bitfield : ComplexType | ||
{ | ||
public IEnumerable<Member> Members { get; } | ||
|
||
public Bitfield(Repository repository, CType? cType, TypeName originalName, TypeName name, IEnumerable<Member> members) : base(repository, cType, name, originalName) | ||
{ | ||
Members = members; | ||
} | ||
|
||
internal override IEnumerable<TypeReference> GetTypeReferences() | ||
=> Members.SelectMany(x => x.GetTypeReferences()); | ||
|
||
internal override bool GetIsResolved() | ||
=> Members.All(x => x.GetIsResolved()); | ||
|
||
internal override bool Matches(TypeReference typeReference) | ||
{ | ||
if (typeReference.SymbolNameReference is not null) | ||
{ | ||
var namespaceOk = typeReference.SymbolNameReference.NamespaceName == Repository.Namespace.Name | ||
|| typeReference.SymbolNameReference.NamespaceName == null; | ||
|
||
return namespaceOk && typeReference.SymbolNameReference.SymbolName == OriginalName; | ||
} | ||
|
||
if (typeReference.CTypeReference is not null) | ||
return typeReference.CTypeReference.CType == CType; | ||
|
||
return false; | ||
} | ||
} | ||
} |
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,47 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace GirLoader.Output.Model | ||
{ | ||
internal class BitfieldFactory | ||
{ | ||
private readonly MemberFactory _memberFactory; | ||
|
||
public BitfieldFactory(MemberFactory memberFactory) | ||
{ | ||
_memberFactory = memberFactory; | ||
} | ||
|
||
public Bitfield Create(Input.Model.Bitfield bitfield, Repository repository) | ||
{ | ||
if (bitfield.Name is null) | ||
throw new Exception("Bitfield has no name"); | ||
|
||
if (bitfield.Type is null) | ||
throw new Exception("Bitfield is missing a type"); | ||
|
||
var members = bitfield.Members.Select(_memberFactory.Create).ToList(); | ||
|
||
bool ValueIsULong(Member member) | ||
{ | ||
// Bitfields are transfered as ulong and thus do not support negative values. | ||
// Negative values can occur as C convenience members as a "mask". | ||
// Those can safely be should be ignored. | ||
|
||
if (ulong.TryParse(member.Value, out var _)) | ||
return true; | ||
|
||
Log.Verbose($"{nameof(Bitfield)} - {member.OriginalName} ignored because value is no ulong: {member.Value}"); | ||
return false; | ||
} | ||
|
||
return new Bitfield( | ||
repository: repository, | ||
originalName: new TypeName(bitfield.Name), | ||
name: new TypeName(bitfield.Name), | ||
members: members.Where(ValueIsULong), | ||
cType: new CType(bitfield.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
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
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