diff options
author | Lars Knoll <lars.knoll@qt.io> | 2018-03-25 15:05:08 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2018-04-27 08:11:46 +0000 |
commit | 00b47c45097d7183ac7f3655aa4b2e6e74359e3f (patch) | |
tree | 1936bb166024948c9325a0c6fe3ebf6a760ccfc6 /src/qml | |
parent | 851b8fe905ff2f3fe5c5199fdbcb930201d52b87 (diff) |
Add support for destructuring variable declarations
Change-Id: Ia7f894fb61cfa760e253963ab4815d98103cfd9b
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/qml')
-rw-r--r-- | src/qml/compiler/qv4codegen.cpp | 41 | ||||
-rw-r--r-- | src/qml/compiler/qv4compilerscanfunctions.cpp | 36 | ||||
-rw-r--r-- | src/qml/parser/qqmljs.g | 1 |
3 files changed, 44 insertions, 34 deletions
diff --git a/src/qml/compiler/qv4codegen.cpp b/src/qml/compiler/qv4codegen.cpp index 0323012686..151e98c673 100644 --- a/src/qml/compiler/qv4codegen.cpp +++ b/src/qml/compiler/qv4codegen.cpp @@ -380,14 +380,7 @@ void Codegen::variableDeclaration(PatternElement *ast) if (!ast->initializer) return; - Reference rhs = expression(ast->initializer); - if (hasError) - return; - - Reference lhs = referenceForName(ast->bindingIdentifier, true); - //### if lhs is a temp, this won't generate a temp-to-temp move. Same for when rhs is a const - rhs.loadInAccumulator(); - lhs.storeConsumeAccumulator(); + initializeAndDestructureBindingElement(ast, Reference()); } void Codegen::variableDeclarationList(VariableDeclarationList *ast) @@ -401,19 +394,25 @@ void Codegen::initializeAndDestructureBindingElement(AST::PatternElement *e, con { RegisterScope scope(this); Reference varToStore = e->bindingIdentifier.isNull() ? Reference::fromStackSlot(this, bytecodeGenerator->newRegister()) : referenceForName(e->bindingIdentifier, true); - if (e->initializer && baseRef == varToStore) { - baseRef.loadInAccumulator(); - BytecodeGenerator::Jump jump = bytecodeGenerator->jumpNotUndefined(); - expression(e->initializer).loadInAccumulator(); - varToStore.storeConsumeAccumulator(); - jump.link(); - } else if (e->initializer) { - baseRef.loadInAccumulator(); - BytecodeGenerator::Jump jump = bytecodeGenerator->jumpNotUndefined(); - expression(e->initializer).loadInAccumulator(); - jump.link(); - varToStore.storeConsumeAccumulator(); - } else if (baseRef != varToStore) { + if (e->initializer) { + if (!baseRef.isValid()) { + // assignment + expression(e->initializer).loadInAccumulator(); + varToStore.storeConsumeAccumulator(); + } else if (baseRef == varToStore) { + baseRef.loadInAccumulator(); + BytecodeGenerator::Jump jump = bytecodeGenerator->jumpNotUndefined(); + expression(e->initializer).loadInAccumulator(); + varToStore.storeConsumeAccumulator(); + jump.link(); + } else { + baseRef.loadInAccumulator(); + BytecodeGenerator::Jump jump = bytecodeGenerator->jumpNotUndefined(); + expression(e->initializer).loadInAccumulator(); + jump.link(); + varToStore.storeConsumeAccumulator(); + } + } else if (baseRef != varToStore && baseRef.isValid()) { baseRef.loadInAccumulator(); varToStore.storeConsumeAccumulator(); } diff --git a/src/qml/compiler/qv4compilerscanfunctions.cpp b/src/qml/compiler/qv4compilerscanfunctions.cpp index 3f98d7fd67..c6e792fc45 100644 --- a/src/qml/compiler/qv4compilerscanfunctions.cpp +++ b/src/qml/compiler/qv4compilerscanfunctions.cpp @@ -196,18 +196,30 @@ bool ScanFunctions::visit(PatternElement *ast) if (!ast->isVariableDeclaration()) return true; - if (_context->isStrict && (ast->bindingIdentifier == QLatin1String("eval") || ast->bindingIdentifier == QLatin1String("arguments"))) - _cg->throwSyntaxError(ast->identifierToken, QStringLiteral("Variable name may not be eval or arguments in strict mode")); - checkName(QStringRef(&ast->bindingIdentifier), ast->identifierToken); - if (ast->bindingIdentifier == QLatin1String("arguments")) - _context->usesArgumentsObject = Context::ArgumentsObjectNotUsed; - if (ast->scope == VariableScope::Const && !ast->initializer) { - _cg->throwSyntaxError(ast->identifierToken, QStringLiteral("Missing initializer in const declaration")); - return false; - } - if (!_context->addLocalVar(ast->bindingIdentifier, ast->initializer ? Context::VariableDefinition : Context::VariableDeclaration, ast->scope)) { - _cg->throwSyntaxError(ast->identifierToken, QStringLiteral("Identifier %1 has already been declared").arg(ast->bindingIdentifier)); - return false; + QStringList names; + ast->boundNames(&names); + + for (const QString &name : qAsConst(names)) { + if (_context->isStrict && (name == QLatin1String("eval") || name == QLatin1String("arguments"))) + _cg->throwSyntaxError(ast->identifierToken, QStringLiteral("Variable name may not be eval or arguments in strict mode")); + checkName(QStringRef(&name), ast->identifierToken); + if (name == QLatin1String("arguments")) + _context->usesArgumentsObject = Context::ArgumentsObjectNotUsed; + if (ast->scope == VariableScope::Const && !ast->initializer) { + _cg->throwSyntaxError(ast->identifierToken, QStringLiteral("Missing initializer in const declaration")); + return false; + } + const Context::Member *m = nullptr; + if (_context->memberInfo(name, &m)) { + if (m->isLexicallyScoped() || ast->isLexicallyScoped()) { + _cg->throwSyntaxError(ast->identifierToken, QStringLiteral("Identifier %1 has already been declared").arg(name)); + return false; + } + } + if (!_context->addLocalVar(name, ast->initializer ? Context::VariableDefinition : Context::VariableDeclaration, ast->scope)) { + _cg->throwSyntaxError(ast->identifierToken, QStringLiteral("Identifier %1 has already been declared").arg(ast->bindingIdentifier)); + return false; + } } return true; } diff --git a/src/qml/parser/qqmljs.g b/src/qml/parser/qqmljs.g index 5200779924..bf733dd6cd 100644 --- a/src/qml/parser/qqmljs.g +++ b/src/qml/parser/qqmljs.g @@ -2824,7 +2824,6 @@ VariableDeclaration: BindingPattern Initializer; VariableDeclaration_In: BindingPattern Initializer_In; /. case $rule_number: { - UNIMPLEMENTED; auto *node = new (pool) AST::PatternElement(sym(1).Pattern, sym(2).Expression); node->identifierToken = loc(1); sym(1).Node = node; |