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'; | 8 import 'compilation_error.dart'; |
9 import 'compiler_options.dart'; | 9 import 'compiler_options.dart'; |
10 import 'dart:async'; | 10 import 'dart:async'; |
11 | 11 |
12 import 'package:analyzer/src/generated/source.dart' show SourceKind; | |
13 import 'package:analyzer/src/summary/package_bundle_reader.dart' | |
14 show InSummarySource; | |
12 // TODO(sigmund): move loader logic under front_end/lib/src/kernel/ | 15 // TODO(sigmund): move loader logic under front_end/lib/src/kernel/ |
13 import 'package:kernel/analyzer/loader.dart'; | 16 import 'package:kernel/analyzer/loader.dart'; |
14 import 'package:kernel/kernel.dart'; | 17 import 'package:kernel/kernel.dart'; |
15 import 'package:source_span/source_span.dart' show SourceSpan; | 18 import 'package:source_span/source_span.dart' show SourceSpan; |
16 | 19 |
17 /// 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 |
18 /// the given [source]. | 21 /// the given [source]. |
19 /// | 22 /// |
20 /// Intended for whole program (non-modular) compilation. | 23 /// Intended for whole program (non-modular) compilation. |
21 /// | 24 /// |
22 /// 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 |
23 /// follows `import`, `export`, and `part` declarations to discover the whole | 26 /// follows `import`, `export`, and `part` declarations to discover the whole |
24 /// program, and converts the result to Dart Kernel format. | 27 /// program, and converts the result to Dart Kernel format. |
25 /// | 28 /// |
26 /// If summaries are provided in [options], they may be used to speed up | 29 /// If summaries are provided in [options], they may be used to speed up |
27 /// analysis, but they will not take the place of Dart source code (since the | 30 /// analysis, but they will not take the place of Dart source code (since the |
28 /// Dart source code is still needed to access the contents of method bodies). | 31 /// Dart source code is still needed to access the contents of method bodies). |
29 /// | 32 /// |
30 /// TODO(paulberry): will the VM have a pickled version of the SDK inside it? If | 33 /// TODO(paulberry): will the VM have a pickled version of the SDK inside it? If |
31 /// so, then maybe this method should not convert SDK libraries to kernel. | 34 /// so, then maybe this method should not convert SDK libraries to kernel. |
32 Future<Program> kernelForProgram(Uri source, CompilerOptions options) async { | 35 Future<Program> kernelForProgram(Uri source, CompilerOptions options) async { |
33 var loader = await _createLoader(options); | 36 var loader = await _createLoader(options, entry: source); |
37 // TODO(sigmund): consider adding support for sdk summaries here as well. | |
34 Program program = loader.loadProgram(source); | 38 Program program = loader.loadProgram(source); |
35 _reportErrors(loader.errors, options.onError); | 39 _reportErrors(loader.errors, options.onError); |
36 return program; | 40 return program; |
37 } | 41 } |
38 | 42 |
39 /// Generates a kernel representation of the build unit whose source files are | 43 /// Generates a kernel representation of the build unit whose source files are |
40 /// in [sources]. | 44 /// in [sources]. |
41 /// | 45 /// |
42 /// Intended for modular compilation. | 46 /// Intended for modular compilation. |
43 /// | 47 /// |
(...skipping 12 matching lines...) Expand all Loading... | |
56 /// permitted to refer to a part file declared in another build unit). | 60 /// permitted to refer to a part file declared in another build unit). |
57 /// | 61 /// |
58 /// The return value is a [Program] object with no main method set. | 62 /// The return value is a [Program] object with no main method set. |
59 /// TODO(paulberry): would it be better to define a data type in kernel to | 63 /// TODO(paulberry): would it be better to define a data type in kernel to |
60 /// represent a bundle of all the libraries in a given build unit? | 64 /// represent a bundle of all the libraries in a given build unit? |
61 /// | 65 /// |
62 /// TODO(paulberry): does additional information need to be output to allow the | 66 /// TODO(paulberry): does additional information need to be output to allow the |
63 /// caller to match up referenced elements to the summary files they were | 67 /// caller to match up referenced elements to the summary files they were |
64 /// obtained from? | 68 /// obtained from? |
65 Future<Program> kernelForBuildUnit( | 69 Future<Program> kernelForBuildUnit( |
66 List<Uri> sources, CompilerOptions options) async { | 70 List<Uri> sources, CompilerOptions options) async { |
67 var repository = new Repository(); | 71 var repository = new Repository(); |
68 var loader = await _createLoader(options, repository: repository); | 72 var loader = await _createLoader(options, |
69 // TODO(sigmund): add special handling for part files. | 73 repository: repository, entry: sources.first); |
Paul Berry
2016/12/09 13:41:40
It seems weird to treat the first source any diffe
Siggi Cherem (dart-lang)
2016/12/09 16:58:21
Good point, will do once we decide on the other as
Siggi Cherem (dart-lang)
2016/12/09 20:46:40
Done.
| |
70 sources.forEach(loader.loadLibrary); | 74 var context = loader.context; |
75 | |
76 // Process every library in the build unit. | |
77 for (var uri in sources) { | |
78 var source = context.sourceFactory.forUri2(uri); | |
79 // We ignore part files, those are handled by their enclosing library. | |
80 if (context.computeKindOf(source) == SourceKind.PART) continue; | |
81 loader.loadLibrary(uri); | |
82 } | |
83 | |
84 // Process any library that has been referenced and that was not loaded from a | |
85 // summary file. | |
Paul Berry
2016/12/09 13:41:40
This is actually undesirable behavior IMHO. It co
Siggi Cherem (dart-lang)
2016/12/09 16:58:21
Let's chat more about this.
I initially was kin t
Siggi Cherem (dart-lang)
2016/12/09 20:46:40
Updated the code based on an offline discussion. H
| |
86 for (int i = 0; i < repository.libraries.length; ++i) { | |
Paul Berry
2016/12/09 13:41:40
Nit: since `i` is not used, this would be clearer
asgerf
2016/12/09 13:56:53
I think the number of libraries will grow during i
Siggi Cherem (dart-lang)
2016/12/09 16:58:21
Asger is correct - the list is growing as more lib
Siggi Cherem (dart-lang)
2016/12/09 20:46:40
Done, added some documentation.
| |
87 var lib = repository.libraries[i]; | |
88 var source = context.sourceFactory.forUri2(lib.importUri); | |
89 if (source is InSummarySource) continue; | |
90 // Depending on the context we can allow or report an error when a library | |
91 // is loaded here. For command-line tools we want to allow it to | |
92 // make it easy to define build units in terms of a few entry point files. | |
93 // For a closed build system like bazel, we can report an error. | |
94 // TODO(sigmund): expose an option to indicate that this should be an error. | |
95 loader.ensureLibraryIsLoaded(lib); | |
96 } | |
97 | |
71 Program program = new Program(repository.libraries); | 98 Program program = new Program(repository.libraries); |
72 _reportErrors(loader.errors, options.onError); | 99 _reportErrors(loader.errors, options.onError); |
73 return program; | 100 return program; |
74 } | 101 } |
75 | 102 |
103 /// Create a [DartLoader] using the provided [options]. | |
104 /// | |
105 /// If [options] contain no configuration to resolve `.packages`, the [entry] | |
106 /// file will be used to search for a `.packages` file. | |
76 Future<DartLoader> _createLoader(CompilerOptions options, | 107 Future<DartLoader> _createLoader(CompilerOptions options, |
77 {Repository repository}) async { | 108 {Repository repository, Uri entry}) async { |
78 var kernelOptions = _convertOptions(options); | 109 var kernelOptions = _convertOptions(options); |
79 var packages = await createPackages(options.packagesFilePath); | 110 var packages = await createPackages(options.packagesFilePath, |
111 discoveryPath: entry?.path); | |
80 return new DartLoader( | 112 return new DartLoader( |
81 repository ?? new Repository(), kernelOptions, packages); | 113 repository ?? new Repository(), kernelOptions, packages); |
82 } | 114 } |
83 | 115 |
84 DartOptions _convertOptions(CompilerOptions options) { | 116 DartOptions _convertOptions(CompilerOptions options) { |
85 return new DartOptions( | 117 return new DartOptions( |
86 sdk: options.sdkPath, | 118 sdk: options.sdkPath, |
119 sdkSummary: options.sdkSummary, | |
87 packagePath: options.packagesFilePath, | 120 packagePath: options.packagesFilePath, |
88 declaredVariables: options.declaredVariables); | 121 declaredVariables: options.declaredVariables); |
89 } | 122 } |
90 | 123 |
91 void _reportErrors(List errors, ErrorHandler onError) { | 124 void _reportErrors(List errors, ErrorHandler onError) { |
92 if (onError == null) return; | 125 if (onError == null) return; |
93 for (var error in errors) { | 126 for (var error in errors) { |
94 onError(new _DartkError(error)); | 127 onError(new _DartkError(error)); |
95 } | 128 } |
96 } | 129 } |
97 | 130 |
98 // TODO(sigmund): delete this class. Dartk should not format errors itself, we | 131 // TODO(sigmund): delete this class. Dartk should not format errors itself, we |
99 // should just pass them along. | 132 // should just pass them along. |
100 class _DartkError implements CompilationError { | 133 class _DartkError implements CompilationError { |
101 String get correction => null; | 134 String get correction => null; |
102 SourceSpan get span => null; | 135 SourceSpan get span => null; |
103 final String message; | 136 final String message; |
104 _DartkError(this.message); | 137 _DartkError(this.message); |
105 } | 138 } |
OLD | NEW |