Chromium Code Reviews| Index: src/ast-numbering.cc |
| diff --git a/src/ast-numbering.cc b/src/ast-numbering.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1c6535441453f72f924409772bbd8bc975985829 |
| --- /dev/null |
| +++ b/src/ast-numbering.cc |
| @@ -0,0 +1,261 @@ |
| +// Copyright 2012 the V8 project authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "src/v8.h" |
| + |
| +#include "src/ast.h" |
| +#include "src/ast-numbering.h" |
| +#include "src/compiler.h" |
| +#include "src/scopes.h" |
| + |
| +namespace v8 { |
| +namespace internal { |
| + |
| + |
| +class AstNumberingVisitor FINAL : public AstVisitor { |
| + public: |
| + explicit AstNumberingVisitor(Zone* zone) : AstVisitor() { |
| + InitializeAstVisitor(zone); |
| + } |
| + |
| + private: |
| +// AST node visitor interface. |
| +#define DEFINE_VISIT(type) \ |
|
Sven Panne
2014/10/14 11:06:00
I don't like this 2nd order macro horror, although
marja
2014/10/14 11:10:00
Acutally, I think it's silly that all visitors nee
|
| + virtual void Visit##type(type* node) { \ |
| + Visit##type##Children(node); \ |
| + node->AllocateBailoutIdRange(&id_gen_); \ |
| + } |
| + AST_NODE_LIST(DEFINE_VISIT) |
| +#undef DEFINE_VISIT |
| + |
| +// Child visit functions. |
| +#define DECLARE_VISIT_CHILDREN(type) void Visit##type##Children(type* node); |
| + AST_NODE_LIST(DECLARE_VISIT_CHILDREN) |
| +#undef DECLARE_VISIT_CHILDREN |
| + |
| + void VisitStatements(ZoneList<Statement*>* statements); |
| + void VisitDeclarations(ZoneList<Declaration*>* declarations); |
| + void VisitArguments(ZoneList<Expression*>* arguments); |
| + void VisitObjectLiteralProperty(ObjectLiteralProperty* property); |
| + |
| + AstNode::IdGen id_gen_; |
| + |
| + DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); |
| + DISALLOW_COPY_AND_ASSIGN(AstNumberingVisitor); |
| +}; |
| + |
| + |
| +#define ZERO_CHILD_AST_NODE_LIST(M) \ |
| + M(VariableDeclaration) \ |
| + M(ExportDeclaration) \ |
| + M(ModuleUrl) \ |
| + M(EmptyStatement) \ |
| + M(ContinueStatement) \ |
| + M(BreakStatement) \ |
| + M(DebuggerStatement) \ |
| + M(NativeFunctionLiteral) \ |
| + M(Literal) \ |
| + M(RegExpLiteral) \ |
| + M(VariableProxy) \ |
| + M(ThisFunction) \ |
| + M(SuperReference) |
| + |
| + |
| +#define ONE_CHILD_AST_NODE_LIST(M) \ |
| + M(ModuleDeclaration, module) \ |
| + M(ImportDeclaration, module) \ |
| + M(ModuleVariable, proxy) \ |
| + M(ModulePath, module) \ |
| + M(ModuleStatement, body) \ |
| + M(ExpressionStatement, expression) \ |
| + M(ReturnStatement, expression) \ |
| + M(Yield, expression) \ |
| + M(Throw, exception) \ |
| + M(UnaryOperation, expression) \ |
| + M(CountOperation, expression) |
| + |
| + |
| +#define ONE_CHILD_CONCRETE_AST_NODE_LIST(M) \ |
| + M(Block, Statements, statements) \ |
| + M(FunctionDeclaration, FunctionLiteral, fun) \ |
| + M(ModuleLiteral, Block, body) \ |
| + M(CallRuntime, Arguments, arguments) |
| + |
| + |
| +#define TWO_CHILD_AST_NODE_LIST(M) \ |
| + M(WithStatement, expression, statement) \ |
| + M(DoWhileStatement, body, cond) \ |
| + M(WhileStatement, cond, body) \ |
| + M(TryCatchStatement, try_block, catch_block) \ |
| + M(TryFinallyStatement, try_block, finally_block) \ |
| + M(Property, key, obj) \ |
| + M(Assignment, target, value) \ |
| + M(BinaryOperation, left, right) \ |
| + M(CompareOperation, left, right) |
| + |
| + |
| +#define THREE_CHILD_AST_NODE_LIST(M) \ |
| + M(ForInStatement, each, enumerable, body) \ |
| + M(ForOfStatement, each, iterable, body) \ |
| + M(Conditional, condition, then_expression, else_expression) |
| + |
| + |
| +#define ZERO_CHILD_VISITOR(type) \ |
| + void AstNumberingVisitor::Visit##type##Children(type* node) {} |
| +ZERO_CHILD_AST_NODE_LIST(ZERO_CHILD_VISITOR) |
| +#undef ZERO_CHILD_VISITOR |
| + |
| + |
| +#define ONE_CHILD_VISITOR(type, child) \ |
| + void AstNumberingVisitor::Visit##type##Children(type* node) { \ |
| + Visit(node->child()); \ |
| + } |
| +ONE_CHILD_AST_NODE_LIST(ONE_CHILD_VISITOR) |
| +#undef ONE_CHILD_VISITOR |
| + |
| + |
| +#define ONE_CHILD_VISITOR_WITH_CHILD_TYPE(type, child_type, child) \ |
| + void AstNumberingVisitor::Visit##type##Children(type* node) { \ |
| + Visit##child_type(node->child()); \ |
| + } |
| +ONE_CHILD_CONCRETE_AST_NODE_LIST(ONE_CHILD_VISITOR_WITH_CHILD_TYPE) |
| +#undef ONE_CHILD_VISITOR_WITH_CHILD_TYPE |
| + |
| + |
| +#define TWO_CHILD_VISITOR(type, child_1, child_2) \ |
| + void AstNumberingVisitor::Visit##type##Children(type* node) { \ |
| + Visit(node->child_1()); \ |
| + Visit(node->child_2()); \ |
| + } |
| +TWO_CHILD_AST_NODE_LIST(TWO_CHILD_VISITOR) |
| +#undef TWO_CHILD_VISITOR |
| + |
| + |
| +#define THREE_CHILD_VISITOR(type, child_1, child_2, child_3) \ |
| + void AstNumberingVisitor::Visit##type##Children(type* node) { \ |
| + Visit(node->child_1()); \ |
| + Visit(node->child_2()); \ |
| + Visit(node->child_3()); \ |
| + } |
| +THREE_CHILD_AST_NODE_LIST(THREE_CHILD_VISITOR) |
| +#undef THREE_CHILD_VISITOR |
| + |
| + |
| +void AstNumberingVisitor::VisitIfStatementChildren(IfStatement* node) { |
| + Visit(node->condition()); |
| + Visit(node->then_statement()); |
| + if (node->HasElseStatement()) { |
| + Visit(node->else_statement()); |
| + } |
| +} |
| + |
| + |
| +void AstNumberingVisitor::VisitSwitchStatementChildren(SwitchStatement* node) { |
| + Visit(node->tag()); |
| + ZoneList<CaseClause*>* cases = node->cases(); |
| + for (int i = 0; i < cases->length(); i++) { |
| + VisitCaseClause(cases->at(i)); |
| + } |
| +} |
| + |
| + |
| +void AstNumberingVisitor::VisitCaseClauseChildren(CaseClause* node) { |
| + if (!node->is_default()) Visit(node->label()); |
| + VisitStatements(node->statements()); |
| +} |
| + |
| + |
| +void AstNumberingVisitor::VisitForStatementChildren(ForStatement* node) { |
| + if (node->init() != NULL) Visit(node->init()); |
| + if (node->cond() != NULL) Visit(node->cond()); |
| + if (node->next() != NULL) Visit(node->next()); |
| + Visit(node->body()); |
| +} |
| + |
| + |
| +void AstNumberingVisitor::VisitClassLiteralChildren(ClassLiteral* node) { |
| + if (node->extends()) Visit(node->extends()); |
| + for (int i = 0; i < node->properties()->length(); i++) { |
| + VisitObjectLiteralProperty(node->properties()->at(i)); |
| + } |
| +} |
| + |
| + |
| +void AstNumberingVisitor::VisitObjectLiteralChildren(ObjectLiteral* node) { |
| + for (int i = 0; i < node->properties()->length(); i++) { |
| + VisitObjectLiteralProperty(node->properties()->at(i)); |
| + } |
| +} |
| + |
| + |
| +void AstNumberingVisitor::VisitObjectLiteralProperty( |
| + ObjectLiteralProperty* node) { |
| + Visit(node->key()); |
| + Visit(node->value()); |
| +} |
| + |
| + |
| +void AstNumberingVisitor::VisitArrayLiteralChildren(ArrayLiteral* node) { |
| + for (int i = 0; i < node->values()->length(); i++) { |
| + Visit(node->values()->at(i)); |
| + } |
| +} |
| + |
| + |
| +void AstNumberingVisitor::VisitCallChildren(Call* node) { |
| + Visit(node->expression()); |
| + VisitArguments(node->arguments()); |
| +} |
| + |
| + |
| +void AstNumberingVisitor::VisitCallNewChildren(CallNew* node) { |
| + Visit(node->expression()); |
| + VisitArguments(node->arguments()); |
| +} |
| + |
| + |
| +void AstNumberingVisitor::VisitStatements(ZoneList<Statement*>* statements) { |
| + if (statements == NULL) return; |
| + for (int i = 0; i < statements->length(); i++) { |
| + Visit(statements->at(i)); |
| + } |
| +} |
| + |
| + |
| +void AstNumberingVisitor::VisitDeclarations( |
| + ZoneList<Declaration*>* declarations) { |
| + for (int i = 0; i < declarations->length(); i++) { |
| + Visit(declarations->at(i)); |
| + } |
| +} |
| + |
| + |
| +void AstNumberingVisitor::VisitArguments(ZoneList<Expression*>* arguments) { |
| + for (int i = 0; i < arguments->length(); i++) { |
| + Visit(arguments->at(i)); |
| + } |
| +} |
| + |
| + |
| +void AstNumberingVisitor::VisitFunctionLiteralChildren( |
| + FunctionLiteral* function) { |
| + VisitDeclarations(function->scope()->declarations()); |
| + VisitStatements(function->body()); |
| +} |
| + |
| + |
| +// Assumes code has been parsed. Mutates the AST, so the AST should not |
| +// continue to be used in the case of failure. |
| +bool AstNumbering::Renumber(CompilationInfo* info) { |
|
Sven Panne
2014/10/14 11:06:00
This is coupled too tightly: Just pass in the Func
|
| + FunctionLiteral* function = info->function(); |
| + DCHECK(function != NULL); |
| + |
| + AstNumberingVisitor visitor(info->zone()); |
| + visitor.Visit(function); |
| + if (visitor.HasStackOverflow()) return false; |
|
marja
2014/10/13 14:44:55
Hmm, when can stack overflow happen here?
wingo
2014/10/13 15:47:24
In the ast.h AstVisitor definition:
#define DEFIN
Sven Panne
2014/10/14 11:06:00
return !visitor.HasStackOverflow();
|
| + |
| + return true; |
| +} |
| +} |
| +} // namespace v8::internal |