Skip to content

Commit 8f1f806

Browse files
authored
[ffigen] Improve docs of config API (#2774)
1 parent dea1bf3 commit 8f1f806

File tree

2 files changed

+92
-18
lines changed

2 files changed

+92
-18
lines changed

pkgs/ffigen/lib/src/config_provider/config.dart

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ final class Headers {
139139

140140
static bool _includeDefault(Uri header) => true;
141141

142-
/// CommandLine Arguments to pass to clang_compiler.
142+
/// Command line arguments to pass to clang_compiler.
143143
final List<String>? compilerOptions;
144144

145145
/// Where to ignore compiler warnings/errors in source header files.
@@ -155,7 +155,12 @@ final class Headers {
155155

156156
/// Configuration for declarations.
157157
final class Declarations {
158-
/// Checks if a name is allowed by a filter.
158+
/// Whether to include the given declaration.
159+
///
160+
/// ```dart
161+
/// // This includes `Foo`, and nothing else:
162+
/// include: (Declaration decl) => decl.originalName == 'Foo'
163+
/// ```
159164
final bool Function(Declaration declaration) include;
160165

161166
/// A function to pass to [include] that excludes all declarations.
@@ -169,10 +174,21 @@ final class Declarations {
169174
static bool Function(Declaration) includeSet(Set<String> names) =>
170175
(Declaration decl) => names.contains(decl.originalName);
171176

172-
/// Whether a member of a declaration should be included.
177+
/// Whether the member of the declaration should be included.
173178
///
174179
/// Only used for [Categories], [Interfaces], and [Protocols] methods and
175-
/// properties.
180+
/// properties. For Objective-C methods, this is the method selector, eg
181+
/// `"arrayWithObjects:count:"`.
182+
///
183+
/// Note that using [includeMember] to include a member of a class doesn't
184+
/// affect whether the class is included. You'll also need to set [include]
185+
/// for the class (this will be fixed in a future version of the API).
186+
///
187+
/// ```dart
188+
/// // This includes `Foo.bar`, and no other methods of `Foo`:
189+
/// includeMember: (Declaration declaration, String member) =>
190+
/// ```
191+
// TODO(https://github.com/dart-lang/native/issues/2770): Merge with include.
176192
final bool Function(Declaration declaration, String member) includeMember;
177193

178194
/// A function to pass to [includeMember] that includes all members of all
@@ -190,10 +206,18 @@ final class Declarations {
190206
(Declaration decl, String member) =>
191207
members[decl.originalName]?.contains(member) ?? true;
192208

193-
/// Checks if the symbol address should be included for this name.
209+
/// Whether the symbol address should be exposed for this declaration.
210+
///
211+
/// The address is exposed as an FFI pointer.
194212
final bool Function(Declaration declaration) includeSymbolAddress;
195213

196-
/// Applies renaming and returns the result.
214+
/// Returns a new name for the declaration, to replace its `originalName`.
215+
///
216+
/// ```dart
217+
/// // This renames `Foo` to `Bar`, and nothing else:
218+
/// rename: (Declaration decl) =>
219+
/// decl.originalName == 'Foo' ? 'Bar' : decl.originalName
220+
/// ```
197221
final String Function(Declaration declaration) rename;
198222

199223
/// A function to pass to [rename] that doesn't rename the declaration.
@@ -211,9 +235,21 @@ final class Declarations {
211235
(Declaration declaration) =>
212236
renames[declaration.originalName] ?? declaration.originalName;
213237

214-
/// Applies member renaming and returns the result. Used for struct/union
215-
/// fields, enum elements, function params, and ObjC
216-
/// interface/protocol/category methods/properties.
238+
/// Returns a new name for the member of the declaration, to replace its
239+
/// `originalName`.
240+
///
241+
/// Used for struct/union fields, enum elements, function params, and
242+
/// Objective-C interface/protocol/category methods/properties.
243+
///
244+
/// ```dart
245+
/// // This renames `Foo.bar` to `Foo.baz`, and nothing else:
246+
/// rename: (Declaration decl, String member) {
247+
/// if (decl.originalName == 'Foo' && member == 'baz') {
248+
/// return 'baz';
249+
/// }
250+
/// return member;
251+
/// }
252+
/// ```
217253
final String Function(Declaration declaration, String member) renameMember;
218254

219255
/// A function to pass to [renameMember] that doesn't rename the member.
@@ -246,8 +282,18 @@ final class Enums extends Declarations {
246282
/// The [EnumStyle] to use for the given enum declaration.
247283
///
248284
/// The `suggestedStyle` is a suggested [EnumStyle] based on the declaration
249-
/// of the enum, if any. For example, ObjC enums declared using NS_OPTIONS
250-
/// are suggested to use [EnumStyle.intConstants].
285+
/// of the enum, if any. For example, Objective-C enums declared using
286+
/// NS_OPTIONS are suggested to use [EnumStyle.intConstants].
287+
///
288+
/// ```dart
289+
/// // This uses `intConstants` for `Foo`, and the default style otherwise:
290+
/// style: (Declaration decl, EnumStyle? suggestedStyle) {
291+
/// if (decl.originalName == 'Foo') {
292+
/// return EnumStyle.intConstants;
293+
/// }
294+
/// return suggestedStyle ?? EnumStyle.dartEnum;
295+
/// }
296+
/// ```
251297
final EnumStyle Function(Declaration declaration, EnumStyle? suggestedStyle)
252298
style;
253299

@@ -288,17 +334,26 @@ enum EnumStyle {
288334

289335
/// Configuration for function declarations.
290336
final class Functions extends Declarations {
291-
/// Whether to expose the function typedef for a given function.
337+
/// Whether to generate a typedef for a given function's native type.
292338
final bool Function(Declaration declaration) includeTypedef;
293339

294340
static bool _includeTypedefDefault(Declaration declaration) => false;
295341

296342
/// Whether the given function is a leaf function.
343+
///
344+
/// This corresponds to the `isLeaf` parameter of FFI's `lookupFunction`.
345+
/// For more details, its documentation is here:
346+
/// https://api.dart.dev/dart-ffi/DynamicLibraryExtension/lookupFunction.html
297347
final bool Function(Declaration declaration) isLeaf;
298348

299349
static bool _isLeafDefault(Declaration declaration) => false;
300350

301-
/// VarArg function handling.
351+
/// Map from function's original name to [VarArgFunction]s.
352+
///
353+
/// Dart doesn't support variadic functions. Instead, variadic functions are
354+
/// handled by generating multiple versions of the same function, with
355+
/// different signatures. Each [VarArgFunction] represents one of those
356+
/// signatures.
302357
final Map<String, List<VarArgFunction>> varArgs;
303358

304359
const Functions({
@@ -491,11 +546,11 @@ final class ObjectiveC {
491546
/// Declaration filters for Objective-C protocols.
492547
final Protocols protocols;
493548

494-
/// Undocumented option that changes code generation for package:objective_c.
495-
/// The main difference is whether NSObject etc are imported from
496-
/// package:objective_c (the default) or code genned like any other class.
497-
/// This is necessary because package:objective_c can't import NSObject from
498-
/// itself.
549+
// Undocumented option that changes code generation for package:objective_c.
550+
// The main difference is whether NSObject etc are imported from
551+
// package:objective_c (the default) or code genned like any other class.
552+
// This is necessary because package:objective_c can't import NSObject from
553+
// itself.
499554
@Deprecated('Only for internal use.')
500555
final bool generateForPackageObjectiveC;
501556

pkgs/ffigen/lib/src/config_provider/config_types.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,13 @@ class RawVarArgFunction {
449449
RawVarArgFunction(this.postfix, this.rawTypeStrings);
450450
}
451451

452+
/// A specialization of a variadic function with specific argument types.
452453
class VarArgFunction {
454+
/// A suffix to append to the function name for this variant.
453455
final String postfix;
456+
457+
/// The types that will passed as the variadic parameters, replacing the
458+
/// `...` in the original definition.
454459
final List<Type> types;
455460

456461
VarArgFunction(this.postfix, this.types);
@@ -461,9 +466,23 @@ class PackingValue {
461466
PackingValue(this.value);
462467
}
463468

469+
/// A declaration, such as a function or a class.
464470
class Declaration {
471+
/// A unique identifier for the declaration.
472+
///
473+
/// USR stands for Unified Symbol Resolution. It is an ID generated by clang
474+
/// that is designed to be unique, and stable across compilations, but not
475+
/// human readable.
476+
///
477+
/// It's usually easiest to filter the declaration by the [originalName]. But
478+
/// the name alone might not be unique. If you have two different declarations
479+
/// with the same [originalName], log their [usr]s, and use that to make your
480+
/// filtering more specific.
465481
final String usr;
482+
483+
/// The original name of the declaration in source code, before any renaming.
466484
final String originalName;
485+
467486
Declaration({required this.usr, required this.originalName});
468487
}
469488

0 commit comments

Comments
 (0)