• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 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/hotfix.h>
22 #include <util/programCache.h>
23 #include <util/symbolTable.h>
24 
25 #include <string>
26 #include <unordered_map>
27 
28 namespace panda::pandasm {
29 struct Program;
30 }  // namespace panda::pandasm
31 
32 namespace panda::es2panda {
33 namespace parser {
34 class ParserImpl;
35 class Transformer;
36 enum class ScriptKind;
37 }  // namespace parser
38 
39 namespace compiler {
40 class CompilerImpl;
41 }  // namespace compiler
42 
43 enum class ScriptExtension {
44     JS,
45     TS,
46     AS,
47 };
48 
49 struct SourceFile {
SourceFileSourceFile50     SourceFile(std::string fn, std::string rn, parser::ScriptKind sk)
51         : fileName(fn), recordName(rn), scriptKind(sk)
52     {
53     }
54 
55     std::string fileName {};
56     std::string recordName {};
57     std::string_view source {};
58     parser::ScriptKind scriptKind {};
59     std::string sourcefile {};
60     std::string pkgName {};
61     uint32_t hash {0};
62 };
63 
64 struct HotfixOptions {
65     std::string dumpSymbolTable {};
66     std::string symbolTable {};
67     bool generatePatch {false};
68     bool hotReload {false};
69 };
70 
71 struct CompilerOptions {
72     bool isDebug {false};
73     bool dumpAst {false};
74     bool dumpAsm {false};
75     bool dumpDebugInfo {false};
76     bool parseOnly {false};
77     bool enableTypeCheck {false};
78     bool dumpLiteralBuffer {false};
79     bool isDebuggerEvaluateExpressionMode {false};
80     bool mergeAbc {false};
81     bool typeExtractor {false};
82     bool typeDtsBuiltin {false};
83     ScriptExtension extension {};
84     int fileThreadCount {0};
85     int functionThreadCount {0};
86     int optLevel {0};
87     std::string output {};
88     std::string debugInfoSourceFile {};
89     std::vector<es2panda::SourceFile> sourceFiles;
90     HotfixOptions hotfixOptions;
91     bool bcVersion {false};
92     bool bcMinVersion {false};
93     std::unordered_map<std::string, std::string> cacheFiles;
94 };
95 
96 enum class ErrorType {
97     GENERIC,
98     SYNTAX,
99     TYPE,
100 };
101 
102 class Error : public std::exception {
103 public:
104     Error() noexcept = default;
Error(ErrorType type,std::string_view message)105     explicit Error(ErrorType type, std::string_view message) noexcept : type_(type), message_(message) {}
Error(ErrorType type,std::string_view message,size_t line,size_t column)106     explicit Error(ErrorType type, std::string_view message, size_t line, size_t column) noexcept
107         : type_(type), message_(message), line_(line), col_(column)
108     {
109     }
110     ~Error() override = default;
111     DEFAULT_COPY_SEMANTIC(Error);
112     DEFAULT_MOVE_SEMANTIC(Error);
113 
Type()114     ErrorType Type() const noexcept
115     {
116         return type_;
117     }
118 
TypeString()119     const char *TypeString() const noexcept
120     {
121         switch (type_) {
122             case ErrorType::SYNTAX:
123                 return "SyntaxError";
124             case ErrorType::TYPE:
125                 return "TypeError";
126             default:
127                 break;
128         }
129 
130         return "Error";
131     }
132 
what()133     const char *what() const noexcept override
134     {
135         return message_.c_str();
136     }
137 
ErrorCode()138     int ErrorCode() const noexcept
139     {
140         return errorCode_;
141     }
142 
Message()143     const std::string &Message() const noexcept
144     {
145         return message_;
146     }
147 
Line()148     size_t Line() const
149     {
150         return line_;
151     }
152 
Col()153     size_t Col() const
154     {
155         return col_;
156     }
157 
158 private:
159     ErrorType type_ {ErrorType::GENERIC};
160     std::string message_;
161     size_t line_ {};
162     size_t col_ {};
163     int errorCode_ {1};
164 };
165 
166 class Compiler {
167 public:
168     explicit Compiler(ScriptExtension ext);
169     explicit Compiler(ScriptExtension ext, size_t threadCount);
170     ~Compiler();
171     NO_COPY_SEMANTIC(Compiler);
172     NO_MOVE_SEMANTIC(Compiler);
173 
174     panda::pandasm::Program *Compile(const SourceFile &input, const CompilerOptions &options,
175         util::SymbolTable *symbolTable = nullptr);
176     panda::pandasm::Program *CompileFile(const CompilerOptions &options, SourceFile *src, util::SymbolTable *symbolTable);
177 
178     static int CompileFiles(CompilerOptions &options,
179         std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo, panda::ArenaAllocator *allocator);
180 
Compile(const SourceFile & input)181     inline panda::pandasm::Program *Compile(const SourceFile &input)
182     {
183         CompilerOptions options;
184 
185         return Compile(input, options);
186     }
187 
188     static void DumpAsm(const panda::pandasm::Program *prog);
189 
GetError()190     const Error &GetError() const noexcept
191     {
192         return error_;
193     }
194 
195 private:
196     util::Hotfix *InitHotfixHelper(const SourceFile &input, const CompilerOptions &options,
197                                    util::SymbolTable *symbolTable);
198     static void CleanHotfixHelper(const util::Hotfix *hotfixHelper);
199 
200     parser::ParserImpl *parser_;
201     compiler::CompilerImpl *compiler_;
202     std::unique_ptr<parser::Transformer> transformer_ {nullptr};
203     Error error_;
204 };
205 }  // namespace panda::es2panda
206 
207 #endif
208