-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[swift2objc] Support wrapper classes for primitives (#1984)
- Loading branch information
1 parent
dce9d73
commit 0df33b3
Showing
9 changed files
with
379 additions
and
56 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
pkgs/swift2objc/lib/src/transformer/_core/primitive_wrappers.dart
This file contains 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,65 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:collection/collection.dart'; | ||
|
||
import '../../ast/_core/interfaces/declaration.dart'; | ||
import '../../ast/_core/shared/referred_type.dart'; | ||
import '../../ast/declarations/built_in/built_in_declaration.dart'; | ||
import '../../ast/declarations/compounds/class_declaration.dart'; | ||
import '../../ast/declarations/compounds/members/property_declaration.dart'; | ||
import '../../parser/_core/utils.dart'; | ||
import '../../transformer/_core/utils.dart'; | ||
import '../transform.dart'; | ||
|
||
final _primitiveWrappers = List<(ReferredType, ReferredType)>.unmodifiable([ | ||
(intType, _createWrapperClass(intType)), | ||
(floatType, _createWrapperClass(floatType)), | ||
(doubleType, _createWrapperClass(doubleType)), | ||
(boolType, _createWrapperClass(boolType)), | ||
]); | ||
|
||
ReferredType _createWrapperClass(DeclaredType primitiveType) { | ||
final property = PropertyDeclaration( | ||
id: primitiveType.id.addIdSuffix('wrappedInstance'), | ||
name: 'wrappedInstance', | ||
type: primitiveType, | ||
); | ||
return ClassDeclaration( | ||
id: primitiveType.id.addIdSuffix('wrapper'), | ||
name: '${primitiveType.name}Wrapper', | ||
hasObjCAnnotation: true, | ||
superClass: objectType, | ||
isWrapper: true, | ||
wrappedInstance: property, | ||
wrapperInitializer: buildWrapperInitializer(property)) | ||
.asDeclaredType; | ||
} | ||
|
||
// Support Optional primitives as return Type | ||
// TODO(https://github.com/dart-lang/native/issues/1743) | ||
|
||
(ReferredType, bool) maybeGetPrimitiveWrapper( | ||
ReferredType type, | ||
bool shouldWrapPrimitives, | ||
TransformationMap transformationMap, | ||
) { | ||
if (type is! DeclaredType || !shouldWrapPrimitives) { | ||
return (type, false); | ||
} | ||
|
||
final wrapper = _getPrimitiveWrapper(type); | ||
if (wrapper == null) { | ||
return (type, false); | ||
} | ||
|
||
transformationMap[type.declaration] = (wrapper as DeclaredType).declaration; | ||
return (wrapper, true); | ||
} | ||
|
||
ReferredType? _getPrimitiveWrapper(DeclaredType other) { | ||
return _primitiveWrappers | ||
.firstWhereOrNull((pair) => pair.$1.sameAs(other)) | ||
?.$2; | ||
} |
This file contains 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 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 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 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 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 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 |
---|---|---|
|
@@ -15,6 +15,7 @@ topics: | |
- codegen | ||
|
||
dependencies: | ||
collection: ^1.19.1 | ||
logging: ^1.3.0 | ||
meta: ^1.16.0 | ||
path: ^1.9.0 | ||
|
Oops, something went wrong.