• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_UTIL_SYMBOL_TABLE_H
17 #define ES2PANDA_UTIL_SYMBOL_TABLE_H
18 
19 #include <libpandabase/utils/arena_containers.h>
20 
21 #include <mutex>
22 
23 namespace panda::es2panda::util {
24 class SymbolTable {
25 public:
26     static const std::string FIRST_LEVEL_SEPERATOR;
27     static const std::string SECOND_LEVEL_SEPERATOR;
28 
29     struct OriginFunctionInfo {
30         std::string recordName;
31         std::string funcInternalName;
32         std::string funcHash;
33         ArenaMap<uint32_t, std::pair<std::string, int>> lexenv;  // lexenv: <slot, <name, type>>
34         ArenaMap<std::string, std::string> classHash;
35 
OriginFunctionInfoOriginFunctionInfo36         explicit OriginFunctionInfo(ArenaAllocator *allocator) : lexenv(allocator->Adapter()),
37                                                                  classHash(allocator->Adapter()) {}
38     };
39 
SymbolTable(const std::string & inputSymbolTable,const std::string & dumpSymbolTable)40     SymbolTable(const std::string &inputSymbolTable, const std::string &dumpSymbolTable)
41         : symbolTable_(inputSymbolTable), dumpSymbolTable_(dumpSymbolTable),
42         allocator_(SpaceType::SPACE_TYPE_COMPILER, nullptr, true),
43         originFunctionInfo_(allocator_.Adapter()),
44         originModuleInfo_(allocator_.Adapter()),
45         originRecordHashFunctionNames_(allocator_.Adapter()) {}
46 
47     bool Initialize();
48     void WriteSymbolTable(const std::string &content);
GetOriginFunctionInfo()49     ArenaUnorderedMap<std::string, OriginFunctionInfo> *GetOriginFunctionInfo()
50     {
51         return &originFunctionInfo_;
52     }
53 
GetOriginModuleInfo()54     ArenaUnorderedMap<std::string, std::string> *GetOriginModuleInfo()
55     {
56         return &originModuleInfo_;
57     }
58 
GetOriginRecordHashFunctionNames()59     ArenaUnorderedMap<std::string, std::unordered_map<std::string, std::string>> *GetOriginRecordHashFunctionNames()
60     {
61         return &originRecordHashFunctionNames_;
62     }
63 
64 private:
65     bool ReadSymbolTable(const std::string &symbolTable);
66     std::vector<std::string_view> GetStringItems(std::string_view input, const std::string &separator);
67     void ReadRecordHashFunctionNames(std::string recordName, std::string funcInternalName,
68                                      std::string specialFuncIndex);
69 
70     std::mutex m_;
71     std::string symbolTable_;
72     std::string dumpSymbolTable_;
73     ArenaAllocator allocator_;
74     ArenaUnorderedMap<std::string, OriginFunctionInfo> originFunctionInfo_;
75     ArenaUnorderedMap<std::string, std::string> originModuleInfo_;
76     // <recordName, <specialFuncIndex, specialFuncName>>
77     ArenaUnorderedMap<std::string, std::unordered_map<std::string, std::string>> originRecordHashFunctionNames_;
78 };
79 }  // namespace panda::es2panda::util
80 
81 #endif  // ES2PANDA_UTIL_SYMBOL_TABLE_H