Skip to content

Commit df96f15

Browse files
scheglovCommit Queue
authored andcommitted
Elements. APIs for constant initializers.
I found that we have ConstTopLevelVariableElementImpl, ConstFieldElementImpl, DefaultSuperFormalParameterElementImpl, etc. But we don't have corresponding Element2. And this is probably good. Having so many classes might be too much. Why not turn the constant initializer into a property? Change-Id: I1fe7c2709e2bee4d8868e2916a3b3e8f7a6fb1f6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/406880 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent b79729c commit df96f15

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

pkg/analyzer/lib/dart/element/element2.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ library;
5050

5151
import 'package:analyzer/dart/analysis/features.dart';
5252
import 'package:analyzer/dart/analysis/session.dart';
53+
import 'package:analyzer/dart/ast/ast.dart';
5354
import 'package:analyzer/dart/constant/value.dart';
5455
import 'package:analyzer/dart/element/element.dart'
5556
show
@@ -253,6 +254,22 @@ abstract class ClassFragment implements InterfaceFragment {
253254
ClassFragment? get previousFragment;
254255
}
255256

257+
/// The initializer of a constant variable, or the default value for a formal
258+
/// parameter.
259+
abstract class ConstantInitializer {
260+
/// The expression of the initializer.
261+
///
262+
/// For variables that have multiple fragments, this is the expression from
263+
/// the last fragment. For formal parameters, only the first fragment can
264+
/// have the default value.
265+
Expression get expression;
266+
267+
/// Returns the result of evaluating [expression], computes it if necessary.
268+
///
269+
/// Returns `null` if the value could not be computed because of errors.
270+
DartObject? evaluate();
271+
}
272+
256273
/// An element representing a constructor defined by a class, enum, or extension
257274
/// type.
258275
///
@@ -2806,6 +2823,13 @@ abstract class TypeParameterizedFragment implements Fragment, Annotatable {
28062823
///
28072824
/// Clients may not extend, implement or mix-in this class.
28082825
abstract class VariableElement2 implements Element2 {
2826+
/// The constant initializer for this constant variable, or the default
2827+
/// value for this formal parameter.
2828+
///
2829+
/// Is `null` if this variable is not a constant, or does not have the
2830+
/// initializer or the default value specified.
2831+
ConstantInitializer? get constantInitializer2;
2832+
28092833
@override
28102834
VariableFragment get firstFragment;
28112835

@@ -2857,6 +2881,13 @@ abstract class VariableElement2 implements Element2 {
28572881
///
28582882
/// Clients may not extend, implement or mix-in this class.
28592883
abstract class VariableFragment implements Fragment {
2884+
/// The constant initializer for this constant variable fragment, or the
2885+
/// default value for this formal parameter fragment.
2886+
///
2887+
/// Is `null` if this variable fragment is not a constant, or does not have
2888+
/// the initializer or the default value specified.
2889+
ConstantInitializer? get constantInitializer2;
2890+
28602891
@override
28612892
VariableElement2 get element;
28622893

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11671,6 +11671,12 @@ abstract class VariableElementImpl extends ElementImpl
1167111671
/// initializers.
1167211672
Expression? get constantInitializer => null;
1167311673

11674+
@override
11675+
ConstantInitializer? get constantInitializer2 {
11676+
// TODO(scheglov): implement it
11677+
throw UnimplementedError();
11678+
}
11679+
1167411680
@override
1167511681
VariableElement get declaration => this;
1167611682

@@ -11768,6 +11774,12 @@ abstract class VariableElementImpl extends ElementImpl
1176811774

1176911775
abstract class VariableElementImpl2 extends ElementImpl2
1177011776
implements VariableElement2OrMember {
11777+
@override
11778+
ConstantInitializer? get constantInitializer2 {
11779+
// TODO(scheglov): implement it
11780+
throw UnimplementedError();
11781+
}
11782+
1177111783
@override
1177211784
void visitChildren2<T>(ElementVisitor2<T> visitor) {
1177311785
for (var child in children2) {

pkg/analyzer/lib/src/dart/element/member.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,12 @@ class FieldMember extends VariableMember
561561
List<Element2> get children2 =>
562562
children.map((fragment) => fragment.asElement2).nonNulls.toList();
563563

564+
@override
565+
ConstantInitializer? get constantInitializer2 {
566+
// TODO(scheglov): implement it
567+
throw UnimplementedError();
568+
}
569+
564570
@override
565571
FieldElement get declaration => super.declaration as FieldElement;
566572

@@ -1211,6 +1217,12 @@ class ParameterMember extends VariableMember
12111217
List<Element2> get children2 =>
12121218
children.map((fragment) => fragment.asElement2).nonNulls.toList();
12131219

1220+
@override
1221+
ConstantInitializer? get constantInitializer2 {
1222+
// TODO(scheglov): implement it
1223+
throw UnimplementedError();
1224+
}
1225+
12141226
@override
12151227
ParameterElementImpl get declaration =>
12161228
super.declaration as ParameterElementImpl;

0 commit comments

Comments
 (0)