5
5
/// Defines the front-end API for converting source code to Dart Kernel objects.
6
6
library front_end.kernel_generator;
7
7
8
- import 'compilation_error.dart' ;
9
8
import 'compiler_options.dart' ;
9
+ import 'dart:async' show Future;
10
10
import 'dart:async' ;
11
-
12
- import 'package:analyzer/src/generated/source.dart' show SourceKind;
13
- import 'package:analyzer/src/generated/engine.dart' show AnalysisOptionsImpl;
14
- import 'package:analyzer/src/summary/package_bundle_reader.dart'
15
- show InSummarySource;
16
- // TODO(sigmund): move loader logic under front_end/lib/src/kernel/
17
- import 'package:analyzer/src/kernel/loader.dart' ;
18
- import 'package:kernel/kernel.dart' ;
19
- import 'package:source_span/source_span.dart' show SourceSpan;
11
+ import 'src/fasta/dill/dill_target.dart' show DillTarget;
12
+ import 'src/fasta/errors.dart' show InputError;
13
+ import 'src/fasta/kernel/kernel_target.dart' show KernelTarget;
14
+ import 'package:kernel/kernel.dart' show Program;
15
+ import 'src/fasta/ticker.dart' show Ticker;
16
+ import 'src/fasta/translate_uri.dart' show TranslateUri;
17
+ import 'src/simple_error.dart' ;
20
18
21
19
/// Generates a kernel representation of the program whose main library is in
22
20
/// the given [source] .
@@ -37,18 +35,54 @@ import 'package:source_span/source_span.dart' show SourceSpan;
37
35
/// take the place of Dart source code (since the Dart source code is still
38
36
/// needed to access the contents of method bodies).
39
37
Future <Program > kernelForProgram (Uri source, CompilerOptions options) async {
40
- var loader = await _createLoader (options, entry: source);
38
+ var fs = options.fileSystem;
39
+
40
+ report (String msg) {
41
+ options.onError (new SimpleError (msg));
42
+ return null ;
43
+ }
41
44
42
- if (options.compileSdk ) {
43
- options.additionalLibraries. forEach (loader.loadLibrary );
45
+ if (! await fs. entityForUri (source). exists () ) {
46
+ return report ( "Entry-point file not found: $ source " );
44
47
}
45
48
46
- // TODO(sigmund): merge what we have in loadEverything and the logic below in
47
- // kernelForBuildUnit so there is a single place where we crawl for
48
- // dependencies.
49
- loader.loadProgram (source, compileSdk: options.compileSdk);
50
- _reportErrors (loader.errors, options.onError);
51
- return loader.program;
49
+ if (! await validateOptions (options)) return null ;
50
+
51
+ try {
52
+ TranslateUri uriTranslator =
53
+ await TranslateUri .parse (null , options.packagesFileUri);
54
+
55
+ var dillTarget =
56
+ new DillTarget (new Ticker (isVerbose: false ), uriTranslator);
57
+ var summary = options.sdkSummary;
58
+ if (summary != null ) dillTarget.read (summary);
59
+
60
+ var kernelTarget =
61
+ new KernelTarget (dillTarget, uriTranslator, options.strongMode);
62
+ kernelTarget.read (source);
63
+
64
+ await dillTarget.writeOutline (null );
65
+ await kernelTarget.writeOutline (null );
66
+ Program program = await kernelTarget.writeProgram (null );
67
+
68
+ if (kernelTarget.errors.isNotEmpty) {
69
+ kernelTarget.errors.forEach ((e) => report ('$e ' ));
70
+ return null ;
71
+ }
72
+
73
+ if (program.mainMethod == null ) {
74
+ return report ("No 'main' method found." );
75
+ }
76
+
77
+ if (! options.compileSdk) {
78
+ // TODO(sigmund): ensure that the result is not including
79
+ // sources for the sdk, only external references.
80
+ }
81
+ return program;
82
+ } on InputError catch (e) {
83
+ options.onError (new SimpleError (e.format ()));
84
+ return null ;
85
+ }
52
86
}
53
87
54
88
/// Generates a kernel representation for a build unit.
@@ -86,103 +120,5 @@ Future<Program> kernelForProgram(Uri source, CompilerOptions options) async {
86
120
/// obtained from?
87
121
Future <Program > kernelForBuildUnit (
88
122
List <Uri > sources, CompilerOptions options) async {
89
- var program = new Program ();
90
- var loader = await _createLoader (options, program: program);
91
- var context = loader.context;
92
-
93
- // Process every library in the build unit.
94
- for (var uri in sources) {
95
- var source = context.sourceFactory.forUri2 (uri);
96
- // We ignore part files, those are handled by their enclosing library.
97
- if (context.computeKindOf (source) == SourceKind .PART ) {
98
- // TODO(sigmund): record it and ensure that this part is within a provided
99
- // library.
100
- continue ;
101
- }
102
- loader.loadLibrary (uri);
103
- }
104
-
105
- // Check whether all dependencies were included in [sources].
106
- // TODO(sigmund): we should look for dependencies using import, export, and
107
- // part directives intead of relying on the dartk-loader. In particular, if a
108
- // library is imported but not used, the logic below will not detect it.
109
- for (int i = 0 ; i < program.libraries.length; ++ i) {
110
- // Note: we don't use a for-in loop because program.libraries grows as
111
- // the loader processes libraries.
112
- var lib = program.libraries[i];
113
- var source = context.sourceFactory.forUri2 (lib.importUri);
114
- if (source is InSummarySource ) continue ;
115
- if (options.chaseDependencies) {
116
- loader.ensureLibraryIsLoaded (lib);
117
- } else if (lib.isExternal) {
118
- // Default behavior: the build should be hermetic and all dependencies
119
- // should be listed.
120
- options.onError (new _DartkError ('hermetic build error: '
121
- 'no source or summary was given for ${lib .importUri }' ));
122
- }
123
- }
124
-
125
- _reportErrors (loader.errors, options.onError);
126
- return program;
127
- }
128
-
129
- /// Create a [DartLoader] using the provided [options] .
130
- ///
131
- /// If [options] contain no configuration to resolve `.packages` , the [entry]
132
- /// file will be used to search for a `.packages` file.
133
- Future <DartLoader > _createLoader (CompilerOptions options,
134
- {Program program, Uri entry}) async {
135
- var kernelOptions = _convertOptions (options);
136
- var packages = await createPackages (_uriToPath (options.packagesFileUri),
137
- discoveryPath: entry? .path);
138
- var loader =
139
- new DartLoader (program ?? new Program (), kernelOptions, packages);
140
- var patchPaths = < String , List <String >> {};
141
-
142
- // TODO(sigmund,paulberry): use ProcessedOptions so that we can resolve the
143
- // URIs correctly even if sdkRoot is inferred and not specified explicitly.
144
- String resolve (Uri patch) => _uriToPath (options.sdkRoot.resolveUri (patch));
145
-
146
- options.targetPatches.forEach ((uri, patches) {
147
- patchPaths['$uri ' ] = patches.map (resolve).toList ();
148
- });
149
- AnalysisOptionsImpl analysisOptions = loader.context.analysisOptions;
150
- analysisOptions.patchPaths = patchPaths;
151
- return loader;
152
- }
153
-
154
- DartOptions _convertOptions (CompilerOptions options) {
155
- return new DartOptions (
156
- strongMode: options.strongMode,
157
- sdk: _uriToPath (options.sdkRoot),
158
- // TODO(sigmund): make it possible to use summaries and still compile the
159
- // sdk sources.
160
- sdkSummary: options.compileSdk ? null : _uriToPath (options.sdkSummary),
161
- packagePath: _uriToPath (options.packagesFileUri),
162
- customUriMappings: options.uriOverride,
163
- declaredVariables: options.declaredVariables);
164
- }
165
-
166
- void _reportErrors (List errors, ErrorHandler onError) {
167
- if (onError == null ) return ;
168
- for (var error in errors) {
169
- onError (new _DartkError (error));
170
- }
171
- }
172
-
173
- String _uriToPath (Uri uri) {
174
- if (uri == null ) return null ;
175
- if (uri.scheme != 'file' ) {
176
- throw new StateError ('Only file URIs are supported: $uri ' );
177
- }
178
- return uri.toFilePath ();
179
- }
180
-
181
- // TODO(sigmund): delete this class. Dartk should not format errors itself, we
182
- // should just pass them along.
183
- class _DartkError implements CompilationError {
184
- String get correction => null ;
185
- SourceSpan get span => null ;
186
- final String message;
187
- _DartkError (this .message);
123
+ throw new UnimplementedError ("kernel for build-unit is not implemented" );
188
124
}
0 commit comments