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 ark::pandasm {
30 struct Program;
31 } // namespace ark::pandasm
32
33 namespace ark::es2panda {
34
35 constexpr std::string_view ES2PANDA_VERSION = "0.1";
36 namespace util {
37 class Options;
38 } // namespace util
39 namespace parser {
40 class ParserImpl;
41 } // namespace parser
42
43 namespace compiler {
44 class CompilerImpl;
45 } // namespace compiler
46
47 namespace varbinder {
48 class VarBinder;
49 } // namespace varbinder
50
51 enum class ScriptExtension {
52 JS,
53 TS,
54 AS,
55 ETS,
56 INVALID,
57 };
58
59 enum class CompilationMode {
60 GEN_STD_LIB,
61 PROJECT,
62 SINGLE_FILE,
63 };
64
ToLanguage(ScriptExtension ext)65 inline Language ToLanguage(ScriptExtension ext)
66 {
67 switch (ext) {
68 case ScriptExtension::JS:
69 return Language(Language::Id::JS);
70 case ScriptExtension::TS:
71 return Language(Language::Id::TS);
72 case ScriptExtension::AS:
73 return Language(Language::Id::AS);
74 case ScriptExtension::ETS:
75 return Language(Language::Id::ETS);
76 default:
77 UNREACHABLE();
78 }
79 UNREACHABLE();
80 }
81
82 struct SourceFile {
83 SourceFile(std::string_view fn, std::string_view s);
84 SourceFile(std::string_view fn, std::string_view s, bool m);
85 SourceFile(std::string_view fn, std::string_view s, std::string_view rp, bool m);
86
87 // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
88 std::string_view filePath {};
89 std::string_view fileFolder {};
90 std::string_view source {};
91 std::string_view resolvedPath {};
92 bool isModule {};
93 // NOLINTEND(misc-non-private-member-variables-in-classes)
94 };
95
96 enum ETSWarnings {
97 NONE,
98 IMPLICIT_BOXING_UNBOXING,
99 PROHIBIT_TOP_LEVEL_STATEMENTS,
100 BOOST_EQUALITY_STATEMENT,
101 REMOVE_LAMBDA,
102 SUGGEST_FINAL,
103 REMOVE_ASYNC_FUNCTIONS,
104 };
105
106 struct CompilerOptions {
107 // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
108 bool isDebug {};
109 bool isEtsModule {};
110 bool isEval {};
111 bool isDirectEval {};
112 bool isFunctionEval {};
113 bool dumpAst {};
114 bool opDumpAstOnlySilent {};
115 bool dumpCheckedAst {};
116 bool dumpAsm {};
117 bool dumpDebugInfo {};
118 bool parseOnly {};
119 bool verifierAllChecks {};
120 bool verifierFullProgram {};
121 std::string stdLib {};
122 std::vector<std::string> plugins {};
123 std::unordered_set<std::string> skipPhases {};
124 std::unordered_set<std::string> verifierWarnings {};
125 std::unordered_set<std::string> verifierErrors {};
126 std::unordered_set<std::string> dumpBeforePhases {};
127 std::unordered_set<std::string> dumpEtsSrcBeforePhases {};
128 std::unordered_set<std::string> dumpAfterPhases {};
129 std::unordered_set<std::string> dumpEtsSrcAfterPhases {};
130 std::shared_ptr<ArkTsConfig> arktsConfig {};
131 CompilationMode compilationMode {};
132 // NOLINTEND(misc-non-private-member-variables-in-classes)
133
134 // ETS Warning Groups
135 bool etsEnableAll {}; // Enable all ETS-warnings for System ArkTS
136 bool etsSubsetWarnings {}; // Enable only ETS-warnings that keep you in subset
137 bool etsNonsubsetWarnings {}; // Enable only ETS-warnings that do not keep you in subset
138 bool etsHasWarnings = false;
139
140 // Subset ETS-Warnings
141 bool etsProhibitTopLevelStatements {};
142 bool etsBoostEqualityStatement {};
143 bool etsRemoveLambda {};
144 bool etsImplicitBoxingUnboxing {};
145
146 // Non-subset ETS-Warnings
147 bool etsSuggestFinal {};
148 bool etsRemoveAsync {};
149
150 bool etsWerror {}; // Treat all enabled ETS-warnings as errors
151 std::vector<ETSWarnings> etsWarningCollection = {};
152 };
153
154 enum class ErrorType {
155 INVALID,
156 GENERIC,
157 SYNTAX,
158 TYPE,
159 ETS_WARNING,
160 };
161
162 class Error : public std::exception {
163 public:
164 Error() noexcept = default;
Error(ErrorType type,std::string_view file,std::string_view message)165 explicit Error(ErrorType type, std::string_view file, std::string_view message) noexcept
166 : type_(type), file_(file), message_(message)
167 {
168 }
Error(ErrorType type,std::string_view file,std::string_view message,size_t line,size_t column)169 explicit Error(ErrorType type, std::string_view file, std::string_view message, size_t line, size_t column) noexcept
170 : type_(type), file_(file), message_(message), line_(line), col_(column)
171 {
172 }
173 ~Error() override = default;
174 DEFAULT_COPY_SEMANTIC(Error);
175 DEFAULT_MOVE_SEMANTIC(Error);
176
Type()177 ErrorType Type() const noexcept
178 {
179 return type_;
180 }
181
TypeString()182 const char *TypeString() const noexcept
183 {
184 switch (type_) {
185 case ErrorType::SYNTAX:
186 return "SyntaxError";
187 case ErrorType::TYPE:
188 return "TypeError";
189 case ErrorType::ETS_WARNING:
190 return "System ArkTS: warning treated as error.";
191 default:
192 break;
193 }
194
195 return "Error";
196 }
197
what()198 const char *what() const noexcept override
199 {
200 return message_.c_str();
201 }
202
ErrorCode()203 int ErrorCode() const noexcept
204 {
205 return errorCode_;
206 }
207
Message()208 const std::string &Message() const noexcept
209 {
210 return message_;
211 }
212
File()213 const std::string &File() const noexcept
214 {
215 return file_;
216 }
217
Line()218 size_t Line() const
219 {
220 return line_;
221 }
222
Col()223 size_t Col() const
224 {
225 return col_;
226 }
227
228 private:
229 ErrorType type_ {ErrorType::INVALID};
230 std::string file_;
231 std::string message_;
232 size_t line_ {};
233 size_t col_ {};
234 int errorCode_ {1};
235 };
236
237 class Compiler {
238 public:
239 explicit Compiler(ScriptExtension ext);
240 explicit Compiler(ScriptExtension ext, size_t threadCount);
241 explicit Compiler(ScriptExtension ext, size_t threadCount, std::vector<util::Plugin> &&plugins);
242 ~Compiler();
243 NO_COPY_SEMANTIC(Compiler);
244 NO_MOVE_SEMANTIC(Compiler);
245
246 pandasm::Program *Compile(const SourceFile &input, const util::Options &options, uint32_t parseStatus = 0);
247
248 static void DumpAsm(const pandasm::Program *prog);
249
250 // This is used as a _different_ channel of error reporting than GetError().
251 // If this is true, the errors in question have already been reported to the user.
252 bool IsAnyError() const noexcept;
253
GetError()254 const Error &GetError() const noexcept
255 {
256 return error_;
257 }
258
Plugins()259 std::vector<util::Plugin> const &Plugins()
260 {
261 return plugins_;
262 }
263
264 private:
265 std::vector<util::Plugin> const plugins_;
266 compiler::CompilerImpl *compiler_;
267 Error error_;
268 ScriptExtension ext_;
269 };
270 } // namespace ark::es2panda
271
272 #endif
273