OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / libs / qmljs / qmljsevaluate.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "qmljsevaluate.h"
35 #include "qmljsinterpreter.h"
36 #include "parser/qmljsparser_p.h"
37 #include "parser/qmljsast_p.h"
38 #include <QtCore/QDebug>
39
40 using namespace QmlJS;
41 using namespace QmlJS::Interpreter;
42
43 Evaluate::Evaluate(const Context *context)
44     : _engine(context->engine()),
45       _context(context),
46       _scope(_engine->globalObject()),
47       _result(0)
48 {
49 }
50
51 Evaluate::~Evaluate()
52 {
53 }
54
55 const Interpreter::Value *Evaluate::operator()(AST::Node *ast)
56 {
57     const Value *result = reference(ast);
58
59     if (const Reference *ref = value_cast<const Reference *>(result))
60         result = _context->lookupReference(ref);
61
62     if (! result)
63         result = _engine->undefinedValue();
64
65     return result;
66 }
67
68 const Interpreter::Value *Evaluate::reference(AST::Node *ast)
69 {
70     // save the result
71     const Value *previousResult = switchResult(0);
72
73     // process the expression
74     accept(ast);
75
76     // restore the previous result
77     return switchResult(previousResult);
78 }
79
80 Interpreter::Engine *Evaluate::switchEngine(Interpreter::Engine *engine)
81 {
82     Interpreter::Engine *previousEngine = _engine;
83     _engine = engine;
84     return previousEngine;
85 }
86
87 const Interpreter::Value *Evaluate::switchResult(const Interpreter::Value *result)
88 {
89     const Interpreter::Value *previousResult = _result;
90     _result = result;
91     return previousResult;
92 }
93
94 const Interpreter::ObjectValue *Evaluate::switchScope(const Interpreter::ObjectValue *scope)
95 {
96     const Interpreter::ObjectValue *previousScope = _scope;
97     _scope = scope;
98     return previousScope;
99 }
100
101 void Evaluate::accept(AST::Node *node)
102 {
103     AST::Node::accept(node, this);
104 }
105
106 bool Evaluate::visit(AST::UiProgram *)
107 {
108     return false;
109 }
110
111 bool Evaluate::visit(AST::UiImportList *)
112 {
113     return false;
114 }
115
116 bool Evaluate::visit(AST::UiImport *)
117 {
118     return false;
119 }
120
121 bool Evaluate::visit(AST::UiPublicMember *)
122 {
123     return false;
124 }
125
126 bool Evaluate::visit(AST::UiSourceElement *)
127 {
128     return false;
129 }
130
131 bool Evaluate::visit(AST::UiObjectDefinition *)
132 {
133     return false;
134 }
135
136 bool Evaluate::visit(AST::UiObjectInitializer *)
137 {
138     return false;
139 }
140
141 bool Evaluate::visit(AST::UiObjectBinding *)
142 {
143     return false;
144 }
145
146 bool Evaluate::visit(AST::UiScriptBinding *)
147 {
148     return false;
149 }
150
151 bool Evaluate::visit(AST::UiArrayBinding *)
152 {
153     return false;
154 }
155
156 bool Evaluate::visit(AST::UiObjectMemberList *)
157 {
158     return false;
159 }
160
161 bool Evaluate::visit(AST::UiArrayMemberList *)
162 {
163     return false;
164 }
165
166 bool Evaluate::visit(AST::UiQualifiedId *ast)
167 {
168     if (! ast->name)
169          return false;
170
171     const Value *value = _context->lookup(ast->name->asString());
172     if (! ast->next) {
173         _result = value;
174
175     } else {
176         const ObjectValue *base = value_cast<const ObjectValue *>(value);
177
178         for (AST::UiQualifiedId *it = ast->next; base && it; it = it->next) {
179             NameId *name = it->name;
180             if (! name)
181                 break;
182
183             const Value *value = base->property(name->asString(), _context);
184             if (! it->next)
185                 _result = value;
186             else
187                 base = value_cast<const ObjectValue *>(value);
188         }
189     }
190
191     return false;
192 }
193
194 bool Evaluate::visit(AST::UiSignature *)
195 {
196     return false;
197 }
198
199 bool Evaluate::visit(AST::UiFormalList *)
200 {
201     return false;
202 }
203
204 bool Evaluate::visit(AST::UiFormal *)
205 {
206     return false;
207 }
208
209 bool Evaluate::visit(AST::ThisExpression *)
210 {
211     return false;
212 }
213
214 bool Evaluate::visit(AST::IdentifierExpression *ast)
215 {
216     if (! ast->name)
217         return false;
218
219     _result = _context->lookup(ast->name->asString());
220     return false;
221 }
222
223 bool Evaluate::visit(AST::NullExpression *)
224 {
225     _result = _engine->nullValue();
226     return false;
227 }
228
229 bool Evaluate::visit(AST::TrueLiteral *)
230 {
231     _result = _engine->booleanValue();
232     return false;
233 }
234
235 bool Evaluate::visit(AST::FalseLiteral *)
236 {
237     _result = _engine->booleanValue();
238     return false;
239 }
240
241 bool Evaluate::visit(AST::StringLiteral *)
242 {
243     _result = _engine->stringValue();
244     return false;
245 }
246
247 bool Evaluate::visit(AST::NumericLiteral *)
248 {
249     _result = _engine->numberValue();
250     return false;
251 }
252
253 bool Evaluate::visit(AST::RegExpLiteral *)
254 {
255     _result = _engine->regexpCtor()->construct();
256     return false;
257 }
258
259 bool Evaluate::visit(AST::ArrayLiteral *)
260 {
261     _result = _engine->arrayCtor()->construct();
262     return false;
263 }
264
265 bool Evaluate::visit(AST::ObjectLiteral *)
266 {
267     return false;
268 }
269
270 bool Evaluate::visit(AST::ElementList *)
271 {
272     return false;
273 }
274
275 bool Evaluate::visit(AST::Elision *)
276 {
277     return false;
278 }
279
280 bool Evaluate::visit(AST::PropertyNameAndValueList *)
281 {
282     return false;
283 }
284
285 bool Evaluate::visit(AST::NestedExpression *)
286 {
287     return true; // visit the child expression
288 }
289
290 bool Evaluate::visit(AST::IdentifierPropertyName *)
291 {
292     return false;
293 }
294
295 bool Evaluate::visit(AST::StringLiteralPropertyName *)
296 {
297     return false;
298 }
299
300 bool Evaluate::visit(AST::NumericLiteralPropertyName *)
301 {
302     return false;
303 }
304
305 bool Evaluate::visit(AST::ArrayMemberExpression *)
306 {
307     return false;
308 }
309
310 bool Evaluate::visit(AST::FieldMemberExpression *ast)
311 {
312     if (! ast->name)
313         return false;
314
315     if (const Interpreter::Value *base = _engine->convertToObject(reference(ast->base))) {
316         if (const Interpreter::ObjectValue *obj = base->asObjectValue()) {
317             _result = obj->property(ast->name->asString(), _context);
318         }
319     }
320
321     return false;
322 }
323
324 bool Evaluate::visit(AST::NewMemberExpression *ast)
325 {
326     if (const FunctionValue *ctor = value_cast<const FunctionValue *>(reference(ast->base))) {
327         _result = ctor->construct();
328     }
329     return false;
330 }
331
332 bool Evaluate::visit(AST::NewExpression *ast)
333 {
334     if (const FunctionValue *ctor = value_cast<const FunctionValue *>(reference(ast->expression))) {
335         _result = ctor->construct();
336     }
337     return false;
338 }
339
340 bool Evaluate::visit(AST::CallExpression *ast)
341 {
342     if (const Interpreter::Value *base = reference(ast->base)) {
343         if (const Interpreter::FunctionValue *obj = base->asFunctionValue()) {
344             _result = obj->returnValue();
345         }
346     }
347     return false;
348 }
349
350 bool Evaluate::visit(AST::ArgumentList *)
351 {
352     return false;
353 }
354
355 bool Evaluate::visit(AST::PostIncrementExpression *)
356 {
357     return false;
358 }
359
360 bool Evaluate::visit(AST::PostDecrementExpression *)
361 {
362     return false;
363 }
364
365 bool Evaluate::visit(AST::DeleteExpression *)
366 {
367     return false;
368 }
369
370 bool Evaluate::visit(AST::VoidExpression *)
371 {
372     return false;
373 }
374
375 bool Evaluate::visit(AST::TypeOfExpression *)
376 {
377     return false;
378 }
379
380 bool Evaluate::visit(AST::PreIncrementExpression *)
381 {
382     return false;
383 }
384
385 bool Evaluate::visit(AST::PreDecrementExpression *)
386 {
387     return false;
388 }
389
390 bool Evaluate::visit(AST::UnaryPlusExpression *)
391 {
392     return false;
393 }
394
395 bool Evaluate::visit(AST::UnaryMinusExpression *)
396 {
397     return false;
398 }
399
400 bool Evaluate::visit(AST::TildeExpression *)
401 {
402     return false;
403 }
404
405 bool Evaluate::visit(AST::NotExpression *)
406 {
407     return false;
408 }
409
410 bool Evaluate::visit(AST::BinaryExpression *)
411 {
412     return false;
413 }
414
415 bool Evaluate::visit(AST::ConditionalExpression *)
416 {
417     return false;
418 }
419
420 bool Evaluate::visit(AST::Expression *)
421 {
422     return false;
423 }
424
425 bool Evaluate::visit(AST::Block *)
426 {
427     return false;
428 }
429
430 bool Evaluate::visit(AST::StatementList *)
431 {
432     return false;
433 }
434
435 bool Evaluate::visit(AST::VariableStatement *)
436 {
437     return false;
438 }
439
440 bool Evaluate::visit(AST::VariableDeclarationList *)
441 {
442     return false;
443 }
444
445 bool Evaluate::visit(AST::VariableDeclaration *)
446 {
447     return false;
448 }
449
450 bool Evaluate::visit(AST::EmptyStatement *)
451 {
452     return false;
453 }
454
455 bool Evaluate::visit(AST::ExpressionStatement *)
456 {
457     return true;
458 }
459
460 bool Evaluate::visit(AST::IfStatement *)
461 {
462     return false;
463 }
464
465 bool Evaluate::visit(AST::DoWhileStatement *)
466 {
467     return false;
468 }
469
470 bool Evaluate::visit(AST::WhileStatement *)
471 {
472     return false;
473 }
474
475 bool Evaluate::visit(AST::ForStatement *)
476 {
477     return false;
478 }
479
480 bool Evaluate::visit(AST::LocalForStatement *)
481 {
482     return false;
483 }
484
485 bool Evaluate::visit(AST::ForEachStatement *)
486 {
487     return false;
488 }
489
490 bool Evaluate::visit(AST::LocalForEachStatement *)
491 {
492     return false;
493 }
494
495 bool Evaluate::visit(AST::ContinueStatement *)
496 {
497     return false;
498 }
499
500 bool Evaluate::visit(AST::BreakStatement *)
501 {
502     return false;
503 }
504
505 bool Evaluate::visit(AST::ReturnStatement *)
506 {
507     return false;
508 }
509
510 bool Evaluate::visit(AST::WithStatement *)
511 {
512     return false;
513 }
514
515 bool Evaluate::visit(AST::SwitchStatement *)
516 {
517     return false;
518 }
519
520 bool Evaluate::visit(AST::CaseBlock *)
521 {
522     return false;
523 }
524
525 bool Evaluate::visit(AST::CaseClauses *)
526 {
527     return false;
528 }
529
530 bool Evaluate::visit(AST::CaseClause *)
531 {
532     return false;
533 }
534
535 bool Evaluate::visit(AST::DefaultClause *)
536 {
537     return false;
538 }
539
540 bool Evaluate::visit(AST::LabelledStatement *)
541 {
542     return false;
543 }
544
545 bool Evaluate::visit(AST::ThrowStatement *)
546 {
547     return false;
548 }
549
550 bool Evaluate::visit(AST::TryStatement *)
551 {
552     return false;
553 }
554
555 bool Evaluate::visit(AST::Catch *)
556 {
557     return false;
558 }
559
560 bool Evaluate::visit(AST::Finally *)
561 {
562     return false;
563 }
564
565 bool Evaluate::visit(AST::FunctionDeclaration *)
566 {
567     return false;
568 }
569
570 bool Evaluate::visit(AST::FunctionExpression *)
571 {
572     return false;
573 }
574
575 bool Evaluate::visit(AST::FormalParameterList *)
576 {
577     return false;
578 }
579
580 bool Evaluate::visit(AST::FunctionBody *)
581 {
582     return false;
583 }
584
585 bool Evaluate::visit(AST::Program *)
586 {
587     return false;
588 }
589
590 bool Evaluate::visit(AST::SourceElements *)
591 {
592     return false;
593 }
594
595 bool Evaluate::visit(AST::FunctionSourceElement *)
596 {
597     return false;
598 }
599
600 bool Evaluate::visit(AST::StatementSourceElement *)
601 {
602     return false;
603 }
604
605 bool Evaluate::visit(AST::DebuggerStatement *)
606 {
607     return false;
608 }