diff options
author | Lars Knoll <lars.knoll@qt.io> | 2018-04-19 21:27:28 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2018-04-27 08:11:32 +0000 |
commit | 851b8fe905ff2f3fe5c5199fdbcb930201d52b87 (patch) | |
tree | be7e68febe1ecf5df960177a78f5aefc44478dbe /src/qml/parser | |
parent | 02252ae08dc36ba44f65fb932c428849c7369299 (diff) |
Use a PatternElement for VariableDeclarations
Required to get proper destructuring working.
Change-Id: I99fc20a9f1bace1fe3981d88ce5466f9c8d98245
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/qml/parser')
-rw-r--r-- | src/qml/parser/qqmljs.g | 32 | ||||
-rw-r--r-- | src/qml/parser/qqmljsast.cpp | 9 | ||||
-rw-r--r-- | src/qml/parser/qqmljsast_p.h | 63 | ||||
-rw-r--r-- | src/qml/parser/qqmljsastfwd_p.h | 1 | ||||
-rw-r--r-- | src/qml/parser/qqmljsastvisitor_p.h | 3 |
5 files changed, 40 insertions, 68 deletions
diff --git a/src/qml/parser/qqmljs.g b/src/qml/parser/qqmljs.g index 607ca8665c..5200779924 100644 --- a/src/qml/parser/qqmljs.g +++ b/src/qml/parser/qqmljs.g @@ -261,6 +261,7 @@ public: union Value { int ival; double dval; + AST::VariableScope scope; AST::ArgumentList *ArgumentList; AST::CaseBlock *CaseBlock; AST::CaseClause *CaseClause; @@ -278,7 +279,6 @@ public: AST::Statement *Statement; AST::StatementList *StatementList; AST::Block *Block; - AST::VariableDeclaration *VariableDeclaration; AST::VariableDeclarationList *VariableDeclarationList; AST::Pattern *Pattern; AST::PatternElement *PatternElement; @@ -2737,20 +2737,20 @@ StatementListOpt: StatementList; LetOrConst: T_LET; /. case $rule_number: { - sym(1).ival = AST::VariableDeclaration::BlockScope; + sym(1).scope = AST::VariableScope::Let; } break; ./ LetOrConst: T_CONST; /. case $rule_number: { - sym(1).ival = AST::VariableDeclaration::ReadOnlyBlockScope; + sym(1).scope = AST::VariableScope::Const; } break; ./ Var: T_VAR; /. case $rule_number: { - sym(1).ival = AST::VariableDeclaration::FunctionScope; + sym(1).scope = AST::VariableScope::Var; } break; ./ @@ -2763,8 +2763,7 @@ VarDeclaration: Var VariableDeclarationList; VarDeclaration_In: Var VariableDeclarationList_In; /. case $rule_number: { - AST::VariableDeclaration::VariableScope s = AST::VariableDeclaration::VariableScope(sym(1).ival); - AST::VariableStatement *node = new (pool) AST::VariableStatement(sym(2).VariableDeclarationList->finish(s)); + AST::VariableStatement *node = new (pool) AST::VariableStatement(sym(2).VariableDeclarationList->finish(sym(1).scope)); node->declarationKindToken = loc(1); sym(1).Node = node; } break; @@ -2782,7 +2781,7 @@ VariableDeclarationList: VariableDeclaration; VariableDeclarationList_In: VariableDeclaration_In; /. case $rule_number: { - sym(1).Node = new (pool) AST::VariableDeclarationList(sym(1).VariableDeclaration); + sym(1).Node = new (pool) AST::VariableDeclarationList(sym(1).PatternElement); } break; ./ @@ -2795,7 +2794,7 @@ VariableDeclarationList: VariableDeclarationList T_COMMA VariableDeclaration; VariableDeclarationList_In: VariableDeclarationList_In T_COMMA VariableDeclaration_In; /. case $rule_number: { - AST::VariableDeclarationList *node = new (pool) AST::VariableDeclarationList(sym(1).VariableDeclarationList, sym(3).VariableDeclaration); + AST::VariableDeclarationList *node = new (pool) AST::VariableDeclarationList(sym(1).VariableDeclarationList, sym(3).PatternElement); node->commaToken = loc(2); sym(1).Node = node; } break; @@ -2810,7 +2809,7 @@ VariableDeclaration: BindingIdentifier InitializerOpt; VariableDeclaration_In: BindingIdentifier InitializerOpt_In; /. case $rule_number: { - AST::VariableDeclaration *node = new (pool) AST::VariableDeclaration(stringRef(1), sym(2).Expression); + auto *node = new (pool) AST::PatternElement(stringRef(1), sym(2).Expression); node->identifierToken = loc(1); sym(1).Node = node; } break; @@ -2823,7 +2822,14 @@ LexicalBinding_In: BindingPattern Initializer_In; VariableDeclaration: BindingPattern Initializer; /. case $rule_number: Q_FALLTHROUGH(); ./ VariableDeclaration_In: BindingPattern Initializer_In; -/. case $rule_number: { UNIMPLEMENTED; } ./ +/. + case $rule_number: { + UNIMPLEMENTED; + auto *node = new (pool) AST::PatternElement(sym(1).Pattern, sym(2).Expression); + node->identifierToken = loc(1); + sym(1).Node = node; + } break; +./ BindingPattern: T_LBRACE ObjectBindingPattern T_RBRACE; /. @@ -3125,7 +3131,7 @@ IterationStatement: T_FOR T_LPAREN LeftHandSideExpression T_IN Expression_In T_R IterationStatement: T_FOR T_LPAREN ForDeclaration T_IN Expression_In T_RPAREN Statement; /. case $rule_number: { - AST::LocalForEachStatement *node = new (pool) AST::LocalForEachStatement(sym(3).VariableDeclaration, sym(5).Expression, sym(7).Statement); + AST::LocalForEachStatement *node = new (pool) AST::LocalForEachStatement(sym(3).PatternElement, sym(5).Expression, sym(7).Statement); node->forToken = loc(1); node->lparenToken = loc(2); node->varToken = loc(3); @@ -3146,9 +3152,9 @@ ForDeclaration: LetOrConst ForBinding; ForDeclaration: Var ForBinding; /. case $rule_number: { - AST::VariableDeclaration *node = new (pool) AST::VariableDeclaration(stringRef(2), nullptr); - node->scope = AST::VariableDeclaration::VariableScope(sym(1).ival); + auto *node = new (pool) AST::PatternElement(stringRef(2), nullptr); node->identifierToken = loc(2); + node->scope = sym(1).scope; sym(1).Node = node; } break; ./ diff --git a/src/qml/parser/qqmljsast.cpp b/src/qml/parser/qqmljsast.cpp index 3aa6f83934..a9fd2e9278 100644 --- a/src/qml/parser/qqmljsast.cpp +++ b/src/qml/parser/qqmljsast.cpp @@ -483,15 +483,6 @@ void VariableDeclarationList::accept0(Visitor *visitor) visitor->endVisit(this); } -void VariableDeclaration::accept0(Visitor *visitor) -{ - if (visitor->visit(this)) { - accept(expression, visitor); - } - - visitor->endVisit(this); -} - void EmptyStatement::accept0(Visitor *visitor) { if (visitor->visit(this)) { diff --git a/src/qml/parser/qqmljsast_p.h b/src/qml/parser/qqmljsast_p.h index 5c0ef707d5..cc44c99c5d 100644 --- a/src/qml/parser/qqmljsast_p.h +++ b/src/qml/parser/qqmljsast_p.h @@ -112,6 +112,13 @@ namespace QQmlJS { namespace AST { +enum class VariableScope { + NoScope, + Var, + Let, + Const +}; + template <typename T1, typename T2> T1 cast(T2 *ast) { @@ -671,6 +678,9 @@ public: PatternElementList *elementList() const { ArrayPattern *a = cast<ArrayPattern *>(bindingPattern); return a ? a->elements : nullptr; } PatternPropertyList *propertyList() const { ObjectPattern *o = cast<ObjectPattern *>(bindingPattern); return o ? o->properties : nullptr; } + bool isVariableDeclaration() const { return scope != VariableScope::NoScope; } + bool isLexicallyScoped() const { return scope == VariableScope::Let || scope == VariableScope::Const; } + virtual void boundNames(QStringList *names); // attributes @@ -679,6 +689,8 @@ public: Pattern *bindingPattern = nullptr; ExpressionNode *initializer = nullptr; Type type = Literal; + // when used in a VariableDeclarationList + VariableScope scope = VariableScope::NoScope; }; class QML_PARSER_EXPORT PatternElementList : public Node @@ -1408,50 +1420,17 @@ public: StatementList *next; }; -class QML_PARSER_EXPORT VariableDeclaration: public Node -{ -public: - QQMLJS_DECLARE_AST_NODE(VariableDeclaration) - - enum VariableScope { - UnknownScope, - FunctionScope, - BlockScope, // let - ReadOnlyBlockScope // const - }; - - VariableDeclaration(const QStringRef &n, ExpressionNode *e) - : name(n), expression(e) - { kind = K; } - - bool isLexicallyScoped() const { return scope != FunctionScope; } - - void accept0(Visitor *visitor) override; - - SourceLocation firstSourceLocation() const override - { return identifierToken; } - - SourceLocation lastSourceLocation() const override - { return expression ? expression->lastSourceLocation() : identifierToken; } - -// attributes - QStringRef name; - ExpressionNode *expression; - SourceLocation identifierToken; - VariableScope scope = UnknownScope; -}; - class QML_PARSER_EXPORT VariableDeclarationList: public Node { public: QQMLJS_DECLARE_AST_NODE(VariableDeclarationList) - VariableDeclarationList(VariableDeclaration *decl): - declaration (decl), next (this) - { kind = K; } + VariableDeclarationList(PatternElement *decl) + : declaration(decl), next(this) + { kind = K; } - VariableDeclarationList(VariableDeclarationList *previous, VariableDeclaration *decl): - declaration (decl) + VariableDeclarationList(VariableDeclarationList *previous, PatternElement *decl) + : declaration(decl) { kind = K; next = previous->next; @@ -1470,7 +1449,7 @@ public: return declaration->lastSourceLocation(); } - inline VariableDeclarationList *finish(VariableDeclaration::VariableScope s) + inline VariableDeclarationList *finish(VariableScope s) { VariableDeclarationList *front = next; next = nullptr; @@ -1482,7 +1461,7 @@ public: } // attributes - VariableDeclaration *declaration; + PatternElement *declaration; VariableDeclarationList *next; SourceLocation commaToken; }; @@ -1724,7 +1703,7 @@ class QML_PARSER_EXPORT LocalForEachStatement: public Statement public: QQMLJS_DECLARE_AST_NODE(LocalForEachStatement) - LocalForEachStatement(VariableDeclaration *v, ExpressionNode *e, Statement *stmt): + LocalForEachStatement(PatternElement *v, ExpressionNode *e, Statement *stmt): declaration (v), expression (e), statement (stmt) { kind = K; } @@ -1737,7 +1716,7 @@ public: { return statement->lastSourceLocation(); } // attributes - VariableDeclaration *declaration; + PatternElement *declaration; ExpressionNode *expression; Statement *statement; SourceLocation forToken; diff --git a/src/qml/parser/qqmljsastfwd_p.h b/src/qml/parser/qqmljsastfwd_p.h index 590f4480a9..063c43c524 100644 --- a/src/qml/parser/qqmljsastfwd_p.h +++ b/src/qml/parser/qqmljsastfwd_p.h @@ -133,7 +133,6 @@ class Block; class StatementList; class VariableStatement; class VariableDeclarationList; -class VariableDeclaration; class EmptyStatement; class ExpressionStatement; class IfStatement; diff --git a/src/qml/parser/qqmljsastvisitor_p.h b/src/qml/parser/qqmljsastvisitor_p.h index 184fcecd1c..b3b54364c4 100644 --- a/src/qml/parser/qqmljsastvisitor_p.h +++ b/src/qml/parser/qqmljsastvisitor_p.h @@ -248,9 +248,6 @@ public: virtual bool visit(VariableDeclarationList *) { return true; } virtual void endVisit(VariableDeclarationList *) {} - virtual bool visit(VariableDeclaration *) { return true; } - virtual void endVisit(VariableDeclaration *) {} - virtual bool visit(EmptyStatement *) { return true; } virtual void endVisit(EmptyStatement *) {} |