| 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.compiler_base; | 5 library dart2js.compiler_base; |
| 6 | 6 |
| 7 import 'dart:async' show Future; | 7 import 'dart:async' show Future; |
| 8 | 8 |
| 9 import '../compiler_new.dart' as api; | 9 import '../compiler_new.dart' as api; |
| 10 import 'closure.dart' as closureMapping show ClosureTask; | 10 import 'closure.dart' as closureMapping show ClosureTask; |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 /// | 373 /// |
| 374 /// [loadedLibraries] contains the newly loaded libraries. | 374 /// [loadedLibraries] contains the newly loaded libraries. |
| 375 /// | 375 /// |
| 376 /// The method returns a [Future] allowing for the loading of additional | 376 /// The method returns a [Future] allowing for the loading of additional |
| 377 /// libraries. | 377 /// libraries. |
| 378 LoadedLibraries processLoadedLibraries(LoadedLibraries loadedLibraries) { | 378 LoadedLibraries processLoadedLibraries(LoadedLibraries loadedLibraries) { |
| 379 loadedLibraries.forEachLibrary((LibraryEntity library) { | 379 loadedLibraries.forEachLibrary((LibraryEntity library) { |
| 380 backend.setAnnotations(library); | 380 backend.setAnnotations(library); |
| 381 }); | 381 }); |
| 382 | 382 |
| 383 for (Uri uri in resolvedUriTranslator.disallowedLibraryUris) { | 383 // TODO(efortuna, sigmund): These validation steps should be done in the |
| 384 if (loadedLibraries.containsLibrary(uri)) { | 384 // front end for the Kernel path since Kernel doesn't have the notion of |
| 385 Set<String> importChains = computeImportChainsFor(loadedLibraries, uri); | 385 // imports (everything has already been resolved). (See |
| 386 reporter.reportInfo( | 386 // https://github.com/dart-lang/sdk/issues/29368) |
| 387 NO_LOCATION_SPANNABLE, MessageKind.DISALLOWED_LIBRARY_IMPORT, { | 387 if (!options.useKernel && !options.loadFromDill) { |
| 388 'uri': uri, | 388 for (Uri uri in resolvedUriTranslator.disallowedLibraryUris) { |
| 389 'importChain': importChains | 389 if (loadedLibraries.containsLibrary(uri)) { |
| 390 .join(MessageTemplate.DISALLOWED_LIBRARY_IMPORT_PADDING) | 390 Set<String> importChains = |
| 391 }); | 391 computeImportChainsFor(loadedLibraries, uri); |
| 392 reporter.reportInfo( |
| 393 NO_LOCATION_SPANNABLE, MessageKind.DISALLOWED_LIBRARY_IMPORT, { |
| 394 'uri': uri, |
| 395 'importChain': importChains |
| 396 .join(MessageTemplate.DISALLOWED_LIBRARY_IMPORT_PADDING) |
| 397 }); |
| 398 } |
| 392 } | 399 } |
| 393 } | |
| 394 | 400 |
| 395 if (loadedLibraries.containsLibrary(Uris.dart_core)) { | 401 if (loadedLibraries.containsLibrary(Uris.dart_core)) { |
| 396 bool importsMirrorsLibrary = | 402 bool importsMirrorsLibrary = |
| 397 loadedLibraries.containsLibrary(Uris.dart_mirrors); | 403 loadedLibraries.containsLibrary(Uris.dart_mirrors); |
| 398 if (importsMirrorsLibrary && !backend.supportsReflection) { | 404 if (importsMirrorsLibrary && !backend.supportsReflection) { |
| 399 Set<String> importChains = | 405 Set<String> importChains = |
| 400 computeImportChainsFor(loadedLibraries, Uris.dart_mirrors); | 406 computeImportChainsFor(loadedLibraries, Uris.dart_mirrors); |
| 401 reporter.reportErrorMessage(NO_LOCATION_SPANNABLE, | 407 reporter.reportErrorMessage(NO_LOCATION_SPANNABLE, |
| 402 MessageKind.MIRRORS_LIBRARY_NOT_SUPPORT_BY_BACKEND, { | 408 MessageKind.MIRRORS_LIBRARY_NOT_SUPPORT_BY_BACKEND, { |
| 403 'importChain': importChains | 409 'importChain': importChains |
| 404 .join(MessageTemplate.MIRRORS_NOT_SUPPORTED_BY_BACKEND_PADDING) | 410 .join(MessageTemplate.MIRRORS_NOT_SUPPORTED_BY_BACKEND_PADDING) |
| 405 }); | 411 }); |
| 406 } else if (importsMirrorsLibrary && !options.enableExperimentalMirrors) { | 412 } else if (importsMirrorsLibrary && |
| 407 Set<String> importChains = | 413 !options.enableExperimentalMirrors) { |
| 408 computeImportChainsFor(loadedLibraries, Uris.dart_mirrors); | 414 Set<String> importChains = |
| 409 reporter.reportWarningMessage( | 415 computeImportChainsFor(loadedLibraries, Uris.dart_mirrors); |
| 410 NO_LOCATION_SPANNABLE, MessageKind.IMPORT_EXPERIMENTAL_MIRRORS, { | 416 reporter.reportWarningMessage( |
| 411 'importChain': importChains | 417 NO_LOCATION_SPANNABLE, MessageKind.IMPORT_EXPERIMENTAL_MIRRORS, { |
| 412 .join(MessageTemplate.IMPORT_EXPERIMENTAL_MIRRORS_PADDING) | 418 'importChain': importChains |
| 413 }); | 419 .join(MessageTemplate.IMPORT_EXPERIMENTAL_MIRRORS_PADDING) |
| 420 }); |
| 421 } |
| 414 } | 422 } |
| 415 } | 423 } |
| 416 backend.onLibrariesLoaded(loadedLibraries); | 424 backend.onLibrariesLoaded(loadedLibraries); |
| 417 return loadedLibraries; | 425 return loadedLibraries; |
| 418 } | 426 } |
| 419 | 427 |
| 420 // TODO(johnniwinther): Move this to [PatchParser] when it is moved to the | 428 // TODO(johnniwinther): Move this to [PatchParser] when it is moved to the |
| 421 // [JavaScriptBackend]. Currently needed for testing. | 429 // [JavaScriptBackend]. Currently needed for testing. |
| 422 String get patchVersion => backend.patchVersion; | 430 String get patchVersion => backend.patchVersion; |
| 423 | 431 |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 588 supportDeferredLoad: deferredLoadTask.isProgramSplit, | 596 supportDeferredLoad: deferredLoadTask.isProgramSplit, |
| 589 supportDumpInfo: options.dumpInfo, | 597 supportDumpInfo: options.dumpInfo, |
| 590 supportSerialization: serialization.supportSerialization); | 598 supportSerialization: serialization.supportSerialization); |
| 591 | 599 |
| 592 phase = PHASE_RESOLVING; | 600 phase = PHASE_RESOLVING; |
| 593 resolutionEnqueuer.applyImpact(mainImpact); | 601 resolutionEnqueuer.applyImpact(mainImpact); |
| 594 if (options.resolveOnly) { | 602 if (options.resolveOnly) { |
| 595 libraryLoader.libraries.where((LibraryEntity library) { | 603 libraryLoader.libraries.where((LibraryEntity library) { |
| 596 return !serialization.isDeserialized(library); | 604 return !serialization.isDeserialized(library); |
| 597 }).forEach((LibraryEntity library) { | 605 }).forEach((LibraryEntity library) { |
| 598 reporter | 606 reporter.log('Enqueuing ${library.canonicalUri}'); |
| 599 .log('Enqueuing ${(library as LibraryElement).canonicalUri}'); | |
| 600 resolutionEnqueuer.applyImpact(computeImpactForLibrary(library)); | 607 resolutionEnqueuer.applyImpact(computeImpactForLibrary(library)); |
| 601 }); | 608 }); |
| 602 } else if (analyzeAll) { | 609 } else if (analyzeAll) { |
| 603 libraryLoader.libraries.forEach((LibraryEntity library) { | 610 libraryLoader.libraries.forEach((LibraryEntity library) { |
| 604 reporter | 611 reporter.log('Enqueuing ${library.canonicalUri}'); |
| 605 .log('Enqueuing ${(library as LibraryElement).canonicalUri}'); | |
| 606 resolutionEnqueuer.applyImpact(computeImpactForLibrary(library)); | 612 resolutionEnqueuer.applyImpact(computeImpactForLibrary(library)); |
| 607 }); | 613 }); |
| 608 } else if (options.analyzeMain) { | 614 } else if (options.analyzeMain) { |
| 609 if (mainApp != null) { | 615 if (mainApp != null) { |
| 610 resolutionEnqueuer.applyImpact(computeImpactForLibrary(mainApp)); | 616 resolutionEnqueuer.applyImpact(computeImpactForLibrary(mainApp)); |
| 611 } | 617 } |
| 612 if (librariesToAnalyzeWhenRun != null) { | 618 if (librariesToAnalyzeWhenRun != null) { |
| 613 for (Uri libraryUri in librariesToAnalyzeWhenRun) { | 619 for (Uri libraryUri in librariesToAnalyzeWhenRun) { |
| 614 resolutionEnqueuer.applyImpact(computeImpactForLibrary( | 620 resolutionEnqueuer.applyImpact(computeImpactForLibrary( |
| 615 libraryLoader.lookupLibrary(libraryUri))); | 621 libraryLoader.lookupLibrary(libraryUri))); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 640 | 646 |
| 641 if (options.resolveOnly && !compilationFailed) { | 647 if (options.resolveOnly && !compilationFailed) { |
| 642 reporter.log('Serializing to ${options.resolutionOutput}'); | 648 reporter.log('Serializing to ${options.resolutionOutput}'); |
| 643 serialization.serializeToSink( | 649 serialization.serializeToSink( |
| 644 userOutputProvider.createOutputSink( | 650 userOutputProvider.createOutputSink( |
| 645 '', 'data', api.OutputType.serializationData), | 651 '', 'data', api.OutputType.serializationData), |
| 646 libraryLoader.libraries.where((LibraryEntity library) { | 652 libraryLoader.libraries.where((LibraryEntity library) { |
| 647 return !serialization.isDeserialized(library); | 653 return !serialization.isDeserialized(library); |
| 648 })); | 654 })); |
| 649 } | 655 } |
| 650 if (options.analyzeOnly) { | 656 if (options.analyzeOnly) return; |
| 651 if (!analyzeAll && !compilationFailed) { | |
| 652 // No point in reporting unused code when [analyzeAll] is true: all | |
| 653 // code is artificially used. | |
| 654 // If compilation failed, it is possible that the error prevents the | |
| 655 // compiler from analyzing all the code. | |
| 656 // TODO(johnniwinther): Reenable this when the reporting is more | |
| 657 // precise. | |
| 658 //reportUnusedCode(); | |
| 659 } | |
| 660 return; | |
| 661 } | |
| 662 assert(mainFunction != null); | 657 assert(mainFunction != null); |
| 663 | 658 |
| 664 ClosedWorldRefiner closedWorldRefiner = closeResolution(); | 659 ClosedWorldRefiner closedWorldRefiner = closeResolution(); |
| 665 ClosedWorld closedWorld = closedWorldRefiner.closedWorld; | 660 ClosedWorld closedWorld = closedWorldRefiner.closedWorld; |
| 666 | 661 |
| 667 reporter.log('Inferring types...'); | 662 reporter.log('Inferring types...'); |
| 668 globalInference.runGlobalTypeInference( | 663 globalInference.runGlobalTypeInference( |
| 669 mainFunction, closedWorld, closedWorldRefiner); | 664 mainFunction, closedWorld, closedWorldRefiner); |
| 670 | 665 |
| 671 if (stopAfterTypeInference) return; | 666 if (stopAfterTypeInference) return; |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 923 Element lookupElementIn(ScopeContainerElement container, String name) { | 918 Element lookupElementIn(ScopeContainerElement container, String name) { |
| 924 Element element = container.localLookup(name); | 919 Element element = container.localLookup(name); |
| 925 if (element == null) { | 920 if (element == null) { |
| 926 throw 'Could not find $name in $container'; | 921 throw 'Could not find $name in $container'; |
| 927 } | 922 } |
| 928 return element; | 923 return element; |
| 929 } | 924 } |
| 930 | 925 |
| 931 bool get isMockCompilation => false; | 926 bool get isMockCompilation => false; |
| 932 | 927 |
| 933 void reportUnusedCode() { | |
| 934 void checkLive(member) { | |
| 935 if (member.isMalformed) return; | |
| 936 if (member.isFunction) { | |
| 937 if (!resolutionWorldBuilder.isMemberUsed(member)) { | |
| 938 reporter.reportHintMessage( | |
| 939 member, MessageKind.UNUSED_METHOD, {'name': member.name}); | |
| 940 } | |
| 941 } else if (member.isClass) { | |
| 942 if (!member.isResolved) { | |
| 943 reporter.reportHintMessage( | |
| 944 member, MessageKind.UNUSED_CLASS, {'name': member.name}); | |
| 945 } else { | |
| 946 member.forEachLocalMember(checkLive); | |
| 947 } | |
| 948 } else if (member.isTypedef) { | |
| 949 if (!member.isResolved) { | |
| 950 reporter.reportHintMessage( | |
| 951 member, MessageKind.UNUSED_TYPEDEF, {'name': member.name}); | |
| 952 } | |
| 953 } | |
| 954 } | |
| 955 | |
| 956 libraryLoader.libraries.forEach((LibraryEntity entity) { | |
| 957 // TODO(ahe): Implement better heuristics to discover entry points of | |
| 958 // packages and use that to discover unused implementation details in | |
| 959 // packages. | |
| 960 LibraryElement library = entity; | |
| 961 if (library.isPlatformLibrary || library.isPackageLibrary) return; | |
| 962 library.compilationUnits.forEach((unit) { | |
| 963 unit.forEachLocalMember(checkLive); | |
| 964 }); | |
| 965 }); | |
| 966 } | |
| 967 | |
| 968 /// Helper for determining whether the current element is declared within | 928 /// Helper for determining whether the current element is declared within |
| 969 /// 'user code'. | 929 /// 'user code'. |
| 970 /// | 930 /// |
| 971 /// See [inUserCode] for what defines 'user code'. | 931 /// See [inUserCode] for what defines 'user code'. |
| 972 bool currentlyInUserCode() { | 932 bool currentlyInUserCode() { |
| 973 return inUserCode(currentElement); | 933 return inUserCode(currentElement); |
| 974 } | 934 } |
| 975 | 935 |
| 976 /// Helper for determining whether [element] is declared within 'user code'. | 936 /// Helper for determining whether [element] is declared within 'user code'. |
| 977 /// | 937 /// |
| (...skipping 1020 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1998 ResolutionFunctionType getLocalFunctionType(LocalFunctionElement function) { | 1958 ResolutionFunctionType getLocalFunctionType(LocalFunctionElement function) { |
| 1999 return function.type; | 1959 return function.type; |
| 2000 } | 1960 } |
| 2001 | 1961 |
| 2002 @override | 1962 @override |
| 2003 ResolutionDartType getUnaliasedType(ResolutionDartType type) { | 1963 ResolutionDartType getUnaliasedType(ResolutionDartType type) { |
| 2004 type.computeUnaliased(_resolution); | 1964 type.computeUnaliased(_resolution); |
| 2005 return type.unaliased; | 1965 return type.unaliased; |
| 2006 } | 1966 } |
| 2007 } | 1967 } |
| OLD | NEW |