1 /** 2 * Copyright (c) 2021-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_PARSER_INCLUDE_PROGRAM_H 17 #define ES2PANDA_PARSER_INCLUDE_PROGRAM_H 18 19 #include "macros.h" 20 #include "mem/pool_manager.h" 21 #include "os/filesystem.h" 22 #include "util/ustring.h" 23 #include "util/path.h" 24 #include "varbinder/varbinder.h" 25 26 #include "es2panda.h" 27 28 #include <set> 29 30 namespace ark::es2panda::ir { 31 class BlockStatement; 32 } // namespace ark::es2panda::ir 33 34 namespace ark::es2panda::varbinder { 35 class VarBinder; 36 } // namespace ark::es2panda::varbinder 37 38 namespace ark::es2panda::parser { 39 enum class ScriptKind { SCRIPT, MODULE, STDLIB }; 40 41 class Program { 42 public: 43 using ExternalSource = ArenaUnorderedMap<util::StringView, ArenaVector<Program *>>; 44 using ETSNolintsCollectionMap = ArenaUnorderedMap<const ir::AstNode *, ArenaSet<ETSWarnings>>; 45 46 template <typename T> NewProgram(ArenaAllocator * allocator)47 static Program NewProgram(ArenaAllocator *allocator) 48 { 49 auto *varbinder = allocator->New<T>(allocator); 50 return Program(allocator, varbinder); 51 } 52 Program(ArenaAllocator * allocator,varbinder::VarBinder * varbinder)53 Program(ArenaAllocator *allocator, varbinder::VarBinder *varbinder) 54 : allocator_(allocator), 55 varbinder_(varbinder), 56 externalSources_(allocator_->Adapter()), 57 extension_(varbinder->Extension()), 58 etsnolintCollection_(allocator_->Adapter()) 59 { 60 } 61 SetKind(ScriptKind kind)62 void SetKind(ScriptKind kind) 63 { 64 kind_ = kind; 65 } 66 67 NO_COPY_SEMANTIC(Program); 68 DEFAULT_MOVE_SEMANTIC(Program); 69 70 ~Program() = default; 71 Allocator()72 ArenaAllocator *Allocator() const 73 { 74 return allocator_; 75 } 76 VarBinder()77 const varbinder::VarBinder *VarBinder() const 78 { 79 return varbinder_; 80 } 81 VarBinder()82 varbinder::VarBinder *VarBinder() 83 { 84 return varbinder_; 85 } 86 Extension()87 ScriptExtension Extension() const 88 { 89 return extension_; 90 } 91 Kind()92 ScriptKind Kind() const 93 { 94 return kind_; 95 } 96 SourceCode()97 util::StringView SourceCode() const 98 { 99 return sourceCode_; 100 } 101 SourceFilePath()102 const util::StringView &SourceFilePath() const 103 { 104 return sourceFile_.GetPath(); 105 } 106 SourceFile()107 const util::Path &SourceFile() const 108 { 109 return sourceFile_; 110 } 111 SourceFileFolder()112 util::StringView SourceFileFolder() const 113 { 114 return sourceFileFolder_; 115 } 116 FileName()117 util::StringView FileName() const 118 { 119 return sourceFile_.GetFileName(); 120 } 121 AbsoluteName()122 util::StringView AbsoluteName() const 123 { 124 return sourceFile_.GetAbsolutePath(); 125 } 126 ResolvedFilePath()127 util::StringView ResolvedFilePath() const 128 { 129 return resolvedFilePath_; 130 } 131 Ast()132 ir::BlockStatement *Ast() 133 { 134 return ast_; 135 } 136 Ast()137 const ir::BlockStatement *Ast() const 138 { 139 return ast_; 140 } 141 SetAst(ir::BlockStatement * ast)142 void SetAst(ir::BlockStatement *ast) 143 { 144 ast_ = ast; 145 } 146 GlobalClass()147 ir::ClassDefinition *GlobalClass() 148 { 149 return globalClass_; 150 } 151 GlobalClass()152 const ir::ClassDefinition *GlobalClass() const 153 { 154 return globalClass_; 155 } 156 SetGlobalClass(ir::ClassDefinition * globalClass)157 void SetGlobalClass(ir::ClassDefinition *globalClass) 158 { 159 globalClass_ = globalClass; 160 } 161 ExternalSources()162 ExternalSource &ExternalSources() 163 { 164 return externalSources_; 165 } 166 ExternalSources()167 const ExternalSource &ExternalSources() const 168 { 169 return externalSources_; 170 } 171 SetSource(const util::StringView & sourceCode,const util::StringView & sourceFilePath,const util::StringView & sourceFileFolder)172 void SetSource(const util::StringView &sourceCode, const util::StringView &sourceFilePath, 173 const util::StringView &sourceFileFolder) 174 { 175 sourceCode_ = sourceCode; 176 sourceFile_ = util::Path(sourceFilePath, Allocator()); 177 sourceFileFolder_ = sourceFileFolder; 178 } 179 SetSource(const ark::es2panda::SourceFile & sourceFile)180 void SetSource(const ark::es2panda::SourceFile &sourceFile) 181 { 182 sourceCode_ = util::UString(sourceFile.source, Allocator()).View(); 183 sourceFile_ = util::Path(sourceFile.filePath, Allocator()); 184 sourceFileFolder_ = util::UString(sourceFile.fileFolder, Allocator()).View(); 185 resolvedFilePath_ = util::UString(sourceFile.resolvedPath, Allocator()).View(); 186 } 187 188 void SetModuleInfo(const util::StringView &name, bool isPackage, bool omitName = false) 189 { 190 moduleInfo_.moduleName = name; 191 moduleInfo_.isPackageModule = isPackage; 192 moduleInfo_.omitModuleName = omitName; 193 } 194 ModuleName()195 const util::StringView &ModuleName() const 196 { 197 return moduleInfo_.moduleName; 198 } 199 IsPackageModule()200 bool IsPackageModule() const 201 { 202 return moduleInfo_.isPackageModule; 203 } 204 OmitModuleName()205 bool OmitModuleName() const 206 { 207 return moduleInfo_.omitModuleName; 208 } 209 IsEntryPoint()210 const bool &IsEntryPoint() const 211 { 212 return entryPoint_; 213 } 214 MarkEntry()215 void MarkEntry() 216 { 217 entryPoint_ = true; 218 } 219 MarkASTAsChecked()220 void MarkASTAsChecked() 221 { 222 isASTchecked_ = true; 223 } 224 IsASTChecked()225 bool IsASTChecked() const 226 { 227 return isASTchecked_; 228 } 229 230 varbinder::ClassScope *GlobalClassScope(); 231 const varbinder::ClassScope *GlobalClassScope() const; 232 233 varbinder::GlobalScope *GlobalScope(); 234 const varbinder::GlobalScope *GlobalScope() const; 235 236 std::string Dump() const; 237 238 void DumpSilent() const; 239 240 void AddNodeToETSNolintCollection(const ir::AstNode *node, const std::set<ETSWarnings> &warningsCollection); 241 bool NodeContainsETSNolint(const ir::AstNode *node, ETSWarnings warning); 242 243 private: 244 struct ModuleInfo { 245 explicit ModuleInfo(util::StringView name = util::StringView(), bool isPackage = false, bool omitName = false) moduleNameModuleInfo246 : moduleName(name), isPackageModule(isPackage), omitModuleName(omitName) 247 { 248 } 249 250 // NOLINTBEGIN(misc-non-private-member-variables-in-classes) 251 util::StringView moduleName; 252 bool isPackageModule; 253 bool omitModuleName; // unclear naming, used to determine the entry point without --ets-module option 254 // NOLINTEND(misc-non-private-member-variables-in-classes) 255 }; 256 257 ArenaAllocator *allocator_ {}; 258 varbinder::VarBinder *varbinder_ {}; 259 ir::BlockStatement *ast_ {}; 260 ir::ClassDefinition *globalClass_ {}; 261 util::StringView sourceCode_ {}; 262 util::Path sourceFile_ {}; 263 util::StringView sourceFileFolder_ {}; 264 util::StringView resolvedFilePath_ {}; 265 ExternalSource externalSources_; 266 ScriptKind kind_ {}; 267 ScriptExtension extension_ {}; 268 bool entryPoint_ {}; 269 ETSNolintsCollectionMap etsnolintCollection_; 270 ModuleInfo moduleInfo_ {}; 271 bool isASTchecked_ {}; 272 }; 273 } // namespace ark::es2panda::parser 274 275 #endif 276