Cleanup of ast_to_binary

Change-Id: Ib065dd32c23c88f64b668a6b9bcefbdb4a8e038d
Reviewed-on: https://dart-review.googlesource.com/c/85241
Reviewed-by: Peter von der Ahé <ahe@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
diff --git a/pkg/kernel/lib/binary/ast_to_binary.dart b/pkg/kernel/lib/binary/ast_to_binary.dart
index 33d8da4..9143687 100644
--- a/pkg/kernel/lib/binary/ast_to_binary.dart
+++ b/pkg/kernel/lib/binary/ast_to_binary.dart
@@ -25,7 +25,6 @@
   final UriIndexer _sourceUriIndexer = new UriIndexer();
   bool _currentlyInNonimplementation = false;
   final List<bool> _sourcesFromRealImplementation = new List<bool>();
-  final Set<Uri> _knownSourceUri = new Set<Uri>();
   Map<LibraryDependency, int> _libraryDependencyIndex =
       <LibraryDependency, int>{};
 
@@ -36,6 +35,7 @@
   final BytesSink _constantsBytesSink;
   BufferedSink _constantsSink;
   BufferedSink _sink;
+  bool includeSources;
 
   List<int> libraryOffsets;
   List<int> classOffsets;
@@ -55,7 +55,8 @@
   ///
   /// The BinaryPrinter will use its own buffer, so the [sink] does not need
   /// one.
-  BinaryPrinter(Sink<List<int>> sink, {StringIndexer stringIndexer})
+  BinaryPrinter(Sink<List<int>> sink,
+      {StringIndexer stringIndexer, this.includeSources = true})
       : _mainSink = new BufferedSink(sink),
         _metadataSink = new BufferedSink(new BytesSink()),
         _constantsBytesSink = new BytesSink(),
@@ -219,11 +220,6 @@
     type.accept(this);
   }
 
-  // The currently active file uri where we are writing [TreeNode]s from.  If
-  // this is set to `null` we cannot write file offsets.  The [writeOffset]
-  // helper function will ensure this.
-  Uri _activeFileUri;
-
   // Returns the new active file uri.
   Uri writeUriReference(Uri uri) {
     final int index = _sourceUriIndexer.put(uri);
@@ -323,7 +319,6 @@
     writeUInt32(Tag.ComponentFile);
     writeUInt32(Tag.BinaryFormatVersion);
     indexLinkTable(component);
-    indexUris(component);
     _collectMetadata(component);
     if (_metadataSubsections != null) {
       _writeNodeMetadataImpl(component, componentOffset);
@@ -495,10 +490,6 @@
     writeUInt32(getBufferOffset() + 4); // total size.
   }
 
-  void indexUris(Component component) {
-    _knownSourceUri.addAll(component.uriToSource.keys);
-  }
-
   void writeUriToSource(Map<Uri, Source> uriToSource) {
     _binaryOffsetForSourceTable = getBufferOffset();
 
@@ -511,7 +502,7 @@
     Utf8Encoder utf8Encoder = const Utf8Encoder();
     for (Uri uri in _sourceUriIndexer.index.keys) {
       index[i] = getBufferOffset();
-      Source source = ((_knownSourceUri.contains(uri) &&
+      Source source = ((includeSources &&
                   _sourcesFromRealImplementation.length > i &&
                   _sourcesFromRealImplementation[i] == true)
               ? uriToSource[uri]
@@ -582,10 +573,6 @@
   }
 
   writeOffset(int offset) {
-    if (_activeFileUri == null) {
-      offset = TreeNode.noOffset;
-    }
-
     // TODO(jensj): Delta-encoding.
     // File offset ranges from -1 and up,
     // but is here saved as unsigned (thus the +1)
@@ -627,11 +614,7 @@
     writeByte(insideExternalLibrary ? 1 : 0);
     writeCanonicalNameReference(getCanonicalNameOfLibrary(node));
     writeStringReference(node.name ?? '');
-    // TODO(jensj): We save (almost) the same URI twice.
-
-    final Uri activeFileUriSaved = _activeFileUri;
-    _activeFileUri = writeUriReference(node.fileUri);
-
+    writeUriReference(node.fileUri);
     writeAnnotationList(node.annotations);
     writeLibraryDependencies(node);
     writeAdditionalExports(node.additionalExports);
@@ -645,8 +628,6 @@
     writeNodeList(node.procedures);
     procedureOffsets.add(getBufferOffset());
 
-    _activeFileUri = activeFileUriSaved;
-
     // Fixed-size ints at the end used as an index.
     assert(classOffsets.length > 0);
     for (int offset in classOffsets) {
@@ -716,10 +697,7 @@
   void visitTypedef(Typedef node) {
     _variableIndexer ??= new VariableIndexer();
     writeCanonicalNameReference(getCanonicalNameOfTypedef(node));
-
-    final Uri activeFileUriSaved = _activeFileUri;
-    _activeFileUri = writeUriReference(node.fileUri);
-
+    writeUriReference(node.fileUri);
     writeOffset(node.fileOffset);
     writeStringReference(node.name);
     writeAnnotationList(node.annotations);
@@ -735,8 +713,6 @@
 
     leaveScope(typeParameters: node.typeParametersOfFunctionType);
     leaveScope(typeParameters: node.typeParameters, variableScope: true);
-
-    _activeFileUri = activeFileUriSaved;
   }
 
   void writeAnnotation(Expression annotation) {
@@ -772,10 +748,7 @@
     }
     writeByte(Tag.Class);
     writeCanonicalNameReference(getCanonicalNameOfClass(node));
-
-    final Uri activeFileUriSaved = _activeFileUri;
-    _activeFileUri = writeUriReference(node.fileUri);
-
+    writeUriReference(node.fileUri);
     writeOffset(node.startFileOffset);
     writeOffset(node.fileOffset);
     writeOffset(node.fileEndOffset);
@@ -797,8 +770,6 @@
     writeNodeList(node.redirectingFactoryConstructors);
     leaveScope(typeParameters: node.typeParameters);
 
-    _activeFileUri = activeFileUriSaved;
-
     assert(procedureOffsets.length > 0);
     for (int offset in procedureOffsets) {
       writeUInt32(offset);
@@ -817,10 +788,7 @@
     enterScope(memberScope: true);
     writeByte(Tag.Constructor);
     writeCanonicalNameReference(getCanonicalNameOfMember(node));
-
-    final Uri activeFileUriSaved = _activeFileUri;
-    _activeFileUri = writeUriReference(node.fileUri);
-
+    writeUriReference(node.fileUri);
     writeOffset(node.startFileOffset);
     writeOffset(node.fileOffset);
     writeOffset(node.fileEndOffset);
@@ -836,8 +804,6 @@
         node.function.namedParameters.length);
     writeNodeList(node.initializers);
 
-    _activeFileUri = activeFileUriSaved;
-
     leaveScope(memberScope: true);
   }
 
@@ -858,10 +824,7 @@
     enterScope(memberScope: true);
     writeByte(Tag.Procedure);
     writeCanonicalNameReference(getCanonicalNameOfMember(node));
-
-    final Uri activeFileUriSaved = _activeFileUri;
-    _activeFileUri = writeUriReference(node.fileUri);
-
+    writeUriReference(node.fileUri);
     writeOffset(node.startFileOffset);
     writeOffset(node.fileOffset);
     writeOffset(node.fileEndOffset);
@@ -872,9 +835,6 @@
     writeOptionalReference(node.forwardingStubSuperTargetReference);
     writeOptionalReference(node.forwardingStubInterfaceTargetReference);
     writeOptionalNode(node.function);
-
-    _activeFileUri = activeFileUriSaved;
-
     leaveScope(memberScope: true);
 
     _currentlyInNonimplementation = currentlyInNonimplementationSaved;
@@ -890,10 +850,7 @@
     enterScope(memberScope: true);
     writeByte(Tag.Field);
     writeCanonicalNameReference(getCanonicalNameOfMember(node));
-
-    final Uri activeFileUriSaved = _activeFileUri;
-    _activeFileUri = writeUriReference(node.fileUri);
-
+    writeUriReference(node.fileUri);
     writeOffset(node.fileOffset);
     writeOffset(node.fileEndOffset);
     writeByte(node.flags);
@@ -901,9 +858,6 @@
     writeAnnotationList(node.annotations);
     writeNode(node.type);
     writeOptionalNode(node.initializer);
-
-    _activeFileUri = activeFileUriSaved;
-
     leaveScope(memberScope: true);
   }
 
@@ -918,10 +872,7 @@
         memberScope: true,
         variableScope: true);
     writeCanonicalNameReference(getCanonicalNameOfMember(node));
-
-    final Uri activeFileUriSaved = _activeFileUri;
-    _activeFileUri = writeUriReference(node.fileUri);
-
+    writeUriReference(node.fileUri);
     writeOffset(node.fileOffset);
     writeOffset(node.fileEndOffset);
     writeByte(node.flags);
@@ -936,8 +887,6 @@
     writeVariableDeclarationList(node.positionalParameters);
     writeVariableDeclarationList(node.namedParameters);
 
-    _activeFileUri = activeFileUriSaved;
-
     leaveScope(
         typeParameters: node.typeParameters,
         memberScope: true,
diff --git a/pkg/kernel/lib/binary/limited_ast_to_binary.dart b/pkg/kernel/lib/binary/limited_ast_to_binary.dart
index 82fe07d..bd00832 100644
--- a/pkg/kernel/lib/binary/limited_ast_to_binary.dart
+++ b/pkg/kernel/lib/binary/limited_ast_to_binary.dart
@@ -24,7 +24,7 @@
 
   LimitedBinaryPrinter(
       Sink<List<int>> sink, this.predicate, this.excludeUriToSource)
-      : super(sink);
+      : super(sink, includeSources: !excludeUriToSource);
 
   @override
   void computeCanonicalNames(Component component) {
@@ -60,13 +60,4 @@
     var librariesToWrite = libraries.where(predicate).toList();
     super.writeComponentIndex(component, librariesToWrite);
   }
-
-  @override
-  void indexUris(Component component) {
-    if (!excludeUriToSource) {
-      super.indexUris(component);
-    } else {
-      // We pretend not to know any uris, thereby excluding all sources.
-    }
-  }
 }