Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_AST_AST_H_ | 5 #ifndef V8_AST_AST_H_ |
| 6 #define V8_AST_AST_H_ | 6 #define V8_AST_AST_H_ |
| 7 | 7 |
| 8 #include "src/ast/ast-value-factory.h" | 8 #include "src/ast/ast-value-factory.h" |
| 9 #include "src/ast/modules.h" | 9 #include "src/ast/modules.h" |
| 10 #include "src/ast/variables.h" | 10 #include "src/ast/variables.h" |
| (...skipping 2868 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2879 | 2879 |
| 2880 | 2880 |
| 2881 // ---------------------------------------------------------------------------- | 2881 // ---------------------------------------------------------------------------- |
| 2882 // Basic visitor | 2882 // Basic visitor |
| 2883 // Sub-class should parametrize AstVisitor with itself, e.g.: | 2883 // Sub-class should parametrize AstVisitor with itself, e.g.: |
| 2884 // class SpecificVisitor : public AstVisitor<SpecificVisitor> { ... } | 2884 // class SpecificVisitor : public AstVisitor<SpecificVisitor> { ... } |
| 2885 | 2885 |
| 2886 template <class Subclass> | 2886 template <class Subclass> |
| 2887 class AstVisitor BASE_EMBEDDED { | 2887 class AstVisitor BASE_EMBEDDED { |
| 2888 public: | 2888 public: |
| 2889 void Visit(AstNode* node) { This()->Visit(node); } | 2889 void Visit(AstNode* node) { impl()->Visit(node); } |
| 2890 | 2890 |
| 2891 void VisitDeclarations(ZoneList<Declaration*>* declarations) { | 2891 void VisitDeclarations(ZoneList<Declaration*>* declarations) { |
| 2892 for (int i = 0; i < declarations->length(); i++) { | 2892 for (int i = 0; i < declarations->length(); i++) { |
| 2893 Visit(declarations->at(i)); | 2893 Visit(declarations->at(i)); |
| 2894 } | 2894 } |
| 2895 } | 2895 } |
| 2896 | 2896 |
| 2897 void VisitStatements(ZoneList<Statement*>* statements) { | 2897 void VisitStatements(ZoneList<Statement*>* statements) { |
| 2898 for (int i = 0; i < statements->length(); i++) { | 2898 for (int i = 0; i < statements->length(); i++) { |
| 2899 Statement* stmt = statements->at(i); | 2899 Statement* stmt = statements->at(i); |
| 2900 Visit(stmt); | 2900 Visit(stmt); |
| 2901 if (stmt->IsJump()) break; | 2901 if (stmt->IsJump()) break; |
| 2902 } | 2902 } |
| 2903 } | 2903 } |
| 2904 | 2904 |
| 2905 void VisitExpressions(ZoneList<Expression*>* expressions) { | 2905 void VisitExpressions(ZoneList<Expression*>* expressions) { |
| 2906 for (int i = 0; i < expressions->length(); i++) { | 2906 for (int i = 0; i < expressions->length(); i++) { |
| 2907 // The variable statement visiting code may pass NULL expressions | 2907 // The variable statement visiting code may pass NULL expressions |
| 2908 // to this code. Maybe this should be handled by introducing an | 2908 // to this code. Maybe this should be handled by introducing an |
| 2909 // undefined expression or literal? Revisit this code if this | 2909 // undefined expression or literal? Revisit this code if this |
| 2910 // changes | 2910 // changes |
| 2911 Expression* expression = expressions->at(i); | 2911 Expression* expression = expressions->at(i); |
| 2912 if (expression != NULL) Visit(expression); | 2912 if (expression != NULL) Visit(expression); |
| 2913 } | 2913 } |
| 2914 } | 2914 } |
| 2915 | 2915 |
| 2916 private: | 2916 protected: |
| 2917 Subclass* This() { return static_cast<Subclass*>(this); } | 2917 Subclass* impl() { return static_cast<Subclass*>(this); } |
| 2918 }; | 2918 }; |
| 2919 | 2919 |
| 2920 #define GENERATE_VISIT_CASE(NodeType) \ | 2920 #define GENERATE_VISIT_CASE(NodeType) \ |
| 2921 case AstNode::k##NodeType: \ | 2921 case AstNode::k##NodeType: \ |
| 2922 return Visit##NodeType(static_cast<NodeType*>(node)); | 2922 return this->impl()->Visit##NodeType(static_cast<NodeType*>(node)); |
|
Toon Verwaest
2016/07/21 11:16:59
This change isn't necessary if you add DEFINE_AST_
nickie
2016/07/22 08:40:18
This was explored in https://codereview.chromium.o
| |
| 2923 | 2923 |
| 2924 #define GENERATE_AST_VISITOR_SWITCH() \ | 2924 #define GENERATE_AST_VISITOR_SWITCH() \ |
| 2925 switch (node->node_type()) { \ | 2925 switch (node->node_type()) { \ |
| 2926 AST_NODE_LIST(GENERATE_VISIT_CASE) \ | 2926 AST_NODE_LIST(GENERATE_VISIT_CASE) \ |
| 2927 case AstNode::kModule: \ | 2927 case AstNode::kModule: \ |
| 2928 UNREACHABLE(); \ | 2928 UNREACHABLE(); \ |
| 2929 } | 2929 } |
| 2930 | 2930 |
| 2931 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \ | 2931 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \ |
| 2932 public: \ | 2932 public: \ |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3025 // `at` and `Set`. | 3025 // `at` and `Set`. |
| 3026 #define AST_REWRITE_LIST_ELEMENT(Type, list, index) \ | 3026 #define AST_REWRITE_LIST_ELEMENT(Type, list, index) \ |
| 3027 do { \ | 3027 do { \ |
| 3028 auto _list = (list); \ | 3028 auto _list = (list); \ |
| 3029 auto _index = (index); \ | 3029 auto _index = (index); \ |
| 3030 AST_REWRITE(Type, _list->at(_index), _list->Set(_index, replacement)); \ | 3030 AST_REWRITE(Type, _list->at(_index), _list->Set(_index, replacement)); \ |
| 3031 } while (false) | 3031 } while (false) |
| 3032 | 3032 |
| 3033 | 3033 |
| 3034 // ---------------------------------------------------------------------------- | 3034 // ---------------------------------------------------------------------------- |
| 3035 // Traversing visitor | |
| 3036 // - fully traverses the entire AST. | |
| 3037 | |
| 3038 // This AstVistor is not final, and provides the AstVisitor methods as virtual | |
| 3039 // methods so they can be specialized by subclasses. | |
| 3040 class AstTraversalVisitor : public AstVisitor<AstTraversalVisitor> { | |
| 3041 public: | |
| 3042 explicit AstTraversalVisitor(Isolate* isolate); | |
| 3043 explicit AstTraversalVisitor(uintptr_t stack_limit); | |
| 3044 virtual ~AstTraversalVisitor() {} | |
| 3045 | |
| 3046 // Iteration left-to-right. | |
| 3047 void VisitDeclarations(ZoneList<Declaration*>* declarations); | |
| 3048 void VisitStatements(ZoneList<Statement*>* statements); | |
| 3049 | |
| 3050 // Individual nodes | |
| 3051 #define DECLARE_VISIT(type) virtual void Visit##type(type* node); | |
| 3052 AST_NODE_LIST(DECLARE_VISIT) | |
| 3053 #undef DECLARE_VISIT | |
| 3054 | |
| 3055 protected: | |
| 3056 int depth() { return depth_; } | |
| 3057 | |
| 3058 private: | |
| 3059 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); | |
| 3060 | |
| 3061 int depth_; | |
| 3062 | |
| 3063 DISALLOW_COPY_AND_ASSIGN(AstTraversalVisitor); | |
| 3064 }; | |
| 3065 | |
| 3066 // ---------------------------------------------------------------------------- | |
| 3067 // AstNode factory | 3035 // AstNode factory |
| 3068 | 3036 |
| 3069 class AstNodeFactory final BASE_EMBEDDED { | 3037 class AstNodeFactory final BASE_EMBEDDED { |
| 3070 public: | 3038 public: |
| 3071 explicit AstNodeFactory(AstValueFactory* ast_value_factory) | 3039 explicit AstNodeFactory(AstValueFactory* ast_value_factory) |
| 3072 : local_zone_(ast_value_factory->zone()), | 3040 : local_zone_(ast_value_factory->zone()), |
| 3073 parser_zone_(ast_value_factory->zone()), | 3041 parser_zone_(ast_value_factory->zone()), |
| 3074 ast_value_factory_(ast_value_factory) {} | 3042 ast_value_factory_(ast_value_factory) {} |
| 3075 | 3043 |
| 3076 AstValueFactory* ast_value_factory() const { return ast_value_factory_; } | 3044 AstValueFactory* ast_value_factory() const { return ast_value_factory_; } |
| (...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3543 : NULL; \ | 3511 : NULL; \ |
| 3544 } | 3512 } |
| 3545 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS) | 3513 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS) |
| 3546 #undef DECLARE_NODE_FUNCTIONS | 3514 #undef DECLARE_NODE_FUNCTIONS |
| 3547 | 3515 |
| 3548 | 3516 |
| 3549 } // namespace internal | 3517 } // namespace internal |
| 3550 } // namespace v8 | 3518 } // namespace v8 |
| 3551 | 3519 |
| 3552 #endif // V8_AST_AST_H_ | 3520 #endif // V8_AST_AST_H_ |
| OLD | NEW |