2012-02-08 13:53:24 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
#include "src/ast-value-factory.h"
|
2014-06-30 13:25:46 +00:00
|
|
|
#include "src/base/platform/platform.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/prettyprinter.h"
|
|
|
|
#include "src/scopes.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
2014-01-21 16:22:52 +00:00
|
|
|
PrettyPrinter::PrettyPrinter(Zone* zone) {
|
2008-07-03 15:10:15 +00:00
|
|
|
output_ = NULL;
|
|
|
|
size_ = 0;
|
|
|
|
pos_ = 0;
|
2014-01-21 16:22:52 +00:00
|
|
|
InitializeAstVisitor(zone);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PrettyPrinter::~PrettyPrinter() {
|
|
|
|
DeleteArray(output_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitBlock(Block* node) {
|
|
|
|
if (!node->is_initializer_block()) Print("{ ");
|
|
|
|
PrintStatements(node->statements());
|
|
|
|
if (node->statements()->length() > 0) Print(" ");
|
|
|
|
if (!node->is_initializer_block()) Print("}");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-09 13:39:26 +00:00
|
|
|
void PrettyPrinter::VisitVariableDeclaration(VariableDeclaration* node) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Print("var ");
|
|
|
|
PrintLiteral(node->proxy()->name(), false);
|
2012-02-28 10:12:39 +00:00
|
|
|
Print(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitFunctionDeclaration(FunctionDeclaration* node) {
|
|
|
|
Print("function ");
|
|
|
|
PrintLiteral(node->proxy()->name(), false);
|
|
|
|
Print(" = ");
|
|
|
|
PrintFunctionLiteral(node->fun());
|
2008-07-03 15:10:15 +00:00
|
|
|
Print(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-09 13:40:41 +00:00
|
|
|
void PrettyPrinter::VisitModuleDeclaration(ModuleDeclaration* node) {
|
|
|
|
Print("module ");
|
|
|
|
PrintLiteral(node->proxy()->name(), false);
|
|
|
|
Print(" = ");
|
|
|
|
Visit(node->module());
|
|
|
|
Print(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-29 12:12:52 +00:00
|
|
|
void PrettyPrinter::VisitImportDeclaration(ImportDeclaration* node) {
|
|
|
|
Print("import ");
|
|
|
|
PrintLiteral(node->proxy()->name(), false);
|
|
|
|
Print(" from ");
|
|
|
|
Visit(node->module());
|
|
|
|
Print(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitExportDeclaration(ExportDeclaration* node) {
|
|
|
|
Print("export ");
|
|
|
|
PrintLiteral(node->proxy()->name(), false);
|
|
|
|
Print(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-09 13:40:41 +00:00
|
|
|
void PrettyPrinter::VisitModuleLiteral(ModuleLiteral* node) {
|
|
|
|
VisitBlock(node->body());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitModuleVariable(ModuleVariable* node) {
|
2012-02-20 14:02:59 +00:00
|
|
|
Visit(node->proxy());
|
2012-02-09 13:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitModulePath(ModulePath* node) {
|
|
|
|
Visit(node->module());
|
|
|
|
Print(".");
|
|
|
|
PrintLiteral(node->name(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitModuleUrl(ModuleUrl* node) {
|
|
|
|
Print("at ");
|
|
|
|
PrintLiteral(node->url(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
void PrettyPrinter::VisitModuleStatement(ModuleStatement* node) {
|
|
|
|
Print("module ");
|
|
|
|
PrintLiteral(node->proxy()->name(), false);
|
|
|
|
Print(" ");
|
|
|
|
Visit(node->body());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
void PrettyPrinter::VisitExpressionStatement(ExpressionStatement* node) {
|
|
|
|
Visit(node->expression());
|
|
|
|
Print(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitEmptyStatement(EmptyStatement* node) {
|
|
|
|
Print(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitIfStatement(IfStatement* node) {
|
|
|
|
Print("if (");
|
|
|
|
Visit(node->condition());
|
|
|
|
Print(") ");
|
|
|
|
Visit(node->then_statement());
|
|
|
|
if (node->HasElseStatement()) {
|
|
|
|
Print(" else ");
|
|
|
|
Visit(node->else_statement());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitContinueStatement(ContinueStatement* node) {
|
|
|
|
Print("continue");
|
2014-06-24 14:03:24 +00:00
|
|
|
ZoneList<const AstRawString*>* labels = node->target()->labels();
|
2008-07-03 15:10:15 +00:00
|
|
|
if (labels != NULL) {
|
|
|
|
Print(" ");
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(labels->length() > 0); // guaranteed to have at least one entry
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintLiteral(labels->at(0), false); // any label from the list is fine
|
|
|
|
}
|
|
|
|
Print(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitBreakStatement(BreakStatement* node) {
|
|
|
|
Print("break");
|
2014-06-24 14:03:24 +00:00
|
|
|
ZoneList<const AstRawString*>* labels = node->target()->labels();
|
2008-07-03 15:10:15 +00:00
|
|
|
if (labels != NULL) {
|
|
|
|
Print(" ");
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(labels->length() > 0); // guaranteed to have at least one entry
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintLiteral(labels->at(0), false); // any label from the list is fine
|
|
|
|
}
|
|
|
|
Print(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitReturnStatement(ReturnStatement* node) {
|
|
|
|
Print("return ");
|
|
|
|
Visit(node->expression());
|
|
|
|
Print(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-12 10:52:49 +00:00
|
|
|
void PrettyPrinter::VisitWithStatement(WithStatement* node) {
|
|
|
|
Print("with (");
|
2008-07-03 15:10:15 +00:00
|
|
|
Visit(node->expression());
|
|
|
|
Print(") ");
|
2011-08-12 10:52:49 +00:00
|
|
|
Visit(node->statement());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitSwitchStatement(SwitchStatement* node) {
|
|
|
|
PrintLabels(node->labels());
|
|
|
|
Print("switch (");
|
|
|
|
Visit(node->tag());
|
|
|
|
Print(") { ");
|
|
|
|
ZoneList<CaseClause*>* cases = node->cases();
|
|
|
|
for (int i = 0; i < cases->length(); i++)
|
2013-10-14 11:06:15 +00:00
|
|
|
Visit(cases->at(i));
|
2008-07-03 15:10:15 +00:00
|
|
|
Print("}");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-14 11:06:15 +00:00
|
|
|
void PrettyPrinter::VisitCaseClause(CaseClause* clause) {
|
|
|
|
if (clause->is_default()) {
|
|
|
|
Print("default");
|
|
|
|
} else {
|
|
|
|
Print("case ");
|
|
|
|
Visit(clause->label());
|
|
|
|
}
|
|
|
|
Print(": ");
|
|
|
|
PrintStatements(clause->statements());
|
|
|
|
if (clause->statements()->length() > 0)
|
|
|
|
Print(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-12 13:14:06 +00:00
|
|
|
void PrettyPrinter::VisitDoWhileStatement(DoWhileStatement* node) {
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintLabels(node->labels());
|
2009-10-12 13:14:06 +00:00
|
|
|
Print("do ");
|
|
|
|
Visit(node->body());
|
|
|
|
Print(" while (");
|
|
|
|
Visit(node->cond());
|
|
|
|
Print(");");
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2009-10-12 13:14:06 +00:00
|
|
|
void PrettyPrinter::VisitWhileStatement(WhileStatement* node) {
|
|
|
|
PrintLabels(node->labels());
|
|
|
|
Print("while (");
|
|
|
|
Visit(node->cond());
|
|
|
|
Print(") ");
|
|
|
|
Visit(node->body());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitForStatement(ForStatement* node) {
|
|
|
|
PrintLabels(node->labels());
|
|
|
|
Print("for (");
|
|
|
|
if (node->init() != NULL) {
|
|
|
|
Visit(node->init());
|
|
|
|
Print(" ");
|
|
|
|
} else {
|
|
|
|
Print("; ");
|
|
|
|
}
|
|
|
|
if (node->cond() != NULL) Visit(node->cond());
|
|
|
|
Print("; ");
|
|
|
|
if (node->next() != NULL) {
|
|
|
|
Visit(node->next()); // prints extra ';', unfortunately
|
|
|
|
// to fix: should use Expression for next
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2009-10-12 13:14:06 +00:00
|
|
|
Print(") ");
|
|
|
|
Visit(node->body());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitForInStatement(ForInStatement* node) {
|
|
|
|
PrintLabels(node->labels());
|
|
|
|
Print("for (");
|
|
|
|
Visit(node->each());
|
|
|
|
Print(" in ");
|
|
|
|
Visit(node->enumerable());
|
|
|
|
Print(") ");
|
|
|
|
Visit(node->body());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-06 14:38:26 +00:00
|
|
|
void PrettyPrinter::VisitForOfStatement(ForOfStatement* node) {
|
|
|
|
PrintLabels(node->labels());
|
|
|
|
Print("for (");
|
|
|
|
Visit(node->each());
|
|
|
|
Print(" of ");
|
|
|
|
Visit(node->iterable());
|
|
|
|
Print(") ");
|
|
|
|
Visit(node->body());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-12 13:14:06 +00:00
|
|
|
void PrettyPrinter::VisitTryCatchStatement(TryCatchStatement* node) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Print("try ");
|
|
|
|
Visit(node->try_block());
|
|
|
|
Print(" catch (");
|
2011-06-08 13:55:33 +00:00
|
|
|
const bool quote = false;
|
2011-06-30 14:37:55 +00:00
|
|
|
PrintLiteral(node->variable()->name(), quote);
|
2008-07-03 15:10:15 +00:00
|
|
|
Print(") ");
|
|
|
|
Visit(node->catch_block());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-12 13:14:06 +00:00
|
|
|
void PrettyPrinter::VisitTryFinallyStatement(TryFinallyStatement* node) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Print("try ");
|
|
|
|
Visit(node->try_block());
|
|
|
|
Print(" finally ");
|
|
|
|
Visit(node->finally_block());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitDebuggerStatement(DebuggerStatement* node) {
|
|
|
|
Print("debugger ");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitFunctionLiteral(FunctionLiteral* node) {
|
|
|
|
Print("(");
|
|
|
|
PrintFunctionLiteral(node);
|
|
|
|
Print(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-01 09:47:37 +00:00
|
|
|
void PrettyPrinter::VisitNativeFunctionLiteral(NativeFunctionLiteral* node) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Print("(");
|
2013-10-01 09:47:37 +00:00
|
|
|
PrintLiteral(node->name(), false);
|
2008-07-03 15:10:15 +00:00
|
|
|
Print(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitConditional(Conditional* node) {
|
|
|
|
Visit(node->condition());
|
|
|
|
Print(" ? ");
|
|
|
|
Visit(node->then_expression());
|
|
|
|
Print(" : ");
|
|
|
|
Visit(node->else_expression());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitLiteral(Literal* node) {
|
2013-06-24 10:37:59 +00:00
|
|
|
PrintLiteral(node->value(), true);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitRegExpLiteral(RegExpLiteral* node) {
|
|
|
|
Print(" RegExp(");
|
|
|
|
PrintLiteral(node->pattern(), false);
|
|
|
|
Print(",");
|
|
|
|
PrintLiteral(node->flags(), false);
|
|
|
|
Print(") ");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitObjectLiteral(ObjectLiteral* node) {
|
|
|
|
Print("{ ");
|
|
|
|
for (int i = 0; i < node->properties()->length(); i++) {
|
|
|
|
if (i != 0) Print(",");
|
|
|
|
ObjectLiteral::Property* property = node->properties()->at(i);
|
|
|
|
Print(" ");
|
|
|
|
Visit(property->key());
|
|
|
|
Print(": ");
|
|
|
|
Visit(property->value());
|
|
|
|
}
|
|
|
|
Print(" }");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitArrayLiteral(ArrayLiteral* node) {
|
|
|
|
Print("[ ");
|
|
|
|
for (int i = 0; i < node->values()->length(); i++) {
|
|
|
|
if (i != 0) Print(",");
|
|
|
|
Visit(node->values()->at(i));
|
|
|
|
}
|
|
|
|
Print(" ]");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitVariableProxy(VariableProxy* node) {
|
|
|
|
PrintLiteral(node->name(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitAssignment(Assignment* node) {
|
|
|
|
Visit(node->target());
|
|
|
|
Print(" %s ", Token::String(node->op()));
|
|
|
|
Visit(node->value());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-02 17:34:59 +00:00
|
|
|
void PrettyPrinter::VisitYield(Yield* node) {
|
|
|
|
Print("yield ");
|
|
|
|
Visit(node->expression());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
void PrettyPrinter::VisitThrow(Throw* node) {
|
|
|
|
Print("throw ");
|
|
|
|
Visit(node->exception());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitProperty(Property* node) {
|
|
|
|
Expression* key = node->key();
|
|
|
|
Literal* literal = key->AsLiteral();
|
2013-06-24 10:37:59 +00:00
|
|
|
if (literal != NULL && literal->value()->IsInternalizedString()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Print("(");
|
|
|
|
Visit(node->obj());
|
|
|
|
Print(").");
|
2013-06-24 10:37:59 +00:00
|
|
|
PrintLiteral(literal->value(), false);
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
Visit(node->obj());
|
|
|
|
Print("[");
|
|
|
|
Visit(key);
|
|
|
|
Print("]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitCall(Call* node) {
|
|
|
|
Visit(node->expression());
|
|
|
|
PrintArguments(node->arguments());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitCallNew(CallNew* node) {
|
|
|
|
Print("new (");
|
|
|
|
Visit(node->expression());
|
|
|
|
Print(")");
|
|
|
|
PrintArguments(node->arguments());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitCallRuntime(CallRuntime* node) {
|
|
|
|
Print("%%");
|
|
|
|
PrintLiteral(node->name(), false);
|
|
|
|
PrintArguments(node->arguments());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitUnaryOperation(UnaryOperation* node) {
|
2011-05-23 07:00:54 +00:00
|
|
|
Token::Value op = node->op();
|
|
|
|
bool needsSpace =
|
|
|
|
op == Token::DELETE || op == Token::TYPEOF || op == Token::VOID;
|
|
|
|
Print("(%s%s", Token::String(op), needsSpace ? " " : "");
|
2008-07-03 15:10:15 +00:00
|
|
|
Visit(node->expression());
|
|
|
|
Print(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitCountOperation(CountOperation* node) {
|
|
|
|
Print("(");
|
|
|
|
if (node->is_prefix()) Print("%s", Token::String(node->op()));
|
|
|
|
Visit(node->expression());
|
|
|
|
if (node->is_postfix()) Print("%s", Token::String(node->op()));
|
|
|
|
Print(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitBinaryOperation(BinaryOperation* node) {
|
|
|
|
Print("(");
|
|
|
|
Visit(node->left());
|
2011-05-23 07:00:54 +00:00
|
|
|
Print(" %s ", Token::String(node->op()));
|
2008-07-03 15:10:15 +00:00
|
|
|
Visit(node->right());
|
|
|
|
Print(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitCompareOperation(CompareOperation* node) {
|
|
|
|
Print("(");
|
|
|
|
Visit(node->left());
|
2011-05-23 07:00:54 +00:00
|
|
|
Print(" %s ", Token::String(node->op()));
|
2008-07-03 15:10:15 +00:00
|
|
|
Visit(node->right());
|
|
|
|
Print(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::VisitThisFunction(ThisFunction* node) {
|
|
|
|
Print("<this-function>");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-18 12:35:34 +00:00
|
|
|
void PrettyPrinter::VisitSuperReference(SuperReference* node) {
|
|
|
|
Print("<super-reference>");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-30 11:53:29 +00:00
|
|
|
const char* PrettyPrinter::Print(AstNode* node) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Init();
|
|
|
|
Visit(node);
|
|
|
|
return output_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char* PrettyPrinter::PrintExpression(FunctionLiteral* program) {
|
|
|
|
Init();
|
|
|
|
ExpressionStatement* statement =
|
|
|
|
program->body()->at(0)->AsExpressionStatement();
|
|
|
|
Visit(statement->expression());
|
|
|
|
return output_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char* PrettyPrinter::PrintProgram(FunctionLiteral* program) {
|
|
|
|
Init();
|
|
|
|
PrintStatements(program->body());
|
|
|
|
Print("\n");
|
|
|
|
return output_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-21 16:22:52 +00:00
|
|
|
void PrettyPrinter::PrintOut(Zone* zone, AstNode* node) {
|
|
|
|
PrettyPrinter printer(zone);
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintF("%s", printer.Print(node));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::Init() {
|
|
|
|
if (size_ == 0) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(output_ == NULL);
|
2008-07-03 15:10:15 +00:00
|
|
|
const int initial_size = 256;
|
|
|
|
output_ = NewArray<char>(initial_size);
|
|
|
|
size_ = initial_size;
|
|
|
|
}
|
|
|
|
output_[0] = '\0';
|
|
|
|
pos_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::Print(const char* format, ...) {
|
|
|
|
for (;;) {
|
|
|
|
va_list arguments;
|
|
|
|
va_start(arguments, format);
|
2014-06-13 16:43:27 +00:00
|
|
|
int n = VSNPrintF(Vector<char>(output_, size_) + pos_,
|
|
|
|
format,
|
|
|
|
arguments);
|
2008-07-03 15:10:15 +00:00
|
|
|
va_end(arguments);
|
|
|
|
|
2008-08-06 10:02:49 +00:00
|
|
|
if (n >= 0) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// there was enough space - we are done
|
|
|
|
pos_ += n;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// there was not enough space - allocate more and try again
|
|
|
|
const int slack = 32;
|
|
|
|
int new_size = size_ + (size_ >> 1) + slack;
|
|
|
|
char* new_output = NewArray<char>(new_size);
|
2014-05-27 07:57:22 +00:00
|
|
|
MemCopy(new_output, output_, pos_);
|
2008-07-03 15:10:15 +00:00
|
|
|
DeleteArray(output_);
|
|
|
|
output_ = new_output;
|
|
|
|
size_ = new_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::PrintStatements(ZoneList<Statement*>* statements) {
|
2011-12-16 13:46:01 +00:00
|
|
|
if (statements == NULL) return;
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < statements->length(); i++) {
|
|
|
|
if (i != 0) Print(" ");
|
|
|
|
Visit(statements->at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
void PrettyPrinter::PrintLabels(ZoneList<const AstRawString*>* labels) {
|
2008-07-03 15:10:15 +00:00
|
|
|
if (labels != NULL) {
|
|
|
|
for (int i = 0; i < labels->length(); i++) {
|
|
|
|
PrintLiteral(labels->at(i), false);
|
|
|
|
Print(": ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::PrintArguments(ZoneList<Expression*>* arguments) {
|
|
|
|
Print("(");
|
|
|
|
for (int i = 0; i < arguments->length(); i++) {
|
|
|
|
if (i != 0) Print(", ");
|
|
|
|
Visit(arguments->at(i));
|
|
|
|
}
|
|
|
|
Print(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::PrintLiteral(Handle<Object> value, bool quote) {
|
|
|
|
Object* object = *value;
|
|
|
|
if (object->IsString()) {
|
|
|
|
String* string = String::cast(object);
|
|
|
|
if (quote) Print("\"");
|
2009-03-17 09:33:06 +00:00
|
|
|
for (int i = 0; i < string->length(); i++) {
|
|
|
|
Print("%c", string->Get(i));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
if (quote) Print("\"");
|
2011-03-18 20:35:07 +00:00
|
|
|
} else if (object->IsNull()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Print("null");
|
2011-03-18 20:35:07 +00:00
|
|
|
} else if (object->IsTrue()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Print("true");
|
2011-03-18 20:35:07 +00:00
|
|
|
} else if (object->IsFalse()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Print("false");
|
2011-03-18 20:35:07 +00:00
|
|
|
} else if (object->IsUndefined()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Print("undefined");
|
|
|
|
} else if (object->IsNumber()) {
|
|
|
|
Print("%g", object->Number());
|
|
|
|
} else if (object->IsJSObject()) {
|
|
|
|
// regular expression
|
|
|
|
if (object->IsJSFunction()) {
|
|
|
|
Print("JS-Function");
|
|
|
|
} else if (object->IsJSArray()) {
|
|
|
|
Print("JS-array[%u]", JSArray::cast(object)->length());
|
|
|
|
} else if (object->IsJSObject()) {
|
|
|
|
Print("JS-Object");
|
|
|
|
} else {
|
|
|
|
Print("?UNKNOWN?");
|
|
|
|
}
|
|
|
|
} else if (object->IsFixedArray()) {
|
|
|
|
Print("FixedArray");
|
|
|
|
} else {
|
|
|
|
Print("<unknown literal %p>", object);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
void PrettyPrinter::PrintLiteral(const AstRawString* value, bool quote) {
|
|
|
|
PrintLiteral(value->string(), quote);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
void PrettyPrinter::PrintParameters(Scope* scope) {
|
|
|
|
Print("(");
|
|
|
|
for (int i = 0; i < scope->num_parameters(); i++) {
|
|
|
|
if (i > 0) Print(", ");
|
|
|
|
PrintLiteral(scope->parameter(i)->name(), false);
|
|
|
|
}
|
|
|
|
Print(")");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::PrintDeclarations(ZoneList<Declaration*>* declarations) {
|
|
|
|
for (int i = 0; i < declarations->length(); i++) {
|
|
|
|
if (i > 0) Print(" ");
|
|
|
|
Visit(declarations->at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyPrinter::PrintFunctionLiteral(FunctionLiteral* function) {
|
|
|
|
Print("function ");
|
|
|
|
PrintLiteral(function->name(), false);
|
|
|
|
PrintParameters(function->scope());
|
|
|
|
Print(" { ");
|
|
|
|
PrintDeclarations(function->scope()->declarations());
|
|
|
|
PrintStatements(function->body());
|
|
|
|
Print(" }");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class IndentedScope BASE_EMBEDDED {
|
|
|
|
public:
|
2013-05-08 11:06:46 +00:00
|
|
|
IndentedScope(AstPrinter* printer, const char* txt)
|
2011-03-25 11:53:29 +00:00
|
|
|
: ast_printer_(printer) {
|
2008-07-03 15:10:15 +00:00
|
|
|
ast_printer_->PrintIndented(txt);
|
|
|
|
ast_printer_->Print("\n");
|
|
|
|
ast_printer_->inc_indent();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~IndentedScope() {
|
|
|
|
ast_printer_->dec_indent();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2011-03-18 20:35:07 +00:00
|
|
|
AstPrinter* ast_printer_;
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2014-01-21 16:22:52 +00:00
|
|
|
AstPrinter::AstPrinter(Zone* zone) : PrettyPrinter(zone), indent_(0) {
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AstPrinter::~AstPrinter() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(indent_ == 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::PrintIndented(const char* txt) {
|
|
|
|
for (int i = 0; i < indent_; i++) {
|
2009-04-15 07:41:04 +00:00
|
|
|
Print(". ");
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
Print(txt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::PrintLiteralIndented(const char* info,
|
|
|
|
Handle<Object> value,
|
|
|
|
bool quote) {
|
|
|
|
PrintIndented(info);
|
|
|
|
Print(" ");
|
|
|
|
PrintLiteral(value, quote);
|
|
|
|
Print("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::PrintLiteralWithModeIndented(const char* info,
|
|
|
|
Variable* var,
|
2011-04-07 14:42:37 +00:00
|
|
|
Handle<Object> value) {
|
2008-07-03 15:10:15 +00:00
|
|
|
if (var == NULL) {
|
|
|
|
PrintLiteralIndented(info, value, true);
|
|
|
|
} else {
|
2008-09-11 14:34:48 +00:00
|
|
|
EmbeddedVector<char, 256> buf;
|
2014-06-13 16:43:27 +00:00
|
|
|
int pos = SNPrintF(buf, "%s (mode = %s", info,
|
|
|
|
Variable::Mode2String(var->mode()));
|
|
|
|
SNPrintF(buf + pos, ")");
|
2008-09-11 14:34:48 +00:00
|
|
|
PrintLiteralIndented(buf.start(), value, true);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 14:03:24 +00:00
|
|
|
void AstPrinter::PrintLabelsIndented(ZoneList<const AstRawString*>* labels) {
|
2013-05-08 13:24:45 +00:00
|
|
|
if (labels == NULL || labels->length() == 0) return;
|
|
|
|
PrintIndented("LABELS ");
|
|
|
|
PrintLabels(labels);
|
|
|
|
Print("\n");
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-30 11:53:29 +00:00
|
|
|
void AstPrinter::PrintIndentedVisit(const char* s, AstNode* node) {
|
2013-05-08 11:06:46 +00:00
|
|
|
IndentedScope indent(this, s);
|
2008-07-03 15:10:15 +00:00
|
|
|
Visit(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char* AstPrinter::PrintProgram(FunctionLiteral* program) {
|
|
|
|
Init();
|
2011-03-25 11:53:29 +00:00
|
|
|
{ IndentedScope indent(this, "FUNC");
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintLiteralIndented("NAME", program->name(), true);
|
2009-04-14 00:51:59 +00:00
|
|
|
PrintLiteralIndented("INFERRED NAME", program->inferred_name(), true);
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintParameters(program->scope());
|
|
|
|
PrintDeclarations(program->scope()->declarations());
|
|
|
|
PrintStatements(program->body());
|
|
|
|
}
|
|
|
|
return Output();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::PrintDeclarations(ZoneList<Declaration*>* declarations) {
|
|
|
|
if (declarations->length() > 0) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "DECLS");
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < declarations->length(); i++) {
|
|
|
|
Visit(declarations->at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::PrintParameters(Scope* scope) {
|
|
|
|
if (scope->num_parameters() > 0) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "PARAMS");
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < scope->num_parameters(); i++) {
|
2009-04-15 07:41:04 +00:00
|
|
|
PrintLiteralWithModeIndented("VAR", scope->parameter(i),
|
2011-04-07 14:42:37 +00:00
|
|
|
scope->parameter(i)->name());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::PrintStatements(ZoneList<Statement*>* statements) {
|
|
|
|
for (int i = 0; i < statements->length(); i++) {
|
|
|
|
Visit(statements->at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::PrintArguments(ZoneList<Expression*>* arguments) {
|
|
|
|
for (int i = 0; i < arguments->length(); i++) {
|
|
|
|
Visit(arguments->at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitBlock(Block* node) {
|
|
|
|
const char* block_txt = node->is_initializer_block() ? "BLOCK INIT" : "BLOCK";
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, block_txt);
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintStatements(node->statements());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-08 13:24:45 +00:00
|
|
|
// TODO(svenpanne) Start with IndentedScope.
|
2012-02-09 13:39:26 +00:00
|
|
|
void AstPrinter::VisitVariableDeclaration(VariableDeclaration* node) {
|
2012-02-28 10:12:39 +00:00
|
|
|
PrintLiteralWithModeIndented(Variable::Mode2String(node->mode()),
|
|
|
|
node->proxy()->var(),
|
|
|
|
node->proxy()->name());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-08 13:24:45 +00:00
|
|
|
// TODO(svenpanne) Start with IndentedScope.
|
2012-02-28 10:12:39 +00:00
|
|
|
void AstPrinter::VisitFunctionDeclaration(FunctionDeclaration* node) {
|
|
|
|
PrintIndented("FUNCTION ");
|
|
|
|
PrintLiteral(node->proxy()->name(), true);
|
|
|
|
Print(" = function ");
|
|
|
|
PrintLiteral(node->fun()->name(), false);
|
|
|
|
Print("\n");
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-09 13:40:41 +00:00
|
|
|
void AstPrinter::VisitModuleDeclaration(ModuleDeclaration* node) {
|
|
|
|
IndentedScope indent(this, "MODULE");
|
|
|
|
PrintLiteralIndented("NAME", node->proxy()->name(), true);
|
|
|
|
Visit(node->module());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-29 12:12:52 +00:00
|
|
|
void AstPrinter::VisitImportDeclaration(ImportDeclaration* node) {
|
|
|
|
IndentedScope indent(this, "IMPORT");
|
|
|
|
PrintLiteralIndented("NAME", node->proxy()->name(), true);
|
|
|
|
Visit(node->module());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitExportDeclaration(ExportDeclaration* node) {
|
|
|
|
IndentedScope indent(this, "EXPORT ");
|
|
|
|
PrintLiteral(node->proxy()->name(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-09 13:40:41 +00:00
|
|
|
void AstPrinter::VisitModuleLiteral(ModuleLiteral* node) {
|
2013-05-08 13:24:45 +00:00
|
|
|
IndentedScope indent(this, "MODULE LITERAL");
|
2012-02-09 13:40:41 +00:00
|
|
|
VisitBlock(node->body());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitModuleVariable(ModuleVariable* node) {
|
2013-05-08 13:24:45 +00:00
|
|
|
IndentedScope indent(this, "MODULE VARIABLE");
|
2012-02-20 14:02:59 +00:00
|
|
|
Visit(node->proxy());
|
2012-02-09 13:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitModulePath(ModulePath* node) {
|
2013-05-08 13:24:45 +00:00
|
|
|
IndentedScope indent(this, "MODULE PATH");
|
|
|
|
PrintIndentedVisit("MODULE PATH PARENT", node->module());
|
|
|
|
PrintLiteralIndented("NAME", node->name(), true);
|
2012-02-09 13:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitModuleUrl(ModuleUrl* node) {
|
|
|
|
PrintLiteralIndented("URL", node->url(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
void AstPrinter::VisitModuleStatement(ModuleStatement* node) {
|
2013-05-08 13:24:45 +00:00
|
|
|
IndentedScope indent(this, "MODULE STATEMENT");
|
Get rid of static module allocation, do it in code.
Modules now have their own local scope, represented by their own context.
Module instance objects have an accessor for every export that forwards
access to the respective slot from the module's context. (Exports that are
modules themselves, however, are simple data properties.)
All modules have a _hosting_ scope/context, which (currently) is the
(innermost) enclosing global scope. To deal with recursion, nested modules
are hosted by the same scope as global ones.
For every (global or nested) module literal, the hosting context has an
internal slot that points directly to the respective module context. This
enables quick access to (statically resolved) module members by 2-dimensional
access through the hosting context. For example,
module A {
let x;
module B { let y; }
}
module C { let z; }
allocates contexts as follows:
[header| .A | .B | .C | A | C ] (global)
| | |
| | +-- [header| z ] (module)
| |
| +------- [header| y ] (module)
|
+------------ [header| x | B ] (module)
Here, .A, .B, .C are the internal slots pointing to the hosted module
contexts, whereas A, B, C hold the actual instance objects (note that every
module context also points to the respective instance object through its
extension slot in the header).
To deal with arbitrary recursion and aliases between modules,
they are created and initialized in several stages. Each stage applies to
all modules in the hosting global scope, including nested ones.
1. Allocate: for each module _literal_, allocate the module contexts and
respective instance object and wire them up. This happens in the
PushModuleContext runtime function, as generated by AllocateModules
(invoked by VisitDeclarations in the hosting scope).
2. Bind: for each module _declaration_ (i.e. literals as well as aliases),
assign the respective instance object to respective local variables. This
happens in VisitModuleDeclaration, and uses the instance objects created
in the previous stage.
For each module _literal_, this phase also constructs a module descriptor
for the next stage. This happens in VisitModuleLiteral.
3. Populate: invoke the DeclareModules runtime function to populate each
_instance_ object with accessors for it exports. This is generated by
DeclareModules (invoked by VisitDeclarations in the hosting scope again),
and uses the descriptors generated in the previous stage.
4. Initialize: execute the module bodies (and other code) in sequence. This
happens by the separate statements generated for module bodies. To reenter
the module scopes properly, the parser inserted ModuleStatements.
R=mstarzinger@chromium.org,svenpanne@chromium.org
BUG=
Review URL: https://codereview.chromium.org/11093074
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@13033 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2012-11-22 10:25:22 +00:00
|
|
|
PrintLiteralIndented("NAME", node->proxy()->name(), true);
|
|
|
|
PrintStatements(node->body()->statements());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
void AstPrinter::VisitExpressionStatement(ExpressionStatement* node) {
|
2013-05-08 13:24:45 +00:00
|
|
|
IndentedScope indent(this, "EXPRESSION STATEMENT");
|
2008-07-03 15:10:15 +00:00
|
|
|
Visit(node->expression());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitEmptyStatement(EmptyStatement* node) {
|
2013-05-08 13:24:45 +00:00
|
|
|
IndentedScope indent(this, "EMPTY");
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitIfStatement(IfStatement* node) {
|
2013-05-08 13:24:45 +00:00
|
|
|
IndentedScope indent(this, "IF");
|
|
|
|
PrintIndentedVisit("CONDITION", node->condition());
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintIndentedVisit("THEN", node->then_statement());
|
|
|
|
if (node->HasElseStatement()) {
|
|
|
|
PrintIndentedVisit("ELSE", node->else_statement());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitContinueStatement(ContinueStatement* node) {
|
2013-05-08 13:24:45 +00:00
|
|
|
IndentedScope indent(this, "CONTINUE");
|
|
|
|
PrintLabelsIndented(node->target()->labels());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitBreakStatement(BreakStatement* node) {
|
2013-05-08 13:24:45 +00:00
|
|
|
IndentedScope indent(this, "BREAK");
|
|
|
|
PrintLabelsIndented(node->target()->labels());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitReturnStatement(ReturnStatement* node) {
|
2013-05-08 13:24:45 +00:00
|
|
|
IndentedScope indent(this, "RETURN");
|
|
|
|
Visit(node->expression());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-12 10:52:49 +00:00
|
|
|
void AstPrinter::VisitWithStatement(WithStatement* node) {
|
|
|
|
IndentedScope indent(this, "WITH");
|
|
|
|
PrintIndentedVisit("OBJECT", node->expression());
|
|
|
|
PrintIndentedVisit("BODY", node->statement());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitSwitchStatement(SwitchStatement* node) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "SWITCH");
|
2013-05-08 13:24:45 +00:00
|
|
|
PrintLabelsIndented(node->labels());
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintIndentedVisit("TAG", node->tag());
|
|
|
|
for (int i = 0; i < node->cases()->length(); i++) {
|
2013-10-14 11:06:15 +00:00
|
|
|
Visit(node->cases()->at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitCaseClause(CaseClause* clause) {
|
|
|
|
if (clause->is_default()) {
|
|
|
|
IndentedScope indent(this, "DEFAULT");
|
|
|
|
PrintStatements(clause->statements());
|
|
|
|
} else {
|
|
|
|
IndentedScope indent(this, "CASE");
|
|
|
|
Visit(clause->label());
|
|
|
|
PrintStatements(clause->statements());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-12 13:14:06 +00:00
|
|
|
void AstPrinter::VisitDoWhileStatement(DoWhileStatement* node) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "DO");
|
2013-05-08 13:24:45 +00:00
|
|
|
PrintLabelsIndented(node->labels());
|
2009-10-12 13:14:06 +00:00
|
|
|
PrintIndentedVisit("BODY", node->body());
|
|
|
|
PrintIndentedVisit("COND", node->cond());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitWhileStatement(WhileStatement* node) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "WHILE");
|
2013-05-08 13:24:45 +00:00
|
|
|
PrintLabelsIndented(node->labels());
|
2009-10-12 13:14:06 +00:00
|
|
|
PrintIndentedVisit("COND", node->cond());
|
|
|
|
PrintIndentedVisit("BODY", node->body());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitForStatement(ForStatement* node) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "FOR");
|
2013-05-08 13:24:45 +00:00
|
|
|
PrintLabelsIndented(node->labels());
|
2008-07-03 15:10:15 +00:00
|
|
|
if (node->init()) PrintIndentedVisit("INIT", node->init());
|
|
|
|
if (node->cond()) PrintIndentedVisit("COND", node->cond());
|
2009-10-12 13:14:06 +00:00
|
|
|
PrintIndentedVisit("BODY", node->body());
|
2008-07-03 15:10:15 +00:00
|
|
|
if (node->next()) PrintIndentedVisit("NEXT", node->next());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitForInStatement(ForInStatement* node) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "FOR IN");
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintIndentedVisit("FOR", node->each());
|
|
|
|
PrintIndentedVisit("IN", node->enumerable());
|
|
|
|
PrintIndentedVisit("BODY", node->body());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-06 14:38:26 +00:00
|
|
|
void AstPrinter::VisitForOfStatement(ForOfStatement* node) {
|
|
|
|
IndentedScope indent(this, "FOR OF");
|
|
|
|
PrintIndentedVisit("FOR", node->each());
|
|
|
|
PrintIndentedVisit("OF", node->iterable());
|
|
|
|
PrintIndentedVisit("BODY", node->body());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-12 13:14:06 +00:00
|
|
|
void AstPrinter::VisitTryCatchStatement(TryCatchStatement* node) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "TRY CATCH");
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintIndentedVisit("TRY", node->try_block());
|
2011-06-30 14:37:55 +00:00
|
|
|
PrintLiteralWithModeIndented("CATCHVAR",
|
|
|
|
node->variable(),
|
|
|
|
node->variable()->name());
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintIndentedVisit("CATCH", node->catch_block());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-12 13:14:06 +00:00
|
|
|
void AstPrinter::VisitTryFinallyStatement(TryFinallyStatement* node) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "TRY FINALLY");
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintIndentedVisit("TRY", node->try_block());
|
|
|
|
PrintIndentedVisit("FINALLY", node->finally_block());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitDebuggerStatement(DebuggerStatement* node) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "DEBUGGER");
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitFunctionLiteral(FunctionLiteral* node) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "FUNC LITERAL");
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintLiteralIndented("NAME", node->name(), false);
|
2009-04-14 00:51:59 +00:00
|
|
|
PrintLiteralIndented("INFERRED NAME", node->inferred_name(), false);
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintParameters(node->scope());
|
|
|
|
// We don't want to see the function literal in this case: it
|
|
|
|
// will be printed via PrintProgram when the code for it is
|
|
|
|
// generated.
|
|
|
|
// PrintStatements(node->body());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-01 09:47:37 +00:00
|
|
|
void AstPrinter::VisitNativeFunctionLiteral(NativeFunctionLiteral* node) {
|
|
|
|
IndentedScope indent(this, "NATIVE FUNC LITERAL");
|
|
|
|
PrintLiteralIndented("NAME", node->name(), false);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitConditional(Conditional* node) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "CONDITIONAL");
|
2013-05-08 13:24:45 +00:00
|
|
|
PrintIndentedVisit("CONDITION", node->condition());
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintIndentedVisit("THEN", node->then_expression());
|
|
|
|
PrintIndentedVisit("ELSE", node->else_expression());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-08 13:24:45 +00:00
|
|
|
// TODO(svenpanne) Start with IndentedScope.
|
2008-07-03 15:10:15 +00:00
|
|
|
void AstPrinter::VisitLiteral(Literal* node) {
|
2013-06-24 10:37:59 +00:00
|
|
|
PrintLiteralIndented("LITERAL", node->value(), true);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitRegExpLiteral(RegExpLiteral* node) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "REGEXP LITERAL");
|
2009-06-04 12:01:55 +00:00
|
|
|
PrintLiteralIndented("PATTERN", node->pattern(), false);
|
|
|
|
PrintLiteralIndented("FLAGS", node->flags(), false);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitObjectLiteral(ObjectLiteral* node) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "OBJ LITERAL");
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < node->properties()->length(); i++) {
|
|
|
|
const char* prop_kind = NULL;
|
|
|
|
switch (node->properties()->at(i)->kind()) {
|
|
|
|
case ObjectLiteral::Property::CONSTANT:
|
|
|
|
prop_kind = "PROPERTY - CONSTANT";
|
|
|
|
break;
|
|
|
|
case ObjectLiteral::Property::COMPUTED:
|
|
|
|
prop_kind = "PROPERTY - COMPUTED";
|
|
|
|
break;
|
2009-07-29 08:46:28 +00:00
|
|
|
case ObjectLiteral::Property::MATERIALIZED_LITERAL:
|
|
|
|
prop_kind = "PROPERTY - MATERIALIZED_LITERAL";
|
|
|
|
break;
|
2008-07-03 15:10:15 +00:00
|
|
|
case ObjectLiteral::Property::PROTOTYPE:
|
|
|
|
prop_kind = "PROPERTY - PROTOTYPE";
|
|
|
|
break;
|
|
|
|
case ObjectLiteral::Property::GETTER:
|
|
|
|
prop_kind = "PROPERTY - GETTER";
|
|
|
|
break;
|
|
|
|
case ObjectLiteral::Property::SETTER:
|
|
|
|
prop_kind = "PROPERTY - SETTER";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope prop(this, prop_kind);
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintIndentedVisit("KEY", node->properties()->at(i)->key());
|
|
|
|
PrintIndentedVisit("VALUE", node->properties()->at(i)->value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitArrayLiteral(ArrayLiteral* node) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "ARRAY LITERAL");
|
2008-07-03 15:10:15 +00:00
|
|
|
if (node->values()->length() > 0) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "VALUES");
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < node->values()->length(); i++) {
|
|
|
|
Visit(node->values()->at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-08 13:24:45 +00:00
|
|
|
// TODO(svenpanne) Start with IndentedScope.
|
2008-07-03 15:10:15 +00:00
|
|
|
void AstPrinter::VisitVariableProxy(VariableProxy* node) {
|
|
|
|
Variable* var = node->var();
|
2011-09-12 14:00:16 +00:00
|
|
|
EmbeddedVector<char, 128> buf;
|
2014-06-13 16:43:27 +00:00
|
|
|
int pos = SNPrintF(buf, "VAR PROXY");
|
2011-09-12 14:00:16 +00:00
|
|
|
switch (var->location()) {
|
|
|
|
case Variable::UNALLOCATED:
|
|
|
|
break;
|
|
|
|
case Variable::PARAMETER:
|
2014-06-13 16:43:27 +00:00
|
|
|
SNPrintF(buf + pos, " parameter[%d]", var->index());
|
2011-09-12 14:00:16 +00:00
|
|
|
break;
|
|
|
|
case Variable::LOCAL:
|
2014-06-13 16:43:27 +00:00
|
|
|
SNPrintF(buf + pos, " local[%d]", var->index());
|
2011-09-12 14:00:16 +00:00
|
|
|
break;
|
|
|
|
case Variable::CONTEXT:
|
2014-06-13 16:43:27 +00:00
|
|
|
SNPrintF(buf + pos, " context[%d]", var->index());
|
2011-09-12 14:00:16 +00:00
|
|
|
break;
|
|
|
|
case Variable::LOOKUP:
|
2014-06-13 16:43:27 +00:00
|
|
|
SNPrintF(buf + pos, " lookup");
|
2011-09-12 14:00:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
PrintLiteralWithModeIndented(buf.start(), var, node->name());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitAssignment(Assignment* node) {
|
2013-05-08 11:06:46 +00:00
|
|
|
IndentedScope indent(this, Token::Name(node->op()));
|
2008-07-03 15:10:15 +00:00
|
|
|
Visit(node->target());
|
|
|
|
Visit(node->value());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-02 17:34:59 +00:00
|
|
|
void AstPrinter::VisitYield(Yield* node) {
|
2013-05-08 13:24:45 +00:00
|
|
|
IndentedScope indent(this, "YIELD");
|
|
|
|
Visit(node->expression());
|
2013-04-02 17:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
void AstPrinter::VisitThrow(Throw* node) {
|
2013-05-08 13:24:45 +00:00
|
|
|
IndentedScope indent(this, "THROW");
|
|
|
|
Visit(node->exception());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitProperty(Property* node) {
|
2013-05-08 11:06:46 +00:00
|
|
|
IndentedScope indent(this, "PROPERTY");
|
2008-07-03 15:10:15 +00:00
|
|
|
Visit(node->obj());
|
|
|
|
Literal* literal = node->key()->AsLiteral();
|
2013-06-24 10:37:59 +00:00
|
|
|
if (literal != NULL && literal->value()->IsInternalizedString()) {
|
|
|
|
PrintLiteralIndented("NAME", literal->value(), false);
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
PrintIndentedVisit("KEY", node->key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitCall(Call* node) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "CALL");
|
2008-07-03 15:10:15 +00:00
|
|
|
Visit(node->expression());
|
|
|
|
PrintArguments(node->arguments());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitCallNew(CallNew* node) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "CALL NEW");
|
2008-07-03 15:10:15 +00:00
|
|
|
Visit(node->expression());
|
|
|
|
PrintArguments(node->arguments());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitCallRuntime(CallRuntime* node) {
|
2013-05-08 13:24:45 +00:00
|
|
|
IndentedScope indent(this, "CALL RUNTIME");
|
|
|
|
PrintLiteralIndented("NAME", node->name(), false);
|
2008-07-03 15:10:15 +00:00
|
|
|
PrintArguments(node->arguments());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitUnaryOperation(UnaryOperation* node) {
|
2013-05-08 13:24:45 +00:00
|
|
|
IndentedScope indent(this, Token::Name(node->op()));
|
|
|
|
Visit(node->expression());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitCountOperation(CountOperation* node) {
|
2008-09-11 14:34:48 +00:00
|
|
|
EmbeddedVector<char, 128> buf;
|
2014-06-13 16:43:27 +00:00
|
|
|
SNPrintF(buf, "%s %s", (node->is_prefix() ? "PRE" : "POST"),
|
|
|
|
Token::Name(node->op()));
|
2013-05-08 13:24:45 +00:00
|
|
|
IndentedScope indent(this, buf.start());
|
|
|
|
Visit(node->expression());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitBinaryOperation(BinaryOperation* node) {
|
2013-05-08 11:06:46 +00:00
|
|
|
IndentedScope indent(this, Token::Name(node->op()));
|
2008-07-03 15:10:15 +00:00
|
|
|
Visit(node->left());
|
|
|
|
Visit(node->right());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitCompareOperation(CompareOperation* node) {
|
2013-05-08 11:06:46 +00:00
|
|
|
IndentedScope indent(this, Token::Name(node->op()));
|
2008-07-03 15:10:15 +00:00
|
|
|
Visit(node->left());
|
|
|
|
Visit(node->right());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AstPrinter::VisitThisFunction(ThisFunction* node) {
|
2011-03-25 11:53:29 +00:00
|
|
|
IndentedScope indent(this, "THIS-FUNCTION");
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 12:35:34 +00:00
|
|
|
|
|
|
|
void AstPrinter::VisitSuperReference(SuperReference* node) {
|
|
|
|
IndentedScope indent(this, "SUPER-REFERENCE");
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|