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 'compiler_options.dart'; | 8 import 'compiler_options.dart'; |
9 import 'dart:async' show Future; | 9 import 'dart:async' show Future; |
10 import 'dart:async'; | 10 import 'dart:async'; |
11 import 'package:front_end/physical_file_system.dart'; | 11 import 'package:front_end/src/base/processed_options.dart'; |
12 import 'src/fasta/dill/dill_target.dart' show DillTarget; | 12 import 'src/fasta/dill/dill_target.dart' show DillTarget; |
13 import 'src/fasta/errors.dart' show InputError; | 13 import 'src/fasta/errors.dart' show InputError; |
14 import 'src/fasta/kernel/kernel_target.dart' show KernelTarget; | 14 import 'src/fasta/kernel/kernel_target.dart' show KernelTarget; |
15 import 'package:kernel/kernel.dart' show Program; | 15 import 'package:kernel/kernel.dart' show Program; |
16 import 'src/fasta/ticker.dart' show Ticker; | 16 import 'src/fasta/ticker.dart' show Ticker; |
17 import 'src/fasta/translate_uri.dart' show TranslateUri; | 17 import 'src/fasta/translate_uri.dart' show TranslateUri; |
18 import 'src/simple_error.dart'; | 18 import 'src/simple_error.dart'; |
19 | 19 |
20 /// 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 |
21 /// the given [source]. | 21 /// the given [source]. |
22 /// | 22 /// |
23 /// Intended for whole program (non-modular) compilation. | 23 /// Intended for whole program (non-modular) compilation. |
24 /// | 24 /// |
25 /// 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 |
26 /// follows `import`, `export`, and `part` declarations to discover the whole | 26 /// follows `import`, `export`, and `part` declarations to discover the whole |
27 /// program, and converts the result to Dart Kernel format. | 27 /// program, and converts the result to Dart Kernel format. |
28 /// | 28 /// |
29 /// If `compileSdk` in [options] is true, the generated program will include | 29 /// If `compileSdk` in [options] is true, the generated program will include |
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 will be used to speed up |
33 /// analysis. If in addition `compileSdk` is false, this will speed up | 33 /// the process. If in addition `compileSdk` is false, then the resulting |
34 /// compilation, as no source of the sdk will be generated. Note however, that | 34 /// program will not contain the sdk contents. This is useful when building apps |
35 /// summaries for application code can also speed up analysis, but they will not | 35 /// for platforms that already embed the sdk (e.g. the VM), so there is no need |
36 /// take the place of Dart source code (since the Dart source code is still | 36 /// to spend time and space rebuilding it. |
37 /// needed to access the contents of method bodies). | |
38 Future<Program> kernelForProgram(Uri source, CompilerOptions options) async { | 37 Future<Program> kernelForProgram(Uri source, CompilerOptions options) async { |
39 var fs = options.fileSystem; | 38 var fs = options.fileSystem; |
40 | |
41 report(String msg) { | 39 report(String msg) { |
42 options.onError(new SimpleError(msg)); | 40 options.onError(new SimpleError(msg)); |
43 return null; | 41 return null; |
44 } | 42 } |
45 | 43 |
46 if (!await fs.entityForUri(source).exists()) { | 44 if (!await fs.entityForUri(source).exists()) { |
47 return report("Entry-point file not found: $source"); | 45 return report("Entry-point file not found: $source"); |
48 } | 46 } |
49 | 47 |
50 if (!await validateOptions(options)) return null; | 48 var pOptions = new ProcessedOptions(options); |
| 49 |
| 50 if (!await pOptions.validateOptions()) return null; |
51 | 51 |
52 try { | 52 try { |
53 TranslateUri uriTranslator = await TranslateUri.parse( | 53 TranslateUri uriTranslator = await pOptions.getUriTranslator(); |
54 PhysicalFileSystem.instance, null, options.packagesFileUri); | |
55 | 54 |
56 var dillTarget = | 55 var dillTarget = |
57 new DillTarget(new Ticker(isVerbose: false), uriTranslator); | 56 new DillTarget(new Ticker(isVerbose: false), uriTranslator); |
58 var summary = options.sdkSummary; | 57 var summary = await pOptions.sdkSummaryProgram; |
59 if (summary != null) dillTarget.read(summary); | 58 if (summary != null) { |
| 59 dillTarget.loader.appendLibraries(summary); |
| 60 } |
60 | 61 |
61 var kernelTarget = new KernelTarget( | 62 var kernelTarget = new KernelTarget( |
62 options.fileSystem, dillTarget, uriTranslator, options.strongMode); | 63 options.fileSystem, dillTarget, uriTranslator, options.strongMode); |
63 kernelTarget.read(source); | 64 kernelTarget.read(source); |
64 | 65 |
65 await dillTarget.buildOutlines(); | 66 await dillTarget.buildOutlines(); |
66 await kernelTarget.buildOutlines(); | 67 await kernelTarget.buildOutlines(); |
67 Program program = await kernelTarget.buildProgram(); | 68 Program program = await kernelTarget.buildProgram(trimDependencies: true); |
68 | 69 |
69 if (kernelTarget.errors.isNotEmpty) { | 70 if (kernelTarget.errors.isNotEmpty) { |
70 kernelTarget.errors.forEach(report); | 71 kernelTarget.errors.forEach(report); |
71 return null; | 72 return null; |
72 } | 73 } |
73 | 74 |
74 if (program.mainMethod == null) { | 75 if (program.mainMethod == null) { |
75 return report("No 'main' method found."); | 76 return report("No 'main' method found."); |
76 } | 77 } |
77 | 78 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 /// | 115 /// |
115 /// The return value is a [Program] object with no main method set. | 116 /// The return value is a [Program] object with no main method set. |
116 /// TODO(paulberry): would it be better to define a data type in kernel to | 117 /// TODO(paulberry): would it be better to define a data type in kernel to |
117 /// represent a bundle of all the libraries in a given build unit? | 118 /// represent a bundle of all the libraries in a given build unit? |
118 /// | 119 /// |
119 /// TODO(paulberry): does additional information need to be output to allow the | 120 /// TODO(paulberry): does additional information need to be output to allow the |
120 /// caller to match up referenced elements to the summary files they were | 121 /// caller to match up referenced elements to the summary files they were |
121 /// obtained from? | 122 /// obtained from? |
122 Future<Program> kernelForBuildUnit( | 123 Future<Program> kernelForBuildUnit( |
123 List<Uri> sources, CompilerOptions options) async { | 124 List<Uri> sources, CompilerOptions options) async { |
124 throw new UnimplementedError("kernel for build-unit is not implemented"); | 125 var fs = options.fileSystem; |
| 126 report(String msg) { |
| 127 options.onError(new SimpleError(msg)); |
| 128 return null; |
| 129 } |
| 130 |
| 131 if (!options.chaseDependencies) { |
| 132 // TODO(sigmund): add support, most likely we can do so by adding a wrapper |
| 133 // on top of filesystem that restricts reads to a set of known files. |
| 134 report("hermetic mode (chaseDependencies = false) is not implemented"); |
| 135 return null; |
| 136 } |
| 137 |
| 138 for (var source in sources) { |
| 139 if (!await fs.entityForUri(source).exists()) { |
| 140 return report("Entry-point file not found: $source"); |
| 141 } |
| 142 } |
| 143 |
| 144 var pOptions = new ProcessedOptions(options); |
| 145 |
| 146 if (!await pOptions.validateOptions()) return null; |
| 147 |
| 148 try { |
| 149 TranslateUri uriTranslator = await pOptions.getUriTranslator(); |
| 150 |
| 151 var dillTarget = |
| 152 new DillTarget(new Ticker(isVerbose: false), uriTranslator); |
| 153 var summary = await pOptions.sdkSummaryProgram; |
| 154 if (summary != null) { |
| 155 dillTarget.loader.appendLibraries(summary); |
| 156 } |
| 157 |
| 158 // TODO(sigmund): this is likely not going to work if done naively: if |
| 159 // summaries contain external references we need to ensure they are loaded |
| 160 // in a specific order. |
| 161 for (var inputSummary in await pOptions.inputSummariesPrograms) { |
| 162 dillTarget.loader.appendLibraries(inputSummary); |
| 163 } |
| 164 |
| 165 await dillTarget.buildOutlines(); |
| 166 |
| 167 var kernelTarget = new KernelTarget( |
| 168 options.fileSystem, dillTarget, uriTranslator, options.strongMode); |
| 169 sources.forEach(kernelTarget.read); |
| 170 await kernelTarget.buildOutlines(); |
| 171 |
| 172 Program program = await kernelTarget.buildProgram(trimDependencies: true); |
| 173 |
| 174 if (kernelTarget.errors.isNotEmpty) { |
| 175 kernelTarget.errors.forEach(report); |
| 176 return null; |
| 177 } |
| 178 |
| 179 return program; |
| 180 } on InputError catch (e) { |
| 181 options.onError(new SimpleError(e.format())); |
| 182 return null; |
| 183 } |
125 } | 184 } |
OLD | NEW |