1 /** 2 * Copyright (c) 2021-2022 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_AOT_OPTIONS_H 17 #define ES2PANDA_AOT_OPTIONS_H 18 19 #include <es2panda.h> 20 #include <macros.h> 21 #include <parser/program/program.h> 22 23 #include <exception> 24 #include <fstream> 25 #include <iostream> 26 #include <util/base64.h> 27 28 namespace panda { 29 class PandArgParser; 30 class PandaArg; 31 } // namespace panda 32 33 namespace panda::es2panda::aot { 34 enum class OptionFlags { 35 DEFAULT = 0, 36 PARSE_ONLY = 1 << 1, 37 SIZE_STAT = 1 << 2, 38 }; 39 40 inline std::underlying_type_t<OptionFlags> operator&(OptionFlags a, OptionFlags b) 41 { 42 using utype = std::underlying_type_t<OptionFlags>; 43 /* NOLINTNEXTLINE(hicpp-signed-bitwise) */ 44 return static_cast<utype>(static_cast<utype>(a) & static_cast<utype>(b)); 45 } 46 47 inline OptionFlags &operator|=(OptionFlags &a, OptionFlags b) 48 { 49 using utype = std::underlying_type_t<OptionFlags>; 50 /* NOLINTNEXTLINE(hicpp-signed-bitwise) */ 51 return a = static_cast<OptionFlags>(static_cast<utype>(a) | static_cast<utype>(b)); 52 } 53 54 class Options { 55 public: 56 Options(); 57 NO_COPY_SEMANTIC(Options); 58 NO_MOVE_SEMANTIC(Options); 59 ~Options(); 60 61 bool Parse(int argc, const char **argv); 62 Extension()63 es2panda::ScriptExtension Extension() const 64 { 65 return extension_; 66 } 67 CompilerOptions()68 const es2panda::CompilerOptions &CompilerOptions() const 69 { 70 return compilerOptions_; 71 } 72 CompilerOptions()73 es2panda::CompilerOptions &CompilerOptions() 74 { 75 return compilerOptions_; 76 } 77 ScriptKind()78 es2panda::parser::ScriptKind ScriptKind() const 79 { 80 return scriptKind_; 81 } 82 CompilerOutput()83 const std::string &CompilerOutput() const 84 { 85 return compilerOutput_; 86 } 87 SourceFile()88 const std::string &SourceFile() const 89 { 90 return sourceFile_; 91 } 92 RecordName()93 const std::string &RecordName() const 94 { 95 return recordName_; 96 } 97 ErrorMsg()98 const std::string &ErrorMsg() const 99 { 100 return errorMsg_; 101 } 102 OptLevel()103 int OptLevel() const 104 { 105 return optLevel_; 106 } 107 ParseOnly()108 bool ParseOnly() const 109 { 110 return (options_ & OptionFlags::PARSE_ONLY) != 0; 111 } 112 SizeStat()113 bool SizeStat() const 114 { 115 return (options_ & OptionFlags::SIZE_STAT) != 0; 116 } 117 118 std::string ExtractContentFromBase64Input(const std::string &inputBase64String); 119 compilerProtoOutput()120 const std::string &compilerProtoOutput() const 121 { 122 return compilerProtoOutput_; 123 } 124 NpmModuleEntryList()125 const std::string &NpmModuleEntryList() const 126 { 127 return npmModuleEntryList_; 128 } 129 OutputFiles()130 const std::unordered_map<std::string, std::string> &OutputFiles() const 131 { 132 return outputFiles_; 133 } 134 135 bool CollectInputFilesFromFileList(const std::string &input); 136 bool CollectInputFilesFromFileDirectory(const std::string &input, const std::string &extension); 137 void ParseCacheFileOption(const std::string &cacheInput); 138 139 private: 140 es2panda::ScriptExtension extension_ {es2panda::ScriptExtension::JS}; 141 es2panda::CompilerOptions compilerOptions_ {}; 142 es2panda::parser::ScriptKind scriptKind_ {es2panda::parser::ScriptKind::SCRIPT}; 143 OptionFlags options_ {OptionFlags::DEFAULT}; 144 panda::PandArgParser *argparser_; 145 std::string base64Input_; 146 std::string compilerOutput_; 147 std::string result_; 148 std::string sourceFile_; 149 std::string recordName_; 150 std::string errorMsg_; 151 std::string compilerProtoOutput_; 152 int optLevel_ {0}; 153 int functionThreadCount_ {0}; 154 int fileThreadCount_ {0}; 155 std::string npmModuleEntryList_; 156 std::vector<es2panda::SourceFile> sourceFiles_; 157 std::unordered_map<std::string, std::string> outputFiles_; 158 }; 159 } // namespace panda::es2panda::aot 160 161 #endif // AOT_OPTIONS_H 162