aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/compiler
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2018-05-08 15:21:27 +0200
committerLars Knoll <lars.knoll@qt.io>2018-05-11 07:17:16 +0000
commita8766dcd8c3a046aab1453ec3d28436bb75c895c (patch)
treea3ec1dd79c8a1604e0009b25aa01a9050c15cbb5 /src/qml/compiler
parent01a1ad296c2b8325476abd6d28c8cc2463c42eb6 (diff)
Unify ForeachStatement and LocalForeachStatement in the AST
This saves quite some duplicated code, but requires a bit of care when iterating over the AST. Change-Id: Ic530de4be8b36b4079c9d544b4b77982c3b8be60 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/qml/compiler')
-rw-r--r--src/qml/compiler/qv4codegen.cpp41
-rw-r--r--src/qml/compiler/qv4codegen_p.h1
-rw-r--r--src/qml/compiler/qv4compilerscanfunctions.cpp12
-rw-r--r--src/qml/compiler/qv4compilerscanfunctions_p.h1
4 files changed, 12 insertions, 43 deletions
diff --git a/src/qml/compiler/qv4codegen.cpp b/src/qml/compiler/qv4codegen.cpp
index 8c6964930d..31c0764570 100644
--- a/src/qml/compiler/qv4codegen.cpp
+++ b/src/qml/compiler/qv4codegen.cpp
@@ -79,7 +79,6 @@ static inline void setJumpOutLocation(QV4::Moth::BytecodeGenerator *bytecodeGene
case Statement::Kind_ForEachStatement:
case Statement::Kind_ForStatement:
case Statement::Kind_IfStatement:
- case Statement::Kind_LocalForEachStatement:
case Statement::Kind_LocalForStatement:
case Statement::Kind_WhileStatement:
bytecodeGenerator->setLocation(fallback);
@@ -2611,7 +2610,16 @@ bool Codegen::visit(ForEachStatement *ast)
bytecodeGenerator->addInstruction(iteratorObjInstr);
iterator.storeConsumeAccumulator();
- Reference lhs = expression(ast->initialiser);
+ Reference lhs;
+ if (ExpressionNode *e = ast->lhs->expressionCast()) {
+ lhs = expression(e);
+ } else if (PatternElement *p = AST::cast<PatternElement *>(ast->lhs)) {
+ variableDeclaration(p);
+ lhs = referenceForName(p->bindingIdentifier, true).asLValue();
+ } else {
+ Q_UNREACHABLE();
+ }
+
if (hasError)
return false;
if (!lhs.isLValue()) {
@@ -2625,32 +2633,6 @@ bool Codegen::visit(ForEachStatement *ast)
return false;
}
-bool Codegen::visit(LocalForEachStatement *ast)
-{
- if (hasError)
- return true;
-
- RegisterScope scope(this);
-
- Reference iterator = Reference::fromStackSlot(this);
- Reference expr = expression(ast->expression);
- if (hasError)
- return true;
-
- variableDeclaration(ast->declaration);
-
- expr.loadInAccumulator();
- Instruction::GetIterator iteratorObjInstr;
- iteratorObjInstr.iterator = (ast->type == ForEachType::Of) ? 1 : 0;
- bytecodeGenerator->addInstruction(iteratorObjInstr);
- iterator.storeConsumeAccumulator();
-
- Reference lhs = referenceForName(ast->declaration->bindingIdentifier, true).asLValue();
- foreachBody(lhs, ast->statement, ast->forToken, iterator);
-
- return false;
-}
-
void Codegen::foreachBody(const Reference &lhs, Statement *statements, const SourceLocation &forToken, const Reference &iterator)
{
RegisterScope scope(this);
@@ -2771,8 +2753,7 @@ bool Codegen::visit(LabelledStatement *ast)
AST::cast<AST::DoWhileStatement *>(ast->statement) ||
AST::cast<AST::ForStatement *>(ast->statement) ||
AST::cast<AST::ForEachStatement *>(ast->statement) ||
- AST::cast<AST::LocalForStatement *>(ast->statement) ||
- AST::cast<AST::LocalForEachStatement *>(ast->statement)) {
+ AST::cast<AST::LocalForStatement *>(ast->statement)) {
statement(ast->statement); // labelledStatement will be associated with the ast->statement's loop.
} else {
BytecodeGenerator::Label breakLabel = bytecodeGenerator->newLabel();
diff --git a/src/qml/compiler/qv4codegen_p.h b/src/qml/compiler/qv4codegen_p.h
index d742fe06b7..18ccf04dc8 100644
--- a/src/qml/compiler/qv4codegen_p.h
+++ b/src/qml/compiler/qv4codegen_p.h
@@ -603,7 +603,6 @@ protected:
bool visit(AST::ForStatement *ast) override;
bool visit(AST::IfStatement *ast) override;
bool visit(AST::LabelledStatement *ast) override;
- bool visit(AST::LocalForEachStatement *ast) override;
bool visit(AST::LocalForStatement *ast) override;
bool visit(AST::ReturnStatement *ast) override;
bool visit(AST::SwitchStatement *ast) override;
diff --git a/src/qml/compiler/qv4compilerscanfunctions.cpp b/src/qml/compiler/qv4compilerscanfunctions.cpp
index 4dbd11ef1c..eadbc3ae17 100644
--- a/src/qml/compiler/qv4compilerscanfunctions.cpp
+++ b/src/qml/compiler/qv4compilerscanfunctions.cpp
@@ -342,17 +342,7 @@ bool ScanFunctions::visit(LocalForStatement *ast) {
}
bool ScanFunctions::visit(ForEachStatement *ast) {
- Node::accept(ast->initialiser, this);
- Node::accept(ast->expression, this);
-
- TemporaryBoolAssignment allowFuncDecls(_allowFuncDecls, !_context->isStrict);
- Node::accept(ast->statement, this);
-
- return false;
-}
-
-bool ScanFunctions::visit(LocalForEachStatement *ast) {
- Node::accept(ast->declaration, this);
+ Node::accept(ast->lhs, this);
Node::accept(ast->expression, this);
TemporaryBoolAssignment allowFuncDecls(_allowFuncDecls, !_context->isStrict);
diff --git a/src/qml/compiler/qv4compilerscanfunctions_p.h b/src/qml/compiler/qv4compilerscanfunctions_p.h
index 31e260b7ad..7d29af4fa3 100644
--- a/src/qml/compiler/qv4compilerscanfunctions_p.h
+++ b/src/qml/compiler/qv4compilerscanfunctions_p.h
@@ -127,7 +127,6 @@ protected:
bool visit(AST::ForStatement *ast) override;
bool visit(AST::LocalForStatement *ast) override;
bool visit(AST::ForEachStatement *ast) override;
- bool visit(AST::LocalForEachStatement *ast) override;
bool visit(AST::ThisExpression *ast) override;
bool visit(AST::Block *ast) override;