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 
16 #ifndef LIBPANDAFILE_METHOD_DATA_ACCESSOR_H
17 #define LIBPANDAFILE_METHOD_DATA_ACCESSOR_H
18 
19 #include "file.h"
20 #include "file_items.h"
21 #include "modifiers.h"
22 
23 namespace panda::panda_file {
24 
25 // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions, hicpp-special-member-functions)
26 class MethodDataAccessor {
27 public:
28     MethodDataAccessor(const File &panda_file, File::EntityId method_id);
29 
30     ~MethodDataAccessor() = default;
31 
32     // quick way to get name id
33     static File::EntityId GetNameId(const File &panda_file, File::EntityId method_id);
34 
35     // quick way to get method name
36     static panda_file::File::StringData GetName(const File &panda_file, File::EntityId method_id);
37 
38     // quick way to get proto id
39     static File::EntityId GetProtoId(const File &panda_file, File::EntityId method_id);
40 
41     // quick way to get class id
42     static File::EntityId GetClassId(const File &panda_file, File::EntityId method_id);
43 
IsExternal() const44     bool IsExternal() const
45     {
46         return is_external_;
47     }
48 
IsStatic() const49     bool IsStatic() const
50     {
51         return (access_flags_ & ACC_STATIC) != 0;
52     }
53 
IsAbstract() const54     bool IsAbstract() const
55     {
56         return (access_flags_ & ACC_ABSTRACT) != 0;
57     }
58 
IsNative() const59     bool IsNative() const
60     {
61         return (access_flags_ & ACC_NATIVE) != 0;
62     }
63 
IsPublic() const64     bool IsPublic() const
65     {
66         return (access_flags_ & ACC_PUBLIC) != 0;
67     }
68 
IsPrivate() const69     bool IsPrivate() const
70     {
71         return (access_flags_ & ACC_PRIVATE) != 0;
72     }
73 
IsProtected() const74     bool IsProtected() const
75     {
76         return (access_flags_ & ACC_PROTECTED) != 0;
77     }
78 
IsFinal() const79     bool IsFinal() const
80     {
81         return (access_flags_ & ACC_FINAL) != 0;
82     }
83 
IsSynthetic() const84     bool IsSynthetic() const
85     {
86         return (access_flags_ & ACC_SYNTHETIC) != 0;
87     }
88 
GetClassId() const89     File::EntityId GetClassId() const
90     {
91         return File::EntityId(class_off_);
92     }
93 
GetClassIdx() const94     File::Index GetClassIdx() const
95     {
96         return class_idx_;
97     }
98 
GetNameId() const99     File::EntityId GetNameId() const
100     {
101         return File::EntityId(name_off_);
102     };
103 
104     panda_file::File::StringData GetName() const;
105 
GetProtoId() const106     File::EntityId GetProtoId() const
107     {
108         return File::EntityId(proto_off_);
109     }
110 
GetProtoIdx() const111     File::Index GetProtoIdx() const
112     {
113         return proto_idx_;
114     }
115 
GetAccessFlags() const116     uint32_t GetAccessFlags() const
117     {
118         return access_flags_;
119     }
120 
121     std::optional<File::EntityId> GetCodeId();
122 
123     std::optional<SourceLang> GetSourceLang();
124 
125     template <class Callback>
126     void EnumerateRuntimeAnnotations(Callback cb);
127 
128     template <typename Callback>
129     void EnumerateTypesInProto(Callback cb, bool skip_this = false);
130 
131     std::optional<File::EntityId> GetRuntimeParamAnnotationId();
132 
133     std::optional<File::EntityId> GetDebugInfoId();
134 
135     template <class Callback>
136     void EnumerateAnnotations(Callback cb);
137 
138     template <class Callback>
139     bool EnumerateRuntimeAnnotationsWithEarlyStop(Callback cb);
140 
141     template <class Callback>
142     bool EnumerateAnnotationsWithEarlyStop(Callback cb);
143 
144     template <class Callback>
145     void EnumerateTypeAnnotations(Callback cb);
146 
147     template <class Callback>
148     void EnumerateRuntimeTypeAnnotations(Callback cb);
149 
150     std::optional<File::EntityId> GetParamAnnotationId();
151 
GetSize()152     size_t GetSize()
153     {
154         if (size_ == 0) {
155             SkipRuntimeTypeAnnotation();
156         }
157 
158         return size_;
159     }
160 
GetPandaFile() const161     const File &GetPandaFile() const
162     {
163         return panda_file_;
164     }
165 
GetMethodId() const166     File::EntityId GetMethodId() const
167     {
168         return method_id_;
169     }
170 
HasValidProto() const171     bool HasValidProto() const
172     {
173         return proto_idx_ != INVALID_INDEX_16;
174     }
175 
176     uint32_t GetAnnotationsNumber();
177     uint32_t GetRuntimeAnnotationsNumber();
178     uint32_t GetTypeAnnotationsNumber();
179     uint32_t GetRuntimeTypeAnnotationsNumber();
180 
181     uint32_t GetNumericalAnnotation(uint32_t field_id);
182 
183 private:
184     void SkipCode();
185 
186     void SkipSourceLang();
187 
188     void SkipRuntimeAnnotations();
189 
190     void SkipRuntimeParamAnnotation();
191 
192     void SkipDebugInfo();
193 
194     void SkipAnnotations();
195 
196     void SkipParamAnnotation();
197 
198     void SkipTypeAnnotation();
199 
200     void SkipRuntimeTypeAnnotation();
201 
202     const File &panda_file_;
203     File::EntityId method_id_;
204 
205     bool is_external_;
206 
207     uint16_t class_idx_;
208     uint16_t proto_idx_;
209     uint32_t class_off_;
210     uint32_t proto_off_ {0};
211     uint32_t name_off_;
212     uint32_t access_flags_;
213 
214     Span<const uint8_t> tagged_values_sp_ {nullptr, nullptr};
215     Span<const uint8_t> source_lang_sp_ {nullptr, nullptr};
216     Span<const uint8_t> runtime_annotations_sp_ {nullptr, nullptr};
217     Span<const uint8_t> runtime_param_annotation_sp_ {nullptr, nullptr};
218     Span<const uint8_t> debug_sp_ {nullptr, nullptr};
219     Span<const uint8_t> annotations_sp_ {nullptr, nullptr};
220     Span<const uint8_t> param_annotation_sp_ {nullptr, nullptr};
221     Span<const uint8_t> type_annotation_sp_ {nullptr, nullptr};
222     Span<const uint8_t> runtime_type_annotation_sp_ {nullptr, nullptr};
223 
224     size_t size_;
225 };
226 
227 }  // namespace panda::panda_file
228 
229 #endif  // LIBPANDAFILE_METHOD_DATA_ACCESSOR_H
230