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

Side by Side Diff: src/ast/ast.h

Issue 2169833002: [parser] Refactor AstTraversalVisitor (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebase Created 4 years, 4 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
« no previous file with comments | « BUILD.gn ('k') | src/ast/ast.cc » ('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 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 2875 matching lines...) Expand 10 before | Expand all | Expand 10 after
2886 2886
2887 2887
2888 // ---------------------------------------------------------------------------- 2888 // ----------------------------------------------------------------------------
2889 // Basic visitor 2889 // Basic visitor
2890 // Sub-class should parametrize AstVisitor with itself, e.g.: 2890 // Sub-class should parametrize AstVisitor with itself, e.g.:
2891 // class SpecificVisitor : public AstVisitor<SpecificVisitor> { ... } 2891 // class SpecificVisitor : public AstVisitor<SpecificVisitor> { ... }
2892 2892
2893 template <class Subclass> 2893 template <class Subclass>
2894 class AstVisitor BASE_EMBEDDED { 2894 class AstVisitor BASE_EMBEDDED {
2895 public: 2895 public:
2896 void Visit(AstNode* node) { This()->Visit(node); } 2896 void Visit(AstNode* node) { impl()->Visit(node); }
2897 2897
2898 void VisitDeclarations(ZoneList<Declaration*>* declarations) { 2898 void VisitDeclarations(ZoneList<Declaration*>* declarations) {
2899 for (int i = 0; i < declarations->length(); i++) { 2899 for (int i = 0; i < declarations->length(); i++) {
2900 Visit(declarations->at(i)); 2900 Visit(declarations->at(i));
2901 } 2901 }
2902 } 2902 }
2903 2903
2904 void VisitStatements(ZoneList<Statement*>* statements) { 2904 void VisitStatements(ZoneList<Statement*>* statements) {
2905 for (int i = 0; i < statements->length(); i++) { 2905 for (int i = 0; i < statements->length(); i++) {
2906 Statement* stmt = statements->at(i); 2906 Statement* stmt = statements->at(i);
2907 Visit(stmt); 2907 Visit(stmt);
2908 if (stmt->IsJump()) break; 2908 if (stmt->IsJump()) break;
2909 } 2909 }
2910 } 2910 }
2911 2911
2912 void VisitExpressions(ZoneList<Expression*>* expressions) { 2912 void VisitExpressions(ZoneList<Expression*>* expressions) {
2913 for (int i = 0; i < expressions->length(); i++) { 2913 for (int i = 0; i < expressions->length(); i++) {
2914 // The variable statement visiting code may pass NULL expressions 2914 // The variable statement visiting code may pass NULL expressions
2915 // to this code. Maybe this should be handled by introducing an 2915 // to this code. Maybe this should be handled by introducing an
2916 // undefined expression or literal? Revisit this code if this 2916 // undefined expression or literal? Revisit this code if this
2917 // changes 2917 // changes
2918 Expression* expression = expressions->at(i); 2918 Expression* expression = expressions->at(i);
2919 if (expression != NULL) Visit(expression); 2919 if (expression != NULL) Visit(expression);
2920 } 2920 }
2921 } 2921 }
2922 2922
2923 private: 2923 protected:
2924 Subclass* This() { return static_cast<Subclass*>(this); } 2924 Subclass* impl() { return static_cast<Subclass*>(this); }
2925 }; 2925 };
2926 2926
2927 #define GENERATE_VISIT_CASE(NodeType) \ 2927 #define GENERATE_VISIT_CASE(NodeType) \
2928 case AstNode::k##NodeType: \ 2928 case AstNode::k##NodeType: \
2929 return Visit##NodeType(static_cast<NodeType*>(node)); 2929 return this->impl()->Visit##NodeType(static_cast<NodeType*>(node));
2930 2930
2931 #define GENERATE_AST_VISITOR_SWITCH() \ 2931 #define GENERATE_AST_VISITOR_SWITCH() \
2932 switch (node->node_type()) { \ 2932 switch (node->node_type()) { \
2933 AST_NODE_LIST(GENERATE_VISIT_CASE) \ 2933 AST_NODE_LIST(GENERATE_VISIT_CASE) \
2934 case AstNode::kModule: \ 2934 case AstNode::kModule: \
2935 UNREACHABLE(); \ 2935 UNREACHABLE(); \
2936 } 2936 }
2937 2937
2938 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \ 2938 #define DEFINE_AST_VISITOR_SUBCLASS_MEMBERS() \
2939 public: \ 2939 public: \
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
3032 // `at` and `Set`. 3032 // `at` and `Set`.
3033 #define AST_REWRITE_LIST_ELEMENT(Type, list, index) \ 3033 #define AST_REWRITE_LIST_ELEMENT(Type, list, index) \
3034 do { \ 3034 do { \
3035 auto _list = (list); \ 3035 auto _list = (list); \
3036 auto _index = (index); \ 3036 auto _index = (index); \
3037 AST_REWRITE(Type, _list->at(_index), _list->Set(_index, replacement)); \ 3037 AST_REWRITE(Type, _list->at(_index), _list->Set(_index, replacement)); \
3038 } while (false) 3038 } while (false)
3039 3039
3040 3040
3041 // ---------------------------------------------------------------------------- 3041 // ----------------------------------------------------------------------------
3042 // Traversing visitor
3043 // - fully traverses the entire AST.
3044
3045 // This AstVistor is not final, and provides the AstVisitor methods as virtual
3046 // methods so they can be specialized by subclasses.
3047 class AstTraversalVisitor : public AstVisitor<AstTraversalVisitor> {
3048 public:
3049 explicit AstTraversalVisitor(Isolate* isolate);
3050 explicit AstTraversalVisitor(uintptr_t stack_limit);
3051 virtual ~AstTraversalVisitor() {}
3052
3053 // Iteration left-to-right.
3054 void VisitDeclarations(ZoneList<Declaration*>* declarations);
3055 void VisitStatements(ZoneList<Statement*>* statements);
3056
3057 // Individual nodes
3058 #define DECLARE_VISIT(type) virtual void Visit##type(type* node);
3059 AST_NODE_LIST(DECLARE_VISIT)
3060 #undef DECLARE_VISIT
3061
3062 protected:
3063 int depth() { return depth_; }
3064
3065 private:
3066 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
3067
3068 int depth_;
3069
3070 DISALLOW_COPY_AND_ASSIGN(AstTraversalVisitor);
3071 };
3072
3073 // ----------------------------------------------------------------------------
3074 // AstNode factory 3042 // AstNode factory
3075 3043
3076 class AstNodeFactory final BASE_EMBEDDED { 3044 class AstNodeFactory final BASE_EMBEDDED {
3077 public: 3045 public:
3078 explicit AstNodeFactory(AstValueFactory* ast_value_factory) 3046 explicit AstNodeFactory(AstValueFactory* ast_value_factory)
3079 : local_zone_(nullptr), 3047 : local_zone_(nullptr),
3080 parser_zone_(nullptr), 3048 parser_zone_(nullptr),
3081 ast_value_factory_(ast_value_factory) { 3049 ast_value_factory_(ast_value_factory) {
3082 if (ast_value_factory != nullptr) { 3050 if (ast_value_factory != nullptr) {
3083 local_zone_ = ast_value_factory->zone(); 3051 local_zone_ = ast_value_factory->zone();
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after
3572 : NULL; \ 3540 : NULL; \
3573 } 3541 }
3574 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS) 3542 AST_NODE_LIST(DECLARE_NODE_FUNCTIONS)
3575 #undef DECLARE_NODE_FUNCTIONS 3543 #undef DECLARE_NODE_FUNCTIONS
3576 3544
3577 3545
3578 } // namespace internal 3546 } // namespace internal
3579 } // namespace v8 3547 } // namespace v8
3580 3548
3581 #endif // V8_AST_AST_H_ 3549 #endif // V8_AST_AST_H_
OLDNEW
« no previous file with comments | « BUILD.gn ('k') | src/ast/ast.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698