From 363ac11c1ffac9721587d6c895d1f8354f3f678e Mon Sep 17 00:00:00 2001 From: Dmitry Stefantsov Date: Thu, 11 Jun 2020 10:10:08 +0000 Subject: [PATCH] [cfe] Support more statements in round-trip serialization * async-version of ForInStatement * AssertBlock * AssertStatement * ReturnStatement without return expressions Change-Id: Ic1dcff70913c37fc09e31f4608eff8a47037eaa1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/150924 Reviewed-by: Johnni Winther Commit-Queue: Dmitry Stefantsov --- .../lib/text/text_serialization_verifier.dart | 7 +-- pkg/kernel/lib/text/text_serializer.dart | 57 ++++++++++++++++++- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/pkg/kernel/lib/text/text_serialization_verifier.dart b/pkg/kernel/lib/text/text_serialization_verifier.dart index dfd9a79b1e95..456b20464f1e 100644 --- a/pkg/kernel/lib/text/text_serialization_verifier.dart +++ b/pkg/kernel/lib/text/text_serialization_verifier.dart @@ -345,19 +345,14 @@ class VerificationState { static bool isStatementNotSupported(Statement node) => node is BreakStatement || - node is AssertBlock || node is VariableDeclaration && (node.parent is! Block || node.name == null) || node is SwitchStatement || node is TryFinally || - node is EmptyStatement || node is LabeledStatement || - node is ForInStatement && node.isAsync || node is TryCatch || node is FunctionDeclaration || - node is ContinueSwitchStatement || - node is AssertStatement || - node is ReturnStatement && node.expression == null; + node is ContinueSwitchStatement; static bool isSupported(Node node) => !isNotSupported(node); diff --git a/pkg/kernel/lib/text/text_serializer.dart b/pkg/kernel/lib/text/text_serializer.dart index 2f071d764f4d..0d3f673ff07b 100644 --- a/pkg/kernel/lib/text/text_serializer.dart +++ b/pkg/kernel/lib/text/text_serializer.dart @@ -1017,7 +1017,10 @@ class StatementTagger extends StatementVisitor String tag(Statement statement) => statement.accept(this); String visitExpressionStatement(ExpressionStatement _) => "expr"; - String visitReturnStatement(ReturnStatement _) => "ret"; + String visitReturnStatement(ReturnStatement node) { + return node.expression == null ? "ret-void" : "ret"; + } + String visitYieldStatement(YieldStatement _) => "yield"; String visitBlock(Block _) => "block"; String visitVariableDeclaration(VariableDeclaration _) => "local"; @@ -1029,7 +1032,12 @@ class StatementTagger extends StatementVisitor String visitWhileStatement(WhileStatement node) => "while"; String visitDoStatement(DoStatement node) => "do-while"; String visitForStatement(ForStatement node) => "for"; - String visitForInStatement(ForInStatement node) => "for-in"; + String visitForInStatement(ForInStatement node) { + return node.isAsync ? "await-for-in" : "for-in"; + } + + String visitAssertStatement(AssertStatement node) => "assert"; + String visitAssertBlock(AssertBlock node) => "assert-block"; } TextSerializer expressionStatementSerializer = new Wrapped( @@ -1054,6 +1062,13 @@ ReturnStatement wrapReturnStatement(Expression expression) { return new ReturnStatement(expression); } +TextSerializer returnVoidStatementSerializer = new Wrapped( + unwrapReturnVoidStatement, wrapReturnVoidStatement, const Nothing()); + +void unwrapReturnVoidStatement(void ignored) {} + +ReturnStatement wrapReturnVoidStatement(void ignored) => new ReturnStatement(); + TextSerializer yieldStatementSerializer = new Wrapped(unwrapYieldStatement, wrapYieldStatement, expressionSerializer); @@ -1063,6 +1078,19 @@ YieldStatement wrapYieldStatement(Expression expression) { return new YieldStatement(expression); } +TextSerializer assertStatementSerializer = new Wrapped( + unwrapAssertStatement, + wrapAssertStatement, + new Tuple2Serializer(expressionSerializer, expressionSerializer)); + +Tuple2 unwrapAssertStatement(AssertStatement node) { + return new Tuple2(node.condition, node.message); +} + +AssertStatement wrapAssertStatement(Tuple2 tuple) { + return new AssertStatement(tuple.first, message: tuple.second); +} + TextSerializer blockSerializer = new Wrapped(unwrapBlock, wrapBlock, const BlockSerializer()); @@ -1070,6 +1098,14 @@ List unwrapBlock(Block node) => node.statements; Block wrapBlock(List statements) => new Block(statements); +TextSerializer assertBlockSerializer = + new Wrapped(unwrapAssertBlock, wrapAssertBlock, const BlockSerializer()); + +List unwrapAssertBlock(AssertBlock node) => node.statements; + +AssertBlock wrapAssertBlock(List statements) => + new AssertBlock(statements); + /// Serializer for [Block]s. /// /// [BlockSerializer] is a combination of [ListSerializer] and [Bind]. As in @@ -1223,6 +1259,19 @@ ForInStatement wrapForInStatement( tuple.second.first, tuple.first, tuple.second.second); } +TextSerializer awaitForInStatementSerializer = new Wrapped( + unwrapForInStatement, + wrapAwaitForInStatement, + new Tuple2Serializer(expressionSerializer, + new Bind(variableDeclarationSerializer, statementSerializer))); + +ForInStatement wrapAwaitForInStatement( + Tuple2> tuple) { + return new ForInStatement( + tuple.second.first, tuple.first, tuple.second.second, + isAsync: true); +} + Case statementSerializer = new Case.uninitialized(const StatementTagger()); @@ -1533,6 +1582,7 @@ void initializeSerializers() { statementSerializer.registerTags({ "expr": expressionStatementSerializer, "ret": returnStatementSerializer, + "ret-void": returnVoidStatementSerializer, "yield": yieldStatementSerializer, "block": blockSerializer, "local": variableDeclarationSerializer, @@ -1543,6 +1593,9 @@ void initializeSerializers() { "do-while": doStatementSerializer, "for": forStatementSerializer, "for-in": forInStatementSerializer, + "await-for-in": awaitForInStatementSerializer, + "assert": assertStatementSerializer, + "assert-block": assertBlockSerializer, }); functionNodeSerializer.registerTags({ "sync": syncFunctionNodeSerializer,