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