| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 analyzer.src.task.dart; | 5 library analyzer.src.task.dart; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/ast/token.dart'; | 10 import 'package:analyzer/dart/ast/token.dart'; |
| (...skipping 1150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1161 | 1161 |
| 1162 @override | 1162 @override |
| 1163 TaskDescriptor get descriptor => DESCRIPTOR; | 1163 TaskDescriptor get descriptor => DESCRIPTOR; |
| 1164 | 1164 |
| 1165 @override | 1165 @override |
| 1166 void internalPerform() { | 1166 void internalPerform() { |
| 1167 LibraryElementImpl library = getRequiredInput(LIBRARY_INPUT); | 1167 LibraryElementImpl library = getRequiredInput(LIBRARY_INPUT); |
| 1168 // | 1168 // |
| 1169 // Compute export namespace. | 1169 // Compute export namespace. |
| 1170 // | 1170 // |
| 1171 ExportNamespaceBuilder builder = new ExportNamespaceBuilder(); | 1171 NamespaceBuilder builder = new NamespaceBuilder(); |
| 1172 Namespace namespace = builder.build(library); | 1172 Namespace namespace = builder.createExportNamespaceForLibrary(library); |
| 1173 library.exportNamespace = namespace; | 1173 library.exportNamespace = namespace; |
| 1174 // | 1174 // |
| 1175 // Update entry point. | 1175 // Update entry point. |
| 1176 // | 1176 // |
| 1177 if (library.entryPoint == null) { | 1177 if (library.entryPoint == null) { |
| 1178 Iterable<Element> exportedElements = namespace.definedNames.values; | 1178 Iterable<Element> exportedElements = namespace.definedNames.values; |
| 1179 library.entryPoint = exportedElements.firstWhere( | 1179 library.entryPoint = exportedElements.firstWhere( |
| 1180 (element) => element is FunctionElement && element.isEntryPoint, | 1180 (element) => element is FunctionElement && element.isEntryPoint, |
| 1181 orElse: () => null); | 1181 orElse: () => null); |
| 1182 } | 1182 } |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1458 BuildPublicNamespaceTask( | 1458 BuildPublicNamespaceTask( |
| 1459 InternalAnalysisContext context, AnalysisTarget target) | 1459 InternalAnalysisContext context, AnalysisTarget target) |
| 1460 : super(context, target); | 1460 : super(context, target); |
| 1461 | 1461 |
| 1462 @override | 1462 @override |
| 1463 TaskDescriptor get descriptor => DESCRIPTOR; | 1463 TaskDescriptor get descriptor => DESCRIPTOR; |
| 1464 | 1464 |
| 1465 @override | 1465 @override |
| 1466 void internalPerform() { | 1466 void internalPerform() { |
| 1467 LibraryElementImpl library = getRequiredInput(LIBRARY_INPUT); | 1467 LibraryElementImpl library = getRequiredInput(LIBRARY_INPUT); |
| 1468 library.publicNamespace = new PublicNamespaceBuilder().build(library); | 1468 NamespaceBuilder builder = new NamespaceBuilder(); |
| 1469 library.publicNamespace = builder.createPublicNamespaceForLibrary(library); |
| 1469 outputs[LIBRARY_ELEMENT3] = library; | 1470 outputs[LIBRARY_ELEMENT3] = library; |
| 1470 } | 1471 } |
| 1471 | 1472 |
| 1472 /** | 1473 /** |
| 1473 * Return a map from the names of the inputs of this kind of task to the task | 1474 * Return a map from the names of the inputs of this kind of task to the task |
| 1474 * input descriptors describing those inputs for a task with the | 1475 * input descriptors describing those inputs for a task with the |
| 1475 * given library [libSource]. | 1476 * given library [libSource]. |
| 1476 */ | 1477 */ |
| 1477 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | 1478 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { |
| 1478 Source source = target; | 1479 Source source = target; |
| (...skipping 966 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2445 * Create an [EvaluateUnitConstantsTask] based on the given [target] in | 2446 * Create an [EvaluateUnitConstantsTask] based on the given [target] in |
| 2446 * the given [context]. | 2447 * the given [context]. |
| 2447 */ | 2448 */ |
| 2448 static EvaluateUnitConstantsTask createTask( | 2449 static EvaluateUnitConstantsTask createTask( |
| 2449 AnalysisContext context, AnalysisTarget target) { | 2450 AnalysisContext context, AnalysisTarget target) { |
| 2450 return new EvaluateUnitConstantsTask(context, target); | 2451 return new EvaluateUnitConstantsTask(context, target); |
| 2451 } | 2452 } |
| 2452 } | 2453 } |
| 2453 | 2454 |
| 2454 /** | 2455 /** |
| 2455 * The helper for building the export [Namespace] of a [LibraryElement]. | |
| 2456 */ | |
| 2457 class ExportNamespaceBuilder { | |
| 2458 /** | |
| 2459 * Build the export [Namespace] of the given [LibraryElement]. | |
| 2460 */ | |
| 2461 Namespace build(LibraryElement library) { | |
| 2462 return new Namespace( | |
| 2463 _createExportMapping(library, new HashSet<LibraryElement>())); | |
| 2464 } | |
| 2465 | |
| 2466 /** | |
| 2467 * Create a mapping table representing the export namespace of the given | |
| 2468 * [library]. | |
| 2469 * | |
| 2470 * The given [visitedElements] a set of libraries that do not need to be | |
| 2471 * visited when processing the export directives of the given library because | |
| 2472 * all of the names defined by them will be added by another library. | |
| 2473 */ | |
| 2474 HashMap<String, Element> _createExportMapping( | |
| 2475 LibraryElement library, HashSet<LibraryElement> visitedElements) { | |
| 2476 visitedElements.add(library); | |
| 2477 try { | |
| 2478 HashMap<String, Element> definedNames = new HashMap<String, Element>(); | |
| 2479 // Add names of the export directives. | |
| 2480 for (ExportElement element in library.exports) { | |
| 2481 LibraryElement exportedLibrary = element.exportedLibrary; | |
| 2482 if (exportedLibrary != null && | |
| 2483 !visitedElements.contains(exportedLibrary)) { | |
| 2484 // | |
| 2485 // The exported library will be null if the URI does not reference a | |
| 2486 // valid library. | |
| 2487 // | |
| 2488 HashMap<String, Element> exportedNames = | |
| 2489 _createExportMapping(exportedLibrary, visitedElements); | |
| 2490 exportedNames = _applyCombinators(exportedNames, element.combinators); | |
| 2491 definedNames.addAll(exportedNames); | |
| 2492 } | |
| 2493 } | |
| 2494 // Add names of the public namespace. | |
| 2495 { | |
| 2496 Namespace publicNamespace = library.publicNamespace; | |
| 2497 if (publicNamespace != null) { | |
| 2498 definedNames.addAll(publicNamespace.definedNames); | |
| 2499 } | |
| 2500 } | |
| 2501 return definedNames; | |
| 2502 } finally { | |
| 2503 visitedElements.remove(library); | |
| 2504 } | |
| 2505 } | |
| 2506 | |
| 2507 /** | |
| 2508 * Apply the given [combinators] to all of the names in [definedNames]. | |
| 2509 */ | |
| 2510 static HashMap<String, Element> _applyCombinators( | |
| 2511 HashMap<String, Element> definedNames, | |
| 2512 List<NamespaceCombinator> combinators) { | |
| 2513 for (NamespaceCombinator combinator in combinators) { | |
| 2514 if (combinator is HideElementCombinator) { | |
| 2515 _hide(definedNames, combinator.hiddenNames); | |
| 2516 } else if (combinator is ShowElementCombinator) { | |
| 2517 definedNames = _show(definedNames, combinator.shownNames); | |
| 2518 } | |
| 2519 } | |
| 2520 return definedNames; | |
| 2521 } | |
| 2522 | |
| 2523 /** | |
| 2524 * Hide all of the [hiddenNames] by removing them from the given | |
| 2525 * [definedNames]. | |
| 2526 */ | |
| 2527 static void _hide( | |
| 2528 HashMap<String, Element> definedNames, List<String> hiddenNames) { | |
| 2529 for (String name in hiddenNames) { | |
| 2530 definedNames.remove(name); | |
| 2531 definedNames.remove('$name='); | |
| 2532 } | |
| 2533 } | |
| 2534 | |
| 2535 /** | |
| 2536 * Show only the given [shownNames] by removing all other names from the given | |
| 2537 * [definedNames]. | |
| 2538 */ | |
| 2539 static HashMap<String, Element> _show( | |
| 2540 HashMap<String, Element> definedNames, List<String> shownNames) { | |
| 2541 HashMap<String, Element> newNames = new HashMap<String, Element>(); | |
| 2542 for (String name in shownNames) { | |
| 2543 Element element = definedNames[name]; | |
| 2544 if (element != null) { | |
| 2545 newNames[name] = element; | |
| 2546 } | |
| 2547 String setterName = '$name='; | |
| 2548 element = definedNames[setterName]; | |
| 2549 if (element != null) { | |
| 2550 newNames[setterName] = element; | |
| 2551 } | |
| 2552 } | |
| 2553 return newNames; | |
| 2554 } | |
| 2555 } | |
| 2556 | |
| 2557 /** | |
| 2558 * A task that builds [USED_IMPORTED_ELEMENTS] for a unit. | 2456 * A task that builds [USED_IMPORTED_ELEMENTS] for a unit. |
| 2559 */ | 2457 */ |
| 2560 class GatherUsedImportedElementsTask extends SourceBasedAnalysisTask { | 2458 class GatherUsedImportedElementsTask extends SourceBasedAnalysisTask { |
| 2561 /** | 2459 /** |
| 2562 * The name of the [RESOLVED_UNIT10] input. | 2460 * The name of the [RESOLVED_UNIT10] input. |
| 2563 */ | 2461 */ |
| 2564 static const String UNIT_INPUT = 'UNIT_INPUT'; | 2462 static const String UNIT_INPUT = 'UNIT_INPUT'; |
| 2565 | 2463 |
| 2566 /** | 2464 /** |
| 2567 * The task descriptor describing this kind of task. | 2465 * The task descriptor describing this kind of task. |
| (...skipping 1407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3975 * Create a [PropagateVariableTypeTask] based on the given [target] in the | 3873 * Create a [PropagateVariableTypeTask] based on the given [target] in the |
| 3976 * given [context]. | 3874 * given [context]. |
| 3977 */ | 3875 */ |
| 3978 static PropagateVariableTypeTask createTask( | 3876 static PropagateVariableTypeTask createTask( |
| 3979 AnalysisContext context, AnalysisTarget target) { | 3877 AnalysisContext context, AnalysisTarget target) { |
| 3980 return new PropagateVariableTypeTask(context, target); | 3878 return new PropagateVariableTypeTask(context, target); |
| 3981 } | 3879 } |
| 3982 } | 3880 } |
| 3983 | 3881 |
| 3984 /** | 3882 /** |
| 3985 * The helper for building the public [Namespace] of a [LibraryElement]. | |
| 3986 */ | |
| 3987 class PublicNamespaceBuilder { | |
| 3988 final HashMap<String, Element> definedNames = new HashMap<String, Element>(); | |
| 3989 | |
| 3990 /** | |
| 3991 * Build a public [Namespace] of the given [library]. | |
| 3992 */ | |
| 3993 Namespace build(LibraryElement library) { | |
| 3994 definedNames.clear(); | |
| 3995 _addPublicNames(library.definingCompilationUnit); | |
| 3996 library.parts.forEach(_addPublicNames); | |
| 3997 return new Namespace(definedNames); | |
| 3998 } | |
| 3999 | |
| 4000 /** | |
| 4001 * Add the given [element] if it has a publicly visible name. | |
| 4002 */ | |
| 4003 void _addIfPublic(Element element) { | |
| 4004 String name = element.name; | |
| 4005 if (name != null && !Scope.isPrivateName(name)) { | |
| 4006 definedNames[name] = element; | |
| 4007 } | |
| 4008 } | |
| 4009 | |
| 4010 /** | |
| 4011 * Add all of the public top-level names that are defined in the given | |
| 4012 * [compilationUnit]. | |
| 4013 */ | |
| 4014 void _addPublicNames(CompilationUnitElement compilationUnit) { | |
| 4015 compilationUnit.accessors.forEach(_addIfPublic); | |
| 4016 compilationUnit.enums.forEach(_addIfPublic); | |
| 4017 compilationUnit.functions.forEach(_addIfPublic); | |
| 4018 compilationUnit.functionTypeAliases.forEach(_addIfPublic); | |
| 4019 compilationUnit.types.forEach(_addIfPublic); | |
| 4020 } | |
| 4021 } | |
| 4022 | |
| 4023 /** | |
| 4024 * A task that ensures that [LIBRARY_ELEMENT2] is ready for the target library | 3883 * A task that ensures that [LIBRARY_ELEMENT2] is ready for the target library |
| 4025 * source and its import/export closure. | 3884 * source and its import/export closure. |
| 4026 */ | 3885 */ |
| 4027 class ReadyLibraryElement2Task extends SourceBasedAnalysisTask { | 3886 class ReadyLibraryElement2Task extends SourceBasedAnalysisTask { |
| 4028 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | 3887 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( |
| 4029 'ReadyLibraryElement2Task', | 3888 'ReadyLibraryElement2Task', |
| 4030 createTask, | 3889 createTask, |
| 4031 buildInputs, | 3890 buildInputs, |
| 4032 <ResultDescriptor>[READY_LIBRARY_ELEMENT2]); | 3891 <ResultDescriptor>[READY_LIBRARY_ELEMENT2]); |
| 4033 | 3892 |
| (...skipping 1356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5390 | 5249 |
| 5391 @override | 5250 @override |
| 5392 bool moveNext() { | 5251 bool moveNext() { |
| 5393 if (_newSources.isEmpty) { | 5252 if (_newSources.isEmpty) { |
| 5394 return false; | 5253 return false; |
| 5395 } | 5254 } |
| 5396 currentTarget = _newSources.removeLast(); | 5255 currentTarget = _newSources.removeLast(); |
| 5397 return true; | 5256 return true; |
| 5398 } | 5257 } |
| 5399 } | 5258 } |
| OLD | NEW |