Qt 4.8
Public Functions | Protected Functions | Properties | List of all members
QDeclarativeRewrite::RewriteBinding Class Reference

#include <qdeclarativerewrite_p.h>

Inheritance diagram for QDeclarativeRewrite::RewriteBinding:
QDeclarativeJS::AST::Visitor

Public Functions

QString operator() (const QString &code, bool *ok=0, bool *sharable=0)
 
QString operator() (QDeclarativeJS::AST::Node *node, const QString &code, bool *sharable=0)
 
void setName (const QByteArray &name)
 

Protected Functions

void accept (AST::Node *node)
 
virtual void endVisit (AST::DoWhileStatement *ast)
 
virtual void endVisit (AST::WhileStatement *ast)
 
virtual void endVisit (AST::ForStatement *ast)
 
virtual void endVisit (AST::LocalForStatement *ast)
 
virtual void endVisit (AST::ForEachStatement *ast)
 
virtual void endVisit (AST::LocalForEachStatement *ast)
 
QString rewrite (QString code, unsigned position, AST::Statement *node)
 
virtual bool visit (AST::Block *ast)
 
virtual bool visit (AST::ExpressionStatement *ast)
 
virtual bool visit (AST::DoWhileStatement *ast)
 
virtual bool visit (AST::WhileStatement *ast)
 
virtual bool visit (AST::ForStatement *ast)
 
virtual bool visit (AST::LocalForStatement *ast)
 
virtual bool visit (AST::ForEachStatement *ast)
 
virtual bool visit (AST::LocalForEachStatement *ast)
 
- Protected Functions inherited from QDeclarativeJS::AST::Visitor
virtual void endVisit (UiProgram *)
 
virtual void endVisit (UiImportList *)
 
virtual void endVisit (UiImport *)
 
virtual void endVisit (UiPublicMember *)
 
virtual void endVisit (UiSourceElement *)
 
virtual void endVisit (UiObjectDefinition *)
 
virtual void endVisit (UiObjectInitializer *)
 
virtual void endVisit (UiObjectBinding *)
 
virtual void endVisit (UiScriptBinding *)
 
virtual void endVisit (UiArrayBinding *)
 
virtual void endVisit (UiObjectMemberList *)
 
virtual void endVisit (UiArrayMemberList *)
 
virtual void endVisit (UiQualifiedId *)
 
virtual void endVisit (UiSignature *)
 
virtual void endVisit (UiFormalList *)
 
virtual void endVisit (UiFormal *)
 
virtual void endVisit (ThisExpression *)
 
virtual void endVisit (IdentifierExpression *)
 
virtual void endVisit (NullExpression *)
 
virtual void endVisit (TrueLiteral *)
 
virtual void endVisit (FalseLiteral *)
 
virtual void endVisit (StringLiteral *)
 
virtual void endVisit (NumericLiteral *)
 
virtual void endVisit (RegExpLiteral *)
 
virtual void endVisit (ArrayLiteral *)
 
virtual void endVisit (ObjectLiteral *)
 
virtual void endVisit (ElementList *)
 
virtual void endVisit (Elision *)
 
virtual void endVisit (PropertyNameAndValueList *)
 
virtual void endVisit (NestedExpression *)
 
virtual void endVisit (IdentifierPropertyName *)
 
virtual void endVisit (StringLiteralPropertyName *)
 
virtual void endVisit (NumericLiteralPropertyName *)
 
virtual void endVisit (ArrayMemberExpression *)
 
virtual void endVisit (FieldMemberExpression *)
 
virtual void endVisit (NewMemberExpression *)
 
virtual void endVisit (NewExpression *)
 
virtual void endVisit (CallExpression *)
 
virtual void endVisit (ArgumentList *)
 
virtual void endVisit (PostIncrementExpression *)
 
virtual void endVisit (PostDecrementExpression *)
 
virtual void endVisit (DeleteExpression *)
 
virtual void endVisit (VoidExpression *)
 
virtual void endVisit (TypeOfExpression *)
 
virtual void endVisit (PreIncrementExpression *)
 
virtual void endVisit (PreDecrementExpression *)
 
virtual void endVisit (UnaryPlusExpression *)
 
virtual void endVisit (UnaryMinusExpression *)
 
virtual void endVisit (TildeExpression *)
 
virtual void endVisit (NotExpression *)
 
virtual void endVisit (BinaryExpression *)
 
virtual void endVisit (ConditionalExpression *)
 
virtual void endVisit (Expression *)
 
virtual void endVisit (Block *)
 
virtual void endVisit (StatementList *)
 
virtual void endVisit (VariableStatement *)
 
virtual void endVisit (VariableDeclarationList *)
 
virtual void endVisit (VariableDeclaration *)
 
virtual void endVisit (EmptyStatement *)
 
virtual void endVisit (ExpressionStatement *)
 
virtual void endVisit (IfStatement *)
 
virtual void endVisit (ContinueStatement *)
 
virtual void endVisit (BreakStatement *)
 
virtual void endVisit (ReturnStatement *)
 
virtual void endVisit (WithStatement *)
 
virtual void endVisit (SwitchStatement *)
 
virtual void endVisit (CaseBlock *)
 
virtual void endVisit (CaseClauses *)
 
virtual void endVisit (CaseClause *)
 
virtual void endVisit (DefaultClause *)
 
virtual void endVisit (LabelledStatement *)
 
virtual void endVisit (ThrowStatement *)
 
virtual void endVisit (TryStatement *)
 
virtual void endVisit (Catch *)
 
virtual void endVisit (Finally *)
 
virtual void endVisit (FunctionDeclaration *)
 
virtual void endVisit (FunctionExpression *)
 
virtual void endVisit (FormalParameterList *)
 
virtual void endVisit (FunctionBody *)
 
virtual void endVisit (Program *)
 
virtual void endVisit (SourceElements *)
 
virtual void endVisit (FunctionSourceElement *)
 
virtual void endVisit (StatementSourceElement *)
 
virtual void endVisit (DebuggerStatement *)
 
virtual void postVisit (Node *)
 
virtual bool preVisit (Node *)
 
virtual bool visit (UiProgram *)
 
virtual bool visit (UiImportList *)
 
virtual bool visit (UiImport *)
 
virtual bool visit (UiPublicMember *)
 
virtual bool visit (UiSourceElement *)
 
virtual bool visit (UiObjectDefinition *)
 
virtual bool visit (UiObjectInitializer *)
 
virtual bool visit (UiObjectBinding *)
 
virtual bool visit (UiScriptBinding *)
 
virtual bool visit (UiArrayBinding *)
 
virtual bool visit (UiObjectMemberList *)
 
virtual bool visit (UiArrayMemberList *)
 
virtual bool visit (UiQualifiedId *)
 
virtual bool visit (UiSignature *)
 
virtual bool visit (UiFormalList *)
 
virtual bool visit (UiFormal *)
 
virtual bool visit (ThisExpression *)
 
virtual bool visit (IdentifierExpression *)
 
virtual bool visit (NullExpression *)
 
virtual bool visit (TrueLiteral *)
 
virtual bool visit (FalseLiteral *)
 
virtual bool visit (StringLiteral *)
 
virtual bool visit (NumericLiteral *)
 
virtual bool visit (RegExpLiteral *)
 
virtual bool visit (ArrayLiteral *)
 
virtual bool visit (ObjectLiteral *)
 
virtual bool visit (ElementList *)
 
virtual bool visit (Elision *)
 
virtual bool visit (PropertyNameAndValueList *)
 
virtual bool visit (NestedExpression *)
 
virtual bool visit (IdentifierPropertyName *)
 
virtual bool visit (StringLiteralPropertyName *)
 
virtual bool visit (NumericLiteralPropertyName *)
 
virtual bool visit (ArrayMemberExpression *)
 
virtual bool visit (FieldMemberExpression *)
 
virtual bool visit (NewMemberExpression *)
 
virtual bool visit (NewExpression *)
 
virtual bool visit (CallExpression *)
 
virtual bool visit (ArgumentList *)
 
virtual bool visit (PostIncrementExpression *)
 
virtual bool visit (PostDecrementExpression *)
 
virtual bool visit (DeleteExpression *)
 
virtual bool visit (VoidExpression *)
 
virtual bool visit (TypeOfExpression *)
 
virtual bool visit (PreIncrementExpression *)
 
virtual bool visit (PreDecrementExpression *)
 
virtual bool visit (UnaryPlusExpression *)
 
virtual bool visit (UnaryMinusExpression *)
 
virtual bool visit (TildeExpression *)
 
virtual bool visit (NotExpression *)
 
virtual bool visit (BinaryExpression *)
 
virtual bool visit (ConditionalExpression *)
 
virtual bool visit (Expression *)
 
virtual bool visit (StatementList *)
 
virtual bool visit (VariableStatement *)
 
virtual bool visit (VariableDeclarationList *)
 
virtual bool visit (VariableDeclaration *)
 
virtual bool visit (EmptyStatement *)
 
virtual bool visit (IfStatement *)
 
virtual bool visit (ContinueStatement *)
 
virtual bool visit (BreakStatement *)
 
virtual bool visit (ReturnStatement *)
 
virtual bool visit (WithStatement *)
 
virtual bool visit (SwitchStatement *)
 
virtual bool visit (CaseBlock *)
 
virtual bool visit (CaseClauses *)
 
virtual bool visit (CaseClause *)
 
virtual bool visit (DefaultClause *)
 
virtual bool visit (LabelledStatement *)
 
virtual bool visit (ThrowStatement *)
 
virtual bool visit (TryStatement *)
 
virtual bool visit (Catch *)
 
virtual bool visit (Finally *)
 
virtual bool visit (FunctionDeclaration *)
 
virtual bool visit (FunctionExpression *)
 
virtual bool visit (FormalParameterList *)
 
virtual bool visit (FunctionBody *)
 
virtual bool visit (Program *)
 
virtual bool visit (SourceElements *)
 
virtual bool visit (FunctionSourceElement *)
 
virtual bool visit (StatementSourceElement *)
 
virtual bool visit (DebuggerStatement *)
 
 Visitor ()
 
virtual ~Visitor ()
 

Properties

int _inLoop
 
QByteArray _name
 
unsigned _position
 
TextWriter_writer
 

Detailed Description

Definition at line 78 of file qdeclarativerewrite_p.h.

Functions

◆ accept()

void QDeclarativeRewrite::RewriteBinding::accept ( AST::Node node)
protected

Definition at line 145 of file qdeclarativerewrite.cpp.

146 {
147  AST::Node::acceptChild(node, this);
148 }

◆ endVisit() [1/6]

void QDeclarativeRewrite::RewriteBinding::endVisit ( AST::DoWhileStatement ast)
protectedvirtual

Reimplemented from QDeclarativeJS::AST::Visitor.

Definition at line 211 of file qdeclarativerewrite.cpp.

◆ endVisit() [2/6]

void QDeclarativeRewrite::RewriteBinding::endVisit ( AST::WhileStatement ast)
protectedvirtual

Reimplemented from QDeclarativeJS::AST::Visitor.

Definition at line 222 of file qdeclarativerewrite.cpp.

◆ endVisit() [3/6]

void QDeclarativeRewrite::RewriteBinding::endVisit ( AST::ForStatement ast)
protectedvirtual

Reimplemented from QDeclarativeJS::AST::Visitor.

Definition at line 233 of file qdeclarativerewrite.cpp.

◆ endVisit() [4/6]

void QDeclarativeRewrite::RewriteBinding::endVisit ( AST::LocalForStatement ast)
protectedvirtual

Reimplemented from QDeclarativeJS::AST::Visitor.

Definition at line 244 of file qdeclarativerewrite.cpp.

◆ endVisit() [5/6]

void QDeclarativeRewrite::RewriteBinding::endVisit ( AST::ForEachStatement ast)
protectedvirtual

Reimplemented from QDeclarativeJS::AST::Visitor.

Definition at line 255 of file qdeclarativerewrite.cpp.

◆ endVisit() [6/6]

void QDeclarativeRewrite::RewriteBinding::endVisit ( AST::LocalForEachStatement ast)
protectedvirtual

Reimplemented from QDeclarativeJS::AST::Visitor.

Definition at line 266 of file qdeclarativerewrite.cpp.

◆ operator()() [1/2]

QString QDeclarativeRewrite::RewriteBinding::operator() ( const QString code,
bool *  ok = 0,
bool *  sharable = 0 
)

Definition at line 75 of file qdeclarativerewrite.cpp.

76 {
77  Engine engine;
78  NodePool pool(QString(), &engine);
79  Lexer lexer(&engine);
80  Parser parser(&engine);
81  lexer.setCode(code, 0);
82  parser.parseStatement();
83  if (!parser.statement()) {
84  if (ok) *ok = false;
85  return QString();
86  } else {
87  if (ok) *ok = true;
88  if (sharable) {
89  SharedBindingTester tester;
90  *sharable = tester.isSharable(parser.statement());
91  }
92  }
93  return rewrite(code, 0, parser.statement());
94 }
The QString class provides a Unicode character string.
Definition: qstring.h:83
QString rewrite(QString code, unsigned position, AST::Statement *node)

◆ operator()() [2/2]

QString QDeclarativeRewrite::RewriteBinding::operator() ( QDeclarativeJS::AST::Node node,
const QString code,
bool *  sharable = 0 
)

Definition at line 96 of file qdeclarativerewrite.cpp.

97 {
98  if (!node)
99  return code;
100 
101  if (sharable) {
102  SharedBindingTester tester;
103  *sharable = tester.isSharable(node);
104  }
105 
107  QDeclarativeJS::AST::Statement *statement = node->statementCast();
108  if(!expression && !statement)
109  return code;
110 
111  TextWriter w;
112  _writer = &w;
113  _position = expression ? expression->firstSourceLocation().begin() : statement->firstSourceLocation().begin();
114  _inLoop = 0;
115 
116  accept(node);
117 
118  unsigned startOfStatement = 0;
119  unsigned endOfStatement = (expression ? expression->lastSourceLocation().end() : statement->lastSourceLocation().end()) - _position;
120 
121  QString startString = QLatin1String("(function ") + QString::fromUtf8(_name) + QLatin1String("() { ");
122  if (expression)
123  startString += QLatin1String("return ");
124  _writer->replace(startOfStatement, 0, startString);
125  _writer->replace(endOfStatement, 0, QLatin1String(" })"));
126 
127  if (rewriteDump()) {
128  qWarning() << "=============================================================";
129  qWarning() << "Rewrote:";
130  qWarning() << qPrintable(code);
131  }
132 
133  QString codeCopy = code;
134  w.write(&codeCopy);
135 
136  if (rewriteDump()) {
137  qWarning() << "To:";
138  qWarning() << qPrintable(code);
139  qWarning() << "=============================================================";
140  }
141 
142  return codeCopy;
143 }
virtual SourceLocation firstSourceLocation() const =0
virtual Statement * statementCast()
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual SourceLocation lastSourceLocation() const =0
virtual SourceLocation firstSourceLocation() const =0
virtual SourceLocation lastSourceLocation() const =0
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
void replace(int pos, int length, const QString &replacement)
Definition: textwriter.cpp:90
Q_CORE_EXPORT void qWarning(const char *,...)
virtual ExpressionNode * expressionCast()
#define qPrintable(string)
Definition: qglobal.h:1750

◆ rewrite()

QString QDeclarativeRewrite::RewriteBinding::rewrite ( QString  code,
unsigned  position,
AST::Statement node 
)
protected

Definition at line 150 of file qdeclarativerewrite.cpp.

152 {
153  TextWriter w;
154  _writer = &w;
156  _inLoop = 0;
157 
158  accept(node);
159 
160  unsigned startOfStatement = node->firstSourceLocation().begin() - _position;
161  unsigned endOfStatement = node->lastSourceLocation().end() - _position;
162 
163  _writer->replace(startOfStatement, 0, QLatin1String("(function ") + QString::fromUtf8(_name) + QLatin1String("() { "));
164  _writer->replace(endOfStatement, 0, QLatin1String(" })"));
165 
166  if (rewriteDump()) {
167  qWarning() << "=============================================================";
168  qWarning() << "Rewrote:";
169  qWarning() << qPrintable(code);
170  }
171 
172  w.write(&code);
173 
174  if (rewriteDump()) {
175  qWarning() << "To:";
176  qWarning() << qPrintable(code);
177  qWarning() << "=============================================================";
178  }
179 
180  return code;
181 }
static qreal position(QGraphicsObject *item, QDeclarativeAnchorLine::AnchorLine anchorLine)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
void replace(int pos, int length, const QString &replacement)
Definition: textwriter.cpp:90
Q_CORE_EXPORT void qWarning(const char *,...)
#define qPrintable(string)
Definition: qglobal.h:1750

◆ setName()

void QDeclarativeRewrite::RewriteBinding::setName ( const QByteArray name)
inline

◆ visit() [1/8]

bool QDeclarativeRewrite::RewriteBinding::visit ( AST::Block ast)
protectedvirtual

Reimplemented from QDeclarativeJS::AST::Visitor.

Definition at line 183 of file qdeclarativerewrite.cpp.

184 {
185  for (AST::StatementList *it = ast->statements; it; it = it->next) {
186  if (! it->next) {
187  // we need to rewrite only the last statement of a block.
188  accept(it->statement);
189  }
190  }
191 
192  return false;
193 }
#define it(className, varName)

◆ visit() [2/8]

bool QDeclarativeRewrite::RewriteBinding::visit ( AST::ExpressionStatement ast)
protectedvirtual

Reimplemented from QDeclarativeJS::AST::Visitor.

Definition at line 195 of file qdeclarativerewrite.cpp.

196 {
197  if (! _inLoop) {
198  unsigned startOfExpressionStatement = ast->firstSourceLocation().begin() - _position;
199  _writer->replace(startOfExpressionStatement, 0, QLatin1String("return "));
200  }
201 
202  return false;
203 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void replace(int pos, int length, const QString &replacement)
Definition: textwriter.cpp:90

◆ visit() [3/8]

bool QDeclarativeRewrite::RewriteBinding::visit ( AST::DoWhileStatement ast)
protectedvirtual

Reimplemented from QDeclarativeJS::AST::Visitor.

Definition at line 205 of file qdeclarativerewrite.cpp.

206 {
207  ++_inLoop;
208  return true;
209 }

◆ visit() [4/8]

bool QDeclarativeRewrite::RewriteBinding::visit ( AST::WhileStatement ast)
protectedvirtual

Reimplemented from QDeclarativeJS::AST::Visitor.

Definition at line 216 of file qdeclarativerewrite.cpp.

217 {
218  ++_inLoop;
219  return true;
220 }

◆ visit() [5/8]

bool QDeclarativeRewrite::RewriteBinding::visit ( AST::ForStatement ast)
protectedvirtual

Reimplemented from QDeclarativeJS::AST::Visitor.

Definition at line 227 of file qdeclarativerewrite.cpp.

228 {
229  ++_inLoop;
230  return true;
231 }

◆ visit() [6/8]

bool QDeclarativeRewrite::RewriteBinding::visit ( AST::LocalForStatement ast)
protectedvirtual

Reimplemented from QDeclarativeJS::AST::Visitor.

Definition at line 238 of file qdeclarativerewrite.cpp.

239 {
240  ++_inLoop;
241  return true;
242 }

◆ visit() [7/8]

bool QDeclarativeRewrite::RewriteBinding::visit ( AST::ForEachStatement ast)
protectedvirtual

Reimplemented from QDeclarativeJS::AST::Visitor.

Definition at line 249 of file qdeclarativerewrite.cpp.

250 {
251  ++_inLoop;
252  return true;
253 }

◆ visit() [8/8]

bool QDeclarativeRewrite::RewriteBinding::visit ( AST::LocalForEachStatement ast)
protectedvirtual

Reimplemented from QDeclarativeJS::AST::Visitor.

Definition at line 260 of file qdeclarativerewrite.cpp.

261 {
262  ++_inLoop;
263  return true;
264 }

Properties

◆ _inLoop

int QDeclarativeRewrite::RewriteBinding::_inLoop
private

Definition at line 119 of file qdeclarativerewrite_p.h.

◆ _name

QByteArray QDeclarativeRewrite::RewriteBinding::_name
private

Definition at line 82 of file qdeclarativerewrite_p.h.

◆ _position

unsigned QDeclarativeRewrite::RewriteBinding::_position
private

Definition at line 80 of file qdeclarativerewrite_p.h.

◆ _writer

TextWriter* QDeclarativeRewrite::RewriteBinding::_writer
private

Definition at line 81 of file qdeclarativerewrite_p.h.


The documentation for this class was generated from the following files: