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

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

Issue 1659523003: Make a DirectiveElementBuilder class for use by BuildDirectiveElementsTask. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: 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 | « no previous file | pkg/analyzer/lib/src/task/dart.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/visitor.dart'; 10 import 'package:analyzer/dart/ast/visitor.dart';
11 import 'package:analyzer/dart/element/element.dart'; 11 import 'package:analyzer/dart/element/element.dart';
12 import 'package:analyzer/dart/element/type.dart'; 12 import 'package:analyzer/dart/element/type.dart';
13 import 'package:analyzer/dart/element/visitor.dart'; 13 import 'package:analyzer/dart/element/visitor.dart';
14 import 'package:analyzer/src/dart/ast/utilities.dart'; 14 import 'package:analyzer/src/dart/ast/utilities.dart';
15 import 'package:analyzer/src/dart/element/element.dart'; 15 import 'package:analyzer/src/dart/element/element.dart';
16 import 'package:analyzer/src/dart/element/member.dart'; 16 import 'package:analyzer/src/dart/element/member.dart';
17 import 'package:analyzer/src/dart/element/type.dart'; 17 import 'package:analyzer/src/dart/element/type.dart';
18 import 'package:analyzer/src/generated/constant.dart'; 18 import 'package:analyzer/src/generated/constant.dart';
19 import 'package:analyzer/src/generated/element_resolver.dart'; 19 import 'package:analyzer/src/generated/element_resolver.dart';
20 import 'package:analyzer/src/generated/engine.dart'; 20 import 'package:analyzer/src/generated/engine.dart';
21 import 'package:analyzer/src/generated/error.dart'; 21 import 'package:analyzer/src/generated/error.dart';
22 import 'package:analyzer/src/generated/error_verifier.dart'; 22 import 'package:analyzer/src/generated/error_verifier.dart';
23 import 'package:analyzer/src/generated/java_core.dart'; 23 import 'package:analyzer/src/generated/java_core.dart';
24 import 'package:analyzer/src/generated/java_engine.dart'; 24 import 'package:analyzer/src/generated/java_engine.dart';
25 import 'package:analyzer/src/generated/scanner.dart'; 25 import 'package:analyzer/src/generated/scanner.dart';
26 import 'package:analyzer/src/generated/sdk.dart';
26 import 'package:analyzer/src/generated/source.dart'; 27 import 'package:analyzer/src/generated/source.dart';
27 import 'package:analyzer/src/generated/static_type_analyzer.dart'; 28 import 'package:analyzer/src/generated/static_type_analyzer.dart';
28 import 'package:analyzer/src/generated/type_system.dart'; 29 import 'package:analyzer/src/generated/type_system.dart';
29 import 'package:analyzer/src/generated/utilities_dart.dart'; 30 import 'package:analyzer/src/generated/utilities_dart.dart';
30 import 'package:analyzer/src/task/strong/info.dart' 31 import 'package:analyzer/src/task/strong/info.dart'
31 show InferredType, StaticInfo; 32 show InferredType, StaticInfo;
32 33
33 export 'package:analyzer/src/generated/type_system.dart'; 34 export 'package:analyzer/src/generated/type_system.dart';
34 35
35 /** 36 /**
(...skipping 2608 matching lines...) Expand 10 before | Expand all | Expand 10 after
2644 */ 2645 */
2645 String _getStringValue(StringLiteral literal) { 2646 String _getStringValue(StringLiteral literal) {
2646 if (literal is StringInterpolation) { 2647 if (literal is StringInterpolation) {
2647 return null; 2648 return null;
2648 } 2649 }
2649 return literal.stringValue; 2650 return literal.stringValue;
2650 } 2651 }
2651 } 2652 }
2652 2653
2653 /** 2654 /**
2655 * Instances of the class `DirectiveElementBuilder` build elements for top
2656 * level library directives.
2657 */
2658 class DirectiveElementBuilder extends SimpleAstVisitor<Object> {
Brian Wilkerson 2016/02/02 14:58:16 Consider moving this into it's own file, such as l
Paul Berry 2016/02/02 17:50:54 Done.
2659 /**
2660 * The analysis context within which directive elements are being built.
2661 */
2662 final AnalysisContext context;
2663
2664 /**
2665 * The library element for which directive elements are being built.
2666 */
2667 final LibraryElementImpl libraryElement;
2668
2669 /**
2670 * Map from sources imported by this library to their corresponding library
2671 * elements.
2672 */
2673 final Map<Source, LibraryElement> importLibraryMap;
2674
2675 /**
2676 * Map from sources imported by this library to their corresponding source
2677 * kinds.
2678 */
2679 final Map<Source, SourceKind> importSourceKindMap;
2680
2681 /**
2682 * Map from sources exported by this library to their corresponding library
2683 * elements.
2684 */
2685 final Map<Source, LibraryElement> exportLibraryMap;
2686
2687 /**
2688 * Map from sources exported by this library to their corresponding source
2689 * kinds.
2690 */
2691 final Map<Source, SourceKind> exportSourceKindMap;
2692
2693 /**
2694 * The [ImportElement]s created so far.
2695 */
2696 final List<ImportElement> imports = <ImportElement>[];
2697
2698 /**
2699 * The [ExportElement]s created so far.
2700 */
2701 final List<ExportElement> exports = <ExportElement>[];
2702
2703 /**
2704 * The errors found while building directive elements.
2705 */
2706 final List<AnalysisError> errors = <AnalysisError>[];
2707
2708 /**
2709 * Map from prefix names to their corresponding elements.
2710 */
2711 final HashMap<String, PrefixElementImpl> nameToPrefixMap =
2712 new HashMap<String, PrefixElementImpl>();
2713
2714 /**
2715 * Indicates whether an explicit import of `dart:core` has been found.
2716 */
2717 bool explicitlyImportsCore = false;
2718
2719 DirectiveElementBuilder(
2720 this.context,
2721 this.libraryElement,
2722 this.importLibraryMap,
2723 this.importSourceKindMap,
2724 this.exportLibraryMap,
2725 this.exportSourceKindMap);
2726
2727 @override
2728 Object visitCompilationUnit(CompilationUnit node) {
2729 //
2730 // Resolve directives.
2731 //
2732 for (Directive directive in node.directives) {
2733 directive.accept(this);
2734 }
2735 //
2736 // Ensure "dart:core" import.
2737 //
2738 Source librarySource = libraryElement.source;
2739 Source coreLibrarySource = context.sourceFactory.forUri(DartSdk.DART_CORE);
2740 if (!explicitlyImportsCore && coreLibrarySource != librarySource) {
2741 ImportElementImpl importElement = new ImportElementImpl(-1);
2742 importElement.importedLibrary = importLibraryMap[coreLibrarySource];
2743 importElement.synthetic = true;
2744 imports.add(importElement);
2745 }
2746 //
2747 // Populate the library element.
2748 //
2749 libraryElement.imports = imports;
2750 libraryElement.exports = exports;
2751 return null;
2752 }
2753
2754 @override
2755 Object visitExportDirective(ExportDirective node) {
2756 Source exportedSource = node.source;
2757 if (exportedSource != null && context.exists(exportedSource)) {
2758 // The exported source will be null if the URI in the export
2759 // directive was invalid.
2760 LibraryElement exportedLibrary = exportLibraryMap[exportedSource];
2761 if (exportedLibrary != null) {
2762 ExportElementImpl exportElement = new ExportElementImpl(node.offset);
2763 StringLiteral uriLiteral = node.uri;
2764 if (uriLiteral != null) {
2765 exportElement.uriOffset = uriLiteral.offset;
2766 exportElement.uriEnd = uriLiteral.end;
2767 }
2768 exportElement.uri = node.uriContent;
2769 exportElement.combinators = _buildCombinators(node);
2770 exportElement.exportedLibrary = exportedLibrary;
2771 _setDoc(exportElement, node);
2772 node.element = exportElement;
2773 exports.add(exportElement);
2774 if (exportSourceKindMap[exportedSource] != SourceKind.LIBRARY) {
2775 errors.add(new AnalysisError(
2776 exportedSource,
2777 uriLiteral.offset,
2778 uriLiteral.length,
2779 CompileTimeErrorCode.EXPORT_OF_NON_LIBRARY,
2780 [uriLiteral.toSource()]));
2781 }
2782 }
2783 }
2784 return null;
2785 }
2786
2787 @override
2788 Object visitImportDirective(ImportDirective node) {
2789 String uriContent = node.uriContent;
2790 if (DartUriResolver.isDartExtUri(uriContent)) {
2791 libraryElement.hasExtUri = true;
2792 }
2793 Source importedSource = node.source;
2794 if (importedSource != null && context.exists(importedSource)) {
2795 // The imported source will be null if the URI in the import
2796 // directive was invalid.
2797 LibraryElement importedLibrary = importLibraryMap[importedSource];
2798 if (importedLibrary != null) {
2799 if (importedLibrary.isDartCore) {
2800 explicitlyImportsCore = true;
2801 }
2802 ImportElementImpl importElement = new ImportElementImpl(node.offset);
2803 StringLiteral uriLiteral = node.uri;
2804 if (uriLiteral != null) {
2805 importElement.uriOffset = uriLiteral.offset;
2806 importElement.uriEnd = uriLiteral.end;
2807 }
2808 importElement.uri = uriContent;
2809 importElement.deferred = node.deferredKeyword != null;
2810 importElement.combinators = _buildCombinators(node);
2811 importElement.importedLibrary = importedLibrary;
2812 _setDoc(importElement, node);
2813 SimpleIdentifier prefixNode = node.prefix;
2814 if (prefixNode != null) {
2815 importElement.prefixOffset = prefixNode.offset;
2816 String prefixName = prefixNode.name;
2817 PrefixElementImpl prefix = nameToPrefixMap[prefixName];
2818 if (prefix == null) {
2819 prefix = new PrefixElementImpl.forNode(prefixNode);
2820 nameToPrefixMap[prefixName] = prefix;
2821 }
2822 importElement.prefix = prefix;
2823 prefixNode.staticElement = prefix;
2824 }
2825 node.element = importElement;
2826 imports.add(importElement);
2827 if (importSourceKindMap[importedSource] != SourceKind.LIBRARY) {
2828 ErrorCode errorCode = (importElement.isDeferred
2829 ? StaticWarningCode.IMPORT_OF_NON_LIBRARY
2830 : CompileTimeErrorCode.IMPORT_OF_NON_LIBRARY);
2831 errors.add(new AnalysisError(importedSource, uriLiteral.offset,
2832 uriLiteral.length, errorCode, [uriLiteral.toSource()]));
2833 }
2834 }
2835 }
2836 return null;
2837 }
2838
2839 /**
2840 * If the given [node] has a documentation comment, remember its content
2841 * and range into the given [element].
2842 */
2843 void _setDoc(ElementImpl element, AnnotatedNode node) {
2844 Comment comment = node.documentationComment;
2845 if (comment != null && comment.isDocumentation) {
2846 element.documentationComment =
2847 comment.tokens.map((Token t) => t.lexeme).join('\n');
2848 element.setDocRange(comment.offset, comment.length);
2849 }
2850 }
2851
2852 /**
2853 * Build the element model representing the combinators declared by
2854 * the given [directive].
2855 */
2856 static List<NamespaceCombinator> _buildCombinators(
2857 NamespaceDirective directive) {
2858 List<NamespaceCombinator> combinators = <NamespaceCombinator>[];
2859 for (Combinator combinator in directive.combinators) {
2860 if (combinator is ShowCombinator) {
Brian Wilkerson 2016/02/02 14:58:16 Would it make sense to use the visitor pattern her
Paul Berry 2016/02/02 17:50:53 Done.
2861 ShowElementCombinatorImpl show = new ShowElementCombinatorImpl();
2862 show.offset = combinator.offset;
2863 show.end = combinator.end;
2864 show.shownNames = _getIdentifiers(combinator.shownNames);
2865 combinators.add(show);
2866 } else if (combinator is HideCombinator) {
2867 HideElementCombinatorImpl hide = new HideElementCombinatorImpl();
2868 hide.hiddenNames = _getIdentifiers(combinator.hiddenNames);
2869 combinators.add(hide);
2870 }
2871 }
2872 return combinators;
2873 }
2874
2875 /**
2876 * Return the lexical identifiers associated with the given [identifiers].
2877 */
2878 static List<String> _getIdentifiers(NodeList<SimpleIdentifier> identifiers) {
2879 return identifiers.map((identifier) => identifier.name).toList();
2880 }
2881 }
2882
2883 /**
2654 * Instances of the class `ElementBuilder` traverse an AST structure and build t he element 2884 * Instances of the class `ElementBuilder` traverse an AST structure and build t he element
2655 * model representing the AST structure. 2885 * model representing the AST structure.
2656 */ 2886 */
2657 class ElementBuilder extends RecursiveAstVisitor<Object> { 2887 class ElementBuilder extends RecursiveAstVisitor<Object> {
2658 /** 2888 /**
2659 * The element holder associated with the element that is currently being buil t. 2889 * The element holder associated with the element that is currently being buil t.
2660 */ 2890 */
2661 ElementHolder _currentHolder; 2891 ElementHolder _currentHolder;
2662 2892
2663 /** 2893 /**
(...skipping 10921 matching lines...) Expand 10 before | Expand all | Expand 10 after
13585 nonFields.add(node); 13815 nonFields.add(node);
13586 return null; 13816 return null;
13587 } 13817 }
13588 13818
13589 @override 13819 @override
13590 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); 13820 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this);
13591 13821
13592 @override 13822 @override
13593 Object visitWithClause(WithClause node) => null; 13823 Object visitWithClause(WithClause node) => null;
13594 } 13824 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/task/dart.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698