Chromium Code Reviews| OLD | NEW |
|---|---|
| 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.dart.resolver.scope; | 5 library analyzer.src.dart.resolver.scope; |
| 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/element/element.dart'; | 10 import 'package:analyzer/dart/element/element.dart'; |
| 11 import 'package:analyzer/src/dart/element/element.dart'; | 11 import 'package:analyzer/src/dart/element/element.dart'; |
| 12 import 'package:analyzer/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
| 13 import 'package:analyzer/src/generated/error.dart'; | 13 import 'package:analyzer/src/generated/error.dart'; |
| 14 import 'package:analyzer/src/generated/java_core.dart'; | 14 import 'package:analyzer/src/generated/java_core.dart'; |
| 15 import 'package:analyzer/src/generated/java_engine.dart'; | 15 import 'package:analyzer/src/generated/java_engine.dart'; |
| 16 import 'package:analyzer/src/generated/source.dart'; | 16 import 'package:analyzer/src/generated/source.dart'; |
| 17 | 17 |
| 18 /** | 18 /** |
| 19 * The scope defined by a block. | |
| 20 */ | |
| 21 class BlockScope extends EnclosedScope { | |
| 22 /** | |
| 23 * Initialize a newly created scope, enclosed within the [enclosingScope], | |
| 24 * based on the given [classElement]. | |
| 25 */ | |
| 26 BlockScope(Scope enclosingScope, Block block) : super(enclosingScope) { | |
| 27 if (block == null) { | |
| 28 throw new IllegalArgumentException("block cannot be null"); | |
| 29 } | |
| 30 _defineElements(block); | |
| 31 } | |
| 32 | |
| 33 void _defineElements(block) { | |
|
scheglov
2016/09/02 23:46:50
Type for "block"?
Brian Wilkerson
2016/09/03 17:12:37
Done
| |
| 34 for (var element in elementsInBlock(block)) { | |
| 35 define(element); | |
| 36 } | |
|
scheglov
2016/09/02 23:46:50
This could be written as:
elementsInBlock(block).f
Brian Wilkerson
2016/09/03 17:12:37
Leaving it as is.
| |
| 37 } | |
| 38 | |
| 39 /** | |
| 40 * Return the elements that are declared directly in the given [block]. This | |
| 41 * does not include elements declared in nested blocks. | |
| 42 */ | |
| 43 static Iterable<Element> elementsInBlock(Block block) sync* { | |
| 44 NodeList<Statement> statements = block.statements; | |
| 45 int statementCount = statements.length; | |
| 46 for (int i = 0; i < statementCount; i++) { | |
| 47 Statement statement = statements[i]; | |
| 48 if (statement is VariableDeclarationStatement) { | |
| 49 NodeList<VariableDeclaration> variables = statement.variables.variables; | |
| 50 int variableCount = variables.length; | |
| 51 for (int j = 0; j < variableCount; j++) { | |
| 52 yield variables[j].element; | |
| 53 } | |
| 54 } else if (statement is FunctionDeclarationStatement) { | |
| 55 yield statement.functionDeclaration.element; | |
| 56 } | |
| 57 } | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 /** | |
| 19 * The scope defined by a class. | 62 * The scope defined by a class. |
| 20 */ | 63 */ |
| 21 class ClassScope extends EnclosedScope { | 64 class ClassScope extends EnclosedScope { |
| 22 /** | 65 /** |
| 23 * Initialize a newly created scope, enclosed within the [enclosingScope], | 66 * Initialize a newly created scope, enclosed within the [enclosingScope], |
| 24 * based on the given [classElement]. | 67 * based on the given [classElement]. |
| 25 */ | 68 */ |
| 26 ClassScope(Scope enclosingScope, ClassElement classElement) | 69 ClassScope(Scope enclosingScope, ClassElement classElement) |
| 27 : super(enclosingScope) { | 70 : super(enclosingScope) { |
| 28 if (classElement == null) { | 71 if (classElement == null) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 * A scope that is lexically enclosed in another scope. | 117 * A scope that is lexically enclosed in another scope. |
| 75 */ | 118 */ |
| 76 class EnclosedScope extends Scope { | 119 class EnclosedScope extends Scope { |
| 77 /** | 120 /** |
| 78 * The scope in which this scope is lexically enclosed. | 121 * The scope in which this scope is lexically enclosed. |
| 79 */ | 122 */ |
| 80 @override | 123 @override |
| 81 final Scope enclosingScope; | 124 final Scope enclosingScope; |
| 82 | 125 |
| 83 /** | 126 /** |
| 84 * A table mapping names that will be defined in this scope, but right now are | |
| 85 * not initialized. According to the scoping rules these names are hidden, | |
| 86 * even if they were defined in an outer scope. | |
| 87 */ | |
| 88 HashMap<String, Element> _hiddenElements = null; | |
| 89 | |
| 90 /** | |
| 91 * Initialize a newly created scope, enclosed within the [enclosingScope]. | 127 * Initialize a newly created scope, enclosed within the [enclosingScope]. |
| 92 */ | 128 */ |
| 93 EnclosedScope(this.enclosingScope); | 129 EnclosedScope(this.enclosingScope); |
| 94 | 130 |
| 95 @override | 131 @override |
| 96 AnalysisErrorListener get errorListener => enclosingScope.errorListener; | 132 AnalysisErrorListener get errorListener => enclosingScope.errorListener; |
| 97 | 133 |
| 98 /** | |
| 99 * Record that given [element] is declared in this scope, but hasn't been | |
| 100 * initialized yet, so it is error to use. If there is already an element with | |
| 101 * the given name defined in an outer scope, then it will become unavailable. | |
| 102 */ | |
| 103 void hide(Element element) { | |
| 104 if (element != null) { | |
| 105 String name = element.name; | |
| 106 if (name != null && !name.isEmpty) { | |
| 107 _hiddenElements ??= new HashMap<String, Element>(); | |
| 108 _hiddenElements[name] = element; | |
| 109 } | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 @override | 134 @override |
| 114 Element internalLookup( | 135 Element internalLookup( |
| 115 Identifier identifier, String name, LibraryElement referencingLibrary) { | 136 Identifier identifier, String name, LibraryElement referencingLibrary) { |
| 116 Element element = localLookup(name, referencingLibrary); | 137 Element element = localLookup(name, referencingLibrary); |
| 117 if (element != null) { | 138 if (element != null) { |
| 118 return element; | 139 return element; |
| 119 } | 140 } |
| 120 // May be there is a hidden Element. | |
| 121 if (_hiddenElements != null) { | |
| 122 Element hiddenElement = _hiddenElements[name]; | |
| 123 if (hiddenElement != null) { | |
| 124 errorListener.onError(new AnalysisError( | |
| 125 getSource(identifier), | |
| 126 identifier.offset, | |
| 127 identifier.length, | |
| 128 CompileTimeErrorCode.REFERENCED_BEFORE_DECLARATION, | |
| 129 [name])); | |
| 130 return hiddenElement; | |
| 131 } | |
| 132 } | |
| 133 // Check enclosing scope. | 141 // Check enclosing scope. |
| 134 return enclosingScope.internalLookup(identifier, name, referencingLibrary); | 142 return enclosingScope.internalLookup(identifier, name, referencingLibrary); |
| 135 } | 143 } |
| 136 | 144 |
| 137 @override | 145 @override |
| 138 Element _internalLookupPrefixed(Identifier identifier, String prefix, | 146 Element _internalLookupPrefixed(Identifier identifier, String prefix, |
| 139 String name, LibraryElement referencingLibrary) { | 147 String name, LibraryElement referencingLibrary) { |
| 140 return enclosingScope._internalLookupPrefixed( | 148 return enclosingScope._internalLookupPrefixed( |
| 141 identifier, prefix, name, referencingLibrary); | 149 identifier, prefix, name, referencingLibrary); |
| 142 } | 150 } |
| (...skipping 1122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1265 | 1273 |
| 1266 /** | 1274 /** |
| 1267 * Define the type parameters declared by the [classElement]. | 1275 * Define the type parameters declared by the [classElement]. |
| 1268 */ | 1276 */ |
| 1269 void _defineTypeParameters(ClassElement classElement) { | 1277 void _defineTypeParameters(ClassElement classElement) { |
| 1270 for (TypeParameterElement typeParameter in classElement.typeParameters) { | 1278 for (TypeParameterElement typeParameter in classElement.typeParameters) { |
| 1271 define(typeParameter); | 1279 define(typeParameter); |
| 1272 } | 1280 } |
| 1273 } | 1281 } |
| 1274 } | 1282 } |
| OLD | NEW |