From f7debe58f00fff87ce3cf9993e08fc04d47925ce Mon Sep 17 00:00:00 2001 From: Liam Appelbe Date: Mon, 9 Oct 2023 13:48:25 +1300 Subject: [PATCH] Refactor `sameFfiDartAndCType` to not require a `Writer` --- lib/src/code_generator/compound.dart | 5 ++++- lib/src/code_generator/enum_class.dart | 6 ++++++ lib/src/code_generator/func_type.dart | 15 +++++++++++++++ lib/src/code_generator/handle.dart | 3 +++ lib/src/code_generator/imports.dart | 6 ++++++ lib/src/code_generator/native_type.dart | 3 +++ lib/src/code_generator/objc_block.dart | 6 ++++++ lib/src/code_generator/objc_interface.dart | 6 ++++++ lib/src/code_generator/objc_nullable.dart | 6 ++++++ lib/src/code_generator/pointer.dart | 7 +++++++ lib/src/code_generator/type.dart | 15 ++++++++++++--- lib/src/code_generator/typealias.dart | 8 +++++++- 12 files changed, 81 insertions(+), 5 deletions(-) diff --git a/lib/src/code_generator/compound.dart b/lib/src/code_generator/compound.dart index c3eb55e9..ddc33c64 100644 --- a/lib/src/code_generator/compound.dart +++ b/lib/src/code_generator/compound.dart @@ -145,7 +145,7 @@ abstract class Compound extends BindingType { s.write('${depth}external ${_getInlineArrayTypeString(m.type, w)} '); s.write('${m.name};\n\n'); } else { - if (!sameDartAndCType(m.type, w)) { + if (!m.type.sameFfiDartAndCType) { s.write('$depth@${m.type.getCType(w)}()\n'); } s.write('${depth}external ${m.type.getFfiDartType(w)} ${m.name};\n\n'); @@ -173,6 +173,9 @@ abstract class Compound extends BindingType { @override String getCType(Writer w) => name; + + @override + bool get sameFfiDartAndCType => true; } class Member { diff --git a/lib/src/code_generator/enum_class.dart b/lib/src/code_generator/enum_class.dart index 70d142b6..089d7ed6 100644 --- a/lib/src/code_generator/enum_class.dart +++ b/lib/src/code_generator/enum_class.dart @@ -86,6 +86,12 @@ class EnumClass extends BindingType { @override String getFfiDartType(Writer w) => nativeType.getFfiDartType(w); + @override + bool get sameFfiDartAndCType => nativeType.sameFfiDartAndCType; + + @override + bool get sameDartAndCType => nativeType.sameDartAndCType; + @override String? getDefaultValue(Writer w, String nativeLib) => '0'; } diff --git a/lib/src/code_generator/func_type.dart b/lib/src/code_generator/func_type.dart index 3ee17d59..e1d07936 100644 --- a/lib/src/code_generator/func_type.dart +++ b/lib/src/code_generator/func_type.dart @@ -81,6 +81,18 @@ class FunctionType extends Type { return sb.toString(); } + @override + bool get sameFfiDartAndCType => + returnType.sameFfiDartAndCType && + parameters.every((p) => p.type.sameFfiDartAndCType) && + varArgParameters.every((p) => p.type.sameFfiDartAndCType); + + @override + bool get sameDartAndCType => + returnType.sameDartAndCType && + parameters.every((p) => p.type.sameDartAndCType) && + varArgParameters.every((p) => p.type.sameDartAndCType); + @override String toString() => _getCacheKeyString(false, (Type t) => t.toString()); @@ -139,6 +151,9 @@ class NativeFunc extends Type { @override String getFfiDartType(Writer w) => getCType(w); + @override + bool get sameFfiDartAndCType => true; + @override String toString() => 'NativeFunction<${_type.toString()}>'; diff --git a/lib/src/code_generator/handle.dart b/lib/src/code_generator/handle.dart index c3817f23..aaa0649e 100644 --- a/lib/src/code_generator/handle.dart +++ b/lib/src/code_generator/handle.dart @@ -18,6 +18,9 @@ class HandleType extends Type { @override String getFfiDartType(Writer w) => 'Object'; + @override + bool get sameFfiDartAndCType => false; + @override String toString() => 'Handle'; } diff --git a/lib/src/code_generator/imports.dart b/lib/src/code_generator/imports.dart index f75a2677..e9dda1cc 100644 --- a/lib/src/code_generator/imports.dart +++ b/lib/src/code_generator/imports.dart @@ -42,6 +42,9 @@ class ImportedType extends Type { @override String getFfiDartType(Writer w) => cType == dartType ? getCType(w) : dartType; + @override + bool get sameFfiDartAndCType => cType == dartType; + @override String toString() => '${libraryImport.name}.$cType'; @@ -64,6 +67,9 @@ class SelfImportedType extends Type { @override String getFfiDartType(Writer w) => dartType; + @override + bool get sameFfiDartAndCType => cType == dartType; + @override String toString() => cType; } diff --git a/lib/src/code_generator/native_type.dart b/lib/src/code_generator/native_type.dart index 19425bab..93ed3561 100644 --- a/lib/src/code_generator/native_type.dart +++ b/lib/src/code_generator/native_type.dart @@ -56,6 +56,9 @@ class NativeType extends Type { @override String getFfiDartType(Writer w) => _dartType; + @override + bool get sameFfiDartAndCType => _cType == _dartType; + @override String toString() => _cType; diff --git a/lib/src/code_generator/objc_block.dart b/lib/src/code_generator/objc_block.dart index 4c82a913..e908abbc 100644 --- a/lib/src/code_generator/objc_block.dart +++ b/lib/src/code_generator/objc_block.dart @@ -225,6 +225,12 @@ class $name extends _ObjCBlockBase { @override String getDartType(Writer w) => name; + @override + bool get sameFfiDartAndCType => true; + + @override + bool get sameDartAndCType => false; + @override String toString() => '($returnType (^)(${argTypes.join(', ')}))'; } diff --git a/lib/src/code_generator/objc_interface.dart b/lib/src/code_generator/objc_interface.dart index 325692ab..421a508d 100644 --- a/lib/src/code_generator/objc_interface.dart +++ b/lib/src/code_generator/objc_interface.dart @@ -389,6 +389,12 @@ class $name extends ${superType?.name ?? '_ObjCWrapper'} { @override String getDartType(Writer w) => name; + @override + bool get sameFfiDartAndCType => true; + + @override + bool get sameDartAndCType => false; + // Utils for converting between the internal types passed to native code, and // the external types visible to the user. For example, ObjCInterfaces are // passed to native as Pointer, but the user sees the Dart wrapper diff --git a/lib/src/code_generator/objc_nullable.dart b/lib/src/code_generator/objc_nullable.dart index 2b080827..db57e5f3 100644 --- a/lib/src/code_generator/objc_nullable.dart +++ b/lib/src/code_generator/objc_nullable.dart @@ -38,6 +38,12 @@ class ObjCNullable extends Type { @override String getDartType(Writer w) => '${child.getDartType(w)}?'; + @override + bool get sameFfiDartAndCType => child.sameFfiDartAndCType; + + @override + bool get sameDartAndCType => false; + @override String toString() => '$child?'; diff --git a/lib/src/code_generator/pointer.dart b/lib/src/code_generator/pointer.dart index f99ee53b..7dfc7d61 100644 --- a/lib/src/code_generator/pointer.dart +++ b/lib/src/code_generator/pointer.dart @@ -31,6 +31,10 @@ class PointerType extends Type { String getCType(Writer w) => '${w.ffiLibraryPrefix}.Pointer<${child.getCType(w)}>'; + // Both the C type and the FFI Dart type are 'Pointer<$cType>'. + @override + bool get sameFfiDartAndCType => true; + @override String toString() => '$child*'; @@ -79,4 +83,7 @@ class ObjCObjectPointer extends PointerType { @override String getDartType(Writer w) => 'NSObject'; + + @override + bool get sameDartAndCType => false; } diff --git a/lib/src/code_generator/type.dart b/lib/src/code_generator/type.dart index 1a69b607..6537a858 100644 --- a/lib/src/code_generator/type.dart +++ b/lib/src/code_generator/type.dart @@ -48,6 +48,12 @@ abstract class Type { /// as getFfiDartType. For ObjC bindings this refers to the wrapper object. String getDartType(Writer w) => getFfiDartType(w); + /// Returns whether the FFI dart type and C type string are same. + bool get sameFfiDartAndCType; + + /// Returns whether the dart type and C type string are same. + bool get sameDartAndCType => sameFfiDartAndCType; + /// Returns the string representation of the Type, for debugging purposes /// only. This string should not be printed as generated code. @override @@ -66,9 +72,6 @@ abstract class Type { String? getDefaultValue(Writer w, String nativeLib) => null; } -/// Function to check if the dart and C type string are same. -bool sameDartAndCType(Type t, Writer w) => t.getCType(w) == t.getFfiDartType(w); - /// Base class for all Type bindings. /// /// Since Dart doesn't have multiple inheritance, this type exists so that we @@ -107,6 +110,9 @@ abstract class BindingType extends NoLookUpBinding implements Type { @override String getDartType(Writer w) => getFfiDartType(w); + @override + bool get sameDartAndCType => sameFfiDartAndCType; + @override String toString() => originalName; @@ -125,4 +131,7 @@ class UnimplementedType extends Type { @override String toString() => '(Unimplemented: $reason)'; + + @override + bool get sameFfiDartAndCType => true; } diff --git a/lib/src/code_generator/typealias.dart b/lib/src/code_generator/typealias.dart index 469a40b6..5c5cc443 100644 --- a/lib/src/code_generator/typealias.dart +++ b/lib/src/code_generator/typealias.dart @@ -121,13 +121,19 @@ class Typealias extends BindingType { String getFfiDartType(Writer w) { // Typealias cannot be used by name in Dart types unless both the C and Dart // type of the underlying types are same. - if (sameDartAndCType(type, w)) { + if (type.sameFfiDartAndCType) { return name; } else { return type.getFfiDartType(w); } } + @override + bool get sameFfiDartAndCType => type.sameFfiDartAndCType; + + @override + bool get sameDartAndCType => type.sameDartAndCType; + @override String cacheKey() => type.cacheKey();