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#include "evaluate/scopedDebugInfoPlugin-inl.h"
17#include "evaluate/entityDeclarator.h"
18#include "evaluate/irCheckHelper.h"
19
20#include "parser/program/program.h"
21#include "ir/expressions/identifier.h"
22#include "ir/ets/etsImportDeclaration.h"
23#include "ir/ets/etsImportSource.h"
24#include "ir/expressions/literals/stringLiteral.h"
25
26namespace ark::es2panda::evaluate {
27
28EntityDeclarator::EntityDeclarator(ScopedDebugInfoPlugin &debugInfoPlugin)
29    : debugInfoPlugin_(debugInfoPlugin),
30      createdEntities_(debugInfoPlugin_.GetIrCheckHelper()->GetChecker()->Allocator()->Adapter())
31{
32}
33
34UMapStringViewVariable &EntityDeclarator::GetOrCreateEntitiesMap(parser::Program *program)
35{
36    ASSERT(program);
37
38    auto iter = createdEntities_.find(program);
39    if (iter == createdEntities_.end()) {
40        auto adapter = debugInfoPlugin_.GetIrCheckHelper()->GetChecker()->Allocator()->Adapter();
41        return createdEntities_.emplace(program, UMapStringViewVariable(adapter)).first->second;
42    }
43    return iter->second;
44}
45
46void EntityDeclarator::CreateAndInsertImportStatement(util::StringView pathToDeclSource, util::StringView declName,
47                                                      parser::Program *importerProgram, util::StringView importedName,
48                                                      varbinder::Variable *var)
49{
50    ASSERT(importerProgram);
51    ASSERT(var);
52
53    auto &importEntitiesMap = GetOrCreateEntitiesMap(importerProgram);
54    auto *findVar = FindEntityVariable(importEntitiesMap, importedName);
55    if (findVar != nullptr) {
56        // If var was found it means that import declaration has already been declared
57        // and there is no need to create it once again.
58        ASSERT(findVar == var);
59        return;
60    }
61
62    auto *importStatement = CreateIrImport(pathToDeclSource, declName, importedName);
63    InsertImportStatement(importStatement, importerProgram);
64
65    if (!importEntitiesMap.emplace(importedName, var).second) {
66        LOG(FATAL, ES2PANDA) << "Failed to emplace " << importedName << " in entity map.";
67    }
68}
69
70ir::ETSImportDeclaration *EntityDeclarator::CreateIrImport(util::StringView pathToDeclSourceFile,
71                                                           util::StringView classDeclName,
72                                                           util::StringView classImportedName)
73{
74    auto *checker = debugInfoPlugin_.GetIrCheckHelper()->GetChecker();
75    auto *allocator = checker->Allocator();
76
77    auto *resolvedSource = checker->AllocNode<ir::StringLiteral>(pathToDeclSourceFile);
78    auto moduleName = debugInfoPlugin_.GetDebugInfoStorage()->GetModuleName(pathToDeclSourceFile.Utf8());
79    auto *source = checker->AllocNode<ir::StringLiteral>(moduleName);
80    auto importLanguage = ToLanguage(debugInfoPlugin_.GetETSBinder()->Extension());
81    auto *importSource = allocator->New<ir::ImportSource>(source, resolvedSource, importLanguage, true);
82
83    auto *local = checker->AllocNode<ir::Identifier>(classDeclName, allocator);
84    auto *imported = checker->AllocNode<ir::Identifier>(classImportedName, allocator);
85    auto *spec = checker->AllocNode<ir::ImportSpecifier>(imported, local);
86    ArenaVector<ir::AstNode *> specifiers(1, spec, allocator->Adapter());
87
88    return checker->AllocNode<ir::ETSImportDeclaration>(importSource, specifiers);
89}
90
91void EntityDeclarator::InsertImportStatement(ir::Statement *importStatement, parser::Program *importerProgram)
92{
93    ASSERT(importerProgram);
94    ASSERT(importStatement);
95
96    auto *topStatement = importerProgram->Ast();
97    importStatement->SetParent(topStatement);
98    // Can't insert right away until block's statements iteration ends.
99    debugInfoPlugin_.RegisterPrologueEpilogue<true>(topStatement, importStatement);
100
101    debugInfoPlugin_.GetIrCheckHelper()->CheckGlobalEntity(importerProgram, importStatement);
102}
103
104bool EntityDeclarator::IsEntityDeclared(parser::Program *program, util::StringView name)
105{
106    ASSERT(program);
107
108    auto &entitiesMap = GetOrCreateEntitiesMap(program);
109    return entitiesMap.find(name) != entitiesMap.end();
110}
111
112/* static */
113varbinder::Variable *EntityDeclarator::FindEntityVariable(UMapStringViewVariable &entitiesMap,
114                                                          util::StringView entityName)
115{
116    const auto &find = entitiesMap.find(entityName);
117    if (find != entitiesMap.end()) {
118        return find->second;
119    }
120    return nullptr;
121}
122
123}  // namespace ark::es2panda::evaluate
124