1 /** 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ES2PANDA_COMPILER_CORE_ITERATE_AST_VISITOR_H 17 #define ES2PANDA_COMPILER_CORE_ITERATE_AST_VISITOR_H 18 19 #include "AstVisitor.h" 20 #include "ir/expressions/literals/undefinedLiteral.h" 21 #include "ir/expressions/blockExpression.h" 22 #include "ir/ets/etsUnionType.h" 23 #include "ir/ets/etsTuple.h" 24 #include "ir/ets/etsNullishTypes.h" 25 #include "ir/statements/functionDeclaration.h" 26 #include "ir/expressions/functionExpression.h" 27 #include "ir/base/scriptFunction.h" 28 #include "ir/base/methodDefinition.h" 29 #include "ir/base/classProperty.h" 30 #include "ir/expressions/identifier.h" 31 #include "ir/expressions/dummyNode.h" 32 #include "ir/ets/etsReExportDeclaration.h" 33 #include "ir/statements/variableDeclaration.h" 34 #include "ir/statements/variableDeclarator.h" 35 36 namespace ark::es2panda::ir::visitor { 37 38 namespace detail { 39 class DefaultBehaviourAstVisitor : public ASTAbstractVisitor { 40 public: 41 DefaultBehaviourAstVisitor() = default; 42 virtual ~DefaultBehaviourAstVisitor() = 0; 43 NO_COPY_SEMANTIC(DefaultBehaviourAstVisitor); 44 NO_MOVE_SEMANTIC(DefaultBehaviourAstVisitor); 45 46 virtual void HandleNode(ir::AstNode *node) = 0; 47 48 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 49 #define DECLARE_CLASSES(nodeType, className) \ 50 void Visit##className(className *node) override \ 51 { \ 52 HandleNode(static_cast<ir::AstNode *>(node)); \ 53 } 54 55 AST_NODE_MAPPING(DECLARE_CLASSES) 56 57 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 58 #define DECLARE_AST_NODE_CHECK_METHOD(nodeType1, nodeType2, baseClass, reinterpretClass) \ 59 DECLARE_CLASSES(nodeType1, baseClass); 60 61 AST_NODE_REINTERPRET_MAPPING(DECLARE_AST_NODE_CHECK_METHOD) 62 #undef DECLARE_AST_NODE_CHECK_METHOD 63 64 #undef DECLARE_CLASSES 65 }; 66 inline DefaultBehaviourAstVisitor::~DefaultBehaviourAstVisitor() = default; 67 } // namespace detail 68 69 /** 70 * Children should declare VisitNode methods (might be virtual might be not) 71 * for all classes or provide default behaviour using 72 * template <T> VisitNode(T *t) {} 73 */ 74 class IterateAstVisitor : public detail::DefaultBehaviourAstVisitor { 75 public: 76 IterateAstVisitor() = default; 77 Iterate(ir::AstNode * node)78 void Iterate(ir::AstNode *node) 79 { 80 if (node != nullptr) { 81 node->Iterate([this](ir::AstNode *child) { child->Accept(this); }); 82 } 83 } 84 HandleNode(ir::AstNode * node)85 void HandleNode(ir::AstNode *node) final 86 { 87 Iterate(node); 88 } 89 }; 90 91 class EmptyAstVisitor : public detail::DefaultBehaviourAstVisitor { 92 public: 93 EmptyAstVisitor() = default; 94 HandleNode(ir::AstNode *)95 void HandleNode(ir::AstNode * /*node*/) final {} 96 }; 97 98 class AbortAstVisitor : public detail::DefaultBehaviourAstVisitor { 99 public: 100 AbortAstVisitor() = default; 101 HandleNode(ir::AstNode *)102 void HandleNode(ir::AstNode * /*node*/) final 103 { 104 UNREACHABLE(); 105 } 106 }; 107 108 using CustomAstVisitor = detail::DefaultBehaviourAstVisitor; 109 110 } // namespace ark::es2panda::ir::visitor 111 112 #endif 113