| 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/src/context/cache.dart'; | 9 import 'package:analyzer/src/context/cache.dart'; |
| 10 import 'package:analyzer/src/generated/ast.dart'; | 10 import 'package:analyzer/src/generated/ast.dart'; |
| (...skipping 1818 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1829 AnalysisContext context, AnalysisTarget target) { | 1829 AnalysisContext context, AnalysisTarget target) { |
| 1830 return new ComputeConstantValueTask(context, target); | 1830 return new ComputeConstantValueTask(context, target); |
| 1831 } | 1831 } |
| 1832 } | 1832 } |
| 1833 | 1833 |
| 1834 /** | 1834 /** |
| 1835 * A task that computes the [INFERABLE_STATIC_VARIABLE_DEPENDENCIES] for a | 1835 * A task that computes the [INFERABLE_STATIC_VARIABLE_DEPENDENCIES] for a |
| 1836 * static variable whose type should be inferred. | 1836 * static variable whose type should be inferred. |
| 1837 */ | 1837 */ |
| 1838 class ComputeInferableStaticVariableDependenciesTask | 1838 class ComputeInferableStaticVariableDependenciesTask |
| 1839 extends ConstantEvaluationAnalysisTask { | 1839 extends InferStaticVariableTask { |
| 1840 /** | 1840 /** |
| 1841 * The name of the [RESOLVED_UNIT5] input. | 1841 * The name of the [RESOLVED_UNIT5] input. |
| 1842 */ | 1842 */ |
| 1843 static const String UNIT_INPUT = 'UNIT_INPUT'; | 1843 static const String UNIT_INPUT = 'UNIT_INPUT'; |
| 1844 | 1844 |
| 1845 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | 1845 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( |
| 1846 'ComputeInferableStaticVariableDependenciesTask', | 1846 'ComputeInferableStaticVariableDependenciesTask', |
| 1847 createTask, | 1847 createTask, |
| 1848 buildInputs, | 1848 buildInputs, |
| 1849 <ResultDescriptor>[INFERABLE_STATIC_VARIABLE_DEPENDENCIES]); | 1849 <ResultDescriptor>[INFERABLE_STATIC_VARIABLE_DEPENDENCIES]); |
| 1850 | 1850 |
| 1851 ComputeInferableStaticVariableDependenciesTask( | 1851 ComputeInferableStaticVariableDependenciesTask( |
| 1852 InternalAnalysisContext context, VariableElement variable) | 1852 InternalAnalysisContext context, VariableElement variable) |
| 1853 : super(context, variable); | 1853 : super(context, variable); |
| 1854 | 1854 |
| 1855 @override | 1855 @override |
| 1856 TaskDescriptor get descriptor => DESCRIPTOR; | 1856 TaskDescriptor get descriptor => DESCRIPTOR; |
| 1857 | 1857 |
| 1858 @override | 1858 @override |
| 1859 void internalPerform() { | 1859 void internalPerform() { |
| 1860 // | 1860 // |
| 1861 // Prepare inputs. | 1861 // Prepare inputs. |
| 1862 // | 1862 // |
| 1863 VariableElement variable = target; | |
| 1864 CompilationUnit unit = getRequiredInput(UNIT_INPUT); | 1863 CompilationUnit unit = getRequiredInput(UNIT_INPUT); |
| 1865 // | 1864 // |
| 1866 // Compute dependencies. | 1865 // Compute dependencies. |
| 1867 // | 1866 // |
| 1868 NodeLocator locator = new NodeLocator(variable.nameOffset); | 1867 VariableDeclaration declaration = getDeclaration(unit); |
| 1869 AstNode node = locator.searchWithin(unit); | |
| 1870 VariableDeclaration declaration = | |
| 1871 node.getAncestor((AstNode ancestor) => ancestor is VariableDeclaration); | |
| 1872 if (declaration == null || declaration.name != node) { | |
| 1873 throw new AnalysisException( | |
| 1874 "NodeLocator failed to find a variable's declaration"); | |
| 1875 } | |
| 1876 VariableGatherer gatherer = new VariableGatherer(_isInferableStatic); | 1868 VariableGatherer gatherer = new VariableGatherer(_isInferableStatic); |
| 1877 declaration.initializer.accept(gatherer); | 1869 declaration.initializer.accept(gatherer); |
| 1878 // | 1870 // |
| 1879 // Record outputs. | 1871 // Record outputs. |
| 1880 // | 1872 // |
| 1881 outputs[INFERABLE_STATIC_VARIABLE_DEPENDENCIES] = gatherer.results.toList(); | 1873 outputs[INFERABLE_STATIC_VARIABLE_DEPENDENCIES] = gatherer.results.toList(); |
| 1882 } | 1874 } |
| 1883 | 1875 |
| 1884 /** | 1876 /** |
| 1885 * Return `true` if the given [variable] is a static variable whose type | 1877 * Return `true` if the given [variable] is a static variable whose type |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2020 static ComputeLibraryCycleTask createTask( | 2012 static ComputeLibraryCycleTask createTask( |
| 2021 AnalysisContext context, AnalysisTarget target) { | 2013 AnalysisContext context, AnalysisTarget target) { |
| 2022 return new ComputeLibraryCycleTask(context, target); | 2014 return new ComputeLibraryCycleTask(context, target); |
| 2023 } | 2015 } |
| 2024 } | 2016 } |
| 2025 | 2017 |
| 2026 /** | 2018 /** |
| 2027 * A task that computes the [PROPAGABLE_VARIABLE_DEPENDENCIES] for a variable. | 2019 * A task that computes the [PROPAGABLE_VARIABLE_DEPENDENCIES] for a variable. |
| 2028 */ | 2020 */ |
| 2029 class ComputePropagableVariableDependenciesTask | 2021 class ComputePropagableVariableDependenciesTask |
| 2030 extends ConstantEvaluationAnalysisTask { | 2022 extends InferStaticVariableTask { |
| 2031 /** | 2023 /** |
| 2032 * The name of the [RESOLVED_UNIT5] input. | 2024 * The name of the [RESOLVED_UNIT5] input. |
| 2033 */ | 2025 */ |
| 2034 static const String UNIT_INPUT = 'UNIT_INPUT'; | 2026 static const String UNIT_INPUT = 'UNIT_INPUT'; |
| 2035 | 2027 |
| 2036 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | 2028 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( |
| 2037 'ComputePropagableVariableDependenciesTask', | 2029 'ComputePropagableVariableDependenciesTask', |
| 2038 createTask, | 2030 createTask, |
| 2039 buildInputs, | 2031 buildInputs, |
| 2040 <ResultDescriptor>[PROPAGABLE_VARIABLE_DEPENDENCIES]); | 2032 <ResultDescriptor>[PROPAGABLE_VARIABLE_DEPENDENCIES]); |
| 2041 | 2033 |
| 2042 ComputePropagableVariableDependenciesTask( | 2034 ComputePropagableVariableDependenciesTask( |
| 2043 InternalAnalysisContext context, VariableElement variable) | 2035 InternalAnalysisContext context, VariableElement variable) |
| 2044 : super(context, variable); | 2036 : super(context, variable); |
| 2045 | 2037 |
| 2046 @override | 2038 @override |
| 2047 TaskDescriptor get descriptor => DESCRIPTOR; | 2039 TaskDescriptor get descriptor => DESCRIPTOR; |
| 2048 | 2040 |
| 2049 @override | 2041 @override |
| 2050 void internalPerform() { | 2042 void internalPerform() { |
| 2051 // | 2043 // |
| 2052 // Prepare inputs. | 2044 // Prepare inputs. |
| 2053 // | 2045 // |
| 2054 VariableElement variable = target; | |
| 2055 CompilationUnit unit = getRequiredInput(UNIT_INPUT); | 2046 CompilationUnit unit = getRequiredInput(UNIT_INPUT); |
| 2056 // | 2047 // |
| 2057 // Compute dependencies. | 2048 // Compute dependencies. |
| 2058 // | 2049 // |
| 2059 NodeLocator locator = new NodeLocator(variable.nameOffset); | 2050 VariableDeclaration declaration = getDeclaration(unit); |
| 2060 AstNode node = locator.searchWithin(unit); | |
| 2061 VariableDeclaration declaration = | |
| 2062 node.getAncestor((AstNode ancestor) => ancestor is VariableDeclaration); | |
| 2063 if (declaration == null || declaration.name != node) { | |
| 2064 throw new AnalysisException( | |
| 2065 "NodeLocator failed to find a variable's declaration"); | |
| 2066 } | |
| 2067 VariableGatherer gatherer = new VariableGatherer(_isPropagable); | 2051 VariableGatherer gatherer = new VariableGatherer(_isPropagable); |
| 2068 declaration.initializer.accept(gatherer); | 2052 declaration.initializer.accept(gatherer); |
| 2069 // | 2053 // |
| 2070 // Record outputs. | 2054 // Record outputs. |
| 2071 // | 2055 // |
| 2072 outputs[PROPAGABLE_VARIABLE_DEPENDENCIES] = gatherer.results.toList(); | 2056 outputs[PROPAGABLE_VARIABLE_DEPENDENCIES] = gatherer.results.toList(); |
| 2073 } | 2057 } |
| 2074 | 2058 |
| 2075 /** | 2059 /** |
| 2076 * Return `true` if the given [variable] is a variable whose type can be | 2060 * Return `true` if the given [variable] is a variable whose type can be |
| (...skipping 885 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2962 InferStaticVariableTask( | 2946 InferStaticVariableTask( |
| 2963 InternalAnalysisContext context, VariableElement variable) | 2947 InternalAnalysisContext context, VariableElement variable) |
| 2964 : super(context, variable); | 2948 : super(context, variable); |
| 2965 | 2949 |
| 2966 /** | 2950 /** |
| 2967 * Return the declaration of the target within the given compilation [unit]. | 2951 * Return the declaration of the target within the given compilation [unit]. |
| 2968 * Throw an exception if the declaration cannot be found. | 2952 * Throw an exception if the declaration cannot be found. |
| 2969 */ | 2953 */ |
| 2970 VariableDeclaration getDeclaration(CompilationUnit unit) { | 2954 VariableDeclaration getDeclaration(CompilationUnit unit) { |
| 2971 VariableElement variable = target; | 2955 VariableElement variable = target; |
| 2972 NodeLocator locator = new NodeLocator(variable.nameOffset); | 2956 // Usually: Type ^name = ... |
| 2957 // Sometimes there is no space after the type: List<Type>^name = ... |
| 2958 // So, we need to use an offset within (or right after) the name: |
| 2959 // Type n^ame = |
| 2960 // List<Type>n^ame = |
| 2961 // Type x^= |
| 2962 int searchOffset = variable.nameOffset + 1; |
| 2963 NodeLocator locator = new NodeLocator(searchOffset); |
| 2973 AstNode node = locator.searchWithin(unit); | 2964 AstNode node = locator.searchWithin(unit); |
| 2974 VariableDeclaration declaration = | 2965 VariableDeclaration declaration = |
| 2975 node.getAncestor((AstNode ancestor) => ancestor is VariableDeclaration); | 2966 node.getAncestor((AstNode ancestor) => ancestor is VariableDeclaration); |
| 2976 if (declaration == null || declaration.name != node) { | 2967 if (declaration == null || declaration.name != node) { |
| 2977 throw new AnalysisException( | 2968 throw new AnalysisException( |
| 2978 "Failed to find the declaration of the variable ${variable.displayName
} in ${variable.source}"); | 2969 "Failed to find the declaration of the variable " |
| 2970 "${variable.displayName} in ${variable.source}"); |
| 2979 } | 2971 } |
| 2980 return declaration; | 2972 return declaration; |
| 2981 } | 2973 } |
| 2982 } | 2974 } |
| 2983 | 2975 |
| 2984 /** | 2976 /** |
| 2985 * A task that ensures that all of the inferable static variables in a | 2977 * A task that ensures that all of the inferable static variables in a |
| 2986 * compilation unit have had their type inferred. | 2978 * compilation unit have had their type inferred. |
| 2987 */ | 2979 */ |
| 2988 class InferStaticVariableTypesInUnitTask extends SourceBasedAnalysisTask { | 2980 class InferStaticVariableTypesInUnitTask extends SourceBasedAnalysisTask { |
| (...skipping 2293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5282 | 5274 |
| 5283 @override | 5275 @override |
| 5284 bool moveNext() { | 5276 bool moveNext() { |
| 5285 if (_newSources.isEmpty) { | 5277 if (_newSources.isEmpty) { |
| 5286 return false; | 5278 return false; |
| 5287 } | 5279 } |
| 5288 currentTarget = _newSources.removeLast(); | 5280 currentTarget = _newSources.removeLast(); |
| 5289 return true; | 5281 return true; |
| 5290 } | 5282 } |
| 5291 } | 5283 } |
| OLD | NEW |