OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'package:kernel/ast.dart'; |
| 6 import 'package:kernel/binary/ast_to_binary.dart'; |
| 7 |
| 8 /// Writes libraries that satisfy the [predicate]. |
| 9 /// |
| 10 /// Only the referenced subset of canonical names is indexed and written, |
| 11 /// so we don't waste time indexing all libraries of a program, when only |
| 12 /// a tiny subset is used. |
| 13 class LimitedBinaryPrinter extends BinaryPrinter { |
| 14 final LibraryFilter predicate; |
| 15 |
| 16 LimitedBinaryPrinter(Sink<List<int>> sink, this.predicate) |
| 17 : super(sink, stringIndexer: new ReferencesStringIndexer()); |
| 18 |
| 19 @override |
| 20 void addCanonicalNamesForLinkTable(List<CanonicalName> list) { |
| 21 ReferencesStringIndexer stringIndexer = this.stringIndexer; |
| 22 stringIndexer.referencedNames.forEach((name) { |
| 23 if (name.index != -1) return; |
| 24 name.index = list.length; |
| 25 list.add(name); |
| 26 }); |
| 27 } |
| 28 |
| 29 @override |
| 30 void buildStringIndex(Program program) { |
| 31 program.libraries.where(predicate).forEach((library) { |
| 32 stringIndexer.scanLibrary(library); |
| 33 }); |
| 34 stringIndexer.finish(); |
| 35 } |
| 36 |
| 37 @override |
| 38 bool shouldWriteLibraryCanonicalNames(Library library) { |
| 39 return predicate(library); |
| 40 } |
| 41 |
| 42 @override |
| 43 void writeLibraries(Program program) { |
| 44 var librariesToWrite = program.libraries.where(predicate).toList(); |
| 45 writeList(librariesToWrite, writeNode); |
| 46 } |
| 47 |
| 48 @override |
| 49 void writeNode(Node node) { |
| 50 if (node is Library && !predicate(node)) return; |
| 51 node.accept(this); |
| 52 } |
| 53 } |
| 54 |
| 55 /// Extension of [StringIndexer] that also indexes canonical names of |
| 56 /// referenced classes and members. |
| 57 class ReferencesStringIndexer extends StringIndexer { |
| 58 final List<CanonicalName> referencedNames = <CanonicalName>[]; |
| 59 |
| 60 defaultMemberReference(Member node) { |
| 61 _handleReferencedName(node.canonicalName); |
| 62 } |
| 63 |
| 64 visitClassReference(Class node) { |
| 65 _handleReferencedName(node.canonicalName); |
| 66 } |
| 67 |
| 68 void _handleReferencedName(CanonicalName name) { |
| 69 if (name == null || name.parent == null) return; |
| 70 _handleReferencedName(name.parent); |
| 71 referencedNames.add(name); |
| 72 name.index = -1; |
| 73 put(name.name); |
| 74 } |
| 75 } |
OLD | NEW |