Elements. Remove Reference.element (misnomer now), now FragmentImpl cannot have reference.

Change-Id: I928abfe136bda2bc49884f9ec79a8bcae772ac13
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/435446
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
diff --git a/pkg/analyzer/lib/src/summary2/ast_binary_reader.dart b/pkg/analyzer/lib/src/summary2/ast_binary_reader.dart
index 27fb037..9780536 100644
--- a/pkg/analyzer/lib/src/summary2/ast_binary_reader.dart
+++ b/pkg/analyzer/lib/src/summary2/ast_binary_reader.dart
@@ -283,7 +283,8 @@
 
   AugmentedExpression _readAugmentedExpression() {
     var node = AugmentedExpressionImpl(augmentedKeyword: Tokens.augmented());
-    node.fragment = _reader.readFragmentOrMember() as FragmentImpl?;
+    // TODO(scheglov): restore when the feature is fully specified
+    // node.fragment = _reader.readFragmentOrMember() as FragmentImpl?;
     _readExpressionResolution(node);
     return node;
   }
@@ -297,7 +298,8 @@
       typeArguments: typeArguments,
       arguments: arguments,
     );
-    node.fragment = _reader.readFragmentOrMember() as ExecutableFragmentImpl?;
+    // TODO(scheglov): restore when the feature is fully specified
+    // node.fragment = _reader.readFragmentOrMember() as ExecutableFragmentImpl?;
     _readExpressionResolution(node);
     return node;
   }
diff --git a/pkg/analyzer/lib/src/summary2/ast_binary_writer.dart b/pkg/analyzer/lib/src/summary2/ast_binary_writer.dart
index da9df25..3ff6f68 100644
--- a/pkg/analyzer/lib/src/summary2/ast_binary_writer.dart
+++ b/pkg/analyzer/lib/src/summary2/ast_binary_writer.dart
@@ -94,7 +94,8 @@
   @override
   void visitAugmentedExpression(covariant AugmentedExpressionImpl node) {
     _writeByte(Tag.AugmentedExpression);
-    _sink.writeFragmentOrMember(node.fragment);
+    // TODO(scheglov): restore when the feature is fully specified
+    // _sink.writeFragmentOrMember(node.fragment);
     _storeExpression(node);
   }
 
@@ -103,7 +104,8 @@
     _writeByte(Tag.AugmentedInvocation);
     _writeOptionalNode(node.typeArguments);
     _writeNode(node.arguments);
-    _sink.writeFragmentOrMember(node.fragment);
+    // TODO(scheglov): restore when the feature is fully specified
+    // _sink.writeFragmentOrMember(node.fragment);
     _storeExpression(node);
   }
 
diff --git a/pkg/analyzer/lib/src/summary2/bundle_reader.dart b/pkg/analyzer/lib/src/summary2/bundle_reader.dart
index a1b43ed..de18ca9 100644
--- a/pkg/analyzer/lib/src/summary2/bundle_reader.dart
+++ b/pkg/analyzer/lib/src/summary2/bundle_reader.dart
@@ -1535,8 +1535,8 @@
 
   late LibraryFragmentImpl currentLibraryFragment;
 
-  /// The stack of [TypeParameterElementImpl]s and [FormalParameterImpl] that
-  /// are available in the scope of [readFragmentOrMember] and [readType].
+  /// The stack of [TypeParameterElementImpl]s and [FormalParameterElementImpl]s
+  /// that are available in the scope of [readElement] and [readType].
   ///
   /// This stack is shared with the client of the reader, and update mostly
   /// by the client. However it is also updated during [_readFunctionType].
@@ -1544,31 +1544,6 @@
 
   ResolutionReader(this._elementFactory, this._referenceReader, this._reader);
 
-  void applyToFormalParameterFragments(
-    List<FormalParameterFragmentImpl> parameters,
-  ) {
-    for (var parameter in parameters) {
-      parameter.metadata = _readMetadata(unitElement: currentLibraryFragment);
-      _readTypeParameters2(
-        currentLibraryFragment,
-        this,
-        parameter.typeParameters,
-      );
-      applyToFormalParameterFragments(parameter.parameters);
-      parameter.type = readRequiredType();
-      if (parameter is ConstVariableFragment) {
-        var defaultParameter = parameter as ConstVariableFragment;
-        var initializer = readOptionalExpression();
-        if (initializer != null) {
-          defaultParameter.constantInitializer = initializer;
-        }
-      }
-      if (parameter is FieldFormalParameterFragmentImpl) {
-        parameter.field = readFragmentOrMember() as FieldFragmentImpl?;
-      }
-    }
-  }
-
   LibraryElementImpl libraryOfUri(Uri uri) {
     return _elementFactory.libraryOfUri2(uri);
   }
@@ -1639,44 +1614,6 @@
     return _reader.readEnum(values);
   }
 
-  FragmentOrMember? readFragmentOrMember() {
-    var memberFlags = _reader.readByte();
-    var fragment = _readFragmentImpl();
-
-    if (fragment == null) {
-      return null;
-    }
-
-    if (memberFlags == Tag.RawElement) {
-      return fragment;
-    }
-
-    if (memberFlags == Tag.MemberWithTypeArguments) {
-      var enclosing = fragment.enclosingElement3 as InstanceFragmentImpl;
-
-      var firstFragment = enclosing.element.firstFragment;
-      var declarationTypeParameters =
-          firstFragment.typeParameters.map((tp) => tp.asElement2).toList();
-
-      var substitution = Substitution.empty;
-      var typeArguments = _readTypeList();
-      if (typeArguments.isNotEmpty) {
-        substitution = Substitution.fromPairs2(
-          declarationTypeParameters,
-          typeArguments,
-        );
-      }
-
-      if (fragment is ExecutableFragmentImpl) {
-        return ExecutableMember.from2(fragment, substitution);
-      } else {
-        fragment as FieldFragmentImpl;
-        return FieldMember.from2(fragment, substitution);
-      }
-    }
-
-    throw UnimplementedError('memberFlags: $memberFlags');
-  }
 
   Map<K, V> readMap<K, V>({
     required K Function() readKey,
@@ -1960,20 +1897,6 @@
     });
   }
 
-  FragmentImpl? _readFragmentImpl() {
-    var index = _reader.readUInt30();
-
-    if ((index & 0x1) == 0x1) {
-      // TODO(scheglov): remove?
-      throw UnimplementedError();
-    }
-
-    var referenceIndex = index >> 1;
-    var reference = _referenceReader.referenceOfIndex(referenceIndex);
-
-    return _elementFactory.elementOfReference(reference);
-  }
-
   String? _readFragmentName() {
     return _reader.readOptionalStringReference();
   }
@@ -2091,19 +2014,6 @@
     return typeParameters;
   }
 
-  void _readTypeParameters2(
-    LibraryFragmentImpl unitElement,
-    ResolutionReader reader,
-    List<TypeParameterFragmentImpl> typeParameters,
-  ) {
-    reader._addTypeParameters(typeParameters);
-    for (var typeParameter in typeParameters) {
-      typeParameter.metadata = reader._readMetadata(unitElement: unitElement);
-      typeParameter.bound = reader.readType();
-      typeParameter.defaultType = reader.readType();
-    }
-  }
-
   static ParameterKind _formalParameterKind(int encoding) {
     if (encoding == Tag.ParameterKindRequiredPositional) {
       return ParameterKind.REQUIRED;
diff --git a/pkg/analyzer/lib/src/summary2/bundle_writer.dart b/pkg/analyzer/lib/src/summary2/bundle_writer.dart
index 4d69e26..4b13672 100644
--- a/pkg/analyzer/lib/src/summary2/bundle_writer.dart
+++ b/pkg/analyzer/lib/src/summary2/bundle_writer.dart
@@ -997,29 +997,6 @@
     }
   }
 
-  // TODO(scheglov): Triage places where we write elements.
-  // Some of then cannot be members, e.g. type names.
-  void writeFragmentOrMember(FragmentOrMember? element) {
-    if (element == null) {
-      writeByte(Tag.RawElement);
-      writeUInt30(0);
-    } else if (element is Member) {
-      var declaration = element.declaration;
-
-      var typeArguments = _enclosingClassTypeArguments(
-        declaration.asElement2!,
-        element.substitution.map,
-      );
-
-      writeByte(Tag.MemberWithTypeArguments);
-      _writeFragmentImpl(declaration);
-      _writeTypeList(typeArguments);
-    } else {
-      writeByte(Tag.RawElement);
-      _writeFragmentImpl(element as FragmentImpl);
-    }
-  }
-
   void writeOptionalTypeList(List<DartType>? types) {
     if (types != null) {
       writeBool(true);
@@ -1128,13 +1105,6 @@
     }
   }
 
-  void _writeFragmentImpl(FragmentImpl element) {
-    // TODO(scheglov): remove?
-    throw UnimplementedError();
-    // var elementIndex = _indexOfElement(element);
-    // writeUInt30(elementIndex);
-  }
-
   void _writeFragmentName(Fragment fragment) {
     _writeOptionalStringReference(fragment.name2);
   }
diff --git a/pkg/analyzer/lib/src/summary2/linked_element_factory.dart b/pkg/analyzer/lib/src/summary2/linked_element_factory.dart
index 75bb41e..9b0d425 100644
--- a/pkg/analyzer/lib/src/summary2/linked_element_factory.dart
+++ b/pkg/analyzer/lib/src/summary2/linked_element_factory.dart
@@ -14,7 +14,6 @@
 import 'package:analyzer/src/summary2/bundle_reader.dart';
 import 'package:analyzer/src/summary2/export.dart';
 import 'package:analyzer/src/summary2/reference.dart';
-import 'package:analyzer/src/utilities/extensions/element.dart';
 import 'package:analyzer/src/utilities/uri_cache.dart';
 import 'package:meta/meta.dart';
 
@@ -155,37 +154,10 @@
 
   void dispose() {
     for (var libraryReference in rootReference.children) {
-      _disposeLibrary(libraryReference.element);
+      _disposeLibrary(libraryReference.element2);
     }
   }
 
-  // TODO(scheglov): Why would this method return `null`?
-  FragmentImpl? elementOfReference(Reference reference) {
-    if (reference.element case var element?) {
-      return element;
-    }
-    if (reference.parent == null) {
-      return null;
-    }
-
-    if (reference.isLibrary) {
-      var uri = uriCache.parse(reference.name);
-      createLibraryElementForReading(uri);
-      return null;
-    }
-
-    var element = reference.element;
-    if (element == null) {
-      throw StateError('Expected existing element: $reference');
-    }
-    return element;
-  }
-
-  // TODO(scheglov): Why would this method return `null`?
-  Element? elementOfReference2(Reference reference) {
-    return elementOfReference(reference)?.asElement2;
-  }
-
   Element elementOfReference3(Reference reference) {
     if (reference.element2 case var element?) {
       return element;
@@ -214,15 +186,6 @@
     return element;
   }
 
-  bool hasLibrary(Uri uri) {
-    // We already have the element, linked or read.
-    if (rootReference['$uri']?.element is LibraryElementImpl) {
-      return true;
-    }
-    // No element yet, but we know how to read it.
-    return _libraryReaders[uri] != null;
-  }
-
   LibraryElementImpl? libraryOfUri(Uri uri) {
     var reference = rootReference.getChild('$uri');
     if (reference.element2 case LibraryElementImpl element) {
@@ -254,7 +217,7 @@
       _libraryReaders.remove(uri);
       libraryManifests.remove(uri);
       var libraryReference = rootReference.removeChild('$uri');
-      _disposeLibrary(libraryReference?.element);
+      _disposeLibrary(libraryReference?.element2);
     }
 
     analysisSession.classHierarchy.removeOfLibraries(uriSet);
@@ -306,5 +269,5 @@
     libraryElement.hasTypeProviderSystemSet = true;
   }
 
-  void _disposeLibrary(FragmentImpl? libraryElement) {}
+  void _disposeLibrary(ElementImpl? libraryElement) {}
 }
diff --git a/pkg/analyzer/lib/src/summary2/reference.dart b/pkg/analyzer/lib/src/summary2/reference.dart
index 5b9aeed..9ce446f 100644
--- a/pkg/analyzer/lib/src/summary2/reference.dart
+++ b/pkg/analyzer/lib/src/summary2/reference.dart
@@ -2,14 +2,13 @@
 // 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.
 
-import 'package:analyzer/dart/element/element.dart';
 import 'package:analyzer/src/dart/element/element.dart';
 import 'package:meta/meta.dart';
 
-/// Indirection between a name and the corresponding [FragmentImpl].
+/// Indirection between a name and the corresponding [ElementImpl].
 ///
 /// References are organized in a prefix tree.
-/// Each reference knows its parent, children, and the [FragmentImpl].
+/// Each reference knows its parent, children, and the [ElementImpl].
 ///
 ///      Library:
 ///         URI of library
@@ -24,7 +23,7 @@
 ///         "@method"
 ///         Name of the method
 ///
-/// There is only one reference object per [FragmentImpl].
+/// There is only one reference object per [ElementImpl].
 class Reference {
   /// The name of the container used for duplicate declarations.
   static const _defName = '@def';
@@ -35,11 +34,8 @@
   /// The simple name of the reference in its [parent].
   String name;
 
-  /// The corresponding [FragmentImpl], or `null` if a named container.
-  FragmentImpl? element;
-
-  /// The corresponding [Element], or `null` if a named container.
-  Element? element2;
+  /// The corresponding [ElementImpl], or `null` if a named container.
+  ElementImpl? element2;
 
   /// Temporary index used during serialization and linking.
   int? index;