Skip to content

Commit aebbcec

Browse files
authored
[swift2objc] Inout params support (dart-lang#3132)
1 parent 592e429 commit aebbcec

11 files changed

Lines changed: 207 additions & 6 deletions

File tree

‎pkgs/swift2objc/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.2.0-wip
2+
3+
- Added support for inout parameters. Preserve inout types in AST and generate
4+
correct & pass-by-reference invocation syntax.
5+
16
## 0.1.0
27

38
- MVP version.

‎pkgs/swift2objc/lib/src/ast/_core/shared/referred_type.dart‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,34 @@ class OptionalType extends AstNode implements ReferredType {
143143
visitor.visit(child);
144144
}
145145
}
146+
147+
class InoutType extends AstNode implements ReferredType {
148+
final ReferredType child;
149+
150+
@override
151+
bool get isObjCRepresentable => false;
152+
153+
@override
154+
String get swiftType => child.swiftType;
155+
156+
@override
157+
bool _sameAs(ReferredType other) =>
158+
other is InoutType && child.sameAs(other.child);
159+
160+
@override
161+
ReferredType get aliasedType => InoutType(child.aliasedType);
162+
163+
InoutType(this.child);
164+
165+
@override
166+
String toString() => 'inout $child';
167+
168+
@override
169+
void visit(Visitation visitation) => visitation.visitInoutType(this);
170+
171+
@override
172+
void visitChildren(Visitor visitor) {
173+
super.visitChildren(visitor);
174+
visitor.visit(child);
175+
}
176+
}

‎pkgs/swift2objc/lib/src/ast/visitor.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ abstract class Visitation {
7878
void visitDeclaredType(DeclaredType node) => visitReferredType(node);
7979
void visitGenericType(GenericType node) => visitReferredType(node);
8080
void visitOptionalType(OptionalType node) => visitReferredType(node);
81+
void visitInoutType(InoutType node) => visitReferredType(node);
8182
void visitDeclaration(Declaration node) => visitAstNode(node);
8283
void visitBuiltInDeclaration(BuiltInDeclaration node) =>
8384
visitDeclaration(node);

‎pkgs/swift2objc/lib/src/parser/parsers/parse_type.dart‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,8 @@ typedef PrefixParselet =
145145
if (_tokenId(fragments[0]) == 'text: ') {
146146
fragments = fragments.slice(1);
147147
}
148-
// TODO(https://github.com/dart-lang/native/issues/1754): Mark the returned
149-
// type as inout (maybe wrap it in a new InOutType AST node?).
150-
return parseType(context, symbolgraph, fragments);
148+
final (type, suffix) = parseType(context, symbolgraph, fragments);
149+
return (InoutType(type), suffix);
151150
}
152151

153152
Map<String, PrefixParselet> _prefixParsets = {

‎pkgs/swift2objc/lib/src/transformer/_core/utils.dart‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ import 'unique_namer.dart';
2424
TransformationState state, {
2525
bool shouldWrapPrimitives = false,
2626
}) {
27+
if (type is InoutType) {
28+
final (newValue, newType) = maybeWrapValue(
29+
type.child,
30+
value,
31+
globalNamer,
32+
state,
33+
shouldWrapPrimitives: shouldWrapPrimitives,
34+
);
35+
return (newValue, InoutType(newType));
36+
}
37+
2738
final (wrappedPrimitiveType, returnsWrappedPrimitive) =
2839
maybeGetPrimitiveWrapper(type, shouldWrapPrimitives, state);
2940
if (returnsWrappedPrimitive) {
@@ -78,6 +89,11 @@ import 'unique_namer.dart';
7889
ReferredType type,
7990
String value,
8091
) {
92+
if (type is InoutType) {
93+
final (newValue, newType) = maybeUnwrapValue(type.child, value);
94+
return (newValue, InoutType(newType));
95+
}
96+
8197
if (!type.isObjCRepresentable) {
8298
return (value, type);
8399
}

‎pkgs/swift2objc/lib/src/transformer/transformers/transform_function.dart‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,14 @@ String generateInvocationParams(
165165
);
166166

167167
assert(unwrappedType.sameAs(originalParam.type));
168+
final invocationValue = originalParam.type is InoutType
169+
? '&$unwrappedParamValue'
170+
: unwrappedParamValue;
168171

169172
argumentsList.add(
170173
originalParam.name.isEmpty || originalParam.name == '_'
171-
? unwrappedParamValue
172-
: '${originalParam.name}: $unwrappedParamValue',
174+
? invocationValue
175+
: '${originalParam.name}: $invocationValue',
173176
);
174177
}
175178
return argumentsList.join(', ');

‎pkgs/swift2objc/lib/src/transformer/transformers/transform_referred_type.dart‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import '../../ast/_core/interfaces/declaration.dart';
66
import '../../ast/_core/shared/referred_type.dart';
77
import '../../ast/declarations/typealias_declaration.dart';
8+
import '../_core/primitive_wrappers.dart';
89
import '../_core/unique_namer.dart';
910
import '../transform.dart';
1011

@@ -16,6 +17,18 @@ ReferredType transformReferredType(
1617
UniqueNamer globalNamer,
1718
TransformationState state,
1819
) {
20+
if (type is InoutType) {
21+
final (wrappedPrimitive, hasWrappedPrimitive) = maybeGetPrimitiveWrapper(
22+
type.child,
23+
true,
24+
state,
25+
);
26+
if (hasWrappedPrimitive) {
27+
return InoutType(wrappedPrimitive);
28+
}
29+
return InoutType(transformReferredType(type.child, globalNamer, state));
30+
}
31+
1932
if (type.isObjCRepresentable) return type;
2033

2134
if (type is GenericType) {

‎pkgs/swift2objc/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
name: swift2objc
66
description: 'A tool for generating bindings that allow interop between Dart and Swift code.'
7-
version: 0.1.0
7+
version: 0.2.0-wip
88
repository: https://github.com/dart-lang/native/tree/main/pkgs/swift2objc
99
issue_tracker: https://github.com/dart-lang/native/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Aswift2objc
1010

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Foundation
2+
3+
public class MyClass {
4+
public func update(_ value: inout Int) {
5+
value += 1
6+
}
7+
}
8+
9+
public class MyOtherClass {
10+
public init() {}
11+
}
12+
13+
public func swapTwoInts(_ a: inout Int, _ b: inout Int) {
14+
let temp = a
15+
a = b
16+
b = temp
17+
}
18+
19+
public func replaceOther(_ value: inout MyOtherClass) {
20+
value = MyOtherClass()
21+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Test preamble text
2+
3+
import Foundation
4+
5+
@objc public class GlobalsWrapper: NSObject {
6+
@objc static public func swapTwoIntsWrapper(_ a: IntWrapper, _ b: IntWrapper) {
7+
return swapTwoInts(&a.wrappedInstance, &b.wrappedInstance)
8+
}
9+
10+
@objc static public func replaceOtherWrapper(_ value: MyOtherClassWrapper) {
11+
return replaceOther(&value.wrappedInstance)
12+
}
13+
14+
}
15+
16+
@objc public class MyOtherClassWrapper: NSObject {
17+
var wrappedInstance: MyOtherClass
18+
19+
init(_ wrappedInstance: MyOtherClass) {
20+
self.wrappedInstance = wrappedInstance
21+
}
22+
23+
@objc override public init() {
24+
wrappedInstance = MyOtherClass()
25+
}
26+
27+
}
28+
29+
@objc public class MyClassWrapper: NSObject {
30+
var wrappedInstance: MyClass
31+
32+
init(_ wrappedInstance: MyClass) {
33+
self.wrappedInstance = wrappedInstance
34+
}
35+
36+
@objc public func update(_ value: IntWrapper) {
37+
return wrappedInstance.update(&value.wrappedInstance)
38+
}
39+
40+
}
41+
42+
@objc public class IntWrapper: NSObject {
43+
var wrappedInstance: Int
44+
45+
init(_ wrappedInstance: Int) {
46+
self.wrappedInstance = wrappedInstance
47+
}
48+
49+
}
50+

0 commit comments

Comments
 (0)