svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 36 | namespace v8 { |
| 37 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | |
| 39 | #ifdef DEBUG |
| 40 | |
| 41 | PrettyPrinter::PrettyPrinter() { |
| 42 | output_ = NULL; |
| 43 | size_ = 0; |
| 44 | pos_ = 0; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | PrettyPrinter::~PrettyPrinter() { |
| 49 | DeleteArray(output_); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | void PrettyPrinter::VisitBlock(Block* node) { |
| 54 | if (!node->is_initializer_block()) Print("{ "); |
| 55 | PrintStatements(node->statements()); |
| 56 | if (node->statements()->length() > 0) Print(" "); |
| 57 | if (!node->is_initializer_block()) Print("}"); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | void PrettyPrinter::VisitDeclaration(Declaration* node) { |
| 62 | Print("var "); |
| 63 | PrintLiteral(node->proxy()->name(), false); |
| 64 | if (node->fun() != NULL) { |
| 65 | Print(" = "); |
| 66 | PrintFunctionLiteral(node->fun()); |
| 67 | } |
| 68 | Print(";"); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | void PrettyPrinter::VisitExpressionStatement(ExpressionStatement* node) { |
| 73 | Visit(node->expression()); |
| 74 | Print(";"); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | void PrettyPrinter::VisitEmptyStatement(EmptyStatement* node) { |
| 79 | Print(";"); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | void PrettyPrinter::VisitIfStatement(IfStatement* node) { |
| 84 | Print("if ("); |
| 85 | Visit(node->condition()); |
| 86 | Print(") "); |
| 87 | Visit(node->then_statement()); |
| 88 | if (node->HasElseStatement()) { |
| 89 | Print(" else "); |
| 90 | Visit(node->else_statement()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | void PrettyPrinter::VisitContinueStatement(ContinueStatement* node) { |
| 96 | Print("continue"); |
| 97 | ZoneStringList* labels = node->target()->labels(); |
| 98 | if (labels != NULL) { |
| 99 | Print(" "); |
| 100 | ASSERT(labels->length() > 0); // guaranteed to have at least one entry |
| 101 | PrintLiteral(labels->at(0), false); // any label from the list is fine |
| 102 | } |
| 103 | Print(";"); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | void PrettyPrinter::VisitBreakStatement(BreakStatement* node) { |
| 108 | Print("break"); |
| 109 | ZoneStringList* labels = node->target()->labels(); |
| 110 | if (labels != NULL) { |
| 111 | Print(" "); |
| 112 | ASSERT(labels->length() > 0); // guaranteed to have at least one entry |
| 113 | PrintLiteral(labels->at(0), false); // any label from the list is fine |
| 114 | } |
| 115 | Print(";"); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | void PrettyPrinter::VisitReturnStatement(ReturnStatement* node) { |
| 120 | Print("return "); |
| 121 | Visit(node->expression()); |
| 122 | Print(";"); |
| 123 | } |
| 124 | |
| 125 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame^] | 126 | void PrettyPrinter::VisitWithStatement(WithStatement* node) { |
| 127 | Print("with ("); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 128 | Visit(node->expression()); |
| 129 | Print(") "); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame^] | 130 | Visit(node->statement()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 134 | void PrettyPrinter::VisitExitContextStatement(ExitContextStatement* node) { |
| 135 | Print("<exit context>"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
| 139 | void PrettyPrinter::VisitSwitchStatement(SwitchStatement* node) { |
| 140 | PrintLabels(node->labels()); |
| 141 | Print("switch ("); |
| 142 | Visit(node->tag()); |
| 143 | Print(") { "); |
| 144 | ZoneList<CaseClause*>* cases = node->cases(); |
| 145 | for (int i = 0; i < cases->length(); i++) |
| 146 | PrintCaseClause(cases->at(i)); |
| 147 | Print("}"); |
| 148 | } |
| 149 | |
| 150 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 151 | void PrettyPrinter::VisitDoWhileStatement(DoWhileStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 152 | PrintLabels(node->labels()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 153 | Print("do "); |
| 154 | Visit(node->body()); |
| 155 | Print(" while ("); |
| 156 | Visit(node->cond()); |
| 157 | Print(");"); |
| 158 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 159 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 160 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 161 | void PrettyPrinter::VisitWhileStatement(WhileStatement* node) { |
| 162 | PrintLabels(node->labels()); |
| 163 | Print("while ("); |
| 164 | Visit(node->cond()); |
| 165 | Print(") "); |
| 166 | Visit(node->body()); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | void PrettyPrinter::VisitForStatement(ForStatement* node) { |
| 171 | PrintLabels(node->labels()); |
| 172 | Print("for ("); |
| 173 | if (node->init() != NULL) { |
| 174 | Visit(node->init()); |
| 175 | Print(" "); |
| 176 | } else { |
| 177 | Print("; "); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 178 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 179 | if (node->cond() != NULL) Visit(node->cond()); |
| 180 | Print("; "); |
| 181 | if (node->next() != NULL) { |
| 182 | Visit(node->next()); // prints extra ';', unfortunately |
| 183 | // to fix: should use Expression for next |
| 184 | } |
| 185 | Print(") "); |
| 186 | Visit(node->body()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | |
| 190 | void PrettyPrinter::VisitForInStatement(ForInStatement* node) { |
| 191 | PrintLabels(node->labels()); |
| 192 | Print("for ("); |
| 193 | Visit(node->each()); |
| 194 | Print(" in "); |
| 195 | Visit(node->enumerable()); |
| 196 | Print(") "); |
| 197 | Visit(node->body()); |
| 198 | } |
| 199 | |
| 200 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 201 | void PrettyPrinter::VisitTryCatchStatement(TryCatchStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 202 | Print("try "); |
| 203 | Visit(node->try_block()); |
| 204 | Print(" catch ("); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 205 | const bool quote = false; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 206 | PrintLiteral(node->variable()->name(), quote); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 207 | Print(") "); |
| 208 | Visit(node->catch_block()); |
| 209 | } |
| 210 | |
| 211 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 212 | void PrettyPrinter::VisitTryFinallyStatement(TryFinallyStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 213 | Print("try "); |
| 214 | Visit(node->try_block()); |
| 215 | Print(" finally "); |
| 216 | Visit(node->finally_block()); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | void PrettyPrinter::VisitDebuggerStatement(DebuggerStatement* node) { |
| 221 | Print("debugger "); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | void PrettyPrinter::VisitFunctionLiteral(FunctionLiteral* node) { |
| 226 | Print("("); |
| 227 | PrintFunctionLiteral(node); |
| 228 | Print(")"); |
| 229 | } |
| 230 | |
| 231 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 232 | void PrettyPrinter::VisitSharedFunctionInfoLiteral( |
| 233 | SharedFunctionInfoLiteral* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 234 | Print("("); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 235 | PrintLiteral(node->shared_function_info(), true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 236 | Print(")"); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | void PrettyPrinter::VisitConditional(Conditional* node) { |
| 241 | Visit(node->condition()); |
| 242 | Print(" ? "); |
| 243 | Visit(node->then_expression()); |
| 244 | Print(" : "); |
| 245 | Visit(node->else_expression()); |
| 246 | } |
| 247 | |
| 248 | |
| 249 | void PrettyPrinter::VisitLiteral(Literal* node) { |
| 250 | PrintLiteral(node->handle(), true); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | void PrettyPrinter::VisitRegExpLiteral(RegExpLiteral* node) { |
| 255 | Print(" RegExp("); |
| 256 | PrintLiteral(node->pattern(), false); |
| 257 | Print(","); |
| 258 | PrintLiteral(node->flags(), false); |
| 259 | Print(") "); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | void PrettyPrinter::VisitObjectLiteral(ObjectLiteral* node) { |
| 264 | Print("{ "); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 265 | for (int i = 0; i < node->properties()->length(); i++) { |
| 266 | if (i != 0) Print(","); |
| 267 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 268 | Print(" "); |
| 269 | Visit(property->key()); |
| 270 | Print(": "); |
| 271 | Visit(property->value()); |
| 272 | } |
| 273 | Print(" }"); |
| 274 | } |
| 275 | |
| 276 | |
| 277 | void PrettyPrinter::VisitArrayLiteral(ArrayLiteral* node) { |
| 278 | Print("[ "); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 279 | for (int i = 0; i < node->values()->length(); i++) { |
| 280 | if (i != 0) Print(","); |
| 281 | Visit(node->values()->at(i)); |
| 282 | } |
| 283 | Print(" ]"); |
| 284 | } |
| 285 | |
| 286 | |
| 287 | void PrettyPrinter::VisitSlot(Slot* node) { |
| 288 | switch (node->type()) { |
| 289 | case Slot::PARAMETER: |
| 290 | Print("parameter[%d]", node->index()); |
| 291 | break; |
| 292 | case Slot::LOCAL: |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 293 | Print("local[%d]", node->index()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 294 | break; |
| 295 | case Slot::CONTEXT: |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 296 | Print("context[%d]", node->index()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 297 | break; |
| 298 | case Slot::LOOKUP: |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 299 | Print("lookup["); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 300 | PrintLiteral(node->var()->name(), false); |
| 301 | Print("]"); |
| 302 | break; |
| 303 | default: |
| 304 | UNREACHABLE(); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | |
| 309 | void PrettyPrinter::VisitVariableProxy(VariableProxy* node) { |
| 310 | PrintLiteral(node->name(), false); |
| 311 | } |
| 312 | |
| 313 | |
| 314 | void PrettyPrinter::VisitAssignment(Assignment* node) { |
| 315 | Visit(node->target()); |
| 316 | Print(" %s ", Token::String(node->op())); |
| 317 | Visit(node->value()); |
| 318 | } |
| 319 | |
| 320 | |
| 321 | void PrettyPrinter::VisitThrow(Throw* node) { |
| 322 | Print("throw "); |
| 323 | Visit(node->exception()); |
| 324 | } |
| 325 | |
| 326 | |
| 327 | void PrettyPrinter::VisitProperty(Property* node) { |
| 328 | Expression* key = node->key(); |
| 329 | Literal* literal = key->AsLiteral(); |
| 330 | if (literal != NULL && literal->handle()->IsSymbol()) { |
| 331 | Print("("); |
| 332 | Visit(node->obj()); |
| 333 | Print(")."); |
| 334 | PrintLiteral(literal->handle(), false); |
| 335 | } else { |
| 336 | Visit(node->obj()); |
| 337 | Print("["); |
| 338 | Visit(key); |
| 339 | Print("]"); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | |
| 344 | void PrettyPrinter::VisitCall(Call* node) { |
| 345 | Visit(node->expression()); |
| 346 | PrintArguments(node->arguments()); |
| 347 | } |
| 348 | |
| 349 | |
| 350 | void PrettyPrinter::VisitCallNew(CallNew* node) { |
| 351 | Print("new ("); |
| 352 | Visit(node->expression()); |
| 353 | Print(")"); |
| 354 | PrintArguments(node->arguments()); |
| 355 | } |
| 356 | |
| 357 | |
| 358 | void PrettyPrinter::VisitCallRuntime(CallRuntime* node) { |
| 359 | Print("%%"); |
| 360 | PrintLiteral(node->name(), false); |
| 361 | PrintArguments(node->arguments()); |
| 362 | } |
| 363 | |
| 364 | |
| 365 | void PrettyPrinter::VisitUnaryOperation(UnaryOperation* node) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 366 | Token::Value op = node->op(); |
| 367 | bool needsSpace = |
| 368 | op == Token::DELETE || op == Token::TYPEOF || op == Token::VOID; |
| 369 | Print("(%s%s", Token::String(op), needsSpace ? " " : ""); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 370 | Visit(node->expression()); |
| 371 | Print(")"); |
| 372 | } |
| 373 | |
| 374 | |
| 375 | void PrettyPrinter::VisitCountOperation(CountOperation* node) { |
| 376 | Print("("); |
| 377 | if (node->is_prefix()) Print("%s", Token::String(node->op())); |
| 378 | Visit(node->expression()); |
| 379 | if (node->is_postfix()) Print("%s", Token::String(node->op())); |
| 380 | Print(")"); |
| 381 | } |
| 382 | |
| 383 | |
| 384 | void PrettyPrinter::VisitBinaryOperation(BinaryOperation* node) { |
| 385 | Print("("); |
| 386 | Visit(node->left()); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 387 | Print(" %s ", Token::String(node->op())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 388 | Visit(node->right()); |
| 389 | Print(")"); |
| 390 | } |
| 391 | |
| 392 | |
| 393 | void PrettyPrinter::VisitCompareOperation(CompareOperation* node) { |
| 394 | Print("("); |
| 395 | Visit(node->left()); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 396 | Print(" %s ", Token::String(node->op())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 397 | Visit(node->right()); |
| 398 | Print(")"); |
| 399 | } |
| 400 | |
| 401 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 402 | void PrettyPrinter::VisitCompareToNull(CompareToNull* node) { |
| 403 | Print("("); |
| 404 | Visit(node->expression()); |
| 405 | Print("%s null)", Token::String(node->op())); |
| 406 | } |
| 407 | |
| 408 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 409 | void PrettyPrinter::VisitThisFunction(ThisFunction* node) { |
| 410 | Print("<this-function>"); |
| 411 | } |
| 412 | |
| 413 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 414 | const char* PrettyPrinter::Print(AstNode* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 415 | Init(); |
| 416 | Visit(node); |
| 417 | return output_; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | const char* PrettyPrinter::PrintExpression(FunctionLiteral* program) { |
| 422 | Init(); |
| 423 | ExpressionStatement* statement = |
| 424 | program->body()->at(0)->AsExpressionStatement(); |
| 425 | Visit(statement->expression()); |
| 426 | return output_; |
| 427 | } |
| 428 | |
| 429 | |
| 430 | const char* PrettyPrinter::PrintProgram(FunctionLiteral* program) { |
| 431 | Init(); |
| 432 | PrintStatements(program->body()); |
| 433 | Print("\n"); |
| 434 | return output_; |
| 435 | } |
| 436 | |
| 437 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 438 | void PrettyPrinter::PrintOut(AstNode* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 439 | PrettyPrinter printer; |
| 440 | PrintF("%s", printer.Print(node)); |
| 441 | } |
| 442 | |
| 443 | |
| 444 | void PrettyPrinter::Init() { |
| 445 | if (size_ == 0) { |
| 446 | ASSERT(output_ == NULL); |
| 447 | const int initial_size = 256; |
| 448 | output_ = NewArray<char>(initial_size); |
| 449 | size_ = initial_size; |
| 450 | } |
| 451 | output_[0] = '\0'; |
| 452 | pos_ = 0; |
| 453 | } |
| 454 | |
| 455 | |
| 456 | void PrettyPrinter::Print(const char* format, ...) { |
| 457 | for (;;) { |
| 458 | va_list arguments; |
| 459 | va_start(arguments, format); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 460 | int n = OS::VSNPrintF(Vector<char>(output_, size_) + pos_, |
| 461 | format, |
| 462 | arguments); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 463 | va_end(arguments); |
| 464 | |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 465 | if (n >= 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 466 | // there was enough space - we are done |
| 467 | pos_ += n; |
| 468 | return; |
| 469 | } else { |
| 470 | // there was not enough space - allocate more and try again |
| 471 | const int slack = 32; |
| 472 | int new_size = size_ + (size_ >> 1) + slack; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 473 | char* new_output = NewArray<char>(new_size); |
| 474 | memcpy(new_output, output_, pos_); |
| 475 | DeleteArray(output_); |
| 476 | output_ = new_output; |
| 477 | size_ = new_size; |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | |
| 483 | void PrettyPrinter::PrintStatements(ZoneList<Statement*>* statements) { |
| 484 | for (int i = 0; i < statements->length(); i++) { |
| 485 | if (i != 0) Print(" "); |
| 486 | Visit(statements->at(i)); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | |
| 491 | void PrettyPrinter::PrintLabels(ZoneStringList* labels) { |
| 492 | if (labels != NULL) { |
| 493 | for (int i = 0; i < labels->length(); i++) { |
| 494 | PrintLiteral(labels->at(i), false); |
| 495 | Print(": "); |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | |
| 501 | void PrettyPrinter::PrintArguments(ZoneList<Expression*>* arguments) { |
| 502 | Print("("); |
| 503 | for (int i = 0; i < arguments->length(); i++) { |
| 504 | if (i != 0) Print(", "); |
| 505 | Visit(arguments->at(i)); |
| 506 | } |
| 507 | Print(")"); |
| 508 | } |
| 509 | |
| 510 | |
| 511 | void PrettyPrinter::PrintLiteral(Handle<Object> value, bool quote) { |
| 512 | Object* object = *value; |
| 513 | if (object->IsString()) { |
| 514 | String* string = String::cast(object); |
| 515 | if (quote) Print("\""); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 516 | for (int i = 0; i < string->length(); i++) { |
| 517 | Print("%c", string->Get(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 518 | } |
| 519 | if (quote) Print("\""); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 520 | } else if (object->IsNull()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 521 | Print("null"); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 522 | } else if (object->IsTrue()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 523 | Print("true"); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 524 | } else if (object->IsFalse()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 525 | Print("false"); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 526 | } else if (object->IsUndefined()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 527 | Print("undefined"); |
| 528 | } else if (object->IsNumber()) { |
| 529 | Print("%g", object->Number()); |
| 530 | } else if (object->IsJSObject()) { |
| 531 | // regular expression |
| 532 | if (object->IsJSFunction()) { |
| 533 | Print("JS-Function"); |
| 534 | } else if (object->IsJSArray()) { |
| 535 | Print("JS-array[%u]", JSArray::cast(object)->length()); |
| 536 | } else if (object->IsJSObject()) { |
| 537 | Print("JS-Object"); |
| 538 | } else { |
| 539 | Print("?UNKNOWN?"); |
| 540 | } |
| 541 | } else if (object->IsFixedArray()) { |
| 542 | Print("FixedArray"); |
| 543 | } else { |
| 544 | Print("<unknown literal %p>", object); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | |
| 549 | void PrettyPrinter::PrintParameters(Scope* scope) { |
| 550 | Print("("); |
| 551 | for (int i = 0; i < scope->num_parameters(); i++) { |
| 552 | if (i > 0) Print(", "); |
| 553 | PrintLiteral(scope->parameter(i)->name(), false); |
| 554 | } |
| 555 | Print(")"); |
| 556 | } |
| 557 | |
| 558 | |
| 559 | void PrettyPrinter::PrintDeclarations(ZoneList<Declaration*>* declarations) { |
| 560 | for (int i = 0; i < declarations->length(); i++) { |
| 561 | if (i > 0) Print(" "); |
| 562 | Visit(declarations->at(i)); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | |
| 567 | void PrettyPrinter::PrintFunctionLiteral(FunctionLiteral* function) { |
| 568 | Print("function "); |
| 569 | PrintLiteral(function->name(), false); |
| 570 | PrintParameters(function->scope()); |
| 571 | Print(" { "); |
| 572 | PrintDeclarations(function->scope()->declarations()); |
| 573 | PrintStatements(function->body()); |
| 574 | Print(" }"); |
| 575 | } |
| 576 | |
| 577 | |
| 578 | void PrettyPrinter::PrintCaseClause(CaseClause* clause) { |
| 579 | if (clause->is_default()) { |
| 580 | Print("default"); |
| 581 | } else { |
| 582 | Print("case "); |
| 583 | Visit(clause->label()); |
| 584 | } |
| 585 | Print(": "); |
| 586 | PrintStatements(clause->statements()); |
| 587 | if (clause->statements()->length() > 0) |
| 588 | Print(" "); |
| 589 | } |
| 590 | |
| 591 | |
| 592 | //----------------------------------------------------------------------------- |
| 593 | |
| 594 | class IndentedScope BASE_EMBEDDED { |
| 595 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 596 | explicit IndentedScope(AstPrinter* printer) : ast_printer_(printer) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 597 | ast_printer_->inc_indent(); |
| 598 | } |
| 599 | |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 600 | IndentedScope(AstPrinter* printer, const char* txt, AstNode* node = NULL) |
| 601 | : ast_printer_(printer) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 602 | ast_printer_->PrintIndented(txt); |
| 603 | ast_printer_->Print("\n"); |
| 604 | ast_printer_->inc_indent(); |
| 605 | } |
| 606 | |
| 607 | virtual ~IndentedScope() { |
| 608 | ast_printer_->dec_indent(); |
| 609 | } |
| 610 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 611 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 612 | AstPrinter* ast_printer_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 613 | }; |
| 614 | |
| 615 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 616 | //----------------------------------------------------------------------------- |
| 617 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 618 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 619 | AstPrinter::AstPrinter() : indent_(0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | |
| 623 | AstPrinter::~AstPrinter() { |
| 624 | ASSERT(indent_ == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | |
| 628 | void AstPrinter::PrintIndented(const char* txt) { |
| 629 | for (int i = 0; i < indent_; i++) { |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 630 | Print(". "); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 631 | } |
| 632 | Print(txt); |
| 633 | } |
| 634 | |
| 635 | |
| 636 | void AstPrinter::PrintLiteralIndented(const char* info, |
| 637 | Handle<Object> value, |
| 638 | bool quote) { |
| 639 | PrintIndented(info); |
| 640 | Print(" "); |
| 641 | PrintLiteral(value, quote); |
| 642 | Print("\n"); |
| 643 | } |
| 644 | |
| 645 | |
| 646 | void AstPrinter::PrintLiteralWithModeIndented(const char* info, |
| 647 | Variable* var, |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 648 | Handle<Object> value) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 649 | if (var == NULL) { |
| 650 | PrintLiteralIndented(info, value, true); |
| 651 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 652 | EmbeddedVector<char, 256> buf; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 653 | int pos = OS::SNPrintF(buf, "%s (mode = %s", info, |
| 654 | Variable::Mode2String(var->mode())); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 655 | OS::SNPrintF(buf + pos, ")"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 656 | PrintLiteralIndented(buf.start(), value, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | |
| 661 | void AstPrinter::PrintLabelsIndented(const char* info, ZoneStringList* labels) { |
| 662 | if (labels != NULL && labels->length() > 0) { |
| 663 | if (info == NULL) { |
| 664 | PrintIndented("LABELS "); |
| 665 | } else { |
| 666 | PrintIndented(info); |
| 667 | Print(" "); |
| 668 | } |
| 669 | PrintLabels(labels); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 670 | } else if (info != NULL) { |
| 671 | PrintIndented(info); |
| 672 | } |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 673 | Print("\n"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 677 | void AstPrinter::PrintIndentedVisit(const char* s, AstNode* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 678 | IndentedScope indent(this, s, node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 679 | Visit(node); |
| 680 | } |
| 681 | |
| 682 | |
| 683 | const char* AstPrinter::PrintProgram(FunctionLiteral* program) { |
| 684 | Init(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 685 | { IndentedScope indent(this, "FUNC"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 686 | PrintLiteralIndented("NAME", program->name(), true); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 687 | PrintLiteralIndented("INFERRED NAME", program->inferred_name(), true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 688 | PrintParameters(program->scope()); |
| 689 | PrintDeclarations(program->scope()->declarations()); |
| 690 | PrintStatements(program->body()); |
| 691 | } |
| 692 | return Output(); |
| 693 | } |
| 694 | |
| 695 | |
| 696 | void AstPrinter::PrintDeclarations(ZoneList<Declaration*>* declarations) { |
| 697 | if (declarations->length() > 0) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 698 | IndentedScope indent(this, "DECLS"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 699 | for (int i = 0; i < declarations->length(); i++) { |
| 700 | Visit(declarations->at(i)); |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | |
| 706 | void AstPrinter::PrintParameters(Scope* scope) { |
| 707 | if (scope->num_parameters() > 0) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 708 | IndentedScope indent(this, "PARAMS"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 709 | for (int i = 0; i < scope->num_parameters(); i++) { |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 710 | PrintLiteralWithModeIndented("VAR", scope->parameter(i), |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 711 | scope->parameter(i)->name()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | |
| 717 | void AstPrinter::PrintStatements(ZoneList<Statement*>* statements) { |
| 718 | for (int i = 0; i < statements->length(); i++) { |
| 719 | Visit(statements->at(i)); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | |
| 724 | void AstPrinter::PrintArguments(ZoneList<Expression*>* arguments) { |
| 725 | for (int i = 0; i < arguments->length(); i++) { |
| 726 | Visit(arguments->at(i)); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | |
| 731 | void AstPrinter::PrintCaseClause(CaseClause* clause) { |
| 732 | if (clause->is_default()) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 733 | IndentedScope indent(this, "DEFAULT"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 734 | PrintStatements(clause->statements()); |
| 735 | } else { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 736 | IndentedScope indent(this, "CASE"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 737 | Visit(clause->label()); |
| 738 | PrintStatements(clause->statements()); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | |
| 743 | void AstPrinter::VisitBlock(Block* node) { |
| 744 | const char* block_txt = node->is_initializer_block() ? "BLOCK INIT" : "BLOCK"; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 745 | IndentedScope indent(this, block_txt); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 746 | PrintStatements(node->statements()); |
| 747 | } |
| 748 | |
| 749 | |
| 750 | void AstPrinter::VisitDeclaration(Declaration* node) { |
| 751 | if (node->fun() == NULL) { |
| 752 | // var or const declarations |
kasperl@chromium.org | 9bbf968 | 2008-10-30 11:53:07 +0000 | [diff] [blame] | 753 | PrintLiteralWithModeIndented(Variable::Mode2String(node->mode()), |
| 754 | node->proxy()->AsVariable(), |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 755 | node->proxy()->name()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 756 | } else { |
| 757 | // function declarations |
| 758 | PrintIndented("FUNCTION "); |
| 759 | PrintLiteral(node->proxy()->name(), true); |
| 760 | Print(" = function "); |
| 761 | PrintLiteral(node->fun()->name(), false); |
| 762 | Print("\n"); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | |
| 767 | void AstPrinter::VisitExpressionStatement(ExpressionStatement* node) { |
| 768 | Visit(node->expression()); |
| 769 | } |
| 770 | |
| 771 | |
| 772 | void AstPrinter::VisitEmptyStatement(EmptyStatement* node) { |
| 773 | PrintIndented("EMPTY\n"); |
| 774 | } |
| 775 | |
| 776 | |
| 777 | void AstPrinter::VisitIfStatement(IfStatement* node) { |
| 778 | PrintIndentedVisit("IF", node->condition()); |
| 779 | PrintIndentedVisit("THEN", node->then_statement()); |
| 780 | if (node->HasElseStatement()) { |
| 781 | PrintIndentedVisit("ELSE", node->else_statement()); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | |
| 786 | void AstPrinter::VisitContinueStatement(ContinueStatement* node) { |
| 787 | PrintLabelsIndented("CONTINUE", node->target()->labels()); |
| 788 | } |
| 789 | |
| 790 | |
| 791 | void AstPrinter::VisitBreakStatement(BreakStatement* node) { |
| 792 | PrintLabelsIndented("BREAK", node->target()->labels()); |
| 793 | } |
| 794 | |
| 795 | |
| 796 | void AstPrinter::VisitReturnStatement(ReturnStatement* node) { |
| 797 | PrintIndentedVisit("RETURN", node->expression()); |
| 798 | } |
| 799 | |
| 800 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame^] | 801 | void AstPrinter::VisitWithStatement(WithStatement* node) { |
| 802 | IndentedScope indent(this, "WITH"); |
| 803 | PrintIndentedVisit("OBJECT", node->expression()); |
| 804 | PrintIndentedVisit("BODY", node->statement()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 808 | void AstPrinter::VisitExitContextStatement(ExitContextStatement* node) { |
| 809 | PrintIndented("EXIT CONTEXT\n"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | |
| 813 | void AstPrinter::VisitSwitchStatement(SwitchStatement* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 814 | IndentedScope indent(this, "SWITCH"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 815 | PrintLabelsIndented(NULL, node->labels()); |
| 816 | PrintIndentedVisit("TAG", node->tag()); |
| 817 | for (int i = 0; i < node->cases()->length(); i++) { |
| 818 | PrintCaseClause(node->cases()->at(i)); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 823 | void AstPrinter::VisitDoWhileStatement(DoWhileStatement* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 824 | IndentedScope indent(this, "DO"); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 825 | PrintLabelsIndented(NULL, node->labels()); |
| 826 | PrintIndentedVisit("BODY", node->body()); |
| 827 | PrintIndentedVisit("COND", node->cond()); |
| 828 | } |
| 829 | |
| 830 | |
| 831 | void AstPrinter::VisitWhileStatement(WhileStatement* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 832 | IndentedScope indent(this, "WHILE"); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 833 | PrintLabelsIndented(NULL, node->labels()); |
| 834 | PrintIndentedVisit("COND", node->cond()); |
| 835 | PrintIndentedVisit("BODY", node->body()); |
| 836 | } |
| 837 | |
| 838 | |
| 839 | void AstPrinter::VisitForStatement(ForStatement* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 840 | IndentedScope indent(this, "FOR"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 841 | PrintLabelsIndented(NULL, node->labels()); |
| 842 | if (node->init()) PrintIndentedVisit("INIT", node->init()); |
| 843 | if (node->cond()) PrintIndentedVisit("COND", node->cond()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 844 | PrintIndentedVisit("BODY", node->body()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 845 | if (node->next()) PrintIndentedVisit("NEXT", node->next()); |
| 846 | } |
| 847 | |
| 848 | |
| 849 | void AstPrinter::VisitForInStatement(ForInStatement* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 850 | IndentedScope indent(this, "FOR IN"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 851 | PrintIndentedVisit("FOR", node->each()); |
| 852 | PrintIndentedVisit("IN", node->enumerable()); |
| 853 | PrintIndentedVisit("BODY", node->body()); |
| 854 | } |
| 855 | |
| 856 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 857 | void AstPrinter::VisitTryCatchStatement(TryCatchStatement* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 858 | IndentedScope indent(this, "TRY CATCH"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 859 | PrintIndentedVisit("TRY", node->try_block()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 860 | PrintLiteralWithModeIndented("CATCHVAR", |
| 861 | node->variable(), |
| 862 | node->variable()->name()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 863 | PrintIndentedVisit("CATCH", node->catch_block()); |
| 864 | } |
| 865 | |
| 866 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 867 | void AstPrinter::VisitTryFinallyStatement(TryFinallyStatement* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 868 | IndentedScope indent(this, "TRY FINALLY"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 869 | PrintIndentedVisit("TRY", node->try_block()); |
| 870 | PrintIndentedVisit("FINALLY", node->finally_block()); |
| 871 | } |
| 872 | |
| 873 | |
| 874 | void AstPrinter::VisitDebuggerStatement(DebuggerStatement* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 875 | IndentedScope indent(this, "DEBUGGER"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | |
| 879 | void AstPrinter::VisitFunctionLiteral(FunctionLiteral* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 880 | IndentedScope indent(this, "FUNC LITERAL"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 881 | PrintLiteralIndented("NAME", node->name(), false); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 882 | PrintLiteralIndented("INFERRED NAME", node->inferred_name(), false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 883 | PrintParameters(node->scope()); |
| 884 | // We don't want to see the function literal in this case: it |
| 885 | // will be printed via PrintProgram when the code for it is |
| 886 | // generated. |
| 887 | // PrintStatements(node->body()); |
| 888 | } |
| 889 | |
| 890 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 891 | void AstPrinter::VisitSharedFunctionInfoLiteral( |
| 892 | SharedFunctionInfoLiteral* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 893 | IndentedScope indent(this, "FUNC LITERAL"); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 894 | PrintLiteralIndented("SHARED INFO", node->shared_function_info(), true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | |
| 898 | void AstPrinter::VisitConditional(Conditional* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 899 | IndentedScope indent(this, "CONDITIONAL"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 900 | PrintIndentedVisit("?", node->condition()); |
| 901 | PrintIndentedVisit("THEN", node->then_expression()); |
| 902 | PrintIndentedVisit("ELSE", node->else_expression()); |
| 903 | } |
| 904 | |
| 905 | |
| 906 | void AstPrinter::VisitLiteral(Literal* node) { |
| 907 | PrintLiteralIndented("LITERAL", node->handle(), true); |
| 908 | } |
| 909 | |
| 910 | |
| 911 | void AstPrinter::VisitRegExpLiteral(RegExpLiteral* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 912 | IndentedScope indent(this, "REGEXP LITERAL"); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 913 | PrintLiteralIndented("PATTERN", node->pattern(), false); |
| 914 | PrintLiteralIndented("FLAGS", node->flags(), false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | |
| 918 | void AstPrinter::VisitObjectLiteral(ObjectLiteral* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 919 | IndentedScope indent(this, "OBJ LITERAL"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 920 | for (int i = 0; i < node->properties()->length(); i++) { |
| 921 | const char* prop_kind = NULL; |
| 922 | switch (node->properties()->at(i)->kind()) { |
| 923 | case ObjectLiteral::Property::CONSTANT: |
| 924 | prop_kind = "PROPERTY - CONSTANT"; |
| 925 | break; |
| 926 | case ObjectLiteral::Property::COMPUTED: |
| 927 | prop_kind = "PROPERTY - COMPUTED"; |
| 928 | break; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 929 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 930 | prop_kind = "PROPERTY - MATERIALIZED_LITERAL"; |
| 931 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 932 | case ObjectLiteral::Property::PROTOTYPE: |
| 933 | prop_kind = "PROPERTY - PROTOTYPE"; |
| 934 | break; |
| 935 | case ObjectLiteral::Property::GETTER: |
| 936 | prop_kind = "PROPERTY - GETTER"; |
| 937 | break; |
| 938 | case ObjectLiteral::Property::SETTER: |
| 939 | prop_kind = "PROPERTY - SETTER"; |
| 940 | break; |
| 941 | default: |
| 942 | UNREACHABLE(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 943 | } |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 944 | IndentedScope prop(this, prop_kind); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 945 | PrintIndentedVisit("KEY", node->properties()->at(i)->key()); |
| 946 | PrintIndentedVisit("VALUE", node->properties()->at(i)->value()); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | |
| 951 | void AstPrinter::VisitArrayLiteral(ArrayLiteral* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 952 | IndentedScope indent(this, "ARRAY LITERAL"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 953 | if (node->values()->length() > 0) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 954 | IndentedScope indent(this, "VALUES"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 955 | for (int i = 0; i < node->values()->length(); i++) { |
| 956 | Visit(node->values()->at(i)); |
| 957 | } |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | |
| 962 | void AstPrinter::VisitSlot(Slot* node) { |
| 963 | PrintIndented("SLOT "); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 964 | PrettyPrinter::VisitSlot(node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 965 | Print("\n"); |
| 966 | } |
| 967 | |
| 968 | |
| 969 | void AstPrinter::VisitVariableProxy(VariableProxy* node) { |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 970 | PrintLiteralWithModeIndented("VAR PROXY", node->AsVariable(), node->name()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 971 | Variable* var = node->var(); |
| 972 | if (var != NULL && var->rewrite() != NULL) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 973 | IndentedScope indent(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 974 | Visit(var->rewrite()); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | |
| 979 | void AstPrinter::VisitAssignment(Assignment* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 980 | IndentedScope indent(this, Token::Name(node->op()), node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 981 | Visit(node->target()); |
| 982 | Visit(node->value()); |
| 983 | } |
| 984 | |
| 985 | |
| 986 | void AstPrinter::VisitThrow(Throw* node) { |
| 987 | PrintIndentedVisit("THROW", node->exception()); |
| 988 | } |
| 989 | |
| 990 | |
| 991 | void AstPrinter::VisitProperty(Property* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 992 | IndentedScope indent(this, "PROPERTY", node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 993 | Visit(node->obj()); |
| 994 | Literal* literal = node->key()->AsLiteral(); |
| 995 | if (literal != NULL && literal->handle()->IsSymbol()) { |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 996 | PrintLiteralIndented("NAME", literal->handle(), false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 997 | } else { |
| 998 | PrintIndentedVisit("KEY", node->key()); |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | |
| 1003 | void AstPrinter::VisitCall(Call* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1004 | IndentedScope indent(this, "CALL"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1005 | Visit(node->expression()); |
| 1006 | PrintArguments(node->arguments()); |
| 1007 | } |
| 1008 | |
| 1009 | |
| 1010 | void AstPrinter::VisitCallNew(CallNew* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1011 | IndentedScope indent(this, "CALL NEW"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1012 | Visit(node->expression()); |
| 1013 | PrintArguments(node->arguments()); |
| 1014 | } |
| 1015 | |
| 1016 | |
| 1017 | void AstPrinter::VisitCallRuntime(CallRuntime* node) { |
| 1018 | PrintLiteralIndented("CALL RUNTIME ", node->name(), false); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1019 | IndentedScope indent(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1020 | PrintArguments(node->arguments()); |
| 1021 | } |
| 1022 | |
| 1023 | |
| 1024 | void AstPrinter::VisitUnaryOperation(UnaryOperation* node) { |
| 1025 | PrintIndentedVisit(Token::Name(node->op()), node->expression()); |
| 1026 | } |
| 1027 | |
| 1028 | |
| 1029 | void AstPrinter::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1030 | EmbeddedVector<char, 128> buf; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 1031 | OS::SNPrintF(buf, "%s %s", (node->is_prefix() ? "PRE" : "POST"), |
| 1032 | Token::Name(node->op())); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1033 | PrintIndentedVisit(buf.start(), node->expression()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | |
| 1037 | void AstPrinter::VisitBinaryOperation(BinaryOperation* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1038 | IndentedScope indent(this, Token::Name(node->op()), node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1039 | Visit(node->left()); |
| 1040 | Visit(node->right()); |
| 1041 | } |
| 1042 | |
| 1043 | |
| 1044 | void AstPrinter::VisitCompareOperation(CompareOperation* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1045 | IndentedScope indent(this, Token::Name(node->op()), node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1046 | Visit(node->left()); |
| 1047 | Visit(node->right()); |
| 1048 | } |
| 1049 | |
| 1050 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1051 | void AstPrinter::VisitCompareToNull(CompareToNull* node) { |
| 1052 | const char* name = node->is_strict() |
| 1053 | ? "COMPARE-TO-NULL-STRICT" |
| 1054 | : "COMPARE-TO-NULL"; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1055 | IndentedScope indent(this, name, node); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1056 | Visit(node->expression()); |
| 1057 | } |
| 1058 | |
| 1059 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1060 | void AstPrinter::VisitThisFunction(ThisFunction* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1061 | IndentedScope indent(this, "THIS-FUNCTION"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1065 | TagScope::TagScope(JsonAstBuilder* builder, const char* name) |
| 1066 | : builder_(builder), next_(builder->tag()), has_body_(false) { |
| 1067 | if (next_ != NULL) { |
| 1068 | next_->use(); |
| 1069 | builder->Print(",\n"); |
| 1070 | } |
| 1071 | builder->set_tag(this); |
| 1072 | builder->PrintIndented("["); |
| 1073 | builder->Print("\"%s\"", name); |
| 1074 | builder->increase_indent(JsonAstBuilder::kTagIndentSize); |
| 1075 | } |
| 1076 | |
| 1077 | |
| 1078 | TagScope::~TagScope() { |
| 1079 | builder_->decrease_indent(JsonAstBuilder::kTagIndentSize); |
| 1080 | if (has_body_) { |
| 1081 | builder_->Print("\n"); |
| 1082 | builder_->PrintIndented("]"); |
| 1083 | } else { |
| 1084 | builder_->Print("]"); |
| 1085 | } |
| 1086 | builder_->set_tag(next_); |
| 1087 | } |
| 1088 | |
| 1089 | |
| 1090 | AttributesScope::AttributesScope(JsonAstBuilder* builder) |
| 1091 | : builder_(builder), attribute_count_(0) { |
| 1092 | builder->set_attributes(this); |
| 1093 | builder->tag()->use(); |
| 1094 | builder->Print(",\n"); |
| 1095 | builder->PrintIndented("{"); |
| 1096 | builder->increase_indent(JsonAstBuilder::kAttributesIndentSize); |
| 1097 | } |
| 1098 | |
| 1099 | |
| 1100 | AttributesScope::~AttributesScope() { |
| 1101 | builder_->decrease_indent(JsonAstBuilder::kAttributesIndentSize); |
| 1102 | if (attribute_count_ > 1) { |
| 1103 | builder_->Print("\n"); |
| 1104 | builder_->PrintIndented("}"); |
| 1105 | } else { |
| 1106 | builder_->Print("}"); |
| 1107 | } |
| 1108 | builder_->set_attributes(NULL); |
| 1109 | } |
| 1110 | |
| 1111 | |
| 1112 | const char* JsonAstBuilder::BuildProgram(FunctionLiteral* program) { |
| 1113 | Init(); |
| 1114 | Visit(program); |
| 1115 | Print("\n"); |
| 1116 | return Output(); |
| 1117 | } |
| 1118 | |
| 1119 | |
| 1120 | void JsonAstBuilder::AddAttributePrefix(const char* name) { |
| 1121 | if (attributes()->is_used()) { |
| 1122 | Print(",\n"); |
| 1123 | PrintIndented("\""); |
| 1124 | } else { |
| 1125 | Print("\""); |
| 1126 | } |
| 1127 | Print("%s\":", name); |
| 1128 | attributes()->use(); |
| 1129 | } |
| 1130 | |
| 1131 | |
| 1132 | void JsonAstBuilder::AddAttribute(const char* name, Handle<String> value) { |
| 1133 | SmartPointer<char> value_string = value->ToCString(); |
| 1134 | AddAttributePrefix(name); |
| 1135 | Print("\"%s\"", *value_string); |
| 1136 | } |
| 1137 | |
| 1138 | |
| 1139 | void JsonAstBuilder::AddAttribute(const char* name, const char* value) { |
| 1140 | AddAttributePrefix(name); |
| 1141 | Print("\"%s\"", value); |
| 1142 | } |
| 1143 | |
| 1144 | |
| 1145 | void JsonAstBuilder::AddAttribute(const char* name, int value) { |
| 1146 | AddAttributePrefix(name); |
| 1147 | Print("%d", value); |
| 1148 | } |
| 1149 | |
| 1150 | |
| 1151 | void JsonAstBuilder::AddAttribute(const char* name, bool value) { |
| 1152 | AddAttributePrefix(name); |
| 1153 | Print(value ? "true" : "false"); |
| 1154 | } |
| 1155 | |
| 1156 | |
| 1157 | void JsonAstBuilder::VisitBlock(Block* stmt) { |
| 1158 | TagScope tag(this, "Block"); |
| 1159 | VisitStatements(stmt->statements()); |
| 1160 | } |
| 1161 | |
| 1162 | |
| 1163 | void JsonAstBuilder::VisitExpressionStatement(ExpressionStatement* stmt) { |
| 1164 | TagScope tag(this, "ExpressionStatement"); |
| 1165 | Visit(stmt->expression()); |
| 1166 | } |
| 1167 | |
| 1168 | |
| 1169 | void JsonAstBuilder::VisitEmptyStatement(EmptyStatement* stmt) { |
| 1170 | TagScope tag(this, "EmptyStatement"); |
| 1171 | } |
| 1172 | |
| 1173 | |
| 1174 | void JsonAstBuilder::VisitIfStatement(IfStatement* stmt) { |
| 1175 | TagScope tag(this, "IfStatement"); |
| 1176 | Visit(stmt->condition()); |
| 1177 | Visit(stmt->then_statement()); |
| 1178 | Visit(stmt->else_statement()); |
| 1179 | } |
| 1180 | |
| 1181 | |
| 1182 | void JsonAstBuilder::VisitContinueStatement(ContinueStatement* stmt) { |
| 1183 | TagScope tag(this, "ContinueStatement"); |
| 1184 | } |
| 1185 | |
| 1186 | |
| 1187 | void JsonAstBuilder::VisitBreakStatement(BreakStatement* stmt) { |
| 1188 | TagScope tag(this, "BreakStatement"); |
| 1189 | } |
| 1190 | |
| 1191 | |
| 1192 | void JsonAstBuilder::VisitReturnStatement(ReturnStatement* stmt) { |
| 1193 | TagScope tag(this, "ReturnStatement"); |
| 1194 | Visit(stmt->expression()); |
| 1195 | } |
| 1196 | |
| 1197 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame^] | 1198 | void JsonAstBuilder::VisitWithStatement(WithStatement* stmt) { |
| 1199 | TagScope tag(this, "WithStatement"); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1200 | Visit(stmt->expression()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame^] | 1201 | Visit(stmt->statement()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1205 | void JsonAstBuilder::VisitExitContextStatement(ExitContextStatement* stmt) { |
| 1206 | TagScope tag(this, "ExitContextStatement"); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | |
| 1210 | void JsonAstBuilder::VisitSwitchStatement(SwitchStatement* stmt) { |
| 1211 | TagScope tag(this, "SwitchStatement"); |
| 1212 | } |
| 1213 | |
| 1214 | |
| 1215 | void JsonAstBuilder::VisitDoWhileStatement(DoWhileStatement* stmt) { |
| 1216 | TagScope tag(this, "DoWhileStatement"); |
| 1217 | Visit(stmt->body()); |
| 1218 | Visit(stmt->cond()); |
| 1219 | } |
| 1220 | |
| 1221 | |
| 1222 | void JsonAstBuilder::VisitWhileStatement(WhileStatement* stmt) { |
| 1223 | TagScope tag(this, "WhileStatement"); |
| 1224 | Visit(stmt->cond()); |
| 1225 | Visit(stmt->body()); |
| 1226 | } |
| 1227 | |
| 1228 | |
| 1229 | void JsonAstBuilder::VisitForStatement(ForStatement* stmt) { |
| 1230 | TagScope tag(this, "ForStatement"); |
| 1231 | if (stmt->init() != NULL) Visit(stmt->init()); |
| 1232 | if (stmt->cond() != NULL) Visit(stmt->cond()); |
| 1233 | Visit(stmt->body()); |
| 1234 | if (stmt->next() != NULL) Visit(stmt->next()); |
| 1235 | } |
| 1236 | |
| 1237 | |
| 1238 | void JsonAstBuilder::VisitForInStatement(ForInStatement* stmt) { |
| 1239 | TagScope tag(this, "ForInStatement"); |
| 1240 | Visit(stmt->each()); |
| 1241 | Visit(stmt->enumerable()); |
| 1242 | Visit(stmt->body()); |
| 1243 | } |
| 1244 | |
| 1245 | |
| 1246 | void JsonAstBuilder::VisitTryCatchStatement(TryCatchStatement* stmt) { |
| 1247 | TagScope tag(this, "TryCatchStatement"); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1248 | { AttributesScope attributes(this); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1249 | AddAttribute("variable", stmt->variable()->name()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 1250 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1251 | Visit(stmt->try_block()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1252 | Visit(stmt->catch_block()); |
| 1253 | } |
| 1254 | |
| 1255 | |
| 1256 | void JsonAstBuilder::VisitTryFinallyStatement(TryFinallyStatement* stmt) { |
| 1257 | TagScope tag(this, "TryFinallyStatement"); |
| 1258 | Visit(stmt->try_block()); |
| 1259 | Visit(stmt->finally_block()); |
| 1260 | } |
| 1261 | |
| 1262 | |
| 1263 | void JsonAstBuilder::VisitDebuggerStatement(DebuggerStatement* stmt) { |
| 1264 | TagScope tag(this, "DebuggerStatement"); |
| 1265 | } |
| 1266 | |
| 1267 | |
| 1268 | void JsonAstBuilder::VisitFunctionLiteral(FunctionLiteral* expr) { |
| 1269 | TagScope tag(this, "FunctionLiteral"); |
| 1270 | { |
| 1271 | AttributesScope attributes(this); |
| 1272 | AddAttribute("name", expr->name()); |
| 1273 | } |
| 1274 | VisitDeclarations(expr->scope()->declarations()); |
| 1275 | VisitStatements(expr->body()); |
| 1276 | } |
| 1277 | |
| 1278 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 1279 | void JsonAstBuilder::VisitSharedFunctionInfoLiteral( |
| 1280 | SharedFunctionInfoLiteral* expr) { |
| 1281 | TagScope tag(this, "SharedFunctionInfoLiteral"); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | |
| 1285 | void JsonAstBuilder::VisitConditional(Conditional* expr) { |
| 1286 | TagScope tag(this, "Conditional"); |
| 1287 | } |
| 1288 | |
| 1289 | |
| 1290 | void JsonAstBuilder::VisitSlot(Slot* expr) { |
| 1291 | TagScope tag(this, "Slot"); |
| 1292 | { |
| 1293 | AttributesScope attributes(this); |
| 1294 | switch (expr->type()) { |
| 1295 | case Slot::PARAMETER: |
| 1296 | AddAttribute("type", "PARAMETER"); |
| 1297 | break; |
| 1298 | case Slot::LOCAL: |
| 1299 | AddAttribute("type", "LOCAL"); |
| 1300 | break; |
| 1301 | case Slot::CONTEXT: |
| 1302 | AddAttribute("type", "CONTEXT"); |
| 1303 | break; |
| 1304 | case Slot::LOOKUP: |
| 1305 | AddAttribute("type", "LOOKUP"); |
| 1306 | break; |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1307 | } |
| 1308 | AddAttribute("index", expr->index()); |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | |
| 1313 | void JsonAstBuilder::VisitVariableProxy(VariableProxy* expr) { |
| 1314 | if (expr->var()->rewrite() == NULL) { |
| 1315 | TagScope tag(this, "VariableProxy"); |
| 1316 | { |
| 1317 | AttributesScope attributes(this); |
| 1318 | AddAttribute("name", expr->name()); |
| 1319 | AddAttribute("mode", Variable::Mode2String(expr->var()->mode())); |
| 1320 | } |
| 1321 | } else { |
| 1322 | Visit(expr->var()->rewrite()); |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | |
| 1327 | void JsonAstBuilder::VisitLiteral(Literal* expr) { |
| 1328 | TagScope tag(this, "Literal"); |
| 1329 | { |
| 1330 | AttributesScope attributes(this); |
| 1331 | Handle<Object> handle = expr->handle(); |
| 1332 | if (handle->IsString()) { |
| 1333 | AddAttribute("handle", Handle<String>(String::cast(*handle))); |
| 1334 | } else if (handle->IsSmi()) { |
| 1335 | AddAttribute("handle", Smi::cast(*handle)->value()); |
| 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | |
| 1341 | void JsonAstBuilder::VisitRegExpLiteral(RegExpLiteral* expr) { |
| 1342 | TagScope tag(this, "RegExpLiteral"); |
| 1343 | } |
| 1344 | |
| 1345 | |
| 1346 | void JsonAstBuilder::VisitObjectLiteral(ObjectLiteral* expr) { |
| 1347 | TagScope tag(this, "ObjectLiteral"); |
| 1348 | } |
| 1349 | |
| 1350 | |
| 1351 | void JsonAstBuilder::VisitArrayLiteral(ArrayLiteral* expr) { |
| 1352 | TagScope tag(this, "ArrayLiteral"); |
| 1353 | } |
| 1354 | |
| 1355 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1356 | void JsonAstBuilder::VisitAssignment(Assignment* expr) { |
| 1357 | TagScope tag(this, "Assignment"); |
| 1358 | { |
| 1359 | AttributesScope attributes(this); |
| 1360 | AddAttribute("op", Token::Name(expr->op())); |
| 1361 | } |
| 1362 | Visit(expr->target()); |
| 1363 | Visit(expr->value()); |
| 1364 | } |
| 1365 | |
| 1366 | |
| 1367 | void JsonAstBuilder::VisitThrow(Throw* expr) { |
| 1368 | TagScope tag(this, "Throw"); |
| 1369 | Visit(expr->exception()); |
| 1370 | } |
| 1371 | |
| 1372 | |
| 1373 | void JsonAstBuilder::VisitProperty(Property* expr) { |
| 1374 | TagScope tag(this, "Property"); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1375 | Visit(expr->obj()); |
| 1376 | Visit(expr->key()); |
| 1377 | } |
| 1378 | |
| 1379 | |
| 1380 | void JsonAstBuilder::VisitCall(Call* expr) { |
| 1381 | TagScope tag(this, "Call"); |
| 1382 | Visit(expr->expression()); |
| 1383 | VisitExpressions(expr->arguments()); |
| 1384 | } |
| 1385 | |
| 1386 | |
| 1387 | void JsonAstBuilder::VisitCallNew(CallNew* expr) { |
| 1388 | TagScope tag(this, "CallNew"); |
| 1389 | Visit(expr->expression()); |
| 1390 | VisitExpressions(expr->arguments()); |
| 1391 | } |
| 1392 | |
| 1393 | |
| 1394 | void JsonAstBuilder::VisitCallRuntime(CallRuntime* expr) { |
| 1395 | TagScope tag(this, "CallRuntime"); |
| 1396 | { |
| 1397 | AttributesScope attributes(this); |
| 1398 | AddAttribute("name", expr->name()); |
| 1399 | } |
| 1400 | VisitExpressions(expr->arguments()); |
| 1401 | } |
| 1402 | |
| 1403 | |
| 1404 | void JsonAstBuilder::VisitUnaryOperation(UnaryOperation* expr) { |
| 1405 | TagScope tag(this, "UnaryOperation"); |
| 1406 | { |
| 1407 | AttributesScope attributes(this); |
| 1408 | AddAttribute("op", Token::Name(expr->op())); |
| 1409 | } |
| 1410 | Visit(expr->expression()); |
| 1411 | } |
| 1412 | |
| 1413 | |
| 1414 | void JsonAstBuilder::VisitCountOperation(CountOperation* expr) { |
| 1415 | TagScope tag(this, "CountOperation"); |
| 1416 | { |
| 1417 | AttributesScope attributes(this); |
| 1418 | AddAttribute("is_prefix", expr->is_prefix()); |
| 1419 | AddAttribute("op", Token::Name(expr->op())); |
| 1420 | } |
| 1421 | Visit(expr->expression()); |
| 1422 | } |
| 1423 | |
| 1424 | |
| 1425 | void JsonAstBuilder::VisitBinaryOperation(BinaryOperation* expr) { |
| 1426 | TagScope tag(this, "BinaryOperation"); |
| 1427 | { |
| 1428 | AttributesScope attributes(this); |
| 1429 | AddAttribute("op", Token::Name(expr->op())); |
| 1430 | } |
| 1431 | Visit(expr->left()); |
| 1432 | Visit(expr->right()); |
| 1433 | } |
| 1434 | |
| 1435 | |
| 1436 | void JsonAstBuilder::VisitCompareOperation(CompareOperation* expr) { |
| 1437 | TagScope tag(this, "CompareOperation"); |
| 1438 | { |
| 1439 | AttributesScope attributes(this); |
| 1440 | AddAttribute("op", Token::Name(expr->op())); |
| 1441 | } |
| 1442 | Visit(expr->left()); |
| 1443 | Visit(expr->right()); |
| 1444 | } |
| 1445 | |
| 1446 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 1447 | void JsonAstBuilder::VisitCompareToNull(CompareToNull* expr) { |
| 1448 | TagScope tag(this, "CompareToNull"); |
| 1449 | { |
| 1450 | AttributesScope attributes(this); |
| 1451 | AddAttribute("is_strict", expr->is_strict()); |
| 1452 | } |
| 1453 | Visit(expr->expression()); |
| 1454 | } |
| 1455 | |
| 1456 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1457 | void JsonAstBuilder::VisitThisFunction(ThisFunction* expr) { |
| 1458 | TagScope tag(this, "ThisFunction"); |
| 1459 | } |
| 1460 | |
| 1461 | |
| 1462 | void JsonAstBuilder::VisitDeclaration(Declaration* decl) { |
| 1463 | TagScope tag(this, "Declaration"); |
| 1464 | { |
| 1465 | AttributesScope attributes(this); |
| 1466 | AddAttribute("mode", Variable::Mode2String(decl->mode())); |
| 1467 | } |
| 1468 | Visit(decl->proxy()); |
| 1469 | if (decl->fun() != NULL) Visit(decl->fun()); |
| 1470 | } |
| 1471 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1472 | |
| 1473 | #endif // DEBUG |
| 1474 | |
| 1475 | } } // namespace v8::internal |