Skip to content

Commit d274520

Browse files
committed
Implement transformation in transform_variable.dart
1 parent f5d1118 commit d274520

File tree

8 files changed

+57
-74
lines changed

8 files changed

+57
-74
lines changed

pkgs/swift2objc/lib/src/ast/_core/interfaces/function_declaration.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@ abstract interface class FunctionDeclaration
2020
CanThrow,
2121
CanAsync {
2222
abstract final ReferredType returnType;
23-
abstract final bool isCallingProperty;
2423
}

pkgs/swift2objc/lib/src/ast/declarations/compounds/members/method_declaration.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ class MethodDeclaration extends AstNode
4343
@override
4444
ReferredType returnType;
4545

46-
@override
47-
bool isCallingProperty;
48-
4946
bool isStatic;
5047

5148
String get fullName => [
@@ -65,7 +62,6 @@ class MethodDeclaration extends AstNode
6562
this.isOverriding = false,
6663
this.throws = false,
6764
this.async = false,
68-
this.isCallingProperty = false,
6965
}) : assert(!isStatic || !isOverriding);
7066

7167
@override

pkgs/swift2objc/lib/src/ast/declarations/globals/globals.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ class GlobalFunctionDeclaration extends AstNode implements FunctionDeclaration {
4646
@override
4747
List<String> statements;
4848

49-
@override
50-
bool isCallingProperty;
51-
5249
GlobalFunctionDeclaration({
5350
required this.id,
5451
required this.name,
@@ -58,7 +55,6 @@ class GlobalFunctionDeclaration extends AstNode implements FunctionDeclaration {
5855
this.statements = const [],
5956
this.throws = false,
6057
this.async = false,
61-
this.isCallingProperty = false,
6258
});
6359

6460
@override

pkgs/swift2objc/lib/src/parser/parsers/declaration_parsers/parse_variable_declaration.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,7 @@ bool _parseVariableIsConstant(Json variableSymbolJson) {
7272
bool _parseVariableThrows(Json json) {
7373
final throws = json['declarationFragments']
7474
.any((frag) => matchFragment(frag, 'keyword', 'throws'));
75-
if (throws) {
76-
// getters.
77-
return true;
78-
}
79-
return false;
75+
return throws;
8076
}
8177

8278
bool _parseVariableAsync(Json json) {

pkgs/swift2objc/lib/src/transformer/transformers/transform_compound.dart

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,15 @@ ClassDeclaration transformCompound(
5252
transformedCompound.nestedDeclarations
5353
.fillNestingParents(transformedCompound);
5454

55-
transformedCompound.properties = originalCompound.properties
56-
.where((property) => !property.throws)
55+
final transformedProperties = originalCompound.properties
5756
.map((property) => transformProperty(
5857
property,
5958
wrappedCompoundInstance,
6059
parentNamer,
6160
transformationMap,
6261
))
6362
.nonNulls
64-
.toList()
65-
..sort((Declaration a, Declaration b) => a.id.compareTo(b.id));
63+
.toList();
6664

6765
transformedCompound.initializers = originalCompound.initializers
6866
.map((initializer) => transformInitializer(
@@ -74,18 +72,25 @@ ClassDeclaration transformCompound(
7472
.toList()
7573
..sort((Declaration a, Declaration b) => a.id.compareTo(b.id));
7674

77-
transformedCompound.methods = (originalCompound.methods +
78-
_convertPropertiesToMethods(originalCompound.properties))
75+
final transformedMethods = originalCompound.methods
7976
.map((method) => transformMethod(
8077
method,
8178
wrappedCompoundInstance,
8279
parentNamer,
8380
transformationMap,
8481
))
8582
.nonNulls
83+
.toList();
84+
85+
transformedCompound.properties = transformedProperties
86+
.whereType<PropertyDeclaration>()
8687
.toList()
8788
..sort((Declaration a, Declaration b) => a.id.compareTo(b.id));
8889

90+
transformedCompound.methods = (transformedMethods +
91+
transformedProperties.whereType<MethodDeclaration>().toList())
92+
..sort((Declaration a, Declaration b) => a.id.compareTo(b.id));
93+
8994
return transformedCompound;
9095
}
9196

@@ -109,23 +114,3 @@ InitializerDeclaration _buildWrapperInitializer(
109114
hasObjCAnnotation: wrappedClassInstance.hasObjCAnnotation,
110115
);
111116
}
112-
113-
List<MethodDeclaration> _convertPropertiesToMethods(
114-
List<PropertyDeclaration> properties,
115-
) {
116-
return properties
117-
.where((property) => property.throws)
118-
.map((property) => MethodDeclaration(
119-
id: property.id,
120-
name: property.name,
121-
returnType: property.type,
122-
params: [],
123-
hasObjCAnnotation: true,
124-
statements: property.getter?.statements ?? [],
125-
isStatic: property.isStatic,
126-
throws: property.throws,
127-
async: property.async,
128-
isCallingProperty: true,
129-
))
130-
.toList();
131-
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ MethodDeclaration? transformMethod(
3939
final methodSource = originalMethod.isStatic
4040
? wrappedClassInstance.type.swiftType
4141
: wrappedClassInstance.name;
42-
return '$methodSource.${originalMethod.name}'
43-
'${originalMethod.isCallingProperty ? '' : '($arguments)'}';
42+
return '$methodSource.${originalMethod.name}($arguments)';
4443
},
4544
);
4645
}
@@ -57,8 +56,8 @@ MethodDeclaration transformGlobalFunction(
5756
wrapperMethodName: globalNamer.makeUnique(
5857
'${globalFunction.name}Wrapper',
5958
),
60-
originalCallStatementGenerator: (arguments) => '${globalFunction.name}'
61-
'${globalFunction.isCallingProperty ? '' : '($arguments)'}',
59+
originalCallStatementGenerator: (arguments) =>
60+
'${globalFunction.name}($arguments)',
6261
);
6362
}
6463

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import '../../ast/_core/interfaces/declaration.dart';
22
import '../../ast/declarations/built_in/built_in_declaration.dart';
33
import '../../ast/declarations/compounds/class_declaration.dart';
4+
import '../../ast/declarations/compounds/members/method_declaration.dart';
5+
import '../../ast/declarations/compounds/members/property_declaration.dart';
46
import '../../ast/declarations/globals/globals.dart';
57
import '../../parser/_core/utils.dart';
68
import '../_core/unique_namer.dart';
@@ -21,41 +23,30 @@ ClassDeclaration transformGlobals(
2123
isWrapper: true,
2224
);
2325

24-
transformedGlobals.properties = globals.variables
25-
.where((variable) => !variable.throws)
26+
final transformedProperties = globals.variables
2627
.map((variable) => transformGlobalVariable(
2728
variable,
2829
globalNamer,
2930
transformationMap,
3031
))
32+
.toList();
33+
34+
final transformedMethods = globals.functions
35+
.map((function) => transformGlobalFunction(
36+
function,
37+
globalNamer,
38+
transformationMap,
39+
))
40+
.toList();
41+
42+
transformedGlobals.properties = transformedProperties
43+
.whereType<PropertyDeclaration>()
3144
.toList()
3245
..sort((Declaration a, Declaration b) => a.id.compareTo(b.id));
3346

34-
transformedGlobals.methods =
35-
(globals.functions + _convertVariablesToFunctions(globals.variables))
36-
.map((function) => transformGlobalFunction(
37-
function,
38-
globalNamer,
39-
transformationMap,
40-
))
41-
.toList()
42-
..sort((Declaration a, Declaration b) => a.id.compareTo(b.id));
47+
transformedGlobals.methods = (transformedMethods +
48+
transformedProperties.whereType<MethodDeclaration>().toList())
49+
..sort((Declaration a, Declaration b) => a.id.compareTo(b.id));
4350

4451
return transformedGlobals;
4552
}
46-
47-
List<GlobalFunctionDeclaration> _convertVariablesToFunctions(
48-
List<GlobalVariableDeclaration> variables,
49-
) {
50-
return variables
51-
.where((variable) => variable.throws)
52-
.map((variable) => GlobalFunctionDeclaration(
53-
id: variable.id,
54-
name: variable.name,
55-
params: [],
56-
returnType: variable.type,
57-
throws: variable.throws,
58-
isCallingProperty: true,
59-
))
60-
.toList();
61-
}

pkgs/swift2objc/lib/src/transformer/transformers/transform_variable.dart

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import '../../ast/_core/interfaces/declaration.dart';
12
import '../../ast/_core/interfaces/variable_declaration.dart';
3+
import '../../ast/declarations/compounds/members/method_declaration.dart';
24
import '../../ast/declarations/compounds/members/property_declaration.dart';
35
import '../../ast/declarations/globals/globals.dart';
46
import '../_core/unique_namer.dart';
@@ -13,7 +15,7 @@ import 'transform_referred_type.dart';
1315
// through the wrapped class instance in the wrapper class. In global variable
1416
// case, it can be referenced directly since it's not a member of any entity.
1517

16-
PropertyDeclaration? transformProperty(
18+
Declaration? transformProperty(
1719
PropertyDeclaration originalProperty,
1820
PropertyDeclaration wrappedClassInstance,
1921
UniqueNamer globalNamer,
@@ -36,7 +38,7 @@ PropertyDeclaration? transformProperty(
3638
);
3739
}
3840

39-
PropertyDeclaration transformGlobalVariable(
41+
Declaration transformGlobalVariable(
4042
GlobalVariableDeclaration globalVariable,
4143
UniqueNamer globalNamer,
4244
TransformationMap transformationMap,
@@ -54,7 +56,7 @@ PropertyDeclaration transformGlobalVariable(
5456

5557
// -------------------------- Core Implementation --------------------------
5658

57-
PropertyDeclaration _transformVariable(
59+
Declaration _transformVariable(
5860
VariableDeclaration originalVariable,
5961
UniqueNamer globalNamer,
6062
TransformationMap transformationMap, {
@@ -71,6 +73,25 @@ PropertyDeclaration _transformVariable(
7173
? originalVariable.hasSetter
7274
: !originalVariable.isConstant;
7375

76+
if (originalVariable.throws) {
77+
return MethodDeclaration(
78+
id: originalVariable.id,
79+
name: wrapperPropertyName,
80+
returnType: transformedType,
81+
params: [],
82+
hasObjCAnnotation: true,
83+
isStatic: originalVariable is PropertyDeclaration
84+
? originalVariable.isStatic
85+
: true,
86+
statements: [
87+
'let result = try $variableReferenceExpression',
88+
'return $transformedType(result)',
89+
],
90+
throws: true,
91+
async: originalVariable.async,
92+
);
93+
}
94+
7495
final transformedProperty = PropertyDeclaration(
7596
id: originalVariable.id,
7697
name: wrapperPropertyName,

0 commit comments

Comments
 (0)