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 } // namespace panda::es2panda::ir
35
36 namespace panda::es2panda {
37 struct CompilerOptions;
38 } // namespace panda::es2panda
39
40 namespace panda::pandasm {
41 struct Program;
42 } // namespace panda::pandasm
43
44
45 namespace panda::es2panda::util {
46
47 class Helpers {
48 public:
49 Helpers() = delete;
50
51 static bool IsGlobalIdentifier(const util::StringView &str);
52 static bool ContainSpreadElement(const ArenaVector<ir::Expression *> &args);
53 static util::StringView LiteralToPropName(const ir::Expression *lit);
54
55 template <typename T>
56 static bool IsInteger(double number);
57 static bool IsIndex(double number);
58 static int64_t GetIndex(const util::StringView &str);
59
60 static std::string ToString(double number);
61 static util::StringView ToStringView(ArenaAllocator *allocator, double number);
62 static util::StringView ToStringView(ArenaAllocator *allocator, int32_t number);
63 static util::StringView ToStringView(ArenaAllocator *allocator, uint32_t number);
64
65 static const ir::ScriptFunction *GetContainingConstructor(const ir::AstNode *node);
66 static const ir::ScriptFunction *GetContainingConstructor(const ir::ClassProperty *node);
67 static const ir::ScriptFunction *GetContainingFunction(const ir::AstNode *node);
68 static const ir::ClassDefinition *GetClassDefiniton(const ir::ScriptFunction *node);
69 static bool IsSpecialPropertyKey(const ir::Expression *expr);
70 static bool IsConstantPropertyKey(const ir::Expression *expr, bool isComputed);
71 static bool IsConstantExpr(const ir::Expression *expr);
72 static bool IsBindingPattern(const ir::AstNode *node);
73 static bool IsPattern(const ir::AstNode *node);
74 static std::vector<const ir::Identifier *> CollectBindingNames(const ir::AstNode *node);
75 static util::StringView FunctionName(const ir::ScriptFunction *func);
76 static std::tuple<util::StringView, bool> ParamName(ArenaAllocator *allocator, const ir::AstNode *param,
77 uint32_t index);
78 static bool IsChild(const ir::AstNode *parent, const ir::AstNode *child);
79 static bool IsObjectPropertyValue(const ir::ObjectExpression *object, const ir::AstNode *ident);
80
81 static void OptimizeProgram(panda::pandasm::Program *prog, const std::string &inputFile);
82 template <typename T>
83 static T BaseName(T const &path, T const &delims = std::string(panda::os::file::File::GetPathDelim()));
84 static bool ReadFileToBuffer(const std::string &file, std::stringstream &ss);
85 static std::wstring Utf8ToUtf16(const std::string &utf8);
86 template <typename T, typename... Args>
87 static T FileStream(const std::string &str, Args &&...args);
88
89 static const uint32_t INVALID_INDEX = 4294967295L;
90 static const uint32_t MAX_INT32 = 2147483647;
91 static const uint32_t MAX_INT16 = std::numeric_limits<int16_t>::max();
92 static const uint32_t MAX_INT8 = std::numeric_limits<int8_t>::max();
93 };
94
95 template <typename T>
IsInteger(double number)96 bool Helpers::IsInteger(double number)
97 {
98 if (std::fabs(number) <= static_cast<double>(std::numeric_limits<T>::max())) {
99 T intNum = static_cast<T>(number);
100
101 if (static_cast<double>(intNum) == number) {
102 return true;
103 }
104 }
105
106 return false;
107 }
108
109 template <class T>
BaseName(T const & path,T const & delims)110 T Helpers::BaseName(T const &path, T const &delims)
111 {
112 return path.substr(path.find_last_of(delims) + 1);
113 }
114
115 template <typename T, typename... Args>
FileStream(const std::string & str,Args &&...args)116 T Helpers::FileStream(const std::string &str, Args &&...args)
117 {
118 T fileStream;
119 #ifdef PANDA_TARGET_WINDOWS
120 std::wstring filename = Helpers::Utf8ToUtf16(str);
121 #else //for linux and mac
122 std::string filename = str;
123 #endif
124 fileStream.open(filename.c_str(), args...);
125 return fileStream;
126 }
127
128 } // namespace panda::es2panda::util
129
130 #endif
131