• 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_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::unordered_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::unordered_map<std::string, PkgInfo>> updateVersionInfo;
57 };
58 }  // namespace panda::es2panda
59 
60 namespace panda::es2panda::binder {
61 class Scope;
62 }
63 namespace panda::pandasm {
64 struct Program;
65 }  // namespace panda::pandasm
66 
67 namespace panda::es2panda::lexer {
68 class LineIndex;
69 class SourcePosition;
70 }
71 
72 namespace panda::es2panda::parser {
73 class Program;
74 }
75 
76 namespace panda::es2panda::util {
77 
78 enum class SignedNumberLiteral {
79     UNRECOGNIZED = 0,
80     POSITIVE = 1,
81     NEGATIVE = 2
82 };
83 
84 class FileSuffix {
85 public:
86     static constexpr std::string_view DLL = ".dll";
87     static constexpr std::string_view SO = ".so";
88     static constexpr std::string_view DYLIB = ".dylib";
89 };
90 
91 template<typename T>
92 class SaveValue {
93 public:
SaveValue(T & value)94     explicit SaveValue(T &value) : ptr_(&value), value_(value) {}
95 
~SaveValue()96     ~SaveValue()
97     {
98         *ptr_ = value_;
99     }
100 private:
101     T *ptr_;
102     T value_;
103 };
104 
105 using AopTransformFuncDef = int (*)(const char *);
106 
107 class Helpers {
108 public:
109     Helpers() = delete;
110 
111     static bool IsGlobalIdentifier(const util::StringView &str);
112     static bool ContainSpreadElement(const ArenaVector<ir::Expression *> &args);
113     static util::StringView LiteralToPropName(ArenaAllocator *allocator, const ir::Expression *lit);
114 
115     template <typename T>
116     static bool IsInteger(double number);
117     static bool IsIndex(double number);
118     static int64_t GetIndex(const util::StringView &str);
119 
120     static bool FileExtensionIs(std::string_view filePath, std::string_view extension);
121     static bool EndsWith(std::string_view str, std::string_view suffix);
122     static std::string DoubleToString(double number);
123     static int32_t GetIntegerSignificandBitCount(double number, int32_t &numberBitCount, char *significandArray);
124     static void GetScientificNotationForDouble(double number, uint32_t significandBitCount, int32_t &numberBitCount,
125                                                char *significandArray, char *sciNotationArray, uint32_t size);
126     static std::string ToString(double number);
127     static util::StringView ToStringView(ArenaAllocator *allocator, double number);
128     static util::StringView ToStringView(ArenaAllocator *allocator, int32_t number);
129     static util::StringView ToStringView(ArenaAllocator *allocator, uint32_t number);
130 
131     static const ir::ScriptFunction *GetContainingConstructor(const ir::AstNode *node);
132     static const ir::ScriptFunction *GetContainingConstructor(const ir::ClassProperty *node);
133     static const ir::ScriptFunction *GetContainingFunction(const ir::AstNode *node);
134     static const ir::ClassDefinition *GetClassDefiniton(const ir::ScriptFunction *node);
135     static bool IsSpecialPropertyKey(const ir::Expression *expr);
136     static bool IsConstantPropertyKey(const ir::Expression *expr, bool isComputed);
137     static bool IsConstantExpr(const ir::Expression *expr);
138     static bool IsBindingPattern(const ir::AstNode *node);
139     static bool IsPattern(const ir::AstNode *node);
140     static std::vector<const ir::Identifier *> CollectBindingNames(const ir::AstNode *node);
141     static util::StringView FunctionName(ArenaAllocator *allocator, const ir::ScriptFunction *func);
142     static util::StringView GetName(ArenaAllocator *allocator, const ir::AstNode *node);
143     static std::tuple<util::StringView, bool> ParamName(ArenaAllocator *allocator, const ir::AstNode *param,
144                                                         uint32_t index);
145     static bool IsChild(const ir::AstNode *parent, const ir::AstNode *child);
146     static bool IsChildScope(const binder::Scope *parent, const binder::Scope *child);
147     static bool IsObjectPropertyValue(const ArenaVector<ir::Expression *> &properties, const ir::AstNode *ident);
148     static SignedNumberLiteral GetSignedNumberLiteral(const ir::Expression *expr);
149 
150     static void SetConstantLocalExportSlots(const std::string &record, const std::unordered_set<uint32_t> &slots);
151     static void AnalysisProgram(panda::pandasm::Program *prog, const std::string &inputFile);
152     static void OptimizeProgram(panda::pandasm::Program *prog, const std::string &inputFile);
153     static bool CheckAopTransformPath(const std::string &libPath);
154     static AopTransformFuncDef LoadAopTransformLibFunc(const std::string &libPath,
155         const std::string &funcName, os::library_loader::LibraryHandle &handler);
156     static bool AopTransform(const std::string &inputFile, const std::string &libPath);
157     template <typename T>
158     static T BaseName(T const &path, T const &delims = std::string(panda::os::file::File::GetPathDelim()));
159     static bool ReadFileToBuffer(const std::string &file, std::stringstream &ss);
160     static void ScanDirectives(ir::ScriptFunction *func, const lexer::LineIndex &lineIndex, bool enableSendableClass,
161                                bool enableSendableFunc);
162     static std::string GetHashString(const std::string &str);
163     static std::wstring Utf8ToUtf16(const std::string &utf8);
164     template <typename T, typename... Args>
165     static T FileStream(const std::string &str, Args &&...args);
166     static void ThrowError(ErrorType type, const parser::Program *program, const lexer::SourcePosition &pos,
167         const std::string_view &msg);
168     static bool IsUseShared(const ir::Statement *statement);
169     static const ir::ClassDefinition *GetContainingSendableClass(const ir::AstNode *node);
170     static bool IsSpecialScopeName(const util::StringView &str);
171     static bool BelongingToRecords(const std::string &name, const std::unordered_set<std::string> &retainRecordSet,
172                                    const std::string &delimiter = std::string(DOT));
173     static void RemoveProgramsRedundantData(std::map<std::string, panda::es2panda::util::ProgramCache*> &progsInfo,
174         const std::map<std::string, std::unordered_set<std::string>> &resolveDepsRelation);
175     static bool IsDefaultApiVersion(int apiVersion, std::string subApiVersion);
176     static bool IsSupportLazyImportVersion(int apiVersion, std::string subApiVersion);
177 
178     static const uint32_t MAX_DOUBLE_DIGIT = 310;
179     static const uint32_t MAX_DOUBLE_PRECISION_DIGIT = 17;
180     static const int32_t MAX_DECIMAL_EXPONENT = 21;
181     static const int32_t MIN_DECIMAL_EXPONENT = -6;
182     static const int32_t FAIL_SNPRINTF_S  = -1;
183     static const uint32_t INVALID_INDEX = 4294967295L;
184     static const uint32_t MAX_INT32 = 2147483647;
185     static const uint32_t MAX_INT16 = std::numeric_limits<int16_t>::max();
186     static const uint32_t MAX_INT8 = std::numeric_limits<int8_t>::max();
187     static constexpr std::string_view USE_CONCURRENT = "use concurrent";
188     static constexpr std::string_view USE_SENDABLE = "use sendable";
189     static constexpr std::string_view USE_SHARED = "use shared";
190     static constexpr std::string_view STRING_EMPTY = ""; // Default tag value, or tag of GlobalScope and ModuleScope
191     static constexpr std::string_view CLASS_SCOPE_TAG = "~";
192     static constexpr std::string_view FUNCTION_TAG = "*";
193     static constexpr std::string_view METHOD_TAG = ">";
194     static constexpr std::string_view CTOR_TAG = "=";
195     static constexpr std::string_view NAMESPACE_TAG = "&";
196     static constexpr std::string_view ENUM_TAG = "%";
197     static constexpr std::string_view STATIC_METHOD_TAG = "<";
198     static constexpr std::string_view DUPLICATED_SEPERATOR = "^";
199     static constexpr std::string_view FUNC_NAME_SEPARATOR = "#";
200     static constexpr std::string_view INDEX_NAME_SPICIFIER = "@";
201     static constexpr std::string_view DOT = ".";
202     static constexpr std::string_view BACKSLASH = "\\";
203     static const uint64_t FNV_PRIME = 1099511628211U;
204     static const uint64_t FNV_OFFSET = 14695981039346656037U;
205     static const uint8_t SENDABLE_CLASS_MIN_SUPPORTED_API_VERSION = 11;
206     static const int32_t DEFAULT_TARGET_API_VERSION = 12;
207     static const int32_t ABC_TO_PROGRAM_MIN_SUPPORTED_API_VERSION = 12;
208     static constexpr std::array<uint8_t, panda_file::File::VERSION_SIZE>
209         ABC_TO_PROGRAM_MIN_SUPPORTED_BYTECODE_VERSION {12, 0, 2, 0};
210     static const int32_t SENDABLE_FUNCTION_MIN_SUPPORTED_API_VERSION = 12;
211     static const int32_t LAZY_IMPORT_MIN_SUPPORTED_API_VERSION = 12;
212     static const int32_t SENDABLE_LAZY_LOADING_MIN_SUPPORTED_API_VERSION = 12;
213     static constexpr std::string_view SUB_API_VERSION_1 = "beta1";
214     static constexpr std::string_view SUB_API_VERSION_2 = "beta2";
215     static constexpr std::string_view DEFAULT_SUB_API_VERSION = SUB_API_VERSION_1;
216 
217 private:
218     static bool SetFuncFlagsForDirectives(const ir::StringLiteral *strLit, ir::ScriptFunction *func,
219                                           const lexer::LineIndex &lineIndex, bool enableSendableClass,
220                                           bool enableSendableFunc);
221 };
222 
223 template <typename T>
IsInteger(double number)224 bool Helpers::IsInteger(double number)
225 {
226     if (std::fabs(number) <= static_cast<double>(std::numeric_limits<T>::max())) {
227         T intNum = static_cast<T>(number);
228 
229         if (static_cast<double>(intNum) == number) {
230             return true;
231         }
232     }
233 
234     return false;
235 }
236 
237 template <class T>
BaseName(T const & path,T const & delims)238 T Helpers::BaseName(T const &path, T const &delims)
239 {
240     return path.substr(path.find_last_of(delims) + 1);
241 }
242 
243 template <typename T, typename... Args>
FileStream(const std::string & str,Args &&...args)244 T Helpers::FileStream(const std::string &str, Args &&...args)
245 {
246     T fileStream;
247 #ifdef PANDA_TARGET_WINDOWS
248     std::wstring filename = Helpers::Utf8ToUtf16(str);
249 #else  //for linux and mac
250     std::string filename = str;
251 #endif
252     fileStream.open(filename.c_str(), args...);
253     return fileStream;
254 }
255 
256 }  // namespace panda::es2panda::util
257 
258 #endif
259