Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1222)

Unified Diff: pkg/analyzer/lib/src/task/dart.dart

Issue 2835703002: Remove ReferencedNames(Builder). (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer/lib/src/task/dart.dart
diff --git a/pkg/analyzer/lib/src/task/dart.dart b/pkg/analyzer/lib/src/task/dart.dart
index 6c999e4501dfe5748e0faf1dd01ed3ebf79c6a31..2de68f1957d9172236a914bee75a231d41491313 100644
--- a/pkg/analyzer/lib/src/task/dart.dart
+++ b/pkg/analyzer/lib/src/task/dart.dart
@@ -676,14 +676,6 @@ final ResultDescriptor<bool> READY_RESOLVED_UNIT =
new ResultDescriptor<bool>('READY_RESOLVED_UNIT', false);
/**
- * The names (resolved and not) referenced by a unit.
- *
- * The result is only available for [Source]s representing a compilation unit.
- */
-final ResultDescriptor<ReferencedNames> REFERENCED_NAMES =
- new ResultDescriptor<ReferencedNames>('REFERENCED_NAMES', null);
-
-/**
* The sources of the Dart files that a library references.
*
* The list is the union of [IMPORTED_LIBRARIES], [EXPORTED_LIBRARIES] and
@@ -3735,7 +3727,6 @@ class ParseDartTask extends SourceBasedAnalysisTask {
LIBRARY_SPECIFIC_UNITS,
PARSE_ERRORS,
PARSED_UNIT,
- REFERENCED_NAMES,
REFERENCED_SOURCES,
SOURCE_KIND,
UNITS,
@@ -3851,11 +3842,6 @@ class ParseDartTask extends SourceBasedAnalysisTask {
sourceKind = SourceKind.PART;
}
//
- // Compute referenced names.
- //
- ReferencedNames referencedNames = new ReferencedNames(_source);
- new ReferencedNamesBuilder(referencedNames).build(unit);
- //
// Compute source lists.
//
List<Source> explicitlyImportedSources =
@@ -3892,7 +3878,6 @@ class ParseDartTask extends SourceBasedAnalysisTask {
outputs[LIBRARY_SPECIFIC_UNITS] = librarySpecificUnits;
outputs[PARSE_ERRORS] = parseErrors;
outputs[PARSED_UNIT] = unit;
- outputs[REFERENCED_NAMES] = referencedNames;
outputs[REFERENCED_SOURCES] = referencedSources.toList();
outputs[SOURCE_KIND] = sourceKind;
outputs[UNITS] = unitSources;
@@ -4266,408 +4251,6 @@ class ReadyResolvedUnitTask extends SourceBasedAnalysisTask {
}
/**
- * Information about a Dart [source] - which names it uses, which names it
- * defines with their externally visible dependencies.
- */
-class ReferencedNames {
- final Source source;
-
- /**
- * The mapping from the name of a class to the set of names of other classes
- * that extend, mix-in, or implement it.
- *
- * If the set of member of a class is changed, these changes might change
- * the list of unimplemented inherited members in the class and classes that
- * extend, mix-in, or implement it. So, we might need to report (or stop
- * reporting) the corresponding warning.
- */
- final Map<String, Set<String>> superToSubs = <String, Set<String>>{};
-
- /**
- * The names of extended classes for which the unnamed constructor is
- * invoked. Because we cannot use the name of the constructor to identify
- * whether the unit is affected, we need to use the class name.
- */
- final Set<String> extendedUsedUnnamedConstructorNames = new Set<String>();
-
- /**
- * The names of instantiated classes.
- *
- * If one of these classes changes its set of members, it might change
- * its list of unimplemented inherited members. So, we might need to report
- * (or stop reporting) the corresponding warning.
- */
- final Set<String> instantiatedNames = new Set<String>();
-
- /**
- * The set of names that are referenced by the library, both inside and
- * outside of method bodies.
- */
- final Set<String> names = new Set<String>();
-
- /**
- * The mapping from the name of a top-level element to the set of names that
- * the element uses in a way that is visible outside of the element, e.g.
- * the return type, or a parameter type.
- */
- final Map<String, Set<String>> userToDependsOn = <String, Set<String>>{};
-
- ReferencedNames(this.source);
-
- void addSubclass(String subName, String superName) {
- superToSubs.putIfAbsent(superName, () => new Set<String>()).add(subName);
- }
-}
-
-/**
- * A builder for creating [ReferencedNames].
- */
-class ReferencedNamesBuilder extends GeneralizingAstVisitor {
- final Set<String> importPrefixNames = new Set<String>();
- final ReferencedNames names;
-
- String enclosingSuperClassName;
- ReferencedNamesScope scope = new ReferencedNamesScope(null);
-
- int localLevel = 0;
- Set<String> dependsOn;
-
- ReferencedNamesBuilder(this.names);
-
- ReferencedNames build(CompilationUnit unit) {
- unit.accept(this);
- return names;
- }
-
- @override
- visitBlock(Block node) {
- ReferencedNamesScope outerScope = scope;
- try {
- scope = new ReferencedNamesScope.forBlock(scope, node);
- super.visitBlock(node);
- } finally {
- scope = outerScope;
- }
- }
-
- @override
- visitClassDeclaration(ClassDeclaration node) {
- ReferencedNamesScope outerScope = scope;
- try {
- scope = new ReferencedNamesScope.forClass(scope, node);
- dependsOn = new Set<String>();
- enclosingSuperClassName =
- _getSimpleName(node.extendsClause?.superclass?.name);
- super.visitClassDeclaration(node);
- String className = node.name.name;
- names.userToDependsOn[className] = dependsOn;
- _addSuperName(className, node.extendsClause?.superclass);
- _addSuperNames(className, node.withClause?.mixinTypes);
- _addSuperNames(className, node.implementsClause?.interfaces);
- } finally {
- enclosingSuperClassName = null;
- dependsOn = null;
- scope = outerScope;
- }
- }
-
- @override
- visitClassTypeAlias(ClassTypeAlias node) {
- ReferencedNamesScope outerScope = scope;
- try {
- scope = new ReferencedNamesScope.forClassTypeAlias(scope, node);
- dependsOn = new Set<String>();
- super.visitClassTypeAlias(node);
- String className = node.name.name;
- names.userToDependsOn[className] = dependsOn;
- _addSuperName(className, node.superclass);
- _addSuperNames(className, node.withClause?.mixinTypes);
- _addSuperNames(className, node.implementsClause?.interfaces);
- } finally {
- dependsOn = null;
- scope = outerScope;
- }
- }
-
- @override
- visitComment(Comment node) {
- try {
- localLevel++;
- super.visitComment(node);
- } finally {
- localLevel--;
- }
- }
-
- @override
- visitConstructorName(ConstructorName node) {
- if (node.parent is! ConstructorDeclaration) {
- super.visitConstructorName(node);
- }
- }
-
- @override
- visitFunctionBody(FunctionBody node) {
- try {
- localLevel++;
- super.visitFunctionBody(node);
- } finally {
- localLevel--;
- }
- }
-
- @override
- visitFunctionDeclaration(FunctionDeclaration node) {
- if (localLevel == 0) {
- ReferencedNamesScope outerScope = scope;
- try {
- scope = new ReferencedNamesScope.forFunction(scope, node);
- dependsOn = new Set<String>();
- super.visitFunctionDeclaration(node);
- names.userToDependsOn[node.name.name] = dependsOn;
- } finally {
- dependsOn = null;
- scope = outerScope;
- }
- } else {
- super.visitFunctionDeclaration(node);
- }
- }
-
- @override
- visitFunctionTypeAlias(FunctionTypeAlias node) {
- if (localLevel == 0) {
- ReferencedNamesScope outerScope = scope;
- try {
- scope = new ReferencedNamesScope.forFunctionTypeAlias(scope, node);
- dependsOn = new Set<String>();
- super.visitFunctionTypeAlias(node);
- names.userToDependsOn[node.name.name] = dependsOn;
- } finally {
- dependsOn = null;
- scope = outerScope;
- }
- } else {
- super.visitFunctionTypeAlias(node);
- }
- }
-
- @override
- visitImportDirective(ImportDirective node) {
- if (node.prefix != null) {
- importPrefixNames.add(node.prefix.name);
- }
- super.visitImportDirective(node);
- }
-
- @override
- visitInstanceCreationExpression(InstanceCreationExpression node) {
- ConstructorName constructorName = node.constructorName;
- Identifier typeName = constructorName.type.name;
- if (typeName is SimpleIdentifier) {
- names.instantiatedNames.add(typeName.name);
- }
- if (typeName is PrefixedIdentifier) {
- String prefixName = typeName.prefix.name;
- if (importPrefixNames.contains(prefixName)) {
- names.instantiatedNames.add(typeName.identifier.name);
- } else {
- names.instantiatedNames.add(prefixName);
- }
- }
- super.visitInstanceCreationExpression(node);
- }
-
- @override
- visitMethodDeclaration(MethodDeclaration node) {
- ReferencedNamesScope outerScope = scope;
- try {
- scope = new ReferencedNamesScope.forMethod(scope, node);
- super.visitMethodDeclaration(node);
- } finally {
- scope = outerScope;
- }
- }
-
- @override
- visitSimpleIdentifier(SimpleIdentifier node) {
- // Ignore all declarations.
- if (node.inDeclarationContext()) {
- return;
- }
- // Ignore class names references from constructors.
- AstNode parent = node.parent;
- if (parent is ConstructorDeclaration && parent.returnType == node) {
- return;
- }
- // Prepare name.
- String name = node.name;
- // Ignore unqualified names shadowed by local elements.
- if (!node.isQualified) {
- if (scope.contains(name)) {
- return;
- }
- if (importPrefixNames.contains(name)) {
- return;
- }
- }
- // Do add the dependency.
- names.names.add(name);
- if (dependsOn != null && localLevel == 0) {
- dependsOn.add(name);
- }
- }
-
- @override
- visitSuperConstructorInvocation(SuperConstructorInvocation node) {
- if (node.constructorName == null && enclosingSuperClassName != null) {
- names.extendedUsedUnnamedConstructorNames.add(enclosingSuperClassName);
- }
- super.visitSuperConstructorInvocation(node);
- }
-
- @override
- visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) {
- VariableDeclarationList variableList = node.variables;
- // Prepare type dependencies.
- Set<String> typeDependencies = new Set<String>();
- dependsOn = typeDependencies;
- variableList.type?.accept(this);
- // Combine individual variable dependencies with the type dependencies.
- for (VariableDeclaration variable in variableList.variables) {
- dependsOn = new Set<String>();
- variable.accept(this);
- dependsOn.addAll(typeDependencies);
- names.userToDependsOn[variable.name.name] = dependsOn;
- }
- dependsOn = null;
- }
-
- void _addSuperName(String className, TypeName type) {
- if (type != null) {
- Identifier typeName = type.name;
- if (typeName is SimpleIdentifier) {
- names.addSubclass(className, typeName.name);
- }
- if (typeName is PrefixedIdentifier) {
- names.addSubclass(className, typeName.identifier.name);
- }
- }
- }
-
- void _addSuperNames(String className, List<TypeName> types) {
- types?.forEach((type) => _addSuperName(className, type));
- }
-
- static String _getSimpleName(Identifier identifier) {
- if (identifier is SimpleIdentifier) {
- return identifier.name;
- }
- if (identifier is PrefixedIdentifier) {
- return identifier.identifier.name;
- }
- return null;
- }
-}
-
-class ReferencedNamesScope {
- final ReferencedNamesScope enclosing;
- Set<String> names;
-
- ReferencedNamesScope(this.enclosing);
-
- factory ReferencedNamesScope.forBlock(
- ReferencedNamesScope enclosing, Block node) {
- ReferencedNamesScope scope = new ReferencedNamesScope(enclosing);
- for (Statement statement in node.statements) {
- if (statement is FunctionDeclarationStatement) {
- scope.add(statement.functionDeclaration.name.name);
- } else if (statement is VariableDeclarationStatement) {
- for (VariableDeclaration variable in statement.variables.variables) {
- scope.add(variable.name.name);
- }
- }
- }
- return scope;
- }
-
- factory ReferencedNamesScope.forClass(
- ReferencedNamesScope enclosing, ClassDeclaration node) {
- ReferencedNamesScope scope = new ReferencedNamesScope(enclosing);
- scope._addTypeParameters(node.typeParameters);
- for (ClassMember member in node.members) {
- if (member is FieldDeclaration) {
- for (VariableDeclaration variable in member.fields.variables) {
- scope.add(variable.name.name);
- }
- } else if (member is MethodDeclaration) {
- scope.add(member.name.name);
- }
- }
- return scope;
- }
-
- factory ReferencedNamesScope.forClassTypeAlias(
- ReferencedNamesScope enclosing, ClassTypeAlias node) {
- ReferencedNamesScope scope = new ReferencedNamesScope(enclosing);
- scope._addTypeParameters(node.typeParameters);
- return scope;
- }
-
- factory ReferencedNamesScope.forFunction(
- ReferencedNamesScope enclosing, FunctionDeclaration node) {
- ReferencedNamesScope scope = new ReferencedNamesScope(enclosing);
- scope._addTypeParameters(node.functionExpression.typeParameters);
- scope._addFormalParameters(node.functionExpression.parameters);
- return scope;
- }
-
- factory ReferencedNamesScope.forFunctionTypeAlias(
- ReferencedNamesScope enclosing, FunctionTypeAlias node) {
- ReferencedNamesScope scope = new ReferencedNamesScope(enclosing);
- scope._addTypeParameters(node.typeParameters);
- return scope;
- }
-
- factory ReferencedNamesScope.forMethod(
- ReferencedNamesScope enclosing, MethodDeclaration node) {
- ReferencedNamesScope scope = new ReferencedNamesScope(enclosing);
- scope._addTypeParameters(node.typeParameters);
- scope._addFormalParameters(node.parameters);
- return scope;
- }
-
- void add(String name) {
- names ??= new Set<String>();
- names.add(name);
- }
-
- bool contains(String name) {
- if (names != null && names.contains(name)) {
- return true;
- }
- if (enclosing != null) {
- return enclosing.contains(name);
- }
- return false;
- }
-
- void _addFormalParameters(FormalParameterList parameterList) {
- if (parameterList != null) {
- parameterList.parameters
- .map((p) => p is NormalFormalParameter ? p.identifier.name : '')
- .forEach(add);
- }
- }
-
- void _addTypeParameters(TypeParameterList typeParameterList) {
- if (typeParameterList != null) {
- typeParameterList.typeParameters.map((p) => p.name.name).forEach(add);
- }
- }
-}
-
-/**
* A task that ensures that the expression AST for a constant is resolved and
* sets the [CONSTANT_EXPRESSION_RESOLVED] result.
*/
« no previous file with comments | « pkg/analyzer/lib/src/generated/incremental_resolver.dart ('k') | pkg/analyzer/test/generated/incremental_resolver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698