-
Notifications
You must be signed in to change notification settings - Fork 55
Separate getDartType and getUserType #623
Changes from 2 commits
51e4bc7
876fa36
0dc2797
724085d
276f30c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,15 @@ import 'writer.dart'; | |
/// Represents a pointer. | ||
class PointerType extends Type { | ||
final Type child; | ||
PointerType(this.child); | ||
|
||
PointerType._(this.child); | ||
|
||
factory PointerType(Type child) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we maybe have something more general to recognize "internal-dart-types" and return the "user-visible-dart-types"? For example, if we have something more general we could also recognize It should probably be a separate pass. Rather than making a choice point in factory constructors of types. // Parse the bindings according to config object provided.
final library = parse(config);
final libraryTransformed = transform(library);
// Generate file for the parsed bindings.
final gen = File(config.output);
libraryTransformed.generateFile(gen); Or maybe because we already make choices in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the moment we can handle all these cases, including your |
||
if (child == objCObjectType) { | ||
return ObjCObjectPointer(); | ||
} | ||
return PointerType._(child); | ||
} | ||
|
||
@override | ||
void addDependencies(Set<Binding> dependencies) { | ||
|
@@ -33,7 +41,7 @@ class PointerType extends Type { | |
/// Represents a constant array, which has a fixed size. | ||
class ConstantArray extends PointerType { | ||
final int length; | ||
ConstantArray(this.length, Type child) : super(child); | ||
ConstantArray(this.length, Type child) : super._(child); | ||
|
||
@override | ||
Type get baseArrayType => child.baseArrayType; | ||
|
@@ -50,7 +58,7 @@ class ConstantArray extends PointerType { | |
|
||
/// Represents an incomplete array, which has an unknown size. | ||
class IncompleteArray extends PointerType { | ||
IncompleteArray(Type child) : super(child); | ||
IncompleteArray(Type child) : super._(child); | ||
|
||
@override | ||
Type get baseArrayType => child.baseArrayType; | ||
|
@@ -61,3 +69,14 @@ class IncompleteArray extends PointerType { | |
@override | ||
String cacheKey() => '${child.cacheKey()}[]'; | ||
} | ||
|
||
/// A pointer to an NSObject. | ||
class ObjCObjectPointer extends PointerType { | ||
factory ObjCObjectPointer() => _inst; | ||
|
||
static final _inst = ObjCObjectPointer._(); | ||
ObjCObjectPointer._() : super._(objCObjectType); | ||
|
||
@override | ||
String getUserType(Writer w) => 'NSObject'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
import 'package:ffigen/src/code_generator.dart'; | ||
|
||
import '../strings.dart' as strings; | ||
import 'binding_string.dart'; | ||
import 'utils.dart'; | ||
import 'writer.dart'; | ||
|
@@ -40,6 +41,18 @@ class Typealias extends BindingType { | |
isInternal: isInternal, | ||
))); | ||
} | ||
if ((originalName ?? name) == strings.objcInstanceType && | ||
type is ObjCObjectPointer) { | ||
return ObjCInstanceType._( | ||
usr: usr, | ||
originalName: originalName, | ||
dartDoc: dartDoc, | ||
name: name, | ||
type: type, | ||
useDartType: useDartType, | ||
isInternal: isInternal, | ||
); | ||
} | ||
return Typealias._( | ||
usr: usr, | ||
originalName: originalName, | ||
|
@@ -122,3 +135,30 @@ class Typealias extends BindingType { | |
String? getDefaultValue(Writer w, String nativeLib) => | ||
type.getDefaultValue(w, nativeLib); | ||
} | ||
|
||
/// Objective C's instancetype. | ||
/// | ||
/// This is an alias for an NSObject* that is special cased in code generation. | ||
liamappelbe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class ObjCInstanceType extends Typealias { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to populate the enclosing class here? So that the code generator doesn't make the decision but we do it here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so. Not with the existing plumbing. |
||
ObjCInstanceType._({ | ||
String? usr, | ||
String? originalName, | ||
String? dartDoc, | ||
required String name, | ||
required Type type, | ||
|
||
/// If true, the binding string uses Dart type instead of C type. | ||
/// | ||
/// E.g if C type is ffi.Void func(ffi.Int32), Dart type is void func(int). | ||
bool useDartType = false, | ||
bool isInternal = false, | ||
}) : super._( | ||
usr: usr, | ||
originalName: originalName, | ||
dartDoc: dartDoc, | ||
name: name, | ||
type: type, | ||
useDartType: useDartType, | ||
isInternal: isInternal, | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice cleanup!
This special case is the
instancetype
right? The one that returns the type of the object itself even in subclasses.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep