1 /*
2  * Copyright (c) 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 ABC2PROGRAM_ABC2PROGRAM_ENTITY_CONTAINER_H
17 #define ABC2PROGRAM_ABC2PROGRAM_ENTITY_CONTAINER_H
18 
19 #include <string>
20 #include <unordered_map>
21 #include <unordered_set>
22 #include <assembly-program.h>
23 #include <debug_info_extractor.h>
24 #include "file.h"
25 
26 namespace panda::abc2program {
27 
28 const panda::panda_file::SourceLang LANG_ECMA = panda::panda_file::SourceLang::ECMASCRIPT;
29 
30 class Abc2ProgramEntityContainer {
31 public:
Abc2ProgramEntityContainer(const panda_file::File &file, pandasm::Program &program, const panda_file::DebugInfoExtractor &debug_info_extractor, uint32_t class_id)32     Abc2ProgramEntityContainer(const panda_file::File &file,
33                                pandasm::Program &program,
34                                const panda_file::DebugInfoExtractor &debug_info_extractor,
35                                uint32_t class_id)
36         : file_(file), program_(program), debug_info_extractor_(debug_info_extractor),
37           current_class_id_(class_id) {}
38     const panda_file::File &GetAbcFile() const;
39     pandasm::Program &GetProgram() const;
40 
41     std::string GetStringById(const panda_file::File::EntityId &entity_id) const;
42     std::string GetFullRecordNameById(const panda_file::File::EntityId &class_id);
43     std::string GetFullMethodNameById(const panda_file::File::EntityId &method_id);
44     void AddProgramString(const std::string &str) const;
45 
46     const std::unordered_set<uint32_t> &GetMouleLiteralArrayIdSet() const;
47     const std::unordered_set<uint32_t> &GetModuleRequestPhaseIdSet() const;
48     const std::unordered_set<uint32_t> &GetUnnestedLiteralArrayIdSet() const;
49     std::unordered_set<uint32_t> &GetUnprocessedNestedLiteralArrayIdSet();
50     void AddModuleLiteralArrayId(uint32_t module_literal_array_id);
51     void AddUnnestedLiteralArrayId(uint32_t literal_array_id);
52     void AddModuleRequestPhaseId(uint32_t module_request_phase_id);
53     void AddProcessedNestedLiteralArrayId(uint32_t nested_literal_array_id);
54     void TryAddUnprocessedNestedLiteralArrayId(uint32_t nested_literal_array_id);
55     std::string GetLiteralArrayIdName(uint32_t literal_array_id);
56     const panda_file::DebugInfoExtractor &GetDebugInfoExtractor() const;
57 
58 private:
59     std::string ConcatFullMethodNameById(const panda_file::File::EntityId &method_id);
60     const panda_file::File &file_;
61     pandasm::Program &program_;
62     const panda_file::DebugInfoExtractor &debug_info_extractor_;
63     std::unordered_map<uint32_t, std::string> record_full_name_map_;
64     std::unordered_map<uint32_t, std::string> method_full_name_map_;
65     std::unordered_set<uint32_t> module_literal_array_id_set_;
66     std::unordered_set<uint32_t> module_request_phase_id_set_;
67     std::unordered_set<uint32_t> unnested_literal_array_id_set_;
68     std::unordered_set<uint32_t> processed_nested_literal_array_id_set_;
69     std::unordered_set<uint32_t> unprocessed_nested_literal_array_id_set_;
70     uint32_t current_class_id_{0};
71 };  // class Abc2ProgramEntityContainer
72 
73 }  // namespace panda::abc2program
74 
75 #endif  // ABC2PROGRAM_ABC2PROGRAM_ENTITY_CONTAINER_H