Skip to content
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

[swift2objc] Support Swift ARC features #2055

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class PropertyDeclaration extends AstNode
PropertyStatements? getter;
PropertyStatements? setter;

bool unowned;

bool weak;

bool lazy;

bool isStatic;

PropertyDeclaration({
Expand All @@ -52,6 +58,9 @@ class PropertyDeclaration extends AstNode
this.isStatic = false,
this.throws = false,
this.async = false,
this.unowned = false,
this.weak = false,
this.lazy = false
}) : assert(!(isConstant && hasSetter)),
assert(!(hasSetter && throws));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@ PropertyDeclaration parsePropertyDeclaration(
ParsedSymbolgraph symbolgraph, {
bool isStatic = false,
}) {
final isConstant = _parseVariableIsConstant(propertySymbolJson);
final info = parsePropertyInfo(
propertySymbolJson['declarationFragments']
);

return PropertyDeclaration(
id: parseSymbolId(propertySymbolJson),
name: parseSymbolName(propertySymbolJson),
type: _parseVariableType(propertySymbolJson, symbolgraph),
hasObjCAnnotation: parseSymbolHasObjcAnnotation(propertySymbolJson),
isConstant: isConstant,
hasSetter: isConstant ? false : _parsePropertyHasSetter(propertySymbolJson),
isConstant: info.constant,
hasSetter: info.constant ? false : _parsePropertyHasSetter(propertySymbolJson),
isStatic: isStatic,
throws: _parseVariableThrows(propertySymbolJson),
async: _parseVariableAsync(propertySymbolJson),
throws: info.throws,
async: info.async,
unowned: info.unowned,
weak: info.weak,
lazy: info.lazy,
);
}

Expand All @@ -36,13 +42,15 @@ GlobalVariableDeclaration parseGlobalVariableDeclaration(
}) {
final isConstant = _parseVariableIsConstant(variableSymbolJson);
final hasSetter = _parsePropertyHasSetter(variableSymbolJson);
final variableModifiers = parsePropertyInfo(variableSymbolJson);

return GlobalVariableDeclaration(
id: parseSymbolId(variableSymbolJson),
name: parseSymbolName(variableSymbolJson),
type: _parseVariableType(variableSymbolJson, symbolgraph),
isConstant: isConstant || !hasSetter,
throws: _parseVariableThrows(variableSymbolJson),
async: _parseVariableAsync(variableSymbolJson),
throws: variableModifiers.throws,
async: variableModifiers.async
);
}

Expand All @@ -53,9 +61,7 @@ ReferredType _parseVariableType(
parseTypeAfterSeparator(
TokenList(propertySymbolJson['names']['subHeading']), symbolgraph);

bool _parseVariableIsConstant(Json variableSymbolJson) {
final fragmentsJson = variableSymbolJson['declarationFragments'];

bool _parseVariableIsConstant(Json fragmentsJson) {
final declarationKeyword = fragmentsJson.firstWhere(
(json) =>
matchFragment(json, 'keyword', 'var') ||
Expand All @@ -69,18 +75,33 @@ bool _parseVariableIsConstant(Json variableSymbolJson) {
return matchFragment(declarationKeyword, 'keyword', 'let');
}

bool _parseVariableThrows(Json json) {
final throws = json['declarationFragments']
.any((frag) => matchFragment(frag, 'keyword', 'throws'));
return throws;
bool _findKeywordInFragments(Json json, String keyword) {
final keywordIsPresent = json
.any((frag) => matchFragment(frag, 'keyword', keyword));
return keywordIsPresent;
}

bool _parseVariableAsync(Json json) {
final async = json['declarationFragments']
.any((frag) => matchFragment(frag, 'keyword', 'async'));
return async;
typedef ParsedPropertyInfo = ({
bool async,
bool throws,
bool unowned,
bool weak,
bool lazy,
bool constant,
});

ParsedPropertyInfo parsePropertyInfo(Json json) {
return (
constant: _parseVariableIsConstant(json),
async: _findKeywordInFragments(json, 'async'),
throws: _findKeywordInFragments(json, 'throws'),
unowned: _findKeywordInFragments(json, 'unowned'),
weak: _findKeywordInFragments(json, 'weak'),
lazy: _findKeywordInFragments(json, 'lazy')
);
}


bool _parsePropertyHasSetter(Json propertySymbolJson) {
final fragmentsJson = propertySymbolJson['declarationFragments'];

Expand Down
2 changes: 1 addition & 1 deletion pkgs/swift2objc/test/unit/parse_function_info_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void main() {
decl.id: ParsedSymbol(json: Json(null), declaration: decl)
};
final emptySymbolgraph = ParsedSymbolgraph(parsedSymbols, {});
group('Valid json', () {
group('Function Valid json', () {
void expectEqualParams(
List<Parameter> actualParams,
List<Parameter> expectedParams,
Expand Down
201 changes: 201 additions & 0 deletions pkgs/swift2objc/test/unit/parse_variable_info_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import 'dart:convert';

import 'package:swift2objc/src/ast/_core/shared/parameter.dart';
import 'package:swift2objc/src/ast/_core/shared/referred_type.dart';
import 'package:swift2objc/src/ast/declarations/built_in/built_in_declaration.dart';
import 'package:swift2objc/src/parser/_core/json.dart';
import 'package:swift2objc/src/parser/_core/parsed_symbolgraph.dart';
import 'package:swift2objc/src/parser/parsers/declaration_parsers/parse_function_declaration.dart';
import 'package:swift2objc/src/parser/parsers/declaration_parsers/parse_variable_declaration.dart';
import 'package:test/test.dart';

void main() {
final parsedSymbols = {
for (final decl in builtInDeclarations)
decl.id: ParsedSymbol(json: Json(null), declaration: decl)
};
final emptySymbolgraph = ParsedSymbolgraph(parsedSymbols, {});

group('Variable Valid json', () {

test('Weak Variable', () {
final json = Json(jsonDecode('''[
{
"kind": "keyword",
"spelling": "weak"
},
{
"kind": "text",
"spelling": " "
},
{
"kind": "keyword",
"spelling": "var"
},
{
"kind": "text",
"spelling": " "
},
{
"kind": "identifier",
"spelling": "dorm"
},
{
"kind": "text",
"spelling": ": "
},
{
"kind": "typeIdentifier",
"spelling": "Dorm",
"preciseIdentifier": "s:24funcs_symbolgraph_module4DormC"
},
{
"kind": "text",
"spelling": "?"
}
]'''));

final info = parsePropertyInfo(json);

expect(info.weak, isTrue);
expect(info.constant, isFalse);
});

test('Unowned Variable', () {
final json = Json(jsonDecode('''[
{
"kind": "keyword",
"spelling": "unowned"
},
{
"kind": "text",
"spelling": " "
},
{
"kind": "keyword",
"spelling": "var"
},
{
"kind": "text",
"spelling": " "
},
{
"kind": "identifier",
"spelling": "school"
},
{
"kind": "text",
"spelling": ": "
},
{
"kind": "typeIdentifier",
"spelling": "School",
"preciseIdentifier": "s:24funcs_symbolgraph_module6SchoolC"
}
]'''));

final info = parsePropertyInfo(json);

expect(info.unowned, isTrue);
expect(info.constant, isFalse);
});

test('Unowned Constant variable', () {
final json = Json(jsonDecode('''[
{
"kind": "keyword",
"spelling": "unowned"
},
{
"kind": "text",
"spelling": " "
},
{
"kind": "keyword",
"spelling": "let"
},
{
"kind": "text",
"spelling": " "
},
{
"kind": "identifier",
"spelling": "almaMatter"
},
{
"kind": "text",
"spelling": ": "
},
{
"kind": "typeIdentifier",
"spelling": "School",
"preciseIdentifier": "s:24funcs_symbolgraph_module6SchoolC"
}
]'''));

final info = parsePropertyInfo(json);

expect(info.unowned, isTrue);
expect(info.constant, isTrue);
});

test('Lazy Variable', () {
final json = Json(jsonDecode('''[
{
"kind": "keyword",
"spelling": "lazy"
},
{
"kind": "text",
"spelling": " "
},
{
"kind": "keyword",
"spelling": "var"
},
{
"kind": "text",
"spelling": " "
},
{
"kind": "identifier",
"spelling": "description"
},
{
"kind": "text",
"spelling": ": "
},
{
"kind": "typeIdentifier",
"spelling": "String",
"preciseIdentifier": "s:SS"
},
{
"kind": "text",
"spelling": " { "
},
{
"kind": "keyword",
"spelling": "get"
},
{
"kind": "text",
"spelling": " "
},
{
"kind": "keyword",
"spelling": "set"
},
{
"kind": "text",
"spelling": " }"
}
]'''));

final info = parsePropertyInfo(json);

expect(info.lazy, isTrue);
expect(info.constant, isFalse);
});
});
}