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 enum class TSTypeKind : uint8_t { 24 PRIMITIVE = 0, 25 // the following typekinds are recorded in abc files and are consistent with the corresponding values 26 CLASS, 27 CLASS_INSTANCE, 28 FUNCTION, 29 UNION, 30 ARRAY, 31 OBJECT, 32 IMPORT, 33 INTERFACE, 34 BUILTIN_INSTANCE, 35 GENERIC_INSTANCE, 36 INDEXSIG, 37 38 // the following typekinds are not recorded in abc files and will be created at compile time 39 ITERATOR_INSTANCE, 40 NAMESPACE, 41 UNKNOWN, 42 43 TYPEKIND_FIRST = CLASS, 44 TYPEKIND_LAST = INDEXSIG, 45 }; 46 47 enum class TSPrimitiveType : int { 48 ANY = 0, 49 NUMBER, 50 BOOLEAN, 51 VOID_TYPE, 52 STRING, 53 SYMBOL, 54 NULL_TYPE, 55 UNDEFINED, 56 INT, 57 BIG_INT, 58 DOUBLE, 59 END 60 }; 61 62 /* 63 * Indicates the position of the iterator object in the runtime table. 64 * Position 0 in the runtime table stores the number of existing types, so start from 1. 65 */ 66 enum class TSRuntimeType : int { 67 ITERATOR_RESULT = 1, 68 ITERATOR_FUNCTION, 69 ITERATOR 70 }; 71 72 enum class ModuleTableIdx : uint8_t { 73 PRIMITIVE = 0, 74 BUILTIN, 75 INFERRED, 76 RUNTIME, 77 GENERICS, 78 NUM_OF_DEFAULT_TABLES, 79 }; 80 81 class GlobalTSTypeRef { 82 public: type_(type)83 explicit GlobalTSTypeRef(uint32_t type = 0) : type_(type) {} GlobalTSTypeRef(int moduleId,int localId)84 GlobalTSTypeRef(int moduleId, int localId) : type_(0) 85 { 86 SetLocalId(static_cast<uint16_t>(localId)); 87 SetModuleId(static_cast<uint16_t>(moduleId)); 88 } 89 90 ~GlobalTSTypeRef() = default; 91 Default()92 static GlobalTSTypeRef Default() 93 { 94 return GlobalTSTypeRef(0u); 95 } 96 GetType()97 uint32_t GetType() const 98 { 99 return type_; 100 } 101 SetType(uint32_t type)102 void SetType(uint32_t type) 103 { 104 type_ = type; 105 } 106 Clear()107 void Clear() 108 { 109 type_ = 0; 110 } 111 IsDefault()112 bool IsDefault() const 113 { 114 return type_ == 0; 115 } 116 117 bool operator <(const GlobalTSTypeRef &other) const 118 { 119 return type_ < other.type_; 120 } 121 122 bool operator ==(const GlobalTSTypeRef &other) const 123 { 124 return type_ == other.type_; 125 } 126 127 bool operator !=(const GlobalTSTypeRef &other) const 128 { 129 return type_ != other.type_; 130 } 131 132 #define IS_TYPE_MODULE_LIST(V) \ 133 V(Primitive, ModuleTableIdx::PRIMITIVE) \ 134 V(Builtin, ModuleTableIdx::BUILTIN) \ 135 V(Inferred, ModuleTableIdx::INFERRED) \ 136 V(Runtime, ModuleTableIdx::RUNTIME) \ 137 V(Generics, ModuleTableIdx::GENERICS) 138 139 #define IS_TYPE_MODULE(NAME, MODULEID) \ 140 inline bool Is##NAME##Module() const \ 141 { \ 142 return (GetModuleId() == static_cast<uint32_t>(MODULEID)); \ 143 } 144 IS_TYPE_MODULE_LIST(IS_TYPE_MODULE)145 IS_TYPE_MODULE_LIST(IS_TYPE_MODULE) 146 #undef IS_TYPE_MODULE 147 148 void Dump() const 149 { 150 uint32_t moduleId = GetModuleId(); 151 uint32_t localId = GetLocalId(); 152 LOG_ECMA(ERROR) << " moduleId: " << moduleId 153 << " localId: " << localId; 154 } 155 GetSizeBits()156 static constexpr uint32_t GetSizeBits() 157 { 158 return SIZE_BITS; 159 } 160 161 static constexpr uint32_t LOCAL_ID_BITS = 14; 162 static constexpr uint32_t MODULE_ID_BITS = 14; 163 static constexpr uint32_t SIZE_BITS = LOCAL_ID_BITS + MODULE_ID_BITS; 164 // MAX_LOCAL_ID types in one ts file can be stored in TSTypeTable 165 static constexpr uint64_t MAX_LOCAL_ID = (1LLU << LOCAL_ID_BITS) - 1; 166 // (MAX_MODULE_ID - ModuleTableIdx::NUM_OF_DEFAULT_TABLES) ts files with types can be stored in TSModuleTable 167 static constexpr uint64_t MAX_MODULE_ID = (1LLU << MODULE_ID_BITS) - 1; 168 169 FIRST_BIT_FIELD(Type, LocalId, uint32_t, LOCAL_ID_BITS); 170 NEXT_BIT_FIELD(Type, ModuleId, uint32_t, MODULE_ID_BITS, LocalId); 171 IsValidLocalId(uint32_t localId)172 static inline bool IsValidLocalId(uint32_t localId) 173 { 174 return (static_cast<uint64_t>(localId) & ~MAX_LOCAL_ID) == 0; 175 } 176 IsValidModuleId(uint32_t moduleId)177 static inline bool IsValidModuleId(uint32_t moduleId) 178 { 179 return (static_cast<uint64_t>(moduleId) & ~MAX_MODULE_ID) == 0; 180 } 181 182 private: 183 uint32_t type_ {0}; 184 }; 185 } // namespace panda::ecmascript 186 187 #endif // ECMASCRIPT_TS_TYPES_GLOBAL_TS_TYPE_REF_H 188