1 /** 2 * Copyright (c) 2021-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 #ifndef PANDA_RUNTIME_FIELD_H_ 16 #define PANDA_RUNTIME_FIELD_H_ 17 18 #include <cstdint> 19 #include <atomic> 20 21 #include "intrinsics_enum.h" 22 #include "libpandafile/file.h" 23 #include "libpandafile/file_items.h" 24 #include "libpandafile/modifiers.h" 25 #include "runtime/include/compiler_interface.h" 26 #include "runtime/include/class_helper.h" 27 #include "runtime/include/value-inl.h" 28 #include "libpandabase/macros.h" 29 30 namespace panda { 31 32 class Class; 33 34 class ClassLinkerErrorHandler; 35 36 class Field { 37 public: 38 using UniqId = uint64_t; 39 Field(Class * klass,panda_file::File::EntityId file_id,uint32_t access_flags,panda_file::Type type)40 Field(Class *klass, panda_file::File::EntityId file_id, uint32_t access_flags, panda_file::Type type) 41 : class_word_(static_cast<ClassHelper::classWordSize>(ToObjPtrType(klass))), file_id_(file_id) 42 { 43 access_flags_ = access_flags | (static_cast<uint32_t>(type.GetEncoding()) << ACC_TYPE_SHIFT); 44 } 45 GetClass()46 Class *GetClass() const 47 { 48 return reinterpret_cast<Class *>(class_word_); 49 } 50 SetClass(Class * cls)51 void SetClass(Class *cls) 52 { 53 class_word_ = static_cast<ClassHelper::classWordSize>(ToObjPtrType(cls)); 54 } 55 GetClassOffset()56 static constexpr uint32_t GetClassOffset() 57 { 58 return MEMBER_OFFSET(Field, class_word_); 59 } 60 61 const panda_file::File *GetPandaFile() const; 62 GetFileId()63 panda_file::File::EntityId GetFileId() const 64 { 65 return file_id_; 66 } 67 GetAccessFlags()68 uint32_t GetAccessFlags() const 69 { 70 return access_flags_; 71 } 72 GetOffset()73 uint32_t GetOffset() const 74 { 75 return offset_; 76 } 77 SetOffset(uint32_t offset)78 void SetOffset(uint32_t offset) 79 { 80 offset_ = offset; 81 } 82 GetOffsetOffset()83 static constexpr uint32_t GetOffsetOffset() 84 { 85 return MEMBER_OFFSET(Field, offset_); 86 } 87 88 Class *ResolveTypeClass(ClassLinkerErrorHandler *error_handler = nullptr) const; 89 GetType()90 panda_file::Type GetType() const 91 { 92 return panda_file::Type(GetTypeId()); 93 } 94 GetTypeId()95 panda_file::Type::TypeId GetTypeId() const 96 { 97 return static_cast<panda_file::Type::TypeId>((access_flags_ & ACC_TYPE) >> ACC_TYPE_SHIFT); 98 } 99 100 panda_file::File::StringData GetName() const; 101 IsPublic()102 bool IsPublic() const 103 { 104 return (access_flags_ & ACC_PUBLIC) != 0; 105 } 106 IsPrivate()107 bool IsPrivate() const 108 { 109 return (access_flags_ & ACC_PRIVATE) != 0; 110 } 111 IsProtected()112 bool IsProtected() const 113 { 114 return (access_flags_ & ACC_PROTECTED) != 0; 115 } 116 IsStatic()117 bool IsStatic() const 118 { 119 return (access_flags_ & ACC_STATIC) != 0; 120 } 121 IsVolatile()122 bool IsVolatile() const 123 { 124 return (access_flags_ & ACC_VOLATILE) != 0; 125 } 126 IsFinal()127 bool IsFinal() const 128 { 129 return (access_flags_ & ACC_FINAL) != 0; 130 } 131 CalcUniqId(const panda_file::File * file,panda_file::File::EntityId file_id)132 static inline UniqId CalcUniqId(const panda_file::File *file, panda_file::File::EntityId file_id) 133 { 134 constexpr uint64_t HALF = 32ULL; 135 uint64_t uid = file->GetFilenameHash(); 136 uid <<= HALF; 137 uid |= file_id.GetOffset(); 138 return uid; 139 } 140 GetUniqId()141 UniqId GetUniqId() const 142 { 143 return CalcUniqId(GetPandaFile(), file_id_); 144 } 145 146 ~Field() = default; 147 148 NO_COPY_SEMANTIC(Field); 149 NO_MOVE_SEMANTIC(Field); 150 151 private: 152 ClassHelper::classWordSize class_word_; 153 panda_file::File::EntityId file_id_; 154 uint32_t access_flags_; 155 uint32_t offset_ {0}; 156 }; 157 158 } // namespace panda 159 160 #endif // PANDA_RUNTIME_FIELD_H_ 161