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

Side by Side Diff: pkg/analyzer/lib/src/generated/element.dart

Issue 1410643003: Add a public API for the results of constant evaluation (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years, 2 months 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) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 engine.element; 5 library engine.element;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 import 'dart:math' show min; 8 import 'dart:math' show min;
9 9
10 import 'package:analyzer/src/generated/utilities_general.dart'; 10 import 'package:analyzer/src/generated/utilities_general.dart';
11 import 'package:analyzer/src/task/dart.dart'; 11 import 'package:analyzer/src/task/dart.dart';
12 import 'package:analyzer/task/model.dart' 12 import 'package:analyzer/task/model.dart'
13 show AnalysisTarget, ConstantEvaluationTarget; 13 show AnalysisTarget, ConstantEvaluationTarget;
14 14
15 import 'ast.dart'; 15 import 'ast.dart';
16 import 'constant.dart' show EvaluationResultImpl; 16 import 'constant.dart' show DartObject, EvaluationResultImpl;
17 import 'engine.dart' show AnalysisContext, AnalysisEngine, AnalysisException; 17 import 'engine.dart' show AnalysisContext, AnalysisEngine, AnalysisException;
18 import 'html.dart' show XmlAttributeNode, XmlTagNode; 18 import 'html.dart' show XmlAttributeNode, XmlTagNode;
19 import 'java_core.dart'; 19 import 'java_core.dart';
20 import 'java_engine.dart'; 20 import 'java_engine.dart';
21 import 'resolver.dart'; 21 import 'resolver.dart';
22 import 'scanner.dart' show Keyword; 22 import 'scanner.dart' show Keyword;
23 import 'sdk.dart' show DartSdk; 23 import 'sdk.dart' show DartSdk;
24 import 'source.dart'; 24 import 'source.dart';
25 import 'utilities_collection.dart'; 25 import 'utilities_collection.dart';
26 import 'utilities_dart.dart'; 26 import 'utilities_dart.dart';
(...skipping 1726 matching lines...) Expand 10 before | Expand all | Expand 10 after
1753 */ 1753 */
1754 @deprecated // Use new ConstFieldElementImpl(name, offset) 1754 @deprecated // Use new ConstFieldElementImpl(name, offset)
1755 ConstFieldElementImpl.con2(String name, int offset) : super(name, offset); 1755 ConstFieldElementImpl.con2(String name, int offset) : super(name, offset);
1756 1756
1757 /** 1757 /**
1758 * Initialize a newly created field element to have the given [name]. 1758 * Initialize a newly created field element to have the given [name].
1759 */ 1759 */
1760 ConstFieldElementImpl.forNode(Identifier name) : super.forNode(name); 1760 ConstFieldElementImpl.forNode(Identifier name) : super.forNode(name);
1761 1761
1762 @override 1762 @override
1763 DartObject get constantValue => _result.value;
1764
1765 @override
1763 EvaluationResultImpl get evaluationResult => _result; 1766 EvaluationResultImpl get evaluationResult => _result;
1764 1767
1765 @override 1768 @override
1766 void set evaluationResult(EvaluationResultImpl result) { 1769 void set evaluationResult(EvaluationResultImpl result) {
1767 this._result = result; 1770 this._result = result;
1768 } 1771 }
1769 } 1772 }
1770 1773
1771 /** 1774 /**
1772 * A [LocalVariableElement] for a local 'const' variable that has an 1775 * A [LocalVariableElement] for a local 'const' variable that has an
(...skipping 11 matching lines...) Expand all
1784 * and [offset]. 1787 * and [offset].
1785 */ 1788 */
1786 ConstLocalVariableElementImpl(String name, int offset) : super(name, offset); 1789 ConstLocalVariableElementImpl(String name, int offset) : super(name, offset);
1787 1790
1788 /** 1791 /**
1789 * Initialize a newly created local variable element to have the given [name]. 1792 * Initialize a newly created local variable element to have the given [name].
1790 */ 1793 */
1791 ConstLocalVariableElementImpl.forNode(Identifier name) : super.forNode(name); 1794 ConstLocalVariableElementImpl.forNode(Identifier name) : super.forNode(name);
1792 1795
1793 @override 1796 @override
1797 DartObject get constantValue => _result.value;
1798
1799 @override
1794 EvaluationResultImpl get evaluationResult => _result; 1800 EvaluationResultImpl get evaluationResult => _result;
1795 1801
1796 @override 1802 @override
1797 void set evaluationResult(EvaluationResultImpl result) { 1803 void set evaluationResult(EvaluationResultImpl result) {
1798 this._result = result; 1804 this._result = result;
1799 } 1805 }
1800 } 1806 }
1801 1807
1802 /** 1808 /**
1803 * An element representing a constructor or a factory method defined within a 1809 * An element representing a constructor or a factory method defined within a
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
2102 */ 2108 */
2103 EvaluationResultImpl _result; 2109 EvaluationResultImpl _result;
2104 2110
2105 /** 2111 /**
2106 * Initialize a newly created top-level variable element to have the given 2112 * Initialize a newly created top-level variable element to have the given
2107 * [name]. 2113 * [name].
2108 */ 2114 */
2109 ConstTopLevelVariableElementImpl(Identifier name) : super.forNode(name); 2115 ConstTopLevelVariableElementImpl(Identifier name) : super.forNode(name);
2110 2116
2111 @override 2117 @override
2118 DartObject get constantValue => _result.value;
2119
2120 @override
2112 EvaluationResultImpl get evaluationResult => _result; 2121 EvaluationResultImpl get evaluationResult => _result;
2113 2122
2114 @override 2123 @override
2115 void set evaluationResult(EvaluationResultImpl result) { 2124 void set evaluationResult(EvaluationResultImpl result) {
2116 this._result = result; 2125 this._result = result;
2117 } 2126 }
2118 } 2127 }
2119 2128
2120 /** 2129 /**
2121 * Mixin used by elements that represent constant variables and have 2130 * Mixin used by elements that represent constant variables and have
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
2266 * The result of evaluating this variable's initializer. 2275 * The result of evaluating this variable's initializer.
2267 */ 2276 */
2268 EvaluationResultImpl _result; 2277 EvaluationResultImpl _result;
2269 2278
2270 /** 2279 /**
2271 * Initialize a newly created parameter element to have the given [name]. 2280 * Initialize a newly created parameter element to have the given [name].
2272 */ 2281 */
2273 DefaultFieldFormalParameterElementImpl(Identifier name) : super(name); 2282 DefaultFieldFormalParameterElementImpl(Identifier name) : super(name);
2274 2283
2275 @override 2284 @override
2285 DartObject get constantValue => _result.value;
2286
2287 @override
2276 EvaluationResultImpl get evaluationResult => _result; 2288 EvaluationResultImpl get evaluationResult => _result;
2277 2289
2278 @override 2290 @override
2279 void set evaluationResult(EvaluationResultImpl result) { 2291 void set evaluationResult(EvaluationResultImpl result) {
2280 this._result = result; 2292 this._result = result;
2281 } 2293 }
2282 } 2294 }
2283 2295
2284 /** 2296 /**
2285 * A [ParameterElement] for parameters that have an initializer. 2297 * A [ParameterElement] for parameters that have an initializer.
2286 */ 2298 */
2287 class DefaultParameterElementImpl extends ParameterElementImpl 2299 class DefaultParameterElementImpl extends ParameterElementImpl
2288 with ConstVariableElement { 2300 with ConstVariableElement {
2289 /** 2301 /**
2290 * The result of evaluating this variable's initializer. 2302 * The result of evaluating this variable's initializer.
2291 */ 2303 */
2292 EvaluationResultImpl _result; 2304 EvaluationResultImpl _result;
2293 2305
2294 /** 2306 /**
2295 * Initialize a newly created parameter element to have the given [name]. 2307 * Initialize a newly created parameter element to have the given [name].
2296 */ 2308 */
2297 DefaultParameterElementImpl(Identifier name) : super.forNode(name); 2309 DefaultParameterElementImpl(Identifier name) : super.forNode(name);
2298 2310
2299 @override 2311 @override
2312 DartObject get constantValue => _result.value;
2313
2314 @override
2300 EvaluationResultImpl get evaluationResult => _result; 2315 EvaluationResultImpl get evaluationResult => _result;
2301 2316
2302 @override 2317 @override
2303 void set evaluationResult(EvaluationResultImpl result) { 2318 void set evaluationResult(EvaluationResultImpl result) {
2304 this._result = result; 2319 this._result = result;
2305 } 2320 }
2306 2321
2307 @override 2322 @override
2308 DefaultFormalParameter computeNode() => 2323 DefaultFormalParameter computeNode() =>
2309 getNodeMatching((node) => node is DefaultFormalParameter); 2324 getNodeMatching((node) => node is DefaultFormalParameter);
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after
2652 /** 2667 /**
2653 * A single annotation associated with an element. 2668 * A single annotation associated with an element.
2654 */ 2669 */
2655 abstract class ElementAnnotation { 2670 abstract class ElementAnnotation {
2656 /** 2671 /**
2657 * An empty list of annotations. 2672 * An empty list of annotations.
2658 */ 2673 */
2659 static const List<ElementAnnotation> EMPTY_LIST = const <ElementAnnotation>[]; 2674 static const List<ElementAnnotation> EMPTY_LIST = const <ElementAnnotation>[];
2660 2675
2661 /** 2676 /**
2677 * Return a representation of the value of this annotation.
2678 *
2679 * Return `null` if the value of this annotation could not be computed because
2680 * of errors.
2681 */
2682 DartObject get constantValue;
2683
2684 /**
2662 * Return the element representing the field, variable, or const constructor 2685 * Return the element representing the field, variable, or const constructor
2663 * being used as an annotation. 2686 * being used as an annotation.
2664 */ 2687 */
2665 Element get element; 2688 Element get element;
2666 2689
2667 /** 2690 /**
2668 * Return `true` if this annotation marks the associated element as being 2691 * Return `true` if this annotation marks the associated element as being
2669 * deprecated. 2692 * deprecated.
2670 */ 2693 */
2671 bool get isDeprecated; 2694 bool get isDeprecated;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
2731 EvaluationResultImpl evaluationResult; 2754 EvaluationResultImpl evaluationResult;
2732 2755
2733 /** 2756 /**
2734 * Initialize a newly created annotation. The given [element] is the element 2757 * Initialize a newly created annotation. The given [element] is the element
2735 * representing the field, variable, or constructor being used as an 2758 * representing the field, variable, or constructor being used as an
2736 * annotation. 2759 * annotation.
2737 */ 2760 */
2738 ElementAnnotationImpl(this.element); 2761 ElementAnnotationImpl(this.element);
2739 2762
2740 @override 2763 @override
2764 DartObject get constantValue => evaluationResult.value;
2765
2766 @override
2741 bool get isDeprecated { 2767 bool get isDeprecated {
2742 if (element != null) { 2768 if (element != null) {
2743 LibraryElement library = element.library; 2769 LibraryElement library = element.library;
2744 if (library != null && library.isDartCore) { 2770 if (library != null && library.isDartCore) {
2745 if (element is ConstructorElement) { 2771 if (element is ConstructorElement) {
2746 ConstructorElement constructorElement = element as ConstructorElement; 2772 ConstructorElement constructorElement = element as ConstructorElement;
2747 if (constructorElement.enclosingElement.name == 2773 if (constructorElement.enclosingElement.name ==
2748 _DEPRECATED_CLASS_NAME) { 2774 _DEPRECATED_CLASS_NAME) {
2749 return true; 2775 return true;
2750 } 2776 }
(...skipping 7852 matching lines...) Expand 10 before | Expand all | Expand 10 after
10603 /** 10629 /**
10604 * A variable. There are concrete subclasses for different kinds of variables. 10630 * A variable. There are concrete subclasses for different kinds of variables.
10605 */ 10631 */
10606 abstract class VariableElement implements Element, ConstantEvaluationTarget { 10632 abstract class VariableElement implements Element, ConstantEvaluationTarget {
10607 /** 10633 /**
10608 * An empty list of variable elements. 10634 * An empty list of variable elements.
10609 */ 10635 */
10610 static const List<VariableElement> EMPTY_LIST = const <VariableElement>[]; 10636 static const List<VariableElement> EMPTY_LIST = const <VariableElement>[];
10611 10637
10612 /** 10638 /**
10639 * Return a representation of the value of this variable.
10640 *
10641 * Return `null` if either this variable was not declared with the 'const'
10642 * modifier or if the value of this variable could not be computed because of
10643 * errors.
10644 */
10645 DartObject get constantValue;
10646
10647 /**
10613 * Return `true` if this variable element did not have an explicit type 10648 * Return `true` if this variable element did not have an explicit type
10614 * specified for it. 10649 * specified for it.
10615 */ 10650 */
10616 bool get hasImplicitType; 10651 bool get hasImplicitType;
10617 10652
10618 /** 10653 /**
10619 * Return a synthetic function representing this variable's initializer, or 10654 * Return a synthetic function representing this variable's initializer, or
10620 * `null` if this variable does not have an initializer. The function will 10655 * `null` if this variable does not have an initializer. The function will
10621 * have no parameters. The return type of the function will be the 10656 * have no parameters. The return type of the function will be the
10622 * compile-time type of the initialization expression. 10657 * compile-time type of the initialization expression.
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
10705 */ 10740 */
10706 VariableElementImpl.forNode(Identifier name) : super.forNode(name); 10741 VariableElementImpl.forNode(Identifier name) : super.forNode(name);
10707 10742
10708 /** 10743 /**
10709 * Set whether this variable is const. 10744 * Set whether this variable is const.
10710 */ 10745 */
10711 void set const3(bool isConst) { 10746 void set const3(bool isConst) {
10712 setModifier(Modifier.CONST, isConst); 10747 setModifier(Modifier.CONST, isConst);
10713 } 10748 }
10714 10749
10750 @override
10751 DartObject get constantValue => null;
10752
10715 /** 10753 /**
10716 * Return the result of evaluating this variable's initializer as a 10754 * Return the result of evaluating this variable's initializer as a
10717 * compile-time constant expression, or `null` if this variable is not a 10755 * compile-time constant expression, or `null` if this variable is not a
10718 * 'const' variable, if it does not have an initializer, or if the compilation 10756 * 'const' variable, if it does not have an initializer, or if the compilation
10719 * unit containing the variable has not been resolved. 10757 * unit containing the variable has not been resolved.
10720 */ 10758 */
10721 EvaluationResultImpl get evaluationResult => null; 10759 EvaluationResultImpl get evaluationResult => null;
10722 10760
10723 /** 10761 /**
10724 * Set the result of evaluating this variable's initializer as a compile-time 10762 * Set the result of evaluating this variable's initializer as a compile-time
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
10917 10955
10918 @override 10956 @override
10919 void visitElement(Element element) { 10957 void visitElement(Element element) {
10920 int offset = element.nameOffset; 10958 int offset = element.nameOffset;
10921 if (offset != -1) { 10959 if (offset != -1) {
10922 map[offset] = element; 10960 map[offset] = element;
10923 } 10961 }
10924 super.visitElement(element); 10962 super.visitElement(element);
10925 } 10963 }
10926 } 10964 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/constant.dart ('k') | pkg/analyzer/lib/src/generated/element_handle.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698