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 'package:kernel/ast.dart' | 9 import 'package:kernel/ast.dart' |
10 show | 10 show |
11 Arguments, | 11 Arguments, |
| 12 Block, |
12 CanonicalName, | 13 CanonicalName, |
13 Class, | 14 Class, |
14 Constructor, | 15 Constructor, |
15 DartType, | 16 DartType, |
16 DynamicType, | 17 DynamicType, |
17 EmptyStatement, | 18 EmptyStatement, |
18 Expression, | 19 Expression, |
19 ExpressionStatement, | 20 ExpressionStatement, |
20 Field, | 21 Field, |
21 FieldInitializer, | 22 FieldInitializer, |
22 FunctionNode, | 23 FunctionNode, |
23 Initializer, | 24 Initializer, |
24 InvalidInitializer, | 25 InvalidInitializer, |
25 Library, | 26 Library, |
26 ListLiteral, | 27 ListLiteral, |
27 Name, | 28 Name, |
28 NamedExpression, | 29 NamedExpression, |
29 NullLiteral, | 30 NullLiteral, |
30 ProcedureKind, | 31 ProcedureKind, |
31 Program, | 32 Program, |
32 Source, | 33 Source, |
| 34 Statement, |
33 StringLiteral, | 35 StringLiteral, |
34 SuperInitializer, | 36 SuperInitializer, |
35 Throw, | 37 Throw, |
36 TypeParameter, | 38 TypeParameter, |
37 VariableDeclaration, | 39 VariableDeclaration, |
38 VariableGet, | 40 VariableGet, |
39 VoidType; | 41 VoidType; |
40 | 42 |
41 import 'package:kernel/type_algebra.dart' show substitute; | 43 import 'package:kernel/type_algebra.dart' show substitute; |
42 | 44 |
43 import '../../../file_system.dart' show FileSystem; | 45 import '../../../file_system.dart' show FileSystem; |
44 | 46 |
45 import '../compiler_context.dart' show CompilerContext; | 47 import '../compiler_context.dart' show CompilerContext; |
46 | 48 |
47 import '../deprecated_problems.dart' | 49 import '../deprecated_problems.dart' |
48 show | 50 show deprecated_InputError, reportCrash, resetCrashReporting; |
49 deprecated_formatUnexpected, | |
50 deprecated_InputError, | |
51 reportCrash, | |
52 resetCrashReporting; | |
53 | 51 |
54 import '../dill/dill_target.dart' show DillTarget; | 52 import '../dill/dill_target.dart' show DillTarget; |
55 | 53 |
56 import '../messages.dart' | 54 import '../messages.dart' |
57 show | 55 show |
58 LocatedMessage, | 56 LocatedMessage, |
59 messageConstConstructorNonFinalField, | 57 messageConstConstructorNonFinalField, |
60 messageConstConstructorNonFinalFieldCause, | 58 messageConstConstructorNonFinalFieldCause, |
61 templateSuperclassHasNoDefaultConstructor; | 59 templateSuperclassHasNoDefaultConstructor; |
62 | 60 |
63 import '../problems.dart' show unhandled; | 61 import '../problems.dart' show unhandled; |
64 | 62 |
| 63 import '../severity.dart' show Severity; |
| 64 |
65 import '../source/source_class_builder.dart' show SourceClassBuilder; | 65 import '../source/source_class_builder.dart' show SourceClassBuilder; |
66 | 66 |
67 import '../source/source_loader.dart' show SourceLoader; | 67 import '../source/source_loader.dart' show SourceLoader; |
68 | 68 |
69 import '../target_implementation.dart' show TargetImplementation; | 69 import '../target_implementation.dart' show TargetImplementation; |
70 | 70 |
71 import '../uri_translator.dart' show UriTranslator; | 71 import '../uri_translator.dart' show UriTranslator; |
72 | 72 |
73 import '../util/relativize.dart' show relativizeUri; | 73 import '../util/relativize.dart' show relativizeUri; |
74 | 74 |
(...skipping 21 matching lines...) Expand all Loading... |
96 | 96 |
97 final DillTarget dillTarget; | 97 final DillTarget dillTarget; |
98 | 98 |
99 /// Shared with [CompilerContext]. | 99 /// Shared with [CompilerContext]. |
100 final Map<String, Source> uriToSource; | 100 final Map<String, Source> uriToSource; |
101 | 101 |
102 SourceLoader<Library> loader; | 102 SourceLoader<Library> loader; |
103 | 103 |
104 Program program; | 104 Program program; |
105 | 105 |
106 final List<String> errors = <String>[]; | 106 final List<LocatedMessage> errors = <LocatedMessage>[]; |
107 | 107 |
108 final TypeBuilder dynamicType = | 108 final TypeBuilder dynamicType = |
109 new KernelNamedTypeBuilder("dynamic", null, -1, null); | 109 new KernelNamedTypeBuilder("dynamic", null, -1, null); |
110 | 110 |
111 bool get strongMode => backendTarget.strongMode; | 111 bool get strongMode => backendTarget.strongMode; |
112 | 112 |
113 bool get disableTypeInference => backendTarget.disableTypeInference; | 113 bool get disableTypeInference => backendTarget.disableTypeInference; |
114 | 114 |
115 KernelTarget( | 115 KernelTarget( |
116 this.fileSystem, DillTarget dillTarget, UriTranslator uriTranslator, | 116 this.fileSystem, DillTarget dillTarget, UriTranslator uriTranslator, |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 cls.mixedInType = null; | 211 cls.mixedInType = null; |
212 builder.supertype = new KernelNamedTypeBuilder("Object", null, | 212 builder.supertype = new KernelNamedTypeBuilder("Object", null, |
213 builder.charOffset, builder.fileUri ?? Uri.parse(cls.fileUri)) | 213 builder.charOffset, builder.fileUri ?? Uri.parse(cls.fileUri)) |
214 ..builder = objectClassBuilder; | 214 ..builder = objectClassBuilder; |
215 builder.interfaces = null; | 215 builder.interfaces = null; |
216 builder.mixedInType = null; | 216 builder.mixedInType = null; |
217 } | 217 } |
218 | 218 |
219 void handleInputError(deprecated_InputError error, {bool isFullProgram}) { | 219 void handleInputError(deprecated_InputError error, {bool isFullProgram}) { |
220 if (error != null) { | 220 if (error != null) { |
221 String message = error.deprecated_format(); | 221 LocatedMessage message = deprecated_InputError.toMessage(error); |
222 print(message); | 222 context.report(message, Severity.error); |
223 errors.add(message); | 223 errors.add(message); |
224 } | 224 } |
225 program = erroneousProgram(isFullProgram); | 225 program = erroneousProgram(isFullProgram); |
226 } | 226 } |
227 | 227 |
228 @override | 228 @override |
229 Future<Program> buildOutlines({CanonicalName nameRoot}) async { | 229 Future<Program> buildOutlines({CanonicalName nameRoot}) async { |
230 if (loader.first == null) return null; | 230 if (loader.first == null) return null; |
231 try { | 231 try { |
232 loader.createTypeInferenceEngine(); | 232 loader.createTypeInferenceEngine(); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 /// replace [program]. | 302 /// replace [program]. |
303 void handleRecoverableErrors(List<LocatedMessage> recoverableErrors) { | 303 void handleRecoverableErrors(List<LocatedMessage> recoverableErrors) { |
304 if (recoverableErrors.isEmpty) return; | 304 if (recoverableErrors.isEmpty) return; |
305 KernelLibraryBuilder mainLibrary = loader.first; | 305 KernelLibraryBuilder mainLibrary = loader.first; |
306 if (mainLibrary == null) { | 306 if (mainLibrary == null) { |
307 program = erroneousProgram(true); | 307 program = erroneousProgram(true); |
308 return; | 308 return; |
309 } | 309 } |
310 List<Expression> expressions = <Expression>[]; | 310 List<Expression> expressions = <Expression>[]; |
311 for (LocatedMessage error in recoverableErrors) { | 311 for (LocatedMessage error in recoverableErrors) { |
312 String message = deprecated_formatUnexpected( | 312 errors.add(error); |
313 error.uri, error.charOffset, error.message); | 313 expressions.add(new StringLiteral(context.format(error, Severity.error))); |
314 errors.add(message); | |
315 expressions.add(new StringLiteral(message)); | |
316 } | 314 } |
317 mainLibrary.library.addMember(new Field(new Name("#errors"), | 315 mainLibrary.library.addMember(new Field(new Name("#errors"), |
318 initializer: new ListLiteral(expressions, isConst: true), | 316 initializer: new ListLiteral(expressions, isConst: true), |
319 isConst: true)); | 317 isConst: true)); |
320 } | 318 } |
321 | 319 |
322 Program erroneousProgram(bool isFullProgram) { | 320 Program erroneousProgram(bool isFullProgram) { |
323 Uri uri = loader.first?.uri ?? Uri.parse("error:error"); | 321 Uri uri = loader.first?.uri ?? Uri.parse("error:error"); |
324 Uri fileUri = loader.first?.fileUri ?? uri; | 322 Uri fileUri = loader.first?.fileUri ?? uri; |
325 KernelLibraryBuilder library = | 323 KernelLibraryBuilder library = |
326 new KernelLibraryBuilder(uri, fileUri, loader, false); | 324 new KernelLibraryBuilder(uri, fileUri, loader, false); |
327 loader.first = library; | 325 loader.first = library; |
328 if (isFullProgram) { | 326 if (isFullProgram) { |
329 // If this is an outline, we shouldn't add an executable main | 327 // If this is an outline, we shouldn't add an executable main |
330 // method. Similarly considerations apply to separate compilation. It | 328 // method. Similarly considerations apply to separate compilation. It |
331 // could also make sense to add a way to mark .dill files as having | 329 // could also make sense to add a way to mark .dill files as having |
332 // compile-time errors. | 330 // compile-time errors. |
333 KernelProcedureBuilder mainBuilder = new KernelProcedureBuilder(null, 0, | 331 KernelProcedureBuilder mainBuilder = new KernelProcedureBuilder(null, 0, |
334 null, "main", null, null, ProcedureKind.Method, library, -1, -1, -1); | 332 null, "main", null, null, ProcedureKind.Method, library, -1, -1, -1); |
335 library.addBuilder(mainBuilder.name, mainBuilder, -1); | 333 library.addBuilder(mainBuilder.name, mainBuilder, -1); |
336 mainBuilder.body = new ExpressionStatement( | 334 mainBuilder.body = new Block(new List<Statement>.from(errors.map( |
337 new Throw(new StringLiteral("${errors.join('\n')}"))); | 335 (LocatedMessage message) => new ExpressionStatement(new Throw( |
| 336 new StringLiteral(context.format(message, Severity.error))))))); |
338 } | 337 } |
339 library.build(loader.coreLibrary); | 338 library.build(loader.coreLibrary); |
340 return link(<Library>[library.library]); | 339 return link(<Library>[library.library]); |
341 } | 340 } |
342 | 341 |
343 /// Creates a program by combining [libraries] with the libraries of | 342 /// Creates a program by combining [libraries] with the libraries of |
344 /// `dillTarget.loader.program`. | 343 /// `dillTarget.loader.program`. |
345 Program link(List<Library> libraries, {CanonicalName nameRoot}) { | 344 Program link(List<Library> libraries, {CanonicalName nameRoot}) { |
346 Map<String, Source> uriToSource = | 345 Map<String, Source> uriToSource = |
347 new Map<String, Source>.from(this.uriToSource); | 346 new Map<String, Source>.from(this.uriToSource); |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
626 /// first time. | 625 /// first time. |
627 void runBuildTransformations() { | 626 void runBuildTransformations() { |
628 backendTarget.performModularTransformationsOnLibraries( | 627 backendTarget.performModularTransformationsOnLibraries( |
629 loader.coreTypes, loader.hierarchy, loader.libraries, | 628 loader.coreTypes, loader.hierarchy, loader.libraries, |
630 logger: (String msg) => ticker.logMs(msg)); | 629 logger: (String msg) => ticker.logMs(msg)); |
631 backendTarget.performGlobalTransformations(loader.coreTypes, program, | 630 backendTarget.performGlobalTransformations(loader.coreTypes, program, |
632 logger: (String msg) => ticker.logMs(msg)); | 631 logger: (String msg) => ticker.logMs(msg)); |
633 } | 632 } |
634 | 633 |
635 void verify() { | 634 void verify() { |
636 var verifyErrors = verifyProgram(program); | 635 errors.addAll(verifyProgram(program)); |
637 errors.addAll(verifyErrors.map((error) => error.message)); | |
638 ticker.logMs("Verified program"); | 636 ticker.logMs("Verified program"); |
639 } | 637 } |
640 | 638 |
641 /// Return `true` if the given [library] was built by this [KernelTarget] | 639 /// Return `true` if the given [library] was built by this [KernelTarget] |
642 /// from sources, and not loaded from a [DillTarget]. | 640 /// from sources, and not loaded from a [DillTarget]. |
643 bool isSourceLibrary(Library library) { | 641 bool isSourceLibrary(Library library) { |
644 return loader.libraries.contains(library); | 642 return loader.libraries.contains(library); |
645 } | 643 } |
646 } | 644 } |
647 | 645 |
648 /// Looks for a constructor call that matches `super()` from a constructor in | 646 /// Looks for a constructor call that matches `super()` from a constructor in |
649 /// [cls]. Such a constructor may have optional arguments, but no required | 647 /// [cls]. Such a constructor may have optional arguments, but no required |
650 /// arguments. | 648 /// arguments. |
651 Constructor defaultSuperConstructor(Class cls) { | 649 Constructor defaultSuperConstructor(Class cls) { |
652 Class superclass = cls.superclass; | 650 Class superclass = cls.superclass; |
653 while (superclass != null && superclass.isMixinApplication) { | 651 while (superclass != null && superclass.isMixinApplication) { |
654 superclass = superclass.superclass; | 652 superclass = superclass.superclass; |
655 } | 653 } |
656 for (Constructor constructor in superclass.constructors) { | 654 for (Constructor constructor in superclass.constructors) { |
657 if (constructor.name.name.isEmpty) { | 655 if (constructor.name.name.isEmpty) { |
658 return constructor.function.requiredParameterCount == 0 | 656 return constructor.function.requiredParameterCount == 0 |
659 ? constructor | 657 ? constructor |
660 : null; | 658 : null; |
661 } | 659 } |
662 } | 660 } |
663 return null; | 661 return null; |
664 } | 662 } |
OLD | NEW |