Cleanup intermediate control flow collection structures in AstBuilder
Change-Id: I8aed607943dea39e5fe393ddf5f5ca00eddb1b42
Reviewed-on: https://dart-review.googlesource.com/c/92840
Commit-Queue: Dan Rubel <danrubel@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/src/fasta/ast_builder.dart b/pkg/analyzer/lib/src/fasta/ast_builder.dart
index 572a8d8..edbcf3c 100644
--- a/pkg/analyzer/lib/src/fasta/ast_builder.dart
+++ b/pkg/analyzer/lib/src/fasta/ast_builder.dart
@@ -288,7 +288,7 @@
@override
void endIfControlFlow(Token token) {
- var thenElement = pop();
+ CollectionElement thenElement = pop();
ParenthesizedExpression condition = pop();
Token ifToken = pop();
pushIfControlFlowInfo(ifToken, condition, thenElement, null, null);
@@ -296,26 +296,31 @@
@override
void endIfElseControlFlow(Token token) {
- var elseElement = pop();
+ CollectionElement elseElement = pop();
Token elseToken = pop();
- var thenElement = pop();
+ CollectionElement thenElement = pop();
ParenthesizedExpression condition = pop();
Token ifToken = pop();
pushIfControlFlowInfo(
ifToken, condition, thenElement, elseToken, elseElement);
}
- void pushIfControlFlowInfo(Token ifToken, ParenthesizedExpression condition,
- var thenElement, Token elseToken, var elseElement) {
+ void pushIfControlFlowInfo(
+ Token ifToken,
+ ParenthesizedExpression condition,
+ CollectionElement thenElement,
+ Token elseToken,
+ CollectionElement elseElement) {
if (enableControlFlowCollections) {
- push(new _IfControlFlowInfo(
- ifToken,
- condition.leftParenthesis,
- condition.expression,
- condition.rightParenthesis,
- thenElement,
- elseToken,
- elseElement));
+ push(ast.ifElement(
+ ifKeyword: ifToken,
+ leftParenthesis: condition.leftParenthesis,
+ condition: condition.expression,
+ rightParenthesis: condition.rightParenthesis,
+ thenElement: thenElement,
+ elseKeyword: elseToken,
+ elseElement: elseElement,
+ ));
} else {
handleRecoverableError(
templateUnexpectedToken.withArguments(ifToken), ifToken, ifToken);
@@ -923,8 +928,14 @@
void pushForControlFlowInfo(Token awaitToken, Token forToken,
Token leftParenthesis, ForLoopParts forLoopParts, Object entry) {
if (enableControlFlowCollections) {
- push(new _ForControlFlowInfo(awaitToken, forToken, leftParenthesis,
- forLoopParts, leftParenthesis.endGroup, entry));
+ push(ast.forElement(
+ awaitKeyword: awaitToken,
+ forKeyword: forToken,
+ leftParenthesis: leftParenthesis,
+ forLoopParts: forLoopParts,
+ rightParenthesis: leftParenthesis.endGroup,
+ body: entry as CollectionElement,
+ ));
} else {
handleRecoverableError(
templateUnexpectedToken.withArguments(forToken), forToken, forToken);
@@ -1079,7 +1090,7 @@
debugEvent("LiteralMap");
if (enableControlFlowCollections || enableSpreadCollections) {
- List<CollectionElement> entries = popMapElements(count);
+ List<CollectionElement> entries = popCollectionElements(count);
TypeArgumentList typeArguments = pop();
push(ast.mapLiteral2(
constKeyword: constKeyword,
@@ -3141,9 +3152,7 @@
final elements = new List<CollectionElement>()..length = count;
for (int index = count - 1; index >= 0; --index) {
var element = pop();
- elements[index] = element is _EntryInfo
- ? element.asCollectionElement(ast)
- : element as CollectionElement;
+ elements[index] = element as CollectionElement;
}
return elements;
}
@@ -3152,17 +3161,6 @@
if (n == 0) return null;
return stack.popList(n, list, null);
}
-
- List<CollectionElement> popMapElements(int count) {
- final entries = new List<CollectionElement>()..length = count;
- for (int index = count - 1; index >= 0; --index) {
- var entry = pop();
- entries[index] = entry is _EntryInfo
- ? entry.asMapElement(ast)
- : entry as CollectionElement;
- }
- return entries;
- }
}
/// Data structure placed on the stack to represent the default parameter
@@ -3276,93 +3274,3 @@
_ConstructorNameWithInvalidTypeArgs(this.name, this.invalidTypeArgs);
}
-
-abstract class _EntryInfo {
- CollectionElement asCollectionElement(AstFactory ast);
- CollectionElement asMapElement(AstFactory ast);
-}
-
-class _ForControlFlowInfo implements _EntryInfo {
- final Token awaitToken;
- final Token forKeyword;
- final Token leftParenthesis;
- final ForLoopParts forLoopParts;
- final Token rightParenthesis;
- final entry;
-
- _ForControlFlowInfo(this.awaitToken, this.forKeyword, this.leftParenthesis,
- this.forLoopParts, this.rightParenthesis, this.entry);
-
- @override
- CollectionElement asCollectionElement(AstFactory ast) => ast.forElement(
- awaitKeyword: awaitToken,
- forKeyword: forKeyword,
- leftParenthesis: leftParenthesis,
- forLoopParts: forLoopParts,
- rightParenthesis: rightParenthesis,
- body: entry is _EntryInfo
- ? entry.asCollectionElement(ast)
- : entry as CollectionElement,
- );
-
- @override
- CollectionElement asMapElement(AstFactory ast) => ast.forElement(
- awaitKeyword: awaitToken,
- forKeyword: forKeyword,
- leftParenthesis: leftParenthesis,
- forLoopParts: forLoopParts,
- rightParenthesis: rightParenthesis,
- body: entry is _EntryInfo
- ? entry.asMapElement(ast)
- : entry as CollectionElement,
- );
-}
-
-class _IfControlFlowInfo implements _EntryInfo {
- final Token ifToken;
- final Token leftParenthesis;
- final Expression conditionExpression;
- final Token rightParenthesis;
- final thenElement;
- final Token elseToken;
- final elseElement;
-
- _IfControlFlowInfo(
- this.ifToken,
- this.leftParenthesis,
- this.conditionExpression,
- this.rightParenthesis,
- this.thenElement,
- this.elseToken,
- this.elseElement);
-
- @override
- CollectionElement asCollectionElement(AstFactory ast) => ast.ifElement(
- ifKeyword: ifToken,
- leftParenthesis: leftParenthesis,
- condition: conditionExpression,
- rightParenthesis: rightParenthesis,
- thenElement: thenElement is _EntryInfo
- ? thenElement.asCollectionElement(ast)
- : thenElement as CollectionElement,
- elseKeyword: elseToken,
- elseElement: elseElement is _EntryInfo
- ? elseElement.asCollectionElement(ast)
- : elseElement as CollectionElement,
- );
-
- @override
- CollectionElement asMapElement(AstFactory ast) => ast.ifElement(
- ifKeyword: ifToken,
- leftParenthesis: leftParenthesis,
- condition: conditionExpression,
- rightParenthesis: rightParenthesis,
- thenElement: thenElement is _EntryInfo
- ? thenElement.asMapElement(ast)
- : thenElement as CollectionElement,
- elseKeyword: elseToken,
- elseElement: elseElement is _EntryInfo
- ? elseElement.asMapElement(ast)
- : elseElement as CollectionElement,
- );
-}