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