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 PANDA_ASSEMBLER_ASSEMBLY_PROGRAM_H 17 #define PANDA_ASSEMBLER_ASSEMBLY_PROGRAM_H 18 19 #include <string> 20 #include <unordered_set> 21 22 #include "assembly-function.h" 23 #include "assembly-record.h" 24 #include "assembly-type.h" 25 #include "assembly-methodhandle.h" 26 #include "assembly-literals.h" 27 #include "extensions/extensions.h" 28 #include "macros.h" 29 30 namespace ark::pandasm { 31 32 // NOLINTBEGIN(misc-non-private-member-variables-in-classes) 33 struct Program { 34 using StringT = std::set<std::string>; 35 36 ark::panda_file::SourceLang lang {ark::panda_file::SourceLang::PANDA_ASSEMBLY}; 37 std::map<std::string, ark::pandasm::Record> recordTable; 38 std::map<std::string, ark::pandasm::Function> functionInstanceTable; 39 std::map<std::string, ark::pandasm::Function> functionStaticTable; 40 std::vector<std::pair<std::string, std::string>> exportStrMap; 41 std::unordered_map<std::string, std::vector<std::string>> 42 functionSynonyms; // we might keep unordered, since we don't iterate over it 43 std::map<std::string, ark::pandasm::LiteralArray> literalarrayTable; 44 StringT strings; 45 std::set<Type> arrayTypes; 46 47 /* 48 * Returns a JSON string with the program structure and scopes locations 49 */ 50 PANDA_PUBLIC_API std::string JsonDump() const; 51 52 void AddToFunctionTable(ark::pandasm::Function func, const std::string &name = "") 53 { 54 if (func.IsStatic()) { 55 functionStaticTable.emplace(name.empty() ? func.name : name, std::move(func)); 56 } else { 57 functionInstanceTable.emplace(name.empty() ? func.name : name, std::move(func)); 58 } 59 } 60 61 std::optional<std::map<std::basic_string<char>, ark::pandasm::Function>::const_iterator> FindAmongAllFunctions( 62 const std::string &name, bool isAmbiguous = false, bool isStatic = false) const 63 { 64 const auto &firstSearchTable = isStatic ? functionStaticTable : functionInstanceTable; 65 const auto &secondSearchTable = isStatic ? functionInstanceTable : functionStaticTable; 66 auto it = firstSearchTable.find(name); 67 if (it == firstSearchTable.cend()) { 68 if (isAmbiguous && !isStatic) { 69 return std::nullopt; 70 } 71 it = secondSearchTable.find(name); 72 if (it == secondSearchTable.cend()) { 73 return std::nullopt; 74 } 75 } 76 return it; 77 } 78 }; 79 // NOLINTEND(misc-non-private-member-variables-in-classes) 80 81 } // namespace ark::pandasm 82 83 #endif // PANDA_ASSEMBLER_ASSEMBLY_PROGRAM_H 84