Skip to content

Commit 49661db

Browse files
authored
[ffigen] Add setters for dart-style enum members for structs (#2855)
1 parent 5765044 commit 49661db

File tree

7 files changed

+37
-1
lines changed

7 files changed

+37
-1
lines changed

pkgs/ffigen/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- __Breaking change__: Certain synthetic USRs have been modified to ensure they
1010
cannot collide with real USRs. It's very unlikely that any user facing USRs
1111
are affected.
12+
- Enum struct members now have setters.
1213
- __Breaking change__: Dart const values will be generated for global variables
1314
marked const in C (e.g. static const int) instead of symbol lookups. This
1415
supports integers, doubles, and string literals. Including the variable name

pkgs/ffigen/example/libclang-example/generated_bindings.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7535,6 +7535,7 @@ final class CXTUResourceUsageEntry extends ffi.Struct {
75357535
external int kindAsInt;
75367536

75377537
CXTUResourceUsageKind get kind => CXTUResourceUsageKind.fromValue(kindAsInt);
7538+
set kind(CXTUResourceUsageKind value) => kindAsInt = value.value;
75387539

75397540
@ffi.UnsignedLong()
75407541
external int amount;
@@ -8667,6 +8668,7 @@ final class CXCursor extends ffi.Struct {
86678668
external int kindAsInt;
86688669

86698670
CXCursorKind get kind => CXCursorKind.fromValue(kindAsInt);
8671+
set kind(CXCursorKind value) => kindAsInt = value.value;
86708672

86718673
@ffi.Int()
86728674
external int xdata;
@@ -9259,6 +9261,7 @@ final class CXType extends ffi.Struct {
92599261
external int kindAsInt;
92609262

92619263
CXTypeKind get kind => CXTypeKind.fromValue(kindAsInt);
9264+
set kind(CXTypeKind value) => kindAsInt = value.value;
92629265

92639266
@ffi.Array.multi([2])
92649267
external ffi.Array<ffi.Pointer<ffi.Void>> data;
@@ -10109,6 +10112,7 @@ final class CXCompletionResult extends ffi.Struct {
1010910112
external int CursorKindAsInt;
1011010113

1011110114
CXCursorKind get CursorKind => CXCursorKind.fromValue(CursorKindAsInt);
10115+
set CursorKind(CXCursorKind value) => CursorKindAsInt = value.value;
1011210116

1011310117
/// The code-completion string that describes how to insert this
1011410118
/// code-completion result into the editing buffer.
@@ -10867,6 +10871,7 @@ final class CXIdxAttrInfo extends ffi.Struct {
1086710871
external int kindAsInt;
1086810872

1086910873
CXIdxAttrKind get kind => CXIdxAttrKind.fromValue(kindAsInt);
10874+
set kind(CXIdxAttrKind value) => kindAsInt = value.value;
1087010875

1087110876
external CXCursor cursor;
1087210877

@@ -10878,17 +10883,21 @@ final class CXIdxEntityInfo extends ffi.Struct {
1087810883
external int kindAsInt;
1087910884

1088010885
CXIdxEntityKind get kind => CXIdxEntityKind.fromValue(kindAsInt);
10886+
set kind(CXIdxEntityKind value) => kindAsInt = value.value;
1088110887

1088210888
@ffi.UnsignedInt()
1088310889
external int templateKindAsInt;
1088410890

1088510891
CXIdxEntityCXXTemplateKind get templateKind =>
1088610892
CXIdxEntityCXXTemplateKind.fromValue(templateKindAsInt);
10893+
set templateKind(CXIdxEntityCXXTemplateKind value) =>
10894+
templateKindAsInt = value.value;
1088710895

1088810896
@ffi.UnsignedInt()
1088910897
external int langAsInt;
1089010898

1089110899
CXIdxEntityLanguage get lang => CXIdxEntityLanguage.fromValue(langAsInt);
10900+
set lang(CXIdxEntityLanguage value) => langAsInt = value.value;
1089210901

1089310902
external ffi.Pointer<ffi.Char> name;
1089410903

@@ -10980,6 +10989,7 @@ final class CXIdxObjCContainerDeclInfo extends ffi.Struct {
1098010989

1098110990
CXIdxObjCContainerKind get kind =>
1098210991
CXIdxObjCContainerKind.fromValue(kindAsInt);
10992+
set kind(CXIdxObjCContainerKind value) => kindAsInt = value.value;
1098310993
}
1098410994

1098510995
final class CXIdxBaseClassInfo extends ffi.Struct {
@@ -11104,6 +11114,7 @@ final class CXIdxEntityRefInfo extends ffi.Struct {
1110411114
external int kindAsInt;
1110511115

1110611116
CXIdxEntityRefKind get kind => CXIdxEntityRefKind.fromValue(kindAsInt);
11117+
set kind(CXIdxEntityRefKind value) => kindAsInt = value.value;
1110711118

1110811119
/// Reference cursor.
1110911120
external CXCursor cursor;
@@ -11132,6 +11143,7 @@ final class CXIdxEntityRefInfo extends ffi.Struct {
1113211143
external int roleAsInt;
1113311144

1113411145
CXSymbolRole get role => CXSymbolRole.fromValue(roleAsInt);
11146+
set role(CXSymbolRole value) => roleAsInt = value.value;
1113511147
}
1113611148

1113711149
/// A group of callbacks used by #clang_indexSourceFile and

pkgs/ffigen/lib/src/code_generator/compound.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ abstract class Compound extends BindingType with HasLocalScope {
110110
final memberName = m.name;
111111
s.write(
112112
'$enumName get $memberName => '
113-
'$enumName.fromValue(${memberName}AsInt);\n\n',
113+
'$enumName.fromValue(${memberName}AsInt);\n',
114+
);
115+
s.write(
116+
'set $memberName($enumName value) => '
117+
'${memberName}AsInt = value.value;\n\n',
114118
);
115119
}
116120
}

pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_enumclass_func_and_struct_bindings.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ final class StructWithEnums extends ffi.Struct {
8787
external int enum1AsInt;
8888

8989
Enum1 get enum1 => Enum1.fromValue(enum1AsInt);
90+
set enum1(Enum1 value) => enum1AsInt = value.value;
9091

9192
@ffi.Int()
9293
external int enum2;

pkgs/ffigen/test/header_parser_tests/expected_bindings/_expected_enum_int_mimic_bindings.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ final class Test extends ffi.Struct {
7777
external int simpleAsInt;
7878

7979
Simple get simple => Simple.fromValue(simpleAsInt);
80+
set simple(Simple value) => simpleAsInt = value.value;
8081

8182
external ffi.Pointer<ffi.Int> simpleWithNegative;
8283

@@ -97,12 +98,16 @@ final class Test extends ffi.Struct {
9798

9899
PositiveIntOverflow get positiveIntOverflow =>
99100
PositiveIntOverflow.fromValue(positiveIntOverflowAsInt);
101+
set positiveIntOverflow(PositiveIntOverflow value) =>
102+
positiveIntOverflowAsInt = value.value;
100103

101104
@ffi.Uint16()
102105
external int explicitTypeWithOverflowAsInt;
103106

104107
ExplicitTypeWithOverflow get explicitTypeWithOverflow =>
105108
ExplicitTypeWithOverflow.fromValue(explicitTypeWithOverflowAsInt);
109+
set explicitTypeWithOverflow(ExplicitTypeWithOverflow value) =>
110+
explicitTypeWithOverflowAsInt = value.value;
106111
}
107112

108113
const int ANONYMOUS1 = 0;

pkgs/ffigen/test/large_integration_tests/_expected_libclang_bindings.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6076,6 +6076,7 @@ final class CXTUResourceUsageEntry extends ffi.Struct {
60766076
external int kindAsInt;
60776077

60786078
CXTUResourceUsageKind get kind => CXTUResourceUsageKind.fromValue(kindAsInt);
6079+
set kind(CXTUResourceUsageKind value) => kindAsInt = value.value;
60796080

60806081
@ffi.UnsignedLong()
60816082
external int amount;
@@ -7040,6 +7041,7 @@ final class CXCursor extends ffi.Struct {
70407041
external int kindAsInt;
70417042

70427043
CXCursorKind get kind => CXCursorKind.fromValue(kindAsInt);
7044+
set kind(CXCursorKind value) => kindAsInt = value.value;
70437045

70447046
@ffi.Int()
70457047
external int xdata;
@@ -7500,6 +7502,7 @@ final class CXType extends ffi.Struct {
75007502
external int kindAsInt;
75017503

75027504
CXTypeKind get kind => CXTypeKind.fromValue(kindAsInt);
7505+
set kind(CXTypeKind value) => kindAsInt = value.value;
75037506

75047507
@ffi.Array.multi([2])
75057508
external ffi.Array<ffi.Pointer<ffi.Void>> data;
@@ -7932,6 +7935,7 @@ final class CXCompletionResult extends ffi.Struct {
79327935
external int CursorKindAsInt;
79337936

79347937
CXCursorKind get CursorKind => CXCursorKind.fromValue(CursorKindAsInt);
7938+
set CursorKind(CXCursorKind value) => CursorKindAsInt = value.value;
79357939

79367940
/// The code-completion string that describes how to insert this
79377941
/// code-completion result into the editing buffer.
@@ -8500,6 +8504,7 @@ final class CXIdxAttrInfo extends ffi.Struct {
85008504
external int kindAsInt;
85018505

85028506
CXIdxAttrKind get kind => CXIdxAttrKind.fromValue(kindAsInt);
8507+
set kind(CXIdxAttrKind value) => kindAsInt = value.value;
85038508

85048509
external CXCursor cursor;
85058510

@@ -8511,17 +8516,21 @@ final class CXIdxEntityInfo extends ffi.Struct {
85118516
external int kindAsInt;
85128517

85138518
CXIdxEntityKind get kind => CXIdxEntityKind.fromValue(kindAsInt);
8519+
set kind(CXIdxEntityKind value) => kindAsInt = value.value;
85148520

85158521
@ffi.UnsignedInt()
85168522
external int templateKindAsInt;
85178523

85188524
CXIdxEntityCXXTemplateKind get templateKind =>
85198525
CXIdxEntityCXXTemplateKind.fromValue(templateKindAsInt);
8526+
set templateKind(CXIdxEntityCXXTemplateKind value) =>
8527+
templateKindAsInt = value.value;
85208528

85218529
@ffi.UnsignedInt()
85228530
external int langAsInt;
85238531

85248532
CXIdxEntityLanguage get lang => CXIdxEntityLanguage.fromValue(langAsInt);
8533+
set lang(CXIdxEntityLanguage value) => langAsInt = value.value;
85258534

85268535
external ffi.Pointer<ffi.Char> name;
85278536

@@ -8625,6 +8634,7 @@ final class CXIdxObjCContainerDeclInfo extends ffi.Struct {
86258634

86268635
CXIdxObjCContainerKind get kind =>
86278636
CXIdxObjCContainerKind.fromValue(kindAsInt);
8637+
set kind(CXIdxObjCContainerKind value) => kindAsInt = value.value;
86288638
}
86298639

86308640
final class CXIdxBaseClassInfo extends ffi.Struct {
@@ -8743,6 +8753,7 @@ final class CXIdxEntityRefInfo extends ffi.Struct {
87438753
external int kindAsInt;
87448754

87458755
CXIdxEntityRefKind get kind => CXIdxEntityRefKind.fromValue(kindAsInt);
8756+
set kind(CXIdxEntityRefKind value) => kindAsInt = value.value;
87468757

87478758
/// Reference cursor.
87488759
external CXCursor cursor;
@@ -8763,6 +8774,7 @@ final class CXIdxEntityRefInfo extends ffi.Struct {
87638774
external int roleAsInt;
87648775

87658776
CXSymbolRole get role => CXSymbolRole.fromValue(roleAsInt);
8777+
set role(CXSymbolRole value) => roleAsInt = value.value;
87668778
}
87678779

87688780
/// A group of callbacks used by #clang_indexSourceFile and

pkgs/ffigen/test/native_test/_expected_native_test_bindings.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ final class StructWithEnums extends ffi.Struct {
276276
external int enum1AsInt;
277277

278278
Enum1 get enum1 => Enum1.fromValue(enum1AsInt);
279+
set enum1(Enum1 value) => enum1AsInt = value.value;
279280

280281
@ffi.Array.multi([5])
281282
external ffi.Array<ffi.UnsignedInt> enum1Array;

0 commit comments

Comments
 (0)