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