1 /** 2 * Copyright (c) 2021 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 <lexer/token/sourceLocation.h> 20 #include <macros.h> 21 #include <mem/arena_allocator.h> 22 #include <parser/module/sourceTextModuleRecord.h> 23 #include <util/hotfix.h> 24 #include <util/ustring.h> 25 26 #include "es2panda.h" 27 28 namespace panda::es2panda::ir { 29 class BlockStatement; 30 } // namespace panda::es2panda::ir 31 32 namespace panda::es2panda::binder { 33 class Binder; 34 } // namespace panda::es2panda::binder 35 36 namespace panda::es2panda::parser { 37 38 enum class ScriptKind { SCRIPT, MODULE, COMMONJS }; 39 40 class Program { 41 public: 42 explicit Program(es2panda::ScriptExtension extension); 43 NO_COPY_SEMANTIC(Program); 44 Program(Program &&other); 45 Program &operator=(Program &&other); 46 ~Program() = default; 47 Allocator()48 ArenaAllocator *Allocator() const 49 { 50 return allocator_.get(); 51 } 52 Binder()53 const binder::Binder *Binder() const 54 { 55 return binder_; 56 } 57 Binder()58 binder::Binder *Binder() 59 { 60 return binder_; 61 } 62 Extension()63 ScriptExtension Extension() const 64 { 65 return extension_; 66 } 67 Kind()68 ScriptKind Kind() const 69 { 70 return kind_; 71 } 72 ModuleRecord()73 SourceTextModuleRecord *ModuleRecord() const 74 { 75 return moduleRecord_; 76 } 77 SourceCode()78 util::StringView SourceCode() const 79 { 80 return sourceCode_.View(); 81 } 82 SourceFile()83 util::StringView SourceFile() const 84 { 85 return sourceFile_.View(); 86 } 87 RecordName()88 util::StringView RecordName() const 89 { 90 return recordName_.View(); 91 } 92 FormatedRecordName()93 util::StringView FormatedRecordName() const 94 { 95 return formatedRecordName_.View(); 96 } 97 GetLineIndex()98 const lexer::LineIndex &GetLineIndex() const 99 { 100 return lineIndex_; 101 } 102 Ast()103 ir::BlockStatement *Ast() 104 { 105 return ast_; 106 } 107 Ast()108 const ir::BlockStatement *Ast() const 109 { 110 return ast_; 111 } 112 SetAst(ir::BlockStatement * ast)113 void SetAst(ir::BlockStatement *ast) 114 { 115 ast_ = ast; 116 } 117 SetSource(const std::string & sourceCode,const std::string & sourceFile,bool isDtsFile)118 void SetSource(const std::string &sourceCode, const std::string &sourceFile, bool isDtsFile) 119 { 120 sourceCode_ = util::UString(sourceCode, Allocator()); 121 sourceFile_ = util::UString(sourceFile, Allocator()); 122 lineIndex_ = lexer::LineIndex(SourceCode()); 123 isDtsFile_ = isDtsFile; 124 } 125 SetRecordName(const std::string & recordName)126 void SetRecordName(const std::string &recordName) 127 { 128 recordName_ = util::UString(recordName, Allocator()); 129 std::string formatedRecordName = recordName + "."; 130 formatedRecordName_ = util::UString(formatedRecordName, Allocator()); 131 } 132 AddHotfixHelper(util::Hotfix * hotfixHelper)133 void AddHotfixHelper(util::Hotfix *hotfixHelper) 134 { 135 hotfixHelper_ = hotfixHelper; 136 } 137 HotfixHelper()138 util::Hotfix *HotfixHelper() 139 { 140 return hotfixHelper_; 141 } 142 IsDtsFile()143 bool IsDtsFile() const 144 { 145 return isDtsFile_; 146 } 147 148 std::string Dump() const; 149 void SetKind(ScriptKind kind); 150 151 private: 152 std::unique_ptr<ArenaAllocator> allocator_ {}; 153 binder::Binder *binder_ {}; 154 ir::BlockStatement *ast_ {}; 155 util::UString sourceCode_ {}; 156 util::UString sourceFile_ {}; 157 util::UString recordName_ {}; 158 util::UString formatedRecordName_ {}; 159 ScriptKind kind_ {}; 160 ScriptExtension extension_ {}; 161 lexer::LineIndex lineIndex_ {}; 162 SourceTextModuleRecord *moduleRecord_ {nullptr}; 163 util::Hotfix *hotfixHelper_ {nullptr}; 164 bool isDtsFile_ {false}; 165 }; 166 167 } // namespace panda::es2panda::parser 168 169 #endif 170