[cfe] Remove SourceCompilationUnit.sourceLibraryBuilder

Change-Id: I8ea290f03c2a15884b0f47e89ab7941d56b80b45
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/373463
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
diff --git a/pkg/front_end/lib/src/builder/library_builder.dart b/pkg/front_end/lib/src/builder/library_builder.dart
index f508d99..1b98ac2 100644
--- a/pkg/front_end/lib/src/builder/library_builder.dart
+++ b/pkg/front_end/lib/src/builder/library_builder.dart
@@ -5,7 +5,7 @@
 library fasta.library_builder;
 
 import 'package:_fe_analyzer_shared/src/messages/severity.dart' show Severity;
-import 'package:kernel/ast.dart' show Class, Library;
+import 'package:kernel/ast.dart' show Class, Library, Version;
 
 import '../api_prototype/experimental_flags.dart';
 import '../base/combinator.dart' show CombinatorBuilder;
@@ -18,6 +18,7 @@
         LocatedMessage,
         Message,
         ProblemReporting,
+        noLength,
         templateInternalProblemConstructorNotFound,
         templateInternalProblemNotFoundIn,
         templateInternalProblemPrivateConstructorAccess;
@@ -48,7 +49,9 @@
   /// This is the canonical uri for the compilation unit, for instance
   /// 'dart:core'.
   Uri get importUri;
+
   Uri get fileUri;
+
   bool get isSynthetic;
 
   /// If true, the library is not supported through the 'dart.library.*' value
@@ -109,11 +112,41 @@
   @override
   SourceLoader get loader;
 
-  // TODO(johnniwinther): Remove this.
-  SourceLibraryBuilder get sourceLibraryBuilder;
-
   OffsetMap get offsetMap;
 
+  /// The language version of this compilation unit as defined by the language
+  /// version of the package it belongs to, if present, or the current language
+  /// version otherwise.
+  ///
+  /// This language version will be used as the language version for the
+  /// compilation unit if the compilation unit does not contain an explicit
+  /// `@dart=` annotation.
+  LanguageVersion get packageLanguageVersion;
+
+  /// Set the language version to an explicit major and minor version.
+  ///
+  /// The default language version specified by the `package_config.json` file
+  /// is passed to the constructor, but the library can have source code that
+  /// specifies another one which should be supported.
+  ///
+  /// Only the first registered language version is used.
+  ///
+  /// [offset] and [length] refers to the offset and length of the source code
+  /// specifying the language version.
+  void registerExplicitLanguageVersion(Version version,
+      {int offset = 0, int length = noLength});
+
+  // TODO(johnniwinther): Remove this.
+  bool get forAugmentationLibrary;
+
+  // TODO(johnniwinther): Remove this.
+  bool get forPatchLibrary;
+
+  /// If this is an compilation unit for an augmentation library, returns the
+  /// import uri for the origin library. Otherwise the [importUri] for the
+  /// compilation unit itself.
+  Uri get originImportUri;
+
   LibraryFeatures get libraryFeatures;
 
   /// Returns `true` if the compilation unit is part of a `dart:` library.
@@ -183,6 +216,10 @@
   /// an error reading its source.
   abstract Message? accessProblem;
 
+  void issuePostponedProblems();
+
+  void markLanguageVersionFinal();
+
   void addSyntheticImport(
       {required String uri,
       required String? prefix,
diff --git a/pkg/front_end/lib/src/source/source_library_builder.dart b/pkg/front_end/lib/src/source/source_library_builder.dart
index b064f3e..583233a 100644
--- a/pkg/front_end/lib/src/source/source_library_builder.dart
+++ b/pkg/front_end/lib/src/source/source_library_builder.dart
@@ -158,12 +158,27 @@
       : currentTypeParameterScopeBuilder = _libraryTypeParameterScopeBuilder;
 
   @override
-  SourceLibraryBuilder get sourceLibraryBuilder => _sourceLibraryBuilder;
+  LanguageVersion get packageLanguageVersion =>
+      _sourceLibraryBuilder.packageLanguageVersion;
+
+  @override
+  void registerExplicitLanguageVersion(Version version,
+      {int offset = 0, int length = noLength}) {
+    _sourceLibraryBuilder.registerExplicitLanguageVersion(version,
+        offset: offset, length: length);
+  }
 
   @override
   LibraryFeatures get libraryFeatures => _sourceLibraryBuilder.libraryFeatures;
 
   @override
+  bool get forAugmentationLibrary =>
+      _sourceLibraryBuilder.isAugmentationLibrary;
+
+  @override
+  bool get forPatchLibrary => _sourceLibraryBuilder.isPatchLibrary;
+
+  @override
   bool get isDartLibrary =>
       _sourceLibraryBuilder.origin.importUri.isScheme("dart") ||
       fileUri.isScheme("org-dartlang-sdk");
@@ -282,6 +297,16 @@
   }
 
   @override
+  void issuePostponedProblems() {
+    _sourceLibraryBuilder.issuePostponedProblems();
+  }
+
+  @override
+  void markLanguageVersionFinal() {
+    _sourceLibraryBuilder.markLanguageVersionFinal();
+  }
+
+  @override
   Iterable<Uri> get dependencies sync* {
     for (Export export in exports) {
       yield export.exportedCompilationUnit.importUri;
@@ -3300,6 +3325,10 @@
       }
     }
   }
+
+  @override
+  // TODO(johnniwinther): Avoid using [_sourceLibraryBuilder.library] here.
+  Uri get originImportUri => _sourceLibraryBuilder.library.importUri;
 }
 
 class SourceLibraryBuilder extends LibraryBuilderImpl {
@@ -3396,7 +3425,7 @@
   /// of the package it belongs to, if present, or the current language version
   /// otherwise.
   ///
-  /// This language version we be used as the language version for the library
+  /// This language version will be used as the language version for the library
   /// if the library does not contain an explicit @dart= annotation.
   final LanguageVersion packageLanguageVersion;
 
diff --git a/pkg/front_end/lib/src/source/source_loader.dart b/pkg/front_end/lib/src/source/source_loader.dart
index 51d675a..7bd8d3d 100644
--- a/pkg/front_end/lib/src/source/source_loader.dart
+++ b/pkg/front_end/lib/src/source/source_loader.dart
@@ -877,7 +877,6 @@
 
   Future<Token> tokenize(SourceCompilationUnit compilationUnit,
       {bool suppressLexicalErrors = false}) async {
-    SourceLibraryBuilder libraryBuilder = compilationUnit.sourceLibraryBuilder;
     target.benchmarker?.beginSubdivide(BenchmarkSubdivides.tokenize);
     Uri fileUri = compilationUnit.fileUri;
 
@@ -940,21 +939,21 @@
             enableTripleShift: target.isExperimentEnabledInLibraryByVersion(
                 ExperimentalFlag.tripleShift,
                 compilationUnit.importUri,
-                libraryBuilder.packageLanguageVersion.version),
+                compilationUnit.packageLanguageVersion.version),
             enableExtensionMethods:
                 target.isExperimentEnabledInLibraryByVersion(
                     ExperimentalFlag.extensionMethods,
                     compilationUnit.importUri,
-                    libraryBuilder.packageLanguageVersion.version),
+                    compilationUnit.packageLanguageVersion.version),
             enableNonNullable: target.isExperimentEnabledInLibraryByVersion(
                 ExperimentalFlag.nonNullable,
                 compilationUnit.importUri,
-                libraryBuilder.packageLanguageVersion.version),
-            forAugmentationLibrary: libraryBuilder.isAugmentationLibrary),
+                compilationUnit.packageLanguageVersion.version),
+            forAugmentationLibrary: compilationUnit.forAugmentationLibrary),
         languageVersionChanged:
             (Scanner scanner, LanguageVersionToken version) {
       if (!suppressLexicalErrors) {
-        libraryBuilder.registerExplicitLanguageVersion(
+        compilationUnit.registerExplicitLanguageVersion(
             new Version(version.major, version.minor),
             offset: version.offset,
             length: version.length);
@@ -973,35 +972,35 @@
       /// We use the [importUri] of the created [Library] and not the
       /// [importUri] of the [LibraryBuilder] since it might be an augmentation
       /// library which is not directly part of the output.
-      Uri importUri = libraryBuilder.library.importUri;
-      if (libraryBuilder.isAugmenting) {
-        // For augmentation libraries we create a "fake" import uri.
+      Uri importUri = compilationUnit.library.importUri;
+      if (compilationUnit.isAugmenting) {
+        // For patch libraries we create a "fake" import uri.
         // We cannot use the import uri from the augmented library because
         // several different files would then have the same import uri,
         // and the VM does not support that. Also, what would, for instance,
         // setting a breakpoint on line 42 of some import uri mean, if the uri
         // represented several files?
-        if (libraryBuilder.isPatchLibrary) {
+        if (compilationUnit.forPatchLibrary) {
           // TODO(johnniwinther): Use augmentation-like solution for patching.
           List<String> newPathSegments =
               new List<String>.of(importUri.pathSegments);
-          newPathSegments.add(libraryBuilder.fileUri.pathSegments.last);
+          newPathSegments.add(compilationUnit.fileUri.pathSegments.last);
           newPathSegments[0] = "${newPathSegments[0]}-patch";
           importUri = importUri.replace(pathSegments: newPathSegments);
         } else {
-          importUri = libraryBuilder.importUri;
+          importUri = compilationUnit.importUri;
         }
       }
       target.addSourceInformation(
-          importUri, libraryBuilder.fileUri, result.lineStarts, source);
+          importUri, compilationUnit.fileUri, result.lineStarts, source);
     }
-    libraryBuilder.issuePostponedProblems();
-    libraryBuilder.markLanguageVersionFinal();
+    compilationUnit.issuePostponedProblems();
+    compilationUnit.markLanguageVersionFinal();
     while (token is ErrorToken) {
       if (!suppressLexicalErrors) {
         ErrorToken error = token;
-        libraryBuilder.addProblem(error.assertionMessage, offsetForToken(token),
-            lengthForToken(token), fileUri);
+        compilationUnit.addProblem(error.assertionMessage,
+            offsetForToken(token), lengthForToken(token), fileUri);
       }
       token = token.next!;
     }