Simplify ConstantEvaluationTarget by deleting context, library, library2, librarySource, source. These are all now accessed through a single libraryFragment interface.

Change-Id: Ie4fd477731a3d4787bbcfd13a6ae6d9e96ddf7c6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/438840
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
diff --git a/pkg/analyzer/lib/src/dart/constant/evaluation.dart b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
index 576c8d2..751b204 100644
--- a/pkg/analyzer/lib/src/dart/constant/evaluation.dart
+++ b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
@@ -13,7 +13,6 @@
 import 'package:analyzer/dart/element/nullability_suffix.dart';
 import 'package:analyzer/dart/element/type.dart';
 import 'package:analyzer/error/listener.dart';
-import 'package:analyzer/source/source.dart';
 import 'package:analyzer/src/dart/ast/ast.dart';
 import 'package:analyzer/src/dart/ast/extensions.dart';
 import 'package:analyzer/src/dart/ast/token.dart';
@@ -86,14 +85,15 @@
       constant = element.declaration as ConstantEvaluationTarget;
     }
 
-    var library = constant.library as LibraryElementImpl;
+    var libraryFragment = constant.libraryFragment!;
+    var library = libraryFragment.element;
     if (constant is FormalParameterElementImpl) {
       var defaultValue = constant.constantInitializer2?.expression;
       if (defaultValue != null) {
         var diagnosticListener = RecordingDiagnosticListener();
         var diagnosticReporter = DiagnosticReporter(
           diagnosticListener,
-          constant.source!,
+          libraryFragment.source,
         );
         var constantVisitor = ConstantVisitor(
           this,
@@ -110,7 +110,7 @@
       if (constantInitializer != null) {
         var diagnosticReporter = DiagnosticReporter(
           RecordingDiagnosticListener(),
-          constant.source!,
+          libraryFragment.source,
         );
         var constantVisitor = ConstantVisitor(
           this,
@@ -194,7 +194,7 @@
         var diagnosticListener = RecordingDiagnosticListener();
         var diagnosticReporter = DiagnosticReporter(
           diagnosticListener,
-          constant.source,
+          libraryFragment.source,
         );
         var constantVisitor = ConstantVisitor(
           this,
@@ -437,7 +437,7 @@
     if (constant is VariableElementImpl) {
       DiagnosticReporter diagnosticReporter = DiagnosticReporter(
         RecordingDiagnosticListener(),
-        constant.source!,
+        constant.libraryFragment!.source,
       );
       // TODO(paulberry): It would be really nice if we could extract enough
       // information from the 'cycle' argument to provide the user with a
@@ -536,24 +536,11 @@
 
 /// Interface for constant evaluation targets.
 abstract class ConstantEvaluationTarget {
-  /// Return the [AnalysisContext] which should be used to evaluate this
-  /// constant.
-  AnalysisContext get context;
-
   /// Return whether this constant is evaluated.
   bool get isConstantEvaluated;
 
-  /// The library with this constant.
-  LibraryElement? get library;
-
-  @Deprecated('Use library instead')
-  LibraryElement? get library2;
-
-  /// The library containing this constant.
-  Source? get librarySource;
-
-  /// The source associated with this constant.
-  Source? get source;
+  /// The library fragment with the first fragment of this constant.
+  LibraryFragmentImpl? get libraryFragment;
 }
 
 /// A visitor used to evaluate constant expressions to produce their
diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart
index 81803c0..4835016 100644
--- a/pkg/analyzer/lib/src/dart/element/element.dart
+++ b/pkg/analyzer/lib/src/dart/element/element.dart
@@ -762,9 +762,6 @@
   }
 
   @override
-  AnalysisContext get context => firstFragment.context;
-
-  @override
   String get displayName {
     var className = enclosingElement.name3 ?? '<null>';
     var name = name3 ?? '<null>';
@@ -825,7 +822,7 @@
   }
 
   @override
-  Source? get librarySource => firstFragment.librarySource;
+  LibraryFragmentImpl get libraryFragment => firstFragment.libraryFragment;
 
   @override
   Element get nonSynthetic {
@@ -852,9 +849,6 @@
   }
 
   @override
-  Source? get source => firstFragment.source;
-
-  @override
   ConstructorElementMixin2? get superConstructor2 {
     _ensureReadResolution();
     return _superConstructor;
@@ -1501,8 +1495,8 @@
   @override
   Element? element2;
 
-  /// The compilation unit in which this annotation appears.
-  LibraryFragmentImpl compilationUnit;
+  @override
+  LibraryFragmentImpl libraryFragment;
 
   /// The AST of the annotation itself, cloned from the resolved AST for the
   /// source code.
@@ -1522,9 +1516,9 @@
   // annotations having a valid result as well as unresolved errors.
   List<Diagnostic>? additionalErrors;
 
-  /// Initialize a newly created annotation. The given [compilationUnit] is the
+  /// Initialize a newly created annotation. The given [libraryFragment] is the
   /// compilation unit in which the annotation appears.
-  ElementAnnotationImpl(this.compilationUnit);
+  ElementAnnotationImpl(this.libraryFragment);
 
   @override
   List<Diagnostic> get constantEvaluationErrors {
@@ -1535,7 +1529,7 @@
       // errors because this result contains the most relevant error.
       return [
         Diagnostic.tmp(
-          source: source,
+          source: libraryFragment.source,
           offset: evaluationResult.offset,
           length: evaluationResult.length,
           diagnosticCode: evaluationResult.diagnosticCode,
@@ -1548,9 +1542,6 @@
   }
 
   @override
-  AnalysisContext get context => compilationUnit.library.context;
-
-  @override
   bool get isAlwaysThrows => _isPackageMetaGetter(_alwaysThrowsVariableName);
 
   @override
@@ -1707,28 +1698,13 @@
   );
 
   @override
-  LibraryElementImpl get library => compilationUnit.library;
-
-  @Deprecated('Use library instead')
-  @override
-  LibraryElementImpl get library2 => compilationUnit.library;
-
-  @override
-  LibraryFragmentImpl get libraryFragment => compilationUnit;
-
-  @override
-  Source get librarySource => compilationUnit.librarySource;
-
-  @override
-  Source get source => compilationUnit.source;
-
-  @override
   DartObject? computeConstantValue() {
     if (evaluationResult == null) {
+      var library = libraryFragment.element;
       computeConstants(
-        declaredVariables: context.declaredVariables,
+        declaredVariables: library.context.declaredVariables,
         constants: [this],
-        featureSet: compilationUnit.library.featureSet,
+        featureSet: library.featureSet,
         configuration: ConstantEvaluationConfiguration(),
       );
     }
@@ -10038,18 +10014,11 @@
   }
 
   @override
-  AnalysisContext get context {
-    return (firstFragment as VariableFragmentImpl).context;
-  }
-
-  @override
   bool get isConstantEvaluated => evaluationResult != null;
 
   @override
-  Source? get librarySource => library!.firstFragment.source;
-
-  @override
-  Source? get source => (firstFragment as VariableFragmentImpl).source;
+  LibraryFragmentImpl? get libraryFragment =>
+      firstFragment.libraryFragment as LibraryFragmentImpl?;
 
   /// Return a representation of the value of this variable, forcing the value
   /// to be computed if it had not previously been computed, or `null` if either
@@ -10058,13 +10027,13 @@
   @override
   DartObject? computeConstantValue() {
     if (evaluationResult == null) {
-      var library = this.library;
+      var library = libraryFragment?.element;
       // TODO(scheglov): https://github.com/dart-lang/sdk/issues/47915
       if (library == null) {
         return null;
       }
       computeConstants(
-        declaredVariables: context.declaredVariables,
+        declaredVariables: library.context.declaredVariables,
         constants: [this],
         featureSet: library.featureSet,
         configuration: ConstantEvaluationConfiguration(),