OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 class DeferredLoadTask extends CompilerTask { |
| 6 DeferredLoadTask(Compiler compiler) : super(compiler); |
| 7 String get name => 'Lazy'; |
| 8 final Set<LibraryElement> deferredLibraries = new Set<LibraryElement>(); |
| 9 |
| 10 bool isDeferred(Element element) { |
| 11 // TODO(ahe): This is really a graph coloring problem. We should |
| 12 // make sure that libraries and elements only used by a deferred |
| 13 // library are also deferred. |
| 14 // Also, if something is deferred depends on your |
| 15 // perspective. Inside a deferred library, other elements of the |
| 16 // same library are not deferred. We should add an extra parameter |
| 17 // to this method to indicate "from where". |
| 18 return deferredLibraries.contains(element.getLibrary()); |
| 19 } |
| 20 |
| 21 void registerMainApp(LibraryElement mainApp) { |
| 22 if (mainApp == null) return; |
| 23 compiler.withCurrentElement(mainApp, () { |
| 24 deferredLibraries.addAll(new List.from(findDeferredLibraries(mainApp))); |
| 25 }); |
| 26 } |
| 27 |
| 28 Link<Element> findDeferredLibraries(LibraryElement library) { |
| 29 Link<Element> link = const Link<Element>(); |
| 30 for (LibraryTag tag in library.tags) { |
| 31 for (MetadataAnnotation metadata in tag.metadata) { |
| 32 metadata.ensureResolved(compiler); |
| 33 if (metadata.value.type.element == compiler.deferLoadClass) { |
| 34 ConstructedConstant value = metadata.value; |
| 35 SourceString expectedName = value.fields[0].toDartString().source; |
| 36 LibraryElement deferredLibrary = library.getDependency(tag); |
| 37 link = link.prepend(deferredLibrary); |
| 38 note(tag, 'Defer loading of $deferredLibrary'); |
| 39 SourceString actualName = |
| 40 new SourceString(deferredLibrary.getLibraryOrScriptName()); |
| 41 if (expectedName != actualName) { |
| 42 compiler.reportErrorCode( |
| 43 metadata, |
| 44 MessageKind.GENERIC, // DO NOT SUBMIT (add proper error code). |
| 45 {'text': |
| 46 'Library name mismatch "${expectedName.slowToString()}" != ' |
| 47 '"${actualName.slowToString()}"'}); |
| 48 } else { |
| 49 // DO NOT SUBMIT (this is for debugging). |
| 50 note(metadata, |
| 51 '${actualName.slowToString()} matches $deferredLibrary'); |
| 52 } |
| 53 } |
| 54 } |
| 55 } |
| 56 return link; |
| 57 } |
| 58 |
| 59 void note(Spannable node, message) { |
| 60 compiler.reportMessage(compiler.spanFromSpannable(node), |
| 61 MessageKind.GENERIC.error({'text': message}), |
| 62 api.Diagnostic.INFO); |
| 63 } |
| 64 } |
OLD | NEW |