1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qqmlrewrite_p.h"
44 #include <private/qqmlglobal_p.h>
46 #include <QtCore/qdebug.h>
50 DEFINE_BOOL_CONFIG_OPTION(rewriteDump, QML_REWRITE_DUMP)
52 namespace QQmlRewrite {
54 static void rewriteStringLiteral(AST::StringLiteral *ast, const QString *code, int startPosition, TextWriter *writer)
56 const unsigned position = ast->firstSourceLocation().begin() - startPosition + 1;
57 const unsigned length = ast->literalToken.length - 2;
58 const QStringRef spell = code->midRef(position, length);
59 const int end = spell.size();
63 const QChar ch = spell.at(index++);
65 if (index < end && ch == QLatin1Char('\\')) {
68 // skip a possibly empty sequence of \r characters
69 while (pos < end && spell.at(pos) == QLatin1Char('\r'))
72 if (pos < end && spell.at(pos) == QLatin1Char('\n')) {
73 // This is a `\' followed by a newline terminator.
74 // In this case there's nothing to replace. We keep the code
75 // as it is and we resume the searching.
76 index = pos + 1; // refresh the index
78 } else if (ch == QLatin1Char('\r') || ch == QLatin1Char('\n')) {
79 const QString sep = ch == QLatin1Char('\r') ? QLatin1String("\\r") : QLatin1String("\\n");
80 const int pos = index - 1;
83 while (index < end && spell.at(index) == ch) {
88 writer->replace(position + pos, index - pos, s);
93 bool SharedBindingTester::isSharable(const QString &code)
97 Parser parser(&engine);
98 lexer.setCode(code, 0);
99 parser.parseStatement();
100 if (!parser.statement())
103 return isSharable(parser.statement());
106 bool SharedBindingTester::isSharable(AST::Node *node)
109 AST::Node::acceptChild(node, this);
113 QString RewriteBinding::operator()(const QString &code, bool *ok, bool *sharable)
116 Lexer lexer(&engine);
117 Parser parser(&engine);
118 lexer.setCode(code, 0);
119 parser.parseStatement();
120 if (!parser.statement()) {
126 SharedBindingTester tester;
127 *sharable = tester.isSharable(parser.statement());
130 return rewrite(code, 0, parser.statement());
133 QString RewriteBinding::operator()(QQmlJS::AST::Node *node, const QString &code, bool *sharable)
139 SharedBindingTester tester;
140 *sharable = tester.isSharable(node);
143 QQmlJS::AST::ExpressionNode *expression = node->expressionCast();
144 QQmlJS::AST::Statement *statement = node->statementCast();
145 if(!expression && !statement)
150 _position = expression ? expression->firstSourceLocation().begin() : statement->firstSourceLocation().begin();
156 unsigned startOfStatement = 0;
157 unsigned endOfStatement = (expression ? expression->lastSourceLocation().end() : statement->lastSourceLocation().end()) - _position;
159 QString startString = QLatin1String("(function ") + _name + QLatin1String("() { ");
161 startString += QLatin1String("return ");
162 _writer->replace(startOfStatement, 0, startString);
163 _writer->replace(endOfStatement, 0, QLatin1String(" })"));
166 qWarning() << "=============================================================";
167 qWarning() << "Rewrote:";
168 qWarning() << qPrintable(code);
171 QString codeCopy = code;
176 qWarning() << qPrintable(codeCopy);
177 qWarning() << "=============================================================";
183 void RewriteBinding::accept(AST::Node *node)
185 AST::Node::acceptChild(node, this);
188 QString RewriteBinding::rewrite(QString code, unsigned position,
189 AST::Statement *node)
193 _position = position;
199 unsigned startOfStatement = node->firstSourceLocation().begin() - _position;
200 unsigned endOfStatement = node->lastSourceLocation().end() - _position;
202 _writer->replace(startOfStatement, 0, QLatin1String("(function ") + _name + QLatin1String("() { "));
203 _writer->replace(endOfStatement, 0, QLatin1String(" })"));
206 qWarning() << "=============================================================";
207 qWarning() << "Rewrote:";
208 qWarning() << qPrintable(code);
215 qWarning() << qPrintable(code);
216 qWarning() << "=============================================================";
222 bool RewriteBinding::visit(AST::Block *ast)
224 for (AST::StatementList *it = ast->statements; it; it = it->next) {
226 // we need to rewrite only the last statement of a block.
227 accept(it->statement);
234 bool RewriteBinding::visit(AST::ExpressionStatement *ast)
237 unsigned startOfExpressionStatement = ast->firstSourceLocation().begin() - _position;
238 _writer->replace(startOfExpressionStatement, 0, QLatin1String("return "));
244 bool RewriteBinding::visit(AST::StringLiteral *ast)
246 rewriteStringLiteral(ast, _code, _position, _writer);
250 bool RewriteBinding::visit(AST::DoWhileStatement *)
256 void RewriteBinding::endVisit(AST::DoWhileStatement *)
261 bool RewriteBinding::visit(AST::WhileStatement *)
267 void RewriteBinding::endVisit(AST::WhileStatement *)
272 bool RewriteBinding::visit(AST::ForStatement *)
278 void RewriteBinding::endVisit(AST::ForStatement *)
283 bool RewriteBinding::visit(AST::LocalForStatement *)
289 void RewriteBinding::endVisit(AST::LocalForStatement *)
294 bool RewriteBinding::visit(AST::ForEachStatement *)
300 void RewriteBinding::endVisit(AST::ForEachStatement *)
305 bool RewriteBinding::visit(AST::LocalForEachStatement *)
311 void RewriteBinding::endVisit(AST::LocalForEachStatement *)
316 bool RewriteBinding::visit(AST::FunctionExpression *)
321 bool RewriteBinding::visit(AST::FunctionDeclaration *)
326 bool RewriteBinding::visit(AST::CaseBlock *ast)
328 // Process the initial sequence of the case clauses.
329 for (AST::CaseClauses *it = ast->clauses; it; it = it->next) {
330 // Return the value of the last statement in the block, if this is the last `case clause'
331 // of the switch statement.
332 bool returnTheValueOfLastStatement = (it->next == 0) && (ast->defaultClause == 0) && (ast->moreClauses == 0);
334 if (AST::CaseClause *clause = it->clause) {
335 accept(clause->expression);
336 rewriteCaseStatements(clause->statements, returnTheValueOfLastStatement);
340 // Process the default case clause
341 if (ast->defaultClause) {
342 // Return the value of the last statement in the block, if this is the last `case clause'
343 // of the switch statement.
344 bool rewriteTheLastStatement = (ast->moreClauses == 0);
346 rewriteCaseStatements(ast->defaultClause->statements, rewriteTheLastStatement);
349 // Process trailing `case clauses'
350 for (AST::CaseClauses *it = ast->moreClauses; it; it = it->next) {
351 // Return the value of the last statement in the block, if this is the last `case clause'
352 // of the switch statement.
353 bool returnTheValueOfLastStatement = (it->next == 0);
355 if (AST::CaseClause *clause = it->clause) {
356 accept(clause->expression);
357 rewriteCaseStatements(clause->statements, returnTheValueOfLastStatement);
364 void RewriteBinding::rewriteCaseStatements(AST::StatementList *statements, bool rewriteTheLastStatement)
366 for (AST::StatementList *it = statements; it; it = it->next) {
367 if (it->next && AST::cast<AST::BreakStatement *>(it->next->statement) != 0) {
368 // The value of the first statement followed by a `break'.
369 accept(it->statement);
371 } else if (!it->next) {
372 if (rewriteTheLastStatement)
373 accept(it->statement);
374 else if (AST::Block *block = AST::cast<AST::Block *>(it->statement))
375 rewriteCaseStatements(block->statements, rewriteTheLastStatement);
380 RewriteSignalHandler::RewriteSignalHandler()
387 void RewriteSignalHandler::accept(AST::Node *node)
389 AST::Node::acceptChild(node, this);
392 bool RewriteSignalHandler::visit(AST::StringLiteral *ast)
394 rewriteStringLiteral(ast, _code, _position, _writer);
398 QString RewriteSignalHandler::operator()(QQmlJS::AST::Node *node, const QString &code, const QString &name)
401 qWarning() << "=============================================================";
402 qWarning() << "Rewrote:";
403 qWarning() << qPrintable(code);
406 QQmlJS::AST::ExpressionNode *expression = node->expressionCast();
407 QQmlJS::AST::Statement *statement = node->statementCast();
408 if (!expression && !statement)
415 _position = expression ? expression->firstSourceLocation().begin() : statement->firstSourceLocation().begin();
418 QString rewritten = code;
421 rewritten = QStringLiteral("(function ") + name + QStringLiteral("() { ") + rewritten + QStringLiteral(" })");
425 qWarning() << qPrintable(rewritten);
426 qWarning() << "=============================================================";
432 QString RewriteSignalHandler::operator()(const QString &code, const QString &name, bool *ok)
435 Lexer lexer(&engine);
436 Parser parser(&engine);
437 lexer.setCode(code, 0);
438 parser.parseStatement();
439 if (!parser.statement()) {
444 return operator()(parser.statement(), code, name);
447 } // namespace QQmlRewrite