Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: runtime/vm/ast_transformer.cc

Issue 536543002: Revert "Fix scoping async functions." (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/ast_transformer.h ('k') | runtime/vm/parser.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/parser.h" 7 #include "vm/parser.h"
8 8
9 namespace dart { 9 namespace dart {
10 10
(...skipping 24 matching lines...) Expand all
35 V(While) 35 V(While)
36 36
37 #define DEFINE_UNREACHABLE(BaseName) \ 37 #define DEFINE_UNREACHABLE(BaseName) \
38 void AwaitTransformer::Visit##BaseName##Node(BaseName##Node* node) { \ 38 void AwaitTransformer::Visit##BaseName##Node(BaseName##Node* node) { \
39 UNREACHABLE(); \ 39 UNREACHABLE(); \
40 } 40 }
41 41
42 FOR_EACH_UNREACHABLE_NODE(DEFINE_UNREACHABLE) 42 FOR_EACH_UNREACHABLE_NODE(DEFINE_UNREACHABLE)
43 #undef DEFINE_UNREACHABLE 43 #undef DEFINE_UNREACHABLE
44 44
45 AwaitTransformer::AwaitTransformer(SequenceNode* preamble,
46 const Library& library,
47 ParsedFunction* const parsed_function,
48 LocalScope* function_top)
49 : preamble_(preamble),
50 temp_cnt_(0),
51 library_(library),
52 parsed_function_(parsed_function),
53 function_top_(function_top),
54 isolate_(Isolate::Current()) {
55 ASSERT(function_top_ != NULL);
56 }
57
58 45
59 AstNode* AwaitTransformer::Transform(AstNode* expr) { 46 AstNode* AwaitTransformer::Transform(AstNode* expr) {
60 expr->Visit(this); 47 expr->Visit(this);
61 return result_; 48 return result_;
62 } 49 }
63 50
64 51
65 LocalVariable* AwaitTransformer::EnsureCurrentTempVar() { 52 LocalVariable* AwaitTransformer::EnsureCurrentTempVar() {
66 const char* await_temp_prefix = ":await_temp_var_"; 53 const char* await_temp_prefix = ":await_temp_var_";
67 const String& cnt_str = String::ZoneHandle( 54 const String& cnt_str = String::ZoneHandle(
68 I, String::NewFormatted("%s%d", await_temp_prefix, temp_cnt_)); 55 I, String::NewFormatted("%s%d", await_temp_prefix, temp_cnt_));
69 const String& symbol = String::ZoneHandle(I, Symbols::New(cnt_str)); 56 const String& symbol = String::ZoneHandle(I, Symbols::New(cnt_str));
70 ASSERT(!symbol.IsNull()); 57 ASSERT(!symbol.IsNull());
71 // Look up the variable through the preamble scope. 58 LocalVariable* await_tmp =
72 LocalVariable* await_tmp = preamble_->scope()->LookupVariable(symbol, false); 59 parsed_function_->await_temps_scope()->LookupVariable(symbol, false);
73 if (await_tmp == NULL) { 60 if (await_tmp == NULL) {
74 // If we need a new temp variable, we add it to the function's top scope. 61 await_tmp = new(I) LocalVariable(
75 await_tmp = new (I) LocalVariable( 62 Scanner::kNoSourcePos,
76 Scanner::kNoSourcePos, symbol, Type::ZoneHandle(Type::DynamicType())); 63 symbol,
77 function_top_->AddVariable(await_tmp); 64 Type::ZoneHandle(I, Type::DynamicType()));
78 // After adding it to the top scope, we can look it up from the preamble. 65 parsed_function_->await_temps_scope()->AddVariable(await_tmp);
79 // The following call includes an ASSERT check.
80 await_tmp = GetVariableInScope(preamble_->scope(), symbol);
81 } 66 }
82 return await_tmp; 67 return await_tmp;
83 } 68 }
84 69
85 70
86 LocalVariable* AwaitTransformer::GetVariableInScope(LocalScope* scope,
87 const String& symbol) {
88 LocalVariable* var = scope->LookupVariable(symbol, false);
89 ASSERT(var != NULL);
90 return var;
91 }
92
93
94 LocalVariable* AwaitTransformer::AddToPreambleNewTempVar(AstNode* node) { 71 LocalVariable* AwaitTransformer::AddToPreambleNewTempVar(AstNode* node) {
95 LocalVariable* tmp_var = EnsureCurrentTempVar(); 72 LocalVariable* tmp_var = EnsureCurrentTempVar();
96 preamble_->Add(new(I) StoreLocalNode(Scanner::kNoSourcePos, tmp_var, node)); 73 preamble_->Add(new(I) StoreLocalNode(Scanner::kNoSourcePos, tmp_var, node));
97 NextTempVar(); 74 NextTempVar();
98 return tmp_var; 75 return tmp_var;
99 } 76 }
100 77
101 78
102 void AwaitTransformer::VisitLiteralNode(LiteralNode* node) { 79 void AwaitTransformer::VisitLiteralNode(LiteralNode* node) {
103 result_ = node; 80 result_ = node;
(...skipping 12 matching lines...) Expand all
116 // :result_param = :await_temp_var_X; 93 // :result_param = :await_temp_var_X;
117 // if (:result_param is Future) { 94 // if (:result_param is Future) {
118 // AwaitMarker(kNewContinuationState); 95 // AwaitMarker(kNewContinuationState);
119 // :result_param.then(:async_op); 96 // :result_param.then(:async_op);
120 // return; // (return_type() == kContinuation) 97 // return; // (return_type() == kContinuation)
121 // } 98 // }
122 // AwaitMarker(kTargetForContinuation); // Join happens here. 99 // AwaitMarker(kTargetForContinuation); // Join happens here.
123 // :saved_try_ctx_var = :await_saved_try_ctx_var_y; 100 // :saved_try_ctx_var = :await_saved_try_ctx_var_y;
124 // :await_temp_var_(X+1) = :result_param; 101 // :await_temp_var_(X+1) = :result_param;
125 102
126 LocalVariable* async_op = GetVariableInScope( 103 LocalVariable* async_op = preamble_->scope()->LookupVariable(
127 preamble_->scope(), Symbols::AsyncOperation()); 104 Symbols::AsyncOperation(), false);
128 LocalVariable* result_param = GetVariableInScope( 105 ASSERT(async_op != NULL);
129 preamble_->scope(), Symbols::AsyncOperationParam()); 106 LocalVariable* result_param = preamble_->scope()->LookupVariable(
107 Symbols::AsyncOperationParam(), false);
108 ASSERT(result_param != NULL);
130 109
131 node->expr()->Visit(this); 110 node->expr()->Visit(this);
132 preamble_->Add(new(I) StoreLocalNode( 111 preamble_->Add(new(I) StoreLocalNode(
133 Scanner::kNoSourcePos, result_param, result_)); 112 Scanner::kNoSourcePos, result_param, result_));
134 LoadLocalNode* load_result_param = new(I) LoadLocalNode( 113 LoadLocalNode* load_result_param = new(I) LoadLocalNode(
135 Scanner::kNoSourcePos, result_param); 114 Scanner::kNoSourcePos, result_param);
136 LocalScope* is_future_scope = ChainNewScope(preamble_->scope()); 115 SequenceNode* is_future_branch = new(I) SequenceNode(
137 SequenceNode* is_future_branch = new (I) SequenceNode( 116 Scanner::kNoSourcePos, preamble_->scope());
138 Scanner::kNoSourcePos, is_future_scope); 117 AwaitMarkerNode* await_marker =
139 AwaitMarkerNode* await_marker = new (I) AwaitMarkerNode( 118 new(I) AwaitMarkerNode(AwaitMarkerNode::kNewContinuationState);
140 AwaitMarkerNode::kNewContinuationState); 119 await_marker->set_scope(preamble_->scope());
141 await_marker->set_scope(is_future_scope);
142 GetVariableInScope(is_future_scope, Symbols::AwaitJumpVar());
143 GetVariableInScope(is_future_scope, Symbols::AwaitContextVar());
144 is_future_branch->Add(await_marker); 120 is_future_branch->Add(await_marker);
145 ArgumentListNode* args = new(I) ArgumentListNode(Scanner::kNoSourcePos); 121 ArgumentListNode* args = new(I) ArgumentListNode(Scanner::kNoSourcePos);
146 args->Add(new(I) LoadLocalNode(Scanner::kNoSourcePos, async_op)); 122 args->Add(new(I) LoadLocalNode(Scanner::kNoSourcePos, async_op));
147 is_future_branch->Add(new(I) InstanceCallNode( 123 is_future_branch->Add(new(I) InstanceCallNode(
148 Scanner::kNoSourcePos, load_result_param, Symbols::FutureThen(), args)); 124 Scanner::kNoSourcePos, load_result_param, Symbols::FutureThen(), args));
149 ReturnNode* continuation_return = new(I) ReturnNode(Scanner::kNoSourcePos); 125 ReturnNode* continuation_return = new(I) ReturnNode(Scanner::kNoSourcePos);
150 continuation_return->set_return_type(ReturnNode::kContinuation); 126 continuation_return->set_return_type(ReturnNode::kContinuation);
151 is_future_branch->Add(continuation_return); 127 is_future_branch->Add(continuation_return);
152 128
153 const Class& cls = Class::ZoneHandle( 129 const Class& cls = Class::ZoneHandle(
154 I, library_.LookupClass(Symbols::Future())); 130 I, library_.LookupClass(Symbols::Future()));
155 const AbstractType& future_type = AbstractType::ZoneHandle(I, cls.RareType()); 131 const AbstractType& future_type = AbstractType::ZoneHandle(I, cls.RareType());
156 ASSERT(!future_type.IsNull()); 132 ASSERT(!future_type.IsNull());
157 preamble_->Add(new(I) IfNode( 133 preamble_->Add(new(I) IfNode(
158 Scanner::kNoSourcePos, 134 Scanner::kNoSourcePos,
159 new (I) ComparisonNode( 135 new (I) ComparisonNode(
160 Scanner::kNoSourcePos, 136 Scanner::kNoSourcePos,
161 Token::kIS, 137 Token::kIS,
162 load_result_param, 138 load_result_param,
163 new (I) TypeNode(Scanner::kNoSourcePos, future_type)), 139 new (I) TypeNode(Scanner::kNoSourcePos, future_type)),
164 is_future_branch, 140 is_future_branch,
165 NULL)); 141 NULL));
166 preamble_->Add(new (I) AwaitMarkerNode( 142 preamble_->Add(new (I) AwaitMarkerNode(
167 AwaitMarkerNode::kTargetForContinuation)); 143 AwaitMarkerNode::kTargetForContinuation));
168
169 // If this expression is part of a try block, also append the code for 144 // If this expression is part of a try block, also append the code for
170 // restoring the saved try context that lives on the stack. 145 // restoring the saved try context that lives on the stack.
171 const String& async_saved_try_ctx_name = 146 if (parsed_function_->saved_try_ctx() != NULL) {
172 String::Handle(I, parsed_function_->async_saved_try_ctx_name());
173 if (!async_saved_try_ctx_name.IsNull()) {
174 LocalVariable* async_saved_try_ctx =
175 GetVariableInScope(preamble_->scope(), async_saved_try_ctx_name);
176 preamble_->Add(new (I) StoreLocalNode( 147 preamble_->Add(new (I) StoreLocalNode(
177 Scanner::kNoSourcePos, 148 Scanner::kNoSourcePos,
178 parsed_function_->saved_try_ctx(), 149 parsed_function_->saved_try_ctx(),
179 new (I) LoadLocalNode(Scanner::kNoSourcePos, async_saved_try_ctx))); 150 new (I) LoadLocalNode(
151 Scanner::kNoSourcePos, parsed_function_->async_saved_try_ctx())));
180 } 152 }
181 153
182 LocalVariable* result = AddToPreambleNewTempVar(new(I) LoadLocalNode( 154 LocalVariable* result = AddToPreambleNewTempVar(new(I) LoadLocalNode(
183 Scanner::kNoSourcePos, result_param)); 155 Scanner::kNoSourcePos, result_param));
184 result_ = new(I) LoadLocalNode(Scanner::kNoSourcePos, result); 156 result_ = new(I) LoadLocalNode(Scanner::kNoSourcePos, result);
185 } 157 }
186 158
187 159
188 // Transforms boolean expressions into a sequence of evaluatons that only lazily 160 // Transforms boolean expressions into a sequence of evaluatons that only lazily
189 // evaluate subexpressions. 161 // evaluate subexpressions.
(...skipping 10 matching lines...) Expand all
200 // } 172 // }
201 // t_3 = t_1 || t_2; // Compiler takes care that lazy evaluation takes place 173 // t_3 = t_1 || t_2; // Compiler takes care that lazy evaluation takes place
202 // on this level. 174 // on this level.
203 AstNode* AwaitTransformer::LazyTransform(const Token::Kind logical_op, 175 AstNode* AwaitTransformer::LazyTransform(const Token::Kind logical_op,
204 AstNode* new_left, 176 AstNode* new_left,
205 AstNode* right) { 177 AstNode* right) {
206 ASSERT(logical_op == Token::kAND || logical_op == Token::kOR); 178 ASSERT(logical_op == Token::kAND || logical_op == Token::kOR);
207 AstNode* result = NULL; 179 AstNode* result = NULL;
208 const Token::Kind compare_logical_op = (logical_op == Token::kAND) ? 180 const Token::Kind compare_logical_op = (logical_op == Token::kAND) ?
209 Token::kEQ : Token::kNE; 181 Token::kEQ : Token::kNE;
210 SequenceNode* eval = new (I) SequenceNode( 182 SequenceNode* eval = new(I) SequenceNode(
211 Scanner::kNoSourcePos, ChainNewScope(preamble_->scope())); 183 Scanner::kNoSourcePos, preamble_->scope());
212 SequenceNode* saved_preamble = preamble_; 184 SequenceNode* saved_preamble = preamble_;
213 preamble_ = eval; 185 preamble_ = eval;
214 result = Transform(right); 186 result = Transform(right);
215 preamble_ = saved_preamble; 187 preamble_ = saved_preamble;
216 IfNode* right_body = new(I) IfNode( 188 IfNode* right_body = new(I) IfNode(
217 Scanner::kNoSourcePos, 189 Scanner::kNoSourcePos,
218 new(I) ComparisonNode( 190 new(I) ComparisonNode(
219 Scanner::kNoSourcePos, 191 Scanner::kNoSourcePos,
220 compare_logical_op, 192 compare_logical_op,
221 new_left, 193 new_left,
222 new(I) LiteralNode(Scanner::kNoSourcePos, Bool::True())), 194 new(I) LiteralNode(Scanner::kNoSourcePos, Bool::True())),
223 eval, 195 eval,
224 NULL); 196 NULL);
225 preamble_->Add(right_body); 197 preamble_->Add(right_body);
226 return result; 198 return result;
227 } 199 }
228 200
229 201
230 LocalScope* AwaitTransformer::ChainNewScope(LocalScope* parent) {
231 return new (I) LocalScope(
232 parent, parent->function_level(), parent->loop_level());
233 }
234
235
236 void AwaitTransformer::VisitBinaryOpNode(BinaryOpNode* node) { 202 void AwaitTransformer::VisitBinaryOpNode(BinaryOpNode* node) {
237 node->left()->Visit(this); 203 node->left()->Visit(this);
238 AstNode* new_left = result_; 204 AstNode* new_left = result_;
239 AstNode* new_right = NULL; 205 AstNode* new_right = NULL;
240 // Preserve lazy evaluaton. 206 // Preserve lazy evaluaton.
241 if ((node->kind() == Token::kAND) || (node->kind() == Token::kOR)) { 207 if ((node->kind() == Token::kAND) || (node->kind() == Token::kOR)) {
242 new_right = LazyTransform(node->kind(), new_left, node->right()); 208 new_right = LazyTransform(node->kind(), new_left, node->right());
243 } else { 209 } else {
244 new_right = Transform(node->right()); 210 new_right = Transform(node->right());
245 } 211 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 LocalVariable* result = AddToPreambleNewTempVar( 256 LocalVariable* result = AddToPreambleNewTempVar(
291 new(I) UnaryOpNode(node->token_pos(), node->kind(), new_operand)); 257 new(I) UnaryOpNode(node->token_pos(), node->kind(), new_operand));
292 result_ = new(I) LoadLocalNode(Scanner::kNoSourcePos, result); 258 result_ = new(I) LoadLocalNode(Scanner::kNoSourcePos, result);
293 } 259 }
294 260
295 261
296 // ::= (<condition>) ? <true-branch> : <false-branch> 262 // ::= (<condition>) ? <true-branch> : <false-branch>
297 // 263 //
298 void AwaitTransformer::VisitConditionalExprNode(ConditionalExprNode* node) { 264 void AwaitTransformer::VisitConditionalExprNode(ConditionalExprNode* node) {
299 AstNode* new_condition = Transform(node->condition()); 265 AstNode* new_condition = Transform(node->condition());
300 SequenceNode* new_true = new (I) SequenceNode( 266 SequenceNode* new_true = new(I) SequenceNode(
301 Scanner::kNoSourcePos, ChainNewScope(preamble_->scope())); 267 Scanner::kNoSourcePos, preamble_->scope());
302 SequenceNode* saved_preamble = preamble_; 268 SequenceNode* saved_preamble = preamble_;
303 preamble_ = new_true; 269 preamble_ = new_true;
304 AstNode* new_true_result = Transform(node->true_expr()); 270 AstNode* new_true_result = Transform(node->true_expr());
305 SequenceNode* new_false = new (I) SequenceNode( 271 SequenceNode* new_false = new(I) SequenceNode(
306 Scanner::kNoSourcePos, ChainNewScope(preamble_->scope())); 272 Scanner::kNoSourcePos, preamble_->scope());
307 preamble_ = new_false; 273 preamble_ = new_false;
308 AstNode* new_false_result = Transform(node->false_expr()); 274 AstNode* new_false_result = Transform(node->false_expr());
309 preamble_ = saved_preamble; 275 preamble_ = saved_preamble;
310 IfNode* new_if = new(I) IfNode(Scanner::kNoSourcePos, 276 IfNode* new_if = new(I) IfNode(Scanner::kNoSourcePos,
311 new_condition, 277 new_condition,
312 new_true, 278 new_true,
313 new_false); 279 new_false);
314 preamble_->Add(new_if); 280 preamble_->Add(new_if);
315 LocalVariable* result = AddToPreambleNewTempVar( 281 LocalVariable* result = AddToPreambleNewTempVar(
316 new(I) ConditionalExprNode(Scanner::kNoSourcePos, 282 new(I) ConditionalExprNode(Scanner::kNoSourcePos,
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
539 void AwaitTransformer::VisitThrowNode(ThrowNode* node) { 505 void AwaitTransformer::VisitThrowNode(ThrowNode* node) {
540 // TODO(mlippautz): Check if relevant. 506 // TODO(mlippautz): Check if relevant.
541 AstNode* new_exception = Transform(node->exception()); 507 AstNode* new_exception = Transform(node->exception());
542 AstNode* new_stacktrace = Transform(node->stacktrace()); 508 AstNode* new_stacktrace = Transform(node->stacktrace());
543 result_ = new(I) ThrowNode(node->token_pos(), 509 result_ = new(I) ThrowNode(node->token_pos(),
544 new_exception, 510 new_exception,
545 new_stacktrace); 511 new_stacktrace);
546 } 512 }
547 513
548 } // namespace dart 514 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/ast_transformer.h ('k') | runtime/vm/parser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698