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 <binder/variableFlags.h>
20 #include <mem/arena_allocator.h>
21 #include <os/file.h>
22 #include <util/ustring.h>
23
24 #include <cmath>
25
26 namespace panda::es2panda::ir {
27 class Expression;
28 class ScriptFunction;
29 class ClassDefinition;
30 class ClassProperty;
31 class Identifier;
32 class AstNode;
33 class ObjectExpression;
34 class StringLiteral;
35 } // namespace panda::es2panda::ir
36
37 namespace panda::es2panda {
38 struct CompilerOptions;
39 } // namespace panda::es2panda
40
41 namespace panda::es2panda::binder {
42 class Scope;
43 }
44 namespace panda::pandasm {
45 struct Program;
46 } // namespace panda::pandasm
47
48 namespace panda::es2panda::lexer {
49 class LineIndex;
50 }
51
52 namespace panda::es2panda::util {
53
54 enum class SignedNumberLiteral {
55 UNRECOGNIZED = 0,
56 POSITIVE = 1,
57 NEGATIVE = 2
58 };
59
60 class Helpers {
61 public:
62 Helpers() = delete;
63
64 static bool IsGlobalIdentifier(const util::StringView &str);
65 static bool ContainSpreadElement(const ArenaVector<ir::Expression *> &args);
66 static util::StringView LiteralToPropName(ArenaAllocator *allocator, const ir::Expression *lit);
67
68 template <typename T>
69 static bool IsInteger(double number);
70 static bool IsIndex(double number);
71 static int64_t GetIndex(const util::StringView &str);
72
73 static bool FileExtensionIs(std::string_view filePath, std::string_view extension);
74 static bool EndsWith(std::string_view str, std::string_view suffix);
75 static std::string DoubleToString(double number);
76 static int32_t GetIntegerSignificandBitCount(double number, int32_t &numberBitCount, char *significandArray);
77 static void GetScientificNotationForDouble(double number, uint32_t significandBitCount, int32_t &numberBitCount,
78 char *significandArray, char *sciNotationArray, uint32_t size);
79 static std::string ToString(double number);
80 static util::StringView ToStringView(ArenaAllocator *allocator, double number);
81 static util::StringView ToStringView(ArenaAllocator *allocator, int32_t number);
82 static util::StringView ToStringView(ArenaAllocator *allocator, uint32_t number);
83
84 static const ir::ScriptFunction *GetContainingConstructor(const ir::AstNode *node);
85 static const ir::ScriptFunction *GetContainingConstructor(const ir::ClassProperty *node);
86 static const ir::ScriptFunction *GetContainingFunction(const ir::AstNode *node);
87 static const ir::ClassDefinition *GetClassDefiniton(const ir::ScriptFunction *node);
88 static bool IsSpecialPropertyKey(const ir::Expression *expr);
89 static bool IsConstantPropertyKey(const ir::Expression *expr, bool isComputed);
90 static bool IsConstantExpr(const ir::Expression *expr);
91 static bool IsBindingPattern(const ir::AstNode *node);
92 static bool IsPattern(const ir::AstNode *node);
93 static std::vector<const ir::Identifier *> CollectBindingNames(const ir::AstNode *node);
94 static util::StringView FunctionName(ArenaAllocator *allocator, const ir::ScriptFunction *func);
95 static std::tuple<util::StringView, bool> ParamName(ArenaAllocator *allocator, const ir::AstNode *param,
96 uint32_t index);
97 static bool IsChild(const ir::AstNode *parent, const ir::AstNode *child);
98 static bool IsObjectPropertyValue(const ArenaVector<ir::Expression *> &properties, const ir::AstNode *ident);
99 static SignedNumberLiteral GetSignedNumberLiteral(const ir::Expression *expr);
100
101 static void OptimizeProgram(panda::pandasm::Program *prog, const std::string &inputFile);
102 template <typename T>
103 static T BaseName(T const &path, T const &delims = std::string(panda::os::file::File::GetPathDelim()));
104 static bool ReadFileToBuffer(const std::string &file, std::stringstream &ss);
105 static void ScanDirectives(ir::ScriptFunction *func, const lexer::LineIndex &lineIndex);
106 static std::string GetHashString(std::string str);
107 static std::wstring Utf8ToUtf16(const std::string &utf8);
108 template <typename T, typename... Args>
109 static T FileStream(const std::string &str, Args &&...args);
110 static bool ShouldCheckConcurrent(const binder::Scope *scope, const util::StringView name);
111 static void SendableCheckForClassStaticInitializer(const util::StringView name, const binder::Scope *&iter,
112 ir::ScriptFunction *&concurrentFunc);
113
114 static const uint32_t MAX_DOUBLE_DIGIT = 310;
115 static const uint32_t MAX_DOUBLE_PRECISION_DIGIT = 17;
116 static const int32_t MAX_DECIMAL_EXPONENT = 21;
117 static const int32_t MIN_DECIMAL_EXPONENT = -6;
118 static const int32_t FAIL_SNPRINTF_S = -1;
119 static const uint32_t INVALID_INDEX = 4294967295L;
120 static const uint32_t MAX_INT32 = 2147483647;
121 static const uint32_t MAX_INT16 = std::numeric_limits<int16_t>::max();
122 static const uint32_t MAX_INT8 = std::numeric_limits<int8_t>::max();
123 static constexpr std::string_view SHOW_SOURCE = "show source";
124 static constexpr std::string_view USE_CONCURRENT = "use concurrent";
125 static constexpr std::string_view USE_SENDABLE = "use sendable";
126 static const uint64_t FNV_PRIME = 1099511628211U;
127 static const uint64_t FNV_OFFSET = 14695981039346656037U;
128 private:
129 static bool SetFuncFlagsForDirectives(const ir::StringLiteral *strLit, ir::ScriptFunction *func,
130 const lexer::LineIndex &lineIndex);
131 };
132
133 template <typename T>
IsInteger(double number)134 bool Helpers::IsInteger(double number)
135 {
136 if (std::fabs(number) <= static_cast<double>(std::numeric_limits<T>::max())) {
137 T intNum = static_cast<T>(number);
138
139 if (static_cast<double>(intNum) == number) {
140 return true;
141 }
142 }
143
144 return false;
145 }
146
147 template <class T>
BaseName(T const & path,T const & delims)148 T Helpers::BaseName(T const &path, T const &delims)
149 {
150 return path.substr(path.find_last_of(delims) + 1);
151 }
152
153 template <typename T, typename... Args>
FileStream(const std::string & str,Args &&...args)154 T Helpers::FileStream(const std::string &str, Args &&...args)
155 {
156 T fileStream;
157 #ifdef PANDA_TARGET_WINDOWS
158 std::wstring filename = Helpers::Utf8ToUtf16(str);
159 #else //for linux and mac
160 std::string filename = str;
161 #endif
162 fileStream.open(filename.c_str(), args...);
163 return fileStream;
164 }
165
166 } // namespace panda::es2panda::util
167
168 #endif
169