The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / deps / v8 / src / prettyprinter.cc @ 40c0f755
History | View | Annotate | Download (27.1 KB)
1 |
// Copyright 2006-2008 the V8 project authors. All rights reserved.
|
---|---|
2 |
// Redistribution and use in source and binary forms, with or without
|
3 |
// modification, are permitted provided that the following conditions are
|
4 |
// met:
|
5 |
//
|
6 |
// * Redistributions of source code must retain the above copyright
|
7 |
// notice, this list of conditions and the following disclaimer.
|
8 |
// * Redistributions in binary form must reproduce the above
|
9 |
// copyright notice, this list of conditions and the following
|
10 |
// disclaimer in the documentation and/or other materials provided
|
11 |
// with the distribution.
|
12 |
// * Neither the name of Google Inc. nor the names of its
|
13 |
// contributors may be used to endorse or promote products derived
|
14 |
// from this software without specific prior written permission.
|
15 |
//
|
16 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
17 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
18 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
19 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
20 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
23 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
24 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
26 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27 |
|
28 |
#include <stdarg.h> |
29 |
|
30 |
#include "v8.h" |
31 |
|
32 |
#include "prettyprinter.h" |
33 |
#include "scopes.h" |
34 |
#include "platform.h" |
35 |
|
36 |
namespace v8 { namespace internal { |
37 |
|
38 |
#ifdef DEBUG
|
39 |
|
40 |
PrettyPrinter::PrettyPrinter() { |
41 |
output_ = NULL;
|
42 |
size_ = 0;
|
43 |
pos_ = 0;
|
44 |
} |
45 |
|
46 |
|
47 |
PrettyPrinter::~PrettyPrinter() { |
48 |
DeleteArray(output_); |
49 |
} |
50 |
|
51 |
|
52 |
void PrettyPrinter::VisitBlock(Block* node) {
|
53 |
if (!node->is_initializer_block()) Print("{ "); |
54 |
PrintStatements(node->statements()); |
55 |
if (node->statements()->length() > 0) Print(" "); |
56 |
if (!node->is_initializer_block()) Print("}"); |
57 |
} |
58 |
|
59 |
|
60 |
void PrettyPrinter::VisitDeclaration(Declaration* node) {
|
61 |
Print("var ");
|
62 |
PrintLiteral(node->proxy()->name(), false);
|
63 |
if (node->fun() != NULL) { |
64 |
Print(" = ");
|
65 |
PrintFunctionLiteral(node->fun()); |
66 |
} |
67 |
Print(";");
|
68 |
} |
69 |
|
70 |
|
71 |
void PrettyPrinter::VisitExpressionStatement(ExpressionStatement* node) {
|
72 |
Visit(node->expression()); |
73 |
Print(";");
|
74 |
} |
75 |
|
76 |
|
77 |
void PrettyPrinter::VisitEmptyStatement(EmptyStatement* node) {
|
78 |
Print(";");
|
79 |
} |
80 |
|
81 |
|
82 |
void PrettyPrinter::VisitIfStatement(IfStatement* node) {
|
83 |
Print("if (");
|
84 |
Visit(node->condition()); |
85 |
Print(") ");
|
86 |
Visit(node->then_statement()); |
87 |
if (node->HasElseStatement()) {
|
88 |
Print(" else ");
|
89 |
Visit(node->else_statement()); |
90 |
} |
91 |
} |
92 |
|
93 |
|
94 |
void PrettyPrinter::VisitContinueStatement(ContinueStatement* node) {
|
95 |
Print("continue");
|
96 |
ZoneStringList* labels = node->target()->labels(); |
97 |
if (labels != NULL) { |
98 |
Print(" ");
|
99 |
ASSERT(labels->length() > 0); // guaranteed to have at least one entry |
100 |
PrintLiteral(labels->at(0), false); // any label from the list is fine |
101 |
} |
102 |
Print(";");
|
103 |
} |
104 |
|
105 |
|
106 |
void PrettyPrinter::VisitBreakStatement(BreakStatement* node) {
|
107 |
Print("break");
|
108 |
ZoneStringList* labels = node->target()->labels(); |
109 |
if (labels != NULL) { |
110 |
Print(" ");
|
111 |
ASSERT(labels->length() > 0); // guaranteed to have at least one entry |
112 |
PrintLiteral(labels->at(0), false); // any label from the list is fine |
113 |
} |
114 |
Print(";");
|
115 |
} |
116 |
|
117 |
|
118 |
void PrettyPrinter::VisitReturnStatement(ReturnStatement* node) {
|
119 |
Print("return ");
|
120 |
Visit(node->expression()); |
121 |
Print(";");
|
122 |
} |
123 |
|
124 |
|
125 |
void PrettyPrinter::VisitWithEnterStatement(WithEnterStatement* node) {
|
126 |
Print("<enter with> (");
|
127 |
Visit(node->expression()); |
128 |
Print(") ");
|
129 |
} |
130 |
|
131 |
|
132 |
void PrettyPrinter::VisitWithExitStatement(WithExitStatement* node) {
|
133 |
Print("<exit with>");
|
134 |
} |
135 |
|
136 |
|
137 |
void PrettyPrinter::VisitSwitchStatement(SwitchStatement* node) {
|
138 |
PrintLabels(node->labels()); |
139 |
Print("switch (");
|
140 |
Visit(node->tag()); |
141 |
Print(") { ");
|
142 |
ZoneList<CaseClause*>* cases = node->cases(); |
143 |
for (int i = 0; i < cases->length(); i++) |
144 |
PrintCaseClause(cases->at(i)); |
145 |
Print("}");
|
146 |
} |
147 |
|
148 |
|
149 |
void PrettyPrinter::VisitLoopStatement(LoopStatement* node) {
|
150 |
PrintLabels(node->labels()); |
151 |
switch (node->type()) {
|
152 |
case LoopStatement::DO_LOOP:
|
153 |
ASSERT(node->init() == NULL);
|
154 |
ASSERT(node->next() == NULL);
|
155 |
Print("do ");
|
156 |
Visit(node->body()); |
157 |
Print(" while (");
|
158 |
Visit(node->cond()); |
159 |
Print(");");
|
160 |
break;
|
161 |
|
162 |
case LoopStatement::FOR_LOOP:
|
163 |
Print("for (");
|
164 |
if (node->init() != NULL) { |
165 |
Visit(node->init()); |
166 |
Print(" ");
|
167 |
} else {
|
168 |
Print("; ");
|
169 |
} |
170 |
if (node->cond() != NULL) |
171 |
Visit(node->cond()); |
172 |
Print("; ");
|
173 |
if (node->next() != NULL) |
174 |
Visit(node->next()); // prints extra ';', unfortunately
|
175 |
// to fix: should use Expression for next
|
176 |
Print(") ");
|
177 |
Visit(node->body()); |
178 |
break;
|
179 |
|
180 |
case LoopStatement::WHILE_LOOP:
|
181 |
ASSERT(node->init() == NULL);
|
182 |
ASSERT(node->next() == NULL);
|
183 |
Print("while (");
|
184 |
Visit(node->cond()); |
185 |
Print(") ");
|
186 |
Visit(node->body()); |
187 |
break;
|
188 |
} |
189 |
} |
190 |
|
191 |
|
192 |
void PrettyPrinter::VisitForInStatement(ForInStatement* node) {
|
193 |
PrintLabels(node->labels()); |
194 |
Print("for (");
|
195 |
Visit(node->each()); |
196 |
Print(" in ");
|
197 |
Visit(node->enumerable()); |
198 |
Print(") ");
|
199 |
Visit(node->body()); |
200 |
} |
201 |
|
202 |
|
203 |
void PrettyPrinter::VisitTryCatch(TryCatch* node) {
|
204 |
Print("try ");
|
205 |
Visit(node->try_block()); |
206 |
Print(" catch (");
|
207 |
Visit(node->catch_var()); |
208 |
Print(") ");
|
209 |
Visit(node->catch_block()); |
210 |
} |
211 |
|
212 |
|
213 |
void PrettyPrinter::VisitTryFinally(TryFinally* node) {
|
214 |
Print("try ");
|
215 |
Visit(node->try_block()); |
216 |
Print(" finally ");
|
217 |
Visit(node->finally_block()); |
218 |
} |
219 |
|
220 |
|
221 |
void PrettyPrinter::VisitDebuggerStatement(DebuggerStatement* node) {
|
222 |
Print("debugger ");
|
223 |
} |
224 |
|
225 |
|
226 |
void PrettyPrinter::VisitFunctionLiteral(FunctionLiteral* node) {
|
227 |
Print("(");
|
228 |
PrintFunctionLiteral(node); |
229 |
Print(")");
|
230 |
} |
231 |
|
232 |
|
233 |
void PrettyPrinter::VisitFunctionBoilerplateLiteral(
|
234 |
FunctionBoilerplateLiteral* node) { |
235 |
Print("(");
|
236 |
PrintLiteral(node->boilerplate(), true);
|
237 |
Print(")");
|
238 |
} |
239 |
|
240 |
|
241 |
void PrettyPrinter::VisitConditional(Conditional* node) {
|
242 |
Visit(node->condition()); |
243 |
Print(" ? ");
|
244 |
Visit(node->then_expression()); |
245 |
Print(" : ");
|
246 |
Visit(node->else_expression()); |
247 |
} |
248 |
|
249 |
|
250 |
void PrettyPrinter::VisitLiteral(Literal* node) {
|
251 |
PrintLiteral(node->handle(), true);
|
252 |
} |
253 |
|
254 |
|
255 |
void PrettyPrinter::VisitRegExpLiteral(RegExpLiteral* node) {
|
256 |
Print(" RegExp(");
|
257 |
PrintLiteral(node->pattern(), false);
|
258 |
Print(",");
|
259 |
PrintLiteral(node->flags(), false);
|
260 |
Print(") ");
|
261 |
} |
262 |
|
263 |
|
264 |
void PrettyPrinter::VisitObjectLiteral(ObjectLiteral* node) {
|
265 |
Print("{ ");
|
266 |
for (int i = 0; i < node->properties()->length(); i++) { |
267 |
if (i != 0) Print(","); |
268 |
ObjectLiteral::Property* property = node->properties()->at(i); |
269 |
Print(" ");
|
270 |
Visit(property->key()); |
271 |
Print(": ");
|
272 |
Visit(property->value()); |
273 |
} |
274 |
Print(" }");
|
275 |
} |
276 |
|
277 |
|
278 |
void PrettyPrinter::VisitArrayLiteral(ArrayLiteral* node) {
|
279 |
Print("[ ");
|
280 |
for (int i = 0; i < node->values()->length(); i++) { |
281 |
if (i != 0) Print(","); |
282 |
Visit(node->values()->at(i)); |
283 |
} |
284 |
Print(" ]");
|
285 |
} |
286 |
|
287 |
|
288 |
void PrettyPrinter::VisitCatchExtensionObject(CatchExtensionObject* node) {
|
289 |
Print("{ ");
|
290 |
Visit(node->key()); |
291 |
Print(": ");
|
292 |
Visit(node->value()); |
293 |
Print(" }");
|
294 |
} |
295 |
|
296 |
|
297 |
void PrettyPrinter::VisitSlot(Slot* node) {
|
298 |
switch (node->type()) {
|
299 |
case Slot::PARAMETER:
|
300 |
Print("parameter[%d]", node->index());
|
301 |
break;
|
302 |
case Slot::LOCAL:
|
303 |
Print("frame[%d]", node->index());
|
304 |
break;
|
305 |
case Slot::CONTEXT:
|
306 |
Print(".context[%d]", node->index());
|
307 |
break;
|
308 |
case Slot::LOOKUP:
|
309 |
Print(".context[");
|
310 |
PrintLiteral(node->var()->name(), false);
|
311 |
Print("]");
|
312 |
break;
|
313 |
default:
|
314 |
UNREACHABLE(); |
315 |
} |
316 |
} |
317 |
|
318 |
|
319 |
void PrettyPrinter::VisitVariableProxy(VariableProxy* node) {
|
320 |
PrintLiteral(node->name(), false);
|
321 |
} |
322 |
|
323 |
|
324 |
void PrettyPrinter::VisitAssignment(Assignment* node) {
|
325 |
Visit(node->target()); |
326 |
Print(" %s ", Token::String(node->op()));
|
327 |
Visit(node->value()); |
328 |
} |
329 |
|
330 |
|
331 |
void PrettyPrinter::VisitThrow(Throw* node) {
|
332 |
Print("throw ");
|
333 |
Visit(node->exception()); |
334 |
} |
335 |
|
336 |
|
337 |
void PrettyPrinter::VisitProperty(Property* node) {
|
338 |
Expression* key = node->key(); |
339 |
Literal* literal = key->AsLiteral(); |
340 |
if (literal != NULL && literal->handle()->IsSymbol()) { |
341 |
Print("(");
|
342 |
Visit(node->obj()); |
343 |
Print(").");
|
344 |
PrintLiteral(literal->handle(), false);
|
345 |
} else {
|
346 |
Visit(node->obj()); |
347 |
Print("[");
|
348 |
Visit(key); |
349 |
Print("]");
|
350 |
} |
351 |
} |
352 |
|
353 |
|
354 |
void PrettyPrinter::VisitCall(Call* node) {
|
355 |
Visit(node->expression()); |
356 |
PrintArguments(node->arguments()); |
357 |
} |
358 |
|
359 |
|
360 |
void PrettyPrinter::VisitCallEval(CallEval* node) {
|
361 |
VisitCall(node); |
362 |
} |
363 |
|
364 |
|
365 |
void PrettyPrinter::VisitCallNew(CallNew* node) {
|
366 |
Print("new (");
|
367 |
Visit(node->expression()); |
368 |
Print(")");
|
369 |
PrintArguments(node->arguments()); |
370 |
} |
371 |
|
372 |
|
373 |
void PrettyPrinter::VisitCallRuntime(CallRuntime* node) {
|
374 |
Print("%%");
|
375 |
PrintLiteral(node->name(), false);
|
376 |
PrintArguments(node->arguments()); |
377 |
} |
378 |
|
379 |
|
380 |
void PrettyPrinter::VisitUnaryOperation(UnaryOperation* node) {
|
381 |
Print("(%s", Token::String(node->op()));
|
382 |
Visit(node->expression()); |
383 |
Print(")");
|
384 |
} |
385 |
|
386 |
|
387 |
void PrettyPrinter::VisitCountOperation(CountOperation* node) {
|
388 |
Print("(");
|
389 |
if (node->is_prefix()) Print("%s", Token::String(node->op())); |
390 |
Visit(node->expression()); |
391 |
if (node->is_postfix()) Print("%s", Token::String(node->op())); |
392 |
Print(")");
|
393 |
} |
394 |
|
395 |
|
396 |
void PrettyPrinter::VisitBinaryOperation(BinaryOperation* node) {
|
397 |
Print("(");
|
398 |
Visit(node->left()); |
399 |
Print("%s", Token::String(node->op()));
|
400 |
Visit(node->right()); |
401 |
Print(")");
|
402 |
} |
403 |
|
404 |
|
405 |
void PrettyPrinter::VisitCompareOperation(CompareOperation* node) {
|
406 |
Print("(");
|
407 |
Visit(node->left()); |
408 |
Print("%s", Token::String(node->op()));
|
409 |
Visit(node->right()); |
410 |
Print(")");
|
411 |
} |
412 |
|
413 |
|
414 |
void PrettyPrinter::VisitThisFunction(ThisFunction* node) {
|
415 |
Print("<this-function>");
|
416 |
} |
417 |
|
418 |
|
419 |
const char* PrettyPrinter::Print(Node* node) { |
420 |
Init(); |
421 |
Visit(node); |
422 |
return output_;
|
423 |
} |
424 |
|
425 |
|
426 |
const char* PrettyPrinter::PrintExpression(FunctionLiteral* program) { |
427 |
Init(); |
428 |
ExpressionStatement* statement = |
429 |
program->body()->at(0)->AsExpressionStatement();
|
430 |
Visit(statement->expression()); |
431 |
return output_;
|
432 |
} |
433 |
|
434 |
|
435 |
const char* PrettyPrinter::PrintProgram(FunctionLiteral* program) { |
436 |
Init(); |
437 |
PrintStatements(program->body()); |
438 |
Print("\n");
|
439 |
return output_;
|
440 |
} |
441 |
|
442 |
|
443 |
void PrettyPrinter::PrintOut(Node* node) {
|
444 |
PrettyPrinter printer; |
445 |
PrintF("%s", printer.Print(node));
|
446 |
} |
447 |
|
448 |
|
449 |
void PrettyPrinter::Init() {
|
450 |
if (size_ == 0) { |
451 |
ASSERT(output_ == NULL);
|
452 |
const int initial_size = 256; |
453 |
output_ = NewArray<char>(initial_size);
|
454 |
size_ = initial_size; |
455 |
} |
456 |
output_[0] = '\0'; |
457 |
pos_ = 0;
|
458 |
} |
459 |
|
460 |
|
461 |
void PrettyPrinter::Print(const char* format, ...) { |
462 |
for (;;) {
|
463 |
va_list arguments; |
464 |
va_start(arguments, format); |
465 |
int n = OS::VSNPrintF(Vector<char>(output_, size_) + pos_, |
466 |
format, |
467 |
arguments); |
468 |
va_end(arguments); |
469 |
|
470 |
if (n >= 0) { |
471 |
// there was enough space - we are done
|
472 |
pos_ += n; |
473 |
return;
|
474 |
} else {
|
475 |
// there was not enough space - allocate more and try again
|
476 |
const int slack = 32; |
477 |
int new_size = size_ + (size_ >> 1) + slack; |
478 |
char* new_output = NewArray<char>(new_size); |
479 |
memcpy(new_output, output_, pos_); |
480 |
DeleteArray(output_); |
481 |
output_ = new_output; |
482 |
size_ = new_size; |
483 |
} |
484 |
} |
485 |
} |
486 |
|
487 |
|
488 |
void PrettyPrinter::PrintStatements(ZoneList<Statement*>* statements) {
|
489 |
for (int i = 0; i < statements->length(); i++) { |
490 |
if (i != 0) Print(" "); |
491 |
Visit(statements->at(i)); |
492 |
} |
493 |
} |
494 |
|
495 |
|
496 |
void PrettyPrinter::PrintLabels(ZoneStringList* labels) {
|
497 |
if (labels != NULL) { |
498 |
for (int i = 0; i < labels->length(); i++) { |
499 |
PrintLiteral(labels->at(i), false);
|
500 |
Print(": ");
|
501 |
} |
502 |
} |
503 |
} |
504 |
|
505 |
|
506 |
void PrettyPrinter::PrintArguments(ZoneList<Expression*>* arguments) {
|
507 |
Print("(");
|
508 |
for (int i = 0; i < arguments->length(); i++) { |
509 |
if (i != 0) Print(", "); |
510 |
Visit(arguments->at(i)); |
511 |
} |
512 |
Print(")");
|
513 |
} |
514 |
|
515 |
|
516 |
void PrettyPrinter::PrintLiteral(Handle<Object> value, bool quote) { |
517 |
Object* object = *value; |
518 |
if (object->IsString()) {
|
519 |
String* string = String::cast(object);
|
520 |
if (quote) Print("\""); |
521 |
for (int i = 0; i < string->length(); i++) { |
522 |
Print("%c", string->Get(i)); |
523 |
} |
524 |
if (quote) Print("\""); |
525 |
} else if (object == Heap::null_value()) { |
526 |
Print("null");
|
527 |
} else if (object == Heap::true_value()) { |
528 |
Print("true");
|
529 |
} else if (object == Heap::false_value()) { |
530 |
Print("false");
|
531 |
} else if (object == Heap::undefined_value()) { |
532 |
Print("undefined");
|
533 |
} else if (object->IsNumber()) { |
534 |
Print("%g", object->Number());
|
535 |
} else if (object->IsJSObject()) { |
536 |
// regular expression
|
537 |
if (object->IsJSFunction()) {
|
538 |
Print("JS-Function");
|
539 |
} else if (object->IsJSArray()) { |
540 |
Print("JS-array[%u]", JSArray::cast(object)->length());
|
541 |
} else if (object->IsJSObject()) { |
542 |
Print("JS-Object");
|
543 |
} else {
|
544 |
Print("?UNKNOWN?");
|
545 |
} |
546 |
} else if (object->IsFixedArray()) { |
547 |
Print("FixedArray");
|
548 |
} else {
|
549 |
Print("<unknown literal %p>", object);
|
550 |
} |
551 |
} |
552 |
|
553 |
|
554 |
void PrettyPrinter::PrintParameters(Scope* scope) {
|
555 |
Print("(");
|
556 |
for (int i = 0; i < scope->num_parameters(); i++) { |
557 |
if (i > 0) Print(", "); |
558 |
PrintLiteral(scope->parameter(i)->name(), false);
|
559 |
} |
560 |
Print(")");
|
561 |
} |
562 |
|
563 |
|
564 |
void PrettyPrinter::PrintDeclarations(ZoneList<Declaration*>* declarations) {
|
565 |
for (int i = 0; i < declarations->length(); i++) { |
566 |
if (i > 0) Print(" "); |
567 |
Visit(declarations->at(i)); |
568 |
} |
569 |
} |
570 |
|
571 |
|
572 |
void PrettyPrinter::PrintFunctionLiteral(FunctionLiteral* function) {
|
573 |
Print("function ");
|
574 |
PrintLiteral(function->name(), false);
|
575 |
PrintParameters(function->scope()); |
576 |
Print(" { ");
|
577 |
PrintDeclarations(function->scope()->declarations()); |
578 |
PrintStatements(function->body()); |
579 |
Print(" }");
|
580 |
} |
581 |
|
582 |
|
583 |
void PrettyPrinter::PrintCaseClause(CaseClause* clause) {
|
584 |
if (clause->is_default()) {
|
585 |
Print("default");
|
586 |
} else {
|
587 |
Print("case ");
|
588 |
Visit(clause->label()); |
589 |
} |
590 |
Print(": ");
|
591 |
PrintStatements(clause->statements()); |
592 |
if (clause->statements()->length() > 0) |
593 |
Print(" ");
|
594 |
} |
595 |
|
596 |
|
597 |
//-----------------------------------------------------------------------------
|
598 |
|
599 |
class IndentedScope BASE_EMBEDDED { |
600 |
public:
|
601 |
IndentedScope() { |
602 |
ast_printer_->inc_indent(); |
603 |
} |
604 |
|
605 |
explicit IndentedScope(const char* txt, SmiAnalysis* type = NULL) { |
606 |
ast_printer_->PrintIndented(txt); |
607 |
if ((type != NULL) && (type->IsKnown())) { |
608 |
ast_printer_->Print(" (type = ");
|
609 |
ast_printer_->Print(SmiAnalysis::Type2String(type)); |
610 |
ast_printer_->Print(")");
|
611 |
} |
612 |
ast_printer_->Print("\n");
|
613 |
ast_printer_->inc_indent(); |
614 |
} |
615 |
|
616 |
virtual ~IndentedScope() {
|
617 |
ast_printer_->dec_indent(); |
618 |
} |
619 |
|
620 |
static void SetAstPrinter(AstPrinter* a) { ast_printer_ = a; } |
621 |
|
622 |
private:
|
623 |
static AstPrinter* ast_printer_;
|
624 |
}; |
625 |
|
626 |
|
627 |
AstPrinter* IndentedScope::ast_printer_ = NULL;
|
628 |
|
629 |
|
630 |
//-----------------------------------------------------------------------------
|
631 |
|
632 |
int AstPrinter::indent_ = 0; |
633 |
|
634 |
|
635 |
AstPrinter::AstPrinter() { |
636 |
ASSERT(indent_ == 0);
|
637 |
IndentedScope::SetAstPrinter(this);
|
638 |
} |
639 |
|
640 |
|
641 |
AstPrinter::~AstPrinter() { |
642 |
ASSERT(indent_ == 0);
|
643 |
IndentedScope::SetAstPrinter(NULL);
|
644 |
} |
645 |
|
646 |
|
647 |
void AstPrinter::PrintIndented(const char* txt) { |
648 |
for (int i = 0; i < indent_; i++) { |
649 |
Print(". ");
|
650 |
} |
651 |
Print(txt); |
652 |
} |
653 |
|
654 |
|
655 |
void AstPrinter::PrintLiteralIndented(const char* info, |
656 |
Handle<Object> value, |
657 |
bool quote) {
|
658 |
PrintIndented(info); |
659 |
Print(" ");
|
660 |
PrintLiteral(value, quote); |
661 |
Print("\n");
|
662 |
} |
663 |
|
664 |
|
665 |
void AstPrinter::PrintLiteralWithModeIndented(const char* info, |
666 |
Variable* var, |
667 |
Handle<Object> value, |
668 |
SmiAnalysis* type) { |
669 |
if (var == NULL) { |
670 |
PrintLiteralIndented(info, value, true);
|
671 |
} else {
|
672 |
EmbeddedVector<char, 256> buf; |
673 |
if (type->IsKnown()) {
|
674 |
OS::SNPrintF(buf, "%s (mode = %s, type = %s)", info,
|
675 |
Variable::Mode2String(var->mode()), |
676 |
SmiAnalysis::Type2String(type)); |
677 |
} else {
|
678 |
OS::SNPrintF(buf, "%s (mode = %s)", info,
|
679 |
Variable::Mode2String(var->mode())); |
680 |
} |
681 |
PrintLiteralIndented(buf.start(), value, true);
|
682 |
} |
683 |
} |
684 |
|
685 |
|
686 |
void AstPrinter::PrintLabelsIndented(const char* info, ZoneStringList* labels) { |
687 |
if (labels != NULL && labels->length() > 0) { |
688 |
if (info == NULL) { |
689 |
PrintIndented("LABELS ");
|
690 |
} else {
|
691 |
PrintIndented(info); |
692 |
Print(" ");
|
693 |
} |
694 |
PrintLabels(labels); |
695 |
Print("\n");
|
696 |
} else if (info != NULL) { |
697 |
PrintIndented(info); |
698 |
} |
699 |
} |
700 |
|
701 |
|
702 |
void AstPrinter::PrintIndentedVisit(const char* s, Node* node) { |
703 |
IndentedScope indent(s); |
704 |
Visit(node); |
705 |
} |
706 |
|
707 |
|
708 |
const char* AstPrinter::PrintProgram(FunctionLiteral* program) { |
709 |
Init(); |
710 |
{ IndentedScope indent("FUNC");
|
711 |
PrintLiteralIndented("NAME", program->name(), true); |
712 |
PrintLiteralIndented("INFERRED NAME", program->inferred_name(), true); |
713 |
PrintParameters(program->scope()); |
714 |
PrintDeclarations(program->scope()->declarations()); |
715 |
PrintStatements(program->body()); |
716 |
} |
717 |
return Output();
|
718 |
} |
719 |
|
720 |
|
721 |
void AstPrinter::PrintDeclarations(ZoneList<Declaration*>* declarations) {
|
722 |
if (declarations->length() > 0) { |
723 |
IndentedScope indent("DECLS");
|
724 |
for (int i = 0; i < declarations->length(); i++) { |
725 |
Visit(declarations->at(i)); |
726 |
} |
727 |
} |
728 |
} |
729 |
|
730 |
|
731 |
void AstPrinter::PrintParameters(Scope* scope) {
|
732 |
if (scope->num_parameters() > 0) { |
733 |
IndentedScope indent("PARAMS");
|
734 |
for (int i = 0; i < scope->num_parameters(); i++) { |
735 |
PrintLiteralWithModeIndented("VAR", scope->parameter(i),
|
736 |
scope->parameter(i)->name(), |
737 |
scope->parameter(i)->type()); |
738 |
} |
739 |
} |
740 |
} |
741 |
|
742 |
|
743 |
void AstPrinter::PrintStatements(ZoneList<Statement*>* statements) {
|
744 |
for (int i = 0; i < statements->length(); i++) { |
745 |
Visit(statements->at(i)); |
746 |
} |
747 |
} |
748 |
|
749 |
|
750 |
void AstPrinter::PrintArguments(ZoneList<Expression*>* arguments) {
|
751 |
for (int i = 0; i < arguments->length(); i++) { |
752 |
Visit(arguments->at(i)); |
753 |
} |
754 |
} |
755 |
|
756 |
|
757 |
void AstPrinter::PrintCaseClause(CaseClause* clause) {
|
758 |
if (clause->is_default()) {
|
759 |
IndentedScope indent("DEFAULT");
|
760 |
PrintStatements(clause->statements()); |
761 |
} else {
|
762 |
IndentedScope indent("CASE");
|
763 |
Visit(clause->label()); |
764 |
PrintStatements(clause->statements()); |
765 |
} |
766 |
} |
767 |
|
768 |
|
769 |
void AstPrinter::VisitBlock(Block* node) {
|
770 |
const char* block_txt = node->is_initializer_block() ? "BLOCK INIT" : "BLOCK"; |
771 |
IndentedScope indent(block_txt); |
772 |
PrintStatements(node->statements()); |
773 |
} |
774 |
|
775 |
|
776 |
void AstPrinter::VisitDeclaration(Declaration* node) {
|
777 |
if (node->fun() == NULL) { |
778 |
// var or const declarations
|
779 |
PrintLiteralWithModeIndented(Variable::Mode2String(node->mode()), |
780 |
node->proxy()->AsVariable(), |
781 |
node->proxy()->name(), |
782 |
node->proxy()->AsVariable()->type()); |
783 |
} else {
|
784 |
// function declarations
|
785 |
PrintIndented("FUNCTION ");
|
786 |
PrintLiteral(node->proxy()->name(), true);
|
787 |
Print(" = function ");
|
788 |
PrintLiteral(node->fun()->name(), false);
|
789 |
Print("\n");
|
790 |
} |
791 |
} |
792 |
|
793 |
|
794 |
void AstPrinter::VisitExpressionStatement(ExpressionStatement* node) {
|
795 |
Visit(node->expression()); |
796 |
} |
797 |
|
798 |
|
799 |
void AstPrinter::VisitEmptyStatement(EmptyStatement* node) {
|
800 |
PrintIndented("EMPTY\n");
|
801 |
} |
802 |
|
803 |
|
804 |
void AstPrinter::VisitIfStatement(IfStatement* node) {
|
805 |
PrintIndentedVisit("IF", node->condition());
|
806 |
PrintIndentedVisit("THEN", node->then_statement());
|
807 |
if (node->HasElseStatement()) {
|
808 |
PrintIndentedVisit("ELSE", node->else_statement());
|
809 |
} |
810 |
} |
811 |
|
812 |
|
813 |
void AstPrinter::VisitContinueStatement(ContinueStatement* node) {
|
814 |
PrintLabelsIndented("CONTINUE", node->target()->labels());
|
815 |
} |
816 |
|
817 |
|
818 |
void AstPrinter::VisitBreakStatement(BreakStatement* node) {
|
819 |
PrintLabelsIndented("BREAK", node->target()->labels());
|
820 |
} |
821 |
|
822 |
|
823 |
void AstPrinter::VisitReturnStatement(ReturnStatement* node) {
|
824 |
PrintIndentedVisit("RETURN", node->expression());
|
825 |
} |
826 |
|
827 |
|
828 |
void AstPrinter::VisitWithEnterStatement(WithEnterStatement* node) {
|
829 |
PrintIndentedVisit("WITH ENTER", node->expression());
|
830 |
} |
831 |
|
832 |
|
833 |
void AstPrinter::VisitWithExitStatement(WithExitStatement* node) {
|
834 |
PrintIndented("WITH EXIT\n");
|
835 |
} |
836 |
|
837 |
|
838 |
void AstPrinter::VisitSwitchStatement(SwitchStatement* node) {
|
839 |
IndentedScope indent("SWITCH");
|
840 |
PrintLabelsIndented(NULL, node->labels());
|
841 |
PrintIndentedVisit("TAG", node->tag());
|
842 |
for (int i = 0; i < node->cases()->length(); i++) { |
843 |
PrintCaseClause(node->cases()->at(i)); |
844 |
} |
845 |
} |
846 |
|
847 |
|
848 |
void AstPrinter::VisitLoopStatement(LoopStatement* node) {
|
849 |
IndentedScope indent(node->OperatorString()); |
850 |
PrintLabelsIndented(NULL, node->labels());
|
851 |
if (node->init()) PrintIndentedVisit("INIT", node->init()); |
852 |
if (node->cond()) PrintIndentedVisit("COND", node->cond()); |
853 |
if (node->body()) PrintIndentedVisit("BODY", node->body()); |
854 |
if (node->next()) PrintIndentedVisit("NEXT", node->next()); |
855 |
} |
856 |
|
857 |
|
858 |
void AstPrinter::VisitForInStatement(ForInStatement* node) {
|
859 |
IndentedScope indent("FOR IN");
|
860 |
PrintIndentedVisit("FOR", node->each());
|
861 |
PrintIndentedVisit("IN", node->enumerable());
|
862 |
PrintIndentedVisit("BODY", node->body());
|
863 |
} |
864 |
|
865 |
|
866 |
void AstPrinter::VisitTryCatch(TryCatch* node) {
|
867 |
IndentedScope indent("TRY CATCH");
|
868 |
PrintIndentedVisit("TRY", node->try_block());
|
869 |
PrintIndentedVisit("CATCHVAR", node->catch_var());
|
870 |
PrintIndentedVisit("CATCH", node->catch_block());
|
871 |
} |
872 |
|
873 |
|
874 |
void AstPrinter::VisitTryFinally(TryFinally* node) {
|
875 |
IndentedScope indent("TRY FINALLY");
|
876 |
PrintIndentedVisit("TRY", node->try_block());
|
877 |
PrintIndentedVisit("FINALLY", node->finally_block());
|
878 |
} |
879 |
|
880 |
|
881 |
void AstPrinter::VisitDebuggerStatement(DebuggerStatement* node) {
|
882 |
IndentedScope indent("DEBUGGER");
|
883 |
} |
884 |
|
885 |
|
886 |
void AstPrinter::VisitFunctionLiteral(FunctionLiteral* node) {
|
887 |
IndentedScope indent("FUNC LITERAL");
|
888 |
PrintLiteralIndented("NAME", node->name(), false); |
889 |
PrintLiteralIndented("INFERRED NAME", node->inferred_name(), false); |
890 |
PrintParameters(node->scope()); |
891 |
// We don't want to see the function literal in this case: it
|
892 |
// will be printed via PrintProgram when the code for it is
|
893 |
// generated.
|
894 |
// PrintStatements(node->body());
|
895 |
} |
896 |
|
897 |
|
898 |
void AstPrinter::VisitFunctionBoilerplateLiteral(
|
899 |
FunctionBoilerplateLiteral* node) { |
900 |
IndentedScope indent("FUNC LITERAL");
|
901 |
PrintLiteralIndented("BOILERPLATE", node->boilerplate(), true); |
902 |
} |
903 |
|
904 |
|
905 |
void AstPrinter::VisitConditional(Conditional* node) {
|
906 |
IndentedScope indent("CONDITIONAL");
|
907 |
PrintIndentedVisit("?", node->condition());
|
908 |
PrintIndentedVisit("THEN", node->then_expression());
|
909 |
PrintIndentedVisit("ELSE", node->else_expression());
|
910 |
} |
911 |
|
912 |
|
913 |
void AstPrinter::VisitLiteral(Literal* node) {
|
914 |
PrintLiteralIndented("LITERAL", node->handle(), true); |
915 |
} |
916 |
|
917 |
|
918 |
void AstPrinter::VisitRegExpLiteral(RegExpLiteral* node) {
|
919 |
IndentedScope indent("REGEXP LITERAL");
|
920 |
PrintLiteral(node->pattern(), false);
|
921 |
Print(",");
|
922 |
PrintLiteral(node->flags(), false);
|
923 |
} |
924 |
|
925 |
|
926 |
void AstPrinter::VisitObjectLiteral(ObjectLiteral* node) {
|
927 |
IndentedScope indent("OBJ LITERAL");
|
928 |
for (int i = 0; i < node->properties()->length(); i++) { |
929 |
const char* prop_kind = NULL; |
930 |
switch (node->properties()->at(i)->kind()) {
|
931 |
case ObjectLiteral::Property::CONSTANT:
|
932 |
prop_kind = "PROPERTY - CONSTANT";
|
933 |
break;
|
934 |
case ObjectLiteral::Property::COMPUTED:
|
935 |
prop_kind = "PROPERTY - COMPUTED";
|
936 |
break;
|
937 |
case ObjectLiteral::Property::PROTOTYPE:
|
938 |
prop_kind = "PROPERTY - PROTOTYPE";
|
939 |
break;
|
940 |
case ObjectLiteral::Property::GETTER:
|
941 |
prop_kind = "PROPERTY - GETTER";
|
942 |
break;
|
943 |
case ObjectLiteral::Property::SETTER:
|
944 |
prop_kind = "PROPERTY - SETTER";
|
945 |
break;
|
946 |
default:
|
947 |
UNREACHABLE(); |
948 |
break;
|
949 |
} |
950 |
IndentedScope prop(prop_kind); |
951 |
PrintIndentedVisit("KEY", node->properties()->at(i)->key());
|
952 |
PrintIndentedVisit("VALUE", node->properties()->at(i)->value());
|
953 |
} |
954 |
} |
955 |
|
956 |
|
957 |
void AstPrinter::VisitArrayLiteral(ArrayLiteral* node) {
|
958 |
IndentedScope indent("ARRAY LITERAL");
|
959 |
if (node->values()->length() > 0) { |
960 |
IndentedScope indent("VALUES");
|
961 |
for (int i = 0; i < node->values()->length(); i++) { |
962 |
Visit(node->values()->at(i)); |
963 |
} |
964 |
} |
965 |
} |
966 |
|
967 |
|
968 |
void AstPrinter::VisitCatchExtensionObject(CatchExtensionObject* node) {
|
969 |
IndentedScope indent("CatchExtensionObject");
|
970 |
PrintIndentedVisit("KEY", node->key());
|
971 |
PrintIndentedVisit("VALUE", node->value());
|
972 |
} |
973 |
|
974 |
|
975 |
void AstPrinter::VisitSlot(Slot* node) {
|
976 |
PrintIndented("SLOT ");
|
977 |
switch (node->type()) {
|
978 |
case Slot::PARAMETER:
|
979 |
Print("parameter[%d]", node->index());
|
980 |
break;
|
981 |
case Slot::LOCAL:
|
982 |
Print("frame[%d]", node->index());
|
983 |
break;
|
984 |
case Slot::CONTEXT:
|
985 |
Print(".context[%d]", node->index());
|
986 |
break;
|
987 |
case Slot::LOOKUP:
|
988 |
Print(".context[");
|
989 |
PrintLiteral(node->var()->name(), false);
|
990 |
Print("]");
|
991 |
break;
|
992 |
default:
|
993 |
UNREACHABLE(); |
994 |
} |
995 |
Print("\n");
|
996 |
} |
997 |
|
998 |
|
999 |
void AstPrinter::VisitVariableProxy(VariableProxy* node) {
|
1000 |
PrintLiteralWithModeIndented("VAR PROXY", node->AsVariable(), node->name(),
|
1001 |
node->type()); |
1002 |
Variable* var = node->var(); |
1003 |
if (var != NULL && var->rewrite() != NULL) { |
1004 |
IndentedScope indent; |
1005 |
Visit(var->rewrite()); |
1006 |
} |
1007 |
} |
1008 |
|
1009 |
|
1010 |
void AstPrinter::VisitAssignment(Assignment* node) {
|
1011 |
IndentedScope indent(Token::Name(node->op()), node->type()); |
1012 |
Visit(node->target()); |
1013 |
Visit(node->value()); |
1014 |
} |
1015 |
|
1016 |
|
1017 |
void AstPrinter::VisitThrow(Throw* node) {
|
1018 |
PrintIndentedVisit("THROW", node->exception());
|
1019 |
} |
1020 |
|
1021 |
|
1022 |
void AstPrinter::VisitProperty(Property* node) {
|
1023 |
IndentedScope indent("PROPERTY");
|
1024 |
Visit(node->obj()); |
1025 |
Literal* literal = node->key()->AsLiteral(); |
1026 |
if (literal != NULL && literal->handle()->IsSymbol()) { |
1027 |
PrintLiteralIndented("NAME", literal->handle(), false); |
1028 |
} else {
|
1029 |
PrintIndentedVisit("KEY", node->key());
|
1030 |
} |
1031 |
} |
1032 |
|
1033 |
|
1034 |
void AstPrinter::VisitCall(Call* node) {
|
1035 |
IndentedScope indent("CALL");
|
1036 |
Visit(node->expression()); |
1037 |
PrintArguments(node->arguments()); |
1038 |
} |
1039 |
|
1040 |
|
1041 |
void AstPrinter::VisitCallEval(CallEval* node) {
|
1042 |
VisitCall(node); |
1043 |
} |
1044 |
|
1045 |
|
1046 |
void AstPrinter::VisitCallNew(CallNew* node) {
|
1047 |
IndentedScope indent("CALL NEW");
|
1048 |
Visit(node->expression()); |
1049 |
PrintArguments(node->arguments()); |
1050 |
} |
1051 |
|
1052 |
|
1053 |
void AstPrinter::VisitCallRuntime(CallRuntime* node) {
|
1054 |
PrintLiteralIndented("CALL RUNTIME ", node->name(), false); |
1055 |
IndentedScope indent; |
1056 |
PrintArguments(node->arguments()); |
1057 |
} |
1058 |
|
1059 |
|
1060 |
void AstPrinter::VisitUnaryOperation(UnaryOperation* node) {
|
1061 |
PrintIndentedVisit(Token::Name(node->op()), node->expression()); |
1062 |
} |
1063 |
|
1064 |
|
1065 |
void AstPrinter::VisitCountOperation(CountOperation* node) {
|
1066 |
EmbeddedVector<char, 128> buf; |
1067 |
if (node->type()->IsKnown()) {
|
1068 |
OS::SNPrintF(buf, "%s %s (type = %s)",
|
1069 |
(node->is_prefix() ? "PRE" : "POST"), |
1070 |
Token::Name(node->op()), |
1071 |
SmiAnalysis::Type2String(node->type())); |
1072 |
} else {
|
1073 |
OS::SNPrintF(buf, "%s %s", (node->is_prefix() ? "PRE" : "POST"), |
1074 |
Token::Name(node->op())); |
1075 |
} |
1076 |
PrintIndentedVisit(buf.start(), node->expression()); |
1077 |
} |
1078 |
|
1079 |
|
1080 |
void AstPrinter::VisitBinaryOperation(BinaryOperation* node) {
|
1081 |
IndentedScope indent(Token::Name(node->op()), node->type()); |
1082 |
Visit(node->left()); |
1083 |
Visit(node->right()); |
1084 |
} |
1085 |
|
1086 |
|
1087 |
void AstPrinter::VisitCompareOperation(CompareOperation* node) {
|
1088 |
IndentedScope indent(Token::Name(node->op()), node->type()); |
1089 |
Visit(node->left()); |
1090 |
Visit(node->right()); |
1091 |
} |
1092 |
|
1093 |
|
1094 |
void AstPrinter::VisitThisFunction(ThisFunction* node) {
|
1095 |
IndentedScope indent("THIS-FUNCTION");
|
1096 |
} |
1097 |
|
1098 |
|
1099 |
|
1100 |
#endif // DEBUG |
1101 |
|
1102 |
} } // namespace v8::internal
|