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/etsStringLiteralType.h" 24 #include "ir/ets/etsTuple.h" 25 #include "ir/ets/etsNullishTypes.h" 26 #include "ir/statements/functionDeclaration.h" 27 #include "ir/expressions/functionExpression.h" 28 #include "ir/base/scriptFunction.h" 29 #include "ir/base/methodDefinition.h" 30 #include "ir/base/classProperty.h" 31 #include "ir/expressions/identifier.h" 32 #include "ir/expressions/dummyNode.h" 33 #include "ir/ets/etsReExportDeclaration.h" 34 #include "ir/statements/variableDeclaration.h" 35 #include "ir/statements/variableDeclarator.h" 36 #include "ir/statements/namespaceDeclaration.h" 37 38 namespace ark::es2panda::ir::visitor { 39 40 namespace detail { 41 class DefaultBehaviourAstVisitor : public ASTAbstractVisitor { 42 public: 43 DefaultBehaviourAstVisitor() = default; 44 virtual ~DefaultBehaviourAstVisitor() = 0; 45 NO_COPY_SEMANTIC(DefaultBehaviourAstVisitor); 46 NO_MOVE_SEMANTIC(DefaultBehaviourAstVisitor); 47 48 virtual void HandleNode(ir::AstNode *node) = 0; 49 50 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 51 #define DECLARE_CLASSES(nodeType, className) \ 52 void Visit##className(className *node) override \ 53 { \ 54 HandleNode(static_cast<ir::AstNode *>(node)); \ 55 } 56 57 AST_NODE_MAPPING(DECLARE_CLASSES) 58 59 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 60 #define DECLARE_AST_NODE_CHECK_METHOD(nodeType1, nodeType2, baseClass, reinterpretClass) \ 61 DECLARE_CLASSES(nodeType1, baseClass); 62 63 AST_NODE_REINTERPRET_MAPPING(DECLARE_AST_NODE_CHECK_METHOD) 64 #undef DECLARE_AST_NODE_CHECK_METHOD 65 66 #undef DECLARE_CLASSES 67 }; 68 inline DefaultBehaviourAstVisitor::~DefaultBehaviourAstVisitor() = default; 69 } // namespace detail 70 71 /** 72 * Children should declare VisitNode methods (might be virtual might be not) 73 * for all classes or provide default behaviour using 74 * template <T> VisitNode(T *t) {} 75 */ 76 class IterateAstVisitor : public detail::DefaultBehaviourAstVisitor { 77 public: 78 IterateAstVisitor() = default; 79 Iterate(ir::AstNode *node)80 void Iterate(ir::AstNode *node) 81 { 82 if (node != nullptr) { 83 node->Iterate([this](ir::AstNode *child) { child->Accept(this); }); 84 } 85 } 86 87 void HandleNode(ir::AstNode *node) final 88 { 89 Iterate(node); 90 } 91 }; 92 93 class EmptyAstVisitor : public detail::DefaultBehaviourAstVisitor { 94 public: 95 EmptyAstVisitor() = default; 96 97 void HandleNode(ir::AstNode * /*node*/) final {} 98 }; 99 100 class AbortAstVisitor : public detail::DefaultBehaviourAstVisitor { 101 public: 102 AbortAstVisitor() = default; 103 104 void HandleNode(ir::AstNode * /*node*/) final 105 { 106 UNREACHABLE(); 107 } 108 }; 109 110 using CustomAstVisitor = detail::DefaultBehaviourAstVisitor; 111 112 } // namespace ark::es2panda::ir::visitor 113 114 #endif 115