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/src/base/processed_options.dart'; | 11 import 'package:front_end/physical_file_system.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 will be used to speed up | 32 /// If summaries are provided in [options], they may be used to speed up |
33 /// the process. If in addition `compileSdk` is false, then the resulting | 33 /// analysis. If in addition `compileSdk` is false, this will speed up |
34 /// program will not contain the sdk contents. This is useful when building apps | 34 /// compilation, as no source of the sdk will be generated. Note however, that |
35 /// for platforms that already embed the sdk (e.g. the VM), so there is no need | 35 /// summaries for application code can also speed up analysis, but they will not |
36 /// to spend time and space rebuilding it. | 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 Future<Program> kernelForProgram(Uri source, CompilerOptions options) async { | 38 Future<Program> kernelForProgram(Uri source, CompilerOptions options) async { |
38 var fs = options.fileSystem; | 39 var fs = options.fileSystem; |
| 40 |
39 report(String msg) { | 41 report(String msg) { |
40 options.onError(new SimpleError(msg)); | 42 options.onError(new SimpleError(msg)); |
41 return null; | 43 return null; |
42 } | 44 } |
43 | 45 |
44 if (!await fs.entityForUri(source).exists()) { | 46 if (!await fs.entityForUri(source).exists()) { |
45 return report("Entry-point file not found: $source"); | 47 return report("Entry-point file not found: $source"); |
46 } | 48 } |
47 | 49 |
48 var pOptions = new ProcessedOptions(options); | 50 if (!await validateOptions(options)) return null; |
49 | |
50 if (!await pOptions.validateOptions()) return null; | |
51 | 51 |
52 try { | 52 try { |
53 TranslateUri uriTranslator = await pOptions.getUriTranslator(); | 53 TranslateUri uriTranslator = await TranslateUri.parse( |
| 54 PhysicalFileSystem.instance, null, options.packagesFileUri); |
54 | 55 |
55 var dillTarget = | 56 var dillTarget = |
56 new DillTarget(new Ticker(isVerbose: false), uriTranslator); | 57 new DillTarget(new Ticker(isVerbose: false), uriTranslator); |
57 var summary = await pOptions.sdkSummaryProgram; | 58 var summary = options.sdkSummary; |
58 if (summary != null) { | 59 if (summary != null) dillTarget.read(summary); |
59 dillTarget.loader.appendLibraries(summary); | |
60 } | |
61 | 60 |
62 var kernelTarget = new KernelTarget( | 61 var kernelTarget = new KernelTarget( |
63 options.fileSystem, dillTarget, uriTranslator, options.strongMode); | 62 options.fileSystem, dillTarget, uriTranslator, options.strongMode); |
64 kernelTarget.read(source); | 63 kernelTarget.read(source); |
65 | 64 |
66 await dillTarget.buildOutlines(); | 65 await dillTarget.buildOutlines(); |
67 await kernelTarget.buildOutlines(); | 66 await kernelTarget.buildOutlines(); |
68 Program program = await kernelTarget.buildProgram(trimDependencies: true); | 67 Program program = await kernelTarget.buildProgram(); |
69 | 68 |
70 if (kernelTarget.errors.isNotEmpty) { | 69 if (kernelTarget.errors.isNotEmpty) { |
71 kernelTarget.errors.forEach(report); | 70 kernelTarget.errors.forEach(report); |
72 return null; | 71 return null; |
73 } | 72 } |
74 | 73 |
75 if (program.mainMethod == null) { | 74 if (program.mainMethod == null) { |
76 return report("No 'main' method found."); | 75 return report("No 'main' method found."); |
77 } | 76 } |
78 | 77 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 /// | 114 /// |
116 /// The return value is a [Program] object with no main method set. | 115 /// The return value is a [Program] object with no main method set. |
117 /// TODO(paulberry): would it be better to define a data type in kernel to | 116 /// TODO(paulberry): would it be better to define a data type in kernel to |
118 /// represent a bundle of all the libraries in a given build unit? | 117 /// represent a bundle of all the libraries in a given build unit? |
119 /// | 118 /// |
120 /// TODO(paulberry): does additional information need to be output to allow the | 119 /// TODO(paulberry): does additional information need to be output to allow the |
121 /// caller to match up referenced elements to the summary files they were | 120 /// caller to match up referenced elements to the summary files they were |
122 /// obtained from? | 121 /// obtained from? |
123 Future<Program> kernelForBuildUnit( | 122 Future<Program> kernelForBuildUnit( |
124 List<Uri> sources, CompilerOptions options) async { | 123 List<Uri> sources, CompilerOptions options) async { |
125 var fs = options.fileSystem; | 124 throw new UnimplementedError("kernel for build-unit is not implemented"); |
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 } | |
184 } | 125 } |
OLD | NEW |