OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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.context.context; | 5 library analyzer.src.context.context; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import 'package:analyzer/src/cancelable_future.dart'; | 10 import 'package:analyzer/src/cancelable_future.dart'; |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 | 206 |
207 @override | 207 @override |
208 void set analysisOptions(AnalysisOptions options) { | 208 void set analysisOptions(AnalysisOptions options) { |
209 bool needsRecompute = this._options.analyzeFunctionBodiesPredicate != | 209 bool needsRecompute = this._options.analyzeFunctionBodiesPredicate != |
210 options.analyzeFunctionBodiesPredicate || | 210 options.analyzeFunctionBodiesPredicate || |
211 this._options.generateImplicitErrors != | 211 this._options.generateImplicitErrors != |
212 options.generateImplicitErrors || | 212 options.generateImplicitErrors || |
213 this._options.generateSdkErrors != options.generateSdkErrors || | 213 this._options.generateSdkErrors != options.generateSdkErrors || |
214 this._options.dart2jsHint != options.dart2jsHint || | 214 this._options.dart2jsHint != options.dart2jsHint || |
215 (this._options.hint && !options.hint) || | 215 (this._options.hint && !options.hint) || |
216 (this._options.lint && !options.lint) || | 216 (this._options.lint && !options.lint) || |
217 this._options.preserveComments != options.preserveComments || | 217 this._options.preserveComments != options.preserveComments || |
218 this._options.enableNullAwareOperators != | 218 this._options.enableNullAwareOperators != |
219 options.enableNullAwareOperators || | 219 options.enableNullAwareOperators || |
220 this._options.enableStrictCallChecks != options.enableStrictCallChecks; | 220 this._options.enableStrictCallChecks != options.enableStrictCallChecks; |
221 int cacheSize = options.cacheSize; | 221 int cacheSize = options.cacheSize; |
222 if (this._options.cacheSize != cacheSize) { | 222 if (this._options.cacheSize != cacheSize) { |
223 this._options.cacheSize = cacheSize; | 223 this._options.cacheSize = cacheSize; |
224 } | 224 } |
225 this._options.analyzeFunctionBodiesPredicate = | 225 this._options.analyzeFunctionBodiesPredicate = |
226 options.analyzeFunctionBodiesPredicate; | 226 options.analyzeFunctionBodiesPredicate; |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 } | 637 } |
638 | 638 |
639 @override | 639 @override |
640 List<CompilationUnit> ensureResolvedDartUnits(Source unitSource) { | 640 List<CompilationUnit> ensureResolvedDartUnits(Source unitSource) { |
641 // Check every library. | 641 // Check every library. |
642 List<CompilationUnit> units = <CompilationUnit>[]; | 642 List<CompilationUnit> units = <CompilationUnit>[]; |
643 List<Source> containingLibraries = getLibrariesContaining(unitSource); | 643 List<Source> containingLibraries = getLibrariesContaining(unitSource); |
644 for (Source librarySource in containingLibraries) { | 644 for (Source librarySource in containingLibraries) { |
645 LibrarySpecificUnit target = | 645 LibrarySpecificUnit target = |
646 new LibrarySpecificUnit(librarySource, unitSource); | 646 new LibrarySpecificUnit(librarySource, unitSource); |
647 CompilationUnit unit = _getResult(target, RESOLVED_UNIT); | 647 CompilationUnit unit = _cache.getValue(target, RESOLVED_UNIT); |
648 if (unit == null) { | 648 if (unit == null) { |
649 units = null; | 649 units = null; |
650 break; | 650 break; |
651 } | 651 } |
652 units.add(unit); | 652 units.add(unit); |
653 } | 653 } |
654 // If we have results, then we're done. | 654 // If we have results, then we're done. |
655 if (units != null) { | 655 if (units != null) { |
656 return units; | 656 return units; |
657 } | 657 } |
658 // Schedule recomputing RESOLVED_UNIT results. | 658 // Schedule recomputing RESOLVED_UNIT results. |
659 for (Source librarySource in containingLibraries) { | 659 for (Source librarySource in containingLibraries) { |
660 LibrarySpecificUnit target = | 660 LibrarySpecificUnit target = |
661 new LibrarySpecificUnit(librarySource, unitSource); | 661 new LibrarySpecificUnit(librarySource, unitSource); |
662 if (_getResultState(target, RESOLVED_UNIT) == CacheState.FLUSHED) { | 662 if (_cache.getState(target, RESOLVED_UNIT) == CacheState.FLUSHED) { |
663 dartWorkManager.addPriorityResult(target, RESOLVED_UNIT); | 663 dartWorkManager.addPriorityResult(target, RESOLVED_UNIT); |
664 } | 664 } |
665 } | 665 } |
666 return null; | 666 return null; |
667 } | 667 } |
668 | 668 |
669 @override | 669 @override |
670 bool exists(Source source) { | 670 bool exists(Source source) { |
671 if (source == null) { | 671 if (source == null) { |
672 return false; | 672 return false; |
(...skipping 11 matching lines...) Expand all Loading... |
684 entry = new cache.CacheEntry(target); | 684 entry = new cache.CacheEntry(target); |
685 _cache.put(entry); | 685 _cache.put(entry); |
686 } | 686 } |
687 return entry; | 687 return entry; |
688 } | 688 } |
689 | 689 |
690 @override | 690 @override |
691 CompilationUnitElement getCompilationUnitElement( | 691 CompilationUnitElement getCompilationUnitElement( |
692 Source unitSource, Source librarySource) { | 692 Source unitSource, Source librarySource) { |
693 AnalysisTarget target = new LibrarySpecificUnit(librarySource, unitSource); | 693 AnalysisTarget target = new LibrarySpecificUnit(librarySource, unitSource); |
694 return _getResult(target, COMPILATION_UNIT_ELEMENT); | 694 return _cache.getValue(target, COMPILATION_UNIT_ELEMENT); |
695 } | 695 } |
696 | 696 |
697 @override | 697 @override |
698 TimestampedData<String> getContents(Source source) { | 698 TimestampedData<String> getContents(Source source) { |
699 String contents = _contentCache.getContents(source); | 699 String contents = _contentCache.getContents(source); |
700 if (contents != null) { | 700 if (contents != null) { |
701 return new TimestampedData<String>( | 701 return new TimestampedData<String>( |
702 _contentCache.getModificationStamp(source), contents); | 702 _contentCache.getModificationStamp(source), contents); |
703 } | 703 } |
704 return source.contents; | 704 return source.contents; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
739 } | 739 } |
740 | 740 |
741 @override | 741 @override |
742 AnalysisErrorInfo getErrors(Source source) { | 742 AnalysisErrorInfo getErrors(Source source) { |
743 // TODO(brianwilkerson) Figure out how to implement this cleanly. The | 743 // TODO(brianwilkerson) Figure out how to implement this cleanly. The |
744 // problem is that _getResult doesn't know to go into the individual inputs | 744 // problem is that _getResult doesn't know to go into the individual inputs |
745 // for the task to get their values for tasks that are just merging other | 745 // for the task to get their values for tasks that are just merging other |
746 // result values. Therefore, if some, but not all, of the error lists have | 746 // result values. Therefore, if some, but not all, of the error lists have |
747 // been computed, no errors will be returned by it. | 747 // been computed, no errors will be returned by it. |
748 List<List<AnalysisError>> errorLists = <List<AnalysisError>>[]; | 748 List<List<AnalysisError>> errorLists = <List<AnalysisError>>[]; |
749 errorLists.add(_getResult(source, BUILD_DIRECTIVES_ERRORS)); | 749 errorLists.add(_cache.getValue(source, BUILD_DIRECTIVES_ERRORS)); |
750 errorLists.add(_getResult(source, BUILD_LIBRARY_ERRORS)); | 750 errorLists.add(_cache.getValue(source, BUILD_LIBRARY_ERRORS)); |
751 errorLists.add(_getResult(source, PARSE_ERRORS)); | 751 errorLists.add(_cache.getValue(source, PARSE_ERRORS)); |
752 errorLists.add(_getResult(source, SCAN_ERRORS)); | 752 errorLists.add(_cache.getValue(source, SCAN_ERRORS)); |
753 for (Source library in getLibrariesContaining(source)) { | 753 for (Source library in getLibrariesContaining(source)) { |
754 LibrarySpecificUnit unit = new LibrarySpecificUnit(library, source); | 754 LibrarySpecificUnit unit = new LibrarySpecificUnit(library, source); |
755 errorLists.add(_getResult(unit, BUILD_FUNCTION_TYPE_ALIASES_ERRORS)); | 755 errorLists.add(_cache.getValue(unit, BUILD_FUNCTION_TYPE_ALIASES_ERRORS)); |
756 errorLists.add(_getResult(unit, HINTS)); | 756 errorLists.add(_cache.getValue(unit, HINTS)); |
757 errorLists.add(_getResult(unit, RESOLVE_REFERENCES_ERRORS)); | 757 errorLists.add(_cache.getValue(unit, RESOLVE_REFERENCES_ERRORS)); |
758 errorLists.add(_getResult(unit, RESOLVE_TYPE_NAMES_ERRORS)); | 758 errorLists.add(_cache.getValue(unit, RESOLVE_TYPE_NAMES_ERRORS)); |
759 errorLists.add(_getResult(unit, VERIFY_ERRORS)); | 759 errorLists.add(_cache.getValue(unit, VERIFY_ERRORS)); |
760 } | 760 } |
761 LineInfo lineInfo = _getResult(source, LINE_INFO); | 761 LineInfo lineInfo = _cache.getValue(source, LINE_INFO); |
762 return new AnalysisErrorInfoImpl( | 762 return new AnalysisErrorInfoImpl( |
763 AnalysisError.mergeLists(errorLists), lineInfo); | 763 AnalysisError.mergeLists(errorLists), lineInfo); |
764 } | 764 } |
765 | 765 |
766 @override | 766 @override |
767 HtmlElement getHtmlElement(Source source) { | 767 HtmlElement getHtmlElement(Source source) { |
768 // TODO(brianwilkerson) Implement this. | 768 // TODO(brianwilkerson) Implement this. |
769 // SourceEntry sourceEntry = getReadableSourceEntryOrNull(source); | 769 // SourceEntry sourceEntry = getReadableSourceEntryOrNull(source); |
770 // if (sourceEntry is HtmlEntry) { | 770 // if (sourceEntry is HtmlEntry) { |
771 // return sourceEntry.getValue(HtmlEntry.ELEMENT); | 771 // return sourceEntry.getValue(HtmlEntry.ELEMENT); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
814 if (htmlSources.isEmpty) { | 814 if (htmlSources.isEmpty) { |
815 return Source.EMPTY_LIST; | 815 return Source.EMPTY_LIST; |
816 } | 816 } |
817 return htmlSources; | 817 return htmlSources; |
818 } | 818 } |
819 | 819 |
820 @override | 820 @override |
821 SourceKind getKindOf(Source source) { | 821 SourceKind getKindOf(Source source) { |
822 String name = source.shortName; | 822 String name = source.shortName; |
823 if (AnalysisEngine.isDartFileName(name)) { | 823 if (AnalysisEngine.isDartFileName(name)) { |
824 return _getResult(source, SOURCE_KIND); | 824 return _cache.getValue(source, SOURCE_KIND); |
825 } else if (AnalysisEngine.isHtmlFileName(name)) { | 825 } else if (AnalysisEngine.isHtmlFileName(name)) { |
826 return SourceKind.HTML; | 826 return SourceKind.HTML; |
827 } | 827 } |
828 return SourceKind.UNKNOWN; | 828 return SourceKind.UNKNOWN; |
829 } | 829 } |
830 | 830 |
831 @override | 831 @override |
832 List<Source> getLibrariesContaining(Source source) { | 832 List<Source> getLibrariesContaining(Source source) { |
833 SourceKind kind = getKindOf(source); | 833 SourceKind kind = getKindOf(source); |
834 if (kind == SourceKind.LIBRARY) { | 834 if (kind == SourceKind.LIBRARY) { |
835 return <Source>[source]; | 835 return <Source>[source]; |
836 } else if (kind == SourceKind.PART) { | 836 } else if (kind == SourceKind.PART) { |
837 List<Source> libraries = <Source>[]; | 837 List<Source> libraries = <Source>[]; |
838 MapIterator<AnalysisTarget, cache.CacheEntry> iterator = | 838 MapIterator<AnalysisTarget, cache.CacheEntry> iterator = |
839 _cache.iterator(); | 839 _cache.iterator(); |
840 while (iterator.moveNext()) { | 840 while (iterator.moveNext()) { |
841 AnalysisTarget target = iterator.key; | 841 AnalysisTarget target = iterator.key; |
842 if (target is Source && getKindOf(target) == SourceKind.LIBRARY) { | 842 if (target is Source && getKindOf(target) == SourceKind.LIBRARY) { |
843 List<Source> parts = _getResult(target, INCLUDED_PARTS); | 843 List<Source> parts = _cache.getValue(target, INCLUDED_PARTS); |
844 if (parts.contains(source)) { | 844 if (parts.contains(source)) { |
845 libraries.add(target); | 845 libraries.add(target); |
846 } | 846 } |
847 } | 847 } |
848 } | 848 } |
849 if (libraries.isNotEmpty) { | 849 if (libraries.isNotEmpty) { |
850 return libraries; | 850 return libraries; |
851 } | 851 } |
852 } | 852 } |
853 return Source.EMPTY_ARRAY; | 853 return Source.EMPTY_ARRAY; |
(...skipping 26 matching lines...) Expand all Loading... |
880 // cache.CacheEntry entry = getReadableSourceEntryOrNull(htmlSource); | 880 // cache.CacheEntry entry = getReadableSourceEntryOrNull(htmlSource); |
881 // if (entry is HtmlEntry) { | 881 // if (entry is HtmlEntry) { |
882 // HtmlEntry htmlEntry = entry; | 882 // HtmlEntry htmlEntry = entry; |
883 // return htmlEntry.getValue(HtmlEntry.REFERENCED_LIBRARIES); | 883 // return htmlEntry.getValue(HtmlEntry.REFERENCED_LIBRARIES); |
884 // } | 884 // } |
885 return Source.EMPTY_LIST; | 885 return Source.EMPTY_LIST; |
886 } | 886 } |
887 | 887 |
888 @override | 888 @override |
889 LibraryElement getLibraryElement(Source source) => | 889 LibraryElement getLibraryElement(Source source) => |
890 _getResult(source, LIBRARY_ELEMENT); | 890 _cache.getValue(source, LIBRARY_ELEMENT); |
891 | 891 |
892 @override | 892 @override |
893 LineInfo getLineInfo(Source source) => _getResult(source, LINE_INFO); | 893 LineInfo getLineInfo(Source source) => _cache.getValue(source, LINE_INFO); |
894 | 894 |
895 @override | 895 @override |
896 int getModificationStamp(Source source) { | 896 int getModificationStamp(Source source) { |
897 int stamp = _contentCache.getModificationStamp(source); | 897 int stamp = _contentCache.getModificationStamp(source); |
898 if (stamp != null) { | 898 if (stamp != null) { |
899 return stamp; | 899 return stamp; |
900 } | 900 } |
901 return source.modificationStamp; | 901 return source.modificationStamp; |
902 } | 902 } |
903 | 903 |
(...skipping 27 matching lines...) Expand all Loading... |
931 return getResolvedCompilationUnit2(unitSource, library.source); | 931 return getResolvedCompilationUnit2(unitSource, library.source); |
932 } | 932 } |
933 | 933 |
934 @override | 934 @override |
935 CompilationUnit getResolvedCompilationUnit2( | 935 CompilationUnit getResolvedCompilationUnit2( |
936 Source unitSource, Source librarySource) { | 936 Source unitSource, Source librarySource) { |
937 if (!AnalysisEngine.isDartFileName(unitSource.shortName) || | 937 if (!AnalysisEngine.isDartFileName(unitSource.shortName) || |
938 !AnalysisEngine.isDartFileName(librarySource.shortName)) { | 938 !AnalysisEngine.isDartFileName(librarySource.shortName)) { |
939 return null; | 939 return null; |
940 } | 940 } |
941 return _getResult( | 941 return _cache.getValue( |
942 new LibrarySpecificUnit(librarySource, unitSource), RESOLVED_UNIT); | 942 new LibrarySpecificUnit(librarySource, unitSource), RESOLVED_UNIT); |
943 } | 943 } |
944 | 944 |
945 @override | 945 @override |
946 ht.HtmlUnit getResolvedHtmlUnit(Source htmlSource) { | 946 ht.HtmlUnit getResolvedHtmlUnit(Source htmlSource) { |
947 // TODO(brianwilkerson) Implement this. | 947 // TODO(brianwilkerson) Implement this. |
948 // SourceEntry sourceEntry = getReadableSourceEntryOrNull(htmlSource); | 948 // SourceEntry sourceEntry = getReadableSourceEntryOrNull(htmlSource); |
949 // if (sourceEntry is HtmlEntry) { | 949 // if (sourceEntry is HtmlEntry) { |
950 // HtmlEntry htmlEntry = sourceEntry; | 950 // HtmlEntry htmlEntry = sourceEntry; |
951 // return htmlEntry.getValue(HtmlEntry.RESOLVED_UNIT); | 951 // return htmlEntry.getValue(HtmlEntry.RESOLVED_UNIT); |
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1456 ChangeNoticeImpl _getNotice(Source source) { | 1456 ChangeNoticeImpl _getNotice(Source source) { |
1457 // Used in commented out code. | 1457 // Used in commented out code. |
1458 ChangeNoticeImpl notice = _pendingNotices[source]; | 1458 ChangeNoticeImpl notice = _pendingNotices[source]; |
1459 if (notice == null) { | 1459 if (notice == null) { |
1460 notice = new ChangeNoticeImpl(source); | 1460 notice = new ChangeNoticeImpl(source); |
1461 _pendingNotices[source] = notice; | 1461 _pendingNotices[source] = notice; |
1462 } | 1462 } |
1463 return notice; | 1463 return notice; |
1464 } | 1464 } |
1465 | 1465 |
1466 Object _getResult(AnalysisTarget target, ResultDescriptor descriptor) { | |
1467 cache.CacheEntry entry = _cache.get(target); | |
1468 if (entry == null) { | |
1469 return descriptor.defaultValue; | |
1470 } | |
1471 if (entry.isValid(descriptor)) { | |
1472 return entry.getValue(descriptor); | |
1473 } | |
1474 return descriptor.defaultValue; | |
1475 } | |
1476 | |
1477 CacheState _getResultState( | |
1478 AnalysisTarget target, ResultDescriptor descriptor) { | |
1479 cache.CacheEntry entry = _cache.get(target); | |
1480 if (entry == null) { | |
1481 return CacheState.INVALID; | |
1482 } | |
1483 return entry.getState(descriptor); | |
1484 } | |
1485 | |
1486 /** | 1466 /** |
1487 * Return a list containing all of the sources known to this context that have | 1467 * Return a list containing all of the sources known to this context that have |
1488 * the given [kind]. | 1468 * the given [kind]. |
1489 */ | 1469 */ |
1490 List<Source> _getSources(SourceKind kind) { | 1470 List<Source> _getSources(SourceKind kind) { |
1491 List<Source> sources = new List<Source>(); | 1471 List<Source> sources = new List<Source>(); |
1492 MapIterator<AnalysisTarget, cache.CacheEntry> iterator = _cache.iterator(); | 1472 MapIterator<AnalysisTarget, cache.CacheEntry> iterator = _cache.iterator(); |
1493 while (iterator.moveNext()) { | 1473 while (iterator.moveNext()) { |
1494 if (iterator.value.getValue(SOURCE_KIND) == kind && | 1474 if (iterator.value.getValue(SOURCE_KIND) == kind && |
1495 iterator.key is Source) { | 1475 iterator.key is Source) { |
(...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2085 PendingFuture pendingFuture = | 2065 PendingFuture pendingFuture = |
2086 new PendingFuture<T>(_context, target, computeValue); | 2066 new PendingFuture<T>(_context, target, computeValue); |
2087 if (!pendingFuture.evaluate(entry)) { | 2067 if (!pendingFuture.evaluate(entry)) { |
2088 _context._pendingFutureTargets | 2068 _context._pendingFutureTargets |
2089 .putIfAbsent(target, () => <PendingFuture>[]) | 2069 .putIfAbsent(target, () => <PendingFuture>[]) |
2090 .add(pendingFuture); | 2070 .add(pendingFuture); |
2091 } | 2071 } |
2092 return pendingFuture.future; | 2072 return pendingFuture.future; |
2093 } | 2073 } |
2094 } | 2074 } |
OLD | NEW |