| 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.inputs; | 5 library analyzer.src.task.inputs; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analyzer/task/model.dart'; | 9 import 'package:analyzer/task/model.dart'; |
| 10 | 10 |
| 11 /** | 11 /** |
| 12 * A function that converts an object of the type [B] into a [TaskInput]. | 12 * A function that converts an object of the type [B] into a [TaskInput]. |
| 13 * This is used, for example, by a [ListToListTaskInput] to create task inputs | 13 * This is used, for example, by a [ListToListTaskInput] to create task inputs |
| 14 * for each value in a list of values. | 14 * for each value in a list of values. |
| 15 */ | 15 */ |
| 16 typedef TaskInput<E> GenerateTaskInputs<B, E>(B object); | 16 typedef TaskInput<E> GenerateTaskInputs<B, E>(B object); |
| 17 | 17 |
| 18 /** | 18 /** |
| 19 * A function that maps one [value] to another value. | 19 * A function that maps one [value] to another value. |
| 20 */ | 20 */ |
| 21 typedef R Mapper<P, R>(P value); | 21 typedef R Mapper<P, R>(P value); |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * An input to an [AnalysisTask] that is computed by accessing a single result | 24 * An input to an [AnalysisTask] that is computed by accessing a single result |
| 25 * defined on a single target. | 25 * defined on a single target. |
| 26 */ | 26 */ |
| 27 class ConstantTaskInput<V> extends TaskInputImpl<V> { |
| 28 final V value; |
| 29 |
| 30 ConstantTaskInput(this.value); |
| 31 |
| 32 @override |
| 33 TaskInputBuilder<V> createBuilder() { |
| 34 return new ConstantTaskInputBuilder<V>(this); |
| 35 } |
| 36 } |
| 37 |
| 38 /** |
| 39 * A [TaskInputBuilder] used to build an input based on a [ConstantTaskInput]. |
| 40 */ |
| 41 class ConstantTaskInputBuilder<V> implements TaskInputBuilder<V> { |
| 42 final ConstantTaskInput input; |
| 43 |
| 44 ConstantTaskInputBuilder(this.input); |
| 45 |
| 46 @override |
| 47 ResultDescriptor get currentResult => null; |
| 48 |
| 49 @override |
| 50 AnalysisTarget get currentTarget => null; |
| 51 |
| 52 @override |
| 53 void set currentValue(Object value) { |
| 54 throw new StateError('Only supported after moveNext() returns true'); |
| 55 } |
| 56 |
| 57 @override |
| 58 bool get flushOnAccess => false; |
| 59 |
| 60 @override |
| 61 V get inputValue => input.value; |
| 62 |
| 63 @override |
| 64 void currentValueNotAvailable() { |
| 65 throw new StateError('Only supported after moveNext() returns true'); |
| 66 } |
| 67 |
| 68 @override |
| 69 bool moveNext() => false; |
| 70 } |
| 71 |
| 72 /** |
| 73 * An input to an [AnalysisTask] that is computed by accessing a single result |
| 74 * defined on a single target. |
| 75 */ |
| 27 class ListTaskInputImpl<E> extends SimpleTaskInput<List<E>> | 76 class ListTaskInputImpl<E> extends SimpleTaskInput<List<E>> |
| 28 with ListTaskInputMixin<E> implements ListTaskInput<E> { | 77 with ListTaskInputMixin<E> |
| 78 implements ListTaskInput<E> { |
| 29 /** | 79 /** |
| 30 * Initialize a newly created task input that computes the input by accessing | 80 * Initialize a newly created task input that computes the input by accessing |
| 31 * the given [result] associated with the given [target]. | 81 * the given [result] associated with the given [target]. |
| 32 */ | 82 */ |
| 33 ListTaskInputImpl(AnalysisTarget target, ResultDescriptor<List<E>> result) | 83 ListTaskInputImpl(AnalysisTarget target, ResultDescriptor<List<E>> result) |
| 34 : super(target, result); | 84 : super._unflushable(target, result); |
| 35 } | 85 } |
| 36 | 86 |
| 37 /** | 87 /** |
| 38 * A mixin-ready implementation of [ListTaskInput]. | 88 * A mixin-ready implementation of [ListTaskInput]. |
| 39 */ | 89 */ |
| 40 abstract class ListTaskInputMixin<E> implements ListTaskInput<E> { | 90 abstract class ListTaskInputMixin<E> implements ListTaskInput<E> { |
| 41 ListTaskInput /*<V>*/ toList(UnaryFunction<E, dynamic /*<V>*/ > mapper) { | 91 ListTaskInput /*<V>*/ toList(UnaryFunction<E, dynamic /*<V>*/ > mapper) { |
| 42 return new ListToListTaskInput<E, dynamic /*V*/ >(this, mapper); | 92 return new ListToListTaskInput<E, dynamic /*V*/ >(this, mapper); |
| 43 } | 93 } |
| 44 | 94 |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 _resultValue = new HashMap<B, E>(); | 211 _resultValue = new HashMap<B, E>(); |
| 162 } | 212 } |
| 163 } | 213 } |
| 164 | 214 |
| 165 /** | 215 /** |
| 166 * A mixin-ready implementation of [MapTaskInput]. | 216 * A mixin-ready implementation of [MapTaskInput]. |
| 167 */ | 217 */ |
| 168 abstract class MapTaskInputMixin<K, V> implements MapTaskInput<K, V> { | 218 abstract class MapTaskInputMixin<K, V> implements MapTaskInput<K, V> { |
| 169 TaskInput<List /*<E>*/ > toFlattenList( | 219 TaskInput<List /*<E>*/ > toFlattenList( |
| 170 BinaryFunction<K, dynamic /*element of V*/, dynamic /*<E>*/ > mapper) { | 220 BinaryFunction<K, dynamic /*element of V*/, dynamic /*<E>*/ > mapper) { |
| 171 return new MapToFlattenListTaskInput<K, dynamic /*element of V*/, dynamic /*
E*/ >( | 221 return new MapToFlattenListTaskInput<K, dynamic /*element of V*/, |
| 222 dynamic /*E*/ >( |
| 172 this as MapTaskInput<K, List /*<element of V>*/ >, mapper); | 223 this as MapTaskInput<K, List /*<element of V>*/ >, mapper); |
| 173 } | 224 } |
| 174 } | 225 } |
| 175 | 226 |
| 176 /** | 227 /** |
| 177 * A [TaskInput] that is computed by the following steps. | 228 * A [TaskInput] that is computed by the following steps. |
| 178 * | 229 * |
| 179 * First the [base] task input is used to compute a [Map]-valued result. | 230 * First the [base] task input is used to compute a [Map]-valued result. |
| 180 * The values of the [Map] must be [List]s. | 231 * The values of the [Map] must be [List]s. |
| 181 * | 232 * |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 @override | 285 @override |
| 235 void set currentValue(Object value) { | 286 void set currentValue(Object value) { |
| 236 if (currentBuilder == null) { | 287 if (currentBuilder == null) { |
| 237 throw new StateError( | 288 throw new StateError( |
| 238 'Cannot set the result value when there is no current result'); | 289 'Cannot set the result value when there is no current result'); |
| 239 } | 290 } |
| 240 currentBuilder.currentValue = value; | 291 currentBuilder.currentValue = value; |
| 241 } | 292 } |
| 242 | 293 |
| 243 @override | 294 @override |
| 295 bool get flushOnAccess => currentBuilder.flushOnAccess; |
| 296 |
| 297 @override |
| 244 void currentValueNotAvailable() { | 298 void currentValueNotAvailable() { |
| 245 if (currentBuilder == null) { | 299 if (currentBuilder == null) { |
| 246 throw new StateError( | 300 throw new StateError( |
| 247 'Cannot set the result value when there is no current result'); | 301 'Cannot set the result value when there is no current result'); |
| 248 } | 302 } |
| 249 currentBuilder.currentValueNotAvailable(); | 303 currentBuilder.currentValueNotAvailable(); |
| 250 } | 304 } |
| 251 | 305 |
| 252 @override | 306 @override |
| 253 bool moveNext() { | 307 bool moveNext() { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 } | 348 } |
| 295 // No more Map values/keys to transform. | 349 // No more Map values/keys to transform. |
| 296 return false; | 350 return false; |
| 297 } | 351 } |
| 298 } | 352 } |
| 299 | 353 |
| 300 /** | 354 /** |
| 301 * An input to an [AnalysisTask] that is computed by mapping the value of | 355 * An input to an [AnalysisTask] that is computed by mapping the value of |
| 302 * another task input to a list of values. | 356 * another task input to a list of values. |
| 303 */ | 357 */ |
| 304 class ObjectToListTaskInput<E> extends TaskInputImpl<List<E>> with ListTaskInput
Mixin<E> | 358 class ObjectToListTaskInput<E> extends TaskInputImpl<List<E>> |
| 359 with ListTaskInputMixin<E> |
| 305 implements ListTaskInput<E> { | 360 implements ListTaskInput<E> { |
| 306 /** | 361 /** |
| 307 * The input used to compute the value to be mapped. | 362 * The input used to compute the value to be mapped. |
| 308 */ | 363 */ |
| 309 final TaskInput baseInput; | 364 final TaskInput baseInput; |
| 310 | 365 |
| 311 /** | 366 /** |
| 312 * The function used to map the value of the base input to the list of values. | 367 * The function used to map the value of the base input to the list of values. |
| 313 */ | 368 */ |
| 314 final Mapper<Object, List<E>> mapper; | 369 final Mapper<Object, List<E>> mapper; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 @override | 439 @override |
| 385 void set currentValue(Object value) { | 440 void set currentValue(Object value) { |
| 386 if (builder == null) { | 441 if (builder == null) { |
| 387 throw new StateError( | 442 throw new StateError( |
| 388 'Cannot set the result value when there is no current result'); | 443 'Cannot set the result value when there is no current result'); |
| 389 } | 444 } |
| 390 builder.currentValue = value; | 445 builder.currentValue = value; |
| 391 } | 446 } |
| 392 | 447 |
| 393 @override | 448 @override |
| 449 bool get flushOnAccess => builder.flushOnAccess; |
| 450 |
| 451 @override |
| 394 List<E> get inputValue { | 452 List<E> get inputValue { |
| 395 if (builder != null) { | 453 if (builder != null) { |
| 396 throw new StateError('Result value has not been created'); | 454 throw new StateError('Result value has not been created'); |
| 397 } | 455 } |
| 398 return _inputValue; | 456 return _inputValue; |
| 399 } | 457 } |
| 400 | 458 |
| 401 @override | 459 @override |
| 402 void currentValueNotAvailable() { | 460 void currentValueNotAvailable() { |
| 403 if (builder == null) { | 461 if (builder == null) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 * The target on which the result is defined. | 493 * The target on which the result is defined. |
| 436 */ | 494 */ |
| 437 final AnalysisTarget target; | 495 final AnalysisTarget target; |
| 438 | 496 |
| 439 /** | 497 /** |
| 440 * The result to be accessed. | 498 * The result to be accessed. |
| 441 */ | 499 */ |
| 442 final ResultDescriptor<V> result; | 500 final ResultDescriptor<V> result; |
| 443 | 501 |
| 444 /** | 502 /** |
| 503 * Return `true` if the value accessed by this input builder should be flushed |
| 504 * from the cache at the time it is retrieved. |
| 505 */ |
| 506 final bool flushOnAccess; |
| 507 |
| 508 /** |
| 445 * Initialize a newly created task input that computes the input by accessing | 509 * Initialize a newly created task input that computes the input by accessing |
| 446 * the given [result] associated with the given [target]. | 510 * the given [result] associated with the given [target]. |
| 447 */ | 511 */ |
| 448 SimpleTaskInput(this.target, this.result); | 512 SimpleTaskInput(this.target, this.result, {this.flushOnAccess: false}); |
| 513 |
| 514 /** |
| 515 * Initialize a newly created task input that computes the input by accessing |
| 516 * the given [result] associated with the given [target]. |
| 517 */ |
| 518 SimpleTaskInput._unflushable(this.target, this.result) |
| 519 : flushOnAccess = false; |
| 449 | 520 |
| 450 @override | 521 @override |
| 451 TaskInputBuilder<V> createBuilder() => new SimpleTaskInputBuilder<V>(this); | 522 TaskInputBuilder<V> createBuilder() => new SimpleTaskInputBuilder<V>(this); |
| 452 } | 523 } |
| 453 | 524 |
| 454 /** | 525 /** |
| 455 * A [TaskInputBuilder] used to build an input based on a [SimpleTaskInput]. | 526 * A [TaskInputBuilder] used to build an input based on a [SimpleTaskInput]. |
| 456 */ | 527 */ |
| 457 class SimpleTaskInputBuilder<V> implements TaskInputBuilder<V> { | 528 class SimpleTaskInputBuilder<V> implements TaskInputBuilder<V> { |
| 458 /** | 529 /** |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 void set currentValue(Object value) { | 581 void set currentValue(Object value) { |
| 511 if (_state != _AT) { | 582 if (_state != _AT) { |
| 512 throw new StateError( | 583 throw new StateError( |
| 513 'Cannot set the result value when there is no current result'); | 584 'Cannot set the result value when there is no current result'); |
| 514 } | 585 } |
| 515 _resultValue = value as V; | 586 _resultValue = value as V; |
| 516 _resultSet = true; | 587 _resultSet = true; |
| 517 } | 588 } |
| 518 | 589 |
| 519 @override | 590 @override |
| 591 bool get flushOnAccess => input.flushOnAccess; |
| 592 |
| 593 @override |
| 520 V get inputValue { | 594 V get inputValue { |
| 521 if (_state != _AFTER) { | 595 if (_state != _AFTER) { |
| 522 throw new StateError('Result value has not been created'); | 596 throw new StateError('Result value has not been created'); |
| 523 } | 597 } |
| 524 return _resultValue; | 598 return _resultValue; |
| 525 } | 599 } |
| 526 | 600 |
| 527 @override | 601 @override |
| 528 void currentValueNotAvailable() { | 602 void currentValueNotAvailable() { |
| 529 if (_state != _AT) { | 603 if (_state != _AT) { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 618 @override | 692 @override |
| 619 void set currentValue(Object value) { | 693 void set currentValue(Object value) { |
| 620 if (currentBuilder == null) { | 694 if (currentBuilder == null) { |
| 621 throw new StateError( | 695 throw new StateError( |
| 622 'Cannot set the result value when there is no current result'); | 696 'Cannot set the result value when there is no current result'); |
| 623 } | 697 } |
| 624 currentBuilder.currentValue = value; | 698 currentBuilder.currentValue = value; |
| 625 } | 699 } |
| 626 | 700 |
| 627 @override | 701 @override |
| 702 bool get flushOnAccess => currentBuilder.flushOnAccess; |
| 703 |
| 704 @override |
| 628 Map<String, Object> get inputValue { | 705 Map<String, Object> get inputValue { |
| 629 if (nameIndex < inputNames.length) { | 706 if (nameIndex < inputNames.length) { |
| 630 throw new StateError('Result value has not been created'); | 707 throw new StateError('Result value has not been created'); |
| 631 } | 708 } |
| 632 return inputs; | 709 return inputs; |
| 633 } | 710 } |
| 634 | 711 |
| 635 /** | 712 /** |
| 636 * Assuming that there is a current input, return its name. | 713 * Assuming that there is a current input, return its name. |
| 637 */ | 714 */ |
| (...skipping 27 matching lines...) Expand all Loading... |
| 665 if (currentBuilder.inputValue != null) { | 742 if (currentBuilder.inputValue != null) { |
| 666 inputs[_currentName] = currentBuilder.inputValue; | 743 inputs[_currentName] = currentBuilder.inputValue; |
| 667 } | 744 } |
| 668 nameIndex++; | 745 nameIndex++; |
| 669 } | 746 } |
| 670 if (nameIndex >= inputNames.length) { | 747 if (nameIndex >= inputNames.length) { |
| 671 // There is no next value, so we're done. | 748 // There is no next value, so we're done. |
| 672 return false; | 749 return false; |
| 673 } | 750 } |
| 674 currentBuilder = inputDescriptors[_currentName].createBuilder(); | 751 currentBuilder = inputDescriptors[_currentName].createBuilder(); |
| 675 // NOTE: This assumes that every builder will require at least one result | 752 while (!currentBuilder.moveNext()) { |
| 676 // value to be created. If that assumption is every broken, this method will | 753 if (currentBuilder.inputValue != null) { |
| 677 // need to be changed to advance until we find a builder that does require | 754 inputs[_currentName] = currentBuilder.inputValue; |
| 678 // a result to be computed (or run out of builders). | 755 } |
| 679 return currentBuilder.moveNext(); | 756 nameIndex++; |
| 757 if (nameIndex >= inputNames.length) { |
| 758 // There is no next value, so we're done. |
| 759 return false; |
| 760 } |
| 761 currentBuilder = inputDescriptors[_currentName].createBuilder(); |
| 762 } |
| 763 return true; |
| 680 } | 764 } |
| 681 } | 765 } |
| 682 | 766 |
| 683 /** | 767 /** |
| 684 * An input to an [AnalysisTask] that is computed by the following steps. First | 768 * An input to an [AnalysisTask] that is computed by the following steps. First |
| 685 * another (base) task input is used to compute a [List]-valued result. An input | 769 * another (base) task input is used to compute a [List]-valued result. An input |
| 686 * generator function is then used to map each element of that list to a task | 770 * generator function is then used to map each element of that list to a task |
| 687 * input. Finally, each of the task inputs are used to access analysis results, | 771 * input. Finally, each of the task inputs are used to access analysis results, |
| 688 * and a collection of the analysis results is used as the input to the task. | 772 * and a collection of the analysis results is used as the input to the task. |
| 689 */ | 773 */ |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 764 @override | 848 @override |
| 765 void set currentValue(Object value) { | 849 void set currentValue(Object value) { |
| 766 if (currentBuilder == null) { | 850 if (currentBuilder == null) { |
| 767 throw new StateError( | 851 throw new StateError( |
| 768 'Cannot set the result value when there is no current result'); | 852 'Cannot set the result value when there is no current result'); |
| 769 } | 853 } |
| 770 currentBuilder.currentValue = value; | 854 currentBuilder.currentValue = value; |
| 771 } | 855 } |
| 772 | 856 |
| 773 @override | 857 @override |
| 858 bool get flushOnAccess => currentBuilder.flushOnAccess; |
| 859 |
| 860 @override |
| 774 C get inputValue { | 861 C get inputValue { |
| 775 if (currentBuilder != null || _resultValue == null) { | 862 if (currentBuilder != null || _resultValue == null) { |
| 776 throw new StateError('Result value has not been created'); | 863 throw new StateError('Result value has not been created'); |
| 777 } | 864 } |
| 778 return _resultValue; | 865 return _resultValue; |
| 779 } | 866 } |
| 780 | 867 |
| 781 /** | 868 /** |
| 782 * The list of values being built. | 869 * The list of values being built. |
| 783 */ | 870 */ |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 831 return false; | 918 return false; |
| 832 } | 919 } |
| 833 _baseListElement = _baseList[_baseListIndex]; | 920 _baseListElement = _baseList[_baseListIndex]; |
| 834 currentBuilder = input.generateTaskInputs(_baseListElement).createBuilder(); | 921 currentBuilder = input.generateTaskInputs(_baseListElement).createBuilder(); |
| 835 return currentBuilder.moveNext(); | 922 return currentBuilder.moveNext(); |
| 836 } | 923 } |
| 837 | 924 |
| 838 void _addResultElement(B baseElement, E resultElement); | 925 void _addResultElement(B baseElement, E resultElement); |
| 839 void _initResultValue(); | 926 void _initResultValue(); |
| 840 } | 927 } |
| OLD | NEW |