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 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 | 174 |
175 /** | 175 /** |
176 * A factory to override how [LibraryResolver] is created. | 176 * A factory to override how [LibraryResolver] is created. |
177 */ | 177 */ |
178 LibraryResolverFactory libraryResolverFactory; | 178 LibraryResolverFactory libraryResolverFactory; |
179 | 179 |
180 /** | 180 /** |
181 * Initialize a newly created analysis context. | 181 * Initialize a newly created analysis context. |
182 */ | 182 */ |
183 AnalysisContextImpl() { | 183 AnalysisContextImpl() { |
184 _privatePartition = new cache.UniversalCachePartition(this, | 184 _privatePartition = new cache.UniversalCachePartition(this); |
185 AnalysisOptionsImpl.DEFAULT_CACHE_SIZE, | |
186 new ContextRetentionPolicy(this)); | |
187 _cache = createCacheFromSourceFactory(null); | 185 _cache = createCacheFromSourceFactory(null); |
188 _taskManager = AnalysisEngine.instance.taskManager; | 186 _taskManager = AnalysisEngine.instance.taskManager; |
189 _driver = new AnalysisDriver(_taskManager, this); | 187 _driver = new AnalysisDriver(_taskManager, this); |
190 _onSourcesChangedController = | 188 _onSourcesChangedController = |
191 new StreamController<SourcesChangedEvent>.broadcast(); | 189 new StreamController<SourcesChangedEvent>.broadcast(); |
192 } | 190 } |
193 | 191 |
194 @override | 192 @override |
195 AnalysisOptions get analysisOptions => _options; | 193 AnalysisOptions get analysisOptions => _options; |
196 | 194 |
197 @override | 195 @override |
198 void set analysisOptions(AnalysisOptions options) { | 196 void set analysisOptions(AnalysisOptions options) { |
199 bool needsRecompute = this._options.analyzeFunctionBodiesPredicate != | 197 bool needsRecompute = this._options.analyzeFunctionBodiesPredicate != |
200 options.analyzeFunctionBodiesPredicate || | 198 options.analyzeFunctionBodiesPredicate || |
201 this._options.generateImplicitErrors != | 199 this._options.generateImplicitErrors != |
202 options.generateImplicitErrors || | 200 options.generateImplicitErrors || |
203 this._options.generateSdkErrors != options.generateSdkErrors || | 201 this._options.generateSdkErrors != options.generateSdkErrors || |
204 this._options.dart2jsHint != options.dart2jsHint || | 202 this._options.dart2jsHint != options.dart2jsHint || |
205 (this._options.hint && !options.hint) || | 203 (this._options.hint && !options.hint) || |
206 this._options.preserveComments != options.preserveComments || | 204 this._options.preserveComments != options.preserveComments || |
207 this._options.enableNullAwareOperators != | 205 this._options.enableNullAwareOperators != |
208 options.enableNullAwareOperators || | 206 options.enableNullAwareOperators || |
209 this._options.enableStrictCallChecks != options.enableStrictCallChecks; | 207 this._options.enableStrictCallChecks != options.enableStrictCallChecks; |
210 int cacheSize = options.cacheSize; | 208 int cacheSize = options.cacheSize; |
211 if (this._options.cacheSize != cacheSize) { | 209 if (this._options.cacheSize != cacheSize) { |
212 this._options.cacheSize = cacheSize; | 210 this._options.cacheSize = cacheSize; |
213 _privatePartition.maxCacheSize = cacheSize; | |
214 } | 211 } |
215 this._options.analyzeFunctionBodiesPredicate = | 212 this._options.analyzeFunctionBodiesPredicate = |
216 options.analyzeFunctionBodiesPredicate; | 213 options.analyzeFunctionBodiesPredicate; |
217 this._options.generateImplicitErrors = options.generateImplicitErrors; | 214 this._options.generateImplicitErrors = options.generateImplicitErrors; |
218 this._options.generateSdkErrors = options.generateSdkErrors; | 215 this._options.generateSdkErrors = options.generateSdkErrors; |
219 this._options.dart2jsHint = options.dart2jsHint; | 216 this._options.dart2jsHint = options.dart2jsHint; |
220 this._options.enableNullAwareOperators = options.enableNullAwareOperators; | 217 this._options.enableNullAwareOperators = options.enableNullAwareOperators; |
221 this._options.enableStrictCallChecks = options.enableStrictCallChecks; | 218 this._options.enableStrictCallChecks = options.enableStrictCallChecks; |
222 this._options.hint = options.hint; | 219 this._options.hint = options.hint; |
223 this._options.incremental = options.incremental; | 220 this._options.incremental = options.incremental; |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 * Return a list of the sources that would be processed by | 359 * Return a list of the sources that would be processed by |
363 * [performAnalysisTask]. This method duplicates, and must therefore be kept | 360 * [performAnalysisTask]. This method duplicates, and must therefore be kept |
364 * in sync with, [getNextAnalysisTask]. This method is intended to be used for | 361 * in sync with, [getNextAnalysisTask]. This method is intended to be used for |
365 * testing purposes only. | 362 * testing purposes only. |
366 */ | 363 */ |
367 List<Source> get sourcesNeedingProcessing { | 364 List<Source> get sourcesNeedingProcessing { |
368 HashSet<Source> sources = new HashSet<Source>(); | 365 HashSet<Source> sources = new HashSet<Source>(); |
369 bool hintsEnabled = _options.hint; | 366 bool hintsEnabled = _options.hint; |
370 bool lintsEnabled = _options.lint; | 367 bool lintsEnabled = _options.lint; |
371 | 368 |
372 MapIterator<AnalysisTarget, cache.CacheEntry> iterator = _privatePartition.i
terator(); | 369 MapIterator<AnalysisTarget, cache.CacheEntry> iterator = |
| 370 _privatePartition.iterator(); |
373 while (iterator.moveNext()) { | 371 while (iterator.moveNext()) { |
374 AnalysisTarget target = iterator.key; | 372 AnalysisTarget target = iterator.key; |
375 if (target is Source) { | 373 if (target is Source) { |
376 _getSourcesNeedingProcessing( | 374 _getSourcesNeedingProcessing( |
377 target, iterator.value, false, hintsEnabled, lintsEnabled, sources); | 375 target, iterator.value, false, hintsEnabled, lintsEnabled, sources); |
378 } | 376 } |
379 } | 377 } |
380 return new List<Source>.from(sources); | 378 return new List<Source>.from(sources); |
381 } | 379 } |
382 | 380 |
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
834 return SourceKind.UNKNOWN; | 832 return SourceKind.UNKNOWN; |
835 } | 833 } |
836 | 834 |
837 @override | 835 @override |
838 List<Source> getLibrariesContaining(Source source) { | 836 List<Source> getLibrariesContaining(Source source) { |
839 SourceKind kind = getKindOf(source); | 837 SourceKind kind = getKindOf(source); |
840 if (kind == SourceKind.LIBRARY) { | 838 if (kind == SourceKind.LIBRARY) { |
841 return <Source>[source]; | 839 return <Source>[source]; |
842 } else if (kind == SourceKind.PART) { | 840 } else if (kind == SourceKind.PART) { |
843 List<Source> libraries = <Source>[]; | 841 List<Source> libraries = <Source>[]; |
844 MapIterator<AnalysisTarget, cache.CacheEntry> iterator = _cache.iterator()
; | 842 MapIterator<AnalysisTarget, cache.CacheEntry> iterator = |
| 843 _cache.iterator(); |
845 while (iterator.moveNext()) { | 844 while (iterator.moveNext()) { |
846 AnalysisTarget target = iterator.key; | 845 AnalysisTarget target = iterator.key; |
847 if (target is Source && getKindOf(target) == SourceKind.LIBRARY) { | 846 if (target is Source && getKindOf(target) == SourceKind.LIBRARY) { |
848 List<Source> parts = _getResult(target, INCLUDED_PARTS); | 847 List<Source> parts = _getResult(target, INCLUDED_PARTS); |
849 if (parts.contains(source)) { | 848 if (parts.contains(source)) { |
850 libraries.add(target); | 849 libraries.add(target); |
851 } | 850 } |
852 } | 851 } |
853 } | 852 } |
854 if (libraries.isNotEmpty) { | 853 if (libraries.isNotEmpty) { |
(...skipping 1103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1958 buffer.write(" "); | 1957 buffer.write(" "); |
1959 buffer.writeln(source.fullName); | 1958 buffer.writeln(source.fullName); |
1960 } | 1959 } |
1961 _logInformation(buffer.toString()); | 1960 _logInformation(buffer.toString()); |
1962 } | 1961 } |
1963 return changedTargets.length > 0; | 1962 return changedTargets.length > 0; |
1964 } | 1963 } |
1965 } | 1964 } |
1966 | 1965 |
1967 /** | 1966 /** |
1968 * A retention policy used by an analysis context. | |
1969 */ | |
1970 class ContextRetentionPolicy implements cache.CacheRetentionPolicy { | |
1971 /** | |
1972 * The context associated with this policy. | |
1973 */ | |
1974 final AnalysisContextImpl context; | |
1975 | |
1976 /** | |
1977 * Initialize a newly created policy to be associated with the given | |
1978 * [context]. | |
1979 */ | |
1980 ContextRetentionPolicy(this.context); | |
1981 | |
1982 @override | |
1983 RetentionPriority getAstPriority( | |
1984 AnalysisTarget target, cache.CacheEntry entry) { | |
1985 int priorityCount = context._priorityOrder.length; | |
1986 for (int i = 0; i < priorityCount; i++) { | |
1987 if (target == context._priorityOrder[i]) { | |
1988 return RetentionPriority.HIGH; | |
1989 } | |
1990 } | |
1991 if (_astIsNeeded(entry)) { | |
1992 return RetentionPriority.MEDIUM; | |
1993 } | |
1994 return RetentionPriority.LOW; | |
1995 } | |
1996 | |
1997 bool _astIsNeeded(cache.CacheEntry entry) => | |
1998 entry.isInvalid(BUILD_FUNCTION_TYPE_ALIASES_ERRORS) || | |
1999 entry.isInvalid(BUILD_LIBRARY_ERRORS) || | |
2000 entry.isInvalid(CONSTRUCTORS_ERRORS) || | |
2001 entry.isInvalid(HINTS) || | |
2002 //entry.isInvalid(LINTS) || | |
2003 entry.isInvalid(RESOLVE_REFERENCES_ERRORS) || | |
2004 entry.isInvalid(RESOLVE_TYPE_NAMES_ERRORS) || | |
2005 entry.isInvalid(VERIFY_ERRORS); | |
2006 } | |
2007 | |
2008 /** | |
2009 * An object that manages the partitions that can be shared between analysis | 1967 * An object that manages the partitions that can be shared between analysis |
2010 * contexts. | 1968 * contexts. |
2011 */ | 1969 */ |
2012 class PartitionManager { | 1970 class PartitionManager { |
2013 /** | 1971 /** |
2014 * The default cache size for a Dart SDK partition. | |
2015 */ | |
2016 static int _DEFAULT_SDK_CACHE_SIZE = 256; | |
2017 | |
2018 /** | |
2019 * A table mapping SDK's to the partitions used for those SDK's. | 1972 * A table mapping SDK's to the partitions used for those SDK's. |
2020 */ | 1973 */ |
2021 HashMap<DartSdk, cache.SdkCachePartition> _sdkPartitions = | 1974 HashMap<DartSdk, cache.SdkCachePartition> _sdkPartitions = |
2022 new HashMap<DartSdk, cache.SdkCachePartition>(); | 1975 new HashMap<DartSdk, cache.SdkCachePartition>(); |
2023 | 1976 |
2024 /** | 1977 /** |
2025 * Clear any cached data being maintained by this manager. | 1978 * Clear any cached data being maintained by this manager. |
2026 */ | 1979 */ |
2027 void clearCache() { | 1980 void clearCache() { |
2028 _sdkPartitions.clear(); | 1981 _sdkPartitions.clear(); |
2029 } | 1982 } |
2030 | 1983 |
2031 /** | 1984 /** |
2032 * Return the partition being used for the given [sdk], creating the partition | 1985 * Return the partition being used for the given [sdk], creating the partition |
2033 * if necessary. | 1986 * if necessary. |
2034 */ | 1987 */ |
2035 cache.SdkCachePartition forSdk(DartSdk sdk) { | 1988 cache.SdkCachePartition forSdk(DartSdk sdk) { |
2036 // Call sdk.context now, because when it creates a new | 1989 // Call sdk.context now, because when it creates a new |
2037 // InternalAnalysisContext instance, it calls forSdk() again, so creates an | 1990 // InternalAnalysisContext instance, it calls forSdk() again, so creates an |
2038 // SdkCachePartition instance. | 1991 // SdkCachePartition instance. |
2039 // So, if we initialize context after "partition == null", we end up | 1992 // So, if we initialize context after "partition == null", we end up |
2040 // with two SdkCachePartition instances. | 1993 // with two SdkCachePartition instances. |
2041 InternalAnalysisContext sdkContext = sdk.context; | 1994 InternalAnalysisContext sdkContext = sdk.context; |
2042 // Check cache for an existing partition. | 1995 // Check cache for an existing partition. |
2043 cache.SdkCachePartition partition = _sdkPartitions[sdk]; | 1996 cache.SdkCachePartition partition = _sdkPartitions[sdk]; |
2044 if (partition == null) { | 1997 if (partition == null) { |
2045 partition = | 1998 partition = new cache.SdkCachePartition(sdkContext); |
2046 new cache.SdkCachePartition(sdkContext, _DEFAULT_SDK_CACHE_SIZE); | |
2047 _sdkPartitions[sdk] = partition; | 1999 _sdkPartitions[sdk] = partition; |
2048 } | 2000 } |
2049 return partition; | 2001 return partition; |
2050 } | 2002 } |
2051 } | 2003 } |
2052 | 2004 |
2053 /** | 2005 /** |
2054 * Representation of a pending computation which is based on the results of | 2006 * Representation of a pending computation which is based on the results of |
2055 * analysis that may or may not have been completed. | 2007 * analysis that may or may not have been completed. |
2056 */ | 2008 */ |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2170 PendingFuture pendingFuture = | 2122 PendingFuture pendingFuture = |
2171 new PendingFuture<T>(_context, target, computeValue); | 2123 new PendingFuture<T>(_context, target, computeValue); |
2172 if (!pendingFuture.evaluate(entry)) { | 2124 if (!pendingFuture.evaluate(entry)) { |
2173 _context._pendingFutureTargets | 2125 _context._pendingFutureTargets |
2174 .putIfAbsent(target, () => <PendingFuture>[]) | 2126 .putIfAbsent(target, () => <PendingFuture>[]) |
2175 .add(pendingFuture); | 2127 .add(pendingFuture); |
2176 } | 2128 } |
2177 return pendingFuture.future; | 2129 return pendingFuture.future; |
2178 } | 2130 } |
2179 } | 2131 } |
OLD | NEW |