source: trunk/src/declarative/qml/qdeclarativerewrite.cpp@ 883

Last change on this file since 883 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the QtDeclarative module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "private/qdeclarativerewrite_p.h"
43
44#include "private/qdeclarativeglobal_p.h"
45
46#include <QtCore/qdebug.h>
47
48QT_BEGIN_NAMESPACE
49
50DEFINE_BOOL_CONFIG_OPTION(rewriteDump, QML_REWRITE_DUMP);
51
52namespace QDeclarativeRewrite {
53
54bool SharedBindingTester::isSharable(const QString &code)
55{
56 Engine engine;
57 NodePool pool(QString(), &engine);
58 Lexer lexer(&engine);
59 Parser parser(&engine);
60 lexer.setCode(code, 0);
61 parser.parseStatement();
62 if (!parser.statement())
63 return false;
64
65 _sharable = true;
66 AST::Node::acceptChild(parser.statement(), this);
67 return _sharable;
68}
69
70QString RewriteBinding::operator()(const QString &code, bool *ok)
71{
72 Engine engine;
73 NodePool pool(QString(), &engine);
74 Lexer lexer(&engine);
75 Parser parser(&engine);
76 lexer.setCode(code, 0);
77 parser.parseStatement();
78 if (!parser.statement()) {
79 if (ok) *ok = false;
80 return QString();
81 } else {
82 if (ok) *ok = true;
83 }
84 return rewrite(code, 0, parser.statement());
85}
86
87void RewriteBinding::accept(AST::Node *node)
88{
89 AST::Node::acceptChild(node, this);
90}
91
92QString RewriteBinding::rewrite(QString code, unsigned position,
93 AST::Statement *node)
94{
95 TextWriter w;
96 _writer = &w;
97 _position = position;
98 _inLoop = 0;
99
100 accept(node);
101
102 unsigned startOfStatement = node->firstSourceLocation().begin() - _position;
103 unsigned endOfStatement = node->lastSourceLocation().end() - _position;
104
105 _writer->replace(startOfStatement, 0, QLatin1String("(function ") + QString::fromUtf8(_name) + QLatin1String("() { "));
106 _writer->replace(endOfStatement, 0, QLatin1String(" })"));
107
108 if (rewriteDump()) {
109 qWarning() << "=============================================================";
110 qWarning() << "Rewrote:";
111 qWarning() << qPrintable(code);
112 }
113
114 w.write(&code);
115
116 if (rewriteDump()) {
117 qWarning() << "To:";
118 qWarning() << qPrintable(code);
119 qWarning() << "=============================================================";
120 }
121
122 return code;
123}
124
125bool RewriteBinding::visit(AST::Block *ast)
126{
127 for (AST::StatementList *it = ast->statements; it; it = it->next) {
128 if (! it->next) {
129 // we need to rewrite only the last statement of a block.
130 accept(it->statement);
131 }
132 }
133
134 return false;
135}
136
137bool RewriteBinding::visit(AST::ExpressionStatement *ast)
138{
139 if (! _inLoop) {
140 unsigned startOfExpressionStatement = ast->firstSourceLocation().begin() - _position;
141 _writer->replace(startOfExpressionStatement, 0, QLatin1String("return "));
142 }
143
144 return false;
145}
146
147bool RewriteBinding::visit(AST::DoWhileStatement *)
148{
149 ++_inLoop;
150 return true;
151}
152
153void RewriteBinding::endVisit(AST::DoWhileStatement *)
154{
155 --_inLoop;
156}
157
158bool RewriteBinding::visit(AST::WhileStatement *)
159{
160 ++_inLoop;
161 return true;
162}
163
164void RewriteBinding::endVisit(AST::WhileStatement *)
165{
166 --_inLoop;
167}
168
169bool RewriteBinding::visit(AST::ForStatement *)
170{
171 ++_inLoop;
172 return true;
173}
174
175void RewriteBinding::endVisit(AST::ForStatement *)
176{
177 --_inLoop;
178}
179
180bool RewriteBinding::visit(AST::LocalForStatement *)
181{
182 ++_inLoop;
183 return true;
184}
185
186void RewriteBinding::endVisit(AST::LocalForStatement *)
187{
188 --_inLoop;
189}
190
191bool RewriteBinding::visit(AST::ForEachStatement *)
192{
193 ++_inLoop;
194 return true;
195}
196
197void RewriteBinding::endVisit(AST::ForEachStatement *)
198{
199 --_inLoop;
200}
201
202bool RewriteBinding::visit(AST::LocalForEachStatement *)
203{
204 ++_inLoop;
205 return true;
206}
207
208void RewriteBinding::endVisit(AST::LocalForEachStatement *)
209{
210 --_inLoop;
211}
212
213} // namespace QDeclarativeRewrite
214
215QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.