| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library analyzer.src.summary.summary_sdk; |
| 6 |
| 7 import 'package:analyzer/dart/element/element.dart'; |
| 8 import 'package:analyzer/dart/element/type.dart'; |
| 9 import 'package:analyzer/file_system/file_system.dart' show ResourceProvider; |
| 10 import 'package:analyzer/src/context/cache.dart' show CacheEntry; |
| 11 import 'package:analyzer/src/context/context.dart'; |
| 12 import 'package:analyzer/src/dart/element/type.dart'; |
| 13 import 'package:analyzer/src/generated/constant.dart'; |
| 14 import 'package:analyzer/src/generated/engine.dart'; |
| 15 import 'package:analyzer/src/generated/resolver.dart'; |
| 16 import 'package:analyzer/src/generated/sdk.dart'; |
| 17 import 'package:analyzer/src/generated/source.dart' |
| 18 show DartUriResolver, Source, SourceFactory; |
| 19 import 'package:analyzer/src/summary/idl.dart'; |
| 20 import 'package:analyzer/src/summary/package_bundle_reader.dart'; |
| 21 import 'package:analyzer/src/summary/resynthesize.dart'; |
| 22 import 'package:analyzer/src/task/dart.dart'; |
| 23 import 'package:analyzer/task/model.dart' show ResultDescriptor, TargetedResult; |
| 24 |
| 25 class SdkSummaryResultProvider extends ResynthesizerResultProvider { |
| 26 final SummaryTypeProvider typeProvider = new SummaryTypeProvider(); |
| 27 |
| 28 SdkSummaryResultProvider( |
| 29 InternalAnalysisContext context, PackageBundle bundle, bool strongMode) |
| 30 : super(context, new SummaryDataStore(const <String>[])) { |
| 31 addBundle(null, bundle); |
| 32 createResynthesizer(null, typeProvider); |
| 33 _buildCoreLibrary(); |
| 34 _buildAsyncLibrary(); |
| 35 resynthesizer.finalizeCoreAsyncLibraries(); |
| 36 context.typeProvider = typeProvider; |
| 37 } |
| 38 |
| 39 @override |
| 40 bool compute(CacheEntry entry, ResultDescriptor result) { |
| 41 if (result == TYPE_PROVIDER) { |
| 42 entry.setValue(result as ResultDescriptor<TypeProvider>, typeProvider, |
| 43 TargetedResult.EMPTY_LIST); |
| 44 return true; |
| 45 } |
| 46 return super.compute(entry, result); |
| 47 } |
| 48 |
| 49 @override |
| 50 bool hasResultsForSource(Source source) { |
| 51 return source.source != null && source.isInSystemLibrary; |
| 52 } |
| 53 |
| 54 void _buildAsyncLibrary() { |
| 55 LibraryElement library = resynthesizer.getLibraryElement('dart:async'); |
| 56 typeProvider.initializeAsync(library); |
| 57 } |
| 58 |
| 59 void _buildCoreLibrary() { |
| 60 LibraryElement library = resynthesizer.getLibraryElement('dart:core'); |
| 61 typeProvider.initializeCore(library); |
| 62 } |
| 63 } |
| 64 |
| 65 /** |
| 66 * The implementation of [SummaryResynthesizer] for Dart SDK. |
| 67 */ |
| 68 class SdkSummaryResynthesizer extends SummaryResynthesizer { |
| 69 final PackageBundle bundle; |
| 70 final Map<String, UnlinkedUnit> unlinkedSummaries = <String, UnlinkedUnit>{}; |
| 71 final Map<String, LinkedLibrary> linkedSummaries = <String, LinkedLibrary>{}; |
| 72 |
| 73 SdkSummaryResynthesizer(AnalysisContext context, TypeProvider typeProvider, |
| 74 SourceFactory sourceFactory, this.bundle, bool strongMode) |
| 75 : super(null, context, typeProvider, sourceFactory, strongMode) { |
| 76 for (int i = 0; i < bundle.unlinkedUnitUris.length; i++) { |
| 77 unlinkedSummaries[bundle.unlinkedUnitUris[i]] = bundle.unlinkedUnits[i]; |
| 78 } |
| 79 for (int i = 0; i < bundle.linkedLibraryUris.length; i++) { |
| 80 linkedSummaries[bundle.linkedLibraryUris[i]] = bundle.linkedLibraries[i]; |
| 81 } |
| 82 } |
| 83 |
| 84 @override |
| 85 LinkedLibrary getLinkedSummary(String uri) { |
| 86 return linkedSummaries[uri]; |
| 87 } |
| 88 |
| 89 @override |
| 90 UnlinkedUnit getUnlinkedSummary(String uri) { |
| 91 return unlinkedSummaries[uri]; |
| 92 } |
| 93 |
| 94 @override |
| 95 bool hasLibrarySummary(String uri) { |
| 96 return uri.startsWith('dart:'); |
| 97 } |
| 98 } |
| 99 |
| 100 /** |
| 101 * An implementation of [DartSdk] which provides analysis results for `dart:` |
| 102 * libraries from the given summary file. This implementation is limited and |
| 103 * suitable only for command-line tools, but not for IDEs - it does not |
| 104 * implement [sdkLibraries], [sdkVersion], [uris] and [fromFileUri]. |
| 105 */ |
| 106 class SummaryBasedDartSdk implements DartSdk { |
| 107 final bool strongMode; |
| 108 SummaryDataStore _dataStore; |
| 109 InSummaryUriResolver _uriResolver; |
| 110 PackageBundle _bundle; |
| 111 ResourceProvider resourceProvider; |
| 112 |
| 113 /** |
| 114 * The [AnalysisContext] which is used for all of the sources in this sdk. |
| 115 */ |
| 116 InternalAnalysisContext _analysisContext; |
| 117 |
| 118 SummaryBasedDartSdk(String summaryPath, this.strongMode) { |
| 119 _dataStore = new SummaryDataStore(<String>[summaryPath]); |
| 120 _uriResolver = new InSummaryUriResolver(resourceProvider, _dataStore); |
| 121 _bundle = _dataStore.bundles.single; |
| 122 } |
| 123 |
| 124 SummaryBasedDartSdk.fromBundle( |
| 125 this.strongMode, PackageBundle bundle, this.resourceProvider) { |
| 126 _dataStore = new SummaryDataStore([]); |
| 127 _dataStore.addBundle('dart_sdk.sum', bundle); |
| 128 _uriResolver = new InSummaryUriResolver(resourceProvider, _dataStore); |
| 129 _bundle = bundle; |
| 130 } |
| 131 |
| 132 /** |
| 133 * Return the [PackageBundle] for this SDK, not `null`. |
| 134 */ |
| 135 PackageBundle get bundle => _bundle; |
| 136 |
| 137 @override |
| 138 AnalysisContext get context { |
| 139 if (_analysisContext == null) { |
| 140 AnalysisOptionsImpl analysisOptions = new AnalysisOptionsImpl() |
| 141 ..strongMode = strongMode; |
| 142 _analysisContext = new SdkAnalysisContext(analysisOptions); |
| 143 SourceFactory factory = new SourceFactory( |
| 144 [new DartUriResolver(this)], null, resourceProvider); |
| 145 _analysisContext.sourceFactory = factory; |
| 146 _analysisContext.resultProvider = |
| 147 new SdkSummaryResultProvider(_analysisContext, _bundle, strongMode); |
| 148 } |
| 149 return _analysisContext; |
| 150 } |
| 151 |
| 152 @override |
| 153 List<SdkLibrary> get sdkLibraries { |
| 154 throw new UnimplementedError(); |
| 155 } |
| 156 |
| 157 @override |
| 158 String get sdkVersion { |
| 159 throw new UnimplementedError(); |
| 160 } |
| 161 |
| 162 @override |
| 163 List<String> get uris { |
| 164 throw new UnimplementedError(); |
| 165 } |
| 166 |
| 167 @override |
| 168 Source fromFileUri(Uri uri) { |
| 169 return null; |
| 170 } |
| 171 |
| 172 @override |
| 173 PackageBundle getLinkedBundle() => _bundle; |
| 174 |
| 175 @override |
| 176 SdkLibrary getSdkLibrary(String uri) { |
| 177 // This is not quite correct, but currently it's used only in |
| 178 // to report errors on importing or exporting of internal libraries. |
| 179 return null; |
| 180 } |
| 181 |
| 182 @override |
| 183 Source mapDartUri(String uriStr) { |
| 184 Uri uri = Uri.parse(uriStr); |
| 185 return _uriResolver.resolveAbsolute(uri); |
| 186 } |
| 187 } |
| 188 |
| 189 /** |
| 190 * Implementation of [TypeProvider] which can be initialized separately with |
| 191 * `dart:core` and `dart:async` libraries. |
| 192 */ |
| 193 class SummaryTypeProvider extends TypeProviderBase { |
| 194 LibraryElement _coreLibrary; |
| 195 LibraryElement _asyncLibrary; |
| 196 |
| 197 InterfaceType _boolType; |
| 198 InterfaceType _deprecatedType; |
| 199 InterfaceType _doubleType; |
| 200 InterfaceType _functionType; |
| 201 InterfaceType _futureDynamicType; |
| 202 InterfaceType _futureNullType; |
| 203 InterfaceType _futureType; |
| 204 InterfaceType _intType; |
| 205 InterfaceType _iterableDynamicType; |
| 206 InterfaceType _iterableType; |
| 207 InterfaceType _listType; |
| 208 InterfaceType _mapType; |
| 209 DartObjectImpl _nullObject; |
| 210 InterfaceType _nullType; |
| 211 InterfaceType _numType; |
| 212 InterfaceType _objectType; |
| 213 InterfaceType _stackTraceType; |
| 214 InterfaceType _streamDynamicType; |
| 215 InterfaceType _streamType; |
| 216 InterfaceType _stringType; |
| 217 InterfaceType _symbolType; |
| 218 InterfaceType _typeType; |
| 219 |
| 220 @override |
| 221 InterfaceType get boolType { |
| 222 assert(_coreLibrary != null); |
| 223 _boolType ??= _getType(_coreLibrary, "bool"); |
| 224 return _boolType; |
| 225 } |
| 226 |
| 227 @override |
| 228 DartType get bottomType => BottomTypeImpl.instance; |
| 229 |
| 230 @override |
| 231 InterfaceType get deprecatedType { |
| 232 assert(_coreLibrary != null); |
| 233 _deprecatedType ??= _getType(_coreLibrary, "Deprecated"); |
| 234 return _deprecatedType; |
| 235 } |
| 236 |
| 237 @override |
| 238 InterfaceType get doubleType { |
| 239 assert(_coreLibrary != null); |
| 240 _doubleType ??= _getType(_coreLibrary, "double"); |
| 241 return _doubleType; |
| 242 } |
| 243 |
| 244 @override |
| 245 DartType get dynamicType => DynamicTypeImpl.instance; |
| 246 |
| 247 @override |
| 248 InterfaceType get functionType { |
| 249 assert(_coreLibrary != null); |
| 250 _functionType ??= _getType(_coreLibrary, "Function"); |
| 251 return _functionType; |
| 252 } |
| 253 |
| 254 @override |
| 255 InterfaceType get futureDynamicType { |
| 256 assert(_asyncLibrary != null); |
| 257 _futureDynamicType ??= futureType.instantiate(<DartType>[dynamicType]); |
| 258 return _futureDynamicType; |
| 259 } |
| 260 |
| 261 @override |
| 262 InterfaceType get futureNullType { |
| 263 assert(_asyncLibrary != null); |
| 264 _futureNullType ??= futureType.instantiate(<DartType>[nullType]); |
| 265 return _futureNullType; |
| 266 } |
| 267 |
| 268 @override |
| 269 InterfaceType get futureType { |
| 270 assert(_asyncLibrary != null); |
| 271 _futureType ??= _getType(_asyncLibrary, "Future"); |
| 272 return _futureType; |
| 273 } |
| 274 |
| 275 @override |
| 276 InterfaceType get intType { |
| 277 assert(_coreLibrary != null); |
| 278 _intType ??= _getType(_coreLibrary, "int"); |
| 279 return _intType; |
| 280 } |
| 281 |
| 282 @override |
| 283 InterfaceType get iterableDynamicType { |
| 284 assert(_coreLibrary != null); |
| 285 _iterableDynamicType ??= iterableType.instantiate(<DartType>[dynamicType]); |
| 286 return _iterableDynamicType; |
| 287 } |
| 288 |
| 289 @override |
| 290 InterfaceType get iterableType { |
| 291 assert(_coreLibrary != null); |
| 292 _iterableType ??= _getType(_coreLibrary, "Iterable"); |
| 293 return _iterableType; |
| 294 } |
| 295 |
| 296 @override |
| 297 InterfaceType get listType { |
| 298 assert(_coreLibrary != null); |
| 299 _listType ??= _getType(_coreLibrary, "List"); |
| 300 return _listType; |
| 301 } |
| 302 |
| 303 @override |
| 304 InterfaceType get mapType { |
| 305 assert(_coreLibrary != null); |
| 306 _mapType ??= _getType(_coreLibrary, "Map"); |
| 307 return _mapType; |
| 308 } |
| 309 |
| 310 @override |
| 311 DartObjectImpl get nullObject { |
| 312 if (_nullObject == null) { |
| 313 _nullObject = new DartObjectImpl(nullType, NullState.NULL_STATE); |
| 314 } |
| 315 return _nullObject; |
| 316 } |
| 317 |
| 318 @override |
| 319 InterfaceType get nullType { |
| 320 assert(_coreLibrary != null); |
| 321 _nullType ??= _getType(_coreLibrary, "Null"); |
| 322 return _nullType; |
| 323 } |
| 324 |
| 325 @override |
| 326 InterfaceType get numType { |
| 327 assert(_coreLibrary != null); |
| 328 _numType ??= _getType(_coreLibrary, "num"); |
| 329 return _numType; |
| 330 } |
| 331 |
| 332 @override |
| 333 InterfaceType get objectType { |
| 334 assert(_coreLibrary != null); |
| 335 _objectType ??= _getType(_coreLibrary, "Object"); |
| 336 return _objectType; |
| 337 } |
| 338 |
| 339 @override |
| 340 InterfaceType get stackTraceType { |
| 341 assert(_coreLibrary != null); |
| 342 _stackTraceType ??= _getType(_coreLibrary, "StackTrace"); |
| 343 return _stackTraceType; |
| 344 } |
| 345 |
| 346 @override |
| 347 InterfaceType get streamDynamicType { |
| 348 assert(_asyncLibrary != null); |
| 349 _streamDynamicType ??= streamType.instantiate(<DartType>[dynamicType]); |
| 350 return _streamDynamicType; |
| 351 } |
| 352 |
| 353 @override |
| 354 InterfaceType get streamType { |
| 355 assert(_asyncLibrary != null); |
| 356 _streamType ??= _getType(_asyncLibrary, "Stream"); |
| 357 return _streamType; |
| 358 } |
| 359 |
| 360 @override |
| 361 InterfaceType get stringType { |
| 362 assert(_coreLibrary != null); |
| 363 _stringType ??= _getType(_coreLibrary, "String"); |
| 364 return _stringType; |
| 365 } |
| 366 |
| 367 @override |
| 368 InterfaceType get symbolType { |
| 369 assert(_coreLibrary != null); |
| 370 _symbolType ??= _getType(_coreLibrary, "Symbol"); |
| 371 return _symbolType; |
| 372 } |
| 373 |
| 374 @override |
| 375 InterfaceType get typeType { |
| 376 assert(_coreLibrary != null); |
| 377 _typeType ??= _getType(_coreLibrary, "Type"); |
| 378 return _typeType; |
| 379 } |
| 380 |
| 381 @override |
| 382 DartType get undefinedType => UndefinedTypeImpl.instance; |
| 383 |
| 384 /** |
| 385 * Initialize the `dart:async` types provided by this type provider. |
| 386 */ |
| 387 void initializeAsync(LibraryElement library) { |
| 388 assert(_coreLibrary != null); |
| 389 assert(_asyncLibrary == null); |
| 390 _asyncLibrary = library; |
| 391 } |
| 392 |
| 393 /** |
| 394 * Initialize the `dart:core` types provided by this type provider. |
| 395 */ |
| 396 void initializeCore(LibraryElement library) { |
| 397 assert(_coreLibrary == null); |
| 398 assert(_asyncLibrary == null); |
| 399 _coreLibrary = library; |
| 400 } |
| 401 |
| 402 /** |
| 403 * Return the type with the given [name] from the given [library], or |
| 404 * throw a [StateError] if there is no class with the given name. |
| 405 */ |
| 406 InterfaceType _getType(LibraryElement library, String name) { |
| 407 Element element = library.getType(name); |
| 408 if (element == null) { |
| 409 throw new StateError("No definition of type $name"); |
| 410 } |
| 411 return (element as ClassElement).type; |
| 412 } |
| 413 } |
| OLD | NEW |