| 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:io' show FileSystemException; | 9 import 'dart:io' show FileSystemException; |
| 10 | 10 |
| 11 import '../scanner/io.dart' show readBytesFromFile; | 11 import '../scanner/io.dart' show readBytesFromFile; |
| 12 | 12 |
| 13 import '../scanner.dart' show ErrorToken, ScannerResult, Token, scan; | 13 import '../scanner.dart' show ErrorToken, ScannerResult, Token, scan; |
| 14 | 14 |
| 15 import '../parser/class_member_parser.dart' show ClassMemberParser; | 15 import '../parser/class_member_parser.dart' show ClassMemberParser; |
| 16 | 16 |
| 17 import 'package:kernel/ast.dart' show Program; | 17 import 'package:kernel/ast.dart' show Program; |
| 18 | 18 |
| 19 import 'package:kernel/class_hierarchy.dart' show ClassHierarchy; | 19 import 'package:kernel/class_hierarchy.dart' show ClassHierarchy; |
| 20 | 20 |
| 21 import 'package:kernel/core_types.dart' show CoreTypes; | 21 import 'package:kernel/core_types.dart' show CoreTypes; |
| 22 | 22 |
| 23 import '../errors.dart' show inputError, printUnexpected; | 23 import '../errors.dart' show inputError, printUnexpected; |
| 24 | 24 |
| 25 import '../messages.dart' show warning; | 25 import '../messages.dart' show warning; |
| 26 | 26 |
| 27 import '../export.dart' show Export; | 27 import '../export.dart' show Export; |
| 28 | 28 |
| 29 import '../analyzer/element_store.dart' show ElementStore; | |
| 30 | |
| 31 import '../builder/builder.dart' show Builder, ClassBuilder, LibraryBuilder; | 29 import '../builder/builder.dart' show Builder, ClassBuilder, LibraryBuilder; |
| 32 | 30 |
| 33 import 'outline_builder.dart' show OutlineBuilder; | 31 import 'outline_builder.dart' show OutlineBuilder; |
| 34 | 32 |
| 35 import '../loader.dart' show Loader; | 33 import '../loader.dart' show Loader; |
| 36 | 34 |
| 37 import '../target_implementation.dart' show TargetImplementation; | 35 import '../target_implementation.dart' show TargetImplementation; |
| 38 | 36 |
| 39 import 'diet_listener.dart' show DietListener; | 37 import 'diet_listener.dart' show DietListener; |
| 40 | 38 |
| 41 import 'diet_parser.dart' show DietParser; | 39 import 'diet_parser.dart' show DietParser; |
| 42 | 40 |
| 43 import 'source_library_builder.dart' show SourceLibraryBuilder; | 41 import 'source_library_builder.dart' show SourceLibraryBuilder; |
| 44 | 42 |
| 45 import '../ast_kind.dart' show AstKind; | |
| 46 | |
| 47 class SourceLoader<L> extends Loader<L> { | 43 class SourceLoader<L> extends Loader<L> { |
| 48 final Map<Uri, List<int>> sourceBytes = <Uri, List<int>>{}; | 44 final Map<Uri, List<int>> sourceBytes = <Uri, List<int>>{}; |
| 49 | 45 |
| 50 // Used when building directly to kernel. | 46 // Used when building directly to kernel. |
| 51 ClassHierarchy hierarchy; | 47 ClassHierarchy hierarchy; |
| 52 CoreTypes coreTypes; | 48 CoreTypes coreTypes; |
| 53 | 49 |
| 54 // Used when building analyzer ASTs. | |
| 55 ElementStore elementStore; | |
| 56 | |
| 57 SourceLoader(TargetImplementation target) : super(target); | 50 SourceLoader(TargetImplementation target) : super(target); |
| 58 | 51 |
| 59 Future<Token> tokenize(SourceLibraryBuilder library, | 52 Future<Token> tokenize(SourceLibraryBuilder library, |
| 60 {bool suppressLexicalErrors: false}) async { | 53 {bool suppressLexicalErrors: false}) async { |
| 61 Uri uri = library.fileUri; | 54 Uri uri = library.fileUri; |
| 62 if (uri == null || uri.scheme != "file") { | 55 if (uri == null || uri.scheme != "file") { |
| 63 return inputError(library.uri, -1, "Not found: ${library.uri}."); | 56 return inputError(library.uri, -1, "Not found: ${library.uri}."); |
| 64 } | 57 } |
| 65 try { | 58 try { |
| 66 List<int> bytes = sourceBytes[uri]; | 59 List<int> bytes = sourceBytes[uri]; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 91 } | 84 } |
| 92 } | 85 } |
| 93 | 86 |
| 94 Future<Null> buildOutline(SourceLibraryBuilder library) async { | 87 Future<Null> buildOutline(SourceLibraryBuilder library) async { |
| 95 Token tokens = await tokenize(library); | 88 Token tokens = await tokenize(library); |
| 96 if (tokens == null) return; | 89 if (tokens == null) return; |
| 97 OutlineBuilder listener = new OutlineBuilder(library); | 90 OutlineBuilder listener = new OutlineBuilder(library); |
| 98 new ClassMemberParser(listener).parseUnit(tokens); | 91 new ClassMemberParser(listener).parseUnit(tokens); |
| 99 } | 92 } |
| 100 | 93 |
| 101 Future<Null> buildBody(LibraryBuilder library, AstKind astKind) async { | 94 Future<Null> buildBody(LibraryBuilder library) async { |
| 102 if (library is SourceLibraryBuilder) { | 95 if (library is SourceLibraryBuilder) { |
| 103 // We tokenize source files twice to keep memory usage low. This is the | 96 // We tokenize source files twice to keep memory usage low. This is the |
| 104 // second time, and the first time was in [buildOutline] above. So this | 97 // second time, and the first time was in [buildOutline] above. So this |
| 105 // time we suppress lexical errors. | 98 // time we suppress lexical errors. |
| 106 Token tokens = await tokenize(library, suppressLexicalErrors: true); | 99 Token tokens = await tokenize(library, suppressLexicalErrors: true); |
| 107 if (tokens == null) return; | 100 if (tokens == null) return; |
| 108 DietListener listener = new DietListener( | 101 DietListener listener = createDietListener(library); |
| 109 library, elementStore, hierarchy, coreTypes, astKind); | |
| 110 DietParser parser = new DietParser(listener); | 102 DietParser parser = new DietParser(listener); |
| 111 parser.parseUnit(tokens); | 103 parser.parseUnit(tokens); |
| 112 for (SourceLibraryBuilder part in library.parts) { | 104 for (SourceLibraryBuilder part in library.parts) { |
| 113 Token tokens = await tokenize(part); | 105 Token tokens = await tokenize(part); |
| 114 if (tokens != null) { | 106 if (tokens != null) { |
| 115 listener.uri = part.fileUri; | 107 listener.uri = part.fileUri; |
| 116 parser.parseUnit(tokens); | 108 parser.parseUnit(tokens); |
| 117 } | 109 } |
| 118 } | 110 } |
| 119 } | 111 } |
| 120 } | 112 } |
| 121 | 113 |
| 114 DietListener createDietListener(LibraryBuilder library) { |
| 115 return new DietListener(library, hierarchy, coreTypes); |
| 116 } |
| 117 |
| 122 void resolveParts() { | 118 void resolveParts() { |
| 123 List<Uri> parts = <Uri>[]; | 119 List<Uri> parts = <Uri>[]; |
| 124 builders.forEach((Uri uri, LibraryBuilder library) { | 120 builders.forEach((Uri uri, LibraryBuilder library) { |
| 125 if (library is SourceLibraryBuilder) { | 121 if (library is SourceLibraryBuilder) { |
| 126 if (library.isPart) { | 122 if (library.isPart) { |
| 127 library.validatePart(); | 123 library.validatePart(); |
| 128 parts.add(uri); | 124 parts.add(uri); |
| 129 } else { | 125 } else { |
| 130 library.includeParts(); | 126 library.includeParts(); |
| 131 } | 127 } |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 | 328 |
| 333 void buildProgram() { | 329 void buildProgram() { |
| 334 builders.forEach((Uri uri, LibraryBuilder library) { | 330 builders.forEach((Uri uri, LibraryBuilder library) { |
| 335 if (library is SourceLibraryBuilder) { | 331 if (library is SourceLibraryBuilder) { |
| 336 libraries.add(library.build()); | 332 libraries.add(library.build()); |
| 337 } | 333 } |
| 338 }); | 334 }); |
| 339 ticker.logMs("Built program"); | 335 ticker.logMs("Built program"); |
| 340 } | 336 } |
| 341 | 337 |
| 342 void buildElementStore() { | |
| 343 elementStore = new ElementStore(coreLibrary, builders); | |
| 344 ticker.logMs("Built analyzer element model."); | |
| 345 } | |
| 346 | |
| 347 void computeHierarchy(Program program) { | 338 void computeHierarchy(Program program) { |
| 348 hierarchy = new ClassHierarchy(program); | 339 hierarchy = new ClassHierarchy(program); |
| 349 ticker.logMs("Computed class hierarchy"); | 340 ticker.logMs("Computed class hierarchy"); |
| 350 coreTypes = new CoreTypes(program); | 341 coreTypes = new CoreTypes(program); |
| 351 ticker.logMs("Computed core types"); | 342 ticker.logMs("Computed core types"); |
| 352 } | 343 } |
| 353 } | 344 } |
| OLD | NEW |