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 ES2PANDA_AOT_DEPEND_RELATION_H
17 #define ES2PANDA_AOT_DEPEND_RELATION_H
18 #include <map>
19 #include <string>
20 #include <queue>
21 #include <unordered_set>
22 #include <vector>
23 
24 #include <aot/options.h>
25 #include <es2panda.h>
26 #include <util/workerQueue.h>
27 
28 namespace panda::es2panda::aot {
29 
30 class DepsRelationResolver {
31 public:
DepsRelationResolver(const std::map<std::string, panda::es2panda::util::ProgramCache *> &progsInfo, const std::unique_ptr<panda::es2panda::aot::Options> &options, std::map<std::string, std::unordered_set<std::string>> &resolvedDepsRelation)32     explicit DepsRelationResolver(const std::map<std::string, panda::es2panda::util::ProgramCache *> &progsInfo,
33                                   const std::unique_ptr<panda::es2panda::aot::Options> &options,
34                                   std::map<std::string, std::unordered_set<std::string>> &resolvedDepsRelation)
35         : progsInfo_(progsInfo), resolvedDepsRelation_(resolvedDepsRelation),
36         compileContextInfo_(options->CompilerOptions().compileContextInfo),
37         dumpDepsInfo_(options->CompilerOptions().dumpDepsInfo)
38     {
39     }
40 
41     ~DepsRelationResolver() = default;
42     void CollectStaticImportDepsRelation(const panda::pandasm::Program &program, const std::string &recordName);
43     void CollectDynamicImportDepsRelation(const panda::pandasm::Program &program, const std::string &recordName);
44     bool Resolve();
45 
46 private:
47     void FillRecord2ProgramMap(std::unordered_map<std::string, std::string> &record2ProgramMap);
48     bool CollectCommonjsRecords(const std::vector<panda::pandasm::Field> &fieldList,
49                                 const std::string &progKey, const std::string &recordName);
50     void CollectDepsIfNeeded(const std::string &ohmurl);
51     void DumpDepsRelations();
52 
53     const std::map<std::string, panda::es2panda::util::ProgramCache *> &progsInfo_;
54     std::map<std::string, std::unordered_set<std::string>> &resolvedDepsRelation_;
55     CompileContextInfo &compileContextInfo_;
56     std::queue<std::string> depsToBeResolved_ {};
57     std::unordered_set<std::string> resolvedRecords_ {};
58     bool dumpDepsInfo_ {false};
59 };
60 } // namespace panda::es2panda::aot
61 
62 #endif
63 
64