Skip to content

Commit d906c6d

Browse files
johnniwinthercommit-bot@chromium.org
authored andcommitted
Reland "[cfe] Move createLibraryBuilder methods to loaders"
This is a reland of 0e7d0f5 Original change's description: > [cfe] Move createLibraryBuilder methods to loaders > > Change-Id: I912ab0494cc91a4a2b665a24a7d55362402542ff > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/216940 > Commit-Queue: Johnni Winther <[email protected]> > Reviewed-by: Jens Johansen <[email protected]> Change-Id: Ibb25413423a9bbd67c8788f9adce95c201ac8ebf Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/217603 Reviewed-by: Jens Johansen <[email protected]> Commit-Queue: Johnni Winther <[email protected]>
1 parent 50d885e commit d906c6d

File tree

7 files changed

+285
-261
lines changed

7 files changed

+285
-261
lines changed

pkg/front_end/lib/src/fasta/dill/dill_loader.dart

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ import 'dart:collection' show Queue;
4747
class DillLoader extends Loader {
4848
SourceLoader? currentSourceLoader;
4949

50+
final Map<Uri, DillLibraryBuilder> _knownLibraryBuilders =
51+
<Uri, DillLibraryBuilder>{};
52+
5053
final Map<Uri, DillLibraryBuilder> _builders = <Uri, DillLibraryBuilder>{};
5154

5255
final Queue<DillLibraryBuilder> _unparsedLibraries =
@@ -88,6 +91,16 @@ class DillLoader extends Loader {
8891

8992
Ticker get ticker => target.ticker;
9093

94+
void registerKnownLibrary(Library library) {
95+
_knownLibraryBuilders[library.importUri] =
96+
new DillLibraryBuilder(library, this);
97+
}
98+
99+
// TODO(johnniwinther): This is never called!?!
100+
void releaseAncillaryResources() {
101+
_knownLibraryBuilders.clear();
102+
}
103+
91104
/// Look up a library builder by the [uri], or if such doesn't exist, create
92105
/// one. The canonical URI of the library is [uri], and its actual location is
93106
/// [fileUri].
@@ -100,32 +113,35 @@ class DillLoader extends Loader {
100113
/// directive. If [accessor] isn't allowed to access [uri], it's a
101114
/// compile-time error.
102115
DillLibraryBuilder read(Uri uri, int charOffset, {LibraryBuilder? accessor}) {
103-
DillLibraryBuilder builder = _builders.putIfAbsent(uri, () {
104-
DillLibraryBuilder library = target.createLibraryBuilder(uri);
105-
assert(library.loader == this);
116+
DillLibraryBuilder? libraryBuilder = _builders[uri];
117+
if (libraryBuilder == null) {
118+
libraryBuilder = _knownLibraryBuilders.remove(uri);
119+
// ignore: unnecessary_null_comparison
120+
assert(libraryBuilder != null, "No library found for $uri.");
121+
_builders[uri] = libraryBuilder!;
122+
assert(libraryBuilder.loader == this);
106123
if (uri.scheme == "dart") {
107124
if (uri.path == "core") {
108-
_coreLibrary = library;
125+
_coreLibrary = libraryBuilder;
109126
}
110127
}
111128
{
112129
// Add any additional logic after this block. Setting the
113130
// firstSourceUri and first library should be done as early as
114131
// possible.
115132
firstSourceUri ??= uri;
116-
first ??= library;
133+
first ??= libraryBuilder;
117134
}
118-
if (_coreLibrary == library) {
135+
if (_coreLibrary == libraryBuilder) {
119136
target.loadExtraRequiredLibraries(this);
120137
}
121138
if (target.backendTarget.mayDefineRestrictedType(uri)) {
122-
library.mayImplementRestrictedTypes = true;
139+
libraryBuilder.mayImplementRestrictedTypes = true;
123140
}
124-
_unparsedLibraries.addLast(library);
125-
return library;
126-
});
141+
_unparsedLibraries.addLast(libraryBuilder);
142+
}
127143
if (accessor != null) {
128-
builder.recordAccess(charOffset, noLength, accessor.fileUri);
144+
libraryBuilder.recordAccess(charOffset, noLength, accessor.fileUri);
129145
if (!accessor.isPatch &&
130146
!accessor.isPart &&
131147
!target.backendTarget
@@ -134,7 +150,7 @@ class DillLoader extends Loader {
134150
noLength, accessor.fileUri);
135151
}
136152
}
137-
return builder;
153+
return libraryBuilder;
138154
}
139155

140156
void _ensureCoreLibrary() {
@@ -259,7 +275,7 @@ severity: $severity
259275
Uri uri = library.importUri;
260276
if (filter == null || filter(library.importUri)) {
261277
libraries.add(library);
262-
target.registerLibrary(library);
278+
registerKnownLibrary(library);
263279
requestedLibraries.add(uri);
264280
requestedLibrariesFileUri.add(library.fileUri);
265281
}
@@ -285,7 +301,7 @@ severity: $severity
285301
//
286302
// Create dill library builder (adds it to a map where it's fetched
287303
// again momentarily).
288-
target.registerLibrary(library);
304+
registerKnownLibrary(library);
289305
// Set up the dill library builder (fetch it from the map again, add it to
290306
// another map and setup some auxiliary things).
291307
return read(library.importUri, -1);

pkg/front_end/lib/src/fasta/dill/dill_target.dart

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'package:_fe_analyzer_shared/src/messages/severity.dart' show Severity;
66

7-
import 'package:kernel/ast.dart' show Library, Source;
7+
import 'package:kernel/ast.dart' show Source;
88

99
import 'package:kernel/target/targets.dart' show Target;
1010

@@ -20,16 +20,11 @@ import '../uri_translator.dart' show UriTranslator;
2020

2121
import '../target_implementation.dart' show TargetImplementation;
2222

23-
import 'dill_library_builder.dart' show DillLibraryBuilder;
24-
2523
import 'dill_loader.dart' show DillLoader;
2624

2725
class DillTarget extends TargetImplementation {
2826
final Ticker ticker;
2927

30-
final Map<Uri, DillLibraryBuilder> _knownLibraryBuilders =
31-
<Uri, DillLibraryBuilder>{};
32-
3328
bool isLoaded = false;
3429

3530
late final DillLoader loader;
@@ -92,23 +87,4 @@ class DillTarget extends TargetImplementation {
9287
}
9388
isLoaded = true;
9489
}
95-
96-
/// Returns the [DillLibraryBuilder] corresponding to [uri].
97-
///
98-
/// The [DillLibraryBuilder] is pulled from [_knownLibraryBuilders].
99-
DillLibraryBuilder createLibraryBuilder(Uri uri) {
100-
DillLibraryBuilder libraryBuilder = _knownLibraryBuilders.remove(uri)!;
101-
// ignore: unnecessary_null_comparison
102-
assert(libraryBuilder != null, "No library found for $uri.");
103-
return libraryBuilder;
104-
}
105-
106-
void registerLibrary(Library library) {
107-
_knownLibraryBuilders[library.importUri] =
108-
new DillLibraryBuilder(library, loader);
109-
}
110-
111-
void releaseAncillaryResources() {
112-
_knownLibraryBuilders.clear();
113-
}
11490
}

pkg/front_end/lib/src/fasta/incremental_compiler.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,7 @@ class IncrementalCompiler implements IncrementalKernelGenerator {
14261426
for (Library lib in module.libraries) {
14271427
if (!dillLoadedData!.loader.containsLibraryBuilder(lib.importUri)) {
14281428
dillLoadedData!.loader.libraries.add(lib);
1429-
dillLoadedData!.registerLibrary(lib);
1429+
dillLoadedData!.loader.registerKnownLibrary(lib);
14301430
reusedLibraries.add(dillLoadedData!.loader.read(lib.importUri, -1));
14311431
usedComponent = true;
14321432
}

pkg/front_end/lib/src/fasta/kernel/kernel_target.dart

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import '../builder/type_variable_builder.dart';
4040
import '../builder/void_type_declaration_builder.dart';
4141
import '../compiler_context.dart' show CompilerContext;
4242
import '../crash.dart' show withCrashReporting;
43-
import '../dill/dill_library_builder.dart' show DillLibraryBuilder;
4443
import '../dill/dill_member_builder.dart' show DillMemberBuilder;
4544
import '../dill/dill_target.dart' show DillTarget;
4645
import '../kernel/constructor_tearoff_lowering.dart';
@@ -50,16 +49,11 @@ import '../messages.dart'
5049
FormattedMessage,
5150
LocatedMessage,
5251
Message,
53-
messageAgnosticWithStrongDillLibrary,
54-
messageAgnosticWithWeakDillLibrary,
5552
messageConstConstructorLateFinalFieldCause,
5653
messageConstConstructorLateFinalFieldError,
5754
messageConstConstructorNonFinalField,
5855
messageConstConstructorNonFinalFieldCause,
5956
messageConstConstructorRedirectionToNonConst,
60-
messageInvalidNnbdDillLibrary,
61-
messageStrongWithWeakDillLibrary,
62-
messageWeakWithStrongDillLibrary,
6357
noLength,
6458
templateFieldNonNullableNotInitializedByConstructorError,
6559
templateFieldNonNullableWithoutInitializerError,
@@ -72,8 +66,7 @@ import '../problems.dart' show unhandled;
7266
import '../scope.dart' show AmbiguousBuilder;
7367
import '../source/name_scheme.dart';
7468
import '../source/source_class_builder.dart' show SourceClassBuilder;
75-
import '../source/source_library_builder.dart'
76-
show LanguageVersion, SourceLibraryBuilder;
69+
import '../source/source_library_builder.dart' show SourceLibraryBuilder;
7770
import '../source/source_loader.dart' show SourceLoader;
7871
import '../target_implementation.dart' show TargetImplementation;
7972
import '../ticker.dart' show Ticker;
@@ -368,90 +361,6 @@ class KernelTarget extends TargetImplementation {
368361
return entryPoint;
369362
}
370363

371-
/// Creates a [LibraryBuilder] corresponding to [uri], if one doesn't exist
372-
/// already.
373-
///
374-
/// [fileUri] must not be null and is a URI that can be passed to FileSystem
375-
/// to locate the corresponding file.
376-
///
377-
/// [origin] is non-null if the created library is a patch to [origin].
378-
///
379-
/// [packageUri] is the base uri for the package which the library belongs to.
380-
/// For instance 'package:foo'.
381-
///
382-
/// This is used to associate libraries in for instance the 'bin' and 'test'
383-
/// folders of a package source with the package uri of the 'lib' folder.
384-
///
385-
/// If the [packageUri] is `null` the package association of this library is
386-
/// based on its [importUri].
387-
///
388-
/// For libraries with a 'package:' [importUri], the package path must match
389-
/// the path in the [importUri]. For libraries with a 'dart:' [importUri] the
390-
/// [packageUri] must be `null`.
391-
///
392-
/// [packageLanguageVersion] is the language version defined by the package
393-
/// which the library belongs to, or the current sdk version if the library
394-
/// doesn't belong to a package.
395-
LibraryBuilder createLibraryBuilder(
396-
Uri uri,
397-
Uri fileUri,
398-
Uri? packageUri,
399-
LanguageVersion packageLanguageVersion,
400-
SourceLibraryBuilder? origin,
401-
Library? referencesFrom,
402-
bool? referenceIsPartOwner) {
403-
if (dillTarget.isLoaded) {
404-
DillLibraryBuilder? builder = dillTarget.loader.lookupLibraryBuilder(uri);
405-
if (builder != null) {
406-
if (!builder.isNonNullableByDefault &&
407-
(loader.nnbdMode == NnbdMode.Strong ||
408-
loader.nnbdMode == NnbdMode.Agnostic)) {
409-
loader.registerStrongOptOutLibrary(builder);
410-
} else {
411-
NonNullableByDefaultCompiledMode libraryMode =
412-
builder.library.nonNullableByDefaultCompiledMode;
413-
if (libraryMode == NonNullableByDefaultCompiledMode.Invalid) {
414-
loader.registerNnbdMismatchLibrary(
415-
builder, messageInvalidNnbdDillLibrary);
416-
} else {
417-
switch (loader.nnbdMode) {
418-
case NnbdMode.Weak:
419-
if (libraryMode != NonNullableByDefaultCompiledMode.Agnostic &&
420-
libraryMode != NonNullableByDefaultCompiledMode.Weak) {
421-
loader.registerNnbdMismatchLibrary(
422-
builder, messageWeakWithStrongDillLibrary);
423-
}
424-
break;
425-
case NnbdMode.Strong:
426-
if (libraryMode != NonNullableByDefaultCompiledMode.Agnostic &&
427-
libraryMode != NonNullableByDefaultCompiledMode.Strong) {
428-
loader.registerNnbdMismatchLibrary(
429-
builder, messageStrongWithWeakDillLibrary);
430-
}
431-
break;
432-
case NnbdMode.Agnostic:
433-
if (libraryMode != NonNullableByDefaultCompiledMode.Agnostic) {
434-
if (libraryMode == NonNullableByDefaultCompiledMode.Strong) {
435-
loader.registerNnbdMismatchLibrary(
436-
builder, messageAgnosticWithStrongDillLibrary);
437-
} else {
438-
loader.registerNnbdMismatchLibrary(
439-
builder, messageAgnosticWithWeakDillLibrary);
440-
}
441-
}
442-
break;
443-
}
444-
}
445-
}
446-
return builder;
447-
}
448-
}
449-
return new SourceLibraryBuilder(
450-
uri, fileUri, packageUri, packageLanguageVersion, loader, origin,
451-
referencesFrom: referencesFrom,
452-
referenceIsPartOwner: referenceIsPartOwner);
453-
}
454-
455364
/// Returns classes defined in libraries in [loader].
456365
List<SourceClassBuilder> collectMyClasses() {
457366
List<SourceClassBuilder> result = <SourceClassBuilder>[];

0 commit comments

Comments
 (0)