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 useDefineSemantic {false}; 86 bool typeExtractor {false}; 87 bool typeDtsBuiltin {false}; 88 bool recordSource {false}; 89 int fileThreadCount {0}; 90 int functionThreadCount {0}; 91 int optLevel {0}; 92 std::string output {}; 93 std::string debugInfoSourceFile {}; 94 std::vector<es2panda::SourceFile> sourceFiles; 95 PatchFixOptions patchFixOptions; 96 bool bcVersion {false}; 97 bool bcMinVersion {false}; 98 int targetApiVersion {0}; 99 std::unordered_map<std::string, std::string> cacheFiles; 100 }; 101 102 enum class ErrorType { 103 GENERIC, 104 SYNTAX, 105 TYPE, 106 }; 107 108 class Error : public std::exception { 109 public: 110 Error() noexcept = default; Error(ErrorType type,std::string_view message)111 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)112 explicit Error(ErrorType type, std::string_view message, size_t line, size_t column) noexcept 113 : type_(type), message_(message), line_(line), col_(column) 114 { 115 } 116 ~Error() override = default; 117 DEFAULT_COPY_SEMANTIC(Error); 118 DEFAULT_MOVE_SEMANTIC(Error); 119 Type()120 ErrorType Type() const noexcept 121 { 122 return type_; 123 } 124 TypeString()125 const char *TypeString() const noexcept 126 { 127 switch (type_) { 128 case ErrorType::SYNTAX: 129 return "SyntaxError"; 130 case ErrorType::TYPE: 131 return "TypeError"; 132 default: 133 break; 134 } 135 136 return "Error"; 137 } 138 what()139 const char *what() const noexcept override 140 { 141 return message_.c_str(); 142 } 143 ErrorCode()144 int ErrorCode() const noexcept 145 { 146 return errorCode_; 147 } 148 Message()149 const std::string &Message() const noexcept 150 { 151 return message_; 152 } 153 Line()154 size_t Line() const 155 { 156 return line_; 157 } 158 Col()159 size_t Col() const 160 { 161 return col_; 162 } 163 164 private: 165 ErrorType type_ {ErrorType::GENERIC}; 166 std::string message_; 167 size_t line_ {}; 168 size_t col_ {}; 169 int errorCode_ {1}; 170 }; 171 172 class Compiler { 173 public: 174 explicit Compiler(ScriptExtension ext); 175 explicit Compiler(ScriptExtension ext, size_t threadCount); 176 ~Compiler(); 177 NO_COPY_SEMANTIC(Compiler); 178 NO_MOVE_SEMANTIC(Compiler); 179 180 panda::pandasm::Program *Compile(const SourceFile &input, const CompilerOptions &options, 181 util::SymbolTable *symbolTable = nullptr); 182 panda::pandasm::Program *CompileFile(const CompilerOptions &options, SourceFile *src, 183 util::SymbolTable *symbolTable); 184 185 static int CompileFiles(CompilerOptions &options, 186 std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo, panda::ArenaAllocator *allocator); 187 Compile(const SourceFile & input)188 inline panda::pandasm::Program *Compile(const SourceFile &input) 189 { 190 CompilerOptions options; 191 192 return Compile(input, options); 193 } 194 195 static void DumpAsm(const panda::pandasm::Program *prog); 196 GetError()197 const Error &GetError() const noexcept 198 { 199 return error_; 200 } 201 202 private: 203 util::PatchFix *InitPatchFixHelper(const SourceFile &input, const CompilerOptions &options, 204 util::SymbolTable *symbolTable); 205 static void CleanPatchFixHelper(const util::PatchFix *patchFixHelper); 206 207 parser::ParserImpl *parser_; 208 compiler::CompilerImpl *compiler_; 209 std::unique_ptr<parser::Transformer> transformer_ {nullptr}; 210 Error error_; 211 }; 212 } // namespace panda::es2panda 213 214 #endif 215