Skip to content

Commit 307a46f

Browse files
davidmorganCommit Queue
authored and
Commit Queue
committed
Add arguments to ConstructorMetadataAnnotation.
Support serializing them. Add helper methods to serialize lists of `Code` and `Map<String, Code`. [email protected] Bug: dart-lang/language#3443 Change-Id: If03b4ad5bf761b089f800834fc103b26143a0ddc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/335446 Commit-Queue: Jake Macdonald <[email protected]> Reviewed-by: Jake Macdonald <[email protected]> Auto-Submit: Morgan :) <[email protected]>
1 parent 92e61ea commit 307a46f

File tree

7 files changed

+86
-9
lines changed

7 files changed

+86
-9
lines changed

pkg/_fe_analyzer_shared/lib/src/macros/api/introspection.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,10 @@ abstract interface class ConstructorMetadataAnnotation
424424
/// For unnamed constructors, the name of this identifier will be the empty
425425
/// String.
426426
Identifier get constructor;
427+
428+
/// The positional arguments of this constructor call.
429+
Iterable<ExpressionCode> get positionalArguments;
430+
431+
/// The named arguments of this constructor call.
432+
Map<String, ExpressionCode> get namedArguments;
427433
}

pkg/_fe_analyzer_shared/lib/src/macros/executor/introspection_impls.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import '../api.dart';
56
import 'remote_instance.dart';
67
import 'serialization.dart';
78
import 'serialization_extensions.dart';
8-
import '../api.dart';
99

1010
class IdentifierImpl extends RemoteInstance implements Identifier {
1111
@override
@@ -287,12 +287,22 @@ class ConstructorMetadataAnnotationImpl extends MetadataAnnotationImpl
287287
@override
288288
final IdentifierImpl type;
289289

290+
@override
291+
final List<ExpressionCode> positionalArguments;
292+
293+
@override
294+
final Map<String, ExpressionCode> namedArguments;
295+
290296
@override
291297
RemoteInstanceKind get kind =>
292298
RemoteInstanceKind.constructorMetadataAnnotation;
293299

294300
ConstructorMetadataAnnotationImpl(
295-
{required int id, required this.constructor, required this.type})
301+
{required int id,
302+
required this.constructor,
303+
required this.type,
304+
required this.positionalArguments,
305+
required this.namedArguments})
296306
: super(id);
297307

298308
@override
@@ -301,6 +311,17 @@ class ConstructorMetadataAnnotationImpl extends MetadataAnnotationImpl
301311

302312
constructor.serialize(serializer);
303313
type.serialize(serializer);
314+
serializer.startList();
315+
for (ExpressionCode positionalArgument in positionalArguments) {
316+
positionalArgument.serialize(serializer);
317+
}
318+
serializer.endList();
319+
serializer.startList();
320+
for (MapEntry<String, ExpressionCode> entry in namedArguments.entries) {
321+
serializer.addString(entry.key);
322+
entry.value.serialize(serializer);
323+
}
324+
serializer.endList();
304325
}
305326
}
306327

pkg/_fe_analyzer_shared/lib/src/macros/executor/serialization.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,11 @@ class JsonDeserializer implements Deserializer {
226226
throw new StateError(
227227
'You must call `moveNext()` before reading any values.');
228228
}
229-
return _path.last.current as T;
229+
Object? current = _path.last.current;
230+
if (current is! T) {
231+
throw new StateError('Expected $T, got: ${_path.last.current}');
232+
}
233+
return current;
230234
}
231235

232236
@override

pkg/_fe_analyzer_shared/lib/src/macros/executor/serialization_extensions.dart

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import 'package:_fe_analyzer_shared/src/macros/executor/introspection_impls.dart';
22

3+
import '../api.dart';
34
import 'remote_instance.dart';
45
import 'serialization.dart';
5-
import '../api.dart';
66

77
extension DeserializerExtensions on Deserializer {
88
T expectRemoteInstance<T extends Object>() {
@@ -83,7 +83,7 @@ extension DeserializerExtensions on Deserializer {
8383

8484
Uri expectUri() => Uri.parse(expectString());
8585

86-
/// Helper method to read a list of [RemoteInstance]s.
86+
/// Reads a list of [RemoteInstance]s.
8787
List<T> _expectRemoteInstanceList<T extends RemoteInstance>() {
8888
expectList();
8989
return [
@@ -92,6 +92,24 @@ extension DeserializerExtensions on Deserializer {
9292
];
9393
}
9494

95+
/// Reads a list of [Code]s.
96+
List<T> _expectCodeList<T extends Code>() {
97+
expectList();
98+
return [
99+
for (bool hasNext = moveNext(); hasNext; hasNext = moveNext())
100+
expectCode(),
101+
];
102+
}
103+
104+
/// Reads a `Map<String, T extends Code>`.
105+
Map<String, T> _expectStringCodeMap<T extends Code>() {
106+
expectList();
107+
return {
108+
for (bool hasNext = moveNext(); hasNext; hasNext = moveNext())
109+
expectString(): (this..moveNext()).expectCode(),
110+
};
111+
}
112+
95113
NamedTypeAnnotationImpl _expectNamedTypeAnnotation(int id) =>
96114
new NamedTypeAnnotationImpl(
97115
id: id,
@@ -284,7 +302,9 @@ extension DeserializerExtensions on Deserializer {
284302
new ConstructorMetadataAnnotationImpl(
285303
id: id,
286304
constructor: expectRemoteInstance(),
287-
type: RemoteInstance.deserialize(this));
305+
type: RemoteInstance.deserialize(this),
306+
positionalArguments: (this..moveNext())._expectCodeList(),
307+
namedArguments: (this..moveNext())._expectStringCodeMap());
288308

289309
IdentifierMetadataAnnotationImpl _expectIdentifierMetadataAnnotation(
290310
int id) =>

pkg/_fe_analyzer_shared/test/macros/executor/serialization_test.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,16 @@ void main() {
625625
id: RemoteInstance.uniqueId,
626626
type: IdentifierImpl(
627627
id: RemoteInstance.uniqueId, name: 'Singleton'),
628-
constructor: IdentifierImpl(
629-
id: RemoteInstance.uniqueId, name: 'someName'));
628+
constructor:
629+
IdentifierImpl(id: RemoteInstance.uniqueId, name: 'someName'),
630+
positionalArguments: [
631+
ExpressionCode.fromString("'foo'"),
632+
ExpressionCode.fromString('12'),
633+
],
634+
namedArguments: {
635+
'bar': ExpressionCode.fromString("'bar'"),
636+
'foobar': ExpressionCode.fromString('13'),
637+
});
630638

631639
expectSerializationEquality<ConstructorMetadataAnnotationImpl>(
632640
constructorMetadata, mode, RemoteInstance.deserialize);

pkg/_fe_analyzer_shared/test/macros/util.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import 'package:_fe_analyzer_shared/src/macros/api.dart';
88
import 'package:_fe_analyzer_shared/src/macros/executor.dart';
99
import 'package:_fe_analyzer_shared/src/macros/executor/introspection_impls.dart';
1010
import 'package:_fe_analyzer_shared/src/macros/executor/remote_instance.dart';
11-
1211
import 'package:test/test.dart';
1312

1413
class TestTypePhaseIntrospector implements TypePhaseIntrospector {
@@ -292,6 +291,23 @@ class _DeepEqualityMatcher extends Matcher {
292291
return false;
293292
}
294293
}
294+
} else if (instance is Map) {
295+
item as Map;
296+
if (!equals(instance.length).matches(item.length, matchState)) {
297+
return false;
298+
}
299+
for (var key in instance.keys) {
300+
// Key sets are same size, so they are equal if every key in `instance`
301+
// is also a key in `item`.
302+
if (!contains(key).matches(item, matchState)) {
303+
return false;
304+
}
305+
// Maps are equal if keys are equal and every value is equal.
306+
if (!_DeepEqualityMatcher(instance[key])
307+
.matches(item[key], matchState)) {
308+
return false;
309+
}
310+
}
295311
} else {
296312
// Handles basic values and identity
297313
if (!equals(instance).matches(item, matchState)) {

pkg/analyzer/lib/src/summary2/macro_declarations.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ class DeclarationBuilder {
187187
getElement: () => node.element,
188188
),
189189
type: identifierMacro,
190+
positionalArguments: [], // TODO(scheglov) implement
191+
namedArguments: {}, // TODO(scheglov) implement
190192
);
191193
} else {
192194
return macro.IdentifierMetadataAnnotationImpl(

0 commit comments

Comments
 (0)