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.source_loader; | 5 library fasta.source_loader; |
6 | 6 |
7 import 'dart:async' show Future; | 7 import 'dart:async' show Future; |
8 | 8 |
9 import 'dart:typed_data' show Uint8List; | 9 import 'dart:typed_data' show Uint8List; |
10 | 10 |
11 import 'package:front_end/src/base/instrumentation.dart' show Instrumentation; | 11 import 'package:front_end/src/base/instrumentation.dart' show Instrumentation; |
12 | 12 |
| 13 import 'package:front_end/src/fasta/builder/ast_factory.dart' show AstFactory; |
| 14 |
| 15 import 'package:front_end/src/fasta/kernel/kernel_ast_factory.dart' |
| 16 show KernelAstFactory; |
| 17 |
13 import 'package:front_end/src/fasta/kernel/kernel_shadow_ast.dart' | 18 import 'package:front_end/src/fasta/kernel/kernel_shadow_ast.dart' |
14 show KernelTypeInferrer; | 19 show KernelTypeInferrer; |
15 | 20 |
16 import 'package:front_end/src/fasta/kernel/kernel_target.dart' | 21 import 'package:front_end/src/fasta/kernel/kernel_target.dart' |
17 show KernelTarget; | 22 show KernelTarget; |
18 | 23 |
| 24 import 'package:front_end/src/fasta/type_inference/type_inferrer.dart' |
| 25 show TypeInferrer; |
| 26 |
19 import 'package:kernel/ast.dart' show Program; | 27 import 'package:kernel/ast.dart' show Program; |
20 | 28 |
21 import 'package:kernel/class_hierarchy.dart' show ClassHierarchy; | 29 import 'package:kernel/class_hierarchy.dart' show ClassHierarchy; |
22 | 30 |
23 import 'package:kernel/core_types.dart' show CoreTypes; | 31 import 'package:kernel/core_types.dart' show CoreTypes; |
24 | 32 |
25 import '../builder/builder.dart' show Builder, ClassBuilder, LibraryBuilder; | 33 import '../builder/builder.dart' show Builder, ClassBuilder, LibraryBuilder; |
26 | 34 |
27 import '../compiler_context.dart' show CompilerContext; | 35 import '../compiler_context.dart' show CompilerContext; |
28 | 36 |
(...skipping 20 matching lines...) Expand all Loading... |
49 import 'source_library_builder.dart' show SourceLibraryBuilder; | 57 import 'source_library_builder.dart' show SourceLibraryBuilder; |
50 | 58 |
51 class SourceLoader<L> extends Loader<L> { | 59 class SourceLoader<L> extends Loader<L> { |
52 final Map<Uri, List<int>> sourceBytes = <Uri, List<int>>{}; | 60 final Map<Uri, List<int>> sourceBytes = <Uri, List<int>>{}; |
53 final bool excludeSource = CompilerContext.current.options.excludeSource; | 61 final bool excludeSource = CompilerContext.current.options.excludeSource; |
54 | 62 |
55 // Used when building directly to kernel. | 63 // Used when building directly to kernel. |
56 ClassHierarchy hierarchy; | 64 ClassHierarchy hierarchy; |
57 CoreTypes coreTypes; | 65 CoreTypes coreTypes; |
58 | 66 |
| 67 final AstFactory astFactory = new KernelAstFactory(); |
| 68 |
| 69 TypeInferrer topLevelTypeInferrer; |
| 70 |
59 Instrumentation instrumentation; | 71 Instrumentation instrumentation; |
60 | 72 |
61 SourceLoader(KernelTarget target) : super(target); | 73 SourceLoader(KernelTarget target) : super(target); |
62 | 74 |
63 Future<Token> tokenize(SourceLibraryBuilder library, | 75 Future<Token> tokenize(SourceLibraryBuilder library, |
64 {bool suppressLexicalErrors: false}) async { | 76 {bool suppressLexicalErrors: false}) async { |
65 Uri uri = library.fileUri; | 77 Uri uri = library.fileUri; |
66 if (uri == null || uri.scheme != "file") { | 78 if (uri == null || uri.scheme != "file") { |
67 return inputError(library.uri, -1, "Not found: ${library.uri}."); | 79 return inputError(library.uri, -1, "Not found: ${library.uri}."); |
68 } | 80 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 listener.uri = part.fileUri; | 134 listener.uri = part.fileUri; |
123 parser.parseUnit(tokens); | 135 parser.parseUnit(tokens); |
124 } | 136 } |
125 } | 137 } |
126 } | 138 } |
127 } | 139 } |
128 | 140 |
129 KernelTarget get target => super.target; | 141 KernelTarget get target => super.target; |
130 | 142 |
131 DietListener createDietListener(LibraryBuilder library) { | 143 DietListener createDietListener(LibraryBuilder library) { |
132 var typeInferrer = new KernelTypeInferrer( | 144 return new DietListener( |
133 coreTypes, hierarchy, instrumentation, target.strongMode); | 145 library, hierarchy, coreTypes, createLocalTypeInferrer()); |
134 return new DietListener(library, hierarchy, coreTypes, typeInferrer); | |
135 } | 146 } |
136 | 147 |
137 void resolveParts() { | 148 void resolveParts() { |
138 List<Uri> parts = <Uri>[]; | 149 List<Uri> parts = <Uri>[]; |
139 builders.forEach((Uri uri, LibraryBuilder library) { | 150 builders.forEach((Uri uri, LibraryBuilder library) { |
140 if (library is SourceLibraryBuilder) { | 151 if (library is SourceLibraryBuilder) { |
141 if (library.isPart) { | 152 if (library.isPart) { |
142 library.validatePart(); | 153 library.validatePart(); |
143 parts.add(uri); | 154 parts.add(uri); |
144 } else { | 155 } else { |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 } | 372 } |
362 | 373 |
363 void checkOverrides(List<SourceClassBuilder> sourceClasses) { | 374 void checkOverrides(List<SourceClassBuilder> sourceClasses) { |
364 assert(hierarchy != null); | 375 assert(hierarchy != null); |
365 for (SourceClassBuilder builder in sourceClasses) { | 376 for (SourceClassBuilder builder in sourceClasses) { |
366 builder.checkOverrides(hierarchy); | 377 builder.checkOverrides(hierarchy); |
367 } | 378 } |
368 ticker.logMs("Checked overrides"); | 379 ticker.logMs("Checked overrides"); |
369 } | 380 } |
370 | 381 |
| 382 void createTopLevelTypeInferrer() { |
| 383 topLevelTypeInferrer = |
| 384 new KernelTypeInferrer(instrumentation, target.strongMode); |
| 385 } |
| 386 |
| 387 /// Performs the first phase of top level initializer inference, which |
| 388 /// consists of creating kernel objects for all fields and top level variables |
| 389 /// that might be subject to type inference, and records dependencies between |
| 390 /// them. |
| 391 void prepareInitializerInference() { |
| 392 topLevelTypeInferrer.coreTypes = coreTypes; |
| 393 topLevelTypeInferrer.classHierarchy = hierarchy; |
| 394 builders.forEach((Uri uri, LibraryBuilder library) { |
| 395 if (library is SourceLibraryBuilder) { |
| 396 library.prepareInitializerInference( |
| 397 topLevelTypeInferrer, library, null); |
| 398 } |
| 399 }); |
| 400 ticker.logMs("Prepared initializer inference"); |
| 401 } |
| 402 |
| 403 /// Performs the second phase of top level initializer inference, which is to |
| 404 /// visit fields and top level variables in topologically-sorted order and |
| 405 /// assign their types. |
| 406 void performInitializerInference() { |
| 407 topLevelTypeInferrer.performInitializerInference(); |
| 408 ticker.logMs("Performed initializer inference"); |
| 409 } |
| 410 |
| 411 /// Creates the type inferrer that should be used inside of method bodies. |
| 412 TypeInferrer createLocalTypeInferrer() { |
| 413 // For kernel, the top level and local type inferrers are the same. |
| 414 return topLevelTypeInferrer; |
| 415 } |
| 416 |
371 List<Uri> getDependencies() => sourceBytes.keys.toList(); | 417 List<Uri> getDependencies() => sourceBytes.keys.toList(); |
372 } | 418 } |
OLD | NEW |