Skip to content

Commit 62e726c

Browse files
johnniwintherCommit Queue
authored andcommitted
[cfe] Add CompilationUnitScope
This splits the library scope (and its import scope) into a compilation scope with a prefix and import for each compilation unit. This prepares for the scope rules of enhanced parts. Change-Id: Iba951b68c01a1dd60415a378a61dd53ee6064154 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/409080 Reviewed-by: Chloe Stefantsova <[email protected]> Commit-Queue: Johnni Winther <[email protected]>
1 parent 5de2352 commit 62e726c

File tree

50 files changed

+665
-154
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+665
-154
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,6 +1972,7 @@ class IncrementalCompiler implements IncrementalKernelGenerator {
19721972
debugLibrary.addImportsToScope();
19731973
_ticker.logMs("Added imports");
19741974
}
1975+
SourceCompilationUnit? orgCompilationUnit = debugCompilationUnit;
19751976
debugCompilationUnit = new SourceCompilationUnitImpl(
19761977
importUri: libraryUri,
19771978
fileUri: debugExprUri,
@@ -1992,25 +1993,25 @@ class IncrementalCompiler implements IncrementalKernelGenerator {
19921993
mayImplementRestrictedTypes: false);
19931994
debugCompilationUnit.markLanguageVersionFinal();
19941995

1995-
SourceLibraryBuilder? orgDebugLibrary = debugLibrary;
19961996
debugLibrary = debugCompilationUnit.createLibrary();
19971997

19981998
// Copy over the prefix namespace for extensions
19991999
// (`forEachExtensionInScope`) to be found when imported via prefixes.
2000-
// TODO(johnniwinther): Extensions should be available through
2001-
// [parentScope].
2002-
orgDebugLibrary.prefixNameSpace.forEachLocalMember((name, member) {
2003-
debugLibrary.prefixNameSpace
2000+
// TODO(johnniwinther): Do we still need these with the new scope
2001+
// structure?
2002+
orgCompilationUnit.prefixNameSpace.forEachLocalMember((name, member) {
2003+
debugCompilationUnit.prefixNameSpace
20042004
.addLocalMember(name, member, setter: false);
20052005
});
20062006
// Does a prefix namespace ever have anything but locals?
2007-
orgDebugLibrary.prefixNameSpace.forEachLocalSetter((name, member) {
2008-
debugLibrary.prefixNameSpace.addLocalMember(name, member, setter: true);
2007+
orgCompilationUnit.prefixNameSpace.forEachLocalSetter((name, member) {
2008+
debugCompilationUnit.prefixNameSpace
2009+
.addLocalMember(name, member, setter: true);
20092010
});
2010-
orgDebugLibrary.prefixNameSpace.forEachLocalExtension((member) {
2011-
debugLibrary.prefixNameSpace.addExtension(member);
2011+
orgCompilationUnit.prefixNameSpace.forEachLocalExtension((member) {
2012+
debugCompilationUnit.prefixNameSpace.addExtension(member);
20122013
});
2013-
orgDebugLibrary = null;
2014+
orgCompilationUnit = null;
20142015

20152016
HybridFileSystem hfs =
20162017
lastGoodKernelTarget.fileSystem as HybridFileSystem;

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

Lines changed: 102 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,23 @@ enum ScopeKind {
106106
/// Scope for type parameters of declarations
107107
typeParameters,
108108

109-
import,
109+
/// Scope for a compilation unit.
110+
///
111+
/// This contains the entities declared in the library to which the
112+
/// compilation unit belongs. Its parent scopes are the [prefix] and [import]
113+
/// scopes of the compilation unit.
114+
compilationUnit,
110115

116+
/// Scope for the prefixed imports in a compilation unit.
117+
///
118+
/// The parent scope is the [import] scope of the same compilation unit.
111119
prefix,
120+
121+
/// Scope for the non-prefixed imports in a compilation unit.
122+
///
123+
/// The parent scope is the [prefix] scope of the parent compilation unit,
124+
/// if any.
125+
import,
112126
}
113127

114128
abstract class LookupScope {
@@ -237,6 +251,9 @@ mixin LookupScopeMixin implements LookupScope {
237251
classNameOrDebugName: classNameOrDebugName,
238252
isSetter: true);
239253
}
254+
255+
@override
256+
String toString() => "$runtimeType(${kind},$classNameOrDebugName)";
240257
}
241258

242259
/// A [LookupScope] based directly on a [NameSpace].
@@ -283,6 +300,9 @@ abstract class BaseNameSpaceLookupScope implements LookupScope {
283300
_nameSpace.forEachLocalExtension(f);
284301
_parent?.forEachExtension(f);
285302
}
303+
304+
@override
305+
String toString() => "$runtimeType(${kind},$classNameOrDebugName)";
286306
}
287307

288308
class NameSpaceLookupScope extends BaseNameSpaceLookupScope {
@@ -337,10 +357,12 @@ abstract class AbstractTypeParameterScope implements LookupScope {
337357
String get classNameOrDebugName => "type parameter";
338358

339359
@override
340-
// Coverage-ignore(suite): Not run.
341360
void forEachExtension(void Function(ExtensionBuilder) f) {
342361
_parent.forEachExtension(f);
343362
}
363+
364+
@override
365+
String toString() => "$runtimeType(${kind},$classNameOrDebugName)";
344366
}
345367

346368
class TypeParameterScope extends AbstractTypeParameterScope {
@@ -410,12 +432,45 @@ class FixedLookupScope implements LookupScope {
410432
void forEachExtension(void Function(ExtensionBuilder) f) {
411433
_parent?.forEachExtension(f);
412434
}
435+
436+
@override
437+
String toString() => "$runtimeType(${kind},$classNameOrDebugName)";
413438
}
414439

415-
// Coverage-ignore(suite): Not run.
416-
// TODO(johnniwinther): Use this instead of [SourceLibraryBuilderScope].
440+
/// The import scope of a compilation unit.
441+
///
442+
/// This includes all declaration available through imports in this compilation
443+
/// unit. Its parent scope is the prefix scope of the parent compilation unit.
444+
/// If the compilation unit has no parent, the
445+
/// [SourceLibraryBuilder.parentScope] is used as the parent. This is not a
446+
/// normal Dart scope, but instead a synthesized scope used for expression
447+
/// compilation.
448+
class CompilationUnitImportScope extends BaseNameSpaceLookupScope {
449+
final SourceCompilationUnit _compilationUnit;
450+
final NameSpace _importNameSpace;
451+
452+
CompilationUnitImportScope(this._compilationUnit, this._importNameSpace)
453+
: super(ScopeKind.import, 'import');
454+
455+
@override
456+
NameSpace get _nameSpace => _importNameSpace;
457+
458+
@override
459+
LookupScope? get _parent =>
460+
_compilationUnit.parentCompilationUnit?.prefixScope ??
461+
_compilationUnit.libraryBuilder.parentScope;
462+
}
463+
464+
/// The scope of a compilation unit.
465+
///
466+
/// This is the enclosing scope for all declarations within the compilation
467+
/// unit. It gives access to all declarations in the library the compilation
468+
/// unit is part of. Its parent scope is the prefix scope, which contains all
469+
/// imports with prefixes declared in this compilation unit. The grand parent
470+
/// scope is the import scope of the compilation unit implemented through
471+
/// [CompilationUnitImportScope].
417472
class CompilationUnitScope extends BaseNameSpaceLookupScope {
418-
final CompilationUnit _compilationUnit;
473+
final SourceCompilationUnit _compilationUnit;
419474

420475
@override
421476
final LookupScope? _parent;
@@ -427,19 +482,54 @@ class CompilationUnitScope extends BaseNameSpaceLookupScope {
427482

428483
@override
429484
NameSpace get _nameSpace => _compilationUnit.libraryBuilder.libraryNameSpace;
430-
}
431485

432-
class SourceLibraryBuilderScope extends BaseNameSpaceLookupScope {
433-
final SourceCompilationUnit _compilationUnit;
486+
/// Set of extension declarations in scope. This is computed lazily in
487+
/// [forEachExtension].
488+
Set<ExtensionBuilder>? _extensions;
434489

435-
SourceLibraryBuilderScope(
436-
this._compilationUnit, super.kind, super.classNameOrDebugName);
490+
@override
491+
void forEachExtension(void Function(ExtensionBuilder) f) {
492+
if (_extensions == null) {
493+
Set<ExtensionBuilder> extensions = _extensions = <ExtensionBuilder>{};
494+
_parent?.forEachExtension(extensions.add);
495+
_nameSpace.forEachLocalExtension(extensions.add);
496+
}
497+
_extensions!.forEach(f);
498+
}
499+
}
437500

501+
/// The scope containing the prefixes imported into a compilation unit.
502+
class CompilationUnitPrefixScope extends BaseNameSpaceLookupScope {
438503
@override
439-
NameSpace get _nameSpace => _compilationUnit.libraryBuilder.libraryNameSpace;
504+
final NameSpace _nameSpace;
440505

441506
@override
442-
LookupScope? get _parent => _compilationUnit.libraryBuilder.prefixScope;
507+
final LookupScope? _parent;
508+
509+
CompilationUnitPrefixScope(
510+
this._nameSpace, super.kind, super.classNameOrDebugName,
511+
{required LookupScope? parent})
512+
: _parent = parent;
513+
514+
/// Set of extension declarations in scope. This is computed lazily in
515+
/// [forEachExtension].
516+
Set<ExtensionBuilder>? _extensions;
517+
518+
@override
519+
void forEachExtension(void Function(ExtensionBuilder) f) {
520+
if (_extensions == null) {
521+
Set<ExtensionBuilder> extensions = _extensions = {};
522+
Iterator<PrefixBuilder> iterator = _nameSpace.filteredIterator(
523+
includeDuplicates: false, includeAugmentations: false);
524+
while (iterator.moveNext()) {
525+
iterator.current.forEachExtension((e) {
526+
extensions.add(e);
527+
});
528+
}
529+
_parent?.forEachExtension(extensions.add);
530+
}
531+
_extensions!.forEach(f);
532+
}
443533
}
444534

445535
abstract class ConstructorScope {

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ abstract class SourceCompilationUnit implements CompilationUnit {
144144
@override
145145
SourceLibraryBuilder get libraryBuilder;
146146

147+
/// The parent compilation unit.
148+
///
149+
/// This is the compilation unit that included this compilation unit as a
150+
/// part or `null` if this is the root compilation unit of a library.
151+
SourceCompilationUnit? get parentCompilationUnit;
152+
147153
LibraryFeatures get libraryFeatures;
148154

149155
/// Returns `true` if the compilation unit is part of a `dart:` library.
@@ -161,7 +167,18 @@ abstract class SourceCompilationUnit implements CompilationUnit {
161167

162168
List<MetadataBuilder>? get metadata;
163169

164-
LookupScope get scope;
170+
/// The scope of this compilation unit.
171+
///
172+
/// This is the enclosing scope for all declarations within the compilation
173+
/// unit.
174+
LookupScope get compilationUnitScope;
175+
176+
/// The prefix scope of this compilation unit.
177+
///
178+
/// This contains all imports with prefixes declared in this compilation unit.
179+
LookupScope get prefixScope;
180+
181+
NameSpace get prefixNameSpace;
165182

166183
bool get mayImplementRestrictedTypes;
167184

@@ -173,8 +190,11 @@ abstract class SourceCompilationUnit implements CompilationUnit {
173190
void includeParts(SourceLibraryBuilder libraryBuilder,
174191
List<SourceCompilationUnit> includedParts, Set<Uri> usedParts);
175192

176-
void validatePart(SourceLibraryBuilder library,
177-
LibraryNameSpaceBuilder libraryNameSpaceBuilder, Set<Uri>? usedParts);
193+
void validatePart(
194+
SourceLibraryBuilder library,
195+
SourceCompilationUnit parentCompilationUnit,
196+
LibraryNameSpaceBuilder libraryNameSpaceBuilder,
197+
Set<Uri>? usedParts);
178198

179199
/// Reports that [feature] is not enabled, using [charOffset] and
180200
/// [length] for the location of the message.

pkg/front_end/lib/src/fragment/field.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,22 +209,20 @@ class FieldFragment
209209
.thisInterfaceType(
210210
declarationBuilder.cls, libraryBuilder.library.nonNullable)
211211
: null;
212+
LookupScope scope = declarationBuilder?.scope ?? libraryBuilder.scope;
212213
TypeInferrer typeInferrer =
213214
libraryBuilder.loader.typeInferenceEngine.createTopLevelTypeInferrer(
214215
fileUri,
215216
enclosingClassThisType,
216217
libraryBuilder,
218+
scope,
217219
builder
218220
.dataForTesting
219221
// Coverage-ignore(suite): Not run.
220222
?.inferenceData);
221223
BodyBuilderContext bodyBuilderContext = createBodyBuilderContext();
222224
BodyBuilder bodyBuilder = libraryBuilder.loader.createBodyBuilderForField(
223-
libraryBuilder,
224-
bodyBuilderContext,
225-
declarationBuilder?.scope ?? libraryBuilder.scope,
226-
typeInferrer,
227-
fileUri);
225+
libraryBuilder, bodyBuilderContext, scope, typeInferrer, fileUri);
228226
bodyBuilder.constantContext =
229227
modifiers.isConst ? ConstantContext.inferred : ConstantContext.none;
230228
bodyBuilder.inFieldInitializer = true;

pkg/front_end/lib/src/kernel/body_builder.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -367,21 +367,21 @@ class BodyBuilder extends StackListenerImpl
367367
uri: uri,
368368
typeInferrer: typeInferrer);
369369

370-
BodyBuilder.forOutlineExpression(SourceLibraryBuilder library,
370+
BodyBuilder.forOutlineExpression(SourceLibraryBuilder libraryBuilder,
371371
BodyBuilderContext bodyBuilderContext, LookupScope scope, Uri fileUri,
372372
{LocalScope? formalParameterScope})
373373
: this(
374-
libraryBuilder: library,
374+
libraryBuilder: libraryBuilder,
375375
context: bodyBuilderContext,
376376
enclosingScope: new EnclosingLocalScope(scope),
377377
formalParameterScope: formalParameterScope,
378-
hierarchy: library.loader.hierarchy,
379-
coreTypes: library.loader.coreTypes,
378+
hierarchy: libraryBuilder.loader.hierarchy,
379+
coreTypes: libraryBuilder.loader.coreTypes,
380380
thisVariable: null,
381381
uri: fileUri,
382-
typeInferrer: library.loader.typeInferenceEngine
383-
.createLocalTypeInferrer(
384-
fileUri, bodyBuilderContext.thisType, library, null));
382+
typeInferrer: libraryBuilder.loader.typeInferenceEngine
383+
.createLocalTypeInferrer(fileUri, bodyBuilderContext.thisType,
384+
libraryBuilder, scope, null));
385385

386386
LocalScope get _localScope => _localScopes.current;
387387

pkg/front_end/lib/src/source/diet_listener.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ class DietListener extends StackListenerImpl {
826826
InterfaceType? thisType =
827827
thisVariable == null ? currentDeclaration?.thisType : null;
828828
TypeInferrer typeInferrer = typeInferenceEngine.createLocalTypeInferrer(
829-
uri, thisType, libraryBuilder, inferenceDataForTesting);
829+
uri, thisType, libraryBuilder, memberScope, inferenceDataForTesting);
830830
ConstantContext constantContext = bodyBuilderContext.constantContext;
831831
BodyBuilder result = createListenerInternal(
832832
bodyBuilderContext,

0 commit comments

Comments
 (0)