1 /**
2 * Copyright (c) 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 #include "metaProto.h"
16
17 namespace panda::proto {
Serialize(const panda::pandasm::RecordMetadata & meta,protoPanda::RecordMetadata & protoMeta)18 void RecordMetadata::Serialize(const panda::pandasm::RecordMetadata &meta, protoPanda::RecordMetadata &protoMeta)
19 {
20 auto *protoItemmetadata = protoMeta.mutable_father();
21 ItemMetadata::Serialize(static_cast<const panda::pandasm::ItemMetadata &>(meta), *protoItemmetadata);
22 }
23
Deserialize(const protoPanda::RecordMetadata & protoMeta,std::unique_ptr<panda::pandasm::RecordMetadata> & meta,panda::ArenaAllocator * allocator)24 void RecordMetadata::Deserialize(const protoPanda::RecordMetadata &protoMeta,
25 std::unique_ptr<panda::pandasm::RecordMetadata> &meta,
26 panda::ArenaAllocator *allocator)
27 {
28 auto &protoItemMetadata = protoMeta.father();
29 ItemMetadata::Deserialize(protoItemMetadata, *meta);
30
31 auto &protoAnnoMetadata = protoItemMetadata.father();
32 AnnotationMetadata::Deserialize(protoAnnoMetadata, *meta, allocator);
33
34 const auto &protoMetadata = protoAnnoMetadata.father();
35 Metadata::Deserialize(protoMetadata, *meta);
36 }
37
Serialize(const panda::pandasm::FunctionMetadata & meta,protoPanda::FunctionMetadata & protoMeta)38 void FunctionMetadata::Serialize(const panda::pandasm::FunctionMetadata &meta,
39 protoPanda::FunctionMetadata &protoMeta)
40 {
41 auto *protoItemmetadata = protoMeta.mutable_father();
42 ItemMetadata::Serialize(static_cast<const panda::pandasm::ItemMetadata &>(meta), *protoItemmetadata);
43 }
44
Deserialize(const protoPanda::FunctionMetadata & protoMeta,std::unique_ptr<panda::pandasm::FunctionMetadata> & meta,panda::ArenaAllocator * allocator)45 void FunctionMetadata::Deserialize(const protoPanda::FunctionMetadata &protoMeta,
46 std::unique_ptr<panda::pandasm::FunctionMetadata> &meta,
47 panda::ArenaAllocator *allocator)
48 {
49 auto &protoItemMetadata = protoMeta.father();
50 ItemMetadata::Deserialize(protoItemMetadata, *meta);
51
52 auto &protoAnnoMetadata = protoItemMetadata.father();
53 AnnotationMetadata::Deserialize(protoAnnoMetadata, *meta, allocator);
54
55 const auto &protoMetadata = protoAnnoMetadata.father();
56 Metadata::Deserialize(protoMetadata, *meta);
57 }
58
Serialize(const panda::pandasm::FieldMetadata & meta,protoPanda::FieldMetadata & protoMeta)59 void FieldMetadata::Serialize(const panda::pandasm::FieldMetadata &meta, protoPanda::FieldMetadata &protoMeta)
60 {
61 auto *protoItemmetadata = protoMeta.mutable_father();
62 ItemMetadata::Serialize(meta, *protoItemmetadata);
63 auto *protoType = protoMeta.mutable_fieldtype();
64 Type::Serialize(meta.GetFieldType(), *protoType);
65 const auto val = meta.GetValue();
66 if (val.has_value()) {
67 auto *protoValue = protoMeta.mutable_value();
68 ScalarValue::Serialize(val.value(), *protoValue);
69 }
70 }
71
Deserialize(const protoPanda::FieldMetadata & protoMeta,std::unique_ptr<panda::pandasm::FieldMetadata> & meta,panda::ArenaAllocator * allocator)72 void FieldMetadata::Deserialize(const protoPanda::FieldMetadata &protoMeta,
73 std::unique_ptr<panda::pandasm::FieldMetadata> &meta,
74 panda::ArenaAllocator *allocator)
75 {
76 auto &protoItemMetadata = protoMeta.father();
77 ItemMetadata::Deserialize(protoItemMetadata, *meta);
78 auto &protoAnnoMetadata = protoItemMetadata.father();
79 AnnotationMetadata::Deserialize(protoAnnoMetadata, *meta, allocator);
80 const auto &protoMetadata = protoAnnoMetadata.father();
81 Metadata::Deserialize(protoMetadata, *meta);
82
83 auto &fieldType = Type::Deserialize(protoMeta.fieldtype(), allocator);
84 meta->SetFieldType(fieldType);
85 ScalarValue scalarValue;
86 if (protoMeta.has_value()) {
87 auto scalar = scalarValue.Deserialize(protoMeta.value(), allocator);
88 meta->SetValue(scalar);
89 }
90 }
91
Serialize(const panda::pandasm::ParamMetadata & meta,protoPanda::ParamMetadata & protoMeta)92 void ParamMetadata::Serialize(const panda::pandasm::ParamMetadata &meta, protoPanda::ParamMetadata &protoMeta)
93 {
94 auto *protoAnnometadata = protoMeta.mutable_father();
95 AnnotationMetadata::Serialize(static_cast<const panda::pandasm::AnnotationMetadata &>(meta), *protoAnnometadata);
96 }
97
Deserialize(const protoPanda::ParamMetadata & protoMeta,std::unique_ptr<panda::pandasm::ParamMetadata> & meta,panda::ArenaAllocator * allocator)98 void ParamMetadata::Deserialize(const protoPanda::ParamMetadata &protoMeta,
99 std::unique_ptr<panda::pandasm::ParamMetadata> &meta,
100 panda::ArenaAllocator *allocator)
101 {
102 const auto &protoAnnoMetadata = protoMeta.father();
103 AnnotationMetadata::Deserialize(protoAnnoMetadata, *meta, allocator);
104 }
105
Serialize(const panda::pandasm::ItemMetadata & meta,protoPanda::ItemMetadata & protoMeta)106 void ItemMetadata::Serialize(const panda::pandasm::ItemMetadata &meta, protoPanda::ItemMetadata &protoMeta)
107 {
108 auto *protoAnnometadata = protoMeta.mutable_father();
109 AnnotationMetadata::Serialize(static_cast<const panda::pandasm::AnnotationMetadata &>(meta), *protoAnnometadata);
110 protoMeta.set_accessflags(meta.GetAccessFlags());
111 }
112
Deserialize(const protoPanda::ItemMetadata & protoMeta,panda::pandasm::ItemMetadata & meta)113 void ItemMetadata::Deserialize(const protoPanda::ItemMetadata &protoMeta, panda::pandasm::ItemMetadata &meta)
114 {
115 meta.SetAccessFlags(protoMeta.accessflags());
116 }
117
Serialize(const panda::pandasm::AnnotationMetadata & meta,protoPanda::AnnotationMetadata & protoMeta)118 void AnnotationMetadata::Serialize(const panda::pandasm::AnnotationMetadata &meta,
119 protoPanda::AnnotationMetadata &protoMeta)
120 {
121 auto *protoMetadata = protoMeta.mutable_father();
122 Metadata::Serialize(static_cast<const panda::pandasm::Metadata &>(meta), *protoMetadata);
123 for (const auto &anno : meta.GetAnnotations()) {
124 auto *proto_anno = protoMeta.add_annotations();
125 AnnotationData::Serialize(anno, *proto_anno);
126 }
127 }
128
Deserialize(const protoPanda::AnnotationMetadata & protoMeta,panda::pandasm::AnnotationMetadata & meta,panda::ArenaAllocator * allocator)129 void AnnotationMetadata::Deserialize(const protoPanda::AnnotationMetadata &protoMeta,
130 panda::pandasm::AnnotationMetadata &meta,
131 panda::ArenaAllocator *allocator)
132 {
133 std::vector<panda::pandasm::AnnotationData> annotations;
134 annotations.reserve(protoMeta.annotations_size());
135 for (const auto &protoAnnotation : protoMeta.annotations()) {
136 auto *annotation = allocator->New<panda::pandasm::AnnotationData>(protoAnnotation.recordname());
137 AnnotationData::Deserialize(protoAnnotation, *annotation, allocator);
138 annotations.emplace_back(std::move(*annotation));
139 }
140 meta.AddAnnotations(annotations);
141 }
142
Serialize(const panda::pandasm::Metadata & meta,protoPanda::Metadata & protoMeta)143 void Metadata::Serialize(const panda::pandasm::Metadata &meta, protoPanda::Metadata &protoMeta)
144 {
145 for (const auto &attr : meta.GetBoolAttributes()) {
146 protoMeta.add_set_attributes(attr);
147 }
148 for (const auto &[name, attrs] : meta.GetAttributes()) {
149 auto *protoKeyVal = protoMeta.add_attributes();
150 protoKeyVal->set_key(name);
151 for (const auto &attr : attrs) {
152 protoKeyVal->add_value(attr);
153 }
154 }
155 }
156
Deserialize(const protoPanda::Metadata & protoMeta,panda::pandasm::Metadata & meta)157 void Metadata::Deserialize(const protoPanda::Metadata &protoMeta, panda::pandasm::Metadata &meta)
158 {
159 for (const auto &attr : protoMeta.set_attributes()) {
160 meta.SetAttribute(attr);
161 }
162 for (const auto &protoKeyVal: protoMeta.attributes()) {
163 auto &key = protoKeyVal.key();
164 for (const auto &attr : protoKeyVal.value()) {
165 meta.SetAttributeValue(key, attr);
166 }
167 }
168 }
169 } // panda::proto
170