| 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 Iterable<NamespaceCombinator> getShowCombinators( |
| 445 ImportElement importElement) => |
| 446 importElement.combinators.where((NamespaceCombinator combinator) => |
| 447 combinator is ShowElementCombinator); |
| 448 if (node is PrefixedIdentifier) { |
| 449 String prefix = node.prefix.name; |
| 450 String name = node.identifier.name; |
| 451 List<ImportElement> imports = _definingLibrary.imports; |
| 452 int count = imports.length; |
| 453 for (int i = 0; i < count; i++) { |
| 454 ImportElement importElement = imports[i]; |
| 455 if (importElement.prefix?.name == prefix && !importElement.uriExists) { |
| 456 Iterable<NamespaceCombinator> showCombinators = |
| 457 getShowCombinators(importElement); |
| 458 if (showCombinators.isEmpty) { |
| 459 return true; |
| 460 } |
| 461 for (ShowElementCombinator combinator in showCombinators) { |
| 462 if (combinator.shownNames.contains(name)) { |
| 463 return true; |
| 464 } |
| 465 } |
| 466 } |
| 467 } |
| 468 } else if (node is SimpleIdentifier) { |
| 469 String name = node.name; |
| 470 List<ImportElement> imports = _definingLibrary.imports; |
| 471 int count = imports.length; |
| 472 for (int i = 0; i < count; i++) { |
| 473 ImportElement importElement = imports[i]; |
| 474 if (importElement.prefix == null && !importElement.uriExists) { |
| 475 for (ShowElementCombinator combinator |
| 476 in getShowCombinators(importElement)) { |
| 477 if (combinator.shownNames.contains(name)) { |
| 478 return true; |
| 479 } |
| 480 } |
| 481 } |
| 482 } |
| 483 } |
| 484 return false; |
| 485 } |
| 486 |
| 441 /** | 487 /** |
| 442 * Create all of the namespaces associated with the libraries imported into | 488 * 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 | 489 * this library. The names are not added to this scope, but are stored for |
| 444 * later reference. | 490 * later reference. |
| 445 */ | 491 */ |
| 446 void _createImportedNamespaces() { | 492 void _createImportedNamespaces() { |
| 447 NamespaceBuilder builder = new NamespaceBuilder(); | 493 NamespaceBuilder builder = new NamespaceBuilder(); |
| 448 List<ImportElement> imports = _definingLibrary.imports; | 494 List<ImportElement> imports = _definingLibrary.imports; |
| 449 int count = imports.length; | 495 int count = imports.length; |
| 450 _importedNamespaces = new List<Namespace>(count); | 496 _importedNamespaces = new List<Namespace>(count); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 } | 560 } |
| 515 | 561 |
| 516 @override | 562 @override |
| 517 Element _internalLookupPrefixed(Identifier identifier, String prefix, | 563 Element _internalLookupPrefixed(Identifier identifier, String prefix, |
| 518 String name, LibraryElement referencingLibrary) { | 564 String name, LibraryElement referencingLibrary) { |
| 519 Element foundElement = _localPrefixedLookup(prefix, name); | 565 Element foundElement = _localPrefixedLookup(prefix, name); |
| 520 if (foundElement != null) { | 566 if (foundElement != null) { |
| 521 return foundElement; | 567 return foundElement; |
| 522 } | 568 } |
| 523 for (int i = 0; i < _importedNamespaces.length; i++) { | 569 for (int i = 0; i < _importedNamespaces.length; i++) { |
| 524 Namespace nameSpace = _importedNamespaces[i]; | 570 Element element = _importedNamespaces[i].getPrefixed(prefix, name); |
| 525 Element element = nameSpace.getPrefixed(prefix, name); | |
| 526 if (element != null) { | 571 if (element != null) { |
| 527 if (foundElement == null) { | 572 if (foundElement == null) { |
| 528 foundElement = element; | 573 foundElement = element; |
| 529 } else if (!identical(foundElement, element)) { | 574 } else if (!identical(foundElement, element)) { |
| 530 foundElement = MultiplyDefinedElementImpl.fromElements( | 575 foundElement = MultiplyDefinedElementImpl.fromElements( |
| 531 _definingLibrary.context, foundElement, element); | 576 _definingLibrary.context, foundElement, element); |
| 532 } | 577 } |
| 533 } | 578 } |
| 534 } | 579 } |
| 535 Element element = foundElement; | 580 Element element = foundElement; |
| (...skipping 618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1154 */ | 1199 */ |
| 1155 Element lookup(Identifier identifier, LibraryElement referencingLibrary) { | 1200 Element lookup(Identifier identifier, LibraryElement referencingLibrary) { |
| 1156 if (identifier is PrefixedIdentifier) { | 1201 if (identifier is PrefixedIdentifier) { |
| 1157 return _internalLookupPrefixed(identifier, identifier.prefix.name, | 1202 return _internalLookupPrefixed(identifier, identifier.prefix.name, |
| 1158 identifier.identifier.name, referencingLibrary); | 1203 identifier.identifier.name, referencingLibrary); |
| 1159 } | 1204 } |
| 1160 return internalLookup(identifier, identifier.name, referencingLibrary); | 1205 return internalLookup(identifier, identifier.name, referencingLibrary); |
| 1161 } | 1206 } |
| 1162 | 1207 |
| 1163 /** | 1208 /** |
| 1209 * Return `true` if the fact that the given [node] is not defined should be |
| 1210 * ignored (from the perspective of error reporting). This will be the case if |
| 1211 * there is at least one import that defines the node's prefix, and if that |
| 1212 * import either has no show combinators or has a show combinator that |
| 1213 * explicitly lists the node's name. |
| 1214 */ |
| 1215 bool shouldIgnoreUndefined(Identifier node) { |
| 1216 if (enclosingScope != null) { |
| 1217 return enclosingScope.shouldIgnoreUndefined(node); |
| 1218 } |
| 1219 return false; |
| 1220 } |
| 1221 |
| 1222 /** |
| 1164 * Return the name that will be used to look up the given [element]. | 1223 * Return the name that will be used to look up the given [element]. |
| 1165 */ | 1224 */ |
| 1166 String _getName(Element element) { | 1225 String _getName(Element element) { |
| 1167 if (element is MethodElement) { | 1226 if (element is MethodElement) { |
| 1168 MethodElement method = element; | 1227 MethodElement method = element; |
| 1169 if (method.name == "-" && method.parameters.length == 0) { | 1228 if (method.name == "-" && method.parameters.length == 0) { |
| 1170 return UNARY_MINUS; | 1229 return UNARY_MINUS; |
| 1171 } | 1230 } |
| 1172 } | 1231 } |
| 1173 return element.name; | 1232 return element.name; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1209 | 1268 |
| 1210 /** | 1269 /** |
| 1211 * Define the type parameters declared by the [classElement]. | 1270 * Define the type parameters declared by the [classElement]. |
| 1212 */ | 1271 */ |
| 1213 void _defineTypeParameters(ClassElement classElement) { | 1272 void _defineTypeParameters(ClassElement classElement) { |
| 1214 for (TypeParameterElement typeParameter in classElement.typeParameters) { | 1273 for (TypeParameterElement typeParameter in classElement.typeParameters) { |
| 1215 define(typeParameter); | 1274 define(typeParameter); |
| 1216 } | 1275 } |
| 1217 } | 1276 } |
| 1218 } | 1277 } |
| OLD | NEW |