• 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 "util/es2pandaMacros.h"
20 #include "util/plugin.h"
21 #include "util/diagnostic.h"
22 #include "generated/options.h"
23 #include "util/language.h"
24 
25 namespace ark::es2panda::ir {
26 class AstNode;
27 class Identifier;
28 class BlockStatement;
29 }  // namespace ark::es2panda::ir
30 
31 namespace ark::pandasm {
32 struct Program;
33 }  // namespace ark::pandasm
34 
35 namespace ark::es2panda {
36 
37 using ETSWarnings = util::gen::ets_warnings::Enum;
38 using EvalMode = util::gen::eval_mode::Enum;
39 using ScriptExtension = util::gen::extension::Enum;
40 
41 constexpr std::string_view ES2PANDA_VERSION = "0.1";
42 constexpr auto COMPILER_SIZE = sizeof(size_t) <= 4 ? 2_GB : 32_GB;
43 
44 namespace util {
45 class Options;
46 class DiagnosticEngine;
47 }  // namespace util
48 namespace parser {
49 class ParserImpl;
50 }  // namespace parser
51 
52 namespace compiler {
53 class CompilerImpl;
54 }  // namespace compiler
55 
56 namespace varbinder {
57 class VarBinder;
58 }  // namespace varbinder
59 
60 enum class CompilationMode {
61     GEN_STD_LIB,
62     PROJECT,
63     SINGLE_FILE,
64     GEN_ABC_FOR_EXTERNAL_SOURCE,
65 };
66 // CC-OFFNXT(G.FUD.06) switch-case, ODR
ToLanguage(ScriptExtension ext)67 inline Language ToLanguage(ScriptExtension ext)
68 {
69     switch (ext) {
70         case ScriptExtension::JS:
71             return Language(Language::Id::JS);
72         case ScriptExtension::TS:
73             return Language(Language::Id::TS);
74         case ScriptExtension::AS:
75             return Language(Language::Id::AS);
76         case ScriptExtension::ETS:
77             return Language(Language::Id::ETS);
78         default:
79             ES2PANDA_UNREACHABLE();
80     }
81 }
82 
83 struct SourceFile {
84     SourceFile(std::string_view fn, std::string_view s);
85     SourceFile(std::string_view fn, std::string_view s, bool m);
86     SourceFile(std::string_view fn, std::string_view s, bool m, std::string_view d);
87     SourceFile(std::string_view fn, std::string_view s, std::string_view rp, bool m, bool d);
88 
89     // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
90     std::string_view filePath {};
91     std::string_view fileFolder {};
92     std::string_view source {};
93     std::string_view resolvedPath {};
94     bool isModule {};
95     // NOTE(dkofanov): Should be aligned with 'Program::moduleInfo_'.
96     bool isDeclForDynamicStaticInterop {};
97     std::string_view dest {};
98     // NOLINTEND(misc-non-private-member-variables-in-classes)
99 };
100 
101 // NOLINTBEGIN(modernize-avoid-c-arrays)
102 inline constexpr char const ERROR_LITERAL[] = "*ERROR_LITERAL*";
103 inline constexpr char const ERROR_TYPE[] = "*ERROR_TYPE*";
104 inline constexpr char const INVALID_EXPRESSION[] = "...";
105 // NOLINTEND(modernize-avoid-c-arrays)
106 
107 class Compiler {
108 public:
109     explicit Compiler(ScriptExtension ext);
110     explicit Compiler(ScriptExtension ext, size_t threadCount);
111     explicit Compiler(ScriptExtension ext, size_t threadCount, std::vector<util::Plugin> &&plugins);
112     ~Compiler();
113     NO_COPY_SEMANTIC(Compiler);
114     NO_MOVE_SEMANTIC(Compiler);
115 
116     pandasm::Program *Compile(const SourceFile &input, const util::Options &options,
117                               util::DiagnosticEngine &diagnosticEngine, uint32_t parseStatus = 0);
118     unsigned int CompileM(std::vector<SourceFile> &inputs, util::Options &options,
119                           util::DiagnosticEngine &diagnosticEngine, std::vector<pandasm::Program *> &result);
120 
121     static void DumpAsm(const pandasm::Program *prog);
122 
GetError()123     const util::ThrowableDiagnostic &GetError() const noexcept
124     {
125         return error_;
126     }
127 
128     std::string GetPhasesList() const;
129 
Plugins()130     std::vector<util::Plugin> const &Plugins()
131     {
132         return plugins_;
133     }
134 
135 private:
136     std::vector<util::Plugin> const plugins_ {};
137     compiler::CompilerImpl *compiler_ {};
138     util::ThrowableDiagnostic error_ {};
139     ScriptExtension ext_ {};
140 };
141 
142 // g_diagnosticEngine used only for flush diagnostic before unexpected process termination:
143 // - inside SIGSEGV handler
144 extern util::DiagnosticEngine *g_diagnosticEngine;
145 }  // namespace ark::es2panda
146 
147 #endif
148