Chromium Code Reviews| 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'; | |
| 9 import 'compiler_options.dart'; | |
| 8 import 'dart:async'; | 10 import 'dart:async'; |
| 9 import 'compiler_options.dart'; | 11 |
| 10 import 'package:kernel/kernel.dart' as kernel; | 12 // TODO(sigmund): move loader logic under front_end/lib/src/kernel/ |
| 13 import 'package:kernel/analyzer/loader.dart'; | |
| 14 import 'package:kernel/kernel.dart'; | |
| 11 | 15 |
| 12 /// Generates a kernel representation of the program whose main library is in | 16 /// Generates a kernel representation of the program whose main library is in |
| 13 /// the given [source]. | 17 /// the given [source]. |
| 14 /// | 18 /// |
| 15 /// Intended for whole program (non-modular) compilation. | 19 /// Intended for whole program (non-modular) compilation. |
| 16 /// | 20 /// |
| 17 /// Given the Uri of a file containing a program's `main` method, this function | 21 /// Given the Uri of a file containing a program's `main` method, this function |
| 18 /// follows `import`, `export`, and `part` declarations to discover the whole | 22 /// follows `import`, `export`, and `part` declarations to discover the whole |
| 19 /// program, and converts the result to Dart Kernel format. | 23 /// program, and converts the result to Dart Kernel format. |
| 20 /// | 24 /// |
| 21 /// If summaries are provided in [options], they may be used to speed up | 25 /// If summaries are provided in [options], they may be used to speed up |
| 22 /// analysis, but they will not take the place of Dart source code (since the | 26 /// analysis, but they will not take the place of Dart source code (since the |
| 23 /// Dart source code is still needed to access the contents of method bodies). | 27 /// Dart source code is still needed to access the contents of method bodies). |
| 24 /// | 28 /// |
| 25 /// TODO(paulberry): will the VM have a pickled version of the SDK inside it? If | 29 /// TODO(paulberry): will the VM have a pickled version of the SDK inside it? If |
| 26 /// so, then maybe this method should not convert SDK libraries to kernel. | 30 /// so, then maybe this method should not convert SDK libraries to kernel. |
| 27 Future<kernel.Program> kernelForProgram(Uri source, CompilerOptions options) => | 31 Future<Program> kernelForProgram(Uri source, CompilerOptions options) async { |
| 28 throw new UnimplementedError(); | 32 var loader = await _createLoader(options); |
| 33 Program program = loader.loadProgram(source); | |
| 34 _reportErrors(loader.errors, options.onError); | |
| 35 return program; | |
| 36 } | |
| 29 | 37 |
| 30 /// Generates a kernel representation of the build unit whose source files are | 38 /// Generates a kernel representation of the build unit whose source files are |
| 31 /// in [sources]. | 39 /// in [sources]. |
| 32 /// | 40 /// |
| 33 /// Intended for modular compilation. | 41 /// Intended for modular compilation. |
| 34 /// | 42 /// |
| 35 /// [sources] should be the complete set of source files for a build unit | 43 /// [sources] should be the complete set of source files for a build unit |
| 36 /// (including both library and part files). All of the library files are | 44 /// (including both library and part files). All of the library files are |
| 37 /// transformed into Dart Kernel Library objects. | 45 /// transformed into Dart Kernel Library objects. |
| 38 /// | 46 /// |
| 39 /// The compilation process is hermetic, meaning that the only files which will | 47 /// The compilation process is hermetic, meaning that the only files which will |
| 40 /// be read are those listed in [sources], [CompilerOptions.inputSummaries], and | 48 /// be read are those listed in [sources], [CompilerOptions.inputSummaries], and |
| 41 /// [CompilerOptions.sdkSummary]. If a source file attempts to refer to a file | 49 /// [CompilerOptions.sdkSummary]. If a source file attempts to refer to a file |
| 42 /// which is not obtainable from these paths, that will result in an error, even | 50 /// which is not obtainable from these paths, that will result in an error, even |
| 43 /// if the file exists on the filesystem. | 51 /// if the file exists on the filesystem. |
| 44 /// | 52 /// |
| 45 /// Any `part` declarations found in [sources] must refer to part files which | 53 /// Any `part` declarations found in [sources] must refer to part files which |
| 46 /// are also listed in [sources], otherwise an error results. (It is not | 54 /// are also listed in [sources], otherwise an error results. (It is not |
| 47 /// permitted to refer to a part file declared in another build unit). | 55 /// permitted to refer to a part file declared in another build unit). |
| 48 /// | 56 /// |
| 49 /// The return value is a [kernel.Program] object with no main method set. | 57 /// The return value is a [Program] object with no main method set. |
| 50 /// TODO(paulberry): would it be better to define a data type in kernel to | 58 /// TODO(paulberry): would it be better to define a data type in kernel to |
| 51 /// represent a bundle of all the libraries in a given build unit? | 59 /// represent a bundle of all the libraries in a given build unit? |
| 52 /// | 60 /// |
| 53 /// TODO(paulberry): does additional information need to be output to allow the | 61 /// TODO(paulberry): does additional information need to be output to allow the |
| 54 /// caller to match up referenced elements to the summary files they were | 62 /// caller to match up referenced elements to the summary files they were |
| 55 /// obtained from? | 63 /// obtained from? |
| 56 Future<kernel.Program> kernelForBuildUnit( | 64 Future<Program> kernelForBuildUnit( |
| 57 List<Uri> sources, CompilerOptions options) => | 65 List<Uri> sources, CompilerOptions options) async { |
| 58 throw new UnimplementedError(); | 66 var repository = new Repository(); |
| 67 var loader = await _createLoader(options, repository: repository); | |
| 68 sources.forEach(loader.loadLibrary); | |
|
Paul Berry
2016/12/04 09:09:52
Does this work correctly if one of the sources is
Siggi Cherem (dart-lang)
2016/12/05 21:00:50
Good point - I'll add a TODO, it doesn't crash but
| |
| 69 Program program = new Program(repository.libraries); | |
| 70 _reportErrors(loader.errors, options.onError); | |
| 71 return program; | |
| 72 } | |
| 73 | |
| 74 Future<DartLoader> _createLoader(CompilerOptions options, | |
| 75 {Repository repository}) async { | |
| 76 var kernelOptions = _convertOptions(options); | |
| 77 var packages = await createPackages(options.packagesFilePath); | |
| 78 return new DartLoader( | |
| 79 repository ?? new Repository(), kernelOptions, packages); | |
| 80 } | |
| 81 | |
| 82 DartOptions _convertOptions(CompilerOptions options) { | |
|
Paul Berry
2016/12/04 09:09:52
How feasible would it be to get dartk to respect C
Siggi Cherem (dart-lang)
2016/12/05 21:00:50
I believe it shouldn't take too much work to do so
| |
| 83 return new DartOptions( | |
| 84 sdk: options.sdkPath, | |
| 85 packagePath: options.packagesFilePath, | |
| 86 declaredVariables: options.declaredVariables); | |
| 87 } | |
| 88 | |
| 89 void _reportErrors(List errors, ErrorHandler onError) { | |
| 90 if (onError == null) return; | |
| 91 for (var error in errors) { | |
| 92 onError(new _DartkError(error)); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 // TODO(sigmund): delete this class. Dartk should not format errors itself, we | |
| 97 // should just pass them along. | |
| 98 class _DartkError implements CompilationError { | |
| 99 String get correction => null; | |
| 100 String get span => null; | |
| 101 final String message; | |
| 102 _DartkError(this.message); | |
| 103 } | |
| OLD | NEW |