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
23namespace panda::es2panda::util {
24class SymbolTable {
25public:
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
36        explicit OriginFunctionInfo(ArenaAllocator *allocator) : lexenv(allocator->Adapter()),
37                                                                 classHash(allocator->Adapter()) {}
38    };
39
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(int targetApiVersion, std::string targetApiSubVersion);
48    void FillSymbolTable(const std::stringstream &content);
49    void WriteSymbolTable();
50
51    ArenaUnorderedMap<std::string, OriginFunctionInfo> *GetOriginFunctionInfo()
52    {
53        return &originFunctionInfo_;
54    }
55
56    ArenaUnorderedMap<std::string, std::string> *GetOriginModuleInfo()
57    {
58        return &originModuleInfo_;
59    }
60
61    ArenaUnorderedMap<std::string, std::unordered_map<std::string, std::string>> *GetOriginRecordHashFunctionNames()
62    {
63        return &originRecordHashFunctionNames_;
64    }
65
66    int GetTargetApiVersion()
67    {
68        return targetApiVersion_;
69    }
70
71    std::string GetTargetApiSubVersion() const
72    {
73        return targetApiSubVersion_;
74    }
75
76private:
77    bool ReadSymbolTable(const std::string &symbolTable);
78    std::vector<std::string_view> GetStringItems(std::string_view input, const std::string &separator);
79    void ReadRecordHashFunctionNames(const std::string &recordName, const std::string &funcInternalName,
80                                     const std::string &specialFuncIndex);
81
82    std::mutex m_;
83    std::string symbolTable_;
84    std::string dumpSymbolTable_;
85    int targetApiVersion_ {0};
86    std::string targetApiSubVersion_;
87    ArenaAllocator allocator_;
88    ArenaUnorderedMap<std::string, OriginFunctionInfo> originFunctionInfo_;
89    ArenaUnorderedMap<std::string, std::string> originModuleInfo_;
90    // <recordName, <specialFuncIndex, specialFuncName>>
91    ArenaUnorderedMap<std::string, std::unordered_map<std::string, std::string>> originRecordHashFunctionNames_;
92
93    std::stringstream symbolTableContent_;
94};
95}  // namespace panda::es2panda::util
96
97#endif  // ES2PANDA_UTIL_SYMBOL_TABLE_H