• 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_annotation_processor.h"
17 #include "abc2program_log.h"
18 
19 namespace panda::abc2program {
20 
AbcAnnotationProcessor(panda_file::File::EntityId entity_id,Abc2ProgramEntityContainer & entity_container,pandasm::Function & function)21 AbcAnnotationProcessor::AbcAnnotationProcessor(panda_file::File::EntityId entity_id,
22                                                Abc2ProgramEntityContainer &entity_container,
23                                                pandasm::Function &function)
24     : AbcFileEntityProcessor(entity_id, entity_container), function_(function)
25 {
26     annotation_data_accessor_ = std::make_unique<panda_file::AnnotationDataAccessor>(*file_, entity_id_);
27     auto type_descriptor_name = GetStringById(annotation_data_accessor_->GetClassId());
28     annotation_name_ = pandasm::Type::FromDescriptor(type_descriptor_name).GetName();
29 }
30 
FillProgramData()31 void AbcAnnotationProcessor::FillProgramData()
32 {
33     FillAnnotation();
34 }
35 
FillAnnotation()36 void AbcAnnotationProcessor::FillAnnotation()
37 {
38     std::vector<pandasm::AnnotationElement> elements;
39     FillAnnotationElements(elements);
40     pandasm::AnnotationData annotation_data(annotation_name_, elements);
41     std::vector<pandasm::AnnotationData> annotations;
42     annotations.emplace_back(std::move(annotation_data));
43     function_.metadata->AddAnnotations(annotations);
44 }
45 
FillAnnotationElements(std::vector<pandasm::AnnotationElement> & elements)46 void AbcAnnotationProcessor::FillAnnotationElements(std::vector<pandasm::AnnotationElement> &elements)
47 {
48     for (uint32_t i = 0; i < annotation_data_accessor_->GetCount(); ++i) {
49         auto annotation_data_accessor_elem = annotation_data_accessor_->GetElement(i);
50         auto annotation_elem_name = GetStringById(annotation_data_accessor_elem.GetNameId());
51         auto value = annotation_data_accessor_elem.GetScalarValue().GetValue();
52         auto value_type = pandasm::Value::GetCharAsType(annotation_data_accessor_->GetTag(i).GetItem());
53         switch (value_type) {
54             case pandasm::Value::Type::U32: {
55                 pandasm::AnnotationElement annotation_element(
56                     annotation_elem_name, std::make_unique<pandasm::ScalarValue>(
57                     pandasm::ScalarValue::Create<pandasm::Value::Type::U32>(value)));
58                 elements.emplace_back(annotation_element);
59                 break;
60             }
61             default:
62                 UNREACHABLE();
63         }
64     }
65 }
66 
67 }  // namespace panda::abc2program
68