| 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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 | 105 |
| 106 void AwaitTransformer::VisitTypeNode(TypeNode* node) { | 106 void AwaitTransformer::VisitTypeNode(TypeNode* node) { |
| 107 result_ = new(Z) TypeNode(node->token_pos(), node->type()); | 107 result_ = new(Z) TypeNode(node->token_pos(), node->type()); |
| 108 } | 108 } |
| 109 | 109 |
| 110 | 110 |
| 111 void AwaitTransformer::VisitAwaitNode(AwaitNode* node) { | 111 void AwaitTransformer::VisitAwaitNode(AwaitNode* node) { |
| 112 // Await transformation: | 112 // Await transformation: |
| 113 // | 113 // |
| 114 // :await_temp_var_X = <expr>; | 114 // :await_temp_var_X = <expr>; |
| 115 // :result_param = :await_temp_var_X; | |
| 116 // if (:result_param is !Future) { | |
| 117 // :result_param = Future.value(:result_param); | |
| 118 // } | |
| 119 // AwaitMarker(kNewContinuationState); | 115 // AwaitMarker(kNewContinuationState); |
| 120 // :result_param = :result_param.then(:async_op); | 116 // :result_param = _awaitHelper( |
| 121 // _asyncCatchHelper(:result_param.catchError, :async_op); | 117 // :await_temp_var_X, :async_then_callback, :async_catch_error_callback); |
| 122 // return; // (return_type() == kContinuationTarget) | 118 // return; // (return_type() == kContinuationTarget) |
| 123 // | 119 // |
| 124 // :saved_try_ctx_var = :await_saved_try_ctx_var_y; | 120 // :saved_try_ctx_var = :await_saved_try_ctx_var_y; |
| 125 // :await_temp_var_(X+1) = :result_param; | 121 // :await_temp_var_(X+1) = :result_param; |
| 126 | 122 |
| 127 LocalVariable* async_op = GetVariableInScope( | 123 LocalVariable* async_then_callback = GetVariableInScope( |
| 128 preamble_->scope(), Symbols::AsyncOperation()); | 124 preamble_->scope(), Symbols::AsyncThenCallback()); |
| 125 LocalVariable* async_catch_error_callback = GetVariableInScope( |
| 126 preamble_->scope(), Symbols::AsyncCatchErrorCallback()); |
| 129 LocalVariable* result_param = GetVariableInScope( | 127 LocalVariable* result_param = GetVariableInScope( |
| 130 preamble_->scope(), Symbols::AsyncOperationParam()); | 128 preamble_->scope(), Symbols::AsyncOperationParam()); |
| 131 LocalVariable* error_param = GetVariableInScope( | 129 LocalVariable* error_param = GetVariableInScope( |
| 132 preamble_->scope(), Symbols::AsyncOperationErrorParam()); | 130 preamble_->scope(), Symbols::AsyncOperationErrorParam()); |
| 133 LocalVariable* stack_trace_param = GetVariableInScope( | 131 LocalVariable* stack_trace_param = GetVariableInScope( |
| 134 preamble_->scope(), Symbols::AsyncOperationStackTraceParam()); | 132 preamble_->scope(), Symbols::AsyncOperationStackTraceParam()); |
| 135 | 133 |
| 136 AstNode* transformed_expr = Transform(node->expr()); | 134 AstNode* transformed_expr = Transform(node->expr()); |
| 137 preamble_->Add(new(Z) StoreLocalNode( | 135 LocalVariable* await_temp = AddToPreambleNewTempVar(transformed_expr); |
| 138 Scanner::kNoSourcePos, result_param, transformed_expr)); | |
| 139 | |
| 140 LoadLocalNode* load_result_param = new(Z) LoadLocalNode( | |
| 141 Scanner::kNoSourcePos, result_param); | |
| 142 | |
| 143 const Class& future_cls = | |
| 144 Class::ZoneHandle(Z, thread()->isolate()->object_store()->future_class()); | |
| 145 ASSERT(!future_cls.IsNull()); | |
| 146 const AbstractType& future_type = | |
| 147 AbstractType::ZoneHandle(Z, future_cls.RareType()); | |
| 148 ASSERT(!future_type.IsNull()); | |
| 149 | |
| 150 LocalScope* is_not_future_scope = ChainNewScope(preamble_->scope()); | |
| 151 SequenceNode* is_not_future_branch = | |
| 152 new (Z) SequenceNode(Scanner::kNoSourcePos, is_not_future_scope); | |
| 153 | |
| 154 // if (:result_param is !Future) { | |
| 155 // :result_param = Future.value(:result_param); | |
| 156 // } | |
| 157 const Function& value_ctor = Function::ZoneHandle( | |
| 158 Z, future_cls.LookupFunction(Symbols::FutureValue())); | |
| 159 ASSERT(!value_ctor.IsNull()); | |
| 160 ArgumentListNode* ctor_args = new (Z) ArgumentListNode(Scanner::kNoSourcePos); | |
| 161 ctor_args->Add(new (Z) LoadLocalNode(Scanner::kNoSourcePos, result_param)); | |
| 162 ConstructorCallNode* ctor_call = | |
| 163 new (Z) ConstructorCallNode(Scanner::kNoSourcePos, | |
| 164 TypeArguments::ZoneHandle(Z), | |
| 165 value_ctor, | |
| 166 ctor_args); | |
| 167 is_not_future_branch->Add(new (Z) StoreLocalNode( | |
| 168 Scanner::kNoSourcePos, result_param, ctor_call)); | |
| 169 AstNode* is_not_future_test = new (Z) ComparisonNode( | |
| 170 Scanner::kNoSourcePos, | |
| 171 Token::kISNOT, | |
| 172 load_result_param, | |
| 173 new (Z) TypeNode(Scanner::kNoSourcePos, future_type)); | |
| 174 preamble_->Add(new(Z) IfNode(Scanner::kNoSourcePos, | |
| 175 is_not_future_test, | |
| 176 is_not_future_branch, | |
| 177 NULL)); | |
| 178 | 136 |
| 179 AwaitMarkerNode* await_marker = new (Z) AwaitMarkerNode(); | 137 AwaitMarkerNode* await_marker = new (Z) AwaitMarkerNode(); |
| 180 await_marker->set_scope(preamble_->scope()); | 138 await_marker->set_scope(preamble_->scope()); |
| 181 preamble_->Add(await_marker); | 139 preamble_->Add(await_marker); |
| 182 ArgumentListNode* args = new(Z) ArgumentListNode(Scanner::kNoSourcePos); | |
| 183 | 140 |
| 184 args->Add(new(Z) LoadLocalNode(Scanner::kNoSourcePos, async_op)); | 141 // :result_param = _awaitHelper( |
| 185 preamble_->Add(new (Z) StoreLocalNode( | 142 // :await_temp, :async_then_callback, :async_catch_error_callback) |
| 186 Scanner::kNoSourcePos, | 143 const Library& async_lib = Library::Handle(Library::AsyncLibrary()); |
| 187 result_param, | 144 const Function& async_await_helper = Function::ZoneHandle( |
| 188 new(Z) InstanceCallNode(node->token_pos(), | 145 Z, async_lib.LookupFunctionAllowPrivate(Symbols::AsyncAwaitHelper())); |
| 189 load_result_param, | 146 ASSERT(!async_await_helper.IsNull()); |
| 190 Symbols::FutureThen(), | 147 ArgumentListNode* async_await_helper_args = new (Z) ArgumentListNode( |
| 191 args))); | |
| 192 const Library& core_lib = Library::Handle(Library::CoreLibrary()); | |
| 193 const Function& async_catch_helper = Function::ZoneHandle( | |
| 194 Z, core_lib.LookupFunctionAllowPrivate(Symbols::AsyncCatchHelper())); | |
| 195 ASSERT(!async_catch_helper.IsNull()); | |
| 196 ArgumentListNode* catch_helper_args = new (Z) ArgumentListNode( | |
| 197 Scanner::kNoSourcePos); | 148 Scanner::kNoSourcePos); |
| 198 InstanceGetterNode* catch_error_getter = new (Z) InstanceGetterNode( | 149 async_await_helper_args->Add( |
| 199 Scanner::kNoSourcePos, | 150 new(Z) LoadLocalNode(Scanner::kNoSourcePos, await_temp)); |
| 200 load_result_param, | 151 async_await_helper_args->Add( |
| 201 Symbols::FutureCatchError()); | 152 new(Z) LoadLocalNode(Scanner::kNoSourcePos, async_then_callback)); |
| 202 catch_helper_args->Add(catch_error_getter); | 153 async_await_helper_args->Add( |
| 203 catch_helper_args->Add(new (Z) LoadLocalNode( | 154 new(Z) LoadLocalNode(Scanner::kNoSourcePos, async_catch_error_callback)); |
| 204 Scanner::kNoSourcePos, async_op)); | 155 StaticCallNode* await_helper_call = new (Z) StaticCallNode( |
| 205 preamble_->Add(new (Z) StaticCallNode( | 156 node->token_pos(), |
| 206 Scanner::kNoSourcePos, | 157 async_await_helper, |
| 207 async_catch_helper, | 158 async_await_helper_args); |
| 208 catch_helper_args)); | 159 |
| 160 preamble_->Add(new(Z) StoreLocalNode( |
| 161 Scanner::kNoSourcePos, result_param, await_helper_call)); |
| 162 |
| 209 ReturnNode* continuation_return = new(Z) ReturnNode(Scanner::kNoSourcePos); | 163 ReturnNode* continuation_return = new(Z) ReturnNode(Scanner::kNoSourcePos); |
| 210 continuation_return->set_return_type(ReturnNode::kContinuationTarget); | 164 continuation_return->set_return_type(ReturnNode::kContinuationTarget); |
| 211 preamble_->Add(continuation_return); | 165 preamble_->Add(continuation_return); |
| 212 | 166 |
| 213 // If this expression is part of a try block, also append the code for | 167 // If this expression is part of a try block, also append the code for |
| 214 // restoring the saved try context that lives on the stack and possibly the | 168 // restoring the saved try context that lives on the stack and possibly the |
| 215 // saved try context of the outer try block. | 169 // saved try context of the outer try block. |
| 216 if (node->saved_try_ctx() != NULL) { | 170 if (node->saved_try_ctx() != NULL) { |
| 217 preamble_->Add(new (Z) StoreLocalNode( | 171 preamble_->Add(new (Z) StoreLocalNode( |
| 218 Scanner::kNoSourcePos, | 172 Scanner::kNoSourcePos, |
| (...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 | 570 |
| 617 | 571 |
| 618 void AwaitTransformer::VisitThrowNode(ThrowNode* node) { | 572 void AwaitTransformer::VisitThrowNode(ThrowNode* node) { |
| 619 AstNode* new_exception = Transform(node->exception()); | 573 AstNode* new_exception = Transform(node->exception()); |
| 620 result_ = new(Z) ThrowNode(node->token_pos(), | 574 result_ = new(Z) ThrowNode(node->token_pos(), |
| 621 new_exception, | 575 new_exception, |
| 622 node->stacktrace()); | 576 node->stacktrace()); |
| 623 } | 577 } |
| 624 | 578 |
| 625 } // namespace dart | 579 } // namespace dart |
| OLD | NEW |