Skip to content

Commit

Permalink
Remove enum type from FFI
Browse files Browse the repository at this point in the history
  • Loading branch information
lithiumtoast committed Mar 31, 2024
1 parent 5563961 commit 6083d1d
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 60 deletions.
17 changes: 9 additions & 8 deletions src/cs/production/c2ffi.Data/CTypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,37 @@ public class CTypeInfo : IEquatable<CTypeInfo>
/// <summary>
/// Gets or sets the byte size of the C type.
/// </summary>
[JsonPropertyName("sizeOf")]
[JsonPropertyName("size_of")]
public int? SizeOf { get; set; }

/// <summary>
/// Gets or sets the byte alignment of the C type.
/// </summary>
[JsonPropertyName("alignOf")]
[JsonPropertyName("align_of")]
public int? AlignOf { get; set; }

/// <summary>
/// Gets or sets the byte size of the element for array and pointer types.
/// </summary>
[JsonPropertyName("sizeOfElement")]
[JsonPropertyName("size_of_element")]
public int? ElementSize { get; set; }

/// <summary>
/// Gets or sets the element size of the array for array types.
/// </summary>
[JsonPropertyName("arraySize")]
[JsonPropertyName("array_size")]
public int? ArraySizeOf { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the C type is anonymous.
/// </summary>
[JsonPropertyName("isAnonymous")]
[JsonPropertyName("is_snonymous")]
public bool? IsAnonymous { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the C type is read-only.
/// </summary>
[JsonPropertyName("isConst")]
[JsonPropertyName("is_const")]
public bool IsConst { get; set; }

/// <summary>
Expand All @@ -72,7 +72,7 @@ public class CTypeInfo : IEquatable<CTypeInfo>
/// <summary>
/// Gets or sets the inner type information for pointer, array, and typedef alias types.
/// </summary>
[JsonPropertyName("innerType")]
[JsonPropertyName("inner_type")]
public CTypeInfo? InnerTypeInfo { get; set; }

/// <inheritdoc />
Expand Down Expand Up @@ -102,7 +102,8 @@ public bool Equals(CTypeInfo? other)
return true;
}

return NodeKind == other.NodeKind &&
return Name == other.Name &&
NodeKind == other.NodeKind &&
SizeOf == other.SizeOf &&
AlignOf == other.AlignOf &&
ElementSize == other.ElementSize &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,24 @@ public partial class JsonSerializerContextCFfiCrossPlatform
{
var properties = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[6];

var info0 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<global::c2ffi.Data.CTypeInfo>
var info0 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<int>
{
IsProperty = true,
IsPublic = true,
IsVirtual = false,
DeclaringType = typeof(global::c2ffi.Data.Nodes.CEnum),
Converter = null,
Getter = static obj => ((global::c2ffi.Data.Nodes.CEnum)obj).IntegerTypeInfo,
Setter = static (obj, value) => ((global::c2ffi.Data.Nodes.CEnum)obj).IntegerTypeInfo = value!,
Getter = static obj => ((global::c2ffi.Data.Nodes.CEnum)obj).SizeOf,
Setter = static (obj, value) => ((global::c2ffi.Data.Nodes.CEnum)obj).SizeOf = value!,
IgnoreCondition = null,
HasJsonInclude = false,
IsExtensionData = false,
NumberHandling = null,
PropertyName = "IntegerTypeInfo",
JsonPropertyName = "type_integer"
PropertyName = "SizeOf",
JsonPropertyName = "size_of"
};

properties[0] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<global::c2ffi.Data.CTypeInfo>(options, info0);
properties[0] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<int>(options, info0);

var info1 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<global::System.Collections.Immutable.ImmutableArray<global::c2ffi.Data.Nodes.CEnumValue>>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public partial class JsonSerializerContextCFfiCrossPlatform
IsExtensionData = false,
NumberHandling = null,
PropertyName = "SizeOf",
JsonPropertyName = "sizeOf"
JsonPropertyName = "size_of"
};

properties[2] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<int?>(options, info2);
Expand All @@ -116,7 +116,7 @@ public partial class JsonSerializerContextCFfiCrossPlatform
IsExtensionData = false,
NumberHandling = null,
PropertyName = "AlignOf",
JsonPropertyName = "alignOf"
JsonPropertyName = "align_of"
};

properties[3] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<int?>(options, info3);
Expand All @@ -135,7 +135,7 @@ public partial class JsonSerializerContextCFfiCrossPlatform
IsExtensionData = false,
NumberHandling = null,
PropertyName = "ElementSize",
JsonPropertyName = "sizeOfElement"
JsonPropertyName = "size_of_element"
};

properties[4] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<int?>(options, info4);
Expand All @@ -154,7 +154,7 @@ public partial class JsonSerializerContextCFfiCrossPlatform
IsExtensionData = false,
NumberHandling = null,
PropertyName = "ArraySizeOf",
JsonPropertyName = "arraySize"
JsonPropertyName = "array_size"
};

properties[5] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<int?>(options, info5);
Expand All @@ -173,7 +173,7 @@ public partial class JsonSerializerContextCFfiCrossPlatform
IsExtensionData = false,
NumberHandling = null,
PropertyName = "IsAnonymous",
JsonPropertyName = "isAnonymous"
JsonPropertyName = "is_snonymous"
};

properties[6] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<bool?>(options, info6);
Expand All @@ -192,7 +192,7 @@ public partial class JsonSerializerContextCFfiCrossPlatform
IsExtensionData = false,
NumberHandling = null,
PropertyName = "IsConst",
JsonPropertyName = "isConst"
JsonPropertyName = "is_const"
};

properties[7] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<bool>(options, info7);
Expand Down Expand Up @@ -230,7 +230,7 @@ public partial class JsonSerializerContextCFfiCrossPlatform
IsExtensionData = false,
NumberHandling = null,
PropertyName = "InnerTypeInfo",
JsonPropertyName = "innerType"
JsonPropertyName = "inner_type"
};

properties[9] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<global::c2ffi.Data.CTypeInfo>(options, info9);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,24 @@ public partial class JsonSerializerContextCFfiTargetPlatform
{
var properties = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[6];

var info0 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<global::c2ffi.Data.CTypeInfo>
var info0 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<int>
{
IsProperty = true,
IsPublic = true,
IsVirtual = false,
DeclaringType = typeof(global::c2ffi.Data.Nodes.CEnum),
Converter = null,
Getter = static obj => ((global::c2ffi.Data.Nodes.CEnum)obj).IntegerTypeInfo,
Setter = static (obj, value) => ((global::c2ffi.Data.Nodes.CEnum)obj).IntegerTypeInfo = value!,
Getter = static obj => ((global::c2ffi.Data.Nodes.CEnum)obj).SizeOf,
Setter = static (obj, value) => ((global::c2ffi.Data.Nodes.CEnum)obj).SizeOf = value!,
IgnoreCondition = null,
HasJsonInclude = false,
IsExtensionData = false,
NumberHandling = null,
PropertyName = "IntegerTypeInfo",
JsonPropertyName = "type_integer"
PropertyName = "SizeOf",
JsonPropertyName = "size_of"
};

properties[0] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<global::c2ffi.Data.CTypeInfo>(options, info0);
properties[0] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<int>(options, info0);

var info1 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<global::System.Collections.Immutable.ImmutableArray<global::c2ffi.Data.Nodes.CEnumValue>>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public partial class JsonSerializerContextCFfiTargetPlatform
IsExtensionData = false,
NumberHandling = null,
PropertyName = "SizeOf",
JsonPropertyName = "sizeOf"
JsonPropertyName = "size_of"
};

properties[2] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<int?>(options, info2);
Expand All @@ -116,7 +116,7 @@ public partial class JsonSerializerContextCFfiTargetPlatform
IsExtensionData = false,
NumberHandling = null,
PropertyName = "AlignOf",
JsonPropertyName = "alignOf"
JsonPropertyName = "align_of"
};

properties[3] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<int?>(options, info3);
Expand All @@ -135,7 +135,7 @@ public partial class JsonSerializerContextCFfiTargetPlatform
IsExtensionData = false,
NumberHandling = null,
PropertyName = "ElementSize",
JsonPropertyName = "sizeOfElement"
JsonPropertyName = "size_of_element"
};

properties[4] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<int?>(options, info4);
Expand All @@ -154,7 +154,7 @@ public partial class JsonSerializerContextCFfiTargetPlatform
IsExtensionData = false,
NumberHandling = null,
PropertyName = "ArraySizeOf",
JsonPropertyName = "arraySize"
JsonPropertyName = "array_size"
};

properties[5] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<int?>(options, info5);
Expand All @@ -173,7 +173,7 @@ public partial class JsonSerializerContextCFfiTargetPlatform
IsExtensionData = false,
NumberHandling = null,
PropertyName = "IsAnonymous",
JsonPropertyName = "isAnonymous"
JsonPropertyName = "is_snonymous"
};

properties[6] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<bool?>(options, info6);
Expand All @@ -192,7 +192,7 @@ public partial class JsonSerializerContextCFfiTargetPlatform
IsExtensionData = false,
NumberHandling = null,
PropertyName = "IsConst",
JsonPropertyName = "isConst"
JsonPropertyName = "is_const"
};

properties[7] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<bool>(options, info7);
Expand Down Expand Up @@ -230,7 +230,7 @@ public partial class JsonSerializerContextCFfiTargetPlatform
IsExtensionData = false,
NumberHandling = null,
PropertyName = "InnerTypeInfo",
JsonPropertyName = "innerType"
JsonPropertyName = "inner_type"
};

properties[9] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<global::c2ffi.Data.CTypeInfo>(options, info9);
Expand Down
15 changes: 9 additions & 6 deletions src/cs/production/c2ffi.Data/Nodes/CEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@ namespace c2ffi.Data.Nodes;
public class CEnum : CNodeWithLocation
{
/// <summary>
/// Gets or sets the type information about the enum's integer type.
/// Gets or sets the byte size of the enum.
/// </summary>
[JsonPropertyName("type_integer")]
public CTypeInfo IntegerTypeInfo { get; set; } = null!;
[JsonPropertyName("size_of")]
public int SizeOf { get; set; }

/// <summary>
/// Gets or sets the enum's values.
/// </summary>
[JsonPropertyName("values")]
public ImmutableArray<CEnumValue> Values { get; set; } = ImmutableArray<CEnumValue>.Empty;

/// <inheritdoc />
[ExcludeFromCodeCoverage]
public override string ToString()
{
return $"Enum '{Name}': {IntegerTypeInfo} @ {Location}";
return $"Enum '{Name}': @ {Location}";
}

/// <inheritdoc />
Expand All @@ -40,7 +43,7 @@ public override bool Equals(CNode? other)
return false;
}

return IntegerTypeInfo.Equals(other2.IntegerTypeInfo) && Values.SequenceEqual(other2.Values);
return SizeOf.Equals(other2.SizeOf) && Values.SequenceEqual(other2.Values);
}

/// <inheritdoc />
Expand All @@ -52,7 +55,7 @@ public override int GetHashCode()
hashCode.Add(baseHashCode);

// ReSharper disable NonReadonlyMemberInGetHashCode
hashCode.Add(IntegerTypeInfo);
hashCode.Add(SizeOf);

foreach (var value in Values)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private CEnum Enum(ExploreContext context, ExploreCandidateInfoNode info)
{
Name = info.Name,
Location = info.Location,
IntegerTypeInfo = integerTypeInfo,
SizeOf = integerTypeInfo.SizeOf!.Value,
Values = enumValues,
Comment = comment,
IsSystem = isSystemCursor
Expand Down
21 changes: 11 additions & 10 deletions src/cs/production/c2ffi.Tool/Commands/Merge/MergeFfisTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void Run(string inputDirectoryPath, string outputFilePath)
GetPlatformFfis(options.InputFilePaths);
var platforms = platformFfis.
Select(x => x.PlatformRequested).ToImmutableArray();
var platformNodesByName = GetPlatformNodesByName(platformFfis);
var ffi = CreateCrossPlatformFfi(platforms, platformNodesByName);
var platformNodesByKey = GetPlatformNodesByKey(platformFfis);
var ffi = CreateCrossPlatformFfi(platforms, platformNodesByKey);

Json.WriteFfiCrossPlatform(_fileSystem, options.OutputFilePath, ffi);
LogWriteAbstractSyntaxTreeSuccess(string.Join(", ", platforms), options.OutputFilePath);
Expand All @@ -77,13 +77,13 @@ private MergeOptions GetOptions(string inputDirectoryPath, string outputFilePath

private CFfiCrossPlatform CreateCrossPlatformFfi(
ImmutableArray<TargetPlatform> platforms,
ImmutableSortedDictionary<string, ImmutableArray<CNodeWithTargetPlatform>> platformNodesByName)
ImmutableSortedDictionary<string, ImmutableArray<CNodeWithTargetPlatform>> platformNodesByKey)
{
var result = new CFfiCrossPlatform();

foreach (var (name, nodes) in platformNodesByName)
foreach (var (key, nodes) in platformNodesByKey)
{
BuildCrossPlatformNodes(platforms, nodes, name);
BuildCrossPlatformNodes(platforms, nodes);
}

result.Platforms = platforms.Sort(
Expand Down Expand Up @@ -113,7 +113,6 @@ private void AddCrossPlatformNode(CNodeWithTargetPlatform nodeWithTargetPlatform
switch (node)
{
case CEnum @enum:
ClearLocationForTypeInfo(@enum.IntegerTypeInfo);
_enums.Add(@enum);
break;
case CVariable variable:
Expand Down Expand Up @@ -179,9 +178,10 @@ private void ClearLocationForTypeInfo(CTypeInfo typeInfo)

private void BuildCrossPlatformNodes(
ImmutableArray<TargetPlatform> platforms,
ImmutableArray<CNodeWithTargetPlatform> nodes,
string nodeName)
ImmutableArray<CNodeWithTargetPlatform> nodes)
{
var nodeName = nodes.FirstOrDefault()?.Node.Name ?? string.Empty;

if (nodes.Length != platforms.Length)
{
var nodePlatforms = nodes.Select(x => x.TargetPlatform);
Expand All @@ -203,7 +203,8 @@ private void BuildCrossPlatformNodes(
{
var node = nodes[i].Node;

if (!node.Equals(firstNode))
var nodesAreEqual = node.Equals(firstNode);
if (!nodesAreEqual)
{
if (node is CMacroObject nodeMacroObject && firstNode is CMacroObject firstNodeMacroObject)
{
Expand All @@ -226,7 +227,7 @@ private void BuildCrossPlatformNodes(
}
}

private ImmutableSortedDictionary<string, ImmutableArray<CNodeWithTargetPlatform>> GetPlatformNodesByName(
private ImmutableSortedDictionary<string, ImmutableArray<CNodeWithTargetPlatform>> GetPlatformNodesByKey(
ImmutableArray<CFfiTargetPlatform> platformFfis)
{
var platformNodesByKey = new Dictionary<string, List<CNodeWithTargetPlatform>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private void FfiEnumExists(CTestFfiTargetPlatform ffi)
{
const string name = $"enum {EnumName}";
var @enum = ffi.GetEnum(name);
@enum.IntegerTypeName.Should().BeOneOf("unsigned int", "int");
@enum.SizeOf.Should().Be(4);

@enum.Values[0].Name.Should().Be("ENUM_UINT8_MIN");
@enum.Values[0].Value.Should().Be(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private void FfiEnumExists(CTestFfiTargetPlatform ffi)
{
const string name = $"enum {EnumName}";
var @enum = ffi.GetEnum(name);
@enum.IntegerTypeName.Should().Be("int");
@enum.SizeOf.Should().Be(4);

@enum.Values[0].Name.Should().Be("ENUM_WEEK_DAY_UNKNOWN");
@enum.Values[0].Value.Should().Be(-1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private void FfiEnumExists(CTestFfiCrossPlatform ffi)
{
const string name = $"enum {EnumName}";
var @enum = ffi.GetEnum(name);
@enum.IntegerTypeName.Should().Be("unsigned int");
@enum.SizeOf.Should().Be(4);

@enum.Values[0].Name.Should().Be("ENUM_UINT8_MIN");
@enum.Values[0].Value.Should().Be(0);
Expand Down
Loading

0 comments on commit 6083d1d

Please sign in to comment.