[cfe] Use AST node getters for the new variable model

This CL migrates CFE to use the getters of the Kernel AST nodes with
return types of the new variable model.

Part of https://github.com/dart-lang/sdk/issues/61572

Change-Id: I0984d076b9b9591c62fb91238a4df08c4eb9b414
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/486200
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Chloe Stefantsova <cstefantsova@google.com>
This commit is contained in:
Chloe Stefantsova
2026-03-09 05:19:46 -07:00
committed by Commit Queue
parent 988cd0f9b7
commit 960b2842e8
12 changed files with 48 additions and 44 deletions
@@ -2561,10 +2561,10 @@ class ExpressionEvaluationHelperImpl implements ExpressionEvaluationHelper {
CompilerContext compilerContext,
Uri fileUri,
) {
if (knownButUnavailable.contains(node.variable)) {
if (knownButUnavailable.contains(node.expressionVariable)) {
return _returnKnownVariableUnavailable(
node,
node.variable,
node.expressionVariable,
problemReporting,
compilerContext,
fileUri,
@@ -2581,10 +2581,10 @@ class ExpressionEvaluationHelperImpl implements ExpressionEvaluationHelper {
CompilerContext compilerContext,
Uri fileUri,
) {
if (knownButUnavailable.contains(node.variable)) {
if (knownButUnavailable.contains(node.expressionVariable)) {
return _returnKnownVariableUnavailable(
node,
node.variable,
node.expressionVariable,
problemReporting,
compilerContext,
fileUri,
@@ -184,7 +184,8 @@ class _ConstantEvaluator extends TryConstantEvaluator {
@override
Constant visitVariableGet(VariableGet node) =>
_lookupVariableGet(node.variable) ?? super.visitVariableGet(node);
_lookupVariableGet(node.expressionVariable) ??
super.visitVariableGet(node);
// Coverage-ignore(suite): Not run.
Constant? _evaluateStaticFieldGet(Field field) {
@@ -583,11 +583,12 @@ class ConstantsTransformer extends RemovingTransformer {
if (expression is StaticGet && expression.target.isConst) {
// Handle [StaticGet] of constant fields also when these are not inlined.
expression = (expression.target as Field).initializer!;
} else if (expression is VariableGet && expression.variable.isConst) {
} else if (expression is VariableGet &&
expression.expressionVariable.isConst) {
// Coverage-ignore-block(suite): Not run.
// Handle [VariableGet] of constant locals also when these are not
// inlined.
expression = expression.variable.initializer!;
expression = expression.expressionVariable.initializer!;
}
if (expression is ConstantExpression) {
if (result.typeArguments.every(isInstantiated)) {
@@ -4780,7 +4781,7 @@ class ConstantEvaluator
//
// TODO(kustermann): The heuristic of allowing all [VariableGet]s on [Let]
// variables might allow more than it should.
final ExpressionVariable variable = node.variable;
final ExpressionVariable variable = node.expressionVariable;
if (enableConstFunctions || inExtensionTypeConstConstructor) {
return env.lookupVariable(variable) ??
// Coverage-ignore(suite): Not run.
@@ -4794,7 +4795,7 @@ class ConstantEvaluator
if (variable.parent is Let ||
variable.parent is LocalInitializer ||
_isFormalParameter(variable)) {
return env.lookupVariable(node.variable) ??
return env.lookupVariable(node.expressionVariable) ??
createEvaluationErrorConstant(
node,
diag.constEvalNonConstantVariableGet.withArguments(
@@ -4817,7 +4818,7 @@ class ConstantEvaluator
@override
Constant visitVariableSet(VariableSet node) {
if (enableConstFunctions || inExtensionTypeConstConstructor) {
final ExpressionVariable variable = node.variable;
final ExpressionVariable variable = node.expressionVariable;
Constant value = _evaluateSubexpression(node.value);
if (value is AbortConstant) return value;
Constant? result = env.updateVariableValue(variable, value);
@@ -6216,11 +6217,11 @@ class StatementConstantEvaluator
) ||
catchClause.guard == defaultType) {
return exprEvaluator.withNewEnvironment(() {
if (catchClause.exception != null) {
if (catchClause.exceptionCatchVariable != null) {
// TODO(kallentu): Store non-constant exceptions.
if (throwValue is Constant) {
exprEvaluator.env.addVariableValue(
catchClause.exception!,
catchClause.exceptionCatchVariable!,
throwValue,
);
}
@@ -6411,15 +6412,15 @@ class EvaluationEnvironment {
<TypeParameter, DartType>{};
/// The references to values of the parameters/variables in scope.
final Map<VariableDeclaration, EvaluationReference> _variables =
<VariableDeclaration, EvaluationReference>{};
final Map<ExpressionVariable, EvaluationReference> _variables =
<ExpressionVariable, EvaluationReference>{};
/// The variables that hold unevaluated constants.
///
/// Variables are removed from this set when looked up, leaving only the
/// unread variables at the end.
final Set<VariableDeclaration> _unreadUnevaluatedVariables =
new Set<VariableDeclaration>();
final Set<ExpressionVariable> _unreadUnevaluatedVariables =
new Set<ExpressionVariable>();
final EvaluationEnvironment? _parent;
@@ -6440,7 +6441,7 @@ class EvaluationEnvironment {
_typeParameters[parameter] = value;
}
void addVariableValue(VariableDeclaration variable, Constant value) {
void addVariableValue(ExpressionVariable variable, Constant value) {
_variables[variable] = new EvaluationReference(value);
if (value is UnevaluatedConstant) {
_unreadUnevaluatedVariables.add(variable);
@@ -6471,7 +6472,7 @@ class EvaluationEnvironment {
if (_unreadUnevaluatedVariables.isEmpty) return const [];
// Coverage-ignore(suite): Not run.
return _unreadUnevaluatedVariables.map<UnevaluatedConstant>(
(VariableDeclaration variable) =>
(ExpressionVariable variable) =>
_variables[variable]!.value as UnevaluatedConstant,
);
}
@@ -1512,7 +1512,7 @@ mixin DelegatingVariableMixin on InternalExpressionVariableMixin
@override
void set type(DartType value) {
astVariable.type = type;
astVariable.type = value;
}
@override
@@ -4773,11 +4773,11 @@ Expression clonePureExpression(Expression node) {
return new ThisExpression()..fileOffset = node.fileOffset;
} else if (node is VariableGet) {
assert(
node.variable.isFinal && !node.variable.isLate,
node.expressionVariable.isFinal && !node.variable.isLate,
"Trying to clone VariableGet of non-final variable"
" ${node.variable}.",
" ${node.expressionVariable}.",
);
return new VariableGet(node.variable, node.promotedType)
return new VariableGet(node.expressionVariable, node.promotedType)
..fileOffset = node.fileOffset;
}
// Coverage-ignore-block(suite): Not run.
@@ -284,7 +284,8 @@ abstract class DataExtractor<T> extends VisitorDefault<void>
@override
void visitEqualsNull(EqualsNull node) {
Expression receiver = node.expression;
if (receiver is VariableGet && receiver.variable.name == null) {
if (receiver is VariableGet &&
receiver.expressionVariable.cosmeticName == null) {
// This is a desugared `?.`.
} else {
_visitInvocation(node, Name.equalsName);
@@ -372,7 +373,8 @@ abstract class DataExtractor<T> extends VisitorDefault<void>
@override
void visitVariableGet(VariableGet node) {
if (node.variable.name != null && !node.variable.isInitializingFormal) {
if (node.expressionVariable.cosmeticName != null &&
!node.expressionVariable.isInitializingFormal) {
// Skip use of synthetic variables.
computeForNode(
node,
@@ -400,7 +402,7 @@ abstract class DataExtractor<T> extends VisitorDefault<void>
@override
void visitVariableSet(VariableSet node) {
if (node.variable.name != null) {
if (node.expressionVariable.cosmeticName != null) {
// Skip use of synthetic variables.
computeForNode(node, createUpdateId(node));
}
@@ -93,7 +93,7 @@ class PatternVariableDeclarationForInVariable implements ForInVariable {
// Coverage-ignore(suite): Not run.
DartType computeElementType(InferenceVisitorBase visitor) {
return (patternVariableDeclaration.initializer as VariableGet)
.variable
.expressionVariable
.type;
}
@@ -1399,7 +1399,7 @@ class InferenceVisitorImpl extends InferenceVisitorBase
PropertyTarget<Expression> computePropertyTarget(Expression target) {
if (_enclosingCascade case Cascade(
:var variable,
) when target is VariableGet && target.variable == variable) {
) when target is VariableGet && target.expressionVariable == variable) {
// `target` is an implicit reference to the target of a cascade
// expression; flow analysis uses `CascadePropertyTarget` to represent
// this situation.
@@ -252,13 +252,13 @@ abstract class InferenceVisitorBase implements InferenceVisitor {
}
TreeNode origNode = node;
while (origNode is VariableGet &&
origNode.variable.name == null &&
origNode.variable.initializer != null) {
origNode.expressionVariable.cosmeticName == null &&
origNode.expressionVariable.initializer != null) {
// This is a read of a synthetic variable, presumably from a "let".
// Find the original expression.
// TODO(johnniwinther): add a general solution for getting the
// original node for testing.
origNode = origNode.variable.initializer!;
origNode = origNode.expressionVariable.initializer!;
}
dataForTesting!.flowAnalysisResult.nonPromotionReasons[origNode] =
nonPromotionReasonText;
@@ -2879,7 +2879,7 @@ abstract class InferenceVisitorBase implements InferenceVisitor {
functionType: null,
)..fileOffset = fileOffset;
} else if (receiver is VariableGet) {
ExpressionVariable variable = receiver.variable;
ExpressionVariable variable = receiver.expressionVariable;
TreeNode? parent = variable.parent;
if (parent is FunctionDeclaration) {
assert(
@@ -4428,11 +4428,11 @@ abstract class InferenceVisitorBase implements InferenceVisitor {
compilerContext: compilerContext,
expression: resultExpression,
message: diag.lateDefinitelyAssignedError.withArguments(
variableName: node.variable.name!,
variableName: node.expressionVariable.cosmeticName!,
),
fileUri: fileUri,
fileOffset: node.fileOffset,
length: node.variable.name!.length,
length: node.expressionVariable.cosmeticName!.length,
),
);
}
@@ -4444,11 +4444,11 @@ abstract class InferenceVisitorBase implements InferenceVisitor {
compilerContext: compilerContext,
expression: resultExpression,
message: diag.finalPossiblyAssignedError.withArguments(
variableName: node.variable.name!,
variableName: node.expressionVariable.cosmeticName!,
),
fileUri: fileUri,
fileOffset: node.fileOffset,
length: node.variable.name!.length,
length: node.expressionVariable.cosmeticName!.length,
),
);
}
@@ -4792,7 +4792,7 @@ abstract class InferenceVisitorBase implements InferenceVisitor {
Expression? initializer = first.initializer;
if (initializer is! StaticInvocation) return false;
if (initializer.target != engine.setFactory) return false;
return value.variable == first;
return value.expressionVariable == first;
}
/// Determines if the given [expression]'s type is precisely known at compile
@@ -55,7 +55,7 @@ class NullabilityDataExtractor extends CfeDataExtractor<String> {
@override
String? computeNodeValue(Id id, TreeNode node) {
if (node is VariableGet && node.promotedType != null) {
if (node.variable.type.nullability != Nullability.nonNullable &&
if (node.expressionVariable.type.nullability != Nullability.nonNullable &&
node.promotedType!.nullability == Nullability.nonNullable) {
return 'nonNullable';
}
@@ -70,7 +70,7 @@ static method testDirectCaptured() → dynamic {
catch-variable s;
]),
] */ {
return () /* #ctx4 */ → core::List<dynamic> => <dynamic>[e, s];
return () /* #ctx4 */ → core::List<core::Object> => <core::Object>[e, s];
}
}
static method testAssertCaptured() → dynamic {
@@ -70,7 +70,7 @@ static method testDirectCaptured() → dynamic {
catch-variable s;
]),
] */ {
return () /* #ctx4 */ → core::List<dynamic> => <dynamic>[e, s];
return () /* #ctx4 */ → core::List<core::Object> => <core::Object>[e, s];
}
}
static method testAssertCaptured() → dynamic {
@@ -84,7 +84,7 @@ class UnreachableIfFinder extends RecursiveVisitor {
}
if (condition is VariableGet) {
bool? knownValue = knownValues[condition.variable];
bool? knownValue = knownValues[condition.expressionVariable];
if (knownValue != null) {
if (conditionNegated) knownValue = !knownValue;
String? hint;
@@ -102,9 +102,9 @@ class UnreachableIfFinder extends RecursiveVisitor {
),
);
} else {
if (condition.variable.isFinal ||
unwritten.contains(condition.variable)) {
newKnownValueHere = condition.variable;
if (condition.expressionVariable.isFinal ||
unwritten.contains(condition.expressionVariable)) {
newKnownValueHere = condition.expressionVariable;
}
}
}
@@ -135,7 +135,7 @@ class EffectivelyFinal extends RecursiveVisitor {
@override
void visitVariableSet(VariableSet node) {
unwritten.remove(node.variable);
unwritten.remove(node.expressionVariable);
super.visitVariableSet(node);
}
}