• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 PANDA_RUNTIME_INCLUDE_FIELD_H_
17 #define PANDA_RUNTIME_INCLUDE_FIELD_H_
18 
19 #include <cstdint>
20 #include <atomic>
21 
22 #include "intrinsics.h"
23 #include "libpandafile/file.h"
24 #include "libpandafile/file_items.h"
25 #include "libpandafile/modifiers.h"
26 #include "runtime/include/compiler_interface.h"
27 namespace panda {
28 
29 class Class;
30 
31 class ClassLinkerErrorHandler;
32 
33 class Field {
34 public:
35     using UniqId = uint64_t;
36 
Field(Class * klass,const panda_file::File * pf,panda_file::File::EntityId file_id,uint32_t access_flags,panda_file::Type type)37     Field(Class *klass, const panda_file::File *pf, panda_file::File::EntityId file_id, uint32_t access_flags,
38           panda_file::Type type)
39         : class_(klass), panda_file_(pf), file_id_(file_id), access_flags_(access_flags), type_(type)
40     {
41     }
42 
GetClass()43     Class *GetClass() const
44     {
45         return class_;
46     }
47 
SetClass(Class * cls)48     void SetClass(Class *cls)
49     {
50         class_ = cls;
51     }
52 
GetClassOffset()53     static constexpr uint32_t GetClassOffset()
54     {
55         return MEMBER_OFFSET(Field, class_);
56     }
57 
GetPandaFile()58     const panda_file::File *GetPandaFile() const
59     {
60         return panda_file_;
61     }
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 type_;
93     }
94 
95     panda_file::File::StringData GetName() const;
96 
IsPublic()97     bool IsPublic() const
98     {
99         return (access_flags_ & ACC_PUBLIC) != 0;
100     }
101 
IsPrivate()102     bool IsPrivate() const
103     {
104         return (access_flags_ & ACC_PRIVATE) != 0;
105     }
106 
IsProtected()107     bool IsProtected() const
108     {
109         return (access_flags_ & ACC_PROTECTED) != 0;
110     }
111 
IsStatic()112     bool IsStatic() const
113     {
114         return (access_flags_ & ACC_STATIC) != 0;
115     }
116 
IsVolatile()117     bool IsVolatile() const
118     {
119         return (access_flags_ & ACC_VOLATILE) != 0;
120     }
121 
IsFinal()122     bool IsFinal() const
123     {
124         return (access_flags_ & ACC_FINAL) != 0;
125     }
126 
CalcUniqId(const panda_file::File * file,panda_file::File::EntityId file_id)127     static inline UniqId CalcUniqId(const panda_file::File *file, panda_file::File::EntityId file_id)
128     {
129         constexpr uint64_t HALF = 32ULL;
130         uint64_t uid = file->GetFilenameHash();
131         uid <<= HALF;
132         uid |= file_id.GetOffset();
133         return uid;
134     }
135 
GetUniqId()136     UniqId GetUniqId() const
137     {
138         return CalcUniqId(panda_file_, file_id_);
139     }
140 
141     ~Field() = default;
142 
143     NO_COPY_SEMANTIC(Field);
144     NO_MOVE_SEMANTIC(Field);
145 
146 private:
147     Class *class_;
148     const panda_file::File *panda_file_;
149     panda_file::File::EntityId file_id_;
150     uint32_t access_flags_;
151     panda_file::Type type_;
152     uint32_t offset_ {0};
153 };
154 
155 }  // namespace panda
156 
157 #endif  // PANDA_RUNTIME_INCLUDE_FIELD_H_
158