[kernel][Contexts] Rename VariableDeclaration to Variable

This is a step towards split variables from their declarations as part of encoding variables and their usage more precisely.

TEST=existing

Change-Id: I4a0eeb2947bdebce3667afda4e6cfbfdf5d7de18
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/504201
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Nicholas Shahan <nshahan@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
SLSA-Policy-Verified: SLSA Policy Verification Service <devtools-gerritcodereview-exitgate@google.com>
This commit is contained in:
Johnni Winther
2026-05-21 01:51:10 -07:00
committed by dart-scoped@luci-project-accounts.iam.gserviceaccount.com
parent ff25d85758
commit 80c9d57250
198 changed files with 1947 additions and 2368 deletions
+23 -28
View File
@@ -482,10 +482,10 @@ class BytecodeGenerator extends RecursiveVisitor {
FunctionNode function,
) {
final parameterNodeLists = <List<Expression>>[];
for (VariableDeclaration variable in function.positionalParameters) {
for (Variable variable in function.positionalParameters) {
parameterNodeLists.add(variable.annotations);
}
for (VariableDeclaration variable in function.namedParameters) {
for (Variable variable in function.namedParameters) {
parameterNodeLists.add(variable.annotations);
}
@@ -829,7 +829,7 @@ class BytecodeGenerator extends RecursiveVisitor {
);
}
ParameterDeclaration getParameterDeclaration(VariableDeclaration variable) {
ParameterDeclaration getParameterDeclaration(Variable variable) {
final name = variable.name!;
final lib = name.startsWith('_') ? enclosingMember!.enclosingLibrary : null;
final nameHandle = objectTable.getNameHandle(lib, name);
@@ -1651,7 +1651,7 @@ class BytecodeGenerator extends RecursiveVisitor {
}
void _genPushContextForVariable(
VariableDeclaration variable, {
Variable variable, {
int? currentContextLevel,
}) {
currentContextLevel ??= locals.currentContextLevel;
@@ -1666,13 +1666,13 @@ class BytecodeGenerator extends RecursiveVisitor {
}
}
void _genPushContextIfCaptured(VariableDeclaration variable) {
void _genPushContextIfCaptured(Variable variable) {
if (locals.isCaptured(variable)) {
_genPushContextForVariable(variable);
}
}
void _genLoadVar(VariableDeclaration v, {int? currentContextLevel}) {
void _genLoadVar(Variable v, {int? currentContextLevel}) {
if (locals.isCaptured(v)) {
_genPushContextForVariable(v, currentContextLevel: currentContextLevel);
asm.emitLoadContextVar(
@@ -1692,7 +1692,7 @@ class BytecodeGenerator extends RecursiveVisitor {
// Stores value into variable.
// If variable is captured, context should be pushed before value.
void _genStoreVar(VariableDeclaration variable) {
void _genStoreVar(Variable variable) {
if (locals.isCaptured(variable)) {
asm.emitStoreContextVar(
locals.getVarContextId(variable),
@@ -1787,7 +1787,7 @@ class BytecodeGenerator extends RecursiveVisitor {
asm.currentSourcePosition = savedSourcePosition;
}
int _getDefaultParamConstIndex(VariableDeclaration param) {
int _getDefaultParamConstIndex(Variable param) {
final paramInitializer = param.initializer;
if (paramInitializer == null) {
return cp.addObjectRef(null);
@@ -2398,7 +2398,7 @@ class BytecodeGenerator extends RecursiveVisitor {
asm.emitSourcePosition();
}
void _copyParamIfCaptured(VariableDeclaration variable) {
void _copyParamIfCaptured(Variable variable) {
if (locals.isCaptured(variable)) {
if (options.emitLocalVarInfo) {
_declareLocalVariable(variable, enclosingFunction!.fileOffset);
@@ -2411,10 +2411,7 @@ class BytecodeGenerator extends RecursiveVisitor {
}
}
void _declareLocalVariable(
VariableDeclaration variable,
int initializedPosition,
) {
void _declareLocalVariable(Variable variable, int initializedPosition) {
bool isCaptured = locals.isCaptured(variable);
// Don't add initializing formals or wildcards.
if (variable.isInitializingFormal ||
@@ -2515,7 +2512,7 @@ class BytecodeGenerator extends RecursiveVisitor {
/// If member being compiled is a forwarding stub, then returns parameter
/// types to check for the forwarding stub target.
Map<VariableDeclaration, DartType>? _getForwardingParameterTypes(
Map<Variable, DartType>? _getForwardingParameterTypes(
FunctionNode function,
Member? forwardingTarget,
Substitution? forwardingSubstitution,
@@ -2526,7 +2523,7 @@ class BytecodeGenerator extends RecursiveVisitor {
if (forwardingTarget is Field) {
if ((enclosingMember as Procedure).isGetter) {
return const <VariableDeclaration, DartType>{};
return const <Variable, DartType>{};
} else {
// Forwarding stub for a covariant field setter.
assert((enclosingMember as Procedure).isSetter);
@@ -2535,14 +2532,14 @@ class BytecodeGenerator extends RecursiveVisitor {
function.positionalParameters.length == 1 &&
function.namedParameters.isEmpty,
);
return <VariableDeclaration, DartType>{
return <Variable, DartType>{
function.positionalParameters.single: forwardingSubstitution!
.substituteType(forwardingTarget.type),
};
}
}
final forwardingParams = <VariableDeclaration, DartType>{};
final forwardingParams = <Variable, DartType>{};
for (int i = 0; i < function.positionalParameters.length; ++i) {
DartType type = forwardingSubstitution!.substituteType(
forwardingTarget.function!.positionalParameters[i].type,
@@ -2550,9 +2547,7 @@ class BytecodeGenerator extends RecursiveVisitor {
forwardingParams[function.positionalParameters[i]] = type;
}
for (var hostParam in function.namedParameters) {
VariableDeclaration targetParam = forwardingTarget
.function!
.namedParameters
Variable targetParam = forwardingTarget.function!.namedParameters
.firstWhere((p) => p.name == hostParam.name);
forwardingParams[hostParam] = forwardingSubstitution!.substituteType(
targetParam.type,
@@ -2650,8 +2645,8 @@ class BytecodeGenerator extends RecursiveVisitor {
/// Returns true if type of [param] should be checked.
bool _parameterNeedsTypeCheck(
VariableDeclaration param,
Map<VariableDeclaration, DartType>? forwardingParameterTypes,
Variable param,
Map<Variable, DartType>? forwardingParameterTypes,
) {
if (canSkipTypeChecksForNonCovariantArguments &&
!param.isCovariantByDeclaration &&
@@ -2672,7 +2667,7 @@ class BytecodeGenerator extends RecursiveVisitor {
bool _hasSkippableTypeChecks(
FunctionNode function,
Map<TypeParameter, DartType>? forwardingBounds,
Map<VariableDeclaration, DartType>? forwardingParamTypes,
Map<Variable, DartType>? forwardingParamTypes,
) {
for (var typeParam in function.typeParameters) {
if (_typeParameterNeedsBoundCheck(typeParam, forwardingBounds)) {
@@ -2724,8 +2719,8 @@ class BytecodeGenerator extends RecursiveVisitor {
};
void _genArgumentTypeCheck(
VariableDeclaration variable,
Map<VariableDeclaration, DartType>? forwardingParameterTypes,
Variable variable,
Map<Variable, DartType>? forwardingParameterTypes,
) {
final DartType type = (forwardingParameterTypes != null)
? forwardingParameterTypes[variable]!
@@ -4870,7 +4865,7 @@ class BytecodeGenerator extends RecursiveVisitor {
finallyBlocks.remove(node);
}
bool _skipVariableInitialization(VariableDeclaration v, bool isCaptured) {
bool _skipVariableInitialization(Variable v, bool isCaptured) {
// We can skip variable initialization if the variable is supposed to be
// initialized to null and it's captured. This is because all the slots in
// the capture context are implicitly initialized to null.
@@ -4888,7 +4883,7 @@ class BytecodeGenerator extends RecursiveVisitor {
}
@override
void defaultVariableDeclaration(VariableDeclaration node) {
void defaultVariable(Variable node) {
_handleVariableInitialization(node);
}
@@ -4902,7 +4897,7 @@ class BytecodeGenerator extends RecursiveVisitor {
_handleVariableInitialization(node.variable);
}
void _handleVariableInitialization(VariableDeclaration node) {
void _handleVariableInitialization(Variable node) {
if (!node.isConst) {
final bool isCaptured = locals.isCaptured(node.variable);
final initializer = node.initializer;
+57 -73
View File
@@ -12,12 +12,12 @@ import 'options.dart' show BytecodeOptions;
class LocalVariables {
final _scopes = new Map<TreeNode, Scope>();
final _vars = new Map<VariableDeclaration, VarDesc>();
final _vars = new Map<Variable, VarDesc>();
Map<TreeNode, List<int>>? _temps;
Map<TreeNode, VariableDeclaration>? _capturedSavedContextVars;
Map<TreeNode, VariableDeclaration>? _capturedExceptionVars;
Map<TreeNode, VariableDeclaration>? _capturedStackTraceVars;
Map<ForInStatement, VariableDeclaration>? _capturedIteratorVars;
Map<TreeNode, Variable>? _capturedSavedContextVars;
Map<TreeNode, Variable>? _capturedExceptionVars;
Map<TreeNode, Variable>? _capturedStackTraceVars;
Map<ForInStatement, Variable>? _capturedIteratorVars;
final BytecodeOptions options;
final StaticTypeContext staticTypeContext;
@@ -27,11 +27,11 @@ class LocalVariables {
Frame? _currentFrameInternal;
Frame get _currentFrame => _currentFrameInternal!;
VarDesc _getVarDesc(VariableDeclaration variable) =>
VarDesc _getVarDesc(Variable variable) =>
_vars[variable] ??
(throw 'Variable descriptor is not created for $variable');
int _getVarIndex(VariableDeclaration variable, bool isCaptured) {
int _getVarIndex(Variable variable, bool isCaptured) {
final v = _getVarDesc(variable);
if (v.isCaptured != isCaptured) {
throw 'Mismatch in captured state of $variable';
@@ -39,20 +39,17 @@ class LocalVariables {
return v.index ?? (throw 'Variable $variable is not allocated');
}
bool isCaptured(VariableDeclaration variable) =>
_getVarDesc(variable).isCaptured;
bool isCaptured(Variable variable) => _getVarDesc(variable).isCaptured;
int getVarIndexInFrame(VariableDeclaration variable) =>
_getVarIndex(variable, false);
int getVarIndexInFrame(Variable variable) => _getVarIndex(variable, false);
int getVarIndexInContext(VariableDeclaration variable) =>
_getVarIndex(variable, true);
int getVarIndexInContext(Variable variable) => _getVarIndex(variable, true);
int getOriginalParamSlotIndex(VariableDeclaration variable) =>
int getOriginalParamSlotIndex(Variable variable) =>
_getVarDesc(variable).originalParamSlotIndex ??
(throw 'Variable $variable does not have originalParamSlotIndex');
int getParamIndexInFrame(VariableDeclaration variable) => isCaptured(variable)
int getParamIndexInFrame(Variable variable) => isCaptured(variable)
? getOriginalParamSlotIndex(variable)
: getVarIndexInFrame(variable);
@@ -72,13 +69,13 @@ class LocalVariables {
_currentFrame.contextLevelAtEntry ??
(throw "Current frame is top level and it doesn't have a context at entry");
int getContextLevelOfVar(VariableDeclaration variable) {
int getContextLevelOfVar(Variable variable) {
final v = _getVarDesc(variable);
assert(v.isCaptured);
return v.scope.contextLevel!;
}
int getVarContextId(VariableDeclaration variable) {
int getVarContextId(Variable variable) {
final v = _getVarDesc(variable);
assert(v.isCaptured);
return v.scope.contextId!;
@@ -111,7 +108,7 @@ class LocalVariables {
(throw 'Suspend state variable is not declared in ${_currentFrame.function}'),
);
VariableDeclaration get functionTypeArgsVar =>
Variable get functionTypeArgsVar =>
_currentFrame.functionTypeArgsVar ??
(throw 'FunctionTypeArgs variable is not declared in ${_currentFrame.function}');
@@ -120,25 +117,25 @@ class LocalVariables {
bool get hasFunctionTypeArgsVar => _currentFrame.functionTypeArgsVar != null;
VariableDeclaration get receiverVar =>
Variable get receiverVar =>
_currentFrame.receiverVar ??
(throw 'Receiver variable is not declared in ${_currentFrame.function}');
bool get hasCapturedReceiverVar => _currentFrame.capturedReceiverVar != null;
VariableDeclaration get capturedReceiverVar =>
Variable get capturedReceiverVar =>
_currentFrame.capturedReceiverVar ??
(throw 'Captured receiver variable is not declared in ${_currentFrame.function}');
bool get hasReceiver => _currentFrame.receiverVar != null;
VariableDeclaration? capturedSavedContextVar(TreeNode node) =>
Variable? capturedSavedContextVar(TreeNode node) =>
_capturedSavedContextVars?[node];
VariableDeclaration? capturedExceptionVar(TreeNode node) =>
Variable? capturedExceptionVar(TreeNode node) =>
_capturedExceptionVars?[node];
VariableDeclaration? capturedStackTraceVar(TreeNode node) =>
Variable? capturedStackTraceVar(TreeNode node) =>
_capturedStackTraceVars?[node];
VariableDeclaration? capturedIteratorVar(ForInStatement node) =>
Variable? capturedIteratorVar(ForInStatement node) =>
_capturedIteratorVars?[node];
int get frameSize => _currentFrame.frameSize;
@@ -152,9 +149,9 @@ class LocalVariables {
bool get isSuspendableFunction => _currentFrame.isSuspendableFunction;
bool get makesCopyOfParameters => _currentFrame.makesCopyOfParameters;
List<VariableDeclaration> get originalNamedParameters =>
List<Variable> get originalNamedParameters =>
_currentFrame.originalNamedParameters;
List<VariableDeclaration> get sortedNamedParameters =>
List<Variable> get sortedNamedParameters =>
_currentFrame.sortedNamedParameters;
LocalVariables(Member node, this.options, this.staticTypeContext) {
@@ -192,7 +189,7 @@ class LocalVariables {
}
class VarDesc {
final VariableDeclaration declaration;
final Variable declaration;
Scope scope;
bool isCaptured = false;
int? index;
@@ -227,22 +224,22 @@ class Frame {
final Frame? parent;
late Scope topScope;
late List<VariableDeclaration> originalNamedParameters;
late List<VariableDeclaration> sortedNamedParameters;
late List<Variable> originalNamedParameters;
late List<Variable> sortedNamedParameters;
int numParameters = 0;
int numTypeArguments = 0;
bool hasOptionalParameters = false;
bool hasCapturedParameters = false;
bool hasClosures = false;
VariableDeclaration? receiverVar;
VariableDeclaration? capturedReceiverVar;
VariableDeclaration? functionTypeArgsVar;
VariableDeclaration? closureVar;
VariableDeclaration? contextVar;
VariableDeclaration? scratchVar;
VariableDeclaration? returnVar;
VariableDeclaration? suspendStateVar;
Map<String, VariableDeclaration>? syntheticVars;
Variable? receiverVar;
Variable? capturedReceiverVar;
Variable? functionTypeArgsVar;
Variable? closureVar;
Variable? contextVar;
Variable? scratchVar;
Variable? returnVar;
Variable? suspendStateVar;
Map<String, Variable>? syntheticVars;
int frameSize = 0;
List<int> temporaries = <int>[];
int? contextLevelAtEntry;
@@ -250,7 +247,7 @@ class Frame {
Frame(this.function, this.parent);
VariableDeclaration getSyntheticVar(String name) {
Variable getSyntheticVar(String name) {
final syntheticVars = this.syntheticVars;
if (syntheticVars == null) {
throw 'No synthetic variables declared in ${function}!';
@@ -308,12 +305,9 @@ class _ScopeBuilder extends RecursiveVisitor {
_ScopeBuilder(this.locals);
List<VariableDeclaration> _sortNamedParameters(FunctionNode function) {
List<Variable> _sortNamedParameters(FunctionNode function) {
final params = function.namedParameters.toList();
params.sort(
(VariableDeclaration a, VariableDeclaration b) =>
a.name!.compareTo(b.name!),
);
params.sort((Variable a, Variable b) => a.name!.compareTo(b.name!));
return params;
}
@@ -330,7 +324,7 @@ class _ScopeBuilder extends RecursiveVisitor {
if (node is Field) {
if (_hasReceiverParameter(node)) {
final receiverVar = _currentFrame.receiverVar =
_findThisVariable(node) ?? VariableDeclaration('this');
_findThisVariable(node) ?? Variable('this');
_declareVariable(receiverVar);
}
node.initializer?.accept(this);
@@ -345,14 +339,13 @@ class _ScopeBuilder extends RecursiveVisitor {
FunctionNode function = (node as dynamic).function;
if (function.dartAsyncMarker != AsyncMarker.Sync) {
final suspendStateVar = _currentFrame.suspendStateVar =
VariableDeclaration(':suspend_state');
final suspendStateVar = _currentFrame.suspendStateVar = Variable(
':suspend_state',
);
_declareVariable(suspendStateVar);
if (function.dartAsyncMarker != AsyncMarker.SyncStar) {
final returnVar = _currentFrame.returnVar = VariableDeclaration(
':return',
);
final returnVar = _currentFrame.returnVar = Variable(':return');
_declareVariable(returnVar);
}
}
@@ -363,14 +356,14 @@ class _ScopeBuilder extends RecursiveVisitor {
if (_currentFrame.numTypeArguments > 0) {
final functionTypeArgsVar = _currentFrame.functionTypeArgsVar =
VariableDeclaration(':function_type_arguments_var')
Variable(':function_type_arguments_var')
..fileOffset = function.fileOffset;
_declareVariable(functionTypeArgsVar);
}
if (_hasReceiverParameter(node)) {
final receiverVar = _currentFrame.receiverVar =
_findThisVariable(node) ?? VariableDeclaration('this');
_findThisVariable(node) ?? Variable('this');
_declareVariable(receiverVar);
} else {
final parentReceiverVar = _currentFrame.parent?.receiverVar;
@@ -379,9 +372,7 @@ class _ScopeBuilder extends RecursiveVisitor {
}
}
if (node is FunctionDeclaration || node is FunctionExpression) {
final closureVar = _currentFrame.closureVar = VariableDeclaration(
':closure',
);
final closureVar = _currentFrame.closureVar = Variable(':closure');
_declareVariable(closureVar);
}
@@ -407,13 +398,9 @@ class _ScopeBuilder extends RecursiveVisitor {
if (node is FunctionDeclaration ||
node is FunctionExpression ||
_currentFrame.hasClosures) {
final contextVar = _currentFrame.contextVar = VariableDeclaration(
':context',
);
final contextVar = _currentFrame.contextVar = Variable(':context');
_declareVariable(contextVar);
final scratchVar = _currentFrame.scratchVar = VariableDeclaration(
':scratch',
);
final scratchVar = _currentFrame.scratchVar = Variable(':scratch');
_declareVariable(scratchVar);
}
@@ -421,8 +408,7 @@ class _ScopeBuilder extends RecursiveVisitor {
if (locals.isCaptured(_currentFrame.receiverVar!)) {
// Duplicate receiver variable for local use.
_currentFrame.capturedReceiverVar = _currentFrame.receiverVar;
final localReceiverVar = _currentFrame.receiverVar =
VariableDeclaration('this');
final localReceiverVar = _currentFrame.receiverVar = Variable('this');
_declareVariable(localReceiverVar);
}
}
@@ -459,7 +445,7 @@ class _ScopeBuilder extends RecursiveVisitor {
_currentScopeInternal = _currentScope.parent;
}
void _declareVariable(VariableDeclaration variable, [Scope? scope]) {
void _declareVariable(Variable variable, [Scope? scope]) {
if (scope == null) {
scope = _currentScope;
}
@@ -471,7 +457,7 @@ class _ScopeBuilder extends RecursiveVisitor {
locals._vars[variable] = v;
}
void _useVariable(VariableDeclaration variable) {
void _useVariable(Variable variable) {
final VarDesc? v = locals._vars[variable];
if (v == null) {
throw 'Variable $variable is used before declared';
@@ -527,7 +513,7 @@ class _ScopeBuilder extends RecursiveVisitor {
}
@override
void defaultVariableDeclaration(VariableDeclaration node) {
void defaultVariable(Variable node) {
_declareVariable(node.variable);
node.visitChildren(this);
}
@@ -669,9 +655,7 @@ class _ScopeBuilder extends RecursiveVisitor {
// an extra variable as they can be generated after all finally blocks.
if (_enclosingTryBlocks.isNotEmpty &&
(node.expression != null && node.expression is! BasicLiteral)) {
final returnVar = _currentFrame.returnVar = VariableDeclaration(
':return',
);
final returnVar = _currentFrame.returnVar = Variable(':return');
_declareVariable(returnVar, _currentFrame.topScope);
}
node.visitChildren(this);
@@ -839,7 +823,7 @@ class _Allocator extends RecursiveVisitor {
);
}
void _allocateVariable(VariableDeclaration variable, {int? paramSlotIndex}) {
void _allocateVariable(Variable variable, {int? paramSlotIndex}) {
final VarDesc v = locals._getVarDesc(variable);
assert(!v.isAllocated);
@@ -871,7 +855,7 @@ class _Allocator extends RecursiveVisitor {
_updateFrameSize();
}
void _ensureVariableAllocated(VariableDeclaration? variable) {
void _ensureVariableAllocated(Variable? variable) {
if (variable != null) {
final VarDesc v = locals._getVarDesc(variable);
if (!v.isAllocated) {
@@ -880,7 +864,7 @@ class _Allocator extends RecursiveVisitor {
}
}
void _allocateParameter(VariableDeclaration node, int i) {
void _allocateParameter(Variable node, int i) {
final numParameters = _currentFrame.numParameters;
assert(0 <= i && i < numParameters);
assert(
@@ -1033,7 +1017,7 @@ class _Allocator extends RecursiveVisitor {
}
@override
void defaultVariableDeclaration(VariableDeclaration node) {
void defaultVariable(Variable node) {
_allocateVariable(node.variable);
node.visitChildren(this);
}
+1 -4
View File
@@ -734,10 +734,7 @@ class ParameterFlags {
return null;
}
static int getVariableDeclarationFlags(
VariableDeclaration variable,
bool isCode,
) {
static int getVariableDeclarationFlags(Variable variable, bool isCode) {
int flags = 0;
if (isCode) {
if (variable.isCovariantByDeclaration) {