1 /* 2 * Copyright (c) 2021-2025 Huawei Device Co., Ltd. All rights reserved. 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_IR_SRCDUMP_H 17 #define ES2PANDA_IR_SRCDUMP_H 18 19 #include <ir/astNode.h> 20 #include <lexer/token/sourceLocation.h> 21 #include "generated/tokenType.h" 22 #include <util/ustring.h> 23 24 #include <sstream> 25 #include <variant> 26 #include <future> 27 28 namespace ark::es2panda::ir { 29 30 // Forward declarations 31 class ClassDefinition; 32 class TSTypeAliasDeclaration; 33 class ClassProperty; 34 class TSInterfaceDeclaration; 35 class TSEnumDeclaration; 36 37 using NodeVariant = 38 std::variant<std::monostate, const ark::es2panda::ir::ClassDefinition *, 39 const ark::es2panda::ir::TSTypeAliasDeclaration *, const ark::es2panda::ir::ClassProperty *, 40 const ark::es2panda::ir::TSInterfaceDeclaration *, const ark::es2panda::ir::TSEnumDeclaration *>; 41 42 class SrcDumper { 43 public: 44 explicit SrcDumper(const ir::AstNode *node); 45 explicit SrcDumper(const ir::AstNode *node, bool isDeclgen, bool isIsolatedDeclgen = false); 46 47 void Add(const std::string &str); 48 void Add(int32_t i); 49 void Add(int64_t l); 50 void Add(float f); 51 void Add(double d); 52 Str()53 std::string Str() const 54 { 55 return ss_.str(); 56 } 57 58 void IncrIndent(); 59 void DecrIndent(); 60 void Endl(size_t num = 1); 61 62 bool IsDeclgen() const; IsIsolatedDeclgen()63 bool IsIsolatedDeclgen() const 64 { 65 return isIsolatedDeclgen_; 66 } 67 void DumpVariant(NodeVariant &node); 68 void DumpNode(const std::string &key); 69 70 template <typename T> 71 void DumpNodeIfPointer(T *value); 72 73 template <typename T> AddNode(const std::string & key,T * value)74 void AddNode(const std::string &key, T *value) 75 { 76 unExportNode_[key] = NodeVariant(value); 77 } 78 RemoveNode(const std::string & key)79 void RemoveNode(const std::string &key) 80 { 81 unExportNode_.erase(key); 82 } 83 IsIndirectDepPhase()84 bool IsIndirectDepPhase() const 85 { 86 return isIndirectDepPhase_; 87 } 88 89 template <typename T> PushTask(T && task)90 void PushTask(T &&task) 91 { 92 taskQueue_.emplace(std::forward<T>(task)); 93 } 94 95 void Run(); 96 97 private: 98 std::stringstream ss_; 99 std::string indent_; 100 bool isDeclgen_ = false; 101 bool isIsolatedDeclgen_ = false; 102 bool isIndirectDepPhase_ = false; 103 std::unordered_map<std::string, NodeVariant> unExportNode_; 104 105 std::queue<std::function<void()>> taskQueue_; 106 }; 107 } // namespace ark::es2panda::ir 108 109 #endif // ES2PANDA_IR_SRCDUMP_H 110