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_PUBLIC_H 17 #define ES2PANDA_PUBLIC_H 18 19 #include <macros.h> 20 #include <mem/arena_allocator.h> 21 #include <util/patchFix.h> 22 #include <util/programCache.h> 23 #include <util/symbolTable.h> 24 #include <abc2program/abc2program_compiler.h> 25 26 #include <string> 27 #include <unordered_map> 28 29 namespace panda::pandasm { 30 struct Program; 31 } // namespace panda::pandasm 32 33 namespace panda::es2panda { 34 struct CompileContextInfo; 35 struct PkgInfo; 36 37 namespace parser { 38 class ParserImpl; 39 class Program; 40 class Transformer; 41 enum class ScriptKind; 42 } // namespace parser 43 44 namespace compiler { 45 class CompilerImpl; 46 } // namespace compiler 47 48 enum class ScriptExtension { 49 JS, 50 TS, 51 AS, 52 ABC, 53 }; 54 55 struct SourceFile { SourceFileSourceFile56 SourceFile(const std::string &fn, const std::string &recordName, parser::ScriptKind sk, ScriptExtension se) 57 : fileName(fn), recordName(recordName), scriptKind(sk), scriptExtension(se) 58 { 59 } 60 61 std::string fileName {}; 62 std::string recordName {}; 63 std::string_view source {}; 64 parser::ScriptKind scriptKind {}; 65 ScriptExtension scriptExtension {}; 66 std::string sourcefile {}; 67 std::string pkgName {}; 68 uint32_t hash {0}; 69 bool isSharedModule {false}; 70 bool isSourceMode {true}; 71 std::string sourceLang {}; 72 }; 73 74 struct PatchFixOptions { 75 std::string dumpSymbolTable {}; 76 std::string symbolTable {}; 77 bool generatePatch {false}; 78 bool hotReload {false}; 79 bool coldReload {false}; 80 bool coldFix {false}; 81 }; 82 83 struct CompilerOptions { 84 bool enableAbcInput {false}; 85 bool dumpAsmProgram {false}; 86 bool dumpNormalizedAsmProgram {false}; 87 bool isDebug {false}; 88 bool dumpAst {false}; 89 bool dumpTransformedAst {false}; 90 bool checkTransformedAstStructure {false}; 91 bool dumpAsm {false}; 92 bool dumpDebugInfo {false}; 93 bool parseOnly {false}; 94 bool enableTypeCheck {false}; 95 bool dumpLiteralBuffer {false}; 96 bool isDebuggerEvaluateExpressionMode {false}; 97 bool mergeAbc {false}; 98 bool useDefineSemantic {false}; 99 bool typeExtractor {false}; 100 bool typeDtsBuiltin {false}; 101 bool recordDebugSource {false}; 102 int fileThreadCount {0}; 103 int functionThreadCount {0}; 104 int abcClassThreadCount {0}; 105 int optLevel {0}; 106 std::string output {}; 107 std::string debugInfoSourceFile {}; 108 std::vector<es2panda::SourceFile> sourceFiles; 109 PatchFixOptions patchFixOptions; 110 bool bcVersion {false}; 111 bool bcMinVersion {false}; 112 int targetApiVersion {0}; 113 bool targetBcVersion {false}; 114 std::unordered_map<std::string, std::string> cacheFiles; 115 std::string transformLib {}; 116 bool branchElimination {false}; 117 bool requireGlobalOptimization {false}; 118 std::string compileContextInfoPath {}; 119 CompileContextInfo compileContextInfo {}; 120 bool dumpDepsInfo {false}; 121 bool updatePkgVersionForAbcInput {false}; 122 bool removeRedundantFile {false}; 123 bool dumpString {false}; 124 std::string targetApiSubVersion; 125 std::string moduleRecordFieldName; 126 bool enableAnnotations; 127 // Ability to modify package names using bytecode 128 std::string modifiedPkgName {}; 129 }; 130 131 enum class ErrorType { 132 GENERIC, 133 SYNTAX, 134 TYPE, 135 }; 136 137 class Error : public std::exception { 138 public: 139 Error() noexcept = default; Error(ErrorType type,const std::string_view & message)140 explicit Error(ErrorType type, const std::string_view &message) noexcept : type_(type), message_(message) {} Error(ErrorType type,const std::string_view & message,size_t line,size_t column)141 explicit Error(ErrorType type, const std::string_view &message, size_t line, size_t column) noexcept 142 : type_(type), message_(message), line_(line), col_(column) 143 { 144 } 145 ~Error() override = default; 146 DEFAULT_COPY_SEMANTIC(Error); 147 DEFAULT_MOVE_SEMANTIC(Error); 148 Type()149 ErrorType Type() const noexcept 150 { 151 return type_; 152 } 153 TypeString()154 const char *TypeString() const noexcept 155 { 156 switch (type_) { 157 case ErrorType::SYNTAX: 158 return "SyntaxError"; 159 case ErrorType::TYPE: 160 return "TypeError"; 161 default: 162 break; 163 } 164 165 return "Error"; 166 } 167 what()168 const char *what() const noexcept override 169 { 170 return message_.c_str(); 171 } 172 ErrorCode()173 int ErrorCode() const noexcept 174 { 175 return errorCode_; 176 } 177 Message()178 const std::string &Message() const noexcept 179 { 180 return message_; 181 } 182 Line()183 size_t Line() const 184 { 185 return line_; 186 } 187 Col()188 size_t Col() const 189 { 190 return col_; 191 } 192 Reported()193 bool Reported() const 194 { 195 return reported_; 196 } 197 SetReported(bool reported)198 void SetReported(bool reported) 199 { 200 reported_ = reported; 201 } 202 203 private: 204 ErrorType type_ {ErrorType::GENERIC}; 205 std::string message_; 206 size_t line_ {}; 207 size_t col_ {}; 208 int errorCode_ {1}; 209 bool reported_ {false}; 210 }; 211 212 class Compiler { 213 public: 214 explicit Compiler(ScriptExtension ext); 215 explicit Compiler(ScriptExtension ext, size_t threadCount); 216 ~Compiler(); 217 NO_COPY_SEMANTIC(Compiler); 218 NO_MOVE_SEMANTIC(Compiler); 219 220 panda::pandasm::Program *Compile(const SourceFile &input, const CompilerOptions &options, 221 util::SymbolTable *symbolTable = nullptr); 222 panda::pandasm::Program *CompileFile(const CompilerOptions &options, SourceFile *src, 223 util::SymbolTable *symbolTable); 224 void ProcessAstForTS(parser::Program *ast, const es2panda::CompilerOptions &options); 225 static int CompileFiles(CompilerOptions &options, 226 std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo, panda::ArenaAllocator *allocator); 227 Compile(const SourceFile & input)228 inline panda::pandasm::Program *Compile(const SourceFile &input) 229 { 230 CompilerOptions options; 231 232 return Compile(input, options); 233 } 234 235 static void DumpAsm(const panda::pandasm::Program *prog); 236 GetError()237 Error &GetError() noexcept 238 { 239 return error_; 240 } 241 242 panda::pandasm::Program *CompileAbcFile(const std::string &fname, const CompilerOptions &options); 243 244 void CompileAbcFileInParallel(SourceFile *src, const CompilerOptions &options, 245 std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo, 246 panda::ArenaAllocator *allocator); 247 GetExpectedProgsCount()248 static size_t GetExpectedProgsCount() 249 { 250 return expectedProgsCount_; 251 } 252 SetExpectedProgsCount(size_t count)253 static void SetExpectedProgsCount(size_t count) 254 { 255 expectedProgsCount_ = count; 256 } 257 258 private: 259 util::PatchFix *InitPatchFixHelper(const SourceFile &input, const CompilerOptions &options, 260 util::SymbolTable *symbolTable); 261 static void CleanPatchFixHelper(const util::PatchFix *patchFixHelper); 262 void CheckOptionsAndFileForAbcInput(const std::string &fname, const CompilerOptions &options); 263 264 static size_t expectedProgsCount_; 265 parser::ParserImpl *parser_; 266 compiler::CompilerImpl *compiler_; 267 panda::abc2program::Abc2ProgramCompiler *abcToAsmCompiler_; 268 std::unique_ptr<parser::Transformer> transformer_ {nullptr}; 269 Error error_; 270 }; 271 } // namespace panda::es2panda 272 273 #endif 274