Update to 5.0.0-beta1
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlrewrite_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QQMLREWRITE_P_H
43 #define QQMLREWRITE_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include <private/textwriter_p.h>
57 #include <private/qqmljslexer_p.h>
58 #include <private/qqmljsparser_p.h>
59 #include <private/qqmljsmemorypool_p.h>
60 #include <private/qhashedstring_p.h>
61
62 QT_BEGIN_NAMESPACE
63
64 namespace QQmlRewrite {
65 using namespace QQmlJS;
66
67 class SharedBindingTester : protected AST::Visitor
68 {
69     bool _sharable;
70 public:
71     bool isSharable(const QString &code);
72     bool isSharable(AST::Node *Node);
73     
74     inline virtual bool visit(AST::FunctionDeclaration *);
75     inline virtual bool visit(AST::FunctionExpression *);
76     inline virtual bool visit(AST::IdentifierExpression *);
77 };
78
79 class RewriteBinding: protected AST::Visitor
80 {
81     unsigned _position;
82     TextWriter *_writer;
83     QString _name;
84     const QString *_code;
85
86 public:
87     QString operator()(const QString &code, bool *ok = 0, bool *sharable = 0);
88     QString operator()(QQmlJS::AST::Node *node, const QString &code, bool *sharable = 0);
89
90     //name of the function:  used for the debugger
91     void setName(const QString &name) { _name = name; }
92
93 protected:
94     using AST::Visitor::visit;
95
96     void accept(AST::Node *node);
97     QString rewrite(QString code, unsigned position, AST::Statement *node);
98     void rewriteCaseStatements(AST::StatementList *statements, bool rewriteTheLastStatement);
99
100     virtual bool visit(AST::StringLiteral *ast);
101     virtual bool visit(AST::Block *ast);
102     virtual bool visit(AST::ExpressionStatement *ast);
103
104     virtual bool visit(AST::DoWhileStatement *ast);
105     virtual void endVisit(AST::DoWhileStatement *ast);
106
107     virtual bool visit(AST::WhileStatement *ast);
108     virtual void endVisit(AST::WhileStatement *ast);
109
110     virtual bool visit(AST::ForStatement *ast);
111     virtual void endVisit(AST::ForStatement *ast);
112
113     virtual bool visit(AST::LocalForStatement *ast);
114     virtual void endVisit(AST::LocalForStatement *ast);
115
116     virtual bool visit(AST::ForEachStatement *ast);
117     virtual void endVisit(AST::ForEachStatement *ast);
118
119     virtual bool visit(AST::LocalForEachStatement *ast);
120     virtual void endVisit(AST::LocalForEachStatement *ast);
121
122     virtual bool visit(AST::CaseBlock *ast);
123
124     virtual bool visit(AST::FunctionExpression *ast);
125     virtual bool visit(AST::FunctionDeclaration *ast);
126
127 private:
128     int _inLoop;
129 };
130
131 class RewriteSignalHandler: protected AST::Visitor
132 {
133 public:
134     RewriteSignalHandler();
135     QString operator()(QQmlJS::AST::Node *node, const QString &code, const QString &name,
136                        const QString &parameterString = QString(),
137                        const QList<QByteArray> &parameterNameList = QList<QByteArray>(),
138                        const QStringHash<bool> &illegalNames = QStringHash<bool>());
139     QString operator()(const QString &code, const QString &name, bool *ok = 0,
140                        const QList<QByteArray> &parameterNameList = QList<QByteArray>(),
141                        const QStringHash<bool> &illegalNames = QStringHash<bool>());
142
143     enum ParameterAccess {
144         ParametersAccessed,
145         ParametersUnaccessed,
146         UnknownAccess
147     };
148
149     //returns the first n signal parameters that are used in the expression
150     int parameterCountForJS() const { return _parameterCountForJS; }
151     ParameterAccess parameterAccess() const { return _parameterAccess; }
152     QString createParameterString(const QList<QByteArray> &parameterNameList,
153                                   const QStringHash<bool> &illegalNames);
154
155     bool hasParameterError() { return !_error.isEmpty(); }
156     QString parameterError() const { return _error; }
157
158 protected:
159     void rewriteMultilineStrings(QString &code);
160
161     using AST::Visitor::visit;
162     void accept(AST::Node *node);
163     virtual bool visit(AST::StringLiteral *ast);
164     virtual bool visit(AST::IdentifierExpression *);
165
166 private:
167     QString createParameterString(const QList<QHashedString> &parameterNameList,
168                                   const QStringHash<bool> &illegalNames);
169
170     TextWriter *_writer;
171     const QString *_code;
172     int _position;
173     QStringHash<int> _parameterNames;
174     QList<QHashedString> _parameterNameList;
175     ParameterAccess _parameterAccess;
176     int _parameterCountForJS;
177     QString _error;
178 };
179
180 bool SharedBindingTester::visit(AST::FunctionDeclaration *)
181 {
182     _sharable = false;
183     return false;
184 }
185
186 bool SharedBindingTester::visit(AST::FunctionExpression *)
187 {
188     _sharable = false;
189     return false;
190 }
191
192 bool SharedBindingTester::visit(AST::IdentifierExpression *e)
193 {
194     static const QString evalString = QStringLiteral("eval");
195     if (e->name == evalString)
196         _sharable = false;
197
198     return false; // IdentifierExpression is a leaf node anyway
199 }
200
201 } // namespace QQmlRewrite
202
203 QT_END_NAMESPACE
204
205 #endif // QQMLREWRITE_P_H
206