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 25 #include <string> 26 #include <unordered_map> 27 28 namespace panda::pandasm { 29 struct Program; 30 } // namespace panda::pandasm 31 32 namespace panda::es2panda { 33 namespace parser { 34 class ParserImpl; 35 class Transformer; 36 enum class ScriptKind; 37 } // namespace parser 38 39 namespace compiler { 40 class CompilerImpl; 41 } // namespace compiler 42 43 enum class ScriptExtension { 44 JS, 45 TS, 46 AS, 47 }; 48 49 struct SourceFile { SourceFileSourceFile50 SourceFile(std::string fn, std::string rn, parser::ScriptKind sk, ScriptExtension se) 51 : fileName(fn), recordName(rn), scriptKind(sk), scriptExtension(se) 52 { 53 } 54 55 std::string fileName {}; 56 std::string recordName {}; 57 std::string_view source {}; 58 parser::ScriptKind scriptKind {}; 59 ScriptExtension scriptExtension {}; 60 std::string sourcefile {}; 61 std::string pkgName {}; 62 uint32_t hash {0}; 63 }; 64 65 struct PatchFixOptions { 66 std::string dumpSymbolTable {}; 67 std::string symbolTable {}; 68 bool generatePatch {false}; 69 bool hotReload {false}; 70 bool coldFix {false}; 71 }; 72 73 struct CompilerOptions { 74 bool isDebug {false}; 75 bool dumpAst {false}; 76 bool dumpTransformedAst {false}; 77 bool checkTransformedAstStructure {false}; 78 bool dumpAsm {false}; 79 bool dumpDebugInfo {false}; 80 bool parseOnly {false}; 81 bool enableTypeCheck {false}; 82 bool dumpLiteralBuffer {false}; 83 bool isDebuggerEvaluateExpressionMode {false}; 84 bool mergeAbc {false}; 85 bool typeExtractor {false}; 86 bool typeDtsBuiltin {false}; 87 int fileThreadCount {0}; 88 int functionThreadCount {0}; 89 int optLevel {0}; 90 std::string output {}; 91 std::string debugInfoSourceFile {}; 92 std::vector<es2panda::SourceFile> sourceFiles; 93 PatchFixOptions patchFixOptions; 94 bool bcVersion {false}; 95 bool bcMinVersion {false}; 96 std::unordered_map<std::string, std::string> cacheFiles; 97 }; 98 99 enum class ErrorType { 100 GENERIC, 101 SYNTAX, 102 TYPE, 103 }; 104 105 class Error : public std::exception { 106 public: 107 Error() noexcept = default; Error(ErrorType type,std::string_view message)108 explicit Error(ErrorType type, std::string_view message) noexcept : type_(type), message_(message) {} Error(ErrorType type,std::string_view message,size_t line,size_t column)109 explicit Error(ErrorType type, std::string_view message, size_t line, size_t column) noexcept 110 : type_(type), message_(message), line_(line), col_(column) 111 { 112 } 113 ~Error() override = default; 114 DEFAULT_COPY_SEMANTIC(Error); 115 DEFAULT_MOVE_SEMANTIC(Error); 116 Type()117 ErrorType Type() const noexcept 118 { 119 return type_; 120 } 121 TypeString()122 const char *TypeString() const noexcept 123 { 124 switch (type_) { 125 case ErrorType::SYNTAX: 126 return "SyntaxError"; 127 case ErrorType::TYPE: 128 return "TypeError"; 129 default: 130 break; 131 } 132 133 return "Error"; 134 } 135 what()136 const char *what() const noexcept override 137 { 138 return message_.c_str(); 139 } 140 ErrorCode()141 int ErrorCode() const noexcept 142 { 143 return errorCode_; 144 } 145 Message()146 const std::string &Message() const noexcept 147 { 148 return message_; 149 } 150 Line()151 size_t Line() const 152 { 153 return line_; 154 } 155 Col()156 size_t Col() const 157 { 158 return col_; 159 } 160 161 private: 162 ErrorType type_ {ErrorType::GENERIC}; 163 std::string message_; 164 size_t line_ {}; 165 size_t col_ {}; 166 int errorCode_ {1}; 167 }; 168 169 class Compiler { 170 public: 171 explicit Compiler(ScriptExtension ext); 172 explicit Compiler(ScriptExtension ext, size_t threadCount); 173 ~Compiler(); 174 NO_COPY_SEMANTIC(Compiler); 175 NO_MOVE_SEMANTIC(Compiler); 176 177 panda::pandasm::Program *Compile(const SourceFile &input, const CompilerOptions &options, 178 util::SymbolTable *symbolTable = nullptr); 179 panda::pandasm::Program *CompileFile(const CompilerOptions &options, 180 SourceFile *src, util::SymbolTable *symbolTable); 181 182 static int CompileFiles(CompilerOptions &options, 183 std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo, panda::ArenaAllocator *allocator); 184 Compile(const SourceFile & input)185 inline panda::pandasm::Program *Compile(const SourceFile &input) 186 { 187 CompilerOptions options; 188 189 return Compile(input, options); 190 } 191 192 static void DumpAsm(const panda::pandasm::Program *prog); 193 GetError()194 const Error &GetError() const noexcept 195 { 196 return error_; 197 } 198 199 private: 200 util::PatchFix *InitPatchFixHelper(const SourceFile &input, const CompilerOptions &options, 201 util::SymbolTable *symbolTable); 202 static void CleanPatchFixHelper(const util::PatchFix *patchFixHelper); 203 204 parser::ParserImpl *parser_; 205 compiler::CompilerImpl *compiler_; 206 std::unique_ptr<parser::Transformer> transformer_ {nullptr}; 207 Error error_; 208 }; 209 } // namespace panda::es2panda 210 211 #endif 212