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 library fasta.kernel_target; | 5 library fasta.kernel_target; |
6 | 6 |
7 import 'dart:async' show Future; | 7 import 'dart:async' show Future; |
8 | 8 |
9 import 'dart:io' show File, IOSink; | 9 import 'dart:io' show File; |
10 | 10 |
11 import 'package:front_end/file_system.dart'; | 11 import 'package:front_end/file_system.dart'; |
12 import 'package:kernel/ast.dart' | 12 import 'package:kernel/ast.dart' |
13 show | 13 show |
14 Arguments, | 14 Arguments, |
15 AsyncMarker, | 15 AsyncMarker, |
16 CanonicalName, | 16 CanonicalName, |
17 Class, | 17 Class, |
18 Constructor, | 18 Constructor, |
19 DartType, | 19 DartType, |
(...skipping 14 matching lines...) Expand all Loading... |
34 Program, | 34 Program, |
35 Source, | 35 Source, |
36 StringLiteral, | 36 StringLiteral, |
37 SuperInitializer, | 37 SuperInitializer, |
38 Throw, | 38 Throw, |
39 TypeParameter, | 39 TypeParameter, |
40 VariableDeclaration, | 40 VariableDeclaration, |
41 VariableGet, | 41 VariableGet, |
42 VoidType; | 42 VoidType; |
43 | 43 |
44 import 'package:kernel/binary/ast_to_binary.dart' show BinaryPrinter; | |
45 | |
46 import 'package:kernel/transformations/erasure.dart' show Erasure; | 44 import 'package:kernel/transformations/erasure.dart' show Erasure; |
47 | 45 |
48 import 'package:kernel/transformations/continuation.dart' as transformAsync; | 46 import 'package:kernel/transformations/continuation.dart' as transformAsync; |
49 | 47 |
50 import 'package:kernel/transformations/mixin_full_resolution.dart' | 48 import 'package:kernel/transformations/mixin_full_resolution.dart' |
51 show MixinFullResolution; | 49 show MixinFullResolution; |
52 | 50 |
53 import 'package:kernel/type_algebra.dart' show substitute; | 51 import 'package:kernel/type_algebra.dart' show substitute; |
54 | 52 |
55 import '../source/source_loader.dart' show SourceLoader; | 53 import '../source/source_loader.dart' show SourceLoader; |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 cls.implementedTypes.clear(); | 208 cls.implementedTypes.clear(); |
211 cls.supertype = null; | 209 cls.supertype = null; |
212 cls.mixedInType = null; | 210 cls.mixedInType = null; |
213 builder.supertype = new KernelNamedTypeBuilder("Object", null, | 211 builder.supertype = new KernelNamedTypeBuilder("Object", null, |
214 builder.charOffset, builder.fileUri ?? Uri.parse(cls.fileUri)) | 212 builder.charOffset, builder.fileUri ?? Uri.parse(cls.fileUri)) |
215 ..builder = objectClassBuilder; | 213 ..builder = objectClassBuilder; |
216 builder.interfaces = null; | 214 builder.interfaces = null; |
217 builder.mixedInType = null; | 215 builder.mixedInType = null; |
218 } | 216 } |
219 | 217 |
220 Future<Program> handleInputError(Uri uri, InputError error, | 218 void handleInputError(InputError error, {bool isFullProgram}) { |
221 {bool isFullProgram}) { | |
222 if (error != null) { | 219 if (error != null) { |
223 String message = error.format(); | 220 String message = error.format(); |
224 print(message); | 221 print(message); |
225 errors.add(message); | 222 errors.add(message); |
226 } | 223 } |
227 _program = erroneousProgram(isFullProgram); | 224 _program = erroneousProgram(isFullProgram); |
228 return uri == null | |
229 ? new Future<Program>.value(_program) | |
230 : writeLinkedProgram(uri, _program, isFullProgram: isFullProgram); | |
231 } | 225 } |
232 | 226 |
233 @override | 227 @override |
234 Future<Program> buildOutlines({CanonicalName nameRoot}) async { | 228 Future<Program> buildOutlines({CanonicalName nameRoot}) async { |
235 if (loader.first == null) return null; | 229 if (loader.first == null) return null; |
236 try { | 230 try { |
237 loader.createTypeInferenceEngine(); | 231 loader.createTypeInferenceEngine(); |
238 await loader.buildOutlines(); | 232 await loader.buildOutlines(); |
239 loader.coreLibrary | 233 loader.coreLibrary |
240 .becomeCoreLibrary(const DynamicType(), const VoidType()); | 234 .becomeCoreLibrary(const DynamicType(), const VoidType()); |
241 dynamicType.bind(loader.coreLibrary["dynamic"]); | 235 dynamicType.bind(loader.coreLibrary["dynamic"]); |
242 loader.resolveParts(); | 236 loader.resolveParts(); |
243 loader.computeLibraryScopes(); | 237 loader.computeLibraryScopes(); |
244 loader.resolveTypes(); | 238 loader.resolveTypes(); |
245 loader.checkSemantics(); | 239 loader.checkSemantics(); |
246 loader.buildProgram(); | 240 loader.buildProgram(); |
247 List<SourceClassBuilder> sourceClasses = collectAllSourceClasses(); | 241 List<SourceClassBuilder> sourceClasses = collectAllSourceClasses(); |
248 installDefaultSupertypes(); | 242 installDefaultSupertypes(); |
249 installDefaultConstructors(sourceClasses); | 243 installDefaultConstructors(sourceClasses); |
250 loader.resolveConstructors(); | 244 loader.resolveConstructors(); |
251 loader.finishTypeVariables(objectClassBuilder); | 245 loader.finishTypeVariables(objectClassBuilder); |
252 _program = | 246 _program = |
253 link(new List<Library>.from(loader.libraries), nameRoot: nameRoot); | 247 link(new List<Library>.from(loader.libraries), nameRoot: nameRoot); |
254 loader.computeHierarchy(_program); | 248 loader.computeHierarchy(_program); |
255 loader.checkOverrides(sourceClasses); | 249 loader.checkOverrides(sourceClasses); |
256 loader.prepareInitializerInference(); | 250 loader.prepareInitializerInference(); |
257 loader.performInitializerInference(); | 251 loader.performInitializerInference(); |
258 return _program; | |
259 } on InputError catch (e) { | 252 } on InputError catch (e) { |
260 return handleInputError(null, e, isFullProgram: false); | 253 handleInputError(e, isFullProgram: false); |
261 } catch (e, s) { | 254 } catch (e, s) { |
262 return reportCrash(e, s, loader?.currentUriForCrashReporting); | 255 return reportCrash(e, s, loader?.currentUriForCrashReporting); |
263 } | 256 } |
264 } | 257 return _program; |
265 | |
266 Future<Null> writeOutline(Uri uri) async { | |
267 try { | |
268 if (uri != null) { | |
269 await writeLinkedProgram(uri, _program, isFullProgram: false); | |
270 } | |
271 } on InputError catch (e) { | |
272 handleInputError(uri, e, isFullProgram: false); | |
273 } catch (e, s) { | |
274 reportCrash(e, s, loader?.currentUriForCrashReporting); | |
275 } | |
276 } | 258 } |
277 | 259 |
278 @override | 260 @override |
279 Future<Program> buildProgram({bool verify: false}) async { | 261 Future<Program> buildProgram({bool verify: false}) async { |
280 if (loader.first == null) return null; | 262 if (loader.first == null) return null; |
281 if (errors.isNotEmpty) { | 263 if (errors.isNotEmpty) { |
282 return handleInputError(null, null, isFullProgram: true); | 264 handleInputError(null, isFullProgram: true); |
| 265 return _program; |
283 } | 266 } |
284 try { | 267 try { |
285 await loader.buildBodies(); | 268 await loader.buildBodies(); |
286 loader.finishStaticInvocations(); | 269 loader.finishStaticInvocations(); |
287 finishAllConstructors(); | 270 finishAllConstructors(); |
288 loader.finishNativeMethods(); | 271 loader.finishNativeMethods(); |
289 runBuildTransformations(); | 272 runBuildTransformations(); |
290 | 273 |
291 if (verify) this.verify(); | 274 if (verify) this.verify(); |
292 errors.addAll(loader.collectCompileTimeErrors().map((e) => e.format())); | 275 errors.addAll(loader.collectCompileTimeErrors().map((e) => e.format())); |
293 if (errors.isNotEmpty) { | 276 if (errors.isNotEmpty) { |
294 return handleInputError(null, null, isFullProgram: true); | 277 handleInputError(null, isFullProgram: true); |
295 } | 278 } |
296 return _program; | |
297 } on InputError catch (e) { | 279 } on InputError catch (e) { |
298 return handleInputError(null, e, isFullProgram: true); | 280 handleInputError(e, isFullProgram: true); |
299 } catch (e, s) { | 281 } catch (e, s) { |
300 return reportCrash(e, s, loader?.currentUriForCrashReporting); | 282 return reportCrash(e, s, loader?.currentUriForCrashReporting); |
301 } | 283 } |
302 } | 284 return _program; |
303 | |
304 Future<Null> writeProgram(Uri uri) async { | |
305 if (loader.first == null) return null; | |
306 try { | |
307 await writeLinkedProgram(uri, _program, isFullProgram: true); | |
308 } on InputError catch (e) { | |
309 return handleInputError(uri, e, isFullProgram: true); | |
310 } catch (e, s) { | |
311 return reportCrash(e, s, loader?.currentUriForCrashReporting); | |
312 } | |
313 } | 285 } |
314 | 286 |
315 Future writeDepsFile(Uri output, Uri depsFile, | 287 Future writeDepsFile(Uri output, Uri depsFile, |
316 {Iterable<Uri> extraDependencies}) async { | 288 {Iterable<Uri> extraDependencies}) async { |
317 String toRelativeFilePath(Uri uri) { | 289 String toRelativeFilePath(Uri uri) { |
318 // Ninja expects to find file names relative to the current working | 290 // Ninja expects to find file names relative to the current working |
319 // directory. We've tried making them relative to the deps file, but that | 291 // directory. We've tried making them relative to the deps file, but that |
320 // doesn't work for downstream projects. Making them absolute also | 292 // doesn't work for downstream projects. Making them absolute also |
321 // doesn't work. | 293 // doesn't work. |
322 // | 294 // |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 program.mainMethod = builder.procedure; | 382 program.mainMethod = builder.procedure; |
411 } | 383 } |
412 } | 384 } |
413 if (errors.isEmpty || dillTarget.isLoaded) { | 385 if (errors.isEmpty || dillTarget.isLoaded) { |
414 runLinkTransformations(program); | 386 runLinkTransformations(program); |
415 } | 387 } |
416 ticker.logMs("Linked program"); | 388 ticker.logMs("Linked program"); |
417 return program; | 389 return program; |
418 } | 390 } |
419 | 391 |
420 Future<Program> writeLinkedProgram(Uri uri, Program program, | |
421 {bool isFullProgram}) async { | |
422 File output = new File.fromUri(uri); | |
423 IOSink sink = output.openWrite(); | |
424 try { | |
425 new BinaryPrinter(sink).writeProgramFile(program); | |
426 program.unbindCanonicalNames(); | |
427 } finally { | |
428 await sink.close(); | |
429 } | |
430 if (isFullProgram) { | |
431 ticker.logMs("Wrote program to ${uri.toFilePath()}"); | |
432 } else { | |
433 ticker.logMs("Wrote outline to ${uri.toFilePath()}"); | |
434 } | |
435 return null; | |
436 } | |
437 | |
438 void installDefaultSupertypes() { | 392 void installDefaultSupertypes() { |
439 Class objectClass = this.objectClass; | 393 Class objectClass = this.objectClass; |
440 loader.builders.forEach((Uri uri, LibraryBuilder library) { | 394 loader.builders.forEach((Uri uri, LibraryBuilder library) { |
441 library.forEach((String name, Builder builder) { | 395 library.forEach((String name, Builder builder) { |
442 if (builder is SourceClassBuilder) { | 396 if (builder is SourceClassBuilder) { |
443 Class cls = builder.target; | 397 Class cls = builder.target; |
444 if (cls != objectClass) { | 398 if (cls != objectClass) { |
445 cls.supertype ??= objectClass.asRawSupertype; | 399 cls.supertype ??= objectClass.asRawSupertype; |
446 } | 400 } |
447 if (builder.isMixinApplication) { | 401 if (builder.isMixinApplication) { |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 } | 686 } |
733 for (Constructor constructor in superclass.constructors) { | 687 for (Constructor constructor in superclass.constructors) { |
734 if (constructor.name.name.isEmpty) { | 688 if (constructor.name.name.isEmpty) { |
735 return constructor.function.requiredParameterCount == 0 | 689 return constructor.function.requiredParameterCount == 0 |
736 ? constructor | 690 ? constructor |
737 : null; | 691 : null; |
738 } | 692 } |
739 } | 693 } |
740 return null; | 694 return null; |
741 } | 695 } |
OLD | NEW |