• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "abc_literal_array_processor.h"
17 #include "abc2program_log.h"
18 #include "method_data_accessor-inl.h"
19 
20 namespace panda::abc2program {
21 
AbcLiteralArrayProcessor(panda_file::File::EntityId entity_id,Abc2ProgramEntityContainer & entity_container,panda_file::LiteralDataAccessor & literal_data_accessor)22 AbcLiteralArrayProcessor::AbcLiteralArrayProcessor(panda_file::File::EntityId entity_id,
23                                                    Abc2ProgramEntityContainer &entity_container,
24                                                    panda_file::LiteralDataAccessor &literal_data_accessor)
25     : AbcFileEntityProcessor(entity_id, entity_container), literal_data_accessor_(literal_data_accessor) {}
26 
FillProgramData()27 void AbcLiteralArrayProcessor::FillProgramData()
28 {
29     GetLiteralArrayById(&literal_array_, entity_id_);
30     program_->literalarray_table.emplace(entity_container_.GetLiteralArrayIdName(entity_id_.GetOffset()),
31                                          std::move(literal_array_));
32 }
33 
FillModuleRequestPhase()34 void AbcLiteralArrayProcessor::FillModuleRequestPhase()
35 {
36     auto sp = file_->GetSpanFromId(entity_id_);
37     auto literal_vals_num = panda_file::helpers::Read<sizeof(uint32_t)>(&sp);
38     for (size_t i = 0; i < literal_vals_num; i++) {
39         pandasm::LiteralArray::Literal lit;
40         lit.tag_ = panda_file::LiteralTag::INTEGER_8;
41         lit.value_ = static_cast<uint8_t>(panda_file::helpers::Read<sizeof(uint8_t)>(&sp));
42         literal_array_.literals_.emplace_back(lit);
43     }
44     program_->literalarray_table.emplace(entity_container_.GetLiteralArrayIdName(entity_id_.GetOffset()),
45                                          std::move(literal_array_));
46 }
47 
GetLiteralArrayById(pandasm::LiteralArray * lit_array,panda_file::File::EntityId lit_array_id) const48 void AbcLiteralArrayProcessor::GetLiteralArrayById(pandasm::LiteralArray *lit_array,
49                                                    panda_file::File::EntityId lit_array_id) const
50 {
51     literal_data_accessor_.EnumerateLiteralVals(
52         lit_array_id, [this, lit_array](
53                     const panda_file::LiteralDataAccessor::LiteralValue &value,
54                     const panda_file::LiteralTag &tag) {
55             FillLiteralData(lit_array, value, tag);
56         });
57 }
58 
FillLiteralData(pandasm::LiteralArray * lit_array,const panda_file::LiteralDataAccessor::LiteralValue & value,const panda_file::LiteralTag & tag) const59 void AbcLiteralArrayProcessor::FillLiteralData(pandasm::LiteralArray *lit_array,
60                                                const panda_file::LiteralDataAccessor::LiteralValue &value,
61                                                const panda_file::LiteralTag &tag) const
62 {
63     pandasm::LiteralArray::Literal value_lit;
64     pandasm::LiteralArray::Literal tag_lit;
65     value_lit.tag_ = tag;
66     switch (tag) {
67         case panda_file::LiteralTag::BOOL:
68             value_lit.value_ = std::get<bool>(value);
69             break;
70         case panda_file::LiteralTag::ACCESSOR:
71         case panda_file::LiteralTag::NULLVALUE:
72         case panda_file::LiteralTag::BUILTINTYPEINDEX:
73             value_lit.value_ = std::get<uint8_t>(value);
74             break;
75         case panda_file::LiteralTag::METHODAFFILIATE:
76             value_lit.value_ = std::get<uint16_t>(value);
77             break;
78         case panda_file::LiteralTag::LITERALBUFFERINDEX:
79         case panda_file::LiteralTag::INTEGER:
80             value_lit.value_ = std::get<uint32_t>(value);
81             break;
82         case panda_file::LiteralTag::DOUBLE:
83             value_lit.value_ = std::get<double>(value);
84             break;
85         case panda_file::LiteralTag::STRING:
86             value_lit.value_ = GetStringById(panda_file::File::EntityId{std::get<uint32_t>(value)});
87             break;
88         case panda_file::LiteralTag::METHOD:
89         case panda_file::LiteralTag::GETTER:
90         case panda_file::LiteralTag::SETTER:
91         case panda_file::LiteralTag::GENERATORMETHOD:
92         case panda_file::LiteralTag::ASYNCGENERATORMETHOD: {
93             panda_file::File::EntityId entity_id(std::get<uint32_t>(value));
94             value_lit.value_ = entity_container_.GetFullMethodNameById(entity_id);
95             break;
96         }
97         case panda_file::LiteralTag::LITERALARRAY:
98             value_lit.value_ = entity_container_.GetLiteralArrayIdName(std::get<uint32_t>(value));
99             entity_container_.TryAddUnprocessedNestedLiteralArrayId(std::get<uint32_t>(value));
100             break;
101         case panda_file::LiteralTag::TAGVALUE:
102             value_lit.value_ = std::get<uint8_t>(value);
103             break;
104         default:
105             UNREACHABLE();
106     }
107 
108     tag_lit.tag_ = panda_file::LiteralTag::TAGVALUE;
109     tag_lit.value_ = static_cast<uint8_t>(value_lit.tag_);
110 
111     lit_array->literals_.emplace_back(tag_lit);
112     lit_array->literals_.emplace_back(value_lit);
113 }
114 
115 } // namespace panda::abc2program
116