[dart2wasm] Name globals and functions for static fields

Adds new names:

- Globals for static field initialization flags are named as
  "$memberName initialized".

- Globals for static field values are named as "$memberName".

- Static field initialization functions are named as "$memberName
  initializer".

Also documents fields of `Globals` and removes unused field
`_globalInitializers`.

Change-Id: Id54c6e679b2d028917e86184e2913bb51e6b5a97
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396240
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
diff --git a/pkg/dart2wasm/lib/functions.dart b/pkg/dart2wasm/lib/functions.dart
index c48e166..6d5b6f7 100644
--- a/pkg/dart2wasm/lib/functions.dart
+++ b/pkg/dart2wasm/lib/functions.dart
@@ -173,6 +173,7 @@
     if (memberName.endsWith('.')) {
       memberName = memberName.substring(0, memberName.length - 1);
     }
+
     if (target.isTypeCheckerReference) {
       if (member is Field || (member is Procedure && member.isSetter)) {
         return '$memberName setter type checker';
@@ -181,6 +182,10 @@
       }
     }
 
+    if (member is Field) {
+      return '$memberName initializer';
+    }
+
     if (target.isInitializerReference) {
       return 'new $memberName (initializer)';
     } else if (target.isConstructorBodyReference) {
diff --git a/pkg/dart2wasm/lib/globals.dart b/pkg/dart2wasm/lib/globals.dart
index db06ac8..8e7cd2e 100644
--- a/pkg/dart2wasm/lib/globals.dart
+++ b/pkg/dart2wasm/lib/globals.dart
@@ -12,10 +12,16 @@
 class Globals {
   final Translator translator;
 
+  /// Maps a static field to its global holding the field value.
   final Map<Field, w.GlobalBuilder> _globals = {};
+
+  /// When a global is read from a module other than the module defining it,
+  /// this maps the global to the getter function defined and exported in
+  /// the defining module.
   final Map<w.Global, w.BaseFunction> _globalGetters = {};
-  final Map<Field, w.BaseFunction> _globalInitializers = {};
+
   final Map<Field, w.Global> _globalInitializedFlag = {};
+
   late final WasmGlobalImporter _globalsModuleMap =
       WasmGlobalImporter(translator, 'global');
 
@@ -68,37 +74,42 @@
     assert(!field.isLate);
     return _globals.putIfAbsent(field, () {
       final Constant? init = _getConstantInitializer(field);
-      w.ValueType type = translator.translateTypeOfField(field);
+      w.ValueType fieldType = translator.translateTypeOfField(field);
       final module = translator.moduleForReference(field.fieldReference);
+      final memberName = field.toString();
       if (init != null &&
           !(translator.constants.ensureConstant(init)?.isLazy ?? false)) {
         // Initialized to a constant
-        final global =
-            module.globals.define(w.GlobalType(type, mutable: !field.isFinal));
+        final global = module.globals.define(
+            w.GlobalType(fieldType, mutable: !field.isFinal), memberName);
         translator.constants
-            .instantiateConstant(global.initializer, init, type);
+            .instantiateConstant(global.initializer, init, fieldType);
         global.initializer.end();
         return global;
       } else {
-        if (type is w.RefType && !type.nullable) {
+        final w.ValueType globalType;
+        if (fieldType is w.RefType && !fieldType.nullable) {
           // Null signals uninitialized
-          type = type.withNullability(true);
+          globalType = fieldType.withNullability(true);
         } else {
           // Explicit initialization flag
-          final flag = module.globals.define(w.GlobalType(w.NumType.i32));
+          globalType = fieldType;
+          final flag = module.globals
+              .define(w.GlobalType(w.NumType.i32), "$memberName initialized");
           flag.initializer.i32_const(0);
           flag.initializer.end();
           _globalInitializedFlag[field] = flag;
         }
 
-        final global = module.globals.define(w.GlobalType(type));
+        final global =
+            module.globals.define(w.GlobalType(globalType), memberName);
         translator
             .getDummyValuesCollectorForModule(module)
-            .instantiateDummyValue(global.initializer, type);
+            .instantiateDummyValue(global.initializer, globalType);
         global.initializer.end();
 
-        _globalInitializers[field] =
-            translator.functions.getFunction(field.fieldReference);
+        // Add initializer function to the compilation queue.
+        translator.functions.getFunction(field.fieldReference);
         return global;
       }
     });
@@ -108,7 +119,6 @@
   /// field has been initialized, if such a flag global is needed.
   ///
   /// Note that [getGlobalForStaticField] must have been called for the field beforehand.
-  w.Global? getGlobalInitializedFlag(Field variable) {
-    return _globalInitializedFlag[variable];
-  }
+  w.Global? getGlobalInitializedFlag(Field variable) =>
+      _globalInitializedFlag[variable];
 }