OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/ast_transformer.h" | 5 #include "vm/ast_transformer.h" |
6 | 6 |
7 #include "vm/object_store.h" | 7 #include "vm/object_store.h" |
8 #include "vm/parser.h" | 8 #include "vm/parser.h" |
9 #include "vm/thread.h" | 9 #include "vm/thread.h" |
10 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 V(NativeBody) \ | 37 V(NativeBody) \ |
38 V(Primary) \ | 38 V(Primary) \ |
39 V(Return) \ | 39 V(Return) \ |
40 V(Sequence) \ | 40 V(Sequence) \ |
41 V(StoreInstanceField) \ | 41 V(StoreInstanceField) \ |
42 V(Switch) \ | 42 V(Switch) \ |
43 V(TryCatch) \ | 43 V(TryCatch) \ |
44 V(While) | 44 V(While) |
45 | 45 |
46 #define DEFINE_UNREACHABLE(BaseName) \ | 46 #define DEFINE_UNREACHABLE(BaseName) \ |
47 void AwaitTransformer::Visit##BaseName##Node(BaseName##Node* node) { \ | 47 void AwaitTransformer::Visit##BaseName##Node(BaseName##Node* node) { \ |
48 UNREACHABLE(); \ | 48 UNREACHABLE(); \ |
49 } | 49 } |
50 | 50 |
51 FOR_EACH_UNREACHABLE_NODE(DEFINE_UNREACHABLE) | 51 FOR_EACH_UNREACHABLE_NODE(DEFINE_UNREACHABLE) |
52 #undef DEFINE_UNREACHABLE | 52 #undef DEFINE_UNREACHABLE |
53 | 53 |
54 AwaitTransformer::AwaitTransformer(SequenceNode* preamble, | 54 AwaitTransformer::AwaitTransformer(SequenceNode* preamble, |
55 LocalScope* async_temp_scope) | 55 LocalScope* async_temp_scope) |
56 : preamble_(preamble), | 56 : preamble_(preamble), |
57 temp_cnt_(0), | 57 temp_cnt_(0), |
58 async_temp_scope_(async_temp_scope), | 58 async_temp_scope_(async_temp_scope), |
59 thread_(Thread::Current()) { | 59 thread_(Thread::Current()) { |
60 ASSERT(async_temp_scope_ != NULL); | 60 ASSERT(async_temp_scope_ != NULL); |
61 } | 61 } |
62 | 62 |
63 | 63 |
64 AstNode* AwaitTransformer::Transform(AstNode* expr) { | 64 AstNode* AwaitTransformer::Transform(AstNode* expr) { |
65 expr->Visit(this); | 65 expr->Visit(this); |
66 return result_; | 66 return result_; |
67 } | 67 } |
68 | 68 |
69 | 69 |
70 LocalVariable* AwaitTransformer::EnsureCurrentTempVar() { | 70 LocalVariable* AwaitTransformer::EnsureCurrentTempVar() { |
71 String& symbol = | 71 String& symbol = |
72 String::ZoneHandle(Z, Symbols::NewFormatted(T, "%d", temp_cnt_)); | 72 String::ZoneHandle(Z, Symbols::NewFormatted(T, "%d", temp_cnt_)); |
73 symbol = Symbols::FromConcat(T, Symbols::AwaitTempVarPrefix(), symbol); | 73 symbol = Symbols::FromConcat(T, Symbols::AwaitTempVarPrefix(), symbol); |
74 ASSERT(!symbol.IsNull()); | 74 ASSERT(!symbol.IsNull()); |
75 // Look up the variable in the scope used for async temp variables. | 75 // Look up the variable in the scope used for async temp variables. |
76 LocalVariable* await_tmp = async_temp_scope_->LocalLookupVariable(symbol); | 76 LocalVariable* await_tmp = async_temp_scope_->LocalLookupVariable(symbol); |
77 if (await_tmp == NULL) { | 77 if (await_tmp == NULL) { |
78 // We need a new temp variable; add it to the function's top scope. | 78 // We need a new temp variable; add it to the function's top scope. |
79 await_tmp = new(Z) LocalVariable( | 79 await_tmp = new (Z) |
80 TokenPosition::kNoSource, | 80 LocalVariable(TokenPosition::kNoSource, TokenPosition::kNoSource, |
81 TokenPosition::kNoSource, | 81 symbol, Object::dynamic_type()); |
82 symbol, | |
83 Object::dynamic_type()); | |
84 async_temp_scope_->AddVariable(await_tmp); | 82 async_temp_scope_->AddVariable(await_tmp); |
85 // After adding it to the top scope, we can look it up from the preamble. | 83 // After adding it to the top scope, we can look it up from the preamble. |
86 // The following call includes an ASSERT check. | 84 // The following call includes an ASSERT check. |
87 await_tmp = GetVariableInScope(preamble_->scope(), symbol); | 85 await_tmp = GetVariableInScope(preamble_->scope(), symbol); |
88 } | 86 } |
89 return await_tmp; | 87 return await_tmp; |
90 } | 88 } |
91 | 89 |
92 | 90 |
93 LocalVariable* AwaitTransformer::GetVariableInScope(LocalScope* scope, | 91 LocalVariable* AwaitTransformer::GetVariableInScope(LocalScope* scope, |
94 const String& symbol) { | 92 const String& symbol) { |
95 LocalVariable* var = scope->LookupVariable(symbol, false); | 93 LocalVariable* var = scope->LookupVariable(symbol, false); |
96 ASSERT(var != NULL); | 94 ASSERT(var != NULL); |
97 return var; | 95 return var; |
98 } | 96 } |
99 | 97 |
100 | 98 |
101 LocalVariable* AwaitTransformer::AddNewTempVarToPreamble( | 99 LocalVariable* AwaitTransformer::AddNewTempVarToPreamble( |
102 AstNode* node, | 100 AstNode* node, |
103 TokenPosition token_pos) { | 101 TokenPosition token_pos) { |
104 LocalVariable* tmp_var = EnsureCurrentTempVar(); | 102 LocalVariable* tmp_var = EnsureCurrentTempVar(); |
105 ASSERT(token_pos.IsSynthetic() || token_pos.IsNoSource()); | 103 ASSERT(token_pos.IsSynthetic() || token_pos.IsNoSource()); |
106 preamble_->Add(new(Z) StoreLocalNode(token_pos, tmp_var, node)); | 104 preamble_->Add(new (Z) StoreLocalNode(token_pos, tmp_var, node)); |
107 NextTempVar(); | 105 NextTempVar(); |
108 return tmp_var; | 106 return tmp_var; |
109 } | 107 } |
110 | 108 |
111 | 109 |
112 LoadLocalNode* AwaitTransformer::MakeName(AstNode* node) { | 110 LoadLocalNode* AwaitTransformer::MakeName(AstNode* node) { |
113 LocalVariable* temp = AddNewTempVarToPreamble(node, ST(node->token_pos())); | 111 LocalVariable* temp = AddNewTempVarToPreamble(node, ST(node->token_pos())); |
114 return new(Z) LoadLocalNode(ST(node->token_pos()), temp); | 112 return new (Z) LoadLocalNode(ST(node->token_pos()), temp); |
115 } | 113 } |
116 | 114 |
117 | 115 |
118 void AwaitTransformer::VisitLiteralNode(LiteralNode* node) { | 116 void AwaitTransformer::VisitLiteralNode(LiteralNode* node) { |
119 result_ = node; | 117 result_ = node; |
120 } | 118 } |
121 | 119 |
122 | 120 |
123 void AwaitTransformer::VisitTypeNode(TypeNode* node) { | 121 void AwaitTransformer::VisitTypeNode(TypeNode* node) { |
124 result_ = new(Z) TypeNode(node->token_pos(), node->type()); | 122 result_ = new (Z) TypeNode(node->token_pos(), node->type()); |
125 } | 123 } |
126 | 124 |
127 | 125 |
128 void AwaitTransformer::VisitAwaitNode(AwaitNode* node) { | 126 void AwaitTransformer::VisitAwaitNode(AwaitNode* node) { |
129 // Await transformation: | 127 // Await transformation: |
130 // | 128 // |
131 // :await_temp_var_X = <expr>; | 129 // :await_temp_var_X = <expr>; |
132 // AwaitMarker(kNewContinuationState); | 130 // AwaitMarker(kNewContinuationState); |
133 // :result_param = _awaitHelper( | 131 // :result_param = _awaitHelper( |
134 // :await_temp_var_X, :async_then_callback, :async_catch_error_callback); | 132 // :await_temp_var_X, :async_then_callback, :async_catch_error_callback); |
135 // return; // (return_type() == kContinuationTarget) | 133 // return; // (return_type() == kContinuationTarget) |
136 // | 134 // |
137 // :saved_try_ctx_var = :await_saved_try_ctx_var_y; | 135 // :saved_try_ctx_var = :await_saved_try_ctx_var_y; |
138 // :await_temp_var_(X+1) = :result_param; | 136 // :await_temp_var_(X+1) = :result_param; |
139 | 137 |
140 const TokenPosition token_pos = ST(node->token_pos()); | 138 const TokenPosition token_pos = ST(node->token_pos()); |
141 LocalVariable* async_op = GetVariableInScope( | 139 LocalVariable* async_op = |
142 preamble_->scope(), Symbols::AsyncOperation()); | 140 GetVariableInScope(preamble_->scope(), Symbols::AsyncOperation()); |
143 LocalVariable* async_then_callback = GetVariableInScope( | 141 LocalVariable* async_then_callback = |
144 preamble_->scope(), Symbols::AsyncThenCallback()); | 142 GetVariableInScope(preamble_->scope(), Symbols::AsyncThenCallback()); |
145 LocalVariable* async_catch_error_callback = GetVariableInScope( | 143 LocalVariable* async_catch_error_callback = GetVariableInScope( |
146 preamble_->scope(), Symbols::AsyncCatchErrorCallback()); | 144 preamble_->scope(), Symbols::AsyncCatchErrorCallback()); |
147 LocalVariable* result_param = GetVariableInScope( | 145 LocalVariable* result_param = |
148 preamble_->scope(), Symbols::AsyncOperationParam()); | 146 GetVariableInScope(preamble_->scope(), Symbols::AsyncOperationParam()); |
149 LocalVariable* error_param = GetVariableInScope( | 147 LocalVariable* error_param = GetVariableInScope( |
150 preamble_->scope(), Symbols::AsyncOperationErrorParam()); | 148 preamble_->scope(), Symbols::AsyncOperationErrorParam()); |
151 LocalVariable* stack_trace_param = GetVariableInScope( | 149 LocalVariable* stack_trace_param = GetVariableInScope( |
152 preamble_->scope(), Symbols::AsyncOperationStackTraceParam()); | 150 preamble_->scope(), Symbols::AsyncOperationStackTraceParam()); |
153 | 151 |
154 AstNode* transformed_expr = Transform(node->expr()); | 152 AstNode* transformed_expr = Transform(node->expr()); |
155 LocalVariable* await_temp = AddNewTempVarToPreamble(transformed_expr, | 153 LocalVariable* await_temp = |
156 ST(node->token_pos())); | 154 AddNewTempVarToPreamble(transformed_expr, ST(node->token_pos())); |
157 | 155 |
158 AwaitMarkerNode* await_marker = | 156 AwaitMarkerNode* await_marker = |
159 new(Z) AwaitMarkerNode(async_temp_scope_, node->scope(), token_pos); | 157 new (Z) AwaitMarkerNode(async_temp_scope_, node->scope(), token_pos); |
160 preamble_->Add(await_marker); | 158 preamble_->Add(await_marker); |
161 | 159 |
162 // :result_param = _awaitHelper( | 160 // :result_param = _awaitHelper( |
163 // :await_temp, :async_then_callback, :async_catch_error_callback) | 161 // :await_temp, :async_then_callback, :async_catch_error_callback) |
164 const Library& async_lib = Library::Handle(Library::AsyncLibrary()); | 162 const Library& async_lib = Library::Handle(Library::AsyncLibrary()); |
165 const Function& async_await_helper = Function::ZoneHandle( | 163 const Function& async_await_helper = Function::ZoneHandle( |
166 Z, async_lib.LookupFunctionAllowPrivate(Symbols::AsyncAwaitHelper())); | 164 Z, async_lib.LookupFunctionAllowPrivate(Symbols::AsyncAwaitHelper())); |
167 ASSERT(!async_await_helper.IsNull()); | 165 ASSERT(!async_await_helper.IsNull()); |
168 ArgumentListNode* async_await_helper_args = | 166 ArgumentListNode* async_await_helper_args = |
169 new(Z) ArgumentListNode(token_pos); | 167 new (Z) ArgumentListNode(token_pos); |
| 168 async_await_helper_args->Add(new (Z) LoadLocalNode(token_pos, await_temp)); |
170 async_await_helper_args->Add( | 169 async_await_helper_args->Add( |
171 new(Z) LoadLocalNode(token_pos, await_temp)); | 170 new (Z) LoadLocalNode(token_pos, async_then_callback)); |
172 async_await_helper_args->Add( | 171 async_await_helper_args->Add( |
173 new(Z) LoadLocalNode(token_pos, async_then_callback)); | 172 new (Z) LoadLocalNode(token_pos, async_catch_error_callback)); |
174 async_await_helper_args->Add( | 173 StaticCallNode* await_helper_call = new (Z) StaticCallNode( |
175 new(Z) LoadLocalNode(token_pos, async_catch_error_callback)); | 174 node->token_pos(), async_await_helper, async_await_helper_args); |
176 StaticCallNode* await_helper_call = new(Z) StaticCallNode( | |
177 node->token_pos(), | |
178 async_await_helper, | |
179 async_await_helper_args); | |
180 | 175 |
181 preamble_->Add(new(Z) StoreLocalNode( | 176 preamble_->Add( |
182 token_pos, result_param, await_helper_call)); | 177 new (Z) StoreLocalNode(token_pos, result_param, await_helper_call)); |
183 | 178 |
184 ReturnNode* continuation_return = new(Z) ReturnNode(token_pos); | 179 ReturnNode* continuation_return = new (Z) ReturnNode(token_pos); |
185 continuation_return->set_return_type(ReturnNode::kContinuationTarget); | 180 continuation_return->set_return_type(ReturnNode::kContinuationTarget); |
186 preamble_->Add(continuation_return); | 181 preamble_->Add(continuation_return); |
187 | 182 |
188 // If this expression is part of a try block, also append the code for | 183 // If this expression is part of a try block, also append the code for |
189 // restoring the saved try context that lives on the stack and possibly the | 184 // restoring the saved try context that lives on the stack and possibly the |
190 // saved try context of the outer try block. | 185 // saved try context of the outer try block. |
191 if (node->saved_try_ctx() != NULL) { | 186 if (node->saved_try_ctx() != NULL) { |
192 preamble_->Add(new(Z) StoreLocalNode( | 187 preamble_->Add(new (Z) StoreLocalNode( |
193 token_pos, | 188 token_pos, node->saved_try_ctx(), |
194 node->saved_try_ctx(), | 189 new (Z) LoadLocalNode(token_pos, node->async_saved_try_ctx()))); |
195 new(Z) LoadLocalNode(token_pos, | |
196 node->async_saved_try_ctx()))); | |
197 if (node->outer_saved_try_ctx() != NULL) { | 190 if (node->outer_saved_try_ctx() != NULL) { |
198 preamble_->Add(new(Z) StoreLocalNode( | 191 preamble_->Add(new (Z) StoreLocalNode( |
199 token_pos, | 192 token_pos, node->outer_saved_try_ctx(), |
200 node->outer_saved_try_ctx(), | 193 new (Z) LoadLocalNode(token_pos, node->outer_async_saved_try_ctx()))); |
201 new(Z) LoadLocalNode(token_pos, | |
202 node->outer_async_saved_try_ctx()))); | |
203 } | 194 } |
204 } else { | 195 } else { |
205 ASSERT(node->outer_saved_try_ctx() == NULL); | 196 ASSERT(node->outer_saved_try_ctx() == NULL); |
206 } | 197 } |
207 | 198 |
208 // Load the async_op variable. It is unused, but the observatory uses it | 199 // Load the async_op variable. It is unused, but the observatory uses it |
209 // to determine if a breakpoint is inside an asynchronous function. | 200 // to determine if a breakpoint is inside an asynchronous function. |
210 LoadLocalNode* load_async_op = new(Z) LoadLocalNode(token_pos, async_op); | 201 LoadLocalNode* load_async_op = new (Z) LoadLocalNode(token_pos, async_op); |
211 preamble_->Add(load_async_op); | 202 preamble_->Add(load_async_op); |
212 | 203 |
213 LoadLocalNode* load_error_param = new(Z) LoadLocalNode( | 204 LoadLocalNode* load_error_param = |
214 token_pos, error_param); | 205 new (Z) LoadLocalNode(token_pos, error_param); |
215 LoadLocalNode* load_stack_trace_param = new(Z) LoadLocalNode( | 206 LoadLocalNode* load_stack_trace_param = |
216 token_pos, stack_trace_param); | 207 new (Z) LoadLocalNode(token_pos, stack_trace_param); |
217 SequenceNode* error_ne_null_branch = new(Z) SequenceNode( | 208 SequenceNode* error_ne_null_branch = |
218 token_pos, ChainNewScope(preamble_->scope())); | 209 new (Z) SequenceNode(token_pos, ChainNewScope(preamble_->scope())); |
219 error_ne_null_branch->Add(new(Z) ThrowNode( | 210 error_ne_null_branch->Add( |
220 token_pos, | 211 new (Z) ThrowNode(token_pos, load_error_param, load_stack_trace_param)); |
221 load_error_param, | 212 preamble_->Add(new (Z) IfNode( |
222 load_stack_trace_param)); | 213 token_pos, new (Z) ComparisonNode( |
223 preamble_->Add(new(Z) IfNode( | 214 token_pos, Token::kNE, load_error_param, |
224 token_pos, | 215 new (Z) LiteralNode(token_pos, Object::null_instance())), |
225 new(Z) ComparisonNode( | 216 error_ne_null_branch, NULL)); |
226 token_pos, | |
227 Token::kNE, | |
228 load_error_param, | |
229 new(Z) LiteralNode(token_pos, | |
230 Object::null_instance())), | |
231 error_ne_null_branch, | |
232 NULL)); | |
233 | 217 |
234 LocalVariable* result = AddNewTempVarToPreamble(new(Z) LoadLocalNode( | 218 LocalVariable* result = AddNewTempVarToPreamble( |
235 token_pos, result_param), ST(node->token_pos())); | 219 new (Z) LoadLocalNode(token_pos, result_param), ST(node->token_pos())); |
236 result_ = new(Z) LoadLocalNode(token_pos, result); | 220 result_ = new (Z) LoadLocalNode(token_pos, result); |
237 } | 221 } |
238 | 222 |
239 | 223 |
240 // Transforms boolean expressions into a sequence of evaluatons that only lazily | 224 // Transforms boolean expressions into a sequence of evaluatons that only lazily |
241 // evaluate subexpressions. | 225 // evaluate subexpressions. |
242 // | 226 // |
243 // Example: | 227 // Example: |
244 // | 228 // |
245 // (a || b) only evaluates b if a is false | 229 // (a || b) only evaluates b if a is false |
246 // | 230 // |
247 // Transformation (roughly): | 231 // Transformation (roughly): |
248 // | 232 // |
249 // t_1 = a; | 233 // t_1 = a; |
250 // if (!t_1) { | 234 // if (!t_1) { |
251 // t_2 = b; | 235 // t_2 = b; |
252 // } | 236 // } |
253 // t_3 = t_1 || t_2; // Compiler takes care that lazy evaluation takes place | 237 // t_3 = t_1 || t_2; // Compiler takes care that lazy evaluation takes place |
254 // on this level. | 238 // on this level. |
255 AstNode* AwaitTransformer::LazyTransform(const Token::Kind logical_op, | 239 AstNode* AwaitTransformer::LazyTransform(const Token::Kind logical_op, |
256 AstNode* new_left, | 240 AstNode* new_left, |
257 AstNode* right) { | 241 AstNode* right) { |
258 ASSERT(logical_op == Token::kAND || logical_op == Token::kOR); | 242 ASSERT(logical_op == Token::kAND || logical_op == Token::kOR); |
259 AstNode* result = NULL; | 243 AstNode* result = NULL; |
260 const Token::Kind compare_logical_op = (logical_op == Token::kAND) ? | 244 const Token::Kind compare_logical_op = |
261 Token::kEQ : Token::kNE; | 245 (logical_op == Token::kAND) ? Token::kEQ : Token::kNE; |
262 SequenceNode* eval = new(Z) SequenceNode( | 246 SequenceNode* eval = new (Z) SequenceNode(ST(new_left->token_pos()), |
263 ST(new_left->token_pos()), ChainNewScope(preamble_->scope())); | 247 ChainNewScope(preamble_->scope())); |
264 SequenceNode* saved_preamble = preamble_; | 248 SequenceNode* saved_preamble = preamble_; |
265 preamble_ = eval; | 249 preamble_ = eval; |
266 result = Transform(right); | 250 result = Transform(right); |
267 preamble_ = saved_preamble; | 251 preamble_ = saved_preamble; |
268 IfNode* right_body = new(Z) IfNode( | 252 IfNode* right_body = new (Z) |
269 ST(new_left->token_pos()), | 253 IfNode(ST(new_left->token_pos()), |
270 new(Z) ComparisonNode( | 254 new (Z) ComparisonNode( |
271 ST(new_left->token_pos()), | 255 ST(new_left->token_pos()), compare_logical_op, new_left, |
272 compare_logical_op, | 256 new (Z) LiteralNode(ST(new_left->token_pos()), Bool::True())), |
273 new_left, | 257 eval, NULL); |
274 new(Z) LiteralNode(ST(new_left->token_pos()), Bool::True())), | |
275 eval, | |
276 NULL); | |
277 preamble_->Add(right_body); | 258 preamble_->Add(right_body); |
278 return result; | 259 return result; |
279 } | 260 } |
280 | 261 |
281 | 262 |
282 LocalScope* AwaitTransformer::ChainNewScope(LocalScope* parent) { | 263 LocalScope* AwaitTransformer::ChainNewScope(LocalScope* parent) { |
283 return new(Z) LocalScope( | 264 return new (Z) |
284 parent, parent->function_level(), parent->loop_level()); | 265 LocalScope(parent, parent->function_level(), parent->loop_level()); |
285 } | 266 } |
286 | 267 |
287 | 268 |
288 void AwaitTransformer::VisitBinaryOpNode(BinaryOpNode* node) { | 269 void AwaitTransformer::VisitBinaryOpNode(BinaryOpNode* node) { |
289 AstNode* new_left = Transform(node->left()); | 270 AstNode* new_left = Transform(node->left()); |
290 AstNode* new_right = NULL; | 271 AstNode* new_right = NULL; |
291 // Preserve lazy evaluaton. | 272 // Preserve lazy evaluaton. |
292 if ((node->kind() == Token::kAND) || (node->kind() == Token::kOR)) { | 273 if ((node->kind() == Token::kAND) || (node->kind() == Token::kOR)) { |
293 new_right = LazyTransform(node->kind(), new_left, node->right()); | 274 new_right = LazyTransform(node->kind(), new_left, node->right()); |
294 } else { | 275 } else { |
295 new_right = Transform(node->right()); | 276 new_right = Transform(node->right()); |
296 } | 277 } |
297 result_ = MakeName(new(Z) BinaryOpNode(node->token_pos(), | 278 result_ = MakeName(new (Z) BinaryOpNode(node->token_pos(), node->kind(), |
298 node->kind(), | 279 new_left, new_right)); |
299 new_left, | |
300 new_right)); | |
301 } | 280 } |
302 | 281 |
303 | 282 |
304 void AwaitTransformer::VisitComparisonNode(ComparisonNode* node) { | 283 void AwaitTransformer::VisitComparisonNode(ComparisonNode* node) { |
305 AstNode* new_left = Transform(node->left()); | 284 AstNode* new_left = Transform(node->left()); |
306 AstNode* new_right = Transform(node->right()); | 285 AstNode* new_right = Transform(node->right()); |
307 result_ = MakeName(new(Z) ComparisonNode(node->token_pos(), | 286 result_ = MakeName(new (Z) ComparisonNode(node->token_pos(), node->kind(), |
308 node->kind(), | 287 new_left, new_right)); |
309 new_left, | |
310 new_right)); | |
311 } | 288 } |
312 | 289 |
313 | 290 |
314 void AwaitTransformer::VisitUnaryOpNode(UnaryOpNode* node) { | 291 void AwaitTransformer::VisitUnaryOpNode(UnaryOpNode* node) { |
315 AstNode* new_operand = Transform(node->operand()); | 292 AstNode* new_operand = Transform(node->operand()); |
316 result_ = MakeName(new(Z) UnaryOpNode(node->token_pos(), | 293 result_ = MakeName( |
317 node->kind(), | 294 new (Z) UnaryOpNode(node->token_pos(), node->kind(), new_operand)); |
318 new_operand)); | |
319 } | 295 } |
320 | 296 |
321 | 297 |
322 // ::= (<condition>) ? <true-branch> : <false-branch> | 298 // ::= (<condition>) ? <true-branch> : <false-branch> |
323 // | 299 // |
324 void AwaitTransformer::VisitConditionalExprNode(ConditionalExprNode* node) { | 300 void AwaitTransformer::VisitConditionalExprNode(ConditionalExprNode* node) { |
325 AstNode* new_condition = Transform(node->condition()); | 301 AstNode* new_condition = Transform(node->condition()); |
326 SequenceNode* new_true = new(Z) SequenceNode( | 302 SequenceNode* new_true = new (Z) SequenceNode( |
327 ST(node->true_expr()->token_pos()), ChainNewScope(preamble_->scope())); | 303 ST(node->true_expr()->token_pos()), ChainNewScope(preamble_->scope())); |
328 SequenceNode* saved_preamble = preamble_; | 304 SequenceNode* saved_preamble = preamble_; |
329 preamble_ = new_true; | 305 preamble_ = new_true; |
330 AstNode* new_true_result = Transform(node->true_expr()); | 306 AstNode* new_true_result = Transform(node->true_expr()); |
331 SequenceNode* new_false = new(Z) SequenceNode( | 307 SequenceNode* new_false = new (Z) SequenceNode( |
332 ST(node->false_expr()->token_pos()), ChainNewScope(preamble_->scope())); | 308 ST(node->false_expr()->token_pos()), ChainNewScope(preamble_->scope())); |
333 preamble_ = new_false; | 309 preamble_ = new_false; |
334 AstNode* new_false_result = Transform(node->false_expr()); | 310 AstNode* new_false_result = Transform(node->false_expr()); |
335 preamble_ = saved_preamble; | 311 preamble_ = saved_preamble; |
336 IfNode* new_if = new(Z) IfNode(ST(node->token_pos()), | 312 IfNode* new_if = |
337 new_condition, | 313 new (Z) IfNode(ST(node->token_pos()), new_condition, new_true, new_false); |
338 new_true, | |
339 new_false); | |
340 preamble_->Add(new_if); | 314 preamble_->Add(new_if); |
341 result_ = MakeName(new(Z) ConditionalExprNode(ST(node->token_pos()), | 315 result_ = MakeName(new (Z) ConditionalExprNode( |
342 new_condition, | 316 ST(node->token_pos()), new_condition, new_true_result, new_false_result)); |
343 new_true_result, | |
344 new_false_result)); | |
345 } | 317 } |
346 | 318 |
347 | 319 |
348 void AwaitTransformer::VisitArgumentListNode(ArgumentListNode* node) { | 320 void AwaitTransformer::VisitArgumentListNode(ArgumentListNode* node) { |
349 ArgumentListNode* new_args = new(Z) ArgumentListNode(node->token_pos()); | 321 ArgumentListNode* new_args = new (Z) ArgumentListNode(node->token_pos()); |
350 for (intptr_t i = 0; i < node->length(); i++) { | 322 for (intptr_t i = 0; i < node->length(); i++) { |
351 new_args->Add(Transform(node->NodeAt(i))); | 323 new_args->Add(Transform(node->NodeAt(i))); |
352 } | 324 } |
353 new_args->set_names(node->names()); | 325 new_args->set_names(node->names()); |
354 result_ = new_args; | 326 result_ = new_args; |
355 } | 327 } |
356 | 328 |
357 | 329 |
358 void AwaitTransformer::VisitArrayNode(ArrayNode* node) { | 330 void AwaitTransformer::VisitArrayNode(ArrayNode* node) { |
359 GrowableArray<AstNode*> new_elements; | 331 GrowableArray<AstNode*> new_elements; |
360 for (intptr_t i = 0; i < node->length(); i++) { | 332 for (intptr_t i = 0; i < node->length(); i++) { |
361 new_elements.Add(Transform(node->ElementAt(i))); | 333 new_elements.Add(Transform(node->ElementAt(i))); |
362 } | 334 } |
363 result_ = new(Z) ArrayNode(node->token_pos(), node->type(), new_elements); | 335 result_ = new (Z) ArrayNode(node->token_pos(), node->type(), new_elements); |
364 } | 336 } |
365 | 337 |
366 | 338 |
367 void AwaitTransformer::VisitStringInterpolateNode(StringInterpolateNode* node) { | 339 void AwaitTransformer::VisitStringInterpolateNode(StringInterpolateNode* node) { |
368 ArrayNode* new_value = Transform(node->value())->AsArrayNode(); | 340 ArrayNode* new_value = Transform(node->value())->AsArrayNode(); |
369 result_ = MakeName(new(Z) StringInterpolateNode(node->token_pos(), | 341 result_ = |
370 new_value)); | 342 MakeName(new (Z) StringInterpolateNode(node->token_pos(), new_value)); |
371 } | 343 } |
372 | 344 |
373 | 345 |
374 void AwaitTransformer::VisitClosureNode(ClosureNode* node) { | 346 void AwaitTransformer::VisitClosureNode(ClosureNode* node) { |
375 AstNode* new_receiver = node->receiver(); | 347 AstNode* new_receiver = node->receiver(); |
376 if (new_receiver != NULL) { | 348 if (new_receiver != NULL) { |
377 new_receiver = Transform(new_receiver); | 349 new_receiver = Transform(new_receiver); |
378 } | 350 } |
379 result_ = MakeName(new(Z) ClosureNode(node->token_pos(), | 351 result_ = MakeName(new (Z) ClosureNode(node->token_pos(), node->function(), |
380 node->function(), | 352 new_receiver, node->scope())); |
381 new_receiver, | |
382 node->scope())); | |
383 } | 353 } |
384 | 354 |
385 | 355 |
386 void AwaitTransformer::VisitInstanceCallNode(InstanceCallNode* node) { | 356 void AwaitTransformer::VisitInstanceCallNode(InstanceCallNode* node) { |
387 AstNode* new_receiver = Transform(node->receiver()); | 357 AstNode* new_receiver = Transform(node->receiver()); |
388 ArgumentListNode* new_args = | 358 ArgumentListNode* new_args = |
389 Transform(node->arguments())->AsArgumentListNode(); | 359 Transform(node->arguments())->AsArgumentListNode(); |
390 result_ = MakeName(new(Z) InstanceCallNode(node->token_pos(), | 360 result_ = MakeName(new (Z) InstanceCallNode(node->token_pos(), new_receiver, |
391 new_receiver, | 361 node->function_name(), new_args, |
392 node->function_name(), | 362 node->is_conditional())); |
393 new_args, | |
394 node->is_conditional())); | |
395 } | 363 } |
396 | 364 |
397 | 365 |
398 void AwaitTransformer::VisitStaticCallNode(StaticCallNode* node) { | 366 void AwaitTransformer::VisitStaticCallNode(StaticCallNode* node) { |
399 ArgumentListNode* new_args = | 367 ArgumentListNode* new_args = |
400 Transform(node->arguments())->AsArgumentListNode(); | 368 Transform(node->arguments())->AsArgumentListNode(); |
401 result_ = MakeName(new(Z) StaticCallNode(node->token_pos(), | 369 result_ = MakeName( |
402 node->function(), | 370 new (Z) StaticCallNode(node->token_pos(), node->function(), new_args)); |
403 new_args)); | |
404 } | 371 } |
405 | 372 |
406 | 373 |
407 void AwaitTransformer::VisitConstructorCallNode(ConstructorCallNode* node) { | 374 void AwaitTransformer::VisitConstructorCallNode(ConstructorCallNode* node) { |
408 ArgumentListNode* new_args = | 375 ArgumentListNode* new_args = |
409 Transform(node->arguments())->AsArgumentListNode(); | 376 Transform(node->arguments())->AsArgumentListNode(); |
410 result_ = MakeName(new(Z) ConstructorCallNode(node->token_pos(), | 377 result_ = MakeName( |
411 node->type_arguments(), | 378 new (Z) ConstructorCallNode(node->token_pos(), node->type_arguments(), |
412 node->constructor(), | 379 node->constructor(), new_args)); |
413 new_args)); | |
414 } | 380 } |
415 | 381 |
416 | 382 |
417 void AwaitTransformer::VisitInstanceGetterNode(InstanceGetterNode* node) { | 383 void AwaitTransformer::VisitInstanceGetterNode(InstanceGetterNode* node) { |
418 AstNode* new_receiver = Transform(node->receiver()); | 384 AstNode* new_receiver = Transform(node->receiver()); |
419 result_ = MakeName(new(Z) InstanceGetterNode(node->token_pos(), | 385 result_ = MakeName(new (Z) InstanceGetterNode(node->token_pos(), new_receiver, |
420 new_receiver, | 386 node->field_name(), |
421 node->field_name(), | 387 node->is_conditional())); |
422 node->is_conditional())); | |
423 } | 388 } |
424 | 389 |
425 | 390 |
426 void AwaitTransformer::VisitInstanceSetterNode(InstanceSetterNode* node) { | 391 void AwaitTransformer::VisitInstanceSetterNode(InstanceSetterNode* node) { |
427 AstNode* new_receiver = node->receiver(); | 392 AstNode* new_receiver = node->receiver(); |
428 if (new_receiver != NULL) { | 393 if (new_receiver != NULL) { |
429 new_receiver = Transform(new_receiver); | 394 new_receiver = Transform(new_receiver); |
430 } | 395 } |
431 AstNode* new_value = Transform(node->value()); | 396 AstNode* new_value = Transform(node->value()); |
432 result_ = MakeName(new(Z) InstanceSetterNode(node->token_pos(), | 397 result_ = MakeName(new (Z) InstanceSetterNode(node->token_pos(), new_receiver, |
433 new_receiver, | 398 node->field_name(), new_value, |
434 node->field_name(), | 399 node->is_conditional())); |
435 new_value, | |
436 node->is_conditional())); | |
437 } | 400 } |
438 | 401 |
439 | 402 |
440 void AwaitTransformer::VisitStaticGetterNode(StaticGetterNode* node) { | 403 void AwaitTransformer::VisitStaticGetterNode(StaticGetterNode* node) { |
441 AstNode* new_receiver = node->receiver(); | 404 AstNode* new_receiver = node->receiver(); |
442 if (new_receiver != NULL) { | 405 if (new_receiver != NULL) { |
443 new_receiver = Transform(new_receiver); | 406 new_receiver = Transform(new_receiver); |
444 } | 407 } |
445 StaticGetterNode* new_getter = | 408 StaticGetterNode* new_getter = new (Z) StaticGetterNode( |
446 new(Z) StaticGetterNode(node->token_pos(), | 409 node->token_pos(), new_receiver, node->cls(), node->field_name()); |
447 new_receiver, | |
448 node->cls(), | |
449 node->field_name()); | |
450 new_getter->set_owner(node->owner()); | 410 new_getter->set_owner(node->owner()); |
451 result_ = MakeName(new_getter); | 411 result_ = MakeName(new_getter); |
452 } | 412 } |
453 | 413 |
454 | 414 |
455 void AwaitTransformer::VisitStaticSetterNode(StaticSetterNode* node) { | 415 void AwaitTransformer::VisitStaticSetterNode(StaticSetterNode* node) { |
456 AstNode* new_receiver = node->receiver(); | 416 AstNode* new_receiver = node->receiver(); |
457 if (new_receiver != NULL) { | 417 if (new_receiver != NULL) { |
458 new_receiver = Transform(new_receiver); | 418 new_receiver = Transform(new_receiver); |
459 } | 419 } |
460 AstNode* new_value = Transform(node->value()); | 420 AstNode* new_value = Transform(node->value()); |
461 StaticSetterNode* new_setter = | 421 StaticSetterNode* new_setter = |
462 node->function().IsNull() | 422 node->function().IsNull() |
463 ? new(Z) StaticSetterNode(node->token_pos(), | 423 ? new (Z) StaticSetterNode(node->token_pos(), new_receiver, |
464 new_receiver, | 424 node->cls(), node->field_name(), new_value) |
465 node->cls(), | 425 : new (Z) StaticSetterNode(node->token_pos(), new_receiver, |
466 node->field_name(), | 426 node->field_name(), node->function(), |
467 new_value) | 427 new_value); |
468 : new(Z) StaticSetterNode(node->token_pos(), | |
469 new_receiver, | |
470 node->field_name(), | |
471 node->function(), | |
472 new_value); | |
473 | 428 |
474 result_ = MakeName(new_setter); | 429 result_ = MakeName(new_setter); |
475 } | 430 } |
476 | 431 |
477 | 432 |
478 void AwaitTransformer::VisitLoadLocalNode(LoadLocalNode* node) { | 433 void AwaitTransformer::VisitLoadLocalNode(LoadLocalNode* node) { |
479 result_ = MakeName(node); | 434 result_ = MakeName(node); |
480 } | 435 } |
481 | 436 |
482 | 437 |
483 void AwaitTransformer::VisitStoreLocalNode(StoreLocalNode* node) { | 438 void AwaitTransformer::VisitStoreLocalNode(StoreLocalNode* node) { |
484 AstNode* new_value = Transform(node->value()); | 439 AstNode* new_value = Transform(node->value()); |
485 result_ = MakeName(new(Z) StoreLocalNode(node->token_pos(), | 440 result_ = MakeName( |
486 &node->local(), | 441 new (Z) StoreLocalNode(node->token_pos(), &node->local(), new_value)); |
487 new_value)); | |
488 } | 442 } |
489 | 443 |
490 | 444 |
491 void AwaitTransformer::VisitLoadStaticFieldNode(LoadStaticFieldNode* node) { | 445 void AwaitTransformer::VisitLoadStaticFieldNode(LoadStaticFieldNode* node) { |
492 result_ = MakeName(node); | 446 result_ = MakeName(node); |
493 } | 447 } |
494 | 448 |
495 | 449 |
496 void AwaitTransformer::VisitStoreStaticFieldNode(StoreStaticFieldNode* node) { | 450 void AwaitTransformer::VisitStoreStaticFieldNode(StoreStaticFieldNode* node) { |
497 AstNode* new_value = Transform(node->value()); | 451 AstNode* new_value = Transform(node->value()); |
498 result_ = MakeName(new(Z) StoreStaticFieldNode(node->token_pos(), | 452 result_ = MakeName(new (Z) StoreStaticFieldNode( |
499 Field::ZoneHandle(Z, node->field().Original()), | 453 node->token_pos(), Field::ZoneHandle(Z, node->field().Original()), |
500 new_value)); | 454 new_value)); |
501 } | 455 } |
502 | 456 |
503 | 457 |
504 void AwaitTransformer::VisitLoadIndexedNode(LoadIndexedNode* node) { | 458 void AwaitTransformer::VisitLoadIndexedNode(LoadIndexedNode* node) { |
505 AstNode* new_array = Transform(node->array()); | 459 AstNode* new_array = Transform(node->array()); |
506 AstNode* new_index = Transform(node->index_expr()); | 460 AstNode* new_index = Transform(node->index_expr()); |
507 result_ = MakeName(new(Z) LoadIndexedNode(node->token_pos(), | 461 result_ = MakeName(new (Z) LoadIndexedNode(node->token_pos(), new_array, |
508 new_array, | 462 new_index, node->super_class())); |
509 new_index, | |
510 node->super_class())); | |
511 } | 463 } |
512 | 464 |
513 | 465 |
514 void AwaitTransformer::VisitStoreIndexedNode(StoreIndexedNode* node) { | 466 void AwaitTransformer::VisitStoreIndexedNode(StoreIndexedNode* node) { |
515 AstNode* new_array = Transform(node->array()); | 467 AstNode* new_array = Transform(node->array()); |
516 AstNode* new_index = Transform(node->index_expr()); | 468 AstNode* new_index = Transform(node->index_expr()); |
517 AstNode* new_value = Transform(node->value()); | 469 AstNode* new_value = Transform(node->value()); |
518 result_ = MakeName(new(Z) StoreIndexedNode(node->token_pos(), | 470 result_ = MakeName(new (Z) StoreIndexedNode( |
519 new_array, | 471 node->token_pos(), new_array, new_index, new_value, node->super_class())); |
520 new_index, | |
521 new_value, | |
522 node->super_class())); | |
523 } | 472 } |
524 | 473 |
525 | 474 |
526 void AwaitTransformer::VisitAssignableNode(AssignableNode* node) { | 475 void AwaitTransformer::VisitAssignableNode(AssignableNode* node) { |
527 AstNode* new_expr = Transform(node->expr()); | 476 AstNode* new_expr = Transform(node->expr()); |
528 result_ = MakeName(new(Z) AssignableNode(node->token_pos(), | 477 result_ = MakeName(new (Z) AssignableNode(node->token_pos(), new_expr, |
529 new_expr, | 478 node->type(), node->dst_name())); |
530 node->type(), | |
531 node->dst_name())); | |
532 } | 479 } |
533 | 480 |
534 | 481 |
535 void AwaitTransformer::VisitLetNode(LetNode* node) { | 482 void AwaitTransformer::VisitLetNode(LetNode* node) { |
536 // Add all the initializer nodes to the preamble and the | 483 // Add all the initializer nodes to the preamble and the |
537 // temporary variables to the scope for async temporary variables. | 484 // temporary variables to the scope for async temporary variables. |
538 // The temporary variables will be captured as a side effect of being | 485 // The temporary variables will be captured as a side effect of being |
539 // added to a scope, and the subsequent nodes that are added to the | 486 // added to a scope, and the subsequent nodes that are added to the |
540 // preample can access them. | 487 // preample can access them. |
541 for (intptr_t i = 0; i < node->num_temps(); i++) { | 488 for (intptr_t i = 0; i < node->num_temps(); i++) { |
542 async_temp_scope_->AddVariable(node->TempAt(i)); | 489 async_temp_scope_->AddVariable(node->TempAt(i)); |
543 AstNode* new_init_val = Transform(node->InitializerAt(i)); | 490 AstNode* new_init_val = Transform(node->InitializerAt(i)); |
544 preamble_->Add(new(Z) StoreLocalNode(node->token_pos(), | 491 preamble_->Add(new (Z) StoreLocalNode(node->token_pos(), node->TempAt(i), |
545 node->TempAt(i), | 492 new_init_val)); |
546 new_init_val)); | |
547 } | 493 } |
548 | 494 |
549 // Add all expressions but the last to the preamble. We must do | 495 // Add all expressions but the last to the preamble. We must do |
550 // this because subexpressions of the awaitable expression we | 496 // this because subexpressions of the awaitable expression we |
551 // are currently transforming may depend on each other, | 497 // are currently transforming may depend on each other, |
552 // e.g. await foo(a++, a++). Thus we must preserve the order of the | 498 // e.g. await foo(a++, a++). Thus we must preserve the order of the |
553 // transformed subexpressions. | 499 // transformed subexpressions. |
554 for (intptr_t i = 0; i < node->nodes().length() - 1; i++) { | 500 for (intptr_t i = 0; i < node->nodes().length() - 1; i++) { |
555 preamble_->Add(Transform(node->nodes()[i])); | 501 preamble_->Add(Transform(node->nodes()[i])); |
556 } | 502 } |
557 | 503 |
558 // The last expression in the let node is the value of the node. | 504 // The last expression in the let node is the value of the node. |
559 // The result of the transformed let node is this expression. | 505 // The result of the transformed let node is this expression. |
560 ASSERT(node->nodes().length() > 0); | 506 ASSERT(node->nodes().length() > 0); |
561 const intptr_t last_node_index = node->nodes().length() - 1; | 507 const intptr_t last_node_index = node->nodes().length() - 1; |
562 result_ = Transform(node->nodes()[last_node_index]); | 508 result_ = Transform(node->nodes()[last_node_index]); |
563 } | 509 } |
564 | 510 |
565 | 511 |
566 void AwaitTransformer::VisitThrowNode(ThrowNode* node) { | 512 void AwaitTransformer::VisitThrowNode(ThrowNode* node) { |
567 AstNode* new_exception = Transform(node->exception()); | 513 AstNode* new_exception = Transform(node->exception()); |
568 result_ = MakeName(new(Z) ThrowNode(node->token_pos(), | 514 result_ = MakeName( |
569 new_exception, | 515 new (Z) ThrowNode(node->token_pos(), new_exception, node->stacktrace())); |
570 node->stacktrace())); | |
571 } | 516 } |
572 | 517 |
573 } // namespace dart | 518 } // namespace dart |
OLD | NEW |