Doc: Enabling Qt QML linking to Qt Quick.
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlrewrite_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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     bool _safe;
71 public:
72     SharedBindingTester();
73
74     bool isSharable() const { return _sharable; }
75     bool isSafe() const { return isSharable() && _safe; }
76
77     void parse(const QString &code);
78     void parse(AST::Node *Node);
79     
80     virtual bool visit(AST::FunctionDeclaration *);
81     virtual bool visit(AST::FunctionExpression *);
82     virtual bool visit(AST::IdentifierExpression *);
83     virtual bool visit(AST::CallExpression *);
84     virtual bool visit(AST::PostDecrementExpression *);
85     virtual bool visit(AST::PostIncrementExpression *);
86     virtual bool visit(AST::PreDecrementExpression *);
87     virtual bool visit(AST::PreIncrementExpression *);
88     virtual bool visit(AST::BinaryExpression *);
89 };
90
91 class RewriteBinding: protected AST::Visitor
92 {
93     unsigned _position;
94     TextWriter *_writer;
95     QString _name;
96     const QString *_code;
97
98 public:
99     QString operator()(const QString &code, bool *ok = 0, bool *sharable = 0, bool *safe = 0);
100     QString operator()(QQmlJS::AST::Node *node, const QString &code, bool *sharable = 0, bool *safe = 0);
101
102     //name of the function:  used for the debugger
103     void setName(const QString &name) { _name = name; }
104
105 protected:
106     using AST::Visitor::visit;
107
108     void accept(AST::Node *node);
109     QString rewrite(QString code, unsigned position, AST::Statement *node);
110     void rewriteCaseStatements(AST::StatementList *statements, bool rewriteTheLastStatement);
111
112     virtual bool visit(AST::StringLiteral *ast);
113     virtual bool visit(AST::Block *ast);
114     virtual bool visit(AST::ExpressionStatement *ast);
115
116     virtual bool visit(AST::DoWhileStatement *ast);
117     virtual void endVisit(AST::DoWhileStatement *ast);
118
119     virtual bool visit(AST::WhileStatement *ast);
120     virtual void endVisit(AST::WhileStatement *ast);
121
122     virtual bool visit(AST::ForStatement *ast);
123     virtual void endVisit(AST::ForStatement *ast);
124
125     virtual bool visit(AST::LocalForStatement *ast);
126     virtual void endVisit(AST::LocalForStatement *ast);
127
128     virtual bool visit(AST::ForEachStatement *ast);
129     virtual void endVisit(AST::ForEachStatement *ast);
130
131     virtual bool visit(AST::LocalForEachStatement *ast);
132     virtual void endVisit(AST::LocalForEachStatement *ast);
133
134     virtual bool visit(AST::CaseBlock *ast);
135
136     virtual bool visit(AST::FunctionExpression *ast);
137     virtual bool visit(AST::FunctionDeclaration *ast);
138
139 private:
140     int _inLoop;
141 };
142
143 class RewriteSignalHandler: protected AST::Visitor
144 {
145 public:
146     RewriteSignalHandler();
147     QString operator()(QQmlJS::AST::Node *node, const QString &code, const QString &name,
148                        const QString &parameterString = QString(),
149                        const QList<QByteArray> &parameterNameList = QList<QByteArray>(),
150                        const QStringHash<bool> &illegalNames = QStringHash<bool>());
151     QString operator()(const QString &code, const QString &name, bool *ok = 0,
152                        const QList<QByteArray> &parameterNameList = QList<QByteArray>(),
153                        const QStringHash<bool> &illegalNames = QStringHash<bool>());
154
155     enum ParameterAccess {
156         ParametersAccessed,
157         ParametersUnaccessed,
158         UnknownAccess
159     };
160
161     //returns the first n signal parameters that are used in the expression
162     int parameterCountForJS() const { return _parameterCountForJS; }
163     ParameterAccess parameterAccess() const { return _parameterAccess; }
164     QString createParameterString(const QList<QByteArray> &parameterNameList,
165                                   const QStringHash<bool> &illegalNames);
166
167     bool hasParameterError() { return !_error.isEmpty(); }
168     QString parameterError() const { return _error; }
169
170 protected:
171     void rewriteMultilineStrings(QString &code);
172
173     using AST::Visitor::visit;
174     void accept(AST::Node *node);
175     virtual bool visit(AST::StringLiteral *ast);
176     virtual bool visit(AST::IdentifierExpression *);
177
178 private:
179     QString createParameterString(const QList<QHashedString> &parameterNameList,
180                                   const QStringHash<bool> &illegalNames);
181
182     TextWriter *_writer;
183     const QString *_code;
184     int _position;
185     QStringHash<int> _parameterNames;
186     QList<QHashedString> _parameterNameList;
187     ParameterAccess _parameterAccess;
188     int _parameterCountForJS;
189     QString _error;
190 };
191
192 } // namespace QQmlRewrite
193
194 QT_END_NAMESPACE
195
196 #endif // QQMLREWRITE_P_H
197