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 ECMASCRIPT_TS_TYPES_TS_TYPE_H 17 #define ECMASCRIPT_TS_TYPES_TS_TYPE_H 18 19 #include "ecmascript/ecma_macros.h" 20 #include "ecmascript/js_tagged_value.h" 21 #include "ecmascript/mem/tagged_object.h" 22 #include "ecmascript/property_attributes.h" 23 #include "ecmascript/ts_types/ts_loader.h" 24 #include "include/hclass.h" 25 #include "utils/bit_field.h" 26 #include "ecmascript/ts_types/ts_obj_layout_info.h" 27 28 namespace panda::ecmascript { 29 class TSType : public TaggedObject { 30 public: 31 static constexpr size_t BIT_FIELD_OFFSET = TaggedObjectSize(); 32 Cast(const TaggedObject * object)33 inline static TSType *Cast(const TaggedObject *object) 34 { 35 return static_cast<TSType *>(const_cast<TaggedObject *>(object)); 36 } 37 38 ACCESSORS_PRIMITIVE_FIELD(GT, uint64_t, BIT_FIELD_OFFSET, SIZE); 39 GetGTRef()40 GlobalTSTypeRef GetGTRef() const 41 { 42 return GlobalTSTypeRef(GetGT()); 43 } 44 SetGTRef(GlobalTSTypeRef r)45 void SetGTRef(GlobalTSTypeRef r) 46 { 47 SetGT(r.GetGlobalTSTypeRef()); 48 } 49 }; 50 51 class TSObjectType : public TSType { 52 public: 53 CAST_CHECK(TSObjectType, IsTSObjectType); 54 55 JSHClass *GetOrCreateHClass(JSThread *thread); 56 57 uint64_t GetTypeIdOfKey(JSTaggedValue key); 58 59 static constexpr size_t PROPERTIES_OFFSET = TSType::SIZE; 60 ACCESSORS(ObjLayoutInfo, PROPERTIES_OFFSET, HCLASS_OFFSET); 61 ACCESSORS(HClass, HCLASS_OFFSET, SIZE); 62 63 DECL_VISIT_OBJECT(PROPERTIES_OFFSET, SIZE) 64 DECL_DUMP() 65 66 private: 67 JSHClass* CreateHClassByProps(JSThread *thread, JSHandle<TSObjLayoutInfo> propType) const; 68 }; 69 70 class TSClassType : public TSType { 71 public: 72 CAST_CHECK(TSClassType, IsTSClassType); 73 74 static constexpr uint8_t FIELD_LENGTH = 4; // every field record name, typeIndex, accessFlag, readonly 75 static constexpr size_t INSTANCE_TYPE_OFFSET = TSType::SIZE; 76 77 static GlobalTSTypeRef SearchProperty(JSThread *thread, JSHandle<TSTypeTable> &table, TSTypeKind typeKind, 78 int localtypeId, JSHandle<EcmaString> propName); 79 80 ACCESSORS(InstanceType, INSTANCE_TYPE_OFFSET, CONSTRUCTOR_TYPE_OFFSET); 81 ACCESSORS(ConstructorType, CONSTRUCTOR_TYPE_OFFSET, PROTOTYPE_TYPE_OFFSET); 82 ACCESSORS(PrototypeType, PROTOTYPE_TYPE_OFFSET, EXTENSION_TYPE_OFFSET); 83 ACCESSORS(ExtensionType, EXTENSION_TYPE_OFFSET, LAST_OFFSET); 84 DEFINE_ALIGN_SIZE(LAST_OFFSET); 85 86 DECL_VISIT_OBJECT(INSTANCE_TYPE_OFFSET, LAST_OFFSET) 87 DECL_DUMP() 88 }; 89 90 class TSClassInstanceType : public TSType { 91 public: 92 CAST_CHECK(TSClassInstanceType, IsTSClassInstanceType); 93 94 static GlobalTSTypeRef SearchProperty(JSThread *thread, JSHandle<TSTypeTable> &table, TSTypeKind typeKind, 95 int localtypeId, JSHandle<EcmaString> propName); 96 97 static constexpr size_t CREATE_CLASS_TYPE_OFFSET = TSType::SIZE; 98 ACCESSORS(CreateClassType, CREATE_CLASS_TYPE_OFFSET, SIZE); 99 100 DECL_VISIT_OBJECT(CREATE_CLASS_TYPE_OFFSET, SIZE) 101 DECL_DUMP() 102 }; 103 104 class TSImportType : public TSType { 105 public: 106 CAST_CHECK(TSImportType, IsTSImportType); 107 108 static constexpr size_t IMPORT_TYPE_ID_OFFSET = TSType::SIZE; 109 110 ACCESSORS(ImportPath, IMPORT_TYPE_ID_OFFSET, IMPORT_PATH); 111 ACCESSORS(TargetType, IMPORT_PATH, LAST_OFFSET); 112 DEFINE_ALIGN_SIZE(LAST_OFFSET); 113 114 DECL_VISIT_OBJECT(IMPORT_TYPE_ID_OFFSET, IMPORT_PATH) 115 DECL_DUMP() 116 }; 117 118 class TSUnionType : public TSType { 119 public: 120 CAST_CHECK(TSUnionType, IsTSUnionType); 121 122 bool IsEqual(JSThread *thread, JSHandle<TSUnionType> unionB); 123 124 static constexpr size_t COMPONENT_OFFSET = TSType::SIZE; 125 ACCESSORS(ComponentTypes, COMPONENT_OFFSET, SIZE); 126 127 DECL_VISIT_OBJECT(COMPONENT_OFFSET, SIZE) 128 DECL_DUMP() 129 }; 130 131 class TSInterfaceType : public TSType { 132 public: 133 CAST_CHECK(TSInterfaceType, IsTSInterfaceType); 134 135 static constexpr size_t EXTENDS_TYPE_ID_OFFSET = TSType::SIZE; 136 ACCESSORS(Extends, EXTENDS_TYPE_ID_OFFSET, KEYS_OFFSET); 137 ACCESSORS(Fields, KEYS_OFFSET, SIZE); 138 139 DECL_VISIT_OBJECT(KEYS_OFFSET, SIZE) 140 DECL_DUMP() 141 }; 142 } // namespace panda::ecmascript 143 144 #endif // ECMASCRIPT_TS_TYPES_TS_TYPE_H 145