Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Unified Diff: pkg/front_end/lib/kernel_generator.dart

Issue 2508933003: Implement the generate-kernel api by invoking dartk. Add example that uses this API to generate a by (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/front_end/tool/example.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/front_end/lib/kernel_generator.dart
diff --git a/pkg/front_end/lib/kernel_generator.dart b/pkg/front_end/lib/kernel_generator.dart
index 3b3508655d2395d5ffd1464a6dbce58792773ed2..3107e7f92ee2cc117574b01c4ea450d9a991c7d5 100644
--- a/pkg/front_end/lib/kernel_generator.dart
+++ b/pkg/front_end/lib/kernel_generator.dart
@@ -5,9 +5,13 @@
/// Defines the front-end API for converting source code to Dart Kernel objects.
library front_end.kernel_generator;
-import 'dart:async';
+import 'compilation_error.dart';
import 'compiler_options.dart';
-import 'package:kernel/kernel.dart' as kernel;
+import 'dart:async';
+
+// TODO(sigmund): move loader logic under front_end/lib/src/kernel/
+import 'package:kernel/analyzer/loader.dart';
+import 'package:kernel/kernel.dart';
/// Generates a kernel representation of the program whose main library is in
/// the given [source].
@@ -24,8 +28,12 @@ import 'package:kernel/kernel.dart' as kernel;
///
/// TODO(paulberry): will the VM have a pickled version of the SDK inside it? If
/// so, then maybe this method should not convert SDK libraries to kernel.
-Future<kernel.Program> kernelForProgram(Uri source, CompilerOptions options) =>
- throw new UnimplementedError();
+Future<Program> kernelForProgram(Uri source, CompilerOptions options) async {
+ var loader = await _createLoader(options);
+ Program program = loader.loadProgram(source);
+ _reportErrors(loader.errors, options.onError);
+ return program;
+}
/// Generates a kernel representation of the build unit whose source files are
/// in [sources].
@@ -46,13 +54,50 @@ Future<kernel.Program> kernelForProgram(Uri source, CompilerOptions options) =>
/// are also listed in [sources], otherwise an error results. (It is not
/// permitted to refer to a part file declared in another build unit).
///
-/// The return value is a [kernel.Program] object with no main method set.
+/// The return value is a [Program] object with no main method set.
/// TODO(paulberry): would it be better to define a data type in kernel to
/// represent a bundle of all the libraries in a given build unit?
///
/// TODO(paulberry): does additional information need to be output to allow the
/// caller to match up referenced elements to the summary files they were
/// obtained from?
-Future<kernel.Program> kernelForBuildUnit(
- List<Uri> sources, CompilerOptions options) =>
- throw new UnimplementedError();
+Future<Program> kernelForBuildUnit(
+ List<Uri> sources, CompilerOptions options) async {
+ var repository = new Repository();
+ var loader = await _createLoader(options, repository: repository);
+ 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
+ Program program = new Program(repository.libraries);
+ _reportErrors(loader.errors, options.onError);
+ return program;
+}
+
+Future<DartLoader> _createLoader(CompilerOptions options,
+ {Repository repository}) async {
+ var kernelOptions = _convertOptions(options);
+ var packages = await createPackages(options.packagesFilePath);
+ return new DartLoader(
+ repository ?? new Repository(), kernelOptions, packages);
+}
+
+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
+ return new DartOptions(
+ sdk: options.sdkPath,
+ packagePath: options.packagesFilePath,
+ declaredVariables: options.declaredVariables);
+}
+
+void _reportErrors(List errors, ErrorHandler onError) {
+ if (onError == null) return;
+ for (var error in errors) {
+ onError(new _DartkError(error));
+ }
+}
+
+// TODO(sigmund): delete this class. Dartk should not format errors itself, we
+// should just pass them along.
+class _DartkError implements CompilationError {
+ String get correction => null;
+ String get span => null;
+ final String message;
+ _DartkError(this.message);
+}
« no previous file with comments | « no previous file | pkg/front_end/tool/example.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698