| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 dart2js.library_loader; | 5 library dart2js.library_loader; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'common/names.dart' show Uris; | 9 import 'common/names.dart' show Uris; |
| 10 import 'common/tasks.dart' show CompilerTask, Measurer; | 10 import 'common/tasks.dart' show CompilerTask, Measurer; |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 * that these imports | 134 * that these imports |
| 135 * | 135 * |
| 136 * import 'package:foo.dart' as a; | 136 * import 'package:foo.dart' as a; |
| 137 * import 'packages/foo.dart' as b; | 137 * import 'packages/foo.dart' as b; |
| 138 * | 138 * |
| 139 * do _not_ resolve to the same library when the package root URI happens to | 139 * do _not_ resolve to the same library when the package root URI happens to |
| 140 * point to the 'packages' folder. | 140 * point to the 'packages' folder. |
| 141 * | 141 * |
| 142 */ | 142 */ |
| 143 abstract class LibraryLoaderTask implements LibraryProvider, CompilerTask { | 143 abstract class LibraryLoaderTask implements LibraryProvider, CompilerTask { |
| 144 factory LibraryLoaderTask( | |
| 145 bool loadFromDillFile, | |
| 146 ResolvedUriTranslator uriTranslator, | |
| 147 ScriptLoader scriptLoader, | |
| 148 ElementScanner scriptScanner, | |
| 149 LibraryDeserializer deserializer, | |
| 150 PatchResolverFunction patchResolverFunc, | |
| 151 PatchParserTask patchParser, | |
| 152 Environment environment, | |
| 153 DiagnosticReporter reporter, | |
| 154 Measurer measurer) => | |
| 155 loadFromDillFile | |
| 156 ? new _DillLibraryLoaderTask( | |
| 157 uriTranslator, scriptLoader, reporter, measurer) | |
| 158 : new _LibraryLoaderTask( | |
| 159 uriTranslator, | |
| 160 scriptLoader, | |
| 161 scriptScanner, | |
| 162 deserializer, | |
| 163 patchResolverFunc, | |
| 164 patchParser, | |
| 165 environment, | |
| 166 reporter, | |
| 167 measurer); | |
| 168 | |
| 169 /// Returns all libraries that have been loaded. | 144 /// Returns all libraries that have been loaded. |
| 170 Iterable<LibraryEntity> get libraries; | 145 Iterable<LibraryEntity> get libraries; |
| 171 | 146 |
| 172 /// Loads the library specified by the [resolvedUri] and returns the | 147 /// Loads the library specified by the [resolvedUri] and returns the |
| 173 /// [LoadedLibraries] that were loaded to load the specified uri. The | 148 /// [LoadedLibraries] that were loaded to load the specified uri. The |
| 174 /// [LibraryElement] itself can be found by calling | 149 /// [LibraryElement] itself can be found by calling |
| 175 /// `loadedLibraries.rootLibrary`. | 150 /// `loadedLibraries.rootLibrary`. |
| 176 /// | 151 /// |
| 177 /// If the library is not already loaded, the method creates the | 152 /// If the library is not already loaded, the method creates the |
| 178 /// [LibraryElement] for the library and computes the import/export scope, | 153 /// [LibraryElement] for the library and computes the import/export scope, |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 * A list of combinators represented as a list of element names to exclude. | 302 * A list of combinators represented as a list of element names to exclude. |
| 328 */ | 303 */ |
| 329 class HideFilter extends CombinatorFilter { | 304 class HideFilter extends CombinatorFilter { |
| 330 final Set<String> excludedNames; | 305 final Set<String> excludedNames; |
| 331 | 306 |
| 332 HideFilter(this.excludedNames); | 307 HideFilter(this.excludedNames); |
| 333 | 308 |
| 334 bool exclude(Element element) => excludedNames.contains(element.name); | 309 bool exclude(Element element) => excludedNames.contains(element.name); |
| 335 } | 310 } |
| 336 | 311 |
| 337 /// Implementation class for [LibraryLoaderTask]. The distinction between | 312 /// Implementation class for [LibraryLoaderTask]. This library loader loads |
| 338 /// [LibraryLoaderTask] and [_LibraryLoaderTask] is made to hide internal | 313 /// '.dart' files into the [Element] model with AST nodes which are resolved |
| 339 /// members from the [LibraryLoaderTask] interface. | 314 /// by the resolver. |
| 340 class _LibraryLoaderTask extends CompilerTask implements LibraryLoaderTask { | 315 class ResolutionLibraryLoaderTask extends CompilerTask |
| 316 implements LibraryLoaderTask { |
| 341 /// Translates internal uris (like dart:core) to a disk location. | 317 /// Translates internal uris (like dart:core) to a disk location. |
| 342 final ResolvedUriTranslator uriTranslator; | 318 final ResolvedUriTranslator uriTranslator; |
| 343 | 319 |
| 344 /// Loads the contents of a script file (a .dart file). Used when loading | 320 /// Loads the contents of a script file (a .dart file). Used when loading |
| 345 /// libraries from source. | 321 /// libraries from source. |
| 346 final ScriptLoader scriptLoader; | 322 final ScriptLoader scriptLoader; |
| 347 | 323 |
| 348 /// Provides a diet element model from a script file containing information | 324 /// Provides a diet element model from a script file containing information |
| 349 /// about imports and exports. Used when loading libraries from source. | 325 /// about imports and exports. Used when loading libraries from source. |
| 350 final ElementScanner scanner; | 326 final ElementScanner scanner; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 363 /// Function that accepts the string name of a library and returns the | 339 /// Function that accepts the string name of a library and returns the |
| 364 /// path to the corresponding patch file. This is a function that is passed in | 340 /// path to the corresponding patch file. This is a function that is passed in |
| 365 /// because our test mock_compiler subclasses compiler. | 341 /// because our test mock_compiler subclasses compiler. |
| 366 // TODO(efortuna): Refactor mock_compiler to not do this. | 342 // TODO(efortuna): Refactor mock_compiler to not do this. |
| 367 final PatchResolverFunction _patchResolverFunc; | 343 final PatchResolverFunction _patchResolverFunc; |
| 368 | 344 |
| 369 List<DeferredAction> _deferredActions = <DeferredAction>[]; | 345 List<DeferredAction> _deferredActions = <DeferredAction>[]; |
| 370 | 346 |
| 371 final DiagnosticReporter reporter; | 347 final DiagnosticReporter reporter; |
| 372 | 348 |
| 373 _LibraryLoaderTask( | 349 ResolutionLibraryLoaderTask( |
| 374 this.uriTranslator, | 350 this.uriTranslator, |
| 375 this.scriptLoader, | 351 this.scriptLoader, |
| 376 this.scanner, | 352 this.scanner, |
| 377 this.deserializer, | 353 this.deserializer, |
| 378 this._patchResolverFunc, | 354 this._patchResolverFunc, |
| 379 this._patchParserTask, | 355 this._patchParserTask, |
| 380 this.environment, | 356 this.environment, |
| 381 this.reporter, | 357 this.reporter, |
| 382 Measurer measurer) | 358 Measurer measurer) |
| 383 : super(measurer); | 359 : super(measurer); |
| (...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 826 Iterable<DeferredAction> pullDeferredActions() { | 802 Iterable<DeferredAction> pullDeferredActions() { |
| 827 Iterable<DeferredAction> actions = _deferredActions.toList(); | 803 Iterable<DeferredAction> actions = _deferredActions.toList(); |
| 828 _deferredActions.clear(); | 804 _deferredActions.clear(); |
| 829 return actions; | 805 return actions; |
| 830 } | 806 } |
| 831 } | 807 } |
| 832 | 808 |
| 833 /// A task for loading a pre-processed .dill file into memory rather than | 809 /// A task for loading a pre-processed .dill file into memory rather than |
| 834 /// parsing Dart source. Use of this task only makes sense when used in | 810 /// parsing Dart source. Use of this task only makes sense when used in |
| 835 /// conjunction with --use-kernel. | 811 /// conjunction with --use-kernel. |
| 836 class _DillLibraryLoaderTask extends CompilerTask implements LibraryLoaderTask { | 812 class DillLibraryLoaderTask extends CompilerTask implements LibraryLoaderTask { |
| 837 final DiagnosticReporter reporter; | 813 final DiagnosticReporter reporter; |
| 838 | 814 |
| 839 final ResolvedUriTranslator uriTranslator; | 815 final ResolvedUriTranslator uriTranslator; |
| 840 | 816 |
| 841 /// Loads the contents of a script file (a .dart file). Used when loading | 817 /// Loads the contents of a script file (a .dart file). Used when loading |
| 842 /// libraries from source. | 818 /// libraries from source. |
| 843 final ScriptLoader scriptLoader; | 819 final ScriptLoader scriptLoader; |
| 844 | 820 |
| 845 /// Holds the mapping of Kernel IR to KElements that is constructed as a | 821 /// Holds the mapping of Kernel IR to KElements that is constructed as a |
| 846 /// result of loading a program. | 822 /// result of loading a program. |
| 847 KernelWorldBuilder _worldBuilder; | 823 final KernelWorldBuilder _worldBuilder; |
| 848 | 824 |
| 849 List<LibraryEntity> _allLoadedLibraries; | 825 List<LibraryEntity> _allLoadedLibraries; |
| 850 | 826 |
| 851 _DillLibraryLoaderTask( | 827 DillLibraryLoaderTask(this._worldBuilder, this.uriTranslator, |
| 852 this.uriTranslator, this.scriptLoader, this.reporter, Measurer measurer) | 828 this.scriptLoader, this.reporter, Measurer measurer) |
| 853 : _allLoadedLibraries = new List<LibraryEntity>(), | 829 : _allLoadedLibraries = new List<LibraryEntity>(), |
| 854 super(measurer); | 830 super(measurer); |
| 855 | 831 |
| 856 /// Loads an entire Kernel [Program] from a file on disk (note, not just a | 832 /// Loads an entire Kernel [Program] from a file on disk (note, not just a |
| 857 /// library, so this name is actuall a bit of a misnomer). | 833 /// library, so this name is actuall a bit of a misnomer). |
| 858 // TODO(efortuna): Rename this once the Element library loader class goes | 834 // TODO(efortuna): Rename this once the Element library loader class goes |
| 859 // away. | 835 // away. |
| 860 Future<LoadedLibraries> loadLibrary(Uri resolvedUri, | 836 Future<LoadedLibraries> loadLibrary(Uri resolvedUri, |
| 861 {bool skipFileWithPartOfTag: false}) { | 837 {bool skipFileWithPartOfTag: false}) { |
| 862 assert(resolvedUri.pathSegments.last.endsWith('.dill')); | 838 assert(resolvedUri.pathSegments.last.endsWith('.dill')); |
| 863 Uri readableUri = uriTranslator.translate(null, resolvedUri, null); | 839 Uri readableUri = uriTranslator.translate(null, resolvedUri, null); |
| 864 return measure(() async { | 840 return measure(() async { |
| 865 Script script = await scriptLoader.readScript(readableUri, null); | 841 Script script = await scriptLoader.readScript(readableUri, null); |
| 866 ir.Program program = new ir.Program(); | 842 ir.Program program = new ir.Program(); |
| 867 // Hack because the existing file has a terminating 0 and the | 843 // Hack because the existing file has a terminating 0 and the |
| 868 // BinaryBuilder doesn't expect that. | 844 // BinaryBuilder doesn't expect that. |
| 869 var bytes = new List<int>.from(script.file.slowUtf8ZeroTerminatedBytes()); | 845 var bytes = new List<int>.from(script.file.slowUtf8ZeroTerminatedBytes()); |
| 870 bytes.removeLast(); | 846 bytes.removeLast(); |
| 871 new BinaryBuilder(bytes).readProgram(program); | 847 new BinaryBuilder(bytes).readProgram(program); |
| 872 return measure(() { | 848 return measure(() { |
| 873 _worldBuilder = new KernelWorldBuilder(reporter, program); | 849 _worldBuilder.addProgram(program); |
| 874 program.libraries.forEach((ir.Library library) => _allLoadedLibraries | 850 program.libraries.forEach((ir.Library library) => _allLoadedLibraries |
| 875 .add(_worldBuilder.lookupLibrary(library.importUri))); | 851 .add(_worldBuilder.lookupLibrary(library.importUri))); |
| 876 LibraryEntity rootLibrary = null; | 852 LibraryEntity rootLibrary = null; |
| 877 if (program.mainMethod != null) { | 853 if (program.mainMethod != null) { |
| 878 rootLibrary = _worldBuilder | 854 rootLibrary = _worldBuilder |
| 879 .lookupLibrary(program.mainMethod.enclosingLibrary.importUri); | 855 .lookupLibrary(program.mainMethod.enclosingLibrary.importUri); |
| 880 } | 856 } |
| 881 return new _LoadedLibrariesAdapter( | 857 return new _LoadedLibrariesAdapter( |
| 882 rootLibrary, _allLoadedLibraries, _worldBuilder); | 858 rootLibrary, _allLoadedLibraries, _worldBuilder); |
| 883 }); | 859 }); |
| 884 }); | 860 }); |
| 885 } | 861 } |
| 886 | 862 |
| 887 KernelWorldBuilder get worldBuilder => _worldBuilder; | 863 KernelWorldBuilder get worldBuilder => _worldBuilder; |
| 888 | 864 |
| 889 void reset({bool reuseLibrary(LibraryElement library)}) { | 865 void reset({bool reuseLibrary(LibraryElement library)}) { |
| 890 throw new UnimplementedError('_DillLibraryLoaderTask.reset'); | 866 throw new UnimplementedError('DillLibraryLoaderTask.reset'); |
| 891 } | 867 } |
| 892 | 868 |
| 893 Future resetAsync(Future<bool> reuseLibrary(LibraryElement library)) { | 869 Future resetAsync(Future<bool> reuseLibrary(LibraryElement library)) { |
| 894 throw new UnimplementedError('_DillLibraryLoaderTask.resetAsync'); | 870 throw new UnimplementedError('DillLibraryLoaderTask.resetAsync'); |
| 895 } | 871 } |
| 896 | 872 |
| 897 Iterable<LibraryEntity> get libraries => _allLoadedLibraries; | 873 Iterable<LibraryEntity> get libraries => _allLoadedLibraries; |
| 898 | 874 |
| 899 LibraryEntity lookupLibrary(Uri canonicalUri) { | 875 LibraryEntity lookupLibrary(Uri canonicalUri) { |
| 900 return _worldBuilder?.lookupLibrary(canonicalUri); | 876 return _worldBuilder?.lookupLibrary(canonicalUri); |
| 901 } | 877 } |
| 902 | 878 |
| 903 Future<Null> resetLibraries(ReuseLibrariesFunction reuseLibraries) { | 879 Future<Null> resetLibraries(ReuseLibrariesFunction reuseLibraries) { |
| 904 throw new UnimplementedError('_DillLibraryLoaderTask.reuseLibraries'); | 880 throw new UnimplementedError('DillLibraryLoaderTask.reuseLibraries'); |
| 905 } | 881 } |
| 906 | 882 |
| 907 void registerDeferredAction(DeferredAction action) { | 883 void registerDeferredAction(DeferredAction action) { |
| 908 throw new UnimplementedError( | 884 throw new UnimplementedError( |
| 909 '_DillLibraryLoaderTask.registerDeferredAction'); | 885 'DillLibraryLoaderTask.registerDeferredAction'); |
| 910 } | 886 } |
| 911 | 887 |
| 912 Iterable<DeferredAction> pullDeferredActions() { | 888 Iterable<DeferredAction> pullDeferredActions() { |
| 913 throw new UnimplementedError('_DillLibraryLoaderTask.pullDeferredActions'); | 889 throw new UnimplementedError('DillLibraryLoaderTask.pullDeferredActions'); |
| 914 } | 890 } |
| 915 } | 891 } |
| 916 | 892 |
| 917 /// A state machine for checking script tags come in the correct order. | 893 /// A state machine for checking script tags come in the correct order. |
| 918 class TagState { | 894 class TagState { |
| 919 /// Initial state. | 895 /// Initial state. |
| 920 static const int NO_TAG_SEEN = 0; | 896 static const int NO_TAG_SEEN = 0; |
| 921 | 897 |
| 922 /// Passed to [checkTag] when a library declaration (the syntax "library | 898 /// Passed to [checkTag] when a library declaration (the syntax "library |
| 923 /// name;") has been seen. Not an actual state. | 899 /// name;") has been seen. Not an actual state. |
| (...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1347 | 1323 |
| 1348 /** | 1324 /** |
| 1349 * Helper class used for computing the possibly cyclic import/export scopes of | 1325 * Helper class used for computing the possibly cyclic import/export scopes of |
| 1350 * a set of libraries. | 1326 * a set of libraries. |
| 1351 * | 1327 * |
| 1352 * This class is used by [ScannerTask.scanLibrary] to collect all newly loaded | 1328 * This class is used by [ScannerTask.scanLibrary] to collect all newly loaded |
| 1353 * libraries and to compute their import/export scopes through a fixed-point | 1329 * libraries and to compute their import/export scopes through a fixed-point |
| 1354 * algorithm. | 1330 * algorithm. |
| 1355 */ | 1331 */ |
| 1356 class LibraryDependencyHandler implements LibraryLoader { | 1332 class LibraryDependencyHandler implements LibraryLoader { |
| 1357 final _LibraryLoaderTask task; | 1333 final ResolutionLibraryLoaderTask task; |
| 1358 final List<LibraryElement> _newLibraries = <LibraryElement>[]; | 1334 final List<LibraryElement> _newLibraries = <LibraryElement>[]; |
| 1359 | 1335 |
| 1360 /** | 1336 /** |
| 1361 * Newly loaded libraries and their corresponding node in the library | 1337 * Newly loaded libraries and their corresponding node in the library |
| 1362 * dependency graph. Libraries that have already been fully loaded are not | 1338 * dependency graph. Libraries that have already been fully loaded are not |
| 1363 * part of the dependency graph of this handler since their export scopes have | 1339 * part of the dependency graph of this handler since their export scopes have |
| 1364 * already been computed. | 1340 * already been computed. |
| 1365 */ | 1341 */ |
| 1366 Map<LibraryElement, LibraryDependencyNode> nodeMap = | 1342 Map<LibraryElement, LibraryDependencyNode> nodeMap = |
| 1367 new Map<LibraryElement, LibraryDependencyNode>(); | 1343 new Map<LibraryElement, LibraryDependencyNode>(); |
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1678 } | 1654 } |
| 1679 | 1655 |
| 1680 /// API used by the library loader to synchronously scan a library or | 1656 /// API used by the library loader to synchronously scan a library or |
| 1681 /// compilation unit and ensure that their library tags are computed. | 1657 /// compilation unit and ensure that their library tags are computed. |
| 1682 abstract class ElementScanner { | 1658 abstract class ElementScanner { |
| 1683 void scanLibrary(LibraryElement library); | 1659 void scanLibrary(LibraryElement library); |
| 1684 void scanUnit(CompilationUnitElement unit); | 1660 void scanUnit(CompilationUnitElement unit); |
| 1685 } | 1661 } |
| 1686 | 1662 |
| 1687 const _reuseLibrarySubtaskName = "Reuse library"; | 1663 const _reuseLibrarySubtaskName = "Reuse library"; |
| OLD | NEW |