• 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_UTIL_OPTIONS_H
17 #define ES2PANDA_UTIL_OPTIONS_H
18 
19 #include "libpandabase/os/file.h"
20 #include "libpandabase/utils/logger.h"
21 #include "util/helpers.h"
22 #include "utils/pandargs.h"
23 #include "arktsconfig.h"
24 #include "es2panda.h"
25 
26 #include <exception>
27 #include <fstream>
28 #include <iostream>
29 #include <variant>
30 
31 namespace ark::es2panda::util {
32 
33 namespace eval_mode = gen::eval_mode;
34 namespace extension = gen::extension;
35 namespace ets_warnings = gen::ets_warnings;
36 
37 template <class T>
BaseName(T const & path)38 T BaseName(T const &path)
39 {
40     return path.substr(path.find_last_of(ark::os::file::File::GetPathDelim()) + 1);
41 }
42 
43 class Options : public gen::Options {
44 public:
Options(std::string_view execPath,util::DiagnosticEngine & diagnosticEngine)45     explicit Options(std::string_view execPath, util::DiagnosticEngine &diagnosticEngine)
46         : gen::Options(std::string(execPath)), diagnosticEngine_(diagnosticEngine)
47     {
48     }
49     NO_COPY_SEMANTIC(Options);
50     NO_MOVE_SEMANTIC(Options);
51     ~Options() = default;
52 
53     bool Parse(Span<const char *const> args);
54 
GetExtension()55     ScriptExtension GetExtension() const
56     {
57         return extension_;
58     }
59 
60     // NOTE(dkofanov): Replace this getter with something that does 'std::move(parserInputContents_)' as currently it
61     // encourages copying of 'parserInputContents_' data.
CStrParserInputContents()62     std::pair<const char *, size_t> CStrParserInputContents() const
63     {
64         return std::pair<const char *, size_t> {parserInputContents_.c_str(), parserInputContents_.size()};
65     }
66 
ArkTSConfig()67     const std::shared_ptr<ArkTsConfig> &ArkTSConfig() const
68     {
69         return arktsConfig_;
70     }
71 
LogLevel()72     Logger::Level LogLevel() const
73     {
74         return logLevel_;
75     }
76 
DetermineCompilationMode()77     void DetermineCompilationMode()
78     {
79         compilationMode_ = IsGenStdlib()         ? CompilationMode::GEN_STD_LIB
80                            : IsSimultaneous()    ? CompilationMode::GEN_ABC_FOR_EXTERNAL_SOURCE
81                            : inputFile_.WasSet() ? CompilationMode::SINGLE_FILE
82                                                  : CompilationMode::PROJECT;
83     }
84 
GetCompilationMode()85     CompilationMode GetCompilationMode() const
86     {
87         return compilationMode_;
88     }
89 
SetEvalMode(std::string_view evalModeStr)90     void SetEvalMode(std::string_view evalModeStr)
91     {
92         ES2PANDA_ASSERT(eval_mode::FromString(evalModeStr) != eval_mode::INVALID);
93         gen::Options::SetEvalMode(std::string(evalModeStr));
94         evalMode_ = eval_mode::FromString(evalModeStr);
95     }
IsEval()96     bool IsEval() const
97     {
98         return evalMode_ != eval_mode::NONE;
99     }
IsDirectEval()100     bool IsDirectEval() const
101     {
102         return evalMode_ == eval_mode::DIRECT;
103     }
IsFunctionEval()104     bool IsFunctionEval() const
105     {
106         return evalMode_ == eval_mode::FUNCTION;
107     }
108 
SourceFileName()109     std::string SourceFileName() const
110     {
111         return inputFile_.GetValue();
112     }
113 
IsDynamic()114     bool IsDynamic() const
115     {
116         return extension_ != ScriptExtension::ETS;
117     }
118 
GetAstVerifierWarnings()119     const std::array<bool, gen::ast_verifier::COUNT> &GetAstVerifierWarnings() const
120     {
121         return verifierWarnings_;
122     }
GetAstVerifierErrors()123     const std::array<bool, gen::ast_verifier::COUNT> &GetAstVerifierErrors() const
124     {
125         return verifierErrors_;
126     }
GetSkipPhases()127     const std::set<std::string> &GetSkipPhases() const
128     {
129         return skipPhases_;
130     }
GetDumpBeforePhases()131     const std::set<std::string> &GetDumpBeforePhases() const
132     {
133         return dumpBeforePhases_;
134     }
GetDumpEtsSrcBeforePhases()135     const std::set<std::string> &GetDumpEtsSrcBeforePhases() const
136     {
137         return dumpEtsSrcBeforePhases_;
138     }
GetDumpAfterPhases()139     const std::set<std::string> &GetDumpAfterPhases() const
140     {
141         return dumpAfterPhases_;
142     }
GetDumpEtsSrcAfterPhases()143     const std::set<std::string> &GetDumpEtsSrcAfterPhases() const
144     {
145         return dumpEtsSrcAfterPhases_;
146     }
147 
GetEtsWarningCollection()148     const std::vector<ETSWarnings> &GetEtsWarningCollection() const
149     {
150         return etsWarningCollection_;
151     }
152 
IsAstVerifierBeforePhases()153     bool IsAstVerifierBeforePhases() const
154     {
155         return astVerifierBeforePhases_ || astVerifierEachPhase_;
156     }
157 
IsAstVerifierEachPhase()158     bool IsAstVerifierEachPhase() const
159     {
160         return astVerifierEachPhase_;
161     }
162 
IsAstVerifierAfterPhases()163     bool IsAstVerifierAfterPhases() const
164     {
165         return astVerifierAfterPhases_ || astVerifierEachPhase_;
166     }
167 
HasVerifierPhase(std::string_view phase)168     bool HasVerifierPhase(std::string_view phase) const
169     {
170         return astVerifierEachPhase_ || astVerifierPhases_.find(std::string(phase)) != astVerifierPhases_.end();
171     }
172 
IsEnableJsdocParse()173     bool IsEnableJsdocParse() const
174     {
175         return parseJsdoc_;
176     }
177 
178 private:
179     template <typename T>
180     static bool CallPandArgParser(const std::vector<std::string> &args, T &options,
181                                   util::DiagnosticEngine &diagnosticEngine);
182     bool CallPandArgParser(const std::vector<std::string> &args);
183     bool ParseInputOutput();
184     bool DetermineExtension();
185     bool ProcessEtsSpecificOptions();
186     std::optional<ArkTsConfig> ParseArktsConfig();
187     void InitCompilerOptions();
188     void InitAstVerifierOptions();
189     void InitializeWarnings();
190 
191 private:
192     ark::PandArg<std::string> inputFile_ {"input", "", "input file"};
193     std::shared_ptr<ArkTsConfig> arktsConfig_ {};
194     ScriptExtension extension_ {ScriptExtension::INVALID};
195     CompilationMode compilationMode_ {};
196     std::set<std::string> skipPhases_ {};
197     std::array<bool, gen::ast_verifier::COUNT> verifierWarnings_ {};
198     std::array<bool, gen::ast_verifier::COUNT> verifierErrors_ {};
199     bool astVerifierBeforePhases_ {};
200     bool astVerifierEachPhase_ {};
201     bool astVerifierAfterPhases_ {};
202     std::set<std::string> astVerifierPhases_ {};
203     std::set<std::string> dumpBeforePhases_ {};
204     std::set<std::string> dumpEtsSrcBeforePhases_ {};
205     std::set<std::string> dumpAfterPhases_ {};
206     std::set<std::string> dumpEtsSrcAfterPhases_ {};
207     std::vector<ETSWarnings> etsWarningCollection_ = {};
208     std::string parserInputContents_;
209     Logger::Level logLevel_ {Logger::Level::ERROR};
210     EvalMode evalMode_ = {EvalMode::NONE};
211     util::DiagnosticEngine &diagnosticEngine_;
212     bool parseJsdoc_ {};
213 };
214 }  // namespace ark::es2panda::util
215 
216 #endif  // UTIL_OPTIONS_H
217