[dart2js] Migrate js_backend/string_reference.dart

Change-Id: I817aff48464131e24284be6da8a7da96aab6e0b7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/245620
Reviewed-by: Nate Biggs <natebiggs@google.com>
Reviewed-by: Joshua Litt <joshualitt@google.com>
Commit-Queue: Stephen Adams <sra@google.com>
diff --git a/pkg/compiler/lib/src/js_backend/string_reference.dart b/pkg/compiler/lib/src/js_backend/string_reference.dart
index 966223b..a07a756 100644
--- a/pkg/compiler/lib/src/js_backend/string_reference.dart
+++ b/pkg/compiler/lib/src/js_backend/string_reference.dart
@@ -2,8 +2,6 @@
 // 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.
 
-// @dart = 2.10
-
 /// StringReferences are 'holes' in the generated JavaScript that are filled in
 /// by the emitter with code to access a large string.
 ///
@@ -64,7 +62,7 @@
 
 import '../constants/values.dart' show StringConstantValue;
 import '../js/js.dart' as js;
-import '../serialization/serialization.dart';
+import '../serialization/serialization_interfaces.dart';
 import '../util/util.dart' show Hashing;
 import 'frequency_assignment.dart';
 import 'name_sequence.dart';
@@ -95,10 +93,10 @@
 
   final StringConstantValue constant;
 
-  js.Expression _value;
+  js.Expression? _value;
 
   @override
-  final js.JavaScriptNodeSourceInformation sourceInformation;
+  final js.JavaScriptNodeSourceInformation? sourceInformation;
 
   StringReference(this.constant) : sourceInformation = null;
   StringReference._(this.constant, this._value, this.sourceInformation);
@@ -117,14 +115,15 @@
   }
 
   set value(js.Expression value) {
-    assert(!isFinalized && value != null);
+    assert(!isFinalized);
+    assert((value as dynamic) != null); // TODO(48820): Remove when sound.
     _value = value;
   }
 
   @override
   js.Expression get value {
     assert(isFinalized, 'StringReference is unassigned');
-    return _value;
+    return _value!;
   }
 
   @override
@@ -137,14 +136,14 @@
 
   @override
   StringReference withSourceInformation(
-      js.JavaScriptNodeSourceInformation newSourceInformation) {
+      js.JavaScriptNodeSourceInformation? newSourceInformation) {
     if (newSourceInformation == sourceInformation) return this;
     if (newSourceInformation == null) return this;
     return StringReference._(constant, _value, newSourceInformation);
   }
 
   @override
-  Iterable<js.Node> get containedNodes => isFinalized ? [_value] : const [];
+  Iterable<js.Node> get containedNodes => isFinalized ? [_value!] : const [];
 
   @override
   String nonfinalizedDebugText() {
@@ -175,23 +174,24 @@
 /// and initializes the variable.
 class StringReferenceResource extends js.DeferredStatement
     implements js.AstContainer {
-  js.Statement _statement;
+  js.Statement? _statement;
 
   @override
-  final js.JavaScriptNodeSourceInformation sourceInformation;
+  final js.JavaScriptNodeSourceInformation? sourceInformation;
 
   StringReferenceResource() : sourceInformation = null;
   StringReferenceResource._(this._statement, this.sourceInformation);
 
   set statement(js.Statement statement) {
-    assert(!isFinalized && statement != null);
+    assert(!isFinalized);
+    assert((statement as dynamic) != null); // TODO(48820): Remove when sound.
     _statement = statement;
   }
 
   @override
   js.Statement get statement {
     assert(isFinalized, 'StringReferenceResource is unassigned');
-    return _statement;
+    return _statement!;
   }
 
   @override
@@ -199,14 +199,15 @@
 
   @override
   StringReferenceResource withSourceInformation(
-      js.JavaScriptNodeSourceInformation newSourceInformation) {
+      js.JavaScriptNodeSourceInformation? newSourceInformation) {
     if (newSourceInformation == sourceInformation) return this;
     if (newSourceInformation == null) return this;
     return StringReferenceResource._(_statement, newSourceInformation);
   }
 
   @override
-  Iterable<js.Node> get containedNodes => isFinalized ? [_statement] : const [];
+  Iterable<js.Node> get containedNodes =>
+      isFinalized ? [_statement!] : const [];
 
   @override
   void visitChildren<T>(js.NodeVisitor<T> visitor) {
@@ -233,8 +234,8 @@
   final bool _minify;
   final int shortestSharedLength; // Configurable for testing.
 
-  /*late final*/ _StringReferenceCollectorVisitor _visitor;
-  StringReferenceResource _resource;
+  late final _StringReferenceCollectorVisitor _visitor;
+  StringReferenceResource? _resource;
 
   /// Maps the recipe (type expression) to the references with the same recipe.
   /// Much of the algorithm's state is stored in the _ReferenceSet objects.
@@ -295,20 +296,20 @@
     List<js.Property> properties = [];
     for (_ReferenceSet referenceSet in referenceSetsUsingProperties) {
       String string = referenceSet.constant.stringValue;
-      var propertyName = js.string(referenceSet.propertyName);
+      var propertyName = js.string(referenceSet.propertyName!);
       properties.add(js.Property(propertyName, js.string(string)));
-      var access = js.js('#.#', [holderLocalName, propertyName]);
+      js.Expression access = js.js('#.#', [holderLocalName, propertyName]);
       for (StringReference ref in referenceSet._references) {
         ref.value = access;
       }
     }
 
     if (properties.isEmpty) {
-      _resource.statement = js.Block.empty();
+      _resource!.statement = js.Block.empty();
     } else {
       js.Expression initializer =
           js.ObjectInitializer(properties, isOneLiner: false);
-      _resource.statement = js.js.statement(
+      _resource!.statement = js.js.statement(
           r'var # = #', [js.VariableDeclaration(holderLocalName), initializer]);
     }
   }
@@ -377,7 +378,7 @@
         referencesByFrequency[index].propertyName = name;
       } else {
         var refSet = referencesByFrequency[index];
-        refSet.propertyName = name + '_' + refSet.name;
+        refSet.propertyName = name + '_' + refSet.name!;
       }
     }
 
@@ -401,10 +402,10 @@
   /// Characteristic name of the recipe - this can be used as a property name
   /// for emitting unminified code, and as a stable hash source for minified
   /// names.  [name] is `null` if [recipe] should always be generated at use.
-  String name;
+  String? name;
 
   /// Property name for 'indexing' into the precomputed types.
-  String propertyName;
+  String? propertyName;
 
   /// A stable hash code that can be used for picking stable minified names.
   int hash = 0;
diff --git a/pkg/compiler/lib/src/js_emitter/startup_emitter/fragment_emitter.dart b/pkg/compiler/lib/src/js_emitter/startup_emitter/fragment_emitter.dart
index 8f32690..5da86d0 100644
--- a/pkg/compiler/lib/src/js_emitter/startup_emitter/fragment_emitter.dart
+++ b/pkg/compiler/lib/src/js_emitter/startup_emitter/fragment_emitter.dart
@@ -807,7 +807,7 @@
         resourceName,
         fragment.fragments,
         holderCode);
-    js.Expression code = js.js(_deferredBoilerplate, {
+    js.Expression /*!*/ code = js.js(_deferredBoilerplate, {
       // TODO(floitsch): don't just reference 'init'.
       'embeddedGlobalsObject': js.Parameter('init'),
       'isCollectingRuntimeMetrics': _options.experimentalTrackAllocations,
diff --git a/pkg/compiler/lib/src/serialization/serialization_interfaces.dart b/pkg/compiler/lib/src/serialization/serialization_interfaces.dart
index 5fac8e9..2285eab 100644
--- a/pkg/compiler/lib/src/serialization/serialization_interfaces.dart
+++ b/pkg/compiler/lib/src/serialization/serialization_interfaces.dart
@@ -4,6 +4,7 @@
 
 import 'package:kernel/ast.dart' as ir show DartType, Member, TreeNode;
 
+import '../constants/values.dart' show ConstantValue;
 import '../elements/entities.dart';
 import '../elements/types.dart' show DartType;
 
@@ -84,6 +85,8 @@
 
   void writeList<E extends Object>(Iterable<E>? values, void f(E value),
       {bool allowNull = false});
+
+  void writeConstant(ConstantValue value);
 }
 
 /// Migrated interface for methods of DataSourceReader.
@@ -129,4 +132,6 @@
 
   List<E> readList<E extends Object>(E f());
   List<E>? readListOrNull<E extends Object>(E f());
+
+  ConstantValue readConstant();
 }
diff --git a/pkg/compiler/lib/src/serialization/sink.dart b/pkg/compiler/lib/src/serialization/sink.dart
index cc836b4..077bfd8 100644
--- a/pkg/compiler/lib/src/serialization/sink.dart
+++ b/pkg/compiler/lib/src/serialization/sink.dart
@@ -925,6 +925,7 @@
   }
 
   /// Writes the constant [value] to this data sink.
+  @override
   void writeConstant(ConstantValue value) {
     _writeDataKind(DataKind.constant);
     _writeConstant(value);
diff --git a/pkg/compiler/lib/src/serialization/source.dart b/pkg/compiler/lib/src/serialization/source.dart
index a0e4284..81e477b 100644
--- a/pkg/compiler/lib/src/serialization/source.dart
+++ b/pkg/compiler/lib/src/serialization/source.dart
@@ -1098,6 +1098,7 @@
   }
 
   /// Reads a constant value from this data source.
+  @override
   ConstantValue readConstant() {
     _checkDataKind(DataKind.constant);
     return _readConstant();