1 /** 2 * Copyright (c) 2021-2024 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 ark::panda_file { 24 25 // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions, hicpp-special-member-functions) 26 class MethodDataAccessor { 27 public: 28 enum AnnotationField : uint32_t { 29 IC_SIZE = 0, 30 FUNCTION_LENGTH, 31 FUNCTION_NAME, 32 STRING_DATA_BEGIN = FUNCTION_NAME, 33 STRING_DATA_END = FUNCTION_NAME 34 }; 35 36 PANDA_PUBLIC_API MethodDataAccessor(const File &pandaFile, File::EntityId methodId); 37 38 ~MethodDataAccessor() = default; 39 40 // quick way to get name id 41 static File::EntityId GetNameId(const File &pandaFile, File::EntityId methodId); 42 43 // quick way to get method name 44 static panda_file::File::StringData GetName(const File &pandaFile, File::EntityId methodId); 45 46 std::string GetClassName() const; 47 48 std::string GetFullName() const; 49 50 // quick way to get proto id 51 static File::EntityId GetProtoId(const File &pandaFile, File::EntityId methodId); 52 53 // quick way to get class id 54 static File::EntityId GetClassId(const File &pandaFile, File::EntityId methodId); 55 IsExternal()56 bool IsExternal() const 57 { 58 return isExternal_; 59 } 60 IsStatic()61 bool IsStatic() const 62 { 63 return (accessFlags_ & ACC_STATIC) != 0; 64 } 65 IsAbstract()66 bool IsAbstract() const 67 { 68 return (accessFlags_ & ACC_ABSTRACT) != 0; 69 } 70 IsVarArgs()71 bool IsVarArgs() const 72 { 73 return (accessFlags_ & ACC_VARARGS) != 0; 74 } 75 IsNative()76 bool IsNative() const 77 { 78 return (accessFlags_ & ACC_NATIVE) != 0; 79 } 80 IsPublic()81 bool IsPublic() const 82 { 83 return (accessFlags_ & ACC_PUBLIC) != 0; 84 } 85 IsPrivate()86 bool IsPrivate() const 87 { 88 return (accessFlags_ & ACC_PRIVATE) != 0; 89 } 90 IsProtected()91 bool IsProtected() const 92 { 93 return (accessFlags_ & ACC_PROTECTED) != 0; 94 } 95 IsFinal()96 bool IsFinal() const 97 { 98 return (accessFlags_ & ACC_FINAL) != 0; 99 } 100 IsSynthetic()101 bool IsSynthetic() const 102 { 103 return (accessFlags_ & ACC_SYNTHETIC) != 0; 104 } 105 GetClassId()106 File::EntityId GetClassId() const 107 { 108 return File::EntityId(classOff_); 109 } 110 GetClassIdx()111 File::Index GetClassIdx() const 112 { 113 return classIdx_; 114 } 115 GetNameId()116 File::EntityId GetNameId() const 117 { 118 return File::EntityId(nameOff_); 119 }; 120 121 panda_file::File::StringData GetName() const; 122 GetProtoId()123 File::EntityId GetProtoId() const 124 { 125 return File::EntityId(protoOff_); 126 } 127 GetAccessFlags()128 uint32_t GetAccessFlags() const 129 { 130 return accessFlags_; 131 } 132 133 std::optional<File::EntityId> GetCodeId(); 134 135 std::optional<SourceLang> GetSourceLang(); 136 137 template <class Callback> 138 void EnumerateRuntimeAnnotations(Callback cb); 139 140 template <typename Callback> 141 void EnumerateTypesInProto(Callback cb, bool skipThis = false); 142 143 Type GetReturnType() const; 144 145 std::optional<File::EntityId> GetRuntimeParamAnnotationId(); 146 147 std::optional<File::EntityId> GetDebugInfoId(); 148 149 template <class Callback> 150 void EnumerateAnnotations(Callback cb); 151 152 template <class Callback> 153 bool EnumerateRuntimeAnnotationsWithEarlyStop(Callback cb); 154 155 template <class Callback> 156 bool EnumerateAnnotationsWithEarlyStop(Callback cb); 157 158 template <class Callback> 159 void EnumerateTypeAnnotations(Callback cb); 160 161 template <class Callback> 162 void EnumerateRuntimeTypeAnnotations(Callback cb); 163 164 std::optional<size_t> GetProfileSize(); 165 166 std::optional<File::EntityId> GetParamAnnotationId(); 167 GetSize()168 size_t GetSize() 169 { 170 if (size_ == 0) { 171 GetProfileSize(); 172 } 173 174 return size_; 175 } 176 GetPandaFile()177 const File &GetPandaFile() const 178 { 179 return pandaFile_; 180 } 181 GetMethodId()182 File::EntityId GetMethodId() const 183 { 184 return methodId_; 185 } 186 187 uint32_t GetAnnotationsNumber(); 188 uint32_t GetRuntimeAnnotationsNumber(); 189 uint32_t GetTypeAnnotationsNumber(); 190 uint32_t GetRuntimeTypeAnnotationsNumber(); 191 192 template <std::size_t SIZE> 193 void NumAnnonationProcess(uint32_t &result, File::EntityId &annotationId, 194 std::array<const char *, SIZE> elemNameTable, uint32_t fieldId); 195 uint32_t GetNumericalAnnotation(uint32_t fieldId); 196 197 private: 198 void SkipCode(); 199 200 void SkipSourceLang(); 201 202 void SkipRuntimeAnnotations(); 203 204 void SkipRuntimeParamAnnotation(); 205 206 void SkipDebugInfo(); 207 208 void SkipAnnotations(); 209 210 void SkipParamAnnotation(); 211 212 void SkipTypeAnnotation(); 213 214 void SkipRuntimeTypeAnnotation(); 215 216 const File &pandaFile_; 217 File::EntityId methodId_; 218 219 bool isExternal_; 220 221 uint16_t classIdx_; 222 uint16_t protoIdx_; 223 uint32_t classOff_; 224 uint32_t protoOff_; 225 uint32_t nameOff_; 226 uint32_t accessFlags_; 227 228 Span<const uint8_t> taggedValuesSp_ {nullptr, nullptr}; 229 Span<const uint8_t> sourceLangSp_ {nullptr, nullptr}; 230 Span<const uint8_t> runtimeAnnotationsSp_ {nullptr, nullptr}; 231 Span<const uint8_t> runtimeParamAnnotationSp_ {nullptr, nullptr}; 232 Span<const uint8_t> debugSp_ {nullptr, nullptr}; 233 Span<const uint8_t> annotationsSp_ {nullptr, nullptr}; 234 Span<const uint8_t> paramAnnotationSp_ {nullptr, nullptr}; 235 Span<const uint8_t> typeAnnotationSp_ {nullptr, nullptr}; 236 Span<const uint8_t> runtimeTypeAnnotationSp_ {nullptr, nullptr}; 237 Span<const uint8_t> profileInfoSp_ {nullptr, nullptr}; 238 239 size_t size_; 240 }; 241 242 } // namespace ark::panda_file 243 244 #endif // LIBPANDAFILE_METHOD_DATA_ACCESSOR_H_ 245