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
16 #include "annotationProto.h"
17
18 namespace panda::proto {
Serialize(const panda::pandasm::AnnotationData & anno,protoPanda::AnnotationData & protoAnno)19 void AnnotationData::Serialize(const panda::pandasm::AnnotationData &anno, protoPanda::AnnotationData &protoAnno)
20 {
21 protoAnno.set_recordname(anno.GetName());
22 for (const auto &element : anno.GetElements()) {
23 auto *protoElement = protoAnno.add_elements();
24 AnnotationElement::Serialize(element, *protoElement);
25 }
26 }
27
Deserialize(const protoPanda::AnnotationData & protoAnno,panda::pandasm::AnnotationData & anno,panda::ArenaAllocator * allocator)28 void AnnotationData::Deserialize(const protoPanda::AnnotationData &protoAnno, panda::pandasm::AnnotationData &anno,
29 panda::ArenaAllocator *allocator)
30 {
31 for (const auto &protoElement : protoAnno.elements()) {
32 panda::pandasm::AnnotationElement &element = AnnotationElement::Deserialize(protoElement, allocator);
33 anno.AddElement(std::move(element));
34 }
35 }
36
Serialize(const panda::pandasm::AnnotationElement & element,protoPanda::AnnotationElement & protoElement)37 void AnnotationElement::Serialize(const panda::pandasm::AnnotationElement &element,
38 protoPanda::AnnotationElement &protoElement)
39 {
40 protoElement.set_name(element.GetName());
41 if (element.GetValue()->IsArray()) {
42 protoElement.set_valuetype(protoPanda::AnnotationElement_ValueType::AnnotationElement_ValueType_ARRAY);
43 auto *protoArray = protoElement.mutable_array();
44 ArrayValue::Serialize(*(element.GetValue()->GetAsArray()), *protoArray);
45 } else {
46 protoElement.set_valuetype(protoPanda::AnnotationElement_ValueType::AnnotationElement_ValueType_SCALAR);
47 auto *protoScalar = protoElement.mutable_scalar();
48 ScalarValue::Serialize(*(element.GetValue()->GetAsScalar()), *protoScalar);
49 }
50 }
51
Deserialize(const protoPanda::AnnotationElement & protoElement,panda::ArenaAllocator * allocator)52 panda::pandasm::AnnotationElement &AnnotationElement::Deserialize(const protoPanda::AnnotationElement &protoElement,
53 panda::ArenaAllocator *allocator)
54 {
55 if (protoElement.valuetype() == protoPanda::AnnotationElement_ValueType::AnnotationElement_ValueType_ARRAY) {
56 panda::pandasm::ArrayValue &array = ArrayValue::Deserialize(protoElement.array(), allocator);
57 auto *element = allocator->New<panda::pandasm::AnnotationElement>(protoElement.name(),
58 std::make_unique<panda::pandasm::ArrayValue>(array));
59 return *element;
60 }
61 panda::pandasm::ScalarValue scalar = ScalarValue::Deserialize(protoElement.scalar(), allocator);
62 auto *element = allocator->New<panda::pandasm::AnnotationElement>(protoElement.name(),
63 std::make_unique<panda::pandasm::ScalarValue>(scalar));
64 return *element;
65 }
66
Serialize(const panda::pandasm::ScalarValue & scalar,protoPanda::ScalarValue & protoScalar)67 void ScalarValue::Serialize(const panda::pandasm::ScalarValue &scalar, protoPanda::ScalarValue &protoScalar)
68 {
69 const auto &valueType = scalar.GetType();
70 protoScalar.mutable_father()->set_type(static_cast<uint32_t>(valueType));
71 auto type = protoPanda::ScalarValue_VariantValueType::ScalarValue_VariantValueType_UINT64;
72 switch (valueType) {
73 case panda::pandasm::Value::Type::U1:
74 case panda::pandasm::Value::Type::U8:
75 protoScalar.set_valueu64(static_cast<uint64_t>(scalar.GetValue<uint8_t>()));
76 break;
77 case panda::pandasm::Value::Type::U16:
78 protoScalar.set_valueu64(static_cast<uint64_t>(scalar.GetValue<uint16_t>()));
79 break;
80 case panda::pandasm::Value::Type::STRING_NULLPTR:
81 case panda::pandasm::Value::Type::U32:
82 protoScalar.set_valueu64(static_cast<uint64_t>(scalar.GetValue<uint32_t>()));
83 break;
84 case panda::pandasm::Value::Type::U64:
85 protoScalar.set_valueu64(scalar.GetValue<uint64_t>());
86 break;
87 case panda::pandasm::Value::Type::I8:
88 protoScalar.set_valueu64(static_cast<uint64_t>(scalar.GetValue<int8_t>()));
89 break;
90 case panda::pandasm::Value::Type::I16:
91 protoScalar.set_valueu64(static_cast<uint64_t>(scalar.GetValue<int16_t>()));
92 break;
93 case panda::pandasm::Value::Type::I32:
94 protoScalar.set_valueu64(static_cast<uint64_t>(scalar.GetValue<int32_t>()));
95 break;
96 case panda::pandasm::Value::Type::I64:
97 protoScalar.set_valueu64(static_cast<uint64_t>(scalar.GetValue<int64_t>()));
98 break;
99 case panda::pandasm::Value::Type::F32:
100 type = protoPanda::ScalarValue_VariantValueType::ScalarValue_VariantValueType_FLOAT;
101 protoScalar.set_valuefloat(scalar.GetValue<float>());
102 break;
103 case panda::pandasm::Value::Type::F64:
104 type = protoPanda::ScalarValue_VariantValueType::ScalarValue_VariantValueType_DOUBLE;
105 protoScalar.set_valuedouble(scalar.GetValue<double>());
106 break;
107 case panda::pandasm::Value::Type::STRING:
108 case panda::pandasm::Value::Type::METHOD:
109 case panda::pandasm::Value::Type::ENUM:
110 case panda::pandasm::Value::Type::LITERALARRAY:
111 type = protoPanda::ScalarValue_VariantValueType::ScalarValue_VariantValueType_STRING;
112 protoScalar.set_valuestr(scalar.GetValue<std::string>());
113 break;
114 case panda::pandasm::Value::Type::RECORD: {
115 type = protoPanda::ScalarValue_VariantValueType::ScalarValue_VariantValueType_PANDASM_TYPE;
116 auto *protoType = protoScalar.mutable_valuetype();
117 Type::Serialize(scalar.GetValue<panda::pandasm::Type>(), *protoType);
118 break;
119 }
120 case panda::pandasm::Value::Type::ANNOTATION: {
121 type = protoPanda::ScalarValue_VariantValueType::ScalarValue_VariantValueType_ANNOTATION_DATA;
122 auto *protoAnno = protoScalar.mutable_valueanno();
123 AnnotationData::Serialize(scalar.GetValue<panda::pandasm::AnnotationData>(), *protoAnno);
124 break;
125 }
126 default:
127 std::cerr << "unknown panda::pandasm::Value::Type" << std::endl;
128 UNREACHABLE();
129 }
130 protoScalar.set_type(type);
131 }
132
Deserialize(const protoPanda::ScalarValue & protoScalar,panda::ArenaAllocator * allocator)133 panda::pandasm::ScalarValue ScalarValue::Deserialize(const protoPanda::ScalarValue &protoScalar,
134 panda::ArenaAllocator *allocator)
135 {
136 protoPanda::ScalarValue_VariantValueType scalarType = protoScalar.type();
137 std::variant<uint64_t, float, double, std::string, panda::pandasm::Type, panda::pandasm::AnnotationData> value;
138 switch (scalarType) {
139 case protoPanda::ScalarValue_VariantValueType::ScalarValue_VariantValueType_UINT64: {
140 value = static_cast<uint64_t>(protoScalar.valueu64());
141 break;
142 }
143 case protoPanda::ScalarValue_VariantValueType::ScalarValue_VariantValueType_FLOAT: {
144 value = static_cast<float>(protoScalar.valuefloat());
145 break;
146 }
147 case protoPanda::ScalarValue_VariantValueType::ScalarValue_VariantValueType_DOUBLE: {
148 value = static_cast<double>(protoScalar.valuedouble());
149 break;
150 }
151 case protoPanda::ScalarValue_VariantValueType::ScalarValue_VariantValueType_STRING: {
152 value = static_cast<std::string>(protoScalar.valuestr());
153 break;
154 }
155 case protoPanda::ScalarValue_VariantValueType::ScalarValue_VariantValueType_PANDASM_TYPE: {
156 value = static_cast<panda::pandasm::Type>(Type::Deserialize(protoScalar.valuetype(), allocator));
157 break;
158 }
159 case protoPanda::ScalarValue_VariantValueType::ScalarValue_VariantValueType_ANNOTATION_DATA: {
160 auto &protoAnnotationData = protoScalar.valueanno();
161 auto *value = allocator->New<panda::pandasm::AnnotationData>(protoAnnotationData.recordname());
162 AnnotationData::Deserialize(protoAnnotationData, *value, allocator);
163 break;
164 }
165 default:
166 UNREACHABLE();
167 }
168 auto scalar = ScalarValue::CreateScalarValue(static_cast<panda::pandasm::Value::Type>(
169 protoScalar.father().type()), value);
170 return scalar;
171 }
172
CreateScalarValue(const panda::pandasm::Value::Type & type,std::variant<uint64_t,float,double,std::string,panda::pandasm::Type,panda::pandasm::AnnotationData> & value)173 panda::pandasm::ScalarValue ScalarValue::CreateScalarValue(const panda::pandasm::Value::Type &type,
174 std::variant<uint64_t, float, double, std::string, panda::pandasm::Type, panda::pandasm::AnnotationData> &value)
175 {
176 switch (type) {
177 case panda::pandasm::Value::Type::U1: {
178 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::U1>(static_cast<uint8_t>(
179 std::get<uint64_t>(value)));
180 }
181 case panda::pandasm::Value::Type::U8: {
182 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::U8>(static_cast<uint8_t>(
183 std::get<uint64_t>(value)));
184 }
185 case panda::pandasm::Value::Type::U16: {
186 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::U16>(static_cast<uint16_t>(
187 std::get<uint64_t>(value)));
188 }
189 case panda::pandasm::Value::Type::STRING_NULLPTR: {
190 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::STRING_NULLPTR>(
191 static_cast<uint32_t>(std::get<uint64_t>(value)));
192 }
193 case panda::pandasm::Value::Type::U32: {
194 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::U32>(static_cast<uint32_t>(
195 std::get<uint64_t>(value)));
196 }
197 case panda::pandasm::Value::Type::U64: {
198 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::U64>(static_cast<uint64_t>(
199 std::get<uint64_t>(value)));
200 }
201 case panda::pandasm::Value::Type::I8: {
202 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::I8>(static_cast<int8_t>(
203 std::get<uint64_t>(value)));
204 }
205 case panda::pandasm::Value::Type::I16: {
206 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::I16>(static_cast<int16_t>(
207 std::get<uint64_t>(value)));
208 }
209 case panda::pandasm::Value::Type::I32: {
210 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::I32>(static_cast<int32_t>(
211 std::get<uint64_t>(value)));
212 }
213 case panda::pandasm::Value::Type::I64: {
214 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::I64>(static_cast<int64_t>(
215 std::get<uint64_t>(value)));
216 }
217 case panda::pandasm::Value::Type::F32: {
218 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::F32>(std::get<float>(value));
219 }
220 case panda::pandasm::Value::Type::F64: {
221 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::F64>(std::get<double>(value));
222 }
223 case panda::pandasm::Value::Type::STRING: {
224 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::STRING>(
225 std::get<std::string>(value));
226 }
227 case panda::pandasm::Value::Type::METHOD: {
228 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::METHOD>(
229 std::get<std::string>(value));
230 }
231 case panda::pandasm::Value::Type::ENUM: {
232 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::ENUM>(std::get<std::string>(value));
233 }
234 case panda::pandasm::Value::Type::RECORD: {
235 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::RECORD>(
236 std::get<panda::pandasm::Type>(value));
237 }
238 case panda::pandasm::Value::Type::ANNOTATION: {
239 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::ANNOTATION>(
240 std::get<panda::pandasm::AnnotationData>(value));
241 }
242 case panda::pandasm::Value::Type::LITERALARRAY: {
243 return panda::pandasm::ScalarValue::Create<panda::pandasm::Value::Type::LITERALARRAY>(
244 std::get<std::string>(value));
245 }
246 default:
247 UNREACHABLE();
248 }
249 }
250
Serialize(const panda::pandasm::ArrayValue & array,protoPanda::ArrayValue & protoArray)251 void ArrayValue::Serialize(const panda::pandasm::ArrayValue &array, protoPanda::ArrayValue &protoArray)
252 {
253 protoArray.mutable_father()->set_type(static_cast<uint32_t>(array.GetType()));
254 protoArray.set_componenttype(static_cast<uint32_t>(array.GetComponentType()));
255 for (const auto &val : array.GetValues()) {
256 auto *protoScalar = protoArray.add_values();
257 ScalarValue::Serialize(val, *protoScalar);
258 }
259 }
260
Deserialize(const protoPanda::ArrayValue & protoArray,panda::ArenaAllocator * allocator)261 panda::pandasm::ArrayValue &ArrayValue::Deserialize(const protoPanda::ArrayValue &protoArray,
262 panda::ArenaAllocator *allocator)
263 {
264 std::vector<panda::pandasm::ScalarValue> values;
265 values.reserve(protoArray.values_size());
266 for (const auto &protoValue : protoArray.values()) {
267 panda::pandasm::ScalarValue scalar = ScalarValue::Deserialize(protoValue, allocator);
268 values.emplace_back(std::move(scalar));
269 }
270 auto *array = allocator->New<panda::pandasm::ArrayValue>(
271 static_cast<panda::pandasm::Value::Type>(protoArray.componenttype()), values);
272 return *array;
273 }
274 } // panda::proto
275