Skip to content

Commit dceefd0

Browse files
johnniwintherCommit Queue
authored andcommitted
[cfe] Cleanup DillLibrary/ExportNameSpace implementation
This simplifies the computation of DillExportNameSpace and removes the need for LazyNameSpace. Change-Id: I6f29e9a6def47ed8751437b57c6ad7d29fcf494a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/429940 Reviewed-by: Chloe Stefantsova <[email protected]>
1 parent 8ac2279 commit dceefd0

File tree

6 files changed

+101
-190
lines changed

6 files changed

+101
-190
lines changed

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

Lines changed: 14 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,12 @@ import '../api_prototype/incremental_kernel_generator.dart'
6666
import '../api_prototype/lowering_predicates.dart'
6767
show isExtensionThisName, syntheticThisName;
6868
import '../api_prototype/memory_file_system.dart' show MemoryFileSystem;
69-
import '../builder/builder.dart' show Builder, NamedBuilder;
69+
import '../builder/builder.dart' show Builder;
7070
import '../builder/declaration_builders.dart'
7171
show ClassBuilder, ExtensionBuilder, ExtensionTypeDeclarationBuilder;
7272
import '../builder/library_builder.dart'
7373
show CompilationUnit, LibraryBuilder, SourceCompilationUnit;
7474
import '../builder/member_builder.dart' show MemberBuilder;
75-
import '../builder/property_builder.dart';
7675
import '../codes/cfe_codes.dart';
7776
import '../dill/dill_class_builder.dart' show DillClassBuilder;
7877
import '../dill/dill_library_builder.dart' show DillLibraryBuilder;
@@ -82,7 +81,6 @@ import '../kernel/benchmarker.dart' show BenchmarkPhases, Benchmarker;
8281
import '../kernel/hierarchy/hierarchy_builder.dart' show ClassHierarchyBuilder;
8382
import '../kernel/internal_ast.dart' show VariableDeclarationImpl;
8483
import '../kernel/kernel_target.dart' show BuildResult, KernelTarget;
85-
import '../source/source_extension_builder.dart';
8684
import '../source/source_library_builder.dart'
8785
show
8886
ImplicitLanguageVersion,
@@ -592,23 +590,16 @@ class IncrementalCompiler implements IncrementalKernelGenerator {
592590
/// dill library builders might have links (via export scopes) to the
593591
/// source builders. Patch that up.
594592
595-
// Maps from old library builder to map of new content.
596-
Map<LibraryBuilder, Map<String, NamedBuilder>>? replacementMap = {};
593+
// Map from old library builder to name space of new content.
594+
Map<LibraryBuilder, NameSpace>? replacementNameSpaceMap = {};
597595

598-
// Maps from old library builder to map of new content.
599-
Map<LibraryBuilder, Map<String, NamedBuilder>>? replacementSettersMap =
600-
{};
601-
602-
Map<LibraryBuilder, NameSpace> replacementLookupMap = {};
603-
604-
_experimentalInvalidationFillReplacementMaps(convertedLibraries!,
605-
replacementMap, replacementSettersMap, replacementLookupMap);
596+
_experimentalInvalidationFillReplacementMaps(
597+
convertedLibraries!, replacementNameSpaceMap);
606598

607599
for (DillLibraryBuilder builder
608600
in experimentalInvalidation.originalNotReusedLibraries) {
609601
if (builder.isBuilt) {
610-
builder.patchUpExportScope(
611-
replacementMap, replacementSettersMap, replacementLookupMap);
602+
builder.patchUpExportScope(replacementNameSpaceMap);
612603

613604
// Clear cached calculations that points (potential) to now replaced
614605
// things.
@@ -619,8 +610,7 @@ class IncrementalCompiler implements IncrementalKernelGenerator {
619610
}
620611
}
621612
}
622-
replacementMap = null;
623-
replacementSettersMap = null;
613+
replacementNameSpaceMap = null;
624614
}
625615
}
626616
nextGoodKernelTarget.loader.buildersCreatedWithReferences.clear();
@@ -770,42 +760,12 @@ class IncrementalCompiler implements IncrementalKernelGenerator {
770760
/// happen because of experimental invalidation.
771761
void _experimentalInvalidationFillReplacementMaps(
772762
Map<LibraryBuilder, CompilationUnit> rebuildBodiesMap,
773-
Map<LibraryBuilder, Map<String, NamedBuilder>> replacementMap,
774-
Map<LibraryBuilder, Map<String, NamedBuilder>> replacementSettersMap,
775-
Map<LibraryBuilder, NameSpace> replacementLookupMap) {
763+
Map<LibraryBuilder, NameSpace> replacementNameSpaceMap) {
776764
for (MapEntry<LibraryBuilder, CompilationUnit> entry
777765
in rebuildBodiesMap.entries) {
778-
Map<String, NamedBuilder> childReplacementMap = {};
779-
Map<String, NamedBuilder> childReplacementSettersMap = {};
780766
CompilationUnit mainCompilationUnit = rebuildBodiesMap[entry.key]!;
781-
replacementMap[entry.key] = childReplacementMap;
782-
replacementSettersMap[entry.key] = childReplacementSettersMap;
783-
replacementLookupMap[entry.key] =
767+
replacementNameSpaceMap[entry.key] =
784768
mainCompilationUnit.libraryBuilder.libraryNameSpace;
785-
786-
Iterator<NamedBuilder> iterator =
787-
mainCompilationUnit.libraryBuilder.unfilteredMembersIterator;
788-
while (iterator.moveNext()) {
789-
NamedBuilder childBuilder = iterator.current;
790-
if (childBuilder is SourceExtensionBuilder &&
791-
childBuilder.isUnnamedExtension) {
792-
continue;
793-
}
794-
String name = childBuilder.name;
795-
Map<String, Builder> map;
796-
if (isMappedAsSetter(childBuilder)) {
797-
map = childReplacementSettersMap;
798-
} else {
799-
map = childReplacementMap;
800-
}
801-
assert(
802-
!map.containsKey(name),
803-
"Unexpected double-entry for $name in "
804-
"${mainCompilationUnit.importUri} "
805-
"(org from ${entry.key.importUri}): "
806-
"$childBuilder and ${map[name]}");
807-
map[name] = childBuilder;
808-
}
809769
}
810770
}
811771

@@ -843,23 +803,17 @@ class IncrementalCompiler implements IncrementalKernelGenerator {
843803
ExperimentalInvalidation? experimentalInvalidation,
844804
Map<DillLibraryBuilder, CompilationUnit> rebuildBodiesMap) {
845805
if (experimentalInvalidation != null) {
846-
// Maps from old library builder to map of new content.
847-
Map<LibraryBuilder, Map<String, NamedBuilder>> replacementMap = {};
848-
849-
// Maps from old library builder to map of new content.
850-
Map<LibraryBuilder, Map<String, NamedBuilder>> replacementSettersMap = {};
851-
852-
Map<LibraryBuilder, NameSpace> replacementLookupMap = {};
806+
// Map from old library builder to name space of new content.
807+
Map<LibraryBuilder, NameSpace> replacementNameSpaceMap = {};
853808

854-
_experimentalInvalidationFillReplacementMaps(rebuildBodiesMap,
855-
replacementMap, replacementSettersMap, replacementLookupMap);
809+
_experimentalInvalidationFillReplacementMaps(
810+
rebuildBodiesMap, replacementNameSpaceMap);
856811

857812
for (DillLibraryBuilder builder
858813
in experimentalInvalidation.originalNotReusedLibraries) {
859814
// There's only something to patch up if it was build already.
860815
if (builder.isBuilt) {
861-
builder.patchUpExportScope(
862-
replacementMap, replacementSettersMap, replacementLookupMap);
816+
builder.patchUpExportScope(replacementNameSpaceMap);
863817
}
864818
}
865819
}

pkg/front_end/lib/src/base/name_space.dart

Lines changed: 69 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import '../builder/declaration_builders.dart';
77
import '../builder/library_builder.dart';
88
import '../builder/member_builder.dart';
99
import '../builder/property_builder.dart';
10-
import '../dill/dill_library_builder.dart';
1110
import 'lookup_result.dart';
1211
import 'scope.dart';
1312
import 'uris.dart';
@@ -375,53 +374,17 @@ final class DillDeclarationNameSpace extends DeclarationNameSpaceBase {
375374
DillDeclarationNameSpace() : super._();
376375
}
377376

378-
abstract base class LazyNameSpace extends ComputedMutableNameSpaceImpl {
379-
LazyNameSpace() : super._();
380-
381-
/// Override this method to lazily populate the scope before access.
382-
void ensureNameSpace();
383-
384-
@override
385-
Map<String, LookupResult>? get _content {
386-
ensureNameSpace();
387-
return super._content;
388-
}
389-
390-
@override
391-
Set<ExtensionBuilder>? get _extensions {
392-
ensureNameSpace();
393-
return super._extensions;
394-
}
395-
}
396-
397-
final class DillLibraryNameSpace extends LazyNameSpace {
398-
final DillLibraryBuilder _libraryBuilder;
399-
400-
DillLibraryNameSpace(this._libraryBuilder);
401-
402-
@override
403-
void ensureNameSpace() {
404-
_libraryBuilder.ensureLoaded();
405-
}
377+
final class DillLibraryNameSpace extends ComputedMutableNameSpaceImpl {
378+
DillLibraryNameSpace() : super._();
406379
}
407380

408-
final class DillExportNameSpace extends LazyNameSpace {
409-
final DillLibraryBuilder _libraryBuilder;
410-
411-
DillExportNameSpace(this._libraryBuilder);
412-
413-
@override
414-
void ensureNameSpace() {
415-
_libraryBuilder.ensureLoaded();
416-
}
381+
final class DillExportNameSpace extends ComputedMutableNameSpaceImpl {
382+
DillExportNameSpace() : super._();
417383

418384
/// Patch up the scope, using the two replacement maps to replace builders in
419385
/// scope. The replacement maps from old LibraryBuilder to map, mapping
420386
/// from name to new (replacement) builder.
421-
void patchUpScope(
422-
Map<LibraryBuilder, Map<String, NamedBuilder>> replacementMap,
423-
Map<LibraryBuilder, Map<String, NamedBuilder>> replacementMapSetters,
424-
Map<LibraryBuilder, NameSpace> replacementLookupMap) {
387+
void patchUpScope(Map<LibraryBuilder, NameSpace> replacementNameSpaceMap) {
425388
// In the following we refer to non-setters as 'getters' for brevity.
426389
//
427390
// We have to replace all getters and setters in [_locals] and [_setters]
@@ -435,70 +398,71 @@ final class DillExportNameSpace extends LazyNameSpace {
435398
// For this reason we start by collecting the names of all getters/setters
436399
// that need (some) replacement. Afterwards we go through these names
437400
// handling both getters and setters at the same time.
438-
{
439-
Set<String> replacedNames = {};
440-
_content?.forEach((String name, LookupResult result) {
441-
if (replacementMap.containsKey(result.getable?.parent)) {
442-
replacedNames.add(name);
443-
}
444-
if (replacementMapSetters.containsKey(result.setable?.parent)) {
445-
replacedNames.add(name);
446-
}
447-
});
448-
if (replacedNames.isNotEmpty) {
449-
for (String name in replacedNames) {
450-
// We start be collecting the relation between an existing getter/setter
451-
// and the getter/setter that will replace it. This information is used
452-
// below to handle all the different cases that can occur.
453-
LookupResult existingResult = _content![name]!;
454-
NamedBuilder? existingGetter = existingResult.getable;
455-
NamedBuilder? existingSetter = existingResult.setable;
456-
LookupResult? replacementResult;
457-
if (existingGetter != null && existingSetter != null) {
458-
if (existingGetter == existingSetter) {
459-
replacementResult = replacementLookupMap[existingGetter.parent]!
460-
.lookupLocalMember(name);
461-
} else {
462-
NamedBuilder? replacementGetter =
463-
replacementLookupMap[existingGetter.parent]
464-
?.lookupLocalMember(name)
465-
?.getable;
466-
NamedBuilder? replacementSetter =
467-
replacementLookupMap[existingSetter.parent]
468-
?.lookupLocalMember(name)
469-
?.setable;
470-
replacementResult = LookupResult.createResult(
471-
replacementGetter ?? existingGetter,
472-
replacementSetter ?? existingSetter);
473-
}
474-
} else if (existingGetter != null) {
475-
replacementResult = LookupResult.createResult(
476-
replacementLookupMap[existingGetter.parent]
401+
402+
Set<String> replacedNames = {};
403+
_content?.forEach((String name, LookupResult result) {
404+
if (replacementNameSpaceMap.containsKey(result.getable?.parent)) {
405+
replacedNames.add(name);
406+
}
407+
if (replacementNameSpaceMap.containsKey(result.setable?.parent)) {
408+
replacedNames.add(name);
409+
}
410+
});
411+
if (replacedNames.isNotEmpty) {
412+
for (String name in replacedNames) {
413+
// We start be collecting the relation between an existing getter/setter
414+
// and the getter/setter that will replace it. This information is used
415+
// below to handle all the different cases that can occur.
416+
LookupResult existingResult = _content![name]!;
417+
NamedBuilder? existingGetter = existingResult.getable;
418+
NamedBuilder? existingSetter = existingResult.setable;
419+
LookupResult? replacementResult;
420+
if (existingGetter != null && existingSetter != null) {
421+
if (existingGetter == existingSetter) {
422+
replacementResult = replacementNameSpaceMap[existingGetter.parent]!
423+
.lookupLocalMember(name);
424+
} else {
425+
NamedBuilder? replacementGetter =
426+
replacementNameSpaceMap[existingGetter.parent]
477427
?.lookupLocalMember(name)
478-
?.getable,
479-
null);
480-
} else if (existingSetter != null) {
481-
replacementResult = LookupResult.createResult(
482-
null,
483-
replacementLookupMap[existingSetter.parent]
428+
?.getable;
429+
NamedBuilder? replacementSetter =
430+
replacementNameSpaceMap[existingSetter.parent]
484431
?.lookupLocalMember(name)
485-
?.setable);
486-
}
487-
if (replacementResult != null) {
488-
(_content ??= // Coverage-ignore(suite): Not run.
489-
{})[name] = replacementResult;
490-
} else {
491-
// Coverage-ignore-block(suite): Not run.
492-
_content?.remove(name);
432+
?.setable;
433+
replacementResult = LookupResult.createResult(
434+
replacementGetter ?? existingGetter,
435+
replacementSetter ?? existingSetter);
493436
}
437+
} else if (existingGetter != null) {
438+
replacementResult = LookupResult.createResult(
439+
replacementNameSpaceMap[existingGetter.parent]
440+
?.lookupLocalMember(name)
441+
?.getable,
442+
null);
443+
} else if (existingSetter != null) {
444+
replacementResult = LookupResult.createResult(
445+
null,
446+
replacementNameSpaceMap[existingSetter.parent]
447+
?.lookupLocalMember(name)
448+
?.setable);
449+
}
450+
if (replacementResult != null) {
451+
(_content ??= // Coverage-ignore(suite): Not run.
452+
{})[name] = replacementResult;
453+
} else {
454+
// Coverage-ignore-block(suite): Not run.
455+
_content?.remove(name);
494456
}
495457
}
496458
}
459+
497460
if (_extensions != null) {
498461
// Coverage-ignore-block(suite): Not run.
499462
bool needsPatching = false;
500463
for (ExtensionBuilder extensionBuilder in _extensions!) {
501-
if (replacementMap.containsKey(extensionBuilder.libraryBuilder)) {
464+
if (replacementNameSpaceMap
465+
.containsKey(extensionBuilder.libraryBuilder)) {
502466
needsPatching = true;
503467
break;
504468
}
@@ -507,12 +471,16 @@ final class DillExportNameSpace extends LazyNameSpace {
507471
Set<ExtensionBuilder> extensionsReplacement =
508472
new Set<ExtensionBuilder>();
509473
for (ExtensionBuilder extensionBuilder in _extensions!) {
510-
if (replacementMap.containsKey(extensionBuilder.libraryBuilder)) {
511-
assert(replacementMap[extensionBuilder.libraryBuilder]![
512-
extensionBuilder.name] !=
474+
if (replacementNameSpaceMap
475+
.containsKey(extensionBuilder.libraryBuilder)) {
476+
assert(replacementNameSpaceMap[extensionBuilder.libraryBuilder]!
477+
.lookupLocalMember(extensionBuilder.name)!
478+
.getable !=
513479
null);
514-
extensionsReplacement.add(replacementMap[extensionBuilder
515-
.libraryBuilder]![extensionBuilder.name] as ExtensionBuilder);
480+
extensionsReplacement.add(
481+
replacementNameSpaceMap[extensionBuilder.libraryBuilder]!
482+
.lookupLocalMember(extensionBuilder.name)!
483+
.getable as ExtensionBuilder);
516484
break;
517485
} else {
518486
extensionsReplacement.add(extensionBuilder);

pkg/front_end/lib/src/builder/library_builder.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,6 @@ abstract class LibraryBuilder implements Builder, ProblemReporting {
355355
/// used in conditional imports and `bool.fromEnvironment` constants.
356356
bool get isUnsupported;
357357

358-
/// Returns an iterator of all members (typedefs, classes and members)
359-
/// declared in this library, including duplicate declarations.
360-
// TODO(johnniwinther): Should the only exist on [SourceLibraryBuilder]?
361-
Iterator<NamedBuilder> get unfilteredMembersIterator;
362-
363358
/// [Iterator] for all declarations declared in this library of type [T].
364359
///
365360
/// If [includeDuplicates] is `true`, duplicate declarations are included.

0 commit comments

Comments
 (0)