• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // CC-OFFNXT(G.FUD.06) switch-case, ODR
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     bool debuggerEvalMode {};
122     uint64_t debuggerEvalLine {};
123     std::string debuggerEvalSource {};
124     std::string stdLib {};
125     std::vector<std::string> plugins {};
126     std::vector<std::string> debuggerEvalPandaFiles {};
127     std::unordered_set<std::string> skipPhases {};
128     std::unordered_set<std::string> verifierWarnings {};
129     std::unordered_set<std::string> verifierErrors {};
130     std::unordered_set<std::string> dumpBeforePhases {};
131     std::unordered_set<std::string> dumpEtsSrcBeforePhases {};
132     std::string exitBeforePhase {};
133     std::unordered_set<std::string> dumpAfterPhases {};
134     std::unordered_set<std::string> dumpEtsSrcAfterPhases {};
135     std::string exitAfterPhase {};
136     std::shared_ptr<ArkTsConfig> arktsConfig {};
137     CompilationMode compilationMode {};
138     // NOLINTEND(misc-non-private-member-variables-in-classes)
139 
140     // ETS Warning Groups
141     bool etsEnableAll {};          // Enable all ETS-warnings for System ArkTS
142     bool etsSubsetWarnings {};     // Enable only ETS-warnings that keep you in subset
143     bool etsNonsubsetWarnings {};  // Enable only ETS-warnings that do not keep you in subset
144     bool etsHasWarnings = false;
145 
146     // Subset ETS-Warnings
147     bool etsProhibitTopLevelStatements {};
148     bool etsBoostEqualityStatement {};
149     bool etsRemoveLambda {};
150     bool etsImplicitBoxingUnboxing {};
151 
152     // Non-subset ETS-Warnings
153     bool etsSuggestFinal {};
154     bool etsRemoveAsync {};
155 
156     bool etsWerror {};  // Treat all enabled ETS-warnings as errors
157     std::vector<ETSWarnings> etsWarningCollection = {};
158 };
159 
160 enum class ErrorType {
161     INVALID,
162     GENERIC,
163     SYNTAX,
164     TYPE,
165     ETS_WARNING,
166 };
167 
168 class Error : public std::exception {
169 public:
170     Error() noexcept = default;
Error(ErrorType type,std::string_view file,std::string_view message)171     explicit Error(ErrorType type, std::string_view file, std::string_view message) noexcept
172         : type_(type), file_(file), message_(message)
173     {
174     }
Error(ErrorType type,std::string_view file,std::string_view message,size_t line,size_t column)175     explicit Error(ErrorType type, std::string_view file, std::string_view message, size_t line, size_t column) noexcept
176         : type_(type), file_(file), message_(message), line_(line), col_(column)
177     {
178     }
179     ~Error() override = default;
180     DEFAULT_COPY_SEMANTIC(Error);
181     DEFAULT_MOVE_SEMANTIC(Error);
182 
Type()183     ErrorType Type() const noexcept
184     {
185         return type_;
186     }
187 
TypeString()188     const char *TypeString() const noexcept
189     {
190         switch (type_) {
191             case ErrorType::SYNTAX:
192                 return "SyntaxError";
193             case ErrorType::TYPE:
194                 return "TypeError";
195             case ErrorType::ETS_WARNING:
196                 return "System ArkTS: warning treated as error.";
197             default:
198                 break;
199         }
200 
201         return "Error";
202     }
203 
what()204     const char *what() const noexcept override
205     {
206         return message_.c_str();
207     }
208 
ErrorCode()209     int ErrorCode() const noexcept
210     {
211         return errorCode_;
212     }
213 
Message()214     const std::string &Message() const noexcept
215     {
216         return message_;
217     }
218 
File()219     const std::string &File() const noexcept
220     {
221         return file_;
222     }
223 
Line()224     size_t Line() const
225     {
226         return line_;
227     }
228 
Col()229     size_t Col() const
230     {
231         return col_;
232     }
233 
234 private:
235     ErrorType type_ {ErrorType::INVALID};
236     std::string file_;
237     std::string message_;
238     size_t line_ {};
239     size_t col_ {};
240     int errorCode_ {1};
241 };
242 
243 class Compiler {
244 public:
245     explicit Compiler(ScriptExtension ext);
246     explicit Compiler(ScriptExtension ext, size_t threadCount);
247     explicit Compiler(ScriptExtension ext, size_t threadCount, std::vector<util::Plugin> &&plugins);
248     ~Compiler();
249     NO_COPY_SEMANTIC(Compiler);
250     NO_MOVE_SEMANTIC(Compiler);
251 
252     pandasm::Program *Compile(const SourceFile &input, const util::Options &options, uint32_t parseStatus = 0);
253 
254     static void DumpAsm(const pandasm::Program *prog);
255 
256     // This is used as a _different_ channel of error reporting than GetError().
257     // If this is true, the errors in question have already been reported to the user.
258     bool IsAnyError() const noexcept;
259 
GetError()260     const Error &GetError() const noexcept
261     {
262         return error_;
263     }
264 
265     std::string GetPhasesList() const;
266 
Plugins()267     std::vector<util::Plugin> const &Plugins()
268     {
269         return plugins_;
270     }
271 
272 private:
273     std::vector<util::Plugin> const plugins_;
274     compiler::CompilerImpl *compiler_;
275     Error error_;
276     ScriptExtension ext_;
277 };
278 }  // namespace ark::es2panda
279 
280 #endif
281