| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'package:analyzer/context/declared_variables.dart'; | 5 import 'package:analyzer/context/declared_variables.dart'; |
| 6 import 'package:analyzer/dart/ast/ast.dart'; | 6 import 'package:analyzer/dart/ast/ast.dart'; |
| 7 import 'package:analyzer/dart/element/element.dart' show CompilationUnitElement; | 7 import 'package:analyzer/dart/element/element.dart' |
| 8 show CompilationUnitElement, LibraryElement; |
| 8 import 'package:analyzer/error/error.dart'; | 9 import 'package:analyzer/error/error.dart'; |
| 9 import 'package:analyzer/src/context/context.dart'; | 10 import 'package:analyzer/src/context/context.dart'; |
| 10 import 'package:analyzer/src/dart/analysis/byte_store.dart'; | 11 import 'package:analyzer/src/dart/analysis/byte_store.dart'; |
| 11 import 'package:analyzer/src/dart/analysis/driver.dart'; | 12 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 12 import 'package:analyzer/src/dart/analysis/file_state.dart'; | 13 import 'package:analyzer/src/dart/analysis/file_state.dart'; |
| 13 import 'package:analyzer/src/dart/analysis/file_tracker.dart'; | 14 import 'package:analyzer/src/dart/analysis/file_tracker.dart'; |
| 14 import 'package:analyzer/src/generated/engine.dart' | 15 import 'package:analyzer/src/generated/engine.dart' |
| 15 show AnalysisContext, AnalysisEngine, AnalysisOptions; | 16 show AnalysisContext, AnalysisEngine, AnalysisOptions; |
| 16 import 'package:analyzer/src/generated/source.dart'; | 17 import 'package:analyzer/src/generated/source.dart'; |
| 17 import 'package:analyzer/src/summary/format.dart'; | 18 import 'package:analyzer/src/summary/format.dart'; |
| 18 import 'package:analyzer/src/summary/idl.dart'; | 19 import 'package:analyzer/src/summary/idl.dart'; |
| 19 import 'package:analyzer/src/summary/link.dart'; | 20 import 'package:analyzer/src/summary/link.dart'; |
| 20 import 'package:analyzer/src/summary/package_bundle_reader.dart'; | 21 import 'package:analyzer/src/summary/package_bundle_reader.dart'; |
| 21 import 'package:analyzer/src/task/dart.dart' show COMPILATION_UNIT_ELEMENT; | 22 import 'package:analyzer/src/task/dart.dart' show COMPILATION_UNIT_ELEMENT; |
| 22 import 'package:analyzer/task/dart.dart' show LibrarySpecificUnit; | 23 import 'package:analyzer/task/dart.dart' |
| 24 show LIBRARY_ELEMENT, LibrarySpecificUnit; |
| 23 | 25 |
| 24 /** | 26 /** |
| 25 * Context information necessary to analyze one or more libraries within an | 27 * Context information necessary to analyze one or more libraries within an |
| 26 * [AnalysisDriver]. | 28 * [AnalysisDriver]. |
| 27 * | 29 * |
| 28 * Currently this is implemented as a wrapper around [AnalysisContext]. | 30 * Currently this is implemented as a wrapper around [AnalysisContext]. |
| 29 * TODO(paulberry): make a front end API that this can make use of instead. | 31 * TODO(paulberry): make a front end API that this can make use of instead. |
| 30 */ | 32 */ |
| 31 class LibraryContext { | 33 class LibraryContext { |
| 32 final SummaryDataStore store; | 34 final SummaryDataStore store; |
| 33 | 35 |
| 34 /** | 36 /** |
| 35 * The [AnalysisContext] which is used to do the analysis. | 37 * The [AnalysisContext] which is used to do the analysis. |
| 36 */ | 38 */ |
| 37 final AnalysisContext _analysisContext; | 39 final AnalysisContext _analysisContext; |
| 38 | 40 |
| 39 /** | 41 /** |
| 40 * Create a [LibraryContext] which is prepared to analyze [targetLibrary]. | 42 * Create a [LibraryContext] which is prepared to analyze [targetLibrary]. |
| 41 */ | 43 */ |
| 42 factory LibraryContext.forSingleLibrary( | 44 factory LibraryContext.forSingleLibrary( |
| 43 FileState targetLibrary, | 45 FileState targetLibrary, |
| 44 PerformanceLog logger, | 46 PerformanceLog logger, |
| 45 PackageBundle sdkBundle, | 47 PackageBundle sdkBundle, |
| 46 ByteStore byteStore, | 48 ByteStore byteStore, |
| 47 AnalysisOptions options, | 49 AnalysisOptions options, |
| 48 DeclaredVariables declaredVariables, | 50 DeclaredVariables declaredVariables, |
| 49 SourceFactory sourceFactory, | 51 SourceFactory sourceFactory, |
| 52 SummaryDataStore externalSummaries, |
| 50 FileTracker fileTracker) { | 53 FileTracker fileTracker) { |
| 51 return logger.run('Create library context', () { | 54 return logger.run('Create library context', () { |
| 52 Map<String, FileState> libraries = <String, FileState>{}; | 55 Map<String, FileState> libraries = <String, FileState>{}; |
| 53 SummaryDataStore store = new SummaryDataStore(const <String>[]); | 56 SummaryDataStore store = new SummaryDataStore(const <String>[]); |
| 54 | 57 |
| 58 if (externalSummaries != null) { |
| 59 store.addStore(externalSummaries); |
| 60 } |
| 61 |
| 55 if (sdkBundle != null) { | 62 if (sdkBundle != null) { |
| 56 store.addBundle(null, sdkBundle); | 63 store.addBundle(null, sdkBundle); |
| 57 } | 64 } |
| 58 | 65 |
| 59 void appendLibraryFiles(FileState library) { | 66 void appendLibraryFiles(FileState library) { |
| 60 if (!libraries.containsKey(library.uriStr)) { | 67 if (!libraries.containsKey(library.uriStr)) { |
| 61 // Serve 'dart:' URIs from the SDK bundle. | 68 // Serve 'dart:' URIs from the SDK bundle. |
| 69 // TODO(scheglov) Repeal and replace with the external store. |
| 62 if (sdkBundle != null && library.uri.scheme == 'dart') { | 70 if (sdkBundle != null && library.uri.scheme == 'dart') { |
| 63 return; | 71 return; |
| 64 } | 72 } |
| 65 | 73 |
| 66 libraries[library.uriStr] = library; | 74 libraries[library.uriStr] = library; |
| 67 | 75 |
| 68 // Append the defining unit. | 76 // Append the defining unit. |
| 69 store.addUnlinkedUnit(library.uriStr, library.unlinked); | 77 store.addUnlinkedUnit(library.uriStr, library.unlinked); |
| 70 | 78 |
| 71 // Append parts. | 79 // Append parts. |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 * Computes a resolved [CompilationUnit] and a list of [AnalysisError]s for | 162 * Computes a resolved [CompilationUnit] and a list of [AnalysisError]s for |
| 155 * the given library/unit pair. | 163 * the given library/unit pair. |
| 156 */ | 164 */ |
| 157 ResolutionResult resolveUnit(Source librarySource, Source unitSource) { | 165 ResolutionResult resolveUnit(Source librarySource, Source unitSource) { |
| 158 CompilationUnit resolvedUnit = | 166 CompilationUnit resolvedUnit = |
| 159 _analysisContext.resolveCompilationUnit2(unitSource, librarySource); | 167 _analysisContext.resolveCompilationUnit2(unitSource, librarySource); |
| 160 List<AnalysisError> errors = _analysisContext.computeErrors(unitSource); | 168 List<AnalysisError> errors = _analysisContext.computeErrors(unitSource); |
| 161 return new ResolutionResult(resolvedUnit, errors); | 169 return new ResolutionResult(resolvedUnit, errors); |
| 162 } | 170 } |
| 163 | 171 |
| 172 /** |
| 173 * TODO(scheglov) document |
| 174 */ |
| 175 static LibraryElement resynthesizeLibrary( |
| 176 AnalysisOptions analysisOptions, |
| 177 DeclaredVariables declaredVariables, |
| 178 SourceFactory sourceFactory, |
| 179 SummaryDataStore store, |
| 180 String uri) { |
| 181 AnalysisContextImpl analysisContext = |
| 182 AnalysisEngine.instance.createAnalysisContext(); |
| 183 analysisContext.useSdkCachePartition = false; |
| 184 analysisContext.analysisOptions = analysisOptions; |
| 185 analysisContext.declaredVariables.addAll(declaredVariables); |
| 186 analysisContext.sourceFactory = sourceFactory.clone(); |
| 187 var provider = new InputPackagesResultProvider(analysisContext, store); |
| 188 var source = sourceFactory.resolveUri(null, uri); |
| 189 var entry = analysisContext.getCacheEntry(source); |
| 190 bool success = provider.compute(entry, LIBRARY_ELEMENT); |
| 191 if (!success) { |
| 192 throw new StateError('Expected successful resynthesis of $source'); |
| 193 } |
| 194 return entry.getValue(LIBRARY_ELEMENT); |
| 195 } |
| 196 |
| 164 static AnalysisContext _createAnalysisContext( | 197 static AnalysisContext _createAnalysisContext( |
| 165 AnalysisOptions _analysisOptions, | 198 AnalysisOptions _analysisOptions, |
| 166 DeclaredVariables declaredVariables, | 199 DeclaredVariables declaredVariables, |
| 167 SourceFactory _sourceFactory, | 200 SourceFactory _sourceFactory, |
| 168 FileTracker fileTracker, | 201 FileTracker fileTracker, |
| 169 SummaryDataStore store) { | 202 SummaryDataStore store) { |
| 170 AnalysisContextImpl analysisContext = | 203 AnalysisContextImpl analysisContext = |
| 171 AnalysisEngine.instance.createAnalysisContext(); | 204 AnalysisEngine.instance.createAnalysisContext(); |
| 172 analysisContext.useSdkCachePartition = false; | 205 analysisContext.useSdkCachePartition = false; |
| 173 analysisContext.analysisOptions = _analysisOptions; | 206 analysisContext.analysisOptions = _analysisOptions; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 @override | 240 @override |
| 208 String getContents(Source source) { | 241 String getContents(Source source) { |
| 209 return _getFileForSource(source).content; | 242 return _getFileForSource(source).content; |
| 210 } | 243 } |
| 211 | 244 |
| 212 @override | 245 @override |
| 213 bool getExists(Source source) { | 246 bool getExists(Source source) { |
| 214 if (source.isInSystemLibrary) { | 247 if (source.isInSystemLibrary) { |
| 215 return true; | 248 return true; |
| 216 } | 249 } |
| 250 if (fileTracker.externalSummaries.hasUnlinkedUnit(source.uri.toString())) { |
| 251 return true; |
| 252 } |
| 217 return _getFileForSource(source).exists; | 253 return _getFileForSource(source).exists; |
| 218 } | 254 } |
| 219 | 255 |
| 220 @override | 256 @override |
| 221 int getModificationStamp(Source source) { | 257 int getModificationStamp(Source source) { |
| 222 if (source.isInSystemLibrary) { | 258 if (source.isInSystemLibrary) { |
| 223 return 0; | 259 return 0; |
| 224 } | 260 } |
| 225 return _getFileForSource(source).exists ? 0 : -1; | 261 return _getFileForSource(source).exists ? 0 : -1; |
| 226 } | 262 } |
| 227 | 263 |
| 228 @override | 264 @override |
| 229 String setContents(Source source, String contents) { | 265 String setContents(Source source, String contents) { |
| 230 throw new UnimplementedError(); | 266 throw new UnimplementedError(); |
| 231 } | 267 } |
| 232 | 268 |
| 233 FileState _getFileForSource(Source source) { | 269 FileState _getFileForSource(Source source) { |
| 234 String path = source.fullName; | 270 String path = source.fullName; |
| 235 return fileTracker.fsState.getFileForPath(path); | 271 return fileTracker.fsState.getFileForPath(path); |
| 236 } | 272 } |
| 237 } | 273 } |
| OLD | NEW |