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