[cfe] Move more properties to SourceCompilationUnit/BuilderFactory(Result)

Change-Id: I57bf2751c4e740564f89ec2b5fa6678f36322923
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/375521
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 e50488f..9ff9f76 100644
--- a/pkg/front_end/lib/src/builder/library_builder.dart
+++ b/pkg/front_end/lib/src/builder/library_builder.dart
@@ -30,7 +30,6 @@
 import '../source/offset_map.dart';
 import '../source/outline_builder.dart';
 import '../source/source_class_builder.dart';
-import '../source/source_function_builder.dart';
 import '../source/source_library_builder.dart';
 import '../source/source_loader.dart';
 import 'builder.dart';
@@ -170,7 +169,7 @@
 
   List<NamedTypeBuilder> get unresolvedNamedTypes;
 
-  List<SourceFunctionBuilder> get nativeMethods;
+  int finishNativeMethods();
 
   String? get partOfName;
 
diff --git a/pkg/front_end/lib/src/source/builder_factory.dart b/pkg/front_end/lib/src/source/builder_factory.dart
index ac86f22..4c7ecf9 100644
--- a/pkg/front_end/lib/src/source/builder_factory.dart
+++ b/pkg/front_end/lib/src/source/builder_factory.dart
@@ -59,6 +59,8 @@
       Map<NominalVariableBuilder, SourceLibraryBuilder> nominalVariables,
       Map<StructuralVariableBuilder, SourceLibraryBuilder> structuralVariables);
 
+  int finishNativeMethods();
+
   void registerUnresolvedNamedTypes(List<NamedTypeBuilder> unboundTypes);
 
   void registerUnresolvedStructuralVariables(
diff --git a/pkg/front_end/lib/src/source/source_builder_factory.dart b/pkg/front_end/lib/src/source/source_builder_factory.dart
index e51a8d8..d256d99 100644
--- a/pkg/front_end/lib/src/source/source_builder_factory.dart
+++ b/pkg/front_end/lib/src/source/source_builder_factory.dart
@@ -129,6 +129,8 @@
 
   final List<StructuralVariableBuilder> _unboundStructuralVariables = [];
 
+  final List<SourceFunctionBuilder> _nativeMethods = [];
+
   BuilderFactoryImpl(
       this._compilationUnit,
       this._augmentationRoot,
@@ -153,9 +155,6 @@
   List<ConstructorReferenceBuilder> get constructorReferences =>
       _compilationUnit.constructorReferences;
 
-  List<SourceFunctionBuilder> get nativeMethods =>
-      _compilationUnit.nativeMethods;
-
   @override
   void beginNestedDeclaration(TypeParameterScopeKind kind, String name,
       {bool hasMembers = true}) {
@@ -1726,7 +1725,7 @@
   }
 
   void _addNativeMethod(SourceFunctionBuilder method) {
-    nativeMethods.add(method);
+    _nativeMethods.add(method);
   }
 
   @override
@@ -2513,4 +2512,12 @@
       List<StructuralVariableBuilder> unboundTypeVariables) {
     this._unboundStructuralVariables.addAll(unboundTypeVariables);
   }
+
+  @override
+  int finishNativeMethods() {
+    for (SourceFunctionBuilder method in _nativeMethods) {
+      method.becomeNative(loader);
+    }
+    return _nativeMethods.length;
+  }
 }
diff --git a/pkg/front_end/lib/src/source/source_compilation_unit.dart b/pkg/front_end/lib/src/source/source_compilation_unit.dart
index 39c44b3..b46cba5 100644
--- a/pkg/front_end/lib/src/source/source_compilation_unit.dart
+++ b/pkg/front_end/lib/src/source/source_compilation_unit.dart
@@ -12,9 +12,14 @@
   @override
   final Uri importUri;
 
+  final Uri? _packageUri;
+
   @override
   final Uri originImportUri;
 
+  @override
+  final SourceLoader loader;
+
   final SourceLibraryBuilder _sourceLibraryBuilder;
 
   SourceLibraryBuilder? _libraryBuilder;
@@ -55,22 +60,47 @@
   @override
   final IndexedLibrary? indexedLibrary;
 
+  @override
+  final LibraryName libraryName;
+
   late final BuilderFactoryImpl _builderFactory;
 
   late final BuilderFactoryResult _builderFactoryResult;
 
   final Scope importScope;
 
+  LibraryFeatures? _libraryFeatures;
+
+  @override
+  final bool forAugmentationLibrary;
+
+  @override
+  final bool forPatchLibrary;
+
+  @override
+  final bool isAugmenting;
+
+  @override
+  final bool isUnsupported;
+
   SourceCompilationUnitImpl(this._sourceLibraryBuilder,
       TypeParameterScopeBuilder libraryTypeParameterScopeBuilder,
       {required this.importUri,
       required this.fileUri,
+      required Uri? packageUri,
       required this.packageLanguageVersion,
       required this.originImportUri,
       required this.indexedLibrary,
+      required this.libraryName,
       Map<String, Builder>? omittedTypeDeclarationBuilders,
-      required this.importScope})
-      : _languageVersion = packageLanguageVersion {
+      required this.importScope,
+      required this.forAugmentationLibrary,
+      required this.forPatchLibrary,
+      required this.isAugmenting,
+      required this.isUnsupported,
+      required this.loader})
+      : _languageVersion = packageLanguageVersion,
+        _packageUri = packageUri {
     // TODO(johnniwinther): Create these in [createOutlineBuilder].
     _builderFactoryResult = _builderFactory = new BuilderFactoryImpl(
         this,
@@ -83,14 +113,9 @@
   }
 
   @override
-  LibraryFeatures get libraryFeatures => _sourceLibraryBuilder.libraryFeatures;
-
-  @override
-  bool get forAugmentationLibrary =>
-      _sourceLibraryBuilder.isAugmentationLibrary;
-
-  @override
-  bool get forPatchLibrary => _sourceLibraryBuilder.isPatchLibrary;
+  LibraryFeatures get libraryFeatures =>
+      _libraryFeatures ??= new LibraryFeatures(loader.target.globalFeatures,
+          _packageUri ?? originImportUri, languageVersion.version);
 
   @override
   bool get isDartLibrary =>
@@ -283,21 +308,12 @@
   }
 
   @override
-  bool get isAugmenting => _sourceLibraryBuilder.isAugmenting;
-
-  @override
   bool get isPart => _builderFactoryResult.isPart;
 
   @override
   bool get isSynthetic => accessProblem != null;
 
   @override
-  bool get isUnsupported => _sourceLibraryBuilder.isUnsupported;
-
-  @override
-  SourceLoader get loader => _sourceLibraryBuilder.loader;
-
-  @override
   NameIterator<Builder> get localMembersNameIterator =>
       _sourceLibraryBuilder.localMembersNameIterator;
 
@@ -386,13 +402,6 @@
   Library get library => _sourceLibraryBuilder.library;
 
   @override
-  LibraryName get libraryName => _sourceLibraryBuilder.libraryName;
-
-  @override
-  List<SourceFunctionBuilder> get nativeMethods =>
-      _sourceLibraryBuilder.nativeMethods;
-
-  @override
   String? get partOfName => _builderFactoryResult.partOfName;
 
   @override
@@ -605,7 +614,6 @@
         // scopes correctly) the exporters in this has to be updated too).
         libraryBuilder.exporters.addAll(part.exporters);
 
-        libraryBuilder.nativeMethods.addAll(part.nativeMethods);
         // Check that the targets are different. This is not normally a problem
         // but is for augmentation libraries.
         if (libraryBuilder.library != part.library &&
@@ -639,6 +647,11 @@
     }
   }
 
+  @override
+  int finishNativeMethods() {
+    return _builderFactoryResult.finishNativeMethods();
+  }
+
   void _clearPartsAndReportExporters() {
     assert(_libraryBuilder != null, "Library has not be set.");
     _builderFactoryResult.parts.clear();
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 edd1c51..04a3333 100644
--- a/pkg/front_end/lib/src/source/source_library_builder.dart
+++ b/pkg/front_end/lib/src/source/source_library_builder.dart
@@ -114,9 +114,6 @@
   Uri? get packageUriForTesting => _packageUri;
 
   @override
-  final bool isUnsupported;
-
-  @override
   String? get name => compilationUnit.name;
 
   @override
@@ -131,8 +128,6 @@
 
   final SourceLibraryBuilder? _immediateOrigin;
 
-  final List<SourceFunctionBuilder> nativeMethods = <SourceFunctionBuilder>[];
-
   final List<PendingBoundsCheck> _pendingBoundsChecks = [];
   final List<GenericFunctionTypeCheck> _pendingGenericFunctionTypeChecks = [];
 
@@ -180,12 +175,6 @@
 
   int augmentationIndex = 0;
 
-  /// `true` if this is an augmentation library.
-  final bool isAugmentationLibrary;
-
-  /// `true` if this is a patch library.
-  final bool isPatchLibrary;
-
   MergedLibraryScope? _mergedScope;
 
   /// If `null`, [SourceLoader.computeFieldPromotability] hasn't been called
@@ -242,14 +231,12 @@
       this.library,
       this._nameOrigin,
       IndexedLibrary? indexedLibrary,
-      {required this.isUnsupported,
+      {required bool isUnsupported,
       required bool isAugmentation,
       required bool isPatch,
       Map<String, Builder>? omittedTypes})
       : _immediateOrigin = origin,
         libraryName = new LibraryName(library.reference),
-        isAugmentationLibrary = isAugmentation,
-        isPatchLibrary = isPatch,
         super(
             fileUri,
             libraryTypeParameterScopeBuilder.toScope(importScope,
@@ -272,24 +259,36 @@
         this, libraryTypeParameterScopeBuilder,
         importUri: importUri,
         fileUri: fileUri,
+        packageUri: _packageUri,
         originImportUri: originImportUri,
         packageLanguageVersion: packageLanguageVersion,
         indexedLibrary: indexedLibrary,
+        libraryName: libraryName,
         omittedTypeDeclarationBuilders: omittedTypes,
-        importScope: importScope);
+        importScope: importScope,
+        forAugmentationLibrary: isAugmentation,
+        forPatchLibrary: isPatch,
+        isAugmenting: origin != null,
+        isUnsupported: isUnsupported,
+        loader: loader);
   }
 
+  /// `true` if this is an augmentation library.
+  bool get isAugmentationLibrary => compilationUnit.forAugmentationLibrary;
+
+  /// `true` if this is a patch library.
+  bool get isPatchLibrary => compilationUnit.forPatchLibrary;
+
+  @override
+  bool get isUnsupported => compilationUnit.isUnsupported;
+
   MergedLibraryScope get mergedScope {
     return _mergedScope ??=
         isAugmenting ? origin.mergedScope : new MergedLibraryScope(this);
   }
 
-  LibraryFeatures? _libraryFeatures;
-
   /// Returns the state of the experimental features within this library.
-  LibraryFeatures get libraryFeatures =>
-      _libraryFeatures ??= new LibraryFeatures(loader.target.globalFeatures,
-          _packageUri ?? origin.importUri, languageVersion.version);
+  LibraryFeatures get libraryFeatures => compilationUnit.libraryFeatures;
 
   /// Reports that [feature] is not enabled, using [charOffset] and
   /// [length] for the location of the message.
@@ -916,7 +915,7 @@
   }
 
   @override
-  bool get isAugmenting => _immediateOrigin != null;
+  bool get isAugmenting => compilationUnit.isAugmenting;
 
   @override
   SourceLibraryBuilder get origin {
@@ -1373,10 +1372,10 @@
       }
     }
 
-    for (SourceFunctionBuilder method in nativeMethods) {
-      method.becomeNative(loader);
+    count += compilationUnit.finishNativeMethods();
+    for (SourceCompilationUnit part in parts) {
+      count += part.finishNativeMethods();
     }
-    count += nativeMethods.length;
 
     return count;
   }