• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 "program.h"
17 #include "macros.h"
18 #include "public/public.h"
19 
20 #include "compiler/core/CFG.h"
21 #include "generated/signatures.h"
22 #include "varbinder/varbinder.h"
23 #include "varbinder/ETSBinder.h"
24 #include "ir/astDump.h"
25 #include "ir/base/classDefinition.h"
26 #include "ir/statements/blockStatement.h"
27 
28 namespace ark::es2panda::parser {
29 
Program(ArenaAllocator * allocator,varbinder::VarBinder * varbinder)30 Program::Program(ArenaAllocator *allocator, varbinder::VarBinder *varbinder)
31     : allocator_(allocator),
32       varbinder_(varbinder),
33       externalSources_(allocator_->Adapter()),
34       directExternalSources_(allocator_->Adapter()),
35       extension_(varbinder != nullptr ? varbinder->Extension() : ScriptExtension::INVALID),
36       etsnolintCollection_(allocator_->Adapter()),
37       cfg_(allocator_->New<compiler::CFG>(allocator_)),
38       functionScopes_(allocator_->Adapter())
39 {
40 }
41 
IsGenAbcForExternal() const42 bool Program::IsGenAbcForExternal() const
43 {
44     return VarBinder()->GetContext()->config->options->GetCompilationMode() ==
45                CompilationMode::GEN_ABC_FOR_EXTERNAL_SOURCE &&
46            genAbcForExternalSource_;
47 }
48 
Dump() const49 std::string Program::Dump() const
50 {
51     ir::AstDumper dumper {ast_, SourceCode()};
52     return dumper.Str();
53 }
54 
DumpSilent() const55 void Program::DumpSilent() const
56 {
57     [[maybe_unused]] ir::AstDumper dumper {ast_, SourceCode()};
58     ES2PANDA_ASSERT(!dumper.Str().empty());
59 }
60 
GlobalClassScope()61 varbinder::ClassScope *Program::GlobalClassScope()
62 {
63     ES2PANDA_ASSERT(globalClass_ != nullptr);
64     ES2PANDA_ASSERT(globalClass_->Scope() != nullptr);
65     return globalClass_->Scope()->AsClassScope();
66 }
67 
GlobalClassScope() const68 const varbinder::ClassScope *Program::GlobalClassScope() const
69 {
70     ES2PANDA_ASSERT(globalClass_ != nullptr);
71     ES2PANDA_ASSERT(globalClass_->Scope() != nullptr);
72     return globalClass_->Scope()->AsClassScope();
73 }
74 
GlobalScope()75 varbinder::GlobalScope *Program::GlobalScope()
76 {
77     ES2PANDA_ASSERT(ast_->Scope()->IsGlobalScope() || ast_->Scope()->IsModuleScope());
78     return static_cast<varbinder::GlobalScope *>(ast_->Scope());
79 }
80 
GlobalScope() const81 const varbinder::GlobalScope *Program::GlobalScope() const
82 {
83     ES2PANDA_ASSERT(ast_->Scope()->IsGlobalScope() || ast_->Scope()->IsModuleScope());
84     return static_cast<const varbinder::GlobalScope *>(ast_->Scope());
85 }
86 
SetPackageInfo(const util::StringView & name,util::ModuleKind kind)87 void Program::SetPackageInfo(const util::StringView &name, util::ModuleKind kind)
88 {
89     moduleInfo_.moduleName = name;
90     moduleInfo_.modulePrefix =
91         name.Empty()
92             ? ""
93             : util::UString(std::string(name).append(compiler::Signatures::METHOD_SEPARATOR), allocator_).View();
94 
95     moduleInfo_.kind = kind;
96 }
97 
98 // NOTE(vpukhov): part of ongoing design
MaybeTransformToDeclarationModule()99 void Program::MaybeTransformToDeclarationModule()
100 {
101     ES2PANDA_ASSERT(ast_ != nullptr);
102     if (IsPackage() || ast_->Statements().empty()) {
103         return;
104     }
105     for (auto stmt : ast_->Statements()) {
106         if (!(stmt->IsDeclare() || stmt->IsTSTypeAliasDeclaration())) {
107             return;
108         }
109     }
110     moduleInfo_.kind = util::ModuleKind::DECLARATION;
111 }
112 
AddNodeToETSNolintCollection(const ir::AstNode * node,const std::set<ETSWarnings> & warningsCollection)113 void Program::AddNodeToETSNolintCollection(const ir::AstNode *node, const std::set<ETSWarnings> &warningsCollection)
114 {
115     ArenaSet<ETSWarnings> tmp(allocator_->Adapter());
116     tmp.insert(warningsCollection.begin(), warningsCollection.end());
117     etsnolintCollection_.insert({node, tmp});
118 }
119 
NodeContainsETSNolint(const ir::AstNode * node,ETSWarnings warning)120 bool Program::NodeContainsETSNolint(const ir::AstNode *node, ETSWarnings warning)
121 {
122     auto nodeEtsnolints = etsnolintCollection_.find(node);
123     if (nodeEtsnolints == etsnolintCollection_.end()) {
124         return false;
125     }
126 
127     return nodeEtsnolints->second.find(warning) != nodeEtsnolints->second.end();
128 }
129 
SetFlag(ProgramFlags flag)130 void Program::SetFlag(ProgramFlags flag)
131 {
132     ES2PANDA_ASSERT(VarBinder() != nullptr && VarBinder()->GetContext() != nullptr);
133     auto compilingState = VarBinder()->GetContext()->compilingState;
134     if (compilingState == public_lib::CompilingState::MULTI_COMPILING_INIT ||
135         compilingState == public_lib::CompilingState::MULTI_COMPILING_FOLLOW) {
136         programFlags_ |= flag;
137     }
138 }
139 
GetFlag(ProgramFlags flag) const140 bool Program::GetFlag(ProgramFlags flag) const
141 {
142     return (programFlags_ & flag) != 0U;
143 }
144 
ClearASTCheckedStatus()145 void Program::ClearASTCheckedStatus()
146 {
147     programFlags_ &= ~ProgramFlags::AST_CHECKED;
148 }
149 
SetASTChecked()150 void Program::SetASTChecked()
151 {
152     programFlags_ |= ProgramFlags::AST_CHECKED;
153 }
154 
IsASTChecked()155 bool Program::IsASTChecked()
156 {
157     return ((programFlags_ & ProgramFlags::AST_CHECKED) != 0U) ||
158            ((programFlags_ & ProgramFlags::AST_CHECK_PROCESSED) != 0U);
159 }
160 
~Program()161 Program::~Program()  // NOLINT(modernize-use-equals-default)
162 {
163 #ifndef NDEBUG
164     poisonValue_ = 0;
165 #endif
166 }
167 
GetCFG()168 compiler::CFG *Program::GetCFG()
169 {
170     return cfg_;
171 }
172 
GetCFG() const173 const compiler::CFG *Program::GetCFG() const
174 {
175     return cfg_;
176 }
177 
178 }  // namespace ark::es2panda::parser
179