Skip to content

Commit d28828c

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Eliminate most of 'lazy' in _fe_analyzer_shared.
There are still 2 left in AbstractScanner. The usual trick with factory constructor does not work there. Change-Id: Iaa5d88468c8cb17c34a9f73ab93ef97251a1bd76 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/182881 Reviewed-by: Johnni Winther <johnniwinther@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent f28fd90 commit d28828c

File tree

9 files changed

+88
-115
lines changed

9 files changed

+88
-115
lines changed

pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3485,7 +3485,8 @@ class _FlowAnalysisImpl<Node extends Object, Statement extends Node,
34853485
final Map<Statement, _BranchTargetContext<Variable, Type>>
34863486
_statementToContext = {};
34873487

3488-
late FlowModel<Variable, Type> _current;
3488+
FlowModel<Variable, Type> _current =
3489+
new FlowModel<Variable, Type>(Reachability.initial);
34893490

34903491
/// The most recently visited expression for which an [ExpressionInfo] object
34913492
/// exists, or `null` if no expression has been visited that has a
@@ -3519,9 +3520,7 @@ class _FlowAnalysisImpl<Node extends Object, Statement extends Node,
35193520
{bool allowLocalBooleanVarsToPromote = false})
35203521
: allowLocalBooleanVarsToPromote =
35213522
allowLocalBooleanVarsToPromoteByDefault ||
3522-
allowLocalBooleanVarsToPromote {
3523-
_current = new FlowModel<Variable, Type>(Reachability.initial);
3524-
}
3523+
allowLocalBooleanVarsToPromote;
35253524

35263525
@override
35273526
bool get isReachable => _current.reachable.overallReachable;
@@ -4141,7 +4140,7 @@ class _FlowAnalysisImpl<Node extends Object, Statement extends Node,
41414140
Variable? exceptionVariable, Variable? stackTraceVariable) {
41424141
_TryContext<Variable, Type> context =
41434142
_stack.last as _TryContext<Variable, Type>;
4144-
_current = context._beforeCatch!;
4143+
_current = context._beforeCatch;
41454144
if (exceptionVariable != null) {
41464145
_current = _current.declare(exceptionVariable, true);
41474146
}
@@ -4162,7 +4161,7 @@ class _FlowAnalysisImpl<Node extends Object, Statement extends Node,
41624161
void tryCatchStatement_end() {
41634162
_TryContext<Variable, Type> context =
41644163
_stack.removeLast() as _TryContext<Variable, Type>;
4165-
_current = context._afterBodyAndCatches!.unsplit();
4164+
_current = context._afterBodyAndCatches.unsplit();
41664165
}
41674166

41684167
@override
@@ -4177,11 +4176,11 @@ class _FlowAnalysisImpl<Node extends Object, Statement extends Node,
41774176
_TryFinallyContext<Variable, Type> context =
41784177
_stack.removeLast() as _TryFinallyContext<Variable, Type>;
41794178
if (allowLocalBooleanVarsToPromote) {
4180-
_current = context._afterBodyAndCatches!
4179+
_current = context._afterBodyAndCatches
41814180
.attachFinally(typeOperations, context._beforeFinally, _current);
41824181
} else {
41834182
_current = _current.restrict(
4184-
typeOperations, context._afterBodyAndCatches!, info._written);
4183+
typeOperations, context._afterBodyAndCatches, info._written);
41854184
}
41864185
}
41874186

@@ -5122,14 +5121,14 @@ class _TryContext<Variable extends Object, Type extends Object>
51225121
extends _SimpleContext<Variable, Type> {
51235122
/// If the statement is a "try/catch" statement, the flow model representing
51245123
/// program state at the top of any `catch` block.
5125-
FlowModel<Variable, Type>? _beforeCatch;
5124+
late final FlowModel<Variable, Type> _beforeCatch;
51265125

51275126
/// If the statement is a "try/catch" statement, the accumulated flow model
51285127
/// representing program state after the `try` block or one of the `catch`
51295128
/// blocks has finished executing. If the statement is a "try/finally"
51305129
/// statement, the flow model representing program state after the `try` block
51315130
/// has finished executing.
5132-
FlowModel<Variable, Type>? _afterBodyAndCatches;
5131+
late FlowModel<Variable, Type> _afterBodyAndCatches;
51335132

51345133
_TryContext(FlowModel<Variable, Type> previous) : super(previous);
51355134

@@ -5143,7 +5142,7 @@ class _TryFinallyContext<Variable extends Object, Type extends Object>
51435142
extends _TryContext<Variable, Type> {
51445143
/// The flow model representing program state at the top of the `finally`
51455144
/// block.
5146-
late FlowModel<Variable, Type> _beforeFinally;
5145+
late final FlowModel<Variable, Type> _beforeFinally;
51475146

51485147
_TryFinallyContext(FlowModel<Variable, Type> previous) : super(previous);
51495148
}

pkg/_fe_analyzer_shared/lib/src/parser/literal_entry_info_impl.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const LiteralEntryInfo spreadOperator = const SpreadOperator();
1717

1818
/// The first step when processing a `for` control flow collection entry.
1919
class ForCondition extends LiteralEntryInfo {
20-
late bool inStyle;
20+
bool _inStyle = false;
2121

2222
ForCondition() : super(false, 0);
2323

@@ -39,12 +39,12 @@ class ForCondition extends LiteralEntryInfo {
3939

4040
if (optional('in', token.next!) || optional(':', token.next!)) {
4141
// Process `for ( ... in ... )`
42-
inStyle = true;
42+
_inStyle = true;
4343
token = parser.parseForInLoopPartsRest(
4444
token, awaitToken, forToken, identifier);
4545
} else {
4646
// Process `for ( ... ; ... ; ... )`
47-
inStyle = false;
47+
_inStyle = false;
4848
token = parser.parseForLoopPartsRest(token, forToken, awaitToken);
4949
}
5050
return token;
@@ -57,17 +57,17 @@ class ForCondition extends LiteralEntryInfo {
5757
(optional('await', next) && optional('for', next.next!))) {
5858
return new Nested(
5959
new ForCondition(),
60-
inStyle ? const ForInComplete() : const ForComplete(),
60+
_inStyle ? const ForInComplete() : const ForComplete(),
6161
);
6262
} else if (optional('if', next)) {
6363
return new Nested(
6464
ifCondition,
65-
inStyle ? const ForInComplete() : const ForComplete(),
65+
_inStyle ? const ForInComplete() : const ForComplete(),
6666
);
6767
} else if (optional('...', next) || optional('...?', next)) {
68-
return inStyle ? const ForInSpread() : const ForSpread();
68+
return _inStyle ? const ForInSpread() : const ForSpread();
6969
}
70-
return inStyle ? const ForInEntry() : const ForEntry();
70+
return _inStyle ? const ForInEntry() : const ForEntry();
7171
}
7272
}
7373

pkg/_fe_analyzer_shared/lib/src/parser/token_stream_rewriter.dart

Lines changed: 36 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import '../scanner/error_token.dart' show UnmatchedToken;
6-
76
import '../scanner/token.dart'
87
show
98
BeginToken,
@@ -227,28 +226,21 @@ abstract class TokenStreamChange {
227226
}
228227

229228
class NextTokenStreamChange implements TokenStreamChange {
230-
late Token setOn;
231-
Token? setOnNext;
232-
late Token nextToken;
233-
Token? nextTokenPrevious;
234-
Token? nextTokenBeforeSynthetic;
235-
236-
NextTokenStreamChange(UndoableTokenStreamRewriter rewriter) {
229+
final Token setOn;
230+
final Token? setOnNext;
231+
final Token nextToken;
232+
final Token? nextTokenPrevious;
233+
final Token? nextTokenBeforeSynthetic;
234+
235+
NextTokenStreamChange(
236+
UndoableTokenStreamRewriter rewriter, this.setOn, this.nextToken)
237+
: setOnNext = setOn.next,
238+
nextTokenPrevious = nextToken.previous,
239+
nextTokenBeforeSynthetic = nextToken.beforeSynthetic {
237240
rewriter._changes.add(this);
238-
}
239-
240-
Token setNext(Token setOn, Token nextToken) {
241-
this.setOn = setOn;
242-
this.setOnNext = setOn.next;
243-
this.nextToken = nextToken;
244-
this.nextTokenPrevious = nextToken.previous;
245-
this.nextTokenBeforeSynthetic = nextToken.beforeSynthetic;
246-
247241
setOn.next = nextToken;
248242
nextToken.previous = setOn;
249243
nextToken.beforeSynthetic = setOn;
250-
251-
return nextToken;
252244
}
253245

254246
@override
@@ -260,17 +252,13 @@ class NextTokenStreamChange implements TokenStreamChange {
260252
}
261253

262254
class EndGroupTokenStreamChange implements TokenStreamChange {
263-
late BeginToken setOn;
264-
Token? endGroup;
255+
final BeginToken setOn;
256+
final Token? endGroup;
265257

266-
EndGroupTokenStreamChange(UndoableTokenStreamRewriter rewriter) {
258+
EndGroupTokenStreamChange(
259+
UndoableTokenStreamRewriter rewriter, this.setOn, Token endGroup)
260+
: endGroup = setOn.endGroup {
267261
rewriter._changes.add(this);
268-
}
269-
270-
void setEndGroup(BeginToken setOn, Token endGroup) {
271-
this.setOn = setOn;
272-
this.endGroup = setOn.endGroup;
273-
274262
setOn.endGroup = endGroup;
275263
}
276264

@@ -281,17 +269,13 @@ class EndGroupTokenStreamChange implements TokenStreamChange {
281269
}
282270

283271
class OffsetTokenStreamChange implements TokenStreamChange {
284-
late Token setOn;
285-
late int offset;
272+
final Token setOn;
273+
final int offset;
286274

287-
OffsetTokenStreamChange(UndoableTokenStreamRewriter rewriter) {
275+
OffsetTokenStreamChange(
276+
UndoableTokenStreamRewriter rewriter, this.setOn, int offset)
277+
: offset = setOn.offset {
288278
rewriter._changes.add(this);
289-
}
290-
291-
void setOffset(Token setOn, int offset) {
292-
this.setOn = setOn;
293-
this.offset = setOn.offset;
294-
295279
setOn.offset = offset;
296280
}
297281

@@ -302,17 +286,13 @@ class OffsetTokenStreamChange implements TokenStreamChange {
302286
}
303287

304288
class PrecedingCommentsTokenStreamChange implements TokenStreamChange {
305-
late SimpleToken setOn;
306-
CommentToken? comment;
289+
final SimpleToken setOn;
290+
final CommentToken? comment;
307291

308-
PrecedingCommentsTokenStreamChange(UndoableTokenStreamRewriter rewriter) {
292+
PrecedingCommentsTokenStreamChange(
293+
UndoableTokenStreamRewriter rewriter, this.setOn, CommentToken? comment)
294+
: comment = setOn.precedingComments {
309295
rewriter._changes.add(this);
310-
}
311-
312-
void setPrecedingComments(SimpleToken setOn, CommentToken? comment) {
313-
this.setOn = setOn;
314-
this.comment = setOn.precedingComments;
315-
316296
setOn.precedingComments = comment;
317297
}
318298

@@ -323,17 +303,13 @@ class PrecedingCommentsTokenStreamChange implements TokenStreamChange {
323303
}
324304

325305
class PreviousTokenStreamChange implements TokenStreamChange {
326-
late Token setOn;
327-
late Token previous;
306+
final Token setOn;
307+
final Token previous;
328308

329-
PreviousTokenStreamChange(UndoableTokenStreamRewriter rewriter) {
309+
PreviousTokenStreamChange(
310+
UndoableTokenStreamRewriter rewriter, this.setOn, Token previous)
311+
: previous = setOn.previous! {
330312
rewriter._changes.add(this);
331-
}
332-
333-
void setPrevious(Token setOn, Token previous) {
334-
this.setOn = setOn;
335-
this.previous = setOn.previous!;
336-
337313
setOn.previous = previous;
338314
}
339315

@@ -359,27 +335,26 @@ class UndoableTokenStreamRewriter extends TokenStreamRewriter {
359335

360336
@override
361337
void _setEndGroup(BeginToken setOn, Token endGroup) {
362-
new EndGroupTokenStreamChange(this).setEndGroup(setOn, endGroup);
338+
new EndGroupTokenStreamChange(this, setOn, endGroup);
363339
}
364340

365341
@override
366342
Token _setNext(Token setOn, Token nextToken) {
367-
return new NextTokenStreamChange(this).setNext(setOn, nextToken);
343+
return new NextTokenStreamChange(this, setOn, nextToken).nextToken;
368344
}
369345

370346
@override
371347
void _setOffset(Token setOn, int offset) {
372-
new OffsetTokenStreamChange(this).setOffset(setOn, offset);
348+
new OffsetTokenStreamChange(this, setOn, offset);
373349
}
374350

375351
@override
376352
void _setPrecedingComments(SimpleToken setOn, CommentToken? comment) {
377-
new PrecedingCommentsTokenStreamChange(this)
378-
.setPrecedingComments(setOn, comment);
353+
new PrecedingCommentsTokenStreamChange(this, setOn, comment);
379354
}
380355

381356
@override
382357
void _setPrevious(Token setOn, Token previous) {
383-
new PreviousTokenStreamChange(this).setPrevious(setOn, previous);
358+
new PreviousTokenStreamChange(this, setOn, previous);
384359
}
385360
}

pkg/_fe_analyzer_shared/lib/src/parser/type_info_impl.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ class ComplexTypeParamOrArgInfo extends TypeParamOrArgInfo {
915915
final bool allowsVariance;
916916

917917
@override
918-
late int typeArgumentCount;
918+
int typeArgumentCount = 0;
919919

920920
/// The `>` token which ends the type parameter or argument.
921921
/// This closer may be synthetic, points to the next token in the stream,
@@ -938,7 +938,6 @@ class ComplexTypeParamOrArgInfo extends TypeParamOrArgInfo {
938938
TypeParamOrArgInfo compute() {
939939
Token token;
940940
Token next = start;
941-
typeArgumentCount = 0;
942941
while (true) {
943942
TypeInfo typeInfo =
944943
computeType(next, /* required = */ true, inDeclaration);

pkg/_fe_analyzer_shared/lib/src/scanner/abstract_scanner.dart

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ abstract class AbstractScanner implements Scanner {
115115
*/
116116
Token? commentsTail;
117117

118-
late final List<int> lineStarts;
118+
final List<int> lineStarts;
119119

120120
/**
121121
* The stack of open groups, e.g [: { ... ( .. :]
@@ -1948,22 +1948,11 @@ TokenType closeBraceInfoFor(BeginToken begin) {
19481948
}
19491949

19501950
class LineStarts extends Object with ListMixin<int> {
1951-
late List<int> array;
1951+
List<int> array;
19521952
int arrayLength = 0;
19531953

1954-
LineStarts(int? numberOfBytesHint) {
1955-
// Let's assume the average Dart file is 300 bytes.
1956-
if (numberOfBytesHint == null) numberOfBytesHint = 300;
1957-
1958-
// Let's assume we have on average 22 bytes per line.
1959-
final int expectedNumberOfLines = 1 + (numberOfBytesHint ~/ 22);
1960-
1961-
if (numberOfBytesHint > 65535) {
1962-
array = new Uint32List(expectedNumberOfLines);
1963-
} else {
1964-
array = new Uint16List(expectedNumberOfLines);
1965-
}
1966-
1954+
LineStarts(int? numberOfBytesHint)
1955+
: array = _createInitialArray(numberOfBytesHint) {
19671956
// The first line starts at character offset 0.
19681957
add(/* value = */ 0);
19691958
}
@@ -2022,6 +2011,20 @@ class LineStarts extends Object with ListMixin<int> {
20222011
newArray.setRange(/* start = */ 0, arrayLength, array);
20232012
array = newArray;
20242013
}
2014+
2015+
static List<int> _createInitialArray(int? numberOfBytesHint) {
2016+
// Let's assume the average Dart file is 300 bytes.
2017+
numberOfBytesHint ??= 300;
2018+
2019+
// Let's assume we have on average 22 bytes per line.
2020+
final int expectedNumberOfLines = 1 + (numberOfBytesHint ~/ 22);
2021+
2022+
if (numberOfBytesHint > 65535) {
2023+
return new Uint32List(expectedNumberOfLines);
2024+
} else {
2025+
return new Uint16List(expectedNumberOfLines);
2026+
}
2027+
}
20252028
}
20262029

20272030
/// [ScannerConfiguration] contains information for configuring which tokens

pkg/_fe_analyzer_shared/lib/src/scanner/reader.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,20 @@ class CharSequenceReader implements CharacterReader {
6060
/**
6161
* The number of characters in the string.
6262
*/
63-
late int _stringLength;
63+
int _stringLength;
6464

6565
/**
6666
* The index, relative to the string, of the next character to be read.
6767
*/
68-
late int _charOffset;
68+
int _charOffset;
6969

7070
/**
7171
* Initialize a newly created reader to read the characters in the given
7272
* [_sequence].
7373
*/
74-
CharSequenceReader(this._sequence) {
75-
this._stringLength = _sequence.length;
76-
this._charOffset = 0;
77-
}
74+
CharSequenceReader(this._sequence)
75+
: _stringLength = _sequence.length,
76+
_charOffset = 0;
7877

7978
@override
8079
int get offset => _charOffset - 1;

0 commit comments

Comments
 (0)