1b1994897Sopenharmony_ci/**
2b1994897Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3b1994897Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1994897Sopenharmony_ci * you may not use this file except in compliance with the License.
5b1994897Sopenharmony_ci * You may obtain a copy of the License at
6b1994897Sopenharmony_ci *
7b1994897Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8b1994897Sopenharmony_ci *
9b1994897Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1994897Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1994897Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1994897Sopenharmony_ci * See the License for the specific language governing permissions and
13b1994897Sopenharmony_ci * limitations under the License.
14b1994897Sopenharmony_ci */
15b1994897Sopenharmony_ci
16b1994897Sopenharmony_ci#include "meta.h"
17b1994897Sopenharmony_ci
18b1994897Sopenharmony_ci#include <cstdlib>
19b1994897Sopenharmony_ci
20b1994897Sopenharmony_ci#include <algorithm>
21b1994897Sopenharmony_ci#include <limits>
22b1994897Sopenharmony_ci
23b1994897Sopenharmony_ci#include "utils/expected.h"
24b1994897Sopenharmony_ci
25b1994897Sopenharmony_cinamespace panda::pandasm {
26b1994897Sopenharmony_ci
27b1994897Sopenharmony_cistd::optional<Metadata::Error> Metadata::ValidateSize(const std::string_view &value) const
28b1994897Sopenharmony_ci{
29b1994897Sopenharmony_ci    constexpr size_t SIZE = 10;
30b1994897Sopenharmony_ci
31b1994897Sopenharmony_ci    if (!std::all_of(value.cbegin(), value.cend(), ::isdigit)) {
32b1994897Sopenharmony_ci        return Error("Unsigned integer value expected", Error::Type::INVALID_VALUE);
33b1994897Sopenharmony_ci    }
34b1994897Sopenharmony_ci
35b1994897Sopenharmony_ci    strtoul(value.data(), nullptr, SIZE);
36b1994897Sopenharmony_ci    if (errno == ERANGE) {
37b1994897Sopenharmony_ci        return Error("Value is out of range", Error::Type::INVALID_VALUE);
38b1994897Sopenharmony_ci    }
39b1994897Sopenharmony_ci
40b1994897Sopenharmony_ci    return {};
41b1994897Sopenharmony_ci}
42b1994897Sopenharmony_ci
43b1994897Sopenharmony_cibool ItemMetadata::IsForeign() const
44b1994897Sopenharmony_ci{
45b1994897Sopenharmony_ci    return GetAttribute("external");
46b1994897Sopenharmony_ci}
47b1994897Sopenharmony_ci
48b1994897Sopenharmony_cistatic panda::pandasm::Value::Type GetType(const std::string_view &value)
49b1994897Sopenharmony_ci{
50b1994897Sopenharmony_ci    using VType = panda::pandasm::Value::Type;
51b1994897Sopenharmony_ci    static std::unordered_map<std::string_view, VType> types {
52b1994897Sopenharmony_ci        {"u1", VType::U1},        {"i8", VType::I8},        {"u8", VType::U8},
53b1994897Sopenharmony_ci        {"i16", VType::I16},      {"u16", VType::U16},      {"i32", VType::I32},
54b1994897Sopenharmony_ci        {"u32", VType::U32},      {"i64", VType::I64},      {"u64", VType::U64},
55b1994897Sopenharmony_ci        {"f32", VType::F32},      {"f64", VType::F64},      {"string", VType::STRING},
56b1994897Sopenharmony_ci        {"class", VType::RECORD}, {"enum", VType::ENUM},    {"annotation", VType::ANNOTATION},
57b1994897Sopenharmony_ci        {"array", VType::ARRAY},  {"method", VType::METHOD}};
58b1994897Sopenharmony_ci
59b1994897Sopenharmony_ci    return types[value];
60b1994897Sopenharmony_ci}
61b1994897Sopenharmony_ci
62b1994897Sopenharmony_citemplate <class T>
63b1994897Sopenharmony_cistatic T ConvertFromString(const std::string_view &value, char **end)
64b1994897Sopenharmony_ci{
65b1994897Sopenharmony_ci    static_assert(std::is_integral_v<T>, "T must be integral type");
66b1994897Sopenharmony_ci
67b1994897Sopenharmony_ci    constexpr T MIN = std::numeric_limits<T>::min();
68b1994897Sopenharmony_ci    constexpr T MAX = std::numeric_limits<T>::max();
69b1994897Sopenharmony_ci
70b1994897Sopenharmony_ci    if constexpr (std::is_signed_v<T>) {
71b1994897Sopenharmony_ci        auto v = ConvertFromString<int64_t>(value, end);
72b1994897Sopenharmony_ci        if (v < MIN || v > MAX) {
73b1994897Sopenharmony_ci            errno = ERANGE;
74b1994897Sopenharmony_ci        }
75b1994897Sopenharmony_ci        return static_cast<T>(v);
76b1994897Sopenharmony_ci    }
77b1994897Sopenharmony_ci
78b1994897Sopenharmony_ci    if constexpr (!std::is_signed_v<T>) {
79b1994897Sopenharmony_ci        auto v = ConvertFromString<uint64_t>(value, end);
80b1994897Sopenharmony_ci        if (v < MIN || v > MAX) {
81b1994897Sopenharmony_ci            errno = ERANGE;
82b1994897Sopenharmony_ci        }
83b1994897Sopenharmony_ci        return static_cast<T>(v);
84b1994897Sopenharmony_ci    }
85b1994897Sopenharmony_ci}
86b1994897Sopenharmony_ci
87b1994897Sopenharmony_citemplate <>
88b1994897Sopenharmony_ciint64_t ConvertFromString(const std::string_view &value, char **end)
89b1994897Sopenharmony_ci{
90b1994897Sopenharmony_ci    return static_cast<int64_t>(strtoll(value.data(), end, 0));
91b1994897Sopenharmony_ci}
92b1994897Sopenharmony_ci
93b1994897Sopenharmony_citemplate <>
94b1994897Sopenharmony_ciuint64_t ConvertFromString(const std::string_view &value, char **end)
95b1994897Sopenharmony_ci{
96b1994897Sopenharmony_ci    return static_cast<uint64_t>(strtoull(value.data(), end, 0));
97b1994897Sopenharmony_ci}
98b1994897Sopenharmony_ci
99b1994897Sopenharmony_citemplate <>
100b1994897Sopenharmony_cifloat ConvertFromString(const std::string_view &value, char **end)
101b1994897Sopenharmony_ci{
102b1994897Sopenharmony_ci    return strtof(value.data(), end);
103b1994897Sopenharmony_ci}
104b1994897Sopenharmony_ci
105b1994897Sopenharmony_citemplate <>
106b1994897Sopenharmony_cidouble ConvertFromString(const std::string_view &value, char **end)
107b1994897Sopenharmony_ci{
108b1994897Sopenharmony_ci    return strtod(value.data(), end);
109b1994897Sopenharmony_ci}
110b1994897Sopenharmony_ci
111b1994897Sopenharmony_citemplate <class T>
112b1994897Sopenharmony_cistatic Expected<T, Metadata::Error> ConvertFromString(const std::string_view &value)
113b1994897Sopenharmony_ci{
114b1994897Sopenharmony_ci    static_assert(std::is_arithmetic_v<T>, "T must be arithmetic type");
115b1994897Sopenharmony_ci
116b1994897Sopenharmony_ci    char *end = nullptr;
117b1994897Sopenharmony_ci    auto v = ConvertFromString<T>(value, &end);
118b1994897Sopenharmony_ci
119b1994897Sopenharmony_ci    if (end != value.data() + value.length()) {
120b1994897Sopenharmony_ci        return Unexpected(Metadata::Error("Excepted integer literal", Metadata::Error::Type::INVALID_VALUE));
121b1994897Sopenharmony_ci    }
122b1994897Sopenharmony_ci
123b1994897Sopenharmony_ci    if (errno == ERANGE) {
124b1994897Sopenharmony_ci        errno = 0;
125b1994897Sopenharmony_ci        return Unexpected(Metadata::Error("Value is out of range", Metadata::Error::Type::INVALID_VALUE));
126b1994897Sopenharmony_ci    }
127b1994897Sopenharmony_ci
128b1994897Sopenharmony_ci    return static_cast<T>(v);
129b1994897Sopenharmony_ci}
130b1994897Sopenharmony_ci
131b1994897Sopenharmony_citemplate <Value::Type type, class T = ValueTypeHelperT<type>>
132b1994897Sopenharmony_cistatic Expected<ScalarValue, Metadata::Error> CreatePrimitiveValue(const std::string_view &value,
133b1994897Sopenharmony_ci                                                                   T max_value = std::numeric_limits<T>::max())
134b1994897Sopenharmony_ci{
135b1994897Sopenharmony_ci    auto res = ConvertFromString<T>(value);
136b1994897Sopenharmony_ci    if (!res) {
137b1994897Sopenharmony_ci        return Unexpected(res.Error());
138b1994897Sopenharmony_ci    }
139b1994897Sopenharmony_ci
140b1994897Sopenharmony_ci    auto converted = res.Value();
141b1994897Sopenharmony_ci    if (converted > max_value) {
142b1994897Sopenharmony_ci        return Unexpected(Metadata::Error("Value is out of range", Metadata::Error::Type::INVALID_VALUE));
143b1994897Sopenharmony_ci    }
144b1994897Sopenharmony_ci
145b1994897Sopenharmony_ci    return ScalarValue::Create<type>(converted);
146b1994897Sopenharmony_ci}
147b1994897Sopenharmony_ci
148b1994897Sopenharmony_cistatic Expected<ScalarValue, Metadata::Error> CreateValue(
149b1994897Sopenharmony_ci    Value::Type type, const std::string_view &value,
150b1994897Sopenharmony_ci    const std::unordered_map<std::string, std::unique_ptr<AnnotationData>> &annotation_id_map = {})
151b1994897Sopenharmony_ci{
152b1994897Sopenharmony_ci    switch (type) {
153b1994897Sopenharmony_ci        case Value::Type::U1: {
154b1994897Sopenharmony_ci            return CreatePrimitiveValue<Value::Type::U1>(value, 1);
155b1994897Sopenharmony_ci        }
156b1994897Sopenharmony_ci        case Value::Type::I8: {
157b1994897Sopenharmony_ci            return CreatePrimitiveValue<Value::Type::I8>(value);
158b1994897Sopenharmony_ci        }
159b1994897Sopenharmony_ci        case Value::Type::U8: {
160b1994897Sopenharmony_ci            return CreatePrimitiveValue<Value::Type::U8>(value);
161b1994897Sopenharmony_ci        }
162b1994897Sopenharmony_ci        case Value::Type::I16: {
163b1994897Sopenharmony_ci            return CreatePrimitiveValue<Value::Type::I16>(value);
164b1994897Sopenharmony_ci        }
165b1994897Sopenharmony_ci        case Value::Type::U16: {
166b1994897Sopenharmony_ci            return CreatePrimitiveValue<Value::Type::U16>(value);
167b1994897Sopenharmony_ci        }
168b1994897Sopenharmony_ci        case Value::Type::I32: {
169b1994897Sopenharmony_ci            return CreatePrimitiveValue<Value::Type::I32>(value);
170b1994897Sopenharmony_ci        }
171b1994897Sopenharmony_ci        case Value::Type::U32: {
172b1994897Sopenharmony_ci            return CreatePrimitiveValue<Value::Type::U32>(value);
173b1994897Sopenharmony_ci        }
174b1994897Sopenharmony_ci        case Value::Type::I64: {
175b1994897Sopenharmony_ci            return CreatePrimitiveValue<Value::Type::I64>(value);
176b1994897Sopenharmony_ci        }
177b1994897Sopenharmony_ci        case Value::Type::U64: {
178b1994897Sopenharmony_ci            return CreatePrimitiveValue<Value::Type::U64>(value);
179b1994897Sopenharmony_ci        }
180b1994897Sopenharmony_ci        case Value::Type::F32: {
181b1994897Sopenharmony_ci            return CreatePrimitiveValue<Value::Type::F32>(value);
182b1994897Sopenharmony_ci        }
183b1994897Sopenharmony_ci        case Value::Type::F64: {
184b1994897Sopenharmony_ci            return CreatePrimitiveValue<Value::Type::F64>(value);
185b1994897Sopenharmony_ci        }
186b1994897Sopenharmony_ci        case Value::Type::STRING: {
187b1994897Sopenharmony_ci            return ScalarValue::Create<Value::Type::STRING>(value);
188b1994897Sopenharmony_ci        }
189b1994897Sopenharmony_ci        case Value::Type::RECORD: {
190b1994897Sopenharmony_ci            return ScalarValue::Create<Value::Type::RECORD>(Type::FromName(value));
191b1994897Sopenharmony_ci        }
192b1994897Sopenharmony_ci        case Value::Type::METHOD: {
193b1994897Sopenharmony_ci            return ScalarValue::Create<Value::Type::METHOD>(value);
194b1994897Sopenharmony_ci        }
195b1994897Sopenharmony_ci        case Value::Type::ENUM: {
196b1994897Sopenharmony_ci            return ScalarValue::Create<Value::Type::ENUM>(value);
197b1994897Sopenharmony_ci        }
198b1994897Sopenharmony_ci        case Value::Type::ANNOTATION: {
199b1994897Sopenharmony_ci            auto it = annotation_id_map.find(std::string(value));
200b1994897Sopenharmony_ci            if (it == annotation_id_map.cend()) {
201b1994897Sopenharmony_ci                return Unexpected(Metadata::Error("Unknown annotation id", Metadata::Error::Type::INVALID_VALUE));
202b1994897Sopenharmony_ci            }
203b1994897Sopenharmony_ci
204b1994897Sopenharmony_ci            auto annotation_value = it->second.get();
205b1994897Sopenharmony_ci            return ScalarValue::Create<Value::Type::ANNOTATION>(*annotation_value);
206b1994897Sopenharmony_ci        }
207b1994897Sopenharmony_ci        default: {
208b1994897Sopenharmony_ci            break;
209b1994897Sopenharmony_ci        }
210b1994897Sopenharmony_ci    }
211b1994897Sopenharmony_ci
212b1994897Sopenharmony_ci    UNREACHABLE();
213b1994897Sopenharmony_ci}
214b1994897Sopenharmony_ci
215b1994897Sopenharmony_cistd::optional<Metadata::Error> AnnotationMetadata::AnnotationElementBuilder::AddValue(
216b1994897Sopenharmony_ci    const std::string_view &value,
217b1994897Sopenharmony_ci    const std::unordered_map<std::string, std::unique_ptr<AnnotationData>> &annotation_id_map)
218b1994897Sopenharmony_ci{
219b1994897Sopenharmony_ci    ASSERT(type_.has_value());
220b1994897Sopenharmony_ci
221b1994897Sopenharmony_ci    auto type = type_.value();
222b1994897Sopenharmony_ci    if (type == Value::Type::ARRAY) {
223b1994897Sopenharmony_ci        ASSERT(component_type_.has_value());
224b1994897Sopenharmony_ci        type = component_type_.value();
225b1994897Sopenharmony_ci    }
226b1994897Sopenharmony_ci
227b1994897Sopenharmony_ci    auto res = CreateValue(type, value, annotation_id_map);
228b1994897Sopenharmony_ci    if (!res) {
229b1994897Sopenharmony_ci        return res.Error();
230b1994897Sopenharmony_ci    }
231b1994897Sopenharmony_ci
232b1994897Sopenharmony_ci    values_.push_back(res.Value());
233b1994897Sopenharmony_ci
234b1994897Sopenharmony_ci    return {};
235b1994897Sopenharmony_ci}
236b1994897Sopenharmony_ci
237b1994897Sopenharmony_cistd::optional<Metadata::Error> AnnotationMetadata::Store(const std::string_view &attribute)
238b1994897Sopenharmony_ci{
239b1994897Sopenharmony_ci    if (IsParseAnnotationElement() && !annotation_element_builder_.IsCompleted()) {
240b1994897Sopenharmony_ci        return Error(std::string("Unexpected attribute '").append(attribute) +
241b1994897Sopenharmony_ci                         "'. Annotation element isn't completely defined",
242b1994897Sopenharmony_ci                     Error::Type::UNEXPECTED_ATTRIBUTE);
243b1994897Sopenharmony_ci    }
244b1994897Sopenharmony_ci
245b1994897Sopenharmony_ci    if (IsParseAnnotation()) {
246b1994897Sopenharmony_ci        ResetAnnotationBuilder();
247b1994897Sopenharmony_ci    }
248b1994897Sopenharmony_ci
249b1994897Sopenharmony_ci    return Metadata::Store(attribute);
250b1994897Sopenharmony_ci}
251b1994897Sopenharmony_ci
252b1994897Sopenharmony_cistd::optional<Metadata::Error> AnnotationMetadata::MeetExpRecordAttribute(const std::string_view &attribute,
253b1994897Sopenharmony_ci                                                                          const std::string_view &value)
254b1994897Sopenharmony_ci{
255b1994897Sopenharmony_ci    if (IsParseAnnotationElement() && !annotation_element_builder_.IsCompleted()) {
256b1994897Sopenharmony_ci        return Error(std::string("Unexpected attribute '").append(attribute) +
257b1994897Sopenharmony_ci                         "'. Annotation element isn't completely defined",
258b1994897Sopenharmony_ci                     Error::Type::UNEXPECTED_ATTRIBUTE);
259b1994897Sopenharmony_ci    }
260b1994897Sopenharmony_ci
261b1994897Sopenharmony_ci    InitializeAnnotationBuilder(value);
262b1994897Sopenharmony_ci
263b1994897Sopenharmony_ci    return {};
264b1994897Sopenharmony_ci}
265b1994897Sopenharmony_ci
266b1994897Sopenharmony_cistd::optional<Metadata::Error> AnnotationMetadata::MeetExpIdAttribute(const std::string_view &attribute,
267b1994897Sopenharmony_ci                                                                      const std::string_view &value)
268b1994897Sopenharmony_ci{
269b1994897Sopenharmony_ci    if (!IsParseAnnotation() || IsParseAnnotationElement()) {
270b1994897Sopenharmony_ci        return Error(std::string("Unexpected attribute '").append(attribute) +
271b1994897Sopenharmony_ci                         "'. Annotation record attribute must be defined first",
272b1994897Sopenharmony_ci                     Error::Type::UNEXPECTED_ATTRIBUTE);
273b1994897Sopenharmony_ci    }
274b1994897Sopenharmony_ci
275b1994897Sopenharmony_ci    if (annotation_builder_.HasId()) {
276b1994897Sopenharmony_ci        return Error(std::string("Unexpected attribute '").append(attribute) +
277b1994897Sopenharmony_ci                         "'. Annotation id attribute already defined",
278b1994897Sopenharmony_ci                     Error::Type::UNEXPECTED_ATTRIBUTE);
279b1994897Sopenharmony_ci    }
280b1994897Sopenharmony_ci
281b1994897Sopenharmony_ci    annotation_builder_.SetId(value);
282b1994897Sopenharmony_ci
283b1994897Sopenharmony_ci    return {};
284b1994897Sopenharmony_ci}
285b1994897Sopenharmony_ci
286b1994897Sopenharmony_cistd::optional<Metadata::Error> AnnotationMetadata::MeetExpElementNameAttribute(const std::string_view &attribute,
287b1994897Sopenharmony_ci                                                                               const std::string_view &value)
288b1994897Sopenharmony_ci{
289b1994897Sopenharmony_ci    if (!IsParseAnnotation()) {
290b1994897Sopenharmony_ci        return Error(std::string("Unexpected attribute '").append(attribute) +
291b1994897Sopenharmony_ci                         "'. Annotation record attribute must be defined first",
292b1994897Sopenharmony_ci                     Error::Type::UNEXPECTED_ATTRIBUTE);
293b1994897Sopenharmony_ci    }
294b1994897Sopenharmony_ci
295b1994897Sopenharmony_ci    if (IsParseAnnotationElement() && !annotation_element_builder_.IsCompleted()) {
296b1994897Sopenharmony_ci        return Error(std::string("Unexpected attribute '").append(attribute) +
297b1994897Sopenharmony_ci                         "'. Previous annotation element isn't defined completely",
298b1994897Sopenharmony_ci                     Error::Type::UNEXPECTED_ATTRIBUTE);
299b1994897Sopenharmony_ci    }
300b1994897Sopenharmony_ci
301b1994897Sopenharmony_ci    InitializeAnnotationElementBuilder(value);
302b1994897Sopenharmony_ci
303b1994897Sopenharmony_ci    return {};
304b1994897Sopenharmony_ci}
305b1994897Sopenharmony_ci
306b1994897Sopenharmony_cistd::optional<Metadata::Error> AnnotationMetadata::MeetExpElementTypeAttribute(
307b1994897Sopenharmony_ci    const std::string_view &attribute, const std::string_view &value)
308b1994897Sopenharmony_ci{
309b1994897Sopenharmony_ci    if (!IsParseAnnotationElement()) {
310b1994897Sopenharmony_ci        return Error(std::string("Unexpected attribute '").append(attribute) +
311b1994897Sopenharmony_ci                         "'. Annotation element name attribute must be defined first",
312b1994897Sopenharmony_ci                     Error::Type::UNEXPECTED_ATTRIBUTE);
313b1994897Sopenharmony_ci    }
314b1994897Sopenharmony_ci
315b1994897Sopenharmony_ci    if (annotation_element_builder_.IsTypeSet()) {
316b1994897Sopenharmony_ci        return Error(std::string("Unexpected attribute '").append(attribute) +
317b1994897Sopenharmony_ci                         "'. Annotation element type attribute already defined",
318b1994897Sopenharmony_ci                     Error::Type::UNEXPECTED_ATTRIBUTE);
319b1994897Sopenharmony_ci    }
320b1994897Sopenharmony_ci
321b1994897Sopenharmony_ci    annotation_element_builder_.SetType(GetType(value));
322b1994897Sopenharmony_ci
323b1994897Sopenharmony_ci    return {};
324b1994897Sopenharmony_ci}
325b1994897Sopenharmony_ci
326b1994897Sopenharmony_cistd::optional<Metadata::Error> AnnotationMetadata::MeetExpElementArrayComponentTypeAttribute(
327b1994897Sopenharmony_ci    const std::string_view &attribute, const std::string_view &value)
328b1994897Sopenharmony_ci{
329b1994897Sopenharmony_ci    if (!IsParseAnnotationElement()) {
330b1994897Sopenharmony_ci        return Error(std::string("Unexpected attribute '").append(attribute) +
331b1994897Sopenharmony_ci                         "'. Annotation element name attribute must be defined first",
332b1994897Sopenharmony_ci                     Error::Type::UNEXPECTED_ATTRIBUTE);
333b1994897Sopenharmony_ci    }
334b1994897Sopenharmony_ci
335b1994897Sopenharmony_ci    if (!annotation_element_builder_.IsArray()) {
336b1994897Sopenharmony_ci        return Error(std::string("Unexpected attribute '").append(attribute) + "'. Annotation element type isn't array",
337b1994897Sopenharmony_ci                     Error::Type::UNEXPECTED_ATTRIBUTE);
338b1994897Sopenharmony_ci    }
339b1994897Sopenharmony_ci
340b1994897Sopenharmony_ci    if (annotation_element_builder_.IsComponentTypeSet()) {
341b1994897Sopenharmony_ci        return Error(std::string("Unexpected attribute '").append(attribute) +
342b1994897Sopenharmony_ci                         "'. Annotation element array component type attribute already defined",
343b1994897Sopenharmony_ci                     Error::Type::UNEXPECTED_ATTRIBUTE);
344b1994897Sopenharmony_ci    }
345b1994897Sopenharmony_ci
346b1994897Sopenharmony_ci    annotation_element_builder_.SetComponentType(GetType(value));
347b1994897Sopenharmony_ci
348b1994897Sopenharmony_ci    return {};
349b1994897Sopenharmony_ci}
350b1994897Sopenharmony_ci
351b1994897Sopenharmony_cistd::optional<Metadata::Error> AnnotationMetadata::MeetExpElementValueAttribute(const std::string_view &attribute,
352b1994897Sopenharmony_ci                                                                                const std::string_view &value)
353b1994897Sopenharmony_ci{
354b1994897Sopenharmony_ci    if (!IsParseAnnotationElement()) {
355b1994897Sopenharmony_ci        return Error(std::string("Unexpected attribute '").append(attribute) +
356b1994897Sopenharmony_ci                         "'. Annotation element name attribute must be defined first",
357b1994897Sopenharmony_ci                     Error::Type::UNEXPECTED_ATTRIBUTE);
358b1994897Sopenharmony_ci    }
359b1994897Sopenharmony_ci
360b1994897Sopenharmony_ci    if (!annotation_element_builder_.IsTypeSet()) {
361b1994897Sopenharmony_ci        return Error(std::string("Unexpected attribute '").append(attribute) +
362b1994897Sopenharmony_ci                         "'. Annotation element type attribute isn't defined",
363b1994897Sopenharmony_ci                     Error::Type::UNEXPECTED_ATTRIBUTE);
364b1994897Sopenharmony_ci    }
365b1994897Sopenharmony_ci
366b1994897Sopenharmony_ci    if (annotation_element_builder_.IsArray() && !annotation_element_builder_.IsComponentTypeSet()) {
367b1994897Sopenharmony_ci        return Error(std::string("Unexpected attribute '").append(attribute) +
368b1994897Sopenharmony_ci                         "'. Annotation element array component type attribute isn't defined",
369b1994897Sopenharmony_ci                     Error::Type::UNEXPECTED_ATTRIBUTE);
370b1994897Sopenharmony_ci    }
371b1994897Sopenharmony_ci
372b1994897Sopenharmony_ci    if (!annotation_element_builder_.IsArray() && annotation_element_builder_.IsCompleted()) {
373b1994897Sopenharmony_ci        return Error(std::string("Unexpected attribute '").append(attribute) +
374b1994897Sopenharmony_ci                         "'. Annotation element is completely defined",
375b1994897Sopenharmony_ci                     Error::Type::UNEXPECTED_ATTRIBUTE);
376b1994897Sopenharmony_ci    }
377b1994897Sopenharmony_ci
378b1994897Sopenharmony_ci    return annotation_element_builder_.AddValue(value, id_map_);
379b1994897Sopenharmony_ci}
380b1994897Sopenharmony_ci
381b1994897Sopenharmony_cistd::optional<Metadata::Error> AnnotationMetadata::StoreValue(const std::string_view &attribute,
382b1994897Sopenharmony_ci                                                              const std::string_view &value)
383b1994897Sopenharmony_ci{
384b1994897Sopenharmony_ci    auto err = Metadata::StoreValue(attribute, value);
385b1994897Sopenharmony_ci    if (err) {
386b1994897Sopenharmony_ci        return err;
387b1994897Sopenharmony_ci    }
388b1994897Sopenharmony_ci
389b1994897Sopenharmony_ci    if (IsAnnotationRecordAttribute(attribute)) {
390b1994897Sopenharmony_ci        return MeetExpRecordAttribute(attribute, value);
391b1994897Sopenharmony_ci    }
392b1994897Sopenharmony_ci
393b1994897Sopenharmony_ci    if (IsAnnotationIdAttribute(attribute)) {
394b1994897Sopenharmony_ci        return MeetExpIdAttribute(attribute, value);
395b1994897Sopenharmony_ci    }
396b1994897Sopenharmony_ci
397b1994897Sopenharmony_ci    if (IsAnnotationElementNameAttribute(attribute)) {
398b1994897Sopenharmony_ci        return MeetExpElementNameAttribute(attribute, value);
399b1994897Sopenharmony_ci    }
400b1994897Sopenharmony_ci
401b1994897Sopenharmony_ci    if (IsAnnotationElementTypeAttribute(attribute)) {
402b1994897Sopenharmony_ci        return MeetExpElementTypeAttribute(attribute, value);
403b1994897Sopenharmony_ci    }
404b1994897Sopenharmony_ci
405b1994897Sopenharmony_ci    if (IsAnnotationElementArrayComponentTypeAttribute(attribute)) {
406b1994897Sopenharmony_ci        return MeetExpElementArrayComponentTypeAttribute(attribute, value);
407b1994897Sopenharmony_ci    }
408b1994897Sopenharmony_ci
409b1994897Sopenharmony_ci    if (IsAnnotationElementValueAttribute(attribute)) {
410b1994897Sopenharmony_ci        return MeetExpElementValueAttribute(attribute, value);
411b1994897Sopenharmony_ci    }
412b1994897Sopenharmony_ci
413b1994897Sopenharmony_ci    if (IsParseAnnotationElement() && !annotation_element_builder_.IsCompleted()) {
414b1994897Sopenharmony_ci        return Error(std::string("Unexpected attribute '").append(attribute) +
415b1994897Sopenharmony_ci                         "'. Annotation element isn't completely defined",
416b1994897Sopenharmony_ci                     Error::Type::UNEXPECTED_ATTRIBUTE);
417b1994897Sopenharmony_ci    }
418b1994897Sopenharmony_ci
419b1994897Sopenharmony_ci    if (IsParseAnnotation()) {
420b1994897Sopenharmony_ci        ResetAnnotationBuilder();
421b1994897Sopenharmony_ci    }
422b1994897Sopenharmony_ci
423b1994897Sopenharmony_ci    return {};
424b1994897Sopenharmony_ci}
425b1994897Sopenharmony_ci
426b1994897Sopenharmony_cistd::optional<Metadata::Error> AnnotationMetadata::ValidateData()
427b1994897Sopenharmony_ci{
428b1994897Sopenharmony_ci    if (IsParseAnnotationElement() && !annotation_element_builder_.IsCompleted()) {
429b1994897Sopenharmony_ci        return Error("Annotation element isn't completely defined", Error::Type::MISSING_ATTRIBUTE);
430b1994897Sopenharmony_ci    }
431b1994897Sopenharmony_ci
432b1994897Sopenharmony_ci    if (IsParseAnnotation()) {
433b1994897Sopenharmony_ci        ResetAnnotationBuilder();
434b1994897Sopenharmony_ci    }
435b1994897Sopenharmony_ci
436b1994897Sopenharmony_ci    return Metadata::ValidateData();
437b1994897Sopenharmony_ci}
438b1994897Sopenharmony_ci
439b1994897Sopenharmony_cistd::string RecordMetadata::GetBase() const
440b1994897Sopenharmony_ci{
441b1994897Sopenharmony_ci    return "";
442b1994897Sopenharmony_ci}
443b1994897Sopenharmony_ci
444b1994897Sopenharmony_cistd::vector<std::string> RecordMetadata::GetInterfaces() const
445b1994897Sopenharmony_ci{
446b1994897Sopenharmony_ci    return {};
447b1994897Sopenharmony_ci}
448b1994897Sopenharmony_ci
449b1994897Sopenharmony_cibool RecordMetadata::IsAnnotation() const
450b1994897Sopenharmony_ci{
451b1994897Sopenharmony_ci    return false;
452b1994897Sopenharmony_ci}
453b1994897Sopenharmony_ci
454b1994897Sopenharmony_cibool RecordMetadata::IsRuntimeAnnotation() const
455b1994897Sopenharmony_ci{
456b1994897Sopenharmony_ci    return false;
457b1994897Sopenharmony_ci}
458b1994897Sopenharmony_ci
459b1994897Sopenharmony_cibool RecordMetadata::IsTypeAnnotation() const
460b1994897Sopenharmony_ci{
461b1994897Sopenharmony_ci    return false;
462b1994897Sopenharmony_ci}
463b1994897Sopenharmony_ci
464b1994897Sopenharmony_cibool RecordMetadata::IsRuntimeTypeAnnotation() const
465b1994897Sopenharmony_ci{
466b1994897Sopenharmony_ci    return false;
467b1994897Sopenharmony_ci}
468b1994897Sopenharmony_ci
469b1994897Sopenharmony_cibool FunctionMetadata::IsCtor() const
470b1994897Sopenharmony_ci{
471b1994897Sopenharmony_ci    return GetAttribute("ctor");
472b1994897Sopenharmony_ci}
473b1994897Sopenharmony_ci
474b1994897Sopenharmony_cibool FunctionMetadata::IsCctor() const
475b1994897Sopenharmony_ci{
476b1994897Sopenharmony_ci    return GetAttribute("cctor");
477b1994897Sopenharmony_ci}
478b1994897Sopenharmony_ci
479b1994897Sopenharmony_cistd::optional<Metadata::Error> FieldMetadata::StoreValue(const std::string_view &attribute,
480b1994897Sopenharmony_ci                                                         const std::string_view &value)
481b1994897Sopenharmony_ci{
482b1994897Sopenharmony_ci    auto err = ItemMetadata::StoreValue(attribute, value);
483b1994897Sopenharmony_ci    if (err) {
484b1994897Sopenharmony_ci        return err;
485b1994897Sopenharmony_ci    }
486b1994897Sopenharmony_ci
487b1994897Sopenharmony_ci    if (IsValueAttribute(attribute)) {
488b1994897Sopenharmony_ci        Value::Type value_type;
489b1994897Sopenharmony_ci        if (!field_type_.IsObject()) {
490b1994897Sopenharmony_ci            value_type = GetType(field_type_.GetName());
491b1994897Sopenharmony_ci        } else {
492b1994897Sopenharmony_ci            value_type = Value::Type::STRING;
493b1994897Sopenharmony_ci        }
494b1994897Sopenharmony_ci
495b1994897Sopenharmony_ci        auto res = CreateValue(value_type, value);
496b1994897Sopenharmony_ci        if (!res) {
497b1994897Sopenharmony_ci            return res.Error();
498b1994897Sopenharmony_ci        }
499b1994897Sopenharmony_ci
500b1994897Sopenharmony_ci        value_ = res.Value();
501b1994897Sopenharmony_ci    }
502b1994897Sopenharmony_ci
503b1994897Sopenharmony_ci    return {};
504b1994897Sopenharmony_ci}
505b1994897Sopenharmony_ci
506b1994897Sopenharmony_ci#include <meta_gen.h>
507b1994897Sopenharmony_ci
508b1994897Sopenharmony_ci}  // namespace panda::pandasm
509