[cfe] Change TypeParameterScopeBuilder to LibraryNameSpaceBuilder
This prepares for creating the library NameSpace after outline building.
Change-Id: I35bb6470341190d76f2005be4c962ae0d1bbe920
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/383300
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
diff --git a/pkg/front_end/lib/src/source/builder_factory.dart b/pkg/front_end/lib/src/source/builder_factory.dart
index f87cb2e..21c7c0b 100644
--- a/pkg/front_end/lib/src/source/builder_factory.dart
+++ b/pkg/front_end/lib/src/source/builder_factory.dart
@@ -66,11 +66,7 @@
void registerUnresolvedStructuralVariables(
List<StructuralVariableBuilder> unboundTypeVariables);
- Iterable<Builder> get members;
-
- Iterable<Builder> get setters;
-
- Iterable<ExtensionBuilder> get extensions;
+ Iterable<Builder> get builders;
List<LibraryPart> get libraryParts;
}
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 fe615aa..3deaa36 100644
--- a/pkg/front_end/lib/src/source/source_builder_factory.dart
+++ b/pkg/front_end/lib/src/source/source_builder_factory.dart
@@ -34,19 +34,15 @@
lateMask,
namedMixinApplicationMask,
staticMask;
-import '../base/name_space.dart';
-import '../base/problems.dart' show internalProblem, unexpected, unhandled;
+import '../base/problems.dart' show internalProblem, unhandled;
import '../base/scope.dart';
-import '../base/uri_offset.dart';
import '../base/uris.dart';
import '../builder/builder.dart';
import '../builder/constructor_reference_builder.dart';
import '../builder/declaration_builders.dart';
import '../builder/formal_parameter_builder.dart';
-import '../builder/function_builder.dart';
import '../builder/function_type_builder.dart';
import '../builder/library_builder.dart';
-import '../builder/member_builder.dart';
import '../builder/metadata_builder.dart';
import '../builder/mixin_application_builder.dart';
import '../builder/named_type_builder.dart';
@@ -90,7 +86,7 @@
// the outline builder.
final SourceLibraryBuilder _parent;
- final TypeParameterScopeBuilder _libraryTypeParameterScopeBuilder;
+ final LibraryNameSpaceBuilder _libraryNameSpaceBuilder;
/// Index of the library we use references for.
final IndexedLibrary? indexedLibrary;
@@ -114,9 +110,6 @@
@override
final List<Export> exports = <Export>[];
- /// List of [PrefixBuilder]s for imports with prefixes.
- List<PrefixBuilder>? _prefixBuilders;
-
/// Map from synthesized names used for omitted types to their corresponding
/// synthesized type declarations.
///
@@ -136,8 +129,6 @@
final LookupScope _scope;
- final NameSpace _nameSpace;
-
/// Index for building unique lowered names for wildcard variables.
int wildcardVariableIndex = 0;
@@ -156,20 +147,18 @@
{required SourceCompilationUnit compilationUnit,
required SourceLibraryBuilder augmentationRoot,
required SourceLibraryBuilder parent,
- required TypeParameterScopeBuilder libraryTypeParameterScopeBuilder,
+ required LibraryNameSpaceBuilder libraryNameSpaceBuilder,
required ProblemReporting problemReporting,
required LookupScope scope,
- required NameSpace nameSpace,
required LibraryName libraryName,
required IndexedLibrary? indexedLibrary,
required Map<String, Builder>? omittedTypeDeclarationBuilders})
: _compilationUnit = compilationUnit,
_augmentationRoot = augmentationRoot,
- _libraryTypeParameterScopeBuilder = libraryTypeParameterScopeBuilder,
+ _libraryNameSpaceBuilder = libraryNameSpaceBuilder,
_problemReporting = problemReporting,
_parent = parent,
_scope = scope,
- _nameSpace = nameSpace,
libraryName = libraryName,
indexedLibrary = indexedLibrary,
_omittedTypeDeclarationBuilders = omittedTypeDeclarationBuilders,
@@ -2825,110 +2814,8 @@
Builder _addBuilderToLibrary(
String name, Builder declaration, int charOffset) {
assert(_declarationFragments.isEmpty);
- if (declaration is SourceExtensionBuilder &&
- declaration.isUnnamedExtension) {
- declaration.parent = _parent;
- _libraryTypeParameterScopeBuilder.extensions.add(declaration);
- return declaration;
- }
-
- if (declaration is MemberBuilder) {
- declaration.parent = _parent;
- } else if (declaration is TypeDeclarationBuilder) {
- declaration.parent = _parent;
- } else if (declaration is PrefixBuilder) {
- assert(declaration.parent == _parent);
- } else {
- return unhandled("${declaration.runtimeType}", "addBuilder", charOffset,
- _compilationUnit.fileUri);
- }
-
- assert(
- !(declaration is FunctionBuilder &&
- (declaration.isConstructor || declaration.isFactory)),
- // Coverage-ignore(suite): Not run.
- "Unexpected constructor in library: $declaration.");
-
- Map<String, Builder> members = declaration.isSetter
- ? _libraryTypeParameterScopeBuilder.setters
- : _libraryTypeParameterScopeBuilder.members;
-
- Builder? existing = members[name];
-
- if (existing == declaration) return declaration;
-
- if (declaration.next != null && declaration.next != existing) {
- unexpected(
- "${declaration.next!.fileUri}@${declaration.next!.charOffset}",
- "${existing?.fileUri}@${existing?.charOffset}",
- declaration.charOffset,
- declaration.fileUri);
- }
- declaration.next = existing;
- if (declaration is PrefixBuilder && existing is PrefixBuilder) {
- assert(existing.next is! PrefixBuilder);
- Builder? deferred;
- Builder? other;
- if (declaration.deferred) {
- deferred = declaration;
- other = existing;
- } else if (existing.deferred) {
- deferred = existing;
- other = declaration;
- }
- if (deferred != null) {
- // Coverage-ignore-block(suite): Not run.
- _problemReporting.addProblem(
- templateDeferredPrefixDuplicated.withArguments(name),
- deferred.charOffset,
- noLength,
- _compilationUnit.fileUri,
- context: [
- templateDeferredPrefixDuplicatedCause
- .withArguments(name)
- .withLocation(
- _compilationUnit.fileUri, other!.charOffset, noLength)
- ]);
- }
- existing.mergeScopes(declaration, _problemReporting, _nameSpace,
- uriOffset: new UriOffset(_compilationUnit.fileUri, charOffset));
- return existing;
- } else if (isDuplicatedDeclaration(existing, declaration)) {
- String fullName = name;
- _problemReporting.addProblem(
- templateDuplicatedDeclaration.withArguments(fullName),
- charOffset,
- fullName.length,
- declaration.fileUri!,
- context: <LocatedMessage>[
- templateDuplicatedDeclarationCause
- .withArguments(fullName)
- .withLocation(
- existing!.fileUri!, existing.charOffset, fullName.length)
- ]);
- } else if (declaration.isExtension) {
- // We add the extension declaration to the extension scope only if its
- // name is unique. Only the first of duplicate extensions is accessible
- // by name or by resolution and the remaining are dropped for the output.
- _libraryTypeParameterScopeBuilder.extensions
- .add(declaration as SourceExtensionBuilder);
- } else if (declaration.isAugment) {
- if (existing != null) {
- if (declaration.isSetter) {
- (_libraryTypeParameterScopeBuilder.setterAugmentations[name] ??= [])
- .add(declaration);
- } else {
- (_libraryTypeParameterScopeBuilder.augmentations[name] ??= [])
- .add(declaration);
- }
- } else {
- // TODO(cstefantsova): Report an error.
- }
- } else if (declaration is PrefixBuilder) {
- _prefixBuilders ??= <PrefixBuilder>[];
- _prefixBuilders!.add(declaration);
- }
- return members[name] = declaration;
+ return _libraryNameSpaceBuilder.addBuilder(_parent, _problemReporting, name,
+ declaration, _compilationUnit.fileUri, charOffset);
}
void _addBuilderToDeclaration(
@@ -2973,16 +2860,7 @@
List<MetadataBuilder>? get metadata => _metadata;
@override
- Iterable<Builder> get members =>
- _libraryTypeParameterScopeBuilder.members.values;
-
- @override
- Iterable<Builder> get setters =>
- _libraryTypeParameterScopeBuilder.setters.values;
-
- @override
- Iterable<ExtensionBuilder> get extensions =>
- _libraryTypeParameterScopeBuilder.extensions;
+ Iterable<Builder> get builders => _libraryNameSpaceBuilder.builders;
@override
bool get isPart => _partOfName != null || _partOfUri != null;
@@ -2997,7 +2875,8 @@
List<Part> get parts => _parts;
@override
- List<PrefixBuilder>? get prefixBuilders => _prefixBuilders;
+ List<PrefixBuilder>? get prefixBuilders =>
+ _libraryNameSpaceBuilder.prefixBuilders;
@override
void registerUnresolvedNamedTypes(List<NamedTypeBuilder> unboundTypes) {
diff --git a/pkg/front_end/lib/src/source/source_compilation_unit.dart b/pkg/front_end/lib/src/source/source_compilation_unit.dart
index 0e76a18..22c556c 100644
--- a/pkg/front_end/lib/src/source/source_compilation_unit.dart
+++ b/pkg/front_end/lib/src/source/source_compilation_unit.dart
@@ -83,7 +83,7 @@
final bool isUnsupported;
SourceCompilationUnitImpl(this._sourceLibraryBuilder,
- TypeParameterScopeBuilder libraryTypeParameterScopeBuilder,
+ LibraryNameSpaceBuilder libraryNameSpaceBuilder,
{required this.importUri,
required this.fileUri,
required Uri? packageUri,
@@ -107,10 +107,9 @@
compilationUnit: this,
augmentationRoot: _sourceLibraryBuilder,
parent: _sourceLibraryBuilder,
- libraryTypeParameterScopeBuilder: libraryTypeParameterScopeBuilder,
+ libraryNameSpaceBuilder: libraryNameSpaceBuilder,
problemReporting: this,
scope: _scope,
- nameSpace: _nameSpace,
libraryName: _libraryName,
indexedLibrary: indexedLibrary,
omittedTypeDeclarationBuilders: omittedTypeDeclarationBuilders);
@@ -1052,18 +1051,9 @@
}
}
- for (Builder declaration in _builderFactoryResult.members) {
+ for (Builder declaration in _builderFactoryResult.builders) {
computeDefaultValuesForDeclaration(declaration);
}
- for (Builder declaration in _builderFactoryResult.setters) {
- computeDefaultValuesForDeclaration(declaration);
- }
- for (ExtensionBuilder declaration in _builderFactoryResult.extensions) {
- if (declaration is SourceExtensionBuilder &&
- declaration.isUnnamedExtension) {
- computeDefaultValuesForDeclaration(declaration);
- }
- }
return count;
}
@@ -1162,7 +1152,7 @@
int computeVariances() {
int count = 0;
- for (Builder? declaration in _builderFactoryResult.members) {
+ for (Builder? declaration in _builderFactoryResult.builders) {
while (declaration != null) {
if (declaration is TypeAliasBuilder &&
declaration.typeVariablesCount > 0) {
diff --git a/pkg/front_end/lib/src/source/source_library_builder.dart b/pkg/front_end/lib/src/source/source_library_builder.dart
index 268a0e2..96c0057 100644
--- a/pkg/front_end/lib/src/source/source_library_builder.dart
+++ b/pkg/front_end/lib/src/source/source_library_builder.dart
@@ -219,8 +219,8 @@
: indexedLibrary?.library.reference)
..setLanguageVersion(packageLanguageVersion.version));
LibraryName libraryName = new LibraryName(library.reference);
- TypeParameterScopeBuilder libraryTypeParameterScopeBuilder =
- new TypeParameterScopeBuilder();
+ LibraryNameSpaceBuilder libraryNameSpaceBuilder =
+ new LibraryNameSpaceBuilder();
NameSpace? importNameSpace = new NameSpaceImpl();
LookupScope importScope = new NameSpaceLookupScope(
importNameSpace, ScopeKind.library, 'top',
@@ -228,7 +228,7 @@
importScope = new FixedLookupScope(
ScopeKind.typeParameters, 'omitted-types',
getables: omittedTypes, parent: importScope);
- NameSpace libraryNameSpace = libraryTypeParameterScopeBuilder.toNameSpace();
+ NameSpace libraryNameSpace = libraryNameSpaceBuilder.toNameSpace();
NameSpace exportNameSpace = origin?.exportNameSpace ?? new NameSpaceImpl();
return new SourceLibraryBuilder._(
loader: loader,
@@ -237,7 +237,7 @@
packageUri: packageUri,
originImportUri: originImportUri,
packageLanguageVersion: packageLanguageVersion,
- libraryTypeParameterScopeBuilder: libraryTypeParameterScopeBuilder,
+ libraryNameSpaceBuilder: libraryNameSpaceBuilder,
importNameSpace: importNameSpace,
importScope: importScope,
libraryNameSpace: libraryNameSpace,
@@ -251,9 +251,8 @@
isAugmentation: isAugmentation,
isPatch: isPatch,
omittedTypes: omittedTypes,
- augmentations: libraryTypeParameterScopeBuilder.augmentations,
- setterAugmentations:
- libraryTypeParameterScopeBuilder.setterAugmentations);
+ augmentations: libraryNameSpaceBuilder.augmentations,
+ setterAugmentations: libraryNameSpaceBuilder.setterAugmentations);
}
SourceLibraryBuilder._(
@@ -263,7 +262,7 @@
required Uri? packageUri,
required Uri originImportUri,
required LanguageVersion packageLanguageVersion,
- required TypeParameterScopeBuilder libraryTypeParameterScopeBuilder,
+ required LibraryNameSpaceBuilder libraryNameSpaceBuilder,
required NameSpace importNameSpace,
required LookupScope importScope,
required NameSpace libraryNameSpace,
@@ -302,7 +301,7 @@
_scope = new SourceLibraryBuilderScope(
this, ScopeKind.typeParameters, 'library');
compilationUnit = new SourceCompilationUnitImpl(
- this, libraryTypeParameterScopeBuilder,
+ this, libraryNameSpaceBuilder,
importUri: importUri,
fileUri: fileUri,
packageUri: _packageUri,
diff --git a/pkg/front_end/lib/src/source/type_parameter_scope_builder.dart b/pkg/front_end/lib/src/source/type_parameter_scope_builder.dart
index 3bd4e8a..3f944f3 100644
--- a/pkg/front_end/lib/src/source/type_parameter_scope_builder.dart
+++ b/pkg/front_end/lib/src/source/type_parameter_scope_builder.dart
@@ -6,37 +6,158 @@
import '../base/name_space.dart';
import '../base/problems.dart';
import '../base/scope.dart';
+import '../base/uri_offset.dart';
import '../builder/builder.dart';
import '../builder/declaration_builders.dart';
import '../builder/function_builder.dart';
import '../builder/member_builder.dart';
+import '../builder/prefix_builder.dart';
import '../builder/type_builder.dart';
import 'name_scheme.dart';
+import 'source_extension_builder.dart';
import 'source_field_builder.dart';
+import 'source_library_builder.dart';
-/// A builder object preparing for building declarations that can introduce type
-/// parameter and/or members.
-///
-/// Unlike [Scope], this scope is used during construction of builders to
-/// ensure types and members are added to and resolved in the correct location.
-class TypeParameterScopeBuilder {
- final Map<String, Builder> members = {};
+class LibraryNameSpaceBuilder {
+ final Map<String, Builder> _members = {};
- final Map<String, MemberBuilder> setters = {};
+ final Map<String, MemberBuilder> _setters = {};
- final Set<ExtensionBuilder> extensions = {};
+ final Set<ExtensionBuilder> _extensions = {};
final Map<String, List<Builder>> augmentations = {};
final Map<String, List<Builder>> setterAugmentations = {};
- NameSpace toNameSpace() {
- return new NameSpaceImpl(
- getables: members, setables: setters, extensions: extensions);
+ /// List of [PrefixBuilder]s for imports with prefixes.
+ List<PrefixBuilder>? _prefixBuilders;
+
+ late final NameSpace _nameSpace;
+
+ LibraryNameSpaceBuilder() {
+ _nameSpace = new NameSpaceImpl(
+ getables: _members, setables: _setters, extensions: _extensions);
}
- @override
- String toString() => 'TypeParameterScopeBuilder(${hashCode})';
+ Iterable<Builder> get builders => [
+ ..._members.values,
+ ..._setters.values,
+ for (Builder builder in _extensions)
+ if (builder is SourceExtensionBuilder && builder.isUnnamedExtension)
+ builder
+ ];
+
+ Builder addBuilder(
+ SourceLibraryBuilder _parent,
+ ProblemReporting _problemReporting,
+ String name,
+ Builder declaration,
+ Uri fileUri,
+ int charOffset) {
+ if (declaration is SourceExtensionBuilder &&
+ declaration.isUnnamedExtension) {
+ declaration.parent = _parent;
+ _extensions.add(declaration);
+ return declaration;
+ }
+
+ if (declaration is MemberBuilder) {
+ declaration.parent = _parent;
+ } else if (declaration is TypeDeclarationBuilder) {
+ declaration.parent = _parent;
+ } else if (declaration is PrefixBuilder) {
+ assert(declaration.parent == _parent);
+ } else {
+ return unhandled(
+ "${declaration.runtimeType}", "addBuilder", charOffset, fileUri);
+ }
+
+ assert(
+ !(declaration is FunctionBuilder &&
+ (declaration.isConstructor || declaration.isFactory)),
+ // Coverage-ignore(suite): Not run.
+ "Unexpected constructor in library: $declaration.");
+
+ Map<String, Builder> members =
+ declaration.isSetter ? _setters : this._members;
+
+ Builder? existing = members[name];
+
+ if (existing == declaration) return declaration;
+
+ if (declaration.next != null && declaration.next != existing) {
+ unexpected(
+ "${declaration.next!.fileUri}@${declaration.next!.charOffset}",
+ "${existing?.fileUri}@${existing?.charOffset}",
+ declaration.charOffset,
+ declaration.fileUri);
+ }
+ declaration.next = existing;
+ if (declaration is PrefixBuilder && existing is PrefixBuilder) {
+ assert(existing.next is! PrefixBuilder);
+ Builder? deferred;
+ Builder? other;
+ if (declaration.deferred) {
+ deferred = declaration;
+ other = existing;
+ } else if (existing.deferred) {
+ deferred = existing;
+ other = declaration;
+ }
+ if (deferred != null) {
+ // Coverage-ignore-block(suite): Not run.
+ _problemReporting.addProblem(
+ templateDeferredPrefixDuplicated.withArguments(name),
+ deferred.charOffset,
+ noLength,
+ fileUri,
+ context: [
+ templateDeferredPrefixDuplicatedCause
+ .withArguments(name)
+ .withLocation(fileUri, other!.charOffset, noLength)
+ ]);
+ }
+ existing.mergeScopes(declaration, _problemReporting, _nameSpace,
+ uriOffset: new UriOffset(fileUri, charOffset));
+ return existing;
+ } else if (isDuplicatedDeclaration(existing, declaration)) {
+ String fullName = name;
+ _problemReporting.addProblem(
+ templateDuplicatedDeclaration.withArguments(fullName),
+ charOffset,
+ fullName.length,
+ declaration.fileUri!,
+ context: <LocatedMessage>[
+ templateDuplicatedDeclarationCause
+ .withArguments(fullName)
+ .withLocation(
+ existing!.fileUri!, existing.charOffset, fullName.length)
+ ]);
+ } else if (declaration.isExtension) {
+ // We add the extension declaration to the extension scope only if its
+ // name is unique. Only the first of duplicate extensions is accessible
+ // by name or by resolution and the remaining are dropped for the output.
+ _extensions.add(declaration as SourceExtensionBuilder);
+ } else if (declaration.isAugment) {
+ if (existing != null) {
+ if (declaration.isSetter) {
+ (setterAugmentations[name] ??= []).add(declaration);
+ } else {
+ (augmentations[name] ??= []).add(declaration);
+ }
+ } else {
+ // TODO(cstefantsova): Report an error.
+ }
+ } else if (declaration is PrefixBuilder) {
+ _prefixBuilders ??= <PrefixBuilder>[];
+ _prefixBuilders!.add(declaration);
+ }
+ return members[name] = declaration;
+ }
+
+ List<PrefixBuilder>? get prefixBuilders => _prefixBuilders;
+
+ NameSpace toNameSpace() => _nameSpace;
}
class NominalParameterScope extends AbstractTypeParameterScope {