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

Unified Diff: pkg/analyzer/lib/src/dart/element/element.dart

Issue 1966323002: Add API to force the computation of constant values (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/analyzer/lib/dart/element/element.dart ('k') | pkg/analyzer/lib/src/dart/element/handle.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/lib/src/dart/element/element.dart
diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart
index bcac6b1e3051481fe0cbd7113d66ab579533ba7e..79ff2d3494b083b0736f9c835dd3c258807f49b3 100644
--- a/pkg/analyzer/lib/src/dart/element/element.dart
+++ b/pkg/analyzer/lib/src/dart/element/element.dart
@@ -27,6 +27,7 @@ import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/generated/utilities_collection.dart';
import 'package:analyzer/src/generated/utilities_dart.dart';
import 'package:analyzer/src/generated/utilities_general.dart';
+import 'package:analyzer/src/task/dart.dart';
/**
* For AST nodes that could be in both the getter and setter contexts
@@ -1264,11 +1265,6 @@ class CompilationUnitElementImpl extends UriReferencedElementImpl
*/
class ConstFieldElementImpl extends FieldElementImpl with ConstVariableElement {
/**
- * The result of evaluating this variable's initializer.
- */
- EvaluationResultImpl _result;
-
- /**
* Initialize a newly created synthetic field element to have the given
* [name] and [offset].
*/
@@ -1278,17 +1274,6 @@ class ConstFieldElementImpl extends FieldElementImpl with ConstVariableElement {
* Initialize a newly created field element to have the given [name].
*/
ConstFieldElementImpl.forNode(Identifier name) : super.forNode(name);
-
- @override
- DartObject get constantValue => _result?.value;
-
- @override
- EvaluationResultImpl get evaluationResult => _result;
-
- @override
- void set evaluationResult(EvaluationResultImpl result) {
- this._result = result;
- }
}
/**
@@ -1298,11 +1283,6 @@ class ConstFieldElementImpl extends FieldElementImpl with ConstVariableElement {
class ConstLocalVariableElementImpl extends LocalVariableElementImpl
with ConstVariableElement {
/**
- * The result of evaluating this variable's initializer.
- */
- EvaluationResultImpl _result;
-
- /**
* Initialize a newly created local variable element to have the given [name]
* and [offset].
*/
@@ -1312,17 +1292,6 @@ class ConstLocalVariableElementImpl extends LocalVariableElementImpl
* Initialize a newly created local variable element to have the given [name].
*/
ConstLocalVariableElementImpl.forNode(Identifier name) : super.forNode(name);
-
- @override
- DartObject get constantValue => _result?.value;
-
- @override
- EvaluationResultImpl get evaluationResult => _result;
-
- @override
- void set evaluationResult(EvaluationResultImpl result) {
- this._result = result;
- }
}
/**
@@ -1454,11 +1423,6 @@ class ConstructorElementImpl extends ExecutableElementImpl
class ConstTopLevelVariableElementImpl extends TopLevelVariableElementImpl
with ConstVariableElement {
/**
- * The result of evaluating this variable's initializer.
- */
- EvaluationResultImpl _result;
-
- /**
* Initialize a newly created synthetic top-level variable element to have the
* given [name] and [offset].
*/
@@ -1471,17 +1435,6 @@ class ConstTopLevelVariableElementImpl extends TopLevelVariableElementImpl
*/
ConstTopLevelVariableElementImpl.forNode(Identifier name)
: super.forNode(name);
-
- @override
- DartObject get constantValue => _result?.value;
-
- @override
- EvaluationResultImpl get evaluationResult => _result;
-
- @override
- void set evaluationResult(EvaluationResultImpl result) {
- this._result = result;
- }
}
/**
@@ -1495,7 +1448,7 @@ class ConstTopLevelVariableElementImpl extends TopLevelVariableElementImpl
*
* This class is not intended to be part of the public API for analyzer.
*/
-abstract class ConstVariableElement {
+abstract class ConstVariableElement implements ConstantEvaluationTarget {
/**
* If this element represents a constant variable, and it has an initializer,
* a copy of the initializer for the constant. Otherwise `null`.
@@ -1506,6 +1459,22 @@ abstract class ConstVariableElement {
* initializers.
*/
Expression constantInitializer;
+
+ @override
+ EvaluationResultImpl evaluationResult;
+
+ /**
+ * Return a representation of the value of this variable, forcing the value
+ * to be computed if it had not previously been computed, or `null` if either
+ * this variable was not declared with the 'const' modifier or if the value of
+ * this variable could not be computed because of errors.
+ */
+ DartObject computeConstantValue() {
+ if (evaluationResult == null) {
+ context?.computeResult(this, CONSTANT_VALUE);
+ }
+ return evaluationResult?.value;
+ }
}
/**
@@ -1514,11 +1483,6 @@ abstract class ConstVariableElement {
class DefaultFieldFormalParameterElementImpl
extends FieldFormalParameterElementImpl with ConstVariableElement {
/**
- * The result of evaluating this variable's initializer.
- */
- EvaluationResultImpl _result;
-
- /**
* Initialize a newly created parameter element to have the given [name] and
* [nameOffset].
*/
@@ -1530,17 +1494,6 @@ class DefaultFieldFormalParameterElementImpl
*/
DefaultFieldFormalParameterElementImpl.forNode(Identifier name)
: super.forNode(name);
-
- @override
- DartObject get constantValue => _result?.value;
-
- @override
- EvaluationResultImpl get evaluationResult => _result;
-
- @override
- void set evaluationResult(EvaluationResultImpl result) {
- this._result = result;
- }
}
/**
@@ -1549,11 +1502,6 @@ class DefaultFieldFormalParameterElementImpl
class DefaultParameterElementImpl extends ParameterElementImpl
with ConstVariableElement {
/**
- * The result of evaluating this variable's initializer.
- */
- EvaluationResultImpl _result;
-
- /**
* Initialize a newly created parameter element to have the given [name] and
* [nameOffset].
*/
@@ -1566,17 +1514,6 @@ class DefaultParameterElementImpl extends ParameterElementImpl
DefaultParameterElementImpl.forNode(Identifier name) : super.forNode(name);
@override
- DartObject get constantValue => _result?.value;
-
- @override
- EvaluationResultImpl get evaluationResult => _result;
-
- @override
- void set evaluationResult(EvaluationResultImpl result) {
- this._result = result;
- }
-
- @override
DefaultFormalParameter computeNode() =>
getNodeMatching((node) => node is DefaultFormalParameter);
}
@@ -1772,6 +1709,14 @@ class ElementAnnotationImpl implements ElementAnnotation {
Source get source => compilationUnit.source;
@override
+ DartObject computeConstantValue() {
+ if (evaluationResult == null) {
+ context?.computeResult(this, CONSTANT_VALUE);
+ }
+ return constantValue;
+ }
+
+ @override
String toString() => '@$element';
}
@@ -4948,7 +4893,7 @@ abstract class VariableElementImpl extends ElementImpl
Expression get constantInitializer => null;
@override
- DartObject get constantValue => null;
+ DartObject get constantValue => evaluationResult?.value;
/**
* Return the result of evaluating this variable's initializer as a
@@ -5021,6 +4966,9 @@ abstract class VariableElementImpl extends ElementImpl
}
@override
+ DartObject computeConstantValue() => null;
+
+ @override
void visitChildren(ElementVisitor visitor) {
super.visitChildren(visitor);
_initializer?.accept(visitor);
« no previous file with comments | « pkg/analyzer/lib/dart/element/element.dart ('k') | pkg/analyzer/lib/src/dart/element/handle.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698