• 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_LIBPANDAFILE_CLASS_DATA_ACCESSOR_H_
17 #define PANDA_LIBPANDAFILE_CLASS_DATA_ACCESSOR_H_
18 
19 #include "file.h"
20 #include "file_items.h"
21 #include "field_data_accessor.h"
22 #include "method_data_accessor.h"
23 
24 namespace panda::panda_file {
25 
26 // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions, hicpp-special-member-functions)
27 class ClassDataAccessor {
28 public:
29     ClassDataAccessor(const File &panda_file, File::EntityId class_id);
30 
31     ~ClassDataAccessor() = default;
32 
GetSuperClassId()33     File::EntityId GetSuperClassId() const
34     {
35         return File::EntityId(super_class_off_);
36     }
37 
IsInterface()38     bool IsInterface() const
39     {
40         return (access_flags_ & ACC_INTERFACE) != 0;
41     }
42 
GetAccessFlags()43     uint32_t GetAccessFlags() const
44     {
45         return access_flags_;
46     }
47 
GetFieldsNumber()48     uint32_t GetFieldsNumber() const
49     {
50         return num_fields_;
51     }
52 
GetMethodsNumber()53     uint32_t GetMethodsNumber() const
54     {
55         return num_methods_;
56     }
57 
GetIfacesNumber()58     uint32_t GetIfacesNumber() const
59     {
60         return num_ifaces_;
61     }
62 
63     uint32_t GetAnnotationsNumber();
64 
65     uint32_t GetRuntimeAnnotationsNumber();
66 
67     File::EntityId GetInterfaceId(size_t idx) const;
68 
69     template <class Callback>
70     void EnumerateInterfaces(const Callback &cb);
71 
72     template <class Callback>
73     void EnumerateRuntimeAnnotations(const Callback &cb);
74 
75     template <class Callback>
76     void EnumerateAnnotations(const Callback &cb);
77 
78     std::optional<SourceLang> GetSourceLang();
79 
80     std::optional<File::EntityId> GetSourceFileId();
81 
82     template <class Callback>
83     void EnumerateFields(const Callback &cb);
84 
85     template <class Callback>
86     void EnumerateMethods(const Callback &cb);
87 
GetSize()88     size_t GetSize()
89     {
90         if (size_ == 0) {
91             SkipMethods();
92         }
93 
94         return size_;
95     }
96 
GetPandaFile()97     const File &GetPandaFile() const
98     {
99         return panda_file_;
100     }
101 
GetClassId()102     File::EntityId GetClassId() const
103     {
104         return class_id_;
105     }
106 
GetDescriptor()107     const uint8_t *GetDescriptor() const
108     {
109         return name_.data;
110     }
111 
112 private:
113     void SkipSourceLang();
114 
115     void SkipRuntimeAnnotations();
116 
117     void SkipAnnotations();
118 
119     void SkipSourceFile();
120 
121     void SkipFields();
122 
123     void SkipMethods();
124 
125     const File &panda_file_;
126     File::EntityId class_id_;
127 
128     File::StringData name_;
129     uint32_t super_class_off_;
130     uint32_t access_flags_;
131     uint32_t num_fields_;
132     uint32_t num_methods_;
133     uint32_t num_ifaces_;
134 
135     Span<const uint8_t> ifaces_offsets_sp_ {nullptr, nullptr};
136     Span<const uint8_t> source_lang_sp_ {nullptr, nullptr};
137     Span<const uint8_t> runtime_annotations_sp_ {nullptr, nullptr};
138     Span<const uint8_t> annotations_sp_ {nullptr, nullptr};
139     Span<const uint8_t> source_file_sp_ {nullptr, nullptr};
140     Span<const uint8_t> fields_sp_ {nullptr, nullptr};
141     Span<const uint8_t> methods_sp_ {nullptr, nullptr};
142 
143     size_t size_;
144 };
145 
146 }  // namespace panda::panda_file
147 
148 #endif  // PANDA_LIBPANDAFILE_CLASS_DATA_ACCESSOR_H_
149