Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bgen] Add support for more backing field types. #21172

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/website/binding_types_reference_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -2250,6 +2250,8 @@ Currently, these types are supported:

* `nint`
* `nuint`
* `int`, `uint`
* `long`, `ulong`
* `NSNumber`
* `NSString` (this is the default if none is specified)

Expand Down
9 changes: 8 additions & 1 deletion src/bgen/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,14 @@ void GenerateEnum (Type type)
var isBackingFieldValueType = backingFieldType.IsValueType;
var visibility = is_internal ? "internal" : "public";

if (backingFieldType != TypeCache.System_nint && backingFieldType != TypeCache.System_nuint && backingFieldType != TypeCache.NSString && backingFieldType != TypeCache.NSNumber) {
if (backingFieldType != TypeCache.System_nint &&
backingFieldType != TypeCache.System_nuint &&
backingFieldType != TypeCache.System_Int32 &&
backingFieldType != TypeCache.System_Int64 &&
backingFieldType != TypeCache.System_UInt32 &&
backingFieldType != TypeCache.System_UInt64 &&
backingFieldType != TypeCache.NSString &&
backingFieldType != TypeCache.NSNumber) {
exceptions.Add (ErrorHelper.CreateError (1088 /* The backing field type '{0}' is invalid. Valid backing field types are: "NSString", "NSNumber", "nint" and "nuint". */, backingFieldType.FullName));
backingFieldType = TypeCache.NSString;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/generator/BGenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,10 @@ public void BackingFieldType (Profile profile)
new { BackingFieldType = "NSNumber", NullableType = "Foundation.NSNumber", RenderedBackingFieldType = "Foundation.NSNumber", SimplifiedNullableType = "Foundation.NSNumber" },
new { BackingFieldType = "NSInteger", NullableType = $"System.Nullable`1<{nintName}>", RenderedBackingFieldType = nintName, SimplifiedNullableType = "System.Nullable`1" },
new { BackingFieldType = "NSUInteger", NullableType = $"System.Nullable`1<{nuintName}>", RenderedBackingFieldType = nuintName, SimplifiedNullableType = "System.Nullable`1" },
new { BackingFieldType = "Int32", NullableType = $"System.Nullable`1<System.Int32>", RenderedBackingFieldType = "System.Int32", SimplifiedNullableType = "System.Nullable`1" },
new { BackingFieldType = "Int64", NullableType = $"System.Nullable`1<System.Int64>", RenderedBackingFieldType = "System.Int64", SimplifiedNullableType = "System.Nullable`1" },
new { BackingFieldType = "UInt32", NullableType = $"System.Nullable`1<System.UInt32>", RenderedBackingFieldType = "System.UInt32", SimplifiedNullableType = "System.Nullable`1" },
new { BackingFieldType = "UInt64", NullableType = $"System.Nullable`1<System.UInt64>", RenderedBackingFieldType = "System.UInt64", SimplifiedNullableType = "System.Nullable`1" },
};

foreach (var tc in testCases) {
Expand Down
36 changes: 36 additions & 0 deletions tests/generator/tests/backingfieldtype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ enum NSNumberFieldType {
C,
}

[BackingFieldType (typeof (Int32))]
enum Int32FieldType {
[Field ("DField", "__Internal")]
D,
}

[BackingFieldType (typeof (Int64))]
enum Int64FieldType {
[Field ("EField", "__Internal")]
E,
}

[BackingFieldType (typeof (UInt32))]
enum UInt32FieldType {
[Field ("FField", "__Internal")]
F,
}

[BackingFieldType (typeof (UInt64))]
enum UInt64FieldType {
[Field ("GField", "__Internal")]
G,
}

[BaseType (typeof (NSObject))]
interface SomeObj {
[Export ("nsIntegerField")]
Expand All @@ -30,5 +54,17 @@ interface SomeObj {

[Export ("nsNumberField")]
NSNumberFieldType NSNumberField { get; set; }

[Export ("int32Field")]
Int32FieldType Int32Field { get; set; }

[Export ("int64Field")]
Int64FieldType Int64Field { get; set; }

[Export ("uint32Field")]
UInt32FieldType UInt32Field { get; set; }

[Export ("uint64Field")]
UInt64FieldType UInt64Field { get; set; }
}
}