| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 /// Defines the front-end API for converting source code to Dart Kernel objects. | 5 /// Defines the front-end API for converting source code to Dart Kernel objects. |
| 6 library front_end.kernel_generator; | 6 library front_end.kernel_generator; |
| 7 | 7 |
| 8 import 'compilation_error.dart'; | 8 import 'compilation_error.dart'; |
| 9 import 'compiler_options.dart'; | 9 import 'compiler_options.dart'; |
| 10 import 'dart:async'; | 10 import 'dart:async'; |
| 11 | 11 |
| 12 import 'package:analyzer/src/generated/source.dart' show SourceKind; |
| 13 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
| 14 show InSummarySource; |
| 12 // TODO(sigmund): move loader logic under front_end/lib/src/kernel/ | 15 // TODO(sigmund): move loader logic under front_end/lib/src/kernel/ |
| 13 import 'package:kernel/analyzer/loader.dart'; | 16 import 'package:kernel/analyzer/loader.dart'; |
| 14 import 'package:kernel/kernel.dart'; | 17 import 'package:kernel/kernel.dart'; |
| 15 import 'package:source_span/source_span.dart' show SourceSpan; | 18 import 'package:source_span/source_span.dart' show SourceSpan; |
| 16 | 19 |
| 17 /// Generates a kernel representation of the program whose main library is in | 20 /// Generates a kernel representation of the program whose main library is in |
| 18 /// the given [source]. | 21 /// the given [source]. |
| 19 /// | 22 /// |
| 20 /// Intended for whole program (non-modular) compilation. | 23 /// Intended for whole program (non-modular) compilation. |
| 21 /// | 24 /// |
| 22 /// Given the Uri of a file containing a program's `main` method, this function | 25 /// Given the Uri of a file containing a program's `main` method, this function |
| 23 /// follows `import`, `export`, and `part` declarations to discover the whole | 26 /// follows `import`, `export`, and `part` declarations to discover the whole |
| 24 /// program, and converts the result to Dart Kernel format. | 27 /// program, and converts the result to Dart Kernel format. |
| 25 /// | 28 /// |
| 29 /// If `compileSdk` in [options] is true, the generated program will include |
| 30 /// code for the SDK. |
| 31 /// |
| 26 /// If summaries are provided in [options], they may be used to speed up | 32 /// If summaries are provided in [options], they may be used to speed up |
| 27 /// analysis, but they will not take the place of Dart source code (since the | 33 /// analysis. If in addition `compileSdk` is false, this will speed up |
| 28 /// Dart source code is still needed to access the contents of method bodies). | 34 /// compilation, as no source of the sdk will be generated. Note however, that |
| 29 /// | 35 /// summaries for application code can also speed up analysis, but they will not |
| 30 /// TODO(paulberry): will the VM have a pickled version of the SDK inside it? If | 36 /// take the place of Dart source code (since the Dart source code is still |
| 31 /// so, then maybe this method should not convert SDK libraries to kernel. | 37 /// needed to access the contents of method bodies). |
| 32 Future<Program> kernelForProgram(Uri source, CompilerOptions options) async { | 38 Future<Program> kernelForProgram(Uri source, CompilerOptions options) async { |
| 33 var loader = await _createLoader(options); | 39 var loader = await _createLoader(options, entry: source); |
| 34 Program program = loader.loadProgram(source); | 40 // TODO(sigmund): merge what we have in loadEverything and the logic below in |
| 41 // kernelForBuildUnit so there is a single place where we crawl for |
| 42 // dependencies. |
| 43 Program program = loader.loadProgram(source, compileSdk: options.compileSdk); |
| 35 _reportErrors(loader.errors, options.onError); | 44 _reportErrors(loader.errors, options.onError); |
| 36 return program; | 45 return program; |
| 37 } | 46 } |
| 38 | 47 |
| 39 /// Generates a kernel representation of the build unit whose source files are | 48 /// Generates a kernel representation for a build unit. |
| 40 /// in [sources]. | |
| 41 /// | 49 /// |
| 42 /// Intended for modular compilation. | 50 /// Intended for modular compilation. |
| 43 /// | 51 /// |
| 44 /// [sources] should be the complete set of source files for a build unit | 52 /// The build unit by default contains only the source files in [sources] |
| 45 /// (including both library and part files). All of the library files are | 53 /// (including library and part files), but if |
| 46 /// transformed into Dart Kernel Library objects. | 54 /// [CompilerOptions.chaseDependencies] is true, it may include some additional |
| 55 /// source files. All of the library files are transformed into Dart Kernel |
| 56 /// Library objects. |
| 47 /// | 57 /// |
| 48 /// The compilation process is hermetic, meaning that the only files which will | 58 /// By default, the compilation process is hermetic, meaning that the only files |
| 49 /// be read are those listed in [sources], [CompilerOptions.inputSummaries], and | 59 /// which will be read are those listed in [sources], |
| 50 /// [CompilerOptions.sdkSummary]. If a source file attempts to refer to a file | 60 /// [CompilerOptions.inputSummaries], and [CompilerOptions.sdkSummary]. If a |
| 51 /// which is not obtainable from these paths, that will result in an error, even | 61 /// source file attempts to refer to a file which is not obtainable from these |
| 52 /// if the file exists on the filesystem. | 62 /// paths, that will result in an error, even if the file exists on the |
| 63 /// filesystem. |
| 64 /// |
| 65 /// When [CompilerOptions.chaseDependencies] is true, this default behavior |
| 66 /// changes, and any dependency of [sources] that is not listed in |
| 67 /// [CompilerOptions.inputSummaries] and [CompilerOptions.sdkSummary] is treated |
| 68 /// as an additional source file for the build unit. |
| 53 /// | 69 /// |
| 54 /// Any `part` declarations found in [sources] must refer to part files which | 70 /// Any `part` declarations found in [sources] must refer to part files which |
| 55 /// are also listed in [sources], otherwise an error results. (It is not | 71 /// are also listed in the build unit sources, otherwise an error results. (It |
| 56 /// permitted to refer to a part file declared in another build unit). | 72 /// is not permitted to refer to a part file declared in another build unit). |
| 57 /// | 73 /// |
| 58 /// The return value is a [Program] object with no main method set. | 74 /// The return value is a [Program] object with no main method set. |
| 59 /// TODO(paulberry): would it be better to define a data type in kernel to | 75 /// TODO(paulberry): would it be better to define a data type in kernel to |
| 60 /// represent a bundle of all the libraries in a given build unit? | 76 /// represent a bundle of all the libraries in a given build unit? |
| 61 /// | 77 /// |
| 62 /// TODO(paulberry): does additional information need to be output to allow the | 78 /// TODO(paulberry): does additional information need to be output to allow the |
| 63 /// caller to match up referenced elements to the summary files they were | 79 /// caller to match up referenced elements to the summary files they were |
| 64 /// obtained from? | 80 /// obtained from? |
| 65 Future<Program> kernelForBuildUnit( | 81 Future<Program> kernelForBuildUnit( |
| 66 List<Uri> sources, CompilerOptions options) async { | 82 List<Uri> sources, CompilerOptions options) async { |
| 67 var repository = new Repository(); | 83 var repository = new Repository(); |
| 68 var loader = await _createLoader(options, repository: repository); | 84 var loader = await _createLoader(options, repository: repository); |
| 69 // TODO(sigmund): add special handling for part files. | 85 var context = loader.context; |
| 70 sources.forEach(loader.loadLibrary); | 86 |
| 87 // Process every library in the build unit. |
| 88 for (var uri in sources) { |
| 89 var source = context.sourceFactory.forUri2(uri); |
| 90 // We ignore part files, those are handled by their enclosing library. |
| 91 if (context.computeKindOf(source) == SourceKind.PART) { |
| 92 // TODO(sigmund): record it and ensure that this part is within a provided |
| 93 // library. |
| 94 continue; |
| 95 } |
| 96 loader.loadLibrary(uri); |
| 97 } |
| 98 |
| 99 // Check whether all dependencies were included in [sources]. |
| 100 // TODO(sigmund): we should look for dependencies using import, export, and |
| 101 // part directives intead of relying on the dartk-loader. In particular, if a |
| 102 // library is imported but not used, the logic below will not detect it. |
| 103 for (int i = 0; i < repository.libraries.length; ++i) { |
| 104 // Note: we don't use a for-in loop because repository.libraries grows as |
| 105 // the loader processes libraries. |
| 106 var lib = repository.libraries[i]; |
| 107 var source = context.sourceFactory.forUri2(lib.importUri); |
| 108 if (source is InSummarySource) continue; |
| 109 if (options.chaseDependencies) { |
| 110 loader.ensureLibraryIsLoaded(lib); |
| 111 } else if (lib.isExternal) { |
| 112 // Default behavior: the build should be hermetic and all dependencies |
| 113 // should be listed. |
| 114 options.onError(new _DartkError('hermetic build error: ' |
| 115 'no source or summary was given for ${lib.importUri}')); |
| 116 } |
| 117 } |
| 118 |
| 71 Program program = new Program(repository.libraries); | 119 Program program = new Program(repository.libraries); |
| 72 _reportErrors(loader.errors, options.onError); | 120 _reportErrors(loader.errors, options.onError); |
| 73 return program; | 121 return program; |
| 74 } | 122 } |
| 75 | 123 |
| 124 /// Create a [DartLoader] using the provided [options]. |
| 125 /// |
| 126 /// If [options] contain no configuration to resolve `.packages`, the [entry] |
| 127 /// file will be used to search for a `.packages` file. |
| 76 Future<DartLoader> _createLoader(CompilerOptions options, | 128 Future<DartLoader> _createLoader(CompilerOptions options, |
| 77 {Repository repository}) async { | 129 {Repository repository, Uri entry}) async { |
| 78 var kernelOptions = _convertOptions(options); | 130 var kernelOptions = _convertOptions(options); |
| 79 var packages = await createPackages(options.packagesFilePath); | 131 var packages = await createPackages(options.packagesFilePath, |
| 132 discoveryPath: entry?.path); |
| 80 return new DartLoader( | 133 return new DartLoader( |
| 81 repository ?? new Repository(), kernelOptions, packages); | 134 repository ?? new Repository(), kernelOptions, packages); |
| 82 } | 135 } |
| 83 | 136 |
| 84 DartOptions _convertOptions(CompilerOptions options) { | 137 DartOptions _convertOptions(CompilerOptions options) { |
| 85 return new DartOptions( | 138 return new DartOptions( |
| 139 strongMode: options.strongMode, |
| 86 sdk: options.sdkPath, | 140 sdk: options.sdkPath, |
| 141 // TODO(sigmund): make it possible to use summaries and still compile the |
| 142 // sdk sources. |
| 143 sdkSummary: options.compileSdk ? null : options.sdkSummary, |
| 87 packagePath: options.packagesFilePath, | 144 packagePath: options.packagesFilePath, |
| 88 declaredVariables: options.declaredVariables); | 145 declaredVariables: options.declaredVariables); |
| 89 } | 146 } |
| 90 | 147 |
| 91 void _reportErrors(List errors, ErrorHandler onError) { | 148 void _reportErrors(List errors, ErrorHandler onError) { |
| 92 if (onError == null) return; | 149 if (onError == null) return; |
| 93 for (var error in errors) { | 150 for (var error in errors) { |
| 94 onError(new _DartkError(error)); | 151 onError(new _DartkError(error)); |
| 95 } | 152 } |
| 96 } | 153 } |
| 97 | 154 |
| 98 // TODO(sigmund): delete this class. Dartk should not format errors itself, we | 155 // TODO(sigmund): delete this class. Dartk should not format errors itself, we |
| 99 // should just pass them along. | 156 // should just pass them along. |
| 100 class _DartkError implements CompilationError { | 157 class _DartkError implements CompilationError { |
| 101 String get correction => null; | 158 String get correction => null; |
| 102 SourceSpan get span => null; | 159 SourceSpan get span => null; |
| 103 final String message; | 160 final String message; |
| 104 _DartkError(this.message); | 161 _DartkError(this.message); |
| 105 } | 162 } |
| OLD | NEW |