Chromium Code Reviews| Index: pkg/kernel/lib/binary/ast_to_binary.dart |
| diff --git a/pkg/kernel/lib/binary/ast_to_binary.dart b/pkg/kernel/lib/binary/ast_to_binary.dart |
| index 0edfdd886af300e6086061ec0f33ef07e236a8f0..daeae9e8759a474e1b8f990bda95848dea6b809a 100644 |
| --- a/pkg/kernel/lib/binary/ast_to_binary.dart |
| +++ b/pkg/kernel/lib/binary/ast_to_binary.dart |
| @@ -19,7 +19,7 @@ class BinaryPrinter extends Visitor { |
| LabelIndexer _labelIndexer; |
| SwitchCaseIndexer _switchCaseIndexer; |
| final TypeParameterIndexer _typeParameterIndexer = new TypeParameterIndexer(); |
| - final StringIndexer _stringIndexer = new StringIndexer(); |
| + StringIndexer _stringIndexer; |
|
ahe
2017/05/22 09:27:05
You can keep this final by adding an optional para
ahe
2017/05/22 09:27:05
Seems like you need this field to be public.
scheglov
2017/05/22 20:36:01
Done.
scheglov
2017/05/22 20:36:01
Done.
|
| final StringIndexer _sourceUriIndexer = new StringIndexer(); |
| Map<DeferredImport, int> _deferredImportIndexer = <DeferredImport, int>{}; |
| @@ -33,7 +33,11 @@ class BinaryPrinter extends Visitor { |
| /// If multiple binaries are to be written based on the same IR, a shared |
| /// [globalIndexer] may be passed in to avoid rebuilding the same indices |
| /// in every printer. |
| - BinaryPrinter(Sink<List<int>> sink) : _sink = new BufferedSink(sink); |
| + BinaryPrinter(Sink<List<int>> sink) : _sink = new BufferedSink(sink) { |
| + _stringIndexer = createStringIndexer(); |
| + } |
| + |
| + StringIndexer createStringIndexer() => new StringIndexer(); |
| void _flush() { |
| _sink.flushAndDestroy(); |
| @@ -132,11 +136,24 @@ class BinaryPrinter extends Visitor { |
| } |
| for (var library in program.libraries) { |
| + if (!shouldWriteLibraryCanonicalNames(library)) continue; |
| visitCanonicalName(library.canonicalName); |
| } |
| + addCanonicalNamesForLinkTable(list); |
| writeList(list, writeCanonicalNameEntry); |
| } |
| + /// Return `true` if all canonical names of the [library] should be written |
| + /// into the link table. If some libraries of the program are skipped, |
| + /// then [addCanonicalNamesForLinkTable] should append all the additional |
| + /// names referenced by the libraries that are written by [writeLibraries]. |
| + bool shouldWriteLibraryCanonicalNames(Library library) => true; |
| + |
| + /// Append additional names for entities that are referenced by the |
| + /// libraries that are written by [writeLibraries], but declared outside |
| + /// of these libraries. |
| + void addCanonicalNamesForLinkTable(List<CanonicalName> list) {} |
| + |
| void writeCanonicalNameEntry(CanonicalName node) { |
| var parent = node.parent; |
| if (parent.isRoot) { |
| @@ -150,15 +167,25 @@ class BinaryPrinter extends Visitor { |
| void writeProgramFile(Program program) { |
| program.computeCanonicalNames(); |
| writeMagicWord(Tag.ProgramFile); |
| - _stringIndexer.scanProgram(program); |
| + buildStringIndex(program); |
| writeStringTable(_stringIndexer); |
| writeUriToSource(program); |
| writeLinkTable(program); |
| - writeList(program.libraries, writeNode); |
| + writeLibraries(program); |
| writeMemberReference(program.mainMethod, allowNull: true); |
| _flush(); |
| } |
| + /// Fill the [_stringIndexer] with all strings we are going to reference. |
| + void buildStringIndex(Program program) { |
| + _stringIndexer.scanProgram(program); |
| + } |
| + |
| + /// Write all of some of the libraries of the [program]. |
| + void writeLibraries(Program program) { |
| + writeList(program.libraries, writeNode); |
| + } |
| + |
| void writeUriToSource(Program program) { |
| program.uriToSource.keys.forEach((uri) { |
| _sourceUriIndexer.put(uri); |
| @@ -1183,8 +1210,19 @@ class StringIndexer extends RecursiveVisitor<Null> { |
| int get numberOfStrings => index.length; |
| - void scanProgram(Node node) { |
| - node.accept(this); |
| + /// Scan all the [program] libraries and [finish] indexing. |
| + void scanProgram(Program program) { |
| + program.accept(this); |
| + finish(); |
| + } |
| + |
| + /// Scan the given library, but don't [finish] indexing yet. |
| + void scanLibrary(Library library) { |
| + library.accept(this); |
| + } |
| + |
| + /// Finish building of the index - sort and assign indices for entries. |
| + void finish() { |
| entries.sort(); |
| for (int i = 0; i < entries.length; ++i) { |
| index[entries[i].value] = i; |