1 /*
2 * Copyright (c) 2024-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 #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/expressions/literals/stringLiteral.h"
24 #include "util/importPathManager.h"
25
26 namespace ark::es2panda::evaluate {
27
EntityDeclarator(ScopedDebugInfoPlugin & debugInfoPlugin)28 EntityDeclarator::EntityDeclarator(ScopedDebugInfoPlugin &debugInfoPlugin)
29 : debugInfoPlugin_(debugInfoPlugin),
30 createdEntities_(debugInfoPlugin_.GetIrCheckHelper()->GetChecker()->Allocator()->Adapter())
31 {
32 }
33
GetOrCreateEntitiesMap(parser::Program * program)34 UMapStringViewVariable &EntityDeclarator::GetOrCreateEntitiesMap(parser::Program *program)
35 {
36 ES2PANDA_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
CreateAndInsertImportStatement(util::StringView pathToDeclSource,util::StringView declName,parser::Program * importerProgram,util::StringView importedName,varbinder::Variable * var)46 void EntityDeclarator::CreateAndInsertImportStatement(util::StringView pathToDeclSource, util::StringView declName,
47 parser::Program *importerProgram, util::StringView importedName,
48 varbinder::Variable *var)
49 {
50 ES2PANDA_ASSERT(importerProgram);
51 ES2PANDA_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 ES2PANDA_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
CreateIrImport(util::StringView pathToDeclSourceFile,util::StringView classDeclName,util::StringView classImportedName)70 ir::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 moduleName = debugInfoPlugin_.GetDebugInfoStorage()->GetModuleName(pathToDeclSourceFile.Utf8());
78 auto *source = checker->AllocNode<ir::StringLiteral>(moduleName);
79 auto importLanguage = ToLanguage(debugInfoPlugin_.GetETSBinder()->Extension());
80 util::ImportPathManager::ImportMetadata importMetadata {
81 util::ImportFlags::NONE, importLanguage.GetId(), pathToDeclSourceFile.Utf8(),
82 util::ImportPathManager::DUMMY_PATH, util::ImportPathManager::DUMMY_PATH};
83
84 auto *local = checker->AllocNode<ir::Identifier>(classDeclName, allocator);
85 auto *imported = checker->AllocNode<ir::Identifier>(classImportedName, allocator);
86 auto *spec = checker->AllocNode<ir::ImportSpecifier>(imported, local);
87 ArenaVector<ir::AstNode *> specifiers(1, spec, allocator->Adapter());
88
89 return checker->AllocNode<ir::ETSImportDeclaration>(source, importMetadata, std::move(specifiers));
90 }
91
InsertImportStatement(ir::Statement * importStatement,parser::Program * importerProgram)92 void EntityDeclarator::InsertImportStatement(ir::Statement *importStatement, parser::Program *importerProgram)
93 {
94 ES2PANDA_ASSERT(importerProgram);
95 ES2PANDA_ASSERT(importStatement);
96
97 auto *topStatement = importerProgram->Ast();
98 importStatement->SetParent(topStatement);
99 // Can't insert right away until block's statements iteration ends.
100 debugInfoPlugin_.RegisterPrologueEpilogue<true>(topStatement, importStatement);
101
102 debugInfoPlugin_.GetIrCheckHelper()->CheckGlobalEntity(importerProgram, importStatement);
103 }
104
IsEntityDeclared(parser::Program * program,util::StringView name)105 bool EntityDeclarator::IsEntityDeclared(parser::Program *program, util::StringView name)
106 {
107 ES2PANDA_ASSERT(program);
108
109 auto &entitiesMap = GetOrCreateEntitiesMap(program);
110 return entitiesMap.find(name) != entitiesMap.end();
111 }
112
113 /* static */
FindEntityVariable(UMapStringViewVariable & entitiesMap,util::StringView entityName)114 varbinder::Variable *EntityDeclarator::FindEntityVariable(UMapStringViewVariable &entitiesMap,
115 util::StringView entityName)
116 {
117 const auto &find = entitiesMap.find(entityName);
118 if (find != entitiesMap.end()) {
119 return find->second;
120 }
121 return nullptr;
122 }
123
124 } // namespace ark::es2panda::evaluate
125