13af6ab5fSopenharmony_ci/**
23af6ab5fSopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ci#include "options.h"
173af6ab5fSopenharmony_ci
183af6ab5fSopenharmony_ci#include "utils/pandargs.h"
193af6ab5fSopenharmony_ci
203af6ab5fSopenharmony_ci#include "arktsconfig.h"
213af6ab5fSopenharmony_ci
223af6ab5fSopenharmony_ci#include <random>
233af6ab5fSopenharmony_ci#include <utility>
243af6ab5fSopenharmony_ci
253af6ab5fSopenharmony_ci#ifdef PANDA_WITH_BYTECODE_OPTIMIZER
263af6ab5fSopenharmony_ci#include "bytecode_optimizer/bytecodeopt_options.h"
273af6ab5fSopenharmony_ci#include "compiler/compiler_options.h"
283af6ab5fSopenharmony_ci#endif
293af6ab5fSopenharmony_ci
303af6ab5fSopenharmony_cinamespace ark::es2panda::util {
313af6ab5fSopenharmony_citemplate <class T>
323af6ab5fSopenharmony_ciT RemoveExtension(T const &filename)
333af6ab5fSopenharmony_ci{
343af6ab5fSopenharmony_ci    typename T::size_type const p(filename.find_last_of('.'));
353af6ab5fSopenharmony_ci    return p > 0 && p != T::npos ? filename.substr(0, p) : filename;
363af6ab5fSopenharmony_ci}
373af6ab5fSopenharmony_ci
383af6ab5fSopenharmony_ci// Options
393af6ab5fSopenharmony_ci
403af6ab5fSopenharmony_ciOptions::Options() : argparser_(new ark::PandArgParser()) {}
413af6ab5fSopenharmony_ci
423af6ab5fSopenharmony_ciOptions::~Options()
433af6ab5fSopenharmony_ci{
443af6ab5fSopenharmony_ci    delete argparser_;
453af6ab5fSopenharmony_ci}
463af6ab5fSopenharmony_ci
473af6ab5fSopenharmony_cistatic std::vector<std::string> SplitToStringVector(std::string const &str)
483af6ab5fSopenharmony_ci{
493af6ab5fSopenharmony_ci    std::vector<std::string> res;
503af6ab5fSopenharmony_ci    std::string_view currStr {str};
513af6ab5fSopenharmony_ci    auto ix = currStr.find(',');
523af6ab5fSopenharmony_ci    while (ix != std::string::npos) {
533af6ab5fSopenharmony_ci        if (ix != 0) {
543af6ab5fSopenharmony_ci            res.emplace_back(currStr.substr(0, ix));
553af6ab5fSopenharmony_ci        }
563af6ab5fSopenharmony_ci        currStr = currStr.substr(ix + 1);
573af6ab5fSopenharmony_ci        ix = currStr.find(',');
583af6ab5fSopenharmony_ci    }
593af6ab5fSopenharmony_ci
603af6ab5fSopenharmony_ci    if (!currStr.empty()) {
613af6ab5fSopenharmony_ci        res.emplace_back(currStr);
623af6ab5fSopenharmony_ci    }
633af6ab5fSopenharmony_ci    return res;
643af6ab5fSopenharmony_ci}
653af6ab5fSopenharmony_ci
663af6ab5fSopenharmony_cistatic std::unordered_set<std::string> SplitToStringSet(std::string const &str)
673af6ab5fSopenharmony_ci{
683af6ab5fSopenharmony_ci    std::vector<std::string> vec = SplitToStringVector(str);
693af6ab5fSopenharmony_ci    std::unordered_set<std::string> res;
703af6ab5fSopenharmony_ci    for (auto &elem : vec) {
713af6ab5fSopenharmony_ci        res.emplace(elem);
723af6ab5fSopenharmony_ci    }
733af6ab5fSopenharmony_ci    return res;
743af6ab5fSopenharmony_ci}
753af6ab5fSopenharmony_ci
763af6ab5fSopenharmony_ci// NOLINTNEXTLINE(modernize-avoid-c-arrays, hicpp-avoid-c-arrays)
773af6ab5fSopenharmony_cistatic void SplitArgs(int argc, const char *argv[], std::vector<std::string> &es2pandaArgs,
783af6ab5fSopenharmony_ci                      std::vector<std::string> &bcoCompilerArgs, std::vector<std::string> &bytecodeoptArgs)
793af6ab5fSopenharmony_ci{
803af6ab5fSopenharmony_ci    constexpr std::string_view COMPILER_PREFIX = "--bco-compiler";
813af6ab5fSopenharmony_ci    constexpr std::string_view OPTIMIZER_PREFIX = "--bco-optimizer";
823af6ab5fSopenharmony_ci
833af6ab5fSopenharmony_ci    enum class OptState { ES2PANDA, JIT_COMPILER, OPTIMIZER };
843af6ab5fSopenharmony_ci    OptState optState = OptState::ES2PANDA;
853af6ab5fSopenharmony_ci
863af6ab5fSopenharmony_ci    std::unordered_map<OptState, std::vector<std::string> *> argsMap = {{OptState::ES2PANDA, &es2pandaArgs},
873af6ab5fSopenharmony_ci                                                                        {OptState::JIT_COMPILER, &bcoCompilerArgs},
883af6ab5fSopenharmony_ci                                                                        {OptState::OPTIMIZER, &bytecodeoptArgs}};
893af6ab5fSopenharmony_ci
903af6ab5fSopenharmony_ci    for (int i = 1; i < argc; i++) {
913af6ab5fSopenharmony_ci        // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
923af6ab5fSopenharmony_ci        const char *argI = argv[i];
933af6ab5fSopenharmony_ci        if (COMPILER_PREFIX == argI) {
943af6ab5fSopenharmony_ci            optState = OptState::JIT_COMPILER;
953af6ab5fSopenharmony_ci            continue;
963af6ab5fSopenharmony_ci        }
973af6ab5fSopenharmony_ci
983af6ab5fSopenharmony_ci        if (OPTIMIZER_PREFIX == argI) {
993af6ab5fSopenharmony_ci            optState = OptState::OPTIMIZER;
1003af6ab5fSopenharmony_ci            continue;
1013af6ab5fSopenharmony_ci        }
1023af6ab5fSopenharmony_ci
1033af6ab5fSopenharmony_ci        argsMap[optState]->emplace_back(argI);
1043af6ab5fSopenharmony_ci        optState = OptState::ES2PANDA;
1053af6ab5fSopenharmony_ci    }
1063af6ab5fSopenharmony_ci}
1073af6ab5fSopenharmony_ci
1083af6ab5fSopenharmony_citemplate <class T>
1093af6ab5fSopenharmony_cistatic bool ParseComponentArgs(const std::vector<std::string> &args, T &options)
1103af6ab5fSopenharmony_ci{
1113af6ab5fSopenharmony_ci    ark::PandArgParser parser;
1123af6ab5fSopenharmony_ci    options.AddOptions(&parser);
1133af6ab5fSopenharmony_ci    if (!parser.Parse(args)) {
1143af6ab5fSopenharmony_ci        std::cerr << parser.GetErrorString();
1153af6ab5fSopenharmony_ci        std::cerr << parser.GetHelpString();
1163af6ab5fSopenharmony_ci        return false;
1173af6ab5fSopenharmony_ci    }
1183af6ab5fSopenharmony_ci
1193af6ab5fSopenharmony_ci    if (auto optionsErr = options.Validate(); optionsErr) {
1203af6ab5fSopenharmony_ci        std::cerr << "Error: " << optionsErr.value().GetMessage() << std::endl;
1213af6ab5fSopenharmony_ci        return false;
1223af6ab5fSopenharmony_ci    }
1233af6ab5fSopenharmony_ci
1243af6ab5fSopenharmony_ci    return true;
1253af6ab5fSopenharmony_ci}
1263af6ab5fSopenharmony_ci
1273af6ab5fSopenharmony_cistatic bool ParseBCOCompilerOptions([[maybe_unused]] const std::vector<std::string> &compilerArgs,
1283af6ab5fSopenharmony_ci                                    [[maybe_unused]] const std::vector<std::string> &bytecodeoptArgs)
1293af6ab5fSopenharmony_ci{
1303af6ab5fSopenharmony_ci#ifdef PANDA_WITH_BYTECODE_OPTIMIZER
1313af6ab5fSopenharmony_ci    if (!ParseComponentArgs(compilerArgs, ark::compiler::g_options)) {
1323af6ab5fSopenharmony_ci        return false;
1333af6ab5fSopenharmony_ci    }
1343af6ab5fSopenharmony_ci    if (!ParseComponentArgs(bytecodeoptArgs, ark::bytecodeopt::g_options)) {
1353af6ab5fSopenharmony_ci        return false;
1363af6ab5fSopenharmony_ci    }
1373af6ab5fSopenharmony_ci#endif
1383af6ab5fSopenharmony_ci
1393af6ab5fSopenharmony_ci    return true;
1403af6ab5fSopenharmony_ci}
1413af6ab5fSopenharmony_ci
1423af6ab5fSopenharmony_cistatic inline bool ETSWarningsGroupSetter(const ark::PandArg<bool> &option)
1433af6ab5fSopenharmony_ci{
1443af6ab5fSopenharmony_ci    return !option.WasSet() || (option.WasSet() && option.GetValue());
1453af6ab5fSopenharmony_ci}
1463af6ab5fSopenharmony_ci
1473af6ab5fSopenharmony_cistatic std::tuple<std::string_view, std::string_view, std::string_view> SplitPath(std::string_view path)
1483af6ab5fSopenharmony_ci{
1493af6ab5fSopenharmony_ci    std::string_view fileDirectory;
1503af6ab5fSopenharmony_ci    std::string_view fileBaseName = path;
1513af6ab5fSopenharmony_ci    auto lastDelimPos = fileBaseName.find_last_of(ark::os::file::File::GetPathDelim());
1523af6ab5fSopenharmony_ci    if (lastDelimPos != std::string_view::npos) {
1533af6ab5fSopenharmony_ci        ++lastDelimPos;
1543af6ab5fSopenharmony_ci        fileDirectory = fileBaseName.substr(0, lastDelimPos);
1553af6ab5fSopenharmony_ci        fileBaseName = fileBaseName.substr(lastDelimPos);
1563af6ab5fSopenharmony_ci    }
1573af6ab5fSopenharmony_ci
1583af6ab5fSopenharmony_ci    // Save all extensions.
1593af6ab5fSopenharmony_ci    std::string_view fileExtensions;
1603af6ab5fSopenharmony_ci    auto fileBaseNamePos = fileBaseName.find_first_of('.');
1613af6ab5fSopenharmony_ci    if (fileBaseNamePos > 0 && fileBaseNamePos != std::string_view::npos) {
1623af6ab5fSopenharmony_ci        fileExtensions = fileBaseName.substr(fileBaseNamePos);
1633af6ab5fSopenharmony_ci        fileBaseName = fileBaseName.substr(0, fileBaseNamePos);
1643af6ab5fSopenharmony_ci    }
1653af6ab5fSopenharmony_ci
1663af6ab5fSopenharmony_ci    return {fileDirectory, fileBaseName, fileExtensions};
1673af6ab5fSopenharmony_ci}
1683af6ab5fSopenharmony_ci
1693af6ab5fSopenharmony_ci/**
1703af6ab5fSopenharmony_ci * @brief Generate evaluated expression wrapping code.
1713af6ab5fSopenharmony_ci * @param sourceFilePath used for generating a unique package name.
1723af6ab5fSopenharmony_ci * @param input expression source code file stream.
1733af6ab5fSopenharmony_ci * @param output stream for generating expression wrapper.
1743af6ab5fSopenharmony_ci */
1753af6ab5fSopenharmony_cistatic void GenerateEvaluationWrapper(std::string_view sourceFilePath, std::ifstream &input, std::stringstream &output)
1763af6ab5fSopenharmony_ci{
1773af6ab5fSopenharmony_ci    static constexpr std::string_view EVAL_PREFIX = "eval_";
1783af6ab5fSopenharmony_ci    static constexpr std::string_view EVAL_SUFFIX = "_eval";
1793af6ab5fSopenharmony_ci
1803af6ab5fSopenharmony_ci    auto splittedPath = SplitPath(sourceFilePath);
1813af6ab5fSopenharmony_ci    auto fileBaseName = std::get<1>(splittedPath);
1823af6ab5fSopenharmony_ci
1833af6ab5fSopenharmony_ci    std::random_device rd;
1843af6ab5fSopenharmony_ci    std::stringstream ss;
1853af6ab5fSopenharmony_ci    ss << EVAL_PREFIX << fileBaseName << '_' << rd() << EVAL_SUFFIX;
1863af6ab5fSopenharmony_ci    auto methodName = ss.str();
1873af6ab5fSopenharmony_ci
1883af6ab5fSopenharmony_ci    output << "package " << methodName << "; class " << methodName << " { private static " << methodName << "() { "
1893af6ab5fSopenharmony_ci           << input.rdbuf() << " } }";
1903af6ab5fSopenharmony_ci}
1913af6ab5fSopenharmony_ci
1923af6ab5fSopenharmony_cistatic auto constexpr DEFAULT_THREAD_COUNT = 0;
1933af6ab5fSopenharmony_ci
1943af6ab5fSopenharmony_cistruct AllArgs {
1953af6ab5fSopenharmony_ci    // NOLINTBEGIN(misc-non-private-member-variables-in-classes)
1963af6ab5fSopenharmony_ci    ark::PandArg<bool> opHelp {"help", false, "Print this message and exit"};
1973af6ab5fSopenharmony_ci    ark::PandArg<bool> opVersion {"version", false, "Print message with version and exit"};
1983af6ab5fSopenharmony_ci
1993af6ab5fSopenharmony_ci    // parser
2003af6ab5fSopenharmony_ci    ark::PandArg<std::string> inputExtension {"extension", "",
2013af6ab5fSopenharmony_ci                                              "Parse the input as the given extension (options: js | ts | as | sts)"};
2023af6ab5fSopenharmony_ci    ark::PandArg<bool> opModule {"module", false, "Parse the input as module (JS only option)"};
2033af6ab5fSopenharmony_ci    ark::PandArg<bool> opParseOnly {"parse-only", false, "Parse the input only"};
2043af6ab5fSopenharmony_ci    ark::PandArg<bool> opDumpAst {"dump-ast", false, "Dump the parsed AST"};
2053af6ab5fSopenharmony_ci    ark::PandArg<bool> opDumpAstOnlySilent {"dump-ast-only-silent", false,
2063af6ab5fSopenharmony_ci                                            "Dump parsed AST with all dumpers available but don't print to stdout"};
2073af6ab5fSopenharmony_ci    ark::PandArg<bool> opDumpCheckedAst {"dump-dynamic-ast", false,
2083af6ab5fSopenharmony_ci                                         "Dump AST with synthetic nodes for dynamic languages"};
2093af6ab5fSopenharmony_ci    ark::PandArg<bool> opListFiles {"list-files", false, "Print names of files that are part of compilation"};
2103af6ab5fSopenharmony_ci
2113af6ab5fSopenharmony_ci    // compiler
2123af6ab5fSopenharmony_ci    ark::PandArg<bool> opDumpAssembly {"dump-assembly", false, "Dump pandasm"};
2133af6ab5fSopenharmony_ci    ark::PandArg<bool> opDebugInfo {"debug-info", false, "Compile with debug info"};
2143af6ab5fSopenharmony_ci    ark::PandArg<bool> opDumpDebugInfo {"dump-debug-info", false, "Dump debug info"};
2153af6ab5fSopenharmony_ci    ark::PandArg<int> opOptLevel {"opt-level", 0, "Compiler optimization level (options: 0 | 1 | 2)", 0, MAX_OPT_LEVEL};
2163af6ab5fSopenharmony_ci    ark::PandArg<bool> opEtsModule {"ets-module", false, "Compile the input as ets-module"};
2173af6ab5fSopenharmony_ci
2183af6ab5fSopenharmony_ci    // ETS-warnings
2193af6ab5fSopenharmony_ci    ark::PandArg<bool> opEtsEnableAll {"ets-warnings-all", false, "Show performance-related ets-warnings"};
2203af6ab5fSopenharmony_ci    ark::PandArg<bool> opEtsWerror {"ets-werror", false, "Treat all enabled performance-related ets-warnings as error"};
2213af6ab5fSopenharmony_ci    ark::PandArg<bool> opEtsSubsetWarnings {"ets-subset-warnings", false, "Show ETS-warnings that keep you in subset"};
2223af6ab5fSopenharmony_ci    ark::PandArg<bool> opEtsNonsubsetWarnings {"ets-nonsubset-warnings", false,
2233af6ab5fSopenharmony_ci                                               "Show ETS-warnings that do not keep you in subset"};
2243af6ab5fSopenharmony_ci    ark::PandArg<bool> opEtsSuggestFinal {"ets-suggest-final", false,
2253af6ab5fSopenharmony_ci                                          "Suggest final keyword warning - ETS non-subset warning"};
2263af6ab5fSopenharmony_ci    ark::PandArg<bool> opEtsProhibitTopLevelStatements {"ets-prohibit-top-level-statements", false,
2273af6ab5fSopenharmony_ci                                                        "Prohibit top-level statements - ETS subset Warning"};
2283af6ab5fSopenharmony_ci    ark::PandArg<bool> opEtsBoostEqualityStatement {"ets-boost-equality-statement", false,
2293af6ab5fSopenharmony_ci                                                    "Suggest boosting Equality Statements - ETS Subset Warning"};
2303af6ab5fSopenharmony_ci    ark::PandArg<bool> opEtsRemoveAsync {
2313af6ab5fSopenharmony_ci        "ets-remove-async", false, "Suggests replacing async functions with coroutines - ETS Non Subset Warnings"};
2323af6ab5fSopenharmony_ci    ark::PandArg<bool> opEtsRemoveLambda {"ets-remove-lambda", false,
2333af6ab5fSopenharmony_ci                                          "Suggestions to replace lambda with regular functions - ETS Subset Warning"};
2343af6ab5fSopenharmony_ci    ark::PandArg<bool> opEtsImplicitBoxingUnboxing {
2353af6ab5fSopenharmony_ci        "ets-implicit-boxing-unboxing", false,
2363af6ab5fSopenharmony_ci        "Check if a program contains implicit boxing or unboxing - ETS Subset Warning"};
2373af6ab5fSopenharmony_ci
2383af6ab5fSopenharmony_ci    ark::PandArg<bool> opDebuggerEvalMode {"debugger-eval-mode", false, "Compile given file in evaluation mode"};
2393af6ab5fSopenharmony_ci    ark::PandArg<uint64_t> opDebuggerEvalLine {
2403af6ab5fSopenharmony_ci        "debugger-eval-line", 0, "Debugger evaluation mode, line in the source file code where evaluate occurs."};
2413af6ab5fSopenharmony_ci    ark::PandArg<std::string> opDebuggerEvalSource {"debugger-eval-source", "",
2423af6ab5fSopenharmony_ci                                                    "Debugger evaluation mode, path to evaluation mode source file"};
2433af6ab5fSopenharmony_ci    ark::PandArg<std::string> opDebuggerEvalPandaFiles {
2443af6ab5fSopenharmony_ci        "debugger-eval-panda-files", "",
2453af6ab5fSopenharmony_ci        "Debugger evaluation mode, paths to evaluation mode (.abc) files, must be accessible"};
2463af6ab5fSopenharmony_ci
2473af6ab5fSopenharmony_ci    ark::PandArg<int> opThreadCount {"thread", DEFAULT_THREAD_COUNT, "Number of worker threads"};
2483af6ab5fSopenharmony_ci    ark::PandArg<bool> opSizeStat {"dump-size-stat", false, "Dump size statistics"};
2493af6ab5fSopenharmony_ci    ark::PandArg<std::string> outputFile {"output", "", "Compiler binary output (.abc)"};
2503af6ab5fSopenharmony_ci    ark::PandArg<std::string> logLevel {"log-level", "error", "Log-level"};
2513af6ab5fSopenharmony_ci    ark::PandArg<std::string> stdLib {"stdlib", "", "Path to standard library"};
2523af6ab5fSopenharmony_ci    ark::PandArg<bool> genStdLib {"gen-stdlib", false, "Gen standard library"};
2533af6ab5fSopenharmony_ci    ark::PandArg<std::string> plugins {"plugins", "", "Plugins"};
2543af6ab5fSopenharmony_ci    ark::PandArg<std::string> skipPhases {"skip-phases", "", "Phases to skip"};
2553af6ab5fSopenharmony_ci    ark::PandArg<std::string> verifierWarnings {
2563af6ab5fSopenharmony_ci        "verifier-warnings", "CheckInfiniteLoopForAll",
2573af6ab5fSopenharmony_ci        "Print errors and continue compilation if AST tree is incorrect. "
2583af6ab5fSopenharmony_ci        "Possible values: "
2593af6ab5fSopenharmony_ci        "NodeHasParentForAll,EveryChildHasValidParentForAll,VariableHasScopeForAll,NodeHasTypeForAll,"
2603af6ab5fSopenharmony_ci        "IdentifierHasVariableForAll,ArithmeticOperationValidForAll,SequenceExpressionHasLastTypeForAll, "
2613af6ab5fSopenharmony_ci        "CheckInfiniteLoopForAll,"
2623af6ab5fSopenharmony_ci        "ForLoopCorrectlyInitializedForAll,VariableHasEnclosingScopeForAll,ModifierAccessValidForAll,"
2633af6ab5fSopenharmony_ci        "ImportExportAccessValid,NodeHasSourceRangeForAll,EveryChildInParentRangeForAll,"
2643af6ab5fSopenharmony_ci        "ReferenceTypeAnnotationIsNullForAll,VariableNameIdentifierNameSameForAll,CheckAbstractMethodForAll,"
2653af6ab5fSopenharmony_ci        "GetterSetterValidationForAll,CheckScopeDeclarationForAll"};
2663af6ab5fSopenharmony_ci    ark::PandArg<std::string> verifierErrors {
2673af6ab5fSopenharmony_ci        "verifier-errors",
2683af6ab5fSopenharmony_ci        "ForLoopCorrectlyInitializedForAll,SequenceExpressionHasLastTypeForAll,NodeHasTypeForAll,NodeHasParentForAll,"
2693af6ab5fSopenharmony_ci        "EveryChildHasValidParentForAll,ModifierAccessValidForAll,ArithmeticOperationValidForAll,"
2703af6ab5fSopenharmony_ci        "VariableHasScopeForAll,IdentifierHasVariableForAll,VariableHasEnclosingScopeForAll,"
2713af6ab5fSopenharmony_ci        "ReferenceTypeAnnotationIsNullForAll,VariableNameIdentifierNameSameForAll,CheckAbstractMethodForAll,"
2723af6ab5fSopenharmony_ci        "GetterSetterValidationForAll,CheckScopeDeclarationForAll",
2733af6ab5fSopenharmony_ci        "Print errors and stop compilation if AST tree is incorrect. "
2743af6ab5fSopenharmony_ci        "Possible values: "
2753af6ab5fSopenharmony_ci        "NodeHasParentForAll,EveryChildHasValidParentForAll,VariableHasScopeForAll,NodeHasTypeForAll,"
2763af6ab5fSopenharmony_ci        "IdentifierHasVariableForAll,ArithmeticOperationValidForAll,SequenceExpressionHasLastTypeForAll,"
2773af6ab5fSopenharmony_ci        "CheckInfiniteLoopForAll,"
2783af6ab5fSopenharmony_ci        "ForLoopCorrectlyInitializedForAll,VariableHasEnclosingScopeForAll,ModifierAccessValidForAll,"
2793af6ab5fSopenharmony_ci        "ImportExportAccessValid,NodeHasSourceRangeForAll,EveryChildInParentRangeForAll,"
2803af6ab5fSopenharmony_ci        "ReferenceTypeAnnotationIsNullForAll,VariableNameIdentifierNameSameForAll,CheckAbstractMethodForAll,"
2813af6ab5fSopenharmony_ci        "GetterSetterValidationForAll,CheckScopeDeclarationForAll"};
2823af6ab5fSopenharmony_ci    ark::PandArg<bool> verifierAllChecks {
2833af6ab5fSopenharmony_ci        "verifier-all-checks", false,
2843af6ab5fSopenharmony_ci        "Run verifier checks on every phase, monotonically expanding them on every phase"};
2853af6ab5fSopenharmony_ci    ark::PandArg<bool> verifierFullProgram {"verifier-full-program", false,
2863af6ab5fSopenharmony_ci                                            "Analyze full program, including program AST and it's dependencies"};
2873af6ab5fSopenharmony_ci    ark::PandArg<std::string> dumpBeforePhases {"dump-before-phases", "",
2883af6ab5fSopenharmony_ci                                                "Generate program dump before running phases in the list"};
2893af6ab5fSopenharmony_ci    ark::PandArg<std::string> dumpEtsSrcBeforePhases {
2903af6ab5fSopenharmony_ci        "dump-ets-src-before-phases", "", "Generate program dump as ets source code before running phases in the list"};
2913af6ab5fSopenharmony_ci    ark::PandArg<std::string> dumpEtsSrcAfterPhases {
2923af6ab5fSopenharmony_ci        "dump-ets-src-after-phases", "", "Generate program dump as ets source code after running phases in the list"};
2933af6ab5fSopenharmony_ci    ark::PandArg<std::string> dumpAfterPhases {"dump-after-phases", "",
2943af6ab5fSopenharmony_ci                                               "Generate program dump after running phases in the list"};
2953af6ab5fSopenharmony_ci    ark::PandArg<bool> opListPhases {"list-phases", false, "Dump list of available phases"};
2963af6ab5fSopenharmony_ci
2973af6ab5fSopenharmony_ci    // tail arguments
2983af6ab5fSopenharmony_ci    ark::PandArg<std::string> inputFile {"input", "", "input file"};
2993af6ab5fSopenharmony_ci
3003af6ab5fSopenharmony_ci    ark::PandArg<std::string> arktsConfig;
3013af6ab5fSopenharmony_ci    // NOLINTEND(misc-non-private-member-variables-in-classes)
3023af6ab5fSopenharmony_ci
3033af6ab5fSopenharmony_ci    explicit AllArgs(const char *argv0)
3043af6ab5fSopenharmony_ci        : arktsConfig {"arktsconfig", ark::es2panda::JoinPaths(ark::es2panda::ParentPath(argv0), "arktsconfig.json"),
3053af6ab5fSopenharmony_ci                       "Path to arkts configuration file"}
3063af6ab5fSopenharmony_ci    {
3073af6ab5fSopenharmony_ci    }
3083af6ab5fSopenharmony_ci
3093af6ab5fSopenharmony_ci    bool ParseInputOutput(CompilationMode compilationMode, std::string &errorMsg, std::string &sourceFile,
3103af6ab5fSopenharmony_ci                          std::string &parserInput, std::string &compilerOutput) const
3113af6ab5fSopenharmony_ci    {
3123af6ab5fSopenharmony_ci        auto isDebuggerEvalMode = opDebuggerEvalMode.GetValue();
3133af6ab5fSopenharmony_ci        if (isDebuggerEvalMode && compilationMode != CompilationMode::SINGLE_FILE) {
3143af6ab5fSopenharmony_ci            errorMsg = "Error: When compiling with --debugger-eval-mode single input file must be provided";
3153af6ab5fSopenharmony_ci            return false;
3163af6ab5fSopenharmony_ci        }
3173af6ab5fSopenharmony_ci
3183af6ab5fSopenharmony_ci        sourceFile = inputFile.GetValue();
3193af6ab5fSopenharmony_ci        if (compilationMode == CompilationMode::SINGLE_FILE) {
3203af6ab5fSopenharmony_ci            std::ifstream inputStream(sourceFile.c_str());
3213af6ab5fSopenharmony_ci            if (inputStream.fail()) {
3223af6ab5fSopenharmony_ci                errorMsg = "Failed to open file: ";
3233af6ab5fSopenharmony_ci                errorMsg.append(sourceFile);
3243af6ab5fSopenharmony_ci                return false;
3253af6ab5fSopenharmony_ci            }
3263af6ab5fSopenharmony_ci
3273af6ab5fSopenharmony_ci            std::stringstream ss;
3283af6ab5fSopenharmony_ci            if (isDebuggerEvalMode) {
3293af6ab5fSopenharmony_ci                GenerateEvaluationWrapper(sourceFile, inputStream, ss);
3303af6ab5fSopenharmony_ci            } else {
3313af6ab5fSopenharmony_ci                ss << inputStream.rdbuf();
3323af6ab5fSopenharmony_ci            }
3333af6ab5fSopenharmony_ci            parserInput = ss.str();
3343af6ab5fSopenharmony_ci        }
3353af6ab5fSopenharmony_ci
3363af6ab5fSopenharmony_ci        if (!outputFile.GetValue().empty()) {
3373af6ab5fSopenharmony_ci            if (compilationMode == CompilationMode::PROJECT) {
3383af6ab5fSopenharmony_ci                errorMsg = "Error: When compiling in project mode --output key is not needed";
3393af6ab5fSopenharmony_ci                return false;
3403af6ab5fSopenharmony_ci            }
3413af6ab5fSopenharmony_ci            compilerOutput = outputFile.GetValue();
3423af6ab5fSopenharmony_ci        } else {
3433af6ab5fSopenharmony_ci            compilerOutput = RemoveExtension(BaseName(sourceFile)).append(".abc");
3443af6ab5fSopenharmony_ci        }
3453af6ab5fSopenharmony_ci
3463af6ab5fSopenharmony_ci        return true;
3473af6ab5fSopenharmony_ci    }
3483af6ab5fSopenharmony_ci
3493af6ab5fSopenharmony_ci    void BindArgs(ark::PandArgParser &argparser)
3503af6ab5fSopenharmony_ci    {
3513af6ab5fSopenharmony_ci        argparser.Add(&opHelp);
3523af6ab5fSopenharmony_ci        argparser.Add(&opVersion);
3533af6ab5fSopenharmony_ci        argparser.Add(&opModule);
3543af6ab5fSopenharmony_ci        argparser.Add(&opDumpAst);
3553af6ab5fSopenharmony_ci        argparser.Add(&opDumpAstOnlySilent);
3563af6ab5fSopenharmony_ci        argparser.Add(&opDumpCheckedAst);
3573af6ab5fSopenharmony_ci        argparser.Add(&opParseOnly);
3583af6ab5fSopenharmony_ci        argparser.Add(&opDumpAssembly);
3593af6ab5fSopenharmony_ci        argparser.Add(&opDebugInfo);
3603af6ab5fSopenharmony_ci        argparser.Add(&opDumpDebugInfo);
3613af6ab5fSopenharmony_ci
3623af6ab5fSopenharmony_ci        argparser.Add(&opOptLevel);
3633af6ab5fSopenharmony_ci        argparser.Add(&opEtsModule);
3643af6ab5fSopenharmony_ci        argparser.Add(&opThreadCount);
3653af6ab5fSopenharmony_ci        argparser.Add(&opSizeStat);
3663af6ab5fSopenharmony_ci        argparser.Add(&opListFiles);
3673af6ab5fSopenharmony_ci
3683af6ab5fSopenharmony_ci        argparser.Add(&inputExtension);
3693af6ab5fSopenharmony_ci        argparser.Add(&outputFile);
3703af6ab5fSopenharmony_ci        argparser.Add(&logLevel);
3713af6ab5fSopenharmony_ci        argparser.Add(&stdLib);
3723af6ab5fSopenharmony_ci        argparser.Add(&genStdLib);
3733af6ab5fSopenharmony_ci        argparser.Add(&plugins);
3743af6ab5fSopenharmony_ci        argparser.Add(&skipPhases);
3753af6ab5fSopenharmony_ci        argparser.Add(&verifierAllChecks);
3763af6ab5fSopenharmony_ci        argparser.Add(&verifierFullProgram);
3773af6ab5fSopenharmony_ci        argparser.Add(&verifierWarnings);
3783af6ab5fSopenharmony_ci        argparser.Add(&verifierErrors);
3793af6ab5fSopenharmony_ci        argparser.Add(&dumpBeforePhases);
3803af6ab5fSopenharmony_ci        argparser.Add(&dumpEtsSrcBeforePhases);
3813af6ab5fSopenharmony_ci        argparser.Add(&dumpAfterPhases);
3823af6ab5fSopenharmony_ci        argparser.Add(&dumpEtsSrcAfterPhases);
3833af6ab5fSopenharmony_ci        argparser.Add(&opListPhases);
3843af6ab5fSopenharmony_ci        argparser.Add(&arktsConfig);
3853af6ab5fSopenharmony_ci
3863af6ab5fSopenharmony_ci        argparser.Add(&opEtsEnableAll);
3873af6ab5fSopenharmony_ci        argparser.Add(&opEtsWerror);
3883af6ab5fSopenharmony_ci        argparser.Add(&opEtsSubsetWarnings);
3893af6ab5fSopenharmony_ci        argparser.Add(&opEtsNonsubsetWarnings);
3903af6ab5fSopenharmony_ci
3913af6ab5fSopenharmony_ci        // ETS-subset warnings
3923af6ab5fSopenharmony_ci        argparser.Add(&opEtsProhibitTopLevelStatements);
3933af6ab5fSopenharmony_ci        argparser.Add(&opEtsBoostEqualityStatement);
3943af6ab5fSopenharmony_ci        argparser.Add(&opEtsRemoveLambda);
3953af6ab5fSopenharmony_ci        argparser.Add(&opEtsImplicitBoxingUnboxing);
3963af6ab5fSopenharmony_ci
3973af6ab5fSopenharmony_ci        // ETS-non-subset warnings
3983af6ab5fSopenharmony_ci        argparser.Add(&opEtsSuggestFinal);
3993af6ab5fSopenharmony_ci        argparser.Add(&opEtsRemoveAsync);
4003af6ab5fSopenharmony_ci
4013af6ab5fSopenharmony_ci        AddDebuggerEvaluationOptions(argparser);
4023af6ab5fSopenharmony_ci
4033af6ab5fSopenharmony_ci        argparser.PushBackTail(&inputFile);
4043af6ab5fSopenharmony_ci        argparser.EnableTail();
4053af6ab5fSopenharmony_ci        argparser.EnableRemainder();
4063af6ab5fSopenharmony_ci    }
4073af6ab5fSopenharmony_ci
4083af6ab5fSopenharmony_ci    void InitCompilerOptions(es2panda::CompilerOptions &compilerOptions, CompilationMode compilationMode) const
4093af6ab5fSopenharmony_ci    {
4103af6ab5fSopenharmony_ci        compilerOptions.dumpAsm = opDumpAssembly.GetValue();
4113af6ab5fSopenharmony_ci        compilerOptions.dumpAst = opDumpAst.GetValue();
4123af6ab5fSopenharmony_ci        compilerOptions.opDumpAstOnlySilent = opDumpAstOnlySilent.GetValue();
4133af6ab5fSopenharmony_ci        compilerOptions.dumpCheckedAst = opDumpCheckedAst.GetValue();
4143af6ab5fSopenharmony_ci        compilerOptions.dumpDebugInfo = opDumpDebugInfo.GetValue();
4153af6ab5fSopenharmony_ci        compilerOptions.isDebug = opDebugInfo.GetValue();
4163af6ab5fSopenharmony_ci        compilerOptions.parseOnly = opParseOnly.GetValue();
4173af6ab5fSopenharmony_ci        compilerOptions.stdLib = stdLib.GetValue();
4183af6ab5fSopenharmony_ci        compilerOptions.isEtsModule = opEtsModule.GetValue();
4193af6ab5fSopenharmony_ci        compilerOptions.plugins = SplitToStringVector(plugins.GetValue());
4203af6ab5fSopenharmony_ci        compilerOptions.skipPhases = SplitToStringSet(skipPhases.GetValue());
4213af6ab5fSopenharmony_ci        compilerOptions.verifierFullProgram = verifierFullProgram.GetValue();
4223af6ab5fSopenharmony_ci        compilerOptions.verifierAllChecks = verifierAllChecks.GetValue();
4233af6ab5fSopenharmony_ci        compilerOptions.verifierWarnings = SplitToStringSet(verifierWarnings.GetValue());
4243af6ab5fSopenharmony_ci        compilerOptions.verifierErrors = SplitToStringSet(verifierErrors.GetValue());
4253af6ab5fSopenharmony_ci        compilerOptions.dumpBeforePhases = SplitToStringSet(dumpBeforePhases.GetValue());
4263af6ab5fSopenharmony_ci        compilerOptions.dumpEtsSrcBeforePhases = SplitToStringSet(dumpEtsSrcBeforePhases.GetValue());
4273af6ab5fSopenharmony_ci        compilerOptions.dumpAfterPhases = SplitToStringSet(dumpAfterPhases.GetValue());
4283af6ab5fSopenharmony_ci        compilerOptions.dumpEtsSrcAfterPhases = SplitToStringSet(dumpEtsSrcAfterPhases.GetValue());
4293af6ab5fSopenharmony_ci
4303af6ab5fSopenharmony_ci        // ETS-Warnings
4313af6ab5fSopenharmony_ci        compilerOptions.etsSubsetWarnings = opEtsSubsetWarnings.GetValue();
4323af6ab5fSopenharmony_ci        compilerOptions.etsWerror = opEtsWerror.GetValue();
4333af6ab5fSopenharmony_ci        compilerOptions.etsNonsubsetWarnings = opEtsNonsubsetWarnings.GetValue();
4343af6ab5fSopenharmony_ci        compilerOptions.etsEnableAll = opEtsEnableAll.GetValue();
4353af6ab5fSopenharmony_ci
4363af6ab5fSopenharmony_ci        if (compilerOptions.etsEnableAll || compilerOptions.etsSubsetWarnings) {
4373af6ab5fSopenharmony_ci            // Adding subset warnings
4383af6ab5fSopenharmony_ci            compilerOptions.etsProhibitTopLevelStatements = ETSWarningsGroupSetter(opEtsProhibitTopLevelStatements);
4393af6ab5fSopenharmony_ci            compilerOptions.etsBoostEqualityStatement = ETSWarningsGroupSetter(opEtsBoostEqualityStatement);
4403af6ab5fSopenharmony_ci            compilerOptions.etsRemoveLambda = ETSWarningsGroupSetter(opEtsRemoveLambda);
4413af6ab5fSopenharmony_ci            compilerOptions.etsImplicitBoxingUnboxing = ETSWarningsGroupSetter(opEtsImplicitBoxingUnboxing);
4423af6ab5fSopenharmony_ci        }
4433af6ab5fSopenharmony_ci
4443af6ab5fSopenharmony_ci        if (compilerOptions.etsEnableAll || compilerOptions.etsNonsubsetWarnings) {
4453af6ab5fSopenharmony_ci            // Adding non-subset warnings
4463af6ab5fSopenharmony_ci            compilerOptions.etsSuggestFinal = ETSWarningsGroupSetter(opEtsSuggestFinal);
4473af6ab5fSopenharmony_ci            compilerOptions.etsRemoveAsync = ETSWarningsGroupSetter(opEtsRemoveAsync);
4483af6ab5fSopenharmony_ci        }
4493af6ab5fSopenharmony_ci
4503af6ab5fSopenharmony_ci        if (!compilerOptions.etsEnableAll && !compilerOptions.etsSubsetWarnings &&
4513af6ab5fSopenharmony_ci            !compilerOptions.etsNonsubsetWarnings) {
4523af6ab5fSopenharmony_ci            // If no warnings groups enabled - check all if enabled
4533af6ab5fSopenharmony_ci            compilerOptions.etsSuggestFinal = opEtsSuggestFinal.GetValue();
4543af6ab5fSopenharmony_ci            compilerOptions.etsProhibitTopLevelStatements = opEtsProhibitTopLevelStatements.GetValue();
4553af6ab5fSopenharmony_ci            compilerOptions.etsBoostEqualityStatement = opEtsBoostEqualityStatement.GetValue();
4563af6ab5fSopenharmony_ci            compilerOptions.etsRemoveAsync = opEtsRemoveAsync.GetValue();
4573af6ab5fSopenharmony_ci            compilerOptions.etsRemoveLambda = opEtsRemoveLambda.GetValue();
4583af6ab5fSopenharmony_ci            compilerOptions.etsImplicitBoxingUnboxing = opEtsImplicitBoxingUnboxing.GetValue();
4593af6ab5fSopenharmony_ci        }
4603af6ab5fSopenharmony_ci
4613af6ab5fSopenharmony_ci        InitDebuggerEvaluationCompilerOptions(compilerOptions);
4623af6ab5fSopenharmony_ci
4633af6ab5fSopenharmony_ci        // Pushing enabled warnings to warning collection
4643af6ab5fSopenharmony_ci        PushingEnabledWarnings(compilerOptions);
4653af6ab5fSopenharmony_ci
4663af6ab5fSopenharmony_ci        compilerOptions.compilationMode = compilationMode;
4673af6ab5fSopenharmony_ci        compilerOptions.arktsConfig = std::make_shared<ark::es2panda::ArkTsConfig>(arktsConfig.GetValue());
4683af6ab5fSopenharmony_ci    }
4693af6ab5fSopenharmony_ci
4703af6ab5fSopenharmony_ciprivate:
4713af6ab5fSopenharmony_ci    static void PushingEnabledWarnings(es2panda::CompilerOptions &compilerOptions)
4723af6ab5fSopenharmony_ci    {
4733af6ab5fSopenharmony_ci        if (compilerOptions.etsSuggestFinal) {
4743af6ab5fSopenharmony_ci            compilerOptions.etsWarningCollection.push_back(ETSWarnings::SUGGEST_FINAL);
4753af6ab5fSopenharmony_ci        }
4763af6ab5fSopenharmony_ci        if (compilerOptions.etsProhibitTopLevelStatements) {
4773af6ab5fSopenharmony_ci            compilerOptions.etsWarningCollection.push_back(ETSWarnings::PROHIBIT_TOP_LEVEL_STATEMENTS);
4783af6ab5fSopenharmony_ci        }
4793af6ab5fSopenharmony_ci        if (compilerOptions.etsBoostEqualityStatement) {
4803af6ab5fSopenharmony_ci            compilerOptions.etsWarningCollection.push_back(ETSWarnings::BOOST_EQUALITY_STATEMENT);
4813af6ab5fSopenharmony_ci        }
4823af6ab5fSopenharmony_ci        if (compilerOptions.etsRemoveAsync) {
4833af6ab5fSopenharmony_ci            compilerOptions.etsWarningCollection.push_back(ETSWarnings::REMOVE_ASYNC_FUNCTIONS);
4843af6ab5fSopenharmony_ci        }
4853af6ab5fSopenharmony_ci        if (compilerOptions.etsRemoveLambda) {
4863af6ab5fSopenharmony_ci            compilerOptions.etsWarningCollection.push_back(ETSWarnings::REMOVE_LAMBDA);
4873af6ab5fSopenharmony_ci        }
4883af6ab5fSopenharmony_ci        if (compilerOptions.etsImplicitBoxingUnboxing) {
4893af6ab5fSopenharmony_ci            compilerOptions.etsWarningCollection.push_back(ETSWarnings::IMPLICIT_BOXING_UNBOXING);
4903af6ab5fSopenharmony_ci        }
4913af6ab5fSopenharmony_ci        if (!compilerOptions.etsWarningCollection.empty()) {
4923af6ab5fSopenharmony_ci            compilerOptions.etsHasWarnings = true;
4933af6ab5fSopenharmony_ci        }
4943af6ab5fSopenharmony_ci    }
4953af6ab5fSopenharmony_ci
4963af6ab5fSopenharmony_ci    void AddDebuggerEvaluationOptions(ark::PandArgParser &argparser)
4973af6ab5fSopenharmony_ci    {
4983af6ab5fSopenharmony_ci        argparser.Add(&opDebuggerEvalMode);
4993af6ab5fSopenharmony_ci        argparser.Add(&opDebuggerEvalLine);
5003af6ab5fSopenharmony_ci        argparser.Add(&opDebuggerEvalSource);
5013af6ab5fSopenharmony_ci        argparser.Add(&opDebuggerEvalPandaFiles);
5023af6ab5fSopenharmony_ci    }
5033af6ab5fSopenharmony_ci
5043af6ab5fSopenharmony_ci    void InitDebuggerEvaluationCompilerOptions(es2panda::CompilerOptions &compilerOptions) const
5053af6ab5fSopenharmony_ci    {
5063af6ab5fSopenharmony_ci        compilerOptions.debuggerEvalMode = opDebuggerEvalMode.GetValue();
5073af6ab5fSopenharmony_ci        if (compilerOptions.debuggerEvalMode) {
5083af6ab5fSopenharmony_ci            compilerOptions.debuggerEvalLine = opDebuggerEvalLine.GetValue();
5093af6ab5fSopenharmony_ci            compilerOptions.debuggerEvalSource = opDebuggerEvalSource.GetValue();
5103af6ab5fSopenharmony_ci            compilerOptions.debuggerEvalPandaFiles = SplitToStringVector(opDebuggerEvalPandaFiles.GetValue());
5113af6ab5fSopenharmony_ci        }
5123af6ab5fSopenharmony_ci    }
5133af6ab5fSopenharmony_ci};
5143af6ab5fSopenharmony_ci
5153af6ab5fSopenharmony_cistatic std::string Usage(const ark::PandArgParser &argparser)
5163af6ab5fSopenharmony_ci{
5173af6ab5fSopenharmony_ci    std::stringstream ss;
5183af6ab5fSopenharmony_ci
5193af6ab5fSopenharmony_ci    ss << argparser.GetErrorString() << std::endl;
5203af6ab5fSopenharmony_ci    ss << "Usage: "
5213af6ab5fSopenharmony_ci       << "es2panda"
5223af6ab5fSopenharmony_ci       << " [OPTIONS] [input file] -- [arguments]" << std::endl;
5233af6ab5fSopenharmony_ci    ss << std::endl;
5243af6ab5fSopenharmony_ci    ss << "optional arguments:" << std::endl;
5253af6ab5fSopenharmony_ci    ss << argparser.GetHelpString() << std::endl;
5263af6ab5fSopenharmony_ci
5273af6ab5fSopenharmony_ci    ss << std::endl;
5283af6ab5fSopenharmony_ci    ss << "--bco-optimizer: Argument directly to bytecode optimizer can be passed after this prefix" << std::endl;
5293af6ab5fSopenharmony_ci    ss << "--bco-compiler: Argument directly to jit-compiler inside bytecode optimizer can be passed after this "
5303af6ab5fSopenharmony_ci          "prefix"
5313af6ab5fSopenharmony_ci       << std::endl;
5323af6ab5fSopenharmony_ci
5333af6ab5fSopenharmony_ci    return ss.str();
5343af6ab5fSopenharmony_ci}
5353af6ab5fSopenharmony_ci
5363af6ab5fSopenharmony_cistatic std::string GetVersion()
5373af6ab5fSopenharmony_ci{
5383af6ab5fSopenharmony_ci    std::stringstream ss;
5393af6ab5fSopenharmony_ci
5403af6ab5fSopenharmony_ci    ss << std::endl;
5413af6ab5fSopenharmony_ci    ss << "  Es2panda Version " << ES2PANDA_VERSION << std::endl;
5423af6ab5fSopenharmony_ci
5433af6ab5fSopenharmony_ci#ifndef PANDA_PRODUCT_BUILD
5443af6ab5fSopenharmony_ci#ifdef ES2PANDA_DATE
5453af6ab5fSopenharmony_ci    ss << std::endl;
5463af6ab5fSopenharmony_ci    ss << "  Build date: ";
5473af6ab5fSopenharmony_ci    ss << ES2PANDA_DATE;
5483af6ab5fSopenharmony_ci#endif  // ES2PANDA_DATE
5493af6ab5fSopenharmony_ci#ifdef ES2PANDA_HASH
5503af6ab5fSopenharmony_ci    ss << std::endl;
5513af6ab5fSopenharmony_ci    ss << "  Last commit hash: ";
5523af6ab5fSopenharmony_ci    ss << ES2PANDA_HASH;
5533af6ab5fSopenharmony_ci    ss << std::endl;
5543af6ab5fSopenharmony_ci#endif  // ES2PANDA_HASH
5553af6ab5fSopenharmony_ci#endif  // PANDA_PRODUCT_BUILD
5563af6ab5fSopenharmony_ci
5573af6ab5fSopenharmony_ci    return ss.str();
5583af6ab5fSopenharmony_ci}
5593af6ab5fSopenharmony_ci
5603af6ab5fSopenharmony_cibool Options::Parse(int argc, const char **argv)
5613af6ab5fSopenharmony_ci{
5623af6ab5fSopenharmony_ci    std::vector<std::string> es2pandaArgs;
5633af6ab5fSopenharmony_ci    std::vector<std::string> bcoCompilerArgs;
5643af6ab5fSopenharmony_ci    std::vector<std::string> bytecodeoptArgs;
5653af6ab5fSopenharmony_ci
5663af6ab5fSopenharmony_ci    SplitArgs(argc, argv, es2pandaArgs, bcoCompilerArgs, bytecodeoptArgs);
5673af6ab5fSopenharmony_ci    if (!ParseBCOCompilerOptions(bcoCompilerArgs, bytecodeoptArgs)) {
5683af6ab5fSopenharmony_ci        return false;
5693af6ab5fSopenharmony_ci    }
5703af6ab5fSopenharmony_ci
5713af6ab5fSopenharmony_ci    AllArgs allArgs(argv[0]);  // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
5723af6ab5fSopenharmony_ci
5733af6ab5fSopenharmony_ci    allArgs.BindArgs(*argparser_);
5743af6ab5fSopenharmony_ci    if (!argparser_->Parse(es2pandaArgs) || allArgs.opHelp.GetValue()) {
5753af6ab5fSopenharmony_ci        errorMsg_ = Usage(*argparser_);
5763af6ab5fSopenharmony_ci        return false;
5773af6ab5fSopenharmony_ci    }
5783af6ab5fSopenharmony_ci
5793af6ab5fSopenharmony_ci    if (allArgs.opVersion.GetValue()) {
5803af6ab5fSopenharmony_ci        errorMsg_ = GetVersion();
5813af6ab5fSopenharmony_ci        return false;
5823af6ab5fSopenharmony_ci    }
5833af6ab5fSopenharmony_ci
5843af6ab5fSopenharmony_ci    // Determine compilation mode
5853af6ab5fSopenharmony_ci    auto compilationMode = DetermineCompilationMode(allArgs.genStdLib, allArgs.inputFile);
5863af6ab5fSopenharmony_ci    if (!allArgs.ParseInputOutput(compilationMode, errorMsg_, sourceFile_, parserInput_, compilerOutput_)) {
5873af6ab5fSopenharmony_ci        return false;
5883af6ab5fSopenharmony_ci    }
5893af6ab5fSopenharmony_ci
5903af6ab5fSopenharmony_ci    // Determine Extension
5913af6ab5fSopenharmony_ci    DetermineExtension(allArgs.inputExtension, allArgs.arktsConfig, compilationMode);
5923af6ab5fSopenharmony_ci    if (extension_ == es2panda::ScriptExtension::INVALID) {
5933af6ab5fSopenharmony_ci        return false;
5943af6ab5fSopenharmony_ci    }
5953af6ab5fSopenharmony_ci
5963af6ab5fSopenharmony_ci    if (extension_ != es2panda::ScriptExtension::JS && allArgs.opModule.GetValue()) {
5973af6ab5fSopenharmony_ci        errorMsg_ = "Error: --module is not supported for this extension.";
5983af6ab5fSopenharmony_ci        return false;
5993af6ab5fSopenharmony_ci    }
6003af6ab5fSopenharmony_ci
6013af6ab5fSopenharmony_ci    // Add Option Flags
6023af6ab5fSopenharmony_ci    AddOptionFlags(allArgs.opParseOnly, allArgs.opModule, allArgs.opSizeStat);
6033af6ab5fSopenharmony_ci
6043af6ab5fSopenharmony_ci    if ((allArgs.dumpEtsSrcBeforePhases.GetValue().size() + allArgs.dumpEtsSrcAfterPhases.GetValue().size() > 0) &&
6053af6ab5fSopenharmony_ci        extension_ != es2panda::ScriptExtension::ETS) {
6063af6ab5fSopenharmony_ci        errorMsg_ = "--dump-ets-src-* option is valid only with ETS extension";
6073af6ab5fSopenharmony_ci        return false;
6083af6ab5fSopenharmony_ci    }
6093af6ab5fSopenharmony_ci
6103af6ab5fSopenharmony_ci    DetermineLogLevel(allArgs.logLevel);
6113af6ab5fSopenharmony_ci    if (logLevel_ == util::LogLevel::INVALID) {
6123af6ab5fSopenharmony_ci        return false;
6133af6ab5fSopenharmony_ci    }
6143af6ab5fSopenharmony_ci
6153af6ab5fSopenharmony_ci    allArgs.InitCompilerOptions(compilerOptions_, compilationMode);
6163af6ab5fSopenharmony_ci    // Some additional checks for ETS extension
6173af6ab5fSopenharmony_ci    if (!CheckEtsSpecificOptions(compilationMode, allArgs.arktsConfig)) {
6183af6ab5fSopenharmony_ci        return false;
6193af6ab5fSopenharmony_ci    }
6203af6ab5fSopenharmony_ci
6213af6ab5fSopenharmony_ci    optLevel_ = allArgs.opOptLevel.GetValue();
6223af6ab5fSopenharmony_ci    threadCount_ = allArgs.opThreadCount.GetValue();
6233af6ab5fSopenharmony_ci    listFiles_ = allArgs.opListFiles.GetValue();
6243af6ab5fSopenharmony_ci    listPhases_ = allArgs.opListPhases.GetValue();
6253af6ab5fSopenharmony_ci
6263af6ab5fSopenharmony_ci    return true;
6273af6ab5fSopenharmony_ci}
6283af6ab5fSopenharmony_ci}  // namespace ark::es2panda::util
629