Skip to content

Commit

Permalink
Merge pull request #9 from bottlenoselabs/enums
Browse files Browse the repository at this point in the history
Enums
  • Loading branch information
lithiumtoast authored Mar 31, 2024
2 parents 4b4daeb + d244d18 commit 9c70d44
Show file tree
Hide file tree
Showing 38 changed files with 506 additions and 589 deletions.
27 changes: 27 additions & 0 deletions src/c/tests/enums/enum_uint8/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"inputFilePath": "./main.c",
"userIncludeDirectories": [
"../../../production/ffi_helper/include"
],
"ignoreIncludeFiles": [
"../../../production/ffi_helper/include/ffi_helper.h"
],
"targetPlatforms": {
"windows": {
"i686-pc-windows-msvc": {},
"x86_64-pc-windows-msvc": {},
"aarch64-pc-windows-msvc": {}
},
"macos": {
"i686-apple-darwin": {},
"aarch64-apple-darwin": {},
"x86_64-apple-darwin": {},
"aarch64-apple-ios": {}
},
"linux": {
"i686-unknown-linux-gnu": {},
"x86_64-unknown-linux-gnu": {},
"aarch64-unknown-linux-gnu": {}
}
}
}
9 changes: 9 additions & 0 deletions src/c/tests/enums/enum_uint8/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdio.h>
#include "ffi_helper.h"

enum enum_uint8 {
ENUM_UINT8_MIN = 0,
ENUM_UINT8_MAX = 255
} enum_uint8;

FFI_API_DECL enum enum_uint8 enum_uint8;
27 changes: 27 additions & 0 deletions src/c/tests/enums/enum_week_day/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"inputFilePath": "./main.c",
"userIncludeDirectories": [
"../../../production/ffi_helper/include"
],
"ignoreIncludeFiles": [
"../../../production/ffi_helper/include/ffi_helper.h"
],
"targetPlatforms": {
"windows": {
"i686-pc-windows-msvc": {},
"x86_64-pc-windows-msvc": {},
"aarch64-pc-windows-msvc": {}
},
"macos": {
"i686-apple-darwin": {},
"aarch64-apple-darwin": {},
"x86_64-apple-darwin": {},
"aarch64-apple-ios": {}
},
"linux": {
"i686-unknown-linux-gnu": {},
"x86_64-unknown-linux-gnu": {},
"aarch64-unknown-linux-gnu": {}
}
}
}
14 changes: 14 additions & 0 deletions src/c/tests/enums/enum_week_day/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>
#include "ffi_helper.h"

typedef enum enum_week_day {
ENUM_WEEK_DAY_UNKNOWN = -1,
ENUM_WEEK_DAY_MONDAY = 1,
ENUM_WEEK_DAY_TUESDAY = 2,
ENUM_WEEK_DAY_WEDNESDAY = 3,
ENUM_WEEK_DAY_THURSDAY = 4,
ENUM_WEEK_DAY_FRIDAY = 5,
_ENUM_WEEK_DAY_MAX = 6
} enum_week_day;

FFI_API_DECL enum_week_day enum_week_day;
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 All @@ -48,9 +48,8 @@ private CEnum Enum(ExploreContext context, ExploreCandidateInfoNode info)

private static CTypeInfo IntegerTypeInfo(ExploreContext context, ExploreCandidateInfoNode info)
{
var integerType = clang_getEnumDeclIntegerType(info.Cursor);
var typeInfo = context.VisitType(integerType, info)!;
return typeInfo;
var clangType = clang_getEnumDeclIntegerType(info.Cursor);
return context.VisitType(clangType, info);
}

private ImmutableArray<CEnumValue> EnumValues(CXCursor cursor)
Expand Down
Loading

0 comments on commit 9c70d44

Please sign in to comment.