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 "assemblyLiteralsProto.h"
17
18 namespace panda::proto {
Serialize(const LiteralValueType & value,protoPanda::VariantValue & protoValue)19 void VariantValue::Serialize(const LiteralValueType &value, protoPanda::VariantValue &protoValue)
20 {
21 const auto type = static_cast<protoPanda::VariantValue_VariantValueType>(value.index());
22 protoValue.set_type(type);
23 switch (type) {
24 case protoPanda::VariantValue_VariantValueType_BOOL: {
25 protoValue.set_valueint(static_cast<uint64_t>(std::get<bool>(value)));
26 return;
27 }
28 case protoPanda::VariantValue_VariantValueType_U8: {
29 protoValue.set_valueint(static_cast<uint64_t>(std::get<uint8_t>(value)));
30 return;
31 }
32 case protoPanda::VariantValue_VariantValueType_U16: {
33 protoValue.set_valueint(static_cast<uint64_t>(std::get<uint16_t>(value)));
34 return;
35 }
36 case protoPanda::VariantValue_VariantValueType_U32: {
37 protoValue.set_valueint(static_cast<uint64_t>(std::get<uint32_t>(value)));
38 return;
39 }
40 case protoPanda::VariantValue_VariantValueType_U64: {
41 protoValue.set_valueint(std::get<uint64_t>(value));
42 return;
43 }
44 case protoPanda::VariantValue_VariantValueType_F32: {
45 protoValue.set_valuefloat(std::get<float>(value));
46 return;
47 }
48 case protoPanda::VariantValue_VariantValueType_F64: {
49 protoValue.set_valuedouble(std::get<double>(value));
50 return;
51 }
52 case protoPanda::VariantValue_VariantValueType_STRING: {
53 protoValue.set_valuestr(std::get<std::string>(value));
54 return;
55 }
56 default:
57 UNREACHABLE();
58 }
59 }
60
Deserialize(const protoPanda::VariantValue & protoValue,LiteralValueType & value)61 void VariantValue::Deserialize(const protoPanda::VariantValue &protoValue, LiteralValueType &value)
62 {
63 auto type = protoValue.type();
64 switch (type) {
65 case protoPanda::VariantValue_VariantValueType_BOOL: {
66 value = static_cast<bool>(protoValue.valueint());
67 return;
68 }
69 case protoPanda::VariantValue_VariantValueType_U8: {
70 value = static_cast<uint8_t>(protoValue.valueint());
71 return;
72 }
73 case protoPanda::VariantValue_VariantValueType_U16: {
74 value = static_cast<uint16_t>(protoValue.valueint());
75 return;
76 }
77 case protoPanda::VariantValue_VariantValueType_U32: {
78 value = static_cast<uint32_t>(protoValue.valueint());
79 return;
80 }
81 case protoPanda::VariantValue_VariantValueType_U64: {
82 value = static_cast<uint64_t>(protoValue.valueint());
83 return;
84 }
85 case protoPanda::VariantValue_VariantValueType_F32: {
86 value = protoValue.valuefloat();
87 return;
88 }
89 case protoPanda::VariantValue_VariantValueType_F64: {
90 value = protoValue.valuedouble();
91 return;
92 }
93 case protoPanda::VariantValue_VariantValueType_STRING: {
94 value = protoValue.valuestr();
95 return;
96 }
97 default:
98 UNREACHABLE();
99 }
100 }
101
Serialize(const panda::pandasm::LiteralArray & array,protoPanda::LiteralArray & protoArray)102 void LiteralArray::Serialize(const panda::pandasm::LiteralArray &array, protoPanda::LiteralArray &protoArray)
103 {
104 for (const auto &literal : array.literals_) {
105 auto *protoLiteral = protoArray.add_literals();
106 Literal::Serialize(literal, *protoLiteral);
107 }
108 }
109
Deserialize(const protoPanda::LiteralArray & protoArray,panda::pandasm::LiteralArray & array)110 void LiteralArray::Deserialize(const protoPanda::LiteralArray &protoArray, panda::pandasm::LiteralArray &array)
111 {
112 array.literals_.reserve(protoArray.literals_size());
113 for (const auto &protoLiteral : protoArray.literals()) {
114 panda::pandasm::LiteralArray::Literal literal;
115 Literal::Deserialize(protoLiteral, literal);
116 array.literals_.emplace_back(literal);
117 }
118 }
119
Serialize(const panda::pandasm::LiteralArray::Literal & literal,protoPanda::Literal & protoLiteral)120 void Literal::Serialize(const panda::pandasm::LiteralArray::Literal &literal, protoPanda::Literal &protoLiteral)
121 {
122 protoLiteral.set_tag(static_cast<uint32_t>(literal.tag_));
123 auto *value = protoLiteral.mutable_value();
124 VariantValue::Serialize(literal.value_, *value);
125 }
126
Deserialize(const protoPanda::Literal & protoLiteral,panda::pandasm::LiteralArray::Literal & literal)127 void Literal::Deserialize(const protoPanda::Literal &protoLiteral, panda::pandasm::LiteralArray::Literal &literal)
128 {
129 literal.tag_ = static_cast<panda::panda_file::LiteralTag>(protoLiteral.tag());
130 VariantValue::Deserialize(protoLiteral.value(), literal.value_);
131 }
132 } // panda::proto
133