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/patchFix.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() const48 ArenaAllocator *Allocator() const 49 { 50 return allocator_.get(); 51 } 52 Binder() const53 const binder::Binder *Binder() const 54 { 55 return binder_; 56 } 57 Binder()58 binder::Binder *Binder() 59 { 60 return binder_; 61 } 62 Extension() const63 ScriptExtension Extension() const 64 { 65 return extension_; 66 } 67 Kind() const68 ScriptKind Kind() const 69 { 70 return kind_; 71 } 72 IsCommonjs() const73 bool IsCommonjs() const 74 { 75 return kind_ == ScriptKind::COMMONJS; 76 } 77 ModuleRecord() const78 SourceTextModuleRecord *ModuleRecord() const 79 { 80 return moduleRecord_; 81 } 82 TypeModuleRecord() const83 SourceTextModuleRecord *TypeModuleRecord() const 84 { 85 return typeModuleRecord_; 86 } 87 SourceCode() const88 util::StringView SourceCode() const 89 { 90 return sourceCode_.View(); 91 } 92 SourceFile() const93 util::StringView SourceFile() const 94 { 95 return sourceFile_.View(); 96 } 97 RecordName() const98 util::StringView RecordName() const 99 { 100 return recordName_.View(); 101 } 102 FormatedRecordName() const103 util::StringView FormatedRecordName() const 104 { 105 return formatedRecordName_.View(); 106 } 107 GetLineIndex() const108 const lexer::LineIndex &GetLineIndex() const 109 { 110 return lineIndex_; 111 } 112 Ast()113 ir::BlockStatement *Ast() 114 { 115 return ast_; 116 } 117 Ast() const118 const ir::BlockStatement *Ast() const 119 { 120 return ast_; 121 } 122 SetAst(ir::BlockStatement *ast)123 void SetAst(ir::BlockStatement *ast) 124 { 125 ast_ = ast; 126 } 127 SetSource(const std::string &sourceCode, const std::string &sourceFile, bool isDtsFile)128 void SetSource(const std::string &sourceCode, const std::string &sourceFile, bool isDtsFile) 129 { 130 sourceCode_ = util::UString(sourceCode, Allocator()); 131 sourceFile_ = util::UString(sourceFile, Allocator()); 132 lineIndex_ = lexer::LineIndex(SourceCode()); 133 isDtsFile_ = isDtsFile; 134 } 135 SetRecordName(const std::string &recordName)136 void SetRecordName(const std::string &recordName) 137 { 138 recordName_ = util::UString(recordName, Allocator()); 139 std::string formatedRecordName = recordName + "."; 140 formatedRecordName_ = util::UString(formatedRecordName, Allocator()); 141 } 142 AddPatchFixHelper(util::PatchFix *patchFixHelper)143 void AddPatchFixHelper(util::PatchFix *patchFixHelper) 144 { 145 patchFixHelper_ = patchFixHelper; 146 } 147 PatchFixHelper()148 util::PatchFix *PatchFixHelper() 149 { 150 return patchFixHelper_; 151 } 152 IsDtsFile() const153 bool IsDtsFile() const 154 { 155 return isDtsFile_; 156 } 157 HasTLA() const158 bool HasTLA() const 159 { 160 return hasTLA_; 161 } 162 SetHasTLA(bool hasTLA)163 void SetHasTLA(bool hasTLA) 164 { 165 hasTLA_ = hasTLA; 166 } 167 IsDebug() const168 bool IsDebug() const 169 { 170 return isDebug_; 171 } 172 SetDebug(bool isDebug)173 void SetDebug(bool isDebug) 174 { 175 isDebug_ = isDebug; 176 } 177 TargetApiVersion() const178 int TargetApiVersion() const 179 { 180 return targetApiVersion_; 181 } 182 SetTargetApiVersion(int targetApiVersion)183 void SetTargetApiVersion(int targetApiVersion) 184 { 185 targetApiVersion_ = targetApiVersion; 186 } 187 SetTargetApiSubVersion(std::string targetApiSubVersion)188 void SetTargetApiSubVersion(std::string targetApiSubVersion) 189 { 190 targetApiSubVersion_ = targetApiSubVersion; 191 } 192 GetTargetApiSubVersion() const193 std::string GetTargetApiSubVersion() const 194 { 195 return targetApiSubVersion_; 196 } 197 UseDefineSemantic() const198 bool UseDefineSemantic() const 199 { 200 return useDefineSemantic_; 201 } 202 SetDefineSemantic(bool useDefineSemantic)203 void SetDefineSemantic(bool useDefineSemantic) 204 { 205 useDefineSemantic_ = useDefineSemantic; 206 } 207 SetShared(bool isShared)208 void SetShared(bool isShared) 209 { 210 isShared_ = isShared; 211 } 212 IsShared() const213 bool IsShared() const 214 { 215 return isShared_; 216 } 217 SetModuleRecordFieldName(std::string moduleRecordFieldName)218 void SetModuleRecordFieldName(std::string moduleRecordFieldName) 219 { 220 moduleRecordFieldName_ = moduleRecordFieldName; 221 } 222 ModuleRecordFieldName() const223 std::string ModuleRecordFieldName() const 224 { 225 return moduleRecordFieldName_; 226 } 227 SetEnableAnnotations(bool enableAnnotations)228 void SetEnableAnnotations(bool enableAnnotations) 229 { 230 enableAnnotations_ = enableAnnotations; 231 } 232 IsEnableAnnotations()233 bool IsEnableAnnotations() 234 { 235 return enableAnnotations_; 236 } 237 238 std::string Dump() const; 239 void SetKind(ScriptKind kind); 240 241 private: 242 std::unique_ptr<ArenaAllocator> allocator_ {}; 243 binder::Binder *binder_ {}; 244 ir::BlockStatement *ast_ {}; 245 util::UString sourceCode_ {}; 246 util::UString sourceFile_ {}; 247 util::UString recordName_ {}; 248 util::UString formatedRecordName_ {}; 249 ScriptKind kind_ {}; 250 ScriptExtension extension_ {}; 251 lexer::LineIndex lineIndex_ {}; 252 SourceTextModuleRecord *moduleRecord_ {nullptr}; 253 SourceTextModuleRecord *typeModuleRecord_ {nullptr}; 254 util::PatchFix *patchFixHelper_ {nullptr}; 255 bool isDtsFile_ {false}; 256 bool hasTLA_ {false}; 257 bool isDebug_ {false}; 258 int targetApiVersion_ {0}; 259 bool useDefineSemantic_ {true}; 260 bool isShared_ {false}; 261 bool enableAnnotations_ {false}; 262 std::string targetApiSubVersion_ { util::Helpers::DEFAULT_SUB_API_VERSION }; 263 std::string moduleRecordFieldName_; 264 }; 265 266 } // namespace panda::es2panda::parser 267 268 #endif 269