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