OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2012 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "src/v8.h" | |
6 | |
7 #include "src/ast.h" | |
8 #include "src/ast-numbering.h" | |
9 #include "src/compiler.h" | |
10 #include "src/scopes.h" | |
11 | |
12 namespace v8 { | |
13 namespace internal { | |
14 | |
15 | |
16 class AstNumberingVisitor FINAL : public AstVisitor { | |
17 public: | |
18 explicit AstNumberingVisitor(Zone* zone) : AstVisitor() { | |
19 InitializeAstVisitor(zone); | |
20 } | |
21 | |
22 private: | |
23 // AST node visitor interface. | |
24 #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
| |
25 virtual void Visit##type(type* node) { \ | |
26 Visit##type##Children(node); \ | |
27 node->AllocateBailoutIdRange(&id_gen_); \ | |
28 } | |
29 AST_NODE_LIST(DEFINE_VISIT) | |
30 #undef DEFINE_VISIT | |
31 | |
32 // Child visit functions. | |
33 #define DECLARE_VISIT_CHILDREN(type) void Visit##type##Children(type* node); | |
34 AST_NODE_LIST(DECLARE_VISIT_CHILDREN) | |
35 #undef DECLARE_VISIT_CHILDREN | |
36 | |
37 void VisitStatements(ZoneList<Statement*>* statements); | |
38 void VisitDeclarations(ZoneList<Declaration*>* declarations); | |
39 void VisitArguments(ZoneList<Expression*>* arguments); | |
40 void VisitObjectLiteralProperty(ObjectLiteralProperty* property); | |
41 | |
42 AstNode::IdGen id_gen_; | |
43 | |
44 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); | |
45 DISALLOW_COPY_AND_ASSIGN(AstNumberingVisitor); | |
46 }; | |
47 | |
48 | |
49 #define ZERO_CHILD_AST_NODE_LIST(M) \ | |
50 M(VariableDeclaration) \ | |
51 M(ExportDeclaration) \ | |
52 M(ModuleUrl) \ | |
53 M(EmptyStatement) \ | |
54 M(ContinueStatement) \ | |
55 M(BreakStatement) \ | |
56 M(DebuggerStatement) \ | |
57 M(NativeFunctionLiteral) \ | |
58 M(Literal) \ | |
59 M(RegExpLiteral) \ | |
60 M(VariableProxy) \ | |
61 M(ThisFunction) \ | |
62 M(SuperReference) | |
63 | |
64 | |
65 #define ONE_CHILD_AST_NODE_LIST(M) \ | |
66 M(ModuleDeclaration, module) \ | |
67 M(ImportDeclaration, module) \ | |
68 M(ModuleVariable, proxy) \ | |
69 M(ModulePath, module) \ | |
70 M(ModuleStatement, body) \ | |
71 M(ExpressionStatement, expression) \ | |
72 M(ReturnStatement, expression) \ | |
73 M(Yield, expression) \ | |
74 M(Throw, exception) \ | |
75 M(UnaryOperation, expression) \ | |
76 M(CountOperation, expression) | |
77 | |
78 | |
79 #define ONE_CHILD_CONCRETE_AST_NODE_LIST(M) \ | |
80 M(Block, Statements, statements) \ | |
81 M(FunctionDeclaration, FunctionLiteral, fun) \ | |
82 M(ModuleLiteral, Block, body) \ | |
83 M(CallRuntime, Arguments, arguments) | |
84 | |
85 | |
86 #define TWO_CHILD_AST_NODE_LIST(M) \ | |
87 M(WithStatement, expression, statement) \ | |
88 M(DoWhileStatement, body, cond) \ | |
89 M(WhileStatement, cond, body) \ | |
90 M(TryCatchStatement, try_block, catch_block) \ | |
91 M(TryFinallyStatement, try_block, finally_block) \ | |
92 M(Property, key, obj) \ | |
93 M(Assignment, target, value) \ | |
94 M(BinaryOperation, left, right) \ | |
95 M(CompareOperation, left, right) | |
96 | |
97 | |
98 #define THREE_CHILD_AST_NODE_LIST(M) \ | |
99 M(ForInStatement, each, enumerable, body) \ | |
100 M(ForOfStatement, each, iterable, body) \ | |
101 M(Conditional, condition, then_expression, else_expression) | |
102 | |
103 | |
104 #define ZERO_CHILD_VISITOR(type) \ | |
105 void AstNumberingVisitor::Visit##type##Children(type* node) {} | |
106 ZERO_CHILD_AST_NODE_LIST(ZERO_CHILD_VISITOR) | |
107 #undef ZERO_CHILD_VISITOR | |
108 | |
109 | |
110 #define ONE_CHILD_VISITOR(type, child) \ | |
111 void AstNumberingVisitor::Visit##type##Children(type* node) { \ | |
112 Visit(node->child()); \ | |
113 } | |
114 ONE_CHILD_AST_NODE_LIST(ONE_CHILD_VISITOR) | |
115 #undef ONE_CHILD_VISITOR | |
116 | |
117 | |
118 #define ONE_CHILD_VISITOR_WITH_CHILD_TYPE(type, child_type, child) \ | |
119 void AstNumberingVisitor::Visit##type##Children(type* node) { \ | |
120 Visit##child_type(node->child()); \ | |
121 } | |
122 ONE_CHILD_CONCRETE_AST_NODE_LIST(ONE_CHILD_VISITOR_WITH_CHILD_TYPE) | |
123 #undef ONE_CHILD_VISITOR_WITH_CHILD_TYPE | |
124 | |
125 | |
126 #define TWO_CHILD_VISITOR(type, child_1, child_2) \ | |
127 void AstNumberingVisitor::Visit##type##Children(type* node) { \ | |
128 Visit(node->child_1()); \ | |
129 Visit(node->child_2()); \ | |
130 } | |
131 TWO_CHILD_AST_NODE_LIST(TWO_CHILD_VISITOR) | |
132 #undef TWO_CHILD_VISITOR | |
133 | |
134 | |
135 #define THREE_CHILD_VISITOR(type, child_1, child_2, child_3) \ | |
136 void AstNumberingVisitor::Visit##type##Children(type* node) { \ | |
137 Visit(node->child_1()); \ | |
138 Visit(node->child_2()); \ | |
139 Visit(node->child_3()); \ | |
140 } | |
141 THREE_CHILD_AST_NODE_LIST(THREE_CHILD_VISITOR) | |
142 #undef THREE_CHILD_VISITOR | |
143 | |
144 | |
145 void AstNumberingVisitor::VisitIfStatementChildren(IfStatement* node) { | |
146 Visit(node->condition()); | |
147 Visit(node->then_statement()); | |
148 if (node->HasElseStatement()) { | |
149 Visit(node->else_statement()); | |
150 } | |
151 } | |
152 | |
153 | |
154 void AstNumberingVisitor::VisitSwitchStatementChildren(SwitchStatement* node) { | |
155 Visit(node->tag()); | |
156 ZoneList<CaseClause*>* cases = node->cases(); | |
157 for (int i = 0; i < cases->length(); i++) { | |
158 VisitCaseClause(cases->at(i)); | |
159 } | |
160 } | |
161 | |
162 | |
163 void AstNumberingVisitor::VisitCaseClauseChildren(CaseClause* node) { | |
164 if (!node->is_default()) Visit(node->label()); | |
165 VisitStatements(node->statements()); | |
166 } | |
167 | |
168 | |
169 void AstNumberingVisitor::VisitForStatementChildren(ForStatement* node) { | |
170 if (node->init() != NULL) Visit(node->init()); | |
171 if (node->cond() != NULL) Visit(node->cond()); | |
172 if (node->next() != NULL) Visit(node->next()); | |
173 Visit(node->body()); | |
174 } | |
175 | |
176 | |
177 void AstNumberingVisitor::VisitClassLiteralChildren(ClassLiteral* node) { | |
178 if (node->extends()) Visit(node->extends()); | |
179 for (int i = 0; i < node->properties()->length(); i++) { | |
180 VisitObjectLiteralProperty(node->properties()->at(i)); | |
181 } | |
182 } | |
183 | |
184 | |
185 void AstNumberingVisitor::VisitObjectLiteralChildren(ObjectLiteral* node) { | |
186 for (int i = 0; i < node->properties()->length(); i++) { | |
187 VisitObjectLiteralProperty(node->properties()->at(i)); | |
188 } | |
189 } | |
190 | |
191 | |
192 void AstNumberingVisitor::VisitObjectLiteralProperty( | |
193 ObjectLiteralProperty* node) { | |
194 Visit(node->key()); | |
195 Visit(node->value()); | |
196 } | |
197 | |
198 | |
199 void AstNumberingVisitor::VisitArrayLiteralChildren(ArrayLiteral* node) { | |
200 for (int i = 0; i < node->values()->length(); i++) { | |
201 Visit(node->values()->at(i)); | |
202 } | |
203 } | |
204 | |
205 | |
206 void AstNumberingVisitor::VisitCallChildren(Call* node) { | |
207 Visit(node->expression()); | |
208 VisitArguments(node->arguments()); | |
209 } | |
210 | |
211 | |
212 void AstNumberingVisitor::VisitCallNewChildren(CallNew* node) { | |
213 Visit(node->expression()); | |
214 VisitArguments(node->arguments()); | |
215 } | |
216 | |
217 | |
218 void AstNumberingVisitor::VisitStatements(ZoneList<Statement*>* statements) { | |
219 if (statements == NULL) return; | |
220 for (int i = 0; i < statements->length(); i++) { | |
221 Visit(statements->at(i)); | |
222 } | |
223 } | |
224 | |
225 | |
226 void AstNumberingVisitor::VisitDeclarations( | |
227 ZoneList<Declaration*>* declarations) { | |
228 for (int i = 0; i < declarations->length(); i++) { | |
229 Visit(declarations->at(i)); | |
230 } | |
231 } | |
232 | |
233 | |
234 void AstNumberingVisitor::VisitArguments(ZoneList<Expression*>* arguments) { | |
235 for (int i = 0; i < arguments->length(); i++) { | |
236 Visit(arguments->at(i)); | |
237 } | |
238 } | |
239 | |
240 | |
241 void AstNumberingVisitor::VisitFunctionLiteralChildren( | |
242 FunctionLiteral* function) { | |
243 VisitDeclarations(function->scope()->declarations()); | |
244 VisitStatements(function->body()); | |
245 } | |
246 | |
247 | |
248 // Assumes code has been parsed. Mutates the AST, so the AST should not | |
249 // continue to be used in the case of failure. | |
250 bool AstNumbering::Renumber(CompilationInfo* info) { | |
Sven Panne
2014/10/14 11:06:00
This is coupled too tightly: Just pass in the Func
| |
251 FunctionLiteral* function = info->function(); | |
252 DCHECK(function != NULL); | |
253 | |
254 AstNumberingVisitor visitor(info->zone()); | |
255 visitor.Visit(function); | |
256 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();
| |
257 | |
258 return true; | |
259 } | |
260 } | |
261 } // namespace v8::internal | |
OLD | NEW |