OLD | NEW |
---|---|
(Empty) | |
1 // 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
| |
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 ReferencesStringIndexer _stringIndexer; | |
16 | |
17 LimitedBinaryPrinter(Sink<List<int>> sink, this.predicate) : super(sink); | |
18 | |
19 @override | |
20 void addCanonicalNamesForLinkTable(List<CanonicalName> list) { | |
21 _stringIndexer.referencedNames.forEach((name) { | |
22 if (name.index != -1) return; | |
23 name.index = list.length; | |
24 list.add(name); | |
25 }); | |
26 } | |
27 | |
28 @override | |
29 void buildStringIndex(Program program) { | |
30 program.libraries.where(predicate).forEach((library) { | |
31 _stringIndexer.scanLibrary(library); | |
32 }); | |
33 _stringIndexer.finish(); | |
34 } | |
35 | |
36 @override | |
37 StringIndexer createStringIndexer() { | |
38 assert(_stringIndexer == null, 'String indexer can be created only once.'); | |
39 return _stringIndexer ??= new ReferencesStringIndexer(); | |
40 } | |
41 | |
42 @override | |
43 bool shouldWriteLibraryCanonicalNames(Library library) { | |
44 return predicate(library); | |
45 } | |
46 | |
47 @override | |
48 void writeLibraries(Program program) { | |
49 var librariesToWrite = program.libraries.where(predicate).toList(); | |
50 writeList(librariesToWrite, writeNode); | |
51 } | |
52 | |
53 @override | |
54 void writeNode(Node node) { | |
55 if (node is Library && !predicate(node)) return; | |
56 node.accept(this); | |
57 } | |
58 } | |
59 | |
60 /// Extension of [StringIndexer] that also indexes canonical names of | |
61 /// referenced classes and members. | |
62 class ReferencesStringIndexer extends StringIndexer { | |
63 final List<CanonicalName> referencedNames = <CanonicalName>[]; | |
64 | |
65 defaultMemberReference(Member node) { | |
66 _visitReferencedName(node.canonicalName); | |
67 } | |
68 | |
69 visitClassReference(Class node) { | |
70 _visitReferencedName(node.canonicalName); | |
71 } | |
72 | |
73 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.
| |
74 if (name == null || name.parent == null) return; | |
75 _visitReferencedName(name.parent); | |
76 referencedNames.add(name); | |
77 name.index = -1; | |
78 put(name.name); | |
79 } | |
80 } | |
OLD | NEW |