Chromium Code Reviews| Index: pkg/analyzer/lib/src/generated/incremental_resolver.dart |
| diff --git a/pkg/analyzer/lib/src/generated/incremental_resolver.dart b/pkg/analyzer/lib/src/generated/incremental_resolver.dart |
| index 6a2ae0f7abdf7e4a3aff5c4442ab23f5d03ff096..480483e028c9a9e54572bac22840bed2e7a54e1a 100644 |
| --- a/pkg/analyzer/lib/src/generated/incremental_resolver.dart |
| +++ b/pkg/analyzer/lib/src/generated/incremental_resolver.dart |
| @@ -21,6 +21,11 @@ import 'source.dart'; |
| */ |
| class DeclarationMatcher extends RecursiveAstVisitor { |
| /** |
| + * The libary containing the AST nodes being visited. |
| + */ |
| + LibraryElement _enclosingLibrary; |
| + |
| + /** |
| * The compilation unit containing the AST nodes being visited. |
| */ |
| CompilationUnitElement _enclosingUnit; |
| @@ -146,33 +151,30 @@ class DeclarationMatcher extends RecursiveAstVisitor { |
| } |
| @override |
| - visitEnumDeclaration(EnumDeclaration node) { |
| + visitEnumConstantDeclaration(EnumConstantDeclaration node) { |
| String name = node.name.name; |
| - ClassElement element = _findElement(_enclosingUnit.enums, name); |
| - _enclosingClass = element; |
| + FieldElement element = _findElement(_enclosingClass.fields, name); |
| _processElement(element); |
| - _assertTrue(element.isEnum); |
| - super.visitEnumDeclaration(node); |
| } |
| @override |
| - visitEnumConstantDeclaration(EnumConstantDeclaration node) { |
| + visitEnumDeclaration(EnumDeclaration node) { |
| String name = node.name.name; |
| - FieldElement element = _findElement(_enclosingClass.fields, name); |
| + ClassElement element = _findElement(_enclosingUnit.enums, name); |
| + _enclosingClass = element; |
| _processElement(element); |
| + _assertTrue(element.isEnum); |
| + super.visitEnumDeclaration(node); |
| } |
| @override |
| visitExportDirective(ExportDirective node) { |
|
Brian Wilkerson
2014/11/21 19:50:41
Export directives also have combinators. Do we nee
scheglov
2014/11/21 20:07:17
Done.
|
| String uri = _getStringValue(node.uri); |
| if (uri != null) { |
| - LibraryElement library = _enclosingUnit.library; |
| - ExportElement exportElement = _findExport( |
| - library.exports, |
| - _enclosingUnit.context.sourceFactory.resolveUri(_enclosingUnit.source, uri)); |
| - _processElement(exportElement); |
| + ExportElement element = |
| + _findUriReferencedElement(_enclosingLibrary.exports, uri); |
| + _processElement(element); |
| } |
| - super.visitExportDirective(node); |
| } |
| @override |
| @@ -245,14 +247,45 @@ class DeclarationMatcher extends RecursiveAstVisitor { |
| visitImportDirective(ImportDirective node) { |
| String uri = _getStringValue(node.uri); |
| if (uri != null) { |
| - LibraryElement library = _enclosingUnit.library; |
| - ImportElement importElement = _findImport( |
| - library.imports, |
| - _enclosingUnit.context.sourceFactory.resolveUri(_enclosingUnit.source, uri), |
| - node.prefix); |
| - _processElement(importElement); |
| + ImportElement element = |
| + _findUriReferencedElement(_enclosingLibrary.imports, uri); |
| + _processElement(element); |
| + // match the prefix |
| + SimpleIdentifier prefixNode = node.prefix; |
| + PrefixElement prefixElement = element.prefix; |
| + if (prefixNode == null) { |
| + _assertNull(prefixElement); |
| + } else { |
| + _assertNotNull(prefixElement); |
| + _assertEquals(prefixNode.name, prefixElement.name); |
| + } |
| + // prepare shown/hidden names in the element |
| + Set<String> showNames = new Set<String>(); |
| + Set<String> hideNames = new Set<String>(); |
| + for (NamespaceCombinator combinator in element.combinators) { |
| + if (combinator is ShowElementCombinator) { |
| + showNames.addAll(combinator.shownNames); |
| + } else if (combinator is HideElementCombinator) { |
| + hideNames.addAll(combinator.hiddenNames); |
| + } |
| + } |
| + // match combinators with the node |
| + for (Combinator combinator in node.combinators) { |
| + if (combinator is ShowCombinator) { |
| + for (SimpleIdentifier nameNode in combinator.shownNames) { |
| + String name = nameNode.name; |
| + _assertTrue(showNames.remove(name)); |
| + } |
| + _assertTrue(showNames.isEmpty); |
| + } else if (combinator is HideCombinator) { |
| + for (SimpleIdentifier nameNode in combinator.hiddenNames) { |
| + String name = nameNode.name; |
| + _assertTrue(hideNames.remove(name)); |
| + } |
| + _assertTrue(hideNames.isEmpty); |
| + } |
| + } |
| } |
| - super.visitImportDirective(node); |
| } |
| @override |
| @@ -288,10 +321,8 @@ class DeclarationMatcher extends RecursiveAstVisitor { |
| visitPartDirective(PartDirective node) { |
| String uri = _getStringValue(node.uri); |
| if (uri != null) { |
| - Source partSource = |
| - _enclosingUnit.context.sourceFactory.resolveUri(_enclosingUnit.source, uri); |
| CompilationUnitElement element = |
| - _findPart(_enclosingUnit.library.parts, partSource); |
| + _findUriReferencedElement(_enclosingLibrary.parts, uri); |
| _processElement(element); |
| } |
| super.visitPartDirective(node); |
| @@ -389,6 +420,12 @@ class DeclarationMatcher extends RecursiveAstVisitor { |
| } |
| } |
| + void _assertNull(Element element) { |
| + if (element != null) { |
| + throw new _DeclarationMismatchException(); |
| + } |
| + } |
| + |
| void _assertSameType(TypeName node, DartType type) { |
| // no return type == dynamic |
| if (node == null) { |
| @@ -431,10 +468,8 @@ class DeclarationMatcher extends RecursiveAstVisitor { |
| } |
| /** |
| - * Given that the comparison is to begin with the given element, capture the enclosing elements |
| - * that might be used while performing the comparison. |
| - * |
| - * @param element the element corresponding to the AST structure to be compared |
| + * Given that the comparison is to begin with the given [element], capture |
| + * the enclosing elements that might be used while performing the comparison. |
| */ |
| void _captureEnclosingElements(Element element) { |
| Element parent = |
| @@ -442,6 +477,7 @@ class DeclarationMatcher extends RecursiveAstVisitor { |
| while (parent != null) { |
| if (parent is CompilationUnitElement) { |
| _enclosingUnit = parent as CompilationUnitElement; |
| + _enclosingLibrary = element.library; |
| } else if (parent is ClassElement) { |
| if (_enclosingClass == null) { |
| _enclosingClass = parent as ClassElement; |
| @@ -472,24 +508,6 @@ class DeclarationMatcher extends RecursiveAstVisitor { |
| } |
| /** |
| - * Return the export element from the given array whose library has the given source, or |
| - * `null` if there is no such export. |
| - * |
| - * @param exports the export elements being searched |
| - * @param source the source of the library associated with the export element to being searched |
| - * for |
| - * @return the export element whose library has the given source |
| - */ |
| - ExportElement _findExport(List<ExportElement> exports, Source source) { |
| - for (ExportElement export in exports) { |
| - if (export.exportedLibrary.source == source) { |
| - return export; |
| - } |
| - } |
| - return null; |
| - } |
| - |
| - /** |
| * Return the element in the given array of elements that was created for the declaration with the |
| * given name. |
| * |
| @@ -569,7 +587,14 @@ class DeclarationMatcher extends RecursiveAstVisitor { |
| } |
| void _gatherElements(Element element) { |
| - element.accept(new _ElementsGatherer(this)); |
| + _ElementsGatherer gatherer = new _ElementsGatherer(this); |
| + element.accept(gatherer); |
| + // TODO(scheglov) push into CompilationUnitElement |
| + if (identical(_enclosingUnit, _enclosingLibrary.definingCompilationUnit)) { |
| + gatherer.addElements(_enclosingLibrary.imports); |
| + gatherer.addElements(_enclosingLibrary.exports); |
| + gatherer.addElements(_enclosingLibrary.parts); |
| + } |
| } |
| /** |
| @@ -593,6 +618,20 @@ class DeclarationMatcher extends RecursiveAstVisitor { |
| } |
| _unmatchedElements.remove(element); |
| } |
| + |
| + /** |
| + * Return the [UriReferencedElement] from [elements] with the given [uri], or |
| + * `null` if there is no such element. |
| + */ |
| + static UriReferencedElement |
| + _findUriReferencedElement(List<UriReferencedElement> elements, String uri) { |
| + for (UriReferencedElement element in elements) { |
| + if (element.uri == uri) { |
| + return element; |
| + } |
| + } |
| + return null; |
| + } |
| } |
| @@ -933,6 +972,14 @@ class _ElementsGatherer extends GeneralizingElementVisitor { |
| _ElementsGatherer(this.matcher); |
| + void addElements(List<Element> elements) { |
| + for (Element element in elements) { |
| + if (!element.isSynthetic) { |
| + _addElement(element); |
| + } |
| + } |
| + } |
| + |
| @override |
| visitElement(Element element) { |
| _addElement(element); |