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.dart'; | 9 import 'common.dart'; |
10 import 'common/names.dart' show | 10 import 'common/names.dart' show |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 /// Looks up the library with the [canonicalUri]. | 140 /// Looks up the library with the [canonicalUri]. |
141 LibraryElement lookupLibrary(Uri canonicalUri); | 141 LibraryElement lookupLibrary(Uri canonicalUri); |
142 | 142 |
143 /// Loads the library specified by the [resolvedUri] and returns its | 143 /// Loads the library specified by the [resolvedUri] and returns its |
144 /// [LibraryElement]. | 144 /// [LibraryElement]. |
145 /// | 145 /// |
146 /// If the library is not already loaded, the method creates the | 146 /// If the library is not already loaded, the method creates the |
147 /// [LibraryElement] for the library and computes the import/export scope, | 147 /// [LibraryElement] for the library and computes the import/export scope, |
148 /// loading and computing the import/export scopes of all required libraries | 148 /// loading and computing the import/export scopes of all required libraries |
149 /// in the process. The method handles cyclic dependency between libraries. | 149 /// in the process. The method handles cyclic dependency between libraries. |
150 Future<LibraryElement> loadLibrary(Uri resolvedUri); | 150 /// |
| 151 /// If [skipFileWithPartOfTag] is `true`, `null` is returned if the |
| 152 /// compilation unit for [resolvedUri] contains a `part of` tag. This is only |
| 153 /// used for analysis through [Compiler.analyzeUri]. |
| 154 Future<LibraryElement> loadLibrary( |
| 155 Uri resolvedUri, |
| 156 {bool skipFileWithPartOfTag: false}); |
151 | 157 |
152 /// Reset the library loader task to prepare for compilation. If provided, | 158 /// Reset the library loader task to prepare for compilation. If provided, |
153 /// libraries matching [reuseLibrary] are reused. | 159 /// libraries matching [reuseLibrary] are reused. |
154 /// | 160 /// |
155 /// This method is used for incremental compilation. | 161 /// This method is used for incremental compilation. |
156 void reset({bool reuseLibrary(LibraryElement library)}); | 162 void reset({bool reuseLibrary(LibraryElement library)}); |
157 | 163 |
158 /// Asynchronous version of [reset]. | 164 /// Asynchronous version of [reset]. |
159 Future resetAsync(Future<bool> reuseLibrary(LibraryElement library)); | 165 Future resetAsync(Future<bool> reuseLibrary(LibraryElement library)); |
160 } | 166 } |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 }); | 338 }); |
333 } | 339 } |
334 | 340 |
335 /// Insert [library] in the internal maps. Used for compiler reuse. | 341 /// Insert [library] in the internal maps. Used for compiler reuse. |
336 void mapLibrary(LibraryElement library) { | 342 void mapLibrary(LibraryElement library) { |
337 libraryCanonicalUriMap[library.canonicalUri] = library; | 343 libraryCanonicalUriMap[library.canonicalUri] = library; |
338 | 344 |
339 Uri resourceUri = library.entryCompilationUnit.script.resourceUri; | 345 Uri resourceUri = library.entryCompilationUnit.script.resourceUri; |
340 libraryResourceUriMap[resourceUri] = library; | 346 libraryResourceUriMap[resourceUri] = library; |
341 | 347 |
342 String name = library.libraryOrScriptName; | 348 if (library.hasLibraryName) { |
343 libraryNames[name] = library; | 349 String name = library.libraryName; |
| 350 libraryNames[name] = library; |
| 351 } |
344 } | 352 } |
345 | 353 |
346 Future<LibraryElement> loadLibrary(Uri resolvedUri) { | 354 Future<LibraryElement> loadLibrary( |
| 355 Uri resolvedUri, |
| 356 {bool skipFileWithPartOfTag: false}) { |
347 return measure(() { | 357 return measure(() { |
348 assert(currentHandler == null); | 358 assert(currentHandler == null); |
349 // TODO(johnniwinther): Ensure that currentHandler correctly encloses the | 359 // TODO(johnniwinther): Ensure that currentHandler correctly encloses the |
350 // loading of a library cluster. | 360 // loading of a library cluster. |
351 currentHandler = new LibraryDependencyHandler(this); | 361 currentHandler = new LibraryDependencyHandler(this); |
352 return createLibrary(currentHandler, null, resolvedUri) | 362 return createLibrary(currentHandler, null, resolvedUri, |
| 363 skipFileWithPartOfTag: skipFileWithPartOfTag) |
353 .then((LibraryElement library) { | 364 .then((LibraryElement library) { |
| 365 if (library == null) { |
| 366 currentHandler = null; |
| 367 return null; |
| 368 } |
354 return reporter.withCurrentElement(library, () { | 369 return reporter.withCurrentElement(library, () { |
355 return measure(() { | 370 return measure(() { |
356 currentHandler.computeExports(); | 371 currentHandler.computeExports(); |
357 LoadedLibraries loadedLibraries = | 372 LoadedLibraries loadedLibraries = |
358 new _LoadedLibraries(library, currentHandler.nodeMap, this); | 373 new _LoadedLibraries(library, currentHandler.nodeMap, this); |
359 currentHandler = null; | 374 currentHandler = null; |
360 return compiler.onLibrariesLoaded(loadedLibraries) | 375 return compiler.onLibrariesLoaded(loadedLibraries) |
361 .then((_) => library); | 376 .then((_) => library); |
362 }); | 377 }); |
363 }); | 378 }); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
499 }); | 514 }); |
500 } else { | 515 } else { |
501 reporter.reportHintMessage( | 516 reporter.reportHintMessage( |
502 library, | 517 library, |
503 MessageKind.DUPLICATED_RESOURCE, | 518 MessageKind.DUPLICATED_RESOURCE, |
504 {'resourceUri': resourceUri, | 519 {'resourceUri': resourceUri, |
505 'canonicalUri1': library.canonicalUri, | 520 'canonicalUri1': library.canonicalUri, |
506 'canonicalUri2': existing.canonicalUri}); | 521 'canonicalUri2': existing.canonicalUri}); |
507 } | 522 } |
508 } else if (library.hasLibraryName) { | 523 } else if (library.hasLibraryName) { |
509 String name = library.libraryOrScriptName; | 524 String name = library.libraryName; |
510 existing = libraryNames.putIfAbsent(name, () => library); | 525 existing = libraryNames.putIfAbsent(name, () => library); |
511 if (!identical(existing, library)) { | 526 if (!identical(existing, library)) { |
512 reporter.withCurrentElement(library, () { | 527 reporter.withCurrentElement(library, () { |
513 reporter.reportWarningMessage( | 528 reporter.reportWarningMessage( |
514 library, | 529 library, |
515 MessageKind.DUPLICATED_LIBRARY_NAME, | 530 MessageKind.DUPLICATED_LIBRARY_NAME, |
516 {'libraryName': name}); | 531 {'libraryName': name}); |
517 }); | 532 }); |
518 reporter.withCurrentElement(existing, () { | 533 reporter.withCurrentElement(existing, () { |
519 reporter.reportWarningMessage( | 534 reporter.reportWarningMessage( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 * registering its dependency in [handler] for the computation of the import/ | 571 * registering its dependency in [handler] for the computation of the import/ |
557 * export scope. If the tag does not contain a valid URI, then its dependency | 572 * export scope. If the tag does not contain a valid URI, then its dependency |
558 * is not registered in [handler]. | 573 * is not registered in [handler]. |
559 */ | 574 */ |
560 Future<Null> registerLibraryFromImportExport( | 575 Future<Null> registerLibraryFromImportExport( |
561 LibraryDependencyHandler handler, | 576 LibraryDependencyHandler handler, |
562 LibraryElement library, | 577 LibraryElement library, |
563 LibraryDependencyElementX libraryDependency) { | 578 LibraryDependencyElementX libraryDependency) { |
564 Uri base = library.canonicalUri; | 579 Uri base = library.canonicalUri; |
565 Uri resolvedUri = base.resolveUri(libraryDependency.uri); | 580 Uri resolvedUri = base.resolveUri(libraryDependency.uri); |
566 return createLibrary(handler, library, resolvedUri, libraryDependency) | 581 return createLibrary(handler, library, resolvedUri, node: libraryDependency) |
567 .then((LibraryElement loadedLibrary) { | 582 .then((LibraryElement loadedLibrary) { |
568 if (loadedLibrary == null) return; | 583 if (loadedLibrary == null) return; |
569 reporter.withCurrentElement(library, () { | 584 reporter.withCurrentElement(library, () { |
570 libraryDependency.libraryDependency = loadedLibrary; | 585 libraryDependency.libraryDependency = loadedLibrary; |
571 handler.registerDependency( | 586 handler.registerDependency( |
572 library, libraryDependency, loadedLibrary); | 587 library, libraryDependency, loadedLibrary); |
573 }); | 588 }); |
574 }); | 589 }); |
575 } | 590 } |
576 | 591 |
(...skipping 16 matching lines...) Expand all Loading... |
593 }); | 608 }); |
594 }); | 609 }); |
595 } | 610 } |
596 | 611 |
597 /** | 612 /** |
598 * Create (or reuse) a library element for the library specified by the | 613 * Create (or reuse) a library element for the library specified by the |
599 * [resolvedUri]. | 614 * [resolvedUri]. |
600 * | 615 * |
601 * If a new library is created, the [handler] is notified. | 616 * If a new library is created, the [handler] is notified. |
602 */ | 617 */ |
603 Future<LibraryElement> createLibrary(LibraryDependencyHandler handler, | 618 Future<LibraryElement> createLibrary( |
604 LibraryElement importingLibrary, | 619 LibraryDependencyHandler handler, |
605 Uri resolvedUri, | 620 LibraryElement importingLibrary, |
606 [Spannable node]) { | 621 Uri resolvedUri, |
| 622 {Spannable node, |
| 623 bool skipFileWithPartOfTag: false}) { |
607 Uri readableUri = | 624 Uri readableUri = |
608 compiler.translateResolvedUri(importingLibrary, resolvedUri, node); | 625 compiler.translateResolvedUri(importingLibrary, resolvedUri, node); |
609 LibraryElement library = libraryCanonicalUriMap[resolvedUri]; | 626 LibraryElement library = libraryCanonicalUriMap[resolvedUri]; |
610 if (library != null) { | 627 if (library != null) { |
611 return new Future.value(library); | 628 return new Future.value(library); |
612 } | 629 } |
613 library = compiler.serialization.readLibrary(resolvedUri); | 630 library = compiler.serialization.readLibrary(resolvedUri); |
614 if (library != null) { | 631 if (library != null) { |
615 return loadDeserializedLibrary(handler, library); | 632 return loadDeserializedLibrary(handler, library); |
616 } | 633 } |
617 var readScript = compiler.readScript; | 634 var readScript = compiler.readScript; |
618 if (readableUri == null) { | 635 if (readableUri == null) { |
619 readableUri = resolvedUri; | 636 readableUri = resolvedUri; |
620 readScript = compiler.synthesizeScript; | 637 readScript = compiler.synthesizeScript; |
621 } | 638 } |
622 return reporter.withCurrentElement(importingLibrary, () { | 639 return reporter.withCurrentElement(importingLibrary, () { |
623 return readScript(node, readableUri).then((Script script) { | 640 return readScript(node, readableUri).then((Script script) { |
624 if (script == null) return null; | 641 if (script == null) return null; |
625 LibraryElement element = | 642 LibraryElement element = |
626 createLibrarySync(handler, script, resolvedUri); | 643 createLibrarySync(handler, script, resolvedUri); |
627 CompilationUnitElementX compilationUnit = element.entryCompilationUnit; | 644 CompilationUnitElementX compilationUnit = element.entryCompilationUnit; |
628 if (compilationUnit.partTag != null) { | 645 if (compilationUnit.partTag != null) { |
| 646 if (skipFileWithPartOfTag) { |
| 647 // TODO(johnniwinther): Avoid calling [Compiler.onLibraryCreated] |
| 648 // for this library. |
| 649 libraryCanonicalUriMap.remove(resolvedUri); |
| 650 return null; |
| 651 } |
629 DiagnosticMessage error = reporter.withCurrentElement( | 652 DiagnosticMessage error = reporter.withCurrentElement( |
630 compilationUnit, | 653 compilationUnit, |
631 () => reporter.createMessage( | 654 () => reporter.createMessage( |
632 compilationUnit.partTag, MessageKind.IMPORT_PART_OF)); | 655 compilationUnit.partTag, MessageKind.IMPORT_PART_OF)); |
633 DiagnosticMessage info = reporter.withCurrentElement( | 656 DiagnosticMessage info = reporter.withCurrentElement( |
634 importingLibrary, | 657 importingLibrary, |
635 () => reporter.createMessage( | 658 () => reporter.createMessage( |
636 node, | 659 node, |
637 MessageKind.IMPORT_PART_OF_HERE)); | 660 MessageKind.IMPORT_PART_OF_HERE)); |
638 reporter.reportError(error, <DiagnosticMessage>[info]); | 661 reporter.reportError(error, <DiagnosticMessage>[info]); |
(...skipping 730 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1369 } | 1392 } |
1370 suffixes.add(const Link<Uri>().prepend(canonicalUri)); | 1393 suffixes.add(const Link<Uri>().prepend(canonicalUri)); |
1371 } | 1394 } |
1372 suffixChainMap[library] = suffixes; | 1395 suffixChainMap[library] = suffixes; |
1373 return; | 1396 return; |
1374 } | 1397 } |
1375 | 1398 |
1376 computeSuffixes(rootLibrary, const Link<Uri>()); | 1399 computeSuffixes(rootLibrary, const Link<Uri>()); |
1377 } | 1400 } |
1378 } | 1401 } |
OLD | NEW |