1 /**
2 * Copyright (c) 2021-2024 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 "util/arktsconfig.h"
21 #include "util/plugin.h"
22 #include "util/ustring.h"
23
24 #include <string>
25 #include <functional>
26 #include <memory>
27 #include <unordered_set>
28
29 namespace panda::pandasm {
30 struct Program;
31 } // namespace panda::pandasm
32
33 namespace panda::es2panda {
34 namespace parser {
35 class ParserImpl;
36 } // namespace parser
37
38 namespace compiler {
39 class CompilerImpl;
40 class CompilerContext;
41 } // namespace compiler
42
43 namespace varbinder {
44 class VarBinder;
45 } // namespace varbinder
46
47 enum class ScriptExtension {
48 JS,
49 TS,
50 AS,
51 ETS,
52 };
53
54 enum class CompilationMode {
55 GEN_STD_LIB,
56 PROJECT,
57 SINGLE_FILE,
58 };
59
ToLanguage(ScriptExtension ext)60 inline Language ToLanguage(ScriptExtension ext)
61 {
62 switch (ext) {
63 case ScriptExtension::JS:
64 return Language(Language::Id::JS);
65 case ScriptExtension::TS:
66 return Language(Language::Id::TS);
67 case ScriptExtension::AS:
68 return Language(Language::Id::AS);
69 case ScriptExtension::ETS:
70 return Language(Language::Id::ETS);
71 }
72 UNREACHABLE();
73 }
74
75 struct SourceFile {
76 SourceFile(std::string_view fn, std::string_view s);
77 SourceFile(std::string_view fn, std::string_view s, bool m);
78 SourceFile(std::string_view fn, std::string_view s, std::string_view rp, bool m);
79
80 // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
81 std::string_view filePath {};
82 std::string_view fileFolder {};
83 std::string_view source {};
84 std::string_view resolvedPath {};
85 bool isModule {};
86 // NOLINTEND(misc-non-private-member-variables-in-classes)
87 };
88
89 struct CompilerOptions {
90 // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
91 bool isDebug {};
92 bool isEtsModule {};
93 bool isEval {};
94 bool isDirectEval {};
95 bool isFunctionEval {};
96 bool dumpAst {};
97 bool opDumpAstOnlySilent {};
98 bool dumpCheckedAst {};
99 bool dumpAsm {};
100 bool dumpDebugInfo {};
101 bool parseOnly {};
102 std::string stdLib {};
103 std::string tsDeclOut {};
104 std::vector<std::string> plugins {};
105 std::unordered_set<std::string> skipPhases {};
106 std::unordered_set<std::string> verifierWarnings {};
107 std::unordered_set<std::string> verifierErrors {};
108 std::unordered_set<std::string> dumpBeforePhases {};
109 std::unordered_set<std::string> dumpEtsSrcBeforePhases {};
110 std::unordered_set<std::string> dumpAfterPhases {};
111 std::unordered_set<std::string> dumpEtsSrcAfterPhases {};
112 std::shared_ptr<ArkTsConfig> arktsConfig {};
113 CompilationMode compilationMode {};
114 // NOLINTEND(misc-non-private-member-variables-in-classes)
115 };
116
117 enum class ErrorType {
118 INVALID,
119 GENERIC,
120 SYNTAX,
121 TYPE,
122 };
123
124 class Error : public std::exception {
125 public:
126 Error() noexcept = default;
Error(ErrorType type,std::string_view file,std::string_view message)127 explicit Error(ErrorType type, std::string_view file, std::string_view message) noexcept
128 : type_(type), file_(file), message_(message)
129 {
130 }
Error(ErrorType type,std::string_view file,std::string_view message,size_t line,size_t column)131 explicit Error(ErrorType type, std::string_view file, std::string_view message, size_t line, size_t column) noexcept
132 : type_(type), file_(file), message_(message), line_(line), col_(column)
133 {
134 }
135 ~Error() override = default;
136 DEFAULT_COPY_SEMANTIC(Error);
137 DEFAULT_MOVE_SEMANTIC(Error);
138
Type()139 ErrorType Type() const noexcept
140 {
141 return type_;
142 }
143
TypeString()144 const char *TypeString() const noexcept
145 {
146 switch (type_) {
147 case ErrorType::SYNTAX:
148 return "SyntaxError";
149 case ErrorType::TYPE:
150 return "TypeError";
151 default:
152 break;
153 }
154
155 return "Error";
156 }
157
what()158 const char *what() const noexcept override
159 {
160 return message_.c_str();
161 }
162
ErrorCode()163 int ErrorCode() const noexcept
164 {
165 return errorCode_;
166 }
167
Message()168 const std::string &Message() const noexcept
169 {
170 return message_;
171 }
172
File()173 const std::string &File() const noexcept
174 {
175 return file_;
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
188 private:
189 ErrorType type_ {ErrorType::INVALID};
190 std::string file_;
191 std::string message_;
192 size_t line_ {};
193 size_t col_ {};
194 int errorCode_ {1};
195 };
196
197 class Compiler {
198 public:
199 explicit Compiler(ScriptExtension ext);
200 explicit Compiler(ScriptExtension ext, size_t threadCount);
201 explicit Compiler(ScriptExtension ext, size_t threadCount, std::vector<util::Plugin> &&plugins);
202 ~Compiler();
203 NO_COPY_SEMANTIC(Compiler);
204 NO_MOVE_SEMANTIC(Compiler);
205
206 pandasm::Program *Compile(const SourceFile &input, const CompilerOptions &options, uint32_t parseStatus = 0);
207
Compile(const SourceFile & input)208 inline pandasm::Program *Compile(const SourceFile &input)
209 {
210 CompilerOptions options;
211
212 return Compile(input, options, 0);
213 }
214
215 static void DumpAsm(const pandasm::Program *prog);
216
GetError()217 const Error &GetError() const noexcept
218 {
219 return error_;
220 }
221
Plugins()222 std::vector<util::Plugin> const &Plugins()
223 {
224 return plugins_;
225 }
226
227 private:
228 std::vector<util::Plugin> const plugins_;
229 compiler::CompilerImpl *compiler_;
230 Error error_;
231 ScriptExtension ext_;
232 };
233 } // namespace panda::es2panda
234
235 #endif
236