74c408cd21e67a7e618593d91c07bb09d1bfd34d
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativerewrite_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 QtDeclarative 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 QDECLARATIVEREWRITE_P_H
43 #define QDECLARATIVEREWRITE_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/qdeclarativejslexer_p.h>
58 #include <private/qdeclarativejsparser_p.h>
59 #include <private/qdeclarativejsmemorypool_p.h>
60
61 QT_BEGIN_NAMESPACE
62
63 namespace QDeclarativeRewrite {
64 using namespace QDeclarativeJS;
65
66 class SharedBindingTester : protected AST::Visitor
67 {
68     bool _sharable;
69 public:
70     bool isSharable(const QString &code);
71     bool isSharable(AST::Node *Node);
72     
73     virtual bool visit(AST::FunctionDeclaration *) { _sharable = false; return false; }
74     virtual bool visit(AST::FunctionExpression *) { _sharable = false; return false; }
75     virtual bool visit(AST::CallExpression *) { _sharable = false; return false; }
76 };
77
78 class RewriteBinding: protected AST::Visitor
79 {
80     unsigned _position;
81     TextWriter *_writer;
82     QString _name;
83     const QString *_code;
84
85 public:
86     QString operator()(const QString &code, bool *ok = 0, bool *sharable = 0);
87     QString operator()(QDeclarativeJS::AST::Node *node, const QString &code, bool *sharable = 0);
88
89     //name of the function:  used for the debugger
90     void setName(const QString &name) { _name = name; }
91
92 protected:
93     using AST::Visitor::visit;
94
95     void accept(AST::Node *node);
96     QString rewrite(QString code, unsigned position, AST::Statement *node);
97     void rewriteCaseStatements(AST::StatementList *statements, bool rewriteTheLastStatement);
98
99     virtual bool visit(AST::StringLiteral *ast);
100     virtual bool visit(AST::Block *ast);
101     virtual bool visit(AST::ExpressionStatement *ast);
102
103     virtual bool visit(AST::DoWhileStatement *ast);
104     virtual void endVisit(AST::DoWhileStatement *ast);
105
106     virtual bool visit(AST::WhileStatement *ast);
107     virtual void endVisit(AST::WhileStatement *ast);
108
109     virtual bool visit(AST::ForStatement *ast);
110     virtual void endVisit(AST::ForStatement *ast);
111
112     virtual bool visit(AST::LocalForStatement *ast);
113     virtual void endVisit(AST::LocalForStatement *ast);
114
115     virtual bool visit(AST::ForEachStatement *ast);
116     virtual void endVisit(AST::ForEachStatement *ast);
117
118     virtual bool visit(AST::LocalForEachStatement *ast);
119     virtual void endVisit(AST::LocalForEachStatement *ast);
120
121     virtual bool visit(AST::CaseBlock *ast);
122
123 private:
124     int _inLoop;
125 };
126
127 class RewriteSignalHandler: protected AST::Visitor
128 {
129     QList<int> _strStarts;
130     QList<int> _strLens;
131     int _position;
132
133 public:
134     QString operator()(QDeclarativeJS::AST::Node *node, const QString &code, const QString &name);
135
136 protected:
137     void rewriteMultilineStrings(QString &code);
138
139     using AST::Visitor::visit;
140     void accept(AST::Node *node);
141     virtual bool visit(AST::StringLiteral *ast);
142 };
143
144 } // namespace QDeclarativeRewrite
145
146 QT_END_NAMESPACE
147
148 #endif // QDECLARATIVEREWRITE_P_H
149