Skip to content
Draft
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
26 changes: 15 additions & 11 deletions pkgs/ffigen/lib/src/code_generator/compound.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import '../code_generator.dart';
import '../context.dart';
import '../visitor/ast.dart';

import 'binding_string.dart';
Expand Down Expand Up @@ -36,7 +37,7 @@ abstract class Compound extends BindingType {
bool get isStruct => compoundType == CompoundType.struct;
bool get isUnion => compoundType == CompoundType.union;

ObjCBuiltInFunctions? objCBuiltInFunctions;
final Context context;

/// The way the native type is written in C source code. This isn't always the
/// same as the originalName, because the type may need to be prefixed with
Expand All @@ -53,7 +54,7 @@ abstract class Compound extends BindingType {
super.dartDoc,
List<CompoundMember>? members,
super.isInternal,
this.objCBuiltInFunctions,
required this.context,
String? nativeType,
}) : members = members ?? [],
nativeType = nativeType ?? originalName ?? name;
Expand All @@ -67,7 +68,7 @@ abstract class Compound extends BindingType {
int? pack,
String? dartDoc,
List<CompoundMember>? members,
ObjCBuiltInFunctions? objCBuiltInFunctions,
required Context context,
String? nativeType,
}) {
switch (type) {
Expand All @@ -80,7 +81,7 @@ abstract class Compound extends BindingType {
pack: pack,
dartDoc: dartDoc,
members: members,
objCBuiltInFunctions: objCBuiltInFunctions,
context: context,
nativeType: nativeType,
);
case CompoundType.union:
Expand All @@ -92,23 +93,23 @@ abstract class Compound extends BindingType {
pack: pack,
dartDoc: dartDoc,
members: members,
objCBuiltInFunctions: objCBuiltInFunctions,
context: context,
nativeType: nativeType,
);
}
}

String _getInlineArrayTypeString(Type type, Writer w) {
if (type is ConstantArray) {
return '${w.ffiLibraryPrefix}.Array<'
return '${context.libs.prefix(ffiImport)}.Array<'
'${_getInlineArrayTypeString(type.child, w)}>';
}
return type.getCType(w);
}

@override
bool get isObjCImport =>
objCBuiltInFunctions?.getBuiltInCompoundName(originalName) != null;
context.objCBuiltInFunctions.getBuiltInCompoundName(originalName) != null;

@override
BindingString toBindingString(Writer w) {
Expand All @@ -131,13 +132,14 @@ abstract class Compound extends BindingType {
}

/// Write @Packed(X) annotation if struct is packed.
final ffiPrefix = context.libs.prefix(ffiImport);
if (isStruct && pack != null) {
s.write('@${w.ffiLibraryPrefix}.Packed($pack)\n');
s.write('@$ffiPrefix.Packed($pack)\n');
}
final dartClassName = isStruct ? 'Struct' : 'Union';
// Write class declaration.
s.write('final class $enclosingClassName extends ');
s.write('${w.ffiLibraryPrefix}.${isOpaque ? 'Opaque' : dartClassName}{\n');
s.write('$ffiPrefix.${isOpaque ? 'Opaque' : dartClassName}{\n');
const depth = ' ';
for (final m in members) {
m.name = localUniqueNamer.makeUnique(m.name);
Expand Down Expand Up @@ -180,10 +182,12 @@ abstract class Compound extends BindingType {

@override
String getCType(Writer w) {
final builtInName = objCBuiltInFunctions?.getBuiltInCompoundName(
final builtInName = context.objCBuiltInFunctions.getBuiltInCompoundName(
originalName,
);
return builtInName != null ? '${w.objcPkgPrefix}.$builtInName' : name;
return builtInName != null
? '${context.libs.prefix(objcPkgImport)}.$builtInName'
: name;
}

@override
Expand Down
10 changes: 5 additions & 5 deletions pkgs/ffigen/lib/src/code_generator/enum_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import 'package:collection/collection.dart';

import '../context.dart';
import '../visitor/ast.dart';

import 'binding_string.dart';
import 'imports.dart';
import 'objc_built_in_functions.dart';
import 'type.dart';
import 'unique_namer.dart';
import 'utils.dart';
Expand Down Expand Up @@ -52,7 +52,7 @@ class EnumClass extends BindingType {
/// Generates new names for all members that don't equal [name].
final UniqueNamer namer;

ObjCBuiltInFunctions? objCBuiltInFunctions;
Context context;

/// Whether this enum should be generated as a collection of integers.
bool generateAsInt;
Expand All @@ -64,7 +64,7 @@ class EnumClass extends BindingType {
super.dartDoc,
Type? nativeType,
List<EnumConstant>? enumConstants,
this.objCBuiltInFunctions,
required this.context,
this.generateAsInt = false,
}) : nativeType = nativeType ?? intType,
enumConstants = enumConstants ?? [],
Expand Down Expand Up @@ -232,7 +232,7 @@ class EnumClass extends BindingType {

@override
bool get isObjCImport =>
objCBuiltInFunctions?.isBuiltInEnum(originalName) ?? false;
context.objCBuiltInFunctions.isBuiltInEnum(originalName);

@override
BindingString toBindingString(Writer w) {
Expand Down Expand Up @@ -275,7 +275,7 @@ class EnumClass extends BindingType {
@override
String getDartType(Writer w) {
if (isObjCImport) {
return '${w.objcPkgPrefix}.$name';
return '${context.libs.prefix(objcPkgImport)}.$name';
} else if (generateAsInt) {
return nativeType.getDartType(w);
} else {
Expand Down
10 changes: 5 additions & 5 deletions pkgs/ffigen/lib/src/code_generator/func.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ $dartReturnType $enclosingFuncName($dartArgDeclString) => $funcImplCall;
// Add to SymbolAddress in writer.
w.symbolAddressWriter.addNativeSymbol(
type:
'${w.ffiLibraryPrefix}.Pointer<'
'${w.ffiLibraryPrefix}.NativeFunction<$cType>>',
'${w.context.libs.prefix(ffiImport)}.Pointer<'
'${w.context.libs.prefix(ffiImport)}.NativeFunction<$cType>>',
name: name,
);
}
Expand All @@ -196,8 +196,8 @@ $dartReturnType $enclosingFuncName($dartArgDeclString) {
// Add to SymbolAddress in writer.
w.symbolAddressWriter.addSymbol(
type:
'${w.ffiLibraryPrefix}.Pointer<'
'${w.ffiLibraryPrefix}.NativeFunction<$cType>>',
'${w.context.libs.prefix(ffiImport)}.Pointer<'
'${w.context.libs.prefix(ffiImport)}.NativeFunction<$cType>>',
name: name,
ptrName: funcPointerName,
);
Expand All @@ -207,7 +207,7 @@ $dartReturnType $enclosingFuncName($dartArgDeclString) {
final lookupStr = UniqueNamer.stringLiteral(_lookupName);
s.write('''
late final $funcPointerName = ${w.lookupFuncIdentifier}<
${w.ffiLibraryPrefix}.NativeFunction<$cType>>('$lookupStr');
${w.context.libs.prefix(ffiImport)}.NativeFunction<$cType>>('$lookupStr');
late final $funcVarName = $funcPointerName.asFunction<$dartType>($isLeafString);

''');
Expand Down
4 changes: 2 additions & 2 deletions pkgs/ffigen/lib/src/code_generator/func_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class FunctionType extends Type {
String getCType(Writer w, {bool writeArgumentNames = true}) => _getTypeImpl(
writeArgumentNames,
(Type t) => t.getCType(w),
varArgWrapper: '${w.ffiLibraryPrefix}.VarArgs',
varArgWrapper: '${w.context.libs.prefix(ffiImport)}.VarArgs',
);

@override
Expand Down Expand Up @@ -167,7 +167,7 @@ class NativeFunc extends Type {
final funcType = _type is FunctionType
? _type.getCType(w, writeArgumentNames: writeArgumentNames)
: _type.getCType(w);
return '${w.ffiLibraryPrefix}.NativeFunction<$funcType>';
return '${w.context.libs.prefix(ffiImport)}.NativeFunction<$funcType>';
}

@override
Expand Down
14 changes: 7 additions & 7 deletions pkgs/ffigen/lib/src/code_generator/global.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import '../visitor/ast.dart';
import 'binding.dart';
import 'binding_string.dart';
import 'compound.dart';
import 'imports.dart';
import 'pointer.dart';
import 'type.dart';
import 'utils.dart';
Expand Down Expand Up @@ -54,6 +55,8 @@ class Global extends LookUpBinding {
? (type as ConstantArray).child.getCType(w)
: type.getCType(w);

final ptrType = '${w.context.libs.prefix(ffiImport)}.Pointer<$cType>';

void generateConvertingGetterAndSetter(String pointerValue) {
final getValue = type.convertFfiDartTypeToDartType(
w,
Expand Down Expand Up @@ -111,25 +114,22 @@ class Global extends LookUpBinding {
}

if (exposeSymbolAddress) {
w.symbolAddressWriter.addNativeSymbol(
type: '${w.ffiLibraryPrefix}.Pointer<$cType>',
name: name,
);
w.symbolAddressWriter.addNativeSymbol(type: ptrType, name: name);
}
} else {
final pointerName = w.wrapperLevelUniqueNamer.makeUnique(
'_$globalVarName',
);

s.write(
'late final ${w.ffiLibraryPrefix}.Pointer<$cType> $pointerName = '
'late final $ptrType $pointerName = '
"${w.lookupFuncIdentifier}<$cType>('$originalName');\n\n",
);
final baseTypealiasType = type.typealiasType;
if (baseTypealiasType is Compound) {
if (baseTypealiasType.isOpaque) {
s.write(
'${w.ffiLibraryPrefix}.Pointer<$cType> get $globalVarName =>'
'$ptrType get $globalVarName =>'
' $pointerName;\n\n',
);
} else {
Expand All @@ -152,7 +152,7 @@ class Global extends LookUpBinding {
if (exposeSymbolAddress) {
// Add to SymbolAddress in writer.
w.symbolAddressWriter.addSymbol(
type: '${w.ffiLibraryPrefix}.Pointer<$cType>',
type: ptrType,
name: name,
ptrName: pointerName,
);
Expand Down
2 changes: 1 addition & 1 deletion pkgs/ffigen/lib/src/code_generator/handle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class HandleType extends Type {
factory HandleType() => _handle;

@override
String getCType(Writer w) => '${w.ffiLibraryPrefix}.Handle';
String getCType(Writer w) => '${w.context.libs.prefix(ffiImport)}.Handle';

@override
String getFfiDartType(Writer w) => 'Object';
Expand Down
28 changes: 12 additions & 16 deletions pkgs/ffigen/lib/src/code_generator/imports.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ class LibraryImport extends AstNode {
final String _importPath;
final String? _importPathWhenImportedByPackageObjC;

String prefix;

LibraryImport(
const LibraryImport(
this.name,
this._importPath, {
String? importPathWhenImportedByPackageObjC,
}) : _importPathWhenImportedByPackageObjC =
importPathWhenImportedByPackageObjC,
prefix = name;
importPathWhenImportedByPackageObjC;

@override
bool operator ==(Object other) {
Expand Down Expand Up @@ -60,16 +57,12 @@ class ImportedType extends Type {
});

@override
String getCType(Writer w) {
w.markImportUsed(libraryImport);
return '${libraryImport.prefix}.$cType';
}
String getCType(Writer w) => '${w.context.libs.prefix(libraryImport)}.$cType';

@override
String getFfiDartType(Writer w) {
if (importedDartType) {
w.markImportUsed(libraryImport);
return '${libraryImport.prefix}.$dartType';
return '${w.context.libs.prefix(libraryImport)}.$dartType';
} else {
return cType == dartType ? getCType(w) : dartType;
}
Expand Down Expand Up @@ -116,15 +109,18 @@ class SelfImportedType extends Type {
String toString() => cType;
}

final ffiImport = LibraryImport('ffi', 'dart:ffi');
final ffiPkgImport = LibraryImport('pkg_ffi', 'package:ffi/ffi.dart');
final objcPkgImport = LibraryImport(
const ffiImport = LibraryImport('ffi', 'dart:ffi');
const ffiPkgImport = LibraryImport('pkg_ffi', 'package:ffi/ffi.dart');
const objcPkgImport = LibraryImport(
'objc',
'package:objective_c/objective_c.dart',
importPathWhenImportedByPackageObjC: '../objective_c.dart',
);
final self = LibraryImport('self', '');
final allLibraries = [ffiImport, ffiPkgImport, objcPkgImport, self];
const selfImport = LibraryImport('self', '');
final builtInLibraries = {
for (final l in [ffiImport, ffiPkgImport, objcPkgImport, selfImport])
l.name: l,
};

final voidType = ImportedType(ffiImport, 'Void', 'void', 'void');

Expand Down
2 changes: 1 addition & 1 deletion pkgs/ffigen/lib/src/code_generator/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Library {
className: name,
classDocComment: description,
header: header,
additionalImports: libraryImports,
additionalImports: libraryImports.map(context.libs.canonicalize).toList(),
generateForPackageObjectiveC: generateForPackageObjectiveC,
silenceEnumWarning: silenceEnumWarning,
nativeEntryPoints: nativeEntryPoints,
Expand Down
2 changes: 1 addition & 1 deletion pkgs/ffigen/lib/src/code_generator/native_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class NativeType extends Type {
factory NativeType(SupportedNativeType type) => _primitives[type]!;

@override
String getCType(Writer w) => '${w.ffiLibraryPrefix}.$_cType';
String getCType(Writer w) => '${w.context.libs.prefix(ffiImport)}.$_cType';

@override
String getFfiDartType(Writer w) => _dartType;
Expand Down
8 changes: 5 additions & 3 deletions pkgs/ffigen/lib/src/code_generator/objc_block.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,15 @@ class ObjCBlock extends BindingType {
final blockType = _blockType(w);
final defaultValue = returnType.getDefaultValue(w);
final exceptionalReturn = defaultValue == null ? '' : ', $defaultValue';
final ffiPrefix = w.context.libs.prefix(ffiImport);

// Write the function pointer based trampoline function.
s.write('''
$returnFfiDartType $funcPtrTrampoline(
$blockCType block, ${func.paramsFfiDartType}) =>
block.ref.target.cast<${func.natFnFfiDartType}>()
.asFunction<${func.ffiDartType}>()(${func.paramsNameOnly});
$voidPtrCType $funcPtrCallable = ${w.ffiLibraryPrefix}.Pointer.fromFunction<
$voidPtrCType $funcPtrCallable = $ffiPrefix.Pointer.fromFunction<
${func.trampCType}>($funcPtrTrampoline $exceptionalReturn).cast();
''');

Expand All @@ -190,7 +191,7 @@ $voidPtrCType $funcPtrCallable = ${w.ffiLibraryPrefix}.Pointer.fromFunction<
$returnFfiDartType $closureTrampoline(
$blockCType block, ${func.paramsFfiDartType}) =>
($getBlockClosure(block) as ${func.ffiDartType})(${func.paramsNameOnly});
$voidPtrCType $closureCallable = ${w.ffiLibraryPrefix}.Pointer.fromFunction<
$voidPtrCType $closureCallable = $ffiPrefix.Pointer.fromFunction<
${func.trampCType}>($closureTrampoline $exceptionalReturn).cast();
''');

Expand Down Expand Up @@ -597,7 +598,8 @@ class _FnHelper {
);
trampCType = trampFnType.getCType(w, writeArgumentNames: false);
trampFfiDartType = trampFnType.getFfiDartType(w, writeArgumentNames: false);
trampNatCallType = '${w.ffiLibraryPrefix}.NativeCallable<$trampCType>';
trampNatCallType =
'${w.context.libs.prefix(ffiImport)}.NativeCallable<$trampCType>';
trampNatFnCType = NativeFunc(trampFnType).getCType(w);

paramsNameOnly = params.map((p) => p.name).join(', ');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class ObjCImport {

const ObjCImport(this.name);

String gen(Writer w) => '${w.objcPkgPrefix}.$name';
String gen(Writer w) => '${w.context.libs.prefix(objcPkgImport)}.$name';
}

/// Globals only used internally by ObjC bindings, such as classes and SELs.
Expand Down
2 changes: 1 addition & 1 deletion pkgs/ffigen/lib/src/code_generator/objc_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ ${generateInstanceMethodBindings(w, this)}

@override
String getDartType(Writer w) =>
isObjCImport ? '${w.objcPkgPrefix}.$name' : name;
isObjCImport ? '${context.libs.prefix(objcPkgImport)}.$name' : name;

@override
String getNativeType({String varName = ''}) => 'id $varName';
Expand Down
Loading
Loading