Skip to content

Commit 70a9e60

Browse files
johnniwintherCommit Queue
authored andcommitted
[cfe] Avoid lowering for LocalIncDec
This changes the encoding of LocalPostIncDec (now called LocalIncDec) to avoid lowering during body building. To achieve this, the inference for VariableGet and VariableSet have been refactoring to allow for reuse through LocalIncDec. Similar refactorings have been done for StaticGet/Set/IncDec and SuperPropertyGet/PropertySet/IncDec. TEST=existing Change-Id: I84522c510520cadf8ade95345d724ffbf2816ff5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/447180 Reviewed-by: Tess Strickland <[email protected]> Reviewed-by: Chloe Stefantsova <[email protected]> Commit-Queue: Johnni Winther <[email protected]>
1 parent 8193b3d commit 70a9e60

File tree

57 files changed

+663
-367
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+663
-367
lines changed

pkg/front_end/lib/src/kernel/body_builder.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3220,14 +3220,19 @@ class BodyBuilder extends StackListenerImpl
32203220
]));
32213221
}
32223222

3223-
/// Helper method to create a [VariableGet] of the [variable] using
3224-
/// [charOffset] as the file offset.
32253223
@override
3226-
VariableGet createVariableGet(VariableDeclaration variable, int charOffset) {
3224+
void registerVariableRead(VariableDeclaration variable) {
32273225
if (!(variable as VariableDeclarationImpl).isLocalFunction &&
32283226
!variable.isWildcard) {
32293227
typeInferrer.assignedVariables.read(variable);
32303228
}
3229+
}
3230+
3231+
/// Helper method to create a [VariableGet] of the [variable] using
3232+
/// [charOffset] as the file offset.
3233+
@override
3234+
VariableGet createVariableGet(VariableDeclaration variable, int charOffset) {
3235+
registerVariableRead(variable);
32313236
return new VariableGet(variable)..fileOffset = charOffset;
32323237
}
32333238

pkg/front_end/lib/src/kernel/expression_generator.dart

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,9 @@ class VariableUseGenerator extends Generator {
318318
final VariableDeclaration variable;
319319

320320
VariableUseGenerator(
321-
ExpressionGeneratorHelper helper, Token token, this.variable)
321+
ExpressionGeneratorHelper helper, Token nameToken, this.variable)
322322
: assert(variable.isAssignable, 'Variable $variable is not assignable'),
323-
super(helper, token);
323+
super(helper, nameToken);
324324

325325
@override
326326
// Coverage-ignore(suite): Not run.
@@ -329,6 +329,8 @@ class VariableUseGenerator extends Generator {
329329
@override
330330
String get _plainNameForRead => variable.name!;
331331

332+
int get _nameOffset => fileOffset;
333+
332334
@override
333335
Expression buildSimpleRead() {
334336
return _createRead();
@@ -343,11 +345,15 @@ class VariableUseGenerator extends Generator {
343345
return _createWrite(fileOffset, value);
344346
}
345347

346-
Expression _createWrite(int offset, Expression value) {
348+
void _checkAssignment(int offset) {
347349
if (_helper.isDeclaredInEnclosingCase(variable)) {
348350
_helper.addProblem(
349351
codePatternVariableAssignmentInsideGuard, offset, noLength);
350352
}
353+
}
354+
355+
Expression _createWrite(int offset, Expression value) {
356+
_checkAssignment(offset);
351357
_helper.registerVariableAssignment(variable);
352358
return new VariableSet(variable, value)..fileOffset = offset;
353359
}
@@ -372,9 +378,36 @@ class VariableUseGenerator extends Generator {
372378
return _createWrite(fileOffset, binary);
373379
}
374380

381+
Expression _buildPrePostfixIncrement(Name binaryOperator,
382+
{required int operatorOffset,
383+
required bool forEffect,
384+
required bool isPost}) {
385+
_checkAssignment(_nameOffset);
386+
_helper.registerVariableRead(variable);
387+
_helper.registerVariableAssignment(variable);
388+
return new LocalIncDec(
389+
variable: variable as VariableDeclarationImpl,
390+
forEffect: forEffect,
391+
isPost: isPost,
392+
isInc: binaryOperator == plusName,
393+
nameOffset: _nameOffset,
394+
operatorOffset: operatorOffset)
395+
..fileOffset = _nameOffset;
396+
}
397+
398+
@override
399+
Expression buildPrefixIncrement(Name binaryOperator,
400+
{required int operatorOffset, bool voidContext = false}) {
401+
return _buildPrePostfixIncrement(binaryOperator,
402+
operatorOffset: operatorOffset, forEffect: voidContext, isPost: false);
403+
}
404+
375405
@override
376406
Expression buildPostfixIncrement(Name binaryOperator,
377407
{required int operatorOffset, bool voidContext = false}) {
408+
return _buildPrePostfixIncrement(binaryOperator,
409+
operatorOffset: operatorOffset, forEffect: voidContext, isPost: true);
410+
/*
378411
Expression value = _forest.createIntLiteral(operatorOffset, 1);
379412
if (voidContext) {
380413
return buildCompoundAssignment(binaryOperator, value,
@@ -388,7 +421,7 @@ class VariableUseGenerator extends Generator {
388421
_helper.createVariableGet(read, fileOffset), binaryOperator, value);
389422
VariableDeclarationImpl write = _helper.createVariableDeclarationForValue(
390423
_createWrite(operatorOffset, binary));
391-
return new LocalPostIncDec(read, write)..fileOffset = operatorOffset;
424+
return new LocalIncDec(read, write)..fileOffset = operatorOffset;*/
392425
}
393426

394427
@override

pkg/front_end/lib/src/kernel/expression_generator_helper.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ abstract class ExpressionGeneratorHelper implements InferenceHelper {
169169
/// offset of the created node.
170170
Expression createVariableGet(VariableDeclaration variable, int charOffset);
171171

172+
/// Registers that [variable] is read from.
173+
///
174+
/// This is needed for type promotion.
175+
void registerVariableRead(VariableDeclaration variable);
176+
172177
/// Registers that [variable] is assigned to.
173178
///
174179
/// This is needed for type promotion.

pkg/front_end/lib/src/kernel/internal_ast.dart

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,28 +1907,63 @@ class ExtensionIncDec extends InternalExpression {
19071907
///
19081908
/// let v1 = a in let v2 = a = v1 + 1 in v1
19091909
///
1910-
class LocalPostIncDec extends InternalExpression {
1911-
/// The expression that reads the local variable.
1912-
VariableDeclarationImpl read;
1910+
class LocalIncDec extends InternalExpression {
1911+
/// The accessed variable.
1912+
final VariableDeclarationImpl variable;
19131913

1914-
/// The expression that writes the result of the binary operation to the
1915-
/// local variable.
1916-
VariableDeclarationImpl write;
1914+
/// `true` if the inc/dec is a postfix expression, i.e. of the form `a++` as
1915+
/// opposed the prefix expression `++a`.
1916+
final bool isPost;
19171917

1918-
LocalPostIncDec(this.read, this.write) {
1919-
read.parent = this;
1920-
write.parent = this;
1921-
}
1918+
/// If `true` the assignment is need for its effect and not for its value.
1919+
final bool forEffect;
1920+
1921+
/// `true` if this is an post increment, i.e. `a++` as opposed to `a--`.
1922+
final bool isInc;
1923+
1924+
/// The file offset of the name of the getter/setter, i.e. `a` in `a++`.
1925+
final int nameOffset;
1926+
1927+
/// The file offset of the `++` or `--` operator.
1928+
final int operatorOffset;
1929+
1930+
LocalIncDec(
1931+
{required this.variable,
1932+
required this.forEffect,
1933+
required this.isPost,
1934+
required this.isInc,
1935+
required this.nameOffset,
1936+
required this.operatorOffset});
19221937

19231938
@override
19241939
ExpressionInferenceResult acceptInference(
19251940
InferenceVisitorImpl visitor, DartType typeContext) {
1926-
return visitor.visitLocalPostIncDec(this, typeContext);
1941+
return visitor.visitLocalIncDec(this, typeContext);
1942+
}
1943+
1944+
@override
1945+
// Coverage-ignore(suite): Not run.
1946+
void toTextInternal(AstPrinter printer) {
1947+
if (!isPost) {
1948+
if (isInc) {
1949+
printer.write('++');
1950+
} else {
1951+
printer.write('--');
1952+
}
1953+
}
1954+
printer.write(variable.name!);
1955+
if (isPost) {
1956+
if (isInc) {
1957+
printer.write('++');
1958+
} else {
1959+
printer.write('--');
1960+
}
1961+
}
19271962
}
19281963

19291964
@override
19301965
String toString() {
1931-
return "LocalPostIncDec(${toStringInternal()})";
1966+
return "LocalIncDec(${toStringInternal()})";
19321967
}
19331968
}
19341969

0 commit comments

Comments
 (0)