Skip to content

Commit

Permalink
[cfe] Add SourceCompilationImpl._becomePart
Browse files Browse the repository at this point in the history
This removes more of the SourceCompilationUnit by only accessing the
removed properties from the inside.

Change-Id: I677fd1cfdf224fc6c2349c90d1e2173f6c38154e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/375680
Reviewed-by: Chloe Stefantsova <[email protected]>
Commit-Queue: Johnni Winther <[email protected]>
  • Loading branch information
johnniwinther authored and Commit Queue committed Jul 16, 2024
1 parent b288e5d commit b26c29a
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 119 deletions.
11 changes: 1 addition & 10 deletions pkg/front_end/lib/src/builder/library_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import '../base/problems.dart' show internalProblem;
import '../base/scope.dart';
import '../base/uri_offset.dart';
import '../kernel/hierarchy/members_builder.dart';
import '../source/name_scheme.dart';
import '../source/offset_map.dart';
import '../source/outline_builder.dart';
import '../source/source_class_builder.dart';
Expand Down Expand Up @@ -156,14 +155,8 @@ abstract class SourceCompilationUnit implements CompilationUnit {

LanguageVersion get languageVersion;

// TODO(johnniwinther): Remove this.
Library get library;

String? get name;

// TODO(johnniwinther): Remove this?
LibraryName get libraryName;

List<NamedTypeBuilder> get unresolvedNamedTypes;

int finishNativeMethods();
Expand All @@ -172,8 +165,6 @@ abstract class SourceCompilationUnit implements CompilationUnit {

Uri? get partOfUri;

Scope get scope;

List<MetadataBuilder>? get metadata;

void takeMixinApplications(
Expand Down Expand Up @@ -230,7 +221,7 @@ abstract class SourceCompilationUnit implements CompilationUnit {

void buildOutlineNode(Library library);

int finishDeferredLoadTearoffs(Library library);
int finishDeferredLoadTearOffs(Library library);

void forEachExtensionInScope(void Function(ExtensionBuilder) f);

Expand Down
34 changes: 22 additions & 12 deletions pkg/front_end/lib/src/source/source_builder_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,33 @@ class BuilderFactoryImpl implements BuilderFactory, BuilderFactoryResult {

final List<SourceFunctionBuilder> _nativeMethods = [];

final LibraryName libraryName;

final Scope scope;

BuilderFactoryImpl(
this._compilationUnit,
this._augmentationRoot,
this._parent,
this._libraryTypeParameterScopeBuilder,
this._problemReporting,
{required this.indexedLibrary,
{required SourceCompilationUnit compilationUnit,
required SourceLibraryBuilder augmentationRoot,
required SourceLibraryBuilder parent,
required TypeParameterScopeBuilder libraryTypeParameterScopeBuilder,
required ProblemReporting problemReporting,
required Scope scope,
required LibraryName libraryName,
required IndexedLibrary? indexedLibrary,
required Map<String, Builder>? omittedTypeDeclarationBuilders})
: currentTypeParameterScopeBuilder = _libraryTypeParameterScopeBuilder,
: _compilationUnit = compilationUnit,
_augmentationRoot = augmentationRoot,
_libraryTypeParameterScopeBuilder = libraryTypeParameterScopeBuilder,
currentTypeParameterScopeBuilder = libraryTypeParameterScopeBuilder,
_problemReporting = problemReporting,
_parent = parent,
scope = scope,
libraryName = libraryName,
indexedLibrary = indexedLibrary,
_omittedTypeDeclarationBuilders = omittedTypeDeclarationBuilders;

Scope get scope => _compilationUnit.scope;

SourceLoader get loader => _compilationUnit.loader;

LibraryName get libraryName => _compilationUnit.libraryName;

LibraryFeatures get libraryFeatures => _compilationUnit.libraryFeatures;

final List<ConstructorReferenceBuilder> _constructorReferences = [];
Expand Down Expand Up @@ -2367,7 +2377,7 @@ class BuilderFactoryImpl implements BuilderFactory, BuilderFactoryResult {
..exportScope.merge(declaration.exportScope,
(String name, Builder existing, Builder member) {
return computeAmbiguousDeclarationForScope(
_problemReporting, _compilationUnit.scope, name, existing, member,
_problemReporting, scope, name, existing, member,
uriOffset: new UriOffset(_compilationUnit.fileUri, charOffset));
});
} else if (_isDuplicatedDeclaration(existing, declaration)) {
Expand Down
185 changes: 93 additions & 92 deletions pkg/front_end/lib/src/source/source_compilation_unit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ class SourceCompilationUnitImpl
@override
final IndexedLibrary? indexedLibrary;

@override
final LibraryName libraryName;
final LibraryName _libraryName;

late final BuilderFactoryImpl _builderFactory;

Expand Down Expand Up @@ -91,23 +90,26 @@ class SourceCompilationUnitImpl
required this.packageLanguageVersion,
required this.originImportUri,
required this.indexedLibrary,
required this.libraryName,
required LibraryName libraryName,
Map<String, Builder>? omittedTypeDeclarationBuilders,
required this.importScope,
required this.forAugmentationLibrary,
required this.forPatchLibrary,
required this.isAugmenting,
required this.isUnsupported,
required this.loader})
: _languageVersion = packageLanguageVersion,
: _libraryName = libraryName,
_languageVersion = packageLanguageVersion,
_packageUri = packageUri {
// TODO(johnniwinther): Create these in [createOutlineBuilder].
_builderFactoryResult = _builderFactory = new BuilderFactoryImpl(
this,
_sourceLibraryBuilder,
_sourceLibraryBuilder,
libraryTypeParameterScopeBuilder,
this,
compilationUnit: this,
augmentationRoot: _sourceLibraryBuilder,
parent: _sourceLibraryBuilder,
libraryTypeParameterScopeBuilder: libraryTypeParameterScopeBuilder,
problemReporting: this,
scope: _scope,
libraryName: _libraryName,
indexedLibrary: indexedLibrary,
omittedTypeDeclarationBuilders: omittedTypeDeclarationBuilders);
}
Expand Down Expand Up @@ -396,17 +398,13 @@ class SourceCompilationUnitImpl
}
}

@override
Library get library => _sourceLibraryBuilder.library;

@override
String? get partOfName => _builderFactoryResult.partOfName;

@override
Uri? get partOfUri => _builderFactoryResult.partOfUri;

@override
Scope get scope => _sourceLibraryBuilder.scope;
Scope get _scope => _sourceLibraryBuilder.scope;

@override
void takeMixinApplications(
Expand Down Expand Up @@ -552,77 +550,6 @@ class SourceCompilationUnitImpl
}

part.validatePart(libraryBuilder, usedParts);
NameIterator partDeclarations = part.localMembersNameIterator;
while (partDeclarations.moveNext()) {
String name = partDeclarations.name;
Builder declaration = partDeclarations.current;

if (declaration.next != null) {
List<Builder> duplicated = <Builder>[];
while (declaration.next != null) {
duplicated.add(declaration);
partDeclarations.moveNext();
declaration = partDeclarations.current;
}
duplicated.add(declaration);
// Handle duplicated declarations in the part.
//
// Duplicated declarations are handled by creating a linked list
// using the `next` field. This is preferred over making all scope
// entries be a `List<Declaration>`.
//
// We maintain the linked list so that the last entry is easy to
// recognize (it's `next` field is null). This means that it is
// reversed with respect to source code order. Since kernel doesn't
// allow duplicated declarations, we ensure that we only add the
// first declaration to the kernel tree.
//
// Since the duplicated declarations are stored in reverse order, we
// iterate over them in reverse order as this is simpler and
// normally not a problem. However, in this case we need to call
// [addBuilder] in source order as it would otherwise create cycles.
//
// We also need to be careful preserving the order of the links. The
// part library still keeps these declarations in its scope so that
// DietListener can find them.
for (int i = duplicated.length; i > 0; i--) {
Builder declaration = duplicated[i - 1];
// No reference: There should be no duplicates when using
// references.
libraryBuilder.addBuilder(
name, declaration, declaration.charOffset);
}
} else {
// No reference: The part is in the same loader so the reference
// - if needed - was already added.
libraryBuilder.addBuilder(
name, declaration, declaration.charOffset);
}
}
libraryBuilder.unresolvedNamedTypes.addAll(part.unresolvedNamedTypes);
part.libraryName.reference =
parentCompilationUnit.libraryName.reference;
part.scope.becomePartOf(libraryBuilder.scope);
// TODO(ahe): Include metadata from part?

// Recovery: Take on all exporters (i.e. if a library has erroneously
// exported the part it has (in validatePart) been recovered to import
// the main library (this) instead --- to make it complete (and set up
// scopes correctly) the exporters in this has to be updated too).
libraryBuilder.exporters.addAll(part.exporters);

// Check that the targets are different. This is not normally a problem
// but is for augmentation libraries.
if (libraryBuilder.library != part.library &&
part.library.problemsAsJson != null) {
(libraryBuilder.library.problemsAsJson ??= <String>[])
.addAll(part.library.problemsAsJson!);
}
if (libraryBuilder.library != part.library) {
// Mark the part library as synthetic as it's not an actual library
// (anymore).
part.library.isSynthetic = true;
}
includedParts.add(part);
return true;
case DillCompilationUnit():
Expand All @@ -644,6 +571,78 @@ class SourceCompilationUnitImpl
}
}

void _becomePart(SourceLibraryBuilder libraryBuilder) {
NameIterator partDeclarations = localMembersNameIterator;
while (partDeclarations.moveNext()) {
String name = partDeclarations.name;
Builder declaration = partDeclarations.current;

if (declaration.next != null) {
List<Builder> duplicated = <Builder>[];
while (declaration.next != null) {
duplicated.add(declaration);
partDeclarations.moveNext();
declaration = partDeclarations.current;
}
duplicated.add(declaration);
// Handle duplicated declarations in the part.
//
// Duplicated declarations are handled by creating a linked list
// using the `next` field. This is preferred over making all scope
// entries be a `List<Declaration>`.
//
// We maintain the linked list so that the last entry is easy to
// recognize (it's `next` field is null). This means that it is
// reversed with respect to source code order. Since kernel doesn't
// allow duplicated declarations, we ensure that we only add the
// first declaration to the kernel tree.
//
// Since the duplicated declarations are stored in reverse order, we
// iterate over them in reverse order as this is simpler and
// normally not a problem. However, in this case we need to call
// [addBuilder] in source order as it would otherwise create cycles.
//
// We also need to be careful preserving the order of the links. The
// part library still keeps these declarations in its scope so that
// DietListener can find them.
for (int i = duplicated.length; i > 0; i--) {
Builder declaration = duplicated[i - 1];
// No reference: There should be no duplicates when using
// references.
libraryBuilder.addBuilder(name, declaration, declaration.charOffset);
}
} else {
// No reference: The part is in the same loader so the reference
// - if needed - was already added.
libraryBuilder.addBuilder(name, declaration, declaration.charOffset);
}
}
libraryBuilder.unresolvedNamedTypes.addAll(unresolvedNamedTypes);
_libraryName.reference = libraryBuilder.libraryName.reference;
_scope.becomePartOf(libraryBuilder.scope);
// TODO(ahe): Include metadata from part?

// Recovery: Take on all exporters (i.e. if a library has erroneously
// exported the part it has (in validatePart) been recovered to import
// the main library (this) instead --- to make it complete (and set up
// scopes correctly) the exporters in this has to be updated too).
libraryBuilder.exporters.addAll(exporters);

// Check that the targets are different. This is not normally a problem
// but is for augmentation libraries.

Library library = _sourceLibraryBuilder.library;
if (libraryBuilder.library != library && library.problemsAsJson != null) {
(libraryBuilder.library.problemsAsJson ??= <String>[])
.addAll(library.problemsAsJson!);
}
if (libraryBuilder.library != library) {
// Mark the part library as synthetic as it's not an actual library
// (anymore).
library.isSynthetic = true;
}
}

@override
int finishNativeMethods() {
return _builderFactoryResult.finishNativeMethods();
Expand All @@ -666,12 +665,13 @@ class SourceCompilationUnitImpl
}

@override
void validatePart(SourceLibraryBuilder library, Set<Uri>? usedParts) {
_libraryBuilder = library;
_partOfLibrary = library;
void validatePart(SourceLibraryBuilder libraryBuilder, Set<Uri>? usedParts) {
_libraryBuilder = libraryBuilder;
_partOfLibrary = libraryBuilder;
if (_builderFactoryResult.parts.isNotEmpty) {
List<LocatedMessage> context = <LocatedMessage>[
messagePartInPartLibraryContext.withLocation(library.fileUri, -1, 1),
messagePartInPartLibraryContext.withLocation(
libraryBuilder.fileUri, -1, 1),
];
for (Part part in _builderFactoryResult.parts) {
addProblem(messagePartInPart, part.offset, noLength, fileUri,
Expand All @@ -681,6 +681,7 @@ class SourceCompilationUnitImpl
}
}
_clearPartsAndReportExporters();
_becomePart(libraryBuilder);
}

@override
Expand Down Expand Up @@ -751,7 +752,7 @@ class SourceCompilationUnitImpl
importScope.addLocalMember(
name,
computeAmbiguousDeclarationForScope(
this, scope, name, existing, member,
this, _scope, name, existing, member,
uriOffset: new UriOffset(fileUri, charOffset),
isImport: isImport),
setter: member.isSetter);
Expand Down Expand Up @@ -783,7 +784,7 @@ class SourceCompilationUnitImpl
}

@override
int finishDeferredLoadTearoffs(Library library) {
int finishDeferredLoadTearOffs(Library library) {
int total = 0;
for (Import import in _builderFactoryResult.imports) {
if (import.deferred) {
Expand All @@ -807,7 +808,7 @@ class SourceCompilationUnitImpl
void forEachExtensionInScope(void Function(ExtensionBuilder) f) {
if (_extensionsInScope == null) {
_extensionsInScope = <ExtensionBuilder>{};
scope.forEachExtension((e) {
_scope.forEachExtension((e) {
if (!e.extension.isExtensionTypeDeclaration) {
_extensionsInScope!.add(e);
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/front_end/lib/src/source/source_library_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1271,20 +1271,20 @@ class SourceLibraryBuilder extends LibraryBuilderImpl {
}
}

int finishDeferredLoadTearoffs() {
int finishDeferredLoadTearOffs() {
int total = 0;

Iterable<SourceLibraryBuilder>? augmentationLibraries =
this.augmentationLibraries;
if (augmentationLibraries != null) {
for (SourceLibraryBuilder augmentationLibrary in augmentationLibraries) {
total += augmentationLibrary.finishDeferredLoadTearoffs();
total += augmentationLibrary.finishDeferredLoadTearOffs();
}
}

total += compilationUnit.finishDeferredLoadTearoffs(library);
total += compilationUnit.finishDeferredLoadTearOffs(library);
for (SourceCompilationUnit part in parts) {
total += part.finishDeferredLoadTearoffs(library);
total += part.finishDeferredLoadTearOffs(library);
}
return total;
}
Expand Down
Loading

0 comments on commit b26c29a

Please sign in to comment.