Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(553)

Side by Side Diff: pkg/analyzer/lib/src/task/dart.dart

Issue 1539623002: Compute values of elements a unit constant expressions depend on. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/element/element.dart'; 9 import 'package:analyzer/dart/element/element.dart';
10 import 'package:analyzer/dart/element/type.dart'; 10 import 'package:analyzer/dart/element/type.dart';
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 'BUILD_LIBRARY_ERRORS', AnalysisError.NO_ERRORS); 77 'BUILD_LIBRARY_ERRORS', AnalysisError.NO_ERRORS);
78 78
79 /** 79 /**
80 * A list of the [ConstantEvaluationTarget]s defined in a unit. This includes 80 * A list of the [ConstantEvaluationTarget]s defined in a unit. This includes
81 * constants defined at top level, statically inside classes, and local to 81 * constants defined at top level, statically inside classes, and local to
82 * functions, as well as constant constructors, annotations, and default values 82 * functions, as well as constant constructors, annotations, and default values
83 * of parameters. 83 * of parameters.
84 * 84 *
85 * The result is only available for [LibrarySpecificUnit]s. 85 * The result is only available for [LibrarySpecificUnit]s.
86 */ 86 */
87 final ListResultDescriptor< 87 final ListResultDescriptor<ConstantEvaluationTarget>
88 ConstantEvaluationTarget> COMPILATION_UNIT_CONSTANTS = 88 COMPILATION_UNIT_CONSTANTS =
89 new ListResultDescriptor<ConstantEvaluationTarget>( 89 new ListResultDescriptor<ConstantEvaluationTarget>(
90 'COMPILATION_UNIT_CONSTANTS', null, 90 'COMPILATION_UNIT_CONSTANTS', null,
91 cachingPolicy: ELEMENT_CACHING_POLICY); 91 cachingPolicy: ELEMENT_CACHING_POLICY);
92 92
93 /** 93 /**
94 * The element model associated with a single compilation unit. 94 * The element model associated with a single compilation unit.
95 * 95 *
96 * The result is only available for [LibrarySpecificUnit]s. 96 * The result is only available for [LibrarySpecificUnit]s.
97 */ 97 */
98 final ResultDescriptor<CompilationUnitElement> COMPILATION_UNIT_ELEMENT = 98 final ResultDescriptor<CompilationUnitElement> COMPILATION_UNIT_ELEMENT =
99 new ResultDescriptor<CompilationUnitElement>( 99 new ResultDescriptor<CompilationUnitElement>(
100 'COMPILATION_UNIT_ELEMENT', null, 100 'COMPILATION_UNIT_ELEMENT', null,
101 cachingPolicy: ELEMENT_CACHING_POLICY); 101 cachingPolicy: ELEMENT_CACHING_POLICY);
102 102
103 /** 103 /**
104 * The list of [ConstantEvaluationTarget]s on which the target constant element 104 * The list of [ConstantEvaluationTarget]s on which the target constant element
105 * depends. 105 * depends.
106 * 106 *
107 * The result is only available for targets representing a 107 * The result is only available for targets representing a
108 * [ConstantEvaluationTarget] (i.e. a constant variable declaration, a constant 108 * [ConstantEvaluationTarget] (i.e. a constant variable declaration, a constant
109 * constructor, or a parameter element with a default value). 109 * constructor, or a parameter element with a default value).
110 */ 110 */
111 final ListResultDescriptor<ConstantEvaluationTarget> CONSTANT_DEPENDENCIES = 111 final ListResultDescriptor<ConstantEvaluationTarget> CONSTANT_DEPENDENCIES =
112 new ListResultDescriptor<ConstantEvaluationTarget>( 112 new ListResultDescriptor<ConstantEvaluationTarget>(
113 'CONSTANT_DEPENDENCIES', const <ConstantEvaluationTarget>[]); 113 'CONSTANT_DEPENDENCIES', const <ConstantEvaluationTarget>[]);
114 114
115 /** 115 /**
116 * The list of [ConstantEvaluationTarget]s on which constant expressions of a
117 * unit depend.
118 *
119 * The result is only available for [LibrarySpecificUnit]s.
120 */
121 final ListResultDescriptor<ConstantEvaluationTarget>
122 CONSTANT_EXPRESSIONS_DEPENDENCIES =
123 new ListResultDescriptor<ConstantEvaluationTarget>(
124 'CONSTANT_EXPRESSIONS_DEPENDENCIES',
125 const <ConstantEvaluationTarget>[]);
126
127 /**
116 * A [ConstantEvaluationTarget] that has been successfully constant-evaluated. 128 * A [ConstantEvaluationTarget] that has been successfully constant-evaluated.
117 * 129 *
118 * TODO(paulberry): is ELEMENT_CACHING_POLICY the correct caching policy? 130 * TODO(paulberry): is ELEMENT_CACHING_POLICY the correct caching policy?
119 * 131 *
120 * The result is only available for [ConstantEvaluationTarget]s. 132 * The result is only available for [ConstantEvaluationTarget]s.
121 * 133 *
122 */ 134 */
123 final ResultDescriptor<ConstantEvaluationTarget> CONSTANT_VALUE = 135 final ResultDescriptor<ConstantEvaluationTarget> CONSTANT_VALUE =
124 new ResultDescriptor<ConstantEvaluationTarget>('CONSTANT_VALUE', null, 136 new ResultDescriptor<ConstantEvaluationTarget>('CONSTANT_VALUE', null,
125 cachingPolicy: ELEMENT_CACHING_POLICY); 137 cachingPolicy: ELEMENT_CACHING_POLICY);
(...skipping 26 matching lines...) Expand all
152 new ListResultDescriptor<AnalysisError>( 164 new ListResultDescriptor<AnalysisError>(
153 'HINT_ERRORS', AnalysisError.NO_ERRORS); 165 'HINT_ERRORS', AnalysisError.NO_ERRORS);
154 166
155 /** 167 /**
156 * A list of the [VariableElement]s whose type should be inferred that another 168 * A list of the [VariableElement]s whose type should be inferred that another
157 * inferable static variable (the target) depends on. 169 * inferable static variable (the target) depends on.
158 * 170 *
159 * The result is only available for [VariableElement]s, and only when strong 171 * The result is only available for [VariableElement]s, and only when strong
160 * mode is enabled. 172 * mode is enabled.
161 */ 173 */
162 final ListResultDescriptor< 174 final ListResultDescriptor<VariableElement>
163 VariableElement> INFERABLE_STATIC_VARIABLE_DEPENDENCIES = 175 INFERABLE_STATIC_VARIABLE_DEPENDENCIES =
164 new ListResultDescriptor<VariableElement>( 176 new ListResultDescriptor<VariableElement>(
165 'INFERABLE_STATIC_VARIABLE_DEPENDENCIES', null); 177 'INFERABLE_STATIC_VARIABLE_DEPENDENCIES', null);
166 178
167 /** 179 /**
168 * A list of the [VariableElement]s defined in a unit whose type should be 180 * A list of the [VariableElement]s defined in a unit whose type should be
169 * inferred. This includes variables defined at the library level as well as 181 * inferred. This includes variables defined at the library level as well as
170 * static members inside classes. 182 * static members inside classes.
171 * 183 *
172 * The result is only available for [LibrarySpecificUnit]s, and only when strong 184 * The result is only available for [LibrarySpecificUnit]s, and only when strong
173 * mode is enabled. 185 * mode is enabled.
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after
762 source, unit, librarySpecificUnit.library); 774 source, unit, librarySpecificUnit.library);
763 } else { 775 } else {
764 new DeclarationResolver().resolve(unit, element); 776 new DeclarationResolver().resolve(unit, element);
765 } 777 }
766 // 778 //
767 // Prepare constants. 779 // Prepare constants.
768 // 780 //
769 ConstantFinder constantFinder = 781 ConstantFinder constantFinder =
770 new ConstantFinder(context, source, librarySpecificUnit.library); 782 new ConstantFinder(context, source, librarySpecificUnit.library);
771 unit.accept(constantFinder); 783 unit.accept(constantFinder);
772 List<ConstantEvaluationTarget> constants = new List< 784 List<ConstantEvaluationTarget> constants =
773 ConstantEvaluationTarget>.from(constantFinder.constantsToCompute); 785 new List<ConstantEvaluationTarget>.from(
786 constantFinder.constantsToCompute);
774 // 787 //
775 // Record outputs. 788 // Record outputs.
776 // 789 //
777 outputs[COMPILATION_UNIT_CONSTANTS] = constants; 790 outputs[COMPILATION_UNIT_CONSTANTS] = constants;
778 outputs[COMPILATION_UNIT_ELEMENT] = element; 791 outputs[COMPILATION_UNIT_ELEMENT] = element;
779 outputs[RESOLVED_UNIT1] = unit; 792 outputs[RESOLVED_UNIT1] = unit;
780 } 793 }
781 794
782 /** 795 /**
783 * Return a map from the names of the inputs of this kind of task to the task 796 * Return a map from the names of the inputs of this kind of task to the task
(...skipping 1097 matching lines...) Expand 10 before | Expand all | Expand 10 after
1881 // 1894 //
1882 // Record outputs. 1895 // Record outputs.
1883 // 1896 //
1884 outputs[INFERABLE_STATIC_VARIABLE_DEPENDENCIES] = gatherer.results.toList(); 1897 outputs[INFERABLE_STATIC_VARIABLE_DEPENDENCIES] = gatherer.results.toList();
1885 } 1898 }
1886 1899
1887 /** 1900 /**
1888 * Return `true` if the given [variable] is a static variable whose type 1901 * Return `true` if the given [variable] is a static variable whose type
1889 * should be inferred. 1902 * should be inferred.
1890 */ 1903 */
1891 bool _isInferableStatic(VariableElement variable) => variable.isStatic && 1904 bool _isInferableStatic(VariableElement variable) =>
1905 variable.isStatic &&
1892 variable.hasImplicitType && 1906 variable.hasImplicitType &&
1893 variable.initializer != null; 1907 variable.initializer != null;
1894 1908
1895 /** 1909 /**
1896 * Return a map from the names of the inputs of this kind of task to the task 1910 * Return a map from the names of the inputs of this kind of task to the task
1897 * input descriptors describing those inputs for a task with the 1911 * input descriptors describing those inputs for a task with the
1898 * given [target]. 1912 * given [target].
1899 */ 1913 */
1900 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { 1914 static Map<String, TaskInput> buildInputs(AnalysisTarget target) {
1901 if (target is VariableElement) { 1915 if (target is VariableElement) {
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
2066 // 2080 //
2067 outputs[PROPAGABLE_VARIABLE_DEPENDENCIES] = gatherer.results.toList(); 2081 outputs[PROPAGABLE_VARIABLE_DEPENDENCIES] = gatherer.results.toList();
2068 } 2082 }
2069 2083
2070 /** 2084 /**
2071 * Return `true` if the given [variable] is a variable whose type can be 2085 * Return `true` if the given [variable] is a variable whose type can be
2072 * propagated. 2086 * propagated.
2073 */ 2087 */
2074 bool _isPropagable(VariableElement variable) => 2088 bool _isPropagable(VariableElement variable) =>
2075 variable is PropertyInducingElement && 2089 variable is PropertyInducingElement &&
2076 (variable.isConst || variable.isFinal) && 2090 (variable.isConst || variable.isFinal) &&
2077 variable.hasImplicitType && 2091 variable.hasImplicitType &&
2078 variable.initializer != null; 2092 variable.initializer != null;
2079 2093
2080 /** 2094 /**
2081 * Return a map from the names of the inputs of this kind of task to the task 2095 * Return a map from the names of the inputs of this kind of task to the task
2082 * input descriptors describing those inputs for a task with the 2096 * input descriptors describing those inputs for a task with the
2083 * given [target]. 2097 * given [target].
2084 */ 2098 */
2085 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { 2099 static Map<String, TaskInput> buildInputs(AnalysisTarget target) {
2086 if (target is VariableElement) { 2100 if (target is VariableElement) {
2087 CompilationUnitElementImpl unit = target 2101 CompilationUnitElementImpl unit = target
2088 .getAncestor((Element element) => element is CompilationUnitElement); 2102 .getAncestor((Element element) => element is CompilationUnitElement);
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
2400 * Return a map from the names of the inputs of this kind of task to the task 2414 * Return a map from the names of the inputs of this kind of task to the task
2401 * input descriptors describing those inputs for a task with the 2415 * input descriptors describing those inputs for a task with the
2402 * given [target]. 2416 * given [target].
2403 */ 2417 */
2404 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { 2418 static Map<String, TaskInput> buildInputs(AnalysisTarget target) {
2405 LibrarySpecificUnit unit = target; 2419 LibrarySpecificUnit unit = target;
2406 return <String, TaskInput>{ 2420 return <String, TaskInput>{
2407 'libraryElement': LIBRARY_ELEMENT8.of(unit.library), 2421 'libraryElement': LIBRARY_ELEMENT8.of(unit.library),
2408 UNIT_INPUT: RESOLVED_UNIT10.of(unit), 2422 UNIT_INPUT: RESOLVED_UNIT10.of(unit),
2409 CONSTANT_VALUES: 2423 CONSTANT_VALUES:
2410 COMPILATION_UNIT_CONSTANTS.of(unit).toListOf(CONSTANT_VALUE) 2424 COMPILATION_UNIT_CONSTANTS.of(unit).toListOf(CONSTANT_VALUE),
2425 'constantExpressionsDependencies':
2426 CONSTANT_EXPRESSIONS_DEPENDENCIES.of(unit).toListOf(CONSTANT_VALUE)
2411 }; 2427 };
2412 } 2428 }
2413 2429
2414 /** 2430 /**
2415 * Create an [EvaluateUnitConstantsTask] based on the given [target] in 2431 * Create an [EvaluateUnitConstantsTask] based on the given [target] in
2416 * the given [context]. 2432 * the given [context].
2417 */ 2433 */
2418 static EvaluateUnitConstantsTask createTask( 2434 static EvaluateUnitConstantsTask createTask(
2419 AnalysisContext context, AnalysisTarget target) { 2435 AnalysisContext context, AnalysisTarget target) {
2420 return new EvaluateUnitConstantsTask(context, target); 2436 return new EvaluateUnitConstantsTask(context, target);
(...skipping 2243 matching lines...) Expand 10 before | Expand all | Expand 10 after
4664 * The name of the [TYPE_PROVIDER] input. 4680 * The name of the [TYPE_PROVIDER] input.
4665 */ 4681 */
4666 static const String TYPE_PROVIDER_INPUT = 'TYPE_PROVIDER_INPUT'; 4682 static const String TYPE_PROVIDER_INPUT = 'TYPE_PROVIDER_INPUT';
4667 4683
4668 /** 4684 /**
4669 * The name of the [RESOLVED_UNIT9] input. 4685 * The name of the [RESOLVED_UNIT9] input.
4670 */ 4686 */
4671 static const String UNIT_INPUT = 'UNIT_INPUT'; 4687 static const String UNIT_INPUT = 'UNIT_INPUT';
4672 4688
4673 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( 4689 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor(
4674 'ResolveUnitTask', 4690 'ResolveUnitTask', createTask, buildInputs, <ResultDescriptor>[
4675 createTask, 4691 CONSTANT_EXPRESSIONS_DEPENDENCIES,
4676 buildInputs, 4692 RESOLVE_UNIT_ERRORS,
4677 <ResultDescriptor>[RESOLVE_UNIT_ERRORS, RESOLVED_UNIT10]); 4693 RESOLVED_UNIT10
4694 ]);
4678 4695
4679 ResolveUnitTask( 4696 ResolveUnitTask(
4680 InternalAnalysisContext context, LibrarySpecificUnit compilationUnit) 4697 InternalAnalysisContext context, LibrarySpecificUnit compilationUnit)
4681 : super(context, compilationUnit); 4698 : super(context, compilationUnit);
4682 4699
4683 @override 4700 @override
4684 TaskDescriptor get descriptor => DESCRIPTOR; 4701 TaskDescriptor get descriptor => DESCRIPTOR;
4685 4702
4686 @override 4703 @override
4687 void internalPerform() { 4704 void internalPerform() {
4688 // 4705 //
4689 // Prepare inputs. 4706 // Prepare inputs.
4690 // 4707 //
4708 LibrarySpecificUnit target = this.target;
4691 LibraryElement libraryElement = getRequiredInput(LIBRARY_INPUT); 4709 LibraryElement libraryElement = getRequiredInput(LIBRARY_INPUT);
4692 CompilationUnit unit = getRequiredInput(UNIT_INPUT); 4710 CompilationUnit unit = getRequiredInput(UNIT_INPUT);
4693 TypeProvider typeProvider = getRequiredInput(TYPE_PROVIDER_INPUT); 4711 TypeProvider typeProvider = getRequiredInput(TYPE_PROVIDER_INPUT);
4694 // 4712 //
4695 // Resolve everything. 4713 // Resolve everything.
4696 // 4714 //
4697 CompilationUnitElement unitElement = unit.element; 4715 CompilationUnitElement unitElement = unit.element;
4698 RecordingErrorListener errorListener = new RecordingErrorListener(); 4716 RecordingErrorListener errorListener = new RecordingErrorListener();
4699 ResolverVisitor visitor = new ResolverVisitor( 4717 ResolverVisitor visitor = new ResolverVisitor(
4700 libraryElement, unitElement.source, typeProvider, errorListener); 4718 libraryElement, unitElement.source, typeProvider, errorListener);
4701 unit.accept(visitor); 4719 unit.accept(visitor);
4702 // 4720 //
4721 // Compute constant expressions' dependencies.
4722 //
4723 List<ConstantEvaluationTarget> constExprDependencies;
4724 {
4725 ConstantExpressionsDependenciesFinder finder =
4726 new ConstantExpressionsDependenciesFinder();
4727 unit.accept(finder);
4728 constExprDependencies = finder.dependencies.toList();
4729 }
4730 //
4703 // Record outputs. 4731 // Record outputs.
4704 // 4732 //
4705 // TODO(brianwilkerson) This task modifies the element model (by copying the 4733 // TODO(brianwilkerson) This task modifies the element model (by copying the
4706 // AST's for constructor initializers into it) but does not produce an 4734 // AST's for constructor initializers into it) but does not produce an
4707 // updated version of the element model. 4735 // updated version of the element model.
4708 // 4736 //
4737 outputs[CONSTANT_EXPRESSIONS_DEPENDENCIES] = constExprDependencies;
4709 outputs[RESOLVE_UNIT_ERRORS] = getTargetSourceErrors(errorListener, target); 4738 outputs[RESOLVE_UNIT_ERRORS] = getTargetSourceErrors(errorListener, target);
4710 outputs[RESOLVED_UNIT10] = unit; 4739 outputs[RESOLVED_UNIT10] = unit;
4711 } 4740 }
4712 4741
4713 /** 4742 /**
4714 * Return a map from the names of the inputs of this kind of task to the task 4743 * Return a map from the names of the inputs of this kind of task to the task
4715 * input descriptors describing those inputs for a task with the given 4744 * input descriptors describing those inputs for a task with the given
4716 * [target]. 4745 * [target].
4717 */ 4746 */
4718 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { 4747 static Map<String, TaskInput> buildInputs(AnalysisTarget target) {
(...skipping 612 matching lines...) Expand 10 before | Expand all | Expand 10 after
5331 5360
5332 @override 5361 @override
5333 bool moveNext() { 5362 bool moveNext() {
5334 if (_newSources.isEmpty) { 5363 if (_newSources.isEmpty) {
5335 return false; 5364 return false;
5336 } 5365 }
5337 currentTarget = _newSources.removeLast(); 5366 currentTarget = _newSources.removeLast();
5338 return true; 5367 return true;
5339 } 5368 }
5340 } 5369 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/constant.dart ('k') | pkg/analyzer/test/generated/non_error_resolver_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698