Skip to content

Commit

Permalink
[cfe] Introduce NameSpace
Browse files Browse the repository at this point in the history
This adds a NameSpace interface to Scope and uses it for access to
local members. The NameSpace represents entities declared with a library
or class/extension (type) declaration and is to be used for creating
a Scope instead of being the scope itself. As such it is added to
LibraryBuilder and IDeclarationBuilder and used in place of the `scope`
property for accessing these entities.

Change-Id: Id9b03bc8e820ad8f6d185cc95d695ac4a5e59f8f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/376622
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
  • Loading branch information
johnniwinther authored and Commit Queue committed Jul 22, 2024
1 parent beee864 commit 66658c8
Show file tree
Hide file tree
Showing 26 changed files with 308 additions and 206 deletions.
24 changes: 12 additions & 12 deletions pkg/front_end/lib/src/base/incremental_compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ class IncrementalCompiler implements IncrementalKernelGenerator {

// Clear cached calculations that points (potential) to now replaced
// things.
for (Builder builder in builder.scope.localMembers) {
for (Builder builder in builder.nameSpace.localMembers) {
if (builder is DillClassBuilder) {
builder.clearCachedValues();
}
Expand Down Expand Up @@ -1042,7 +1042,7 @@ class IncrementalCompiler implements IncrementalKernelGenerator {
if (builder.isBuiltAndMarked) {
// Clear cached calculations in classes which upon calculation can
// mark things as needed.
for (Builder builder in builder.scope.localMembers) {
for (Builder builder in builder.nameSpace.localMembers) {
if (builder is DillClassBuilder) {
builder.clearCachedValues();
}
Expand Down Expand Up @@ -1886,8 +1886,8 @@ class IncrementalCompiler implements IncrementalKernelGenerator {

Class? cls;
if (className != null) {
Builder? scopeMember =
libraryBuilder.scope.lookupLocalMember(className, setter: false);
Builder? scopeMember = libraryBuilder.nameSpace
.lookupLocalMember(className, setter: false);
if (scopeMember is ClassBuilder) {
cls = scopeMember.cls;
} else {
Expand All @@ -1902,8 +1902,8 @@ class IncrementalCompiler implements IncrementalKernelGenerator {
if (indexOfDot >= 0) {
String beforeDot = methodName.substring(0, indexOfDot);
String afterDot = methodName.substring(indexOfDot + 1);
Builder? builder =
libraryBuilder.scope.lookupLocalMember(beforeDot, setter: false);
Builder? builder = libraryBuilder.nameSpace
.lookupLocalMember(beforeDot, setter: false);
extensionName = beforeDot;
if (builder is ExtensionBuilder) {
extension = builder.extension;
Expand Down Expand Up @@ -1983,14 +1983,14 @@ class IncrementalCompiler implements IncrementalKernelGenerator {
isPatch: false,
);
debugLibrary.compilationUnit.createLibrary();
libraryBuilder.scope.forEachLocalMember((name, member) {
debugLibrary.scope.addLocalMember(name, member, setter: false);
libraryBuilder.nameSpace.forEachLocalMember((name, member) {
debugLibrary.nameSpace.addLocalMember(name, member, setter: false);
});
libraryBuilder.scope.forEachLocalSetter((name, member) {
debugLibrary.scope.addLocalMember(name, member, setter: true);
libraryBuilder.nameSpace.forEachLocalSetter((name, member) {
debugLibrary.nameSpace.addLocalMember(name, member, setter: true);
});
libraryBuilder.scope.forEachLocalExtension((member) {
debugLibrary.scope.addExtension(member);
libraryBuilder.nameSpace.forEachLocalExtension((member) {
debugLibrary.nameSpace.addExtension(member);
});
_ticker.logMs("Created debug library");

Expand Down
65 changes: 64 additions & 1 deletion pkg/front_end/lib/src/base/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// ignore_for_file: annotate_overrides

library fasta.scope;

import 'package:kernel/ast.dart';
Expand Down Expand Up @@ -178,7 +180,68 @@ class MutableScope {
String toString() => "Scope(${kind}, $classNameOrDebugName, ${_local?.keys})";
}

class Scope extends MutableScope implements ParentScope, LookupScope {
abstract class NameSpace {
void addLocalMember(String name, Builder member, {required bool setter});

Builder? lookupLocalMember(String name, {required bool setter});

void forEachLocalMember(void Function(String name, Builder member) f);

void forEachLocalSetter(void Function(String name, MemberBuilder member) f);

void forEachLocalExtension(void Function(ExtensionBuilder member) f);

Iterable<Builder> get localMembers;

/// Returns an iterator of all members and setters mapped in this scope,
/// including duplicate members mapped to the same name.
///
/// The iterator does _not_ include the members and setters mapped in the
/// [parent] scope.
Iterator<Builder> get unfilteredIterator;

/// Returns an iterator of all members and setters mapped in this scope,
/// including duplicate members mapped to the same name.
///
/// The iterator does _not_ include the members and setters mapped in the
/// [parent] scope.
///
/// Compared to [unfilteredIterator] this iterator also gives access to the
/// name that the builders are mapped to.
NameIterator get unfilteredNameIterator;

/// Returns a filtered iterator of members and setters mapped in this scope.
///
/// Only members of type [T] are included. If [parent] is provided, on members
/// declared in [parent] are included. If [includeDuplicates] is `true`, all
/// duplicates of the same name are included, otherwise, only the first
/// declared member is included. If [includeAugmentations] is `true`, both
/// original and augmenting/patching members are included, otherwise, only
/// original members are included.
Iterator<T> filteredIterator<T extends Builder>(
{Builder? parent,
required bool includeDuplicates,
required bool includeAugmentations});

/// Returns a filtered iterator of members and setters mapped in this scope.
///
/// Only members of type [T] are included. If [parent] is provided, on members
/// declared in [parent] are included. If [includeDuplicates] is `true`, all
/// duplicates of the same name are included, otherwise, only the first
/// declared member is included. If [includeAugmentations] is `true`, both
/// original and augmenting/patching members are included, otherwise, only
/// original members are included.
///
/// Compared to [filteredIterator] this iterator also gives access to the
/// name that the builders are mapped to.
NameIterator<T> filteredNameIterator<T extends Builder>(
{Builder? parent,
required bool includeDuplicates,
required bool includeAugmentations});
}

class Scope extends MutableScope
implements ParentScope, LookupScope, NameSpace {
/// Indicates whether an attempt to declare new names in this scope should
/// succeed.
final bool isModifiable;
Expand Down
4 changes: 2 additions & 2 deletions pkg/front_end/lib/src/builder/builder_mixins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ mixin DeclarationBuilderMixin implements IDeclarationBuilder {
}

void forEach(void f(String name, Builder builder)) {
scope
nameSpace
.filteredNameIterator(
includeDuplicates: false, includeAugmentations: false)
.forEach(f);
Expand All @@ -72,7 +72,7 @@ mixin DeclarationBuilderMixin implements IDeclarationBuilder {
{bool setter = false, bool required = false}) {
// TODO(johnniwinther): Support augmented on extensions/extension type
// declarations.
Builder? builder = scope.lookupLocalMember(name, setter: setter);
Builder? builder = nameSpace.lookupLocalMember(name, setter: setter);
if (required && builder == null) {
internalProblem(
templateInternalProblemNotFoundIn.withArguments(
Expand Down
17 changes: 5 additions & 12 deletions pkg/front_end/lib/src/builder/class_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,9 @@ abstract class ClassBuilderImpl extends DeclarationBuilderImpl
InterfaceType? _nonNullableRawType;
InterfaceType? _thisType;

ClassBuilderImpl(
List<MetadataBuilder>? metadata,
int modifiers,
String name,
Scope scope,
ConstructorScope constructorScope,
LibraryBuilder parent,
int charOffset)
: super(metadata, modifiers, name, parent, charOffset, scope,
constructorScope);
ClassBuilderImpl(List<MetadataBuilder>? metadata, int modifiers, String name,
LibraryBuilder parent, int charOffset)
: super(metadata, modifiers, name, parent, charOffset);

@override
String get debugName => "ClassBuilder";
Expand Down Expand Up @@ -236,10 +229,10 @@ abstract class ClassBuilderImpl extends DeclarationBuilderImpl
@override
Builder? lookupLocalMember(String name,
{bool setter = false, bool required = false}) {
Builder? builder = scope.lookupLocalMember(name, setter: setter);
Builder? builder = nameSpace.lookupLocalMember(name, setter: setter);
if (builder == null && isAugmenting) {
// Coverage-ignore-block(suite): Not run.
builder = origin.scope.lookupLocalMember(name, setter: setter);
builder = origin.nameSpace.lookupLocalMember(name, setter: setter);
}
if (required && builder == null) {
internalProblem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ConstructorReferenceBuilder {
"${suffix == null ? '' : '.$suffix'}";
}

void resolveIn(Scope scope, LibraryBuilder accessingLibrary) {
void resolveIn(LookupScope scope, LibraryBuilder accessingLibrary) {
Builder? declaration;
String? qualifier = typeName.qualifier;
if (qualifier != null) {
Expand Down
18 changes: 4 additions & 14 deletions pkg/front_end/lib/src/builder/declaration_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ part of 'declaration_builders.dart';
abstract class IDeclarationBuilder implements ITypeDeclarationBuilder {
Scope get scope;

NameSpace get nameSpace;

LibraryBuilder get libraryBuilder;

@override
Expand Down Expand Up @@ -48,23 +50,11 @@ abstract class IDeclarationBuilder implements ITypeDeclarationBuilder {

abstract class DeclarationBuilderImpl extends TypeDeclarationBuilderImpl
implements IDeclarationBuilder {
@override
final Scope scope;

@override
final ConstructorScope constructorScope;

@override
final Uri fileUri;

DeclarationBuilderImpl(
List<MetadataBuilder>? metadata,
int modifiers,
String name,
LibraryBuilder parent,
int charOffset,
this.scope,
this.constructorScope)
DeclarationBuilderImpl(List<MetadataBuilder>? metadata, int modifiers,
String name, LibraryBuilder parent, int charOffset)
: fileUri = parent.fileUri,
super(metadata, modifiers, name, parent, charOffset);

Expand Down
5 changes: 2 additions & 3 deletions pkg/front_end/lib/src/builder/extension_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ abstract class ExtensionBuilderImpl extends DeclarationBuilderImpl
with DeclarationBuilderMixin
implements ExtensionBuilder {
ExtensionBuilderImpl(List<MetadataBuilder>? metadata, int modifiers,
String name, LibraryBuilder parent, int charOffset, Scope scope)
: super(metadata, modifiers, name, parent, charOffset, scope,
new ConstructorScope(name, const {}));
String name, LibraryBuilder parent, int charOffset)
: super(metadata, modifiers, name, parent, charOffset);

@override
DartType buildAliasedTypeWithBuiltArguments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,9 @@ abstract class ExtensionTypeDeclarationBuilderImpl
extends DeclarationBuilderImpl
with DeclarationBuilderMixin
implements ExtensionTypeDeclarationBuilder {
ExtensionTypeDeclarationBuilderImpl(
List<MetadataBuilder>? metadata,
int modifiers,
String name,
LibraryBuilder parent,
int charOffset,
Scope scope,
ConstructorScope constructorScope)
: super(metadata, modifiers, name, parent, charOffset, scope,
constructorScope);
ExtensionTypeDeclarationBuilderImpl(List<MetadataBuilder>? metadata,
int modifiers, String name, LibraryBuilder parent, int charOffset)
: super(metadata, modifiers, name, parent, charOffset);

@override
DartType buildAliasedTypeWithBuiltArguments(
Expand Down
10 changes: 6 additions & 4 deletions pkg/front_end/lib/src/builder/library_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ abstract class SourceCompilationUnit implements CompilationUnit {
abstract class LibraryBuilder implements Builder, ProblemReporting {
Scope get scope;

NameSpace get nameSpace;

Scope get exportScope;

List<Export> get exporters;
Expand Down Expand Up @@ -420,19 +422,19 @@ abstract class LibraryBuilderImpl extends ModifierBuilderImpl

@override
Iterator<Builder> get localMembersIterator {
return scope.filteredIterator(
return nameSpace.filteredIterator(
parent: this, includeDuplicates: true, includeAugmentations: true);
}

@override
Iterator<T> localMembersIteratorOfType<T extends Builder>() {
return scope.filteredIterator<T>(
return nameSpace.filteredIterator<T>(
parent: this, includeDuplicates: true, includeAugmentations: true);
}

@override
NameIterator<Builder> get localMembersNameIterator {
return scope.filteredNameIterator(
return nameSpace.filteredNameIterator(
parent: this, includeDuplicates: true, includeAugmentations: true);
}

Expand Down Expand Up @@ -523,7 +525,7 @@ abstract class LibraryBuilderImpl extends ModifierBuilderImpl

@override
Builder? lookupLocalMember(String name, {bool required = false}) {
Builder? builder = scope.lookupLocalMember(name, setter: false);
Builder? builder = nameSpace.lookupLocalMember(name, setter: false);
if (required && builder == null) {
internalProblem(
templateInternalProblemNotFoundIn.withArguments(
Expand Down
2 changes: 1 addition & 1 deletion pkg/front_end/lib/src/builder/named_type_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ abstract class NamedTypeBuilderImpl extends NamedTypeBuilder {
}

@override
void resolveIn(Scope scope, int charOffset, Uri fileUri,
void resolveIn(LookupScope scope, int charOffset, Uri fileUri,
ProblemReporting problemReporting) {
if (_declaration != null) return;
Builder? member;
Expand Down
2 changes: 1 addition & 1 deletion pkg/front_end/lib/src/builder/type_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ abstract class NamedTypeBuilder extends TypeBuilder {
@override
TypeName get typeName;

void resolveIn(Scope scope, int charOffset, Uri fileUri,
void resolveIn(LookupScope scope, int charOffset, Uri fileUri,
ProblemReporting problemReporting);
void bind(
ProblemReporting problemReporting, TypeDeclarationBuilder declaration);
Expand Down
Loading

0 comments on commit 66658c8

Please sign in to comment.