Skip to content

Commit

Permalink
[cfe] Support more statements in round-trip serialization
Browse files Browse the repository at this point in the history
* 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 <johnniwinther@google.com>
Commit-Queue: Dmitry Stefantsov <dmitryas@google.com>
  • Loading branch information
Dmitry Stefantsov authored and commit-bot@chromium.org committed Jun 11, 2020
1 parent 83d4f3a commit 363ac11
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
7 changes: 1 addition & 6 deletions pkg/kernel/lib/text/text_serialization_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
57 changes: 55 additions & 2 deletions pkg/kernel/lib/text/text_serializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,10 @@ class StatementTagger extends StatementVisitor<String>
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";
Expand All @@ -1029,7 +1032,12 @@ class StatementTagger extends StatementVisitor<String>
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<ExpressionStatement> expressionStatementSerializer = new Wrapped(
Expand All @@ -1054,6 +1062,13 @@ ReturnStatement wrapReturnStatement(Expression expression) {
return new ReturnStatement(expression);
}

TextSerializer<ReturnStatement> returnVoidStatementSerializer = new Wrapped(
unwrapReturnVoidStatement, wrapReturnVoidStatement, const Nothing());

void unwrapReturnVoidStatement(void ignored) {}

ReturnStatement wrapReturnVoidStatement(void ignored) => new ReturnStatement();

TextSerializer<YieldStatement> yieldStatementSerializer =
new Wrapped(unwrapYieldStatement, wrapYieldStatement, expressionSerializer);

Expand All @@ -1063,13 +1078,34 @@ YieldStatement wrapYieldStatement(Expression expression) {
return new YieldStatement(expression);
}

TextSerializer<AssertStatement> assertStatementSerializer = new Wrapped(
unwrapAssertStatement,
wrapAssertStatement,
new Tuple2Serializer(expressionSerializer, expressionSerializer));

Tuple2<Expression, Expression> unwrapAssertStatement(AssertStatement node) {
return new Tuple2<Expression, Expression>(node.condition, node.message);
}

AssertStatement wrapAssertStatement(Tuple2<Expression, Expression> tuple) {
return new AssertStatement(tuple.first, message: tuple.second);
}

TextSerializer<Block> blockSerializer =
new Wrapped(unwrapBlock, wrapBlock, const BlockSerializer());

List<Statement> unwrapBlock(Block node) => node.statements;

Block wrapBlock(List<Statement> statements) => new Block(statements);

TextSerializer<AssertBlock> assertBlockSerializer =
new Wrapped(unwrapAssertBlock, wrapAssertBlock, const BlockSerializer());

List<Statement> unwrapAssertBlock(AssertBlock node) => node.statements;

AssertBlock wrapAssertBlock(List<Statement> statements) =>
new AssertBlock(statements);

/// Serializer for [Block]s.
///
/// [BlockSerializer] is a combination of [ListSerializer] and [Bind]. As in
Expand Down Expand Up @@ -1223,6 +1259,19 @@ ForInStatement wrapForInStatement(
tuple.second.first, tuple.first, tuple.second.second);
}

TextSerializer<ForInStatement> awaitForInStatementSerializer = new Wrapped(
unwrapForInStatement,
wrapAwaitForInStatement,
new Tuple2Serializer(expressionSerializer,
new Bind(variableDeclarationSerializer, statementSerializer)));

ForInStatement wrapAwaitForInStatement(
Tuple2<Expression, Tuple2<VariableDeclaration, Statement>> tuple) {
return new ForInStatement(
tuple.second.first, tuple.first, tuple.second.second,
isAsync: true);
}

Case<Statement> statementSerializer =
new Case.uninitialized(const StatementTagger());

Expand Down Expand Up @@ -1533,6 +1582,7 @@ void initializeSerializers() {
statementSerializer.registerTags({
"expr": expressionStatementSerializer,
"ret": returnStatementSerializer,
"ret-void": returnVoidStatementSerializer,
"yield": yieldStatementSerializer,
"block": blockSerializer,
"local": variableDeclarationSerializer,
Expand All @@ -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,
Expand Down

0 comments on commit 363ac11

Please sign in to comment.