OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 dart2js.library_loader; | 5 library dart2js.library_loader; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'common/names.dart' show Uris; | 9 import 'common/names.dart' show Uris; |
10 import 'common/tasks.dart' show CompilerTask, Measurer; | 10 import 'common/tasks.dart' show CompilerTask, Measurer; |
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 } | 420 } |
421 } | 421 } |
422 | 422 |
423 Future<LibraryElement> loadLibrary(Uri resolvedUri, | 423 Future<LibraryElement> loadLibrary(Uri resolvedUri, |
424 {bool skipFileWithPartOfTag: false}) { | 424 {bool skipFileWithPartOfTag: false}) { |
425 return measure(() { | 425 return measure(() { |
426 assert(currentHandler == null); | 426 assert(currentHandler == null); |
427 // TODO(johnniwinther): Ensure that currentHandler correctly encloses the | 427 // TODO(johnniwinther): Ensure that currentHandler correctly encloses the |
428 // loading of a library cluster. | 428 // loading of a library cluster. |
429 currentHandler = new LibraryDependencyHandler(this); | 429 currentHandler = new LibraryDependencyHandler(this); |
430 return createLibrary(currentHandler, null, resolvedUri, | 430 return createLibrary( |
| 431 currentHandler, null, resolvedUri, NO_LOCATION_SPANNABLE, |
431 skipFileWithPartOfTag: skipFileWithPartOfTag) | 432 skipFileWithPartOfTag: skipFileWithPartOfTag) |
432 .then((LibraryElement library) { | 433 .then((LibraryElement library) { |
433 if (library == null) { | 434 if (library == null) { |
434 currentHandler = null; | 435 currentHandler = null; |
435 return null; | 436 return null; |
436 } | 437 } |
437 return reporter.withCurrentElement(library, () { | 438 return reporter.withCurrentElement(library, () { |
438 return measure(() { | 439 return measure(() { |
439 currentHandler.computeExports(); | 440 currentHandler.computeExports(); |
440 LoadedLibraries loadedLibraries = new _LoadedLibraries(library, | 441 LoadedLibraries loadedLibraries = new _LoadedLibraries(library, |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
536 } | 537 } |
537 }); | 538 }); |
538 }).then((_) { | 539 }).then((_) { |
539 return listener.onLibraryScanned(library, handler); | 540 return listener.onLibraryScanned(library, handler); |
540 }).then((_) { | 541 }).then((_) { |
541 return reporter.withCurrentElement(library, () { | 542 return reporter.withCurrentElement(library, () { |
542 checkDuplicatedLibraryName(library); | 543 checkDuplicatedLibraryName(library); |
543 | 544 |
544 // Import dart:core if not already imported. | 545 // Import dart:core if not already imported. |
545 if (!importsDartCore && library.canonicalUri != Uris.dart_core) { | 546 if (!importsDartCore && library.canonicalUri != Uris.dart_core) { |
546 return createLibrary(handler, null, Uris.dart_core) | 547 return createLibrary(handler, null, Uris.dart_core, library) |
547 .then((LibraryElement coreLibrary) { | 548 .then((LibraryElement coreLibrary) { |
548 handler.registerDependency( | 549 handler.registerDependency( |
549 library, | 550 library, |
550 new SyntheticImportElement( | 551 new SyntheticImportElement( |
551 library.entryCompilationUnit, Uris.dart_core, coreLibrary), | 552 library.entryCompilationUnit, Uris.dart_core, coreLibrary), |
552 coreLibrary); | 553 coreLibrary); |
553 }); | 554 }); |
554 } | 555 } |
555 }); | 556 }); |
556 }).then((_) { | 557 }).then((_) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
622 /** | 623 /** |
623 * Handle an import/export tag by loading the referenced library and | 624 * Handle an import/export tag by loading the referenced library and |
624 * registering its dependency in [handler] for the computation of the import/ | 625 * registering its dependency in [handler] for the computation of the import/ |
625 * export scope. If the tag does not contain a valid URI, then its dependency | 626 * export scope. If the tag does not contain a valid URI, then its dependency |
626 * is not registered in [handler]. | 627 * is not registered in [handler]. |
627 */ | 628 */ |
628 Future<Null> registerLibraryFromImportExport(LibraryDependencyHandler handler, | 629 Future<Null> registerLibraryFromImportExport(LibraryDependencyHandler handler, |
629 LibraryElement library, LibraryDependencyElementX libraryDependency) { | 630 LibraryElement library, LibraryDependencyElementX libraryDependency) { |
630 Uri base = library.canonicalUri; | 631 Uri base = library.canonicalUri; |
631 Uri resolvedUri = base.resolveUri(libraryDependency.uri); | 632 Uri resolvedUri = base.resolveUri(libraryDependency.uri); |
632 return createLibrary(handler, library, resolvedUri, node: libraryDependency) | 633 return createLibrary(handler, library, resolvedUri, libraryDependency) |
633 .then((LibraryElement loadedLibrary) { | 634 .then((LibraryElement loadedLibrary) { |
634 if (loadedLibrary == null) return; | 635 if (loadedLibrary == null) return; |
635 reporter.withCurrentElement(library, () { | 636 reporter.withCurrentElement(library, () { |
636 libraryDependency.libraryDependency = loadedLibrary; | 637 libraryDependency.libraryDependency = loadedLibrary; |
637 handler.registerDependency(library, libraryDependency, loadedLibrary); | 638 handler.registerDependency(library, libraryDependency, loadedLibrary); |
638 }); | 639 }); |
639 }); | 640 }); |
640 } | 641 } |
641 | 642 |
642 /// Loads the deserialized [library] with the [handler]. | 643 /// Loads the deserialized [library] with the [handler]. |
643 /// | 644 /// |
644 /// All libraries imported or exported transitively from [library] will be | 645 /// All libraries imported or exported transitively from [library] will be |
645 /// loaded as well. | 646 /// loaded as well. |
646 Future<LibraryElement> loadDeserializedLibrary( | 647 Future<LibraryElement> loadDeserializedLibrary( |
647 LibraryDependencyHandler handler, LibraryElement library) { | 648 LibraryDependencyHandler handler, LibraryElement library) { |
648 libraryCanonicalUriMap[library.canonicalUri] = library; | 649 libraryCanonicalUriMap[library.canonicalUri] = library; |
649 handler.registerNewLibrary(library); | 650 handler.registerNewLibrary(library); |
650 return listener.onLibraryScanned(library, handler).then((_) { | 651 return listener.onLibraryScanned(library, handler).then((_) { |
651 return Future.forEach(library.imports, (ImportElement import) { | 652 return Future.forEach(library.imports, (ImportElement import) { |
652 Uri resolvedUri = library.canonicalUri.resolveUri(import.uri); | 653 Uri resolvedUri = library.canonicalUri.resolveUri(import.uri); |
653 return createLibrary(handler, library, resolvedUri); | 654 return createLibrary(handler, library, resolvedUri, library); |
654 }).then((_) { | 655 }).then((_) { |
655 return Future.forEach(library.exports, (ExportElement export) { | 656 return Future.forEach(library.exports, (ExportElement export) { |
656 Uri resolvedUri = library.canonicalUri.resolveUri(export.uri); | 657 Uri resolvedUri = library.canonicalUri.resolveUri(export.uri); |
657 return createLibrary(handler, library, resolvedUri); | 658 return createLibrary(handler, library, resolvedUri, library); |
658 }).then((_) { | 659 }).then((_) { |
659 // TODO(johnniwinther): Shouldn't there be an [ImportElement] for the | 660 // TODO(johnniwinther): Shouldn't there be an [ImportElement] for the |
660 // implicit import of dart:core? | 661 // implicit import of dart:core? |
661 return createLibrary(handler, library, Uris.dart_core); | 662 return createLibrary(handler, library, Uris.dart_core, library); |
662 }).then((_) => library); | 663 }).then((_) => library); |
663 }); | 664 }); |
664 }); | 665 }); |
665 } | 666 } |
666 | 667 |
667 Future<Script> _readScript( | 668 Future<Script> _readScript( |
668 Spannable spannable, Uri readableUri, Uri resolvedUri) { | 669 Spannable spannable, Uri readableUri, Uri resolvedUri) { |
669 if (readableUri == null) { | 670 if (readableUri == null) { |
670 return new Future.value(new Script.synthetic(resolvedUri)); | 671 return new Future.value(new Script.synthetic(resolvedUri)); |
671 } else { | 672 } else { |
672 return scriptLoader.readScript(readableUri, spannable); | 673 return scriptLoader.readScript(readableUri, spannable); |
673 } | 674 } |
674 } | 675 } |
675 | 676 |
676 /** | 677 /** |
677 * Create (or reuse) a library element for the library specified by the | 678 * Create (or reuse) a library element for the library specified by the |
678 * [resolvedUri]. | 679 * [resolvedUri]. |
679 * | 680 * |
680 * If a new library is created, the [handler] is notified. | 681 * If a new library is created, the [handler] is notified. |
681 */ | 682 */ |
682 Future<LibraryElement> createLibrary(LibraryDependencyHandler handler, | 683 Future<LibraryElement> createLibrary(LibraryDependencyHandler handler, |
683 LibraryElement importingLibrary, Uri resolvedUri, | 684 LibraryElement importingLibrary, Uri resolvedUri, Spannable spannable, |
684 {Spannable node, bool skipFileWithPartOfTag: false}) { | 685 {bool skipFileWithPartOfTag: false}) { |
685 Uri readableUri = | 686 Uri readableUri = |
686 uriTranslator.translate(importingLibrary, resolvedUri, node); | 687 uriTranslator.translate(importingLibrary, resolvedUri, spannable); |
687 LibraryElement library = libraryCanonicalUriMap[resolvedUri]; | 688 LibraryElement library = libraryCanonicalUriMap[resolvedUri]; |
688 if (library != null) { | 689 if (library != null) { |
689 return new Future.value(library); | 690 return new Future.value(library); |
690 } | 691 } |
691 return deserializer.readLibrary(resolvedUri).then((LibraryElement library) { | 692 return deserializer.readLibrary(resolvedUri).then((LibraryElement library) { |
692 if (library != null) { | 693 if (library != null) { |
693 return loadDeserializedLibrary(handler, library); | 694 return loadDeserializedLibrary(handler, library); |
694 } | 695 } |
695 return reporter.withCurrentElement(importingLibrary, () { | 696 return reporter.withCurrentElement(importingLibrary, () { |
696 return _readScript(node, readableUri, resolvedUri) | 697 return _readScript(spannable, readableUri, resolvedUri) |
697 .then((Script script) { | 698 .then((Script script) { |
698 if (script == null) return null; | 699 if (script == null) return null; |
699 LibraryElement element = | 700 LibraryElement element = |
700 createLibrarySync(handler, script, resolvedUri); | 701 createLibrarySync(handler, script, resolvedUri); |
701 CompilationUnitElementX compilationUnit = | 702 CompilationUnitElementX compilationUnit = |
702 element.entryCompilationUnit; | 703 element.entryCompilationUnit; |
703 if (compilationUnit.partTag != null) { | 704 if (compilationUnit.partTag != null) { |
704 if (skipFileWithPartOfTag) { | 705 if (skipFileWithPartOfTag) { |
705 // TODO(johnniwinther): Avoid calling [listener.onLibraryCreated] | 706 // TODO(johnniwinther): Avoid calling [listener.onLibraryCreated] |
706 // for this library. | 707 // for this library. |
707 libraryCanonicalUriMap.remove(resolvedUri); | 708 libraryCanonicalUriMap.remove(resolvedUri); |
708 return null; | 709 return null; |
709 } | 710 } |
710 if (importingLibrary == null) { | 711 if (importingLibrary == null) { |
711 DiagnosticMessage error = reporter.withCurrentElement( | 712 DiagnosticMessage error = reporter.withCurrentElement( |
712 compilationUnit, | 713 compilationUnit, |
713 () => reporter.createMessage( | 714 () => reporter.createMessage( |
714 compilationUnit.partTag, MessageKind.MAIN_HAS_PART_OF)); | 715 compilationUnit.partTag, MessageKind.MAIN_HAS_PART_OF)); |
715 reporter.reportError(error); | 716 reporter.reportError(error); |
716 } else { | 717 } else { |
717 DiagnosticMessage error = reporter.withCurrentElement( | 718 DiagnosticMessage error = reporter.withCurrentElement( |
718 compilationUnit, | 719 compilationUnit, |
719 () => reporter.createMessage( | 720 () => reporter.createMessage( |
720 compilationUnit.partTag, MessageKind.IMPORT_PART_OF)); | 721 compilationUnit.partTag, MessageKind.IMPORT_PART_OF)); |
721 DiagnosticMessage info = reporter.withCurrentElement( | 722 DiagnosticMessage info = reporter.withCurrentElement( |
722 importingLibrary, | 723 importingLibrary, |
723 () => reporter.createMessage( | 724 () => reporter.createMessage( |
724 node, MessageKind.IMPORT_PART_OF_HERE)); | 725 spannable, MessageKind.IMPORT_PART_OF_HERE)); |
725 reporter.reportError(error, [info]); | 726 reporter.reportError(error, [info]); |
726 } | 727 } |
727 } | 728 } |
728 return processLibraryTags(handler, element).then((_) { | 729 return processLibraryTags(handler, element).then((_) { |
729 reporter.withCurrentElement(element, () { | 730 reporter.withCurrentElement(element, () { |
730 handler.registerLibraryExports(element); | 731 handler.registerLibraryExports(element); |
731 }); | 732 }); |
732 return element; | 733 return element; |
733 }); | 734 }); |
734 }); | 735 }); |
(...skipping 782 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1517 Future onLibrariesLoaded(LoadedLibraries results); | 1518 Future onLibrariesLoaded(LoadedLibraries results); |
1518 | 1519 |
1519 /// Called whenever a library element is created. | 1520 /// Called whenever a library element is created. |
1520 void onLibraryCreated(LibraryElement library); | 1521 void onLibraryCreated(LibraryElement library); |
1521 | 1522 |
1522 /// Called whenever a library is scanned from a script file. | 1523 /// Called whenever a library is scanned from a script file. |
1523 Future onLibraryScanned(LibraryElement library, LibraryLoader loader); | 1524 Future onLibraryScanned(LibraryElement library, LibraryLoader loader); |
1524 } | 1525 } |
1525 | 1526 |
1526 const _reuseLibrarySubtaskName = "Reuse library"; | 1527 const _reuseLibrarySubtaskName = "Reuse library"; |
OLD | NEW |