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 #include "qqmlrewrite_p.h"
44 #include <private/qqmlglobal_p.h>
46 #include <QtCore/qdebug.h>
50 DEFINE_BOOL_CONFIG_OPTION(rewriteDump, QML_REWRITE_DUMP)
52 namespace QQmlRewrite {
54 static void rewriteStringLiteral(AST::StringLiteral *ast, const QString *code, int startPosition, TextWriter *writer)
56 const unsigned position = ast->firstSourceLocation().begin() - startPosition + 1;
57 const unsigned length = ast->literalToken.length - 2;
58 const QStringRef spell = code->midRef(position, length);
59 const int end = spell.size();
63 const QChar ch = spell.at(index++);
65 if (index < end && ch == QLatin1Char('\\')) {
68 // skip a possibly empty sequence of \r characters
69 while (pos < end && spell.at(pos) == QLatin1Char('\r'))
72 if (pos < end && spell.at(pos) == QLatin1Char('\n')) {
73 // This is a `\' followed by a newline terminator.
74 // In this case there's nothing to replace. We keep the code
75 // as it is and we resume the searching.
76 index = pos + 1; // refresh the index
78 } else if (ch == QLatin1Char('\r') || ch == QLatin1Char('\n')) {
79 const QString sep = ch == QLatin1Char('\r') ? QLatin1String("\\r") : QLatin1String("\\n");
80 const int pos = index - 1;
83 while (index < end && spell.at(index) == ch) {
88 writer->replace(position + pos, index - pos, s);
93 bool SharedBindingTester::isSharable(const QString &code)
97 Parser parser(&engine);
98 lexer.setCode(code, 0);
99 parser.parseStatement();
100 if (!parser.statement())
103 return isSharable(parser.statement());
106 bool SharedBindingTester::isSharable(AST::Node *node)
109 AST::Node::acceptChild(node, this);
113 QString RewriteBinding::operator()(const QString &code, bool *ok, bool *sharable)
116 Lexer lexer(&engine);
117 Parser parser(&engine);
118 lexer.setCode(code, 0);
119 parser.parseStatement();
120 if (!parser.statement()) {
126 SharedBindingTester tester;
127 *sharable = tester.isSharable(parser.statement());
130 return rewrite(code, 0, parser.statement());
133 QString RewriteBinding::operator()(QQmlJS::AST::Node *node, const QString &code, bool *sharable)
139 SharedBindingTester tester;
140 *sharable = tester.isSharable(node);
143 QQmlJS::AST::ExpressionNode *expression = node->expressionCast();
144 QQmlJS::AST::Statement *statement = node->statementCast();
145 if(!expression && !statement)
150 _position = expression ? expression->firstSourceLocation().begin() : statement->firstSourceLocation().begin();
156 unsigned startOfStatement = 0;
157 unsigned endOfStatement = (expression ? expression->lastSourceLocation().end() : statement->lastSourceLocation().end()) - _position;
159 QString startString = QLatin1String("(function ") + _name + QLatin1String("() { ");
161 startString += QLatin1String("return ");
162 _writer->replace(startOfStatement, 0, startString);
163 _writer->replace(endOfStatement, 0, QLatin1String(" })"));
166 qWarning() << "=============================================================";
167 qWarning() << "Rewrote:";
168 qWarning() << qPrintable(code);
171 QString codeCopy = code;
176 qWarning() << qPrintable(codeCopy);
177 qWarning() << "=============================================================";
183 void RewriteBinding::accept(AST::Node *node)
185 AST::Node::acceptChild(node, this);
188 QString RewriteBinding::rewrite(QString code, unsigned position,
189 AST::Statement *node)
193 _position = position;
199 unsigned startOfStatement = node->firstSourceLocation().begin() - _position;
200 unsigned endOfStatement = node->lastSourceLocation().end() - _position;
202 _writer->replace(startOfStatement, 0, QLatin1String("(function ") + _name + QLatin1String("() { "));
203 _writer->replace(endOfStatement, 0, QLatin1String(" })"));
206 qWarning() << "=============================================================";
207 qWarning() << "Rewrote:";
208 qWarning() << qPrintable(code);
215 qWarning() << qPrintable(code);
216 qWarning() << "=============================================================";
222 bool RewriteBinding::visit(AST::Block *ast)
224 for (AST::StatementList *it = ast->statements; it; it = it->next) {
226 // we need to rewrite only the last statement of a block.
227 accept(it->statement);
234 bool RewriteBinding::visit(AST::ExpressionStatement *ast)
237 unsigned startOfExpressionStatement = ast->firstSourceLocation().begin() - _position;
238 _writer->replace(startOfExpressionStatement, 0, QLatin1String("return "));
244 bool RewriteBinding::visit(AST::StringLiteral *ast)
246 rewriteStringLiteral(ast, _code, _position, _writer);
250 bool RewriteBinding::visit(AST::DoWhileStatement *)
256 void RewriteBinding::endVisit(AST::DoWhileStatement *)
261 bool RewriteBinding::visit(AST::WhileStatement *)
267 void RewriteBinding::endVisit(AST::WhileStatement *)
272 bool RewriteBinding::visit(AST::ForStatement *)
278 void RewriteBinding::endVisit(AST::ForStatement *)
283 bool RewriteBinding::visit(AST::LocalForStatement *)
289 void RewriteBinding::endVisit(AST::LocalForStatement *)
294 bool RewriteBinding::visit(AST::ForEachStatement *)
300 void RewriteBinding::endVisit(AST::ForEachStatement *)
305 bool RewriteBinding::visit(AST::LocalForEachStatement *)
311 void RewriteBinding::endVisit(AST::LocalForEachStatement *)
316 bool RewriteBinding::visit(AST::FunctionExpression *)
321 bool RewriteBinding::visit(AST::FunctionDeclaration *)
326 bool RewriteBinding::visit(AST::CaseBlock *ast)
328 // Process the initial sequence of the case clauses.
329 for (AST::CaseClauses *it = ast->clauses; it; it = it->next) {
330 // Return the value of the last statement in the block, if this is the last `case clause'
331 // of the switch statement.
332 bool returnTheValueOfLastStatement = (it->next == 0) && (ast->defaultClause == 0) && (ast->moreClauses == 0);
334 if (AST::CaseClause *clause = it->clause) {
335 accept(clause->expression);
336 rewriteCaseStatements(clause->statements, returnTheValueOfLastStatement);
340 // Process the default case clause
341 if (ast->defaultClause) {
342 // Return the value of the last statement in the block, if this is the last `case clause'
343 // of the switch statement.
344 bool rewriteTheLastStatement = (ast->moreClauses == 0);
346 rewriteCaseStatements(ast->defaultClause->statements, rewriteTheLastStatement);
349 // Process trailing `case clauses'
350 for (AST::CaseClauses *it = ast->moreClauses; it; it = it->next) {
351 // Return the value of the last statement in the block, if this is the last `case clause'
352 // of the switch statement.
353 bool returnTheValueOfLastStatement = (it->next == 0);
355 if (AST::CaseClause *clause = it->clause) {
356 accept(clause->expression);
357 rewriteCaseStatements(clause->statements, returnTheValueOfLastStatement);
364 void RewriteBinding::rewriteCaseStatements(AST::StatementList *statements, bool rewriteTheLastStatement)
366 for (AST::StatementList *it = statements; it; it = it->next) {
367 if (it->next && AST::cast<AST::BreakStatement *>(it->next->statement) != 0) {
368 // The value of the first statement followed by a `break'.
369 accept(it->statement);
371 } else if (!it->next) {
372 if (rewriteTheLastStatement)
373 accept(it->statement);
374 else if (AST::Block *block = AST::cast<AST::Block *>(it->statement))
375 rewriteCaseStatements(block->statements, rewriteTheLastStatement);
382 RewriteSignalHandler performs two different types of rewrites, depending on what information
385 When the target object is known, the rewriter can be provided a list of parameter names (and an
386 optional preconstructed parameter string), which allows us to:
387 1. Check whether the parameters are used
388 2. Rewrite with the parameters included in the rewrite
389 When this information is not available, we do a more generic rewrite, and rely on the expression
390 to perform a second rewrite with the parameter information (using createParameterString)
391 once the target object is known.
393 RewriteSignalHandler::RewriteSignalHandler()
397 , _parameterAccess(UnknownAccess)
398 , _parameterCountForJS(0)
402 void RewriteSignalHandler::accept(AST::Node *node)
404 AST::Node::acceptChild(node, this);
407 bool RewriteSignalHandler::visit(AST::StringLiteral *ast)
409 rewriteStringLiteral(ast, _code, _position, _writer);
413 //if we never make use of the signal parameters in our expression,
414 //there is no need to provide them
415 bool RewriteSignalHandler::visit(AST::IdentifierExpression *e)
417 //optimization: don't need to compare strings if a parameter has already been marked as used.
418 if (_parameterAccess == ParametersAccessed)
421 static const QString argumentsString = QStringLiteral("arguments");
422 if (_parameterNames.contains(e->name) || e->name == argumentsString)
423 _parameterAccess = ParametersAccessed;
427 static QString unnamed_error_string(QLatin1String(QT_TR_NOOP("Signal uses unnamed parameter followed by named parameter.")));
428 static QString global_error_string(QLatin1String(QT_TR_NOOP("Signal parameter \"%1\" hides global variable.")));
430 #define EXIT_ON_ERROR(error) \
436 //create a parameter string which can be inserted into a generic rewrite
437 QString RewriteSignalHandler::createParameterString(const QList<QByteArray> ¶meterNameList,
438 const QStringHash<bool> &illegalNames)
440 QList<QHashedString> hashedParameterNameList;
441 for (int i = 0; i < parameterNameList.count(); ++i)
442 hashedParameterNameList.append(QString::fromUtf8(parameterNameList.at(i).constData()));
444 return createParameterString(hashedParameterNameList, illegalNames);
447 QString RewriteSignalHandler::createParameterString(const QList<QHashedString> ¶meterNameList,
448 const QStringHash<bool> &illegalNames)
451 bool unnamedParam = false;
452 for (int i = 0; i < parameterNameList.count(); ++i) {
453 const QHashedString ¶m = parameterNameList.at(i);
456 else if (unnamedParam)
457 EXIT_ON_ERROR(unnamed_error_string)
458 else if (illegalNames.contains(param))
459 EXIT_ON_ERROR(global_error_string.arg(param))
460 ++_parameterCountForJS;
462 if (i < parameterNameList.count()-1)
463 parameters += QStringLiteral(",");
465 if (parameters.endsWith(QLatin1Char(',')))
466 parameters.resize(parameters.length() - 1);
471 If \a parameterString is provided, use \a parameterNameList to test whether the
472 parameters are used in the body of the function
473 * if unused, the rewrite will not include parameters, else
474 * if used, the rewrite will use \a parameterString
475 If \a parameterString is not provided, it is constructed from \a parameterNameList
478 QString RewriteSignalHandler::operator()(QQmlJS::AST::Node *node, const QString &code, const QString &name,
479 const QString ¶meterString,
480 const QList<QByteArray> ¶meterNameList,
481 const QStringHash<bool> &illegalNames)
484 qWarning() << "=============================================================";
485 qWarning() << "Rewrote:";
486 qWarning() << qPrintable(code);
489 bool hasParameterString = !parameterString.isEmpty();
491 QQmlJS::AST::ExpressionNode *expression = node->expressionCast();
492 QQmlJS::AST::Statement *statement = node->statementCast();
493 if (!expression && !statement)
496 if (!parameterNameList.isEmpty()) {
497 for (int i = 0; i < parameterNameList.count(); ++i) {
498 QHashedString param(QString::fromUtf8(parameterNameList.at(i).constData()));
499 _parameterNames.insert(param, i);
500 if (!hasParameterString)
501 _parameterNameList.append(param);
504 //this is set to Unaccessed here, and will be set to Accessed
505 //if we detect that a parameter has been used
506 _parameterAccess = ParametersUnaccessed;
513 _position = expression ? expression->firstSourceLocation().begin() : statement->firstSourceLocation().begin();
516 QString rewritten = code;
519 QString parameters = (_parameterAccess == ParametersUnaccessed) ? QString()
520 : hasParameterString ? parameterString
521 : createParameterString(_parameterNameList, illegalNames);
522 rewritten = QStringLiteral("(function ") + name + QStringLiteral("(") + parameters + QStringLiteral(") { ") + rewritten + QStringLiteral(" })");
526 qWarning() << qPrintable(rewritten);
527 qWarning() << "=============================================================";
533 QString RewriteSignalHandler::operator()(const QString &code, const QString &name, bool *ok,
534 const QList<QByteArray> ¶meterNameList,
535 const QStringHash<bool> &illegalNames)
538 Lexer lexer(&engine);
539 Parser parser(&engine);
540 lexer.setCode(code, 0);
541 parser.parseStatement();
542 if (!parser.statement()) {
547 return operator()(parser.statement(), code, name, QString(), parameterNameList, illegalNames);
550 } // namespace QQmlRewrite