[cfe] Use NameSpace and LookupScope in declaration builders
Change-Id: I695077e10bae94805da313fa219fc1fbb553faed
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/377660
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
diff --git a/pkg/front_end/lib/src/base/name_space.dart b/pkg/front_end/lib/src/base/name_space.dart
index 2748d7b..e4e2bb7 100644
--- a/pkg/front_end/lib/src/base/name_space.dart
+++ b/pkg/front_end/lib/src/base/name_space.dart
@@ -74,6 +74,14 @@
Map<String, MemberBuilder>? _setables;
Set<ExtensionBuilder>? _extensions;
+ NameSpaceImpl(
+ {Map<String, Builder>? getables,
+ Map<String, MemberBuilder>? setables,
+ Set<ExtensionBuilder>? extensions})
+ : _getables = getables,
+ _setables = setables,
+ _extensions = extensions;
+
@override
void addLocalMember(String name, Builder member, {required bool setter}) {
if (setter) {
diff --git a/pkg/front_end/lib/src/builder/library_builder.dart b/pkg/front_end/lib/src/builder/library_builder.dart
index bdf6509..53f5dff 100644
--- a/pkg/front_end/lib/src/builder/library_builder.dart
+++ b/pkg/front_end/lib/src/builder/library_builder.dart
@@ -467,7 +467,7 @@
} else {
if (existing != null) {
Builder result = computeAmbiguousDeclarationForScope(
- this, scope, name, existing, member,
+ this, nameSpace, name, existing, member,
uriOffset: uriOffset, isExport: true);
exportScope.addLocalMember(name, result, setter: member.isSetter);
return result != existing;
@@ -489,7 +489,7 @@
-1,
null);
}
- Builder? cls = (bypassLibraryPrivacy ? scope : exportScope)
+ Builder? cls = (bypassLibraryPrivacy ? nameSpace : exportScope)
.lookupLocalMember(className, setter: false);
if (cls is TypeAliasBuilder) {
// Coverage-ignore-block(suite): Not run.
diff --git a/pkg/front_end/lib/src/dill/dill_class_builder.dart b/pkg/front_end/lib/src/dill/dill_class_builder.dart
index 09cabe9..133b6c2 100644
--- a/pkg/front_end/lib/src/dill/dill_class_builder.dart
+++ b/pkg/front_end/lib/src/dill/dill_class_builder.dart
@@ -53,7 +53,9 @@
@override
final Class cls;
- final Scope _scope;
+ late final LookupScope _scope;
+
+ final NameSpace _nameSpace;
@override
final ConstructorScope constructorScope;
@@ -65,23 +67,21 @@
List<TypeBuilder>? _interfaceBuilders;
DillClassBuilder(this.cls, DillLibraryBuilder parent)
- : _scope = new Scope(
- kind: ScopeKind.declaration,
- local: <String, MemberBuilder>{},
- setters: <String, MemberBuilder>{},
- parent: parent.scope,
- debugName: "class ${cls.name}",
- isModifiable: false),
+ : _nameSpace = new NameSpaceImpl(),
constructorScope =
new ConstructorScope(cls.name, <String, MemberBuilder>{}),
super(/*metadata builders*/ null, computeModifiers(cls), cls.name,
- parent, cls.fileOffset);
+ parent, cls.fileOffset) {
+ _scope = new NameSpaceLookupScope(
+ _nameSpace, ScopeKind.declaration, "class ${cls.name}",
+ parent: parent.scope);
+ }
@override
LookupScope get scope => _scope;
@override
- NameSpace get nameSpace => _scope;
+ NameSpace get nameSpace => _nameSpace;
@override
bool get isEnum => cls.isEnum;
diff --git a/pkg/front_end/lib/src/dill/dill_extension_builder.dart b/pkg/front_end/lib/src/dill/dill_extension_builder.dart
index 0626e81..f4f0980 100644
--- a/pkg/front_end/lib/src/dill/dill_extension_builder.dart
+++ b/pkg/front_end/lib/src/dill/dill_extension_builder.dart
@@ -8,7 +8,6 @@
import '../base/scope.dart';
import '../builder/declaration_builders.dart';
import '../builder/library_builder.dart';
-import '../builder/member_builder.dart';
import '../builder/type_builder.dart';
import 'dill_builder_mixins.dart';
import 'dill_class_builder.dart';
@@ -19,7 +18,9 @@
@override
final Extension extension;
- final Scope _scope;
+ late final LookupScope _scope;
+
+ final NameSpace _nameSpace;
@override
final ConstructorScope constructorScope;
@@ -28,16 +29,13 @@
TypeBuilder? _onType;
DillExtensionBuilder(this.extension, LibraryBuilder parent)
- : _scope = new Scope(
- kind: ScopeKind.declaration,
- local: <String, MemberBuilder>{},
- setters: <String, MemberBuilder>{},
- parent: parent.scope,
- debugName: "extension ${extension.name}",
- isModifiable: false),
+ : _nameSpace = new NameSpaceImpl(),
constructorScope = new ConstructorScope(extension.name, const {}),
super(/* metadata = */ null, 0, extension.name, parent,
extension.fileOffset) {
+ _scope = new NameSpaceLookupScope(
+ _nameSpace, ScopeKind.declaration, "extension ${extension.name}",
+ parent: parent.scope);
for (ExtensionMemberDescriptor descriptor in extension.memberDescriptors) {
Name name = descriptor.name;
switch (descriptor.kind) {
@@ -94,7 +92,7 @@
LookupScope get scope => _scope;
@override
- NameSpace get nameSpace => _scope;
+ NameSpace get nameSpace => _nameSpace;
@override
List<NominalVariableBuilder>? get typeParameters {
diff --git a/pkg/front_end/lib/src/dill/dill_extension_type_declaration_builder.dart b/pkg/front_end/lib/src/dill/dill_extension_type_declaration_builder.dart
index e9fc120..1399162 100644
--- a/pkg/front_end/lib/src/dill/dill_extension_type_declaration_builder.dart
+++ b/pkg/front_end/lib/src/dill/dill_extension_type_declaration_builder.dart
@@ -20,7 +20,9 @@
with DillClassMemberAccessMixin, DillDeclarationBuilderMixin {
final ExtensionTypeDeclaration _extensionTypeDeclaration;
- final Scope _scope;
+ late final LookupScope _scope;
+
+ final NameSpace _nameSpace;
@override
final ConstructorScope constructorScope;
@@ -33,23 +35,18 @@
DillExtensionTypeDeclarationBuilder(
this._extensionTypeDeclaration, DillLibraryBuilder parent)
- : _scope = new Scope(
- kind: ScopeKind.declaration,
- local: <String, MemberBuilder>{},
- setters: <String, MemberBuilder>{},
- parent: parent.scope,
- debugName: "extension type ${_extensionTypeDeclaration.name}",
- isModifiable: false),
+ : _nameSpace = new NameSpaceImpl(),
constructorScope = new ConstructorScope(
_extensionTypeDeclaration.name, <String, MemberBuilder>{}),
super(
- /*metadata builders*/
- null,
- /* modifiers*/
- 0,
+ /*metadata builders*/ null,
+ /* modifiers*/ 0,
_extensionTypeDeclaration.name,
parent,
_extensionTypeDeclaration.fileOffset) {
+ _scope = new NameSpaceLookupScope(_nameSpace, ScopeKind.declaration,
+ "extension type ${_extensionTypeDeclaration.name}",
+ parent: parent.scope);
for (Procedure procedure in _extensionTypeDeclaration.procedures) {
String name = procedure.name.text;
switch (procedure.kind) {
@@ -155,7 +152,7 @@
LookupScope get scope => _scope;
@override
- NameSpace get nameSpace => _scope;
+ NameSpace get nameSpace => _nameSpace;
@override
DartType get declaredRepresentationType =>
diff --git a/pkg/front_end/lib/src/source/source_builder_factory.dart b/pkg/front_end/lib/src/source/source_builder_factory.dart
index 9293c86..ce75071 100644
--- a/pkg/front_end/lib/src/source/source_builder_factory.dart
+++ b/pkg/front_end/lib/src/source/source_builder_factory.dart
@@ -508,15 +508,10 @@
Map<String, MemberBuilder> constructors = declaration.constructors!;
Map<String, MemberBuilder> setters = declaration.setters!;
- Scope typeParameterScope = scope.withTypeVariables(typeVariables);
- Scope enumScope = new Scope(
- kind: ScopeKind.declaration,
- local: members,
- setters: setters,
- parent: typeParameterScope,
- debugName: "enum $name",
- isModifiable: false);
- NameSpace enumNameSpace = enumScope;
+ LookupScope typeParameterScope =
+ TypeParameterScope.fromList(scope, typeVariables);
+ NameSpace enumNameSpace =
+ new NameSpaceImpl(getables: members, setables: setters);
SourceEnumBuilder enumBuilder = new SourceEnumBuilder(
metadata,
name,
@@ -547,7 +542,6 @@
charEndOffset,
referencesFromIndexedClass,
typeParameterScope,
- enumScope,
enumNameSpace,
new ConstructorScope(name, constructors),
loader.coreLibrary);
@@ -670,14 +664,10 @@
Map<String, MemberBuilder> constructors = declaration.constructors!;
Map<String, MemberBuilder> setters = declaration.setters!;
- Scope typeParameterScope = scope.withTypeVariables(typeVariables);
- Scope classScope = new Scope(
- kind: ScopeKind.declaration,
- local: members,
- setters: setters,
- parent: typeParameterScope,
- debugName: "class $className",
- isModifiable: false);
+ LookupScope typeParameterScope =
+ TypeParameterScope.fromList(scope, typeVariables);
+ NameSpace classNameSpace =
+ new NameSpaceImpl(getables: members, setables: setters);
// When looking up a constructor, we don't consider type variables or the
// library scope.
@@ -712,7 +702,7 @@
// here.
null,
typeParameterScope,
- classScope,
+ classNameSpace,
constructorScope,
_parent,
new List<ConstructorReferenceBuilder>.of(_constructorReferences),
@@ -1070,7 +1060,9 @@
indexedLibrary!.lookupIndexedClass(fullname);
}
- Scope typeParameterScope = scope.withTypeVariables(typeVariables);
+ LookupScope typeParameterScope =
+ TypeParameterScope.fromList(scope, typeVariables);
+ NameSpace nameSpace = new NameSpaceImpl();
SourceClassBuilder application = new SourceClassBuilder(
isNamedMixinApplication ? metadata : null,
isNamedMixinApplication
@@ -1086,13 +1078,7 @@
: null,
null, // No `on` clause types.
typeParameterScope,
- new Scope(
- kind: ScopeKind.declaration,
- local: <String, MemberBuilder>{},
- setters: <String, MemberBuilder>{},
- parent: typeParameterScope,
- debugName: "mixin $fullname ",
- isModifiable: false),
+ nameSpace,
new ConstructorScope(fullname, <String, MemberBuilder>{}),
_parent,
<ConstructorReferenceBuilder>[],
@@ -1164,14 +1150,10 @@
Map<String, MemberBuilder> constructors = declaration.constructors!;
Map<String, MemberBuilder> setters = declaration.setters!;
- Scope typeParameterScope = scope.withTypeVariables(typeVariables);
- Scope classScope = new Scope(
- kind: ScopeKind.declaration,
- local: members,
- setters: setters,
- parent: typeParameterScope,
- debugName: "extension $name",
- isModifiable: false);
+ LookupScope typeParameterScope =
+ TypeParameterScope.fromList(scope, typeVariables);
+ NameSpace extensionNameSpace =
+ new NameSpaceImpl(getables: members, setables: setters);
Extension? referenceFrom;
ExtensionName extensionName = declaration.extensionName!;
@@ -1186,7 +1168,7 @@
typeVariables,
type,
typeParameterScope,
- classScope,
+ extensionNameSpace,
_parent,
startOffset,
nameOffset,
@@ -1252,14 +1234,10 @@
Map<String, MemberBuilder> constructors = declaration.constructors!;
Map<String, MemberBuilder> setters = declaration.setters!;
- Scope typeParameterScope = scope.withTypeVariables(typeVariables);
- Scope memberScope = new Scope(
- kind: ScopeKind.declaration,
- local: members,
- setters: setters,
- parent: typeParameterScope,
- debugName: "extension type $name",
- isModifiable: false);
+ LookupScope typeParameterScope =
+ TypeParameterScope.fromList(scope, typeVariables);
+ NameSpace extensionTypeNameSpace =
+ new NameSpaceImpl(getables: members, setables: setters);
ConstructorScope constructorScope =
new ConstructorScope(name, constructors);
@@ -1288,7 +1266,7 @@
typeVariables,
interfaces,
typeParameterScope,
- memberScope,
+ extensionTypeNameSpace,
constructorScope,
_parent,
new List<ConstructorReferenceBuilder>.of(_constructorReferences),
diff --git a/pkg/front_end/lib/src/source/source_class_builder.dart b/pkg/front_end/lib/src/source/source_class_builder.dart
index 1e3caf3..7ea41b3 100644
--- a/pkg/front_end/lib/src/source/source_class_builder.dart
+++ b/pkg/front_end/lib/src/source/source_class_builder.dart
@@ -92,7 +92,9 @@
implements Comparable<SourceClassBuilder>, ClassDeclaration {
final Class actualCls;
- final Scope _scope;
+ final LookupScope _scope;
+
+ final NameSpace _nameSpace;
@override
final ConstructorScope constructorScope;
@@ -173,7 +175,7 @@
this.interfaceBuilders,
this.onTypes,
this.typeParameterScope,
- Scope scope,
+ NameSpace nameSpace,
this.constructorScope,
SourceLibraryBuilder parent,
this.constructorReferences,
@@ -191,7 +193,10 @@
this.isFinal = false,
bool isAugmentation = false,
this.isMixinClass = false})
- : _scope = scope,
+ : _nameSpace = nameSpace,
+ _scope = new NameSpaceLookupScope(
+ nameSpace, ScopeKind.declaration, "class $name",
+ parent: typeParameterScope),
actualCls = initializeClass(cls, typeVariables, name, parent,
startCharOffset, nameOffset, charEndOffset, indexedContainer,
isAugmentation: isAugmentation),
@@ -204,14 +209,13 @@
LookupScope get scope => _scope;
@override
- NameSpace get nameSpace => _scope;
+ NameSpace get nameSpace => _nameSpace;
// TODO(johnniwinther): Remove this.
- Map<String, List<Builder>>? get augmentations => _scope.augmentations;
+ Map<String, List<Builder>>? get augmentations => null;
// TODO(johnniwinther): Remove this.
- Map<String, List<Builder>>? get setterAugmentations =>
- _scope.setterAugmentations;
+ Map<String, List<Builder>>? get setterAugmentations => null;
MergedClassMemberScope get mergedScope => _mergedScope ??= isAugmenting
?
diff --git a/pkg/front_end/lib/src/source/source_enum_builder.dart b/pkg/front_end/lib/src/source/source_enum_builder.dart
index 2978f73..dca9180 100644
--- a/pkg/front_end/lib/src/source/source_enum_builder.dart
+++ b/pkg/front_end/lib/src/source/source_enum_builder.dart
@@ -91,7 +91,7 @@
TypeBuilder supertypeBuilder,
List<TypeBuilder>? interfaceBuilders,
LookupScope typeParameterScope,
- Scope memberScope,
+ NameSpace nameSpace,
ConstructorScope constructors,
Class cls,
this.elementBuilders,
@@ -116,7 +116,7 @@
interfaceBuilders,
/* onTypes = */ null,
typeParameterScope,
- memberScope,
+ nameSpace,
constructors,
parent,
constructorReferences,
@@ -140,8 +140,7 @@
int charEndOffset,
IndexedClass? referencesFromIndexed,
LookupScope typeParameterScope,
- Scope memberScope,
- NameSpace memberNameSpace,
+ NameSpace enumNameSpace,
ConstructorScope constructorScope,
LibraryBuilder coreLibrary) {
assert(enumConstantInfos == null || enumConstantInfos.isNotEmpty);
@@ -238,7 +237,7 @@
}
Builder? customValuesDeclaration =
- memberNameSpace.lookupLocalMember("values", setter: false);
+ enumNameSpace.lookupLocalMember("values", setter: false);
if (customValuesDeclaration != null) {
// Retrieve the earliest declaration for error reporting.
while (customValuesDeclaration?.next != null) {
@@ -256,7 +255,7 @@
"hashCode",
"=="
]) {
- Builder? customIndexDeclaration = memberNameSpace
+ Builder? customIndexDeclaration = enumNameSpace
.lookupLocalMember(restrictedInstanceMemberName, setter: false);
if (customIndexDeclaration is MemberBuilder &&
!customIndexDeclaration.isAbstract) {
@@ -414,10 +413,10 @@
String className = name;
final int startCharOffsetComputed =
metadata == null ? startCharOffset : metadata.first.charOffset;
- memberNameSpace.forEachLocalMember((name, member) {
+ enumNameSpace.forEachLocalMember((name, member) {
members[name] = member as MemberBuilder;
});
- memberNameSpace.forEachLocalSetter((name, member) {
+ enumNameSpace.forEachLocalSetter((name, member) {
setters[name] = member;
});
@@ -508,14 +507,10 @@
supertypeBuilder,
interfaceBuilders,
typeParameterScope,
- new Scope(
- kind: ScopeKind.declaration,
- local: members,
- setters: setters,
- // TODO(johnniwinther): Why is the parent not the [memberScope]?
- parent: memberScope.parent,
- debugName: "enum $name",
- isModifiable: false),
+ // We create a new name space to include the synthesized members.
+ // TODO(johnniwinther): Could we add the new members directly to the
+ // name space?
+ new NameSpaceImpl(getables: members, setables: setters),
constructorScope..addLocalMembers(constructors),
cls,
elementBuilders,
diff --git a/pkg/front_end/lib/src/source/source_extension_builder.dart b/pkg/front_end/lib/src/source/source_extension_builder.dart
index 301114b..eebfe79 100644
--- a/pkg/front_end/lib/src/source/source_extension_builder.dart
+++ b/pkg/front_end/lib/src/source/source_extension_builder.dart
@@ -33,7 +33,9 @@
MergedClassMemberScope? _mergedScope;
- final Scope _scope;
+ final LookupScope _scope;
+
+ final NameSpace _nameSpace;
@override
final ConstructorScope constructorScope;
@@ -56,13 +58,16 @@
this.typeParameters,
this.onType,
this.typeParameterScope,
- Scope scope,
+ NameSpace nameSpace,
SourceLibraryBuilder parent,
int startOffset,
int nameOffset,
int endOffset,
Extension? referenceFrom)
- : _scope = scope,
+ : _nameSpace = nameSpace,
+ _scope = new NameSpaceLookupScope(
+ nameSpace, ScopeKind.declaration, "extension ${extensionName.name}",
+ parent: typeParameterScope),
_extension = new Extension(
name: extensionName.name,
fileUri: parent.fileUri,
@@ -80,7 +85,7 @@
LookupScope get scope => _scope;
@override
- NameSpace get nameSpace => _scope;
+ NameSpace get nameSpace => _nameSpace;
@override
SourceLibraryBuilder get libraryBuilder =>
diff --git a/pkg/front_end/lib/src/source/source_extension_type_declaration_builder.dart b/pkg/front_end/lib/src/source/source_extension_type_declaration_builder.dart
index a046074..e49e007 100644
--- a/pkg/front_end/lib/src/source/source_extension_type_declaration_builder.dart
+++ b/pkg/front_end/lib/src/source/source_extension_type_declaration_builder.dart
@@ -56,7 +56,9 @@
MergedClassMemberScope? _mergedScope;
- final Scope _scope;
+ final LookupScope _scope;
+
+ final NameSpace _nameSpace;
@override
final ConstructorScope constructorScope;
@@ -81,7 +83,7 @@
this.typeParameters,
this.interfaceBuilders,
this.typeParameterScope,
- Scope scope,
+ NameSpace nameSpace,
this.constructorScope,
SourceLibraryBuilder parent,
this.constructorReferences,
@@ -90,7 +92,10 @@
int endOffset,
this.indexedContainer,
this.representationFieldBuilder)
- : _scope = scope,
+ : _nameSpace = nameSpace,
+ _scope = new NameSpaceLookupScope(
+ nameSpace, ScopeKind.declaration, "extension type $name",
+ parent: typeParameterScope),
_extensionTypeDeclaration = new ExtensionTypeDeclaration(
name: name,
fileUri: parent.fileUri,
@@ -104,7 +109,7 @@
LookupScope get scope => _scope;
@override
- NameSpace get nameSpace => _scope;
+ NameSpace get nameSpace => _nameSpace;
@override
SourceLibraryBuilder get libraryBuilder =>
diff --git a/pkg/front_end/test/coverage_suite_expected.dart b/pkg/front_end/test/coverage_suite_expected.dart
index 23682b0..5a1eea8 100644
--- a/pkg/front_end/test/coverage_suite_expected.dart
+++ b/pkg/front_end/test/coverage_suite_expected.dart
@@ -178,10 +178,10 @@
hitCount: 29,
missCount: 0,
),
- // 73.52941176470588%.
+ // 96.35036496350365%.
"package:front_end/src/base/name_space.dart": (
- hitCount: 100,
- missCount: 36,
+ hitCount: 132,
+ missCount: 5,
),
// 100.0%.
"package:front_end/src/base/operator.dart": (
@@ -198,10 +198,10 @@
hitCount: 246,
missCount: 0,
),
- // 94.53883495145631%.
+ // 93.56796116504854%.
"package:front_end/src/base/scope.dart": (
- hitCount: 779,
- missCount: 45,
+ hitCount: 771,
+ missCount: 53,
),
// 100.0%.
"package:front_end/src/base/ticker.dart": (
@@ -413,14 +413,14 @@
hitCount: 16,
missCount: 0,
),
- // 98.84393063583815%.
+ // 98.85714285714286%.
"package:front_end/src/dill/dill_class_builder.dart": (
- hitCount: 171,
+ hitCount: 173,
missCount: 2,
),
- // 97.43589743589743%.
+ // 97.5%.
"package:front_end/src/dill/dill_extension_builder.dart": (
- hitCount: 76,
+ hitCount: 78,
missCount: 2,
),
// 100.0%.
@@ -428,9 +428,9 @@
hitCount: 71,
missCount: 0,
),
- // 98.66666666666667%.
+ // 98.68421052631578%.
"package:front_end/src/dill/dill_extension_type_declaration_builder.dart": (
- hitCount: 148,
+ hitCount: 150,
missCount: 2,
),
// 100.0%.
@@ -780,7 +780,7 @@
),
// 100.0%.
"package:front_end/src/source/source_builder_factory.dart": (
- hitCount: 1209,
+ hitCount: 1202,
missCount: 0,
),
// 98.13664596273291%.
@@ -788,9 +788,9 @@
hitCount: 158,
missCount: 3,
),
- // 99.75530179445352%.
+ // 99.75490196078431%.
"package:front_end/src/source/source_class_builder.dart": (
- hitCount: 1223,
+ hitCount: 1221,
missCount: 3,
),
// 99.47089947089947%.
@@ -805,18 +805,18 @@
),
// 100.0%.
"package:front_end/src/source/source_enum_builder.dart": (
- hitCount: 453,
+ hitCount: 451,
missCount: 0,
),
// 100.0%.
"package:front_end/src/source/source_extension_builder.dart": (
- hitCount: 73,
+ hitCount: 76,
missCount: 0,
),
// 100.0%.
"package:front_end/src/source/source_extension_type_declaration_builder.dart":
(
- hitCount: 430,
+ hitCount: 432,
missCount: 0,
),
// 100.0%.