| 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 #ifndef VM_AST_TRANSFORMER_H_ | 5 #ifndef VM_AST_TRANSFORMER_H_ |
| 6 #define VM_AST_TRANSFORMER_H_ | 6 #define VM_AST_TRANSFORMER_H_ |
| 7 | 7 |
| 8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
| 9 #include "vm/ast.h" | 9 #include "vm/ast.h" |
| 10 | 10 |
| 11 namespace dart { | 11 namespace dart { |
| 12 | 12 |
| 13 class ParsedFunction; | |
| 14 class Thread; | 13 class Thread; |
| 15 | 14 |
| 16 // Translate an AstNode containing an expression (that itself contains one or | 15 // Translate an AstNode containing an expression (that itself contains one or |
| 17 // more awaits) into a sequential representation where subexpressions are | 16 // more awaits) into a sequential representation where subexpressions are |
| 18 // evaluated sequentially into intermediates. Those intermediates are stored | 17 // evaluated sequentially into intermediates. Those intermediates are stored |
| 19 // within a context. | 18 // within a context. |
| 20 // | 19 // |
| 21 // This allows a function to be suspended and resumed from within evaluating an | 20 // This allows a function to be suspended and resumed from within evaluating an |
| 22 // expression. The evaluation is split among a so-called preamble and the | 21 // expression. The evaluation is split among a so-called preamble and the |
| 23 // evaluation of the resulting expression (which is only a single load). | 22 // evaluation of the resulting expression (which is only a single load). |
| 24 // | 23 // |
| 25 // Example (minimalistic): | 24 // Example (minimalistic): |
| 26 // | 25 // |
| 27 // var a = (await bar()) + foo(); | 26 // var a = (await bar()) + foo(); |
| 28 // | 27 // |
| 29 // This translates to a premable similar to: | 28 // This translates to a premable similar to: |
| 30 // | 29 // |
| 31 // var t_1, t_2, t_3, t_4; // All stored in a context. | 30 // var t_1, t_2, t_3, t_4; // All stored in a context. |
| 32 // t_1 = bar(); | 31 // t_1 = bar(); |
| 33 // :result_param = t_1; | 32 // :result_param = t_1; |
| 34 // <continuation logic> | 33 // <continuation logic> |
| 35 // t_2 = :result_param; | 34 // t_2 = :result_param; |
| 36 // t_3 = foo(); | 35 // t_3 = foo(); |
| 37 // t_4 = t_2.operator+(t_3); | 36 // t_4 = t_2.operator+(t_3); |
| 38 // | 37 // |
| 39 // and a resulting expression of a load of t_4. | 38 // and a resulting expression of a load of t_4. |
| 40 // | 39 // |
| 41 class AwaitTransformer : public AstNodeVisitor { | 40 class AwaitTransformer : public AstNodeVisitor { |
| 42 public: | 41 public: |
| 43 AwaitTransformer(SequenceNode* preamble, | 42 AwaitTransformer(SequenceNode* preamble, LocalScope* function_top); |
| 44 const ParsedFunction& parsed_function, | |
| 45 LocalScope* function_top); | |
| 46 | 43 |
| 47 #define DECLARE_VISIT(BaseName) \ | 44 #define DECLARE_VISIT(BaseName) \ |
| 48 virtual void Visit##BaseName##Node(BaseName##Node* node); | 45 virtual void Visit##BaseName##Node(BaseName##Node* node); |
| 49 | 46 |
| 50 FOR_EACH_NODE(DECLARE_VISIT) | 47 FOR_EACH_NODE(DECLARE_VISIT) |
| 51 #undef DECLARE_VISIT | 48 #undef DECLARE_VISIT |
| 52 | 49 |
| 53 AstNode* Transform(AstNode* expr); | 50 AstNode* Transform(AstNode* expr); |
| 54 | 51 |
| 52 static AstNode* RestoreSavedTryContext(Zone* zone, |
| 53 LocalScope* scope, |
| 54 int16_t try_index); |
| 55 |
| 55 private: | 56 private: |
| 56 LocalVariable* EnsureCurrentTempVar(); | 57 LocalVariable* EnsureCurrentTempVar(); |
| 57 LocalVariable* AddToPreambleNewTempVar(AstNode* node); | 58 LocalVariable* AddToPreambleNewTempVar(AstNode* node); |
| 58 ArgumentListNode* TransformArguments(ArgumentListNode* node); | 59 ArgumentListNode* TransformArguments(ArgumentListNode* node); |
| 59 AstNode* LazyTransform(const Token::Kind kind, | 60 AstNode* LazyTransform(const Token::Kind kind, |
| 60 AstNode* new_left, | 61 AstNode* new_left, |
| 61 AstNode* right); | 62 AstNode* right); |
| 62 LocalScope* ChainNewScope(LocalScope* parent); | 63 LocalScope* ChainNewScope(LocalScope* parent); |
| 63 LocalVariable* GetVariableInScope(LocalScope* scope, const String& symbol); | 64 LocalVariable* GetVariableInScope(LocalScope* scope, const String& symbol); |
| 64 | 65 |
| 65 void NextTempVar() { temp_cnt_++; } | 66 void NextTempVar() { temp_cnt_++; } |
| 66 | 67 |
| 67 Thread* thread() const { return thread_; } | 68 Thread* thread() const { return thread_; } |
| 68 | 69 |
| 69 SequenceNode* preamble_; | 70 SequenceNode* preamble_; |
| 70 int32_t temp_cnt_; | 71 int32_t temp_cnt_; |
| 71 AstNode* result_; | 72 AstNode* result_; |
| 72 const ParsedFunction& parsed_function_; | |
| 73 LocalScope* function_top_; | 73 LocalScope* function_top_; |
| 74 | 74 |
| 75 Thread* thread_; | 75 Thread* thread_; |
| 76 | 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(AwaitTransformer); | 77 DISALLOW_COPY_AND_ASSIGN(AwaitTransformer); |
| 78 }; | 78 }; |
| 79 | 79 |
| 80 } // namespace dart | 80 } // namespace dart |
| 81 | 81 |
| 82 #endif // VM_AST_TRANSFORMER_H_ | 82 #endif // VM_AST_TRANSFORMER_H_ |
| OLD | NEW |