1 /* 2 * Copyright (c) 2023 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 ECMASCRIPT_COMPILER_DEBUG_INFO_H 17 #define ECMASCRIPT_COMPILER_DEBUG_INFO_H 18 19 #include "ecmascript/mem/chunk_containers.h" 20 21 namespace panda::ecmascript::kungfu { 22 class DebugInfo { 23 public: 24 DebugInfo(NativeAreaAllocator* allocator, bool enable = false); 25 ~DebugInfo(); IsEnable()26 bool IsEnable() const 27 { 28 return enable_; 29 } 30 size_t AddComment(const char* str); 31 void AddFuncDebugInfo(const std::string &name); 32 const std::string &GetComment(const std::string &funcName, size_t index) const; 33 34 private: 35 class FuncDebugInfo { 36 public: FuncDebugInfo(Chunk * chunk)37 FuncDebugInfo(Chunk *chunk) : chunk_(chunk), name_("") 38 { 39 comments_ = new ChunkVector<std::string>(chunk_); 40 } 41 ~FuncDebugInfo()42 ~FuncDebugInfo() 43 { 44 if (comments_ != nullptr) { 45 delete comments_; 46 comments_ = nullptr; 47 } 48 } 49 Name()50 const std::string &Name() const 51 { 52 return name_; 53 } 54 SetName(const std::string & n)55 void SetName(const std::string &n) 56 { 57 name_ = n; 58 } 59 Add(const std::string & str)60 size_t Add(const std::string &str) 61 { 62 comments_->push_back(str); 63 return comments_->size() - 1; 64 } 65 GetComment(size_t index)66 const std::string &GetComment(size_t index) 67 { 68 ASSERT(comments_ != nullptr); 69 if (index < comments_->size()) { 70 return comments_->at(index); 71 } 72 return EmptyComment(); 73 } 74 EmptyComment()75 static const std::string &EmptyComment() 76 { 77 return EMPTY_COMMENT; 78 } 79 80 private: 81 static std::string EMPTY_COMMENT; 82 Chunk* chunk_ {nullptr}; 83 std::string name_; 84 ChunkVector<std::string> *comments_ {nullptr}; 85 }; 86 87 void AddFuncName(const std::string &name); 88 89 Chunk chunk_; 90 ChunkMap<std::string, size_t> funcToDInfo_; 91 ChunkVector<FuncDebugInfo*> dInfos_; 92 bool enable_ {false}; 93 }; 94 } // namespace panda::ecmascript::kungfu 95 #endif // ECMASCRIPT_COMPILER_DEBUG_INFO_H 96 97