Cleanup toString transformer

This is a final cleanup of old version of toString removal transformer
which was moved to pkg/vm in https://dart-review.googlesource.com/c/sdk/+/200525.

Also, this change removes handling of _KeepToString annotation class
after dart:ui @keepToString switched to use pragma and _KeepToString
was removed.

TEST=existing tests

Closes https://github.com/dart-lang/sdk/issues/46022

Change-Id: I7208ec70e6703d25b93d6ebbb306c18bae4b6987
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/201162
Reviewed-by: Slava Egorov <vegorov@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
This commit is contained in:
Alexander Markov 2021-06-07 16:56:22 +00:00 committed by commit-bot@chromium.org
parent b6fad9c4f3
commit 37806ba461
3 changed files with 0 additions and 98 deletions

View file

@ -38,8 +38,6 @@ import 'package:vm/kernel_front_end.dart';
import 'src/javascript_bundle.dart';
import 'src/strong_components.dart';
export 'src/to_string_transformer.dart';
ArgParser argParser = ArgParser(allowTrailingOptions: true)
..addFlag('train',
help: 'Run through sample command line to produce snapshot',

View file

@ -1,93 +0,0 @@
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:kernel/ast.dart';
import '../frontend_server.dart';
// Transformer/visitor for toString
// If we add any more of these, they really should go into a separate library.
/// A [RecursiveVisitor] that replaces [Object.toString] overrides with
/// `super.toString()`.
class ToStringVisitor extends RecursiveVisitor {
/// The [packageUris] must not be null.
ToStringVisitor(this._packageUris) : assert(_packageUris != null);
/// A set of package URIs to apply this transformer to, e.g. 'dart:ui' and
/// 'package:flutter/foundation.dart'.
final Set<String> _packageUris;
/// Turn 'dart:ui' into 'dart:ui', or
/// 'package:flutter/src/semantics_event.dart' into 'package:flutter'.
String _importUriToPackage(Uri importUri) =>
'${importUri.scheme}:${importUri.pathSegments.first}';
bool _isInTargetPackage(Procedure node) {
return _packageUris
.contains(_importUriToPackage(node.enclosingLibrary.importUri));
}
bool _hasKeepAnnotation(Procedure node) {
for (ConstantExpression expression
in node.annotations.whereType<ConstantExpression>()) {
if (expression.constant is! InstanceConstant) {
continue;
}
final InstanceConstant constant = expression.constant as InstanceConstant;
if (constant.classNode.name == '_KeepToString' &&
constant.classNode.enclosingLibrary.importUri.toString() ==
'dart:ui') {
return true;
}
}
return false;
}
@override
void visitProcedure(Procedure node) {
if (node.name.text == 'toString' &&
node.enclosingClass != null &&
node.enclosingLibrary != null &&
!node.isStatic &&
!node.isAbstract &&
!node.enclosingClass.isEnum &&
_isInTargetPackage(node) &&
!_hasKeepAnnotation(node)) {
node.function.body.replaceWith(
ReturnStatement(
SuperMethodInvocation(
node.name,
Arguments(<Expression>[]),
),
),
);
}
}
@override
void defaultMember(Member node) {}
}
/// Replaces [Object.toString] overrides with calls to super for the specified
/// [packageUris].
class ToStringTransformer extends ProgramTransformer {
/// The [packageUris] parameter must not be null, but may be empty.
ToStringTransformer(this._child, this._packageUris)
: assert(_packageUris != null);
final ProgramTransformer _child;
/// A set of package URIs to apply this transformer to, e.g. 'dart:ui' and
/// 'package:flutter/foundation.dart'.
final Set<String> _packageUris;
@override
void transform(Component component) {
assert(_child is! ToStringTransformer);
if (_packageUris.isNotEmpty) {
component.visitChildren(ToStringVisitor(_packageUris));
}
_child?.transform(component);
}
}

View file

@ -39,9 +39,6 @@ class ToStringVisitor extends RecursiveVisitor {
final className = constant.classNode.name;
final libraryUri =
constant.classNode.enclosingLibrary.importUri.toString();
if (className == '_KeepToString' && libraryUri == 'dart:ui') {
return true;
}
if (className == 'pragma' && libraryUri == 'dart:core') {
for (var fieldRef in constant.fieldValues.keys) {
if (fieldRef.asField.name.text == 'name') {