deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / rewriter.cc
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/parser.h"
9 #include "src/rewriter.h"
10 #include "src/scopes.h"
11
12 namespace v8 {
13 namespace internal {
14
15 class Processor: public AstVisitor {
16  public:
17   Processor(Isolate* isolate, Variable* result,
18             AstValueFactory* ast_value_factory)
19       : result_(result),
20         result_assigned_(false),
21         is_set_(false),
22         in_try_(false),
23         factory_(ast_value_factory) {
24     InitializeAstVisitor(isolate, ast_value_factory->zone());
25   }
26
27   virtual ~Processor() { }
28
29   void Process(ZoneList<Statement*>* statements);
30   bool result_assigned() const { return result_assigned_; }
31
32   AstNodeFactory* factory() { return &factory_; }
33
34  private:
35   Variable* result_;
36
37   // We are not tracking result usage via the result_'s use
38   // counts (we leave the accurate computation to the
39   // usage analyzer). Instead we simple remember if
40   // there was ever an assignment to result_.
41   bool result_assigned_;
42
43   // To avoid storing to .result all the time, we eliminate some of
44   // the stores by keeping track of whether or not we're sure .result
45   // will be overwritten anyway. This is a bit more tricky than what I
46   // was hoping for
47   bool is_set_;
48   bool in_try_;
49
50   AstNodeFactory factory_;
51
52   Expression* SetResult(Expression* value) {
53     result_assigned_ = true;
54     VariableProxy* result_proxy = factory()->NewVariableProxy(result_);
55     return factory()->NewAssignment(
56         Token::ASSIGN, result_proxy, value, RelocInfo::kNoPosition);
57   }
58
59   // Node visitors.
60 #define DEF_VISIT(type) virtual void Visit##type(type* node) OVERRIDE;
61   AST_NODE_LIST(DEF_VISIT)
62 #undef DEF_VISIT
63
64   void VisitIterationStatement(IterationStatement* stmt);
65
66   DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
67 };
68
69
70 void Processor::Process(ZoneList<Statement*>* statements) {
71   for (int i = statements->length() - 1; i >= 0; --i) {
72     Visit(statements->at(i));
73   }
74 }
75
76
77 void Processor::VisitBlock(Block* node) {
78   // An initializer block is the rewritten form of a variable declaration
79   // with initialization expressions. The initializer block contains the
80   // list of assignments corresponding to the initialization expressions.
81   // While unclear from the spec (ECMA-262, 3rd., 12.2), the value of
82   // a variable declaration with initialization expression is 'undefined'
83   // with some JS VMs: For instance, using smjs, print(eval('var x = 7'))
84   // returns 'undefined'. To obtain the same behavior with v8, we need
85   // to prevent rewriting in that case.
86   if (!node->is_initializer_block()) Process(node->statements());
87 }
88
89
90 void Processor::VisitModuleStatement(ModuleStatement* node) {
91   bool set_after_body = is_set_;
92   Visit(node->body());
93   is_set_ = is_set_ && set_after_body;
94 }
95
96
97 void Processor::VisitExpressionStatement(ExpressionStatement* node) {
98   // Rewrite : <x>; -> .result = <x>;
99   if (!is_set_ && !node->expression()->IsThrow()) {
100     node->set_expression(SetResult(node->expression()));
101     if (!in_try_) is_set_ = true;
102   }
103 }
104
105
106 void Processor::VisitIfStatement(IfStatement* node) {
107   // Rewrite both then and else parts (reversed).
108   bool save = is_set_;
109   Visit(node->else_statement());
110   bool set_after_then = is_set_;
111   is_set_ = save;
112   Visit(node->then_statement());
113   is_set_ = is_set_ && set_after_then;
114 }
115
116
117 void Processor::VisitIterationStatement(IterationStatement* node) {
118   // Rewrite the body.
119   bool set_after_loop = is_set_;
120   Visit(node->body());
121   is_set_ = is_set_ && set_after_loop;
122 }
123
124
125 void Processor::VisitDoWhileStatement(DoWhileStatement* node) {
126   VisitIterationStatement(node);
127 }
128
129
130 void Processor::VisitWhileStatement(WhileStatement* node) {
131   VisitIterationStatement(node);
132 }
133
134
135 void Processor::VisitForStatement(ForStatement* node) {
136   VisitIterationStatement(node);
137 }
138
139
140 void Processor::VisitForInStatement(ForInStatement* node) {
141   VisitIterationStatement(node);
142 }
143
144
145 void Processor::VisitForOfStatement(ForOfStatement* node) {
146   VisitIterationStatement(node);
147 }
148
149
150 void Processor::VisitTryCatchStatement(TryCatchStatement* node) {
151   // Rewrite both try and catch blocks (reversed order).
152   bool set_after_catch = is_set_;
153   Visit(node->catch_block());
154   is_set_ = is_set_ && set_after_catch;
155   bool save = in_try_;
156   in_try_ = true;
157   Visit(node->try_block());
158   in_try_ = save;
159 }
160
161
162 void Processor::VisitTryFinallyStatement(TryFinallyStatement* node) {
163   // Rewrite both try and finally block (reversed order).
164   Visit(node->finally_block());
165   bool save = in_try_;
166   in_try_ = true;
167   Visit(node->try_block());
168   in_try_ = save;
169 }
170
171
172 void Processor::VisitSwitchStatement(SwitchStatement* node) {
173   // Rewrite statements in all case clauses in reversed order.
174   ZoneList<CaseClause*>* clauses = node->cases();
175   bool set_after_switch = is_set_;
176   for (int i = clauses->length() - 1; i >= 0; --i) {
177     CaseClause* clause = clauses->at(i);
178     Process(clause->statements());
179   }
180   is_set_ = is_set_ && set_after_switch;
181 }
182
183
184 void Processor::VisitContinueStatement(ContinueStatement* node) {
185   is_set_ = false;
186 }
187
188
189 void Processor::VisitBreakStatement(BreakStatement* node) {
190   is_set_ = false;
191 }
192
193
194 void Processor::VisitWithStatement(WithStatement* node) {
195   bool set_after_body = is_set_;
196   Visit(node->statement());
197   is_set_ = is_set_ && set_after_body;
198 }
199
200
201 // Do nothing:
202 void Processor::VisitVariableDeclaration(VariableDeclaration* node) {}
203 void Processor::VisitFunctionDeclaration(FunctionDeclaration* node) {}
204 void Processor::VisitModuleDeclaration(ModuleDeclaration* node) {}
205 void Processor::VisitImportDeclaration(ImportDeclaration* node) {}
206 void Processor::VisitExportDeclaration(ExportDeclaration* node) {}
207 void Processor::VisitModuleLiteral(ModuleLiteral* node) {}
208 void Processor::VisitModulePath(ModulePath* node) {}
209 void Processor::VisitModuleUrl(ModuleUrl* node) {}
210 void Processor::VisitEmptyStatement(EmptyStatement* node) {}
211 void Processor::VisitReturnStatement(ReturnStatement* node) {}
212 void Processor::VisitDebuggerStatement(DebuggerStatement* node) {}
213
214
215 // Expressions are never visited yet.
216 #define DEF_VISIT(type)                                         \
217   void Processor::Visit##type(type* expr) { UNREACHABLE(); }
218 EXPRESSION_NODE_LIST(DEF_VISIT)
219 #undef DEF_VISIT
220
221
222 // Assumes code has been parsed.  Mutates the AST, so the AST should not
223 // continue to be used in the case of failure.
224 bool Rewriter::Rewrite(ParseInfo* info) {
225   FunctionLiteral* function = info->function();
226   DCHECK(function != NULL);
227   Scope* scope = function->scope();
228   DCHECK(scope != NULL);
229   if (!scope->is_script_scope() && !scope->is_eval_scope()) return true;
230
231   ZoneList<Statement*>* body = function->body();
232   if (!body->is_empty()) {
233     Variable* result =
234         scope->NewTemporary(info->ast_value_factory()->dot_result_string());
235     // The name string must be internalized at this point.
236     DCHECK(!result->name().is_null());
237     Processor processor(info->isolate(), result, info->ast_value_factory());
238     processor.Process(body);
239     if (processor.HasStackOverflow()) return false;
240
241     if (processor.result_assigned()) {
242       DCHECK(function->end_position() != RelocInfo::kNoPosition);
243       // Set the position of the assignment statement one character past the
244       // source code, such that it definitely is not in the source code range
245       // of an immediate inner scope. For example in
246       //   eval('with ({x:1}) x = 1');
247       // the end position of the function generated for executing the eval code
248       // coincides with the end of the with scope which is the position of '1'.
249       int pos = function->end_position();
250       VariableProxy* result_proxy =
251           processor.factory()->NewVariableProxy(result, pos);
252       Statement* result_statement =
253           processor.factory()->NewReturnStatement(result_proxy, pos);
254       body->Add(result_statement, info->zone());
255     }
256   }
257
258   return true;
259 }
260
261
262 } }  // namespace v8::internal