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 #include "field_data_accessor.h"
17
18 namespace panda::panda_file {
19
FieldDataAccessor(const File & panda_file,File::EntityId field_id)20 FieldDataAccessor::FieldDataAccessor(const File &panda_file, File::EntityId field_id)
21 : panda_file_(panda_file), field_id_(field_id)
22 {
23 auto sp = panda_file_.GetSpanFromId(field_id_);
24
25 auto class_idx = helpers::Read<IDX_SIZE>(&sp);
26 auto type_idx = helpers::Read<IDX_SIZE>(&sp);
27
28 class_off_ = panda_file.ResolveClassIndex(field_id, class_idx).GetOffset();
29 type_off_ = panda_file.ResolveClassIndex(field_id, type_idx).GetOffset();
30
31 name_off_ = helpers::Read<ID_SIZE>(&sp);
32
33 is_external_ = panda_file_.IsExternal(field_id_);
34
35 if (!is_external_) {
36 access_flags_ = helpers::ReadULeb128(&sp);
37 tagged_values_sp_ = sp;
38 size_ = 0;
39 } else {
40 access_flags_ = 0;
41 size_ = panda_file_.GetIdFromPointer(sp.data()).GetOffset() - field_id_.GetOffset();
42 }
43 }
44
GetValueInternal()45 std::optional<FieldDataAccessor::FieldValue> FieldDataAccessor::GetValueInternal()
46 {
47 panda_file_.ThrowIfWithCheck(tagged_values_sp_.Size() == 0U, File::INVALID_FILE_OFFSET, File::FIELD_DATA_ACCESSOR);
48
49 auto sp = tagged_values_sp_;
50 auto tag = static_cast<FieldTag>(sp[0]);
51 FieldValue value;
52
53 if (tag == FieldTag::INT_VALUE) {
54 panda_file_.ThrowIfWithCheck(sp.Size() == 0U, File::INVALID_FILE_OFFSET, File::FIELD_DATA_ACCESSOR);
55 sp = sp.SubSpan(1);
56 value = static_cast<uint32_t>(helpers::ReadLeb128(&sp));
57 } else if (tag == FieldTag::VALUE) {
58 panda_file_.ThrowIfWithCheck(sp.Size() == 0U, File::INVALID_FILE_OFFSET, File::FIELD_DATA_ACCESSOR);
59 sp = sp.SubSpan(1);
60
61 switch (GetType()) {
62 case Type(Type::TypeId::F32).GetFieldEncoding(): {
63 value = static_cast<uint32_t>(helpers::Read<sizeof(uint32_t)>(&sp));
64 break;
65 }
66 case Type(Type::TypeId::I64).GetFieldEncoding():
67 case Type(Type::TypeId::U64).GetFieldEncoding():
68 case Type(Type::TypeId::F64).GetFieldEncoding(): {
69 auto offset = static_cast<uint32_t>(helpers::Read<sizeof(uint32_t)>(&sp));
70 auto value_sp = panda_file_.GetSpanFromId(File::EntityId(offset));
71 value = static_cast<uint64_t>(helpers::Read<sizeof(uint64_t)>(value_sp));
72 break;
73 }
74 default: {
75 value = static_cast<uint32_t>(helpers::Read<sizeof(uint32_t)>(&sp));
76 break;
77 }
78 }
79 }
80
81 runtime_annotations_sp_ = sp;
82
83 if (tag == FieldTag::INT_VALUE || tag == FieldTag::VALUE) {
84 return value;
85 }
86
87 return {};
88 }
89
90 } // namespace panda::panda_file
91