• 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 <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 
86     static const uint32_t INVALID_INDEX = 4294967295L;
87     static const uint32_t MAX_INT32 = 2147483647;
88     static const uint32_t MAX_INT16 = std::numeric_limits<int16_t>::max();
89     static const uint32_t MAX_INT8 = std::numeric_limits<int8_t>::max();
90 };
91 
92 template <typename T>
IsInteger(double number)93 bool Helpers::IsInteger(double number)
94 {
95     if (std::fabs(number) <= static_cast<double>(std::numeric_limits<T>::max())) {
96         T intNum = static_cast<T>(number);
97 
98         if (static_cast<double>(intNum) == number) {
99             return true;
100         }
101     }
102 
103     return false;
104 }
105 
106 template <class T>
BaseName(T const & path,T const & delims)107 T Helpers::BaseName(T const &path, T const &delims)
108 {
109     return path.substr(path.find_last_of(delims) + 1);
110 }
111 
112 }  // namespace panda::es2panda::util
113 
114 #endif
115