1b1994897Sopenharmony_ci/**
2b1994897Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3b1994897Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1994897Sopenharmony_ci * you may not use this file except in compliance with the License.
5b1994897Sopenharmony_ci * You may obtain a copy of the License at
6b1994897Sopenharmony_ci *
7b1994897Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8b1994897Sopenharmony_ci *
9b1994897Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1994897Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1994897Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1994897Sopenharmony_ci * See the License for the specific language governing permissions and
13b1994897Sopenharmony_ci * limitations under the License.
14b1994897Sopenharmony_ci */
15b1994897Sopenharmony_ci
16b1994897Sopenharmony_ci#ifndef LIBPANDAFILE_INDEX_ACCESSOR_H
17b1994897Sopenharmony_ci#define LIBPANDAFILE_INDEX_ACCESSOR_H
18b1994897Sopenharmony_ci
19b1994897Sopenharmony_ci#include "file.h"
20b1994897Sopenharmony_ci#include "file_items.h"
21b1994897Sopenharmony_ci
22b1994897Sopenharmony_cinamespace panda::panda_file {
23b1994897Sopenharmony_ci
24b1994897Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions, hicpp-special-member-functions)
25b1994897Sopenharmony_ciclass IndexAccessor {
26b1994897Sopenharmony_cipublic:
27b1994897Sopenharmony_ci    IndexAccessor(const File &pf, File::EntityId method_id)
28b1994897Sopenharmony_ci    {
29b1994897Sopenharmony_ci        constexpr size_t SKIP_NUM = 3;  // 3 : skip class_idx and proto_idx and name
30b1994897Sopenharmony_ci        auto sp = pf.GetSpanFromId(method_id).SubSpan(IDX_SIZE * (SKIP_NUM - 1) + ID_SIZE);
31b1994897Sopenharmony_ci        access_flags_ = helpers::ReadULeb128(&sp);
32b1994897Sopenharmony_ci        header_index_ = access_flags_ >> (FUNTION_KIND_WIDTH + FLAG_WIDTH);
33b1994897Sopenharmony_ci        num_headers_ = pf.GetHeader()->num_indexes;
34b1994897Sopenharmony_ci        const auto *header = &(pf.GetIndexHeaders()[header_index_]);
35b1994897Sopenharmony_ci        indexes_ = pf.GetMethodIndex(header);
36b1994897Sopenharmony_ci    }
37b1994897Sopenharmony_ci
38b1994897Sopenharmony_ci    ~IndexAccessor() = default;
39b1994897Sopenharmony_ci
40b1994897Sopenharmony_ci    // quick way to resolve offset by 16-bit id in method's instructions
41b1994897Sopenharmony_ci    uint32_t GetOffsetById(uint16_t idx) const
42b1994897Sopenharmony_ci    {
43b1994897Sopenharmony_ci        return indexes_[idx].GetOffset();
44b1994897Sopenharmony_ci    }
45b1994897Sopenharmony_ci
46b1994897Sopenharmony_ci    FunctionKind GetFunctionKind() const
47b1994897Sopenharmony_ci    {
48b1994897Sopenharmony_ci        return static_cast<FunctionKind>((access_flags_ & FUNCTION_KIND_MASK) >> FLAG_WIDTH);
49b1994897Sopenharmony_ci    }
50b1994897Sopenharmony_ci
51b1994897Sopenharmony_ci    uint16_t GetHeaderIndex() const
52b1994897Sopenharmony_ci    {
53b1994897Sopenharmony_ci        return header_index_;
54b1994897Sopenharmony_ci    }
55b1994897Sopenharmony_ci
56b1994897Sopenharmony_ci    uint32_t GetNumHeaders() const
57b1994897Sopenharmony_ci    {
58b1994897Sopenharmony_ci        return num_headers_;
59b1994897Sopenharmony_ci    }
60b1994897Sopenharmony_ci
61b1994897Sopenharmony_ciprivate:
62b1994897Sopenharmony_ci    Span<const File::EntityId> indexes_;
63b1994897Sopenharmony_ci    uint32_t access_flags_;
64b1994897Sopenharmony_ci    uint16_t header_index_;
65b1994897Sopenharmony_ci    uint32_t num_headers_;
66b1994897Sopenharmony_ci};
67b1994897Sopenharmony_ci
68b1994897Sopenharmony_ci}  // namespace panda::panda_file
69b1994897Sopenharmony_ci
70b1994897Sopenharmony_ci#endif  // LIBPANDAFILE_INDEX_ACCESSOR_H
71