source: trunk/src/script/qscriptcompiler_p.h@ 318

Last change on this file since 318 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 11.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtScript module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QSCRIPTCOMPILER_P_H
43#define QSCRIPTCOMPILER_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 <QtCore/QMap>
57
58#ifndef QT_NO_SCRIPT
59
60#include <QtCore/QVector>
61
62#include "qscriptastvisitor_p.h"
63#include "qscriptasm_p.h"
64
65QT_BEGIN_NAMESPACE
66
67class QScriptEnginePrivate;
68
69namespace QScript {
70
71class Compiler: protected AST::Visitor
72{
73public:
74 Compiler(QScriptEnginePrivate *eng);
75 virtual ~Compiler();
76
77 inline QScriptEnginePrivate *engine() const { return m_eng; }
78
79 bool topLevelCompiler() const;
80 void setTopLevelCompiler(bool b);
81
82 CompilationUnit compile(AST::Node *node, const QList<QScriptNameIdImpl *> &formals
83 = QList<QScriptNameIdImpl *>());
84
85 struct Label {
86 Label(int o = 0):
87 offset (o) {}
88
89 int offset;
90 QVector<int> uses;
91 };
92
93// instruction set
94 void iAdd();
95 void iAssign();
96 void iBitAnd();
97 void iBitOr();
98 void iBitXor();
99 void iBitNot();
100 void iBranch(int index);
101 void iBranchTrue(int index);
102 void iBranchFalse(int index);
103 void iCall(int argc);
104 void iDeclareLocal(QScriptNameIdImpl *id, bool readOnly);
105 void iDecr();
106 void iDelete();
107 void iDiv();
108 void iDuplicate();
109 void iEqual();
110 void iFetch(QScriptNameIdImpl *id);
111 void iFetchField();
112 void iLazyArguments();
113 void iGreatOrEqual();
114 void iGreatThan();
115 void iIncr();
116 void iInplaceAdd();
117 void iInplaceAnd();
118 void iInplaceDecr();
119 void iInplaceDiv();
120 void iInplaceLeftShift();
121 void iInplaceMod();
122 void iInplaceMul();
123 void iInplaceOr();
124 void iInplaceRightShift();
125 void iInplaceSub();
126 void iInplaceURightShift();
127 void iInstanceOf();
128 void iLeftShift();
129 void iLessOrEqual();
130 void iLessThan();
131 void iLoadFalse();
132 void iLoadNumber(double number);
133 void iLoadString(QScriptNameIdImpl *id);
134 void iNewString(QScriptNameIdImpl *id);
135 void iLoadThis();
136 void iLoadActivation();
137 void iLoadNull();
138 void iLoadTrue();
139 void iLoadUndefined();
140 void iMod();
141 void iMul();
142 void iUnaryPlus();
143 void iUnaryMinus();
144 void iNew(int argc);
145 void iNewArray();
146 void iNewClosure(AST::FunctionExpression *expr);
147 void iNewObject();
148 void iNewRegExp(QScriptNameIdImpl *pattern);
149 void iNewRegExp(QScriptNameIdImpl *pattern, int flags);
150 void iNot();
151 void iNotEqual();
152 void iPop();
153 void iPostDecr();
154 void iPostIncr();
155 void iPutField();
156 void iResolve(QScriptNameIdImpl *id);
157 void iRet();
158 void iRightShift();
159 void iSub();
160 void iStrictEqual();
161 void iStrictNotEqual();
162 void iSwap();
163 void iThrow();
164 void iTypeOf();
165 void iURightShift();
166 void iInplaceXor();
167 void iLine(AST::Node *node);
168 void iNewEnumeration();
169 void iToFirstElement();
170 void iHasNextElement();
171 void iNextElement();
172 void iEnterWith();
173 void iLeaveWith();
174 void iBeginCatch(QScriptNameIdImpl *id);
175 void iEndCatch();
176 void iSync();
177 void iHalt();
178 void iMakeReference();
179 void iIn();
180 void iNop();
181 void iDebugger();
182
183protected:
184 virtual bool preVisit(AST::Node *node);
185
186 virtual bool visit(AST::ArrayLiteral *node);
187 virtual bool visit(AST::ArrayMemberExpression *node);
188 virtual bool visit(AST::Expression *node);
189 virtual bool visit(AST::BinaryExpression *node);
190 virtual bool visit(AST::BreakStatement *node);
191 virtual bool visit(AST::CallExpression *node);
192 virtual bool visit(AST::ConditionalExpression *node);
193 virtual bool visit(AST::ContinueStatement *node);
194 virtual bool visit(AST::DeleteExpression *node);
195 virtual bool visit(AST::DoWhileStatement *node);
196 virtual bool visit(AST::ExpressionStatement *node);
197 virtual bool visit(AST::FalseLiteral *node);
198 virtual bool visit(AST::FieldMemberExpression *node);
199 virtual bool visit(AST::ForEachStatement *node);
200 virtual bool visit(AST::LocalForEachStatement *node);
201 virtual bool visit(AST::ForStatement *node);
202 virtual bool visit(AST::FunctionDeclaration *node);
203 virtual bool visit(AST::FunctionExpression *node);
204 virtual bool visit(AST::FunctionSourceElement *node);
205 virtual bool visit(AST::IdentifierExpression *node);
206 virtual bool visit(AST::IfStatement *node);
207 virtual bool visit(AST::LabelledStatement *node);
208 virtual bool visit(AST::LocalForStatement *node);
209 virtual bool visit(AST::NewExpression *node);
210 virtual bool visit(AST::NewMemberExpression *node);
211 virtual bool visit(AST::NumericLiteral *node);
212 virtual bool visit(AST::ObjectLiteral *node);
213 virtual bool visit(AST::PostDecrementExpression *node);
214 virtual bool visit(AST::PostIncrementExpression *node);
215 virtual bool visit(AST::PreDecrementExpression *node);
216 virtual bool visit(AST::PreIncrementExpression *node);
217 virtual bool visit(AST::RegExpLiteral *node);
218 virtual bool visit(AST::ReturnStatement *node);
219 virtual bool visit(AST::SourceElements *node);
220 virtual bool visit(AST::StatementList *node);
221 virtual bool visit(AST::StatementSourceElement *node);
222 virtual bool visit(AST::StringLiteral *node);
223 virtual bool visit(AST::SwitchStatement *node);
224 virtual bool visit(AST::ThisExpression *node);
225 virtual bool visit(AST::NullExpression *node);
226 virtual bool visit(AST::ThrowStatement *node);
227 virtual bool visit(AST::TryStatement *node);
228 virtual bool visit(AST::TrueLiteral *node);
229 virtual bool visit(AST::VariableStatement *node);
230 virtual bool visit(AST::VariableDeclaration *node);
231 virtual bool visit(AST::WhileStatement *node);
232 virtual bool visit(AST::WithStatement *node);
233 virtual bool visit(AST::Block *node);
234 virtual bool visit(AST::TypeOfExpression *node);
235 virtual bool visit(AST::DebuggerStatement *node);
236
237 virtual void endVisit(AST::BreakStatement *node);
238 virtual void endVisit(AST::ContinueStatement *node);
239 virtual void endVisit(AST::ExpressionStatement *node);
240 virtual void endVisit(AST::NotExpression *node);
241 virtual void endVisit(AST::ReturnStatement *node);
242 virtual void endVisit(AST::ThrowStatement *node);
243 virtual void endVisit(AST::TildeExpression *node);
244 virtual void endVisit(AST::UnaryPlusExpression *node);
245 virtual void endVisit(AST::UnaryMinusExpression *node);
246 virtual void endVisit(AST::VoidExpression *node);
247 virtual void endVisit(AST::EmptyStatement *node);
248
249 void visitForInternal(AST::Statement *node, AST::ExpressionNode *condition,
250 AST::Statement *statement, AST::ExpressionNode *expression);
251
252 bool isAssignmentOperator(int op) const;
253 int inplaceAssignmentOperator(int op) const;
254
255 inline int nextInstructionOffset() const
256 { return m_instructions.count(); }
257
258 inline void pushInstruction(QScriptInstruction::Operator op)
259 {
260 pushInstruction(op, QScriptValueImpl(), QScriptValueImpl());
261 }
262
263 inline void pushInstruction(QScriptInstruction::Operator op,
264 const QScriptValueImpl &arg1)
265 {
266 pushInstruction(op, arg1, QScriptValueImpl());
267 }
268
269 inline void pushInstruction(QScriptInstruction::Operator op,
270 const QScriptValueImpl &arg1,
271 const QScriptValueImpl &arg2)
272 {
273 QScriptInstruction i;
274 i.op = op;
275 i.operand[0] = arg1;
276 i.operand[1] = arg2;
277 m_instructions.append(i);
278 }
279
280 inline bool generateReferences(bool b)
281 {
282 bool was = m_generateReferences;
283 m_generateReferences = b;
284 return was;
285 }
286
287 inline bool generateFastArgumentLookup(bool b)
288 {
289 bool was = m_generateFastArgumentLookup;
290 m_generateFastArgumentLookup= b;
291 return was;
292 }
293
294 inline bool iterationStatement(bool b)
295 {
296 bool was = m_iterationStatement;
297 m_iterationStatement = b;
298 return was;
299 }
300
301 inline bool switchStatement(bool b)
302 {
303 bool was = m_switchStatement;
304 m_switchStatement = b;
305 return was;
306 }
307
308 inline bool changeParseStatements(bool b)
309 {
310 bool was = m_parseStatements;
311 m_parseStatements = b;
312 return was;
313 }
314
315 inline bool withStatement(bool b)
316 {
317 bool was = m_withStatement;
318 m_withStatement = b;
319 return was;
320 }
321
322 inline bool generateLeaveOnBreak(bool b)
323 {
324 bool was = m_generateLeaveWithOnBreak;
325 m_generateLeaveWithOnBreak = b;
326 return was;
327 }
328
329 void patchInstruction(int index, int offset);
330
331private:
332 QScriptEnginePrivate *m_eng;
333
334 uint m_generateReferences: 1;
335 uint m_iterationStatement: 1;
336 uint m_switchStatement: 1;
337 uint m_withStatement: 1;
338 uint m_generateLeaveWithOnBreak: 1;
339 uint m_generateFastArgumentLookup: 1;
340 uint m_parseStatements: 1;
341 uint m_pad: 25;
342
343 bool m_topLevelCompiler; // bit
344 QVector<QScriptInstruction> m_instructions;
345 QVector<ExceptionHandlerDescriptor> m_exceptionHandlers;
346 QList<QScriptNameIdImpl *> m_formals;
347
348 struct Loop {
349 Loop(QScriptNameIdImpl *n = 0):
350 name(n) {}
351
352 QScriptNameIdImpl *name;
353 Label breakLabel;
354 Label continueLabel;
355 };
356
357 inline Loop *changeActiveLoop(Loop *activeLoop)
358 {
359 Loop *was = m_activeLoop;
360 m_activeLoop = activeLoop;
361 return was;
362 }
363
364 Loop *findLoop(QScriptNameIdImpl *name = 0);
365
366 Loop *m_activeLoop;
367 QMap<AST::Statement*, Loop> m_loops;
368 CompilationUnit m_compilationUnit;
369};
370
371} // namespace QScript
372
373#endif // QT_NO_SCRIPT
374
375QT_END_NAMESPACE
376
377#endif
Note: See TracBrowser for help on using the repository browser.