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 engine.incremental_resolver; | 5 library engine.incremental_resolver; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 import 'dart:math' as math; | 8 import 'dart:math' as math; |
9 | 9 |
10 import 'package:analyzer/src/context/cache.dart' | 10 import 'package:analyzer/src/context/cache.dart' |
11 show CacheEntry, TargetedResult; | 11 show CacheEntry, Delta, DeltaResult, TargetedResult; |
12 import 'package:analyzer/src/generated/constant.dart'; | 12 import 'package:analyzer/src/generated/constant.dart'; |
13 import 'package:analyzer/src/task/dart.dart' | 13 import 'package:analyzer/src/task/dart.dart'; |
14 show | 14 import 'package:analyzer/task/dart.dart'; |
15 HINTS, | |
16 INFER_STATIC_VARIABLE_TYPES_ERRORS, | |
17 LIBRARY_UNIT_ERRORS, | |
18 PARSE_ERRORS, | |
19 PARTIALLY_RESOLVE_REFERENCES_ERRORS, | |
20 RESOLVE_FUNCTION_BODIES_ERRORS, | |
21 RESOLVE_TYPE_NAMES_ERRORS, | |
22 SCAN_ERRORS, | |
23 USED_IMPORTED_ELEMENTS, | |
24 USED_LOCAL_ELEMENTS, | |
25 VARIABLE_REFERENCE_ERRORS, | |
26 VERIFY_ERRORS; | |
27 import 'package:analyzer/task/dart.dart' | |
28 show DART_ERRORS, LibrarySpecificUnit, PARSED_UNIT, TOKEN_STREAM; | |
29 import 'package:analyzer/task/general.dart' show CONTENT, LINE_INFO; | 15 import 'package:analyzer/task/general.dart' show CONTENT, LINE_INFO; |
30 import 'package:analyzer/task/model.dart' show ResultDescriptor, TargetedResult; | 16 import 'package:analyzer/task/model.dart' |
17 show AnalysisTarget, ResultDescriptor, TargetedResult, TaskDescriptor; | |
31 | 18 |
32 import 'ast.dart'; | 19 import 'ast.dart'; |
33 import 'element.dart'; | 20 import 'element.dart'; |
34 import 'engine.dart'; | 21 import 'engine.dart' |
22 show | |
23 AnalysisContext, | |
24 AnalysisOptions, | |
25 CacheState, | |
26 DartEntry, | |
27 DataDescriptor, | |
28 InternalAnalysisContext, | |
29 RecordingErrorListener, | |
30 SourceEntry; | |
35 import 'error.dart'; | 31 import 'error.dart'; |
36 import 'error_verifier.dart'; | 32 import 'error_verifier.dart'; |
37 import 'incremental_logger.dart' show logger, LoggingTimer; | 33 import 'incremental_logger.dart' show logger, LoggingTimer; |
38 import 'java_engine.dart'; | 34 import 'java_engine.dart'; |
39 import 'parser.dart'; | 35 import 'parser.dart'; |
40 import 'resolver.dart'; | 36 import 'resolver.dart'; |
41 import 'scanner.dart'; | 37 import 'scanner.dart'; |
42 import 'source.dart'; | 38 import 'source.dart'; |
43 import 'utilities_dart.dart'; | 39 import 'utilities_dart.dart'; |
44 | 40 |
(...skipping 778 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
823 | 819 |
824 final String name; | 820 final String name; |
825 | 821 |
826 const DeclarationMatchKind(this.name); | 822 const DeclarationMatchKind(this.name); |
827 | 823 |
828 @override | 824 @override |
829 String toString() => name; | 825 String toString() => name; |
830 } | 826 } |
831 | 827 |
832 /** | 828 /** |
829 * The [Delta] implementation used by incremental resolver. | |
830 * It keeps Dart results that are either don't change or are updated. | |
831 */ | |
832 class IncrementalBodyDelta extends Delta { | |
833 /** | |
834 * The offset of the changed contents. | |
835 */ | |
836 final int updateOffset; | |
837 | |
838 /** | |
839 * The end of the changed contents in the old unit. | |
840 */ | |
841 final int updateEndOld; | |
842 | |
843 /** | |
844 * The end of the changed contents in the new unit. | |
845 */ | |
846 final int updateEndNew; | |
847 | |
848 /** | |
849 * The delta between [updateEndNew] and [updateEndOld]. | |
850 */ | |
851 final int updateDelta; | |
852 | |
853 IncrementalBodyDelta(Source source, this.updateOffset, this.updateEndOld, | |
854 this.updateEndNew, this.updateDelta) | |
855 : super(source); | |
856 | |
857 @override | |
858 DeltaResult validate(InternalAnalysisContext context, AnalysisTarget target, | |
859 ResultDescriptor descriptor) { | |
860 // don't invalidate results of standard Dart tasks | |
861 bool isByTask(TaskDescriptor taskDescriptor) { | |
862 return taskDescriptor.results.contains(descriptor); | |
863 } | |
864 if (descriptor == CONTENT) { | |
865 return DeltaResult.KEEP_CONTINUE; | |
866 } | |
867 if (isByTask(BuildCompilationUnitElementTask.DESCRIPTOR) || | |
868 isByTask(BuildDirectiveElementsTask.DESCRIPTOR) || | |
869 isByTask(BuildEnumMemberElementsTask.DESCRIPTOR) || | |
870 isByTask(BuildExportNamespaceTask.DESCRIPTOR) || | |
871 isByTask(BuildLibraryElementTask.DESCRIPTOR) || | |
872 isByTask(BuildPublicNamespaceTask.DESCRIPTOR) || | |
873 isByTask(BuildSourceExportClosureTask.DESCRIPTOR) || | |
874 isByTask(BuildSourceImportExportClosureTask.DESCRIPTOR) || | |
875 isByTask(ComputeConstantDependenciesTask.DESCRIPTOR) || | |
876 isByTask(ComputeConstantValueTask.DESCRIPTOR) || | |
877 isByTask(EvaluateUnitConstantsTask.DESCRIPTOR) || | |
878 isByTask(InferInstanceMembersInUnitTask.DESCRIPTOR) || | |
879 isByTask(InferStaticVariableTypesInUnitTask.DESCRIPTOR) || | |
880 isByTask(ParseDartTask.DESCRIPTOR) || | |
881 isByTask(PartiallyResolveUnitReferencesTask.DESCRIPTOR) || | |
882 isByTask(ScanDartTask.DESCRIPTOR) || | |
883 isByTask(ResolveFunctionBodiesInUnitTask.DESCRIPTOR) || | |
884 isByTask(ResolveLibraryReferencesTask.DESCRIPTOR) || | |
885 isByTask(ResolveLibraryTypeNamesTask.DESCRIPTOR) || | |
886 isByTask(ResolveUnitTypeNamesTask.DESCRIPTOR) || | |
887 isByTask(ResolveVariableReferencesTask.DESCRIPTOR) || | |
888 isByTask(VerifyUnitTask.DESCRIPTOR) || | |
889 false) { | |
Brian Wilkerson
2015/09/10 20:06:51
???
| |
890 return DeltaResult.KEEP_CONTINUE; | |
891 } | |
892 // invalidate all the other results | |
893 return DeltaResult.INVALIDATE; | |
894 } | |
895 } | |
896 | |
897 /** | |
833 * Instances of the class [IncrementalResolver] resolve the smallest portion of | 898 * Instances of the class [IncrementalResolver] resolve the smallest portion of |
834 * an AST structure that we currently know how to resolve. | 899 * an AST structure that we currently know how to resolve. |
835 */ | 900 */ |
836 class IncrementalResolver { | 901 class IncrementalResolver { |
837 /** | 902 /** |
838 * The element of the compilation unit being resolved. | 903 * The element of the compilation unit being resolved. |
839 */ | 904 */ |
840 final CompilationUnitElementImpl _definingUnit; | 905 final CompilationUnitElementImpl _definingUnit; |
841 | 906 |
842 /** | 907 /** |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
887 /** | 952 /** |
888 * The end of the changed contents in the old unit. | 953 * The end of the changed contents in the old unit. |
889 */ | 954 */ |
890 final int _updateEndOld; | 955 final int _updateEndOld; |
891 | 956 |
892 /** | 957 /** |
893 * The end of the changed contents in the new unit. | 958 * The end of the changed contents in the new unit. |
894 */ | 959 */ |
895 final int _updateEndNew; | 960 final int _updateEndNew; |
896 | 961 |
962 /** | |
963 * The delta between [_updateEndNew] and [_updateEndOld]. | |
964 */ | |
897 int _updateDelta; | 965 int _updateDelta; |
898 | 966 |
899 /** | 967 /** |
900 * The set of [AnalysisError]s that have been already shifted. | 968 * The set of [AnalysisError]s that have been already shifted. |
901 */ | 969 */ |
902 Set<AnalysisError> _alreadyShiftedErrors = new HashSet.identity(); | 970 Set<AnalysisError> _alreadyShiftedErrors = new HashSet.identity(); |
903 | 971 |
904 RecordingErrorListener errorListener = new RecordingErrorListener(); | 972 RecordingErrorListener errorListener = new RecordingErrorListener(); |
905 ResolutionContext _resolutionContext; | 973 ResolutionContext _resolutionContext; |
906 | 974 |
(...skipping 26 matching lines...) Expand all Loading... | |
933 * [node] - the root of the AST structure to be resolved. | 1001 * [node] - the root of the AST structure to be resolved. |
934 * | 1002 * |
935 * Returns `true` if resolution was successful. | 1003 * Returns `true` if resolution was successful. |
936 */ | 1004 */ |
937 bool resolve(AstNode node) { | 1005 bool resolve(AstNode node) { |
938 logger.enter('resolve: $_definingUnit'); | 1006 logger.enter('resolve: $_definingUnit'); |
939 try { | 1007 try { |
940 AstNode rootNode = _findResolutionRoot(node); | 1008 AstNode rootNode = _findResolutionRoot(node); |
941 _prepareResolutionContext(rootNode); | 1009 _prepareResolutionContext(rootNode); |
942 // update elements | 1010 // update elements |
1011 _updateCache(); | |
943 _updateElementNameOffsets(); | 1012 _updateElementNameOffsets(); |
944 _buildElements(rootNode); | 1013 _buildElements(rootNode); |
945 if (!_canBeIncrementallyResolved(rootNode)) { | 1014 if (!_canBeIncrementallyResolved(rootNode)) { |
946 return false; | 1015 return false; |
947 } | 1016 } |
948 // resolve | 1017 // resolve |
949 _resolveReferences(rootNode); | 1018 _resolveReferences(rootNode); |
950 _computeConstants(rootNode); | 1019 _computeConstants(rootNode); |
951 _resolveErrors = errorListener.getErrorsForSource(_source); | 1020 _resolveErrors = errorListener.getErrorsForSource(_source); |
952 // verify | 1021 // verify |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1164 List<AnalysisError> errors = newUnitEntry.getValue(descriptor); | 1233 List<AnalysisError> errors = newUnitEntry.getValue(descriptor); |
1165 _shiftErrors(errors); | 1234 _shiftErrors(errors); |
1166 } | 1235 } |
1167 | 1236 |
1168 void _shiftErrors_OLD(DataDescriptor<List<AnalysisError>> descriptor) { | 1237 void _shiftErrors_OLD(DataDescriptor<List<AnalysisError>> descriptor) { |
1169 List<AnalysisError> errors = | 1238 List<AnalysisError> errors = |
1170 oldEntry.getValueInLibrary(descriptor, _librarySource); | 1239 oldEntry.getValueInLibrary(descriptor, _librarySource); |
1171 _shiftErrors(errors); | 1240 _shiftErrors(errors); |
1172 } | 1241 } |
1173 | 1242 |
1243 void _updateCache() { | |
1244 if (newSourceEntry != null) { | |
1245 newSourceEntry.setState(CONTENT, CacheState.INVALID, | |
1246 delta: new IncrementalBodyDelta(_source, _updateOffset, _updateEndOld, | |
1247 _updateEndNew, _updateDelta)); | |
1248 } | |
1249 } | |
1250 | |
1174 void _updateElementNameOffsets() { | 1251 void _updateElementNameOffsets() { |
1175 LoggingTimer timer = logger.startTimer(); | 1252 LoggingTimer timer = logger.startTimer(); |
1176 try { | 1253 try { |
1177 _definingUnit | 1254 _definingUnit |
1178 .accept(new _ElementNameOffsetUpdater(_updateOffset, _updateDelta)); | 1255 .accept(new _ElementNameOffsetUpdater(_updateOffset, _updateDelta)); |
1179 } finally { | 1256 } finally { |
1180 timer.stop('update element offsets'); | 1257 timer.stop('update element offsets'); |
1181 } | 1258 } |
1182 } | 1259 } |
1183 | 1260 |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1367 _shiftTokens(firstPair.oldToken); | 1444 _shiftTokens(firstPair.oldToken); |
1368 { | 1445 { |
1369 IncrementalResolver incrementalResolver = new IncrementalResolver( | 1446 IncrementalResolver incrementalResolver = new IncrementalResolver( |
1370 _oldEntry, | 1447 _oldEntry, |
1371 _newSourceEntry, | 1448 _newSourceEntry, |
1372 _newUnitEntry, | 1449 _newUnitEntry, |
1373 _unitElement, | 1450 _unitElement, |
1374 _updateOffset, | 1451 _updateOffset, |
1375 _updateEndOld, | 1452 _updateEndOld, |
1376 _updateEndNew); | 1453 _updateEndNew); |
1454 incrementalResolver._updateCache(); | |
1377 incrementalResolver._updateElementNameOffsets(); | 1455 incrementalResolver._updateElementNameOffsets(); |
1378 incrementalResolver._shiftEntryErrors(); | 1456 incrementalResolver._shiftEntryErrors(); |
1379 } | 1457 } |
1380 _updateEntry(); | 1458 _updateEntry(); |
1381 logger.log('Success.'); | 1459 logger.log('Success.'); |
1382 return true; | 1460 return true; |
1383 } | 1461 } |
1384 // fall-through, end-of-line comment | 1462 // fall-through, end-of-line comment |
1385 } | 1463 } |
1386 // Find nodes covering the "old" and "new" token ranges. | 1464 // Find nodes covering the "old" and "new" token ranges. |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1534 NodeReplacer.replace(oldComment, newComment); | 1612 NodeReplacer.replace(oldComment, newComment); |
1535 // update elements | 1613 // update elements |
1536 IncrementalResolver incrementalResolver = new IncrementalResolver( | 1614 IncrementalResolver incrementalResolver = new IncrementalResolver( |
1537 _oldEntry, | 1615 _oldEntry, |
1538 _newSourceEntry, | 1616 _newSourceEntry, |
1539 _newUnitEntry, | 1617 _newUnitEntry, |
1540 _unitElement, | 1618 _unitElement, |
1541 _updateOffset, | 1619 _updateOffset, |
1542 _updateEndOld, | 1620 _updateEndOld, |
1543 _updateEndNew); | 1621 _updateEndNew); |
1622 incrementalResolver._updateCache(); | |
1544 incrementalResolver._updateElementNameOffsets(); | 1623 incrementalResolver._updateElementNameOffsets(); |
1545 incrementalResolver._shiftEntryErrors(); | 1624 incrementalResolver._shiftEntryErrors(); |
1546 _updateEntry(); | 1625 _updateEntry(); |
1547 // resolve references in the comment | 1626 // resolve references in the comment |
1548 incrementalResolver._resolveReferences(newComment); | 1627 incrementalResolver._resolveReferences(newComment); |
1549 // OK | 1628 // OK |
1550 return true; | 1629 return true; |
1551 } | 1630 } |
1552 | 1631 |
1553 Token _scan(String code) { | 1632 Token _scan(String code) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1602 if (_oldEntry != null) { | 1681 if (_oldEntry != null) { |
1603 _updateEntry_OLD(); | 1682 _updateEntry_OLD(); |
1604 } else { | 1683 } else { |
1605 _updateEntry_NEW(); | 1684 _updateEntry_NEW(); |
1606 } | 1685 } |
1607 } | 1686 } |
1608 | 1687 |
1609 void _updateEntry_NEW() { | 1688 void _updateEntry_NEW() { |
1610 _newSourceEntry.setState(DART_ERRORS, CacheState.INVALID); | 1689 _newSourceEntry.setState(DART_ERRORS, CacheState.INVALID); |
1611 // scan results | 1690 // scan results |
1612 _newSourceEntry.setState(SCAN_ERRORS, CacheState.INVALID); | |
1613 List<TargetedResult> scanDeps = <TargetedResult>[ | 1691 List<TargetedResult> scanDeps = <TargetedResult>[ |
1614 new TargetedResult(_unitSource, CONTENT) | 1692 new TargetedResult(_unitSource, CONTENT) |
1615 ]; | 1693 ]; |
1694 _newSourceEntry.setState(SCAN_ERRORS, CacheState.INVALID); | |
1695 _newSourceEntry.setValue(SCAN_ERRORS, _newScanErrors, scanDeps); | |
1616 _newSourceEntry.setValue(LINE_INFO, _newLineInfo, scanDeps); | 1696 _newSourceEntry.setValue(LINE_INFO, _newLineInfo, scanDeps); |
1617 _newSourceEntry.setValue(SCAN_ERRORS, _newScanErrors, scanDeps); | |
1618 // parse results | 1697 // parse results |
1619 List<TargetedResult> parseDeps = <TargetedResult>[ | 1698 List<TargetedResult> parseDeps = <TargetedResult>[ |
1620 new TargetedResult(_unitSource, TOKEN_STREAM) | 1699 new TargetedResult(_unitSource, TOKEN_STREAM) |
1621 ]; | 1700 ]; |
1622 _newSourceEntry.setState(PARSE_ERRORS, CacheState.INVALID); | 1701 _newSourceEntry.setState(PARSE_ERRORS, CacheState.INVALID); |
1623 _newSourceEntry.setValue(PARSE_ERRORS, _newParseErrors, parseDeps); | 1702 _newSourceEntry.setValue(PARSE_ERRORS, _newParseErrors, parseDeps); |
1624 _newSourceEntry.setValue(PARSED_UNIT, _oldUnit, parseDeps); | 1703 _newSourceEntry.setValue(PARSED_UNIT, _oldUnit, parseDeps); |
1625 } | 1704 } |
1626 | 1705 |
1627 void _updateEntry_OLD() { | 1706 void _updateEntry_OLD() { |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2031 @override | 2110 @override |
2032 String toString() => name; | 2111 String toString() => name; |
2033 } | 2112 } |
2034 | 2113 |
2035 class _TokenPair { | 2114 class _TokenPair { |
2036 final _TokenDifferenceKind kind; | 2115 final _TokenDifferenceKind kind; |
2037 final Token oldToken; | 2116 final Token oldToken; |
2038 final Token newToken; | 2117 final Token newToken; |
2039 _TokenPair(this.kind, this.oldToken, this.newToken); | 2118 _TokenPair(this.kind, this.oldToken, this.newToken); |
2040 } | 2119 } |
OLD | NEW |