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_HELPERS_H
17 #define ES2PANDA_UTIL_HELPERS_H
18
19 #include <cmath>
20
21 #include <mem/arena_allocator.h>
22 #include <os/file.h>
23 #include <os/library_loader.h>
24
25 #include <binder/variableFlags.h>
26 #include <programCache.h>
27 #include <util/ustring.h>
28
29 namespace panda::es2panda::ir {
30 class Expression;
31 class ScriptFunction;
32 class ClassDefinition;
33 class ClassProperty;
34 class Identifier;
35 class AstNode;
36 class ObjectExpression;
37 class StringLiteral;
38 class Statement;
39 } // namespace panda::es2panda::ir
40
41 namespace panda::es2panda {
42 struct CompilerOptions;
43 enum class ErrorType;
44
45 struct PkgInfo {
46 std::string packageName {};
47 std::string version {};
48 };
49
50 struct CompileContextInfo {
51 std::vector<std::string> compileEntries;
52 std::set<std::string> externalPkgNames;
53 std::map<std::string, PkgInfo> pkgContextInfo;
54 // The key of updateVersionInfo is the package name for an abc file, and the value contains the name of its
55 // dependent pacakge and corresponding package version which need to update version.
56 std::unordered_map<std::string, std::map<std::string, PkgInfo>> updateVersionInfo;
57 /**
58 * When there is an abc file as input and needModifyRecord is true, it is necessary to modify the recordName
59 * in abc2program and modify the ohmurl for dynamic and static imports.
60 **/
61 bool needModifyRecord {false};
62 std::string bundleName {};
63 };
64 } // namespace panda::es2panda
65
66 namespace panda::es2panda::binder {
67 class Scope;
68 }
69 namespace panda::pandasm {
70 struct Program;
71 } // namespace panda::pandasm
72
73 namespace panda::es2panda::lexer {
74 class LineIndex;
75 class SourcePosition;
76 }
77
78 namespace panda::es2panda::parser {
79 class Program;
80 }
81
82 namespace panda::es2panda::util {
83
84 enum class SignedNumberLiteral {
85 UNRECOGNIZED = 0,
86 POSITIVE = 1,
87 NEGATIVE = 2
88 };
89
90 class FileSuffix {
91 public:
92 static constexpr std::string_view DLL = ".dll";
93 static constexpr std::string_view SO = ".so";
94 static constexpr std::string_view DYLIB = ".dylib";
95 };
96
97 template<typename T>
98 class SaveValue {
99 public:
SaveValue(T & value)100 explicit SaveValue(T &value) : ptr_(&value), value_(value) {}
101
~SaveValue()102 ~SaveValue()
103 {
104 *ptr_ = value_;
105 }
106 private:
107 T *ptr_;
108 T value_;
109 };
110
111 using AopTransformFuncDef = int (*)(const char *);
112
113 struct DirectiveScanConfig {
114 const lexer::LineIndex &lineIndex;
115 bool enableSendableClass;
116 bool enableSendableFunc;
117 bool enableEtsImplements;
118 };
119
120 class Helpers {
121 public:
122 Helpers() = delete;
123
124 static bool IsGlobalIdentifier(const util::StringView &str);
125 static bool ContainSpreadElement(const ArenaVector<ir::Expression *> &args);
126 static util::StringView LiteralToPropName(ArenaAllocator *allocator, const ir::Expression *lit);
127
128 template <typename T>
129 static bool IsInteger(double number);
130 static bool IsIndex(double number);
131 static int64_t GetIndex(const util::StringView &str);
132
133 static bool FileExtensionIs(std::string_view filePath, std::string_view extension);
134 static bool EndsWith(std::string_view str, std::string_view suffix);
135 static std::string DoubleToString(double number);
136 static int32_t GetIntegerSignificandBitCount(double number, int32_t &numberBitCount, char *significandArray);
137 static void GetScientificNotationForDouble(double number, uint32_t significandBitCount, int32_t &numberBitCount,
138 char *significandArray, char *sciNotationArray, uint32_t size);
139 static std::string ToString(double number);
140 static util::StringView ToStringView(ArenaAllocator *allocator, double number);
141 static util::StringView ToStringView(ArenaAllocator *allocator, int32_t number);
142 static util::StringView ToStringView(ArenaAllocator *allocator, uint32_t number);
143
144 static const ir::ScriptFunction *GetContainingConstructor(const ir::AstNode *node);
145 static const ir::ScriptFunction *GetContainingConstructor(const ir::ClassProperty *node);
146 static const ir::ScriptFunction *GetContainingFunction(const ir::AstNode *node);
147 static const ir::ClassDefinition *GetClassDefiniton(const ir::ScriptFunction *node);
148 static bool IsSpecialPropertyKey(const ir::Expression *expr);
149 static bool IsConstantPropertyKey(const ir::Expression *expr, bool isComputed);
150 static bool IsConstantExpr(const ir::Expression *expr);
151 static bool IsBindingPattern(const ir::AstNode *node);
152 static bool IsPattern(const ir::AstNode *node);
153 static std::vector<const ir::Identifier *> CollectBindingNames(const ir::AstNode *node);
154 static util::StringView FunctionName(ArenaAllocator *allocator, const ir::ScriptFunction *func);
155 static util::StringView GetName(ArenaAllocator *allocator, const ir::AstNode *node);
156 static std::tuple<util::StringView, bool> ParamName(ArenaAllocator *allocator, const ir::AstNode *param,
157 uint32_t index);
158 static bool IsChild(const ir::AstNode *parent, const ir::AstNode *child);
159 static bool IsChildScope(const binder::Scope *parent, const binder::Scope *child);
160 static bool IsObjectPropertyValue(const ArenaVector<ir::Expression *> &properties, const ir::AstNode *ident);
161 static SignedNumberLiteral GetSignedNumberLiteral(const ir::Expression *expr);
162
163 static void SetConstantLocalExportSlots(const std::string &record, const std::unordered_set<uint32_t> &slots);
164 static void AnalysisProgram(panda::pandasm::Program *prog, const std::string &inputFile);
165 static void OptimizeProgram(panda::pandasm::Program *prog, const std::string &inputFile);
166 static bool CheckAopTransformPath(const std::string &libPath);
167 static AopTransformFuncDef LoadAopTransformLibFunc(const std::string &libPath,
168 const std::string &funcName, os::library_loader::LibraryHandle &handler);
169 static bool AopTransform(const std::string &inputFile, const std::string &libPath);
170 template <typename T>
171 static T BaseName(T const &path, T const &delims = std::string(panda::os::file::File::GetPathDelim()));
172 static bool ReadFileToBuffer(const std::string &file, std::stringstream &ss);
173 static void ScanDirectives(ir::ScriptFunction *func, const DirectiveScanConfig &scanConfig);
174 static std::string GetHashString(const std::string &str);
175 static std::wstring Utf8ToUtf16(const std::string &utf8);
176 template <typename T, typename... Args>
177 static T FileStream(const std::string &str, Args &&...args);
178 static void ThrowError(ErrorType type, const parser::Program *program, const lexer::SourcePosition &pos,
179 const std::string_view &msg);
180 static bool IsUseShared(const ir::Statement *statement);
181 static const ir::ClassDefinition *GetContainingSendableClass(const ir::AstNode *node);
182 static bool IsSpecialScopeName(const util::StringView &str);
183 static bool BelongingToRecords(const std::string &name, const std::unordered_set<std::string> &retainRecordSet,
184 const std::string &delimiter = std::string(DOT));
185 static bool IsInnerAnnotationRecordName(const std::string_view &name);
186 static size_t RemoveRecordSuffixAnnotationName(std::string &name);
187 static void RemoveProgramsRedundantData(std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
188 const std::map<std::string, std::unordered_set<std::string>> &resolveDepsRelation);
189 static bool IsDefaultApiVersion(int apiVersion, std::string subApiVersion);
190 static bool IsSupportLazyImportVersion(int apiVersion, std::string subApiVersion);
191 static bool IsSupportLazyImportDefaultVersion(int apiVersion);
192 static bool IsSupportEtsImplementsVersion(int apiVersion);
193 static bool IsEnableExpectedPropertyCountApiVersion(int apiVersion);
194 static bool IsSupportAnnotationVersion(int apiVersion);
195
196 static const uint32_t MAX_DOUBLE_DIGIT = 310;
197 static const uint32_t MAX_DOUBLE_PRECISION_DIGIT = 17;
198 static const int32_t MAX_DECIMAL_EXPONENT = 21;
199 static const int32_t MIN_DECIMAL_EXPONENT = -6;
200 static const int32_t FAIL_SNPRINTF_S = -1;
201 static const uint32_t INVALID_INDEX = 4294967295L;
202 static const uint32_t MAX_INT32 = 2147483647;
203 static const uint32_t MAX_INT16 = std::numeric_limits<int16_t>::max();
204 static const uint32_t MAX_INT8 = std::numeric_limits<int8_t>::max();
205 static constexpr std::string_view USE_CONCURRENT = "use concurrent";
206 static constexpr std::string_view USE_SENDABLE = "use sendable";
207 static constexpr std::string_view USE_SHARED = "use shared";
208 static constexpr std::string_view IMPLEMENTS_STATIC = "implements static:";
209 static constexpr std::string_view STRING_EMPTY = ""; // Default tag value, or tag of GlobalScope and ModuleScope
210 static constexpr std::string_view CLASS_SCOPE_TAG = "~";
211 static constexpr std::string_view FUNCTION_TAG = "*";
212 static constexpr std::string_view METHOD_TAG = ">";
213 static constexpr std::string_view CTOR_TAG = "=";
214 static constexpr std::string_view NAMESPACE_TAG = "&";
215 static constexpr std::string_view ENUM_TAG = "%";
216 static constexpr std::string_view STATIC_METHOD_TAG = "<";
217 static constexpr std::string_view DUPLICATED_SEPERATOR = "^";
218 static constexpr std::string_view FUNC_NAME_SEPARATOR = "#";
219 static constexpr std::string_view INDEX_NAME_SPICIFIER = "@";
220 static constexpr std::string_view DOT = ".";
221 static constexpr std::string_view BACKSLASH = "\\";
222 static const uint64_t FNV_PRIME = 1099511628211U;
223 static const uint64_t FNV_OFFSET = 14695981039346656037U;
224 static const uint8_t SENDABLE_CLASS_MIN_SUPPORTED_API_VERSION = 11;
225 static const int32_t DEFAULT_TARGET_API_VERSION = 12;
226 static const int32_t ABC_TO_PROGRAM_MIN_SUPPORTED_API_VERSION = 12;
227 static constexpr std::array<uint8_t, panda_file::File::VERSION_SIZE>
228 ABC_TO_PROGRAM_MIN_SUPPORTED_BYTECODE_VERSION {12, 0, 2, 0};
229 static const int32_t SENDABLE_FUNCTION_MIN_SUPPORTED_API_VERSION = 12;
230 static const int32_t LAZY_IMPORT_MIN_SUPPORTED_API_VERSION = 12;
231 static const int32_t SENDABLE_LAZY_LOADING_MIN_SUPPORTED_API_VERSION = 12;
232 static const int8_t SUPER_CALL_OPT_MIN_SUPPORTED_API_VERSION = 18;
233 static const int8_t SENDABLE_CLASS_USING_LOCAL_MODULE_VAR_MIN_SUPPORTED_API_VERSION = 18;
234 static const int8_t ENABLE_EXPECTED_PROPERTY_COUNT_MIN_SUPPORTED_API_VERSION = 18;
235 static const int8_t ANNOTATION_SUPPORTED_API_VERSION = 20;
236 static constexpr std::string_view SUB_API_VERSION_1 = "beta1";
237 static constexpr std::string_view SUB_API_VERSION_2 = "beta2";
238 static constexpr std::string_view DEFAULT_SUB_API_VERSION = SUB_API_VERSION_1;
239
240 private:
241 static bool SetFuncFlagsForDirectives(const ir::StringLiteral *strLit, ir::ScriptFunction *func,
242 const DirectiveScanConfig &scanConfig);
243 };
244
245 template <typename T>
IsInteger(double number)246 bool Helpers::IsInteger(double number)
247 {
248 if (std::fabs(number) <= static_cast<double>(std::numeric_limits<T>::max())) {
249 T intNum = static_cast<T>(number);
250
251 if (static_cast<double>(intNum) == number) {
252 return true;
253 }
254 }
255
256 return false;
257 }
258
259 template <class T>
BaseName(T const & path,T const & delims)260 T Helpers::BaseName(T const &path, T const &delims)
261 {
262 return path.substr(path.find_last_of(delims) + 1);
263 }
264
265 template <typename T, typename... Args>
FileStream(const std::string & str,Args &&...args)266 T Helpers::FileStream(const std::string &str, Args &&...args)
267 {
268 T fileStream;
269 #ifdef PANDA_TARGET_WINDOWS
270 std::wstring filename = Helpers::Utf8ToUtf16(str);
271 #else //for linux and mac
272 std::string filename = str;
273 #endif
274 fileStream.open(filename.c_str(), args...);
275 return fileStream;
276 }
277
278 } // namespace panda::es2panda::util
279
280 #endif
281