1 /* 2 * Copyright (c) 2023 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_TYPE_INFO_H 17 #define ECMASCRIPT_TS_TYPES_GLOBAL_TYPE_INFO_H 18 19 #include "ecmascript/jspandafile/js_pandafile.h" 20 21 namespace panda::ecmascript { 22 // a unique ID to determine whether the corresponding GT has been generated 23 class GlobalTypeID { 24 public: GlobalTypeID(const JSPandaFile * jsPandaFile,PGOSampleType pgoTypeId)25 explicit GlobalTypeID(const JSPandaFile *jsPandaFile, PGOSampleType pgoTypeId) 26 : jsPandaFile_(jsPandaFile), typeId_(0), pgoTypeId_(pgoTypeId) {} 27 GlobalTypeID(const JSPandaFile * jsPandaFile,uint32_t typeId)28 explicit GlobalTypeID(const JSPandaFile *jsPandaFile, uint32_t typeId) 29 : jsPandaFile_(jsPandaFile), typeId_(typeId), pgoTypeId_() {} 30 31 bool operator==(const GlobalTypeID &id) const 32 { 33 return jsPandaFile_ == id.jsPandaFile_ && 34 typeId_ == id.typeId_ && 35 pgoTypeId_ == id.pgoTypeId_; 36 } 37 GetJSPandaFile()38 const JSPandaFile *GetJSPandaFile() const 39 { 40 return jsPandaFile_; 41 } 42 GetTypeId()43 uint32_t GetTypeId() const 44 { 45 return typeId_; 46 } 47 GetPGOTypeId()48 PGOSampleType GetPGOTypeId() const 49 { 50 return pgoTypeId_; 51 } 52 IsPGOType()53 bool IsPGOType() const 54 { 55 return !pgoTypeId_.IsNone(); 56 } 57 58 private: 59 const JSPandaFile *jsPandaFile_; 60 uint32_t typeId_; // the information is recording in the typeliteral 61 PGOSampleType pgoTypeId_; // the information is recording in the '.ap' file 62 }; 63 64 struct HashGlobalTypeID { operatorHashGlobalTypeID65 size_t operator()(const GlobalTypeID &id) const 66 { 67 if (id.IsPGOType()) { 68 return std::hash<const JSPandaFile*>()(id.GetJSPandaFile()) ^ 69 std::hash<int32_t>()(id.GetPGOTypeId().GetClassType().GetClassType()); 70 } 71 return std::hash<const JSPandaFile*>()(id.GetJSPandaFile()) ^ 72 std::hash<uint32_t>()(id.GetTypeId()); 73 } 74 }; 75 76 // this class records information about definition points related to class and object types. 77 class TypeLocation { 78 public: TypeLocation(const JSPandaFile * jsPandaFile,uint32_t methodOffset,int32_t bcIdx)79 explicit TypeLocation(const JSPandaFile *jsPandaFile, uint32_t methodOffset, 80 int32_t bcIdx) 81 : jsPandaFile_(jsPandaFile), methodOffset_(methodOffset), bcIdx_(bcIdx) {} 82 83 bool operator==(const TypeLocation &loc) const 84 { 85 return jsPandaFile_ == loc.jsPandaFile_ && 86 methodOffset_ == loc.methodOffset_ && 87 bcIdx_ == loc.bcIdx_; 88 } 89 GetBcIdx()90 int32_t GetBcIdx() const 91 { 92 return bcIdx_; 93 } 94 SetBcIdx(int32_t bcIdx)95 void SetBcIdx(int32_t bcIdx) 96 { 97 bcIdx_ = bcIdx; 98 } 99 GetJSPandaFile()100 const JSPandaFile *GetJSPandaFile() const 101 { 102 return jsPandaFile_; 103 } 104 GetMethodOffset()105 uint32_t GetMethodOffset() const 106 { 107 return methodOffset_; 108 } 109 110 private: 111 const JSPandaFile *jsPandaFile_; 112 uint32_t methodOffset_; 113 int32_t bcIdx_; 114 }; 115 116 struct HashTypeLocation { operatorHashTypeLocation117 size_t operator()(const TypeLocation &loc) const 118 { 119 return std::hash<const JSPandaFile*>()(loc.GetJSPandaFile()) ^ 120 std::hash<uint32_t>()(loc.GetMethodOffset()) ^ 121 std::hash<int32_t>()(loc.GetBcIdx()); 122 } 123 }; 124 } // panda::ecmascript 125 #endif // ECMASCRIPT_TS_TYPES_GLOBAL_TYPE_INFO_H 126