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

Side by Side Diff: pkg/analyzer/lib/src/generated/resolver.dart

Issue 1700263002: Add data structures to AST to indicate potentially mutated variables. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Follow Brian's suggestion Created 4 years, 10 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 unified diff | Download patch
« no previous file with comments | « pkg/analyzer/lib/src/dart/ast/ast.dart ('k') | pkg/analyzer/test/src/task/dart_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library analyzer.src.generated.resolver; 5 library analyzer.src.generated.resolver;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 import 'package:analyzer/dart/ast/ast.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/ast/token.dart'; 10 import 'package:analyzer/dart/ast/token.dart';
11 import 'package:analyzer/dart/ast/visitor.dart'; 11 import 'package:analyzer/dart/ast/visitor.dart';
12 import 'package:analyzer/dart/element/element.dart'; 12 import 'package:analyzer/dart/element/element.dart';
13 import 'package:analyzer/dart/element/type.dart'; 13 import 'package:analyzer/dart/element/type.dart';
14 import 'package:analyzer/dart/element/visitor.dart'; 14 import 'package:analyzer/dart/element/visitor.dart';
15 import 'package:analyzer/src/dart/ast/ast.dart';
15 import 'package:analyzer/src/dart/ast/token.dart'; 16 import 'package:analyzer/src/dart/ast/token.dart';
16 import 'package:analyzer/src/dart/ast/utilities.dart'; 17 import 'package:analyzer/src/dart/ast/utilities.dart';
17 import 'package:analyzer/src/dart/element/element.dart'; 18 import 'package:analyzer/src/dart/element/element.dart';
18 import 'package:analyzer/src/dart/element/member.dart'; 19 import 'package:analyzer/src/dart/element/member.dart';
19 import 'package:analyzer/src/dart/element/type.dart'; 20 import 'package:analyzer/src/dart/element/type.dart';
20 import 'package:analyzer/src/dart/element/utilities.dart'; 21 import 'package:analyzer/src/dart/element/utilities.dart';
21 import 'package:analyzer/src/generated/constant.dart'; 22 import 'package:analyzer/src/generated/constant.dart';
22 import 'package:analyzer/src/generated/element_resolver.dart'; 23 import 'package:analyzer/src/generated/element_resolver.dart';
23 import 'package:analyzer/src/generated/engine.dart'; 24 import 'package:analyzer/src/generated/engine.dart';
24 import 'package:analyzer/src/generated/error.dart'; 25 import 'package:analyzer/src/generated/error.dart';
(...skipping 12420 matching lines...) Expand 10 before | Expand all | Expand 10 after
12445 * [SimpleIdentifier]s to local variables and formal parameters. 12446 * [SimpleIdentifier]s to local variables and formal parameters.
12446 */ 12447 */
12447 class VariableResolverVisitor extends ScopedVisitor { 12448 class VariableResolverVisitor extends ScopedVisitor {
12448 /** 12449 /**
12449 * The method or function that we are currently visiting, or `null` if we are not inside a 12450 * The method or function that we are currently visiting, or `null` if we are not inside a
12450 * method or function. 12451 * method or function.
12451 */ 12452 */
12452 ExecutableElement _enclosingFunction; 12453 ExecutableElement _enclosingFunction;
12453 12454
12454 /** 12455 /**
12456 * Information about local variables in the enclosing function or method.
12457 */
12458 LocalVariableInfo _localVariableInfo;
12459
12460 /**
12455 * Initialize a newly created visitor to resolve the nodes in an AST node. 12461 * Initialize a newly created visitor to resolve the nodes in an AST node.
12456 * 12462 *
12457 * [definingLibrary] is the element for the library containing the node being 12463 * [definingLibrary] is the element for the library containing the node being
12458 * visited. 12464 * visited.
12459 * [source] is the source representing the compilation unit containing the 12465 * [source] is the source representing the compilation unit containing the
12460 * node being visited 12466 * node being visited
12461 * [typeProvider] is the object used to access the types from the core 12467 * [typeProvider] is the object used to access the types from the core
12462 * library. 12468 * library.
12463 * [errorListener] is the error listener that will be informed of any errors 12469 * [errorListener] is the error listener that will be informed of any errors
12464 * that are found during resolution. 12470 * that are found during resolution.
12465 * [nameScope] is the scope used to resolve identifiers in the node that will 12471 * [nameScope] is the scope used to resolve identifiers in the node that will
12466 * first be visited. If `null` or unspecified, a new [LibraryScope] will be 12472 * first be visited. If `null` or unspecified, a new [LibraryScope] will be
12467 * created based on [definingLibrary] and [typeProvider]. 12473 * created based on [definingLibrary] and [typeProvider].
12468 */ 12474 */
12469 VariableResolverVisitor(LibraryElement definingLibrary, Source source, 12475 VariableResolverVisitor(LibraryElement definingLibrary, Source source,
12470 TypeProvider typeProvider, AnalysisErrorListener errorListener, 12476 TypeProvider typeProvider, AnalysisErrorListener errorListener,
12471 {Scope nameScope}) 12477 {Scope nameScope})
12472 : super(definingLibrary, source, typeProvider, errorListener, 12478 : super(definingLibrary, source, typeProvider, errorListener,
12473 nameScope: nameScope); 12479 nameScope: nameScope);
12474 12480
12475 @override 12481 @override
12482 Object visitConstructorDeclaration(ConstructorDeclaration node) {
12483 ExecutableElement outerFunction = _enclosingFunction;
12484 LocalVariableInfo outerLocalVariableInfo = _localVariableInfo;
12485 try {
12486 _localVariableInfo ??= new LocalVariableInfo();
12487 (node.body as FunctionBodyImpl).localVariableInfo = _localVariableInfo;
12488 _enclosingFunction = node.element;
12489 return super.visitConstructorDeclaration(node);
12490 } finally {
12491 _localVariableInfo = outerLocalVariableInfo;
12492 _enclosingFunction = outerFunction;
12493 }
12494 }
12495
12496 @override
12476 Object visitExportDirective(ExportDirective node) => null; 12497 Object visitExportDirective(ExportDirective node) => null;
12477 12498
12478 @override 12499 @override
12479 Object visitFunctionDeclaration(FunctionDeclaration node) { 12500 Object visitFunctionDeclaration(FunctionDeclaration node) {
12480 ExecutableElement outerFunction = _enclosingFunction; 12501 ExecutableElement outerFunction = _enclosingFunction;
12502 LocalVariableInfo outerLocalVariableInfo = _localVariableInfo;
12481 try { 12503 try {
12504 _localVariableInfo ??= new LocalVariableInfo();
12505 (node.functionExpression.body as FunctionBodyImpl).localVariableInfo =
12506 _localVariableInfo;
12482 _enclosingFunction = node.element; 12507 _enclosingFunction = node.element;
12483 return super.visitFunctionDeclaration(node); 12508 return super.visitFunctionDeclaration(node);
12484 } finally { 12509 } finally {
12510 _localVariableInfo = outerLocalVariableInfo;
12485 _enclosingFunction = outerFunction; 12511 _enclosingFunction = outerFunction;
12486 } 12512 }
12487 } 12513 }
12488 12514
12489 @override 12515 @override
12490 Object visitFunctionExpression(FunctionExpression node) { 12516 Object visitFunctionExpression(FunctionExpression node) {
12491 if (node.parent is! FunctionDeclaration) { 12517 if (node.parent is! FunctionDeclaration) {
12492 ExecutableElement outerFunction = _enclosingFunction; 12518 ExecutableElement outerFunction = _enclosingFunction;
12519 LocalVariableInfo outerLocalVariableInfo = _localVariableInfo;
12493 try { 12520 try {
12521 _localVariableInfo ??= new LocalVariableInfo();
12522 (node.body as FunctionBodyImpl).localVariableInfo = _localVariableInfo;
12494 _enclosingFunction = node.element; 12523 _enclosingFunction = node.element;
12495 return super.visitFunctionExpression(node); 12524 return super.visitFunctionExpression(node);
12496 } finally { 12525 } finally {
12526 _localVariableInfo = outerLocalVariableInfo;
12497 _enclosingFunction = outerFunction; 12527 _enclosingFunction = outerFunction;
12498 } 12528 }
12499 } else { 12529 } else {
12500 return super.visitFunctionExpression(node); 12530 return super.visitFunctionExpression(node);
12501 } 12531 }
12502 } 12532 }
12503 12533
12504 @override 12534 @override
12505 Object visitImportDirective(ImportDirective node) => null; 12535 Object visitImportDirective(ImportDirective node) => null;
12506 12536
12507 @override 12537 @override
12508 Object visitMethodDeclaration(MethodDeclaration node) { 12538 Object visitMethodDeclaration(MethodDeclaration node) {
12509 ExecutableElement outerFunction = _enclosingFunction; 12539 ExecutableElement outerFunction = _enclosingFunction;
12540 LocalVariableInfo outerLocalVariableInfo = _localVariableInfo;
12510 try { 12541 try {
12542 _localVariableInfo ??= new LocalVariableInfo();
12543 (node.body as FunctionBodyImpl).localVariableInfo = _localVariableInfo;
12511 _enclosingFunction = node.element; 12544 _enclosingFunction = node.element;
12512 return super.visitMethodDeclaration(node); 12545 return super.visitMethodDeclaration(node);
12513 } finally { 12546 } finally {
12547 _localVariableInfo = outerLocalVariableInfo;
12514 _enclosingFunction = outerFunction; 12548 _enclosingFunction = outerFunction;
12515 } 12549 }
12516 } 12550 }
12517 12551
12518 @override 12552 @override
12519 Object visitSimpleIdentifier(SimpleIdentifier node) { 12553 Object visitSimpleIdentifier(SimpleIdentifier node) {
12520 // Ignore if already resolved - declaration or type. 12554 // Ignore if already resolved - declaration or type.
12521 if (node.inDeclarationContext()) { 12555 if (node.inDeclarationContext()) {
12522 return null; 12556 return null;
12523 } 12557 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
12555 return null; 12589 return null;
12556 } 12590 }
12557 // Must be local or parameter. 12591 // Must be local or parameter.
12558 ElementKind kind = element.kind; 12592 ElementKind kind = element.kind;
12559 if (kind == ElementKind.LOCAL_VARIABLE) { 12593 if (kind == ElementKind.LOCAL_VARIABLE) {
12560 node.staticElement = element; 12594 node.staticElement = element;
12561 LocalVariableElementImpl variableImpl = 12595 LocalVariableElementImpl variableImpl =
12562 element as LocalVariableElementImpl; 12596 element as LocalVariableElementImpl;
12563 if (node.inSetterContext()) { 12597 if (node.inSetterContext()) {
12564 variableImpl.markPotentiallyMutatedInScope(); 12598 variableImpl.markPotentiallyMutatedInScope();
12599 _localVariableInfo.potentiallyMutatedInScope.add(element);
12565 if (element.enclosingElement != _enclosingFunction) { 12600 if (element.enclosingElement != _enclosingFunction) {
12566 variableImpl.markPotentiallyMutatedInClosure(); 12601 variableImpl.markPotentiallyMutatedInClosure();
12602 _localVariableInfo.potentiallyMutatedInClosure.add(element);
12567 } 12603 }
12568 } 12604 }
12569 } else if (kind == ElementKind.PARAMETER) { 12605 } else if (kind == ElementKind.PARAMETER) {
12570 node.staticElement = element; 12606 node.staticElement = element;
12571 if (node.inSetterContext()) { 12607 if (node.inSetterContext()) {
12572 ParameterElementImpl parameterImpl = element as ParameterElementImpl; 12608 ParameterElementImpl parameterImpl = element as ParameterElementImpl;
12573 parameterImpl.markPotentiallyMutatedInScope(); 12609 parameterImpl.markPotentiallyMutatedInScope();
12610 _localVariableInfo.potentiallyMutatedInScope.add(element);
12574 // If we are in some closure, check if it is not the same as where 12611 // If we are in some closure, check if it is not the same as where
12575 // variable is declared. 12612 // variable is declared.
12576 if (_enclosingFunction != null && 12613 if (_enclosingFunction != null &&
12577 (element.enclosingElement != _enclosingFunction)) { 12614 (element.enclosingElement != _enclosingFunction)) {
12578 parameterImpl.markPotentiallyMutatedInClosure(); 12615 parameterImpl.markPotentiallyMutatedInClosure();
12616 _localVariableInfo.potentiallyMutatedInClosure.add(element);
12579 } 12617 }
12580 } 12618 }
12581 } 12619 }
12582 return null; 12620 return null;
12583 } 12621 }
12584 12622
12585 @override 12623 @override
12586 Object visitTypeName(TypeName node) { 12624 Object visitTypeName(TypeName node) {
12587 return null; 12625 return null;
12588 } 12626 }
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
12734 nonFields.add(node); 12772 nonFields.add(node);
12735 return null; 12773 return null;
12736 } 12774 }
12737 12775
12738 @override 12776 @override
12739 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); 12777 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this);
12740 12778
12741 @override 12779 @override
12742 Object visitWithClause(WithClause node) => null; 12780 Object visitWithClause(WithClause node) => null;
12743 } 12781 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/dart/ast/ast.dart ('k') | pkg/analyzer/test/src/task/dart_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698