-
Notifications
You must be signed in to change notification settings - Fork 69
[swift2objc] Support Protocols #1832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nikeokoronkwo
wants to merge
18
commits into
dart-lang:main
Choose a base branch
from
nikeokoronkwo:swift2objc-protocols
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,766
−8
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b0c806f
Initial commit for branch
nikeokoronkwo 6f6d50f
implemented parsing basic protocols with inheritance (conformance)
nikeokoronkwo 2370826
Merge branch 'main' into swift2objc-protocols
nikeokoronkwo fa00ec4
added support for associated type
nikeokoronkwo be6b8c8
completed Protocol Declaration parsing
nikeokoronkwo 1e2f855
completed setting up tests
nikeokoronkwo c2c4c6d
removed redundant/completed TODO
nikeokoronkwo 354bb50
Added related issues for dangling TODOs
nikeokoronkwo d75dc8b
Search for superclass and inherit, fallback on NSObject if no superclass
nikeokoronkwo d441639
Added Protocol Transformation and Generation
nikeokoronkwo a312c76
Formatting and Fixing
nikeokoronkwo fae3b99
Merge branch 'main' into swift2objc-protocols
nikeokoronkwo 9b5c264
Removed dangling TODO
nikeokoronkwo e9c299e
Merge branch 'swift2objc-protocols' of https://github.com/nikeokoronk…
nikeokoronkwo ecd7bfd
Formatted files
nikeokoronkwo 4db39a7
Updated TODOs, referencing a pull request
nikeokoronkwo cb34323
Merge branch 'main' into swift2objc-protocols
nikeokoronkwo 478d543
Made changes to protocol
nikeokoronkwo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
pkgs/swift2objc/lib/src/ast/declarations/compounds/members/associated_type_declaration.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import '../../../_core/interfaces/declaration.dart'; | ||
import '../../../_core/shared/referred_type.dart'; | ||
import '../../../ast_node.dart'; | ||
import '../protocol_declaration.dart'; | ||
|
||
class AssociatedTypeDeclaration extends AstNode implements Declaration { | ||
@override | ||
String id; | ||
|
||
@override | ||
String name; | ||
|
||
List<DeclaredType<ProtocolDeclaration>> conformedProtocols; | ||
|
||
AssociatedTypeDeclaration( | ||
{required this.id, required this.name, required this.conformedProtocols}); | ||
} | ||
|
||
extension AsAssociatedType<T extends AssociatedTypeDeclaration> on T { | ||
AssociatedType asType() => AssociatedType( | ||
id: id, name: name, conformedProtocols: conformedProtocols); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
pkgs/swift2objc/lib/src/generator/generators/protocol_generator.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import '../../ast/declarations/built_in/built_in_declaration.dart'; | ||
import '../../ast/declarations/compounds/members/initializer_declaration.dart'; | ||
import '../../ast/declarations/compounds/members/method_declaration.dart'; | ||
import '../../ast/declarations/compounds/members/property_declaration.dart'; | ||
import '../../ast/declarations/compounds/protocol_declaration.dart'; | ||
import '../_core/utils.dart'; | ||
|
||
List<String> generateProtocol(ProtocolDeclaration declaration) { | ||
return [ | ||
'${_generateProtocolHeader(declaration)} {', | ||
...<String>[ | ||
..._generateProtocolProperties(declaration), | ||
..._generateInitializers(declaration), | ||
..._generateProtocolMethods(declaration) | ||
].nonNulls.indent(), | ||
'}\n', | ||
]; | ||
} | ||
|
||
String _generateProtocolHeader(ProtocolDeclaration declaration) { | ||
final header = StringBuffer(); | ||
|
||
if (declaration.hasObjCAnnotation) { | ||
header.write('@objc '); | ||
} | ||
|
||
header.write('public protocol ${declaration.name}'); | ||
|
||
final superClassAndProtocols = [ | ||
...declaration.conformedProtocols | ||
.map((protocol) => protocol.declaration.name), | ||
].nonNulls; | ||
|
||
if (superClassAndProtocols.isNotEmpty) { | ||
header.write(': ${superClassAndProtocols.join(", ")}'); | ||
} | ||
|
||
return header.toString(); | ||
} | ||
|
||
List<String> _generateProtocolMethods(ProtocolDeclaration declaration) => [ | ||
for (final method in declaration.methods) | ||
..._generateProtocolMethod(method) | ||
]; | ||
|
||
List<String> _generateProtocolMethod(MethodDeclaration method) { | ||
final header = StringBuffer(); | ||
|
||
if (method.hasObjCAnnotation) { | ||
header.write('@objc '); | ||
} | ||
|
||
if (method.isStatic) { | ||
header.write('static '); | ||
} | ||
|
||
header.write( | ||
'func ${method.name}(${generateParameters(method.params)}) ', | ||
); | ||
|
||
header.write(generateAnnotations(method)); | ||
|
||
if (!method.returnType.sameAs(voidType)) { | ||
header.write('-> ${method.returnType.swiftType} '); | ||
} | ||
|
||
return ['$header']; | ||
} | ||
|
||
List<String> _generateProtocolProperties(ProtocolDeclaration declaration) => [ | ||
for (final property in declaration.properties) | ||
..._generateProtocolProperty(property), | ||
]; | ||
|
||
List<String> _generateProtocolProperty(PropertyDeclaration property) { | ||
final header = StringBuffer(); | ||
|
||
if (property.hasObjCAnnotation) { | ||
header.write('@objc '); | ||
} | ||
|
||
if (property.isStatic) { | ||
header.write('static '); | ||
} | ||
|
||
header.write(property.isConstant ? 'let' : 'var'); | ||
header.write(' ${property.name}: ${property.type.swiftType}'); | ||
header.write(' { ${[ | ||
if (property.getter != null) 'get', | ||
if (property.setter != null) 'set' | ||
].join(' ')} }'); | ||
|
||
return [ | ||
header.toString(), | ||
]; | ||
} | ||
|
||
List<String> _generateInitializers(ProtocolDeclaration declaration) { | ||
final initializers = [ | ||
...declaration.initializers, | ||
].nonNulls; | ||
return [for (final init in initializers) ..._generateInitializer(init)]; | ||
} | ||
|
||
List<String> _generateInitializer(InitializerDeclaration initializer) { | ||
final header = StringBuffer(); | ||
|
||
if (initializer.hasObjCAnnotation) { | ||
header.write('@objc '); | ||
} | ||
|
||
header.write('init'); | ||
|
||
if (initializer.isFailable) { | ||
header.write('?'); | ||
} | ||
|
||
header.write('(${generateParameters(initializer.params)})'); | ||
|
||
return [ | ||
'$header ${generateAnnotations(initializer)}', | ||
]; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.