1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef QQMLREWRITE_P_H
43 #define QQMLREWRITE_P_H
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.
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>
64 namespace QQmlRewrite {
65 using namespace QQmlJS;
67 class SharedBindingTester : protected AST::Visitor
71 bool isSharable(const QString &code);
72 bool isSharable(AST::Node *Node);
74 inline virtual bool visit(AST::FunctionDeclaration *);
75 inline virtual bool visit(AST::FunctionExpression *);
76 inline virtual bool visit(AST::IdentifierExpression *);
79 class RewriteBinding: protected AST::Visitor
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);
90 //name of the function: used for the debugger
91 void setName(const QString &name) { _name = name; }
94 using AST::Visitor::visit;
96 void accept(AST::Node *node);
97 QString rewrite(QString code, unsigned position, AST::Statement *node);
98 void rewriteCaseStatements(AST::StatementList *statements, bool rewriteTheLastStatement);
100 virtual bool visit(AST::StringLiteral *ast);
101 virtual bool visit(AST::Block *ast);
102 virtual bool visit(AST::ExpressionStatement *ast);
104 virtual bool visit(AST::DoWhileStatement *ast);
105 virtual void endVisit(AST::DoWhileStatement *ast);
107 virtual bool visit(AST::WhileStatement *ast);
108 virtual void endVisit(AST::WhileStatement *ast);
110 virtual bool visit(AST::ForStatement *ast);
111 virtual void endVisit(AST::ForStatement *ast);
113 virtual bool visit(AST::LocalForStatement *ast);
114 virtual void endVisit(AST::LocalForStatement *ast);
116 virtual bool visit(AST::ForEachStatement *ast);
117 virtual void endVisit(AST::ForEachStatement *ast);
119 virtual bool visit(AST::LocalForEachStatement *ast);
120 virtual void endVisit(AST::LocalForEachStatement *ast);
122 virtual bool visit(AST::CaseBlock *ast);
124 virtual bool visit(AST::FunctionExpression *ast);
125 virtual bool visit(AST::FunctionDeclaration *ast);
131 class RewriteSignalHandler: protected AST::Visitor
134 RewriteSignalHandler();
135 QString operator()(QQmlJS::AST::Node *node, const QString &code, const QString &name,
136 const QString ¶meterString = QString(),
137 const QList<QByteArray> ¶meterNameList = 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> ¶meterNameList = QList<QByteArray>(),
141 const QStringHash<bool> &illegalNames = QStringHash<bool>());
143 enum ParameterAccess {
145 ParametersUnaccessed,
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> ¶meterNameList,
153 const QStringHash<bool> &illegalNames);
155 bool hasParameterError() { return !_error.isEmpty(); }
156 QString parameterError() const { return _error; }
159 void rewriteMultilineStrings(QString &code);
161 using AST::Visitor::visit;
162 void accept(AST::Node *node);
163 virtual bool visit(AST::StringLiteral *ast);
164 virtual bool visit(AST::IdentifierExpression *);
167 QString createParameterString(const QList<QHashedString> ¶meterNameList,
168 const QStringHash<bool> &illegalNames);
171 const QString *_code;
173 QStringHash<int> _parameterNames;
174 QList<QHashedString> _parameterNameList;
175 ParameterAccess _parameterAccess;
176 int _parameterCountForJS;
180 bool SharedBindingTester::visit(AST::FunctionDeclaration *)
186 bool SharedBindingTester::visit(AST::FunctionExpression *)
192 bool SharedBindingTester::visit(AST::IdentifierExpression *e)
194 static const QString evalString = QStringLiteral("eval");
195 if (e->name == evalString)
198 return false; // IdentifierExpression is a leaf node anyway
201 } // namespace QQmlRewrite
205 #endif // QQMLREWRITE_P_H