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 "private/qdeclarativerewrite_p.h"
44 #include "private/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)
57 NodePool pool(QString(), &engine);
59 Parser parser(&engine);
60 lexer.setCode(code, 0);
61 parser.parseStatement();
62 if (!parser.statement())
65 return isSharable(parser.statement());
68 bool SharedBindingTester::isSharable(AST::Node *node)
71 AST::Node::acceptChild(node, this);
75 QString RewriteBinding::operator()(const QString &code, bool *ok, bool *sharable)
78 NodePool pool(QString(), &engine);
80 Parser parser(&engine);
81 lexer.setCode(code, 0);
82 parser.parseStatement();
83 if (!parser.statement()) {
89 SharedBindingTester tester;
90 *sharable = tester.isSharable(parser.statement());
93 return rewrite(code, 0, parser.statement());
96 QString RewriteBinding::operator()(QDeclarativeJS::AST::Node *node, const QString &code, bool *sharable)
102 SharedBindingTester tester;
103 *sharable = tester.isSharable(node);
106 QDeclarativeJS::AST::ExpressionNode *expression = node->expressionCast();
107 QDeclarativeJS::AST::Statement *statement = node->statementCast();
108 if(!expression && !statement)
113 _position = expression ? expression->firstSourceLocation().begin() : statement->firstSourceLocation().begin();
118 unsigned startOfStatement = 0;
119 unsigned endOfStatement = (expression ? expression->lastSourceLocation().end() : statement->lastSourceLocation().end()) - _position;
121 QString startString = QLatin1String("(function ") + QString::fromUtf8(_name) + QLatin1String("() { ");
123 startString += QLatin1String("return ");
124 _writer->replace(startOfStatement, 0, startString);
125 _writer->replace(endOfStatement, 0, QLatin1String(" })"));
128 qWarning() << "=============================================================";
129 qWarning() << "Rewrote:";
130 qWarning() << qPrintable(code);
133 QString codeCopy = code;
138 qWarning() << qPrintable(codeCopy);
139 qWarning() << "=============================================================";
145 void RewriteBinding::accept(AST::Node *node)
147 AST::Node::acceptChild(node, this);
150 QString RewriteBinding::rewrite(QString code, unsigned position,
151 AST::Statement *node)
155 _position = position;
160 unsigned startOfStatement = node->firstSourceLocation().begin() - _position;
161 unsigned endOfStatement = node->lastSourceLocation().end() - _position;
163 _writer->replace(startOfStatement, 0, QLatin1String("(function ") + QString::fromUtf8(_name) + QLatin1String("() { "));
164 _writer->replace(endOfStatement, 0, QLatin1String(" })"));
167 qWarning() << "=============================================================";
168 qWarning() << "Rewrote:";
169 qWarning() << qPrintable(code);
176 qWarning() << qPrintable(code);
177 qWarning() << "=============================================================";
183 bool RewriteBinding::visit(AST::Block *ast)
185 for (AST::StatementList *it = ast->statements; it; it = it->next) {
187 // we need to rewrite only the last statement of a block.
188 accept(it->statement);
195 bool RewriteBinding::visit(AST::ExpressionStatement *ast)
198 unsigned startOfExpressionStatement = ast->firstSourceLocation().begin() - _position;
199 _writer->replace(startOfExpressionStatement, 0, QLatin1String("return "));
205 bool RewriteBinding::visit(AST::DoWhileStatement *)
211 void RewriteBinding::endVisit(AST::DoWhileStatement *)
216 bool RewriteBinding::visit(AST::WhileStatement *)
222 void RewriteBinding::endVisit(AST::WhileStatement *)
227 bool RewriteBinding::visit(AST::ForStatement *)
233 void RewriteBinding::endVisit(AST::ForStatement *)
238 bool RewriteBinding::visit(AST::LocalForStatement *)
244 void RewriteBinding::endVisit(AST::LocalForStatement *)
249 bool RewriteBinding::visit(AST::ForEachStatement *)
255 void RewriteBinding::endVisit(AST::ForEachStatement *)
260 bool RewriteBinding::visit(AST::LocalForEachStatement *)
266 void RewriteBinding::endVisit(AST::LocalForEachStatement *)
271 } // namespace QDeclarativeRewrite