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, IOSink; |
10 | 10 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 show | 74 show |
75 Builder, | 75 Builder, |
76 ClassBuilder, | 76 ClassBuilder, |
77 InvalidTypeBuilder, | 77 InvalidTypeBuilder, |
78 KernelClassBuilder, | 78 KernelClassBuilder, |
79 KernelLibraryBuilder, | 79 KernelLibraryBuilder, |
80 KernelNamedTypeBuilder, | 80 KernelNamedTypeBuilder, |
81 KernelProcedureBuilder, | 81 KernelProcedureBuilder, |
82 LibraryBuilder, | 82 LibraryBuilder, |
83 MemberBuilder, | 83 MemberBuilder, |
84 MixinApplicationBuilder, | |
85 NamedMixinApplicationBuilder, | |
86 NamedTypeBuilder, | 84 NamedTypeBuilder, |
87 TypeBuilder, | 85 TypeBuilder, |
| 86 TypeDeclarationBuilder, |
88 TypeVariableBuilder; | 87 TypeVariableBuilder; |
89 | 88 |
90 import 'verifier.dart' show verifyProgram; | 89 import 'verifier.dart' show verifyProgram; |
91 | 90 |
92 class KernelTarget extends TargetImplementation { | 91 class KernelTarget extends TargetImplementation { |
93 /// The [FileSystem] which should be used to access files. | 92 /// The [FileSystem] which should be used to access files. |
94 final FileSystem fileSystem; | 93 final FileSystem fileSystem; |
95 | 94 |
96 final bool strongMode; | 95 final bool strongMode; |
97 | 96 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 LibraryBuilder createLibraryBuilder(Uri uri, Uri fileUri) { | 140 LibraryBuilder createLibraryBuilder(Uri uri, Uri fileUri) { |
142 if (dillTarget.isLoaded) { | 141 if (dillTarget.isLoaded) { |
143 var builder = dillTarget.loader.builders[uri]; | 142 var builder = dillTarget.loader.builders[uri]; |
144 if (builder != null) { | 143 if (builder != null) { |
145 return builder; | 144 return builder; |
146 } | 145 } |
147 } | 146 } |
148 return new KernelLibraryBuilder(uri, fileUri, loader); | 147 return new KernelLibraryBuilder(uri, fileUri, loader); |
149 } | 148 } |
150 | 149 |
| 150 void forEachDirectSupertype(ClassBuilder cls, void f(NamedTypeBuilder type)) { |
| 151 TypeBuilder supertype = cls.supertype; |
| 152 if (supertype is NamedTypeBuilder) { |
| 153 f(supertype); |
| 154 } else if (supertype != null) { |
| 155 internalError("Unhandled: ${supertype.runtimeType}"); |
| 156 } |
| 157 if (cls.interfaces != null) { |
| 158 for (NamedTypeBuilder t in cls.interfaces) { |
| 159 f(t); |
| 160 } |
| 161 } |
| 162 if (cls.library.loader == loader && |
| 163 // TODO(ahe): Implement DillClassBuilder.mixedInType and remove the |
| 164 // above check. |
| 165 cls.mixedInType != null) { |
| 166 f(cls.mixedInType); |
| 167 } |
| 168 } |
| 169 |
151 void addDirectSupertype(ClassBuilder cls, Set<ClassBuilder> set) { | 170 void addDirectSupertype(ClassBuilder cls, Set<ClassBuilder> set) { |
152 if (cls == null) return; | 171 if (cls == null) return; |
153 TypeBuilder supertype = cls.supertype; | 172 forEachDirectSupertype(cls, (NamedTypeBuilder type) { |
154 add(NamedTypeBuilder type) { | |
155 Builder builder = type.builder; | 173 Builder builder = type.builder; |
156 if (builder is ClassBuilder) { | 174 if (builder is ClassBuilder) { |
157 set.add(builder); | 175 set.add(builder); |
158 } | 176 } |
159 } | 177 }); |
160 | |
161 if (supertype == null) { | |
162 // OK. | |
163 } else if (supertype is MixinApplicationBuilder) { | |
164 add(supertype.supertype); | |
165 for (NamedTypeBuilder t in supertype.mixins) { | |
166 add(t); | |
167 } | |
168 } else if (supertype is NamedTypeBuilder) { | |
169 add(supertype); | |
170 } else { | |
171 internalError("Unhandled: ${supertype.runtimeType}"); | |
172 } | |
173 if (cls.interfaces != null) { | |
174 for (NamedTypeBuilder t in cls.interfaces) { | |
175 add(t); | |
176 } | |
177 } | |
178 } | 178 } |
179 | 179 |
180 List<ClassBuilder> collectAllClasses() { | 180 List<ClassBuilder> collectAllClasses() { |
181 List<ClassBuilder> result = <ClassBuilder>[]; | 181 List<ClassBuilder> result = <ClassBuilder>[]; |
182 loader.builders.forEach((Uri uri, LibraryBuilder library) { | 182 loader.builders.forEach((Uri uri, LibraryBuilder library) { |
183 library.forEach((String name, Builder member) { | 183 library.forEach((String name, Builder member) { |
184 if (member is KernelClassBuilder) { | 184 if (member is KernelClassBuilder) { |
185 result.add(member); | 185 result.add(member); |
186 } | 186 } |
187 }); | 187 }); |
(...skipping 19 matching lines...) Expand all Loading... |
207 | 207 |
208 void breakCycle(ClassBuilder builder) { | 208 void breakCycle(ClassBuilder builder) { |
209 Class cls = builder.target; | 209 Class cls = builder.target; |
210 cls.implementedTypes.clear(); | 210 cls.implementedTypes.clear(); |
211 cls.supertype = null; | 211 cls.supertype = null; |
212 cls.mixedInType = null; | 212 cls.mixedInType = null; |
213 builder.supertype = new KernelNamedTypeBuilder("Object", null, | 213 builder.supertype = new KernelNamedTypeBuilder("Object", null, |
214 builder.charOffset, builder.fileUri ?? Uri.parse(cls.fileUri)) | 214 builder.charOffset, builder.fileUri ?? Uri.parse(cls.fileUri)) |
215 ..builder = objectClassBuilder; | 215 ..builder = objectClassBuilder; |
216 builder.interfaces = null; | 216 builder.interfaces = null; |
| 217 builder.mixedInType = null; |
217 } | 218 } |
218 | 219 |
219 Future<Program> handleInputError(Uri uri, InputError error, | 220 Future<Program> handleInputError(Uri uri, InputError error, |
220 {bool isFullProgram}) { | 221 {bool isFullProgram}) { |
221 if (error != null) { | 222 if (error != null) { |
222 String message = error.format(); | 223 String message = error.format(); |
223 print(message); | 224 print(message); |
224 errors.add(message); | 225 errors.add(message); |
225 } | 226 } |
226 program = erroneousProgram(isFullProgram); | 227 program = erroneousProgram(isFullProgram); |
227 return uri == null | 228 return uri == null |
228 ? new Future<Program>.value(program) | 229 ? new Future<Program>.value(program) |
229 : writeLinkedProgram(uri, program, isFullProgram: isFullProgram); | 230 : writeLinkedProgram(uri, program, isFullProgram: isFullProgram); |
230 } | 231 } |
231 | 232 |
232 Future<Program> writeOutline(Uri uri) async { | 233 Future<Program> writeOutline(Uri uri) async { |
233 if (loader.first == null) return null; | 234 if (loader.first == null) return null; |
234 try { | 235 try { |
235 loader.createTypeInferenceEngine(); | 236 loader.createTypeInferenceEngine(); |
236 await loader.buildOutlines(); | 237 await loader.buildOutlines(); |
237 loader.coreLibrary | 238 loader.coreLibrary |
238 .becomeCoreLibrary(const DynamicType(), const VoidType()); | 239 .becomeCoreLibrary(const DynamicType(), const VoidType()); |
239 dynamicType.bind(loader.coreLibrary["dynamic"]); | 240 dynamicType.bind(loader.coreLibrary["dynamic"]); |
240 loader.resolveParts(); | 241 loader.resolveParts(); |
241 loader.computeLibraryScopes(); | 242 loader.computeLibraryScopes(); |
242 loader.resolveTypes(); | 243 loader.resolveTypes(); |
| 244 loader.checkSemantics(); |
243 loader.buildProgram(); | 245 loader.buildProgram(); |
244 loader.checkSemantics(); | |
245 List<SourceClassBuilder> sourceClasses = collectAllSourceClasses(); | 246 List<SourceClassBuilder> sourceClasses = collectAllSourceClasses(); |
246 installDefaultSupertypes(); | 247 installDefaultSupertypes(); |
247 installDefaultConstructors(sourceClasses); | 248 installDefaultConstructors(sourceClasses); |
248 loader.resolveConstructors(); | 249 loader.resolveConstructors(); |
249 loader.finishTypeVariables(objectClassBuilder); | 250 loader.finishTypeVariables(objectClassBuilder); |
250 program = link(new List<Library>.from(loader.libraries)); | 251 program = link(new List<Library>.from(loader.libraries)); |
251 loader.computeHierarchy(program); | 252 loader.computeHierarchy(program); |
252 loader.checkOverrides(sourceClasses); | 253 loader.checkOverrides(sourceClasses); |
253 loader.prepareInitializerInference(); | 254 loader.prepareInitializerInference(); |
254 loader.performInitializerInference(); | 255 loader.performInitializerInference(); |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 } | 441 } |
441 ticker.logMs("Installed default constructors"); | 442 ticker.logMs("Installed default constructors"); |
442 } | 443 } |
443 | 444 |
444 KernelClassBuilder get objectClassBuilder => loader.coreLibrary["Object"]; | 445 KernelClassBuilder get objectClassBuilder => loader.coreLibrary["Object"]; |
445 | 446 |
446 Class get objectClass => objectClassBuilder.cls; | 447 Class get objectClass => objectClassBuilder.cls; |
447 | 448 |
448 /// If [builder] doesn't have a constructors, install the defaults. | 449 /// If [builder] doesn't have a constructors, install the defaults. |
449 void installDefaultConstructor(SourceClassBuilder builder) { | 450 void installDefaultConstructor(SourceClassBuilder builder) { |
450 if (builder.cls.isMixinApplication) { | 451 if (builder.isMixinApplication && !builder.isNamedMixinApplication) return; |
451 // We have to test if builder.cls is a mixin application. [builder] may | |
452 // think it's a mixin application, but if its mixed-in type couldn't be | |
453 // resolved, the target class won't be a mixin application and we need | |
454 // to add a default constructor to complete error recovery. | |
455 return; | |
456 } | |
457 if (builder.constructors.local.isNotEmpty) return; | 452 if (builder.constructors.local.isNotEmpty) return; |
458 | 453 |
459 /// Quotes below are from [Dart Programming Language Specification, 4th | 454 /// Quotes below are from [Dart Programming Language Specification, 4th |
460 /// Edition]( | 455 /// Edition]( |
461 /// https://ecma-international.org/publications/files/ECMA-ST/ECMA-408.pdf): | 456 /// https://ecma-international.org/publications/files/ECMA-ST/ECMA-408.pdf): |
462 if (builder is NamedMixinApplicationBuilder) { | 457 if (builder.isNamedMixinApplication) { |
463 /// >A mixin application of the form S with M; defines a class C with | 458 /// >A mixin application of the form S with M; defines a class C with |
464 /// >superclass S. | 459 /// >superclass S. |
465 /// >... | 460 /// >... |
466 | 461 |
467 /// >Let LM be the library in which M is declared. For each generative | 462 /// >Let LM be the library in which M is declared. For each generative |
468 /// >constructor named qi(Ti1 ai1, . . . , Tiki aiki), i in 1..n of S | 463 /// >constructor named qi(Ti1 ai1, . . . , Tiki aiki), i in 1..n of S |
469 /// >that is accessible to LM , C has an implicitly declared constructor | 464 /// >that is accessible to LM , C has an implicitly declared constructor |
470 /// >named q'i = [C/S]qi of the form q'i(ai1,...,aiki) : | 465 /// >named q'i = [C/S]qi of the form q'i(ai1,...,aiki) : |
471 /// >super(ai1,...,aiki);. | 466 /// >super(ai1,...,aiki);. |
472 Builder supertype = builder; | 467 TypeDeclarationBuilder supertype = builder; |
473 while (supertype is NamedMixinApplicationBuilder) { | 468 while (supertype.isMixinApplication) { |
474 NamedMixinApplicationBuilder named = supertype; | 469 SourceClassBuilder named = supertype; |
475 TypeBuilder type = named.mixinApplication; | 470 TypeBuilder type = named.supertype; |
476 if (type is MixinApplicationBuilder) { | |
477 MixinApplicationBuilder t = type; | |
478 type = t.supertype; | |
479 } | |
480 if (type is NamedTypeBuilder) { | 471 if (type is NamedTypeBuilder) { |
481 supertype = type.builder; | 472 supertype = type.builder; |
482 } else { | 473 } else { |
483 internalError("Unhandled: ${type.runtimeType}"); | 474 internalError("Unhandled: ${type.runtimeType}"); |
484 } | 475 } |
485 } | 476 } |
486 if (supertype is KernelClassBuilder) { | 477 if (supertype is KernelClassBuilder) { |
487 Map<TypeParameter, DartType> substitutionMap = | 478 Map<TypeParameter, DartType> substitutionMap = |
488 computeKernelSubstitutionMap( | 479 computeKernelSubstitutionMap( |
489 builder.getSubstitutionMap(supertype, builder.fileUri, | 480 builder.getSubstitutionMap(supertype, builder.fileUri, |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
727 } | 718 } |
728 for (Constructor constructor in superclass.constructors) { | 719 for (Constructor constructor in superclass.constructors) { |
729 if (constructor.name.name.isEmpty) { | 720 if (constructor.name.name.isEmpty) { |
730 return constructor.function.requiredParameterCount == 0 | 721 return constructor.function.requiredParameterCount == 0 |
731 ? constructor | 722 ? constructor |
732 : null; | 723 : null; |
733 } | 724 } |
734 } | 725 } |
735 return null; | 726 return null; |
736 } | 727 } |
OLD | NEW |