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