1 /*
2  * Copyright (c) 2021-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 "annotation.h"
17 
18 namespace ark::pandasm {
19 
20 // CC-OFFNXT(huge_method[C++], G.FUN.01-CPP) big switch case
InitScalarValue(const ScalarValue &scVal)21 std::unique_ptr<ScalarValue> InitScalarValue(const ScalarValue &scVal)
22 {
23     std::unique_ptr<ScalarValue> copyVal;
24     switch (scVal.GetType()) {
25         case Value::Type::U1: {
26             copyVal = std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::U1>(scVal.GetValue<uint8_t>()));
27             break;
28         }
29         case Value::Type::U8: {
30             copyVal = std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::U8>(scVal.GetValue<uint8_t>()));
31             break;
32         }
33         case Value::Type::U16: {
34             copyVal = std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::U16>(scVal.GetValue<uint16_t>()));
35             break;
36         }
37         case Value::Type::U32: {
38             copyVal = std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::U32>(scVal.GetValue<uint32_t>()));
39             break;
40         }
41         case Value::Type::U64: {
42             copyVal = std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::U64>(scVal.GetValue<uint64_t>()));
43             break;
44         }
45         case Value::Type::I8: {
46             copyVal = std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::I8>(scVal.GetValue<int8_t>()));
47             break;
48         }
49         case Value::Type::I16: {
50             copyVal = std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::I16>(scVal.GetValue<int16_t>()));
51             break;
52         }
53         case Value::Type::I32: {
54             copyVal = std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::I32>(scVal.GetValue<int32_t>()));
55             break;
56         }
57         case Value::Type::I64: {
58             copyVal = std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::I64>(scVal.GetValue<int64_t>()));
59             break;
60         }
61         case Value::Type::F32: {
62             copyVal = std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::F32>(scVal.GetValue<float>()));
63             break;
64         }
65         case Value::Type::F64: {
66             copyVal = std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::F64>(scVal.GetValue<double>()));
67             break;
68         }
69         case Value::Type::STRING: {
70             copyVal =
71                 std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::STRING>(scVal.GetValue<std::string>()));
72             break;
73         }
74         case Value::Type::STRING_NULLPTR: {
75             copyVal = std::make_unique<ScalarValue>(
76                 ScalarValue::Create<Value::Type::STRING_NULLPTR>(scVal.GetValue<int32_t>()));
77             break;
78         }
79         case Value::Type::RECORD: {
80             copyVal = std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::RECORD>(scVal.GetValue<Type>()));
81             break;
82         }
83         case Value::Type::METHOD: {
84             copyVal =
85                 std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::METHOD>(scVal.GetValue<std::string>()));
86             break;
87         }
88         case Value::Type::ENUM: {
89             copyVal =
90                 std::make_unique<ScalarValue>(ScalarValue::Create<Value::Type::ENUM>(scVal.GetValue<std::string>()));
91             break;
92         }
93         case Value::Type::ANNOTATION: {
94             copyVal = std::make_unique<ScalarValue>(
95                 ScalarValue::Create<Value::Type::ANNOTATION>(scVal.GetValue<AnnotationData>()));
96             break;
97         }
98         default: {
99             UNREACHABLE();
100             copyVal = nullptr;
101             break;
102         }
103     }
104     return copyVal;
105 }
106 
MakingValue(const AnnotationElement &annElem)107 std::unique_ptr<Value> MakingValue(const AnnotationElement &annElem)
108 {
109     std::unique_ptr<Value> copyVal;
110     switch (annElem.GetValue()->GetType()) {
111         case Value::Type::U1:
112         case Value::Type::U8:
113         case Value::Type::U16:
114         case Value::Type::U32:
115         case Value::Type::U64:
116         case Value::Type::I8:
117         case Value::Type::I16:
118         case Value::Type::I32:
119         case Value::Type::I64:
120         case Value::Type::F32:
121         case Value::Type::F64:
122         case Value::Type::STRING:
123         case Value::Type::STRING_NULLPTR:
124         case Value::Type::RECORD:
125         case Value::Type::METHOD:
126         case Value::Type::ENUM:
127         case Value::Type::ANNOTATION: {
128             copyVal = InitScalarValue(*static_cast<ScalarValue *>(annElem.GetValue()));
129             break;
130         }
131         case Value::Type::ARRAY: {
132             Value::Type cType;
133             auto *elemArr = static_cast<ArrayValue *>(annElem.GetValue());
134             if (elemArr->GetValues().empty()) {
135                 cType = Value::Type::VOID;
136             } else {
137                 cType = elemArr->GetValues().front().GetType();
138             }
139             std::vector<ScalarValue> scVals;
140             for (const auto &scVal : elemArr->GetValues()) {
141                 scVals.push_back(*InitScalarValue(scVal));
142             }
143             copyVal = std::make_unique<ArrayValue>(cType, std::move(scVals));
144             break;
145         }
146         default: {
147             UNREACHABLE();
148             copyVal = nullptr;
149             break;
150         }
151     }
152     return copyVal;
153 }
154 
AnnotationElement(const AnnotationElement &annElem)155 AnnotationElement::AnnotationElement(const AnnotationElement &annElem)
156 {
157     this->value_ = MakingValue(annElem);
158     this->name_ = annElem.GetName();
159 }
160 
operator =(const AnnotationElement &annElem)161 AnnotationElement &AnnotationElement::operator=(const AnnotationElement &annElem)
162 {
163     if (this == &annElem) {
164         return *this;
165     }
166 
167     this->value_ = MakingValue(annElem);
168     this->name_ = annElem.GetName();
169     return *this;
170 }
171 
GetAsScalar()172 ScalarValue *Value::GetAsScalar()
173 {
174     ASSERT(!IsArray());
175     return static_cast<ScalarValue *>(this);
176 }
177 
GetAsScalar() const178 const ScalarValue *Value::GetAsScalar() const
179 {
180     ASSERT(!IsArray());
181     return static_cast<const ScalarValue *>(this);
182 }
183 
GetAsArray()184 ArrayValue *Value::GetAsArray()
185 {
186     ASSERT(IsArray());
187     return static_cast<ArrayValue *>(this);
188 }
189 
GetAsArray() const190 const ArrayValue *Value::GetAsArray() const
191 {
192     ASSERT(IsArray());
193     return static_cast<const ArrayValue *>(this);
194 }
195 
196 /* static */
TypeToString(Value::Type type)197 std::string AnnotationElement::TypeToString(Value::Type type)
198 {
199     switch (type) {
200         case Value::Type::U1:
201             return "u1";
202         case Value::Type::I8:
203             return "i8";
204         case Value::Type::U8:
205             return "u8";
206         case Value::Type::I16:
207             return "i16";
208         case Value::Type::U16:
209             return "u16";
210         case Value::Type::I32:
211             return "i32";
212         case Value::Type::U32:
213             return "u32";
214         case Value::Type::I64:
215             return "i64";
216         case Value::Type::U64:
217             return "u64";
218         case Value::Type::F32:
219             return "f32";
220         case Value::Type::F64:
221             return "f64";
222         case Value::Type::STRING:
223             return "string";
224         case Value::Type::RECORD:
225             return "record";
226         case Value::Type::METHOD:
227             return "method";
228         case Value::Type::ENUM:
229             return "enum";
230         case Value::Type::ANNOTATION:
231             return "annotation";
232         case Value::Type::ARRAY:
233             return "array";
234         case Value::Type::VOID:
235             return "void";
236         default: {
237             UNREACHABLE();
238             return "unknown";
239         }
240     }
241 }
242 
243 }  // namespace ark::pandasm
244