Skip to content

Commit

Permalink
Revert "[cfe] Encapsulate Dill/SourceLoader.builders" and "[cfe] Move…
Browse files Browse the repository at this point in the history
… createLibraryBuilder methods to loaders"

This reverts commits 2bbb5dc and 0e7d0f5.

Change-Id: I140ae6eab70102d39b1bec665bedd3a3dbfb02bd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/217300
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
  • Loading branch information
johnniwinther authored and commit-bot@chromium.org committed Oct 19, 2021
1 parent 7ca94e9 commit 3006908
Show file tree
Hide file tree
Showing 19 changed files with 383 additions and 456 deletions.
2 changes: 1 addition & 1 deletion pkg/dev_compiler/lib/src/kernel/expression_compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ class ExpressionCompiler {

Future<Library> _getLibrary(Uri libraryUri) async {
return await _compiler.context.runInContext((_) async {
var builder = _compiler.userCode.loader.lookupLibraryBuilder(libraryUri);
var builder = _compiler.userCode.loader.builders[libraryUri];
if (builder != null) {
var library =
_compiler.userCode.loader.read(libraryUri, -1, accessor: builder);
Expand Down
2 changes: 1 addition & 1 deletion pkg/front_end/lib/src/fasta/dill/dill_library_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ class DillLibraryBuilder extends LibraryBuilderImpl {
} else {
unhandled("${node.runtimeType}", "finalizeExports", -1, fileUri);
}
LibraryBuilder? library = loader.lookupLibraryBuilder(libraryUri);
LibraryBuilder? library = loader.builders[libraryUri];
if (library == null) {
internalProblem(
templateUnspecified
Expand Down
85 changes: 25 additions & 60 deletions pkg/front_end/lib/src/fasta/dill/dill_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ import 'dart:collection' show Queue;
class DillLoader extends Loader {
SourceLoader? currentSourceLoader;

final Map<Uri, DillLibraryBuilder> _knownLibraryBuilders =
<Uri, DillLibraryBuilder>{};

final Map<Uri, DillLibraryBuilder> _builders = <Uri, DillLibraryBuilder>{};
@override
final Map<Uri, DillLibraryBuilder> builders = <Uri, DillLibraryBuilder>{};

final Queue<DillLibraryBuilder> _unparsedLibraries =
new Queue<DillLibraryBuilder>();
Expand Down Expand Up @@ -89,17 +87,11 @@ class DillLoader extends Loader {
@override
LibraryBuilder get coreLibrary => _coreLibrary!;

Ticker get ticker => target.ticker;

void registerKnownLibrary(Library library) {
_knownLibraryBuilders[library.importUri] =
new DillLibraryBuilder(library, this);
void set coreLibrary(LibraryBuilder value) {
_coreLibrary = value;
}

// TODO(johnniwinther): This is never called!?!
void releaseAncillaryResources() {
_knownLibraryBuilders.clear();
}
Ticker get ticker => target.ticker;

/// Look up a library builder by the [uri], or if such doesn't exist, create
/// one. The canonical URI of the library is [uri], and its actual location is
Expand All @@ -113,35 +105,32 @@ class DillLoader extends Loader {
/// directive. If [accessor] isn't allowed to access [uri], it's a
/// compile-time error.
DillLibraryBuilder read(Uri uri, int charOffset, {LibraryBuilder? accessor}) {
DillLibraryBuilder? libraryBuilder = _builders[uri];
if (libraryBuilder == null) {
libraryBuilder = _knownLibraryBuilders.remove(uri);
// ignore: unnecessary_null_comparison
assert(libraryBuilder != null, "No library found for $uri.");
_builders[uri] = libraryBuilder!;
assert(libraryBuilder.loader == this);
DillLibraryBuilder builder = builders.putIfAbsent(uri, () {
DillLibraryBuilder library = target.createLibraryBuilder(uri);
assert(library.loader == this);
if (uri.scheme == "dart") {
if (uri.path == "core") {
_coreLibrary = libraryBuilder;
_coreLibrary = library;
}
}
{
// Add any additional logic after this block. Setting the
// firstSourceUri and first library should be done as early as
// possible.
firstSourceUri ??= uri;
first ??= libraryBuilder;
first ??= library;
}
if (_coreLibrary == libraryBuilder) {
if (_coreLibrary == library) {
target.loadExtraRequiredLibraries(this);
}
if (target.backendTarget.mayDefineRestrictedType(uri)) {
libraryBuilder.mayImplementRestrictedTypes = true;
library.mayImplementRestrictedTypes = true;
}
_unparsedLibraries.addLast(libraryBuilder);
}
_unparsedLibraries.addLast(library);
return library;
});
if (accessor != null) {
libraryBuilder.recordAccess(charOffset, noLength, accessor.fileUri);
builder.recordAccess(charOffset, noLength, accessor.fileUri);
if (!accessor.isPatch &&
!accessor.isPart &&
!target.backendTarget
Expand All @@ -150,7 +139,7 @@ class DillLoader extends Loader {
noLength, accessor.fileUri);
}
}
return libraryBuilder;
return builder;
}

void _ensureCoreLibrary() {
Expand All @@ -176,7 +165,7 @@ class DillLoader extends Loader {
void _logSummary(Template<SummaryTemplate> template) {
ticker.log((Duration elapsed, Duration sinceStart) {
int libraryCount = 0;
for (DillLibraryBuilder library in libraryBuilders) {
for (DillLibraryBuilder library in builders.values) {
assert(library.loader == this);
libraryCount++;
}
Expand Down Expand Up @@ -275,7 +264,7 @@ severity: $severity
Uri uri = library.importUri;
if (filter == null || filter(library.importUri)) {
libraries.add(library);
registerKnownLibrary(library);
target.registerLibrary(library);
requestedLibraries.add(uri);
requestedLibrariesFileUri.add(library.fileUri);
}
Expand All @@ -301,7 +290,7 @@ severity: $severity
//
// Create dill library builder (adds it to a map where it's fetched
// again momentarily).
registerKnownLibrary(library);
target.registerLibrary(library);
// Set up the dill library builder (fetch it from the map again, add it to
// another map and setup some auxiliary things).
return read(library.importUri, -1);
Expand All @@ -316,19 +305,19 @@ severity: $severity
}

void finalizeExports({bool suppressFinalizationErrors: false}) {
for (DillLibraryBuilder builder in libraryBuilders) {
builder.markAsReadyToFinalizeExports(
for (LibraryBuilder builder in builders.values) {
DillLibraryBuilder library = builder as DillLibraryBuilder;
library.markAsReadyToFinalizeExports(
suppressFinalizationErrors: suppressFinalizationErrors);
}
}

@override
ClassBuilder computeClassBuilderFromTargetClass(Class cls) {
Library kernelLibrary = cls.enclosingLibrary;
LibraryBuilder? library = lookupLibraryBuilder(kernelLibrary.importUri);
LibraryBuilder? library = builders[kernelLibrary.importUri];
if (library == null) {
library =
currentSourceLoader?.lookupLibraryBuilder(kernelLibrary.importUri);
library = currentSourceLoader?.builders[kernelLibrary.importUri];
}
return library!.lookupLocalMember(cls.name, required: true) as ClassBuilder;
}
Expand All @@ -337,28 +326,4 @@ severity: $severity
TypeBuilder computeTypeBuilder(DartType type) {
return type.accept(new TypeBuilderComputer(this));
}

bool containsLibraryBuilder(Uri importUri) =>
_builders.containsKey(importUri);

@override
DillLibraryBuilder? lookupLibraryBuilder(Uri importUri) =>
_builders[importUri];

Iterable<DillLibraryBuilder> get libraryBuilders => _builders.values;

Iterable<Uri> get libraryImportUris => _builders.keys;

void registerLibraryBuilder(DillLibraryBuilder libraryBuilder) {
Uri importUri = libraryBuilder.importUri;
libraryBuilder.loader = this;
if (importUri.scheme == "dart" && importUri.path == "core") {
_coreLibrary = libraryBuilder;
}
_builders[importUri] = libraryBuilder;
}

DillLibraryBuilder? deregisterLibraryBuilder(Uri importUri) {
return _builders.remove(importUri);
}
}
27 changes: 26 additions & 1 deletion pkg/front_end/lib/src/fasta/dill/dill_target.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

import 'package:kernel/ast.dart' show Source;
import 'package:kernel/ast.dart' show Library, Source;

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

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

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

import 'dill_library_builder.dart' show DillLibraryBuilder;

import 'dill_loader.dart' show DillLoader;

class DillTarget extends TargetImplementation {
final Ticker ticker;

final Map<Uri, DillLibraryBuilder> _knownLibraryBuilders =
<Uri, DillLibraryBuilder>{};

bool isLoaded = false;

late final DillLoader loader;
Expand Down Expand Up @@ -87,4 +92,24 @@ class DillTarget extends TargetImplementation {
}
isLoaded = true;
}

/// Returns the [DillLibraryBuilder] corresponding to [uri].
///
/// The [DillLibraryBuilder] is pulled from [_knownLibraryBuilders].
DillLibraryBuilder createLibraryBuilder(Uri uri) {
DillLibraryBuilder libraryBuilder =
_knownLibraryBuilders.remove(uri) as DillLibraryBuilder;
// ignore: unnecessary_null_comparison
assert(libraryBuilder != null, "No library found for $uri.");
return libraryBuilder;
}

void registerLibrary(Library library) {
_knownLibraryBuilders[library.importUri] =
new DillLibraryBuilder(library, loader);
}

void releaseAncillaryResources() {
_knownLibraryBuilders.clear();
}
}
Loading

0 comments on commit 3006908

Please sign in to comment.