Skip to content

Commit 7cbd5f6

Browse files
Dmitry Stefantsovcommit-bot@chromium.org
Dmitry Stefantsov
authored and
commit-bot@chromium.org
committed
[cfe] Add round-trip serialization support for loop nodes
The following nodes are supported now: * ForStatement * ForInStatement * WhileStatement * DoStatement Change-Id: I42d43b949da80a631887996fac4b6a6f4c2a8c79 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/147100 Commit-Queue: Dmitry Stefantsov <dmitryas@google.com> Reviewed-by: Johnni Winther <johnniwinther@google.com>
1 parent 9491c6c commit 7cbd5f6

File tree

3 files changed

+103
-16
lines changed

3 files changed

+103
-16
lines changed

pkg/front_end/test/utils/kernel_chain.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,8 @@ class KernelTextSerialization
373373
}
374374
}
375375

376-
for (RoundTripStatus failure in verifier.failures) {
376+
List<RoundTripStatus> failures = verifier.failures;
377+
for (RoundTripStatus failure in failures) {
377378
LocatedMessage message = templateUnspecified
378379
.withArguments("\n${failure}")
379380
.withLocation(failure.uri, failure.offset, 1);
@@ -386,15 +387,15 @@ class KernelTextSerialization
386387
String filename = "${uri.toFilePath()}${suffix}";
387388
uri = new File(filename).uri;
388389
StringBuffer buffer = new StringBuffer();
389-
for (RoundTripStatus status in verifier.status) {
390+
for (RoundTripStatus status in verifier.takeStatus()) {
390391
status.printOn(buffer);
391392
}
392393
await openWrite(uri, (IOSink sink) {
393394
sink.write(buffer.toString());
394395
});
395396
}
396397

397-
if (verifier.failures.isNotEmpty) {
398+
if (failures.isNotEmpty) {
398399
return new Result<ComponentResult>(
399400
null,
400401
context.expectationSet["TextSerializationFailure"],

pkg/kernel/lib/text/text_serialization_verifier.dart

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,11 @@ class VerificationState {
379379
node.parent is Block &&
380380
node.name != null ||
381381
node is YieldStatement ||
382-
node is IfStatement;
382+
node is IfStatement ||
383+
node is WhileStatement ||
384+
node is DoStatement ||
385+
node is ForStatement ||
386+
node is ForInStatement && !node.isAsync;
383387

384388
static bool isSupported(Node node) =>
385389
node is DartType && isDartTypeSupported(node) ||
@@ -396,7 +400,7 @@ class TextSerializationVerifier extends RecursiveVisitor<void> {
396400
defaultValue: false);
397401

398402
/// List of status for all round-trip serialization attempts.
399-
final List<RoundTripStatus> status = <RoundTripStatus>[];
403+
final List<RoundTripStatus> _status = <RoundTripStatus>[];
400404

401405
final CanonicalName root;
402406

@@ -408,7 +412,9 @@ class TextSerializationVerifier extends RecursiveVisitor<void> {
408412
}
409413

410414
/// List of errors produced during round trips on the visited nodes.
411-
Iterable<RoundTripStatus> get failures => status.where((s) => s.isFailure);
415+
Iterable<RoundTripStatus> get _failures => _status.where((s) => s.isFailure);
416+
417+
List<RoundTripStatus> get failures => _failures.toList()..sort();
412418

413419
VerificationState get currentState => _stateStackTop;
414420

@@ -472,17 +478,17 @@ class TextSerializationVerifier extends RecursiveVisitor<void> {
472478
} catch (exception, stackTrace) {
473479
String message =
474480
showStackTrace ? "${exception}\n${stackTrace}" : "${exception}";
475-
status.add(new RoundTripDeserializationFailure(node, message,
481+
_status.add(new RoundTripDeserializationFailure(node, message,
476482
context: lastSeenTreeNodeWithLocation));
477483
return null;
478484
}
479485
if (stream.moveNext()) {
480-
status.add(new RoundTripDeserializationFailure(
486+
_status.add(new RoundTripDeserializationFailure(
481487
node, "unexpected trailing text",
482488
context: lastSeenTreeNodeWithLocation));
483489
}
484490
if (result == null) {
485-
status.add(new RoundTripDeserializationFailure(
491+
_status.add(new RoundTripDeserializationFailure(
486492
node, "Deserialization of the following returned null: '${input}'",
487493
context: lastSeenTreeNodeWithLocation));
488494
}
@@ -497,7 +503,7 @@ class TextSerializationVerifier extends RecursiveVisitor<void> {
497503
} catch (exception, stackTrace) {
498504
String message =
499505
showStackTrace ? "${exception}\n${stackTrace}" : "${exception}";
500-
status.add(new RoundTripInitialSerializationFailure(node, message,
506+
_status.add(new RoundTripInitialSerializationFailure(node, message,
501507
context: lastSeenTreeNodeWithLocation));
502508
}
503509
return buffer.toString();
@@ -524,15 +530,15 @@ class TextSerializationVerifier extends RecursiveVisitor<void> {
524530
}
525531

526532
void makeRoundTrip<T extends Node>(T node, TextSerializer<T> serializer) {
527-
int failureCount = failures.length;
533+
int failureCount = _failures.length;
528534
String initial = writeNode(node, serializer);
529-
if (failures.length != failureCount) {
535+
if (_failures.length != failureCount) {
530536
return;
531537
}
532538

533539
// Do the round trip.
534540
T deserialized = readNode(node, initial, serializer);
535-
if (failures.length != failureCount) {
541+
if (_failures.length != failureCount) {
536542
return;
537543
}
538544

@@ -542,17 +548,23 @@ class TextSerializationVerifier extends RecursiveVisitor<void> {
542548
}
543549

544550
String serialized = writeNode(deserialized, serializer);
545-
if (failures.length != failureCount) {
551+
if (_failures.length != failureCount) {
546552
return;
547553
}
548554

549555
if (initial != serialized) {
550-
status.add(new RoundTripSecondSerializationFailure(
556+
_status.add(new RoundTripSecondSerializationFailure(
551557
node, initial, serialized,
552558
context: lastSeenTreeNodeWithLocation));
553559
} else {
554-
status.add(new RoundTripSuccess(node, initial,
560+
_status.add(new RoundTripSuccess(node, initial,
555561
context: lastSeenTreeNodeWithLocation));
556562
}
557563
}
564+
565+
List<RoundTripStatus> takeStatus() {
566+
List<RoundTripStatus> result = _status.toList()..sort();
567+
_status.clear();
568+
return result;
569+
}
558570
}

pkg/kernel/lib/text/text_serializer.dart

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,10 @@ class StatementTagger extends StatementVisitor<String>
10021002
}
10031003

10041004
String visitEmptyStatement(EmptyStatement node) => "skip";
1005+
String visitWhileStatement(WhileStatement node) => "while";
1006+
String visitDoStatement(DoStatement node) => "do-while";
1007+
String visitForStatement(ForStatement node) => "for";
1008+
String visitForInStatement(ForInStatement node) => "for-in";
10051009
}
10061010

10071011
TextSerializer<ExpressionStatement> expressionStatementSerializer = new Wrapped(
@@ -1129,6 +1133,72 @@ void unwrapEmptyStatement(EmptyStatement node) {}
11291133

11301134
EmptyStatement wrapEmptyStatement(void ignored) => new EmptyStatement();
11311135

1136+
TextSerializer<WhileStatement> whileStatementSerializer = new Wrapped(
1137+
unwrapWhileStatement,
1138+
wrapWhileStatement,
1139+
new Tuple2Serializer(expressionSerializer, statementSerializer));
1140+
1141+
Tuple2<Expression, Statement> unwrapWhileStatement(WhileStatement node) {
1142+
return new Tuple2(node.condition, node.body);
1143+
}
1144+
1145+
WhileStatement wrapWhileStatement(Tuple2<Expression, Statement> tuple) {
1146+
return new WhileStatement(tuple.first, tuple.second);
1147+
}
1148+
1149+
TextSerializer<DoStatement> doStatementSerializer = new Wrapped(
1150+
unwrapDoStatement,
1151+
wrapDoStatement,
1152+
new Tuple2Serializer(statementSerializer, expressionSerializer));
1153+
1154+
Tuple2<Statement, Expression> unwrapDoStatement(DoStatement node) {
1155+
return new Tuple2(node.body, node.condition);
1156+
}
1157+
1158+
DoStatement wrapDoStatement(Tuple2<Statement, Expression> tuple) {
1159+
return new DoStatement(tuple.first, tuple.second);
1160+
}
1161+
1162+
TextSerializer<ForStatement> forStatementSerializer = new Wrapped(
1163+
unwrapForStatement,
1164+
wrapForStatement,
1165+
new Bind(
1166+
ListSerializer(variableDeclarationSerializer),
1167+
new Tuple3Serializer(expressionSerializer,
1168+
new ListSerializer(expressionSerializer), statementSerializer)));
1169+
1170+
Tuple2<List<VariableDeclaration>,
1171+
Tuple3<Expression, List<Expression>, Statement>>
1172+
unwrapForStatement(ForStatement node) {
1173+
return new Tuple2(
1174+
node.variables, new Tuple3(node.condition, node.updates, node.body));
1175+
}
1176+
1177+
ForStatement wrapForStatement(
1178+
Tuple2<List<VariableDeclaration>,
1179+
Tuple3<Expression, List<Expression>, Statement>>
1180+
tuple) {
1181+
return new ForStatement(
1182+
tuple.first, tuple.second.first, tuple.second.second, tuple.second.third);
1183+
}
1184+
1185+
TextSerializer<ForInStatement> forInStatementSerializer = new Wrapped(
1186+
unwrapForInStatement,
1187+
wrapForInStatement,
1188+
new Tuple2Serializer(expressionSerializer,
1189+
new Bind(variableDeclarationSerializer, statementSerializer)));
1190+
1191+
Tuple2<Expression, Tuple2<VariableDeclaration, Statement>> unwrapForInStatement(
1192+
ForInStatement node) {
1193+
return new Tuple2(node.iterable, new Tuple2(node.variable, node.body));
1194+
}
1195+
1196+
ForInStatement wrapForInStatement(
1197+
Tuple2<Expression, Tuple2<VariableDeclaration, Statement>> tuple) {
1198+
return new ForInStatement(
1199+
tuple.second.first, tuple.first, tuple.second.second);
1200+
}
1201+
11321202
Case<Statement> statementSerializer =
11331203
new Case.uninitialized(const StatementTagger());
11341204

@@ -1443,6 +1513,10 @@ void initializeSerializers() {
14431513
"if": ifStatementSerializer,
14441514
"if-else": ifElseStatementSerializer,
14451515
"skip": emptyStatementSerializer,
1516+
"while": whileStatementSerializer,
1517+
"do-while": doStatementSerializer,
1518+
"for": forStatementSerializer,
1519+
"for-in": forInStatementSerializer,
14461520
});
14471521
functionNodeSerializer.registerTags({
14481522
"sync": syncFunctionNodeSerializer,

0 commit comments

Comments
 (0)