• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 ECMASCRIPT_TS_TYPES_GLOBAL_TS_TYPE_REF_H
17 #define ECMASCRIPT_TS_TYPES_GLOBAL_TS_TYPE_REF_H
18 
19 #include "ecmascript/ecma_macros.h"
20 #include "libpandabase/utils/bit_field.h"
21 
22 namespace panda::ecmascript {
23 /*
24  * To maintain consistency with the frontend, ITERATOR_INSTANCE should always be at
25  * the end of TSTypeKind, because there is no ITERATOR_INSTANCE in the frontend.
26  */
27 enum class TSTypeKind : int {
28     PRIMITIVE = 0,
29     CLASS,
30     CLASS_INSTANCE,
31     FUNCTION,
32     UNION,
33     ARRAY,
34     OBJECT,
35     IMPORT,
36     INTERFACE_KIND,
37     ITERATOR_INSTANCE,
38     UNKNOWN
39 };
40 
41 enum class TSPrimitiveType : int {
42     ANY = 0,
43     NUMBER,
44     BOOLEAN,
45     VOID_TYPE,
46     STRING,
47     SYMBOL,
48     NULL_TYPE,
49     UNDEFINED,
50     INT,
51     BIG_INT,
52     DOUBLE,
53     END
54 };
55 
56 /*
57  * Indicates the position of the iterator object in the runtime table.
58  * Position 0 in the runtime table stores the number of existing types, so start from 1.
59  */
60 enum class TSRuntimeType : int {
61     ITERATOR_RESULT = 1,
62     ITERATOR_FUNCTION,
63     ITERATOR
64 };
65 
66 class GlobalTSTypeRef {
67 public:
type_(type)68     explicit GlobalTSTypeRef(uint32_t type = 0) : type_(type) {}
GlobalTSTypeRef(int moduleId,int localId)69     explicit GlobalTSTypeRef(int moduleId, int localId) : type_(0)
70     {
71         SetLocalId(static_cast<uint16_t>(localId));
72         SetModuleId(static_cast<uint16_t>(moduleId));
73     }
74 
75     ~GlobalTSTypeRef() = default;
76 
Default()77     static GlobalTSTypeRef Default()
78     {
79         return GlobalTSTypeRef(0u);
80     }
81 
GetType()82     uint32_t GetType() const
83     {
84         return type_;
85     }
86 
SetType(uint32_t type)87     void SetType(uint32_t type)
88     {
89         type_ = type;
90     }
91 
Clear()92     void Clear()
93     {
94         type_ = 0;
95     }
96 
IsDefault()97     bool IsDefault() const
98     {
99         return type_ == 0;
100     }
101 
102     bool operator <(const GlobalTSTypeRef &other) const
103     {
104         return type_ < other.type_;
105     }
106 
107     bool operator ==(const GlobalTSTypeRef &other) const
108     {
109         return type_ == other.type_;
110     }
111 
112     bool operator !=(const GlobalTSTypeRef &other) const
113     {
114         return type_ != other.type_;
115     }
116 
Dump()117     void Dump() const
118     {
119         uint32_t moduleId = GetModuleId();
120         uint32_t localId = GetLocalId();
121         LOG_ECMA(ERROR) << " moduleId: " << moduleId
122                         << " localId: " << localId;
123     }
124 
GetSizeBits()125     static constexpr uint32_t GetSizeBits()
126     {
127         return SIZE_BITS;
128     }
129 
130     static constexpr uint32_t LOCAL_ID_BITS = 14;
131     static constexpr uint32_t MODULE_ID_BITS = 14;
132     static constexpr uint32_t SIZE_BITS = LOCAL_ID_BITS + MODULE_ID_BITS;
133     // MAX_LOCAL_ID types in one ts file can be stored in TSTypeTable
134     static constexpr uint64_t MAX_LOCAL_ID = (1LLU << LOCAL_ID_BITS) - 1;
135     // (MAX_MODULE_ID - TSModuleTable::DEFAULT_NUMBER_OF_TABLES) ts files with types can be stored in TSModuleTable
136     static constexpr uint64_t MAX_MODULE_ID = (1LLU << MODULE_ID_BITS) - 1;
137 
138     FIRST_BIT_FIELD(Type, LocalId, uint32_t, LOCAL_ID_BITS);
139     NEXT_BIT_FIELD(Type, ModuleId, uint32_t, MODULE_ID_BITS, LocalId);
140 
IsVaildLocalId(uint32_t localId)141     static inline bool IsVaildLocalId(uint32_t localId)
142     {
143         return (static_cast<uint64_t>(localId) & ~MAX_LOCAL_ID) == 0;
144     }
145 
IsVaildModuleId(uint32_t moduleId)146     static inline bool IsVaildModuleId(uint32_t moduleId)
147     {
148         return (static_cast<uint64_t>(moduleId) & ~MAX_MODULE_ID) == 0;
149     }
150 
151 private:
152     uint32_t type_ {0};
153 };
154 }  // namespace panda::ecmascript
155 
156 #endif  // ECMASCRIPT_TS_TYPES_GLOBAL_TS_TYPE_REF_H
157