DeCo. Centralize resolver scope helpers in ScopeContext.

Expand ScopeContext into the single place that manages resolver
name-scope transitions, and migrate visitors to use it directly.

ScopeContext now:
- Owns the current name scope and handles push/pop via withScope().
- Provides focused helpers for common resolver scopes:
  - withLocalScope(), withInstanceScope(), withExtensionScope()
  - withTypeParameterScope() and withTypeParameterList()
  - withFormalParameterScope()
  - withConstructorInitializerScope() and withPrimaryParameterScope()
  - withDocImportScope() for documentation comment resolution
- Renames walkMixinDeclarationScopes() to visitMixinDeclaration() and routes
  mixin traversal through the shared helper.

Update ResolutionVisitor, ScopeResolverVisitor, and ReferenceResolver

to:
- Remove temporary routing getters/methods and ad-hoc try/finally scope
  management.
- Use the new ScopeContext helpers for consistent scoping across declarations,
  function bodies, and DeCo/primary-constructor-related initializer contexts.
- Reduce duplicated scope wiring and keep scope behavior localized for easier
  future evolution.

This is reland of https://dart-review.googlesource.com/c/sdk/+/481981 with support for ScopeResolverVisitor.visitAnonymousMethodInvocation, see PS(s).

Change-Id: Ieee6c732872fc32af86710237972cd7397ca54dd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/482365
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov
2026-02-20 11:18:28 -08:00
committed by Commit Queue
parent c6a9c9536e
commit bdfede5b08
4 changed files with 376 additions and 530 deletions
@@ -120,8 +120,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
nameScope: nameScope,
);
// TODO(scheglov): Remove this temporary routing getter.
Scope get _nameScope => _scopeContext.nameScope;
Scope get nameScope => _scopeContext.nameScope;
/// Set information about enclosing declarations.
void prepareEnclosingDeclarations({
@@ -140,7 +139,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
covariant AssignedVariablePatternImpl node,
) {
var name = node.name.lexeme;
var element = _nameScope.lookup(name).getter;
var element = nameScope.lookup(name).getter;
node.element = element;
if (element == null) {
@@ -173,7 +172,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
@override
void visitBlock(Block node) {
withLocalScope(() {
_scopeContext.withLocalScope(() {
var statements = node.statements;
_buildLocalElements(statements);
statements.accept(this);
@@ -185,7 +184,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
var exceptionTypeNode = node.exceptionType;
exceptionTypeNode?.accept(this);
withLocalScope(() {
_scopeContext.withLocalScope(() {
var exceptionNode = node.exceptionParameter;
if (exceptionNode != null) {
var fragment = exceptionNode.declaredFragment!;
@@ -218,7 +217,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
_namedTypeResolver.enclosingClass = element;
node.metadata.accept(this);
_withTypeParameterScope(node.namePart.typeParameters, () {
_scopeContext.withTypeParameterList(node.namePart.typeParameters, () {
node.namePart.accept(this);
var extendsClause = node.extendsClause;
@@ -239,7 +238,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
);
_withEnclosingInstanceElement(element, () {
withInstanceScope(element, () {
_scopeContext.withInstanceScope(element, () {
node.body.accept(this);
});
});
@@ -255,7 +254,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
node.metadata.accept(this);
_withTypeParameterScope(node.typeParameters, () {
_scopeContext.withTypeParameterList(node.typeParameters, () {
node.typeParameters?.accept(this);
_resolveType(declaration: node, clause: null, namedType: node.superclass);
@@ -280,15 +279,18 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
node.parameters.accept(this);
withScope(ConstructorInitializerScope(_nameScope, fragment.element), () {
_scopeContext.withConstructorInitializerScope(fragment.element, () {
node.initializers.accept(this);
});
node.redirectedConstructor?.accept(this);
_withFormalParameterScope(fragment.element.formalParameters, () {
node.body.accept(this);
});
_scopeContext.withFormalParameterScope(
fragment.element.formalParameters,
() {
node.body.accept(this);
},
);
}
@override
@@ -361,7 +363,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
_namedTypeResolver.enclosingClass = element;
node.metadata.accept(this);
_withTypeParameterScope(node.namePart.typeParameters, () {
_scopeContext.withTypeParameterList(node.namePart.typeParameters, () {
node.namePart.accept(this);
_resolveWithClause(declaration: node, clause: node.withClause);
@@ -371,7 +373,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
);
_withEnclosingInstanceElement(element, () {
withInstanceScope(element, () {
_scopeContext.withInstanceScope(element, () {
node.body.accept(this);
});
});
@@ -392,12 +394,12 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
node.metadata.accept(this);
_withTypeParameterScope(node.typeParameters, () {
_scopeContext.withTypeParameterList(node.typeParameters, () {
node.typeParameters?.accept(this);
node.onClause?.accept(this);
_withEnclosingInstanceElement(element, () {
withScope(ExtensionScope(_nameScope, element), () {
_scopeContext.withExtensionScope(element, () {
node.body.accept(this);
});
});
@@ -414,20 +416,23 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
_namedTypeResolver.enclosingClass = element;
node.metadata.accept(this);
_withTypeParameterScope(node.primaryConstructor.typeParameters, () {
node.primaryConstructor.accept(this);
_scopeContext.withTypeParameterList(
node.primaryConstructor.typeParameters,
() {
node.primaryConstructor.accept(this);
_resolveImplementsClause(
declaration: node,
clause: node.implementsClause,
);
_resolveImplementsClause(
declaration: node,
clause: node.implementsClause,
);
_withEnclosingInstanceElement(element, () {
withInstanceScope(element, () {
node.body.accept(this);
_withEnclosingInstanceElement(element, () {
_scopeContext.withInstanceScope(element, () {
node.body.accept(this);
});
});
});
});
},
);
_namedTypeResolver.enclosingClass = null;
}
@@ -436,7 +441,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
void visitFieldFormalParameter(covariant FieldFormalParameterImpl node) {
node.metadata.accept(this);
_withTypeParameterScope(node.typeParameters, () {
_scopeContext.withTypeParameterList(node.typeParameters, () {
node.typeParameters?.accept(this);
node.parameters?.accept(this);
node.type?.accept(this);
@@ -468,14 +473,14 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
@override
void visitForElement(covariant ForElementImpl node) {
withLocalScope(() {
_scopeContext.withLocalScope(() {
super.visitForElement(node);
});
}
@override
void visitForStatement(covariant ForStatementImpl node) {
withLocalScope(() {
_scopeContext.withLocalScope(() {
super.visitForStatement(node);
});
}
@@ -483,9 +488,12 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
@override
void visitFunctionDeclaration(covariant FunctionDeclarationImpl node) {
var fragment = node.declaredFragment!;
_withTypeParameterScope(node.functionExpression.typeParameters, () {
super.visitFunctionDeclaration(node);
});
_scopeContext.withTypeParameterList(
node.functionExpression.typeParameters,
() {
super.visitFunctionDeclaration(node);
},
);
if (node.parent is FunctionDeclarationStatement) {
fragment.element.returnType =
@@ -511,13 +519,16 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
fragment.element.returnType = _typeProvider.dynamicType;
}
_withTypeParameterScope(node.typeParameters, () {
_scopeContext.withTypeParameterList(node.typeParameters, () {
node.typeParameters?.accept(this);
node.parameters?.accept(this);
if (fragment != null) {
_withFormalParameterScope(fragment.element.formalParameters, () {
node.body.accept(this);
});
_scopeContext.withFormalParameterScope(
fragment.element.formalParameters,
() {
node.body.accept(this);
},
);
} else {
node.body.accept(this);
}
@@ -528,7 +539,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
void visitFunctionTypeAlias(covariant FunctionTypeAliasImpl node) {
node.metadata.accept(this);
_withTypeParameterScope(node.typeParameters, () {
_scopeContext.withTypeParameterList(node.typeParameters, () {
node.typeParameters?.accept(this);
node.returnType?.accept(this);
@@ -543,7 +554,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
var fragment = node.declaredFragment;
node.metadata.accept(this);
_withTypeParameterScope(node.typeParameters, () {
_scopeContext.withTypeParameterList(node.typeParameters, () {
node.typeParameters?.accept(this);
node.parameters.accept(this);
node.returnType?.accept(this);
@@ -567,7 +578,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
void visitGenericFunctionType(GenericFunctionType node) {
node as GenericFunctionTypeImpl;
var fragment = node.declaredFragment!;
_withTypeParameterScope(node.typeParameters, () {
_scopeContext.withTypeParameterList(node.typeParameters, () {
node.typeParameters?.accept(this);
node.parameters.accept(this);
node.returnType?.accept(this);
@@ -598,7 +609,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
void visitGenericTypeAlias(covariant GenericTypeAliasImpl node) {
node.metadata.accept(this);
_withTypeParameterScope(node.typeParameters, () {
_scopeContext.withTypeParameterList(node.typeParameters, () {
node.typeParameters?.accept(this);
node.type.accept(this);
});
@@ -636,7 +647,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
covariant InstanceCreationExpressionImpl node,
) {
var newNode = _astRewriter.instanceCreationExpression(
_nameScope,
nameScope,
node,
libraryElement: _libraryElement,
enclosingInstanceElement: _enclosingInstanceElement,
@@ -712,20 +723,23 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
node.metadata.accept(this);
_withTypeParameterScope(node.typeParameters, () {
_scopeContext.withTypeParameterList(node.typeParameters, () {
node.typeParameters?.accept(this);
node.parameters?.accept(this);
node.returnType?.accept(this);
_withFormalParameterScope(fragment.element.formalParameters, () {
node.body.accept(this);
});
_scopeContext.withFormalParameterScope(
fragment.element.formalParameters,
() {
node.body.accept(this);
},
);
});
}
@override
void visitMethodInvocation(covariant MethodInvocationImpl node) {
var newNode = _astRewriter.methodInvocation(_nameScope, node);
var newNode = _astRewriter.methodInvocation(nameScope, node);
if (newNode != node) {
return newNode.accept(this);
}
@@ -738,7 +752,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
var fragment = node.declaredFragment!;
var element = fragment.element;
_scopeContext.walkMixinDeclarationScopes(
_scopeContext.visitMixinDeclaration(
node,
visitor: this,
visitBody: (body) {
@@ -761,7 +775,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
void visitNamedType(covariant NamedTypeImpl node) {
node.typeArguments?.accept(this);
_namedTypeResolver.nameScope = _nameScope;
_namedTypeResolver.nameScope = nameScope;
_namedTypeResolver.resolve(node, dataForTesting: dataForTesting);
if (_namedTypeResolver.rewriteResult != null) {
@@ -803,7 +817,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
@override
void visitPrefixedIdentifier(covariant PrefixedIdentifierImpl node) {
var newNode = _astRewriter.prefixedIdentifier(_nameScope, node);
var newNode = _astRewriter.prefixedIdentifier(nameScope, node);
if (newNode != node) {
return newNode.accept(this);
}
@@ -819,12 +833,12 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
node.visitChildrenWithHooks(
this,
visitInitializers: (initializers) {
withScope(ConstructorInitializerScope(_nameScope, element), () {
_scopeContext.withConstructorInitializerScope(element, () {
initializers.accept(this);
});
},
visitBody: (body) {
withScope(PrimaryParameterScope(_nameScope, element), () {
_scopeContext.withPrimaryParameterScope(element, () {
body.accept(this);
});
},
@@ -844,7 +858,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
@override
void visitPropertyAccess(covariant PropertyAccessImpl node) {
var newNode = _astRewriter.propertyAccess(_nameScope, node);
var newNode = _astRewriter.propertyAccess(nameScope, node);
if (newNode != node) {
return newNode.accept(this);
}
@@ -875,7 +889,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
@override
void visitSimpleIdentifier(covariant SimpleIdentifierImpl node) {
var newNode = _astRewriter.simpleIdentifier(_nameScope, node);
var newNode = _astRewriter.simpleIdentifier(nameScope, node);
if (newNode != node) {
return newNode.accept(this);
}
@@ -887,7 +901,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
void visitSuperFormalParameter(covariant SuperFormalParameterImpl node) {
node.metadata.accept(this);
_withTypeParameterScope(node.typeParameters, () {
_scopeContext.withTypeParameterList(node.typeParameters, () {
node.typeParameters?.accept(this);
node.type?.accept(this);
node.parameters?.accept(this);
@@ -954,7 +968,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
group.variables = _patternVariables.switchStatementSharedCaseScopeFinish(
group,
);
withLocalScope(() {
_scopeContext.withLocalScope(() {
var statements = group.statements;
_buildLocalElements(statements);
statements.accept(this);
@@ -1014,50 +1028,6 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
}
}
// TODO(scheglov): Remove this temporary routing method.
void withInstanceScope(InstanceElementImpl element, void Function() f) {
_scopeContext.withInstanceScope(element, f);
}
// TODO(scheglov): Remove this temporary routing method.
void withLocalScope(void Function() f) {
_scopeContext.withLocalScope(f);
}
// TODO(scheglov): Remove this temporary routing method.
void withScope(Scope scope, void Function() f) {
_scopeContext.withScope(scope, f);
}
// TODO(scheglov): Remove this temporary routing method.
void withTypeParameterScope(
List<TypeParameterElementImpl> elements,
void Function() f,
) {
_scopeContext.withTypeParameterScope(elements, f);
}
/// Ensure that each type parameter from the [typeParameterList] has its
/// fragment set.
///
/// Returns the corresponding elements in declaration order.
List<TypeParameterElement> _bindTypeParameterElements(
TypeParameterListImpl? typeParameterList,
) {
if (typeParameterList == null) return const [];
var elements = <TypeParameterElement>[];
for (var typeParameter in typeParameterList.typeParameters) {
var fragment = typeParameter.declaredFragment;
if (fragment != null) {
elements.add(fragment.element);
}
}
return elements;
}
void _buildLocalElements(List<Statement> statements) {
for (var statement in statements) {
if (statement is FunctionDeclarationStatementImpl) {
@@ -1069,7 +1039,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
}
void _define(Element element) {
if (_nameScope case LocalScope nameScope) {
if (nameScope case LocalScope nameScope) {
nameScope.add(element);
}
}
@@ -1113,7 +1083,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
sharedCaseScopeKey: sharedCaseScopeKey,
);
// Matched variables are available in `whenClause`.
withLocalScope(() {
_scopeContext.withLocalScope(() {
for (var variable in variables.values) {
_define(variable);
}
@@ -1342,28 +1312,6 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
}
}
void _withFormalParameterScope(
List<FormalParameterElement> parameters,
void Function() f,
) {
withScope(FormalParameterScope(_nameScope, parameters), f);
}
void _withTypeParameterScope(
TypeParameterListImpl? typeParameterList,
void Function() f,
) {
var elements = _bindTypeParameterElements(typeParameterList);
withScope(
TypeParameterScope(
_nameScope,
elements,
featureSet: _libraryElement.featureSet,
),
f,
);
}
/// We always build local elements for [VariableDeclarationStatement]s and
/// [FunctionDeclarationStatement]s in blocks, because invalid code might try
/// to use forward references.
@@ -2,19 +2,31 @@
// 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.
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/scope.dart';
import 'package:analyzer/src/dart/ast/ast.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/scope.dart';
import 'package:meta/meta.dart';
class ScopeContext {
final LibraryFragmentImpl libraryFragment;
Scope nameScope;
final LibraryFragmentImpl _libraryFragment;
final DocumentationCommentScope _docImportScope;
Scope _nameScope;
ScopeContext({required this.libraryFragment, required this.nameScope});
ScopeContext({
required LibraryFragmentImpl libraryFragment,
required Scope nameScope,
List<LibraryElement> docImportLibraries = const [],
}) : _libraryFragment = libraryFragment,
_docImportScope = DocumentationCommentScope(
nameScope,
docImportLibraries,
),
_nameScope = nameScope;
void walkMixinDeclarationScopes(
Scope get nameScope => _nameScope;
void visitMixinDeclaration(
MixinDeclarationImpl node, {
required AstVisitor visitor,
void Function(CommentImpl)? visitDocumentationComment,
@@ -43,37 +55,94 @@ class ScopeContext {
});
}
void withInstanceScope(InstanceElementImpl element, void Function() f) {
withScope(InstanceScope(nameScope, element), f);
void withConstructorInitializerScope(
ConstructorElementImpl element,
void Function() operation,
) {
withScope(ConstructorInitializerScope(nameScope, element), operation);
}
/// Run [f] with a new [LocalScope].
void withLocalScope(void Function() f) {
withScope(LocalScope(nameScope), f);
}
@nonVirtual
void withScope(Scope scope, void Function() f) {
var outerScope = nameScope;
void withDocImportScope(CommentImpl node, void Function() operation) {
var docImportInnerScope = _docImportScope.innerScope;
_docImportScope.innerScope = nameScope;
try {
nameScope = scope;
f();
withScope(_docImportScope, () {
node.nameScope = nameScope;
operation();
});
} finally {
nameScope = outerScope;
_docImportScope.innerScope = docImportInnerScope;
}
}
void withExtensionScope(
ExtensionElementImpl element,
void Function() operation,
) {
withScope(ExtensionScope(nameScope, element), operation);
}
void withFormalParameterScope(
List<FormalParameterElementImpl> elements,
void Function() operation,
) {
withScope(FormalParameterScope(nameScope, elements), operation);
}
void withInstanceScope(
InstanceElementImpl element,
void Function() operation,
) {
withScope(InstanceScope(nameScope, element), operation);
}
/// Run [operation] with a new [LocalScope].
void withLocalScope(void Function() operation) {
withScope(LocalScope(nameScope), operation);
}
void withPrimaryParameterScope(
ConstructorElementImpl element,
void Function() operation,
) {
withScope(PrimaryParameterScope(nameScope, element), operation);
}
void withScope(Scope scope, void Function() operation) {
var outerScope = _nameScope;
try {
_nameScope = scope;
operation();
} finally {
_nameScope = outerScope;
}
}
void withTypeParameterList(
TypeParameterListImpl? typeParameterList,
void Function() operation,
) {
if (typeParameterList != null) {
var elements = typeParameterList.typeParameters
.map((node) => node.declaredFragment!.element)
.toList();
withTypeParameterScope(elements, operation);
} else {
operation();
}
}
void withTypeParameterScope(
List<TypeParameterElementImpl> elements,
void Function() f,
void Function() operation,
) {
withScope(
TypeParameterScope(
nameScope,
elements,
featureSet: libraryFragment.library.featureSet,
featureSet: _libraryFragment.library.featureSet,
),
f,
operation,
);
}
}
+183 -339
View File
@@ -5020,18 +5020,12 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
// TODO(paulberry): migrate the responsibility for all scope resolution into
// this visitor.
class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
/// The library fragment in which the AST nodes are being resolved.
final LibraryFragmentImpl _libraryFragment;
/// The diagnostic reporter that will be informed of any diagnostics that are
/// found during resolution.
final DiagnosticReporter diagnosticReporter;
final ScopeContext _scopeContext;
/// The scope of libraries imported by `@docImport`s.
final DocumentationCommentScope _docImportScope;
/// The scope used to resolve unlabeled `break` and `continue` statements.
ImplicitLabelScope _implicitLabelScope = ImplicitLabelScope.ROOT;
@@ -5058,46 +5052,32 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
required LibraryFragmentImpl libraryFragment,
required Scope nameScope,
List<LibraryElement> docImportLibraries = const [],
}) : _libraryFragment = libraryFragment,
_docImportScope = DocumentationCommentScope(
nameScope,
docImportLibraries,
),
_scopeContext = ScopeContext(
}) : _scopeContext = ScopeContext(
libraryFragment: libraryFragment,
nameScope: nameScope,
docImportLibraries: docImportLibraries,
);
/// Return the implicit label scope in which the current node is being
/// resolved.
ImplicitLabelScope get implicitLabelScope => _implicitLabelScope;
// TODO(scheglov): Remove this temporary routing getter.
Scope get nameScope => _scopeContext.nameScope;
// TODO(scheglov): Remove this temporary routing setter.
set nameScope(Scope value) {
_scopeContext.nameScope = value;
}
@override
void visitAnonymousMethodInvocation(AnonymousMethodInvocation node) {
node.target?.accept(this);
var outerScope = nameScope;
try {
_scopeContext.withLocalScope(() {
var parameters = node.parameters;
if (parameters != null) {
nameScope = LocalScope(nameScope);
for (var parameter in parameters.parameters) {
_define(parameter.declaredFragment!.element);
}
}
node.parameters?.accept(this);
node.body.accept(this);
} finally {
nameScope = outerScope;
}
});
}
@override
@@ -5135,18 +5115,14 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
void visitCatchClause(CatchClause node) {
var exception = node.exceptionParameter;
if (exception != null) {
Scope outerScope = nameScope;
try {
nameScope = LocalScope(nameScope);
_scopeContext.withLocalScope(() {
_define(exception.declaredFragment!.element);
var stackTrace = node.stackTraceParameter;
if (stackTrace != null) {
_define(stackTrace.declaredFragment!.element);
}
super.visitCatchClause(node);
} finally {
nameScope = outerScope;
}
});
} else {
super.visitCatchClause(node);
}
@@ -5154,16 +5130,10 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
@override
void visitClassDeclaration(covariant ClassDeclarationImpl node) {
Scope outerScope = nameScope;
try {
var element = node.declaredFragment!.element;
node.metadata.accept(this);
var element = node.declaredFragment!.element;
node.metadata.accept(this);
nameScope = TypeParameterScope(
nameScope,
element.typeParameters,
featureSet: _libraryFragment.library.featureSet,
);
_scopeContext.withTypeParameterScope(element.typeParameters, () {
node.nameScope = nameScope;
node.namePart.typeParameters?.accept(this);
node.extendsClause?.accept(this);
@@ -5171,40 +5141,30 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
node.implementsClause?.accept(this);
node.nativeClause?.accept(this);
nameScope = InstanceScope(nameScope, element);
_visitDocumentationComment(node.documentationComment);
node.namePart
.tryCast<PrimaryConstructorDeclarationImpl>()
?.formalParameters
.accept(this);
node.body.accept(this);
} finally {
nameScope = outerScope;
}
_scopeContext.withInstanceScope(element, () {
_visitDocumentationComment(node.documentationComment);
node.namePart
.tryCast<PrimaryConstructorDeclarationImpl>()
?.formalParameters
.accept(this);
node.body.accept(this);
});
});
}
@override
void visitClassTypeAlias(covariant ClassTypeAliasImpl node) {
node.metadata.accept(this);
Scope outerScope = nameScope;
try {
var element = node.declaredFragment!.element;
nameScope = InstanceScope(
TypeParameterScope(
nameScope,
element.typeParameters,
featureSet: _libraryFragment.library.featureSet,
),
element,
);
_visitDocumentationComment(node.documentationComment);
node.typeParameters?.accept(this);
node.superclass.accept(this);
node.withClause.accept(this);
node.implementsClause?.accept(this);
} finally {
nameScope = outerScope;
}
var element = node.declaredFragment!.element;
_scopeContext.withTypeParameterScope(element.typeParameters, () {
_scopeContext.withInstanceScope(element, () {
_visitDocumentationComment(node.documentationComment);
node.typeParameters?.accept(this);
node.superclass.accept(this);
node.withClause.accept(this);
node.implementsClause?.accept(this);
});
});
}
@override
@@ -5216,29 +5176,22 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
@override
void visitConstructorDeclaration(covariant ConstructorDeclarationImpl node) {
node.body.localVariableInfo = _localVariableInfo;
Scope outerScope = nameScope;
try {
var element = node.declaredFragment!.element;
var element = node.declaredFragment!.element;
node.metadata.accept(this);
node.typeName?.accept(this);
node.parameters.accept(this);
node.metadata.accept(this);
node.typeName?.accept(this);
node.parameters.accept(this);
try {
nameScope = ConstructorInitializerScope(nameScope, element);
node.initializers.accept(this);
_visitDocumentationComment(node.documentationComment);
} finally {
nameScope = outerScope;
}
_scopeContext.withConstructorInitializerScope(element, () {
node.initializers.accept(this);
_visitDocumentationComment(node.documentationComment);
});
node.redirectedConstructor?.accept(this);
node.redirectedConstructor?.accept(this);
nameScope = FormalParameterScope(nameScope, element.formalParameters);
_scopeContext.withFormalParameterScope(element.formalParameters, () {
node.body.accept(this);
} finally {
nameScope = outerScope;
}
});
}
@override
@@ -5275,31 +5228,24 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
@override
void visitEnumDeclaration(covariant EnumDeclarationImpl node) {
Scope outerScope = nameScope;
try {
var element = node.declaredFragment!.element;
node.metadata.accept(this);
var element = node.declaredFragment!.element;
node.metadata.accept(this);
nameScope = TypeParameterScope(
nameScope,
element.typeParameters,
featureSet: _libraryFragment.library.featureSet,
);
_scopeContext.withTypeParameterScope(element.typeParameters, () {
node.nameScope = nameScope;
node.namePart.typeParameters?.accept(this);
node.withClause?.accept(this);
node.implementsClause?.accept(this);
nameScope = InstanceScope(nameScope, element);
_visitDocumentationComment(node.documentationComment);
node.namePart
.tryCast<PrimaryConstructorDeclarationImpl>()
?.formalParameters
.accept(this);
node.body.accept(this);
} finally {
nameScope = outerScope;
}
_scopeContext.withInstanceScope(element, () {
_visitDocumentationComment(node.documentationComment);
node.namePart
.tryCast<PrimaryConstructorDeclarationImpl>()
?.formalParameters
.accept(this);
node.body.accept(this);
});
});
}
@override
@@ -5310,53 +5256,39 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
@override
void visitExtensionDeclaration(covariant ExtensionDeclarationImpl node) {
var outerScope = nameScope;
try {
var element = node.declaredFragment!.element;
node.metadata.accept(this);
var element = node.declaredFragment!.element;
node.metadata.accept(this);
nameScope = TypeParameterScope(
nameScope,
element.typeParameters,
featureSet: _libraryFragment.library.featureSet,
);
_scopeContext.withTypeParameterScope(element.typeParameters, () {
node.nameScope = nameScope;
node.typeParameters?.accept(this);
node.onClause?.accept(this);
nameScope = ExtensionScope(nameScope, element);
_visitDocumentationComment(node.documentationComment);
node.body.accept(this);
} finally {
nameScope = outerScope;
}
_scopeContext.withExtensionScope(element, () {
_visitDocumentationComment(node.documentationComment);
node.body.accept(this);
});
});
}
@override
void visitExtensionTypeDeclaration(
covariant ExtensionTypeDeclarationImpl node,
) {
Scope outerScope = nameScope;
try {
var element = node.declaredFragment!.element;
node.metadata.accept(this);
var element = node.declaredFragment!.element;
node.metadata.accept(this);
nameScope = TypeParameterScope(
nameScope,
element.typeParameters,
featureSet: _libraryFragment.library.featureSet,
);
_scopeContext.withTypeParameterScope(element.typeParameters, () {
node.nameScope = nameScope;
node.primaryConstructor.typeParameters?.accept(this);
node.implementsClause?.accept(this);
nameScope = InstanceScope(nameScope, element);
_visitDocumentationComment(node.documentationComment);
node.primaryConstructor.formalParameters.accept(this);
node.body.accept(this);
} finally {
nameScope = outerScope;
}
_scopeContext.withInstanceScope(element, () {
_visitDocumentationComment(node.documentationComment);
node.primaryConstructor.formalParameters.accept(this);
node.body.accept(this);
});
});
}
@override
@@ -5364,26 +5296,21 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
node.metadata.accept(this);
_visitDocumentationComment(node.documentationComment);
var outerScope = nameScope;
try {
if (!node.isStatic && node.fields.lateKeyword == null) {
var primaryConstructor = node.parent?.parent
.tryCast<Declaration>()
?.declaredFragment!
.element
.tryCast<InterfaceElementImpl>()
?.primaryConstructor;
if (primaryConstructor != null) {
nameScope = ConstructorInitializerScope(
nameScope,
primaryConstructor,
);
}
if (!node.isStatic && node.fields.lateKeyword == null) {
var primaryConstructor = node.parent?.parent
.tryCast<Declaration>()
?.declaredFragment!
.element
.tryCast<InterfaceElementImpl>()
?.primaryConstructor;
if (primaryConstructor != null) {
_scopeContext.withConstructorInitializerScope(primaryConstructor, () {
node.fields.accept(this);
});
return;
}
node.fields.accept(this);
} finally {
nameScope = outerScope;
}
node.fields.accept(this);
}
@override
@@ -5411,108 +5338,78 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
@override
void visitForElement(covariant ForElementImpl node) {
Scope outerNameScope = nameScope;
try {
nameScope = LocalScope(nameScope);
_scopeContext.withLocalScope(() {
node.nameScope = nameScope;
_predeclareForPartsVariables(node.forLoopParts);
node.forLoopParts.accept(this);
node.body.accept(this);
} finally {
nameScope = outerNameScope;
}
}
@override
void visitFormalParameterList(FormalParameterList node) {
super.visitFormalParameterList(node);
// We finished resolving function signature, now include formal parameters
// scope. Note: we must not do this if the parent is a
// FunctionTypedFormalParameter, because in that case we aren't finished
// resolving the full function signature, just a part of it.
var parent = node.parent;
if (parent is FunctionExpression) {
var element = parent.declaredFragment!.element;
nameScope = FormalParameterScope(nameScope, element.formalParameters);
} else if (parent is FunctionTypeAlias) {
var scope = nameScope = LocalScope(nameScope);
scope.addFormalParameters(parent.parameters);
} else if (parent is MethodDeclaration) {
var element = parent.declaredFragment!.element;
nameScope = FormalParameterScope(nameScope, element.formalParameters);
}
});
}
@override
void visitForStatement(covariant ForStatementImpl node) {
Scope outerNameScope = nameScope;
ImplicitLabelScope outerImplicitScope = _implicitLabelScope;
try {
nameScope = LocalScope(nameScope);
_scopeContext.withLocalScope(() {
var outerImplicitScope = _implicitLabelScope;
_implicitLabelScope = _implicitLabelScope.nest(node);
node.nameScope = nameScope;
_predeclareForPartsVariables(node.forLoopParts);
node.forLoopParts.accept(this);
_visitStatementInScope(node.body);
} finally {
nameScope = outerNameScope;
_implicitLabelScope = outerImplicitScope;
}
try {
node.nameScope = nameScope;
_predeclareForPartsVariables(node.forLoopParts);
node.forLoopParts.accept(this);
_visitStatementInScope(node.body);
} finally {
_implicitLabelScope = outerImplicitScope;
}
});
}
@override
void visitFunctionDeclaration(covariant FunctionDeclarationImpl node) {
node.functionExpression.body.localVariableInfo = _localVariableInfo;
var outerClosure = _enclosingClosure;
Scope outerScope = nameScope;
try {
var element = node.declaredFragment!.element;
_enclosingClosure = element.tryCast<LocalFunctionElement>();
_enclosingClosure = element.tryCast<LocalFunctionElementImpl>();
node.metadata.accept(this);
nameScope = TypeParameterScope(
nameScope,
element.typeParameters,
featureSet: _libraryFragment.library.featureSet,
);
node.nameScope = nameScope;
node.returnType?.accept(this);
node.functionExpression.accept(this);
_scopeContext.withTypeParameterScope(element.typeParameters.cast(), () {
node.nameScope = nameScope;
node.returnType?.accept(this);
node.functionExpression.accept(this);
});
} finally {
nameScope = outerScope;
_enclosingClosure = outerClosure;
}
}
@override
void visitFunctionExpression(FunctionExpression node) {
void visitFunctionExpression(covariant FunctionExpressionImpl node) {
var outerClosure = _enclosingClosure;
Scope outerScope = nameScope;
try {
var element = node.declaredFragment!.element;
if (node.parent is! FunctionDeclaration) {
(node.body as FunctionBodyImpl).localVariableInfo = _localVariableInfo;
_enclosingClosure = element as LocalFunctionElement;
node.body.localVariableInfo = _localVariableInfo;
_enclosingClosure = element as LocalFunctionElementImpl;
}
var parent = node.parent;
if (parent is FunctionDeclarationImpl) {
// We have already created a function scope and don't need to do so again.
super.visitFunctionExpression(node);
_visitDocumentationComment(parent.documentationComment);
node.typeParameters?.accept(this);
node.parameters?.accept(this);
_scopeContext.withFormalParameterScope(element.formalParameters, () {
_visitDocumentationComment(parent.documentationComment);
node.body.accept(this);
});
return;
}
nameScope = TypeParameterScope(
nameScope,
element.typeParameters,
featureSet: _libraryFragment.library.featureSet,
);
node.typeParameters?.accept(this);
node.parameters?.accept(this);
_scopeContext.withTypeParameterScope(element.typeParameters.cast(), () {
node.typeParameters?.accept(this);
node.parameters?.accept(this);
nameScope = FormalParameterScope(nameScope, element.formalParameters);
node.body.accept(this);
_scopeContext.withFormalParameterScope(element.formalParameters, () {
node.body.accept(this);
});
});
} finally {
nameScope = outerScope;
_enclosingClosure = outerClosure;
}
}
@@ -5520,23 +5417,18 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
@override
void visitFunctionTypeAlias(covariant FunctionTypeAliasImpl node) {
node.metadata.accept(this);
Scope outerScope = nameScope;
try {
var element = node.declaredFragment!.element;
nameScope = TypeParameterScope(
nameScope,
element.typeParameters,
featureSet: _libraryFragment.library.featureSet,
);
var element = node.declaredFragment!.element;
_scopeContext.withTypeParameterScope(element.typeParameters.cast(), () {
node.returnType?.accept(this);
node.typeParameters?.accept(this);
node.parameters.accept(this);
// Visiting the parameters added them to the scope as a side effect. So it
// is safe to visit the documentation comment now.
_visitDocumentationComment(node.documentationComment);
} finally {
nameScope = outerScope;
}
_scopeContext.withLocalScope(() {
(nameScope as LocalScope).addFormalParameters(node.parameters);
_visitDocumentationComment(node.documentationComment);
});
});
}
@override
@@ -5544,72 +5436,51 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
covariant FunctionTypedFormalParameterImpl node,
) {
node.metadata.accept(this);
Scope outerScope = nameScope;
try {
var element = node.declaredFragment!.element;
nameScope = TypeParameterScope(
nameScope,
element.typeParameters,
featureSet: _libraryFragment.library.featureSet,
);
_visitDocumentationComment(node.documentationComment);
var element = node.declaredFragment!.element;
_scopeContext.withTypeParameterScope(element.typeParameters, () {
node.returnType?.accept(this);
node.typeParameters?.accept(this);
node.parameters.accept(this);
} finally {
nameScope = outerScope;
}
_scopeContext.withFormalParameterScope(element.formalParameters, () {
_visitDocumentationComment(node.documentationComment);
});
});
}
@override
void visitGenericFunctionType(covariant GenericFunctionTypeImpl node) {
var element = node.declaredFragment!.element;
Scope outerScope = nameScope;
try {
nameScope = TypeParameterScope(
nameScope,
element.typeParameters,
featureSet: _libraryFragment.library.featureSet,
);
_scopeContext.withTypeParameterScope(element.typeParameters, () {
node.nameScope = nameScope;
super.visitGenericFunctionType(node);
} finally {
nameScope = outerScope;
}
});
}
@override
void visitGenericTypeAlias(covariant GenericTypeAliasImpl node) {
node.metadata.accept(this);
Scope outerScope = nameScope;
try {
var element = node.declaredFragment!.element;
nameScope = TypeParameterScope(
nameScope,
element.typeParameters,
featureSet: _libraryFragment.library.featureSet,
);
var element = node.declaredFragment!.element;
_scopeContext.withTypeParameterScope(element.typeParameters.cast(), () {
node.nameScope = nameScope;
node.typeParameters?.accept(this);
node.type.accept(this);
if (node.type case GenericFunctionType functionTypeNode) {
if (functionTypeNode.typeParameters case var typeParameterList?) {
nameScope = TypeParameterScope(
nameScope,
typeParameterList.typeParameters
.map((n) => n.declaredFragment!.element)
.toList(),
featureSet: _libraryFragment.library.featureSet,
);
}
var scope = nameScope = LocalScope(nameScope);
scope.addFormalParameters(functionTypeNode.parameters);
if (node.type case GenericFunctionTypeImpl functionTypeNode) {
_scopeContext.withTypeParameterList(
functionTypeNode.typeParameters,
() {
_scopeContext.withLocalScope(() {
(nameScope as LocalScope).addFormalParameters(
functionTypeNode.parameters,
);
_visitDocumentationComment(node.documentationComment);
});
},
);
} else {
_visitDocumentationComment(node.documentationComment);
}
_visitDocumentationComment(node.documentationComment);
} finally {
nameScope = outerScope;
}
});
}
@override
@@ -5676,25 +5547,19 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
void visitMethodDeclaration(covariant MethodDeclarationImpl node) {
node.body.localVariableInfo = _localVariableInfo;
node.metadata.accept(this);
Scope outerScope = nameScope;
try {
var element = node.declaredFragment!.element;
nameScope = TypeParameterScope(
nameScope,
element.typeParameters,
featureSet: _libraryFragment.library.featureSet,
);
var element = node.declaredFragment!.element;
_scopeContext.withTypeParameterScope(element.typeParameters.cast(), () {
node.nameScope = nameScope;
node.returnType?.accept(this);
node.typeParameters?.accept(this);
node.parameters?.accept(this);
// Visiting the parameters added them to the scope as a side effect. So it
// is safe to visit the documentation comment now.
_visitDocumentationComment(node.documentationComment);
node.body.accept(this);
} finally {
nameScope = outerScope;
}
_scopeContext.withFormalParameterScope(element.formalParameters, () {
_visitDocumentationComment(node.documentationComment);
node.body.accept(this);
});
});
}
@override
@@ -5713,7 +5578,7 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
@override
void visitMixinDeclaration(covariant MixinDeclarationImpl node) {
_scopeContext.walkMixinDeclarationScopes(
_scopeContext.visitMixinDeclaration(
node,
visitor: this,
visitDocumentationComment: _visitDocumentationComment,
@@ -5748,25 +5613,27 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
@override
void visitPrimaryConstructorBody(covariant PrimaryConstructorBodyImpl node) {
var outerScope = nameScope;
try {
var fragment = node.declaration?.declaredFragment;
var element = fragment?.element;
var fragment = node.declaration?.declaredFragment;
var element = fragment?.element;
node.metadata.accept(this);
node.metadata.accept(this);
if (element != null) {
nameScope = ConstructorInitializerScope(outerScope, element);
}
if (element != null) {
_scopeContext.withConstructorInitializerScope(element, () {
node.initializers.accept(this);
});
} else {
node.initializers.accept(this);
}
if (element != null) {
nameScope = PrimaryParameterScope(outerScope, element);
}
if (element != null) {
_scopeContext.withPrimaryParameterScope(element, () {
_visitDocumentationComment(node.documentationComment);
node.body.accept(this);
});
} else {
_visitDocumentationComment(node.documentationComment);
node.body.accept(this);
} finally {
nameScope = outerScope;
}
}
@@ -6021,23 +5888,14 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
}
}
/// Visits a documentation comment with a [DocumentationCommentScope] that encloses the
/// current [nameScope].
/// Visits a documentation comment with a [DocumentationCommentScope] that
/// encloses the current [nameScope].
void _visitDocumentationComment(CommentImpl? node) {
if (node == null) return;
Scope outerScope = nameScope;
Scope docImportInnerScope = _docImportScope.innerScope;
try {
_docImportScope.innerScope = nameScope;
nameScope = _docImportScope;
node.nameScope = nameScope;
_scopeContext.withDocImportScope(node, () {
node.accept(this);
} finally {
nameScope = outerScope;
_docImportScope.innerScope = docImportInnerScope;
}
});
}
void _visitIf(IfElementOrStatementImpl node) {
@@ -6072,13 +5930,9 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
// own scope.
visitBlock(node);
} else if (node != null) {
var outerNameScope = nameScope;
try {
nameScope = LocalScope(nameScope);
_scopeContext.withLocalScope(() {
node.accept(this);
} finally {
nameScope = outerNameScope;
}
});
}
}
@@ -6087,33 +5941,23 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
List<Statement> statements,
void Function() f,
) {
var outerScope = nameScope;
try {
var enclosedScope = LocalScope(nameScope);
_scopeContext.withLocalScope(() {
var enclosedScope = nameScope as LocalScope;
for (var statement in BlockScope.elementsInStatements(statements)) {
if (!statement.isWildcardFunction) {
enclosedScope.add(statement);
}
}
nameScope = enclosedScope;
node.nameScope = nameScope;
node.nameScope = enclosedScope;
f();
} finally {
nameScope = outerScope;
}
});
}
/// Run [f] with the new name scope.
void _withNameScope(void Function() f) {
var current = nameScope;
try {
nameScope = LocalScope(current);
f();
} finally {
nameScope = current;
}
_scopeContext.withLocalScope(f);
}
/// Return the [Scope] to use while resolving inside the [node].
@@ -8,7 +8,6 @@ import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/scope.dart';
import 'package:analyzer/src/dart/ast/ast.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/scope.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/dart/element/type_system.dart';
import 'package:analyzer/src/dart/resolver/scope_context.dart';
@@ -86,14 +85,14 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
var fragment = node.declaredFragment!;
var element = fragment.element;
withTypeParameterScope(element.typeParameters, () {
_scopeContext.withTypeParameterScope(element.typeParameters, () {
node.metadata.accept(this);
node.namePart.typeParameters?.accept(this);
node.extendsClause?.accept(this);
node.withClause?.accept(this);
node.implementsClause?.accept(this);
withInstanceScope(element, () {
_scopeContext.withInstanceScope(element, () {
LinkingNodeContext(node, nameScope);
node.namePart
@@ -111,7 +110,7 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
var fragment = node.declaredFragment!;
var element = fragment.element;
withTypeParameterScope(element.typeParameters, () {
_scopeContext.withTypeParameterScope(element.typeParameters, () {
LinkingNodeContext(node, nameScope);
node.metadata.accept(this);
@@ -137,7 +136,7 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
var fragment = node.declaredFragment!;
var element = fragment.element;
withTypeParameterScope(element.typeParameters, () {
_scopeContext.withTypeParameterScope(element.typeParameters, () {
LinkingNodeContext(node, nameScope);
node.metadata.accept(this);
@@ -159,13 +158,13 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
var fragment = node.declaredFragment!;
var element = fragment.element;
withTypeParameterScope(element.typeParameters, () {
_scopeContext.withTypeParameterScope(element.typeParameters, () {
node.metadata.accept(this);
node.namePart.typeParameters?.accept(this);
node.implementsClause?.accept(this);
node.withClause?.accept(this);
withInstanceScope(element, () {
_scopeContext.withInstanceScope(element, () {
LinkingNodeContext(node, nameScope);
node.namePart
@@ -202,12 +201,12 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
var fragment = node.declaredFragment!;
var element = fragment.element;
withTypeParameterScope(element.typeParameters, () {
_scopeContext.withTypeParameterScope(element.typeParameters, () {
node.metadata.accept(this);
node.typeParameters?.accept(this);
node.onClause?.accept(this);
withScope(ExtensionScope(nameScope, element), () {
_scopeContext.withExtensionScope(element, () {
LinkingNodeContext(node, nameScope);
node.body.members.accept(this);
@@ -228,12 +227,12 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
var fragment = node.declaredFragment!;
var element = fragment.element;
withTypeParameterScope(element.typeParameters, () {
_scopeContext.withTypeParameterScope(element.typeParameters, () {
node.metadata.accept(this);
node.primaryConstructor.typeParameters?.accept(this);
node.implementsClause?.accept(this);
withInstanceScope(element, () {
_scopeContext.withInstanceScope(element, () {
LinkingNodeContext(node, nameScope);
node.primaryConstructor.formalParameters.accept(this);
@@ -251,7 +250,14 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
fields.type?.accept(this);
nodesToBuildType.addDeclaration(fields);
Scope variableScope = nameScope;
void bindVariables() {
for (var variable in fields.variables) {
var fragment = variable.declaredFragment!;
var variableNode = linker.elementNodes[fragment]!;
LinkingNodeContext(variableNode, nameScope);
}
}
if (!node.isStatic && fields.lateKeyword == null) {
var primaryConstructor = node.parent?.parent
.tryCast<Declaration>()
@@ -260,18 +266,15 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
.tryCast<InterfaceElementImpl>()
?.primaryConstructor;
if (primaryConstructor != null) {
variableScope = ConstructorInitializerScope(
nameScope,
_scopeContext.withConstructorInitializerScope(
primaryConstructor,
bindVariables,
);
return;
}
}
for (var variable in fields.variables) {
var fragment = variable.declaredFragment!;
var variableNode = linker.elementNodes[fragment]!;
LinkingNodeContext(variableNode, variableScope);
}
bindVariables();
}
@override
@@ -279,7 +282,7 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
var fragment = node.declaredFragment!;
var element = fragment.element;
withTypeParameterScope(element.typeParameters, () {
_scopeContext.withTypeParameterScope(element.typeParameters, () {
node.type?.accept(this);
node.typeParameters?.accept(this);
node.parameters?.accept(this);
@@ -297,7 +300,7 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
var fragment = node.declaredFragment!;
var element = fragment.element;
withTypeParameterScope(element.typeParameters, () {
_scopeContext.withTypeParameterScope(element.typeParameters, () {
LinkingNodeContext(node, nameScope);
node.metadata.accept(this);
@@ -318,7 +321,7 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
var fragment = node.declaredFragment!;
var element = fragment.element;
withTypeParameterScope(element.typeParameters, () {
_scopeContext.withTypeParameterScope(element.typeParameters, () {
node.returnType?.accept(this);
node.typeParameters?.accept(this);
node.parameters.accept(this);
@@ -334,7 +337,7 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
var fragment = node.declaredFragment!;
var element = fragment.element;
withTypeParameterScope(element.typeParameters, () {
_scopeContext.withTypeParameterScope(element.typeParameters, () {
node.returnType?.accept(this);
node.typeParameters?.accept(this);
node.parameters.accept(this);
@@ -347,7 +350,7 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
var fragment = node.declaredFragment!;
var element = fragment.element;
withTypeParameterScope(element.typeParameters, () {
_scopeContext.withTypeParameterScope(element.typeParameters, () {
node.returnType?.accept(this);
node.typeParameters?.accept(this);
node.parameters.accept(this);
@@ -365,7 +368,7 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
var fragment = node.declaredFragment!;
var element = fragment.element;
withTypeParameterScope(element.typeParameters, () {
_scopeContext.withTypeParameterScope(element.typeParameters, () {
node.metadata.accept(this);
node.typeParameters?.accept(this);
node.type.accept(this);
@@ -390,7 +393,7 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
var fragment = node.declaredFragment!;
var element = fragment.element;
withTypeParameterScope(element.typeParameters, () {
_scopeContext.withTypeParameterScope(element.typeParameters, () {
LinkingNodeContext(node, nameScope);
node.metadata.accept(this);
@@ -404,7 +407,7 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
@override
void visitMixinDeclaration(covariant MixinDeclarationImpl node) {
nodesToBuildType.addDeclaration(node);
_scopeContext.walkMixinDeclarationScopes(
_scopeContext.visitMixinDeclaration(
node,
visitor: this,
enterBodyScope: () {
@@ -528,7 +531,7 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
var fragment = node.declaredFragment!;
var element = fragment.element;
withTypeParameterScope(element.typeParameters, () {
_scopeContext.withTypeParameterScope(element.typeParameters, () {
node.type?.accept(this);
node.typeParameters?.accept(this);
node.parameters?.accept(this);
@@ -584,24 +587,6 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
node.mixinTypes.accept(this);
}
// TODO(scheglov): Remove this temporary routing method.
void withInstanceScope(InstanceElementImpl element, void Function() f) {
_scopeContext.withInstanceScope(element, f);
}
// TODO(scheglov): Remove this temporary routing method.
void withScope(Scope scope, void Function() f) {
_scopeContext.withScope(scope, f);
}
// TODO(scheglov): Remove this temporary routing method.
void withTypeParameterScope(
List<TypeParameterElementImpl> elements,
void Function() f,
) {
_scopeContext.withTypeParameterScope(elements, f);
}
NullabilitySuffix _getNullabilitySuffix(bool hasQuestion) {
if (hasQuestion) {
return NullabilitySuffix.question;