diff options
author | Michael Brasser <michael.brasser@nokia.com> | 2010-11-18 04:26:33 (GMT) |
---|---|---|
committer | Michael Brasser <michael.brasser@nokia.com> | 2010-11-19 00:41:12 (GMT) |
commit | 03c671e557a59a2c908cb8241c7ad5c31536841d (patch) | |
tree | 5f7415134fd7e1b23ee384e4442298d252aa2cf0 /src/declarative/qml/qdeclarativerewrite.cpp | |
parent | e8e28735046d419463e235a58a7c4c88d04163db (diff) | |
download | Qt-03c671e557a59a2c908cb8241c7ad5c31536841d.zip Qt-03c671e557a59a2c908cb8241c7ad5c31536841d.tar.gz Qt-03c671e557a59a2c908cb8241c7ad5c31536841d.tar.bz2 |
Optimize binding rewrites.
Use the existing AST rather than recreating it.
Task-number: QTBUG-15331
Reviewed-by: Aaron Kennedy
Diffstat (limited to 'src/declarative/qml/qdeclarativerewrite.cpp')
-rw-r--r-- | src/declarative/qml/qdeclarativerewrite.cpp | 53 |
1 files changed, 51 insertions, 2 deletions
diff --git a/src/declarative/qml/qdeclarativerewrite.cpp b/src/declarative/qml/qdeclarativerewrite.cpp index 80b9ab2..219674f 100644 --- a/src/declarative/qml/qdeclarativerewrite.cpp +++ b/src/declarative/qml/qdeclarativerewrite.cpp @@ -65,10 +65,10 @@ bool SharedBindingTester::isSharable(const QString &code) return isSharable(parser.statement()); } -bool SharedBindingTester::isSharable(AST::Statement *statement) +bool SharedBindingTester::isSharable(AST::Node *node) { _sharable = true; - AST::Node::acceptChild(statement, this); + AST::Node::acceptChild(node, this); return _sharable; } @@ -93,6 +93,55 @@ QString RewriteBinding::operator()(const QString &code, bool *ok, bool *sharable return rewrite(code, 0, parser.statement()); } +QString RewriteBinding::operator()(QDeclarativeJS::AST::Node *node, const QString &code, bool *sharable) +{ + if (!node) + return code; + + if (sharable) { + SharedBindingTester tester; + *sharable = tester.isSharable(node); + } + + QDeclarativeJS::AST::ExpressionNode *expression = node->expressionCast(); + QDeclarativeJS::AST::Statement *statement = node->statementCast(); + if(!expression && !statement) + return code; + + TextWriter w; + _writer = &w; + _position = expression ? expression->firstSourceLocation().begin() : statement->firstSourceLocation().begin(); + _inLoop = 0; + + accept(node); + + unsigned startOfStatement = 0; + unsigned endOfStatement = (expression ? expression->lastSourceLocation().end() : statement->lastSourceLocation().end()) - _position; + + QString startString = QLatin1String("(function ") + QString::fromUtf8(_name) + QLatin1String("() { "); + if (expression) + startString += QLatin1String("return "); + _writer->replace(startOfStatement, 0, startString); + _writer->replace(endOfStatement, 0, QLatin1String(" })")); + + if (rewriteDump()) { + qWarning() << "============================================================="; + qWarning() << "Rewrote:"; + qWarning() << qPrintable(code); + } + + QString codeCopy = code; + w.write(&codeCopy); + + if (rewriteDump()) { + qWarning() << "To:"; + qWarning() << qPrintable(code); + qWarning() << "============================================================="; + } + + return codeCopy; +} + void RewriteBinding::accept(AST::Node *node) { AST::Node::acceptChild(node, this); |