• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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()44     bool IsExternal() const
45     {
46         return is_external_;
47     }
48 
IsStatic()49     bool IsStatic() const
50     {
51         return (access_flags_ & ACC_STATIC) != 0;
52     }
53 
IsAbstract()54     bool IsAbstract() const
55     {
56         return (access_flags_ & ACC_ABSTRACT) != 0;
57     }
58 
IsNative()59     bool IsNative() const
60     {
61         return (access_flags_ & ACC_NATIVE) != 0;
62     }
63 
IsPublic()64     bool IsPublic() const
65     {
66         return (access_flags_ & ACC_PUBLIC) != 0;
67     }
68 
IsPrivate()69     bool IsPrivate() const
70     {
71         return (access_flags_ & ACC_PRIVATE) != 0;
72     }
73 
IsProtected()74     bool IsProtected() const
75     {
76         return (access_flags_ & ACC_PROTECTED) != 0;
77     }
78 
IsFinal()79     bool IsFinal() const
80     {
81         return (access_flags_ & ACC_FINAL) != 0;
82     }
83 
IsSynthetic()84     bool IsSynthetic() const
85     {
86         return (access_flags_ & ACC_SYNTHETIC) != 0;
87     }
88 
GetClassId()89     File::EntityId GetClassId() const
90     {
91         return File::EntityId(class_off_);
92     }
93 
GetClassIdx()94     File::Index GetClassIdx() const
95     {
96         return class_idx_;
97     }
98 
GetNameId()99     File::EntityId GetNameId() const
100     {
101         return File::EntityId(name_off_);
102     };
103 
104     panda_file::File::StringData GetName() const;
105 
GetProtoId()106     File::EntityId GetProtoId() const
107     {
108         return File::EntityId(proto_off_);
109     }
110 
GetAccessFlags()111     uint32_t GetAccessFlags() const
112     {
113         return access_flags_;
114     }
115 
116     std::optional<File::EntityId> GetCodeId();
117 
118     std::optional<SourceLang> GetSourceLang();
119 
120     template <class Callback>
121     void EnumerateRuntimeAnnotations(Callback cb);
122 
123     template <typename Callback>
124     void EnumerateTypesInProto(Callback cb, bool skip_this = false);
125 
126     std::optional<File::EntityId> GetRuntimeParamAnnotationId();
127 
128     std::optional<File::EntityId> GetDebugInfoId();
129 
130     template <class Callback>
131     void EnumerateAnnotations(Callback cb);
132 
133     template <class Callback>
134     bool EnumerateRuntimeAnnotationsWithEarlyStop(Callback cb);
135 
136     template <class Callback>
137     bool EnumerateAnnotationsWithEarlyStop(Callback cb);
138 
139     template <class Callback>
140     void EnumerateTypeAnnotations(Callback cb);
141 
142     template <class Callback>
143     void EnumerateRuntimeTypeAnnotations(Callback cb);
144 
145     std::optional<File::EntityId> GetParamAnnotationId();
146 
GetSize()147     size_t GetSize()
148     {
149         if (size_ == 0) {
150             SkipRuntimeTypeAnnotation();
151         }
152 
153         return size_;
154     }
155 
GetPandaFile()156     const File &GetPandaFile() const
157     {
158         return panda_file_;
159     }
160 
GetMethodId()161     File::EntityId GetMethodId() const
162     {
163         return method_id_;
164     }
165 
166     uint32_t GetAnnotationsNumber();
167     uint32_t GetRuntimeAnnotationsNumber();
168     uint32_t GetTypeAnnotationsNumber();
169     uint32_t GetRuntimeTypeAnnotationsNumber();
170 
171     uint32_t GetNumericalAnnotation(uint32_t field_id);
172 
173 private:
174     void SkipCode();
175 
176     void SkipSourceLang();
177 
178     void SkipRuntimeAnnotations();
179 
180     void SkipRuntimeParamAnnotation();
181 
182     void SkipDebugInfo();
183 
184     void SkipAnnotations();
185 
186     void SkipParamAnnotation();
187 
188     void SkipTypeAnnotation();
189 
190     void SkipRuntimeTypeAnnotation();
191 
192     const File &panda_file_;
193     File::EntityId method_id_;
194 
195     bool is_external_;
196 
197     uint16_t class_idx_;
198     uint16_t proto_idx_;
199     uint32_t class_off_;
200     uint32_t proto_off_;
201     uint32_t name_off_;
202     uint32_t access_flags_;
203 
204     Span<const uint8_t> tagged_values_sp_ {nullptr, nullptr};
205     Span<const uint8_t> source_lang_sp_ {nullptr, nullptr};
206     Span<const uint8_t> runtime_annotations_sp_ {nullptr, nullptr};
207     Span<const uint8_t> runtime_param_annotation_sp_ {nullptr, nullptr};
208     Span<const uint8_t> debug_sp_ {nullptr, nullptr};
209     Span<const uint8_t> annotations_sp_ {nullptr, nullptr};
210     Span<const uint8_t> param_annotation_sp_ {nullptr, nullptr};
211     Span<const uint8_t> type_annotation_sp_ {nullptr, nullptr};
212     Span<const uint8_t> runtime_type_annotation_sp_ {nullptr, nullptr};
213 
214     size_t size_;
215 };
216 
217 }  // namespace panda::panda_file
218 
219 #endif  // LIBPANDAFILE_METHOD_DATA_ACCESSOR_H
220