| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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_H_ | 5 #ifndef VM_AST_H_ |
| 6 #define VM_AST_H_ | 6 #define VM_AST_H_ |
| 7 | 7 |
| 8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
| 9 #include "vm/allocation.h" | 9 #include "vm/allocation.h" |
| 10 #include "vm/growable_array.h" | 10 #include "vm/growable_array.h" |
| 11 #include "vm/scopes.h" | 11 #include "vm/scopes.h" |
| 12 #include "vm/object.h" | 12 #include "vm/object.h" |
| 13 #include "vm/native_entry.h" | 13 #include "vm/native_entry.h" |
| 14 #include "vm/token.h" | 14 #include "vm/token.h" |
| 15 | 15 |
| 16 namespace dart { | 16 namespace dart { |
| 17 | 17 |
| 18 #define NODE_LIST(V) \ | 18 #define FOR_EACH_NODE(V) \ |
| 19 V(ReturnNode, "return") \ | 19 V(Return) \ |
| 20 V(LiteralNode, "literal") \ | 20 V(Literal) \ |
| 21 V(TypeNode, "type") \ | 21 V(Type) \ |
| 22 V(AssignableNode, "assignable") \ | 22 V(Assignable) \ |
| 23 V(BinaryOpNode, "binop") \ | 23 V(BinaryOp) \ |
| 24 V(BinaryOpWithMask32Node, "binop with mask 32") \ | 24 V(BinaryOpWithMask32) \ |
| 25 V(ComparisonNode, "compare") \ | 25 V(Comparison) \ |
| 26 V(UnaryOpNode, "unaryop") \ | 26 V(UnaryOp) \ |
| 27 V(ConditionalExprNode, "?:") \ | 27 V(ConditionalExpr) \ |
| 28 V(IfNode, "if") \ | 28 V(If) \ |
| 29 V(SwitchNode, "switch") \ | 29 V(Switch) \ |
| 30 V(CaseNode, "case") \ | 30 V(Case) \ |
| 31 V(WhileNode, "while") \ | 31 V(While) \ |
| 32 V(DoWhileNode, "dowhile") \ | 32 V(DoWhile) \ |
| 33 V(ForNode, "for") \ | 33 V(For) \ |
| 34 V(JumpNode, "jump") \ | 34 V(Jump) \ |
| 35 V(ArgumentListNode, "args") \ | 35 V(ArgumentList) \ |
| 36 V(ArrayNode, "array") \ | 36 V(Array) \ |
| 37 V(ClosureNode, "closure") \ | 37 V(Closure) \ |
| 38 V(InstanceCallNode, "instance call") \ | 38 V(InstanceCall) \ |
| 39 V(StaticCallNode, "static call") \ | 39 V(StaticCall) \ |
| 40 V(ClosureCallNode, "closure call") \ | 40 V(ClosureCall) \ |
| 41 V(CloneContextNode, "clone context") \ | 41 V(CloneContext) \ |
| 42 V(ConstructorCallNode, "constructor call") \ | 42 V(ConstructorCall) \ |
| 43 V(InstanceGetterNode, "instance getter call") \ | 43 V(InstanceGetter) \ |
| 44 V(InstanceSetterNode, "instance setter call") \ | 44 V(InstanceSetter) \ |
| 45 V(StaticGetterNode, "static getter") \ | 45 V(StaticGetter) \ |
| 46 V(StaticSetterNode, "static setter") \ | 46 V(StaticSetter) \ |
| 47 V(NativeBodyNode, "native body") \ | 47 V(NativeBody) \ |
| 48 V(PrimaryNode, "primary") \ | 48 V(Primary) \ |
| 49 V(LoadLocalNode, "load local") \ | 49 V(LoadLocal) \ |
| 50 V(StoreLocalNode, "store local") \ | 50 V(StoreLocal) \ |
| 51 V(LoadInstanceFieldNode, "load field") \ | 51 V(LoadInstanceField) \ |
| 52 V(StoreInstanceFieldNode, "store field") \ | 52 V(StoreInstanceField) \ |
| 53 V(LoadStaticFieldNode, "load static field") \ | 53 V(LoadStaticField) \ |
| 54 V(StoreStaticFieldNode, "store static field") \ | 54 V(StoreStaticField) \ |
| 55 V(LoadIndexedNode, "load indexed") \ | 55 V(LoadIndexed) \ |
| 56 V(StoreIndexedNode, "store indexed") \ | 56 V(StoreIndexed) \ |
| 57 V(SequenceNode, "seq") \ | 57 V(Sequence) \ |
| 58 V(LetNode, "let") \ | 58 V(Let) \ |
| 59 V(CatchClauseNode, "catch clause block") \ | 59 V(CatchClause) \ |
| 60 V(TryCatchNode, "try catch block") \ | 60 V(TryCatch) \ |
| 61 V(ThrowNode, "throw") \ | 61 V(Throw) \ |
| 62 V(InlinedFinallyNode, "inlined finally") \ | 62 V(InlinedFinally) \ |
| 63 | 63 |
| 64 | 64 |
| 65 #define DEFINE_FORWARD_DECLARATION(type, name) class type; | 65 #define FORWARD_DECLARATION(BaseName) class BaseName##Node; |
| 66 NODE_LIST(DEFINE_FORWARD_DECLARATION) | 66 FOR_EACH_NODE(FORWARD_DECLARATION) |
| 67 #undef DEFINE_FORWARD_DECLARATION | 67 #undef FORWARD_DECLARATION |
| 68 | 68 |
| 69 | 69 |
| 70 // Abstract class to implement an AST node visitor. An example is AstPrinter. | 70 // Abstract class to implement an AST node visitor. An example is AstPrinter. |
| 71 class AstNodeVisitor : public ValueObject { | 71 class AstNodeVisitor : public ValueObject { |
| 72 public: | 72 public: |
| 73 AstNodeVisitor() {} | 73 AstNodeVisitor() {} |
| 74 virtual ~AstNodeVisitor() {} | 74 virtual ~AstNodeVisitor() {} |
| 75 | 75 |
| 76 #define DEFINE_VISITOR_FUNCTION(type, name) \ | 76 #define DEFINE_VISITOR_FUNCTION(BaseName) \ |
| 77 virtual void Visit##type(type* node) { } | 77 virtual void Visit##BaseName##Node(BaseName##Node* node) { } |
| 78 NODE_LIST(DEFINE_VISITOR_FUNCTION) | 78 |
| 79 FOR_EACH_NODE(DEFINE_VISITOR_FUNCTION) |
| 79 #undef DEFINE_VISITOR_FUNCTION | 80 #undef DEFINE_VISITOR_FUNCTION |
| 80 | 81 |
| 81 private: | 82 private: |
| 82 DISALLOW_COPY_AND_ASSIGN(AstNodeVisitor); | 83 DISALLOW_COPY_AND_ASSIGN(AstNodeVisitor); |
| 83 }; | 84 }; |
| 84 | 85 |
| 85 | 86 |
| 86 #define DECLARE_COMMON_NODE_FUNCTIONS(type) \ | 87 #define DECLARE_COMMON_NODE_FUNCTIONS(type) \ |
| 87 virtual void Visit(AstNodeVisitor* visitor); \ | 88 virtual void Visit(AstNodeVisitor* visitor); \ |
| 88 virtual const char* PrettyName() const; \ | 89 virtual const char* PrettyName() const; \ |
| 89 virtual bool Is##type() const { return true; } \ | 90 virtual bool Is##type() const { return true; } \ |
| 90 virtual type* As##type() { return this; } | 91 virtual type* As##type() { return this; } |
| 91 | 92 |
| 92 | 93 |
| 93 class AstNode : public ZoneAllocated { | 94 class AstNode : public ZoneAllocated { |
| 94 public: | 95 public: |
| 95 explicit AstNode(intptr_t token_pos) | 96 explicit AstNode(intptr_t token_pos) |
| 96 : token_pos_(token_pos) { | 97 : token_pos_(token_pos) { |
| 97 ASSERT(token_pos_ >= 0); | 98 ASSERT(token_pos_ >= 0); |
| 98 } | 99 } |
| 99 | 100 |
| 100 intptr_t token_pos() const { return token_pos_; } | 101 intptr_t token_pos() const { return token_pos_; } |
| 101 | 102 |
| 102 #define AST_TYPE_CHECK(type, name) \ | 103 #define AST_TYPE_CHECK(BaseName) \ |
| 103 virtual bool Is##type() const { return false; } \ | 104 virtual bool Is##BaseName##Node() const { return false; } \ |
| 104 virtual type* As##type() { return NULL; } | 105 virtual BaseName##Node* As##BaseName##Node() { return NULL; } |
| 105 NODE_LIST(AST_TYPE_CHECK) | 106 |
| 107 FOR_EACH_NODE(AST_TYPE_CHECK) |
| 106 #undef AST_TYPE_CHECK | 108 #undef AST_TYPE_CHECK |
| 107 | 109 |
| 108 virtual void Visit(AstNodeVisitor* visitor) = 0; | 110 virtual void Visit(AstNodeVisitor* visitor) = 0; |
| 109 virtual void VisitChildren(AstNodeVisitor* visitor) const = 0; | 111 virtual void VisitChildren(AstNodeVisitor* visitor) const = 0; |
| 110 virtual const char* PrettyName() const = 0; | 112 virtual const char* PrettyName() const = 0; |
| 111 | 113 |
| 112 // Convert the node into an assignment node using the rhs which is passed in, | 114 // Convert the node into an assignment node using the rhs which is passed in, |
| 113 // this is typically used for converting nodes like LoadLocalNode, | 115 // this is typically used for converting nodes like LoadLocalNode, |
| 114 // LoadStaticFieldNode, InstanceGetterNode etc. which were created during | 116 // LoadStaticFieldNode, InstanceGetterNode etc. which were created during |
| 115 // parsing as the assignment context was not known yet at that time. | 117 // parsing as the assignment context was not known yet at that time. |
| (...skipping 770 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 886 | 888 |
| 887 private: | 889 private: |
| 888 SourceLabel* label_; | 890 SourceLabel* label_; |
| 889 AstNode* condition_; | 891 AstNode* condition_; |
| 890 SequenceNode* body_; | 892 SequenceNode* body_; |
| 891 | 893 |
| 892 DISALLOW_IMPLICIT_CONSTRUCTORS(DoWhileNode); | 894 DISALLOW_IMPLICIT_CONSTRUCTORS(DoWhileNode); |
| 893 }; | 895 }; |
| 894 | 896 |
| 895 | 897 |
| 896 // initializer, condition, increment expressions can be NULL. | 898 // The condition can be NULL. |
| 897 class ForNode : public AstNode { | 899 class ForNode : public AstNode { |
| 898 public: | 900 public: |
| 899 ForNode(intptr_t token_pos, | 901 ForNode(intptr_t token_pos, |
| 900 SourceLabel* label, | 902 SourceLabel* label, |
| 901 SequenceNode* initializer, | 903 SequenceNode* initializer, |
| 902 AstNode* condition, | 904 AstNode* condition, |
| 903 SequenceNode* increment, | 905 SequenceNode* increment, |
| 904 SequenceNode* body) | 906 SequenceNode* body) |
| 905 : AstNode(token_pos), | 907 : AstNode(token_pos), |
| 906 label_(label), | 908 label_(label), |
| (...skipping 841 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1748 const intptr_t try_index_; | 1750 const intptr_t try_index_; |
| 1749 | 1751 |
| 1750 DISALLOW_IMPLICIT_CONSTRUCTORS(InlinedFinallyNode); | 1752 DISALLOW_IMPLICIT_CONSTRUCTORS(InlinedFinallyNode); |
| 1751 }; | 1753 }; |
| 1752 | 1754 |
| 1753 } // namespace dart | 1755 } // namespace dart |
| 1754 | 1756 |
| 1755 #undef DECLARE_COMMON_NODE_FUNCTIONS | 1757 #undef DECLARE_COMMON_NODE_FUNCTIONS |
| 1756 | 1758 |
| 1757 #endif // VM_AST_H_ | 1759 #endif // VM_AST_H_ |
| OLD | NEW |