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'; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 return element; | 118 return element; |
| 119 } | 119 } |
| 120 // May be there is a hidden Element. | 120 // May be there is a hidden Element. |
| 121 if (_hiddenElements != null) { | 121 if (_hiddenElements != null) { |
| 122 Element hiddenElement = _hiddenElements[name]; | 122 Element hiddenElement = _hiddenElements[name]; |
| 123 if (hiddenElement != null) { | 123 if (hiddenElement != null) { |
| 124 errorListener.onError(new AnalysisError( | 124 errorListener.onError(new AnalysisError( |
| 125 getSource(identifier), | 125 getSource(identifier), |
| 126 identifier.offset, | 126 identifier.offset, |
| 127 identifier.length, | 127 identifier.length, |
| 128 CompileTimeErrorCode.REFERENCED_BEFORE_DECLARATION, [name])); | 128 CompileTimeErrorCode.REFERENCED_BEFORE_DECLARATION, |
| 129 [name])); | |
| 129 return hiddenElement; | 130 return hiddenElement; |
| 130 } | 131 } |
| 131 } | 132 } |
| 132 // Check enclosing scope. | 133 // Check enclosing scope. |
| 133 return enclosingScope.internalLookup(identifier, name, referencingLibrary); | 134 return enclosingScope.internalLookup(identifier, name, referencingLibrary); |
| 134 } | 135 } |
| 135 | 136 |
| 136 @override | 137 @override |
| 137 Element _internalLookupPrefixed(Identifier identifier, String prefix, | 138 Element _internalLookupPrefixed(Identifier identifier, String prefix, |
| 138 String name, LibraryElement referencingLibrary) { | 139 String name, LibraryElement referencingLibrary) { |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 431 StringUtilities.printListOfQuotedNames(libraryNames) | 432 StringUtilities.printListOfQuotedNames(libraryNames) |
| 432 ])); | 433 ])); |
| 433 return foundElement; | 434 return foundElement; |
| 434 } | 435 } |
| 435 if (foundElement != null) { | 436 if (foundElement != null) { |
| 436 defineNameWithoutChecking(name, foundElement); | 437 defineNameWithoutChecking(name, foundElement); |
| 437 } | 438 } |
| 438 return foundElement; | 439 return foundElement; |
| 439 } | 440 } |
| 440 | 441 |
| 442 @override | |
| 443 bool shouldIgnoreUndefined(Identifier node) { | |
| 444 bool exists(ImportElement importElement) => | |
| 445 importElement.context.exists(importElement.importedLibrary.source); | |
|
scheglov
2016/08/08 21:10:20
AnalysisContext.exists() is an expensive operation
Brian Wilkerson
2016/08/08 21:19:29
Yeah, I considered that possibility, but forgot to
| |
| 446 Iterable<NamespaceCombinator> getShowCombinators( | |
| 447 ImportElement importElement) => | |
| 448 importElement.combinators.where((NamespaceCombinator combinator) => | |
| 449 combinator is ShowElementCombinator); | |
| 450 if (node is PrefixedIdentifier) { | |
| 451 String prefix = node.prefix.name; | |
| 452 String name = node.identifier.name; | |
| 453 List<ImportElement> imports = _definingLibrary.imports; | |
| 454 int count = imports.length; | |
| 455 for (int i = 0; i < count; i++) { | |
| 456 ImportElement importElement = imports[i]; | |
| 457 if (importElement.prefix?.name == prefix && !exists(importElement)) { | |
|
scheglov
2016/08/08 21:10:20
What if there is an import with the same prefix na
Brian Wilkerson
2016/08/08 21:19:29
Then presumably we would have found the correspond
| |
| 458 Iterable<NamespaceCombinator> showCombinators = | |
| 459 getShowCombinators(importElement); | |
| 460 if (showCombinators.isEmpty) { | |
| 461 return true; | |
| 462 } | |
| 463 for (ShowElementCombinator combinator in showCombinators) { | |
| 464 if (combinator.shownNames.contains(name)) { | |
| 465 return true; | |
| 466 } | |
| 467 } | |
| 468 } | |
| 469 } | |
| 470 } else if (node is SimpleIdentifier) { | |
| 471 String name = node.name; | |
| 472 List<ImportElement> imports = _definingLibrary.imports; | |
| 473 int count = imports.length; | |
| 474 for (int i = 0; i < count; i++) { | |
| 475 ImportElement importElement = imports[i]; | |
| 476 if (importElement.prefix == null && !exists(importElement)) { | |
| 477 for (ShowElementCombinator combinator | |
| 478 in getShowCombinators(importElement)) { | |
| 479 if (combinator.shownNames.contains(name)) { | |
| 480 return true; | |
| 481 } | |
| 482 } | |
| 483 } | |
| 484 } | |
| 485 } | |
| 486 return false; | |
| 487 } | |
| 488 | |
| 441 /** | 489 /** |
| 442 * Create all of the namespaces associated with the libraries imported into | 490 * Create all of the namespaces associated with the libraries imported into |
| 443 * this library. The names are not added to this scope, but are stored for | 491 * this library. The names are not added to this scope, but are stored for |
| 444 * later reference. | 492 * later reference. |
| 445 */ | 493 */ |
| 446 void _createImportedNamespaces() { | 494 void _createImportedNamespaces() { |
| 447 NamespaceBuilder builder = new NamespaceBuilder(); | 495 NamespaceBuilder builder = new NamespaceBuilder(); |
| 448 List<ImportElement> imports = _definingLibrary.imports; | 496 List<ImportElement> imports = _definingLibrary.imports; |
| 449 int count = imports.length; | 497 int count = imports.length; |
| 450 _importedNamespaces = new List<Namespace>(count); | 498 _importedNamespaces = new List<Namespace>(count); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 514 } | 562 } |
| 515 | 563 |
| 516 @override | 564 @override |
| 517 Element _internalLookupPrefixed(Identifier identifier, String prefix, | 565 Element _internalLookupPrefixed(Identifier identifier, String prefix, |
| 518 String name, LibraryElement referencingLibrary) { | 566 String name, LibraryElement referencingLibrary) { |
| 519 Element foundElement = _localPrefixedLookup(prefix, name); | 567 Element foundElement = _localPrefixedLookup(prefix, name); |
| 520 if (foundElement != null) { | 568 if (foundElement != null) { |
| 521 return foundElement; | 569 return foundElement; |
| 522 } | 570 } |
| 523 for (int i = 0; i < _importedNamespaces.length; i++) { | 571 for (int i = 0; i < _importedNamespaces.length; i++) { |
| 524 Namespace nameSpace = _importedNamespaces[i]; | 572 Element element = _importedNamespaces[i].getPrefixed(prefix, name); |
| 525 Element element = nameSpace.getPrefixed(prefix, name); | |
| 526 if (element != null) { | 573 if (element != null) { |
| 527 if (foundElement == null) { | 574 if (foundElement == null) { |
| 528 foundElement = element; | 575 foundElement = element; |
| 529 } else if (!identical(foundElement, element)) { | 576 } else if (!identical(foundElement, element)) { |
| 530 foundElement = MultiplyDefinedElementImpl.fromElements( | 577 foundElement = MultiplyDefinedElementImpl.fromElements( |
| 531 _definingLibrary.context, foundElement, element); | 578 _definingLibrary.context, foundElement, element); |
| 532 } | 579 } |
| 533 } | 580 } |
| 534 } | 581 } |
| 535 Element element = foundElement; | 582 Element element = foundElement; |
| (...skipping 618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1154 */ | 1201 */ |
| 1155 Element lookup(Identifier identifier, LibraryElement referencingLibrary) { | 1202 Element lookup(Identifier identifier, LibraryElement referencingLibrary) { |
| 1156 if (identifier is PrefixedIdentifier) { | 1203 if (identifier is PrefixedIdentifier) { |
| 1157 return _internalLookupPrefixed(identifier, identifier.prefix.name, | 1204 return _internalLookupPrefixed(identifier, identifier.prefix.name, |
| 1158 identifier.identifier.name, referencingLibrary); | 1205 identifier.identifier.name, referencingLibrary); |
| 1159 } | 1206 } |
| 1160 return internalLookup(identifier, identifier.name, referencingLibrary); | 1207 return internalLookup(identifier, identifier.name, referencingLibrary); |
| 1161 } | 1208 } |
| 1162 | 1209 |
| 1163 /** | 1210 /** |
| 1211 * Return `true` if the fact that the given [node] is not defined should be | |
| 1212 * ignored (from the perspective of error reporting). This will be the case if | |
| 1213 * there is at least one import that defines the node's prefix, and if that | |
| 1214 * import either has no show combinators or has a show combinator that | |
| 1215 * explicitly lists the node's name. | |
| 1216 */ | |
| 1217 bool shouldIgnoreUndefined(Identifier node) { | |
| 1218 if (enclosingScope != null) { | |
| 1219 return enclosingScope.shouldIgnoreUndefined(node); | |
| 1220 } | |
| 1221 return false; | |
| 1222 } | |
| 1223 | |
| 1224 /** | |
| 1164 * Return the name that will be used to look up the given [element]. | 1225 * Return the name that will be used to look up the given [element]. |
| 1165 */ | 1226 */ |
| 1166 String _getName(Element element) { | 1227 String _getName(Element element) { |
| 1167 if (element is MethodElement) { | 1228 if (element is MethodElement) { |
| 1168 MethodElement method = element; | 1229 MethodElement method = element; |
| 1169 if (method.name == "-" && method.parameters.length == 0) { | 1230 if (method.name == "-" && method.parameters.length == 0) { |
| 1170 return UNARY_MINUS; | 1231 return UNARY_MINUS; |
| 1171 } | 1232 } |
| 1172 } | 1233 } |
| 1173 return element.name; | 1234 return element.name; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1209 | 1270 |
| 1210 /** | 1271 /** |
| 1211 * Define the type parameters declared by the [classElement]. | 1272 * Define the type parameters declared by the [classElement]. |
| 1212 */ | 1273 */ |
| 1213 void _defineTypeParameters(ClassElement classElement) { | 1274 void _defineTypeParameters(ClassElement classElement) { |
| 1214 for (TypeParameterElement typeParameter in classElement.typeParameters) { | 1275 for (TypeParameterElement typeParameter in classElement.typeParameters) { |
| 1215 define(typeParameter); | 1276 define(typeParameter); |
| 1216 } | 1277 } |
| 1217 } | 1278 } |
| 1218 } | 1279 } |
| OLD | NEW |