| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library fasta.loader; |
| 6 |
| 7 import 'dart:async' show |
| 8 Future; |
| 9 |
| 10 import 'dart:collection' show |
| 11 Queue; |
| 12 |
| 13 import 'ast_kind.dart' show |
| 14 AstKind; |
| 15 |
| 16 import 'builder/builder.dart' show |
| 17 LibraryBuilder; |
| 18 |
| 19 import 'errors.dart' show |
| 20 InputError, |
| 21 firstSourceUri; |
| 22 |
| 23 import 'target_implementation.dart' show |
| 24 TargetImplementation; |
| 25 |
| 26 import 'ticker.dart' show |
| 27 Ticker; |
| 28 |
| 29 abstract class Loader<L> { |
| 30 final Map<Uri, LibraryBuilder> builders = <Uri, LibraryBuilder>{}; |
| 31 |
| 32 final Queue<LibraryBuilder> unparsedLibraries = new Queue<LibraryBuilder>(); |
| 33 |
| 34 final List<L> libraries = <L>[]; |
| 35 |
| 36 final TargetImplementation target; |
| 37 |
| 38 LibraryBuilder coreLibrary; |
| 39 |
| 40 LibraryBuilder first; |
| 41 |
| 42 int byteCount = 0; |
| 43 |
| 44 Uri currentUriForCrashReporting; |
| 45 |
| 46 Loader(this.target); |
| 47 |
| 48 Ticker get ticker => target.ticker; |
| 49 |
| 50 LibraryBuilder read(Uri uri) { |
| 51 firstSourceUri ??= uri; |
| 52 LibraryBuilder builder = builders.putIfAbsent(uri, () { |
| 53 LibraryBuilder library = target.createLibraryBuilder(uri); |
| 54 if (uri.scheme == "dart" && uri.path == "core") { |
| 55 coreLibrary = library; |
| 56 } |
| 57 first ??= library; |
| 58 if (library.loader == this) { |
| 59 unparsedLibraries.addLast(library); |
| 60 } |
| 61 return library; |
| 62 }); |
| 63 return builder; |
| 64 } |
| 65 |
| 66 void ensureCoreLibrary() { |
| 67 if (coreLibrary == null) { |
| 68 read(Uri.parse("dart:core")); |
| 69 assert(coreLibrary != null); |
| 70 } |
| 71 } |
| 72 |
| 73 Future<Null> buildBodies(AstKind astKind) async { |
| 74 assert(coreLibrary != null); |
| 75 for (LibraryBuilder library in builders.values) { |
| 76 currentUriForCrashReporting = library.uri; |
| 77 await buildBody(library, astKind); |
| 78 } |
| 79 currentUriForCrashReporting = null; |
| 80 ticker.log((Duration elapsed, Duration sinceStart) { |
| 81 int libraryCount = builders.length; |
| 82 double ms = |
| 83 elapsed.inMicroseconds / Duration.MICROSECONDS_PER_MILLISECOND; |
| 84 String message = "Built $libraryCount compilation units"; |
| 85 print(""" |
| 86 $sinceStart: $message ($byteCount bytes) in ${format(ms, 3, 0)}ms, that is, |
| 87 ${format(byteCount / ms, 3, 12)} bytes/ms, and |
| 88 ${format(ms / libraryCount, 3, 12)} ms/compilation unit."""); |
| 89 }); |
| 90 } |
| 91 |
| 92 Future<Null> buildOutlines() async { |
| 93 ensureCoreLibrary(); |
| 94 while (unparsedLibraries.isNotEmpty) { |
| 95 LibraryBuilder library = unparsedLibraries.removeFirst(); |
| 96 currentUriForCrashReporting = library.uri; |
| 97 await buildOutline(library); |
| 98 } |
| 99 currentUriForCrashReporting = null; |
| 100 ticker.log((Duration elapsed, Duration sinceStart) { |
| 101 int libraryCount = builders.length; |
| 102 double ms = |
| 103 elapsed.inMicroseconds / Duration.MICROSECONDS_PER_MILLISECOND; |
| 104 String message = "Built outlines for $libraryCount compilation units"; |
| 105 // TODO(ahe): Share this message with [buildBodies]. Also make it easy to |
| 106 // tell the difference between outlines read from a dill file or source |
| 107 // files. Currently, [libraryCount] is wrong for dill files. |
| 108 print(""" |
| 109 $sinceStart: $message ($byteCount bytes) in ${format(ms, 3, 0)}ms, that is, |
| 110 ${format(byteCount / ms, 3, 12)} bytes/ms, and |
| 111 ${format(ms / libraryCount, 3, 12)} ms/compilation unit."""); |
| 112 }); |
| 113 } |
| 114 |
| 115 Future<Null> buildOutline(LibraryBuilder library); |
| 116 |
| 117 Future<Null> buildBody(LibraryBuilder library, AstKind astKind); |
| 118 |
| 119 List<InputError> collectCompileTimeErrors() { |
| 120 List<InputError> errors = <InputError>[]; |
| 121 for (LibraryBuilder library in builders.values) { |
| 122 if (library.loader == this) { |
| 123 errors.addAll(library.compileTimeErrors); |
| 124 } |
| 125 } |
| 126 return errors; |
| 127 } |
| 128 } |
| 129 |
| 130 String format(double d, int fractionDigits, int width) { |
| 131 return d.toStringAsFixed(fractionDigits).padLeft(width); |
| 132 } |
| OLD | NEW |