Skip to content

Commit 6bf102a

Browse files
authored
[ffigen] Added allocate constructor for native C structs (dart-lang#3097)
1 parent 173690d commit 6bf102a

31 files changed

Lines changed: 2075 additions & 3 deletions

‎pkgs/ffigen/CHANGELOG.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
- __Breaking change__: Deleted the config option `Output.sort`. Sorting is now
2323
always enabled.
2424
- Fix(https://github.com/dart-lang/native/issues/2877)
25-
such that ObjCObject `isA` now accepts a nullable `ObjCObject?` and returns `false` when called with `null`, aligning its behavior with Dart’s `is`operator.
25+
such that ObjCObject `isA` now accepts a nullable `ObjCObject?` and returns
26+
`false` when called with `null`, aligning its behavior with Dart’s `is`operator.
2627
- Use `xcrun` for resolving macOS SDK paths, enabling support for non-standard
27-
Xcode installations. ([#3134](https://github.com/dart-lang/native/issues/3134))
28+
Xcode installations. [#3134](https://github.com/dart-lang/native/issues/3134)
29+
- Add allocate constructor for native C structs:
30+
`$allocate(Allocator $allocator, {required ...})`
2831

2932
## 20.1.1
3033

‎pkgs/ffigen/example/c_json/cjson_generated_bindings.dart‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,26 @@ final class cJSON extends ffi.Struct {
12451245
external double valuedouble;
12461246

12471247
external ffi.Pointer<ffi.Char> string;
1248+
1249+
static ffi.Pointer<cJSON> $allocate(
1250+
ffi.Allocator $allocator, {
1251+
required ffi.Pointer<cJSON> next,
1252+
required ffi.Pointer<cJSON> prev,
1253+
required ffi.Pointer<cJSON> child,
1254+
required int type,
1255+
required ffi.Pointer<ffi.Char> valuestring,
1256+
required int valueint,
1257+
required double valuedouble,
1258+
required ffi.Pointer<ffi.Char> string,
1259+
}) => $allocator<cJSON>()
1260+
..ref.next = next
1261+
..ref.prev = prev
1262+
..ref.child = child
1263+
..ref.type = type
1264+
..ref.valuestring = valuestring
1265+
..ref.valueint = valueint
1266+
..ref.valuedouble = valuedouble
1267+
..ref.string = string;
12481268
}
12491269

12501270
const int cJSON_Array = 32;
@@ -1261,6 +1281,20 @@ final class cJSON_Hooks extends ffi.Struct {
12611281
ffi.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Void> ptr)>
12621282
>
12631283
free_fn;
1284+
1285+
static ffi.Pointer<cJSON_Hooks> $allocate(
1286+
ffi.Allocator $allocator, {
1287+
required ffi.Pointer<
1288+
ffi.NativeFunction<ffi.Pointer<ffi.Void> Function(ffi.Size sz)>
1289+
>
1290+
malloc_fn,
1291+
required ffi.Pointer<
1292+
ffi.NativeFunction<ffi.Void Function(ffi.Pointer<ffi.Void> ptr)>
1293+
>
1294+
free_fn,
1295+
}) => $allocator<cJSON_Hooks>()
1296+
..ref.malloc_fn = malloc_fn
1297+
..ref.free_fn = free_fn;
12641298
}
12651299

12661300
const int cJSON_Invalid = 0;

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

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6858,6 +6858,14 @@ final class CXCodeCompleteResults extends ffi.Struct {
68586858
/// \c Results array.
68596859
@ffi.UnsignedInt()
68606860
external int NumResults;
6861+
6862+
static ffi.Pointer<CXCodeCompleteResults> $allocate(
6863+
ffi.Allocator $allocator, {
6864+
required ffi.Pointer<CXCompletionResult> Results,
6865+
required int NumResults,
6866+
}) => $allocator<CXCodeCompleteResults>()
6867+
..ref.Results = Results
6868+
..ref.NumResults = NumResults;
68616869
}
68626870

68636871
/// Describes a single piece of text within a code-completion string.
@@ -7054,6 +7062,14 @@ final class CXCompletionResult extends ffi.Struct {
70547062
/// The code-completion string that describes how to insert this
70557063
/// code-completion result into the editing buffer.
70567064
external CXCompletionString CompletionString;
7065+
7066+
static ffi.Pointer<CXCompletionResult> $allocate(
7067+
ffi.Allocator $allocator, {
7068+
required CXCursorKind CursorKind,
7069+
required CXCompletionString CompletionString,
7070+
}) => $allocator<CXCompletionResult>()
7071+
..ref.CursorKind = CursorKind
7072+
..ref.CompletionString = CompletionString;
70577073
}
70587074

70597075
/// A semantic string that describes a code-completion result.
@@ -7108,6 +7124,19 @@ final class CXCursorAndRangeVisitor extends ffi.Struct {
71087124
>
71097125
>
71107126
visit;
7127+
7128+
static ffi.Pointer<CXCursorAndRangeVisitor> $allocate(
7129+
ffi.Allocator $allocator, {
7130+
required ffi.Pointer<ffi.Void> context,
7131+
required ffi.Pointer<
7132+
ffi.NativeFunction<
7133+
ffi.UnsignedInt Function(ffi.Pointer<ffi.Void>, CXCursor, CXSourceRange)
7134+
>
7135+
>
7136+
visit,
7137+
}) => $allocator<CXCursorAndRangeVisitor>()
7138+
..ref.context = context
7139+
..ref.visit = visit;
71117140
}
71127141

71137142
/// Describes the kind of entity that a cursor refers to.
@@ -8425,6 +8454,16 @@ final class CXIdxCXXClassDeclInfo extends ffi.Struct {
84258454

84268455
@ffi.UnsignedInt()
84278456
external int numBases;
8457+
8458+
static ffi.Pointer<CXIdxCXXClassDeclInfo> $allocate(
8459+
ffi.Allocator $allocator, {
8460+
required ffi.Pointer<CXIdxDeclInfo> declInfo,
8461+
required ffi.Pointer<ffi.Pointer<CXIdxBaseClassInfo>> bases,
8462+
required int numBases,
8463+
}) => $allocator<CXIdxCXXClassDeclInfo>()
8464+
..ref.declInfo = declInfo
8465+
..ref.bases = bases
8466+
..ref.numBases = numBases;
84288467
}
84298468

84308469
/// The client's data object that is associated with an AST file (PCH
@@ -8768,6 +8807,14 @@ final class CXIdxObjCContainerDeclInfo extends ffi.Struct {
87688807
CXIdxObjCContainerKind get kind =>
87698808
CXIdxObjCContainerKind.fromValue(kindAsInt);
87708809
set kind(CXIdxObjCContainerKind value) => kindAsInt = value.value;
8810+
8811+
static ffi.Pointer<CXIdxObjCContainerDeclInfo> $allocate(
8812+
ffi.Allocator $allocator, {
8813+
required ffi.Pointer<CXIdxDeclInfo> declInfo,
8814+
required CXIdxObjCContainerKind kind,
8815+
}) => $allocator<CXIdxObjCContainerDeclInfo>()
8816+
..ref.declInfo = declInfo
8817+
..ref.kind = kind;
87718818
}
87728819

87738820
enum CXIdxObjCContainerKind {
@@ -8794,6 +8841,16 @@ final class CXIdxObjCInterfaceDeclInfo extends ffi.Struct {
87948841
external ffi.Pointer<CXIdxBaseClassInfo> superInfo;
87958842

87968843
external ffi.Pointer<CXIdxObjCProtocolRefListInfo> protocols;
8844+
8845+
static ffi.Pointer<CXIdxObjCInterfaceDeclInfo> $allocate(
8846+
ffi.Allocator $allocator, {
8847+
required ffi.Pointer<CXIdxObjCContainerDeclInfo> containerInfo,
8848+
required ffi.Pointer<CXIdxBaseClassInfo> superInfo,
8849+
required ffi.Pointer<CXIdxObjCProtocolRefListInfo> protocols,
8850+
}) => $allocator<CXIdxObjCInterfaceDeclInfo>()
8851+
..ref.containerInfo = containerInfo
8852+
..ref.superInfo = superInfo
8853+
..ref.protocols = protocols;
87978854
}
87988855

87998856
final class CXIdxObjCPropertyDeclInfo extends ffi.Struct {
@@ -8802,6 +8859,16 @@ final class CXIdxObjCPropertyDeclInfo extends ffi.Struct {
88028859
external ffi.Pointer<CXIdxEntityInfo> getter;
88038860

88048861
external ffi.Pointer<CXIdxEntityInfo> setter;
8862+
8863+
static ffi.Pointer<CXIdxObjCPropertyDeclInfo> $allocate(
8864+
ffi.Allocator $allocator, {
8865+
required ffi.Pointer<CXIdxDeclInfo> declInfo,
8866+
required ffi.Pointer<CXIdxEntityInfo> getter,
8867+
required ffi.Pointer<CXIdxEntityInfo> setter,
8868+
}) => $allocator<CXIdxObjCPropertyDeclInfo>()
8869+
..ref.declInfo = declInfo
8870+
..ref.getter = getter
8871+
..ref.setter = setter;
88058872
}
88068873

88078874
final class CXIdxObjCProtocolRefInfo extends ffi.Struct {
@@ -8817,6 +8884,14 @@ final class CXIdxObjCProtocolRefListInfo extends ffi.Struct {
88178884

88188885
@ffi.UnsignedInt()
88198886
external int numProtocols;
8887+
8888+
static ffi.Pointer<CXIdxObjCProtocolRefListInfo> $allocate(
8889+
ffi.Allocator $allocator, {
8890+
required ffi.Pointer<ffi.Pointer<CXIdxObjCProtocolRefInfo>> protocols,
8891+
required int numProtocols,
8892+
}) => $allocator<CXIdxObjCProtocolRefListInfo>()
8893+
..ref.protocols = protocols
8894+
..ref.numProtocols = numProtocols;
88208895
}
88218896

88228897
/// Visitor invoked for each file in a translation unit
@@ -9134,6 +9209,14 @@ final class CXSourceRangeList extends ffi.Struct {
91349209

91359210
/// An array of \c CXSourceRanges.
91369211
external ffi.Pointer<CXSourceRange> ranges;
9212+
9213+
static ffi.Pointer<CXSourceRangeList> $allocate(
9214+
ffi.Allocator $allocator, {
9215+
required int count,
9216+
required ffi.Pointer<CXSourceRange> ranges,
9217+
}) => $allocator<CXSourceRangeList>()
9218+
..ref.count = count
9219+
..ref.ranges = ranges;
91379220
}
91389221

91399222
/// A character string.
@@ -9147,13 +9230,29 @@ final class CXString extends ffi.Struct {
91479230

91489231
@ffi.UnsignedInt()
91499232
external int private_flags;
9233+
9234+
static ffi.Pointer<CXString> $allocate(
9235+
ffi.Allocator $allocator, {
9236+
required ffi.Pointer<ffi.Void> data,
9237+
required int private_flags,
9238+
}) => $allocator<CXString>()
9239+
..ref.data = data
9240+
..ref.private_flags = private_flags;
91509241
}
91519242

91529243
final class CXStringSet extends ffi.Struct {
91539244
external ffi.Pointer<CXString> Strings;
91549245

91559246
@ffi.UnsignedInt()
91569247
external int Count;
9248+
9249+
static ffi.Pointer<CXStringSet> $allocate(
9250+
ffi.Allocator $allocator, {
9251+
required ffi.Pointer<CXString> Strings,
9252+
required int Count,
9253+
}) => $allocator<CXStringSet>()
9254+
..ref.Strings = Strings
9255+
..ref.Count = Count;
91579256
}
91589257

91599258
/// Roles that are attributed to symbol occurrences.
@@ -9216,6 +9315,16 @@ final class CXTUResourceUsage extends ffi.Struct {
92169315
external int numEntries;
92179316

92189317
external ffi.Pointer<CXTUResourceUsageEntry> entries;
9318+
9319+
static ffi.Pointer<CXTUResourceUsage> $allocate(
9320+
ffi.Allocator $allocator, {
9321+
required ffi.Pointer<ffi.Void> data,
9322+
required int numEntries,
9323+
required ffi.Pointer<CXTUResourceUsageEntry> entries,
9324+
}) => $allocator<CXTUResourceUsage>()
9325+
..ref.data = data
9326+
..ref.numEntries = numEntries
9327+
..ref.entries = entries;
92199328
}
92209329

92219330
final class CXTUResourceUsageEntry extends ffi.Struct {
@@ -9227,6 +9336,14 @@ final class CXTUResourceUsageEntry extends ffi.Struct {
92279336

92289337
@ffi.UnsignedLong()
92299338
external int amount;
9339+
9340+
static ffi.Pointer<CXTUResourceUsageEntry> $allocate(
9341+
ffi.Allocator $allocator, {
9342+
required CXTUResourceUsageKind kind,
9343+
required int amount,
9344+
}) => $allocator<CXTUResourceUsageEntry>()
9345+
..ref.kind = kind
9346+
..ref.amount = amount;
92309347
}
92319348

92329349
/// Categorizes how memory is being used by a translation unit.
@@ -9689,6 +9806,16 @@ final class CXUnsavedFile extends ffi.Struct {
96899806
/// The length of the unsaved contents of this buffer.
96909807
@ffi.UnsignedLong()
96919808
external int Length;
9809+
9810+
static ffi.Pointer<CXUnsavedFile> $allocate(
9811+
ffi.Allocator $allocator, {
9812+
required ffi.Pointer<ffi.Char> Filename,
9813+
required ffi.Pointer<ffi.Char> Contents,
9814+
required int Length,
9815+
}) => $allocator<CXUnsavedFile>()
9816+
..ref.Filename = Filename
9817+
..ref.Contents = Contents
9818+
..ref.Length = Length;
96929819
}
96939820

96949821
/// Describes a version number of the form major.minor.subminor.
@@ -9709,6 +9836,16 @@ final class CXVersion extends ffi.Struct {
97099836
/// e.g., in version '10' or '10.7'.
97109837
@ffi.Int()
97119838
external int Subminor;
9839+
9840+
static ffi.Pointer<CXVersion> $allocate(
9841+
ffi.Allocator $allocator, {
9842+
required int Major,
9843+
required int Minor,
9844+
required int Subminor,
9845+
}) => $allocator<CXVersion>()
9846+
..ref.Major = Major
9847+
..ref.Minor = Minor
9848+
..ref.Subminor = Subminor;
97129849
}
97139850

97149851
enum CXVisibilityKind {
@@ -9882,6 +10019,82 @@ final class IndexerCallbacks extends ffi.Struct {
988210019
>
988310020
>
988410021
indexEntityReference;
10022+
10023+
static ffi.Pointer<IndexerCallbacks> $allocate(
10024+
ffi.Allocator $allocator, {
10025+
required ffi.Pointer<
10026+
ffi.NativeFunction<
10027+
ffi.Int Function(
10028+
CXClientData client_data,
10029+
ffi.Pointer<ffi.Void> reserved,
10030+
)
10031+
>
10032+
>
10033+
abortQuery,
10034+
required ffi.Pointer<
10035+
ffi.NativeFunction<
10036+
ffi.Void Function(CXClientData, CXDiagnosticSet, ffi.Pointer<ffi.Void>)
10037+
>
10038+
>
10039+
diagnostic,
10040+
required ffi.Pointer<
10041+
ffi.NativeFunction<
10042+
CXIdxClientFile Function(
10043+
CXClientData client_data,
10044+
CXFile mainFile,
10045+
ffi.Pointer<ffi.Void> reserved,
10046+
)
10047+
>
10048+
>
10049+
enteredMainFile,
10050+
required ffi.Pointer<
10051+
ffi.NativeFunction<
10052+
CXIdxClientFile Function(
10053+
CXClientData,
10054+
ffi.Pointer<CXIdxIncludedFileInfo>,
10055+
)
10056+
>
10057+
>
10058+
ppIncludedFile,
10059+
required ffi.Pointer<
10060+
ffi.NativeFunction<
10061+
CXIdxClientASTFile Function(
10062+
CXClientData,
10063+
ffi.Pointer<CXIdxImportedASTFileInfo>,
10064+
)
10065+
>
10066+
>
10067+
importedASTFile,
10068+
required ffi.Pointer<
10069+
ffi.NativeFunction<
10070+
CXIdxClientContainer Function(
10071+
CXClientData client_data,
10072+
ffi.Pointer<ffi.Void> reserved,
10073+
)
10074+
>
10075+
>
10076+
startedTranslationUnit,
10077+
required ffi.Pointer<
10078+
ffi.NativeFunction<
10079+
ffi.Void Function(CXClientData, ffi.Pointer<CXIdxDeclInfo>)
10080+
>
10081+
>
10082+
indexDeclaration,
10083+
required ffi.Pointer<
10084+
ffi.NativeFunction<
10085+
ffi.Void Function(CXClientData, ffi.Pointer<CXIdxEntityRefInfo>)
10086+
>
10087+
>
10088+
indexEntityReference,
10089+
}) => $allocator<IndexerCallbacks>()
10090+
..ref.abortQuery = abortQuery
10091+
..ref.diagnostic = diagnostic
10092+
..ref.enteredMainFile = enteredMainFile
10093+
..ref.ppIncludedFile = ppIncludedFile
10094+
..ref.importedASTFile = importedASTFile
10095+
..ref.startedTranslationUnit = startedTranslationUnit
10096+
..ref.indexDeclaration = indexDeclaration
10097+
..ref.indexEntityReference = indexEntityReference;
988510098
}
988610099

988710100
typedef NativeClang_CXCursorSet_contains =

‎pkgs/ffigen/example/shared_bindings/lib/generated/a_shared_b_gen.dart‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ const int A_MACRO_1 = 1;
101101
final class A_Struct1 extends ffi.Struct {
102102
@ffi.Int()
103103
external int a;
104+
105+
static ffi.Pointer<A_Struct1> $allocate(
106+
ffi.Allocator $allocator, {
107+
required int a,
108+
}) => $allocator<A_Struct1>()..ref.a = a;
104109
}
105110

106111
final class A_Union1 extends ffi.Union {

0 commit comments

Comments
 (0)