yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 1 | // Copyright 2012 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; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 45 | InitializeAstVisitor(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | |
| 49 | PrettyPrinter::~PrettyPrinter() { |
| 50 | DeleteArray(output_); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | void PrettyPrinter::VisitBlock(Block* node) { |
| 55 | if (!node->is_initializer_block()) Print("{ "); |
| 56 | PrintStatements(node->statements()); |
| 57 | if (node->statements()->length() > 0) Print(" "); |
| 58 | if (!node->is_initializer_block()) Print("}"); |
| 59 | } |
| 60 | |
| 61 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 62 | void PrettyPrinter::VisitVariableDeclaration(VariableDeclaration* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 63 | Print("var "); |
| 64 | PrintLiteral(node->proxy()->name(), false); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 65 | Print(";"); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | void PrettyPrinter::VisitFunctionDeclaration(FunctionDeclaration* node) { |
| 70 | Print("function "); |
| 71 | PrintLiteral(node->proxy()->name(), false); |
| 72 | Print(" = "); |
| 73 | PrintFunctionLiteral(node->fun()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 74 | Print(";"); |
| 75 | } |
| 76 | |
| 77 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 78 | void PrettyPrinter::VisitModuleDeclaration(ModuleDeclaration* node) { |
| 79 | Print("module "); |
| 80 | PrintLiteral(node->proxy()->name(), false); |
| 81 | Print(" = "); |
| 82 | Visit(node->module()); |
| 83 | Print(";"); |
| 84 | } |
| 85 | |
| 86 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 87 | void PrettyPrinter::VisitImportDeclaration(ImportDeclaration* node) { |
| 88 | Print("import "); |
| 89 | PrintLiteral(node->proxy()->name(), false); |
| 90 | Print(" from "); |
| 91 | Visit(node->module()); |
| 92 | Print(";"); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | void PrettyPrinter::VisitExportDeclaration(ExportDeclaration* node) { |
| 97 | Print("export "); |
| 98 | PrintLiteral(node->proxy()->name(), false); |
| 99 | Print(";"); |
| 100 | } |
| 101 | |
| 102 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 103 | void PrettyPrinter::VisitModuleLiteral(ModuleLiteral* node) { |
| 104 | VisitBlock(node->body()); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | void PrettyPrinter::VisitModuleVariable(ModuleVariable* node) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 109 | Visit(node->proxy()); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
| 113 | void PrettyPrinter::VisitModulePath(ModulePath* node) { |
| 114 | Visit(node->module()); |
| 115 | Print("."); |
| 116 | PrintLiteral(node->name(), false); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | void PrettyPrinter::VisitModuleUrl(ModuleUrl* node) { |
| 121 | Print("at "); |
| 122 | PrintLiteral(node->url(), true); |
| 123 | } |
| 124 | |
| 125 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 126 | void PrettyPrinter::VisitModuleStatement(ModuleStatement* node) { |
| 127 | Print("module "); |
| 128 | PrintLiteral(node->proxy()->name(), false); |
| 129 | Print(" "); |
| 130 | Visit(node->body()); |
| 131 | } |
| 132 | |
| 133 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 134 | void PrettyPrinter::VisitExpressionStatement(ExpressionStatement* node) { |
| 135 | Visit(node->expression()); |
| 136 | Print(";"); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | void PrettyPrinter::VisitEmptyStatement(EmptyStatement* node) { |
| 141 | Print(";"); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | void PrettyPrinter::VisitIfStatement(IfStatement* node) { |
| 146 | Print("if ("); |
| 147 | Visit(node->condition()); |
| 148 | Print(") "); |
| 149 | Visit(node->then_statement()); |
| 150 | if (node->HasElseStatement()) { |
| 151 | Print(" else "); |
| 152 | Visit(node->else_statement()); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | |
| 157 | void PrettyPrinter::VisitContinueStatement(ContinueStatement* node) { |
| 158 | Print("continue"); |
| 159 | ZoneStringList* labels = node->target()->labels(); |
| 160 | if (labels != NULL) { |
| 161 | Print(" "); |
| 162 | ASSERT(labels->length() > 0); // guaranteed to have at least one entry |
| 163 | PrintLiteral(labels->at(0), false); // any label from the list is fine |
| 164 | } |
| 165 | Print(";"); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | void PrettyPrinter::VisitBreakStatement(BreakStatement* node) { |
| 170 | Print("break"); |
| 171 | ZoneStringList* labels = node->target()->labels(); |
| 172 | if (labels != NULL) { |
| 173 | Print(" "); |
| 174 | ASSERT(labels->length() > 0); // guaranteed to have at least one entry |
| 175 | PrintLiteral(labels->at(0), false); // any label from the list is fine |
| 176 | } |
| 177 | Print(";"); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | void PrettyPrinter::VisitReturnStatement(ReturnStatement* node) { |
| 182 | Print("return "); |
| 183 | Visit(node->expression()); |
| 184 | Print(";"); |
| 185 | } |
| 186 | |
| 187 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 188 | void PrettyPrinter::VisitWithStatement(WithStatement* node) { |
| 189 | Print("with ("); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 190 | Visit(node->expression()); |
| 191 | Print(") "); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 192 | Visit(node->statement()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 196 | void PrettyPrinter::VisitSwitchStatement(SwitchStatement* node) { |
| 197 | PrintLabels(node->labels()); |
| 198 | Print("switch ("); |
| 199 | Visit(node->tag()); |
| 200 | Print(") { "); |
| 201 | ZoneList<CaseClause*>* cases = node->cases(); |
| 202 | for (int i = 0; i < cases->length(); i++) |
| 203 | PrintCaseClause(cases->at(i)); |
| 204 | Print("}"); |
| 205 | } |
| 206 | |
| 207 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 208 | void PrettyPrinter::VisitDoWhileStatement(DoWhileStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 209 | PrintLabels(node->labels()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 210 | Print("do "); |
| 211 | Visit(node->body()); |
| 212 | Print(" while ("); |
| 213 | Visit(node->cond()); |
| 214 | Print(");"); |
| 215 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 216 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 217 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 218 | void PrettyPrinter::VisitWhileStatement(WhileStatement* node) { |
| 219 | PrintLabels(node->labels()); |
| 220 | Print("while ("); |
| 221 | Visit(node->cond()); |
| 222 | Print(") "); |
| 223 | Visit(node->body()); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | void PrettyPrinter::VisitForStatement(ForStatement* node) { |
| 228 | PrintLabels(node->labels()); |
| 229 | Print("for ("); |
| 230 | if (node->init() != NULL) { |
| 231 | Visit(node->init()); |
| 232 | Print(" "); |
| 233 | } else { |
| 234 | Print("; "); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 235 | } |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 236 | if (node->cond() != NULL) Visit(node->cond()); |
| 237 | Print("; "); |
| 238 | if (node->next() != NULL) { |
| 239 | Visit(node->next()); // prints extra ';', unfortunately |
| 240 | // to fix: should use Expression for next |
| 241 | } |
| 242 | Print(") "); |
| 243 | Visit(node->body()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | |
| 247 | void PrettyPrinter::VisitForInStatement(ForInStatement* node) { |
| 248 | PrintLabels(node->labels()); |
| 249 | Print("for ("); |
| 250 | Visit(node->each()); |
| 251 | Print(" in "); |
| 252 | Visit(node->enumerable()); |
| 253 | Print(") "); |
| 254 | Visit(node->body()); |
| 255 | } |
| 256 | |
| 257 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 258 | void PrettyPrinter::VisitTryCatchStatement(TryCatchStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 259 | Print("try "); |
| 260 | Visit(node->try_block()); |
| 261 | Print(" catch ("); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 262 | const bool quote = false; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 263 | PrintLiteral(node->variable()->name(), quote); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 264 | Print(") "); |
| 265 | Visit(node->catch_block()); |
| 266 | } |
| 267 | |
| 268 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 269 | void PrettyPrinter::VisitTryFinallyStatement(TryFinallyStatement* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 270 | Print("try "); |
| 271 | Visit(node->try_block()); |
| 272 | Print(" finally "); |
| 273 | Visit(node->finally_block()); |
| 274 | } |
| 275 | |
| 276 | |
| 277 | void PrettyPrinter::VisitDebuggerStatement(DebuggerStatement* node) { |
| 278 | Print("debugger "); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | void PrettyPrinter::VisitFunctionLiteral(FunctionLiteral* node) { |
| 283 | Print("("); |
| 284 | PrintFunctionLiteral(node); |
| 285 | Print(")"); |
| 286 | } |
| 287 | |
| 288 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 289 | void PrettyPrinter::VisitSharedFunctionInfoLiteral( |
| 290 | SharedFunctionInfoLiteral* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 291 | Print("("); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 292 | PrintLiteral(node->shared_function_info(), true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 293 | Print(")"); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | void PrettyPrinter::VisitConditional(Conditional* node) { |
| 298 | Visit(node->condition()); |
| 299 | Print(" ? "); |
| 300 | Visit(node->then_expression()); |
| 301 | Print(" : "); |
| 302 | Visit(node->else_expression()); |
| 303 | } |
| 304 | |
| 305 | |
| 306 | void PrettyPrinter::VisitLiteral(Literal* node) { |
| 307 | PrintLiteral(node->handle(), true); |
| 308 | } |
| 309 | |
| 310 | |
| 311 | void PrettyPrinter::VisitRegExpLiteral(RegExpLiteral* node) { |
| 312 | Print(" RegExp("); |
| 313 | PrintLiteral(node->pattern(), false); |
| 314 | Print(","); |
| 315 | PrintLiteral(node->flags(), false); |
| 316 | Print(") "); |
| 317 | } |
| 318 | |
| 319 | |
| 320 | void PrettyPrinter::VisitObjectLiteral(ObjectLiteral* node) { |
| 321 | Print("{ "); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 322 | for (int i = 0; i < node->properties()->length(); i++) { |
| 323 | if (i != 0) Print(","); |
| 324 | ObjectLiteral::Property* property = node->properties()->at(i); |
| 325 | Print(" "); |
| 326 | Visit(property->key()); |
| 327 | Print(": "); |
| 328 | Visit(property->value()); |
| 329 | } |
| 330 | Print(" }"); |
| 331 | } |
| 332 | |
| 333 | |
| 334 | void PrettyPrinter::VisitArrayLiteral(ArrayLiteral* node) { |
| 335 | Print("[ "); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 336 | for (int i = 0; i < node->values()->length(); i++) { |
| 337 | if (i != 0) Print(","); |
| 338 | Visit(node->values()->at(i)); |
| 339 | } |
| 340 | Print(" ]"); |
| 341 | } |
| 342 | |
| 343 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 344 | void PrettyPrinter::VisitVariableProxy(VariableProxy* node) { |
| 345 | PrintLiteral(node->name(), false); |
| 346 | } |
| 347 | |
| 348 | |
| 349 | void PrettyPrinter::VisitAssignment(Assignment* node) { |
| 350 | Visit(node->target()); |
| 351 | Print(" %s ", Token::String(node->op())); |
| 352 | Visit(node->value()); |
| 353 | } |
| 354 | |
| 355 | |
| 356 | void PrettyPrinter::VisitThrow(Throw* node) { |
| 357 | Print("throw "); |
| 358 | Visit(node->exception()); |
| 359 | } |
| 360 | |
| 361 | |
| 362 | void PrettyPrinter::VisitProperty(Property* node) { |
| 363 | Expression* key = node->key(); |
| 364 | Literal* literal = key->AsLiteral(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 365 | if (literal != NULL && literal->handle()->IsInternalizedString()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 366 | Print("("); |
| 367 | Visit(node->obj()); |
| 368 | Print(")."); |
| 369 | PrintLiteral(literal->handle(), false); |
| 370 | } else { |
| 371 | Visit(node->obj()); |
| 372 | Print("["); |
| 373 | Visit(key); |
| 374 | Print("]"); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | |
| 379 | void PrettyPrinter::VisitCall(Call* node) { |
| 380 | Visit(node->expression()); |
| 381 | PrintArguments(node->arguments()); |
| 382 | } |
| 383 | |
| 384 | |
| 385 | void PrettyPrinter::VisitCallNew(CallNew* node) { |
| 386 | Print("new ("); |
| 387 | Visit(node->expression()); |
| 388 | Print(")"); |
| 389 | PrintArguments(node->arguments()); |
| 390 | } |
| 391 | |
| 392 | |
| 393 | void PrettyPrinter::VisitCallRuntime(CallRuntime* node) { |
| 394 | Print("%%"); |
| 395 | PrintLiteral(node->name(), false); |
| 396 | PrintArguments(node->arguments()); |
| 397 | } |
| 398 | |
| 399 | |
| 400 | void PrettyPrinter::VisitUnaryOperation(UnaryOperation* node) { |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 401 | Token::Value op = node->op(); |
| 402 | bool needsSpace = |
| 403 | op == Token::DELETE || op == Token::TYPEOF || op == Token::VOID; |
| 404 | Print("(%s%s", Token::String(op), needsSpace ? " " : ""); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 405 | Visit(node->expression()); |
| 406 | Print(")"); |
| 407 | } |
| 408 | |
| 409 | |
| 410 | void PrettyPrinter::VisitCountOperation(CountOperation* node) { |
| 411 | Print("("); |
| 412 | if (node->is_prefix()) Print("%s", Token::String(node->op())); |
| 413 | Visit(node->expression()); |
| 414 | if (node->is_postfix()) Print("%s", Token::String(node->op())); |
| 415 | Print(")"); |
| 416 | } |
| 417 | |
| 418 | |
| 419 | void PrettyPrinter::VisitBinaryOperation(BinaryOperation* node) { |
| 420 | Print("("); |
| 421 | Visit(node->left()); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 422 | Print(" %s ", Token::String(node->op())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 423 | Visit(node->right()); |
| 424 | Print(")"); |
| 425 | } |
| 426 | |
| 427 | |
| 428 | void PrettyPrinter::VisitCompareOperation(CompareOperation* node) { |
| 429 | Print("("); |
| 430 | Visit(node->left()); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 431 | Print(" %s ", Token::String(node->op())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 432 | Visit(node->right()); |
| 433 | Print(")"); |
| 434 | } |
| 435 | |
| 436 | |
| 437 | void PrettyPrinter::VisitThisFunction(ThisFunction* node) { |
| 438 | Print("<this-function>"); |
| 439 | } |
| 440 | |
| 441 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 442 | const char* PrettyPrinter::Print(AstNode* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 443 | Init(); |
| 444 | Visit(node); |
| 445 | return output_; |
| 446 | } |
| 447 | |
| 448 | |
| 449 | const char* PrettyPrinter::PrintExpression(FunctionLiteral* program) { |
| 450 | Init(); |
| 451 | ExpressionStatement* statement = |
| 452 | program->body()->at(0)->AsExpressionStatement(); |
| 453 | Visit(statement->expression()); |
| 454 | return output_; |
| 455 | } |
| 456 | |
| 457 | |
| 458 | const char* PrettyPrinter::PrintProgram(FunctionLiteral* program) { |
| 459 | Init(); |
| 460 | PrintStatements(program->body()); |
| 461 | Print("\n"); |
| 462 | return output_; |
| 463 | } |
| 464 | |
| 465 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 466 | void PrettyPrinter::PrintOut(AstNode* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 467 | PrettyPrinter printer; |
| 468 | PrintF("%s", printer.Print(node)); |
| 469 | } |
| 470 | |
| 471 | |
| 472 | void PrettyPrinter::Init() { |
| 473 | if (size_ == 0) { |
| 474 | ASSERT(output_ == NULL); |
| 475 | const int initial_size = 256; |
| 476 | output_ = NewArray<char>(initial_size); |
| 477 | size_ = initial_size; |
| 478 | } |
| 479 | output_[0] = '\0'; |
| 480 | pos_ = 0; |
| 481 | } |
| 482 | |
| 483 | |
| 484 | void PrettyPrinter::Print(const char* format, ...) { |
| 485 | for (;;) { |
| 486 | va_list arguments; |
| 487 | va_start(arguments, format); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 488 | int n = OS::VSNPrintF(Vector<char>(output_, size_) + pos_, |
| 489 | format, |
| 490 | arguments); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 491 | va_end(arguments); |
| 492 | |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 493 | if (n >= 0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 494 | // there was enough space - we are done |
| 495 | pos_ += n; |
| 496 | return; |
| 497 | } else { |
| 498 | // there was not enough space - allocate more and try again |
| 499 | const int slack = 32; |
| 500 | int new_size = size_ + (size_ >> 1) + slack; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 501 | char* new_output = NewArray<char>(new_size); |
| 502 | memcpy(new_output, output_, pos_); |
| 503 | DeleteArray(output_); |
| 504 | output_ = new_output; |
| 505 | size_ = new_size; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | |
| 511 | void PrettyPrinter::PrintStatements(ZoneList<Statement*>* statements) { |
ricow@chromium.org | 7ad6522 | 2011-12-19 12:13:11 +0000 | [diff] [blame] | 512 | if (statements == NULL) return; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 513 | for (int i = 0; i < statements->length(); i++) { |
| 514 | if (i != 0) Print(" "); |
| 515 | Visit(statements->at(i)); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | |
| 520 | void PrettyPrinter::PrintLabels(ZoneStringList* labels) { |
| 521 | if (labels != NULL) { |
| 522 | for (int i = 0; i < labels->length(); i++) { |
| 523 | PrintLiteral(labels->at(i), false); |
| 524 | Print(": "); |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | |
| 530 | void PrettyPrinter::PrintArguments(ZoneList<Expression*>* arguments) { |
| 531 | Print("("); |
| 532 | for (int i = 0; i < arguments->length(); i++) { |
| 533 | if (i != 0) Print(", "); |
| 534 | Visit(arguments->at(i)); |
| 535 | } |
| 536 | Print(")"); |
| 537 | } |
| 538 | |
| 539 | |
| 540 | void PrettyPrinter::PrintLiteral(Handle<Object> value, bool quote) { |
| 541 | Object* object = *value; |
| 542 | if (object->IsString()) { |
| 543 | String* string = String::cast(object); |
| 544 | if (quote) Print("\""); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 545 | for (int i = 0; i < string->length(); i++) { |
| 546 | Print("%c", string->Get(i)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 547 | } |
| 548 | if (quote) Print("\""); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 549 | } else if (object->IsNull()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 550 | Print("null"); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 551 | } else if (object->IsTrue()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 552 | Print("true"); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 553 | } else if (object->IsFalse()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 554 | Print("false"); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 555 | } else if (object->IsUndefined()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 556 | Print("undefined"); |
| 557 | } else if (object->IsNumber()) { |
| 558 | Print("%g", object->Number()); |
| 559 | } else if (object->IsJSObject()) { |
| 560 | // regular expression |
| 561 | if (object->IsJSFunction()) { |
| 562 | Print("JS-Function"); |
| 563 | } else if (object->IsJSArray()) { |
| 564 | Print("JS-array[%u]", JSArray::cast(object)->length()); |
| 565 | } else if (object->IsJSObject()) { |
| 566 | Print("JS-Object"); |
| 567 | } else { |
| 568 | Print("?UNKNOWN?"); |
| 569 | } |
| 570 | } else if (object->IsFixedArray()) { |
| 571 | Print("FixedArray"); |
| 572 | } else { |
| 573 | Print("<unknown literal %p>", object); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | |
| 578 | void PrettyPrinter::PrintParameters(Scope* scope) { |
| 579 | Print("("); |
| 580 | for (int i = 0; i < scope->num_parameters(); i++) { |
| 581 | if (i > 0) Print(", "); |
| 582 | PrintLiteral(scope->parameter(i)->name(), false); |
| 583 | } |
| 584 | Print(")"); |
| 585 | } |
| 586 | |
| 587 | |
| 588 | void PrettyPrinter::PrintDeclarations(ZoneList<Declaration*>* declarations) { |
| 589 | for (int i = 0; i < declarations->length(); i++) { |
| 590 | if (i > 0) Print(" "); |
| 591 | Visit(declarations->at(i)); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | |
| 596 | void PrettyPrinter::PrintFunctionLiteral(FunctionLiteral* function) { |
| 597 | Print("function "); |
| 598 | PrintLiteral(function->name(), false); |
| 599 | PrintParameters(function->scope()); |
| 600 | Print(" { "); |
| 601 | PrintDeclarations(function->scope()->declarations()); |
| 602 | PrintStatements(function->body()); |
| 603 | Print(" }"); |
| 604 | } |
| 605 | |
| 606 | |
| 607 | void PrettyPrinter::PrintCaseClause(CaseClause* clause) { |
| 608 | if (clause->is_default()) { |
| 609 | Print("default"); |
| 610 | } else { |
| 611 | Print("case "); |
| 612 | Visit(clause->label()); |
| 613 | } |
| 614 | Print(": "); |
| 615 | PrintStatements(clause->statements()); |
| 616 | if (clause->statements()->length() > 0) |
| 617 | Print(" "); |
| 618 | } |
| 619 | |
| 620 | |
| 621 | //----------------------------------------------------------------------------- |
| 622 | |
| 623 | class IndentedScope BASE_EMBEDDED { |
| 624 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 625 | explicit IndentedScope(AstPrinter* printer) : ast_printer_(printer) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 626 | ast_printer_->inc_indent(); |
| 627 | } |
| 628 | |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 629 | IndentedScope(AstPrinter* printer, const char* txt, AstNode* node = NULL) |
| 630 | : ast_printer_(printer) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 631 | ast_printer_->PrintIndented(txt); |
| 632 | ast_printer_->Print("\n"); |
| 633 | ast_printer_->inc_indent(); |
| 634 | } |
| 635 | |
| 636 | virtual ~IndentedScope() { |
| 637 | ast_printer_->dec_indent(); |
| 638 | } |
| 639 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 640 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 641 | AstPrinter* ast_printer_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 642 | }; |
| 643 | |
| 644 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 645 | //----------------------------------------------------------------------------- |
| 646 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 647 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 648 | AstPrinter::AstPrinter() : indent_(0) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | |
| 652 | AstPrinter::~AstPrinter() { |
| 653 | ASSERT(indent_ == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | |
| 657 | void AstPrinter::PrintIndented(const char* txt) { |
| 658 | for (int i = 0; i < indent_; i++) { |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 659 | Print(". "); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 660 | } |
| 661 | Print(txt); |
| 662 | } |
| 663 | |
| 664 | |
| 665 | void AstPrinter::PrintLiteralIndented(const char* info, |
| 666 | Handle<Object> value, |
| 667 | bool quote) { |
| 668 | PrintIndented(info); |
| 669 | Print(" "); |
| 670 | PrintLiteral(value, quote); |
| 671 | Print("\n"); |
| 672 | } |
| 673 | |
| 674 | |
| 675 | void AstPrinter::PrintLiteralWithModeIndented(const char* info, |
| 676 | Variable* var, |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 677 | Handle<Object> value) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 678 | if (var == NULL) { |
| 679 | PrintLiteralIndented(info, value, true); |
| 680 | } else { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 681 | EmbeddedVector<char, 256> buf; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 682 | int pos = OS::SNPrintF(buf, "%s (mode = %s", info, |
| 683 | Variable::Mode2String(var->mode())); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 684 | OS::SNPrintF(buf + pos, ")"); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 685 | PrintLiteralIndented(buf.start(), value, true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
| 689 | |
| 690 | void AstPrinter::PrintLabelsIndented(const char* info, ZoneStringList* labels) { |
| 691 | if (labels != NULL && labels->length() > 0) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 692 | PrintIndented(info == NULL ? "LABELS" : info); |
| 693 | Print(" "); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 694 | PrintLabels(labels); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 695 | Print("\n"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 696 | } else if (info != NULL) { |
| 697 | PrintIndented(info); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 698 | Print("\n"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
| 702 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 703 | void AstPrinter::PrintIndentedVisit(const char* s, AstNode* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 704 | IndentedScope indent(this, s, node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 705 | Visit(node); |
| 706 | } |
| 707 | |
| 708 | |
| 709 | const char* AstPrinter::PrintProgram(FunctionLiteral* program) { |
| 710 | Init(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 711 | { IndentedScope indent(this, "FUNC"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 712 | PrintLiteralIndented("NAME", program->name(), true); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 713 | PrintLiteralIndented("INFERRED NAME", program->inferred_name(), true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 714 | PrintParameters(program->scope()); |
| 715 | PrintDeclarations(program->scope()->declarations()); |
| 716 | PrintStatements(program->body()); |
| 717 | } |
| 718 | return Output(); |
| 719 | } |
| 720 | |
| 721 | |
| 722 | void AstPrinter::PrintDeclarations(ZoneList<Declaration*>* declarations) { |
| 723 | if (declarations->length() > 0) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 724 | IndentedScope indent(this, "DECLS"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 725 | for (int i = 0; i < declarations->length(); i++) { |
| 726 | Visit(declarations->at(i)); |
| 727 | } |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | |
| 732 | void AstPrinter::PrintParameters(Scope* scope) { |
| 733 | if (scope->num_parameters() > 0) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 734 | IndentedScope indent(this, "PARAMS"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 735 | for (int i = 0; i < scope->num_parameters(); i++) { |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 736 | PrintLiteralWithModeIndented("VAR", scope->parameter(i), |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 737 | scope->parameter(i)->name()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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()) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 759 | IndentedScope indent(this, "DEFAULT"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 760 | PrintStatements(clause->statements()); |
| 761 | } else { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 762 | IndentedScope indent(this, "CASE"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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"; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 771 | IndentedScope indent(this, block_txt); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 772 | PrintStatements(node->statements()); |
| 773 | } |
| 774 | |
| 775 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 776 | void AstPrinter::VisitVariableDeclaration(VariableDeclaration* node) { |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 777 | PrintLiteralWithModeIndented(Variable::Mode2String(node->mode()), |
| 778 | node->proxy()->var(), |
| 779 | node->proxy()->name()); |
| 780 | } |
| 781 | |
| 782 | |
| 783 | void AstPrinter::VisitFunctionDeclaration(FunctionDeclaration* node) { |
| 784 | PrintIndented("FUNCTION "); |
| 785 | PrintLiteral(node->proxy()->name(), true); |
| 786 | Print(" = function "); |
| 787 | PrintLiteral(node->fun()->name(), false); |
| 788 | Print("\n"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 792 | void AstPrinter::VisitModuleDeclaration(ModuleDeclaration* node) { |
| 793 | IndentedScope indent(this, "MODULE"); |
| 794 | PrintLiteralIndented("NAME", node->proxy()->name(), true); |
| 795 | Visit(node->module()); |
| 796 | } |
| 797 | |
| 798 | |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 799 | void AstPrinter::VisitImportDeclaration(ImportDeclaration* node) { |
| 800 | IndentedScope indent(this, "IMPORT"); |
| 801 | PrintLiteralIndented("NAME", node->proxy()->name(), true); |
| 802 | Visit(node->module()); |
| 803 | } |
| 804 | |
| 805 | |
| 806 | void AstPrinter::VisitExportDeclaration(ExportDeclaration* node) { |
| 807 | IndentedScope indent(this, "EXPORT "); |
| 808 | PrintLiteral(node->proxy()->name(), true); |
| 809 | } |
| 810 | |
| 811 | |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 812 | void AstPrinter::VisitModuleLiteral(ModuleLiteral* node) { |
| 813 | VisitBlock(node->body()); |
| 814 | } |
| 815 | |
| 816 | |
| 817 | void AstPrinter::VisitModuleVariable(ModuleVariable* node) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 818 | Visit(node->proxy()); |
yangguo@chromium.org | 78d1ad4 | 2012-02-09 13:53:47 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | |
| 822 | void AstPrinter::VisitModulePath(ModulePath* node) { |
| 823 | IndentedScope indent(this, "PATH"); |
| 824 | PrintIndentedVisit("MODULE", node->module()); |
| 825 | PrintLiteralIndented("NAME", node->name(), false); |
| 826 | } |
| 827 | |
| 828 | |
| 829 | void AstPrinter::VisitModuleUrl(ModuleUrl* node) { |
| 830 | PrintLiteralIndented("URL", node->url(), true); |
| 831 | } |
| 832 | |
| 833 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 834 | void AstPrinter::VisitModuleStatement(ModuleStatement* node) { |
| 835 | IndentedScope indent(this, "MODULE"); |
| 836 | PrintLiteralIndented("NAME", node->proxy()->name(), true); |
| 837 | PrintStatements(node->body()->statements()); |
| 838 | } |
| 839 | |
| 840 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 841 | void AstPrinter::VisitExpressionStatement(ExpressionStatement* node) { |
| 842 | Visit(node->expression()); |
| 843 | } |
| 844 | |
| 845 | |
| 846 | void AstPrinter::VisitEmptyStatement(EmptyStatement* node) { |
| 847 | PrintIndented("EMPTY\n"); |
| 848 | } |
| 849 | |
| 850 | |
| 851 | void AstPrinter::VisitIfStatement(IfStatement* node) { |
| 852 | PrintIndentedVisit("IF", node->condition()); |
| 853 | PrintIndentedVisit("THEN", node->then_statement()); |
| 854 | if (node->HasElseStatement()) { |
| 855 | PrintIndentedVisit("ELSE", node->else_statement()); |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | |
| 860 | void AstPrinter::VisitContinueStatement(ContinueStatement* node) { |
| 861 | PrintLabelsIndented("CONTINUE", node->target()->labels()); |
| 862 | } |
| 863 | |
| 864 | |
| 865 | void AstPrinter::VisitBreakStatement(BreakStatement* node) { |
| 866 | PrintLabelsIndented("BREAK", node->target()->labels()); |
| 867 | } |
| 868 | |
| 869 | |
| 870 | void AstPrinter::VisitReturnStatement(ReturnStatement* node) { |
| 871 | PrintIndentedVisit("RETURN", node->expression()); |
| 872 | } |
| 873 | |
| 874 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 875 | void AstPrinter::VisitWithStatement(WithStatement* node) { |
| 876 | IndentedScope indent(this, "WITH"); |
| 877 | PrintIndentedVisit("OBJECT", node->expression()); |
| 878 | PrintIndentedVisit("BODY", node->statement()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 882 | void AstPrinter::VisitSwitchStatement(SwitchStatement* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 883 | IndentedScope indent(this, "SWITCH"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 884 | PrintLabelsIndented(NULL, node->labels()); |
| 885 | PrintIndentedVisit("TAG", node->tag()); |
| 886 | for (int i = 0; i < node->cases()->length(); i++) { |
| 887 | PrintCaseClause(node->cases()->at(i)); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 892 | void AstPrinter::VisitDoWhileStatement(DoWhileStatement* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 893 | IndentedScope indent(this, "DO"); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 894 | PrintLabelsIndented(NULL, node->labels()); |
| 895 | PrintIndentedVisit("BODY", node->body()); |
| 896 | PrintIndentedVisit("COND", node->cond()); |
| 897 | } |
| 898 | |
| 899 | |
| 900 | void AstPrinter::VisitWhileStatement(WhileStatement* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 901 | IndentedScope indent(this, "WHILE"); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 902 | PrintLabelsIndented(NULL, node->labels()); |
| 903 | PrintIndentedVisit("COND", node->cond()); |
| 904 | PrintIndentedVisit("BODY", node->body()); |
| 905 | } |
| 906 | |
| 907 | |
| 908 | void AstPrinter::VisitForStatement(ForStatement* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 909 | IndentedScope indent(this, "FOR"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 910 | PrintLabelsIndented(NULL, node->labels()); |
| 911 | if (node->init()) PrintIndentedVisit("INIT", node->init()); |
| 912 | if (node->cond()) PrintIndentedVisit("COND", node->cond()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 913 | PrintIndentedVisit("BODY", node->body()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 914 | if (node->next()) PrintIndentedVisit("NEXT", node->next()); |
| 915 | } |
| 916 | |
| 917 | |
| 918 | void AstPrinter::VisitForInStatement(ForInStatement* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 919 | IndentedScope indent(this, "FOR IN"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 920 | PrintIndentedVisit("FOR", node->each()); |
| 921 | PrintIndentedVisit("IN", node->enumerable()); |
| 922 | PrintIndentedVisit("BODY", node->body()); |
| 923 | } |
| 924 | |
| 925 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 926 | void AstPrinter::VisitTryCatchStatement(TryCatchStatement* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 927 | IndentedScope indent(this, "TRY CATCH"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 928 | PrintIndentedVisit("TRY", node->try_block()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 929 | PrintLiteralWithModeIndented("CATCHVAR", |
| 930 | node->variable(), |
| 931 | node->variable()->name()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 932 | PrintIndentedVisit("CATCH", node->catch_block()); |
| 933 | } |
| 934 | |
| 935 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 936 | void AstPrinter::VisitTryFinallyStatement(TryFinallyStatement* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 937 | IndentedScope indent(this, "TRY FINALLY"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 938 | PrintIndentedVisit("TRY", node->try_block()); |
| 939 | PrintIndentedVisit("FINALLY", node->finally_block()); |
| 940 | } |
| 941 | |
| 942 | |
| 943 | void AstPrinter::VisitDebuggerStatement(DebuggerStatement* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 944 | IndentedScope indent(this, "DEBUGGER"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | |
| 948 | void AstPrinter::VisitFunctionLiteral(FunctionLiteral* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 949 | IndentedScope indent(this, "FUNC LITERAL"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 950 | PrintLiteralIndented("NAME", node->name(), false); |
kasperl@chromium.org | d1e3e72 | 2009-04-14 13:38:25 +0000 | [diff] [blame] | 951 | PrintLiteralIndented("INFERRED NAME", node->inferred_name(), false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 952 | PrintParameters(node->scope()); |
| 953 | // We don't want to see the function literal in this case: it |
| 954 | // will be printed via PrintProgram when the code for it is |
| 955 | // generated. |
| 956 | // PrintStatements(node->body()); |
| 957 | } |
| 958 | |
| 959 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 960 | void AstPrinter::VisitSharedFunctionInfoLiteral( |
| 961 | SharedFunctionInfoLiteral* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 962 | IndentedScope indent(this, "FUNC LITERAL"); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 963 | PrintLiteralIndented("SHARED INFO", node->shared_function_info(), true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | |
| 967 | void AstPrinter::VisitConditional(Conditional* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 968 | IndentedScope indent(this, "CONDITIONAL"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 969 | PrintIndentedVisit("?", node->condition()); |
| 970 | PrintIndentedVisit("THEN", node->then_expression()); |
| 971 | PrintIndentedVisit("ELSE", node->else_expression()); |
| 972 | } |
| 973 | |
| 974 | |
| 975 | void AstPrinter::VisitLiteral(Literal* node) { |
| 976 | PrintLiteralIndented("LITERAL", node->handle(), true); |
| 977 | } |
| 978 | |
| 979 | |
| 980 | void AstPrinter::VisitRegExpLiteral(RegExpLiteral* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 981 | IndentedScope indent(this, "REGEXP LITERAL"); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 982 | PrintLiteralIndented("PATTERN", node->pattern(), false); |
| 983 | PrintLiteralIndented("FLAGS", node->flags(), false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | |
| 987 | void AstPrinter::VisitObjectLiteral(ObjectLiteral* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 988 | IndentedScope indent(this, "OBJ LITERAL"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 989 | for (int i = 0; i < node->properties()->length(); i++) { |
| 990 | const char* prop_kind = NULL; |
| 991 | switch (node->properties()->at(i)->kind()) { |
| 992 | case ObjectLiteral::Property::CONSTANT: |
| 993 | prop_kind = "PROPERTY - CONSTANT"; |
| 994 | break; |
| 995 | case ObjectLiteral::Property::COMPUTED: |
| 996 | prop_kind = "PROPERTY - COMPUTED"; |
| 997 | break; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 998 | case ObjectLiteral::Property::MATERIALIZED_LITERAL: |
| 999 | prop_kind = "PROPERTY - MATERIALIZED_LITERAL"; |
| 1000 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1001 | case ObjectLiteral::Property::PROTOTYPE: |
| 1002 | prop_kind = "PROPERTY - PROTOTYPE"; |
| 1003 | break; |
| 1004 | case ObjectLiteral::Property::GETTER: |
| 1005 | prop_kind = "PROPERTY - GETTER"; |
| 1006 | break; |
| 1007 | case ObjectLiteral::Property::SETTER: |
| 1008 | prop_kind = "PROPERTY - SETTER"; |
| 1009 | break; |
| 1010 | default: |
| 1011 | UNREACHABLE(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1012 | } |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1013 | IndentedScope prop(this, prop_kind); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1014 | PrintIndentedVisit("KEY", node->properties()->at(i)->key()); |
| 1015 | PrintIndentedVisit("VALUE", node->properties()->at(i)->value()); |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | |
| 1020 | void AstPrinter::VisitArrayLiteral(ArrayLiteral* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1021 | IndentedScope indent(this, "ARRAY LITERAL"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1022 | if (node->values()->length() > 0) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1023 | IndentedScope indent(this, "VALUES"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1024 | for (int i = 0; i < node->values()->length(); i++) { |
| 1025 | Visit(node->values()->at(i)); |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1031 | void AstPrinter::VisitVariableProxy(VariableProxy* node) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1032 | Variable* var = node->var(); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1033 | EmbeddedVector<char, 128> buf; |
| 1034 | int pos = OS::SNPrintF(buf, "VAR PROXY"); |
| 1035 | switch (var->location()) { |
| 1036 | case Variable::UNALLOCATED: |
| 1037 | break; |
| 1038 | case Variable::PARAMETER: |
| 1039 | OS::SNPrintF(buf + pos, " parameter[%d]", var->index()); |
| 1040 | break; |
| 1041 | case Variable::LOCAL: |
| 1042 | OS::SNPrintF(buf + pos, " local[%d]", var->index()); |
| 1043 | break; |
| 1044 | case Variable::CONTEXT: |
| 1045 | OS::SNPrintF(buf + pos, " context[%d]", var->index()); |
| 1046 | break; |
| 1047 | case Variable::LOOKUP: |
| 1048 | OS::SNPrintF(buf + pos, " lookup"); |
| 1049 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1050 | } |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 1051 | PrintLiteralWithModeIndented(buf.start(), var, node->name()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | |
| 1055 | void AstPrinter::VisitAssignment(Assignment* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1056 | IndentedScope indent(this, Token::Name(node->op()), node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1057 | Visit(node->target()); |
| 1058 | Visit(node->value()); |
| 1059 | } |
| 1060 | |
| 1061 | |
| 1062 | void AstPrinter::VisitThrow(Throw* node) { |
| 1063 | PrintIndentedVisit("THROW", node->exception()); |
| 1064 | } |
| 1065 | |
| 1066 | |
| 1067 | void AstPrinter::VisitProperty(Property* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1068 | IndentedScope indent(this, "PROPERTY", node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1069 | Visit(node->obj()); |
| 1070 | Literal* literal = node->key()->AsLiteral(); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame^] | 1071 | if (literal != NULL && literal->handle()->IsInternalizedString()) { |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 1072 | PrintLiteralIndented("NAME", literal->handle(), false); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1073 | } else { |
| 1074 | PrintIndentedVisit("KEY", node->key()); |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | |
| 1079 | void AstPrinter::VisitCall(Call* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1080 | IndentedScope indent(this, "CALL"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1081 | Visit(node->expression()); |
| 1082 | PrintArguments(node->arguments()); |
| 1083 | } |
| 1084 | |
| 1085 | |
| 1086 | void AstPrinter::VisitCallNew(CallNew* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1087 | IndentedScope indent(this, "CALL NEW"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1088 | Visit(node->expression()); |
| 1089 | PrintArguments(node->arguments()); |
| 1090 | } |
| 1091 | |
| 1092 | |
| 1093 | void AstPrinter::VisitCallRuntime(CallRuntime* node) { |
| 1094 | PrintLiteralIndented("CALL RUNTIME ", node->name(), false); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1095 | IndentedScope indent(this); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1096 | PrintArguments(node->arguments()); |
| 1097 | } |
| 1098 | |
| 1099 | |
| 1100 | void AstPrinter::VisitUnaryOperation(UnaryOperation* node) { |
| 1101 | PrintIndentedVisit(Token::Name(node->op()), node->expression()); |
| 1102 | } |
| 1103 | |
| 1104 | |
| 1105 | void AstPrinter::VisitCountOperation(CountOperation* node) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1106 | EmbeddedVector<char, 128> buf; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 1107 | OS::SNPrintF(buf, "%s %s", (node->is_prefix() ? "PRE" : "POST"), |
| 1108 | Token::Name(node->op())); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1109 | PrintIndentedVisit(buf.start(), node->expression()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | |
| 1113 | void AstPrinter::VisitBinaryOperation(BinaryOperation* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1114 | IndentedScope indent(this, Token::Name(node->op()), node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1115 | Visit(node->left()); |
| 1116 | Visit(node->right()); |
| 1117 | } |
| 1118 | |
| 1119 | |
| 1120 | void AstPrinter::VisitCompareOperation(CompareOperation* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1121 | IndentedScope indent(this, Token::Name(node->op()), node); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1122 | Visit(node->left()); |
| 1123 | Visit(node->right()); |
| 1124 | } |
| 1125 | |
| 1126 | |
| 1127 | void AstPrinter::VisitThisFunction(ThisFunction* node) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1128 | IndentedScope indent(this, "THIS-FUNCTION"); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1131 | #endif // DEBUG |
| 1132 | |
| 1133 | } } // namespace v8::internal |