| 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'; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 /// code for the SDK. | 30 /// code for the SDK. |
| 31 /// | 31 /// |
| 32 /// 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 |
| 33 /// analysis. If in addition `compileSdk` is false, this will speed up | 33 /// analysis. If in addition `compileSdk` is false, this will speed up |
| 34 /// compilation, as no source of the sdk will be generated. Note however, that | 34 /// compilation, as no source of the sdk will be generated. Note however, that |
| 35 /// summaries for application code can also speed up analysis, but they will not | 35 /// summaries for application code can also speed up analysis, but they will not |
| 36 /// take the place of Dart source code (since the Dart source code is still | 36 /// take the place of Dart source code (since the Dart source code is still |
| 37 /// needed to access the contents of method bodies). | 37 /// needed to access the contents of method bodies). |
| 38 Future<Program> kernelForProgram(Uri source, CompilerOptions options) async { | 38 Future<Program> kernelForProgram(Uri source, CompilerOptions options) async { |
| 39 var loader = await _createLoader(options, entry: source); | 39 var loader = await _createLoader(options, entry: source); |
| 40 // TODO(sigmund): delete this. At this time we have no need to explicitly list |
| 41 // VM libraries, since they are normally found by chasing dependencies. |
| 42 // `dart:_builtin` is an exception because it is used by the kernel |
| 43 // transformers to inform the VM about where the main entrypoint is. This is |
| 44 // expected to change, and we should be able to remove these lines at that |
| 45 // point. We check for the presense of `dart:developer` in the targetPatches |
| 46 // to ensure we only load this library while running on the VM. |
| 47 if (options.compileSdk && |
| 48 options.targetPatches.containsKey(Uri.parse('dart:developer'))) { |
| 49 loader.loadLibrary(Uri.parse('dart:_builtin')); |
| 50 } |
| 40 // TODO(sigmund): merge what we have in loadEverything and the logic below in | 51 // 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 | 52 // kernelForBuildUnit so there is a single place where we crawl for |
| 42 // dependencies. | 53 // dependencies. |
| 43 Program program = loader.loadProgram(source, compileSdk: options.compileSdk); | 54 Program program = loader.loadProgram(source, compileSdk: options.compileSdk); |
| 44 _reportErrors(loader.errors, options.onError); | 55 _reportErrors(loader.errors, options.onError); |
| 45 return program; | 56 return program; |
| 46 } | 57 } |
| 47 | 58 |
| 48 /// Generates a kernel representation for a build unit. | 59 /// Generates a kernel representation for a build unit. |
| 49 /// | 60 /// |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 /// Create a [DartLoader] using the provided [options]. | 135 /// Create a [DartLoader] using the provided [options]. |
| 125 /// | 136 /// |
| 126 /// If [options] contain no configuration to resolve `.packages`, the [entry] | 137 /// If [options] contain no configuration to resolve `.packages`, the [entry] |
| 127 /// file will be used to search for a `.packages` file. | 138 /// file will be used to search for a `.packages` file. |
| 128 Future<DartLoader> _createLoader(CompilerOptions options, | 139 Future<DartLoader> _createLoader(CompilerOptions options, |
| 129 {Repository repository, Uri entry}) async { | 140 {Repository repository, Uri entry}) async { |
| 130 var kernelOptions = _convertOptions(options); | 141 var kernelOptions = _convertOptions(options); |
| 131 var packages = await createPackages( | 142 var packages = await createPackages( |
| 132 _uriToPath(options.packagesFileUri, options), | 143 _uriToPath(options.packagesFileUri, options), |
| 133 discoveryPath: entry?.path); | 144 discoveryPath: entry?.path); |
| 134 return new DartLoader( | 145 var loader = new DartLoader( |
| 135 repository ?? new Repository(), kernelOptions, packages); | 146 repository ?? new Repository(), kernelOptions, packages); |
| 147 var patchPaths = {}; |
| 148 |
| 149 // TODO(sigmund,paulberry): use ProcessedOptions so that we can resolve the |
| 150 // URIs correctly even if sdkRoot is inferred and not specified explicitly. |
| 151 String resolve(Uri patch) => |
| 152 options.fileSystem.context.fromUri(options.sdkRoot.resolveUri(patch)); |
| 153 |
| 154 options.targetPatches.forEach((uri, patches) { |
| 155 patchPaths['$uri'] = patches.map(resolve).toList(); |
| 156 }); |
| 157 loader.context.analysisOptions.patchPaths = patchPaths; |
| 158 return loader; |
| 136 } | 159 } |
| 137 | 160 |
| 138 DartOptions _convertOptions(CompilerOptions options) { | 161 DartOptions _convertOptions(CompilerOptions options) { |
| 139 return new DartOptions( | 162 return new DartOptions( |
| 140 strongMode: options.strongMode, | 163 strongMode: options.strongMode, |
| 141 sdk: _uriToPath(options.sdkRoot, options), | 164 sdk: _uriToPath(options.sdkRoot, options), |
| 142 // TODO(sigmund): make it possible to use summaries and still compile the | 165 // TODO(sigmund): make it possible to use summaries and still compile the |
| 143 // sdk sources. | 166 // sdk sources. |
| 144 sdkSummary: | 167 sdkSummary: |
| 145 options.compileSdk ? null : _uriToPath(options.sdkSummary, options), | 168 options.compileSdk ? null : _uriToPath(options.sdkSummary, options), |
| 146 packagePath: _uriToPath(options.packagesFileUri, options), | 169 packagePath: _uriToPath(options.packagesFileUri, options), |
| 170 customUriMappings: options.uriOverride, |
| 147 declaredVariables: options.declaredVariables); | 171 declaredVariables: options.declaredVariables); |
| 148 } | 172 } |
| 149 | 173 |
| 150 void _reportErrors(List errors, ErrorHandler onError) { | 174 void _reportErrors(List errors, ErrorHandler onError) { |
| 151 if (onError == null) return; | 175 if (onError == null) return; |
| 152 for (var error in errors) { | 176 for (var error in errors) { |
| 153 onError(new _DartkError(error)); | 177 onError(new _DartkError(error)); |
| 154 } | 178 } |
| 155 } | 179 } |
| 156 | 180 |
| 157 String _uriToPath(Uri uri, CompilerOptions options) { | 181 String _uriToPath(Uri uri, CompilerOptions options) { |
| 158 if (uri == null) return null; | 182 if (uri == null) return null; |
| 159 if (uri.scheme != 'file') { | 183 if (uri.scheme != 'file') { |
| 160 throw new StateError('Only file URIs are supported'); | 184 throw new StateError('Only file URIs are supported'); |
| 161 } | 185 } |
| 162 return options.fileSystem.context.fromUri(uri); | 186 return options.fileSystem.context.fromUri(uri); |
| 163 } | 187 } |
| 164 | 188 |
| 165 // TODO(sigmund): delete this class. Dartk should not format errors itself, we | 189 // TODO(sigmund): delete this class. Dartk should not format errors itself, we |
| 166 // should just pass them along. | 190 // should just pass them along. |
| 167 class _DartkError implements CompilationError { | 191 class _DartkError implements CompilationError { |
| 168 String get correction => null; | 192 String get correction => null; |
| 169 SourceSpan get span => null; | 193 SourceSpan get span => null; |
| 170 final String message; | 194 final String message; |
| 171 _DartkError(this.message); | 195 _DartkError(this.message); |
| 172 } | 196 } |
| OLD | NEW |