| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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_impl; | 6 library front_end.kernel_generator_impl; |
| 7 | 7 |
| 8 import 'dart:async' show Future; | 8 import 'dart:async' show Future; |
| 9 import 'dart:async'; | 9 import 'dart:async'; |
| 10 | 10 |
| 11 import 'package:kernel/kernel.dart' show Program, CanonicalName; | 11 import 'package:kernel/kernel.dart' show Program, CanonicalName; |
| 12 | 12 |
| 13 import 'base/processed_options.dart'; | 13 import 'base/processed_options.dart'; |
| 14 import 'fasta/compiler_command_line.dart' show CompilerCommandLine; | 14 import 'fasta/severity.dart' show Severity; |
| 15 import 'fasta/compiler_context.dart' show CompilerContext; | 15 import 'fasta/compiler_context.dart' show CompilerContext; |
| 16 import 'fasta/deprecated_problems.dart' show deprecated_InputError, reportCrash; | 16 import 'fasta/deprecated_problems.dart' show deprecated_InputError, reportCrash; |
| 17 import 'fasta/dill/dill_target.dart' show DillTarget; | 17 import 'fasta/dill/dill_target.dart' show DillTarget; |
| 18 import 'fasta/kernel/kernel_outline_shaker.dart'; | 18 import 'fasta/kernel/kernel_outline_shaker.dart'; |
| 19 import 'fasta/kernel/kernel_target.dart' show KernelTarget; | 19 import 'fasta/kernel/kernel_target.dart' show KernelTarget; |
| 20 import 'fasta/kernel/utils.dart'; | 20 import 'fasta/kernel/utils.dart'; |
| 21 import 'fasta/kernel/verifier.dart'; | 21 import 'fasta/kernel/verifier.dart'; |
| 22 import 'fasta/uri_translator.dart' show UriTranslator; | 22 import 'fasta/uri_translator.dart' show UriTranslator; |
| 23 | 23 |
| 24 /// Implementation for the `package:front_end/kernel_generator.dart` and | 24 /// Implementation for the `package:front_end/kernel_generator.dart` and |
| 25 /// `package:front_end/summary_generator.dart` APIs. | 25 /// `package:front_end/summary_generator.dart` APIs. |
| 26 Future<CompilerResult> generateKernel(ProcessedOptions options, | 26 Future<CompilerResult> generateKernel(ProcessedOptions options, |
| 27 {bool buildSummary: false, | 27 {bool buildSummary: false, |
| 28 bool buildProgram: true, | 28 bool buildProgram: true, |
| 29 bool trimDependencies: false}) async { | 29 bool trimDependencies: false}) async { |
| 30 // TODO(sigmund): Replace CompilerCommandLine and instead simply use a | 30 return await CompilerContext.runWithOptions(options, (_) async { |
| 31 // CompilerContext that directly uses the ProcessedOptions through the | 31 return await generateKernelInternal( |
| 32 // system. | |
| 33 String programName = ""; | |
| 34 List<String> arguments = <String>[programName, "--target=none"]; | |
| 35 if (options.strongMode) { | |
| 36 arguments.add("--strong-mode"); | |
| 37 } | |
| 38 if (options.verbose) { | |
| 39 arguments.add("--verbose"); | |
| 40 } | |
| 41 if (options.setExitCodeOnProblem) { | |
| 42 arguments.add("--set-exit-code-on-problem"); | |
| 43 } | |
| 44 return await CompilerCommandLine.withGlobalOptions(programName, arguments, | |
| 45 (CompilerContext context) async { | |
| 46 context.options.options["--target"] = options.target; | |
| 47 return await generateKernelInternal(options, | |
| 48 buildSummary: buildSummary, | 32 buildSummary: buildSummary, |
| 49 buildProgram: buildProgram, | 33 buildProgram: buildProgram, |
| 50 trimDependencies: trimDependencies); | 34 trimDependencies: trimDependencies); |
| 51 }); | 35 }); |
| 52 } | 36 } |
| 53 | 37 |
| 54 Future<CompilerResult> generateKernelInternal(ProcessedOptions options, | 38 Future<CompilerResult> generateKernelInternal( |
| 55 {bool buildSummary: false, | 39 {bool buildSummary: false, |
| 56 bool buildProgram: true, | 40 bool buildProgram: true, |
| 57 bool trimDependencies: false}) async { | 41 bool trimDependencies: false}) async { |
| 42 var options = CompilerContext.current.options; |
| 58 var fs = options.fileSystem; | 43 var fs = options.fileSystem; |
| 59 if (!await options.validateOptions()) return null; | 44 if (!await options.validateOptions()) return null; |
| 60 options.ticker.logMs("Validated arguments"); | 45 options.ticker.logMs("Validated arguments"); |
| 61 | 46 |
| 62 try { | 47 try { |
| 63 UriTranslator uriTranslator = await options.getUriTranslator(); | 48 UriTranslator uriTranslator = await options.getUriTranslator(); |
| 64 | 49 |
| 65 var dillTarget = | 50 var dillTarget = |
| 66 new DillTarget(options.ticker, uriTranslator, options.target); | 51 new DillTarget(options.ticker, uriTranslator, options.target); |
| 67 | 52 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 // program is destructive, so if we are emitting summaries and the | 98 // program is destructive, so if we are emitting summaries and the |
| 114 // program in a single API call, we would need to clone the program here | 99 // program in a single API call, we would need to clone the program here |
| 115 // to avoid deleting pieces that are needed by kernelTarget.buildProgram | 100 // to avoid deleting pieces that are needed by kernelTarget.buildProgram |
| 116 // below. | 101 // below. |
| 117 assert(!buildProgram); | 102 assert(!buildProgram); |
| 118 var excluded = | 103 var excluded = |
| 119 dillTarget.loader.libraries.map((lib) => lib.importUri).toSet(); | 104 dillTarget.loader.libraries.map((lib) => lib.importUri).toSet(); |
| 120 trimProgram(summaryProgram, (uri) => !excluded.contains(uri)); | 105 trimProgram(summaryProgram, (uri) => !excluded.contains(uri)); |
| 121 } | 106 } |
| 122 if (options.verify) { | 107 if (options.verify) { |
| 123 verifyProgram(summaryProgram).forEach(options.reportMessage); | 108 for (var error in verifyProgram(summaryProgram)) { |
| 109 options.report(error, Severity.error); |
| 110 } |
| 124 } | 111 } |
| 125 if (options.debugDump) { | 112 if (options.debugDump) { |
| 126 printProgramText(summaryProgram, | 113 printProgramText(summaryProgram, |
| 127 libraryFilter: kernelTarget.isSourceLibrary); | 114 libraryFilter: kernelTarget.isSourceLibrary); |
| 128 } | 115 } |
| 129 if (kernelTarget.errors.isEmpty) { | 116 if (kernelTarget.errors.isEmpty) { |
| 130 summary = serializeProgram(summaryProgram, excludeUriToSource: true); | 117 summary = serializeProgram(summaryProgram, excludeUriToSource: true); |
| 131 } | 118 } |
| 132 options.ticker.logMs("Generated outline"); | 119 options.ticker.logMs("Generated outline"); |
| 133 } | 120 } |
| 134 | 121 |
| 135 Program program; | 122 Program program; |
| 136 if (buildProgram && kernelTarget.errors.isEmpty) { | 123 if (buildProgram && kernelTarget.errors.isEmpty) { |
| 137 program = await kernelTarget.buildProgram(verify: options.verify); | 124 program = await kernelTarget.buildProgram(verify: options.verify); |
| 138 if (trimDependencies) { | 125 if (trimDependencies) { |
| 139 var excluded = | 126 var excluded = |
| 140 dillTarget.loader.libraries.map((lib) => lib.importUri).toSet(); | 127 dillTarget.loader.libraries.map((lib) => lib.importUri).toSet(); |
| 141 trimProgram(program, (uri) => !excluded.contains(uri)); | 128 trimProgram(program, (uri) => !excluded.contains(uri)); |
| 142 } | 129 } |
| 143 if (options.debugDump) { | 130 if (options.debugDump) { |
| 144 printProgramText(program, libraryFilter: kernelTarget.isSourceLibrary); | 131 printProgramText(program, libraryFilter: kernelTarget.isSourceLibrary); |
| 145 } | 132 } |
| 146 options.ticker.logMs("Generated program"); | 133 options.ticker.logMs("Generated program"); |
| 147 } | 134 } |
| 148 | 135 |
| 149 if (kernelTarget.errors.isNotEmpty) { | 136 if (kernelTarget.errors.isNotEmpty) { |
| 150 // TODO(ahe): The errors have already been reported via CompilerContext. | 137 // Note: we don't report errors here because they have been already |
| 151 kernelTarget.errors.forEach(options.reportMessage); | 138 // reported through the compiler context. |
| 152 return null; | 139 return null; |
| 153 } | 140 } |
| 154 | 141 |
| 155 return new CompilerResult( | 142 return new CompilerResult( |
| 156 summary: summary, | 143 summary: summary, |
| 157 program: program, | 144 program: program, |
| 158 deps: kernelTarget.loader.getDependencies()); | 145 deps: kernelTarget.loader.getDependencies()); |
| 159 } on deprecated_InputError catch (e) { | 146 } on deprecated_InputError catch (e) { |
| 160 options.reportMessage(deprecated_InputError.toMessage(e)); | 147 options.report( |
| 148 deprecated_InputError.toMessage(e), Severity.internalProblem); |
| 161 return null; | 149 return null; |
| 162 } catch (e, t) { | 150 } catch (e, t) { |
| 163 return reportCrash(e, t); | 151 return reportCrash(e, t); |
| 164 } | 152 } |
| 165 } | 153 } |
| 166 | 154 |
| 167 /// Result object of [generateKernel]. | 155 /// Result object of [generateKernel]. |
| 168 class CompilerResult { | 156 class CompilerResult { |
| 169 /// The generated summary bytes, if it was requested. | 157 /// The generated summary bytes, if it was requested. |
| 170 final List<int> summary; | 158 final List<int> summary; |
| 171 | 159 |
| 172 /// The generated program, if it was requested. | 160 /// The generated program, if it was requested. |
| 173 final Program program; | 161 final Program program; |
| 174 | 162 |
| 175 /// Dependencies traversed by the compiler. Used only for generating | 163 /// Dependencies traversed by the compiler. Used only for generating |
| 176 /// dependency .GN files in the dart-sdk build system. | 164 /// dependency .GN files in the dart-sdk build system. |
| 177 /// Note this might be removed when we switch to compute depencencies without | 165 /// Note this might be removed when we switch to compute depencencies without |
| 178 /// using the compiler itself. | 166 /// using the compiler itself. |
| 179 final List<Uri> deps; | 167 final List<Uri> deps; |
| 180 | 168 |
| 181 CompilerResult({this.summary, this.program, this.deps}); | 169 CompilerResult({this.summary, this.program, this.deps}); |
| 182 } | 170 } |
| OLD | NEW |