• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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     bool enableEtsImplements {false};
128     // Ability to modify package names using bytecode
129     std::string modifiedPkgName {};
130     /**
131      * Column number recording switch for bytecode instructions.
132      * In debug mode: adds column numbers to all bytecode instructions.
133      * In other mode: adds column numbers to call instructions when on,
134      * adds no column numbers when off.
135      */
136     bool enableColumn {false};
137 };
138 
139 enum class ErrorType {
140     GENERIC,
141     SYNTAX,
142     TYPE,
143 };
144 
145 class Error : public std::exception {
146 public:
147     Error() noexcept = default;
Error(ErrorType type,const std::string_view & message)148     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)149     explicit Error(ErrorType type, const std::string_view &message, size_t line, size_t column) noexcept
150         : type_(type), message_(message), line_(line), col_(column)
151     {
152     }
153     ~Error() override = default;
154     DEFAULT_COPY_SEMANTIC(Error);
155     DEFAULT_MOVE_SEMANTIC(Error);
156 
Type()157     ErrorType Type() const noexcept
158     {
159         return type_;
160     }
161 
TypeString()162     const char *TypeString() const noexcept
163     {
164         switch (type_) {
165             case ErrorType::SYNTAX:
166                 return "SyntaxError";
167             case ErrorType::TYPE:
168                 return "TypeError";
169             default:
170                 break;
171         }
172 
173         return "Error";
174     }
175 
what()176     const char *what() const noexcept override
177     {
178         return message_.c_str();
179     }
180 
ErrorCode()181     int ErrorCode() const noexcept
182     {
183         return errorCode_;
184     }
185 
Message()186     const std::string &Message() const noexcept
187     {
188         return message_;
189     }
190 
Line()191     size_t Line() const
192     {
193         return line_;
194     }
195 
Col()196     size_t Col() const
197     {
198         return col_;
199     }
200 
Reported()201     bool Reported() const
202     {
203         return reported_;
204     }
205 
SetReported(bool reported)206     void SetReported(bool reported)
207     {
208         reported_ = reported;
209     }
210 
211 private:
212     ErrorType type_ {ErrorType::GENERIC};
213     std::string message_;
214     size_t line_ {};
215     size_t col_ {};
216     int errorCode_ {1};
217     bool reported_ {false};
218 };
219 
220 class Compiler {
221 public:
222     explicit Compiler(ScriptExtension ext);
223     explicit Compiler(ScriptExtension ext, size_t threadCount);
224     ~Compiler();
225     NO_COPY_SEMANTIC(Compiler);
226     NO_MOVE_SEMANTIC(Compiler);
227 
228     panda::pandasm::Program *Compile(const SourceFile &input, const CompilerOptions &options,
229                                      util::SymbolTable *symbolTable = nullptr);
230     panda::pandasm::Program *CompileFile(const CompilerOptions &options, SourceFile *src,
231                                          util::SymbolTable *symbolTable);
232     void ProcessAstForTS(parser::Program *ast, const es2panda::CompilerOptions &options);
233     static int CompileFiles(CompilerOptions &options,
234         std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo, panda::ArenaAllocator *allocator);
235 
Compile(const SourceFile & input)236     inline panda::pandasm::Program *Compile(const SourceFile &input)
237     {
238         CompilerOptions options;
239 
240         return Compile(input, options);
241     }
242 
243     static void DumpAsm(const panda::pandasm::Program *prog);
244 
GetError()245     Error &GetError() noexcept
246     {
247         return error_;
248     }
249 
250     panda::pandasm::Program *CompileAbcFile(const std::string &fname, const CompilerOptions &options);
251 
252     void CompileAbcFileInParallel(SourceFile *src, const CompilerOptions &options,
253                                   std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
254                                   panda::ArenaAllocator *allocator);
255 
GetExpectedProgsCount()256     static size_t GetExpectedProgsCount()
257     {
258         return expectedProgsCount_;
259     }
260 
SetExpectedProgsCount(size_t count)261     static void SetExpectedProgsCount(size_t count)
262     {
263         expectedProgsCount_ = count;
264     }
265 
266 private:
267     util::PatchFix *InitPatchFixHelper(const SourceFile &input, const CompilerOptions &options,
268                                        util::SymbolTable *symbolTable);
269     static void CleanPatchFixHelper(const util::PatchFix *patchFixHelper);
270     void CheckOptionsAndFileForAbcInput(const std::string &fname, const CompilerOptions &options);
271 
272     static size_t expectedProgsCount_;
273     parser::ParserImpl *parser_;
274     compiler::CompilerImpl *compiler_;
275     panda::abc2program::Abc2ProgramCompiler *abcToAsmCompiler_;
276     std::unique_ptr<parser::Transformer> transformer_ {nullptr};
277     Error error_;
278 };
279 }  // namespace panda::es2panda
280 
281 #endif
282