Chromium Code Reviews| Index: pkg/front_end/lib/src/incremental/limited_ast_to_binary.dart |
| diff --git a/pkg/front_end/lib/src/incremental/limited_ast_to_binary.dart b/pkg/front_end/lib/src/incremental/limited_ast_to_binary.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ca113f7ece8aabf685fa03722bbfbc8552f45717 |
| --- /dev/null |
| +++ b/pkg/front_end/lib/src/incremental/limited_ast_to_binary.dart |
| @@ -0,0 +1,80 @@ |
| +// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
|
ahe
2017/05/22 09:27:05
I think this file should be moved to package:kerne
scheglov
2017/05/22 20:36:01
I would not argue about.
But I'm very cautious abo
|
| +// 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 'package:kernel/binary/ast_to_binary.dart'; |
| + |
| +/// Writes libraries that satisfy the [predicate]. |
| +/// |
| +/// Only the referenced subset of canonical names is indexed and written, |
| +/// so we don't waste time indexing all libraries of a program, when only |
| +/// a tiny subset is used. |
| +class LimitedBinaryPrinter extends BinaryPrinter { |
| + final LibraryFilter predicate; |
| + ReferencesStringIndexer _stringIndexer; |
| + |
| + LimitedBinaryPrinter(Sink<List<int>> sink, this.predicate) : super(sink); |
| + |
| + @override |
| + void addCanonicalNamesForLinkTable(List<CanonicalName> list) { |
| + _stringIndexer.referencedNames.forEach((name) { |
| + if (name.index != -1) return; |
| + name.index = list.length; |
| + list.add(name); |
| + }); |
| + } |
| + |
| + @override |
| + void buildStringIndex(Program program) { |
| + program.libraries.where(predicate).forEach((library) { |
| + _stringIndexer.scanLibrary(library); |
| + }); |
| + _stringIndexer.finish(); |
| + } |
| + |
| + @override |
| + StringIndexer createStringIndexer() { |
| + assert(_stringIndexer == null, 'String indexer can be created only once.'); |
| + return _stringIndexer ??= new ReferencesStringIndexer(); |
| + } |
| + |
| + @override |
| + bool shouldWriteLibraryCanonicalNames(Library library) { |
| + return predicate(library); |
| + } |
| + |
| + @override |
| + void writeLibraries(Program program) { |
| + var librariesToWrite = program.libraries.where(predicate).toList(); |
| + writeList(librariesToWrite, writeNode); |
| + } |
| + |
| + @override |
| + void writeNode(Node node) { |
| + if (node is Library && !predicate(node)) return; |
| + node.accept(this); |
| + } |
| +} |
| + |
| +/// Extension of [StringIndexer] that also indexes canonical names of |
| +/// referenced classes and members. |
| +class ReferencesStringIndexer extends StringIndexer { |
| + final List<CanonicalName> referencedNames = <CanonicalName>[]; |
| + |
| + defaultMemberReference(Member node) { |
| + _visitReferencedName(node.canonicalName); |
| + } |
| + |
| + visitClassReference(Class node) { |
| + _visitReferencedName(node.canonicalName); |
| + } |
| + |
| + void _visitReferencedName(CanonicalName name) { |
|
ahe
2017/05/22 09:27:05
Rename to handleReferencedName or something as thi
scheglov
2017/05/22 20:36:01
Done.
|
| + if (name == null || name.parent == null) return; |
| + _visitReferencedName(name.parent); |
| + referencedNames.add(name); |
| + name.index = -1; |
| + put(name.name); |
| + } |
| +} |