1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qdeclarativerewrite_p.h"
44 #include "qdeclarativeglobal_p.h"
46 #include <QtCore/qdebug.h>
50 DEFINE_BOOL_CONFIG_OPTION(rewriteDump, QML_REWRITE_DUMP);
52 namespace QDeclarativeRewrite {
54 bool SharedBindingTester::isSharable(const QString &code)
58 Parser parser(&engine);
59 lexer.setCode(code, 0);
60 parser.parseStatement();
61 if (!parser.statement())
64 return isSharable(parser.statement());
67 bool SharedBindingTester::isSharable(AST::Node *node)
70 AST::Node::acceptChild(node, this);
74 QString RewriteBinding::operator()(const QString &code, bool *ok, bool *sharable)
78 Parser parser(&engine);
79 lexer.setCode(code, 0);
80 parser.parseStatement();
81 if (!parser.statement()) {
87 SharedBindingTester tester;
88 *sharable = tester.isSharable(parser.statement());
91 return rewrite(code, 0, parser.statement());
94 QString RewriteBinding::operator()(QDeclarativeJS::AST::Node *node, const QString &code, bool *sharable)
100 SharedBindingTester tester;
101 *sharable = tester.isSharable(node);
104 QDeclarativeJS::AST::ExpressionNode *expression = node->expressionCast();
105 QDeclarativeJS::AST::Statement *statement = node->statementCast();
106 if(!expression && !statement)
111 _position = expression ? expression->firstSourceLocation().begin() : statement->firstSourceLocation().begin();
116 unsigned startOfStatement = 0;
117 unsigned endOfStatement = (expression ? expression->lastSourceLocation().end() : statement->lastSourceLocation().end()) - _position;
119 QString startString = QLatin1String("(function ") + _name + QLatin1String("() { ");
121 startString += QLatin1String("return ");
122 _writer->replace(startOfStatement, 0, startString);
123 _writer->replace(endOfStatement, 0, QLatin1String(" })"));
126 qWarning() << "=============================================================";
127 qWarning() << "Rewrote:";
128 qWarning() << qPrintable(code);
131 QString codeCopy = code;
136 qWarning() << qPrintable(codeCopy);
137 qWarning() << "=============================================================";
143 void RewriteBinding::accept(AST::Node *node)
145 AST::Node::acceptChild(node, this);
148 QString RewriteBinding::rewrite(QString code, unsigned position,
149 AST::Statement *node)
153 _position = position;
158 unsigned startOfStatement = node->firstSourceLocation().begin() - _position;
159 unsigned endOfStatement = node->lastSourceLocation().end() - _position;
161 _writer->replace(startOfStatement, 0, QLatin1String("(function ") + _name + QLatin1String("() { "));
162 _writer->replace(endOfStatement, 0, QLatin1String(" })"));
165 qWarning() << "=============================================================";
166 qWarning() << "Rewrote:";
167 qWarning() << qPrintable(code);
174 qWarning() << qPrintable(code);
175 qWarning() << "=============================================================";
181 bool RewriteBinding::visit(AST::Block *ast)
183 for (AST::StatementList *it = ast->statements; it; it = it->next) {
185 // we need to rewrite only the last statement of a block.
186 accept(it->statement);
193 bool RewriteBinding::visit(AST::ExpressionStatement *ast)
196 unsigned startOfExpressionStatement = ast->firstSourceLocation().begin() - _position;
197 _writer->replace(startOfExpressionStatement, 0, QLatin1String("return "));
203 bool RewriteBinding::visit(AST::DoWhileStatement *)
209 void RewriteBinding::endVisit(AST::DoWhileStatement *)
214 bool RewriteBinding::visit(AST::WhileStatement *)
220 void RewriteBinding::endVisit(AST::WhileStatement *)
225 bool RewriteBinding::visit(AST::ForStatement *)
231 void RewriteBinding::endVisit(AST::ForStatement *)
236 bool RewriteBinding::visit(AST::LocalForStatement *)
242 void RewriteBinding::endVisit(AST::LocalForStatement *)
247 bool RewriteBinding::visit(AST::ForEachStatement *)
253 void RewriteBinding::endVisit(AST::ForEachStatement *)
258 bool RewriteBinding::visit(AST::LocalForEachStatement *)
264 void RewriteBinding::endVisit(AST::LocalForEachStatement *)
269 } // namespace QDeclarativeRewrite