44
55import '../../ast/_core/interfaces/declaration.dart';
66import '../../ast/_core/shared/referred_type.dart';
7+ import '../../ast/declarations/built_in/built_in_declaration.dart';
8+ import '../../ast/declarations/compounds/class_declaration.dart';
9+ import '../../ast/declarations/compounds/members/property_declaration.dart';
710import '../../ast/declarations/typealias_declaration.dart';
811import '../_core/primitive_wrappers.dart';
912import '../_core/unique_namer.dart';
13+ import '../_core/utils.dart';
1014import '../transform.dart';
1115
1216// TODO(https://github.com/dart-lang/native/issues/1358): Refactor this as a
@@ -42,7 +46,9 @@ ReferredType transformReferredType(
4246
4347 if (type.isObjCRepresentable) return type;
4448
45- if (type is GenericType) {
49+ if (type is TupleType) {
50+ return _transformTupleType(type, globalNamer, state);
51+ } else if (type is GenericType) {
4652 throw UnimplementedError('Generic types are not supported yet');
4753 } else if (type is DeclaredType) {
4854 final decl = type.declaration;
@@ -56,3 +62,142 @@ ReferredType transformReferredType(
5662 throw UnimplementedError('Unknown type: $type');
5763 }
5864}
65+
66+ DeclaredType _transformTupleType(
67+ TupleType tupleType,
68+ UniqueNamer globalNamer,
69+ TransformationState state,
70+ ) {
71+ final signature = tupleType.swiftType;
72+
73+ if (state.tupleWrappers.containsKey(signature)) {
74+ return state.tupleWrappers[signature]!.asDeclaredType;
75+ }
76+
77+ final className = _generateTupleClassName(tupleType, globalNamer, state);
78+
79+ final wrapperClass = _generateTupleWrapperClass(
80+ tupleType,
81+ className,
82+ globalNamer,
83+ state,
84+ );
85+
86+ return wrapperClass.asDeclaredType;
87+ }
88+
89+ String _generateTupleClassName(
90+ TupleType tuple,
91+ UniqueNamer globalNamer,
92+ TransformationState state,
93+ ) {
94+ final parts = <String>[];
95+
96+ for (var i = 0; i < tuple.elements.length; i++) {
97+ final element = tuple.elements[i];
98+ if (element.label != null) {
99+ parts.add('${element.label}_${_sanitizeTypeName(element.type)}');
100+ } else {
101+ parts.add(_sanitizeTypeName(element.type));
102+ }
103+ }
104+
105+ return globalNamer.makeUnique('Tuple_${parts.join('_')}');
106+ }
107+
108+ String _sanitizeTypeName(ReferredType type) {
109+ return type.swiftType
110+ .replaceAll('<', '_')
111+ .replaceAll('?', 'Optional')
112+ .replaceAll('[', 'Array_')
113+ .replaceAll(RegExp(r'[^\w]'), '');
114+ }
115+
116+ ClassDeclaration _generateTupleWrapperClass(
117+ TupleType tupleType,
118+ String className,
119+ UniqueNamer globalNamer,
120+ TransformationState state,
121+ ) {
122+ final wrappedInstanceProperty = PropertyDeclaration(
123+ id: 'tuple_${className}_wrappedInstance',
124+ name: 'wrappedInstance',
125+ source: null,
126+ availability: const [],
127+ type: tupleType,
128+ hasSetter: false,
129+ isConstant: false,
130+ hasObjCAnnotation: false,
131+ isStatic: false,
132+ throws: false,
133+ async: false,
134+ unowned: false,
135+ lazy: false,
136+ weak: false,
137+ );
138+
139+ final wrapperClass = ClassDeclaration(
140+ id: 'tuple_wrapper_$className',
141+ name: className,
142+ source: null,
143+ availability: const [],
144+ superClass: objectType,
145+ wrappedInstance: wrappedInstanceProperty,
146+ wrapperInitializer: buildWrapperInitializer(wrappedInstanceProperty),
147+ hasObjCAnnotation: true,
148+ );
149+
150+ state.tupleWrappers[tupleType.swiftType] = wrapperClass;
151+
152+ final properties = <PropertyDeclaration>[];
153+
154+ for (var i = 0; i < tupleType.elements.length; i++) {
155+ final element = tupleType.elements[i];
156+ final propertyName = element.label ?? '_$i';
157+
158+ final transformedType = transformReferredType(
159+ element.type,
160+ globalNamer,
161+ state,
162+ );
163+
164+ final property = PropertyDeclaration(
165+ id: 'tuple_${className}_$propertyName',
166+ name: propertyName,
167+ source: null,
168+ availability: const [],
169+ type: transformedType,
170+ hasSetter: true,
171+ isConstant: false,
172+ hasObjCAnnotation: true,
173+ isStatic: false,
174+ throws: false,
175+ async: false,
176+ unowned: false,
177+ lazy: false,
178+ weak: false,
179+ );
180+
181+ final accessor = element.label != null
182+ ? 'wrappedInstance.${element.label}'
183+ : 'wrappedInstance.$i';
184+
185+ final (wrappedValue, _) = maybeWrapValue(
186+ element.type,
187+ accessor,
188+ globalNamer,
189+ state,
190+ );
191+
192+ property.getter = PropertyStatements([wrappedValue]);
193+
194+ final (unwrappedValue, _) = maybeUnwrapValue(transformedType, 'newValue');
195+ property.setter = PropertyStatements(['$accessor = $unwrappedValue']);
196+
197+ properties.add(property);
198+ }
199+
200+ wrapperClass.properties = properties;
201+
202+ return wrapperClass;
203+ }
0 commit comments