| 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' | 7 import 'package:analyzer/dart/element/element.dart' |
| 8 show CompilationUnitElement, LibraryElement; | 8 show CompilationUnitElement, LibraryElement; |
| 9 import 'package:analyzer/error/error.dart'; | 9 import 'package:analyzer/error/error.dart'; |
| 10 import 'package:analyzer/src/context/context.dart'; | 10 import 'package:analyzer/src/context/context.dart'; |
| 11 import 'package:analyzer/src/dart/analysis/driver.dart'; | 11 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 12 import 'package:analyzer/src/dart/analysis/file_state.dart'; | 12 import 'package:analyzer/src/dart/analysis/file_state.dart'; |
| 13 import 'package:analyzer/src/dart/element/element.dart'; |
| 14 import 'package:analyzer/src/dart/element/handle.dart'; |
| 13 import 'package:analyzer/src/generated/engine.dart' | 15 import 'package:analyzer/src/generated/engine.dart' |
| 14 show AnalysisContext, AnalysisEngine, AnalysisOptions; | 16 show AnalysisContext, AnalysisEngine, AnalysisOptions; |
| 15 import 'package:analyzer/src/generated/source.dart'; | 17 import 'package:analyzer/src/generated/source.dart'; |
| 16 import 'package:analyzer/src/summary/format.dart'; | 18 import 'package:analyzer/src/summary/format.dart'; |
| 17 import 'package:analyzer/src/summary/idl.dart'; | 19 import 'package:analyzer/src/summary/idl.dart'; |
| 18 import 'package:analyzer/src/summary/link.dart'; | 20 import 'package:analyzer/src/summary/link.dart'; |
| 19 import 'package:analyzer/src/summary/package_bundle_reader.dart'; | 21 import 'package:analyzer/src/summary/package_bundle_reader.dart'; |
| 20 import 'package:analyzer/src/task/dart.dart' show COMPILATION_UNIT_ELEMENT; | |
| 21 import 'package:analyzer/task/dart.dart' show LibrarySpecificUnit; | |
| 22 import 'package:front_end/src/base/performace_logger.dart'; | 22 import 'package:front_end/src/base/performace_logger.dart'; |
| 23 import 'package:front_end/src/byte_store/byte_store.dart'; | 23 import 'package:front_end/src/byte_store/byte_store.dart'; |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * Context information necessary to analyze one or more libraries within an | 26 * Context information necessary to analyze one or more libraries within an |
| 27 * [AnalysisDriver]. | 27 * [AnalysisDriver]. |
| 28 * | 28 * |
| 29 * Currently this is implemented as a wrapper around [AnalysisContext]. | 29 * Currently this is implemented as a wrapper around [AnalysisContext]. |
| 30 * TODO(paulberry): make a front end API that this can make use of instead. | 30 * TODO(paulberry): make a front end API that this can make use of instead. |
| 31 */ | 31 */ |
| 32 class LibraryContext { | 32 class LibraryContext { |
| 33 final SummaryDataStore store; | 33 final SummaryDataStore store; |
| 34 | 34 |
| 35 /** | 35 /** |
| 36 * The [AnalysisContext] which is used to do the analysis. | 36 * The [AnalysisContext] which is used to do the analysis. |
| 37 */ | 37 */ |
| 38 final AnalysisContext _analysisContext; | 38 final AnalysisContext _analysisContext; |
| 39 | 39 |
| 40 /** | 40 /** |
| 41 * The resynthesizer that resynthesizes elements in [_analysisContext]. |
| 42 */ |
| 43 final ElementResynthesizer _resynthesizer; |
| 44 |
| 45 /** |
| 41 * Create a [LibraryContext] which is prepared to analyze [targetLibrary]. | 46 * Create a [LibraryContext] which is prepared to analyze [targetLibrary]. |
| 42 */ | 47 */ |
| 43 factory LibraryContext.forSingleLibrary( | 48 factory LibraryContext.forSingleLibrary( |
| 44 FileState targetLibrary, | 49 FileState targetLibrary, |
| 45 PerformanceLog logger, | 50 PerformanceLog logger, |
| 46 PackageBundle sdkBundle, | 51 PackageBundle sdkBundle, |
| 47 ByteStore byteStore, | 52 ByteStore byteStore, |
| 48 AnalysisOptions options, | 53 AnalysisOptions options, |
| 49 DeclaredVariables declaredVariables, | 54 DeclaredVariables declaredVariables, |
| 50 SourceFactory sourceFactory, | 55 SourceFactory sourceFactory, |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 130 |
| 126 linkedLibraries.forEach((uri, linkedBuilder) { | 131 linkedLibraries.forEach((uri, linkedBuilder) { |
| 127 FileState library = libraries[uri]; | 132 FileState library = libraries[uri]; |
| 128 String key = '${library.transitiveSignature}.linked'; | 133 String key = '${library.transitiveSignature}.linked'; |
| 129 List<int> bytes = linkedBuilder.toBuffer(); | 134 List<int> bytes = linkedBuilder.toBuffer(); |
| 130 LinkedLibrary linked = new LinkedLibrary.fromBuffer(bytes); | 135 LinkedLibrary linked = new LinkedLibrary.fromBuffer(bytes); |
| 131 store.addLinkedLibrary(uri, linked); | 136 store.addLinkedLibrary(uri, linked); |
| 132 byteStore.put(key, bytes); | 137 byteStore.put(key, bytes); |
| 133 }); | 138 }); |
| 134 | 139 |
| 135 AnalysisContextImpl analysisContext = _createAnalysisContext( | 140 var resynthesizingContext = _createResynthesizingContext( |
| 136 options, declaredVariables, sourceFactory, store); | 141 options, declaredVariables, sourceFactory, store); |
| 137 analysisContext.contentCache = new _ContentCacheWrapper(fsState); | 142 resynthesizingContext.context.contentCache = |
| 143 new _ContentCacheWrapper(fsState); |
| 138 | 144 |
| 139 return new LibraryContext._(store, analysisContext); | 145 return new LibraryContext._(store, resynthesizingContext.context, |
| 146 resynthesizingContext.resynthesizer); |
| 140 }); | 147 }); |
| 141 } | 148 } |
| 142 | 149 |
| 143 LibraryContext._(this.store, this._analysisContext); | 150 LibraryContext._(this.store, this._analysisContext, this._resynthesizer); |
| 144 | 151 |
| 145 /** | 152 /** |
| 146 * Computes a [CompilationUnitElement] for the given library/unit pair. | 153 * Computes a [CompilationUnitElement] for the given library/unit pair. |
| 147 */ | 154 */ |
| 148 CompilationUnitElement computeUnitElement( | 155 CompilationUnitElement computeUnitElement( |
| 149 Source librarySource, Source unitSource) { | 156 Source librarySource, Source unitSource) { |
| 150 return _analysisContext.computeResult( | 157 String libraryUri = librarySource.uri.toString(); |
| 151 new LibrarySpecificUnit(librarySource, unitSource), | 158 String unitUri = unitSource.uri.toString(); |
| 152 COMPILATION_UNIT_ELEMENT); | 159 return _resynthesizer.getElement( |
| 160 new ElementLocationImpl.con3(<String>[libraryUri, unitUri])); |
| 153 } | 161 } |
| 154 | 162 |
| 155 /** | 163 /** |
| 156 * Cleans up any persistent resources used by this [LibraryContext]. | 164 * Cleans up any persistent resources used by this [LibraryContext]. |
| 157 * | 165 * |
| 158 * Should be called once the [LibraryContext] is no longer needed. | 166 * Should be called once the [LibraryContext] is no longer needed. |
| 159 */ | 167 */ |
| 160 void dispose() { | 168 void dispose() { |
| 161 _analysisContext.dispose(); | 169 _analysisContext.dispose(); |
| 162 } | 170 } |
| 163 | 171 |
| 164 /** | 172 /** |
| 165 * Computes a resolved [CompilationUnit] and a list of [AnalysisError]s for | |
| 166 * the given library/unit pair. | |
| 167 */ | |
| 168 ResolutionResult resolveUnit(Source librarySource, Source unitSource) { | |
| 169 CompilationUnit resolvedUnit = | |
| 170 _analysisContext.resolveCompilationUnit2(unitSource, librarySource); | |
| 171 List<AnalysisError> errors = _analysisContext.computeErrors(unitSource); | |
| 172 return new ResolutionResult(resolvedUnit, errors); | |
| 173 } | |
| 174 | |
| 175 /** | |
| 176 * Resynthesize the [LibraryElement] from the given [store]. | 173 * Resynthesize the [LibraryElement] from the given [store]. |
| 177 */ | 174 */ |
| 178 static LibraryElement resynthesizeLibraryElement( | 175 static LibraryElement resynthesizeLibraryElement( |
| 179 AnalysisOptions analysisOptions, | 176 AnalysisOptions analysisOptions, |
| 180 DeclaredVariables declaredVariables, | 177 DeclaredVariables declaredVariables, |
| 181 SourceFactory sourceFactory, | 178 SourceFactory sourceFactory, |
| 182 SummaryDataStore store, | 179 SummaryDataStore store, |
| 183 String uri) { | 180 String uri) { |
| 184 AnalysisContextImpl analysisContext = _createAnalysisContext( | 181 var resynthesizingContext = _createResynthesizingContext( |
| 185 analysisOptions, declaredVariables, sourceFactory, store); | 182 analysisOptions, declaredVariables, sourceFactory, store); |
| 186 try { | 183 try { |
| 187 return new StoreBasedSummaryResynthesizer( | 184 return resynthesizingContext.resynthesizer |
| 188 analysisContext, sourceFactory, analysisOptions.strongMode, store) | 185 .getElement(new ElementLocationImpl.con3([uri])); |
| 189 .getLibraryElement(uri); | |
| 190 } finally { | 186 } finally { |
| 191 analysisContext.dispose(); | 187 resynthesizingContext.context.dispose(); |
| 192 } | 188 } |
| 193 } | 189 } |
| 194 | 190 |
| 195 static AnalysisContextImpl _createAnalysisContext( | 191 static _ResynthesizingAnalysisContext _createResynthesizingContext( |
| 196 AnalysisOptions analysisOptions, | 192 AnalysisOptions analysisOptions, |
| 197 DeclaredVariables declaredVariables, | 193 DeclaredVariables declaredVariables, |
| 198 SourceFactory sourceFactory, | 194 SourceFactory sourceFactory, |
| 199 SummaryDataStore store) { | 195 SummaryDataStore store) { |
| 200 AnalysisContextImpl analysisContext = | 196 AnalysisContextImpl analysisContext = |
| 201 AnalysisEngine.instance.createAnalysisContext(); | 197 AnalysisEngine.instance.createAnalysisContext(); |
| 202 analysisContext.useSdkCachePartition = false; | 198 analysisContext.useSdkCachePartition = false; |
| 203 analysisContext.analysisOptions = analysisOptions; | 199 analysisContext.analysisOptions = analysisOptions; |
| 204 analysisContext.declaredVariables.addAll(declaredVariables); | 200 analysisContext.declaredVariables.addAll(declaredVariables); |
| 205 analysisContext.sourceFactory = sourceFactory.clone(); | 201 analysisContext.sourceFactory = sourceFactory.clone(); |
| 206 analysisContext.resultProvider = | 202 var provider = new InputPackagesResultProvider(analysisContext, store); |
| 207 new InputPackagesResultProvider(analysisContext, store); | 203 analysisContext.resultProvider = provider; |
| 208 return analysisContext; | 204 return new _ResynthesizingAnalysisContext( |
| 205 analysisContext, provider.resynthesizer); |
| 209 } | 206 } |
| 210 } | 207 } |
| 211 | 208 |
| 212 /** | 209 /** |
| 213 * Container object holding the result of a call to | 210 * Container object holding the result of a call to |
| 214 * [LibraryContext.resolveUnit]. | 211 * [LibraryContext.resolveUnit]. |
| 215 */ | 212 */ |
| 216 class ResolutionResult { | 213 class ResolutionResult { |
| 217 final CompilationUnit resolvedUnit; | 214 final CompilationUnit resolvedUnit; |
| 218 final List<AnalysisError> errors; | 215 final List<AnalysisError> errors; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 @override | 258 @override |
| 262 String setContents(Source source, String contents) { | 259 String setContents(Source source, String contents) { |
| 263 throw new UnimplementedError(); | 260 throw new UnimplementedError(); |
| 264 } | 261 } |
| 265 | 262 |
| 266 FileState _getFileForSource(Source source) { | 263 FileState _getFileForSource(Source source) { |
| 267 String path = source.fullName; | 264 String path = source.fullName; |
| 268 return fsState.getFileForPath(path); | 265 return fsState.getFileForPath(path); |
| 269 } | 266 } |
| 270 } | 267 } |
| 268 |
| 269 /** |
| 270 * Container with analysis context and the corresponding resynthesizer. |
| 271 */ |
| 272 class _ResynthesizingAnalysisContext { |
| 273 final AnalysisContextImpl context; |
| 274 final ElementResynthesizer resynthesizer; |
| 275 |
| 276 _ResynthesizingAnalysisContext(this.context, this.resynthesizer); |
| 277 } |
| OLD | NEW |