| 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 import 'dart:io' show Platform; |
| 9 |
| 10 import 'package:front_end/front_end.dart' as fe; |
| 11 import 'package:kernel/ast.dart' as ir; |
| 12 import 'package:kernel/binary/ast_from_binary.dart' show BinaryBuilder; |
| 13 import 'package:kernel/kernel.dart' hide LibraryDependency, Combinator; |
| 14 import 'package:kernel/target/targets.dart'; |
| 15 |
| 16 import '../compiler_new.dart' as api; |
| 17 import 'kernel/front_end_adapter.dart'; |
| 18 import 'kernel/dart2js_target.dart' show Dart2jsTarget; |
| 8 | 19 |
| 9 import 'common/names.dart' show Uris; | 20 import 'common/names.dart' show Uris; |
| 10 import 'common/tasks.dart' show CompilerTask, Measurer; | 21 import 'common/tasks.dart' show CompilerTask, Measurer; |
| 11 import 'common.dart'; | 22 import 'common.dart'; |
| 12 import 'elements/elements.dart' | 23 import 'elements/elements.dart' |
| 13 show | 24 show |
| 14 CompilationUnitElement, | 25 CompilationUnitElement, |
| 15 Element, | 26 Element, |
| 16 ImportElement, | 27 ImportElement, |
| 17 ExportElement, | 28 ExportElement, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 32 import 'environment.dart'; | 43 import 'environment.dart'; |
| 33 import 'io/source_file.dart' show Binary; | 44 import 'io/source_file.dart' show Binary; |
| 34 import 'kernel/element_map_impl.dart' show KernelToElementMapForImpactImpl; | 45 import 'kernel/element_map_impl.dart' show KernelToElementMapForImpactImpl; |
| 35 import 'patch_parser.dart' show PatchParserTask; | 46 import 'patch_parser.dart' show PatchParserTask; |
| 36 import 'resolved_uri_translator.dart'; | 47 import 'resolved_uri_translator.dart'; |
| 37 import 'script.dart'; | 48 import 'script.dart'; |
| 38 import 'serialization/serialization.dart' show LibraryDeserializer; | 49 import 'serialization/serialization.dart' show LibraryDeserializer; |
| 39 import 'tree/tree.dart'; | 50 import 'tree/tree.dart'; |
| 40 import 'util/util.dart' show Link, LinkBuilder; | 51 import 'util/util.dart' show Link, LinkBuilder; |
| 41 | 52 |
| 42 import 'package:kernel/ast.dart' as ir; | |
| 43 import 'package:kernel/binary/ast_from_binary.dart' show BinaryBuilder; | |
| 44 | |
| 45 typedef Future<Iterable<LibraryElement>> ReuseLibrariesFunction( | 53 typedef Future<Iterable<LibraryElement>> ReuseLibrariesFunction( |
| 46 Iterable<LibraryElement> libraries); | 54 Iterable<LibraryElement> libraries); |
| 47 | 55 |
| 48 typedef Uri PatchResolverFunction(String dartLibraryPath); | 56 typedef Uri PatchResolverFunction(String dartLibraryPath); |
| 49 | 57 |
| 50 /** | 58 /** |
| 51 * [CompilerTask] for loading libraries and setting up the import/export scopes. | 59 * [CompilerTask] for loading libraries and setting up the import/export scopes. |
| 52 * | 60 * |
| 53 * The library loader uses four different kinds of URIs in different parts of | 61 * The library loader uses four different kinds of URIs in different parts of |
| 54 * the loading process. | 62 * the loading process. |
| (...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 800 _deferredActions.add(action); | 808 _deferredActions.add(action); |
| 801 } | 809 } |
| 802 | 810 |
| 803 Iterable<DeferredAction> pullDeferredActions() { | 811 Iterable<DeferredAction> pullDeferredActions() { |
| 804 Iterable<DeferredAction> actions = _deferredActions.toList(); | 812 Iterable<DeferredAction> actions = _deferredActions.toList(); |
| 805 _deferredActions.clear(); | 813 _deferredActions.clear(); |
| 806 return actions; | 814 return actions; |
| 807 } | 815 } |
| 808 } | 816 } |
| 809 | 817 |
| 810 /// A task for loading a pre-processed .dill file into memory rather than | 818 /// A loader that builds a kernel IR representation of the program (or set of |
| 811 /// parsing Dart source. Use of this task only makes sense when used in | 819 /// libraries). |
| 812 /// conjunction with --use-kernel. | 820 /// |
| 813 class DillLibraryLoaderTask extends CompilerTask implements LibraryLoaderTask { | 821 /// It supports loading both .dart source files or pre-compiled .dill files. |
| 822 /// When given .dart source files, it invokes the shared frontend |
| 823 /// (`package:front_end`) to produce the corresponding kernel IR representation. |
| 824 // TODO(sigmund): move this class to a new file under src/kernel/. |
| 825 class KernelLibraryLoaderTask extends CompilerTask |
| 826 implements LibraryLoaderTask { |
| 814 final DiagnosticReporter reporter; | 827 final DiagnosticReporter reporter; |
| 815 | 828 |
| 816 final ResolvedUriTranslator uriTranslator; | 829 final api.CompilerInput compilerInput; |
| 817 | |
| 818 /// Loads the contents of a script file (a .dart file). Used when loading | |
| 819 /// libraries from source. | |
| 820 final ScriptLoader scriptLoader; | |
| 821 | 830 |
| 822 /// Holds the mapping of Kernel IR to KElements that is constructed as a | 831 /// Holds the mapping of Kernel IR to KElements that is constructed as a |
| 823 /// result of loading a program. | 832 /// result of loading a program. |
| 824 final KernelToElementMapForImpactImpl _elementMap; | 833 final KernelToElementMapForImpactImpl _elementMap; |
| 825 | 834 |
| 826 List<LibraryEntity> _allLoadedLibraries; | 835 List<LibraryEntity> _allLoadedLibraries; |
| 827 | 836 |
| 828 DillLibraryLoaderTask(this._elementMap, this.uriTranslator, this.scriptLoader, | 837 KernelLibraryLoaderTask( |
| 829 this.reporter, Measurer measurer) | 838 this._elementMap, this.compilerInput, this.reporter, Measurer measurer) |
| 830 : _allLoadedLibraries = new List<LibraryEntity>(), | 839 : _allLoadedLibraries = new List<LibraryEntity>(), |
| 831 super(measurer); | 840 super(measurer); |
| 832 | 841 |
| 833 /// Loads an entire Kernel [Program] from a file on disk (note, not just a | 842 /// Loads an entire Kernel [Program] from a file on disk (note, not just a |
| 834 /// library, so this name is actually a bit of a misnomer). | 843 /// library, so this name is actually a bit of a misnomer). |
| 835 // TODO(efortuna): Rename this once the Element library loader class goes | 844 // TODO(efortuna): Rename this once the Element library loader class goes |
| 836 // away. | 845 // away. |
| 837 Future<LoadedLibraries> loadLibrary(Uri resolvedUri, | 846 Future<LoadedLibraries> loadLibrary(Uri resolvedUri, |
| 838 {bool skipFileWithPartOfTag: false}) { | 847 {bool skipFileWithPartOfTag: false}) { |
| 839 assert(resolvedUri.pathSegments.last.endsWith('.dill'), | |
| 840 'Invalid uri: $resolvedUri'); | |
| 841 Uri readableUri = uriTranslator.translate(null, resolvedUri, null); | |
| 842 return measure(() async { | 848 return measure(() async { |
| 843 Binary binary = await scriptLoader.readBinary(readableUri, null); | 849 var isDill = resolvedUri.path.endsWith('.dill'); |
| 844 ir.Program program = new ir.Program(); | 850 ir.Program program; |
| 845 new BinaryBuilder(binary.data).readProgram(program); | 851 if (isDill) { |
| 846 return measure(() { | 852 api.Input input = await compilerInput.readFromUri(resolvedUri, |
| 847 return createLoadedLibraries(program); | 853 inputKind: api.InputKind.binary); |
| 848 }); | 854 program = new ir.Program(); |
| 855 new BinaryBuilder(input.data).readProgram(program); |
| 856 } else { |
| 857 var options = new fe.CompilerOptions() |
| 858 ..fileSystem = new CompilerFileSystem(compilerInput) |
| 859 ..target = new Dart2jsTarget(new TargetFlags()) |
| 860 ..compileSdk = true |
| 861 ..linkedDependencies = [ |
| 862 // TODO(sigmund): infer the location of platform.dill |
| 863 // once it is shipped as part of the sdk. |
| 864 new Uri.file(Platform.resolvedExecutable) |
| 865 .resolve('patched_dart2js_sdk/platform.dill') |
| 866 ] |
| 867 ..onError = (e) => reportFrontEndMessage(reporter, e); |
| 868 |
| 869 program = await fe.kernelForProgram(resolvedUri, options); |
| 870 } |
| 871 if (program == null) return null; |
| 872 return createLoadedLibraries(program); |
| 849 }); | 873 }); |
| 850 } | 874 } |
| 851 | 875 |
| 876 // Only visible for unit testing. |
| 852 LoadedLibraries createLoadedLibraries(ir.Program program) { | 877 LoadedLibraries createLoadedLibraries(ir.Program program) { |
| 853 _elementMap.addProgram(program); | 878 _elementMap.addProgram(program); |
| 854 program.libraries.forEach((ir.Library library) => | 879 program.libraries.forEach((ir.Library library) => |
| 855 _allLoadedLibraries.add(_elementMap.lookupLibrary(library.importUri))); | 880 _allLoadedLibraries.add(_elementMap.lookupLibrary(library.importUri))); |
| 856 LibraryEntity rootLibrary = null; | 881 LibraryEntity rootLibrary = null; |
| 857 if (program.mainMethod != null) { | 882 if (program.mainMethod != null) { |
| 858 rootLibrary = _elementMap | 883 rootLibrary = _elementMap |
| 859 .lookupLibrary(program.mainMethod.enclosingLibrary.importUri); | 884 .lookupLibrary(program.mainMethod.enclosingLibrary.importUri); |
| 860 } | 885 } |
| 861 return new _LoadedLibrariesAdapter( | 886 return new _LoadedLibrariesAdapter( |
| 862 rootLibrary, _allLoadedLibraries, _elementMap); | 887 rootLibrary, _allLoadedLibraries, _elementMap); |
| 863 } | 888 } |
| 864 | 889 |
| 865 KernelToElementMapForImpactImpl get elementMap => _elementMap; | 890 KernelToElementMapForImpactImpl get elementMap => _elementMap; |
| 866 | 891 |
| 867 void reset({bool reuseLibrary(LibraryElement library)}) { | 892 void reset({bool reuseLibrary(LibraryElement library)}) { |
| 868 throw new UnimplementedError('DillLibraryLoaderTask.reset'); | 893 throw new UnimplementedError('KernelLibraryLoaderTask.reset'); |
| 869 } | 894 } |
| 870 | 895 |
| 871 Future resetAsync(Future<bool> reuseLibrary(LibraryElement library)) { | 896 Future resetAsync(Future<bool> reuseLibrary(LibraryElement library)) { |
| 872 throw new UnimplementedError('DillLibraryLoaderTask.resetAsync'); | 897 throw new UnimplementedError('KernelLibraryLoaderTask.resetAsync'); |
| 873 } | 898 } |
| 874 | 899 |
| 875 Iterable<LibraryEntity> get libraries => _allLoadedLibraries; | 900 Iterable<LibraryEntity> get libraries => _allLoadedLibraries; |
| 876 | 901 |
| 877 LibraryEntity lookupLibrary(Uri canonicalUri) { | 902 LibraryEntity lookupLibrary(Uri canonicalUri) { |
| 878 return _elementMap?.lookupLibrary(canonicalUri); | 903 return _elementMap?.lookupLibrary(canonicalUri); |
| 879 } | 904 } |
| 880 | 905 |
| 881 Future<Null> resetLibraries(ReuseLibrariesFunction reuseLibraries) { | 906 Future<Null> resetLibraries(ReuseLibrariesFunction reuseLibraries) { |
| 882 throw new UnimplementedError('DillLibraryLoaderTask.reuseLibraries'); | 907 throw new UnimplementedError('KernelLibraryLoaderTask.reuseLibraries'); |
| 883 } | 908 } |
| 884 | 909 |
| 885 void registerDeferredAction(DeferredAction action) { | 910 void registerDeferredAction(DeferredAction action) { |
| 886 throw new UnimplementedError( | 911 throw new UnimplementedError( |
| 887 'DillLibraryLoaderTask.registerDeferredAction'); | 912 'KernelLibraryLoaderTask.registerDeferredAction'); |
| 888 } | 913 } |
| 889 | 914 |
| 890 Iterable<DeferredAction> pullDeferredActions() => const <DeferredAction>[]; | 915 Iterable<DeferredAction> pullDeferredActions() => const <DeferredAction>[]; |
| 891 } | 916 } |
| 892 | 917 |
| 893 /// A state machine for checking script tags come in the correct order. | 918 /// A state machine for checking script tags come in the correct order. |
| 894 class TagState { | 919 class TagState { |
| 895 /// Initial state. | 920 /// Initial state. |
| 896 static const int NO_TAG_SEEN = 0; | 921 static const int NO_TAG_SEEN = 0; |
| 897 | 922 |
| (...skipping 769 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1667 } | 1692 } |
| 1668 | 1693 |
| 1669 /// API used by the library loader to synchronously scan a library or | 1694 /// API used by the library loader to synchronously scan a library or |
| 1670 /// compilation unit and ensure that their library tags are computed. | 1695 /// compilation unit and ensure that their library tags are computed. |
| 1671 abstract class ElementScanner { | 1696 abstract class ElementScanner { |
| 1672 void scanLibrary(LibraryElement library); | 1697 void scanLibrary(LibraryElement library); |
| 1673 void scanUnit(CompilationUnitElement unit); | 1698 void scanUnit(CompilationUnitElement unit); |
| 1674 } | 1699 } |
| 1675 | 1700 |
| 1676 const _reuseLibrarySubtaskName = "Reuse library"; | 1701 const _reuseLibrarySubtaskName = "Reuse library"; |
| OLD | NEW |